merry-coding 0.1.4

Coding prompts, project rules, tool catalog, permission policy, and subagent orchestration 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
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
use super::*;
use crate::{CodingAgentProfileBuilder, CodingModelRoleConfig, CodingPermissionPolicy};
use futures_util::StreamExt;
use merry_core::{
    RuntimeJournalEvent, RuntimeJournalPayload, SessionId, ToolCallResultStatus, ToolName,
};
use merry_llm::{
    FinishReason, GenerationConfig, ModelEvent, ModelName, ModelOutput, ModelProvider,
    ModelResponse, ModelRetryPolicy, ModelToolCall, ModelToolCallId, ToolArguments,
    testing::FakeModelProvider,
};
use merry_process::{LocalProcessBackend, ProcessBackend, ProcessSession, TokioProcessRunner};
use merry_runtime::{
    AcceptedLocalWorkspaceProcessAdmission, ChildWorkspaceScope, CitationCompactionPolicy,
    CompactionConfig, PermissionAdmissionContext, PermissionAdmissionDecision,
    PermissionAdmissionFuture, PermissionAdmissionSource, ProcessRunner as RuntimeProcessRunner,
    Runtime, RuntimeModelRole, StaticPermissionedProcessRunnerFactory, StepContext, StepInput,
    SubagentConfig, SubagentTaskSpec, TaskAnchor,
};
use serde_json::json;
use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
};

#[test]
fn explicit_empty_write_scope_requests_read_only_child_workspace() {
    let catalog = SubagentRoleCatalog::builtin();
    let fallback_provider: Arc<dyn ModelProvider> = Arc::new(FakeModelProvider::new(Vec::new()));
    let fallback_model = ModelName::new("fallback").expect("valid model");
    let generation = GenerationConfig::default();

    let task = SubagentTaskSpec::new("Read the assigned files.", 1)
        .expect("valid task")
        .with_write_scope(Vec::<&str>::new())
        .expect("empty write scope is valid");
    let scope = ChildWorkspaceScope::from_task(&task);
    let ro = resolve_subagent_runtime(
        &catalog,
        &SubagentResolutionInput {
            task: &task,
            parent_allowed_tools: &[],
            workspace_scope: &scope,
            parent_workspace_writable: true,
            depth: 1,
            max_depth: 1,
            max_model_turns_cap: 80,
            parent_primary_model: None,
            fallback_provider: Arc::clone(&fallback_provider),
            fallback_model: &fallback_model,
            parent_generation_config: &generation,
        },
    )
    .expect("ro resolves");
    assert!(!ro.workspace_writable);

    let writable_task = SubagentTaskSpec::new("Edit assigned files.", 1)
        .expect("valid task")
        .with_write_scope(["src"])
        .expect("write scope is valid");
    let writable_scope = ChildWorkspaceScope::from_task(&writable_task);
    let rw = resolve_subagent_runtime(
        &catalog,
        &SubagentResolutionInput {
            task: &writable_task,
            parent_allowed_tools: &[],
            workspace_scope: &writable_scope,
            parent_workspace_writable: true,
            depth: 1,
            max_depth: 1,
            max_model_turns_cap: 80,
            parent_primary_model: None,
            fallback_provider,
            fallback_model: &fallback_model,
            parent_generation_config: &generation,
        },
    )
    .expect("rw resolves");
    assert!(rw.workspace_writable);
}

fn process_backend() -> Arc<dyn ProcessBackend> {
    let runner: Arc<dyn RuntimeProcessRunner> = Arc::new(TokioProcessRunner::new());
    let permissioned_factory = Arc::new(StaticPermissionedProcessRunnerFactory::new(Arc::clone(
        &runner,
    )));
    let session = ProcessSession::from_parts(
        AcceptedLocalWorkspaceProcessAdmission::accept_local_workspace(),
        runner,
        permissioned_factory,
    );
    Arc::new(LocalProcessBackend::from_session(session))
}

