Add deterministic JSON-driven file editor (token-free)#56
Merged
Conversation
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
There was a problem hiding this comment.
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.mjswith a pure text op engine and a filesystem executor. - Adds
simplicio-editCLI and wires it intopackage.jsonexports/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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 puroapplyOps(text, ops)(string → string) + camada de filesystemapplyEditScript(script, { dryRun, cwd }).replace,regexReplace,insertAfter,insertBefore,insertAtLine,deleteLine,deleteMatching,append,prepend.create(com guarda deoverwrite) edelete.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 aotest: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— binsimplicio-edit+ export./json-file-editor.Segurança por padrão
findsem match é erro (a menos queoptional: true).replaceambíguo (>1 ocorrência) falha semall: true.createnão sobrescreve semoverwrite: true.--dry-runpré-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