Skip to content

Commit 5066bd7

Browse files
authored
fix(agents): continue oversized read lines (#126587)
1 parent 366dbeb commit 5066bd7

8 files changed

Lines changed: 723 additions & 177 deletions

‎src/agents/agent-tools.create-openclaw-coding-tools.test.ts‎

Lines changed: 170 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -189,39 +189,84 @@ describe("createOpenClawCodingTools", () => {
189189
expect(latestCreateOpenClawToolsOptions().webSearchEnabled).toBe(false);
190190
});
191191

192-
it("reads node-hosted skill content through the assembled workspace-only read tool", async () => {
193-
const locator = "node://node-1/skills/pond/SKILL.md";
194-
const tools = createOpenClawCodingTools({
195-
config: { tools: { fs: { workspaceOnly: true } } },
196-
skillsSnapshot: {
197-
prompt: "",
198-
skills: [{ name: "pond" }],
199-
resolvedSkills: [
200-
{
201-
name: "pond",
202-
description: "Pond skill",
203-
filePath: locator,
204-
baseDir: "node://node-1/skills/pond",
205-
readContent: "# Pond\nassembled-marker",
206-
source: "openclaw-node",
207-
sourceInfo: {
208-
source: "openclaw-node",
209-
path: locator,
210-
scope: "temporary",
211-
origin: "top-level",
192+
it.each([
193+
{
194+
name: "fitting node",
195+
backend: "node",
196+
content: "# Pond\nassembled-marker",
197+
oversized: false,
198+
},
199+
{
200+
name: "multi-page node",
201+
backend: "node",
202+
content: `${"ok\n".repeat(2_100)}done`,
203+
oversized: false,
204+
},
205+
{
206+
name: "oversized node",
207+
backend: "node",
208+
content: `# Pond\n${"x".repeat(33 * 1024)}`,
209+
oversized: true,
210+
},
211+
{ name: "fitting local", backend: "local", content: "# Pond\nlocal-marker", oversized: false },
212+
{
213+
name: "oversized local",
214+
backend: "local",
215+
content: `# Pond\n${"x".repeat(33 * 1024)}`,
216+
oversized: true,
217+
},
218+
])(
219+
"serves $name skill instructions whole or refuses them",
220+
async ({ backend, content, oversized }) => {
221+
const virtual = backend === "node";
222+
const baseDir = virtual
223+
? "node://node-1/skills/pond"
224+
: tempDirs.make("openclaw-assembled-local-skill-");
225+
const locator = virtual ? `${baseDir}/SKILL.md` : path.join(baseDir, "SKILL.md");
226+
if (!virtual) {
227+
await fs.writeFile(locator, content, "utf8");
228+
}
229+
const tools = createOpenClawCodingTools({
230+
config: { tools: { fs: { workspaceOnly: true } } },
231+
skillsSnapshot: {
232+
prompt: "",
233+
skills: [{ name: "pond" }],
234+
resolvedSkills: [
235+
{
236+
name: "pond",
237+
description: "Pond skill",
238+
filePath: locator,
239+
baseDir,
240+
...(virtual ? { readContent: content } : {}),
241+
source: virtual ? "openclaw-node" : "test",
242+
sourceInfo: {
243+
source: virtual ? "openclaw-node" : "test",
244+
path: locator,
245+
scope: "temporary",
246+
origin: "top-level",
247+
},
248+
disableModelInvocation: false,
212249
},
213-
disableModelInvocation: false,
214-
},
215-
],
216-
},
217-
});
250+
],
251+
},
252+
});
218253

219-
const result = await requireTool(tools, "read").execute("node-skill-read", {
220-
path: locator,
221-
});
254+
const result = await requireTool(tools, "read").execute("whole-skill-read", {
255+
path: locator,
256+
});
222257

223-
expect(JSON.stringify(result)).toContain("assembled-marker");
224-
});
258+
if (oversized) {
259+
expect(extractToolText(result)).toMatch(
260+
/(?:cannot|omitted|exceeds).*whole|whole.*(?:cannot|exceeds)|partially served/i,
261+
);
262+
expect(extractToolText(result)).not.toContain("# Pond");
263+
expect(Buffer.byteLength(extractToolText(result), "utf8")).toBeLessThanOrEqual(32 * 1024);
264+
return;
265+
}
266+
267+
expect(extractToolText(result)).toBe(content);
268+
},
269+
);
225270

226271
const testConfig: OpenClawConfig = {};
227272

@@ -1104,6 +1149,40 @@ describe("createOpenClawCodingTools", () => {
11041149
expect(names.has("message")).toBe(false);
11051150
});
11061151

1152+
it("continues oversized data through the assembled shell-disabled read tool", async () => {
1153+
const workspaceDir = tempDirs.make("openclaw-read-no-shell-");
1154+
const original = JSON.stringify({ generated: "x".repeat(52 * 1024) });
1155+
await fs.writeFile(path.join(workspaceDir, "generated.json"), original, "utf8");
1156+
const tools = createOpenClawCodingTools({
1157+
workspaceDir,
1158+
toolConstructionPlan: {
1159+
includeBaseCodingTools: true,
1160+
includeShellTools: false,
1161+
includeChannelTools: false,
1162+
includeOpenClawTools: false,
1163+
includePluginTools: false,
1164+
},
1165+
});
1166+
const names = new Set(tools.map((tool) => tool.name));
1167+
expect(names.has("exec")).toBe(false);
1168+
expect(names.has("process")).toBe(false);
1169+
const read = requireTool(tools, "read");
1170+
expect(read.description).not.toMatch(/\b(?:bash|sed|head)\b/);
1171+
1172+
const result = await read.execute("read-no-shell", { path: "generated.json" });
1173+
const text = extractToolText(result);
1174+
const continuation = (result.details as { continuation?: { cursor?: number } }).continuation;
1175+
expect(text).not.toMatch(/\b(?:bash|sed|head)\b/);
1176+
expect(Buffer.byteLength(text, "utf8")).toBeLessThanOrEqual(32 * 1024);
1177+
expect(continuation?.cursor).toBeGreaterThan(0);
1178+
expect(text).toContain(`cursor=${continuation?.cursor}`);
1179+
const next = await read.execute("read-no-shell-next", {
1180+
path: "generated.json",
1181+
cursor: continuation?.cursor,
1182+
});
1183+
expect(extractToolText(next)).toBe(original.slice(continuation?.cursor));
1184+
});
1185+
11071186
it("passes plugin suppression into OpenClaw tool construction plans", () => {
11081187
const createOpenClawToolsMock = vi.mocked(createOpenClawTools);
11091188
createOpenClawToolsMock.mockClear();
@@ -2733,6 +2812,37 @@ function extractToolText(result: unknown): string {
27332812
}
27342813

27352814
describe("createOpenClawCodingTools read behavior", () => {
2815+
it("protects materialized sandbox skill identities when no skill snapshot exists", async () => {
2816+
const root = tempDirs.make("openclaw-sandbox-skill-whole-");
2817+
const relativePath = "skills/demo/SKILL.md";
2818+
const filePath = path.join(root, relativePath);
2819+
await fs.mkdir(path.dirname(filePath), { recursive: true });
2820+
await fs.writeFile(filePath, "# Demo\ncomplete instructions", "utf8");
2821+
const sandbox = createAgentToolsSandboxContext({
2822+
workspaceDir: root,
2823+
fsBridge: createHostSandboxFsBridge(root),
2824+
});
2825+
const tools = createOpenClawCodingTools({
2826+
sandbox,
2827+
skillUsagePaths: [
2828+
{
2829+
readPath: `/workspace/${relativePath}`,
2830+
skillFile: filePath,
2831+
skillName: "demo",
2832+
skillSource: "workspace",
2833+
},
2834+
],
2835+
});
2836+
const read = requireTool(tools, "read");
2837+
2838+
expect(extractToolText(await read.execute("sandbox-skill", { path: relativePath }))).toBe(
2839+
"# Demo\ncomplete instructions",
2840+
);
2841+
await expect(
2842+
read.execute("sandbox-skill-window", { path: `/workspace/${relativePath}`, cursor: 0 }),
2843+
).rejects.toThrow(/whole|partial|window/i);
2844+
});
2845+
27362846
it("reads exact node skill locators without sending them to the filesystem backend", async () => {
27372847
const locator = "node://node-1/skills/pond/SKILL.md";
27382848
const execute = vi.fn(async () => {
@@ -2752,6 +2862,11 @@ describe("createOpenClawCodingTools read behavior", () => {
27522862
const result = await tool.execute("node-skill-read", { path: locator });
27532863

27542864
expect(extractToolText(result)).toContain("remote-marker");
2865+
for (const window of [{ offset: 1 }, { limit: 1 }, { cursor: 0 }]) {
2866+
await expect(
2867+
tool.execute("whole-skill-window", { path: locator, ...window }),
2868+
).rejects.toThrow(/whole|partial|window/i);
2869+
}
27552870
expect(execute).not.toHaveBeenCalled();
27562871
});
27572872

@@ -2914,11 +3029,35 @@ describe("createOpenClawCodingTools read behavior", () => {
29143029
expect(text).toContain("line-0001");
29153030
expect(text).toContain("[Read output capped at 32KB for this call. Use offset=");
29163031
expect(text).not.toContain("line-8000");
3032+
expect(Buffer.byteLength(text, "utf8")).toBeLessThanOrEqual(32 * 1024);
29173033
} finally {
29183034
await fs.rm(tmpDir, { recursive: true, force: true });
29193035
}
29203036
});
29213037

3038+
it.each([
3039+
{ name: "without an explicit limit", args: {} },
3040+
{ name: "with an explicit line limit", args: { limit: 1 } },
3041+
])("caps the first read page including its notice $name", async ({ args }) => {
3042+
const root = tempDirs.make("openclaw-read-first-page-cap-");
3043+
const original = "é🦞".repeat(9 * 1024);
3044+
await fs.writeFile(path.join(root, "unicode.txt"), original, "utf8");
3045+
const read = createSandboxedReadTool({ root, bridge: createHostSandboxFsBridge(root) });
3046+
3047+
const result = await read.execute("read-first-page-cap", { path: "unicode.txt", ...args });
3048+
const text = extractToolText(result);
3049+
const details = result.details as {
3050+
continuation?: { kind: string; offset: number; cursor: number };
3051+
};
3052+
3053+
expect(Buffer.byteLength(text, "utf8")).toBeLessThanOrEqual(32 * 1024);
3054+
expect(details.continuation).toMatchObject({ kind: "cursor", offset: 1 });
3055+
expect(text).toContain(`cursor=${details.continuation?.cursor}`);
3056+
expect(text.replace(/\n\n\[Read output capped[^\]]*\]$/, "")).toBe(
3057+
original.slice(0, details.continuation?.cursor),
3058+
);
3059+
});
3060+
29223061
it("describes explicit offsets beyond EOF", async () => {
29233062
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-read-offset-eof-"));
29243063
await fs.writeFile(path.join(tmpDir, "notes.txt"), "one\ntwo\nthree", "utf8");
@@ -3032,6 +3171,7 @@ describe("createOpenClawCodingTools read behavior", () => {
30323171
firstLineExceedsLimit: false,
30333172
content: "hidden duplicate payload",
30343173
},
3174+
continuation: { kind: "line", offset: 2 },
30353175
},
30363176
};
30373177
const baseRead: AgentTool = {

0 commit comments

Comments
 (0)