zagens-cli 0.8.3

Zagens headless CLI + HTTP/SSE runtime sidecar (`zagens`, `zagens-runtime` binaries)
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
//! Input schemas for miscellaneous built-in tools (kernel-v2 M2).

use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::{Value, json};

use crate::tools::tool_schema::derived_input_schema;

const REVERT_TURN_MAX_OFFSET: u64 = 50;

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
struct FileInfoInput {
    #[schemars(description = "Path to the file (relative to workspace or absolute)")]
    pub path: String,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
struct RememberInput {
    #[schemars(description = "The single-sentence durable note to remember.")]
    pub note: String,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
struct DescribeImageInput {
    #[schemars(description = "Path to the image file. Supported: png, jpg, jpeg, gif, bmp, webp.")]
    pub path: String,
    #[schemars(description = "Optional custom prompt for the vision model.")]
    pub prompt: Option<String>,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
struct FimEditInput {
    #[schemars(description = "Path to the file to edit (relative to workspace)")]
    pub path: String,
    #[schemars(
        description = "Text anchor marking the end of the prefix. Everything up to and including this anchor is kept as-is before the generated middle."
    )]
    pub prefix_anchor: String,
    #[schemars(
        description = "Text anchor marking the start of the suffix. Everything from this anchor onward is kept as-is after the generated middle."
    )]
    pub suffix_anchor: String,
    #[schemars(description = "Maximum tokens to generate (default: 1024)")]
    pub max_tokens: Option<u64>,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
struct ProjectMapInput {
    #[schemars(description = "Maximum depth for the tree view (default: 3).")]
    pub max_depth: Option<u64>,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
struct RecallArchiveInput {
    #[schemars(description = "Search query. Tokenized and BM25-scored against archived messages.")]
    pub query: String,
    #[schemars(description = "Optional: limit to a specific prior cycle number.")]
    pub cycle: Option<u64>,
    #[schemars(description = "Maximum hits to return (default 3, hard-capped at 10).")]
    pub max_results: Option<u64>,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
#[serde(deny_unknown_fields)]
struct LoadSkillInput {
    #[schemars(
        description = "Skill id (the `name` field from the SKILL.md frontmatter, also shown in the `## Skills` listing)."
    )]
    pub name: String,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
#[serde(deny_unknown_fields)]
struct RevertTurnInput {
    #[schemars(
        extend("minimum" = 1, "maximum" = REVERT_TURN_MAX_OFFSET),
        description = "How many turns back to revert (default 1)."
    )]
    pub turn_offset: Option<u64>,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
#[serde(deny_unknown_fields)]
struct RunTestsInput {
    #[schemars(description = "Optional extra arguments to pass to `cargo test` (shell-style).")]
    pub args: Option<String>,
    #[schemars(description = "When true, include `--all-features`.")]
    pub all_features: Option<bool>,
    #[schemars(
        description = "Wall-clock timeout in milliseconds before the test run is killed (default 600,000; max 1,800,000)."
    )]
    pub timeout_ms: Option<u64>,
}

#[derive(Debug, Clone, Copy, Deserialize, JsonSchema)]
enum ValidateDataFormatInput {
    #[serde(rename = "auto")]
    Auto,
    #[serde(rename = "json")]
    Json,
    #[serde(rename = "toml")]
    Toml,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
#[serde(deny_unknown_fields)]
struct ValidateDataInput {
    #[schemars(description = "Optional path to a file within the workspace.")]
    pub path: Option<String>,
    #[schemars(description = "Optional inline content to validate.")]
    pub content: Option<String>,
    #[schemars(
        extend("default" = "auto"),
        description = "Validation format. 'auto' infers from extension then falls back to trying both."
    )]
    pub format: Option<ValidateDataFormatInput>,
}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
#[serde(deny_unknown_fields)]
struct DiagnosticsInput {}

#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(inline)]
struct ReviewInput {
    #[schemars(
        description = "File path, PR URL, or the literal 'diff'/'staged' for git diff review."
    )]
    pub target: String,
    #[schemars(description = "Optional explicit target type: file, diff, or pr.")]
    pub kind: Option<String>,
    #[schemars(description = "Optional git base ref when using diff target (e.g. origin/main).")]
    pub base: Option<String>,
    #[schemars(description = "Review staged changes when using diff target (default: false).")]
    pub staged: Option<bool>,
    #[schemars(description = "Maximum characters to include from the source (default: 200000).")]
    pub max_chars: Option<u64>,
}

