Skip to content

Add deterministic JSON-driven file editor (token-free)#56

Merged
wesleysimplicio merged 1 commit into
mainfrom
claude/fastest-file-editing-escrM
Jun 2, 2026
Merged

Add deterministic JSON-driven file editor (token-free)#56
wesleysimplicio merged 1 commit into
mainfrom
claude/fastest-file-editing-escrM

Conversation

@wesleysimplicio
Copy link
Copy Markdown
Owner

O que é

Responde à pergunta "qual é a forma mais rápida e econômica de editar um arquivo?" com uma implementação concreta: edição determinística guiada por JSON, sem tokens de LLM.

A ideia: o LLM (ou um script, ou você) decide o que mudar e emite um pequeno patch JSON; um executor determinístico aplica esse patch — sem chamada de modelo, sem tokens, em microssegundos, e 100% reproduzível. O modelo nunca precisa reescrever o arquivo inteiro.

O que entra

  • utils/json-file-editor.mjs — motor puro applyOps(text, ops) (string → string) + camada de filesystem applyEditScript(script, { dryRun, cwd }).
    • Ops de texto: replace, regexReplace, insertAfter, insertBefore, insertAtLine, deleteLine, deleteMatching, append, prepend.
    • Ops de arquivo: create (com guarda de overwrite) e delete.
  • bin/simplicio-edit.mjs — CLI: lê de arquivo ou stdin, --dry-run, --json, --cwd. Exit codes 0/1/2.
  • tests/json-file-editor.test.mjs — 18 testes, integrados ao test:node.
  • docs/json-file-editor.md — referência completa + a conta de custo (JSON vs LLM).
  • examples/node/json-edit.mjs — exemplo executável.
  • package.json — bin simplicio-edit + export ./json-file-editor.

Segurança por padrão

  • Sem adivinhação: find sem match é erro (a menos que optional: true).
  • replace ambíguo (>1 ocorrência) falha sem all: true.
  • create não sobrescreve sem overwrite: true.
  • Newlines (CRLF/LF) preservadas.
  • --dry-run pré-visualiza sem tocar no disco.

Testes

npm run test:node — toda a suíte passa (incluindo os 18 novos testes). Exemplo e CLi validados ponta a ponta.

https://claude.ai/code/session_01ThmKaDugQ5X7H1pFZS5MbK


Generated by Claude Code

Implements a zero-token, reproducible file-editing engine: an LLM (or
script) emits a small JSON patch describing what to change, and a
deterministic executor applies it without any model call.

- utils/json-file-editor.mjs: pure applyOps engine + applyEditScript fs
  layer with dryRun; ops: replace, regexReplace, insert{After,Before,AtLine},
  delete{Line,Matching}, append, prepend, plus create/delete files.
- bin/simplicio-edit.mjs: CLI (file or stdin, --dry-run, --json, --cwd).
- tests/json-file-editor.test.mjs: 18 tests, wired into test:node.
- docs/json-file-editor.md + examples/node/json-edit.mjs.
- package.json: simplicio-edit bin + ./json-file-editor export.

Safety by default: no guessing on missing matches, ambiguous replace
fails without all:true, create won't clobber without overwrite, newlines
preserved.

https://claude.ai/code/session_01ThmKaDugQ5X7H1pFZS5MbK
@wesleysimplicio wesleysimplicio marked this pull request as ready for review June 2, 2026 04:16
Copilot AI review requested due to automatic review settings June 2, 2026 04:16
@wesleysimplicio wesleysimplicio merged commit 4579fbf into main Jun 2, 2026
3 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a deterministic JSON-driven editing engine plus a CLI wrapper to apply edits to files without LLM calls, along with docs, examples, and tests.

Changes:

  • Introduces utils/json-file-editor.mjs with a pure text op engine and a filesystem executor.
  • Adds simplicio-edit CLI and wires it into package.json exports/bin/scripts.
  • Adds docs, a runnable Node example, and a Node test suite for the editor.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
utils/json-file-editor.mjs New deterministic edit engine (pure ops + filesystem script executor).
tests/json-file-editor.test.mjs New Node tests covering core ops, parsing, and filesystem behaviors.
package.json Exposes the module via package exports, adds new CLI bin, and runs the new tests.
examples/node/json-edit.mjs Demonstrates applying an edit script with dry-run + apply.
docs/json-file-editor.md Documents script format, ops, and new CLI usage.
bin/simplicio-edit.mjs New CLI entrypoint to apply edit scripts from file/stdin with reporting.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +99 to +102
const missing = (what) => {
if (optional) return { text, changed: false };
throw new EditError(`${op.op}: ${what} not found: ${JSON.stringify(op.find)}`, ctx);
};
throw new EditError(`deleteLine requires a 1-based integer "line"`, ctx);
}
const lines = splitLines(text);
if (op.line > lines.length) return missing(`line ${op.line}`);
});
});

test("multi-file script applies atomically in order", () => {
*
* Demonstrates the cost argument behind it: an LLM may emit the small JSON
* patch below once, but applying it costs zero tokens and runs in microseconds.
* Re-running it is idempotent-safe — a missing match is reported, never guessed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants