Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: the chat list waits for the engine at start

ogsamratdcf92e9d7258
diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts
index 7bf20170..9c865fb9 100644
--- a/apps/server/src/server.ts
+++ b/apps/server/src/server.ts
@@ -309,6 +309,7 @@ async function capturePermissionMode(config: ServerConfig): Promise<ApprovalMode
const autoCompactorsByServer = new WeakMap<ServerConfig, AutoCompactor>();
/** How long a request waits for the 2.x engine while it starts (its boot timeout, managed-opencode-v2.ts). */
const ENGINE_V2_START_WAIT_MS = 60_000;
+const BY_FOLDER_ENGINE_WAIT_MS = 30_000;
const engineV2ByServer = new WeakMap<ServerConfig, EngineV2Preview>();
const editorLspByServer = new WeakMap<ServerConfig, ReturnType<typeof createEditorLsp>>();
function editorLspFor(config: ServerConfig): ReturnType<typeof createEditorLsp> {
@@ -5184,9 +5185,7 @@ function createRoutes(
const requested = Number(ctx.url.searchParams.get("limit"));
const limit = Number.isInteger(requested) && requested > 0 ? Math.min(requested, 500) : 200;
if (ctx.url.searchParams.get("engine") === "v2") {
- const v2 = engineV2ByServer.get(config)?.connection();
- if (!v2) throw new ApiError(503, "opencode_unconfigured", "The engine is not running");
- return jsonResponse(await listEngineV2SessionsInFolder(v2, directory, limit));
+ return jsonResponse(await listEngineV2FolderSessions(engineV2ByServer.get(config), directory, limit));
}
return jsonResponse(await listEngineSessionsInFolder(config, workspace, directory, limit));
});
@@ -6667,6 +6666,19 @@ export async function listEngineSessionsInFolder(
* direction, a trailing slash or the long-path prefix, or by real path
* (symlink, junction, subst drive).
*/
+/** The by-folder list of the 2.x engine. At start the engine is still coming up: waited for (the app's call allows 45 s), a 503 only when it is not up by then. */
+export async function listEngineV2FolderSessions(
+ preview: Pick<EngineV2Preview, "awaitConnection"> | undefined,
+ directory: string,
+ limit: number,
+ waitMs = BY_FOLDER_ENGINE_WAIT_MS,
+ fetchImpl?: (input: string, init?: RequestInit) => Promise<Response>,
+): Promise<unknown[]> {
+ const v2 = await preview?.awaitConnection(waitMs);
+ if (!v2) throw new ApiError(503, "opencode_unconfigured", "The engine is not running");
+ return listEngineV2SessionsInFolder(v2, directory, limit, fetchImpl);
+}
+
export async function listEngineV2SessionsInFolder(
v2: { url: string; password: string },
directory: string,
diff --git a/apps/server/src/sessions-by-folder.test.ts b/apps/server/src/sessions-by-folder.test.ts
index d1d7933c..887a19a8 100644
--- a/apps/server/src/sessions-by-folder.test.ts
+++ b/apps/server/src/sessions-by-folder.test.ts
@@ -3,7 +3,8 @@ import { mkdirSync, mkdtempSync, rmSync, symlinkSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { isSameSessionFolder, listEngineSessionsInFolder, listEngineV2SessionsInFolder } from "./server.js";
+import { isSameSessionFolder, listEngineSessionsInFolder, listEngineV2FolderSessions, listEngineV2SessionsInFolder } from "./server.js";
+import { awaitEngineConnection } from "./engine-v2-preview.js";
import type { ServerConfig, WorkspaceInfo } from "./types.js";
describe("isSameSessionFolder", () => {
@@ -117,3 +118,16 @@ describe("listEngineV2SessionsInFolder", () => {
}
});
});
+
+describe("listEngineV2FolderSessions", () => {
+ test("at start it waits for the engine instead of answering 503; 503 only when the engine never comes up", async () => {
+ let up: { url: string; username: string; password: string } | undefined;
+ setTimeout(() => { up = { url: "http://engine.invalid", username: "opencode", password: "pw" }; }, 150);
+ const starting = { awaitConnection: (ms: number) => awaitEngineConnection({ connection: () => up, starting: () => undefined, failed: () => false }, ms, 20) };
+ const listed = await listEngineV2FolderSessions(starting, "/p", 10, 5_000, async () => Response.json({ data: [{ id: "ses_p", location: { directory: "/p" } }] }));
+ expect(listed.map((session) => (session as { id: string }).id)).toEqual(["ses_p"]);
+ const never = { awaitConnection: (ms: number) => awaitEngineConnection({ connection: (): { url: string; username: string; password: string } | undefined => undefined, starting: () => undefined, failed: () => false }, ms, 20) };
+ await expect(listEngineV2FolderSessions(never, "/p", 10, 100)).rejects.toMatchObject({ status: 503 });
+ await expect(listEngineV2FolderSessions(undefined, "/p", 10, 100)).rejects.toMatchObject({ status: 503 });
+ });
+});