Files
sessions/rust/crates/session_protocol/tests/envelope_parity.rs
Myeongseon Choi 0d370dee0b feat(session_protocol): PR 13a — Wave 2 envelope spec freeze + ref impl
PYTHON_THINNING_PLAN §5 PR 13a (Wave 2 게이트). 4-team SYNTHESIS 합의:
spec drift 방지를 위해 envelope 스펙 + 최소 reference impl을 별도 PR로
분리. PR 13a land *후*에야 PR 16 (PR-A 본체)이 envelope 표준에
정합하게 빚어진다는 보장.

산출물:

- rust/crates/session_protocol/src/envelope.rs 신설:
  - ``Envelope { v, channel, kind, body }`` struct (serde Derive).
  - ``Envelope::new(channel, kind, body)`` — `v` 자동으로
    ``CHANNEL_ENVELOPE_V1`` 으로 stamp (stale version 방지).
  - ``Envelope::is_current_version()`` — forward-compat marker 검증.
  - ``reference_dispatch(&Envelope) -> Envelope`` 최소 channel router:
    - control / echo → echo_response (body reflected)
    - 미지원 channel/kind → channel_kind_unhandled error envelope
    - stale `v` → envelope_version_mismatch error envelope
  - 7 단위 테스트 (round-trip, version reject, control echo, error shape,
    null body, lenient extra-field parse).

- rust/crates/session_protocol/src/lib.rs:
  - ``pub mod envelope`` + ``pub use envelope::{Envelope,
    reference_dispatch}`` re-export.

- rust/crates/session_protocol/tests/envelope_parity.rs 신설 (5 테스트):
  - byte-for-byte NDJSON shape pin (4 field 순서 + value).
  - reference_dispatch round-trip / version reject / unknown channel.
  - cross-crate import 경로 검증 (PR 13b/PR 16에서 같은 경로 사용).

PR 13b 후속 (Wave 2 envelope 완전 구현):
- file / exec_once / lsp:* channel handlers 추가.
- per-request timeout / 취소 / 우선순위 / back-pressure.
- session_helper 측 cancellation hook (현재 lib.rs:215 "not yet implemented").

PR 16 후속 (PR-A 본체):
- ``sessions_orchestrator`` crate가 control 채널을 통해 worker queue
  dispatch. envelope shape 정합 보장은 PR 13a 의 reference_dispatch가
  컴파일 시점에 강제.

테스트: cargo test --workspace 그린 (session_protocol 5 신규 + 기존 64
+ envelope.rs 단위 7 + 다른 crate 그대로). clippy 그린 (테스트 시그니처
``Result<(), serde_json::Error>`` + ``?`` 패턴 — workspace
``unwrap_used / expect_used = "deny"`` 정합).

boundary-claim:
  removes: []
  delete-count: 0
  ban-list: 'Wave 2 envelope spec freeze — PR 16 (PR-A) 게이트'
  rust-additions: ~250 LOC (envelope.rs + parity tests)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 00:33:10 +09:00

94 lines
3.6 KiB
Rust

//! Wave 2 envelope parity test (PR 13a — spec freeze gate).
//!
//! Wire-shape pin for the `v` / `channel` / `kind` / `body` envelope. Any
//! future change to those four field names breaks this fixture by design —
//! Wave 2 implementations (PR 13b channel supervisor, PR 16 PR-A worker
//! body) must round-trip through this exact NDJSON shape, so the freeze
//! lives here in tests rather than buried in implementation files.
//!
//! Internal serde behaviour is covered by `envelope::tests` inside the
//! crate. This integration test exists for the *cross-crate parity*
//! contract — it imports through the public `session_protocol` re-export
//! exactly as `local_bridge` / `session_helper` / `sessions_native` will.
use session_protocol::{CHANNEL_ENVELOPE_V1, Envelope, reference_dispatch};
#[test]
fn envelope_canonical_ndjson_shape_is_frozen() -> Result<(), serde_json::Error> {
// The four-field shape every Wave 2 channel handler must accept. If you
// need to extend the wire shape, bump CHANNEL_ENVELOPE_V1 and add a new
// parity fixture below — do not edit this one.
let canonical = serde_json::json!({
"v": "sessions.channel.v1",
"channel": "control",
"kind": "echo",
"body": {"hello": "world"},
});
let env: Envelope = serde_json::from_value(canonical.clone())?;
assert_eq!(env.v, CHANNEL_ENVELOPE_V1);
assert_eq!(env.channel, "control");
assert_eq!(env.kind, "echo");
assert_eq!(env.body, serde_json::json!({"hello": "world"}));
let re_serialized = serde_json::to_value(&env)?;
assert_eq!(re_serialized, canonical);
Ok(())
}
#[test]
fn reference_dispatch_round_trips_control_echo() {
let request = Envelope::new(
"control",
"echo",
serde_json::json!({"id": "req-1", "payload": [1, 2, 3]}),
);
let response = reference_dispatch(&request);
assert!(response.is_current_version());
assert_eq!(response.channel, "control");
assert_eq!(response.kind, "echo_response");
assert_eq!(
response.body,
serde_json::json!({"id": "req-1", "payload": [1, 2, 3]}),
);
}
#[test]
fn reference_dispatch_rejects_stale_version() {
let request = Envelope {
v: "sessions.channel.v0".to_string(),
channel: "control".to_string(),
kind: "echo".to_string(),
body: serde_json::Value::Null,
};
let response = reference_dispatch(&request);
assert_eq!(response.kind, "error");
assert_eq!(response.body["code"], "envelope_version_mismatch");
// The error envelope itself is *current* version — only the rejected
// request held the stale `v`.
assert!(response.is_current_version());
}
#[test]
fn unknown_channel_kind_yields_structured_error_envelope() {
let request = Envelope::new("file", "tree/list", serde_json::Value::Null);
let response = reference_dispatch(&request);
assert_eq!(response.kind, "error");
assert_eq!(response.body["code"], "channel_kind_unhandled");
// PR 13b will replace this branch with a real `file` channel handler.
assert_eq!(response.body["channel"], "file");
assert_eq!(response.body["kind"], "tree/list");
}
#[test]
fn ndjson_round_trip_preserves_byte_for_byte_field_names() -> Result<(), serde_json::Error> {
// Byte-level pin: serde-derived Serialize emits keys in struct order.
// PR 16 (PR-A) relies on this when comparing recorded fixtures.
let env = Envelope::new("control", "echo", serde_json::json!({"x": 1}));
let line = serde_json::to_string(&env)?;
assert_eq!(
line,
r#"{"v":"sessions.channel.v1","channel":"control","kind":"echo","body":{"x":1}}"#,
);
Ok(())
}