perf(server): stop caching unused OpenCode tool parts (#9738)

Do not retain OpenCode tool parts after emitting their runtime events. The remaining cache readers need text, reasoning, or step-usage parts, not tool input and output.

Keep tool lifecycle events, output bytes, late-role assistant text, and usage handling unchanged. Add focused lifecycle coverage and verify retained memory through the real adapter.

Created with GPT-6 Astra (preview) in Codex.
This commit is contained in:
Theo Browne authored and GitHub committed 2026-09-04 11:04:05 -07:00
1 parent 088cc3f955
commit c8f77e0d44
2 files changed
+132 -1

No files matched your search

@@ -21,6 +21,7 @@ import type {
Event as OpenCodeEvent,
PermissionRequest,
QuestionRequest,
ToolPart,
} from "@opencode-ai/sdk/v2";
import {
@@ -6536,6 +6537,133 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
}),
);
it.effect("emits tool lifecycle events before late assistant metadata", () =>
Effect.gen(function* () {
const adapter = yield* OpenCodeAdapter;
const threadId = asThreadId("thread-tool-lifecycle");
const sessionID = "http://127.0.0.1:9999/session";
const messageID = "msg-tools-before-role";
const start = promiseWithResolvers<OpenCodeEvent>();
const input = { command: "pwd" };
const states = [
{ status: "pending", input, raw: "" },
{ status: "running", input, title: "Working directory", time: { start: 1 } },
{
status: "completed",
input,
output: "/repo\n",
title: "Working directory",
metadata: {},
time: { start: 1, end: 2 },
},
{ status: "error", input, error: "Command failed", time: { start: 3, end: 4 } },
] satisfies ReadonlyArray<ToolPart["state"]>;
runtimeMock.state.subscribedEvents = [
start.promise,
...states.map(
(state) =>
({
id: `evt-tool-${state.status}`,
type: "message.part.updated",
properties: {
sessionID,
time: 4,
part: {
id: state.status === "error" ? "part-failed" : "part-working",
sessionID,
messageID,
type: "tool",
callID: state.status === "error" ? "call-failed" : "call-working",
tool: "bash",
state,
},
},
}) satisfies OpenCodeEvent,
),
{
id: "evt-text-before-role",
type: "message.part.updated",
properties: {
sessionID,
time: 4,
part: {
id: "part-late-text",
sessionID,
messageID,
type: "text",
text: "Tool results received",
time: { start: 4 },
},
},
},
{
id: "evt-late-assistant-role",
type: "message.updated",
properties: { sessionID, info: { id: messageID, role: "assistant" } },
},
{
id: "evt-tool-lifecycle-drained",
type: "session.compacted",
properties: { sessionID },
},
];
const eventsFiber = yield* adapter.streamEvents.pipe(
Stream.filter((event) => event.threadId === threadId),
Stream.takeUntil((event) => event.type === "thread.state.changed"),
Stream.runCollect,
Effect.forkChild,
);
yield* adapter.startSession({
provider: ProviderDriverKind.make("opencode"),
threadId,
runtimeMode: "full-access",
});
yield* adapter.sendTurn({
threadId,
input: "Read the working directory",
modelSelection: createModelSelection(
ProviderInstanceId.make("opencode"),
"opencode/kimi-k3",
),
});
start.resolve({
id: "evt-tool-lifecycle-started",
type: "session.status",
properties: { sessionID, status: { type: "busy" } },
});
const events = yield* Fiber.join(eventsFiber);
const tools = events.filter(
(event) =>
event.type === "item.started" ||
event.type === "item.updated" ||
event.type === "item.completed",
);
NodeAssert.deepEqual(
tools.map((event) => [event.type, event.itemId, event.payload.status]),
[
["item.started", "call-working", "inProgress"],
["item.updated", "call-working", "inProgress"],
["item.completed", "call-working", "completed"],
["item.completed", "call-failed", "failed"],
],
);
NodeAssert.partialDeepStrictEqual(tools[2]?.payload.data, {
command: "pwd",
result: "/repo\n",
});
NodeAssert.partialDeepStrictEqual(tools[3]?.payload.data, {
command: "pwd",
state: { error: "Command failed" },
});
NodeAssert.deepEqual(
events
.filter((event) => event.type === "content.delta")
.map((event) => event.payload.delta),
["Tool results received"],
);
}),
);
it.effect("maps native task progress only while a turn is active", () =>
Effect.gen(function* () {
const adapter = yield* OpenCodeAdapter;
@@ -2408,7 +2408,10 @@ export function makeOpenCodeAdapter(
case "message.part.updated": {
const part = event.properties.part;
context.partById.set(part.id, part);
// Tool events use the incoming part and do not need a cached copy.
if (part.type !== "tool") {
context.partById.set(part.id, part);
}
const messageRole = messageRoleForPart(context, part);
if (turnId && part.type === "step-finish" && context.turnTokenUsage) {