Compare commits

...
Author SHA1 Message Date
paseo-bot[bot] ded35187d5 fix(opencode): preserve the prompt cache through compaction
OpenCode runs compaction through a separate request hook. Apply the same session tool filters there so unused and unrelated tool definitions do not change the cached prefix.
2026-12-04 14:33:53 +00:00
4 changed files with 40 additions and 8 deletions
@@ -283,6 +283,22 @@ describe.each<Mode>(["explicit", "discovered"])("OpenCode v2 %s shared service",
expect.objectContaining({ name: "hub_marker", description: "Return HUB_OK_2" }),
]);
const compactionRelevantTools = (request: ScriptedRequest) =>
request.tools.filter(
(tool) =>
tool.name.startsWith("hub_") ||
tool.name.startsWith("local_") ||
tool.name === "paseo_structured_output",
);
const ordinaryTools = compactionRelevantTools(service.requestsFor(secondCall).at(-1)!);
const beforeCompaction = service.agentRequests.length;
await second!.run("/compact");
const compactionRequests = service.agentRequests.slice(beforeCompaction);
expect(compactionRequests.length).toBeGreaterThan(0);
for (const request of compactionRequests) {
expect(compactionRelevantTools(request)).toEqual(ordinaryTools);
}
// A session the user opens in OpenCode itself sees no Paseo MCP tools. It keeps the
// same-named tool from the user's own OpenCode configuration.
const unrelatedPrompt = `unrelated ${scriptedCall("hub_marker")}`;
@@ -282,7 +282,7 @@ export async function registerSharedBridge(context) {
},
},
);
const filter = await context.session.hook("context", async (input) => {
async function configureTools(input) {
const binding = await find(input.sessionID);
if (binding) await adopt(binding, input.sessionID);
const scope =
@@ -303,12 +303,20 @@ export async function registerSharedBridge(context) {
if (name.startsWith("paseo_") && name !== STRUCTURED_OUTPUT_TOOL && !allowed.has(name))
delete input.tools[name];
}
// The instructions of Paseo-hosted MCP servers go with every request of the session and its
return scope;
}
const filter = await context.session.hook("context", async (input) => {
const scope = await configureTools(input);
// The instructions of Paseo-hosted MCP servers go with every turn request of the session and its
// descendants. OpenCode loads instruction entries before this hook runs, so an entry written
// when a child is adopted would miss the child's first request.
if (scope?.mcpInstructions) input.system.push({ type: "text", text: scope.mcpInstructions });
});
const compaction = await context.session.hook("compaction", async (input) => {
await configureTools(input);
});
return async () => {
await compaction.dispose();
await filter.dispose();
await rpc.dispose();
await Promise.all([...registrations.values()].map((registration) => registration.dispose()));
@@ -115,8 +115,8 @@ test("a shared plugin binds Paseo sessions without involving unrelated sessions"
context: async () =>
structured ? [{ type: "user", metadata: { paseoOutputSchema: { type: "string" } } }] : [],
get: async () => ({ parentID: "" }),
hook: async (_name: string, fn: (input: TestContext) => Promise<void>) => {
hooks.push(fn);
hook: async (name: string, fn: (input: TestContext) => Promise<void>) => {
if (name === "context") hooks.push(fn);
return { dispose: async () => {} };
},
},
@@ -484,8 +484,8 @@ test("a shared plugin gives each session its own MCP tools behind OpenCode's per
get: async ({ sessionID }: { sessionID: string }) => ({
parentID: parents[sessionID] ?? "",
}),
hook: async (_name: string, fn: (input: TestContext) => Promise<void>) => {
hooks.push(fn);
hook: async (name: string, fn: (input: TestContext) => Promise<void>) => {
if (name === "context") hooks.push(fn);
return { dispose: async () => {} };
},
},
@@ -34,11 +34,11 @@ export async function registerStructuredOutput(context) {
},
});
});
const hook = await context.session.hook("context", async (input) => {
async function configureTool(input) {
const schema = await schemaFor(input.sessionID);
if (schema === undefined) {
delete input.tools[STRUCTURED_OUTPUT_TOOL];
return;
return false;
}
const tool = input.tools[STRUCTURED_OUTPUT_TOOL];
if (!tool) throw new Error("Structured-output tool is unavailable");
@@ -48,12 +48,20 @@ export async function registerStructuredOutput(context) {
required: ["value"],
additionalProperties: false,
};
return true;
}
const hook = await context.session.hook("context", async (input) => {
if (!(await configureTool(input))) return;
input.system.push({
type: "text",
text: `Complete the user's task, then submit the final answer using ${STRUCTURED_OUTPUT_TOOL}. Its value must satisfy the tool schema. Do not finish with an ordinary text answer. After the tool accepts the value, end the turn without additional text or tool calls.`,
});
});
const compaction = await context.session.hook("compaction", async (input) => {
await configureTool(input);
});
return async () => {
await compaction.dispose();
await hook.dispose();
await tools.dispose();
};