Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,22 @@ premium spend for routing, and makes each stage, fix and phase commit with
its trailers. Every attempt goes into the stage file's `record`, and
`phase.md`'s status is rendered from the records, so a resumed session
picks up where it stopped. The phase reviewer writes `review.json` in the
phase folder with a closed list of finding types. SKILL.md is a
short outline; each step's directions are in its own guide.
phase folder with a closed list of finding types.

### The phase driver

implement-in-phases does not run the phase loop itself: for every phase it
runs `plugins/phased-delivery/scripts/run-phase.py <phase dir>`, which
revises the phase, routes, builds, gates and commits each stage, runs the
review and its fixes and finishes the phase. Every step is a headless
`claude` at the model, thinking level and cost cap the routing gives, and a
builder sees only its own stage's packet. When a step cannot go on (a brief
that cannot be built, a review question, a sign-in or service failure) the
driver writes a halt into `phase.json` and exits 10; the session does what
the halt's `next` says, asking you where the skill says to, and reruns it,
which resumes from the records. The session itself builds only a stage
marked `session_only`. Version 1 builds with Claude only: Codex routing
(below) is not used by the driver yet.

Some tools, Codex among them, let subagents use a different model only when the
user asks. To allow it, add to the project's `AGENTS.md` or `CLAUDE.md`:
Expand Down
2 changes: 1 addition & 1 deletion plugins/phased-delivery/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "phased-delivery",
"version": "0.12.0",
"version": "0.13.0",
"description": "Plan work as self-contained phases, then implement it phase by phase.",
"author": {
"name": "Bryan"
Expand Down
32 changes: 29 additions & 3 deletions plugins/phased-delivery/scripts/claude-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,55 @@
# file edits work and commands follow the project's allow rules). A session
# already in a sandbox container sets it to bypassPermissions (the ai-coding
# sandbox image does).
# CLAUDE_BUILD_STALL (seconds, default 900) stops the call when stream.jsonl
# gains no line for that long: a hung call, not a slow one (the longest
# silence in 39 recorded runs was 130 s).
# Prints "Tokens: N" (input, output, cache read and cache creation, from
# modelUsage, else usage) and
# "Cost: X.XX" (USD), or unknown, for the stage commit.
#
# Exit: 0 built; 2 usage; 8 the budget was reached; other non-zero: the
# Exit: 0 built; 2 usage; 7 stalled (stopped after CLAUDE_BUILD_STALL
# seconds without output); 8 the budget was reached; other non-zero: the
# claude exit status. Never commits; the orchestrator does.
set -u
[ $# -eq 6 ] || { sed -n '2,17p' "$0" >&2; exit 2; }
[ $# -eq 6 ] || { sed -n '2,23p' "$0" >&2; exit 2; }
REPO=$1; PROMPT=$2; MODEL=$3; EFFORT=$4; BUDGET=$5; OUT=$6
MODE=${CLAUDE_BUILD_PERMISSIONS:-acceptEdits}
case "$MODE" in
acceptEdits|bypassPermissions) ;;
*) echo "CLAUDE_BUILD_PERMISSIONS must be unset or bypassPermissions, not '$MODE'" >&2; exit 2 ;;
esac
STALL=${CLAUDE_BUILD_STALL:-900}
case "$STALL" in
''|*[!0-9]*|0) echo "CLAUDE_BUILD_STALL must be a whole number of seconds, not '$STALL'" >&2; exit 2 ;;
esac
mkdir -p "$OUT"
TEXT=$(cat "$PROMPT") || exit 2
(cd "$REPO" && exec claude -p "$TEXT" --model "$MODEL" --effort "$EFFORT" \
--max-budget-usd "$BUDGET" \
--settings '{"enabledPlugins":{"phased-delivery@ai-coding-plugins":false}}' \
--permission-mode "$MODE" --no-session-persistence \
--output-format stream-json --verbose) \
< /dev/null > "$OUT/stream.jsonl" 2> "$OUT/stderr.txt"
< /dev/null > "$OUT/stream.jsonl" 2> "$OUT/stderr.txt" &
pid=$!
rm -f "$OUT/stalled"
# the watchdog: stops claude after STALL seconds without a new stream line
(size=-1; quiet=0
while sleep 1; do
now=$(wc -c < "$OUT/stream.jsonl")
if [ "$now" = "$size" ]; then quiet=$((quiet + 1)); else size=$now; quiet=0; fi
[ $quiet -lt "$STALL" ] || { : > "$OUT/stalled"; kill $pid; exit; }
done) > /dev/null 2>&1 &
watch=$!
wait $pid
status=$?
kill $watch 2>/dev/null
if [ -e "$OUT/stalled" ]; then
: > "$OUT/report.md"
printf 'Tokens: unknown\nCost: unknown\n'
echo "No output for $STALL s; the call was stopped as stalled" >&2
exit 7
fi
# Exits 0 for a successful result, 8 when the budget was reached, 9 for another
# error result, 1 without one.
python3 - "$OUT/stream.jsonl" "$OUT/report.md" <<'PY'
Expand Down
132 changes: 93 additions & 39 deletions plugins/phased-delivery/scripts/plan-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