#[must_use]
pub fn file_info_input_schema() -> Value {
    derived_input_schema::<FileInfoInput>()
}

#[must_use]
pub fn remember_input_schema() -> Value {
    derived_input_schema::<RememberInput>()
}

#[must_use]
pub fn describe_image_input_schema() -> Value {
    derived_input_schema::<DescribeImageInput>()
}

#[must_use]
pub fn fim_edit_input_schema() -> Value {
    derived_input_schema::<FimEditInput>()
}

#[must_use]
pub fn project_map_input_schema() -> Value {
    derived_input_schema::<ProjectMapInput>()
}

#[must_use]
pub fn recall_archive_input_schema() -> Value {
    derived_input_schema::<RecallArchiveInput>()
}

#[must_use]
pub fn load_skill_input_schema() -> Value {
    derived_input_schema::<LoadSkillInput>()
}

#[must_use]
pub fn revert_turn_input_schema() -> Value {
    derived_input_schema::<RevertTurnInput>()
}

#[must_use]
pub fn run_tests_input_schema() -> Value {
    derived_input_schema::<RunTestsInput>()
}

#[must_use]
pub fn validate_data_input_schema() -> Value {
    derived_input_schema::<ValidateDataInput>()
}

#[must_use]
pub fn diagnostics_input_schema() -> Value {
    let mut schema = derived_input_schema::<DiagnosticsInput>();
    if schema.get("properties").is_none() {
        schema["properties"] = json!({});
    }
    schema
}

#[must_use]
pub fn review_input_schema() -> Value {
    derived_input_schema::<ReviewInput>()
}

#[must_use]
pub fn request_user_input_input_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "questions": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "header": { "type": "string" },
                        "id": { "type": "string" },
                        "question": { "type": "string" },
                        "options": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "label": { "type": "string" },
                                    "description": { "type": "string" }
                                },
                                "required": ["label", "description"]
                            },
                            "minItems": 2,
                            "maxItems": 3
                        }
                    },
                    "required": ["header", "id", "question", "options"]
                },
                "minItems": 1,
                "maxItems": 3
            }
        },
        "required": ["questions"]
    })
}

#[must_use]
pub fn multi_tool_use_parallel_input_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "tool_uses": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "recipient_name": { "type": "string" },
                        "parameters": { "type": "object" }
                    },
                    "required": ["recipient_name", "parameters"]
                }
            }
        },
        "required": ["tool_uses"]
    })
}

#[must_use]
pub fn apply_patch_input_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "path": {
                "type": "string",
                "description": "Path to the file to patch (relative to workspace)"
            },
            "patch": {
                "type": "string",
                "description": "Unified diff patch content"
            },
            "changes": {
                "type": "array",
                "description": "Optional full file replacements (path + content).",
                "items": {
                    "type": "object",
                    "properties": {
                        "path": { "type": "string" },
                        "content": { "type": "string" }
                    },
                    "required": ["path", "content"]
                }
            },
            "fuzz": {
                "type": "integer",
                "description": "Maximum fuzz factor for fuzzy matching (default: 3, max: 50)"
            },
            "create_if_missing": {
                "type": "boolean",
                "description": "Create the file if it doesn't exist (for new file patches)"
            }
        },
        "oneOf": [
            { "required": ["patch"] },
            { "required": ["changes"] }
        ]
    })
}

