rpi-cli 0.35.0

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! Real native RPC process coverage. All config, homes and JSONL sessions are
//! synthetic. Manual-access tests never prompt; resource acceptance uses only a
//! controlled local provider. No recovery or tool replay is requested.
#[path = "support/native_durable_context.rs"]
mod context;
#[path = "support/native_durable_pages.rs"]
mod pages;
#[path = "support/resource_fixture.rs"]
mod resource_fixture;
#[path = "support/native_durable_resources.rs"]
mod resources;
#[path = "support/native_durable_shutdown.rs"]
mod shutdown;
use serde_json::{json, Value};
use std::{
    io::{BufRead, BufReader, Read, Write},
    net::TcpListener,
    path::{Path, PathBuf},
    process::{Child, ChildStdin, Command, Stdio},
    sync::{mpsc, Arc, Mutex},
    thread::{self, JoinHandle},
    time::{Duration, Instant},
};

const WAIT: Duration = Duration::from_secs(25);
const STDERR_LIMIT: usize = 16 * 1024;

struct Fixture {
    root: tempfile::TempDir,
    provider: TcpListener,
}
impl Fixture {
    fn new() -> Self {
        let root = tempfile::tempdir().unwrap();
        for directory in ["agent", "home", "project", "sessions", "config", "data"] {
            std::fs::create_dir_all(root.path().join(directory)).unwrap();
        }
        // A local tripwire, not an HTTP server: even an accidental provider
        // connection is observable without contacting an external service.
        let provider = TcpListener::bind("127.0.0.1:0").unwrap();
        provider.set_nonblocking(true).unwrap();
        std::fs::write(
            root.path().join("agent/models.json"),
            json!({"providers":{"fixture":{
                "api":"openai-completions",
                "baseUrl":format!("http://{}/v1", provider.local_addr().unwrap()),
                "apiKey":"synthetic-test-key",
                "models":[{"id":"test","contextWindow":131072,"maxTokens":256}]
            }}})
            .to_string(),
        )
        .unwrap();
        std::fs::write(root.path().join("agent/auth.json"), "{}").unwrap();
        std::fs::write(
            root.path().join("agent/settings.json"),
            r#"{"retry":{"enabled":false}}"#,
        )
        .unwrap();
        Self { root, provider }
    }

    // Resource tests supply their own explicit selection/discovery policy.
    fn command(&self, enabled: bool, extra: &[&str]) -> Command {
        let root = self.root.path();
        let mut command = Command::new(env!("CARGO_BIN_EXE_rpi"));
        command.current_dir(root.join("project")).env_clear();
        // Retain only OS execution plumbing, never inherited provider/config
        // overrides or credentials. Override all home/config paths below.
        for name in [
            "PATH",
            "SystemRoot",
            "WINDIR",
            "COMSPEC",
            "PATHEXT",
            "TEMP",
            "TMP",
        ] {
            if let Some(value) = std::env::var_os(name) {
                command.env(name, value);
            }
        }
        command
            .env("HOME", root.join("home"))
            .env("USERPROFILE", root.join("home"))
            .env("APPDATA", root.join("config"))
            .env("LOCALAPPDATA", root.join("data"))
            .env("XDG_CONFIG_HOME", root.join("config"))
            .env("XDG_DATA_HOME", root.join("data"))
            .env("RPI_CODING_AGENT_DIR", root.join("agent"))
            .env("RPI_CODING_AGENT_SESSION_DIR", root.join("sessions"))
            .env("RPI_EXTENSIONS_DIR", root.join("agent/extensions"))
            .args([
                "--offline",
                "--provider",
                "fixture",
                "--model",
                "test",
                "--mode",
                "rpc",
            ]);
        if enabled {
            command.arg("--native-durable");
        } else {
            command.arg("--no-native-durable");
        }
        command.args(extra);
        command
    }

