Repository navigation
fix: the skill contract Stop hook waits for Claude Code's transcript flush (GST-1703) - #4104
Merged
Merged
Conversation
…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>
|
😎 This pull request was merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
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)
hosts/claude/hooks/skill-contract-stop-hook.tsnow 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'slast_assistant_message, for at most 2 s, then judges what is there. The loop inlib/skill-gates.tsis nowreadSettledTranscript, which passes read and parse errors through to the caller;settledTranscriptstays the gate wrapper that caps the wait at half the gate deadline and maps those errors toGATE_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_contractsisoffinSWITCH_DEFAULT, lib/skill-route.ts:43, read), and a stop whose transcript already ends on the final message is read once, becausewithFinalMessagereturns the same transcript object (lib/skill-contract.ts:226-233, read). The gate hooks keep their behavior on thesettledTranscriptwrapper: 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
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 fixedBun.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 whoselast_assistant_messagenever 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.
hosts/claude/hooks/skill-contract-stop-hook.ts:95The 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).lib/skill-contract.ts:230Every 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.lib/skill-gates.ts:81A 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 testcommands; verdict pass.bun test test/skill-contract-stop-hook.test.tsat head: 24 pass, 0 fail.bun teston 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:
readSettledTranscript.settledTranscriptreference still matches the gate wrapper.Coverage debt:
readSettledTranscriptis 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 0bun test test/skill-contract-stop-hook.test.ts: 24 pass, 0 failbun teston the CEO, autoplan, skill-gates and investigate gate files: 117 pass, 0 fail