The best way to control Claude Code, Codex and more from any computer or phone
fix(opencode): apply runtime mode changes to existing OpenCode sessions (#26182)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 27, 2026 at 18:41 UTC
b5af4608ca7a6b2d31eddb1d3fd1c4ca5acb930f
2 files changed
+89
-10
packages/provider-opencode/src/server/adapter.test.ts
+58
-1
@@ -184,6 +184,7 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct
184
const startTurn = (
185
text = "hello",
186
startProviderThread: OrchestrationV2ProviderThread = providerThread,
187
+ turnPolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy = policy,
188
) =>
189
runtime.startTurn({
190
appThread: {
@@ -224,7 +225,7 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct
225
attachments: [],
226
},
227
modelSelection,
227
- runtimePolicy: policy,
228
+ runtimePolicy: turnPolicy,
229
});
230
return {
231
nativeSessionId,
@@ -239,6 +240,62 @@ const makeOpenCodeRuntimeHarness = Effect.fn("makeOpenCodeRuntimeHarness")(funct
240
});
241
242
describe("OpenCodeAdapterV2", () => {
243
+ it.effect("applies a changed runtime policy to the session on resume and turn start", () =>
244
+ Effect.gen(function* () {
245
+ const updates: Array<{ sessionID: string; permission: unknown }> = [];
246
+ const fullAccess = runtimePolicy("full-access", { cwd: "/workspace" });
247
+ const supervised = runtimePolicy("approval-required", { cwd: "/workspace" });
248
+ const harness = yield* makeOpenCodeRuntimeHarness("policy-change", "root", {
249
+ event: { subscribe: async () => ({ stream: asyncEventStream().stream }) },
250
+ session: {
251
+ create: async (input: { permission: unknown }) => ({
252
+ data: {
253
+ id: "root",
254
+ permission: input.permission,
255
+ time: { created: 1, updated: 1 },
256
+ },
257
+ }),
258
+ get: async () => ({
259
+ data: {
260
+ id: "root",
261
+ permission: openCodePermissionRules(fullAccess),
262
+ time: { created: 1, updated: 1 },
263
+ },
264
+ }),
265
+ update: async (input: { sessionID: string; permission: unknown }) => {
266
+ updates.push(input);
267
+ return { data: { id: input.sessionID, time: { created: 1, updated: 2 } } };
268
+ },
269
+ promptAsync: async () => ({ data: true }),
270
+ abort: async () => ({ data: true }),
271
+ children: async () => ({ data: [] }),
272
+ },
273
+ });
274
+
275
+ yield* harness.runtime.resumeThread({
276
+ providerThread: harness.providerThread,
277
+ runtimePolicy: fullAccess,
278
+ });
279
+ assert.deepStrictEqual(updates, []);
280
+
281
+ yield* harness.runtime.resumeThread({
282
+ providerThread: harness.providerThread,
283
+ runtimePolicy: supervised,
284
+ });
285
+ assert.deepStrictEqual(updates, [
286
+ { sessionID: "root", permission: openCodePermissionRules(supervised) },
287
+ ]);
288
+
289
+ yield* harness.startTurn("hello", harness.providerThread, fullAccess);
290
+ assert.deepStrictEqual(updates.slice(1), [
291
+ { sessionID: "root", permission: openCodePermissionRules(fullAccess) },
292
+ ]);
293
+ }).pipe(
294
+ Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer)),
295
+ Effect.scoped,
296
+ ),
297
+ );
298
+
299
it.effect.each(["completed", "failed", "unavailable"] as const)(
300
"normalizes OpenCode step usage for %s turns",
301
(ending) =>
packages/provider-opencode/src/server/adapter.ts
+31
-9
@@ -379,6 +379,7 @@ interface OpenCodeThreadState {
379
parentSubagent: OpenCodeSubagentContext | null;
380
nextChildTurnOrdinal: number;
381
nextAdmissionGeneration: number;
382
+ permission: PermissionRuleset;
383
}
384
385
interface OpenCodeRequestOwner {
@@ -1395,6 +1396,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
1396
parentSubagent: context,
1397
nextChildTurnOrdinal: 1,
1398
nextAdmissionGeneration: 1,
1399
+ permission: childPermission,
1400
});
1401
yield* emitProviderEvent({
1402
type: "app_thread.created",
@@ -2775,6 +2777,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2777
if (existing !== undefined) {
2778
existing.providerThread = providerThread;
2779
if (appThread !== null) existing.appThread = appThread;
2780
+ if (nativeSession.permission !== undefined)
2781
+ existing.permission = nativeSession.permission;
2782
return existing;
2783
}
2784
const state: OpenCodeThreadState = {
@@ -2789,11 +2793,29 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2793
parentSubagent: subagentsByChildSessionId.get(nativeSession.id) ?? null,
2794
nextChildTurnOrdinal: 1,
2795
nextAdmissionGeneration: 1,
2796
+ permission: nativeSession.permission ?? [],
2797
};
2798
threads.set(nativeSession.id, state);
2799
return state;
2800
};
2801
2802
+ const applyPermissionRules = Effect.fnUntraced(function* (
2803
+ state: OpenCodeThreadState,
2804
+ policy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
2805
+ ) {
2806
+ const permission = openCodePermissionRules(policy);
2807
+ if (
2808
+ permission.length === state.permission.length &&
2809
+ permission.every((rule, index) => permissionRuleEquals(rule, state.permission[index]!))
2810
+ ) {
2811
+ return;
2812
+ }
2813
+ yield* sdkCall("session.update", { sessionID: state.nativeSessionId, permission }, () =>
2814
+ client.session.update({ sessionID: state.nativeSessionId, permission }),
2815
+ );
2816
+ state.permission = permission;
2817
+ });
2818
+
2819
const resolvePromptParts = Effect.fnUntraced(function* (
2820
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
2821
) {
@@ -2988,14 +3010,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3010
providerThread: threadInput.existingProviderThread,
3011
});
3012
}
3013
+ const permission = openCodePermissionRules(threadInput.runtimePolicy);
3014
// OpenCode generates a title only when session.create leaves it unset.
2992
- const response = yield* sdkCall(
2993
- "session.create",
2994
- { permission: openCodePermissionRules(threadInput.runtimePolicy) },
2995
- () =>
2996
- client.session.create({
2997
- permission: openCodePermissionRules(threadInput.runtimePolicy),
2998
- }),
3015
+ const response = yield* sdkCall("session.create", { permission }, () =>
3016
+ client.session.create({ permission }),
3017
);
3018
const nativeSession = unwrapData("session.create", response);
3019
const createdAt = yield* DateTime.now;
@@ -3019,7 +3037,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3037
status: created.status,
3038
updatedAt: created.updatedAt,
3039
};
3022
- registerThread(nativeSession, providerThread, null);
3040
+ registerThread(nativeSession, providerThread, null).permission = permission;
3041
return providerThread;
3042
},
3043
(effect, threadInput) =>
@@ -3048,7 +3066,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3066
status: "idle" as const,
3067
updatedAt: dateTimeFromEpoch(nativeSession.time.updated, resumedAt),
3068
};
3051
- registerThread(nativeSession, providerThread, null);
3069
+ const state = registerThread(nativeSession, providerThread, null);
3070
+ if (threadInput.runtimePolicy !== undefined) {
3071
+ yield* applyPermissionRules(state, threadInput.runtimePolicy);
3072
+ }
3073
return providerThread;
3074
},
3075
(effect, threadInput) =>
@@ -3096,6 +3117,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3117
`OpenCode model '${turnInput.modelSelection.model}' must use provider/model format`,
3118
);
3119
}
3120
+ yield* applyPermissionRules(state, turnInput.runtimePolicy);
3121
const isCompaction =
3122
turnInput.message.text.trim() === "/compact" &&
3123
turnInput.message.attachments.length === 0;