    fn start(&self, enabled: bool, extra: &[&str]) -> Host {
        let mut command = self.command(
            enabled,
            &[
                "--package-only",
                "--no-extensions",
                "--no-skills",
                "--no-context-files",
                "--no-prompt-templates",
            ],
        );
        let mut host = Host::spawn(command.args(extra));
        let ready = host.until(|v| v["type"] == "ready");
        assert_eq!(ready["durable"]["enabled"], enabled, "{ready}");
        assert_eq!(ready["durable"]["version"], 1, "{ready}");
        assert_eq!(ready["durable"]["prototype"], true, "{ready}");
        host
    }

    fn session_file(&self) -> PathBuf {
        fn collect(directory: &Path, files: &mut Vec<PathBuf>) {
            for entry in std::fs::read_dir(directory).unwrap() {
                let path = entry.unwrap().path();
                if path.is_dir() {
                    collect(&path, files);
                } else if path
                    .extension()
                    .is_some_and(|extension| extension == "jsonl")
                    && !path
                        .file_name()
                        .unwrap()
                        .to_string_lossy()
                        .ends_with(".progress.jsonl")
                {
                    files.push(path);
                }
            }
        }
        let mut files = Vec::new();
        collect(&self.root.path().join("sessions"), &mut files);
        assert_eq!(files.len(), 1, "test-owned JSONL sessions: {files:?}");
        files.pop().unwrap()
    }

    fn assert_no_provider_calls(&self) {
        match self.provider.accept() {
            Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {}
            other => panic!("manual durable access contacted the provider: {other:?}"),
        }
    }
}

struct Host {
    child: Child,
    input: Option<ChildStdin>,
    output: Option<mpsc::Receiver<String>>,
    readers: Vec<JoinHandle<()>>,
    stderr: Arc<Mutex<Vec<u8>>>,
    next_id: u64,
}
impl Host {
    fn spawn(command: &mut Command) -> Self {
        let mut child = command
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .unwrap();
        let input = child.stdin.take().unwrap();
        let stdout = child.stdout.take().unwrap();
        let mut error_pipe = child.stderr.take().unwrap();
        let (sender, receiver) = mpsc::sync_channel(64);
        let output_reader = thread::spawn(move || {
            for line in BufReader::new(stdout).lines() {
                let Ok(line) = line else { break };
                if sender.send(line).is_err() {
                    break;
                }
            }
        });
        let stderr = Arc::new(Mutex::new(Vec::new()));
        let tail = Arc::clone(&stderr);
        let error_reader = thread::spawn(move || {
            let mut chunk = [0; 4096];
            while let Ok(count) = error_pipe.read(&mut chunk) {
                if count == 0 {
                    break;
                }
                let mut bytes = tail.lock().unwrap();
                bytes.extend_from_slice(&chunk[..count]);
                let excess = bytes.len().saturating_sub(STDERR_LIMIT);
                bytes.drain(..excess);
            }
        });
        Self {
            child,
            input: Some(input),
            output: Some(receiver),
            readers: vec![output_reader, error_reader],
            stderr,
            next_id: 0,
        }
    }

    fn until(&mut self, predicate: impl Fn(&Value) -> bool) -> Value {
        let deadline = Instant::now() + WAIT;
        loop {
            let result = self
                .output
                .as_ref()
                .unwrap()
                .recv_timeout(deadline.saturating_duration_since(Instant::now()));
            let line = result.unwrap_or_else(|error| {
                panic!(
                    "native RPC response timeout/EOF ({error}); stderr: {}",
                    String::from_utf8_lossy(&self.stderr.lock().unwrap())
                )
            });
            let value: Value = serde_json::from_str(&line)
                .unwrap_or_else(|error| panic!("non-JSON native stdout ({error}): {line}"));
            assert!(
                !matches!(
                    value["type"].as_str(),
                    Some("agent_start" | "tool_execution_start")
                ),
                "durable access must not start an agent or tool: {value}"
            );
            if predicate(&value) {
                return value;
            }
            assert!(
                Instant::now() < deadline,
                "native RPC emitted events without a response"
            );
        }
    }

