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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions build/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -1660,14 +1660,18 @@ prior commit.
- **Exit 1 (runtime error)**: Existing error path. See Step M3.
- **Exit 2 (test failure)**: Existing test-fix path. See Step M3.

- **Exit 3 (STALEMATE)**: User picked `[m]anual mode` at the bail-out or stalemate gate, OR the non-TTY `fail-fast` mode fired on a CRITICAL round.
- **Exit 3 (STALEMATE)**: One of three sub-cases:
- User picked `[m]anual mode` at the bail-out or stalemate gate, OR the non-TTY `fail-fast` mode fired on a CRITICAL round (`state.planReview.status === "critical_exit_pending"`).
- **synth_failure_stalemate**: a prior session's synthesizer returned `ok:false` and the orchestrator's stalemate guard refused to auto-retry on resume (`state.planReview.status === "synth_failure"` or `"synth_failure_stalemate"`). A stdout STALEMATE event like `{"event":"STALEMATE","reason":"synth_failure_promoted_on_resume",...}` accompanies this exit.

1. Read `~/.gstack/build-state/<stateSlug>/plan-review-report.json` (where `stateSlug` is `runs[0].stateSlug` from the manifest). Extract the `objections` array (CRITICAL severity only) and the `round` field. Also read `~/.gstack/build-state/<stateSlug>/plan-review-history.jsonl` for the full trajectory.
1. Read `~/.gstack/build-state/<stateSlug>/plan-review-report.json` (where `stateSlug` is `runs[0].stateSlug` from the manifest). Extract the `objections` array (CRITICAL severity only) and the `round` field. Also read `~/.gstack/build-state/<stateSlug>/plan-review-history.jsonl` for the full trajectory. For synth_failure_stalemate, also check the convergence aggregate at `~/.gstack/analytics/convergence.jsonl` for the `outcome` field — a value of `synth_failure` or `synth_failure_stalemate` confirms the sub-case.

2. AskUser with options:
- A) **Override** — proceed with the current plan as-is (re-launch `gstack-build` with `--no-plan-review`)
- B) **Apply suggested fixes** — read the CRITICAL objections, edit the plan file manually, then re-launch (the loop will restart from round 1 because the plan changed substantively; alternatively keep history.jsonl and the next reviewer call will see the prior rounds)
- C) **Edit manually** — open the living plan file in `$EDITOR`, resolve the objections by hand, then re-launch
- A) **Override** — proceed with the current plan as-is (re-launch `gstack-build` with `--no-plan-review`). For synth_failure_stalemate this is often the right call when the failure was caused by a known-transient provider issue (model outage, rate limit).
- B) **Apply suggested fixes** — read the CRITICAL objections, edit the plan file manually, then re-launch (the loop will restart from round 1 because the plan changed substantively; alternatively keep history.jsonl and the next reviewer call will see the prior rounds). For synth_failure_stalemate, also delete `state.planReview` from the build-state file so the loop starts fresh — leaving the synth_failure status would re-trigger the stalemate guard.
- C) **Edit manually** — open the living plan file in `$EDITOR`, resolve the objections by hand, then re-launch.

**Restart storm note** (post-fix-plan-review-loop-stalemate-restart-storm): the monitor caps auto-resumes at 3 per run (configurable via `GSTACK_MONITOR_MAX_AUTO_RESUME`). If you see `RUN_HALTED_STALEMATE` or `RUN_HALTED_RESTART_CAP` events in the monitor log, the monitor stopped trying to auto-resume — manual intervention is required.

- **Exit 4 (USER ABORT)**: User picked `[q]uit` at the triage gate or stalemate gate.

Expand Down
14 changes: 9 additions & 5 deletions build/SKILL.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,18 @@ prior commit.
- **Exit 1 (runtime error)**: Existing error path. See Step M3.
- **Exit 2 (test failure)**: Existing test-fix path. See Step M3.

- **Exit 3 (STALEMATE)**: User picked `[m]anual mode` at the bail-out or stalemate gate, OR the non-TTY `fail-fast` mode fired on a CRITICAL round.
- **Exit 3 (STALEMATE)**: One of three sub-cases:
- User picked `[m]anual mode` at the bail-out or stalemate gate, OR the non-TTY `fail-fast` mode fired on a CRITICAL round (`state.planReview.status === "critical_exit_pending"`).
- **synth_failure_stalemate**: a prior session's synthesizer returned `ok:false` and the orchestrator's stalemate guard refused to auto-retry on resume (`state.planReview.status === "synth_failure"` or `"synth_failure_stalemate"`). A stdout STALEMATE event like `{"event":"STALEMATE","reason":"synth_failure_promoted_on_resume",...}` accompanies this exit.

