fix(agent): chat console keeps its thread across turns and reloads (#1859)
Three user-reported failures in the assistant panel, one root cause each: 1. "The chat asks what I'm referring to" when continuing a thread. The single-call console (general.help, AskConsole -> /api/agent/ask) was stateless since the 08-20 model-agnostic cutover: conversationId was only the tool actor id, so every turn was answered blind, reload or not. The provider-agnostic GenerateTextRequest gains an optional `history` (real message turns before the prompt, in both the Anthropic-family and the OpenAI-compatible adapter; absent/empty leaves the request byte-identical to the single-turn call). The route loads the thread's earlier turns server-side (loadChatHistory: text only, hidden and tool rows dropped, alternation repaired, newest 16 rows / 10k chars) before writing the new question, and hands them to the model. 2. A full page reload (the deploy prompt's "Ladda om") closed the docked panel and dropped the thread from view. The panel now remembers its open thread per tab in sessionStorage (lib/agent-panel/session-restore) and the provider reopens it on mount; the sheet loads it exactly like a pick from "Tidigare konversationer". Close and "Ny konversation" forget it; a thread that no longer opens is dropped instead of retried on every reload. 3. "Can't type any more" once the update banner shows. DeployReloadPrompt's full-width wrapper sits at z-[60] after the panel in DOM order and swallowed clicks on the panel's composer; only the card takes input now. Claude-Session: https://claude.ai/code/session_01VjoXN3xdNZrHZeYA6qMi3g Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Jakob Wennberg
Claude Fable 5
parent
f75ea2384d
commit
3edbf0a2e3
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import {
|
||||
AGENT_SHEET_SESSION_KEY,
|
||||
clearAgentSheetSession,
|
||||
parseAgentSheetSession,
|
||||
readAgentSheetSession,
|
||||
writeAgentSheetSession,
|
||||
} from '../session-restore'
|
||||
|
||||
// A minimal sessionStorage double on a fake window; the helpers must also be
|
||||
// inert when neither exists (server render) or when access throws.
|
||||
function installStorage(opts: { throwOnAccess?: boolean } = {}) {
|
||||
const map = new Map<string, string>()
|
||||
const store = {
|
||||
getItem: (k: string) => map.get(k) ?? null,
|
||||
setItem: (k: string, v: string) => {
|
||||
map.set(k, v)
|
||||
},
|
||||
removeItem: (k: string) => {
|
||||
map.delete(k)
|
||||
},
|
||||
}
|
||||
const win: Record<string, unknown> = {}
|
||||
if (opts.throwOnAccess) {
|
||||
Object.defineProperty(win, 'sessionStorage', {
|
||||
get() {
|
||||
throw new Error('SecurityError')
|
||||
},
|
||||
})
|
||||
} else {
|
||||
win.sessionStorage = store
|
||||
}
|
||||
;(globalThis as { window?: unknown }).window = win
|
||||
return map
|
||||
}
|
||||
|
||||
const saved = (globalThis as { window?: unknown }).window
|
||||
beforeEach(() => {
|
||||
delete (globalThis as { window?: unknown }).window
|
||||
})
|
||||
afterEach(() => {
|
||||
if (saved === undefined) delete (globalThis as { window?: unknown }).window
|
||||
else (globalThis as { window?: unknown }).window = saved
|
||||
})
|
||||
|
||||
const session = {
|
||||
conversationId: 'conv-1',
|
||||
intentId: 'general.help',
|
||||
contextRef: 'report:vat:2026-07',
|
||||
collapsed: false,
|
||||
}
|
||||
|
||||
describe('session-restore', () => {
|
||||
it('round-trips the open thread through sessionStorage', () => {
|
||||
const map = installStorage()
|
||||
writeAgentSheetSession(session)
|
||||
expect(map.has(AGENT_SHEET_SESSION_KEY)).toBe(true)
|
||||
expect(readAgentSheetSession()).toEqual(session)
|
||||
clearAgentSheetSession()
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
})
|
||||
|
||||
it('reads nothing without a window (server) and never throws', () => {
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
expect(() => writeAgentSheetSession(session)).not.toThrow()
|
||||
expect(() => clearAgentSheetSession()).not.toThrow()
|
||||
})
|
||||
|
||||
it('treats a storage that throws on access as empty', () => {
|
||||
installStorage({ throwOnAccess: true })
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
expect(() => writeAgentSheetSession(session)).not.toThrow()
|
||||
})
|
||||
|
||||
it('ignores malformed stored values', () => {
|
||||
const map = installStorage()
|
||||
map.set(AGENT_SHEET_SESSION_KEY, 'not json')
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
map.set(AGENT_SHEET_SESSION_KEY, JSON.stringify({ intentId: 'general.help' }))
|
||||
expect(readAgentSheetSession()).toBeNull()
|
||||
})
|
||||
|
||||
it('parses defensively: contextRef defaults to null, collapsed to false', () => {
|
||||
expect(parseAgentSheetSession({ conversationId: 'c', intentId: 'i' })).toEqual({
|
||||
conversationId: 'c',
|
||||
intentId: 'i',
|
||||
contextRef: null,
|
||||
collapsed: false,
|
||||
})
|
||||
expect(parseAgentSheetSession({ conversationId: 'c', intentId: 'i', collapsed: true, contextRef: 7 })).toEqual({
|
||||
conversationId: 'c',
|
||||
intentId: 'i',
|
||||
contextRef: null,
|
||||
collapsed: true,
|
||||
})
|
||||
expect(parseAgentSheetSession(null)).toBeNull()
|
||||
expect(parseAgentSheetSession({ conversationId: '', intentId: 'i' })).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Per-tab memory of which thread the docked assistant panel had open.
|
||||
*
|
||||
* The panel's state lives in React only, so a full page reload (the "Ladda om"
|
||||
* deploy prompt, a browser refresh, a crash recovery) closed it and dropped the
|
||||
* thread from view; the user had to dig it out of "Tidigare konversationer" by
|
||||
* hand. This remembers just enough to reopen the same thread after a reload:
|
||||
* the conversation id (the thread itself is already persisted server-side),
|
||||
* the intent that shaped it, and whether the panel was collapsed.
|
||||
*
|
||||
* sessionStorage on purpose, not user_preferences: this is "this tab, this
|
||||
* session" state. It survives a reload of the tab and nothing else, so a
|
||||
* thread never follows the user to another device or another tab, and a closed
|
||||
* tab forgets it. Nothing here is authoritative: the id is re-validated by the
|
||||
* conversations API on restore, and a thread that no longer opens is dropped.
|
||||
*/
|
||||
|
||||
export const AGENT_SHEET_SESSION_KEY = 'accounted-agent-sheet'
|
||||
|
||||
export interface AgentSheetSession {
|
||||
conversationId: string
|
||||
intentId: string
|
||||
contextRef: string | null
|
||||
collapsed: boolean
|
||||
}
|
||||
|
||||
function storage(): Storage | null {
|
||||
try {
|
||||
if (typeof window === 'undefined') return null
|
||||
return window.sessionStorage
|
||||
} catch {
|
||||
// Storage access can throw (privacy modes, sandboxed frames): then there
|
||||
// is simply nothing to remember.
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function readAgentSheetSession(): AgentSheetSession | null {
|
||||
const store = storage()
|
||||
if (!store) return null
|
||||
try {
|
||||
const raw = store.getItem(AGENT_SHEET_SESSION_KEY)
|
||||
if (!raw) return null
|
||||
return parseAgentSheetSession(JSON.parse(raw))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate an untrusted parsed value; anything malformed reads as "nothing stored". */
|
||||
export function parseAgentSheetSession(value: unknown): AgentSheetSession | null {
|
||||
if (!value || typeof value !== 'object') return null
|
||||
const v = value as Record<string, unknown>
|
||||
if (typeof v.conversationId !== 'string' || v.conversationId.length === 0) return null
|
||||
if (typeof v.intentId !== 'string' || v.intentId.length === 0) return null
|
||||
return {
|
||||
conversationId: v.conversationId,
|
||||
intentId: v.intentId,
|
||||
contextRef: typeof v.contextRef === 'string' ? v.contextRef : null,
|
||||
collapsed: v.collapsed === true,
|
||||
}
|
||||
}
|
||||
|
||||
export function writeAgentSheetSession(session: AgentSheetSession): void {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
try {
|
||||
store.setItem(AGENT_SHEET_SESSION_KEY, JSON.stringify(session))
|
||||
} catch {
|
||||
// Quota or privacy mode: a lost write only costs the reload convenience.
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAgentSheetSession(): void {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
try {
|
||||
store.removeItem(AGENT_SHEET_SESSION_KEY)
|
||||
} catch {
|
||||
// ignored: see writeAgentSheetSession
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user