    fn raw(&mut self, line: &str, id: &Value) -> Value {
        let input = self.input.as_mut().unwrap();
        writeln!(input, "{line}").unwrap();
        input.flush().unwrap();
        self.until(|value| value["type"] == "response" && &value["id"] == id)
    }

    fn request(&mut self, kind: &str, mut fields: Value) -> Value {
        self.next_id += 1;
        let id = json!(format!("transport-{}", self.next_id));
        fields["type"] = json!(kind);
        fields["id"] = id.clone();
        self.raw(&fields.to_string(), &id)
    }

    fn data(&mut self, kind: &str, fields: Value) -> Value {
        let response = self.request(kind, fields);
        assert_eq!(response["success"], true, "{response}");
        response["data"].clone()
    }

    fn state(&mut self) -> Value {
        let data = self.data("get_durable_state", json!({"version":1}));
        assert_eq!(data["version"], 1, "{data}");
        let state = data["state"].clone();
        for field in ["scope", "state", "executionControl"] {
            assert!(state.get(field).is_some(), "missing {field}: {state}");
        }
        state
    }

    fn mutate(&mut self, request_id: &str, action: Value) -> Value {
        let scope = self.state()["scope"].clone();
        self.data("durable_mutate", mutation(request_id, scope, action))["outcome"].clone()
    }

    fn assert_idle(&mut self) {
        let state = self.data("get_state", json!({}));
        assert!(state["activeOperation"].is_null(), "{state}");
        let messages = self.data("get_messages", json!({}));
        assert_eq!(messages["messages"], json!([]), "{messages}");
    }
}
impl Drop for Host {
    fn drop(&mut self) {
        // Closing the receiver also releases a reader blocked on its bounded
        // channel. Always kill/reap, including assertion and timeout paths.
        self.input.take();
        self.output.take();
        let _ = self.child.kill();
        let _ = self.child.wait();
        for reader in self.readers.drain(..) {
            let _ = reader.join();
        }
    }
}

fn mutation(request_id: &str, scope: Value, action: Value) -> Value {
    json!({"version":1,"requestId":request_id,"expected":scope,"action":action})
}
fn start_action() -> Value {
    json!({"action":"start","name":"synthetic workflow",
        "steps":[{"id":"verify","title":"Verify persistence"}]})
}
fn assert_error(response: &Value, code: &str) {
    assert_eq!(response["success"], false, "{response}");
    assert_eq!(response["errorDetails"]["code"], code, "{response}");
    assert!(response.get("data").is_none(), "{response}");
}
fn has_name(data: &Value, field: &str, name: &str) -> bool {
    data[field]
        .as_array()
        .unwrap()
        .iter()
        .any(|value| value == name || value["name"] == name)
}

#[test]
fn session_selectors_report_a_damaged_exact_id() {
    let fixture = Fixture::new();
    let id = "audit_id";
    let prefix_match = "audit_id-extra";
    let seed = fixture.start(false, &["--session-id", prefix_match]);
    let journal = fixture.session_file();
    drop(seed);

    let other_project = fixture.root.path().join("sessions/--other-project--");
    std::fs::create_dir(&other_project).unwrap();
    std::fs::write(
        other_project.join("2020-01-01T00-00-00-000Z_other_audit_id.jsonl"),
        [0xff, b'\n'],
    )
    .unwrap();
    let mut host = fixture.start(false, &["--session", id]);
    assert_eq!(host.data("get_state", json!({}))["sessionId"], prefix_match);
    drop(host);

    let damaged_name = "2020-01-01T00-00-01-000Z_audit_id.jsonl";
    let damaged = other_project.join(damaged_name);
    let invalid_headers: &[&[u8]] = &[&[0xff, b'\n'], b"{\n", b""];
    for &header in invalid_headers {
        std::fs::write(&damaged, header).unwrap();
        for flag in ["--session", "--session-id"] {
            let output = fixture
                .command(false, &["--no-extensions", flag, id])
                .output()
                .unwrap();
            let stderr = String::from_utf8_lossy(&output.stderr);
            assert!(!output.status.success(), "{flag} accepted {header:?}");
            assert!(stderr.contains(damaged_name), "{flag}: {stderr}");
        }
        assert_eq!(std::fs::read(&damaged).unwrap(), header);
        assert!(!std::fs::read_dir(journal.parent().unwrap())
            .unwrap()
            .any(|entry| entry
                .unwrap()
                .file_name()
                .to_string_lossy()
                .ends_with("_audit_id.jsonl")));
    }
    fixture.assert_no_provider_calls();
}