1. Read `~/.gstack/build-state/<stateSlug>/plan-review-report.json` (where `stateSlug` is `runs[0].stateSlug` from the manifest). Extract the `objections` array (CRITICAL severity only) and the `round` field. Also read `~/.gstack/build-state/<stateSlug>/plan-review-history.jsonl` for the full trajectory.
1. Read `~/.gstack/build-state/<stateSlug>/plan-review-report.json` (where `stateSlug` is `runs[0].stateSlug` from the manifest). Extract the `objections` array (CRITICAL severity only) and the `round` field. Also read `~/.gstack/build-state/<stateSlug>/plan-review-history.jsonl` for the full trajectory. For synth_failure_stalemate, also check the convergence aggregate at `~/.gstack/analytics/convergence.jsonl` for the `outcome` field — a value of `synth_failure` or `synth_failure_stalemate` confirms the sub-case.

2. AskUser with options:
- A) **Override** — proceed with the current plan as-is (re-launch `gstack-build` with `--no-plan-review`)
- B) **Apply suggested fixes** — read the CRITICAL objections, edit the plan file manually, then re-launch (the loop will restart from round 1 because the plan changed substantively; alternatively keep history.jsonl and the next reviewer call will see the prior rounds)
- C) **Edit manually** — open the living plan file in `$EDITOR`, resolve the objections by hand, then re-launch
- A) **Override** — proceed with the current plan as-is (re-launch `gstack-build` with `--no-plan-review`). For synth_failure_stalemate this is often the right call when the failure was caused by a known-transient provider issue (model outage, rate limit).
- B) **Apply suggested fixes** — read the CRITICAL objections, edit the plan file manually, then re-launch (the loop will restart from round 1 because the plan changed substantively; alternatively keep history.jsonl and the next reviewer call will see the prior rounds). For synth_failure_stalemate, also delete `state.planReview` from the build-state file so the loop starts fresh — leaving the synth_failure status would re-trigger the stalemate guard.
- C) **Edit manually** — open the living plan file in `$EDITOR`, resolve the objections by hand, then re-launch.

**Restart storm note** (post-fix-plan-review-loop-stalemate-restart-storm): the monitor caps auto-resumes at 3 per run (configurable via `GSTACK_MONITOR_MAX_AUTO_RESUME`). If you see `RUN_HALTED_STALEMATE` or `RUN_HALTED_RESTART_CAP` events in the monitor log, the monitor stopped trying to auto-resume — manual intervention is required.

- **Exit 4 (USER ABORT)**: User picked `[q]uit` at the triage gate or stalemate gate.

Expand Down
78 changes: 78 additions & 0 deletions build/orchestrator/__tests__/cli-finally-exit-code.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Pins the structural fix for Bug A: cli.ts's outer finally honors any
* ExitError thrown inside the try via a captured pendingExitError. Without
* this, `process.exit(exitCode)` in the finally would mask thrown codes
* because `exitCode` defaults to 1 and gets set to 0 on the success path
* BEFORE the failure branches throw — so a throw new ExitError(3) after a
* successful prior phase would exit 0 (false success).
*
* The behavioral consequence of the bug is the synth_failure restart storm
* documented in ~/.claude/plans/fix-plan-review-loop-stalemate-restart-storm.md:
* the monitor sees exit 0 + a "synth_failure" state status, auto-resumes the
* crashed run, which hits the same failure again, ad infinitum.
*
* This test is intentionally a static-grep wiring check rather than a
* subprocess invocation. The pattern is 3 lines of plain JS semantics; what
* matters is that all three pieces stay wired together. Behavioral coverage
* comes from plan-reviewer-loop.test.ts (which exercises the new exit-3
* path end-to-end through the actual loop function).
*/

import { describe, it, expect } from "bun:test";
import * as fs from "node:fs";
import * as path from "node:path";

describe("Bug A: ExitError code propagation through outer finally", () => {
const cliPath = path.resolve(import.meta.dir, "../cli.ts");
const content = fs.readFileSync(cliPath, "utf-8");

it("declares pendingExitError near exitCode", () => {
expect(content).toContain(
"let pendingExitError: ExitError | null = null",
);
});

it("captures ExitError in catch and re-throws", () => {
expect(content).toMatch(
/if \(err instanceof ExitError\) pendingExitError = err/,
);
});

it("finally exits with the pending ExitError code if any", () => {
expect(content).toMatch(
/process\.exit\(pendingExitError\?\.code \?\? exitCode\)/,
);
});

it("imports ExitError (the captured type)", () => {
expect(content).toMatch(/import \{[^}]*ExitError[^}]*\} from "\.\/errors"/);
});

