Skip to content

test(hooks): wait for async hook markers instead of sleeping - #2224

Merged
eric8810 merged 3 commits into
mainfrom
dim/2217-hooks-timing
Nov 26, 2026
Merged

eric8810 merged 3 commits into
mainfrom
dim/2217-hooks-timing

Conversation

@eric8810

@eric8810 eric8810 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

One slice of #2217. an_async_handler_does_not_delay_the_turn_or_the_next_point used 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:

  • The 1200 ms bound asserted that the turn did not wait for the async hook. The same property is already visible from the ordering: the synchronous hook writes done before the turn returns, while late does not exist yet.
  • The fixed 2000 ms sleep waited for the async hook to finish. Waiting for its late marker gives the same assertion with a clear timeout and no fixed window.
  • The 1.5 s hook sleep became a gate file. The async handler blocks until the test writes the gate, so the test proves it was still running when the turn returned instead of assuming the runner finished the turn within 1.5 s.

The test now waits for done, asserts late is absent while the async handler is gated, then releases the gate and waits for late. 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 M inventory remains.

Design rule checklist

  • Scope: n/a — test-only change; no part, state or lifecycle.
  • Commands and results: n/a — no module state changes.
  • Handover: n/a — no handover points.
  • Timeline before callbacks: n/a — no Timeline writes.
  • Infrastructure: n/a — Timeline, Dispatcher, assembly and facades unchanged.
  • Core mechanisms: n/a — no plugin-specific handling.
  • Batch: n/a — no facts are written.
  • Context: n/a — no context inputs.
  • No reading another plugin's internal state: n/a.
  • Cross-language faces: n/a — no service or face changes.
  • Business configuration: n/a — no loader rows.
  • Missing mechanism capabilities: n/a.
  • Timeline facts: n/a.

Documentation

  • No accepted ADR is modified or deleted.
  • No other module's design is changed.
  • No delivery status is added to an ADR, index or roadmap.

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.

@zerob13 zerob13 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@eric8810

Copy link
Copy Markdown
Collaborator Author

Addressed the marker race in 82085a313.

The async hook now writes late.tmp and renames it to late, so wait_file only observes the complete marker; the creation-to-write window is gone.

I also took the non-blocking suggestion: the async command now emits a distinct additionalContext (async context) after publishing the marker. If that contribution leaked, the existing one-block assertions would fail; the test passes, so the async context is excluded rather than merely unproven.

Local run: hooks_flow::an_async_handler_does_not_delay_the_turn_or_the_next_point passed in 0.86 s; cargo clippy -p dim-agent-capabilities --test capabilities_flow -- -D warnings and cargo fmt --all -- --check passed.

The broader verify failure remains the two App Server timeouts outside this patch; their rerun is still in progress.

@zerob13 zerob13 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 yyhhyyyyyy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-tree is clean against origin/main 626f280 and against 49c14ad (#2197), bed9408 (#2206), 57f9390 (#2211), 2bbbb18 (#2214), 1a55806 (#2223), cfb6c05 (#2212) and 3914eaa (#2174). #2174 touches tests/capabilities_flow.rs, not hooks_flow.rs.
  • Only crates/dim-agent-capabilities/tests/hooks_flow.rs changed.

"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\"}}}}'",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

3 participants