Skip to content

Commit 219d67c

Browse files
committed
feat(opencode): mirror native goals across clients
1 parent cec8d73 commit 219d67c

6 files changed

Lines changed: 243 additions & 12 deletions

File tree

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

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const event = (
8282
id: `evt_${type.replaceAll(".", "")}0000`,
8383
created: 1,
8484
type,
85+
...(type.startsWith("rpc.") ? { location: { directory: WORK } } : {}),
8586
data,
8687
durable: { ...durable.durable, version },
8788
},
@@ -163,6 +164,7 @@ const withInstructions = (
163164
"session.compact",
164165
"command.list",
165166
"skill.list",
167+
"rpc.call",
166168
].includes(String(entry.frame.type)),
167169
);
168170
const after = entries.findIndex(
@@ -422,6 +424,17 @@ const history = {
422424
cursor: {},
423425
};
424426

427+
const nativeGoal = (status: "active" | "paused" | "complete") => ({
428+
id: "goal-1",
429+
objective: "Write proof.txt",
430+
status,
431+
rounds: 2,
432+
maxRounds: 100,
433+
boot: "boot-1",
434+
updatedAt: 1,
435+
reason: "Checked proof.txt",
436+
});
437+
425438
it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
426439
it.effect("switches the session's model and variant before a turn that changed them", () =>
427440
Effect.gen(function* () {
@@ -5031,6 +5044,133 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
50315044
assert.lengthOf(offers, 1);
50325045
}).pipe(Effect.scoped, Effect.provide(IdAllocator.layer)),
50335046
);
5047+
it.effect("reads an existing OpenCode 2.2.5 goal when resuming a session", () =>
5048+
Effect.gen(function* () {
5049+
const runtime = yield* openCode2ReplayRuntime(
5050+
[
5051+
...opening,
5052+
out("session.get", { sessionID: SESSION }),
5053+
replyData("session.get", sessionInfo()),
5054+
...noOpenRequests,
5055+
out("rpc.call", {
5056+
rpcID: "opencode.goal",
5057+
method: "get",
5058+
"location[directory]": WORK,
5059+
input: { sessionID: SESSION },
5060+
}),
5061+
reply("rpc.call", { output: nativeGoal("paused") }),
5062+
],
5063+
{ version: "2.2.5" },
5064+
);
5065+
const thread = yield* runtime.resumeThread({
5066+
providerThread: providerThread(yield* DateTime.now),
5067+
threadId,
5068+
modelSelection: bigPickle,
5069+
runtimePolicy: policy(),
5070+
});
5071+
assert.deepEqual(thread.goal, {
5072+
objective: "Write proof.txt",
5073+
status: "paused",
5074+
checks: 2,
5075+
lastCheck: "Checked proof.txt",
5076+
});
5077+
}).pipe(Effect.scoped),
5078+
);
5079+
5080+
it.effect.each(["complete", "paused"] as const)(
5081+
"keeps an OpenCode goal turn open across executions until its goal is %s",
5082+
(status) =>
5083+
Effect.gen(function* () {
5084+
const { runtime, thread } = yield* resumed([
5085+
out("command.list", "<any>"),
5086+
reply("command.list", { location: { directory: WORK }, data: [{ name: "goal" }] }),
5087+
out("message.list", { sessionID: SESSION, order: "desc", limit: "1" }),
5088+
reply("message.list", { data: [], cursor: {} }),
5089+
out("session.command", { sessionID: SESSION, name: "goal", text: "Write proof.txt" }),
5090+
reply("session.command", null),
5091+
event("rpc.opencode.goal.changed", { sessionID: SESSION, goal: nativeGoal("active") }),
5092+
event("session.execution.succeeded", { sessionID: SESSION }),
5093+
...(status === "complete"
5094+
? [event("session.execution.started", { sessionID: SESSION })]
5095+
: []),
5096+
event("rpc.opencode.goal.changed", { sessionID: SESSION, goal: nativeGoal(status) }),
5097+
...(status === "complete"
5098+
? [event("session.execution.succeeded", { sessionID: SESSION })]
5099+
: []),
5100+
]);
5101+
const events = yield* runtime.events.pipe(
5102+
Stream.takeUntil((e) => e.type === "turn.terminal"),
5103+
Stream.runCollect,
5104+
Effect.forkScoped,
5105+
);
5106+
const input = turnInput(thread);
5107+
yield* runtime.startTurn({
5108+
...input,
5109+
message: { ...input.message, text: "/goal Write proof.txt" },
5110+
});
5111+
const collected = yield* Fiber.join(events);
5112+
const goals = collected.flatMap((e) =>
5113+
e.type === "provider_thread.updated" && e.providerThread.goal
5114+
? [e.providerThread.goal]
5115+
: [],
5116+
);
5117+
assert.includeDeepMembers(goals, [
5118+
{
5119+
objective: "Write proof.txt",
5120+
status: "active",
5121+
checks: 2,
5122+
lastCheck: "Checked proof.txt",
5123+
},
5124+
{
5125+
objective: "Write proof.txt",
5126+
status,
5127+
checks: 2,
5128+
lastCheck: "Checked proof.txt",
5129+
},
5130+
]);
5131+
assert.deepInclude(collected.at(-1), { type: "turn.terminal", status: "completed" });
5132+
}).pipe(Effect.scoped),
5133+
);
5134+
5135+
it.effect.each(["show", "pause", "clear"] as const)(
5136+
"settles OpenCode /goal %s without waiting for an execution",
5137+
(control) =>
5138+
Effect.gen(function* () {
5139+
const goal = control === "clear" ? null : nativeGoal("paused");
5140+
const { runtime, thread } = yield* resumed(
5141+
control === "show"
5142+
? [out("rpc.call", "<any>"), reply("rpc.call", { output: goal })]
5143+
: [
5144+
out("command.list", "<any>"),
5145+
reply("command.list", { location: { directory: WORK }, data: [{ name: "goal" }] }),
5146+
out("message.list", { sessionID: SESSION, order: "desc", limit: "1" }),
5147+
reply("message.list", { data: [], cursor: {} }),
5148+
out("session.command", { sessionID: SESSION, name: "goal", text: control }),
5149+
event("rpc.opencode.goal.changed", { sessionID: SESSION, goal }),
5150+
reply("session.command", null),
5151+
],
5152+
);
5153+
const events = yield* runtime.events.pipe(
5154+
Stream.takeUntil((e) => e.type === "turn.terminal"),
5155+
Stream.runCollect,
5156+
Effect.forkScoped,
5157+
);
5158+
const input = turnInput(thread);
5159+
yield* runtime.startTurn({
5160+
...input,
5161+
message: { ...input.message, text: control === "show" ? "/goal" : `/goal ${control}` },
5162+
});
5163+
const collected = yield* Fiber.join(events);
5164+
const updated = collected.findLast((e) => e.type === "provider_thread.updated");
5165+
assert.equal(
5166+
updated?.type === "provider_thread.updated"
5167+
? (updated.providerThread.goal?.status ?? null)
5168+
: undefined,
5169+
goal?.status ?? null,
5170+
);
5171+
assert.deepInclude(collected.at(-1), { type: "turn.terminal", status: "completed" });
5172+
}).pipe(Effect.scoped),
5173+
);
50345174
});
50355175