#[must_use]
pub fn rlm_input_schema() -> Value {
    json!({
        "type": "object",
        "required": ["task"],
        "properties": {
            "task": {
                "type": "string",
                "description": "What to do with the input (e.g. \"Summarize the security model\", \"Extract all API endpoints\", \"Categorize each row by sentiment\"). The sub-agent uses this as its objective."
            },
            "file_path": {
                "type": "string",
                "description": "Workspace-relative path to a file to load as PROMPT. Preferred — keeps the long input out of your context. Mutually exclusive with `content`."
            },
            "content": {
                "type": "string",
                "description": "Inline content to load as PROMPT. Use only when the input isn't a file you can point at. Capped at 200k chars."
            },
            "max_depth": {
                "type": "integer",
                "description": "Recursion budget for `sub_rlm()` calls. 0 disables recursion; default 1 matches paper experiments."
            }
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::apply_patch::ApplyPatchTool;
    use crate::tools::describe_image::DescribeImageTool;
    use crate::tools::diagnostics::DiagnosticsTool;
    use crate::tools::file_info::FileInfoTool;
    use crate::tools::fim::FimEditTool;
    use crate::tools::parallel::MultiToolUseParallelTool;
    use crate::tools::project::ProjectMapTool;
    use crate::tools::recall_archive::RecallArchiveTool;
    use crate::tools::remember::RememberTool;
    use crate::tools::revert_turn::RevertTurnTool;
    use crate::tools::review::ReviewTool;
    use crate::tools::rlm::RlmTool;
    use crate::tools::schema_sanitize;
    use crate::tools::skill::LoadSkillTool;
    use crate::tools::spec::ToolSpec;
    use crate::tools::test_runner::RunTestsTool;
    use crate::tools::user_input::RequestUserInputTool;
    use crate::tools::validate_data::ValidateDataTool;

    fn model_visible_input_schema(tool: &dyn ToolSpec) -> Value {
        let mut schema = tool.input_schema();
        schema_sanitize::sanitize(&mut schema);
        schema
    }

    const MISC_SCHEMA_SNAPSHOT_DIR: &str = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../../fixtures/harness/kernel-v2-schema-snapshots"
    );

    fn fim_tool() -> FimEditTool {
        FimEditTool::new(None, String::new())
    }

    fn review_tool() -> ReviewTool {
        ReviewTool::new(None, String::new())
    }

    fn rlm_tool() -> RlmTool {
        RlmTool::new(None, String::new())
    }

    #[test]
    #[ignore = "bootstrap kernel-v2 misc-tool schema snapshot fixtures"]
    fn dump_misc_tool_schemas_for_snapshot_bootstrap() {
        let tools: [(&str, &dyn ToolSpec); 16] = [
            ("file_info", &FileInfoTool),
            ("remember", &RememberTool),
            ("request_user_input", &RequestUserInputTool),
            ("revert_turn", &RevertTurnTool),
            ("multi_tool_use.parallel", &MultiToolUseParallelTool),
            ("project_map", &ProjectMapTool),
            ("run_tests", &RunTestsTool),
            ("recall_archive", &RecallArchiveTool),
            ("validate_data", &ValidateDataTool),
            ("fim_edit", &fim_tool()),
            ("describe_image", &DescribeImageTool),
            ("diagnostics", &DiagnosticsTool),
            ("load_skill", &LoadSkillTool),
            ("apply_patch", &ApplyPatchTool),
            ("review", &review_tool()),
            ("rlm", &rlm_tool()),
        ];
        for (name, tool) in tools {
            let schema = model_visible_input_schema(tool);
            let pretty = serde_json::to_string_pretty(&schema).expect("serialize");
            println!("=== {name} ===\n{pretty}\n");
        }
    }

    #[test]
    fn misc_tool_model_visible_schemas_match_snapshots() {
        let tools: [(&str, &dyn ToolSpec); 16] = [
            ("file_info", &FileInfoTool),
            ("remember", &RememberTool),
            ("request_user_input", &RequestUserInputTool),
            ("revert_turn", &RevertTurnTool),
            ("multi_tool_use.parallel", &MultiToolUseParallelTool),
            ("project_map", &ProjectMapTool),
            ("run_tests", &RunTestsTool),
            ("recall_archive", &RecallArchiveTool),
            ("validate_data", &ValidateDataTool),
            ("fim_edit", &fim_tool()),
            ("describe_image", &DescribeImageTool),
            ("diagnostics", &DiagnosticsTool),
            ("load_skill", &LoadSkillTool),
            ("apply_patch", &ApplyPatchTool),
            ("review", &review_tool()),
            ("rlm", &rlm_tool()),
        ];
        for (name, tool) in tools {
            assert_eq!(tool.name(), name);
            let schema = model_visible_input_schema(tool);
            let path = format!("{MISC_SCHEMA_SNAPSHOT_DIR}/misc-{name}.json");
            let expected: Value = serde_json::from_str(
                &std::fs::read_to_string(&path)
                    .unwrap_or_else(|e| panic!("missing snapshot {path}: {e}")),
            )
            .expect("parse snapshot JSON");
            assert_eq!(
                schema, expected,
                "model-visible schema drift for {name} — update fixture only after explicit KV-cache review"
            );
        }
    }
}