From 311d36b2b39a44b7a0e31ac6ed22b974e0067bf4 Mon Sep 17 00:00:00 2001 From: broglah1 Date: Mon, 28 Sep 2026 23:18:45 +0100 Subject: [PATCH] fix(chat): send the first new-session message after a slash command Typing "/" loads slash commands through ensure_session, which spawns the omp session before its first prompt. omp writes the session file only once the first message is committed, so the pre-dispatch boundary read (context?boundary=1) returned 404 and the send failed with "HTTP 404". Return an empty boundary for a live, idle wrapper whose file does not exist yet. An in-flight unpersisted run still returns 404, so its entries are never attributed to the next prompt. Fixes #160 --- CHANGELOG.md | 1 + app/api/sessions/[id]/context/route.ts | 19 +++++++++++---- lib/session-routes.test.mjs | 33 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ca934ce0..80284747c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,6 +88,7 @@ All notable changes to **omp-web** (`@kahme247/ompweb`) are documented in this f - Catch up missed conversation entries incrementally after reconnecting or returning to the page, including during active runs. Restore quiet partial responses and live tool output without duplicating history or overwriting newer updates. - Send prompts with image attachments in full again: commands reach OMP as one unchunked JSONL record. Protocol-v2 `rpc_chunk` framing is outbound-only, so any prompt over 1 MiB was rejected as `Unknown command: rpc_chunk` and reset the session after the prompt-ack timeout. - Show the **New session** fork action below agent replies as well as user prompts, so the newest message in a conversation can fork the session. omp's `branch` command accepts a user entry only, so each reply forks at the prompt that started its turn; replies with no earlier prompt keep no fork action. +- Send the first message of a new session after typing `/` or running a slash command. Opening the command list starts the session before OMP has written its file, so the pre-send history check failed with `Failed to send message: HTTP 404`. An idle session with no file now counts as having no prior history; while a run is still unsaved, the check still reports it as unknown. --- diff --git a/app/api/sessions/[id]/context/route.ts b/app/api/sessions/[id]/context/route.ts index 1d6d92bbe..f02113955 100644 --- a/app/api/sessions/[id]/context/route.ts +++ b/app/api/sessions/[id]/context/route.ts @@ -65,15 +65,24 @@ 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. - // Never replace previously confirmed history when its file is missing. - const session = sync === "1" && !cursor?.lastEntryId && leafId === undefined ? getRpcSession(id) : undefined; - if (session?.isAlive() && !existsSync(session.sessionFile)) { + // committed, so a live wrapper can exist before its file does. + const live = getRpcSession(id); + const unpersisted = live?.isAlive() && !existsSync(live.sessionFile) ? live : undefined; + // A session created ahead of its first prompt (typing "/" loads commands + // through ensure_session) is idle and has persisted nothing: its run + // boundary is empty. While a run is in flight the boundary stays unknown + // (404) — that run's unpersisted entries must not count as the next one's. + if (unpersisted && boundary === "1" && !unpersisted.isRunning()) { + return NextResponse.json({ entryIds: [] }, { headers: { "Cache-Control": "no-store" } }); + } + // A live wrapper can still recover that first partial reply. Never + // replace previously confirmed history when its file is missing. + if (unpersisted && sync === "1" && !cursor?.lastEntryId && leafId === undefined) { const response: SessionSyncResponse = { ...selectSessionHistory(buildSessionContext([]), cursor, limit), sessionId: id, leafId: null, - live: session.getStreamSnapshot(), + live: unpersisted.getStreamSnapshot(), }; return NextResponse.json(response, { headers: { "Cache-Control": "no-store" } }); } diff --git a/lib/session-routes.test.mjs b/lib/session-routes.test.mjs index 6eab07bfb..8eace2484 100644 --- a/lib/session-routes.test.mjs +++ b/lib/session-routes.test.mjs @@ -581,3 +581,36 @@ test("sync recovers the first live reply before persistence without erasing an e } }); }); + +test("boundary is empty for an idle session created before its first prompt, unknown once a run starts", async () => { + await withAgentDir(async (dir) => { + const id = "ensured-not-prompted"; + const filePath = join(dir, "2026-01-01_ensured-not-prompted.jsonl"); + let emit; + const wrapper = new AgentSessionWrapper({ + isAlive: true, + onFrame(listener) { emit = listener; return () => {}; }, + sendCommand: async () => ({ sessionId: id, sessionFile: filePath, isStreaming: false, isCompacting: false }), + sendFrame() {}, + dispose: async () => {}, + }, dir); + wrapper.start(); + await wrapper.send({ type: "get_state" }); + globalThis.__ompSessions ??= new Map(); + globalThis.__ompSessions.set(id, wrapper); + try { + const idle = await getContext(id, "boundary=1"); + assert.equal(idle.status, 200); + assert.equal(idle.headers.get("Cache-Control"), "no-store"); + assert.deepEqual(await idle.json(), { entryIds: [] }); + assert.equal(existsSync(filePath), false); + emit({ type: "agent_start" }); + assert.equal((await getContext(id, "boundary=1")).status, 404, "an in-flight unpersisted run keeps the boundary unknown"); + globalThis.__ompSessions.delete(id); + assert.equal((await getContext(id, "boundary=1")).status, 404, "without a live wrapper a missing file is not found"); + } finally { + await wrapper.destroyAndWait(); + globalThis.__ompSessions.delete(id); + } + }); +});