merry-runtime 0.1.4

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
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
//! Runtime-owned process command tool registration helper.
//!
//! This module exposes a small provider-neutral tool that turns a model
//! supplied shell command into [`crate::ProcessActionIntent`] proposal
//! evidence. It never spawns a process itself; execution remains owned by the
//! runtime process policy and injected [`crate::ProcessRunner`] lanes.

use crate::{
    ActionProposal, ActionProposalEvidence, MAX_PROCESS_ARG_BYTES, MAX_PROCESS_WAIT_TIMEOUT_SECS,
    ProcessActionIntent, ProcessEnvPolicy, RegisteredTool, ToolActionKind, ToolActionPreflight,
    ToolActionProposalFuture, ToolExecutionContext, ToolExecutionError, ToolExecutionOutcome,
    ToolExecutor, ToolExecutorFuture,
    permission::{
        RequestedCapabilitiesInput, permission_reason_schema_for_schemars,
        process_cwd_schema_for_schemars, requested_capabilities_schema_for_schemars,
    },
    process::shell_command_argv,
    tool_input::deserialize_non_empty_process_command,
};
use merry_core::{CoreError, ErrorInfo, PendingToolCall, ToolName};
use merry_tools_macros::tool;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
use thiserror::Error;

const DEFAULT_PROCESS_TOOL_STDOUT_LIMIT_BYTES: usize = 64 * 1024;
const DEFAULT_PROCESS_TOOL_STDERR_LIMIT_BYTES: usize = 64 * 1024;

#[tool(
    crate = "crate",
    name = "run_process",
    description = "Run a validated shell command through the runtime process policy."
)]
#[derive(Debug, JsonSchema)]
struct ProcessCommandInput {
    #[schemars(
        description = "Shell command to execute. Newline and tab are allowed; other control characters are rejected. JSON strings must escape embedded control characters.",
        length(min = 1, max = MAX_PROCESS_ARG_BYTES)
    )]
    command: String,
    #[schemars(
        schema_with = "process_cwd_schema_for_schemars",
        description = "Optional workspace-relative working directory. Omit it, use \".\", or use null for the current workspace directory; an empty string is accepted as the same root default for compatibility."
    )]
    #[serde(default)]
    cwd: String,
    #[schemars(
        schema_with = "permission_reason_schema_for_schemars",
        description = "Optional short explanation of why the current task needs the requested capability. Use it only together with permissions."
    )]
    #[serde(default)]
    reason: Option<String>,
    #[schemars(schema_with = "requested_capabilities_schema_for_schemars")]
    #[serde(default)]
    permissions: Option<RequestedCapabilitiesInput>,
    #[schemars(
        description = "When true, return once the command is still running, include its id, and keep it until stop_background_process or this session ends. Use it for a GUI or a server. The default waits until the command exits or hits timeout_secs. Do not background the command in the shell instead."
    )]
    #[serde(default)]
    background: bool,
    #[schemars(
        description = "Optional foreground wait timeout in seconds (1..=3600, default 120). Set this higher for intentionally slow finite commands (such as full builds, test suites, or large downloads). If the command is still running when this timeout elapses, it is NOT killed; Merry retains it in the background and returns its id and partial output so you can observe progress with read_background_process or terminate it with stop_background_process. Ignored when background is true.",
        range(min = 1, max = MAX_PROCESS_WAIT_TIMEOUT_SECS)
    )]
    #[serde(default)]
    timeout_secs: Option<u64>,
    #[schemars(
        description = "Optional short human-readable display name for a backgrounded process (recommended when background is true, e.g. \"web api server\" or \"dev watcher\"). If omitted and a foreground command hands off on timeout, Merry derives a short fallback label."
    )]
    #[serde(default)]
    display_name: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct ProcessCommandInputWire {
    #[serde(deserialize_with = "deserialize_non_empty_process_command")]
    command: String,
    #[serde(default)]
    cwd: Option<String>,
    #[serde(default)]
    reason: Option<String>,
    #[serde(default)]
    permissions: Option<RequestedCapabilitiesInput>,
    #[serde(default)]
    background: bool,
    #[serde(default)]
    timeout_secs: Option<u64>,
    #[serde(default)]
    display_name: Option<String>,
}

