Files
claude-config/bin/agent_cycle/escalation_mark.py
T

61 lines
4.4 KiB
Python

"""The escalation mark makes one escalation trigger step the worker up one rung, never two (docs/decisions.md → `routing: a worker whose run ends asking for a higher rung`).
A worker whose run ends asking its driver, such as loop-claude, for a higher rung cites the trigger that fired, and the judge quotes the passage where it fired as the verdict's escalation. The mark, Route.escalated_at, is how many transcript entries the session held when an escalation last stepped the worker up, 0 before any. The session's transcript only grows, so the mark holds across cycles, and the archive's Worker: lines keep it for a rerun.
- Stamp. stamped() writes on the judge's verdict how many entries the judge read.
- Step. routing.next_route() steps the worker up on an escalation and moves the mark to moved_to(verdict).
- Show. transcript() puts step_line after the mark, telling the judge that the triggers before that line are spent. quote_after_step, in the judge's schema, tells the judge to quote only the worker's words after that line.
- Check. checked() keeps an escalation only where it quotes both the transcript the judge was shown and the worker's own lines after the mark, which after_mark() renders, so a passage that bought one step cannot buy another.
"""
import dataclasses
import typing
from claude_code.transcript import assistant_lines, render, Typed
from claude_code.entries import AssistantTurn, Entry
from agent_cycle.verdicts import Escalating, Verdict
from agent_cycle.quotes import verbatim
# Only the judge reads the step line, so it is not a [loop] line.
STEP_TAG = "[step]"
def asks_higher_rung(driver: str) -> str:
"""How a worker's run ends where it wants the rest of its chunk on a higher rung. The worker's prompt tells it to, and the judge's rules name the same words, so both take them from here. driver is the entry point that drives the cycle, such as `do-epic`."""
return f"asking {driver} for a higher rung"
def step_line(driver: str) -> str:
return f"{STEP_TAG} {driver} stepped the worker up a rung here, so the escalation triggers that fired before this line are spent."
def quote_after_step(driver: str) -> str:
"""Reply.escalation's description in agent_cycle/judge.py carries this sentence, so the judge reads step_line as the mark."""
return f"Where a {STEP_TAG} line says {driver} stepped the worker up a rung, quote only the worker's own words after it, and leave this empty where the trigger fired only before it."
def stamped(verdict: Verdict, entries_read: int) -> Verdict:
"""The verdict with how many transcript entries the judge read, where it may carry an escalation."""
return dataclasses.replace(verdict, entries_read=entries_read) if isinstance(verdict, Escalating) else verdict
def moved_to(verdict: Escalating) -> int | None:
"""Where the verdict's escalation would move the mark, if the escalation steps the worker; None for a verdict without one."""
return verdict.entries_read if verdict.escalation else None
def transcript(entries: list[Entry], at: int, typed_answer: typing.Callable[[str], Typed | None], driver: str) -> str:
"""The transcript as the judge sees it. Where the mark, at, is above 0, step_line comes after that many entries."""
return "\n\n".join(part for part in (render(entries[:at], typed_answer), step_line(driver) if at else "", render(entries[at:], typed_answer)) if part)
def after_mark(entries: list[Entry], at: int) -> str:
"""All the text and tool calls of the worker's assistant turns after the mark, even where the judge is shown only the transcript's tail. The driver's prompts and the human's lines are left out, since they repeat earlier passages, such as the judge's evidence or the worker's question."""
return "\n\n".join(line for entry in entries[at:] if isinstance(entry, AssistantTurn) for line in assistant_lines(entry))
def checked(verdict: Verdict, *, after: str, shown: str) -> Verdict:
"""The verdict without its escalation unless the escalation quotes both texts: the argument after, which holds the worker's lines after the mark as after_mark renders them, and the argument shown, the transcript the judge was shown. A misquote, or a quote of a passage before the mark, drops the escalation, so the worker's rung holds."""
if isinstance(verdict, Escalating) and verdict.escalation and not (verbatim(verdict.escalation, after) and verbatim(verdict.escalation, shown)):
return dataclasses.replace(verdict, escalation="")
return verdict