Skip to content

Commit 949e7b7

Browse files
vincentkocralf003
andcommitted
fix(read): explain directory paths
Co-authored-by: ralf003 <[email protected]>
1 parent 63651a1 commit 949e7b7

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import path from "node:path";
99
import type { AgentTool, AgentToolResult } from "openclaw/plugin-sdk/agent-core";
1010
import { Type } from "typebox";
1111
import { afterEach, describe, expect, it, vi } from "vitest";
12+
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
1213
import { replaceSessionEntry } from "../config/sessions/session-accessor.js";
1314
import type { SessionEntry } from "../config/sessions/types.js";
1415
import type { OpenClawConfig } from "../config/types.openclaw.js";
@@ -57,6 +58,7 @@ const tinyPngBuffer = Buffer.from(
5758
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO2f7z8AAAAASUVORK5CYII=",
5859
"base64",
5960
);
61+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
6062
const XAI_UNSUPPORTED_SCHEMA_KEYWORDS = new Set(["minContains", "maxContains"]);
6163
function collectActionValues(schema: unknown, values: Set<string>): void {
6264
if (!schema || typeof schema !== "object") {
@@ -2573,6 +2575,23 @@ describe("createOpenClawCodingTools read behavior", () => {
25732575
}
25742576
});
25752577

2578+
it("rejects sandbox directory reads before calling the bridge read operation", async () => {
2579+
const tmpDir = tempDirs.make("openclaw-sbx-directory-");
2580+
const directoryName = "notes";
2581+
await fs.mkdir(path.join(tmpDir, directoryName));
2582+
const hostBridge = createHostSandboxFsBridge(tmpDir);
2583+
const readFile = vi.fn(hostBridge.readFile.bind(hostBridge));
2584+
const readTool = createSandboxedReadTool({
2585+
root: tmpDir,
2586+
bridge: { ...hostBridge, readFile },
2587+
});
2588+
2589+
await expect(readTool.execute("sandbox-directory", { path: directoryName })).rejects.toThrow(
2590+
`Read requires a file path, but ${directoryName} is a directory. List the directory, then read a specific file.`,
2591+
);
2592+
expect(readFile).not.toHaveBeenCalled();
2593+
});
2594+
25762595
it("auto-pages read output across chunks when context window budget allows", async () => {
25772596
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-read-autopage-"));
25782597
const filePath = path.join(tmpDir, "big.txt");

‎src/agents/agent-tools.read.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,9 @@ async function assertSandboxFileExists(params: SandboxToolParams, absolutePath:
10901090
if (!stat) {
10911091
throw createFsAccessError("ENOENT", absolutePath);
10921092
}
1093+
if (stat.type === "directory") {
1094+
throw createFsAccessError("EISDIR", absolutePath);
1095+
}
10931096
}
10941097

10951098
function expandTildeToOsHome(filePath: string): string {

‎src/agents/sessions/tools/read.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ describe("read tool", () => {
119119
);
120120
});
121121

122+
it("explains that directory paths must be listed before reading a file", async () => {
123+
const tempDir = tempDirs.make("openclaw-read-directory-");
124+
const tool = createReadToolDefinition(tempDir);
125+
126+
await expect(
127+
tool.execute("call-directory", { path: "." }, undefined, undefined, {} as never),
128+
).rejects.toThrow(
129+
"Read requires a file path, but . is a directory. List the directory, then read a specific file.",
130+
);
131+
});
132+
122133
it("shell-quotes the long-first-line fallback path", async () => {
123134
// The fallback command is shown to the model; quote the path so suggested
124135
// follow-up commands cannot execute path text as shell syntax.

‎src/agents/sessions/tools/read.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { access as fsAccess, readFile as fsReadFile } from "node:fs/promises";
33
import { basename, dirname, isAbsolute, relative, resolve as resolvePath, sep } from "node:path";
44
import { Text } from "@earendil-works/pi-tui";
55
import { Type } from "typebox";
6-
import { toErrorObject } from "../../../infra/errors.js";
6+
import { hasErrnoCode, toErrorObject } from "../../../infra/errors.js";
77
import { decodeWindowsTextFileBuffer } from "../../../infra/windows-encoding.js";
88
import type { ImageContent, Model, TextContent } from "../../../llm/types.js";
99
import {
@@ -119,6 +119,16 @@ function createReadDetails(
119119
}
120120
return { kind: "text", content: text };
121121
}
122+
123+
function normalizeReadError(error: unknown, filePath: string): Error {
124+
if (hasErrnoCode(error, "EISDIR")) {
125+
return new Error(
126+
`Read requires a file path, but ${filePath} is a directory. List the directory, then read a specific file.`,
127+
);
128+
}
129+
return toErrorObject(error, "Non-Error rejection");
130+
}
131+
122132
interface CompactReadClassification {
123133
kind: "docs" | "resource" | "skill";
124134
label: string;
@@ -481,7 +491,7 @@ export function createReadToolDefinition(
481491
} catch (error: unknown) {
482492
signal?.removeEventListener("abort", onAbort);
483493
if (!aborted) {
484-
reject(toErrorObject(error, "Non-Error rejection"));
494+
reject(normalizeReadError(error, path));
485495
}
486496
}
487497
})();

0 commit comments

Comments
 (0)