Skip to content

fix: the skill contract Stop hook waits for Claude Code's transcript flush (GST-1703) - #4104

Merged
capy-ai[bot] merged 2 commits into
mainfrom
capy/gst-1703-contract-hook-settle
Nov 7, 2026
Merged

capy-ai[bot] merged 2 commits into
mainfrom
capy/gst-1703-contract-hook-settle

Conversation

@garrytan

@garrytan garrytan commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner
{
  "schema_version": 1,
  "pr": 4104,
  "head": "078d43a4a514c2fead23f07ff56c2a5d5241f31c",
  "version": "1.97.0.0",
  "base": "main",
  "gate": {
    "tests": {
      "pass": 1623,
      "fail": 0,
      "skip": 0
    }
  },
  "ci_carried": [
    {
      "lane": "tests",
      "run": 42324365321,
      "attempt": 1,
      "tree": "622f78c8aac33f60a24e504951eac9b000626633"
    }
  ],
  "session_kind": "unattended",
  "head_tree": "622f78c8aac33f60a24e504951eac9b000626633",
  "product_patch_id": "22f357f8a4d87c478e66accbf3a8ce890ce59472",
  "gstack_version": "1.97.0.0",
  "review_depth": {
    "depth": "light",
    "class": "code",
    "runtime_lines": 38,
    "rules": "70895942bb8a8ee2",
    "base": "6c3d0ec53648e25a2d5778080550abad87401b9b",
    "registry": "6c3d0ec53648e25a2d5778080550abad87401b9b",
    "gate_scan": {
      "tagged": 0,
      "head": "078d43a4a514c2fead23f07ff56c2a5d5241f31c"
    },
    "reasons": [
      "runtime=38"
    ],
    "risk": [],
    "mode": "panel"
  },
  "pregate": "requires-remote cleared @622f78c8aac3 (8 checks, current for 078d43a4a514)",
  "requires_remote": [
    "free-tests.yml/cso-macos-launcher: https://github.com/garrytan/gstack/actions/runs/42324365321/attempts/1",
    "free-tests.yml/macos-named-regressions: https://github.com/garrytan/gstack/actions/runs/42324365321/attempts/1",
    "windows-smoke.yml/windows-smoke: https://github.com/garrytan/gstack/actions/runs/42324365335/attempts/1",
    "pregate.json/remote_gate: remote-gate tree=622f78c8aac3 tests=36134 skipped=111 runs=42324365321/1 profiles=ci-reuse/free-tests heads=078d43a4a514 verified=2026-11-07T01:47:43.518Z"
  ],
  "issues": {
    "fixes": [],
    "refs": [],
    "flakes": []
  },
  "docs": {
    "status": "current",
    "scope": "AGENTS.md, CLAUDE.md, CONTRIBUTING.md, README.md and docs/ (141 files) against 6c3d0ec..078d43a"
  },
  "review_loop": {
    "passes": 1,
    "blocking_found": 0,
    "deferred": 0,
    "noted": 0,
    "fixed": 0,
    "scope": "frozen",
    "rounds": 1,
    "ledgers_read": 3,
    "dispatches": {
      "specialist": 3,
      "red-team": 1
    }
  }
}

What changed

The two late-flush tests fail on the single-read contract hook (c2e150f) and pass once it waits for the flush (078d43a)

The two late-flush tests fail on the single-read contract hook (c2e150f) and pass once it waits for the flush (078d43a).

Before/after as text: The two late-flush tests fail on the single-read contract hook (c2e150f) and pass once it waits for the flush (078d43a)
--- Before · before.txt · captured output
+++ After · after.txt · captured output
+$ bun test test/skill-contract-stop-hook.test.ts   # 078d43a (readSettledTranscript)
+(pass) block once, with the pinned text > a Stop that fires before the CLI flushes the turn waits for the final message: a run that reran its check is met, not blocked [360.13ms]
+(pass) block once, with the pinned text > a Stop that fires before the CLI flushes the load itself still checks the run once the final message lands [352.55ms]
+ 24 pass
+ 0 fail
+Ran 24 tests across 1 file. [4.14s]
-$ bun test test/skill-contract-stop-hook.test.ts -t "fires before the CLI flushes"   # c2e150f (tests only, single-read hook)
-(fail) block once, with the pinned text > a Stop that fires before the CLI flushes the turn waits for the final message: a run that reran its check is met, not blocked [302.96ms]
-(fail) block once, with the pinned text > a Stop that fires before the CLI flushes the load itself still checks the run once the final message lands [302.39ms]
- 0 pass
- 2 fail
-Ran 2 tests across 1 file. [628.00ms]
 

