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
52 changes: 51 additions & 1 deletion crates/harness/src/prime/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,16 @@ impl PrimeServerCapabilities {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(super) enum OwnedSessionCleanupStatus {
Active,
Stopping,
Settled,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub(super) enum BridgeResponse {
#[serde(rename_all = "camelCase")]
Loaded {
Expand All @@ -107,6 +115,12 @@ pub(super) enum BridgeResponse {
protocol_version: u64,
capabilities: Vec<String>,
},
#[serde(rename_all = "camelCase")]
OwnedSessionCleanup {
v: u8,
id: u64,
status: OwnedSessionCleanupStatus,
},
Shutdown {
v: u8,
id: u64,
Expand All @@ -124,6 +138,7 @@ impl BridgeResponse {
let (version, id) = match self {
Self::Loaded { v, id, .. }
| Self::Ready { v, id, .. }
| Self::OwnedSessionCleanup { v, id, .. }
| Self::Shutdown { v, id, .. }
| Self::Error { v, id, .. } => (*v, *id),
};
Expand Down Expand Up @@ -193,4 +208,39 @@ mod tests {
};
assert!(response.validate_meta(1).is_err());
}

#[test]
fn cleanup_status_is_bounded_and_metadata_is_correlated() {
for (wire, expected) in [
("active", OwnedSessionCleanupStatus::Active),
("stopping", OwnedSessionCleanupStatus::Stopping),
("settled", OwnedSessionCleanupStatus::Settled),
] {
let bytes = format!(
r#"{{"v":{CONTROL_VERSION},"id":7,"kind":"owned-session-cleanup","status":"{wire}"}}"#
);
let response: BridgeResponse = serde_json::from_str(&bytes).unwrap();
assert!(response.validate_meta(7).is_ok());
assert!(matches!(
response,
BridgeResponse::OwnedSessionCleanup { status, .. } if status == expected
));
}

let unknown = format!(
r#"{{"v":{CONTROL_VERSION},"id":7,"kind":"owned-session-cleanup","status":"/private/native-id"}}"#
);
assert!(serde_json::from_str::<BridgeResponse>(&unknown).is_err());
let injected = format!(
r#"{{"v":{CONTROL_VERSION},"id":7,"kind":"owned-session-cleanup","status":"settled","raw":"/private/native-id"}}"#
);
assert!(serde_json::from_str::<BridgeResponse>(&injected).is_err());

let response = BridgeResponse::OwnedSessionCleanup {
v: CONTROL_VERSION,
id: 7,
status: OwnedSessionCleanupStatus::Settled,
};
assert!(response.validate_meta(8).is_err());
}
}
37 changes: 37 additions & 0 deletions crates/harness/src/prime/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ pub enum PrimeDaemonError {
},
#[error("Prime Agent bridge emitted an invalid control frame ({reason})")]
InvalidBridgeFrame { reason: &'static str },
#[error("Prime Agent session host emitted an invalid control frame ({reason})")]
InvalidSessionHostFrame { reason: &'static str },
#[error("Prime Agent session {stage} failed ({code})")]
Session {
stage: &'static str,
code: &'static str,
},
#[error("Prime Agent owned-session cleanup is uncertain")]
CleanupUncertain,
}

impl PrimeDaemonError {
Expand All @@ -51,3 +60,31 @@ impl From<PrimeDaemonError> for crate::HarnessError {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn session_errors_are_fixed_sanitized_protocol_failures() {
let errors = [
PrimeDaemonError::InvalidSessionHostFrame {
reason: "response kind is invalid",
},
PrimeDaemonError::Session {
stage: "attach",
code: "host-operation-failed",
},
PrimeDaemonError::CleanupUncertain,
];
for error in errors {
let message = error.to_string();
assert!(!message.contains("/private/"));
assert!(!message.contains("active-session"));
assert!(matches!(
crate::HarnessError::from(error),
crate::HarnessError::Protocol(_)
));
}
}
}
Loading
Loading