fix(opencode): restore stable v1 runtime

This commit is contained in:
2026-08-05 17:59:35 +08:00
parent 764a1f4e82
commit 1407dd3bbe
33 changed files with 1106 additions and 1668 deletions
+39 -82
View File
@@ -1,57 +1,34 @@
import { describe, expect, it } from "bun:test";
import { type OpenCodeClient } from "@opencode-ai/client";
import { type OpencodeClient } from "@opencode-ai/sdk/v2";
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",
);
});
});
import { config } from "../../src/config.js";
import { OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
const createRuntimeAdapter = (
messages: unknown[],
calls: { staged: string[]; committed: string[] } = {
staged: [],
committed: [],
},
calls: {
reverted: string[];
removed: string[];
} = { reverted: [], removed: [] },
) =>
Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
messages: async () => messages,
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,
revertMessage: async (_sessionId: string, messageId: string) => {
calls.reverted.push(messageId);
},
removeMessage: async (_sessionId: string, messageId: string) => {
calls.removed.push(messageId);
},
}) as OpencodeRuntimeAdapter;
describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
it("skips reverting the first user message when the runtime session is empty", async () => {
const calls = { staged: [] as string[], committed: [] as string[] };
const calls = { reverted: [] as string[], removed: [] as string[] };
const runtime = createRuntimeAdapter([], calls);
await runtime.revertToUserMessage("session-1", { userOrdinal: 1 });
expect(calls).toEqual({ staged: [], committed: [] });
expect(calls).toEqual({ reverted: [], removed: [] });
});
it("keeps ordinal mismatches visible when runtime messages exist", async () => {
@@ -65,8 +42,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
).rejects.toThrow("target user message not found to revert");
});
it("stages and commits the V2 revert at the target user message", async () => {
const calls = { staged: [] as string[], committed: [] as string[] };
it("reverts and removes messages from the target user message onward", async () => {
const calls = { reverted: [] as string[], removed: [] as string[] };
const runtime = createRuntimeAdapter(
[
{ info: { id: "user-1", role: "user" } },
@@ -80,8 +57,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
await runtime.revertToUserMessage("session-1", { userOrdinal: 2 });
expect(calls).toEqual({
staged: ["user-2"],
committed: ["session-1"],
reverted: ["user-2"],
removed: ["assistant-2", "user-2"],
});
});
});
@@ -90,10 +67,11 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
it("retries bootstrap after a failed startup attempt", async () => {
let attempts = 0;
const client = {
health: { get: async () => ({ healthy: true }) },
} as unknown as OpenCodeClient;
global: { health: async () => ({ data: { 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) {
@@ -110,66 +88,45 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
});
describe("OpencodeRuntimeAdapter.warmup", () => {
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 () => {
it("initializes the project session and model tools before reporting ready", async () => {
const calls: string[] = [];
const client = {
health: {
get: async () => {
calls.push("health.get");
return { healthy: true, version: "0.0.0-next-16741" };
global: {
health: async () => {
calls.push("health");
return { data: { healthy: true, version: "test" } };
},
},
session: {
create: async () => {
calls.push("session.create");
return { id: "warmup-session" };
return { data: { id: "warmup-session" } };
},
remove: async ({ sessionID }: { sessionID: string }) => {
calls.push(`session.remove:${sessionID}`);
delete: async ({ sessionID }: { sessionID: string }) => {
calls.push(`session.delete:${sessionID}`);
return { data: true };
},
},
model: {
list: async () => {
calls.push("model.list");
tool: {
list: async (model: { provider: string; model: string }) => {
calls.push(`tool.list:${model.provider}/${model.model}`);
return { data: [] };
},
},
plugin: {
list: async () => {
calls.push("plugin.list");
return [];
},
},
} as unknown as OpenCodeClient;
} 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.get",
"health",
"session.create",
"model.list",
"plugin.list",
"session.remove:warmup-session",
`tool.list:${config.OPENCODE_MODEL}`,
"session.delete:warmup-session",
]);
});
});