stellar-agent-toolsets 0.1.0-alpha.11

Signed-toolset (TOOLSET.md) format parsing and capability-manifest validation.
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
//! Integration tests for [`stellar_agent_toolsets::parse_toolset`].
#![allow(clippy::unwrap_used, clippy::expect_used)]
//!
//! Each test group covers one category of the agentskills format specification.
//!
//! Fixtures live under `tests/fixtures/`:
//! - `valid-minimal/` — one valid minimal toolset (name + description only).
//! - `valid-full/` — one valid toolset with all optional fields + capability manifest.
//! - `invalid-*/` — one toolset per refusal rule.

use std::path::Path;

use stellar_agent_toolsets::{Capability, ToolsetFormatError, parse_toolset};

fn fixture(rel: &str) -> std::path::PathBuf {
    let manifest_dir = env!("CARGO_MANIFEST_DIR");
    Path::new(manifest_dir).join("tests/fixtures").join(rel)
}

// ── Valid minimal toolset ───────────────────────────────────────────────────────

#[test]
fn ac1_valid_minimal_toolset_parses() {
    let dir = fixture("valid-minimal/read-balance");
    let toolset = parse_toolset(&dir).expect("valid minimal toolset should parse");
    assert_eq!(toolset.name, "read-balance");
    assert!(!toolset.description.is_empty());
    assert!(toolset.license.is_none());
    assert!(toolset.compatibility.is_none());
    assert!(toolset.metadata.is_empty());
    assert!(toolset.allowed_tools.is_empty());
    assert!(toolset.capabilities.is_empty());
    assert!(!toolset.instructions.is_empty());
}

// ── Valid full toolset ──────────────────────────────────────────────────────────

#[test]
fn ac1_valid_full_toolset_parses() {
    let dir = fixture("valid-full/portfolio-summary");
    let toolset = parse_toolset(&dir).expect("valid full toolset should parse");
    assert_eq!(toolset.name, "portfolio-summary");
    assert_eq!(toolset.license.as_deref(), Some("Apache-2.0"));
    assert!(toolset.compatibility.is_some());
    assert!(!toolset.metadata.is_empty());
    assert!(!toolset.allowed_tools.is_empty());
    // Capability manifest: read-balance + observe-event
    assert!(toolset.capabilities.contains(Capability::ReadBalance));
    assert!(toolset.capabilities.contains(Capability::ObserveEvent));
    assert_eq!(toolset.capabilities.len(), 2);
    // metadata map retains the capability key (not stripped).
    assert!(toolset.metadata.contains_key("stellar-agent-capabilities"));
}

// ── Name validation ───────────────────────────────────────────────────────────

#[test]
fn ac2_name_too_long_refused() {
    let dir = fixture(
        "invalid-name-too-long/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    );
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::NameTooLong),
        "expected NameTooLong, got {err:?}"
    );
}

#[test]
fn ac2_name_uppercase_refused() {
    let dir = fixture("invalid-name-uppercase/MyToolset");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::NameInvalidChar),
        "expected NameInvalidChar, got {err:?}"
    );
}

#[test]
fn ac2_name_leading_hyphen_refused() {
    let dir = fixture("invalid-name-leading-hyphen/-bad-toolset");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::NameLeadingTrailingHyphen),
        "expected NameLeadingTrailingHyphen, got {err:?}"
    );
}

#[test]
fn ac2_name_consecutive_hyphens_refused() {
    let dir = fixture("invalid-name-consecutive-hyphens/bad--toolset");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::NameConsecutiveHyphens),
        "expected NameConsecutiveHyphens, got {err:?}"
    );
}

#[test]
fn ac2_name_dir_mismatch_refused() {
    let dir = fixture("invalid-name-dir-mismatch/my-toolset");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::NameDirMismatch { .. }),
        "expected NameDirMismatch, got {err:?}"
    );
}

// ── Description + compatibility validation ────────────────────────────────────

#[test]
fn ac3_description_empty_refused() {
    let dir = fixture("invalid-description-empty/no-desc");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::DescriptionEmpty),
        "expected DescriptionEmpty, got {err:?}"
    );
}

#[test]
fn ac3_description_too_long_refused() {
    let dir = fixture("invalid-description-too-long/long-desc");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::DescriptionTooLong),
        "expected DescriptionTooLong, got {err:?}"
    );
}

// ── Capability manifest ───────────────────────────────────────────────────────

#[test]
fn ac4_known_capability_tokens_parse() {
    // Tested via the valid-full fixture above (ac1_valid_full_toolset_parses).
    let dir = fixture("valid-full/portfolio-summary");
    let toolset = parse_toolset(&dir).unwrap();
    assert!(toolset.capabilities.contains(Capability::ReadBalance));
    assert!(toolset.capabilities.contains(Capability::ObserveEvent));
}

