saya-cli 0.4.1

Database-aware AI agent for the terminal: full-screen TUI, schema discovery, and bounded read-only SQL over PostgreSQL, MySQL, SQLite, DuckDB, and Snowflake.
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
//! Tests for the `workspace_edit` tool (`replace` variant only): the
//! model-facing anchored edit that resolves `old_text` to exactly one byte
//! range and commits through `Workspace::patch_range`. Every row of the
//! failure contract (§4) is a named property here; every refusal leaves the
//! file byte-identical.

use std::{fs, path::PathBuf, sync::Arc};

use super::*;
use saya_agent::{
    AgentEvent, AgentEventSink, AgentLimits, AgentRequest, LocalStateEffect, ToolCall,
    ToolDefinition, ToolExecutor,
};
use saya_harness::workspace::Workspace;

use super::database_tools::WORKSPACE_EDIT_MAX_BYTES;

/// A sandbox workspace under the OS temp dir, removed on drop. The root sits
/// one level down (`outer/ws`) so a `..` escape has a real target to name.
struct Sandbox {
    outer: PathBuf,
    ws: Workspace,
}

impl Sandbox {
    fn new(label: &str) -> Self {
        let outer =
            std::env::temp_dir().join(format!("saya-wsedit-{label}-{}", std::process::id()));
        let _ = fs::remove_dir_all(&outer);
        fs::create_dir_all(outer.join("ws")).expect("sandbox directory must create");
        let ws = Workspace::open(&outer.join("ws")).expect("workspace root must open");
        Self { outer, ws }
    }

    fn ws_root(&self) -> PathBuf {
        self.outer.join("ws")
    }

    /// Database tools with the sandbox workspace attached and no connections —
    /// a workspace-only run has no selected profile.
    fn tools(&self) -> DatabaseTools {
        DatabaseTools::with_registry(
            crate::connection::ConnectionRegistry::new("primary"),
            100,
            true,
            None,
        )
        .with_workspace(Some(Arc::new(self.ws.clone())))
    }
}

impl Drop for Sandbox {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.outer);
    }
}

/// The real definition, taken from the advertised list: the loop tests must
/// exercise the gate against the definition the model would actually see.
fn workspace_edit_definition() -> ToolDefinition {
    DatabaseTools::definitions(false, false, false, true, false)
        .into_iter()
        .find(|tool| tool.name == "workspace_edit")
        .expect("workspace_edit is advertised when workspace writes are permitted")
}

fn edit_call(path: &str, old_text: &str, new_text: &str) -> ToolCall {
    ToolCall {
        id: "c1".into(),
        name: "workspace_edit".into(),
        arguments: serde_json::json!({"path": path, "old_text": old_text, "new_text": new_text}),
    }
}

/// A provider that emits one tool call on its first `stream` invocation and a
/// plain text answer afterwards, so the run completes instead of looping on
/// the turn limit. Mirrors the harness in `workspace_write_tests`.
struct OneCallProvider {
    call: ToolCall,
    turn: std::sync::Mutex<u32>,
}

#[async_trait::async_trait]
impl saya_agent::ChatProvider for OneCallProvider {
    fn name(&self) -> &str {
        "one-call-mock"
    }
    async fn complete(
        &self,
        _: saya_agent::ChatRequest,
    ) -> Result<saya_agent::ChatResponse, saya_agent::ProviderError> {
        unreachable!("stream path is used")
    }
    async fn stream(
        &self,
        _: saya_agent::ChatRequest,
        _: saya_agent::CancellationToken,
    ) -> Result<saya_agent::ProviderStream, saya_agent::ProviderError> {
        let first = {
            let mut turn = self.turn.lock().unwrap();
            let was = *turn;
            *turn += 1;
            was == 0
        };
        let events = if first {
            vec![
                Ok(saya_agent::ProviderEvent::ToolCalls(vec![
                    self.call.clone(),
                ])),
                Ok(saya_agent::ProviderEvent::Done),
            ]
        } else {
            vec![
                Ok(saya_agent::ProviderEvent::TextDelta("done".into())),
                Ok(saya_agent::ProviderEvent::Done),
            ]
        };
        Ok(Box::pin(futures_util::stream::iter(events)))
    }
}

