From c4fdb1d7abac0d9a25e3c942ca9981b4392f337c Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Tue, 29 Sep 2026 01:00:22 +0000 Subject: [PATCH] fix(chat): do not 404 sends to a live session before its first reply is saved omp creates the session file only once the first assistant message is committed. handleSend reads context?boundary=1 before dispatching every prompt; for a live session without a file that returned 404, so the prompt was never sent and every retry failed the same way. Boundary mode now returns an empty entry list for a live wrapper whose file does not exist yet, mirroring the sync fallback. --- app/api/sessions/[id]/context/route.ts | 8 ++++++-- lib/session-routes.test.mjs | 12 +++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/api/sessions/[id]/context/route.ts b/app/api/sessions/[id]/context/route.ts index 1d6d92bbe..ba8c7648f 100644 --- a/app/api/sessions/[id]/context/route.ts +++ b/app/api/sessions/[id]/context/route.ts @@ -65,10 +65,14 @@ export async function GET( const resolved = await resolveSessionPathOr404(id); if ("response" in resolved) { // OMP may not create the file until the first assistant message is - // committed. A live wrapper can still recover that first partial reply. + // committed. A live wrapper can still recover that first partial reply, + // and its persisted boundary is empty (the next prompt must not 404). // Never replace previously confirmed history when its file is missing. - const session = sync === "1" && !cursor?.lastEntryId && leafId === undefined ? getRpcSession(id) : undefined; + const session = boundary === "1" || (sync === "1" && !cursor?.lastEntryId && leafId === undefined) ? getRpcSession(id) : undefined; if (session?.isAlive() && !existsSync(session.sessionFile)) { + if (boundary === "1") { + return NextResponse.json({ entryIds: [] }, { headers: { "Cache-Control": "no-store" } }); + } const response: SessionSyncResponse = { ...selectSessionHistory(buildSessionContext([]), cursor, limit), sessionId: id, diff --git a/lib/session-routes.test.mjs b/lib/session-routes.test.mjs index 6eab07bfb..10048dc6f 100644 --- a/lib/session-routes.test.mjs +++ b/lib/session-routes.test.mjs @@ -566,15 +566,25 @@ test("sync recovers the first live reply before persistence without erasing an e assert.equal("context" in await unavailable.json(), false, "an unavailable file is not an empty-history reset"); assert.equal((await getContext(id, "sync=1&leafId=saved-branch")).status, 404); assert.equal((await getContext(id)).status, 404, "legacy no-sync existence rules remain unchanged"); - assert.equal((await getContext(id, "boundary=1")).status, 404, "boundary never substitutes unpersisted live output"); + // A live session that has not persisted anything yet has an empty boundary: + // the next prompt must be dispatchable (a 404 here aborted the send). + const emptyBoundary = await getContext(id, "boundary=1"); + assert.equal(emptyBoundary.status, 200); + assert.equal(emptyBoundary.headers.get("Cache-Control"), "no-store"); + assert.deepEqual(await emptyBoundary.json(), { entryIds: [] }, "an unpersisted live session has an empty boundary"); writeFileSync(filePath, "malformed existing file\n"); + assert.equal((await getContext(id, "boundary=1")).status, 404, "an existing unresolvable file is not an empty boundary"); const { cacheSessionPath } = await jiti.import("./session-reader.ts"); cacheSessionPath(id, filePath); + const malformedBoundary = await getContext(id, "boundary=1"); + assert.equal(malformedBoundary.status, 404); + assert.equal((await malformedBoundary.json()).code, "session_file_malformed"); const malformed = await getContext(id, "sync=1"); assert.equal(malformed.status, 404); assert.equal((await malformed.json()).code, "session_file_malformed", "an existing bad file cannot masquerade as not-yet-persisted"); globalThis.__ompSessions.delete(id); assert.equal((await getContext("file-only-missing", "sync=1")).status, 404); + assert.equal((await getContext("file-only-missing", "boundary=1")).status, 404, "without a live wrapper a missing file stays 404"); } finally { await wrapper.destroyAndWait(); globalThis.__ompSessions.delete(id);