50365176
const providerTurnId = Effect.gen(function* () {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ const operationOf = (
5454
query: Record<string, unknown>,
5555
body: unknown,
5656
) => {
57+
const rpc = /^\/api\/rpc\/([^/]+)\/([^/]+)$/.exec(path);
58+
if (method === "POST" && rpc !== null)
59+
return {
60+
type: "rpc.call",
61+
input: { rpcID: rpc[1], method: rpc[2], ...query, ...(body as object) },
62+
};
5763
const session = /^\/api\/session\/([^/]+)(\/.*)?$/.exec(path);
5864
if (method === "GET" && path === "/api/event") return { type: "event.subscribe" };
5965
if (method === "GET" && path === "/api/model") return { type: "model.list", input: query };
@@ -292,6 +298,7 @@ export const openCode2ReplayRuntime = (
292298
entries: ReadonlyArray<ProviderReplayEntry>,
293299
options?: {
294300
readonly external?: boolean;
301+
readonly version?: string;
295302
readonly onPrompt?: (input: unknown) => void;
296303
readonly borrowers?: { current: number };
297304
readonly replayGate?: ProviderReplayGate;
@@ -306,7 +313,7 @@ export const openCode2ReplayRuntime = (
306313
{
307314
provider: OPENCODE_PROVIDER,
308315
protocol: OPENCODE2_HTTP_PROTOCOL,
309-
version: "2.0.18",
316+
version: options?.version ?? "2.0.18",
310317
scenario: "opencode2_adapter",
311318
entries,
312319
},

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
11711171
const { driver } = yield* providerAdapters
11721172
.get(providerInstanceId)
11731173
.pipe(mapDispatchError(command));
1174-
return driver === "codex" || driver === "claudeAgent" ? driver : undefined;
1174+
return driver === "codex" || driver === "claudeAgent" || driver === "opencode"
1175+
? driver
1176+
: undefined;
11751177
});
11761178

11771179
/** Claude Code reads `/goal pause` and `/goal resume` as new goals named "pause" and "resume". */
@@ -6101,7 +6103,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
61016103
) {
61026104
dispatchMode = { type: "queue_after_active" };
61036105
}
6104-
// Codex and Claude own /goal; elsewhere it is ordinary text that cannot steer.
6106+
// Native goal commands must not steer an existing turn.
61056107
const parsedGoalCommand = parseGoalCommand(command);
61066108
const goalCommand =
61076109
parsedGoalCommand === null ||

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,11 @@ export const layer: Layer.Layer<
841841
text: message.text,
842842
records: message.context?.records ?? [],
843843
});
844-
// Codex and Claude parse /goal from the prompt, so it goes verbatim and history waits a turn.
845-
const goalDriver = session.driver === "codex" || session.driver === "claudeAgent";
844+
// Native goal commands go verbatim; handoff history waits a turn.
845+
const goalDriver =
846+
session.driver === "codex" ||
847+
session.driver === "claudeAgent" ||
848+
session.driver === "opencode";
846849
const goalCommand = goalDriver && parseGoalCommand(message) !== null;
847850
// The provider turn marks cancellation work delivered; restart continuations use their own text or resume natively.
848851
const noteContinuation = isRestartNoteContinuation(

‎docs/user/composer.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,15 @@ Claude instances can also compact idle threads for you. See
304304

305305
## Goals
306306

307-
With Codex and Claude, send `/goal` followed by what "done" means, for example
307+
With Codex, Claude, and OpenCode 2.2.5 or later, send `/goal` followed by what "done" means, for example
308308
`/goal all tests in packages/api pass`. The agent keeps working across turns
309309
until it judges the goal met. The thread shows **Goal** while it works, and a
310310
line at the end of the thread (above the composer on mobile) shows the goal and
311311
its progress.
312312

313313
- `/goal` alone shows the current goal. `/goal clear` removes it.
314-
- Codex also supports `/goal pause` and `/goal resume`. Stopping a Codex goal
315-
pauses it, and so does Codex ending its work without meeting the goal.
314+
- Codex and OpenCode also support `/goal pause` and `/goal resume`. Stopping
315+
their goal pauses it. Codex also pauses when its work ends without meeting the goal.
316316
- Stopping Claude ends the current turn, but the goal stays set. Claude checks it
317317
again at the end of your next message.
318318
- Sending a new goal, or `/goal resume`, while the agent works stops the current

0 commit comments

Comments
 (0)