| 1 | use crate::conformance::{Allowed, Conformance, Side}; |
| 2 | use codewhale_engine::external_agents::acp::{AcpClient, ControlWrite, PermissionPolicy}; |
| 3 | use codewhale_engine::fleet::host::{ |
| 4 | FleetHostAdapter, FleetWorkerCommand, FleetWorkerStartRequest, FleetWorkerTransport, |
| 5 | LocalProcessFleetHostAdapter, |
| 6 | }; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | use std::time::Duration; |
| 9 | |
| 10 | const KNOWN: &[Allowed] = &[ |
| 11 | Allowed { |
| 12 | side: Side::Client, |
| 13 | frame: "session/set_model request", |
| 14 | problem: "`session/set_model` is not a client request in official v1", |
| 15 | reason: "compatibility, kept: sent only to an agent that offers the pre-v1 `models` object and no `model` config option", |
| 16 | }, |
| 17 | Allowed { |
| 18 | side: Side::Agent, |
| 19 | frame: "session/new response", |
| 20 | problem: "field `.models` is not in the official type", |
| 21 | reason: "the fixture's legacy leg offers the pre-v1 `models` object on purpose, to make the client send session/set_model", |
| 22 | }, |
| 23 | Allowed { |
| 24 | side: Side::Agent, |
| 25 | frame: "initialize response", |
| 26 | problem: "field `.agentCapabilities.sessionCapabilities.fork` is not in the official type", |
| 27 | reason: "options fixture advertises session/fork, which is unstable_session_fork, not stable v1", |
| 28 | }, |
| 29 | Allowed { |
| 30 | side: Side::Agent, |
| 31 | frame: "session/prompt response", |
| 32 | problem: "field `.usage` is not in the official type", |
| 33 | reason: "options fixture reports PromptResponse.usage, which is unstable_end_turn_token_usage, not stable v1", |
| 34 | }, |
| 35 | ]; |
| 36 | |
| 37 | const STEP: Duration = Duration::from_secs(20); |
| 38 | |
| 39 | fn fixture(name: &str) -> String { |
| 40 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 41 | .join("tests/fixtures") |
| 42 | .join(name) |
| 43 | .display() |
| 44 | .to_string() |
| 45 | } |
| 46 | |
| 47 | fn captured( |
| 48 | agent: &str, |
| 49 | workspace: &Path, |
| 50 | log: &Path, |
| 51 | drive: impl FnOnce(&mut AcpClient), |
| 52 | ) -> Vec<(Side, serde_json::Value)> { |
| 53 | let mut host = LocalProcessFleetHostAdapter::new(workspace); |
| 54 | let mut request = FleetWorkerStartRequest::new( |
| 55 | "w-acp-conformance", |
| 56 | FleetWorkerCommand::new( |
| 57 | "node", |
| 58 | [ |
| 59 | fixture("acp-tee.mjs"), |
| 60 | log.display().to_string(), |
| 61 | fixture(agent), |
| 62 | ], |
| 63 | ), |
| 64 | ); |
| 65 | request.transport = FleetWorkerTransport::JsonRpc; |
| 66 | request.cwd = Some(workspace.to_path_buf()); |
| 67 | host.start_worker(request).unwrap(); |
| 68 | let mut client = AcpClient::new(host.take_worker_io("w-acp-conformance").unwrap()); |
| 69 | drive(&mut client); |
| 70 | host.stop_worker("w-acp-conformance").unwrap(); |
| 71 | std::fs::read_to_string(log) |
| 72 | .unwrap() |
| 73 | .lines() |
| 74 | .map(|line| { |
| 75 | let (tag, frame) = line.split_once(' ').unwrap(); |
| 76 | let side = if tag == "C" { |
| 77 | Side::Client |
| 78 | } else { |
| 79 | Side::Agent |
| 80 | }; |
| 81 | (side, serde_json::from_str(frame).unwrap()) |
| 82 | }) |
| 83 | .collect() |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn the_acp_client_and_its_fixture_agents_speak_official_v1_shapes() { |
| 88 | let has_node = std::process::Command::new("node") |
| 89 | .arg("--version") |
| 90 | .output() |
| 91 | .is_ok_and(|output| output.status.success()); |
| 92 | if !has_node { |
| 93 | eprintln!("skipped: node is not installed"); |
| 94 | return; |
| 95 | } |
| 96 | let capture = tempfile::tempdir().unwrap(); |
| 97 | let workspace = tempfile::tempdir().unwrap(); |
| 98 | let mut frames = captured( |
| 99 | "acp-agent.mjs", |
| 100 | workspace.path(), |
| 101 | &capture.path().join("agent.log"), |
| 102 | |client| { |
| 103 | let identity = client.initialize(STEP).unwrap(); |
| 104 | let session = client |
| 105 | .new_session(&identity, workspace.path(), &[], STEP) |
| 106 | .unwrap() |
| 107 | .session_id; |
| 108 | client |
| 109 | .set_config_option(&session, "model", "fixture-model", STEP) |
| 110 | .unwrap(); |
| 111 | for policy in [ |
| 112 | PermissionPolicy::AllowOnce, |
| 113 | PermissionPolicy::RejectOnce, |
| 114 | PermissionPolicy::Cancel, |
| 115 | ] { |
| 116 | client.prompt(&session, "conform", policy, STEP).unwrap(); |
| 117 | } |
| 118 | client.cancel(&session).unwrap(); |
| 119 | client |
| 120 | .reopen_session(&identity, &session, workspace.path(), &[], STEP) |
| 121 | .unwrap(); |
| 122 | }, |
| 123 | ); |
| 124 | let options_workspace = tempfile::tempdir().unwrap(); |
| 125 | frames.extend(captured( |
| 126 | "acp-options-agent.mjs", |
| 127 | options_workspace.path(), |
| 128 | &capture.path().join("options.log"), |
| 129 | |client| { |
| 130 | let identity = client.initialize(STEP).unwrap(); |
| 131 | let created = client |
| 132 | .new_session(&identity, options_workspace.path(), &[], STEP) |
| 133 | .unwrap(); |
| 134 | let mut applied = 0; |
| 135 | for control in &created.offer.controls { |
| 136 | if control.write.is_none() || control.unavailable.is_some() { |
| 137 | continue; |
| 138 | } |
| 139 | if let Some(choice) = control.choices.last() { |
| 140 | client |
| 141 | .apply_control(&created.session_id, control, &choice.value, STEP) |
| 142 | .unwrap(); |
| 143 | applied += 1; |
| 144 | } |
| 145 | } |
| 146 | assert!( |
| 147 | applied >= 2, |
| 148 | "options fixture offered {applied} writable controls" |
| 149 | ); |
| 150 | client |
| 151 | .prompt( |
| 152 | &created.session_id, |
| 153 | "conform", |
| 154 | PermissionPolicy::AllowOnce, |
| 155 | STEP, |
| 156 | ) |
| 157 | .unwrap(); |
| 158 | }, |
| 159 | )); |
| 160 | |
| 161 | let legacy_workspace = tempfile::tempdir().unwrap(); |
| 162 | std::fs::write( |
| 163 | legacy_workspace.path().join("fixture-plan.json"), |
| 164 | r#"{"legacyModels":true}"#, |
| 165 | ) |
| 166 | .unwrap(); |
| 167 | frames.extend(captured( |
| 168 | "acp-agent.mjs", |
| 169 | legacy_workspace.path(), |
| 170 | &capture.path().join("legacy.log"), |
| 171 | |client| { |
| 172 | let identity = client.initialize(STEP).unwrap(); |
| 173 | let created = client |
| 174 | .new_session(&identity, legacy_workspace.path(), &[], STEP) |
| 175 | .unwrap(); |
| 176 | let model = created.offer.control("model").unwrap(); |
| 177 | assert_eq!(model.write, Some(ControlWrite::SetModel)); |
| 178 | client |
| 179 | .apply_control(&created.session_id, model, "legacy-b", STEP) |
| 180 | .unwrap(); |
| 181 | }, |
| 182 | )); |
| 183 | |
| 184 | let mut observed = Conformance::checking(&[Side::Client, Side::Agent]); |
| 185 | for (side, frame) in &frames { |
| 186 | observed.observe(*side, frame); |
| 187 | } |
| 188 | assert!( |
| 189 | observed.frames() > 30, |
| 190 | "only {} frames observed", |
| 191 | observed.frames() |
| 192 | ); |
| 193 | observed.assert_only(KNOWN); |
| 194 | } |
| 195 |