fn build_factory(
    root: &std::path::Path,
    provider: Arc<dyn ModelProvider>,
    policy: CodingRuntimePolicy,
) -> CodingChildRuntimeFactory {
    let profile_builder = CodingAgentProfileBuilder::new(root)
        .patch_tool()
        .retry_policy(ModelRetryPolicy::disabled())
        .accepted_process_session({
            let runner: Arc<dyn RuntimeProcessRunner> = Arc::new(TokioProcessRunner::new());
            let permissioned_factory = Arc::new(StaticPermissionedProcessRunnerFactory::new(
                Arc::clone(&runner),
            ));
            ProcessSession::from_parts(
                AcceptedLocalWorkspaceProcessAdmission::accept_local_workspace(),
                runner,
                permissioned_factory,
            )
        });
    CodingChildRuntimeFactory::from_composition(CodingRuntimeComposition {
        profile_builder,
        provider,
        model: ModelName::new("child-primary").expect("child model should be valid"),
        process_backend: process_backend(),
        parent_process_permissions: None,
        parent_workspace_writable: true,
        subagent_config: SubagentConfig::new(1, 1)
            .expect("subagent config should be valid")
            .with_model_turn_bounds(1, 80)
            .expect("coding subagent bounds should be valid"),
        role_catalog: SubagentRoleCatalog::builtin(),
        automatic_compaction: CompactionConfig::disabled(),
        max_parallel_tool_calls: None,
        policy,
        worktree_manager: WorktreeIsolationManager::new(),
    })
}

fn child_input(session: &str, allowed_tools: Vec<ToolName>) -> ChildRuntimeInput {
    let task = SubagentTaskSpec::new("Exercise the child runtime.", 4)
        .expect("child task should be valid");
    ChildRuntimeInput {
        parent_session_id: SessionId::new("parent-session").expect("parent session valid"),
        agent_id: None,
        session_id: SessionId::new(session).expect("child session should be valid"),
        session_store: None,
        loaded_session: None,
        existing_worktree: None,
        task_anchor: TaskAnchor::new("Exercise the child runtime.")
            .expect("child task anchor should be valid"),
        task,
        allowed_tools,
        workspace_scope: ChildWorkspaceScope::workspace_root(),
        depth: 0,
        generation_config: GenerationConfig::default(),
        parent_primary_model: None,
        parent_generation_config: GenerationConfig::default(),
        activity_hub: None,
    }
}

async fn run_text_step(runtime: &Runtime, text: &str) -> Vec<RuntimeJournalEvent> {
    runtime
        .step(
            StepInput::user_text(text).expect("step input should be valid"),
            StepContext::default(),
        )
        .expect("child step should start")
        .collect()
        .await
}

fn completed_text(text: &str) -> ModelEvent {
    ModelEvent::Completed {
        response: ModelResponse::new(vec![ModelOutput::text(text)], FinishReason::Stop, None),
    }
}

fn compaction_candidate() -> ModelEvent {
    completed_text(
        r#"{
  "confirmed_decisions": [],
  "rejected_approaches": [],
  "constraints_preferences_boundaries": [],
  "corrected_misunderstandings": [],
  "durable_conclusions": [{"id": "child-c1", "text": "The child runtime compacted its history.", "refs": ["h0"]}],
  "open_questions": [],
  "current_progress_and_next_steps": [],
  "exact_details": [],
  "handoffs": []
}"#,
    )
}

fn permission_call_event() -> ModelEvent {
    let call = ModelToolCall::new(
        ModelToolCallId::new("child-permission-call").expect("call id should be valid"),
        ToolName::new("request_permissions").expect("tool name should be valid"),
        ToolArguments::try_from(json!({
            "reason": "Run the exact child command.",
            "requested": {"network": true},
            "for_action": {
                "command": "printf child-permission",
                "cwd": "."
            }
        }))
        .expect("permission arguments should be valid"),
    );
    ModelEvent::Completed {
        response: ModelResponse::new(
            vec![ModelOutput::tool_call(call)],
            FinishReason::ToolCalls,
            None,
        ),
    }
}

fn approval_event() -> ModelEvent {
    completed_text(
        r#"{"schema_version":"permission_review.v1","decision":"approve","risk":"low","user_authorization":"high","rationale":"The child task authorizes this exact command."}"#,
    )
}

fn permission_tool_name() -> ToolName {
    ToolName::new("request_permissions").expect("permission tool name should be valid")
}

fn resolved_success(events: &[RuntimeJournalEvent]) -> bool {
    events.iter().any(|event| {
        matches!(
            &event.payload,
            RuntimeJournalPayload::ToolCallResolved { result }
                if result.status() == ToolCallResultStatus::Succeeded
        )
    })
}

