Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions pkgs/id/contract/shell-contract.v1.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"contract": "id-shell",
"version": 1,
"revision": 8,
"revision": 9,
"description": "The client-server frame protocol every id shell (terminal, web, native) speaks to a world host. Version 1 is the protocol behind the /id-world/1 ALPN. A shell sends only frames and fields named here; it ignores server frame types and fields it does not know. Additive changes bump `revision`; removing or changing a frame or field needs version 2 and a new ALPN. Event-producing frames must name their stable world-event contract here.",
"max_frame_bytes": 1048576,
"routing_fields": ["world"],
Expand Down Expand Up @@ -312,6 +312,16 @@
"scopes": "string[]",
"expires_at": "u64?"
}
},
"provider_offer": {
"class": "ephemeral",
"phases": ["joined"],
"fields": { "providers": "string[]" }
},
"provider_reply": {
"class": "ephemeral",
"phases": ["joined"],
"fields": { "nonce": "hex", "ok": "bool", "result_hex": "hex?", "error": "string?" }
}
},
"server_frames": {
Expand Down Expand Up @@ -463,6 +473,15 @@
"error": {
"class": "reply",
"fields": { "message": "string", "code": "string?" }
},
"provider_request": {
"class": "ephemeral",
"fields": {
"nonce": "hex",
"provider": { "type": "string", "max_bytes": 128 },
"args": "object",
"deadline_ms": "u64"
}
}
},
"projection": {
Expand All @@ -476,6 +495,6 @@
"presentation_tiers": "In-band terminal tier detection and negotiation (F13.4). Needs a new frame kind, so it waits for F11.12.",
"focus": "A focus frame for typed input (F13.4/F13.5). Not in version 1.",
"kitty_keyboard": "Kitty keyboard protocol key events (F13.5).",
"client_providers": "Client-side capability requests and replies (F13.7). Gated on F12.4 and F12.6."
"client_providers": "Client-side capability requests and replies (F13.7). The offer/request/reply frames landed in revision 9 (F13.7 step 2); the broker that sends requests is F13.7 step 3."
}
}
60 changes: 60 additions & 0 deletions pkgs/id/crates/id-worlds/src/shell_contract/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn server_frame_type(frame: &ServerFrame<'_>) -> &'static str {
ServerFrame::EventHistoryBegin { .. } => "event_history_begin",
ServerFrame::EventHistoryEvent { .. } => "event_history_event",
ServerFrame::Error { .. } => "error",
ServerFrame::ProviderRequest { .. } => "provider_request",
}
}

Expand Down Expand Up @@ -93,6 +94,7 @@ const SERVER_FRAME_TYPES: &[&str] = &[
"event_history_begin",
"event_history_event",
"error",
"provider_request",
];

