trusty-code 0.8.2

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
//! Unit tests for the `.trusty-code/` project-configuration resolver (#5426).
//!
//! Why: the precedence rule, the fail-open warning, and the native-write guard
//! are the three properties every other #5426 change depends on; each is proven
//! here against a real temp tree rather than a mocked filesystem.
//! What: precedence across the three search roots, the default when nothing
//! exists, the unreadable-candidate fallback (directory AND file), the
//! write-target guard including a symlink escape, and the secret-key scanner.
//! Test: this file IS the test module.

use super::*;

use std::sync::{Arc, Mutex};

/// A `tracing` writer collecting every formatted event into a shared buffer.
///
/// Why: the Fail-Open Check requires the fallback to LOG at warn with the path
/// tried; asserting on the returned `unreadable` field alone would pass even if
/// the warning were deleted.
/// What: a `MakeWriter` over an `Arc<Mutex<Vec<u8>>>`, installed per-test with
/// `tracing::subscriber::with_default` (thread-local, so parallel tests in this
/// binary never race a global subscriber).
/// Test: used by `unreadable_native_dir_falls_back_and_is_reported`.
#[derive(Clone)]
struct CaptureWriter(Arc<Mutex<Vec<u8>>>);

impl std::io::Write for CaptureWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for CaptureWriter {
    type Writer = CaptureWriter;

    fn make_writer(&'a self) -> Self::Writer {
        self.clone()
    }
}

/// Run `f` with a thread-local tracing subscriber and return its captured output.
fn capture_logs<T>(f: impl FnOnce() -> T) -> (T, String) {
    let buffer = Arc::new(Mutex::new(Vec::<u8>::new()));
    let subscriber = tracing_subscriber::fmt()
        .with_writer(CaptureWriter(Arc::clone(&buffer)))
        .with_ansi(false)
        .with_max_level(tracing::Level::WARN)
        .finish();
    let out = tracing::subscriber::with_default(subscriber, f);
    let logs =
        String::from_utf8_lossy(&buffer.lock().unwrap_or_else(|e| e.into_inner())).to_string();
    (out, logs)
}

/// Create `<root>/<dirname>/<relative>` as a directory.
fn mkdir(root: &std::path::Path, dirname: &str, relative: &str) -> PathBuf {
    let p = root.join(dirname).join(relative);
    std::fs::create_dir_all(&p).expect("mkdir");
    p
}

/// Whether this process runs as root (which ignores permission bits).
#[cfg(unix)]
fn running_as_root() -> bool {
    // SAFETY: `geteuid` takes no arguments, touches no memory, and cannot fail.
    unsafe { libc::geteuid() == 0 }
}

// -- precedence ------------------------------------------------------------

/// `.trusty-code/` wins over `.claude/` when both exist.
///
/// Why: this is the whole inversion #5426 asks for — Trusty Code's own
/// directory is the source of truth, and `.claude/` only fills in behind it.
/// What: creates both `agents` directories and asserts the resolved path and
/// source name the native one.
/// Test: this function IS the test.
#[test]
fn precedence_prefers_trusty_code_over_claude() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let native = mkdir(tmp.path(), TRUSTY_CODE_DIRNAME, "agents");
    mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "agents");

    let resolved = agents_dir(tmp.path());

    assert_eq!(resolved.path, native);
    assert_eq!(resolved.source, ConfigSource::TrustyCode);
    assert!(resolved.unreadable.is_empty());
}

/// `.claude/` still resolves when `.trusty-code/` is absent.
///
/// Why: every existing project has only `.claude/`; breaking them was never on
/// the table.
/// What: creates only `.claude/agents` and asserts it wins with the
/// compatibility source.
/// Test: this function IS the test.
#[test]
fn falls_back_to_claude_when_trusty_code_absent() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let claude = mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "agents");

    let resolved = agents_dir(tmp.path());

    assert_eq!(resolved.path, claude);
    assert_eq!(resolved.source, ConfigSource::ClaudeCompat);
    assert!(!resolved.source.is_native());
}