#[derive(Clone)]
struct CountingAdmission {
    calls: Arc<AtomicUsize>,
    decision: PermissionAdmissionDecision,
}

impl CountingAdmission {
    fn approving() -> Self {
        Self {
            calls: Arc::new(AtomicUsize::new(0)),
            decision: PermissionAdmissionDecision::approved("host approved"),
        }
    }

    fn calls(&self) -> usize {
        self.calls.load(Ordering::SeqCst)
    }
}

impl PermissionAdmissionSource for CountingAdmission {
    fn review<'a>(
        &'a self,
        _request: merry_runtime::PermissionRequest,
        _context: PermissionAdmissionContext,
    ) -> PermissionAdmissionFuture<'a> {
        Box::pin(async move {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Ok(self.decision.clone())
        })
    }
}

#[tokio::test(flavor = "current_thread")]
async fn child_runtime_uses_composed_context_compaction_provider() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let primary = Arc::new(FakeModelProvider::new(vec![
        Ok(completed_text("first child turn")),
        Ok(completed_text("second child turn")),
    ]));
    let compactor = Arc::new(FakeModelProvider::new(vec![Ok(compaction_candidate())]));
    let compaction_role = CodingModelRoleConfig::new(
        RuntimeModelRole::ContextCompaction,
        compactor.clone(),
        ModelName::new("child-compactor").expect("compaction model should be valid"),
    )
    .expect("context compaction role should be valid");
    let policy =
        CodingRuntimePolicy::try_new(vec![compaction_role], CodingPermissionPolicy::default())
            .expect("child policy should not contain duplicate roles");
    let factory = build_factory(temp.path(), primary.clone(), policy);
    let runtime = factory
        .build_child(child_input("child-compaction", Vec::new()))
        .expect("child runtime should build");

    run_text_step(&runtime, "old child history").await;
    run_text_step(&runtime, "retained child history").await;
    let policy =
        CitationCompactionPolicy::new(None, None, 1).expect("compaction policy should be valid");
    let outcome = runtime
        .compact_context_once(policy, StepContext::default())
        .await
        .expect("child compaction should complete")
        .expect("child history should be compactable");

    assert!(outcome.covered_history_item_count() > 0);
    assert_eq!(compactor.recorded_requests().len(), 1);
    assert_eq!(
        compactor.recorded_requests()[0].model().as_str(),
        "child-compactor"
    );
    assert_eq!(primary.recorded_requests().len(), 2);
}

#[tokio::test(flavor = "current_thread")]
async fn child_runtime_uses_composed_approval_review_provider() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let primary = Arc::new(FakeModelProvider::new(vec![Ok(permission_call_event())]));
    let approval = Arc::new(FakeModelProvider::new(vec![Ok(approval_event())]));
    let approval_role = CodingModelRoleConfig::new(
        RuntimeModelRole::ApprovalReview,
        approval.clone(),
        ModelName::new("child-approval").expect("approval model should be valid"),
    )
    .expect("approval role should be valid");
    let policy =
        CodingRuntimePolicy::try_new(vec![approval_role], CodingPermissionPolicy::default())
            .expect("child policy should not contain duplicate roles");
    let factory = build_factory(temp.path(), primary.clone(), policy);
    let runtime = factory
        .build_child(child_input(
            "child-approval-provider",
            vec![permission_tool_name()],
        ))
        .expect("child runtime should build");

    run_text_step(&runtime, "Run the exact child command.").await;
    let pending = runtime.pending_tool_calls().await;
    assert_eq!(pending.len(), 1);
    let resolved = runtime
        .execute_tool_call(
            pending[0].id(),
            merry_runtime::ToolExecutionContext::default(),
        )
        .await
        .expect("child permission request should resolve");

    assert!(resolved_success(&resolved));
    assert_eq!(approval.recorded_requests().len(), 1);
    assert_eq!(
        approval.recorded_requests()[0].model().as_str(),
        "child-approval"
    );
    assert_eq!(primary.recorded_requests().len(), 1);
}

