feat: migrate protocol and HTTP to structured interface references

T-20: identify interfaces by exact scope/name pairs; add optional opaque scope_ref metadata and separate shape validation from path-dependent scope compatibility. Preserve call error precedence, strict JSON, validators and transport boundaries. Mechanically migrate consumers and document API/wire compatibility and deferred lifecycle semantics.

Validation: cargo fmt --all --check; git diff --check; cargo check --workspace --all-targets; cargo test -p wip-protocol -p wip-http; cargo test --workspace (221 passing tests including doctests); cargo test --workspace --all-targets; nix develop --command cargo clippy --workspace --all-targets -- -D warnings; RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps. All pass. cargo/rustc 1.95.0; development-shell Clippy/rustc 1.97.1.
This commit is contained in:
2026-10-07 18:29:51 +09:00
parent bf5fa54f44
commit 8ecc307fc9
34 changed files with 2124 additions and 263 deletions
@@ -0,0 +1,219 @@
use std::collections::BTreeMap;
use wip_protocol::{
CallOperationRequest, FetchInterfaceRequest, FetchInterfaceResponse, InterfaceDescriptor,
InterfaceReference, InterfaceTarget, Object, ObjectObservation, ObserveRequest,
OperationDeclaration, ReturnDeclaration, Target, TypeExpr, ValidationErrorKind,
};
fn reference(scope: &str, name: &str) -> InterfaceReference {
InterfaceReference {
scope: scope.into(),
name: name.into(),
}
}
fn object(path: &str, interfaces: Vec<InterfaceReference>) -> Object {
Object {
name: path.rsplit('/').next().unwrap().into(),
description: None,
interfaces,
r#ref: None,
validator: None,
}
}
fn descriptor() -> InterfaceDescriptor {
InterfaceDescriptor {
format: wip_protocol::INTERFACE_FORMAT_V1.into(),
documentation: None,
types: vec![],
operations: vec![OperationDeclaration {
name: "run".into(),
documentation: None,
parameters: vec![],
returns: ReturnDeclaration {
documentation: None,
r#type: TypeExpr::Unit,
},
}],
}
}
#[test]
fn scope_is_segment_based_ancestor_or_self_not_indexability() {
for (scope, path) in [
("/", "/"),
("/", "/a/b"),
("/a", "/a"),
("/a", "/a/b"),
("/a/b", "/a/b/c"),
] {
reference(scope, "read").validate_for_path(path).unwrap();
object(path, vec![reference(scope, "read")])
.validate_at_path(path)
.unwrap();
}
for (scope, path) in [
("/a", "/ab"),
("/a", "/ab/c"),
("/a/b", "/a/c"),
("/a/b", "/a"),
("/a", "/"),
] {
assert!(matches!(
reference(scope, "read")
.validate_for_path(path)
.unwrap_err()
.kind,
ValidationErrorKind::InterfaceScopeMismatch { .. }
));
// Without the placement context, the Object/reference shape is valid.
let object = object(path, vec![reference(scope, "read")]);
object.validate().unwrap();
assert!(matches!(
object.validate_at_path(path).unwrap_err().kind,
ValidationErrorKind::InterfaceScopeMismatch { .. }
));
}
}
#[test]
fn shape_validation_does_not_normalize_or_parse_fields() {
for scope in ["", "relative", "//a", "/a/", "/a//b", "/a/./b", "/a/../b"] {
let reference = reference(scope, "read");
assert!(matches!(
reference.validate().unwrap_err().kind,
ValidationErrorKind::InvalidPath { .. }
));
assert!(
FetchInterfaceRequest {
interface: reference.clone()
}
.validate()
.is_err()
);
assert!(object("/a", vec![reference]).validate().is_err());
}
assert_eq!(
reference("/", "").validate().unwrap_err().kind,
ValidationErrorKind::EmptyInterfaceName
);
for name in [".", "..", "a/b", "::", "#", "読み::書き#λ", "e\u{301}"] {
let reference = reference("/世::界/#/%2F", name);
reference.validate().unwrap();
assert_eq!(reference.name, name);
assert_eq!(reference.scope, "/世::界/#/%2F");
}
assert_ne!(reference("/", "é"), reference("/", "e\u{301}"));
assert_ne!(reference("/A", "read"), reference("/a", "read"));
assert_ne!(reference("/%61", "read"), reference("/a", "read"));
}
#[test]
fn duplicate_membership_and_correlation_use_complete_pair() {
let root = reference("/", "read");
let scoped = reference("/a", "read");
let mut object = object("/a", vec![root.clone(), scoped.clone()]);
object.validate_at_path("/a").unwrap();
assert!(object.interfaces.contains(&scoped));
assert!(!object.interfaces.contains(&reference("/b", "read")));
object.interfaces.push(root.clone());
assert_eq!(
object.validate().unwrap_err().kind,
ValidationErrorKind::DuplicateInterface {
reference: root.clone()
}
);
let request = FetchInterfaceRequest { interface: root };
let mut response = FetchInterfaceResponse {
interface: scoped,
scope_ref: None,
descriptor: descriptor(),
validator: None,
};
assert!(matches!(
response.validate_for(&request).unwrap_err().kind,
ValidationErrorKind::InterfaceResponseMismatch { .. }
));
response.interface = request.interface.clone();
for scope_ref in [None, Some(String::new()), Some("opaque::世界#/%".into())] {
response.scope_ref = scope_ref;
response.validate_for(&request).unwrap();
}
response.interface.name.clear();
assert_eq!(
response.validate_for(&request).unwrap_err().kind,
ValidationErrorKind::EmptyInterfaceName
);
}
#[test]
fn observe_checks_scope_at_every_node_including_depth_boundary() {
let request = ObserveRequest {
path: "/a".into(),
depth: 1,
};
let mut response = ObjectObservation {
object: object("/a", vec![reference("/a", "read")]),
children: Some(vec![ObjectObservation {
object: object("/a/b", vec![reference("/a/b", "read")]),
children: None,
}]),
};
request.validate_response(&response).unwrap();
response.children.as_mut().unwrap()[0].object.interfaces[0].scope = "/a/c".into();
assert!(matches!(
request.validate_response(&response).unwrap_err().kind,
ValidationErrorKind::InterfaceScopeMismatch { .. }
));
}
#[test]
fn fetch_needs_no_target_path_and_call_separates_shape_from_scope_selection() {
let reference = reference("/some/unrelated/scope", "read");
let fetch = FetchInterfaceRequest {
interface: reference.clone(),
};
fetch.validate().unwrap();
FetchInterfaceResponse {
interface: reference.clone(),
scope_ref: None,
descriptor: descriptor(),
validator: None,
}
.validate_for(&fetch)
.unwrap();
let mut call = CallOperationRequest {
target: Target {
path: "/a".into(),
validator: None,
},
interface: InterfaceTarget {
reference,
scope_ref: Some("opaque".into()),
validator: None,
},
operation: "run".into(),
arguments: BTreeMap::new(),
};
call.validate().unwrap();
call.validate_with(&descriptor()).unwrap();
// This stage is Host InterfaceMismatch, after NotFound, not shape InvalidRequest.
assert!(matches!(
call.interface
.reference
.validate_for_path(&call.target.path)
.unwrap_err()
.kind,
ValidationErrorKind::InterfaceScopeMismatch { .. }
));
call.interface.reference.name.clear();
assert_eq!(
call.validate().unwrap_err().kind,
ValidationErrorKind::EmptyInterfaceName
);
assert_eq!(
call.validate_with(&descriptor()).unwrap_err().kind,
ValidationErrorKind::EmptyInterfaceName
);
}
+26 -13
View File
@@ -3,12 +3,19 @@ use std::collections::BTreeMap;
use wip_protocol::{
CallOperationRequest, CallOperationResponse, EnumCase, FetchInterfaceRequest,
FetchInterfaceResponse, FieldDeclaration, INTERFACE_FORMAT_V1, InterfaceDescriptor,
InterfaceTarget, MAX_SAFE_INTEGER, NameNamespace, Object, ObjectObservation, ObserveRequest,
OperationDeclaration, ParameterDeclaration, PathSegment, ProtocolError, ProtocolErrorCode,
ProtocolInteraction, ProtocolResult, ReturnDeclaration, Target, TypeDeclaration, TypeExpr,
UnionCase, ValidationErrorKind, Value, ValueKind,
InterfaceReference, InterfaceTarget, MAX_SAFE_INTEGER, NameNamespace, Object,
ObjectObservation, ObserveRequest, OperationDeclaration, ParameterDeclaration, PathSegment,
ProtocolError, ProtocolErrorCode, ProtocolInteraction, ProtocolResult, ReturnDeclaration,
Target, TypeDeclaration, TypeExpr, UnionCase, ValidationErrorKind, Value, ValueKind,
};
fn reference(scope: &str, name: &str) -> InterfaceReference {
InterfaceReference {
scope: scope.into(),
name: name.into(),
}
}
fn record(entries: impl IntoIterator<Item = (&'static str, Value)>) -> Value {
Value::Record(
entries
@@ -135,7 +142,7 @@ fn object(name: &str) -> Object {
Object {
name: name.into(),
description: Some(format!("Object {name}")),
interfaces: vec!["catalog-v1".into(), "search-v2".into()],
interfaces: vec![reference("/", "catalog-v1"), reference("/", "search-v2")],
r#ref: Some("opaque-object-ref".into()),
validator: Some(vec![1, 2, 3]),
}
@@ -146,16 +153,19 @@ fn object_fields_have_canonical_identity_and_ordered_set_semantics() {
let object = object("article");
object.validate().unwrap();
object.validate_at_path("/articles/article").unwrap();
assert_eq!(object.interfaces, ["catalog-v1", "search-v2"]);
assert_eq!(
object.interfaces,
[reference("/", "catalog-v1"), reference("/", "search-v2")]
);
let duplicate = Object {
interfaces: vec!["catalog-v1".into(), "catalog-v1".into()],
interfaces: vec![reference("/", "catalog-v1"), reference("/", "catalog-v1")],
..object.clone()
};
assert_eq!(
duplicate.validate().unwrap_err().kind,
ValidationErrorKind::DuplicateInterface {
reference: "catalog-v1".into()
reference: reference("/", "catalog-v1")
}
);
@@ -574,7 +584,8 @@ fn calls_validate_canonical_paths_named_arguments_and_unit_results() {
validator: Some(vec![1]),
},
interface: InterfaceTarget {
reference: "catalog-v1".into(),
scope_ref: None,
reference: reference("/", "catalog-v1"),
validator: Some(vec![2]),
},
operation: "publish".into(),
@@ -659,17 +670,18 @@ fn observe_and_fetch_interface_dtos_validate_request_response_correspondence() {
}
let request = FetchInterfaceRequest {
interface: "catalog-v1".into(),
interface: reference("/", "catalog-v1"),
};
let response = FetchInterfaceResponse {
interface: "catalog-v1".into(),
scope_ref: None,
interface: reference("/", "catalog-v1"),
descriptor: descriptor(),
validator: Some(vec![9]),
};
response.validate_for(&request).unwrap();
assert!(matches!(
FetchInterfaceResponse {
interface: "other".into(),
interface: reference("/", "other"),
..response
}
.validate_for(&request)
@@ -847,7 +859,7 @@ fn invalid_success_payloads_remain_client_local_validation_failures() {
));
let interface_request = FetchInterfaceRequest {
interface: "catalog-v1".into(),
interface: reference("/", "catalog-v1"),
};
let mut invalid_descriptor = descriptor();
invalid_descriptor
@@ -856,6 +868,7 @@ fn invalid_success_payloads_remain_client_local_validation_failures() {
assert!(matches!(
FetchInterfaceResponse {
interface: interface_request.interface.clone(),
scope_ref: None,
descriptor: invalid_descriptor,
validator: None,
}