From d0e7526ca66b8761b99c62d01e518b96b12463bc Mon Sep 17 00:00:00 2001 From: scotares Date: Thu, 26 Nov 2026 13:54:41 +0800 Subject: [PATCH 1/3] test(hooks): wait for async hook markers instead of sleeping --- crates/dim-agent-capabilities/tests/hooks_flow.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/crates/dim-agent-capabilities/tests/hooks_flow.rs b/crates/dim-agent-capabilities/tests/hooks_flow.rs index 77e88cfdf4..3986dda8ae 100644 --- a/crates/dim-agent-capabilities/tests/hooks_flow.rs +++ b/crates/dim-agent-capabilities/tests/hooks_flow.rs @@ -1682,28 +1682,17 @@ async fn an_async_handler_does_not_delay_the_turn_or_the_next_point() { let session = env.session().await; let note = env.path("work/note.txt"); std::fs::write(¬e, "note\n").unwrap(); - let started = std::time::Instant::now(); turn( &session, &format!("tool:read {{\"path\":{:?}}}", note.to_str().unwrap()), ) .await; - assert!(started.elapsed() < Duration::from_millis(1200)); - for _ in 0..100 { - if done.exists() { - break; - } - tokio::time::sleep(Duration::from_millis(10)).await; - } - assert!( - done.exists(), - "the PostToolUse hook did not write its marker" - ); + wait_file(&done, "the PostToolUse hook did not write its marker").await; assert_eq!(std::fs::read_to_string(&now).unwrap(), "now"); assert!(!late.exists(), "the async handler is still running"); let blocks = wait_blocks(&session, "post-tool", 1).await; assert_eq!(blocks.len(), 1, "the async handler writes no context"); - tokio::time::sleep(Duration::from_millis(2000)).await; + wait_file(&late, "the async handler did not finish").await; assert_eq!(std::fs::read_to_string(&late).unwrap(), "late"); assert_eq!(hook_blocks(&session, "post-tool").await.len(), 1); env.close().await; From 2fc6fcecc143d90855583ef714cb0ea500b7d8cc Mon Sep 17 00:00:00 2001 From: scotares Date: Thu, 26 Nov 2026 13:55:35 +0800 Subject: [PATCH 2/3] test(hooks): gate the async handler instead of sleeping --- crates/dim-agent-capabilities/tests/hooks_flow.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/dim-agent-capabilities/tests/hooks_flow.rs b/crates/dim-agent-capabilities/tests/hooks_flow.rs index 3986dda8ae..05e179166c 100644 --- a/crates/dim-agent-capabilities/tests/hooks_flow.rs +++ b/crates/dim-agent-capabilities/tests/hooks_flow.rs @@ -1663,11 +1663,16 @@ async fn an_async_handler_does_not_delay_the_turn_or_the_next_point() { let late = env.path("late"); let now = env.path("now"); let done = env.path("now.done"); + let gate = env.path("gate"); env.sources.set(document(serde_json::json!({ "PostToolUse": [ command(serde_json::json!({ "type": "command", "async": true, - "command": format!("{}; printf 'late' > {}", sleep(1500), late.display()), + "command": format!( + "while [ ! -f {} ]; do sleep 0.01; done; printf 'late' > {}", + gate.display(), + late.display(), + ), })), command(serde_json::json!({ "type": "command", @@ -1692,6 +1697,7 @@ async fn an_async_handler_does_not_delay_the_turn_or_the_next_point() { assert!(!late.exists(), "the async handler is still running"); let blocks = wait_blocks(&session, "post-tool", 1).await; assert_eq!(blocks.len(), 1, "the async handler writes no context"); + std::fs::write(&gate, "").unwrap(); wait_file(&late, "the async handler did not finish").await; assert_eq!(std::fs::read_to_string(&late).unwrap(), "late"); assert_eq!(hook_blocks(&session, "post-tool").await.len(), 1); From 82085a313dd4f94cec9741637dcd2550e3db0b4b Mon Sep 17 00:00:00 2001 From: scotares Date: Thu, 26 Nov 2026 14:19:06 +0800 Subject: [PATCH 3/3] test(hooks): publish the late marker atomically --- crates/dim-agent-capabilities/tests/hooks_flow.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/dim-agent-capabilities/tests/hooks_flow.rs b/crates/dim-agent-capabilities/tests/hooks_flow.rs index 05e179166c..67015e9e46 100644 --- a/crates/dim-agent-capabilities/tests/hooks_flow.rs +++ b/crates/dim-agent-capabilities/tests/hooks_flow.rs @@ -1669,9 +1669,11 @@ async fn an_async_handler_does_not_delay_the_turn_or_the_next_point() { command(serde_json::json!({ "type": "command", "async": true, "command": format!( - "while [ ! -f {} ]; do sleep 0.01; done; printf 'late' > {}", + "while [ ! -f {} ]; do sleep 0.01; done; printf 'late' > {}.tmp && mv {}.tmp {}; printf '{{\"hookSpecificOutput\":{{\"additionalContext\":\"async context\"}}}}'", gate.display(), late.display(), + late.display(), + late.display(), ), })), command(serde_json::json!({