| 1 | use std::collections::BTreeMap; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | use serde::{Deserialize, Serialize}; |
| 5 | use serde_json::Value; |
| 6 | |
| 7 | pub mod watch_event; |
| 8 | |
| 9 | pub const RUNTIME_EVENT_ENVELOPE_SCHEMA_VERSION: u32 = 1; |
| 10 | pub const RUNTIME_API_VERSION: &str = "1.0"; |
| 11 | |
| 12 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 13 | #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] |
| 14 | #[serde(rename_all = "snake_case")] |
| 15 | pub enum FleetProfileDocumentKind { |
| 16 | Profile, |
| 17 | Fleet, |
| 18 | } |
| 19 | |
| 20 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 21 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 22 | #[serde(deny_unknown_fields)] |
| 23 | pub struct FleetProfileDocument { |
| 24 | pub id: String, |
| 25 | pub kind: FleetProfileDocumentKind, |
| 26 | pub origin: String, |
| 27 | pub path: Option<String>, |
| 28 | pub text: String, |
| 29 | pub revision: String, |
| 30 | pub writable: bool, |
| 31 | pub refusal_reason: Option<String>, |
| 32 | } |
| 33 | |
| 34 | /// Maximum JSON input (including base64 expansion) for an image turn. |
| 35 | pub const MAX_RUNTIME_IMAGE_BODY_BYTES: usize = 8 * 1024 * 1024; |
| 36 | pub const MAX_RUNTIME_IMAGES: usize = 10; |
| 37 | pub const MAX_RUNTIME_IMAGE_BYTES: usize = 4 * 1024 * 1024; |
| 38 | pub const MAX_RUNTIME_IMAGE_TOTAL_BYTES: usize = 5 * 1024 * 1024; |
| 39 | |
| 40 | /// Inline bytes only: neither host paths nor remote URLs confer attachment authority. |
| 41 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 42 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 43 | #[serde(rename_all = "camelCase", deny_unknown_fields)] |
| 44 | pub struct RuntimeImageInput { |
| 45 | pub mime: String, |
| 46 | pub data_base64: String, |
| 47 | } |
| 48 | |
| 49 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 50 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 51 | pub struct RuntimeEventEnvelope { |
| 52 | #[serde(default = "default_runtime_event_envelope_schema_version")] |
| 53 | pub schema_version: u32, |
| 54 | pub seq: u64, |
| 55 | pub event: String, |
| 56 | pub kind: String, |
| 57 | pub thread_id: String, |
| 58 | pub turn_id: Option<String>, |
| 59 | pub item_id: Option<String>, |
| 60 | pub timestamp: String, |
| 61 | #[cfg_attr( |
| 62 | feature = "schema", |
| 63 | schemars(default, schema_with = "optional_string_schema") |
| 64 | )] |
| 65 | #[serde(skip_serializing_if = "Option::is_none")] |
| 66 | pub created_at: Option<String>, |
| 67 | pub payload: Value, |
| 68 | #[serde(default)] |
| 69 | #[serde(flatten)] |
| 70 | pub extra: BTreeMap<String, Value>, |
| 71 | } |
| 72 | |
| 73 | fn default_runtime_event_envelope_schema_version() -> u32 { |
| 74 | RUNTIME_EVENT_ENVELOPE_SCHEMA_VERSION |
| 75 | } |
| 76 | |
| 77 | #[cfg(feature = "schema")] |
| 78 | fn optional_string_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema { |
| 79 | if generator.contract().is_serialize() { |
| 80 | generator.subschema_for::<String>() |
| 81 | } else { |
| 82 | generator.subschema_for::<Option<String>>() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // --------------------------------------------------------------------------- |
| 87 | // Capability advertisement |
| 88 | // --------------------------------------------------------------------------- |
| 89 | |
| 90 | /// Fixed capability map advertised by `GET /v1/runtime/info`. |
| 91 | /// |
| 92 | /// All fields are required on serialization so clients can rely on the shape. |
| 93 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 94 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 95 | pub struct RuntimeCapabilities { |
| 96 | /// Device client tokens have immutable watch/drive intent. Watch cannot |
| 97 | /// mutate Runtime state, acquire control, or forward display input. |
| 98 | #[serde(default)] |
| 99 | pub client_token_intents: bool, |
| 100 | #[serde(default)] |
| 101 | pub account_session: bool, |
| 102 | #[serde(default)] |
| 103 | pub account_model_owner: bool, |
| 104 | #[serde(default)] |
| 105 | pub session_provider_keys: bool, |
| 106 | pub threads: bool, |
| 107 | #[serde(default)] |
| 108 | pub thread_graph: bool, |
| 109 | #[serde(default)] |
| 110 | pub thread_create_idempotency: bool, |
| 111 | #[serde(default)] |
| 112 | pub exact_route_thread_create: bool, |
| 113 | #[serde(default)] |
| 114 | pub thread_idle_precondition: bool, |
| 115 | #[serde(default)] |
| 116 | pub thread_patch_undo_turn_id: bool, |
| 117 | #[serde(default)] |
| 118 | pub thread_model_hotswitch: bool, |
| 119 | #[serde(default)] |
| 120 | pub thread_scoped_reads: bool, |
| 121 | /// Git stage, unstage, discard, commit, push and branch accept |
| 122 | /// `?thread_id=` and act on that thread's workspace. |
| 123 | #[serde(default)] |
| 124 | pub thread_scoped_git_writes: bool, |
| 125 | /// `PUT /v1/workspace/files` accepts `?thread_id=` and writes into that |
| 126 | /// thread's workspace with the same confinement and revision rules. |
| 127 | #[serde(default)] |
| 128 | pub thread_scoped_file_writes: bool, |
| 129 | /// `GET /v1/snapshots` and `POST /v1/snapshots/{id}/restore` accept |
| 130 | /// `?thread_id=` and act on that thread's workspace. |
| 131 | #[serde(default)] |
| 132 | pub thread_scoped_snapshots: bool, |
| 133 | /// `GET /v1/threads/{id}/terminals` lists the thread's live shells, and |
| 134 | /// `/v1/threads/{id}/terminals/{terminal_id}/output|input|resize|kill` |
| 135 | /// drive one by the identity the Engine issued it. |
| 136 | #[serde(default)] |
| 137 | pub thread_scoped_terminals: bool, |
| 138 | /// `GET`/`POST /v1/worktrees` and `POST /v1/worktrees/remove`. |
| 139 | #[serde(default)] |
| 140 | pub worktrees: bool, |
| 141 | #[serde(default)] |
| 142 | pub job_operations: bool, |
| 143 | /// Explicit per-thread shell opt-in is checked against loaded policy and |
| 144 | /// cannot broaden a conversation while it has an active turn. |
| 145 | #[serde(default)] |
| 146 | pub thread_shell_consent: bool, |
| 147 | #[serde(default)] |
| 148 | pub thread_standing_context: bool, |
| 149 | #[serde(default)] |
| 150 | pub workspace_customizations: bool, |
| 151 | pub turns: bool, |
| 152 | /// `POST /v1/threads/{id}/turns` accepts a durable, thread-scoped |
| 153 | /// `operation_key` and returns the original turn for exact retries. |
| 154 | #[serde(default)] |
| 155 | pub turn_operation_idempotency: bool, |
| 156 | /// Read-only exact accepted-turn lookup by thread and operation key. |
| 157 | #[serde(default)] |
| 158 | pub turn_operation_lookup: bool, |
| 159 | #[serde(default)] |
| 160 | pub watch_event_turns: bool, |
| 161 | #[serde(default)] |
| 162 | pub structured_review_turns: bool, |
| 163 | #[serde(default)] |
| 164 | pub structured_review_version: u32, |
| 165 | /// Automation patch/run/pause/resume honor `expected_revision` (409 |
| 166 | /// `revision_conflict` carrying `current_revision`); create/patch/run/pause/ |
| 167 | /// resume honor a bounded, persisted `Idempotency-Key`. Delete honors |
| 168 | /// neither. |
| 169 | #[serde(default)] |
| 170 | pub automation_mutation_preconditions: bool, |
| 171 | /// Bounded inline image inputs, persisted and replayed with their turn. |
| 172 | #[serde(default)] |
| 173 | pub turn_image_inputs: bool, |
| 174 | /// Per-turn maxOutputTokens is validated and intersected with the route ceiling. |
| 175 | #[serde(default)] |
| 176 | pub turn_output_token_limit: bool, |
| 177 | /// Account profile snapshots and Engine-owned constitution preview. |
| 178 | #[serde(default)] |
| 179 | pub profile_constitution: bool, |
| 180 | pub turn_steer: bool, |
| 181 | pub turn_interrupt: bool, |
| 182 | pub event_replay: bool, |
| 183 | pub external_tools: bool, |
| 184 | pub environments: bool, |
| 185 | pub worker_runtime: bool, |
| 186 | #[serde(default)] |
| 187 | pub fleet_run_create: bool, |
| 188 | #[serde(default)] |
| 189 | pub fleet_worker_provider_pins: bool, |
| 190 | #[serde(default)] |
| 191 | pub fleet_mission_board: bool, |
| 192 | #[serde(default)] |
| 193 | pub fleet_worker_model_pool: bool, |
| 194 | #[serde(default)] |
| 195 | pub thread_fleet_members: bool, |
| 196 | /// Member starts are durable per `(store, thread, operation_key)`; |
| 197 | /// `GET /v1/threads/{id}/members/operations/{operation_key}` reads them. |
| 198 | #[serde(default)] |
| 199 | pub thread_fleet_member_durable_operations_v1: bool, |
| 200 | #[serde(default)] |
| 201 | pub fleet_run_start: bool, |
| 202 | #[serde(default)] |
| 203 | pub fleet_event_replay: bool, |
| 204 | #[serde(default)] |
| 205 | pub fleet_event_stream: bool, |
| 206 | #[serde(default)] |
| 207 | pub fleet_local_target: bool, |
| 208 | #[serde(default)] |
| 209 | pub fleet_worker_thread_executor: bool, |
| 210 | /// `GET/PUT/DELETE /v1/threads/{id}/goal` and the `complete`/`block` |
| 211 | /// lifecycle actions are available. |
| 212 | #[serde(default)] |
| 213 | pub thread_goals: bool, |
| 214 | /// `GET /v1/memory` and `GET /v1/memory/{id}` are available for |
| 215 | /// bounded inspection of the native memory store. `POST /v1/memory` |
| 216 | /// and `DELETE /v1/memory` are also available (auth-gated via the |
| 217 | /// standard route layer) for lifecycle controls. |
| 218 | #[serde(default)] |
| 219 | pub memory: bool, |
| 220 | /// Whether the runtime supports create/update/enable/disable/reconnect/delete |
| 221 | /// operations on MCP server configuration via the `POST|GET|PATCH|DELETE |
| 222 | /// /v1/apps/mcp/servers` family of endpoints. |
| 223 | #[serde(default)] |
| 224 | pub mcp_server_management: bool, |
| 225 | /// Skill lifecycle operations (install, update, uninstall, trust, audit) |
| 226 | /// are available via the HTTP API. |
| 227 | #[serde(default)] |
| 228 | pub skill_lifecycle: bool, |
| 229 | /// `GET /v1/skills/{name}` returns one skill's full body and routing |
| 230 | /// metadata, so a client can compose an activation instruction for its |
| 231 | /// next turn. `GET /v1/skills` rows also carry `invocation`, `aliases`, |
| 232 | /// and `bundled_tier` routing fields when this flag is set. |
| 233 | #[serde(default)] |
| 234 | pub skill_detail: bool, |
| 235 | /// Plugin bundle and marketplace lifecycle operations (list/detail, |
| 236 | /// install/update/uninstall, trust/enable/disable/revoke, marketplace |
| 237 | /// add/remove/install) are available via the `/v1/apps/plugins` and |
| 238 | /// `/v1/apps/marketplaces` endpoint families. |
| 239 | #[serde(default)] |
| 240 | pub plugin_management: bool, |
| 241 | /// Durable, workspace-scoped cross-task Agent Mail endpoints and events. |
| 242 | #[serde(default)] |
| 243 | pub agent_mail: bool, |
| 244 | /// `GET /v1/threads/{id}/events` puts the durable `seq` on every journal |
| 245 | /// frame as the SSE `id:` and resumes from a `Last-Event-ID` header, so a |
| 246 | /// browser `EventSource` reconnects without a cursor in the query string. |
| 247 | #[serde(default)] |
| 248 | pub event_stream_resume: bool, |
| 249 | /// `GET /v1/permissions/rules` lists the user `permissions.toml` rules |
| 250 | /// with per-rule removal tokens, and `POST /v1/permissions/rules/remove` |
| 251 | /// removes one for an owner caller. |
| 252 | #[serde(default)] |
| 253 | pub permission_rules: bool, |
| 254 | /// `POST /v1/approvals/{id}` accepts `remember_scope: "workspace"`, which |
| 255 | /// saves the exact repo-scoped allow rule to `permissions.toml`. |
| 256 | #[serde(default)] |
| 257 | pub approval_remember_workspace: bool, |
| 258 | /// `GET /v1/approval-grants` lists active and suspended grants across |
| 259 | /// threads. |
| 260 | #[serde(default)] |
| 261 | pub approval_grants_list: bool, |
| 262 | #[serde(default)] |
| 263 | pub search_consent: bool, |
| 264 | #[serde(default)] |
| 265 | pub worker_wake_and_revive: bool, |
| 266 | #[serde(default)] |
| 267 | pub fleet_worker_model_hotswitch: bool, |
| 268 | } |
| 269 | |
| 270 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 271 | #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] |
| 272 | #[serde(rename_all = "snake_case")] |
| 273 | pub enum ApprovalKind { |
| 274 | ThirdPartySearch, |
| 275 | } |
| 276 | |
| 277 | /// Experimental opt-in flags advertised by `GET /v1/runtime/info`. |
| 278 | /// |
| 279 | /// Fields are additive and default to `false` when omitted by older servers. |
| 280 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 281 | #[derive(Debug, Clone, Default, Serialize, Deserialize)] |
| 282 | pub struct RuntimeExperimentalCapabilities { |
| 283 | #[serde(default)] |
| 284 | pub environments: bool, |
| 285 | } |
| 286 | |
| 287 | // --------------------------------------------------------------------------- |
| 288 | // External Tool Bridge protocol types |
| 289 | // --------------------------------------------------------------------------- |
| 290 | |
| 291 | /// Specification for a dynamic external tool registered by a runtime client. |
| 292 | /// |
| 293 | /// Example JSON from the spec: |
| 294 | /// |
| 295 | /// ```json |
| 296 | /// { |
| 297 | /// "namespace": "tau_bench", |
| 298 | /// "name": "get_reservation", |
| 299 | /// "description": "Look up an airline reservation.", |
| 300 | /// "input_schema": { |
| 301 | /// "type": "object", |
| 302 | /// "properties": { |
| 303 | /// "reservation_id": { "type": "string" } |
| 304 | /// }, |
| 305 | /// "required": ["reservation_id"], |
| 306 | /// "additionalProperties": false |
| 307 | /// } |
| 308 | /// } |
| 309 | /// ``` |
| 310 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 311 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 312 | pub struct DynamicToolSpec { |
| 313 | /// Optional namespace that groups related tools (e.g. `"tau_bench"`). |
| 314 | /// When present, the runtime may expose the tool as |
| 315 | /// `<namespace>::<name>` to the model. |
| 316 | #[cfg_attr( |
| 317 | feature = "schema", |
| 318 | schemars(default, schema_with = "optional_string_schema") |
| 319 | )] |
| 320 | #[serde(skip_serializing_if = "Option::is_none")] |
| 321 | pub namespace: Option<String>, |
| 322 | |
| 323 | /// Short tool name. Combined with `namespace` it forms a unique tool id. |
| 324 | pub name: String, |
| 325 | |
| 326 | /// Human-readable description exposed to the model. |
| 327 | pub description: String, |
| 328 | |
| 329 | /// JSON Schema describing the tool's input parameters. |
| 330 | pub input_schema: Value, |
| 331 | |
| 332 | /// If true, the runtime may defer schema validation / tool loading until |
| 333 | /// the model actually calls the tool. |
| 334 | /// |
| 335 | /// Defaults to `false` so that older clients omitting this field still |
| 336 | /// behave the same way. |
| 337 | #[serde(default)] |
| 338 | pub defer_loading: bool, |
| 339 | } |
| 340 | |
| 341 | /// Lifecycle status of a dynamic tool item shown in thread detail and event |
| 342 | /// payloads. |
| 343 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 344 | #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] |
| 345 | #[serde(rename_all = "snake_case")] |
| 346 | pub enum DynamicToolItemStatus { |
| 347 | InProgress, |
| 348 | Completed, |
| 349 | Failed, |
| 350 | } |
| 351 | |
| 352 | /// Parameters identifying a dynamic tool call request emitted by the runtime. |
| 353 | /// |
| 354 | /// This is the typed payload for `tool_call.requested` events and also the |
| 355 | /// natural identifier used when the runtime looks up a pending call. |
| 356 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 357 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 358 | pub struct DynamicToolCallParams { |
| 359 | pub thread_id: String, |
| 360 | pub turn_id: String, |
| 361 | pub call_id: String, |
| 362 | |
| 363 | /// Optional namespace that was registered with the tool. |
| 364 | #[cfg_attr( |
| 365 | feature = "schema", |
| 366 | schemars(default, schema_with = "optional_string_schema") |
| 367 | )] |
| 368 | #[serde(skip_serializing_if = "Option::is_none")] |
| 369 | pub namespace: Option<String>, |
| 370 | |
| 371 | /// Tool name that the model invoked. |
| 372 | pub tool: String, |
| 373 | |
| 374 | /// Arguments supplied by the model, validated against `input_schema`. |
| 375 | pub arguments: Value, |
| 376 | } |
| 377 | |
| 378 | /// Result submitted by a runtime client after executing a dynamic tool. |
| 379 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 380 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 381 | pub struct DynamicToolCallResult { |
| 382 | /// Whether the client-side tool execution succeeded. |
| 383 | pub success: bool, |
| 384 | |
| 385 | /// Content fragments returned by the tool. |
| 386 | /// |
| 387 | /// Defaults to an empty vector when omitted so clients can send a minimal |
| 388 | /// `{ "success": false }` payload. |
| 389 | #[serde(default)] |
| 390 | pub content: Vec<DynamicToolCallContent>, |
| 391 | } |
| 392 | |
| 393 | /// A single content fragment inside a [`DynamicToolCallResult`]. |
| 394 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 395 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 396 | #[serde(tag = "type", rename_all = "snake_case")] |
| 397 | pub enum DynamicToolCallContent { |
| 398 | InputText { text: String }, |
| 399 | InputImage { image_url: String }, |
| 400 | } |
| 401 | |
| 402 | // --------------------------------------------------------------------------- |
| 403 | // Environment targeting protocol types |
| 404 | // --------------------------------------------------------------------------- |
| 405 | |
| 406 | /// Environment target selected for a turn's shell/filesystem work. |
| 407 | /// |
| 408 | /// Example JSON: |
| 409 | /// |
| 410 | /// ```json |
| 411 | /// { |
| 412 | /// "environment_id": "local", |
| 413 | /// "cwd": "/workspace" |
| 414 | /// } |
| 415 | /// ``` |
| 416 | #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] |
| 417 | #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
| 418 | pub struct TurnEnvironmentParams { |
| 419 | pub environment_id: String, |
| 420 | pub cwd: PathBuf, |
| 421 | } |
| 422 | |
| 423 | #[cfg(test)] |
| 424 | mod tests { |
| 425 | use super::*; |
| 426 | use serde_json::json; |
| 427 | |
| 428 | #[test] |
| 429 | fn dynamic_tool_spec_roundtrip() { |
| 430 | let spec = DynamicToolSpec { |
| 431 | namespace: Some("tau_bench".into()), |
| 432 | name: "get_reservation".into(), |
| 433 | description: "Look up an airline reservation.".into(), |
| 434 | input_schema: json!({ |
| 435 | "type": "object", |
| 436 | "properties": { |
| 437 | "reservation_id": { "type": "string" } |
| 438 | }, |
| 439 | "required": ["reservation_id"], |
| 440 | "additionalProperties": false |
| 441 | }), |
| 442 | defer_loading: false, |
| 443 | }; |
| 444 | |
| 445 | let serialized = serde_json::to_string(&spec).unwrap(); |
| 446 | let deserialized: DynamicToolSpec = serde_json::from_str(&serialized).unwrap(); |
| 447 | assert_eq!(spec, deserialized); |
| 448 | } |
| 449 | |
| 450 | #[test] |
| 451 | fn dynamic_tool_spec_omits_defer_loading_defaults_false() { |
| 452 | let json = r#"{ |
| 453 | "namespace": "tau_bench", |
| 454 | "name": "get_reservation", |
| 455 | "description": "Look up an airline reservation.", |
| 456 | "input_schema": { "type": "object" } |
| 457 | }"#; |
| 458 | |
| 459 | let spec: DynamicToolSpec = serde_json::from_str(json).unwrap(); |
| 460 | assert_eq!(spec.namespace, Some("tau_bench".into())); |
| 461 | assert_eq!(spec.name, "get_reservation"); |
| 462 | assert!(!spec.defer_loading); |
| 463 | } |
| 464 | |
| 465 | #[test] |
| 466 | fn dynamic_tool_item_status_snake_case() { |
| 467 | assert_eq!( |
| 468 | serde_json::to_string(&DynamicToolItemStatus::InProgress).unwrap(), |
| 469 | "\"in_progress\"" |
| 470 | ); |
| 471 | assert_eq!( |
| 472 | serde_json::from_str::<DynamicToolItemStatus>("\"completed\"").unwrap(), |
| 473 | DynamicToolItemStatus::Completed |
| 474 | ); |
| 475 | assert_eq!( |
| 476 | serde_json::from_str::<DynamicToolItemStatus>("\"failed\"").unwrap(), |
| 477 | DynamicToolItemStatus::Failed |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | #[test] |
| 482 | fn dynamic_tool_call_params_roundtrip() { |
| 483 | let params = DynamicToolCallParams { |
| 484 | thread_id: "thr_123".into(), |
| 485 | turn_id: "turn_456".into(), |
| 486 | call_id: "call_abc".into(), |
| 487 | namespace: Some("tau_bench".into()), |
| 488 | tool: "get_reservation".into(), |
| 489 | arguments: json!({ "reservation_id": "ABC123" }), |
| 490 | }; |
| 491 | |
| 492 | let serialized = serde_json::to_string(¶ms).unwrap(); |
| 493 | let deserialized: DynamicToolCallParams = serde_json::from_str(&serialized).unwrap(); |
| 494 | assert_eq!(params, deserialized); |
| 495 | } |
| 496 | |
| 497 | #[test] |
| 498 | fn dynamic_tool_call_content_roundtrip() { |
| 499 | let content = vec![ |
| 500 | DynamicToolCallContent::InputText { |
| 501 | text: "{\"status\":\"confirmed\"}".into(), |
| 502 | }, |
| 503 | DynamicToolCallContent::InputImage { |
| 504 | image_url: "http://example.com/receipt.png".into(), |
| 505 | }, |
| 506 | ]; |
| 507 | |
| 508 | let value = serde_json::to_value(&content).unwrap(); |
| 509 | let deserialized: Vec<DynamicToolCallContent> = serde_json::from_value(value).unwrap(); |
| 510 | assert_eq!(content, deserialized); |
| 511 | |
| 512 | // Verify the exact JSON tag names expected by the spec. |
| 513 | assert_eq!( |
| 514 | serde_json::to_string(&DynamicToolCallContent::InputText { text: "x".into() }).unwrap(), |
| 515 | r#"{"type":"input_text","text":"x"}"# |
| 516 | ); |
| 517 | assert_eq!( |
| 518 | serde_json::to_string(&DynamicToolCallContent::InputImage { |
| 519 | image_url: "y".into() |
| 520 | }) |
| 521 | .unwrap(), |
| 522 | r#"{"type":"input_image","image_url":"y"}"# |
| 523 | ); |
| 524 | } |
| 525 | |
| 526 | #[test] |
| 527 | fn dynamic_tool_call_result_defaults_empty_content() { |
| 528 | let json = r#"{ "success": false }"#; |
| 529 | let result: DynamicToolCallResult = serde_json::from_str(json).unwrap(); |
| 530 | assert!(!result.success); |
| 531 | assert!(result.content.is_empty()); |
| 532 | } |
| 533 | |
| 534 | #[test] |
| 535 | fn dynamic_tool_call_result_roundtrip_with_content() { |
| 536 | let result = DynamicToolCallResult { |
| 537 | success: true, |
| 538 | content: vec![DynamicToolCallContent::InputText { |
| 539 | text: "done".into(), |
| 540 | }], |
| 541 | }; |
| 542 | |
| 543 | let serialized = serde_json::to_string(&result).unwrap(); |
| 544 | let deserialized: DynamicToolCallResult = serde_json::from_str(&serialized).unwrap(); |
| 545 | assert_eq!(result, deserialized); |
| 546 | } |
| 547 | |
| 548 | #[test] |
| 549 | fn turn_environment_params_roundtrip() { |
| 550 | let env = TurnEnvironmentParams { |
| 551 | environment_id: "local".into(), |
| 552 | cwd: PathBuf::from("/workspace"), |
| 553 | }; |
| 554 | |
| 555 | let serialized = serde_json::to_string(&env).unwrap(); |
| 556 | let deserialized: TurnEnvironmentParams = serde_json::from_str(&serialized).unwrap(); |
| 557 | assert_eq!(env, deserialized); |
| 558 | |
| 559 | // Verify JSON from the spec deserializes directly. |
| 560 | let from_spec = r#"{ |
| 561 | "environment_id": "local", |
| 562 | "cwd": "/workspace" |
| 563 | }"#; |
| 564 | let parsed: TurnEnvironmentParams = serde_json::from_str(from_spec).unwrap(); |
| 565 | assert_eq!(parsed.environment_id, "local"); |
| 566 | assert_eq!(parsed.cwd, PathBuf::from("/workspace")); |
| 567 | } |
| 568 | |
| 569 | #[test] |
| 570 | fn runtime_capabilities_serializes_expected_shape() { |
| 571 | let caps = RuntimeCapabilities { |
| 572 | client_token_intents: true, |
| 573 | turn_output_token_limit: false, |
| 574 | profile_constitution: false, |
| 575 | account_session: true, |
| 576 | account_model_owner: false, |
| 577 | session_provider_keys: false, |
| 578 | threads: true, |
| 579 | thread_graph: true, |
| 580 | thread_create_idempotency: true, |
| 581 | exact_route_thread_create: true, |
| 582 | thread_idle_precondition: true, |
| 583 | thread_patch_undo_turn_id: true, |
| 584 | thread_model_hotswitch: true, |
| 585 | thread_scoped_reads: true, |
| 586 | thread_scoped_git_writes: true, |
| 587 | thread_scoped_file_writes: true, |
| 588 | thread_scoped_snapshots: true, |
| 589 | thread_scoped_terminals: true, |
| 590 | worktrees: true, |
| 591 | job_operations: true, |
| 592 | thread_shell_consent: true, |
| 593 | thread_standing_context: true, |
| 594 | workspace_customizations: true, |
| 595 | turns: true, |
| 596 | turn_operation_idempotency: true, |
| 597 | turn_operation_lookup: true, |
| 598 | watch_event_turns: true, |
| 599 | structured_review_turns: true, |
| 600 | structured_review_version: 2, |
| 601 | automation_mutation_preconditions: true, |
| 602 | turn_image_inputs: true, |
| 603 | turn_steer: true, |
| 604 | turn_interrupt: true, |
| 605 | event_replay: true, |
| 606 | external_tools: false, |
| 607 | environments: false, |
| 608 | worker_runtime: false, |
| 609 | fleet_run_create: true, |
| 610 | fleet_worker_provider_pins: true, |
| 611 | fleet_mission_board: true, |
| 612 | fleet_worker_model_pool: true, |
| 613 | thread_fleet_members: true, |
| 614 | thread_fleet_member_durable_operations_v1: true, |
| 615 | fleet_run_start: true, |
| 616 | fleet_event_replay: true, |
| 617 | fleet_event_stream: true, |
| 618 | fleet_local_target: true, |
| 619 | fleet_worker_thread_executor: true, |
| 620 | thread_goals: true, |
| 621 | memory: true, |
| 622 | mcp_server_management: false, |
| 623 | skill_lifecycle: false, |
| 624 | skill_detail: false, |
| 625 | plugin_management: false, |
| 626 | agent_mail: true, |
| 627 | event_stream_resume: true, |
| 628 | permission_rules: true, |
| 629 | approval_remember_workspace: true, |
| 630 | approval_grants_list: true, |
| 631 | search_consent: true, |
| 632 | worker_wake_and_revive: true, |
| 633 | fleet_worker_model_hotswitch: true, |
| 634 | }; |
| 635 | let value = serde_json::to_value(&caps).unwrap(); |
| 636 | let obj = value.as_object().unwrap(); |
| 637 | assert_eq!(obj.get("client_token_intents"), Some(&json!(true))); |
| 638 | let mut legacy_intents = value.clone(); |
| 639 | legacy_intents |
| 640 | .as_object_mut() |
| 641 | .unwrap() |
| 642 | .remove("client_token_intents"); |
| 643 | assert!( |
| 644 | !serde_json::from_value::<RuntimeCapabilities>(legacy_intents) |
| 645 | .unwrap() |
| 646 | .client_token_intents |
| 647 | ); |
| 648 | assert_eq!(obj.get("threads").unwrap(), &json!(true)); |
| 649 | assert_eq!(obj.get("thread_patch_undo_turn_id"), Some(&json!(true))); |
| 650 | let mut legacy_patch_undo = value.clone(); |
| 651 | legacy_patch_undo |
| 652 | .as_object_mut() |
| 653 | .unwrap() |
| 654 | .remove("thread_patch_undo_turn_id"); |
| 655 | assert!( |
| 656 | !serde_json::from_value::<RuntimeCapabilities>(legacy_patch_undo) |
| 657 | .unwrap() |
| 658 | .thread_patch_undo_turn_id |
| 659 | ); |
| 660 | assert_eq!(obj.get("thread_shell_consent"), Some(&json!(true))); |
| 661 | assert!( |
| 662 | serde_json::from_value::<RuntimeCapabilities>(value.clone()) |
| 663 | .unwrap() |
| 664 | .thread_shell_consent |
| 665 | ); |
| 666 | let mut legacy_shell = value.clone(); |
| 667 | legacy_shell |
| 668 | .as_object_mut() |
| 669 | .unwrap() |
| 670 | .remove("thread_shell_consent"); |
| 671 | assert!( |
| 672 | !serde_json::from_value::<RuntimeCapabilities>(legacy_shell) |
| 673 | .unwrap() |
| 674 | .thread_shell_consent |
| 675 | ); |
| 676 | |
| 677 | assert_eq!(obj.get("account_session").unwrap(), &json!(true)); |
| 678 | assert_eq!(obj.get("turn_operation_idempotency").unwrap(), &json!(true)); |
| 679 | assert_eq!(obj.get("turn_operation_lookup").unwrap(), &json!(true)); |
| 680 | assert_eq!(obj.get("watch_event_turns").unwrap(), &json!(true)); |
| 681 | let mut watch_legacy = value.clone(); |
| 682 | watch_legacy |
| 683 | .as_object_mut() |
| 684 | .unwrap() |
| 685 | .remove("watch_event_turns"); |
| 686 | assert!( |
| 687 | !serde_json::from_value::<RuntimeCapabilities>(watch_legacy) |
| 688 | .unwrap() |
| 689 | .watch_event_turns |
| 690 | ); |
| 691 | let mut without_lookup = value.clone(); |
| 692 | without_lookup |
| 693 | .as_object_mut() |
| 694 | .unwrap() |
| 695 | .remove("turn_operation_lookup"); |
| 696 | assert!( |
| 697 | !serde_json::from_value::<RuntimeCapabilities>(without_lookup) |
| 698 | .unwrap() |
| 699 | .turn_operation_lookup |
| 700 | ); |
| 701 | assert_eq!(obj.get("turn_image_inputs").unwrap(), &json!(true)); |
| 702 | let mut legacy = value.clone(); |
| 703 | legacy.as_object_mut().unwrap().remove("turn_image_inputs"); |
| 704 | assert!( |
| 705 | !serde_json::from_value::<RuntimeCapabilities>(legacy) |
| 706 | .unwrap() |
| 707 | .turn_image_inputs |
| 708 | ); |
| 709 | assert_eq!(obj.get("external_tools").unwrap(), &json!(false)); |
| 710 | assert!(obj.contains_key("worker_runtime")); |
| 711 | assert_eq!(obj.get("fleet_run_create").unwrap(), &json!(true)); |
| 712 | assert_eq!(obj.get("fleet_event_stream").unwrap(), &json!(true)); |
| 713 | assert_eq!(obj.get("thread_goals").unwrap(), &json!(true)); |
| 714 | assert_eq!(obj.get("thread_scoped_file_writes"), Some(&json!(true))); |
| 715 | let mut legacy_file_writes = value.clone(); |
| 716 | legacy_file_writes |
| 717 | .as_object_mut() |
| 718 | .unwrap() |
| 719 | .remove("thread_scoped_file_writes"); |
| 720 | assert!( |
| 721 | !serde_json::from_value::<RuntimeCapabilities>(legacy_file_writes) |
| 722 | .unwrap() |
| 723 | .thread_scoped_file_writes |
| 724 | ); |
| 725 | assert_eq!(obj.get("memory").unwrap(), &json!(true)); |
| 726 | assert_eq!(obj.get("plugin_management").unwrap(), &json!(false)); |
| 727 | assert_eq!(obj.get("agent_mail").unwrap(), &json!(true)); |
| 728 | } |
| 729 | |
| 730 | #[test] |
| 731 | fn runtime_event_envelope_schema_version_default() { |
| 732 | let json = r#"{ |
| 733 | "seq": 1, |
| 734 | "event": "test", |
| 735 | "kind": "test", |
| 736 | "thread_id": "thr_1", |
| 737 | "timestamp": "2026-06-12T00:00:00Z", |
| 738 | "payload": {} |
| 739 | }"#; |
| 740 | let envelope: RuntimeEventEnvelope = serde_json::from_str(json).unwrap(); |
| 741 | assert_eq!( |
| 742 | envelope.schema_version, |
| 743 | RUNTIME_EVENT_ENVELOPE_SCHEMA_VERSION |
| 744 | ); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 749 | #[serde(deny_unknown_fields)] |
| 750 | pub struct NativeEmpty {} |
| 751 | |
| 752 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 753 | #[serde(deny_unknown_fields)] |
| 754 | pub struct NativeThreadList { |
| 755 | pub include_archived: Option<bool>, |
| 756 | pub limit: Option<u32>, |
| 757 | } |
| 758 | |
| 759 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 760 | #[serde(deny_unknown_fields)] |
| 761 | pub struct NativeThreadId { |
| 762 | pub thread_id: String, |
| 763 | } |
| 764 | |
| 765 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 766 | #[serde(deny_unknown_fields)] |
| 767 | pub struct NativeCreateThread { |
| 768 | pub body: NativeCreateThreadBody, |
| 769 | } |
| 770 | |
| 771 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 772 | #[serde(deny_unknown_fields)] |
| 773 | pub struct NativeCreateThreadBody { |
| 774 | pub operation_key: String, |
| 775 | pub model: Option<String>, |
| 776 | pub model_provider: Option<String>, |
| 777 | pub model_provider_id: Option<String>, |
| 778 | pub reasoning_effort: Option<String>, |
| 779 | pub mode: Option<NativeThreadMode>, |
| 780 | } |
| 781 | |
| 782 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 783 | #[serde(deny_unknown_fields)] |
| 784 | pub struct NativeUpdateThread { |
| 785 | pub thread_id: String, |
| 786 | pub body: NativeUpdateThreadBody, |
| 787 | } |
| 788 | |
| 789 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 790 | #[serde(deny_unknown_fields)] |
| 791 | pub struct NativeUpdateThreadBody { |
| 792 | pub title: Option<String>, |
| 793 | pub archived: Option<bool>, |
| 794 | pub expected_idle: Option<bool>, |
| 795 | pub model: Option<String>, |
| 796 | pub model_provider: Option<String>, |
| 797 | pub model_provider_id: Option<String>, |
| 798 | } |
| 799 | |
| 800 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 801 | #[serde(deny_unknown_fields)] |
| 802 | pub struct NativeProviderModels { |
| 803 | pub provider: String, |
| 804 | pub provider_id: Option<String>, |
| 805 | pub cursor: Option<String>, |
| 806 | } |
| 807 | |
| 808 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 809 | #[serde(deny_unknown_fields)] |
| 810 | pub struct NativeStartTurn { |
| 811 | pub thread_id: String, |
| 812 | pub body: NativeStartTurnBody, |
| 813 | } |
| 814 | |
| 815 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 816 | #[serde(deny_unknown_fields)] |
| 817 | pub struct NativeStartTurnBody { |
| 818 | pub operation_key: String, |
| 819 | pub prompt: String, |
| 820 | pub images: Option<Vec<RuntimeImageInput>>, |
| 821 | pub allowed_tools: Option<Vec<String>>, |
| 822 | pub model: Option<String>, |
| 823 | pub reasoning_effort: Option<String>, |
| 824 | pub model_provider: Option<String>, |
| 825 | pub model_provider_id: Option<String>, |
| 826 | } |
| 827 | |
| 828 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 829 | #[serde(deny_unknown_fields)] |
| 830 | pub struct NativeInterruptTurn { |
| 831 | pub thread_id: String, |
| 832 | pub turn_id: String, |
| 833 | } |
| 834 | |
| 835 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 836 | #[serde(deny_unknown_fields)] |
| 837 | pub struct NativeApproval { |
| 838 | pub approval_id: String, |
| 839 | pub thread_id: String, |
| 840 | pub turn_id: String, |
| 841 | pub tool_call_id: Option<String>, |
| 842 | pub decision: NativeApprovalDecision, |
| 843 | pub remember: Option<bool>, |
| 844 | } |
| 845 | |
| 846 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 847 | #[serde(deny_unknown_fields)] |
| 848 | pub struct NativeSubscribe { |
| 849 | pub thread_id: String, |
| 850 | pub since_seq: u64, |
| 851 | } |
| 852 | |
| 853 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 854 | #[serde(deny_unknown_fields)] |
| 855 | pub struct NativeUnsubscribe { |
| 856 | pub subscription_id: String, |
| 857 | } |
| 858 | |
| 859 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 860 | #[serde(deny_unknown_fields)] |
| 861 | pub struct NativeCreateAutomation { |
| 862 | pub operation_key: String, |
| 863 | pub body: NativeCreateAutomationBody, |
| 864 | } |
| 865 | |
| 866 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 867 | #[serde(deny_unknown_fields)] |
| 868 | pub struct NativeCreateAutomationBody { |
| 869 | pub name: String, |
| 870 | pub prompt: String, |
| 871 | pub rrule: String, |
| 872 | pub destination_thread_id: String, |
| 873 | pub model: Option<String>, |
| 874 | pub model_provider: Option<String>, |
| 875 | pub model_provider_id: Option<String>, |
| 876 | pub status: Option<NativeAutomationStatus>, |
| 877 | pub auto_approve: Option<bool>, |
| 878 | pub trust_mode: Option<bool>, |
| 879 | } |
| 880 | |
| 881 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 882 | #[serde(deny_unknown_fields)] |
| 883 | pub struct NativeAutomationAction { |
| 884 | pub automation_id: String, |
| 885 | pub expected_revision: u64, |
| 886 | pub operation_key: String, |
| 887 | } |
| 888 | |
| 889 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 890 | #[serde(rename_all = "snake_case")] |
| 891 | pub enum NativeApprovalDecision { |
| 892 | Allow, |
| 893 | Deny, |
| 894 | } |
| 895 | |
| 896 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 897 | #[serde(rename_all = "snake_case")] |
| 898 | pub enum NativeAutomationStatus { |
| 899 | Active, |
| 900 | Paused, |
| 901 | } |
| 902 | |
| 903 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 904 | #[serde(rename_all = "snake_case")] |
| 905 | pub enum NativeThreadMode { |
| 906 | Plan, |
| 907 | Agent, |
| 908 | } |
| 909 | |
| 910 | pub const NATIVE_RUNTIME_CAPABILITY: &str = "native_runtime_v1"; |
| 911 | pub const NATIVE_RUNTIME_METHODS: &[&str] = &[ |
| 912 | "runtime/capabilities", |
| 913 | "runtime/threads/list", |
| 914 | "runtime/thread/read", |
| 915 | "runtime/thread/create", |
| 916 | "runtime/thread/update", |
| 917 | "runtime/providers/list", |
| 918 | "runtime/provider/models", |
| 919 | "runtime/turn/start", |
| 920 | "runtime/turn/interrupt", |
| 921 | "runtime/approval/decide", |
| 922 | "runtime/events/subscribe", |
| 923 | "runtime/events/unsubscribe", |
| 924 | "runtime/account/status", |
| 925 | "runtime/automations/list", |
| 926 | "runtime/automation/create", |
| 927 | "runtime/automation/pause", |
| 928 | "runtime/automation/resume", |
| 929 | ]; |
| 930 | |
| 931 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 932 | #[serde(tag = "method", content = "params", deny_unknown_fields)] |
| 933 | pub enum NativeRuntimeRequest { |
| 934 | #[serde(rename = "runtime/capabilities")] |
| 935 | Capabilities(NativeEmpty), |
| 936 | #[serde(rename = "runtime/threads/list")] |
| 937 | ListThreads(NativeThreadList), |
| 938 | #[serde(rename = "runtime/thread/read")] |
| 939 | ReadThread(NativeThreadId), |
| 940 | #[serde(rename = "runtime/thread/create")] |
| 941 | CreateThread(NativeCreateThread), |
| 942 | #[serde(rename = "runtime/thread/update")] |
| 943 | UpdateThread(NativeUpdateThread), |
| 944 | #[serde(rename = "runtime/providers/list")] |
| 945 | Providers(NativeEmpty), |
| 946 | #[serde(rename = "runtime/provider/models")] |
| 947 | ProviderModels(NativeProviderModels), |
| 948 | #[serde(rename = "runtime/turn/start")] |
| 949 | StartTurn(NativeStartTurn), |
| 950 | #[serde(rename = "runtime/turn/interrupt")] |
| 951 | InterruptTurn(NativeInterruptTurn), |
| 952 | #[serde(rename = "runtime/approval/decide")] |
| 953 | DecideApproval(NativeApproval), |
| 954 | #[serde(rename = "runtime/events/subscribe")] |
| 955 | Subscribe(NativeSubscribe), |
| 956 | #[serde(rename = "runtime/events/unsubscribe")] |
| 957 | Unsubscribe(NativeUnsubscribe), |
| 958 | #[serde(rename = "runtime/account/status")] |
| 959 | AccountStatus(NativeEmpty), |
| 960 | #[serde(rename = "runtime/automations/list")] |
| 961 | ListAutomations(NativeEmpty), |
| 962 | #[serde(rename = "runtime/automation/create")] |
| 963 | CreateAutomation(NativeCreateAutomation), |
| 964 | #[serde(rename = "runtime/automation/pause")] |
| 965 | PauseAutomation(NativeAutomationAction), |
| 966 | #[serde(rename = "runtime/automation/resume")] |
| 967 | ResumeAutomation(NativeAutomationAction), |
| 968 | } |
| 969 |