ADR-0298:
Atomic Multi-File Edit + Auto-Fix + Impacted-Test
(tenon batch)
- Status: Accepted
- Date: 2026-11-28
- Wave / Idea: Wave 1 · Rank 16 · Idea #46
- Diagnostic Codes:
E1175–E1179
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:
Multi-File Transaction Schema (
tenon-batch-1):- Reads a JSON object from stdin or
--file <path>containingops(and optionalroot,commit_on_test_fail). - Supports six operation types:
write,replace_fn,patch,insert,rename, andadd_plugin. - Every operation accepts an optional
expect_hashoptimistic concurrency precondition (matching either the 16-hexoutline::hashor 64-hex file SHA-256).
- Reads a JSON object from stdin or
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
.tnfiles, checking, auto-fixing, dependency analysis, andinterpunit tests run 100% in memory viamodules::load_with_overrides. - When a transaction creates brand-new
.tnfiles or modifiestenon.toml(e.g., viaadd_plugin), verification mirrors the workspace into an isolated temporary directory understd::env::temp_dir(), refreshestenon.lockoffline viaPackages::fetch_offlinewhentenon.tomlchanged, and stages any lockfile/fix updates back into the overlay — preserving 0 dirty bytes in the target workspace on failure.
- All operations execute against an in-memory
Checked Auto-Fix + Package Typecheck + Impacted
interpTests:- Runs
fix::fix_overlay(the multi-pass checked auto-fix engine fromcrates/tenon/src/fix/mod.rs) across the staged overlay. - Typechecks the root package and any standalone staged
.tnfiles. - Builds the dependency graph across the staged overlay
(
deps::build_with_overrides), computes transitively impactedtestitems (deps::impacted_tests), and executes only those impacted unit tests ininterp(in-memory, no C compilation).
- Runs
Atomic Publication &
harness_verify-Compatible Delta Diagnostics:- On green (or when
--commit-on-test-failis set and typechecking succeeded), commits all modified existing files atomically viaedit::write_batch(directory-lockedrenameat_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 (E1178without--commit-on-test-fail), leaves 0 dirty bytes on disk and emits structuredtenon-batch-1JSON containing normalized diagnostics and aharness_verify-compatibledelta(introduced,fixed,pre_existing_count,summaryusing 16-line bucket stability).
- On green (or when
Reserved Diagnostic Codes:
E1175: invalid batch CLI usage, I/O error, or malformed transaction JSON.E1176:expect_hashprecondition 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
- Agents complete multi-file refactors (struct + callers + tests
+ plugins) in a single atomic tool call with automatic checked
fixes and millisecond
interpverification. - Failed refactors never leave partial or broken
.tnfiles on disk. tenon-dev-mcpexposestenon.batchunderEffect.Write, returning structuredtenon-batch-1JSON directly on both green commits and typecheck/test aborts.