Skip to content

Commit 27c9f6a

Browse files
committed
fix(opencode): gate native goal dispatch on provider capabilities
1 parent 1b1826b commit 27c9f6a

9 files changed

Lines changed: 141 additions & 32 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5165,6 +5165,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
51655165
],
51665166
{ goal: { objective: "Saved before downgrade", status: "active" } },
51675167
);
5168+
assert.isFalse(runtime.providerSession.capabilities.turns.supportsNativeGoals);
51685169
const events = yield* runtime.events.pipe(
51695170
Stream.takeUntil((e) => e.type === "turn.terminal"),
51705171
Stream.runCollect,
@@ -5206,6 +5207,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
52065207
],
52075208
{ version: "2.2.5" },
52085209
);
5210+
assert.isTrue(runtime.providerSession.capabilities.turns.supportsNativeGoals);
52095211
const thread = yield* runtime.resumeThread({
52105212
providerThread: providerThread(yield* DateTime.now),
52115213
threadId,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,12 +1168,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11681168
command: OrchestrationV2ServerCommand,
11691169
providerInstanceId: ProviderInstanceId,
11701170
) {
1171-
const { driver } = yield* providerAdapters
1172-
.get(providerInstanceId)
1173-
.pipe(mapDispatchError(command));
1174-
return driver === "codex" || driver === "claudeAgent" || driver === "opencode"
1175-
? driver
1176-
: undefined;
1171+
const adapter = yield* providerAdapters.get(providerInstanceId).pipe(mapDispatchError(command));
1172+
const { driver } = adapter;
1173+
if (driver === "opencode") {
1174+
const capabilities = yield* adapter.getCapabilities().pipe(mapDispatchError(command));
1175+
return capabilities.turns.supportsNativeGoals === true ? driver : undefined;
1176+
}
1177+
return driver === "codex" || driver === "claudeAgent" ? driver : undefined;
11771178
});
11781179

