Repository navigation
test(hooks): wait for async hook markers instead of sleeping - #2224
Conversation
zerob13
left a comment
There was a problem hiding this comment.
Reviewed 2fc6fcecc143d90855583ef714cb0ea500b7d8cc. The gate is a good way to prove that the turn and the synchronous hook finish without waiting for the async hook. One reliability blocker remains:
Wait for a complete marker, not just a new file. The shell redirection creates late before printf writes the contents. wait_file can observe the empty file and the next read can fail even when the hook behaved correctly. Verify that observing the marker cannot leave a creation-to-write race: the reader must only advance once the complete marker is visible, within a bounded deadline. Fixture
Non-blocking pre-existing gap: the async command emits no context, and a single aggregated block does not prove the async contribution is excluded. A distinct valid async contribution would make that assertion meaningful.
The targeted hook test passed locally and in Linux CI. The broader gate is still red on App Server timeouts outside this patch; their cause has not been established.
|
Addressed the marker race in The async hook now writes I also took the non-blocking suggestion: the async command now emits a distinct Local run: The broader verify failure remains the two App Server timeouts outside this patch; their rerun is still in progress. |
zerob13
left a comment
There was a problem hiding this comment.
Reviewed 82085a313dd4f94cec9741637dcd2550e3db0b4b. The async hook now closes the temporary marker and renames it within the same directory, so the reader cannot observe a partially written final marker. The exact shell fixture passed 20 isolated runs. The gate still proves that the turn and synchronous hook progress without waiting on the async hook. This closes my previous change request.
Non-blocking: asserting the absence of the new async sentinel in the aggregated block would strengthen the context check. Exact Linux workspace run and ci-ok are independent of this review.
yyhhyyyyyy
left a comment
There was a problem hiding this comment.
Test-only change: it rewrites an_async_handler_does_not_delay_the_turn_or_the_next_point to gate the async PostToolUse handler on a file and to wait for marker files instead of sleeping. One file, three commits (+11/-14); no production code changed. The no-delay property is still gated and no longer depends on wall-clock timing.
Verdict: APPROVE, with one optional P3.
P3 (optional): the added async context is not asserted ignored
Problem. The async command publishes late (printf 'late' > late.tmp && mv late.tmp late) and only then prints additionalContext (crates/dim-agent-capabilities/tests/hooks_flow.rs:1672). wait_file(&late, ...) at hooks_flow.rs:1703 returns at the marker, and the final hook_blocks(...).len() == 1 at hooks_flow.rs:1705 runs immediately, without waiting for the handler to exit, for its stdout to be read, or for the point to settle. The count is also blind to the merged case: post_tools joins every finished run of the point into one <dim-hooks kind="post-tool"> block (crates/dim-agent-capabilities/src/hooks/runner.rs:1871-1887), so an async run included in runs would keep the count at one even when its text is in the block.
Failure scenario. A change that reads an async handler's result and appends its context once the handler finishes (instead of treating the handler as synchronous, which the gate catches) can append after line 1705; the final check then passes even though the async context reached the session. Today spawn_async discards the result (runner.rs:188-206), so this is a coverage gap, not a current behavior bug.
Evidence. Local mutation: a second post-tool block containing async context, appended by the runtime 50 ms after the point. The instrumented append returned Ok and the test passed (0.10 s); the same append with a 0 ms delay failed line 1701 with left: 2, right: 1. So the final check only sees blocks that land before it, and the print added at 1672 is not covered.
Fix. Make the marker follow the context print (or add a completion marker after it), wait for the async run's settle (the test already imports watch_settled), and assert the single post-tool block's text does not contain async context, not only its count. If only the no-delay property is intended, drop the async context print and the the async handler writes no context claim so the test does not read as covering the output rule.
What still holds
The removed started.elapsed() < 1200ms is replaced by a causal gate that covers the same property, and the gate is not timing-dependent: with the async handler treated as synchronous, the point cannot write its block while the gate is closed, and the test fails at wait_blocks (hooks_flow.rs:944) after its 5 s budget (6.44 s total measured). The no-delay property is still asserted and no longer depends on machine speed. The atomic marker is correct: mv publishes the content in one step, so wait_file cannot read a partially written late; every added wait is bounded (wait_file 5 s, turn 10 s), and the gate loop ends at the handler timeout or when the process tree is dropped.
Verification
cargo test -p dim-agent-capabilities --test capabilities_flow hooks_flow::an_async_handler_does_not_delay_the_turn_or_the_next_point: passes at 82085a3 in four runs after the first build, 0.04-0.11 s each (first build 1 m 11 s).- Mutations were applied and reverted in the review worktree; it is clean at 82085a3.
- No residual hook processes after the runs.
cargo fmt --check -p dim-agent-capabilities: clean.- CI at 82085a3: ci-ok, changes, macos-native and verify (linux) passed; verify (linux) completed at 2026-11-26T06:31:48Z. static-checks, scripts, web, dependencies, acp-registry and windows-cli were skipped.
git merge-tree --write-treeis clean against origin/main 626f280 and against 49c14ad (#2197), bed9408 (#2206), 57f9390 (#2211), 2bbbb18 (#2214), 1a55806 (#2223), cfb6c05 (#2212) and 3914eaa (#2174). #2174 touchestests/capabilities_flow.rs, nothooks_flow.rs.- Only
crates/dim-agent-capabilities/tests/hooks_flow.rschanged.
| "type": "command", "async": true, | ||
| "command": format!("{}; printf 'late' > {}", sleep(1500), late.display()), | ||
| "command": format!( | ||
| "while [ ! -f {} ]; do sleep 0.01; done; printf 'late' > {}.tmp && mv {}.tmp {}; printf '{{\"hookSpecificOutput\":{{\"additionalContext\":\"async context\"}}}}'", |
There was a problem hiding this comment.
P3: this prints the async context after the marker is published, so wait_file(&late) at 1703 returns before the print, before the handler exits, and before the runtime reads its stdout. The final count check at 1705 can therefore pass with async context written later: locally, an instrumented append of a second post-tool block containing async context 50 ms after the point returned Ok and the test still passed, while the same append at 0 ms failed 1701 with left 2. The count also misses the merged case, since post_tools joins every run into one block (runner.rs:1871-1887). Today the result is discarded (runner.rs:188-206), so this is coverage only, but the check cannot catch the regression. Please publish the marker after the print (or wait for a completion marker plus the run's settle) and assert the block text lacks async context, or drop the print.
Summary
One slice of #2217.
an_async_handler_does_not_delay_the_turn_or_the_next_pointused a 1200 ms wall-clock upper bound, a fixed 2000 ms sleep after the turn, and a 1.5 s sleep inside the async hook fixture:donebefore the turn returns, whilelatedoes not exist yet.latemarker gives the same assertion with a clear timeout and no fixed window.The test now waits for
done, assertslateis absent while the async handler is gated, then releases the gate and waits forlate. It still asserts that the async handler writes no context. Local runtime drops from about 3.7 s to 0.07 s.Refs #2217. The rest of the
Minventory remains.Design rule checklist
beforecallbacks: n/a — no Timeline writes.Documentation
Verification
cargo test --locked -p dim-agent-capabilities --test capabilities_flow hooks_flow::an_async_handler_does_not_delay_the_turn_or_the_next_point -- --exact --test-threads=1— passed in 0.07 s.cargo clippy --locked -p dim-agent-capabilities --test capabilities_flow -- -D warnings— passed.cargo fmt --all -- --check— passed.