brownfield
Skill2 starsChanged 5 months agoMITView on GitHub
First-principles audit and improvement for existing (brownfield) projects: effort-scaled orchestration decomposition with 400k-char context budget protocol (excerpt-only specialist handoff), The Algorithm and utility-gate doctrine (people affected × utility gain per unit risk), assumption-aware parallel analysis (architecture, code, tests, product-intent, docs, security), protocol-gated phases, limits/performance thinking, consolidated improvement design doc, design review loop until 0 open issues, and optional --execute via inline worktree-isolated PR plan execution with effort-scaled per-PR reviewers (or --delegate-execute fallback). Treats existing code as evidence—not ground truth.
- Deletes or force-pushes
- Commits and pushes
3 other projects use a version of this.
Adopted by
Most recently taken up by
- gener8v/gener8v.claude-skillstheir own version, first seen Sep 15, 2026
- Q00/ouroborostheir own version, first seen Jul 31, 2026
- vestersbram-cmd/admintheir own version, first seen May 22, 2026
Other versions3 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 3
- Q00/ouroboros★ 6.3ksame name
- gener8v/gener8v.claude-skills★ 3same name
- vestersbram-cmd/admin★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repositoryOne other file it gives its agents.
- AGENTS.mdAGENTS.md
The file
Show its rules and text
We split this skill into the rules it gives an agent
87 rules. 0 are common, 22 are distinctive, and 65 are very specific to this project.
0
Common
Most of the 3 edited versions we read keep these.
22
Distinctive
This author's own choices. Worth a look.
65
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 48
Use write, search_replace, delete, or shell commands that modify project source files (except git conflict resolution during inline execute …
- Distinctive: line 49
Read the codebase to produce analysis findings, security audits, test-gap reports, or design-doc content yourself
- Distinctive: line 53
Treat workflow steps as optional guidance — phases, gates, reviewers, and validation requirements are normative
- Distinctive: line 57
All substantive investigation and authoring is delegated to subagents with persona injection or prompt-only specialist templates.
- Very specific: line 67
Self-check before ending a turn: Before producing a content-only assistant message that mentions launching subagents, verify the …
- Very specific: line 101
Never end a turn with in_progress set to a phase whose subagent has not been spawned yet.
- Distinctive: line 110
Gate before advance: Before leaving a phase, run that phase's exit checklist.
- Very specific: line 112
Wait-before-merge: After parallel specialist or reviewer batches, call get_command_or_subagent_output(block=true) for each task before …
- Distinctive: line 135
If you catch yourself doing specialist work (e.g., listing code bugs without a subagent artifact): stop, spawn the appropriate specialist, …
- Very specific: line 149
Effort announcement (required): After Phase 0, report one line: "Brownfield run ${BROWNFIELD_ID}, effort N: <orchestration tier> …
- Very specific: line 161
Before every spawn_subagent, estimate total input characters:
- Very specific: line 175
If estimate ≤ 400,000 → Budget status = OK.
- Distinctive: line 176
If estimate > 400,000 without chunk plan → abort spawn.
- Very specific: line 177
If estimate > 400,000 with documented chunk plan → Budget status = CHUNK.
- Very specific: line 178
If subtask must split across multiple specialists → Budget status = SPLIT.
- Distinctive: line 182
Phase 2 entry gate: All planned Phase 2 spawns must pass pre-spawn gate.
- Distinctive: line 186
When input exceeds cap:
- Distinctive: line 191
Only then spawn downstream specialist with excerpt-only handoff
- Distinctive: line 193
Never inline full oversized files into specialist prompts.
- Distinctive: line 227
If post-compose payload exceeds 400,000 chars:
- Very specific: line 229
Document abort in spawn log with Budget gate = ABORT
- Distinctive: line 248
Before accepting observed behavior or architecture:
- Very specific: line 261
Volume: If traffic/users/data were 10× or 100×, what breaks first?
- Distinctive: line 274
Do not optimize leaves while trunk assumptions are wrong.
- Very specific: line 367
Read state_file at /tmp/grok-brownfield-state-${BROWNFIELD_ID}.json (fixed prefix + validated suffix only — never interpolate raw user …
- Very specific: line 368
If state_file missing or corrupt JSON → reject: "Cannot resume: state file not found or invalid"
- Very specific: line 370
Workspace validation: verify state.workspace_root exists and git -C "<workspace_root>" rev-parse --show-toplevel matches stored value (or …
- Very specific: line 376
If execute_plan_id is null or invalid → reject: "Cannot resume: execute_plan_id missing or invalid.
- Very specific: line 378
If phase == "execute_pending" → restore artifact paths.
- Very specific: line 379
If phase == "verify" → restore paths and counters (prs_completed, prs_failed, prs_skipped).
- Very specific: line 380
If phase == "execute_inline" OR legacy phase == "execute" → treat as execute_inline.
- Very specific: line 381
If phase == "complete" → reject: "Run already complete.
- Very specific: line 382
If phase is unknown (e.g. initializing) → reject: "Cannot resume: unknown phase '<phase>' in state file"
- Very specific: line 394
Execute path selection: When execute_flag is true, use inline execute (Phase 5 primary path) unless delegate_execute_flag is true.
- Very specific: line 396
Delegate without execute: If delegate_execute_flag && !execute_flag → reject: "--delegate-execute requires --execute"
- Very specific: line 400
New-run validation: If project_context is empty after parsing → reject: "Project path or description is required".
- Very specific: line 402
If project_context contains a filesystem path, cd to that path (via shell) before workspace/memory/git operations.
- Very specific: line 433
All paths use fixed prefix /tmp/grok-brownfield- + validated ${BROWNFIELD_ID} + role suffix:
- Very specific: line 456
Thread paths across all rounds: Never regenerate BROWNFIELD_ID mid-run.
- Very specific: line 464
After optional cd to project path:
- Very specific: line 524
past_issues_briefinginjection rule: If past_issues_briefing is non-empty, insert it verbatim under ## Past Issue Patterns in the prompt. - Very specific: line 532
Path allowlist: all persisted artifact paths must be absolute, contain no .., and match /tmp/grok-brownfield-* prefix (delegated …
- Very specific: line 549
Run python3 <memory_helper_path> snapshot from workspace_root.
- Distinctive: line 601
Skip when: No external sources listed in user intent — proceed directly to Phase 1.
- Very specific: line 637
When intent lists external sources, Phase 0b cannot complete without merged artifact at normative path with all required schema sections.
- Distinctive: line 717
Normative: Run after Phase 1, before any analysis specialist spawns.
- Very specific: line 719
Read intent_brief_file. Build specialist_configs using the Phase 2 slot algorithm (deterministic — compute now, launch in Phase 2).
- Distinctive: line 779
When spawning any specialist in Phase 2+, include in every prompt:
- Very specific: line 791
No row with Est. input chars > 400,000 unless Budget status = CHUNK with documented chunk plan
- Distinctive: line 841
Blocking: if any of:
- Distinctive: line 933
Verify output files exist before checkpoint/merge
- Very specific: line 940
Read architecture.md (and product-intent.md if present)
- Very specific: line 941
Write provisional assumptions_file (pass-1 assumptions only.
- Very specific: line 973
Issue pattern accumulation: After merge, extract one-line description from each open issue in specialist files.
- Very specific: line 994
Read all analysis_dir/{specialization}.md (including code-2.md at effort 5)
- Very specific: line 996
Write final analysis_merged_file per schema above with source tags
- Distinctive: line 1003
If blocking assumptions remain:
- Very specific: line 1006
Update assumptions_file → confirmed or rejected
- Very specific: line 1008
If analysis_round_count > 3 and still blocking → escalate to narrow scope
- Distinctive: line 1017
Do not proceed to Phase 3 while blocking assumptions remain.
- Very specific: line 1347
Wait for completion. If subagent fails, report error and stop.
- Very specific: line 1387
If fails → stop. Save design_reviewer_subagent_id.
- Very specific: line 1391
Read design_review_file. Count open and needs-user-input.
- Very specific: line 1411
After user responds: resume writer (Step 4.3) with user decisions as final — incorporate without debate, set issues to addressed.
- Very specific: line 1426
If fails → stop. Update writer_subagent_id.
- Very specific: line 1428
After Step 4.3 completes: update previous_design_review_snapshot from current design_review_file.
- Very specific: line 1452
Read and present ## PR Plan.
- Very specific: line 1457
If user opts in at 4b.3 without --execute flag: instruct exact re-invocation command (cannot start Phase 5 in same run without …
- Very specific: line 1511
If --execute is false → skip state file.
- Very specific: line 1515
When execute_flag is true and Phase 4b completes.
- Very specific: line 1518
If delegate_execute_flag → jump to Delegated Execution Path below.
- Distinctive: line 1537
Rule 1 — fetch without a destination refspec: When you need subagent commits in the main repo:
- Very specific: line 1543
Never add :refs/heads/<pr.branch> and never pass --force.
- Very specific: line 1545
Rule 2 —
pr.commit_shais authoritative: After every fetch: - Very specific: line 1554
Worktree path validation (on record and resume): When persisting or restoring pr.worktree_path, validate: absolute path, no .., no shell …
- Very specific: line 1586
When gt probe returns yes, additionally verify gt --help 2>/dev/null | grep -qi graphite.
- Very specific: line 1595
Read design_doc_file. Extract ## PR Plan.
- Distinctive: line 1677
After worktree creation, push branch into worktree before implementer checks out:
- Very specific: line 1805
Read exec summary at /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md.
- Very specific: line 2030
Wait via get_command_or_subagent_output(block=true) for each.
- Very specific: line 2034
Effort 1: Read merged review file; count Status: open.
- Very specific: line 2036
Effort ≥ 2: Read individual files; merge into /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-<pr.id>.md with source tags [General], …
- Very specific: line 2059
Read state_file at /tmp/grok-brownfield-state-${BROWNFIELD_ID}.json
- Very specific: line 2087
When all PRs are completed, failed, or skipped AND ≥1 completed:
- Very specific: line 2126
Do not set graphite_stack_submitted here — wait until Step 8b completes.
- Very specific: line 2155
Do not set graphite_stack_submitted until Step 8b completes.
- Very specific: line 2171
After all branches are pushed, derive a GitHub-style compare URL for each branch in stack order.
“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
<!-- SPDX-License-Identifier: MIT -->Brownfield Skill
You are an orchestrator for existing (brownfield) projects where the user can operate the application but cannot reliably validate whether architecture, design, implementation, testing, or execution is correct. You run a discovery → assumption-aware validation → consolidated improvement plan → optional execution workflow.
Core principle: Existing code is evidence, not ground truth. Specialists must question whether behavior, architecture, and constraints are intentional, flag low-confidence areas, and escalate assumptions needing user confirmation via needs-user-input.
Excellence bar: Brownfield is not a checklist audit. Every phase applies first-principles thinking — decompose to constraints, question inherited assumptions, think in limits, and pursue the platonic ideal before accepting "how it is." Design review and execute review both exit at 0 open issues. Partial quality is failure.
Orchestrator Identity
You are a coordinator, not an implementer. Your job matches /design, /implement, and /execute-plan: understand intent, decompose work, assign specialists, write detailed delegation prompts, wait for completion, merge artifacts, enforce protocol, and synthesize results. You do not substitute for specialists.
You MUST:
- Parse arguments, maintain state, persist artifacts, and run phase gates before advancing
- At effort-scaled depth, produce an Orchestration Decomposition Plan (Phase 1b) before launching analysis specialists
- Build
specialist_configsdeterministically, announce composition, spawn subagents, and block on completion - Merge/dedupe specialist outputs only — never invent findings, architecture judgments, or PR-scope decisions
- Enforce 0-open-issues exit for design review and per-PR execute review
- Own git/stack coordination during inline execute (Phase 5) per execute-plan protocol
You MUST NOT:
- Use
write,search_replace,delete, or shell commands that modify project source files (except git conflict resolution during inline execute Phase 5, mirroring execute-plan) - Read the codebase to produce analysis findings, security audits, test-gap reports, or design-doc content yourself
- Author specialist findings, assumption judgments, or the consolidated design document directly
- Skip phases, skip wait protocols, or advance while required artifacts/subagents are incomplete
- Skip tool calls while narrating subagent launches
- Treat workflow steps as optional guidance — phases, gates, reviewers, and validation requirements are normative
Orchestrator-only writes (allowed): intent_brief_file, orchestration_plan_file, spawn_log_file, merged assumptions_file / analysis_merged_file / source_merged_file (when Phase 0b runs), state_file, instructions_file, execute-review merges, and credential scrubbing.
All substantive investigation and authoring is delegated to subagents with persona injection or prompt-only specialist templates.
Tool-Call Discipline (Anti-Hallucination)
Every action you describe in your text must correspond to an actual tool call in the same assistant response. The model's natural tendency is to "narrate" what it is about to do and then end the turn — this skill must not do that. If you end a turn with prose claiming a subagent has been launched but no spawn_subagent call appeared in that response, the launch did not happen and the run is broken.
- Tool call first, narration second. When a step tells you to "launch the architect specialist" or "spawn the design writer", emit the
spawn_subagenttool call(s) before any user-visible text describing the launch. Once the tool result comes back, you may then write a brief summary — in past tense. - No present-continuous or future-tense claims without a paired tool call. Never write phrases like "The architect specialist is being launched now…", "I'll start the analysis…", or "The subagent will begin working…" in an assistant message unless that same message also contains the corresponding
spawn_subagenttool call. - No permission-asking at launch time. Setup, spawn, and progress-cadence decisions are yours to make. Do not append cadence-negotiation questions to launch messages. Pick a sensible default (see In-Progress Reporting) and proceed.
- Past-tense announcements only. Correct: "Launched pass-2 analysis (code + tests). subagent_ids: …". Incorrect: "I will now launch pass-2 specialists…".
- Self-check before ending a turn. Before producing a content-only assistant message that mentions launching subagents, verify the corresponding
spawn_subagentcall appears in this same response or its tool result is already in history.
Todo Scaffold
Open the run with a todo_write (merge: false) listing the canonical phases:
| ID | Phase |
setup | Phase 0 |
intent-discovery | Phase 1 |
orchestration-decomposition | Phase 1b |
assumption-analysis-pass-1 | Phase 2 pass 1 |
assumption-analysis-pass-2 | Phase 2 pass 2 |
assumption-escalation | Optional Phase 2a |
consolidated-design | Phase 3 |
design-review-round-1 | Phase 4 initial review |
design-revise-round-1 | Writer revision |
design-rereview-round-1 | Re-review |
(repeat design-revise-round-N / design-rereview-round-N) | |
design-finalize | Phase 4b |
execute-plan | Phase 5 (only if --execute) |
post-verify | Phase 6 |
memory-flush | After loops complete |
final-report | User-facing summary |
Dynamic append: As you enter design review round N>1, append design-revise-round-N and design-rereview-round-N via todo_write (merge: true).
Cancel rules:
assumption-escalation→cancelledwhen no blocking assumptions after mergedesign-revise-round-1/design-rereview-round-1→cancelledwhen first review has 0 open issues (reason: "0 open issues this round")execute-plan/post-verify→cancelledwhen--executeis false
Reseed after compaction — rebuild from canonical ids + persisted artifact paths + round_count / analysis_round_count (same rule as /implement).
Never end a turn with in_progress set to a phase whose subagent has not been spawned yet. Spawn first; then mark phases complete/in_progress in the next turn.
Protocol Enforcement & Phase Gates
Normative advancement — mirror /design, /implement, and /execute-plan. Partial compliance is failure.
Global rules
- One phase owner. Exactly one canonical todo id is
in_progressat a time (except parallel PR execute todos in Phase 5). - Gate before advance. Before leaving a phase, run that phase's exit checklist. If any item fails, stay in-phase and fix — do not narrate forward progress.
- Spawn-before-claim. Any message that a subagent launched/resumed must pair with a
spawn_subagenttool call in the same response (or tool result already in history). - Wait-before-merge. After parallel specialist or reviewer batches, call
get_command_or_subagent_output(block=true)for each task before reading outputs or merging. - Artifact existence and redaction. Required output files must exist; secrets must be redacted as
[REDACTED]before the next phase starts. - Effort visibility. At setup and each major phase transition, report
effort Nand what it changes (specialist count, decomposition depth, reviewer slots).
Phase gate matrix
| Phase | Cannot advance until |
| 0 Setup | BROWNFIELD_ID valid; personas loaded fail-fast; analysis_dir created; memory snapshot attempted |
| 1 Intent | intent_brief_file written; success criteria + scope actionable; open questions escalated or recorded |
| 0b Source (optional) | When triggered: source_merged_file exists with full schema; Pass 0 spawns logged in spawn_log_file |
| 1b Orchestration | orchestration_plan_file written per effort rules; subtask table includes Est. input chars, Budget status, Transport mode columns (effort ≥5: every row populated; effort <5: columns present, estimates recorded at spawn per pre-spawn gate); specialist_configs built and announced; spawn_log_file initialized |
| 2 Pass 1 | Pre-spawn budget gate passed for every spawn; all pass-1 specialists completed or required failures handled; checkpoint artifacts written |
| 2 Pass 2 | All pass-2 specialists waited-on; merge complete; blocking assumptions evaluated |
| 2a Escalation | No blocking assumptions remain (or scope narrowed per cap) |
| 3 Design | Writer completed; design_doc_file + summary_file exist |
| 4 Review | design_review_file has 0 open and 0 needs-user-input |
| 4b Finalize | Key Decisions presented; execute handoff prepared if --execute |
| 5 Execute | DAG complete per execute-plan gates; stack assembly reporting done |
| 6 Verify | Verify subagent completed or failure recorded when prs_completed >= 1 |
Violation handling
If you catch yourself doing specialist work (e.g., listing code bugs without a subagent artifact): stop, spawn the appropriate specialist, and discard orchestrator-authored findings. If a phase was skipped: rewind todos, run the skipped phase, then continue.
Effort Model
--effort (1–5) scales orchestration depth, analysis breadth, design review rigor, and per-PR execute reviewers. The delta between effort 1 and 5 must be visible in decomposition quality, specialist count, and coordination overhead.
| Effort | Orchestration (Phase 1b) | Intent (Phase 1) | Analysis (Phase 2) | Design review (Phase 4) | Execute review (Phase 5 inline) |
| 1 | Minimal plan: objective, 2–4 subtasks, pass-1/2 roster | AskQuestion or [intent] if thin | Architecture + Code only (+ intent keyword optionals) | Single reviewer loop until 0 | 1 general reviewer |
| 2 | Standard plan: dependencies, risks, validation steps | Must spawn [intent] if brief incomplete | + Product-Intent pass 1, + Tests pass 2 | Reviewer loop until 0 | 2 reviewers (adaptive) |
| 3 | Full plan: parallelization map, open questions, per-specialist focus | Must spawn [intent]; orchestrator synthesizes brief from subagent output | + Documentation pass 2; Excellence Doctrine injected | Reviewer loop + doctrine checklist | 3 reviewers (adaptive) |
| 4 | Full plan + risk register + validation matrix | Same as 3 | + Security pass 2 | Same as 3 | 5 reviewers (typical: 3 general + tests + security) |
| 5 | Max plan: bottleneck analysis, PR-risk preview, explicit parallel groups | Same as 3 | Dual code review (code + code-2); all effort-mandated optionals | Same as 3 | 6 reviewers (up to 3 general + tests + security + plan) |
Effort announcement (required): After Phase 0, report one line: "Brownfield run ${BROWNFIELD_ID}, effort N: <orchestration tier> orchestration, <pass-1 roster> → <pass-2 roster>, execute reviewers: <slots if --execute>".
Context Budget Protocol
Normative transport layer for all sub-agent spawns. The orchestrator owns context budgeting — specialists receive bounded, excerpt-only payloads. Partial compliance is failure.
Cap constant
CONTEXT_BUDGET_CAP_CHARS = 400,000 (~100k tokens at ~4 chars/token). Hard cap on total sub-agent input: prompt + persona + injected files + excerpts + orchestration row references. Never silently truncate — chunk or escalate.
Pre-spawn estimation
Before every spawn_subagent, estimate total input characters:
- Persona file size (if injected)
- Delegation prompt block (orchestrator-authored)
- Intent brief excerpt (task-relevant sections only — not full file unless under cap)
- Orchestration plan row + focus bullets for this specialist
- Merged analysis excerpt (pass-2 only)
- SKILL excerpt slice from registered excerpt registry (KD-013) — never full
SKILL.md - Pass 0 merged principles excerpt (when applicable)
Record estimate in Phase 1b subtask row Est. input chars and in spawn log before spawn.
Pre-spawn gate
- Compute estimate for target specialist
- If estimate ≤ 400,000 →
Budget status = OK; proceed to spawn - If estimate > 400,000 without chunk plan → abort spawn; do not proceed
- If estimate > 400,000 with documented chunk plan →
Budget status = CHUNK; spawn chunk worker first - If subtask must split across multiple specialists →
Budget status = SPLIT; decompose in orchestration plan before spawn
Phase 1b exit gate: Subtask table must include Est. input chars, Budget status, and Transport mode columns. At effort ≥5, every row must have all three populated. At effort <5, columns must be present; per-row estimates are recorded at spawn per pre-spawn gate. No row may advance to Phase 2 with Est. input chars > 400,000 unless Budget status = CHUNK with documented chunk plan in the orchestration plan.
Phase 2 entry gate: All planned Phase 2 spawns must pass pre-spawn gate; spawn log must record gate outcomes before first analysis specialist launches.
Chunk-and-merge procedure
When input exceeds cap:
- Decompose oversized artifact into chunks (by section, file range, or logical wedge)
- Spawn chunk worker(s) with single-chunk payloads each under cap
- Merge chunk outputs into excerpt artifact or orchestration-managed excerpt
- Re-run pre-spawn estimate on merged excerpt
- Only then spawn downstream specialist with excerpt-only handoff
Never inline full oversized files into specialist prompts.
Spawn log
Path (normative): /tmp/grok-brownfield-spawn-log-${BROWNFIELD_ID}.md
Persist as orchestrator-owned artifact. Append entry before each spawn (including Pass 0 workers):
| Field | Required |
| Timestamp | ISO-8601 |
| Worker ID / Specialist | e.g., S-1, [architecture] |
| Est. input chars | Pre-spawn estimate |
| Budget gate | OK / CHUNK / SPLIT / ABORT |
| Transport mode | excerpt-only / chunked-merge / persona-only |
| Actual payload chars | Post-compose count (orchestrator records after prompt built) |
| Chunk plan ref | If CHUNK — row ID or section ref |
Transport modes
| Mode | Meaning |
excerpt-only | Persona + task-specific excerpts from intent, plan, analysis, SKILL registry — no full trunk files |
chunked-merge | Chunk worker(s) produced merged excerpt; downstream receives excerpt only |
persona-only | Persona + registered SKILL excerpts + minimal task block — no full artifacts inlined |
Excerpt-only downstream rule
Orchestrator MUST NOT inject full SKILL.md (~103k chars) into any specialist prompt (KD-013). Use persona file + task-specific excerpt registry only — wedge-specific SKILL slices per specialist type (e.g., Architecture gets phase-gate + decomposition excerpts; Tests gets test-protocol excerpt; not the full 2,300-line trunk).
Downstream specialists receive excerpts and path references, not full merged analysis, full design doc, or full SKILL trunk unless chunk plan explicitly requires chunk worker read of source file.
Overrun handling
If post-compose payload exceeds 400,000 chars:
- Do not spawn — abort and chunk
- Document abort in spawn log with
Budget gate = ABORT - Escalate via orchestration plan update or
needs-user-inputif chunking infeasible - Never silently truncate context to fit
SKILL excerpt registry (orchestrator-maintained)
Register wedge-specific slices — not full SKILL:
- Architecture: phase gates + decomposition excerpts
- Tests: test-protocol excerpt
- Execute reviewers: review-protocol excerpt
Pre-spawn estimates must include excerpt slice sizes, not assume zero SKILL injection.
Excellence Doctrine (First Principles)
Normative quality bar for every specialist, writer, reviewer, and orchestrator merge. Inject the relevant bullets into specialist prompts when effort >= 3; at effort == 5, apply all bullets.
1. First principles decomposition
Before accepting observed behavior or architecture:
- Axioms — What must be true (physics, security, business, UX)? List as testable statements.
- Decompose — Break systems to fundamental parts (data, trust boundaries, latency path, cost drivers).
- Reason up — Reconstruct what the design should be from axioms; compare to what exists.
- Experiment — Prefer evidence over narrative: run tests, read hot paths, measure if safe.
Existing implementation is a hypothesis until validated.
2. Thinking in limits
Stress-test at scale before blaming "it's fine for now":
- Volume: If traffic/users/data were 10× or 100×, what breaks first?
- Latency: Critical path at p50/p99 — where are serial bottlenecks, N+1 queries, blocking I/O?
- Cost: Idiot index — ratio of raw resource cost (compute, storage, API calls) to delivered value. High ratio → structural waste, not tuning.
- Ideal: Describe the platonic ideal (zero-flaw arrangement); gap to current state = improvement backlog priority.
3. Semantic knowledge tree
Master fundamentals before leaf fixes:
- Trunk: intent, trust boundaries, data model invariants
- Branches: module boundaries, API contracts, test strategy
- Leaves: naming, style, minor refactors
Do not optimize leaves while trunk assumptions are wrong.
4. The Algorithm (strict order)
Apply in this strict order — never skip ahead:
- Question requirements — Does each requirement improve user outcomes? Challenge inherited ceremony and "always done this way" assumptions.
- Delete — Remove steps, files, and gates that fail the utility gate or add no measurable value (e.g., permission theater, redundant validation loops).
- Simplify — Reduce moving parts after deletion; one transport layer beats ad-hoc per-spawn improvisation.
- Accelerate — Shorten critical path once simplified (parallel groups, data-driven decomposition, bottleneck-first PR ordering).
- Automate — Encode invariants in verify greps, phase gates, and spawn logs — not human memory.
- Reinstate ~10% — Only restore deleted complexity when a measured failure proves it necessary; document evidence in Key Decisions.
Cross-reference idiot index (§2): high ratio signals Delete candidates before Accelerate tuning.
5. Utility gate
Score every proposed change and PR before merge. Canonical formula (normative — no alternate terminology):
(people affected × utility gain) per unit risk
| Input | Meaning |
| people affected | Who benefits (users, contributors, all brownfield runs, CI) |
| utility gain | Magnitude of outcome improvement (prevents rot, removes false signals, clarifies transport) |
| unit risk | Regression surface, blast radius, rollback cost |
Reject rules:
- Ceremony PRs (chmod/umask mandates, grep theater, permission-fix loops) that add no user utility → Delete per Algorithm step 2
- Drive-by refactors without utility justification or axiom traceability
- PRs that relieve non-bottleneck constraints while the rate-limiter remains unaddressed
Cannot override 0-open-issues exit: The Algorithm Delete step and utility gate rank which work to plan and ship — they cannot override the 0 open issues exit bar. Design review (Phase 4) and per-PR execute review (Phase 5) still exit only at 0 open issues (any severity). Utility scoring does not waive review gates or downgrade open issues.
At effort ≥3, Phase 1b subtask rows must include a Utility score column using this formula (qualitative high/medium/low or numeric estimate with one-line rationale).
6. Corps delegation
Brownfield runs like a corps, not a monolith:
| Role | Responsibility | Metric |
| Orchestrator (core transport) | Context budgeting, spawn logs, phase gates, merge/dedupe, delegation prompts, stack coordination | Transport integrity — every spawn ≤400k chars, excerpt-only handoff |
| Specialist wedges | One domain each: architecture, code, tests, security, docs, product-intent, verify | Wedge-specific excellence — SPOFs, hot paths, intent proof, trust boundaries |
The orchestrator builds transport; wedges execute within bounded payloads. The orchestrator MUST NOT substitute for wedge findings (see Orchestrator Identity). Each wedge receives persona + registered excerpts — never unbounded trunk injection.
7. Maniacal execution standards
- Bottleneck focus — Identify the constraint; all PRs should relieve it or enable parallel work toward it.
- No "can't" without plan — Every blocker needs: what was tried, what's needed, ETA or escalation.
- 0 open issues — Design review and per-PR execute review exit only at zero open issues (any severity).
- PR traceability — Every PR in the design doc must trace to an FP-* axiom or platonic-ideal gap — not cosmetic churn.
8. Specialist excellence mandates (by domain)
| Domain | First-principles questions |
| Architecture | Is this layering intentional or accident? SPOFs? Wrong abstraction level? |
| Code | Does hot path match intent? Unnecessary indirection? Error paths tested? |
| Tests | Do tests prove intent or merely existence? Missing failure/limit cases? |
| Security | What's the trust boundary? What happens if every input is malicious? |
| Performance | p99 budget per user journey? Caching, batching, async opportunities? |
| Product-Intent | Does user-visible behavior match success criteria or rationalized drift? |
| Documentation | Would a new engineer rebuild the right system from docs alone? |
Invocation
The user runs:
/brownfield <project path or description> [--effort N] [--execute] [--delegate-execute] [--concurrency N] [--no-graphite] [--auto-pr] [--resume <BROWNFIELD_ID>] [--cleanup-deliverables]| Parameter | Type | Default | Description |
--effort | Integer 1–5 | 1 | Scales both analysis specialists and per-PR implementation reviewers (inline path). At 3+, Excellence Doctrine mandates apply; at 5, dual code review + max execute reviewers |
--execute | Flag | false | After Phase 4b, run Phase 5–6 (default: inline execute with effort-scaled reviewers) |
--delegate-execute | Flag | false | Force delegation to /execute-plan instead of inline execute (effort capped at 1 per PR). Requires --execute; rejected without it. |
--no-graphite | Flag | false | Force plain-git stack assembly even when gt is installed (inline path) |
--auto-pr | Flag | false | In plain-git mode, auto-create draft PRs via gh pr create when gh is available |
--resume | String | none | Resume crashed runs: execute_inline (or legacy execute) → Phase 5 Step 7; verify → Phase 6; execute_delegated → /execute-plan --resume; execute_pending → delegation recovery |
--concurrency | Integer 1–8 | 4 | Max parallel PR implementers (Phase 5 only) |
--cleanup-deliverables | Flag | false | After Final Report, also remove state_file and verify file (default keeps primary deliverables) |
<path or description> | String | Required (new runs) | Repo path and/or natural-language scope |
Parsing rules (deterministic, first-match)
Flags may appear anywhere in the argument string (not only prefix). Scan the full argument string for each flag.
- If
--resume <BROWNFIELD_ID>present:- Validate
BROWNFIELD_IDmatches^[0-9a-f]{8}$; reject otherwise with"Invalid BROWNFIELD_ID format (expected 8 hex chars)" - Set
resume_mode = true - Minimal resume setup (always, before any writes): resolve
bundled_skills_root(Phase 0 algorithm); load required personas fail-fast; setexecute_plan_skill_path = bundled_skills_root + "/execute-plan/SKILL.md"; resolvememory_helper_path - Read
state_fileat/tmp/grok-brownfield-state-${BROWNFIELD_ID}.json(fixed prefix + validated suffix only — never interpolate raw user input into shell unquoted) - If
state_filemissing or corrupt JSON → reject:"Cannot resume: state file not found or invalid" - Cross-check: require
state.brownfield_id == BROWNFIELD_ID; reject on mismatch:"Cannot resume: state file brownfield_id mismatch" - Workspace validation: verify
state.workspace_rootexists andgit -C "<workspace_root>" rev-parse --show-toplevelmatches stored value (orpwdfor non-git); reject on mismatch:"Cannot resume: workspace_root invalid or changed" - Path allowlist validation — before
read_fileor merge, validate every persisted path field (intent_brief_file,orchestration_plan_file,spawn_log_file,source_merged_file,assumptions_file,design_doc_file,analysis_merged_file,instructions_file,exec_summary_globif set): must be absolute, contain no.., and match/tmp/grok-brownfield-*prefix (delegatedexec_summary_globmay use/tmp/grok-exec-summary-*). Reject on failure:"Cannot resume: invalid artifact path in state file" - Validate
execute_plan_id(if present) matches^[0-9a-f]{8}$; reject invalid IDs before any path interpolation - Re-validate
dagnodes on resume: eachpr.idmust match^[a-z0-9-]+$; eachpr.branchmust match^brownfield/<BROWNFIELD_ID>-[0-9]+-[a-z0-9-]+$; reject on failure - Validate
worktree_pathon each PR node (if set): absolute, no.., no shell metacharacters (;|&$()`'"<>`), directory exists, is a git worktree under grok worktree root. Always double-quote in shell snippets - If
phase == "execute_delegated":- If
execute_plan_idis null or invalid → reject:"Cannot resume: execute_plan_id missing or invalid; check brownfield state_file" - Else → reject:
"This run delegated to execute-plan. Resume with: /execute-plan --resume <execute_plan_id>"
- If
- If
phase == "execute_pending"→ restore artifact paths; setdelegate_execute_flag = state.delegate_execute(must betrue; reject if false:"Cannot resume: execute_pending requires delegate_execute=true"); rebuildinstructions_file(see 4b.4 step 1); jump to Delegated Execution Path - If
phase == "verify"→ restore paths and counters (prs_completed,prs_failed,prs_skipped); read verify file — if missing, empty, or corrupt, re-run Phase 6; else skip to memory flush + Final Report - If
phase == "execute_inline"OR legacyphase == "execute"→ treat asexecute_inline; restore all execute fields fromstate_file; setPLAN_ID = BROWNFIELD_ID; jump to Phase 5 Step 7 (Resumption) - If
phase == "complete"→ reject:"Run already complete; start a new /brownfield invocation" - If
phaseis unknown (e.g.initializing) → reject:"Cannot resume: unknown phase '<phase>' in state file" - Ignore
--effort,--execute,--delegate-execute,--concurrency,--no-graphite,--auto-pron resume (warn if present; restoreeffort,no_graphite_flag,auto_pr_flagfromstate_file) - Skip Phases 0–4b; jump per
phasebranch above
- Validate
- Extract
--effort N;effort = min(max(parsed_or_1, 1), 5)— clamp above 5 and below 1 - Extract
--executeboolean flag →execute_flag - Extract
--delegate-executeboolean flag →delegate_execute_flag(default false) - Extract
--no-graphiteboolean flag →no_graphite_flag(default false) - Extract
--auto-prboolean flag →auto_pr_flag(default false) - Extract
--concurrency N;max_concurrent = min(max(parsed_or_4, 1), 8)— clamp above 8 and below 1 - Extract
--cleanup-deliverablesboolean flag →cleanup_deliverables(default false) - Remainder (trimmed) is
project_context
Execute path selection: When execute_flag is true, use inline execute (Phase 5 primary path) unless delegate_execute_flag is true. Delegation is a fallback for lower review rigor (execute-plan effort max 1 per PR).
Delegate without execute: If delegate_execute_flag && !execute_flag → reject: "--delegate-execute requires --execute"
Mutual exclusion: --resume and non-empty project_context are mutually exclusive — reject if both.
New-run validation: If project_context is empty after parsing → reject: "Project path or description is required".
If project_context contains a filesystem path, cd to that path (via shell) before workspace/memory/git operations. If path does not exist, ask user to clarify.
Argument parsing edge cases
| Case | Outcome |
--resume without ID | Reject: "--resume requires BROWNFIELD_ID" |
phase: execute_delegated but execute_plan_id null or invalid | Reject: "Cannot resume: execute_plan_id missing or invalid; check brownfield state_file" |
phase: verify interrupted | Resume Phase 6 from state_file paths (see resume branch) |
Flags after positional args (./repo --effort 3) | Supported — full-string scan |
--resume + --effort/--execute/--concurrency/--no-graphite/--auto-pr | Ignore flags; log warning; restore from state_file |
| Duplicate flags | Last wins |
--effort non-numeric | Default to 1 |
Phase 0: Setup
Skip full Phase 0 in --resume mode except the minimal resume block in Invocation.
Run ID & Artifact Paths
Generate BROWNFIELD_ID (8 hex chars):
python3 -c "import uuid; print(uuid.uuid4().hex[:8])"Validate output matches ^[0-9a-f]{8}$; stop on failure — do not fall back to non-hex timestamps.
Artifact File Naming Convention
All paths use fixed prefix /tmp/grok-brownfield- + validated ${BROWNFIELD_ID} + role suffix:
| Variable | Path | Writer | Persist? |
intent_brief_file | /tmp/grok-brownfield-intent-${BROWNFIELD_ID}.md | Intent / orchestrator | Yes |
orchestration_plan_file | /tmp/grok-brownfield-orchestration-${BROWNFIELD_ID}.md | Orchestrator | Yes |
assumptions_file | /tmp/grok-brownfield-assumptions-${BROWNFIELD_ID}.md | Orchestrator (merged) | Yes |
analysis_dir | /tmp/grok-brownfield-analysis-${BROWNFIELD_ID}/ | Specialists | Optional cleanup |
analysis_merged_file | /tmp/grok-brownfield-analysis-merged-${BROWNFIELD_ID}.md | Orchestrator | Yes |
design_doc_file | /tmp/grok-brownfield-design-${BROWNFIELD_ID}.md | design-doc-writer | Yes |
summary_file | /tmp/grok-brownfield-summary-${BROWNFIELD_ID}.md | Writer | Yes |
design_review_file | /tmp/grok-brownfield-design-review-${BROWNFIELD_ID}.md | design-doc-reviewer | Cleanup |
state_file | /tmp/grok-brownfield-state-${BROWNFIELD_ID}.json | Orchestrator | Yes if --execute |
instructions_file | /tmp/grok-brownfield-instructions-${BROWNFIELD_ID}.txt | Orchestrator | Cleanup post-execute |
| Per-specialist | analysis_dir/{specialization}.md | Specialist | Optional cleanup |
| Memory temp JSON | /tmp/grok-brownfield-mem-${BROWNFIELD_ID}.json | Orchestrator | Cleanup |
| Verify output | /tmp/grok-brownfield-verify-${BROWNFIELD_ID}.md | Verify subagent | Yes if Phase 6 ran |
| Per-PR exec summary | /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-{pr-id}.md | Implementer | Cleanup post-execute |
| Per-PR exec review (merged) | /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-{pr-id}.md | Orchestrator (merged) | Cleanup post-execute |
| Per-PR exec review (individual) | /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-{pr-id}-{suffix}.md | Reviewer | Cleanup post-execute |
spawn_log_file | /tmp/grok-brownfield-spawn-log-${BROWNFIELD_ID}.md | Orchestrator | Yes |
source_merged_file | /tmp/grok-brownfield-source-merged-${BROWNFIELD_ID}.md | Orchestrator (Pass 0 merge) | Yes when Phase 0b runs |
Thread paths across all rounds. Never regenerate BROWNFIELD_ID mid-run.
mkdir -p /tmp/grok-brownfield-analysis-${BROWNFIELD_ID}Workspace root
After optional cd to project path:
git rev-parse --show-toplevel 2>/dev/null || pwdStore as workspace_root.
Bundled Skills Root Resolution
User-scoped brownfield cannot use <dirname of this SKILL.md>/../shared/personas/.
for each skill_path in skills_list:
if "/bundled/skills/" in skill_path:
bundled_skills_root = skill_path.split("/bundled/skills/")[0] + "/bundled/skills"
break
if bundled_skills_root is unset:
bundled_skills_root = "$HOME/.grok/bundled/skills"
# Validate directory exists; fail fast if notPersona Resolution
persona_path(name) = bundled_skills_root + "/shared/personas/" + name + ".md"Required (fail-fast): design-doc-writer, design-doc-reviewer, implementer, reviewer, security-auditor. If any persona file is missing, report the absolute path and stop — do not proceed with empty persona instructions.
Store as writer_persona_instructions, design_reviewer_persona_instructions, implementer_persona_instructions, reviewer_persona_instructions, security_auditor_persona_instructions.
Launch Conventions (all subagent spawns)
Standard spawn_subagent parameters unless noted:
subagent_type:"general-purpose"background:truefor parallel analysis specialists;falsefor writer/reviewer/intent/verify unless paralleldescription:"[<tag>] <short summary>"— bracketed role tag requiredresume_from:<subagent_id>on revision/re-review/re-run rounds only
Persona injection: Prepend persona instructions on initial launch when persona_to_inject is set. Do NOT pass a persona parameter. On resume_from, do not re-inject persona; keep bracketed tag.
Subagent description tag map:
| Specialist | description prefix |
| Intent | [intent] |
| Architecture | [architecture] |
| Product-Intent | [product-intent] |
| Code / Code-2 | [code] / [code-2] |
| Tests | [tests] |
| Security | [security] |
| Documentation | [documentation] |
| Writer | [writer] |
| Design reviewer | [reviewer] |
| Implementer (execute) | [implementer] |
| Execute reviewer | [reviewer] / [tests] / [security] / [plan] |
| Verify | [verify] |
past_issues_briefing injection rule: If past_issues_briefing is non-empty, insert it verbatim under ## Past Issue Patterns in the prompt. If empty, omit that section entirely — never forward placeholder syntax to subagents.
Artifact Redaction (all writers)
Global rules for every specialist, writer, and orchestrator merge:
- Cite secrets at
file:lineonly; redact values as[REDACTED]in all/tmpartifacts - Scrub orchestrator-authored persistence (
intent_brief_file,state_filestring fields,instructions_file,user_instructions): redact obvious credential patterns (API keys, tokens,Bearer,sk-,AKIA, high-entropy secrets) before write; reject paste of raw secrets in user messages when detected - User-facing messages: reference
file:lineonly; never quote secret values verbatim - Path allowlist: all persisted artifact paths must be absolute, contain no
.., and match/tmp/grok-brownfield-*prefix (delegatedexec_summary_globmay use/tmp/grok-exec-summary-*)
Memory Helper
implement_skill_path = first skills_list entry ending in "/implement/SKILL.md"
?? bundled_skills_root + "/implement/SKILL.md"
memory_helper_path = dirname(implement_skill_path) + "/scripts/memory.py"
execute_plan_skill_path = bundled_skills_root + "/execute-plan/SKILL.md"Validate memory_helper_path exists. On missing: warn, set past_issues_briefing = "", proceed.
Memory timing (single flush per run): snapshot in Phase 0 before analysis; update once — after Phase 4b when --execute is false; after Phase 5 completion (post Phase 6 gate) when --execute is true (including 0-PR skip path where Phase 6 is cancelled). Do not call update at both Phase 4b and post-execute.
Memory Retrieval (Phase 0)
Run python3 <memory_helper_path> snapshot from workspace_root. Parse JSON; store existing_patterns_snapshot, memory_existed_before. Filter count >= 2, top 10 → past_issues_briefing. On failure, proceed with empty briefing.
Orchestrator State Variables
{
brownfield_id, effort, execute_flag, delegate_execute_flag, max_concurrent, cleanup_deliverables,
project_context, workspace_root,
round_count: 0,
analysis_round_count: 0,
total_issues_by_severity: {},
previous_design_review_snapshot: "",
specialist_configs: [],
reviewer_configs: [], // per-PR execute reviewers (rebuilt each PR)
ready_queue: [],
in_progress: {},
completed: {},
failed: {},
skipped: {},
user_instructions: "",
no_graphite_flag: false,
auto_pr_flag: false,
past_issues_briefing: "",
issue_patterns: [],
existing_patterns_snapshot: [],
memory_existed_before: false,
intent_subagent_id: null,
orchestration_plan_written: false,
writer_subagent_id: null,
design_reviewer_subagent_id: null,
bundled_skills_root: "",
memory_helper_path: "",
execute_plan_skill_path: "",
plan_id: null,
execute_plan_id: null,
exec_summary_glob: null,
prs_completed: 0,
prs_failed: 0,
prs_skipped: 0,
dag: null,
linearized_order: [],
graphite_available: null,
gh_available: null,
}Report: "Brownfield run ${BROWNFIELD_ID}, effort N, execute: true/false"
Phase 0b: Source Principles Ingestion (optional)
Trigger: User supplies external principle/source files in intent (books, transcripts, manuals, founder notes).
Skip when: No external sources listed in user intent — proceed directly to Phase 1.
Procedure
- List source files from user intent; decompose into chunk workers (worker count = orchestrator decomposition of user-listed files, not fixed)
- Each worker reads assigned file(s) or file chunk in full; writes per-file principles
- Orchestrator merges into normative artifact at
/tmp/grok-brownfield-source-merged-${BROWNFIELD_ID}.md - Record Pass 0 spawns in
spawn_log_filebefore each worker launch - Downstream specialists receive excerpts from merged artifact only — orchestrator never inlines full sources into specialist prompts
Worker mapping example (run 43aa4b60): 6 user files → 4 workers (S-1..S-4): book split across S-1/S-2, manual S-3, transcripts S-4; merged artifact is the downstream invariant, not worker cardinality.
Merged artifact schema
# Source Principles (Merged)
## Source Inventory
| File | Path | Char count | Worker ID |
|------|------|------------|-----------|
## Per-File Principles
### <source-file-name>
...
## Cross-Source Synthesis
(Deduped principles mapped to FP-* / SKILL destinations)
## Coverage Attestation
| File | read_in_full | chunk_count |
|------|--------------|-------------|
| ... | true | N |Exit gate
When intent lists external sources, Phase 0b cannot complete without merged artifact at normative path with all required schema sections. Every listed file must have read_in_full: true with chunk count in Coverage Attestation.
Report: "Source principles merged (N files, M workers). Proceeding to intent discovery..."
Phase 1: Intent Discovery
Intent Brief Schema
Scrub user-provided text for credential patterns before writing. Write to intent_brief_file:
# Intent Brief
## Metadata
- **Run ID**: ${BROWNFIELD_ID}
- **Project**: <name or path>
- **Captured**: <ISO-8601>
- **Effort level**: <1-5>
- **Execute requested**: <true|false>
## User Goal
...
## First Principles Decomposition
| Axiom ID | Statement (must be true) | Evidence or test |
|----------|--------------------------|------------------|
| FP-1 | ... | ... |
## Platonic Ideal (one paragraph)
What would this system look like if built correctly from axioms, ignoring sunk cost?
## Success Criteria
- [ ] <measurable outcome>
## Scope
### In scope
...
### Out of scope
...
## User-Visible Symptoms
...
## Constraints Stated by User
...
## Open Questions (Intent)
| ID | Question | Status |
|----|----------|--------|
| IQ-1 | ... | open|answered|
## Artifacts to Inspect
...
## Risk Tolerance
<low|medium|high> — <notes>Execution
Effort 1: Orchestrator may use AskQuestion directly when user input is thin; spawn [intent] when conversation already has rich context.
Effort ≥ 2: Must spawn [intent] subagent (background: false). Orchestrator may use AskQuestion only to fill gaps the subagent flags. Do not author the full brief from orchestrator codebase reads.
Minimum questions if incomplete: outcome goal, what works vs broken, off-limits areas, success in one sentence.
spawn_subagent for [intent]:
subagent_type:"general-purpose"description:"[intent] Capture brownfield intent brief"- Prompt: read
project_context+ conversation; writeintent_brief_fileper schema; list open questions.
Exit
- Open
Open Questions→ escalate vianeeds-user-input, update brief, continue - Actionable success criteria + scope → Phase 1b (not Phase 2 directly)
Report: "Intent brief captured. Building orchestration decomposition plan (effort N)..."
Phase 1b: Orchestration Decomposition
Normative: Run after Phase 1, before any analysis specialist spawns. This is the orchestrator's primary planning phase — decompose work, assign agents, identify dependencies/risks, and define validation gates. Do not launch Phase 2 until this phase completes.
Read intent_brief_file. Build specialist_configs using the Phase 2 slot algorithm (deterministic — compute now, launch in Phase 2). Write orchestration_plan_file.
Orchestration Plan schema
# Orchestration Decomposition Plan
## Metadata
- **Run ID**: ${BROWNFIELD_ID}
- **Effort**: <1-5>
- **Execute**: <true|false>
## Objective (one paragraph)
...
## Subtask Decomposition
| ID | Subtask | Assigned agent | Depends on | Est. input chars | Budget status | Transport mode | Utility score | Validation gate |
|----|---------|----------------|------------|------------------|---------------|----------------|---------------|-----------------|
| T-1 | ... | [architecture] | — | 85000 | OK | excerpt-only | high — all runs, prevents rot | architecture.md exists |
| T-2 | ... | [code] | T-1 | 120000 | OK | excerpt-only | medium — hot-path clarity | code.md exists |
## Specialist Roster (computed)
### Pass 1
- ...
### Pass 2
- ...
## Parallelization
- **Pass 1 parallel group**: ...
- **Pass 2 parallel group**: ...
- **Sequential gates**: pass 1 → checkpoint → pass 2
## Dependencies & Risks
| ID | Risk | Mitigation | Owner |
|----|------|------------|-------|
## Open Questions (Orchestration)
| ID | Question | Blocks | Status |
|----|----------|--------|--------|
## Validation Checklist (pre-Phase 3)
- [ ] Required specialists succeeded
- [ ] Blocking assumptions escalated or resolved
- [ ] Merged analysis covers all FP-* axioms from intent briefEffort-specific required sections:
| Effort | Required depth |
| 1 | Objective, Subtask table (≥2 rows), Specialist Roster, Parallelization (1 line each) |
| 2 | + Dependencies & Risks (≥2 rows), Validation Checklist |
| 3 | + per-specialist focus bullets (2–3 each), Open Questions table, Utility score on every subtask row (canonical formula) |
| 4 | + Risk register (≥4 rows), validation matrix mapping success criteria → specialists; Utility score on every subtask row |
| 5 | + bottleneck hypothesis, PR-risk preview if --execute, explicit parallel groups with rationale; every subtask row must have Est. input chars, Budget status, and Transport mode populated before Phase 2 |
Set orchestration_plan_written = true.
Delegation prompt rule
When spawning any specialist in Phase 2+, include in every prompt:
- Paths:
intent_brief_file,orchestration_plan_file(excerpt references — not full inline unless under budget) - This subtask's row from Subtask Decomposition (ID, validation gate,
Est. input chars,Budget status,Transport mode) - Effort-scaled focus bullets from Specialist Roster (effort ≥ 3)
- Excellence Doctrine block when
effort >= 3(include The Algorithm, Utility gate formula, Corps delegation) - Pre-spawn budget check: confirm composed prompt ≤ 400,000 chars; record estimate in
spawn_log_filebefore spawn - Excerpt-only handoff: persona + registered SKILL excerpts + task excerpts — orchestrator MUST NOT inject full
SKILL.mdinto any specialist prompt (KD-013)
Exit
orchestration_plan_fileexists and meets effort depth table- Subtask table includes
Est. input chars,Budget status,Transport mode,Utility scorecolumns; at effort ≥5 every row populated; at effort ≥3 every row has Utility score; effort <5 budget columns present (estimates recorded at spawn per pre-spawn gate) - No row with
Est. input chars> 400,000 unlessBudget status = CHUNKwith documented chunk plan spawn_log_fileinitialized at/tmp/grok-brownfield-spawn-log-${BROWNFIELD_ID}.md- Over-budget rows without CHUNK plan block Phase 2 advance
- Blocking orchestration open questions → AskQuestion, update plan, continue → Phase 2
Report (effort-dependent):
- Effort 1:
"Orchestration plan ready (minimal). Starting analysis pass 1 (Architecture)..." - Effort ≥2:
"Orchestration plan ready. Starting analysis pass 1 (Architecture + Product-Intent)..." - Effort ≥4: include risk count:
"Orchestration plan ready (N risks tracked). Starting pass 1..."
Phase 2: Assumption-Aware Analysis
Assumptions Register Schema
# Assumptions Register
## Summary
- **Total assumptions**: N
- **High confidence**: X
- **Medium confidence**: Y
- **Low confidence**: Z
- **Needs user confirmation**: W
## Entries
### A-001
- **Statement**: ...
- **Evidence**: file:line
- **Confidence**: high | medium | low
- **Status**: confirmed | provisional | needs_confirmation | rejected
- **Source**: [Architecture] | [Code] | [Tests] | [Product-Intent] | [Documentation] | [Security]
- **Impact if wrong**: bug | suggestion | nit | critical
- **Suggested validation**: ...
- **User question** (if needs_confirmation): ...Confidence levels:
| Level | Meaning | Action |
high | Multiple independent evidence sources agree | Proceed; cite in design doc |
medium | Evidence incomplete or single-source | Flag in Risks |
low | Inferred from naming, comments, partial reads | If blocking → Phase 2a; else Risks |
needs_confirmation is a Status value (not a Confidence level). Blocking when Status: needs_confirmation.
Blocking vs. Non-Blocking
Blocking if any of:
Status: needs_confirmationConfidence: lowANDImpact if wrong∈ {bug,critical}- Referenced in Intent Brief Success Criteria with open status
Non-blocking low-confidence (Impact ∈ {suggestion, nit}): document in Phase 3 Gaps & Risks only.
Analysis Specialist Catalog
| Specialization | Persona | Output | Pass | When |
| Architecture | prompt-only | architecture.md | 1 | Always |
| Product-Intent | prompt-only | product-intent.md | 1 | effort ≥ 2 |
| Code | reviewer | code.md | 2 | Always |
| Tests | prompt-only | tests.md | 2 | effort ≥ 2 OR intent signals |
| Security | security-auditor | security.md | 2 | effort ≥ 4 OR security signals |
| Documentation | prompt-only | documentation.md | 2 | effort ≥ 3 OR docs signals |
Slot Algorithm (pass-2)
Build intent_text = lowercase concat of project_context + Intent Brief contents (read after Phase 1).
Effort 1 behavior: Pass 2 always runs Code (1 specialist). No effort-mandated optionals. Intent keyword signals may still add Tests, Security, or Documentation in addition — e.g. a security-focused audit at effort 1 spawns Code + Security.
effort_mandated = [] # dedupe on insert
if effort >= 2: effort_mandated.append("tests") if "tests" not in effort_mandated
if effort >= 3: effort_mandated.append("documentation") if "documentation" not in effort_mandated
if effort >= 4: effort_mandated.append("security") if "security" not in effort_mandated
intent_additions = [] # dedupe on insert
# Intent-based (can add below effort threshold)
if intent_text matches auth|security|user input|api key|secret|encryption|permission|token|owasp:
intent_additions.append("security") if "security" not in intent_additions
if intent_text matches new logic|endpoint|data processing|algorithm|business rule:
intent_additions.append("tests") if "tests" not in intent_additions
if intent_text matches readme|adr|openapi|runbook|documentation|docs:
intent_additions.append("documentation") if "documentation" not in intent_additions
# Intent-only = triggered by keywords but not already effort-mandated
intent_only = [s for s in intent_additions if s not in effort_mandated]
PRIORITY = ["tests", "documentation", "security"]
# Always include effort-mandated; add intent-only in addition to effort-mandated (no optional_slots cap)
selected_optional = dedupe(effort_mandated + intent_only)
sort selected_optional by PRIORITY index (unknown entries last)
num_code = 2 if effort == 5 else 1
# pass-2 specialist count K = num_code + len(selected_optional) (may exceed nominal effort slots when intent expands)Building specialist_configs (deterministic)
specialist_configs = []
# Pass 1
specialist_configs.append({ pass: 1, specialization: "architecture",
persona_to_inject: null, output_file: .../architecture.md, background: true })
if effort >= 2:
specialist_configs.append({ pass: 1, specialization: "product-intent",
persona_to_inject: null, output_file: .../product-intent.md, background: true })
# Pass 2 — Code (always)
specialist_configs.append({ pass: 2, specialization: "code",
persona_to_inject: "reviewer", output_file: .../code.md, background: true })
if effort == 5:
specialist_configs.append({ pass: 2, specialization: "code-2",
persona_to_inject: "reviewer", output_file: .../code-2.md, background: true })
# Pass 2 — optionals from selected_optional
PERSONA_MAP = { tests: null, documentation: null, security: "security-auditor" }
for spec in selected_optional:
specialist_configs.append({ pass: 2, specialization: spec,
persona_to_inject: PERSONA_MAP[spec], output_file: .../{spec}.md, background: true })Pre-launch gate: Confirm orchestration_plan_written == true and orchestration_plan_file exists. If false, return to Phase 1b — do not improvise roster at launch time.
Announce pass-1 and pass-2 compositions once (must match orchestration plan roster), then launch.
Wait for Completion (after each parallel pass)
Mirror /implement Step 2 parallel reviewers:
- Launch all configs for the pass with
background: true - For each launch,
get_command_or_subagent_output(task_id=..., block=true) - Save each
subagent_idtospecialist_configs - Required specialists (Architecture, Code): failure → report error and stop
- Optional specialists (Product-Intent, Tests, Security, Documentation, code-2): failure → warn, remove from configs, continue
- Verify output files exist before checkpoint/merge
Two-Pass Launch Order
Pass 1 → wait → checkpoint → Pass 2 → wait → merge
Checkpoint after pass 1:
- Read
architecture.md(andproduct-intent.mdif present) - Write provisional
assumptions_file(pass-1 assumptions only; assign A-### ids) - Write partial
analysis_merged_file:
## Brownfield Analysis Findings (Pass 1 — provisional)
### Architecture Summary
<excerpt from architecture.md Executive Summary>
### Product-Intent Summary
<excerpt if present>
### Pass-1 Issues
<issues tagged [Architecture] / [Product-Intent]>
### Pass-1 Assumptions
<assumption excerpts for pass-2 context>Report: "Pass 1 complete. Starting analysis pass 2 (K specialists)..."
Merge Analysis (after pass 2)
Orchestrator concatenates and deduplicates specialist outputs — no new findings. Only orchestrator-authored metadata: A-### assignment, Summary counts, section headers.
Assumption ID procedure:
- Collect all
## Assumptionsentries from specialist files - Dedupe key = normalized statement + evidence
- Assign sequential
A-001,A-002, … - On collision, keep highest impact severity
- Recompute Summary block counts
Issue pattern accumulation: After merge, extract one-line description from each open issue in specialist files; append to issue_patterns (dedupe exact matches).
Final analysis_merged_file schema (after pass 2):
## Brownfield Analysis Findings
### Critical Gaps
### Bugs (confirmed or highly likely)
### Design / Architecture Issues
### Test Coverage Gaps
### Documentation Drift
### Security Findings
### Product-Intent Mismatches
### Assumptions Needing Confirmation
### Non-Blocking Low-Confidence Assumptions (document in Risks)Populate each subsection from specialist issues tagged by source. Code-2 dedup: when merging code.md + code-2.md, dedupe on file+description; keep higher severity; tag surviving issues [Code] or [Code-2] per origin.
Steps:
- Read all
analysis_dir/{specialization}.md(includingcode-2.mdat effort 5) - Merge into final
assumptions_file - Write final
analysis_merged_fileper schema above with source tags - Evaluate blocking assumptions
Report: "Analysis complete. N assumptions (W need confirmation)."
Phase 2a: Assumption Escalation
If blocking assumptions remain:
- Present each via AskQuestion (evidence + 2–4 options + "Other")
- Update
assumptions_file→confirmedorrejected - Increment
analysis_round_count - If
analysis_round_count > 3and still blocking → escalate to narrow scope
Re-run with resume_from when possible:
Map escalated A-### ids to Source tags → specialist_configs entries.
- Architecture/Product-Intent sources: resume affected pass-1 specialists via
resume_from(inject user decisions); re-checkpoint; re-run all pass-2 specialists (fresh or resume) - Pass-2 sources only: resume pass-2 specialists whose assumptions share the escalated A-### id or matching
Sourcetag; pass-1 outputs unchanged
Do not proceed to Phase 3 while blocking assumptions remain.
Specialist Prompt Templates
Architecture Specialist (prompt-only)
You are a senior systems architect performing brownfield architecture analysis.
CRITICAL: Existing code is EVIDENCE, not ground truth.
Apply **Excellence Doctrine (First Principles)** from the brownfield skill: decompose to axioms, reason up to ideal architecture, think in limits (10×/100× scale, p99 latency, idiot index on cost).
## Intent Brief
Read: <intent_brief_file>
## Orchestration Plan
Read: <orchestration_plan_file>
Your subtask: <T-ID from Subtask Decomposition for [architecture]>
## Your Task
- Reconstruct **intended** architecture from Intent Brief axioms and platonic ideal — then diff against observed code
- Module boundaries, layering, circular dependencies
- Data flow and trust boundaries
- Scalability bottlenecks, SPOFs (think in limits: what breaks at 10× load?)
- Performance: critical user journeys, serial bottlenecks, caching/async opportunities (cite hot paths)
- Divergence from documented architecture and from platonic ideal
- Legacy constraints validity — which are physics vs accident?
## Assumption discipline
For each claim: Statement, Evidence (file:line), Confidence, Impact if wrong
## Artifact security
Cite file:line only; redact secret values as [REDACTED].
## Output
Write to: <analysis_dir>/architecture.md
## Executive Summary
## Current Architecture (as observed)
## First-Principles Ideal (reconstructed)
## Intent Alignment
## Limits Analysis (scale, latency, cost)
## Issues (severity: bug|suggestion|nit)
### Issue N [Architecture] — Severity: ...
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: open
## Assumptions
## Recommended Investigation OrderLaunch: description: "[architecture] Brownfield pass-1 analysis", background: true.
Product-Intent Specialist (prompt-only)
You are a product-minded engineer validating implementation vs Intent Brief.
CRITICAL: Do not rationalize existing behavior as correct because code exists.
## Intent Brief
Read: <intent_brief_file>
## Orchestration Plan
Read: <orchestration_plan_file>
Your subtask: <T-ID from Subtask Decomposition for [product-intent]>
## Your Task
- Map user-visible flows to code paths
- Contradictions vs Intent Brief / user expectations
- Feature flags, dead paths, half-implemented flows
- UX-adjacent backend issues
## Artifact security
Redact secrets as [REDACTED].
## Output
Write to: <analysis_dir>/product-intent.md
Same Issue + Assumptions structure. Source tag: [Product-Intent]Launch: description: "[product-intent] Product-intent validation", background: true.
Documentation Specialist (prompt-only, pass 2)
You are auditing documentation fidelity. Pass 2 — pass-1 analysis complete.
## Intent Brief: <intent_brief_file>
## Orchestration Plan: <orchestration_plan_file>
Your subtask: <T-ID for [documentation]>
## Pass-1 Context
Read: <analysis_dir>/architecture.md
Read if present: <analysis_dir>/product-intent.md
Read: <assumptions_file>
## Your Task
- README, ADRs, inline docs, OpenAPI vs code
- Drift: missing | stale | misleading | accurate
- Operational docs gaps
## Output
Write to: <analysis_dir>/documentation.md
Source tag: [Documentation]. Redact secrets as [REDACTED].Launch: description: "[documentation] Documentation fidelity audit", background: true.
Code Specialist (pass 2)
Inject reviewer_persona_instructions. Include ## Past Issue Patterns block only when past_issues_briefing is non-empty.
<reviewer_persona_instructions>
---
Brownfield CODE analysis — existing codebase (not a diff). Pass 2.
Apply Excellence Doctrine: question whether code matches axioms/platonic ideal, not just style. Flag hot-path latency issues and structural waste (high idiot index).
## Intent Brief: <intent_brief_file>
## Orchestration Plan: <orchestration_plan_file>
Your subtask: <T-ID for [code] or [code-2]>
## Pass-1 Context
Read: <analysis_dir>/architecture.md
Read if present: <analysis_dir>/product-intent.md
Read: <assumptions_file>
## Scope
Prioritize Intent Brief + Orchestration focus bullets + Architecture findings + Limits Analysis.
## Assumption discipline
Record inferences with confidence; challenge pass-1 assumptions if code contradicts.
## Output
Write to: <analysis_dir>/code.md
### Issue N [Code] — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: open
## AssumptionsLaunch: description: "[code] Brownfield code analysis", background: true.
Code-2 Specialist (pass 2, effort 5 only)
Same as Code Specialist with independent review pass. Inject reviewer_persona_instructions. Include ## Past Issue Patterns only when past_issues_briefing is non-empty.
Source tag: [Code-2]. Output: <analysis_dir>/code-2.md. Launch: description: "[code-2] Brownfield code analysis (pass 2b)", background: true.
Tests Specialist (prompt-only, pass 2)
You are a thorough test engineer analyzing a brownfield codebase.
CRITICAL: Existing tests are EVIDENCE, not proof of adequate coverage. Green CI does not mean intent is validated.
## Intent Brief: <intent_brief_file>
## Orchestration Plan: <orchestration_plan_file>
Your subtask: <T-ID for [tests]>
## Pass-1 Context
Read: <analysis_dir>/architecture.md
Read if present: <analysis_dir>/product-intent.md
Read: <assumptions_file>
Include `## Past Issue Patterns` block only when `past_issues_briefing` is non-empty.
## Your Task
- Coverage vs intent success criteria and critical paths
- Edge cases, error paths, boundary conditions
- Assertion quality (not just "doesn't throw")
- Integration/e2e for user-visible flows
- Mocking appropriateness
- Flaky tests, brittle snapshots, missing negative-path tests
- Locate test infrastructure: package.json scripts, pytest.ini, Cargo.toml, CI workflows
- Undocumented or failing test commands; missing coverage thresholds
- Challenge pass-1 assumptions when test evidence contradicts them
## Output
Write to: <analysis_dir>/tests.md
## Issues
### Issue N [Tests] — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: open
## AssumptionsLaunch: description: "[tests] Test coverage analysis", background: true.
Security Specialist (pass 2)
Inject security_auditor_persona_instructions.
<security_auditor_persona_instructions>
---
IGNORE persona severity labels (critical/high/medium/low) and Finding format.
Use ONLY bug/suggestion/nit and Issue format below.
Analyze brownfield codebase for security issues. Pass 2.
## Intent Brief: <intent_brief_file>
## Orchestration Plan: <orchestration_plan_file>
Your subtask: <T-ID for [security]>
## Pass-1 Context
Read: <analysis_dir>/architecture.md
Read if present: <analysis_dir>/product-intent.md
Read: <assumptions_file>
Include `## Past Issue Patterns` block only when `past_issues_briefing` is non-empty.
Focus: input validation, authz, injection, secrets/PII, crypto, rate limiting, OWASP Top 10.
IMPORTANT severity mapping:
- bug: exploitable vulnerabilities
- suggestion: defense-in-depth
- nit: informational
Never copy credential values into security.md — file:line + [REDACTED] only.
## Output
Write to: <analysis_dir>/security.md
### Issue N [Security] — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: open
## AssumptionsLaunch: description: "[security] Security audit", background: true.
Verify Specialist (prompt-only)
You are validating brownfield improvements met original intent.
## Inputs
- Intent Brief: <intent_brief_file>
- Design doc: <design_doc_file>
- Execute summaries: <exec_summary_glob>
- Execute plan ID: <execute_plan_id> (delegated path only)
- Execute plan state: /tmp/grok-exec-plan-<execute_plan_id>.json (if delegated)
- Assumptions Register: <assumptions_file>
- Pre-implementation tests review: <analysis_dir>/tests.md (if exists)
- Merged analysis: <analysis_merged_file>
- PR completion stats: completed=<prs_completed>, failed=<prs_failed>, skipped=<prs_skipped>
## Task
1. Run documented test command from execute summaries if safe; cite exit codes
2. Map each Success Criterion → met | partially met | not met | not verified
3. Build **Intent Traceability Matrix**: each Success Criterion → design Key Decision → PR(s) → test/runtime evidence
4. Diff open [Tests] issues from tests.md against post-execute evidence
5. Flag regressions and unresolved assumptions
6. Note PRs not verified due to failed/skipped status
7. Assess first-principles gaps remaining (platonic ideal vs shipped state)
8. Classify blockers as bug/suggestion; recommend /implement for scoped fixes or new /brownfield for broad re-audit
9. v1: no verify review loop — informational only
## Output
Write to: /tmp/grok-brownfield-verify-${BROWNFIELD_ID}.md
## Intent Traceability Matrix
| Success Criterion | Key Decision | PR(s) | Evidence | Status |
|-------------------|--------------|-------|----------|--------|
## Success Criteria Verification
| Criterion | Status | Evidence |
|-----------|--------|----------|
| ... | met/partial/not met/not verified | ... |
## Regressions
### Issue N — Severity: bug|suggestion
- **Description**: ...
- **Suggestion**: ...
## Follow-up PRs
...
Phase 3: Consolidated Design Document
Launch design-doc-writer:
spawn_subagent parameters:
subagent_type:"general-purpose"description:"[writer] Write brownfield design doc"
<writer_persona_instructions>
---
Write a BROWNFIELD improvement design document.
## Inputs
- Intent Brief: <intent_brief_file>
- Merged analysis: <analysis_merged_file>
- Assumptions Register: <assumptions_file>
- Project context: <project_context>
## Required sections
- Current State Assessment
- First-Principles Assessment (axioms, platonic ideal gap, limits at scale)
- Validated Findings (grouped by severity)
- Assumptions & Confidence (A-### table)
- Gaps & Risks (prioritize by bottleneck / utility impact)
- Improvement Strategy (bottleneck-first ordering)
- Key Decisions (MANDATORY)
- PR Plan (MANDATORY, ### PR N: headings)
- Open Questions
Include Mermaid diagrams for current vs. proposed architecture where helpful.
Apply Excellence Doctrine: every PR should trace to an axiom or platonic-ideal gap, not cosmetic churn.
The PR Plan must be realistic for /execute-plan parsing (### PR N: headings).
Redact secrets in design doc. Write to: <design_doc_file> and <summary_file>Wait for completion. If subagent fails, report error and stop. Save writer_subagent_id.
Report: "Design document drafted. Starting design review..."
Phase 4: Design Review Loop
Severity taxonomy: Analysis findings use bug/suggestion/nit in merged analysis. Design-review issues use critical/major/minor/nit (per /design). Do not mix taxonomies in design_review_file.
Normative reference: /design Steps 2–5.
Step 4.1: Review
spawn_subagent:
subagent_type:"general-purpose"description:"[reviewer] Review brownfield design doc"
<design_reviewer_persona_instructions>
---
Review brownfield improvement design document.
Files: <design_doc_file>, <summary_file>, <assumptions_file>, <analysis_merged_file>
Write review to: <design_review_file>
Severity: critical|major|minor|nit. Every issue Status: open.
Pay special attention to: assumptions escalation, evidence citations, PR Plan ordering, do-no-harm rollout, Key Decisions.
**Excellence Doctrine review checklist** (flag open issues for any gap):
- First-Principles Assessment present and grounded in Intent Brief FP-* axioms
- Platonic ideal gap addressed — not just incremental patches on accidental design
- Limits thinking: scale, latency, cost (idiot index) for critical paths
- PR Plan is bottleneck-first; no drive-by refactors without utility justification
- Every Key Decision cites evidence or explicit user-confirmed assumption
## Artifact security
Redact secret values as [REDACTED] in review_file; cite file:line only.If fails → stop. Save design_reviewer_subagent_id. Increment round_count.
Step 4.2: Check Exit Condition
Read design_review_file. Count open and needs-user-input.
Decision order (strict):
- 0 open AND 0 needs-user-input → Phase 4b
- Any needs-user-input → Step 4.2a
- Stalemate (wontfix re-opened) → Step 4.2a
- Any open → Step 4.3
Stalemate detection: Compare against previous_design_review_snapshot after writer revision (snapshot taken post-Step 4.3, not pre-revise). If wontfix issue re-opened as open → stalemate.
Accumulate severities into total_issues_by_severity. Extract one-line descriptions from open issues → append to issue_patterns (dedupe).
Step 4.2a: Escalate to User
For needs-user-input and stalemate issues:
- Frame question in plain language for non-technical users
- Include reviewer position and writer position (stalemates)
- Provide selectable options + "Other"
- Include design-doc context
After user responds: resume writer (Step 4.3) with user decisions as final — incorporate without debate, set issues to addressed.
Step 4.3: Revise
spawn_subagent:
resume_from:<writer_subagent_id>description:"[writer] Revise design doc"
The review_file is at: <design_review_file>
Address ALL Status: open issues. Update design_doc_file and review_file (open → addressed + Response).
May set wontfix with justification or needs-user-input.
Append Revision Summary to review_file.If fails → stop. Update writer_subagent_id.
After Step 4.3 completes: update previous_design_review_snapshot from current design_review_file.
Step 4.4: Re-review
spawn_subagent:
resume_from:<design_reviewer_subagent_id>description:"[reviewer] Re-review design doc"
Rewrite design_review_file. Go back to Step 4.2 until 0 open.
Report on pass: "Design review passed with 0 issues. Finalizing Key Decisions and PR Plan..."
Phase 4b: Summarize and Ask Open Questions
4b.1: Extract Key Decisions
Present concise summary from design_doc_file § Key Decisions.
4b.2: Ask Open Questions
For unresolved Open Questions: plain-language AskQuestion with options + "Other". If answered: resume writer once, skip re-review.
4b.3: Present PR Plan
Read and present ## PR Plan.
- If
execute_flagis true: Present PR Plan summary informatively; do not re-ask whether to execute — proceed directly to 4b.4 and Phase 5. - If
execute_flagis false: Present PR Plan and offer paths: take design doc only, or re-invoke/brownfield --execute --effort <N> <project_context>.
If user opts in at 4b.3 without --execute flag: instruct exact re-invocation command (cannot start Phase 5 in same run without execute_flag).
4b.4: Prepare Execute Handoff + State File (if --execute)
Order (required):
- Build
instructions_file— readintent_brief_file+assumptions_file; extract Success Criteria + confirmed assumptions; scrub secrets; write to/tmp/grok-brownfield-instructions-${BROWNFIELD_ID}.txt. On resume rebuild: fail fast if source files missing. - Write
state_filewith current orchestrator values (not defaults). Setphaseby path:- Inline (default):
phase: "execute_inline",exec_summary_glob: "/tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-*.md" - Delegation (
--delegate-execute):phase: "execute_pending",exec_summary_glob: nulluntil PLAN_ID known
- Inline (default):
{
"brownfield_id": "<BROWNFIELD_ID>",
"plan_id": "<BROWNFIELD_ID>",
"execute_plan_id": null,
"exec_summary_glob": "<inline glob or null>",
"phase": "execute_inline|execute_pending",
"effort": "<effort>",
"delegate_execute": "<delegate_execute_flag>",
"max_concurrent": "<max_concurrent>",
"no_graphite_flag": "<no_graphite_flag>",
"auto_pr_flag": "<auto_pr_flag>",
"project_context": "<scrubbed project_context>",
"workspace_root": "<workspace_root>",
"intent_brief_file": "<intent_brief_file>",
"assumptions_file": "<assumptions_file>",
"design_doc_file": "<design_doc_file>",
"analysis_merged_file": "<analysis_merged_file>",
"design_doc_path": "<design_doc_file>",
"instructions_file": "<instructions_file>",
"status": "initializing",
"created_at": "<ISO 8601>",
"linearized_order": [],
"dag": { "nodes": [] },
"stack_assembly_started": false,
"stack_assembly_progress": [],
"graphite_stack_submitted": false,
"graphite_available": null,
"gh_available": null,
"pr_urls": [],
"pr_create_commands": [],
"user_instructions": "<scrubbed inline text from instructions_file>",
"analysis_round_count": "<analysis_round_count>",
"round_count": "<round_count>",
"prs_completed": 0,
"prs_failed": 0,
"prs_skipped": 0
}Per-PR dag.nodes[] include worktree_cleaned: false (and all execute-plan node fields: commit_sha, worktree_path, review_rounds, reviewer_configs, etc.) as execution progresses.
If --execute is false → skip state file; proceed to memory flush + Final Report.
Phase 5: Execution (--execute)
When execute_flag is true and Phase 4b completes.
Path selection (first action in Phase 5):
- If
delegate_execute_flag→ jump to Delegated Execution Path below. - Else → Inline Execution Path (primary). Read
instructions_fileintouser_instructions; setphase: "execute_inline"and persiststate_fileif not already set.
Path summary:
- Primary (default): Inline execute — worktree-isolated implementers, effort-scaled per-PR reviewers, stack assembly.
phase: execute_inline. - Fallback:
--delegate-execute→ nested/execute-planat effort 1.phase: execute_delegated.
You are the single point of control for all git and stack-tooling during inline execute. Subagents implement in worktrees; you create branches, collect commits, and submit/push stacks.
Report: "Executing PR Plan: M PRs, concurrency C, effort N, mode: inline|delegated"
Inline Execution Path (PRIMARY)
Substitute PLAN_ID = BROWNFIELD_ID and design_doc_path = design_doc_file throughout. Artifact prefix: /tmp/grok-brownfield-exec-{summary|review}-*. Branch prefix: brownfield/<BROWNFIELD_ID>-<pr-number>-<slug>.
Subagent Worktree Protocol
The orchestrator interacts with subagent worktrees through git commands that work uniformly across environments. Do not branch on host or worktree mechanism — this protocol is the only contract.
Rule 1 — fetch without a destination refspec. When you need subagent commits in the main repo:
git fetch "<worktree_path>" HEAD --no-tagsNever add :refs/heads/<pr.branch> and never pass --force. Always double-quote "<worktree_path>" in shell.
Rule 2 — pr.commit_sha is authoritative. After every fetch:
pr.commit_sha = $(git -C "<worktree_path>" rev-parse HEAD)
git cat-file -t "<pr.commit_sha>" # must print "commit"Downstream steps key off commit_sha, never refs/heads/<pr.branch> in the main repo.
Worktree path validation (on record and resume): When persisting or restoring pr.worktree_path, validate: absolute path, no .., no shell metacharacters, directory exists, is a git worktree under grok worktree root. Reject invalid paths before interpolation into shell.
Rule 3 — tear down worktree before mutating branch ref. Immediately before stack assembly mutates refs/heads/<pr.branch>:
if [ -n "<pr.worktree_path>" ] && [ -d "<pr.worktree_path>" ]; then
grok worktree rm --force "<pr.worktree_path>"
fiIdempotent — safe when worktree already gone or never created.
Two Assembly Modes
Recorded in state_file as graphite_available (probed in Step 0.5):
- Graphite mode (
graphite_available == true): per-PRgt create+ cherry-pick; singlegt submit --stackepilogue (Step 8a). - Plain-git mode (default when
gtmissing): plain git branch chain +git push --force-with-lease. PRs not auto-created unlessghavailable and orchestrator choosesgh pr create(optional; compare URLs printed otherwise).
Execute Todo Scaffold
Each DAG node → todo pr-<node-id> with sub-ids: pr-<n>:branch-prep, pr-<n>:execute, pr-<n>:review, pr-<n>:merge-ready. Terminal: all pr-<n>:merge-ready completed. Reseed from state_file after compaction.
Step 0.5: Tool Detection (Graphite & gh)
Probe once (safe if ... then ... else ... fi form):
if command -v gt >/dev/null 2>&1; then echo "yes"; else echo "no"; fi
if command -v gh >/dev/null 2>&1; then echo "yes"; else echo "no"; fiWhen gt probe returns yes, additionally verify gt --help 2>/dev/null | grep -qi graphite; treat unrecognized output as no.
gh_available = (gh probe == "yes")graphite_available = (gt verified AND no_graphite_flag == false)
Rewrite state_file with updated values. Report Graphite or plain-git mode (mirror execute-plan messages). Persist after probe.
Step 1: Parse PR Plan DAG
Read design_doc_file. Extract ## PR Plan; parse each ### PR N: into PRNode:
PRNode {
id, title, slug, description, files[], dependencies[], level,
status: "pending", branch, subagent_id, worktree_path,
base_sha, commit_sha, error, reviewer_subagent_ids: {},
review_rounds: 0, reviewer_configs: [], started_at, completed_at,
worktree_cleaned: false
}Slug rules (same as execute-plan): lowercase, hyphens, [a-z0-9-] only, truncate 50, strip .lock, fallback "unnamed". Validate DAG: resolve dependencies, detect cycles, unique ids. Store in dag.nodes. Persist state_file.
Report: "Parsed PR Plan: <N> PRs found."
Step 2: DAG Processing and Linearization
Assign levels: level = 0 if no deps else max(level(dep)) + 1.
Linearize (deterministic — sort by numeric PR number within level, not lexicographic id):
def linearize(dag):
nodes_by_level = group_by(dag.nodes, key=n.level)
result = []
for level in sorted(nodes_by_level.keys()):
for node in sorted(nodes_by_level[level], key=lambda n: int(n.id.split('-')[1])):
result.append(node)
return resultBranch name: brownfield/<BROWNFIELD_ID>-<pr-number>-<slug>. Store linearized_order; persist state_file.
Report linearized order, max parallelism, level count.
Step 3: Branch Preparation
git fetch origin mainLevel 0: git branch <pr.branch> origin/main; pr.base_sha = $(git rev-parse <pr.branch>)
Single dependency (JIT when dep completes): git branch <pr.branch> <dep.commit_sha>
Multiple dependencies (diamond):
git branch "<pr.branch>" "<first-dep.commit_sha>"
TEMP_WT=$(mktemp -d)
git worktree add "$TEMP_WT" "<pr.branch>"
git -C "$TEMP_WT" merge "<second-dep.commit_sha>" --no-edit
git worktree remove "$TEMP_WT"
pr.base_sha = $(git rev-parse "<pr.branch>")Key off dep.commit_sha, never branch name. Do NOT switch orchestrator checked-out branch — use git branch without checkout.
Set status "branch_created"; persist state_file.
Report: "Created branches for <N> level-0 PRs. Starting implementation..."
Step 4: Execution Loop
Ready-queue loop (mirror execute-plan):
ready_queue = PRs with status "branch_created" and deps completed
while ready_queue or in_progress:
launch up to max_concurrent implementers
wait_commands_or_subagents(mode="wait_any", timeout_ms=600000)
on timeout (>15min): kill, mark failed, cascade_skip
on completion: process_completion → review_pr
enqueue newly-ready PRs (create branches JIT)Step 4a: Launch Implementer
After worktree creation, push branch into worktree before implementer checks out:
git push "<worktree_path>" refs/heads/<pr.branch>:refs/heads/<pr.branch>spawn_subagent: subagent_type: "general-purpose", isolation: "worktree", background: true, description: "[implementer] <pr.id>: <pr.title>"
Prepend implementer_persona_instructions. Prompt:
<implementer_persona_instructions>
---
You are implementing a single PR as part of a brownfield improvement plan.
## Your PR
- Title: <pr.title>
- Description: <pr.description>
- Files to modify: <pr.files joined by comma>
- Branch: <pr.branch>
## Context from Design Document
<relevant sections from design_doc_file for this PR's scope>
<if past_issues_briefing is non-empty, include verbatim:>
<past_issues_briefing>
Be proactive about avoiding these patterns in your implementation.
<end if>
<if user_instructions is non-empty, include verbatim:>
## User Instructions
<user_instructions>
These instructions apply to all work in this plan. Follow them strictly.
<end if>
## Artifact security
Cite secrets at file:line only; redact values as [REDACTED].
## Instructions
1. git checkout <pr.branch>
2. Implement the changes described above.
3. Verify compile/basic checks (cargo check, tsc --noEmit, pytest, etc.).
4. Commit all changes with a descriptive message.
5. Write implementation summary to: /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md
Include: files changed, key decisions, deviations from plan.Set status "implementing"; persist state_file.
Step 4b: Process Completion
On success: record subagent_id, worktree_path (validate before persist per Worktree Protocol); git -C "<worktree_path>" rev-parse HEAD → commit_sha; git fetch "<worktree_path>" HEAD --no-tags; git cat-file -t "<commit_sha>". Status → "reviewing".
On failure: status "failed", cascade_skip (Step 6). Persist after each transition.
Step 5: Per-PR Review (Effort-Scaled)
Build reviewer_configs per PR using /implement Specialization Selection with description = pr.title + " " + pr.description + design doc excerpt. Use brownfield effort (1–5), not hardcoded 1.
Effort → reviewer slots (same as /implement):
| Effort | Reviewers | Composition |
| 1 | 1 | General reviewer |
| 2 | 2 | General + specialist OR 2 generals |
| 3 | 3 | Up to 2 generals + specialists |
| 4 | 5 | 3 generals + tests + security (typical) |
| 5 | 6 | 3 generals + tests + security + plan alignment (when matched) |
Decision algorithm (per PR) — port /implement Specialization Selection Step 2:
description = pr.title + " " + pr.description + " " + <design doc excerpt for this PR>
if effort <= 3: total_slots = effort
elif effort == 4: total_slots = 5
else: total_slots = 6
matched_specialists = []
if description mentions auth, security, user input, API keys,
secrets, encryption, permissions, tokens, or OWASP:
matched_specialists.append("security")
if description references design doc, plan, spec, RFC, PR Plan,
or linked document:
matched_specialists.append("plan_alignment")
if description involves new logic, endpoints, data processing,
algorithms, or business rules:
matched_specialists.append("tests")
specialists = matched_specialists[:total_slots - 1]
num_generals = total_slots - len(specialists)Building reviewer_configs (per PR):
reviewer_configs = []
for i in 1..num_generals:
tag = "general" if i == 1 else f"general-{i}"
reviewer_configs.append({
subagent_id: null,
persona_to_inject: "reviewer",
specialization: tag,
review_file: effort == 1
? "/tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-<pr.id>.md"
: f"/tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-<pr.id>-{tag}.md"
})
suffix_map = {tests: "tests", security: "security", plan_alignment: "plan"}
for specialist in specialists:
reviewer_configs.append({
subagent_id: null,
persona_to_inject: specialist == "security" ? "security-auditor" : null,
specialization: specialist,
review_file: f"/tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-<pr.id>-{suffix_map[specialist]}.md"
})Announce once per PR: "<pr.id> review: effort <N>, <total_slots> reviewers (<composition>)"
Merged review file: /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-<pr.id>.md
Step 5a: Launch Reviewer(s)
Read exec summary at /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md; derive reviewer_focus_areas (2–3 items).
Effort 1: Single reviewer with cwd: "<pr.worktree_path>", prepend reviewer_persona_instructions. Prompt:
<reviewer_persona_instructions>
---
Review the changes made for <pr.id>: <pr.title>.
The implementation summary is at: /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md
Review all modified files in the worktree (cwd).
<if past_issues_briefing is non-empty, include verbatim:>
<past_issues_briefing>
<end if>
<if user_instructions is non-empty, include verbatim:>
## User Instructions
<user_instructions>
<end if>
<if reviewer_focus_areas is non-empty:>
## Additional focus areas (from implementation summary)
<reviewer_focus_areas>
<end if>
## Artifact security
Redact secrets as [REDACTED].
Write review to: /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-<pr.id>.md
### Issue N — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: openEffort ≥ 2: Launch all reviewers in parallel (background: true) with cwd: "<pr.worktree_path>". Use the inlined Specialized Review Prompts below (brownfield exec artifact paths; do not cross-lookup /implement).
Exec summary path: /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md
General Reviewer (persona_to_inject: "reviewer"):
<reviewer_persona_instructions>
---
Review the changes made for <pr.id>: <pr.title>.
The implementation summary is at: /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md
Review all modified files in the worktree (cwd).
<if past_issues_briefing is non-empty, include verbatim:>
<past_issues_briefing>
<end if>
<if user_instructions is non-empty, include verbatim:>
## User Instructions
<user_instructions>
<end if>
<if reviewer_focus_areas is non-empty:>
## Additional focus areas (from implementation summary)
<reviewer_focus_areas>
<end if>
## Artifact security
Redact secrets as [REDACTED].
Write review to: <reviewer_configs[i].review_file>
### Issue N — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: openTests Specialist (prompt-only; no persona):
You are a thorough test engineer reviewing code changes for test coverage and quality.
Review the changes made for <pr.id>: <pr.title>, focusing specifically on test coverage and quality.
The implementation summary is at: /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md
Review all modified files in the worktree (cwd).
<if past_issues_briefing is non-empty, include verbatim:>
<past_issues_briefing>
<end if>
<if user_instructions is non-empty, include verbatim:>
## User Instructions
<user_instructions>
<end if>
<if reviewer_focus_areas is non-empty:>
## Additional focus areas (from implementation summary)
<reviewer_focus_areas>
<end if>
Your review should focus on:
- Whether new/changed code has adequate test coverage
- Whether tests cover edge cases, error paths, and boundary conditions
- Whether test assertions are specific enough (not just "doesn't throw")
- Whether tests are maintainable and not overly coupled to implementation details
- Whether integration tests exist for new endpoints or interfaces
- Whether mocking is used appropriately (not over-mocking)
Do NOT review for general code style, naming, or architecture — another reviewer handles that.
## Artifact security
Redact secrets as [REDACTED].
Write review to: <reviewer_configs[i].review_file>
### Issue N — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: openSecurity Specialist (persona_to_inject: "security-auditor"):
<security_auditor_persona_instructions>
---
Review the changes made for <pr.id>: <pr.title>, focusing specifically on security.
The implementation summary is at: /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md
Review all modified files in the worktree (cwd).
<if past_issues_briefing is non-empty, include verbatim:>
<past_issues_briefing>
<end if>
<if user_instructions is non-empty, include verbatim:>
## User Instructions
<user_instructions>
<end if>
<if reviewer_focus_areas is non-empty:>
## Additional focus areas (from implementation summary)
<reviewer_focus_areas>
<end if>
Your review should focus on:
- Input validation and sanitization
- Authentication and authorization checks
- Injection vulnerabilities (SQL, command, path traversal)
- Sensitive data handling (secrets, PII, tokens in logs)
- Cryptographic correctness
- Rate limiting and abuse prevention
- OWASP Top 10 patterns
IMPORTANT: Use severity labels bug/suggestion/nit (not security-standard severities):
- bug: critical/high (exploitable vulnerabilities)
- suggestion: medium (defense-in-depth)
- nit: low/informational (best-practice)
Only flag real, exploitable issues — not theoretical concerns.
Do NOT review for general code style or test coverage — other reviewers handle that.
## Artifact security
Redact secrets as [REDACTED].
Write review to: <reviewer_configs[i].review_file>
### Issue N — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: openPlan Alignment Specialist (prompt-only; no persona):
You are a technical lead reviewing whether an implementation correctly follows its design plan.
Review the changes made for <pr.id>: <pr.title>, focusing on whether the implementation matches the plan/design.
The implementation summary is at: /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-<pr.id>.md
Review all modified files in the worktree (cwd).
Read the design document in full before starting: <design_doc_file>
<if past_issues_briefing is non-empty, include verbatim:>
<past_issues_briefing>
<end if>
<if user_instructions is non-empty, include verbatim:>
## User Instructions
<user_instructions>
<end if>
<if reviewer_focus_areas is non-empty:>
## Additional focus areas (from implementation summary)
<reviewer_focus_areas>
<end if>
Your review should focus on:
- Whether all requirements from the plan are addressed for this PR's scope
- Whether the implementation deviates from the planned approach
- Whether any scope creep has occurred (implementing things not in the plan)
- Whether any planned items for this PR are missing
- Whether interfaces match what was specified
Do NOT review for code style, tests, or security — other reviewers handle that.
## Artifact security
Redact secrets as [REDACTED].
Write review to: <reviewer_configs[i].review_file>
### Issue N — Severity: bug|suggestion|nit
- **File**: path:line
- **Description**: ...
- **Suggestion**: ...
- **Status**: openWait via get_command_or_subagent_output(block=true) for each. Save subagent_id to reviewer_configs[].subagent_id. General failure → stop PR; specialist failure → warn, remove from configs, continue.
Step 5b: Merge & Check Results
Effort 1: Read merged review file; count Status: open.
Effort ≥ 2: Read individual files; merge into /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-<pr.id>.md with source tags [General], [General-2], [Tests], [Security], [Plan]. Dedupe obvious duplicates; when in doubt keep both.
Extract one-line descriptions → issue_patterns (dedupe). Add severities to total_issues_by_severity. Increment pr.review_rounds.
- 0 open issues: status
"completed",completed_at; return to Step 4 loop. - Any open: Step 5c fix cycle (no iteration cap — all severities must reach 0).
Step 5c: Fix Cycle
Resume implementer (resume_from: pr.subagent_id). Prompt: read merged review file, fix all Status: open, update to fixed/wontfix, commit, include user_instructions.
Re-fetch commits (Rule 1–2). Resume reviewer(s) (resume_from per reviewer_configs[].subagent_id; update all reviewer_configs[].subagent_id after each resume round). On resume failure: fresh launch with full Step 5a prompt + cwd: "<pr.worktree_path>"; update ids. Re-merge if effort ≥ 2.
Stalemate: wontfix re-opened → escalate to user via AskQuestion; user decision is final.
Persist state_file after each round.
Step 6: Failure Handling
On PR failure: mark "failed" with error; cascade_skip all transitive dependents (status: "skipped", error: "Skipped: dependency <id> failed"). Independent PRs continue. Persist state_file.
Step 7: Resumption (--resume with phase: execute_inline or legacy execute)
- Read
state_fileat/tmp/grok-brownfield-state-${BROWNFIELD_ID}.json - Restore:
design_doc_file,effort,max_concurrent,dag,linearized_order,user_instructions,no_graphite_flag,auto_pr_flag,graphite_available,gh_available,pr_urls,pr_create_commands,prs_completed,prs_failed,prs_skipped, brownfield artifact paths. Defaults for missing fields:no_graphite_flag: false,auto_pr_flag: false,pr_create_commands: [],stack_assembly_progress: []. Ifgraphite_availableorgh_availableis null, re-run Step 0.5 probe. - Re-validate each
dagnode slug/branch andworktree_path(see Invocation resume validation) - Per PR status:
completed→ skipfailed→ ifworktree_pathset andworktree_cleanedis false,grok worktree rm --force "<worktree_path>"; reset to"pending"(clear error, subagent_id, reviewer ids inreviewer_configs, worktree_path, base_sha, commit_sha, review_rounds, worktree_cleaned)skipped→ re-evaluate if deps now completedimplementing/reviewing→ cleanup worktree (worktree_cleanedguard); reset to"pending"; clear reviewer ids inreviewer_configspending/branch_created→ keep
- If
stack_assembly_started&& !graphite_stack_submitted:- Graphite mode:
gt ls, delete partial branches withgt delete, reset to main - Plain-git mode:
git cherry-pick --abort 2>/dev/null || true git merge --abort 2>/dev/null || true git rebase --abort 2>/dev/null || true git fetch origin main git checkout -f main git reset --hard origin/main while IFS= read -r branch; do git branch -D "$branch"; done < <(git for-each-ref --format='%(refname:short)' "refs/heads/brownfield/<BROWNFIELD_ID>-*") - Set
stack_assembly_started = false; clearstack_assembly_progress
- Graphite mode:
- Re-read
design_doc_file; resume from Step 3 + Step 4
Report: "Resuming brownfield execute ${BROWNFIELD_ID}. <N> completed, <M> to retry, <K> skipped."
Step 8: Stack Assembly
When all PRs are completed, failed, or skipped AND ≥1 completed:
Set stack_assembly_started = true; persist.
Branch on graphite_available:
true→ Step 8a (Graphite mode)false→ Step 8a (plain-git mode)
Step 8a (Graphite mode): Build the Stack
One-time prologue at loop start:
git checkout main
git pull origin mainPer PR (linearized order, skip failed/skipped):
# Rule 3 — free branch name
if [ -n "<pr.worktree_path>" ] && [ -d "<pr.worktree_path>" ]; then
grok worktree rm --force "<pr.worktree_path>"
fi
pr.worktree_cleaned = true
gt create "<pr.branch>" --no-interactive
git cat-file -t "<commit_sha>"
git cherry-pick <base_sha>..<commit_sha> --allow-emptyCherry-pick conflicts: orchestrator resolves directly (read files, merge intents, remove markers, git add, git cherry-pick --continue); unresolvable → abort, mark PR failed, cascade-skip, continue stack.
Epilogue (once after all PRs):
gt submit --stack --no-edit --no-interactiveDo not set graphite_stack_submitted here — wait until Step 8b completes.
Step 8a (plain-git mode): Build the Stack
test -z "$(git status --porcelain)" || { echo "Working tree dirty; aborting"; exit 1; }
git fetch origin main
git checkout main
git reset --hard origin/main
parent_branch="main"Per PR (linearized order, skip failed/skipped):
if [ -n "<pr.worktree_path>" ] && [ -d "<pr.worktree_path>" ]; then
grok worktree rm --force "<pr.worktree_path>"
fi
pr.worktree_cleaned = true
git checkout -B "<pr.branch>" "$parent_branch"
git cat-file -t "<commit_sha>"
git cherry-pick <base_sha>..<commit_sha> --allow-empty
git push --force-with-lease origin "<pr.branch>" # retry once after 5s on failure
parent_branch="<pr.branch>"Record progress in stack_assembly_progress. On push failure after retry: mark PR failed, cascade-skip, do not advance parent_branch. Cherry-pick conflict handling same as Graphite mode; unresolvable → git cherry-pick --abort, git checkout main, mark failed, do not advance parent_branch.
Do not set graphite_stack_submitted until Step 8b completes.
Step 8b (Graphite mode): Collect PR URLs
After gt submit succeeds:
gt ls --jsonParse PR URLs per branch. Store in pr_urls (plain URL strings). Persist pr_urls. Set graphite_stack_submitted = true.
Report: "Stack submitted: <N> PRs. URLs: [...]"
Step 8b (plain-git mode): Collect PR URLs
After all branches are pushed, derive a GitHub-style compare URL for each branch in stack order. Branch prefix: brownfield/<BROWNFIELD_ID>-*.
remote_url=$(git config --get remote.origin.url)Parse remote_url into (host, owner, repo). Accept any of these forms, in order:
git@<host>:<owner>/<repo>[.git](canonical SSH)ssh://git@<host>[:port]/<owner>/<repo>[.git](scheme-prefixed SSH)https://[<user>[:<pass>]@]<host>/<owner>/<repo>[.git][/](HTTPS, optionally with embedded credentials and/or trailing slash)
A pragmatic regex that captures all three (operate against the trimmed remote URL):
^(?:git@|ssh://(?:[^@]+@)?|https?://(?:[^@/]+@)?)([^:/]+)[:/](?:[^/]+/)*([^/]+)/([^/]+?)(?:\.git)?/?$Capture groups: host, owner, repo. Strip any user[:pass]@ segment before logging the URL anywhere to avoid leaking credentials embedded in the remote URL.
- If
hostdoes not matchgithub.comorgithub.*(e.g.,github.example.comfor GHE), or if the regex fails entirely, the remote is non-GitHub. Skip compare-URL synthesis and skipgh pr create(even if--auto-prwas set). Instead, populatepr_urlswith{branch: <pr.branch>, url: null, kind: "pushed-only", note: "non-GitHub remote (<remote_url with credentials stripped>) — open a PR via your forge UI"}for each pushed branch. Continue to Step 8c.
Otherwise, for each PR in linearized stack order, compute:
compare_url = "https://<host>/<owner>/<repo>/compare/<parent_branch>...<pr.branch>?expand=1"where <parent_branch> is the branch immediately below this PR in the stack (or main for the bottom).
pr_urls in plain-git mode is a list of objects, one per pushed branch in stack order:
{
"branch": "<pr.branch>",
"url": "<URL string or null>",
"kind": "pr" | "compare" | "pushed-only",
"note": "<optional explanatory string>"
}The note field is optional: populated for pushed-only entries and may be attached to compare entries that resulted from a failed gh pr create invocation.
If auto_pr_flag == true AND gh_available == true AND host is GitHub/GHE: create draft PRs sequentially:
gh pr create --base "<parent_branch>" --head "<pr.branch>" --fill --draftCapture stdout (created PR URL) and append an entry with kind: "pr". If gh pr create fails for a single branch, log the failure, append an entry with kind: "compare" and the synthesized compare URL (attach error in note), and continue. Sequential — never parallel — to preserve ordering.
Otherwise (no --auto-pr, or no gh): populate every entry with kind: "compare" and the synthesized compare URL. Build pr_create_commands:
gh pr create --base <parent_branch> --head <pr.branch> --fill --draftone per PR in stack order.
Persist pr_urls, pr_create_commands. Set graphite_stack_submitted = true.
Step 8c: Stack Assembly Reporting
Report using the variant that matches the mix of pr_urls entry kinds (plain-git) or Graphite URL list:
- All
kind: "pr":"Stack pushed and PRs created: <N> branches. URLs: [...]" - All
kind: "compare":"Stack pushed: <N> branches. Open these compare URLs (or run the gh pr create commands) to open PRs: [...]" - Mixed
kind: "pr"andkind: "compare":"Stack pushed: <N> branches. <X> PRs were created via gh, <Y> need to be opened manually via the printed compare URLs." - Any
kind: "pushed-only":"Stack pushed: <N> branches to non-GitHub remote. Open PRs via your forge UI." - Graphite mode (plain URL strings in
pr_urls):"Stack submitted: <N> PRs. URLs: [...]"
Include pr_urls and pr_create_commands in state for Final Report. Render note verbatim when present on plain-git entries.
Step 9: Execute Cleanup
Normative order: Run Step 9 after Phase 6 (Inline Path Completion step 4). Do not delete exec summaries before verify reads them.
Kill implementer/reviewer subagents:
for each pr in dag.nodes:
if pr.subagent_id is not null:
kill_command_or_subagent(pr.subagent_id)
for each config in pr.reviewer_configs:
if config.subagent_id is not null:
kill_command_or_subagent(config.subagent_id)Remove remaining worktrees (worktree_cleaned guard):
for each pr in dag.nodes where worktree_path is not null and not pr.worktree_cleaned:
if directory "<worktree_path>" exists:
grok worktree rm --force "<worktree_path>"
pr.worktree_cleaned = trueRemove per-PR exec summary/review temps:
rm -f /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-*.md
rm -f /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-*.mdKeep state_file for resume/history (strip volatile fields on phase: "complete" — see Inline Path Completion).
Inline Path Completion
Normative order: Step 8 (8a→8b→8c) → Phase 6 gate → Step 9 cleanup → memory flush → Final Report.
- Count
prs_completed,prs_failed,prs_skippedfromdag.nodes - Set
exec_summary_glob = "/tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-*.md" - Collect execute review patterns into
issue_patterns - Phase 6 gate — if
prs_completed < 1, cancelpost-verify; else setphase: "verify", persist, run Phase 6 (verify reads exec summaries before Step 9 rm) - Step 9 cleanup (exec artifact rm)
- Memory flush → Final Report → set
phase: "complete"; strip volatile fields fromstate_file(worktree_path,commit_sha,user_instructions,reviewer_configsper node); persist
Delegated Execution Path (FALLBACK — --delegate-execute)
When delegate_execute_flag is true:
- Read bundled
execute_plan_skill_pathviaread_file - Read
instructions_file; store asuser_instructions - Context-budget check: Before nested
/execute-plan, estimate total handoff payload (instructions_fileexcerpts +design_doc_fileexcerpts + persona). If > 400,000 chars, chunk into excerpt artifacts — excerpt-only handoff to nested orchestrator; never pass full design doc or instructions inline if over cap. Apply KD-013 — no fullSKILL.mdin nested handoff. Record inspawn_log_file(spawn-log continuity with prior phases). - Follow execute-plan as nested orchestrator — do not merely echo a slash command
- Invoke with file-only instructions (no secrets in argv):
If/execute-plan <design_doc_file> --effort 1 --concurrency <max_concurrent> --instructions-file <instructions_file>--instructions-fileunsupported, use--instructionswith path reference only:"See instructions at <instructions_file>"— never embed scrubbed content in process args. - Forward flags when set: append
--no-graphiteifno_graphite_flag; append--auto-prifauto_pr_flag
Delegation start persistence: When execute-plan Setup reports PLAN_ID, immediately persist execute_plan_id, phase: "execute_delegated", exec_summary_glob: "/tmp/grok-exec-summary-${execute_plan_id}-*.md".
Delegated Path Completion:
- Merge stats from
/tmp/grok-exec-plan-${execute_plan_id}.json(dagnode statuses,pr_urls,prs_completed, etc.) - Collect review patterns into
issue_patterns - Phase 6 gate — if
prs_completed < 1, cancelpost-verify; else setphase: "verify", persist, run Phase 6 - Memory flush → Final Report →
phase: "complete"(strip volatile fields); persist
Delegated resume: /brownfield --resume rejected when phase == "execute_delegated". Use /execute-plan --resume <execute_plan_id>.
Without --execute
Suggest primary path:
/brownfield --execute --effort <N> <project_context>Alternative (lower review rigor):
/brownfield --execute --delegate-execute <project_context>or /execute-plan <design_doc_file> --effort 1.
Phase 6: Post-Implementation Verification
Runs only when prs_completed >= 1.
| Path | exec_summary_glob |
| Inline | /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-*.md |
| Delegated | /tmp/grok-exec-summary-${execute_plan_id}-*.md |
Spawn [verify] subagent using Verify Specialist template. Include validated execute_plan_id when delegated.
Wait for completion (mirror Phase 2):
spawn_subagentwithbackground: false(or block ontask_idif background)get_command_or_subagent_output(task_id=..., block=true)- If subagent fails → report error; note in Final Report; still run memory flush
- Confirm
/tmp/grok-brownfield-verify-${BROWNFIELD_ID}.mdexists
Report: "Post-implementation verification complete."
Memory Flush
Follow /implement Step 6 verbatim (6a–6d) with brownfield substitutions.
| implement | brownfield |
IMPL_ID | BROWNFIELD_ID |
review_file | design_review_file + execute review files |
run.description | "Brownfield: <short project_context>" |
run.specializations | analysis tags + execute review tags |
run.rounds | round_count + execute review rounds |
| Temp JSON | /tmp/grok-brownfield-mem-${BROWNFIELD_ID}.json |
Timing: once — after Phase 4b (no execute); after Phase 5 completion post-Phase-6-gate (execute path, including 0-PR skip).
Step 6a: Collect & Categorize
Use issue_patterns accumulated across merge + design review (+ execute reviews). Generalize each pattern — strip file/variable/domain names; rewrite as reusable principles:
- Bad: "JWT token not validated for expiration" → Good: "Missing expiration/TTL validation on tokens or credentials"
- Bad: "No test for
handleUserAutherror path" → Good: "Missing tests for error/edge case paths" - If already general, keep as-is; collapse duplicates
Categorize: Error Handling, Testing, Security, Code Quality, Naming, Documentation, Performance, Architecture, Assumptions.
Step 6b: Harmonize Phrasing
Against existing_patterns_snapshot, match semantically equivalent descriptions; use exact existing description string when matched. Dedup within this run's list too.
Step 6c–6d
Build JSON spec; write /tmp/grok-brownfield-mem-${BROWNFIELD_ID}.json; python3 <memory_helper_path> update < /tmp/grok-brownfield-mem-${BROWNFIELD_ID}.json. Graceful degradation on failure.
Cleanup
After Final Report:
rm -f <design_review_file> /tmp/grok-brownfield-mem-${BROWNFIELD_ID}.json <instructions_file>
rm -f /tmp/grok-brownfield-exec-summary-${BROWNFIELD_ID}-*.md # if not already removed in Step 9
rm -f /tmp/grok-brownfield-exec-review-${BROWNFIELD_ID}-*.md
rm -f /tmp/grok-brownfield-analysis-${BROWNFIELD_ID}/*.md # optional
if cleanup_deliverables:
rm -f <state_file> /tmp/grok-brownfield-verify-${BROWNFIELD_ID}.mdDefault post-run retention: Keep primary deliverables (intent, assumptions, analysis merged, design doc, summary) unless user passes --cleanup-deliverables (document in Final Report).
Always remove: design_review_file, mem temp JSON, instructions_file, per-specialist analysis files.
With --cleanup-deliverables: After Final Report, also rm -f state_file and verify file (/tmp/grok-brownfield-verify-${BROWNFIELD_ID}.md).
Manual scrub (Final Report bullet 13):
rm -f /tmp/grok-brownfield-*-${BROWNFIELD_ID}* # removes all run artifacts for this runFinal Report
Present after memory flush. Read deliverables before cleanup.
Required bullets (15)
- Run metadata —
BROWNFIELD_ID, effort,--execute,project_context,workspace_root - Intent brief — path; 1-line goal
- Orchestration plan —
orchestration_plan_file; subtask count; risks tracked - Assumptions summary — counts; non-blocking low-confidence items
- Analysis —
analysis_merged_file; specialist composition;analysis_round_count - Design document —
design_doc_file - Design review stats —
round_count;total_issues_by_severity - Key Decisions — excerpt
- Open Questions — resolved or none
- PR Plan — summary or path
- Execution — PRs completed/failed/skipped;
execute_plan_idif delegated; resume command byphase - Verification — verify file path; Intent Traceability Matrix + success criteria table (or "skipped — 0 PRs completed")
- Memory update — patterns; path; created vs updated
- Cleanup — removed vs retained; manual scrub command
- Suggested follow-ups —
/brownfield --execute --effort N,/brownfield --resume <BROWNFIELD_ID>(inline),/execute-plan --resume <id>(delegated),/implement
In-Progress Reporting
| Event | Message |
| Setup | "Brownfield run ${BROWNFIELD_ID}, effort N: <tier> orchestration, <rosters>, execute reviewers: <slots>" |
| Intent | "Intent brief captured. Building orchestration decomposition plan (effort N)..." |
| Orchestration (effort 1) | "Orchestration plan ready (minimal). Starting analysis pass 1 (Architecture)..." |
| Orchestration (effort ≥2) | "Orchestration plan ready. Starting analysis pass 1 (Architecture + Product-Intent)..." |
| Orchestration (effort ≥4) | "Orchestration plan ready (N risks tracked). Starting pass 1..." |
| Pass 1 done | "Pass 1 complete. Starting analysis pass 2 (K specialists)..." |
| Analysis merge | "Analysis complete. N assumptions (W need confirmation)." |
| Escalation | "Assumption A-### needs your input." |
| Design drafted | "Design document drafted. Starting design review..." |
| Design 0 issues | "Design review passed with 0 issues. Finalizing..." |
| Execute launch | "Executing PR Plan: M PRs, concurrency C, effort N, mode: inline|delegated" |
| Tool detection | "Graphite mode: ..." or "Plain-git mode: ..." |
| Delegation start | "Delegated to execute-plan PLAN_ID=...; state persisted" |
| Per PR | "Launching <pr.id>...", review rounds, fix cycles (mirror execute-plan) |
| Stack assembly | "All PRs complete. Building Graphite|git branch stack..." |
| Inline resume | "Resuming brownfield execute ${BROWNFIELD_ID}..." |
| Verify | "Post-implementation verification complete." |
| Memory | Mirror implement memory flush messages |
Rules
- Orchestrator identity — coordinate only; specialists investigate and author; orchestrator plans, delegates, merges, enforces gates
- Context budget protocol — 400,000-char cap; pre-spawn gate; excerpt-only downstream; never inject full SKILL.md; spawn log at
spawn_log_file - Protocol enforcement — phase gates are normative; violation → rewind and run skipped phase
- Orchestration decomposition — Phase 1b required before analysis; effort scales plan depth; budget columns always on subtask table (populated at effort ≥5)
- Effort model — visible delta 1→5 in plan depth, specialist roster, reviewer slots
- Excellence Doctrine — first-principles decomposition, limits thinking, 0 open issues, bottleneck-first PR plans
- Tool-call discipline — paired
spawn_subagentfor every launch claim - Bundled path resolution —
bundled_skills_rootalgorithm; never../shared/personasfrom user scope - Memory via implement helper — single
updateper run - 0 open issues exit — design review: critical/major/minor/nit
- Assumptions first-class — escalate blocking before Phase 3
- Two-pass analysis — wait protocol between passes
- resume_from — design review, Phase 2a re-runs
- Merge only — orchestrator concatenates/dedupes; no new findings
- Secrets — redact in artifacts; file:line in user messages
- Inline execute primary — effort scales per-PR reviewers via
/implementreviewer_configs algorithm - Delegated execute fallback —
--delegate-executeonly; same-session Phase 6 + memory + Final Report - Subagent Worktree Protocol — fetch without refspec;
commit_shaauthoritative; rm worktree before branch mutation - Execute resume —
--resumewithphase: execute_inlinejumps to Phase 5 Step 7
Security & Privacy
- Redact secrets as
[REDACTED]in all/tmpartifacts; citefile:lineonly in user messages - Path allowlist: persisted paths must match
/tmp/grok-brownfield-*(delegated exec summaries may use/tmp/grok-exec-summary-*); reject invalid paths before read/merge - Memory file
0o600viamemory.py(bundled dependency) - Remap security-auditor native severities (critical/high/medium/low) → workflow severities (bug/suggestion/nit) in analysis artifacts
/tmpdeliverables have no TTL — default keep deliverables; optional--cleanup-deliverablesand manual scrub documented in Final Report
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

