symforge 7.9.2

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
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
//! Worktree-awareness acceptance tests (TDD red state).
//!
//! Mirrors §4 of the spec at `wiki/concepts/SymForge Worktree Awareness.md`
//! (Obsidian: [[SymForge Worktree Awareness]]) plus the expanded matrix in
//! its Implementation Notes (added 2026-04-17).
//!
//! Every test here is expected to fail until:
//!   - `src/worktree.rs` ships canonicalization, `git worktree list` cache,
//!     and `resolve_target_path`.
//!   - The 7 edit handlers listed in spec §2.1 accept `working_directory`
//!     and emit `wrote_to` / `indexed_path` / `rerouted` in their response.
//!   - `README.md` documents the parameter with one example.
//!
//! The feature is flagged behind `SYMFORGE_WORKTREE_AWARE=1`. Each test
//! enables the flag before building its fixture. The tests set env vars, so
//! they MUST run single-threaded — project `CLAUDE.md` already mandates
//! `--test-threads=1` for this crate.
//!
//! Harness shells out to the system `git` binary (version 2.5+ for
//! `git worktree add`). On dev machines and CI this is always present.
//!
//! Response-format assertions in this file (`rerouted: true`,
//! `WorkingDirectoryNotARecognizedWorktree`, `TargetFileMissing`, etc.) are
//! a TDD-driven *choice* — the spec shows a JSON shape but does not
//! prescribe the formatter. Implementation task 3 either matches these
//! strings or updates this file. Keep the contract explicit either way.

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

use parking_lot::Mutex;
use serde_json::{Value, json};
use symforge::live_index::LiveIndex;
use symforge::protocol::SymForgeServer;
use symforge::watcher::WatcherInfo;
use tempfile::TempDir;

// ─── Constants ──────────────────────────────────────────────────────────────

/// The 7 edit tools listed in spec §2.1.
const EDIT_TOOLS: &[&str] = &[
    "edit_within_symbol",
    "replace_symbol_body",
    "insert_symbol",
    "delete_symbol",
    "batch_edit",
    "batch_insert",
    "batch_rename",
];

/// Shared sample content for tests that just need a parseable Rust file.
const HELLO_RS: &str =
    "fn hello() {\n    println!(\"hello\");\n}\n\nfn world() {\n    println!(\"world\");\n}\n";

// ─── Feature flag helpers ───────────────────────────────────────────────────

fn enable_feature_flag() {
    // SAFETY: tests are `--test-threads=1` per project `CLAUDE.md`. Rust 2024
    // requires `unsafe` for env-var mutation; see `.planning/STATE.md:66`.
    unsafe { std::env::set_var("SYMFORGE_WORKTREE_AWARE", "1") };
}

// ─── Git helpers (shell out; git is required dev tooling) ───────────────────

fn run_git(cwd: &Path, args: &[&str]) -> String {
    let out = Command::new("git")
        .current_dir(cwd)
        .args(args)
        .output()
        .unwrap_or_else(|e| panic!("git spawn failed for {args:?}: {e}"));
    if !out.status.success() {
        panic!(
            "git {args:?} in {cwd:?} failed:\nstdout: {}\nstderr: {}",
            String::from_utf8_lossy(&out.stdout),
            String::from_utf8_lossy(&out.stderr),
        );
    }
    String::from_utf8_lossy(&out.stdout).into_owned()
}

fn git_init_with_initial_commit(root: &Path) {
    run_git(root, &["init", "-b", "main"]);
    run_git(root, &["config", "user.email", "test@example.com"]);
    run_git(root, &["config", "user.name", "wtaware-test"]);
    run_git(root, &["add", "-A"]);
    run_git(root, &["commit", "-m", "initial"]);
}

fn git_worktree_add(indexed_root: &Path, worktree_root: &Path, branch: &str) {
    if let Some(parent) = worktree_root.parent() {
        fs::create_dir_all(parent).expect("worktree parent dir");
    }
    run_git(
        indexed_root,
        &[
            "worktree",
            "add",
            "-b",
            branch,
            worktree_root.to_str().expect("utf-8 worktree path"),
        ],
    );
}