Usage:
plan-check.py PLAN_DIR [--repo DIR] [--spec FILE] [--design FILE] [--outline] [--phase NN]
plan-check.py PLAN_DIR --packet NN.n

The plan is PLAN_DIR/plan.json (plan.schema.json: outcomes, cross-phase
decisions, existing interfaces, phases) and, per phase, a folder NN-name
Expand All @@ -19,6 +20,10 @@
ones; --phase NN limits them to that phase. A brief that changed since an
attempt built it needs a higher `revision`.

--packet prints the stage packet, the only plan text a builder is given: the
phase's role, why, how, before, after and test budget, then the stage's
brief as phase.md shows it. It checks and renders nothing; exit 2 for an unknown stage.

--spec and --design default to the paths plan.json names in `docs`, relative
to --repo, which defaults to the current directory.

Expand Down Expand Up @@ -51,20 +56,26 @@

# ---- a small JSON Schema subset (the plugin's scripts use the standard library only)

TYPES = {"object": dict, "array": list, "string": str, "boolean": bool, "null": type(None),
"integer": int, "number": (int, float)}


def is_type(data, name):
# a bool is an int in Python, not in JSON
return isinstance(data, TYPES[name]) and (name == "boolean" or not isinstance(data, bool))


def validate(data, schema, root=None, path=""):
root = root or schema
if "$ref" in schema:
schema = root["definitions"][schema["$ref"].split("/")[-1]]
out = []
t = schema.get("type")
types = {"object": dict, "array": list, "string": str, "boolean": bool}
if t == "integer":
if not isinstance(data, int) or isinstance(data, bool):
return [(path, f"must be an integer, got {type(data).__name__}")]
if data < schema.get("minimum", data) or data > schema.get("maximum", data):
out.append((path, f"{data} is outside {schema.get('minimum')}..{schema.get('maximum')}"))
elif t and not isinstance(data, types[t]):
return [(path, f"must be {t}, got {type(data).__name__}")]
if t and not any(is_type(data, name) for name in ([t] if isinstance(t, str) else t)):
wanted = "an integer" if t == "integer" else t if isinstance(t, str) else " or ".join(t)
return [(path, f"must be {wanted}, got {type(data).__name__}")]
if is_type(data, "number") and (data < schema.get("minimum", data) or data > schema.get("maximum", data)):
out.append((path, f"{data} is outside {schema.get('minimum')}..{schema.get('maximum')}"))
if "enum" in schema and data not in schema["enum"]:
out.append((path, f"{data!r} is not one of {schema['enum']}"))
if "pattern" in schema and isinstance(data, str) and not re.search(schema["pattern"], data):
Expand All @@ -74,11 +85,14 @@ def validate(data, schema, root=None, path=""):
for k in schema.get("required", []):
if k not in data:
out.append((path, f"missing {k!r}"))
extra = schema.get("additionalProperties")
for k, v in data.items():
if k in props:
out += validate(v, props[k], root, f"{path}/{k}")
elif schema.get("additionalProperties") is False:
elif extra is False:
out.append((path, f"unknown field {k!r}"))
elif isinstance(extra, dict):
out += validate(v, extra, root, f"{path}/{k}")
if isinstance(data, list):
if len(data) < schema.get("minItems", 0):
out.append((path, f"needs at least {schema['minItems']} items"))
Expand Down Expand Up @@ -399,11 +413,42 @@ def contract_lines(sid, brief, contracts, existing):
return mine + used or ["none"]