hosts/claude/hooks/skill-contract-stop-hook.ts now reads the transcript through the bounded re-read the /autoplan and /plan-ceo-review gates use. It re-reads until the transcript holds the Stop payload's last_assistant_message, for at most 2 s, then judges what is there. The loop in lib/skill-gates.ts is now readSettledTranscript, which passes read and parse errors through to the caller; settledTranscript stays the gate wrapper that caps the wait at half the gate deadline and maps those errors to GATE_TRANSCRIPT.

Why

GST-1703: Claude Code fires Stop about 200 ms before it flushes the turn's last lines, and the contract hook read the transcript once. So a finished /investigate run whose final test rerun had not flushed was blocked for a missing repro, and a run whose skill load had not flushed stopped unchecked. The hook now waits for the stop's final message, so the first run is recorded as met and the second is checked and blocked.

Blast radius

The contract hook is opt-in (skill_contracts is off in SWITCH_DEFAULT, lib/skill-route.ts:43, read), and a stop whose transcript already ends on the final message is read once, because withFinalMessage returns the same transcript object (lib/skill-contract.ts:226-233, read). The gate hooks keep their behavior on the settledTranscript wrapper: the CEO, autoplan, skill-gates and investigate gate tests pass unchanged (117 of 117, probe 003, script ran).

Approvals

No approvals recorded for branch capy/gst-1703-contract-hook-settle.

Summary

The skill contract Stop hook judged a transcript Claude Code had not finished writing, so it could block a finished /investigate run or let an unfinished one through. It now re-reads until the stop's final message is on disk, for at most 2 s, through the settle loop the stop gates already share, which moved into an exported readSettledTranscript. Failing-first tests landed first (c2e150f), then the fix (078d43a).

Test Coverage

CODE PATHS
[+] lib/skill-gates.ts
  ├── readSettledTranscript()
  │   ├── [★★★ TESTED] final message already in the transcript → returns right away (stop-hook met/blocked cases)
  │   ├── [★★★ TESTED] late flush adds the final message → stop-hook.test.ts:112, :125; ceo-review-gate.test.ts:223, :236; autoplan-stop-gate.test.ts:179
  │   ├── [GAP] maxWaitMs runs out and the final message never lands → judged on withFinalMessage's synthetic message
  │   └── [★★ TESTED] read and parse errors pass through → stop-hook.test.ts:248 (UnknownShape)
  └── settledTranscript() (wrapper)
      ├── [★★★ TESTED] StateError/UnknownShape → GATE_TRANSCRIPT → ceo-review-gate.test.ts:318, investigate-gate.test.ts:492, autoplan:245
      └── [★★ TESTED] wait capped at half the remaining deadline (cap itself not asserted)
[+] hosts/claude/hooks/skill-contract-stop-hook.ts
  ├── [★★★ TESTED] late flush → met (:112) / late load → blocked (:125)
  └── [★★ TESTED] UnknownShape → fails open and bumps the canary (:248)
COVERAGE: 8/9 paths (89%) | GAPS: 1