/// `.open-mpm/` is still honoured behind both newer roots.
///
/// Why: `agents::locate_agents_dir` already supported it; the rewrite must not
/// quietly drop a supported layout.
/// What: creates only `.open-mpm/agents` and asserts it wins.
/// Test: this function IS the test.
#[test]
fn falls_back_to_open_mpm_when_neither_native_nor_claude() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let legacy = mkdir(tmp.path(), OPEN_MPM_LEGACY_DIRNAME, "agents");

    let resolved = agents_dir(tmp.path());

    assert_eq!(resolved.path, legacy);
    assert_eq!(resolved.source, ConfigSource::OpenMpmLegacy);
}

/// With nothing on disk, the resolver names the NATIVE path.
///
/// Why: "a clean project can install and run without a `.claude/` directory"
/// requires that the not-yet-created default point at `.trusty-code/`, not at
/// the compatibility root.
/// What: an empty project root; asserts the default path and source.
/// Test: this function IS the test.
#[test]
fn default_source_when_nothing_exists() {
    let tmp = tempfile::tempdir().expect("tempdir");

    let resolved = agents_dir(tmp.path());

    assert_eq!(
        resolved.path,
        tmp.path().join(TRUSTY_CODE_DIRNAME).join("agents")
    );
    assert_eq!(resolved.source, ConfigSource::Default);
    assert!(resolved.source.is_native());
}

/// Skills, plugins, and settings follow the same precedence as agents.
///
/// Why: the point of one resolver is that no entry has its own rule; a per-kind
/// test is what proves the wiring actually routes through it.
/// What: creates each native entry alongside a `.claude/` twin and asserts the
/// native one wins in all three.
/// Test: this function IS the test.
#[test]
fn skills_dir_prefers_trusty_code() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let native = mkdir(tmp.path(), TRUSTY_CODE_DIRNAME, "skills");
    mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "skills");
    assert_eq!(skills_dir(tmp.path()).path, native);
}

/// See `skills_dir_prefers_trusty_code`.
#[test]
fn plugins_dir_prefers_trusty_code() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let native = mkdir(tmp.path(), TRUSTY_CODE_DIRNAME, "plugins");
    mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "plugins");
    assert_eq!(plugins_dir(tmp.path()).path, native);
}

/// See `skills_dir_prefers_trusty_code`.
#[test]
fn agents_dir_prefers_trusty_code() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let native = mkdir(tmp.path(), TRUSTY_CODE_DIRNAME, "agents");
    mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "agents");
    assert_eq!(agents_dir(tmp.path()).path, native);
}

/// `settings.json` resolves as a FILE, native first.
///
/// Why: a directory named `settings.json` must not satisfy the lookup, and a
/// project with only `.trusty-code/settings.json` must be able to set
/// `code_harness.mode`.
/// What: writes both files and asserts the native one wins.
/// Test: this function IS the test.
#[test]
fn settings_file_prefers_trusty_code() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let native_dir = mkdir(tmp.path(), TRUSTY_CODE_DIRNAME, "");
    let claude_dir = mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "");
    std::fs::write(native_dir.join(SETTINGS_FILENAME), "{}").expect("write");
    std::fs::write(claude_dir.join(SETTINGS_FILENAME), "{}").expect("write");

    let resolved = settings_file(tmp.path());

    assert_eq!(resolved.path, native_dir.join(SETTINGS_FILENAME));
    assert_eq!(resolved.source, ConfigSource::TrustyCode);
}

/// The source tokens are the wire contract the CLI and logs share.
#[test]
fn source_tokens_are_stable() {
    assert_eq!(ConfigSource::TrustyCode.as_str(), "trusty-code");
    assert_eq!(ConfigSource::ClaudeCompat.as_str(), "claude");
    assert_eq!(ConfigSource::OpenMpmLegacy.as_str(), "open-mpm");
    assert_eq!(ConfigSource::Default.as_str(), "default");
    assert!(ConfigSource::TrustyCode.is_native());
    assert!(ConfigSource::Default.is_native());
    assert!(!ConfigSource::ClaudeCompat.is_native());
    assert!(!ConfigSource::OpenMpmLegacy.is_native());
}

// -- fail-open behaviour ---------------------------------------------------

