Files
hapi/web/src/lib/sessionResume.ts
T
cc4025abdb fix(web,hub): queued bar SSE + never-started inactive resume (#761)
* fix(web): apply messages-consumed on global SSE connection

The global all-sessions SSE subscription returned early on message-stream
events without updating the message-window store. When session-scoped SSE
was reconnecting or the user had another session selected, messages-consumed
never cleared the queued bar even though the hub had stamped invoked_at.

Also harden mergeMessages so a stale invokedAt:null snapshot cannot clobber
an existing ack timestamp.

Fixes tiann/hapi#758

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web,hub): resume never-started inactive sessions on first send

Hub fresh-spawns when inactive session has path but no agent thread id and
zero messages. Web guards resume, updates inactive banner copy, and surfaces
resume_unavailable before POST /resume when resume is impossible.

Fixes tiann/hapi#759

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): scope sessionResume guard to current flavor only

Hub `resolveAgentResumeId` only honors the metadata.flavor's id; the web
guard was falling back across all flavors so a cursor session with a stale
codexSessionId still tried to resume and 409'd. Mirror the hub switch and
default to claude when flavor is unknown.

Addresses HAPI Bot review on tiann/hapi#761.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): allow claude session resume via hub message-id recovery

Hub `resolveAgentResumeId` falls back to `recoverClaudeSessionIdFromMessages`
on the claude branch when `metadata.claudeSessionId` is absent, so the web
guard must not block inactive claude sessions that have stored messages but
no metadata id. Other flavors have no such recovery path and stay rejected.

Addresses second HAPI Bot review thread on tiann/hapi#761
(`web/src/lib/sessionResume.ts:41`).

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-01 12:07:13 +08:00

50 lines
1.9 KiB
TypeScript

import { isKnownFlavor } from '@hapi/protocol'
import type { Session } from '@/types/api'
/** Agent thread id used by hub `resolveAgentResumeId`, flavor-specific.
* Mirrors hub: cross-flavor ids are ignored to avoid the web layer claiming a
* session is resumable when the hub will only honor the current flavor's id. */
export function resolveAgentSessionIdFromMetadata(
metadata: Session['metadata'] | null | undefined,
): string | undefined {
if (!metadata) {
return undefined
}
const flavor = isKnownFlavor(metadata.flavor) ? metadata.flavor : 'claude'
switch (flavor) {
case 'codex': return metadata.codexSessionId ?? undefined
case 'gemini': return metadata.geminiSessionId ?? undefined
case 'opencode': return metadata.opencodeSessionId ?? undefined
case 'cursor': return metadata.cursorSessionId ?? undefined
case 'kimi': return metadata.kimiSessionId ?? undefined
default: return metadata.claudeSessionId ?? undefined
}
}
/**
* Whether an inactive session can be activated via resume (or fresh spawn on first send).
* Matches hub: resume with agent id, or fresh spawn when path exists, no agent id, no user messages.
* Claude with messages but no `claudeSessionId` is allowed because hub
* `recoverClaudeSessionIdFromMessages` reconstructs the resume id from the
* stored message log (only the claude path has this recovery fallback).
*/
export function inactiveSessionCanResume(
session: Session,
userMessageCount: number,
): boolean {
if (session.active) {
return true
}
if (!session.metadata?.path) {
return false
}
if (resolveAgentSessionIdFromMetadata(session.metadata)) {
return true
}
const flavor = isKnownFlavor(session.metadata.flavor) ? session.metadata.flavor : 'claude'
if (flavor === 'claude' && userMessageCount > 0) {
return true
}
return userMessageCount === 0
}