Tests: 1748 → 1748 (+0 new in this /ship run; the PR's own commit c2e150f adds 2)
Coverage: 89% value-weighted
Test value: 0 tests written, 0 rejected, 0 existing tests extended, 0 paths weakly covered.

Proposed (not written): a never-flushing transcript where the hook still exits inside DEADLINE_MS and records an outcome.

Regression proof: fails at c2e150f (tests only, single-read hook: 2 of 2 fail, probe 002) and passes after the fix at 078d43a (24 of 24, probe 001).

Tests ran: tests 1623 (files; carried from CI run 42324365321/1 at tree 622f78c8aac3)

Pre-Landing Review

2 issues (0 critical, 2 informational), 0 fixed, 2 skipped unattended (below the blocking floor):

  • test/skill-contract-stop-hook.test.ts:117 (testing specialist, confidence 5): the two late-flush tests race a fixed Bun.sleep(300) against the hook's first read, so a hook slower than 300 ms to start would pass them without exercising the re-read loop. They cannot fail spuriously, since the 2 s settle outlasts the append. Skipped with evidence: probe 002 records both red against the pre-fix hook on this machine.
  • hosts/claude/hooks/skill-contract-stop-hook.ts:95 (core, confidence 4): the hook settles before it knows a contract skill is loaded, so a stop whose last_assistant_message never matches the last text event pays the full 2 s. Untraced; listed with the adversarial notes below.

REVIEW_LOOP: passes=1 blocking_found=0 deferred=0 noted=0 fixed=0 scope=frozen ledgers_read=3 dispatches=4

Gate edits: none detected (2 listed, 0 read; patterns: RH-1,RH-3,RH-4,RH-12,RH-13,RH-14,RH-15,RH-16; unlisted idioms: none)

Outside review: openai/gpt-6.1-sol (host subagent), completed, no actionable findings. Native adversarial (claude-opus-5-5): 3 findings, all Low and non-blocking, listed under Review notes.

Review notes

Below the blocking floor (advisory, informational, test-gap or wording items); recorded here and not fixed in this PR.

  • INFORMATIONAL hosts/claude/hooks/skill-contract-stop-hook.ts:95 The contract hook calls readSettledTranscript with no maxWaitMs, so it always waits up to SETTLE_MS (2 s) and the synchronous Atomics.wait loop keeps the DEADLINE_MS (5 s) timer from firing; on a near-32 MB transcript stdin (500 ms) + settle + parse + sessionKind (1 s) can pass 5 s, and the timer then fails open before a block flushes. Fails open only; the gates cap at min(SETTLE_MS, remaining/2).
  • INFORMATIONAL lib/skill-contract.ts:230 Every Stop in a session with skill_contracts on settles before target(), so a last_assistant_message that never equals the last text event (blocks the payload joins, CLI normalization) waits the full 2 s and re-parses about 40 times. Unverified against a live CLI payload.
  • INFORMATIONAL lib/skill-gates.ts:81 A turn that writes the same final text before and after its last tool calls can settle early on the stale first copy and judge without the rerun; same weakness the gates had before this change.

Exploratory QA

Functional surface, bounded smoke (300 s), 3 probes with repository-native bun test commands; verdict pass.

  • 001: bun test test/skill-contract-stop-hook.test.ts at head: 24 pass, 0 fail.
  • 002 (checkpoint 002): the two late-flush tests against the pre-fix hook (worktree at c2e150f): 2 fail, as they must to prove the fix.
  • 003 (checkpoint 003): bun test on the CEO, autoplan, skill-gates and investigate gate files: 117 pass, 0 fail.

Not exercised: real Claude Code flush timing (fixture append at 300 ms) and a final message that never lands.

Design Review

No frontend files changed, so design review was skipped.

Eval Results

No prompt-related files changed, so evals were skipped.

Review bot comments

No review bot findings.

Scope Drift

Scope Check: CLEAN
Intent: the skill contract Stop hook waits for the transcript flush (GST-1703), through the settle loop #4102 shared.
Delivered: exported readSettledTranscript, the hook calls it, two failing-first tests, docs paragraph and changelog fragment.

Plan Completion

Plan completion audit: not run (no plan is bound to this branch). This is a bug fix with failing-first tests (R65), which skips plan review.

Issues

No GitHub issue links (Linear GST-1703). Follows #4102.

Verification Results

No Step 8.1 obligations (no plan). Exploratory probes: 3 PASS, 0 FAIL, 0 BLOCKED, 0 NOT RUN.

TODOS

No TODO items completed in this PR.

Documentation

Status: current. No documentation edits were needed.

Scope: the candidate diff 6c3d0ec..078d43a on capy/gst-1703-contract-hook-settle: 5 committed files, with nothing staged, unstaged or untracked. I read the changed hook and library source, plus the docs that describe the contract Stop hook and the skill gates.

Documentation health:

  • docs/skill-routing-and-contracts.md: current. It already explains the up-to-2-second re-read through readSettledTranscript.
  • docs/skill-gates.md: current. Its settledTranscript reference still matches the gate wrapper.
  • changelog.d/capy-gst-1703-contract-hook-settle.md: current. It is accurate and belongs to the parent.

Coverage debt: readSettledTranscript is an internal helper, and the hook doc gives it reference and explanation coverage. There are no critical gaps.

Diagram drift: none found.

Test plan

  • bun run test (free suite): carried from CI run 42324365321/1 at tree 622f78c8aac3, 1623 files, exit 0
  • bun test test/skill-contract-stop-hook.test.ts: 24 pass, 0 fail
  • bun test on the CEO, autoplan, skill-gates and investigate gate files: 117 pass, 0 fail
  • pre-push guards: typecheck, skill-size, parity-suite, context-budget, module-size, goldens, conflict-markers passed

Open in Capy

garrytan and others added 2 commits November 7, 2026 01:27
…transcript before Claude Code flushes it (GST-1703)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…ugh the shared settle loop (GST-1703)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@trunk-io

trunk-io Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

😎 This pull request was merged.

@capy-ai
capy-ai Bot merged commit 487506b into main Nov 7, 2026
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant