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
208 changes: 156 additions & 52 deletions apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ const directoryModels = (directory: string): ReadonlyArray<ProviderReplayEntry>
];

const bigPickle: ModelSelection = { instanceId, model: "opencode/big-pickle" };
const policy = (runtimeMode: "full-access" | "approval-required" = "full-access") => ({
const policy = (runtimeMode: "full-access" | "approval-required" | "auto" = "full-access") => ({
runtimeMode,
interactionMode: "default" as const,
cwd: WORK,
Expand All @@ -242,7 +242,7 @@ const providerThread = (now: DateTime.Utc): OrchestrationV2ProviderThread => ({
const turnInput = (
thread: OrchestrationV2ProviderThread,
modelSelection: ModelSelection = bigPickle,
runtimeMode: "full-access" | "approval-required" = "full-access",
runtimeMode: "full-access" | "approval-required" | "auto" = "full-access",
) => ({
appThread: { title: TITLE } as OrchestrationV2AppThread,
threadId,
Expand Down Expand Up @@ -282,7 +282,14 @@ const agentList = {
description: "The default agent.",
mode: "primary",
hidden: false,
permissions: [{ action: "*", resource: "*", effect: "allow" }, ...buildPaths],
permissions: [
{ action: "*", resource: "*", effect: "allow" },
{ action: "shell", resource: "*", effect: "ask" },
{ action: "shell", resource: "rm *", effect: "allow" },
{ action: "shell", resource: "printf *", effect: "allow" },
{ action: "shell", resource: "*", effect: "deny" },
...buildPaths,
],
},
],
};
Expand All @@ -293,6 +300,20 @@ const supervisedRules = [
...buildPaths,
...mcpRules,
];
// Auto keeps the shared asks, then the agent's rules in native order (last match wins), minus
// the catch-all allow so Auto stays stricter than Full access.
const autoRules = [
{ action: "shell", resource: "*", effect: "ask" },
{ action: "edit", resource: "*", effect: "ask" },
{ action: "external_directory", resource: "*", effect: "ask" },
{ action: "shell", resource: "*", effect: "ask" },
{ action: "shell", resource: "rm *", effect: "allow" },
{ action: "shell", resource: "printf *", effect: "allow" },
{ action: "shell", resource: "*", effect: "deny" },
...buildPaths,
...buildPaths,
...mcpRules,
];

const shellAsk = {
data: {
Expand Down Expand Up @@ -333,6 +354,7 @@ const resumed = (
readonly contextUsage?: OrchestrationV2ProviderThread["contextUsage"];
readonly external?: boolean;
readonly supervised?: boolean;
readonly auto?: boolean;
readonly processExit?: Effect.Effect<void>;
readonly beforeResponse?: (path: string) => Effect.Effect<void>;
readonly borrowers?: { current: number };
Expand Down Expand Up @@ -362,11 +384,14 @@ const resumed = (
: emptyShellInventory),
])),
...withInstructions([
...(options?.supervised === true
...(options?.supervised === true || options?.auto === true
? [
out("agent.list", "<any>"),
reply("agent.list", agentList),
out("session.update", { sessionID: SESSION, permissions: supervisedRules }),
out("session.update", {
sessionID: SESSION,
permissions: options?.auto === true ? autoRules : supervisedRules,
}),
reply("session.update", null),
]
: []),
Expand Down Expand Up @@ -406,7 +431,13 @@ const resumed = (
},
threadId,
modelSelection: bigPickle,
runtimePolicy: policy(options?.supervised === true ? "approval-required" : "full-access"),
runtimePolicy: policy(
options?.auto === true
? "auto"
: options?.supervised === true
? "approval-required"
: "full-access",
),
});
return { runtime, thread };
});
Expand Down Expand Up @@ -1575,52 +1606,71 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}),
);

it.effect("gives a background subagent the rules of a mode changed while it runs", () =>
Effect.gen(function* () {
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", {
sessionID: CHILD,
permissions: [...supervisedRules.slice(0, 3), ...mcpRules],
}),
reply("session.update", null),
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
promptAccepted,
event("session.execution.succeeded", { sessionID: SESSION }),
]);
const firstEnded = yield* Deferred.make<void>();
const ended = yield* runtime.events.pipe(
Stream.filter(
(
event,
): event is Extract<ProviderAdapter.ProviderAdapterV2Event, { type: "turn.terminal" }> =>
event.type === "turn.terminal",
),
Stream.tap(() => Deferred.succeed(firstEnded, undefined)),
Stream.take(2),
Stream.runCollect,
Effect.forkScoped,
);
yield* runtime.startTurn(withLineage(thread));
yield* Deferred.await(firstEnded);
yield* runtime.startTurn({
...withLineage(thread),
runId: RunId.make("run:opencode2-adapter:2"),
runOrdinal: 2,
providerTurnOrdinal: 2,
attemptId: RunAttemptId.make("attempt:opencode2-adapter:2"),
runtimePolicy: policy("approval-required"),
});
assert.deepEqual(
[...(yield* Fiber.join(ended))].map((terminal) => terminal.status),
["completed", "completed"],
);
}),
it.effect.each(["approval-required", "auto"] as const)(
"gives a background subagent the %s rules of a mode changed while it runs",
(mode) =>
Effect.gen(function* () {
// Built-in subagents such as explore ship their own allows; Auto must not copy them.
const general = {
...agentList.data[0]!,
id: "general",
name: "General",
mode: "subagent",
permissions: [
{ action: "*", resource: "*", effect: "deny" },
{ action: "shell", resource: "*", effect: "allow" },
{ action: "external_directory", resource: "*", effect: "allow" },
],
};
const { runtime, thread } = yield* resumed([
...backgroundLaunch(CHILD),
event("session.execution.succeeded", { sessionID: SESSION }),
out("agent.list", "<any>"),
reply("agent.list", { ...agentList, data: [...agentList.data, general] }),
out("session.update", {
sessionID: SESSION,
permissions: mode === "auto" ? autoRules : supervisedRules,
}),
reply("session.update", null),
out("session.update", {
sessionID: CHILD,
permissions: [...supervisedRules.slice(0, 3), ...mcpRules],
}),
reply("session.update", null),
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
promptAccepted,
event("session.execution.succeeded", { sessionID: SESSION }),
]);
const firstEnded = yield* Deferred.make<void>();
const ended = yield* runtime.events.pipe(
Stream.filter(
(
event,
): event is Extract<
ProviderAdapter.ProviderAdapterV2Event,
{ type: "turn.terminal" }
> => event.type === "turn.terminal",
),
Stream.tap(() => Deferred.succeed(firstEnded, undefined)),
Stream.take(2),
Stream.runCollect,
Effect.forkScoped,
);
yield* runtime.startTurn(withLineage(thread));
yield* Deferred.await(firstEnded);
yield* runtime.startTurn({
...withLineage(thread),
runId: RunId.make("run:opencode2-adapter:2"),
runOrdinal: 2,
providerTurnOrdinal: 2,
attemptId: RunAttemptId.make("attempt:opencode2-adapter:2"),
runtimePolicy: policy(mode),
});
assert.deepEqual(
[...(yield* Fiber.join(ended))].map((terminal) => terminal.status),
["completed", "completed"],
);
}),
);

