On this page

ADR-0298: Atomic Multi-File Edit + Auto-Fix + Impacted-Test (tenon batch)

Context

Multi-file refactors in Tenon (for example: adding a field to a struct in model.tn, updating callers in caller.tn, and updating assertions in main.tn or tests.tn) previously required sequential single-file commands (tenon write --no-check, tenon edit replace-fn, tenon fix --apply, tenon check, tenon test). Because intermediate states between files do not typecheck, agents had to disable checking mid-refactor, risking partial dirty writes on disk and paying multiple round-trip tool calls.

Decision

Introduce a single atomic transaction command tenon batch [--file <tx.json>] [--root <main.tn>] [--commit-on-test-fail] [--json] (implemented in crates/tenon/src/edit/batch.rs) and expose it over MCP as tenon.batch in tenon-dev-mcp:

  1. Multi-File Transaction Schema (tenon-batch-1):

    • Reads a JSON object from stdin or --file <path> containing ops (and optional root, commit_on_test_fail).
    • Supports six operation types: write, replace_fn, patch, insert, rename, and add_plugin.
    • Every operation accepts an optional expect_hash optimistic concurrency precondition (matching either the 16-hex outline::hash or 64-hex file SHA-256).
  2. In-Memory Overlay VFS Staging:

    • All operations execute against an in-memory BTreeMap<PathBuf, StagedFile> without writing to the target workspace on disk.
    • When modifying existing .tn files, checking, auto-fixing, dependency analysis, and interp unit tests run 100% in memory via modules::load_with_overrides.
    • When a transaction creates brand-new .tn files or modifies tenon.toml (e.g., via add_plugin), verification mirrors the workspace into an isolated temporary directory under std::env::temp_dir(), refreshes tenon.lock offline via Packages::fetch_offline when tenon.toml changed, and stages any lockfile/fix updates back into the overlay — preserving 0 dirty bytes in the target workspace on failure.
  3. Checked Auto-Fix + Package Typecheck + Impacted interp Tests:

    • Runs fix::fix_overlay (the multi-pass checked auto-fix engine from crates/tenon/src/fix/mod.rs) across the staged overlay.
    • Typechecks the root package and any standalone staged .tn files.
    • Builds the dependency graph across the staged overlay (deps::build_with_overrides), computes transitively impacted test items (deps::impacted_tests), and executes only those impacted unit tests in interp (in-memory, no C compilation).
  4. Atomic Publication & harness_verify-Compatible Delta Diagnostics:

    • On green (or when --commit-on-test-fail is set and typechecking succeeded), commits all modified existing files atomically via edit::write_batch (directory-locked renameat_with(EXCHANGE) with rollback) and publishes any new files via sibling temp-file rename.
    • On precondition failure (E1176), staging/typecheck failure (E1177), or test failure (E1178 without --commit-on-test-fail), leaves 0 dirty bytes on disk and emits structured tenon-batch-1 JSON containing normalized diagnostics and a harness_verify-compatible delta (introduced, fixed, pre_existing_count, summary using 16-line bucket stability).
  5. Reserved Diagnostic Codes:

    • E1175: invalid batch CLI usage, I/O error, or malformed transaction JSON.
    • E1176: expect_hash precondition mismatch (edit_stale_hash).
    • E1177: operation staging or package typecheck failure.
    • E1178: impacted unit test failure (aborted unless --commit-on-test-fail).
    • E1179: atomic commit failure (e.g., concurrent external modification).

Consequences