// ─── Fixtures ───────────────────────────────────────────────────────────────

/// Single-tree fixture: indexed repo, no worktree.
///
/// Used for backward-compat and "`working_directory` == indexed root" cases,
/// plus the cache-refresh test (which creates a worktree mid-session).
struct IndexedOnlyFixture {
    _dir: TempDir,
    root: PathBuf,
    server: SymForgeServer,
}

impl IndexedOnlyFixture {
    fn new(files: &[(&str, &str)]) -> Self {
        let dir = tempfile::tempdir().expect("tempdir");
        let container = dir.path().to_path_buf();
        let root = container.join("main");
        fs::create_dir_all(&root).expect("create main dir");
        for (rel, content) in files {
            let path = root.join(rel);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).expect("create parent dir");
            }
            fs::write(&path, content).expect("write fixture file");
        }
        git_init_with_initial_commit(&root);

        let shared = LiveIndex::load(&root).expect("LiveIndex::load");
        let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
        let server = SymForgeServer::new(
            shared,
            "worktree_awareness_indexed_only".to_string(),
            watcher_info,
            Some(root.clone()),
            None,
        );
        Self { _dir: dir, root, server }
    }

    fn read(&self, rel: &str) -> String {
        fs::read_to_string(self.root.join(rel)).expect("read file")
    }
}

/// Two-tree fixture: an indexed repo plus one parallel worktree at
/// `<container>/wt_one` on branch `tentacle/test`.
struct WorktreeFixture {
    _dir: TempDir,
    root: PathBuf,
    worktree_root: PathBuf,
    server: SymForgeServer,
}

impl WorktreeFixture {
    fn new(files: &[(&str, &str)]) -> Self {
        let dir = tempfile::tempdir().expect("tempdir");
        let container = dir.path().to_path_buf();
        let root = container.join("main");
        fs::create_dir_all(&root).expect("create main dir");
        for (rel, content) in files {
            let path = root.join(rel);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).expect("create parent dir");
            }
            fs::write(&path, content).expect("write fixture file");
        }
        git_init_with_initial_commit(&root);

        let worktree_root = container.join("wt_one");
        git_worktree_add(&root, &worktree_root, "tentacle/test");

        let shared = LiveIndex::load(&root).expect("LiveIndex::load");
        let watcher_info = Arc::new(Mutex::new(WatcherInfo::default()));
        let server = SymForgeServer::new(
            shared,
            "worktree_awareness_test".to_string(),
            watcher_info,
            Some(root.clone()),
            None,
        );
        Self {
            _dir: dir,
            root,
            worktree_root,
            server,
        }
    }

    fn container(&self) -> PathBuf {
        self.root.parent().expect("container").to_path_buf()
    }

    fn read_indexed(&self, rel: &str) -> String {
        fs::read_to_string(self.root.join(rel)).expect("read indexed file")
    }

    fn read_worktree(&self, rel: &str) -> String {
        fs::read_to_string(self.worktree_root.join(rel)).expect("read worktree file")
    }
}

// ─── Call helper ────────────────────────────────────────────────────────────

async fn call(server: &SymForgeServer, tool: &str, params: Value) -> String {
    server.dispatch_tool_for_tests(tool, params).await
}

fn assert_contains(result: &str, needle: &str) {
    assert!(
        result.contains(needle),
        "expected result to contain `{needle}`; result was:\n{result}"
    );
}

fn assert_not_contains(result: &str, needle: &str) {
    assert!(
        !result.contains(needle),
        "expected result NOT to contain `{needle}`; result was:\n{result}"
    );
}

// ─── §4 Acceptance criteria (7 tests, 1:1 mapping) ──────────────────────────