#[test]
fn ac4_unknown_token_refused() {
    let dir = fixture("invalid-capability-unknown/unknown-cap");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::UnknownCapability { .. }),
        "expected UnknownCapability, got {err:?}"
    );
}

#[test]
fn ac4_capability_non_string_refused() {
    let dir = fixture("invalid-capability-non-string/non-string-cap");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::CapabilityManifestMalformed { .. }),
        "expected CapabilityManifestMalformed, got {err:?}"
    );
}

// ── Structural parse failures ─────────────────────────────────────────────────

#[test]
fn ac5_no_frontmatter_refused() {
    let dir = fixture("invalid-no-frontmatter/my-toolset");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::MissingFrontmatter),
        "expected MissingFrontmatter, got {err:?}"
    );
}

// ── No-bare-sign airtightness ─────────────────────────────────────────────────

#[test]
fn ac6_bare_sign_transaction_refused() {
    let dir = fixture("invalid-capability-bare-sign/sign-toolset");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::BareSignTransactionForbidden),
        "expected BareSignTransactionForbidden, got {err:?}"
    );
}

#[test]
fn ac6_sign_transaction_uppercase_refused_at_charset_gate() {
    // Inline toolset with Sign-Transaction (uppercase) in metadata.
    // Parse inline via a temp dir.
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: Sign-Transaction\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::CapabilityTokenInvalidChar { .. }),
        "expected CapabilityTokenInvalidChar for 'Sign-Transaction', got {err:?}"
    );
}

#[test]
fn ac6_sign_transaction_all_caps_refused_at_charset_gate() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: SIGN-TRANSACTION\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::CapabilityTokenInvalidChar { .. }),
        "expected CapabilityTokenInvalidChar for 'SIGN-TRANSACTION', got {err:?}"
    );
}

#[test]
fn ac6_sign_transaction_underscore_refused_at_charset_gate() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: sign_transaction\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::CapabilityTokenInvalidChar { .. }),
        "expected CapabilityTokenInvalidChar for 'sign_transaction', got {err:?}"
    );
}

#[test]
fn ac6_sign_transaction_whitespace_padded_refused() {
    // Whitespace is stripped by tokenisation, leaving "sign-transaction" which
    // then hits BareSignTransactionForbidden.
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: \"  sign-transaction  \"\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::BareSignTransactionForbidden),
        "expected BareSignTransactionForbidden for whitespace-padded token, got {err:?}"
    );
}

#[test]
fn ac6_sign_transaction_unicode_homoglyph_refused_at_charset_gate() {
    // Cyrillic 's' look-alike (ѕ, U+0455) — charset gate must catch it.
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // ѕ is U+0455, a Cyrillic small letter dze that looks like 's'
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: \"\u{0455}ign-transaction\"\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::CapabilityTokenInvalidChar { .. }),
        "expected CapabilityTokenInvalidChar for homoglyph token, got {err:?}"
    );
}

// ── Capability smuggling ──────────────────────────────────────────────────────

#[test]
fn ac7_duplicate_capability_key_refused() {
    // YAML permits duplicate keys; we refuse.
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: read-balance\n  stellar-agent-capabilities: observe-event\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::DuplicateKey { .. }),
        "expected DuplicateKey, got {err:?}"
    );
}

#[test]
fn ac7_duplicate_top_level_key_refused() {
    let dir = fixture("invalid-duplicate-key/dup-key");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::DuplicateKey { .. }),
        "expected DuplicateKey, got {err:?}"
    );
}

#[test]
fn ac7_reserved_metadata_key_refused() {
    let dir = fixture("invalid-reserved-metadata-key/reserved-key");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::ReservedMetadataKey { .. }),
        "expected ReservedMetadataKey, got {err:?}"
    );
}

#[test]
fn ac7_capability_invalid_char_refused() {
    let dir = fixture("invalid-capability-invalid-char/bad-char-cap");
    let err = parse_toolset(&dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::CapabilityTokenInvalidChar { .. }),
        "expected CapabilityTokenInvalidChar, got {err:?}"
    );
}

// ── Resource bounds ───────────────────────────────────────────────────────────

#[test]
fn ac8_file_too_large_refused() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("big-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // 256 KiB + 1 byte
    let big_content = vec![b'x'; 256 * 1024 + 1];
    f.write_all(&big_content).unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::ToolsetFileTooLarge { .. }),
        "expected ToolsetFileTooLarge, got {err:?}"
    );
}

