Files
orca/src/main/agent-hooks/server/server-ingest-structured.ts
T
641e4792b9 Startup shows chats as Interrupted or Done without reading their history (#32146)
* 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>
2026-10-29 10:20:52 -07:00

205 lines
8.0 KiB
TypeScript

import type { StructuredAgentSessionStatusPublisher } from '../../native-chat/agent-session-wire/structured-agent-session-status-ownership'
import type { AgentSessionStatusSummary } from '../../../shared/agent-session-wire'
import type { AgentStatusIpcPayload } from '../../../shared/agent-status-types'
import {
parseAgentStatusSubject,
serializeAgentStatusSubject,
type AgentStatusStructuredSessionSubject
} from '../../../shared/agent-status-subject'
import {
structuredAgentSessionPaneKey,
structuredAgentSessionTabId
} from '../../../shared/structured-agent-session-projection'
import {
continueMainAgentStatus,
isAgentStatusHeldOpenByChildWork,
mainAgentTurnInterrupted
} from '../../../shared/agent-lead-status-fold'
import { structuredAgentSessionAgentStatus } from '../../../shared/structured-agent-session-agent-status'
import {
structuredAgentSessionDatedMainAgent,
structuredAgentSessionRowStateStartedAt
} from '../../../shared/structured-agent-session-status-started-at'
import { structuredStatusLegacyEvent } from './server-structured-status-row'
import { AgentHookServerIngestTerminal } from './server-ingest-terminal'
export abstract class AgentHookServerIngestStructured extends AgentHookServerIngestTerminal {
withSavedStructuredStatusGroup(
visit: (sink: StructuredAgentSessionStatusPublisher) => void
): void {
let pendingAt: number | undefined
const defer = (mutate: (notify: () => void) => void): void => {
const before = this.statusChangeNotificationVersion
mutate(() => {
// Reentrant live notifications already include this committed saved mutation.
if (before === this.statusChangeNotificationVersion) {
pendingAt = before
}
})
}
const flush = (): void => {
const pending = pendingAt
pendingAt = undefined
if (pending !== undefined && pending === this.statusChangeNotificationVersion) {
this.notifyStatusChangeListeners()
}
}
try {
visit({
publish: (summary, subject) =>
defer((notify) => this.ingestStructuredStatus(summary, subject, notify)),
forget: (subject) => defer((notify) => this.dropStructuredStatus(subject, notify))
})
} catch (error) {
try {
flush()
} catch {
/* Preserve the original visit failure. */
}
throw error
}
flush()
}
ingestStructuredStatus(
summary: AgentSessionStatusSummary,
subject: AgentStatusStructuredSessionSubject,
notify = () => this.notifyStatusChangeListeners()
): void {
const parsed = parseAgentStatusSubject(subject)
if (
!parsed ||
parsed.kind !== 'structured-session' ||
parsed.sessionId !== summary.sessionId ||
parsed.workspaceId !== summary.workspaceId ||
!Number.isFinite(summary.updatedAt) ||
summary.updatedAt < 0
) {
throw new Error('Structured status does not match its trusted owner subject')
}
if (!summary.status) {
this.dropStructuredStatus(parsed, notify)
return
}
const previous = this.canonicalStatusStore.getParent(parsed)
const priorStatus = previous?.status
// Read canonical children; the summary's task list loses information.
const agentStatus = structuredAgentSessionAgentStatus({
status: summary.status,
childWork: this.canonicalStatusStore.getChildren(parsed),
turnOutcome: summary.turnOutcome,
...(summary.stopping ? { stopping: summary.stopping } : {})
})
const { state, workingMode } = agentStatus
// Journal dates preserve main-agent continuity across republishing after restart.
const mainAgent = continueMainAgentStatus(
priorStatus?.mainAgent,
structuredAgentSessionDatedMainAgent(agentStatus.mainAgent, summary),
summary.updatedAt
)
const tabId = structuredAgentSessionTabId(parsed.sessionId)
const paneKey = structuredAgentSessionPaneKey(tabId, parsed.sessionId)
if (this.state.lastStatusByPaneKey.has(paneKey)) {
throw new Error('Structured status address conflicts with legacy evidence')
}
const snapshot = this.canonicalStatusStore.getRevision()
const observedAt = Math.max(Date.now(), priorStatus?.receivedAt ?? 0)
const status: AgentStatusIpcPayload = {
paneKey,
tabId,
worktreeId: parsed.workspaceId,
connectionId: null,
structuredHost: summary.hostExecutionOwned ? 'owned' : 'held',
...(summary.providerSession ? { providerSession: summary.providerSession } : {}),
state,
...(workingMode ? { workingMode } : {}),
mainAgent,
// Older readers use interrupted instead of mainAgent.
interrupted: mainAgentTurnInterrupted(mainAgent),
prompt: summary.latestPrompt,
agentType: summary.agent,
...(summary.model ? { model: summary.model } : {}),
...(summary.toolName ? { toolName: summary.toolName } : {}),
...(summary.toolInput ? { toolInput: summary.toolInput } : {}),
...(summary.lastAssistantMessage
? { lastAssistantMessage: summary.lastAssistantMessage }
: {}),
receivedAt: observedAt,
// Child-held work needs a live observation clock; the stopped main-agent journal would make it expire.
evidenceObservedAt: isAgentStatusHeldOpenByChildWork({ state, mainAgent })
? observedAt
: summary.updatedAt,
// Monitoring becoming a real turn must restart the clock even if state remains working.
stateStartedAt:
structuredAgentSessionRowStateStartedAt({ state, mainAgent }, summary) ??
(priorStatus?.state === state && priorStatus.workingMode === workingMode
? priorStatus.stateStartedAt
: summary.updatedAt),
observation: {
origin: 'structured',
kind: 'transition',
authorityId: snapshot.epoch,
incarnation: 0,
revision: snapshot.revision + 1,
observedAt: summary.updatedAt
}
}
const publication = this.canonicalStatusStore.applyMutation({
parent: { subject: parsed, status, firstObservedAt: previous?.firstObservedAt ?? Date.now() }
})
if (!publication) {
return
}
const key = serializeAgentStatusSubject(parsed)
const subjects =
this.canonicalSubjectsByPane.get(paneKey) ??
new Map<string, AgentStatusStructuredSessionSubject>()
subjects.set(key, parsed)
this.canonicalSubjectsByPane.set(paneKey, subjects)
if (!this.canonicalListingOrder.has(key)) {
this.canonicalListingOrder.set(key, this.nextStatusListingOrder())
}
const committed = this.canonicalStatusStore.getParent(parsed)?.status
if (!committed) {
throw new Error('Committed structured status is missing')
}
const after = structuredStatusLegacyEvent(committed)
this.commitStatusRowMutation(priorStatus && structuredStatusLegacyEvent(priorStatus), after)
notify()
this.emitEnrichedStatus(after)
}
/** Only the owning feed can forget a structured row; pane cleanup cannot. */
dropStructuredStatus(
subject: AgentStatusStructuredSessionSubject,
notify = () => this.notifyStatusChangeListeners()
): void {
const parsed = parseAgentStatusSubject(subject)
if (!parsed || parsed.kind !== 'structured-session') {
throw new Error('Structured status removal requires its exact owner subject')
}
const previous = this.canonicalStatusStore.getParent(parsed)
if (!previous) {
return
}
const publication = this.canonicalStatusStore.applyMutation({
removeParent: parsed
})
if (!publication) {
return
}
const key = serializeAgentStatusSubject(parsed)
this.canonicalListingOrder.delete(key)
if (previous.status) {
const subjects = this.canonicalSubjectsByPane.get(previous.status.paneKey)
subjects?.delete(key)
if (subjects?.size === 0) {
this.canonicalSubjectsByPane.delete(previous.status.paneKey)
}
this.commitStatusRowMutation(structuredStatusLegacyEvent(previous.status), undefined)
notify()
this.emitStatusDropped(previous.status.paneKey)
}
}
}