#[test]
fn session_selectors_ignore_a_damaged_backup_of_a_healthy_session() {
    let fixture = Fixture::new();
    let id = "audit_id";
    let seed = fixture.start(false, &["--session-id", id]);
    let journal = fixture.session_file();
    drop(seed);

    let other_project = fixture.root.path().join("sessions/--other-project--");
    std::fs::create_dir(&other_project).unwrap();
    let backup = other_project.join("notes_audit_id.jsonl");
    std::fs::write(&backup, [0xff, b'\n']).unwrap();

    for flag in ["--session", "--session-id"] {
        let mut host = fixture.start(false, &[flag, id]);
        let state = host.data("get_state", json!({}));
        assert_eq!(state["sessionId"], id);
    }
    std::fs::remove_file(backup).unwrap();
    assert_eq!(fixture.session_file(), journal);
    fixture.assert_no_provider_calls();
}

#[test]
fn startup_plugin_error_preserves_rpc_stdout() {
    let fixture = Fixture::new();
    let extensions = fixture.root.path().join("broken-extensions");
    std::fs::create_dir(&extensions).unwrap();
    let filename = format!("broken-fixture{}", std::env::consts::DLL_SUFFIX);
    std::fs::write(extensions.join(&filename), "not a dynamic library").unwrap();

    let output = fixture
        .command(
            false,
            &[
                "--no-session",
                "--extensions-dir",
                extensions.to_str().unwrap(),
            ],
        )
        .output()
        .unwrap();
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(output.status.success(), "{stderr}");
    assert!(
        stderr.contains("skipped plugin") && stderr.contains(&filename),
        "{stderr}"
    );
    let frames: Vec<Value> = String::from_utf8(output.stdout)
        .unwrap()
        .lines()
        .map(|line| serde_json::from_str(line).unwrap())
        .collect();
    assert_eq!(frames[0]["type"], "ready");
    fixture.assert_no_provider_calls();
}

#[test]
fn native_durable_can_be_disabled_with_the_transition_flag() {
    let fixture = Fixture::new();
    let mut host = fixture.start(false, &["--no-session", "--no-tools"]);
    for (kind, fields) in [
        ("get_durable_state", json!({"version":1})),
        (
            "get_durable_request",
            json!({"version":1,"requestId":"absent"}),
        ),
        (
            "durable_mutate",
            mutation("disabled-start", json!({}), start_action()),
        ),
    ] {
        assert_error(&host.request(kind, fields), "disabled");
    }
    let tools = host.data("get_available_tools", json!({}));
    assert!(!has_name(&tools, "tools", "durable"));
    let commands = host.data("get_commands", json!({}));
    assert!(!has_name(&commands, "commands", "durable"));
    host.assert_idle();
    drop(host);
    fixture.assert_no_provider_calls();
}

