Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/api/sessions/[id]/context/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion lib/session-routes.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading