Skip to content

Commit ec83081

Browse files
committed
fix(opencode): preserve native permission rules in auto and full access
1 parent 7c22e24 commit ec83081

20 files changed

Lines changed: 224 additions & 66 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 106 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,10 @@ const resumed = (
330330
out("session.get", { sessionID: SESSION }),
331331
replyData("session.get", sessionInfo()),
332332
...noOpenRequests,
333+
out("agent.list", "<any>"),
334+
reply("agent.list", agentList),
333335
...(options?.supervised === true
334336
? [
335-
out("agent.list", "<any>"),
336-
reply("agent.list", agentList),
337337
out("session.update", { sessionID: SESSION, permissions: supervisedRules }),
338338
reply("session.update", null),
339339
]
@@ -907,8 +907,6 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
907907
const { runtime, thread } = yield* resumed([
908908
...backgroundLaunch(CHILD),
909909
event("session.execution.succeeded", { sessionID: SESSION }),
910-
out("agent.list", "<any>"),
911-
reply("agent.list", agentList),
912910
out("session.update", { sessionID: SESSION, permissions: supervisedRules }),
913911
reply("session.update", null),
914912
out("session.update", {
@@ -1974,6 +1972,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
19741972
}),
19751973
),
19761974
...noOpenRequests,
1975+
out("agent.list", "<any>"),
1976+
reply("agent.list", agentList),
19771977
out("session.update", { sessionID: SESSION, permissions: t3Rules }),
19781978
reply("session.update", null),
19791979
]);
@@ -1994,6 +1994,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
19941994
out("session.get", { sessionID: SESSION }),
19951995
replyData("session.get", sessionInfo()),
19961996
...noOpenRequests,
1997+
out("agent.list", "<any>"),
1998+
reply("agent.list", agentList),
19971999
out("session.move", { sessionID: SESSION, directory: "/work/opencode2-feature" }),
19982000
reply("session.move", null),
19992001
]);
@@ -2049,6 +2051,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
20492051
reply("permission.reply", null),
20502052
out("session.form.cancel", { sessionID: SESSION, formID: colorForm.id }),
20512053
reply("session.form.cancel", null),
2054+
out("agent.list", "<any>"),
2055+
reply("agent.list", agentList),
20522056
out("session.active"),
20532057
reply("session.active", { data: {} }),
20542058
event("session.execution.interrupted", { sessionID: SESSION, reason: "user" }),
@@ -2265,6 +2269,94 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
22652269
}).pipe(Effect.scoped),
22662270
);
22672271