#[test]
fn guarded_mutations_deduplicate_queue_and_survive_process_death() {
    let fixture = Fixture::new();
    let mut host = fixture.start(
        true,
        &[
            "--session-id",
            "native-durable-lifecycle",
            "--tools",
            "durable",
        ],
    );
    let tools = host.data("get_available_tools", json!({}));
    assert_eq!(tools["tools"], json!(["durable"]), "{tools}");
    assert_eq!(tools["activeTools"], json!(["durable"]), "{tools}");
    let commands = host.data("get_commands", json!({}));
    assert!(has_name(&commands, "commands", "durable"));
    let initial = host.state();
    assert!(initial["state"]["workflow"].is_null(), "{initial}");
    let start = mutation("start-once", initial["scope"].clone(), start_action());
    let first = host.data("durable_mutate", start.clone())["outcome"].clone();
    assert_eq!(first["replayed"], false);
    assert_eq!(first["snapshot"]["state"]["workflow"]["status"], "running");
    let replay = host.data("durable_mutate", start)["outcome"].clone();
    assert_eq!(replay["replayed"], true);
    assert_eq!(replay["receipt"], first["receipt"]);
    assert_eq!(replay["snapshot"], first["snapshot"]);
    assert_error(
        &host.request(
            "durable_mutate",
            mutation(
                "stale-enqueue",
                initial["scope"].clone(),
                json!({"action":"enqueue","text":"stale"}),
            ),
        ),
        "conflict",
    );
    let receipt = host.data(
        "get_durable_request",
        json!({"version":1,"requestId":"start-once"}),
    );
    assert_eq!(receipt["receipt"], first["receipt"]);
    assert_eq!(receipt["receipt"]["actor"], "native-cli");
    let slash_receipt = host.data("execute_command", json!({"command":"durable", "args":json!({"action":"status", "requestId":"start-once"}).to_string()}));
    assert_eq!(slash_receipt, receipt);

    let enqueue = host.mutate(
        "enqueue-once",
        json!({"action":"enqueue","text":"manual work"}),
    );
    let item = enqueue["snapshot"]["state"]["queue"][0]["id"].clone();
    assert!(item.is_string(), "{enqueue}");
    let claim = host.mutate("claim-once", json!({"action":"claim","item":item}));
    assert_eq!(claim["snapshot"]["state"]["queue"][0]["status"], "claimed");
    let finish = host.mutate(
        "finish-once",
        json!({"action":"finish","item":item,"status":"completed"}),
    );
    assert_eq!(
        finish["snapshot"]["state"]["queue"][0]["status"],
        "completed"
    );
    for custom_type in ["rpi.durable.control.v1", "rpi.durable.forged.v99"] {
        let response = host.request(
            "append_entry",
            json!({"customType":custom_type,"data":{"forged":true}}),
        );
        assert_eq!(response["success"], false, "{response}");
    }
    let saved = host.state();
    let slash = host.data("execute_command", json!({"command":"durable","args":""}));
    assert_eq!(slash["state"], saved);
    host.assert_idle();
    let session_id = host.data("get_state", json!({}))["sessionId"].clone();
    assert_eq!(session_id, "native-durable-lifecycle");
    let session_file = fixture.session_file();
    // No stop handshake: prove already-acknowledged writes survive process death.
    drop(host);
    let mut reopened = fixture.start(
        true,
        &[
            "--session",
            session_file.to_str().unwrap(),
            "--tools",
            "durable",
        ],
    );
    assert_eq!(
        reopened.data("get_state", json!({}))["sessionId"],
        session_id
    );
    let restored = reopened.state();
    assert_eq!(restored["state"], saved["state"]);
    assert_eq!(restored["executionControl"], saved["executionControl"]);
    assert_eq!(
        restored["scope"]["controlRevision"],
        saved["scope"]["controlRevision"]
    );
    // bindingToken is intentionally process-local; do not reuse an old guard
    // or issue a second start. Query committed receipts without replay instead.
    assert_eq!(
        reopened.data(
            "get_durable_request",
            json!({
                "version":1,"requestId":"start-once"
            })
        )["receipt"],
        first["receipt"]
    );
    assert_eq!(
        reopened.data(
            "get_durable_request",
            json!({
                "version":1,"requestId":"finish-once"
            })
        )["receipt"],
        finish["receipt"]
    );
    reopened.assert_idle();
    drop(reopened);
    fixture.assert_no_provider_calls();
}

