merry-coding 0.1.2

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
use crate::{
    CODING_AGENT_POLICY_PROMPT, CodingAgentRunPolicy, CodingFinalReportPolicy, ProjectRules,
    TaskAnchor, coding_agent,
    tests::{bridge_tool, bridge_tool_with_description, process_session},
};
use merry_core::ToolName;
use merry_llm::ModelRetryPolicy;
use merry_process::TokioProcessRunner;
use merry_runtime::{AcceptedLocalWorkspaceProcessAdmission, DurableMemory, MemoryLayout};
use serde_json::json;
use std::sync::Arc;

#[test]
fn workspace_profile_builds_read_tools_and_coding_defaults() {
    let temp = tempfile::tempdir().expect("tempdir should be created");

    let profile = coding_agent(temp.path())
        .build()
        .expect("workspace profile should build");

    let tool_names = profile
        .registered_tools()
        .iter()
        .map(|tool| tool.spec().name().as_str())
        .collect::<Vec<_>>();
    assert!(tool_names.contains(&"read_file"));
    assert!(!tool_names.contains(&"apply_patch"));
    assert!(profile.runtime_profile().progress_commentary());
    assert_eq!(
        profile.runtime_profile().model_retry_policy(),
        Some(ModelRetryPolicy::coding_agent_default())
    );
}

#[test]
fn coding_agent_profile_has_one_canonical_workspace_tool_order() {
    let temp = tempfile::tempdir().expect("tempdir should be created");

    let profile = coding_agent(temp.path())
        .build()
        .expect("coding-agent profile should build");
    let tool_names = profile
        .tool_names()
        .into_iter()
        .map(ToolName::as_str)
        .collect::<Vec<_>>();

    assert_eq!(tool_names, vec!["read_file"]);
}

#[test]
fn coding_agent_profile_owns_process_permission_and_patch_order() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let runner: Arc<dyn merry_runtime::ProcessRunner> = Arc::new(TokioProcessRunner::new());
    let profile = coding_agent(temp.path())
        .patch_tool()
        .accepted_process_session(process_session(
            AcceptedLocalWorkspaceProcessAdmission::accept_local_workspace(),
            runner,
        ))
        .build()
        .expect("coding-agent profile should build");

    let tool_names = profile
        .tool_names()
        .into_iter()
        .map(ToolName::as_str)
        .collect::<Vec<_>>();
    assert_eq!(
        tool_names,
        vec![
            "run_process",
            "request_permissions",
            "read_background_process",
            "stop_background_process",
            "read_file",
            "apply_patch",
        ]
    );
    let process = &profile.registered_tools()[0];
    assert!(
        process
            .spec()
            .description()
            .contains("omit cwd for the current workspace directory")
    );
    assert!(
        process
            .spec()
            .description()
            .contains("runtime will review before running this exact command")
    );
    assert!(process.spec().description().contains(
        "Paths and host integrations enabled by trusted global configuration are already available"
    ));
    assert!(
        process
            .spec()
            .description()
            .contains("Network is never granted implicitly")
    );
    assert!(
        process
            .spec()
            .description()
            .contains("Set background to true")
    );
    assert!(
        process
            .spec()
            .description()
            .contains("read_background_process")
    );
    let schema = process.spec().input_schema().as_schema().as_value();
    assert_eq!(schema["required"], json!(["command"]));
    assert!(schema["properties"]["permissions"].is_object());
    assert!(
        schema["properties"]["permissions"]["properties"]["host_integrations"]["description"]
            .as_str()
            .expect("host integration description should be text")
            .contains("Integrations enabled by trusted global configuration are already available")
    );
    assert!(
        schema["properties"]["permissions"]["properties"]["network"]["description"]
            .as_str()
            .expect("network description should be text")
            .contains("Network is never granted implicitly")
    );
}