/// AC1: Every write tool listed in spec §2.1 accepts a `working_directory`
/// parameter.
///
/// Drives each tool through `dispatch_tool_for_tests` with a payload that
/// includes `working_directory`, and asserts the dispatcher does not
/// reject the param at the input-struct layer (`"invalid tool parameters"`
/// would indicate missing `#[serde(default)] pub working_directory` on
/// the tool's input struct).
#[tokio::test]
async fn ac1_all_seven_edit_tools_accept_working_directory_param() {
    enable_feature_flag();
    // Lock the tool set in one place; future edit tools must be added here
    // before the file is re-landed.
    assert_eq!(EDIT_TOOLS.len(), 7, "spec §2.1 lists exactly 7 write tools");

    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let wt_arg = fx.worktree_root.to_str().unwrap().to_string();

    // Minimum payload per tool that (a) parses, (b) includes
    // `working_directory`. Most tools will still return a downstream
    // error because the dummy payloads skip real arguments, but that is
    // fine — AC1 only asserts input-struct acceptance, not happy-path
    // success.
    let payloads: Vec<(&str, Value)> = vec![
        (
            "edit_within_symbol",
            json!({
                "path": "src/lib.rs",
                "name": "hello",
                "old_text": "hello",
                "new_text": "HELLO",
                "working_directory": wt_arg.clone(),
            }),
        ),
        (
            "replace_symbol_body",
            json!({
                "path": "src/lib.rs",
                "name": "hello",
                "new_body": "fn hello() {}",
                "working_directory": wt_arg.clone(),
            }),
        ),
        (
            "insert_symbol",
            json!({
                "path": "src/lib.rs",
                "name": "hello",
                "content": "fn added() {}",
                "working_directory": wt_arg.clone(),
            }),
        ),
        (
            "delete_symbol",
            json!({
                "path": "src/lib.rs",
                "name": "hello",
                "working_directory": wt_arg.clone(),
            }),
        ),
        (
            "batch_edit",
            json!({
                "edits": [],
                "working_directory": wt_arg.clone(),
            }),
        ),
        (
            "batch_insert",
            json!({
                "content": "fn x() {}",
                "position": "after",
                "targets": [],
                "working_directory": wt_arg.clone(),
            }),
        ),
        (
            "batch_rename",
            json!({
                "path": "src/lib.rs",
                "name": "hello",
                "new_name": "greet",
                "working_directory": wt_arg.clone(),
            }),
        ),
    ];

    assert_eq!(
        payloads.len(),
        EDIT_TOOLS.len(),
        "AC1 payload table must cover every tool in EDIT_TOOLS"
    );

    for (tool, params) in payloads {
        let result = call(&fx.server, tool, params).await;
        assert!(
            !result.contains("invalid tool parameters"),
            "tool `{tool}` rejected the `working_directory` param; \
             response was:\n{result}"
        );
        assert!(
            !result.starts_with("dispatch_tool_for_tests: unknown tool"),
            "tool `{tool}` is not wired into dispatch_tool_for_tests: {result}"
        );
    }
}

/// AC2: When `working_directory` is omitted, behavior is byte-identical to
/// today (indexed-path write, pre-existing response format).
#[tokio::test]
async fn ac2_omitted_working_directory_is_byte_identical() {
    enable_feature_flag();
    let fx = IndexedOnlyFixture::new(&[("src/lib.rs", HELLO_RS)]);

    let result = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {\n    println!(\"HELLO\");\n}",
            // intentionally NO `working_directory`
        }),
    )
    .await;

    // Pre-existing contract lines — must remain verbatim so omitted-param
    // callers see byte-identical output:
    assert_contains(&result, "Edit safety: structural-edit-safe");
    assert_contains(&result, "Path authority: repository-bound");
    assert_contains(&result, "Write semantics: atomic write + reindex");
    // When the param is omitted, the reroute marker must NOT appear — that
    // is how callers recognise "no reroute happened, default path".
    assert_not_contains(&result, "rerouted: true");

    let on_disk = fx.read("src/lib.rs");
    assert!(
        on_disk.contains("HELLO"),
        "indexed copy must be written when working_directory is omitted: {on_disk}",
    );
}