2272+
it.effect.each([
2273+
["auto", true],
2274+
["full-access", true],
2275+
["approval-required", true],
2276+
["auto-accept-edits", true],
2277+
["auto", false],
2278+
] as const)(
2279+
"preserves OpenCode permissions in %s (configured: %s)",
2280+
([runtimeMode, configured]) =>
2281+
Effect.gen(function* () {
2282+
const nativeRules = configured
2283+
? [
2284+
{ action: "shell", resource: "*", effect: "allow" },
2285+
{ action: "shell", resource: "git push *", effect: "ask" },
2286+
{ action: "shell", resource: "rm *", effect: "deny" },
2287+
{ action: "read", resource: "*.env*", effect: "deny" },
2288+
{ action: "read", resource: "*.env.example", effect: "allow" },
2289+
{ action: "webfetch", resource: "*", effect: "ask" },
2290+
]
2291+
: [];
2292+
const permissions = [
2293+
...(runtimeMode === "auto"
2294+
? [...supervisedRules.slice(configured ? 1 : 0, 3), ...buildPaths, ...nativeRules]
2295+
: runtimeMode === "full-access"
2296+
? [
2297+
t3Rules[0],
2298+
...nativeRules.slice(2, -1),
2299+
{ action: "webfetch", resource: "*", effect: "allow" },
2300+
]
2301+
: [
2302+
{ action: "shell", resource: "*", effect: "ask" },
2303+
{
2304+
action: "edit",
2305+
resource: "*",
2306+
effect: runtimeMode === "auto-accept-edits" ? "allow" : "ask",
2307+
},
2308+
{ action: "external_directory", resource: "*", effect: "ask" },
2309+
...buildPaths,
2310+
]),
2311+
...mcpRules,
2312+
];
2313+
const runtime = yield* openCode2ReplayRuntime([
2314+
...opening,
2315+
out("agent.list", "<any>"),
2316+
reply("agent.list", {
2317+
...agentList,
2318+
data: agentList.data.map((agent) => ({
2319+
...agent,
2320+
permissions: [...agent.permissions, ...nativeRules],
2321+
})),
2322+
}),
2323+
out("session.create", {
2324+
location: { directory: WORK },
2325+
model: { providerID: "opencode", id: "big-pickle" },
2326+
permissions,
2327+
}),
2328+
replyData("session.create", sessionInfo({ permissions })),
2329+
]);
2330+
const thread = yield* runtime.ensureThread({
2331+
threadId,
2332+
modelSelection: bigPickle,
2333+
runtimePolicy: { ...policy(), runtimeMode },
2334+
});
2335+
assert.equal(thread.nativeThreadRef?.nativeId, SESSION);
2336+
}).pipe(Effect.scoped),
2337+
);
2338+
2339+
it.effect("does not create an unrestricted session when native permissions cannot be read", () =>
2340+
Effect.gen(function* () {
2341+
const runtime = yield* openCode2ReplayRuntime([
2342+
...opening,
2343+
out("agent.list", "<any>"),
2344+
reply("agent.list", {
2345+
status: 500,
2346+
body: { _tag: "UnknownError", message: "Cannot load permissions" },
2347+
}),
2348+
]);
2349+
const failed = yield* runtime
2350+
.ensureThread({
2351+
threadId,
2352+
modelSelection: bigPickle,
2353+
runtimePolicy: policy(),
2354+
})
2355+
.pipe(Effect.flip);
2356+
assert.equal(failed._tag, "ProviderAdapterEnsureThreadError");
2357+
}).pipe(Effect.scoped),
2358+
);
2359+
22682360
it.effect("reads the agents again while a fresh server still lists none", () =>
22692361
Effect.gen(function* () {
22702362
// OpenCode 2.0.18 briefly returns no agents on startup; using that response drops agent paths.
@@ -2844,6 +2936,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
28442936
}),
28452937
),
28462938
...noOpenRequests,
2939+
out("agent.list", "<any>"),
2940+
reply("agent.list", agentList),
28472941
out("mcp.add", {
28482942
server,
28492943
"location[directory]": WORK,
@@ -3211,6 +3305,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
32113305
out("session.get", { sessionID: SESSION }),
32123306
replyData("session.get", sessionInfo()),
32133307
...noOpenRequests,
3308+
out("agent.list", "<any>"),
3309+
reply("agent.list", agentList),
32143310
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
32153311
promptAccepted,
32163312
event("session.execution.succeeded", { sessionID: SESSION }),
@@ -3570,6 +3666,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
35703666
limit: { context: 64000, input: 48000, output: 8000 },
35713667
})),
35723668
}),
3669+
out("agent.list", { "location[directory]": custom }),
3670+
reply("agent.list", { ...agentList, location: { directory: custom } }),
35733671
out("session.create", {
35743672
location: { directory: custom },
35753673
model: { providerID: "opencode", id: "big-pickle" },
@@ -4256,6 +4354,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
42564354
out("session.get", { sessionID: SESSION }),
42574355
replyData("session.get", sessionInfo()),
42584356
...noOpenRequests,
4357+
out("agent.list", "<any>"),
4358+
reply("agent.list", agentList),
42594359
out("message.list", "<any>"),
42604360
reply("message.list", {
42614361
data: [
@@ -4350,6 +4450,8 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
43504450
const { runtime, thread } = yield* resumed([
43514451
out("session.fork", { sessionID: SESSION }),
43524452
replyData("session.fork", sessionInfo({ id: FORK })),
4453+
out("agent.list", { "location[directory]": target }),
4454+
reply("agent.list", { ...agentList, location: { directory: target } }),
43534455
out("session.update", {
43544456
sessionID: FORK,
43554457
permissions: [

‎apps/server/src/orchestration-v2/OpenCode2OrchestratorV2.integration.test.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ const createdSession = (
186186
directory: string,
187187
name: string,
188188
permissions: ReadonlyArray<unknown> = t3Rules(name),
189-
190-
narrows = false,
191189
): ReadonlyArray<ProviderReplayEntry> => [
192190
out("event.subscribe"),
193191
out("model.list", "<any>"),
@@ -198,7 +196,8 @@ const createdSession = (
198196
catalogModel("mimo-v2.6-flash-free", "MiMo V2.6 Flash Free"),
199197
],
200198
}),
201-
...(narrows ? [out("agent.list", "<any>"), reply("agent.list", agentList(directory))] : []),
199+
out("agent.list", "<any>"),
200+
reply("agent.list", agentList(directory)),
202201

203202
out("session.create", { location: { directory }, model: "<any>", permissions, title: name }),
204203
reply("session.create", sessionInfo(directory, permissions, name)),
@@ -371,8 +370,6 @@ describe("OpenCode 2 through the orchestrator", () => {
371370

372371
out("session.get", { sessionID: SESSION }),
373372
reply("session.get", sessionInfo(cwd, t3Rules(name), name)),
374-
out("agent.list", "<any>"),
375-
reply("agent.list", agentList(cwd)),
376373
out("session.update", { sessionID: SESSION, permissions: autoEditRules(name) }),
377374
reply("session.update", null),
378375
...answeredPrompt("SECOND"),
@@ -409,18 +406,28 @@ describe("OpenCode 2 through the orchestrator", () => {
409406
name,
410407
threadId: thread.threadId,
411408
entries: [
412-
...createdSession(cwd, name, planRules(name), true),
409+
...createdSession(cwd, name, planRules(name)),
413410

414411
out("session.switchAgent", { sessionID: SESSION, agent: "plan" }),
415412
reply("session.switchAgent", null),
416413
...instructionsWritten,
417414
...answeredPrompt("PLANNED"),
418415
out("session.get", { sessionID: SESSION }),
419416
reply("session.get", sessionInfo(cwd, planRules(name), name)),
420-
out("session.update", { sessionID: SESSION, permissions: t3Rules(name) }),
417+
out("session.update", {
418+
sessionID: SESSION,
419+
permissions: [
420+
...FULL_ACCESS,
421+
{ action: "edit", resource: "*", effect: "deny" },
422+
...PLAN_PATHS.slice(BUILD_PATHS.length),
423+
...mcpRules(name),
424+
],
425+
}),
421426
reply("session.update", null),
422427
out("session.switchAgent", { sessionID: SESSION, agent: "build" }),
423428
reply("session.switchAgent", null),
429+
out("session.update", { sessionID: SESSION, permissions: t3Rules(name) }),
430+
reply("session.update", null),
424431
...answeredPrompt("BUILT"),
425432
],
426433
commands: [

‎apps/server/src/orchestration-v2/testkit/fixtures/opencode2_background/opencode_transcript.ndjson‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
{"type": "transcript_start", "provider": "opencode", "protocol": "opencode2-http.sse", "version": "2.0.18", "scenario": "opencode2_background", "metadata": {"source": "opencode2-spike/recordings/background.ndjson", "capturedAt": "2026-09-28", "nativeSessionId": "ses_f1485cda4ffeXuc6GjAU9vDiRb", "description": "A real OpenCode 2.0.18 session recorded at the HTTP and SSE boundary. T3's per-turn instructions entry (answered 204, as 2.0.18 does) is written before the thread's first turn."}}
1+
{"type": "transcript_start", "provider": "opencode", "protocol": "opencode2-http.sse", "version": "2.0.18", "scenario": "opencode2_background", "metadata": {"source": "opencode2-spike/recordings/background.ndjson", "capturedAt": "2026-09-28", "nativeSessionId": "ses_f1485cda4ffeXuc6GjAU9vDiRb", "description": "A real OpenCode 2.0.18 session recorded at the HTTP and SSE boundary. T3's per-turn instructions entry (answered 204, as 2.0.18 does) is written before the thread's first turn.", "permissionInventorySource": "opencode2_permission: agent.list recording reused for the permission-policy lookup"}}
22
{"type": "expect_outbound", "label": "event.subscribe", "frame": {"type": "event.subscribe"}}
33
{"type": "expect_outbound", "label": "model.list", "frame": {"type": "model.list", "input": {"location[directory]": "<any>"}}}
44
{"type": "emit_inbound", "label": "model.list.response", "frame": {"type": "sdk.response", "operation": "model.list", "data": {"location": {"directory": "<work>"}, "data": [{"id": "space-bunny-free", "modelID": "space-bunny-free", "providerID": "opencode", "name": "Space Bunny Free", "compatibility": {"reasoningField": "reasoning_content"}, "package": "@opencode/ai/providers/openai-compatible", "settings": {"apiKey": "public", "baseURL": "https://opencode.ai/zen/v1", "provider": "opencode"}, "capabilities": {"tools": true, "input": ["text", "image", "video"], "output": ["text"]}, "variants": [{"id": "low", "settings": {"reasoningEffort": "low"}}, {"id": "medium", "settings": {"reasoningEffort": "medium"}}, {"id": "high", "settings": {"reasoningEffort": "high"}}, {"id": "xhigh", "settings": {"reasoningEffort": "xhigh"}}, {"id": "max", "settings": {"reasoningEffort": "max"}}], "time": {"released": 1790121600000}, "cost": [{"input": 0, "output": 0, "cache": {"read": 0, "write": 0}}], "status": "active", "enabled": true, "limit": {"context": 1048576, "input": 524288, "output": 524288}}, {"id": "big-pickle", "modelID": "big-pickle", "providerID": "opencode", "family": "big-pickle", "name": "Big Pickle", "compatibility": {"reasoningField": "reasoning_content"}, "package": "@opencode/ai/providers/openai-compatible", "settings": {"apiKey": "public", "baseURL": "https://opencode.ai/zen/v1", "provider": "opencode"}, "capabilities": {"tools": true, "input": ["text"], "output": ["text"]}, "variants": [], "time": {"released": 1760659200000}, "cost": [{"input": 0, "output": 0, "cache": {"read": 0, "write": 0}}], "status": "active", "enabled": true, "limit": {"context": 200000, "input": 160000, "output": 32000}}]}}}
55
{"type": "emit_inbound", "label": "server.connected", "frame": {"type": "sdk.event", "event": {"id": "evt_0eb7a3259001f54idsWUsyJ1uY", "type": "server.connected", "data": {}}}}
6+
{"type": "expect_outbound", "label": "agent.list", "frame": {"type": "agent.list", "input": {"location[directory]": "<any>"}}}
7+
{"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"}]}]}}}
68
{"type": "expect_outbound", "label": "session.create", "frame": {"type": "session.create", "input": "<any>"}}
79
{"type": "emit_inbound", "label": "session.create.response", "frame": {"type": "sdk.response", "operation": "session.create", "data": {"data": {"id": "ses_f1485cda4ffeXuc6GjAU9vDiRb", "projectID": "global", "model": {"id": "big-pickle", "providerID": "opencode", "variant": "default"}, "cost": 0, "tokens": {"input": 0, "output": 0, "reasoning": 0, "cache": {"read": 0, "write": 0}}, "time": {"created": 1790657049201, "updated": 1790657049201}, "title": "spike background", "location": {"directory": "<work>"}}}}}
810
{"type": "emit_inbound", "label": "session.created", "frame": {"type": "sdk.event", "event": {"id": "evt_0eb7a3271001Z06XG1eDh56UaK", "created": 1790657049201, "type": "session.created", "location": {"directory": "<work>"}, "data": {"sessionID": "ses_f1485cda4ffeXuc6GjAU9vDiRb", "slug": "cosmic-harbor", "version": "2.0.18", "projectID": "global", "location": {"directory": "<work>"}, "subpath": "", "title": "spike background", "model": {"id": "big-pickle", "providerID": "opencode"}}, "durable": {"aggregateID": "ses_f1485cda4ffeXuc6GjAU9vDiRb", "seq": 0, "version": 1}}}}

0 commit comments

Comments
 (0)