fix(agent): isolate conversation workspaces
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { type OpencodeClient } from "@opencode-ai/sdk/v2";
|
||||
import { mkdtemp, readdir, rm, stat } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, relative } from "node:path";
|
||||
|
||||
import { config } from "../../src/config.js";
|
||||
import { OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
|
||||
@@ -87,6 +90,79 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("OpencodeRuntimeAdapter.createSession", () => {
|
||||
it("creates a real chat session inside a dedicated conversation workspace", async () => {
|
||||
const workspaceRoot = await mkdtemp(join(tmpdir(), "tjwater-conversations-"));
|
||||
const calls: Array<Record<string, unknown>> = [];
|
||||
const client = {
|
||||
session: {
|
||||
create: async (input: Record<string, unknown>) => {
|
||||
calls.push(input);
|
||||
return {
|
||||
data: {
|
||||
id: "runtime-session-1",
|
||||
directory: input.directory,
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
} as unknown as OpencodeClient;
|
||||
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
|
||||
clientPromise: null,
|
||||
closeServer: null,
|
||||
ensureClient: async () => client,
|
||||
}) as OpencodeRuntimeAdapter;
|
||||
|
||||
try {
|
||||
const session = await runtime.createSession("chat", {
|
||||
conversationWorkspace: true,
|
||||
workspaceRoot,
|
||||
});
|
||||
const directory = String(calls[0]?.directory);
|
||||
|
||||
expect(relative(workspaceRoot, directory).startsWith("..")).toBe(false);
|
||||
const workspaceStat = await stat(directory);
|
||||
expect(workspaceStat.isDirectory()).toBe(true);
|
||||
expect(workspaceStat.mode & 0o777).toBe(0o700);
|
||||
expect(session.directory).toBe(directory);
|
||||
expect(calls[0]?.permission).toEqual([
|
||||
{ permission: "read", pattern: `${directory}/**`, action: "allow" },
|
||||
{ permission: "edit", pattern: `${directory}/**`, action: "ask" },
|
||||
]);
|
||||
} finally {
|
||||
await rm(workspaceRoot, { force: true, recursive: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("removes an empty conversation workspace when session creation fails", async () => {
|
||||
const workspaceRoot = await mkdtemp(join(tmpdir(), "tjwater-conversations-"));
|
||||
const client = {
|
||||
session: {
|
||||
create: async () => {
|
||||
throw new Error("session creation failed");
|
||||
},
|
||||
},
|
||||
} as unknown as OpencodeClient;
|
||||
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
|
||||
clientPromise: null,
|
||||
closeServer: null,
|
||||
ensureClient: async () => client,
|
||||
}) as OpencodeRuntimeAdapter;
|
||||
|
||||
try {
|
||||
await expect(
|
||||
runtime.createSession("chat", {
|
||||
conversationWorkspace: true,
|
||||
workspaceRoot,
|
||||
}),
|
||||
).rejects.toThrow("session creation failed");
|
||||
expect(await readdir(workspaceRoot)).toEqual([]);
|
||||
} finally {
|
||||
await rm(workspaceRoot, { force: true, recursive: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("OpencodeRuntimeAdapter.warmup", () => {
|
||||
it("initializes the project session and model tools before reporting ready", async () => {
|
||||
const calls: string[] = [];
|
||||
|
||||
Reference in New Issue
Block a user