/// AC3: `working_directory` at a known worktree routes the write to the
/// worktree's copy and leaves the indexed copy untouched.
#[tokio::test]
async fn ac3_working_directory_at_known_worktree_writes_to_worktree() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let wt_arg = fx.worktree_root.to_str().unwrap().to_string();

    let result = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {\n    println!(\"HELLO_WT\");\n}",
            "working_directory": wt_arg,
        }),
    )
    .await;

    // §2.3 response shape — every rerouted write must self-describe.
    assert_contains(&result, "rerouted: true");
    assert_contains(&result, "wrote_to");
    assert_contains(&result, "indexed_path");

    let wt_after = fx.read_worktree("src/lib.rs");
    assert!(
        wt_after.contains("HELLO_WT"),
        "worktree copy must receive the write: {wt_after}",
    );
    let indexed_after = fx.read_indexed("src/lib.rs");
    assert!(
        !indexed_after.contains("HELLO_WT"),
        "indexed copy must NOT be touched when rerouting: {indexed_after}",
    );
}

/// AC4: `working_directory` at a path that is NOT a recognized worktree
/// returns an error and writes zero bytes.
#[tokio::test]
async fn ac4_working_directory_at_unknown_path_errors_and_writes_nothing() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    // A real, existing directory that isn't registered with the indexed
    // repo's `git worktree list`.
    let bogus = tempfile::tempdir().expect("bogus tempdir");

    let indexed_before = fx.read_indexed("src/lib.rs");
    let wt_before = fx.read_worktree("src/lib.rs");

    let result = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {\n    println!(\"SHOULD_NOT_LAND\");\n}",
            "working_directory": bogus.path().to_str().unwrap(),
        }),
    )
    .await;

    // Error surface — implementation returns
    // `WorkingDirectoryNotARecognizedWorktree` per spec §2.2.
    assert_contains(&result, "WorkingDirectoryNotARecognizedWorktree");
    // Actionable hint requirement from spec §2.2.
    assert_contains(&result, "git worktree list");

    // Zero bytes written anywhere.
    assert_eq!(
        fx.read_indexed("src/lib.rs"),
        indexed_before,
        "indexed copy modified despite unknown working_directory",
    );
    assert_eq!(
        fx.read_worktree("src/lib.rs"),
        wt_before,
        "worktree copy modified despite unknown working_directory",
    );
}

/// AC5: Response includes `wrote_to`, `indexed_path`, and `rerouted` fields
/// so callers can verify the actual write target.
#[tokio::test]
async fn ac5_response_surfaces_wrote_to_and_indexed_path_and_rerouted() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let wt_arg = fx.worktree_root.to_str().unwrap().to_string();

    let result = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() { }",
            "working_directory": wt_arg,
        }),
    )
    .await;

    assert_contains(&result, "wrote_to");
    assert_contains(&result, "indexed_path");
    assert_contains(&result, "rerouted: true");
    // The rerouted write target must appear verbatim in the response so the
    // caller can log / verify it.
    assert_contains(&result, fx.worktree_root.to_str().unwrap());
}

/// AC6: Test matrix covers the two cases not covered by AC2-AC4:
///  (a) `working_directory` == indexed root → `rerouted: false`.
///  (b) `working_directory` is a worktree where the file doesn't exist at
///      HEAD → `TargetFileMissing` with an actionable hint.
#[tokio::test]
async fn ac6_matrix_covers_indexed_root_and_missing_file_cases() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);

    // Case A: working_directory = indexed root → same behaviour as omitted.
    let result_a = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {}",
            "working_directory": fx.root.to_str().unwrap(),
        }),
    )
    .await;
    assert_contains(&result_a, "rerouted: false");

    // Case B: working_directory = worktree, but file is missing at HEAD in
    // that worktree. Simulate by deleting it before the call.
    fs::remove_file(fx.worktree_root.join("src/lib.rs")).expect("remove worktree file");
    let result_b = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {}",
            "working_directory": fx.worktree_root.to_str().unwrap(),
        }),
    )
    .await;
    assert_contains(&result_b, "TargetFileMissing");
    // Actionable hint from spec §2.2.
    assert_contains(&result_b, "git ls-tree");
}

/// AC7: `README.md` documents the `working_directory` parameter with an
/// example.
#[test]
fn ac7_readme_documents_working_directory_parameter() {
    let readme_path = format!("{}/README.md", env!("CARGO_MANIFEST_DIR"));
    let readme = fs::read_to_string(&readme_path)
        .unwrap_or_else(|e| panic!("README.md unreadable at {readme_path}: {e}"));
    assert!(
        readme.contains("working_directory"),
        "README.md must document the `working_directory` parameter",
    );
    assert!(
        readme.to_lowercase().contains("worktree"),
        "README.md must reference git worktrees",
    );
}

