Root cause: two proven corruption mechanisms — (M1) non-idempotent apply stamped the same block N times when a quantized model re-emitted the same edit_file call or a turn was retried; (M2) Levenshtein tier 4 was fail-open with no uniqueness guard, silently splicing into the wrong location. Fixes applied at every layer of the pipeline: Matcher (fuzzy-match.ts): raise SIMILARITY_THRESHOLD 0.66 → 0.85; add AMBIGUITY_EPSILON uniqueness guard — two windows within 0.05 of the top score → ambiguous, not a guess; add block-anchor gate (≥3-line needles require first+last line exact match before a window is scored). Edit planner (pending_changes.ts): extract planEdit() as a pure function; idempotency guards detect already-applied states (anchored insert re-stamp, old-gone-but-new-present); findPendingDuplicate() collapses identical pending rows at queue time so M1 never reaches applyOne. Atomic writes (pending_changes.ts): temp-file + rename on the same filesystem so a crash can't leave a half-written source file; realpath() first so symlinks survive the rename. Per-file mutex (pending_changes.ts): withFileLock() serializes concurrent read-modify-write on the same path via a chained-Promise Map. EOL preservation (pending_changes.ts): normalize CRLF → LF for matching, restore native line ending on write so Windows-style files stay clean. Context isolation (inference_context.ts): replace module-level singleton with AsyncLocalStorage so concurrent inference runs (arena parallel dispatch, dispatcher poll racing a user message) each get their own scoped context with no clobbering. Tests: plan-edit.test.ts (pure planEdit unit tests), extended fuzzy-match and pending_changes_integration suites, ALS isolation test that proves overlapping runs get correct session IDs. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { z } from 'zod';
|
|
import type { ToolDef, ToolContext } from './types.js';
|
|
import { queueCreate } from '../pending_changes.js';
|
|
import { denyReadOnly, finalizeWrite } from './write-gate.js';
|
|
|
|
const CreateFileInput = z.object({
|
|
file_path: z.string().min(1),
|
|
content: z.string(),
|
|
});
|
|
type CreateFileInputT = z.infer<typeof CreateFileInput>;
|
|
|
|
export const createFileTool: ToolDef<CreateFileInputT> = {
|
|
name: 'create_file',
|
|
description:
|
|
'Queue creation of a new file with the given content. ' +
|
|
'The change is staged in pending_changes and must be applied explicitly.',
|
|
inputSchema: CreateFileInput,
|
|
jsonSchema: {
|
|
type: 'function',
|
|
function: {
|
|
name: 'create_file',
|
|
description:
|
|
'Queue creation of a new file with the given content. ' +
|
|
'The change is staged in pending_changes and must be applied explicitly.',
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
file_path: { type: 'string', description: 'Path for the new file (relative to project root or absolute)' },
|
|
content: { type: 'string', description: 'Full content of the file to create' },
|
|
},
|
|
required: ['file_path', 'content'],
|
|
},
|
|
},
|
|
},
|
|
async execute(input: CreateFileInputT, projectRoot: string, context: ToolContext): Promise<unknown> {
|
|
if (context.permissionMode === 'plan') return denyReadOnly('create_file');
|
|
const change = await queueCreate(
|
|
context.sql,
|
|
context.sessionId,
|
|
context.taskId,
|
|
input.file_path,
|
|
input.content,
|
|
projectRoot,
|
|
);
|
|
return finalizeWrite(
|
|
context,
|
|
projectRoot,
|
|
change,
|
|
`File creation queued: ${change.file_path}. Use apply_pending to write changes to disk.`,
|
|
);
|
|
},
|
|
};
|