fix: keep crash repair away from live sessions

This commit is contained in:
_Kerman
2026-07-23 18:31:37 +08:00
parent 022b42a450
commit 7f5ba286fe
12 files changed
+161 -30

No files matched your search

@@ -256,6 +256,8 @@ export class PersistenceCoordinator<TornMarker = unknown> {
* @returns the header plus the event log, ending on a balanced `turn/end`.
*/
load(id: SessionId): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
const live = this.ctx.sessions.get(id)
if (live !== undefined) return this.loadLiveSnapshot(live)
return this.serialize(id, () => this.loadCore(id))
}
@@ -279,6 +281,18 @@ export class PersistenceCoordinator<TornMarker = unknown> {
return { meta, events: balanced }
}
/** Return a durable balanced live snapshot without applying cold crash repair. */
private async loadLiveSnapshot(session: Session): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
const meta = structuredClone(session.header)
const events = session.events.map(event => structuredClone(event))
await this.flush(session)
if (events.length === 0) throw new Error(`session "${session.id}" not found`)
if (interruptedTurnClosers(events).length > 0) {
throw new Error(`cannot load session "${session.id}" while its live turn is open; use the live Session or wait for the turn to close`)
}
return { meta, events }
}
// Listing is a direct backend read and needs no coordinator state.
// --- per-id serialization + adoption helpers ---
@@ -74,9 +74,11 @@ export abstract class SessionPersistence extends Service {
/**
* Load a header and balanced contiguous log. A complete interrupted final
* turn is preserved and durably closed with missing tool errors plus any open
* step and turn boundaries; only a torn final record is discarded. Unknown
* versions and corruption in the committed prefix reject.
* turn is preserved and durably closed with missing tool errors plus any open
* step and turn boundaries; only a torn final record is discarded. Unknown
* versions and corruption in the committed prefix reject. Implementations
* MUST NOT crash-repair an identity still bound to a live Session: a balanced
* live log may return as a durable snapshot, while an open live turn rejects.
* @param id - the persisted session to reload.
* @returns the header and a log ending on a balanced `turn/end`.
*/