Skip to content
Closed
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
139 changes: 134 additions & 5 deletions apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,23 @@ const agentList = {
hidden: false,
permissions: [{ action: "*", resource: "*", effect: "allow" }, ...buildPaths],
},
{
id: "reader",
name: "Reader",
request: { settings: {}, headers: {}, body: {} },
description: "Reads anything, edits only src.",
mode: "subagent",
hidden: false,
permissions: [
{ action: "*", resource: "*", effect: "allow" },
{ action: "*", resource: "*", effect: "deny" },
{ action: "e*", resource: "src/**", effect: "allow" },
{ action: "shell *", resource: "*", effect: "allow" },
],
},
],
};
const listAgents = [out("agent.list", "<any>"), reply("agent.list", agentList)];
const supervisedRules = [
{ action: "shell", resource: "*", effect: "ask" },
{ action: "edit", resource: "*", effect: "ask" },
Expand Down Expand Up @@ -893,7 +908,10 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
agent: "general",
model: { id: "big-pickle", providerID: "opencode", variant: "default" },
});
const backgroundLaunch = (child: string): ReadonlyArray<ProviderReplayEntry> => {
const backgroundLaunch = (
child: string,
created: Record<string, unknown> = childCreated(child),
): ReadonlyArray<ProviderReplayEntry> => {
const call = "call-background";
const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: call };
return [
Expand All @@ -907,7 +925,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
input: { description: "Sleep", prompt: "sleep", background: true },
executed: false,
}),
event("session.created", childCreated(child)),
event("session.created", created),
event("session.tool.progress", {
...tool,
metadata: { sessionID: child, status: "running" },
Expand Down Expand Up @@ -1504,6 +1522,93 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}),
);

// OpenCode copies the parent's session rules into a child, and session rules outrank agent rules.
it.effect("keeps a subagent's own agent denies under the Full access rules it inherits", () =>
Effect.gen(function* () {
const editDeny = { action: "edit", resource: "*", effect: "deny" };
const { runtime, thread } = yield* resumed([
...backgroundLaunch(CHILD, {
...childCreated(CHILD),
agent: "review",
permissions: t3Rules,
}),
out("agent.list", "<any>"),
reply("agent.list", {
...agentList,
data: [
...agentList.data,
{
...agentList.data[0]!,
id: "review",
name: "review",
mode: "subagent",
permissions: [{ action: "*", resource: "*", effect: "allow" }, editDeny],
},
],
}),
out("session.update", {
sessionID: CHILD,
permissions: [t3Rules[0], editDeny, ...mcpRules],
}),
reply("session.update", null),
out("session.interrupt", { sessionID: SESSION }),
reply("session.interrupt", { interrupted: true }),
event("session.execution.interrupted", { sessionID: SESSION }),
]);
const watch = yield* watchBackgroundTurn(runtime);
yield* runtime.startTurn(withLineage(thread));
yield* watch.attached;
yield* runtime.interruptTurn({
providerThread: thread,
providerTurnId: yield* providerTurnId,
});
assert.equal((yield* watch.terminal)?.status, "interrupted");
}),
);

it.effect("keeps a subagent's own agent denies and T3's asks under Supervised rules", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed(
[
...backgroundLaunch(CHILD, {
...childCreated(CHILD),
agent: "reader",
permissions: supervisedRules,
}),
out("session.update", {
sessionID: CHILD,
permissions: [
...supervisedRules.slice(0, 3),
{ action: "*", resource: "*", effect: "deny" },
{ action: "e*", resource: "src/**", effect: "allow" },
{ action: "edit", resource: "src/**", effect: "ask" },
{ action: "external_directory", resource: "src/**", effect: "ask" },
{ action: "shell *", resource: "*", effect: "allow" },
{ action: "shell", resource: "*", effect: "ask" },
...mcpRules,
],
}),
reply("session.update", null),
out("session.interrupt", { sessionID: SESSION }),
reply("session.interrupt", { interrupted: true }),
event("session.execution.interrupted", { sessionID: SESSION }),
],
{ supervised: true },
);
const watch = yield* watchBackgroundTurn(runtime);
yield* runtime.startTurn({
...withLineage(thread),
runtimePolicy: policy("approval-required"),
});
yield* watch.attached;
yield* runtime.interruptTurn({
providerThread: thread,
providerTurnId: yield* providerTurnId,
});
assert.equal((yield* watch.terminal)?.status, "interrupted");
}),
);