impl<'de> Deserialize<'de> for ProcessCommandInput {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let input = ProcessCommandInputWire::deserialize(deserializer)?;
        Ok(Self {
            command: input.command,
            cwd: input.cwd.unwrap_or_default(),
            reason: input.reason,
            permissions: input.permissions,
            background: input.background,
            timeout_secs: input.timeout_secs,
            display_name: input.display_name,
        })
    }
}

/// Errors raised while constructing the runtime-owned process command tool.
#[derive(Debug, Error)]
pub enum ProcessCommandToolError {
    /// A Merry core protocol value rejected the tool definition.
    #[error(transparent)]
    Core {
        /// Source core validation error.
        #[from]
        source: CoreError,
    },
}

/// Creates a registered process command tool for runtime agent loops.
///
/// The provider-visible tool accepts a JSON object with a shell `command`,
/// nullable workspace-relative `cwd`, and optional capabilities to request
/// before execution. The returned registered tool is a `CommandExec` action
/// with proposal evidence enabled, so admitted execution goes through runtime
/// process policy and an injected [`crate::ProcessRunner`].
pub fn process_command_tool(
    name: ToolName,
    description: &str,
) -> Result<RegisteredTool, ProcessCommandToolError> {
    let spec =
        ProcessCommandInput::tool_spec_with(name.as_str(), description).map_err(|error| {
            ProcessCommandToolError::Core {
                source: error.into(),
            }
        })?;
    Ok(RegisteredTool::new(
        spec,
        Arc::new(ProcessCommandToolExecutor),
        ToolActionKind::CommandExec,
    )
    .with_action_proposal()
    .with_parallel_safe_execution())
}

pub(crate) fn process_call_requests_permission(call: &PendingToolCall) -> bool {
    let arguments = call.arguments().as_object();
    arguments
        .get("permissions")
        .is_some_and(|permissions| !permissions.is_null())
        || arguments
            .get("reason")
            .is_some_and(|reason| !reason.is_null())
}

#[derive(Debug)]
struct ProcessCommandToolExecutor;

impl ToolExecutor for ProcessCommandToolExecutor {
    fn propose<'a>(
        &'a self,
        call: PendingToolCall,
        _context: ToolExecutionContext,
    ) -> ToolActionProposalFuture<'a> {
        Box::pin(async move {
            let intent = match process_intent_from_call(&call) {
                Ok(intent) => intent,
                Err(error) => {
                    tracing::debug!(
                        tool_call_id = call.id().as_str(),
                        tool_name = call.name().as_str(),
                        diagnostic_code = PROCESS_COMMAND_INVALID_ARGUMENTS_CODE,
                        "process command tool proposal rejected invalid arguments"
                    );
                    return Ok(ToolActionPreflight::Outcome(invalid_arguments_outcome(
                        call.name().as_str(),
                        error,
                    )));
                }
            };
            let cwd_label = if intent.cwd().is_some() {
                "workspace-relative process"
            } else {
                "workspace-root process"
            };
            let proposal = ActionProposal::new(
                &call,
                ToolActionKind::CommandExec,
                "process command",
                cwd_label,
                "Run the validated shell command through the platform process wrapper using an empty environment".to_owned(),
                ActionProposalEvidence::ProcessAction(intent),
            )
            .map_err(|error| ToolExecutionError::infrastructure(error.to_string()))?;

            Ok(ToolActionPreflight::Proposal(proposal))
        })
    }

    fn execute<'a>(
        &'a self,
        call: PendingToolCall,
        _context: ToolExecutionContext,
    ) -> ToolExecutorFuture<'a> {
        Box::pin(async move {
            if let Err(error) = process_intent_from_call(&call) {
                return Ok(invalid_arguments_outcome(call.name().as_str(), error));
            }
            Err(ToolExecutionError::infrastructure(
                "process command tool must be executed through runtime process policy",
            ))
        })
    }
}