it.effect(
Expand Down Expand Up @@ -3182,6 +3232,22 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}),
);

it.effect("Auto applies the selected agent's action-specific native rules", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed(
[
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
promptAccepted,
event("session.execution.succeeded", { sessionID: SESSION }),
],
{ auto: true },
);
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
yield* runtime.startTurn(turnInput(thread, bigPickle, "auto"));
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
}),
);

it.effect("counts an answer to a request OpenCode already dropped as delivered", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed(
Expand Down Expand Up @@ -4394,6 +4460,44 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}),
);

it.effect("Auto reloads native rules after a reconnect", () =>
Effect.gen(function* () {
const [build] = agentList.data;
const denied = { action: "shell", resource: "rm *", effect: "deny" };
const { runtime, thread } = yield* resumed(
[
out("session.prompt", { sessionID: SESSION, id: PROMPT_ID, text: "<any>" }),
promptAccepted,
...reconnected({}, "succeeded"),
out("agent.list", "<any>"),
reply("agent.list", {
...agentList,
data: [{ ...build, permissions: [...build!.permissions, denied] }],
}),
out("session.update", {
sessionID: SESSION,
permissions: [
...autoRules.slice(0, -mcpRules.length - buildPaths.length),
denied,
...buildPaths,
...mcpRules,
],
}),
reply("session.update", null),
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
promptAccepted,
event("session.execution.succeeded", { sessionID: SESSION }),
],
{ auto: true },
);
for (let turn = 0; turn < 2; turn++) {
const terminal = yield* terminalOf(runtime).pipe(Effect.forkScoped);
yield* runtime.startTurn(turnInput(thread, bigPickle, "auto"));
assert.equal((yield* Fiber.join(terminal))?.status, "completed");
}
}),
);

it.effect("continues a reply still streaming at reconnect from the text already shown", () =>
Effect.gen(function* () {
const delta = (text: string) =>
Expand Down
6 changes: 4 additions & 2 deletions docs/user/permission-modes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ the parent conversation with the subagent's name, so you can respond without swi

Providers enforce permissions differently. Some read-only actions can proceed in **Supervised**.
**Auto** uses automatic review on Codex, Claude, Cursor, Grok, Antigravity, and fx; providers without
an equivalent, including OpenCode, fall back to asking. On Grok and Antigravity, actions their review
does not approve come to you for approval. On fx, prompts it still sends also come to you.
an equivalent fall back to asking. OpenCode 2's **Auto** also applies action-specific native permission
rules, so a configured allow can run without a prompt; without those rules it asks. On Grok and
Antigravity, actions their review does not approve come to you for approval. On fx, prompts it still
sends also come to you.

Antigravity offers **Auto** only when your Antigravity sign-in provides its Auto mode; some accounts,
including some personal Google accounts, do not. Without it, an Antigravity thread already set to
Expand Down
10 changes: 8 additions & 2 deletions docs/user/providers-opencode.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ session.

## Approvals

OpenCode follows the shared [permission modes](./permission-modes.md). **Auto** has
the same rules as **Supervised** because OpenCode has no AI approval reviewer.
OpenCode follows the shared [permission modes](./permission-modes.md). OpenCode 2's
**Auto** applies the selected agent's native permission rules for specific actions,
so the `permission` (v1) or `permissions` (v2) block in `opencode.jsonc` can allow
routine commands while other commands still ask. Without those rules, **Auto** asks
as **Supervised** does; a catch-all `"*": "allow"` does not remove Auto's default
approval requirements. Subagents' own rules are not applied, so their
commands ask as in **Supervised**. OpenCode 1.x keeps its existing mode rules, where **Auto**
has the same rules as **Supervised**.
Environment files such as `.env` and `.env.local` need approval in restricted
modes even though normal file reads do not; `.env.example` is allowed.

Expand Down
Loading
Loading