#[test]
fn coding_agent_profile_hash_is_deterministic_and_tracks_stable_material() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let first = coding_agent(temp.path())
        .project_rules(
            ProjectRules::new("AGENTS.md", "Use the project rules.")
                .expect("project rules should be valid"),
        )
        .task_anchor(TaskAnchor::new("First task").expect("task anchor should be valid"))
        .build()
        .expect("coding-agent profile should build");
    let dynamic_change = coding_agent(temp.path())
        .project_rules(
            ProjectRules::new("AGENTS.md", "Use the project rules.")
                .expect("project rules should be valid"),
        )
        .task_anchor(TaskAnchor::new("Different task").expect("task anchor should be valid"))
        .build()
        .expect("coding-agent profile should build");
    let stable_change = coding_agent(temp.path())
        .project_rules(
            ProjectRules::new("AGENTS.md", "Changed project rules.")
                .expect("project rules should be valid"),
        )
        .task_anchor(TaskAnchor::new("First task").expect("task anchor should be valid"))
        .build()
        .expect("coding-agent profile should build");
    let retry_change = coding_agent(temp.path())
        .retry_policy(ModelRetryPolicy::disabled())
        .project_rules(
            ProjectRules::new("AGENTS.md", "Use the project rules.")
                .expect("project rules should be valid"),
        )
        .task_anchor(TaskAnchor::new("First task").expect("task anchor should be valid"))
        .build()
        .expect("coding-agent profile should build");

    assert_eq!(first.profile_hash(), dynamic_change.profile_hash());
    assert_ne!(first.profile_hash(), stable_change.profile_hash());
    assert_ne!(first.profile_hash(), retry_change.profile_hash());
    assert!(first.profile_hash().as_str().starts_with("fnv1a64:"));
}

#[test]
fn coding_agent_profile_hash_includes_advertised_tool_order() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let first = coding_agent(temp.path())
        .register_tools([bridge_tool("alpha"), bridge_tool("beta")])
        .build()
        .expect("coding-agent profile should build");
    let reordered = coding_agent(temp.path())
        .register_tools([bridge_tool("beta"), bridge_tool("alpha")])
        .build()
        .expect("coding-agent profile should build");

    assert_ne!(first.profile_hash(), reordered.profile_hash());
    assert_eq!(
        first
            .tool_names()
            .into_iter()
            .map(ToolName::as_str)
            .collect::<Vec<_>>(),
        vec!["read_file", "alpha", "beta"]
    );
}

#[test]
fn coding_agent_profile_hash_tracks_tool_schema_and_coding_run_policy() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let base = coding_agent(temp.path())
        .register_tool(bridge_tool("catalog_tool"))
        .build()
        .expect("base profile should build");
    let changed_schema = coding_agent(temp.path())
        .register_tool(bridge_tool_with_description(
            "catalog_tool",
            "Changed description",
        ))
        .build()
        .expect("changed schema profile should build");
    let changed_policy = coding_agent(temp.path())
        .run_policy(
            CodingAgentRunPolicy::new(16, CodingFinalReportPolicy::EvidenceBackedSummary)
                .expect("valid coding run policy"),
        )
        .build()
        .expect("changed policy profile should build");

    assert_ne!(base.profile_hash(), changed_schema.profile_hash());
    assert_ne!(base.profile_hash(), changed_policy.profile_hash());
    assert_eq!(base.run_policy().max_model_turns(), usize::MAX);
    assert_eq!(
        changed_policy
            .loop_config()
            .expect("loop config")
            .max_model_turns(),
        16
    );
    assert_eq!(
        changed_policy.run_policy().final_report().as_str(),
        "evidence_backed_summary"
    );
}

#[test]
fn coding_agent_profile_owns_the_coding_prompt_and_hashes_its_exact_text() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let profile = coding_agent(temp.path())
        .build()
        .expect("coding-agent profile should build");
    let runtime_profile = profile.runtime_profile();
    let prompt = runtime_profile.prompt_profile();

    assert_eq!(prompt.stable_blocks().len(), 1);
    assert_eq!(prompt.stable_blocks()[0].tag(), "merry_coding_policy");
    assert_eq!(prompt.stable_blocks()[0].text(), CODING_AGENT_POLICY_PROMPT);
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("evidence-backed summary")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("same `run_process` call under `permissions`")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("discovered after a failed sandboxed attempt")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("already available to sandboxed commands")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("starts with no network access")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("preferring the installed modern search tools")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("Do not delete coverage, weaken an assertion")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("Commit, push, merge, or publish only when the user asks")
    );
    assert!(
        prompt.stable_blocks()[0].text().contains(
            "Subagent orchestration discipline (`spawn_subagents`, `send_subagent_message`"
        )
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("Prefer editing an existing file")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("Set `background` to true")
    );
    assert!(
        prompt.stable_blocks()[0]
            .text()
            .contains("`read_background_process`")
    );
}