fn process_intent_from_call(
    call: &PendingToolCall,
) -> Result<ProcessActionIntent, InvalidProcessCommandArguments> {
    let input = call
        .arguments()
        .deserialize_as::<ProcessCommandInput>()
        .map_err(|error| InvalidProcessCommandArguments::new(error.to_string()))?;
    let _ = (&input.reason, &input.permissions);
    let argv = shell_command_argv(&input.command);
    let cwd = (!input.cwd.is_empty()).then_some(input.cwd);

    ProcessActionIntent::new(
        argv,
        cwd,
        ProcessEnvPolicy::empty(),
        None,
        DEFAULT_PROCESS_TOOL_STDOUT_LIMIT_BYTES,
        DEFAULT_PROCESS_TOOL_STDERR_LIMIT_BYTES,
    )
    .and_then(|intent| {
        intent
            .with_background(input.background)
            .with_display_name(input.display_name)
            .with_timeout_secs(input.timeout_secs)
    })
    .map_err(|error| InvalidProcessCommandArguments::new(error.to_string()))
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct InvalidProcessCommandArguments {
    message: String,
}

impl InvalidProcessCommandArguments {
    fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }

    fn message(&self) -> &str {
        &self.message
    }
}

const PROCESS_COMMAND_INVALID_ARGUMENTS_CODE: &str = "process_command_invalid_arguments";

fn invalid_arguments_outcome(
    tool_name: &str,
    error: InvalidProcessCommandArguments,
) -> ToolExecutionOutcome {
    let payload = json!({
        "ok": false,
        "tool": tool_name,
        "error": {
            "code": PROCESS_COMMAND_INVALID_ARGUMENTS_CODE,
            "message": error.message(),
        },
        "recovery": {
            "command_contract": "Provide command as one non-empty shell command string. Newline and tab are allowed; other control characters are rejected. Escape control characters for the surrounding JSON string.",
            "cwd_contract": "cwd, when provided, must be non-empty, workspace-relative, and must not contain control characters. For the workspace root, omit cwd or use \".\".",
        },
        "guidance": {
            "kind": "invalid_process_arguments",
            "message": "Fix the process tool arguments before retrying. Use command as one JSON string, and set cwd to null or a workspace-relative directory such as \".\".",
        }
    });
    ToolExecutionOutcome::failed_json(
        payload.to_string(),
        ErrorInfo::new(PROCESS_COMMAND_INVALID_ARGUMENTS_CODE, error.message())
            .expect("static process command diagnostic code is valid"),
    )
}

#[cfg(test)]
mod tests {
    use super::{
        PROCESS_COMMAND_INVALID_ARGUMENTS_CODE, ProcessCommandToolExecutor,
        process_call_requests_permission, process_intent_from_call,
    };
    use crate::{
        ActionProposalEvidence, ToolActionPreflight, ToolExecutionContext, ToolExecutor,
        process::shell_command_argv,
    };
    use merry_core::{PendingToolCall, ToolCallArguments, ToolCallId, ToolName};
    use serde_json::{Value, json};

    fn pending_call(arguments: serde_json::Value) -> PendingToolCall {
        PendingToolCall::new(
            ToolCallId::new("call-process").expect("valid tool call id"),
            ToolName::new("run_process").expect("valid tool name"),
            ToolCallArguments::try_from(arguments).expect("valid tool call arguments"),
        )
    }

    #[test]
    fn process_intent_from_call_parses_command_and_cwd() {
        let call = pending_call(json!({
            "command": "rustc --version",
            "cwd": "crates/merry-runtime"
        }));

        let intent = process_intent_from_call(&call).expect("process intent should parse");

        assert_eq!(intent.argv(), shell_command_argv("rustc --version"));
        assert_eq!(intent.cwd(), Some("crates/merry-runtime"));
        assert!(intent.stdin_text().is_none());
        assert!(!intent.background());
    }