struct RecordingSink {
    events: std::sync::Arc<std::sync::Mutex<Vec<AgentEvent>>>,
}

#[async_trait::async_trait]
impl AgentEventSink for RecordingSink {
    async fn emit(&self, event: AgentEvent) {
        self.events.lock().unwrap().push(event);
    }
}

fn request() -> AgentRequest {
    AgentRequest {
        prompt: "edit a file".into(),
        profile_names: Vec::new(),
        model: "mock-model".into(),
        system_prompt: None,
        history: Vec::new(),
        context_blocks: Vec::new(),
    }
}

/// Zero matches: the tool refuses, names the count, and writes nothing.
#[tokio::test]
async fn zero_matches_writes_nothing() {
    let sandbox = Sandbox::new("zero");
    sandbox
        .ws
        .write("notes.md", b"hello workspace\n")
        .expect("seed write must succeed");
    let before = fs::read(sandbox.ws_root().join("notes.md")).expect("seed file must exist");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "no such anchor anywhere",
                "new_text": "replacement",
            }),
        )
        .await
        .expect_err("zero matches must refuse, not write");
    let text = error.to_string();
    assert!(
        text.contains("matched 0"),
        "the refusal must name the count, got: {text}"
    );
    assert!(
        text.contains("size:") && text.contains("digest:"),
        "the refusal names the current size and digest so the model can re-anchor: {text}"
    );
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        before,
        "a refused edit leaves the file byte-identical"
    );
}

/// Multiple matches: the tool refuses with bounded line numbers — never
/// "first wins" — and writes nothing.
#[tokio::test]
async fn multiple_matches_writes_nothing_and_reports_the_lines() {
    let sandbox = Sandbox::new("multi");
    let seed = b"line one: needle here\nline two: filler\nline three: needle here\n";
    sandbox
        .ws
        .write("notes.md", seed)
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "needle here",
                "new_text": "replacement",
            }),
        )
        .await
        .expect_err("multiple matches must refuse, not pick the first");
    match error {
        saya_agent::ToolError::WorkspaceEditAmbiguous { matches, lines, .. } => {
            assert_eq!(matches, 2, "the refusal must name the count");
            assert_eq!(
                lines,
                vec![1, 3],
                "the refusal reports bounded line numbers"
            );
        }
        other => panic!("expected a typed ambiguity refusal, got: {other}"),
    }
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        seed,
        "a refused edit leaves the file byte-identical"
    );
}

/// A moved anchor: an `expected_size`/`expected_digest` mismatch against the
/// file's current state refuses with no write.
#[tokio::test]
async fn a_moved_anchor_refuses_on_the_precondition() {
    let sandbox = Sandbox::new("moved");
    sandbox
        .ws
        .write("notes.md", b"version one: anchor\n")
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    // The model measured a stale size; the file no longer has it.
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "anchor",
                "new_text": "replacement",
                "expected_size": 9999,
            }),
        )
        .await
        .expect_err("a stale size precondition must refuse");
    let text = error.to_string();
    assert!(
        text.contains("changed since measured"),
        "the refusal must name the moved anchor: {text}"
    );
    // The model measured a stale digest; same refusal, no write.
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "anchor",
                "new_text": "replacement",
                "expected_digest": "deadbeef",
            }),
        )
        .await
        .expect_err("a stale digest precondition must refuse");
    let text = error.to_string();
    assert!(
        text.contains("changed since measured"),
        "the refusal must name the moved anchor: {text}"
    );
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        b"version one: anchor\n",
        "a refused edit leaves the file byte-identical"
    );
}

/// An empty anchor matches everywhere, so it is a validation error — never
/// an edit, never a write.
#[tokio::test]
async fn an_empty_anchor_is_rejected() {
    let sandbox = Sandbox::new("empty");
    sandbox
        .ws
        .write("notes.md", b"hello workspace\n")
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "",
                "new_text": "replacement",
            }),
        )
        .await
        .expect_err("an empty anchor must be rejected");
    assert_eq!(
        error,
        saya_agent::ToolError::WorkspaceEditEmptyAnchor {
            path: "notes.md".into(),
        }
    );
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        b"hello workspace\n",
        "a refused edit leaves the file byte-identical"
    );
}