it.effect("stops background subagents on a user Stop", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed([
Expand Down Expand Up @@ -1696,6 +1801,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
// The adapter writes a child's rules only when they changed.
...(writesRules
? [
...listAgents,
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
reply("session.update", null),
]
Expand Down Expand Up @@ -1902,7 +2008,29 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
);

// regression: #21979 a failed background subagent pinned its thread as busy
it.effect("settles the background call a foreground call joined to a failed child", () =>
it.effect.each([
{
name: "",
rules: [
...listAgents,
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
reply("session.update", null),
],
},
{
// The chosen policy still applies when OpenCode cannot list the subagent's agent.
name: " while its agent's rules cannot be read",
rules: [
out("agent.list", "<any>"),
reply("agent.list", {
status: 502,
body: { _tag: "UnknownError", message: "bad gateway" },
}),
out("session.update", { sessionID: CHILD, permissions: t3Rules }),
reply("session.update", null),
],
},
])("settles the background call a foreground call joined to a failed child$name", ({ rules }) =>
Effect.gen(function* () {
const again = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-again" };
const { runtime, thread } = yield* resumed([
Expand All @@ -1921,8 +2049,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
...again,
metadata: { sessionID: CHILD, status: "running" },
}),
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
reply("session.update", null),
...rules,
// The child fails while the joined call is still the foreground one.
event("session.execution.failed", {
sessionID: CHILD,
Expand Down Expand Up @@ -2326,6 +2453,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
...toolOf(SESSION, "call-other"),
metadata: { sessionID: OTHER, status: "running" },
}),
...listAgents,
event("session.tool.success", {
...toolOf(SESSION, "call-other"),
content: [{ type: "text", text: "OTHER_OK" }],
Expand Down Expand Up @@ -2712,6 +2840,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
[
...backgroundLaunch(CHILD),
event("session.execution.succeeded", { sessionID: SESSION }),
...listAgents,
out("session.update", { sessionID: CHILD, permissions: t3Rules }),
reply("session.update", null),
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
{"type": "emit_inbound", "label": "session.step.streamed", "frame": {"type": "sdk.event", "event": {"id": "evt_0f73f91d5001vrzflyt1WQFGva", "created": 1790854533589, "type": "session.step.streamed", "location": {"directory": "<work>"}, "data": {"sessionID": "ses_f08c08821ffefFAD3sMjkyP0Fd", "assistantMessageID": "msg_0f73f7b54001L2fnmF6VSokmbn"}, "durable": {"aggregateID": "ses_f08c08821ffefFAD3sMjkyP0Fd", "seq": 11, "version": 1}}}}
{"type": "emit_inbound", "label": "session.created.2", "frame": {"type": "sdk.event", "event": {"id": "evt_0f73f91dd001BBDOI1lGk77sTL", "created": 1790854533597, "type": "session.created", "location": {"directory": "<work>"}, "data": {"sessionID": "ses_f08c06e25ffeF0asx24ydmo3oy", "slug": "kind-forest", "version": "2.0.18", "projectID": "3b9597874c088ee0849b0d68c211296e47876139", "parentID": "ses_f08c08821ffefFAD3sMjkyP0Fd", "location": {"directory": "<work>"}, "subpath": "", "title": "Chain subagents with background", "agent": "general", "permissions": [{"action": "*", "resource": "*", "effect": "allow"}], "model": {"id": "deepseek/deepseek-v4-flash", "providerID": "openrouter", "variant": "default"}}, "durable": {"aggregateID": "ses_f08c06e25ffeF0asx24ydmo3oy", "seq": 0, "version": 1}}}}
{"type": "emit_inbound", "label": "session.tool.progress", "frame": {"type": "sdk.event", "event": {"id": "evt_0f73f91e00015k1c4ULxR6t2Sb", "created": 1790854533600, "type": "session.tool.progress", "location": {"directory": "<work>"}, "data": {"sessionID": "ses_f08c08821ffefFAD3sMjkyP0Fd", "assistantMessageID": "msg_0f73f7b54001L2fnmF6VSokmbn", "id": "call_3d90a8b7d44c49f6ae053fd7", "metadata": {"sessionID": "ses_f08c06e25ffeF0asx24ydmo3oy", "status": "running"}}}}}
{"type": "expect_outbound", "label": "agent.list", "frame": {"type": "agent.list", "input": {"location[directory]": "<any>"}}}
{"type": "emit_inbound", "label": "agent.list.response", "frame": {"type": "sdk.response", "operation": "agent.list", "data": {"location": {"directory": "<work>"}, "data": [{"id": "build", "name": "Build", "request": {"settings": {}, "headers": {}, "body": {}}, "description": "The default agent. Executes tools based on configured permissions.", "mode": "primary", "hidden": false, "permissions": [{"action": "*", "resource": "*", "effect": "allow"}, {"action": "external_directory", "resource": "*", "effect": "ask"}, {"action": "read", "resource": "*.env", "effect": "ask"}, {"action": "read", "resource": "*.env.*", "effect": "ask"}, {"action": "read", "resource": "*.env.example", "effect": "allow"}, {"action": "external_directory", "resource": "<home>/data/opencode/shell/*/*", "effect": "allow"}, {"action": "external_directory", "resource": "<home>/data/opencode/tool-output/*", "effect": "allow"}, {"action": "external_directory", "resource": "/tmp/opencode/*", "effect": "allow"}, {"action": "external_directory", "resource": "<home>/config/opencode/*", "effect": "allow"}, {"action": "question", "resource": "*", "effect": "allow"}]}, {"id": "plan", "name": "Plan", "request": {"settings": {}, "headers": {}, "body": {}}, "description": "Read-only agent for exploring the codebase and planning work before implementation.", "mode": "primary", "hidden": false, "permissions": [{"action": "*", "resource": "*", "effect": "allow"}, {"action": "external_directory", "resource": "*", "effect": "ask"}, {"action": "read", "resource": "*.env", "effect": "ask"}, {"action": "read", "resource": "*.env.*", "effect": "ask"}, {"action": "read", "resource": "*.env.example", "effect": "allow"}, {"action": "external_directory", "resource": "<home>/data/opencode/shell/*/*", "effect": "allow"}, {"action": "external_directory", "resource": "<home>/data/opencode/tool-output/*", "effect": "allow"}, {"action": "external_directory", "resource": "/tmp/opencode/*", "effect": "allow"}, {"action": "external_directory", "resource": "<home>/config/opencode/*", "effect": "allow"}, {"action": "question", "resource": "*", "effect": "allow"}, {"action": "edit", "resource": "*", "effect": "deny"}, {"action": "edit", "resource": "<home>/.opencode/plan/*", "effect": "allow"}, {"action": "external_directory", "resource": "<home>/.opencode/plan/*", "effect": "allow"}]}]}}}
{"type": "emit_inbound", "label": "session.inbox.enqueued.2", "frame": {"type": "sdk.event", "event": {"id": "evt_0f73f91e200143nPcVfCFMVD3q", "created": 1790854533602, "type": "session.inbox.enqueued", "location": {"directory": "<work>"}, "data": {"inboxID": "msg_0f73f91e0002lgRNb7FtMzcoQu", "sessionID": "ses_f08c06e25ffeF0asx24ydmo3oy", "item": {"type": "user", "payload": {"text": "You are a subagent spawned by another session.\nUse the subagent tool with background set to true to delegate to the general subagent with the prompt: Run the shell command `sleep 25` with the shell tool, then reply exactly GRANDCHILD_OK. As soon as it is launched, reply exactly MIDDLE_OK and end your turn without waiting for it."}, "delivery": "steer"}}, "durable": {"aggregateID": "ses_f08c06e25ffeF0asx24ydmo3oy", "seq": 1, "version": 1}}}}
{"type": "emit_inbound", "label": "session.execution.started.2", "frame": {"type": "sdk.event", "event": {"id": "evt_0f73f91e4001B2I7Jd3RFqZj5q", "created": 1790854533604, "type": "session.execution.started", "data": {"sessionID": "ses_f08c06e25ffeF0asx24ydmo3oy"}, "durable": {"aggregateID": "ses_f08c06e25ffeF0asx24ydmo3oy", "seq": 2, "version": 1}}}}
{"type": "emit_inbound", "label": "session.instructions.updated.2", "frame": {"type": "sdk.event", "event": {"id": "evt_0f73f91ee001ixy9HSfoqk0dfn", "created": 1790854533614, "metadata": {"instructions": {"initial": true}}, "type": "session.instructions.updated", "location": {"directory": "<work>"}, "data": {"sessionID": "ses_f08c06e25ffeF0asx24ydmo3oy", "delta": {"core/environment": "1c986c3eac0f6ca81397ee564526171942f29600037576981dd43f39f8d186fa", "core/date": "9d407747420cc512794008e266cb494f38764d6dcf796660ba21354b97d331f9", "core/codemode": "616a7ad8b8e40e8a71eab238dfbbe9eaf0f4a9e6fdad853226d075bfe602e1e6", "core/skill-guidance": "0d001919496102654717308c72635523bf7c3cd99c81e058cc79f2947104f9a5"}}, "durable": {"aggregateID": "ses_f08c06e25ffeF0asx24ydmo3oy", "seq": 3, "version": 2}}}}
Expand Down
Loading
Loading