#[test]
fn ac8_yaml_alias_bomb_refused_without_oom() {
    // A minimal alias-bomb: define anchor &a on a scalar, then alias *a many
    // times.  The event-based parser must emit Event::Alias immediately and we
    // refuse pre-expansion — no OOM, no stack-overflow.
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("alias-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    write!(f, "---\na: &a large_value\nb: *a\nc: *a\n---\n").unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::YamlAnchorsForbidden),
        "expected YamlAnchorsForbidden for alias bomb, got {err:?}"
    );
}

#[test]
fn ac8_deep_nesting_refused_without_stack_overflow() {
    // 10 levels of nesting > MAX_DEPTH (8).
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("deep-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    write!(
        f,
        "---\na:\n  b:\n    c:\n      d:\n        e:\n          f:\n            g:\n              h:\n                i:\n                  j: deep\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::FrontmatterTooDeep),
        "expected FrontmatterTooDeep, got {err:?}"
    );
}

/// A deeply-nested FLOW-style document must return `Err` without stack overflow.
///
/// The iterative event-pull loop must stop at the depth bound before the structure
/// is materialised.  This test exercises the full `parse_toolset` path (not just
/// `parse_frontmatter`) to ensure the depth bound is wired end-to-end.
#[test]
fn ac8_deeply_nested_flow_document_refused_without_stack_overflow() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("deep-flow-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();

    // Build a frontmatter string with 20 000 open brackets (~20 KB of `[`).
    // Each `[` encodes one level of FLOW sequence nesting; MAX_DEPTH is 8, so
    // the guard fires after 9 `[` chars — the rest is never reached.
    let deep_brackets: String = "[".repeat(20_000);
    write!(f, "---\n{deep_brackets}\n---\n").unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(
            err,
            ToolsetFormatError::FrontmatterTooDeep
                | ToolsetFormatError::MalformedFrontmatter { .. }
        ),
        "expected FrontmatterTooDeep or MalformedFrontmatter for deeply-nested FLOW document; \
        got {err:?}"
    );
}

/// A deeply-nested FLOW mapping must also be refused before parser recursion.
///
/// `{a:{a:…}}` repeated to depth 5 000 must be caught by the event-level depth
/// bound without stack overflow.
#[test]
fn ac8_deeply_nested_flow_mapping_refused_without_stack_overflow() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("deep-map-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();

    // 5 000 levels of `{a:` is ~15 KB.
    let levels = 5_000_usize;
    let deep_braces: String = "{a:".repeat(levels);
    write!(f, "---\n{deep_braces}\n---\n").unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(
            err,
            ToolsetFormatError::FrontmatterTooDeep
                | ToolsetFormatError::MalformedFrontmatter { .. }
        ),
        "expected FrontmatterTooDeep or MalformedFrontmatter for deeply-nested FLOW mapping; \
        got {err:?}"
    );
}

// ── Render hardening ──────────────────────────────────────────────────────────

#[test]
fn ac9_ansi_in_error_token_is_sanitised() {
    // A token with an ANSI escape sequence; the Display of the error must not
    // echo raw escape bytes.
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // Token with ANSI red-text escape before "Unknown" and reset after.
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: \"\\x1b[31mUnknown\\x1b[0m\"\n---\n"
    )
    .unwrap();

    let result = parse_toolset(&toolset_dir);
    // Whether it errors or succeeds, the error Display must not contain raw ESC.
    if let Err(ref e) = result {
        let rendered = e.to_string();
        assert!(
            !rendered.contains('\x1b'),
            "ANSI escape leaked into error message: {rendered:?}"
        );
    }
}

#[test]
fn ac9_newline_in_error_token_sanitised() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // Capability token with a literal newline injected via YAML scalar folding.
    // YAML plain scalars cannot contain newlines, so use a double-quoted scalar.
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: \"read\\nbalance\"\n---\n"
    )
    .unwrap();

    let result = parse_toolset(&toolset_dir);
    if let Err(ref e) = result {
        let rendered = e.to_string();
        assert!(
            !rendered.contains('\n') || rendered.lines().count() == 1,
            "newline leaked into single-line error message: {rendered:?}"
        );
    }
}