/// A replacement over the bound refuses whole — never truncated — and leaves
/// no partial edit behind.
#[tokio::test]
async fn a_replacement_over_the_bound_refuses_whole() {
    let sandbox = Sandbox::new("bound");
    sandbox
        .ws
        .write("notes.md", b"hello anchor world\n")
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    let oversized = "a".repeat(WORKSPACE_EDIT_MAX_BYTES + 1);
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "anchor",
                "new_text": oversized,
            }),
        )
        .await
        .expect_err("over the bound must be refused whole");
    match error {
        saya_agent::ToolError::WorkspaceEditTooLarge { limit, found, .. } => {
            assert_eq!(limit, WORKSPACE_EDIT_MAX_BYTES);
            assert_eq!(found, WORKSPACE_EDIT_MAX_BYTES + 1);
        }
        other => panic!("expected a typed over-bound refusal, got: {other}"),
    }
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        b"hello anchor world\n",
        "a refused edit leaves the file byte-identical"
    );
    // The boundary case: exactly the bound is a legal replacement.
    let exact = "a".repeat(WORKSPACE_EDIT_MAX_BYTES);
    let result = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "anchor",
                "new_text": exact,
            }),
        )
        .await
        .expect("a replacement exactly at the bound must be accepted");
    assert_eq!(result["path"], "notes.md");
}

/// An over-bound anchor refuses whole too: the range is never resolved, the
/// file never opened for a splice.
#[tokio::test]
async fn an_anchor_over_the_bound_refuses_whole() {
    let sandbox = Sandbox::new("anchor-bound");
    sandbox
        .ws
        .write("notes.md", b"hello workspace\n")
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    let oversized = "a".repeat(WORKSPACE_EDIT_MAX_BYTES + 1);
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": oversized,
                "new_text": "replacement",
            }),
        )
        .await
        .expect_err("an over-bound anchor must be refused whole");
    assert!(
        matches!(error, saya_agent::ToolError::WorkspaceEditTooLarge { .. }),
        "expected a typed over-bound refusal, got: {error}"
    );
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        b"hello workspace\n",
        "a refused edit leaves the file byte-identical"
    );
}

/// Errors carry counts, line numbers, sizes and digests — never unbounded
/// file content. A secret-shaped sentinel planted in the file must never
/// appear in any error payload.
#[tokio::test]
async fn an_error_payload_never_carries_file_content() {
    let sandbox = Sandbox::new("sentinel");
    // The sentinel is secret-shaped (`token=...`) so it would trip the
    // transcript redaction if it ever reached an error path; the contract is
    // stronger: it never reaches the payload at all.
    let sentinel = "token=SENTINEL-7f3a9c-must-never-leak";
    let seed = format!("header line\n{sentinel}\nfooter line\n");
    sandbox
        .ws
        .write("notes.md", seed.as_bytes())
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    // Zero matches: the refusal names the anchor argument (model-supplied),
    // never the file.
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "no such anchor",
                "new_text": "replacement",
            }),
        )
        .await
        .expect_err("zero matches must refuse");
    let text = error.to_string();
    assert!(
        !text.contains("SENTINEL-7f3a9c"),
        "a zero-match refusal must not carry file content: {text}"
    );
    // Multiple matches on a *different* anchor: the refusal names counts and
    // lines, never the sentinel line's content.
    sandbox
        .ws
        .write("dup.md", b"dup one\nfiller\ndup one\n")
        .expect("seed write must succeed");
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "dup.md",
                "old_text": "dup one",
                "new_text": "replacement",
            }),
        )
        .await
        .expect_err("multiple matches must refuse");
    let text = error.to_string();
    assert!(
        !text.contains("dup one"),
        "an ambiguity refusal reports lines, not excerpts: {text}"
    );
    // The sentinel file itself is untouched.
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        seed.as_bytes(),
        "a refused edit leaves the file byte-identical"
    );
}

