feat: add user_id index to profiles table for faster lookups
The profiles table previously lacked an index on the user_id column, causing full table scans during user lookups. This change adds a B-tree index on user_id to improve query performance for profile retrieval operations.
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
|
||||
from ..agent import (
|
||||
tool,
|
||||
react_loop,
|
||||
AgentState,
|
||||
get_tool,
|
||||
get_tool_payloads,
|
||||
MarkdownRenderer,
|
||||
close_http_client,
|
||||
_with_datetime,
|
||||
)
|
||||
|
||||
REPORTS_DIR = Path(__file__).resolve().parent.parent / "reports"
|
||||
|
||||
WRITE_TOOLS = ("create_file", "write_file", "edit_file", "patch_file")
|
||||
WEB_TOOLS = (
|
||||
"web_search",
|
||||
"deep_search",
|
||||
"ai_search",
|
||||
"fetch_url",
|
||||
"download_file",
|
||||
"download_files",
|
||||
"describe_image",
|
||||
)
|
||||
SWARM_TOOLS = (
|
||||
"spawn",
|
||||
"swarm_wait",
|
||||
"swarm_result",
|
||||
"swarm_tail",
|
||||
"swarm_kill",
|
||||
"swarm_cleanup",
|
||||
)
|
||||
SEVERITIES = ("error", "warning", "info")
|
||||
|
||||
_FINDINGS: list[dict[str, Any]] = []
|
||||
|
||||
|
||||
def reset_findings() -> None:
|
||||
_FINDINGS.clear()
|
||||
|
||||
|
||||
def findings() -> list[dict[str, Any]]:
|
||||
return list(_FINDINGS)
|
||||
|
||||
|
||||
@tool
|
||||
async def report_finding(
|
||||
dimension: str,
|
||||
severity: str,
|
||||
message: str,
|
||||
file: str = "",
|
||||
line: int = 0,
|
||||
rule: str = "",
|
||||
fixed: bool = False,
|
||||
fix_summary: str = "",
|
||||
):
|
||||
"""Record one maintenance finding for the run report. Call once per confirmed issue.
|
||||
dimension: The check that produced this finding.
|
||||
severity: One of error, warning, info.
|
||||
message: Human-readable description of the issue.
|
||||
file: Repository-relative path of the offending file.
|
||||
line: 1-based line number when known, else 0.
|
||||
rule: Short machine rule id, e.g. mutation-without-record.
|
||||
fixed: True if this run already applied a fix.
|
||||
fix_summary: One line describing the fix when fixed is true.
|
||||
"""
|
||||
severity_value = severity if severity in SEVERITIES else "warning"
|
||||
_FINDINGS.append(
|
||||
{
|
||||
"severity": severity_value,
|
||||
"dimension": dimension,
|
||||
"file": file,
|
||||
"line": int(line) if line else 0,
|
||||
"rule": rule,
|
||||
"message": message,
|
||||
"fixed": bool(fixed),
|
||||
"fix_summary": fix_summary,
|
||||
}
|
||||
)
|
||||
return json.dumps({"status": "success", "recorded": True, "total_findings": len(_FINDINGS)})
|
||||
|
||||
|
||||
def payloads_for(mode: str) -> list[dict[str, Any]]:
|
||||
exclude = WEB_TOOLS + SWARM_TOOLS
|
||||
if mode == "check":
|
||||
exclude = exclude + WRITE_TOOLS + ("verify",)
|
||||
return get_tool_payloads(exclude=exclude)
|
||||
|
||||
|
||||
def payloads_named(names: tuple[str, ...]) -> list[dict[str, Any]]:
|
||||
out: list[dict[str, Any]] = []
|
||||
for name in names:
|
||||
func = get_tool(name)
|
||||
if func is not None:
|
||||
out.append(func._tool_payload)
|
||||
return out
|
||||
|
||||
|
||||
def summarize(items: list[dict[str, Any]]) -> dict[str, int]:
|
||||
errors = sum(1 for f in items if f["severity"] == "error")
|
||||
warnings = sum(1 for f in items if f["severity"] == "warning")
|
||||
fixed = sum(1 for f in items if f["fixed"])
|
||||
return {
|
||||
"total": len(items),
|
||||
"errors": errors,
|
||||
"warnings": warnings,
|
||||
"fixed": fixed,
|
||||
"unfixed": len(items) - fixed,
|
||||
}
|
||||
|
||||
|
||||
def _md_report(name: str, mode: str, summary: dict[str, int], items: list[dict[str, Any]]) -> str:
|
||||
lines = [
|
||||
f"# {name} report ({mode})",
|
||||
"",
|
||||
f"- total: {summary['total']}",
|
||||
f"- errors: {summary['errors']}",
|
||||
f"- warnings: {summary['warnings']}",
|
||||
f"- fixed: {summary['fixed']}",
|
||||
f"- unfixed: {summary['unfixed']}",
|
||||
"",
|
||||
"## Findings",
|
||||
"",
|
||||
]
|
||||
if not items:
|
||||
lines.append("None.")
|
||||
return "\n".join(lines)
|
||||
by_file: dict[str, list[dict[str, Any]]] = {}
|
||||
for finding in items:
|
||||
by_file.setdefault(finding["file"] or "(repository)", []).append(finding)
|
||||
for path, group in sorted(by_file.items()):
|
||||
lines.append(f"### {path}")
|
||||
for finding in group:
|
||||
location = f":{finding['line']}" if finding["line"] else ""
|
||||
mark = "fixed" if finding["fixed"] else finding["severity"]
|
||||
label = finding["rule"] or finding["dimension"]
|
||||
lines.append(f"- [{mark}] {label}{location}: {finding['message']}")
|
||||
if finding["fixed"] and finding["fix_summary"]:
|
||||
lines.append(f" fix: {finding['fix_summary']}")
|
||||
lines.append("")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def write_reports(
|
||||
name: str,
|
||||
mode: str,
|
||||
started: datetime,
|
||||
finished: datetime,
|
||||
cost: Optional[dict[str, Any]] = None,
|
||||
) -> dict[str, Any]:
|
||||
REPORTS_DIR.mkdir(parents=True, exist_ok=True)
|
||||
items = findings()
|
||||
summary = summarize(items)
|
||||
stamp = started.strftime("%Y%m%d-%H%M%S")
|
||||
base = REPORTS_DIR / f"{name}-{stamp}"
|
||||
payload = {
|
||||
"agent": name,
|
||||
"mode": mode,
|
||||
"started_at": started.isoformat(),
|
||||
"finished_at": finished.isoformat(),
|
||||
"summary": summary,
|
||||
"cost": cost or {},
|
||||
"findings": items,
|
||||
}
|
||||
base.with_suffix(".json").write_text(json.dumps(payload, indent=2), encoding="utf-8")
|
||||
base.with_suffix(".md").write_text(_md_report(name, mode, summary, items), encoding="utf-8")
|
||||
return {"summary": summary, "json": str(base.with_suffix(".json")), "items": items}
|
||||
|
||||
|
||||
__all__ = [
|
||||
"tool",
|
||||
"react_loop",
|
||||
"AgentState",
|
||||
"MarkdownRenderer",
|
||||
"close_http_client",
|
||||
"_with_datetime",
|
||||
"report_finding",
|
||||
"reset_findings",
|
||||
"findings",
|
||||
"payloads_for",
|
||||
"payloads_named",
|
||||
"summarize",
|
||||
"write_reports",
|
||||
"REPORTS_DIR",
|
||||
]
|
||||
@@ -0,0 +1,13 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import ( # noqa: F401
|
||||
cost_session_total,
|
||||
cost_record,
|
||||
set_cost_stream,
|
||||
format_usd,
|
||||
usd_str,
|
||||
PER_MILLION,
|
||||
PRICE_CACHE_HIT_PER_M,
|
||||
PRICE_CACHE_MISS_PER_M,
|
||||
PRICE_OUTPUT_PER_M,
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import run_command, verify, stream_subprocess # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import read_file, read_lines, write_file, create_file, edit_file, patch_file, list_dir, glob_files, grep, find_symbol # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import Backend, get_backends, vision_available, llm_call, _call_backend, MODEL, DEEPSEEK_MODEL # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import AgentState, react_loop, compact_messages, context_size, find_compaction_split # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import SYSTEM_PROMPT, SUB_AGENT_SYSTEM_PROMPT, system_message, _with_datetime # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import MarkdownRenderer # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import run_once, interactive, amain, main # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import delegate, spawn, swarm_wait, swarm_result, swarm_tail, swarm_kill, swarm_cleanup, retrieve, CorpusIndex, get_corpus_index # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import tool, get_tool, get_tool_payloads, execute_tool_call, coerce_tool_args, validate_tool_args, _build_function_payload # noqa: F401
|
||||
@@ -0,0 +1,3 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from ..agent import web_search, deep_search, ai_search, fetch_url, download_file, download_files, describe_image # noqa: F401
|
||||
Reference in New Issue
Block a user