#[test]
fn coding_agent_profile_appends_memory_instructions_after_policy() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let workspace = temp.path().join("workspace");
    std::fs::create_dir(&workspace).expect("workspace dir should be created");
    let memory = DurableMemory::open(
        MemoryLayout::at(&temp.path().join("memory"), &workspace)
            .expect("memory layout should resolve"),
    )
    .expect("memory store should open");
    let without = coding_agent(&workspace)
        .build()
        .expect("profile without memory should build");
    let profile = coding_agent(&workspace)
        .durable_memory(memory)
        .build()
        .expect("profile with memory should build");
    let runtime_profile = profile.runtime_profile();
    let blocks = runtime_profile.prompt_profile().stable_blocks();

    assert_eq!(blocks.len(), 2);
    assert_eq!(blocks[0].tag(), "merry_coding_policy");
    assert_eq!(blocks[0].text(), CODING_AGENT_POLICY_PROMPT);
    assert_eq!(blocks[1].tag(), "merry_memory");
    assert!(blocks[1].text().contains("`read_file`"));
    assert!(blocks[1].text().contains("`apply_patch`"));
    assert!(blocks[1].text().contains("never read or edit MEMORY.md"));
    assert_ne!(without.profile_hash(), profile.profile_hash());
}

#[test]
fn coding_agent_profile_hash_distinguishes_patch_scope_and_process_admission() {
    let temp = tempfile::tempdir().expect("tempdir should be created");
    let runner: Arc<dyn merry_runtime::ProcessRunner> = Arc::new(TokioProcessRunner::new());
    let unrestricted = coding_agent(temp.path())
        .patch_tool()
        .build()
        .expect("unrestricted profile should build");
    let read_only = coding_agent(temp.path())
        .patch_tool()
        .read_only_patch_scope()
        .build()
        .expect("read-only profile should build");
    let isolated = coding_agent(temp.path())
        .accepted_process_session(process_session(
            AcceptedLocalWorkspaceProcessAdmission::accept_local_workspace(),
            Arc::clone(&runner),
        ))
        .build()
        .expect("isolated profile should build");
    let host = coding_agent(temp.path())
        .accepted_process_session(process_session(
            AcceptedLocalWorkspaceProcessAdmission::accept_host(),
            runner,
        ))
        .build()
        .expect("host profile should build");

    assert_ne!(unrestricted.profile_hash(), read_only.profile_hash());
    assert_ne!(isolated.profile_hash(), host.profile_hash());
}

#[test]
fn workspace_profile_can_enable_patch_tool() {
    let temp = tempfile::tempdir().expect("tempdir should be created");

    let profile = coding_agent(temp.path())
        .patch_tool()
        .read_only_patch_scope()
        .build()
        .expect("workspace profile should build");

    assert!(
        profile
            .registered_tools()
            .iter()
            .any(|tool| tool.spec().name().as_str() == "apply_patch")
    );
}

#[test]
fn workspace_profile_web_tools_registers_web_and_browser_tools_in_order() {
    let temp = tempfile::tempdir().expect("tempdir should be created");

    let bare = coding_agent(temp.path())
        .build()
        .expect("bare profile should build");
    let with_web = coding_agent(temp.path())
        .web_tools()
        .build()
        .expect("web profile should build");

    let names = with_web
        .tool_names()
        .into_iter()
        .map(ToolName::as_str)
        .collect::<Vec<_>>();
    assert_eq!(
        names,
        vec!["read_file", "web_fetch", "browser_tab", "browser_network"]
    );
    assert_ne!(bare.profile_hash(), with_web.profile_hash());
    assert!(CODING_AGENT_POLICY_PROMPT.contains("browser_tab"));
    assert!(CODING_AGENT_POLICY_PROMPT.contains("web_fetch"));
    assert!(CODING_AGENT_POLICY_PROMPT.contains("data:text/html"));
    assert!(CODING_AGENT_POLICY_PROMPT.contains("A failed open does not start a session"));
    assert!(CODING_AGENT_POLICY_PROMPT.contains("policy refusal"));
}