fix: reduce supervisor stalls and preserve task delivery

Load Supervisor graphs only when opened, preserve source details on demand, reuse indexed story digests and window selection lists without losing keyboard or focus behavior. Route heavy UI reads through the existing worker, batch scoped queries and construct each publication snapshot once.

Keep remote cancellation responsive during model-route cleanup, remove late routes and retain reported usage after response-cap cancellation. Preserve Telegram pending replies through polling conflicts, show delivery errors and honor the final rate-limit cooldown.

Schema 62 adds lookup indexes without rewriting user content; older clients cannot open upgraded data. Large review publication still blocks Main and remains documented follow-up work. Record the future storage-service direction without performing the architecture migration.

Validation: exact staged code passed isolated full tests (6212 passed, 87 skipped), typecheck and lint. Three-round source performance comparisons, SQLite migration equivalence, native Electron interaction checks and current-source Linux x64 Host validation completed; eight external text-model requests.

Release note: Supervisor opens reviews without loading the entire graph, keeps long selection lists responsive and preserves details and keyboard navigation. Remote cancellation and Telegram reply recovery are more reliable; remote fixes require an Agent build containing this change.
This commit is contained in:
mesalogo
2026-10-05 14:18:33 +08:00
parent e50ca6d149
commit b90ff50f2a
59 changed files with 2108 additions and 265 deletions
@@ -320,12 +320,13 @@ it('persists supervision knowledge locators and keeps memory sources distinct',
}
database.saveSupervisionResult(result)
const graph = database.getSupervisionGraph()
expect(graph.sources).toEqual(expect.arrayContaining([
expect.objectContaining({ source_type: 'knowledge', locator_json: JSON.stringify({ libraryId: 'library-1', documentId: 'document-1', chunkId: 'chunk-1' }) }),
expect.objectContaining({ source_type: 'memory', locator_json: null })
const sources = (graph.sources as Array<{ id: string }>).map(source => database.getSupervisionSource(source.id)!)
expect(sources).toEqual(expect.arrayContaining([
expect.objectContaining({ sourceType: 'knowledge', locatorJson: JSON.stringify({ libraryId: 'library-1', documentId: 'document-1', chunkId: 'chunk-1' }) }),
expect.objectContaining({ sourceType: 'memory', locatorJson: null })
]))
const source = (graph.sources as Array<{ id: string; source_type: string }>).find((item) => item.source_type === 'knowledge')!
expect(database.getSupervisionSource(source.id)).toMatchObject({ sourceType: 'knowledge', locatorJson: JSON.stringify({ libraryId: 'library-1', documentId: 'document-1', chunkId: 'chunk-1' }) })
const source = sources.find(item => item.sourceType === 'knowledge')!
expect(source).toMatchObject({ content: result.evidence[0]!.content, sourceType: 'knowledge', locatorJson: JSON.stringify({ libraryId: 'library-1', documentId: 'document-1', chunkId: 'chunk-1' }) })
} finally {
database.close()
}
@@ -443,7 +444,8 @@ it.each(['same-project', 'different-project'] as const)('isolates supervision mo
expect(graph.events).toHaveLength(1)
expect(graph.entities).toHaveLength(2)
expect(graph.sources).toHaveLength(1)
expect(graph.sources).toEqual(expect.arrayContaining([expect.objectContaining({ id: sourceId, locator_json: JSON.stringify(results.find((row) => row.id === event.result_id)!.summary === 'First' ? result.evidence[0]!.locator : next.evidence[0]!.locator) })]))
expect(graph.sources).toEqual(expect.arrayContaining([expect.objectContaining({ id: sourceId })]))
expect(database.getSupervisionSource(sourceId)).toMatchObject({ locatorJson: JSON.stringify(results.find((row) => row.id === event.result_id)!.summary === 'First' ? result.evidence[0]!.locator : next.evidence[0]!.locator) })
}
expect(inspected.prepare('PRAGMA foreign_key_check').all()).toEqual([])
} finally { inspected.close() }
@@ -2936,7 +2938,7 @@ describe('AssistantDatabase', () => {
migrated.upgradeStorage(progress, () => false, { migrateNotes: false, reclaimSpace: false })
expect(progress).not.toHaveBeenCalled()
const check = new DatabaseSync(path)
expect(check.prepare('PRAGMA user_version').get()!.user_version).toBe(61)
expect(check.prepare('PRAGMA user_version').get()!.user_version).toBe(ASSISTANT_DATABASE_SCHEMA_VERSION)
expect(tables.map(table => check.prepare(`SELECT rowid, * FROM ${table} ORDER BY rowid`).all())).toEqual(rows)
expect(check.prepare('SELECT type, name, tbl_name, rootpage, sql FROM sqlite_schema ORDER BY name').all()).toEqual(
schema.map(row => ['projects', 'conversations'].includes(row.name as string) && row.type === 'table'
+55 -17
View File
@@ -136,7 +136,7 @@ import {
SUBAGENT_PROGRESS_STORAGE_SCHEMA_VERSION
} from './subagent-progress-storage'
export const ASSISTANT_DATABASE_SCHEMA_VERSION = 61
export const ASSISTANT_DATABASE_SCHEMA_VERSION = 62
function withoutHistoricalWorkMode(value: unknown): unknown {
if (!value || typeof value !== 'object' || Array.isArray(value)) return value
@@ -9144,6 +9144,7 @@ export class AssistantDatabase {
resultId, runId, result.output.summary, result.output.changeDigest,
JSON.stringify(result.output.openItems), JSON.stringify(result.coverage ?? { evidence: result.evidence.length }), timestamp
)
const graph: { entities: Record<string, unknown>[]; relations: Record<string, unknown>[] } = { entities: [], relations: [] }
const saveFacts = (result: StoredSupervisionResult) => {
// Model IDs are local to one batch, never persistent cross-run identities.
const sourceIds = new Map(result.evidence.map((source) => [source.id, randomUUID()]))
@@ -9222,15 +9223,8 @@ export class AssistantDatabase {
story.id, persistentId(entityIds, relation.fromEntityId), persistentId(entityIds, relation.toEntityId), relation.relationType),
source_reference_ids_json: sourceReferences(relation.sourceReferenceIds)
}))
database.prepare(`UPDATE supervision_results SET graph_snapshot_json = json_object(
'entities', json((SELECT json_group_array(json(value)) FROM (
SELECT value FROM json_each(supervision_results.graph_snapshot_json, '$.entities')
UNION ALL SELECT value FROM json_each(?)
))),
'relations', json((SELECT json_group_array(json(value)) FROM (
SELECT value FROM json_each(supervision_results.graph_snapshot_json, '$.relations')
UNION ALL SELECT value FROM json_each(?)
)))) WHERE id = ?`).run(JSON.stringify(entities), JSON.stringify(relations), resultId)
for (const entity of entities) graph.entities.push(entity)
for (const relation of relations) graph.relations.push(relation)
}
if (result.coverage && result.runId) {
for (let offset = 0;; offset += 10) {
@@ -9242,12 +9236,15 @@ export class AssistantDatabase {
// each leaf's full description and references remain independently readable.
database.prepare(`UPDATE supervision_results SET graph_snapshot_json = json_object(
'entities', json((SELECT json_group_array(json(value)) FROM (
SELECT value FROM json_each(supervision_results.graph_snapshot_json, '$.entities') GROUP BY json_extract(value, '$.id')))),
SELECT value FROM json_each(?) GROUP BY json_extract(value, '$.id')))),
'relations', json((SELECT json_group_array(json(value)) FROM (
SELECT value FROM json_each(supervision_results.graph_snapshot_json, '$.relations') GROUP BY json_extract(value, '$.id')))))
WHERE id = ?`).run(resultId)
SELECT value FROM json_each(?) GROUP BY json_extract(value, '$.id')))))
WHERE id = ?`).run(JSON.stringify(graph.entities), JSON.stringify(graph.relations), resultId)
this.supervisionReviewStore().commitCheckpoints(result.runId, result.request)
} else saveFacts(result)
} else {
saveFacts(result)
database.prepare('UPDATE supervision_results SET graph_snapshot_json = ? WHERE id = ?').run(JSON.stringify(graph), resultId)
}
placeTimelineEvents(database, resultId)
supersedeReextractedEvents(database, resultId)
if (result.batch) this.saveReviewCheckpoints(result.batch)
@@ -9273,6 +9270,36 @@ export class AssistantDatabase {
return this.requireDatabase().prepare('SELECT id, summary FROM supervision_results WHERE id = ?').get(id) as { id: string; summary: string } | undefined
}
async listSupervisionResultsAsync(limit = 20, target?: SupervisionTarget, resultId?: string): Promise<ReturnType<AssistantDatabase['listSupervisionResults']>> {
this.requireDatabase()
const reader = this.readonlyQueryReader()
if (reader) return reader.call('supervisionOverview', [limit, target, resultId])
if (this.databasePath !== ':memory:') throw new Error('Supervision worker is not configured')
return this.listSupervisionResults(limit, target, resultId)
}
async getSupervisionGraphAsync(input: SupervisionGraphRequest = {}): Promise<Record<string, unknown>> {
this.requireDatabase()
const reader = this.readonlyQueryReader()
if (reader) return reader.call('supervisionGraph', [input])
if (this.databasePath !== ':memory:') throw new Error('Supervision worker is not configured')
return this.getSupervisionGraph(input)
}
getSupervisionStories(scope: SupervisionRunRequest['scope']) {
const stories = this.supervisionStories()
return { stories: stories.list(scope), experiences: this.supervisionExperiences().list(scope.kind === 'projects' ? scope.projectIds : undefined),
unassigned: stories.unassignedCount(scope), canUndo: stories.canUndo() }
}
async getSupervisionStoriesAsync(scope: SupervisionRunRequest['scope']): Promise<ReturnType<AssistantDatabase['getSupervisionStories']>> {
this.requireDatabase()
const reader = this.readonlyQueryReader()
if (reader) return reader.call('supervisionStories', [scope])
if (this.databasePath !== ':memory:') throw new Error('Supervision worker is not configured')
return this.getSupervisionStories(scope)
}
listSupervisionResults(limit = 20, target?: SupervisionTarget, resultId?: string) {
const owner = target?.type === 'conversation' ? this.getConversation(target.conversationId)
: target?.type === 'task' ? this.getTask(target.taskId) : undefined
@@ -9325,7 +9352,7 @@ export class AssistantDatabase {
attention: supervisionAttention(database, scope.kind === 'projects' ? scope.projectIds : undefined,
new Date(range.from).toISOString(), new Date(range.to).toISOString()),
events: database.prepare('SELECT * FROM supervision_events WHERE result_id = ? ORDER BY occurred_at').all(input.resultId),
sources: database.prepare('SELECT * FROM supervision_sources WHERE result_id = ?').all(input.resultId),
sources: database.prepare('SELECT id, title, occurred_at FROM supervision_sources WHERE result_id = ? ORDER BY rowid').all(input.resultId),
eventEntities: database.prepare(`SELECT ee.event_id, ee.entity_id FROM supervision_event_entities ee JOIN supervision_events e ON e.id = ee.event_id WHERE e.result_id = ?`).all(input.resultId),
eventSources: database.prepare(`SELECT es.event_id, es.source_id FROM supervision_event_sources es JOIN supervision_events e ON e.id = es.event_id WHERE e.result_id = ?`).all(input.resultId)
}
@@ -9352,9 +9379,9 @@ export class AssistantDatabase {
GROUP BY r.from_entity_id, r.to_entity_id, r.relation_type`).all(id),
eventEntities: database.prepare('SELECT event_id, entity_id FROM supervision_event_entities WHERE event_id IN (SELECT id FROM temp.story_events)').all(),
eventSources: database.prepare('SELECT event_id, source_id FROM supervision_event_sources WHERE event_id IN (SELECT id FROM temp.story_events)').all(),
sources: database.prepare(`SELECT * FROM supervision_sources WHERE id IN (SELECT source_id FROM supervision_event_sources
sources: database.prepare(`SELECT id, title, occurred_at FROM supervision_sources WHERE id IN (SELECT source_id FROM supervision_event_sources
WHERE event_id IN (SELECT id FROM temp.story_events)) OR result_id IN (SELECT r.id FROM supervision_results r
JOIN supervision_runs sr ON sr.id = r.run_id WHERE sr.scope_json = ?)`).all(String((story as { scope_json: string }).scope_json)),
JOIN supervision_runs sr ON sr.id = r.run_id WHERE sr.scope_json = ?) ORDER BY rowid`).all(String((story as { scope_json: string }).scope_json)),
attention: this.storyAttention(scope)
}
} finally { database.exec('DROP TABLE IF EXISTS temp.story_events') }
@@ -12334,6 +12361,17 @@ export class AssistantDatabase {
database.enableDefensive(true)
}
}
if (version.user_version < 62) {
database.exec('BEGIN IMMEDIATE')
try {
database.exec(`
CREATE INDEX IF NOT EXISTS supervision_sources_result ON supervision_sources(result_id, occurred_at, source_type, id);
CREATE INDEX IF NOT EXISTS supervision_results_created ON supervision_results(created_at);
PRAGMA user_version = 62;
COMMIT;
`)
} catch (error) { database.exec('ROLLBACK'); throw error }
}
}
supervisionExperiences(): SupervisionExperienceStore {
+24 -12
View File
@@ -196,20 +196,32 @@ export class SupervisionExperienceStore {
}
list(projectIds?: string[]): SupervisionExperience[] {
const rows = this.db.prepare("SELECT * FROM supervision_experiences WHERE status = 'current' ORDER BY created_at, rowid").all()
const links = this.db.prepare(`SELECT x.event_id, x.role, x.note, e.title, e.project_id, COALESCE(e.started_at, e.occurred_at) AS at,
(SELECT s.id FROM supervision_event_stories es JOIN supervision_stories s ON s.id = es.story_id WHERE es.event_id = e.id AND es.is_primary = 1) AS story_id,
(SELECT s.name FROM supervision_event_stories es JOIN supervision_stories s ON s.id = es.story_id WHERE es.event_id = e.id AND es.is_primary = 1) AS story_name
const rows = this.db.prepare(`SELECT * FROM supervision_experiences WHERE status = 'current'
AND id IN (SELECT x.experience_id FROM supervision_experience_events x JOIN supervision_events e ON e.id = x.event_id
WHERE x.role = 'formed' AND e.superseded_by IS NULL)
AND (? IS NULL OR id IN (SELECT x.experience_id FROM supervision_experience_events x JOIN supervision_events e ON e.id = x.event_id
WHERE e.superseded_by IS NULL AND e.project_id != '' AND e.project_id IN (SELECT value FROM json_each(?))))
ORDER BY created_at, rowid`).all(projectIds ? 1 : null, JSON.stringify(projectIds ?? []))
if (!rows.length) return []
// Scope selects experiences, not their links: cross-project context remains complete.
const links = this.db.prepare(`SELECT x.experience_id, x.event_id, x.role, x.note, e.title, e.project_id,
COALESCE(e.started_at, e.occurred_at) AS at, s.id AS story_id, s.name AS story_name
FROM supervision_experience_events x JOIN supervision_events e ON e.id = x.event_id
WHERE x.experience_id = ? AND e.superseded_by IS NULL ORDER BY at, e.id`)
return rows.map(row => {
const events = links.all(String(row.id)).map(link => ({ id: String(link.event_id), role: link.role as 'formed' | 'applied', note: String(link.note),
LEFT JOIN supervision_event_stories es ON es.event_id = e.id AND es.is_primary = 1
LEFT JOIN supervision_stories s ON s.id = es.story_id
WHERE x.experience_id IN (SELECT value FROM json_each(?)) AND e.superseded_by IS NULL
ORDER BY x.experience_id, at, e.id, x.role`).all(JSON.stringify(rows.map(row => row.id)))
const byExperience = new Map<string, SupervisionExperience['events']>()
for (const link of links) {
const id = String(link.experience_id)
const events = byExperience.get(id) ?? []
events.push({ id: String(link.event_id), role: link.role as 'formed' | 'applied', note: String(link.note),
title: String(link.title), projectId: link.project_id ? String(link.project_id) : null, at: String(link.at),
storyId: link.story_id ? String(link.story_id) : null, storyName: link.story_name ? String(link.story_name) : null }))
return { id: String(row.id), statement: String(row.statement), conditions: String(row.conditions), boundaries: String(row.boundaries),
userEdited: row.user_edited === 1, events }
// Experiences are global; a project view keeps those formed or applied in its projects.
}).filter(item => item.events.some(event => event.role === 'formed') && (!projectIds || item.events.some(event => event.projectId && projectIds.includes(event.projectId))))
storyId: link.story_id ? String(link.story_id) : null, storyName: link.story_name ? String(link.story_name) : null })
byExperience.set(id, events)
}
return rows.map(row => ({ id: String(row.id), statement: String(row.statement), conditions: String(row.conditions), boundaries: String(row.boundaries),
userEdited: row.user_edited === 1, events: byExperience.get(String(row.id)) ?? [] }))
}
/** Snapshot of the experiences and links an adjustment changes, for undo. */
@@ -81,7 +81,7 @@ it('supervision preserves result history and protected identities within scope t
expect(read(latest.id).entities.find((entity) => entity.id === confirmedEntity.id)).toMatchObject({ ...confirmedEntity, confirmation_state: 'confirmed' })
expect(read(latest.id).relations[0]).toMatchObject({ id: relationId, reason: 'Original reason', confirmation_state: 'confirmed' })
const storedGraph = db.getSupervisionGraph({ resultId: latest.id })
const currentSource = (storedGraph.sources as Array<{ id: string; source_type: string }>).find((source) => source.source_type === 'conversation')!
const currentSource = (storedGraph.sources as Array<{ id: string }>).find((source) => db.getSupervisionSource(source.id)!.sourceType === 'conversation')!
expect((storedGraph.entities as Array<{ source_reference_ids_json: string }>).every((entity) => entity.source_reference_ids_json === JSON.stringify([currentSource.id]))).toBe(true)
expect(read(a.id)).toEqual(original)
db.applySupervisionRelationAction({ resultId: latest.id, relationId, action: 'revoke' })
@@ -229,3 +229,94 @@ it('retries an actual interrupted direct-model stream and retains its reported u
expect(f.release).toHaveBeenCalledTimes(2)
expect(f.dispose).toHaveBeenCalledOnce()
})
it.each([
['openai-chat-completions', false], ['anthropic-messages', false],
['openai-chat-completions', true], ['anthropic-messages', true]
] as const)(
'retains reported usage when %s output exceeds capacity without retrying (buffered=%s)', async (protocol, buffered) => {
const text = 'x'.repeat(1024 * 1024 + 1)
const events = protocol === 'anthropic-messages'
? [{ type: 'message_start', message: { id: 'call', model: 'fake', usage: { input_tokens: 12, output_tokens: 3 } } },
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text } }]
: [{ choices: [{ delta: { content: text } }], usage: { prompt_tokens: 12, completion_tokens: 3 } }]
let body!: ReadableStreamDefaultController<Uint8Array>
const stream = new ReadableStream<Uint8Array>({ start(controller) { body = controller } })
body.enqueue(new TextEncoder().encode(events.map(event => `data: ${JSON.stringify(event)}\n\n`).join('')))
if (buffered) {
body.enqueue(new TextEncoder().encode(protocol === 'anthropic-messages' ? 'data: {"type":"message_stop"}\n\n' : 'data: [DONE]\n\n'))
body.close()
}
const fetcher = vi.fn<typeof fetch>(async (_input, init) => {
if (!buffered) init!.signal!.addEventListener('abort', () => body.error(init!.signal!.reason), { once: true })
return new Response(stream)
})
const runtime = new ModelAgentRuntime({ baseUrl: 'https://model.invalid', model: 'fake', authentication: 'none',
protocol, toolProvider: noModelTools, fetcher })
const f = fixture(runtime.run.bind(runtime))
f.release.mockImplementation(runtime.releaseConversation.bind(runtime))
f.dispose.mockImplementation(runtime.dispose.bind(runtime))
await expect(f.start()).rejects.toThrow('1024 KiB')
expect(fetcher).toHaveBeenCalledOnce()
expect(stream.locked).toBe(false)
expect(f.db.getTokenUsageSummary().systemTotals).toMatchObject({ callCount: 1, input: 12, output: 3 })
expect(f.release).toHaveBeenCalledOnce()
expect(f.dispose).toHaveBeenCalledOnce()
expect(vi.getTimerCount()).toBe(0)
})
it('drains usage only after overflow and preserves the cap error over later errors and timeout', async () => {
let reason: unknown
const finalized = vi.fn()
const f = fixture(async function* (input, signal) {
try {
yield { type: 'text', requestId: input.requestId, delta: 'x'.repeat(1024 * 1024 + 1) }
reason = signal.reason
expect(signal.aborted).toBe(true)
await new Promise(resolve => setTimeout(resolve, 200))
yield { type: 'text', requestId: input.requestId, delta: 'discard trailing text' }
yield { type: 'model-usage', requestId: input.requestId, callId: 'call', runtime: 'model', provider: 'openai',
model: 'fake', inputTokens: 12, outputTokens: 3, cacheReadTokens: 0, cacheWriteTokens: 0 }
yield { type: 'error', requestId: input.requestId, status: 'failed', message: 'terminated' }
yield { type: 'done', requestId: input.requestId }
} finally { finalized() }
}, 0.1)
const pending = f.start().catch(error => error)
await vi.advanceTimersByTimeAsync(250)
expect(await pending).toBe(reason)
expect(reason).toBeInstanceOf(Error)
expect((reason as Error).message).toContain('1024 KiB')
expect(f.runtime.run).toHaveBeenCalledOnce()
expect(finalized).toHaveBeenCalledOnce()
expect(f.db.getTokenUsageSummary().systemTotals).toMatchObject({ callCount: 1, input: 12, output: 3 })
expect(f.release).toHaveBeenCalledOnce()
expect(f.dispose).toHaveBeenCalledOnce()
expect(vi.getTimerCount()).toBe(0)
})
it('bounds overflow accounting drain and ignores events arriving after cleanup', async () => {
let release!: () => void
const gate = new Promise<void>(resolve => { release = resolve })
const finalized = vi.fn()
const f = fixture(async function* (input) {
try {
yield { type: 'text', requestId: input.requestId, delta: 'x'.repeat(1024 * 1024 + 1) }
await gate
yield { type: 'model-usage', requestId: input.requestId, callId: 'late', runtime: 'model', provider: 'openai',
model: 'fake', inputTokens: 12, outputTokens: 3, cacheReadTokens: 0, cacheWriteTokens: 0 }
} finally { finalized() }
})
const pending = f.start().catch(error => error)
try {
await vi.advanceTimersByTimeAsync(999)
expect(f.release).not.toHaveBeenCalled()
await vi.advanceTimersByTimeAsync(1)
expect(await pending).toMatchObject({ message: expect.stringContaining('1024 KiB') })
expect(f.release).toHaveBeenCalledOnce()
expect(f.dispose).toHaveBeenCalledOnce()
await expect(f.pool.run(async () => 'released')).resolves.toBe('released')
} finally { release(); await vi.advanceTimersByTimeAsync(1) }
expect(finalized).toHaveBeenCalledOnce()
expect(f.db.getTokenUsageSummary().systemTotals).toMatchObject({ callCount: 0 })
expect(vi.getTimerCount()).toBe(0)
})
+28 -10
View File
@@ -73,22 +73,37 @@ async function runSupervisionModel(dependencies: SupervisionModelDependencies, r
}, timeoutSeconds * 1000)
const responseKiB = settings?.supervisionReview?.responseKiB ?? 1024
let output = '', completed = false
let overflowError: Error | undefined
let drainTimeout: ReturnType<typeof setTimeout> | undefined
let drainDeadline: Promise<never> | undefined
let events: ReturnType<AgentRuntime['run']> | undefined
const requestId = randomUUID()
const conversationId = `supervision:${requestId}`
try {
// Each attempt has its own hidden task so reported usage survives retries.
database.createTask({ id: requestId, conversationId, title: request.title, instructions: request.instructions,
origin: 'assistant', visible: false })
for await (const event of runtime.run({ requestId, conversationId, prompt: request.prompt },
modelSignal, async approval => { await request.authorizeTool(approval.toolName ?? approval.scopeKey); return 'deny' })) {
if (event.type === 'text') {
output += event.delta
if (Buffer.byteLength(output) > responseKiB * 1024) {
controller.abort(new Error(`单次模型响应超过 ${responseKiB} KiB。请在监督者设置中调高响应容量后继续;已保存批次会保留。`))
modelSignal.throwIfAborted()
}
}
events = runtime.run({ requestId, conversationId, prompt: request.prompt },
modelSignal, async approval => { await request.authorizeTool(approval.toolName ?? approval.scopeKey); return 'deny' })
while (true) {
const next = events.next()
const step = await (drainDeadline ? Promise.race([next, drainDeadline]) : next)
if (step.done) break
const event = step.value
if (event.type === 'model-usage') persistUsage(event)
if (overflowError) continue
if (event.type === 'text') {
if (Buffer.byteLength(output + event.delta) > responseKiB * 1024) {
overflowError = new Error(`单次模型响应超过 ${responseKiB} KiB。请在监督者设置中调高响应容量后继续;已保存批次会保留。`)
output = ''
controller.abort(overflowError)
// Let the aborted stream emit usage, but never retain further text
// or let an unresponsive iterator hold the supervisor pool forever.
drainDeadline = new Promise<never>((_resolve, reject) => {
drainTimeout = setTimeout(() => reject(overflowError), 1_000)
})
} else output += event.delta
}
if (event.type === 'tool') throw new Error('监督者只允许只读模型摘要,不允许工具调用')
if (event.type === 'error') {
const error = new Error(event.message)
@@ -102,7 +117,7 @@ async function runSupervisionModel(dependencies: SupervisionModelDependencies, r
database.updateTaskStatus(requestId, 'completed')
return output
} catch (error) {
const failure = timeoutError ?? (modelSignal.aborted ? modelSignal.reason : error)
const failure = overflowError ?? timeoutError ?? (modelSignal.aborted ? modelSignal.reason : error)
try {
database.updateTaskStatus(requestId, modelSignal.aborted ? 'cancelled' : 'failed',
failure instanceof Error ? failure.message.slice(0, 2_000) : '监督者模型调用失败')
@@ -110,7 +125,10 @@ async function runSupervisionModel(dependencies: SupervisionModelDependencies, r
if (attempt > 0 || modelSignal.aborted || !isTransientSupervisionError(failure)) throw failure
} finally {
clearTimeout(timeout)
clearTimeout(drainTimeout)
controller.abort()
const closing = events?.return().catch(() => undefined)
if (!overflowError) await closing
await runtime.releaseConversation?.(conversationId)
}
signal.throwIfAborted()
+14 -4
View File
@@ -389,12 +389,22 @@ export class SupervisionStoryStore {
AND EXISTS (SELECT 1 FROM supervision_event_stories es JOIN supervision_events e ON e.id = es.event_id
WHERE es.story_id = s.id AND e.project_id IN (SELECT value FROM json_each(?)))))
ORDER BY s.level, s.name`).all(projects, projects, projects ?? '[]')
const events = this.db.prepare(`SELECT es.event_id, es.is_primary, es.user_set, e.title, e.project_id,
if (!rows.length) return []
const events = this.db.prepare(`SELECT es.story_id, es.event_id, es.is_primary, es.user_set, e.title, e.project_id,
COALESCE(e.started_at, e.occurred_at) AS started_at, COALESCE(e.ended_at, e.occurred_at) AS ended_at
FROM supervision_event_stories es JOIN supervision_events e ON e.id = es.event_id WHERE es.story_id = ? AND e.superseded_by IS NULL ORDER BY started_at, e.id`)
FROM supervision_event_stories es JOIN supervision_events e ON e.id = es.event_id
WHERE es.story_id IN (SELECT value FROM json_each(?)) AND e.superseded_by IS NULL
ORDER BY es.story_id, started_at, e.id`).all(JSON.stringify(rows.map(row => row.id)))
const byStory = new Map<string, SupervisionStory['events']>()
for (const e of events) {
const id = String(e.story_id)
const linked = byStory.get(id) ?? []
linked.push({ id: String(e.event_id), title: String(e.title), projectId: e.project_id ? String(e.project_id) : null,
startedAt: String(e.started_at), endedAt: String(e.ended_at), primary: e.is_primary === 1, userSet: e.user_set === 1 })
byStory.set(id, linked)
}
return rows.map(row => {
const linked = events.all(String(row.id)).map(e => ({ id: String(e.event_id), title: String(e.title), projectId: e.project_id ? String(e.project_id) : null,
startedAt: String(e.started_at), endedAt: String(e.ended_at), primary: e.is_primary === 1, userSet: e.user_set === 1 }))
const linked = byStory.get(String(row.id)) ?? []
// A cross story's span comes from the events it links.
const spanning = row.level === 'cross' ? linked : linked.filter(e => e.primary)
return { id: String(row.id), projectId: row.project_id ? String(row.project_id) : null, projectName: row.project_name ? String(row.project_name) : null,
@@ -0,0 +1,232 @@
// @vitest-environment node
import { randomUUID } from 'node:crypto'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { DatabaseSync } from 'node:sqlite'
import { build } from 'esbuild'
import { afterAll, afterEach, beforeAll, expect, it, vi } from 'vitest'
import { AssistantDatabase, ASSISTANT_DATABASE_SCHEMA_VERSION } from './assistant-database'
import { getPendingAssistantStorageUpgrade, upgradeAssistantStorage } from './assistant-storage-upgrade'
import { supervisionReviewSettingsSchema } from '../../shared/supervision-review-contracts'
import type { SupervisionSummaryOutput } from '../../shared/supervision-contracts'
let directory: string, workerPath: string
const cleanups: Array<() => void> = []
beforeAll(async () => {
directory = await mkdtemp(join(tmpdir(), 'supervision-ui-reads-'))
workerPath = join(directory, 'worker.cjs')
await build({ entryPoints: [resolve('src/main/readonly-query-worker.ts')], outfile: workerPath,
bundle: true, platform: 'node', format: 'cjs', logLevel: 'silent' })
}, 60_000)
afterEach(() => { vi.restoreAllMocks(); for (const close of cleanups.splice(0)) close() })
afterAll(async () => { await rm(directory, { recursive: true, force: true, maxRetries: 10 }) })
const at = '2026-10-01T12:00:00.000Z'
const request = { trigger: 'manual' as const, scope: { kind: 'global' as const }, timeRange: { from: at, to: at } }
const empty: SupervisionSummaryOutput = { summary: 'Review', changeDigest: '', openItems: [], events: [], entities: [], entityChanges: [], relations: [] }
function fixture() {
const path = join(directory, `${randomUUID()}.sqlite`)
const db = new AssistantDatabase(path)
db.initialize(directory)
const sql = (db as unknown as { database: DatabaseSync }).database
cleanups.push(() => db.close())
const project = db.listProjects()[0]!
const other = db.createProject({ name: 'Other', description: '', rootPath: directory })
db.saveSupervisionResult({ request,
evidence: Array.from({ length: 6 }, (_, i) => ({ id: `s${i}`, sourceType: 'conversation' as const, sourceId: 'conversation',
title: `Source ${i}`, content: `Full source ${i} ${'body'.repeat(2000)}`, occurredAt: at,
locator: { projectId: i < 3 ? project.id : other.id } })),
output: { ...empty, events: Array.from({ length: 6 }, (_, i) => ({ title: `Event ${i}`, description: 'Description', occurredAt: at,
eventType: 'decision' as const, sourceReferenceIds: [`s${i}`], entityIds: [] })) } })
const result = db.listSupervisionResults()[0]!
const events = sql.prepare('SELECT id, project_id FROM supervision_events ORDER BY title').all()
for (const [i, event] of events.entries()) {
sql.prepare(`INSERT INTO supervision_stories (id, project_id, level, name, created_at, updated_at)
VALUES (?, ?, 'feature', ?, ?, ?)`).run(`story${i}`, String(event.project_id), `Story ${i}`, at, at)
sql.prepare('INSERT INTO supervision_event_stories (event_id, story_id, is_primary) VALUES (?, ?, 1)').run(String(event.id), `story${i}`)
sql.prepare('INSERT INTO supervision_experiences (id, statement, created_at, updated_at) VALUES (?, ?, ?, ?)').run(`experience${i}`, `Experience ${i}`, at, at)
sql.prepare("INSERT INTO supervision_experience_events (experience_id, event_id, role) VALUES (?, ?, 'formed')").run(`experience${i}`, String(event.id))
}
// A cross-project experience must retain all links, not only the selected project's links.
sql.prepare("INSERT INTO supervision_experience_events (experience_id, event_id, role) VALUES ('experience0', ?, 'applied')").run(String(events[3]!.id))
return { db, sql, path, project, other, result, events }
}
it('returns source headers only in both graph paths while retaining full source detail and source order', () => {
const f = fixture()
const expected = f.sql.prepare('SELECT id, title, occurred_at FROM supervision_sources ORDER BY rowid').all()
for (const input of [{ resultId: f.result.id }, { storyLineId: f.result.storyLineId! }]) {
expect(f.db.getSupervisionGraph(input).sources).toEqual(expected)
}
for (const source of expected) expect(f.db.getSupervisionSource(String(source.id))?.content).toBe(`Full source ${String(source.title).slice(-1)} ${'body'.repeat(2000)}`)
})
it('reads story memberships and scoped experiences in a constant number of SQL statements', () => {
const f = fixture()
let queries = 0
const prepare = f.sql.prepare.bind(f.sql)
vi.spyOn(f.sql, 'prepare').mockImplementation(sql => {
const statement = prepare(sql)
const all = statement.all.bind(statement)
vi.spyOn(statement, 'all').mockImplementation((...args) => { queries++; return all(...args) })
return statement
})
const stories = f.db.supervisionStories().list({ kind: 'global' })
expect(stories.map(story => [story.name, story.events.map(event => event.title)])).toEqual(
Array.from({ length: 6 }, (_, i) => [`Story ${i}`, [`Event ${i}`]]))
expect(queries).toBe(2)
queries = 0
const experiences = f.db.supervisionExperiences().list([f.other.id])
expect(experiences.map(item => item.statement)).toEqual(['Experience 0', 'Experience 3', 'Experience 4', 'Experience 5'])
expect(experiences[0]!.events.map(event => [event.title, event.role, event.storyName])).toEqual(
[0, 3].sort((a, b) => String(f.events[a]!.id).localeCompare(String(f.events[b]!.id))).map(i => [`Event ${i}`, i ? 'applied' : 'formed', `Story ${i}`]))
expect(queries).toBe(2)
queries = 0
expect(f.db.supervisionExperiences().list(['unrelated'])).toEqual([])
expect(queries).toBe(1)
})
it('serves UI reads on the real worker and observes writes committed before the next read', async () => {
const f = fixture()
const overview = f.db.listSupervisionResults()
const graph = f.db.getSupervisionGraph({ resultId: f.result.id })
const stories = { stories: f.db.supervisionStories().list(request.scope), experiences: f.db.supervisionExperiences().list(),
unassigned: f.db.supervisionStories().unassignedCount(request.scope), canUndo: false }
f.db.enableReadonlyWorker(workerPath)
const syncOverview = vi.spyOn(f.db, 'listSupervisionResults').mockImplementation(() => { throw new Error('Main overview') })
const syncGraph = vi.spyOn(f.db, 'getSupervisionGraph').mockImplementation(() => { throw new Error('Main graph') })
const syncStories = vi.spyOn(f.db, 'supervisionStories').mockImplementation(() => { throw new Error('Main stories') })
expect(await f.db.listSupervisionResultsAsync()).toEqual(overview)
expect(await f.db.getSupervisionGraphAsync({ resultId: f.result.id })).toEqual(graph)
expect(await f.db.getSupervisionStoriesAsync(request.scope)).toEqual(stories)
f.sql.prepare('UPDATE supervision_results SET summary = ? WHERE id = ?').run('Committed', f.result.id)
expect((await f.db.listSupervisionResultsAsync())[0]!.summary).toBe('Committed')
await expect(f.db.getSupervisionGraphAsync({ resultId: f.result.id, storyLineId: 'wrong' })).rejects.toThrow('不匹配')
await f.db.readonlyWorkerForTest!.terminateWorkerForTest()
await expect(f.db.listSupervisionResultsAsync()).rejects.toThrow('backing off')
await expect(f.db.getSupervisionGraphAsync({ resultId: f.result.id })).rejects.toThrow('backing off')
await expect(f.db.getSupervisionStoriesAsync(request.scope)).rejects.toThrow('backing off')
expect(syncOverview).not.toHaveBeenCalled()
expect(syncGraph).not.toHaveBeenCalled()
expect(syncStories).not.toHaveBeenCalled()
})
it('rejects missing production worker configuration instead of performing a synchronous UI read', async () => {
const f = fixture()
await expect(f.db.listSupervisionResultsAsync()).rejects.toThrow('worker is not configured')
await expect(f.db.getSupervisionGraphAsync({ resultId: f.result.id })).rejects.toThrow('worker is not configured')
await expect(f.db.getSupervisionStoriesAsync(request.scope)).rejects.toThrow('worker is not configured')
f.db.enableReadonlyWorker(join(directory, 'missing.cjs'))
await expect(f.db.listSupervisionResultsAsync()).rejects.toThrow()
})
it('keeps a multi-statement UI read on one WAL snapshot while another connection commits', () => {
const f = fixture()
const reader = new AssistantDatabase(f.path)
reader.openReadOnly()
cleanups.push(() => reader.close())
reader.readSnapshot(() => {
expect(reader.listSupervisionResults()[0]!.summary).toBe('Review')
f.sql.prepare('UPDATE supervision_results SET summary = ? WHERE id = ?').run('New summary', f.result.id)
f.sql.prepare("UPDATE supervision_stories SET name = 'Renamed' WHERE id = 'story0'").run()
expect(reader.listSupervisionResults()[0]!.summary).toBe('Review')
expect(reader.getSupervisionStories(request.scope).stories[0]!.name).toBe('Story 0')
})
expect(reader.listSupervisionResults()[0]!.summary).toBe('New summary')
expect(reader.getSupervisionStories(request.scope).stories.some(story => story.name === 'Renamed')).toBe(true)
})
it('upgrades schema 61 indexes without changing rows, rowids, source order or reclaiming free pages', () => {
const f = fixture()
const tables = f.sql.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name").all()
const contents = () => tables.map(row => [row.name, f.sql.prepare(`SELECT rowid, * FROM "${row.name}" ORDER BY rowid`).all()])
const before = contents()
const sources = f.db.getSupervisionGraph({ resultId: f.result.id }).sources
f.sql.exec(`DROP INDEX IF EXISTS supervision_sources_result;
DROP INDEX IF EXISTS supervision_results_created;
CREATE TABLE disposable (body BLOB); INSERT INTO disposable VALUES (zeroblob(1048576)); DROP TABLE disposable;
PRAGMA user_version = 61`)
expect(f.sql.prepare('PRAGMA freelist_count').get()!.freelist_count).toBeGreaterThan(0)
f.db.close()
expect(getPendingAssistantStorageUpgrade(f.path)).toEqual({ migrateNotes: false, reclaimSpace: false })
const progress = vi.fn()
upgradeAssistantStorage(f.path, progress)
const reopened = new AssistantDatabase(f.path)
reopened.openReadOnly()
cleanups.push(() => reopened.close())
const sql = (reopened as unknown as { database: DatabaseSync }).database
expect(tables.map(row => [row.name, sql.prepare(`SELECT rowid, * FROM "${row.name}" ORDER BY rowid`).all()])).toEqual(before)
expect(sql.prepare('PRAGMA user_version').get()!.user_version).toBe(ASSISTANT_DATABASE_SCHEMA_VERSION)
expect(sql.prepare("SELECT name FROM sqlite_master WHERE name IN ('supervision_sources_result', 'supervision_results_created') ORDER BY name").all())
.toEqual([{ name: 'supervision_results_created' }, { name: 'supervision_sources_result' }])
expect(reopened.getSupervisionGraph({ resultId: f.result.id }).sources).toEqual(sources)
expect(sql.prepare('PRAGMA integrity_check').get()).toEqual({ integrity_check: 'ok' })
expect(sql.prepare('PRAGMA foreign_key_check').all()).toEqual([])
expect(progress.mock.calls.some(([value]) => value.stage === 'compacting' || value.stage === 'converting')).toBe(false)
expect(getPendingAssistantStorageUpgrade(f.path)).toBeUndefined()
upgradeAssistantStorage(f.path, progress)
})
it('publishes one final snapshot for multiple leaves and rolls back results and checkpoints together', () => {
const f = fixture()
const runId = f.db.startSupervisionRun(request)
const store = f.db.supervisionReviewStore()
f.db.saveLocalConversations([{ header: { id: 'conversation', projectId: f.project.id, title: 'Synthetic', updatedAt: Date.parse(at) },
messages: [0, 1].map(i => ({ id: `source${i}`, role: 'user', state: 'complete', content: 'body', createdAt: Date.parse(at) })) }])
store.initialize(runId, { request, config: { ...supervisionReviewSettingsSchema.parse({}), version: 1, concurrency: 1, timeoutSeconds: 30 }, phase: 'saving' })
for (let i = 0; i < 2; i++) {
const source = f.sql.prepare('SELECT source, revision FROM supervision_review_sources WHERE run_id = ? AND source = ?').get(runId, `message:source${i}`)!
store.save(runId, f.project.id, 'conversation', [{ id: 'source', sourceType: 'conversation', sourceId: 'conversation', title: `Leaf ${i}`, content: 'body', occurredAt: at,
locator: { source: String(source.source), revision: String(source.revision), start: 0, end: 4, projectId: f.project.id } }],
{ ...empty, entities: [{ id: 'entity', label: `Entity ${i}`, description: 'Original', sourceReferenceIds: ['source'] }] })
}
f.sql.exec(`CREATE TRIGGER snapshot_once BEFORE UPDATE OF graph_snapshot_json ON supervision_results
WHEN OLD.graph_snapshot_json != '{"entities":[],"relations":[]}' BEGIN SELECT RAISE(ABORT, 'Repeated graph snapshot'); END;
CREATE TRIGGER fail_checkpoints BEFORE INSERT ON review_checkpoints BEGIN SELECT RAISE(ABORT, 'Checkpoint failed'); END`)
const publication = { request, runId, coverage: store.progress(runId), evidence: [], output: empty }
expect(() => f.db.saveSupervisionResult(publication)).toThrow('Checkpoint failed')
expect(f.db.listSupervisionResults()).toHaveLength(1)
expect(f.sql.prepare('SELECT status FROM supervision_runs WHERE id = ?').get(runId)!.status).toBe('running')
expect(f.sql.prepare('SELECT * FROM review_checkpoints').all()).toEqual([])
f.sql.exec('DROP TRIGGER fail_checkpoints')
f.db.saveSupervisionResult(publication)
const latest = f.db.listSupervisionResults()[0]!
const graph = f.db.getSupervisionGraph({ resultId: latest.id })
expect(graph.entities).toHaveLength(2)
expect(graph.sources).toHaveLength(2)
expect(f.sql.prepare('SELECT COUNT(*) AS n FROM review_checkpoints').get()!.n).toBe(2)
expect(f.sql.prepare('PRAGMA foreign_key_check').all()).toEqual([])
})
it('retains first-leaf snapshot values and source references when persisted identities recur across leaves', () => {
const f = fixture()
f.db.saveSupervisionResult({ request, evidence: [], output: { ...empty,
entities: ['A', 'B'].map(id => ({ id, label: id, description: 'Original', sourceReferenceIds: [] })),
relations: [{ fromEntityId: 'A', toEntityId: 'B', relationType: 'related', reason: 'Original', sourceReferenceIds: [] }] } })
const candidates = f.db.listSupervisionCandidates(request)
const a = candidates.find(item => item.label === 'A')!, b = candidates.find(item => item.label === 'B')!
f.db.applySupervisionEntityAction({ entityId: a.id, action: 'confirm' })
const runId = f.db.startSupervisionRun(request)
const store = f.db.supervisionReviewStore()
f.db.saveLocalConversations([{ header: { id: 'conversation', projectId: f.project.id, title: 'Synthetic', updatedAt: Date.parse(at) },
messages: [0, 1].map(i => ({ id: `source${i}`, role: 'user', state: 'complete', content: 'body', createdAt: Date.parse(at) })) }])
store.initialize(runId, { request, config: { ...supervisionReviewSettingsSchema.parse({}), version: 1, concurrency: 1, timeoutSeconds: 30 }, phase: 'saving' })
for (let i = 0; i < 2; i++) {
const source = f.sql.prepare('SELECT source, revision FROM supervision_review_sources WHERE run_id = ? AND source = ?').get(runId, `message:source${i}`)!
store.save(runId, f.project.id, 'conversation', [{ id: 'source', sourceType: 'conversation', sourceId: 'conversation', title: `Leaf ${i}`, content: 'body', occurredAt: at,
locator: { source: String(source.source), revision: String(source.revision), start: 0, end: 4, projectId: f.project.id } }], { ...empty,
entities: [a, b].map(item => ({ id: item.label, persistedId: item.id, label: item.label, description: `Leaf ${i}`, sourceReferenceIds: ['source'] })),
relations: [{ fromEntityId: 'A', toEntityId: 'B', relationType: 'related', reason: `Leaf ${i}`, sourceReferenceIds: ['source'] }] })
}
f.db.saveSupervisionResult({ request, runId, candidates, coverage: store.progress(runId), evidence: [], output: empty })
const result = f.db.listSupervisionResults()[0]!
const graph = f.db.getSupervisionGraph({ resultId: result.id })
const sourceId = (graph.sources as Array<{ id: string; title: string }>).find(source => source.title === 'Leaf 0')!.id
expect(graph.entities).toEqual([a, b].sort((x, y) => x.id < y.id ? -1 : 1).map(item => expect.objectContaining({ id: item.id,
description: item.id === a.id ? 'Original' : 'Leaf 0', confirmation_state: item.id === a.id ? 'confirmed' : 'automatic',
source_reference_ids_json: JSON.stringify([sourceId]) })))
expect(graph.relations).toEqual([expect.objectContaining({ reason: 'Leaf 0', source_reference_ids_json: JSON.stringify([sourceId]) })])
expect(f.sql.prepare('SELECT description FROM supervision_entities WHERE id = ?').get(b.id)!.description).toBe('Leaf 1')
})