def phase_rows(found, stages):
return [t for t in outcome_rows(stages) if t["written_in"] in found or t["passes_from"] in found]


def render_stage(sid, s, plan, stages, proves):
# the stage's section of phase.md, also the body of its packet
b = s["brief"]
c = b["context"]
L = ["", f"## {sid} {b['title']}".rstrip(), "", f"Goal: {b['goal']}", "", "Context:"]
ctx = [f"- Builds on {x['source']}: {x['what']}" for x in c["builds_on"]]
ctx += [f"- `{x['where']}`: {x['fact']}" for x in c["code_read"]]
ctx += [f"- {x['doc'].capitalize()}, {x['section']}: \"{x['quote']}\"" for x in c["design"]]
L += ctx or ["none"]
L += ["", "Contracts:"] + contract_lines(sid, b, registry(stages), plan["existing"])
L += ["", "Files:"] + bullets(b["files"], lambda x: f"`{x['path']}` ({x['action']}): {x['role']}")
L += ["", "Behaviour:"] + bullets(b["behaviour"], lambda x:
f"Given {x['given']}, when {x['when']}, then {x['then'].rstrip('.')}.")
L += ["", "External inputs:"] + bullets(b["external_inputs"], lambda x:
f"{x['source']}: {x['check']}; on failure, {x['on_failure']}")
L += ["", "Tests:"] + bullets(b["tests"], lambda x:
f"{x['action']} `{x['test'].strip('`')}` ({x['kind']}): "
+ (proves.get(x["test"].strip("`"), x["proves"]) if x["kind"] == "outcome" else x["proves"])
+ (f" (unit because: {x['why_unit']})" if x.get("why_unit") else ""))
L += ["", "Done when:"] + bullets(b["done_when"], str)
L += ["", "Checks:"] + bullets(b["checks"], lambda x: f"`{x}`")
L += ["", "Out of scope:"] + bullets(b["out_of_scope"], str)
total, t, why = tier(b["model_basis"])
L += ["", "Model basis: " + " ".join(f"{m}{b['model_basis'][m]}" for m in MEASURES)
+ f" = {total} -> {t}" + (f" (override: {why})" if why else "")]
return L


