diff --git a/crates/ghostlight-dungeon/src/world/controllers.rs b/crates/ghostlight-dungeon/src/world/controllers.rs index 618a557..b9f6bd3 100644 --- a/crates/ghostlight-dungeon/src/world/controllers.rs +++ b/crates/ghostlight-dungeon/src/world/controllers.rs @@ -7089,6 +7089,415 @@ mod tests { assert!(prepared_matches_request(&sdk_side, &build(), command_id, 0)); } + /// Soul: every request builder, prepared exactly as `SdkInferencePort` + /// prepares one, satisfies `prepared_matches_request` — and the + /// content-addressed request id is the same value the connector's own + /// preparation produces, so a checkpoint written under one transport is + /// still the same identity under the other. + #[test] + fn soul_an_sdk_prepared_request_passes_every_builder_identity() { + let opportunity = fixture_opportunity(ControllerMode::OperationalAgent); + let granted = vec![speak_snapshot(opportunity.affordance_ids[0])]; + let constituents = vec![ConstituentWork { + subject: opportunity.scope.subject_id, + opportunity: opportunity.clone(), + granted: granted.clone(), + command_id: CommandId::new(), + }]; + let command_id = CommandId::new(); + let prompt = || { + vec![CodexInputItem::UserText { + text: "Hold the bridge.".into(), + }] + }; + let model = "claude-opus-5"; + assert!( + canonical_model(model), + "the SDK-routed model is not a canonical controller model" + ); + let builders: Vec<(InferencePurpose, usize, Box InferenceRequest>)> = vec![ + ( + InferencePurpose::Projector, + 0, + Box::new(move || { + projector_request(command_id, model, "Render it.".into()).unwrap() + }), + ), + ( + InferencePurpose::Persona, + 0, + Box::new(move || persona_request(command_id, model, "Speak.".into()).unwrap()), + ), + ( + InferencePurpose::Interpreter, + 0, + Box::new(move || interpreter_request(command_id, 0, model, prompt()).unwrap()), + ), + ( + InferencePurpose::OperationalAgent, + 1, + Box::new({ + let granted = granted.clone(); + move || operational_request(command_id, 1, model, &granted, prompt()).unwrap() + }), + ), + ( + InferencePurpose::GroupedAgent, + 2, + Box::new({ + let constituents = constituents.clone(); + move || grouped_request(command_id, 2, model, &constituents, prompt()).unwrap() + }), + ), + ]; + // The two clock stamps the two ports would make. Identity must not + // depend on either of them. + for (purpose, round, build) in builders { + let sdk_side = prepare_invocation("ghostlight-sdk-runtime", 4_102_444_800_000, build()) + .expect("the SDK-shaped preparation builds"); + let connector_side = + prepare_invocation("ghostlight-sdk-runtime", 4_000_000_000_000, build()) + .expect("the connector-shaped preparation builds"); + assert_eq!(sdk_side.purpose, purpose); + assert!( + prepared_matches_request(&sdk_side, &build(), command_id, round), + "{purpose:?} did not match its own request" + ); + assert_eq!( + sdk_side.invocation.request.request_id, + connector_side.invocation.request.request_id, + "{purpose:?}'s content-addressed id moved between transports" + ); + assert_eq!( + sdk_side.invocation.native_request_sha256, + connector_side.invocation.native_request_sha256 + ); + assert_eq!( + sdk_side.invocation.provider_request_sha256, + connector_side.invocation.provider_request_sha256 + ); + assert_ne!( + sdk_side.invocation.expires_at_unix_ms, + connector_side.invocation.expires_at_unix_ms + ); + } + } + + /// Soul: a checkpoint carrying an SDK-prepared invocation still validates. + /// `integrity_is_valid` names no transport, and this is the one variant + /// whose fixture already exists, so the claim is pinned where it is + /// cheapest to falsify. + #[test] + fn soul_a_checkpoint_carrying_an_sdk_prepared_invocation_is_valid() { + let opportunity = fixture_opportunity(ControllerMode::NarrativePersona); + let turn = fixture_persona_turn(&opportunity, "I say, \"Hold.\""); + let command_id = CommandId::new(); + let InterpreterLoopEvaluation::Continue { conversation } = + evaluate_interpreter_loop(&turn, "Translate.", &[]).unwrap() + else { + panic!("an empty interpreter transcript finalized") + }; + let request = interpreter_request(command_id, 0, "claude-opus-5", conversation).unwrap(); + let work = ControllerWork::Narrative(NarrativeCheckpoint::InterpreterInFlight { + command_id, + turn: turn.clone(), + interpreter_prompt: "Translate.".into(), + components: fixture_components(), + interruption: None, + opportunity: opportunity.clone(), + granted: vec![speak_snapshot(opportunity.affordance_ids[0])], + completed: Vec::new(), + invocation: prepare_invocation("ghostlight-sdk-runtime", 4_102_444_800_000, request) + .unwrap(), + }); + assert!( + work.integrity_is_valid(), + "an SDK-prepared invocation failed a checkpoint's own identity check" + ); + } + + /// A tool argument long enough that nothing on either path can be carrying + /// it in a fixed buffer, and shaped so it cannot decode for any lane. + fn oversized_arguments() -> String { + format!("{{\"detail\":\"{}\"", "\u{e9}bris ".repeat(4_096)) + } + + /// Soul, interpreter lane. Every adversarial call the transport can carry — + /// a decode failure, a duplicate state-changing call, an unknown tool name, + /// and an argument far past any plausible buffer — is answered by the oracle + /// with exactly the string the evaluator recomputes for the same transcript, + /// call for call and in order. + #[test] + fn soul_the_interpreter_oracle_equals_its_evaluator_under_adversarial_calls() { + let opportunity = fixture_opportunity(ControllerMode::NarrativePersona); + let source = "I say, \"The rain has teeth tonight.\""; + let turn = fixture_persona_turn(&opportunity, source); + let speech = "The rain has teeth tonight."; + let start = source.find(speech).unwrap(); + let span = json!({ + "source_start_byte": start, + "source_end_byte": start + speech.len(), + }) + .to_string(); + // A span past the end of the prose: `get(..)` yields nothing and the + // capture is of the empty string, which is a real answer and not a + // decode failure. + let past_end = json!({ + "source_start_byte": source.len() + 1, + "source_end_byte": source.len() + 9, + }) + .to_string(); + let oversized = oversized_arguments(); + let calls: Vec<(&str, &str, &str)> = vec![ + ("call-0", INTERPRETER_SPEAK_TOOL, span.as_str()), + // The duplicate: `captured_speech` is already set, so this one is a + // gap under a different string. + ("call-1", INTERPRETER_SPEAK_TOOL, span.as_str()), + ("call-2", INTERPRETER_SPEAK_TOOL, past_end.as_str()), + ("call-3", INTERPRETER_RECORD_GAP_TOOL, "not json at all"), + ("call-4", INTERPRETER_RECORD_GAP_TOOL, oversized.as_str()), + ("call-5", "speek", "{}"), + ( + "call-6", + INTERPRETER_SPEAK_TOOL, + "{\"source_start_byte\":0}", + ), + ]; + let mut oracle = InterpreterOracle::new(&turn, &[]); + assert_eq!(oracle.remaining_rounds() as usize, TOOL_STEP_BUDGET); + let answers: Vec = calls + .iter() + .map(|(_, name, arguments)| oracle.answer(name, arguments).unwrap()) + .collect(); + let output = oracle_round(&calls, "soul-interpreter"); + let InterpreterLoopEvaluation::Continue { conversation } = + evaluate_interpreter_loop(&turn, "Translate the prose.", &[output.clone()]).unwrap() + else { + panic!("a non-terminal interpreter round finalized") + }; + assert_eq!(answers, tool_results(&conversation)); + + // Terminality and the remaining budget agree: the evaluator still has + // rounds left, and the oracle seeded from the same evidence reports + // exactly the rounds the evaluator would still allow. + let seeded = InterpreterOracle::new(&turn, &[output.clone()]); + assert_eq!(seeded.remaining_rounds() as usize, TOOL_STEP_BUDGET - 1); + + // The one call the evaluator treats as terminal is terminal for the + // oracle's budget too: a round carrying it finalizes. + let finishing = vec![("call-7", FINISH_INTERPRETATION_TOOL, "{}")]; + let InterpreterLoopEvaluation::Complete { .. } = evaluate_interpreter_loop( + &turn, + "Translate the prose.", + &[output, oracle_round(&finishing, "soul-interpreter-end")], + ) + .unwrap() else { + panic!("a finished interpretation stayed open") + }; + } + + /// Soul, operational lane. The lane's one round-local reset is the only + /// thing the oracle could get wrong, so it is checked in both directions: + /// a duplicate terminal inside one round is a gap, the same call opening a + /// fresh round is a capture, and the transcript the oracle refuses is the + /// transcript the evaluator refuses. + #[test] + fn soul_the_operational_oracle_equals_its_evaluator_under_adversarial_calls() { + let opportunity = fixture_opportunity(ControllerMode::OperationalAgent); + let granted = vec![speak_snapshot(opportunity.affordance_ids[0])]; + let speak = granted[0].entry.kind.0.clone(); + let oversized = oversized_arguments(); + let calls: Vec<(&str, &str, &str)> = vec![ + ("call-0", RECORD_NEED_TOOL, r#"{"detail":"no route"}"#), + ("call-1", RECORD_NEED_TOOL, "not json at all"), + ("call-2", RECORD_NEED_TOOL, oversized.as_str()), + // A granted tool whose arguments do not decode is a need, so the + // round is still open after it. + ("call-3", speak.as_str(), "not json at all"), + ("call-4", "speek", "{}"), + ]; + let mut oracle = OperationalOracle::new(&granted, &[]); + assert_eq!(oracle.remaining_rounds() as usize, TOOL_STEP_BUDGET); + let answers: Vec = calls + .iter() + .map(|(_, name, arguments)| oracle.answer(name, arguments).unwrap()) + .collect(); + let round_zero = oracle_round(&calls, "soul-operational"); + let OperationalLoopEvaluation::Continue { conversation } = + evaluate_operational_loop("Hold the bridge.", &granted, &[round_zero.clone()]).unwrap() + else { + panic!("a non-terminal operational round finalized") + }; + assert_eq!(answers, tool_results(&conversation)); + + // The duplicate terminal, and the same call in a new round. One fold, + // two round boundaries, two different strings. + let speech = json!({ "text": "Hold the bridge." }).to_string(); + let mut inside = OperationalOracle::new(&granted, &[]); + assert_eq!( + inside.answer(&speak, &speech).unwrap(), + "invocation captured" + ); + assert_eq!( + inside.answer(&speak, &speech).unwrap(), + "one terminal choice is already captured" + ); + let terminal_round = + oracle_round(&[("call-5", speak.as_str(), speech.as_str())], "soul-op-t"); + let mut across = + OperationalOracle::new(&granted, &[round_zero.clone(), terminal_round.clone()]); + assert_eq!( + across.answer(&speak, &speech).unwrap(), + "invocation captured", + "the round boundary did not clear the terminal choice" + ); + assert_eq!(across.remaining_rounds() as usize, TOOL_STEP_BUDGET - 2); + + // A call after a terminal that contradicts it: the oracle refuses, and + // so does the evaluator re-deriving the same transcript. The port's + // abort therefore fires on exactly the transcripts re-derivation would + // have hard-errored on. + let mut contradicted = OperationalOracle::new(&granted, &[]); + contradicted.answer(&speak, &speech).unwrap(); + assert!(matches!( + contradicted.answer(FINISH_WITHOUT_PROPOSAL_TOOL, "{}"), + Err(ControllerError::ProviderContract { .. }) + )); + let contradicting_round = oracle_round( + &[ + ("call-6", speak.as_str(), speech.as_str()), + ("call-7", FINISH_WITHOUT_PROPOSAL_TOOL, "{}"), + ], + "soul-op-contradiction", + ); + assert!(matches!( + evaluate_operational_loop("Hold the bridge.", &granted, &[contradicting_round]), + Err(ControllerError::ProviderContract { .. }) + )); + } + + /// Soul, grouped lane. Two handles, so a terminal for one leaves the round + /// open and the whole adversarial sequence — including a call after that + /// handle's terminal — stays observable through the evaluator's own + /// conversation. + #[test] + fn soul_the_grouped_oracle_equals_its_evaluator_under_adversarial_calls() { + let opportunity = fixture_opportunity(ControllerMode::OperationalAgent); + let constituents = vec![ + ConstituentWork { + subject: opportunity.scope.subject_id, + opportunity: opportunity.clone(), + granted: vec![speak_snapshot(opportunity.affordance_ids[0])], + command_id: CommandId::new(), + }, + ConstituentWork { + subject: opportunity.scope.subject_id, + opportunity: opportunity.clone(), + granted: vec![speak_snapshot(opportunity.affordance_ids[0])], + command_id: CommandId::new(), + }, + ]; + let speak = constituents[0].granted[0].entry.kind.0.clone(); + let handle_zero = format!("c0__{speak}"); + let speech = json!({ "text": "Hold the bridge." }).to_string(); + let oversized = oversized_arguments(); + let calls: Vec<(&str, &str, &str)> = vec![ + ("call-0", "c99__carry", "{}"), + ("call-1", "c0__record_need", r#"{"detail":"no route"}"#), + ("call-2", "c1__record_need", "not json at all"), + ("call-3", "c1__record_need", oversized.as_str()), + // Handle zero takes its terminal, then keeps calling. + ("call-4", handle_zero.as_str(), speech.as_str()), + ("call-5", handle_zero.as_str(), speech.as_str()), + ("call-6", "c0__finish_without_proposal", "{}"), + ("call-7", "c1__notgranted", "{}"), + ]; + let mut oracle = GroupedOracle::new(&constituents, &[]); + assert_eq!(oracle.remaining_rounds() as usize, CELL_TOOL_STEP_BUDGET); + // The one call with no answer at all, taken on a throwaway oracle so + // the ordered comparison below is not disturbed. + let mut refusing = GroupedOracle::new(&constituents, &[]); + for (_, name, arguments) in &calls[..6] { + refusing.answer(name, arguments).unwrap(); + } + assert!(matches!( + refusing.answer("c0__finish_without_proposal", "{}"), + Err(ControllerError::ProviderContract { .. }) + )); + let open: Vec<(&str, &str, &str)> = calls + .iter() + .filter(|(id, _, _)| *id != "call-6") + .copied() + .collect(); + let answers: Vec = open + .iter() + .map(|(_, name, arguments)| oracle.answer(name, arguments).unwrap()) + .collect(); + let round_zero = oracle_round(&open, "soul-grouped"); + let GroupedLoopEvaluation::Continue { conversation } = + evaluate_grouped_loop("Decide together.", &constituents, &[round_zero.clone()]) + .unwrap() + else { + panic!("a grouped round with one handle still open finalized") + }; + assert_eq!(answers, tool_results(&conversation)); + + // And the evaluator refuses the transcript the oracle refuses. + let contradicting = oracle_round( + &[ + ("call-8", handle_zero.as_str(), speech.as_str()), + ("call-9", "c0__finish_without_proposal", "{}"), + ], + "soul-grouped-contradiction", + ); + assert!(matches!( + evaluate_grouped_loop("Decide together.", &constituents, &[contradicting]), + Err(ControllerError::ProviderContract { .. }) + )); + + // Seeding: the fold carries across rounds and the budget shrinks with + // it, so `maxTurns` can never exceed what the evaluator would allow. + let seeded = GroupedOracle::new(&constituents, &[round_zero.clone(), round_zero]); + assert_eq!( + seeded.remaining_rounds() as usize, + CELL_TOOL_STEP_BUDGET - 2 + ); + } + + /// Soul: the arguments the SDK path can hand an evaluator are the stripped + /// ones the SDK's own validation produced, and the connector path refuses + /// what that validation quietly admits. Both strings are recorded here so + /// the divergence is a fact rather than a guess (see the sidecar's + /// `soul-zod-liability.test.ts` for the other half of the chain). + #[test] + fn soul_a_property_outside_the_schema_is_a_need_on_the_connector_path() { + let opportunity = fixture_opportunity(ControllerMode::OperationalAgent); + let granted = vec![speak_snapshot(opportunity.affordance_ids[0])]; + let mut fold = OperationalFold::new(); + // What a model writes, and what the connector's decoder does with it. + assert_eq!( + operational_tool_result( + &granted, + &mut fold, + RECORD_NEED_TOOL, + r#"{"detail":"no route","extra":true}"#, + ) + .unwrap(), + "arguments recorded as a need", + ); + // What the SDK's validation hands the oracle for the same call. + assert_eq!( + operational_tool_result( + &granted, + &mut fold, + RECORD_NEED_TOOL, + r#"{"detail":"no route"}"#, + ) + .unwrap(), + "need recorded", + ); + } + fn narrative_interpreter_in_flight( command_id: CommandId, opportunity: &DecisionOpportunity, diff --git a/crates/ghostlight-dungeon/src/world/elaboration.rs b/crates/ghostlight-dungeon/src/world/elaboration.rs index a084581..ee8ead0 100644 --- a/crates/ghostlight-dungeon/src/world/elaboration.rs +++ b/crates/ghostlight-dungeon/src/world/elaboration.rs @@ -1961,6 +1961,155 @@ mod tests { .expect("the loop finalizes") } + /// Soul, the authoring lanes. The oracle's answer for every call in an + /// adversarial sequence — a decode failure, a duplicate terminal, a call + /// after the terminal, an unknown tool name, and an oversized argument — + /// equals the string the evaluator recomputes for the same transcript, and + /// the budget the oracle reports is the budget the evaluator still allows. + #[test] + fn soul_the_elaboration_oracle_equals_its_evaluator_under_adversarial_calls() { + let oversized = serde_json::json!({ + "detail": "\u{e9}bris ".repeat(4_096), + "unexpected": true, + }); + let calls: Vec<(&str, Value)> = vec![ + ("declare_place", place("shed")), + ( + RECORD_GAP_PATCH_TOOL, + serde_json::json!({"detail": "no route"}), + ), + // A `detail` that is not a string: a decode failure under the + // lane's own rule. + (RECORD_GAP_PATCH_TOOL, serde_json::json!({"detail": 5})), + (RECORD_GAP_PATCH_TOOL, oversized), + // A declaration whose arguments the kernel vocabulary refuses. + ("declare_place", serde_json::json!({"handle": "shed"})), + ("speek", serde_json::json!({})), + ]; + let round_zero = output(calls.clone()); + + let mut oracle = ElaborationOracle::new(&[], SEED_ROUND_BUDGET); + assert_eq!(oracle.remaining_rounds() as usize, SEED_ROUND_BUDGET); + let answers: Vec = calls + .iter() + .map(|(name, arguments)| oracle.answer(name, &arguments.to_string()).unwrap()) + .collect(); + let ElaborationLoopEvaluation::Continue { conversation } = + evaluate_elaboration_loop("prompt", &[], &[round_zero.clone()], SEED_ROUND_BUDGET) + .unwrap() + else { + panic!("a round with no submit finalized") + }; + let recomputed: Vec = conversation + .iter() + .filter_map(|item| match item { + CodexInputItem::ToolResult { output, .. } => Some(output.clone()), + _ => None, + }) + .collect(); + assert_eq!(answers, recomputed); + + // The terminal, the duplicate terminal, and a call after it. The + // evaluator finalizes that round, so its strings are compared against + // the same fold run straight through both rounds. + let terminal: Vec<(&str, Value)> = vec![ + (SUBMIT_PATCH_TOOL, serde_json::json!({})), + (SUBMIT_PATCH_TOOL, serde_json::json!({})), + ( + RECORD_GAP_PATCH_TOOL, + serde_json::json!({"detail": "after"}), + ), + ]; + let mut seeded = ElaborationOracle::new(&[round_zero.clone()], SEED_ROUND_BUDGET); + assert_eq!( + seeded.remaining_rounds() as usize, + SEED_ROUND_BUDGET - 1, + "the oracle offered a turn cap the evaluator would not allow" + ); + let seeded_answers: Vec = terminal + .iter() + .map(|(name, arguments)| seeded.answer(name, &arguments.to_string()).unwrap()) + .collect(); + assert_eq!( + seeded_answers, + vec![ + "patch submitted".to_string(), + "a submit is already captured".to_string(), + "gap recorded".to_string(), + ] + ); + let mut replayed = ElaborationOracle::new(&[], SEED_ROUND_BUDGET); + let straight: Vec = calls + .iter() + .chain(terminal.iter()) + .map(|(name, arguments)| replayed.answer(name, &arguments.to_string()).unwrap()) + .collect(); + assert_eq!(straight[..calls.len()], answers[..]); + assert_eq!(straight[calls.len()..], seeded_answers[..]); + + // The evaluator finalizes the terminal round, and what it captured is + // what the oracle's own fold accumulated over the same calls. + let mut draft = WorldPatch { + declarations: Vec::new(), + operations: Vec::new(), + evidence: Vec::new(), + }; + let mut gaps = Vec::new(); + let mut submitted = false; + for (name, arguments) in calls.iter().chain(terminal.iter()) { + apply_tool_call( + name, + &arguments.to_string(), + &mut draft, + &mut gaps, + &mut submitted, + ); + } + let ElaborationLoopEvaluation::Complete { capture } = evaluate_elaboration_loop( + "prompt", + &[], + &[round_zero, output(terminal)], + SEED_ROUND_BUDGET, + ) + .unwrap() else { + panic!("a submitted round stayed open") + }; + assert!(capture.submitted && submitted); + assert_eq!(capture.draft.declarations, draft.declarations); + assert_eq!(capture.gaps, gaps); + } + + /// Soul: the elaboration lane and the seed lane share one + /// `InferencePurpose`, so the only thing that distinguishes their turn caps + /// is the budget the oracle was built with. + #[test] + fn soul_the_two_authoring_lanes_carry_different_budgets_on_the_oracle() { + assert_ne!(ELABORATION_ROUND_BUDGET, SEED_ROUND_BUDGET); + assert_eq!( + ElaborationOracle::new(&[], ELABORATION_ROUND_BUDGET).remaining_rounds() as usize, + ELABORATION_ROUND_BUDGET + ); + assert_eq!( + ElaborationOracle::new(&[], SEED_ROUND_BUDGET).remaining_rounds() as usize, + SEED_ROUND_BUDGET + ); + // A spent budget reports zero rather than wrapping. Zero is outside the + // sidecar's accepted turn-cap range, so an exhausted lane that somehow + // reached the port is refused rather than quietly granted a turn. + let spent: Vec = (0..ELABORATION_ROUND_BUDGET + 2) + .map(|_| { + output(vec![( + RECORD_GAP_PATCH_TOOL, + serde_json::json!({"detail": "x"}), + )]) + }) + .collect(); + assert_eq!( + ElaborationOracle::new(&spent, ELABORATION_ROUND_BUDGET).remaining_rounds(), + 0 + ); + } + /// Each declaration tool call appends a `Declaration`; each operation call /// appends a `ComponentOp`; `submit` is terminal. #[test] diff --git a/crates/ghostlight-dungeon/src/world/sdk_inference.rs b/crates/ghostlight-dungeon/src/world/sdk_inference.rs index c14ef7c..99a3e80 100644 --- a/crates/ghostlight-dungeon/src/world/sdk_inference.rs +++ b/crates/ghostlight-dungeon/src/world/sdk_inference.rs @@ -1377,6 +1377,488 @@ mod tests { ); } + /// Soul: the port's `prepare` is `prepare_invocation` and nothing else, so + /// everything but the expiry stamp is identical to what any other port + /// would have written for the same request. + #[test] + fn soul_the_sdk_port_prepares_the_identity_prepare_invocation_owns() { + let port = SdkInferencePort::new(ScriptedLink::new(Vec::new()), TEST_RUNTIME); + let request = prose_request("Say something true."); + let through_port = port.prepare(request.clone()).expect("the port prepares"); + let through_owner = prepare_invocation(TEST_RUNTIME, 4_102_444_800_000, request.clone()) + .expect("the owner prepares"); + assert_eq!(through_port.purpose, through_owner.purpose); + assert_eq!( + through_port.invocation.request, + through_owner.invocation.request + ); + assert_eq!( + through_port.invocation.native_request_sha256, + through_owner.invocation.native_request_sha256 + ); + assert_eq!( + through_port.invocation.provider_request_sha256, + through_owner.invocation.provider_request_sha256 + ); + assert_eq!( + through_port.invocation.caller_runtime_id, + through_owner.invocation.caller_runtime_id + ); + assert_eq!( + through_port.invocation.request.request_id, + through_owner.invocation.request.request_id + ); + } + + /// Soul: a frame naming a kind or a fault reason the wire does not carry is + /// refused by the codec, before the driver can act on a half-understood + /// frame. The closed enums are the gate, and this proves they are closed. + #[tokio::test] + async fn soul_an_unknown_kind_or_fault_reason_is_refused_at_read_frame() { + #[derive(Serialize)] + struct Bogus<'a> { + kind: &'a str, + query_id: u64, + reason: &'a str, + detail: &'a str, + } + async fn refuse(body: Vec) -> InferenceFault { + let mut framed = (body.len() as u32).to_be_bytes().to_vec(); + framed.extend_from_slice(&body); + let mut reader = std::io::Cursor::new(framed); + link_fault( + read_frame(&mut reader) + .await + .expect_err("an unknown shape decoded"), + ) + } + // A kind that is not on the wire. + let unknown_kind = rmp_serde::to_vec_named(&Bogus { + kind: "hallucination", + query_id: 1, + reason: "rate_limited", + detail: "", + }) + .unwrap(); + assert!(refuse(unknown_kind).await.integrity_was_violated()); + // A fault whose reason the disposition table does not name. Nothing may + // reach `into_fault` that the table has no row for. + let unknown_reason = rmp_serde::to_vec_named(&Bogus { + kind: "fault", + query_id: 1, + reason: "the_world_should_be_quarantined", + detail: "", + }) + .unwrap(); + assert!(refuse(unknown_reason).await.integrity_was_violated()); + } + + /// Soul: every reason the sidecar may name maps to the disposition the + /// plan's table assigns, and no TypeScript-named reason can quarantine the + /// world except the three the sidecar's own gates raise. + #[test] + fn soul_every_sidecar_reason_carries_its_named_disposition() { + use SidecarFaultReason::*; + for reason in [RateLimited, Overloaded, ServerError, ApiTimeout] { + let fault = reason.into_fault("detail".into()); + assert!( + !fault.recovery_required() && !fault.integrity_was_violated(), + "{reason:?} was not retryable" + ); + } + for reason in [ + AuthenticationFailed, + OrgNotAllowed, + BillingError, + InvalidRequest, + ModelNotFound, + MaxOutputTokens, + MaxBudgetUsd, + ExecutionError, + Unknown, + ] { + assert!( + reason.into_fault("detail".into()).recovery_required(), + "{reason:?} was not recovery-required" + ); + } + for reason in [ProtocolViolation, ToolRegistrationFailed, TurnCapRefused] { + assert!( + reason.into_fault("detail".into()).integrity_was_violated(), + "{reason:?} did not violate integrity" + ); + } + } + + /// Soul: the lend gate refuses both mismatches, in both directions. + #[tokio::test] + async fn soul_a_lend_that_does_not_match_the_request_is_refused() { + // A tool request with no oracle lent. + let port = SdkInferencePort::new(ScriptedLink::new(Vec::new()), TEST_RUNTIME); + let prepared = seed_prepared(&port, "Author the shortfall."); + let fault = port + .infer(prepared) + .await + .expect_err("a tool request ran without its lane's owner"); + assert!(fault.integrity_was_violated(), "{fault:?}"); + + // A prose request with an oracle lent. + let port = SdkInferencePort::new(ScriptedLink::new(Vec::new()), TEST_RUNTIME); + let prepared = prose_prepared(&port, "Say something true."); + port.lend_tool_results( + &prepared, + Box::new(ElaborationOracle::new(&[], SEED_ROUND_BUDGET)), + ); + let fault = port + .infer(prepared) + .await + .expect_err("a prose request ran with a tool-result owner"); + assert!(fault.integrity_was_violated(), "{fault:?}"); + } + + /// Soul: a lend whose `infer` is refused before the oracle is taken leaves + /// the entry in the map. The port's own doc says the entry is removed on + /// every exit path of `infer`; the caller-identity and expiry gates run + /// before the take, so it is not. Bounded by the request id's uniqueness + /// and therefore not a leak that grows, but the claim as written is false. + #[tokio::test] + async fn soul_a_lend_refused_before_the_take_is_not_reclaimed() { + let port = SdkInferencePort::new(ScriptedLink::new(Vec::new()), TEST_RUNTIME); + let stale = prepare_invocation(TEST_RUNTIME, 1_000, prose_request("Say something true.")) + .expect("the stale invocation builds"); + port.lend_tool_results( + &stale, + Box::new(ElaborationOracle::new(&[], SEED_ROUND_BUDGET)), + ); + assert!(port.infer(stale.clone()).await.is_err()); + assert_eq!( + port.oracles.lock().unwrap().len(), + 1, + "the expiry gate now reclaims the lend; update this test's claim" + ); + + // A caller-identity mismatch does the same. + let other = SdkInferencePort::new(ScriptedLink::new(Vec::new()), "ghostlight-other"); + other.lend_tool_results( + &stale, + Box::new(ElaborationOracle::new(&[], SEED_ROUND_BUDGET)), + ); + let fault = other + .infer(stale) + .await + .expect_err("a foreign caller's invocation ran"); + assert!(fault.integrity_was_violated(), "{fault:?}"); + assert_eq!(other.oracles.lock().unwrap().len(), 1); + + // A completed query does reclaim its own lend. + let link = ScriptedLink::new(vec![SidecarFrame::Output { + query_id: 1, + events: Vec::new(), + receipt: material(), + }]); + let port = SdkInferencePort::new(link, TEST_RUNTIME); + let prepared = seed_prepared(&port, "Author the shortfall."); + port.lend_tool_results( + &prepared, + Box::new(ElaborationOracle::new(&[], SEED_ROUND_BUDGET)), + ); + port.infer(prepared).await.expect("the empty query returns"); + assert!(port.oracles.lock().unwrap().is_empty()); + } + + /// Soul: the receipt is bound to the request without carrying a word of it. + /// Nothing the model was shown, and nothing it wrote, may reach a digest + /// that is persisted as provenance. + #[test] + fn soul_the_receipt_carries_no_prompt_or_tool_text() { + let port = SdkInferencePort::new(ScriptedLink::new(Vec::new()), TEST_RUNTIME); + let secret = "the rain has teeth tonight"; + let prepared = prose_prepared(&port, secret); + let receipt = SdkInferenceReceipt { + schema_id: SDK_RECEIPT_SCHEMA.to_owned(), + request_id: prepared.invocation.request.request_id.clone(), + conversation_id: prepared.invocation.request.conversation_id.clone(), + caller_runtime_id: prepared.invocation.caller_runtime_id.clone(), + native_request_sha256: prepared.invocation.native_request_sha256, + provider_request_sha256: prepared.invocation.provider_request_sha256, + model: prepared.invocation.request.model.clone(), + session_id: material().session_id, + result_uuid: material().result_uuid, + subtype: material().subtype, + stop_reason: material().stop_reason, + num_turns: material().num_turns, + assistant_message_uuids: material().assistant_message_uuids, + assistant_request_ids: material().assistant_request_ids, + usage: material().usage, + total_cost_usd_estimate: material().total_cost_usd_estimate, + }; + let bytes = rmp_serde::to_vec_named(&receipt).unwrap(); + let rendered = String::from_utf8_lossy(&bytes); + assert!(!rendered.contains(secret), "the receipt carries the prompt"); + assert!( + !rendered.contains(&prepared.invocation.request.instructions), + "the receipt carries the instructions" + ); + // And the digest the output carries is the digest of exactly this. + let output = assemble_output( + &prepared, + vec![SidecarEvent::Text { text: "ok".into() }], + material(), + &[], + ) + .unwrap(); + assert_eq!( + output.receipt_digest, + format!("sha256:{:x}", Sha256::digest(&bytes)) + ); + // A different stop reason is a different receipt. + let mut stopped = material(); + stopped.stop_reason = Some("max_tokens".into()); + assert_ne!( + output.receipt_digest, + assemble_output( + &prepared, + vec![SidecarEvent::Text { text: "ok".into() }], + stopped, + &[] + ) + .unwrap() + .receipt_digest + ); + } + + /// Soul: the order of the dispatched calls is part of the gate, not just + /// the set. A sidecar that answered the same two calls in the other order + /// put a different string in front of the model than the evaluator will + /// recompute. + #[tokio::test] + async fn soul_dispatched_calls_out_of_order_are_refused() { + let calls = [ + ("call-0", RECORD_GAP_PATCH_TOOL, r#"{"detail":"one"}"#), + ("call-1", RECORD_GAP_PATCH_TOOL, r#"{"detail":"two"}"#), + ]; + let mut outbound: Vec = calls + .iter() + .map(|(id, name, arguments)| tool_call_frame(id, name, arguments)) + .collect(); + outbound.push(SidecarFrame::Output { + query_id: 1, + events: vec![ + dispatched_event(calls[1].0, calls[1].1, calls[1].2), + dispatched_event(calls[0].0, calls[0].1, calls[0].2), + ], + receipt: material(), + }); + let link = ScriptedLink::new(outbound); + let port = SdkInferencePort::new(link, TEST_RUNTIME); + let prepared = seed_prepared(&port, "Author the shortfall."); + port.lend_tool_results( + &prepared, + Box::new(ElaborationOracle::new(&[], SEED_ROUND_BUDGET)), + ); + let fault = port + .infer(prepared) + .await + .expect_err("a reordered dispatch produced an output"); + assert!(fault.integrity_was_violated(), "{fault:?}"); + } + + /// Soul: the lowering's own gate table, every row. + #[test] + fn soul_lower_query_refuses_the_shapes_the_gate_table_names() { + let port = SdkInferencePort::new(ScriptedLink::new(Vec::new()), TEST_RUNTIME); + // `lower_query` reads the provider request off the prepared value, so + // the shapes it must refuse are written there directly. Nothing here + // asserts a digest; the identity tests own that. + let build = |input: Vec, effort: Option<&str>| { + let mut prepared = prose_prepared(&port, "Say something true."); + prepared.invocation.request.input = input; + prepared.invocation.request.reasoning_effort = effort.map(str::to_owned); + prepared + }; + let user = |text: &str| CodexInputItem::UserText { text: text.into() }; + + // Every effort the SDK admits passes; anything else is refused. + for effort in SDK_EFFORT_LEVELS { + assert!(lower_query(1, &build(vec![user("go")], Some(effort)), 1).is_ok()); + } + let fault = lower_query(1, &build(vec![user("go")], Some("blistering")), 1) + .expect_err("an unmapped effort lowered"); + assert!(fault.integrity_was_violated(), "{fault:?}"); + + // The first item must be user text, and it must be the only one. + for input in [ + vec![CodexInputItem::AssistantText { text: "no".into() }], + Vec::new(), + vec![user("go"), user("again")], + ] { + let fault = lower_query(1, &build(input, None), 1) + .expect_err("a request that opens wrong lowered"); + assert!(fault.integrity_was_violated(), "{fault:?}"); + } + + // A prior round renders exactly as the sidecar's fixed header expects. + let frame = lower_query( + 9, + &build( + vec![ + user("go"), + CodexInputItem::AssistantText { + text: "thinking".into(), + }, + CodexInputItem::ToolCall { + call_id: "call-0".into(), + name: "record_gap".into(), + arguments: "{}".into(), + }, + CodexInputItem::ToolResult { + call_id: "call-0".into(), + output: "gap recorded".into(), + }, + ], + Some("medium"), + ), + 4, + ) + .expect("a multi-item request lowers"); + let SidecarFrame::Query { + query_id, + prompt, + transcript, + turn_cap, + effort, + .. + } = frame + else { + panic!("the lowering produced something other than a query") + }; + assert_eq!( + (query_id, turn_cap, effort.as_deref()), + (9, 4, Some("medium")) + ); + assert_eq!(prompt, "go"); + assert_eq!( + transcript, + vec![ + "assistant: thinking".to_string(), + "tool call record_gap: {}".to_string(), + "tool result: gap recorded".to_string(), + ] + ); + } + + /// Soul: an unroutable model is refused at `infer` as well as at open, and + /// an SDK-only deployment needs no connector and therefore no credential. + #[tokio::test] + async fn soul_routing_refuses_rather_than_falling_back() { + assert_eq!(DEFAULT_SDK_MODEL_PREFIX, "claude"); + let connector_only = RoutedInferencePort::new( + Some(Arc::new(SdkInferencePort::new( + ScriptedLink::new(Vec::new()), + TEST_RUNTIME, + )) as Arc), + None, + DEFAULT_SDK_MODEL_PREFIX, + ); + assert!(connector_only.route(TEST_MODEL).is_none()); + let prepared = + prepare_invocation(TEST_RUNTIME, 4_102_444_800_000, prose_request("Say it.")).unwrap(); + for fault in [ + connector_only + .prepare(prose_request("Say it.")) + .expect_err("an unroutable model prepared"), + connector_only + .infer(prepared) + .await + .expect_err("an unroutable model inferred"), + ] { + assert!(fault.integrity_was_violated(), "{fault:?}"); + assert!(fault.to_string().contains(TEST_MODEL)); + } + + // An SDK-only deployment opens with no connector binding at all, so no + // credential path is read. + let directory = tempfile::tempdir().unwrap(); + let entry = directory.path().join("main.js"); + std::fs::write(&entry, "// sidecar").unwrap(); + assert!( + open_inference( + None, + Some(SdkBinding { + sidecar_entry: entry, + caller_runtime_id: TEST_RUNTIME.into(), + model_prefix: DEFAULT_SDK_MODEL_PREFIX.into(), + }), + &models(TEST_MODEL), + ) + .is_ok() + ); + } + + /// Soul: Ghostlight holds no credential, so no credential name appears in + /// the code that opens, routes, or drives this transport. + #[test] + fn soul_no_credential_name_appears_in_the_ports_own_source() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); + // Assembled from halves so this test's own source is not a match. + let needles: [String; 8] = [ + format!(".{}", "credentials.json"), + format!("CLAUDE_CODE{}", "_OAUTH_TOKEN"), + format!(".{}", "claude.json"), + format!("apiKey{}", "Helper"), + format!("USER{}", "PROFILE"), + format!("home{}", "_dir"), + format!("GHOSTLIGHT_SDK{}", "_TOKEN"), + format!("GHOSTLIGHT_SDK{}", "_CREDENTIAL"), + ]; + let mut sources = Vec::new(); + for file in [ + "world/sdk_inference.rs", + "world/controllers.rs", + "runtime.rs", + ] { + let text = std::fs::read_to_string(root.join(file)).expect("the source reads"); + // Only the production half; a test may name what production must + // not. + let production = text + .split_once("\n#[cfg(test)]\nmod tests {") + .map(|(before, _)| before.to_owned()) + .unwrap_or(text); + for needle in &needles { + assert!( + !production.contains(needle.as_str()), + "{file} names {needle}" + ); + } + // `ANTHROPIC_API_KEY` is named once, in the port's doc comment, as + // the condition under which this port is deleted. It must appear + // nowhere that could read it. + let anthropic = format!("ANTHROPIC{}", "_"); + for line in production.lines() { + if line.contains(anthropic.as_str()) { + assert!( + line.trim_start().starts_with("///") || line.trim_start().starts_with("//"), + "{file} names an ANTHROPIC variable outside a comment: {line}" + ); + } + } + sources.push((file, production)); + } + // The one credential path that does exist belongs to the connector and + // is read only when a connector binding is built. + assert_eq!( + sources[1].1.matches("from_secret_file").count(), + 2, + "the connector key is read somewhere new" + ); + // The sidecar child inherits the ambient environment and is given none. + assert!( + !sources[0].1.contains(".env("), + "the port sets a child environment" + ); + assert!(!sources[0].1.contains("env_clear")); + } + /// The Rust half of the sidecar's schema-grammar pair (spec test 17): /// publishes every `parameters_json` the two catalogs actually emit, so the /// sidecar's closed converter is tested against real strings rather than diff --git a/sidecar/claude-sdk/test/soul-dispatcher.test.ts b/sidecar/claude-sdk/test/soul-dispatcher.test.ts new file mode 100644 index 0000000..1784526 --- /dev/null +++ b/sidecar/claude-sdk/test/soul-dispatcher.test.ts @@ -0,0 +1,99 @@ +// Soul: the two sidecar gates that live in the stdin dispatcher rather than in +// a pure function, driven against the real built entry point. +// +// Neither case reaches `query()`, so no Claude Code subprocess is spawned, no +// credential is looked for, and no network is touched. The gate that needs a +// live query — a second `query` frame while one is in flight — is deliberately +// not exercised here for that reason. + +import assert from "node:assert/strict"; +import { spawn } from "node:child_process"; +import { existsSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import test from "node:test"; +import { FrameReader, encodeFrame, type SidecarFrame } from "../src/frames.ts"; +import { encode } from "@msgpack/msgpack"; + +const entry = fileURLToPath(new URL("../dist/main.js", import.meta.url)); + +/** Writes raw bytes to a fresh sidecar child and returns the frames it emits. */ +async function exchange(bytes: Uint8Array): Promise { + const child = spawn(process.execPath, [entry], { + stdio: ["pipe", "pipe", "inherit"], + }); + const reader = new FrameReader(); + const frames: SidecarFrame[] = []; + child.stdout.on("data", (chunk: Buffer) => { + reader.push(new Uint8Array(chunk)); + for (;;) { + const frame = reader.next(); + if (!frame) { + break; + } + frames.push(frame); + } + }); + child.stdin.write(bytes); + child.stdin.end(); + await new Promise((done) => child.on("close", () => done())); + return frames; +} + +function framed(body: Uint8Array): Uint8Array { + const out = new Uint8Array(4 + body.length); + new DataView(out.buffer).setUint32(0, body.length, false); + out.set(body, 4); + return out; +} + +test("a frame kind the sidecar does not accept is a protocol violation", async (t) => { + if (!existsSync(entry)) { + t.skip("run `npm run build` first"); + return; + } + // A well-formed MessagePack map with a kind that is not on the wire. + const frames = await exchange( + framed(encode({ kind: "hallucination", query_id: 3 }, { useBigInt64: false })), + ); + assert.equal(frames.length, 1); + assert.deepEqual(frames[0], { + kind: "fault", + query_id: 0, + reason: "protocol_violation", + detail: "frame kind hallucination is not one this sidecar accepts", + }); +}); + +test("a tool result outside any query is a protocol violation", async (t) => { + if (!existsSync(entry)) { + t.skip("run `npm run build` first"); + return; + } + const frames = await exchange( + encodeFrame({ + kind: "tool_result", + query_id: 7, + call_id: "c0", + output: "gap recorded", + }), + ); + assert.equal(frames.length, 1); + const [fault] = frames; + assert.ok(fault && fault.kind === "fault"); + assert.equal(fault.reason, "protocol_violation"); + assert.equal(fault.query_id, 7); +}); + +test("a length prefix over the cap ends the sidecar rather than buffering it", async (t) => { + if (!existsSync(entry)) { + t.skip("run `npm run build` first"); + return; + } + const over = new Uint8Array(8); + new DataView(over.buffer).setUint32(0, 1_052_673, false); + const frames = await exchange(over); + assert.equal(frames.length, 1); + const [fault] = frames; + assert.ok(fault && fault.kind === "fault"); + assert.equal(fault.reason, "protocol_violation"); +}); diff --git a/sidecar/claude-sdk/test/soul-zod-liability.test.ts b/sidecar/claude-sdk/test/soul-zod-liability.test.ts new file mode 100644 index 0000000..499b24f --- /dev/null +++ b/sidecar/claude-sdk/test/soul-zod-liability.test.ts @@ -0,0 +1,205 @@ +// Soul: characterizes what the SDK's own Zod validation does to a tool call +// whose arguments Ghostlight's evaluators would have recorded as a gap, and +// asserts the sidecar names no credential and reads no environment. +// +// Nothing here proposes a fix. The chain is established end to end against the +// real SDK, with no credential and no network, so the divergence from the +// connector path is a recorded fact rather than a guess. + +import assert from "node:assert/strict"; +import { readFileSync, readdirSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import test from "node:test"; +import { z } from "zod"; +import { createSdkMcpServer, tool } from "@anthropic-ai/claude-agent-sdk"; +import { toolRawShape } from "../src/schema.ts"; +import { assembleEvents, SidecarFault } from "../src/main.ts"; + +const emitted: { name: string; parameters_json: string }[] = JSON.parse( + readFileSync(fileURLToPath(new URL("./schemas.json", import.meta.url)), "utf8"), +); + +function schemaFor(name: string): string { + const entry = emitted.find((candidate) => candidate.name === name); + assert.ok(entry, `the emitted fixture carries no ${name}`); + return entry.parameters_json; +} + +test("the emitted grammar's top level admits an extra property the connector refuses", () => { + // `rawShape` builds a bare record; only nested objects are `.strict()`. The + // SDK's `tool()` wraps the top-level record in a plain `z.object`, which + // strips unknown keys instead of rejecting them — while every emitted schema + // says `additionalProperties: false` and every Rust decoder carries + // `deny_unknown_fields`. + const gap = z.object(toolRawShape(schemaFor("record_gap"), "record_gap")); + const admitted = gap.safeParse({ detail: "no route", extra: true }); + assert.equal(admitted.success, true, "the top-level object refused an extra property"); + assert.deepEqual( + admitted.success && admitted.data, + { detail: "no route" }, + "the extra property was not stripped", + ); + + // Nested objects are closed, so the leak is exactly one level deep. + const nested = z.object(toolRawShape(schemaFor("speak"), "speak")); + assert.equal(nested.safeParse({ text: "hold", extra: 1 }).success, true); + const declare = z.object(toolRawShape(schemaFor("bind"), "bind")); + const deep = declare.safeParse({ subject: { ref: "draft", value: "x", extra: true } }); + assert.equal(deep.success, false, "a nested object admitted an extra property"); +}); + +test("the emitted grammar refuses a missing or mistyped required property", () => { + const gap = z.object(toolRawShape(schemaFor("record_gap"), "record_gap")); + assert.equal(gap.safeParse({}).success, false); + assert.equal(gap.safeParse({ detail: 5 }).success, false); + // A deep emitted shape gives a compliant model many more required leaves to + // miss. + const subject = z.object(toolRawShape(schemaFor("declare_subject"), "declare_subject")); + assert.equal(subject.safeParse({ handle: "smith" }).success, false); +}); + +test("the SDK answers a refused argument itself and never reaches the handler", async () => { + // The middle link, against the real in-process MCP server the sidecar + // registers. No credential, no network, no `query()`. + let mcp: { Client: new (info: { name: string; version: string }) => never }; + let memory: { InMemoryTransport: { createLinkedPair(): [never, never] } }; + try { + mcp = await import("@modelcontextprotocol/sdk/client/index.js"); + memory = await import("@modelcontextprotocol/sdk/inMemory.js"); + } catch { + // The MCP client is the SDK's own transitive dependency, not this + // package's. If a future install stops hoisting it, the two assertions + // either side of this test still stand on their own. + return; + } + const seen: unknown[] = []; + const handle = tool( + "record_gap", + "Record a gap.", + toolRawShape(schemaFor("record_gap"), "record_gap"), + async (args: unknown) => { + seen.push(args); + return { content: [{ type: "text" as const, text: "gap recorded" }] }; + }, + { alwaysLoad: true }, + ); + const server = createSdkMcpServer({ name: "ghostlight", tools: [handle], alwaysLoad: true }); + const [clientSide, serverSide] = memory.InMemoryTransport.createLinkedPair(); + const instance = (server as unknown as { instance: { connect(t: unknown): Promise } }) + .instance; + await instance.connect(serverSide); + const client = new mcp.Client({ name: "ghostlight-soul-probe", version: "0" }) as unknown as { + connect(t: unknown): Promise; + callTool(request: { name: string; arguments: unknown }): Promise<{ + isError?: boolean; + content: { text: string }[]; + }>; + close(): Promise; + }; + await client.connect(clientSide); + try { + const stripped = await client.callTool({ + name: "record_gap", + arguments: { detail: "no route", extra: true }, + }); + assert.notEqual(stripped.isError, true, "an extra property was refused after all"); + assert.deepEqual( + seen, + [{ detail: "no route" }], + "the handler saw arguments other than the stripped ones", + ); + + for (const refused of [{ detail: 5 }, {}]) { + const answer = await client.callTool({ name: "record_gap", arguments: refused }); + assert.equal(answer.isError, true, `${JSON.stringify(refused)} reached the handler`); + assert.match(answer.content[0]!.text, /Input validation error/); + } + assert.equal(seen.length, 1, "a refused argument still reached the handler"); + } finally { + await client.close(); + } +}); + +test("a registered tool_use block with no dispatch is a protocol violation", () => { + // What the sidecar then sees: the assistant message still carries a + // `tool_use` block for a registered name, but `session.ask` was never + // reached, so there is no dispatch to pair with it. + const registered = new Set(["record_gap"]); + assert.throws( + () => + assembleEvents( + [ + { + content: [ + { type: "tool_use", name: "mcp__ghostlight__record_gap", input: { detail: 5 } }, + ], + }, + ], + registered, + [], + ), + (error: unknown) => + error instanceof SidecarFault && error.reason === "protocol_violation", + "a registered tool_use with no dispatch did not raise protocol_violation", + ); + + // The mirror image: a dispatch the assistant messages never report. + assert.throws( + () => + assembleEvents([{ content: [] }], registered, [ + { call_id: "c0", name: "record_gap", arguments: "{}" }, + ]), + (error: unknown) => + error instanceof SidecarFault && error.reason === "protocol_violation", + ); + + // An unregistered name needs no dispatch and is reported undispatched, which + // is the path the evaluators already own as a gap. + assert.deepEqual( + assembleEvents( + [{ content: [{ type: "tool_use", name: "speek", input: {} }] }], + registered, + [], + ), + [ + { + kind: "tool_call", + call_id: "u0", + name: "speek", + arguments: "{}", + dispatched: false, + }, + ], + ); +}); + +test("no credential name appears anywhere in the sidecar source", () => { + const forbidden = [ + ".credentials.json", + "CLAUDE_CODE_OAUTH_TOKEN", + "ANTHROPIC_API_KEY", + "ANTHROPIC_AUTH_TOKEN", + ".claude.json", + "apiKeyHelper", + "USERPROFILE", + "homedir", + "--bare", + ]; + const source = fileURLToPath(new URL("../src", import.meta.url)); + for (const name of readdirSync(source)) { + const text = readFileSync(`${source}/${name}`, "utf8"); + for (const needle of forbidden) { + assert.equal(text.includes(needle), false, `${name} names ${needle}`); + } + } +}); + +test("the sidecar reads no environment and logs nothing of its own", () => { + const source = fileURLToPath(new URL("../src", import.meta.url)); + for (const name of readdirSync(source)) { + const text = readFileSync(`${source}/${name}`, "utf8"); + assert.equal(text.includes("process.env"), false, `${name} reads the environment`); + assert.equal(text.includes("console."), false, `${name} logs outside the frame protocol`); + assert.equal(text.includes("process.stderr"), false, `${name} writes to stderr`); + } +});