Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): scope trajectory recording to the session that started it (#3632)

umutcagand758488e582b7
diff --git a/docs/content/docs/reference/cua-driver/mcp-tools-linux.mdx b/docs/content/docs/reference/cua-driver/mcp-tools-linux.mdx
index ecdfb4b46f..fc9a57cdd9 100644
--- a/docs/content/docs/reference/cua-driver/mcp-tools-linux.mdx
+++ b/docs/content/docs/reference/cua-driver/mcp-tools-linux.mdx
@@ -563,7 +563,9 @@ accepts it. Omit it to use the authenticated transport's implicit lifecycle sess
### `start_recording`
-Start trajectory recording. Every subsequent action-tool invocation (click, right_click, scroll, type_text, press_key, hotkey, set_value) writes a turn folder under `output_dir`:
+Start trajectory recording for the calling session. Each action-tool invocation (click, right_click, scroll, type_text, press_key, hotkey, set_value) from that session writes a turn folder under `output_dir`. Without `session`, every call on the same connection is recorded, including named-session calls. Other connections and session lifecycle calls (`start_session` / `end_session`) are not recorded. CLI recordings started with `cua-driver recording start` are daemon-wide.
+
+Each turn folder holds:
- `before_state.json` / `after_state.json` — application AX/UIA/AT-SPI state immediately before and after the action.
- `before.png` / `after.png` — target-window screenshots immediately before and after the action.
@@ -590,6 +592,7 @@ State persists for the life of the daemon; a restart resets to disabled with no
- `include_accessibility_tree` (boolean, optional): Default true. Set false to skip the per-turn before/after accessibility walks entirely; screenshots, click markers and action.json are still recorded and state is classified `state_capture_disabled`. default: `true`
- `output_dir` (string, required): Absolute or ~-rooted directory where turn folders and (when enabled) the video file are written.
- `record_video` (boolean, optional): Capture the main display to <output_dir>/recording.mp4. Default: false. Set to true to also capture the main display to recording.mp4 (otherwise only the per-turn screenshots + JSON are recorded). On macOS this uses native ScreenCaptureKit (macOS 15.0+); macOS 26 can show a one-time direct screen-capture consent on first use (see `cua-driver permissions grant`). On Windows + Linux it requires ffmpeg on PATH.
+- `session` (string, optional): For multi-call work, prefer a short public session label and repeat it on every call that accepts it. Omit it to use the authenticated transport's implicit lifecycle session.
- `state_timeout_ms` (integer, optional): Wall-clock budget in milliseconds for EACH per-turn before/after accessibility walk (same default and bounds as get_window_state's timeout_ms). A walk that runs out records the partial tree and marks it truncated in evidence.json. default: `1000`; range: 100–120000
```json
diff --git a/docs/content/docs/reference/cua-driver/mcp-tools-windows.mdx b/docs/content/docs/reference/cua-driver/mcp-tools-windows.mdx
index a8bbd23238..597e314ebe 100644
--- a/docs/content/docs/reference/cua-driver/mcp-tools-windows.mdx
+++ b/docs/content/docs/reference/cua-driver/mcp-tools-windows.mdx
@@ -655,7 +655,9 @@ accepts it. Omit it to use the authenticated transport's implicit lifecycle sess
### `start_recording`
-Start trajectory recording. Every subsequent action-tool invocation (click, right_click, scroll, type_text, press_key, hotkey, set_value) writes a turn folder under `output_dir`:
+Start trajectory recording for the calling session. Each action-tool invocation (click, right_click, scroll, type_text, press_key, hotkey, set_value) from that session writes a turn folder under `output_dir`. Without `session`, every call on the same connection is recorded, including named-session calls. Other connections and session lifecycle calls (`start_session` / `end_session`) are not recorded. CLI recordings started with `cua-driver recording start` are daemon-wide.
+
+Each turn folder holds:
- `before_state.json` / `after_state.json` — application AX/UIA/AT-SPI state immediately before and after the action.
- `before.png` / `after.png` — target-window screenshots immediately before and after the action.
@@ -682,6 +684,7 @@ State persists for the life of the daemon; a restart resets to disabled with no
- `include_accessibility_tree` (boolean, optional): Default true. Set false to skip the per-turn before/after accessibility walks entirely; screenshots, click markers and action.json are still recorded and state is classified `state_capture_disabled`. default: `true`
- `output_dir` (string, required): Absolute or ~-rooted directory where turn folders and (when enabled) the video file are written.
- `record_video` (boolean, optional): Capture the main display to <output_dir>/recording.mp4. Default: false. Set to true to also capture the main display to recording.mp4 (otherwise only the per-turn screenshots + JSON are recorded). On macOS this uses native ScreenCaptureKit (macOS 15.0+); macOS 26 can show a one-time direct screen-capture consent on first use (see `cua-driver permissions grant`). On Windows + Linux it requires ffmpeg on PATH.
+- `session` (string, optional): For multi-call work, prefer a short public session label and repeat it on every call that accepts it. Omit it to use the authenticated transport's implicit lifecycle session.
- `state_timeout_ms` (integer, optional): Wall-clock budget in milliseconds for EACH per-turn before/after accessibility walk (same default and bounds as get_window_state's timeout_ms). A walk that runs out records the partial tree and marks it truncated in evidence.json. default: `1000`; range: 100–120000
```json
diff --git a/docs/content/docs/reference/cua-driver/mcp-tools.mdx b/docs/content/docs/reference/cua-driver/mcp-tools.mdx
index 70803b53be..f32bda3eb5 100644
--- a/docs/content/docs/reference/cua-driver/mcp-tools.mdx
+++ b/docs/content/docs/reference/cua-driver/mcp-tools.mdx
@@ -613,7 +613,9 @@ accepts it. Omit it to use the authenticated transport's implicit lifecycle sess
### `start_recording`
-Start trajectory recording. Every subsequent action-tool invocation (click, right_click, scroll, type_text, press_key, hotkey, set_value) writes a turn folder under `output_dir`:
+Start trajectory recording for the calling session. Each action-tool invocation (click, right_click, scroll, type_text, press_key, hotkey, set_value) from that session writes a turn folder under `output_dir`. Without `session`, every call on the same connection is recorded, including named-session calls. Other connections and session lifecycle calls (`start_session` / `end_session`) are not recorded. CLI recordings started with `cua-driver recording start` are daemon-wide.
+
+Each turn folder holds:
- `before_state.json` / `after_state.json` — application AX/UIA/AT-SPI state immediately before and after the action.
- `before.png` / `after.png` — target-window screenshots immediately before and after the action.
@@ -640,6 +642,7 @@ State persists for the life of the daemon; a restart resets to disabled with no
- `include_accessibility_tree` (boolean, optional): Default true. Set false to skip the per-turn before/after accessibility walks entirely; screenshots, click markers and action.json are still recorded and state is classified `state_capture_disabled`. default: `true`
- `output_dir` (string, required): Absolute or ~-rooted directory where turn folders and (when enabled) the video file are written.
- `record_video` (boolean, optional): Capture the main display to <output_dir>/recording.mp4. Default: false. Set to true to also capture the main display to recording.mp4 (otherwise only the per-turn screenshots + JSON are recorded). On macOS this uses native ScreenCaptureKit (macOS 15.0+); macOS 26 can show a one-time direct screen-capture consent on first use (see `cua-driver permissions grant`). On Windows + Linux it requires ffmpeg on PATH.
+- `session` (string, optional): For multi-call work, prefer a short public session label and repeat it on every call that accepts it. Omit it to use the authenticated transport's implicit lifecycle session.
- `state_timeout_ms` (integer, optional): Wall-clock budget in milliseconds for EACH per-turn before/after accessibility walk (same default and bounds as get_window_state's timeout_ms). A walk that runs out records the partial tree and marks it truncated in evidence.json. default: `1000`; range: 100–120000
```json
diff --git a/libs/cua-driver/rust/Skills/cua-driver/RECORDING.md b/libs/cua-driver/rust/Skills/cua-driver/RECORDING.md
index 953e4b54ce..dbc36e3bcb 100644
--- a/libs/cua-driver/rust/Skills/cua-driver/RECORDING.md
+++ b/libs/cua-driver/rust/Skills/cua-driver/RECORDING.md
@@ -1,8 +1,9 @@
# Recording and replay
Record only when the user requests it. Keep the recording controls and actions
-on one persistent MCP connection; see [RUNTIME.md](RUNTIME.md). The recording
-tools do not advertise a public `session` parameter. Do not add one.
+on one persistent MCP connection; see [RUNTIME.md](RUNTIME.md). When the actions
+pass a `session` label, you may pass the same label to `start_recording`.
+`stop_recording` and `get_recording_state` do not accept `session`.
## Start, observe, stop
@@ -22,11 +23,18 @@ off by default; explicitly set `record_video:true` when requested. Inspect
`video_active` and `last_error`, not just the successful tool status. Per-turn
capture may continue even when video initialization failed.
-The current recorder is shared within its runtime. Manual `stop_recording`
-stops whichever recording is active, regardless of its starting session.
-Do not start over or stop another run's recording. Session-owned teardown
-does not establish concurrent recording isolation. If a recorder is already
-active, coordinate with its owner rather than taking it over.
+A recording keeps only its owner's actions. With a `session` label, it keeps
+that session's calls. Without one, it keeps every call on the connection that
+started it, including calls that pass a `session` label. Calls from other
+connections, including one-shot `cua-driver <tool>` processes, and
+`start_session` / `end_session` write no turns. A recording started with CLI
+`cua-driver recording start` has no owning session and records every
+session's actions.
+
+There is still one recorder per runtime. Manual `stop_recording` stops
+whichever recording is active, regardless of its starting session. Do not
+start over or stop another run's recording. If a recorder is already active,
+coordinate with its owner rather than taking it over.
Stop and inspect `last_video_path` before ending the connection. A disconnect
can tear down owned recording; a runtime restart loses in-memory state.
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/recording.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/recording.rs
index 674d53d494..3cbb9bc697 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/recording.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/recording.rs
@@ -453,6 +453,25 @@ pub struct PendingTurn {
dispatch_click: Option<Arc<DispatchClickScope>>,
}
+/// Trusted identity of the call that may reserve a turn.
+///
+/// `session` is the call's runtime session (its explicit `session` label, or
+/// the transport lease when it has none) and `transport` is the connection
+/// that carried it. A recording owned by a session keeps calls from that
+/// session. A recording started without a label is owned by its transport's
+/// lease, so it also keeps the same connection's named-session calls.
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
+pub struct RecordingCaller<'a> {
+ pub session: Option<&'a str>,
+ pub transport: Option<&'a str>,
+}
+
+impl RecordingCaller<'_> {
+ fn within(self, owner: &str) -> bool {
+ self.session == Some(owner) || self.transport == Some(owner)
+ }
+}
+
/// Persistent recording session state owned by one tool registry.
pub struct RecordingSession {
inner: Mutex<RecordingInner>,
@@ -821,9 +840,15 @@ impl RecordingSession {
}
/// Reserve a turn and capture its target immediately before tool dispatch.
- /// No-op when recording is disabled.
- pub fn begin_turn(&self, tool_name: &str, args: &Value, start_ms: u64) -> Option<PendingTurn> {
- self.begin_turn_with_capture(tool_name, args, start_ms, true, None)
+ /// No-op when disabled or when `caller` is outside the recording's scope.
+ pub fn begin_turn(
+ &self,
+ tool_name: &str,
+ args: &Value,
+ start_ms: u64,
+ caller: RecordingCaller<'_>,
+ ) -> Option<PendingTurn> {
+ self.begin_turn_with_capture(tool_name, args, start_ms, caller, true, None)
}
/// [`Self::begin_turn`] for a call the dispatcher already knows the tool
@@ -837,9 +862,10 @@ impl RecordingSession {
tool_name: &str,
args: &Value,
start_ms: u64,
+ caller: RecordingCaller<'_>,
predicted_refusal: Option<&'static str>,
) -> Option<PendingTurn> {
- self.begin_turn_with_capture(tool_name, args, start_ms, true, predicted_refusal)
+ self.begin_turn_with_capture(tool_name, args, start_ms, caller, true, predicted_refusal)
}
/// Reserve a turn while deliberately suppressing visual and accessibility
@@ -851,15 +877,18 @@ impl RecordingSession {
tool_name: &str,
args: &Value,
start_ms: u64,
+ caller: RecordingCaller<'_>,
) -> Option<PendingTurn> {
- self.begin_turn_with_capture(tool_name, args, start_ms, false, None)
+ self.begin_turn_with_capture(tool_name, args, start_ms, caller, false, None)
}
+ /// Scope-check and reserve under one lock; ownerless recordings accept all callers.
fn begin_turn_with_capture(
&self,
tool_name: &str,
args: &Value,
start_ms: u64,
+ caller: RecordingCaller<'_>,
capture_visual_state: bool,
predicted_refusal: Option<&'static str>,
) -> Option<PendingTurn> {
@@ -868,6 +897,11 @@ impl RecordingSession {
if !inner.enabled {
return None;
}
+ if let Some(owner) = inner.owner.as_deref() {
+ if !caller.within(owner) {
+ return None;
+ }
+ }
let out = inner.output_dir.clone()?;
let idx = inner.next_turn;
inner.next_turn += 1;
@@ -1017,8 +1051,15 @@ impl RecordingSession {
/// Compatibility helper for callers that only report completed calls.
/// New dispatch paths should use `begin_turn` and `finish_turn` so the
/// before phase is captured before the action changes application state.
- pub fn record(&self, tool_name: &str, args: &Value, result_text: &str, start_ms: u64) {
- let Some(pending) = self.begin_turn(tool_name, args, start_ms) else {
+ pub fn record(
+ &self,
+ tool_name: &str,
+ args: &Value,
+ result_text: &str,
+ start_ms: u64,
+ caller: RecordingCaller<'_>,
+ ) {
+ let Some(pending) = self.begin_turn(tool_name, args, start_ms, caller) else {
return;
};
self.finish_turn(pending, result_text);
@@ -1572,6 +1613,7 @@ mod tests {
"click",
&serde_json::json!({"pid": 1, "window_id": 2, "x": 3, "y": 4}),
now_ms(),
+ RecordingCaller::default(),
)
.unwrap()
.click_point
@@ -1666,14 +1708,16 @@ mod tests {
.await;
let session = RecordingSession::new();
- assert!(session.begin_turn("click", &pending.args, 0).is_none());
+ assert!(session
+ .begin_turn("click", &pending.args, 0, RecordingCaller::default())
+ .is_none());
{
let mut inner = session.inner.lock().unwrap();
inner.enabled = true;
inner.output_dir = Some(root.path().to_path_buf());
}
let private = session
- .begin_private_turn("click", &pending.args, 0)
+ .begin_private_turn("click", &pending.args, 0, RecordingCaller::default())
.unwrap();
scope_dispatch_click_capture(Some(&private), async {
capture_dispatch_click_target(902, 901, forbidden);
@@ -1758,6 +1802,7 @@ mod tests {
"pid":901, "window_id":902, "element_index":3,
}),
0,
+ RecordingCaller::default(),
)
.unwrap();
scope_dispatch_click_capture(Some(&pending), async {
@@ -2201,6 +2246,7 @@ mod tests {
"click",
&serde_json::json!({"pid": 1, "window_id": 2, "x": 3, "y": 4}),
now_ms(),
+ RecordingCaller::default(),
)
.expect("recording should reserve a turn");
let turn = output_dir.join("turn-00001");
@@ -2241,6 +2287,7 @@ mod tests {
"click",
&serde_json::json!({"pid": 1, "element_token": token}),
now_ms(),
+ RecordingCaller::default(),
)
.expect("token-only click should reserve a targeted turn");
session.finish_turn(pending, "token click");
@@ -2261,6 +2308,7 @@ mod tests {
"click",
&serde_json::json!({"pid": 1, "element_token": stale_token}),
now_ms(),
+ RecordingCaller::default(),
)
.expect("stale-token refusal should reserve an evidence turn");
session.finish_turn_with_outcome(pending, "stale token", None, true);
@@ -2287,6 +2335,7 @@ mod tests {
"click",
&serde_json::json!({"pid": 1, "window_id": 2, "x": 3, "y": 4}),
now_ms(),
+ RecordingCaller::default(),
)
.expect("resolved refusal should reserve an evidence turn");
let refusal_record = crate::action_record::ActionExecutionRecord::builder(
@@ -2394,7 +2443,12 @@ mod tests {
inner.session_start_ms = now_ms();
}
let pending = session
- .begin_turn("click", &serde_json::json!({"x": 1, "y": 2}), now_ms())
+ .begin_turn(
+ "click",
+ &serde_json::json!({"x": 1, "y": 2}),
+ now_ms(),
+ RecordingCaller::default(),
+ )
.expect("reserve first generation turn");
session.inner.lock().unwrap().generation = 2;
session.finish_turn(pending, "must be discarded");
@@ -2597,6 +2651,7 @@ mod tests {
"browser_prepare",
&serde_json::json!({"pid": 1, "window_id": 2}),
now_ms(),
+ RecordingCaller::default(),
)
.expect("reserve private consent turn");
session.finish_turn(pending, "attached");
@@ -2708,4 +2763,145 @@ mod tests {
.expect("finalized nonempty output must pass");
let _ = std::fs::remove_dir_all(output_dir);
}
+
+ #[test]
+ fn owned_recording_reserves_turns_only_for_its_owning_session() {
+ let root = tempfile::tempdir().expect("temp dir");
+ let output_dir = root.path().join("owned");
+ let session = RecordingSession::new();
+ session
+ .start(
+ output_dir.to_str().expect("utf-8 output dir"),
+ false,
+ Some("owner"),
+ )
+ .expect("start an owned recording");
+
+ // Avoid the (pid 1, window 2) target whose screenshot phase another test counts.
+ let args = serde_json::json!({"pid": 41, "window_id": 42, "x": 3, "y": 4});
+ assert!(
+ session
+ .begin_turn(
+ "click",
+ &args,
+ now_ms(),
+ RecordingCaller {
+ session: Some("other"),
+ transport: None
+ }
+ )
+ .is_none(),
+ "another session's call must not be recorded"
+ );
+ assert!(
+ session
+ .begin_turn("click", &args, now_ms(), RecordingCaller::default())
+ .is_none(),
+ "a sessionless call must not be recorded"
+ );
+
+ let pending = session
+ .begin_turn(
+ "click",
+ &args,
+ now_ms(),
+ RecordingCaller {
+ session: Some("owner"),
+ transport: None,
+ },
+ )
+ .expect("the owning session's call reserves a turn");
+ session.finish_turn(pending, "clicked");
+ session.stop_owner(None).expect("stop owned recording");
+
+ let action: Value = serde_json::from_slice(
+ &std::fs::read(output_dir.join("turn-00001").join("action.json"))
+ .expect("read the owner's turn"),
+ )
+ .expect("parse action.json");
+ assert_eq!(action["tool"], "click");
+ assert!(
+ !output_dir.join("turn-00002").exists(),
+ "foreign calls must not leave turn folders"
+ );
+ }
+
+ #[test]
+ fn unlabelled_recording_keeps_named_sessions_on_its_own_transport() {
+ let root = tempfile::tempdir().expect("temp dir");
+ let output_dir = root.path().join("transport-owned");
+ let session = RecordingSession::new();
+ // An unlabelled start is owned by the transport lease that carried it.
+ session
+ .start(
+ output_dir.to_str().expect("utf-8 output dir"),
+ false,
+ Some("transport-1"),
+ )
+ .expect("start a transport-owned recording");
+
+ // Avoid the (pid 1, window 2) target whose screenshot phase another test counts.
+ let args = serde_json::json!({"pid": 41, "window_id": 42, "x": 3, "y": 4});
+ assert!(
+ session
+ .begin_turn(
+ "click",
+ &args,
+ now_ms(),
+ RecordingCaller {
+ session: Some("demo"),
+ transport: Some("transport-2"),
+ },
+ )
+ .is_none(),
+ "a named session on another transport must not be recorded"
+ );
+ let pending = session
+ .begin_turn(
+ "click",
+ &args,
+ now_ms(),
+ RecordingCaller {
+ session: Some("demo"),
+ transport: Some("transport-1"),
+ },
+ )
+ .expect("a named session on the owning transport reserves a turn");
+ session.finish_turn(pending, "clicked");
+ session
+ .stop_owner(None)
+ .expect("stop transport-owned recording");
+
+ assert!(output_dir.join("turn-00001").join("action.json").exists());
+ assert!(!output_dir.join("turn-00002").exists());
+ }
+
+ #[test]
+ fn anonymous_recording_stays_daemon_wide() {
+ let root = tempfile::tempdir().expect("temp dir");
+ let output_dir = root.path().join("anonymous");
+ let session = RecordingSession::new();
+ session
+ .start(output_dir.to_str().expect("utf-8 output dir"), false, None)
+ .expect("start an anonymous recording");
+
+ // Avoid the (pid 1, window 2) target whose screenshot phase another test counts.
+ let args = serde_json::json!({"pid": 41, "window_id": 42, "x": 3, "y": 4});
+ for caller in [
+ RecordingCaller {
+ session: Some("some-session"),
+ transport: None,
+ },
+ RecordingCaller::default(),
+ ] {
+ let pending = session
+ .begin_turn("click", &args, now_ms(), caller)
+ .expect("an anonymous recording records every caller");
+ session.finish_turn(pending, "clicked");
+ }
+ session.stop_owner(None).expect("stop anonymous recording");
+
+ assert!(output_dir.join("turn-00001").join("action.json").exists());
+ assert!(output_dir.join("turn-00002").join("action.json").exists());
+ }
}
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/recording_tools.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/recording_tools.rs
index dc6751a545..5b891c7ecb 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/recording_tools.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/recording_tools.rs
@@ -45,9 +45,14 @@ impl Tool for StartRecordingTool {
fn def(&self) -> &ToolDef {
START_REC_DEF.get_or_init(|| ToolDef {
name: "start_recording".into(),
- description: "Start trajectory recording. Every subsequent action-tool \
+ description: "Start trajectory recording for the calling session. Each action-tool \
invocation (click, right_click, scroll, type_text, press_key, hotkey, \
- set_value) writes a turn folder under `output_dir`:\n\n\
+ set_value) from that session writes a turn folder under `output_dir`. \
+ Without `session`, every call on the same connection is recorded, \
+ including named-session calls. Other connections and session lifecycle \
+ calls (`start_session` / `end_session`) are not recorded. CLI recordings started with \
+ `cua-driver recording start` are daemon-wide.\n\n\
+ Each turn folder holds:\n\n\
- `before_state.json` / `after_state.json` — application AX/UIA/AT-SPI \
state immediately before and after the action.\n\
- `before.png` / `after.png` — target-window screenshots immediately \
@@ -135,6 +140,12 @@ impl Tool for StartRecordingTool {
before/after accessibility walks entirely; screenshots, \
click markers and action.json are still recorded and state \
is classified `state_capture_disabled`."
+ },
+ "session": {
+ "type": "string",
+ "description": "For multi-call work, prefer a short public session label \
+ and repeat it on every call that accepts it. Omit it to use the \
+ authenticated transport's implicit lifecycle session."
}
},
"additionalProperties": false
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs
index 47b56e7134..56392b612c 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/tool.rs
@@ -1558,10 +1558,16 @@ impl ToolRegistry {
// Reserve and capture the turn before dispatch so recorded evidence
// shows the application immediately before the action changed it.
+ // Exclude session lifecycle calls, including one-shot CLI teardown.
let should_record = !tool.def().read_only
&& !matches!(
resolved_name,
- "start_recording" | "stop_recording" | "get_recording_state" | "replay_trajectory"
+ "start_recording"
+ | "stop_recording"
+ | "get_recording_state"
+ | "replay_trajectory"
+ | "start_session"
+ | "end_session"
);
let private_consent_turn = is_existing_profile_prepare(resolved_name, &args);
let _desktop_action = if requires_desktop_coordination(
@@ -1584,9 +1590,18 @@ impl ToolRegistry {
};
let pending_turn = should_record
.then(|| {
+ // Use the same trusted identities the recording owner was minted from.
+ let caller = crate::recording::RecordingCaller {
+ session: runtime_session.as_deref(),
+ transport: args.get("_transport_session_id").and_then(Value::as_str),
+ };
if private_consent_turn {
- self.recording
- .begin_private_turn(resolved_name, &recording_args, start_ms)
+ self.recording.begin_private_turn(
+ resolved_name,
+ &recording_args,
+ start_ms,
+ caller,
+ )
} else {
// A capture-bound click whose capture is already unknown,
// expired, or superseded is refused by every platform
@@ -1600,6 +1615,7 @@ impl ToolRegistry {
resolved_name,
&recording_args,
start_ms,
+ caller,
predicted_refusal,
)
}
@@ -3290,6 +3306,89 @@ mod runtime_isolation_tests {
registry
}
+ fn recording_scope_registry(hits: Arc<AtomicUsize>) -> Arc<super::ToolRegistry> {
+ let mut registry = super::ToolRegistry::new();
+ registry.register(Box::new(ReplayProbe {
+ hits,
+ def: super::ToolDef {
+ name: "probe".into(),
+ description: "runtime-local recording-scope probe".into(),
+ input_schema: serde_json::json!({
+ "type": "object",
+ "properties": { "session": { "type": "string" } },
+ }),
+ read_only: false,
+ destructive: false,
+ idempotent: true,
+ open_world: false,
+ },
+ }));
+ registry.register_recording_tools();
+ registry.register_session_tools();
+ let registry = Arc::new(registry);
+ registry.init_self_weak();
+ registry
+ }
+
+ #[tokio::test]
+ async fn recording_keeps_only_the_owning_session_and_never_lifecycle_calls() {
+ let registry = recording_scope_registry(Arc::new(AtomicUsize::new(0)));
+ let context = unrestricted_context();
+ let output = tempfile::tempdir().expect("temp dir");
+
+ let started = registry
+ .invoke_with_context(
+ "start_recording",
+ serde_json::json!({
+ "output_dir": output.path(),
+ "record_video": false,
+ "session": "owner",
+ }),
+ context.clone(),
+ )
+ .await;
+ assert_ne!(started.is_error, Some(true), "{started:?}");
+
+ for (tool, session) in [
+ ("probe", "other"),
+ ("end_session", "other"),
+ ("end_session", "owner-lifecycle"),
+ ] {
+ let result = registry
+ .invoke_with_context(
+ tool,
+ serde_json::json!({ "session": session }),
+ context.clone(),
+ )
+ .await;
+ assert_ne!(result.is_error, Some(true), "{tool} {session}: {result:?}");
+ }
+
+ let owned = registry
+ .invoke_with_context(
+ "probe",
+ serde_json::json!({ "session": "owner" }),
+ context.clone(),
+ )
+ .await;
+ assert_ne!(owned.is_error, Some(true), "{owned:?}");
+ registry
+ .invoke_with_context("stop_recording", serde_json::json!({}), context)
+ .await;
+
+ let action: serde_json::Value = serde_json::from_slice(
+ &std::fs::read(output.path().join("turn-00001").join("action.json"))
+ .expect("the owning session's action is turn-00001"),
+ )
+ .expect("parse action.json");
+ assert_eq!(action["tool"], "probe");
+ assert_eq!(action["arguments"]["session"], "owner");
+ assert!(
+ !output.path().join("turn-00002").exists(),
+ "foreign and lifecycle calls must not leave turn folders"
+ );
+ }
+
#[tokio::test]
async fn replay_dispatches_only_through_the_owning_registry() {
let hits_a = Arc::new(AtomicUsize::new(0));
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/tests/recording_semantic_evidence.rs b/libs/cua-driver/rust/crates/cua-driver-core/tests/recording_semantic_evidence.rs
index 4c8f35c22e..1a0d0f0ec8 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/tests/recording_semantic_evidence.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/tests/recording_semantic_evidence.rs
@@ -2,7 +2,7 @@ use cua_driver_core::action_record::{
ActionEffect, ActionExecutionRecord, ActionTransport, ActualDelivery, RequestedDelivery,
};
use cua_driver_core::recording::{
- set_ax_snapshot_fn, set_element_bounds_fn, set_screenshot_fn, RecordingSession,
+ set_ax_snapshot_fn, set_element_bounds_fn, set_screenshot_fn, RecordingCaller, RecordingSession,
};
use serde_json::{json, Value};
@@ -34,7 +34,9 @@ fn macos_semantic_activation_without_a_point_preserves_truth_without_a_marker()
.actual_delivery(ActualDelivery::Foreground)
.build()
.unwrap();
- let pending = recording.begin_turn("click", &args, 0).unwrap();
+ let pending = recording
+ .begin_turn("click", &args, 0, RecordingCaller::default())
+ .unwrap();
recording.finish_turn_with_outcome(pending, "Performed AXPress", Some(&action), false);
recording.stop_owner(None).unwrap();
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/tests/recording_transport_identity.rs b/libs/cua-driver/rust/crates/cua-driver-core/tests/recording_transport_identity.rs
new file mode 100644
index 0000000000..52e0c2d3cb
--- /dev/null
+++ b/libs/cua-driver/rust/crates/cua-driver-core/tests/recording_transport_identity.rs
@@ -0,0 +1,99 @@
+use std::sync::{Arc, Mutex};
+
+use async_trait::async_trait;
+use cua_driver_core::{
+ protocol::Request,
+ recording::RecordingSession,
+ recording_tools::StartRecordingTool,
+ server::{handle_request_with_transport_session, ToolProvider},
+ tool::Tool,
+};
+use serde_json::{json, Value};
+
+#[derive(Default)]
+struct CaptureProvider {
+ calls: Mutex<Vec<(String, Value)>>,
+}
+
+#[async_trait]
+impl ToolProvider for CaptureProvider {
+ fn tools_list(&self) -> Value {
+ json!({"tools": []})
+ }
+
+ async fn invoke_tool(&self, name: &str, arguments: Value) -> Result<Value, String> {
+ self.calls
+ .lock()
+ .unwrap()
+ .push((name.to_owned(), arguments));
+ Ok(json!({
+ "content": [{"type": "text", "text": "ok"}],
+ "isError": false
+ }))
+ }
+}
+
+fn tool_call(id: u64, name: &str, arguments: Value) -> Request {
+ serde_json::from_value(json!({
+ "jsonrpc": "2.0",
+ "id": id,
+ "method": "tools/call",
+ "params": {"name": name, "arguments": arguments}
+ }))
+ .unwrap()
+}
+
+async fn dispatch(provider: &CaptureProvider, id: u64, name: &str, arguments: Value) {
+ let request = tool_call(id, name, arguments);
+ let _ =
+ handle_request_with_transport_session(request, json!(id), provider, "transport-1").await;
+}
+
+#[test]
+fn start_recording_contract_accepts_a_public_session_label() {
+ let tool = StartRecordingTool::new(Arc::new(RecordingSession::new()));
+ assert_eq!(
+ tool.def().input_schema["properties"]["session"]["type"],
+ "string"
+ );
+}
+
+#[tokio::test]
+async fn named_and_implicit_recording_calls_share_the_transport_resolved_identity() {
+ let provider = CaptureProvider::default();
+
+ dispatch(
+ &provider,
+ 1,
+ "start_recording",
+ json!({"output_dir": "/tmp/named", "session": "demo"}),
+ )
+ .await;
+ dispatch(
+ &provider,
+ 2,
+ "click",
+ json!({"x": 1, "y": 2, "session": "demo"}),
+ )
+ .await;
+ dispatch(
+ &provider,
+ 3,
+ "start_recording",
+ json!({"output_dir": "/tmp/implicit"}),
+ )
+ .await;
+ dispatch(&provider, 4, "click", json!({"x": 3, "y": 4})).await;
+
+ let calls = provider.calls.lock().unwrap();
+ assert_eq!(calls.len(), 4);
+
+ for index in [0usize, 1] {
+ assert_eq!(calls[index].1["_session_id"], "demo");
+ assert_eq!(calls[index].1["_transport_session_id"], "transport-1");
+ }
+ for index in [2usize, 3] {
+ assert_eq!(calls[index].1["_session_id"], "transport-1");
+ assert_eq!(calls[index].1["_transport_session_id"], "transport-1");
+ }
+}