fix(tools): resolve Read/Write/Edit paths against workspace directory

Previously, Read/Write/Edit tools used the global tool instances from
pi-coding-agent which had process.cwd() baked in at import time. Since
the gateway starts from /root/dev/ai/clawdbot, relative paths like
'SOUL.md' would incorrectly resolve there instead of the agent's
workspace (/root/clawd).

This fix:
- Adds workspaceDir option to createClawdbotCodingTools
- Creates fresh Read/Write/Edit tools bound to workspaceDir
- Adds cwd option to Bash tool defaults for consistency
- Passes effectiveWorkspace from pi-embedded-runner

Absolute paths and ~/... paths are unaffected. Sandboxed sessions
continue to use sandbox root as before.

Includes tests for Read/Write/Edit workspace path resolution.
This commit is contained in:
Muhammed Mukhthar CM
2026-01-10 05:36:09 +00:00
committed by Peter Steinberger
parent bf0184d0cf
commit de5b75eff6
4 changed files with 115 additions and 6 deletions

View File

@@ -61,6 +61,7 @@ export type BashToolDefaults = {
elevated?: BashElevatedDefaults;
allowBackground?: boolean;
scopeKey?: string;
cwd?: string;
};
export type ProcessToolDefaults = {
@@ -202,7 +203,8 @@ export function createBashTool(
}
const sandbox = elevatedRequested ? undefined : defaults?.sandbox;
const rawWorkdir = params.workdir?.trim() || process.cwd();
const rawWorkdir =
params.workdir?.trim() || defaults?.cwd || process.cwd();
let workdir = rawWorkdir;
let containerWorkdir = sandbox?.containerWorkdir;
if (sandbox) {

View File

@@ -849,6 +849,7 @@ export async function compactEmbeddedPiSession(params: {
agentAccountId: params.agentAccountId,
sessionKey: params.sessionKey ?? params.sessionId,
agentDir,
workspaceDir: effectiveWorkspace,
config: params.config,
abortSignal: runAbortController.signal,
modelProvider: model.provider,
@@ -1232,6 +1233,7 @@ export async function runEmbeddedPiAgent(params: {
agentAccountId: params.agentAccountId,
sessionKey: params.sessionKey ?? params.sessionId,
agentDir,
workspaceDir: effectiveWorkspace,
config: params.config,
abortSignal: runAbortController.signal,
modelProvider: model.provider,

View File

@@ -419,4 +419,98 @@ describe("createClawdbotCodingTools", () => {
expect(violations).toEqual([]);
}
});
it("uses workspaceDir for Read tool path resolution", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-ws-"));
try {
// Create a test file in the "workspace"
const testFile = "test-workspace-file.txt";
const testContent = "workspace path resolution test";
await fs.writeFile(path.join(tmpDir, testFile), testContent, "utf8");
// Create tools with explicit workspaceDir
const tools = createClawdbotCodingTools({ workspaceDir: tmpDir });
const readTool = tools.find((tool) => tool.name === "read");
expect(readTool).toBeDefined();
// Read using relative path - should resolve against workspaceDir
const result = await readTool?.execute("tool-ws-1", {
path: testFile,
});
const textBlocks = result?.content?.filter(
(block) => block.type === "text",
) as Array<{ text?: string }> | undefined;
const combinedText = textBlocks
?.map((block) => block.text ?? "")
.join("\n");
expect(combinedText).toContain(testContent);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it("uses workspaceDir for Write tool path resolution", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-ws-"));
try {
const testFile = "test-write-file.txt";
const testContent = "written via workspace path";
// Create tools with explicit workspaceDir
const tools = createClawdbotCodingTools({ workspaceDir: tmpDir });
const writeTool = tools.find((tool) => tool.name === "write");
expect(writeTool).toBeDefined();
// Write using relative path - should resolve against workspaceDir
await writeTool?.execute("tool-ws-2", {
path: testFile,
content: testContent,
});
// Verify file was written to workspaceDir
const written = await fs.readFile(path.join(tmpDir, testFile), "utf8");
expect(written).toBe(testContent);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it("uses workspaceDir for Edit tool path resolution", async () => {
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-ws-"));
try {
const testFile = "test-edit-file.txt";
const originalContent = "hello world";
const expectedContent = "hello universe";
await fs.writeFile(path.join(tmpDir, testFile), originalContent, "utf8");
// Create tools with explicit workspaceDir
const tools = createClawdbotCodingTools({ workspaceDir: tmpDir });
const editTool = tools.find((tool) => tool.name === "edit");
expect(editTool).toBeDefined();
// Edit using relative path - should resolve against workspaceDir
await editTool?.execute("tool-ws-3", {
path: testFile,
oldText: "world",
newText: "universe",
});
// Verify file was edited in workspaceDir
const edited = await fs.readFile(path.join(tmpDir, testFile), "utf8");
expect(edited).toBe(expectedContent);
} finally {
await fs.rm(tmpDir, { recursive: true, force: true });
}
});
it("falls back to process.cwd() when workspaceDir not provided", () => {
const prevCwd = process.cwd();
const tools = createClawdbotCodingTools();
// Tools should be created without error
expect(tools.some((tool) => tool.name === "read")).toBe(true);
expect(tools.some((tool) => tool.name === "write")).toBe(true);
expect(tools.some((tool) => tool.name === "edit")).toBe(true);
// cwd should be unchanged
expect(process.cwd()).toBe(prevCwd);
});
});

View File

@@ -531,6 +531,7 @@ export function createClawdbotCodingTools(options?: {
sandbox?: SandboxContext | null;
sessionKey?: string;
agentDir?: string;
workspaceDir?: string;
config?: ClawdbotConfig;
abortSignal?: AbortSignal;
/**
@@ -571,20 +572,30 @@ export function createClawdbotCodingTools(options?: {
]);
const sandboxRoot = sandbox?.workspaceDir;
const allowWorkspaceWrites = sandbox?.workspaceAccess !== "ro";
const workspaceRoot = options?.workspaceDir ?? process.cwd();
const base = (codingTools as unknown as AnyAgentTool[]).flatMap((tool) => {
if (tool.name === readTool.name) {
return sandboxRoot
? [createSandboxedReadTool(sandboxRoot)]
: [createClawdbotReadTool(tool)];
if (sandboxRoot) {
return [createSandboxedReadTool(sandboxRoot)];
}
const freshReadTool = createReadTool(workspaceRoot);
return [createClawdbotReadTool(freshReadTool)];
}
if (tool.name === bashToolName) return [];
if (sandboxRoot && (tool.name === "write" || tool.name === "edit")) {
return [];
if (tool.name === "write") {
if (sandboxRoot) return [];
return [createWriteTool(workspaceRoot)];
}
if (tool.name === "edit") {
if (sandboxRoot) return [];
return [createEditTool(workspaceRoot)];
}
return [tool as AnyAgentTool];
});
const bashTool = createBashTool({
...options?.bash,
cwd: options?.workspaceDir,
allowBackground,
scopeKey,
sandbox: sandbox