Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { basename, extname, isAbsolute, join, resolve } from "node:path";
import type { RuntimeToolResult } from "@vetta/runtime-core/kernel";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import type { ToolCompatibilityDefinition } from "../compatibility/tool-compatibility-contract.js";
const TINY_PNG_BASE64 =
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==";
export interface ReadBehaviorInput {
readonly description?: string;
readonly path: string;
readonly offset?: number;
readonly limit?: number;
}
export interface ReadBehaviorOperations {
readFile(absolutePath: string): Promise<Buffer>;
access(absolutePath: string): Promise<void>;
detectImageMimeType?(absolutePath: string): Promise<string | null | undefined>;
}
export interface ReadBehaviorSubjectOptions {
readonly autoResizeImages?: boolean;
readonly operations?: ReadBehaviorOperations;
readonly preserveFullText?: (absolutePath: string) => boolean;
readonly binaryContentHint?: (extension: string) => string | undefined;
}
export interface ReadBehaviorSubject {
readonly definition: ToolCompatibilityDefinition;
execute(input: ReadBehaviorInput, signal?: AbortSignal): Promise<RuntimeToolResult>;
}
export type CreateReadBehaviorSubject = (cwd: string, options?: ReadBehaviorSubjectOptions) => ReadBehaviorSubject;
function isInstructionMarkdown(path: string): boolean {
return (
basename(path) === "SKILL.md" ||
(extname(path).toLowerCase() === ".md" && path.split(/[/\\]+/).includes("skills"))
);
}
export function defineReadBehaviorContract(subjectName: string, createSubject: CreateReadBehaviorSubject): void {
describe(`${subjectName} read behavior contract`, () => {
let testDirectory: string;
beforeEach(() => {
testDirectory = mkdtempSync(join(tmpdir(), "runtime-tools-read-contract-"));
});
afterEach(() => {
rmSync(testDirectory, { recursive: true, force: true });
});
it("preserves the model-visible definition", () => {
const subject = createSubject(testDirectory);
expect(subject.definition).toMatchObject({
name: "read",
label: "read",
schema: {
type: "object",
required: ["path"],
properties: {
description: {
type: "string",
maxLength: 100,
},
path: {
type: "string",
description: "Path to the file to read (relative or absolute)",
},
offset: {
type: "number",
description: "Line number to start reading from (1-indexed)",
},
limit: {
type: "number",
description: "Maximum number of lines to read",
},
},
},
});
expect(subject.definition.schema).not.toHaveProperty("additionalProperties");
expect(subject.definition.description).toContain("prefixed with their line number");
expect(subject.definition.description).toContain("offset and limit");
expect(subject.definition.description).not.toContain("SKILL.md");
});
it("reads UTF-8 text with numbered lines the edit tool can point at", async () => {
const path = join(testDirectory, "hello.txt");
writeFileSync(path, "first line\nsecond line");
const result = await createSubject(testDirectory).execute({ path: "hello.txt" });
expect(textOutput(result)).toMatch(/^1:first line\n2:second line$/);
expect(result.details).toEqual({ totalLines: 2 });
});
it("falls back to GB18030 when UTF-8 decoding fails", async () => {
const path = join(testDirectory, "gb18030.txt");
writeFileSync(path, Buffer.from([0xd6, 0xd0, 0xce, 0xc4]));
const result = await createSubject(testDirectory).execute({ path });
expect(textOutput(result)).toMatch(/^1:中文$/);
});
it("preserves empty-file output and missing-file failures", async () => {
const emptyPath = join(testDirectory, "empty.txt");
writeFileSync(emptyPath, "");
const subject = createSubject(testDirectory);
expect(textOutput(await subject.execute({ path: emptyPath }))).toMatch(/^1:$/);
await expect(subject.execute({ path: join(testDirectory, "missing.txt") })).rejects.toThrow();
});
it("preserves relative, absolute, home, Unicode-space, and fuzzy CJK path resolution", async () => {
const exactName = "招标文件-发布稿.txt";
const exactPath = join(testDirectory, exactName);
writeFileSync(exactPath, "resolved");
const subject = createSubject(testDirectory);
await expect(subject.execute({ path: exactName })).resolves.toMatchObject({
content: [{ type: "text" }],
});
await expect(subject.execute({ path: exactPath })).resolves.toMatchObject({
content: [{ type: "text" }],
});
expect(textOutput(await subject.execute({ path: "招标文件 - 发布稿.txt" }))).toContain("resolved");
const unicodeSpaceName = "unicode space.txt";
writeFileSync(join(testDirectory, unicodeSpaceName), "unicode-space");
expect(textOutput(await subject.execute({ path: "unicode\u00a0space.txt" }))).toContain("unicode-space");
const observedPaths: string[] = [];
const homeSubject = createSubject(testDirectory, {
operations: {
async access(absolutePath) {
observedPaths.push(absolutePath);
},
async readFile() {
return Buffer.from("home");
},
},
});
await homeSubject.execute({ path: "~/runtime-tools-read-contract-home.txt" });
expect(observedPaths[0]).toBe(`${homedir()}/runtime-tools-read-contract-home.txt`);
expect(isAbsolute(observedPaths[0])).toBe(true);
});
it("preserves macOS narrow-space, NFD, and curly-quote path fallbacks", async () => {
const fixtures = [
{
actual: "Screenshot 10.00.00\u202fAM.txt",
requested: "Screenshot 10.00.00 AM.txt",
content: "narrow-space",
},
{
actual: "cafe\u0301.txt",
requested: "café.txt",
content: "nfd",
},
{
actual: "Capture d\u2019ecran.txt",
requested: "Capture d'ecran.txt",
content: "curly-quote",
},
{
actual: "Capture d\u2019e\u0301cran.txt",
requested: "Capture d'écran.txt",
content: "nfd-curly",
},
] as const;
const subject = createSubject(testDirectory);
for (const fixture of fixtures) {
writeFileSync(join(testDirectory, fixture.actual), fixture.content);
expect(textOutput(await subject.execute({ path: fixture.requested }))).toContain(fixture.content);
}
});
it("applies offset and limit without changing anchor line numbers", async () => {
const path = join(testDirectory, "lines.txt");
writeFileSync(path, Array.from({ length: 100 }, (_, index) => `line ${index + 1}`).join("\n"));
const subject = createSubject(testDirectory);
const offsetResult = await subject.execute({ path, offset: 51, limit: 2 });
expect(textOutput(offsetResult)).toMatch(/^51:line 51\n52:line 52/);
expect(textOutput(offsetResult)).toContain("[48 more lines in file. Use offset=53 to continue.]");
const zeroOffsetResult = await subject.execute({ path, offset: 0, limit: 1 });
expect(textOutput(zeroOffsetResult)).toMatch(/^1:line 1/);
});
it("rejects offsets beyond the end of the file with the legacy error", async () => {
const path = join(testDirectory, "short.txt");
writeFileSync(path, "one\ntwo");
await expect(createSubject(testDirectory).execute({ path, offset: 100 })).rejects.toThrow(
"Offset 100 is beyond end of file (2 lines total)",
);
});
it("preserves line truncation details and continuation notice", async () => {
const path = join(testDirectory, "many-lines.txt");
writeFileSync(path, Array.from({ length: 2001 }, (_, index) => `line ${index + 1}`).join("\n"));
const result = await createSubject(testDirectory).execute({ path });
expect(textOutput(result)).toContain("[Showing lines 1-2000 of 2001. Use offset=2001 to continue.]");
expect(result.details).toMatchObject({
totalLines: 2001,
truncation: {
truncated: true,
truncatedBy: "lines",
totalLines: 2001,
outputLines: 2000,
},
});
});
it("preserves the oversized first-line notice and truncation details", async () => {
const path = join(testDirectory, "long-line.txt");
writeFileSync(path, "x".repeat(60 * 1024));
const result = await createSubject(testDirectory).execute({ path });
expect(textOutput(result)).toContain("[Line 1 is 60.0KB, exceeds 50.0KB limit.");
expect(result.details).toMatchObject({
truncation: {
truncated: true,
truncatedBy: "bytes",
outputLines: 0,
firstLineExceedsLimit: true,
},
});
});
it("detects images by file magic and can return original bytes without resizing", async () => {
const path = join(testDirectory, "image.txt");
const imageBuffer = Buffer.from(TINY_PNG_BASE64, "base64");
writeFileSync(path, imageBuffer);
const result = await createSubject(testDirectory, { autoResizeImages: false }).execute({ path });
const image = result.content.find((item) => item.type === "image");
expect(textOutput(result)).toBe("Read image file [image/png]");
expect(image).toEqual({
type: "image",
data: imageBuffer.toString("base64"),
mimeType: "image/png",
});
expect(result.details).toMatchObject({
image: {
originalPath: path,
originalMimeType: "image/png",
originalSizeBytes: imageBuffer.length,
processedMimeType: "image/png",
processedSizeBytes: imageBuffer.length,
wasResized: false,
},
});
});
it("preserves default image processing and dimension details", async () => {
const path = join(testDirectory, "default-image.png");
writeFileSync(path, Buffer.from(TINY_PNG_BASE64, "base64"));
const result = await createSubject(testDirectory).execute({ path });
expect(result.content.some((item) => item.type === "image")).toBe(true);
expect(result.details).toMatchObject({
image: {
originalPath: path,
originalMimeType: "image/png",
originalWidth: 1,
originalHeight: 1,
processedWidth: 1,
processedHeight: 1,
wasResized: false,
},
});
});
it("does not trust an image extension when file magic is text", async () => {
const path = join(testDirectory, "not-an-image.png");
writeFileSync(path, "definitely not a png");
const result = await createSubject(testDirectory).execute({ path });
expect(textOutput(result)).toContain("definitely not a png");
expect(result.content.some((item) => item.type === "image")).toBe(false);
});
it("uses an injected hint instead of exposing raw binary document bytes", async () => {
const path = join(testDirectory, "spec.docx");
writeFileSync(path, Buffer.from([0x50, 0x4b, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00]));
const result = await createSubject(testDirectory, {
binaryContentHint: () => 'Load the "docx" extension for instructions on how to handle this file.',
}).execute({ path });
expect(textOutput(result)).toBe(
'Binary file detected (.docx, 8B). Raw bytes are not shown to avoid context pollution.\nLoad the "docx" extension for instructions on how to handle this file.',
);
expect(result.details).toBeUndefined();
});
it("detects binary content without a known extension", async () => {
const path = join(testDirectory, "binary-data");
writeFileSync(path, Buffer.from([0x00, 0x01, 0x02, 0x03]));
const result = await createSubject(testDirectory).execute({ path });
expect(textOutput(result)).toBe(
"Binary file detected ((no extension), 4B). Raw bytes are not shown to avoid context pollution.",
);
});
it("honours an injected full-text preservation policy", async () => {
const oversized = Array.from({ length: 2500 }, (_, index) => `step ${index + 1}`).join("\n");
const skillDirectory = join(testDirectory, "skills", "deploy");
mkdirSync(skillDirectory, { recursive: true });
writeFileSync(join(skillDirectory, "SKILL.md"), oversized);
writeFileSync(join(skillDirectory, "reference.md"), oversized);
writeFileSync(join(testDirectory, "SKILL.md"), oversized);
const subject = createSubject(testDirectory, { preserveFullText: isInstructionMarkdown });
for (const path of ["skills/deploy/SKILL.md", "skills/deploy/reference.md", "SKILL.md"]) {
const result = await subject.execute({ path });
expect(textOutput(result)).toContain("step 2500");
expect(textOutput(result)).not.toContain("Use offset=");
expect(result.details).toEqual({ totalLines: 2500 });
}
});
it("still truncates Markdown outside a skills directory and near-miss directory names", async () => {
const oversized = Array.from({ length: 2500 }, (_, index) => `step ${index + 1}`).join("\n");
const nearMiss = join(testDirectory, "skills-preset");
mkdirSync(nearMiss, { recursive: true });
writeFileSync(join(nearMiss, "guide.md"), oversized);
writeFileSync(join(testDirectory, "guide.md"), oversized);
const subject = createSubject(testDirectory, { preserveFullText: isInstructionMarkdown });
for (const path of ["skills-preset/guide.md", "guide.md"]) {
expect(textOutput(await subject.execute({ path }))).toContain("Use offset=2001 to continue.");
}
});
it("honours an explicit range inside a preserved file without auto-truncating the rest", async () => {
const oversized = Array.from({ length: 2500 }, (_, index) => `step ${index + 1}`).join("\n");
writeFileSync(join(testDirectory, "SKILL.md"), oversized);
const result = await createSubject(testDirectory, { preserveFullText: isInstructionMarkdown }).execute({
path: "SKILL.md",
offset: 10,
limit: 2,
});
expect(textOutput(result)).toMatch(/^10:step 10\n11:step 11/);
expect(textOutput(result)).toContain("[2489 more lines in file. Use offset=12 to continue.]");
});
it("classifies missing, directory, and unreadable paths into distinct tool errors", async () => {
const subject = createSubject(testDirectory);
mkdirSync(join(testDirectory, "a-directory"), { recursive: true });
await expect(subject.execute({ path: "missing.txt" })).rejects.toMatchObject({
name: "RuntimeToolExecutionError",
details: { code: "read_file_not_found", retryable: false },
});
await expect(subject.execute({ path: "a-directory" })).rejects.toMatchObject({
name: "RuntimeToolExecutionError",
details: { code: "read_is_a_directory", retryable: false },
});
await expect(subject.execute({ path: "a-directory" })).rejects.toThrow("Use the `ls` tool");
const denied = createSubject(testDirectory, {
operations: {
async access() {
throw Object.assign(new Error("EACCES: permission denied"), { code: "EACCES" });
},
async readFile() {
return Buffer.from("unreachable");
},
},
});
await expect(denied.execute({ path: "secret.txt" })).rejects.toMatchObject({
name: "RuntimeToolExecutionError",
details: { code: "read_permission_denied", retryable: false },
});
});
it("classifies an out-of-range offset without changing the legacy message", async () => {
const path = join(testDirectory, "two-lines.txt");
writeFileSync(path, "one\ntwo");
await expect(createSubject(testDirectory).execute({ path, offset: 100 })).rejects.toMatchObject({
name: "RuntimeToolExecutionError",
message: "Offset 100 is beyond end of file (2 lines total)",
details: { code: "read_offset_out_of_range", retryable: false },
});
});
it("uses injected read operations without changing path resolution", async () => {
const observed: Array<{ operation: string; path: string }> = [];
const subject = createSubject(testDirectory, {
operations: {
async access(absolutePath) {
observed.push({ operation: "access", path: absolutePath });
},
async detectImageMimeType(absolutePath) {
observed.push({ operation: "detect", path: absolutePath });
return undefined;
},
async readFile(absolutePath) {
observed.push({ operation: "read", path: absolutePath });
return Buffer.from("remote text");
},
},
});
const result = await subject.execute({ path: "remote.txt" });
const expectedPath = resolve(testDirectory, "remote.txt");
expect(textOutput(result)).toContain("remote text");
expect(observed).toEqual([
{ operation: "access", path: expectedPath },
{ operation: "detect", path: expectedPath },
{ operation: "read", path: expectedPath },
]);
});
it("rejects before invoking operations when already aborted", async () => {
let operationCount = 0;
const subject = createSubject(testDirectory, {
operations: {
async access() {
operationCount += 1;
},
async readFile() {
operationCount += 1;
return Buffer.from("unused");
},
},
});
const controller = new AbortController();
controller.abort();
await expect(subject.execute({ path: "unused.txt" }, controller.signal)).rejects.toThrow("Operation aborted");
expect(operationCount).toBe(0);
});
it("rejects an in-flight read and does not continue to readFile after abort", async () => {
let releaseAccess: (() => void) | undefined;
let readCount = 0;
const subject = createSubject(testDirectory, {
operations: {
access() {
return new Promise<void>((resolveAccess) => {
releaseAccess = resolveAccess;
});
},
async readFile() {
readCount += 1;
return Buffer.from("unused");
},
},
});
const controller = new AbortController();
const pending = subject.execute({ path: "slow.txt" }, controller.signal);
await Promise.resolve();
controller.abort();
releaseAccess?.();
await expect(pending).rejects.toThrow("Operation aborted");
await Promise.resolve();
expect(readCount).toBe(0);
});
});
}
function textOutput(result: RuntimeToolResult): string {
return result.content
.filter((item) => item.type === "text")
.map((item) => item.text)
.join("\n");
}