/// The definition is hidden unless workspace writes are permitted: the loop's
/// gate keys on the declared `WriteWorkspace` effect, and a write tool the
/// model can see but never use wastes context and invites retries.
#[test]
fn the_tool_is_hidden_when_writes_are_not_permitted() {
    let hidden = DatabaseTools::definitions(false, false, false, false, false);
    assert!(
        !hidden.iter().any(|tool| tool.name == "workspace_edit"),
        "workspace_edit must be hidden when writes are not permitted"
    );
    let shown = DatabaseTools::definitions(false, false, false, true, false);
    let tool = shown
        .iter()
        .find(|tool| tool.name == "workspace_edit")
        .expect("workspace_edit must be advertised when writes are permitted");
    assert!(
        !tool.read_only,
        "workspace_edit writes a file; it is not read-only"
    );
    assert!(!tool.effect.database_data);
    assert!(!tool.effect.external_side_effect);
    assert!(
        !tool.effect.requires_approval,
        "the scope approval and the permit are the gate; no per-call prompt (D7)"
    );
    assert_eq!(tool.effect.local_state, LocalStateEffect::WriteWorkspace);
    assert_eq!(tool.parameters["required"], serde_json::json!(["path"]));
    let variants = tool.parameters["oneOf"]
        .as_array()
        .expect("workspace_edit schema must expose its variants with oneOf");
    assert_eq!(variants.len(), 2);
    assert!(
        variants
            .iter()
            .any(|variant| { variant["required"] == serde_json::json!(["old_text", "new_text"]) })
    );
    assert!(
        variants
            .iter()
            .any(|variant| { variant["required"] == serde_json::json!(["offset", "chunk"]) })
    );
    assert_eq!(tool.completion.as_deref(), Some("workspace file edited"));
}

/// Happy path: a successful edit changes only the matched range — the bytes
/// before and after the anchor are untouched — and reports the new size and
/// digest.
#[tokio::test]
async fn a_successful_edit_changes_only_the_matched_range() {
    let sandbox = Sandbox::new("happy");
    sandbox
        .ws
        .write("notes.md", b"hello anchor world\nsecond line\n")
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    let result = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "anchor",
                "new_text": "ANCHOR",
            }),
        )
        .await
        .expect("a unique anchor must edit");
    assert_eq!(result["path"], "notes.md");
    assert_eq!(result["bytes_replaced"], 6);
    assert_eq!(result["bytes_written"], 6);
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must exist"),
        b"hello ANCHOR world\nsecond line\n",
        "only the matched range changes; the bytes around it are untouched"
    );
    // The reported size and digest describe the file after the edit.
    let after = fs::read(sandbox.ws_root().join("notes.md")).expect("file must exist");
    assert_eq!(result["size"], after.len() as u64);
    let digest = result["digest"].as_str().expect("digest must be a string");
    assert_eq!(digest.len(), 64, "the digest is a sha256 hex string");
}

/// With `permit_workspace_writes: false` (the default), the loop denies the
/// call before the executor runs — asserted on the filesystem, not just the
/// event stream. The loop's effect-keyed gate (`workspace_write_denied`,
/// which keys on `WriteWorkspace`, not on the tool name) covers
/// `workspace_edit` with no loop change.
#[tokio::test]
async fn workspace_edit_is_denied_by_the_loop_by_default_and_writes_nothing() {
    let sandbox = Sandbox::new("loop-deny");
    sandbox
        .ws
        .write("notes.md", b"hello anchor world\n")
        .expect("seed write must succeed");
    let events = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let provider = OneCallProvider {
        call: edit_call("notes.md", "anchor", "ANCHOR"),
        turn: std::sync::Mutex::new(0),
    };
    let token = saya_agent::CancellationToken::new();
    let output = saya_agent::run_agent_with_sink(
        &provider,
        &sandbox.tools(),
        request(),
        vec![workspace_edit_definition()],
        AgentLimits::default(),
        &saya_agent::AllowReadOnlyApproval,
        &RecordingSink {
            events: events.clone(),
        },
        token,
    )
    .await
    .expect("denial is not a turn-ending error");
    let denied = events.lock().unwrap().iter().find_map(|event| match event {
        AgentEvent::ToolDenied { name, reason } => Some((name.clone(), reason.clone())),
        _ => None,
    });
    let (name, reason) = denied.expect("a ToolDenied event must be emitted");
    assert_eq!(name, "workspace_edit");
    assert!(
        reason.contains("workspace writes are not permitted"),
        "the denial must name the workspace gate, not approval: {reason}"
    );
    assert_eq!(output.tool_metadata[0].status, "denied");
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must survive"),
        b"hello anchor world\n",
        "no byte may change on a denied call"
    );
}

