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..1e2dd37a27 100644 --- a/crates/dimcode/tests/cli_flow/hooks.rs +++ b/crates/dimcode/tests/cli_flow/hooks.rs @@ -373,13 +373,74 @@ 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); + client + .rpc("session.close", json!({"sessionId": created["sessionId"]})) + .await + .expect("close the Session"); + }); +} + +/// 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 = 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( @@ -487,18 +548,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 +564,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()); } 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"]