a3s-code-core 3.2.0

A3S Code Core - Embeddable AI agent library with tool execution
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
use super::*;
use serde_json::json;

// ========================================================================
// PermissionRule Tests
// ========================================================================

#[test]
fn test_rule_parse_simple() {
    let rule = PermissionRule::new("Bash");
    assert_eq!(rule.tool_name, Some("Bash".to_string()));
    assert_eq!(rule.arg_pattern, None);
}

#[test]
fn test_rule_parse_with_pattern() {
    let rule = PermissionRule::new("Bash(cargo:*)");
    assert_eq!(rule.tool_name, Some("Bash".to_string()));
    assert_eq!(rule.arg_pattern, Some("cargo:*".to_string()));
}

#[test]
fn test_rule_parse_wildcard() {
    let rule = PermissionRule::new("Grep(*)");
    assert_eq!(rule.tool_name, Some("Grep".to_string()));
    assert_eq!(rule.arg_pattern, Some("*".to_string()));
}

#[test]
fn test_rule_match_tool_only() {
    let rule = PermissionRule::new("Bash");
    assert!(rule.matches("Bash", &json!({"command": "ls -la"})));
    assert!(rule.matches("bash", &json!({"command": "echo hello"})));
    assert!(!rule.matches("Read", &json!({})));
}

#[test]
fn test_rule_match_wildcard() {
    let rule = PermissionRule::new("Grep(*)");
    assert!(rule.matches("Grep", &json!({"pattern": "foo", "path": "/tmp"})));
    assert!(rule.matches("grep", &json!({"pattern": "bar"})));
}

#[test]
fn test_rule_match_prefix_wildcard() {
    let rule = PermissionRule::new("Bash(cargo:*)");
    assert!(rule.matches("Bash", &json!({"command": "cargo build"})));
    assert!(rule.matches("Bash", &json!({"command": "cargo test --lib"})));
    assert!(rule.matches("Bash", &json!({"command": "cargo"})));
    assert!(!rule.matches("Bash", &json!({"command": "npm install"})));
}

#[test]
fn test_rule_match_npm_commands() {
    let rule = PermissionRule::new("Bash(npm run:*)");
    assert!(rule.matches("Bash", &json!({"command": "npm run test"})));
    assert!(rule.matches("Bash", &json!({"command": "npm run build"})));
    assert!(!rule.matches("Bash", &json!({"command": "npm install"})));
}

#[test]
fn test_rule_match_file_path() {
    let rule = PermissionRule::new("Read(src/*.rs)");
    assert!(rule.matches("Read", &json!({"file_path": "src/main.rs"})));
    assert!(rule.matches("Read", &json!({"file_path": "src/lib.rs"})));
    assert!(!rule.matches("Read", &json!({"file_path": "src/foo/bar.rs"})));
}

#[test]
fn test_rule_match_recursive_glob() {
    let rule = PermissionRule::new("Read(src/**/*.rs)");
    assert!(rule.matches("Read", &json!({"file_path": "src/main.rs"})));
    assert!(rule.matches("Read", &json!({"file_path": "src/foo/bar.rs"})));
    assert!(rule.matches("Read", &json!({"file_path": "src/a/b/c.rs"})));
}

#[test]
fn test_rule_match_mcp_tool() {
    let rule = PermissionRule::new("mcp__pencil");
    assert!(rule.matches("mcp__pencil", &json!({})));
    assert!(rule.matches("mcp__pencil__batch_design", &json!({})));
    assert!(rule.matches("mcp__pencil__batch_get", &json!({})));
    assert!(!rule.matches("mcp__other", &json!({})));
}

#[test]
fn test_rule_match_mcp_tool_wildcard() {
    // "mcp__longvt__*" must match all tools from the longvt server.
    // Previously this failed because matches_tool_name treated '*' as a
    // literal character and starts_with("mcp__longvt__*") always returned false.
    let rule = PermissionRule::new("mcp__longvt__*");
    assert!(rule.matches("mcp__longvt__search", &json!({})));
    assert!(rule.matches("mcp__longvt__create_memory", &json!({})));
    assert!(rule.matches("mcp__longvt__delete", &json!({})));
    assert!(!rule.matches("mcp__pencil__batch_design", &json!({})));
    assert!(!rule.matches("mcp__other__tool", &json!({})));

    // "mcp__*" must match any MCP tool from any server.
    let rule_all = PermissionRule::new("mcp__*");
    assert!(rule_all.matches("mcp__longvt__search", &json!({})));
    assert!(rule_all.matches("mcp__pencil__draw", &json!({})));
    assert!(!rule_all.matches("bash", &json!({})));
}