#[test]
Expand Down Expand Up @@ -227,6 +229,12 @@ fn every_server_frame_variant_is_in_the_contract_and_nothing_else_is() {
message: "denied",
code: Some("actor_denied"),
},
ServerFrame::ProviderRequest {
nonce: "ab12",
provider: "client.file.read",
args: serde_json::json!({"path": "notes.txt"}),
deadline_ms: 5000,
},
ServerFrame::Revoked {
participant_id: 7,
revoked: true,
Expand Down Expand Up @@ -497,3 +505,55 @@ fn a_transcript_follows_the_session_phases() {
assert!(check_transcript(contract, "not json").is_err());
assert!(check_transcript(contract, r#"{"from": "peer", "frame": {}}"#).is_err());
}

#[test]
fn provider_frames_conform_only_where_the_contract_allows_them() {
let contract = contract();

// F13.7 step 2: the offer and the reply are client frames for a
// joined session only — a provider frame in the opening phase is
// a violation, so a session that has not joined can neither
// advertise a provider nor answer a request.
let offer = json!({"type": "provider_offer", "providers": ["client.file.read"]});
assert_eq!(
contract.check_client_frame(&offer, Phase::Joined).unwrap(),
"provider_offer"
);
assert!(contract.check_client_frame(&offer, Phase::Opening).is_err());
let parsed: ClientFrame = serde_json::from_value(offer).unwrap();
assert!(matches!(parsed, ClientFrame::ProviderOffer { .. }));

let reply =
json!({"type": "provider_reply", "nonce": "ab12", "ok": true, "result_hex": "00ff"});
assert_eq!(
contract.check_client_frame(&reply, Phase::Joined).unwrap(),
"provider_reply"
);
assert!(contract.check_client_frame(&reply, Phase::Query).is_err());
let parsed: ClientFrame = serde_json::from_value(reply).unwrap();
assert!(matches!(parsed, ClientFrame::ProviderReply { .. }));

// The request is a server frame; an undeclared field (a session
// or participant claim the design says never to trust from the
// wire) is a violation.
let request = json!({
"type": "provider_request",
"nonce": "ab12",
"provider": "client.file.read",
"args": {"path": "notes.txt"},
"deadline_ms": 5000
});
assert_eq!(
contract.check_server_frame(&request).unwrap(),
"provider_request"
);
let mut claimed = request.clone();
claimed["session"] = json!("someone-else");
assert!(contract.check_server_frame(&claimed).is_err());

// Nothing about the v1 frame set changed: every pre-existing
// client frame type is still in the contract (the sync test
// above pins the exact set both ways).
assert!(contract.client_frames.contains_key("join"));
assert!(contract.server_frames.contains_key("view"));
}
22 changes: 19 additions & 3 deletions pkgs/id/crates/id-worlds/src/world_session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ async fn run_world_session<I: SessionIo>(
| ClientFrame::Resize { .. }
| ClientFrame::Revoke { .. }
| ClientFrame::Paste { .. }
| ClientFrame::ProviderOffer { .. }
| ClientFrame::ProviderReply { .. }
| ClientFrame::FormResult { .. } => {
let _ = send_error(
service.frame_meter(),
Expand Down Expand Up @@ -1188,9 +1190,15 @@ async fn query_session<I: SessionIo>(
io.mark_authorized();
}
let store = documents_store.as_ref().or(service.blobs.as_ref());
if let Err(message) =
document_session(service.frame_meter(), service.world(), io, token, store, &name)
.await
if let Err(message) = document_session(
service.frame_meter(),
service.world(),
io,
token,
store,
&name,
)
.await
{
let _ = send_error(service.frame_meter(), io, message).await;
return;
Expand Down Expand Up @@ -2655,6 +2663,14 @@ async fn handle_client_frame<I: SessionIo>(
return send_error(service.frame_meter(), io, "invalid world frame").await;
};
let result = match frame {
ClientFrame::ProviderOffer { .. } | ClientFrame::ProviderReply { .. } => {
// F13.7 step 2 lands the frame types and the contract
// rules only: no broker exists yet (step 3), so an offer
// enables nothing and a reply correlates with nothing.
// Both are contract-valid in a joined session and are
// ignored here — fail-closed, and never a session error.
return true;
}
ClientFrame::Attenuate {
name,
scopes,
Expand Down
45 changes: 45 additions & 0 deletions pkgs/id/crates/id-worlds/src/world_session/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,34 @@ pub(crate) enum ClientFrame {
Revoke {
participant_id: u64,
},
/// F13.7 step 2: the shell advertises the client-side providers
/// it can serve for this session — only ones the local user has
/// opted into. An offer enables nothing by itself: the host
/// broker (step 3) may only request an offered provider, and
/// only when world policy also allows it. A shell that never
/// offers is a v1 shell for provider purposes and receives no
/// `provider_request` frames.
ProviderOffer {
/// Provider names, each under the `client.` namespace.
providers: Vec<String>,
},
/// F13.7 step 2: the shell's answer to one `provider_request`,
/// correlated by the host-minted `nonce`. Exactly one of
/// `result_hex` / `error` is meaningful, per `ok`. The reply is
/// ephemeral: only the broker's accepted result event is
/// journaled, never the reply itself.
ProviderReply {
/// The nonce from the request being answered, hex-encoded.
nonce: String,
/// Whether the local adapter executed the request.
ok: bool,
/// The bounded result bytes, hex-encoded, when `ok`.
#[serde(default, skip_serializing_if = "Option::is_none")]
result_hex: Option<String>,
/// A stable refusal code when not `ok`.
#[serde(default, skip_serializing_if = "Option::is_none")]
error: Option<String>,
},
}

/// Signer metadata supplied with an uploaded module. Public keys and
Expand All @@ -360,6 +388,23 @@ pub(crate) enum ServerFrame<'a> {
Event {
event: &'a WorldEvent,
},
/// F13.7 step 2: the host broker asks the originating session's
/// shell to execute one client-side provider. Sent only to a
/// session that advertised the provider in `provider_offer`;
/// shells that do not know this frame ignore it (the contract's
/// additive-revision rule), and a reply is accepted only for the
/// bound session and nonce (broker rules, step 3). Ephemeral:
/// the pending request is never journaled.
ProviderRequest {
/// Host-minted correlation nonce, hex-encoded.
nonce: &'a str,
/// The provider name, under the `client.` namespace.
provider: &'a str,
/// The request arguments, per the provider's manifest schema.
args: serde_json::Value,
/// The broker's deadline for the reply, in milliseconds.
deadline_ms: u64,
},
/// Acknowledges a deferred form answer; on field validation failure the
/// fresh token lets the trusted presenter show the pending form again.
FormResultStatus {
Expand Down