/// With the permit granted the loop runs the tool, and the file lands with
/// exactly the spliced bytes — end to end through the real executor.
#[tokio::test]
async fn workspace_edit_runs_in_the_loop_when_writes_are_permitted() {
    let sandbox = Sandbox::new("loop-allow");
    sandbox
        .ws
        .write("notes.md", b"hello anchor world\n")
        .expect("seed write must succeed");
    let provider = OneCallProvider {
        call: edit_call("notes.md", "anchor", "ANCHOR"),
        turn: std::sync::Mutex::new(0),
    };
    let token = saya_agent::CancellationToken::new();
    let limits = AgentLimits {
        permit_workspace_writes: true,
        ..AgentLimits::default()
    };
    let output = saya_agent::run_agent_with_sink(
        &provider,
        &sandbox.tools(),
        request(),
        vec![workspace_edit_definition()],
        limits,
        &saya_agent::AllowReadOnlyApproval,
        &RecordingSink {
            events: std::sync::Arc::new(std::sync::Mutex::new(Vec::new())),
        },
        token,
    )
    .await
    .expect("run completes");
    assert!(
        !output
            .events
            .iter()
            .any(|event| matches!(event, AgentEvent::ToolDenied { .. })),
        "no denial when workspace writes are permitted"
    );
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("the file must exist"),
        b"hello ANCHOR world\n",
        "the file must hold exactly the spliced bytes"
    );
}

/// No workspace attached (the state before a run engine opens one): the tool
/// denies with a typed error instead of pretending to edit.
#[tokio::test]
async fn workspace_edit_denies_when_no_workspace_is_attached() {
    let tools = DatabaseTools::with_registry(
        crate::connection::ConnectionRegistry::new("primary"),
        100,
        true,
        None,
    );
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "anchor",
                "new_text": "replacement",
            }),
        )
        .await
        .expect_err("no workspace, no edit");
    assert_eq!(error, saya_agent::ToolError::WorkspaceUnavailable);
}

/// Unknown arguments are rejected before anything else runs.
#[tokio::test]
async fn workspace_edit_rejects_unknown_arguments() {
    let sandbox = Sandbox::new("unknown-args");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "a",
                "new_text": "b",
                "sql": "SELECT 1",
            }),
        )
        .await
        .expect_err("unknown arguments must be rejected");
    assert_eq!(error, saya_agent::ToolError::UnsupportedProperty);
}

/// A non-string `old_text` is a typed error of its own, distinct from the
/// path's, so the model can fix the right argument. Same for `new_text` and
/// the optional `expected_*` precondition.
#[tokio::test]
async fn workspace_edit_rejects_mistyped_arguments() {
    let sandbox = Sandbox::new("arg-types");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({"path": "notes.md", "old_text": 7, "new_text": "b"}),
        )
        .await
        .expect_err("a non-string old_text must be rejected at validation");
    assert_eq!(error, saya_agent::ToolError::OldTextNotString);
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({"path": "notes.md", "old_text": "a", "new_text": 7}),
        )
        .await
        .expect_err("a non-string new_text must be rejected at validation");
    assert_eq!(error, saya_agent::ToolError::NewTextNotString);
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "a",
                "new_text": "b",
                "expected_size": "huge",
            }),
        )
        .await
        .expect_err("a non-integer expected_size must be rejected at validation");
    assert_eq!(error, saya_agent::ToolError::ExpectedSizeNotUint);
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "a",
                "new_text": "b",
                "expected_digest": 7,
            }),
        )
        .await
        .expect_err("a non-string expected_digest must be rejected at validation");
    assert_eq!(error, saya_agent::ToolError::ExpectedDigestNotString);
}

