mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Strengthen code mode wire integer types
This commit is contained in:
@@ -11,6 +11,7 @@ use super::CapabilitySet;
|
||||
use super::ClientHello;
|
||||
use super::ClientToHost;
|
||||
use super::DelegateRequest;
|
||||
use super::DelegateRequestId;
|
||||
use super::DelegateResponse;
|
||||
use super::HandshakeRejectReason;
|
||||
use super::HostHello;
|
||||
@@ -18,6 +19,7 @@ use super::HostRequest;
|
||||
use super::HostResponse;
|
||||
use super::HostToClient;
|
||||
use super::ProtocolVersion;
|
||||
use super::RequestId;
|
||||
use super::SessionId;
|
||||
use super::SupportedProtocolVersions;
|
||||
use super::WireCellId;
|
||||
@@ -32,6 +34,7 @@ use super::WireToolKind;
|
||||
use super::WireToolName;
|
||||
use super::WireWaitOutcome;
|
||||
use super::WireWaitRequest;
|
||||
use crate::ExecuteRequest;
|
||||
|
||||
fn session_id() -> SessionId {
|
||||
SessionId::new("session-1").expect("valid session ID")
|
||||
@@ -41,6 +44,14 @@ fn cell_id(value: &str) -> WireCellId {
|
||||
WireCellId::new(value)
|
||||
}
|
||||
|
||||
fn request_id(value: i64) -> RequestId {
|
||||
RequestId::new(value)
|
||||
}
|
||||
|
||||
fn delegate_request_id(value: i64) -> DelegateRequestId {
|
||||
DelegateRequestId::new(value)
|
||||
}
|
||||
|
||||
fn capability(value: &str) -> Capability {
|
||||
Capability::new(value).expect("valid capability")
|
||||
}
|
||||
@@ -225,14 +236,14 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
let execute_request = execute_request();
|
||||
for (id, request, encoded_request) in [
|
||||
(
|
||||
1,
|
||||
request_id(1),
|
||||
HostRequest::OpenSession {
|
||||
session_id: session_id(),
|
||||
},
|
||||
json!({ "method": "session/open", "sessionId": "session-1" }),
|
||||
),
|
||||
(
|
||||
2,
|
||||
request_id(2),
|
||||
HostRequest::Execute {
|
||||
session_id: session_id(),
|
||||
request: execute_request,
|
||||
@@ -270,7 +281,7 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
}),
|
||||
),
|
||||
(
|
||||
3,
|
||||
request_id(3),
|
||||
HostRequest::Wait {
|
||||
session_id: session_id(),
|
||||
request: WireWaitRequest {
|
||||
@@ -285,7 +296,7 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
}),
|
||||
),
|
||||
(
|
||||
4,
|
||||
request_id(4),
|
||||
HostRequest::Terminate {
|
||||
session_id: session_id(),
|
||||
cell_id: cell_id("cell-1"),
|
||||
@@ -297,7 +308,7 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
}),
|
||||
),
|
||||
(
|
||||
5,
|
||||
request_id(5),
|
||||
HostRequest::ShutdownSession {
|
||||
session_id: session_id(),
|
||||
},
|
||||
@@ -316,7 +327,7 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
|
||||
for (id, result, encoded_result) in [
|
||||
(
|
||||
6,
|
||||
delegate_request_id(6),
|
||||
WireResult::Ok {
|
||||
value: DelegateResponse::ToolResult {
|
||||
result: json!({ "answer": 42 }),
|
||||
@@ -328,7 +339,7 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
}),
|
||||
),
|
||||
(
|
||||
7,
|
||||
delegate_request_id(7),
|
||||
WireResult::Ok {
|
||||
value: DelegateResponse::NotificationDelivered,
|
||||
},
|
||||
@@ -338,7 +349,7 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
}),
|
||||
),
|
||||
(
|
||||
8,
|
||||
delegate_request_id(8),
|
||||
WireResult::Err {
|
||||
message: "delegate failed".to_string(),
|
||||
},
|
||||
@@ -360,21 +371,21 @@ fn client_to_host_v1_variants_are_pinned() {
|
||||
fn host_to_client_v1_variants_are_pinned() {
|
||||
for (id, response, encoded_response) in [
|
||||
(
|
||||
1,
|
||||
request_id(1),
|
||||
HostResponse::SessionReady {
|
||||
session_id: session_id(),
|
||||
},
|
||||
json!({ "type": "session/ready", "sessionId": "session-1" }),
|
||||
),
|
||||
(
|
||||
2,
|
||||
request_id(2),
|
||||
HostResponse::ExecutionStarted {
|
||||
cell_id: cell_id("cell-1"),
|
||||
},
|
||||
json!({ "type": "execution/started", "cellId": "cell-1" }),
|
||||
),
|
||||
(
|
||||
3,
|
||||
request_id(3),
|
||||
HostResponse::WaitCompleted {
|
||||
outcome: WireWaitOutcome::LiveCell(WireRuntimeResponse::Yielded {
|
||||
cell_id: cell_id("cell-1"),
|
||||
@@ -394,7 +405,7 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
}),
|
||||
),
|
||||
(
|
||||
4,
|
||||
request_id(4),
|
||||
HostResponse::WaitCompleted {
|
||||
outcome: WireWaitOutcome::MissingCell(WireRuntimeResponse::Result {
|
||||
cell_id: cell_id("missing-cell"),
|
||||
@@ -416,7 +427,7 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
}),
|
||||
),
|
||||
(
|
||||
5,
|
||||
request_id(5),
|
||||
HostResponse::SessionClosed {
|
||||
session_id: session_id(),
|
||||
},
|
||||
@@ -437,7 +448,7 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
}
|
||||
assert_wire_round_trip(
|
||||
HostToClient::Response {
|
||||
id: 6,
|
||||
id: request_id(6),
|
||||
result: WireResult::Err {
|
||||
message: "operation failed".to_string(),
|
||||
},
|
||||
@@ -451,7 +462,7 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
|
||||
assert_wire_round_trip(
|
||||
HostToClient::InitialResponse {
|
||||
id: 7,
|
||||
id: request_id(7),
|
||||
result: WireResult::Ok {
|
||||
value: WireRuntimeResponse::Terminated {
|
||||
cell_id: cell_id("cell-1"),
|
||||
@@ -472,7 +483,7 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
);
|
||||
assert_wire_round_trip(
|
||||
HostToClient::InitialResponse {
|
||||
id: 8,
|
||||
id: request_id(8),
|
||||
result: WireResult::Err {
|
||||
message: "execution failed".to_string(),
|
||||
},
|
||||
@@ -486,7 +497,7 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
|
||||
assert_wire_round_trip(
|
||||
HostToClient::DelegateRequest {
|
||||
id: 9,
|
||||
id: delegate_request_id(9),
|
||||
session_id: session_id(),
|
||||
request: DelegateRequest::InvokeTool {
|
||||
invocation: WireNestedToolCall {
|
||||
@@ -522,7 +533,7 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
);
|
||||
assert_wire_round_trip(
|
||||
HostToClient::DelegateRequest {
|
||||
id: 10,
|
||||
id: delegate_request_id(10),
|
||||
session_id: session_id(),
|
||||
request: DelegateRequest::Notify {
|
||||
call_id: "call-1".to_string(),
|
||||
@@ -543,7 +554,9 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
}),
|
||||
);
|
||||
assert_wire_round_trip(
|
||||
HostToClient::CancelDelegateRequest { id: 11 },
|
||||
HostToClient::CancelDelegateRequest {
|
||||
id: delegate_request_id(11),
|
||||
},
|
||||
json!({ "type": "delegate/cancel", "id": 11 }),
|
||||
);
|
||||
assert_wire_round_trip(
|
||||
@@ -559,6 +572,30 @@ fn host_to_client_v1_variants_are_pinned() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn execute_request_integer_bounds_are_enforced() {
|
||||
let wire_request = execute_request();
|
||||
let domain_request = ExecuteRequest::try_from(wire_request.clone())
|
||||
.expect("valid wire request converts to the domain");
|
||||
assert_eq!(
|
||||
WireExecuteRequest::try_from(domain_request.clone())
|
||||
.expect("valid domain request converts to the wire"),
|
||||
wire_request
|
||||
);
|
||||
|
||||
let too_large = ExecuteRequest {
|
||||
max_output_tokens: Some(usize::try_from(i32::MAX).expect("i32::MAX fits usize") + 1),
|
||||
..domain_request
|
||||
};
|
||||
assert!(WireExecuteRequest::try_from(too_large).is_err());
|
||||
|
||||
let negative = WireExecuteRequest {
|
||||
max_output_tokens: Some(-1),
|
||||
..wire_request
|
||||
};
|
||||
assert!(ExecuteRequest::try_from(negative).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_protocol_states_cannot_be_constructed_or_decoded() {
|
||||
assert!(SessionId::new("").is_err());
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::num::TryFromIntError;
|
||||
|
||||
use codex_protocol::ToolName;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
@@ -142,30 +144,34 @@ pub struct WireExecuteRequest {
|
||||
pub enabled_tools: Vec<WireToolDefinition>,
|
||||
pub source: String,
|
||||
pub yield_time_ms: Option<u64>,
|
||||
pub max_output_tokens: Option<usize>,
|
||||
pub max_output_tokens: Option<i32>,
|
||||
}
|
||||
|
||||
impl From<ExecuteRequest> for WireExecuteRequest {
|
||||
fn from(value: ExecuteRequest) -> Self {
|
||||
Self {
|
||||
impl TryFrom<ExecuteRequest> for WireExecuteRequest {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
fn try_from(value: ExecuteRequest) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
tool_call_id: value.tool_call_id,
|
||||
enabled_tools: value.enabled_tools.into_iter().map(Into::into).collect(),
|
||||
source: value.source,
|
||||
yield_time_ms: value.yield_time_ms,
|
||||
max_output_tokens: value.max_output_tokens,
|
||||
}
|
||||
max_output_tokens: value.max_output_tokens.map(i32::try_from).transpose()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<WireExecuteRequest> for ExecuteRequest {
|
||||
fn from(value: WireExecuteRequest) -> Self {
|
||||
Self {
|
||||
impl TryFrom<WireExecuteRequest> for ExecuteRequest {
|
||||
type Error = TryFromIntError;
|
||||
|
||||
fn try_from(value: WireExecuteRequest) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
tool_call_id: value.tool_call_id,
|
||||
enabled_tools: value.enabled_tools.into_iter().map(Into::into).collect(),
|
||||
source: value.source,
|
||||
yield_time_ms: value.yield_time_ms,
|
||||
max_output_tokens: value.max_output_tokens,
|
||||
}
|
||||
max_output_tokens: value.max_output_tokens.map(usize::try_from).transpose()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,27 @@ use serde::Serialize;
|
||||
use serde::Serializer;
|
||||
use serde::de::Error as _;
|
||||
|
||||
pub type RequestId = u64;
|
||||
pub type DelegateRequestId = u64;
|
||||
/// Correlates one client operation request with the host's response.
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct RequestId(i64);
|
||||
|
||||
impl RequestId {
|
||||
pub const fn new(value: i64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// Correlates one host delegate request with the client's response.
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct DelegateRequestId(i64);
|
||||
|
||||
impl DelegateRequestId {
|
||||
pub const fn new(value: i64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||
#[serde(transparent)]
|
||||
|
||||
Reference in New Issue
Block a user