Weave Brainstorm & Refine
Generate independent originals from adversarial participants, then refine them through judge-weave-incorporate cycles. Host-native sub-agents are the default; separate model CLIs are available by explicit choice.
When to Use
- You want the best of both worlds: creative divergence then convergent refinement
- You want to explore a problem space broadly, then hone in on the best solution
- You need a high-quality result and are willing to spend tokens on multiple passes
Key Features
--workers=subagents|model-clis: Use host-native sub-agents by default or explicitly select separate model CLIs- Phase 1 (Brainstorm): Independent originals from each participant, optional
--variants=N - Transition gate: You choose which originals enter refinement
- Phase 2 (Refine): Iterative judge-weave-distribute cycle over
--passes=N - Full rationale chain from brainstorm through every refinement pass
--judge=host|round-robin: Host judges every pass, or rotate judging across participants
The Pipeline
- Brainstorm: Each participant generates independent original responses
- Review: All originals presented — you pick which ones to refine
- Refine: Judge picks the best, weaves in strengths from runners-up, redistributes to all participants
- Repeat: Each pass improves the woven result until convergence or passes exhausted
The full pipeline: generate originals from independent adversarial workers, then iteratively refine them through a judge-weave-distribute cycle. Host-native sub-agents are the default; separate model CLIs are optional. Phase 1 brainstorms diverse responses with optional multiple variants per worker. Phase 2 takes the best originals through iterative refinement where each pass picks the best, incorporates strengths from runners-up, and distributes the woven result back for another round.
This is a project-read-only command — no files in your repository are written, edited, or deleted. Session artifacts (model outputs, judge assessments, woven results) are persisted to $AI_AIP_ROOT for post-session inspection; this directory is outside your repository.
The prompt comes from $ARGUMENTS. If no arguments are provided, ask the user what they want to brainstorm and refine.
Worker selection
Before any other unresolved configuration choice or operational step, read
../../references/worker-backends.md. Resolve
worker_backend from --workers=subagents|model-clis using that reference;
if the flag is absent, ask its worker question first.
If interactive choice is unavailable, honor its documented headless default.
The selected backend governs the whole session: dispatch, retry, judging, refinement, artifacts, session metadata, and presentation. The shared reference adapts provider-named examples across every later phase to that backend.
When worker_backend == subagents, use only the reference's native sub-agent
path. Skip every model-CLI detection, timeout question, timeout resolution,
retry, fallback, and dispatch instruction below. Every such instruction below
is conditional on worker_backend == model-clis.
Phase 1: Gather Context
Goal: Understand the project and prepare the prompt.
Read CLAUDE.md / AGENTS.md if present — project conventions inform better responses.
Determine trunk branch (for prompts about branch changes):
git remote show origin | grep 'HEAD branch'Fall back to
main, thenmaster, if detection fails.Capture the prompt: Use
$ARGUMENTSas the user's prompt. If$ARGUMENTSis empty, ask the user what they want to brainstorm and refine.
Phase 1b: Build Context Packet
After Phase 1 context gathering (reading CLAUDE.md, exploring files, capturing the task), assemble a structured context bundle that will be included verbatim in ALL model prompts. This ensures every model works from the same information.
Write to $SESSION_DIR/context-packet.md (the actual file write happens after Session Directory Initialization in Phase 2 creates $SESSION_DIR):
Conventions summary — key rules from CLAUDE.md/AGENTS.md (max 50 lines). Focus on commit format, test patterns, code style, and quality gates relevant to the task.
Repo state — branch, HEAD ref, trunk branch, uncommitted changes summary:
git status --shortChanged files — branch changes relative to trunk:
git diff --stat origin/<trunk>...HEADRelevant file list — files matching task keywords discovered during Phase 1 exploration. Include paths only, not content.
Key snippets — critical function signatures, types, test patterns, or API contracts relevant to the task (max 200 lines). Prioritize interfaces over implementations.
Known unknowns — aspects of the task that need discovery during execution. List what the model should investigate.
Size limit: 400 lines total. Prioritize by task relevance. If the packet exceeds 400 lines, truncate the least relevant sections (snippets first, then file list).
Usage in model prompts:
- For the Claude Task agent: reference the file path (
$SESSION_DIR/context-packet.md) — the agent reads it directly - For Antigravity and GPT sub-agents: include the context packet content in the agent prompt, which the sub-agent then passes to the external CLI
For brainstorm-and-refine, include conventions summary, relevant file list, key snippets, and known unknowns. Prioritize content that helps models produce diverse, high-quality originals.
Phase 2: Configuration and Model Detection
Step 1: Parse Flags
Scan $ARGUMENTS for explicit flags anywhere in the text. Flags use --name=value syntax and are stripped from the prompt text before sending to models.
| Flag | Values | Default | Description |
|---|---|---|---|
--variants=N |
1–3 | 1 | Independent prompts per model (brainstorm phase) |
--passes=N |
1–5 | 2 | Number of refinement passes (refine phase) |
--timeout=N|none |
seconds or none |
450s | Timeout for external model commands |
--mode=fast|balanced|deep |
mode preset | balanced |
Execution mode preset |
--judge=host|round-robin |
judge mode | host |
Who judges refinement passes |
--preamble=... |
text | built-in | Override brainstorm variant preamble |
--no-deslop |
flag | off | Skip the final deslop pass on the woven synthesis |
--quiet-deslop |
flag | off | Replace the 8-line deslop summary with one line |
--verbose-deslop |
flag | off | Add tier letter, signature id, confidence per finding |
Mode presets set defaults for variants, passes, and timeout when not explicitly overridden:
| Mode | Variants | Passes | Timeout multiplier |
|---|---|---|---|
fast |
1 | 1 | 0.5x default |
balanced |
1 | 2 | 1x default |
deep |
2 | 3 | 1.5x default |
Backward compatibility: Legacy trigger words are silently recognized as aliases:
multipass(case-insensitive) →--passes=2x<N>(N = 2–5, regex\bx([2-5])\b) →--passes=Ntimeout:<seconds>→--timeout=<seconds>timeout:none→--timeout=none
Legacy triggers are scanned on the first and last line only (to avoid false positives in pasted content). Explicit -- flags take priority over legacy triggers.
Values above 3 for --variants are capped at 3 with a note to the user. Values above 5 for --passes are capped at 5.
--judge flag: --judge=host (default) uses the host agent (Claude) as judge for all refinement passes. --judge=round-robin rotates judging across available models: Pass 1 → Claude, Pass 2 → Antigravity, Pass 3 → GPT, Pass 4 → Claude, etc. The rotation includes only models that are available (detected in Step 3). If only one model is available, round-robin degrades to host mode. External model judges produce scores and pick winners; the host agent always weaves. See the External Judge Protocol in Phase 5 Step 1 for details.
Config flags (used in Step 2):
variant_count= parsed from--variants, mode preset, or nullpass_count= parsed from--passes, mode preset, or legacy trigger. Null if not provided.timeout_value= parsed from--timeout, mode preset, or legacy trigger. Null if not provided.judge_mode= parsed from--judgeor"host"default
Step 2: Interactive Configuration
When flags are provided, skip the corresponding question. When --variants is provided, skip the variants question. When --passes is provided, skip the passes question. When --timeout is provided, skip the timeout question.
If AskUserQuestion is unavailable (headless mode via claude -p), use flag values if set, otherwise: 1 variant, 2 passes, 450s timeout.
Use AskUserQuestion to prompt the user for any unresolved settings:
Question 1 — Variants (skipped when --variants was provided):
- question: "How many brainstorm variants per model? Each variant gets a distinct creative direction."
- header: "Variants"
- When
variant_countexists (from mode preset), move the matching option first with "(Recommended)" suffix. - When
variant_countis null, use default ordering:- "1 — one per model (Recommended)" — Each model produces one original response.
- "2 — two per model" — Each model produces two originals with different creative directions.
- "3 — three per model" — Maximum diversity. Three originals per model.
Question 2 — Passes (skipped when --passes was provided):
- question: "How many refinement passes after brainstorming? Each pass judges, weaves, and redistributes."
- header: "Passes"
- When
pass_countexists (from mode preset or legacy trigger), move the matching option first with "(Recommended)" suffix. - When
pass_countis null, use default ordering:- "2 — two passes (Recommended)" — One full refinement cycle. Good balance of quality and cost.
- "1 — single pass" — Judge and weave once. Minimal refinement.
- "3 — triple pass" — Two refinement rounds. Maximum depth, highest token usage.
Question 3 — Timeout (skipped when --timeout was provided):
- question: "Timeout for external model commands?"
- header: "Timeout"
- options:
- "Default (450s)" — Use this command's built-in default timeout.
- "Quick — 225s" — For fast queries (0.5x default). May timeout on complex tasks.
- "Long — 675s" — For complex tasks (1.5x default). Higher wait on failures.
- "None" — No timeout. Wait indefinitely for each model.
Step 3: Detect Available Models
Goal: Check which AI CLI tools are installed locally.
Run these checks in parallel:
command -v agy >/dev/null 2>&1 && echo "agy:available" || echo "agy:missing"
command -v gemini >/dev/null 2>&1 && echo "gemini:available" || echo "gemini:missing"
command -v codex >/dev/null 2>&1 && echo "codex:available" || echo "codex:missing"
command -v agent >/dev/null 2>&1 && echo "agent:available" || echo "agent:missing"
Model resolution (priority order)
| Slot | Priority 1 (native) | Native model | Fallback chain | Agent model |
|---|---|---|---|---|
| Claude | Always available (this agent) | — | — | — |
| Antigravity | agy binary |
Gemini 3.1 Pro (High) |
gemini -m gemini-3-pro-preview → agent --model gemini-3.1-pro |
gemini-3.1-pro |
| GPT | codex binary |
(default) | agent --model gpt-5.4-high |
gpt-5.4-high |
Resolution logic for each external slot:
- Native CLI found → use it
- Else next CLI in the fallback chain → use it (
agentslots use the--modelflag) - Else → slot unavailable, note in report
The Antigravity slot is Google's lane: agy (Antigravity) supersedes the standalone gemini CLI, which Google retires on 2026-06-18. agy has no native read-only mode, so read-only commands isolate it in a disposable git worktree (Repo Guard Layer 1; see docs/repo-guard-protocol.md). Because brainstorm runs multiple variants per model that may execute in parallel, each variant isolates agy in its own uniquely-named worktree.
Report which models will participate and which backend each uses.
Step 3b: Detect Search Tools
Run these checks in parallel alongside model detection:
command -v rg >/dev/null 2>&1 && echo "rg:available" || echo "rg:missing"
command -v ag >/dev/null 2>&1 && echo "ag:available" || echo "ag:missing"
command -v fd >/dev/null 2>&1 && echo "fd:available" || echo "fd:missing"
Resolution priority: rg > ag > grep for content search; fd > find for file discovery. Used during context-packet building for faster file/content discovery.
Step 4: Detect Timeout Command
command -v timeout >/dev/null 2>&1 && echo "timeout:available" || { command -v gtimeout >/dev/null 2>&1 && echo "gtimeout:available" || echo "timeout:none"; }
On Linux, timeout is available by default. On macOS, gtimeout is available via GNU coreutils. If neither is found, run external commands without a timeout prefix — time limits will not be enforced. Do not install packages automatically.
Store the resolved timeout command (timeout, gtimeout, or empty) for use in all subsequent CLI invocations. When constructing bash commands, replace <timeout_cmd> with the resolved command and <timeout_seconds> with the resolved value (from trigger parsing, interactive config, or the command's default). If no timeout command is available, omit the prefix entirely. When --timeout=none is configured (via flag or interactive selection), also omit <timeout_cmd> and <timeout_seconds> entirely — run external commands without any timeout prefix.
Session Directory Initialization
Step 1: Resolve storage root
if [ -n "$AI_AIP_ROOT" ]; then
AIP_ROOT="$AI_AIP_ROOT"
elif [ -n "$XDG_STATE_HOME" ]; then
AIP_ROOT="$XDG_STATE_HOME/ai-aip"
elif [ "$(uname -s)" = "Darwin" ]; then
AIP_ROOT="$HOME/Library/Application Support/ai-aip"
else
AIP_ROOT="$HOME/.local/state/ai-aip"
fi
Create a /tmp/ai-aip symlink to the resolved root for backward compatibility (if /tmp/ai-aip doesn't already exist or isn't already correct):
ln -sfn "$AIP_ROOT" /tmp/ai-aip 2>/dev/null || true
Step 2: Compute repo identity
REPO_TOPLEVEL="$(git rev-parse --show-toplevel)"
REPO_SLUG="$(basename "$REPO_TOPLEVEL" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9._-]/-/g')"
REPO_ORIGIN="$(git remote get-url origin 2>/dev/null || true)"
if [ -n "$REPO_ORIGIN" ]; then
REPO_KEY="${REPO_ORIGIN}|${REPO_SLUG}"
else
REPO_KEY="$REPO_TOPLEVEL"
fi
if command -v sha256sum >/dev/null 2>&1; then
REPO_ID="$(printf '%s' "$REPO_KEY" | sha256sum | cut -c1-12)"
else
REPO_ID="$(printf '%s' "$REPO_KEY" | shasum -a 256 | cut -c1-12)"
fi
REPO_DIR="${REPO_SLUG}--${REPO_ID}"
Step 3: Generate session ID
SESSION_ID="$(date -u '+%Y%m%d-%H%M%SZ')-$$-$(head -c2 /dev/urandom | od -An -tx1 | tr -d ' ')"
Step 4: Create session directory
SESSION_DIR="$AIP_ROOT/repos/$REPO_DIR/sessions/brainstorm-and-refine/$SESSION_ID"
Create the session directory tree:
mkdir -p -m 700 "$SESSION_DIR/brainstorm/outputs" "$SESSION_DIR/brainstorm/stderr" "$SESSION_DIR/refine/pass-0001/outputs" "$SESSION_DIR/refine/pass-0001/stderr"
Step 5: Write repo.json (if missing)
If $AIP_ROOT/repos/$REPO_DIR/repo.json does not exist, write it with these contents:
{
"schema_version": 1,
"slug": "<REPO_SLUG>",
"id": "<REPO_ID>",
"toplevel": "<REPO_TOPLEVEL>",
"origin": "<REPO_ORIGIN or null>"
}
Step 6: Write session.json (atomic replace)
Write to $SESSION_DIR/session.json.tmp, then mv session.json.tmp session.json:
{
"schema_version": 1,
"session_id": "<SESSION_ID>",
"command": "brainstorm-and-refine",
"status": "in_progress",
"phase": "brainstorm",
"branch": "<current branch>",
"ref": "<short SHA>",
"worker_backend": "<subagents or model-clis>",
"participants": ["<participant artifact ID>", "..."],
"executors": {"<participant artifact ID>": "<executor>"},
"judge_mode": "<host or round-robin>",
"variants_per_model": <N>,
"pass_count": <M>,
"completed_passes": 0,
"prompt_summary": "<first 120 chars of user prompt>",
"created_at": "<ISO 8601 UTC>",
"updated_at": "<ISO 8601 UTC>"
}
When worker_backend == model-clis, add a "models" array containing the
resolved model for each participant. Omit "models" when
worker_backend == subagents.
Step 7: Append events.jsonl
Append one event line to $SESSION_DIR/events.jsonl:
{"event":"session_start","timestamp":"<ISO 8601 UTC>","command":"brainstorm-and-refine","worker_backend":"<subagents or model-clis>","participants":["<participant artifact ID>","..."],"variants_per_model":<N>,"pass_count":<M>}
Step 8: Write metadata.md
Write to $SESSION_DIR/metadata.md containing:
- Command name, start time, configured variant count and pass count
- Worker backend, participant artifact IDs, and executor mapping
- Resolved models only for
model-clis, timeout setting when applicable, judge mode - Git branch (
git branch --show-current), commit ref (git rev-parse --short HEAD)
Store $SESSION_DIR for use in all subsequent phases.
Step 8b: Repo Guard — Capture Fingerprint
Capture the repository state before any model runs. See
docs/repo-guard-protocol.md Layer 2 for the full protocol.
REPO_TOPLEVEL="$(git rev-parse --show-toplevel)"
REPO_HEAD="$(git -C "$REPO_TOPLEVEL" rev-parse HEAD)"
REPO_FINGERPRINT="$(git -C "$REPO_TOPLEVEL" status --porcelain)"
Write $SESSION_DIR/repo-fingerprint.txt containing the HEAD ref and
status output. Store $REPO_TOPLEVEL for use in all subsequent phases.
Step 9: Write Context Packet
Write the Context Packet built in Phase 1b to $SESSION_DIR/context-packet.md.
Phase 3: Brainstorm — Generate All Originals in Parallel
Goal: Send the prompt to all available models simultaneously, with variant differentiation.
Variant Preambles
Each variant receives a distinct creative-direction preamble to prevent anchoring. There are no role preambles (no Maintainer/Skeptic/Builder) during brainstorming — brainstorming is about original thinking, not evaluation lenses.
| Variant | Default Preamble |
|---|---|
| 1 | "Take the most conventional, well-established approach." |
| 2 | "Take an unconventional or creative approach. Challenge the obvious solution." |
| 3 | "Take a contrarian approach. Question the premise itself." |
When --preamble is provided, it replaces the built-in preamble text for ALL variants. Variant numbering still differentiates — each variant receives the user preamble prefixed with "Variant N of M."
Prompt Preparation
For each variant, write a separate prompt file containing the fully rendered prompt for that variant. This ensures shell-safe CLI invocation via $(cat ...) and persists each variant's exact prompt as a session artifact.
For each variant N (1 through variant_count), write $SESSION_DIR/brainstorm/prompts/variant-<N>.md containing:
- Reasoning directive (first line):
Think through the problem step-by-step and consider multiple angles before producing your final response. - The variant preamble for variant N
- The base user prompt (with flags stripped)
- The context packet content
Create the prompts directory:
mkdir -p "$SESSION_DIR/brainstorm/prompts"
Also write $SESSION_DIR/brainstorm/prompt.md as a summary file listing: the base prompt, all variant preambles, and the context packet reference.
Claude Variants (Task agents)
For each Claude variant (1 through variant_count), launch a separate Task agent with subagent_type: "general-purpose":
Prompt for each Claude variant agent:
[Variant preamble for this variant number]
Respond to the following prompt about this codebase. Read any relevant files to give a thorough, original response. Read CLAUDE.md/AGENTS.md for project conventions.
Prompt: <user's prompt>
Read the context packet at
$SESSION_DIR/context-packet.mdfor project context.Provide a clear, well-structured response. Cite specific files and line numbers where relevant. CRITICAL: Do NOT write, edit, create, or delete any files in the repository. Do NOT use Write, Edit, or Bash commands that modify repository files. All session artifacts are written to
$SESSION_DIR, which is outside the repository. This is a READ-ONLY research task.
Each Claude variant agent writes its output to $SESSION_DIR/brainstorm/outputs/claude-v<N>.md.
Antigravity Variants (sub-agents)
For each Antigravity variant (1 through variant_count), launch a separate Task agent (subagent_type: "general-purpose", mode: "default") to execute the Antigravity (agy) model. Include in the agent prompt: the resolved backend command and timeout from Phase 2, the $SESSION_DIR path, the $REPO_TOPLEVEL path and $REPO_FINGERPRINT value for repo guard verification, the variant number, and the prompt with variant preamble and additional instructions:
[Variant preamble for this variant number]
<user's prompt>
Additional instructions: Read relevant files and AGENTS.md/CLAUDE.md for project conventions. Provide a clear, original response citing specific files where relevant. CRITICAL: Do NOT write, edit, create, or delete any files. Do NOT use any file-writing or file-modification tools. This is a READ-ONLY research task. All output must go to stdout. Any file modifications will be automatically detected and reverted.
The agent must:
Read the variant prompt from
$SESSION_DIR/brainstorm/prompts/variant-<N>.mdRun the resolved Antigravity command with output redirection. Repo Guard:
agyhas no native read-only mode (its print mode reads and writes), so isolate it in a disposable git worktree checked out atHEAD— agy reads the snapshot while any stray write lands in the throwaway worktree, never the main repo (seedocs/repo-guard-protocol.mdLayer 1). Because multiple variants can run concurrently, each variant's worktree path carries its variant number (-v<N>) so parallel runs never share a worktree. Thegeminiandagentfallbacks keep their own native read-only modes.Primary (
agyCLI, disposable worktree):(AGY_RO_WT="${REPO_TOPLEVEL}-weave-agy-ro-v<N>"; git -C "$REPO_TOPLEVEL" worktree remove --force "$AGY_RO_WT" 2>/dev/null; git -C "$REPO_TOPLEVEL" worktree add -q --detach "$AGY_RO_WT" HEAD && (cd "$AGY_RO_WT" && <timeout_cmd> <timeout_seconds> agy --model "Gemini 3.1 Pro (High)" --add-dir "$AGY_RO_WT" --dangerously-skip-permissions -p "$(cat "$SESSION_DIR/brainstorm/prompts/variant-<N>.md")" </dev/null >"$SESSION_DIR/brainstorm/outputs/agy-v<N>.md" 2>"$SESSION_DIR/brainstorm/stderr/agy-v<N>.txt"); rc=$?; git -C "$REPO_TOPLEVEL" worktree remove --force "$AGY_RO_WT" 2>/dev/null; exit "$rc")Fallback (
geminiCLI):(cd "$SESSION_DIR" && <timeout_cmd> <timeout_seconds> gemini -m gemini-3-pro-preview --approval-mode plan --include-directories "$REPO_TOPLEVEL" --skip-trust -p "$(cat "$SESSION_DIR/brainstorm/prompts/variant-<N>.md")" >"$SESSION_DIR/brainstorm/outputs/agy-v<N>.md" 2>"$SESSION_DIR/brainstorm/stderr/agy-v<N>.txt")Fallback (
agentCLI):(cd "$SESSION_DIR" && <timeout_cmd> <timeout_seconds> agent -p --mode plan --trust --workspace "$REPO_TOPLEVEL" --model gemini-3.1-pro "$(cat "$SESSION_DIR/brainstorm/prompts/variant-<N>.md")" >"$SESSION_DIR/brainstorm/outputs/agy-v<N>.md" 2>>"$SESSION_DIR/brainstorm/stderr/agy-v<N>.txt")Repo Guard: After the CLI command completes, verify the repository was not modified:
CURRENT_STATUS="$(git -C "$REPO_TOPLEVEL" status --porcelain)"If
$CURRENT_STATUSdiffers from$REPO_FINGERPRINT, revert the repository:git -C "$REPO_TOPLEVEL" checkout -- .git -C "$REPO_TOPLEVEL" clean -fdLog the violation:
printf '{"event":"repo_guard_violation","timestamp":"%s","model":"agy","reverted":true}\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" >>"$SESSION_DIR/guard-events.jsonl"On failure: classify (timeout → retry with 1.5x timeout; rate-limit → retry after 10s; credit-exhausted → skip retry, escalate to the next backend immediately; crash → not retryable; empty → retry once), retry max once with same backend, then fall back down the chain (agy → gemini → agent) if a native CLI was used; if all are credit-exhausted or unavailable, use the lesser model (
Gemini 3.5 Flash (High)via agy for Antigravity; gpt-5.4-mini via agent for GPT)Return: exit code, elapsed time, retry count, output file path
GPT Variants (sub-agents)
For each GPT variant (1 through variant_count), launch a separate Task agent (subagent_type: "general-purpose", mode: "default") to execute the GPT model. Include in the agent prompt: the resolved backend command and timeout from Phase 2, the $SESSION_DIR path, the $REPO_TOPLEVEL path and $REPO_FINGERPRINT value for repo guard verification, the variant number, and the prompt with variant preamble and additional instructions:
[Variant preamble for this variant number]
<user's prompt>
Additional instructions: Read relevant files and AGENTS.md/CLAUDE.md for project conventions. Provide a clear, original response citing specific files where relevant. CRITICAL: Do NOT write, edit, create, or delete any files. Do NOT use any file-writing or file-modification tools. This is a READ-ONLY research task. All output must go to stdout. Any file modifications will be automatically detected and reverted.
The agent must:
Read the variant prompt from
$SESSION_DIR/brainstorm/prompts/variant-<N>.mdRun the resolved GPT command with output redirection. Repo Guard: invoke the CLI in its native read-only sandbox — it reads the repo but cannot write it (see
docs/repo-guard-protocol.mdLayer 1):Native (
codexCLI):(cd "$SESSION_DIR" && <timeout_cmd> <timeout_seconds> codex exec -s read-only -C "$REPO_TOPLEVEL" --skip-git-repo-check </dev/null \ -c model_reasoning_effort=medium \ "$(cat "$SESSION_DIR/brainstorm/prompts/variant-<N>.md")" >"$SESSION_DIR/brainstorm/outputs/gpt-v<N>.md" 2>"$SESSION_DIR/brainstorm/stderr/gpt-v<N>.txt")Fallback (
agentCLI):(cd "$SESSION_DIR" && <timeout_cmd> <timeout_seconds> agent -p --mode plan --trust --workspace "$REPO_TOPLEVEL" --model gpt-5.4-high "$(cat "$SESSION_DIR/brainstorm/prompts/variant-<N>.md")" >"$SESSION_DIR/brainstorm/outputs/gpt-v<N>.md" 2>>"$SESSION_DIR/brainstorm/stderr/gpt-v<N>.txt")Repo Guard: After the CLI command completes, verify the repository was not modified:
CURRENT_STATUS="$(git -C "$REPO_TOPLEVEL" status --porcelain)"If
$CURRENT_STATUSdiffers from$REPO_FINGERPRINT, revert the repository:git -C "$REPO_TOPLEVEL" checkout -- .git -C "$REPO_TOPLEVEL" clean -fdLog the violation:
printf '{"event":"repo_guard_violation","timestamp":"%s","model":"gpt","reverted":true}\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" >>"$SESSION_DIR/guard-events.jsonl"On failure: classify (timeout → retry with 1.5x timeout; rate-limit → retry after 10s; credit-exhausted → skip retry, escalate to agent CLI immediately; crash → not retryable; empty → retry once), retry max once with same backend, then fall back to agent CLI if native was used; if agent is also credit-exhausted or unavailable, use lesser model (
Gemini 3.5 Flash (High)via agy for Antigravity; gpt-5.4-mini via agent for GPT)Return: exit code, elapsed time, retry count, output file path
Artifact Capture
After each model variant completes, persist its output to the session directory:
- Claude variants: Written by each Claude Task agent to
$SESSION_DIR/brainstorm/outputs/claude-v<N>.md - Antigravity variants: Written by each Antigravity sub-agent to
$SESSION_DIR/brainstorm/outputs/agy-v<N>.md - GPT variants: Written by each GPT sub-agent to
$SESSION_DIR/brainstorm/outputs/gpt-v<N>.md
Execution Strategy
- Launch ALL model x variant agents in the same turn to execute simultaneously. If parallel dispatch is unavailable, launch sequentially — the presentation phase handles partial results.
- Each variant MUST be a separate, independent prompt invocation. Never send multiple variants to the same model in a single prompt — this prevents anchoring.
- Each sub-agent handles its own retry and fallback protocol internally (see steps 3-4 in each agent's instructions above).
- After all agents return, verify output files exist in
$SESSION_DIR/brainstorm/outputs/. - If a sub-agent reports failure after exhausting retries, mark that model variant as unavailable and include failure details in the report.
- Never block the entire workflow on a single model variant failure.
Phase 4: Present Brainstorm Results and Transition Gate
Goal: Display all brainstorm originals, then let the user select which ones enter refinement.
Step 1: Present All Originals
Read each output file from $SESSION_DIR/brainstorm/outputs/ and present them.
When variant_count is 1, omit the variant label from output headers. Use just the model name (e.g., "Claude", not "Claude — Variant 1").
Format for variants=1:
# Brainstorm Results
**Prompt**: <user's prompt>
**Models**: Claude, Antigravity, GPT
---
## Claude
<response>
## Antigravity
<response>
## GPT
<response>
Format for variants > 1:
# Brainstorm Results
**Prompt**: <user's prompt>
**Models**: Claude, Antigravity, GPT | **Variants per model**: N
---
## Claude — Variant 1 (Conventional)
<response>
## Claude — Variant 2 (Creative)
<response>
## Antigravity — Variant 1 (Conventional)
<response>
## Antigravity — Variant 2 (Creative)
<response>
## GPT — Variant 1 (Conventional)
<response>
## GPT — Variant 2 (Creative)
<response>
The variant label in parentheses matches the preamble direction:
- Variant 1: (Conventional)
- Variant 2: (Creative)
- Variant 3: (Contrarian)
When --preamble was used, omit the parenthetical label since the user provided a custom preamble. Use just "Variant 1", "Variant 2", "Variant 3".
If a model variant failed or was unavailable, include a note in place of its response.
Step 2: Transition Gate
After presenting the brainstorm results, always ask the user which originals should enter refinement using AskUserQuestion:
- question: "Which brainstorm originals should enter the refinement phase?"
- header: "Refine"
- options:
- "All of them (Recommended)" — All successful originals enter refinement as candidates.
- "Let me pick specific ones" — Presents a follow-up multi-select question listing each original by label (e.g., "Claude-v1", "Antigravity-v2"). The user selects which ones to include.
- "None — satisfied with brainstorm results" — Exit early. Skip refinement entirely.
If the user selects "Let me pick specific ones", present a follow-up AskUserQuestion with multiSelect: true:
- question: "Select which originals to refine:"
- header: "Originals"
- options: One option per successful model variant (e.g., "Claude — Variant 1", "Antigravity — Variant 2")
If the user selects "None", finalize the session immediately:
- Update
session.jsonvia atomic replace: setstatusto"completed",phaseto"brainstorm_only",updated_atto now. - Append events to
events.jsonl:
{"event":"brainstorm_to_refine","timestamp":"<ISO 8601 UTC>","selected_originals":[],"total_available":<N>}
{"event":"session_complete","timestamp":"<ISO 8601 UTC>","command":"brainstorm-and-refine","brainstorm_originals":<N>,"selected_for_refine":0,"completed_passes":0,"converged":false}
- Update
latestsymlink:
ln -sfn "$SESSION_ID" "$AIP_ROOT/repos/$REPO_DIR/sessions/brainstorm-and-refine/latest"
Then stop — do not proceed to Phase 5 or beyond.
If the user selects originals (either "All" or specific ones), append a transition event to events.jsonl:
{"event":"brainstorm_to_refine","timestamp":"<ISO 8601 UTC>","selected_originals":["claude-v1","agy-v2","..."],"total_available":<N>}
Update session.json via atomic replace: set phase to "refine", updated_at to now.
Phase 5: Refine — Initial Judge and Weave
Goal: Judge the selected brainstorm originals as the initial pool, weave the best, and begin the refinement cycle.
Step 1: Judge the Brainstorm Originals
Determine this pass's judge. If judge_mode is "host", Claude judges. If "round-robin", build a rotation array from available models starting with Claude: [claude, agy, gpt] (skipping any model not detected in Phase 2 Step 3). The judge for pass N is rotation[(N - 1) % len(rotation)]. Since Claude is always index 0, Pass 1 is always judged by Claude — this is intentional because brainstorm originals have varied structure where Claude's flexible parsing is most valuable.
Self-judging note: In round-robin mode, the judge model is also a participant whose output is being judged. The host agent should cross-check the external judge's winner selection against its own reading of the outputs during the weave step. If the external judge selected its own output as winner and the host's assessment disagrees, the host may override the winner selection for weaving purposes. Record any override in judge.md with a note: **Override**: Host overrode external judge's self-selection of <model> — <reason>.
Host Judge Protocol (Claude judges)
When the judge is Claude (host), the host agent reads all selected brainstorm originals and produces an assessment.
Read selected outputs from $SESSION_DIR/brainstorm/outputs/. Only include the originals the user selected in the transition gate.
Score each original on four dimensions, 0-10:
| Dimension | Description |
|---|---|
| Quality | Writing quality, clarity, precision, technical accuracy |
| Originality | Novel approaches, creative solutions, fresh perspectives |
| Completeness | Covers all aspects, nothing important missing |
| Coherence | Internal consistency, logical flow, well-structured |
Compute the total score for each original (sum of four dimensions, max 40).
Pick the winner: The original with the highest total score. In case of tie, prefer the original that is most comprehensive.
Write the judge's assessment to $SESSION_DIR/refine/pass-0001/judge.md:
# Judge's Assessment — Pass 1 (from brainstorm originals)
**Judged by**: Claude (host)
## Scores
| Dimension | <label-1> | <label-2> | ... |
|-----------|-----------|-----------|-----|
| Quality (0-10) | X | X | ... |
| Originality (0-10) | X | X | ... |
| Completeness (0-10) | X | X | ... |
| Coherence (0-10) | X | X | ... |
| **Total** | XX | XX | ... |
## Winner
**<label>** with a score of XX/40.
## Rationale
<Why this original was the best. What specific qualities made it stand out.>
## Runner-Up Analysis
### <label>
- <specific strength this original has that the winner lacks>
External Judge Protocol (Antigravity or GPT judges)
When the judge for a pass is an external model (pass 2+ in round-robin mode), follow the External Judge Protocol defined in /weave:refine Phase 4 Step 1. The protocol is identical:
- Construct judge prompt with scoring rubric, expected output format, and all model outputs inline
- Write prompt to
$SESSION_DIR/refine/pass-NNNN/judge-prompt.md - Dispatch to the judge model via CLI (same mechanism as participation dispatch)
- Parse the response (scores table, winner, rationale, runner-up analysis)
- Validate and fall back to host judging if parsing fails
- Write
judge.mdwith**Judged by**: <model> (round-robin pass N)header, or**Judged by**: Claude (fallback from <model> — <reason>)if fallback was triggered
The host agent always weaves regardless of who judged. When the external judge's Runner-Up Analysis is thin, the host supplements with its own observations.
Step 2: Analyze Runners-Up
For each non-winning original, identify specific strengths it has that the winner lacks. Be concrete:
- Quote specific passages or phrasings that are better
- Identify techniques, approaches, or structural choices worth incorporating
- Note any aspects that this original addresses but the winner does not
Step 3: Weave
Produce a woven version that combines the best of all selected originals:
- Start from the winner as the base
- Incorporate identified strengths from runners-up — integrate specific passages, techniques, or structural improvements
- Address weaknesses found in the winner during judging
- Preserve coherence — the woven result must read as a unified artifact, not a patchwork of different styles
Write the woven version to $SESSION_DIR/refine/pass-0001/woven.md.
Step 4: Distribute for Pass 2
If pass_count is 1, skip distribution and proceed to Phase 7.
Create the next pass directory:
mkdir -p -m 700 "$SESSION_DIR/refine/pass-0002/outputs" "$SESSION_DIR/refine/pass-0002/stderr"
Each model receives the woven version with role preambles (Maintainer/Skeptic/Builder) for the refinement phase:
| Slot | Role | Bias | Preamble |
|---|---|---|---|
| Claude | Maintainer | Conservative, convention-enforcing, minimal-change | "You are the Maintainer. Prioritize correctness, convention adherence, and minimal scope. Challenge any change that isn't strictly necessary. Enforce all project conventions from CLAUDE.md/AGENTS.md." |
| Antigravity | Skeptic | Challenge assumptions, find edge cases, question necessity | "You are the Skeptic. Challenge every assumption. Find edge cases, failure modes, and unstated requirements. Question whether the proposed approach is even the right one. Prioritize what could go wrong." |
| GPT | Builder | Pragmatic, shippable, favor simplicity over abstraction | "You are the Builder. Prioritize practical, shippable solutions. Favor simplicity over abstraction. Focus on what gets the job done with the least complexity. Call out over-engineering." |
Distribution prompt:
You previously contributed an original response to a brainstorm. The judge has evaluated all originals and produced a woven result incorporating the best elements from each contribution.
Woven version (the current best):
<woven version from pass 1>
Judge's rationale:
Strengths incorporated from other originals:
- <strength 1 and its source>
- <strength 2 and its source>
Weaknesses addressed:
- <weakness 1 and how it was fixed>
Critique this woven version further and produce an improved version. Use the following three-section format:
Critique
What is strong? What is weak? What is missing? Be specific.
Improved Version
Produce your complete improved version. This must be a full replacement.
Rationale
Explain why you made each change.
Write this prompt to $SESSION_DIR/refine/pass-0002/prompt.md.
Dispatch all models using the same execution strategy as Phase 3 (Claude via Task agent, external models via CLI with agent fallback). Each model receives its role preamble prepended to the distribution prompt. Write outputs to `$SESSION_DIR/refine/pass
…(truncated)