| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001 |
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
- import { mkdtemp, rm } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import {
- createFileSystemError,
- createRootTraceContext,
- createSessionId,
- createToolCallId,
- createTurnId,
- modelMessageContentToText,
- type FileSystemLineEndings,
- type FileSystemPort,
- type FileSystemReadTextRangeResult,
- type FileSystemReadTextResult,
- type FileSystemRevision,
- type FileSystemSearchFilesResult,
- type FileSystemSearchTextResult,
- type FileSystemStatResult,
- type FileSystemWriteTextRequest,
- type ModelMessageContent,
- type PermissionRuleset,
- } from "@zcode/contracts";
- import { PermissionService, defaultPermissionConfig } from "../src/permission/service.js";
- import { createToolExecutor } from "../src/tool/executor.js";
- import { writeHandler, writeToolEntry } from "../src/tool/handlers/write.js";
- import { createReadFileStateKey } from "../src/tool/read-file-state.js";
- import { createToolRegistry } from "../src/tool/registry.js";
- import type { ReadFileStateMap, ToolExecutionContext } from "../src/tool/types.js";
- const RUN_WRITE_STRICT_CONTRACT = process.env.ZCODE_RUN_WRITE_STRICT_CONTRACT === "1";
- const describeWriteStrictContract = RUN_WRITE_STRICT_CONTRACT ? describe : describe.skip;
- const WRITE_STORAGE_SUFFIX =
- " (file state is current in your context \u2014 no need to Read it back)";
- const WRITE_USER_MODIFIED_NOTE = " The user modified your proposed content before accepting it.";
- let tmpDir: string;
- beforeEach(async () => {
- tmpDir = await mkdtemp(join(tmpdir(), "zcode-write-contract-"));
- });
- afterEach(async () => {
- await rm(tmpDir, { recursive: true, force: true });
- });
- describe("Write model-result harness", () => {
- it("returns executor modelContent and serialization for create results", async () => {
- const file = join(tmpDir, "nested", "created.txt");
- const fs = createMemoryFileSystem({});
- const result = await executeWrite(
- {
- file_path: file,
- content: "alpha\nbeta\n",
- },
- fs,
- );
- expect(result.success).toBe(true);
- expect(result.output).toEqual({
- type: "create",
- filePath: file,
- content: "alpha\nbeta\n",
- structuredPatch: [],
- originalFile: null,
- userModified: false,
- });
- expect(asModelText(result.modelContent)).toBe(
- `File created successfully at: ${file}${WRITE_STORAGE_SUFFIX}`,
- );
- expect(result.serialization?.content).toBe(
- `File created successfully at: ${file}${WRITE_STORAGE_SUFFIX}`,
- );
- expect(fs.files.get(file)).toBe("alpha\nbeta\n");
- expect(fs.readPaths).toEqual([file]);
- });
- it("documents update model content for existing nonempty files", async () => {
- const file = join(tmpDir, "existing.txt");
- const fs = createMemoryFileSystem({
- [file]: "old line\nkept line\n",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "old line\nkept line\n"),
- });
- const result = await writeForModel(
- {
- file_path: file,
- content: "new line\nkept line\n",
- },
- context,
- );
- expect(result.output).toMatchObject({
- type: "update",
- filePath: file,
- content: "new line\nkept line\n",
- originalFile: "old line\nkept line\n",
- userModified: false,
- });
- expect(result.output.structuredPatch[0]?.lines).toContain("-old line");
- expect(result.output.structuredPatch[0]?.lines).toContain("+new line");
- expect(asModelText(result.modelContent)).toBe(
- `The file ${file} has been updated successfully.${WRITE_STORAGE_SUFFIX}`,
- );
- expect(fs.files.get(file)).toBe("new line\nkept line\n");
- });
- it("adds Write performance attribution without exposing file content", async () => {
- const file = join(tmpDir, "perf-existing.txt");
- const fs = createMemoryFileSystem({
- [file]: "old line\n",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "old line\n"),
- });
- const result = await writeForModel(
- {
- file_path: file,
- content: "new line\nextra\n",
- },
- context,
- );
- expect(result.output).toMatchObject({
- type: "update",
- perf: {
- detail: {
- kind: "filesystem",
- filesystem: {
- readMs: expect.any(Number),
- writeMs: expect.any(Number),
- fileCount: 1,
- totalBytes: Buffer.byteLength("new line\nextra\n", "utf8"),
- maxFileBytes: Buffer.byteLength("new line\nextra\n", "utf8"),
- workspaceKind: "local",
- },
- },
- },
- });
- expect(JSON.stringify((result.output as any).perf)).not.toContain(file);
- expect(JSON.stringify((result.output as any).perf)).not.toContain("new line");
- });
- it("accepts a normalized read-state key when the stored display path differs", async () => {
- const file = join(tmpDir, "existing-normalized-key.txt");
- const fs = createMemoryFileSystem({
- [file]: "old line\n",
- });
- const revision = revisionFor(file, "old line\n");
- const readFileState: ReadFileStateMap = new Map([
- [
- createReadFileStateKey(file, 1, undefined),
- {
- path: `${tmpDir}/nested/../existing-normalized-key.txt`,
- content: "old line\n",
- offset: 1,
- limit: undefined,
- isPartialView: false,
- readAt: new Date(),
- revisionId: revision.id,
- mtimeMs: revision.mtimeMs,
- sizeBytes: revision.sizeBytes,
- },
- ],
- ]);
- const context = contextWith(fs, { readFileState });
- const result = await writeForModel(
- {
- file_path: file,
- content: "new line\n",
- },
- context,
- );
- expect(result.output).toMatchObject({
- type: "update",
- filePath: file,
- content: "new line\n",
- });
- expect(fs.files.get(file)).toBe("new line\n");
- });
- it("allows writes after an unchanged range Read in the same runtime", async () => {
- const file = join(tmpDir, "same-runtime-range-read.txt");
- const fs = createMemoryFileSystem({
- [file]: "line one\nline two\n",
- });
- const readFileState = createReadFileState(file, "line two\n", {
- limit: 1,
- offset: 2,
- revisionContent: "line one\nline two\n",
- });
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement\n",
- },
- contextWith(fs, { readFileState }),
- ),
- ).resolves.toMatchObject({
- output: {
- filePath: file,
- originalFile: "line one\nline two\n",
- type: "update",
- },
- });
- });
- it("allows writes after an unchanged offset-1 full Read when only mtime advances", async () => {
- const file = join(tmpDir, "mtime-offset-one-write.txt");
- const fs = createMemoryFileSystem({
- [file]: "same content\n",
- });
- const readFileState = createReadFileState(file, "same content\n", {
- offset: 1,
- mtimeMs: 1,
- });
- fs.setMtime(file, 2);
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement\n",
- },
- contextWith(fs, { readFileState }),
- ),
- ).resolves.toMatchObject({
- output: {
- filePath: file,
- originalFile: "same content\n",
- type: "update",
- },
- });
- });
- it("rejects writes when content changes inside the same millisecond", async () => {
- const file = join(tmpDir, "same-ms-content-change-default.txt");
- const fs = createMemoryFileSystem({
- [file]: "before",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "before", { mtimeMs: 1000.2 }),
- });
- fs.files.set(file, "after!");
- fs.setMtime(file, 1000.8);
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement",
- },
- context,
- ),
- ).rejects.toMatchObject({
- message:
- "File has been modified since read, either by the user or by a linter. Read it again before attempting to write it.",
- });
- });
- it("rejects writes from a token-truncated partial view", async () => {
- const file = join(tmpDir, "partial-view-write.txt");
- const fs = createMemoryFileSystem({
- [file]: "full content\n",
- });
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement\n",
- },
- contextWith(fs, {
- readFileState: createReadFileState(file, "full", {
- isPartialView: true,
- revisionContent: "full content\n",
- }),
- }),
- ),
- ).rejects.toMatchObject({
- message: "File has not been read yet. Read it first before writing to it.",
- });
- });
- it("documents existing-empty-file behavior as create", async () => {
- const file = join(tmpDir, "empty-existing.txt");
- const fs = createMemoryFileSystem({
- [file]: "",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, ""),
- });
- const result = await writeForModel(
- {
- file_path: file,
- content: "now filled\n",
- },
- context,
- );
- expect(result.output).toEqual({
- type: "create",
- filePath: file,
- content: "now filled\n",
- structuredPatch: [],
- originalFile: null,
- userModified: false,
- });
- expect(asModelText(result.modelContent)).toBe(
- `File created successfully at: ${file}${WRITE_STORAGE_SUFFIX}`,
- );
- });
- it("documents relative path output as the original input path", async () => {
- const fs = createMemoryFileSystem({});
- const result = await writeForModel(
- {
- file_path: "relative-created.txt",
- content: "relative content",
- },
- contextWith(fs, {
- workingDirectory: tmpDir,
- workspaceRoot: tmpDir,
- }),
- );
- const absolutePath = join(tmpDir, "relative-created.txt");
- expect(result.output).toEqual({
- type: "create",
- filePath: "relative-created.txt",
- content: "relative content",
- structuredPatch: [],
- originalFile: null,
- userModified: false,
- });
- expect(asModelText(result.modelContent)).toBe(
- `File created successfully at: relative-created.txt${WRITE_STORAGE_SUFFIX}`,
- );
- expect(fs.files.get(absolutePath)).toBe("relative content");
- });
- it("documents current update writes preserving the old file line-ending style", async () => {
- const file = join(tmpDir, "line-endings-update.txt");
- const fs = createMemoryFileSystem({
- [file]: "first\r\nsecond\r\n",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "first\r\nsecond\r\n"),
- });
- const result = await writeForModel(
- {
- file_path: file,
- content: "first\nchanged\n",
- },
- context,
- );
- expect(result.output).toMatchObject({
- type: "update",
- originalFile: "first\nsecond\n",
- content: "first\nchanged\n",
- });
- expect(fs.lastWrite?.lineEndings).toBe("CRLF");
- expect(fs.files.get(file)).toBe("first\r\nchanged\r\n");
- });
- });
- describeWriteStrictContract("Write strict model-result contract", () => {
- it("creates a new file and maps the create result for the model", async () => {
- const file = join(tmpDir, "nested", "created.txt");
- const fs = createMemoryFileSystem({});
- const result = await writeForModel(
- {
- file_path: file,
- content: "alpha\nbeta\n",
- },
- contextWith(fs),
- );
- expect(fs.files.get(file)).toBe("alpha\nbeta\n");
- expect(result.output).toEqual({
- type: "create",
- filePath: file,
- content: "alpha\nbeta\n",
- structuredPatch: [],
- originalFile: null,
- userModified: false,
- });
- expect(asModelText(result.modelContent)).toBe(
- `File created successfully at: ${file}${WRITE_STORAGE_SUFFIX}`,
- );
- });
- it("updates an existing nonempty file and maps the update result for the model", async () => {
- const file = join(tmpDir, "existing.txt");
- const fs = createMemoryFileSystem({
- [file]: "old line\nkept line\n",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "old line\nkept line\n"),
- });
- const result = await writeForModel(
- {
- file_path: file,
- content: "new line\nkept line\n",
- },
- context,
- );
- expect(fs.files.get(file)).toBe("new line\nkept line\n");
- expect(result.output).toMatchObject({
- type: "update",
- filePath: file,
- content: "new line\nkept line\n",
- originalFile: "old line\nkept line\n",
- userModified: false,
- });
- expect(result.output.structuredPatch.length).toBeGreaterThan(0);
- expect(result.output.structuredPatch[0]?.lines).toContain("-old line");
- expect(result.output.structuredPatch[0]?.lines).toContain("+new line");
- expect(asModelText(result.modelContent)).toBe(
- `The file ${file} has been updated successfully.${WRITE_STORAGE_SUFFIX}`,
- );
- });
- it("treats an existing empty file as a create result after it has been read", async () => {
- const file = join(tmpDir, "empty-existing.txt");
- const fs = createMemoryFileSystem({
- [file]: "",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, ""),
- });
- const result = await writeForModel(
- {
- file_path: file,
- content: "now filled\n",
- },
- context,
- );
- expect(fs.files.get(file)).toBe("now filled\n");
- expect(result.output).toEqual({
- type: "create",
- filePath: file,
- content: "now filled\n",
- structuredPatch: [],
- originalFile: null,
- userModified: false,
- });
- expect(asModelText(result.modelContent)).toBe(
- `File created successfully at: ${file}${WRITE_STORAGE_SUFFIX}`,
- );
- });
- it("returns an update result even when a nonempty overwrite is a no-op", async () => {
- const file = join(tmpDir, "same-content.txt");
- const fs = createMemoryFileSystem({
- [file]: "same\ncontent\n",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "same\ncontent\n"),
- });
- const result = await writeForModel(
- {
- file_path: file,
- content: "same\ncontent\n",
- },
- context,
- );
- expect(fs.files.get(file)).toBe("same\ncontent\n");
- expect(result.output).toMatchObject({
- type: "update",
- filePath: file,
- content: "same\ncontent\n",
- originalFile: "same\ncontent\n",
- userModified: false,
- });
- expect(asModelText(result.modelContent)).toBe(
- `The file ${file} has been updated successfully.${WRITE_STORAGE_SUFFIX}`,
- );
- });
- it("writes relative paths under cwd but preserves the original path in the model result", async () => {
- const fs = createMemoryFileSystem({});
- const result = await writeForModel(
- {
- file_path: "relative-created.txt",
- content: "relative content",
- },
- contextWith(fs, {
- workingDirectory: tmpDir,
- workspaceRoot: tmpDir,
- }),
- );
- expect(fs.files.get(join(tmpDir, "relative-created.txt"))).toBe("relative content");
- expect(result.output).toEqual({
- type: "create",
- filePath: "relative-created.txt",
- content: "relative content",
- structuredPatch: [],
- originalFile: null,
- userModified: false,
- });
- expect(asModelText(result.modelContent)).toBe(
- `File created successfully at: relative-created.txt${WRITE_STORAGE_SUFFIX}`,
- );
- });
- it("uses the user-modified note instead of the storage freshness suffix", () => {
- expect(
- asModelText(
- formatWriteModelContent({
- type: "create",
- filePath: "/work/user-edited.txt",
- content: "edited",
- structuredPatch: [],
- originalFile: null,
- userModified: true,
- }),
- ),
- ).toBe(`File created successfully at: /work/user-edited.txt${WRITE_USER_MODIFIED_NOTE}`);
- expect(
- asModelText(
- formatWriteModelContent({
- type: "update",
- filePath: "/work/user-edited.txt",
- content: "edited",
- structuredPatch: [],
- originalFile: "old",
- userModified: true,
- }),
- ),
- ).toBe(
- `The file /work/user-edited.txt has been updated successfully.${WRITE_USER_MODIFIED_NOTE}`,
- );
- });
- it("preserves explicit CRLF content when creating a file", async () => {
- const file = join(tmpDir, "line-endings.txt");
- const fs = createMemoryFileSystem({});
- const result = await writeForModel(
- {
- file_path: file,
- content: "first\r\nsecond\r\n",
- },
- contextWith(fs),
- );
- expect(fs.files.get(file)).toBe("first\r\nsecond\r\n");
- expect(result.output).toMatchObject({
- type: "create",
- content: "first\r\nsecond\r\n",
- });
- });
- it("requires an existing file to be read before writing", async () => {
- const file = join(tmpDir, "unread.txt");
- const fs = createMemoryFileSystem({
- [file]: "existing",
- });
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement",
- },
- contextWith(fs),
- ),
- ).rejects.toMatchObject({
- message: "File has not been read yet. Read it first before writing to it.",
- });
- });
- it("rejects writes when the file has changed since the cached read", async () => {
- const file = join(tmpDir, "stale.txt");
- const fs = createMemoryFileSystem({
- [file]: "before",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "before"),
- });
- fs.files.set(file, "after");
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement",
- },
- context,
- ),
- ).rejects.toMatchObject({
- message:
- "File has been modified since read, either by the user or by a linter. Read it again before attempting to write it.",
- });
- });
- it("allows writes when unchanged content only drifts inside the same millisecond", async () => {
- const file = join(tmpDir, "mtime-sub-ms-write.txt");
- const fs = createMemoryFileSystem({
- [file]: "before",
- });
- fs.setMtime(file, 1000.8);
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "before", { mtimeMs: 1000.2 }),
- });
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement",
- },
- context,
- ),
- ).resolves.toMatchObject({
- output: {
- type: "update",
- filePath: file,
- originalFile: "before",
- },
- });
- });
- it("rejects writes when content changes inside the same millisecond", async () => {
- const file = join(tmpDir, "same-ms-content-change.txt");
- const fs = createMemoryFileSystem({
- [file]: "before",
- });
- const context = contextWith(fs, {
- readFileState: createReadFileState(file, "before", { mtimeMs: 1000.2 }),
- });
- fs.files.set(file, "after!");
- fs.setMtime(file, 1000.8);
- await expect(
- writeForModel(
- {
- file_path: file,
- content: "replacement",
- },
- context,
- ),
- ).rejects.toMatchObject({
- message:
- "File has been modified since read, either by the user or by a linter. Read it again before attempting to write it.",
- });
- });
- it("honors Edit deny rules before filesystem access", () => {
- const service = new PermissionService(defaultPermissionConfig);
- const file = join(tmpDir, "blocked", "secret.txt");
- const projectRules: PermissionRuleset = {
- version: 1,
- deny: [
- {
- toolName: "Edit",
- ruleContent: `${tmpDir}/blocked/*`,
- },
- ],
- };
- const decision = service.checkPermission(
- {
- input: {
- file_path: file,
- content: "secret",
- },
- mode: "edit",
- riskLevel: writeToolEntry.metadata.riskLevel,
- toolName: "Write",
- },
- {
- ...writeToolEntry.metadata,
- permission: writeToolEntry.permission,
- },
- projectRules,
- );
- expect(decision).toMatchObject({
- allowed: false,
- decision: "deny",
- });
- });
- });
- async function writeForModel(input: unknown, context: ToolExecutionContext) {
- const output = await writeHandler(input, context);
- return {
- output,
- modelContent: formatWriteModelContent(output),
- };
- }
- function formatWriteModelContent(output: unknown): ModelMessageContent {
- const modelContent = writeToolEntry.formatModelContent?.(output);
- if (modelContent === undefined) {
- throw new Error("Write tool is missing formatModelContent");
- }
- return modelContent;
- }
- function asModelText(content: ModelMessageContent | undefined): string {
- if (content === undefined) {
- throw new Error("Missing model content");
- }
- return modelMessageContentToText(content);
- }
- async function executeWrite(input: unknown, fileSystemPort: FileSystemPort) {
- const sessionId = createSessionId("write-contract");
- const turnId = createTurnId("write-contract");
- const traceContext = createRootTraceContext({ sessionId, turnId });
- const registry = createToolRegistry();
- registry.register(writeToolEntry);
- return createToolExecutor({
- emitEvent: async () => {},
- fileSystemPort,
- mode: "edit",
- permissionService: new PermissionService(defaultPermissionConfig),
- registry,
- sessionId,
- traceContext,
- turnId,
- workingDirectory: tmpDir,
- workspaceRoot: tmpDir,
- }).execute(
- {
- id: createToolCallId("write-contract"),
- input,
- name: "Write",
- },
- { traceContext },
- );
- }
- function contextWith(
- fileSystemPort: FileSystemPort,
- options: {
- readFileState?: ReadFileStateMap;
- workingDirectory?: string;
- workspaceRoot?: string;
- } = {},
- ): ToolExecutionContext {
- return {
- toolCallId: "tool_test",
- traceId: "trace_test",
- spanId: "span_test",
- abortSignal: new AbortController().signal,
- fileSystemPort,
- readFileState: options.readFileState,
- workingDirectory: options.workingDirectory ?? tmpDir,
- workspaceRoot: options.workspaceRoot ?? tmpDir,
- sessionId: "sess_test",
- turnId: "turn_test",
- };
- }
- function createReadFileState(
- filePath: string,
- content: string,
- options: {
- isPartialView?: boolean;
- limit?: number;
- mtimeMs?: number;
- offset?: number;
- readAt?: Date;
- revisionContent?: string;
- } = {},
- ): ReadFileStateMap {
- const revisionContent = options.revisionContent ?? content;
- const revision = revisionFor(filePath, revisionContent, options.mtimeMs ?? 1);
- return new Map([
- [
- [
- filePath,
- String(options.offset ?? 1),
- options.limit === undefined ? "" : String(options.limit),
- ].join("\0"),
- {
- path: filePath,
- content: normalizeLineEndings(content),
- offset: options.offset,
- limit: options.limit,
- isPartialView: options.isPartialView ?? false,
- readAt: options.readAt ?? new Date(),
- revisionId: revision.id,
- mtimeMs: revision.mtimeMs,
- sizeBytes: revision.sizeBytes,
- },
- ],
- ]);
- }
- interface MemoryFileSystem extends FileSystemPort {
- files: Map<string, string>;
- lastWrite?: FileSystemWriteTextRequest;
- mtimes: Map<string, number>;
- readPaths: string[];
- setMtime(path: string, mtimeMs: number): void;
- }
- function createMemoryFileSystem(initial: Record<string, string>): MemoryFileSystem {
- const files = new Map(Object.entries(initial));
- const mtimes = new Map<string, number>();
- const fs: MemoryFileSystem = {
- files,
- mtimes,
- readPaths: [],
- setMtime(path, mtimeMs) {
- mtimes.set(path, mtimeMs);
- },
- async stat(request): Promise<FileSystemStatResult> {
- const content = files.get(request.path);
- if (content === undefined) throw missingFile(request.path);
- const mtimeMs = mtimes.get(request.path) ?? 1;
- return {
- kind: "file",
- path: request.path,
- mtimeMs,
- revision: revisionFor(request.path, content, mtimeMs),
- sizeBytes: Buffer.byteLength(content, "utf8"),
- };
- },
- async readTextFile(request): Promise<FileSystemReadTextResult> {
- fs.readPaths.push(request.path);
- const content = readTextOrThrow(request.path, files);
- const normalized = normalizeLineEndings(content);
- const mtimeMs = mtimes.get(request.path) ?? 1;
- return {
- bytesRead: Buffer.byteLength(content, "utf8"),
- content: normalized,
- encoding: request.encoding ?? "utf8",
- lineEndings: detectLineEndings(content),
- path: request.path,
- revision: revisionFor(request.path, content, mtimeMs),
- sizeBytes: Buffer.byteLength(content, "utf8"),
- truncated: false,
- };
- },
- async readBinaryFile(request) {
- const content = readTextOrThrow(request.path, files);
- const buffer = Buffer.from(content, "utf8");
- return {
- bytesRead: buffer.byteLength,
- content: buffer,
- path: request.path,
- revision: revisionFor(request.path, content, mtimes.get(request.path) ?? 1),
- sizeBytes: buffer.byteLength,
- };
- },
- async readTextFileRange(request): Promise<FileSystemReadTextRangeResult> {
- const content = readTextOrThrow(request.path, files);
- const normalized = normalizeLineEndings(content);
- const lines = normalized.length === 0 ? [] : normalized.split("\n");
- const offsetLine = Math.max(0, Math.trunc(request.offsetLine ?? 0));
- const limitLines =
- request.limitLines === undefined ? undefined : Math.max(0, Math.trunc(request.limitLines));
- const selected =
- limitLines === undefined
- ? lines.slice(offsetLine)
- : lines.slice(offsetLine, offsetLine + limitLines);
- return {
- bytesRead: Buffer.byteLength(content, "utf8"),
- content: selected.join("\n"),
- encoding: request.encoding ?? "utf8",
- lineCount: selected.length,
- lineEndings: detectLineEndings(content),
- path: request.path,
- revision: revisionFor(request.path, content, mtimes.get(request.path) ?? 1),
- sizeBytes: Buffer.byteLength(content, "utf8"),
- startLine: offsetLine + 1,
- totalLines: lines.length,
- truncated: false,
- };
- },
- async writeTextFile(request) {
- const content = applyLineEndings(request.content, request.lineEndings);
- fs.lastWrite = request;
- files.set(request.path, content);
- mtimes.set(request.path, (mtimes.get(request.path) ?? 1) + 1);
- return {
- bytesWritten: Buffer.byteLength(content, "utf8"),
- path: request.path,
- revision: revisionFor(request.path, content, mtimes.get(request.path) ?? 1),
- };
- },
- async removeFile(request) {
- const removed = files.delete(request.path);
- if (!removed && request.missingOk !== true) throw missingFile(request.path);
- return {
- path: request.path,
- removed,
- };
- },
- async listDirectory(request) {
- const prefix = `${request.path.replace(/\/+$/u, "")}/`;
- const entries = Array.from(files.keys())
- .filter((path) => path.startsWith(prefix))
- .map((path) => ({
- kind: "file" as const,
- name: path.slice(prefix.length),
- path,
- }));
- return {
- durationMs: 0,
- entries,
- numEntries: entries.length,
- path: request.path,
- };
- },
- async searchFiles(request): Promise<FileSystemSearchFilesResult> {
- return {
- durationMs: 0,
- files: [],
- numFiles: 0,
- path: request.path,
- pattern: request.pattern,
- truncated: false,
- };
- },
- async searchText(request): Promise<FileSystemSearchTextResult> {
- return {
- durationMs: 0,
- entries: [],
- files: [],
- mode: request.outputMode ?? "files_with_matches",
- numMatches: 0,
- path: request.path,
- pattern: request.pattern,
- truncated: false,
- };
- },
- };
- return fs;
- }
- function readTextOrThrow(path: string, files: Map<string, string>): string {
- const content = files.get(path);
- if (content === undefined) throw missingFile(path);
- return content;
- }
- function missingFile(path: string): Error {
- return createFileSystemError({
- code: "not_found",
- message: `missing: ${path}`,
- path,
- });
- }
- function normalizeLineEndings(content: string): string {
- return content.replaceAll("\r\n", "\n");
- }
- function applyLineEndings(content: string, lineEndings: FileSystemLineEndings | undefined): string {
- if (lineEndings !== "CRLF") return content;
- return content.replaceAll("\r\n", "\n").split("\n").join("\r\n");
- }
- function detectLineEndings(content: string): "LF" | "CRLF" {
- let crlfCount = 0;
- let lfCount = 0;
- for (let index = 0; index < content.length; index += 1) {
- if (content[index] !== "\n") continue;
- if (index > 0 && content[index - 1] === "\r") crlfCount += 1;
- else lfCount += 1;
- }
- return crlfCount > lfCount ? "CRLF" : "LF";
- }
- function revisionFor(path: string, content: string, mtimeMs = 1): FileSystemRevision {
- return {
- id: `rev:${path}:${content.length}:${mtimeMs}`,
- mtimeMs,
- sizeBytes: Buffer.byteLength(content, "utf8"),
- };
- }
|