(
workspaceId: string,
metadata: CompactionCompletionMetadata
)
| 880 | } |
| 881 | |
| 882 | async getMessagesForCompactionEpoch( |
| 883 | workspaceId: string, |
| 884 | metadata: CompactionCompletionMetadata |
| 885 | ): Promise<Result<{ messages: MuxMessage[]; summary: MuxMessage }>> { |
| 886 | assert( |
| 887 | typeof workspaceId === "string" && workspaceId.trim().length > 0, |
| 888 | "workspaceId is required" |
| 889 | ); |
| 890 | assert( |
| 891 | metadata.workspaceId === workspaceId, |
| 892 | "compaction metadata workspace must match request" |
| 893 | ); |
| 894 | assert( |
| 895 | isNonNegativeInteger(metadata.summaryHistorySequence), |
| 896 | "summaryHistorySequence must be a non-negative integer" |
| 897 | ); |
| 898 | |
| 899 | try { |
| 900 | const messages: MuxMessage[] = []; |
| 901 | let summary: MuxMessage | undefined; |
| 902 | const lowerBound = metadata.previousBoundaryHistorySequence; |
| 903 | const seenHistorySequences = new Set<number>(); |
| 904 | |
| 905 | // The just-compacted epoch can straddle chat-archive.jsonl and chat.jsonl after |
| 906 | // sealed-history rotation, so scan the full logical history under the workspace |
| 907 | // lock; otherwise a concurrent boundary rotation can move rows between files mid-scan. |
| 908 | const iteration = await this.fileLocks.withLock(workspaceId, () => |
| 909 | this.iterateFullHistory(workspaceId, "forward", (chunk) => { |
| 910 | for (const message of chunk) { |
| 911 | const sequence = message.metadata?.historySequence; |
| 912 | if (!isNonNegativeInteger(sequence)) continue; |
| 913 | if (seenHistorySequences.has(sequence)) continue; |
| 914 | seenHistorySequences.add(sequence); |
| 915 | |
| 916 | if ( |
| 917 | sequence === metadata.summaryHistorySequence && |
| 918 | message.id === metadata.summaryMessageId |
| 919 | ) { |
| 920 | summary = message; |
| 921 | continue; |
| 922 | } |
| 923 | |
| 924 | if (sequence >= metadata.summaryHistorySequence) continue; |
| 925 | if (lowerBound !== undefined && sequence <= lowerBound) continue; |
| 926 | if (message.id === metadata.compactionRequestMessageId) continue; |
| 927 | if (isDurableContextBoundaryMarker(message)) continue; |
| 928 | messages.push(message); |
| 929 | } |
| 930 | }) |
| 931 | ); |
| 932 | if (!iteration.success) { |
| 933 | return Err(`Failed to read compaction epoch messages: ${iteration.error}`); |
| 934 | } |
| 935 | |
| 936 | if (summary === undefined) { |
| 937 | return Err(`Compaction summary not found: ${metadata.summaryMessageId}`); |
| 938 | } |
| 939 |
no test coverage detected