Skip to content

Commit 43de869

Browse files
Fluffy-Bunny-23opencode/minimax-m3.1-flash-free
andcommitted
fix(opencode): runtime mode changes reach an existing OpenCode session
Switching a thread from Supervised to Full access did not stop OpenCode from asking for approval. OpenCode keeps permission rules on the native session, and the adapter only wrote them at session.create, so every later turn resumed the session with the rules it was born with. The adapter now tracks the rules a native session carries and writes them back on resume when the runtime mode changed. Native subagent sessions keep their own agent-specific rules. Co-Authored-By: opencode/minimax-m3.1-flash-free <[email protected]>
1 parent ff9f41b commit 43de869

2 files changed

Lines changed: 118 additions & 5 deletions

File tree

‎packages/provider-opencode/src/server/adapter.test.ts‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert, describe, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3-
import type { OpencodeClient, ToolPart } from "@opencode-ai/sdk/v2";
3+
import type { OpencodeClient, PermissionRuleset, ToolPart } from "@opencode-ai/sdk/v2";
44
import {
55
CheckpointId,
66
NodeId,
@@ -2756,6 +2756,74 @@ describe("OpenCodeAdapterV2", () => {
27562756
});
27572757
});
27582758

2759+
it.effect("reapplies the runtime mode permission rules when a session is resumed", () =>
2760+
Effect.gen(function* () {
2761+
const nativeEvents = asyncEventStream();
2762+
const createdPermissions: Array<unknown> = [];
2763+
const updatedPermissions: Array<unknown> = [];
2764+
const instanceId = ProviderInstanceId.make("opencode-resume-policy");
2765+
const threadId = ThreadId.make("thread-opencode-resume-policy");
2766+
const modelSelection = {
2767+
instanceId,
2768+
model: "anthropic/claude-sonnet",
2769+
options: [],
2770+
};
2771+
const adapter = yield* makeOpenCodeAdapterV2({
2772+
instanceId,
2773+
settings: OPEN_CODE_TEST_SETTINGS,
2774+
environment: {},
2775+
}).pipe(
2776+
Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, {
2777+
connectToOpenCodeServer: () =>
2778+
Effect.succeed({ url: "http://test.invalid", external: true }),
2779+
createOpenCodeSdkClient: () => ({
2780+
event: { subscribe: async () => ({ stream: nativeEvents.stream }) },
2781+
session: {
2782+
create: async (input: { permission?: unknown }) => {
2783+
createdPermissions.push(input.permission);
2784+
return { data: { id: "root", time: { created: 1, updated: 1 } } };
2785+
},
2786+
update: async (input: { permission?: unknown }) => {
2787+
updatedPermissions.push(input.permission);
2788+
return { data: true };
2789+
},
2790+
get: async () => ({ data: { id: "root", time: { created: 1, updated: 1 } } }),
2791+
},
2792+
}),
2793+
} as unknown as OpenCodeRuntime.OpenCodeRuntime["Service"]),
2794+
Effect.provideService(ProviderHost.ProviderHost, {
2795+
paths: { cwd: "/workspace" },
2796+
resolveAttachmentPath: () => null,
2797+
} as unknown as ProviderHost.ProviderHost["Service"]),
2798+
);
2799+
const runtime = yield* adapter.openSession({
2800+
threadId,
2801+
providerSessionId: ProviderSessionId.make("session-opencode-resume-policy"),
2802+
modelSelection,
2803+
runtimePolicy: runtimePolicy("approval-required", { cwd: "/workspace" }),
2804+
});
2805+
const providerThread = yield* runtime.ensureThread({
2806+
threadId,
2807+
modelSelection,
2808+
runtimePolicy: runtimePolicy("approval-required", { cwd: "/workspace" }),
2809+
});
2810+
assert.equal(permissionAction(createdPermissions[0] as PermissionRuleset, "bash"), "ask");
2811+
2812+
yield* runtime.resumeThread({
2813+
providerThread,
2814+
threadId,
2815+
modelSelection,
2816+
runtimePolicy: runtimePolicy("full-access", { cwd: "/workspace" }),
2817+
});
2818+
2819+
assert.equal(updatedPermissions.length, 1);
2820+
assert.equal(permissionAction(updatedPermissions[0] as PermissionRuleset, "bash"), "allow");
2821+
}).pipe(
2822+
Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)),
2823+
Effect.scoped,
2824+
),
2825+
);
2826+
27592827
it("preserves OpenCode's recursion guard on task-created child sessions", () => {
27602828
const childRules = openCodeChildPermissionRules(runtimePolicy("full-access"), [
27612829
{ permission: "task", pattern: "*", action: "deny" },

‎packages/provider-opencode/src/server/adapter.ts‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ interface OpenCodeThreadState {
388388
parentSubagent: OpenCodeSubagentContext | null;
389389
nextChildTurnOrdinal: number;
390390
nextAdmissionGeneration: number;
391+
/**
392+
* The permission rules the native session currently carries. OpenCode keeps
393+
* them on the session, so a runtime mode change only lands when they are
394+
* written back.
395+
*/
396+
appliedPermissionRules: PermissionRuleset | null;
391397
}
392398

393399
interface OpenCodeRequestOwner {
@@ -706,6 +712,17 @@ function permissionRuleEquals(
706712
);
707713
}
708714

715+
function permissionRulesEqual(left: PermissionRuleset | null, right: PermissionRuleset): boolean {
716+
return (
717+
left !== null &&
718+
left.length === right.length &&
719+
left.every((rule, index) => {
720+
const other = right[index];
721+
return other !== undefined && permissionRuleEquals(rule, other);
722+
})
723+
);
724+
}
725+
709726
/**
710727
* OpenCode task sessions inherit only the parent's deny/external-directory
711728
* rules and then add agent-specific restrictions such as disabling nested
@@ -1417,6 +1434,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
14171434
parentSubagent: context,
14181435
nextChildTurnOrdinal: 1,
14191436
nextAdmissionGeneration: 1,
1437+
appliedPermissionRules: childPermission,
14201438
});
14211439
yield* emitProviderEvent({
14221440
type: "app_thread.created",
@@ -2806,6 +2824,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
28062824
parentSubagent: subagentsByChildSessionId.get(nativeSession.id) ?? null,
28072825
nextChildTurnOrdinal: 1,
28082826
nextAdmissionGeneration: 1,
2827+
appliedPermissionRules: null,
28092828
};
28102829
threads.set(nativeSession.id, state);
28112830
return state;
@@ -2991,6 +3010,26 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
29913010
);
29923011
});
29933012

3013+
const applyPermissionRules = (
3014+
state: OpenCodeThreadState,
3015+
runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy | undefined,
3016+
) =>
3017+
Effect.gen(function* () {
3018+
// A native subagent session carries the parent's rules plus the
3019+
// restrictions of its own agent, so the parent policy must not
3020+
// overwrite them.
3021+
if (runtimePolicy === undefined || state.parentSubagent !== null) return;
3022+
const rules = openCodePermissionRules(runtimePolicy);
3023+
if (permissionRulesEqual(state.appliedPermissionRules, rules)) return;
3024+
yield* sdkCall("session.update", { sessionID: state.nativeSessionId }, () =>
3025+
client.session.update({
3026+
sessionID: state.nativeSessionId,
3027+
permission: rules,
3028+
}),
3029+
);
3030+
state.appliedPermissionRules = rules;
3031+
});
3032+
29943033
const runtimeSession: ProviderAdapter.ProviderAdapterV2SessionRuntime = {
29953034
instanceId: options.instanceId,
29963035
driver: OPENCODE_PROVIDER,
@@ -3003,16 +3042,20 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
30033042
if (threadInput.existingProviderThread?.nativeThreadRef != null) {
30043043
return yield* runtimeSession.resumeThread({
30053044
providerThread: threadInput.existingProviderThread,
3045+
threadId: threadInput.threadId,
3046+
modelSelection: threadInput.modelSelection,
3047+
runtimePolicy: threadInput.runtimePolicy,
30063048
});
30073049
}
30083050
// No title: OpenCode generates one from the first prompt only when
30093051
// session.create leaves it unset (SessionPrompt.ensureTitle).
3052+
const permissionRules = openCodePermissionRules(threadInput.runtimePolicy);
30103053
const response = yield* sdkCall(
30113054
"session.create",
3012-
{ permission: openCodePermissionRules(threadInput.runtimePolicy) },
3055+
{ permission: permissionRules },
30133056
() =>
30143057
client.session.create({
3015-
permission: openCodePermissionRules(threadInput.runtimePolicy),
3058+
permission: permissionRules,
30163059
}),
30173060
);
30183061
const nativeSession = unwrapData("session.create", response);
@@ -3037,7 +3080,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
30373080
status: created.status,
30383081
updatedAt: created.updatedAt,
30393082
};
3040-
registerThread(nativeSession, providerThread, null);
3083+
const state = registerThread(nativeSession, providerThread, null);
3084+
state.appliedPermissionRules = permissionRules;
30413085
return providerThread;
30423086
}).pipe(
30433087
Effect.mapError(
@@ -3063,7 +3107,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
30633107
status: "idle" as const,
30643108
updatedAt: dateTimeFromEpoch(nativeSession.time.updated, resumedAt),
30653109
};
3066-
registerThread(nativeSession, providerThread, null);
3110+
const state = registerThread(nativeSession, providerThread, null);
3111+
yield* applyPermissionRules(state, threadInput.runtimePolicy);
30673112
return providerThread;
30683113
}).pipe(
30693114
Effect.mapError(

0 commit comments

Comments
 (0)