merry-runtime 0.1.3

Structured session, task ledger, context compaction, memory, permission, and tool runtime for Merry.
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
use super::{PathAccessDecision, PathDenialReason, WorkspaceAccessPolicy};
use crate::{
    PathAccess, PathAccessRule, PathAccessRuleSource, ProcessPathGrant, ProcessPathGrantConstraint,
    ProcessSessionPermissions,
};
use std::path::PathBuf;

#[test]
fn workspace_access_policy_enforces_workspace_baseline_session_and_once_grants() {
    let workspace = tempfile::tempdir().expect("workspace");
    let outside = tempfile::tempdir().expect("outside");
    let session = ProcessSessionPermissions::new();

    let policy = WorkspaceAccessPolicy::new(workspace.path(), true)
        .with_static_rules([PathAccessRule::new(
            outside.path().join("readonly"),
            PathAccess::ReadOnly,
            PathAccessRuleSource::TrustedGlobalConfig,
        )])
        .with_session_permissions(session.view());

    assert_eq!(
        policy.evaluate_read(&workspace.path().join("src/lib.rs"), &[]),
        PathAccessDecision::Allow
    );
    assert_eq!(
        policy.evaluate_write(&workspace.path().join("src/lib.rs"), &[]),
        PathAccessDecision::Allow
    );

    let ro_file = outside.path().join("readonly/doc.txt");
    assert_eq!(
        policy.evaluate_read(&ro_file, &[]),
        PathAccessDecision::Allow
    );
    assert_eq!(
        policy.evaluate_write(&ro_file, &[]),
        PathAccessDecision::Deny
    );

    let secret = outside.path().join("secret/id_rsa");
    assert_eq!(
        policy.evaluate_read(&secret, &[]),
        PathAccessDecision::RequireReview
    );
    assert_eq!(
        policy.evaluate_write(&secret, &[]),
        PathAccessDecision::RequireReview
    );

    let once = [PathAccessRule::new(
        &secret,
        PathAccess::ReadOnly,
        PathAccessRuleSource::PermissionReview,
    )];
    assert_eq!(
        policy.evaluate_read(&secret, &once),
        PathAccessDecision::Allow
    );
    assert_eq!(
        policy.evaluate_write(&secret, &once),
        PathAccessDecision::RequireReview
    );
    assert_eq!(
        policy.evaluate_read(&secret, &[]),
        PathAccessDecision::RequireReview
    );

    let session_dir = outside.path().join("shared");
    session
        .retain_path_grants(vec![ProcessPathGrant::new(
            PathAccessRule::new(
                &session_dir,
                PathAccess::ReadWrite,
                PathAccessRuleSource::PermissionReview,
            ),
            ProcessPathGrantConstraint::Ordinary,
        )])
        .expect("retain session path");
    assert_eq!(
        policy.evaluate_read(&session_dir.join("a.txt"), &[]),
        PathAccessDecision::Allow
    );
    assert_eq!(
        policy.evaluate_write(&session_dir.join("a.txt"), &[]),
        PathAccessDecision::Allow
    );

    session.clear().expect("clear session");
    assert_eq!(
        policy.evaluate_read(&session_dir.join("a.txt"), &[]),
        PathAccessDecision::RequireReview
    );
}

#[test]
fn read_only_workspace_denies_workspace_writes() {
    let workspace = tempfile::tempdir().expect("workspace");
    let policy = WorkspaceAccessPolicy::new(workspace.path(), false);
    assert_eq!(
        policy.evaluate_read(&workspace.path().join("README.md"), &[]),
        PathAccessDecision::Allow
    );
    assert_eq!(
        policy.evaluate_write(&workspace.path().join("README.md"), &[]),
        PathAccessDecision::Deny
    );
    assert_eq!(
        policy.workspace_root(),
        PathBuf::from(workspace.path()).as_path()
    );
}

