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:
Jakob Wennberg
2026-08-24 16:37:30 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Fable 5
parent f75ea2384d
commit 3edbf0a2e3
18 changed files with 680 additions and 20 deletions
+53
View File
@@ -146,6 +146,36 @@ describe('generateText / generateStructured', () => {
expect(result.text).toBe('{"ok":true}')
})
it('generateText sends earlier turns as real message turns before the prompt', async () => {
const svc = createAnthropicFamilyService(readAiConfig())
await svc.generateText({
tier: 'assistant',
system: 'S',
prompt: 'Och förra månaden?',
maxTokens: 50,
history: [
{ role: 'user', text: 'Vad är min största utgift?' },
{ role: 'assistant', text: '12 345 kr på 5010.' },
],
})
expect(mockCreate.mock.calls[0][0].messages).toEqual([
{ role: 'user', content: 'Vad är min största utgift?' },
{ role: 'assistant', content: '12 345 kr på 5010.' },
{ role: 'user', content: 'Och förra månaden?' },
])
})
it('generateText with an empty history is byte-identical to the single-turn call', async () => {
const svc = createAnthropicFamilyService(readAiConfig())
await svc.generateText({ tier: 'assistant', system: 'S', prompt: 'Hej', maxTokens: 50, history: [] })
expect(mockCreate.mock.calls[0][0]).toEqual({
model: 'eu.anthropic.claude-sonnet-5',
max_tokens: 50,
system: 'S',
messages: [{ role: 'user', content: 'Hej' }],
})
})
it('generateStructured forces one named tool and returns its input', async () => {
mockCreate.mockResolvedValueOnce({
content: [{ type: 'tool_use', id: 't1', name: 'verdict', input: { paired: true } }],
@@ -237,6 +267,29 @@ describe('generateText read-only tool loop', () => {
expect(result.usage.outputTokens).toBe(13)
})
it('keeps the earlier turns in front of the tool loop', async () => {
const svc = createAnthropicFamilyService(readAiConfig())
mockCreate.mockResolvedValueOnce({
stop_reason: 'end_turn',
content: [{ type: 'text', text: 'Svar.' }],
usage: { input_tokens: 1, output_tokens: 1 },
})
await svc.generateText({
tier: 'assistant',
prompt: 'Och förra månaden?',
maxTokens: 50,
history: [
{ role: 'user', text: 'Vad är min största utgift?' },
{ role: 'assistant', text: '12 345 kr på 5010.' },
],
tools: [{ name: 't', description: 'd', jsonSchema: { type: 'object' }, execute: vi.fn() }],
})
const messages = mockCreate.mock.calls[0][0].messages
expect(messages).toHaveLength(3)
expect(messages[0]).toEqual({ role: 'user', content: 'Vad är min största utgift?' })
expect(messages[2]).toEqual({ role: 'user', content: 'Och förra månaden?' })
})
it('surfaces a tool failure as an is_error result rather than throwing', async () => {
const execute = vi.fn().mockRejectedValue(new Error('report timeout'))
const svc = createAnthropicFamilyService(readAiConfig())
@@ -102,6 +102,25 @@ describe('createOpenAICompatibleService', () => {
})
})
it('generateText sends earlier turns as message turns before the prompt', async () => {
const svc = createOpenAICompatibleService(readAiConfig())
await svc.generateText({
tier: 'assistant',
system: 'S',
prompt: 'Och förra månaden?',
maxTokens: 50,
history: [
{ role: 'user', text: 'Vad är min största utgift?' },
{ role: 'assistant', text: '12 345 kr på 5010.' },
],
})
const prompt = promptOf()
expect(prompt.map((m) => m.role)).toEqual(['system', 'user', 'assistant', 'user'])
expect(prompt[1].content).toEqual([{ type: 'text', text: 'Vad är min största utgift?' }])
expect(prompt[2].content).toEqual([{ type: 'text', text: '12 345 kr på 5010.' }])
expect(prompt[3].content).toEqual([{ type: 'text', text: 'Och förra månaden?' }])
})
it('generateText forwards read-only tools to the model when provided', async () => {
const svc = createOpenAICompatibleService(readAiConfig())
const execute = vi.fn().mockResolvedValue({ ok: true })
+1
View File
@@ -5,6 +5,7 @@ import type { AiService } from './types'
export type {
AiCapabilities,
AiChatTurn,
AiDocumentInput,
AiImageMediaType,
AiPdfMode,
+19 -2
View File
@@ -3,6 +3,7 @@ import { createAiClient, toProviderModelId, type AiClient } from '../provider'
import type { ResolvedAiConfig } from '../config'
import { capabilitiesFor } from '../config'
import type {
AiChatTurn,
AiDocumentInput,
AiService,
AiTier,
@@ -39,6 +40,22 @@ const EMPTY_USAGE: AiUsage = {
cacheReadInputTokens: 0,
}
/**
* Earlier turns as real message turns, then the prompt as the final user
* message. With no history this is exactly the single-message array every
* non-chat caller sent before (hosted stays byte-identical for them).
*/
function messagesFor(
prompt: string,
history: AiChatTurn[] | undefined,
): Anthropic.Messages.MessageParam[] {
const prior: Anthropic.Messages.MessageParam[] = (history ?? []).map((t) => ({
role: t.role,
content: t.text,
}))
return [...prior, { role: 'user', content: prompt }]
}
/** Sum usage across the turns of a tool loop so the reported cost is truthful. */
function addUsage(a: AiUsage, b: AiUsage): AiUsage {
const s = (x: number | null, y: number | null) => (x ?? 0) + (y ?? 0)
@@ -136,7 +153,7 @@ export function createAnthropicFamilyService(cfg: ResolvedAiConfig): AiService {
model,
max_tokens: req.maxTokens,
...(req.system ? { system: req.system } : {}),
messages: [{ role: 'user', content: req.prompt }],
messages: messagesFor(req.prompt, req.history),
}
const resp = await getClient().messages.create(params)
return { text: textOf(resp), model, usage: usageOf(resp) }
@@ -151,7 +168,7 @@ export function createAnthropicFamilyService(cfg: ResolvedAiConfig): AiService {
input_schema: t.jsonSchema as Anthropic.Messages.Tool['input_schema'],
}))
const byName = new Map<string, AiToolDef>(req.tools.map((t) => [t.name, t]))
const messages: Anthropic.Messages.MessageParam[] = [{ role: 'user', content: req.prompt }]
const messages: Anthropic.Messages.MessageParam[] = messagesFor(req.prompt, req.history)
const maxSteps = req.maxSteps ?? DEFAULT_MAX_STEPS
let usage = EMPTY_USAGE
+17 -1
View File
@@ -13,6 +13,7 @@ import { capabilitiesFor, type ResolvedAiConfig } from '../config'
import { extractJsonObject } from '../json'
import { rasterizePdf } from '../rasterize-pdf'
import type {
AiChatTurn,
AiDocumentInput,
AiService,
AiTier,
@@ -29,6 +30,16 @@ import type {
const DEFAULT_MAX_STEPS = 4
/** Earlier turns as message turns, then the prompt as the final user message. */
function messagesWithHistory(prompt: string, history: AiChatTurn[]): ModelMessage[] {
const prior: ModelMessage[] = history.map((t) =>
t.role === 'assistant'
? { role: 'assistant', content: t.text }
: { role: 'user', content: t.text },
)
return [...prior, { role: 'user', content: prompt }]
}
/**
* Map our provider-agnostic tool defs onto the AI SDK's tool() shape. The SDK
* runs the loop itself (calls execute, feeds the result back) up to the
@@ -163,7 +174,12 @@ export function createOpenAICompatibleService(cfg: ResolvedAiConfig): AiService
const result = await generateText({
model: provider(model),
...(req.system ? { system: req.system } : {}),
prompt: req.prompt,
// The SDK takes either `prompt` or `messages`, never both: a plain
// single-turn call keeps `prompt`; a conversation sends the earlier
// turns as real messages with the prompt as the final user turn.
...(req.history && req.history.length > 0
? { messages: messagesWithHistory(req.prompt, req.history) }
: { prompt: req.prompt }),
maxOutputTokens: req.maxTokens,
...(tools ? { tools, stopWhen: stepCountIs(req.maxSteps ?? DEFAULT_MAX_STEPS) } : {}),
})
+18
View File
@@ -66,11 +66,29 @@ export interface AiToolDef {
execute: (args: Record<string, unknown>) => Promise<unknown>
}
/**
* One earlier turn of a conversation, text only. Sent to the model as a real
* message turn (not inlined into the prompt), so the answer can refer back
* to what was said without the caller re-describing it.
*/
export interface AiChatTurn {
role: 'user' | 'assistant'
text: string
}
export interface GenerateTextRequest {
tier: AiTier
system?: string
prompt: string
maxTokens: number
/**
* Earlier turns of the same conversation, oldest first, placed before
* `prompt` (which stays the final user message). Callers must hand over a
* clean alternation that starts with a user turn (see
* lib/agent/ask/persist.ts loadChatHistory); the services do not repair it.
* Absent or empty leaves the request exactly as a single-turn call.
*/
history?: AiChatTurn[]
/**
* Read-only tools the model may call to gather data before answering. When
* present AND the backend supports tool use (capabilities.toolUse), the