#[test]
fn test_rule_case_insensitive() {
    let rule = PermissionRule::new("BASH(cargo:*)");
    assert!(rule.matches("Bash", &json!({"command": "cargo build"})));
    assert!(rule.matches("bash", &json!({"command": "cargo test"})));
    assert!(rule.matches("BASH", &json!({"command": "cargo check"})));
}

// ========================================================================
// PermissionPolicy Tests
// ========================================================================

#[test]
fn test_policy_default() {
    let policy = PermissionPolicy::default();
    assert!(policy.enabled);
    assert_eq!(policy.default_decision, PermissionDecision::Ask);
    assert!(policy.allow.is_empty());
    assert!(policy.deny.is_empty());
    assert!(policy.ask.is_empty());
}

#[test]
fn test_policy_explicit_allow_default() {
    let policy = PermissionPolicy {
        default_decision: PermissionDecision::Allow,
        ..PermissionPolicy::default()
    };
    assert_eq!(policy.default_decision, PermissionDecision::Allow);
}

#[test]
fn test_policy_strict() {
    let policy = PermissionPolicy::strict();
    assert_eq!(policy.default_decision, PermissionDecision::Ask);
}

#[test]
fn test_policy_builder() {
    let policy = PermissionPolicy::new()
        .allow("Bash(cargo:*)")
        .allow("Grep(*)")
        .deny("Bash(rm -rf:*)")
        .ask("Write(*)");

    assert_eq!(policy.allow.len(), 2);
    assert_eq!(policy.deny.len(), 1);
    assert_eq!(policy.ask.len(), 1);
}

#[test]
fn test_policy_check_allow() {
    let policy = PermissionPolicy::new().allow("Bash(cargo:*)");

    let decision = policy.check("Bash", &json!({"command": "cargo build"}));
    assert_eq!(decision, PermissionDecision::Allow);
}

#[test]
fn test_policy_check_deny() {
    let policy = PermissionPolicy::new().deny("Bash(rm -rf:*)");

    let decision = policy.check("Bash", &json!({"command": "rm -rf /"}));
    assert_eq!(decision, PermissionDecision::Deny);
}

#[test]
fn test_policy_check_ask() {
    let policy = PermissionPolicy::new().ask("Write(*)");

    let decision = policy.check("Write", &json!({"file_path": "/tmp/test.txt"}));
    assert_eq!(decision, PermissionDecision::Ask);
}

#[test]
fn test_policy_check_default() {
    let policy = PermissionPolicy::new();

    let decision = policy.check("Unknown", &json!({}));
    assert_eq!(decision, PermissionDecision::Ask);
}

#[test]
fn test_policy_deny_wins_over_allow() {
    let policy = PermissionPolicy::new().allow("Bash(*)").deny("Bash(rm:*)");

    // Allow matches, but deny also matches - deny wins
    let decision = policy.check("Bash", &json!({"command": "rm -rf /tmp"}));
    assert_eq!(decision, PermissionDecision::Deny);

    // Only allow matches
    let decision = policy.check("Bash", &json!({"command": "ls -la"}));
    assert_eq!(decision, PermissionDecision::Allow);
}

#[test]
fn test_policy_allow_wins_over_ask() {
    let policy = PermissionPolicy::new()
        .allow("Bash(cargo:*)")
        .ask("Bash(*)");

    // Both match, but allow is checked before ask
    let decision = policy.check("Bash", &json!({"command": "cargo build"}));
    assert_eq!(decision, PermissionDecision::Allow);

    // Only ask matches
    let decision = policy.check("Bash", &json!({"command": "npm install"}));
    assert_eq!(decision, PermissionDecision::Ask);
}

#[test]
fn test_policy_disabled() {
    let mut policy = PermissionPolicy::new().deny("Bash(rm:*)").ask("Bash(*)");
    policy.enabled = false;

    // When disabled, everything is allowed
    let decision = policy.check("Bash", &json!({"command": "rm -rf /"}));
    assert_eq!(decision, PermissionDecision::Allow);
}

#[test]
fn test_policy_is_allowed() {
    let policy = PermissionPolicy::new().allow("Bash(cargo:*)");

    assert!(policy.is_allowed("Bash", &json!({"command": "cargo build"})));
    assert!(!policy.is_allowed("Bash", &json!({"command": "npm install"})));
}

