feat(opencode): migrate agent runtime to v2

This commit is contained in:
2026-08-04 16:56:04 +08:00
parent 07016451d6
commit 764a1f4e82
33 changed files with 1662 additions and 1057 deletions
+82 -39
View File
@@ -1,34 +1,57 @@
import { describe, expect, it } from "bun:test";
import { type OpencodeClient } from "@opencode-ai/sdk/v2";
import { type OpenCodeClient } from "@opencode-ai/client";
import { config } from "../../src/config.js";
import { OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
import {
getEmbeddedServicePaths,
OpencodeRuntimeAdapter,
} from "../../src/runtime/opencode.js";
describe("getEmbeddedServicePaths", () => {
it("isolates service registrations by workspace and Agent port", () => {
const internal = getEmbeddedServicePaths("/srv/tjwater-agent", 8787);
const customer = getEmbeddedServicePaths("/srv/tjwater-agent-customer", 8787);
const secondPort = getEmbeddedServicePaths("/srv/tjwater-agent", 8788);
expect(internal.registrationFile).not.toBe(customer.registrationFile);
expect(internal.registrationFile).not.toBe(secondPort.registrationFile);
expect(internal.registrationFile).toEndWith(
"/data/opencode-service/8787/opencode/service.json",
);
});
});
const createRuntimeAdapter = (
messages: unknown[],
calls: {
reverted: string[];
removed: string[];
} = { reverted: [], removed: [] },
calls: { staged: string[]; committed: string[] } = {
staged: [],
committed: [],
},
) =>
Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
messages: async () => messages,
revertMessage: async (_sessionId: string, messageId: string) => {
calls.reverted.push(messageId);
},
removeMessage: async (_sessionId: string, messageId: string) => {
calls.removed.push(messageId);
},
ensureClient: async () =>
({
session: {
revert: {
stage: async ({ messageID }: { messageID: string }) => {
calls.staged.push(messageID);
},
commit: async ({ sessionID }: { sessionID: string }) => {
calls.committed.push(sessionID);
},
},
},
}) as unknown as OpenCodeClient,
}) as OpencodeRuntimeAdapter;
describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
it("skips reverting the first user message when the runtime session is empty", async () => {
const calls = { reverted: [] as string[], removed: [] as string[] };
const calls = { staged: [] as string[], committed: [] as string[] };
const runtime = createRuntimeAdapter([], calls);
await runtime.revertToUserMessage("session-1", { userOrdinal: 1 });
expect(calls).toEqual({ reverted: [], removed: [] });
expect(calls).toEqual({ staged: [], committed: [] });
});
it("keeps ordinal mismatches visible when runtime messages exist", async () => {
@@ -42,8 +65,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
).rejects.toThrow("target user message not found to revert");
});
it("reverts and removes messages from the target user message onward", async () => {
const calls = { reverted: [] as string[], removed: [] as string[] };
it("stages and commits the V2 revert at the target user message", async () => {
const calls = { staged: [] as string[], committed: [] as string[] };
const runtime = createRuntimeAdapter(
[
{ info: { id: "user-1", role: "user" } },
@@ -57,8 +80,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
await runtime.revertToUserMessage("session-1", { userOrdinal: 2 });
expect(calls).toEqual({
reverted: ["user-2"],
removed: ["assistant-2", "user-2"],
staged: ["user-2"],
committed: ["session-1"],
});
});
});
@@ -67,11 +90,10 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
it("retries bootstrap after a failed startup attempt", async () => {
let attempts = 0;
const client = {
global: { health: async () => ({ data: { healthy: true } }) },
} as unknown as OpencodeClient;
health: { get: async () => ({ healthy: true }) },
} as unknown as OpenCodeClient;
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
clientPromise: null,
closeServer: null,
bootstrapClient: async () => {
attempts += 1;
if (attempts === 1) {
@@ -88,45 +110,66 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
});
describe("OpencodeRuntimeAdapter.warmup", () => {
it("initializes the project session and model tools before reporting ready", async () => {
it("rejects a service that is not the pinned V2 release", async () => {
const client = {
health: {
get: async () => ({ healthy: true, version: "1.18.12" }),
},
} as unknown as OpenCodeClient;
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
clientPromise: null,
ensureClient: async () => client,
}) as OpencodeRuntimeAdapter;
await expect(runtime.warmup()).rejects.toThrow(
"incompatible OpenCode service version",
);
});
it("checks V2 health, catalogs and plugins, then removes the probe session", async () => {
const calls: string[] = [];
const client = {
global: {
health: async () => {
calls.push("health");
return { data: { healthy: true, version: "test" } };
health: {
get: async () => {
calls.push("health.get");
return { healthy: true, version: "0.0.0-next-16741" };
},
},
session: {
create: async () => {
calls.push("session.create");
return { data: { id: "warmup-session" } };
return { id: "warmup-session" };
},
delete: async ({ sessionID }: { sessionID: string }) => {
calls.push(`session.delete:${sessionID}`);
return { data: true };
remove: async ({ sessionID }: { sessionID: string }) => {
calls.push(`session.remove:${sessionID}`);
},
},
tool: {
list: async (model: { provider: string; model: string }) => {
calls.push(`tool.list:${model.provider}/${model.model}`);
model: {
list: async () => {
calls.push("model.list");
return { data: [] };
},
},
} as unknown as OpencodeClient;
plugin: {
list: async () => {
calls.push("plugin.list");
return [];
},
},
} as unknown as OpenCodeClient;
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
clientPromise: null,
closeServer: null,
ensureClient: async () => client,
}) as OpencodeRuntimeAdapter;
await runtime.warmup();
expect(calls).toEqual([
"health",
"health.get",
"session.create",
`tool.list:${config.OPENCODE_MODEL}`,
"session.delete:warmup-session",
"model.list",
"plugin.list",
"session.remove:warmup-session",
]);
});
});