refactor(agent)!: migrate to Pi Durable and embed the agent queue (#525)

* refactor(agent)!: migrate sessions to Pi Durable

Replace patched AgentHarness storage with per-session SQLite and PostgreSQL commit mirroring. Migrate legacy history automatically and run agent queues with the main server.

BREAKING CHANGE: Remove standalone agent workers and return native Pi entry records from chat history APIs.

* fix(agent): constrain session log dialog scrolling

Keep the header visible and wrap long output while log entries and expanded records scroll within the dialog. Cover desktop and narrow viewports through the dashboard Sessions flow.

* fix(agent): hide entry IDs from session log card headers

Retain internal entry IDs in expanded raw records for debugging. Verify that card headers omit the IDs while raw JSON preserves them.

* test(e2e): handle browser wheel limits in session log checks

Verify that a normal wheel event moves the viewport before explicitly scrolling to the final record. Keep the dialog bounds, fixed header, wrapping, and expanded-record assertions.

* fix(agent): select model providers without inheriting unrelated auth

* fix(webui): prevent resize feedback when toggling agent panel

* test(agent): reproduce concurrent comment session races

* fix(agent): serialize comment sessions and deduplicate history

* docs(changelog): consolidate Pi Durable changes into one entry
This commit is contained in:
Yiling-J
2026-11-11 16:05:18 +08:00
committed by GitHub
parent 843a56304c
commit 35a0e82284
120 changed files with 7593 additions and 5972 deletions
+66 -47
View File
@@ -1,7 +1,7 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { useChatbotStore, AGENT_PREFERENCE_STORAGE_KEY } from './chatbot'
import { client } from '@/ui/api/client'
import type { ChatMessage } from '@shumai/dtos'
import { type EntryRecord, getShumaiMessage } from '@shumai/dtos'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { AGENT_PREFERENCE_STORAGE_KEY, useChatbotStore } from './chatbot'
const localStorageMock = (() => {
let store: Record<string, string> = {}
@@ -86,7 +86,14 @@ describe('useChatbotStore', () => {
useChatbotStore.setState({
selectedAgentId: 'agent-temporary-historical',
currentSessionId: 'sess-historical',
messages: [{ id: 'msg-1', role: 'user', content: 'test' } as unknown as ChatMessage],
messages: [
{
id: 10,
conversationId: 1,
kind: 'pi.user',
model: [{ role: 'user', content: 'test', timestamp: 0 }],
} as unknown as EntryRecord,
],
})
useChatbotStore.getState().startNewSession()
@@ -102,7 +109,14 @@ describe('useChatbotStore', () => {
useChatbotStore.setState({
currentSessionId: 'sess-abc',
messages: [{ id: 'msg-1', role: 'user', content: 'test' } as unknown as ChatMessage],
messages: [
{
id: 10,
conversationId: 1,
kind: 'pi.user',
model: [{ role: 'user', content: 'test', timestamp: 0 }],
} as unknown as EntryRecord,
],
})
// Navigating or changing other state does not wipe inputText
expect(useChatbotStore.getState().inputText).toBe('Draft text in progress')
@@ -132,7 +146,14 @@ describe('useChatbotStore', () => {
it('should reset currentSessionId and messages on startNewSession', () => {
useChatbotStore.setState({
currentSessionId: 'sess-abc',
messages: [{ id: 'msg-1', role: 'user', content: 'test' } as unknown as ChatMessage],
messages: [
{
id: 10,
conversationId: 1,
kind: 'pi.user',
model: [{ role: 'user', content: 'test', timestamp: 0 }],
} as unknown as EntryRecord,
],
chatAssets: [{ id: 'asset-1', name: 'Asset', type: 'file' }],
})
@@ -146,8 +167,8 @@ describe('useChatbotStore', () => {
it('should handle incoming SSE stream and replace optimistic message in a new session', async () => {
const mockEvents = [
'data: {"type":"session","sessionId":"sess-123"}\n\n',
'data: {"type":"entry","entry":{"id":"user-msg-ulid","role":"user","content":[{"type":"text","text":"hello"}],"timestamp":123}}\n\n',
'data: {"type":"entry","entry":{"id":"assistant-msg-ulid","role":"assistant","content":[{"type":"text","text":"hi user"}],"timestamp":124}}\n\n',
'data: {"type":"entry","entry":{"id":12,"conversationId":1,"kind":"pi.user","model":[{"role":"user","content":[{"type":"text","text":"hello"}],"timestamp":123}]}}\n\n',
'data: {"type":"entry","entry":{"id":13,"conversationId":1,"kind":"pi.assistant","model":[{"role":"assistant","content":[{"type":"text","text":"hi user"}],"timestamp":124}]}}\n\n',
'data: {"type":"done","status":"completed"}\n\n',
]
@@ -180,8 +201,8 @@ describe('useChatbotStore', () => {
// Optimistic message should be present immediately
const stateWithOptimistic = useChatbotStore.getState()
expect(stateWithOptimistic.messages).toHaveLength(1)
expect(stateWithOptimistic.messages[0].id).toContain('temp-')
expect((stateWithOptimistic.messages[0] as { content: unknown }).content).toBe('hello')
expect(stateWithOptimistic.messages[0].id).toBeLessThan(0)
expect(getShumaiMessage(stateWithOptimistic.messages[0])?.displayText).toBe('hello')
expect(stateWithOptimistic.isStreaming).toBe(true)
await sendPromise
@@ -191,8 +212,8 @@ describe('useChatbotStore', () => {
expect(finalState.isStreaming).toBe(false)
expect(finalState.currentSessionId).toBe('sess-123')
expect(finalState.messages).toHaveLength(2)
expect(finalState.messages[0].id).toBe('user-msg-ulid')
expect(finalState.messages[1].id).toBe('assistant-msg-ulid')
expect(finalState.messages[0].id).toBe(12)
expect(finalState.messages[1].id).toBe(13)
})
it('should not remove optimistic message when historical user message streams back', async () => {
@@ -200,19 +221,21 @@ describe('useChatbotStore', () => {
currentSessionId: 'sess-123',
messages: [
{
id: 'user-msg-ulid-old',
role: 'user',
content: [{ type: 'text', text: 'old message' }],
timestamp: 100,
} as unknown as ChatMessage,
id: 11,
conversationId: 1,
kind: 'pi.user',
model: [
{ role: 'user', content: [{ type: 'text', text: 'old message' }], timestamp: 100 },
],
} as unknown as EntryRecord,
],
chatAssets: [],
})
const mockEvents = [
'data: {"type":"session","sessionId":"sess-123"}\n\n',
'data: {"type":"entry","entry":{"id":"user-msg-ulid-old","role":"user","content":[{"type":"text","text":"old message"}],"timestamp":100}}\n\n',
'data: {"type":"entry","entry":{"id":"user-msg-ulid-new","role":"user","content":[{"type":"text","text":"new message"}],"timestamp":150}}\n\n',
'data: {"type":"entry","entry":{"id":11,"conversationId":1,"kind":"pi.user","model":[{"role":"user","content":[{"type":"text","text":"old message"}],"timestamp":100}]}}\n\n',
'data: {"type":"entry","entry":{"id":14,"conversationId":1,"kind":"pi.user","model":[{"role":"user","content":[{"type":"text","text":"new message"}],"timestamp":150}]}}\n\n',
'data: {"type":"done","status":"completed"}\n\n',
]
@@ -247,16 +270,16 @@ describe('useChatbotStore', () => {
// Optimistic message should be appended immediately
const stateWithOptimistic = useChatbotStore.getState()
expect(stateWithOptimistic.messages).toHaveLength(2)
expect(stateWithOptimistic.messages[0].id).toBe('user-msg-ulid-old')
expect(stateWithOptimistic.messages[1].id).toContain('temp-')
expect(stateWithOptimistic.messages[0].id).toBe(11)
expect(stateWithOptimistic.messages[1].id).toBeLessThan(0)
await sendPromise
// Final state assertions
const finalState = useChatbotStore.getState()
expect(finalState.messages).toHaveLength(2)
expect(finalState.messages[0].id).toBe('user-msg-ulid-old')
expect(finalState.messages[1].id).toBe('user-msg-ulid-new')
expect(finalState.messages[0].id).toBe(11)
expect(finalState.messages[1].id).toBe(14)
})
it('should create optimistic context display message when sending with assets', async () => {
@@ -268,8 +291,8 @@ describe('useChatbotStore', () => {
// 2. Mock POST response with context_display_info entry streaming back
const mockEvents = [
'data: {"type":"session","sessionId":"sess-123"}\n\n',
'data: {"type":"entry","entry":{"id":"context-display-ulid","role":"custom","customType":"context_display_info","details":{"assets":[{"id":"asset-1","name":"File 1.txt","type":"file"}]},"timestamp":120}}\n\n',
'data: {"type":"entry","entry":{"id":"user-msg-ulid","role":"user","content":[{"type":"text","text":"hello"}],"timestamp":123}}\n\n',
'data: {"type":"entry","entry":{"id":15,"conversationId":1,"kind":"shumai.custom","data":{"customType":"context_display_info","details":{"assets":[{"id":"asset-1","name":"File 1.txt","type":"file"}]}}}}\n\n',
'data: {"type":"entry","entry":{"id":12,"conversationId":1,"kind":"pi.user","model":[{"role":"user","content":[{"type":"text","text":"hello"}],"timestamp":123}]}}\n\n',
'data: {"type":"done","status":"completed"}\n\n',
]
@@ -304,13 +327,10 @@ describe('useChatbotStore', () => {
expect(stateWithOptimistic.messages).toHaveLength(1)
// Message should be the optimistic shumai_message with referencedAssets in details
expect(stateWithOptimistic.messages[0].id).toContain('temp-')
expect(stateWithOptimistic.messages[0].role).toBe('custom')
expect((stateWithOptimistic.messages[0] as { customType?: string }).customType).toBe(
'shumai_message',
)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((stateWithOptimistic.messages[0] as any).details.referencedAssets).toEqual([
expect(stateWithOptimistic.messages[0].id).toBeLessThan(0)
expect(stateWithOptimistic.messages[0].kind).toBe('shumai.optimistic')
expect(getShumaiMessage(stateWithOptimistic.messages[0])?.context?.referencedAssets).toEqual([
{ id: 'asset-1', name: 'File 1.txt', type: 'file' },
])
@@ -319,15 +339,15 @@ describe('useChatbotStore', () => {
// 4. Verify message list after streaming completes
const finalState = useChatbotStore.getState()
expect(finalState.messages).toHaveLength(2)
expect(finalState.messages[0].id).toBe('context-display-ulid')
expect(finalState.messages[1].id).toBe('user-msg-ulid')
expect(finalState.messages[0].id).toBe(15)
expect(finalState.messages[1].id).toBe(12)
})
it('should invoke onAssetMutation callback when asset creation tool result arrives', async () => {
const mockEvents = [
'data: {"type":"session","sessionId":"sess-123"}\n\n',
'data: {"type":"entry","entry":{"id":"user-msg-ulid","role":"user","content":[{"type":"text","text":"create a file"}],"timestamp":123}}\n\n',
'data: {"type":"entry","entry":{"id":"tool-res-ulid","role":"toolResult","toolName":"create_file","isError":false,"content":"File created","timestamp":124}}\n\n',
'data: {"type":"entry","entry":{"id":12,"conversationId":1,"kind":"pi.user","model":[{"role":"user","content":[{"type":"text","text":"create a file"}],"timestamp":123}]}}\n\n',
'data: {"type":"entry","entry":{"id":16,"conversationId":1,"kind":"pi.tool-result","model":[{"role":"toolResult","content":"File created","timestamp":124,"toolName":"create_file","isError":false}]}}\n\n',
'data: {"type":"done","status":"completed"}\n\n',
]
@@ -366,8 +386,8 @@ describe('useChatbotStore', () => {
it('should replace optimistic message when shumai_message entry streams back', async () => {
const mockEvents = [
'data: {"type":"session","sessionId":"sess-123"}\n\n',
'data: {"type":"entry","entry":{"id":"user-msg-ulid","role":"custom","customType":"shumai_message","content":"hello","details":{"user":{"id":"u1","name":"User 1"}},"timestamp":123}}\n\n',
'data: {"type":"entry","entry":{"id":"assistant-msg-ulid","role":"assistant","content":[{"type":"text","text":"Hi!"}],"timestamp":124}}\n\n',
'data: {"type":"entry","entry":{"id":12,"conversationId":1,"kind":"shumai.message","data":{"displayText":"hello","timestamp":123,"context":{"user":{"id":"u1","name":"User 1"}}}}}\n\n',
'data: {"type":"entry","entry":{"id":13,"conversationId":1,"kind":"pi.assistant","model":[{"role":"assistant","content":[{"type":"text","text":"Hi!"}],"timestamp":124}]}}\n\n',
'data: {"type":"done","status":"completed"}\n\n',
]
@@ -400,17 +420,16 @@ describe('useChatbotStore', () => {
// Optimistic message should be present immediately
const stateWithOptimistic = useChatbotStore.getState()
expect(stateWithOptimistic.messages).toHaveLength(1)
expect(stateWithOptimistic.messages[0].id).toContain('temp-')
expect(stateWithOptimistic.messages[0].id).toBeLessThan(0)
await sendPromise
// Final state assertions: optimistic message must be replaced
const finalState = useChatbotStore.getState()
expect(finalState.messages).toHaveLength(2)
expect(finalState.messages[0].id).toBe('user-msg-ulid')
expect(finalState.messages[0].role).toBe('custom')
expect((finalState.messages[0] as { customType?: string }).customType).toBe('shumai_message')
expect(finalState.messages[1].id).toBe('assistant-msg-ulid')
expect(finalState.messages[0].id).toBe(12)
expect(finalState.messages[0].kind).toBe('shumai.message')
expect(finalState.messages[1].id).toBe(13)
})
it('should include attachments, annotations, and second in optimistic message and API call', async () => {
@@ -459,20 +478,20 @@ describe('useChatbotStore', () => {
},
],
12.5,
[{ id: 'att-1', name: 'screenshot.png', type: 'image', mediaType: 'image/png' }],
[{ id: 'att-1', name: 'screenshot.png', type: 'image', mediaType: 'image' }],
)
// Check optimistic message has annotations, second position, and attachedFiles
const optimisticState = useChatbotStore.getState()
expect(optimisticState.messages).toHaveLength(1)
const optMsg = optimisticState.messages[0] as unknown as Record<string, unknown>
expect(optMsg.customType).toBe('shumai_message')
const details = optMsg.details as Record<string, unknown>
expect(optMsg.kind).toBe('shumai.optimistic')
const details = getShumaiMessage(optimisticState.messages[0])!.context
expect(details.annotation).toBe(true)
expect(details.annotations).toHaveLength(1)
expect(details.position).toEqual({ type: 'time', seconds: 12.5 })
expect(details.attachedFiles).toEqual([
{ id: 'att-1', name: 'screenshot.png', type: 'image', mediaType: 'image/png' },
{ id: 'att-1', name: 'screenshot.png', type: 'image', mediaType: 'image' },
])
await sendPromise
+39 -45
View File
@@ -1,8 +1,10 @@
import { create } from 'zustand'
import { client } from '@/ui/api/client'
import type { ChatMessage, ChatSessionInfo } from '@shumai/dtos'
import type { Annotation } from '@/ui/types'
import type { EntryId, EntryRecord, JsonObject } from '@earendil-works/pi-durable'
import { ROOT_CONVERSATION_ID } from '@earendil-works/pi-durable'
import type { ChatSessionInfo } from '@shumai/dtos'
import { toast } from 'sonner'
import { create } from 'zustand'
export const AGENT_PREFERENCE_STORAGE_KEY = 'shumai_selected_agent_id'
@@ -39,8 +41,8 @@ interface ChatbotState {
addAssets: (assets: ChatAsset[]) => void
removeAsset: (id: string) => void
clearAssets: () => void
messages: ChatMessage[]
setMessages: (messages: ChatMessage[]) => void
messages: EntryRecord[]
setMessages: (messages: EntryRecord[]) => void
historySessions: ChatSessionInfo[]
setHistorySessions: (sessions: ChatSessionInfo[]) => void
isStreaming: boolean
@@ -142,7 +144,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
const agentId = sessionInfo?.agentId || null
set({
currentSessionId: sessionId,
messages: messages as ChatMessage[],
messages: messages as EntryRecord[],
isHistoryMode: false,
scrollTop: 0,
isAtBottom: true,
@@ -205,27 +207,34 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
const state = useChatbotStore.getState()
if (state.isStreaming) return
const tempId = `temp-${Date.now()}`
const optimisticMsg = {
const tempId = -Date.now() as EntryId
const optimisticMsg: EntryRecord = {
id: tempId,
role: 'custom' as const,
customType: 'shumai_message',
content: text,
display: true,
details: {
currentAsset: contextAssetId ? { id: contextAssetId, name: '', type: 'file' } : undefined,
position:
second !== undefined && second !== null ? { type: 'time', seconds: second } : undefined,
annotation: !!(annotations && annotations.length > 0),
annotations: annotations || undefined,
attachedFiles: attachedFilesMeta || undefined,
referencedAssets:
state.chatAssets.length > 0
? state.chatAssets.map((a) => ({ id: a.id, name: a.name, type: a.type }))
: undefined,
},
timestamp: Date.now(),
} as unknown as ChatMessage
conversationId: ROOT_CONVERSATION_ID,
kind: 'shumai.optimistic',
data: JSON.parse(
JSON.stringify({
displayText: text,
timestamp: Date.now(),
context: {
currentAsset: contextAssetId
? { id: contextAssetId, name: '', type: 'file' }
: undefined,
position:
second !== undefined && second !== null
? { type: 'time', seconds: second }
: undefined,
annotation: !!(annotations && annotations.length > 0),
annotations: annotations || undefined,
attachedFiles: attachedFilesMeta || undefined,
referencedAssets:
state.chatAssets.length > 0
? state.chatAssets.map((a) => ({ id: a.id, name: a.name, type: a.type }))
: undefined,
},
}),
) as JsonObject,
}
const newMessages = [...state.messages, optimisticMsg]
@@ -302,10 +311,10 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
set({ currentSessionId: data.sessionId })
}
} else if (data.type === 'entry') {
const entry = data.entry as ChatMessage
const entryObj = entry as unknown as Record<string, unknown>
const entry = data.entry as EntryRecord
const entryObj = entry.model?.[0]
if (
entryObj.role === 'toolResult' &&
entryObj?.role === 'toolResult' &&
typeof entryObj.toolName === 'string' &&
[
'create_file',
@@ -323,23 +332,8 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
set((s) => {
const isExisting = s.messages.some((m) => m.id === entry.id)
const isUserMessage =
(entry.role === 'user' ||
(entry.role === 'custom' && entry.customType === 'shumai_message')) &&
!isExisting
// Replace temp/optimistic message when the real counterpart streams back
const filtered = s.messages.filter((m) => {
if (isUserMessage) {
return !m.id.startsWith('temp-')
}
if (
entry.role === 'custom' &&
entry.customType === 'context_display_info' &&
!isExisting
) {
return !m.id.startsWith('temp-context-')
}
return true
})
['pi.user', 'shumai.message'].includes(entry.kind) && !isExisting
const filtered = s.messages.filter((m) => !isUserMessage || m.id >= 0)
// Prevent duplicates by ID
const existingIdx = filtered.findIndex((m) => m.id === entry.id)