Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 211 additions & 15 deletions apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ const resumed = (
...(options?.version === "2.2.5" || options?.version === "2.2.6"
? [out("rpc.call", "<any>"), reply("rpc.call", { output: null })]
: []),
out("agent.list", "<any>"),
reply("agent.list", agentList),
...(options?.supervised === true
? [
out("session.update", { sessionID: SESSION, permissions: supervisedRules }),
reply("session.update", null),
]
: []),
...(options?.shellRecovery ??
(options?.version === undefined
? []
Expand All @@ -359,17 +367,7 @@ const resumed = (
? [out("job.list", { kind: "shell" }), reply("job.list", { data: [] })]
: emptyShellInventory),
])),
...withInstructions([
...(options?.supervised === true
? [
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.update", { sessionID: SESSION, permissions: supervisedRules }),
reply("session.update", null),
]
: []),
...entries,
]),
...withInstructions(entries),
],
{
...(options?.version === undefined ? {} : { version: options.version }),
Expand Down Expand Up @@ -1531,8 +1529,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
const { runtime, thread } = yield* resumed([
...backgroundLaunch(CHILD),
event("session.execution.succeeded", { sessionID: SESSION }),
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.update", { sessionID: SESSION, permissions: supervisedRules }),
reply("session.update", null),
out("session.update", {
Expand Down Expand Up @@ -1831,6 +1827,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
replyData("permission.list", []),
out("session.form.list", { sessionID: CHILD }),
replyData("session.form.list", []),
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
reply("session.update", null),
out("shell.list", "<any>"),
reply("shell.list", response([shell])),
out("shell.get", { id: shell.id }),
Expand All @@ -1840,8 +1838,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
"shell.output",
response({ output: "done", cursor: 4, size: 4, truncated: false }),
),
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
reply("session.update", null),
]
: []),
...launchedAndRunning,
Expand Down Expand Up @@ -2852,6 +2848,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}),
),
...noOpenRequests,
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.update", { sessionID: SESSION, permissions: t3Rules }),
reply("session.update", null),
]);
Expand All @@ -2872,6 +2870,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
out("session.get", { sessionID: SESSION }),
replyData("session.get", sessionInfo()),
...noOpenRequests,
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.move", { sessionID: SESSION, directory: "/work/opencode2-feature" }),
reply("session.move", null),
]);
Expand Down Expand Up @@ -2927,6 +2927,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
reply("permission.reply", null),
out("session.form.cancel", { sessionID: SESSION, formID: colorForm.id }),
reply("session.form.cancel", null),
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.active"),
reply("session.active", { data: {} }),
event("session.execution.interrupted", { sessionID: SESSION, reason: "user" }),
Expand Down Expand Up @@ -3143,6 +3145,164 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}),
);

it.effect.each([
["auto", true],
["full-access", true],
["approval-required", true],
["auto-accept-edits", true],
["auto", false],
] as const)(
"preserves OpenCode permissions in %s (configured: %s)",
([runtimeMode, configured]) =>
Effect.gen(function* () {
const nativeRules = configured
? [
{ action: "shell", resource: "*", effect: "allow" },
{ action: "shell", resource: "git push *", effect: "ask" },
{ action: "shell", resource: "rm *", effect: "deny" },
{ action: "read", resource: "*.env*", effect: "deny" },
{ action: "read", resource: "*.env.example", effect: "allow" },
{ action: "webfetch", resource: "*", effect: "ask" },
]
: [];
const permissions = [
...(runtimeMode === "auto"
? [...supervisedRules.slice(configured ? 1 : 0, 3), ...buildPaths, ...nativeRules]
: runtimeMode === "full-access"
? [
t3Rules[0],
...nativeRules.slice(2, -1),
{ action: "webfetch", resource: "*", effect: "allow" },
]
: [
{ action: "shell", resource: "*", effect: "ask" },
{
action: "edit",
resource: "*",
effect: runtimeMode === "auto-accept-edits" ? "allow" : "ask",
},
{ action: "external_directory", resource: "*", effect: "ask" },
...buildPaths,
]),
...mcpRules,
];
const runtime = yield* openCode2ReplayRuntime([
...opening,
out("agent.list", "<any>"),
reply("agent.list", {
...agentList,
data: agentList.data.map((agent) => ({
...agent,
permissions: [...agent.permissions, ...nativeRules],
})),
}),
out("session.create", {
location: { directory: WORK },
model: { providerID: "opencode", id: "big-pickle" },
permissions,
}),
replyData("session.create", sessionInfo({ permissions })),
]);
const thread = yield* runtime.ensureThread({
threadId,
modelSelection: bigPickle,
runtimePolicy: { ...policy(), runtimeMode },
});
assert.equal(thread.nativeThreadRef?.nativeId, SESSION);
}).pipe(Effect.scoped),
);

