Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/server/src/mcp/toolkits/orchestrator/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const WorkflowStopTool = Tool.make("t3_workflow_stop", {

const BackgroundWaitTool = Tool.make("t3_background_wait", {
description:
"Keep this thread in Working while background work runs. Pass its task ID after starting it. Not for work left running (dev server, watcher). `waiting=false` clears. Does not stop the task. Claude only.",
"Keep this thread in Working while background work runs. Pass its task ID after starting it. Not for work left running (dev server, watcher). `waiting=false` clears. Does not stop the task. Claude and OpenCode only.",
parameters: Schema.Struct({
taskIds: Schema.Array(TrimmedNonEmptyString).check(
Schema.isMinLength(1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,10 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
const terminal = yield* runtime.events.pipe(
Stream.tap((e) =>
Effect.gen(function* () {
if (e.type === "provider_thread.updated") {
for (const task of e.providerThread.pendingBackgroundTasks ?? [])
assert.isUndefined(task.awaited);
}
if (e.type !== "turn_item.updated") return;
items.push(e.turnItem);
if (e.turnItem.type === "command_execution" && e.turnItem.output === "retained\n")
Expand All @@ -1011,6 +1015,43 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}
yield* Fiber.join(terminal);
assert.isTrue(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
for (const awaited of [true, false, true]) {
const roster = yield* runtime.events.pipe(
Stream.filter(
(e) =>
e.type === "provider_thread.updated" &&
e.providerThread.pendingBackgroundTasks?.some(
(task) => task.taskId === shellID && (task.awaited === true) === awaited,
) === true,
),
Stream.runHead,
Effect.forkScoped,
);
assert.deepEqual(
yield* runtime.awaitBackgroundTasks!({
providerThread: thread,
taskIds: [shellID, shellID, "sh_unknown"],
awaited,
}),
{ matched: [shellID] },
);
yield* Fiber.join(roster);
}
assert.deepEqual(
yield* runtime.awaitBackgroundTasks!({
providerThread: {
...thread,
nativeThreadRef: {
driver: OPENCODE_PROVIDER,
nativeId: "ses_foreign",
strength: "strong",
},
},
taskIds: [shellID],
awaited: false,
}),
{ matched: [] },
);
const settled = yield* runtime.events.pipe(
Stream.tap((e) =>
Effect.sync(() => {
Expand All @@ -1035,6 +1076,14 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
else gate.release("shell-exit");
yield* Fiber.join(settled);
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
assert.deepEqual(
yield* runtime.awaitBackgroundTasks!({
providerThread: thread,
taskIds: [shellID],
awaited: true,
}),
{ matched: [] },
);
assert.deepInclude(items.filter((i) => i.type === "command_execution").at(-1), {
type: "command_execution",
status: stopped ? "interrupted" : stop === "failed" ? "failed" : "completed",
Expand Down Expand Up @@ -1167,6 +1216,14 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
discovery !== "settled",
);
yield* runtime.stopTask!({ providerThread: thread, taskId: "sh_other" });
assert.deepEqual(
yield* runtime.awaitBackgroundTasks!({
providerThread: thread,
taskIds: [id, "sh_other"],
awaited: true,
}),
{ matched: discovery === "settled" ? [] : [id] },
);
assert.equal(
yield* runtime.hasPendingBackgroundWorkForThread!(thread),
discovery !== "settled",
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 5597,
"codexAdditionalContextBytes": 7327,
"mcpToolListJsonBytes": 48126
"mcpToolListJsonBytes": 48139
}
2 changes: 2 additions & 0 deletions evals/mcp-tool-use/cases/background-wait.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"id":"background-wait-opencode-tests","area":"background-wait","kind":"bad","pattern":"OpenCode awaits a background validation result -> mark only that shell awaited","experience":"OpenCode background shells outlived successful turns without a supported explicit wait marker, so T3 presented validation as finished.","privacy":"commit-safe","setup":{"role":"top","context":"You are running through the OpenCode provider in T3. Two native background shells have already launched: sh_tests runs the requested validation; sh_dev serves the development app and should stay running after the work is done. Their completion notifications will wake you automatically. There is no other work to do until validation ends.","mocks":{"t3_background_wait":{"matched":["sh_tests"],"unmatched":[]}}},"prompt":"Wait for the validation shell sh_tests, then inspect its result when it finishes. Leave sh_dev running. Do not poll or stop either shell.","expect":{"calls":[{"tool":"t3_background_wait","args":{"taskIds":["sh_tests"]}}],"callsMustNot":[{"tool":"t3_background_wait","args":{"taskIds":["sh_dev"]}},{"tool":"t3_background_wait","args":{"waiting":false}}]},"maxSteps":5,"source":{"issue":"https://github.com/pingdotgg/t3code/issues/35214"},"notes":"API replay exercises the provider-neutral tool description with OpenCode context, not an OpenCode harness capture or the adapter's injected instructions."}
{"id":"background-wait-opencode-dev-server","area":"background-wait","kind":"guard","pattern":"intentionally detached OpenCode dev server -> no wait mark","experience":"The old customization kept finished threads in Working merely because their dev servers were still running.","privacy":"commit-safe","setup":{"role":"top","context":"You are running through the OpenCode provider in T3. Validation passed. The only background work is sh_dev, the development server, which is ready at http://localhost:3000. No further agent work depends on its exit."},"prompt":"We're done. Leave the dev server running and give me a short summary. Don't inspect the browser.","expect":{"callsMustNot":[{"tool":"t3_background_wait"}]},"maxSteps":3,"source":{"decision":"https://github.com/pingdotgg/t3code/pull/24610"}}
28 changes: 27 additions & 1 deletion packages/provider-opencode/src/server/v2/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
tool?: { name: string; input: Record<string, unknown> };
command: string;
running: boolean;
awaited?: boolean;
stopping?: boolean;
output: string;
cursor: number;
Expand Down Expand Up @@ -1569,6 +1570,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
taskId,
kind: "command",
description: shell.command,
...(shell.awaited === true ? { awaited: true } : {}),
...(shell.state.subagent === undefined
? {}
: { parentTaskId: shell.state.subagent.call.nativeId }),
Expand All @@ -1588,7 +1590,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
: [],
),
);
const allTasks = [...roster, ...imageTasks];
const allTasks = roster.concat(imageTasks);
const current = state.providerThread.pendingBackgroundTasks ?? [];
if (
state.publishedBackgroundWork === hasBackground(state) &&
Expand All @@ -1597,6 +1599,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
(task, index) =>
task.taskId === allTasks[index]?.taskId &&
task.description === allTasks[index]?.description &&
task.awaited === allTasks[index]?.awaited &&
(task.kind === "subagent" ? task.childThreadId : undefined) ===
(allTasks[index]?.kind === "subagent" ? allTasks[index]?.childThreadId : undefined),
)
Expand Down Expand Up @@ -4091,6 +4094,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
const instructions = [
buildRuntimeInstructions({ harness: "OpenCode", model: turnInput.modelSelection.model }),
t3OrchestrationSystemPrompt(mcpAvailable, mcpSession?.capabilities?.has("device") === true),
mcpAvailable
? "To wait for a background shell you started (tests, a build), call `t3_background_wait` with its shell ID before you end your turn. Do not mark work you leave running, or PRs."
: undefined,
]
.filter((part) => part !== undefined && part.length > 0)
.join("\n\n");
Expand Down Expand Up @@ -4861,6 +4867,26 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
),
),
),
awaitBackgroundTasks: Effect.fn("OpenCode2Adapter.awaitBackgroundTasks")(
function* ({ providerThread, taskIds, awaited }) {
const nativeThreadId = providerThread.nativeThreadRef?.nativeId;
const state = nativeThreadId == null ? undefined : threads.get(nativeThreadId);
if (state === undefined) return { matched: [] };
const sessions = sessionsOf(state);
const matched = [...new Set(taskIds)].filter((taskId) => {
const shell = shells.get(taskId);
return shell !== undefined && shell.running && sessions.includes(shell.state);
});
if (matched.length === 0) return { matched };
for (const taskId of matched) {
const shell = shells.get(taskId)!;
shell.awaited = awaited;
yield* syncRosterAncestors(shell.state);
}
return { matched };
},
(effect) => effect.pipe(lock.withPermit),
),
stopTask: Effect.fn("OpenCode2Adapter.stopTask")(function* ({ providerThread, taskId }) {
const shell = shells.get(taskId);
const state =
Expand Down
Loading