#[test]
fn ac9_10kb_token_truncated_in_error_display() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // A 10 KB token; write as a quoted string to avoid YAML parsing issues.
    let big_token = "a".repeat(10_240);
    write!(
        f,
        "---\nname: test-toolset\ndescription: test\nmetadata:\n  stellar-agent-capabilities: \"{big_token}\"\n---\n"
    )
    .unwrap();

    if let Err(e) = parse_toolset(&toolset_dir) {
        let rendered = e.to_string();
        // The rendered error must not be more than ~300 chars (256 + overhead).
        assert!(
            rendered.len() <= 512,
            "large token not truncated in error: len={}",
            rendered.len()
        );
        // Must end with the ellipsis marker if truncated.
        assert!(
            rendered.contains('\u{2026}'),
            "truncated error should contain ellipsis: {rendered}"
        );
    }
}

// ── No-panic table ────────────────────────────────────────────────────────────

/// Feed adversarial inputs through `parse_toolset` and assert that each one
/// returns `Err`, never panics, OOMs, or stack-overflows.
#[test]
fn ac10_no_panic_table() {
    use std::io::Write;

    struct Case {
        name: &'static str,
        content: &'static [u8],
    }

    let cases: &[Case] = &[
        Case {
            name: "truncated-utf8",
            content: b"---\nname: foo\ndesc",
        },
        Case {
            name: "garbage-binary",
            content: b"---\n\x00\x01\x02\x03\xff\xfe\n---\n",
        },
        Case {
            name: "alias-bomb",
            content: b"---\na: &a large\nb: *a\n---\n",
        },
        Case {
            name: "deep-nest",
            content: b"---\na:\n  b:\n    c:\n      d:\n        e:\n          f:\n            g:\n              h:\n                i: x\n---\n",
        },
        Case {
            name: "empty-file",
            content: b"",
        },
        Case {
            name: "only-fence",
            content: b"---\n",
        },
    ];

    for case in cases {
        let tmp = tempfile::TempDir::new().unwrap();
        let toolset_dir = tmp.path().join("test-toolset");
        std::fs::create_dir_all(&toolset_dir).unwrap();
        let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
        f.write_all(case.content).unwrap();

        let result = parse_toolset(&toolset_dir);
        assert!(
            result.is_err(),
            "no-panic table case '{}' unexpectedly succeeded",
            case.name
        );
    }
}

// ── Forward-compatibility: unknown keys are skipped, known keys still parsed ──

/// An unknown mapping-valued top-level key BEFORE the `metadata` block must not
/// swallow the real metadata block.  The `read-balance` capability declared AFTER
/// the unknown map must be parsed.
#[test]
fn unknown_map_before_metadata_capabilities_preserved() {
    let dir = fixture("forward-compat-unknown-map/test-toolset");
    let toolset =
        parse_toolset(&dir).expect("toolset with unknown map before metadata should parse");
    assert!(
        toolset
            .capabilities
            .contains(stellar_agent_toolsets::Capability::ReadBalance),
        "read-balance must be parsed even when an unknown map precedes the metadata block; \
        got capabilities: {:?}",
        toolset.capabilities.len()
    );
    assert_eq!(toolset.capabilities.len(), 1);
}

/// `sign-transaction` in `metadata` placed AFTER a leading unknown nested mapping
/// must still produce `BareSignTransactionForbidden`, NOT `Ok` with an empty
/// capability set.
#[test]
fn sign_transaction_behind_unknown_map_still_refused() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // NOTE: indentation must be explicit in the literal (no `\n\` continuation).
    f.write_all(
        b"---\nname: test-toolset\ndescription: test\nextended-info:\n  author: example\n  url: https://example.com\nmetadata:\n  stellar-agent-capabilities: sign-transaction\n---\n",
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::BareSignTransactionForbidden),
        "expected BareSignTransactionForbidden when sign-transaction is behind an unknown map; \
        got {err:?}"
    );
}

/// An unknown mapping-valued top-level key followed by a `stellar-agent-policy`
/// metadata key must still produce `ReservedMetadataKey`.
#[test]
fn unknown_map_then_reserved_metadata_key_refused() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    f.write_all(
        b"---\nname: test-toolset\ndescription: test\nextended-info:\n  author: example\nmetadata:\n  stellar-agent-policy: some-policy\n---\n",
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::ReservedMetadataKey { .. }),
        "expected ReservedMetadataKey; got {err:?}"
    );
}

/// An inner key of an unknown nested map whose name equals a top-level key
/// (e.g. `name:`) must NOT produce a false `DuplicateKey` error.
/// The inner key lives in a different (skipped) namespace.
#[test]
fn inner_key_same_as_top_level_no_false_duplicate() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // The inner `name:` key inside `extended-info` must be skipped, not inserted
    // into top_level_seen_keys.
    // NOTE: use write_all with byte slices to avoid Rust string-literal `\n\`
    // line-continuation stripping indentation.
    f.write_all(
        b"---\nname: test-toolset\ndescription: A test toolset.\nextended-info:\n  name: inner-name-should-be-skipped\n  version: \"1.0\"\nmetadata:\n  stellar-agent-capabilities: read-balance\n---\n",
    )
    .unwrap();

    let toolset = parse_toolset(&toolset_dir)
        .expect("inner key with same name as top-level key must NOT produce DuplicateKey");
    assert!(
        toolset
            .capabilities
            .contains(stellar_agent_toolsets::Capability::ReadBalance)
    );
}