it.effect.each(["auto", "full-access"] as const)(
"retains ordered native edit denies and exceptions in %s plan mode",
(runtimeMode) =>
Effect.gen(function* () {
const privatePath = { action: "edit", resource: "/plans/private/*", effect: "allow" };
const denied = { action: "edit", resource: "/plans/*", effect: "deny" };
const allowed = { action: "edit", resource: "/plans/allowed.md", effect: "allow" };
const externalDeny = {
action: "external_directory",
resource: "/private/*",
effect: "deny",
};
const externalAsk = {
action: "external_directory",
resource: "/private/reference/*",
effect: "ask",
};
const externalFallback = { ...externalAsk, resource: "*" };
const permissions = [
...(runtimeMode === "full-access" ? [t3Rules[0]] : [supervisedRules[0]]),
// Native outside-directory rules keep their order, ahead of any session grant.
externalDeny,
...(runtimeMode === "full-access"
? [
{ ...externalAsk, effect: "allow" },
{ ...externalFallback, effect: "allow" },
]
: [externalAsk, externalFallback]),
{ action: "edit", resource: "*", effect: "deny" },
...buildPaths,
privatePath,
denied,
allowed,
...mcpRules,
];
const runtime = yield* openCode2ReplayRuntime([
...opening,
out("agent.list", "<any>"),
reply("agent.list", {
...agentList,
data: agentList.data.map((agent) => ({
...agent,
permissions: [
...agent.permissions,
privatePath,
{ ...denied, effect: "allow" },
denied,
allowed,
externalDeny,
externalAsk,
externalFallback,
],
})),
}),
out("session.create", {
location: { directory: WORK },
model: { providerID: "opencode", id: "big-pickle" },
permissions,
}),
replyData("session.create", sessionInfo({ permissions })),
]);
const thread = yield* runtime.ensureThread({
threadId,
modelSelection: bigPickle,
runtimePolicy: { ...policy(), runtimeMode, interactionMode: "plan" },
});
assert.equal(thread.nativeThreadRef?.nativeId, SESSION);
}).pipe(Effect.scoped),
);

it.effect("does not create an unrestricted session when native permissions cannot be read", () =>
Effect.gen(function* () {
const runtime = yield* openCode2ReplayRuntime([
...opening,
out("agent.list", "<any>"),
reply("agent.list", {
status: 500,
body: { _tag: "UnknownError", message: "Cannot load permissions" },
}),
]);
const failed = yield* runtime
.ensureThread({
threadId,
modelSelection: bigPickle,
runtimePolicy: policy(),
})
.pipe(Effect.flip);
assert.equal(failed._tag, "ProviderAdapterEnsureThreadError");
}).pipe(Effect.scoped),
);

it.effect("reads the agents again while a fresh server still lists none", () =>
Effect.gen(function* () {
// OpenCode 2.0.18 briefly returns no agents on startup; using that response drops agent paths.
Expand Down Expand Up @@ -4084,6 +4244,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {

it.effect("keeps a turn still running after a reconnect open for its next events", () =>
Effect.gen(function* () {
const denied = { action: "shell", resource: "*", effect: "deny" };
const { runtime, thread } = yield* resumed([
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
promptAccepted,
Expand All @@ -4096,6 +4257,23 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
text: "Arrived on the new stream.",
}),
event("session.execution.succeeded", { sessionID: SESSION }),
...directoryModels(WORK),
out("agent.list", "<any>"),
reply("agent.list", {
...agentList,
data: agentList.data.map((agent) => ({
...agent,
permissions: [...agent.permissions, denied],
})),
}),
out("session.update", {
sessionID: SESSION,
permissions: [t3Rules[0], denied, ...mcpRules],
}),
reply("session.update", null),
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
promptAccepted,
event("session.execution.succeeded", { sessionID: SESSION }),
]);
const events = yield* runtime.events.pipe(
Stream.takeUntil((event) => event.type === "turn.terminal"),
Expand All @@ -4114,6 +4292,9 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
"Arrived on the new stream.",
]);
assert.deepInclude(collected.at(-1), { type: "turn.terminal", status: "completed" });
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
yield* runtime.startTurn({ ...turnInput(thread), runOrdinal: 2, providerTurnOrdinal: 2 });
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
}),
);

Expand Down Expand Up @@ -5016,6 +5197,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}),
),
...noOpenRequests,
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("mcp.add", {
server,
"location[directory]": WORK,
Expand Down Expand Up @@ -5387,6 +5570,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
out("session.get", { sessionID: SESSION }),
replyData("session.get", sessionInfo()),
...noOpenRequests,
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
promptAccepted,
event("session.execution.succeeded", { sessionID: SESSION }),
Expand Down Expand Up @@ -5746,6 +5931,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
limit: { context: 64000, input: 48000, output: 8000 },
})),
}),
out("agent.list", { "location[directory]": custom }),
reply("agent.list", { ...agentList, location: { directory: custom } }),
out("session.create", {
location: { directory: custom },
model: { providerID: "opencode", id: "big-pickle" },
Expand Down Expand Up @@ -6432,6 +6619,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
out("session.get", { sessionID: SESSION }),
replyData("session.get", sessionInfo()),
...noOpenRequests,
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("message.list", "<any>"),
reply("message.list", {
data: [
Expand Down Expand Up @@ -6598,6 +6787,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
const { runtime, thread } = yield* resumed([
out("session.fork", { sessionID: SESSION }),
replyData("session.fork", sessionInfo({ id: FORK })),
out("agent.list", { "location[directory]": target }),
reply("agent.list", { ...agentList, location: { directory: target } }),
out("session.update", {
sessionID: FORK,
permissions: [
Expand Down Expand Up @@ -7328,6 +7519,9 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
sessionID: SESSION,
model: { id: "buffered", providerID: "opencode" },
}),
...directoryModels(WORK),
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.switchModel", { sessionID: SESSION, model: "<any>" }),
reply("session.switchModel", null),
...backgroundLaunch(CHILD),
Expand Down Expand Up @@ -7807,6 +8001,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
input: { sessionID: SESSION },
}),
reply("rpc.call", { output: nativeGoal("paused") }),
out("agent.list", "<any>"),
reply("agent.list", agentList),
...emptyShellInventory,
],
{ version: "2.2.5" },
Expand Down
Loading
Loading