#[test]
fn strict_wire_validation_rejects_duplicates_and_bad_guards_before_mutating() {
    let fixture = Fixture::new();
    let mut host = fixture.start(true, &["--no-session", "--no-tools"]);
    let before = host.state();
    let valid = mutation("strict-start", before["scope"].clone(), start_action());
    let mut bad = Vec::new();
    for version in [
        Value::Null,
        json!(0),
        json!(2),
        json!(1.0),
        json!("1"),
        json!(true),
    ] {
        let mut request = valid.clone();
        request["version"] = version;
        bad.push(request);
    }
    for missing in ["version", "requestId", "expected"] {
        let mut request = valid.clone();
        request.as_object_mut().unwrap().remove(missing);
        bad.push(request);
    }
    for missing in [
        "sessionId",
        "lane",
        "bindingToken",
        "leafId",
        "historyRevision",
        "entryId",
        "controlRevision",
    ] {
        let mut request = valid.clone();
        request["expected"].as_object_mut().unwrap().remove(missing);
        bad.push(request);
    }
    for field in ["actor", "__rpi", "unknown"] {
        let mut request = valid.clone();
        request[field] = json!({"spoofed":true});
        bad.push(request);
    }
    let mut unknown_guard = valid.clone();
    unknown_guard["expected"]["unknown"] = json!(true);
    bad.push(unknown_guard);
    let mut unknown_action = valid.clone();
    unknown_action["action"]["actor"] = json!("spoofed");
    bad.push(unknown_action);
    let mut unknown_step = valid.clone();
    unknown_step["action"]["steps"][0]["status"] = json!("completed");
    bad.push(unknown_step);
    for request in bad {
        assert_error(&host.request("durable_mutate", request), "invalid_request");
    }
    let scope = before["scope"].to_string();
    let duplicate_frames = [
        format!(
            r#"{{"type":"durable_mutate","id":"duplicate-top","version":1,"version":1,"requestId":"duplicate-top","expected":{scope},"action":{}}}"#,
            start_action()
        ),
        format!(
            r#"{{"type":"durable_mutate","id":"duplicate-action","version":1,"requestId":"duplicate-action","expected":{scope},"action":{{"action":"reset","action":"start","name":"bad","steps":[{{"id":"a","title":"A"}}]}}}}"#
        ),
        format!(
            r#"{{"type":"durable_mutate","id":"duplicate-step","version":1,"requestId":"duplicate-step","expected":{scope},"action":{{"action":"start","name":"bad","steps":[{{"id":"a","\u0069d":"b","title":"A"}}]}}}}"#
        ),
    ];
    for (raw, id) in
        duplicate_frames
            .iter()
            .zip(["duplicate-top", "duplicate-action", "duplicate-step"])
    {
        assert_error(&host.raw(raw, &json!(id)), "invalid_request");
        assert!(
            host.data("get_durable_request", json!({"version":1,"requestId":id}))["receipt"]
                .is_null()
        );
    }
    for raw in [
        format!(
            "{}{}",
            " ".repeat(64 * 1024),
            json!({"id":"padded","type":"durable_mutate","version":1,"requestId":"padded","expected":before["scope"],"action":start_action()})
        ),
        format!(
            "{}{}",
            json!({"id":"padded","type":"get_durable_state","version":1}),
            " ".repeat(64 * 1024)
        ),
    ] {
        assert_error(&host.raw(&raw, &json!("padded")), "limit_exceeded");
    }
    assert_eq!(
        host.state(),
        before,
        "invalid frames must not append durable state"
    );
    // A subsequent valid frame still works; malformed input did not poison RPC.
    let outcome = host.data("durable_mutate", valid);
    assert_eq!(outcome["outcome"]["replayed"], false);
    host.assert_idle();
    drop(host);
    fixture.assert_no_provider_calls();
}