/// An unreadable `.trusty-code/agents` falls back to `.claude/agents`, records
/// the skipped path, and WARNS with it.
///
/// Why: the Fail-Open Check. A harness that aborts because an optional config
/// directory lost its read bit is worse than one that starts with less config —
/// but a silent fallback is worse than both, because the operator then debugs
/// the wrong directory.
/// What: chmods `.trusty-code/agents` to `0o000`, resolves under a captured
/// subscriber, and asserts the winner is `.claude`, the skipped path is
/// reported, and the warning names the unreadable path.
/// Test: this function IS the test.
#[cfg(unix)]
#[test]
fn unreadable_native_dir_falls_back_and_is_reported() {
    use std::os::unix::fs::PermissionsExt;

    if running_as_root() {
        // root ignores permission bits, so the unreadable case cannot be staged.
        return;
    }
    let tmp = tempfile::tempdir().expect("tempdir");
    let native = mkdir(tmp.path(), TRUSTY_CODE_DIRNAME, "agents");
    let claude = mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "agents");
    std::fs::set_permissions(&native, std::fs::Permissions::from_mode(0o000)).expect("chmod");

    let (resolved, logs) = capture_logs(|| agents_dir(tmp.path()));

    // Restore before any assertion can unwind past the tempdir cleanup.
    std::fs::set_permissions(&native, std::fs::Permissions::from_mode(0o755)).expect("restore");

    assert_eq!(resolved.source, ConfigSource::ClaudeCompat);
    assert_eq!(resolved.path, claude);
    assert_eq!(resolved.unreadable, vec![native.clone()]);
    assert!(
        logs.contains(&native.display().to_string()),
        "warning must name the unreadable path tried; logs were:\n{logs}"
    );
    assert!(
        logs.contains("WARN"),
        "fallback must be logged at warn; logs were:\n{logs}"
    );
}

/// An unreadable `settings.json` falls back to the next root and WARNS.
///
/// Why: the file-shaped half of the same Fail-Open Check — `probe` uses a
/// different syscall for files, so the directory test does not cover it.
/// What: chmods `.trusty-code/settings.json` to `0o000` and asserts the
/// `.claude/` one wins with a warning naming the unreadable path.
/// Test: this function IS the test.
#[cfg(unix)]
#[test]
fn unreadable_settings_file_falls_back_and_is_reported() {
    use std::os::unix::fs::PermissionsExt;

    if running_as_root() {
        return;
    }
    let tmp = tempfile::tempdir().expect("tempdir");
    let native_dir = mkdir(tmp.path(), TRUSTY_CODE_DIRNAME, "");
    let claude_dir = mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "");
    let native = native_dir.join(SETTINGS_FILENAME);
    std::fs::write(&native, "{}").expect("write");
    std::fs::write(claude_dir.join(SETTINGS_FILENAME), "{}").expect("write");
    std::fs::set_permissions(&native, std::fs::Permissions::from_mode(0o000)).expect("chmod");

    let (resolved, logs) = capture_logs(|| settings_file(tmp.path()));

    std::fs::set_permissions(&native, std::fs::Permissions::from_mode(0o644)).expect("restore");

    assert_eq!(resolved.source, ConfigSource::ClaudeCompat);
    assert_eq!(resolved.unreadable, vec![native.clone()]);
    assert!(
        logs.contains(&native.display().to_string()),
        "warning must name the unreadable path tried; logs were:\n{logs}"
    );
}

// -- native write boundary -------------------------------------------------

/// `native_config_dir` never consults the filesystem.
///
/// Why: the read path may resolve into `.claude/`; the WRITE path must not, even
/// on a project where only `.claude/` exists.
/// What: an empty root and a `.claude`-only root both yield `.trusty-code`.
/// Test: this function IS the test.
#[test]
fn native_config_dir_is_always_trusty_code() {
    let tmp = tempfile::tempdir().expect("tempdir");
    mkdir(tmp.path(), CLAUDE_COMPAT_DIRNAME, "agents");

    assert_eq!(
        native_config_dir(tmp.path()),
        tmp.path().join(TRUSTY_CODE_DIRNAME)
    );
    assert_eq!(
        native_child(tmp.path(), "agents/pm.md"),
        tmp.path().join(TRUSTY_CODE_DIRNAME).join("agents/pm.md")
    );
}

/// A target beneath `.trusty-code/` is permitted.
#[test]
fn write_to_native_dir_is_allowed() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let target = native_child(tmp.path(), "agents/pm.md");
    assert_eq!(
        check_native_write_target(tmp.path(), &target).expect("native target must be allowed"),
        target
    );
}

