(
oldSessionId: string,
newSessionId: string,
namespace: string,
options: { deleteOldSession: boolean; mergeAgentState?: boolean }
)
| 680 | } |
| 681 | |
| 682 | private async mergeSessionData( |
| 683 | oldSessionId: string, |
| 684 | newSessionId: string, |
| 685 | namespace: string, |
| 686 | options: { deleteOldSession: boolean; mergeAgentState?: boolean } |
| 687 | ): Promise<void> { |
| 688 | if (oldSessionId === newSessionId) { |
| 689 | return |
| 690 | } |
| 691 | |
| 692 | const oldStored = this.store.sessions.getSessionByNamespace(oldSessionId, namespace) |
| 693 | const newStored = this.store.sessions.getSessionByNamespace(newSessionId, namespace) |
| 694 | if (!oldStored || !newStored) { |
| 695 | throw new Error('Session not found for merge') |
| 696 | } |
| 697 | |
| 698 | const movedMessages = this.store.messages.mergeSessionMessages(oldSessionId, newSessionId) |
| 699 | if (movedMessages.moved > 0) { |
| 700 | if (!options.deleteOldSession) { |
| 701 | this.publisher.emit({ type: 'messages-invalidated', sessionId: oldSessionId, namespace }) |
| 702 | } |
| 703 | this.publisher.emit({ type: 'messages-invalidated', sessionId: newSessionId, namespace }) |
| 704 | } |
| 705 | |
| 706 | const mergedMetadata = this.mergeSessionMetadata(oldStored.metadata, newStored.metadata) |
| 707 | if (mergedMetadata !== null && mergedMetadata !== newStored.metadata) { |
| 708 | for (let attempt = 0; attempt < 2; attempt += 1) { |
| 709 | const latest = this.store.sessions.getSessionByNamespace(newSessionId, namespace) |
| 710 | if (!latest) break |
| 711 | const result = this.store.sessions.updateSessionMetadata( |
| 712 | newSessionId, |
| 713 | mergedMetadata, |
| 714 | latest.metadataVersion, |
| 715 | namespace, |
| 716 | { touchUpdatedAt: false } |
| 717 | ) |
| 718 | if (result.result === 'success') { |
| 719 | break |
| 720 | } |
| 721 | if (result.result === 'error') { |
| 722 | break |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | if (newStored.model === null && oldStored.model !== null) { |
| 728 | const updated = this.store.sessions.setSessionModel(newSessionId, oldStored.model, namespace, { |
| 729 | touchUpdatedAt: false |
| 730 | }) |
| 731 | if (!updated) { |
| 732 | throw new Error('Failed to preserve session model during merge') |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if (newStored.modelReasoningEffort === null && oldStored.modelReasoningEffort !== null) { |
| 737 | const updated = this.store.sessions.setSessionModelReasoningEffort(newSessionId, oldStored.modelReasoningEffort, namespace, { |
| 738 | touchUpdatedAt: false |
| 739 | }) |
no test coverage detected