#[cfg(unix)]
#[test]
fn symlink_inside_workspace_or_allowed_dir_cannot_escape_policy() {
    use std::os::unix::fs::symlink;

    let root = tempfile::tempdir().expect("root");
    let workspace = root.path().join("workspace");
    let allowed = root.path().join("allowed");
    let secret_dir = root.path().join("secret");
    std::fs::create_dir_all(&workspace).expect("workspace");
    std::fs::create_dir_all(&allowed).expect("allowed");
    std::fs::create_dir_all(&secret_dir).expect("secret");
    let secret_file = secret_dir.join("id_rsa");
    std::fs::write(&secret_file, "private").expect("seed secret");

    let ws_escape = workspace.join("escape");
    symlink(&secret_file, &ws_escape).expect("workspace symlink");
    let allowed_escape = allowed.join("escape");
    symlink(&secret_file, &allowed_escape).expect("allowed symlink");

    let policy =
        WorkspaceAccessPolicy::new(&workspace, true).with_static_rules([PathAccessRule::new(
            &allowed,
            PathAccess::ReadWrite,
            PathAccessRuleSource::TrustedGlobalConfig,
        )]);

    assert_eq!(
        policy.evaluate_read(&ws_escape, &[]),
        PathAccessDecision::RequireReview,
        "symlink inside workspace pointing outside must not inherit workspace access"
    );
    assert_eq!(
        policy.evaluate_write(&ws_escape, &[]),
        PathAccessDecision::RequireReview,
        "symlink inside workspace pointing outside must not inherit workspace write access"
    );
    assert_eq!(
        policy.evaluate_read(&allowed_escape, &[]),
        PathAccessDecision::RequireReview,
        "symlink inside allowed rule pointing outside must not inherit rule access"
    );
}

#[test]
fn static_deny_and_trusted_readonly_ceilings_return_deny_while_allowing_deeper_overrides() {
    let root = tempfile::tempdir().expect("root");
    let parent = root.path().join("parent");
    let workspace = parent.join("workspace");
    let external = root.path().join("external");
    let secret = external.join("providers/api.key");
    let ro_dir = external.join("readonly");
    let deeper_rw = ro_dir.join("writable");
    std::fs::create_dir_all(&workspace).expect("workspace");
    std::fs::create_dir_all(secret.parent().expect("parent")).expect("providers");
    std::fs::create_dir_all(&deeper_rw).expect("deeper rw");
    std::fs::write(&secret, "secret").expect("secret");
    let ro_file = ro_dir.join("config.txt");
    std::fs::write(&ro_file, "readonly").expect("ro file");
    let rw_file = deeper_rw.join("output.txt");
    std::fs::write(&rw_file, "writable").expect("rw file");

    let session = ProcessSessionPermissions::new();
    session
        .retain_path_grants(vec![ProcessPathGrant::new(
            PathAccessRule::new(
                &external,
                PathAccess::ReadWrite,
                PathAccessRuleSource::PermissionReview,
            ),
            ProcessPathGrantConstraint::Ordinary,
        )])
        .expect("retain broad external session grant");

    let policy = WorkspaceAccessPolicy::new(&workspace, true)
        .with_static_rules([
            PathAccessRule::new(
                &parent,
                PathAccess::ReadOnly,
                PathAccessRuleSource::TrustedGlobalConfig,
            ),
            PathAccessRule::new(
                external.join("providers"),
                PathAccess::Deny,
                PathAccessRuleSource::ProductPrivate,
            ),
            PathAccessRule::new(
                external.join("user-denied"),
                PathAccess::Deny,
                PathAccessRuleSource::TrustedGlobalConfig,
            ),
            PathAccessRule::new(
                &ro_dir,
                PathAccess::ReadOnly,
                PathAccessRuleSource::TrustedGlobalConfig,
            ),
            PathAccessRule::new(
                &deeper_rw,
                PathAccess::ReadWrite,
                PathAccessRuleSource::TrustedGlobalConfig,
            ),
        ])
        .with_session_permissions(session.view());

    assert_eq!(
        policy.evaluate_read(&secret, &[]),
        PathAccessDecision::RequireReview,
        "ProductPrivate must require review even when a broad session grant exists"
    );
    let once_secret = [PathAccessRule::new(
        &secret,
        PathAccess::ReadWrite,
        PathAccessRuleSource::PermissionReview,
    )];
    assert_eq!(
        policy.evaluate_read(&secret, &once_secret),
        PathAccessDecision::Allow,
        "ProductPrivate must allow access when the user explicitly grants once"
    );
    assert_eq!(
        policy.evaluate_write(&secret, &once_secret),
        PathAccessDecision::Allow,
        "ProductPrivate must allow write when the user explicitly grants once"
    );

    let user_denied_file = external.join("user-denied/file.txt");
    let once_user_denied = [PathAccessRule::new(
        &user_denied_file,
        PathAccess::ReadWrite,
        PathAccessRuleSource::PermissionReview,
    )];
    assert_eq!(
        policy.evaluate_read(&user_denied_file, &once_user_denied),
        PathAccessDecision::Deny,
        "user-configured Deny must fail closed even with once/session grants"
    );

    let once_ro = [PathAccessRule::new(
        &ro_file,
        PathAccess::ReadWrite,
        PathAccessRuleSource::PermissionReview,
    )];
    assert_eq!(
        policy.evaluate_read(&ro_file, &[]),
        PathAccessDecision::Allow
    );
    assert_eq!(
        policy.evaluate_write(&ro_file, &once_ro),
        PathAccessDecision::Deny,
        "TrustedGlobalConfig ReadOnly ceiling must hard-deny write escalation via session or once grants"
    );

    assert_eq!(
        policy.evaluate_write(&workspace.join("src/lib.rs"), &[]),
        PathAccessDecision::Allow
    );
    assert_eq!(
        policy.evaluate_write(&rw_file, &[]),
        PathAccessDecision::Allow
    );
}