/// A write aimed at `.claude/` is refused as a cross-product write.
///
/// Why: "native project writes stay beneath `<project>/.trusty-code/`" — a
/// harness that writes into Claude Code's directory is mutating another
/// product's state.
/// What: asserts a `.claude/agents/pm.md` target yields `CrossProduct`.
/// Test: this function IS the test.
#[test]
fn write_to_claude_dir_is_refused() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let target = tmp.path().join(CLAUDE_COMPAT_DIRNAME).join("agents/pm.md");

    let err = check_native_write_target(tmp.path(), &target).expect_err("must be refused");

    assert!(matches!(err, WriteTargetError::CrossProduct { .. }));
    assert!(err.to_string().contains(TRUSTY_CODE_DIRNAME));
}

/// A write outside the project entirely is refused.
#[test]
fn write_outside_project_is_refused() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let err = check_native_write_target(tmp.path(), std::path::Path::new("/etc/passwd"))
        .expect_err("must be refused");
    assert!(matches!(err, WriteTargetError::CrossProduct { .. }));
}

/// A lexically-inside target that resolves outside through a symlink is refused.
///
/// Why: `starts_with` alone is satisfied by `.trusty-code/agents ->
/// /somewhere/else`, and every write would then land outside the project.
/// What: symlinks `.trusty-code/agents` at a sibling temp directory and asserts
/// a target inside it is refused as a `SymlinkEscape`.
/// Test: this function IS the test.
#[cfg(unix)]
#[test]
fn symlinked_native_subdir_escape_is_refused() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let outside = tempfile::tempdir().expect("tempdir");
    let native = native_config_dir(tmp.path());
    std::fs::create_dir_all(&native).expect("mkdir");
    std::os::unix::fs::symlink(outside.path(), native.join("agents")).expect("symlink");

    let target = native.join("agents").join("pm.md");
    let err = check_native_write_target(tmp.path(), &target).expect_err("must be refused");

    assert!(
        matches!(err, WriteTargetError::SymlinkEscape { .. }),
        "expected SymlinkEscape, got {err:?}"
    );
}

// -- secret scanning -------------------------------------------------------

/// Secret-bearing keys are found anywhere in the JSON tree, by dotted path.
#[test]
fn secret_bearing_keys_are_detected() {
    let value = serde_json::json!({
        "code_harness": { "mode": "parity" },
        "mcp": [{ "name": "x", "env": { "OPENROUTER_API_KEY": "sk-live" } }],
    });
    assert_eq!(
        find_secret_key(&value).as_deref(),
        Some("mcp.0.env.OPENROUTER_API_KEY")
    );

    let top = serde_json::json!({ "authToken": "abc" });
    assert_eq!(find_secret_key(&top).as_deref(), Some("authToken"));
}

/// An ordinary settings file carries no secret.
#[test]
fn ordinary_settings_carry_no_secret() {
    let value = serde_json::json!({ "code_harness": { "mode": "daily-driver" } });
    assert_eq!(find_secret_key(&value), None);
}

/// Kebab, camel, header and SCREAMING spellings of the same key all match.
///
/// Why: code-critic HIGH on PR #6980 — the scanner matched raw lowercase
/// substrings, so `api_key` was caught and `API-KEY`, `x-api-key`, `access-key`
/// and `private-key` were not. A credential under any of those spellings
/// imported straight into a file the project commits.
/// What: one key per spelling, each asserted secret-bearing.
/// Test: this function IS the test.
#[test]
fn secret_keys_match_across_separator_and_case_spellings() {
    for key in [
        "api_key",
        "API-KEY",
        "x-api-key",
        "xApiKey",
        "apiKey",
        "APIKEY",
        "access-key",
        "accessKey",
        "private-key",
        "AWS_SECRET_ACCESS_KEY",
        "Authorization",
        "auth-token",
        "authToken",
        "user.password",
        "PassPhrase",
        "gh-credential",
    ] {
        assert!(
            is_secret_key(key),
            "{key} must be recognised as secret-bearing"
        );
    }
}

/// Plural spellings match too — `tokens` is as much a credential as `token`.
///
/// Why: code-critic round 2 HIGH on PR #6980. The hint list carried
/// `credential` AND `credentials` but left `secret`, `token`, `accesskey`,
/// `apikey` and `privatekey` singular-only, so seven ordinary plural spellings
/// walked past the matcher into a file the project commits.
/// What: the seven spellings the critic probed, each asserted secret-bearing.
/// Test: this function IS the test.
#[test]
fn secret_keys_match_plural_spellings() {
    for key in [
        "tokens",
        "secretsFile",
        "refreshTokens",
        "apiSecrets",
        "clientSecrets",
        "accessKeys",
        "apiKeys",
        "credentials",
        "private-keys",
    ] {
        assert!(
            is_secret_key(key),
            "{key} must be recognised as secret-bearing"
        );
    }
}

