feat(coder): add edit safety guards against truncation

This commit is contained in:
2026-06-07 17:57:15 +00:00
parent 9106334e70
commit 373ba86e5d
2 changed files with 47 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import { randomBytes } from 'node:crypto';
import type { Sql } from '../db.js';
import { resolveWritePath } from './write_guard.js';
import { locateMatch } from './fuzzy-match.js';
import { validateEditResult, formatGuardError } from './edit-guards.js';
/**
* Write a file atomically: stage to a sibling temp file, then rename over the
@@ -285,6 +286,10 @@ export async function applyOne(
);
}
if (plan.kind === 'apply') {
const guard = validateEditResult(toLf(raw), plan.updated, change.file_path);
if (!guard.ok) {
throw new Error(formatGuardError(guard, change.file_path));
}
const out = eol === '\r\n' ? plan.updated.replaceAll('\n', '\r\n') : plan.updated;
await writeFileAtomic(change.file_path, out);
} else {