From 769ef64af64b92ccfb11de83d895741e6caed77d Mon Sep 17 00:00:00 2001 From: scotares Date: Sat, 21 Nov 2026 17:16:04 +0800 Subject: [PATCH 1/4] test(hooks): wait for SessionStart at activation instead of racing the first turn The user-document flow started a Session with a first prompt and then waited for the SessionStart hook to write its marker. The hook and the turn both start when the branch activates, and the hook is skipped when a turn starts first, so a loaded runner could fail the flow. Create the Session over the App Server wire without a turn, wait for the SessionStart hook fact, and close it. This exercises the same hook point deterministically; the wire client is a dev-dependency. --- Cargo.lock | 1 + crates/dimcode/Cargo.toml | 1 + crates/dimcode/tests/cli_flow/hooks.rs | 81 ++++++++++++++++++++------ 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd7cb22b64..8a5f64642d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1583,6 +1583,7 @@ dependencies = [ "dim-agent-types", "dim-app-server", "dim-cli", + "dim-client", "dim-event-hub", "dim-goal", "dim-image-understanding", diff --git a/crates/dimcode/Cargo.toml b/crates/dimcode/Cargo.toml index f325123394..ad06493f9e 100644 --- a/crates/dimcode/Cargo.toml +++ b/crates/dimcode/Cargo.toml @@ -60,6 +60,7 @@ dim-agent-capabilities.workspace = true agent-client-protocol-schema = { version = "=1.12.0", default-features = false } base64 = "0.22.1" sha2 = "0.10.9" +dim-client.workspace = true dim-agent-types.workspace = true # The legacy prompt-reference flow seeds a real branch through a Session, which # needs the runtime's test host. diff --git a/crates/dimcode/tests/cli_flow/hooks.rs b/crates/dimcode/tests/cli_flow/hooks.rs index b4a117ffff..74f2316573 100644 --- a/crates/dimcode/tests/cli_flow/hooks.rs +++ b/crates/dimcode/tests/cli_flow/hooks.rs @@ -373,13 +373,70 @@ fn hooks_toggle_conflict_reads_the_area_again() { daemon.join().unwrap(); } +/// Creates a Session over the App Server's own wire and waits for its +/// SessionStart hook point. A Session with no first turn runs the hook at +/// activation, so the check does not race a turn's model request. +fn start_hook_session( + home: &Home, + binary: &str, + project: &std::path::Path, + marker: &std::path::Path, + expected: &str, +) { + let endpoint = home.0.join("rust/daemon.sock"); + let runtime = tokio::runtime::Runtime::new().unwrap(); + runtime.block_on(async { + let mut client = dim_client::Client::connect(&dim_client::ConnectionConfig { + endpoint, + server: dim_client::Server { + program: binary.into(), + embedded: false, + }, + profile: None, + client_kind: "other", + client_name: "cli_flow hooks-flow", + protocol_version: 4, + }) + .await + .expect("connect to the App Server"); + let created = client + .rpc("session.create", json!({"cwd": project.to_str().unwrap()})) + .await + .expect("create the Session"); + assert!(created["sessionId"].is_string(), "{created}"); + wait_for_hook(marker, expected); + let closed = client + .rpc("session.close", json!({"sessionId": created["sessionId"]})) + .await + .expect("close the Session"); + assert!(closed["error"].is_null(), "{closed}"); + }); +} + +/// Waits for the next hook point to run the saved document, without assuming +/// when the started Session reaches activation. +fn wait_for_hook(marker: &std::path::Path, expected: &str) { + let deadline = Instant::now() + PATIENCE; + loop { + let seen = fs::read_to_string(marker).unwrap_or_default(); + if seen == expected { + return; + } + assert!( + Instant::now() < deadline, + "the saved document did not reach the next hook point: {seen:?}" + ); + thread::sleep(Duration::from_millis(10)); + } +} + #[test] #[ignore = "requires DIM_TEST_APP_SERVER pointing to the built production App Server"] fn project_hooks_start_disabled_and_the_user_enables_them() { let binary = std::env::var("DIM_TEST_APP_SERVER").unwrap(); let home = Home::new(); let _daemon = OwnedDaemon(home.0.join("rust/daemon.sock")); - let (config, model) = rejecting_model_config(&home, 2); + let (config, _model) = rejecting_model_config(&home, 2); let project = home.0.join("work"); fs::create_dir_all(project.join(".dimcode")).unwrap(); fs::write( @@ -487,18 +544,10 @@ fn project_hooks_start_disabled_and_the_user_enables_them() { ), "{listed}" ); - // The next hook point runs the document the CLI wrote. SessionStart - // fires before the model is asked, so the rejecting fixture endpoint - // does not keep it from running; that endpoint fails the turn - // immediately without retrying, so `Stop` is not reachable here. A - // turn's own failure is expected and not this test's subject. + // A Session with no first turn reaches its SessionStart hook point at + // activation. let marker = project.join("user-ran.txt"); - let _ = run(&["exec", "-p", "go"]); - assert_eq!( - fs::read_to_string(&marker).unwrap_or_default(), - "user-one", - "the saved document did not reach the next hook point" - ); + start_hook_session(&home, &binary, &project, &marker, "user-one"); // A replacement reaches the next hook point too. fs::write( &document, @@ -511,13 +560,7 @@ fn project_hooks_start_disabled_and_the_user_enables_them() { .status .success() ); - let _ = run(&["exec", "-p", "go"]); - assert_eq!( - fs::read_to_string(&marker).unwrap_or_default(), - "user-two", - "the replacement did not reach the next hook point" - ); - model.join().unwrap(); + start_hook_session(&home, &binary, &project, &marker, "user-two"); assert!(run(&["daemon", "stop"]).status.success()); } From 03e61ebe23bd5047a1af2ac6cf0d99e972143ef1 Mon Sep 17 00:00:00 2001 From: scotares Date: Sat, 21 Nov 2026 17:28:31 +0800 Subject: [PATCH 2/4] docs(architecture): record the dimcode test dependency on dim-client --- docs/architecture/rust-agent/generated/crates.json | 4 ++++ docs/architecture/rust-agent/generated/crates.mmd | 1 + 2 files changed, 5 insertions(+) diff --git a/docs/architecture/rust-agent/generated/crates.json b/docs/architecture/rust-agent/generated/crates.json index c0734d46cd..8fa6fc1eb4 100644 --- a/docs/architecture/rust-agent/generated/crates.json +++ b/docs/architecture/rust-agent/generated/crates.json @@ -575,6 +575,10 @@ "from": "dimcode", "to": "dim-agent-types" }, + { + "from": "dimcode", + "to": "dim-client" + }, { "from": "dimcode", "to": "rutis" diff --git a/docs/architecture/rust-agent/generated/crates.mmd b/docs/architecture/rust-agent/generated/crates.mmd index 975d552da9..42df31d836 100644 --- a/docs/architecture/rust-agent/generated/crates.mmd +++ b/docs/architecture/rust-agent/generated/crates.mmd @@ -144,6 +144,7 @@ flowchart LR dimcode["dimcode"] --> dim_agent_capabilities["dim-agent-capabilities"] dimcode["dimcode"] --> dim_agent_runtime["dim-agent-runtime"] dimcode["dimcode"] --> dim_agent_types["dim-agent-types"] + dimcode["dimcode"] --> dim_client["dim-client"] dimcode["dimcode"] --> rutis["rutis"] xtask["xtask"] --> dim_agent_aimux["dim-agent-aimux"] xtask["xtask"] --> dim_agent_attachments["dim-agent-attachments"] From 81c9852cb2e4c588dfdc598880cd791d171c974a Mon Sep 17 00:00:00 2001 From: scotares Date: Sat, 21 Nov 2026 17:41:09 +0800 Subject: [PATCH 3/4] test(hooks): drop the unused model fixture and the vacuous close check The wire flow submits no turn, so the rejecting endpoint only waited for requests that never came and panicked on its own. Write the offline fixture instead. Client::rpc already maps a JSON-RPC error to Err, so the close assertion could not fail. --- crates/dimcode/tests/cli_flow/hooks.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/dimcode/tests/cli_flow/hooks.rs b/crates/dimcode/tests/cli_flow/hooks.rs index 74f2316573..c1595d8455 100644 --- a/crates/dimcode/tests/cli_flow/hooks.rs +++ b/crates/dimcode/tests/cli_flow/hooks.rs @@ -436,7 +436,12 @@ fn project_hooks_start_disabled_and_the_user_enables_them() { let binary = std::env::var("DIM_TEST_APP_SERVER").unwrap(); let home = Home::new(); let _daemon = OwnedDaemon(home.0.join("rust/daemon.sock")); - let (config, _model) = rejecting_model_config(&home, 2); + let config = home.0.join("model.json"); + fs::write( + &config, + include_str!("../../../dim-agent-aimux/examples/offline.json"), + ) + .unwrap(); let project = home.0.join("work"); fs::create_dir_all(project.join(".dimcode")).unwrap(); fs::write( From 7605996d6618a11ff59e1c48d14ee163eeff5092 Mon Sep 17 00:00:00 2001 From: scotares Date: Sat, 21 Nov 2026 18:01:42 +0800 Subject: [PATCH 4/4] test(hooks): drop the vacuous session close assertion Client::rpc returns the result value and maps a JSON-RPC error to Err, and session.close replies with an empty object, so the assertion could not fail. --- crates/dimcode/tests/cli_flow/hooks.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/dimcode/tests/cli_flow/hooks.rs b/crates/dimcode/tests/cli_flow/hooks.rs index c1595d8455..1e2dd37a27 100644 --- a/crates/dimcode/tests/cli_flow/hooks.rs +++ b/crates/dimcode/tests/cli_flow/hooks.rs @@ -405,11 +405,10 @@ fn start_hook_session( .expect("create the Session"); assert!(created["sessionId"].is_string(), "{created}"); wait_for_hook(marker, expected); - let closed = client + client .rpc("session.close", json!({"sessionId": created["sessionId"]})) .await .expect("close the Session"); - assert!(closed["error"].is_null(), "{closed}"); }); }