/// `max_tokens` names a COUNT, not a credential, and stays benign.
///
/// Why: making the match plural-insensitive turns `tokens` into a hit, which
/// would sweep up `max_tokens` — an LLM sampling parameter this crate's own
/// `code_harness` settings carry. Refusing to import the very file the feature
/// exists to migrate is worse than the leak risk of a key that holds an integer,
/// so a short exemption list of count-shaped compounds ([`SECRET_KEY_EXEMPTIONS`])
/// is checked first. The exemption matches the WHOLE key only, so appending a
/// real credential (`max_tokens_api_key`) still trips the scanner.
/// What: asserts each count-shaped key is clean and that the exemption cannot be
/// used as a prefix to smuggle a credential past the check.
/// Test: this function IS the test.
#[test]
fn max_tokens_stays_benign_as_a_count_not_a_credential() {
    for key in [
        "max_tokens",
        "maxTokens",
        "min_tokens",
        "token_count",
        "tokenLimit",
        "total_tokens",
    ] {
        assert!(
            !is_secret_key(key),
            "{key} names a count and must NOT be flagged"
        );
    }

    assert!(
        is_secret_key("max_tokens_api_key"),
        "an exemption must not shield a real credential appended to it"
    );
}

/// A benign key that merely CONTAINS a hint is not flagged.
///
/// Why: the fix for the spelling gap must not be a looser substring match —
/// `tokenizer` and `secretary` contain `token` and `secret` and are ordinary
/// configuration, so flagging them would refuse imports for no reason.
/// What: asserts each benign key is clean, including a compound where the hint
/// is a strict prefix or suffix of a real word.
/// Test: this function IS the test.
#[test]
fn benign_keys_containing_a_hint_are_not_flagged() {
    for key in [
        "tokenizer",
        "tokenizerPath",
        "secretary",
        "max_tokens",
        "keychain",
        "keyboard",
        "passwordless_hint_text",
        "mode",
        "cadence_turns",
    ] {
        assert!(!is_secret_key(key), "{key} must NOT be flagged as secret");
    }
}

/// `find_secret_key` reports the dotted path of a kebab-spelled key.
///
/// Why: the walk and the per-key rule are separate; proving the rule alone would
/// not show the walk actually applies it at depth.
/// What: a header-style key nested under an array element.
/// Test: this function IS the test.
#[test]
fn nested_kebab_secret_is_reported_by_path() {
    let value = serde_json::json!({
        "mcp": [{ "name": "x", "headers": { "X-Api-Key": "sk-live" } }],
    });
    assert_eq!(
        find_secret_key(&value).as_deref(),
        Some("mcp.0.headers.X-Api-Key")
    );
}

/// A `.trusty-code` that is ITSELF a symlink out of the project is refused.
///
/// Why: code-critic BLOCK on PR #6980. The guard resolved the target and the
/// config root the same way and compared them to EACH OTHER, so when
/// `<project>/.trusty-code` pointed at an outside directory, every "protected"
/// write agreed with a root that was already outside the project and passed.
/// `symlinked_native_subdir_escape_is_refused` covers a symlinked SUBdirectory
/// and does not reach this case.
/// What: symlinks `<project>/.trusty-code` at a sibling temp directory and
/// asserts a target inside it is refused as a `SymlinkEscape`.
/// Test: this function IS the test.
#[cfg(unix)]
#[test]
fn symlinked_native_root_escape_is_refused() {
    let tmp = tempfile::tempdir().expect("tempdir");
    let outside = tempfile::tempdir().expect("tempdir");
    std::os::unix::fs::symlink(outside.path(), tmp.path().join(TRUSTY_CODE_DIRNAME))
        .expect("symlink");

    let target = native_child(tmp.path(), "agents/pm.md");
    let err = check_native_write_target(tmp.path(), &target).expect_err("must be refused");

    assert!(
        matches!(err, WriteTargetError::SymlinkEscape { .. }),
        "expected SymlinkEscape, got {err:?}"
    );
}