#[tokio::test(flavor = "current_thread")]
async fn child_runtime_propagates_host_only_and_fully_trusted_permission_modes() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let host_only = CountingAdmission::approving();
    let primary = Arc::new(FakeModelProvider::new(vec![Ok(permission_call_event())]));
    let policy = CodingRuntimePolicy::try_new(
        Vec::new(),
        CodingPermissionPolicy::host_decision_only(Arc::new(host_only.clone())),
    )
    .expect("child policy should not contain duplicate roles");
    let factory = build_factory(temp.path(), primary.clone(), policy);
    let runtime = factory
        .build_child(child_input("child-host-only", vec![permission_tool_name()]))
        .expect("host-only child runtime should build");
    run_text_step(&runtime, "Run through the host admission source.").await;
    let pending = runtime.pending_tool_calls().await;
    let events = runtime
        .execute_tool_call(
            pending[0].id(),
            merry_runtime::ToolExecutionContext::default(),
        )
        .await
        .expect("host-only permission request should resolve");
    assert!(resolved_success(&events));
    assert_eq!(host_only.calls(), 1);

    let fully_trusted_source = CountingAdmission::approving();
    let trusted_primary = Arc::new(FakeModelProvider::new(vec![Ok(permission_call_event())]));
    let trusted_policy =
        CodingRuntimePolicy::try_new(Vec::new(), CodingPermissionPolicy::fully_trusted())
            .expect("child policy should not contain duplicate roles");
    let trusted_factory = build_factory(temp.path(), trusted_primary, trusted_policy);
    let trusted_runtime = trusted_factory
        .build_child(child_input(
            "child-fully-trusted",
            vec![permission_tool_name()],
        ))
        .expect("fully trusted child runtime should build");
    run_text_step(&trusted_runtime, "Run through fully trusted admission.").await;
    let pending = trusted_runtime.pending_tool_calls().await;
    let events = trusted_runtime
        .execute_tool_call(
            pending[0].id(),
            merry_runtime::ToolExecutionContext::default(),
        )
        .await
        .expect("fully trusted permission request should resolve");
    assert!(resolved_success(&events));
    assert_eq!(fully_trusted_source.calls(), 0);
}