/// A non-UTF-8 target is refused loudly: reads are lossy, so a byte-exact
/// anchor cannot be trusted. Binary support is a later slice, not this one.
#[tokio::test]
async fn workspace_edit_refuses_a_non_utf8_target() {
    let sandbox = Sandbox::new("non-utf8");
    sandbox
        .ws
        .write("blob.bin", &[0x66, 0x6f, 0xff, 0xfe, 0x6f])
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "blob.bin",
                "old_text": "fo",
                "new_text": "bar",
            }),
        )
        .await
        .expect_err("a non-UTF-8 target must be refused");
    assert_eq!(
        error,
        saya_agent::ToolError::WorkspaceEditNotText {
            path: "blob.bin".into(),
        }
    );
    assert_eq!(
        fs::read(sandbox.ws_root().join("blob.bin")).expect("file must survive"),
        vec![0x66, 0x6f, 0xff, 0xfe, 0x6f],
        "a refused edit leaves the file byte-identical"
    );
}

/// A `..` escape is refused with the harness's containment reason, and the
/// sentinel planted outside the workspace is byte-for-byte unchanged.
#[tokio::test]
async fn workspace_edit_refuses_a_dot_dot_escape() {
    let sandbox = Sandbox::new("escape");
    fs::write(sandbox.outer.join("outside.txt"), b"sentinel").expect("sentinel plant must succeed");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "../outside.txt",
                "old_text": "sentinel",
                "new_text": "overwritten",
            }),
        )
        .await
        .expect_err("a path outside the workspace must be refused, not edited");
    let text = error.to_string();
    assert!(
        text.contains("escapes the run workspace"),
        "the refusal must name the containment failure: {text}"
    );
    assert_eq!(
        fs::read(sandbox.outer.join("outside.txt")).expect("sentinel must survive"),
        b"sentinel",
        "nothing may be written outside the workspace"
    );
}

// -- the redaction-placeholder guard: refuse an edit that would copy a -----
// -- masked tool-result marker back over the real value on disk. -----------

/// An edit that would replace the real value with the literal `[redacted]`
/// marker is refused — the same guard `workspace_write` applies — and the
/// file on disk keeps the real value.
#[tokio::test]
async fn workspace_edit_refuses_to_add_redaction_placeholders() {
    let sandbox = Sandbox::new("redaction-guard");
    let seed = b"a = f(token=real_value)\n";
    sandbox
        .ws
        .write("a.py", seed)
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    let error = tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "a.py",
                "old_text": "token=real_value",
                "new_text": "token=[redacted]",
            }),
        )
        .await
        .expect_err("an edit that adds a redaction placeholder must be refused");
    let text = error.to_string();
    assert!(
        text.contains("[redacted]"),
        "the refusal must name the marker: {text}"
    );
    assert_eq!(
        fs::read(sandbox.ws_root().join("a.py")).expect("file must survive"),
        seed,
        "a refused edit leaves the real value on disk untouched"
    );
}

/// A file that already holds the marker (e.g. an earlier legitimate write)
/// can still be edited as long as the edit does not grow the count: the
/// guard is a growth check, not a ban on the literal text.
#[tokio::test]
async fn a_file_that_already_holds_the_marker_can_still_be_edited() {
    let sandbox = Sandbox::new("marker-stays-flat");
    let seed = b"status: [redacted]\nother: line\n";
    sandbox
        .ws
        .write("notes.md", seed)
        .expect("seed write must succeed");
    let tools = sandbox.tools();
    tools
        .execute(
            "workspace_edit",
            serde_json::json!({
                "path": "notes.md",
                "old_text": "other: line",
                "new_text": "other: replaced",
            }),
        )
        .await
        .expect("the count stays at 1, so the edit must be allowed");
    assert_eq!(
        fs::read(sandbox.ws_root().join("notes.md")).expect("file must exist"),
        b"status: [redacted]\nother: replaced\n".to_vec(),
        "the edit must land; the pre-existing marker is untouched"
    );
}