From 343f2a8a6148b8004d6f4d74fb779b4f30c9686a Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 03:11:22 +0100 Subject: M1-M5: sessions, six tools, approvals, prompt, Ctrl-C, target gate - append-only JSONL sessions with --continue/--session; reasoning stored, stripped on replay - read/write/edit/grep/glob + bash with timeout and capped output - diff-preview approval for the first edit of a session, per-call approval for bash - system prompt frozen per session with AGENTS.md snapshot and change warning - SIGINT aborts the turn, second Ctrl-C exits - glibc gate ignores weak symbols; verified in linux/386 debian:12 - acceptance test (calculator in C) passes on both x86-64 and i386 --- src/tools/mod.rs | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/tools/mod.rs (limited to 'src/tools/mod.rs') diff --git a/src/tools/mod.rs b/src/tools/mod.rs new file mode 100644 index 0000000..503de08 --- /dev/null +++ b/src/tools/mod.rs @@ -0,0 +1,148 @@ +pub mod fs; +pub mod shell; + +use serde_json::{json, Value}; + +use fs::Change; + +/// When a tool needs a human before it runs. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Approval { + /// Read-only or harmless. + Never, + /// Prompts on first use per session, then remembers. + Once, + /// Prompts on every call, no memory. + EveryCall, +} + +pub fn approval(name: &str) -> Approval { + match name { + "write" | "edit" => Approval::Once, + "bash" => Approval::EveryCall, + _ => Approval::Never, + } +} + +/// Tools that execute immediately. +pub fn run(name: &str, args: &Value) -> Result { + match name { + "read" => fs::read(args), + "grep" => fs::grep(args), + "glob" => fs::glob(args), + "bash" => shell::bash(args, std::time::Duration::from_secs(120)), + other => Err(unknown(other)), + } +} + +/// Tools that produce a change needing approval before it is applied. +pub fn plan(name: &str, args: &Value) -> Result { + match name { + "write" => fs::plan_write(args), + "edit" => fs::plan_edit(args), + other => Err(unknown(other)), + } +} + +fn unknown(name: &str) -> String { + format!("unknown tool `{name}`; available: read, write, edit, bash, grep, glob") +} + +/// Static and in a fixed order, so the serialised prefix never changes. +pub fn schemas() -> Vec { + vec![ + json!({ + "type": "function", + "function": { + "name": "read", + "description": "Read a text file. Returns numbered lines. Read before editing.", + "parameters": { + "type": "object", + "properties": { + "path": { "type": "string", "description": "file path" }, + "offset": { "type": "integer", "description": "first line, 1-based (default 1)" }, + "limit": { "type": "integer", "description": "maximum lines (default and cap 2000)" } + }, + "required": ["path"] + } + } + }), + json!({ + "type": "function", + "function": { + "name": "write", + "description": "Create or replace a whole file. The parent directory must already exist.", + "parameters": { + "type": "object", + "properties": { + "path": { "type": "string" }, + "content": { "type": "string" } + }, + "required": ["path", "content"] + } + } + }), + json!({ + "type": "function", + "function": { + "name": "edit", + "description": "Replace one exact occurrence of `old` with `new`. Fails if `old` is missing or appears more than once, so include enough context to be unique.", + "parameters": { + "type": "object", + "properties": { + "path": { "type": "string" }, + "old": { "type": "string", "description": "exact existing text, including indentation" }, + "new": { "type": "string", "description": "replacement text" } + }, + "required": ["path", "old", "new"] + } + } + }), + json!({ + "type": "function", + "function": { + "name": "bash", + "description": "Run a shell command. Each call is a fresh shell at the working directory, so `cd` does not persist; pass cwd instead. Times out after 120s. Returns combined output and the exit code.", + "parameters": { + "type": "object", + "properties": { + "cmd": { "type": "string" }, + "cwd": { "type": "string", "description": "working directory (default: project root)" } + }, + "required": ["cmd"] + } + } + }), + json!({ + "type": "function", + "function": { + "name": "grep", + "description": "Search file contents for a literal string, recursively. Skips .git, target, node_modules and similar.", + "parameters": { + "type": "object", + "properties": { + "pattern": { "type": "string" }, + "path": { "type": "string", "description": "file or directory (default .)" }, + "ignore_case": { "type": "boolean" } + }, + "required": ["pattern"] + } + } + }), + json!({ + "type": "function", + "function": { + "name": "glob", + "description": "List files matching a glob. Supports *, ? and ** across path segments, e.g. **/*.rs.", + "parameters": { + "type": "object", + "properties": { + "pattern": { "type": "string" }, + "path": { "type": "string", "description": "directory to search (default .)" } + }, + "required": ["pattern"] + } + } + }), + ] +} -- cgit v1.2.3