// ─── Implementation Notes expanded matrix (5 tests) ─────────────────────────

/// Matrix #1 — Windows path separator normalization: `C:\repo` vs `C:/repo`.
/// Gated to Windows because `\` is a path separator only there; on POSIX it
/// is a legal filename character and the assertion would be nonsense.
#[cfg(target_os = "windows")]
#[tokio::test]
async fn matrix_windows_forward_and_back_slash_canonicalize_equal() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let back = fx.worktree_root.to_str().unwrap().to_string();
    let forward = back.replace('\\', "/");

    let result = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {}",
            "working_directory": forward,
        }),
    )
    .await;

    assert_contains(&result, "rerouted: true");
    assert_not_contains(&result, "WorkingDirectoryNotARecognizedWorktree");
}

/// Matrix #2 — Mixed-case drive letter / path on Windows' case-insensitive
/// filesystem. Canonicalization must treat `C:\Repo` and `c:\repo` as the
/// same worktree.
#[cfg(target_os = "windows")]
#[tokio::test]
async fn matrix_mixed_case_windows_canonicalize_equal() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let original = fx.worktree_root.to_str().unwrap().to_string();
    // Flip-case selected characters (drive letter, a couple of segments) so
    // the string differs from any cached entry.
    let mixed: String = original
        .chars()
        .enumerate()
        .map(|(i, c)| {
            if i % 3 == 0 {
                c.to_ascii_uppercase()
            } else {
                c.to_ascii_lowercase()
            }
        })
        .collect();

    let result = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {}",
            "working_directory": mixed,
        }),
    )
    .await;

    assert_contains(&result, "rerouted: true");
}

/// Matrix #3 — Trailing-slash asymmetry: the same directory passed with and
/// without a trailing separator must canonicalize to the same worktree.
#[tokio::test]
async fn matrix_trailing_slash_canonicalize_equal() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let base = fx.worktree_root.to_str().unwrap().to_string();
    let sep = std::path::MAIN_SEPARATOR;
    let with_trailing = if base.ends_with(sep) {
        base.clone()
    } else {
        format!("{base}{sep}")
    };

    let result = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {}",
            "working_directory": with_trailing,
        }),
    )
    .await;

    assert_contains(&result, "rerouted: true");
    assert_not_contains(&result, "WorkingDirectoryNotARecognizedWorktree");
}

/// Matrix #4 — Two worktrees, two writes: no cross-talk.
///
/// The `#[tokio::test]` default runtime serialises task execution, so this
/// does not exercise a true data race — the spec already out-of-scopes
/// file-level locking (§5). What we verify is that *routing* is
/// per-call-correct: wt1's write lands in wt1, wt2's in wt2, indexed root
/// is untouched by either.
#[tokio::test]
async fn matrix_two_worktrees_concurrent_calls_have_no_crosstalk() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);

    let wt2 = fx.container().join("wt_two");
    git_worktree_add(&fx.root, &wt2, "tentacle/test-two");

    let wt1_arg = fx.worktree_root.to_str().unwrap().to_string();
    let wt2_arg = wt2.to_str().unwrap().to_string();

    let r1 = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() { /* wt1 */ }",
            "working_directory": wt1_arg,
        }),
    )
    .await;
    let r2 = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() { /* wt2 */ }",
            "working_directory": wt2_arg,
        }),
    )
    .await;

    assert_contains(&r1, "rerouted: true");
    assert_contains(&r2, "rerouted: true");

    let c1 = fs::read_to_string(fx.worktree_root.join("src/lib.rs")).unwrap();
    let c2 = fs::read_to_string(wt2.join("src/lib.rs")).unwrap();
    assert!(
        c1.contains("wt1") && !c1.contains("wt2"),
        "wt1 cross-written:\n{c1}",
    );
    assert!(
        c2.contains("wt2") && !c2.contains("wt1"),
        "wt2 cross-written:\n{c2}",
    );
    let indexed = fx.read_indexed("src/lib.rs");
    assert!(
        !indexed.contains("wt1") && !indexed.contains("wt2"),
        "indexed root polluted:\n{indexed}",
    );
}