#[test]
fn test_policy_is_denied() {
    let policy = PermissionPolicy::new().deny("Bash(rm:*)");

    assert!(policy.is_denied("Bash", &json!({"command": "rm -rf /"})));
    assert!(!policy.is_denied("Bash", &json!({"command": "ls -la"})));
}

#[test]
fn test_policy_requires_confirmation() {
    // Create a policy that explicitly allows Read but asks for Write
    let mut policy = PermissionPolicy::new().allow("Read(*)").ask("Write(*)");
    policy.default_decision = PermissionDecision::Deny; // Make default deny to test ask rule

    assert!(policy.requires_confirmation("Write", &json!({"file_path": "/tmp/test"})));
    assert!(!policy.requires_confirmation("Read", &json!({"file_path": "/tmp/test"})));
}

#[test]
fn test_policy_matching_rules() {
    let policy = PermissionPolicy::new()
        .allow("Bash(cargo:*)")
        .deny("Bash(cargo fmt:*)")
        .ask("Bash(*)");

    let matching = policy.get_matching_rules("Bash", &json!({"command": "cargo fmt"}));
    assert_eq!(matching.deny.len(), 1);
    assert_eq!(matching.allow.len(), 1);
    assert_eq!(matching.ask.len(), 1);
}

#[test]
fn test_policy_allow_all() {
    let policy = PermissionPolicy::new().allow_all(&["Bash(cargo:*)", "Bash(npm:*)", "Grep(*)"]);

    assert_eq!(policy.allow.len(), 3);
    assert!(policy.is_allowed("Bash", &json!({"command": "cargo build"})));
    assert!(policy.is_allowed("Bash", &json!({"command": "npm run test"})));
    assert!(policy.is_allowed("Grep", &json!({"pattern": "foo"})));
}

// ========================================================================
// PermissionRule Deserialization Tests
// ========================================================================

#[test]
fn test_rule_deserialize_plain_string() {
    // YAML: `- read`
    let rule: PermissionRule = serde_yaml::from_str("read").unwrap();
    assert_eq!(rule.rule, "read");
    assert!(rule.matches("read", &json!({})));
    assert!(!rule.matches("write", &json!({})));
}

#[test]
fn test_rule_deserialize_plain_string_with_pattern() {
    // YAML: `- "Bash(cargo:*)"`
    let rule: PermissionRule = serde_yaml::from_str("\"Bash(cargo:*)\"").unwrap();
    assert_eq!(rule.rule, "Bash(cargo:*)");
    assert!(rule.matches("Bash", &json!({"command": "cargo build"})));
}

#[test]
fn test_rule_deserialize_struct_form() {
    // YAML: `- rule: read`
    let rule: PermissionRule = serde_yaml::from_str("rule: read").unwrap();
    assert_eq!(rule.rule, "read");
    assert!(rule.matches("read", &json!({})));
}

#[test]
fn test_rule_deserialize_in_policy() {
    // Full policy YAML with mixed formats
    let yaml = r#"
allow:
  - read
  - "Bash(cargo:*)"
  - rule: grep
deny:
  - write
"#;
    let policy: PermissionPolicy = serde_yaml::from_str(yaml).unwrap();
    assert_eq!(policy.allow.len(), 3);
    assert_eq!(policy.deny.len(), 1);
    assert!(policy.is_allowed("read", &json!({})));
    assert!(policy.is_allowed("Bash", &json!({"command": "cargo build"})));
    assert!(policy.is_allowed("grep", &json!({})));
    assert!(policy.is_denied("write", &json!({})));
}

// ========================================================================
// PermissionManager Tests
// ========================================================================

#[test]
fn test_manager_default() {
    let manager = PermissionManager::new();
    assert_eq!(
        manager.global_policy().default_decision,
        PermissionDecision::Ask
    );
}

#[test]
fn test_manager_with_global_policy() {
    let policy = PermissionPolicy {
        default_decision: PermissionDecision::Allow,
        ..PermissionPolicy::default()
    };
    let manager = PermissionManager::with_global_policy(policy);
    assert_eq!(
        manager.global_policy().default_decision,
        PermissionDecision::Allow
    );
}

