* Restore archive-related metadata fields that were captured before a reopen attempt. * Used when `resumeSession` fails after `clearSessionArchiveMetadata` already ran so the * session does not drift into a "not archived, not active" zombie state. * * Restores the four archive fi
(
sessionId: string,
snapshot: {
lifecycleState?: string
archivedBy?: string
archiveReason?: string
lifecycleStateSince?: number
}
)
| 586 | * on version mismatch so the caller can decide whether to retry. |
| 587 | */ |
| 588 | async restoreSessionArchiveMetadata( |
| 589 | sessionId: string, |
| 590 | snapshot: { |
| 591 | lifecycleState?: string |
| 592 | archivedBy?: string |
| 593 | archiveReason?: string |
| 594 | lifecycleStateSince?: number |
| 595 | } |
| 596 | ): Promise<void> { |
| 597 | const session = this.sessions.get(sessionId) |
| 598 | if (!session) return |
| 599 | const current = session.metadata |
| 600 | if (!current) return |
| 601 | |
| 602 | const next: Record<string, unknown> = { ...current } |
| 603 | if (snapshot.lifecycleState !== undefined) { |
| 604 | next.lifecycleState = snapshot.lifecycleState |
| 605 | } else { |
| 606 | delete next.lifecycleState |
| 607 | } |
| 608 | if (snapshot.archivedBy !== undefined) { |
| 609 | next.archivedBy = snapshot.archivedBy |
| 610 | } else { |
| 611 | delete next.archivedBy |
| 612 | } |
| 613 | if (snapshot.archiveReason !== undefined) { |
| 614 | next.archiveReason = snapshot.archiveReason |
| 615 | } else { |
| 616 | delete next.archiveReason |
| 617 | } |
| 618 | if (snapshot.lifecycleStateSince !== undefined) { |
| 619 | next.lifecycleStateSince = snapshot.lifecycleStateSince |
| 620 | } else { |
| 621 | delete next.lifecycleStateSince |
| 622 | } |
| 623 | |
| 624 | const result = this.store.sessions.updateSessionMetadata( |
| 625 | sessionId, |
| 626 | next, |
| 627 | session.metadataVersion, |
| 628 | session.namespace, |
| 629 | { touchUpdatedAt: false } |
| 630 | ) |
| 631 | |
| 632 | if (result.result === 'error') { |
| 633 | throw new Error('Failed to restore archive metadata') |
| 634 | } |
| 635 | |
| 636 | if (result.result === 'version-mismatch') { |
| 637 | throw new Error('Session was modified concurrently during reopen rollback') |
| 638 | } |
| 639 | |
| 640 | this.refreshSession(sessionId) |
| 641 | } |
| 642 | |
| 643 | async deleteSession(sessionId: string): Promise<void> { |
| 644 | const session = this.sessions.get(sessionId) |
no test coverage detected