2026-09-19 15:24:26 +03:00
|
|
|
/**
|
|
|
|
|
* The read tool (sources v0) — a Node port of pi's read semantics:
|
|
|
|
|
* truncation with continuation offsets, image mime detection, error paths.
|
2026-09-25 23:32:13 +03:00
|
|
|
* Driven through the NATIVE AgentTool execute (Wave A, 2026-09-26): text
|
|
|
|
|
* reads return the raw string as details (the record's tool part output
|
|
|
|
|
* reads it); image reads return native pi content parts (text + image)
|
|
|
|
|
* with null details (the output-less part until the record persists).
|
2026-09-19 15:24:26 +03:00
|
|
|
*/
|
|
|
|
|
import assert from "node:assert/strict";
|
|
|
|
|
import * as fs from "node:fs";
|
|
|
|
|
import * as os from "node:os";
|
|
|
|
|
import * as path from "node:path";
|
|
|
|
|
import { describe, it } from "node:test";
|
|
|
|
|
import { readTool } from "../src/agent/tools/read.ts";
|
|
|
|
|
|
|
|
|
|
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "lh-read-test-"));
|
|
|
|
|
|
|
|
|
|
function write(name: string, content: string): string {
|
|
|
|
|
const file = path.join(tmp, name);
|
|
|
|
|
fs.writeFileSync(file, content, "utf-8");
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 23:32:13 +03:00
|
|
|
/** Native execute → the raw string body (text reads ride details verbatim). */
|
2026-09-19 15:24:26 +03:00
|
|
|
const run = (args: { path: string; offset?: number; limit?: number }) =>
|
2026-09-25 23:32:13 +03:00
|
|
|
readTool.execute!("test", args).then((r) => {
|
|
|
|
|
assert.ok(r.content.length >= 1);
|
|
|
|
|
const first = r.content[0]!;
|
|
|
|
|
assert.equal(first.type, "text");
|
|
|
|
|
return { text: (first as { type: "text"; text: string }).text, details: r.details };
|
2026-09-19 15:24:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("read tool — text semantics", () => {
|
|
|
|
|
it("reads a small file verbatim", async () => {
|
|
|
|
|
const file = write("small.txt", "hello\nworld");
|
2026-09-25 23:32:13 +03:00
|
|
|
const { text, details } = await run({ path: file });
|
|
|
|
|
assert.equal(text, "hello\nworld");
|
|
|
|
|
assert.equal(details, "hello\nworld", "the record part output carries the string verbatim");
|
2026-09-19 15:24:26 +03:00
|
|
|
});
|
|
|
|
|
|
2026-09-19 16:50:20 +03:00
|
|
|
it("resolves relative paths against the learner's home (sources v1 anchoring)", async () => {
|
|
|
|
|
// sources v1: the harness has no meaningful cwd — "find my notes" must
|
|
|
|
|
// work from a bare relative path, so the home directory is the anchor
|
|
|
|
|
// (fs-shared resolveSourcePath). Scratch file in home, cleaned up.
|
|
|
|
|
const rel = `lh-read-rel-test-${process.pid}.txt`;
|
|
|
|
|
const homeFile = path.join(os.homedir(), rel);
|
|
|
|
|
fs.writeFileSync(homeFile, "rel", "utf-8");
|
2026-09-19 15:24:26 +03:00
|
|
|
try {
|
2026-09-25 23:32:13 +03:00
|
|
|
assert.equal((await run({ path: rel })).text, "rel");
|
2026-09-19 15:24:26 +03:00
|
|
|
} finally {
|
2026-09-19 16:50:20 +03:00
|
|
|
fs.unlinkSync(homeFile);
|
2026-09-19 15:24:26 +03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("honors offset (1-indexed) and limit", async () => {
|
|
|
|
|
const body = Array.from({ length: 30 }, (_, i) => `line ${i + 1}`).join("\n");
|
|
|
|
|
const file = write("thirty.txt", body);
|
2026-09-25 23:32:13 +03:00
|
|
|
const out = (await run({ path: file, offset: 10, limit: 3 })).text;
|
2026-09-19 15:24:26 +03:00
|
|
|
// pi parity: the explicit limit stopping short gets a remaining notice
|
|
|
|
|
assert.match(out, /^line 10\nline 11\nline 12\n\n\[18 more lines in file\. Use offset=13 to continue\.\]$/);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("offset beyond EOF is an error the model sees", async () => {
|
|
|
|
|
const file = write("tiny.txt", "one");
|
|
|
|
|
await assert.rejects(() => run({ path: file, offset: 99 }), /beyond end of file/);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("a missing file throws (pi's error-result semantics)", async () => {
|
|
|
|
|
await assert.rejects(() => run({ path: path.join(tmp, "nope.txt") }), /ENOENT/);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("truncates at 2000 lines with a use-offset continuation notice", async () => {
|
|
|
|
|
const body = Array.from({ length: 3000 }, (_, i) => `l${i + 1}`).join("\n");
|
|
|
|
|
const file = write("big.txt", body);
|
2026-09-25 23:32:13 +03:00
|
|
|
const out = (await run({ path: file })).text;
|
2026-09-19 15:24:26 +03:00
|
|
|
assert.match(out, /Showing lines 1-2000 of 3000 \(2000 line limit\)/);
|
|
|
|
|
assert.match(out, /Use offset=2001 to continue/);
|
|
|
|
|
assert.ok(!out.includes("l2001\n"), "line 2001 must not ride along");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("truncates at 50KB with the same continuation notice", async () => {
|
|
|
|
|
// 200 lines of ~400B = ~80KB total, under the line cap — byte cap fires
|
|
|
|
|
const line = "x".repeat(400);
|
|
|
|
|
const body = Array.from({ length: 200 }, () => line).join("\n");
|
|
|
|
|
const file = write("wide.txt", body);
|
2026-09-25 23:32:13 +03:00
|
|
|
const out = (await run({ path: file })).text;
|
2026-09-19 15:24:26 +03:00
|
|
|
assert.match(out, /50\.0KB limit\)\. Use offset=\d+ to continue\./);
|
|
|
|
|
// and the truncation actually cut content
|
|
|
|
|
assert.ok(out.length < 52_000, "the 50KB ceiling must cut the 80KB body");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("an explicit limit that stops short gets a remaining-lines notice", async () => {
|
|
|
|
|
const body = Array.from({ length: 10 }, (_, i) => `l${i + 1}`).join("\n");
|
|
|
|
|
const file = write("ten.txt", body);
|
2026-09-25 23:32:13 +03:00
|
|
|
const out = (await run({ path: file, limit: 4 })).text;
|
2026-09-19 15:24:26 +03:00
|
|
|
assert.match(out, /\[6 more lines in file\. Use offset=5 to continue\.\]/);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-25 23:32:13 +03:00
|
|
|
describe("read tool — image semantics (native content parts)", () => {
|
|
|
|
|
it("returns native text + image parts for image files, details null (output-less part)", async () => {
|
2026-09-19 15:24:26 +03:00
|
|
|
const file = path.join(tmp, "dot.png");
|
|
|
|
|
// minimal valid PNG header bytes are enough — detection is by extension
|
|
|
|
|
fs.writeFileSync(file, Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));
|
2026-09-25 23:32:13 +03:00
|
|
|
const result = await readTool.execute!("test", { path: file });
|
|
|
|
|
assert.equal(result.content.length, 2);
|
|
|
|
|
const parts = result.content as Array<{ type: string; text?: string; data?: string; mimeType?: string }>;
|
|
|
|
|
const textPart = parts[0]!;
|
|
|
|
|
const imagePart = parts[1]!;
|
|
|
|
|
assert.equal(textPart.type, "text");
|
|
|
|
|
assert.match(textPart.text!, /Read image file \[image\/png\]/);
|
|
|
|
|
assert.equal(imagePart.type, "image");
|
|
|
|
|
assert.equal(imagePart.mimeType, "image/png");
|
|
|
|
|
assert.match(imagePart.data!, /^[A-Za-z0-9+/]+={0,2}$/);
|
|
|
|
|
assert.equal(result.details, null, "image reads keep the record part output-less");
|
2026-09-19 15:24:26 +03:00
|
|
|
});
|
|
|
|
|
|
2026-09-25 23:32:13 +03:00
|
|
|
it("plain string results map to a single text part (parity)", async () => {
|
2026-09-19 15:24:26 +03:00
|
|
|
const file = write("plain.txt", "body");
|
2026-09-25 23:32:13 +03:00
|
|
|
const result = await readTool.execute!("test", { path: file });
|
|
|
|
|
assert.equal(result.content.length, 1);
|
|
|
|
|
assert.equal(result.content[0]!.type, "text");
|
|
|
|
|
assert.equal((result.content[0] as { type: "text"; text: string }).text, "body");
|
2026-09-19 15:24:26 +03:00
|
|
|
});
|
2026-09-25 23:32:13 +03:00
|
|
|
});
|