|
|
|
@@ -1,6 +1,7 @@
|
|
|
|
|
"""What loop-claude says to the worker each cycle. routed_by, review_launches, escalation and reruns build the sentences about its rungs; rungs_held says the last three again in a prompt after a step-up or an unfinished verdict."""
|
|
|
|
|
from model_routing.ladder import climbed, LADDER, position, review_rungs, reviewers_rung, ReviewKind, Rung
|
|
|
|
|
from model_routing.route import ESCALATION_MARKER, HIGHEST_ESCAPE_AND_IMPACT, Route
|
|
|
|
|
"""What loop-claude says to the worker each cycle. model_routing.review_choice chooses where the reviews run; review_launches and reruns say that choice in whole sentences, and escalation says where escalated subagents run. rung_sentences gathers the three, which routed_by puts in a chunk's first prompt and rungs_held says again after a step-up or an unfinished verdict."""
|
|
|
|
|
from model_routing.review_choice import chosen, ReviewChoice
|
|
|
|
|
from model_routing.ladder import climbed, LADDER, position, Rung
|
|
|
|
|
from model_routing.route import ESCALATION_MARKER, Route
|
|
|
|
|
from loop_claude.routing import escalation_rungs
|
|
|
|
|
from loop_claude.project.records import HUMAN_RULE_NAMES, RECORD_FILES
|
|
|
|
|
from markdown_text import listed, quoted
|
|
|
|
@@ -12,8 +13,10 @@ QUOTE_SOURCES = listed(("README.md's Goals and Audience", *RECORD_FILES, *HUMAN_
|
|
|
|
|
# Where the worker may write an answer the human typed, so it counts as recorded.
|
|
|
|
|
ANSWER_DESTINATIONS = "README.md's Goals or Audience, " + ", ".join(RECORD_FILES) + ", todo.md or a comment"
|
|
|
|
|
ANSWERS_RECORDED = f"every answer the human typed in it that a later chunk could need is recorded in {ANSWER_DESTINATIONS} at the one place it governs"
|
|
|
|
|
# Each kind of review, as review_launches names those it launches: a row per pass, since a replay may lower one pass alone. "each reviewer" names every pass no replay lowered.
|
|
|
|
|
REVIEWS: tuple[tuple[str, ReviewKind], ...] = (*((f"each {agent}", kind) for kind, agent in REVIEWERS.items()), ("each comprehension seat", "seat"), ("the security pass's subagent", "security"))
|
|
|
|
|
# How the launch and rerun sentences name the reviews that no replay lowered: "each reviewer" for every such pass, then the two reviews that are no pass.
|
|
|
|
|
EACH_REVIEWER = "each reviewer"
|
|
|
|
|
EACH_SEAT = "each comprehension seat"
|
|
|
|
|
THE_SECURITY_PASS = "the security pass's subagent"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AFTER_CODE_HEALTH = 'which rank after code health (~/.claude/CLAUDE.md → "Shared goal") unless README.md voids it'
|
|
|
|
@@ -21,18 +24,22 @@ AFTER_CODE_HEALTH = 'which rank after code health (~/.claude/CLAUDE.md → "Shar
|
|
|
|
|
|
|
|
|
|
def fresh(task: str, premises: str, route: Route, project: Rung | None, facts: str) -> str:
|
|
|
|
|
"""A chunk's first prompt; facts is preflight_facts' or preflight_failed's text."""
|
|
|
|
|
escalated = f"{escalation(route, project)}{reruns(route, project)}".lstrip()
|
|
|
|
|
routed = f"{routed_by(route, project)}{recount(route)}\n\n" if route.item else (f"{escalated}\n\n" if escalated else "")
|
|
|
|
|
escalated = " ".join((*escalation(route, project), *reruns(route, project)))
|
|
|
|
|
routed = f"{routed_by(route, project)} {recount(route)}\n\n" if route.item else (f"{escalated}\n\n" if escalated else "")
|
|
|
|
|
return f"{task}\n\nContinue any open draft PR or worktree for the chunk before creating a new one.\n\n{routed}{facts}\n\nThe chunk's todo.md items, or else the task above, set its scope. The goals below, {AFTER_CODE_HEALTH}, choose between ways to deliver that scope, and never widen it.\n\nFrom README.md:\n{premises}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def routed_by(route: Route, project: Rung | None) -> str:
|
|
|
|
|
return f"loop-claude runs you on {route.rung}, routed by item {route.item}'s L of {route.likelihood}. Name each todo.md item this chunk takes, with its L and E + I, whether or not it is item {route.item}.{review_launches(route, project)}{escalation(route, project)}{reruns(route, project)}"
|
|
|
|
|
return " ".join((
|
|
|
|
|
f"loop-claude runs you on {route.rung}, routed by item {route.item}'s L of {route.likelihood}.",
|
|
|
|
|
f"Name each todo.md item this chunk takes, with its L and E + I, whether or not it is item {route.item}.",
|
|
|
|
|
*rung_sentences(route, project),
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def recount(route: Route) -> str:
|
|
|
|
|
"""Has the worker count the routing item's size again once it knows the code, since the L that routed it was scored before anyone read that code."""
|
|
|
|
|
return f" After your first read of the code, count item {route.item}'s size again: the lines, files and components you will change. Where that count would change its L, have a subagent that scores todo.md items re-score it; a re-score whose L picks a rung above yours is escalation trigger 1."
|
|
|
|
|
return f"After your first read of the code, count item {route.item}'s size again: the lines, files and components you will change. Where that count would change its L, have a subagent that scores todo.md items re-score it; a re-score whose L picks a rung above yours is escalation trigger 1."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def preflight_facts(printed: str) -> str:
|
|
|
|
@@ -45,50 +52,39 @@ def preflight_failed(reason: str) -> str:
|
|
|
|
|
return f"loop-claude could not run `~/.claude/bin/chunk-preflight`, so find the chunk's facts yourself. Why it could not run: {reason}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def review_launches(route: Route, project: Rung | None) -> str:
|
|
|
|
|
"""Which reviews to launch on a rung other than the worker's, and on which, then the rung of each pass a replay lowered; below the project's rung also where to launch a subagent that files or scores todo.md items; then taken_in_launches, the reviewers' rung each higher E + I picks. Empty off the ladder.
|
|
|
|
|
def review_launches(route: Route, project: Rung | None) -> list[str]:
|
|
|
|
|
"""The sentences that say which reviews to launch on a rung other than the worker's, and on which; then the rung of each pass a replay lowered; below the project's rung also where to launch a subagent that files or scores todo.md items; then the reviewers' rung each higher E + I picks. None off the ladder.
|
|
|
|
|
|
|
|
|
|
For a worker on Sonnet 5.5 at high, on an Opus 5.5 project, routed by item 3 of E + I 5, it opens: ` Launch each reviewer with the Agent tool set to model opus, effort high, the rung item 3's E + I of 5 picks. Launch each comprehension seat and the security pass's subagent with the Agent tool set to model opus, effort medium, the project's rung.` Where a replay lowered the prose pass to Haiku 5.5 at low, and the E + I steps no review up, a sentence follows: ` Launch each prose-reviewer instead with the Agent tool set to model haiku, effort low, the rung the replay of its pass chose.`
|
|
|
|
|
For a worker on Sonnet 5.5 at high, on an Opus 5.5 project, routed by item 3 of E + I 5, they are: `Launch each reviewer with the Agent tool set to model opus, effort high, the rung item 3's E + I of 5 picks.` and `Launch each comprehension seat and the security pass's subagent with the Agent tool set to model opus, effort medium, the project's rung.` Where a replay lowered the prose pass to Haiku 5.5 at low, and the E + I steps no review up, a sentence follows: `Launch each prose-reviewer instead with the Agent tool set to model haiku, effort low, the rung the replay of its pass chose.`
|
|
|
|
|
"""
|
|
|
|
|
if project is None:
|
|
|
|
|
return ""
|
|
|
|
|
rungs, reviewers = review_rungs(project, route.rung, route.reviewed_on()), reviewers_rung(project, route.rung, route.reviewed_on())
|
|
|
|
|
lowered_launches = "".join(f" Launch {reviews} instead with the Agent tool set to {rung.agent_args()}, the rung the replay of its pass chose." for reviews, rung in lowered(rungs, reviewers))
|
|
|
|
|
scan = " Run a full scan only from such a subagent, since the scan's agents run on the model that starts them." if rungs["security"] != route.rung else ""
|
|
|
|
|
planning = f" Your rung is below the project's: launch each subagent that files or scores todo.md items on the rung `~/.claude/bin/subagent-rung --plan {project.model} <L>` prints." if position(route.rung) < position(project) else ""
|
|
|
|
|
return f"{shared_launches(rungs, reviewers, route, project)}{lowered_launches}{scan}{planning}{taken_in_launches(route, project)}"
|
|
|
|
|
return []
|
|
|
|
|
choice = chosen(project, route)
|
|
|
|
|
lowered = [f"Launch each {REVIEWERS[kind]} instead with the Agent tool set to {rung.agent_args()}, the rung the replay of its pass chose." for kind, rung in choice.lowered.items()]
|
|
|
|
|
scan = ["Run a full scan only from such a subagent, since the scan's agents run on the model that starts them."] if choice.security != route.rung else []
|
|
|
|
|
planning = [f"Your rung is below the project's: launch each subagent that files or scores todo.md items on the rung `~/.claude/bin/subagent-rung --plan {project.model} <L>` prints."] if position(route.rung) < position(project) else []
|
|
|
|
|
return [*shared_launches(choice, route, project), *lowered, *scan, *planning, *taken_in_launches(choice)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def shared_launches(rungs: dict[ReviewKind, Rung], reviewers: Rung, route: Route, project: Rung) -> str:
|
|
|
|
|
"""A sentence for each rung other than the worker's that a review runs on. "each reviewer" names every pass no replay lowered; those run on reviewers."""
|
|
|
|
|
together = {"each reviewer": reviewers} | {reviews: rungs[kind] for reviews, kind in REVIEWS if kind not in REVIEWERS}
|
|
|
|
|
def shared_launches(choice: ReviewChoice, route: Route, project: Rung) -> list[str]:
|
|
|
|
|
"""A sentence for each rung other than the worker's that a review no replay lowered runs on."""
|
|
|
|
|
together = {EACH_REVIEWER: choice.reviewers, EACH_SEAT: choice.seat, THE_SECURITY_PASS: choice.security}
|
|
|
|
|
launched: dict[Rung, list[str]] = {}
|
|
|
|
|
for reviews, rung in together.items():
|
|
|
|
|
if rung != route.rung:
|
|
|
|
|
launched.setdefault(rung, []).append(reviews)
|
|
|
|
|
return "".join(f" Launch {listed(reviews, 'and')} with the Agent tool set to {rung.agent_args()}, {rung_named(rung, route, project)}." for rung, reviews in launched.items())
|
|
|
|
|
return [f"Launch {listed(reviews, 'and')} with the Agent tool set to {rung.agent_args()}, {rung_named(rung, route, project)}." for rung, reviews in launched.items()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def lowered(rungs: dict[ReviewKind, Rung], reviewers: Rung) -> list[tuple[str, Rung]]:
|
|
|
|
|
"""Each pass a replay put below reviewers, the rung of every other pass, as REVIEWS names it, with the rung the replay chose."""
|
|
|
|
|
return [(reviews, rungs[kind]) for reviews, kind in REVIEWS if kind in REVIEWERS and rungs[kind] != reviewers]
|
|
|
|
|
def taken_in_launches(choice: ReviewChoice) -> list[str]:
|
|
|
|
|
"""The sentence naming the reviewers' rung each higher E + I picks, so the worker reviews an item it takes in on that rung in the same run; the judge reports it as the cycle ends, so loop-claude routes on it only from the next cycle. None where no higher E + I picks a higher rung.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def taken_in_launches(route: Route, project: Rung) -> str:
|
|
|
|
|
"""The rung each higher E + I picks for the reviewers, so the worker reviews an item it takes in on that rung in the same run; the judge reports it as the cycle ends, so loop-claude routes on it only from the next cycle. Empty where no higher E + I picks a higher rung.
|
|
|
|
|
|
|
|
|
|
For a worker on Opus 5.5 at medium, routed by an item of E + I 6, it reads: ` Where an item you take into the chunk has an E + I of 7 or more, launch each reviewer with the Agent tool set to `model opus, effort max` from 7 and `model fable, effort max` from 8.`
|
|
|
|
|
For a worker on Opus 5.5 at medium, routed by an item of E + I 6, it reads: `Where an item you take into the chunk has an E + I of 7 or more, launch each reviewer with the Agent tool set to `model opus, effort max` from 7 and `model fable, effort max` from 8.`
|
|
|
|
|
"""
|
|
|
|
|
now = review_rungs(project, route.rung, route.reviewed_on())
|
|
|
|
|
higher: dict[Rung, int] = {}
|
|
|
|
|
for escape_and_impact in range(route.reviewed_on() + 1, HIGHEST_ESCAPE_AND_IMPACT + 1):
|
|
|
|
|
# An E + I that lifts any pass puts every pass on reviewers_rung, since a replay lowers a pass only where the E + I steps no review up.
|
|
|
|
|
if review_rungs(project, route.rung, escape_and_impact) != now:
|
|
|
|
|
higher.setdefault(reviewers_rung(project, route.rung, escape_and_impact), escape_and_impact)
|
|
|
|
|
if not higher:
|
|
|
|
|
return ""
|
|
|
|
|
picks = [f"`{rung.agent_args()}` from {escape_and_impact}" for rung, escape_and_impact in higher.items()]
|
|
|
|
|
return f" Where an item you take into the chunk has an E + I of {min(higher.values())} or more, launch each reviewer with the Agent tool set to {listed(picks, 'and')}."
|
|
|
|
|
if not choice.higher:
|
|
|
|
|
return []
|
|
|
|
|
picks = [f"`{rung.agent_args()}` from {escape_and_impact}" for rung, escape_and_impact in choice.higher.items()]
|
|
|
|
|
return [f"Where an item you take into the chunk has an E + I of {min(choice.higher.values())} or more, launch each reviewer with the Agent tool set to {listed(picks, 'and')}."]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rung_named(rung: Rung, route: Route, project: Rung) -> str:
|
|
|
|
@@ -100,50 +96,50 @@ def rung_named(rung: Rung, route: Route, project: Rung) -> str:
|
|
|
|
|
return f"the rung item {route.item}'s E + I of {route.escape_and_impact} picks"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def escalation(route: Route, project: Rung | None) -> str:
|
|
|
|
|
"""The prompt's escalation sentences, the marker's among them, each led by a space. Empty off the ladder or on its top rung. Where escalation_rungs opens none, the worker escalates only with the human's yes.
|
|
|
|
|
def escalation(route: Route, project: Rung | None) -> list[str]:
|
|
|
|
|
"""The prompt's escalation sentences, the marker's among them. None off the ladder or on its top rung. Where escalation_rungs opens none, the worker escalates only with the human's yes.
|
|
|
|
|
Else they name the rungs escalation_rungs opens to a subagent, and offer to resume the worker one rung up for the rest of the chunk, up to the highest of those rungs.
|
|
|
|
|
|
|
|
|
|
For a worker on Opus 5.5 at high, on an Opus 5.5 project, routed by item 3 of L 4, they read: ` When an escalation trigger in `~/.claude/CLAUDE.md` fires, hand the hard part of the task to a subagent with the Agent tool set to `model opus, effort xhigh`, one rung above yours, and on each further trigger one rung higher, up to `model opus, effort max`; above that, ask the human first. Open the subagent's prompt with `Escalation trigger <n>:`, n the trigger's number. Where the hard part is the rest of the chunk, end your run instead, asking loop-claude for a higher rung and naming a trigger that fired since your last step-up, if any: once the judge finds it in your transcript, the next cycle resumes you one rung up, up to `model opus, effort max`.`
|
|
|
|
|
For a worker on Opus 5.5 at high, on an Opus 5.5 project, routed by item 3 of L 4, they read: `When an escalation trigger in `~/.claude/CLAUDE.md` fires, hand the hard part of the task to a subagent with the Agent tool set to `model opus, effort xhigh`, one rung above yours, and on each further trigger one rung higher, up to `model opus, effort max`; above that, ask the human first.`, `Open the subagent's prompt with `Escalation trigger <n>:`, n the trigger's number.` and `Where the hard part is the rest of the chunk, end your run instead, asking loop-claude for a higher rung and naming a trigger that fired since your last step-up, if any: once the judge finds it in your transcript, the next cycle resumes you one rung up, up to `model opus, effort max`.`
|
|
|
|
|
"""
|
|
|
|
|
if project is None or route.rung not in LADDER[:-1]:
|
|
|
|
|
return ""
|
|
|
|
|
return []
|
|
|
|
|
marked = f"Open the subagent's prompt with `{ESCALATION_MARKER} <n>:`, n the trigger's number."
|
|
|
|
|
rungs = [f"`{rung.agent_args()}`" for rung in escalation_rungs(project, route)]
|
|
|
|
|
if not rungs:
|
|
|
|
|
return " " + " ".join((
|
|
|
|
|
return [
|
|
|
|
|
f"An escalation trigger in `~/.claude/CLAUDE.md` lets you hand the hard part of a task to a subagent with the Agent tool set to `{climbed(route.rung, 1).agent_args()}`, one rung above yours, only once the human agrees; loop-claude will not resume you higher for a trigger either, so ask the human for that yes.",
|
|
|
|
|
marked,
|
|
|
|
|
))
|
|
|
|
|
]
|
|
|
|
|
further = f", and on each further trigger one rung higher, up to {rungs[-1]}" if len(rungs) > 1 else ""
|
|
|
|
|
return " " + " ".join((
|
|
|
|
|
return [
|
|
|
|
|
f"When an escalation trigger in `~/.claude/CLAUDE.md` fires, hand the hard part of the task to a subagent with the Agent tool set to {rungs[0]}, one rung above yours{further}; above that, ask the human first.",
|
|
|
|
|
marked,
|
|
|
|
|
f"Where the hard part is the rest of the chunk, end your run instead, asking loop-claude for a higher rung and naming a trigger that fired since your last step-up, if any: once the judge finds it in your transcript, the next cycle resumes you one rung up, up to {rungs[-1]}.",
|
|
|
|
|
))
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reruns(route: Route, project: Rung | None) -> str:
|
|
|
|
|
"""The prompt's sentences naming the rung one above a review's, on which it reruns when it asks: shared_reruns' for a reviewer and a seat, then one for each pass a replay lowered. Empty off the ladder.
|
|
|
|
|
def reruns(route: Route, project: Rung | None) -> list[str]:
|
|
|
|
|
"""The sentences naming the rung one above a review's, on which it reruns when it asks: shared_reruns' for a reviewer and a seat, then one for each pass a replay lowered. None off the ladder.
|
|
|
|
|
|
|
|
|
|
For a worker on Sonnet 5.5 at high, on an Opus 5.5 project whose prose pass a replay lowered to Haiku 5.5 at low, routed by an item of E + I 4, they read: ` A reviewer or comprehension seat that asks for a rerun one rung up reruns with the Agent tool set to `model opus, effort high`. Rerun each prose-reviewer that asks with the Agent tool set to `model haiku, effort medium` instead.`
|
|
|
|
|
For a worker on Sonnet 5.5 at high, on an Opus 5.5 project whose prose pass a replay lowered to Haiku 5.5 at low, routed by an item of E + I 4, they read: `A reviewer or comprehension seat that asks for a rerun one rung up reruns with the Agent tool set to `model opus, effort high`.` and `Rerun each prose-reviewer that asks with the Agent tool set to `model haiku, effort medium` instead.`
|
|
|
|
|
"""
|
|
|
|
|
if project is None or route.rung not in LADDER:
|
|
|
|
|
return ""
|
|
|
|
|
rungs, reviewers = review_rungs(project, route.rung, route.reviewed_on()), reviewers_rung(project, route.rung, route.reviewed_on())
|
|
|
|
|
lowered_reruns = "".join(f" Rerun {reviews} that asks with the Agent tool set to `{climbed(rung, 1).agent_args()}` instead." for reviews, rung in lowered(rungs, reviewers))
|
|
|
|
|
return f"{shared_reruns(reviewers, rungs['seat'])}{lowered_reruns}"
|
|
|
|
|
return []
|
|
|
|
|
choice = chosen(project, route)
|
|
|
|
|
lowered = [f"Rerun each {REVIEWERS[kind]} that asks with the Agent tool set to `{climbed(rung, 1).agent_args()}` instead." for kind, rung in choice.lowered.items()]
|
|
|
|
|
return [*shared_reruns(choice), *lowered]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def shared_reruns(reviewers: Rung, seats: Rung) -> str:
|
|
|
|
|
def shared_reruns(choice: ReviewChoice) -> list[str]:
|
|
|
|
|
"""The sentence naming the rung one above reviewers, the rung of each pass no replay lowered, and one above seats, leaving out one already on the top rung."""
|
|
|
|
|
launched = (("reviewer", reviewers), ("comprehension seat", seats))
|
|
|
|
|
launched = (("reviewer", choice.reviewers), ("comprehension seat", choice.seat))
|
|
|
|
|
rerun = {name: climbed(rung, 1) for name, rung in launched if rung != LADDER[-1]}
|
|
|
|
|
if not rerun:
|
|
|
|
|
return ""
|
|
|
|
|
return []
|
|
|
|
|
if len(set(rerun.values())) == 1:
|
|
|
|
|
return f" A {listed(list(rerun), 'or')} that asks for a rerun one rung up reruns with the Agent tool set to `{next(iter(rerun.values())).agent_args()}`."
|
|
|
|
|
return f" A reviewer that asks for a rerun one rung up reruns with the Agent tool set to `{rerun['reviewer'].agent_args()}`, and a comprehension seat with `{rerun['comprehension seat'].agent_args()}`."
|
|
|
|
|
return [f"A {listed(list(rerun), 'or')} that asks for a rerun one rung up reruns with the Agent tool set to `{next(iter(rerun.values())).agent_args()}`."]
|
|
|
|
|
return [f"A reviewer that asks for a rerun one rung up reruns with the Agent tool set to `{rerun['reviewer'].agent_args()}`, and a comprehension seat with `{rerun['comprehension seat'].agent_args()}`."]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resumed(unfinished_evidence: str, route: Route, project: Rung | None) -> str:
|
|
|
|
@@ -153,9 +149,14 @@ def resumed(unfinished_evidence: str, route: Route, project: Rung | None) -> str
|
|
|
|
|
return f"Continue the task; the previous run ended before the chunk was finished.{ruled}The chunk is finished once it is merged, or nothing remains that you could do on your own. Even then, it is finished only once {ANSWERS_RECORDED}.{on}{rungs_held(route, project)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rungs_held(route: Route, project: Rung | None) -> str:
|
|
|
|
|
def rung_sentences(route: Route, project: Rung | None) -> list[str]:
|
|
|
|
|
"""The sentences that depend on the worker's rung: where its reviews run and rerun, and where its escalated subagents run."""
|
|
|
|
|
return f"{review_launches(route, project)}{escalation(route, project)}{reruns(route, project)}"
|
|
|
|
|
return [*review_launches(route, project), *escalation(route, project), *reruns(route, project)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rungs_held(route: Route, project: Rung | None) -> str:
|
|
|
|
|
"""rung_sentences, each after a space, to follow the sentence a prompt ends on."""
|
|
|
|
|
return "".join(f" {sentence}" for sentence in rung_sentences(route, project))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def answered(question: str, answer: str, route: Route, project: Rung | None, stepped: bool) -> str:
|
|
|
|
|