* Clear archive-related metadata on an archived session so it can be resumed. * - Removes `lifecycleState`, `archivedBy`, `archiveReason`, and stamps * `lifecycleStateSince` so subsequent CLI lifecycle writes still win on time. * - For Cursor sessions that pre-date #799 (no `cursorS
(sessionId: string)
| 525 | * No-op when metadata is null (callers should pre-check). |
| 526 | */ |
| 527 | async clearSessionArchiveMetadata(sessionId: string): Promise<{ cursorSessionProtocol?: 'acp' | 'stream-json' }> { |
| 528 | const session = this.sessions.get(sessionId) |
| 529 | if (!session) { |
| 530 | throw new Error('Session not found') |
| 531 | } |
| 532 | |
| 533 | const currentMetadata = session.metadata |
| 534 | if (!currentMetadata) { |
| 535 | throw new Error('Session metadata missing') |
| 536 | } |
| 537 | |
| 538 | const next: Record<string, unknown> = { ...currentMetadata } |
| 539 | delete next.lifecycleState |
| 540 | delete next.archivedBy |
| 541 | delete next.archiveReason |
| 542 | next.lifecycleStateSince = Date.now() |
| 543 | |
| 544 | let cursorSessionProtocol: 'acp' | 'stream-json' | undefined |
| 545 | if (currentMetadata.flavor === 'cursor') { |
| 546 | const existing = currentMetadata.cursorSessionProtocol |
| 547 | if (existing === 'acp' || existing === 'stream-json') { |
| 548 | cursorSessionProtocol = existing |
| 549 | } else if (currentMetadata.cursorSessionId) { |
| 550 | // Pre-#799 default: presence of cursorSessionId without protocol means stream-json. |
| 551 | cursorSessionProtocol = 'stream-json' |
| 552 | next.cursorSessionProtocol = 'stream-json' |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | const result = this.store.sessions.updateSessionMetadata( |
| 557 | sessionId, |
| 558 | next, |
| 559 | session.metadataVersion, |
| 560 | session.namespace, |
| 561 | { touchUpdatedAt: false } |
| 562 | ) |
| 563 | |
| 564 | if (result.result === 'error') { |
| 565 | throw new Error('Failed to update session metadata') |
| 566 | } |
| 567 | |
| 568 | if (result.result === 'version-mismatch') { |
| 569 | throw new Error('Session was modified concurrently. Please try again.') |
| 570 | } |
| 571 | |
| 572 | this.refreshSession(sessionId) |
| 573 | return cursorSessionProtocol ? { cursorSessionProtocol } : {} |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Restore archive-related metadata fields that were captured before a reopen attempt. |
no test coverage detected