#[test]
fn tool_filters_do_not_disable_explicit_native_rpc() {
    for flags in [
        vec!["--no-tools"],
        vec!["--no-builtin-tools"],
        vec!["--tools", "durable", "--exclude-tools", "durable"],
    ] {
        let fixture = Fixture::new();
        let mut args = vec!["--no-session"];
        args.extend(flags);
        let mut host = fixture.start(true, &args);
        let tools = host.data("get_available_tools", json!({}));
        assert!(!has_name(&tools, "tools", "durable"), "{tools}");
        assert!(!has_name(&tools, "activeTools", "durable"), "{tools}");
        let commands = host.data("get_commands", json!({}));
        let durable: Vec<_> = commands["commands"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|command| command["name"] == "durable")
            .collect();
        assert_eq!(
            durable.len(),
            1,
            "native discovery is host opt-in, not model-tool selection"
        );
        assert_eq!(
            durable[0]["description"],
            "Native durable workflow / explicit run view or guarded JSON mutation (v1 prototype)"
        );
        let outcome = host.mutate("manual-start", start_action());
        assert_eq!(
            outcome["snapshot"]["state"]["workflow"]["name"],
            "synthetic workflow"
        );
        let state = host.state();
        assert_eq!(
            host.data("execute_command", json!({"command":"durable","args":""}))["state"],
            state
        );
        host.assert_idle();
        drop(host);
        fixture.assert_no_provider_calls();
    }
}

#[test]
fn native_command_discovery_preserves_json_execution_and_rejects_non_string_args() {
    let fixture = Fixture::new();
    let mut host = fixture.start(true, &["--no-session", "--no-tools"]);
    let before = host.state();
    let commands = host.data("get_commands", json!({}));
    assert_eq!(
        commands["commands"]
            .as_array()
            .unwrap()
            .iter()
            .filter(|command| command["name"] == "durable")
            .count(),
        1
    );
    for args in [
        Value::Null,
        json!(42),
        json!(false),
        json!({"action":"status"}),
        json!([]),
    ] {
        let response = host.request("execute_command", json!({"command":"/durable","args":args}));
        assert_eq!(response["success"], false, "{response}");
        assert_eq!(response["error"], "invalid command payload", "{response}");
        assert!(response.get("data").is_none());
        assert!(
            response.get("errorDetails").is_none(),
            "retain the legacy outer Request error contract"
        );
        assert_eq!(
            host.state(),
            before,
            "malformed args must not be silently treated as a status read"
        );
    }
    // Help belongs only to the human TUI; RPC remains the flat-JSON API.
    assert_error(
        &host.request(
            "execute_command",
            json!({"command":"durable","args":"help"}),
        ),
        "invalid_request",
    );
    let default_status = host.data("execute_command", json!({"command":"durable"}));
    assert_eq!(default_status["version"], 1);
    assert_eq!(default_status["state"], before);
    let payload = json!({"action":"enqueue","requestId":"discovery-spaces","text":"keep  界  spaces
inside"});
    let outcome = host.data(
        "execute_command",
        json!({"command":"/durable","args":payload.to_string()}),
    );
    assert_eq!(outcome["version"], 1);
    assert_eq!(
        outcome["outcome"]["snapshot"]["state"]["queue"][0]["text"],
        payload["text"]
    );
    assert_eq!(outcome["outcome"]["replayed"], false);
    let receipt = host.data("execute_command", json!({"command":"durable","args":json!({"action":"status","requestId":"discovery-spaces"}).to_string()}));
    assert_eq!(receipt["receipt"]["requestId"], "discovery-spaces");
    assert_eq!(
        receipt["receipt"]["entryId"],
        outcome["outcome"]["receipt"]["entryId"]
    );
    let after = host.state();
    assert_eq!(after, outcome["outcome"]["snapshot"]);
    let _ = host.data("get_commands", json!({}));
    assert_eq!(
        host.state(),
        after,
        "command discovery must not execute, recover or write"
    );
    host.assert_idle();
    drop(host);
    fixture.assert_no_provider_calls();
}