fix(8139): a shape the agent walked past at the end-of-turn check is named again in its next notes

The after phase answers `left`, the asked lines still unjudged; the Stop hook
carries them as `earlier=` into the next checks, each up to two more asks, and
the note names them apart from the last turn's writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 09:41:09 -05:00
co-authored by Claude Opus 5.5
parent 69f3adae2c
commit 55f53de92d
7 changed files with 260 additions and 40 deletions
+23 -5
View File
@@ -755,17 +755,28 @@ async def shape_check():
Query:
written (str) — newline-separated `path<TAB>kind<TAB>name` lines,
repo-relative, kind css|sym|file.
earlier (opt) — the same lines for shapes this session wrote in an
earlier turn, was asked about and walked past. The
hook carries them for a bounded number of asks;
the note names them apart from this turn's.
phase (str) — check | after. Anything else is a 400.
repo (opt) — working repo remote, resolved like the other arms.
project_id (opt) — explicit scope override.
The after phase also answers `left`: the asked lines still unjudged, as
ledger lines, which the hook carries into its next ask.
"""
phase = (request.args.get("phase") or "check").strip()
if phase not in shape_check_svc.PHASES:
return jsonify({"error": f"phase must be one of {list(shape_check_svc.PHASES)}"}), 400
written = shape_check_svc.parse_written(request.args.get("written") or "")
earlier = [
line for line in shape_check_svc.parse_written(request.args.get("earlier") or "")
if line not in written
]
project_id, repo, _unbound = await _project_scope()
body: dict = {"status": "ok", "unjudged": []}
if not project_id or not written:
if not project_id or not (written or earlier):
return jsonify(body)
from grimoire.services import access
from grimoire.services import coverage as coverage_svc
@@ -774,7 +785,9 @@ async def shape_check():
if not await access.can_read_project(g.user.id, project_id):
return jsonify(body)
repo_key = repo_bindings_svc.normalize_repo_key(repo) if repo else ""
unjudged = await shape_ledger_svc.unjudged_shapes(project_id, written, repo_key=repo_key)
unjudged = await shape_ledger_svc.unjudged_shapes(
project_id, written + earlier, repo_key=repo_key,
)
# A snippet recorded AT a shape is the strongest verdict there is — the
# next refresh stamps that row canonical. Until then the ledger cannot
# know, so the recorded locations answer for it: an agent who just
@@ -786,17 +799,22 @@ async def shape_check():
}
unjudged = [u for u in unjudged if (u["path"], u["symbol"]) not in recorded
and not (u["kind"] == "file" and (u["path"], "") in recorded)]
carried = set(earlier)
fresh = [u for u in unjudged if (u["path"], u["kind"], u["symbol"]) not in carried]
repeat = [u for u in unjudged if (u["path"], u["kind"], u["symbol"]) in carried]
outcome = shape_check_svc.outcome_for(phase, unjudged)
await shape_check_svc.record_shape_check(
g.user.id, outcome, written=len(written), unjudged=len(unjudged),
project_id=project_id,
g.user.id, outcome, written=len(written) + len(earlier), unjudged=len(unjudged),
project_id=project_id, earlier=len(repeat),
)
body["outcome"] = outcome
body["unjudged"] = unjudged
if outcome == "noted":
body["note"] = shape_check_svc.note_text(
unjudged, project_id=project_id, repo=repo_key or repo,
fresh, project_id=project_id, repo=repo_key or repo, earlier=repeat,
)
if phase == "after":
body["left"] = shape_check_svc.ledger_lines(unjudged)
return jsonify(body)
+58 -15
View File
@@ -68,31 +68,62 @@ def _evidence_note(evidence: dict) -> str:
return f" ({'; '.join(parts)})" if parts else ""
def note_text(unjudged: list[dict], *, project_id: int, repo: str) -> str:
"""What the agent reads at the start of the turn after one that wrote
unjudged shapes.
Phrased as a practice: what to decide and the one
call that records it, not a prohibition. The reusing-code skill owns the
longer version of what each verdict means.
"""
def _shape_lines(items: list[dict], *, project_id: int) -> list[str]:
lines = []
for item in unjudged[:_LISTED]:
for item in items[:_LISTED]:
new = " — new" if item.get("status") == "new" else ""
lines.append(
f"- {item['path']} · {item['symbol']} ({item['kind']}){new}"
f"{_evidence_note(item.get('evidence') or {})}"
)
more = len(unjudged) - _LISTED
more = len(items) - _LISTED
if more > 0:
lines.append(f"- … and {more} more (list_shapes(project_id={project_id}, path=…) shows them)")
return lines
def _count(n: int) -> str:
return f"{n} definition{'s' if n != 1 else ''}"
def note_text(unjudged: list[dict], *, project_id: int, repo: str,
earlier: list[dict] | None = None) -> str:
"""What the agent reads at the start of the turn after one that wrote
unjudged shapes.
``earlier`` is what this session wrote in an earlier turn, was asked
about, and walked past. The per-turn ledger forgets a line once its note
has been answered, and nothing else names a shape by name — the coverage
line carries only a count — so the hook carries walked-past lines into
a bounded number of later asks. The note says plainly that they are a
repeat, and why.
Phrased as a practice: what to decide and the one
call that records it, not a prohibition. The reusing-code skill owns the
longer version of what each verdict means.
"""
earlier = earlier or []
parts = []
if unjudged:
parts.append(
f"Your last turn wrote {_count(len(unjudged))} that nobody has judged yet. "
"You built them, so you know what each one is — record it alongside this "
"turn's work, while that is still true, so the next session starts from your "
"answer instead of rebuilding it:\n"
+ "\n".join(_shape_lines(unjudged, project_id=project_id))
)
if earlier:
verb = "is" if len(earlier) == 1 else "are"
parts.append(
f"{'Also: ' if unjudged else ''}{_count(len(earlier))} you wrote earlier in this "
f"session {verb} still unjudged. You were asked and moved on; nothing else will "
"name them again, so they are asked once more while you still know what they "
"are:\n"
+ "\n".join(_shape_lines(earlier, project_id=project_id))
)
repo_arg = f', repo="{repo}"' if repo else ""
return (
f"Your last turn wrote {len(unjudged)} definition{'s' if len(unjudged) != 1 else ''} "
"that nobody has judged yet. You built them, so you know what each one is — "
"record it alongside this turn's work, while that is still true, so the next "
"session starts from your answer instead of rebuilding it:\n"
+ "\n".join(lines)
"\n\n".join(parts)
+ "\n\nFor each: something another part of the code should reuse → record it "
"with create_snippet (name, code, when to reach for it, location); built from a "
"recorded snippet → `instance` of it; a deliberate departure from one → "
@@ -105,6 +136,13 @@ def note_text(unjudged: list[dict], *, project_id: int, repo: str) -> str:
)
def ledger_lines(items: list[dict]) -> str:
"""``items`` back as the hook's own `path<TAB>kind<TAB>name` lines — what
the after phase hands the hook so it can carry the walked-past shapes
forward without parsing a JSON list in shell."""
return "\n".join(f"{i['path']}\t{i['kind']}\t{i['symbol']}" for i in items)
async def record_shape_check(
user_id: int | None,
outcome: str,
@@ -112,12 +150,17 @@ async def record_shape_check(
written: int,
unjudged: int,
project_id: int | None = None,
earlier: int = 0,
) -> None:
if outcome not in OUTCOMES:
raise ValueError(f"unknown shape-check outcome {outcome!r}")
details: dict = {"outcome": outcome, "written": written, "unjudged": unjudged}
if project_id:
details["project_id"] = project_id
# How many of `unjudged` were carried from an earlier turn's walked-past
# ask — so a window can tell a fresh ask from a repeat.
if earlier:
details["earlier"] = earlier
async with async_session() as session:
session.add(AppLog(
category="plugin",