#[tokio::test(flavor = "current_thread")]
async fn child_runtime_routes_configured_role_model_and_live_parent_model_switch() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let primary = Arc::new(FakeModelProvider::new(vec![Ok(completed_text(
        "primary reply",
    ))]));
    let switched_primary = Arc::new(FakeModelProvider::new(vec![Ok(completed_text(
        "switched reply",
    ))]));
    let browser = Arc::new(FakeModelProvider::new(vec![Ok(completed_text(
        "browser reply",
    ))]));
    let mut catalog = SubagentRoleCatalog::builtin();
    catalog
        .apply_role_overlay(
            "browser",
            crate::SubagentRoleOverlay {
                base: Some("review".to_owned()),
                model_override: Some(crate::SubagentModelOverride {
                    provider: browser.clone(),
                    model: ModelName::new("browser-vision-model").expect("browser model valid"),
                    retry_policy: None,
                }),
                ..Default::default()
            },
        )
        .expect("browser role overlay valid");
    let policy = CodingRuntimePolicy::try_new(Vec::new(), CodingPermissionPolicy::default())
        .expect("policy valid");
    let mut factory = build_factory(temp.path(), primary.clone(), policy);
    factory.composition.role_catalog = catalog;

    let mut browser_input = child_input(
        "child-browser-only",
        vec![
            ToolName::new("browser_tab").expect("valid name"),
            ToolName::new("browser_network").expect("valid name"),
            ToolName::new("read_file").expect("valid name"),
        ],
    );
    browser_input.task = browser_input
        .task
        .with_role(Some("browser".to_owned()))
        .expect("valid role");
    let browser_child = factory
        .build_child(browser_input)
        .expect("browser child runtime builds");
    run_text_step(&browser_child, "Inspect preview page.").await;
    assert_eq!(browser.recorded_requests().len(), 1);
    assert_eq!(
        browser.recorded_requests()[0].model().as_str(),
        "browser-vision-model"
    );
    assert_eq!(primary.recorded_requests().len(), 0);

    // Live switched parent model takes effect immediately for roles without explicit model override.
    let mut switched_input = child_input(
        "child-switched",
        vec![ToolName::new("read_file").expect("valid name")],
    );
    switched_input.parent_primary_model = Some(merry_runtime::ParentPrimaryModel {
        provider: switched_primary.clone(),
        model: ModelName::new("live-switched-model").expect("valid"),
        retry_policy: ModelRetryPolicy::disabled(),
    });
    let switched_child = factory
        .build_child(switched_input)
        .expect("switched child runtime builds");
    run_text_step(&switched_child, "Use live parent model.").await;
    assert_eq!(switched_primary.recorded_requests().len(), 1);
    assert_eq!(
        switched_primary.recorded_requests()[0].model().as_str(),
        "live-switched-model"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn writable_child_with_explicit_write_scope_keeps_accepted_process_session() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let run_call = ModelToolCall::new(
        ModelToolCallId::new("child-proc-call").expect("call id valid"),
        ToolName::new("run_process").expect("tool name valid"),
        ToolArguments::try_from(json!({
            "command": "mkdir -p out && printf subagent-ran > out/ok.txt"
        }))
        .expect("arguments valid"),
    );
    let primary = Arc::new(FakeModelProvider::new(vec![Ok(ModelEvent::Completed {
        response: ModelResponse::new(
            vec![ModelOutput::tool_call(run_call)],
            FinishReason::ToolCalls,
            None,
        ),
    })]));
    let policy = CodingRuntimePolicy::try_new(Vec::new(), CodingPermissionPolicy::default())
        .expect("policy valid");
    let runner: Arc<dyn RuntimeProcessRunner> =
        Arc::new(TokioProcessRunner::new_at_workspace_root(temp.path()));
    let permissioned_factory = Arc::new(StaticPermissionedProcessRunnerFactory::new(Arc::clone(
        &runner,
    )));
    let session = ProcessSession::from_parts(
        AcceptedLocalWorkspaceProcessAdmission::accept_local_workspace(),
        runner,
        permissioned_factory,
    );
    let profile_builder = CodingAgentProfileBuilder::new(temp.path())
        .patch_tool()
        .retry_policy(ModelRetryPolicy::disabled())
        .accepted_process_session(session.clone());
    let factory = CodingChildRuntimeFactory::from_composition(CodingRuntimeComposition {
        profile_builder,
        provider: primary,
        model: ModelName::new("child-primary").expect("child model should be valid"),
        process_backend: Arc::new(LocalProcessBackend::from_session(session)),
        parent_process_permissions: None,
        parent_workspace_writable: true,
        subagent_config: SubagentConfig::new(1, 1)
            .expect("subagent config should be valid")
            .with_model_turn_bounds(1, 80)
            .expect("coding subagent bounds should be valid"),
        role_catalog: SubagentRoleCatalog::builtin(),
        automatic_compaction: CompactionConfig::disabled(),
        max_parallel_tool_calls: None,
        policy,
        worktree_manager: WorktreeIsolationManager::new(),
    });

    let task = SubagentTaskSpec::new("Run verification command.", 4)
        .expect("task valid")
        .with_write_scope(["src"])
        .expect("write scope valid");
    let scope = ChildWorkspaceScope::from_task(&task);
    let runtime = factory
        .build_child(ChildRuntimeInput {
            parent_session_id: SessionId::new("parent-writable-proc").expect("parent valid"),
            agent_id: None,
            session_id: SessionId::new("child-writable-proc").expect("session id valid"),
            session_store: None,
            loaded_session: None,
            existing_worktree: None,
            task_anchor: TaskAnchor::new("Run verification command.").expect("anchor valid"),
            task,
            allowed_tools: vec![ToolName::new("run_process").expect("tool name valid")],
            workspace_scope: scope,
            depth: 1,
            generation_config: GenerationConfig::default(),
            parent_primary_model: None,
            parent_generation_config: GenerationConfig::default(),
            activity_hub: None,
        })
        .expect("writable child should build");

    run_text_step(&runtime, "Run command.").await;
    let pending = runtime.pending_tool_calls().await;
    assert_eq!(pending.len(), 1);
    let events = runtime
        .execute_tool_call(
            pending[0].id(),
            merry_runtime::ToolExecutionContext::default(),
        )
        .await
        .expect("child run_process should resolve");
    assert!(
        resolved_success(&events),
        "writable child with explicit write_scope must not be downgraded to read_only_process_runner: {events:?}"
    );
    assert_eq!(
        std::fs::read_to_string(temp.path().join("out/ok.txt")).expect("child wrote file"),
        "subagent-ran"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn child_uses_dedicated_subagent_prompt_handoff_and_shared_role_cache_key() {
    let temp = tempfile::tempdir().expect("tempdir should exist");
    std::fs::create_dir_all(temp.path().join("src")).expect("src dir");
    std::fs::write(
        temp.path().join("src/lib.rs"),
        "pub fn alpha() {}\npub fn beta() {}\npub fn gamma() {}\n",
    )
    .expect("seed file");

    let provider = Arc::new(FakeModelProvider::new(vec![Ok(completed_text("done"))]));
    let factory = build_factory(
        temp.path(),
        provider.clone(),
        CodingRuntimePolicy::try_new(Vec::new(), CodingPermissionPolicy::default())
            .expect("policy valid"),
    );

    let build_explore = |session: &str| {
        let task = SubagentTaskSpec::new("Find beta definition.", 8)
            .expect("valid task")
            .with_role(Some("explore".to_owned()))
            .expect("valid role")
            .with_read_scope(["."])
            .expect("read scope valid")
            .with_write_scope(Vec::<&str>::new())
            .expect("read-only scope")
            .with_context_files(["src/lib.rs:2-3"])
            .expect("valid context_files")
            .with_expected_output(Some("Return path:line only.".to_owned()));
        let scope = ChildWorkspaceScope::from_task(&task);
        factory
            .build_child(ChildRuntimeInput {
                parent_session_id: SessionId::new("parent-cache-session")
                    .expect("parent session valid"),
                agent_id: None,
                session_id: SessionId::new(session).expect("child session valid"),
                session_store: None,
                loaded_session: None,
                existing_worktree: None,
                task_anchor: TaskAnchor::new("Find beta definition.").expect("anchor valid"),
                task,
                allowed_tools: vec![ToolName::new("read_file").expect("tool valid")],
                workspace_scope: scope,
                depth: 1,
                generation_config: GenerationConfig::default(),
                parent_primary_model: None,
                parent_generation_config: GenerationConfig::default(),
                activity_hub: None,
            })
            .expect("explore child should build")
    };

    let child_one = build_explore("child-explore-1");
    let child_two = build_explore("child-explore-2");
    run_text_step(&child_one, "Find beta definition.").await;
    run_text_step(&child_two, "Find beta definition.").await;

    let requests = provider.recorded_requests();
    assert_eq!(requests.len(), 2);

    let first_system_texts = requests[0]
        .input()
        .iter()
        .filter_map(|item| match item {
            merry_llm::ModelInputItem::Message(msg)
                if msg.role() == merry_llm::ModelMessageRole::System =>
            {
                Some(msg.content().as_text())
            }
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("\n\n");

    assert!(first_system_texts.contains("<merry_subagent_instructions>"));
    assert!(!first_system_texts.contains("<merry_runtime_instructions>"));
    assert!(first_system_texts.contains("<merry_coding_subagent_policy>"));
    assert!(!first_system_texts.contains("Subagent orchestration discipline"));
    assert!(first_system_texts.contains("<merry_subagent_role>"));
    assert!(first_system_texts.contains("read-only codebase researcher"));
    assert!(
        !first_system_texts.contains("<merry_task_anchor>"),
        "child must not duplicate task in task_anchor"
    );
    assert!(first_system_texts.contains("<merry_subagent_handoff>"));
    assert!(
        first_system_texts
            .contains("<expected_output>\nReturn path:line only.\n</expected_output>")
    );
    assert!(first_system_texts.contains("<context_file path=\"src/lib.rs:2-3\">"));
    assert!(first_system_texts.contains("2: pub fn beta() {}"));

    // Stable prefixes of two sibling explore children must be byte-identical.
    let prefix_len = requests[0].stable_prefix_item_count();
    assert_eq!(prefix_len, requests[1].stable_prefix_item_count());
    assert_eq!(
        &requests[0].input()[..prefix_len],
        &requests[1].input()[..prefix_len],
        "sibling children of the same role must share a byte-identical stable prefix"
    );
}

#[tokio::test(flavor = "current_thread")]
async fn read_only_child_retains_connected_session_permissions_and_static_path_rules() {
    use merry_runtime::{
        CapabilityPolicy, ChildWorkspacePolicyOverride, LivePolicyBaseline, LivePolicyHandle,
        NetworkAccessPolicy, ProcessSessionPermissions, WorkspaceAccessPolicy,
    };

    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path().join("workspace");
    let allowed_ext = temp.path().join("allowed-ext");
    std::fs::create_dir_all(&workspace).expect("workspace");
    std::fs::create_dir_all(&allowed_ext).expect("allowed external dir");
    std::fs::write(allowed_ext.join("doc.txt"), "hello external\n").expect("write external doc");

    let access = WorkspaceAccessPolicy::new(&workspace, true).with_static_rules([
        merry_runtime::PathAccessRule::new(
            &allowed_ext,
            merry_runtime::PathAccess::ReadOnly,
            merry_runtime::PathAccessRuleSource::TrustedGlobalConfig,
        ),
    ]);
    let live_handle = LivePolicyHandle::new(LivePolicyBaseline::new(
        true,
        Some(access),
        Vec::new(),
        NetworkAccessPolicy::new(CapabilityPolicy::Ask, true),
    ));
    let child_perms = ProcessSessionPermissions::new();
    let child_handle = live_handle.for_child(
        Some(child_perms.view()),
        Some(ChildWorkspacePolicyOverride::new(false)),
    );
    let runner: Arc<dyn RuntimeProcessRunner> = Arc::new(TokioProcessRunner::new());
    let permissioned_factory = Arc::new(StaticPermissionedProcessRunnerFactory::new(Arc::clone(
        &runner,
    )));
    let child_session = ProcessSession::from_parts(
        AcceptedLocalWorkspaceProcessAdmission::accept_local_workspace(),
        runner,
        permissioned_factory,
    )
    .with_session_permissions(child_perms.clone())
    .with_live_policy(child_handle.clone());

    // Build a CodingAgentProfile using read_only_process_session (no run_process tool).
    let profile = CodingAgentProfileBuilder::new(&workspace)
        .read_only_process_session(child_session)
        .build()
        .expect("profile should build");
    let read_call_event = ModelEvent::Completed {
        response: ModelResponse::new(
            vec![ModelOutput::ToolCall {
                call: ModelToolCall::new(
                    ModelToolCallId::new("call-ro-ext").expect("call id"),
                    ToolName::new("read_file").expect("tool name"),
                    ToolArguments::try_from(serde_json::json!({
                        "path": allowed_ext.join("doc.txt").display().to_string()
                    }))
                    .expect("args"),
                ),
            }],
            FinishReason::ToolCalls,
            None,
        ),
    };
    let builder = Runtime::builder(SessionId::new("child-ro-connected").expect("session id"))
        .model_provider(
            Arc::new(FakeModelProvider::new(vec![Ok(read_call_event)])),
            ModelName::new("test-model").expect("model"),
        );
    let runtime = profile
        .apply_to(builder)
        .expect("apply profile")
        .build()
        .expect("build runtime");

    // 1. Static path rules from the live handle are still enforced on read_file!
    let _ = run_text_step(&runtime, "read external doc").await;
    let call_id = merry_core::ToolCallId::new("call-ro-ext").expect("id");
    let read_events = runtime
        .execute_tool_call(&call_id, merry_runtime::ToolExecutionContext::default())
        .await
        .expect("execute read_file");
    assert!(
        resolved_success(&read_events),
        "read-only child must retain static path rules from LivePolicyHandle: {read_events:?}"
    );

    // 2. Runtime's ProcessSessionPermissions is connected to child_handle's live network policy:
    //    retaining a domain grant via session permissions immediately permits that host.
    assert_eq!(
        child_handle.evaluate_network_host("docs.rs", &[]),
        merry_runtime::NetworkAccessDecision::RequireReview
    );
    child_perms
        .retain_domain_grants(["docs.rs".to_owned()])
        .expect("retain session domain grant");
    assert_eq!(
        child_handle.evaluate_network_host("docs.rs", &[]),
        merry_runtime::NetworkAccessDecision::Allow
    );
    assert!(
        runtime
            .active_permission_grants()
            .grants()
            .iter()
            .any(|g| matches!(
                g.kind(),
                merry_runtime::SessionPermissionGrantKind::NetworkDomain(d) if d == "docs.rs"
            )),
        "runtime must share the exact same ProcessSessionPermissions as child_handle"
    );
}