11791180
/** Claude Code reads `/goal pause` and `/goal resume` as new goals named "pause" and "resume". */

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ it.each([
269269

270270
function makeLocalCommandHarness(input: {
271271
readonly text: string;
272+
readonly nativeGoals?: boolean;
272273
readonly worktreePath?: string;
273274
readonly worktreesDir?: string;
274275
readonly workspaceRoot?: string;
@@ -337,7 +338,7 @@ function makeLocalCommandHarness(input: {
337338
};
338339
const providerThread: OrchestrationV2ThreadProjection["providerThreads"][number] = {
339340
id: providerThreadId,
340-
driver: ProviderDriverKind.make("codex"),
341+
driver: ProviderDriverKind.make(input.nativeGoals === undefined ? "codex" : "opencode"),
341342
providerInstanceId: newInstanceId,
342343
providerSessionId,
343344
appThreadId: threadId,
@@ -778,7 +779,13 @@ function makeLocalCommandHarness(input: {
778779
status: "ready",
779780
cwd: "/tmp/native-account-command",
780781
model: null,
781-
capabilities: CodexProviderCapabilitiesV2,
782+
capabilities: {
783+
...CodexProviderCapabilitiesV2,
784+
turns: {
785+
...CodexProviderCapabilitiesV2.turns,
786+
...(input.nativeGoals === undefined ? {} : { supportsNativeGoals: input.nativeGoals }),
787+
},
788+
},
782789
createdAt: now,
783790
updatedAt: now,
784791
lastError: null,
@@ -1338,6 +1345,23 @@ effectIt.effect.each([
13381345
}),
13391346
);
13401347

1348+
effectIt.effect.each([false, true])(
1349+
"OpenCode goal prompts defer switched-provider history only with native support: %s",
1350+
(nativeGoals) =>
1351+
Effect.gen(function* () {
1352+
const harness = makeLocalCommandHarness({
1353+
text: "/goal all tests pass",
1354+
nativeGoals,
1355+
sideChat: { contextRun: "failed", switchedProvider: true },
1356+
});
1357+
harness.tryHandlePromptCommand.mockReturnValue(Effect.succeed(false));
1358+
yield* harness.start;
1359+
expect(harness.startTurn.mock.calls[0]?.[0].message.text.includes("SIDE_CHAT_CONTEXT")).toBe(
1360+
!nativeGoals,
1361+
);
1362+
}),
1363+
);
1364+
13411365
effectIt.effect.each([
13421366
{ runStatus: "failed", turnStatus: "completed" },
13431367
{ runStatus: "completed", turnStatus: "completed" },

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,8 @@ export const layer: Layer.Layer<
845845
const goalDriver =
846846
session.driver === "codex" ||
847847
session.driver === "claudeAgent" ||
848-
session.driver === "opencode";
848+
(session.driver === "opencode" &&
849+
session.providerSession.capabilities.turns.supportsNativeGoals === true);
849850
const goalCommand = goalDriver && parseGoalCommand(message) !== null;
850851
// The provider turn marks cancellation work delivered; restart continuations use their own text or resume natively.
851852
const noteContinuation = isRestartNoteContinuation(

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

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,9 @@ const nextTurnSelectionHarness = Effect.fn("nextTurnSelectionHarness")(function*
488488
canSteer = true,
489489
interruptTurn: Effect.Effect<void> = Effect.void,
490490
steeringAcceptsAttachments = true,
491+
nativeGoals?: boolean,
491492
) {
493+
const driver = ProviderDriverKind.make(nativeGoals === undefined ? "codex" : "opencode");
492494
const cwd = yield* checkpointWorkspace(name);
493495
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
494496
const started: ProviderAdapter.ProviderAdapterV2TurnInput[] = [];
@@ -501,6 +503,7 @@ const nextTurnSelectionHarness = Effect.fn("nextTurnSelectionHarness")(function*
501503
supportsActiveSteering: canSteer,
502504
supportsSteeringByInterruptRestart: canRestart,
503505
activeSteeringAcceptsAttachments: steeringAcceptsAttachments,
506+
...(nativeGoals === undefined ? {} : { supportsNativeGoals: nativeGoals }),
504507
},
505508
};
506509
const adapter: ProviderAdapter.ProviderAdapterV2["Service"] = {
@@ -2060,8 +2063,15 @@ it.effect("queues a plain send behind the stopped queue and releases its hold",
20602063
),
20612064
);
20622065

2063-
const goalHarness = Effect.fn("goalHarness")(function* (name: string) {
2064-
const harness = yield* nextTurnSelectionHarness(name);
2066+
const goalHarness = Effect.fn("goalHarness")(function* (name: string, nativeGoals?: boolean) {
2067+
const harness = yield* nextTurnSelectionHarness(
2068+
name,
2069+
false,
2070+
true,
2071+
Effect.void,
2072+
true,
2073+
nativeGoals,
2074+
);
20652075
const send = (id: string, text: string, delivery: "queue" | "auto", freshContext = false) =>
20662076
Effect.gen(function* () {
20672077
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -2101,26 +2111,34 @@ const goalHarness = Effect.fn("goalHarness")(function* (name: string) {
21012111
return { ...harness, send, read };
21022112
});
21032113

2104-
it.effect("stops the running turn and starts a goal sent into it next", () =>
2105-
Effect.scoped(
2106-
Effect.gen(function* () {
2107-
const { steered, layer, startFirstTurn, send, read } = yield* goalHarness("goal-steer");
2108-
yield* Effect.gen(function* () {
2109-
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2110-
const threadId = yield* startFirstTurn;
2111-
yield* send("queued", "queued", "queue");
2112-
yield* send("goal", "/goal all tests pass", "auto");
2113-
yield* worker.drain();
2114+
it.effect.each([undefined, false, true])(
2115+
"goal dispatch stops the active turn only with native support: %s",
2116+
(nativeGoals) =>
2117+
Effect.scoped(
2118+
Effect.gen(function* () {
2119+
const { steered, layer, startFirstTurn, send, read } = yield* goalHarness(
2120+
"goal-steer",
2121+
nativeGoals,
2122+
);
2123+
yield* Effect.gen(function* () {
2124+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2125+
const threadId = yield* startFirstTurn;
2126+
yield* send("queued", "queued", "queue");
2127+
const sending = send("goal", "/goal all tests pass", "auto");
2128+
if (nativeGoals === false) {
2129+
yield* Effect.flip(sending);
2130+
} else yield* sending;
2131+
yield* worker.drain();
21142132

2115-
assert.deepEqual(steered, []);
2116-
assert.deepEqual(yield* read(threadId), {
2117-
queue: ["queued"],
2118-
started: ["first", "/goal all tests pass"],
2119-
stopRequested: true,
2120-
});
2121-
}).pipe(Effect.provide(layer));
2122-
}),
2123-
),
2133+
assert.deepEqual(steered, []);
2134+
assert.deepEqual(yield* read(threadId), {
2135+
queue: ["queued"],
2136+
started: nativeGoals === false ? ["first"] : ["first", "/goal all tests pass"],
2137+
stopRequested: nativeGoals !== false,
2138+
});
2139+
}).pipe(Effect.provide(layer));
2140+
}),
2141+
),
21242142
);
21252143

21262144
it.effect("starts a goal that names no model on the thread's model ahead of a queued pick", () =>

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ export const OrchestrationV2ThreadCapabilities = Schema.Struct({
278278
export type OrchestrationV2ThreadCapabilities = typeof OrchestrationV2ThreadCapabilities.Type;
279279

280280
export const OrchestrationV2TurnCapabilities = Schema.Struct({
281+
supportsNativeGoals: Schema.optional(Schema.Boolean),
281282
exposesNativeTurnId: Schema.Boolean,
282283
emitsTurnStarted: Schema.Boolean,
283284
emitsTurnCompleted: Schema.Boolean,

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,42 @@ it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
222222
}).pipe(Effect.scoped),
223223
);
224224

225+
it.effect.each([
226+
"1.18.32",
227+
"opencode v2.0.18",
228+
"opencode v2.2.4",
229+
"opencode v2.2.5",
230+
"opencode v2.2.6",
231+
])("advertises native goal support only for supported OpenCode versions: %s", (version) =>
232+
Effect.gen(function* () {
233+
const fs = yield* FileSystem.FileSystem;
234+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "opencode-goal-capability-" });
235+
const binaryPath = NodePath.join(
236+
root,
237+
"lib",
238+
"node_modules",
239+
"@opencode",
240+
"cli",
241+
"bin",
242+
"opencode",
243+
);
244+
yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
245+
yield* fs.writeFileString(binaryPath, "");
246+
const instance = yield* create({ binaryPath }, noHttp).pipe(
247+
Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, {
248+
...openCode2Runtime,
249+
runOpenCodeCommand: () => Effect.succeed({ stdout: version, stderr: "", code: 0 }),
250+
}),
251+
);
252+
yield* instance.snapshot.resolveMaintenance({ fresh: true });
253+
const capabilities = yield* instance.orchestrationAdapter.getCapabilities();
254+
assert.equal(
255+
capabilities.turns.supportsNativeGoals === true,
256+
version.endsWith("2.2.5") || version.endsWith("2.2.6"),
257+
);
258+
}).pipe(Effect.scoped),
259+
);
260+
225261
it.effect("answers capability reads without waiting on an unreachable server", () =>
226262
Effect.gen(function* () {
227263
const hang = HttpClient.make(() => Effect.never);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderL
1818
import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
1919
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
2020
import * as HostProcess from "@t3tools/shared/HostProcess";
21+
import { compareSemverVersions } from "@t3tools/shared/semver";
2122
import * as OpenCodeAdapterV2 from "./adapter.ts";
2223
import * as OpenCode2AdapterV2 from "./v2/adapter.ts";
2324
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
@@ -120,7 +121,19 @@ function selectOpenCodeRuntimeAdapter(input: {
120121
return {
121122
instanceId: input.v1.instanceId,
122123
driver: DRIVER_KIND,
123-
getCapabilities: () => Effect.flatMap(hot, (adapter) => adapter.getCapabilities()),
124+
getCapabilities: Effect.fn("OpenCodeDriver.getCapabilities")(function* () {
125+
const probed = Option.getOrUndefined(yield* input.probe.lastSuccess);
126+
const adapter = yield* pick(Effect.succeed(probed));
127+
const capabilities = yield* adapter.getCapabilities();
128+
return {
129+
...capabilities,
130+
turns: {
131+
...capabilities.turns,
132+
supportsNativeGoals:
133+
probed?.generation === "v2" && compareSemverVersions(probed.version, "2.2.5") >= 0,
134+
},
135+
};
136+
}),
124137
planSelectionTransition: (transition) =>
125138
Effect.flatMap(hot, (adapter) => adapter.planSelectionTransition(transition)),
126139
openSession: (session) =>

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
732732
status: "ready",
733733
cwd: input.runtimePolicy.cwd ?? host.paths.cwd,
734734
model: input.modelSelection.model,
735-
capabilities: OpenCode2ProviderCapabilities,
735+
capabilities: {
736+
...OpenCode2ProviderCapabilities,
737+
turns: {
738+
...OpenCode2ProviderCapabilities.turns,
739+
supportsNativeGoals: supportsNativeGoals(),
740+
},
741+
},
736742
createdAt: now,
737743
updatedAt: now,
738744
lastError: null,
@@ -2751,6 +2757,13 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
27512757
const previous = currentScope;
27522758
connection = next;
27532759
client = next.client;
2760+
session = {
2761+
...session,
2762+
capabilities: {
2763+
...session.capabilities,
2764+
turns: { ...session.capabilities.turns, supportsNativeGoals: supportsNativeGoals() },
2765+
},
2766+
};
27542767
currentScope = scope;
27552768
yield* Scope.close(previous, Exit.void);
27562769
// A restarted server forgot T3's MCP servers; the next turn adds them again.

0 commit comments

Comments
 (0)