    #[test]
    fn process_intent_from_call_opts_into_background() {
        let call = pending_call(json!({
            "command": "sleep 30",
            "background": true
        }));

        let intent = process_intent_from_call(&call).expect("process intent should parse");

        assert!(intent.background());
    }

    #[test]
    fn process_intent_from_call_parses_and_validates_timeout_secs() {
        let default_call = pending_call(json!({ "command": "cargo check" }));
        let default_intent = process_intent_from_call(&default_call).expect("default parses");
        assert_eq!(default_intent.timeout_secs(), None);
        assert_eq!(
            default_intent.effective_wait_timeout_secs(),
            crate::DEFAULT_PROCESS_WAIT_TIMEOUT_SECS
        );

        let custom_call = pending_call(json!({
            "command": "cargo test --workspace",
            "timeout_secs": 300
        }));
        let custom_intent = process_intent_from_call(&custom_call).expect("custom timeout parses");
        assert_eq!(custom_intent.timeout_secs(), Some(300));
        assert_eq!(custom_intent.effective_wait_timeout_secs(), 300);

        let zero_call = pending_call(json!({
            "command": "cargo check",
            "timeout_secs": 0
        }));
        let zero_err = process_intent_from_call(&zero_call).expect_err("zero timeout must fail");
        assert!(zero_err.message().contains("greater than zero"));

        let huge_call = pending_call(json!({
            "command": "cargo check",
            "timeout_secs": crate::MAX_PROCESS_WAIT_TIMEOUT_SECS + 1
        }));
        let huge_err = process_intent_from_call(&huge_call).expect_err("oversized timeout fails");
        assert!(huge_err.message().contains("maximum allowed wait timeout"));
    }

    #[test]
    fn process_command_tool_schema_is_provider_compatible() {
        let tool = super::process_command_tool(
            ToolName::new("run_process").expect("valid tool name"),
            "Run a process.",
        )
        .expect("process command tool should build");
        let schema = serde_json::to_value(tool.spec().input_schema().as_schema())
            .expect("schema should serialize");
        crate::schema_contract::assert_provider_input_schema_fields_have_descriptions(tool.spec());

        assert!(
            schema["properties"]["cwd"]["description"]
                .as_str()
                .expect("cwd description should be text")
                .contains("Omit it")
        );
        assert!(
            schema["properties"]["cwd"]["description"]
                .as_str()
                .expect("cwd description should be text")
                .contains("current workspace directory")
        );
        let cwd_string_schema = schema["properties"]["cwd"]["anyOf"]
            .as_array()
            .expect("cwd should have nullable branches")
            .iter()
            .find(|branch| branch["type"] == "string")
            .expect("cwd should have a string branch");
        assert_eq!(cwd_string_schema["minLength"], 1);
        assert_eq!(cwd_string_schema["maxLength"], crate::MAX_PROCESS_CWD_BYTES);
        assert!(
            !schema["properties"]["command"]["description"]
                .as_str()
                .unwrap_or_default()
                .is_empty()
        );
        assert_eq!(schema["properties"]["command"]["minLength"], 1);
        assert_eq!(
            schema["properties"]["command"]["maxLength"],
            crate::MAX_PROCESS_ARG_BYTES
        );
        assert_eq!(schema["required"], json!(["command"]));
        assert!(
            schema["properties"]["background"]["description"]
                .as_str()
                .is_some_and(|description| description.contains("still running"))
        );
        assert!(
            schema["properties"]["permissions"]["description"]
                .as_str()
                .is_some_and(|description| description.contains("exact action"))
        );
        assert!(schema["properties"]["reason"]["description"].is_string());
    }

    #[test]
    fn process_intent_from_call_treats_empty_cwd_as_workspace_root() {
        let call = pending_call(json!({
            "command": "ping -c 1 baidu.com",
            "cwd": ""
        }));

        let intent = process_intent_from_call(&call).expect("process intent should parse");

        assert_eq!(intent.argv(), shell_command_argv("ping -c 1 baidu.com"));
        assert_eq!(intent.cwd(), None);
    }