#[test]
fn unified_path_precedence_eliminates_all_four_legacy_drifts_and_note_redlines() {
    let root = tempfile::tempdir().expect("tempdir");
    let workspace = root.path().join("workspace");
    let cfg_ro = root.path().join("cfg-ro");
    let cfg_rw_child = cfg_ro.join("writable");
    let denied_root = root.path().join("denied-root");
    let product_ro_under_denied = denied_root.join("config-merry");
    let shared_equal_depth = root.path().join("shared-equal");
    let product_private = root.path().join("providers");
    let ws_equal_depth = root.path().join("ws-equal");
    let shallow_home = root.path().join("home");
    let shallow_config = shallow_home.join(".config");
    let deep_product_ro = shallow_config.join("merry");
    for dir in [
        &workspace,
        &cfg_rw_child,
        &product_ro_under_denied,
        &shared_equal_depth,
        &ws_equal_depth,
        &product_private,
        &deep_product_ro,
    ] {
        std::fs::create_dir_all(dir).expect("create dir");
    }

    let policy = WorkspaceAccessPolicy::new(&workspace, true).with_static_rules([
        PathAccessRule::new(
            &cfg_ro,
            PathAccess::ReadOnly,
            PathAccessRuleSource::TrustedGlobalConfig,
        ),
        PathAccessRule::new(
            &cfg_rw_child,
            PathAccess::ReadWrite,
            PathAccessRuleSource::TrustedGlobalConfig,
        ),
        PathAccessRule::new(
            &denied_root,
            PathAccess::Deny,
            PathAccessRuleSource::TrustedGlobalConfig,
        ),
        PathAccessRule::new(
            &product_ro_under_denied,
            PathAccess::ReadOnly,
            PathAccessRuleSource::ProductReadOnly,
        ),
        PathAccessRule::new(
            &shared_equal_depth,
            PathAccess::ReadWrite,
            PathAccessRuleSource::TrustedGlobalConfig,
        ),
        PathAccessRule::new(
            &shared_equal_depth,
            PathAccess::ReadOnly,
            PathAccessRuleSource::ProductReadOnly,
        ),
        PathAccessRule::new(
            &ws_equal_depth,
            PathAccess::ReadWrite,
            PathAccessRuleSource::WorkspacePermissions,
        ),
        PathAccessRule::new(
            &ws_equal_depth,
            PathAccess::ReadOnly,
            PathAccessRuleSource::ProductReadOnly,
        ),
        PathAccessRule::new(
            &product_private,
            PathAccess::Deny,
            PathAccessRuleSource::TrustedGlobalConfig,
        ),
        PathAccessRule::new(
            &product_private,
            PathAccess::Deny,
            PathAccessRuleSource::ProductPrivate,
        ),
        PathAccessRule::new(
            &shallow_home,
            PathAccess::ReadWrite,
            PathAccessRuleSource::TrustedGlobalConfig,
        ),
        PathAccessRule::new(
            &deep_product_ro,
            PathAccess::ReadOnly,
            PathAccessRuleSource::ProductReadOnly,
        ),
    ]);

    // Drift 1: TrustedGlobalConfig RO ancestor + deeper TrustedGlobalConfig RW -> Allow write.
    assert_eq!(
        policy.evaluate_write(&cfg_rw_child.join("out.txt"), &[]),
        PathAccessDecision::Allow
    );

    // Drift 2: ProductReadOnly under TrustedGlobalConfig Deny -> Deny on BOTH read and write.
    assert_eq!(
        policy.evaluate_read(&product_ro_under_denied.join("config.toml"), &[]),
        PathAccessDecision::Deny
    );
    assert_eq!(
        policy.evaluate_write(&product_ro_under_denied.join("config.toml"), &[]),
        PathAccessDecision::Deny
    );

    // Drift 3: Equal-depth TrustedGlobalConfig RW overrides default ProductReadOnly,
    // while equal-depth WorkspacePermissions RW cannot override ProductReadOnly.
    let trusted_equal = policy.evaluate_path(
        &shared_equal_depth.join("file.txt"),
        PathAccess::ReadWrite,
        None,
        &[],
    );
    assert_eq!(trusted_equal.decision, PathAccessDecision::Allow);
    assert!(!trusted_equal.protected_target);

    let ws_equal = policy.evaluate_path(
        &ws_equal_depth.join("file.txt"),
        PathAccess::ReadWrite,
        None,
        &[],
    );
    assert_eq!(ws_equal.decision, PathAccessDecision::RequireReview);
    assert!(ws_equal.protected_target);

    // Shallower TrustedGlobalConfig RW ancestor (home) must NOT bypass deeper ProductReadOnly
    // (home/.config/merry) in either single-path or directory-grant checks.
    let deep_ro_single = policy.evaluate_path(
        &deep_product_ro.join("config.toml"),
        PathAccess::ReadWrite,
        None,
        &[],
    );
    assert_eq!(deep_ro_single.decision, PathAccessDecision::RequireReview);
    assert!(deep_ro_single.protected_target);
    let shallow_dir_grant =
        policy.evaluate_directory_grant(&shallow_config, PathAccess::ReadWrite, None, &[]);
    assert_eq!(shallow_dir_grant.decision, PathAccessDecision::Deny);
    assert_eq!(
        shallow_dir_grant.denial_reason,
        Some(PathDenialReason::CoversProtectedReadOnlyDescendant)
    );

    // Drift 4: User TrustedGlobalConfig Deny vs ProductPrivate -> hard Deny even with once grant.
    let once_private = [PathAccessRule::new(
        &product_private,
        PathAccess::ReadOnly,
        PathAccessRuleSource::PermissionReview,
    )];
    assert_eq!(
        policy.evaluate_read(&product_private.join("secret.key"), &once_private),
        PathAccessDecision::Deny
    );

    // Unified user_note validation accepts multiline/tab notes and rejects control chars.
    assert_eq!(
        crate::validate_permission_user_note("  line 1\n\tline 2  ").expect("multiline note"),
        "line 1\n\tline 2"
    );
    assert!(crate::validate_permission_user_note("bad\u{0000}note").is_err());
}