Optimize: a single /optimize call in Optimizer, the detectors and the tools

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maximeandClaude Opus 5.5 committed 2026-11-16 14:15:55 +01:00
1 parent 91cd39525e
commit beaa248687
6 files changed
+47 -55

No files matched your search

+29 -36
View File
@@ -5,17 +5,17 @@ labeling/auto/analyses/<STATION>_<start>_<end>/ its labels (labels.jsonl) and a
next to it); at the end, the server submits its labels to labeling/auto/<STATION>.jsonl, marked with their analysis (field
`analysis`). Then the user can write to it: the same session resumes (`claude -p --resume`), and its labels, if they
change, replace those it had submitted.
On a finished analysis, "Optimize" launches `/improve` in Optimizer (optimizer/, the skill that improves the detectors on the
range of the analysis, its labels as truth), then the tool-builder of the agents repository, which brings the analyst's
scripts into the reading tools (tools/); one optimization at a time, no analysis or message meanwhile; the summary of the
session (optimizer/sessions/<analysis>_<time>/summary.md) and that of the tools (<analysis>/tools.md) show under the report.
On a finished analysis, "Optimize" launches `/optimize` in Optimizer (optimizer/, the skill that, from the analysis, improves
the detectors on its range, its labels as truth, then brings the analyst's scripts into the reading tools, tools/); one
optimization at a time, no analysis or message meanwhile; the summary of the session
(optimizer/sessions/<analysis>_<time>/summary.md, and the <analysis>/tools.md of an older session) shows under the report.
"""
import html
import json
import os
import subprocess
import threading
from datetime import date, datetime, timedelta, timezone
from datetime import datetime, timezone
import markdown
@@ -24,13 +24,13 @@ import station
FOLDER = station.LABELS / "auto" / "analyses"
DETECTORS = station.ROOT / "detectors"
AGENTS = station.ROOT / "agents" # the plugin of the generic agents (~/agents)
OPTIMIZER = station.ROOT / "optimizer" # the Optimizer repository (~/Optimizer), where /improve runs
OPTIMIZER = station.ROOT / "optimizer" # the Optimizer repository (~/Optimizer), where /optimize runs
SESSIONS = OPTIMIZER / "sessions"
# the request of /improve (optimizer/README.md), paths relative to the root of Optimizer (its links data, labeling, detectors)
IMPROVE = """/improve DETECTORS: detectors/. STATION: data/{station}. FROM: {start}. TO: {end}. \
LABELS: labeling/auto/{station}.jsonl. REFERENCE: --labels labeling/auto --data data. FAMILIES: labeling/families.md. \
CONTEXT: labeling/auto/analyses/{name}/report.md. OUT: sessions/{name}_{time}/. Caller's context: Diagnostic-app, through \
Optimizer; read-only on data/ and labeling/; commit only in detectors/, on its current branch; do not commit OUT."""
# the request of /optimize (optimizer/README.md), paths relative to the root of Optimizer (its links); Optimizer reads the
# station and the range in the analysis
OPTIMIZE = """/optimize ANALYSIS: labeling/auto/analyses/{name}/. OUT: sessions/{name}_{time}/. Caller's context: \
Diagnostic-app, through Optimizer; read-only on data/ and labeling/; commit only in detectors/ and tools/, on their current \
branch; do not commit OUT."""
REQUEST = """Station: data/{station}/. Range: {start} → {end} (UTC). Hints: {hints}. \
Output folder: {out}/. DETECTORS: no. INSTRUCTIONS: instructions/analyst.md."""
@@ -229,7 +229,7 @@ def report(name, dark):
return _page(file(name, "report.md").read_text(encoding="utf-8"), name, dark)
# --- "Optimize": /improve in Optimizer on the analysis, then the tool-builder, by claude -p
# --- "Optimize": /optimize in Optimizer on the analysis, by claude -p
def _summary_file(name):
"""The summary of the last session of this analysis, or None. The time pattern rules out the sessions of another
@@ -258,8 +258,8 @@ def optimize(name):
"""The "Optimize" button on a finished analysis: the session in a thread; refused if an optimization or an analysis runs."""
if state(name)["state"] != "done":
raise station.Refusal("only a finished analysis can be optimized")
if not ((OPTIMIZER / ".claude" / "skills" / "improve" / "SKILL.md").exists()
and all((OPTIMIZER / link).exists() for link in ("data", "labeling", "detectors"))
if not ((OPTIMIZER / ".claude" / "skills" / "optimize" / "SKILL.md").exists()
and all((OPTIMIZER / link).exists() for link in ("data", "labeling", "detectors", "tools"))
and (AGENTS / ".claude-plugin" / "plugin.json").exists()):
raise station.Refusal("no Optimizer in optimizer/ or no agents in agents/ (the links: README.md)")
with lock:
@@ -273,32 +273,25 @@ def optimize(name):
def optimize_thread(name):
"""The two turns of the optimization, one after the other: the detectors (/improve, in Optimizer), then the reading
tools (the tool-builder, even if the detectors did not change)."""
"""The optimization: /optimize in Optimizer, on the analysis (the detectors, then the reading tools)."""
o, out = optimizations[name], FOLDER / name
rel = os.path.relpath(out, station.ROOT)
(out / "work").mkdir(exist_ok=True)
with open(out / "work" / "optimization.jsonl", "a", encoding="utf-8") as stream:
try:
d = json.loads((out / "request.json").read_text(encoding="utf-8"))
request = IMPROVE.format( # TO excluded: the day after the end of the range, as in the page
station=d["station"], start=d["start"][:10], end=date.fromisoformat(d["end"][:10]) + timedelta(days=1),
name=name, time=f"{datetime.now(timezone.utc):%Y%m%dT%H%MZ}")
request = OPTIMIZE.format(name=name, time=f"{datetime.now(timezone.utc):%Y%m%dT%H%MZ}")
# ponytail: Optimizer reads data/ and labeling/ through its own links; AIMSIGHT_DATA / AIMSIGHT_LABELS do not follow
turns = [(OPTIMIZER.resolve(), [request, "--plugin-dir", str(AGENTS.resolve()),
"--add-dir", str(DETECTORS.resolve()), # the target of the link, where the session writes
"--allowedTools", "Read,Write,Edit,Bash,Agent"]),
(station.ROOT, [f"Analysis: {rel}/. Tools: tools/. Write {rel}/tools.md.", "--plugin-dir", str(AGENTS),
"--agent", "agents:tool-builder", "--allowedTools", "Read,Write,Edit,Bash"])]
for cwd, (prompt, *options) in turns:
p = subprocess.Popen(["claude", "-p", prompt, *ISOLATED, "--output-format", "stream-json", "--verbose",
"--permission-mode", "acceptEdits", *station.links(), *options],
cwd=cwd, env=_env(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
for line in p.stdout:
stream.write(line)
o["lines"] += digest(line)
if p.wait():
raise RuntimeError(f"claude ended with code {p.returncode}")
p = subprocess.Popen(["claude", "-p", request, *ISOLATED, "--output-format", "stream-json", "--verbose",
"--permission-mode", "acceptEdits", *station.links(), # tools/ among them, where it writes
"--plugin-dir", str(AGENTS.resolve()),
"--add-dir", str(DETECTORS.resolve()), # the target of the link, where the session writes
"--allowedTools", "Read,Write,Edit,Bash,Agent"],
cwd=OPTIMIZER.resolve(), env=_env(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True)
for line in p.stdout:
stream.write(line)
o["lines"] += digest(line)
if p.wait():
raise RuntimeError(f"claude ended with code {p.returncode}")
o["state"] = "done"
except Exception as e: # noqa: BLE001 — the error shows in the page, the server goes on
o["lines"].append(f"Error: {e}")
@@ -309,7 +302,7 @@ def optimize_thread(name):
def summary(name, dark):
"""The summary of the detectors' session, then that of the tools (<analysis>/tools.md), in one page."""
"""The summary of the session (the detectors and the tools), then the <analysis>/tools.md of an older session."""
s, tools = _summary_file(name), FOLDER / name / "tools.md"
if s is None and not tools.exists():
raise station.Refusal("no summary for this analysis")