Merge pull request #642 from mukhtharcm/fix/workspace-path-resolution
fix(tools): resolve Read/Write/Edit paths against workspace directory
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
- iMessage: fix reasoning persistence across DMs; avoid partial/duplicate replies when reasoning is enabled. (#655) — thanks @antons.
|
- iMessage: fix reasoning persistence across DMs; avoid partial/duplicate replies when reasoning is enabled. (#655) — thanks @antons.
|
||||||
- Models/Auth: allow MiniMax API configs without `models.providers.minimax.apiKey` (auth profiles / `MINIMAX_API_KEY`). (#656) — thanks @mneves75.
|
- Models/Auth: allow MiniMax API configs without `models.providers.minimax.apiKey` (auth profiles / `MINIMAX_API_KEY`). (#656) — thanks @mneves75.
|
||||||
- Agents: avoid duplicate replies when the message tool sends. (#659) — thanks @mickahouan.
|
- Agents: avoid duplicate replies when the message tool sends. (#659) — thanks @mickahouan.
|
||||||
|
- Agents/Tools: resolve workspace-relative Read/Write/Edit paths; align bash default cwd. (#642) — thanks @mukhtharcm.
|
||||||
- iOS/Android: enable stricter concurrency/lint checks; fix Swift 6 strict concurrency issues + Android lint errors (ExifInterface, obsolete SDK check). (#662) — thanks @KristijanJovanovski.
|
- iOS/Android: enable stricter concurrency/lint checks; fix Swift 6 strict concurrency issues + Android lint errors (ExifInterface, obsolete SDK check). (#662) — thanks @KristijanJovanovski.
|
||||||
- Docs: showcase entries for ParentPay, R2 Upload, iOS TestFlight, and Oura Health. (#650) — thanks @henrino3.
|
- Docs: showcase entries for ParentPay, R2 Upload, iOS TestFlight, and Oura Health. (#650) — thanks @henrino3.
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ export type BashToolDefaults = {
|
|||||||
elevated?: BashElevatedDefaults;
|
elevated?: BashElevatedDefaults;
|
||||||
allowBackground?: boolean;
|
allowBackground?: boolean;
|
||||||
scopeKey?: string;
|
scopeKey?: string;
|
||||||
|
cwd?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ProcessToolDefaults = {
|
export type ProcessToolDefaults = {
|
||||||
@@ -202,7 +203,8 @@ export function createBashTool(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const sandbox = elevatedRequested ? undefined : defaults?.sandbox;
|
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 workdir = rawWorkdir;
|
||||||
let containerWorkdir = sandbox?.containerWorkdir;
|
let containerWorkdir = sandbox?.containerWorkdir;
|
||||||
if (sandbox) {
|
if (sandbox) {
|
||||||
|
|||||||
@@ -849,6 +849,7 @@ export async function compactEmbeddedPiSession(params: {
|
|||||||
agentAccountId: params.agentAccountId,
|
agentAccountId: params.agentAccountId,
|
||||||
sessionKey: params.sessionKey ?? params.sessionId,
|
sessionKey: params.sessionKey ?? params.sessionId,
|
||||||
agentDir,
|
agentDir,
|
||||||
|
workspaceDir: effectiveWorkspace,
|
||||||
config: params.config,
|
config: params.config,
|
||||||
abortSignal: runAbortController.signal,
|
abortSignal: runAbortController.signal,
|
||||||
modelProvider: model.provider,
|
modelProvider: model.provider,
|
||||||
@@ -1232,6 +1233,7 @@ export async function runEmbeddedPiAgent(params: {
|
|||||||
agentAccountId: params.agentAccountId,
|
agentAccountId: params.agentAccountId,
|
||||||
sessionKey: params.sessionKey ?? params.sessionId,
|
sessionKey: params.sessionKey ?? params.sessionId,
|
||||||
agentDir,
|
agentDir,
|
||||||
|
workspaceDir: effectiveWorkspace,
|
||||||
config: params.config,
|
config: params.config,
|
||||||
abortSignal: runAbortController.signal,
|
abortSignal: runAbortController.signal,
|
||||||
modelProvider: model.provider,
|
modelProvider: model.provider,
|
||||||
|
|||||||
@@ -419,4 +419,98 @@ describe("createClawdbotCodingTools", () => {
|
|||||||
expect(violations).toEqual([]);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -531,6 +531,7 @@ export function createClawdbotCodingTools(options?: {
|
|||||||
sandbox?: SandboxContext | null;
|
sandbox?: SandboxContext | null;
|
||||||
sessionKey?: string;
|
sessionKey?: string;
|
||||||
agentDir?: string;
|
agentDir?: string;
|
||||||
|
workspaceDir?: string;
|
||||||
config?: ClawdbotConfig;
|
config?: ClawdbotConfig;
|
||||||
abortSignal?: AbortSignal;
|
abortSignal?: AbortSignal;
|
||||||
/**
|
/**
|
||||||
@@ -571,20 +572,30 @@ export function createClawdbotCodingTools(options?: {
|
|||||||
]);
|
]);
|
||||||
const sandboxRoot = sandbox?.workspaceDir;
|
const sandboxRoot = sandbox?.workspaceDir;
|
||||||
const allowWorkspaceWrites = sandbox?.workspaceAccess !== "ro";
|
const allowWorkspaceWrites = sandbox?.workspaceAccess !== "ro";
|
||||||
|
const workspaceRoot = options?.workspaceDir ?? process.cwd();
|
||||||
|
|
||||||
const base = (codingTools as unknown as AnyAgentTool[]).flatMap((tool) => {
|
const base = (codingTools as unknown as AnyAgentTool[]).flatMap((tool) => {
|
||||||
if (tool.name === readTool.name) {
|
if (tool.name === readTool.name) {
|
||||||
return sandboxRoot
|
if (sandboxRoot) {
|
||||||
? [createSandboxedReadTool(sandboxRoot)]
|
return [createSandboxedReadTool(sandboxRoot)];
|
||||||
: [createClawdbotReadTool(tool)];
|
}
|
||||||
|
const freshReadTool = createReadTool(workspaceRoot);
|
||||||
|
return [createClawdbotReadTool(freshReadTool)];
|
||||||
}
|
}
|
||||||
if (tool.name === bashToolName) return [];
|
if (tool.name === bashToolName) return [];
|
||||||
if (sandboxRoot && (tool.name === "write" || tool.name === "edit")) {
|
if (tool.name === "write") {
|
||||||
return [];
|
if (sandboxRoot) return [];
|
||||||
|
return [createWriteTool(workspaceRoot)];
|
||||||
|
}
|
||||||
|
if (tool.name === "edit") {
|
||||||
|
if (sandboxRoot) return [];
|
||||||
|
return [createEditTool(workspaceRoot)];
|
||||||
}
|
}
|
||||||
return [tool as AnyAgentTool];
|
return [tool as AnyAgentTool];
|
||||||
});
|
});
|
||||||
const bashTool = createBashTool({
|
const bashTool = createBashTool({
|
||||||
...options?.bash,
|
...options?.bash,
|
||||||
|
cwd: options?.workspaceDir,
|
||||||
allowBackground,
|
allowBackground,
|
||||||
scopeKey,
|
scopeKey,
|
||||||
sandbox: sandbox
|
sandbox: sandbox
|
||||||
|
|||||||
Reference in New Issue
Block a user