#[test]
fn test_manager_session_policy() {
    let mut manager = PermissionManager::new();

    let session_policy = PermissionPolicy::new().allow("Bash(cargo:*)");
    manager.set_session_policy("session-1", session_policy);

    // Session 1 has custom policy
    let decision = manager.check("session-1", "Bash", &json!({"command": "cargo build"}));
    assert_eq!(decision, PermissionDecision::Allow);

    // Session 2 uses global policy
    let decision = manager.check("session-2", "Bash", &json!({"command": "cargo build"}));
    assert_eq!(decision, PermissionDecision::Ask);
}

#[test]
fn test_manager_remove_session_policy() {
    let mut manager = PermissionManager::new();

    let session_policy = PermissionPolicy {
        default_decision: PermissionDecision::Allow,
        ..PermissionPolicy::default()
    };
    manager.set_session_policy("session-1", session_policy);

    // Before removal
    let decision = manager.check("session-1", "Bash", &json!({"command": "anything"}));
    assert_eq!(decision, PermissionDecision::Allow);

    manager.remove_session_policy("session-1");

    // After removal - falls back to global
    let decision = manager.check("session-1", "Bash", &json!({"command": "anything"}));
    assert_eq!(decision, PermissionDecision::Ask);
}

#[test]
fn test_manager_global_deny_overrides_session_allow() {
    let mut manager =
        PermissionManager::with_global_policy(PermissionPolicy::new().deny("Bash(rm:*)"));

    let session_policy = PermissionPolicy::new().allow("Bash(*)");
    manager.set_session_policy("session-1", session_policy);

    // Session allows all Bash, but global denies rm
    let decision = manager.check("session-1", "Bash", &json!({"command": "rm -rf /"}));
    assert_eq!(decision, PermissionDecision::Deny);

    // Other commands are allowed
    let decision = manager.check("session-1", "Bash", &json!({"command": "ls -la"}));
    assert_eq!(decision, PermissionDecision::Allow);
}

// ========================================================================
// Integration Tests
// ========================================================================

#[test]
fn test_realistic_dev_policy() {
    let policy = PermissionPolicy::new()
        // Allow common dev commands
        .allow_all(&[
            "Bash(cargo:*)",
            "Bash(npm:*)",
            "Bash(pnpm:*)",
            "Bash(just:*)",
            "Bash(git status:*)",
            "Bash(git diff:*)",
            "Bash(echo:*)",
            "Grep(*)",
            "Glob(*)",
            "Ls(*)",
        ])
        // Deny dangerous commands
        .deny_all(&["Bash(rm -rf:*)", "Bash(sudo:*)", "Bash(curl | sh:*)"])
        // Always ask for writes
        .ask_all(&["Write(*)", "Edit(*)"]);

    // Allowed
    assert!(policy.is_allowed("Bash", &json!({"command": "cargo build"})));
    assert!(policy.is_allowed("Bash", &json!({"command": "npm run test"})));
    assert!(policy.is_allowed("Grep", &json!({"pattern": "TODO"})));

    // Denied
    assert!(policy.is_denied("Bash", &json!({"command": "rm -rf /"})));
    assert!(policy.is_denied("Bash", &json!({"command": "sudo apt install"})));

    // Ask
    assert!(policy.requires_confirmation("Write", &json!({"file_path": "/tmp/test.rs"})));
    assert!(policy.requires_confirmation("Edit", &json!({"file_path": "src/main.rs"})));
}

#[test]
fn test_mcp_tool_permissions() {
    let policy = PermissionPolicy::new()
        .allow("mcp__pencil")
        .deny("mcp__dangerous");

    assert!(policy.is_allowed("mcp__pencil__batch_design", &json!({})));
    assert!(policy.is_allowed("mcp__pencil__batch_get", &json!({})));
    assert!(policy.is_denied("mcp__dangerous__execute", &json!({})));
}

#[test]
fn test_allow_by_default_with_mcp_wildcard_deny() {
    // Allow-by-default policies can still carry explicit deny rules.
    let policy = PermissionPolicy {
        default_decision: PermissionDecision::Allow,
        ..PermissionPolicy::default()
    }
    .deny("mcp__longvt__*");

    // longvt tools are denied
    assert_eq!(
        policy.check("mcp__longvt__search", &json!({})),
        PermissionDecision::Deny
    );
    assert_eq!(
        policy.check("mcp__longvt__create_memory", &json!({})),
        PermissionDecision::Deny
    );
    // other MCP tools are still allowed by the explicit default decision
    assert_eq!(
        policy.check("mcp__pencil__draw", &json!({})),
        PermissionDecision::Allow
    );
    assert_eq!(
        policy.check("bash", &json!({"command": "ls"})),
        PermissionDecision::Allow
    );
}