    #[test]
    fn process_intent_from_call_defaults_omitted_cwd_to_workspace_root() {
        let call = pending_call(json!({
            "command": "pwd"
        }));

        let intent = process_intent_from_call(&call).expect("process intent should parse");

        assert_eq!(intent.cwd(), None);
    }

    #[test]
    fn nullable_permission_reason_is_treated_as_omitted() {
        let call = pending_call(json!({
            "command": "rg --files",
            "cwd": null,
            "reason": null,
        }));

        assert!(!process_call_requests_permission(&call));
        process_intent_from_call(&call).expect("nullable reason should remain valid");
    }

    #[test]
    fn nullable_permissions_are_treated_as_omitted() {
        let call = pending_call(json!({
            "command": "rg --files",
            "permissions": null,
        }));

        assert!(!process_call_requests_permission(&call));
        process_intent_from_call(&call).expect("nullable permissions should remain valid");
    }

    #[tokio::test(flavor = "current_thread")]
    async fn malformed_arguments_fail_before_execution() {
        let call = pending_call(json!({ "command": 42, "cwd": null }));
        let proposal = ProcessCommandToolExecutor
            .propose(call.clone(), ToolExecutionContext::default())
            .await
            .expect("malformed argv should not be infrastructure failure");

        assert!(matches!(proposal, ToolActionPreflight::Outcome(_)));

        let outcome = ProcessCommandToolExecutor
            .execute(call, ToolExecutionContext::default())
            .await
            .expect("malformed argv should resolve as failed tool outcome");

        assert_eq!(outcome.status(), merry_core::ToolCallResultStatus::Failed);
        let diagnostic = outcome
            .diagnostic()
            .expect("failed process argument outcome should include diagnostic");
        assert_eq!(diagnostic.code(), PROCESS_COMMAND_INVALID_ARGUMENTS_CODE);
        let payload: Value = serde_json::from_str(outcome.content().as_str())
            .expect("failed process argument outcome should parse as JSON");
        assert_eq!(
            payload["error"]["message"],
            "command must be a non-empty string"
        );
        assert_eq!(payload["guidance"]["kind"], "invalid_process_arguments");
        assert!(
            payload["guidance"]["message"]
                .as_str()
                .expect("guidance should be text")
                .contains("set cwd to null or a workspace-relative directory")
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn proposal_carries_process_action_evidence() {
        let call = pending_call(json!({ "command": "rustc --version", "cwd": null }));
        let preflight = ProcessCommandToolExecutor
            .propose(call, ToolExecutionContext::default())
            .await
            .expect("proposal should succeed");
        let ToolActionPreflight::Proposal(proposal) = preflight else {
            panic!("proposal should be present");
        };

        let ActionProposalEvidence::ProcessAction(intent) = proposal.evidence() else {
            panic!("proposal should carry process action evidence");
        };
        assert_eq!(intent.argv(), shell_command_argv("rustc --version"));
    }

    #[tokio::test(flavor = "current_thread")]
    async fn proposal_accepts_multiline_command_for_policy_classification() {
        let call = pending_call(json!({
            "command": "cargo check -p merry-runtime\ncargo test -p merry-runtime",
            "cwd": "."
        }));
        let preflight = ProcessCommandToolExecutor
            .propose(call, ToolExecutionContext::default())
            .await
            .expect("multiline shell argv should not be infrastructure failure");
        let ToolActionPreflight::Proposal(proposal) = preflight else {
            panic!("multiline shell argv should produce process proposal");
        };

        let ActionProposalEvidence::ProcessAction(intent) = proposal.evidence() else {
            panic!("proposal should carry process action evidence");
        };
        assert_eq!(
            intent.argv(),
            shell_command_argv("cargo check -p merry-runtime\ncargo test -p merry-runtime")
        );
    }
}