/// Matrix #5 — Cache refresh: a worktree created mid-session must be
/// accepted on the next tool call, not rejected as unknown.
#[tokio::test]
async fn matrix_cache_refresh_newly_created_worktree_accepted() {
    enable_feature_flag();
    let fx = IndexedOnlyFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let container = fx.root.parent().expect("container").to_path_buf();
    let lateborn = container.join("wt_lateborn");

    // First call: the worktree doesn't exist yet → must be rejected.
    let pre = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {}",
            "working_directory": lateborn.to_str().unwrap(),
        }),
    )
    .await;
    assert_contains(&pre, "WorkingDirectoryNotARecognizedWorktree");

    // Create the worktree mid-session.
    git_worktree_add(&fx.root, &lateborn, "tentacle/lateborn");

    // Second call: the same path must now be accepted via cache refresh.
    let post = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {}",
            "working_directory": lateborn.to_str().unwrap(),
        }),
    )
    .await;
    assert_contains(&post, "rerouted: true");
}

// ─── Item 4: health misuse counter + conventions answer ─────────────────────

/// `health` surfaces a rolling "last hour" misuse counter whose value bumps
/// each time an edit tool is called without `working_directory` while
/// `SYMFORGE_WORKTREE_AWARE=1` is set.
#[tokio::test]
async fn health_surfaces_worktree_misuse_counter() {
    enable_feature_flag();
    let fx = IndexedOnlyFixture::new(&[("src/lib.rs", HELLO_RS)]);

    // Baseline: before any edit calls, counter reads 0.
    let baseline = call(&fx.server, "health", json!({})).await;
    assert_contains(&baseline, "Worktree-awareness misuse");
    assert_contains(
        &baseline,
        "edit tool calls without working_directory (last hour): 0",
    );

    // One edit call without `working_directory` should bump the counter.
    let _ = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() {\n    println!(\"bump\");\n}",
        }),
    )
    .await;
    let after_one = call(&fx.server, "health", json!({})).await;
    assert_contains(
        &after_one,
        "edit tool calls without working_directory (last hour): 1",
    );

    // A second call using a DIFFERENT edit tool must also increment.
    let _ = call(
        &fx.server,
        "edit_within_symbol",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "old_text": "bump",
            "new_text": "bumped",
        }),
    )
    .await;
    let after_two = call(&fx.server, "health", json!({})).await;
    assert_contains(
        &after_two,
        "edit tool calls without working_directory (last hour): 2",
    );
}

/// When `working_directory` is supplied, the misuse counter must NOT
/// increment — the caller did the right thing.
#[tokio::test]
async fn health_misuse_counter_does_not_increment_when_working_directory_supplied() {
    enable_feature_flag();
    let fx = WorktreeFixture::new(&[("src/lib.rs", HELLO_RS)]);
    let wt_arg = fx.worktree_root.to_str().unwrap().to_string();

    // Call an edit tool WITH `working_directory`.
    let _ = call(
        &fx.server,
        "replace_symbol_body",
        json!({
            "path": "src/lib.rs",
            "name": "hello",
            "new_body": "fn hello() { }",
            "working_directory": wt_arg,
        }),
    )
    .await;

    let after = call(&fx.server, "health", json!({})).await;
    assert_contains(
        &after,
        "edit tool calls without working_directory (last hour): 0",
    );
}

/// `conventions` output documents the `working_directory` parameter,
/// `SYMFORGE_WORKTREE_AWARE` flag, and points at the README — so agents
/// that query project conventions learn how to call edit tools from
/// inside a worktree.
#[tokio::test]
async fn conventions_surfaces_worktree_awareness_guidance() {
    let fx = IndexedOnlyFixture::new(&[("src/lib.rs", HELLO_RS)]);

    let output = call(&fx.server, "conventions", json!({})).await;

    assert_contains(&output, "Worktree awareness");
    assert_contains(&output, "working_directory");
    assert_contains(&output, "SYMFORGE_WORKTREE_AWARE");
    assert_contains(&output, "README");
}