#[test]
fn test_serialization() {
    let policy = PermissionPolicy::new()
        .allow("Bash(cargo:*)")
        .deny("Bash(rm:*)");

    let json = serde_json::to_string(&policy).unwrap();
    let deserialized: PermissionPolicy = serde_json::from_str(&json).unwrap();

    assert_eq!(deserialized.allow.len(), 1);
    assert_eq!(deserialized.deny.len(), 1);
}

#[test]
fn test_matching_rules_is_empty() {
    let rules = MatchingRules {
        deny: vec![],
        allow: vec![],
        ask: vec![],
    };
    assert!(rules.is_empty());

    let rules = MatchingRules {
        deny: vec!["Bash".to_string()],
        allow: vec![],
        ask: vec![],
    };
    assert!(!rules.is_empty());

    let rules = MatchingRules {
        deny: vec![],
        allow: vec!["Read".to_string()],
        ask: vec![],
    };
    assert!(!rules.is_empty());

    let rules = MatchingRules {
        deny: vec![],
        allow: vec![],
        ask: vec!["Write".to_string()],
    };
    assert!(!rules.is_empty());
}

#[test]
fn test_permission_manager_default() {
    let pm = PermissionManager::default();
    let policy = pm.global_policy();
    assert!(policy.allow.is_empty());
    assert!(policy.deny.is_empty());
    assert!(policy.ask.is_empty());
}

#[test]
fn test_permission_manager_set_global_policy() {
    let mut pm = PermissionManager::new();
    let policy = PermissionPolicy::new().allow("Bash(*)");
    pm.set_global_policy(policy);
    assert_eq!(pm.global_policy().allow.len(), 1);
}

#[test]
fn test_permission_manager_session_policy() {
    let mut pm = PermissionManager::new();
    let policy = PermissionPolicy::new().deny("Bash(rm:*)");
    pm.set_session_policy("s1", policy);

    let effective = pm.get_effective_policy("s1");
    assert_eq!(effective.deny.len(), 1);

    // Non-existent session falls back to global
    let global = pm.get_effective_policy("s2");
    assert!(global.deny.is_empty());
}

#[test]
fn test_permission_manager_remove_session_policy() {
    let mut pm = PermissionManager::new();
    pm.set_session_policy("s1", PermissionPolicy::new().deny("Bash(*)"));
    assert_eq!(pm.get_effective_policy("s1").deny.len(), 1);

    pm.remove_session_policy("s1");
    assert!(pm.get_effective_policy("s1").deny.is_empty());
}

#[test]
fn test_permission_manager_check_deny() {
    let mut pm = PermissionManager::new();
    pm.set_global_policy(PermissionPolicy::new().deny("Bash(rm:*)"));

    let decision = pm.check("s1", "Bash", &json!({"command": "rm -rf /"}));
    assert_eq!(decision, PermissionDecision::Deny);
}

#[test]
fn test_permission_manager_check_allow() {
    let mut pm = PermissionManager::new();
    pm.set_global_policy(PermissionPolicy::new().allow("Bash(cargo:*)"));

    let decision = pm.check("s1", "Bash", &json!({"command": "cargo build"}));
    assert_eq!(decision, PermissionDecision::Allow);
}

#[test]
fn test_permission_manager_check_session_override() {
    let mut pm = PermissionManager::new();
    pm.set_global_policy(PermissionPolicy::new().allow("Bash(*)"));
    pm.set_session_policy("s1", PermissionPolicy::new().deny("Bash(rm:*)"));

    // Session policy denies rm
    let decision = pm.check("s1", "Bash", &json!({"command": "rm -rf /"}));
    assert_eq!(decision, PermissionDecision::Deny);

    // Other session uses global (allow all)
    let decision = pm.check("s2", "Bash", &json!({"command": "rm -rf /"}));
    assert_eq!(decision, PermissionDecision::Allow);
}

#[test]
fn test_permission_manager_with_global_policy() {
    let policy = PermissionPolicy::new().allow("Read(*)").deny("Write(*)");
    let pm = PermissionManager::with_global_policy(policy);
    assert_eq!(pm.global_policy().allow.len(), 1);
    assert_eq!(pm.global_policy().deny.len(), 1);
}