it("pendingExitError pattern verified semantically (sanity)", () => {
// Sanity check: prove the JS semantics of the pattern work. If this ever
// fails, JavaScript itself broke. Kept for completeness so a future
// contributor can read this file and see the pattern in action.
class TestExitError extends Error {
constructor(public readonly code: number) {
super(`ExitError(${code})`);
}
}
let exitCode = 1;
let pendingExitError: TestExitError | null = null;
let effectiveCode: number | undefined;
try {
try {
exitCode = 0; // success path runs first
throw new TestExitError(3); // then failure throws
} catch (err) {
if (err instanceof TestExitError) pendingExitError = err;
throw err;
} finally {
effectiveCode = pendingExitError?.code ?? exitCode;
}
} catch {
// expected — the inner throw re-throws past finally
}
expect(effectiveCode).toBe(3);
});
});
88 changes: 88 additions & 0 deletions build/orchestrator/__tests__/cli-synth-failure-resume.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Pins Fix B: cli.ts refuses to auto-retry plan-review-loop when prior
* session left state.planReview.status === "synth_failure" or
* "synth_failure_stalemate". The first occurrence triggers a STALEMATE
* stdout event, promotes the status, and throws ExitError(3). This breaks
* the restart storm at its source.
*
* See ~/.claude/plans/fix-plan-review-loop-stalemate-restart-storm.md
* Bug B for the full failure chain.
*/

import { describe, it, expect } from "bun:test";
import * as fs from "node:fs";
import * as path from "node:path";

describe("Fix B: synth_failure resume guard", () => {
const cliPath = path.resolve(import.meta.dir, "../cli.ts");
const content = fs.readFileSync(cliPath, "utf-8");

it("declares the stalemate guard BEFORE the plan-review-loop-launch if", () => {
// Locate the guard and the loop launcher.
const guardIdx = content.indexOf("Stalemate guard:");
const loopIfIdx = content.indexOf(
"// Plan review: second-opinion pass before Phase 1 of Feature 1.",
);
expect(guardIdx).toBeGreaterThan(0);
expect(loopIfIdx).toBeGreaterThan(guardIdx);
});

it("guard refuses both synth_failure AND synth_failure_stalemate statuses", () => {
// Extract the guard block (between "Stalemate guard:" and the next blank-comment boundary).
const guardStart = content.indexOf("Stalemate guard:");
const guardEnd = content.indexOf(
"// Plan review: second-opinion pass",
guardStart,
);
const guard = content.slice(guardStart, guardEnd);
expect(guard).toContain('status === "synth_failure"');
expect(guard).toContain('status === "synth_failure_stalemate"');
expect(guard).toContain('throw new ExitError(3)');
expect(guard).toContain('event: "STALEMATE"');
});

it("removes synth_failure from the resume-retry list", () => {
// The original retry condition included synth_failure. Find the new
// condition and verify only critical_exit_pending and user_aborted
// remain as auto-retry triggers (synth_failure is handled by the guard).
const loopIfStart = content.indexOf(
"// Plan review: second-opinion pass",
);
const loopIfEnd = content.indexOf(") {", loopIfStart) + 3;
const loopIfBlock = content.slice(loopIfStart, loopIfEnd);
expect(loopIfBlock).toContain('"critical_exit_pending"');
expect(loopIfBlock).toContain('"user_aborted"');
// synth_failure MUST NOT appear in the retry condition anymore.
expect(loopIfBlock).not.toContain('"synth_failure"');
});

it("the new exit-3 handler records synth_failure_stalemate status when loop returns synth_failure_stalemate outcome", () => {
// Verify the routing logic from loopResult.outcome → state.planReview.status.
const exitThreeStart = content.indexOf(
"if (loopResult.exitCode === 3)",
);
expect(exitThreeStart).toBeGreaterThan(0);
const exitThreeEnd = content.indexOf("if (loopResult.exitCode === 4)");
const exitThreeBlock = content.slice(exitThreeStart, exitThreeEnd);
expect(exitThreeBlock).toContain(
'loopResult.outcome === "synth_failure_stalemate"',
);
// The status is computed into a variable; verify both the variable
// declaration and the assignment-to-state pattern.
expect(exitThreeBlock).toMatch(/"synth_failure_stalemate"/);
expect(exitThreeBlock).toMatch(/status:\s*stalemateStatus/);
});

it("emits STALEMATE event with reason field for telemetry", () => {
const guardStart = content.indexOf("Stalemate guard:");
const guardEnd = content.indexOf(
"// Plan review: second-opinion pass",
guardStart,
);
const guard = content.slice(guardStart, guardEnd);
// Two reason variants depending on whether status was already stalemate
// or got promoted on this resume.
expect(guard).toContain('"synth_failure_promoted_on_resume"');
expect(guard).toContain('"synth_failure_stalemate_carried_over"');
});
});
Loading