/// An unknown SEQUENCE-valued top-level key must be skipped without producing a
/// spurious `DuplicateKey` error.
#[test]
fn unknown_sequence_top_level_no_spurious_error() {
    let dir = fixture("forward-compat-unknown-seq/test-toolset");
    let toolset =
        parse_toolset(&dir).expect("toolset with unknown sequence top-level key should parse");
    assert!(
        toolset
            .capabilities
            .contains(stellar_agent_toolsets::Capability::ReadBalance),
        "read-balance must be parsed after a top-level sequence key; got {:?}",
        toolset.capabilities.len()
    );
}

/// Malformed YAML must produce `MalformedFrontmatter`.
#[test]
fn ac5_malformed_yaml_produces_malformed_frontmatter() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // Deliberately invalid YAML: unmatched quote, tab in indentation.
    write!(
        f,
        "---\nname: test-toolset\ndescription: \"unterminated\nfoo: bar\n---\n"
    )
    .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::MalformedFrontmatter { .. }),
        "expected MalformedFrontmatter for invalid YAML, got {err:?}"
    );
}

/// Non-UTF-8 bytes must produce `NotUtf8`.
#[test]
fn ac5_non_utf8_bytes_produce_not_utf8() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // Write a frontmatter fence followed by invalid UTF-8 bytes.
    f.write_all(b"---\n").unwrap();
    f.write_all(b"\xff\xfe invalid utf8\n").unwrap();
    f.write_all(b"---\n").unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::NotUtf8),
        "expected NotUtf8 for non-UTF-8 TOOLSET.md, got {err:?}"
    );
}

/// CRLF frontmatter fence must be accepted (`split_frontmatter` handles CRLF).
#[test]
fn ac5_crlf_frontmatter_fence_accepted() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // CRLF line endings throughout, including the opening fence.
    f.write_all(b"---\r\nname: test-toolset\r\ndescription: A test toolset.\r\n---\r\n")
        .unwrap();

    let toolset = parse_toolset(&toolset_dir).expect("CRLF frontmatter fence must be accepted");
    assert_eq!(toolset.name, "test-toolset");
}

/// A lone CR (`\r` without `\n`) opening fence is NOT accepted.
///
/// The agentskills format requires `\n` or `\r\n` line endings for the TOOLSET.md
/// fence; lone CR is not a recognised line ending.
#[test]
fn ac5_lone_cr_frontmatter_fence_refused() {
    use std::io::Write;
    let tmp = tempfile::TempDir::new().unwrap();
    let toolset_dir = tmp.path().join("test-toolset");
    std::fs::create_dir_all(&toolset_dir).unwrap();
    let mut f = std::fs::File::create(toolset_dir.join("TOOLSET.md")).unwrap();
    // Lone CR after the opening fence (not CRLF, not LF).
    f.write_all(b"---\rname: test-toolset\rdescription: A test toolset.\r---\r")
        .unwrap();

    let err = parse_toolset(&toolset_dir).unwrap_err();
    assert!(
        matches!(err, ToolsetFormatError::MissingFrontmatter),
        "lone CR opening fence must produce MissingFrontmatter, got {err:?}"
    );
}

// ── Render sanitisation: error Display does not leak raw control chars ────────

#[test]
fn error_display_io_is_sanitised() {
    let err = ToolsetFormatError::Io {
        detail: "error\x01detail".to_owned(),
    };
    let s = err.to_string();
    assert!(!s.contains('\x01'), "control char leaked: {s:?}");
}

#[test]
fn error_display_duplicate_key_sanitised() {
    let err = ToolsetFormatError::DuplicateKey {
        key: "key\x1b[31mred\x1b[0m".to_owned(),
    };
    let s = err.to_string();
    assert!(!s.contains('\x1b'), "ANSI escape leaked: {s:?}");
}

#[test]
fn error_display_unknown_capability_sanitised() {
    let err = ToolsetFormatError::UnknownCapability {
        token: "tok\ninjection".to_owned(),
    };
    let s = err.to_string();
    // The rendered error must be a single line (no newline injection).
    assert!(s.lines().count() == 1, "newline injected into error: {s:?}");
}