codex-mobile-bridge 0.3.14

Remote bridge and service manager for codex-mobile.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use serde_json::json;
use tokio::time::{Duration, timeout};

use crate::app_server::AppServerInbound;
use crate::storage::PRIMARY_RUNTIME_ID;

use super::super::events::handle_app_server_message;
use super::support::bootstrap_test_state;

#[tokio::test]
async fn command_execution_render_snapshot_preserves_exec_kind() {
    let state = bootstrap_test_state().await;

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "item/completed".to_string(),
                params: json!({
                    "threadId": "thread-render-1",
                    "turnId": "turn-1",
                    "item": {
                        "id": "item-explored",
                        "type": "commandExecution",
                        "status": "completed",
                        "command": "ls -la",
                        "aggregatedOutput": "file-a\nfile-b\n",
                        "exitCode": 0,
                        "commandActions": [
                            {
                                "type": "listFiles",
                                "command": "ls -la",
                                "path": "."
                            }
                        ]
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 commandExecution item/completed 超时")
    .expect("处理 commandExecution item/completed 失败");

    let snapshots = state
        .thread_render_snapshots
        .lock()
        .expect("thread render snapshots poisoned");
    let snapshot = snapshots
        .get("thread-render-1")
        .expect("应缓存 thread render snapshot");
    let exec_node = snapshot
        .nodes
        .iter()
        .find_map(|node| match node {
            crate::bridge_protocol::ThreadRenderNode::ExecGroup {
                kind, output_text, ..
            } => Some((kind.clone(), output_text.clone())),
            _ => None,
        })
        .expect("应存在 exec group 节点");
    assert_eq!("explored", exec_node.0);
    assert_eq!(Some("file-a\nfile-b\n".to_string()), exec_node.1);
}

#[tokio::test]
async fn file_change_render_snapshot_preserves_per_file_diff() {
    let state = bootstrap_test_state().await;

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "item/completed".to_string(),
                params: json!({
                    "threadId": "thread-render-2",
                    "turnId": "turn-2",
                    "item": {
                        "id": "item-file-change",
                        "type": "fileChange",
                        "status": "completed",
                        "changes": [
                            {
                                "path": "/srv/workspace/src/main.rs",
                                "kind": {
                                    "update": {}
                                },
                                "diff": "diff --git a/src/main.rs b/src/main.rs\n--- a/src/main.rs\n+++ b/src/main.rs\n@@ -1 +1 @@\n-old\n+new\n"
                            }
                        ]
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 fileChange item/completed 超时")
    .expect("处理 fileChange item/completed 失败");

    let snapshots = state
        .thread_render_snapshots
        .lock()
        .expect("thread render snapshots poisoned");
    let snapshot = snapshots
        .get("thread-render-2")
        .expect("应缓存 thread render snapshot");
    let change = snapshot
        .nodes
        .iter()
        .find_map(|node| match node {
            crate::bridge_protocol::ThreadRenderNode::FileChange { changes, .. } => {
                changes.first().cloned()
            }
            _ => None,
        })
        .expect("应存在 file change 节点");
    assert_eq!("/srv/workspace/src/main.rs", change.path);
    assert_eq!(
        Some(
            "diff --git a/src/main.rs b/src/main.rs\n--- a/src/main.rs\n+++ b/src/main.rs\n@@ -1 +1 @@\n-old\n+new\n"
                .to_string()
        ),
        change.diff,
    );
}

#[tokio::test]
async fn user_message_render_snapshot_collects_data_urls_without_polluting_text() {
    let state = bootstrap_test_state().await;

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "item/completed".to_string(),
                params: json!({
                    "threadId": "thread-render-user-message",
                    "turnId": "turn-user-1",
                    "item": {
                        "id": "item-user-1",
                        "type": "userMessage",
                        "content": [
                            {
                                "type": "text",
                                "text": "请分析这张图"
                            },
                            {
                                "type": "image",
                                "url": "data:image/png;base64,aGVsbG8="
                            }
                        ]
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 userMessage item/completed 超时")
    .expect("处理 userMessage item/completed 失败");

    let snapshots = state
        .thread_render_snapshots
        .lock()
        .expect("thread render snapshots poisoned");
    let snapshot = snapshots
        .get("thread-render-user-message")
        .expect("应缓存用户消息快照");
    let user_node = snapshot
        .nodes
        .iter()
        .find_map(|node| match node {
            crate::bridge_protocol::ThreadRenderNode::UserMessage {
                text, image_urls, ..
            } => Some((text.clone(), image_urls.clone())),
            _ => None,
        })
        .expect("应存在 user message 节点");
    assert_eq!("请分析这张图", user_node.0);
    assert_eq!(
        vec!["data:image/png;base64,aGVsbG8=".to_string()],
        user_node.1
    );
}

#[tokio::test]
async fn hook_notifications_upsert_single_render_node() {
    let state = bootstrap_test_state().await;

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "hook/started".to_string(),
                params: json!({
                    "threadId": "thread-render-hook",
                    "turnId": "turn-hook",
                    "run": {
                        "id": "hook-run-1",
                        "eventName": "preToolUse",
                        "statusMessage": "checking tool arguments"
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 hook/started 超时")
    .expect("处理 hook/started 失败");

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "hook/completed".to_string(),
                params: json!({
                    "threadId": "thread-render-hook",
                    "turnId": "turn-hook",
                    "run": {
                        "id": "hook-run-1",
                        "eventName": "preToolUse",
                        "status": "completed",
                        "statusMessage": "checking tool arguments",
                        "entries": [
                            {
                                "kind": "feedback",
                                "text": "validated"
                            }
                        ]
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 hook/completed 超时")
    .expect("处理 hook/completed 失败");

    let snapshots = state
        .thread_render_snapshots
        .lock()
        .expect("thread render snapshots poisoned");
    let snapshot = snapshots
        .get("thread-render-hook")
        .expect("应缓存 hook thread render snapshot");
    let hook_nodes = snapshot
        .nodes
        .iter()
        .filter_map(|node| match node {
            crate::bridge_protocol::ThreadRenderNode::HookEvent {
                title,
                state,
                detail_lines,
                ..
            } => Some((title.clone(), state.clone(), detail_lines.clone())),
            _ => None,
        })
        .collect::<Vec<_>>();
    assert_eq!(1, hook_nodes.len());
    assert_eq!("PreToolUse hook (completed)", hook_nodes[0].0);
    assert_eq!("completed", hook_nodes[0].1);
    assert_eq!(
        vec![
            "checking tool arguments".to_string(),
            "feedback: validated".to_string(),
        ],
        hook_nodes[0].2,
    );
}

#[tokio::test]
async fn terminal_interaction_notification_preserves_command_and_input() {
    let state = bootstrap_test_state().await;

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "item/completed".to_string(),
                params: json!({
                    "threadId": "thread-render-terminal",
                    "turnId": "turn-terminal",
                    "item": {
                        "id": "item-exec-terminal",
                        "type": "commandExecution",
                        "status": "completed",
                        "command": "python manage.py migrate"
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 terminal command item/completed 超时")
    .expect("处理 terminal command item/completed 失败");

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "item/commandExecution/terminalInteraction".to_string(),
                params: json!({
                    "threadId": "thread-render-terminal",
                    "turnId": "turn-terminal",
                    "itemId": "item-exec-terminal",
                    "stdin": "yes\ncontinue\n"
                }),
            },
        ),
    )
    .await
    .expect("处理 terminalInteraction 超时")
    .expect("处理 terminalInteraction 失败");

    let snapshots = state
        .thread_render_snapshots
        .lock()
        .expect("thread render snapshots poisoned");
    let snapshot = snapshots
        .get("thread-render-terminal")
        .expect("应缓存 terminal thread render snapshot");
    let terminal = snapshot
        .nodes
        .iter()
        .find_map(|node| match node {
            crate::bridge_protocol::ThreadRenderNode::TerminalInteraction {
                title,
                command,
                stdin,
                waited,
                ..
            } => Some((title.clone(), command.clone(), stdin.clone(), *waited)),
            _ => None,
        })
        .expect("应存在 terminal interaction 节点");
    assert_eq!("Interacted with background terminal", terminal.0);
    assert_eq!(Some("python manage.py migrate".to_string()), terminal.1);
    assert_eq!("yes\ncontinue\n".to_string(), terminal.2);
    assert!(!terminal.3);
}

#[tokio::test]
async fn approval_review_notifications_upsert_single_render_node() {
    let state = bootstrap_test_state().await;

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "item/autoApprovalReview/started".to_string(),
                params: json!({
                    "threadId": "thread-render-approval",
                    "turnId": "turn-approval",
                    "targetItemId": "item-command-1",
                    "reviewId": "review-1",
                    "action": {
                        "type": "command",
                        "command": "git status",
                        "cwd": "/workspace"
                    },
                    "review": {
                        "status": "inProgress",
                        "riskLevel": "medium"
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 approval review started 超时")
    .expect("处理 approval review started 失败");

    timeout(
        Duration::from_secs(2),
        handle_app_server_message(
            &state,
            AppServerInbound::Notification {
                runtime_id: PRIMARY_RUNTIME_ID.to_string(),
                method: "item/autoApprovalReview/completed".to_string(),
                params: json!({
                    "threadId": "thread-render-approval",
                    "turnId": "turn-approval",
                    "targetItemId": "item-command-1",
                    "reviewId": "review-1",
                    "decisionSource": "guardian",
                    "action": {
                        "type": "command",
                        "command": "git status",
                        "cwd": "/workspace"
                    },
                    "review": {
                        "status": "approved",
                        "riskLevel": "medium",
                        "rationale": "matches allowlist"
                    }
                }),
            },
        ),
    )
    .await
    .expect("处理 approval review completed 超时")
    .expect("处理 approval review completed 失败");

    let snapshots = state
        .thread_render_snapshots
        .lock()
        .expect("thread render snapshots poisoned");
    let snapshot = snapshots
        .get("thread-render-approval")
        .expect("应缓存 approval thread render snapshot");
    let approval_nodes = snapshot
        .nodes
        .iter()
        .filter_map(|node| match node {
            crate::bridge_protocol::ThreadRenderNode::ApprovalReview {
                title,
                state,
                detail_lines,
                ..
            } => Some((title.clone(), state.clone(), detail_lines.clone())),
            _ => None,
        })
        .collect::<Vec<_>>();
    assert_eq!(1, approval_nodes.len());
    assert_eq!("Reviewed command approval request", approval_nodes[0].0);
    assert_eq!("approved", approval_nodes[0].1);
    assert_eq!(
        vec![
            "command: git status @ /workspace".to_string(),
            "status: approved".to_string(),
            "risk: medium".to_string(),
            "matches allowlist".to_string(),
            "decision source: guardian".to_string(),
        ],
        approval_nodes[0].2,
    );
}