mirror of
https://github.com/stablyai/orca.git
synced 2026-10-30 16:47:34 +00:00
* feat(agent-session-journal): save each chat's open root turn beside its epoch A restart could only tell whether a chat was cut mid-turn by replaying its whole journal. journal_sessions now keeps the open root turn (open_turn_id, open_turn_since), set by the row that opens a turn and cleared only by a finish the provider reported, in that row's own transaction. A turn a quit, crash or Stop cut short stays open, which is what the next launch reads as Interrupted. Journal schema version 5 adds the two nullable columns with a forward migration; a new epoch starts with no turn open. * fix(native-chat): restore chat tabs at startup without replaying any history Startup opened every chat in the saved tab index and replayed its whole journal into the main process to learn its status. With hundreds of tabs and gigabytes of history, Orca froze in GC on every launch. Startup now lists each restored chat from its record and publishes its row as Interrupted (a turn was open when its host stopped, or the last quit's resume offer lists it) or Done, through the status stream and the agent-status sink, reading only each chat's journal_sessions row and its newest row's time. A chat's history is read when something opens it; that open settles what the gone generation left running and publishes the journal's own row over the restored one. No other startup path replays chats: restart offers list from their markers without opening chats, and orchestration mail opens the chats it must read one at a time. Opened chats are capped at 24 resident folds; opening one past the cap closes the least recently active folds that are only a cache, with the idle sweep's own close test. * Fix lazy chat restoration and cache ownership * Restore saved chat work across runtime restarts * Wait for committed structural progress in migration fixture * Correct restored ownership and bounded journal recovery * Preserve bounded work selection during legacy recovery * Complete deferred restart status publication * Fix restart recovery test fixtures * Use legal legacy recovery fixtures * Correct population delete observation * Bound legacy journal replay in a readonly worker * Repair legacy worker proof oracles * Reduce legacy scratch key serialization cost * Decode population meter namespaces with scratch UTF-16 encoding * Consolidate structured session imports for CI * Remove redundant casts and document checked type exceptions * Correct compiler contracts and retain journal fixture invariants * Fix checked test fixture contracts after compiler validation * Name population fixture identity type in exported signature * Preserve historical quit presentation after rewind * Fix structured session retry and remote worker delivery * Preserve settlement and restored status contracts * Enable legacy chat readiness on standalone hosts * Repair structured session static checks * Verify retained submission identity across rewinds * Show saved chat status without replaying history at startup * Keep chats closed when startup status finishes loading * Keep startup owner recovery independent of history loading * Preserve intentional quit verdicts in saved chat status * Use a checked host fixture in startup restoration tests * Group saved startup roster publication and verify scoped status work * Use checked array access in status cut counters * Complete checked saved status fixture typing * Declare portable roster counter contract * Use current saved chat startup APIs in contracts * Preserve complete chat tab authority across startup * Repair checked startup restoration test consumers * Keep authority fixture module mocks in test entries * Consolidate authority test fixture imports * Check restore fixture contracts and recorded queue recovery --------- Co-authored-by: m4air <m4air@Mac.localdomain> Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
71 lines
2.9 KiB
TypeScript
71 lines
2.9 KiB
TypeScript
import type { AgentSessionResumeTrigger } from '../../../shared/agent-session-resume-marker'
|
|
import type { StructuredAgentSessionHost } from '../../native-chat/agent-session-wire/structured-agent-session-host'
|
|
import type { JournalHostDatabase } from '../../native-chat/agent-session-journal/journal-host-database'
|
|
import { recordAgentSessionRuntimeEnd } from '../agent-session-runtime-end-record'
|
|
|
|
export type InstalledRuntime = {
|
|
host: StructuredAgentSessionHost
|
|
adapter: { closeAll(): Promise<void> }
|
|
/** Closed last, once every conversation has drained into it. */
|
|
journalDatabase: JournalHostDatabase
|
|
// Wait for observed adapter exits to publish and all recovery callbacks they raised to settle.
|
|
waitForRecovery: () => Promise<void>
|
|
/** Host bookkeeping that runs on timers, stopped before anything else. */
|
|
stopBackgroundWork?: () => void
|
|
}
|
|
|
|
/** Latch the resume-marker trigger: the startup-ordering ratchet requires the quit call without arguments. */
|
|
let teardownTrigger: AgentSessionResumeTrigger = 'quit'
|
|
|
|
export function setStructuredAgentSessionTeardownTrigger(trigger: AgentSessionResumeTrigger): void {
|
|
teardownTrigger = trigger
|
|
}
|
|
|
|
export function structuredAgentSessionTeardownTrigger(): AgentSessionResumeTrigger {
|
|
return teardownTrigger
|
|
}
|
|
|
|
export async function tearDownRuntime(
|
|
installed: InstalledRuntime,
|
|
trigger: AgentSessionResumeTrigger
|
|
): Promise<void> {
|
|
// Record quit before any await so an interrupted shutdown is not mistaken for a crash.
|
|
recordAgentSessionRuntimeEnd(trigger)
|
|
// Stop delivery before recovery can wake queued sends and start children teardown would kill.
|
|
installed.stopBackgroundWork?.()
|
|
installed.host.stopDelivery()
|
|
// Drain an in-flight recovery before stopping children; recovery may still
|
|
// be writing lifecycle rows or acquiring a replacement child.
|
|
await installed.waitForRecovery()
|
|
const failures: unknown[] = []
|
|
// Evict host sessions first so child stop, tail drain, settlement and lease release run with their sinks.
|
|
// closeAll backs up unindexed/refused children; drain their late exit callbacks before closing the database.
|
|
try {
|
|
await installed.host.flushAllStreamedEvents({ trigger })
|
|
} catch (error) {
|
|
failures.push(error)
|
|
}
|
|
try {
|
|
// Backstop for children eviction never took: unindexed acquisitions and refused evictions.
|
|
await installed.adapter.closeAll()
|
|
} catch (error) {
|
|
failures.push(error)
|
|
}
|
|
// A backstop close can still deliver a final exit callback.
|
|
try {
|
|
await installed.waitForRecovery()
|
|
} catch (error) {
|
|
failures.push(error)
|
|
}
|
|
await installed.host.drainStatusWrites()
|
|
if (failures.length === 0) {
|
|
// Close only after all sinks drain and conversations settle.
|
|
installed.journalDatabase.close()
|
|
return
|
|
}
|
|
if (failures.length === 1) {
|
|
throw failures[0]
|
|
}
|
|
throw new AggregateError(failures, 'structured agent-session runtime teardown failed')
|
|
}
|