def render_phase(ph, pj, found, plan, stages):
num, d = ph["phase"], pj["brief"]
outcomes = {x["id"]: x["observable"] for x in plan["outcomes"]}
contracts = registry(stages)
rows = [t for t in outcome_rows(stages) if t["written_in"] in found or t["passes_from"] in found]
rows = phase_rows(found, stages)
proves = {t["test"]: outcomes.get(t["outcome"], t["outcome"]) for t in rows}
L = [f"# Phase {num}: {ph['name']}", "",
"Implement with the implement-in-phases skill; do not build from this file",
Expand All @@ -420,29 +465,7 @@ def render_phase(ph, pj, found, plan, stages):
L += [f"| {sid} | {s['brief']['goal']} | {tier(s['brief']['model_basis'])[1]} | {s['brief']['order']} | "
f"{stage_status(s)} |" for sid, s in found.items()]
for sid, s in found.items():
b = s["brief"]
c = b["context"]
L += ["", f"## {sid} {b['title']}".rstrip(), "", f"Goal: {b['goal']}", "", "Context:"]
ctx = [f"- Builds on {x['source']}: {x['what']}" for x in c["builds_on"]]
ctx += [f"- `{x['where']}`: {x['fact']}" for x in c["code_read"]]
ctx += [f"- {x['doc'].capitalize()}, {x['section']}: \"{x['quote']}\"" for x in c["design"]]
L += ctx or ["none"]
L += ["", "Contracts:"] + contract_lines(sid, b, contracts, plan["existing"])
L += ["", "Files:"] + bullets(b["files"], lambda x: f"`{x['path']}` ({x['action']}): {x['role']}")
L += ["", "Behaviour:"] + bullets(b["behaviour"], lambda x:
f"Given {x['given']}, when {x['when']}, then {x['then'].rstrip('.')}.")
L += ["", "External inputs:"] + bullets(b["external_inputs"], lambda x:
f"{x['source']}: {x['check']}; on failure, {x['on_failure']}")
L += ["", "Tests:"] + bullets(b["tests"], lambda x:
f"{x['action']} `{x['test'].strip('`')}` ({x['kind']}): "
+ (proves.get(x["test"].strip("`"), x["proves"]) if x["kind"] == "outcome" else x["proves"])
+ (f" (unit because: {x['why_unit']})" if x.get("why_unit") else ""))
L += ["", "Done when:"] + bullets(b["done_when"], str)
L += ["", "Checks:"] + bullets(b["checks"], lambda x: f"`{x}`")
L += ["", "Out of scope:"] + bullets(b["out_of_scope"], str)
total, t, why = tier(b["model_basis"])
L += ["", "Model basis: " + " ".join(f"{m}{b['model_basis'][m]}" for m in MEASURES)
+ f" = {total} -> {t}" + (f" (override: {why})" if why else "")]
L += render_stage(sid, s, plan, stages, proves)
notes = list(d["notes"])
for x in plan["decisions"]:
if "all" in x["phases"] or num in x["phases"]:
Expand Down Expand Up @@ -487,12 +510,32 @@ def write(plan_dir, plan, phases, nums):
return list(texts)


def read_plan(plan_dir):
# (plan.json, [(its phase entry, phase.json, {stage id: stage file})]) as they are, without checks
plan = read_json(os.path.join(plan_dir, "plan.json"))
return plan, [(ph, read_json(os.path.join(plan_dir, ph["dir"], "phase.json")),
{n[:-5]: read_json(os.path.join(plan_dir, ph["dir"], n))
for n in stage_files(os.path.join(plan_dir, ph["dir"]))}) for ph in plan["phases"]]


def packet(plan_dir, sid):
"""The stage packet, the only plan text a builder is given, or None for an unknown stage."""
plan, phases = read_plan(plan_dir)
stages = flatten(phases)
for _, pj, found in phases:
if sid in found:
d = pj["brief"]
outcomes = {x["id"]: x["observable"] for x in plan["outcomes"]}
proves = {t["test"]: outcomes.get(t["outcome"], t["outcome"]) for t in phase_rows(found, stages)}
head = [f"Role: {d['role']}", f"Why: {d['why']}", f"How: {d['how']}",
f"Before: {d['before']}", f"After: {d['after']}", f"Test budget: {d['test_budget']}"]
return "\n".join(head + render_stage(sid, found[sid], plan, stages, proves)) + "\n"
return None


def render_plan(plan_dir):
"""Re-render plan.md and every phase.md from the JSON as it is, without checks; returns the paths written."""
plan = read_json(os.path.join(plan_dir, "plan.json"))
phases = [(ph, read_json(os.path.join(plan_dir, ph["dir"], "phase.json")),
{n[:-5]: read_json(os.path.join(plan_dir, ph["dir"], n))
for n in stage_files(os.path.join(plan_dir, ph["dir"]))}) for ph in plan["phases"]]
plan, phases = read_plan(plan_dir)
return [os.path.join(plan_dir, n) for n in write(plan_dir, plan, phases, [ph["phase"] for ph in plan["phases"]])]


Expand Down Expand Up @@ -546,7 +589,7 @@ def load_phase(plan_dir, ph, outline, stage_schema, r):


def main(argv):
args, opts = [], {"--repo": ".", "--spec": None, "--design": None, "--phase": None}
args, opts = [], {"--repo": ".", "--spec": None, "--design": None, "--phase": None, "--packet": None}
outline, i = False, 0
while i < len(argv):
if argv[i] in opts and i + 1 < len(argv):
Expand All @@ -561,6 +604,17 @@ def main(argv):
print(__doc__, file=sys.stderr)
return 2
plan_dir, repo, scope = args[0], opts["--repo"], opts["--phase"]
if opts["--packet"] is not None:
try:
text = packet(plan_dir, opts["--packet"])
except (OSError, ValueError) as e:
print(e, file=sys.stderr)
return 2
if text is None:
print(f"--packet {opts['--packet']} is not a stage of the plan", file=sys.stderr)
return 2
print(text, end="")
return 0
r = Report()
plan_path = os.path.join(plan_dir, "plan.json")
if not os.path.exists(plan_path):
Expand Down
Loading
Loading