Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

test(cli): close the conformance guard's probe hole, drop duplicate checks

thinhlpgda3c734efdc1
diff --git a/src-tauri/src/core/cli/contract_conformance.rs b/src-tauri/src/core/cli/contract_conformance.rs
index 51865731c..147a56d53 100644
--- a/src-tauri/src/core/cli/contract_conformance.rs
+++ b/src-tauri/src/core/cli/contract_conformance.rs
@@ -19,14 +19,22 @@
//! claiming coverage the build does not have, which is the only failure mode
//! a conformance suite actually has.
//!
-//! Each row therefore carries two field lists: what the contract says the
-//! message must carry, and what this build actually produces for it. The gap
-//! between them is the row's remaining work, stated rather than inferred.
+//! The probes are type-correct by construction: each row carries sample values
+//! for the fields the contract requires, because a probe of `null`s fails to
+//! deserialize *even once the row is implemented* and would let the table rot
+//! without a red test.
//!
//! Transcribed from #368's "Host -> session" and "Session -> host" tables plus
//! its "fields the mapping must not lose" table. The contract as written, not as
//! this build implements it.
+//!
+//! The one thing this module deliberately does not re-assert is that
+//! `INPUT_KINDS` and the parser agree: `stream_input`'s own
+//! `the_advertised_kinds_are_the_accepted_ones` pins that, in both directions
+//! and with a sample line per kind, so a second copy here would be weaker
+//! coverage that reads like more.
+use super::stream_input::InputMessage;
use crate::core::agent::events::StreamEvent;
/// One row of "Host -> session".
@@ -35,7 +43,7 @@ struct InputRow {
contract: &'static str,
/// The `type` this build accepts for the same job today, if it has one.
here: Option<&'static str>,
- /// The contract's payload fields, as `(key, sample JSON value)`.
+ /// The contract's payload fields, as `(key, sample JSON)`.
fields: &'static [(&'static str, &'static str)],
}
@@ -44,8 +52,13 @@ struct OutputRow {
contract: &'static str,
/// The event tag this build emits for the same job today, if it has one.
here: Option<&'static str>,
- /// Every field the contract says the message carries.
- contract_requires: &'static [&'static str],
+ /// Every field the contract says the message carries, as
+ /// `(key, sample JSON)`. For a row this build does not implement these build
+ /// the probe. For one it does, they are the contract's requirement written
+ /// down next to what is produced, so the gap is readable rather than
+ /// inferred; the assertion that holds the implementation to a field list is
+ /// [`build_produces`](Self::build_produces).
+ contract_requires: &'static [(&'static str, &'static str)],
/// What this build actually produces under `here`, asserted *exactly*:
/// adding or dropping a field is a failure until this table moves, because
/// either one changes what a client reads.
@@ -102,7 +115,13 @@ const SESSION_TO_HOST: &[OutputRow] = &[
// R5 compares `tool_specs` for value equality and R8 filters `models`
// to the ones that accept images. Neither is carried, and `provider` is
// not either: a client cannot tell which provider answered.
- contract_requires: &["model", "provider", "models", "tools", "tool_specs"],
+ contract_requires: &[
+ ("model", "\"claude-sonnet-4-5\""),
+ ("provider", "\"anthropic\""),
+ ("models", "[]"),
+ ("tools", "[]"),
+ ("tool_specs", "[]"),
+ ],
build_produces: &[
"protocol_version",
"session_id",
@@ -116,7 +135,11 @@ const SESSION_TO_HOST: &[OutputRow] = &[
OutputRow {
contract: "tool_request",
here: None,
- contract_requires: &["id", "name", "args"],
+ contract_requires: &[
+ ("id", "\"call-7\""),
+ ("name", "\"observe\""),
+ ("args", "{}"),
+ ],
build_produces: &[],
},
OutputRow {
@@ -124,7 +147,7 @@ const SESSION_TO_HOST: &[OutputRow] = &[
here: Some("step"),
// `step` counts upstream requests, not turns, and carries no cap: R9
// wants the cap visible *before* it is hit.
- contract_requires: &["turns", "maxTurns"],
+ contract_requires: &[("turns", "1"), ("maxTurns", "10")],
build_produces: &["index", "max"],
},
OutputRow {
@@ -136,13 +159,13 @@ const SESSION_TO_HOST: &[OutputRow] = &[
OutputRow {
contract: "text_delta",
here: Some("token"),
- contract_requires: &["text"],
+ contract_requires: &[("text", "\"hi\"")],
build_produces: &["text"],
},
OutputRow {
contract: "assistant_end",
here: None,
- contract_requires: &["text", "error"],
+ contract_requires: &[("text", "\"hi\""), ("error", "null")],
build_produces: &[],
},
OutputRow {
@@ -150,7 +173,7 @@ const SESSION_TO_HOST: &[OutputRow] = &[
here: Some("tool_call_started"),
// The contract carries the args with the start; this build streams them
// separately (`tool_call_args_delta`) and assembles them in `tool_call`.
- contract_requires: &["name", "args"],
+ contract_requires: &[("name", "\"command\""), ("args", "{}")],
build_produces: &["id", "name"],
},
OutputRow {
@@ -159,13 +182,17 @@ const SESSION_TO_HOST: &[OutputRow] = &[
// R11 wants a failure flag (carried as `is_error`) and structured
// `details` (absent). `content` being a `String` is R2's gap as much as
// a naming one: an image-bearing result cannot be expressed here.
- contract_requires: &["name", "error", "details"],
+ contract_requires: &[
+ ("name", "\"command\""),
+ ("error", "null"),
+ ("details", "null"),
+ ],
build_produces: &["id", "content", "is_error"],
},
OutputRow {
contract: "notice",
here: Some("notice"),
- contract_requires: &["text"],
+ contract_requires: &[("text", "\"compacted\"")],
build_produces: &["text"],
},
OutputRow {
@@ -173,36 +200,42 @@ const SESSION_TO_HOST: &[OutputRow] = &[
here: Some("error"),
// The contract's `error` is one operator-facing string; this build
// splits it into a code and a message.
- contract_requires: &["text"],
+ contract_requires: &[("text", "\"boom\"")],
build_produces: &["code", "message"],
},
OutputRow {
contract: "done",
here: Some("done"),
- contract_requires: &["stopped"],
+ contract_requires: &[("stopped", "true")],
build_produces: &["stop_reason", "usage"],
},
OutputRow {
contract: "model_changed",
here: None,
- contract_requires: &["success"],
+ contract_requires: &[
+ ("success", "true"),
+ ("provider", "\"anthropic\""),
+ ("model", "\"claude-sonnet-4-5\""),
+ ],
build_produces: &[],
},
OutputRow {
contract: "code_enabled",
here: None,
- contract_requires: &["success", "enabled", "tools"],
+ contract_requires: &[("success", "true"), ("enabled", "true"), ("tools", "[]")],
build_produces: &[],
},
OutputRow {
contract: "history_cleared",
here: None,
- contract_requires: &["success", "tools", "tool_specs"],
+ contract_requires: &[("success", "true"), ("tools", "[]"), ("tool_specs", "[]")],
build_produces: &[],
},
OutputRow {
// Synthesized by the host when stdout closes, so Jan never emits it.
- // The row is here to be checked as *not* an event.
+ // The row is here to be checked as *not* an event, which is a real
+ // invariant: a `worker_exit` on the wire would be a second, contradictory
+ // source of truth for a fact the host already knows.
contract: "worker_exit",
here: None,
contract_requires: &[],
@@ -210,53 +243,47 @@ const SESSION_TO_HOST: &[OutputRow] = &[
},
];
-/// The contract message as a line: its tag plus every field it must carry.
-///
-/// Used as a probe. It is refused while the row is unimplemented and accepted
-/// once the row lands, which is the whole mechanism of the "asserted missing"
-/// rule: the test does not have to be remembered, it fails on its own.
-fn line_for(row: &InputRow) -> String {
+/// A JSON object from a tag and `(key, sample JSON)` pairs.
+fn probe(tag: &str, fields: &[(&str, &str)]) -> serde_json::Value {
let mut value = serde_json::Map::new();
- value.insert("type".to_string(), row.contract.into());
- for (key, sample) in row.fields {
+ value.insert("type".to_string(), tag.into());
+ for (key, sample) in fields {
value.insert(
(*key).to_string(),
serde_json::from_str(sample).expect("sample"),
);
}
- serde_json::Value::Object(value).to_string()
+ serde_json::Value::Object(value)
}
-/// Rows the build implements parse as the kind the table names.
+/// The contract message a host would send for this row: its tag plus every
+/// field the contract says it carries.
+fn line_for(row: &InputRow) -> String {
+ probe(row.contract, row.fields).to_string()
+}
+
+/// The only host -> session row this build implements parses, as its own kind.
#[test]
-fn every_implemented_host_row_parses_as_the_kind_the_table_names() {
- let probes = [
- ("abort", serde_json::json!({ "type": "abort" })),
- (
- "user",
- serde_json::json!({ "type": "user", "text": "hello" }),
- ),
- (
- "permission",
- serde_json::json!({
- "type": "permission", "request_id": "ask-1", "decision": "deny"
- }),
- ),
- ];
+fn the_implemented_host_row_parses_as_its_own_kind() {
for row in HOST_TO_SESSION.iter().filter(|r| r.here.is_some()) {
let here = row.here.expect("filtered");
- let probe = probes
- .iter()
- .find(|(kind, _)| *kind == here)
- .map(|(_, probe)| probe)
- .unwrap_or_else(|| panic!("{here} is claimed as an implementation and has no probe"));
- super::stream_input::parse_input_line(&probe.to_string())
- .unwrap_or_else(|e| panic!("{here} is advertised but refused: {e}"));
+ // Built the way a client that read `init.input_kinds` would build it,
+ // not from the contract's fields, which this row does not share.
+ let line = match here {
+ "abort" => serde_json::json!({ "type": "abort" }).to_string(),
+ other => panic!("{other} is claimed as implemented and has no sample line"),
+ };
+ assert_eq!(
+ super::stream_input::parse_input_line(&line)
+ .unwrap_or_else(|e| panic!("{here} is advertised but refused: {e}")),
+ InputMessage::Abort,
+ "{here} must parse as the kind the table names, not merely parse"
+ );
}
}
-/// Rows the build does not implement are refused, and the refusal names what the
-/// channel does accept.
+/// Rows the build does not implement are refused, and the refusal names the kind
+/// that was rejected.
///
/// This is the guard that keeps the table honest: implementing `tool_result`
/// makes this test fail until the row moves, so the suite cannot rot into a list
@@ -271,56 +298,24 @@ fn every_unimplemented_host_row_is_refused_by_name() {
row.contract
),
};
+ // The client sent several lines; without the kind in the refusal it
+ // cannot tell which one was rejected.
assert!(
error.contains(row.contract),
"the refusal should name the offending kind: {error}"
);
- // The client is told what it may send, which is the only way it can
- // recover from a version skew without reading this build's source.
- for kind in super::stream_input::INPUT_KINDS {
- assert!(
- error.contains(kind),
- "the refusal must name every accepted kind ({kind}): {error}"
- );
- }
- }
-}
-
-/// The kinds the handshake advertises are exactly the kinds the parser accepts.
-///
-/// A kind added to the parser without a line in `INPUT_KINDS` is a capability no
-/// client is told about; one added to `INPUT_KINDS` without a parser arm is a
-/// promise the channel breaks.
-#[test]
-fn the_advertised_input_kinds_are_the_ones_the_channel_accepts() {
- for kind in super::stream_input::INPUT_KINDS {
- let probe = serde_json::json!({ "type": kind }).to_string();
- let unknown = format!(
- "unknown type '{kind}' ({})",
- super::stream_input::INPUT_KINDS.join(", ")
- );
- // A bare probe may legitimately fail for a missing field (`user` needs
- // `text`); what it must never do is fall through to "unknown type",
- // which is the arm a kind with no parser reaches.
- if let Err(error) = super::stream_input::parse_input_line(&probe) {
- assert_ne!(error, unknown, "{kind} is advertised but has no parser arm");
- }
}
}
/// Rows the build does not emit are not events yet.
///
-/// Probed with the contract's own fields, so the row stops failing here the
+/// Probed with the contract's own message, so the row stops failing here the
/// moment a variant lands and the table has to move with it.
#[test]
fn every_unimplemented_session_row_is_not_an_event_yet() {
for row in SESSION_TO_HOST.iter().filter(|r| r.here.is_none()) {
- let mut value = serde_json::Map::new();
- value.insert("type".to_string(), row.contract.into());
- for field in row.contract_requires {
- value.insert((*field).to_string(), serde_json::Value::Null);
- }
- if let Ok(event) = serde_json::from_value::<StreamEvent>(serde_json::Value::Object(value)) {
+ let message = probe(row.contract, row.contract_requires);
+ if let Ok(event) = serde_json::from_value::<StreamEvent>(message) {
panic!(
"'{}' is now an event ({event:?}) - move this row in SESSION_TO_HOST",
row.contract
@@ -402,14 +397,10 @@ fn every_implemented_session_row_carries_exactly_the_fields_claimed() {
}
}
-/// Every row states its own remaining work: a row with no `here` has no
-/// implementation, and a row with one produces something.
-///
-/// Cheap, and it is what makes the two field lists above readable as a gap
-/// rather than as noise: a `here` with an empty `build_produces` would be a row
-/// that claims an implementation while asserting nothing about it.
+/// The table says which rows are implemented, so a row cannot claim an
+/// implementation while asserting nothing about it.
#[test]
-fn the_table_states_which_rows_are_implemented() {
+fn every_implemented_session_row_declares_what_it_produces() {
for row in SESSION_TO_HOST {
assert_eq!(
row.here.is_some(),