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:
2026-06-12 04:30:08 +00:00
parent bb82b7c6e6
commit 9a8ed464d7
38 changed files with 679 additions and 86 deletions
+32 -1
View File
@@ -1312,8 +1312,39 @@ def tool(func: Callable[..., Any]) -> Callable[..., Any]:
return wrapper
_orchestration_tools: set[str] = set()
_active_tool_scope: contextvars.ContextVar[Optional[frozenset]] = contextvars.ContextVar("active_tool_scope", default=None)
def mark_orchestration_tools(*names: str) -> None:
for name in names:
func = _registry.get(name)
if func is not None:
func._orchestration = True # type: ignore[attr-defined]
_orchestration_tools.add(name)
def set_tool_scope(names: Optional[tuple[str, ...]]) -> Any:
scope = None if names is None else frozenset(names)
return _active_tool_scope.set(scope)
def reset_tool_scope(token: Any) -> None:
_active_tool_scope.reset(token)
def get_tool_payloads(exclude: tuple[str, ...] = ()) -> list[dict[str, Any]]:
return [f._tool_payload for n, f in _registry.items() if n not in exclude]
scope = _active_tool_scope.get()
out: list[dict[str, Any]] = []
for name, func in _registry.items():
if name in exclude:
continue
if getattr(func, "_orchestration", False):
continue
if scope is not None and name not in scope:
continue
out.append(func._tool_payload)
return out
def get_tool(name: str) -> Optional[Callable[..., Any]]:
+6
View File
@@ -21,7 +21,9 @@ from .agent import (
install_timestamps,
protect_agents,
react_loop,
reset_tool_scope,
set_shell_restricted,
set_tool_scope,
set_write_budget,
usd_str,
_with_datetime,
@@ -49,6 +51,8 @@ MAINT_HEADER = """You are {name}, an autonomous maintenance agent for the DevPla
ABSOLUTE EXCLUSION (NON-NEGOTIABLE): The `agents/` directory is the maintenance fleet's OWN source code. You MUST NOT read it for analysis, grep it, scan it, report on it, or modify it under any circumstances. It deliberately contains the very patterns you hunt for (em-dash characters, forbidden-name examples, destructive-command strings, HTML entities) as DETECTION DATA, not as violations. A "violation" found in `agents/` is never real. Always exclude `agents/` from every grep and glob (the search root is the application code, not the tooling). The engine also hard-blocks any write under `agents/`, so an attempt to fix something there will fail by design. Treat `agents/` as if it does not exist.
REPOSITORY LAYOUT (do NOT waste tool calls rediscovering this): all application code lives under the `devplacepy/` package. There is NO top-level `static/`, `routers/`, `templates/`, or `services/` directory; they are `devplacepy/static/` (with `js/`, `css/`, `vendor/`), `devplacepy/routers/`, `devplacepy/templates/`, and `devplacepy/services/`. Tests live in the top-level `tests/`. Packaging is the top-level `pyproject.toml` and `Makefile`. Start your investigation directly inside `devplacepy/` (and `tests/` for the test dimension); never probe top-level `static`/`routers` first.
OPERATING PROTOCOL
1. PLAN FIRST. Your VERY FIRST tool call MUST be plan() with goal, steps, success_criteria, and confidence.
2. INVESTIGATE BEFORE CONCLUDING. Use grep, glob_files, list_dir, find_symbol, read_file, read_lines, and retrieve to gather evidence. Never assume a violation; confirm it against the source.
@@ -140,6 +144,7 @@ class MaintenanceAgent:
distinct = {f.get("file") for f in seed_findings if f.get("file")}
budget = max(WRITE_BUDGET, len(distinct) + 2)
set_write_budget(budget)
scope_token = set_tool_scope(core.worker_tool_names(mode))
cost_before = cost_session_total()["cost"]
started = datetime.now()
codename = core.report_codename()
@@ -172,6 +177,7 @@ class MaintenanceAgent:
renderer=renderer,
)
finally:
reset_tool_scope(scope_token)
clear_protected_trees()
clear_write_budget()
set_shell_restricted(False)
+27 -4
View File
@@ -113,11 +113,33 @@ async def report_finding(
return json.dumps({"status": "success", "recorded": True, "total_findings": len(_FINDINGS)})
WORKER_READ_TOOLS = (
"read_file",
"read_lines",
"list_dir",
"glob_files",
"grep",
"find_symbol",
"retrieve",
)
WORKER_REASONING_TOOLS = (
"plan",
"reflect",
"delegate",
"report_finding",
"get_current_isodate",
)
def worker_tool_names(mode: str) -> tuple[str, ...]:
names = WORKER_READ_TOOLS + WORKER_REASONING_TOOLS
if mode != "check":
names = names + WRITE_TOOLS + ("verify",)
return names
def payloads_for(mode: str) -> list[dict[str, Any]]:
exclude = WEB_TOOLS + SWARM_TOOLS + ("run_command",)
if mode == "check":
exclude = exclude + WRITE_TOOLS + ("verify",)
return get_tool_payloads(exclude=exclude)
return payloads_named(worker_tool_names(mode))
def payloads_named(names: tuple[str, ...]) -> list[dict[str, Any]]:
@@ -240,6 +262,7 @@ __all__ = [
"findings",
"payloads_for",
"payloads_named",
"worker_tool_names",
"summarize",
"write_reports",
"report_codename",
+54 -3
View File
@@ -16,6 +16,7 @@ from .agent import (
close_http_client,
get_tool,
install_timestamps,
mark_orchestration_tools,
react_loop,
tool,
_with_datetime,
@@ -146,8 +147,56 @@ async def run_tests(mode: str = "check", scope: str = ""):
async def run_fleet_tool(mode: str = "check"):
"""Run the whole maintenance fleet in dependency order via the orchestrator. mode: check (default) or fix."""
run_mode = "fix" if str(mode).lower() == "fix" else "check"
code = await run_fleet(run_mode, None, MAESTRO_AGENT_MAX_ITER, renderer=_RENDERER)
return json.dumps({"status": "success", "mode": run_mode, "fleet_exit_code": code})
per_agent: list[dict[str, Any]] = []
fleet_items: list[dict[str, Any]] = []
def _record(name: str, result: dict[str, Any]) -> None:
_LAST_RESULTS[name] = result
if run_mode == "check":
_LAST_CHECK[name] = result["items"]
fleet_items.extend(result["items"])
per_agent.append(
{
"agent": name,
"summary": result["summary"],
"incomplete": result.get("incomplete", False),
"cost": result.get("cost"),
"report": result["json"],
}
)
code = await run_fleet(run_mode, None, MAESTRO_AGENT_MAX_ITER, renderer=_RENDERER, on_result=_record)
totals = {key: sum(a["summary"][key] for a in per_agent) for key in ("total", "errors", "warnings", "fixed", "unfixed")}
incomplete_agents = [a["agent"] for a in per_agent if a["incomplete"]]
_LAST_RESULTS["fleet"] = {"summary": totals, "items": fleet_items, "json": ""}
return json.dumps(
{
"status": "success",
"mode": run_mode,
"fleet_exit_code": code,
"totals": totals,
"incomplete_agents": incomplete_agents,
"agents": per_agent,
"note": "Per-agent results are cached. Summarize directly from this payload and use read_report(agent) for detail; do NOT re-run agents to gather results.",
}
)
mark_orchestration_tools(
"list_agents",
"read_report",
"run_security",
"run_audit",
"run_devii",
"run_docs",
"run_fanout",
"run_dry",
"run_style",
"run_frontend",
"run_seo",
"run_tests",
"run_fleet_tool",
)
MAESTRO_TOOLS = (
@@ -188,7 +237,9 @@ THE FLEET (run each via its run_* tool):
- run_tests: integration-test coverage (writes tests, never runs the suite)
- run_fleet_tool: the whole fleet in dependency order
ROUTING: map the request to the right dimension and call its tool. Use list_agents if unsure. For an "everything" request use run_fleet_tool. Clarify an ambiguous request before running anything.
ROUTING: map the request to the right dimension and call its tool. Use list_agents if unsure. For an "everything" request use run_fleet_tool EXACTLY ONCE. Clarify an ambiguous request before running anything.
NEVER RE-RUN TO GATHER RESULTS: run_fleet_tool already runs every agent once and returns a complete payload (per-agent summaries, totals, incomplete_agents) AND caches each agent's result. After it returns, summarize straight from that payload; use read_report(agent) or read_report("fleet") for detail. Do NOT call the individual run_* tools again after a fleet run, and do NOT run the fleet a second time, unless the operator explicitly asks you to re-run a specific agent.
MODE POLICY: a question defaults to check (read-only). Run fix ONLY when the operator explicitly asks to fix, and CONFIRM before any fix run that writes, especially run_fleet_tool in fix mode. Never silently edit in answer to a question. When the operator asks to fix issues you just found in a check, call the SAME agent's run_* tool with mode=fix exactly once; it is automatically seeded with the confirmed findings and fixes them directly (do not re-run check first, and do not call it more than once).
+10 -2
View File
@@ -8,7 +8,7 @@ import json
import logging
import sys
from datetime import datetime
from typing import Optional
from typing import Callable, Optional
from . import core
from .agent import MarkdownRenderer, close_http_client, cost_session_total, format_usd, install_timestamps, usd_str
@@ -28,7 +28,13 @@ def _parser() -> argparse.ArgumentParser:
return parser
async def run_fleet(mode: str, only: Optional[str], max_iter: int, renderer: Optional[MarkdownRenderer]) -> int:
async def run_fleet(
mode: str,
only: Optional[str],
max_iter: int,
renderer: Optional[MarkdownRenderer],
on_result: Optional[Callable[[str, dict], None]] = None,
) -> int:
names = ordered_agents(only.split(",") if only else None)
started = datetime.now()
results: list[dict] = []
@@ -37,6 +43,8 @@ async def run_fleet(mode: str, only: Optional[str], max_iter: int, renderer: Opt
if renderer is not None:
renderer.print(f"\n## {name}")
result = await agent.run(mode, None, max_iter, renderer)
if on_result is not None:
on_result(name, result)
results.append(
{
"name": name,
+14 -3
View File
@@ -17,7 +17,15 @@ class StyleAgent(MaintenanceAgent):
"_v1/_v2/_v3, better_, best_, simple_, my_, the_, _data, _info, and the rest of the forbidden list.\n"
"- No comments or docstrings in source files, EXCEPT the mandatory header and the docstrings that @tool functions "
"require for their schema (the proven agent engine convention).\n"
"- No em-dash anywhere: neither the em-dash character (U+2014) nor its HTML entities; use a hyphen.\n"
"- Em-dash, CONTEXT-AWARE (think before you touch it): the rule bans em-dashes (U+2014, and U+2013) "
"that WE authored as prose - in a comment, a docstring, a user-facing string or label or error message, "
"markdown or template copy. An em-dash that is DATA is NOT a violation and MUST be left exactly as is: when "
"the character is the target or source of a transformation (str.replace, str.maketrans, a regex character "
"class, a sanitizer or normaliser that converts typographic punctuation to ASCII), a parser literal, or a "
"test fixture that deliberately feeds an em-dash to exercise handling. Rewriting such a literal negates the "
"code's whole purpose (e.g. a `.replace(\"<em-dash>\", \"-\")` cleaner stops cleaning). When unsure whether "
"an occurrence is prose or data, read the surrounding lines; if it is operated on rather than displayed, "
"treat it as data and skip it (record at most one info finding, never an edit).\n"
"- Full typing coverage on Python function signatures and variables.\n"
"- pathlib instead of the os module for paths.\n"
"- A fixed-key dict that should be a dataclass.\n"
@@ -28,7 +36,10 @@ class StyleAgent(MaintenanceAgent):
"touch' rule forbids. If files lack the header, record at most ONE info finding stating the count, and never "
"auto-edit a file solely to add a header.\n"
"- No magic numbers; named constants instead. No warnings.\n\n"
"FIX: rename the symbol to an intent-revealing name, strip the stray comment or docstring, replace the em-dash, "
"FIX: rename the symbol to an intent-revealing name, strip the stray comment or docstring, replace a PROSE "
"em-dash with a literal ASCII hyphen (never with the escape backslash-u-2014, which is the SAME character and "
"fixes nothing, and never with an HTML entity inside non-HTML source, which injects literal text into code or a "
"string), leaving every data em-dash untouched, "
"add the type annotation, convert os.path to pathlib, convert the dict to a dataclass, remove the version pin, add "
"the header, or name the constant. Only touch code you are already editing for a finding; do not restyle untouched "
"code. A rename that would change a public API symbol is reported, not auto-applied."
@@ -38,7 +49,7 @@ class StyleAgent(MaintenanceAgent):
return [
("forbidden-names", "devplacepy/**/*.py forbidden naming prefixes/suffixes"),
("headers", "retoor header on created/edited files only; one info finding for pre-existing files that lack it, never a mass sweep"),
("em-dash", "no em-dash character or its HTML entities in any touched file"),
("em-dash", "prose em-dashes (comments, docstrings, user-facing strings, markdown) become hyphens; em-dashes that are DATA (replace/maketrans/regex targets, sanitizers, fixtures) are left untouched"),
("typing", "Python function signatures and variables fully typed"),
("pathlib", "pathlib over the os module; no magic numbers; no version pinning"),
("frontend-style", "static/js and static/css naming and constants"),