From 795d8ceba0678d45088b83b5af1cc57b06cbfa2c Mon Sep 17 00:00:00 2001 From: James Chapman Date: Tue, 17 Nov 2026 20:36:50 +0000 Subject: [PATCH 1/4] feat: add model multi-edit and create file tools Add approval-gated workspace_multi_edit_file (1 to 16 exact replacements, one checkpointed apply) and workspace_create_file (exclusive create, no checkpoint). Both reuse the file_write approval and writers-stopped gate under their own digest domains. Tool schema 0.4.0, version 0.36.0, ADR-0054. Windows missing paths now classify as the recoverable not_found. Co-Authored-By: Claude Sonnet 5.5 --- AGENTS.md | 15 +- CHANGELOG.md | 54 ++ Cargo.lock | 10 +- Cargo.toml | 2 +- IMPLEMENTATION_PLAN.md | 47 +- README.md | 25 +- crates/omgcode-app/src/tool_mode.rs | 14 +- crates/omgcode-app/tests/desktop_approvals.rs | 160 +++ crates/omgcode-cli/src/main.rs | 2 +- crates/omgcode-cli/tests/file_workflow.rs | 2 +- crates/omgcode-cli/tests/model_tools.rs | 210 +++- crates/omgcode-core/src/turn.rs | 918 ++++++++++++++++-- crates/omgcode-core/src/turn/review.rs | 224 ++++- crates/omgcode-core/tests/approval_review.rs | 305 ++++++ crates/omgcode-core/tests/turn_runtime.rs | 747 +++++++++++++- .../omgcode-desktop/ui/src/approvals/model.ts | 2 +- crates/omgcode-desktop/ui/src/mock/backend.ts | 11 +- .../ui/tests/approvals.test.ts | 49 +- crates/omgcode-desktop/ui/tests/mock.test.ts | 16 + crates/omgcode-platform/src/broker.rs | 21 + docs/README.md | 19 +- ...-multi-edit-and-create-model-file-tools.md | 120 +++ docs/adr/index.md | 1 + docs/agent-instructions.md | 25 +- docs/development.md | 68 +- docs/execution.md | 7 +- docs/file-access.md | 51 +- docs/platform-support.md | 2 +- docs/security-coding-rules.md | 17 +- docs/session-storage.md | 5 +- docs/testing-standards.md | 1 + docs/workspace-permissions.md | 20 +- 32 files changed, 2940 insertions(+), 230 deletions(-) create mode 100644 docs/adr/0054-add-multi-edit-and-create-model-file-tools.md diff --git a/AGENTS.md b/AGENTS.md index 5f13932..58bf867 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,12 +13,15 @@ without an installer, has run only on Windows, and gives the webview no authorit Rust side authorizes every command and the trusted CLI remains. Follow the [desktop interface](docs/desktop-interface.md) contract. Chat offers the local LM Studio model read and list tools; when the workspace -also enables `read` and `write`, one exact-edit tool; and, when it also enables -`execute`, `read`, and `write`, one command tool. The edit and command tools -each require a trusted single-use approval. The edit tool replaces one exact -text occurrence in an existing file through checkpointed apply and needs the -operator's writers-stopped acknowledgement. No model create, delete, rename, -network, Git, or MCP tool exists. Chat offers tools only for a +also enables `read` and `write`, three file-write tools (exact edit, multi-edit, +and create); and, when it also enables `execute`, `read`, and `write`, one +command tool. The file-write and command tools each require a trusted +single-use approval. The edit tool replaces one exact text occurrence in an +existing file, and the multi-edit tool several exact occurrences in one +existing file, each through checkpointed apply. The create tool makes one new +file through the exclusive create, without a checkpoint. All three need the +operator's writers-stopped acknowledgement. No model delete, rename, network, +Git, or MCP tool exists. Chat offers tools only for a workspace whose `model_tools` preference is enabled. There are no unapproved model writes, other provider adapters, or unreviewed model-callable process execution. The diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c6bf50..03819b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,60 @@ Changes are recorded here before a release version is assigned. ### Added +- Version 0.36.0 adds two approval-gated model file tools, multi-edit and create + (Phase C18). See ADR-0054 and the + [multi-edit and create routes](docs/file-access.md#model-multi-edit-and-create-routes-adr-0054). + Until now the model could change a file only one exact replacement at a time, + each needing its own approval and checkpoint, and could not create a file. + It is on branch `codex/model-file-tools`, stacked on + `codex/checkpoint-ledger-compaction`; it is not merged. It adds no dependency. + - **Tools:** `workspace_multi_edit_file` applies one to 16 exact replacements + to one existing file under one approval and one checkpointed apply. + `workspace_create_file` creates one new UTF-8 file through the broker's + exclusive create. Both are advertised and dispatched only under live + `model_tools`, `read`, and `write`, with the edit tool's per-call recheck + and `edit_disabled` denial. Tool schema 0.4.0; 0.2.0 and 0.3.0 sessions + still replay. Six of the eight tool-name slots are used, and no cap rose. + - **Approvals:** both reuse the `file_write` operation, so the + writers-stopped acknowledgement is required and no enum, wire type, or + TypeScript union changed. Each has its own digest domain tag + (`omgcode-file-multi-edit-v1`, `omgcode-file-create-v1`), so an approval for + `workspace_edit_file`, multi-edit, or create is never consumed by another + tool. The approval flow itself is one shared gate; the edit tool's digest, + codes, and messages are unchanged. + - **Multi-edit rules:** every `old_text` must occur exactly once in the + original live bytes and the matches may not overlap (`edit_overlap`); + touching matches are allowed. The messages name the 1-based edit index and + never file text. The result must differ and fit 1 MiB. + - **Create rules:** before an approval is recorded, a live audited read must + find the path absent (`file_exists`) and a listing must find the parent + (`not_found`). A file that another writer creates after approval is never + replaced; the call returns `file_exists` and the approval stays spent. + Creation is not checkpointed. An uncertain creation is a terminal failure + that leaves the pending file-operation record for the trusted + acknowledgement. + - **Review:** the desktop approvals window reproduces the digest from any of + the three tools (at most eight distinct candidates in total) and shows + numbered `Old text N` and `New text N` fields for a multi-edit and + `File`, `Size`, `SHA-256`, and `Content` for a create. A create review is + stale once its path exists. The approvals label reads "File change". + - **Limits:** the 4 KiB argument cap bounds create content and multi-edit + size. There is no multi-file atomicity and no concurrent-writer detection, + and neither tool is power-loss qualified. No model delete or rename tool + exists. The schema estimate for context accounting rises from 4 KiB to + 5 KiB (the six definitions serialize to 4,546 bytes). + - **Windows missing paths:** the platform now exports `is_missing_entry`, + which recognises the native not-found status Windows keeps inside an + `ErrorKind::Other` error. A missing path in a read, list, edit, or create + call is now the recoverable `not_found` on Windows, as it already was on + Linux, instead of a terminal `native_error`. + - **Tests:** new core tests cover strict argument parsing, the approval flow, + cross-tool non-consumption, refusals, live-permission gating, output-fit + refusal, summaries, and schema 0.3.0 replay. New review tests cover exact + fields, staleness, the shared candidate bound, and hostile text. New app + and CLI tests drive both tools through the desktop service and the operator + flow. New frontend tests cover the review fields and the mock detail. + - Version 0.35.0 compacts the checkpoint ledger in place and raises its bounds (Phase A5). See ADR-0053 and [ledger compaction](docs/file-access.md#implemented-ledger-compaction-phase-a5). diff --git a/Cargo.lock b/Cargo.lock index bd0bb0d..c0b07e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2522,7 +2522,7 @@ dependencies = [ [[package]] name = "omgcode-app" -version = "0.35.0" +version = "0.36.0" dependencies = [ "directories", "omgcode-core", @@ -2537,7 +2537,7 @@ dependencies = [ [[package]] name = "omgcode-cli" -version = "0.35.0" +version = "0.36.0" dependencies = [ "clap", "omgcode-app", @@ -2550,7 +2550,7 @@ dependencies = [ [[package]] name = "omgcode-core" -version = "0.35.0" +version = "0.36.0" dependencies = [ "libc", "omgcode-platform", @@ -2568,7 +2568,7 @@ dependencies = [ [[package]] name = "omgcode-desktop" -version = "0.35.0" +version = "0.36.0" dependencies = [ "omgcode-app", "serde", @@ -2580,7 +2580,7 @@ dependencies = [ [[package]] name = "omgcode-platform" -version = "0.35.0" +version = "0.36.0" dependencies = [ "libc", "rustix", diff --git a/Cargo.toml b/Cargo.toml index 25e53e6..5f00434 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/omgcode-core", "crates/omgcode-cli", "crates/omgcode-platform", "crates/omgcode-app", "crates/omgcode-desktop"] [workspace.package] -version = "0.35.0" +version = "0.36.0" edition = "2024" rust-version = "1.99.1" license = "Apache-2.0" diff --git a/IMPLEMENTATION_PLAN.md b/IMPLEMENTATION_PLAN.md index e4f5f80..962c1df 100644 --- a/IMPLEMENTATION_PLAN.md +++ b/IMPLEMENTATION_PLAN.md @@ -126,8 +126,8 @@ Implemented today: - Conservative context accounting, manual `session compact`, and pre-turn auto-compaction for the local LM Studio route. Summaries are labelled untrusted evidence and follow the live tool-result replay rule (ADR-0041). -- Core turn/tool runtime for read/list, the approval-gated exact edit, and the - approval-gated command through a caller-supplied model interface. It has strict tool-call parsing, round/call/byte/prompt limits, +- Core turn/tool runtime for read/list, the approval-gated exact edit, + multi-edit, and create, and the approval-gated command through a caller-supplied model interface. It has strict tool-call parsing, round/call/byte/prompt limits, cooperative cancellation, source-labelled messages, and fresh per-operation policy bindings. Chat dispatches it through the LM Studio adapter for a workspace that enables `model_tools`. @@ -138,8 +138,9 @@ Implemented today: `chat --stream` and the desktop shell stream the reply as provisional text (ADR-0049). It is text-only unless the session's workspace enables `model_tools`; then it offers the read - and list tools, the approval-gated edit tool when `read` and `write` also - allow it, and the approval-gated command tool when `execute`, `read`, and + and list tools, the approval-gated edit, multi-edit, and create tools when + `read` and `write` also allow it, and the approval-gated command tool when + `execute`, `read`, and `write` also allow it. See [ADR-0026](docs/adr/0026-offer-read-list-tools-through-a-workspace-preference.md), [ADR-0040](docs/adr/0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md), @@ -204,8 +205,8 @@ Implemented today: (ADR-0038). Trusted approval records bind a future model-tool call to a session, workspace, operation, request digest, and settings revision, expire, and are consumed once (ADR-0039). Provider profiles enable no connection; the - command and edit tools are the only model tools that consume an approval - record. + command, edit, multi-edit, and create tools are the only model tools that + consume an approval record. - Provider credentials in protected OS storage (ADR-0042): DPAPI files with verified private DACLs on Windows and Secret Service items in the default collection on Linux, with no plaintext fallback. Trusted @@ -229,8 +230,16 @@ Implemented today: The first call records a pending `file_write` approval with no file text; approval needs `--writers-stopped`, and an identical later call consumes it and calls the checkpointed apply with the live-read expected value. - `session inspect` lists recorded tool requests for review. No model create, - delete, rename, network, Git, or MCP tool exists. + `session inspect` lists recorded tool requests for review. +- The `workspace_multi_edit_file` and `workspace_create_file` model tools + (0.36.0, ADR-0054, on branch `codex/model-file-tools`, stacked on + `codex/checkpoint-ledger-compaction`, not merged) are offered under the same + live `model_tools`, `read`, and `write` preferences, share the `file_write` + approvals and writers-stopped acknowledgement, and each has its own digest + domain. Multi-edit applies one to 16 non-overlapping exact replacements + through one checkpointed apply; create makes one new file through the + exclusive create without a checkpoint. Tool schema 0.4.0; no model delete, + rename, network, Git, or MCP tool exists. - Trusted `file restore` through a new checkpoint and `file delete-checkpoint --permanent` for one resolved checkpoint, on the same filesystems as apply. Neither is power-loss qualified. See @@ -247,7 +256,7 @@ Not implemented: - Apply or restore on other filesystems, workspace file deletion, ledger compaction, and standalone CLI checkpoint preparation. -- Model create, delete, rename, multi-edit, network, Git, and MCP tools; +- Model delete, rename, network, Git, and MCP tools; concurrent-writer detection for any apply route; live provider tool-call qualification; and streaming against a live server, discovery, and cancellation of any request against a live provider. A read, list, or edit @@ -971,8 +980,21 @@ on Windows, and trusted CLI commands as the first caller. See unsupported filesystems are recoverable results. Tool schema 0.3.0; 0.2.0 sessions still replay. See [ADR-0043](docs/adr/0043-gate-the-model-file-edit-tool-with-approvals-and-checkpointed-apply.md). - Concurrent-writer detection, multi-edit, create, delete, and rename remain - unimplemented. + Concurrent-writer detection, delete, and rename remain unimplemented. +- **C18 (version 0.36.0):** the `workspace_multi_edit_file` and + `workspace_create_file` model tools (ADR-0054), on branch + `codex/model-file-tools`, stacked on `codex/checkpoint-ledger-compaction` + and not merged. Both are offered and dispatched under the edit tool's live + `model_tools`, `read`, and `write` rule and reuse the `file_write` approval + operation with its writers-stopped gate, each under its own digest domain + (`omgcode-file-multi-edit-v1`, `omgcode-file-create-v1`). Multi-edit matches + one to 16 `old_text` values against the original live bytes (each exactly + once, no overlap) and uses one checkpointed apply. Create requires an absent + path and an existing parent before any approval is recorded, and uses the + exclusive create without a checkpoint. Tool schema 0.4.0; 0.2.0 and 0.3.0 + sessions still replay. The 4 KiB argument cap bounds create content and + multi-edit size, there is no multi-file atomicity, and 6 of the 8 tool-name + slots are used. - **Run network destination allowlist: deferred (2026-11-16).** No unprivileged mechanism passes the ADR-0034 gate on both hosts. The only candidate is a Linux user and network namespace with a trusted relay, which @@ -987,8 +1009,7 @@ Next decisions, not yet scheduled: endpoint rules for non-loopback providers, HTTPS, and certificates (one adapter, the local LM Studio route, now reads a stored credential, only at its profile's loopback endpoint); -- model create, delete, rename, and multi-edit tools, and network, Git, and - MCP tools; +- model delete and rename tools, and network, Git, and MCP tools; - registry credentials and development-server networking. ### Phase D: additional providers, streaming, and account lifecycle diff --git a/README.md b/README.md index 9ddc3a5..e36617f 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ OMGCode aims to connect language models to controlled development tools. The current code is an early Rust core and command-line prototype. A bounded native workspace file broker is available through trusted CLI commands. A core turn/tool runtime and schema-v2 session format let chat offer the local LM Studio -model read, list, approval-gated exact-edit, and approval-gated command tools, +model read, list, approval-gated edit, multi-edit, create, and command tools, only for a workspace that enables `model_tools` and each tool's own -preferences. Model file creation, deletion, and renaming, and Git workflows +preferences. Model file deletion and renaming, and Git workflows remain planned. A prototype desktop shell (Tauri, with a strict TypeScript frontend) browses and searches sessions, runs chat, and decides model-tool approvals in a separate window; it has no installer and has run only on Windows. @@ -31,11 +31,13 @@ directory grants. > When `execute`, `read`, and `write` are also enabled, it offers a confined > command tool that records a pending approval and runs only after the operator > approves that exact call. When `read` and `write` are enabled, it offers an -> edit tool that replaces one exact text occurrence in an existing file. Each -> edit records a pending approval and is applied through a checkpoint only -> after the operator approves that exact call and acknowledges that other -> writers are stopped. It has no model create, delete, rename, network, Git, or -> MCP tools. +> edit tool that replaces one exact text occurrence in an existing file, a +> multi-edit tool that replaces up to 16 exact occurrences in one existing +> file, and a create tool that makes one new file. Each records a pending +> approval and acts only after the operator approves that exact call and +> acknowledges that other writers are stopped; edits are applied through a +> checkpoint, and creation is not checkpointed. It has no model delete, rename, +> network, Git, or MCP tools. > The trusted > CLI can replace or restore one existing file through a checkpoint on qualified > Linux ext4 or Windows NTFS, and can permanently delete one resolved checkpoint. @@ -107,7 +109,7 @@ frontend in `crates/omgcode-desktop/ui`. The CLI provides: loopback HTTP endpoint. It does not discover, start, or download models, and it rejects model tool requests unless the session's workspace enables `model_tools`. Then it offers read/list tools; when `read` and `write` are - also enabled, the approval-gated edit tool; and when `execute`, `read`, and + also enabled, the approval-gated edit, multi-edit, and create tools; and when `execute`, `read`, and `write` are all enabled, the approval-gated command tool. Each is rechecked against live settings before every call. @@ -162,9 +164,10 @@ an explicit current-content check. Trusted `file delete-checkpoint --permanent` removes one resolved checkpoint's files and keeps its ledger history until trusted `file compact-checkpoints --discard-history`, or an apply, restore, or deletion that would not fit, compacts the ledger. Neither -command is power-loss qualified. The model edit tool uses the same -checkpointed apply after an approved, writers-stopped decision; it cannot -create, delete, or rename files and does not detect a concurrent writer. +command is power-loss qualified. The model edit and multi-edit tools use the same +checkpointed apply after an approved, writers-stopped decision; the create tool +uses the exclusive create without a checkpoint. None can delete or rename files, +and none detects a concurrent writer. Model-dispatched process execution, Git, MCP, and memories remain planned. See the [workspace settings contract](docs/workspace-permissions.md), the [workspace file-access guide](docs/file-access.md), and the [development diff --git a/crates/omgcode-app/src/tool_mode.rs b/crates/omgcode-app/src/tool_mode.rs index 90a5d77..570662e 100644 --- a/crates/omgcode-app/src/tool_mode.rs +++ b/crates/omgcode-app/src/tool_mode.rs @@ -21,9 +21,10 @@ pub const MODEL_TOOLS_SUPPORTED: bool = cfg!(any(target_os = "linux", target_os /// Conservative byte allowance for the advertised tool definitions. /// -/// The largest set (read, list, edit, and command) serializes to 2,763 bytes -/// in the adapter's wire shape; a unit test keeps this allowance above it. -pub(crate) const TOOL_SCHEMA_ESTIMATE_BYTES: usize = 4 * 1024; +/// The largest set (read, list, edit, multi-edit, create, and command) +/// serializes to 4,546 bytes in the adapter's wire shape; a unit test keeps +/// this allowance above it. +pub(crate) const TOOL_SCHEMA_ESTIMATE_BYTES: usize = 5 * 1024; /// Whether this chat turn offers the model the workspace tools. #[derive(Debug)] @@ -115,7 +116,10 @@ pub fn tool_replay_allowed( #[cfg(test)] mod tests { - use omgcode_core::turn::{command_tool_definition, edit_tool_definition, tool_definitions}; + use omgcode_core::turn::{ + command_tool_definition, create_tool_definition, edit_tool_definition, + multi_edit_tool_definition, tool_definitions, + }; use serde_json::{Value, json}; use super::TOOL_SCHEMA_ESTIMATE_BYTES; @@ -126,6 +130,8 @@ mod tests { fn the_schema_estimate_covers_every_advertised_tool_definition() { let mut definitions = tool_definitions(); definitions.push(edit_tool_definition()); + definitions.push(multi_edit_tool_definition()); + definitions.push(create_tool_definition()); definitions.push(command_tool_definition()); let wire: Vec = definitions .iter() diff --git a/crates/omgcode-app/tests/desktop_approvals.rs b/crates/omgcode-app/tests/desktop_approvals.rs index 2f25165..a17192d 100644 --- a/crates/omgcode-app/tests/desktop_approvals.rs +++ b/crates/omgcode-app/tests/desktop_approvals.rs @@ -578,3 +578,163 @@ fn without_a_settings_store_there_are_no_approvals() { assert_eq!(missing.code(), "not_found"); assert!(json_of(&listed).get("now_unix").is_some()); } + +const MULTI_EDIT_TOOL: &str = "workspace_multi_edit_file"; +const CREATE_TOOL: &str = "workspace_create_file"; + +fn multi_edit_args(path: &str) -> Value { + json!({ + "path": path, + "edits": [ + {"old_text": "gamma", "new_text": "G"}, + {"old_text": "alpha", "new_text": "A"}, + ], + }) +} + +fn create_args(path: &str) -> Value { + json!({"path": path, "content": "hello\nworld\n"}) +} + +fn field_value<'a>(detail: &'a omgcode_app::desktop::ApprovalDetail, label: &str) -> &'a str { + detail + .fields + .iter() + .find(|field| field.label == label) + .map(|field| field.value.as_str()) + .unwrap_or_else(|| panic!("field {label}")) +} + +#[test] +fn a_multi_edit_approval_is_reviewed_approved_and_consumed_by_the_identical_call() { + let setup = setup(vec![ + Step::reply(tool_call( + "multi-1", + MULTI_EDIT_TOOL, + &multi_edit_args("notes.txt"), + )), + Step::reply(text("waiting for approval")), + Step::reply(tool_call( + "multi-2", + MULTI_EDIT_TOOL, + &multi_edit_args("notes.txt"), + )), + Step::reply(text("applied")), + ]); + let notes = setup.workspace.path.join("notes.txt"); + fs::write(¬es, "alpha beta gamma\n").expect("workspace file"); + let session = setup.session("Multi"); + setup.turn(session, "change two things"); + + let pending = setup.pending(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].operation, ApprovalOperationDto::FileWrite); + let id = pending[0].id; + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Exact); + assert_eq!(detail.tool.as_deref(), Some(MULTI_EDIT_TOOL)); + assert_eq!(field_value(&detail, "File"), "notes.txt"); + assert_eq!(field_value(&detail, "Edits"), "2"); + assert_eq!(field_value(&detail, "Old text 1"), "gamma"); + assert_eq!(field_value(&detail, "New text 2"), "A"); + assert_eq!(field_value(&detail, "Size"), "17 -> 9 bytes"); + assert_eq!( + fs::read_to_string(¬es).expect("read"), + "alpha beta gamma\n" + ); + let token = detail.review_token.clone().expect("a review token"); + setup + .decide(id, "approve", true, Some(&token)) + .expect("approve"); + + setup.turn(session, "try again"); + assert_eq!(fs::read_to_string(¬es).expect("read"), "A beta G\n"); + assert_eq!(setup.state_of(id), ApprovalStateDto::Consumed); + assert_eq!(setup.server.finish(), 4); +} + +#[test] +fn a_changed_multi_edit_target_makes_the_review_stale() { + let setup = setup(vec![ + Step::reply(tool_call( + "multi-1", + MULTI_EDIT_TOOL, + &multi_edit_args("notes.txt"), + )), + Step::reply(text("waiting for approval")), + ]); + fs::write(setup.workspace.path.join("notes.txt"), "alpha beta gamma\n").expect("file"); + let session = setup.session("Multi"); + setup.turn(session, "change two things"); + let id = setup.pending()[0].id; + assert_eq!(setup.detail(id).binding, Binding::Exact); + fs::write( + setup.workspace.path.join("notes.txt"), + "alpha beta gamma!\n", + ) + .expect("change"); + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Stale); + assert_eq!(detail.review_token, None); + assert_eq!( + setup + .decide(id, "approve", true, None) + .expect_err("a stale review has no token") + .code(), + "review_expired" + ); + assert_eq!(setup.server.finish(), 2); +} + +#[test] +fn a_create_approval_is_reviewed_approved_and_consumed_by_the_identical_call() { + let setup = setup(vec![ + Step::reply(tool_call("create-1", CREATE_TOOL, &create_args("new.txt"))), + Step::reply(text("waiting for approval")), + Step::reply(tool_call("create-2", CREATE_TOOL, &create_args("new.txt"))), + Step::reply(text("created")), + ]); + let created = setup.workspace.path.join("new.txt"); + let session = setup.session("Create"); + setup.turn(session, "add a file"); + + let pending = setup.pending(); + assert_eq!(pending.len(), 1); + let id = pending[0].id; + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Exact); + assert_eq!(detail.tool.as_deref(), Some(CREATE_TOOL)); + assert_eq!(field_value(&detail, "File"), "new.txt"); + assert_eq!(field_value(&detail, "Size"), "12 bytes"); + assert_eq!(field_value(&detail, "Content"), "hello\nworld\n"); + assert!(!created.exists()); + let token = detail.review_token.clone().expect("a review token"); + setup + .decide(id, "approve", true, Some(&token)) + .expect("approve"); + + setup.turn(session, "try again"); + assert_eq!( + fs::read_to_string(&created).expect("read"), + "hello\nworld\n" + ); + assert_eq!(setup.state_of(id), ApprovalStateDto::Consumed); + assert_eq!(setup.server.finish(), 4); +} + +#[test] +fn a_create_review_becomes_stale_once_the_file_exists() { + let setup = setup(vec![ + Step::reply(tool_call("create-1", CREATE_TOOL, &create_args("new.txt"))), + Step::reply(text("waiting for approval")), + ]); + let session = setup.session("Create"); + setup.turn(session, "add a file"); + let id = setup.pending()[0].id; + assert_eq!(setup.detail(id).binding, Binding::Exact); + fs::write(setup.workspace.path.join("new.txt"), "someone else").expect("other writer"); + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Stale); + assert_eq!(detail.review_token, None); + assert_eq!(setup.server.finish(), 2); +} diff --git a/crates/omgcode-cli/src/main.rs b/crates/omgcode-cli/src/main.rs index 5229484..0b16661 100644 --- a/crates/omgcode-cli/src/main.rs +++ b/crates/omgcode-cli/src/main.rs @@ -332,7 +332,7 @@ fn run(cli: Cli) -> Result<(), CliError> { "unavailable" }, "tool_policy": if cfg!(any(target_os = "linux", target_os = "windows")) { - "read_list_when_workspace_model_tools_enabled_edit_and_command_require_approval" + "read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_and_command_require_approval" } else { "reject all model-requested tools" }, diff --git a/crates/omgcode-cli/tests/file_workflow.rs b/crates/omgcode-cli/tests/file_workflow.rs index 194b640..08179ab 100644 --- a/crates/omgcode-cli/tests/file_workflow.rs +++ b/crates/omgcode-cli/tests/file_workflow.rs @@ -263,7 +263,7 @@ fn cli_creates_lists_reads_and_audits_files_without_recording_contents() { assert_eq!( capabilities["tool_policy"], if cfg!(any(target_os = "linux", target_os = "windows")) { - "read_list_when_workspace_model_tools_enabled_edit_and_command_require_approval" + "read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_and_command_require_approval" } else { "reject all model-requested tools" } diff --git a/crates/omgcode-cli/tests/model_tools.rs b/crates/omgcode-cli/tests/model_tools.rs index e41fb33..8f30ea1 100644 --- a/crates/omgcode-cli/tests/model_tools.rs +++ b/crates/omgcode-cli/tests/model_tools.rs @@ -336,7 +336,9 @@ fn an_enabled_preference_lets_the_model_read_the_workspace_through_the_runtime() [ "workspace_read_file", "workspace_list_directory", - "workspace_edit_file" + "workspace_edit_file", + "workspace_multi_edit_file", + "workspace_create_file" ] ); // The compiled base prompt remains the only system message. @@ -1531,3 +1533,209 @@ fn chat_does_not_offer_or_run_the_edit_tool_without_write() { let events = fs::read_to_string(&journal).expect("journal"); assert!(events.contains("edit_disabled")); } + +/// The operator flow for a model multi-edit: one approval covers two +/// replacements, which apply together through one checkpoint. +#[test] +fn an_approved_model_multi_edit_applies_through_the_operator_flow() { + let fixture = Fixture::new(NOTE); + let journal = fixture.session(); + fixture.set("--model-tools", true); + let edit = json!({ + "path": "note.txt", + "edits": [ + {"old_text": "model", "new_text": "agent"}, + {"old_text": "notes", "new_text": "facts"}, + ], + }); + + let server = SequenceServer::start(vec![ + tool_call("call-1", "workspace_multi_edit_file", &edit), + text("Waiting for operator approval."), + ]); + let first = fixture.chat(&journal, &server.endpoint, "edit the note twice"); + let requests = server.finish(); + assert_success(&first); + let names: Vec<&str> = requests[0]["tools"] + .as_array() + .expect("tools") + .iter() + .map(|tool| tool["function"]["name"].as_str().expect("name")) + .collect(); + assert!(names.contains(&"workspace_multi_edit_file"), "{names:?}"); + let result = user_texts(&requests[1]) + .into_iter() + .find(|text| text.starts_with("Tool result [call call-1")) + .expect("approval result sent to the model"); + assert!( + result.contains("\"status\":\"approval_required\""), + "{result}" + ); + assert_eq!( + fs::read(fixture.workspace.join("note.txt")).expect("note"), + NOTE + ); + + let inspected = run_cli(&fixture.data, &["session", "inspect", path_arg(&journal)]); + assert_success(&inspected); + let inspected = json_output(&inspected); + let recorded = inspected["tool_requests"] + .as_array() + .expect("tool requests") + .iter() + .find(|request| request["tool"] == "workspace_multi_edit_file") + .expect("the recorded multi-edit request"); + let arguments: Value = + serde_json::from_str(recorded["arguments"].as_str().expect("arguments")).expect("JSON"); + assert_eq!(arguments, edit); + + let listed = run_cli(&fixture.data, &["approvals", "list"]); + assert_success(&listed); + let approvals = json_output(&listed)["approvals"] + .as_array() + .expect("approvals") + .to_vec(); + assert_eq!(approvals.len(), 1, "one pending request: {approvals:?}"); + assert_eq!(approvals[0]["operation"], "file_write"); + let summary = approvals[0]["summary"].as_str().expect("summary"); + assert!( + summary.starts_with("multi-edit note.txt (2 edits): 29 -> 29 bytes, sha256 "), + "{summary}" + ); + assert!(!summary.contains("workspace"), "{summary}"); + let approval = approvals[0]["id"].as_str().expect("approval id").to_owned(); + + assert_error_code( + &run_cli(&fixture.data, &["approvals", "approve", &approval]), + "approval", + ); + assert_success(&run_cli( + &fixture.data, + &["approvals", "approve", &approval, "--writers-stopped"], + )); + + let server = SequenceServer::start(vec![ + tool_call("call-2", "workspace_multi_edit_file", &edit), + text("The note is edited."), + ]); + let second = fixture.chat(&journal, &server.endpoint, "edit the note twice"); + let requests = server.finish(); + assert_success(&second); + let result = user_texts(&requests[1]) + .into_iter() + .find(|text| text.starts_with("Tool result [call call-2")) + .expect("applied result sent to the model"); + assert!(result.contains("\"status\":\"applied\""), "{result}"); + assert!(result.contains("\"edits\":2"), "{result}"); + assert!( + !result.contains("notes") && !result.contains("facts"), + "no file text: {result}" + ); + assert_eq!( + fs::read(fixture.workspace.join("note.txt")).expect("note"), + b"workspace facts for the agent" + ); + let listed = run_cli(&fixture.data, &["approvals", "list"]); + assert_success(&listed); + assert_eq!(json_output(&listed)["approvals"][0]["state"], "consumed"); + let checkpoints = run_cli(&fixture.data, &["file", "checkpoints"]); + assert_success(&checkpoints); + assert_eq!( + json_output(&checkpoints)["operations"] + .as_array() + .expect("operations") + .len(), + 1 + ); +} + +/// The operator flow for a model file creation: approval, then one exclusive +/// create with no checkpoint, and a repeat is refused because the file exists. +#[test] +fn an_approved_model_create_makes_the_file_through_the_operator_flow() { + let fixture = Fixture::new(NOTE); + let journal = fixture.session(); + fixture.set("--model-tools", true); + let create = json!({"path": "created.txt", "content": "fresh\n"}); + + let server = SequenceServer::start(vec![ + tool_call("call-1", "workspace_create_file", &create), + text("Waiting for operator approval."), + ]); + let first = fixture.chat(&journal, &server.endpoint, "add a file"); + let requests = server.finish(); + assert_success(&first); + assert!( + requests[0]["tools"] + .to_string() + .contains("workspace_create_file") + ); + assert!(!fixture.workspace.join("created.txt").exists()); + + let listed = run_cli(&fixture.data, &["approvals", "list"]); + assert_success(&listed); + let approvals = json_output(&listed)["approvals"] + .as_array() + .expect("approvals") + .to_vec(); + assert_eq!(approvals.len(), 1, "one pending request: {approvals:?}"); + assert_eq!(approvals[0]["operation"], "file_write"); + let summary = approvals[0]["summary"].as_str().expect("summary"); + assert!( + summary.starts_with("create created.txt: 6 bytes, sha256 "), + "{summary}" + ); + assert!(!summary.contains("fresh"), "{summary}"); + let approval = approvals[0]["id"].as_str().expect("approval id").to_owned(); + assert_error_code( + &run_cli(&fixture.data, &["approvals", "approve", &approval]), + "approval", + ); + assert_success(&run_cli( + &fixture.data, + &["approvals", "approve", &approval, "--writers-stopped"], + )); + + let server = SequenceServer::start(vec![ + tool_call("call-2", "workspace_create_file", &create), + text("The file is created."), + tool_call("call-3", "workspace_create_file", &create), + text("It already exists."), + ]); + let second = fixture.chat(&journal, &server.endpoint, "add a file"); + assert_success(&second); + assert_eq!( + fs::read(fixture.workspace.join("created.txt")).expect("created"), + b"fresh\n" + ); + let third = fixture.chat(&journal, &server.endpoint, "add it again"); + assert_success(&third); + let requests = server.finish(); + let result = user_texts(&requests[1]) + .into_iter() + .find(|text| text.starts_with("Tool result [call call-2")) + .expect("created result sent to the model"); + assert!(result.contains("\"status\":\"created\""), "{result}"); + assert!(!result.contains("fresh"), "no file text: {result}"); + assert!( + user_texts(&requests[3]).iter().any(|text| { + text.starts_with("Tool error [call call-3") && text.contains("file_exists") + }), + "a second creation is refused" + ); + let listed = run_cli(&fixture.data, &["approvals", "list"]); + assert_success(&listed); + let approvals = json_output(&listed)["approvals"].clone(); + assert_eq!(approvals.as_array().expect("approvals").len(), 1); + assert_eq!(approvals[0]["state"], "consumed"); + let checkpoints = run_cli(&fixture.data, &["file", "checkpoints"]); + assert_success(&checkpoints); + assert_eq!( + json_output(&checkpoints)["operations"] + .as_array() + .expect("operations") + .len(), + 0, + "creation is not checkpointed" + ); +} diff --git a/crates/omgcode-core/src/turn.rs b/crates/omgcode-core/src/turn.rs index 943e000..bdbfb47 100644 --- a/crates/omgcode-core/src/turn.rs +++ b/crates/omgcode-core/src/turn.rs @@ -16,6 +16,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, Instant}; +use omgcode_platform::broker::is_missing_entry; use serde::Serialize; use serde_json::{Value, json}; use thiserror::Error; @@ -46,9 +47,10 @@ pub use review::{ /// Revision of the tool schemas advertised by this runtime. /// -/// Version `0.3.0` adds `workspace_edit_file`. Journals record the version -/// that was advertised; earlier versions replay unchanged. -pub const TOOL_SCHEMA_VERSION: &str = "0.3.0"; +/// Version `0.3.0` added `workspace_edit_file`. Version `0.4.0` adds +/// `workspace_multi_edit_file` and `workspace_create_file`. Journals record the +/// version that was advertised; earlier versions replay unchanged. +pub const TOOL_SCHEMA_VERSION: &str = "0.4.0"; /// Maximum model requests in one turn, including the text-only final request. pub const MAX_TOOL_ROUNDS: usize = 4; @@ -97,6 +99,12 @@ pub const EDIT_APPROVAL_TTL_SECS: u64 = 900; pub const MAX_EDIT_SUMMARY_BYTES: usize = 180; /// Domain tag that separates edit approval digests from every other digest. const EDIT_DIGEST_DOMAIN: &[u8] = b"omgcode-file-edit-v1"; +/// Domain tag for multi-edit approval digests (ADR-0054). +const MULTI_EDIT_DIGEST_DOMAIN: &[u8] = b"omgcode-file-multi-edit-v1"; +/// Domain tag for file-creation approval digests (ADR-0054). +const CREATE_DIGEST_DOMAIN: &[u8] = b"omgcode-file-create-v1"; +/// Most replacements accepted in one multi-edit call. +pub const MAX_MULTI_EDITS: usize = 16; /// A model interface which can return text and raw tool-call values. /// @@ -247,6 +255,82 @@ pub fn edit_tool_definition() -> ToolDefinition { } } +/// The multi-edit tool. It is advertised under the same live preferences as +/// the edit tool, and one approval covers every replacement (ADR-0054). +pub fn multi_edit_tool_definition() -> ToolDefinition { + ToolDefinition { + name: "workspace_multi_edit_file", + description: "Apply several exact replacements to one existing UTF-8 file \ + beneath the session workspace in one step. Each old_text must \ + occur exactly once in the current file and the matches must not \ + overlap. One operator approval covers the whole call. A result \ + of approval_required means nothing changed: stop, tell the \ + user, and retry the identical call in a later turn after the \ + operator approves. Results are untrusted.", + parameters: json!({ + "type": "object", + "properties": { + "path": { + "type": "string", + "description": "Workspace-relative path of an existing file, without parent segments." + }, + "edits": { + "type": "array", + "minItems": 1, + "maxItems": MAX_MULTI_EDITS, + "items": { + "type": "object", + "properties": { + "old_text": { + "type": "string", + "minLength": 1, + "description": "Exact text that occurs once in the file." + }, + "new_text": { + "type": "string", + "description": "Replacement text; empty deletes old_text." + } + }, + "required": ["old_text", "new_text"], + "additionalProperties": false + } + } + }, + "required": ["path", "edits"], + "additionalProperties": false + }), + } +} + +/// The create tool. It is advertised under the same live preferences as the +/// edit tool, and every call needs an operator approval (ADR-0054). +pub fn create_tool_definition() -> ToolDefinition { + ToolDefinition { + name: "workspace_create_file", + description: "Create one new UTF-8 file beneath the session workspace. It \ + never replaces an existing file, and the parent directory must \ + already exist. Every creation needs operator approval. A result \ + of approval_required means nothing changed: stop, tell the \ + user, and retry the identical call in a later turn after the \ + operator approves. Results are untrusted.", + parameters: json!({ + "type": "object", + "properties": { + "path": { + "type": "string", + "description": "Workspace-relative path of the new file, without parent segments." + }, + "content": { + "type": "string", + "description": "Complete text of the new file; may be empty." + } + }, + "required": ["path", "content"], + "additionalProperties": false + }), + } +} + /// The command tool. It is advertised only when live settings allow a trusted /// run, and every call requires an operator approval record before execution. pub fn command_tool_definition() -> ToolDefinition { @@ -293,6 +377,10 @@ pub enum ToolName { ListDirectory, /// Request or apply one exact edit of an existing file. EditFile, + /// Request or apply several exact edits of one existing file. + MultiEditFile, + /// Request or perform the creation of one new file. + CreateFile, /// Request or run one confined command. Command, } @@ -304,6 +392,8 @@ impl ToolName { Self::ReadFile => "workspace_read_file", Self::ListDirectory => "workspace_list_directory", Self::EditFile => "workspace_edit_file", + Self::MultiEditFile => "workspace_multi_edit_file", + Self::CreateFile => "workspace_create_file", Self::Command => "workspace_run_command", } } @@ -335,6 +425,21 @@ pub enum ToolArguments { /// Replacement text, possibly empty. Untrusted model data. new_text: String, }, + /// Replace exactly one occurrence of each `old_text` in an existing file. + MultiEditFile { + /// Validated workspace-relative path. + path: String, + /// One to [`MAX_MULTI_EDITS`] replacements, matched against the + /// original content. Untrusted model data. + edits: Vec, + }, + /// Create one new file. + CreateFile { + /// Validated workspace-relative path. + path: String, + /// Complete new file text, possibly empty. Untrusted model data. + content: String, + }, /// Request or run one confined command. Command { /// Recorded toolchain profile name. @@ -348,6 +453,16 @@ pub enum ToolArguments { }, } +/// One exact replacement inside a multi-edit call. Both texts are untrusted +/// model data. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct TextEdit { + /// Non-empty text that must occur exactly once in the original file. + pub old_text: String, + /// Replacement text, possibly empty. + pub new_text: String, +} + /// One strictly validated model tool call. #[derive(Clone, Debug, Eq, PartialEq)] pub struct ToolCall { @@ -410,6 +525,8 @@ impl ToolCall { "workspace_read_file" => ToolName::ReadFile, "workspace_list_directory" => ToolName::ListDirectory, "workspace_edit_file" => ToolName::EditFile, + "workspace_multi_edit_file" => ToolName::MultiEditFile, + "workspace_create_file" => ToolName::CreateFile, "workspace_run_command" => ToolName::Command, _ => return Err(ToolCallError::UnsupportedTool), }; @@ -506,6 +623,55 @@ impl ToolCall { new_text: new_text.to_owned(), } } + ToolName::MultiEditFile => { + let mut path = None; + let mut edits = None; + for (key, value) in arguments_object { + match key.as_str() { + "path" => { + path = Some(value.as_str().ok_or(ToolCallError::InvalidArguments)?); + } + "edits" => { + let values = value.as_array().ok_or(ToolCallError::InvalidArguments)?; + if !(1..=MAX_MULTI_EDITS).contains(&values.len()) { + return Err(ToolCallError::InvalidArguments); + } + let mut parsed = Vec::with_capacity(values.len()); + for item in values { + parsed.push(parse_text_edit(item)?); + } + edits = Some(parsed); + } + _ => return Err(ToolCallError::InvalidArguments), + } + } + let path = path.ok_or(ToolCallError::InvalidArguments)?; + let edits = edits.ok_or(ToolCallError::InvalidArguments)?; + validate_relative_path(path, false)?; + ToolArguments::MultiEditFile { + path: path.to_owned(), + edits, + } + } + ToolName::CreateFile => { + let mut path = None; + let mut content = None; + for (key, value) in arguments_object { + let text = value.as_str().ok_or(ToolCallError::InvalidArguments)?; + match key.as_str() { + "path" => path = Some(text), + "content" => content = Some(text), + _ => return Err(ToolCallError::InvalidArguments), + } + } + let path = path.ok_or(ToolCallError::InvalidArguments)?; + let content = content.ok_or(ToolCallError::InvalidArguments)?; + validate_relative_path(path, false)?; + ToolArguments::CreateFile { + path: path.to_owned(), + content: content.to_owned(), + } + } ToolName::Command => { let mut profile = None; let mut program = None; @@ -588,7 +754,9 @@ impl ToolCall { match &self.arguments { ToolArguments::ReadFile { path, .. } | ToolArguments::ListDirectory { path, .. } - | ToolArguments::EditFile { path, .. } => path, + | ToolArguments::EditFile { path, .. } + | ToolArguments::MultiEditFile { path, .. } + | ToolArguments::CreateFile { path, .. } => path, ToolArguments::Command { .. } => "", } } @@ -599,6 +767,31 @@ impl ToolCall { } } +/// Parse one element of a multi-edit `edits` array: exactly `old_text` +/// (non-empty) and `new_text`, both strings. +fn parse_text_edit(value: &Value) -> Result { + let object = value.as_object().ok_or(ToolCallError::InvalidArguments)?; + let mut old_text = None; + let mut new_text = None; + for (key, value) in object { + let text = value.as_str().ok_or(ToolCallError::InvalidArguments)?; + match key.as_str() { + "old_text" => old_text = Some(text), + "new_text" => new_text = Some(text), + _ => return Err(ToolCallError::InvalidArguments), + } + } + let old_text = old_text.ok_or(ToolCallError::InvalidArguments)?; + let new_text = new_text.ok_or(ToolCallError::InvalidArguments)?; + if old_text.is_empty() { + return Err(ToolCallError::InvalidArguments); + } + Ok(TextEdit { + old_text: old_text.to_owned(), + new_text: new_text.to_owned(), + }) +} + /// A rejected tool-call value. Messages contain no caller-supplied text. #[derive(Clone, Copy, Debug, Eq, Error, PartialEq)] pub enum ToolCallError { @@ -991,6 +1184,8 @@ impl<'a> TurnRuntime<'a> { let mut tools = tool_definitions(); if permissions.is_some_and(edit_tool_allowed) { tools.push(edit_tool_definition()); + tools.push(multi_edit_tool_definition()); + tools.push(create_tool_definition()); } if permissions.is_some_and(command_tool_allowed) { tools.push(command_tool_definition()); @@ -1374,6 +1569,21 @@ impl ToolExecution { } } + /// A recoverable refusal with a short fixed-format message that names an + /// edit index or count but never file or model text. + fn recoverable_detail(code: &'static str, call: &ToolCall, message: &str) -> Self { + let body = json!({"tool": call.name.as_str(), "error": code, "message": message}); + Self { + outcome: ToolOutcome::Failed, + code: Some(code), + text: format!( + "Tool error [call {}; source core_observed; untrusted workspace data]:\n{body}", + call.call_id + ), + terminal: None, + } + } + fn denied(code: &'static str, call: &ToolCall, error: FileError) -> Self { Self { outcome: ToolOutcome::Denied, @@ -1451,7 +1661,12 @@ impl WorkspaceToolDispatcher<'_> { } return self.dispatch_command(control, call, max_result_bytes); } - if matches!(call.arguments, ToolArguments::EditFile { .. }) { + if matches!( + call.arguments, + ToolArguments::EditFile { .. } + | ToolArguments::MultiEditFile { .. } + | ToolArguments::CreateFile { .. } + ) { if !edit_tool_allowed(permissions) { return ToolExecution::denied( "edit_disabled", @@ -1459,7 +1674,15 @@ impl WorkspaceToolDispatcher<'_> { FileError::Denied(DecisionReason::PermissionDisabled), ); } - return self.dispatch_edit(control, call, max_result_bytes); + return match call.arguments { + ToolArguments::MultiEditFile { .. } => { + self.dispatch_multi_edit(control, call, max_result_bytes) + } + ToolArguments::CreateFile { .. } => { + self.dispatch_create(control, call, max_result_bytes) + } + _ => self.dispatch_edit(control, call, max_result_bytes), + }; } let binding = match control.bind_session(self.session_id, self.workspace_id) { Ok(binding) => binding, @@ -1494,7 +1717,11 @@ impl WorkspaceToolDispatcher<'_> { Err(error) => classify_file_error(call, error), } } - ToolArguments::EditFile { .. } => ToolExecution::recoverable("invalid_request", call), + ToolArguments::EditFile { .. } + | ToolArguments::MultiEditFile { .. } + | ToolArguments::CreateFile { .. } => { + ToolExecution::recoverable("invalid_request", call) + } ToolArguments::Command { .. } => ToolExecution::recoverable("invalid_command", call), } } @@ -1540,19 +1767,207 @@ impl WorkspaceToolDispatcher<'_> { Ok(prepared) => prepared, Err(code) => return ToolExecution::recoverable(code, call), }; - let PreparedEdit { - normalized, - current_artifact, - proposed, - proposed_artifact, - } = prepared; let digest = edit_digest( + self.session_id, + self.workspace_id, + &prepared.normalized, + &prepared.current_artifact, + &prepared.proposed_artifact, + ); + let request = WriteRequest { + digest, + summary: edit_summary( + &prepared.normalized, + &prepared.current_artifact, + &prepared.proposed_artifact, + ), + widest_success: edit_success_message( + call, + &prepared.normalized, + u64::MAX, + &"f".repeat(64), + Some(Uuid::max()), + None, + ), + noun: "edit", + }; + if let WriteGate::Respond(execution) = + self.write_gate(&mut control, call, request, max_result_bytes) + { + return execution; + } + apply_prepared_edit(&mut control, &binding, &target, call, &prepared, None) + } + + /// Request or apply several exact edits of one file under one approval. + /// + /// The control flow matches [`Self::dispatch_edit`]: one live audited read + /// supplies the expected current value, the shared gate handles the + /// approval, and the same checkpointed apply publishes the result. + fn dispatch_multi_edit( + &self, + mut control: ControlStore, + call: &ToolCall, + max_result_bytes: usize, + ) -> ToolExecution { + let ToolArguments::MultiEditFile { path, edits } = &call.arguments else { + return ToolExecution::recoverable("invalid_request", call); + }; + let binding = match control.bind_session(self.session_id, self.workspace_id) { + Ok(binding) => binding, + Err(error) => { + return ToolExecution::denied( + "policy_unavailable", + call, + FileError::Control(error), + ); + } + }; + let target = edit_target(self.session_id, self.workspace_id, path); + let current = match read_edit_source(&mut control, &binding, &target) { + Ok(bytes) => bytes, + Err(error) => return classify_edit_error(call, error), + }; + let prepared = match prepare_multi_edit(path, edits, ¤t) { + Ok(prepared) => prepared, + Err(refusal) => return refusal.into_execution(call), + }; + let digest = multi_edit_digest( + self.session_id, + self.workspace_id, + &prepared.normalized, + &prepared.current_artifact, + &prepared.proposed_artifact, + ); + let request = WriteRequest { + digest, + summary: multi_edit_summary( + &prepared.normalized, + edits.len(), + &prepared.current_artifact, + &prepared.proposed_artifact, + ), + widest_success: edit_success_message( + call, + &prepared.normalized, + u64::MAX, + &"f".repeat(64), + Some(Uuid::max()), + Some(edits.len()), + ), + noun: "multi-edit", + }; + if let WriteGate::Respond(execution) = + self.write_gate(&mut control, call, request, max_result_bytes) + { + return execution; + } + apply_prepared_edit( + &mut control, + &binding, + &target, + call, + &prepared, + Some(edits.len()), + ) + } + + /// Request or perform the creation of one new file. + /// + /// Before any approval is recorded, a live audited read must find the path + /// absent and a listing must find its parent. An approval is consumed + /// before the exclusive create, so a file that another writer creates in + /// the meantime spends it. Creation is not checkpointed: there is no prior + /// content to keep, and the exclusive create refuses an existing entry. + fn dispatch_create( + &self, + mut control: ControlStore, + call: &ToolCall, + max_result_bytes: usize, + ) -> ToolExecution { + let ToolArguments::CreateFile { path, content } = &call.arguments else { + return ToolExecution::recoverable("invalid_request", call); + }; + let Some(content_artifact) = artifact(content.as_bytes()) else { + return ToolExecution::recoverable("edit_too_large", call); + }; + if content.len() > MAX_EDIT_FILE_BYTES { + return ToolExecution::recoverable("edit_too_large", call); + } + let binding = match control.bind_session(self.session_id, self.workspace_id) { + Ok(binding) => binding, + Err(error) => { + return ToolExecution::denied( + "policy_unavailable", + call, + FileError::Control(error), + ); + } + }; + let target = edit_target(self.session_id, self.workspace_id, path); + if let Some(refusal) = create_precheck(&mut control, &binding, &target, call) { + return refusal; + } + let normalized = normalized_path(path); + let digest = create_digest( self.session_id, self.workspace_id, &normalized, - ¤t_artifact, - &proposed_artifact, + &content_artifact, ); + let request = WriteRequest { + digest, + summary: create_summary(&normalized, &content_artifact), + widest_success: create_success_message( + call, + &normalized, + u64::MAX, + &"f".repeat(64), + Some(Uuid::max()), + ), + noun: "file creation", + }; + if let WriteGate::Respond(execution) = + self.write_gate(&mut control, call, request, max_result_bytes) + { + return execution; + } + // The approval is spent. A concurrent creator makes this a conflict. + match FileBroker::new(&mut control).create_file(&binding, &target, content.as_bytes()) { + Ok(created) => ToolExecution::success(create_success_message( + call, + &normalized, + content_artifact.bytes, + &content_artifact.sha256, + Some(created.operation_id), + )), + Err(FileError::Conflict) => ToolExecution::recoverable("file_exists", call), + // An uncertain creation stays a terminal failure and leaves its + // pending audit record for the trusted acknowledgement. + Err(error) => classify_edit_error(call, error), + } + } + + /// The approval flow shared by every model file-write tool. + /// + /// An approved record is consumed only after the widest success report is + /// known to fit, and before the caller writes. A pending record repeats the + /// request, a denied one refuses, and anything else records a new pending + /// request. Lookup filters are the session, workspace, `file_write` + /// operation, exact digest, and current settings revision. + fn write_gate( + &self, + control: &mut ControlStore, + call: &ToolCall, + request: WriteRequest, + max_result_bytes: usize, + ) -> WriteGate { + let WriteRequest { + digest, + summary, + widest_success, + noun, + } = request; let now = now_unix(); let revision = control.revision(); let statuses = control.approval_statuses(now); @@ -1569,17 +1984,10 @@ impl WorkspaceToolDispatcher<'_> { if let Some(approved) = matching(ApprovalState::Approved) { // Check that the success report fits before anything is spent or // written, using the widest size, digest, and identifier it can - // carry. Otherwise an applied edit could only be reported as a + // carry. Otherwise an applied change could only be reported as a // failure. The approval stays unconsumed for a later retry. - let widest = edit_success_message( - call, - &normalized, - u64::MAX, - &"f".repeat(64), - Some(Uuid::max()), - ); - if widest.len() > max_result_bytes.min(MAX_MESSAGE_BYTES) { - return ToolExecution::recoverable("output_too_large", call); + if widest_success.len() > max_result_bytes.min(MAX_MESSAGE_BYTES) { + return WriteGate::Respond(ToolExecution::recoverable("output_too_large", call)); } let approval = ApprovalBinding { session_id: self.session_id, @@ -1588,51 +1996,34 @@ impl WorkspaceToolDispatcher<'_> { request_sha256: digest.clone(), }; if let Err(error) = control.consume_approval(approved.id, &approval, now) { - return status_result( + return WriteGate::Respond(status_result( call, ToolOutcome::Failed, "approval_failed", json!({"error": error.to_string()}), max_result_bytes, - ); + )); } // The record is spent before the write, so no crash or retry can - // reuse it. The apply rechecks live read/write, the binding, the - // qualified filesystem, and that the target still has the exact - // captured size and digest. - let applied = FileBroker::new(&mut control).apply_replacement( - &binding, - &target, - ¤t_artifact, - proposed.as_bytes(), - ); - drop(control); - return match applied { - // The widest report was checked above, so this one fits. - Ok(receipt) if receipt.applied => ToolExecution::success(edit_success_message( - call, - &normalized, - receipt.proposed.bytes, - &receipt.proposed.sha256, - receipt.operation_id, - )), - // Different proposed bytes always publish or fail; an - // unapplied receipt can only mean the bytes were identical. - Ok(_) => ToolExecution::recoverable("edit_unchanged", call), - Err(error) => classify_edit_error(call, error), - }; + // reuse it. + return WriteGate::Approved; } if let Some(pending) = matching(ApprovalState::Pending) { - return edit_approval_required(call, pending.id, max_result_bytes); + return WriteGate::Respond(file_write_approval_required( + call, + pending.id, + noun, + max_result_bytes, + )); } if matching(ApprovalState::Denied).is_some() { - return status_result( + return WriteGate::Respond(status_result( call, ToolOutcome::Denied, "approval_denied", - json!({"message": "The operator denied this edit. Do not retry it."}), + json!({"message": format!("The operator denied this {noun}. Do not retry it.")}), max_result_bytes, - ); + )); } let status = match control.request_approval( &ApprovalDeclaration { @@ -1640,24 +2031,28 @@ impl WorkspaceToolDispatcher<'_> { workspace_id: self.workspace_id, operation: ApprovalOperation::FileWrite, request_sha256: digest, - summary: edit_summary(&normalized, ¤t_artifact, &proposed_artifact), + summary, ttl_secs: EDIT_APPROVAL_TTL_SECS, }, now, ) { Ok(status) => status, Err(error) => { - return status_result( + return WriteGate::Respond(status_result( call, ToolOutcome::Failed, "approval_failed", json!({"error": error.to_string()}), max_result_bytes, - ); + )); } }; - drop(control); - edit_approval_required(call, status.id, max_result_bytes) + WriteGate::Respond(file_write_approval_required( + call, + status.id, + noun, + max_result_bytes, + )) } /// Request or execute one command. The control store is dropped before the @@ -1980,6 +2375,53 @@ fn replace_once(text: &str, start: usize, length: usize, replacement: &str) -> O Some(proposed) } +/// What the shared approval gate needs to know about one file-write request. +struct WriteRequest { + /// The tool-specific request digest the approval is bound to. + digest: String, + /// The bounded operator-facing summary recorded with a new request. + summary: String, + /// The success report with the widest size, digest, and identifier. + widest_success: String, + /// The noun used in model-facing approval messages. + noun: &'static str, +} + +/// The result of the shared approval gate. +enum WriteGate { + /// An approved record was consumed; the caller may write. + Approved, + /// The caller must return this result and write nothing. + Respond(ToolExecution), +} + +/// A recoverable refusal from multi-edit preparation. `detail` names an edit +/// index or count and never holds file or model text. +struct EditRefusal { + code: &'static str, + detail: Option, +} + +impl EditRefusal { + const fn plain(code: &'static str) -> Self { + Self { code, detail: None } + } + + fn at(code: &'static str, detail: String) -> Self { + Self { + code, + detail: Some(detail), + } + } + + fn into_execution(self, call: &ToolCall) -> ToolExecution { + match self.detail { + Some(detail) => ToolExecution::recoverable_detail(self.code, call, &detail), + None => ToolExecution::recoverable(self.code, call), + } + } +} + /// One exact edit prepared against the live bytes of its target file. struct PreparedEdit { /// The `/`-joined path used in the digest and the summary. @@ -2049,6 +2491,100 @@ fn prepare_edit( }) } +/// Turn parsed multi-edit arguments and the live current bytes into one +/// proposed buffer. +/// +/// Every `old_text` is matched against the original content and must occur +/// exactly once there. Matches may not overlap, so no replacement can see the +/// output of another; touching matches are allowed. The replacements apply in +/// ascending offset order. Refusal details name 1-based edit indexes only. +fn prepare_multi_edit( + path: &str, + edits: &[TextEdit], + current: &[u8], +) -> Result { + let text = std::str::from_utf8(current).map_err(|_| EditRefusal::plain("non_utf8"))?; + // (start, end, index of the edit) + let mut ranges: Vec<(usize, usize, usize)> = Vec::with_capacity(edits.len()); + for (index, edit) in edits.iter().enumerate() { + let number = index.saturating_add(1); + match unique_match(text, &edit.old_text) { + TextMatch::None => { + return Err(EditRefusal::at( + "edit_no_match", + format!("old_text of edit {number} does not occur in the file"), + )); + } + TextMatch::Many => { + return Err(EditRefusal::at( + "edit_ambiguous", + format!("old_text of edit {number} occurs more than once in the file"), + )); + } + TextMatch::Once(start) => { + let end = start + .checked_add(edit.old_text.len()) + .ok_or_else(|| EditRefusal::plain("edit_too_large"))?; + ranges.push((start, end, index)); + } + } + } + ranges.sort_by_key(|&(start, _, index)| (start, index)); + for pair in ranges.windows(2) { + let (_, first_end, first_index) = pair[0]; + let (second_start, _, second_index) = pair[1]; + if second_start < first_end { + return Err(EditRefusal::at( + "edit_overlap", + format!( + "edits {} and {} match overlapping text", + first_index.saturating_add(1), + second_index.saturating_add(1) + ), + )); + } + } + let mut size = text.len(); + for &(start, end, index) in &ranges { + let replacement = edits.get(index).map_or(0, |edit| edit.new_text.len()); + size = size + .checked_sub(end.saturating_sub(start)) + .and_then(|size| size.checked_add(replacement)) + .ok_or_else(|| EditRefusal::plain("edit_too_large"))?; + } + if size > MAX_EDIT_FILE_BYTES { + return Err(EditRefusal::plain("edit_too_large")); + } + let mut proposed = String::with_capacity(size); + let mut cursor = 0usize; + for &(start, end, index) in &ranges { + let (Some(kept), Some(edit)) = (text.get(cursor..start), edits.get(index)) else { + return Err(EditRefusal::plain("edit_too_large")); + }; + proposed.push_str(kept); + proposed.push_str(&edit.new_text); + cursor = end; + } + let Some(tail) = text.get(cursor..) else { + return Err(EditRefusal::plain("edit_too_large")); + }; + proposed.push_str(tail); + if proposed == text { + return Err(EditRefusal::plain("edit_unchanged")); + } + let (Some(current_artifact), Some(proposed_artifact)) = + (artifact(current), artifact(proposed.as_bytes())) + else { + return Err(EditRefusal::plain("edit_too_large")); + }; + Ok(PreparedEdit { + normalized: normalized_path(path), + current_artifact, + proposed, + proposed_artifact, + }) +} + /// The labelled report of an applied edit. It never contains file text. fn edit_success_message( call: &ToolCall, @@ -2056,8 +2592,9 @@ fn edit_success_message( bytes: u64, sha256: &str, operation_id: Option, + edits: Option, ) -> String { - let body = json!({ + let mut body = json!({ "tool": call.name.as_str(), "status": "applied", "path": path, @@ -2065,9 +2602,102 @@ fn edit_success_message( "sha256": sha256, "operation_id": operation_id, }); + if let (Some(edits), Some(object)) = (edits, body.as_object_mut()) { + object.insert("edits".to_owned(), json!(edits)); + } labelled_result(call, &body.to_string()) } +/// The labelled report of a created file. It never contains file text. +fn create_success_message( + call: &ToolCall, + path: &str, + bytes: u64, + sha256: &str, + operation_id: Option, +) -> String { + let body = json!({ + "tool": call.name.as_str(), + "status": "created", + "path": path, + "bytes": bytes, + "sha256": sha256, + "operation_id": operation_id, + }); + labelled_result(call, &body.to_string()) +} + +/// Apply a prepared edit through the checkpointed apply and report it. +/// +/// The apply rechecks live read/write, the binding, the qualified filesystem, +/// and that the target still has the exact captured size and digest. The +/// caller has already spent the approval. +fn apply_prepared_edit( + control: &mut ControlStore, + binding: &SessionBinding, + target: &FileTarget, + call: &ToolCall, + prepared: &PreparedEdit, + edits: Option, +) -> ToolExecution { + let applied = FileBroker::new(control).apply_replacement( + binding, + target, + &prepared.current_artifact, + prepared.proposed.as_bytes(), + ); + match applied { + // The widest report was checked by the gate, so this one fits. + Ok(receipt) if receipt.applied => ToolExecution::success(edit_success_message( + call, + &prepared.normalized, + receipt.proposed.bytes, + &receipt.proposed.sha256, + receipt.operation_id, + edits, + )), + // Different proposed bytes always publish or fail; an unapplied + // receipt can only mean the bytes were identical. + Ok(_) => ToolExecution::recoverable("edit_unchanged", call), + Err(error) => classify_edit_error(call, error), + } +} + +/// Refuse a creation whose target exists or whose parent is missing, before +/// any approval is recorded. Returns `None` when creation may be requested. +/// +/// A live audited read must report the path absent, and a one-entry listing +/// of a non-root parent must succeed. Both are audited broker calls under the +/// live `read` permission. +fn create_precheck( + control: &mut ControlStore, + binding: &SessionBinding, + target: &FileTarget, + call: &ToolCall, +) -> Option { + match read_edit_source(control, binding, target) { + Ok(_) => return Some(ToolExecution::recoverable("file_exists", call)), + Err(FileError::Native(native)) if is_missing_entry(&native) => {} + Err(error) => return Some(classify_edit_error(call, error)), + } + let parent = target + .path + .parent() + .filter(|parent| !parent.as_os_str().is_empty())?; + let parent_target = FileTarget { + session_id: target.session_id, + workspace_id: target.workspace_id, + path: parent.to_path_buf(), + }; + match FileBroker::new(control).list(binding, &parent_target, 1) { + Ok(_) => None, + Err(FileError::Native(native)) if is_missing_entry(&native) => { + Some(ToolExecution::recoverable("not_found", call)) + } + Err(error) => Some(classify_edit_error(call, error)), + } +} + /// Describe exact bytes by size and SHA-256. fn artifact(bytes: &[u8]) -> Option { Some(RecordedArtifact { @@ -2099,9 +2729,64 @@ fn edit_digest( path: &str, current: &RecordedArtifact, proposed: &RecordedArtifact, +) -> String { + replacement_digest( + EDIT_DIGEST_DOMAIN, + session_id, + workspace_id, + path, + current, + proposed, + ) +} + +/// Bind one model multi-edit like [`edit_digest`], under its own domain tag so +/// an approval for one tool can never be consumed by the other. +fn multi_edit_digest( + session_id: Uuid, + workspace_id: WorkspaceId, + path: &str, + current: &RecordedArtifact, + proposed: &RecordedArtifact, +) -> String { + replacement_digest( + MULTI_EDIT_DIGEST_DOMAIN, + session_id, + workspace_id, + path, + current, + proposed, + ) +} + +/// Bind one model file creation to the session, workspace, path, and the +/// exact size and SHA-256 of the new content, under its own domain tag. +fn create_digest( + session_id: Uuid, + workspace_id: WorkspaceId, + path: &str, + content: &RecordedArtifact, +) -> String { + let mut bytes = Vec::new(); + push_field(&mut bytes, CREATE_DIGEST_DOMAIN); + push_field(&mut bytes, session_id.as_bytes()); + push_field(&mut bytes, workspace_id.as_uuid().as_bytes()); + push_field(&mut bytes, path.as_bytes()); + push_field(&mut bytes, &content.bytes.to_le_bytes()); + push_field(&mut bytes, content.sha256.as_bytes()); + sha256_hex(&bytes) +} + +fn replacement_digest( + domain: &[u8], + session_id: Uuid, + workspace_id: WorkspaceId, + path: &str, + current: &RecordedArtifact, + proposed: &RecordedArtifact, ) -> String { let mut bytes = Vec::new(); - push_field(&mut bytes, EDIT_DIGEST_DOMAIN); + push_field(&mut bytes, domain); push_field(&mut bytes, session_id.as_bytes()); push_field(&mut bytes, workspace_id.as_uuid().as_bytes()); push_field(&mut bytes, path.as_bytes()); @@ -2118,15 +2803,50 @@ fn edit_digest( /// formatting characters in the path become `?`, and a long path is shortened on a /// character boundary so the summary fits [`MAX_EDIT_SUMMARY_BYTES`]. fn edit_summary(path: &str, current: &RecordedArtifact, proposed: &RecordedArtifact) -> String { - let short = |digest: &str| digest.get(..12).unwrap_or(digest).to_owned(); + let suffix = format!(": {}", size_change(current, proposed)); + bounded_path_summary("edit ", path, &suffix) +} + +/// A bounded multi-edit label: `multi-edit {path} ({n} edits): ...`. It never +/// contains file text. +fn multi_edit_summary( + path: &str, + edits: usize, + current: &RecordedArtifact, + proposed: &RecordedArtifact, +) -> String { + let suffix = format!(" ({edits} edits): {}", size_change(current, proposed)); + bounded_path_summary("multi-edit ", path, &suffix) +} + +/// A bounded creation label: `create {path}: {n} bytes, sha256 {12}`. It never +/// contains file text. +fn create_summary(path: &str, content: &RecordedArtifact) -> String { let suffix = format!( - ": {} -> {} bytes, sha256 {} -> {}", + ": {} bytes, sha256 {}", + content.bytes, + short_digest(&content.sha256) + ); + bounded_path_summary("create ", path, &suffix) +} + +fn short_digest(digest: &str) -> &str { + digest.get(..12).unwrap_or(digest) +} + +fn size_change(current: &RecordedArtifact, proposed: &RecordedArtifact) -> String { + format!( + "{} -> {} bytes, sha256 {} -> {}", current.bytes, proposed.bytes, - short(¤t.sha256), - short(&proposed.sha256) - ); - let prefix = "edit "; + short_digest(¤t.sha256), + short_digest(&proposed.sha256) + ) +} + +/// Join a fixed prefix, a sanitized and shortened path, and a fixed suffix so +/// the whole summary fits [`MAX_EDIT_SUMMARY_BYTES`]. +fn bounded_path_summary(prefix: &str, path: &str, suffix: &str) -> String { let budget = MAX_EDIT_SUMMARY_BYTES .saturating_sub(prefix.len()) .saturating_sub(suffix.len()); @@ -2189,9 +2909,10 @@ const fn is_hidden_format(character: char) -> bool { } /// Tell the model that an operator must approve the edit before a retry. -fn edit_approval_required( +fn file_write_approval_required( call: &ToolCall, approval_id: Uuid, + noun: &str, max_result_bytes: usize, ) -> ToolExecution { status_result( @@ -2200,7 +2921,7 @@ fn edit_approval_required( "approval_required", json!({ "approval_id": approval_id, - "message": "Nothing changed. The operator must approve this edit. Stop now and retry the identical call in a later turn after approval.", + "message": format!("Nothing changed. The operator must approve this {noun}. Stop now and retry the identical call in a later turn after approval."), }), max_result_bytes, ) @@ -2356,6 +3077,11 @@ fn classify_file_error(call: &ToolCall, error: FileError) -> ToolExecution { ToolExecution::recoverable("invalid_request", call) } FileError::Conflict => ToolExecution::recoverable("conflict", call), + // Windows reports an absent name as a native status inside an + // `ErrorKind::Other` error; it is the same recoverable result. + FileError::Native(native) if is_missing_entry(&native) => { + ToolExecution::recoverable("not_found", call) + } FileError::Native(native) => match native.kind() { io::ErrorKind::NotFound => ToolExecution::recoverable("not_found", call), io::ErrorKind::InvalidInput | io::ErrorKind::InvalidData => { @@ -2424,7 +3150,8 @@ fn validate_relative_path(path: &str, allow_empty: bool) -> Result<(), ToolCallE #[cfg(test)] mod summary_tests { use super::{ - MAX_EDIT_SUMMARY_BYTES, TextMatch, artifact, command_summary, edit_summary, + MAX_EDIT_SUMMARY_BYTES, TextMatch, artifact, command_summary, create_digest, + create_summary, edit_digest, edit_summary, multi_edit_digest, multi_edit_summary, normalized_path, replace_once, unique_match, }; @@ -2452,6 +3179,55 @@ mod summary_tests { assert!(summary.contains("...: 19 -> 20 bytes, sha256 ")); } + #[test] + fn multi_edit_and_create_summaries_are_bounded_and_hold_no_text() { + let current = artifact(b"secret current text").expect("current"); + let proposed = artifact(b"secret proposed text").expect("proposed"); + let summary = multi_edit_summary("src/main.rs", 3, ¤t, &proposed); + assert_eq!( + summary, + format!( + "multi-edit src/main.rs (3 edits): 19 -> 20 bytes, sha256 {} -> {}", + ¤t.sha256[..12], + &proposed.sha256[..12] + ) + ); + let created = create_summary("src/new.rs", ¤t); + assert_eq!( + created, + format!( + "create src/new.rs: 19 bytes, sha256 {}", + ¤t.sha256[..12] + ) + ); + assert!(!summary.contains("secret") && !created.contains("secret")); + + let hostile = format!("{}\u{202e}\u{7}\u{2028}{}", "d/".repeat(40), "é".repeat(80)); + for summary in [ + multi_edit_summary(&hostile, 16, ¤t, &proposed), + create_summary(&hostile, ¤t), + ] { + assert!(summary.len() <= MAX_EDIT_SUMMARY_BYTES, "{}", summary.len()); + assert!(!summary.chars().any(char::is_control)); + assert!(!summary.contains('\u{202e}') && !summary.contains('\u{2028}')); + assert!(summary.contains("...")); + } + } + + #[test] + fn the_three_file_write_digests_never_collide() { + let session = uuid::Uuid::from_u128(1); + let workspace = crate::policy::WorkspaceId::fresh(); + let current = artifact(b"alpha").expect("current"); + let proposed = artifact(b"beta").expect("proposed"); + let edit = edit_digest(session, workspace, "a.txt", ¤t, &proposed); + let multi = multi_edit_digest(session, workspace, "a.txt", ¤t, &proposed); + let create = create_digest(session, workspace, "a.txt", &proposed); + assert_ne!(edit, multi); + assert_ne!(edit, create); + assert_ne!(multi, create); + } + #[test] fn overlapping_occurrences_are_ambiguous_and_a_single_one_is_found() { assert!(matches!(unique_match("aaa", "aa"), TextMatch::Many)); diff --git a/crates/omgcode-core/src/turn/review.rs b/crates/omgcode-core/src/turn/review.rs index 60e8163..6f0cba9 100644 --- a/crates/omgcode-core/src/turn/review.rs +++ b/crates/omgcode-core/src/turn/review.rs @@ -10,20 +10,23 @@ use std::collections::HashSet; use std::path::Path; +use omgcode_platform::broker::is_missing_entry; use serde::Serialize; use serde_json::json; use thiserror::Error; use super::{ - PreparedEdit, ToolArguments, ToolCall, ToolName, command_digest, command_run_request, - edit_digest, edit_target, is_hidden_format, prepare_edit, read_edit_source, + PreparedEdit, TextEdit, ToolArguments, ToolCall, ToolName, artifact, command_digest, + command_run_request, create_digest, edit_digest, edit_target, is_hidden_format, + multi_edit_digest, normalized_path, prepare_edit, prepare_multi_edit, read_edit_source, }; use crate::control::{ApprovalOperation, ApprovalStatus, ControlError, ControlStore}; use crate::file_audit::FileAuditError; use crate::files::FileError; use crate::replacement::CheckpointError; -/// Most distinct edit requests evaluated for one review. +/// Most distinct file-write requests (edit, multi-edit, and create together) +/// evaluated for one review. /// /// Each evaluation reads a workspace file through the audited broker and /// writes one audit record, so the work and the audit growth are bounded. @@ -118,17 +121,20 @@ pub enum ReviewError { /// store or unregistered workspace gives `NotFound`, a busy store gives /// [`ReviewError::Busy`], and a settings revision that differs from the /// approval's gives `Stale(SettingsChanged)`. -/// 3. Only candidates whose tool is the operation's tool are considered; with -/// none, the result is `NotFound`. +/// 3. Only candidates whose tool belongs to the operation are considered: the +/// command tool for `Command`, and the edit, multi-edit, and create tools +/// for `FileWrite`; with none, the result is `NotFound`. /// 4. They are tried newest first, each parsed by [`ToolCall::parse`]. A /// candidate that fails to parse is skipped. A command candidate is built -/// into a run request by the dispatcher's own constructor. An edit candidate -/// is read live through the audited broker under the live `read` -/// requirement and prepared by the dispatcher's own edit preparation. At -/// most eight distinct edit candidates (distinct by exact arguments) are -/// evaluated, newest first. A candidate whose preparation fails for a -/// recoverable reason (missing file, no match, ambiguous match, too large, -/// read denied) is not a match. +/// into a run request by the dispatcher's own constructor. A file-write +/// candidate is read live through the audited broker under the live `read` +/// requirement and prepared by the dispatcher's own preparation for its +/// tool, then compared with that tool's own digest. At most eight distinct +/// file-write candidates in total (distinct by tool and exact arguments) +/// are evaluated, newest first. A candidate whose preparation fails for a +/// recoverable reason (missing file, no match, ambiguous match, overlap, +/// too large, read denied) is not a match, and a create candidate whose +/// path now exists is not a match. /// 5. If none reproduces the digest, the result is `Stale(NoMatchingRequest)`. /// /// The fields of an `Exact` result come from the request that reproduced the @@ -163,9 +169,13 @@ pub fn review_approval( approval: &ApprovalStatus, candidates: &[ReviewCandidate<'_>], ) -> Result { - let tool = match approval.operation { - ApprovalOperation::Command => ToolName::Command, - ApprovalOperation::FileWrite => ToolName::EditFile, + let tools: &[ToolName] = match approval.operation { + ApprovalOperation::Command => &[ToolName::Command], + ApprovalOperation::FileWrite => &[ + ToolName::EditFile, + ToolName::MultiEditFile, + ToolName::CreateFile, + ], ApprovalOperation::GitPush | ApprovalOperation::Network => { return Ok(ApprovalReview::NotFound); } @@ -185,17 +195,17 @@ pub fn review_approval( let mut relevant = candidates .iter() .rev() - .filter(|candidate| candidate.tool == tool.as_str()) + .filter(|candidate| tools.iter().any(|tool| candidate.tool == tool.as_str())) .peekable(); if relevant.peek().is_none() { return Ok(ApprovalReview::NotFound); } - let found = match tool { - ToolName::Command => { + let found = match approval.operation { + ApprovalOperation::Command => { drop(control); review_command(approval, relevant) } - _ => review_edit(control, approval, relevant)?, + _ => review_file_write(control, approval, relevant)?, }; Ok(found.map_or( ApprovalReview::Stale(StaleReason::NoMatchingRequest), @@ -253,10 +263,11 @@ fn review_command<'a>( None } -/// Find the newest edit candidate that reproduces the approval's digest. +/// Find the newest file-write candidate that reproduces the approval's digest. /// -/// Evaluates at most [`MAX_REVIEW_EDIT_CANDIDATES`] distinct candidates. -fn review_edit<'a>( +/// Evaluates at most [`MAX_REVIEW_EDIT_CANDIDATES`] distinct candidates across +/// the edit, multi-edit, and create tools. +fn review_file_write<'a>( mut control: ControlStore, approval: &ApprovalStatus, candidates: impl Iterator>, @@ -270,52 +281,102 @@ fn review_edit<'a>( let mut seen = HashSet::new(); let mut evaluated = 0usize; for candidate in candidates { - let Some(call) = parse_candidate(candidate, ToolName::EditFile) else { + let Some(tool) = file_write_tool(candidate.tool) else { continue; }; - let ToolArguments::EditFile { - path, - old_text, - new_text, - } = &call.arguments - else { + let Some(call) = parse_candidate(candidate, tool) else { continue; }; - if !seen.insert(candidate.arguments) { + if !seen.insert((candidate.tool, candidate.arguments)) { continue; } if evaluated == MAX_REVIEW_EDIT_CANDIDATES { break; } evaluated += 1; + let path = call.path(); let target = edit_target(approval.session_id, approval.workspace_id, path); let current = match read_edit_source(&mut control, &binding, &target) { - Ok(bytes) => bytes, + Ok(bytes) => Some(bytes), + // A creation is reviewable only while its path is absent. + Err(FileError::Native(native)) + if tool == ToolName::CreateFile && is_missing_entry(&native) => + { + None + } Err(error) => { classify_review_read(error)?; continue; } }; - let Ok(prepared) = prepare_edit(path, old_text, new_text, ¤t) else { - continue; + let reviewed = match (&call.arguments, current) { + ( + ToolArguments::EditFile { + old_text, new_text, .. + }, + Some(current), + ) => prepare_edit(path, old_text, new_text, ¤t) + .ok() + .filter(|prepared| { + edit_digest( + approval.session_id, + approval.workspace_id, + &prepared.normalized, + &prepared.current_artifact, + &prepared.proposed_artifact, + ) == approval.request_sha256 + }) + .map(|prepared| edit_fields(&prepared, old_text, new_text)), + (ToolArguments::MultiEditFile { edits, .. }, Some(current)) => { + prepare_multi_edit(path, edits, ¤t) + .ok() + .filter(|prepared| { + multi_edit_digest( + approval.session_id, + approval.workspace_id, + &prepared.normalized, + &prepared.current_artifact, + &prepared.proposed_artifact, + ) == approval.request_sha256 + }) + .map(|prepared| multi_edit_fields(&prepared, edits)) + } + (ToolArguments::CreateFile { content, .. }, None) => { + let normalized = normalized_path(path); + artifact(content.as_bytes()) + .filter(|content_artifact| { + create_digest( + approval.session_id, + approval.workspace_id, + &normalized, + content_artifact, + ) == approval.request_sha256 + }) + .map(|content_artifact| create_fields(&normalized, content, &content_artifact)) + } + _ => None, }; - let digest = edit_digest( - approval.session_id, - approval.workspace_id, - &prepared.normalized, - &prepared.current_artifact, - &prepared.proposed_artifact, - ); - if digest == approval.request_sha256 { + if let Some(fields) = reviewed { return Ok(Some(ReviewedRequest { - tool: ToolName::EditFile.as_str().to_owned(), - fields: edit_fields(&prepared, old_text, new_text), + tool: tool.as_str().to_owned(), + fields, })); } } Ok(None) } +/// Map a recorded tool name to a file-write tool. +fn file_write_tool(name: &str) -> Option { + [ + ToolName::EditFile, + ToolName::MultiEditFile, + ToolName::CreateFile, + ] + .into_iter() + .find(|tool| tool.as_str() == name) +} + /// Decide whether a failed review read is a plain non-match (`Ok`) or leaves /// the review unable to continue (`Err`). /// @@ -445,6 +506,83 @@ fn edit_fields(prepared: &PreparedEdit, old_text: &str, new_text: &str) -> Vec Vec { + let mut fields = vec![ + single("File", display_safe_line(&prepared.normalized)), + single("Edits", edits.len().to_string()), + ]; + for (edit, (old_label, new_label)) in edits.iter().zip(EDIT_LABELS) { + fields.push(ReviewField { + label: old_label, + value: display_safe(&edit.old_text), + multiline: true, + }); + fields.push(ReviewField { + label: new_label, + value: display_safe(&edit.new_text), + multiline: true, + }); + } + fields.push(single( + "Size", + format!( + "{} -> {} bytes", + prepared.current_artifact.bytes, prepared.proposed_artifact.bytes + ), + )); + fields.push(single( + "Current SHA-256", + prepared.current_artifact.sha256.clone(), + )); + fields.push(single( + "Proposed SHA-256", + prepared.proposed_artifact.sha256.clone(), + )); + fields +} + +/// Create fields. The path and content come from the parsed arguments that +/// reproduced the digest. +fn create_fields( + normalized: &str, + content: &str, + content_artifact: &crate::replacement::RecordedArtifact, +) -> Vec { + vec![ + single("File", display_safe_line(normalized)), + single("Size", format!("{} bytes", content_artifact.bytes)), + single("SHA-256", content_artifact.sha256.clone()), + ReviewField { + label: "Content", + value: display_safe(content), + multiline: true, + }, + ] +} + fn single(label: &'static str, value: String) -> ReviewField { ReviewField { label, diff --git a/crates/omgcode-core/tests/approval_review.rs b/crates/omgcode-core/tests/approval_review.rs index 26a3f0e..4f299d4 100644 --- a/crates/omgcode-core/tests/approval_review.rs +++ b/crates/omgcode-core/tests/approval_review.rs @@ -803,3 +803,308 @@ fn hostile_edit_text_is_escaped_and_a_literal_escape_stays_distinguishable() { "ok\\u{2028}\\u{feff}\\u{000d}\\u{5c}u{200b}" ); } + +// ------------------------------------------------- multi-edit and create + +const MULTI_EDIT_TOOL: &str = "workspace_multi_edit_file"; +const CREATE_TOOL: &str = "workspace_create_file"; + +fn multi_call(id: &str, path: &str, edits: &[(&str, &str)]) -> Value { + let edits: Vec = edits + .iter() + .map(|(old, new)| json!({"old_text": old, "new_text": new})) + .collect(); + call(id, MULTI_EDIT_TOOL, json!({"path": path, "edits": edits})) +} + +fn create_call(id: &str, path: &str, content: &str) -> Value { + call(id, CREATE_TOOL, json!({"path": path, "content": content})) +} + +fn approve(fixture: &Fixture, approval: &ApprovalStatus) { + ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store") + .approve_with_writers_stopped(approval.id, now_unix()) + .expect("approve"); +} + +#[test] +fn a_multi_edit_approval_reviews_exact_fields_without_touching_the_file() { + let mut fixture = Fixture::new(); + let target = fixture.write_file("notes.txt", "alpha beta gamma\n"); + fixture.turn(vec![multi_call( + "multi-1", + "notes.txt", + &[("gamma", "G"), ("alpha", "A")], + )]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let before = statuses_json(&fixture.approvals()); + let reads = fixture.audit_reads(); + + let request = exact(fixture.review(&approval, &recorded)); + + assert_eq!(request.tool, MULTI_EDIT_TOOL); + let values: Vec<(&str, String, bool)> = request + .fields + .iter() + .map(|field| (field.label, field.value.clone(), field.multiline)) + .collect(); + assert_eq!( + values, + [ + ("File", "notes.txt".to_owned(), false), + ("Edits", "2".to_owned(), false), + ("Old text 1", "gamma".to_owned(), true), + ("New text 1", "G".to_owned(), true), + ("Old text 2", "alpha".to_owned(), true), + ("New text 2", "A".to_owned(), true), + ("Size", "17 -> 9 bytes".to_owned(), false), + ("Current SHA-256", sha256(b"alpha beta gamma\n"), false), + ("Proposed SHA-256", sha256(b"A beta G\n"), false), + ] + ); + assert_eq!(fs::read(&target).expect("read"), b"alpha beta gamma\n"); + assert_eq!(fixture.checkpoint_operations(), 0); + assert_eq!(statuses_json(&fixture.approvals()), before); + assert_eq!(fixture.audit_reads(), reads + 1, "one audited read"); +} + +/// After an exact review and an approval, the identical multi-edit applies: +/// the review digest is the dispatch digest. +#[test] +fn an_exactly_reviewed_multi_edit_is_the_call_that_applies() { + let mut fixture = Fixture::new(); + let target = fixture.write_file("notes.txt", "alpha beta\n"); + let edits = [("alpha", "A"), ("beta", "B")]; + fixture.turn(vec![multi_call("multi-1", "notes.txt", &edits)]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + approve(&fixture, &approval); + fixture.turn(vec![multi_call("multi-2", "notes.txt", &edits)]); + + assert_eq!(fs::read_to_string(&target).expect("read"), "A B\n"); + assert_eq!(fixture.state(approval.id), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 1); +} + +#[test] +fn a_multi_edit_review_is_stale_after_the_file_changes() { + let mut fixture = Fixture::new(); + let target = fixture.write_file("notes.txt", "one two"); + fixture.turn(vec![multi_call( + "multi-1", + "notes.txt", + &[("one", "1"), ("two", "2")], + )]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + fs::write(&target, "one two!").expect("change the file after the request"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + // A change that breaks one match is also not a match. + fs::write(&target, "one three").expect("remove a match"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); +} + +#[test] +fn a_create_approval_reviews_exact_fields_and_is_stale_once_the_file_exists() { + let mut fixture = Fixture::new(); + fixture.turn(vec![create_call("create-1", "new.txt", "hello\nworld\n")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let before = statuses_json(&fixture.approvals()); + + let request = exact(fixture.review(&approval, &recorded)); + + assert_eq!(request.tool, CREATE_TOOL); + let values: Vec<(&str, String, bool)> = request + .fields + .iter() + .map(|field| (field.label, field.value.clone(), field.multiline)) + .collect(); + assert_eq!( + values, + [ + ("File", "new.txt".to_owned(), false), + ("Size", "12 bytes".to_owned(), false), + ("SHA-256", sha256(b"hello\nworld\n"), false), + ("Content", "hello\nworld\n".to_owned(), true), + ] + ); + assert!(!fixture.workspace.join("new.txt").exists()); + assert_eq!(statuses_json(&fixture.approvals()), before); + + // Another writer creates the file: the request can no longer proceed. + fixture.write_file("new.txt", "other"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + assert_eq!( + fs::read(fixture.workspace.join("new.txt")).expect("read"), + b"other" + ); +} + +/// After an exact review and an approval, the identical call creates the file. +#[test] +fn an_exactly_reviewed_create_is_the_call_that_creates() { + let mut fixture = Fixture::new(); + fixture.turn(vec![create_call("create-1", "new.txt", "content")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + approve(&fixture, &approval); + fixture.turn(vec![create_call("create-2", "new.txt", "content")]); + + assert_eq!( + fs::read_to_string(fixture.workspace.join("new.txt")).expect("read"), + "content" + ); + assert_eq!(fixture.state(approval.id), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 0); +} + +/// A review matches only the tool whose digest the approval holds: an edit +/// approval is not shown as a multi-edit, and the reverse. +#[test] +fn a_review_never_matches_another_tools_candidate() { + let mut fixture = Fixture::new(); + fixture.write_file("notes.txt", "alpha beta\n"); + fixture.turn(vec![edit_call("edit-1", "notes.txt", "beta", "gamma")]); + let edit_approval = fixture.pending(ApprovalOperation::FileWrite); + let edit_recorded = fixture.recorded(); + + // Only an equivalent multi-edit is offered for the edit approval. + let equivalent = vec![RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: "multi-1".into(), + tool: MULTI_EDIT_TOOL.to_owned(), + arguments: + json!({"path": "notes.txt", "edits": [{"old_text": "beta", "new_text": "gamma"}]}) + .to_string(), + }]; + assert_eq!( + fixture.review(&edit_approval, &equivalent).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + // The real edit candidate still matches, even with the multi-edit present. + let mut both = equivalent; + both.splice(0..0, edit_recorded); + let request = exact(fixture.review(&edit_approval, &both)); + assert_eq!(request.tool, EDIT_TOOL); + + // A create candidate never matches a replacement approval either. + let creates = vec![RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: "create-1".into(), + tool: CREATE_TOOL.to_owned(), + arguments: json!({"path": "fresh.txt", "content": "gamma"}).to_string(), + }]; + assert_eq!( + fixture.review(&edit_approval, &creates).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); +} + +/// The eight-candidate bound counts distinct requests across all three +/// file-write tools together, newest first. +#[test] +fn the_candidate_bound_spans_all_file_write_tools() { + let mut fixture = Fixture::new(); + fixture.write_file("notes.txt", "alpha bcc\n"); + fixture.turn(vec![create_call("create-1", "new.txt", "x")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let matching = fixture.recorded(); + + let newer = |count: usize| { + let mut all = matching.clone(); + for index in 0..count { + let (tool, arguments) = match index % 3 { + 0 => ( + EDIT_TOOL, + json!({"path": "notes.txt", "old_text": "alpha", "new_text": format!("o{index}")}), + ), + 1 => ( + MULTI_EDIT_TOOL, + json!({"path": "notes.txt", "edits": [{"old_text": "alpha", "new_text": format!("o{index}")}]}), + ), + _ => ( + CREATE_TOOL, + json!({"path": format!("other{index}.txt"), "content": "y"}), + ), + }; + all.push(RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: format!("call-{index}"), + tool: tool.to_owned(), + arguments: arguments.to_string(), + }); + } + all + }; + + // Seven newer candidates: the matching one is the eighth and is found. + let reads = fixture.audit_reads(); + exact(fixture.review(&approval, &newer(7))); + assert_eq!(fixture.audit_reads() - reads, 8); + + // Eight newer candidates push it beyond the bound. + let reads = fixture.audit_reads(); + assert_eq!( + fixture.review(&approval, &newer(8)).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + assert_eq!(fixture.audit_reads() - reads, 8); +} + +#[test] +fn hostile_multi_edit_and_create_text_is_escaped() { + let mut fixture = Fixture::new(); + let contents = "a\u{202e}b \\u{202e} c\n"; + fixture.write_file("odd.txt", contents); + fixture.turn(vec![multi_call( + "multi-1", + "odd.txt", + &[("a\u{202e}b", "x\u{2028}y\r"), ("\\u{202e}", "\\u{200b}")], + )]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let request = exact(fixture.review(&approval, &recorded)); + for field in &request.fields { + assert_display_clean(&field.value); + } + assert_eq!(field(&request, "Old text 1").value, "a\\u{202e}b"); + assert_eq!(field(&request, "New text 1").value, "x\\u{2028}y\\u{000d}"); + assert_eq!(field(&request, "Old text 2").value, "\\u{5c}u{202e}"); + assert_eq!(field(&request, "New text 2").value, "\\u{5c}u{200b}"); + + let mut fixture = Fixture::new(); + fixture.turn(vec![create_call( + "create-1", + "new.txt", + "ok\u{202e}\u{feff}\r\\u{200b}\0", + )]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let request = exact(fixture.review(&approval, &recorded)); + for field in &request.fields { + assert_display_clean(&field.value); + } + assert_eq!( + field(&request, "Content").value, + "ok\\u{202e}\\u{feff}\\u{000d}\\u{5c}u{200b}\\u{0000}" + ); +} diff --git a/crates/omgcode-core/tests/turn_runtime.rs b/crates/omgcode-core/tests/turn_runtime.rs index 28d9f26..6e214d0 100644 --- a/crates/omgcode-core/tests/turn_runtime.rs +++ b/crates/omgcode-core/tests/turn_runtime.rs @@ -549,7 +549,9 @@ fn allowed_read_and_list_produce_durable_source_labelled_results() { Some(vec![ "workspace_read_file".to_owned(), "workspace_list_directory".to_owned(), - "workspace_edit_file".to_owned() + "workspace_edit_file".to_owned(), + "workspace_multi_edit_file".to_owned(), + "workspace_create_file".to_owned() ]) ); @@ -594,7 +596,9 @@ fn allowed_read_and_list_produce_durable_source_labelled_results() { vec![ "workspace_read_file", "workspace_list_directory", - "workspace_edit_file" + "workspace_edit_file", + "workspace_multi_edit_file", + "workspace_create_file" ] ); @@ -661,7 +665,9 @@ fn allowed_read_and_list_produce_durable_source_labelled_results() { json!([ "workspace_read_file", "workspace_list_directory", - "workspace_edit_file" + "workspace_edit_file", + "workspace_multi_edit_file", + "workspace_create_file" ]) ); } @@ -1508,7 +1514,9 @@ fn runtime_owns_custom_instruction_and_tool_manifest_fields() { Some(vec![ "workspace_read_file".to_owned(), "workspace_list_directory".to_owned(), - "workspace_edit_file".to_owned() + "workspace_edit_file".to_owned(), + "workspace_multi_edit_file".to_owned(), + "workspace_create_file".to_owned() ]) ); fixture.close_journal(); @@ -2424,10 +2432,9 @@ fn a_pending_checkpoint_operation_is_a_recoverable_edit_result() { ); } -/// A session whose earlier turn advertised tool schema 0.2.0 still replays and -/// accepts a new turn, which records the current schema version. -#[test] -fn a_tool_schema_0_2_0_session_replays_and_accepts_a_new_turn() { +/// A session whose earlier turn advertised an older tool schema still replays +/// and accepts a new turn, which records the current schema version. +fn an_older_schema_session_replays_and_accepts_a_new_turn(version: &str, tools: &[&str]) { let mut fixture = Fixture::new(); { let journal = fixture.journal.as_mut().expect("journal is open"); @@ -2439,16 +2446,12 @@ fn a_tool_schema_0_2_0_session_replays_and_accepts_a_new_turn() { .record_manifest( request_id, RequestManifest { - tool_schema_version: Some("0.2.0".into()), - tools: Some(vec![ - "workspace_read_file".into(), - "workspace_list_directory".into(), - "workspace_run_command".into(), - ]), + tool_schema_version: Some(version.into()), + tools: Some(tools.iter().map(|name| (*name).to_owned()).collect()), ..manifest() }, ) - .expect("schema 0.2.0 manifest"); + .expect("older schema manifest"); journal .record_message(MessageRole::Assistant, "earlier reply") .expect("assistant message"); @@ -2460,7 +2463,7 @@ fn a_tool_schema_0_2_0_session_replays_and_accepts_a_new_turn() { let mut model = ScriptedModel::new(vec![text_reply("current reply")]); fixture .run(&mut model, TurnLimits::default()) - .expect("a new turn after a 0.2.0 turn"); + .expect("a new turn after an older schema turn"); assert!(request_contains(&model.requests[0], "earlier reply")); fixture.close_journal(); let versions: Vec = fixture @@ -2469,6 +2472,714 @@ fn a_tool_schema_0_2_0_session_replays_and_accepts_a_new_turn() { .filter(|event| event["type"] == "request.manifest") .map(|event| event["payload"]["manifest"]["tool_schema_version"].clone()) .collect(); - assert_eq!(versions, [json!("0.2.0"), json!(TOOL_SCHEMA_VERSION)]); - assert_eq!(TOOL_SCHEMA_VERSION, "0.3.0"); + assert_eq!(versions, [json!(version), json!(TOOL_SCHEMA_VERSION)]); + assert_eq!(TOOL_SCHEMA_VERSION, "0.4.0"); +} + +/// A session whose earlier turn advertised tool schema 0.2.0 still replays and +/// accepts a new turn, which records the current schema version. +#[test] +fn a_tool_schema_0_2_0_session_replays_and_accepts_a_new_turn() { + an_older_schema_session_replays_and_accepts_a_new_turn( + "0.2.0", + &[ + "workspace_read_file", + "workspace_list_directory", + "workspace_run_command", + ], + ); +} + +/// The same holds for a session recorded under schema 0.3.0, which offered the +/// single exact-edit tool but not multi-edit or create. +#[test] +fn a_tool_schema_0_3_0_session_replays_and_accepts_a_new_turn() { + an_older_schema_session_replays_and_accepts_a_new_turn( + "0.3.0", + &[ + "workspace_read_file", + "workspace_list_directory", + "workspace_edit_file", + "workspace_run_command", + ], + ); +} + +// --------------------------------------------------------------------------- +// Multi-edit and create tools (ADR-0054) +// --------------------------------------------------------------------------- + +fn multi_call(id: &str, path: &str, edits: &[(&str, &str)]) -> Value { + let edits: Vec = edits + .iter() + .map(|(old, new)| json!({"old_text": old, "new_text": new})) + .collect(); + call( + id, + "workspace_multi_edit_file", + json!({"path": path, "edits": edits}), + ) +} + +fn create_call(id: &str, path: &str, content: &str) -> Value { + call( + id, + "workspace_create_file", + json!({"path": path, "content": content}), + ) +} + +impl Fixture { + /// The text of every recorded tool message, read back from the journal file. + fn tool_messages_after_close(&self) -> Vec { + SessionJournal::open(&self.journal_path) + .expect("replay") + .snapshot() + .messages + .iter() + .filter(|message| message.role == MessageRole::Tool) + .map(|message| message.text.clone()) + .collect() + } +} + +#[test] +fn multi_edit_tool_arguments_are_strictly_validated() { + let parsed = ToolCall::parse(&multi_call( + "m-1", + "src/lib.rs", + &[("old", ""), ("two", "2")], + )) + .expect("valid multi-edit"); + assert_eq!(parsed.path(), "src/lib.rs"); + assert!(matches!( + parsed.arguments, + ToolArguments::MultiEditFile { ref edits, .. } if edits.len() == 2 + )); + + let sixteen: Vec = (0..16) + .map(|index| json!({"old_text": format!("o{index}"), "new_text": "n"})) + .collect(); + assert!( + ToolCall::parse(&call( + "m-1", + "workspace_multi_edit_file", + json!({"path": "a.txt", "edits": sixteen}) + )) + .is_ok(), + "sixteen edits are the accepted maximum" + ); + let seventeen: Vec = (0..17) + .map(|index| json!({"old_text": format!("o{index}"), "new_text": "n"})) + .collect(); + for arguments in [ + json!({"path": "a.txt", "edits": []}), + json!({"path": "a.txt", "edits": seventeen}), + json!({"path": "a.txt"}), + json!({"edits": [{"old_text": "x", "new_text": "y"}]}), + json!({"path": "a.txt", "edits": [{"old_text": "", "new_text": "y"}]}), + json!({"path": "a.txt", "edits": [{"old_text": "x"}]}), + json!({"path": "a.txt", "edits": [{"new_text": "y"}]}), + json!({"path": "a.txt", "edits": [{"old_text": "x", "new_text": "y", "extra": "z"}]}), + json!({"path": "a.txt", "edits": [{"old_text": 1, "new_text": "y"}]}), + json!({"path": "a.txt", "edits": ["x"]}), + json!({"path": "a.txt", "edits": {"old_text": "x", "new_text": "y"}}), + json!({"path": 7, "edits": [{"old_text": "x", "new_text": "y"}]}), + json!({"path": "a.txt", "edits": [{"old_text": "x", "new_text": "y"}], "mode": "all"}), + ] { + assert!( + matches!( + ToolCall::parse(&call("m-1", "workspace_multi_edit_file", arguments.clone())), + Err(ToolCallError::InvalidArguments) + ), + "{arguments} was accepted" + ); + } + assert!(matches!( + ToolCall::parse(&multi_call("m-1", "../outside.txt", &[("x", "y")])), + Err(ToolCallError::InvalidPath) + )); + // Duplicate keys at either level are refused before typed parsing. + for raw in [ + r#"{"path":"a.txt","path":"b.txt","edits":[{"old_text":"x","new_text":"y"}]}"#, + r#"{"path":"a.txt","edits":[{"old_text":"x","new_text":"y","new_text":"z"}]}"#, + r#"{"path":"a.txt","edits":[{"old_text":"x","new_text":"y"}],"edits":[]}"#, + ] { + assert!(matches!( + ToolCall::parse(&raw_call("m-1", "workspace_multi_edit_file", raw)), + Err(ToolCallError::InvalidArguments) + )); + } + // The whole arguments stay within the 4 KiB bound. + let large = "x".repeat(4 * 1024); + assert!(matches!( + ToolCall::parse(&multi_call("m-1", "a.txt", &[(&large, "y")])), + Err(ToolCallError::InvalidArguments) + )); +} + +#[test] +fn create_tool_arguments_are_strictly_validated() { + let parsed = ToolCall::parse(&create_call("c-1", "src/new.rs", "")).expect("empty content"); + assert_eq!( + parsed.arguments, + ToolArguments::CreateFile { + path: "src/new.rs".into(), + content: String::new(), + } + ); + for arguments in [ + json!({"path": "a.txt"}), + json!({"content": "x"}), + json!({"path": "a.txt", "content": 1}), + json!({"path": 1, "content": "x"}), + json!({"path": "a.txt", "content": "x", "mode": "0600"}), + ] { + assert!( + matches!( + ToolCall::parse(&call("c-1", "workspace_create_file", arguments.clone())), + Err(ToolCallError::InvalidArguments) + ), + "{arguments} was accepted" + ); + } + for path in ["", "../outside.txt", "sub/../../x", "./a.txt"] { + assert!( + matches!( + ToolCall::parse(&create_call("c-1", path, "x")), + Err(ToolCallError::InvalidPath) + ), + "{path:?} was accepted" + ); + } + assert!(matches!( + ToolCall::parse(&raw_call( + "c-1", + "workspace_create_file", + r#"{"path":"a.txt","content":"x","content":"y"}"# + )), + Err(ToolCallError::InvalidArguments) + )); + let large = "x".repeat(4 * 1024); + assert!(matches!( + ToolCall::parse(&create_call("c-1", "a.txt", &large)), + Err(ToolCallError::InvalidArguments) + )); +} + +/// Every replacement matches the original text, the replacements apply in +/// offset order whatever order the model lists them in, and one approval and +/// one checkpoint cover the whole call. +#[test] +fn an_approved_multi_edit_applies_once_through_one_checkpoint() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("notes.txt"); + fs::write(&target, "alpha beta gamma\nab\n").expect("write workspace file"); + let session_id = fixture.snapshot().session_id; + // Listed out of offset order on purpose. + let edits = [("gamma", "G"), ("alpha", "A"), ("ab\n", "xy\n")]; + + let (_, model) = fixture.edit_turn(vec![multi_call("m-1", "notes.txt", &edits)]); + assert_eq!( + model.requests[0].tools, + [ + "workspace_read_file", + "workspace_list_directory", + "workspace_edit_file", + "workspace_multi_edit_file", + "workspace_create_file", + ] + ); + assert_eq!( + fs::read_to_string(&target).expect("read"), + "alpha beta gamma\nab\n" + ); + let message = fixture.last_tool_message(); + assert!(message.contains("approval_required"), "{message}"); + assert!(!message.contains("alpha"), "{message}"); + let approvals = fixture.file_write_approvals(); + assert_eq!(approvals.len(), 1); + assert_eq!(approvals[0].session_id, session_id); + assert_eq!(approvals[0].state, ApprovalState::Pending); + assert_eq!( + approvals[0].summary, + format!( + "multi-edit notes.txt (3 edits): 20 -> 12 bytes, sha256 {} -> {}", + &sha256(b"alpha beta gamma\nab\n")[..12], + &sha256(b"A beta G\nxy\n")[..12] + ) + ); + assert!(!approvals[0].summary.contains("alpha")); + assert_eq!(fixture.checkpoint_operations(), 0); + + // A repeat while pending records nothing new. + fixture.edit_turn(vec![multi_call("m-2", "notes.txt", &edits)]); + assert_eq!(fixture.file_write_approvals().len(), 1); + + let approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![multi_call("m-3", "notes.txt", &edits)]); + assert_eq!(fs::read_to_string(&target).expect("read"), "A beta G\nxy\n"); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 1); + let message = fixture.last_tool_message(); + assert!(message.contains("\"status\":\"applied\""), "{message}"); + assert!(message.contains("\"tool\":\"workspace_multi_edit_file\"")); + assert!(message.contains("\"edits\":3"), "{message}"); + assert!(message.contains(&sha256(b"A beta G\nxy\n")), "{message}"); + assert!(!message.contains("beta"), "the result holds no file text"); + + // The consumed record cannot apply a second time: the old texts are gone. + fixture.edit_turn(vec![multi_call("m-4", "notes.txt", &edits)]); + assert_eq!(fs::read_to_string(&target).expect("read"), "A beta G\nxy\n"); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + [ + Some("approval_required"), + Some("approval_required"), + None, + Some("edit_no_match") + ] + .map(|code| code.map(str::to_owned)) + ); +} + +/// Touching matches are allowed: they do not share a byte. +#[test] +fn touching_multi_edit_matches_apply_together() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("pair.txt"); + fs::write(&target, "ab").expect("write workspace file"); + let edits = [("a", "1"), ("b", "2")]; + fixture.edit_turn(vec![multi_call("m-1", "pair.txt", &edits)]); + fixture.approve_pending_edit(); + fixture.edit_turn(vec![multi_call("m-2", "pair.txt", &edits)]); + assert_eq!(fs::read_to_string(&target).expect("read"), "12"); +} + +#[test] +fn a_multi_edit_approval_does_not_cover_a_changed_file_or_another_request() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("notes.txt"); + fs::write(&target, "one two three").expect("write workspace file"); + let edits = [("one", "1"), ("two", "2")]; + fixture.edit_turn(vec![multi_call("m-1", "notes.txt", &edits)]); + let approval = fixture.approve_pending_edit(); + + // A different replacement of the same file records a new request. + fixture.edit_turn(vec![multi_call( + "m-2", + "notes.txt", + &[("one", "1"), ("two", "3")], + )]); + assert_eq!(fs::read_to_string(&target).expect("read"), "one two three"); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert_eq!(fixture.file_write_approvals().len(), 2); + + // A changed file changes the digest. + fs::write(&target, "one two three!").expect("change the file"); + fixture.edit_turn(vec![multi_call("m-3", "notes.txt", &edits)]); + assert_eq!(fs::read_to_string(&target).expect("read"), "one two three!"); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert_eq!(fixture.file_write_approvals().len(), 3); + assert_eq!(fixture.checkpoint_operations(), 0); +} + +/// Each file-write tool has its own digest domain, so an approval recorded for +/// one tool is never consumed by an equivalent call of another. +#[test] +fn an_edit_approval_cannot_be_consumed_by_an_equivalent_multi_edit_and_the_reverse() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("notes.txt"); + fs::write(&target, "alpha beta\n").expect("write workspace file"); + fixture.edit_turn(vec![edit_call("e-1", "notes.txt", "beta", "gamma")]); + let edit_approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![multi_call("m-1", "notes.txt", &[("beta", "gamma")])]); + assert_eq!( + fs::read_to_string(&target).expect("read"), + "alpha beta\n", + "the equivalent multi-edit did not apply" + ); + assert_eq!( + fixture.approval_state(edit_approval), + ApprovalState::Approved + ); + let approvals = fixture.file_write_approvals(); + assert_eq!(approvals.len(), 2); + assert_eq!(approvals[1].state, ApprovalState::Pending); + assert_ne!(approvals[0].request_sha256, approvals[1].request_sha256); + assert!(fixture.last_tool_message().contains("approval_required")); + + // Reverse: an approved multi-edit does not apply the equivalent edit. + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("notes.txt"); + fs::write(&target, "alpha beta\n").expect("write workspace file"); + fixture.edit_turn(vec![multi_call("m-1", "notes.txt", &[("beta", "gamma")])]); + let multi_approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![edit_call("e-1", "notes.txt", "beta", "gamma")]); + assert_eq!(fs::read_to_string(&target).expect("read"), "alpha beta\n"); + assert_eq!( + fixture.approval_state(multi_approval), + ApprovalState::Approved + ); + assert_eq!(fixture.file_write_approvals().len(), 2); +} + +#[test] +fn a_denied_multi_edit_writes_nothing_and_the_turn_continues() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("notes.txt"); + fs::write(&target, "alpha beta\n").expect("write workspace file"); + let edits = [("alpha", "a"), ("beta", "b")]; + fixture.edit_turn(vec![multi_call("m-1", "notes.txt", &edits)]); + let pending = fixture.file_write_approvals()[0].id; + ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store") + .decide_approval(pending, false, now_unix()) + .expect("deny"); + let (report, _) = fixture.edit_turn(vec![multi_call("m-2", "notes.txt", &edits)]); + assert_eq!(report.reply, "reported"); + assert_eq!(fs::read_to_string(&target).expect("read"), "alpha beta\n"); + assert_eq!(fixture.file_write_approvals().len(), 1); + assert_eq!(fixture.checkpoint_operations(), 0); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("approval_denied")); +} + +#[test] +fn multi_edit_refusals_are_recoverable_and_write_nothing() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("notes.txt"), "one two two three\n").expect("notes"); + fs::write(fixture.workspace.join("binary.dat"), [0xff_u8, 0xfe, b'x']).expect("binary"); + let mut near_limit = "a".repeat(MAX_EDIT_FILE_BYTES - 4); + near_limit.push_str("MARK"); + fs::write(fixture.workspace.join("full.txt"), &near_limit).expect("near limit"); + + let mut model = ScriptedModel::new(vec![ + tool_reply(vec![ + // The second edit does not match. + multi_call("m-1", "notes.txt", &[("one", "1"), ("four", "4")]), + // The second edit is ambiguous. + multi_call("m-2", "notes.txt", &[("one", "1"), ("two", "2")]), + // The same text twice shares one range. + multi_call("m-3", "notes.txt", &[("one", "1"), ("one", "uno")]), + // Different but overlapping ranges. + multi_call("m-4", "notes.txt", &[("one two", "x"), ("two two", "y")]), + // Every edit replaces text with itself. + multi_call("m-5", "notes.txt", &[("one", "one"), ("three", "three")]), + ]), + tool_reply(vec![ + multi_call("m-6", "binary.dat", &[("x", "y")]), + multi_call("m-7", "full.txt", &[("MARK", "MARKS")]), + multi_call("m-8", "missing.txt", &[("x", "y")]), + ]), + text_reply("all refused"), + ]); + let report = fixture + .run(&mut model, TurnLimits::default()) + .expect("recoverable refusals keep the turn going"); + assert_eq!(report.reply, "all refused"); + assert!(fixture.file_write_approvals().is_empty()); + assert_eq!( + fs::read_to_string(fixture.workspace.join("notes.txt")).expect("read"), + "one two two three\n" + ); + assert_eq!( + fs::read(fixture.workspace.join("full.txt")).expect("read"), + near_limit.as_bytes() + ); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + [ + "edit_no_match", + "edit_ambiguous", + "edit_overlap", + "edit_overlap", + "edit_unchanged", + "non_utf8", + "edit_too_large", + "not_found", + ] + .map(|code| Some(code.to_owned())) + ); + // The messages name the 1-based edit index, never file or model text. + let messages = fixture.tool_messages_after_close(); + assert!(messages[0].contains("edit 2"), "{}", messages[0]); + assert!(messages[1].contains("edit 2"), "{}", messages[1]); + assert!(messages[2].contains("edits 1 and 2"), "{}", messages[2]); + assert!(!messages[0].contains("four"), "{}", messages[0]); +} + +#[test] +fn a_multi_edit_whose_report_cannot_fit_keeps_its_approval() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("notes.txt"); + fs::write(&target, "old1 old2").expect("write workspace file"); + let edits = [("old1", "new1"), ("old2", "new2")]; + fixture.edit_turn(vec![multi_call("m-1", "notes.txt", &edits)]); + let approval = fixture.approve_pending_edit(); + let mut model = ScriptedModel::new(vec![ + tool_reply(vec![multi_call("m-2", "notes.txt", &edits)]), + text_reply("reported"), + ]); + let limits = TurnLimits { + max_result_bytes: 200, + ..TurnLimits::default() + }; + fixture.run(&mut model, limits).expect("recoverable"); + assert_eq!(fs::read_to_string(&target).expect("read"), "old1 old2"); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert!(fixture.last_tool_message().contains("output_too_large")); + fixture.edit_turn(vec![multi_call("m-3", "notes.txt", &edits)]); + assert_eq!(fs::read_to_string(&target).expect("read"), "new1 new2"); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); +} + +/// An approved creation writes a new file once and spends its approval. It is +/// not checkpointed: nothing existed before. +#[test] +fn an_approved_create_makes_the_file_once_without_a_checkpoint() { + let mut fixture = Fixture::new(); + fs::create_dir(fixture.workspace.join("sub")).expect("parent"); + let target = fixture.workspace.join("sub").join("new.txt"); + let content = "hello\nworld\n"; + + let (_, model) = fixture.edit_turn(vec![create_call("c-1", "sub/new.txt", content)]); + assert!( + model.requests[0] + .tools + .iter() + .any(|name| name == "workspace_create_file") + ); + assert!(!target.exists()); + let message = fixture.last_tool_message(); + assert!(message.contains("approval_required"), "{message}"); + assert!(!message.contains("hello"), "{message}"); + let approvals = fixture.file_write_approvals(); + assert_eq!(approvals.len(), 1); + assert_eq!(approvals[0].state, ApprovalState::Pending); + assert_eq!( + approvals[0].summary, + format!( + "create sub/new.txt: 12 bytes, sha256 {}", + &sha256(content.as_bytes())[..12] + ) + ); + assert!(!approvals[0].summary.contains("hello")); + + // A repeat while pending records nothing new. + fixture.edit_turn(vec![create_call("c-2", "sub/new.txt", content)]); + assert_eq!(fixture.file_write_approvals().len(), 1); + + let approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![create_call("c-3", "sub/new.txt", content)]); + assert_eq!(fs::read_to_string(&target).expect("read"), content); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 0); + let message = fixture.last_tool_message(); + assert!(message.contains("\"status\":\"created\""), "{message}"); + assert!(message.contains("\"bytes\":12"), "{message}"); + assert!(message.contains(&sha256(content.as_bytes())), "{message}"); + assert!(message.contains("\"operation_id\":\""), "{message}"); + assert!(!message.contains("hello"), "the result holds no file text"); + + // The file now exists: a repeat is refused and records no approval. + fixture.edit_turn(vec![create_call("c-4", "sub/new.txt", content)]); + assert_eq!(fixture.file_write_approvals().len(), 1); + assert_eq!(fs::read_to_string(&target).expect("read"), content); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + [ + Some("approval_required"), + Some("approval_required"), + None, + Some("file_exists") + ] + .map(|code| code.map(str::to_owned)) + ); +} + +#[test] +fn an_empty_file_can_be_created() { + let mut fixture = Fixture::new(); + fixture.edit_turn(vec![create_call("c-1", "empty.txt", "")]); + fixture.approve_pending_edit(); + fixture.edit_turn(vec![create_call("c-2", "empty.txt", "")]); + assert_eq!( + fs::read(fixture.workspace.join("empty.txt")).expect("read"), + Vec::::new() + ); +} + +#[test] +fn a_create_approval_does_not_cover_other_content_or_path() { + let mut fixture = Fixture::new(); + fixture.edit_turn(vec![create_call("c-1", "a.txt", "one")]); + let approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![create_call("c-2", "a.txt", "two")]); + fixture.edit_turn(vec![create_call("c-3", "b.txt", "one")]); + assert!(!fixture.workspace.join("a.txt").exists()); + assert!(!fixture.workspace.join("b.txt").exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert_eq!(fixture.file_write_approvals().len(), 3); +} + +/// An approved creation cannot edit a file, and its digest is not an edit's. +#[test] +fn a_create_approval_does_not_authorize_edits() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("notes.txt"), "alpha").expect("notes"); + fixture.edit_turn(vec![create_call("c-1", "new.txt", "alpha")]); + let create_approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![ + edit_call("e-1", "notes.txt", "alpha", "beta"), + multi_call("m-1", "notes.txt", &[("alpha", "beta")]), + ]); + assert_eq!( + fs::read_to_string(fixture.workspace.join("notes.txt")).expect("read"), + "alpha" + ); + assert_eq!( + fixture.approval_state(create_approval), + ApprovalState::Approved + ); + assert!(!fixture.workspace.join("new.txt").exists()); + assert_eq!(fixture.file_write_approvals().len(), 3); +} + +#[test] +fn a_denied_create_writes_nothing_and_the_turn_continues() { + let mut fixture = Fixture::new(); + fixture.edit_turn(vec![create_call("c-1", "new.txt", "x")]); + let pending = fixture.file_write_approvals()[0].id; + ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store") + .decide_approval(pending, false, now_unix()) + .expect("deny"); + let (report, _) = fixture.edit_turn(vec![create_call("c-2", "new.txt", "x")]); + assert_eq!(report.reply, "reported"); + assert!(!fixture.workspace.join("new.txt").exists()); + assert_eq!(fixture.file_write_approvals().len(), 1); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("approval_denied")); +} + +#[test] +fn create_refusals_are_recoverable_and_record_no_approval() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("present.txt"), "keep").expect("present"); + let mut model = ScriptedModel::new(vec![ + tool_reply(vec![ + create_call("c-1", "present.txt", "replace"), + create_call("c-2", "no-such/new.txt", "x"), + ]), + text_reply("all refused"), + ]); + let report = fixture + .run(&mut model, TurnLimits::default()) + .expect("recoverable refusals keep the turn going"); + assert_eq!(report.reply, "all refused"); + assert!(fixture.file_write_approvals().is_empty()); + assert_eq!( + fs::read_to_string(fixture.workspace.join("present.txt")).expect("read"), + "keep" + ); + assert!(!fixture.workspace.join("no-such").exists()); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + [Some("file_exists".to_owned()), Some("not_found".to_owned())] + ); +} + +/// If another writer creates the file after approval, the retry's live check +/// refuses it before the approval is consumed, and nothing is replaced. +#[test] +fn a_file_created_after_approval_is_never_replaced() { + let mut fixture = Fixture::new(); + fixture.edit_turn(vec![create_call("c-1", "race.txt", "mine")]); + let approval = fixture.approve_pending_edit(); + fs::write(fixture.workspace.join("race.txt"), "theirs").expect("other writer"); + fixture.edit_turn(vec![create_call("c-2", "race.txt", "mine")]); + assert_eq!( + fs::read_to_string(fixture.workspace.join("race.txt")).expect("read"), + "theirs" + ); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("file_exists")); +} + +#[test] +fn a_create_whose_report_cannot_fit_keeps_its_approval() { + let mut fixture = Fixture::new(); + fixture.edit_turn(vec![create_call("c-1", "new.txt", "x")]); + let approval = fixture.approve_pending_edit(); + let mut model = ScriptedModel::new(vec![ + tool_reply(vec![create_call("c-2", "new.txt", "x")]), + text_reply("reported"), + ]); + let limits = TurnLimits { + max_result_bytes: 200, + ..TurnLimits::default() + }; + fixture.run(&mut model, limits).expect("recoverable"); + assert!(!fixture.workspace.join("new.txt").exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert!(fixture.last_tool_message().contains("output_too_large")); + fixture.edit_turn(vec![create_call("c-3", "new.txt", "x")]); + assert_eq!( + fs::read_to_string(fixture.workspace.join("new.txt")).expect("read"), + "x" + ); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); +} + +/// Without live `read` and `write`, neither new tool is offered, and a call +/// made anyway is denied before any file access or approval. +#[test] +fn the_new_file_tools_require_live_read_and_write() { + for (read, write) in [(true, false), (false, true)] { + for name in ["workspace_multi_edit_file", "workspace_create_file"] { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("notes.txt"), "alpha beta\n").expect("notes"); + let workspace_id = fixture.workspace_id; + let control = fixture.control_mut(); + control + .set_permissions( + workspace_id, + PermissionSet { + model_tools: true, + read, + write, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("change preferences"); + let attempt = if name == "workspace_create_file" { + create_call("n-1", "new.txt", "x") + } else { + multi_call("n-1", "notes.txt", &[("beta", "gamma")]) + }; + let mut model = ScriptedModel::new(vec![tool_reply(vec![attempt])]); + let error = fixture + .run(&mut model, TurnLimits::default()) + .expect_err("the call is denied"); + assert!(matches!(error, TurnError::ToolDenied(_)), "{error:?}"); + assert!( + !model.requests[0].tools.iter().any(|tool| tool == name), + "{name} was offered with read={read} write={write}" + ); + assert!(!fixture.workspace.join("new.txt").exists()); + assert!(fixture.file_write_approvals().is_empty()); + assert!(fixture.audit_events().is_empty(), "no broker operation ran"); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("edit_disabled")); + } + } } diff --git a/crates/omgcode-desktop/ui/src/approvals/model.ts b/crates/omgcode-desktop/ui/src/approvals/model.ts index f0b6e64..3870331 100644 --- a/crates/omgcode-desktop/ui/src/approvals/model.ts +++ b/crates/omgcode-desktop/ui/src/approvals/model.ts @@ -6,7 +6,7 @@ export const WRITERS_STOPPED_TEXT = const OPERATION_LABELS: Record = { command: "Command", - file_write: "File edit", + file_write: "File change", git_push: "Git push", network: "Network", }; diff --git a/crates/omgcode-desktop/ui/src/mock/backend.ts b/crates/omgcode-desktop/ui/src/mock/backend.ts index 24022b9..7ac811f 100644 --- a/crates/omgcode-desktop/ui/src/mock/backend.ts +++ b/crates/omgcode-desktop/ui/src/mock/backend.ts @@ -101,7 +101,7 @@ export function createMockBackend(now: () => number = Date.now): { invoke: Invok approval( "a2", "file_write", - `Replace 1 occurrence in src/lib.rs ${HOSTILE.script}`, + `multi-edit src/lib.rs (2 edits): 120 -> 118 bytes ${HOSTILE.script}`, 300, 1, "Refactor the session index", @@ -402,8 +402,11 @@ export function createMockBackend(now: () => number = Date.now): { invoke: Invok ] : [ { label: "File", value: "src/lib.rs", multiline: false }, - { label: "Old text", value: `fn old() {}\n${HOSTILE.script}`, multiline: true }, - { label: "New text", value: "fn new() {}", multiline: true }, + { label: "Edits", value: "2", multiline: false }, + { label: "Old text 1", value: `fn old() {}\n${HOSTILE.script}`, multiline: true }, + { label: "New text 1", value: "fn new() {}", multiline: true }, + { label: "Old text 2", value: "const A: u8 = 1;", multiline: true }, + { label: "New text 2", value: "const A: u8 = 2;", multiline: true }, ]; return clone({ approval: view, @@ -412,7 +415,7 @@ export function createMockBackend(now: () => number = Date.now): { invoke: Invok ? null : view.operation === "command" ? "workspace_run_command" - : "workspace_edit_file", + : "workspace_multi_edit_file", fields: stale ? [] : fields, review_token: token, }); diff --git a/crates/omgcode-desktop/ui/tests/approvals.test.ts b/crates/omgcode-desktop/ui/tests/approvals.test.ts index 724a939..235dc52 100644 --- a/crates/omgcode-desktop/ui/tests/approvals.test.ts +++ b/crates/omgcode-desktop/ui/tests/approvals.test.ts @@ -151,7 +151,7 @@ describe("approvals window", () => { await start(); const rows = Array.from(root.querySelectorAll(".approval")); expect(rows.map((r) => r.classList.contains("pending"))).toEqual([true, true, false]); - expect(rows[0]?.textContent).toContain("File edit"); + expect(rows[0]?.textContent).toContain("File change"); expect(rows[0]?.textContent).toContain("first"); expect(rows[0]?.textContent).toContain("Session title"); expect(rows[2]?.textContent).toContain("denied"); @@ -191,7 +191,52 @@ describe("approvals window", () => { expect(approve.disabled).toBe(false); }); - it("requires the writers-stopped acknowledgement for a file edit", async () => { + it("renders the fields of the multi-edit and create tools as plain text", async () => { + approvals = [view({ operation: "file_write" })]; + current = detail( + { + tool: "workspace_multi_edit_file", + fields: [ + { label: "File", value: "src/lib.rs", multiline: false }, + { label: "Edits", value: "2", multiline: false }, + { label: "Old text 1", value: `a\n${HOSTILE_IMG}`, multiline: true }, + { label: "New text 1", value: "b", multiline: true }, + { label: "Old text 2", value: "c", multiline: true }, + { label: "New text 2", value: "d", multiline: true }, + ], + }, + { operation: "file_write" }, + ); + await start(); + await select(); + let review = root.querySelector(".review") as HTMLElement; + expect(review.textContent).toContain("workspace_multi_edit_file"); + expect(review.textContent).toContain("Old text 2"); + expect(review.textContent).toContain("New text 2"); + expect(review.querySelector("img")).toBeNull(); + expect(review.querySelector("pre")?.textContent).toBe(`a\n${HOSTILE_IMG}`); + + current = detail( + { + tool: "workspace_create_file", + fields: [ + { label: "File", value: "new.txt", multiline: false }, + { label: "Size", value: "12 bytes", multiline: false }, + { label: "SHA-256", value: "e".repeat(64), multiline: false }, + { label: "Content", value: `hello\n${HOSTILE_IMG}`, multiline: true }, + ], + }, + { operation: "file_write" }, + ); + await select(); + review = root.querySelector(".review") as HTMLElement; + expect(review.textContent).toContain("workspace_create_file"); + expect(review.textContent).toContain("Content"); + expect(review.querySelector("img")).toBeNull(); + expect(review.querySelector("pre")?.textContent).toBe(`hello\n${HOSTILE_IMG}`); + }); + + it("requires the writers-stopped acknowledgement for a file change", async () => { approvals = [view({ operation: "file_write" })]; current = detail({ tool: "workspace_edit_file" }, { operation: "file_write" }); await start(); diff --git a/crates/omgcode-desktop/ui/tests/mock.test.ts b/crates/omgcode-desktop/ui/tests/mock.test.ts index de4e16a..c2b871b 100644 --- a/crates/omgcode-desktop/ui/tests/mock.test.ts +++ b/crates/omgcode-desktop/ui/tests/mock.test.ts @@ -6,6 +6,8 @@ import { OMGCODE_MOCK_FIXTURE_MARKER } from "../src/mock/fixtures"; interface Detail { review_token: string | null; binding: string; + tool: string | null; + fields: Array<{ label: string }>; } describe("mock backend", () => { @@ -65,6 +67,20 @@ describe("mock backend", () => { expect(stale.review_token).toBeNull(); }); + it("reviews a multi-edit with numbered old and new text fields", async () => { + const { invoke } = createMockBackend(); + const detail = (await invoke("approval_detail", { id: "a2" })) as Detail; + expect(detail.tool).toBe("workspace_multi_edit_file"); + expect(detail.fields.map((field) => field.label)).toEqual([ + "File", + "Edits", + "Old text 1", + "New text 1", + "Old text 2", + "New text 2", + ]); + }); + it("requires the acknowledgement for a file edit", async () => { const { invoke } = createMockBackend(); const detail = (await invoke("approval_detail", { id: "a2" })) as Detail; diff --git a/crates/omgcode-platform/src/broker.rs b/crates/omgcode-platform/src/broker.rs index fee61c4..2216156 100644 --- a/crates/omgcode-platform/src/broker.rs +++ b/crates/omgcode-platform/src/broker.rs @@ -37,6 +37,27 @@ const MAX_PATH_UNITS: usize = 4096; const MAX_COMPONENT_UNITS: usize = 255; const MAX_COMPONENTS: usize = 32; +/// Report whether a broker error means that a named workspace entry was +/// absent. +/// +/// Linux reports `ErrorKind::NotFound`. Windows keeps the native status inside +/// an `ErrorKind::Other` error, so a missing name is recognised from that +/// status. Callers use this to tell "does not exist" from other native +/// failures; it grants no authority. +pub fn is_missing_entry(error: &io::Error) -> bool { + if error.kind() == io::ErrorKind::NotFound { + return true; + } + #[cfg(windows)] + { + windows::is_name_not_found(error) + } + #[cfg(not(windows))] + { + false + } +} + /// A bounded sequence of native names. It grants no filesystem authority. /// /// Empty paths identify the workspace for listing only. Other paths reject diff --git a/docs/README.md b/docs/README.md index 2c06464..aa21c08 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,10 +5,11 @@ bounded native workspace file broker, and a prototype Tauri desktop shell with a session tree, search, chat, and an isolated approvals window. The shell is without an installer and has run only on Windows. The broker is available only through trusted operator commands and, when a workspace enables `model_tools`, to the -local model's read, list, approval-gated exact-edit, and approval-gated command -tools. The edit tool changes one existing file only through checkpointed apply -after an approved, writers-stopped decision. Model create, delete, rename, -network, Git, and MCP tools are not implemented. The +local model's read, list, approval-gated edit, multi-edit, create, and command +tools. The edit and multi-edit tools change one existing file only through +checkpointed apply after an approved, writers-stopped decision; the create tool +makes one new file through the exclusive create, without a checkpoint. Model +delete, rename, network, Git, and MCP tools are not implemented. The trusted CLI `run` command runs one program under Linux Landlock or Windows AppContainer confinement with an operator-declared toolchain profile. A model tool starts a process only through the approval-gated command tool and the @@ -109,11 +110,13 @@ adapter reads a stored credential, through an operator-selected profile that supplies both the endpoint and the token (ADR-0047, ADR-0048); no cloud adapter or login exists. That adapter can stream a reply (ADR-0049): the text is provisional, and the journals record only the validated reply. Approval records authorize one exact -model-tool call, and the edit and command tools are their only consumers. Chat +model-tool call, and the file-write and command tools are their only consumers. Chat dispatches the read/list tools and, with the required live permissions, the -approval-gated edit and command tools, only for a workspace that enables -`model_tools`. The edit tool writes only through checkpointed apply after an -approved, writers-stopped decision and does not detect concurrent writers. +approval-gated edit, multi-edit, create, and command tools, only for a workspace +that enables `model_tools`. The edit and multi-edit tools write only through +checkpointed apply after an approved, writers-stopped decision, the create tool +only through the exclusive create after the same decision, and none detects +concurrent writers. A read-only CLI preview compares a workspace file with trusted proposed content without writing to the workspace or creating a checkpoint. Dependency security audits, native containment qualification, and release checks diff --git a/docs/adr/0054-add-multi-edit-and-create-model-file-tools.md b/docs/adr/0054-add-multi-edit-and-create-model-file-tools.md new file mode 100644 index 0000000..46f8aaf --- /dev/null +++ b/docs/adr/0054-add-multi-edit-and-create-model-file-tools.md @@ -0,0 +1,120 @@ +# 0054: Add multi-edit and create model file tools + +- Status: Accepted +- Date: 2026-11-17 + +## Context + +`workspace_edit_file` (ADR-0043) replaces one exact occurrence in an existing +file. A model that must change several places in a file needs one approval and +one checkpoint per replacement, and each later call reads the output of the +earlier one. The model also cannot create a file at all: ADR-0043 left create, +delete, and rename disabled. The broker already provides an exclusive create +that never replaces an existing entry, and the checkpointed apply, the +`file_write` approval operation, the writers-stopped gate, and the desktop +review already exist. + +The session and provider layers cap tool names at eight. Six tools are +advertised after this change (read, list, edit, multi-edit, create, command), +and a later increment is expected to add two more. + +## Decision + +Add two model tools, `workspace_multi_edit_file` and `workspace_create_file` +(tool schema 0.4.0). Both follow the edit tool's contract; this record states +only what differs. The owning guides are the +[model edit route](../file-access.md#model-edit-route-adr-0043) and the +[multi-edit and create routes](../file-access.md#model-multi-edit-and-create-routes-adr-0054). + +Common rules: + +- Both are advertised and dispatched only under live `model_tools`, `read`, and + `write`, with the same per-call recheck and the same `edit_disabled` denial. + They are advertised in the order read, list, edit, multi-edit, create, + command. +- Both reuse `ApprovalOperation::FileWrite`, so the writers-stopped + acknowledgement is required and no approval enum, wire type, or TypeScript + union changes. Each has its own digest domain tag + (`omgcode-file-multi-edit-v1`, `omgcode-file-create-v1`), so a record for one + tool, including `workspace_edit_file`, can never be consumed by another. +- Approval lifetime (900 seconds), lookup filters (session, workspace, + operation, exact digest, current settings revision), the output-fits check + before consumption, consumption before the write, and the approved, pending, + and denied handling are shared with the edit tool through one gate. + `workspace_edit_file` keeps its digest, codes, and messages. +- The desktop review considers candidates from all three tools, at most eight + distinct candidates in total, newest first, and is exact only for the + candidate whose tool-specific digest matches. The approvals label becomes + "File change". + +`workspace_multi_edit_file` takes `path` and one to 16 `edits` of +`old_text` (non-empty) and `new_text`, with `additionalProperties: false` at +both levels and the whole call within 4 KiB. Every `old_text` is matched against +the original live bytes and must occur exactly once (`edit_no_match`, +`edit_ambiguous`, naming the 1-based index). The matched ranges must not overlap +(`edit_overlap`); touching ranges are allowed. The replacements apply in +ascending offset order to one proposed buffer, which must differ +(`edit_unchanged`) and fit 1 MiB (`edit_too_large`). One approval and one +checkpointed apply cover the call. The digest covers the session, workspace, +normalized path, and the size and SHA-256 of the current and proposed bytes. +The summary is `multi-edit {path} ({n} edits): {cur} -> {new} bytes, sha256 ...`. + +`workspace_create_file` takes `path` and `content` (empty allowed). Before any +approval is recorded, a live audited read must find the path absent +(`file_exists` otherwise) and a one-entry listing of a non-root parent must +succeed (`not_found` otherwise). The digest covers the session, workspace, +normalized path, and the content's size and SHA-256. After approval the handler +checks that the success report fits, consumes the record, and calls the +broker's exclusive `create_file`. A conflict returns `file_exists` and the +approval stays spent. An uncertain outcome is a terminal failure that leaves the +file-operation journal's pending record for the trusted acknowledgement; the +runtime never retries or deletes. Creation is not checkpointed. + +A missing name is recognised on Windows from the native status the platform +broker keeps inside an `ErrorKind::Other` error (`omgcode_platform::broker::is_missing_entry`). +`classify_file_error` now maps it to the recoverable `not_found` that Linux +already returned, so reading, editing, or listing a missing path no longer ends +a Windows turn. + +## Alternatives considered + +- **Extend `workspace_edit_file` with an optional `edits` array.** Rejected: the + two argument shapes are mutually exclusive, so the schema could not express + them, and the edit tool's strict parser and digest would change. +- **One digest domain for all file tools.** Rejected: an approval recorded for a + single edit could then be consumed by an equivalent multi-edit, and the + reverse. Separate tags keep each approval bound to the tool the operator + reviewed. +- **A new approval operation.** Rejected for now: `FileWrite` already carries the + writers-stopped gate and the desktop and CLI flows, and a new operation would + change the approval enum, wire types, and TypeScript for no new authority. +- **Checkpoint creation.** Not needed: nothing is lost, and the exclusive create + refuses an existing entry. A checkpoint needs prior content. +- **Sequential single edits.** Rejected: each needs its own approval and sees + the previous result, so a failure mid-sequence leaves a partial change. + +## Consequences + +- The 4 KiB argument cap bounds the content of a created file and the size of + a multi-edit. Larger files need several calls or the trusted CLI. +- A create approval is spent if another writer creates the path between the + check and the create. The create refuses to replace it, and the model must + ask again. +- There is no atomicity across files, and neither tool detects a concurrent + writer. Neither is power-loss qualified. +- The set is six tools of the eight-name cap, so the next increment can add two + without raising a cap. The schema estimate used for context accounting rises + from 4 KiB to 5 KiB (the six definitions serialize to 4,546 bytes). +- Windows reads of missing paths become recoverable, which also changes the + read and edit tools' result for that case from a terminal failure to + `not_found`. +- Review fields use fixed numbered labels ("Old text 1" to "Old text 16") so the + label type stays static. + +## References + +- [ADR-0039](0039-gate-future-model-tools-with-bound-single-use-approval-records.md) +- [ADR-0040](0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md) +- [ADR-0043](0043-gate-the-model-file-edit-tool-with-approvals-and-checkpointed-apply.md) +- [ADR-0046](0046-decide-model-tool-approvals-in-an-isolated-desktop-window.md) +- [File access contract](../file-access.md) diff --git a/docs/adr/index.md b/docs/adr/index.md index 8f6c7ba..484b76a 100644 --- a/docs/adr/index.md +++ b/docs/adr/index.md @@ -68,3 +68,4 @@ meaningful alternative does not need an ADR. | [0051: Cancel every model request and let an interrupt cancel a CLI turn](0051-cancel-every-model-request-and-interrupt-cli-turns.md) | Accepted | 2026-11-17 | | [0052: Publish rolling testing builds from main](0052-publish-rolling-testing-builds-from-main.md) | Accepted | 2026-11-17 | | [0053: Compact the checkpoint ledger in place](0053-compact-the-checkpoint-ledger-in-place.md) | Accepted | 2026-11-17 | +| [0054: Add multi-edit and create model file tools](0054-add-multi-edit-and-create-model-file-tools.md) | Accepted | 2026-11-17 | diff --git a/docs/agent-instructions.md b/docs/agent-instructions.md index 7d4db21..9bef5b1 100644 --- a/docs/agent-instructions.md +++ b/docs/agent-instructions.md @@ -32,11 +32,12 @@ skill/tool/memory selections, or an account profile. The core turn/tool runtime records the exact tool schema version and tool names in a schema-version-2 request manifest. Its read/list results are appended as -source-labelled user-level tool messages and are never system messages. Edit -and command results follow the same rule and remain untrusted. Chat runs this -runtime when the workspace enables `model_tools`; the edit tool also needs -`read` and `write`, the command tool also needs `execute`, `read`, and `write`, -and each runs only after a trusted approval. +source-labelled user-level tool messages and are never system messages. Edit, +multi-edit, create, and command results follow the same rule and remain +untrusted. Chat runs this runtime when the workspace enables `model_tools`; the +edit, multi-edit, and create tools also need `read` and `write`, the command +tool also needs `execute`, `read`, and `write`, and each runs only after a +trusted approval. Otherwise chat sends no tool definitions and rejects any provider tool call. Prompt assembly limits custom instruction text and each history message to 16 @@ -44,10 +45,10 @@ KiB, history to 128 messages, and the assembled UTF-8 text to 256 KiB. The adapter applies a caller-supplied context budget using a byte-based estimate and fixed overhead. This is not an exact tokenizer or model-capacity check. The prototype has no settings UI, application/workspace/session preference store, -preview UI, exact token-count display, skills, memories, model create, +preview UI, exact token-count display, skills, memories, model delete, rename, network, Git, or MCP tools, or provider-specific subscription routes. -Its only model tools are read, list, the approval-gated exact edit, and the -approval-gated command tool. The current +Its only model tools are read, list, the approval-gated exact edit, +multi-edit, and create tools, and the approval-gated command tool. The current `--instructions` value is per invocation, not a saved preference with scope or revision history. @@ -173,8 +174,12 @@ must contain no secrets or control characters, and is for the operator. The `workspace_run_command` tool creates the request and consumes an approved record immediately before calling the trusted runner. The `workspace_edit_file` -tool creates a `file_write` request bound to the live file and consumes an -approved record immediately before the checkpointed apply. Each first call +and `workspace_multi_edit_file` tools create a `file_write` request bound to +the live file and consume an approved record immediately before the +checkpointed apply. The `workspace_create_file` tool creates a `file_write` +request bound to the path and exact content, and consumes an approved record +immediately before the exclusive create. Each tool has its own digest domain, +so a record for one tool never authorizes another. Each first call returns `approval_required`; the operator approves between turns, and the model retries the same call. Approving a `file_write` record needs `approvals approve --writers-stopped`; the decision records that diff --git a/docs/development.md b/docs/development.md index 51cbedf..2fc5a86 100644 --- a/docs/development.md +++ b/docs/development.md @@ -8,7 +8,8 @@ files/directories, and preview a bounded diff against trusted proposed content in a registered workspace. When a workspace enables `model_tools`, chat runs the core's bounded turn/tool runtime so the model can read and list files in that workspace; with `read` and `write` enabled it can request an -approval-gated exact edit of an existing file, and with `execute`, `read`, and +approval-gated exact edit or multi-edit of an existing file or the creation of +a new file, and with `execute`, `read`, and `write` enabled it can request an approval-gated command. Trusted CLI apply can replace an existing file on Linux ext4 or Windows NTFS. Trusted restore puts a retained checkpoint's original bytes back through apply, and trusted checkpoint @@ -21,9 +22,10 @@ time. A program started by `run` can create, change, and delete workspace files without a checkpoint. The CLI's `file` commands cannot delete workspace files, and the CLI cannot use Git or connect to MCP servers. When a workspace enables `model_tools`, chat lets the local model read and list -files in that workspace, request an approval-gated exact edit with read/write, -and request an approval-gated command with execute/read/write. Model create, -delete, rename, network, Git, and MCP tools are not implemented. Trusted commands record and apply the Windows container +files in that workspace, request an approval-gated exact edit, multi-edit, or +file creation with read/write, and request an approval-gated command with +execute/read/write. Model delete, rename, network, Git, and MCP tools are not +implemented. Trusted commands record and apply the Windows container grants; see [command execution](execution.md). Version 0.29.0 also adds a prototype Tauri desktop shell, @@ -207,7 +209,8 @@ default to disabled. All decisions still report `execution_allowed: false`; only the trusted `run` command starts a process, and the model command tool reaches it only after consuming an approval. Settings commands do not dispatch tools. `--model-tools true` lets chat offer the model read/list tools, with -read/write the approval-gated edit tool, and with execute/read/write the +read/write the approval-gated edit, multi-edit, and create tools, and with +execute/read/write the approval-gated command tool; see [model tools](workspace-permissions.md#model-readlist-and-command-tools). See the [workspace contract](workspace-permissions.md) for limits and failures. @@ -412,7 +415,8 @@ native mount. Execute remains disabled. The `audit`, `acknowledge`, `compact-checkpoints` commands require existing workspace settings. They do not require a configured root, workspace registration, or session selection. Chat advertises the read/list tools when the session's workspace -enables `model_tools`, the edit tool when `read` and `write` are also enabled, +enables `model_tools`, the edit, multi-edit, and create tools when `read` and +`write` are also enabled, and the command tool when `execute`, `read`, and `write` are also enabled; see [model tools](workspace-permissions.md#model-readlist-and-command-tools). @@ -428,11 +432,11 @@ implementation, and `false` elsewhere. The CLI does not probe the mount, metadat or permissions for that flag. `checkpoint_restore` and `checkpoint_deletion` follow the same rule for `file restore` and `file delete-checkpoint`. `model_read_list_tools` is `true` on Linux and Windows, and `tool_policy` is -`read_list_when_workspace_model_tools_enabled_edit_and_command_require_approval` +`read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_and_command_require_approval` there. `model_write_tools` and `model_command_tool` are `approval_required`. None of them reports whether any workspace enables the preference. -`workspace_file_tools`, which covers general model file tools such as create, -delete, or rename, remains `false`. +`workspace_file_tools`, which covers general model file tools such as delete +or rename, remains `false`. ```text omgcode --data-dir C:\omgcode-data file read WORKSPACE_ID --session SESSION_UUID src\main.rs --max-bytes 65536 @@ -482,7 +486,8 @@ model/tool turn. `chat` runs it when the session's workspace enables trusted application-data directory, and the workspace ID. The runtime: - parses strict OpenAI-style function calls for `workspace_read_file`, - `workspace_list_directory`, `workspace_edit_file`, and + `workspace_list_directory`, `workspace_edit_file`, + `workspace_multi_edit_file`, `workspace_create_file`, and `workspace_run_command`; - rejects unknown tools, malformed or duplicate arguments, absolute or parent paths, and duplicate call identifiers before dispatch; @@ -502,13 +507,16 @@ trusted application-data directory, and the workspace ID. The runtime: - sends saved tool results from earlier turns again only while the live `model_tools` and `read` preferences both allow it, and otherwise replaces each with a fixed withheld notice; -- offers `workspace_edit_file` only under live `model_tools`, `read`, and - `write`, and `workspace_run_command` only when `execute` is also enabled; - every edit or command first needs a trusted single-use approval; +- offers `workspace_edit_file`, `workspace_multi_edit_file`, and + `workspace_create_file` only under live `model_tools`, `read`, and `write`, + and `workspace_run_command` only when `execute` is also enabled; every + edit, multi-edit, creation, or command first needs a trusted single-use + approval; - reports the final reply's provider `finish_reason`. -The runtime has no create, delete, rename, Git, MCP, or network tool. Its only -write is the approved exact edit through checkpointed apply, and its only +The runtime has no delete, rename, Git, MCP, or network tool. Its only +writes are the approved exact edit and multi-edit through checkpointed apply and +the approved creation of one new file through the exclusive create, and its only process execution is the approved command through the trusted runner. `LocalToolModel` adapts the LM Studio client to its model interface. The adapter sends the tool definitions in the OpenAI-compatible `tools` field, parses the @@ -549,7 +557,8 @@ remains text. When the workspace enables `model_tools`, chat offers `workspace_read_file` and `workspace_list_directory`. When `read` and `write` also allow it, it offers -the approval-gated `workspace_edit_file`. When `execute`, `read`, and `write` +the approval-gated `workspace_edit_file`, `workspace_multi_edit_file`, and +`workspace_create_file`. When `execute`, `read`, and `write` also allow it, it offers the approval-gated `workspace_run_command`. The model may make up to four requests in one turn. Each read/list call is rechecked against live settings and stays inside the workspace. Results return to the model as @@ -669,6 +678,30 @@ recoverable result with its checkpoint code. The apply cannot detect a concurrent writer; the acknowledgement records the operating assumption only. Restore and checkpoint management stay trusted `file` commands. +`workspace_multi_edit_file` takes a `path` and one to 16 `edits`, each with a +non-empty `old_text` and a `new_text`, all within the 4 KiB argument limit. +Every `old_text` must occur exactly once in the original file, so no +replacement can see another's output. The matches must not overlap +(`edit_overlap`); touching matches are allowed. A missing or repeated +`old_text` returns `edit_no_match` or `edit_ambiguous` with the 1-based edit +index in the message, and a call whose result equals the file returns +`edit_unchanged`. The approval digest uses its own domain tag, so an approval +for a `workspace_edit_file` call never applies a multi-edit. Review shows each +`Old text N` and `New text N`. One approval covers one checkpointed apply, and +there is no atomicity across files. + +`workspace_create_file` takes a `path` and `content` (possibly empty) within the +same 4 KiB limit, which also bounds the new file. Before recording an approval +it reads the path through the audited broker and lists its parent: an existing +file returns `file_exists`, and a missing parent returns `not_found`, and +neither records an approval. After approval, the approval is consumed and the +broker's exclusive create runs. If another writer created the file in the +meantime, the create returns `file_exists`, replaces nothing, and the approval +is spent. An uncertain creation outcome is a terminal failure that leaves the +file-operation journal's pending record for the trusted acknowledgement. Creation +is not checkpointed: there is no prior content to keep. The result reports the +path, size, SHA-256, and operation ID, never the content. + ## Saved sessions By default, journals use the platform's per-user local application data @@ -728,7 +761,8 @@ can archive, restore, and interrupt a session; see the | Encoded journal event, including LF | 64 KiB | | Journal | 8 MiB and 10,000 events | | Tool call arguments in a session event | 4 KiB | -| Model edit file size, before and after | 1 MiB | +| Model edit and multi-edit file size, before and after | 1 MiB | +| Model multi-edit replacements per call | 16 | | Read file size per call | 15 KiB maximum, 8 KiB default | | Edit preview content | 1 MiB per side | | Edit preview lines | 65,536 per side when changed | diff --git a/docs/execution.md b/docs/execution.md index 32e32d3..aca27bc 100644 --- a/docs/execution.md +++ b/docs/execution.md @@ -698,7 +698,9 @@ approval records with the `file_write` operation, but never starts a process: it writes only through the checkpointed apply in the [file-access contract](file-access.md#model-edit-route-adr-0043). Approving its record requires `approvals approve --writers-stopped`, which the command -operation refuses. +operation refuses. The `workspace_multi_edit_file` and `workspace_create_file` +tools (0.36.0, ADR-0054) use the same `file_write` records, each under its own +digest domain, and start no process either. ### Toolchain profiles @@ -1079,10 +1081,11 @@ These remain true after C6: | C13 (0.24.0) | Provider profiles and trusted approval records | Done: see the [provider authentication guide](provider-authentication.md#recorded-provider-profiles-0240) and [approval records](agent-instructions.md#approval-records). No adapter or model tool executed. | | C14 (0.25.0) | Approval-gated model command tool | Done: see [model command tool](#model-command-tool) and ADR-0040. A CLI acceptance test qualifies one successful approved command on both hosts; confinement correctness remains the platform suites' responsibility. | | C17 (0.28.0) | Approval-gated model edit tool | Done: see the [model edit route](file-access.md#model-edit-route-adr-0043) and ADR-0043. It starts no process; it writes one exact edit through the checkpointed apply after a writers-stopped approval. | +| C18 (0.36.0) | Approval-gated model multi-edit and create tools | Done: see the [model multi-edit and create routes](file-access.md#model-multi-edit-and-create-routes-adr-0054) and ADR-0054. They start no process; multi-edit writes through the checkpointed apply and create through the broker's exclusive create, each after a writers-stopped approval. | Later work needs its own decisions: - registry credentials and development-server networking; -- model create, delete, rename, network, Git, and MCP tools; +- model delete, rename, network, Git, and MCP tools; Live qualification on each supported OS remains a separate gate. diff --git a/docs/file-access.md b/docs/file-access.md index d83384e..49b0ffa 100644 --- a/docs/file-access.md +++ b/docs/file-access.md @@ -171,8 +171,10 @@ Phase A2b adds Linux private checkpoint storage and existing-only recovery. Phase A2c adds trusted native apply on Linux ext4. Phase A3b adds Windows NTFS storage and apply. Phase A4 adds explicit restore and checkpoint deletion on both. Since 0.28.0 the approval-gated model edit tool reaches the same apply; see -[Model edit route](#model-edit-route-adr-0043). No model tool creates, deletes, -renames, restores, or manages checkpoints. +[Model edit route](#model-edit-route-adr-0043). Since 0.36.0 the model +multi-edit tool reaches it too, and the model create tool reaches the exclusive +create; see [Model multi-edit and create routes](#model-multi-edit-and-create-routes-adr-0054). +No model tool deletes, renames, restores, or manages checkpoints. ### Implemented record inspection (Phase A2a) @@ -485,6 +487,51 @@ recorded request and a live read of the file; otherwise it offers Deny only. See the [desktop interface guide](desktop-interface.md#approvals). Restore and checkpoint management stay trusted CLI commands. +### Model multi-edit and create routes (ADR-0054) + +Both tools are advertised and dispatched only under the same live +`model_tools`, `read`, and `write` preferences as `workspace_edit_file`, with +the same per-call recheck and `edit_disabled` denial. Both use +`file_write` approvals with the 900-second lifetime and the writers-stopped +acknowledgement, and the same lookup filters (session, workspace, operation, +exact digest, current settings revision). Each has its own digest domain tag, +so a record for one tool, including `workspace_edit_file`, can never be +consumed by another. + +`workspace_multi_edit_file` takes a path and one to 16 `old_text`/`new_text` +pairs. It reads the live file through the audited broker (at most 1 MiB, UTF-8) +and matches every `old_text` against that original content: each must occur +exactly once (`edit_no_match`, `edit_ambiguous`, naming the 1-based edit index), +and the matched ranges must not overlap (`edit_overlap`). It applies the +replacements in ascending offset order into one proposed buffer, which must +differ from the file (`edit_unchanged`) and fit 1 MiB (`edit_too_large`). The +digest, under `omgcode-file-multi-edit-v1`, covers the session, workspace, +normalized path, and the size and SHA-256 of the current and proposed bytes. +After approval, the handler checks the success report fits, consumes the +record, and calls `apply_replacement` with the captured size and digest, like +the single edit. One approval and one checkpoint cover the whole call. + +`workspace_create_file` takes a path and complete UTF-8 content (empty +allowed), bounded by the 4 KiB argument limit. Before recording any approval, a +live audited read must find the path absent (an existing entry returns +`file_exists`) and a one-entry listing must find its parent directory (a missing +parent returns `not_found`); nothing is recorded in either case. The digest, +under `omgcode-file-create-v1`, covers the session, workspace, normalized path, +and the size and SHA-256 of the content. After approval, the handler checks the +success report fits, consumes the record, and calls the broker's exclusive +`create_file`. The write is not checkpointed: nothing existed before, and the +exclusive create refuses an existing entry. If another writer creates the path +between the check and the create, the create returns `file_exists`, replaces +nothing, and the approval stays spent. An uncertain outcome is a terminal +failure that leaves the file-operation journal's pending record for the trusted +acknowledgement; nothing is retried or deleted. A success reports the path, +size, SHA-256, and operation ID, never the content. + +Neither tool detects a concurrent writer, and neither is power-loss qualified. +The desktop approvals window reproduces the digest from a recorded request of +any of the three tools before it shows fields. A create request also becomes +stale once its path exists. + ### Private checkpoints, quota, and retention Use `/replacement-v1/`, outside the development root and workspace. diff --git a/docs/platform-support.md b/docs/platform-support.md index e7cd719..fc05117 100644 --- a/docs/platform-support.md +++ b/docs/platform-support.md @@ -22,7 +22,7 @@ containment. | User-project toolchains | Required; native installed/managed profile matrix not yet qualified | | Workspace directory identity | Configuration-only metadata check; required native ID and creation time must be available | | Workspace file broker | Bounded read/list/exclusive-create/single-directory creation through trusted CLI commands; release filesystem support is not qualified | -| Model file tools | Read and list, plus an approval-gated exact-edit tool (with `read` and `write`) and an approval-gated command tool (with `execute`, `read`, and `write`), through chat for a workspace that enables `model_tools`. Each edit needs an approval with the writers-stopped acknowledgement and is applied through the checkpointed apply on Linux ext4 or Windows NTFS. Create, delete, rename, and general file tools are disabled and `workspace_file_tools` remains false | +| Model file tools | Read and list, plus approval-gated exact-edit, multi-edit, and create tools (with `read` and `write`) and an approval-gated command tool (with `execute`, `read`, and `write`), through chat for a workspace that enables `model_tools`. Each edit, multi-edit, or creation needs an approval with the writers-stopped acknowledgement; edits and multi-edits are applied through the checkpointed apply on Linux ext4 or Windows NTFS, and a creation uses the broker's exclusive create without a checkpoint. Delete, rename, and general file tools are disabled and `workspace_file_tools` remains false | | Process-tree runner | Called only by the trusted CLI `run`, through `run_confined`. Windows uses a Job Object assigned at creation and a per-workspace AppContainer. Linux uses a process group, seccomp, `close_range` (kernel 5.11 or later, x86_64 or aarch64), and Landlock ABI 3 or later. Tested on Windows 11 build 26300 and WSL 6.6 only ([execution](execution.md)) | | Tree limits | Every trusted run. Windows uses job memory and active-process limits and job CPU accounting. Linux needs a delegated cgroup v2 with `memory` and `pids` (Linux 5.14 or later for `cgroup.kill`), normally from a `systemd-run --user` scope, so it needs a user service manager. Disk growth is sampled by the supervisor on both platforms; it is not a filesystem quota. Tested on Windows 11 build 26300 and WSL 6.6 only ([tree limits](execution.md#tree-limits)) | | Package networking | Profiles are recorded on both platforms; no run accepts one, `--network` remains all-or-nothing, and no unprivileged destination filter exists on either platform. The trusted `packages fetch` command uses system TLS trust and a per-workspace cache root. The root is exposed read-only on Linux and Windows; Windows needs a read-execute container grant and traversable ancestors, and Linux falls back to application storage when no root is selected (ADR-0034, ADR-0035, ADR-0037) ([fetch](execution.md#trusted-package-fetch)) | diff --git a/docs/security-coding-rules.md b/docs/security-coding-rules.md index a8bae48..fd3ecd6 100644 --- a/docs/security-coding-rules.md +++ b/docs/security-coding-rules.md @@ -152,8 +152,9 @@ the next model request, and records tool intents and results as untrusted evidence. Its checks do not isolate hostile same-user processes or provide process containment. Since 0.12.0, chat drives this runtime when the session's -workspace enables `model_tools`. Since 0.28.0 the only model write is the -approval-gated exact edit (ADR-0043). Model create, delete, rename, and +workspace enables `model_tools`. Since 0.28.0 the model writes only through +approval-gated tools: the exact edit (ADR-0043) and, since 0.36.0, the +multi-edit and create tools (ADR-0054). Model delete, rename, and general file tools remain disabled; capability output keeps `workspace_file_tools` false. For model tools: @@ -277,6 +278,18 @@ crate. consuming a `file_write` approval, consume it before the apply, and use only the checkpointed apply. Do not claim concurrent-writer detection. See ADR-0043. +- The `workspace_multi_edit_file` and `workspace_create_file` model tools follow + the same advertisement, live-recheck, approval, and writers-stopped rules as + `workspace_edit_file`, each under its own digest domain tag + (`omgcode-file-multi-edit-v1`, `omgcode-file-create-v1`) so no approval can be + consumed by another tool. A multi-edit matches every `old_text` against the + original live bytes, refuses ambiguous, missing, overlapping, unchanged, or + oversized results before recording an approval, and uses only the checkpointed + apply. A creation must find the path absent and its parent present through + audited broker calls before recording an approval, and uses only the broker's + exclusive create after consuming the approval; it never replaces, retries, or + deletes, and leaves an uncertain outcome's pending record for the trusted + acknowledgement. Keep file text out of summaries and results. See ADR-0054. - The `workspace_run_command` model tool may be advertised only under live `model_tools`, `execute`, `read`, and `write`. Its first call records a pending approval and starts nothing; only a later exact call may consume an diff --git a/docs/session-storage.md b/docs/session-storage.md index d0349ca..4c3d9c7 100644 --- a/docs/session-storage.md +++ b/docs/session-storage.md @@ -117,8 +117,9 @@ journal, the current control-store writer, and the workspace ID from trusted settings. The journal's recorded workspace ID is only a coherence check. The runtime supports `workspace_read_file` and `workspace_list_directory`, -plus two approval-gated tools offered only under their live settings: -`workspace_edit_file` (ADR-0043) and `workspace_run_command` (ADR-0040). It parses strict OpenAI-style function calls, +plus approval-gated tools offered only under their live settings: +`workspace_edit_file` (ADR-0043), `workspace_multi_edit_file` and +`workspace_create_file` (ADR-0054), and `workspace_run_command` (ADR-0040). It parses strict OpenAI-style function calls, rejects unknown tools, malformed or duplicate arguments, absolute or parent paths, and duplicate call identifiers before dispatch. For each operation it opens the control store, obtains a fresh session binding, releases the settings diff --git a/docs/testing-standards.md b/docs/testing-standards.md index 2144350..27314af 100644 --- a/docs/testing-standards.md +++ b/docs/testing-standards.md @@ -189,6 +189,7 @@ As the relevant components are implemented, include: | Model tool chat | Preference off by default and rechecked per call, settings compatibility, confinement against parent, absolute, rooted, UNC, link, junction, and hard-link paths, strict provider tool-call parsing, token removal before journal and model, refusal of token-bearing calls, withheld replay after revocation, finish reason, one call per turn in escape cases | | Model command tool | Strict profile/program/argument/cwd parsing, tool advertised only with live model-tools/execute/read/write, first call records a pending approval and starts nothing, denial, approval consumption, exact digest and binding, settings-revision invalidation, run hand-off failure, output bounds, no replay, and a durable `model_tools` revocation count that stops a model-origin run even after re-enable. An end-to-end CLI case drives the real chat process, a mock provider, an operator approval, and the trusted runner to a successful confined command on both hosts | | Model edit tool | Strict path/`old_text`/`new_text` parsing with unknown-field, empty-`old_text`, duplicate-key, absolute, and parent-path refusal; the tool advertised and dispatched only with live model-tools/read/write and denied before any file access otherwise; first call records a pending `file_write` approval and writes nothing; a summary with sizes and short digests but no file text or control characters; denial; approval consumption and one checkpointed apply; a further identical call after success needing a new approval; a same-size changed file and a same-size different replacement each needing a new approval; recoverable no-match, ambiguous (including overlaps), unchanged, non-UTF-8, oversized, too-large, and pending-checkpoint results; `session inspect` listing the exact request; schema-0.2.0 replay. A CLI case drives the real chat process, a mock provider, and operator approval to an applied edit and a recorded checkpoint ([ADR-0043](adr/0043-gate-the-model-file-edit-tool-with-approvals-and-checkpointed-apply.md)) | +| Model multi-edit and create tools | Strict parsing of `edits` (zero and 17 entries, empty `old_text`, unknown or duplicate keys at both levels, wrong types) and of `content`; both tools advertised and dispatched only with live model-tools/read/write, in the documented order, and denied before any file access otherwise; first call records a pending `file_write` approval and writes nothing; summaries with sizes and short digests but no text; denial; approval consumption and one checkpointed apply for a multi-edit (edits listed out of offset order, touching matches) and one exclusive create with no checkpoint; a changed file or different request needing a new approval; an `workspace_edit_file` approval never consumed by an equivalent multi-edit and the reverse, and a create approval never authorizing an edit; recoverable no-match and ambiguous (naming the edit index), overlap, unchanged, non-UTF-8, too-large, `file_exists`, and missing-parent results that record no approval; an output-too-large refusal that keeps the approval; schema-0.2.0 and 0.3.0 replay; desktop review fields, the shared eight-candidate bound across the three tools, stale review after a change or once a created path exists, and hostile text escaped. CLI cases drive the real chat process, a mock provider, and operator approval for each tool ([ADR-0054](adr/0054-add-multi-edit-and-create-model-file-tools.md)) | | MCP | Version/transport compatibility, authorization, untrusted content, local confinement, OAuth/redirects, cancellation | | LM Studio | Model discovery, token/no-token profiles, streamed tools, context limits, unavailable models, no hosted-tool bypass | | Instructions | Compiled base/UI/request match, runtime replacement denial, role-injection attempts, preference/task precedence, revisions, safe rendering | diff --git a/docs/workspace-permissions.md b/docs/workspace-permissions.md index ab95a64..f9037fc 100644 --- a/docs/workspace-permissions.md +++ b/docs/workspace-permissions.md @@ -93,7 +93,8 @@ it runs. A change that withdraws one stops the running tree, which is recorded as `revoked`. The `permissions` command reports success only after the runs it affects have stopped; see [settings changes during a run](execution.md#settings-changes-during-a-run). Per-target grants remain -required. Single-use approvals now exist for the model edit and command tools: +required. Single-use approvals now exist for the model edit, multi-edit, create, and +command tools: the trusted CLI and the desktop approvals window decide them. Workspace settings still change only through the trusted CLI; the desktop shell has no settings interface. @@ -315,6 +316,16 @@ is denied with `edit_disabled` and ends the turn. Disable `write` to remove the tool. See [model edit route](file-access.md#model-edit-route-adr-0043) and [ADR-0043](adr/0043-gate-the-model-file-edit-tool-with-approvals-and-checkpointed-apply.md). +The same three preferences also offer `workspace_multi_edit_file`, which +applies up to 16 exact replacements to one existing file under one approval and +one checkpoint, and `workspace_create_file`, which creates one new file through +the exclusive create without a checkpoint. They share the edit tool's +`file_write` approvals, writers-stopped acknowledgement, per-call recheck, and +`edit_disabled` denial, and each has its own digest domain, so an approval for +one never authorizes another. Disabling `write` or `read` removes all three. See +[model multi-edit and create routes](file-access.md#model-multi-edit-and-create-routes-adr-0054) +and [ADR-0054](adr/0054-add-multi-edit-and-create-model-file-tools.md). + When `model_tools`, `execute`, `read`, and `write` are all enabled, the runtime also offers `workspace_run_command`. Its first call records a pending approval and does not run; after `approvals approve `, a second identical call @@ -322,9 +333,10 @@ consumes the approval and starts the trusted run. The model cannot approve. The tool uses fixed limits and no network or host services. See [model command tool](execution.md#model-command-tool) and [ADR-0040](adr/0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md). -The tool schema version is 0.3.0, which added `workspace_edit_file`. Each -request manifest records the version it advertised, and sessions that recorded -0.2.0 still replay and accept new turns. +The tool schema version is 0.4.0, which added `workspace_multi_edit_file` and +`workspace_create_file`; 0.3.0 added `workspace_edit_file`. Each request +manifest records the version it advertised, and sessions that recorded 0.2.0 or +0.3.0 still replay and accept new turns. `workspace status` always reports `model_tools`. Persisted settings omit the field while it is `false`, so settings that never enable it keep their exact From f2740943f72745713b0d4eb5e53ace43ed0a97ad Mon Sep 17 00:00:00 2001 From: James Chapman Date: Tue, 17 Nov 2026 20:33:47 +0000 Subject: [PATCH 2/4] feat: send registry credentials to one declared fetch origin Bind one stored secret, bearer or basic, to one declared origin of a package-network profile and send it from the trusted packages fetch as a sensitive Authorization header, only to the longest matching origin. A bound secret that cannot be read refuses the fetch before anything is sent or recorded. Journals record only the opaque credential id. Phase C20, ADR-0056, version 0.37.0. Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 27 + Cargo.lock | 10 +- Cargo.toml | 2 +- IMPLEMENTATION_PLAN.md | 11 +- crates/omgcode-cli/src/control_cli.rs | 67 +- crates/omgcode-cli/src/credential_cli.rs | 151 +++- crates/omgcode-cli/src/main.rs | 1 + .../tests/package_credential_workflow.rs | 610 +++++++++++++ crates/omgcode-core/src/control.rs | 37 +- crates/omgcode-core/src/control/credential.rs | 45 +- crates/omgcode-core/src/control/package.rs | 109 ++- .../src/control/package/credential.rs | 619 +++++++++++++ .../src/control/package/credential/tests.rs | 815 ++++++++++++++++++ .../omgcode-core/src/control/package/tests.rs | 10 +- crates/omgcode-core/src/lib.rs | 2 + crates/omgcode-core/src/packages.rs | 112 ++- .../src/packages/authorization.rs | 83 ++ .../src/packages/authorization/tests.rs | 46 + crates/omgcode-core/src/packages/tests.rs | 275 +++++- crates/omgcode-core/src/test_support.rs | 34 + crates/omgcode-platform/src/credentials.rs | 2 +- ...ges-through-a-trusted-allowlisted-cache.md | 1 + ...stry-credentials-to-one-declared-origin.md | 123 +++ docs/adr/index.md | 1 + docs/development-tools.md | 5 +- docs/development.md | 23 + docs/execution.md | 56 +- docs/platform-support.md | 2 +- docs/security-coding-rules.md | 11 +- docs/testing-standards.md | 2 +- docs/workspace-permissions.md | 8 + 31 files changed, 3219 insertions(+), 81 deletions(-) create mode 100644 crates/omgcode-cli/tests/package_credential_workflow.rs create mode 100644 crates/omgcode-core/src/control/package/credential.rs create mode 100644 crates/omgcode-core/src/control/package/credential/tests.rs create mode 100644 crates/omgcode-core/src/packages/authorization.rs create mode 100644 crates/omgcode-core/src/packages/authorization/tests.rs create mode 100644 crates/omgcode-core/src/test_support.rs create mode 100644 docs/adr/0056-send-registry-credentials-to-one-declared-origin.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 03819b8..31473c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,33 @@ Changes are recorded here before a release version is assigned. ### Added +- Version 0.37.0 adds registry credentials for the trusted `packages fetch` + (Phase C20). See ADR-0056 and + [registry credentials](docs/execution.md#registry-credentials). Until now + fetch sent no credentials, so a private registry could not be used. It is on + branch `codex/model-file-tools`, stacked on + `codex/checkpoint-ledger-compaction`; it is not merged. It adds no + dependency. + - **Binding:** `workspace package-credential-set --origin + --auth bearer|basic --revision ` reads one secret from piped input only + and binds it to one declared origin of a package-network profile. + `package-credential-remove` unbinds and deletes it, and removing the + profile unbinds every credential in the same event. Secrets are stored + through the existing DPAPI or Secret Service store; the control journal + records only the opaque ID and backend. Bearer secrets are visible ASCII + without spaces; Basic secrets need a non-empty user before the first `:`. + - **Fetch:** the request carries a sensitive `Authorization` header only when + the matched origin, chosen by the longest path prefix, has a binding. + Redirects stay refused, so the header cannot follow one. A bound secret + that cannot be read refuses the fetch before a start record, and nothing is + sent. The start record and report carry an optional `credential_id`. + - **Limits:** the secret still crosses the network to the declared origin, + which may log it. A journal with the new events cannot be read by an older + version. Only loopback HTTP is tested; live HTTPS registries are not. + - **Tests:** header shape for both schemes, no header on an unbound origin, + no request when the secret is missing, no credential on a refused + redirect, journal replay and refusals, store ordering, base64 vectors, and + a CLI workflow that scans every data file and output for the secret. - Version 0.36.0 adds two approval-gated model file tools, multi-edit and create (Phase C18). See ADR-0054 and the [multi-edit and create routes](docs/file-access.md#model-multi-edit-and-create-routes-adr-0054). diff --git a/Cargo.lock b/Cargo.lock index c0b07e1..e8ed3b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2522,7 +2522,7 @@ dependencies = [ [[package]] name = "omgcode-app" -version = "0.36.0" +version = "0.37.0" dependencies = [ "directories", "omgcode-core", @@ -2537,7 +2537,7 @@ dependencies = [ [[package]] name = "omgcode-cli" -version = "0.36.0" +version = "0.37.0" dependencies = [ "clap", "omgcode-app", @@ -2550,7 +2550,7 @@ dependencies = [ [[package]] name = "omgcode-core" -version = "0.36.0" +version = "0.37.0" dependencies = [ "libc", "omgcode-platform", @@ -2568,7 +2568,7 @@ dependencies = [ [[package]] name = "omgcode-desktop" -version = "0.36.0" +version = "0.37.0" dependencies = [ "omgcode-app", "serde", @@ -2580,7 +2580,7 @@ dependencies = [ [[package]] name = "omgcode-platform" -version = "0.36.0" +version = "0.37.0" dependencies = [ "libc", "rustix", diff --git a/Cargo.toml b/Cargo.toml index 5f00434..e0379b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/omgcode-core", "crates/omgcode-cli", "crates/omgcode-platform", "crates/omgcode-app", "crates/omgcode-desktop"] [workspace.package] -version = "0.36.0" +version = "0.37.0" edition = "2024" rust-version = "1.99.1" license = "Apache-2.0" diff --git a/IMPLEMENTATION_PLAN.md b/IMPLEMENTATION_PLAN.md index 962c1df..6a8c975 100644 --- a/IMPLEMENTATION_PLAN.md +++ b/IMPLEMENTATION_PLAN.md @@ -995,6 +995,15 @@ on Windows, and trusted CLI commands as the first caller. See sessions still replay. The 4 KiB argument cap bounds create content and multi-edit size, there is no multi-file atomicity, and 6 of the 8 tool-name slots are used. +- **C20 (version 0.37.0):** registry credentials for the trusted + `packages fetch`. `workspace package-credential-set` and + `package-credential-remove` bind one stored secret, `bearer` or `basic`, to + one declared origin of a package-network profile. Fetch sends it as a + sensitive `Authorization` header only when that origin is the longest + matching one, refuses the fetch when a bound secret cannot be read, and + records only the opaque credential ID. No run, model, or journal receives + the secret. See + [ADR-0056](docs/adr/0056-send-registry-credentials-to-one-declared-origin.md). - **Run network destination allowlist: deferred (2026-11-16).** No unprivileged mechanism passes the ADR-0034 gate on both hosts. The only candidate is a Linux user and network namespace with a trusted relay, which @@ -1010,7 +1019,7 @@ Next decisions, not yet scheduled: adapter, the local LM Studio route, now reads a stored credential, only at its profile's loopback endpoint); - model delete and rename tools, and network, Git, and MCP tools; -- registry credentials and development-server networking. +- development-server networking. ### Phase D: additional providers, streaming, and account lifecycle diff --git a/crates/omgcode-cli/src/control_cli.rs b/crates/omgcode-cli/src/control_cli.rs index dcae3b6..517178f 100644 --- a/crates/omgcode-cli/src/control_cli.rs +++ b/crates/omgcode-cli/src/control_cli.rs @@ -82,12 +82,44 @@ pub(crate) enum WorkspaceCommand { PackageProfileAdd(PackageProfileAddArgs), /// Show one recorded package-network profile. PackageProfileShow { name: String }, - /// Remove a package-network profile. + /// Remove a package-network profile. Credentials bound to its origins are + /// unbound by the same recorded event and then deleted from the native store. PackageProfileRemove { name: String, #[arg(long)] revision: u64, }, + /// Store a registry credential and bind it to one declared origin of a + /// package-network profile. Only `packages fetch` reads it, and it sends it + /// to that origin alone. + /// + /// The secret is read from standard input only, up to 16 KiB; one trailing + /// newline is removed. It must be visible ASCII without control characters + /// or surrounding whitespace. `basic` needs `user:password`. A previously + /// bound credential is deleted after the new binding is recorded. + PackageCredentialSet { + /// The package-network profile name. + name: String, + /// One of the profile's declared origins, such as + /// https://registry.example.com/api/. + #[arg(long)] + origin: String, + /// How the secret is sent: `bearer` or `basic`. + #[arg(long)] + auth: String, + #[arg(long)] + revision: u64, + }, + /// Unbind a registry credential from one origin, then delete it natively. + PackageCredentialRemove { + /// The package-network profile name. + name: String, + /// The origin the credential is bound to. + #[arg(long)] + origin: String, + #[arg(long)] + revision: u64, + }, /// Record a provider connection shape, optionally with its endpoint. No /// credential is stored; the local lm_studio adapter reads a selected profile. ProviderProfileAdd(ProviderProfileAddArgs), @@ -348,17 +380,36 @@ pub(crate) fn run(action: WorkspaceCommand, data_root: &Path) -> Result<(), CliE WorkspaceCommand::PackageProfileRemove { name, revision } => { let mut store = open_existing(data_root)? .ok_or_else(|| CliError::new("control", "Workspace control is not initialized"))?; - let profile_id = store - .remove_package_profile(&name, revision) - .map_err(control_error)?; - let output = json!({ - "removed_package_profile_id": profile_id, - "snapshot": store.snapshot(), - }); + let output = + credential_cli::remove_package_profile(&mut store, data_root, &name, revision)?; drop(store); stop_withdrawn_runs(data_root, &[])?; print_json(&output) } + WorkspaceCommand::PackageCredentialSet { + name, + origin, + auth, + revision, + } => { + // As for provider credentials: read the secret before the control + // store is opened, so its lock is never held while input is awaited. + let secret = credential_cli::read_secret()?; + let mut store = open_existing(data_root)? + .ok_or_else(|| CliError::new("control", "Workspace control is not initialized"))?; + credential_cli::set_package( + &mut store, data_root, &name, &origin, &auth, secret, revision, + ) + } + WorkspaceCommand::PackageCredentialRemove { + name, + origin, + revision, + } => { + let mut store = open_existing(data_root)? + .ok_or_else(|| CliError::new("control", "Workspace control is not initialized"))?; + credential_cli::remove_package(&mut store, data_root, &name, &origin, revision) + } WorkspaceCommand::ProviderProfileAdd(args) => provider_profile_add(args, data_root), WorkspaceCommand::ProviderProfileShow { name } => { let store = open_existing(data_root)? diff --git a/crates/omgcode-cli/src/credential_cli.rs b/crates/omgcode-cli/src/credential_cli.rs index 29e14c2..fa0a953 100644 --- a/crates/omgcode-cli/src/credential_cli.rs +++ b/crates/omgcode-cli/src/credential_cli.rs @@ -9,7 +9,8 @@ use omgcode_core::control::{ ControlStore, CredentialError, CredentialStore, CredentialStoreStatus, OrphanedCredential, - ProviderCredentialError, Secret, delete_unbound_credential, + PackageAuth, PackageCredentialError, ProviderCredentialError, Secret, + delete_unbound_credential, }; use serde_json::{Value, json}; use std::io::{self, IsTerminal}; @@ -107,6 +108,128 @@ pub(crate) fn remove_profile( Ok(output) } +/// Store `secret` as a new registry credential, bind it to one declared +/// origin of the package profile `name`, and delete any credential it +/// replaces. The caller reads the secret with [`read_secret`] before it opens +/// the control store. The secret is validated before anything is stored. +pub(crate) fn set_package( + store: &mut ControlStore, + data_root: &Path, + name: &str, + origin: &str, + auth: &str, + secret: Secret, + revision: u64, +) -> Result<(), CliError> { + let auth = PackageAuth::parse(auth) + .ok_or_else(|| CliError::new("configuration", "Authentication must be bearer or basic"))?; + let credentials = CredentialStore::open(data_root).map_err(credential_error)?; + let set = store + .set_package_credential(&credentials, name, origin, auth, &secret, revision) + .map_err(package_credential_error)?; + drop(secret); + let mut output = json!({ + "package_credential": { + "profile": name, + "profile_id": set.profile_id, + "origin": set.origin, + "auth": set.auth.as_str(), + "credential_id": set.binding.id().as_str(), + "backend": set.binding.backend().as_str(), + "replaced_credential_id": set.replaced.as_ref().map(|id| id.as_str()), + }, + "snapshot": store.snapshot(), + }); + add_orphan_warning(&mut output, set.orphan.as_ref()); + print_json(&output) +} + +/// Record that one origin of the package profile `name` has no credential, +/// then delete the native item. +pub(crate) fn remove_package( + store: &mut ControlStore, + data_root: &Path, + name: &str, + origin: &str, + revision: u64, +) -> Result<(), CliError> { + // Open the native store first, so an unavailable or locked store refuses + // the command before anything is recorded. + let credentials = CredentialStore::open(data_root).map_err(credential_error)?; + let removal = store + .remove_package_credential(&credentials, name, origin, revision) + .map_err(package_credential_error)?; + let mut output = json!({ + "removed_package_credential": { + "profile": name, + "profile_id": removal.profile_id, + "origin": removal.origin, + "credential_id": removal.removed.as_str(), + }, + "snapshot": store.snapshot(), + }); + add_orphan_warning(&mut output, removal.orphan.as_ref()); + print_json(&output) +} + +/// Remove a package profile. Every credential bound to its origins ends in +/// the same recorded event, and their native items are deleted afterwards. +pub(crate) fn remove_package_profile( + store: &mut ControlStore, + data_root: &Path, + name: &str, + revision: u64, +) -> Result { + let bound = store + .package_profile(name) + .is_some_and(|status| !status.credentials().is_empty()); + let credentials = if bound { + Some(CredentialStore::open(data_root).map_err(credential_error)?) + } else { + None + }; + let removal = store + .remove_package_profile(name, revision) + .map_err(crate::control_cli::control_error)?; + let orphans: Vec = removal + .credentials + .iter() + .filter_map(|binding| match &credentials { + Some(credentials) => delete_unbound_credential(credentials, binding), + None => Some(OrphanedCredential { + id: binding.id().clone(), + error: CredentialError::Unavailable( + "the credential store was not opened".to_owned(), + ), + }), + }) + .collect(); + let mut output = json!({ + "removed_package_profile_id": removal.profile_id, + "removed_credential_ids": removal + .credentials + .iter() + .map(|binding| binding.id().as_str()) + .collect::>(), + "snapshot": store.snapshot(), + }); + if let (Some(first), Some(object)) = (orphans.first(), output.as_object_mut()) { + object.insert( + "warning".to_owned(), + json!({ + "code": "credential_orphaned", + "credential_ids": orphans.iter().map(|orphan| orphan.id.as_str()).collect::>(), + "message": format!( + "{} credential(s) are no longer bound, but their native items could not be deleted (first: {}). Remove them manually.", + orphans.len(), + first.error + ), + }), + ); + } + Ok(output) +} + /// The `credential_store` capability: a backend name, or `unavailable` with /// its reason. Probing creates, unlocks, and reads nothing. pub(crate) fn capability() -> Value { @@ -204,6 +327,32 @@ fn provider_credential_error(error: ProviderCredentialError) -> CliError { } } +fn package_credential_error(error: PackageCredentialError) -> CliError { + match error { + PackageCredentialError::Control(error) => crate::control_cli::control_error(error), + PackageCredentialError::Credential(error) => credential_error(error), + error @ (PackageCredentialError::Unrecorded { .. } + | PackageCredentialError::Unstored { .. }) => { + CliError::new("credential_orphaned", error.to_string()) + } + error @ PackageCredentialError::BindingUncertain { .. } => { + let replaced = match &error { + PackageCredentialError::BindingUncertain { + replaced: Some(old), + .. + } => format!( + " If the new binding is recorded, the replaced credential {old} is no longer bound and was not deleted; delete it by hand." + ), + _ => String::new(), + }; + CliError::new( + "credential_binding_uncertain", + format!("{error}.{replaced}"), + ) + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/omgcode-cli/src/main.rs b/crates/omgcode-cli/src/main.rs index 0b16661..c6c8a0b 100644 --- a/crates/omgcode-cli/src/main.rs +++ b/crates/omgcode-cli/src/main.rs @@ -315,6 +315,7 @@ fn run(cli: Cli) -> Result<(), CliError> { "streaming_adapters": ["lm_studio"], "cancellable_adapters": ["lm_studio"], "provider_credentials": omgcode_core::control::CredentialBackend::native().is_some(), + "package_registry_credentials": omgcode_core::control::CredentialBackend::native().is_some(), "credential_store": credential_cli::capability(), "approvals": true, "process_execution": if RUN_SUPPORTED { diff --git a/crates/omgcode-cli/tests/package_credential_workflow.rs b/crates/omgcode-cli/tests/package_credential_workflow.rs new file mode 100644 index 0000000..353f73d --- /dev/null +++ b/crates/omgcode-cli/tests/package_credential_workflow.rs @@ -0,0 +1,610 @@ +//! Registry credentials for the trusted package fetch, through the real CLI, +//! the real native credential store, and a loopback registry fixture. On +//! Linux these need an unlocked Secret Service default collection and fail +//! without one. +#![cfg(any(target_os = "linux", windows))] + +mod support; + +use std::fs::{self, File}; +use std::io::{self, Read, Write}; +use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::path::{Path, PathBuf}; +use std::process::{Output, Stdio}; +use std::thread::{self, JoinHandle}; +use std::time::{Duration, Instant}; + +use omgcode_core::control::{CredentialError, CredentialId, CredentialStore}; +use serde_json::Value; +use support::{ + CLI_DEADLINE, assert_control_error, assert_error_code, assert_success, cli_command, + json_output, path_arg, run_cli, run_with_deadline, +}; +use tempfile::TempDir; + +const PROFILE: &str = "registries"; +const SENTINEL: &str = "reg-omgcode-cli-sentinel-3d7a90"; +const ROTATED: &str = "alice:reg-omgcode-rotated-sentinel-5b21"; +const BODY: &[u8] = b"fetched-package-bytes"; + +/// A one-connection loopback registry that records the request head. +struct Fixture { + address: SocketAddr, + task: JoinHandle, +} + +impl Fixture { + fn start() -> Self { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind fixture"); + listener.set_nonblocking(true).expect("nonblocking"); + let address = listener.local_addr().expect("fixture address"); + let task = thread::spawn(move || { + let deadline = Instant::now() + Duration::from_secs(30); + let (mut stream, _) = loop { + match listener.accept() { + Ok(connection) => break connection, + Err(error) if error.kind() == io::ErrorKind::WouldBlock => { + assert!(Instant::now() < deadline, "fixture accept timed out"); + thread::sleep(Duration::from_millis(2)); + } + Err(error) => panic!("fixture accept failed: {error}"), + } + }; + stream.set_nonblocking(false).expect("blocking stream"); + stream + .set_read_timeout(Some(Duration::from_secs(3))) + .expect("read timeout"); + let head = read_head(&mut stream); + let response = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + BODY.len() + ); + stream.write_all(response.as_bytes()).expect("headers"); + stream.write_all(BODY).expect("body"); + head + }); + Self { address, task } + } + + fn origin(&self) -> String { + format!("http://{}/fixtures/", self.address) + } + + fn finish(self) -> String { + let deadline = Instant::now() + Duration::from_secs(30); + while !self.task.is_finished() { + assert!(Instant::now() < deadline, "fixture finished"); + thread::sleep(Duration::from_millis(2)); + } + self.task.join().expect("fixture joined") + } +} + +fn read_head(stream: &mut TcpStream) -> String { + let mut bytes = Vec::new(); + let mut buffer = [0u8; 1024]; + while bytes.len() < 16 * 1024 { + let read = stream.read(&mut buffer).expect("read request"); + if read == 0 { + break; + } + bytes.extend_from_slice(&buffer[..read]); + if bytes.windows(4).any(|window| window == b"\r\n\r\n") { + break; + } + } + String::from_utf8(bytes).expect("request head is UTF-8") +} + +struct Scene { + temporary: TempDir, + data: PathBuf, + workspace_id: String, +} + +fn revision(data: &Path) -> u64 { + let output = run_cli(data, &["workspace", "status"]); + assert_success(&output); + json_output(&output)["revision"].as_u64().expect("revision") +} + +/// A trusted data root with a registered `task_network` workspace and a +/// package profile with the fixture origin and a second, unbound origin. +fn scene(origin: &str) -> Scene { + let temporary = TempDir::new().expect("temporary directory"); + let data = temporary.path().join("application-data"); + #[cfg(windows)] + support::workspace_fixture::restrict_to_current_user(&data); + let root = temporary.path().join("dev"); + let workspace = root.join("work"); + fs::create_dir_all(&workspace).expect("workspace"); + assert_success(&run_cli( + &data, + &["workspace", "set-root", path_arg(&root), "--revision", "0"], + )); + let registered = run_cli( + &data, + &[ + "workspace", + "register", + path_arg(&workspace), + "--revision", + &revision(&data).to_string(), + ], + ); + assert_success(®istered); + let workspace_id = json_output(®istered)["registration_id"] + .as_str() + .expect("workspace id") + .to_owned(); + assert_success(&run_cli( + &data, + &[ + "workspace", + "permissions", + &workspace_id, + "--task-network", + "true", + "--revision", + &revision(&data).to_string(), + ], + )); + assert_success(&run_cli( + &data, + &[ + "workspace", + "package-profile-add", + PROFILE, + "--registry", + origin, + "--registry", + "https://cdn.example.com/files/", + "--revision", + &revision(&data).to_string(), + ], + )); + #[cfg(windows)] + { + let cache_root = temporary.path().join("package-cache-root"); + fs::create_dir(&cache_root).expect("cache root"); + assert_success(&run_cli( + &data, + &[ + "workspace", + "set-package-cache", + &workspace_id, + path_arg(&cache_root), + "--revision", + &revision(&data).to_string(), + ], + )); + } + Scene { + temporary, + data, + workspace_id, + } +} + +/// Run the CLI with `input` as its standard input, from a file outside the +/// data root so the data-root scan sees only what the CLI wrote. +fn run_with_stdin(scene: &Scene, args: &[&str], input: &[u8]) -> Output { + let path = scene.temporary.path().join("stdin-input"); + fs::write(&path, input).expect("stdin fixture"); + let mut command = cli_command(&scene.data, args); + command.stdin(Stdio::from(File::open(&path).expect("open stdin fixture"))); + let result = run_with_deadline(command, CLI_DEADLINE).expect("run CLI"); + assert!(!result.timed_out, "CLI timed out"); + fs::remove_file(&path).expect("remove stdin fixture"); + result.output +} + +fn bind(scene: &Scene, origin: &str, auth: &str, input: &[u8]) -> Output { + let revision = revision(&scene.data).to_string(); + run_with_stdin( + scene, + &[ + "workspace", + "package-credential-set", + PROFILE, + "--origin", + origin, + "--auth", + auth, + "--revision", + &revision, + ], + input, + ) +} + +fn credential_id(output: &Value) -> CredentialId { + CredentialId::parse( + output["package_credential"]["credential_id"] + .as_str() + .expect("credential id"), + ) + .expect("valid credential id") +} + +fn native(data: &Path) -> CredentialStore { + CredentialStore::open(data).expect("native credential store") +} + +fn assert_absent(data: &Path, id: &CredentialId) { + assert!( + matches!(native(data).load(id), Err(CredentialError::NotFound)), + "credential {id} must be deleted" + ); +} + +fn assert_no_secret(output: &Output) { + for secret in [SENTINEL, ROTATED, "reg-omgcode-rotated"] { + for (name, stream) in [("stdout", &output.stdout), ("stderr", &output.stderr)] { + assert!( + !String::from_utf8_lossy(stream).contains(secret), + "{name} contains a secret" + ); + } + } +} + +/// Scan every file under the data root, including journals and credential +/// files, for the plaintext secrets. +fn assert_no_secret_in_files(root: &Path) { + let mut pending = vec![root.to_path_buf()]; + let mut files = 0; + while let Some(directory) = pending.pop() { + for entry in fs::read_dir(&directory).expect("read data directory") { + let entry = entry.expect("directory entry"); + let kind = entry.file_type().expect("file type"); + if kind.is_dir() { + pending.push(entry.path()); + } else if kind.is_file() { + files += 1; + let bytes = fs::read(entry.path()).expect("read data file"); + for secret in [SENTINEL, "reg-omgcode-rotated"] { + assert!( + !bytes + .windows(secret.len()) + .any(|window| window == secret.as_bytes()), + "{} contains a secret", + entry.path().display() + ); + } + } + } + } + assert!(files > 0, "the scan found no files"); +} + +fn fetch(scene: &Scene, url: &str, name: &str) -> Output { + run_cli( + &scene.data, + &[ + "packages", + "fetch", + &scene.workspace_id, + "--profile", + PROFILE, + "--url", + url, + "--name", + name, + ], + ) +} + +/// Bind, show, fetch with the header, rotate, and remove, with no output or +/// file ever holding the secret. +#[test] +fn a_bound_credential_is_shown_sent_rotated_and_removed_without_disclosure() { + let fixture = Fixture::start(); + let scene = scene(&fixture.origin()); + let data = &scene.data; + let origin = fixture.origin(); + + // The secret is read from piped input, one line ending removed. + let output = bind( + &scene, + &origin, + "bearer", + format!("{SENTINEL}\r\n").as_bytes(), + ); + assert_success(&output); + assert_no_secret(&output); + let first = json_output(&output); + let first_id = credential_id(&first); + assert_eq!(first["package_credential"]["auth"], "bearer"); + assert_eq!(first["package_credential"]["profile"], PROFILE); + assert_eq!( + first["package_credential"]["backend"], + native(data).backend().as_str() + ); + assert_eq!( + first["package_credential"]["replaced_credential_id"], + Value::Null + ); + assert_eq!( + native(data).load(&first_id).expect("stored").expose(), + SENTINEL.as_bytes() + ); + + // Show lists the bound origin, auth, identifier, and backend only. + let output = run_cli(data, &["workspace", "package-profile-show", PROFILE]); + assert_success(&output); + assert_no_secret(&output); + let shown = json_output(&output); + let credentials = shown["package_profile"]["credentials"] + .as_array() + .expect("credentials"); + assert_eq!(credentials.len(), 1); + assert_eq!(credentials[0]["auth"], "bearer"); + assert_eq!( + credentials[0]["binding"]["credential_id"], + first_id.as_str() + ); + assert!( + credentials[0]["origin"] + .as_str() + .expect("origin") + .starts_with("http://127.0.0.1:") + ); + + // A fetch sends exactly the bearer header and reports only the identifier. + let url = format!("{origin}pkg.bin"); + let output = fetch(&scene, &url, "pkg.bin"); + assert_success(&output); + assert_no_secret(&output); + let report = json_output(&output)["fetch"].clone(); + assert_eq!(report["credential_id"], first_id.as_str()); + assert_eq!(report["bytes"], BODY.len()); + let head = fixture.finish(); + assert!( + head.lines() + .any(|line| line.eq_ignore_ascii_case(&format!("authorization: Bearer {SENTINEL}"))), + "{head}" + ); + let audit = run_cli(data, &["workspace", "audit", "--limit", "100"]); + assert_success(&audit); + assert_no_secret(&audit); + assert!( + String::from_utf8_lossy(&audit.stdout).contains(first_id.as_str()), + "the fetch start names the credential" + ); + + // Rotation binds a new identifier (now `basic`) and deletes the old item. + let output = bind(&scene, &origin, "basic", format!("{ROTATED}\n").as_bytes()); + assert_success(&output); + assert_no_secret(&output); + let second = json_output(&output); + let second_id = credential_id(&second); + assert_ne!(second_id, first_id); + assert_eq!( + second["package_credential"]["replaced_credential_id"], + first_id.as_str() + ); + assert_absent(data, &first_id); + assert_eq!( + native(data).load(&second_id).expect("rotated").expose(), + ROTATED.as_bytes() + ); + assert_no_secret_in_files(data); + + // Removal unbinds then deletes the native item. + let current = revision(data).to_string(); + let output = run_cli( + data, + &[ + "workspace", + "package-credential-remove", + PROFILE, + "--origin", + &origin, + "--revision", + ¤t, + ], + ); + assert_success(&output); + assert_no_secret(&output); + assert_eq!( + json_output(&output)["removed_package_credential"]["credential_id"], + second_id.as_str() + ); + assert_absent(data, &second_id); + let current = revision(data).to_string(); + let output = run_cli( + data, + &[ + "workspace", + "package-credential-remove", + PROFILE, + "--origin", + &origin, + "--revision", + ¤t, + ], + ); + assert_control_error(&output); + let output = run_cli(data, &["workspace", "package-profile-show", PROFILE]); + assert!( + json_output(&output)["package_profile"] + .get("credentials") + .is_none() + ); + assert_no_secret_in_files(data); +} + +/// Refused input records nothing and stores nothing. +#[test] +fn refused_registry_credential_input_records_nothing() { + let scene = scene("http://127.0.0.1:9/fixtures/"); + let data = &scene.data; + let origin = "http://127.0.0.1:9/fixtures/"; + let before = revision(data); + + // Empty or oversized input is refused as credential input. + for input in [&b""[..], b"\n"] { + assert_error_code(&bind(&scene, origin, "bearer", input), "credential_input"); + } + let oversized = vec![b'k'; 16 * 1024 + 1]; + assert_error_code( + &bind(&scene, origin, "bearer", &oversized), + "credential_input", + ); + // Invalid content is a control refusal, whatever the scheme. + for (auth, input) in [ + ("bearer", format!("{SENTINEL} extra")), + ("bearer", format!(" {SENTINEL}")), + ("bearer", format!("{SENTINEL}\u{7}")), + ("bearer", format!("{SENTINEL}\tx")), + ("bearer", "tok\u{e9}n-sentinel".to_owned()), + ("basic", SENTINEL.to_owned()), + ("basic", format!(":{SENTINEL}")), + ] { + let output = bind(&scene, origin, auth, input.as_bytes()); + assert_control_error(&output); + assert_no_secret(&output); + } + // A bad scheme, an undeclared origin, an unknown profile, a stale revision. + assert_error_code( + &bind(&scene, origin, "token", SENTINEL.as_bytes()), + "configuration", + ); + assert_control_error(&bind( + &scene, + "https://other.example.com/", + "bearer", + SENTINEL.as_bytes(), + )); + assert_control_error(&bind( + &scene, + "http://127.0.0.1:9/other/", + "bearer", + SENTINEL.as_bytes(), + )); + let output = run_with_stdin( + &scene, + &[ + "workspace", + "package-credential-set", + "missing", + "--origin", + origin, + "--auth", + "bearer", + "--revision", + &revision(data).to_string(), + ], + SENTINEL.as_bytes(), + ); + assert_control_error(&output); + let output = run_with_stdin( + &scene, + &[ + "workspace", + "package-credential-set", + PROFILE, + "--origin", + origin, + "--auth", + "bearer", + "--revision", + &(before - 1).to_string(), + ], + SENTINEL.as_bytes(), + ); + assert_control_error(&output); + assert_no_secret(&output); + + assert_eq!(revision(data), before, "no refused command was recorded"); + #[cfg(windows)] + assert!( + !data.join("credentials-v1").exists(), + "no native credential was stored" + ); + assert_no_secret_in_files(data); +} + +/// A fetch to an origin without a binding sends no header, even when another +/// origin of the profile has one. +#[test] +fn a_fetch_to_an_unbound_origin_sends_no_authorization() { + let fixture = Fixture::start(); + let scene = scene(&fixture.origin()); + // Bind the profile's other origin; the fixture origin stays unbound. + let output = bind( + &scene, + "https://cdn.example.com/files/", + "bearer", + SENTINEL.as_bytes(), + ); + assert_success(&output); + let output = fetch(&scene, &format!("{}pkg.bin", fixture.origin()), "pkg.bin"); + assert_success(&output); + assert!(json_output(&output)["fetch"].get("credential_id").is_none()); + let head = fixture.finish(); + assert!( + !head.to_ascii_lowercase().contains("authorization:"), + "{head}" + ); + assert_no_secret(&output); + assert_no_secret_in_files(&scene.data); +} + +/// Removing a profile deletes every credential bound to its origins. +#[test] +fn removing_a_package_profile_deletes_its_credentials() { + let scene = scene("http://127.0.0.1:9/fixtures/"); + let data = &scene.data; + let mut ids = Vec::new(); + for origin in [ + "http://127.0.0.1:9/fixtures/", + "https://cdn.example.com/files/", + ] { + let output = bind(&scene, origin, "bearer", SENTINEL.as_bytes()); + assert_success(&output); + ids.push(credential_id(&json_output(&output))); + } + let current = revision(data).to_string(); + let output = run_cli( + data, + &[ + "workspace", + "package-profile-remove", + PROFILE, + "--revision", + ¤t, + ], + ); + assert_success(&output); + assert_no_secret(&output); + let removed = json_output(&output); + assert!(removed.get("warning").is_none(), "{removed}"); + let listed = removed["removed_credential_ids"] + .as_array() + .expect("removed ids"); + assert_eq!(listed.len(), 2); + for id in &ids { + assert!(listed.iter().any(|value| value == id.as_str())); + assert_absent(data, id); + } + assert_control_error(&run_cli( + data, + &["workspace", "package-profile-show", PROFILE], + )); + assert_no_secret_in_files(data); +} + +/// Capabilities report that registry credentials are available with the +/// native store. +#[test] +fn capabilities_report_registry_credentials() { + let temporary = TempDir::new().expect("temporary directory"); + let output = run_cli(&temporary.path().join("unused"), &["capabilities"]); + assert_success(&output); + assert_eq!( + json_output(&output)["package_registry_credentials"], + cfg!(any(windows, target_os = "linux")) + ); +} diff --git a/crates/omgcode-core/src/control.rs b/crates/omgcode-core/src/control.rs index 9c8ca7d..1389e83 100644 --- a/crates/omgcode-core/src/control.rs +++ b/crates/omgcode-core/src/control.rs @@ -43,16 +43,18 @@ pub use credential::{ CredentialBackend, CredentialBinding, CredentialError, CredentialId, CredentialStore, CredentialStoreStatus, LoadedProviderConnection, MAX_SECRET_BYTES, OrphanedCredential, ProviderConnection, ProviderCredentialError, ProviderCredentialRemoval, ProviderCredentialSet, - ProviderCredentialUseError, ProviderProfileRemoval, Secret, delete_unbound_credential, - probe_credential_store, + ProviderCredentialUseError, ProviderProfileRemoval, Secret, Zeroizing, + delete_unbound_credential, probe_credential_store, }; /// Whether this platform can record and apply container grants. pub use omgcode_platform::container::CONTAINER_GRANTS_SUPPORTED; pub use package::{ - MAX_PACKAGE_ORIGIN_BYTES, MAX_PACKAGE_ORIGINS, MAX_PACKAGE_OUTPUT_BYTES, MAX_PACKAGE_PROFILES, - PackageFetchFailure, PackageFetchFinish, PackageFetchOutcome, PackageFetchStart, PackageOrigin, - PackageProfile, PackageProfileDeclaration, PackageProfileStatus, PackageScheme, parse_origin, - valid_output_name, + LoadedPackageCredential, MAX_PACKAGE_ORIGIN_BYTES, MAX_PACKAGE_ORIGINS, + MAX_PACKAGE_OUTPUT_BYTES, MAX_PACKAGE_PROFILES, PackageAuth, PackageCredentialError, + PackageCredentialRemoval, PackageCredentialSet, PackageCredentialStatus, + PackageCredentialUseError, PackageFetchFailure, PackageFetchFinish, PackageFetchOutcome, + PackageFetchStart, PackageOrigin, PackageProfile, PackageProfileDeclaration, + PackageProfileRemoval, PackageProfileStatus, PackageScheme, parse_origin, valid_output_name, }; pub use profile::{ MAX_PROFILE_NAME_CHARS, MAX_PROFILE_PATH_ENTRIES, MAX_PROFILE_ROOTS, MAX_PROFILE_VARIABLES, @@ -361,6 +363,27 @@ enum Action { }, PackageProfileRemoved { profile_id: Uuid, + /// The credentials whose bindings this removal also ends. Absent in + /// earlier journals and for a profile with no binding. + #[serde( + default, + skip_serializing_if = "Vec::is_empty", + with = "credential::id_list_text" + )] + credential_ids: Vec, + }, + PackageCredentialBound { + profile_id: Uuid, + /// The profile origin's canonical text ([`PackageOrigin::render`]). + origin: String, + auth: PackageAuth, + binding: CredentialBinding, + }, + PackageCredentialRemoved { + profile_id: Uuid, + origin: String, + #[serde(with = "credential::id_text")] + credential_id: CredentialId, }, PackageFetchStarted { start: PackageFetchStart, @@ -1266,6 +1289,8 @@ impl ControlStore { } Action::PackageProfileAdded { .. } | Action::PackageProfileRemoved { .. } + | Action::PackageCredentialBound { .. } + | Action::PackageCredentialRemoved { .. } | Action::PackageFetchStarted { .. } | Action::PackageFetchFinished { .. } => { package::apply(state, &entry.action, revision)?; diff --git a/crates/omgcode-core/src/control/credential.rs b/crates/omgcode-core/src/control/credential.rs index c70f5b7..d21b199 100644 --- a/crates/omgcode-core/src/control/credential.rs +++ b/crates/omgcode-core/src/control/credential.rs @@ -25,7 +25,7 @@ use uuid::Uuid; pub use omgcode_platform::credentials::{ CredentialBackend, CredentialError, CredentialId, CredentialStore, CredentialStoreStatus, - MAX_SECRET_BYTES, Secret, probe as probe_credential_store, + MAX_SECRET_BYTES, Secret, Zeroizing, probe as probe_credential_store, }; use super::{ @@ -41,6 +41,11 @@ pub struct CredentialBinding { } impl CredentialBinding { + /// A binding for an item this process just stored. + pub(crate) const fn new(id: CredentialId, backend: CredentialBackend) -> Self { + Self { id, backend } + } + /// The opaque credential identifier. It is not secret. pub const fn id(&self) -> &CredentialId { &self.id @@ -125,6 +130,31 @@ pub(super) mod optional_id_text { } } +/// Serde helpers for a list of recorded credential identifiers. +pub(super) mod id_list_text { + use super::CredentialId; + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + + pub(crate) fn serialize( + ids: &[CredentialId], + serializer: S, + ) -> Result { + ids.iter() + .map(CredentialId::as_str) + .collect::>() + .serialize(serializer) + } + + pub(crate) fn deserialize<'de, D: Deserializer<'de>>( + deserializer: D, + ) -> Result, D::Error> { + Vec::::deserialize(deserializer)? + .iter() + .map(|text| CredentialId::parse(text).map_err(serde::de::Error::custom)) + .collect() + } +} + /// Operator-facing binding state: `{"state":"none"}` or /// `{"state":"bound","id":...,"backend":...}`. pub(super) fn serialize_state( @@ -752,7 +782,7 @@ pub fn delete_unbound_credential( /// Report a failed native store. When the failure may have left an item for /// `id`, delete it; an item that cannot be deleted or confirmed absent is /// reported with its identifier so it is never lost. -fn store_failure( +pub(super) fn store_failure( credentials: &CredentialStore, id: CredentialId, error: CredentialError, @@ -812,7 +842,7 @@ fn store_may_leave_item(error: &CredentialError) -> bool { /// complete event on disk that replay accepts. A failed seek writes nothing, /// but it shares that variant, so it is treated as uncertain too. Listing /// every variant makes a new one choose its category explicitly. -fn binding_write_uncertain(error: &ControlError) -> bool { +pub(super) fn binding_write_uncertain(error: &ControlError) -> bool { match error { ControlError::Io(_) => true, ControlError::Invalid(_) @@ -872,6 +902,15 @@ pub(super) fn apply(state: &mut ControlSnapshot, action: &Action) -> Result<(), "a credential is bound to more than one provider profile", )); } + if state + .package_profiles + .iter() + .any(|status| status.binds_credential(binding.id())) + { + return Err(ControlError::Invalid( + "a credential is bound to both a provider profile and a package origin", + )); + } let status = state .provider_profiles .iter_mut() diff --git a/crates/omgcode-core/src/control/package.rs b/crates/omgcode-core/src/control/package.rs index 113a0ee..08739e3 100644 --- a/crates/omgcode-core/src/control/package.rs +++ b/crates/omgcode-core/src/control/package.rs @@ -12,9 +12,18 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; +use super::credential::{CredentialBinding, CredentialId}; use super::{Action, ControlError, ControlSnapshot, ControlStore, valid_profile_name, valid_uuid}; use crate::policy::WorkspaceId; +mod credential; + +pub use credential::{ + LoadedPackageCredential, PackageAuth, PackageCredentialError, PackageCredentialRemoval, + PackageCredentialSet, PackageCredentialStatus, PackageCredentialUseError, + PackageProfileRemoval, +}; + /// Most recorded package-network profiles. pub const MAX_PACKAGE_PROFILES: usize = 32; /// Most registry origins in one profile. @@ -80,6 +89,19 @@ impl PackageOrigin { pub fn path_prefix(&self) -> &str { &self.path_prefix } + + /// The origin's canonical text: `scheme://host:port/prefix/`, with a + /// lowercase host and an explicit port. A credential binding names its + /// origin by this text, so recording and matching use one spelling. + pub fn render(&self) -> String { + format!( + "{}://{}:{}{}", + self.scheme.as_str(), + self.host.to_ascii_lowercase(), + self.port, + self.path_prefix + ) + } } /// A recorded package-network profile. @@ -114,6 +136,10 @@ pub struct PackageProfileStatus { #[serde(flatten)] profile: PackageProfile, revision: u64, + /// Registry credentials bound to origins of this profile. Opaque + /// identifiers only, never a secret. + #[serde(skip_serializing_if = "Vec::is_empty")] + pub(super) credentials: Vec, } impl PackageProfileStatus { @@ -126,6 +152,26 @@ impl PackageProfileStatus { pub const fn revision(&self) -> u64 { self.revision } + + /// The registry credentials bound to this profile's origins. + pub fn credentials(&self) -> &[PackageCredentialStatus] { + &self.credentials + } + + /// The credential bound to `origin`, if any. + pub fn credential_for(&self, origin: &PackageOrigin) -> Option<&PackageCredentialStatus> { + let text = origin.render(); + self.credentials + .iter() + .find(|credential| credential.origin() == text) + } + + /// Whether any origin of this profile is bound to credential `id`. + pub(super) fn binds_credential(&self, id: &CredentialId) -> bool { + self.credentials + .iter() + .any(|credential| credential.binding().id() == id) + } } /// An operator's profile declaration, before any origin is parsed. @@ -154,6 +200,13 @@ pub struct PackageFetchStart { pub url_sha256: String, /// The cache file name. pub output_name: String, + /// The credential attached to the request, when one was. Never the secret. + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "super::credential::optional_id_text" + )] + pub credential_id: Option, /// The greatest response body accepted. pub max_bytes: u64, /// The request deadline. @@ -330,22 +383,39 @@ impl ControlStore { /// Remove a package-network profile by name. This advances the settings /// revision. + /// + /// Every credential bound to the profile's origins is unbound by the same + /// recorded event, which names their identifiers. The caller then deletes + /// the native items with [`super::delete_unbound_credential`]. pub fn remove_package_profile( &mut self, name: &str, expected_revision: u64, - ) -> Result { + ) -> Result { self.check_revision(expected_revision)?; - let profile_id = self + let status = self .package_profile(name) - .ok_or(ControlError::UnknownPackageProfile)? - .profile - .id; + .ok_or(ControlError::UnknownPackageProfile)?; + let profile_id = status.profile.id; + let credentials: Vec = status + .credentials + .iter() + .map(|credential| credential.binding().clone()) + .collect(); self.append( - Action::PackageProfileRemoved { profile_id }, + Action::PackageProfileRemoved { + profile_id, + credential_ids: credentials + .iter() + .map(|binding| binding.id().clone()) + .collect(), + }, self.next_revision()?, )?; - Ok(profile_id) + Ok(PackageProfileRemoval { + profile_id, + credentials, + }) } /// Record the intent of a trusted package fetch. This preserves the @@ -437,9 +507,13 @@ pub(super) fn apply( state.package_profiles.push(PackageProfileStatus { profile: profile.clone(), revision, + credentials: Vec::new(), }); } - Action::PackageProfileRemoved { profile_id } => { + Action::PackageProfileRemoved { + profile_id, + credential_ids, + } => { let index = state .package_profiles .iter() @@ -447,8 +521,27 @@ pub(super) fn apply( .ok_or(ControlError::Invalid( "removal of an unknown package profile", ))?; + // Every bound credential must be named by the removal itself, so + // the journal always shows which native items became unbound. + let bound = state + .package_profiles + .get(index) + .map(|status| status.credentials.as_slice()) + .unwrap_or_default(); + if bound.len() != credential_ids.len() + || !bound + .iter() + .all(|credential| credential_ids.contains(credential.binding().id())) + { + return Err(ControlError::Invalid( + "package profile removal does not match its credential bindings", + )); + } state.package_profiles.remove(index); } + Action::PackageCredentialBound { .. } | Action::PackageCredentialRemoved { .. } => { + credential::apply(state, action)?; + } Action::PackageFetchStarted { start } => { validate_fetch_start(start) .map_err(|_| ControlError::Invalid("invalid package fetch start"))?; diff --git a/crates/omgcode-core/src/control/package/credential.rs b/crates/omgcode-core/src/control/package/credential.rs new file mode 100644 index 0000000..0451a08 --- /dev/null +++ b/crates/omgcode-core/src/control/package/credential.rs @@ -0,0 +1,619 @@ +//! Registry credential bindings for package-network profiles. See ADR-0056. +//! +//! A binding names one profile, one of that profile's origins, an +//! authentication scheme, and an opaque credential identifier. The journal +//! never records a secret. Secrets live only in the platform credential store, +//! and a credential is never overwritten: rotation stores a new credential, +//! records the binding, and only then deletes the old item, with the same +//! recovery rules as provider credentials (ADR-0042). +//! +//! Only the trusted `packages fetch` command reads a bound credential, and it +//! sends it to the one origin the binding names ([`ControlStore::load_package_credential`]). +//! No run, model tool, or provider adapter reads it. + +use serde::{Deserialize, Serialize}; +use thiserror::Error; +use uuid::Uuid; + +use super::{PackageOrigin, parse_origin, refused}; +use crate::control::credential::{ + CredentialBackend, CredentialBinding, CredentialError, CredentialId, CredentialStore, + OrphanedCredential, ProviderCredentialError, Secret, binding_write_uncertain, + delete_unbound_credential, store_failure, +}; +use crate::control::{Action, ControlError, ControlSnapshot, ControlStore}; + +/// How a bound registry credential is sent. +#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum PackageAuth { + /// `Authorization: Bearer `. + Bearer, + /// `Authorization: Basic `; the secret is `user:password`. + Basic, +} + +impl PackageAuth { + /// The scheme as the operator spells it. + pub const fn as_str(self) -> &'static str { + match self { + Self::Bearer => "bearer", + Self::Basic => "basic", + } + } + + /// Parse an operator's spelling. + pub fn parse(text: &str) -> Option { + match text { + "bearer" => Some(Self::Bearer), + "basic" => Some(Self::Basic), + _ => None, + } + } +} + +/// One credential bound to one origin of a package-network profile. +#[derive(Clone, Debug, Serialize)] +pub struct PackageCredentialStatus { + origin: String, + auth: PackageAuth, + binding: CredentialBinding, +} + +impl PackageCredentialStatus { + /// The origin's canonical text ([`PackageOrigin::render`]). + pub fn origin(&self) -> &str { + &self.origin + } + + /// How the credential is sent. + pub const fn auth(&self) -> PackageAuth { + self.auth + } + + /// The opaque credential identifier and its backend. + pub const fn binding(&self) -> &CredentialBinding { + &self.binding + } +} + +/// The outcome of removing a package profile. +#[derive(Debug)] +#[must_use] +pub struct PackageProfileRemoval { + /// The removed profile. + pub profile_id: Uuid, + /// The bindings the same event ended. The caller deletes each native + /// item with [`delete_unbound_credential`]. + pub credentials: Vec, +} + +/// The outcome of storing and binding a new registry credential. +#[derive(Debug)] +#[must_use] +pub struct PackageCredentialSet { + /// The profile that now holds the binding. + pub profile_id: Uuid, + /// The origin's canonical text. + pub origin: String, + /// How the credential is sent. + pub auth: PackageAuth, + /// The new binding. + pub binding: CredentialBinding, + /// The credential this binding replaced, if any. + pub replaced: Option, + /// The replaced credential, when its native item could not be deleted. + pub orphan: Option, +} + +/// The outcome of removing a binding and its native item. +#[derive(Debug)] +#[must_use] +pub struct PackageCredentialRemoval { + /// The profile whose binding was removed. + pub profile_id: Uuid, + /// The origin's canonical text. + pub origin: String, + /// The removed credential's identifier. + pub removed: CredentialId, + /// The removed credential, when its native item could not be deleted. + pub orphan: Option, +} + +/// A registry credential command failure. Journal and native-store +/// categories stay distinct, and no message contains secret bytes. +#[derive(Debug, Error)] +pub enum PackageCredentialError { + /// The settings journal refused or failed the change. + #[error(transparent)] + Control(#[from] ControlError), + /// The native store refused or failed. Nothing was recorded. + #[error(transparent)] + Credential(#[from] CredentialError), + /// The new credential was stored, its binding was not recorded, and the + /// new native item could not be deleted. It is an unbound orphan. + #[error( + "the binding was not recorded ({error}), and the new credential {credential_id} could not be deleted: {cleanup}" + )] + Unrecorded { + /// Why the binding was not recorded. + #[source] + error: ControlError, + /// The orphaned credential's identifier. + credential_id: CredentialId, + /// Why it could not be deleted. + cleanup: CredentialError, + }, + /// The native store failed after it may have created an item for the new + /// identifier, and that item could not be deleted or confirmed absent. + #[error( + "the credential was not stored ({error}), and an item for the new credential {credential_id} may remain and could not be deleted: {cleanup}" + )] + Unstored { + /// Why the store failed. + #[source] + error: CredentialError, + /// The identifier any remaining item has. + credential_id: CredentialId, + /// Why it could not be deleted. + cleanup: CredentialError, + }, + /// The new credential was stored, but the binding's journal write or + /// sync failed after it began, so the binding may or may not be + /// recorded. The new item is kept and any replaced credential was not + /// deleted. + #[error( + "the binding of the new credential {credential_id} may or may not be recorded ({error}); the credential was kept. Run package-profile-show: if it shows this identifier, keep it or remove it with package-credential-remove; otherwise delete the unbound item by hand" + )] + BindingUncertain { + /// Why the binding write is uncertain. + #[source] + error: ControlError, + /// The kept credential's identifier. + credential_id: CredentialId, + /// The credential that was bound before, if any. If the new binding + /// is recorded, it is no longer bound and was not deleted. + replaced: Option, + }, +} + +impl From for PackageCredentialError { + fn from(error: ProviderCredentialError) -> Self { + match error { + ProviderCredentialError::Control(error) => Self::Control(error), + ProviderCredentialError::Credential(error) => Self::Credential(error), + ProviderCredentialError::Unrecorded { + error, + credential_id, + cleanup, + } => Self::Unrecorded { + error, + credential_id, + cleanup, + }, + ProviderCredentialError::Unstored { + error, + credential_id, + cleanup, + } => Self::Unstored { + error, + credential_id, + cleanup, + }, + ProviderCredentialError::BindingUncertain { + error, + credential_id, + replaced, + } => Self::BindingUncertain { + error, + credential_id, + replaced, + }, + } + } +} + +/// A refusal or failure to read a bound registry credential for a fetch. +/// +/// No message contains secret bytes; an opaque credential identifier may +/// appear. A native failure keeps its category. +#[derive(Debug, Error)] +pub enum PackageCredentialUseError { + /// The settings store cannot be used. + #[error("the settings store cannot be used: {0}")] + Settings(#[source] ControlError), + /// The profile is no longer recorded. + #[error("the package profile is not recorded")] + UnknownProfile, + /// The bound credential was stored by a different backend than the one + /// this host opened. + #[error( + "credential {credential_id} was stored by {}, not {}", + .stored.as_str(), + .current.as_str() + )] + OtherBackend { + /// The bound credential's identifier. + credential_id: CredentialId, + /// The backend recorded in the binding. + stored: CredentialBackend, + /// The backend of the open credential store. + current: CredentialBackend, + }, + /// The native store could not return the bound credential. + #[error("credential {credential_id} could not be read: {error}")] + Store { + /// The bound credential's identifier. + credential_id: CredentialId, + /// The native failure, with its category. + #[source] + error: CredentialError, + }, +} + +/// A registry credential read for one fetch. The secret zeroes itself on drop +/// and `Debug` redacts it. +pub struct LoadedPackageCredential { + auth: PackageAuth, + credential_id: CredentialId, + secret: Secret, +} + +impl LoadedPackageCredential { + /// How the credential is sent. + pub const fn auth(&self) -> PackageAuth { + self.auth + } + + /// The credential's opaque identifier. + pub const fn credential_id(&self) -> &CredentialId { + &self.credential_id + } + + /// The secret. It must not be logged, displayed, or persisted. + pub const fn secret(&self) -> &Secret { + &self.secret + } +} + +impl std::fmt::Debug for LoadedPackageCredential { + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter + .debug_struct("LoadedPackageCredential") + .field("auth", &self.auth) + .field("credential_id", &self.credential_id) + .field("secret", &"") + .finish() + } +} + +/// Check a secret before anything is stored: visible ASCII only, no control +/// characters or line breaks, no leading or trailing whitespace. A bearer +/// token has no space at all; a basic secret is `user:password` with a +/// nonempty user name before the first colon. +/// +/// # Errors +/// +/// Returns [`ControlError::PackageProfileRefused`] with a fixed reason that +/// never contains the secret. +pub fn validate_package_secret(auth: PackageAuth, secret: &Secret) -> Result<(), ControlError> { + let bytes = secret.expose(); + let (Some(first), Some(last)) = (bytes.first(), bytes.last()) else { + return Err(refused("a registry credential must not be empty")); + }; + if bytes.len() > crate::control::MAX_SECRET_BYTES { + return Err(refused("a registry credential is too long")); + } + if !bytes.iter().all(|byte| (0x20..=0x7e).contains(byte)) { + return Err(refused( + "a registry credential must be visible ASCII without control characters or line breaks", + )); + } + if *first == b' ' || *last == b' ' { + return Err(refused( + "a registry credential must not start or end with whitespace", + )); + } + match auth { + PackageAuth::Bearer => { + if bytes.contains(&b' ') { + return Err(refused("a bearer token must not contain whitespace")); + } + } + PackageAuth::Basic => { + let user = bytes.split(|byte| *byte == b':').next().unwrap_or_default(); + if !bytes.contains(&b':') || user.is_empty() { + return Err(refused( + "a basic credential must be user:password with a nonempty user", + )); + } + } + } + Ok(()) +} + +impl ControlStore { + /// Store `secret` as a new credential and bind it to `origin` of the named + /// package profile, then delete any credential it replaces. + /// + /// The order and the recovery rules are those of + /// [`ControlStore::set_provider_credential`]: the native store, then the + /// journal binding, then the old item's deletion. A binding refused before + /// any journal write deletes the new item; a binding whose journal write + /// may have happened keeps it and reports + /// [`PackageCredentialError::BindingUncertain`]. + /// + /// # Errors + /// + /// The revision, profile, origin, and secret checks run before any native + /// I/O and return [`PackageCredentialError::Control`]. + pub fn set_package_credential( + &mut self, + credentials: &CredentialStore, + name: &str, + origin: &str, + auth: PackageAuth, + secret: &Secret, + expected_revision: u64, + ) -> Result { + self.check_revision(expected_revision)?; + let (profile_id, origin) = self.credential_target(name, origin)?; + validate_package_secret(auth, secret)?; + let replaced = self.bound_package_credential(profile_id, &origin); + let id = CredentialId::parse(&Uuid::new_v4().simple().to_string())?; + if let Err(error) = credentials.store(&id, secret) { + return Err(store_failure(credentials, id, error).into()); + } + let binding = CredentialBinding::new(id.clone(), credentials.backend()); + let appended = self.next_revision().and_then(|revision| { + self.append( + Action::PackageCredentialBound { + profile_id, + origin: origin.clone(), + auth, + binding: binding.clone(), + }, + revision, + ) + }); + if let Err(error) = appended { + if binding_write_uncertain(&error) { + return Err(PackageCredentialError::BindingUncertain { + error, + credential_id: id, + replaced: replaced.map(|old| old.id().clone()), + }); + } + return Err(match credentials.delete(&id) { + Ok(()) | Err(CredentialError::NotFound) => PackageCredentialError::Control(error), + Err(cleanup) => PackageCredentialError::Unrecorded { + error, + credential_id: id, + cleanup, + }, + }); + } + let orphan = replaced + .as_ref() + .and_then(|old| delete_unbound_credential(credentials, old)); + Ok(PackageCredentialSet { + profile_id, + origin, + auth, + binding, + replaced: replaced.map(|old| old.id().clone()), + orphan, + }) + } + + /// Record that `origin` of the named profile no longer has a credential, + /// then delete the native item. A failed deletion is returned as an + /// orphan. + /// + /// # Errors + /// + /// Refuses a stale revision, an unknown profile or origin, or an origin + /// without a binding before any native I/O. + pub fn remove_package_credential( + &mut self, + credentials: &CredentialStore, + name: &str, + origin: &str, + expected_revision: u64, + ) -> Result { + self.check_revision(expected_revision)?; + let (profile_id, origin) = self.credential_target(name, origin)?; + let binding = self + .bound_package_credential(profile_id, &origin) + .ok_or_else(|| refused("no credential is bound to this registry origin"))?; + self.append( + Action::PackageCredentialRemoved { + profile_id, + origin: origin.clone(), + credential_id: binding.id().clone(), + }, + self.next_revision()?, + )?; + let orphan = delete_unbound_credential(credentials, &binding); + Ok(PackageCredentialRemoval { + profile_id, + origin, + removed: binding.id().clone(), + orphan, + }) + } + + /// Read the credential bound to `origin` of profile `profile_id` for one + /// fetch, or `None` when the origin has no binding. This takes the origin + /// the fetch matched, so a credential is never paired with another + /// destination. + /// + /// The caller must keep this `ControlStore` open across the call, so a + /// rotation (which needs `&mut self` and the journal lock) cannot delete + /// the item between the lookup and the load, and must drop it before any + /// network I/O. A read records nothing. + /// + /// # Errors + /// + /// See [`PackageCredentialUseError`]. A binding that cannot be read is an + /// error, never `None`: the caller must not send an unauthenticated + /// request in its place. + pub fn load_package_credential( + &self, + credentials: &CredentialStore, + profile_id: Uuid, + origin: &PackageOrigin, + ) -> Result, PackageCredentialUseError> { + self.ensure_healthy() + .map_err(PackageCredentialUseError::Settings)?; + let status = self + .package_profiles() + .iter() + .find(|status| status.profile().id() == profile_id) + .ok_or(PackageCredentialUseError::UnknownProfile)?; + let Some(bound) = status.credential_for(origin) else { + return Ok(None); + }; + let binding = bound.binding(); + if binding.backend() != credentials.backend() { + return Err(PackageCredentialUseError::OtherBackend { + credential_id: binding.id().clone(), + stored: binding.backend(), + current: credentials.backend(), + }); + } + let secret = + credentials + .load(binding.id()) + .map_err(|error| PackageCredentialUseError::Store { + credential_id: binding.id().clone(), + error, + })?; + Ok(Some(LoadedPackageCredential { + auth: bound.auth(), + credential_id: binding.id().clone(), + secret, + })) + } + + /// Resolve the profile and one of its origins, by the origin's canonical + /// text. The origin must be one the profile declares. + fn credential_target(&self, name: &str, origin: &str) -> Result<(Uuid, String), ControlError> { + let status = self + .package_profile(name) + .ok_or(ControlError::UnknownPackageProfile)?; + let text = parse_origin(origin)?.render(); + if !status + .profile() + .origins() + .iter() + .any(|declared| declared.render() == text) + { + return Err(refused( + "the origin is not one declared by this package-network profile", + )); + } + Ok((status.profile().id(), text)) + } + + /// The binding an origin holds now, which a new binding would replace. + fn bound_package_credential( + &self, + profile_id: Uuid, + origin: &str, + ) -> Option { + self.package_profiles() + .iter() + .find(|status| status.profile().id() == profile_id)? + .credentials + .iter() + .find(|credential| credential.origin == origin) + .map(|credential| credential.binding.clone()) + } +} + +/// Validate and apply one package-credential action during replay or append +/// preflight. +pub(super) fn apply(state: &mut ControlSnapshot, action: &Action) -> Result<(), ControlError> { + match action { + Action::PackageCredentialBound { + profile_id, + origin, + auth, + binding, + } => { + let bound_elsewhere = state + .package_profiles + .iter() + .any(|status| status.binds_credential(binding.id())) + || state.provider_profiles.iter().any(|status| { + status + .credential() + .is_some_and(|other| other.id() == binding.id()) + }); + if bound_elsewhere { + return Err(ControlError::Invalid( + "a credential is already bound to a package origin or provider profile", + )); + } + let status = state + .package_profiles + .iter_mut() + .find(|status| status.profile().id() == *profile_id) + .ok_or(ControlError::Invalid( + "credential binding for an unknown package profile", + ))?; + if !status + .profile() + .origins() + .iter() + .any(|declared| declared.render() == *origin) + { + return Err(ControlError::Invalid( + "credential binding for an origin the profile does not declare", + )); + } + let entry = PackageCredentialStatus { + origin: origin.clone(), + auth: *auth, + binding: binding.clone(), + }; + match status + .credentials + .iter_mut() + .find(|current| current.origin == *origin) + { + Some(current) => *current = entry, + None => status.credentials.push(entry), + } + } + Action::PackageCredentialRemoved { + profile_id, + origin, + credential_id, + } => { + let status = state + .package_profiles + .iter_mut() + .find(|status| status.profile().id() == *profile_id) + .ok_or(ControlError::Invalid( + "credential removal for an unknown package profile", + ))?; + let index = status + .credentials + .iter() + .position(|current| { + current.origin == *origin && current.binding.id() == credential_id + }) + .ok_or(ControlError::Invalid( + "credential removal does not match the recorded binding", + ))?; + status.credentials.remove(index); + } + _ => {} + } + Ok(()) +} + +#[cfg(test)] +mod tests; diff --git a/crates/omgcode-core/src/control/package/credential/tests.rs b/crates/omgcode-core/src/control/package/credential/tests.rs new file mode 100644 index 0000000..eb43e81 --- /dev/null +++ b/crates/omgcode-core/src/control/package/credential/tests.rs @@ -0,0 +1,815 @@ +//! Registry credential bindings against the real control journal and the real +//! native credential store. On Linux these need an unlocked Secret Service +//! default collection and fail without one. + +use super::*; +use std::fs; +use std::path::{Path, PathBuf}; + +use crate::control::{ + FILE_NAME, PackageProfileDeclaration, ProviderProfileDeclaration, + credential as provider_binding, +}; +use crate::test_support::private_data_root; + +const SENTINEL: &[u8] = b"omgcode-registry-sentinel-token-77c1"; +const REGISTRY: &str = "https://registry.example.com/api/"; +const CDN: &str = "https://cdn.example.com/files/"; + +struct Fixture { + data: PathBuf, + credentials: CredentialStore, + _directory: tempfile::TempDir, +} + +impl Fixture { + fn store(&self) -> ControlStore { + ControlStore::open(&self.data).expect("control store") + } +} + +/// A private data root with one two-origin package profile and one `api_key` +/// provider profile. +fn fixture() -> Fixture { + let directory = tempfile::tempdir().expect("temporary directory"); + let data = directory.path().join("data"); + private_data_root(&data); + let mut store = ControlStore::open(&data).expect("control store"); + let revision = store.revision(); + store + .add_package_profile( + &PackageProfileDeclaration { + name: "registries".into(), + origins: vec![REGISTRY.into(), CDN.into()], + }, + revision, + ) + .expect("package profile"); + let revision = store.revision(); + store + .add_provider_profile( + &ProviderProfileDeclaration { + name: "keys".into(), + kind: "openai".into(), + auth: "api_key".into(), + endpoint: None, + }, + revision, + ) + .expect("provider profile"); + drop(store); + let credentials = CredentialStore::open(&data) + .expect("a native credential store is required (on Linux, an unlocked Secret Service)"); + Fixture { + data, + credentials, + _directory: directory, + } +} + +fn secret(bytes: &[u8]) -> Secret { + Secret::new(bytes.to_vec()).expect("secret") +} + +fn journal(data: &Path) -> Vec { + fs::read(data.join(FILE_NAME)).expect("journal bytes") +} + +fn contains(haystack: &[u8], needle: &[u8]) -> bool { + haystack + .windows(needle.len()) + .any(|window| window == needle) +} + +fn new_id() -> CredentialId { + CredentialId::parse(&Uuid::new_v4().simple().to_string()).expect("id") +} + +fn origin(text: &str) -> PackageOrigin { + parse_origin(text).expect("origin") +} + +fn profile_id(store: &ControlStore) -> Uuid { + store + .package_profile("registries") + .expect("profile") + .profile() + .id() +} + +/// Binding, rotating, and unbinding are revisioned, replayed, reported, and +/// never put secret bytes in the journal. +#[test] +fn bindings_are_recorded_rotated_replayed_and_removed_without_secret_bytes() { + let fixture = fixture(); + let (data, credentials) = (&fixture.data, &fixture.credentials); + let mut store = fixture.store(); + let revision = store.revision(); + // A differently spelled origin resolves to the declared one. + let set = store + .set_package_credential( + credentials, + "REGISTRIES", + "https://Registry.Example.com:443/api", + PackageAuth::Bearer, + &secret(SENTINEL), + revision, + ) + .expect("set credential"); + assert!(set.replaced.is_none() && set.orphan.is_none()); + assert_eq!(set.origin, "https://registry.example.com:443/api/"); + assert_eq!(store.revision(), revision + 1); + assert_eq!( + credentials.load(set.binding.id()).expect("stored").expose(), + SENTINEL + ); + let status = store.package_profile("registries").expect("profile"); + let bound = status.credential_for(&origin(REGISTRY)).expect("bound"); + assert_eq!(bound.auth(), PackageAuth::Bearer); + assert_eq!(bound.binding(), &set.binding); + assert!(status.credential_for(&origin(CDN)).is_none()); + let encoded = serde_json::to_value(store.snapshot()).expect("snapshot"); + let shown = &encoded["package_profiles"][0]["credentials"][0]; + assert_eq!(shown["origin"], "https://registry.example.com:443/api/"); + assert_eq!(shown["auth"], "bearer"); + assert_eq!(shown["binding"]["credential_id"], set.binding.id().as_str()); + assert_eq!(shown["binding"]["backend"], credentials.backend().as_str()); + + // Rotation: new identifier, new auth, old item deleted after the bind. + let rotated = store + .set_package_credential( + credentials, + "registries", + REGISTRY, + PackageAuth::Basic, + &secret(b"alice:hunter2-sentinel"), + revision + 1, + ) + .expect("rotate"); + assert_eq!(rotated.replaced.as_ref(), Some(set.binding.id())); + assert!(rotated.orphan.is_none()); + assert_ne!(rotated.binding.id(), set.binding.id()); + assert!(matches!( + credentials.load(set.binding.id()), + Err(CredentialError::NotFound) + )); + drop(store); + + let mut store = fixture.store(); + let status = store.package_profile("registries").expect("replayed"); + assert_eq!(status.credentials().len(), 1); + assert_eq!(status.credentials()[0].auth(), PackageAuth::Basic); + assert_eq!(status.credentials()[0].binding(), &rotated.binding); + + let revision = store.revision(); + let removal = store + .remove_package_credential(credentials, "registries", REGISTRY, revision) + .expect("remove"); + assert_eq!(&removal.removed, rotated.binding.id()); + assert!(removal.orphan.is_none()); + assert_eq!(store.revision(), revision + 1); + assert!(matches!( + credentials.load(rotated.binding.id()), + Err(CredentialError::NotFound) + )); + assert!(matches!( + store.remove_package_credential(credentials, "registries", REGISTRY, revision + 1), + Err(PackageCredentialError::Control( + ControlError::PackageProfileRefused(_) + )) + )); + drop(store); + + let store = fixture.store(); + assert!( + store + .package_profile("registries") + .expect("profile") + .credentials() + .is_empty() + ); + let encoded = serde_json::to_value(store.snapshot()).expect("snapshot"); + assert!(encoded["package_profiles"][0].get("credentials").is_none()); + drop(store); + let bytes = journal(data); + assert!(!contains(&bytes, SENTINEL)); + assert!(!contains(&bytes, b"hunter2")); +} + +/// Stale revisions, unknown profiles and origins, and invalid secrets are +/// refused before any native write. +#[test] +fn refused_bindings_store_nothing_and_record_nothing() { + let fixture = fixture(); + let (data, credentials) = (&fixture.data, &fixture.credentials); + let mut store = fixture.store(); + let entries = store.entries.len(); + let bytes = store.bytes; + let revision = store.revision(); + let cases = [ + ("registries", REGISTRY, revision - 1), + ("missing", REGISTRY, revision), + ("registries", "https://other.example.com/", revision), + // Same host, but a path the profile does not declare. + ( + "registries", + "https://registry.example.com/other/", + revision, + ), + // Not an origin at all. + ( + "registries", + "https://registry.example.com/api/?x=1", + revision, + ), + ( + "registries", + "https://user:pw@registry.example.com/api/", + revision, + ), + ]; + for (name, origin_text, expected) in cases { + let result = store.set_package_credential( + credentials, + name, + origin_text, + PackageAuth::Bearer, + &secret(SENTINEL), + expected, + ); + assert!( + matches!(result, Err(PackageCredentialError::Control(_))), + "{name} {origin_text}: {result:?}" + ); + } + assert_eq!(store.revision(), revision); + assert_eq!(store.entries.len(), entries, "nothing was recorded"); + assert_eq!(store.bytes, bytes); + drop(store); + // On Windows the credential directory is created only by a store write, + // so its absence shows that no native write happened. + #[cfg(windows)] + assert!(!data.join("credentials-v1").exists()); + #[cfg(not(windows))] + let _ = data; +} + +/// Secret validation: visible ASCII only, no controls, no edge whitespace, +/// bearer without any space, basic with a nonempty user and a colon. +#[test] +fn invalid_secrets_are_refused_before_anything_is_stored() { + for (auth, bytes) in [ + (PackageAuth::Bearer, b"tok\ten".as_slice()), + (PackageAuth::Bearer, b"tok\ren"), + (PackageAuth::Bearer, b"tok\nen"), + (PackageAuth::Bearer, b"tok\0en"), + (PackageAuth::Bearer, b"tok\x7fen"), + (PackageAuth::Bearer, "tok\u{e9}en".as_bytes()), + (PackageAuth::Bearer, b" token"), + (PackageAuth::Bearer, b"token "), + (PackageAuth::Bearer, b"to ken"), + (PackageAuth::Basic, b"nocolon"), + (PackageAuth::Basic, b":password"), + (PackageAuth::Basic, b" alice:pw"), + (PackageAuth::Basic, b"alice:pw\n"), + (PackageAuth::Basic, b"alice:pw "), + (PackageAuth::Basic, "alice:p\u{e9}".as_bytes()), + ] { + let result = validate_package_secret(auth, &secret(bytes)); + assert!( + matches!(result, Err(ControlError::PackageProfileRefused(_))), + "{auth:?} {bytes:?}: {result:?}" + ); + } + for (auth, bytes) in [ + (PackageAuth::Bearer, b"abc.DEF_123-~+/=".as_slice()), + (PackageAuth::Basic, b"alice:pass word"), + (PackageAuth::Basic, b"a:"), + (PackageAuth::Basic, b"a:b:c"), + ] { + validate_package_secret(auth, &secret(bytes)) + .unwrap_or_else(|error| panic!("{auth:?} {bytes:?}: {error}")); + } + // The largest accepted secret is valid; one more byte is refused by the + // secret type itself. + let largest = vec![b'a'; crate::control::MAX_SECRET_BYTES]; + validate_package_secret(PackageAuth::Bearer, &secret(&largest)).expect("largest"); + assert!(Secret::new(vec![b'a'; crate::control::MAX_SECRET_BYTES + 1]).is_err()); + + // The refusal comes before the native store, the journal, and any item. + let fixture = fixture(); + let (data, credentials) = (&fixture.data, &fixture.credentials); + let mut store = fixture.store(); + let revision = store.revision(); + let entries = store.entries.len(); + let result = store.set_package_credential( + credentials, + "registries", + REGISTRY, + PackageAuth::Basic, + &secret(b"no-colon-sentinel"), + revision, + ); + assert!( + matches!( + &result, + Err(PackageCredentialError::Control( + ControlError::PackageProfileRefused(message) + )) if !message.contains("sentinel") + ), + "{result:?}" + ); + assert_eq!(store.entries.len(), entries); + drop(store); + #[cfg(windows)] + assert!(!data.join("credentials-v1").exists()); + #[cfg(not(windows))] + let _ = data; +} + +/// A binding write whose sync fails may already be on disk, so the new item +/// is kept and named, and the replaced item is not deleted. +#[test] +fn an_uncertain_binding_write_keeps_the_new_item_and_names_it() { + let fixture = fixture(); + let (data, credentials) = (&fixture.data, &fixture.credentials); + let mut store = fixture.store(); + let revision = store.revision(); + let first = store + .set_package_credential( + credentials, + "registries", + REGISTRY, + PackageAuth::Bearer, + &secret(b"first-token"), + revision, + ) + .expect("first"); + store.fail_next_sync = true; + let result = store.set_package_credential( + credentials, + "registries", + REGISTRY, + PackageAuth::Bearer, + &secret(SENTINEL), + revision + 1, + ); + let (kept, replaced) = match result { + Err(PackageCredentialError::BindingUncertain { + error: ControlError::Io(_), + credential_id, + replaced, + }) => (credential_id, replaced), + other => panic!("expected an uncertain binding: {other:?}"), + }; + assert_eq!(replaced.as_ref(), Some(first.binding.id())); + assert_eq!(credentials.load(&kept).expect("kept").expose(), SENTINEL); + assert_eq!( + credentials + .load(first.binding.id()) + .expect("replaced item kept") + .expose(), + b"first-token" + ); + drop(store); + + // The injected failure follows a completed write, so it is on disk. + let mut store = fixture.store(); + let binding = store + .package_profile("registries") + .expect("profile") + .credential_for(&origin(REGISTRY)) + .expect("uncertain write landed") + .binding() + .clone(); + assert_eq!(binding.id(), &kept); + let revision = store.revision(); + let removal = store + .remove_package_credential(credentials, "registries", REGISTRY, revision) + .expect("remove the kept binding"); + assert!(removal.orphan.is_none()); + credentials + .delete(first.binding.id()) + .expect("delete the replaced item by hand"); + drop(store); + assert!(!contains(&journal(data), SENTINEL)); +} + +/// A binding append refused before any write records nothing and deletes the +/// new item. +#[test] +fn a_binding_refused_before_any_write_deletes_the_new_item() { + let fixture = fixture(); + let (data, credentials) = (&fixture.data, &fixture.credentials); + let mut store = fixture.store(); + let revision = store.revision(); + let entries = store.entries.len(); + // A full journal refuses the append before anything is written. + store.bytes = crate::control::MAX_BYTES; + let result = store.set_package_credential( + credentials, + "registries", + REGISTRY, + PackageAuth::Bearer, + &secret(SENTINEL), + revision, + ); + assert!( + matches!( + result, + Err(PackageCredentialError::Control(ControlError::LimitReached)) + ), + "{result:?}" + ); + assert_eq!(store.entries.len(), entries); + drop(store); + let store = fixture.store(); + assert_eq!(store.revision(), revision, "nothing was recorded"); + assert!( + store + .package_profile("registries") + .expect("profile") + .credentials() + .is_empty() + ); + #[cfg(windows)] + assert_eq!( + fs::read_dir(data.join("credentials-v1")) + .expect("credential directory") + .count(), + 0 + ); + #[cfg(not(windows))] + let _ = data; +} + +/// The native store failure categories map through the shared helpers to the +/// package error without losing the orphan identifier. +#[test] +fn shared_store_failures_keep_their_package_categories() { + let id = new_id(); + let unstored = PackageCredentialError::from(ProviderCredentialError::Unstored { + error: CredentialError::Io(std::io::Error::other("directory sync failed")), + credential_id: id.clone(), + cleanup: CredentialError::Locked, + }); + assert!( + matches!(&unstored, PackageCredentialError::Unstored { credential_id, .. } if *credential_id == id), + "{unstored:?}" + ); + let uncertain = PackageCredentialError::from(ProviderCredentialError::BindingUncertain { + error: ControlError::Io(std::io::Error::other("sync failed")), + credential_id: id.clone(), + replaced: None, + }); + let message = uncertain.to_string(); + assert!(message.contains(id.as_str()), "{message}"); + assert!(message.contains("package-credential-remove"), "{message}"); + assert!(!message.contains("provider-"), "{message}"); +} + +/// Removing a profile ends every one of its bindings in the same event. +#[test] +fn profile_removal_ends_its_bindings_in_the_same_event() { + let fixture = fixture(); + let (data, credentials) = (&fixture.data, &fixture.credentials); + let mut store = fixture.store(); + let mut revision = store.revision(); + let mut bound = Vec::new(); + for (origin_text, auth) in [(REGISTRY, PackageAuth::Bearer), (CDN, PackageAuth::Basic)] { + let secret_bytes: &[u8] = if auth == PackageAuth::Basic { + b"user:pw-sentinel" + } else { + SENTINEL + }; + let set = store + .set_package_credential( + credentials, + "registries", + origin_text, + auth, + &secret(secret_bytes), + revision, + ) + .expect("set"); + bound.push(set.binding); + revision += 1; + } + let removal = store + .remove_package_profile("registries", revision) + .expect("remove profile"); + assert_eq!(removal.credentials.len(), 2); + for binding in &removal.credentials { + assert!(bound.contains(binding)); + assert!(delete_unbound_credential(credentials, binding).is_none()); + assert!(matches!( + credentials.load(binding.id()), + Err(CredentialError::NotFound) + )); + } + assert_eq!(store.revision(), revision + 1); + drop(store); + + let store = ControlStore::open(data).expect("replay"); + assert!(store.package_profile("registries").is_none()); + drop(store); + let text = String::from_utf8(journal(data)).expect("journal text"); + let removed = text + .lines() + .find(|line| line.contains("package_profile_removed")) + .expect("removal event"); + for binding in &bound { + assert!(removed.contains(binding.id().as_str()), "{removed}"); + } + assert!(!text.contains("sentinel")); +} + +/// A profile removal without bindings keeps the earlier journal shape, and an +/// earlier journal line (no `credential_ids`) still parses and replays. +#[test] +fn earlier_journals_without_the_new_fields_still_replay() { + let fixture = fixture(); + let data = &fixture.data; + let mut store = fixture.store(); + let revision = store.revision(); + let _ = store + .remove_package_profile("registries", revision) + .expect("remove unbound profile"); + drop(store); + let text = String::from_utf8(journal(data)).expect("journal text"); + let removed = text + .lines() + .find(|line| line.contains("package_profile_removed")) + .expect("removal event"); + assert!(!removed.contains("credential_ids"), "{removed}"); + assert!( + ControlStore::open(data) + .expect("replay") + .package_profiles() + .is_empty() + ); + + let earlier: Action = serde_json::from_value(serde_json::json!({ + "type": "package_profile_removed", + "payload": { "profile_id": Uuid::new_v4() }, + })) + .expect("earlier removal parses"); + assert!(matches!( + earlier, + Action::PackageProfileRemoved { credential_ids, .. } if credential_ids.is_empty() + )); + let start: crate::control::PackageFetchStart = serde_json::from_value(serde_json::json!({ + "fetch_id": Uuid::new_v4(), + "workspace_id": Uuid::new_v4(), + "profile_id": Uuid::new_v4(), + "profile_name": "registries", + "profile_revision": 2, + "url_sha256": "0".repeat(64), + "output_name": "pkg.bin", + "max_bytes": 10, + "timeout_ms": 1000, + })) + .expect("earlier fetch start parses"); + assert!(start.credential_id.is_none()); +} + +/// A load returns the bound secret and its scheme for the matched origin, +/// nothing for an unbound origin, and an error for a binding it cannot read. +#[test] +fn a_load_is_tied_to_the_matched_origin_and_never_falls_back() { + let fixture = fixture(); + let credentials = &fixture.credentials; + let mut store = fixture.store(); + let revision = store.revision(); + let set = store + .set_package_credential( + credentials, + "registries", + REGISTRY, + PackageAuth::Basic, + &secret(b"alice:pw"), + revision, + ) + .expect("set"); + let id = profile_id(&store); + let loaded = store + .load_package_credential(credentials, id, &origin(REGISTRY)) + .expect("load") + .expect("bound"); + assert_eq!(loaded.auth(), PackageAuth::Basic); + assert_eq!(loaded.credential_id(), set.binding.id()); + assert_eq!(loaded.secret().expose(), b"alice:pw"); + assert!(!format!("{loaded:?}").contains("alice")); + // Another origin of the same profile has no binding and gets no secret. + assert!( + store + .load_package_credential(credentials, id, &origin(CDN)) + .expect("load") + .is_none() + ); + assert!(matches!( + store.load_package_credential(credentials, Uuid::new_v4(), &origin(REGISTRY)), + Err(PackageCredentialUseError::UnknownProfile) + )); + + // A bound item missing from the native store is an error, not `None`. + credentials.delete(set.binding.id()).expect("delete item"); + assert!(matches!( + store.load_package_credential(credentials, id, &origin(REGISTRY)), + Err(PackageCredentialUseError::Store { + error: CredentialError::NotFound, + .. + }) + )); + + // A binding recorded by another backend is never read from this one. + let foreign = match credentials.backend() { + CredentialBackend::WindowsDpapi => CredentialBackend::LinuxSecretService, + CredentialBackend::LinuxSecretService => CredentialBackend::WindowsDpapi, + }; + store.snapshot.package_profiles[0].credentials[0].binding = + CredentialBinding::new(new_id(), foreign); + assert!(matches!( + store.load_package_credential(credentials, id, &origin(REGISTRY)), + Err(PackageCredentialUseError::OtherBackend { .. }) + )); +} + +fn bound_action(profile_id: Uuid, origin_text: &str, id: &CredentialId) -> Action { + Action::PackageCredentialBound { + profile_id, + origin: origin(origin_text).render(), + auth: PackageAuth::Bearer, + binding: CredentialBinding::new(id.clone(), CredentialBackend::WindowsDpapi), + } +} + +/// Replay refuses package-credential events that break the binding rules. +#[test] +fn replay_rejects_invalid_package_credential_events() { + let fixture = fixture(); + let store = fixture.store(); + let profile = profile_id(&store); + let provider = store.provider_profile("keys").expect("keys").profile().id(); + let id = new_id(); + + // An unknown profile, an undeclared origin, and a non-canonical spelling. + let mut state = store.snapshot.clone(); + for action in [ + bound_action(Uuid::new_v4(), REGISTRY, &id), + bound_action(profile, "https://other.example.com/", &id), + Action::PackageCredentialBound { + profile_id: profile, + origin: "https://registry.example.com/api".to_owned(), + auth: PackageAuth::Bearer, + binding: CredentialBinding::new(id.clone(), CredentialBackend::WindowsDpapi), + }, + ] { + assert!( + matches!( + apply(&mut state.clone(), &action), + Err(ControlError::Invalid(_)) + ), + "{action:?}" + ); + } + + apply(&mut state, &bound_action(profile, REGISTRY, &id)).expect("valid binding"); + // The same identifier cannot be rebound to the origin, shared with + // another origin, or shared with a provider profile. + for action in [ + bound_action(profile, REGISTRY, &id), + bound_action(profile, CDN, &id), + ] { + assert!( + matches!( + apply(&mut state.clone(), &action), + Err(ControlError::Invalid(_)) + ), + "{action:?}" + ); + } + assert!(matches!( + provider_binding::apply( + &mut state.clone(), + &Action::ProviderCredentialBound { + profile_id: provider, + binding: CredentialBinding::new(id.clone(), CredentialBackend::WindowsDpapi), + } + ), + Err(ControlError::Invalid(_)) + )); + // The reverse: an identifier a provider holds cannot be bound to an origin. + let mut with_provider = store.snapshot.clone(); + let provider_id = new_id(); + provider_binding::apply( + &mut with_provider, + &Action::ProviderCredentialBound { + profile_id: provider, + binding: CredentialBinding::new(provider_id.clone(), CredentialBackend::WindowsDpapi), + }, + ) + .expect("provider binding"); + assert!(matches!( + apply( + &mut with_provider, + &bound_action(profile, CDN, &provider_id) + ), + Err(ControlError::Invalid(_)) + )); + // A different identifier rotates the origin; at most one stays bound. + let rotated = new_id(); + apply(&mut state, &bound_action(profile, REGISTRY, &rotated)).expect("rotation"); + assert_eq!(state.package_profiles[0].credentials.len(), 1); + + // A removal must name the recorded origin and identifier. + let removed = + |origin_text: &str, credential_id: &CredentialId| Action::PackageCredentialRemoved { + profile_id: profile, + origin: origin(origin_text).render(), + credential_id: credential_id.clone(), + }; + for action in [ + removed(REGISTRY, &id), + removed(CDN, &rotated), + Action::PackageCredentialRemoved { + profile_id: Uuid::new_v4(), + origin: origin(REGISTRY).render(), + credential_id: rotated.clone(), + }, + ] { + assert!( + matches!( + apply(&mut state.clone(), &action), + Err(ControlError::Invalid(_)) + ), + "{action:?}" + ); + } + let mut after = state.clone(); + apply(&mut after, &removed(REGISTRY, &rotated)).expect("matching removal"); + assert!(after.package_profiles[0].credentials.is_empty()); + + // A profile removal must name exactly its bound credentials. + for credential_ids in [vec![], vec![id.clone()], vec![rotated.clone(), id.clone()]] { + assert!( + matches!( + super::super::apply( + &mut state.clone(), + &Action::PackageProfileRemoved { + profile_id: profile, + credential_ids, + }, + state.revision, + ), + Err(ControlError::Invalid(_)) + ), + "profile removal must match its bindings" + ); + } + super::super::apply( + &mut state.clone(), + &Action::PackageProfileRemoved { + profile_id: profile, + credential_ids: vec![rotated], + }, + state.revision, + ) + .expect("matching profile removal"); +} + +/// A tampered journal is refused on open, and an unknown action is refused by +/// an older reader (the action enum denies unknown variants). +#[test] +fn a_tampered_journal_with_a_bad_binding_is_refused_on_open() { + let fixture = fixture(); + let data = &fixture.data; + let store = fixture.store(); + let profile = profile_id(&store); + drop(store); + // Append a well-formed event that binds an undeclared origin. + let mut entry: serde_json::Value = serde_json::from_slice( + journal(data) + .split(|byte| *byte == b'\n') + .rfind(|line| !line.is_empty()) + .expect("last line"), + ) + .expect("entry"); + entry["sequence"] = serde_json::json!(entry["sequence"].as_u64().expect("sequence") + 1); + entry["revision"] = serde_json::json!(entry["revision"].as_u64().expect("revision") + 1); + entry["event_id"] = serde_json::json!(Uuid::new_v4()); + entry["action"] = + serde_json::to_value(bound_action(profile, REGISTRY, &new_id())).expect("action"); + entry["action"]["origin"] = serde_json::json!("https://evil.example.com:443/api/"); + let mut file = fs::OpenOptions::new() + .append(true) + .open(data.join(FILE_NAME)) + .expect("journal"); + use std::io::Write as _; + writeln!(file, "{entry}").expect("append"); + drop(file); + assert!( + matches!(ControlStore::open(data), Err(ControlError::Invalid(_))), + "a binding for an undeclared origin must not replay" + ); +} diff --git a/crates/omgcode-core/src/control/package/tests.rs b/crates/omgcode-core/src/control/package/tests.rs index 6880c61..ef8c79b 100644 --- a/crates/omgcode-core/src/control/package/tests.rs +++ b/crates/omgcode-core/src/control/package/tests.rs @@ -59,7 +59,10 @@ fn package_profiles_are_recorded_replayed_and_removed() { Err(ControlError::PackageProfileRefused(_)) )); assert_eq!( - store.remove_package_profile("crates", 2).expect("remove"), + store + .remove_package_profile("crates", 2) + .expect("remove") + .profile_id, profile.id() ); assert_eq!(store.revision(), 3); @@ -196,9 +199,10 @@ fn package_profile_bounds_are_enforced() { )); let revision = store.revision(); - store + let _freed = store .remove_package_profile("p0", revision) - .expect("free a profile slot"); + .expect("free a profile slot") + .profile_id; let host = format!("{}{}", "a".repeat(200), "b".repeat(52)); let path = format!("/{}/", "c".repeat(1000)); let origins: Vec = (0..MAX_PACKAGE_ORIGINS) diff --git a/crates/omgcode-core/src/lib.rs b/crates/omgcode-core/src/lib.rs index 8f01da1..baee9b1 100644 --- a/crates/omgcode-core/src/lib.rs +++ b/crates/omgcode-core/src/lib.rs @@ -16,5 +16,7 @@ pub mod run_journal; pub mod session; pub mod session_index; mod storage; +#[cfg(test)] +mod test_support; pub mod turn; mod workspace; diff --git a/crates/omgcode-core/src/packages.rs b/crates/omgcode-core/src/packages.rs index 2124115..bd4abf6 100644 --- a/crates/omgcode-core/src/packages.rs +++ b/crates/omgcode-core/src/packages.rs @@ -20,6 +20,7 @@ use std::time::{Duration, Instant}; use reqwest::Client; use reqwest::Url; +use reqwest::header::{AUTHORIZATION, HeaderValue}; use reqwest::redirect::Policy; use serde::Serialize; use sha2::{Digest, Sha256}; @@ -28,13 +29,16 @@ use tokio::io::AsyncWriteExt; use uuid::Uuid; use crate::control::{ - ControlError, ControlStore, PackageFetchFailure, PackageFetchFinish, PackageFetchOutcome, - PackageFetchStart, PackageProfile, valid_output_name, + ControlError, ControlStore, CredentialError, CredentialStore, PackageCredentialUseError, + PackageFetchFailure, PackageFetchFinish, PackageFetchOutcome, PackageFetchStart, PackageOrigin, + PackageProfile, valid_output_name, }; use crate::policy::WorkspaceId; use crate::session::NativePath; use crate::storage; +mod authorization; + /// Default greatest response body accepted. pub const DEFAULT_MAX_FETCH_BYTES: u64 = 64 * 1024 * 1024; /// Largest accepted response body. @@ -78,6 +82,10 @@ pub struct FetchReport { pub profile_revision: u64, /// SHA-256 of the request URL. pub url_sha256: String, + /// The credential sent with the request, when the matched origin has a + /// binding. Its opaque identifier only, never the secret. + #[serde(skip_serializing_if = "Option::is_none")] + pub credential_id: Option, /// The private cache file. pub output: NativePath, /// Bytes written. @@ -100,6 +108,14 @@ pub enum FetchError { /// The request could not be sent or the connection failed. #[error("the registry request failed: {0}")] Transport(#[source] reqwest::Error), + /// The profile binds a credential to the matched origin, and it could not + /// be read. Nothing was sent: the fetch never falls back to an + /// unauthenticated request or to another credential. + #[error("the registry credential could not be used: {0}")] + Credential(#[source] PackageCredentialUseError), + /// The native credential store could not be opened for a bound credential. + #[error("the credential store could not be opened: {0}")] + CredentialStore(#[source] CredentialError), /// The response body could not be read. #[error("the response body could not be read: {0}")] Body(#[source] io::Error), @@ -169,7 +185,27 @@ pub fn fetch(data_root: &Path, request: &FetchRequest) -> Result None, + Some(_) => { + let credentials = + CredentialStore::open(data_root).map_err(FetchError::CredentialStore)?; + let loaded = control + .load_package_credential(&credentials, profile.id(), origin) + .map_err(FetchError::Credential)? + .ok_or(FetchError::Refused( + "the bound registry credential disappeared", + ))?; + let value = authorization::header_value(loaded.auth(), loaded.secret()).ok_or( + FetchError::Refused("the registry credential is not a valid header value"), + )?; + Some((loaded.credential_id().clone(), value)) + } + }; let cache = match control.package_cache_root(request.workspace_id) { Some(root) => { if !root.is_current().unwrap_or(false) { @@ -213,6 +249,7 @@ pub fn fetch(data_root: &Path, request: &FetchRequest) -> Result Result { let finish = PackageFetchFinish { fetch_id, @@ -284,8 +322,13 @@ fn validate_request(request: &FetchRequest) -> Result<(), FetchError> { } /// Require the URL to match a declared origin and path prefix, with no -/// credentials, query, or fragment. -fn matching_origin(profile: &PackageProfile, url: &Url) -> Result<(), FetchError> { +/// credentials, query, or fragment, and return the origin it matched. When +/// several origins match, the longest path prefix wins, so a credential bound +/// to a broad origin is never sent to a path another origin claims. +fn matching_origin<'a>( + profile: &'a PackageProfile, + url: &Url, +) -> Result<&'a PackageOrigin, FetchError> { if !url.username().is_empty() || url.password().is_some() { return Err(FetchError::Refused("the URL must not contain credentials")); } @@ -302,18 +345,19 @@ fn matching_origin(profile: &PackageProfile, url: &Url) -> Result<(), FetchError .port_or_known_default() .ok_or(FetchError::Refused("the URL needs a port"))?; let path = url.path(); - let matched = profile.origins().iter().any(|origin| { - origin.scheme().as_str() == url.scheme() - && origin.host().eq_ignore_ascii_case(host) - && origin.port() == port - && path.starts_with(origin.path_prefix()) - }); - if !matched { - return Err(FetchError::Refused( + profile + .origins() + .iter() + .filter(|origin| { + origin.scheme().as_str() == url.scheme() + && origin.host().eq_ignore_ascii_case(host) + && origin.port() == port + && path.starts_with(origin.path_prefix()) + }) + .max_by_key(|origin| origin.path_prefix().len()) + .ok_or(FetchError::Refused( "the URL is not under a declared registry origin and path", - )); - } - Ok(()) + )) } /// Publish a completed private partial under a new cache name without @@ -340,6 +384,7 @@ async fn publish_cache_file( fn perform( start: &PackageFetchStart, url: &Url, + authorization: Option, cache: &Path, final_path: &Path, ) -> Result<(FetchReport, u16), FailedFetch> { @@ -369,18 +414,23 @@ fn perform( status: None, })?; let remaining = deadline.saturating_duration_since(Instant::now()); - let mut response = - match tokio::time::timeout(remaining, client.get(url.as_str()).send()).await { - Ok(Ok(response)) => response, - Ok(Err(error)) => { - return Err(FailedFetch { - error: FetchError::Transport(error), - reason: PackageFetchFailure::Transport, - status: None, - }); - } - Err(_) => return Err(deadline_failure(None)), - }; + let mut request = client.get(url.as_str()); + if let Some(value) = authorization { + // The value is already marked sensitive. Redirects are refused, + // so the credential cannot follow one to another origin. + request = request.header(AUTHORIZATION, value); + } + let mut response = match tokio::time::timeout(remaining, request.send()).await { + Ok(Ok(response)) => response, + Ok(Err(error)) => { + return Err(FailedFetch { + error: FetchError::Transport(error), + reason: PackageFetchFailure::Transport, + status: None, + }); + } + Err(_) => return Err(deadline_failure(None)), + }; let http_status = response.status().as_u16(); if !response.status().is_success() { return Err(FailedFetch { @@ -506,6 +556,10 @@ fn perform( profile_name: start.profile_name.clone(), profile_revision: start.profile_revision, url_sha256: start.url_sha256.clone(), + credential_id: start + .credential_id + .as_ref() + .map(|id| id.as_str().to_owned()), output: NativePath::from_path(final_path).map_err(|error| FailedFetch { error: FetchError::Cache(io::Error::other(error.to_string())), reason: PackageFetchFailure::Cache, diff --git a/crates/omgcode-core/src/packages/authorization.rs b/crates/omgcode-core/src/packages/authorization.rs new file mode 100644 index 0000000..5713f7e --- /dev/null +++ b/crates/omgcode-core/src/packages/authorization.rs @@ -0,0 +1,83 @@ +//! The `Authorization` header for a bound registry credential. See ADR-0056. +//! +//! The plaintext stays in zeroizing buffers until `HeaderValue` copies it, and +//! the value is marked sensitive so `Debug` and HTTP/2 header compression +//! treat it as such. The copy inside the HTTP stack is outside this module's +//! control. + +use reqwest::header::HeaderValue; + +use crate::control::{PackageAuth, Secret, Zeroizing}; + +const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/// Build the header value: `Bearer ` or `Basic `. +/// +/// Returns `None` when the bytes are not a legal header value. Bind-time +/// validation already limits secrets to visible ASCII, so this is a second +/// check, not the policy. +pub(super) fn header_value(auth: PackageAuth, secret: &Secret) -> Option { + // Reserve the exact length first: a reallocation would free a partial + // copy of the secret without wiping it. + let secret_len = secret.expose().len(); + let capacity = match auth { + PackageAuth::Bearer => b"Bearer ".len().checked_add(secret_len)?, + PackageAuth::Basic => secret_len + .div_ceil(3) + .checked_mul(4)? + .checked_add(b"Basic ".len())?, + }; + let mut buffer: Zeroizing> = Zeroizing::new(Vec::with_capacity(capacity)); + match auth { + PackageAuth::Bearer => { + buffer.extend_from_slice(b"Bearer "); + buffer.extend_from_slice(secret.expose()); + } + PackageAuth::Basic => { + buffer.extend_from_slice(b"Basic "); + encode_base64(secret.expose(), &mut buffer); + } + } + debug_assert_eq!(buffer.len(), capacity); + let mut value = HeaderValue::from_bytes(&buffer).ok()?; + value.set_sensitive(true); + Some(value) +} + +/// Standard RFC 4648 base64 with padding, appended to `output`. +fn encode_base64(input: &[u8], output: &mut Vec) { + for chunk in input.chunks(3) { + let first = chunk.first().copied().unwrap_or(0); + let second = chunk.get(1).copied(); + let third = chunk.get(2).copied(); + let group = (u32::from(first) << 16) + | (u32::from(second.unwrap_or(0)) << 8) + | u32::from(third.unwrap_or(0)); + let sextets = [ + (group >> 18) & 0x3f, + (group >> 12) & 0x3f, + (group >> 6) & 0x3f, + group & 0x3f, + ]; + let kept = match chunk.len() { + 1 => 2, + 2 => 3, + _ => 4, + }; + for (index, sextet) in sextets.iter().enumerate() { + let symbol = if index < kept { + // The sextet is masked to six bits, so the lookup is in range. + ALPHABET + .get(usize::try_from(*sextet).unwrap_or(0)) + .copied() + .unwrap_or(b'=') + } else { + b'=' + }; + output.push(symbol); + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/omgcode-core/src/packages/authorization/tests.rs b/crates/omgcode-core/src/packages/authorization/tests.rs new file mode 100644 index 0000000..ef3daf6 --- /dev/null +++ b/crates/omgcode-core/src/packages/authorization/tests.rs @@ -0,0 +1,46 @@ +use super::*; + +fn encoded(input: &[u8]) -> String { + let mut output = Vec::new(); + encode_base64(input, &mut output); + String::from_utf8(output).expect("base64 is ASCII") +} + +fn secret(text: &str) -> Secret { + Secret::new(text.as_bytes().to_vec()).expect("secret") +} + +/// The encoder matches every RFC 4648 section 10 test vector. +#[test] +fn base64_matches_the_rfc_4648_vectors() { + for (input, expected) in [ + ("", ""), + ("f", "Zg=="), + ("fo", "Zm8="), + ("foo", "Zm9v"), + ("foob", "Zm9vYg=="), + ("fooba", "Zm9vYmE="), + ("foobar", "Zm9vYmFy"), + ] { + assert_eq!(encoded(input.as_bytes()), expected, "input {input:?}"); + } +} + +/// Every byte value encodes to the standard alphabet, including `+` and `/`. +#[test] +fn base64_uses_the_standard_alphabet_for_all_bytes() { + assert_eq!(encoded(&[0xfb, 0xff, 0xbf]), "+/+/"); + assert_eq!(encoded(&[0x00, 0x10, 0x83]), "ABCD"); +} + +#[test] +fn bearer_and_basic_values_are_sensitive_and_exact() { + let bearer = header_value(PackageAuth::Bearer, &secret("tok_123.abc")).expect("bearer"); + assert_eq!(bearer.as_bytes(), b"Bearer tok_123.abc"); + assert!(bearer.is_sensitive()); + assert!(!format!("{bearer:?}").contains("tok_123")); + + let basic = header_value(PackageAuth::Basic, &secret("alice:s3cret")).expect("basic"); + assert_eq!(basic.as_bytes(), b"Basic YWxpY2U6czNjcmV0"); + assert!(basic.is_sensitive()); +} diff --git a/crates/omgcode-core/src/packages/tests.rs b/crates/omgcode-core/src/packages/tests.rs index fc6540d..7042b78 100644 --- a/crates/omgcode-core/src/packages/tests.rs +++ b/crates/omgcode-core/src/packages/tests.rs @@ -5,8 +5,9 @@ use std::path::PathBuf; use std::thread::{self, JoinHandle}; use std::time::{Duration, Instant}; -use crate::control::{ControlStore, PackageProfileDeclaration}; +use crate::control::{ControlStore, CredentialId, PackageAuth, PackageProfileDeclaration, Secret}; use crate::policy::{PermissionSet, WorkspaceId}; +use crate::test_support::private_data_root; const PROFILE: &str = "registries"; const MAX_REQUEST_HEADER_BYTES: usize = 16 * 1024; @@ -119,6 +120,7 @@ impl Scene { let root = directory.path().join("dev"); let workspace = root.join("workspace"); fs::create_dir_all(&workspace).expect("workspace"); + private_data_root(&data); let mut store = ControlStore::open(&data).expect("store"); store.set_development_root(&root, 0).expect("root"); let workspace_id = store @@ -167,6 +169,35 @@ impl Scene { } } + /// Bind a registry credential to one declared origin through the real + /// control journal and the real native credential store. + fn bind(&self, origin: &str, auth: PackageAuth, secret: &str) -> CredentialId { + let credentials = CredentialStore::open(&self.data) + .expect("a native credential store is required (on Linux, an unlocked Secret Service)"); + let mut store = ControlStore::open(&self.data).expect("store"); + let revision = store.revision(); + let set = store + .set_package_credential( + &credentials, + PROFILE, + origin, + auth, + &Secret::new(secret.as_bytes().to_vec()).expect("secret"), + revision, + ) + .expect("bind credential"); + set.binding.id().clone() + } + + /// Delete a bound credential's native item, as an operator could outside + /// OMGCode, leaving the binding in place. + fn delete_native_item(&self, id: &CredentialId) { + CredentialStore::open(&self.data) + .expect("native credential store") + .delete(id) + .expect("delete item"); + } + fn cache_file(&self, name: &str) -> PathBuf { if cfg!(target_os = "linux") { self.data @@ -190,6 +221,248 @@ fn hex_digest(bytes: &[u8]) -> String { hex(&Sha256::digest(bytes)) } +/// A listener that records whether anything connected to it. +struct Silent { + listener: TcpListener, +} + +impl Silent { + fn new() -> Self { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind silent listener"); + listener.set_nonblocking(true).expect("nonblocking"); + Self { listener } + } + + fn address(&self) -> SocketAddr { + self.listener.local_addr().expect("address") + } + + /// Whether no connection is waiting. A connection, even one that sent no + /// bytes, would have been accepted by the kernel into the backlog. + fn received_nothing(&self) -> bool { + matches!( + self.listener.accept(), + Err(error) if error.kind() == io::ErrorKind::WouldBlock + ) + } +} + +/// The values of every `Authorization` header in a raw request head. +fn authorization_values(head: &str) -> Vec { + head.lines() + .filter_map(|line| { + let (name, value) = line.split_once(':')?; + name.eq_ignore_ascii_case("authorization") + .then(|| value.trim().to_owned()) + }) + .collect() +} + +#[test] +fn a_bound_bearer_credential_is_sent_to_its_origin_and_audited_by_identifier() { + let body = b"bearer-bytes".to_vec(); + let fixture = Fixture::start(200, body.clone(), ""); + let scene = Scene::new(&[format!("http://{}/fixtures/", fixture.address)]); + let origin = format!("http://{}/fixtures/", fixture.address); + let id = scene.bind(&origin, PackageAuth::Bearer, "tok-sentinel_9f3.abc"); + let report = fetch( + &scene.data, + &scene.request(fixture.url("/fixtures/pkg.bin"), "pkg.bin", 1024), + ) + .expect("fetch with a credential"); + assert_eq!(report.credential_id.as_deref(), Some(id.as_str())); + let head = fixture.finish(); + assert_eq!( + authorization_values(&head), + vec!["Bearer tok-sentinel_9f3.abc".to_owned()], + "{head}" + ); + let audit = scene.audit_text(); + assert!(audit.contains(id.as_str()), "{audit}"); + assert!(!audit.contains("sentinel"), "{audit}"); + let receipt = serde_json::to_string(&report).expect("report JSON"); + assert!(!receipt.contains("sentinel"), "{receipt}"); + assert!( + !fs::read(scene.data.join("control-v1.jsonl")) + .expect("journal") + .windows(8) + .any(|window| window == b"sentinel") + ); +} + +#[test] +fn a_bound_basic_credential_is_sent_as_base64_user_and_password() { + let fixture = Fixture::start(200, b"basic-bytes".to_vec(), ""); + let scene = Scene::new(&[format!("http://{}/fixtures/", fixture.address)]); + scene.bind( + &format!("http://{}/fixtures/", fixture.address), + PackageAuth::Basic, + "alice:s3cret", + ); + fetch( + &scene.data, + &scene.request(fixture.url("/fixtures/pkg.bin"), "pkg.bin", 1024), + ) + .expect("fetch with a credential"); + let head = fixture.finish(); + assert_eq!( + authorization_values(&head), + vec!["Basic YWxpY2U6czNjcmV0".to_owned()], + "{head}" + ); +} + +/// A credential belongs to one origin: another origin of the same profile +/// gets no `Authorization` header and no `credential_id` in its audit start. +#[test] +fn an_unbound_origin_of_the_same_profile_sends_no_authorization() { + let fixture = Fixture::start(200, b"public-bytes".to_vec(), ""); + let scene = Scene::new(&[ + format!("http://{}/private/", fixture.address), + format!("http://{}/public/", fixture.address), + ]); + scene.bind( + &format!("http://{}/private/", fixture.address), + PackageAuth::Bearer, + "tok-sentinel", + ); + let report = fetch( + &scene.data, + &scene.request(fixture.url("/public/pkg.bin"), "pkg.bin", 1024), + ) + .expect("fetch from the unbound origin"); + assert!(report.credential_id.is_none()); + let head = fixture.finish(); + assert!(authorization_values(&head).is_empty(), "{head}"); + let audit = scene.audit_text(); + let start = audit + .split("package_fetch_started") + .nth(1) + .and_then(|rest| rest.split("package_fetch_finished").next()) + .expect("fetch start record"); + assert!(!start.contains("credential_id"), "{start}"); + assert!(!audit.contains("sentinel"), "{audit}"); +} + +/// When origins nest, the most specific one decides, so a credential bound to +/// a broad origin is not sent to a path a narrower origin declares. +#[test] +fn the_most_specific_matching_origin_decides_the_credential() { + let fixture = Fixture::start(200, b"nested-bytes".to_vec(), ""); + let scene = Scene::new(&[ + format!("http://{}/", fixture.address), + format!("http://{}/public/", fixture.address), + ]); + scene.bind( + &format!("http://{}/", fixture.address), + PackageAuth::Bearer, + "tok-sentinel", + ); + fetch( + &scene.data, + &scene.request(fixture.url("/public/pkg.bin"), "pkg.bin", 1024), + ) + .expect("fetch under the narrower origin"); + let head = fixture.finish(); + assert!(authorization_values(&head).is_empty(), "{head}"); +} + +/// A binding that cannot be read refuses the fetch. Nothing is sent, nothing +/// falls back to an unauthenticated request, and no start record exists. +#[test] +fn a_bound_credential_that_cannot_be_read_sends_nothing() { + let silent = Silent::new(); + let scene = Scene::new(&[format!("http://{}/fixtures/", silent.address())]); + let id = scene.bind( + &format!("http://{}/fixtures/", silent.address()), + PackageAuth::Bearer, + "tok-sentinel", + ); + scene.delete_native_item(&id); + let before = scene.audit_text(); + let error = fetch( + &scene.data, + &scene.request( + format!("http://{}/fixtures/pkg.bin", silent.address()), + "pkg.bin", + 1024, + ), + ) + .expect_err("an unreadable credential refuses the fetch"); + assert!( + matches!( + error, + FetchError::Credential(PackageCredentialUseError::Store { + error: CredentialError::NotFound, + .. + }) + ), + "{error}" + ); + assert!(error.to_string().contains(id.as_str()), "{error}"); + assert!(!error.to_string().contains("sentinel"), "{error}"); + assert!(silent.received_nothing(), "a request reached the registry"); + assert_eq!(scene.audit_text(), before, "no start record is written"); + assert!(!scene.cache_file("pkg.bin").exists()); +} + +/// Redirects stay refused for a credentialed request, so the credential +/// cannot follow one to another origin. +#[test] +fn a_redirect_from_a_credentialed_origin_is_not_followed() { + let target = Silent::new(); + let fixture = Fixture::start( + 302, + Vec::new(), + &format!("Location: http://{}/steal\r\n", target.address()), + ); + let scene = Scene::new(&[format!("http://{}/fixtures/", fixture.address)]); + scene.bind( + &format!("http://{}/fixtures/", fixture.address), + PackageAuth::Bearer, + "tok-sentinel", + ); + let error = fetch( + &scene.data, + &scene.request(fixture.url("/fixtures/pkg.bin"), "pkg.bin", 1024), + ) + .expect_err("redirect"); + assert!(matches!(error, FetchError::Status(302)), "{error}"); + let head = fixture.finish(); + assert_eq!(authorization_values(&head).len(), 1, "{head}"); + assert!( + target.received_nothing(), + "the redirect target was contacted" + ); + assert!(!scene.audit_text().contains("sentinel")); +} + +/// A rejected credential leaves no secret in the error, the audit, or the +/// receipt, whatever the registry answers. +#[test] +fn a_rejected_credential_is_not_echoed_anywhere() { + let fixture = Fixture::start(401, b"denied".to_vec(), ""); + let scene = Scene::new(&[format!("http://{}/fixtures/", fixture.address)]); + let id = scene.bind( + &format!("http://{}/fixtures/", fixture.address), + PackageAuth::Basic, + "alice:pw-sentinel", + ); + let error = fetch( + &scene.data, + &scene.request(fixture.url("/fixtures/pkg.bin"), "pkg.bin", 1024), + ) + .expect_err("unauthorized"); + assert!(matches!(error, FetchError::Status(401)), "{error}"); + for text in [format!("{error}"), format!("{error:?}"), scene.audit_text()] { + assert!(!text.contains("sentinel"), "{text}"); + assert!(!text.contains("YWxpY2U6"), "{text}"); + } + assert!(scene.audit_text().contains(id.as_str())); + let head = fixture.finish(); + assert_eq!(authorization_values(&head).len(), 1, "{head}"); +} + #[test] fn a_declared_origin_is_fetched_and_audited() { let body = b"package-bytes".to_vec(); diff --git a/crates/omgcode-core/src/test_support.rs b/crates/omgcode-core/src/test_support.rs new file mode 100644 index 0000000..1fca3a1 --- /dev/null +++ b/crates/omgcode-core/src/test_support.rs @@ -0,0 +1,34 @@ +//! Helpers shared by tests that need the real native credential store. + +use std::fs; +use std::path::Path; + +/// Create `data` as a private data root. Windows temporary directories +/// inherit grants the private store refuses, so give the root an owner-only +/// ACL, as an installed profile has. +pub(crate) fn private_data_root(data: &Path) { + fs::create_dir(data).expect("data root"); + #[cfg(windows)] + { + use std::process::Command; + let output = Command::new("whoami") + .args(["/user", "/fo", "csv", "/nh"]) + .output() + .expect("whoami"); + assert!(output.status.success(), "whoami failed"); + let text = String::from_utf8(output.stdout).expect("UTF-8"); + let sid = text + .trim() + .rsplit(',') + .next() + .expect("SID column") + .trim_matches('"') + .to_owned(); + let status = Command::new("icacls") + .arg(data) + .args(["/inheritance:r", "/grant:r", &format!("*{sid}:(OI)(CI)F")]) + .output() + .expect("icacls"); + assert!(status.status.success(), "icacls failed"); + } +} diff --git a/crates/omgcode-platform/src/credentials.rs b/crates/omgcode-platform/src/credentials.rs index b78e5bc..0e9358a 100644 --- a/crates/omgcode-platform/src/credentials.rs +++ b/crates/omgcode-platform/src/credentials.rs @@ -15,7 +15,7 @@ use std::fmt; use std::io::{self, Read}; use std::path::Path; -use zeroize::Zeroizing; +pub use zeroize::Zeroizing; #[cfg(target_os = "linux")] mod linux; diff --git a/docs/adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md b/docs/adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md index bab9f40..bb3205b 100644 --- a/docs/adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md +++ b/docs/adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md @@ -86,6 +86,7 @@ Rules: inspect or import it until the exposure increment lands. - Registry credentials are still unsupported. The URL scheme, host, port, and path are the only authorization; a credential path needs a separate design. + Amended by ADR-0056. - `reqwest` gains the `native-tls` feature and its dependency tree. TLS uses the system trust store. The tests use loopback HTTP; they do not qualify a live HTTPS registry. diff --git a/docs/adr/0056-send-registry-credentials-to-one-declared-origin.md b/docs/adr/0056-send-registry-credentials-to-one-declared-origin.md new file mode 100644 index 0000000..11ecd20 --- /dev/null +++ b/docs/adr/0056-send-registry-credentials-to-one-declared-origin.md @@ -0,0 +1,123 @@ +# 0056: Send registry credentials to one declared origin + +- Status: Accepted +- Date: 2026-11-17 + +## Context + +The trusted `packages fetch` command (ADR-0035) sends no credentials, so it +cannot reach a private registry. ADR-0035 left credentials to a separate +design and ADR-0034 forbids secrets in profiles, run environments, journals, +and model context. ADR-0042 stores provider secrets in protected OS storage +and binds them by opaque identifier, and ADR-0047 and ADR-0048 tie a stored +credential to the one destination it was recorded for. + +A package-network profile may mix a registry with a download host such as a +CDN. A credential that suits the registry must not reach the CDN. The fetch +refuses redirects, so a response cannot move a credential to another origin, +but the profile itself can name several. + +## Decision + +An operator can bind one stored secret to one declared origin of a +package-network profile. Only the trusted `packages fetch` reads it. + +- **Binding.** The control journal records + `package_credential_bound { profile_id, origin, auth, binding }` and + `package_credential_removed { profile_id, origin, credential_id }`. `origin` + is the canonical text of one of the profile's stored origins + (`scheme://host:port/prefix/`, lowercase host, explicit port), used both + when recording and when matching. `auth` is `bearer` or `basic`. `binding` + is the identifier and backend of ADR-0042. The journal never holds a secret. + `package_profile_removed` gains an optional `credential_ids` list, so + removing a profile ends every binding in the same event, as provider profile + removal does. Journals written earlier still replay. +- **Replay rules.** The profile must exist and the origin must be one of its + origins. An origin holds at most one credential. A credential identifier is + bound at most once across all package and provider bindings, and rebinding an + origin to the same identifier is invalid. A removal must name the currently + bound identifier. Binding and removal advance the settings revision and need + the expected revision. +- **Store and rotate.** The order follows `set_provider_credential`: store the + new native secret, record the binding, then delete the replaced secret. A + refusal before the journal write deletes the new item; an uncertain journal + write keeps it and reports its identifier. The shared helpers + (`store_failure`, `binding_write_uncertain`, `delete_unbound_credential`) + serve both kinds of binding. +- **Secret validation.** Before anything is stored: at most 16 KiB, visible + ASCII, no control character or line break, and no leading or trailing + whitespace. A bearer token has no whitespace at all. A basic secret is + `user:password` with a colon and a nonempty user. +- **Fetch.** The matched origin decides. When several origins match the URL, + the longest path prefix wins, so a credential bound to a broad origin is not + sent to a path a narrower origin declares. If the matched origin has a + binding, the fetch opens the credential store, loads the secret under the + still-open control store, builds one `Authorization` header (`Bearer + ` or `Basic `), and drops the control store + before any network I/O. The header value is marked sensitive and built in + zeroizing buffers. If the store cannot be opened or the secret cannot be + read, the fetch fails with a typed error and sends nothing. It never falls + back to an unauthenticated request or to another credential. Like every + refusal before the request, this failure happens before the start record is + written, so nothing is recorded. An origin without a binding behaves as + before. Redirects stay refused, so the header cannot follow one. +- **Audit.** `package_fetch_started` carries the optional `credential_id` only + when a credential was attached. The receipt reports the same identifier. The + secret, the header, and the URL text are never recorded or printed. +- **Operator only.** `workspace package-credential-set` reads the secret from + piped standard input only, refuses a terminal, and reads it before the + control store opens. `package-credential-remove` unbinds, then deletes. + `package-profile-remove` deletes the profile's bound secrets after its + event. `package-profile-show` lists the bound origins with auth, identifier, + and backend. `capabilities` reports `package_registry_credentials`. No model + tool, run, or provider adapter can bind, read, or receive one. +- **Runs.** A confined run never receives a registry credential: no run + environment variable, file, or journal field carries one. + +## Alternatives considered + +- **One credential per profile.** Rejected: a profile may mix a registry and a + CDN, and one token would go to both. +- **A credential in the URL, a profile, or an environment variable.** + Rejected by ADR-0035 and ADR-0034: URL text is not recorded, profiles hold + no secret, and environment input leaks into process listings and children. +- **Giving runs the credential.** Rejected: no run-side destination control + exists (ADR-0034), so a hostile tree could send it anywhere. +- **Fall back to an unauthenticated request when the secret is unreadable.** + Rejected: a silent downgrade can fetch a public file of the same name and + hide a broken binding. +- **A new third-party base64 crate.** Rejected: the encoder is small, has the + RFC 4648 vectors as tests, and avoids a new direct dependency. + +## Consequences + +- A private registry can be fetched without a secret in a journal, a + command line, a run, or model context. +- The credential still crosses the network to the declared origin, and the + registry can log it. Transport security is the declared origin's `https`; + only a loopback test fixture may use `http`. +- `Bearer` and `Basic` are the only schemes. Tokens that need another header + or a custom signing step are unsupported. +- The operator chooses the scheme. A wrong scheme fails at the registry. +- An older binary refuses a journal that records the new actions, because the + action enum denies unknown events; a journal written with these events + cannot be read by the previous version. +- A bound origin whose secret was deleted outside OMGCode refuses every fetch + from it until the operator rebinds or removes the credential. +- Memory zeroing is best effort: the HTTP stack keeps its own copy of the + header. Any process running as the same user can read the stored secret + (ADR-0042). +- The tests use loopback HTTP and the real native store. They do not qualify a + live HTTPS registry. + +## References + +- [Command execution](../execution.md#trusted-package-fetch) +- [Security coding rules](../security-coding-rules.md) +- [Testing standards](../testing-standards.md) +- [ADR-0034](0034-gate-package-networking-behind-enforceable-destination-control.md) +- [ADR-0035](0035-fetch-packages-through-a-trusted-allowlisted-cache.md) +- [ADR-0037](0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md) +- [ADR-0042](0042-store-provider-credentials-in-protected-os-storage.md) +- [ADR-0047](0047-read-the-local-provider-token-from-the-credential-store.md) +- [ADR-0048](0048-bind-a-stored-credential-to-its-profile-endpoint.md) diff --git a/docs/adr/index.md b/docs/adr/index.md index 484b76a..1604f03 100644 --- a/docs/adr/index.md +++ b/docs/adr/index.md @@ -69,3 +69,4 @@ meaningful alternative does not need an ADR. | [0052: Publish rolling testing builds from main](0052-publish-rolling-testing-builds-from-main.md) | Accepted | 2026-11-17 | | [0053: Compact the checkpoint ledger in place](0053-compact-the-checkpoint-ledger-in-place.md) | Accepted | 2026-11-17 | | [0054: Add multi-edit and create model file tools](0054-add-multi-edit-and-create-model-file-tools.md) | Accepted | 2026-11-17 | +| [0056: Send registry credentials to one declared origin](0056-send-registry-credentials-to-one-declared-origin.md) | Accepted | 2026-11-17 | diff --git a/docs/development-tools.md b/docs/development-tools.md index 71f0a1f..8e711d0 100644 --- a/docs/development-tools.md +++ b/docs/development-tools.md @@ -137,8 +137,9 @@ into the selected per-workspace cache root. A run can read only that root through `OMGCODE_PACKAGE_CACHE`; Windows needs a read-execute container grant, and Linux falls back to application storage when no root is selected. Tool-specific -configuration to use that directory is operator work. Registry credentials -require a narrow reviewed credential path. Offline tasks +configuration to use that directory is operator work. A registry credential can +be bound to one declared origin for the trusted fetch only +([ADR-0056](adr/0056-send-registry-credentials-to-one-declared-origin.md)); a run never receives one. Offline tasks must work with available dependencies or explain which download is needed. Development servers, debuggers, license servers, hardware access, and other diff --git a/docs/development.md b/docs/development.md index 2fc5a86..0503dbf 100644 --- a/docs/development.md +++ b/docs/development.md @@ -320,6 +320,29 @@ backend and its state. A control journal written by 0.27.0 cannot be read by Any process running as the same user can read these credentials. The stores protect them at rest and from other users, not from same-user code. +### Registry credentials for package fetch + +[ADR-0056](adr/0056-send-registry-credentials-to-one-declared-origin.md) binds a stored secret to one declared origin of a package-network +profile. The commands are trusted-CLI only: + +```powershell +omgcode --data-dir C:\omgcode-data workspace package-credential-set crates --origin https://registry.example.com/api/ --auth bearer --revision 9 +omgcode --data-dir C:\omgcode-data workspace package-profile-show crates +omgcode --data-dir C:\omgcode-data workspace package-credential-remove crates --origin https://registry.example.com/api/ --revision 10 +``` + +`package-credential-set` takes `--auth bearer` or `--auth basic` and reads the +secret from piped standard input exactly as `provider-credential-set` does (see +[provider credentials](#provider-credentials)): no argument or environment +input, a terminal refused, one trailing line ending removed, and the secret read +before the control store opens. A `basic` secret is `user:password`. A bound +secret is sent only by `packages fetch`, only to that origin, and the output +reports the identifier and backend, never the secret. +`package-credential-remove` and `package-profile-remove` delete the native +items after the journal event. `capabilities` reports +`package_registry_credentials`. The error codes are those of provider +credentials. + ### Use a stored credential for chat ADR-0047 lets `chat` and `session compact` read the LM Studio server token diff --git a/docs/execution.md b/docs/execution.md index aca27bc..65fa45d 100644 --- a/docs/execution.md +++ b/docs/execution.md @@ -240,8 +240,9 @@ implemented: the profile rather than falling back to general network access; - its exact limits and bypass analysis are documented and qualified on both supported hosts; -- registry credentials remain a separate reviewed design and never enter a - profile, run environment, journal, or model context; +- a registry credential never enters a profile, run environment, journal, or + model context. The trusted fetch's bound credential ([ADR-0056](adr/0056-send-registry-credentials-to-one-declared-origin.md)) is a + separate path; a run never receives one; - acceptance tests prove allowed-origin success, undeclared-origin refusal, redirect refusal, credential refusal, offline fallback, and platform refusal. @@ -293,7 +294,9 @@ Before the request it checks: 5. The byte limit is 1 byte to 1 GiB and the deadline is 1 to 600 seconds. The request is one GET. Redirects are not followed, proxy environment -variables are ignored, no credentials are sent, and nothing is retried. TLS +variables are ignored, and nothing is retried. No credential is sent unless +the operator bound one to the matched origin (see +[registry credentials](#registry-credentials)). TLS uses the operating-system trust store through `reqwest`'s `native-tls` feature; certificate validation is never disabled. The response must be 2xx and its body is streamed, hashed with SHA-256, and stopped if it passes the @@ -324,8 +327,43 @@ before the program starts. Without a selected root, Linux uses the application-storage fallback and Windows exposes nothing. The root's ancestors must be traversable by the container, so use a dedicated top-level directory rather than a user's private temporary tree. Tool-specific use of the -directory is operator configuration. Registry credentials remain unsupported. -`workspace status` lists selected roots under `package_cache_roots`. +directory is operator configuration. A run never receives a registry +credential. `workspace status` lists selected roots under `package_cache_roots`. + +### Registry credentials + +[ADR-0056](adr/0056-send-registry-credentials-to-one-declared-origin.md) lets the operator bind one stored secret to one declared origin of +a package-network profile. Only `packages fetch` reads it, and it sends it to +that origin alone. Binding is a trusted CLI action, never a model tool: + +```text +omgcode workspace package-credential-set --origin --auth bearer|basic --revision +omgcode workspace package-credential-remove --origin --revision +``` + +`package-credential-set` reads the secret from piped standard input only (as +`provider-credential-set` does), refuses a terminal, and reads it before the +control store opens. `--origin` must equal one of the profile's stored origins +after normalization. The secret is at most 16 KiB of visible ASCII with no +control character, line break, or leading or trailing whitespace. A `bearer` +token has no whitespace; a `basic` secret is `user:password` with a nonempty +user. Invalid input is refused before anything is stored. Storage, rotation, +and recovery follow the provider credential rules (ADR-0042): the native item +is stored, then `package_credential_bound` is recorded, then a replaced item is +deleted. `package_credential_removed` and a profile removal that names its +bound identifiers unbind the same way. `package-profile-show` and `status` +list bound origins with the scheme, identifier, and backend, never the secret. + +At fetch time the matched origin decides; when several origins match, the +longest path prefix wins. A bound origin sends `Authorization: Bearer ` +or `Authorization: Basic `, marked sensitive. An unbound origin +sends no header. If the credential store cannot be opened or the bound item +cannot be read, the fetch fails before the start record and sends nothing; it +never falls back to an unauthenticated request or another credential. +Redirects stay refused, so the header cannot follow one. `package_fetch_started` +records the `credential_id` only when one was attached, and the receipt shows +it. The credential still crosses the network to the declared origin, and the +registry can log it; use `https`. The runner's environment is not touched. ## Container grants @@ -1003,8 +1041,9 @@ These remain true after C6: does not expose the cache. A proxy or proxy environment variables are not a boundary: a hostile tree can connect directly. - **Package fetch limits.** The fetch is trusted-CLI only. It sends no - credentials, follows no redirects, ignores proxy variables, and uses the - system TLS store. The cache is the selected per-workspace root or, on Linux, + credentials unless one is bound to the matched origin + ([registry credentials](#registry-credentials)), follows no redirects, + ignores proxy variables, and uses the system TLS store. The cache is the selected per-workspace root or, on Linux, the application-storage fallback. It makes no power-loss claim. Tests use loopback HTTP; a live HTTPS registry is not qualified. - **Package cache exposure.** A run receives `OMGCODE_PACKAGE_CACHE` when the @@ -1082,10 +1121,11 @@ These remain true after C6: | C14 (0.25.0) | Approval-gated model command tool | Done: see [model command tool](#model-command-tool) and ADR-0040. A CLI acceptance test qualifies one successful approved command on both hosts; confinement correctness remains the platform suites' responsibility. | | C17 (0.28.0) | Approval-gated model edit tool | Done: see the [model edit route](file-access.md#model-edit-route-adr-0043) and ADR-0043. It starts no process; it writes one exact edit through the checkpointed apply after a writers-stopped approval. | | C18 (0.36.0) | Approval-gated model multi-edit and create tools | Done: see the [model multi-edit and create routes](file-access.md#model-multi-edit-and-create-routes-adr-0054) and ADR-0054. They start no process; multi-edit writes through the checkpointed apply and create through the broker's exclusive create, each after a writers-stopped approval. | +| C20 (0.37.0) | Registry credentials for the trusted fetch | Done: see [registry credentials](#registry-credentials) and ADR-0056. One bound secret per declared origin; no run receives it. | Later work needs its own decisions: -- registry credentials and development-server networking; +- development-server networking; - model delete, rename, network, Git, and MCP tools; Live qualification on each supported OS remains a separate gate. diff --git a/docs/platform-support.md b/docs/platform-support.md index fc05117..d09b0df 100644 --- a/docs/platform-support.md +++ b/docs/platform-support.md @@ -25,7 +25,7 @@ containment. | Model file tools | Read and list, plus approval-gated exact-edit, multi-edit, and create tools (with `read` and `write`) and an approval-gated command tool (with `execute`, `read`, and `write`), through chat for a workspace that enables `model_tools`. Each edit, multi-edit, or creation needs an approval with the writers-stopped acknowledgement; edits and multi-edits are applied through the checkpointed apply on Linux ext4 or Windows NTFS, and a creation uses the broker's exclusive create without a checkpoint. Delete, rename, and general file tools are disabled and `workspace_file_tools` remains false | | Process-tree runner | Called only by the trusted CLI `run`, through `run_confined`. Windows uses a Job Object assigned at creation and a per-workspace AppContainer. Linux uses a process group, seccomp, `close_range` (kernel 5.11 or later, x86_64 or aarch64), and Landlock ABI 3 or later. Tested on Windows 11 build 26300 and WSL 6.6 only ([execution](execution.md)) | | Tree limits | Every trusted run. Windows uses job memory and active-process limits and job CPU accounting. Linux needs a delegated cgroup v2 with `memory` and `pids` (Linux 5.14 or later for `cgroup.kill`), normally from a `systemd-run --user` scope, so it needs a user service manager. Disk growth is sampled by the supervisor on both platforms; it is not a filesystem quota. Tested on Windows 11 build 26300 and WSL 6.6 only ([tree limits](execution.md#tree-limits)) | -| Package networking | Profiles are recorded on both platforms; no run accepts one, `--network` remains all-or-nothing, and no unprivileged destination filter exists on either platform. The trusted `packages fetch` command uses system TLS trust and a per-workspace cache root. The root is exposed read-only on Linux and Windows; Windows needs a read-execute container grant and traversable ancestors, and Linux falls back to application storage when no root is selected (ADR-0034, ADR-0035, ADR-0037) ([fetch](execution.md#trusted-package-fetch)) | +| Package networking | Profiles are recorded on both platforms; no run accepts one, `--network` remains all-or-nothing, and no unprivileged destination filter exists on either platform. The trusted `packages fetch` command uses system TLS trust and a per-workspace cache root, and sends only a credential the operator bound to the matched origin (ADR-0056). The root is exposed read-only on Linux and Windows; Windows needs a read-execute container grant and traversable ancestors, and Linux falls back to application storage when no root is selected (ADR-0034, ADR-0035, ADR-0037) ([fetch](execution.md#trusted-package-fetch)) | | Trusted runs | `omgcode run` with operator-declared toolchain profiles on Linux and Windows. Acceptance passed for Rust, Python, Node, C/C++, and Go on both hosts; Java has not run ([execution](execution.md#known-limits)) | | Linux broker prerequisites | Linux 5.6 or later for required `openat2`; `/proc/self/fd` must be available for pinned-file reads; unsupported features fail closed | | Checkpoint preparation/storage | Core API on Linux and Windows. Linux requires openat2/procfs, current-user mode0700 directories/mode0600 files, native birth-time metadata and real file/directory sync. Windows requires NTFS, exact protected per-user DACLs, and a trusted data root (ADR-0023) | diff --git a/docs/security-coding-rules.md b/docs/security-coding-rules.md index fd3ecd6..aace607 100644 --- a/docs/security-coding-rules.md +++ b/docs/security-coding-rules.md @@ -306,8 +306,15 @@ crate. loopback, and alternate socket families. See ADR-0034. - A trusted `packages fetch` is an operator action, never a model tool. It may contact only a declared origin and path prefix, must disable redirects and - proxy variables, send no credentials, cap and hash the response, and write - only the private per-workspace cache. Record the intent and outcome without + proxy variables, cap and hash the response, and write only the private + per-workspace cache. It sends no credential unless the operator bound one to + the matched origin (ADR-0056): then it sends only that origin's credential, + read through core's load under the open control store, as one sensitive + `Authorization` header, and never to another origin. A bound credential that + cannot be read stops the fetch before any request; it never falls back to no + credential or another credential. Never put it in a URL, run environment, + journal, report, error text, or model context, and record only its opaque + identifier. Record the intent and outcome without the URL text. A run may read only the selected per-workspace cache root; do not expose its parent, journals, settings, or another workspace's root. On Windows the root needs a read-execute container grant, and overlapping cache diff --git a/docs/testing-standards.md b/docs/testing-standards.md index 27314af..7b8cdb5 100644 --- a/docs/testing-standards.md +++ b/docs/testing-standards.md @@ -169,7 +169,7 @@ As the relevant components are implemented, include: | Processes | Descendant escape attempts, limits, cleanup, startup failure | | Disk limits | Sampled growth ends the whole run as `disk`, baseline excludes prior content, links and reparse points are not followed, the ceiling stops a scan early, unreadable or oversized roots fail closed | | Networking | Disabled egress, host-local access, redirects, provider routing | -| Package network | Profile storage and the trusted fetch: recording, replay, revision, origin normalization, duplicate and invalid-origin refusal, removal, task-network gating, undeclared-origin and path refusal before a request, redirect refusal, size limits, cache-name validation, existing-cache refusal, remaining-time send and body reads under a trickled response, atomic no-replace publication under a concurrent collision, private cache content, and start/finish audit without the URL text. Cache roots cover per-workspace set/clear/replay, overlap refusals, grant revocation when a root changes or clears, stale foreign-grant refusal, refusal of a root another workspace can already read, the configured fetch location, Linux read/write/parent denial, Windows access with a scoped grant, and Windows refusal without one or without a selected root. No run accepts a profile. A future run-side mechanism must add allowed-origin success, undeclared-origin refusal, redirect and credential refusal, offline fallback, and platform refusal ([ADR-0034](adr/0034-gate-package-networking-behind-enforceable-destination-control.md), [ADR-0035](adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md), [ADR-0037](adr/0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md)) | +| Package network | Profile storage and the trusted fetch: recording, replay, revision, origin normalization, duplicate and invalid-origin refusal, removal, task-network gating, undeclared-origin and path refusal before a request, redirect refusal, size limits, cache-name validation, existing-cache refusal, remaining-time send and body reads under a trickled response, atomic no-replace publication under a concurrent collision, private cache content, and start/finish audit without the URL text. Cache roots cover per-workspace set/clear/replay, overlap refusals, grant revocation when a root changes or clears, stale foreign-grant refusal, refusal of a root another workspace can already read, the configured fetch location, Linux read/write/parent denial, Windows access with a scoped grant, and Windows refusal without one or without a selected root. No run accepts a profile. Registry credentials cover: bind, rotate, unbind, profile-removal unbinding, and replay with no secret in the journal; refusal of an unknown profile or origin, a stale revision, a credential identifier bound twice (including across a provider binding), and tampered replay records; earlier journals without the new fields; secret validation (control characters, whitespace, `basic` without a user and colon, oversize) before any native write; the shared store, uncertain-write, and refusal-before-write ordering; and, against a loopback fixture with the real native store, the exact bearer and basic `Authorization` header in the raw request head, no header for an unbound origin or a narrower nested origin, no request at all when the bound secret cannot be read, a redirect from a credentialed origin leaving its target untouched, the start record carrying `credential_id` only when used, and no secret in errors, audit, reports, CLI output, or any data-root file. No run test is added: a run never receives the credential, so the run suites' curated-environment test covers it ([ADR-0056](adr/0056-send-registry-credentials-to-one-declared-origin.md)). A future run-side mechanism must add allowed-origin success, undeclared-origin refusal, redirect and credential refusal, offline fallback, and platform refusal ([ADR-0034](adr/0034-gate-package-networking-behind-enforceable-destination-control.md), [ADR-0035](adr/0035-fetch-packages-through-a-trusted-allowlisted-cache.md), [ADR-0037](adr/0037-use-a-per-workspace-cache-root-and-a-scoped-windows-grant.md)) | | Providers | Partial/malformed streams, duplicate calls, interruption, retries | | Provider profiles | Kind/authentication pairing, duplicate and invalid names, case-insensitive lookup, revisioned add/remove, replay, no credential or endpoint storage, and capability reporting. No cloud adapter or login is exercised | | Provider credentials | Identifier format refused before I/O; empty, oversized, and line-ending input; `Debug` redaction; native round trip, create-only refusal, not-found, and delete on both hosts; on Windows, edited, truncated, and identifier-swapped blobs as corrupt and broadened or unprotected DACLs refused; on Linux, exact item attributes and label and duplicate items as corrupt; revisioned bind/unbind and replay; refusal for subscription and `none` profiles before any native store; rotation deleting the old item; a binding refused before any journal write deleting the new item; a binding whose journal sync failed keeping and naming the new item, with the binding on disk naming a present item; store-failure cleanup classification, cleanup of a left item, and an identifier kept when cleanup fails; on Windows, an injected directory-sync failure after a complete write removing the file, and with a failed removal as well, both failures reported and the file kept loadable and deletable; the secret read before the control store opens; on Linux, a pending placeholder loading as corrupt, a mock service's create and delete prompts refused without `Prompt` being called, and a create reply carrying both an item and a prompt refused with its placeholder deleted; profile removal naming its credential; and no secret in stdout, stderr, the control journal, or any file under the data root. A locked Linux collection is not exercised, because unlocking it again needs a prompt ([ADR-0042](adr/0042-store-provider-credentials-in-protected-os-storage.md)) | diff --git a/docs/workspace-permissions.md b/docs/workspace-permissions.md index f9037fc..412d4ca 100644 --- a/docs/workspace-permissions.md +++ b/docs/workspace-permissions.md @@ -206,6 +206,14 @@ the URL text), the cache name, limits, HTTP status, byte count, and content digest. A failed fetch records a controlled reason; an abandoned fetch leaves its start event. A control journal written by 0.21.0 cannot be read by 0.20.0. +A registry credential bound to one origin of a profile +([ADR-0056](adr/0056-send-registry-credentials-to-one-declared-origin.md)) +appends `package_credential_bound` and `package_credential_removed` events, +each of which advances the settings revision. `package_profile_removed` then +lists the identifiers it unbinds, and `package_fetch_started` records the +`credential_id` of a credential it attached. A journal that holds these events +cannot be read by an earlier version. Journals record only opaque identifiers. + ## Package cache roots `workspace set-package-cache --revision ` selects one From 2a8b529d6bea50c1be044e36215f729ae8ddb716 Mon Sep 17 00:00:00 2001 From: James Chapman Date: Tue, 17 Nov 2026 21:33:51 +0000 Subject: [PATCH 3/4] feat: add checkpointed delete and no-replace rename model tools Add approval-gated workspace_rename_file (no-replace rename, no checkpoint) and workspace_delete_file (removal after a durable checkpoint). Both reuse the file_write approval and writers-stopped gate under their own digest domains. Operation records gain an optional kind (replace or remove); trusted file restore --removed recreates a deleted file. Tool schema 0.5.0, version 0.38.0, ADR-0055. Linux paths are not compiled or run here. Co-Authored-By: Claude Sonnet 5.5 --- AGENTS.md | 17 +- CHANGELOG.md | 74 ++ Cargo.lock | 10 +- Cargo.toml | 2 +- IMPLEMENTATION_PLAN.md | 50 +- README.md | 34 +- crates/omgcode-app/src/tool_mode.rs | 15 +- crates/omgcode-app/tests/desktop_approvals.rs | 115 +++ crates/omgcode-cli/src/file_cli.rs | 60 +- crates/omgcode-cli/src/main.rs | 2 +- .../omgcode-cli/tests/checkpoint_lifecycle.rs | 51 ++ crates/omgcode-cli/tests/file_workflow.rs | 2 +- crates/omgcode-cli/tests/model_tools.rs | 250 ++++++- crates/omgcode-core/src/file_audit.rs | 195 ++++++ crates/omgcode-core/src/files.rs | 341 ++++++++- crates/omgcode-core/src/files/checkpoint.rs | 16 +- crates/omgcode-core/src/files/replacement.rs | 157 ++++- .../src/files/replacement/tests.rs | 348 +++++++++- crates/omgcode-core/src/replacement.rs | 35 +- .../omgcode-core/src/replacement/manifest.rs | 10 +- crates/omgcode-core/src/replacement/replay.rs | 50 +- crates/omgcode-core/src/replacement/store.rs | 57 +- crates/omgcode-core/src/turn.rs | 473 ++++++++++++- crates/omgcode-core/src/turn/review.rs | 109 ++- crates/omgcode-core/tests/approval_review.rs | 287 ++++++++ .../omgcode-core/tests/replacement_records.rs | 255 ++++++- crates/omgcode-core/tests/turn_runtime.rs | 655 +++++++++++++++++- .../ui/tests/approvals.test.ts | 41 ++ crates/omgcode-platform/src/broker.rs | 98 +++ crates/omgcode-platform/src/broker/linux.rs | 9 +- .../src/broker/linux/rename.rs | 366 ++++++++++ .../src/broker/linux/replacement.rs | 159 ++++- crates/omgcode-platform/src/broker/windows.rs | 38 +- .../src/broker/windows/rename.rs | 472 +++++++++++++ .../src/broker/windows/replacement.rs | 87 ++- .../src/broker/windows/replacement/tests.rs | 100 +++ .../src/private_store/windows.rs | 22 +- docs/README.md | 24 +- ...elete-and-no-replace-rename-model-tools.md | 153 ++++ docs/adr/index.md | 1 + docs/agent-instructions.md | 22 +- docs/desktop-interface.md | 17 +- docs/development.md | 91 ++- docs/execution.md | 8 +- docs/file-access.md | 132 +++- docs/platform-support.md | 2 +- docs/security-coding-rules.md | 22 +- docs/session-storage.md | 11 +- docs/testing-standards.md | 1 + docs/workspace-permissions.md | 25 +- 50 files changed, 5346 insertions(+), 225 deletions(-) create mode 100644 crates/omgcode-platform/src/broker/linux/rename.rs create mode 100644 crates/omgcode-platform/src/broker/windows/rename.rs create mode 100644 docs/adr/0055-add-checkpointed-delete-and-no-replace-rename-model-tools.md diff --git a/AGENTS.md b/AGENTS.md index 58bf867..fafa13b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,14 +13,17 @@ without an installer, has run only on Windows, and gives the webview no authorit Rust side authorizes every command and the trusted CLI remains. Follow the [desktop interface](docs/desktop-interface.md) contract. Chat offers the local LM Studio model read and list tools; when the workspace -also enables `read` and `write`, three file-write tools (exact edit, multi-edit, -and create); and, when it also enables `execute`, `read`, and `write`, one -command tool. The file-write and command tools each require a trusted -single-use approval. The edit tool replaces one exact text occurrence in an -existing file, and the multi-edit tool several exact occurrences in one +also enables `read` and `write`, five file-write tools (exact edit, multi-edit, +create, rename, and delete); and, when it also enables `execute`, `read`, and +`write`, one command tool. The file-write and command tools each require a +trusted single-use approval. The edit tool replaces one exact text occurrence +in an existing file, and the multi-edit tool several exact occurrences in one existing file, each through checkpointed apply. The create tool makes one new -file through the exclusive create, without a checkpoint. All three need the -operator's writers-stopped acknowledgement. No model delete, rename, network, +file through the exclusive create, without a checkpoint. The rename tool moves +one regular file to a name that does not exist and never replaces an entry, +without a checkpoint. The delete tool removes one regular file of at most 1 MiB +after a durable checkpoint of its bytes, which the trusted CLI can restore. All +five need the operator's writers-stopped acknowledgement. No model network, Git, or MCP tool exists. Chat offers tools only for a workspace whose `model_tools` preference is enabled. There are no unapproved model writes, other provider adapters, or unreviewed model-callable process diff --git a/CHANGELOG.md b/CHANGELOG.md index 31473c1..42a1535 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,80 @@ Changes are recorded here before a release version is assigned. ### Added +- Version 0.38.0 adds two approval-gated model file tools, rename and delete + (Phase C19). See ADR-0055 and the + [rename and delete routes](docs/file-access.md#model-rename-and-delete-routes-adr-0055). + Until now the model could neither move nor remove a file. It is on branch + `codex/model-file-tools`, stacked on `codex/checkpoint-ledger-compaction`; it + is not merged. It adds no dependency. + - **Tools:** `workspace_rename_file` moves one regular file to a name that + does not exist and never replaces an entry. `workspace_delete_file` removes + one regular file after a durable checkpoint of its bytes. Both are advertised + and dispatched only under live `model_tools`, `read`, and `write`, with the + edit tool's per-call recheck and `edit_disabled` denial, in the order read, + list, edit, multi-edit, create, rename, delete, command. Tool schema 0.5.0; + 0.2.0 to 0.4.0 sessions still replay. Eight of eight tool-name slots are used, + and no cap rose. + - **Approvals:** both reuse the `file_write` operation, so the + writers-stopped acknowledgement is required and no enum, wire type, or + TypeScript union changed. Each has its own digest domain tag + (`omgcode-file-rename-v1`, `omgcode-file-delete-v1`), so an approval for any + of the five file-write tools is never consumed by another. Before an + approval is recorded, a live audited read supplies the source's exact size + and SHA-256; a source over 1 MiB is the recoverable `edit_too_large`, and + non-UTF-8 content is accepted. A rename also needs an absent destination + (`file_exists`) and an existing parent (`not_found`). + - **Rename:** a new `FileBroker::rename_file` records a `rename_file` + file-operation intent that names both the source and the destination, then + calls the platform's `rename_no_replace`. The source is pinned, its bytes + are verified through the pinned handle, and directories, links, junctions, + hard-linked files, and other filesystems are refused. Linux uses + `renameat2` with `RENAME_NOREPLACE` and compares the name with the pinned + inode before and after. Windows holds a DELETE handle that denies other + writers and deleters and renames with POSIX semantics and no replace flag. + An error after the native call is uncertain, leaves the audit record pending, + and is never retried. Rename is not checkpointed. + - **Delete:** a new `FileBroker::apply_removal` follows the checkpointed + apply without a stage: the same binding, permission, admission, and + qualified-filesystem gates, a durable checkpoint, a recheck, commit intent, + one native removal, and the same uncertain-outcome recovery. Linux unlinks by + name and reports a swapped name as uncertain from the pinned inode's link + count; Windows deletes through the verified handle. Neither is power-loss + qualified. + - **Ledger:** operation records gain an optional `kind` of `replace` or + `remove`, omitted for `replace` so every earlier ledger, carried record, and + manifest stays byte-identical. A `remove` proposes exactly the empty artifact, + has no stage, and goes from `checkpoint_prepared` to `commit_intended`. + Replay refuses a `remove` that proposes bytes, has a stage record, or carries + a stage identity; compaction carries `kind`. Older binaries refuse a ledger + that holds a `remove` record. A `remove` entry's original need not be UTF-8. + - **Restore:** trusted `file restore --removed` (and + `FileBroker::restore_removed_file`) recreates a deleted file through the + audited exclusive create when its name is free, records no new operation, and + does not restore its mode, owner, access list, or timestamps. A replacement + checkpoint and a removal checkpoint each refuse the other route. + - **Review:** the desktop approvals window reproduces the digest from any of the + five file-write tools (at most eight distinct candidates) and shows `From`, + `To`, `Size`, and `SHA-256` for a rename and `File`, `Size`, and `SHA-256` + for a delete. A rename review is stale once its source changed or its + destination exists; a delete review is stale once the file changed or is gone. + - **Limits:** neither tool detects a concurrent writer, and neither is + power-loss qualified. The context estimate for tool definitions rises from + 5 KiB to 6 KiB (the eight definitions serialize to 5,968 bytes). The Linux + paths are not qualified on a Linux host. A bounded read that exceeds its + limit now carries a typed marker, `is_oversize_entry`, so a source over + 1 MiB is told from other invalid objects; the error kind is unchanged. + - **Tests:** new platform tests cover the no-replace rename (existing target, + directories, junctions, hard links, changed source, a swap the sharing mode + prevents, and faults before and after the call) and the verified-handle + removal (swapped name refused, single attempt, faults). New core tests cover + the `remove` ledger rules and refusals, removal and restore through the + broker, the audit record, strict parsing, the approval flow, cross-tool + non-consumption, refusals, live-permission gating, output-fit refusal, schema + 0.4.0 replay, the eight-tool list, and review fields and staleness. New app, + CLI, and frontend tests drive both tools through the desktop service, the + operator flow, and `file restore --removed`. + - Version 0.37.0 adds registry credentials for the trusted `packages fetch` (Phase C20). See ADR-0056 and [registry credentials](docs/execution.md#registry-credentials). Until now diff --git a/Cargo.lock b/Cargo.lock index e8ed3b5..1e867ca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2522,7 +2522,7 @@ dependencies = [ [[package]] name = "omgcode-app" -version = "0.37.0" +version = "0.38.0" dependencies = [ "directories", "omgcode-core", @@ -2537,7 +2537,7 @@ dependencies = [ [[package]] name = "omgcode-cli" -version = "0.37.0" +version = "0.38.0" dependencies = [ "clap", "omgcode-app", @@ -2550,7 +2550,7 @@ dependencies = [ [[package]] name = "omgcode-core" -version = "0.37.0" +version = "0.38.0" dependencies = [ "libc", "omgcode-platform", @@ -2568,7 +2568,7 @@ dependencies = [ [[package]] name = "omgcode-desktop" -version = "0.37.0" +version = "0.38.0" dependencies = [ "omgcode-app", "serde", @@ -2580,7 +2580,7 @@ dependencies = [ [[package]] name = "omgcode-platform" -version = "0.37.0" +version = "0.38.0" dependencies = [ "libc", "rustix", diff --git a/Cargo.toml b/Cargo.toml index e0379b8..0f00a9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/omgcode-core", "crates/omgcode-cli", "crates/omgcode-platform", "crates/omgcode-app", "crates/omgcode-desktop"] [workspace.package] -version = "0.37.0" +version = "0.38.0" edition = "2024" rust-version = "1.99.1" license = "Apache-2.0" diff --git a/IMPLEMENTATION_PLAN.md b/IMPLEMENTATION_PLAN.md index 6a8c975..6404e62 100644 --- a/IMPLEMENTATION_PLAN.md +++ b/IMPLEMENTATION_PLAN.md @@ -127,7 +127,7 @@ Implemented today: auto-compaction for the local LM Studio route. Summaries are labelled untrusted evidence and follow the live tool-result replay rule (ADR-0041). - Core turn/tool runtime for read/list, the approval-gated exact edit, - multi-edit, and create, and the approval-gated command through a caller-supplied model interface. It has strict tool-call parsing, round/call/byte/prompt limits, + multi-edit, create, rename, and delete, and the approval-gated command through a caller-supplied model interface. It has strict tool-call parsing, round/call/byte/prompt limits, cooperative cancellation, source-labelled messages, and fresh per-operation policy bindings. Chat dispatches it through the LM Studio adapter for a workspace that enables `model_tools`. @@ -138,8 +138,9 @@ Implemented today: `chat --stream` and the desktop shell stream the reply as provisional text (ADR-0049). It is text-only unless the session's workspace enables `model_tools`; then it offers the read - and list tools, the approval-gated edit, multi-edit, and create tools when - `read` and `write` also allow it, and the approval-gated command tool when + and list tools, the approval-gated edit, multi-edit, create, rename, and + delete tools when `read` and `write` also allow it, and the approval-gated + command tool when `execute`, `read`, and `write` also allow it. See [ADR-0026](docs/adr/0026-offer-read-list-tools-through-a-workspace-preference.md), @@ -205,8 +206,8 @@ Implemented today: (ADR-0038). Trusted approval records bind a future model-tool call to a session, workspace, operation, request digest, and settings revision, expire, and are consumed once (ADR-0039). Provider profiles enable no connection; the - command, edit, multi-edit, and create tools are the only model tools that - consume an approval record. + command, edit, multi-edit, create, rename, and delete tools are the only model + tools that consume an approval record. - Provider credentials in protected OS storage (ADR-0042): DPAPI files with verified private DACLs on Windows and Secret Service items in the default collection on Linux, with no plaintext fallback. Trusted @@ -238,8 +239,17 @@ Implemented today: approvals and writers-stopped acknowledgement, and each has its own digest domain. Multi-edit applies one to 16 non-overlapping exact replacements through one checkpointed apply; create makes one new file through the - exclusive create without a checkpoint. Tool schema 0.4.0; no model delete, - rename, network, Git, or MCP tool exists. + exclusive create without a checkpoint. Tool schema 0.4.0. +- The `workspace_rename_file` and `workspace_delete_file` model tools (0.38.0, + ADR-0055, on branch `codex/model-file-tools`, not merged) are offered under + the same live preferences, share the `file_write` approvals and + writers-stopped acknowledgement, and each has its own digest domain. Rename + moves one file to a name that does not exist through a no-replace primitive, + without a checkpoint; delete removes one file after a durable checkpoint of + its bytes, under a new `remove` kind of checkpoint record. Both refuse a + source over 1 MiB. The trusted `file restore --removed` recreates a deleted + file without its former mode or access list. Tool schema 0.5.0; no model + network, Git, or MCP tool exists. - Trusted `file restore` through a new checkpoint and `file delete-checkpoint --permanent` for one resolved checkpoint, on the same filesystems as apply. Neither is power-loss qualified. See @@ -254,9 +264,9 @@ Implemented today: Not implemented: -- Apply or restore on other filesystems, workspace file deletion, ledger - compaction, and standalone CLI checkpoint preparation. -- Model delete, rename, network, Git, and MCP tools; +- Apply or restore on other filesystems, standalone CLI checkpoint + preparation, and a trusted CLI workspace-file delete. +- Model network, Git, and MCP tools; concurrent-writer detection for any apply route; live provider tool-call qualification; and streaming against a live server, discovery, and cancellation of any request against a live provider. A read, list, or edit @@ -995,6 +1005,24 @@ on Windows, and trusted CLI commands as the first caller. See sessions still replay. The 4 KiB argument cap bounds create content and multi-edit size, there is no multi-file atomicity, and 6 of the 8 tool-name slots are used. +- **C19 (version 0.38.0):** the `workspace_rename_file` and + `workspace_delete_file` model tools (ADR-0055), on branch + `codex/model-file-tools` and not merged. Both are offered and dispatched under + the edit tool's live `model_tools`, `read`, and `write` rule and reuse the + `file_write` approval operation with its writers-stopped gate, each under its + own digest domain (`omgcode-file-rename-v1`, `omgcode-file-delete-v1`). Rename + requires an existing source of at most 1 MiB, an absent destination, and an + existing parent before any approval is recorded, then uses a native no-replace + rename (Linux `renameat2`, Windows POSIX-semantics rename) with a new + `rename_file` file-operation audit record naming both names; it is not + checkpointed. Delete uses `FileBroker::apply_removal`, a sibling of the + checkpointed apply with a native removal (Linux `unlinkat` with a link-count + check afterwards, Windows deletion through the verified handle) and a + `remove` kind on the checkpoint ledger, which older binaries refuse. Restoring + a deleted file is a trusted `file restore --removed` through the exclusive + create. Tool schema 0.5.0; 0.2.0 to 0.4.0 sessions still replay. All 8 + tool-name slots are used, and the context estimate rises to 6 KiB. The Linux + paths are not yet qualified on a Linux host. - **C20 (version 0.37.0):** registry credentials for the trusted `packages fetch`. `workspace package-credential-set` and `package-credential-remove` bind one stored secret, `bearer` or `basic`, to @@ -1018,7 +1046,7 @@ Next decisions, not yet scheduled: endpoint rules for non-loopback providers, HTTPS, and certificates (one adapter, the local LM Studio route, now reads a stored credential, only at its profile's loopback endpoint); -- model delete and rename tools, and network, Git, and MCP tools; +- model network, Git, and MCP tools; - development-server networking. ### Phase D: additional providers, streaming, and account lifecycle diff --git a/README.md b/README.md index e36617f..bd797c3 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,9 @@ OMGCode aims to connect language models to controlled development tools. The current code is an early Rust core and command-line prototype. A bounded native workspace file broker is available through trusted CLI commands. A core turn/tool runtime and schema-v2 session format let chat offer the local LM Studio -model read, list, approval-gated edit, multi-edit, create, and command tools, -only for a workspace that enables `model_tools` and each tool's own -preferences. Model file deletion and renaming, and Git workflows -remain planned. A prototype desktop shell (Tauri, with a strict TypeScript +model read, list, approval-gated edit, multi-edit, create, rename, delete, and +command tools, only for a workspace that enables `model_tools` and each tool's +own preferences. Git workflows remain planned. A prototype desktop shell (Tauri, with a strict TypeScript frontend) browses and searches sessions, runs chat, and decides model-tool approvals in a separate window; it has no installer and has run only on Windows. The trusted CLI `run` command starts one program for a workspace whose @@ -33,15 +32,18 @@ directory grants. > approves that exact call. When `read` and `write` are enabled, it offers an > edit tool that replaces one exact text occurrence in an existing file, a > multi-edit tool that replaces up to 16 exact occurrences in one existing -> file, and a create tool that makes one new file. Each records a pending -> approval and acts only after the operator approves that exact call and -> acknowledges that other writers are stopped; edits are applied through a -> checkpoint, and creation is not checkpointed. It has no model delete, rename, -> network, Git, or MCP tools. +> file, a create tool that makes one new file, a rename tool that moves one file +> to a name that does not exist, and a delete tool that removes one file after +> a checkpoint of its bytes. Each records a pending approval and acts only after +> the operator approves that exact call and acknowledges that other writers are +> stopped; edits and deletions are applied through a checkpoint, and creation +> and renaming are not checkpointed. It has no model network, Git, or MCP +> tools. > The trusted > CLI can replace or restore one existing file through a checkpoint on qualified > Linux ext4 or Windows NTFS, and can permanently delete one resolved checkpoint. -> The `file` commands cannot delete workspace files. The trusted CLI `run` +> The `file` commands cannot delete workspace files, though `file restore +> --removed` recreates one that a model deletion removed. The trusted CLI `run` > command executes programs under native confinement, and those programs can > create, change, and delete workspace files without a checkpoint. That > confinement was tested on one Windows build and one Linux kernel only and is @@ -109,7 +111,8 @@ frontend in `crates/omgcode-desktop/ui`. The CLI provides: loopback HTTP endpoint. It does not discover, start, or download models, and it rejects model tool requests unless the session's workspace enables `model_tools`. Then it offers read/list tools; when `read` and `write` are - also enabled, the approval-gated edit, multi-edit, and create tools; and when `execute`, `read`, and + also enabled, the approval-gated edit, multi-edit, create, rename, and delete + tools; and when `execute`, `read`, and `write` are all enabled, the approval-gated command tool. Each is rechecked against live settings before every call. @@ -130,7 +133,7 @@ source-labelled messages. The edit and command tools additionally require a consumed approval; an edit also needs the operator's writers-stopped acknowledgement and writes only through checkpointed apply. `chat` runs it with the local LM Studio adapter for a workspace that enables `model_tools`. -It has no create, delete, rename, Git, MCP, or network tool. +It has no Git, MCP, or network tool. New sessions use schema version 3; version 1 and 2 journals remain readable and writable with their original subsets. @@ -166,8 +169,11 @@ trusted `file compact-checkpoints --discard-history`, or an apply, restore, or deletion that would not fit, compacts the ledger. Neither command is power-loss qualified. The model edit and multi-edit tools use the same checkpointed apply after an approved, writers-stopped decision; the create tool -uses the exclusive create without a checkpoint. None can delete or rename files, -and none detects a concurrent writer. +uses the exclusive create without a checkpoint; the delete tool uses a +checkpointed removal; and the rename tool uses a no-replace rename without a +checkpoint. None detects a concurrent writer, and none is power-loss +qualified. A deleted file is restored with trusted `file restore --removed`, +which does not restore its mode or access list. Model-dispatched process execution, Git, MCP, and memories remain planned. See the [workspace settings contract](docs/workspace-permissions.md), the [workspace file-access guide](docs/file-access.md), and the [development diff --git a/crates/omgcode-app/src/tool_mode.rs b/crates/omgcode-app/src/tool_mode.rs index 570662e..b6aa2b0 100644 --- a/crates/omgcode-app/src/tool_mode.rs +++ b/crates/omgcode-app/src/tool_mode.rs @@ -21,10 +21,10 @@ pub const MODEL_TOOLS_SUPPORTED: bool = cfg!(any(target_os = "linux", target_os /// Conservative byte allowance for the advertised tool definitions. /// -/// The largest set (read, list, edit, multi-edit, create, and command) -/// serializes to 4,546 bytes in the adapter's wire shape; a unit test keeps -/// this allowance above it. -pub(crate) const TOOL_SCHEMA_ESTIMATE_BYTES: usize = 5 * 1024; +/// The largest set (read, list, edit, multi-edit, create, rename, delete, and +/// command) serializes to 5,968 bytes in the adapter's wire shape; a unit test +/// keeps this allowance above it. +pub(crate) const TOOL_SCHEMA_ESTIMATE_BYTES: usize = 6 * 1024; /// Whether this chat turn offers the model the workspace tools. #[derive(Debug)] @@ -117,8 +117,8 @@ pub fn tool_replay_allowed( #[cfg(test)] mod tests { use omgcode_core::turn::{ - command_tool_definition, create_tool_definition, edit_tool_definition, - multi_edit_tool_definition, tool_definitions, + command_tool_definition, create_tool_definition, delete_tool_definition, + edit_tool_definition, multi_edit_tool_definition, rename_tool_definition, tool_definitions, }; use serde_json::{Value, json}; @@ -132,7 +132,10 @@ mod tests { definitions.push(edit_tool_definition()); definitions.push(multi_edit_tool_definition()); definitions.push(create_tool_definition()); + definitions.push(rename_tool_definition()); + definitions.push(delete_tool_definition()); definitions.push(command_tool_definition()); + assert_eq!(definitions.len(), 8, "the eight-tool cap is fully used"); let wire: Vec = definitions .iter() .map(|definition| json!({"type": "function", "function": definition})) diff --git a/crates/omgcode-app/tests/desktop_approvals.rs b/crates/omgcode-app/tests/desktop_approvals.rs index a17192d..e814d75 100644 --- a/crates/omgcode-app/tests/desktop_approvals.rs +++ b/crates/omgcode-app/tests/desktop_approvals.rs @@ -738,3 +738,118 @@ fn a_create_review_becomes_stale_once_the_file_exists() { assert_eq!(detail.review_token, None); assert_eq!(setup.server.finish(), 2); } + +const RENAME_TOOL: &str = "workspace_rename_file"; +const DELETE_TOOL: &str = "workspace_delete_file"; + +#[test] +fn a_rename_approval_is_reviewed_approved_and_consumed_by_the_identical_call() { + let args = json!({"from": "a.txt", "to": "b.txt"}); + let setup = setup(vec![ + Step::reply(tool_call("rename-1", RENAME_TOOL, &args)), + Step::reply(text("waiting for approval")), + Step::reply(tool_call("rename-2", RENAME_TOOL, &args)), + Step::reply(text("renamed")), + ]); + let source = setup.workspace.path.join("a.txt"); + fs::write(&source, "alpha\n").expect("workspace file"); + let session = setup.session("Rename"); + setup.turn(session, "rename a file"); + + let pending = setup.pending(); + assert_eq!(pending.len(), 1); + assert_eq!(pending[0].operation, ApprovalOperationDto::FileWrite); + let id = pending[0].id; + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Exact); + assert_eq!(detail.tool.as_deref(), Some(RENAME_TOOL)); + assert_eq!(field_value(&detail, "From"), "a.txt"); + assert_eq!(field_value(&detail, "To"), "b.txt"); + assert_eq!(field_value(&detail, "Size"), "6 bytes"); + assert!(source.exists()); + let token = detail.review_token.clone().expect("a review token"); + setup + .decide(id, "approve", true, Some(&token)) + .expect("approve"); + + setup.turn(session, "try again"); + assert!(!source.exists()); + assert_eq!( + fs::read_to_string(setup.workspace.path.join("b.txt")).expect("read"), + "alpha\n" + ); + assert_eq!(setup.state_of(id), ApprovalStateDto::Consumed); + assert_eq!(setup.server.finish(), 4); +} + +#[test] +fn a_rename_review_becomes_stale_once_the_destination_exists() { + let args = json!({"from": "a.txt", "to": "b.txt"}); + let setup = setup(vec![ + Step::reply(tool_call("rename-1", RENAME_TOOL, &args)), + Step::reply(text("waiting for approval")), + ]); + fs::write(setup.workspace.path.join("a.txt"), "alpha\n").expect("file"); + let session = setup.session("Rename"); + setup.turn(session, "rename a file"); + let id = setup.pending()[0].id; + assert_eq!(setup.detail(id).binding, Binding::Exact); + fs::write(setup.workspace.path.join("b.txt"), "someone else").expect("other writer"); + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Stale); + assert_eq!(detail.review_token, None); + assert_eq!(setup.server.finish(), 2); +} + +#[test] +fn a_delete_approval_is_reviewed_approved_and_consumed_by_the_identical_call() { + let args = json!({"path": "old.txt"}); + let setup = setup(vec![ + Step::reply(tool_call("delete-1", DELETE_TOOL, &args)), + Step::reply(text("waiting for approval")), + Step::reply(tool_call("delete-2", DELETE_TOOL, &args)), + Step::reply(text("deleted")), + ]); + let target = setup.workspace.path.join("old.txt"); + fs::write(&target, "alpha\n").expect("workspace file"); + let session = setup.session("Delete"); + setup.turn(session, "delete a file"); + + let pending = setup.pending(); + assert_eq!(pending.len(), 1); + let id = pending[0].id; + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Exact); + assert_eq!(detail.tool.as_deref(), Some(DELETE_TOOL)); + assert_eq!(field_value(&detail, "File"), "old.txt"); + assert_eq!(field_value(&detail, "Size"), "6 bytes"); + assert!(target.exists()); + let token = detail.review_token.clone().expect("a review token"); + setup + .decide(id, "approve", true, Some(&token)) + .expect("approve"); + + setup.turn(session, "try again"); + assert!(!target.exists()); + assert_eq!(setup.state_of(id), ApprovalStateDto::Consumed); + assert_eq!(setup.server.finish(), 4); +} + +#[test] +fn a_delete_review_becomes_stale_once_the_file_changes() { + let args = json!({"path": "old.txt"}); + let setup = setup(vec![ + Step::reply(tool_call("delete-1", DELETE_TOOL, &args)), + Step::reply(text("waiting for approval")), + ]); + fs::write(setup.workspace.path.join("old.txt"), "alpha\n").expect("file"); + let session = setup.session("Delete"); + setup.turn(session, "delete a file"); + let id = setup.pending()[0].id; + assert_eq!(setup.detail(id).binding, Binding::Exact); + fs::write(setup.workspace.path.join("old.txt"), "alpha changed\n").expect("change"); + let detail = setup.detail(id); + assert_eq!(detail.binding, Binding::Stale); + assert_eq!(detail.review_token, None); + assert_eq!(setup.server.finish(), 2); +} diff --git a/crates/omgcode-cli/src/file_cli.rs b/crates/omgcode-cli/src/file_cli.rs index 3e156c9..e1f28ab 100644 --- a/crates/omgcode-cli/src/file_cli.rs +++ b/crates/omgcode-cli/src/file_cli.rs @@ -92,11 +92,15 @@ pub(crate) enum FileCommand { #[arg(long, required = true, action = clap::ArgAction::SetTrue)] writers_stopped: bool, }, - /// Restore a retained checkpoint's original bytes over the current file. + /// Restore a retained checkpoint's original bytes. /// - /// The expected digest and size describe the current file, never the - /// checkpoint. The current bytes are checkpointed again before publication. - /// Stop every other writer; this does not provide atomic compare-and-swap. + /// For a replacement checkpoint, the expected digest and size describe the + /// current file, never the checkpoint, and the current bytes are + /// checkpointed again before publication. With `--removed`, the checkpoint + /// must be one that a removal recorded: the file must not exist and is + /// recreated through the exclusive create, without its former mode or + /// access list, and no new checkpoint is recorded. Stop every other + /// writer; this does not provide atomic compare-and-swap. Restore { #[command(flatten)] scope: Scope, @@ -106,11 +110,15 @@ pub(crate) enum FileCommand { #[arg(long)] checkpoint: Uuid, /// Expected SHA-256 of the current file's exact bytes. - #[arg(long)] - expected_sha256: String, + #[arg(long, required_unless_present = "removed", conflicts_with = "removed")] + expected_sha256: Option, /// Expected byte count of the current file. + #[arg(long, required_unless_present = "removed", conflicts_with = "removed")] + expected_bytes: Option, + /// The checkpoint recorded a file that a removal deleted; recreate it. + /// The name must be free, so there is no current file to describe. #[arg(long)] - expected_bytes: u64, + removed: bool, /// Acknowledge that all other writers are stopped. This does not provide CAS. #[arg(long, required = true, action = clap::ArgAction::SetTrue)] writers_stopped: bool, @@ -166,6 +174,16 @@ struct RestoreOutput { receipt: omgcode_core::replacement::ReplacementReceipt, } +/// Output of restoring a deleted file: the checkpoint it read and the audited +/// creation that recreated the file. +#[cfg(any(target_os = "linux", windows))] +#[derive(serde::Serialize)] +struct RestoreRemovedOutput { + restored_from: Uuid, + #[serde(flatten)] + created: omgcode_core::files::FileCreated, +} + /// Open existing trusted settings. No command silently chooses a development root. pub(crate) fn run(command: FileCommand, data_root: &Path) -> Result<(), CliError> { let mut control = ControlStore::open_existing(data_root) @@ -291,11 +309,19 @@ pub(crate) fn run(command: FileCommand, data_root: &Path) -> Result<(), CliError checkpoint, expected_sha256, expected_bytes, + removed, writers_stopped: _, } => { #[cfg(not(any(target_os = "linux", windows)))] { - let _ = (scope, path, checkpoint, expected_sha256, expected_bytes); + let _ = ( + scope, + path, + checkpoint, + expected_sha256, + expected_bytes, + removed, + ); Err(CliError::new( "file_unsupported", "File restore is available only on Linux and Windows", @@ -307,6 +333,24 @@ pub(crate) fn run(command: FileCommand, data_root: &Path) -> Result<(), CliError .bind_session(scope.session, scope.workspace) .map_err(|error| CliError::new("control", error.to_string()))?; let target = target(scope, path); + if removed { + let created = FileBroker::new(&mut control) + .restore_removed_file(&binding, &target, checkpoint) + .map_err(file_error)?; + return print_json(&RestoreRemovedOutput { + restored_from: checkpoint, + created, + }); + } + // Clap requires both unless `--removed` is present. + let (Some(expected_sha256), Some(expected_bytes)) = + (expected_sha256, expected_bytes) + else { + return Err(CliError::new( + "usage", + "restore needs --expected-sha256 and --expected-bytes, or --removed", + )); + }; let expected = RecordedArtifact { bytes: expected_bytes, sha256: expected_sha256, diff --git a/crates/omgcode-cli/src/main.rs b/crates/omgcode-cli/src/main.rs index c6c8a0b..72a588c 100644 --- a/crates/omgcode-cli/src/main.rs +++ b/crates/omgcode-cli/src/main.rs @@ -333,7 +333,7 @@ fn run(cli: Cli) -> Result<(), CliError> { "unavailable" }, "tool_policy": if cfg!(any(target_os = "linux", target_os = "windows")) { - "read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_and_command_require_approval" + "read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_rename_delete_and_command_require_approval" } else { "reject all model-requested tools" }, diff --git a/crates/omgcode-cli/tests/checkpoint_lifecycle.rs b/crates/omgcode-cli/tests/checkpoint_lifecycle.rs index 61de838..04cc6c6 100644 --- a/crates/omgcode-cli/tests/checkpoint_lifecycle.rs +++ b/crates/omgcode-cli/tests/checkpoint_lifecycle.rs @@ -262,3 +262,54 @@ fn compacting_unused_storage_creates_nothing() { assert!(!fixture.data.join("replacement-v1").exists()); assert!(!fixture.data.join("replacement-init-v1.jsonl").exists()); } + +/// `--removed` restores only the checkpoint of a removed file, so a +/// replacement checkpoint is refused. It excludes the current-file +/// precondition, and a half-specified precondition is a usage error. +#[cfg(any(target_os = "linux", windows))] +#[test] +fn a_removed_restore_refuses_a_replacement_checkpoint_and_mixed_preconditions() { + let fixture = Fixture::new(ORIGINAL); + let target = fixture.workspace.join("note.txt"); + let applied = fixture.apply_bytes(PROPOSED); + let current = fixture.current_sha256(); + let workspace_id = fixture.workspace_id.to_string(); + let session_id = fixture.session_id.to_string(); + let base = [ + "file", + "restore", + workspace_id.as_str(), + "note.txt", + "--session", + session_id.as_str(), + "--checkpoint", + applied.as_str(), + "--writers-stopped", + ]; + let mut removed = base.to_vec(); + removed.push("--removed"); + let output = run_cli(&fixture.data, &removed); + assert_error_code(&output, "checkpoint_unavailable"); + assert_eq!(fs::read(&target).expect("unchanged"), PROPOSED); + // Without `--removed` the current-file precondition is required. + let output = run_cli(&fixture.data, &base); + assert!(!output.status.success()); + assert!(String::from_utf8_lossy(&output.stderr).contains("--expected-sha256")); + // The two ways exclude each other. + let mut both = removed.clone(); + both.extend_from_slice(&[ + "--expected-sha256", + current.as_str(), + "--expected-bytes", + "1", + ]); + assert!(!run_cli(&fixture.data, &both).status.success()); + + let mut half = base.to_vec(); + half.extend_from_slice(&["--expected-sha256", current.as_str()]); + assert!( + !run_cli(&fixture.data, &half).status.success(), + "an expected digest without a size is a usage error" + ); + assert_eq!(fs::read(&target).expect("unchanged"), PROPOSED); +} diff --git a/crates/omgcode-cli/tests/file_workflow.rs b/crates/omgcode-cli/tests/file_workflow.rs index 08179ab..a81021c 100644 --- a/crates/omgcode-cli/tests/file_workflow.rs +++ b/crates/omgcode-cli/tests/file_workflow.rs @@ -263,7 +263,7 @@ fn cli_creates_lists_reads_and_audits_files_without_recording_contents() { assert_eq!( capabilities["tool_policy"], if cfg!(any(target_os = "linux", target_os = "windows")) { - "read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_and_command_require_approval" + "read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_rename_delete_and_command_require_approval" } else { "reject all model-requested tools" } diff --git a/crates/omgcode-cli/tests/model_tools.rs b/crates/omgcode-cli/tests/model_tools.rs index 8f30ea1..c876971 100644 --- a/crates/omgcode-cli/tests/model_tools.rs +++ b/crates/omgcode-cli/tests/model_tools.rs @@ -338,7 +338,9 @@ fn an_enabled_preference_lets_the_model_read_the_workspace_through_the_runtime() "workspace_list_directory", "workspace_edit_file", "workspace_multi_edit_file", - "workspace_create_file" + "workspace_create_file", + "workspace_rename_file", + "workspace_delete_file" ] ); // The compiled base prompt remains the only system message. @@ -1739,3 +1741,249 @@ fn an_approved_model_create_makes_the_file_through_the_operator_flow() { "creation is not checkpointed" ); } + +/// The operator flow for a model rename: approval, then one no-replace rename +/// with no checkpoint. A repeat is refused because the source is gone. +#[test] +fn an_approved_model_rename_moves_the_file_through_the_operator_flow() { + let fixture = Fixture::new(NOTE); + let journal = fixture.session(); + fixture.set("--model-tools", true); + let rename = json!({"from": "note.txt", "to": "moved.txt"}); + + let server = SequenceServer::start(vec![ + tool_call("call-1", "workspace_rename_file", &rename), + text("Waiting for operator approval."), + ]); + let first = fixture.chat(&journal, &server.endpoint, "rename the note"); + let requests = server.finish(); + assert_success(&first); + let names: Vec<&str> = requests[0]["tools"] + .as_array() + .expect("tools") + .iter() + .map(|tool| tool["function"]["name"].as_str().expect("name")) + .collect(); + assert!(names.contains(&"workspace_rename_file"), "{names:?}"); + assert!(names.contains(&"workspace_delete_file"), "{names:?}"); + assert!(fixture.workspace.join("note.txt").exists()); + + let listed = run_cli(&fixture.data, &["approvals", "list"]); + assert_success(&listed); + let approvals = json_output(&listed)["approvals"] + .as_array() + .expect("approvals") + .to_vec(); + assert_eq!(approvals.len(), 1, "one pending request: {approvals:?}"); + assert_eq!(approvals[0]["operation"], "file_write"); + let summary = approvals[0]["summary"].as_str().expect("summary"); + assert!( + summary.starts_with(&format!( + "rename note.txt -> moved.txt: {} bytes, sha256 ", + NOTE.len() + )), + "{summary}" + ); + let approval = approvals[0]["id"].as_str().expect("approval id").to_owned(); + assert_error_code( + &run_cli(&fixture.data, &["approvals", "approve", &approval]), + "approval", + ); + assert_success(&run_cli( + &fixture.data, + &["approvals", "approve", &approval, "--writers-stopped"], + )); + + let server = SequenceServer::start(vec![ + tool_call("call-2", "workspace_rename_file", &rename), + text("The note is renamed."), + tool_call("call-3", "workspace_rename_file", &rename), + text("It is gone."), + ]); + let second = fixture.chat(&journal, &server.endpoint, "rename the note"); + assert_success(&second); + assert!(!fixture.workspace.join("note.txt").exists()); + assert_eq!( + fs::read(fixture.workspace.join("moved.txt")).expect("moved"), + NOTE + ); + let third = fixture.chat(&journal, &server.endpoint, "rename it again"); + assert_success(&third); + let requests = server.finish(); + let result = user_texts(&requests[1]) + .into_iter() + .find(|text| text.starts_with("Tool result [call call-2")) + .expect("renamed result sent to the model"); + assert!(result.contains("\"status\":\"renamed\""), "{result}"); + assert!(result.contains("\"from\":\"note.txt\""), "{result}"); + assert!(result.contains("\"to\":\"moved.txt\""), "{result}"); + assert!(!result.contains("notes for"), "no file text: {result}"); + assert!( + user_texts(&requests[3]).iter().any(|text| { + text.starts_with("Tool error [call call-3") && text.contains("not_found") + }), + "a second rename is refused" + ); + let listed = run_cli(&fixture.data, &["approvals", "list"]); + assert_success(&listed); + let approvals = json_output(&listed)["approvals"].clone(); + assert_eq!(approvals.as_array().expect("approvals").len(), 1); + assert_eq!(approvals[0]["state"], "consumed"); + let checkpoints = run_cli(&fixture.data, &["file", "checkpoints"]); + assert_success(&checkpoints); + assert_eq!( + json_output(&checkpoints)["operations"] + .as_array() + .expect("operations") + .len(), + 0, + "rename is not checkpointed" + ); + // The file-operation audit names both the source and the destination. + let audit = run_cli(&fixture.data, &["file", "audit"]); + assert_success(&audit); + let audit = json_output(&audit); + assert!( + audit["entries"].as_array().expect("entries").iter().any( + |entry| entry["action"]["payload"]["operation"] == "rename_file" + && entry["action"]["payload"]["destination"].is_object() + ), + "{audit}" + ); +} + +/// The operator flow for a model deletion: approval, then one checkpointed +/// removal, and the trusted `file restore` recreates the exact bytes. +#[test] +fn an_approved_model_delete_removes_the_file_and_the_operator_restores_it() { + let fixture = Fixture::new(NOTE); + let journal = fixture.session(); + fixture.set("--model-tools", true); + let delete = json!({"path": "note.txt"}); + + let server = SequenceServer::start(vec![ + tool_call("call-1", "workspace_delete_file", &delete), + text("Waiting for operator approval."), + ]); + let first = fixture.chat(&journal, &server.endpoint, "delete the note"); + server.finish(); + assert_success(&first); + assert!(fixture.workspace.join("note.txt").exists()); + + let listed = run_cli(&fixture.data, &["approvals", "list"]); + assert_success(&listed); + let approvals = json_output(&listed)["approvals"] + .as_array() + .expect("approvals") + .to_vec(); + assert_eq!(approvals.len(), 1, "one pending request: {approvals:?}"); + let summary = approvals[0]["summary"].as_str().expect("summary"); + assert!( + summary.starts_with(&format!("delete note.txt: {} bytes, sha256 ", NOTE.len())), + "{summary}" + ); + let approval = approvals[0]["id"].as_str().expect("approval id").to_owned(); + assert_error_code( + &run_cli(&fixture.data, &["approvals", "approve", &approval]), + "approval", + ); + assert_success(&run_cli( + &fixture.data, + &["approvals", "approve", &approval, "--writers-stopped"], + )); + + let server = SequenceServer::start(vec![ + tool_call("call-2", "workspace_delete_file", &delete), + text("The note is deleted."), + tool_call("call-3", "workspace_delete_file", &delete), + text("It is gone."), + ]); + let second = fixture.chat(&journal, &server.endpoint, "delete the note"); + assert_success(&second); + assert!(!fixture.workspace.join("note.txt").exists()); + let third = fixture.chat(&journal, &server.endpoint, "delete it again"); + assert_success(&third); + let requests = server.finish(); + let result = user_texts(&requests[1]) + .into_iter() + .find(|text| text.starts_with("Tool result [call call-2")) + .expect("deleted result sent to the model"); + assert!(result.contains("\"status\":\"deleted\""), "{result}"); + assert!(!result.contains("notes for"), "no file text: {result}"); + assert!( + user_texts(&requests[3]).iter().any(|text| { + text.starts_with("Tool error [call call-3") && text.contains("not_found") + }), + "a second deletion is refused" + ); + + let checkpoints = run_cli(&fixture.data, &["file", "checkpoints"]); + assert_success(&checkpoints); + let checkpoints = json_output(&checkpoints); + let operations = checkpoints["operations"].as_array().expect("operations"); + assert_eq!(operations.len(), 1, "one checkpoint: {checkpoints}"); + assert_eq!(operations[0]["kind"], "remove"); + assert_eq!(operations[0]["outcome"], "succeeded"); + let checkpoint = operations[0]["operation_id"] + .as_str() + .expect("operation ID") + .to_owned(); + assert!( + result.contains(&checkpoint), + "the result names the checkpoint: {result}" + ); + + // The trusted restore with `--removed` recreates the file and needs no + // current-file precondition; a replacement restore refuses a removal + // checkpoint. + let workspace_id = fixture.workspace_id.to_string(); + let session_id = fixture.session_id.to_string(); + let restore = |extra: &[&str]| { + let mut arguments = vec![ + "file", + "restore", + workspace_id.as_str(), + "note.txt", + "--session", + session_id.as_str(), + "--checkpoint", + checkpoint.as_str(), + "--writers-stopped", + ]; + arguments.extend_from_slice(extra); + run_cli(&fixture.data, &arguments) + }; + let wrong_kind = restore(&[ + "--expected-sha256", + &"0".repeat(64), + "--expected-bytes", + "1", + ]); + assert_error_code(&wrong_kind, "checkpoint_unavailable"); + assert!(!fixture.workspace.join("note.txt").exists()); + let restored = restore(&["--removed"]); + assert_success(&restored); + let restored = json_output(&restored); + assert_eq!(restored["restored_from"], checkpoint); + assert_eq!(restored["bytes_written"], NOTE.len()); + assert_eq!( + fs::read(fixture.workspace.join("note.txt")).expect("restored"), + NOTE + ); + // The name is taken now: a second restore never replaces it. + assert_error_code(&restore(&["--removed"]), "checkpoint_conflict"); + assert_eq!( + fs::read(fixture.workspace.join("note.txt")).expect("kept"), + NOTE + ); + let after = run_cli(&fixture.data, &["file", "checkpoints"]); + assert_success(&after); + assert_eq!( + json_output(&after)["operations"] + .as_array() + .expect("operations") + .len(), + 1, + "a restore of a removal records no new checkpoint" + ); +} diff --git a/crates/omgcode-core/src/file_audit.rs b/crates/omgcode-core/src/file_audit.rs index a121f72..9e7e88d 100644 --- a/crates/omgcode-core/src/file_audit.rs +++ b/crates/omgcode-core/src/file_audit.rs @@ -44,6 +44,9 @@ pub(crate) enum FileOperation { CreateFile, /// Create one directory. CreateDirectory, + /// Rename one regular file without replacing an entry. The record names + /// both the source (`target`) and the `destination`. Older readers refuse it. + RenameFile, } /// The observed result of one file operation. @@ -92,6 +95,10 @@ pub(crate) enum FileAuditAction { revision: u64, operation: FileOperation, target: NativePath, + /// The new name of a rename; absent for every other operation, so + /// earlier records stay byte-identical. + #[serde(default, skip_serializing_if = "Option::is_none")] + destination: Option, }, /// Record the result for the one active intent. OperationFinished { @@ -351,6 +358,40 @@ impl FileAudit { revision: u64, operation: FileOperation, target: NativePath, + ) -> Result { + self.start_with(session_id, workspace_id, revision, operation, target, None) + } + + /// Durably record the intent to rename `target` to `destination`. + /// + /// Both names are validated as native workspace-relative paths, neither may + /// be the root, and they must differ. Everything else matches [`Self::start`]. + pub(crate) fn start_rename( + &mut self, + session_id: Uuid, + workspace_id: WorkspaceId, + revision: u64, + target: NativePath, + destination: NativePath, + ) -> Result { + self.start_with( + session_id, + workspace_id, + revision, + FileOperation::RenameFile, + target, + Some(destination), + ) + } + + fn start_with( + &mut self, + session_id: Uuid, + workspace_id: WorkspaceId, + revision: u64, + operation: FileOperation, + target: NativePath, + destination: Option, ) -> Result { self.ensure_healthy()?; if let Some(pending) = self.state.pending { @@ -362,6 +403,7 @@ impl FileAudit { )); } validate_target(operation, &target)?; + validate_destination(operation, &target, destination.as_ref())?; let operation_id = Uuid::new_v4(); let action = FileAuditAction::OperationStarted { operation_id, @@ -370,6 +412,7 @@ impl FileAudit { revision, operation, target, + destination, }; self.append_with_reserve(action, 2, COMPLETION_AND_ACK_RESERVE_BYTES)?; Ok(operation_id) @@ -645,6 +688,7 @@ impl FileAudit { revision, operation, target, + destination, } => { if expected_sequence == 0 || state.pending.is_some() @@ -659,6 +703,7 @@ impl FileAudit { )); } validate_target(*operation, target)?; + validate_destination(*operation, target, destination.as_ref())?; state.operation_ids.insert(*operation_id); state.last_revision = Some(*revision); state.pending = Some(PendingOperation { @@ -763,6 +808,32 @@ fn validate_target(operation: FileOperation, target: &NativePath) -> Result<(), Ok(()) } +/// A rename records a second, distinct, non-root name; no other operation does. +fn validate_destination( + operation: FileOperation, + target: &NativePath, + destination: Option<&NativePath>, +) -> Result<(), FileAuditError> { + match (operation, destination) { + (FileOperation::RenameFile, Some(destination)) => { + validate_target(FileOperation::RenameFile, destination)?; + if destination == target { + return Err(FileAuditError::Invalid( + "rename source and destination are the same", + )); + } + Ok(()) + } + (FileOperation::RenameFile, None) => { + Err(FileAuditError::Invalid("a rename needs a destination")) + } + (_, Some(_)) => Err(FileAuditError::Invalid( + "only a rename records a destination", + )), + (_, None) => Ok(()), + } +} + /// Encode exactly one JSON event and its terminating newline. fn encode_entry(entry: &AuditEntry) -> Result, FileAuditError> { let mut encoded = serde_json::to_vec(entry) @@ -1123,6 +1194,7 @@ mod tests { revision: 1, operation: FileOperation::Read, target: valid_target(), + destination: None, }); add(FileAuditAction::OperationFinished { operation_id: first, @@ -1140,6 +1212,7 @@ mod tests { revision: 1, operation: FileOperation::Read, target: valid_target(), + destination: None, }); add(FileAuditAction::OperationFinished { operation_id: id, @@ -1654,4 +1727,126 @@ mod tests { )); assert_eq!(audit.bytes, bytes); } + + #[test] + fn a_rename_records_both_names_and_other_operations_record_none() { + let directory = tempfile::tempdir().expect("temporary data directory"); + let store_id = Uuid::new_v4(); + let (session_id, workspace_id) = scope(); + let mut audit = FileAudit::open(directory.path(), store_id).expect("open audit"); + let renamed = audit + .start_rename( + session_id, + workspace_id, + 1, + native_target(Path::new("a.txt")), + native_target(Path::new("sub/b.txt")), + ) + .expect("rename intent"); + audit + .finish(renamed, FileOutcome::Succeeded) + .expect("rename outcome"); + let read = audit + .start( + session_id, + workspace_id, + 1, + FileOperation::Read, + valid_target(), + ) + .expect("read intent"); + audit + .finish(read, FileOutcome::Succeeded) + .expect("read outcome"); + let entries: Vec = audit + .entries(10) + .expect("entries") + .iter() + .map(|entry| serde_json::to_value(entry).expect("entry JSON")) + .collect(); + let started: Vec<&serde_json::Value> = entries + .iter() + .filter(|entry| entry["action"]["type"] == "operation_started") + .collect(); + assert_eq!(started[0]["action"]["payload"]["operation"], "rename_file"); + assert!(started[0]["action"]["payload"]["destination"].is_object()); + assert!( + started[1]["action"]["payload"].get("destination").is_none(), + "an earlier-shaped record has no destination field" + ); + drop(audit); + FileAudit::open(directory.path(), store_id).expect("the log replays"); + } + + #[test] + fn a_rename_needs_two_distinct_non_root_names_and_only_a_rename_has_two() { + let directory = tempfile::tempdir().expect("temporary data directory"); + let (session_id, workspace_id) = scope(); + let mut audit = FileAudit::open(directory.path(), Uuid::new_v4()).expect("open audit"); + let refused = [ + audit.start_rename(session_id, workspace_id, 1, valid_target(), valid_target()), + audit.start_rename( + session_id, + workspace_id, + 1, + valid_target(), + native_target(Path::new("")), + ), + audit.start_rename( + session_id, + workspace_id, + 1, + valid_target(), + native_target(Path::new("../outside")), + ), + audit.start( + session_id, + workspace_id, + 1, + FileOperation::RenameFile, + valid_target(), + ), + audit.start_with( + session_id, + workspace_id, + 1, + FileOperation::Read, + valid_target(), + Some(native_target(Path::new("other.txt"))), + ), + ]; + for result in refused { + assert!( + matches!(result, Err(FileAuditError::Invalid(_))), + "{result:?}" + ); + } + assert_eq!(audit.pending(), None, "nothing was started"); + } + + #[test] + fn a_tampered_rename_record_is_refused_on_reopen() { + let directory = tempfile::tempdir().expect("temporary data directory"); + let store_id = Uuid::new_v4(); + let (session_id, workspace_id) = scope(); + let mut audit = FileAudit::open(directory.path(), store_id).expect("open audit"); + let id = audit + .start_rename( + session_id, + workspace_id, + 1, + valid_target(), + native_target(Path::new("moved.txt")), + ) + .expect("rename intent"); + audit.finish(id, FileOutcome::Succeeded).expect("outcome"); + drop(audit); + let path = directory.path().join(FILE_NAME); + let text = fs::read_to_string(&path).expect("log"); + fs::write(&path, text.replace("rename_file", "create_file")).expect("tamper"); + assert!(matches!( + FileAudit::open(directory.path(), store_id), + Err(FileAuditError::Invalid(_)) + )); + } } diff --git a/crates/omgcode-core/src/files.rs b/crates/omgcode-core/src/files.rs index e276335..8af6e3f 100644 --- a/crates/omgcode-core/src/files.rs +++ b/crates/omgcode-core/src/files.rs @@ -18,6 +18,7 @@ use uuid::Uuid; use crate::control::{ControlError, ControlStore}; use crate::file_audit::{FileAudit, FileAuditError, FileOperation, FileOutcome}; use crate::policy::{DecisionReason, Operation, PolicyRequest, SessionBinding, WorkspaceId}; +use crate::replacement::RecordedArtifact; use crate::session::{NativePath, SessionError}; /// Explicit request scope. IDs and path arguments do not establish authority. @@ -160,6 +161,21 @@ pub struct FileCreated { pub bytes_written: usize, } +/// A successfully audited no-replace rename. It does not promise power-loss durability. +#[derive(Debug, Serialize)] +pub struct FileRenamed { + /// Durable operation identifier. + pub operation_id: Uuid, + /// Workspace from the verified request scope. + pub workspace_id: WorkspaceId, + /// Relative source name, which no longer exists. + pub from: NativePath, + /// Relative new name of the same file. + pub to: NativePath, + /// Exact byte count of the renamed file. + pub bytes: u64, +} + /// Metadata for trusted inspection. An unresolved intent never triggers action replay. #[derive(Debug, Serialize)] pub struct FileAuditView { @@ -305,6 +321,84 @@ impl<'a> FileBroker<'a> { }) } + /// Rename one regular file to a name that does not exist (ADR-0055). + /// + /// `expected` is the exact size and SHA-256 of the live source from a + /// trusted read. Both names must be distinct workspace-relative files + /// names in this binding's workspace. The live binding must allow read and + /// write, and the checkpoint store must have no unresolved operation. The + /// operation is audited with both names. The native step pins the source, + /// reads its bytes through the pinned handle, and renames it with the + /// kernel's no-replace primitive, so an existing entry is never replaced. + /// Nothing is checkpointed: no content is destroyed, and renaming back + /// reverses it. The caller must stop other writers; this is not + /// compare-and-swap. A failure after the native call may have happened is + /// uncertain, leaves the audit record pending for trusted acknowledgement, + /// and is never retried. + pub fn rename_file( + &mut self, + binding: &SessionBinding, + from: &FileTarget, + to: &FileTarget, + expected: &RecordedArtifact, + ) -> Result { + #[cfg(any(target_os = "linux", windows))] + { + if from.session_id != to.session_id || from.workspace_id != to.workspace_id { + return Err(FileError::Invalid("rename names must share one scope")); + } + if expected.bytes > u64::try_from(broker::MAX_FILE_BYTES).unwrap_or(0) + || expected.sha256.len() != 64 + || !expected + .sha256 + .bytes() + .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)) + { + return Err(FileError::Invalid( + "rename requires a bounded exact SHA-256/size precondition", + )); + } + let from_path = validate_target(from, false)?; + let to_path = validate_target(to, false)?; + if from_path == to_path { + return Err(FileError::Invalid("rename names must differ")); + } + // Both preferences from this live binding, as for every file write. + checkpoint::check_permissions(self.control, binding, from)?; + let from_native = NativePath::from_path(&from_path.to_path_buf())?; + let to_native = NativePath::from_path(&to_path.to_path_buf())?; + let (workspace, mut audit, operation_id) = self.begin_with_destination( + binding, + from, + &from_path, + FileOperation::RenameFile, + Operation::Write, + Some(to_native.clone()), + )?; + let sha256 = expected.sha256.as_str(); + let result = + workspace.rename_no_replace(&from_path, &to_path, expected.bytes, &|bytes| { + crate::session::sha256_hex(bytes) == sha256 + }); + complete_rename(&mut audit, operation_id, result)?; + Ok(FileRenamed { + operation_id, + workspace_id: from.workspace_id, + from: from_native, + to: to_native, + bytes: expected.bytes, + }) + } + #[cfg(not(any(target_os = "linux", windows)))] + { + let _ = (binding, from, to, expected); + Err(FileError::Native(io::Error::new( + io::ErrorKind::Unsupported, + "native rename is not qualified on this platform", + ))) + } + } + /// Create one new directory with an existing, independently opened parent. pub fn create_directory( &mut self, @@ -405,6 +499,19 @@ impl<'a> FileBroker<'a> { path: &RelativePath, operation: FileOperation, permission: Operation, + ) -> Result<(Workspace, FileAudit, Uuid), FileError> { + self.begin_with_destination(binding, target, path, operation, permission, None) + } + + /// Like [`Self::begin`], recording the destination name of a rename. + fn begin_with_destination( + &mut self, + binding: &SessionBinding, + target: &FileTarget, + path: &RelativePath, + operation: FileOperation, + permission: Operation, + destination: Option, ) -> Result<(Workspace, FileAudit, Uuid), FileError> { let decision = self.control.check_file_policy( binding, @@ -444,13 +551,23 @@ impl<'a> FileBroker<'a> { } let (data_root, store_id) = self.control.file_audit_context()?; let mut audit = FileAudit::open(&data_root, store_id)?; - let operation_id = audit.start( - target.session_id, - target.workspace_id, - self.control.revision(), - operation, - NativePath::from_path(&path.to_path_buf())?, - )?; + let source = NativePath::from_path(&path.to_path_buf())?; + let operation_id = match destination { + Some(destination) => audit.start_rename( + target.session_id, + target.workspace_id, + self.control.revision(), + source, + destination, + )?, + None => audit.start( + target.session_id, + target.workspace_id, + self.control.revision(), + operation, + source, + )?, + }; // Native setup and audit sync can take time. Recheck the live lease // immediately before admitting the target operation; no persisted // policy result is reused as an executor permit. @@ -494,6 +611,48 @@ fn validate_target(target: &FileTarget, allow_root: bool) -> Result, +) -> Result<(), FileError> { + match result { + Ok(()) => audit + .finish(operation_id, FileOutcome::Succeeded) + .map_err(|source| FileError::Uncertain { + operation_id, + source: Box::new(source), + }), + Err(broker::RenameError::Uncertain(error)) => { + if let Err(source) = audit.finish(operation_id, FileOutcome::Uncertain) { + return Err(FileError::Uncertain { + operation_id, + source: Box::new(source), + }); + } + Err(FileError::Uncertain { + operation_id, + source: Box::new(error), + }) + } + Err(refusal) => { + audit.finish(operation_id, FileOutcome::Failed)?; + Err(match refusal { + broker::RenameError::TargetExists => FileError::Conflict, + broker::RenameError::SourceChanged => { + crate::replacement::CheckpointError::Conflict.into() + } + broker::RenameError::Refused(error) | broker::RenameError::Uncertain(error) => { + FileError::Native(error) + } + }) + } + } +} + /// Keep uncertain mutations pending and never report success before durable audit. fn complete_creation( audit: &mut FileAudit, @@ -666,4 +825,172 @@ mod tests { b"partial" ); } + + fn artifact_of(bytes: &[u8]) -> RecordedArtifact { + RecordedArtifact { + bytes: u64::try_from(bytes.len()).expect("small fixture"), + sha256: crate::session::sha256_hex(bytes), + } + } + + fn named(target: &FileTarget, path: &str) -> FileTarget { + FileTarget { + session_id: target.session_id, + workspace_id: target.workspace_id, + path: PathBuf::from(path), + } + } + + /// A rename moves exactly the expected file, never replaces, and audits + /// both names; a refusal leaves both files and no pending record. + #[cfg(any(target_os = "linux", windows))] + #[test] + fn rename_moves_the_expected_file_without_replacing_and_audits_both_names() { + let (_temporary, mut control, binding, target, root) = fixture(); + fs::write(root.join("entry.txt"), b"moved bytes").expect("source"); + fs::write(root.join("present.txt"), b"keep").expect("existing destination"); + fs::create_dir(root.join("sub")).expect("destination parent"); + let expected = artifact_of(b"moved bytes"); + let mut broker = FileBroker::new(&mut control); + + // An existing destination is a conflict, and both files survive. + assert!(matches!( + broker.rename_file(&binding, &target, &named(&target, "present.txt"), &expected), + Err(FileError::Conflict) + )); + assert_eq!(fs::read(root.join("entry.txt")).unwrap(), b"moved bytes"); + assert_eq!(fs::read(root.join("present.txt")).unwrap(), b"keep"); + assert_eq!(broker.audit(100).expect("audit").pending, None); + + // A different size or digest is a conflict before anything moves. + for wrong in [artifact_of(b"other"), artifact_of(b"moved byteX")] { + assert!(matches!( + broker.rename_file(&binding, &target, &named(&target, "sub/new.txt"), &wrong), + Err(FileError::Checkpoint( + crate::replacement::CheckpointError::Conflict + )) + )); + } + assert!(root.join("entry.txt").exists() && !root.join("sub/new.txt").exists()); + + // The same name, or malformed expectations, are refused outright. + assert!(matches!( + broker.rename_file(&binding, &target, &target, &expected), + Err(FileError::Invalid(_)) + )); + let malformed = RecordedArtifact { + bytes: expected.bytes, + sha256: "ABC".to_owned(), + }; + assert!(matches!( + broker.rename_file( + &binding, + &target, + &named(&target, "sub/new.txt"), + &malformed + ), + Err(FileError::Invalid(_)) + )); + + let renamed = broker + .rename_file(&binding, &target, &named(&target, "sub/new.txt"), &expected) + .expect("rename"); + assert_eq!(renamed.bytes, 11); + assert_eq!(renamed.workspace_id, target.workspace_id); + assert!(!root.join("entry.txt").exists()); + assert_eq!(fs::read(root.join("sub/new.txt")).unwrap(), b"moved bytes"); + let view = broker.audit(100).expect("audit"); + assert_eq!(view.pending, None); + let finished = view.entries.iter().any(|entry| { + entry["action"]["type"] == "operation_started" + && entry["action"]["payload"]["operation_id"] == renamed.operation_id.to_string() + && entry["action"]["payload"]["operation"] == "rename_file" + && entry["action"]["payload"]["destination"].is_object() + }); + assert!(finished, "{view:?}"); + } + + #[cfg(any(target_os = "linux", windows))] + #[test] + fn rename_refuses_directories_and_denied_writes_and_missing_sources() { + let (_temporary, mut control, binding, target, root) = fixture(); + fs::create_dir(root.join("dir")).expect("directory"); + let expected = artifact_of(b""); + let mut broker = FileBroker::new(&mut control); + assert!( + broker + .rename_file( + &binding, + &named(&target, "dir"), + &named(&target, "moved"), + &expected, + ) + .is_err() + ); + assert!(root.join("dir").is_dir() && !root.join("moved").exists()); + assert!( + broker + .rename_file( + &binding, + &named(&target, "missing.txt"), + &named(&target, "moved"), + &expected, + ) + .is_err() + ); + assert_eq!(broker.audit(100).expect("audit").pending, None); + } + + #[cfg(any(target_os = "linux", windows))] + #[test] + fn rename_needs_both_preferences_from_the_live_binding() { + let (_temporary, mut control, _stale, target, root) = fixture(); + fs::write(root.join("entry.txt"), b"bytes").expect("source"); + let workspace = target.workspace_id; + let revision = control.revision(); + control + .set_permissions( + workspace, + crate::policy::PermissionSet { + read: true, + write: false, + ..crate::policy::PermissionSet::default() + }, + revision, + ) + .expect("deny write"); + let binding = control + .bind_session(target.session_id, workspace) + .expect("a current binding under the new preferences"); + let mut broker = FileBroker::new(&mut control); + let result = broker.rename_file( + &binding, + &target, + &named(&target, "moved.txt"), + &artifact_of(b"bytes"), + ); + assert!(result.is_err(), "{result:?}"); + assert!(root.join("entry.txt").exists() && !root.join("moved.txt").exists()); + } + + /// A completed rename stays an uncertain caller outcome if audit sync fails. + #[cfg(any(target_os = "linux", windows))] + #[test] + fn a_rename_with_a_failed_audit_sync_never_returns_success() { + let (_temporary, mut control, binding, target, root) = fixture(); + fs::write(root.join("entry.txt"), b"bytes").expect("source"); + let mut broker = FileBroker::new(&mut control); + broker.fail_outcome_sync = true; + assert!(matches!( + broker.rename_file( + &binding, + &target, + &named(&target, "moved.txt"), + &artifact_of(b"bytes"), + ), + Err(FileError::Uncertain { .. }) + )); + assert!(!root.join("entry.txt").exists()); + assert_eq!(fs::read(root.join("moved.txt")).unwrap(), b"bytes"); + } } diff --git a/crates/omgcode-core/src/files/checkpoint.rs b/crates/omgcode-core/src/files/checkpoint.rs index 553141e..e6c98a2 100644 --- a/crates/omgcode-core/src/files/checkpoint.rs +++ b/crates/omgcode-core/src/files/checkpoint.rs @@ -166,6 +166,19 @@ pub(super) fn validate_request( pub(super) fn validate_capture( capture: &FileCapture, expected: &RecordedArtifact, +) -> Result<(), FileError> { + validate_capture_bytes(capture, expected)?; + if std::str::from_utf8(&capture.bytes).is_err() { + return Err(FileError::Invalid("checkpoint source is not UTF-8")); + } + Ok(()) +} + +/// Compare the exact size and SHA-256 of the retained source without +/// interpreting its bytes. A removal checkpoint keeps them as they are. +pub(super) fn validate_capture_bytes( + capture: &FileCapture, + expected: &RecordedArtifact, ) -> Result<(), FileError> { if u64::try_from(capture.bytes.len()) .map_err(|_| FileError::Invalid("capture size overflow"))? @@ -174,8 +187,5 @@ pub(super) fn validate_capture( { return Err(CheckpointError::Conflict.into()); } - if std::str::from_utf8(&capture.bytes).is_err() { - return Err(FileError::Invalid("checkpoint source is not UTF-8")); - } Ok(()) } diff --git a/crates/omgcode-core/src/files/replacement.rs b/crates/omgcode-core/src/files/replacement.rs index 28f6e4a..b2a2f64 100644 --- a/crates/omgcode-core/src/files/replacement.rs +++ b/crates/omgcode-core/src/files/replacement.rs @@ -6,8 +6,10 @@ use std::io; use omgcode_platform::broker; #[cfg(any(target_os = "linux", windows))] -use super::checkpoint::{check_permissions, validate_capture, validate_request}; -use super::{FileBroker, FileError, FileTarget}; +use super::checkpoint::{ + check_permissions, validate_capture, validate_capture_bytes, validate_request, +}; +use super::{FileBroker, FileCreated, FileError, FileTarget}; #[cfg(any(target_os = "linux", windows))] use super::{FileOperation, FileOutcome}; #[cfg(any(target_os = "linux", windows))] @@ -15,7 +17,7 @@ use crate::policy::Operation; use crate::policy::SessionBinding; #[cfg(any(target_os = "linux", windows))] use crate::replacement::store::{self, Store}; -use crate::replacement::{CheckpointError, RecordedArtifact, ReplacementReceipt}; +use crate::replacement::{CheckpointError, RecordedArtifact, ReplacementKind, ReplacementReceipt}; use uuid::Uuid; #[cfg(all(test, any(target_os = "linux", windows)))] @@ -77,7 +79,8 @@ impl FileBroker<'_> { check_permissions(self.control, binding, target)?; // The store opens read-only and closes before apply reopens it, so // the lock order stays control, then one subordinate journal. - let original = store::read_original(self.control, checkpoint, target)?; + let original = + store::read_original(self.control, checkpoint, target, ReplacementKind::Replace)?; self.apply_with_preparation_hook(binding, target, expected, &original, |_| {}) } #[cfg(not(any(target_os = "linux", windows)))] @@ -91,6 +94,152 @@ impl FileBroker<'_> { } } + /// Remove one existing regular file after a durable checkpoint of its bytes. + /// + /// `expected` is the exact size and SHA-256 of the live file, from a trusted + /// read. The sequence mirrors [`Self::apply_replacement`] without a stage: + /// the same live-binding, permission, admission, qualified-filesystem and + /// capture checks run, the original is checkpointed under a `Remove` + /// record, the target is rechecked, commit intent is recorded, and the + /// native removal is attempted once. Linux removes by name and detects a + /// swapped name afterwards, so a swap between the recheck and the call is + /// an uncertain result, not a prevented one; Windows deletes through the + /// verified handle. A trusted caller must stop independent writers. Any + /// failure after commit intent leaves the operation unresolved for + /// inspection and acknowledgement; nothing is retried, restored, or + /// cleaned up. A non-UTF-8 original is allowed: nothing interprets it. + pub fn apply_removal( + &mut self, + binding: &SessionBinding, + target: &FileTarget, + expected: &RecordedArtifact, + ) -> Result { + #[cfg(any(target_os = "linux", windows))] + { + self.removal_with_preparation_hook(binding, target, expected, |_| {}) + } + #[cfg(not(any(target_os = "linux", windows)))] + { + let _ = (binding, target, expected); + Err(CheckpointError::Io(io::Error::new( + io::ErrorKind::Unsupported, + "native removal is not qualified on this platform", + )) + .into()) + } + } + + /// Recreate a file that a removal checkpoint retained, if its name is free. + /// + /// The checkpoint must be a prepared, retained `Remove` record for this + /// workspace and exact native target. Its bytes are reread and checked + /// against the record, then written through the audited exclusive create, + /// so an existing entry of any kind is a conflict and is never replaced. + /// No new checkpoint operation is recorded because nothing is destroyed. + /// Mode, owner, ACL, and timestamps are not restored: the new file gets + /// the broker's default creation metadata. The caller must stop other + /// writers. + pub fn restore_removed_file( + &mut self, + binding: &SessionBinding, + target: &FileTarget, + checkpoint: Uuid, + ) -> Result { + #[cfg(any(target_os = "linux", windows))] + { + super::validate_target(target, false)?; + check_permissions(self.control, binding, target)?; + let original = + store::read_original(self.control, checkpoint, target, ReplacementKind::Remove)?; + match self.create_file(binding, target, &original) { + Err(FileError::Conflict) => Err(CheckpointError::Conflict.into()), + other => other, + } + } + #[cfg(not(any(target_os = "linux", windows)))] + { + let _ = (binding, target, checkpoint); + Err(CheckpointError::Io(io::Error::new( + io::ErrorKind::Unsupported, + "native restore is not qualified on this platform", + )) + .into()) + } + } + + /// The test hook runs after checkpoint sync and before the recheck. The + /// control writer stays held for the whole removal. + #[cfg(any(target_os = "linux", windows))] + fn removal_with_preparation_hook( + &mut self, + binding: &SessionBinding, + target: &FileTarget, + expected: &RecordedArtifact, + after_preparation: impl FnOnce(&mut Store), + ) -> Result { + let path = validate_request(target, expected, &[])?; + check_permissions(self.control, binding, target)?; + let (workspace, mut audit, read_id) = + self.begin(binding, target, &path, FileOperation::Read, Operation::Read)?; + let native = match workspace.replacement_target(&path, broker::MAX_FILE_BYTES) { + Ok(target) => target, + Err(source) => { + audit.finish(read_id, FileOutcome::Failed)?; + return Err(FileError::Native(source)); + } + }; + audit.finish(read_id, FileOutcome::Succeeded)?; + // Control, then at most one subordinate journal. + drop(audit); + + let capture = native.capture(); + validate_capture_bytes(capture, expected)?; + let mut store = Store::open_or_initialize(self.control, capture.volume, |root| { + native.check_storage(root)?; + check_permissions(self.control, binding, target) + })?; + check_permissions(self.control, binding, target)?; + let prepared = store.prepare_kind( + ReplacementKind::Remove, + target, + self.control.revision(), + capture, + &[], + )?; + let operation_id = prepared.operation_id; + after_preparation(&mut store); + + let same_target = + store.with_publication_evidence(operation_id, |_| Ok(native.recheck_removal()?))?; + if !same_target { + store.with_publication_evidence(operation_id, |store| { + store.finish_conflict(operation_id) + })?; + return Err(CheckpointError::Conflict.into()); + } + + store.with_publication_evidence(operation_id, |store| { + check_permissions(self.control, binding, target)?; + store.record_commit_intent(operation_id)?; + // A denial after commit intent stays unresolved; nothing is removed. + check_permissions(self.control, binding, target)?; + store.publication_boundary("before_native_commit")?; + native.remove(|| { + check_permissions(self.control, binding, target).map_err(io::Error::other) + })?; + store.publication_boundary("after_native_commit")?; + store.finish_publication_success(operation_id) + })?; + + Ok(ReplacementReceipt { + operation_id: Some(operation_id), + original: prepared.original, + proposed: prepared.proposed, + changed: true, + applied: true, + }) + } + /// The test hook runs after checkpoint sync and before staging. Production /// uses a no-op. The control writer remains held for the entire operation. #[cfg(any(target_os = "linux", windows))] diff --git a/crates/omgcode-core/src/files/replacement/tests.rs b/crates/omgcode-core/src/files/replacement/tests.rs index ccbe768..06f14ae 100644 --- a/crates/omgcode-core/src/files/replacement/tests.rs +++ b/crates/omgcode-core/src/files/replacement/tests.rs @@ -8,8 +8,8 @@ use crate::control::ControlStore; use crate::policy::{PermissionSet, SessionBinding, WorkspaceId}; use crate::replacement::{ CheckpointCompaction, CheckpointDeletion, CheckpointError, CheckpointInspection, - CheckpointRetention, RecordedArtifact, RecordedReplacement, ReplacementOutcome, - ReplacementProgress, + CheckpointRetention, RecordedArtifact, RecordedReplacement, ReplacementKind, + ReplacementOutcome, ReplacementProgress, }; use sha2::{Digest, Sha256}; use tempfile::TempDir; @@ -1343,3 +1343,347 @@ fn a_full_ledger_is_compacted_before_apply_and_before_deletion() { ); assert!(!fixture.operation_dir(retained).exists()); } + +// --------------------------------------------------------------------------- +// Checkpointed removal and restore of a removed file (ADR-0055) +// --------------------------------------------------------------------------- + +fn remove_hooked( + fixture: &mut Fixture, + expected: &[u8], + hook: impl FnOnce(&mut Store), +) -> Result { + let binding = fixture.binding(); + let target = fixture.target(); + FileBroker::new(&mut fixture.control).removal_with_preparation_hook( + &binding, + &target, + &artifact(expected), + hook, + ) +} + +fn remove(fixture: &mut Fixture, expected: &[u8]) -> Result { + remove_hooked(fixture, expected, |_| {}) +} + +fn restore_removed( + fixture: &mut Fixture, + checkpoint: Uuid, +) -> Result { + let binding = fixture.binding(); + let target = fixture.target(); + FileBroker::new(&mut fixture.control).restore_removed_file(&binding, &target, checkpoint) +} + +fn removed_id(fixture: &mut Fixture, expected: &[u8]) -> Uuid { + remove(fixture, expected) + .expect("removal") + .operation_id + .expect("a removal creates an operation") +} + +#[test] +fn removal_checkpoints_the_original_and_removes_only_the_file() { + let mut fixture = Fixture::new(); + let bystander = fixture.workspace.join("bystander.txt"); + fs::write(&bystander, b"keep").expect("bystander"); + let target = fixture.workspace.join("source.txt"); + + let receipt = remove(&mut fixture, ORIGINAL).expect("removal"); + assert!(receipt.changed && receipt.applied); + assert_eq!(receipt.original, artifact(ORIGINAL)); + assert_eq!(receipt.proposed, artifact(b"")); + let id = receipt.operation_id.expect("operation"); + assert!(!target.exists()); + assert_eq!(fs::read(&bystander).unwrap(), b"keep"); + + // The private entry keeps the original and the empty proposal, and has no stage. + let directory = fixture.operation_dir(id); + assert_eq!(fs::read(directory.join("original")).unwrap(), ORIGINAL); + assert_eq!(fs::read(directory.join("proposed")).unwrap(), b""); + assert!(directory.join("manifest.json").exists()); + assert!(!directory.join("stage").exists()); + let inspection = fixture.inspect().expect("inspect removal"); + assert_eq!(inspection.pending, None); + let record = operation(&inspection, id); + assert_eq!(record.kind, ReplacementKind::Remove); + assert_eq!(record.progress, ReplacementProgress::CommitIntended); + assert_eq!(record.outcome, Some(ReplacementOutcome::Succeeded)); + assert!(record.stage_identity.is_none()); + assert_eq!(record.proposed, artifact(b"")); + assert_eq!(record.retention, CheckpointRetention::Retained); +} + +#[test] +fn removal_refuses_a_wrong_precondition_a_missing_target_and_denied_preferences() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("source.txt"); + // A different size or digest is a conflict before any private evidence. + assert!(matches!( + checkpoint_error(remove(&mut fixture, b"other bytes")), + CheckpointError::Conflict + )); + assert!(target.exists()); + assert!(fixture.inspect().expect("inspect").operations.is_empty()); + + // Current preferences still apply. + fixture + .control + .set_permissions( + fixture.workspace_id, + PermissionSet { + read: true, + write: false, + ..PermissionSet::default() + }, + fixture.control.revision(), + ) + .expect("deny write"); + assert!(matches!( + checkpoint_error(remove(&mut fixture, ORIGINAL)), + CheckpointError::Denied(_) + )); + assert!(target.exists()); + fixture + .control + .set_permissions( + fixture.workspace_id, + PermissionSet { + read: true, + write: true, + ..PermissionSet::default() + }, + fixture.control.revision(), + ) + .expect("restore preferences"); + + // There is nothing to remove from a missing target. + fs::remove_file(&target).expect("remove target"); + assert!(remove(&mut fixture, ORIGINAL).is_err()); + assert!(fixture.inspect().expect("inspect").operations.is_empty()); +} + +#[test] +fn a_target_replaced_after_the_checkpoint_conflicts_and_is_not_removed() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("source.txt"); + let workspace = fixture.workspace.clone(); + let result = remove_hooked(&mut fixture, ORIGINAL, |_store| { + let replacement = workspace.join("independent.txt"); + fs::write(&replacement, ORIGINAL).expect("independent inode, equal bytes"); + set_source_security(&replacement); + fs::rename(replacement, &target).expect("replace target inode"); + }); + assert!(matches!( + result, + Err(FileError::Checkpoint(CheckpointError::Conflict)) + )); + assert_eq!( + fs::read(fixture.workspace.join("source.txt")).unwrap(), + ORIGINAL + ); + let inspection = fixture.inspect().expect("inspect conflict"); + assert_eq!(inspection.pending, None); + let record = &inspection.operations[0]; + assert_eq!(record.kind, ReplacementKind::Remove); + assert_eq!(record.progress, ReplacementProgress::CheckpointPrepared); + assert_eq!(record.outcome, Some(ReplacementOutcome::Conflict)); + assert!( + !fixture + .operation_dir(record.operation_id) + .join("stage") + .exists() + ); +} + +#[test] +fn removal_faults_leave_exact_unresolved_evidence_and_are_not_retried() { + for (fault, removed) in [ + ("commit_intent_sync", false), + ("before_native_commit", false), + ("after_native_commit", true), + ("publication_outcome_sync", true), + ] { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("source.txt"); + let result = remove_hooked(&mut fixture, ORIGINAL, |store| { + store.inject_publication_fault(fault); + }); + let id = uncertain_id(result); + assert_eq!(target.exists(), !removed, "{fault}"); + assert_eq!( + fs::read(fixture.operation_dir(id).join("original")).unwrap(), + ORIGINAL, + "{fault}" + ); + let inspection = fixture.inspect().expect("reopen exact evidence"); + let record = operation(&inspection, id); + assert_eq!(record.kind, ReplacementKind::Remove, "{fault}"); + assert_eq!( + record.progress, + ReplacementProgress::CommitIntended, + "{fault}" + ); + let outcome_recorded = fault == "publication_outcome_sync"; + assert_eq!(record.outcome.is_some(), outcome_recorded, "{fault}"); + assert_eq!( + inspection.pending, + (!outcome_recorded).then_some(id), + "{fault}" + ); + // Nothing is retried while the operation is unresolved. + if !outcome_recorded { + assert!(remove(&mut fixture, ORIGINAL).is_err(), "{fault}"); + FileBroker::new(&mut fixture.control) + .checkpoint_acknowledge(id) + .expect("acknowledge without claiming success"); + assert_eq!(fixture.inspect().expect("inspect").pending, None); + } + } +} + +#[test] +fn restoring_a_removal_recreates_the_exact_bytes_and_never_replaces() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("source.txt"); + let id = removed_id(&mut fixture, ORIGINAL); + assert!(!target.exists()); + + let created = restore_removed(&mut fixture, id).expect("restore the removed file"); + assert_eq!(created.bytes_written, ORIGINAL.len()); + assert_eq!(fs::read(&target).unwrap(), ORIGINAL); + // Nothing is destroyed by a restore, so no checkpoint operation is added. + let inspection = fixture.inspect().expect("inspect"); + assert_eq!(inspection.operations.len(), 1); + assert_eq!(inspection.pending, None); + + // The name is taken now, whatever it holds. + fs::write(&target, b"someone else").expect("another writer"); + assert!(matches!( + checkpoint_error(restore_removed(&mut fixture, id)), + CheckpointError::Conflict + )); + assert_eq!(fs::read(&target).unwrap(), b"someone else"); +} + +#[test] +fn a_removed_non_utf8_file_is_checkpointed_and_restored_exactly() { + let mut fixture = Fixture::new(); + let bytes: &[u8] = &[0xff, 0xfe, 0x00, 0x80, 0x01]; + let target = fixture.workspace.join("source.txt"); + fs::write(&target, bytes).expect("binary source"); + set_source_security(&target); + let id = removed_id(&mut fixture, bytes); + assert_eq!( + fs::read(fixture.operation_dir(id).join("original")).unwrap(), + bytes + ); + // Reopening validates the entry again without interpreting its bytes. + assert_eq!(fixture.inspect().expect("inspect").operations.len(), 1); + restore_removed(&mut fixture, id).expect("restore"); + assert_eq!(fs::read(&target).unwrap(), bytes); +} + +#[test] +fn the_two_restore_routes_refuse_the_other_kind_and_foreign_targets() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("source.txt"); + let applied = applied_id(&mut fixture, ORIGINAL, PROPOSED); + // A replacement checkpoint cannot be restored as a removal. + fs::remove_file(&target).expect("remove target"); + assert!(matches!( + checkpoint_error(restore_removed(&mut fixture, applied)), + CheckpointError::Unavailable + )); + assert!(!target.exists()); + fs::write(&target, PROPOSED).expect("recreate target"); + + let removed = removed_id(&mut fixture, PROPOSED); + // A removal checkpoint cannot be restored over a file. + fs::write(&target, b"current").expect("current file"); + assert!(matches!( + checkpoint_error(restore(&mut fixture, removed, b"current")), + CheckpointError::Unavailable + )); + assert_eq!(fs::read(&target).unwrap(), b"current"); + fs::remove_file(&target).expect("clear the name"); + + // It restores only its recorded workspace and exact target. + let binding = fixture.binding(); + let other = FileTarget { + path: PathBuf::from("other.txt"), + ..fixture.target() + }; + assert!(matches!( + checkpoint_error( + FileBroker::new(&mut fixture.control).restore_removed_file(&binding, &other, removed) + ), + CheckpointError::TargetMismatch + )); + assert!(!fixture.workspace.join("other.txt").exists()); + assert!(matches!( + checkpoint_error(restore_removed(&mut fixture, Uuid::new_v4())), + CheckpointError::Unavailable + )); +} + +#[test] +fn a_removal_checkpoint_can_be_deleted_and_then_cannot_restore() { + let mut fixture = Fixture::new(); + let id = removed_id(&mut fixture, ORIGINAL); + let deletion = delete(&mut fixture, id).expect("delete the checkpoint"); + assert!(!deletion.resumed); + assert!(!fixture.operation_dir(id).exists()); + let inspection = fixture.inspect().expect("history stays"); + let record = operation(&inspection, id); + assert_eq!(record.kind, ReplacementKind::Remove); + assert_eq!(record.retention, CheckpointRetention::Deleted); + assert!(matches!( + checkpoint_error(restore_removed(&mut fixture, id)), + CheckpointError::Unavailable + )); +} + +#[test] +fn compaction_carries_a_removal_and_it_restores_afterwards() { + let mut fixture = Fixture::new(); + // One deleted entry makes the ledger shrinkable. + let applied = applied_id(&mut fixture, ORIGINAL, PROPOSED); + delete(&mut fixture, applied).expect("delete a replacement checkpoint"); + let removed = removed_id(&mut fixture, PROPOSED); + let before = fixture.inspect().expect("inspect"); + let result = compact(&mut fixture).expect("compact"); + assert!(result.compacted); + assert_eq!((result.carried, result.dropped), (1, 1)); + let after = fixture.inspect().expect("inspect compacted ledger"); + assert_eq!(after.operations, retained_operations(&before)); + assert_eq!(operation(&after, removed).kind, ReplacementKind::Remove); + let ledger = fs::read_to_string(ledger_path(&fixture)).expect("ledger"); + assert!(ledger.contains("\"kind\":\"remove\""), "{ledger}"); + + restore_removed(&mut fixture, removed).expect("restore after compaction"); + assert_eq!( + fs::read(fixture.workspace.join("source.txt")).unwrap(), + PROPOSED + ); +} + +#[test] +fn a_replacement_record_stays_byte_identical_without_a_kind() { + let mut fixture = Fixture::new(); + applied_id(&mut fixture, ORIGINAL, PROPOSED); + let ledger = fs::read_to_string(ledger_path(&fixture)).expect("ledger"); + assert!(!ledger.contains("\"kind\""), "{ledger}"); +} + +#[test] +fn a_stage_file_in_a_removal_entry_is_refused_on_reopen() { + let mut fixture = Fixture::new(); + let id = removed_id(&mut fixture, ORIGINAL); + fs::write(fixture.operation_dir(id).join("stage"), b"unexpected").expect("planted stage"); + assert!(matches!( + fixture.inspect(), + Err(FileError::Checkpoint(CheckpointError::Invalid(_))) + )); +} diff --git a/crates/omgcode-core/src/replacement.rs b/crates/omgcode-core/src/replacement.rs index 1c608e4..ee13747 100644 --- a/crates/omgcode-core/src/replacement.rs +++ b/crates/omgcode-core/src/replacement.rs @@ -100,11 +100,35 @@ pub enum ReplacementProgress { /// An exact manifest reference has been recorded. CheckpointPrepared, /// A stage identity and matching proposed-content descriptor were recorded. + /// A removal never reaches this step. StagePrepared, - /// Commit intent exists; replacement may have occurred. + /// Commit intent exists; replacement or removal may have occurred. CommitIntended, } +/// What a recorded operation does to its target. +/// +/// A `Replace` publishes proposed bytes over an existing file. A `Remove` +/// deletes the file (ADR-0055): its proposed artifact is the empty artifact +/// and it has no stage. The default keeps every earlier record unchanged; +/// older readers refuse a `Remove` record. +#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum ReplacementKind { + /// Publish proposed bytes over the existing file. + #[default] + Replace, + /// Remove the existing file after its bytes are checkpointed. + Remove, +} + +impl ReplacementKind { + /// Whether this is the default kind, which is not written to a record. + pub const fn is_replace(&self) -> bool { + matches!(self, Self::Replace) + } +} + /// Recorded terminal result. This enum cannot establish a native side effect. #[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(rename_all = "snake_case")] @@ -158,6 +182,10 @@ pub struct RecordedFileIdentity { pub struct RecordedReplacement { /// Unique operation ID, also the fixed UUID-named private entry locator. pub operation_id: Uuid, + /// Whether the operation replaces or removes its target. Absent from the + /// serialized form for a replacement. + #[serde(skip_serializing_if = "ReplacementKind::is_replace")] + pub kind: ReplacementKind, /// Historical session association. pub session_id: Uuid, /// Historical workspace association, not a binding. @@ -168,11 +196,12 @@ pub struct RecordedReplacement { pub target: NativePath, /// Expected original-byte claim, bounded to 1 MiB. pub expected: RecordedArtifact, - /// Proposed-byte claim, bounded to 1 MiB. + /// Proposed-byte claim, bounded to 1 MiB. Always the empty artifact for a + /// removal. pub proposed: RecordedArtifact, /// Exact manifest reference, if preparation was recorded. pub manifest: Option, - /// Stage identity, if stage preparation was recorded. + /// Stage identity, if stage preparation was recorded. A removal has none. pub stage_identity: Option, /// Last recorded step, retained even after outcome or acknowledgement. pub progress: ReplacementProgress, diff --git a/crates/omgcode-core/src/replacement/manifest.rs b/crates/omgcode-core/src/replacement/manifest.rs index d67e82a..f336776 100644 --- a/crates/omgcode-core/src/replacement/manifest.rs +++ b/crates/omgcode-core/src/replacement/manifest.rs @@ -6,7 +6,9 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use super::store::CheckpointError; -use super::{RecordedArtifact, RecordedFileIdentity, RecordedReplacement, ReplacementScope}; +use super::{ + RecordedArtifact, RecordedFileIdentity, RecordedReplacement, ReplacementKind, ReplacementScope, +}; use crate::files::FileTarget; use crate::session::{NativePath, sha256_hex}; use crate::storage::parse_strict_json; @@ -21,6 +23,9 @@ pub(super) struct Manifest { control_store_id: Uuid, replacement_store_id: Uuid, operation_id: Uuid, + /// Absent for a replacement, so earlier manifests stay byte-identical. + #[serde(default, skip_serializing_if = "ReplacementKind::is_replace")] + kind: ReplacementKind, session_id: Uuid, workspace_id: Uuid, revision: u64, @@ -125,6 +130,7 @@ impl Manifest { pub(super) fn encode( scope: ReplacementScope, operation_id: Uuid, + kind: ReplacementKind, target: &FileTarget, revision: u64, capture: &FileCapture, @@ -135,6 +141,7 @@ impl Manifest { control_store_id: scope.control_store_id(), replacement_store_id: scope.replacement_store_id(), operation_id, + kind, session_id: target.session_id, workspace_id: *target.workspace_id.as_uuid(), revision, @@ -174,6 +181,7 @@ impl Manifest { || manifest.control_store_id != scope.control_store_id() || manifest.replacement_store_id != scope.replacement_store_id() || manifest.operation_id != operation.operation_id + || manifest.kind != operation.kind || manifest.session_id != operation.session_id || manifest.workspace_id != *operation.workspace_id.as_uuid() || manifest.revision != operation.revision diff --git a/crates/omgcode-core/src/replacement/replay.rs b/crates/omgcode-core/src/replacement/replay.rs index ee597e0..7080688 100644 --- a/crates/omgcode-core/src/replacement/replay.rs +++ b/crates/omgcode-core/src/replacement/replay.rs @@ -12,7 +12,8 @@ use uuid::{Uuid, Variant, Version}; use super::{ CheckpointRetention, InitializationPhase, InitializationSnapshot, RecordedArtifact, RecordedCompaction, RecordedFileIdentity, RecordedReplacement, ReplacementError, - ReplacementOutcome, ReplacementProgress, ReplacementScope, ReplacementSnapshot, + ReplacementKind, ReplacementOutcome, ReplacementProgress, ReplacementScope, + ReplacementSnapshot, }; use crate::policy::WorkspaceId; use crate::session::NativePath; @@ -26,6 +27,9 @@ pub(super) const MAX_LEDGER_RECORDS: usize = 65_536; pub(super) const MAX_OPERATIONS: usize = 64; const MAX_PAYLOAD_BYTES: u64 = 1024 * 1024; const MAX_MANIFEST_BYTES: u64 = 64 * 1024; +/// SHA-256 of the empty byte string: the proposed artifact of every removal. +pub(super) const EMPTY_SHA256: &str = + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; #[derive(Deserialize, Serialize)] #[serde(deny_unknown_fields)] @@ -71,6 +75,10 @@ pub(super) enum LedgerAction { Created, ReplacementStarted { operation_id: Uuid, + /// Absent for a replacement, so earlier records stay byte-identical. + /// Older readers refuse a record that carries it. + #[serde(default, skip_serializing_if = "ReplacementKind::is_replace")] + kind: ReplacementKind, session_id: Uuid, workspace_id: Uuid, revision: u64, @@ -118,6 +126,9 @@ pub(super) enum LedgerAction { /// compaction. It follows the compaction record or another carried record. CheckpointCarried { operation_id: Uuid, + /// Absent for a replacement, like the started record. + #[serde(default, skip_serializing_if = "ReplacementKind::is_replace")] + kind: ReplacementKind, session_id: Uuid, workspace_id: Uuid, revision: u64, @@ -136,6 +147,7 @@ pub(super) enum LedgerAction { /// The recorded fields that start an operation, whether new or carried. struct Started { operation_id: Uuid, + kind: ReplacementKind, session_id: Uuid, workspace_id: Uuid, revision: u64, @@ -417,6 +429,7 @@ fn apply(snapshot: &mut ReplacementSnapshot, action: LedgerAction) -> Result<(), LedgerAction::Created => {} LedgerAction::ReplacementStarted { operation_id, + kind, session_id, workspace_id, revision, @@ -438,6 +451,7 @@ fn apply(snapshot: &mut ReplacementSnapshot, action: LedgerAction) -> Result<(), snapshot, Started { operation_id, + kind, session_id, workspace_id, revision, @@ -468,6 +482,9 @@ fn apply(snapshot: &mut ReplacementSnapshot, action: LedgerAction) -> Result<(), content, } => { let operation = pending(snapshot, operation_id)?; + if operation.kind == ReplacementKind::Remove { + return Err(ReplacementError::Invalid("a removal has no stage")); + } if operation.progress != ReplacementProgress::CheckpointPrepared || operation.outcome.is_some() || content != operation.proposed @@ -481,9 +498,12 @@ fn apply(snapshot: &mut ReplacementSnapshot, action: LedgerAction) -> Result<(), } LedgerAction::CommitIntended { operation_id } => { let operation = pending(snapshot, operation_id)?; - if operation.progress != ReplacementProgress::StagePrepared - || operation.outcome.is_some() - { + // A removal has no stage, so its intent follows the checkpoint. + let required = match operation.kind { + ReplacementKind::Replace => ReplacementProgress::StagePrepared, + ReplacementKind::Remove => ReplacementProgress::CheckpointPrepared, + }; + if operation.progress != required || operation.outcome.is_some() { return Err(ReplacementError::Invalid("commit intent is out of order")); } operation.progress = ReplacementProgress::CommitIntended; @@ -596,6 +616,7 @@ fn apply(snapshot: &mut ReplacementSnapshot, action: LedgerAction) -> Result<(), } LedgerAction::CheckpointCarried { operation_id, + kind, session_id, workspace_id, revision, @@ -612,6 +633,7 @@ fn apply(snapshot: &mut ReplacementSnapshot, action: LedgerAction) -> Result<(), snapshot, Started { operation_id, + kind, session_id, workspace_id, revision, @@ -663,6 +685,7 @@ fn carry( )); } let content = started.proposed.clone(); + let kind = started.kind; start(snapshot, started, true)?; let Carried { manifest, @@ -673,10 +696,14 @@ fn carry( acknowledged, } = carried; let prepared = progress != ReplacementProgress::Intent; - let staged = matches!( - progress, - ReplacementProgress::StagePrepared | ReplacementProgress::CommitIntended - ); + if kind == ReplacementKind::Remove && progress == ReplacementProgress::StagePrepared { + return Err(ReplacementError::Invalid("a removal has no stage")); + } + let staged = kind == ReplacementKind::Replace + && matches!( + progress, + ReplacementProgress::StagePrepared | ReplacementProgress::CommitIntended + ); match (prepared, manifest) { (true, Some(manifest)) => apply( snapshot, @@ -751,6 +778,7 @@ fn start( ) -> Result<(), ReplacementError> { let Started { operation_id, + kind, session_id, workspace_id, revision, @@ -780,6 +808,11 @@ fn start( validate_target(&target)?; validate_artifact(&expected, MAX_PAYLOAD_BYTES, true)?; validate_artifact(&proposed, MAX_PAYLOAD_BYTES, true)?; + if kind == ReplacementKind::Remove && (proposed.bytes != 0 || proposed.sha256 != EMPTY_SHA256) { + return Err(ReplacementError::Invalid( + "a removal proposes the empty artifact", + )); + } let workspace_id = WorkspaceId::from_uuid(workspace_id) .map_err(|_| ReplacementError::Invalid("invalid workspace ID"))?; snapshot @@ -791,6 +824,7 @@ fn start( .ok_or(ReplacementError::Limit("retained operations"))?; snapshot.operations.push(RecordedReplacement { operation_id, + kind, session_id, workspace_id, revision, diff --git a/crates/omgcode-core/src/replacement/store.rs b/crates/omgcode-core/src/replacement/store.rs index ade1b83..9071898 100644 --- a/crates/omgcode-core/src/replacement/store.rs +++ b/crates/omgcode-core/src/replacement/store.rs @@ -24,8 +24,8 @@ use super::replay::{ }; use super::{ CheckpointRetention, InitializationPhase, RecordedArtifact, RecordedCompaction, - RecordedReplacement, ReplacementProgress, ReplacementScope, ReplacementSnapshot, - inspect_initialization, inspect_ledger, + RecordedReplacement, ReplacementKind, ReplacementProgress, ReplacementScope, + ReplacementSnapshot, inspect_initialization, inspect_ledger, }; use crate::control::{ControlError, ControlStore}; use crate::files::FileTarget; @@ -416,13 +416,16 @@ fn compact_with( /// Return one retained checkpoint's original bytes for an explicit restore. /// -/// The entry must be prepared and recorded for the same workspace and native -/// target path. Its bytes are reread and compared with the recorded digest. -/// The store is opened read-only and closed before the caller applies them. +/// The entry must be prepared, of the requested kind, and recorded for the +/// same workspace and native target path. Its bytes are reread and compared +/// with the recorded digest. A replacement checkpoint's original must be +/// UTF-8; a removal checkpoint's need not be, because nothing interprets it. +/// The store is opened read-only and closed before the caller restores them. pub(crate) fn read_original( control: &ControlStore, operation_id: Uuid, target: &FileTarget, + kind: ReplacementKind, ) -> Result, CheckpointError> { let (expected, control_id) = context(control)?; if !state_present(&expected.live_path()?)? { @@ -441,6 +444,7 @@ pub(crate) fn read_original( .iter() .find(|operation| { operation.operation_id == operation_id + && operation.kind == kind && operation.retention == CheckpointRetention::Retained && operation.manifest.is_some() }) @@ -455,7 +459,9 @@ pub(crate) fn read_original( .open_dir(OsStr::new(&operation_id.to_string()))?; let mut file = entry.open_file(OsStr::new("original"), false)?; let bytes = read_bounded(&mut file, MAX_PAYLOAD)?; - if descriptor(&bytes)? != operation.expected || std::str::from_utf8(&bytes).is_err() { + if descriptor(&bytes)? != operation.expected + || (kind == ReplacementKind::Replace && std::str::from_utf8(&bytes).is_err()) + { return Err(CheckpointError::Invalid( "checkpoint payload differs or is not UTF-8", )); @@ -737,10 +743,31 @@ impl Store { revision: u64, capture: &FileCapture, proposed: &[u8], + ) -> Result { + self.prepare_kind( + ReplacementKind::Replace, + target, + revision, + capture, + proposed, + ) + } + + /// Prepare a checkpoint of either kind. A removal proposes no bytes. + pub(crate) fn prepare_kind( + &mut self, + kind: ReplacementKind, + target: &FileTarget, + revision: u64, + capture: &FileCapture, + proposed: &[u8], ) -> Result { if self.poisoned { return Err(CheckpointError::Poisoned); } + if kind == ReplacementKind::Remove && !proposed.is_empty() { + return Err(CheckpointError::Invalid("a removal proposes no bytes")); + } if let Some(operation) = self.snapshot.pending() { return Err(CheckpointError::Pending(operation.operation_id)); } @@ -763,6 +790,7 @@ impl Store { let manifest = Manifest::encode( self.snapshot.scope, operation_id, + kind, target, revision, capture, @@ -770,6 +798,7 @@ impl Store { )?; let start = LedgerAction::ReplacementStarted { operation_id, + kind, session_id: target.session_id, workspace_id: *target.workspace_id.as_uuid(), revision, @@ -1121,6 +1150,7 @@ impl Store { for (offset, operation) in retained.enumerate() { let carried = LedgerAction::CheckpointCarried { operation_id: operation.operation_id, + kind: operation.kind, session_id: operation.session_id, workspace_id: *operation.workspace_id.as_uuid(), revision: operation.revision, @@ -1376,6 +1406,14 @@ fn inventory( } else { return Err(CheckpointError::Invalid("unknown checkpoint artifact")); }; + if filename == OsStr::new("stage") + && operation.kind == ReplacementKind::Remove + && operation.retention != CheckpointRetention::Deleting + { + return Err(CheckpointError::Invalid( + "a removal checkpoint has no stage", + )); + } let file = if filename == OsStr::new("stage") { entry.open_stage()? } else { @@ -1463,7 +1501,12 @@ fn validate_prepared( ] { let mut file = entry.open_file(OsStr::new(name), false)?; let bytes = read_bounded(&mut file, MAX_PAYLOAD)?; - if descriptor(&bytes)? != *expected || std::str::from_utf8(&bytes).is_err() { + // A removal keeps its original bytes only to restore them, so they + // are not interpreted as text. + let text_required = name == "proposed" || operation.kind == ReplacementKind::Replace; + if descriptor(&bytes)? != *expected + || (text_required && std::str::from_utf8(&bytes).is_err()) + { return Err(CheckpointError::Invalid( "checkpoint payload differs or is not UTF-8", )); diff --git a/crates/omgcode-core/src/turn.rs b/crates/omgcode-core/src/turn.rs index bdbfb47..50258e5 100644 --- a/crates/omgcode-core/src/turn.rs +++ b/crates/omgcode-core/src/turn.rs @@ -16,7 +16,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, Instant}; -use omgcode_platform::broker::is_missing_entry; +use omgcode_platform::broker::{is_missing_entry, is_oversize_entry}; use serde::Serialize; use serde_json::{Value, json}; use thiserror::Error; @@ -47,10 +47,11 @@ pub use review::{ /// Revision of the tool schemas advertised by this runtime. /// -/// Version `0.3.0` added `workspace_edit_file`. Version `0.4.0` adds -/// `workspace_multi_edit_file` and `workspace_create_file`. Journals record the +/// Version `0.3.0` added `workspace_edit_file`. Version `0.4.0` added +/// `workspace_multi_edit_file` and `workspace_create_file`. Version `0.5.0` adds +/// `workspace_rename_file` and `workspace_delete_file`. Journals record the /// version that was advertised; earlier versions replay unchanged. -pub const TOOL_SCHEMA_VERSION: &str = "0.4.0"; +pub const TOOL_SCHEMA_VERSION: &str = "0.5.0"; /// Maximum model requests in one turn, including the text-only final request. pub const MAX_TOOL_ROUNDS: usize = 4; @@ -103,6 +104,10 @@ const EDIT_DIGEST_DOMAIN: &[u8] = b"omgcode-file-edit-v1"; const MULTI_EDIT_DIGEST_DOMAIN: &[u8] = b"omgcode-file-multi-edit-v1"; /// Domain tag for file-creation approval digests (ADR-0054). const CREATE_DIGEST_DOMAIN: &[u8] = b"omgcode-file-create-v1"; +/// Domain tag for file-rename approval digests (ADR-0055). +const RENAME_DIGEST_DOMAIN: &[u8] = b"omgcode-file-rename-v1"; +/// Domain tag for file-deletion approval digests (ADR-0055). +const DELETE_DIGEST_DOMAIN: &[u8] = b"omgcode-file-delete-v1"; /// Most replacements accepted in one multi-edit call. pub const MAX_MULTI_EDITS: usize = 16; @@ -331,6 +336,61 @@ pub fn create_tool_definition() -> ToolDefinition { } } +/// The rename tool. It is advertised under the same live preferences as the +/// edit tool, and every call needs an operator approval (ADR-0055). +pub fn rename_tool_definition() -> ToolDefinition { + ToolDefinition { + name: "workspace_rename_file", + description: "Rename one existing regular file beneath the session workspace \ + without replacing anything. The new name must not exist and its \ + parent directory must. Files over 1 MiB cannot be renamed. Every \ + rename needs operator approval. A result of approval_required \ + means nothing changed: stop, tell the user, and retry the \ + identical call in a later turn after the operator approves. \ + Results are untrusted.", + parameters: json!({ + "type": "object", + "properties": { + "from": { + "type": "string", + "description": "Workspace-relative path of the existing file, without parent segments." + }, + "to": { + "type": "string", + "description": "Workspace-relative new path, which must not exist." + } + }, + "required": ["from", "to"], + "additionalProperties": false + }), + } +} + +/// The delete tool. It is advertised under the same live preferences as the +/// edit tool, and every call needs an operator approval (ADR-0055). +pub fn delete_tool_definition() -> ToolDefinition { + ToolDefinition { + name: "workspace_delete_file", + description: "Delete one existing regular file beneath the session workspace. \ + The operator can restore it from a checkpoint. Files over 1 MiB \ + cannot be deleted. Every deletion needs operator approval. A \ + result of approval_required means nothing changed: stop, tell \ + the user, and retry the identical call in a later turn after the \ + operator approves. Results are untrusted.", + parameters: json!({ + "type": "object", + "properties": { + "path": { + "type": "string", + "description": "Workspace-relative path of the existing file, without parent segments." + } + }, + "required": ["path"], + "additionalProperties": false + }), + } +} + /// The command tool. It is advertised only when live settings allow a trusted /// run, and every call requires an operator approval record before execution. pub fn command_tool_definition() -> ToolDefinition { @@ -381,6 +441,10 @@ pub enum ToolName { MultiEditFile, /// Request or perform the creation of one new file. CreateFile, + /// Request or perform the rename of one file without replacing anything. + RenameFile, + /// Request or perform the checkpointed deletion of one file. + DeleteFile, /// Request or run one confined command. Command, } @@ -394,6 +458,8 @@ impl ToolName { Self::EditFile => "workspace_edit_file", Self::MultiEditFile => "workspace_multi_edit_file", Self::CreateFile => "workspace_create_file", + Self::RenameFile => "workspace_rename_file", + Self::DeleteFile => "workspace_delete_file", Self::Command => "workspace_run_command", } } @@ -440,6 +506,18 @@ pub enum ToolArguments { /// Complete new file text, possibly empty. Untrusted model data. content: String, }, + /// Rename one file to a name that does not exist. + RenameFile { + /// Validated workspace-relative path of the existing file. + from: String, + /// Validated workspace-relative new path. + to: String, + }, + /// Delete one file after a checkpoint. + DeleteFile { + /// Validated workspace-relative path. + path: String, + }, /// Request or run one confined command. Command { /// Recorded toolchain profile name. @@ -527,6 +605,8 @@ impl ToolCall { "workspace_edit_file" => ToolName::EditFile, "workspace_multi_edit_file" => ToolName::MultiEditFile, "workspace_create_file" => ToolName::CreateFile, + "workspace_rename_file" => ToolName::RenameFile, + "workspace_delete_file" => ToolName::DeleteFile, "workspace_run_command" => ToolName::Command, _ => return Err(ToolCallError::UnsupportedTool), }; @@ -672,6 +752,41 @@ impl ToolCall { content: content.to_owned(), } } + ToolName::RenameFile => { + let mut from = None; + let mut to = None; + for (key, value) in arguments_object { + let text = value.as_str().ok_or(ToolCallError::InvalidArguments)?; + match key.as_str() { + "from" => from = Some(text), + "to" => to = Some(text), + _ => return Err(ToolCallError::InvalidArguments), + } + } + let from = from.ok_or(ToolCallError::InvalidArguments)?; + let to = to.ok_or(ToolCallError::InvalidArguments)?; + validate_relative_path(from, false)?; + validate_relative_path(to, false)?; + ToolArguments::RenameFile { + from: from.to_owned(), + to: to.to_owned(), + } + } + ToolName::DeleteFile => { + let mut path = None; + for (key, value) in arguments_object { + let text = value.as_str().ok_or(ToolCallError::InvalidArguments)?; + match key.as_str() { + "path" => path = Some(text), + _ => return Err(ToolCallError::InvalidArguments), + } + } + let path = path.ok_or(ToolCallError::InvalidArguments)?; + validate_relative_path(path, false)?; + ToolArguments::DeleteFile { + path: path.to_owned(), + } + } ToolName::Command => { let mut profile = None; let mut program = None; @@ -756,7 +871,9 @@ impl ToolCall { | ToolArguments::ListDirectory { path, .. } | ToolArguments::EditFile { path, .. } | ToolArguments::MultiEditFile { path, .. } - | ToolArguments::CreateFile { path, .. } => path, + | ToolArguments::CreateFile { path, .. } + | ToolArguments::DeleteFile { path } => path, + ToolArguments::RenameFile { from, .. } => from, ToolArguments::Command { .. } => "", } } @@ -1186,6 +1303,8 @@ impl<'a> TurnRuntime<'a> { tools.push(edit_tool_definition()); tools.push(multi_edit_tool_definition()); tools.push(create_tool_definition()); + tools.push(rename_tool_definition()); + tools.push(delete_tool_definition()); } if permissions.is_some_and(command_tool_allowed) { tools.push(command_tool_definition()); @@ -1666,6 +1785,8 @@ impl WorkspaceToolDispatcher<'_> { ToolArguments::EditFile { .. } | ToolArguments::MultiEditFile { .. } | ToolArguments::CreateFile { .. } + | ToolArguments::RenameFile { .. } + | ToolArguments::DeleteFile { .. } ) { if !edit_tool_allowed(permissions) { return ToolExecution::denied( @@ -1681,6 +1802,12 @@ impl WorkspaceToolDispatcher<'_> { ToolArguments::CreateFile { .. } => { self.dispatch_create(control, call, max_result_bytes) } + ToolArguments::RenameFile { .. } => { + self.dispatch_rename(control, call, max_result_bytes) + } + ToolArguments::DeleteFile { .. } => { + self.dispatch_delete(control, call, max_result_bytes) + } _ => self.dispatch_edit(control, call, max_result_bytes), }; } @@ -1719,7 +1846,9 @@ impl WorkspaceToolDispatcher<'_> { } ToolArguments::EditFile { .. } | ToolArguments::MultiEditFile { .. } - | ToolArguments::CreateFile { .. } => { + | ToolArguments::CreateFile { .. } + | ToolArguments::RenameFile { .. } + | ToolArguments::DeleteFile { .. } => { ToolExecution::recoverable("invalid_request", call) } ToolArguments::Command { .. } => ToolExecution::recoverable("invalid_command", call), @@ -1948,6 +2077,164 @@ impl WorkspaceToolDispatcher<'_> { } } + /// Request or perform the rename of one file without replacing anything. + /// + /// Before any approval is recorded, a live audited read of the source + /// supplies its exact size and SHA-256, the destination must be absent, + /// and its parent directory must exist. An approval is consumed before the + /// rename, so a destination that another writer creates in the meantime + /// spends it. Rename is not checkpointed: no content is destroyed and + /// renaming back reverses it. + fn dispatch_rename( + &self, + mut control: ControlStore, + call: &ToolCall, + max_result_bytes: usize, + ) -> ToolExecution { + let ToolArguments::RenameFile { from, to } = &call.arguments else { + return ToolExecution::recoverable("invalid_request", call); + }; + let from_normalized = normalized_path(from); + let to_normalized = normalized_path(to); + if from_normalized == to_normalized { + return ToolExecution::recoverable("invalid_request", call); + } + let binding = match control.bind_session(self.session_id, self.workspace_id) { + Ok(binding) => binding, + Err(error) => { + return ToolExecution::denied( + "policy_unavailable", + call, + FileError::Control(error), + ); + } + }; + let source = edit_target(self.session_id, self.workspace_id, from); + let destination = edit_target(self.session_id, self.workspace_id, to); + let current = match read_move_source(&mut control, &binding, &source, call) { + Ok(artifact) => artifact, + Err(refusal) => return refusal, + }; + if let Some(refusal) = rename_precheck(&mut control, &binding, &destination, call) { + return refusal; + } + let digest = rename_digest( + self.session_id, + self.workspace_id, + &from_normalized, + &to_normalized, + ¤t, + ); + let request = WriteRequest { + digest, + summary: rename_summary(&from_normalized, &to_normalized, ¤t), + widest_success: rename_success_message( + call, + &from_normalized, + &to_normalized, + u64::MAX, + &"f".repeat(64), + Some(Uuid::max()), + ), + noun: "rename", + }; + if let WriteGate::Respond(execution) = + self.write_gate(&mut control, call, request, max_result_bytes) + { + return execution; + } + // The approval is spent. A changed source or a new destination is a + // refusal; an uncertain rename stays terminal and leaves its pending + // audit record for the trusted acknowledgement. + match FileBroker::new(&mut control).rename_file(&binding, &source, &destination, ¤t) { + Ok(renamed) => ToolExecution::success(rename_success_message( + call, + &from_normalized, + &to_normalized, + current.bytes, + ¤t.sha256, + Some(renamed.operation_id), + )), + Err(FileError::Conflict) => ToolExecution::recoverable("file_exists", call), + // An unsupported object or filesystem (another device, an owner + // other than the effective user) changed nothing. + Err(FileError::Native(native)) if native.kind() == io::ErrorKind::Unsupported => { + ToolExecution::recoverable("invalid_request", call) + } + Err(error) => classify_edit_error(call, error), + } + } + + /// Request or perform the deletion of one file after a checkpoint. + /// + /// A live audited read supplies the exact size and SHA-256 that the + /// checkpointed removal must still find. An approval is consumed before the + /// removal, so a changed file spends it. The removal keeps a durable copy + /// of the bytes in the checkpoint store, from which the operator can + /// restore the file. + fn dispatch_delete( + &self, + mut control: ControlStore, + call: &ToolCall, + max_result_bytes: usize, + ) -> ToolExecution { + let ToolArguments::DeleteFile { path } = &call.arguments else { + return ToolExecution::recoverable("invalid_request", call); + }; + let binding = match control.bind_session(self.session_id, self.workspace_id) { + Ok(binding) => binding, + Err(error) => { + return ToolExecution::denied( + "policy_unavailable", + call, + FileError::Control(error), + ); + } + }; + let target = edit_target(self.session_id, self.workspace_id, path); + let current = match read_move_source(&mut control, &binding, &target, call) { + Ok(artifact) => artifact, + Err(refusal) => return refusal, + }; + let normalized = normalized_path(path); + let digest = delete_digest(self.session_id, self.workspace_id, &normalized, ¤t); + let request = WriteRequest { + digest, + summary: delete_summary(&normalized, ¤t), + widest_success: delete_success_message( + call, + &normalized, + u64::MAX, + &"f".repeat(64), + Some(Uuid::max()), + ), + noun: "deletion", + }; + if let WriteGate::Respond(execution) = + self.write_gate(&mut control, call, request, max_result_bytes) + { + return execution; + } + match FileBroker::new(&mut control).apply_removal(&binding, &target, ¤t) { + // The widest report was checked by the gate, so this one fits. + Ok(receipt) if receipt.applied => ToolExecution::success(delete_success_message( + call, + &normalized, + current.bytes, + ¤t.sha256, + receipt.operation_id, + )), + Ok(_) => ToolExecution::recoverable("invalid_request", call), + // An object the checkpoint cannot capture (a read-only, hidden, or + // otherwise unsupported file) is refused before any private + // evidence exists and changed nothing. + Err(FileError::Native(native)) if native.kind() == io::ErrorKind::Unsupported => { + ToolExecution::recoverable("invalid_request", call) + } + Err(error) => classify_edit_error(call, error), + } + } + /// The approval flow shared by every model file-write tool. /// /// An approved record is consumed only after the widest success report is @@ -2627,6 +2914,47 @@ fn create_success_message( labelled_result(call, &body.to_string()) } +/// The labelled report of a renamed file. It never contains file text. +fn rename_success_message( + call: &ToolCall, + from: &str, + to: &str, + bytes: u64, + sha256: &str, + operation_id: Option, +) -> String { + let body = json!({ + "tool": call.name.as_str(), + "status": "renamed", + "from": from, + "to": to, + "bytes": bytes, + "sha256": sha256, + "operation_id": operation_id, + }); + labelled_result(call, &body.to_string()) +} + +/// The labelled report of a deleted file. It never contains file text. The +/// operation ID names the checkpoint from which the operator can restore it. +fn delete_success_message( + call: &ToolCall, + path: &str, + bytes: u64, + sha256: &str, + operation_id: Option, +) -> String { + let body = json!({ + "tool": call.name.as_str(), + "status": "deleted", + "path": path, + "bytes": bytes, + "sha256": sha256, + "operation_id": operation_id, + }); + labelled_result(call, &body.to_string()) +} + /// Apply a prepared edit through the checkpointed apply and report it. /// /// The apply rechecks live read/write, the binding, the qualified filesystem, @@ -2698,6 +3026,40 @@ fn create_precheck( } } +/// Read the live source of a rename or deletion and describe it by size and +/// SHA-256, or return the recoverable refusal. +/// +/// This is the audited read of at most 1 MiB that edit uses, so a larger file +/// is refused as `edit_too_large`. The bytes are not interpreted: neither a +/// rename nor a deletion needs text, so a non-UTF-8 file is accepted. +fn read_move_source( + control: &mut ControlStore, + binding: &SessionBinding, + target: &FileTarget, + call: &ToolCall, +) -> Result { + match read_edit_source(control, binding, target) { + Ok(bytes) => { + artifact(&bytes).ok_or_else(|| ToolExecution::recoverable("edit_too_large", call)) + } + Err(FileError::Native(native)) if is_oversize_entry(&native) => { + Err(ToolExecution::recoverable("edit_too_large", call)) + } + Err(error) => Err(classify_edit_error(call, error)), + } +} + +/// Refuse a rename whose destination exists or whose parent is missing, before +/// any approval is recorded. Returns `None` when the rename may be requested. +fn rename_precheck( + control: &mut ControlStore, + binding: &SessionBinding, + destination: &FileTarget, + call: &ToolCall, +) -> Option { + create_precheck(control, binding, destination, call) +} + /// Describe exact bytes by size and SHA-256. fn artifact(bytes: &[u8]) -> Option { Some(RecordedArtifact { @@ -2777,6 +3139,44 @@ fn create_digest( sha256_hex(&bytes) } +/// Bind one model rename to the session, workspace, both normalized names, +/// and the exact size and SHA-256 of the live source, under its own domain tag. +fn rename_digest( + session_id: Uuid, + workspace_id: WorkspaceId, + from: &str, + to: &str, + source: &RecordedArtifact, +) -> String { + let mut bytes = Vec::new(); + push_field(&mut bytes, RENAME_DIGEST_DOMAIN); + push_field(&mut bytes, session_id.as_bytes()); + push_field(&mut bytes, workspace_id.as_uuid().as_bytes()); + push_field(&mut bytes, from.as_bytes()); + push_field(&mut bytes, to.as_bytes()); + push_field(&mut bytes, &source.bytes.to_le_bytes()); + push_field(&mut bytes, source.sha256.as_bytes()); + sha256_hex(&bytes) +} + +/// Bind one model deletion to the session, workspace, normalized path, and the +/// exact size and SHA-256 of the live file, under its own domain tag. +fn delete_digest( + session_id: Uuid, + workspace_id: WorkspaceId, + path: &str, + current: &RecordedArtifact, +) -> String { + let mut bytes = Vec::new(); + push_field(&mut bytes, DELETE_DIGEST_DOMAIN); + push_field(&mut bytes, session_id.as_bytes()); + push_field(&mut bytes, workspace_id.as_uuid().as_bytes()); + push_field(&mut bytes, path.as_bytes()); + push_field(&mut bytes, ¤t.bytes.to_le_bytes()); + push_field(&mut bytes, current.sha256.as_bytes()); + sha256_hex(&bytes) +} + fn replacement_digest( domain: &[u8], session_id: Uuid, @@ -2830,6 +3230,48 @@ fn create_summary(path: &str, content: &RecordedArtifact) -> String { bounded_path_summary("create ", path, &suffix) } +/// A bounded rename label: `rename {from} -> {to}: {n} bytes, sha256 {12}`. +/// It never contains file text. Both paths are sanitized like an edit path and +/// share the budget, so the summary fits [`MAX_EDIT_SUMMARY_BYTES`]. +fn rename_summary(from: &str, to: &str, source: &RecordedArtifact) -> String { + let suffix = format!( + ": {} bytes, sha256 {}", + source.bytes, + short_digest(&source.sha256) + ); + let prefix = "rename "; + let arrow = " -> "; + // Split what the fixed text leaves between the two names. A short name + // leaves its unused share to the other. + let budget = MAX_EDIT_SUMMARY_BYTES.saturating_sub(prefix.len() + arrow.len() + suffix.len()); + let sanitized = |path: &str| sanitize_label(path); + let (from, to) = (sanitized(from), sanitized(to)); + let half = budget / 2; + let (from_budget, to_budget) = if from.len() <= half { + (from.len(), budget.saturating_sub(from.len())) + } else if to.len() <= half { + (budget.saturating_sub(to.len()), to.len()) + } else { + (half, budget.saturating_sub(half)) + }; + format!( + "{prefix}{}{arrow}{}{suffix}", + shorten_label(from, from_budget), + shorten_label(to, to_budget) + ) +} + +/// A bounded deletion label: `delete {path}: {n} bytes, sha256 {12}`. It never +/// contains file text. +fn delete_summary(path: &str, current: &RecordedArtifact) -> String { + let suffix = format!( + ": {} bytes, sha256 {}", + current.bytes, + short_digest(¤t.sha256) + ); + bounded_path_summary("delete ", path, &suffix) +} + fn short_digest(digest: &str) -> &str { digest.get(..12).unwrap_or(digest) } @@ -2850,8 +3292,14 @@ fn bounded_path_summary(prefix: &str, path: &str, suffix: &str) -> String { let budget = MAX_EDIT_SUMMARY_BYTES .saturating_sub(prefix.len()) .saturating_sub(suffix.len()); - let mut label: String = path - .chars() + let label = shorten_label(sanitize_label(path), budget); + format!("{prefix}{label}{suffix}") +} + +/// Replace control, invisible, and bidirectional formatting characters with +/// `?`, so a path cannot hide or reorder displayed text. +fn sanitize_label(path: &str) -> String { + path.chars() .map(|character| { if character.is_control() || is_hidden_format(character) { '?' @@ -2859,7 +3307,12 @@ fn bounded_path_summary(prefix: &str, path: &str, suffix: &str) -> String { character } }) - .collect(); + .collect() +} + +/// Shorten a sanitized label to `budget` bytes on a character boundary, +/// ending in `...` when it was cut. +fn shorten_label(mut label: String, budget: usize) -> String { if label.len() > budget { let mut end = budget.saturating_sub(3); while !label.is_char_boundary(end) { @@ -2868,7 +3321,7 @@ fn bounded_path_summary(prefix: &str, path: &str, suffix: &str) -> String { label.truncate(end); label.push_str("..."); } - format!("{prefix}{label}{suffix}") + label } /// Invisible, line-breaking, or bidirectional formatting characters that can diff --git a/crates/omgcode-core/src/turn/review.rs b/crates/omgcode-core/src/turn/review.rs index 6f0cba9..827e696 100644 --- a/crates/omgcode-core/src/turn/review.rs +++ b/crates/omgcode-core/src/turn/review.rs @@ -17,19 +17,21 @@ use thiserror::Error; use super::{ PreparedEdit, TextEdit, ToolArguments, ToolCall, ToolName, artifact, command_digest, - command_run_request, create_digest, edit_digest, edit_target, is_hidden_format, + command_run_request, create_digest, delete_digest, edit_digest, edit_target, is_hidden_format, multi_edit_digest, normalized_path, prepare_edit, prepare_multi_edit, read_edit_source, + rename_digest, }; use crate::control::{ApprovalOperation, ApprovalStatus, ControlError, ControlStore}; use crate::file_audit::FileAuditError; use crate::files::FileError; use crate::replacement::CheckpointError; -/// Most distinct file-write requests (edit, multi-edit, and create together) -/// evaluated for one review. +/// Most distinct file-write requests (edit, multi-edit, create, rename, and +/// delete together) evaluated for one review. /// -/// Each evaluation reads a workspace file through the audited broker and -/// writes one audit record, so the work and the audit growth are bounded. +/// Each evaluation reads one or two workspace files through the audited broker +/// and writes one audit record per read, so the work and the audit growth are +/// bounded. const MAX_REVIEW_EDIT_CANDIDATES: usize = 8; /// One recorded model tool request offered as a candidate for review. @@ -122,8 +124,8 @@ pub enum ReviewError { /// [`ReviewError::Busy`], and a settings revision that differs from the /// approval's gives `Stale(SettingsChanged)`. /// 3. Only candidates whose tool belongs to the operation are considered: the -/// command tool for `Command`, and the edit, multi-edit, and create tools -/// for `FileWrite`; with none, the result is `NotFound`. +/// command tool for `Command`, and the edit, multi-edit, create, rename, and +/// delete tools for `FileWrite`; with none, the result is `NotFound`. /// 4. They are tried newest first, each parsed by [`ToolCall::parse`]. A /// candidate that fails to parse is skipped. A command candidate is built /// into a run request by the dispatcher's own constructor. A file-write @@ -134,7 +136,8 @@ pub enum ReviewError { /// are evaluated, newest first. A candidate whose preparation fails for a /// recoverable reason (missing file, no match, ambiguous match, overlap, /// too large, read denied) is not a match, and a create candidate whose -/// path now exists is not a match. +/// path now exists is not a match. A rename candidate also needs its +/// destination to be absent now. /// 5. If none reproduces the digest, the result is `Stale(NoMatchingRequest)`. /// /// The fields of an `Exact` result come from the request that reproduced the @@ -171,11 +174,7 @@ pub fn review_approval( ) -> Result { let tools: &[ToolName] = match approval.operation { ApprovalOperation::Command => &[ToolName::Command], - ApprovalOperation::FileWrite => &[ - ToolName::EditFile, - ToolName::MultiEditFile, - ToolName::CreateFile, - ], + ApprovalOperation::FileWrite => &FILE_WRITE_TOOLS, ApprovalOperation::GitPush | ApprovalOperation::Network => { return Ok(ApprovalReview::NotFound); } @@ -341,6 +340,46 @@ fn review_file_write<'a>( }) .map(|prepared| multi_edit_fields(&prepared, edits)) } + (ToolArguments::RenameFile { from, to }, Some(current)) => { + let from_normalized = normalized_path(from); + let to_normalized = normalized_path(to); + // A rename is reviewable only while its destination is absent. + let destination = edit_target(approval.session_id, approval.workspace_id, to); + let absent = match read_edit_source(&mut control, &binding, &destination) { + Ok(_) => false, + Err(FileError::Native(native)) if is_missing_entry(&native) => true, + Err(error) => { + classify_review_read(error)?; + false + } + }; + artifact(¤t) + .filter(|source| { + absent + && from_normalized != to_normalized + && rename_digest( + approval.session_id, + approval.workspace_id, + &from_normalized, + &to_normalized, + source, + ) == approval.request_sha256 + }) + .map(|source| rename_fields(&from_normalized, &to_normalized, &source)) + } + (ToolArguments::DeleteFile { .. }, Some(current)) => { + let normalized = normalized_path(path); + artifact(¤t) + .filter(|source| { + delete_digest( + approval.session_id, + approval.workspace_id, + &normalized, + source, + ) == approval.request_sha256 + }) + .map(|source| delete_fields(&normalized, &source)) + } (ToolArguments::CreateFile { content, .. }, None) => { let normalized = normalized_path(path); artifact(content.as_bytes()) @@ -366,15 +405,20 @@ fn review_file_write<'a>( Ok(None) } +/// Every tool whose requests a `FileWrite` approval can be bound to. +const FILE_WRITE_TOOLS: [ToolName; 5] = [ + ToolName::EditFile, + ToolName::MultiEditFile, + ToolName::CreateFile, + ToolName::RenameFile, + ToolName::DeleteFile, +]; + /// Map a recorded tool name to a file-write tool. fn file_write_tool(name: &str) -> Option { - [ - ToolName::EditFile, - ToolName::MultiEditFile, - ToolName::CreateFile, - ] - .into_iter() - .find(|tool| tool.as_str() == name) + FILE_WRITE_TOOLS + .into_iter() + .find(|tool| tool.as_str() == name) } /// Decide whether a failed review read is a plain non-match (`Ok`) or leaves @@ -583,6 +627,31 @@ fn create_fields( ] } +/// Rename fields. Both names come from the parsed arguments that reproduced +/// the digest, and the size and digest from the live source. +fn rename_fields( + from: &str, + to: &str, + source: &crate::replacement::RecordedArtifact, +) -> Vec { + vec![ + single("From", display_safe_line(from)), + single("To", display_safe_line(to)), + single("Size", format!("{} bytes", source.bytes)), + single("SHA-256", source.sha256.clone()), + ] +} + +/// Delete fields. The path comes from the parsed arguments that reproduced +/// the digest, and the size and digest from the live file. +fn delete_fields(path: &str, current: &crate::replacement::RecordedArtifact) -> Vec { + vec![ + single("File", display_safe_line(path)), + single("Size", format!("{} bytes", current.bytes)), + single("SHA-256", current.sha256.clone()), + ] +} + fn single(label: &'static str, value: String) -> ReviewField { ReviewField { label, diff --git a/crates/omgcode-core/tests/approval_review.rs b/crates/omgcode-core/tests/approval_review.rs index 4f299d4..012398b 100644 --- a/crates/omgcode-core/tests/approval_review.rs +++ b/crates/omgcode-core/tests/approval_review.rs @@ -1108,3 +1108,290 @@ fn hostile_multi_edit_and_create_text_is_escaped() { "ok\\u{202e}\\u{feff}\\u{000d}\\u{5c}u{200b}\\u{0000}" ); } + +// ---------------------------------------------------- rename and delete + +const RENAME_TOOL: &str = "workspace_rename_file"; +const DELETE_TOOL: &str = "workspace_delete_file"; + +fn rename_call(id: &str, from: &str, to: &str) -> Value { + call(id, RENAME_TOOL, json!({"from": from, "to": to})) +} + +fn delete_call(id: &str, path: &str) -> Value { + call(id, DELETE_TOOL, json!({"path": path})) +} + +fn labelled(request: &ReviewedRequest) -> Vec<(&'static str, String, bool)> { + request + .fields + .iter() + .map(|field| (field.label, field.value.clone(), field.multiline)) + .collect() +} + +#[test] +fn a_rename_approval_reviews_exact_fields_without_touching_the_files() { + let mut fixture = Fixture::new(); + let source = fixture.write_file("a.txt", "alpha\nbeta\n"); + fs::create_dir(fixture.workspace.join("sub")).expect("destination parent"); + fixture.turn(vec![rename_call("rename-1", "a.txt", "sub/b.txt")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let before = statuses_json(&fixture.approvals()); + let reads = fixture.audit_reads(); + + let request = exact(fixture.review(&approval, &recorded)); + + assert_eq!(request.tool, RENAME_TOOL); + assert_eq!( + labelled(&request), + [ + ("From", "a.txt".to_owned(), false), + ("To", "sub/b.txt".to_owned(), false), + ("Size", "11 bytes".to_owned(), false), + ("SHA-256", sha256(b"alpha\nbeta\n"), false), + ] + ); + assert_eq!(fs::read(&source).expect("read"), b"alpha\nbeta\n"); + assert!(!fixture.workspace.join("sub/b.txt").exists()); + assert_eq!(statuses_json(&fixture.approvals()), before); + assert_eq!(fixture.audit_reads(), reads + 2, "source and destination"); +} + +#[test] +fn a_rename_review_is_stale_when_the_source_changes_or_the_destination_exists() { + let mut fixture = Fixture::new(); + let source = fixture.write_file("a.txt", "alpha"); + fixture.turn(vec![rename_call("rename-1", "a.txt", "b.txt")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + // Another writer creates the destination. + let destination = fixture.write_file("b.txt", "theirs"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + assert_eq!(fs::read(&destination).expect("read"), b"theirs"); + fs::remove_file(&destination).expect("clear the destination"); + exact(fixture.review(&approval, &recorded)); + + // The source changes, then disappears. + fs::write(&source, "alpha changed").expect("change the source"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + fs::remove_file(&source).expect("remove the source"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); +} + +/// After an exact review and an approval, the identical call renames the file. +#[test] +fn an_exactly_reviewed_rename_is_the_call_that_renames() { + let mut fixture = Fixture::new(); + fixture.write_file("a.txt", "alpha"); + fixture.turn(vec![rename_call("rename-1", "a.txt", "b.txt")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + approve(&fixture, &approval); + fixture.turn(vec![rename_call("rename-2", "a.txt", "b.txt")]); + + assert!(!fixture.workspace.join("a.txt").exists()); + assert_eq!( + fs::read_to_string(fixture.workspace.join("b.txt")).expect("read"), + "alpha" + ); + assert_eq!(fixture.state(approval.id), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 0); +} + +#[test] +fn a_delete_approval_reviews_exact_fields_and_is_stale_after_a_change() { + let mut fixture = Fixture::new(); + let target = fixture.write_file("old.txt", "alpha\nbeta\n"); + fixture.turn(vec![delete_call("delete-1", "old.txt")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let before = statuses_json(&fixture.approvals()); + let reads = fixture.audit_reads(); + + let request = exact(fixture.review(&approval, &recorded)); + + assert_eq!(request.tool, DELETE_TOOL); + assert_eq!( + labelled(&request), + [ + ("File", "old.txt".to_owned(), false), + ("Size", "11 bytes".to_owned(), false), + ("SHA-256", sha256(b"alpha\nbeta\n"), false), + ] + ); + assert_eq!(fs::read(&target).expect("read"), b"alpha\nbeta\n"); + assert_eq!(fixture.checkpoint_operations(), 0); + assert_eq!(statuses_json(&fixture.approvals()), before); + assert_eq!(fixture.audit_reads(), reads + 1, "one audited read"); + + fs::write(&target, "alpha\nbeta\nchanged\n").expect("change the file"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + fs::remove_file(&target).expect("remove the file"); + assert_eq!( + fixture.review(&approval, &recorded).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); +} + +/// After an exact review and an approval, the identical call deletes the file +/// through a checkpoint. +#[test] +fn an_exactly_reviewed_delete_is_the_call_that_deletes() { + let mut fixture = Fixture::new(); + fixture.write_file("old.txt", "alpha"); + fixture.turn(vec![delete_call("delete-1", "old.txt")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + exact(fixture.review(&approval, &recorded)); + + approve(&fixture, &approval); + fixture.turn(vec![delete_call("delete-2", "old.txt")]); + + assert!(!fixture.workspace.join("old.txt").exists()); + assert_eq!(fixture.state(approval.id), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 1); +} + +/// A review matches only the tool whose digest the approval holds. +#[test] +fn rename_and_delete_candidates_never_match_each_others_or_other_approvals() { + let mut fixture = Fixture::new(); + fixture.write_file("a.txt", "alpha"); + fixture.turn(vec![rename_call("rename-1", "a.txt", "b.txt")]); + let rename_approval = fixture.pending(ApprovalOperation::FileWrite); + let renames = fixture.recorded(); + let others = [ + (DELETE_TOOL, json!({"path": "a.txt"})), + ( + EDIT_TOOL, + json!({"path": "a.txt", "old_text": "alpha", "new_text": "b"}), + ), + (CREATE_TOOL, json!({"path": "b.txt", "content": "alpha"})), + ] + .map(|(tool, arguments)| RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: "other".into(), + tool: tool.to_owned(), + arguments: arguments.to_string(), + }); + assert_eq!( + fixture.review(&rename_approval, &others).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + // The real rename candidate still matches among them. + let mut all = renames; + all.extend(others); + assert_eq!( + exact(fixture.review(&rename_approval, &all)).tool, + RENAME_TOOL + ); + + let mut fixture = Fixture::new(); + fixture.write_file("a.txt", "alpha"); + fixture.turn(vec![delete_call("delete-1", "a.txt")]); + let delete_approval = fixture.pending(ApprovalOperation::FileWrite); + let renamed = vec![RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: "rename-1".into(), + tool: RENAME_TOOL.to_owned(), + arguments: json!({"from": "a.txt", "to": "b.txt"}).to_string(), + }]; + assert_eq!( + fixture.review(&delete_approval, &renamed).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); +} + +/// The eight-candidate bound counts distinct requests across all five +/// file-write tools together, newest first. +#[test] +fn the_candidate_bound_spans_rename_and_delete_too() { + let mut fixture = Fixture::new(); + fixture.write_file("a.txt", "alpha"); + fixture.turn(vec![rename_call("rename-1", "a.txt", "b.txt")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let matching = fixture.recorded(); + + let newer = |count: usize| { + let mut all = matching.clone(); + for index in 0..count { + let (tool, arguments) = if index % 2 == 0 { + (DELETE_TOOL, json!({"path": format!("gone{index}.txt")})) + } else { + ( + RENAME_TOOL, + json!({"from": format!("gone{index}.txt"), "to": "c.txt"}), + ) + }; + all.push(RecordedToolRequest { + request_id: Uuid::new_v4(), + call_id: format!("call-{index}"), + tool: tool.to_owned(), + arguments: arguments.to_string(), + }); + } + all + }; + + // Seven newer candidates read one missing file each, then the match reads + // its source and destination. + let reads = fixture.audit_reads(); + exact(fixture.review(&approval, &newer(7))); + assert_eq!(fixture.audit_reads() - reads, 7 + 2); + + // Eight newer candidates push it beyond the bound. + let reads = fixture.audit_reads(); + assert_eq!( + fixture.review(&approval, &newer(8)).expect("review"), + ApprovalReview::Stale(StaleReason::NoMatchingRequest) + ); + assert_eq!(fixture.audit_reads() - reads, 8); +} + +#[test] +fn hostile_rename_and_delete_paths_are_escaped() { + let mut fixture = Fixture::new(); + fixture.write_file("a\u{202e}b.txt", "alpha"); + fixture.turn(vec![rename_call( + "rename-1", + "a\u{202e}b.txt", + "c\u{200b}d\u{2028}e.txt", + )]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let request = exact(fixture.review(&approval, &recorded)); + for field in &request.fields { + assert_display_clean(&field.value); + } + assert_eq!(field(&request, "From").value, "a\\u{202e}b.txt"); + assert_eq!(field(&request, "To").value, "c\\u{200b}d\\u{2028}e.txt"); + + let mut fixture = Fixture::new(); + fixture.write_file("a\u{202e}b.txt", "alpha"); + fixture.turn(vec![delete_call("delete-1", "a\u{202e}b.txt")]); + let approval = fixture.pending(ApprovalOperation::FileWrite); + let recorded = fixture.recorded(); + let request = exact(fixture.review(&approval, &recorded)); + for field in &request.fields { + assert_display_clean(&field.value); + } + assert_eq!(field(&request, "File").value, "a\\u{202e}b.txt"); +} diff --git a/crates/omgcode-core/tests/replacement_records.rs b/crates/omgcode-core/tests/replacement_records.rs index 62737bd..f8c4213 100644 --- a/crates/omgcode-core/tests/replacement_records.rs +++ b/crates/omgcode-core/tests/replacement_records.rs @@ -3,8 +3,9 @@ use std::path::Path; use omgcode_core::replacement::{ - CheckpointRetention, InitializationPhase, ReplacementError, ReplacementOutcome, - ReplacementProgress, ReplacementScope, inspect_initialization, inspect_ledger, + CheckpointRetention, InitializationPhase, ReplacementError, ReplacementKind, + ReplacementOutcome, ReplacementProgress, ReplacementScope, inspect_initialization, + inspect_ledger, }; use omgcode_core::session::NativePath; use serde_json::{Value, json}; @@ -1201,3 +1202,253 @@ fn compaction_claims_have_strict_bounds() { fixture.events[1]["action"]["payload"]["extra"] = json!(true); assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_err()); } + +// --------------------------------------------------------------------------- +// Removal records (ADR-0055) +// --------------------------------------------------------------------------- + +/// SHA-256 of the empty byte string, the proposed artifact of every removal. +const EMPTY_SHA256: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"; + +impl Fixture { + /// Start a removal: kind `remove` and the empty proposed artifact. + fn start_removal(&mut self) { + self.start(); + let payload = &mut self.events.last_mut().expect("started")["action"]["payload"]; + payload["kind"] = json!("remove"); + payload["proposed"] = json!({"bytes": 0, "sha256": EMPTY_SHA256}); + } + + /// A carried removal that succeeded: no stage, commit intent recorded. + fn carried_removal_payload(&self) -> Value { + let mut payload = self.carried_payload(); + payload["kind"] = json!("remove"); + payload["proposed"] = json!({"bytes": 0, "sha256": EMPTY_SHA256}); + payload["stage_identity"] = Value::Null; + payload + } +} + +#[test] +fn the_empty_digest_constant_is_the_digest_of_nothing() { + use sha2::{Digest, Sha256}; + let digest: String = Sha256::digest(b"") + .iter() + .map(|byte| format!("{byte:02x}")) + .collect(); + assert_eq!(digest, EMPTY_SHA256); +} + +#[test] +fn a_removal_replays_without_a_stage_and_a_replacement_keeps_its_kind() { + let mut fixture = ledger(); + fixture.start_removal(); + fixture.prepared(); + fixture.commit(); + fixture.finish("succeeded"); + let snapshot = inspect_ledger(&fixture.bytes(), fixture.scope()).expect("removal lifecycle"); + assert!(snapshot.pending().is_none()); + let record = &snapshot.operations[0]; + assert_eq!(record.kind, ReplacementKind::Remove); + assert_eq!(record.progress, ReplacementProgress::CommitIntended); + assert_eq!(record.outcome, Some(ReplacementOutcome::Succeeded)); + assert_eq!(record.proposed.bytes, 0); + assert!(record.stage_identity.is_none()); + assert_eq!( + serde_json::to_value(record).expect("serialized record")["kind"], + json!("remove") + ); + + // An earlier record without a kind is a replacement and serializes none. + let mut fixture = ledger(); + fixture.start(); + fixture.prepared(); + fixture.staged(); + fixture.commit(); + fixture.finish("succeeded"); + assert!(fixture.events[1]["action"]["payload"]["kind"].is_null()); + let snapshot = inspect_ledger(&fixture.bytes(), fixture.scope()).expect("replacement"); + assert_eq!(snapshot.operations[0].kind, ReplacementKind::Replace); + assert!( + serde_json::to_value(&snapshot.operations[0]).expect("serialized record")["kind"].is_null() + ); +} + +#[test] +fn a_removal_may_conflict_fail_or_stay_uncertain_before_or_after_intent() { + let mut fixture = ledger(); + fixture.start_removal(); + fixture.prepared(); + fixture.finish("conflict"); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_ok()); + + let mut fixture = ledger(); + fixture.start_removal(); + fixture.prepared(); + fixture.commit(); + fixture.finish("uncertain"); + let snapshot = inspect_ledger(&fixture.bytes(), fixture.scope()).expect("uncertain removal"); + assert_eq!( + snapshot.pending().map(|operation| operation.operation_id), + Some(fixture.operation) + ); + // Success needs commit intent, as for a replacement. + let mut fixture = ledger(); + fixture.start_removal(); + fixture.prepared(); + fixture.finish("succeeded"); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_err()); + // Failure forbids it. + let mut fixture = ledger(); + fixture.start_removal(); + fixture.prepared(); + fixture.commit(); + fixture.finish("failed"); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_err()); +} + +#[test] +fn a_removal_proposes_exactly_the_empty_artifact() { + for proposed in [ + json!({"bytes": 5, "sha256": "b".repeat(64)}), + json!({"bytes": 0, "sha256": "b".repeat(64)}), + json!({"bytes": 5, "sha256": EMPTY_SHA256}), + ] { + let mut fixture = ledger(); + fixture.start_removal(); + fixture.events[1]["action"]["payload"]["proposed"] = proposed.clone(); + assert!( + inspect_ledger(&fixture.bytes(), fixture.scope()).is_err(), + "{proposed}" + ); + } + // A replacement may propose an empty file with any digest rules it had. + let mut fixture = ledger(); + fixture.start(); + fixture.events[1]["action"]["payload"]["proposed"] = + json!({"bytes": 0, "sha256": EMPTY_SHA256}); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_ok()); +} + +#[test] +fn a_removal_has_no_stage_and_commit_intent_follows_the_checkpoint() { + // A stage record is refused for a removal. + let mut fixture = ledger(); + fixture.start_removal(); + fixture.prepared(); + fixture.push( + "stage_prepared", + Some(json!({ + "operation_id": fixture.operation, + "identity": {"volume": 7, "file_id": vec![9; 16]}, + "content": {"bytes": 0, "sha256": EMPTY_SHA256} + })), + ); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_err()); + // Commit intent before the checkpoint is prepared is refused. + let mut fixture = ledger(); + fixture.start_removal(); + fixture.commit(); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_err()); + // A replacement still needs its stage before commit intent. + let mut fixture = ledger(); + fixture.start(); + fixture.prepared(); + fixture.commit(); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_err()); +} + +#[test] +fn an_unknown_operation_kind_is_refused() { + for kind in [json!("delete"), json!(""), json!(1), json!(null)] { + let mut fixture = ledger(); + fixture.start(); + fixture.events[1]["action"]["payload"]["kind"] = kind.clone(); + assert!( + inspect_ledger(&fixture.bytes(), fixture.scope()).is_err(), + "{kind}" + ); + } +} + +#[test] +fn a_carried_removal_is_resolved_stage_free_and_empty_proposed() { + let check = |change: &dyn Fn(&mut Value), valid: bool| { + let mut fixture = ledger(); + fixture.compacted(Some(3)); + let mut payload = fixture.carried_removal_payload(); + change(&mut payload); + fixture.carried(payload); + assert_eq!( + inspect_ledger(&fixture.bytes(), fixture.scope()).is_ok(), + valid, + "{}", + fixture.events[2]["action"]["payload"] + ); + }; + check(&|_| {}, true); + let snapshot = { + let mut fixture = ledger(); + fixture.compacted(Some(3)); + fixture.carried(fixture.carried_removal_payload()); + inspect_ledger(&fixture.bytes(), fixture.scope()).expect("carried removal") + }; + assert_eq!(snapshot.operations[0].kind, ReplacementKind::Remove); + assert!(snapshot.operations[0].stage_identity.is_none()); + // A carried removal with a stage identity is refused. + check( + &|payload| payload["stage_identity"] = json!({"volume": 7, "file_id": vec![9; 16]}), + false, + ); + // So is one that claims the stage step, or a non-empty proposed artifact. + check( + &|payload| { + payload["progress"] = json!("stage_prepared"); + payload["outcome"] = json!("failed"); + payload["code"] = json!("conflict"); + }, + false, + ); + check( + &|payload| payload["proposed"] = json!({"bytes": 5, "sha256": "b".repeat(64)}), + false, + ); + // A removal that stopped before commit intent is a resolved failure. + check( + &|payload| { + payload["progress"] = json!("checkpoint_prepared"); + payload["outcome"] = json!("conflict"); + payload["code"] = json!("target_changed"); + }, + true, + ); + // Success still requires commit intent. + check( + &|payload| payload["progress"] = json!("checkpoint_prepared"), + false, + ); + // A carried replacement keeps its stage rules. + let mut fixture = ledger(); + fixture.compacted(Some(3)); + let mut payload = fixture.carried_payload(); + payload["kind"] = json!("replace"); + fixture.carried(payload); + assert!(inspect_ledger(&fixture.bytes(), fixture.scope()).is_ok()); +} + +#[test] +fn a_removal_entry_can_be_deleted_like_any_other() { + let mut fixture = ledger(); + fixture.start_removal(); + fixture.prepared(); + fixture.commit(); + fixture.finish("succeeded"); + fixture.delete_started(); + fixture.deleted(); + let snapshot = inspect_ledger(&fixture.bytes(), fixture.scope()).expect("deleted removal"); + assert_eq!( + snapshot.operations[0].retention, + CheckpointRetention::Deleted + ); + assert_eq!(snapshot.operations[0].kind, ReplacementKind::Remove); +} diff --git a/crates/omgcode-core/tests/turn_runtime.rs b/crates/omgcode-core/tests/turn_runtime.rs index 6e214d0..8e6f94b 100644 --- a/crates/omgcode-core/tests/turn_runtime.rs +++ b/crates/omgcode-core/tests/turn_runtime.rs @@ -551,7 +551,9 @@ fn allowed_read_and_list_produce_durable_source_labelled_results() { "workspace_list_directory".to_owned(), "workspace_edit_file".to_owned(), "workspace_multi_edit_file".to_owned(), - "workspace_create_file".to_owned() + "workspace_create_file".to_owned(), + "workspace_rename_file".to_owned(), + "workspace_delete_file".to_owned() ]) ); @@ -598,7 +600,9 @@ fn allowed_read_and_list_produce_durable_source_labelled_results() { "workspace_list_directory", "workspace_edit_file", "workspace_multi_edit_file", - "workspace_create_file" + "workspace_create_file", + "workspace_rename_file", + "workspace_delete_file" ] ); @@ -667,7 +671,9 @@ fn allowed_read_and_list_produce_durable_source_labelled_results() { "workspace_list_directory", "workspace_edit_file", "workspace_multi_edit_file", - "workspace_create_file" + "workspace_create_file", + "workspace_rename_file", + "workspace_delete_file" ]) ); } @@ -780,7 +786,7 @@ fn revocation_during_model_io_takes_effect_at_the_next_broker_call() { fn unknown_malformed_and_duplicate_calls_fail_before_dispatch() { let cases = [ ( - call("unknown", "workspace_delete_file", json!({})), + call("unknown", "workspace_move_file", json!({})), ToolCallError::UnsupportedTool, ), ( @@ -1516,7 +1522,9 @@ fn runtime_owns_custom_instruction_and_tool_manifest_fields() { "workspace_list_directory".to_owned(), "workspace_edit_file".to_owned(), "workspace_multi_edit_file".to_owned(), - "workspace_create_file".to_owned() + "workspace_create_file".to_owned(), + "workspace_rename_file".to_owned(), + "workspace_delete_file".to_owned() ]) ); fixture.close_journal(); @@ -2473,7 +2481,7 @@ fn an_older_schema_session_replays_and_accepts_a_new_turn(version: &str, tools: .map(|event| event["payload"]["manifest"]["tool_schema_version"].clone()) .collect(); assert_eq!(versions, [json!(version), json!(TOOL_SCHEMA_VERSION)]); - assert_eq!(TOOL_SCHEMA_VERSION, "0.4.0"); + assert_eq!(TOOL_SCHEMA_VERSION, "0.5.0"); } /// A session whose earlier turn advertised tool schema 0.2.0 still replays and @@ -2688,6 +2696,8 @@ fn an_approved_multi_edit_applies_once_through_one_checkpoint() { "workspace_edit_file", "workspace_multi_edit_file", "workspace_create_file", + "workspace_rename_file", + "workspace_delete_file", ] ); assert_eq!( @@ -3183,3 +3193,636 @@ fn the_new_file_tools_require_live_read_and_write() { } } } + +/// A session recorded under schema 0.4.0 offered multi-edit and create but +/// neither rename nor delete. +#[test] +fn a_tool_schema_0_4_0_session_replays_and_accepts_a_new_turn() { + an_older_schema_session_replays_and_accepts_a_new_turn( + "0.4.0", + &[ + "workspace_read_file", + "workspace_list_directory", + "workspace_edit_file", + "workspace_multi_edit_file", + "workspace_create_file", + "workspace_run_command", + ], + ); +} + +// --------------------------------------------------------------------------- +// Rename and delete tools (ADR-0055) +// --------------------------------------------------------------------------- + +fn rename_call(id: &str, from: &str, to: &str) -> Value { + call(id, "workspace_rename_file", json!({"from": from, "to": to})) +} + +fn delete_call(id: &str, path: &str) -> Value { + call(id, "workspace_delete_file", json!({"path": path})) +} + +impl Fixture { + /// The retained checkpoint operations, with their recorded kind. + fn checkpoint_kinds(&self) -> Vec { + let mut control = ControlStore::open_existing(&self.data) + .expect("open control") + .expect("control store"); + omgcode_core::files::FileBroker::new(&mut control) + .checkpoint_inspect() + .expect("inspect checkpoints") + .operations + .into_iter() + .map(|operation| operation.kind) + .collect() + } +} + +#[test] +fn rename_and_delete_arguments_are_strictly_validated() { + let parsed = ToolCall::parse(&rename_call("r-1", "a.txt", "sub/b.txt")).expect("valid rename"); + assert_eq!( + parsed.arguments, + ToolArguments::RenameFile { + from: "a.txt".to_owned(), + to: "sub/b.txt".to_owned(), + } + ); + assert_eq!(parsed.path(), "a.txt"); + let parsed = ToolCall::parse(&delete_call("d-1", "sub/old.txt")).expect("valid delete"); + assert_eq!( + parsed.arguments, + ToolArguments::DeleteFile { + path: "sub/old.txt".to_owned(), + } + ); + assert_eq!(parsed.path(), "sub/old.txt"); + + for arguments in [ + json!({}), + json!({"from": "a"}), + json!({"to": "b"}), + json!({"from": "a", "to": "b", "extra": 1}), + json!({"from": 1, "to": "b"}), + json!({"from": "a", "to": null}), + json!({"from": "/absolute", "to": "b"}), + json!({"from": "a", "to": "../escape"}), + json!({"from": "", "to": "b"}), + json!({"from": "a", "to": ""}), + json!({"path": "a"}), + ] { + assert!( + ToolCall::parse(&call("r", "workspace_rename_file", arguments.clone())).is_err(), + "{arguments}" + ); + } + for arguments in [ + json!({}), + json!({"path": 1}), + json!({"path": "a", "extra": 1}), + json!({"path": "../a"}), + json!({"path": ""}), + json!({"path": "/abs"}), + json!({"from": "a"}), + ] { + assert!( + ToolCall::parse(&call("d", "workspace_delete_file", arguments.clone())).is_err(), + "{arguments}" + ); + } +} + +#[test] +fn an_approved_rename_moves_the_file_once_without_a_checkpoint() { + let mut fixture = Fixture::new(); + fs::create_dir(fixture.workspace.join("sub")).expect("destination parent"); + let content = "alpha\nbeta\n"; + let source = fixture.workspace.join("a.txt"); + let destination = fixture.workspace.join("sub").join("b.txt"); + fs::write(&source, content).expect("source"); + + let (_, model) = fixture.edit_turn(vec![rename_call("r-1", "a.txt", "sub/b.txt")]); + assert!( + model.requests[0] + .tools + .iter() + .any(|name| name == "workspace_rename_file") + ); + assert!(source.exists() && !destination.exists()); + let message = fixture.last_tool_message(); + assert!(message.contains("approval_required"), "{message}"); + assert!(!message.contains("alpha"), "{message}"); + let approvals = fixture.file_write_approvals(); + assert_eq!(approvals.len(), 1); + assert_eq!(approvals[0].state, ApprovalState::Pending); + assert_eq!( + approvals[0].summary, + format!( + "rename a.txt -> sub/b.txt: 11 bytes, sha256 {}", + &sha256(content.as_bytes())[..12] + ) + ); + + // A repeat while pending records nothing new. + fixture.edit_turn(vec![rename_call("r-2", "a.txt", "sub/b.txt")]); + assert_eq!(fixture.file_write_approvals().len(), 1); + + let approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![rename_call("r-3", "a.txt", "sub/b.txt")]); + assert!(!source.exists()); + assert_eq!(fs::read_to_string(&destination).expect("read"), content); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); + assert_eq!( + fixture.checkpoint_operations(), + 0, + "rename is not checkpointed" + ); + let message = fixture.last_tool_message(); + assert!(message.contains("\"status\":\"renamed\""), "{message}"); + assert!(message.contains("\"from\":\"a.txt\""), "{message}"); + assert!(message.contains("\"to\":\"sub/b.txt\""), "{message}"); + assert!(message.contains("\"bytes\":11"), "{message}"); + assert!(message.contains(&sha256(content.as_bytes())), "{message}"); + assert!(message.contains("\"operation_id\":\""), "{message}"); + assert!(!message.contains("alpha"), "the result holds no file text"); + + // The source is gone: a repeat is refused and records no approval. + fixture.edit_turn(vec![rename_call("r-4", "a.txt", "sub/b.txt")]); + assert_eq!(fixture.file_write_approvals().len(), 1); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + [ + Some("approval_required"), + Some("approval_required"), + None, + Some("not_found") + ] + .map(|code| code.map(str::to_owned)) + ); + // The audit journal names both the source and the destination. + let started: Vec = fixture + .audit_events() + .into_iter() + .filter(|event| { + event["action"]["type"] == "operation_started" + && event["action"]["payload"]["operation"] == "rename_file" + }) + .collect(); + assert_eq!(started.len(), 1, "{started:?}"); + let payload = &started[0]["action"]["payload"]; + assert!(payload["destination"].is_object(), "{payload}"); + assert_ne!(payload["destination"], payload["target"]); +} + +#[test] +fn a_rename_of_a_non_utf8_file_keeps_its_bytes() { + let mut fixture = Fixture::new(); + let bytes = [0xff_u8, 0xfe, 0x00, 0x01]; + fs::write(fixture.workspace.join("blob.bin"), bytes).expect("binary source"); + fixture.edit_turn(vec![rename_call("r-1", "blob.bin", "moved.bin")]); + fixture.approve_pending_edit(); + fixture.edit_turn(vec![rename_call("r-2", "blob.bin", "moved.bin")]); + assert!(!fixture.workspace.join("blob.bin").exists()); + assert_eq!( + fs::read(fixture.workspace.join("moved.bin")).expect("read"), + bytes + ); +} + +#[test] +fn a_rename_approval_does_not_cover_a_changed_source_or_another_request() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + fixture.edit_turn(vec![rename_call("r-1", "a.txt", "b.txt")]); + let approval = fixture.approve_pending_edit(); + + // A different destination is a different request. + fixture.edit_turn(vec![rename_call("r-2", "a.txt", "c.txt")]); + assert!(fixture.workspace.join("a.txt").exists()); + assert!(!fixture.workspace.join("c.txt").exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + + // Another writer changes the source after approval. + fs::write(fixture.workspace.join("a.txt"), "alpha changed").expect("other writer"); + fixture.edit_turn(vec![rename_call("r-3", "a.txt", "b.txt")]); + assert!(fixture.workspace.join("a.txt").exists()); + assert!(!fixture.workspace.join("b.txt").exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert_eq!(fixture.file_write_approvals().len(), 3); +} + +/// An approval records which tool's digest it covers: a delete or an edit of +/// the same file never authorizes the rename, and the reverse. +#[test] +fn rename_and_delete_approvals_cannot_be_consumed_by_other_tools() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + fixture.edit_turn(vec![delete_call("d-1", "a.txt")]); + let delete_approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![ + rename_call("r-1", "a.txt", "b.txt"), + edit_call("e-1", "a.txt", "alpha", "beta"), + multi_call("m-1", "a.txt", &[("alpha", "beta")]), + create_call("c-1", "b.txt", "alpha"), + ]); + assert_eq!( + fs::read_to_string(fixture.workspace.join("a.txt")).expect("read"), + "alpha" + ); + assert!(!fixture.workspace.join("b.txt").exists()); + assert_eq!( + fixture.approval_state(delete_approval), + ApprovalState::Approved + ); + assert_eq!(fixture.file_write_approvals().len(), 5); + + // The reverse: an approved rename does not delete. + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + fixture.edit_turn(vec![rename_call("r-1", "a.txt", "b.txt")]); + let rename_approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![delete_call("d-1", "a.txt")]); + assert!(fixture.workspace.join("a.txt").exists()); + assert_eq!( + fixture.approval_state(rename_approval), + ApprovalState::Approved + ); + assert_eq!(fixture.checkpoint_operations(), 0); +} + +#[test] +fn a_denied_rename_or_delete_writes_nothing_and_the_turn_continues() { + for delete in [false, true] { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + let make = |id: &str| { + if delete { + delete_call(id, "a.txt") + } else { + rename_call(id, "a.txt", "b.txt") + } + }; + fixture.edit_turn(vec![make("x-1")]); + let pending = fixture.file_write_approvals()[0].id; + ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store") + .decide_approval(pending, false, now_unix()) + .expect("deny"); + let (report, _) = fixture.edit_turn(vec![make("x-2")]); + assert_eq!(report.reply, "reported"); + assert!(fixture.workspace.join("a.txt").exists()); + assert!(!fixture.workspace.join("b.txt").exists()); + assert_eq!(fixture.file_write_approvals().len(), 1); + assert_eq!(fixture.checkpoint_operations(), 0); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("approval_denied")); + } +} + +#[test] +fn rename_refusals_are_recoverable_and_record_no_approval() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + fs::write(fixture.workspace.join("present.txt"), "keep").expect("present"); + fs::write( + fixture.workspace.join("big.txt"), + vec![b'x'; MAX_EDIT_FILE_BYTES + 1], + ) + .expect("oversize source"); + let mut model = ScriptedModel::new(vec![ + tool_reply(vec![ + rename_call("r-1", "missing.txt", "b.txt"), + rename_call("r-2", "a.txt", "present.txt"), + rename_call("r-3", "a.txt", "no-such/b.txt"), + rename_call("r-4", "big.txt", "huge.txt"), + rename_call("r-5", "a.txt", "a.txt"), + ]), + text_reply("all refused"), + ]); + let report = fixture + .run(&mut model, TurnLimits::default()) + .expect("recoverable refusals keep the turn going"); + assert_eq!(report.reply, "all refused"); + assert!(fixture.file_write_approvals().is_empty()); + assert_eq!( + fs::read_to_string(fixture.workspace.join("a.txt")).expect("read"), + "alpha" + ); + assert_eq!( + fs::read_to_string(fixture.workspace.join("present.txt")).expect("read"), + "keep" + ); + assert!(!fixture.workspace.join("no-such").exists()); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + [ + "not_found", + "file_exists", + "not_found", + "edit_too_large", + "invalid_request" + ] + .map(|code| Some(code.to_owned())) + ); +} + +/// If another writer creates the destination after approval, the retry's live +/// check refuses it before the approval is consumed, and nothing is replaced. +#[test] +fn a_destination_created_after_approval_is_never_replaced() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + fixture.edit_turn(vec![rename_call("r-1", "a.txt", "b.txt")]); + let approval = fixture.approve_pending_edit(); + fs::write(fixture.workspace.join("b.txt"), "theirs").expect("other writer"); + fixture.edit_turn(vec![rename_call("r-2", "a.txt", "b.txt")]); + assert_eq!( + fs::read_to_string(fixture.workspace.join("b.txt")).expect("read"), + "theirs" + ); + assert!(fixture.workspace.join("a.txt").exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("file_exists")); +} + +#[test] +fn a_rename_or_delete_whose_report_cannot_fit_keeps_its_approval() { + for delete in [false, true] { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + let make = |id: &str| { + if delete { + delete_call(id, "a.txt") + } else { + rename_call(id, "a.txt", "b.txt") + } + }; + fixture.edit_turn(vec![make("x-1")]); + let approval = fixture.approve_pending_edit(); + let mut model = + ScriptedModel::new(vec![tool_reply(vec![make("x-2")]), text_reply("reported")]); + let limits = TurnLimits { + max_result_bytes: 200, + ..TurnLimits::default() + }; + fixture.run(&mut model, limits).expect("recoverable"); + assert!(fixture.workspace.join("a.txt").exists()); + assert!(!fixture.workspace.join("b.txt").exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert!(fixture.last_tool_message().contains("output_too_large")); + fixture.edit_turn(vec![make("x-3")]); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); + assert!(!fixture.workspace.join("a.txt").exists()); + assert_eq!(fixture.workspace.join("b.txt").exists(), !delete); + } +} + +#[test] +fn an_approved_delete_removes_the_file_once_after_a_checkpoint() { + use omgcode_core::replacement::ReplacementKind; + + let mut fixture = Fixture::new(); + let content = "alpha\nbeta\n"; + let target = fixture.workspace.join("old.txt"); + fs::write(&target, content).expect("source"); + + let (_, model) = fixture.edit_turn(vec![delete_call("d-1", "old.txt")]); + assert!( + model.requests[0] + .tools + .iter() + .any(|name| name == "workspace_delete_file") + ); + assert!(target.exists()); + let message = fixture.last_tool_message(); + assert!(message.contains("approval_required"), "{message}"); + let approvals = fixture.file_write_approvals(); + assert_eq!(approvals.len(), 1); + assert_eq!( + approvals[0].summary, + format!( + "delete old.txt: 11 bytes, sha256 {}", + &sha256(content.as_bytes())[..12] + ) + ); + + fixture.edit_turn(vec![delete_call("d-2", "old.txt")]); + assert_eq!(fixture.file_write_approvals().len(), 1); + assert!(target.exists()); + + let approval = fixture.approve_pending_edit(); + fixture.edit_turn(vec![delete_call("d-3", "old.txt")]); + assert!(!target.exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_kinds(), [ReplacementKind::Remove]); + let message = fixture.last_tool_message(); + assert!(message.contains("\"status\":\"deleted\""), "{message}"); + assert!(message.contains("\"path\":\"old.txt\""), "{message}"); + assert!(message.contains("\"bytes\":11"), "{message}"); + assert!(message.contains(&sha256(content.as_bytes())), "{message}"); + assert!(!message.contains("alpha"), "the result holds no file text"); + + // The operation ID names the checkpoint that restores the file. + let operation = { + let mut control = ControlStore::open_existing(&fixture.data) + .expect("open control") + .expect("control store"); + omgcode_core::files::FileBroker::new(&mut control) + .checkpoint_inspect() + .expect("inspect") + .operations[0] + .operation_id + }; + assert!(message.contains(&operation.to_string()), "{message}"); + + // The file is gone: a repeat is refused and records no approval. + fixture.edit_turn(vec![delete_call("d-4", "old.txt")]); + assert_eq!(fixture.file_write_approvals().len(), 1); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + [ + Some("approval_required"), + Some("approval_required"), + None, + Some("not_found") + ] + .map(|code| code.map(str::to_owned)) + ); +} + +#[test] +fn a_non_utf8_file_can_be_deleted_and_is_checkpointed_exactly() { + let mut fixture = Fixture::new(); + let bytes = [0xff_u8, 0xfe, 0x00, 0x01, 0x80]; + fs::write(fixture.workspace.join("blob.bin"), bytes).expect("binary source"); + fixture.edit_turn(vec![delete_call("d-1", "blob.bin")]); + fixture.approve_pending_edit(); + fixture.edit_turn(vec![delete_call("d-2", "blob.bin")]); + assert!(!fixture.workspace.join("blob.bin").exists()); + assert_eq!(fixture.checkpoint_operations(), 1); +} + +#[test] +fn a_delete_approval_does_not_cover_a_changed_file() { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + fixture.edit_turn(vec![delete_call("d-1", "a.txt")]); + let approval = fixture.approve_pending_edit(); + fs::write(fixture.workspace.join("a.txt"), "alpha changed").expect("other writer"); + fixture.edit_turn(vec![delete_call("d-2", "a.txt")]); + assert_eq!( + fs::read_to_string(fixture.workspace.join("a.txt")).expect("read"), + "alpha changed" + ); + assert_eq!(fixture.approval_state(approval), ApprovalState::Approved); + assert_eq!(fixture.file_write_approvals().len(), 2); + assert_eq!(fixture.checkpoint_operations(), 0); +} + +#[test] +fn delete_refusals_are_recoverable_and_record_no_approval() { + let mut fixture = Fixture::new(); + fs::write( + fixture.workspace.join("big.txt"), + vec![b'x'; MAX_EDIT_FILE_BYTES + 1], + ) + .expect("oversize source"); + let mut model = ScriptedModel::new(vec![ + tool_reply(vec![ + delete_call("d-1", "missing.txt"), + delete_call("d-2", "big.txt"), + ]), + text_reply("all refused"), + ]); + let report = fixture + .run(&mut model, TurnLimits::default()) + .expect("recoverable refusals keep the turn going"); + assert_eq!(report.reply, "all refused"); + assert!(fixture.file_write_approvals().is_empty()); + assert!(fixture.workspace.join("big.txt").exists()); + assert_eq!(fixture.checkpoint_operations(), 0); + fixture.close_journal(); + assert_eq!( + fixture.tool_codes(), + ["not_found", "edit_too_large"].map(|code| Some(code.to_owned())) + ); +} + +/// Without live `read` and `write`, neither tool is offered, and a call made +/// anyway is denied before any file access or approval. +#[test] +fn the_rename_and_delete_tools_require_live_read_and_write() { + for (read, write) in [(true, false), (false, true)] { + for name in ["workspace_rename_file", "workspace_delete_file"] { + let mut fixture = Fixture::new(); + fs::write(fixture.workspace.join("a.txt"), "alpha").expect("source"); + let workspace_id = fixture.workspace_id; + let control = fixture.control_mut(); + control + .set_permissions( + workspace_id, + PermissionSet { + model_tools: true, + read, + write, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("change preferences"); + let attempt = if name == "workspace_rename_file" { + rename_call("n-1", "a.txt", "b.txt") + } else { + delete_call("n-1", "a.txt") + }; + let mut model = ScriptedModel::new(vec![tool_reply(vec![attempt])]); + let error = fixture + .run(&mut model, TurnLimits::default()) + .expect_err("the call is denied"); + assert!(matches!(error, TurnError::ToolDenied(_)), "{error:?}"); + assert!( + !model.requests[0].tools.iter().any(|tool| tool == name), + "{name} was offered with read={read} write={write}" + ); + assert!(fixture.workspace.join("a.txt").exists()); + assert!(fixture.file_write_approvals().is_empty()); + assert!(fixture.audit_events().is_empty(), "no broker operation ran"); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("edit_disabled")); + } + } +} + +/// With `execute` also enabled, all eight tool slots are used, in the +/// documented order, and the manifest records the same list. +#[test] +fn all_eight_tools_are_advertised_in_order_with_execute_enabled() { + let mut fixture = Fixture::new(); + let workspace_id = fixture.workspace_id; + let control = fixture.control_mut(); + control + .set_permissions( + workspace_id, + PermissionSet { + model_tools: true, + execute: true, + ..PermissionSet::default() + }, + control.revision(), + ) + .expect("enable execute"); + let mut model = ScriptedModel::new(vec![text_reply("done")]); + let report = fixture + .run(&mut model, TurnLimits::default()) + .expect("turn completes"); + let expected = [ + "workspace_read_file", + "workspace_list_directory", + "workspace_edit_file", + "workspace_multi_edit_file", + "workspace_create_file", + "workspace_rename_file", + "workspace_delete_file", + "workspace_run_command", + ]; + assert_eq!(model.requests[0].tools, expected); + assert_eq!( + report.manifest.tools, + Some(expected.map(str::to_owned).to_vec()) + ); +} + +/// A file that the checkpoint cannot capture is refused as a recoverable result +/// after the approval is spent, and nothing is removed or recorded. +#[cfg(windows)] +#[test] +#[allow( + clippy::permissions_set_readonly_false, + reason = "Windows only: this clears the read-only attribute, not a Unix mode" +)] +fn deleting_a_read_only_file_is_a_recoverable_refusal() { + let mut fixture = Fixture::new(); + let target = fixture.workspace.join("locked.txt"); + fs::write(&target, "alpha").expect("source"); + let mut permissions = fs::metadata(&target).expect("metadata").permissions(); + permissions.set_readonly(true); + fs::set_permissions(&target, permissions).expect("read-only"); + fixture.edit_turn(vec![delete_call("d-1", "locked.txt")]); + let approval = fixture.approve_pending_edit(); + let (report, _) = fixture.edit_turn(vec![delete_call("d-2", "locked.txt")]); + assert_eq!(report.reply, "reported"); + assert!(target.exists()); + assert_eq!(fixture.approval_state(approval), ApprovalState::Consumed); + assert_eq!(fixture.checkpoint_operations(), 0); + let mut permissions = fs::metadata(&target).expect("metadata").permissions(); + permissions.set_readonly(false); + fs::set_permissions(&target, permissions).expect("make it removable again"); + fixture.close_journal(); + assert_eq!(fixture.last_tool_code().as_deref(), Some("invalid_request")); +} diff --git a/crates/omgcode-desktop/ui/tests/approvals.test.ts b/crates/omgcode-desktop/ui/tests/approvals.test.ts index 235dc52..50bff9a 100644 --- a/crates/omgcode-desktop/ui/tests/approvals.test.ts +++ b/crates/omgcode-desktop/ui/tests/approvals.test.ts @@ -236,6 +236,47 @@ describe("approvals window", () => { expect(review.querySelector("pre")?.textContent).toBe(`hello\n${HOSTILE_IMG}`); }); + it("renders the fields of the rename and delete tools as plain text", async () => { + approvals = [view({ operation: "file_write" })]; + current = detail( + { + tool: "workspace_rename_file", + fields: [ + { label: "From", value: "src/old.rs", multiline: false }, + { label: "To", value: `src/new${HOSTILE_IMG}.rs`, multiline: false }, + { label: "Size", value: "11 bytes", multiline: false }, + { label: "SHA-256", value: "e".repeat(64), multiline: false }, + ], + }, + { operation: "file_write" }, + ); + await start(); + await select(); + let review = root.querySelector(".review") as HTMLElement; + expect(review.textContent).toContain("workspace_rename_file"); + expect(review.textContent).toContain("From"); + expect(review.textContent).toContain("To"); + expect(review.textContent).toContain(`src/new${HOSTILE_IMG}.rs`); + expect(review.querySelector("img")).toBeNull(); + + current = detail( + { + tool: "workspace_delete_file", + fields: [ + { label: "File", value: `old${HOSTILE_IMG}.txt`, multiline: false }, + { label: "Size", value: "11 bytes", multiline: false }, + { label: "SHA-256", value: "f".repeat(64), multiline: false }, + ], + }, + { operation: "file_write" }, + ); + await select(); + review = root.querySelector(".review") as HTMLElement; + expect(review.textContent).toContain("workspace_delete_file"); + expect(review.textContent).toContain(`old${HOSTILE_IMG}.txt`); + expect(review.querySelector("img")).toBeNull(); + }); + it("requires the writers-stopped acknowledgement for a file change", async () => { approvals = [view({ operation: "file_write" })]; current = detail({ tool: "workspace_edit_file" }, { operation: "file_write" }); diff --git a/crates/omgcode-platform/src/broker.rs b/crates/omgcode-platform/src/broker.rs index 2216156..1b43ff4 100644 --- a/crates/omgcode-platform/src/broker.rs +++ b/crates/omgcode-platform/src/broker.rs @@ -58,6 +58,35 @@ pub fn is_missing_entry(error: &io::Error) -> bool { } } +/// Marks a read refusal whose cause is that the file is larger than the +/// caller's byte bound, so callers can tell it from other invalid objects. +#[derive(Debug)] +struct OversizeFile(&'static str); + +impl std::fmt::Display for OversizeFile { + fn fmt(&self, output: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + output.write_str(self.0) + } +} + +impl std::error::Error for OversizeFile {} + +/// Build the `InvalidData` error that a bounded read reports for an oversize +/// file. The kind is unchanged from earlier releases. +#[cfg(any(target_os = "linux", windows))] +pub(crate) fn oversize_file(message: &'static str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, OversizeFile(message)) +} + +/// Report whether a broker read error means the file exceeded the byte bound. +/// This grants no authority; it only classifies a refusal. +pub fn is_oversize_entry(error: &io::Error) -> bool { + error + .get_ref() + .and_then(|inner| inner.downcast_ref::()) + .is_some() +} + /// A bounded sequence of native names. It grants no filesystem authority. /// /// Empty paths identify the workspace for listing only. Other paths reject @@ -238,6 +267,40 @@ impl std::error::Error for CreateError { } } +/// Why a no-replace rename did not complete. Never retry an `Uncertain` result. +#[derive(Debug)] +pub enum RenameError { + /// Nothing was renamed: the destination name already exists. + TargetExists, + /// Nothing was renamed: the source name no longer resolves to the object + /// whose size and content the caller expected. + SourceChanged, + /// Nothing was renamed: the request or an object was refused. + Refused(io::Error), + /// A rename may have happened, or its durability is unknown. The caller + /// must inspect the workspace and must not retry. + Uncertain(io::Error), +} + +impl std::fmt::Display for RenameError { + fn fmt(&self, output: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::TargetExists => output.write_str("rename destination already exists"), + Self::SourceChanged => output.write_str("rename source no longer matches"), + Self::Refused(error) | Self::Uncertain(error) => error.fmt(output), + } + } +} + +impl std::error::Error for RenameError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::Refused(error) | Self::Uncertain(error) => Some(error), + Self::TargetExists | Self::SourceChanged => None, + } + } +} + /// Captured source bytes and native metadata from the same retained file. /// This is evidence, not a write grant or an atomic filesystem snapshot. #[derive(Debug)] @@ -355,6 +418,41 @@ impl Workspace { require_file_target(path).map_err(not_created)?; self.native.create_dir(path) } + + /// Rename one regular, single-link file to a name that does not exist, + /// without ever replacing an entry. + /// + /// Both parents are opened handle-relative beneath the workspace without + /// links or mount crossings. The source is pinned, must be a regular file + /// with one link on the destination's volume, and its bytes are read + /// through the pinned handle: the length must equal `expected_len` and + /// `matches` must accept them, or nothing is renamed. `matches` is called + /// with at most one MiB and holds no authority. One rename attempt is made + /// and never repeated; an error after it can be `Uncertain`. The caller + /// must stop other writers. Windows makes no power-loss claim (ADR-0023). + #[cfg(any(target_os = "linux", windows))] + pub fn rename_no_replace( + &self, + from: &RelativePath, + to: &RelativePath, + expected_len: u64, + matches: &dyn Fn(&[u8]) -> bool, + ) -> Result<(), RenameError> { + require_file_target(from).map_err(RenameError::Refused)?; + require_file_target(to).map_err(RenameError::Refused)?; + if from == to { + return Err(RenameError::Refused(invalid( + "rename source and destination are the same", + ))); + } + let limit = u64::try_from(MAX_FILE_BYTES) + .map_err(|_| RenameError::Refused(invalid("rename size bound is invalid")))?; + if expected_len > limit { + return Err(RenameError::Refused(invalid("rename source exceeds 1 MiB"))); + } + self.native + .rename_no_replace(from, to, expected_len, matches) + } } /// A workspace root marker is valid only for directory listing. diff --git a/crates/omgcode-platform/src/broker/linux.rs b/crates/omgcode-platform/src/broker/linux.rs index e5c8954..911849a 100644 --- a/crates/omgcode-platform/src/broker/linux.rs +++ b/crates/omgcode-platform/src/broker/linux.rs @@ -21,6 +21,7 @@ use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; use std::time::SystemTime; +pub(super) mod rename; pub(super) mod replacement; const OPEN_RETRIES: usize = 4; @@ -146,7 +147,7 @@ impl NativeWorkspace { > u64::try_from(max_bytes) .map_err(|_| invalid_input("read limit cannot be represented"))? { - return Err(invalid_data("file exceeds the read limit")); + return Err(super::oversize_file("file exceeds the read limit")); } after_validation()?; @@ -174,7 +175,7 @@ impl NativeWorkspace { .take(take_limit) .read_to_end(&mut bytes)?; if bytes.len() > max_bytes { - return Err(invalid_data("file grew beyond the read limit")); + return Err(super::oversize_file("file grew beyond the read limit")); } let after = regular_single_link_snapshot(&readable)?; if !reopened.same_object_and_version(after) { @@ -613,11 +614,11 @@ mod tests { use std::sync::mpsc; use std::time::Duration; - fn relative(path: &str) -> RelativePath { + pub(super) fn relative(path: &str) -> RelativePath { RelativePath::new(Path::new(path)).expect("valid relative path") } - fn workspace() -> (tempfile::TempDir, NativeWorkspace) { + pub(super) fn workspace() -> (tempfile::TempDir, NativeWorkspace) { let temporary = tempfile::tempdir().expect("temporary root"); let root = temporary.path().join("root"); let selected = root.join("workspace"); diff --git a/crates/omgcode-platform/src/broker/linux/rename.rs b/crates/omgcode-platform/src/broker/linux/rename.rs new file mode 100644 index 0000000..d0f011f --- /dev/null +++ b/crates/omgcode-platform/src/broker/linux/rename.rs @@ -0,0 +1,366 @@ +//! Linux handle-relative rename of one pinned regular file that never replaces +//! an entry (ADR-0055). +//! +//! The kernel call is `renameat2` with `RENAME_NOREPLACE` between two parent +//! directory descriptors that were opened beneath the workspace without links +//! or mount crossings. The source is pinned and read through the pinned +//! object first. The name is compared with the pinned inode immediately before +//! the call and the destination name is compared with it afterwards. + +use std::ffi::OsString; +use std::fs::File; +use std::io::{self, Read}; +use std::os::unix::fs::MetadataExt; +use std::path::Path; + +use rustix::fs::{Mode, OFlags, RenameFlags, ResolveFlags, fsync, openat2, renameat_with}; +use rustix::io::Errno; + +use super::{ + NativeWorkspace, RelativePath, invalid_data, invalid_input, regular_single_link_snapshot, + reopen_pinned_file, +}; +use crate::broker::RenameError; + +/// Points where a test can interrupt the protocol with an injected failure. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(super) enum RenamePoint { + /// After the last source check; a failure here renames nothing. + BeforeRename, + /// After the kernel call, before the destination is verified. + PublishedCheck, + /// Before the destination parent is flushed. + TargetParentSync, + /// Before the source parent is flushed. + SourceParentSync, +} + +impl NativeWorkspace { + pub(in crate::broker) fn rename_no_replace( + &self, + from: &RelativePath, + to: &RelativePath, + expected_len: u64, + matches: &dyn Fn(&[u8]) -> bool, + ) -> Result<(), RenameError> { + self.rename_with_hook(from, to, expected_len, matches, &|_| Ok(())) + } + + pub(super) fn rename_with_hook( + &self, + from: &RelativePath, + to: &RelativePath, + expected_len: u64, + matches: &dyn Fn(&[u8]) -> bool, + hook: &dyn Fn(RenamePoint) -> io::Result<()>, + ) -> Result<(), RenameError> { + let workspace = self.workspace_handle().map_err(RenameError::Refused)?; + let (from_parent, from_name) = self + .parent_and_name(&workspace, from) + .map_err(RenameError::Refused)?; + let (to_parent, to_name) = self + .parent_and_name(&workspace, to) + .map_err(RenameError::Refused)?; + let pinned = open_name(&from_parent, &from_name).map_err(source_error)?; + let snapshot = regular_single_link_snapshot(&pinned).map_err(RenameError::Refused)?; + if snapshot.owner != rustix::process::geteuid().as_raw() { + return Err(RenameError::Refused(io::Error::new( + io::ErrorKind::Unsupported, + "rename requires a source owned by the effective user", + ))); + } + if snapshot.length != expected_len { + return Err(RenameError::SourceChanged); + } + let from_directory = from_parent.metadata().map_err(RenameError::Refused)?; + let to_directory = to_parent.metadata().map_err(RenameError::Refused)?; + if from_directory.dev() != to_directory.dev() || snapshot.device != to_directory.dev() { + return Err(RenameError::Refused(io::Error::new( + io::ErrorKind::Unsupported, + "rename source and destination are on different filesystems", + ))); + } + let mut readable = reopen_pinned_file(&pinned).map_err(RenameError::Refused)?; + let reopened = regular_single_link_snapshot(&readable).map_err(RenameError::Refused)?; + if !snapshot.same_object_and_version(reopened) { + return Err(RenameError::SourceChanged); + } + let limit = expected_len + .checked_add(1) + .ok_or_else(|| RenameError::Refused(invalid_input("rename size bound overflows")))?; + let mut bytes = Vec::new(); + Read::by_ref(&mut readable) + .take(limit) + .read_to_end(&mut bytes) + .map_err(RenameError::Refused)?; + let read_length = u64::try_from(bytes.len()) + .map_err(|_| RenameError::Refused(invalid_data("rename source length overflows")))?; + if read_length != expected_len || !matches(&bytes) { + return Err(RenameError::SourceChanged); + } + let after = regular_single_link_snapshot(&readable).map_err(RenameError::Refused)?; + if !snapshot.same_object_and_version(after) { + return Err(RenameError::SourceChanged); + } + // The name must still be the pinned inode immediately before the call. + let current = open_name(&from_parent, &from_name).map_err(source_error)?; + let current = current.metadata().map_err(RenameError::Refused)?; + if current.dev() != snapshot.device || current.ino() != snapshot.inode { + return Err(RenameError::SourceChanged); + } + hook(RenamePoint::BeforeRename).map_err(RenameError::Refused)?; + match renameat_with( + &from_parent, + &from_name, + &to_parent, + &to_name, + RenameFlags::NOREPLACE, + ) { + Ok(()) => {} + Err(Errno::EXIST) => return Err(RenameError::TargetExists), + Err(error) if is_definite_refusal(error) => { + return Err(RenameError::Refused(error.into())); + } + Err(error) => return Err(RenameError::Uncertain(error.into())), + } + // From here the rename happened. Any failure is reported as uncertain + // durability or identity and is never retried. + let same_directory = from_directory.dev() == to_directory.dev() + && from_directory.ino() == to_directory.ino(); + (|| -> io::Result<()> { + hook(RenamePoint::PublishedCheck)?; + let renamed = open_name(&to_parent, &to_name)?.metadata()?; + if renamed.dev() != snapshot.device || renamed.ino() != snapshot.inode { + return Err(invalid_data( + "renamed name does not resolve to the pinned source", + )); + } + hook(RenamePoint::TargetParentSync)?; + fsync(&to_parent)?; + if !same_directory { + hook(RenamePoint::SourceParentSync)?; + fsync(&from_parent)?; + } + Ok(()) + })() + .map_err(RenameError::Uncertain) + } +} + +/// Open a name as an `O_PATH` handle without following links or leaving the +/// mount, the same way replacement pins its target. +fn open_name(parent: &File, name: &OsString) -> io::Result { + Ok(File::from(openat2( + parent, + Path::new(name), + OFlags::PATH | OFlags::CLOEXEC, + Mode::empty(), + ResolveFlags::BENEATH + | ResolveFlags::NO_SYMLINKS + | ResolveFlags::NO_MAGICLINKS + | ResolveFlags::NO_XDEV, + )?)) +} + +/// A missing source name means it changed since the caller's read. +fn source_error(error: io::Error) -> RenameError { + if error.kind() == io::ErrorKind::NotFound { + RenameError::SourceChanged + } else { + RenameError::Refused(error) + } +} + +/// Kernel failures that prove `renameat2` renamed nothing. Any other failure +/// is reported as uncertain. +fn is_definite_refusal(error: Errno) -> bool { + [ + Errno::NOENT, + Errno::ACCESS, + Errno::PERM, + Errno::ROFS, + Errno::XDEV, + Errno::INVAL, + Errno::NOSYS, + Errno::NOTDIR, + Errno::ISDIR, + Errno::LOOP, + Errno::NAMETOOLONG, + Errno::NOSPC, + Errno::DQUOT, + Errno::BUSY, + Errno::MLINK, + ] + .contains(&error) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::broker::linux::tests::{relative, workspace}; + use std::fs; + use std::sync::atomic::{AtomicUsize, Ordering}; + + /// The workspace directory inside the fixture's temporary root. + fn ws(directory: &tempfile::TempDir) -> std::path::PathBuf { + directory.path().join("root").join("workspace") + } + + fn digest_matches(expected: &'static [u8]) -> impl Fn(&[u8]) -> bool { + move |bytes| bytes == expected + } + + #[test] + fn rename_moves_one_file_and_keeps_its_bytes() { + let (directory, native) = workspace(); + fs::write(ws(&directory).join("a.txt"), b"alpha").expect("source"); + fs::create_dir(ws(&directory).join("sub")).expect("destination parent"); + native + .rename_no_replace( + &relative("a.txt"), + &relative("sub/b.txt"), + 5, + &digest_matches(b"alpha"), + ) + .expect("rename"); + assert!(!ws(&directory).join("a.txt").exists()); + assert_eq!( + fs::read(ws(&directory).join("sub/b.txt")).expect("renamed"), + b"alpha" + ); + } + + #[test] + fn rename_refuses_an_existing_destination_and_keeps_both_files() { + let (directory, native) = workspace(); + fs::write(ws(&directory).join("a.txt"), b"alpha").expect("source"); + fs::write(ws(&directory).join("b.txt"), b"beta").expect("existing"); + let error = native + .rename_no_replace( + &relative("a.txt"), + &relative("b.txt"), + 5, + &digest_matches(b"alpha"), + ) + .expect_err("an existing destination is never replaced"); + assert!(matches!(error, RenameError::TargetExists), "{error:?}"); + assert_eq!(fs::read(ws(&directory).join("a.txt")).expect("a"), b"alpha"); + assert_eq!(fs::read(ws(&directory).join("b.txt")).expect("b"), b"beta"); + } + + #[test] + fn rename_refuses_directories_links_and_hard_links() { + let (directory, native) = workspace(); + fs::create_dir(ws(&directory).join("dir")).expect("directory"); + fs::write(ws(&directory).join("file"), b"x").expect("file"); + std::os::unix::fs::symlink("file", ws(&directory).join("link")).expect("link"); + fs::hard_link(ws(&directory).join("file"), ws(&directory).join("twin")).expect("hard link"); + for name in ["dir", "link", "file", "twin"] { + let error = native + .rename_no_replace( + &relative(name), + &relative("moved"), + 1, + &digest_matches(b"x"), + ) + .expect_err("refused before any rename"); + assert!( + matches!(error, RenameError::Refused(_)), + "{name}: {error:?}" + ); + } + assert!(!ws(&directory).join("moved").exists()); + } + + #[test] + fn rename_refuses_a_source_whose_bytes_or_length_differ() { + let (directory, native) = workspace(); + fs::write(ws(&directory).join("a.txt"), b"alpha").expect("source"); + for (length, expected) in [ + (5u64, b"alphx" as &'static [u8]), + (4u64, b"alph" as &'static [u8]), + ] { + let error = native + .rename_no_replace( + &relative("a.txt"), + &relative("b.txt"), + length, + &digest_matches(expected), + ) + .expect_err("a different source is refused"); + assert!(matches!(error, RenameError::SourceChanged), "{error:?}"); + } + assert!(ws(&directory).join("a.txt").exists()); + assert!(!ws(&directory).join("b.txt").exists()); + } + + #[test] + fn a_source_swapped_before_the_call_is_refused() { + let (directory, native) = workspace(); + let source = ws(&directory).join("a.txt"); + fs::write(&source, b"alpha").expect("source"); + let swap = source.clone(); + let error = native + .rename_with_hook( + &relative("a.txt"), + &relative("b.txt"), + 5, + &digest_matches(b"alpha"), + &move |point| { + // The hook runs after the last check, so a swap here is the + // race that the post-call identity check must expose. + if point == RenamePoint::BeforeRename { + fs::remove_file(&swap)?; + fs::write(&swap, b"other")?; + } + Ok(()) + }, + ) + .expect_err("the destination no longer holds the pinned inode"); + assert!(matches!(error, RenameError::Uncertain(_)), "{error:?}"); + } + + #[test] + fn faults_before_and_after_the_call_are_refused_or_uncertain() { + let (directory, native) = workspace(); + fs::write(ws(&directory).join("a.txt"), b"alpha").expect("source"); + let error = native + .rename_with_hook( + &relative("a.txt"), + &relative("b.txt"), + 5, + &digest_matches(b"alpha"), + &|point| { + if point == RenamePoint::BeforeRename { + Err(io::Error::other("injected")) + } else { + Ok(()) + } + }, + ) + .expect_err("a fault before the call renames nothing"); + assert!(matches!(error, RenameError::Refused(_)), "{error:?}"); + assert!(ws(&directory).join("a.txt").exists()); + + let calls = AtomicUsize::new(0); + let error = native + .rename_with_hook( + &relative("a.txt"), + &relative("b.txt"), + 5, + &digest_matches(b"alpha"), + &|point| { + calls.fetch_add(1, Ordering::SeqCst); + if point == RenamePoint::TargetParentSync { + Err(io::Error::other("injected")) + } else { + Ok(()) + } + }, + ) + .expect_err("a fault after the call is uncertain"); + assert!(matches!(error, RenameError::Uncertain(_)), "{error:?}"); + assert!(ws(&directory).join("b.txt").exists(), "the rename happened"); + assert!(!ws(&directory).join("a.txt").exists()); + } +} diff --git a/crates/omgcode-platform/src/broker/linux/replacement.rs b/crates/omgcode-platform/src/broker/linux/replacement.rs index 6f8b1ad..193e231 100644 --- a/crates/omgcode-platform/src/broker/linux/replacement.rs +++ b/crates/omgcode-platform/src/broker/linux/replacement.rs @@ -8,8 +8,8 @@ use crate::broker::{FileCapture, MAX_FILE_BYTES}; use crate::directory_identity; use crate::private_store::{NativeSecurity, PrivateDirectory, PrivateFileIdentity, StageMetadata}; use rustix::fs::{ - AtFlags, Mode, OFlags, RenameFlags, ResolveFlags, StatxFlags, fchmod, fchown, fsync, openat2, - renameat_with, statx, + AtFlags, Mode, OFlags, RenameFlags, ResolveFlags, StatxFlags, fchmod, fchown, fstat, fsync, + openat2, renameat_with, statx, unlinkat, }; use rustix::process::{Gid, Uid}; use std::ffi::{OsStr, OsString}; @@ -56,6 +56,8 @@ enum TestPoint { StageSync, SourceParentSync, TargetParentSync, + Unlink, + Unlinked, } impl NativeWorkspace { @@ -329,6 +331,45 @@ impl ReplacementTarget<'_> { Ok(()) } + /// Recheck the retained target before a removal without changing it. + /// + /// `false` means the name no longer resolves to the object whose bytes were + /// captured, so a caller can record a conflict before any removal intent. + pub fn recheck_removal(&self) -> io::Result { + self.target_matches_current() + } + + /// Make one native removal attempt of the verified name. + /// + /// The target is consumed, so one retained target allows one attempt. The + /// name is rechecked, then removed with `unlinkat`. That call is by name, + /// so a swap between the recheck and the call cannot be prevented here. + /// It is detected afterwards: the pinned file's link count must be zero, + /// otherwise a different entry was removed and the result is an error. + /// Every error is an uncertain result and must never be retried. + pub fn remove(self, before_attempt: impl FnOnce() -> io::Result<()>) -> io::Result<()> { + if !self.target_matches_current()? { + return Err(changed_during_operation()); + } + #[cfg(test)] + self.run_hook(TestPoint::AfterFinalCheck)?; + before_attempt()?; + #[cfg(test)] + self.run_hook(TestPoint::Unlink)?; + unlinkat(&self.parent, &self.name, AtFlags::empty())?; + #[cfg(test)] + self.run_hook(TestPoint::Unlinked)?; + if fstat(&self.source)?.st_nlink != 0 { + return Err(invalid_data( + "the removed name was not the verified file; a different entry may be gone", + )); + } + #[cfg(test)] + self.run_hook(TestPoint::TargetParentSync)?; + fsync(&self.parent)?; + Ok(()) + } + #[cfg(test)] fn run_hook(&self, point: TestPoint) -> io::Result<()> { if let Some(hook) = &self.hook { @@ -1015,4 +1056,118 @@ mod tests { 42 ); } + + #[test] + fn removal_deletes_only_the_verified_name_after_ordered_checks() { + let (root, workspace, _storage) = fixture(); + let source_path = root.path().join("workspace/file"); + fs::write(&source_path, b"before").expect("source"); + fs::write(root.path().join("workspace/other"), b"keep").expect("bystander"); + let mut target = target(&workspace); + assert!(target.recheck_removal().expect("recheck")); + let events = Arc::new(Mutex::new(Vec::new())); + let recorded = Arc::clone(&events); + target.hook = Some(Arc::new(move |point| { + recorded.lock().expect("event lock").push(point); + Ok(()) + })); + target.remove(|| Ok(())).expect("remove"); + assert!(!source_path.exists()); + assert_eq!( + fs::read(root.path().join("workspace/other")).expect("bystander"), + b"keep" + ); + assert_eq!( + *events.lock().expect("events"), + vec![ + TestPoint::AfterFinalCheck, + TestPoint::Unlink, + TestPoint::Unlinked, + TestPoint::TargetParentSync, + ] + ); + } + + #[test] + fn removal_recheck_fails_for_changed_bytes_and_a_swapped_inode() { + let (root, workspace, _storage) = fixture(); + let source_path = root.path().join("workspace/file"); + fs::write(&source_path, b"before").expect("source"); + let target = target(&workspace); + fs::write(&source_path, b"changed").expect("content change"); + assert!(!target.recheck_removal().expect("recheck")); + assert!(target.remove(|| Ok(())).is_err()); + assert!(source_path.exists(), "a changed file is never removed"); + } + + #[test] + fn a_name_swapped_before_unlink_is_reported_and_the_pinned_file_survives() { + let (root, workspace, _storage) = fixture(); + let source_path = root.path().join("workspace/file"); + fs::write(&source_path, b"before").expect("source"); + let mut target = target(&workspace); + let moved = root.path().join("workspace/moved"); + let swapped = source_path.clone(); + let moved_to = moved.clone(); + target.hook = Some(Arc::new(move |point| { + if point == TestPoint::Unlink { + // Keep the verified inode linked under another name, then put a + // different file at the verified name. + fs::rename(&swapped, &moved_to)?; + fs::write(&swapped, b"impostor")?; + } + Ok(()) + })); + let error = target + .remove(|| Ok(())) + .expect_err("a swapped name is detected after the unlink"); + assert!( + error.to_string().contains("not the verified file"), + "{error}" + ); + assert_eq!(fs::read(&moved).expect("verified file kept"), b"before"); + } + + #[test] + fn removal_faults_before_and_after_the_call_are_single_attempts() { + let (root, workspace, _storage) = fixture(); + let source_path = root.path().join("workspace/file"); + fs::write(&source_path, b"before").expect("source"); + let mut before = target(&workspace); + before.hook = Some(Arc::new(|point| { + if point == TestPoint::Unlink { + return Err(io::Error::other("injected fault before unlink")); + } + Ok(()) + })); + assert!(before.remove(|| Ok(())).is_err()); + assert!(source_path.exists(), "nothing was removed"); + + let mut after = target(&workspace); + after.hook = Some(Arc::new(|point| { + if point == TestPoint::Unlinked { + return Err(io::Error::other("injected fault after unlink")); + } + Ok(()) + })); + assert!(after.remove(|| Ok(())).is_err()); + assert!( + !source_path.exists(), + "the removal happened and is reported as an error" + ); + } + + #[test] + fn a_refusal_from_the_last_moment_check_removes_nothing() { + let (root, workspace, _storage) = fixture(); + let source_path = root.path().join("workspace/file"); + fs::write(&source_path, b"before").expect("source"); + let target = target(&workspace); + assert!( + target + .remove(|| Err(io::Error::other("authorization refused"))) + .is_err() + ); + assert!(source_path.exists()); + } } diff --git a/crates/omgcode-platform/src/broker/windows.rs b/crates/omgcode-platform/src/broker/windows.rs index 4bfbd60..0d21f50 100644 --- a/crates/omgcode-platform/src/broker/windows.rs +++ b/crates/omgcode-platform/src/broker/windows.rs @@ -42,6 +42,7 @@ use crate::{DirectoryIdentity, directory_identity}; use super::{CreateError, DirectoryEntry, EntryKind, Listing, RelativePath}; +pub(super) mod rename; pub(super) mod replacement; const WINDOWS_EPOCH_TICKS: i64 = 116_444_736_000_000_000; @@ -589,8 +590,32 @@ fn encode_component(name: &OsStr) -> io::Result> { /// source handle needs DELETE access, and every open handle to the replaced /// object must permit delete sharing or the call fails. One attempt is made; /// an error is never retried here. The caller flushes the parent. -#[allow(unsafe_code)] pub(crate) fn rename_onto(source: &File, parent: &File, name: &OsStr) -> io::Result<()> { + rename_by_handle( + source, + parent, + name, + FILE_RENAME_REPLACE_IF_EXISTS | FILE_RENAME_POSIX_SEMANTICS, + ) +} + +/// Move the object behind `source` to `name` beneath `parent` and never +/// replace an existing entry (ADR-0055). +/// +/// The rename is by handle with POSIX semantics and without the +/// replace-if-exists flag, so an existing name fails with +/// `STATUS_OBJECT_NAME_COLLISION`, which is reported as +/// `ErrorKind::AlreadyExists`. The source handle needs DELETE access and +/// `parent` needs FILE_ADD_FILE. One attempt is made; an error is never +/// retried here. The caller flushes both parents. +pub(crate) fn rename_no_replace_onto(source: &File, parent: &File, name: &OsStr) -> io::Result<()> { + rename_by_handle(source, parent, name, FILE_RENAME_POSIX_SEMANTICS) +} + +/// Build one `FILE_RENAME_INFORMATION` for `name` beneath `parent` and apply +/// it to `source` with the given extended flags. +#[allow(unsafe_code)] +fn rename_by_handle(source: &File, parent: &File, name: &OsStr, flags: u32) -> io::Result<()> { let units = encode_component(name)?; if units.is_empty() { return Err(invalid_input("replacement requires a target name")); @@ -616,8 +641,7 @@ pub(crate) fn rename_onto(source: &File, parent: &File, name: &OsStr) -> io::Res // `total` bytes, so every field and all of `name_bytes` at `name_offset` are // in bounds. `parent` outlives the synchronous call that reads its handle. let status = unsafe { - (*information).Anonymous.Flags = - FILE_RENAME_REPLACE_IF_EXISTS | FILE_RENAME_POSIX_SEMANTICS; + (*information).Anonymous.Flags = flags; (*information).RootDirectory = parent.as_raw_handle() as HANDLE; (*information).FileNameLength = file_name_length; std::ptr::copy_nonoverlapping( @@ -696,7 +720,9 @@ fn require_file_size(file: &File, max_bytes: usize) -> io::Result<()> { let limit = u64::try_from(max_bytes).map_err(|_| invalid_input("file read limit is too large"))?; if size > limit { - return Err(invalid_data("file exceeds the requested read limit")); + return Err(super::oversize_file( + "file exceeds the requested read limit", + )); } Ok(()) } @@ -709,7 +735,9 @@ fn read_bounded(reader: impl Read, max_bytes: usize) -> io::Result> { let mut data = Vec::new(); reader.take(read_limit).read_to_end(&mut data)?; if data.len() > max_bytes { - return Err(invalid_data("file exceeds the requested read limit")); + return Err(super::oversize_file( + "file exceeds the requested read limit", + )); } Ok(data) } diff --git a/crates/omgcode-platform/src/broker/windows/rename.rs b/crates/omgcode-platform/src/broker/windows/rename.rs new file mode 100644 index 0000000..5da7a69 --- /dev/null +++ b/crates/omgcode-platform/src/broker/windows/rename.rs @@ -0,0 +1,472 @@ +//! Windows handle-relative rename of one pinned regular file that never +//! replaces an entry (ADR-0055). +//! +//! The source is opened relative to its parent with DELETE access and with +//! sharing that denies every other writer and deleter, so the name cannot be +//! swapped or removed while the handle is held. Its bytes are read through +//! that handle. The rename is `NtSetInformationFile` with +//! `FileRenameInformationEx`, POSIX semantics, and no replace-if-exists flag, +//! with the destination parent as the root directory. An existing name fails +//! with a collision status. Afterwards the destination is reopened and must be +//! the same file. Windows directory flushes carry no power-loss claim +//! (ADR-0023). + +use std::ffi::OsStr; +use std::fs::File; +use std::io::{self, Seek, SeekFrom}; + +use windows_sys::Wdk::Storage::FileSystem::FILE_OPEN; +use windows_sys::Win32::Foundation::{ + NTSTATUS, STATUS_ACCESS_DENIED, STATUS_DELETE_PENDING, STATUS_DIRECTORY_NOT_EMPTY, + STATUS_FILE_IS_A_DIRECTORY, STATUS_INVALID_PARAMETER, STATUS_NOT_SAME_DEVICE, + STATUS_NOT_SUPPORTED, STATUS_OBJECT_PATH_NOT_FOUND, STATUS_SHARING_VIOLATION, +}; +use windows_sys::Win32::Storage::FileSystem::{ + DELETE, FILE_ADD_FILE, FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE, FILE_SHARE_READ, SYNCHRONIZE, +}; + +use super::{ + DIRECTORY_ACCESS, NativeStatus, NativeWorkspace, RelativePath, SHARE_READ_WRITE, invalid_data, + is_name_not_found, last_directory, open_relative, query_identity, query_standard, read_bounded, + rename_no_replace_onto, validate_component, validate_directory, validate_regular_file, +}; +use crate::broker::RenameError; +use crate::directory_identity; +use crate::private_store::windows::{DIRECTORY_OPTIONS, FILE_OPTIONS, FILE_READ_ACCESS}; + +/// Points where a test can interrupt the protocol with an injected failure. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(super) enum RenamePoint { + /// After the last source check; a failure here renames nothing. + BeforeRename, + /// After the native call, before the destination is verified. + PublishedCheck, + /// Before the destination parent is flushed. + TargetParentSync, + /// Before the source parent is flushed. + SourceParentSync, +} + +/// Statuses that prove the native call renamed nothing. Any other failure is +/// reported as uncertain. +const DEFINITE_REFUSALS: [NTSTATUS; 9] = [ + STATUS_ACCESS_DENIED, + STATUS_SHARING_VIOLATION, + STATUS_OBJECT_PATH_NOT_FOUND, + STATUS_NOT_SAME_DEVICE, + STATUS_INVALID_PARAMETER, + STATUS_NOT_SUPPORTED, + STATUS_DELETE_PENDING, + STATUS_FILE_IS_A_DIRECTORY, + STATUS_DIRECTORY_NOT_EMPTY, +]; + +impl NativeWorkspace { + pub(in crate::broker) fn rename_no_replace( + &self, + from: &RelativePath, + to: &RelativePath, + expected_len: u64, + matches: &dyn Fn(&[u8]) -> bool, + ) -> Result<(), RenameError> { + self.rename_with_hook(from, to, expected_len, matches, &|_| Ok(())) + } + + pub(super) fn rename_with_hook( + &self, + from: &RelativePath, + to: &RelativePath, + expected_len: u64, + matches: &dyn Fn(&[u8]) -> bool, + hook: &dyn Fn(RenamePoint) -> io::Result<()>, + ) -> Result<(), RenameError> { + let refuse = RenameError::Refused; + self.ensure_current().map_err(refuse)?; + let (from_handles, from_name) = self.open_parent(from).map_err(refuse)?; + let (to_handles, to_name) = self.open_parent(to).map_err(refuse)?; + validate_component(&from_name).map_err(refuse)?; + validate_component(&to_name).map_err(refuse)?; + let from_directory = last_directory(&from_handles).map_err(refuse)?; + let to_directory = last_directory(&to_handles).map_err(refuse)?; + let volume = self.workspace_identity.volume; + + // Deny every other writer and deleter for as long as this handle lives. + let source = open_relative( + from_directory, + &from_name, + FILE_READ_ACCESS | DELETE, + FILE_OPEN, + FILE_OPTIONS, + FILE_SHARE_READ, + 0, + ) + .map_err(|error| { + if is_name_not_found(&error) { + RenameError::SourceChanged + } else { + RenameError::Refused(error) + } + })?; + validate_regular_file(&source, volume).map_err(refuse)?; + let source_identity = query_identity(&source).map_err(refuse)?; + let length = u64::try_from(query_standard(&source).map_err(refuse)?.EndOfFile) + .map_err(|_| RenameError::Refused(invalid_data("native file size is negative")))?; + if length != expected_len { + return Err(RenameError::SourceChanged); + } + let limit = usize::try_from(expected_len) + .map_err(|_| RenameError::Refused(invalid_data("rename size is out of range")))?; + let mut reader = source.try_clone().map_err(refuse)?; + reader.seek(SeekFrom::Start(0)).map_err(refuse)?; + let bytes = read_bounded(reader, limit).map_err(refuse)?; + if bytes.len() != limit || !matches(&bytes) { + return Err(RenameError::SourceChanged); + } + let after = query_identity(&source).map_err(refuse)?; + if after.VolumeSerialNumber != source_identity.VolumeSerialNumber + || after.FileId.Identifier != source_identity.FileId.Identifier + { + return Err(RenameError::SourceChanged); + } + + // Reopen both parents with the access that rename and flush need. + let reopen = |directory: &File| { + open_relative( + directory, + OsStr::new(""), + DIRECTORY_ACCESS | FILE_ADD_FILE, + FILE_OPEN, + DIRECTORY_OPTIONS, + SHARE_READ_WRITE, + 0, + ) + }; + let target_parent = reopen(to_directory).map_err(refuse)?; + let source_parent = reopen(from_directory).map_err(refuse)?; + validate_directory(&target_parent, volume).map_err(refuse)?; + validate_directory(&source_parent, volume).map_err(refuse)?; + if source_identity.VolumeSerialNumber != volume { + return Err(RenameError::Refused(io::Error::new( + io::ErrorKind::Unsupported, + "rename source is not on the workspace volume", + ))); + } + hook(RenamePoint::BeforeRename).map_err(RenameError::Refused)?; + if let Err(error) = rename_no_replace_onto(&source, &target_parent, &to_name) { + return Err(classify(error)); + } + + // From here the rename happened. Any failure is reported as uncertain + // identity or durability and is never retried. + let same_directory = directory_identity(&target_parent).map_err(RenameError::Uncertain)? + == directory_identity(&source_parent).map_err(RenameError::Uncertain)?; + (|| -> io::Result<()> { + hook(RenamePoint::PublishedCheck)?; + let renamed = open_relative( + to_directory, + &to_name, + FILE_READ_ACCESS, + FILE_OPEN, + FILE_OPTIONS, + FILE_SHARE_READ | FILE_SHARE_DELETE, + 0, + )?; + validate_regular_file(&renamed, volume)?; + let identity = query_identity(&renamed)?; + if identity.VolumeSerialNumber != source_identity.VolumeSerialNumber + || identity.FileId.Identifier != source_identity.FileId.Identifier + { + return Err(invalid_data( + "renamed name does not resolve to the pinned source", + )); + } + match open_relative( + from_directory, + &from_name, + FILE_READ_ATTRIBUTES | SYNCHRONIZE, + FILE_OPEN, + FILE_OPTIONS, + FILE_SHARE_READ | FILE_SHARE_DELETE, + 0, + ) { + Err(error) if is_name_not_found(&error) => {} + // Another entry may have taken the old name; the pinned + // source must not still be there. + Ok(old) + if query_identity(&old)?.FileId.Identifier + == source_identity.FileId.Identifier => + { + return Err(invalid_data("the old name still resolves to the source")); + } + Ok(_) => {} + Err(error) => return Err(error), + } + hook(RenamePoint::TargetParentSync)?; + target_parent.sync_all()?; + if !same_directory { + hook(RenamePoint::SourceParentSync)?; + source_parent.sync_all()?; + } + Ok(()) + })() + .map_err(RenameError::Uncertain) + } +} + +/// Map a native rename failure: a collision proves the destination exists, a +/// known refusal status proves nothing was renamed, anything else is uncertain. +fn classify(error: io::Error) -> RenameError { + if error.kind() == io::ErrorKind::AlreadyExists { + return RenameError::TargetExists; + } + if error.kind() == io::ErrorKind::InvalidInput { + return RenameError::Refused(error); + } + let refused = error + .get_ref() + .and_then(|inner| inner.downcast_ref::()) + .is_some_and(|status| DEFINITE_REFUSALS.contains(&status.0)); + if refused { + RenameError::Refused(error) + } else { + RenameError::Uncertain(error) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::broker::windows::tests::create_junction_bounded; + use std::fs; + use std::path::{Path, PathBuf}; + use std::sync::atomic::{AtomicBool, Ordering}; + + struct Fixture { + workspace: NativeWorkspace, + directory: PathBuf, + _root: tempfile::TempDir, + } + + fn fixture() -> Fixture { + let root = tempfile::tempdir().expect("root"); + let directory = root.path().join("workspace"); + fs::create_dir(&directory).expect("workspace"); + let open = |path: &Path| { + directory_identity(&super::super::open_configured_root(path).expect("directory")) + .expect("identity") + }; + let workspace = NativeWorkspace::open( + root.path(), + open(root.path()), + &RelativePath::new(Path::new("workspace")).expect("workspace path"), + open(&directory), + ) + .expect("native workspace"); + Fixture { + workspace, + directory, + _root: root, + } + } + + fn relative(path: &str) -> RelativePath { + RelativePath::new(Path::new(path)).expect("relative path") + } + + fn accepts(expected: &'static [u8]) -> impl Fn(&[u8]) -> bool { + move |bytes| bytes == expected + } + + #[test] + fn rename_moves_a_file_across_directories_and_keeps_its_identity() { + let fixture = fixture(); + fs::write(fixture.directory.join("a.txt"), b"alpha").expect("source"); + fs::create_dir(fixture.directory.join("sub")).expect("destination parent"); + let before = fs::metadata(fixture.directory.join("a.txt")).expect("metadata"); + fixture + .workspace + .rename_no_replace( + &relative("a.txt"), + &relative("sub/b.txt"), + 5, + &accepts(b"alpha"), + ) + .expect("rename"); + assert!(!fixture.directory.join("a.txt").exists()); + assert_eq!( + fs::read(fixture.directory.join("sub/b.txt")).expect("renamed"), + b"alpha" + ); + let after = fs::metadata(fixture.directory.join("sub/b.txt")).expect("metadata"); + assert_eq!(before.len(), after.len()); + } + + #[test] + fn rename_refuses_an_existing_destination_and_keeps_both_files() { + let fixture = fixture(); + fs::write(fixture.directory.join("a.txt"), b"alpha").expect("source"); + fs::write(fixture.directory.join("b.txt"), b"beta").expect("existing"); + let error = fixture + .workspace + .rename_no_replace( + &relative("a.txt"), + &relative("b.txt"), + 5, + &accepts(b"alpha"), + ) + .expect_err("an existing destination is never replaced"); + assert!(matches!(error, RenameError::TargetExists), "{error:?}"); + assert_eq!( + fs::read(fixture.directory.join("a.txt")).expect("a"), + b"alpha" + ); + assert_eq!( + fs::read(fixture.directory.join("b.txt")).expect("b"), + b"beta" + ); + } + + #[test] + fn rename_refuses_directories_junctions_and_hard_links() { + let fixture = fixture(); + fs::create_dir(fixture.directory.join("dir")).expect("directory"); + fs::write(fixture.directory.join("file"), b"x").expect("file"); + fs::hard_link( + fixture.directory.join("file"), + fixture.directory.join("twin"), + ) + .expect("hard link"); + let outside = fixture._root.path().join("outside"); + fs::create_dir(&outside).expect("outside"); + create_junction_bounded(&fixture.directory.join("junction"), &outside) + .expect("junction fixture"); + for name in ["dir", "junction", "file", "twin"] { + let error = fixture + .workspace + .rename_no_replace(&relative(name), &relative("moved"), 1, &accepts(b"x")) + .expect_err("refused before any rename"); + assert!( + matches!(error, RenameError::Refused(_)), + "{name}: {error:?}" + ); + } + assert!(!fixture.directory.join("moved").exists()); + assert!(outside.exists()); + } + + #[test] + fn rename_refuses_a_source_whose_bytes_or_length_differ() { + let fixture = fixture(); + fs::write(fixture.directory.join("a.txt"), b"alpha").expect("source"); + for (length, expected) in [ + (5u64, b"alphx" as &'static [u8]), + (4u64, b"alph" as &'static [u8]), + ] { + let error = fixture + .workspace + .rename_no_replace( + &relative("a.txt"), + &relative("b.txt"), + length, + &accepts(expected), + ) + .expect_err("a different source is refused"); + assert!(matches!(error, RenameError::SourceChanged), "{error:?}"); + } + assert!(fixture.directory.join("a.txt").exists()); + assert!(!fixture.directory.join("b.txt").exists()); + } + + #[test] + fn a_missing_source_is_a_changed_source() { + let fixture = fixture(); + let error = fixture + .workspace + .rename_no_replace(&relative("a.txt"), &relative("b.txt"), 0, &|_| true) + .expect_err("nothing to rename"); + assert!(matches!(error, RenameError::SourceChanged), "{error:?}"); + } + + #[test] + fn the_pinned_source_cannot_be_swapped_or_removed_before_the_call() { + let fixture = fixture(); + let source = fixture.directory.join("a.txt"); + fs::write(&source, b"alpha").expect("source"); + let blocked = AtomicBool::new(false); + fixture + .workspace + .rename_with_hook( + &relative("a.txt"), + &relative("b.txt"), + 5, + &accepts(b"alpha"), + &|point| { + if point == RenamePoint::BeforeRename { + // The retained handle denies delete sharing, so the + // swap that the protocol must exclude cannot happen. + let moved = fixture.directory.join("moved.txt"); + blocked.store( + fs::rename(&source, &moved).is_err() + && fs::remove_file(&source).is_err(), + Ordering::SeqCst, + ); + } + Ok(()) + }, + ) + .expect("rename"); + assert!(blocked.load(Ordering::SeqCst)); + assert_eq!( + fs::read(fixture.directory.join("b.txt")).expect("renamed"), + b"alpha" + ); + } + + #[test] + fn faults_before_and_after_the_call_are_refused_or_uncertain() { + let fixture = fixture(); + fs::write(fixture.directory.join("a.txt"), b"alpha").expect("source"); + let error = fixture + .workspace + .rename_with_hook( + &relative("a.txt"), + &relative("b.txt"), + 5, + &accepts(b"alpha"), + &|point| { + if point == RenamePoint::BeforeRename { + Err(io::Error::other("injected")) + } else { + Ok(()) + } + }, + ) + .expect_err("a fault before the call renames nothing"); + assert!(matches!(error, RenameError::Refused(_)), "{error:?}"); + assert!(fixture.directory.join("a.txt").exists()); + + for failing in [RenamePoint::PublishedCheck, RenamePoint::TargetParentSync] { + let fixture = self::fixture(); + fs::write(fixture.directory.join("a.txt"), b"alpha").expect("source"); + let error = fixture + .workspace + .rename_with_hook( + &relative("a.txt"), + &relative("b.txt"), + 5, + &accepts(b"alpha"), + &|point| { + if point == failing { + Err(io::Error::other("injected")) + } else { + Ok(()) + } + }, + ) + .expect_err("a fault after the call is uncertain"); + assert!(matches!(error, RenameError::Uncertain(_)), "{error:?}"); + assert!(fixture.directory.join("b.txt").exists(), "{failing:?}"); + assert!(!fixture.directory.join("a.txt").exists(), "{failing:?}"); + } + } +} diff --git a/crates/omgcode-platform/src/broker/windows/replacement.rs b/crates/omgcode-platform/src/broker/windows/replacement.rs index 16f12c1..7fdb8b6 100644 --- a/crates/omgcode-platform/src/broker/windows/replacement.rs +++ b/crates/omgcode-platform/src/broker/windows/replacement.rs @@ -24,22 +24,22 @@ use windows_sys::Win32::Foundation::{ STATUS_OBJECT_NAME_NOT_FOUND, STATUS_REPARSE_POINT_ENCOUNTERED, STATUS_SHARING_VIOLATION, }; use windows_sys::Win32::Storage::FileSystem::{ - DELETE, FILE_ADD_FILE, FILE_NAME_INFO, FILE_SHARE_DELETE, FILE_SHARE_READ, - FileNormalizedNameInfo, GetFileInformationByHandleEx, SYNCHRONIZE, + DELETE, FILE_ADD_FILE, FILE_NAME_INFO, FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE, + FILE_SHARE_READ, FileNormalizedNameInfo, GetFileInformationByHandleEx, SYNCHRONIZE, }; use super::{ DIRECTORY_ACCESS, NativeStatus, NativeWorkspace, RelativePath, SHARE_READ_WRITE, creation_time, - invalid_data, invalid_input, last_directory, open_relative, query_attributes, query_basic, - query_identity, query_standard, read_bounded, rename_onto, validate_component, - validate_directory, validate_regular_file, + invalid_data, invalid_input, is_name_not_found, last_directory, open_relative, + query_attributes, query_basic, query_identity, query_standard, read_bounded, rename_onto, + validate_component, validate_directory, validate_regular_file, }; use crate::broker::{FileCapture, MAX_FILE_BYTES}; use crate::directory_identity; use crate::private_store::windows::{ DIRECTORY_OPTIONS, FILE_OPTIONS, FILE_READ_ACCESS, STAGE_NAME, apply_security, is_private_file, - read_native_security, require_ntfs, set_content_indexing, split_native_security, - validate_file_metadata, + mark_for_deletion, read_native_security, require_ntfs, set_content_indexing, + split_native_security, validate_file_metadata, }; use crate::private_store::{ NativeSecurity, PrivateDirectory, PrivateFileIdentity, StageMetadata, stage_metadata, @@ -86,6 +86,8 @@ enum TestPoint { SourceParentSync, TargetParentSync, RestorePrivacy, + Delete, + Deleted, } /// One object and version. Access time is excluded because reads change it. @@ -398,6 +400,77 @@ impl ReplacementTarget<'_> { self.parent.sync_all() } + /// Recheck the retained target before a removal without changing it. + /// + /// `false` means the name no longer resolves to the object whose bytes, + /// version, and security were captured. A read-only source was already + /// refused at capture. + pub fn recheck_removal(&self) -> io::Result { + self.target_matches_current() + } + + /// Make one native removal attempt of the verified file. + /// + /// The target is consumed, so one retained target allows one attempt. The + /// name is opened with DELETE access, and that same handle is verified + /// (object, name, security, and every byte) and then marked for + /// POSIX-semantics deletion, so exactly the verified file is deleted even + /// if the name is swapped after the recheck. Every error is an uncertain + /// result and must never be retried. Windows makes no power-loss claim for + /// the parent flush (ADR-0023). + pub fn remove(self, before_attempt: impl FnOnce() -> io::Result<()>) -> io::Result<()> { + if !self.recheck_removal()? { + return Err(changed_during_operation()); + } + #[cfg(test)] + self.run_hook(TestPoint::AfterFinalCheck)?; + before_attempt()?; + #[cfg(test)] + self.run_hook(TestPoint::Delete)?; + let directory = last_directory(&self.handles)?; + let doomed = open_relative( + directory, + &self.name, + FILE_READ_ACCESS | DELETE, + FILE_OPEN, + FILE_OPTIONS, + FILE_SHARE_READ, + 0, + )?; + let verified = file_snapshot(&doomed)? == self.source_snapshot + && has_exact_name(&doomed, &self.name)? + && security_matches(&doomed, &self.capture.security)? + && file_bytes_matches(&doomed, &self.capture.bytes)? + && file_snapshot(&doomed)? == self.source_snapshot; + if !verified { + return Err(changed_during_operation()); + } + mark_for_deletion(&doomed, false)?; + drop(doomed); + #[cfg(test)] + self.run_hook(TestPoint::Deleted)?; + match open_relative( + directory, + &self.name, + FILE_READ_ATTRIBUTES | SYNCHRONIZE, + FILE_OPEN, + FILE_OPTIONS, + SHARE_ALL, + 0, + ) { + Err(error) if is_name_not_found(&error) => {} + Ok(_) => { + return Err(invalid_data( + "the removed name still resolves after deletion", + )); + } + Err(error) => return Err(error), + } + #[cfg(test)] + self.run_hook(TestPoint::TargetParentSync)?; + self.parent.sync_all() + } + /// Give the verified private stage the captured source security and /// indexing opt-out, flush it, and read the result back exactly. /// diff --git a/crates/omgcode-platform/src/broker/windows/replacement/tests.rs b/crates/omgcode-platform/src/broker/windows/replacement/tests.rs index 9dac219..002d72a 100644 --- a/crates/omgcode-platform/src/broker/windows/replacement/tests.rs +++ b/crates/omgcode-platform/src/broker/windows/replacement/tests.rs @@ -966,3 +966,103 @@ fn name_records_return_the_last_component_and_reject_truncation() { assert!(final_component(&bytes).is_err()); assert!(final_component(&[0u8; 2]).is_err()); } + +#[test] +fn removal_deletes_only_the_verified_file_in_order() { + let fixture = Fixture::with_source(b"before"); + let bystander = fixture.path.with_file_name("other"); + fs::write(&bystander, b"keep").expect("bystander"); + let mut target = fixture.target(); + assert!(target.recheck_removal().expect("recheck")); + let events = Arc::new(Mutex::new(Vec::new())); + let recorded = Arc::clone(&events); + target.hook = Some(Arc::new(move |point| { + recorded.lock().expect("event lock").push(point); + Ok(()) + })); + target.remove(|| Ok(())).expect("remove"); + assert!(!fixture.path.exists()); + assert_eq!(fs::read(&bystander).expect("bystander"), b"keep"); + assert_eq!( + *events.lock().expect("events"), + vec![ + TestPoint::AfterFinalCheck, + TestPoint::Delete, + TestPoint::Deleted, + TestPoint::TargetParentSync, + ] + ); +} + +#[test] +fn a_name_swapped_after_the_recheck_is_refused_and_the_impostor_survives() { + let fixture = Fixture::with_source(b"captured"); + let mut target = fixture.target(); + let path = fixture.path.clone(); + target.hook = Some(Arc::new(move |point| { + if point == TestPoint::AfterFinalCheck { + fs::remove_file(&path)?; + fs::write(&path, b"captured")?; + } + Ok(()) + })); + let error = target + .remove(|| Ok(())) + .expect_err("the opened handle is not the captured file"); + assert_eq!(error.kind(), io::ErrorKind::WouldBlock, "{error}"); + assert_eq!( + fs::read(&fixture.path).expect("impostor kept"), + b"captured", + "equal bytes in a different file are never removed" + ); +} + +#[test] +fn removal_recheck_and_removal_refuse_a_changed_target() { + let fixture = Fixture::with_source(b"captured"); + let target = fixture.target(); + fs::remove_file(&fixture.path).expect("remove source"); + fs::write(&fixture.path, b"captured").expect("equal bytes, new file"); + assert!(!target.recheck_removal().expect("recheck")); + assert!(target.remove(|| Ok(())).is_err()); + assert!(fixture.path.exists()); +} + +#[test] +fn removal_faults_before_and_after_the_call_are_single_attempts() { + let fixture = Fixture::with_source(b"before"); + let mut before = fixture.target(); + before.hook = Some(Arc::new(|point| { + if point == TestPoint::Delete { + return Err(io::Error::other("injected fault before delete")); + } + Ok(()) + })); + assert!(before.remove(|| Ok(())).is_err()); + assert!(fixture.path.exists(), "nothing was removed"); + + let mut after = fixture.target(); + after.hook = Some(Arc::new(|point| { + if point == TestPoint::Deleted { + return Err(io::Error::other("injected fault after delete")); + } + Ok(()) + })); + assert!(after.remove(|| Ok(())).is_err()); + assert!( + !fixture.path.exists(), + "the removal happened and is reported as an error" + ); +} + +#[test] +fn a_last_moment_denial_removes_nothing() { + let fixture = Fixture::with_source(b"before"); + let target = fixture.target(); + assert!( + target + .remove(|| Err(io::Error::other("authorization refused"))) + .is_err() + ); + assert!(fixture.path.exists()); +} diff --git a/crates/omgcode-platform/src/private_store/windows.rs b/crates/omgcode-platform/src/private_store/windows.rs index ddad501..0327417 100644 --- a/crates/omgcode-platform/src/private_store/windows.rs +++ b/crates/omgcode-platform/src/private_store/windows.rs @@ -812,7 +812,7 @@ impl WindowsPrivateDirectory { ) .map_err(map_missing)?; validate_regular_file(&file, self.volume)?; - mark_for_deletion(&file)?; + mark_for_deletion(&file, true)?; drop(file); self.require_removed(name) } @@ -835,7 +835,7 @@ impl WindowsPrivateDirectory { .map_err(map_missing)?; validate_directory(&file, self.volume)?; require_private(&file, &self.principals)?; - mark_for_deletion(&file)?; + mark_for_deletion(&file, true)?; drop(file); self.require_removed(name) } @@ -963,18 +963,20 @@ pub fn file_identity(file: &File) -> io::Result { }) } -/// Marks an open handle for POSIX-semantics deletion, ignoring read-only. +/// Marks an open handle for POSIX-semantics deletion, optionally ignoring the +/// read-only attribute. /// /// The handle must have DELETE access. Microsoft documents that the name is /// removed when this handle closes, even if other handles remain open. A -/// nonempty directory fails with `ERROR_DIR_NOT_EMPTY`. +/// nonempty directory fails with `ERROR_DIR_NOT_EMPTY`. Private entries ignore +/// the read-only attribute; a workspace removal does not (ADR-0055). #[allow(unsafe_code)] -fn mark_for_deletion(file: &File) -> io::Result<()> { - let information = FILE_DISPOSITION_INFO_EX { - Flags: FILE_DISPOSITION_FLAG_DELETE - | FILE_DISPOSITION_FLAG_POSIX_SEMANTICS - | FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE, - }; +pub(crate) fn mark_for_deletion(file: &File, ignore_read_only: bool) -> io::Result<()> { + let mut flags = FILE_DISPOSITION_FLAG_DELETE | FILE_DISPOSITION_FLAG_POSIX_SEMANTICS; + if ignore_read_only { + flags |= FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE; + } + let information = FILE_DISPOSITION_INFO_EX { Flags: flags }; let length = u32::try_from(size_of::()) .map_err(|_| invalid_data("disposition information is too large"))?; // SAFETY: `file` is a live handle opened with DELETE access, and diff --git a/docs/README.md b/docs/README.md index aa21c08..a67f3d9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,11 +5,14 @@ bounded native workspace file broker, and a prototype Tauri desktop shell with a session tree, search, chat, and an isolated approvals window. The shell is without an installer and has run only on Windows. The broker is available only through trusted operator commands and, when a workspace enables `model_tools`, to the -local model's read, list, approval-gated edit, multi-edit, create, and command -tools. The edit and multi-edit tools change one existing file only through -checkpointed apply after an approved, writers-stopped decision; the create tool -makes one new file through the exclusive create, without a checkpoint. Model -delete, rename, network, Git, and MCP tools are not implemented. The +local model's read, list, approval-gated edit, multi-edit, create, rename, +delete, and command tools. The edit and multi-edit tools change one existing +file only through checkpointed apply after an approved, writers-stopped +decision; the create tool makes one new file through the exclusive create, +without a checkpoint; the rename tool moves one file to a name that does not +exist, without a checkpoint; and the delete tool removes one file after a +checkpoint of its bytes. Model network, Git, and MCP tools are not +implemented. The trusted CLI `run` command runs one program under Linux Landlock or Windows AppContainer confinement with an operator-declared toolchain profile. A model tool starts a process only through the approval-gated command tool and the @@ -112,11 +115,12 @@ or login exists. That adapter can stream a reply (ADR-0049): the text is provisional, and the journals record only the validated reply. Approval records authorize one exact model-tool call, and the file-write and command tools are their only consumers. Chat dispatches the read/list tools and, with the required live permissions, the -approval-gated edit, multi-edit, create, and command tools, only for a workspace -that enables `model_tools`. The edit and multi-edit tools write only through -checkpointed apply after an approved, writers-stopped decision, the create tool -only through the exclusive create after the same decision, and none detects -concurrent writers. +approval-gated edit, multi-edit, create, rename, delete, and command tools, only +for a workspace that enables `model_tools`. The edit and multi-edit tools write +only through checkpointed apply after an approved, writers-stopped decision, the +create tool only through the exclusive create, the rename tool only through the +no-replace rename, and the delete tool only through a checkpointed removal, each +after the same decision, and none detects concurrent writers. A read-only CLI preview compares a workspace file with trusted proposed content without writing to the workspace or creating a checkpoint. Dependency security audits, native containment qualification, and release checks diff --git a/docs/adr/0055-add-checkpointed-delete-and-no-replace-rename-model-tools.md b/docs/adr/0055-add-checkpointed-delete-and-no-replace-rename-model-tools.md new file mode 100644 index 0000000..8235ee9 --- /dev/null +++ b/docs/adr/0055-add-checkpointed-delete-and-no-replace-rename-model-tools.md @@ -0,0 +1,153 @@ +# 0055: Add checkpointed delete and no-replace rename model tools + +- Status: Accepted +- Date: 2026-11-17 + +## Context + +After ADR-0054 the model can edit, multi-edit, and create files, each after a +trusted single-use approval. It still cannot remove or rename one. Both are +useful during a refactor, and both can destroy work: a removal loses the file's +content, and a rename that replaces an existing name loses that file's content. +The tool-name cap is eight, and six names are in use. This record uses the last +two slots. + +The platform already has a handle-relative replacement commit (ADR-0022, +ADR-0024), a private checkpoint store with a ledger (ADR-0021, ADR-0023, +ADR-0025, ADR-0053), and the audited broker with an exclusive create (ADR-0043, +ADR-0054). Nothing removes a workspace file or renames one without replacing. + +## Decision + +Add two model tools, `workspace_rename_file` and `workspace_delete_file` (tool +schema 0.5.0). Both follow the edit tool's contract; this record states only +what differs. The owning guide is +[model rename and delete routes](../file-access.md#model-rename-and-delete-routes-adr-0055). + +Common rules: + +- Both are advertised and dispatched only under live `model_tools`, `read`, and + `write`, with the edit tool's per-call recheck and `edit_disabled` denial. + They are advertised in the order read, list, edit, multi-edit, create, + rename, delete, command. That is eight names, the existing cap; no cap rose. +- Both reuse `ApprovalOperation::FileWrite`, so the writers-stopped + acknowledgement is required and no approval enum, wire type, or TypeScript + union changes. Each has its own digest domain tag + (`omgcode-file-rename-v1`, `omgcode-file-delete-v1`), so a record for one + tool, or for any of the three earlier file-write tools, is never consumed by + another. Approval lifetime, lookup filters, the output-fits check, and + consumption before the write come from the shared gate. +- Before any approval is recorded, a live audited read of the source supplies + its exact size and SHA-256. A source over 1 MiB is refused as the recoverable + `edit_too_large`. Neither tool interprets the bytes, so a non-UTF-8 source is + accepted. +- The desktop review considers candidates from all five file-write tools, at + most eight distinct candidates in total, newest first, and is exact only for + the candidate whose tool-specific digest matches. + +`workspace_rename_file` takes `from` and `to`, with `additionalProperties: +false`. Both are validated as relative paths and must differ after +normalization. Before an approval is recorded, the source read must succeed +(`not_found`), the destination must not exist (`file_exists`), and its parent +must (`not_found`). The digest covers the session, workspace, both normalized +names, and the source's size and SHA-256. After approval, the broker's new +`rename_file` records the intent with both names in the file-operation audit +journal and calls the platform's `rename_no_replace`. Rename is not +checkpointed: no content is destroyed, and renaming back reverses it. An +uncertain outcome leaves the audit journal's pending record for the trusted +acknowledgement and is never retried. + +`workspace_delete_file` takes `path`. The digest covers the session, workspace, +normalized path, and the file's size and SHA-256. After approval, the broker's +new `apply_removal` runs the same sequence as the checkpointed apply: the same +live-binding, permission, admission, and qualified-filesystem gates, a durable +checkpoint of the original bytes, a recheck, commit intent, one native removal +attempt, and the same uncertain-outcome recovery. The success message names the +checkpoint's operation ID, from which the trusted `file restore` recreates the +file. + +Ledger change. Every operation record gains a `kind` of `replace` or `remove`. +It is omitted for `replace`, so every existing record, carried record, and +manifest stays byte-identical and replays unchanged. For `remove` the proposed +artifact is exactly the empty artifact, no stage exists, and commit intent +follows the prepared checkpoint directly. Replay refuses a removal that +proposes bytes, that has a stage record, or whose carried form has a stage +identity. Compaction carries `kind`. Older binaries refuse a ledger that holds a +`remove` record, because they reject the unknown field. The checkpoint payload +rule that the original is UTF-8 applies only to a `replace`; a `remove` keeps +its original bytes as they are. + +Restoring a `remove` checkpoint is not an apply: there is nothing to +checkpoint. The target must not exist, and the retained original is written +through the broker's audited exclusive create. An existing entry is +`checkpoint_conflict`. The new file takes the broker's default creation +metadata; mode, owner, access list, and timestamps are not restored. The +trusted CLI `file restore` omits the expected digest and size for this case. + +Native primitives: + +- `rename_no_replace` opens both parents handle-relative beneath the workspace + without links or mount crossings, pins the source as a regular single-link + file, reads it through the pinned handle, and requires its size and + SHA-256 to match. On Linux it calls `renameat2` with `RENAME_NOREPLACE`, + re-statting the source name against the pinned inode immediately before and + the destination name against it afterwards. On Windows the source handle + carries DELETE access with sharing that denies every other writer and + deleter, and the rename is `FileRenameInformationEx` with POSIX semantics and + without the replace flag, so an existing name fails with a collision status. +- The removal on Linux is `unlinkat` by name after the recheck, then a check + that the pinned inode's link count is zero. On Windows it opens the name with + DELETE, verifies that same handle (file ID, volume, name, security, and every + byte), and marks that handle for POSIX-semantics deletion. + +## Alternatives considered + +- **Delete without a checkpoint.** Rejected: it destroys the content, and the + operator could not recover from a wrong approval. +- **Delete by moving into a workspace trash directory.** Rejected: it adds + entries to the workspace, and the model could read the file back. +- **Delete by moving into the private store.** Rejected: the data root can be + on another volume, so the move is not an atomic rename. Copy-then-delete + gives the same sequence as the checkpoint plus removal without the new + failure modes. +- **Rename that replaces an existing name.** Rejected: it destroys the replaced + file without a checkpoint. +- **Checkpoint a rename.** Not needed: nothing is destroyed, and renaming back + reverses it. +- **A new approval operation or a shared digest domain.** Rejected for the + reasons recorded in ADR-0054. + +## Consequences + +- Both tools refuse a source over 1 MiB, because the approval-bound read and the + checkpoint are bounded by it. Larger files cannot be renamed or deleted by the + model. +- On Linux the unlink is by name, so a swap between the recheck and the unlink + cannot be prevented. It is detected afterwards through the pinned inode's link + count and reported as uncertain; a different file may then be gone and the + operator must inspect it. On Windows the removal is by the verified handle, so + exactly the verified file is deleted. +- A rename of a file whose name is swapped between the last check and the call + is caught on Linux by the destination identity check, reported uncertain. On + Windows the retained source handle denies other deleters, so the swap cannot + happen. +- Restoring a deleted file does not restore its mode, owner, access list, or + timestamps. +- Older binaries refuse a checkpoint ledger that holds a removal record. +- All eight tool slots are used. A further tool needs a cap decision. +- The schema estimate for context accounting rises from 5 KiB to 6 KiB (the + eight definitions serialize to 5,968 bytes). +- Neither primitive is power-loss qualified, and neither detects a concurrent + writer. Windows directory flushes carry no power-loss claim (ADR-0023). The + Linux paths are not qualified on a Linux host. + +## References + +- [ADR-0021](0021-store-checkpoints-with-native-private-handles.md) +- [ADR-0023](0023-store-windows-checkpoints-with-verified-acls.md) +- [ADR-0024](0024-publish-one-verified-stage-on-windows-ntfs.md) +- [ADR-0025](0025-restore-and-delete-checkpoints-explicitly.md) +- [ADR-0043](0043-gate-the-model-file-edit-tool-with-approvals-and-checkpointed-apply.md) +- [ADR-0053](0053-compact-the-checkpoint-ledger-in-place.md) +- [ADR-0054](0054-add-multi-edit-and-create-model-file-tools.md) +- [File access contract](../file-access.md) diff --git a/docs/adr/index.md b/docs/adr/index.md index 1604f03..2bc2c24 100644 --- a/docs/adr/index.md +++ b/docs/adr/index.md @@ -69,4 +69,5 @@ meaningful alternative does not need an ADR. | [0052: Publish rolling testing builds from main](0052-publish-rolling-testing-builds-from-main.md) | Accepted | 2026-11-17 | | [0053: Compact the checkpoint ledger in place](0053-compact-the-checkpoint-ledger-in-place.md) | Accepted | 2026-11-17 | | [0054: Add multi-edit and create model file tools](0054-add-multi-edit-and-create-model-file-tools.md) | Accepted | 2026-11-17 | +| [0055: Add checkpointed delete and no-replace rename model tools](0055-add-checkpointed-delete-and-no-replace-rename-model-tools.md) | Accepted | 2026-11-17 | | [0056: Send registry credentials to one declared origin](0056-send-registry-credentials-to-one-declared-origin.md) | Accepted | 2026-11-17 | diff --git a/docs/agent-instructions.md b/docs/agent-instructions.md index 9bef5b1..ed59d53 100644 --- a/docs/agent-instructions.md +++ b/docs/agent-instructions.md @@ -33,11 +33,11 @@ skill/tool/memory selections, or an account profile. The core turn/tool runtime records the exact tool schema version and tool names in a schema-version-2 request manifest. Its read/list results are appended as source-labelled user-level tool messages and are never system messages. Edit, -multi-edit, create, and command results follow the same rule and remain -untrusted. Chat runs this runtime when the workspace enables `model_tools`; the -edit, multi-edit, and create tools also need `read` and `write`, the command -tool also needs `execute`, `read`, and `write`, and each runs only after a -trusted approval. +multi-edit, create, rename, delete, and command results follow the same rule +and remain untrusted. Chat runs this runtime when the workspace enables +`model_tools`; the edit, multi-edit, create, rename, and delete tools also need +`read` and `write`, the command tool also needs `execute`, `read`, and `write`, +and each runs only after a trusted approval. Otherwise chat sends no tool definitions and rejects any provider tool call. Prompt assembly limits custom instruction text and each history message to 16 @@ -46,9 +46,10 @@ adapter applies a caller-supplied context budget using a byte-based estimate and fixed overhead. This is not an exact tokenizer or model-capacity check. The prototype has no settings UI, application/workspace/session preference store, preview UI, exact token-count display, skills, memories, model -delete, rename, network, Git, or MCP tools, or provider-specific subscription routes. +network, Git, or MCP tools, or provider-specific subscription routes. Its only model tools are read, list, the approval-gated exact edit, -multi-edit, and create tools, and the approval-gated command tool. The current +multi-edit, create, rename, and delete tools, and the approval-gated command +tool. The current `--instructions` value is per invocation, not a saved preference with scope or revision history. @@ -178,7 +179,12 @@ and `workspace_multi_edit_file` tools create a `file_write` request bound to the live file and consume an approved record immediately before the checkpointed apply. The `workspace_create_file` tool creates a `file_write` request bound to the path and exact content, and consumes an approved record -immediately before the exclusive create. Each tool has its own digest domain, +immediately before the exclusive create. The `workspace_rename_file` tool +creates one bound to both names and the source's size and SHA-256, and +consumes an approved record immediately before the no-replace rename. The +`workspace_delete_file` tool creates one bound to the file's size and SHA-256, +and consumes an approved record immediately before the checkpointed removal. +Each tool has its own digest domain, so a record for one tool never authorizes another. Each first call returns `approval_required`; the operator approves between turns, and the model retries the same call. Approving a `file_write` record needs diff --git a/docs/desktop-interface.md b/docs/desktop-interface.md index a8cc1cd..5dff1c2 100644 --- a/docs/desktop-interface.md +++ b/docs/desktop-interface.md @@ -412,7 +412,8 @@ A panicked turn thread is recorded as a failed turn with the code `internal`. ## Approvals -The model's edit and command tools need a trusted single-use approval +The model's file-write (edit, multi-edit, create, rename, and delete) and +command tools need a trusted single-use approval ([agent instructions](agent-instructions.md#approval-records)). The CLI commands `approvals list`, `show`, `approve`, and `deny` still work and remain the surface for headless use. The approvals window is a second surface for the @@ -459,15 +460,21 @@ Reviewed fields: directory, Network (`off`), Host services (`off`), Time limit (`20 seconds`), and Output limit (`4096 bytes per stream`). - **Edit:** File, Old text, New text, Size, Current SHA-256, and Proposed - SHA-256. + SHA-256. The multi-edit tool numbers its texts (`Old text 1`, `New text 1`, + and so on), and the create tool shows File, Size, SHA-256, and Content. +- **Rename:** From, To, Size, and SHA-256 of the source. The review is stale + once the source changed or the destination exists. +- **Delete:** File, Size, and SHA-256. The review is stale once the file + changed or is gone. Review reads only what the approval needs: - Only `command` and `file_write` approvals can be reviewed. - Opening a command approval changes nothing. -- Opening an edit approval makes one audited broker read of the target file, - under the same live `read` requirement as the tool, and writes one audit - record. It tries at most eight distinct candidate requests, newest first. +- Opening a file-write approval makes one audited broker read of the target + file (two for a rename, which also reads the destination), under the same + live `read` requirement as the tool, and writes one audit record per read. + It tries at most eight distinct candidate requests, newest first. It happens only when the operator opens that approval, never while listing. - Opening a record that is no longer pending reads nothing. - Review does not check that the approval is still unexpired. Core checks that diff --git a/docs/development.md b/docs/development.md index 0503dbf..89ab9ad 100644 --- a/docs/development.md +++ b/docs/development.md @@ -8,8 +8,9 @@ files/directories, and preview a bounded diff against trusted proposed content in a registered workspace. When a workspace enables `model_tools`, chat runs the core's bounded turn/tool runtime so the model can read and list files in that workspace; with `read` and `write` enabled it can request an -approval-gated exact edit or multi-edit of an existing file or the creation of -a new file, and with `execute`, `read`, and +approval-gated exact edit or multi-edit of an existing file, the creation of a +new file, the rename of a file, or the deletion of a file after a checkpoint, and +with `execute`, `read`, and `write` enabled it can request an approval-gated command. Trusted CLI apply can replace an existing file on Linux ext4 or Windows NTFS. Trusted restore puts a retained checkpoint's original bytes back through apply, and trusted checkpoint @@ -22,10 +23,10 @@ time. A program started by `run` can create, change, and delete workspace files without a checkpoint. The CLI's `file` commands cannot delete workspace files, and the CLI cannot use Git or connect to MCP servers. When a workspace enables `model_tools`, chat lets the local model read and list -files in that workspace, request an approval-gated exact edit, multi-edit, or -file creation with read/write, and request an approval-gated command with -execute/read/write. Model delete, rename, network, Git, and MCP tools are not -implemented. Trusted commands record and apply the Windows container +files in that workspace, request an approval-gated exact edit, multi-edit, file +creation, rename, or checkpointed deletion with read/write, and request an +approval-gated command with execute/read/write. Model network, Git, and MCP +tools are not implemented. Trusted commands record and apply the Windows container grants; see [command execution](execution.md). Version 0.29.0 also adds a prototype Tauri desktop shell, @@ -78,7 +79,24 @@ omgcode --data-dir /private/omgcode-data file restore WORKSPACE_UUID notes.txt - ``` A checkpoint restores only the workspace and exact path it recorded. A later -edit causes `checkpoint_conflict` without mutation. To free checkpoint capacity, +edit causes `checkpoint_conflict` without mutation. + +To get back a file that the model's approved `workspace_delete_file` removed, +name its checkpoint and add `--removed` instead of a current-file digest and +size. The file must not exist, and the restore recreates it through the +exclusive create: + +```text +omgcode --data-dir /private/omgcode-data file restore WORKSPACE_UUID notes.txt --session SESSION_UUID --checkpoint OPERATION_UUID --removed --writers-stopped +``` + +The output reports `restored_from`, and the checkpoint stays retained. An +existing entry of any name is `checkpoint_conflict` and is never replaced. The +recreated file takes the broker's default creation mode or inherited access +list; its former mode, owner, access list, and timestamps are not restored. No +new checkpoint is recorded, because nothing is destroyed. A replacement +checkpoint is refused with `--removed`, and a removal checkpoint is refused +without it. `--removed` cannot be combined with the digest and size options. To free checkpoint capacity, acknowledge any unresolved operation, then permanently delete one entry: ```text @@ -455,11 +473,12 @@ implementation, and `false` elsewhere. The CLI does not probe the mount, metadat or permissions for that flag. `checkpoint_restore` and `checkpoint_deletion` follow the same rule for `file restore` and `file delete-checkpoint`. `model_read_list_tools` is `true` on Linux and Windows, and `tool_policy` is -`read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_and_command_require_approval` +`read_list_when_workspace_model_tools_enabled_edit_multi_edit_create_rename_delete_and_command_require_approval` there. `model_write_tools` and `model_command_tool` are `approval_required`. None of them reports whether any workspace enables the preference. -`workspace_file_tools`, which covers general model file tools such as delete -or rename, remains `false`. +`workspace_file_tools`, which covers general model file tools beyond the +approval-gated edit, multi-edit, create, rename, and delete tools, remains +`false`. ```text omgcode --data-dir C:\omgcode-data file read WORKSPACE_ID --session SESSION_UUID src\main.rs --max-bytes 65536 @@ -510,7 +529,8 @@ trusted application-data directory, and the workspace ID. The runtime: - parses strict OpenAI-style function calls for `workspace_read_file`, `workspace_list_directory`, `workspace_edit_file`, - `workspace_multi_edit_file`, `workspace_create_file`, and + `workspace_multi_edit_file`, `workspace_create_file`, + `workspace_rename_file`, `workspace_delete_file`, and `workspace_run_command`; - rejects unknown tools, malformed or duplicate arguments, absolute or parent paths, and duplicate call identifiers before dispatch; @@ -530,17 +550,20 @@ trusted application-data directory, and the workspace ID. The runtime: - sends saved tool results from earlier turns again only while the live `model_tools` and `read` preferences both allow it, and otherwise replaces each with a fixed withheld notice; -- offers `workspace_edit_file`, `workspace_multi_edit_file`, and - `workspace_create_file` only under live `model_tools`, `read`, and `write`, +- offers `workspace_edit_file`, `workspace_multi_edit_file`, + `workspace_create_file`, `workspace_rename_file`, and + `workspace_delete_file` only under live `model_tools`, `read`, and `write`, and `workspace_run_command` only when `execute` is also enabled; every - edit, multi-edit, creation, or command first needs a trusted single-use - approval; + edit, multi-edit, creation, rename, deletion, or command first needs a + trusted single-use approval; - reports the final reply's provider `finish_reason`. -The runtime has no delete, rename, Git, MCP, or network tool. Its only -writes are the approved exact edit and multi-edit through checkpointed apply and -the approved creation of one new file through the exclusive create, and its only -process execution is the approved command through the trusted runner. +The runtime has no Git, MCP, or network tool. Its only writes are the approved +exact edit and multi-edit through checkpointed apply, the approved creation of +one new file through the exclusive create, the approved rename through the +no-replace rename, and the approved deletion through the checkpointed removal, +and its only process execution is the approved command through the trusted +runner. `LocalToolModel` adapts the LM Studio client to its model interface. The adapter sends the tool definitions in the OpenAI-compatible `tools` field, parses the response with the strict duplicate-key-rejecting parser, and returns each call @@ -580,8 +603,9 @@ remains text. When the workspace enables `model_tools`, chat offers `workspace_read_file` and `workspace_list_directory`. When `read` and `write` also allow it, it offers -the approval-gated `workspace_edit_file`, `workspace_multi_edit_file`, and -`workspace_create_file`. When `execute`, `read`, and `write` +the approval-gated `workspace_edit_file`, `workspace_multi_edit_file`, +`workspace_create_file`, `workspace_rename_file`, and `workspace_delete_file`. +When `execute`, `read`, and `write` also allow it, it offers the approval-gated `workspace_run_command`. The model may make up to four requests in one turn. Each read/list call is rechecked against live settings and stays inside the workspace. Results return to the model as @@ -725,6 +749,30 @@ file-operation journal's pending record for the trusted acknowledgement. Creatio is not checkpointed: there is no prior content to keep. The result reports the path, size, SHA-256, and operation ID, never the content. +`workspace_rename_file` takes `from` and `to`, which must differ. Before +recording an approval it reads `from` through the audited broker (at most +1 MiB, any bytes), requires `to` to be absent, and lists its parent: a missing +source or parent returns `not_found`, an existing destination returns +`file_exists`, a source over 1 MiB returns `edit_too_large`, and identical names +return `invalid_request`; none records an approval. After approval, the +broker pins the source, rechecks its size and SHA-256, and renames it without +ever replacing an entry. A destination that another writer created in the +meantime returns `file_exists` and the approval is spent. An uncertain outcome +is a terminal failure that leaves the file-operation journal's pending record +for the trusted acknowledgement. Renaming is not checkpointed: renaming back +reverses it. The result reports the old and new names, size, SHA-256, and +operation ID. + +`workspace_delete_file` takes a `path`. Before recording an approval it reads +the file as above, so a missing file returns `not_found` and one over 1 MiB +returns `edit_too_large`. After approval, the checkpointed removal checkpoints +the bytes, rechecks the file, and removes it. The result reports the path, +size, SHA-256, and the checkpoint's operation ID, which the trusted +`file restore --removed` takes to recreate the file. A file that changes after +approval conflicts and records no removal. An uncertain outcome leaves the +checkpoint operation unresolved until the trusted acknowledgement. Neither tool +detects a concurrent writer, and neither is power-loss qualified. + ## Saved sessions By default, journals use the platform's per-user local application data @@ -785,6 +833,7 @@ can archive, restore, and interrupt a session; see the | Journal | 8 MiB and 10,000 events | | Tool call arguments in a session event | 4 KiB | | Model edit and multi-edit file size, before and after | 1 MiB | +| Model rename and delete source size | 1 MiB | | Model multi-edit replacements per call | 16 | | Read file size per call | 15 KiB maximum, 8 KiB default | | Edit preview content | 1 MiB per side | diff --git a/docs/execution.md b/docs/execution.md index 65fa45d..6c82fd9 100644 --- a/docs/execution.md +++ b/docs/execution.md @@ -737,8 +737,9 @@ it writes only through the checkpointed apply in the [file-access contract](file-access.md#model-edit-route-adr-0043). Approving its record requires `approvals approve --writers-stopped`, which the command operation refuses. The `workspace_multi_edit_file` and `workspace_create_file` -tools (0.36.0, ADR-0054) use the same `file_write` records, each under its own -digest domain, and start no process either. +tools (0.36.0, ADR-0054) and the `workspace_rename_file` and +`workspace_delete_file` tools (0.38.0, ADR-0055) use the same `file_write` +records, each under its own digest domain, and start no process either. ### Toolchain profiles @@ -1121,11 +1122,12 @@ These remain true after C6: | C14 (0.25.0) | Approval-gated model command tool | Done: see [model command tool](#model-command-tool) and ADR-0040. A CLI acceptance test qualifies one successful approved command on both hosts; confinement correctness remains the platform suites' responsibility. | | C17 (0.28.0) | Approval-gated model edit tool | Done: see the [model edit route](file-access.md#model-edit-route-adr-0043) and ADR-0043. It starts no process; it writes one exact edit through the checkpointed apply after a writers-stopped approval. | | C18 (0.36.0) | Approval-gated model multi-edit and create tools | Done: see the [model multi-edit and create routes](file-access.md#model-multi-edit-and-create-routes-adr-0054) and ADR-0054. They start no process; multi-edit writes through the checkpointed apply and create through the broker's exclusive create, each after a writers-stopped approval. | +| C19 (0.38.0) | Approval-gated model rename and delete tools | Done: see the [model rename and delete routes](file-access.md#model-rename-and-delete-routes-adr-0055) and ADR-0055. They start no process; delete writes through a checkpointed removal and rename through a no-replace rename, each after a writers-stopped approval. | | C20 (0.37.0) | Registry credentials for the trusted fetch | Done: see [registry credentials](#registry-credentials) and ADR-0056. One bound secret per declared origin; no run receives it. | Later work needs its own decisions: - development-server networking; -- model delete, rename, network, Git, and MCP tools; +- model network, Git, and MCP tools; Live qualification on each supported OS remains a separate gate. diff --git a/docs/file-access.md b/docs/file-access.md index 49b0ffa..32ad8a2 100644 --- a/docs/file-access.md +++ b/docs/file-access.md @@ -174,7 +174,10 @@ Since 0.28.0 the approval-gated model edit tool reaches the same apply; see [Model edit route](#model-edit-route-adr-0043). Since 0.36.0 the model multi-edit tool reaches it too, and the model create tool reaches the exclusive create; see [Model multi-edit and create routes](#model-multi-edit-and-create-routes-adr-0054). -No model tool deletes, renames, restores, or manages checkpoints. +Since 0.38.0 the model delete tool reaches a checkpointed removal and the +model rename tool a no-replace rename; see +[Model rename and delete routes](#model-rename-and-delete-routes-adr-0055). No +model tool restores or manages checkpoints. ### Implemented record inspection (Phase A2a) @@ -209,15 +212,15 @@ The remaining action sequence is: | Action type | Payload and replay rule | | --- | --- | -| `replacement_started` | A unique operation ID, valid session/workspace IDs, nondecreasing policy `revision`, `target`, `expected`, `proposed`, and `artifacts`. No other operation may be unresolved. | +| `replacement_started` | A unique operation ID, valid session/workspace IDs, nondecreasing policy `revision`, `target`, `expected`, `proposed`, and `artifacts`, and optionally `kind`: `replace` (the default, never written) or `remove`. A `remove` proposes exactly the empty artifact. No other operation may be unresolved. | | `checkpoint_prepared` | Matching `operation_id` and exact `manifest` descriptor. Requires the intent phase. | -| `stage_prepared` | Matching `operation_id`, native `identity`, and `content` descriptor equal to the intent's proposed bytes/digest. Requires checkpoint preparation. | -| `commit_intended` | Matching `operation_id`. Requires stage preparation. | +| `stage_prepared` | Matching `operation_id`, native `identity`, and `content` descriptor equal to the intent's proposed bytes/digest. Requires checkpoint preparation. Invalid for a `remove`, which has no stage. | +| `commit_intended` | Matching `operation_id`. Requires stage preparation, or for a `remove` the prepared checkpoint. | | `replacement_finished` | Matching `operation_id`, `outcome`, and bounded non-success `code`. Success requires commit intent. Clean failure/conflict is accepted only before commit intent. Uncertainty remains unresolved. | | `replacement_acknowledged` | Matching unresolved `operation_id`. Clears only the recorded blocker; retains progress and outcome and never creates a success result. | | `checkpoint_deletion_started`, `checkpoint_deleted` | One resolved retained entry's deletion; see [Phase A4](#implemented-restore-and-checkpoint-deletion-phase-a4). | | `compacted` | Only as the second record. The replaced ledger's `previous` descriptor (nonempty, at most 64 MiB), `previous_records` (1 to 65,536), and the optional `revision_floor` that later starts must not go below. | -| `checkpoint_carried` | Only directly after `compacted` or another carried record. One retained entry's whole recorded state. Replay rebuilds it through the ordinary transitions above, so only a reachable state is accepted, and it must be resolved. Carried revisions are nondecreasing and at most the floor. See [ledger compaction](#implemented-ledger-compaction-phase-a5). | +| `checkpoint_carried` | Only directly after `compacted` or another carried record. One retained entry's whole recorded state, including its `kind` (never a `remove` with a stage identity). Replay rebuilds it through the ordinary transitions above, so only a reachable state is accepted, and it must be resolved. Carried revisions are nondecreasing and at most the floor. See [ledger compaction](#implemented-ledger-compaction-phase-a5). | Artifact descriptors have `bytes` and lowercase hexadecimal `sha256`. File payload claims are at most 1 MiB; a manifest claim is nonempty and at most @@ -529,8 +532,115 @@ size, SHA-256, and operation ID, never the content. Neither tool detects a concurrent writer, and neither is power-loss qualified. The desktop approvals window reproduces the digest from a recorded request of -any of the three tools before it shows fields. A create request also becomes -stale once its path exists. +any of the file-write tools before it shows fields. A create request also +becomes stale once its path exists. + +### Model rename and delete routes (ADR-0055) + +Both tools are advertised and dispatched only under the same live +`model_tools`, `read`, and `write` preferences as `workspace_edit_file`, with +the same per-call recheck and `edit_disabled` denial, in the order read, list, +edit, multi-edit, create, rename, delete, command (eight names, the existing +cap). Both use `file_write` approvals with the 900-second lifetime and the +writers-stopped acknowledgement, and the same lookup filters. Each has its own +digest domain tag (`omgcode-file-rename-v1`, `omgcode-file-delete-v1`), so a +record for one tool, including the three earlier file-write tools, can never be +consumed by another. Both read the live source through the audited broker +first, at most 1 MiB; a larger source is the recoverable `edit_too_large`, so +the model cannot rename or delete a file over 1 MiB. The bytes are not +interpreted, so a non-UTF-8 source is accepted. + +`workspace_rename_file` takes `from` and `to`. The names must differ after +normalization (`invalid_request`). Before an approval is recorded, `from` must +read (`not_found`), `to` must be absent (`file_exists`), and its parent must +list (`not_found`). The digest covers the session, workspace, both normalized +names, and the source's size and SHA-256, and the summary is `rename {from} -> +{to}: {n} bytes, sha256 {12}`. After approval, the handler checks the success +report fits, consumes the record, and calls `FileBroker::rename_file`. That +call requires both preferences from the live binding and no unresolved +checkpoint operation, records a `rename_file` intent that names the source and +the destination in the file-operation journal, and calls the platform's +`rename_no_replace`. A changed source is `checkpoint_conflict`, an existing +destination is `file_exists`, and an error after the native call may have +happened is uncertain: the audit record stays pending for the trusted +acknowledgement, and nothing is retried. Rename is not checkpointed: no content +is destroyed, and renaming back reverses it. A success reports both names, the +size, the SHA-256, and the operation ID. + +The native rename, on both platforms, opens both parents handle-relative +beneath the workspace without links or mount crossings, pins the source as a +regular single-link file, reads it through the pinned handle, and requires the +size and SHA-256 to match. Directories, links, junctions, reparse points, +hard-linked files, devices, and a source on another filesystem are refused before +any rename. Linux (a source owned by the effective user) re-stats the source name +against the pinned inode immediately before `renameat2` with +`RENAME_NOREPLACE`, requires the destination name to resolve to the pinned inode +afterwards, and flushes both parents. Windows opens the source with DELETE access +and sharing that denies every other writer and deleter, so the name cannot be +swapped or removed while the handle is held, and renames with +`FileRenameInformationEx` using POSIX semantics and no replace flag, so an +existing name fails with a collision status. It reopens the destination to +compare file IDs and flushes both parents; the flush carries the ADR-0023 +limitation. There is no filesystem qualification gate beyond the broker's +existing checks, as for create. + +`workspace_delete_file` takes `path`. The digest covers the session, workspace, +normalized path, and the file's size and SHA-256, and the summary is `delete +{path}: {n} bytes, sha256 {12}`. After approval, the handler checks the success +report fits, consumes the record, and calls `FileBroker::apply_removal`. It +runs the checkpointed apply's sequence without a stage: the live binding, both +preferences, checkpoint admission, the qualified filesystem (Linux ext4, +Windows NTFS), a capture whose size and SHA-256 must match, a durable checkpoint +of the original bytes under a `remove` record, a recheck that the target is +still the captured object, commit intent, one native removal attempt, and a +success record. A change before commit intent is `checkpoint_conflict` and +records a conflict. Any later error is uncertain: the operation stays +unresolved for the trusted acknowledgement, and nothing is retried, restored, +or cleaned up. The success message names the checkpoint's operation ID. + +The native removal is a sibling of the replacement commit. On Linux it +rechecks that the name still resolves to the pinned inode, then calls +`unlinkat` by name and requires the pinned inode's link count to be zero. The +call is by name, so a swap between the recheck and the call cannot be +prevented: a different file may then have been removed, and the check reports +it as uncertain. On Windows it opens the name with DELETE access, verifies that +same handle (file ID, volume, exact name, security, and every byte), and marks +that handle for POSIX-semantics deletion, so exactly the verified file is +deleted. It then requires the name to stop resolving and flushes the parent +(ADR-0023 limitation). + +**Ledger.** Each operation record has a `kind` of `replace` or `remove`. It is +omitted for `replace`, so every earlier ledger, carried record, and manifest +stays byte-identical and replays unchanged. For `remove`, the `proposed` +artifact must be exactly the empty artifact (0 bytes, the SHA-256 of the empty +string) and the empty `proposed` file keeps the fixed entry layout; there is no +stage, so a `stage_prepared` record is invalid and `commit_intended` follows +`checkpoint_prepared` directly; success still requires commit intent. A carried +`remove` with a stage identity or the stage step is refused, and compaction +carries `kind`. A `remove` entry holding a `stage` file fails inspection. A +`remove` entry's original need not be UTF-8, because nothing interprets it; +a `replace` entry's must be. Older binaries refuse a ledger that holds a +`remove` record, because they reject the unknown `kind` field. Acknowledgement +and checkpoint deletion work on `remove` entries unchanged. + +**Restore.** A `remove` checkpoint is restored by `FileBroker::restore_removed_file` +and the trusted `file restore --removed`, not by the apply path. The target must +not exist: the retained original is reread and compared with its record, then +written through the audited exclusive create, so an existing entry is +`checkpoint_conflict` and is never replaced. No operation is recorded, because +nothing is destroyed. Mode, owner, access list, and timestamps are not restored: +the new file gets the broker's default creation metadata. The checkpoint must be +for the same workspace and exact target. `file restore` of a replacement +checkpoint and `--removed` of a removal checkpoint are each refused as +`checkpoint_unavailable`. + +Neither tool detects a concurrent writer, and neither primitive is power-loss +qualified. The desktop approvals window reproduces the digest from a recorded +request of any of the five file-write tools (at most eight distinct candidates, +newest first) before it shows fields: `From`, `To`, `Size`, and `SHA-256` for +a rename, and `File`, `Size`, and `SHA-256` for a delete. A rename review is +stale once its source changed or its destination exists, and a delete review is +stale once the file changed or is gone. ### Private checkpoints, quota, and retention @@ -796,6 +906,14 @@ and create no operation. A missing target is refused. The output adds `restored_from` to the apply receipt. The ledger does not link the two operations, so a restore stays readable by 0.10.0. +Since 0.38.0, `file restore --removed` (without the current digest and size) +restores the checkpoint of a file that the model's approved deletion removed. +The target must not exist, and the retained original is written through the +broker's audited exclusive create instead of apply; an existing entry is +`checkpoint_conflict`. No operation is recorded, mode and access list are not +restored, and the output has `restored_from` and the creation receipt. See +[Model rename and delete routes](#model-rename-and-delete-routes-adr-0055). + `file delete-checkpoint` removes one resolved entry: an unresolved operation must be acknowledged first. It needs no workspace binding and opens no workspace file. It appends a synced `checkpoint_deletion_started` record. Then diff --git a/docs/platform-support.md b/docs/platform-support.md index d09b0df..8e796ee 100644 --- a/docs/platform-support.md +++ b/docs/platform-support.md @@ -22,7 +22,7 @@ containment. | User-project toolchains | Required; native installed/managed profile matrix not yet qualified | | Workspace directory identity | Configuration-only metadata check; required native ID and creation time must be available | | Workspace file broker | Bounded read/list/exclusive-create/single-directory creation through trusted CLI commands; release filesystem support is not qualified | -| Model file tools | Read and list, plus approval-gated exact-edit, multi-edit, and create tools (with `read` and `write`) and an approval-gated command tool (with `execute`, `read`, and `write`), through chat for a workspace that enables `model_tools`. Each edit, multi-edit, or creation needs an approval with the writers-stopped acknowledgement; edits and multi-edits are applied through the checkpointed apply on Linux ext4 or Windows NTFS, and a creation uses the broker's exclusive create without a checkpoint. Delete, rename, and general file tools are disabled and `workspace_file_tools` remains false | +| Model file tools | Read and list, plus approval-gated exact-edit, multi-edit, create, rename, and delete tools (with `read` and `write`) and an approval-gated command tool (with `execute`, `read`, and `write`), through chat for a workspace that enables `model_tools`. Each needs an approval with the writers-stopped acknowledgement. Edits, multi-edits, and deletions go through the checkpointed apply or removal on Linux ext4 or Windows NTFS; a creation uses the broker's exclusive create and a rename the no-replace rename (Linux `renameat2` with `RENAME_NOREPLACE`, Windows POSIX-semantics rename without replace), neither with a checkpoint. A Linux removal is by name and a swap is detected afterwards; a Windows removal is by the verified handle. The Linux rename and removal paths are not qualified on a Linux host. General file tools are disabled and `workspace_file_tools` remains false | | Process-tree runner | Called only by the trusted CLI `run`, through `run_confined`. Windows uses a Job Object assigned at creation and a per-workspace AppContainer. Linux uses a process group, seccomp, `close_range` (kernel 5.11 or later, x86_64 or aarch64), and Landlock ABI 3 or later. Tested on Windows 11 build 26300 and WSL 6.6 only ([execution](execution.md)) | | Tree limits | Every trusted run. Windows uses job memory and active-process limits and job CPU accounting. Linux needs a delegated cgroup v2 with `memory` and `pids` (Linux 5.14 or later for `cgroup.kill`), normally from a `systemd-run --user` scope, so it needs a user service manager. Disk growth is sampled by the supervisor on both platforms; it is not a filesystem quota. Tested on Windows 11 build 26300 and WSL 6.6 only ([tree limits](execution.md#tree-limits)) | | Package networking | Profiles are recorded on both platforms; no run accepts one, `--network` remains all-or-nothing, and no unprivileged destination filter exists on either platform. The trusted `packages fetch` command uses system TLS trust and a per-workspace cache root, and sends only a credential the operator bound to the matched origin (ADR-0056). The root is exposed read-only on Linux and Windows; Windows needs a read-execute container grant and traversable ancestors, and Linux falls back to application storage when no root is selected (ADR-0034, ADR-0035, ADR-0037) ([fetch](execution.md#trusted-package-fetch)) | diff --git a/docs/security-coding-rules.md b/docs/security-coding-rules.md index aace607..504fda8 100644 --- a/docs/security-coding-rules.md +++ b/docs/security-coding-rules.md @@ -153,9 +153,9 @@ evidence. Its checks do not isolate hostile same-user processes or provide process containment. Since 0.12.0, chat drives this runtime when the session's workspace enables `model_tools`. Since 0.28.0 the model writes only through -approval-gated tools: the exact edit (ADR-0043) and, since 0.36.0, the -multi-edit and create tools (ADR-0054). Model delete, rename, and -general file tools remain disabled; capability output keeps +approval-gated tools: the exact edit (ADR-0043), since 0.36.0 the multi-edit +and create tools (ADR-0054), and since 0.38.0 the rename and delete tools +(ADR-0055). General file tools remain disabled; capability output keeps `workspace_file_tools` false. For model tools: - Check the live `model_tools` preference before each call, then let the broker @@ -290,6 +290,22 @@ crate. exclusive create after consuming the approval; it never replaces, retries, or deletes, and leaves an uncertain outcome's pending record for the trusted acknowledgement. Keep file text out of summaries and results. See ADR-0054. +- The `workspace_rename_file` and `workspace_delete_file` model tools follow the + same advertisement, live-recheck, approval, and writers-stopped rules, under + their own digest domain tags (`omgcode-file-rename-v1`, + `omgcode-file-delete-v1`). Each reads its source live through the audited + broker (at most 1 MiB, bytes not interpreted) before recording an approval, + and binds the approval to that size and SHA-256 and to the normalized names. + A rename must find its destination absent and its parent present before an + approval, never replaces an entry, and is not checkpointed. A delete uses only + `FileBroker::apply_removal`, which checkpoints the original bytes first and + follows the checkpointed apply's gates, recheck, and uncertain-outcome rules. + The native rename must pin the source, verify its bytes through the pinned + handle, and use a no-replace primitive; the native removal must verify the + object it removes (a verified handle on Windows, the pinned inode's link count + afterwards on Linux). Neither retries, repairs, or deletes after an uncertain + outcome, and neither reports concurrent-writer detection. Keep file text and + full paths out of results. See ADR-0055. - The `workspace_run_command` model tool may be advertised only under live `model_tools`, `execute`, `read`, and `write`. Its first call records a pending approval and starts nothing; only a later exact call may consume an diff --git a/docs/session-storage.md b/docs/session-storage.md index 4c3d9c7..9a3d9cf 100644 --- a/docs/session-storage.md +++ b/docs/session-storage.md @@ -119,7 +119,8 @@ settings. The journal's recorded workspace ID is only a coherence check. The runtime supports `workspace_read_file` and `workspace_list_directory`, plus approval-gated tools offered only under their live settings: `workspace_edit_file` (ADR-0043), `workspace_multi_edit_file` and -`workspace_create_file` (ADR-0054), and `workspace_run_command` (ADR-0040). It parses strict OpenAI-style function calls, +`workspace_create_file` (ADR-0054), `workspace_rename_file` and +`workspace_delete_file` (ADR-0055), and `workspace_run_command` (ADR-0040). It parses strict OpenAI-style function calls, rejects unknown tools, malformed or duplicate arguments, absolute or parent paths, and duplicate call identifiers before dispatch. For each operation it opens the control store, obtains a fresh session binding, releases the settings @@ -152,10 +153,10 @@ Every tool result is recorded first as `tool.requested`, then `tool.completed`, then a `message.recorded` entry with role `tool` and source `core_tool`. The tool text starts with a source label and remains user-level, untrusted content when the next request is assembled. Model-requested text -cannot become a system message. Read and list run directly. An edit or a -command runs only after a matching single-use record that the operator -approved; there are no general file write, create, delete, or rename tools, -and Git, MCP, and network access remain unavailable. The separate +cannot become a system message. Read and list run directly. An edit, a +multi-edit, a create, a rename, a delete, or a command runs only after a +matching single-use record that the operator approved; there are no general +file write tools, and Git, MCP, and network access remain unavailable. The separate [file-operation journal](file-access.md) remains the broker's operation ledger. The runtime does not redact by itself. The caller must redact user text and diff --git a/docs/testing-standards.md b/docs/testing-standards.md index 7b8cdb5..90e4eb3 100644 --- a/docs/testing-standards.md +++ b/docs/testing-standards.md @@ -190,6 +190,7 @@ As the relevant components are implemented, include: | Model command tool | Strict profile/program/argument/cwd parsing, tool advertised only with live model-tools/execute/read/write, first call records a pending approval and starts nothing, denial, approval consumption, exact digest and binding, settings-revision invalidation, run hand-off failure, output bounds, no replay, and a durable `model_tools` revocation count that stops a model-origin run even after re-enable. An end-to-end CLI case drives the real chat process, a mock provider, an operator approval, and the trusted runner to a successful confined command on both hosts | | Model edit tool | Strict path/`old_text`/`new_text` parsing with unknown-field, empty-`old_text`, duplicate-key, absolute, and parent-path refusal; the tool advertised and dispatched only with live model-tools/read/write and denied before any file access otherwise; first call records a pending `file_write` approval and writes nothing; a summary with sizes and short digests but no file text or control characters; denial; approval consumption and one checkpointed apply; a further identical call after success needing a new approval; a same-size changed file and a same-size different replacement each needing a new approval; recoverable no-match, ambiguous (including overlaps), unchanged, non-UTF-8, oversized, too-large, and pending-checkpoint results; `session inspect` listing the exact request; schema-0.2.0 replay. A CLI case drives the real chat process, a mock provider, and operator approval to an applied edit and a recorded checkpoint ([ADR-0043](adr/0043-gate-the-model-file-edit-tool-with-approvals-and-checkpointed-apply.md)) | | Model multi-edit and create tools | Strict parsing of `edits` (zero and 17 entries, empty `old_text`, unknown or duplicate keys at both levels, wrong types) and of `content`; both tools advertised and dispatched only with live model-tools/read/write, in the documented order, and denied before any file access otherwise; first call records a pending `file_write` approval and writes nothing; summaries with sizes and short digests but no text; denial; approval consumption and one checkpointed apply for a multi-edit (edits listed out of offset order, touching matches) and one exclusive create with no checkpoint; a changed file or different request needing a new approval; an `workspace_edit_file` approval never consumed by an equivalent multi-edit and the reverse, and a create approval never authorizing an edit; recoverable no-match and ambiguous (naming the edit index), overlap, unchanged, non-UTF-8, too-large, `file_exists`, and missing-parent results that record no approval; an output-too-large refusal that keeps the approval; schema-0.2.0 and 0.3.0 replay; desktop review fields, the shared eight-candidate bound across the three tools, stale review after a change or once a created path exists, and hostile text escaped. CLI cases drive the real chat process, a mock provider, and operator approval for each tool ([ADR-0054](adr/0054-add-multi-edit-and-create-model-file-tools.md)) | +| Model rename and delete tools | Strict parsing of `from`/`to` and `path` (missing, extra, wrong-type, absolute, parent, and empty values); both tools advertised and dispatched only with live model-tools/read/write, in the documented order with the eight-name cap fully used, and denied before any file access otherwise; first call records a pending `file_write` approval and writes nothing; summaries with sizes and short digests but no text; denial; approval consumption and one no-replace rename with no checkpoint or one checkpointed removal; a changed source, a different destination, or another tool's approval needing a new approval; recoverable `not_found`, `file_exists`, missing-parent, `edit_too_large`, and same-name results that record no approval; non-UTF-8 files accepted; an output-too-large refusal that keeps the approval; schema-0.4.0 replay. Platform cases refuse existing destinations, directories, links and junctions, and hard links, refuse a swapped or changed source, delete only the verified file, report a swapped name as uncertain on Linux, and inject faults before and after each native call. Ledger cases cover the `remove` kind: replay, every refusal (proposed bytes, a stage record, a carried stage identity), inventory without a stage, compaction, deletion of the entry, restore of the exact bytes including non-UTF-8, and refusal of an existing target. Review cases cover exact fields, stale destination or source, and hostile path text. CLI cases drive the real chat process, a mock provider, operator approval, and `file restore --removed` ([ADR-0055](adr/0055-add-checkpointed-delete-and-no-replace-rename-model-tools.md)) | | MCP | Version/transport compatibility, authorization, untrusted content, local confinement, OAuth/redirects, cancellation | | LM Studio | Model discovery, token/no-token profiles, streamed tools, context limits, unavailable models, no hosted-tool bypass | | Instructions | Compiled base/UI/request match, runtime replacement denial, role-injection attempts, preference/task precedence, revisions, safe rendering | diff --git a/docs/workspace-permissions.md b/docs/workspace-permissions.md index 412d4ca..e1a7600 100644 --- a/docs/workspace-permissions.md +++ b/docs/workspace-permissions.md @@ -93,8 +93,8 @@ it runs. A change that withdraws one stops the running tree, which is recorded as `revoked`. The `permissions` command reports success only after the runs it affects have stopped; see [settings changes during a run](execution.md#settings-changes-during-a-run). Per-target grants remain -required. Single-use approvals now exist for the model edit, multi-edit, create, and -command tools: +required. Single-use approvals now exist for the model edit, multi-edit, create, +rename, delete, and command tools: the trusted CLI and the desktop approvals window decide them. Workspace settings still change only through the trusted CLI; the desktop shell has no settings interface. @@ -334,6 +334,18 @@ one never authorizes another. Disabling `write` or `read` removes all three. See [model multi-edit and create routes](file-access.md#model-multi-edit-and-create-routes-adr-0054) and [ADR-0054](adr/0054-add-multi-edit-and-create-model-file-tools.md). +The same three preferences also offer `workspace_rename_file`, which moves one +file to a name that does not exist and never replaces an entry, and +`workspace_delete_file`, which removes one file after a checkpoint of its bytes. +Both refuse a source over 1 MiB. They share the edit tool's `file_write` +approvals, writers-stopped acknowledgement, per-call recheck, and +`edit_disabled` denial, and each has its own digest domain. Renaming is not +checkpointed; a deleted file is restored with the trusted +`file restore --removed`. Disabling `write` or `read` removes all five file-write +tools. See +[model rename and delete routes](file-access.md#model-rename-and-delete-routes-adr-0055) +and [ADR-0055](adr/0055-add-checkpointed-delete-and-no-replace-rename-model-tools.md). + When `model_tools`, `execute`, `read`, and `write` are all enabled, the runtime also offers `workspace_run_command`. Its first call records a pending approval and does not run; after `approvals approve `, a second identical call @@ -341,10 +353,11 @@ consumes the approval and starts the trusted run. The model cannot approve. The tool uses fixed limits and no network or host services. See [model command tool](execution.md#model-command-tool) and [ADR-0040](adr/0040-gate-the-model-command-tool-with-approvals-and-the-trusted-runner.md). -The tool schema version is 0.4.0, which added `workspace_multi_edit_file` and -`workspace_create_file`; 0.3.0 added `workspace_edit_file`. Each request -manifest records the version it advertised, and sessions that recorded 0.2.0 or -0.3.0 still replay and accept new turns. +The tool schema version is 0.5.0, which added `workspace_rename_file` and +`workspace_delete_file`; 0.4.0 added `workspace_multi_edit_file` and +`workspace_create_file`, and 0.3.0 added `workspace_edit_file`. Each request +manifest records the version it advertised, and sessions that recorded 0.2.0, +0.3.0, or 0.4.0 still replay and accept new turns. `workspace status` always reports `model_tools`. Persisted settings omit the field while it is `false`, so settings that never enable it keep their exact From 1fc2b7f456c247bbbffeead447209f53ae9b6ad6 Mon Sep 17 00:00:00 2001 From: James Chapman Date: Tue, 17 Nov 2026 23:05:59 +0000 Subject: [PATCH 4/4] test: keep fixture secrets out of credential validation messages CodeQL flagged cleartext logging: the validation test loops printed the secret bytes and the validation result in their failure messages. They now name only the case index and auth scheme. Co-Authored-By: Claude Opus 5.5 --- .../src/control/package/credential/tests.rs | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/crates/omgcode-core/src/control/package/credential/tests.rs b/crates/omgcode-core/src/control/package/credential/tests.rs index eb43e81..d36ddec 100644 --- a/crates/omgcode-core/src/control/package/credential/tests.rs +++ b/crates/omgcode-core/src/control/package/credential/tests.rs @@ -258,7 +258,9 @@ fn refused_bindings_store_nothing_and_record_nothing() { /// bearer without any space, basic with a nonempty user and a colon. #[test] fn invalid_secrets_are_refused_before_anything_is_stored() { - for (auth, bytes) in [ + // Failure messages name the case index and scheme only: secret bytes and + // validation results never reach test output. + for (case, (auth, bytes)) in [ (PackageAuth::Bearer, b"tok\ten".as_slice()), (PackageAuth::Bearer, b"tok\ren"), (PackageAuth::Bearer, b"tok\nen"), @@ -274,21 +276,27 @@ fn invalid_secrets_are_refused_before_anything_is_stored() { (PackageAuth::Basic, b"alice:pw\n"), (PackageAuth::Basic, b"alice:pw "), (PackageAuth::Basic, "alice:p\u{e9}".as_bytes()), - ] { - let result = validate_package_secret(auth, &secret(bytes)); - assert!( - matches!(result, Err(ControlError::PackageProfileRefused(_))), - "{auth:?} {bytes:?}: {result:?}" + ] + .into_iter() + .enumerate() + { + let refused = matches!( + validate_package_secret(auth, &secret(bytes)), + Err(ControlError::PackageProfileRefused(_)) ); + assert!(refused, "invalid case {case} ({auth:?}) was not refused"); } - for (auth, bytes) in [ + for (case, (auth, bytes)) in [ (PackageAuth::Bearer, b"abc.DEF_123-~+/=".as_slice()), (PackageAuth::Basic, b"alice:pass word"), (PackageAuth::Basic, b"a:"), (PackageAuth::Basic, b"a:b:c"), - ] { - validate_package_secret(auth, &secret(bytes)) - .unwrap_or_else(|error| panic!("{auth:?} {bytes:?}: {error}")); + ] + .into_iter() + .enumerate() + { + let accepted = validate_package_secret(auth, &secret(bytes)).is_ok(); + assert!(accepted, "valid case {case} ({auth:?}) was refused"); } // The largest accepted secret is valid; one more byte is refused by the // secret type itself.