|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import test from "node:test" |
| 3 | +import type { CompressionBlock } from "../lib/state" |
| 4 | +import { |
| 5 | + appendMissingBlockSummaries, |
| 6 | + injectBlockPlaceholders, |
| 7 | + parseBlockPlaceholders, |
| 8 | + validateSummaryPlaceholders, |
| 9 | + wrapCompressedSummary, |
| 10 | + type BoundaryReference, |
| 11 | +} from "../lib/tools/utils" |
| 12 | + |
| 13 | +function createBlock(blockId: number, body: string): CompressionBlock { |
| 14 | + return { |
| 15 | + blockId, |
| 16 | + active: true, |
| 17 | + deactivatedByUser: false, |
| 18 | + compressedTokens: 0, |
| 19 | + topic: `Block ${blockId}`, |
| 20 | + startId: "m0001", |
| 21 | + endId: "m0002", |
| 22 | + anchorMessageId: `msg-${blockId}`, |
| 23 | + compressMessageId: `compress-${blockId}`, |
| 24 | + includedBlockIds: [], |
| 25 | + consumedBlockIds: [], |
| 26 | + parentBlockIds: [], |
| 27 | + directMessageIds: [], |
| 28 | + directToolIds: [], |
| 29 | + effectiveMessageIds: [`msg-${blockId}`], |
| 30 | + effectiveToolIds: [], |
| 31 | + createdAt: blockId, |
| 32 | + summary: wrapCompressedSummary(blockId, body), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function createMessageBoundary(messageId: string, rawIndex: number): BoundaryReference { |
| 37 | + return { |
| 38 | + kind: "message", |
| 39 | + messageId, |
| 40 | + rawIndex, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +test("compress placeholder validation keeps valid placeholders and ignores invalid ones", () => { |
| 45 | + const summaryByBlockId = new Map([ |
| 46 | + [1, createBlock(1, "First compressed summary")], |
| 47 | + [2, createBlock(2, "Second compressed summary")], |
| 48 | + ]) |
| 49 | + const summary = "Intro (b1) unknown (b9) duplicate (b1) out-of-range (b2) outro" |
| 50 | + const parsed = parseBlockPlaceholders(summary) |
| 51 | + |
| 52 | + const missingBlockIds = validateSummaryPlaceholders( |
| 53 | + parsed, |
| 54 | + [1], |
| 55 | + createMessageBoundary("msg-a", 0), |
| 56 | + createMessageBoundary("msg-b", 1), |
| 57 | + summaryByBlockId, |
| 58 | + ) |
| 59 | + |
| 60 | + assert.deepEqual( |
| 61 | + parsed.map((placeholder) => placeholder.blockId), |
| 62 | + [1], |
| 63 | + ) |
| 64 | + assert.equal(missingBlockIds.length, 0) |
| 65 | + |
| 66 | + const injected = injectBlockPlaceholders( |
| 67 | + summary, |
| 68 | + parsed, |
| 69 | + summaryByBlockId, |
| 70 | + createMessageBoundary("msg-a", 0), |
| 71 | + createMessageBoundary("msg-b", 1), |
| 72 | + ) |
| 73 | + |
| 74 | + assert.match(injected.expandedSummary, /First compressed summary/) |
| 75 | + assert.doesNotMatch(injected.expandedSummary, /Second compressed summary/) |
| 76 | + assert.match(injected.expandedSummary, /\(b9\)/) |
| 77 | + assert.match(injected.expandedSummary, /\(b2\)/) |
| 78 | + assert.deepEqual(injected.consumedBlockIds, [1]) |
| 79 | +}) |
| 80 | + |
| 81 | +test("compress continues by appending required block summaries the model omitted", () => { |
| 82 | + const summaryByBlockId = new Map([[1, createBlock(1, "Recovered compressed summary")]]) |
| 83 | + const summary = "The model forgot to include the prior block." |
| 84 | + const parsed = parseBlockPlaceholders(summary) |
| 85 | + |
| 86 | + const missingBlockIds = validateSummaryPlaceholders( |
| 87 | + parsed, |
| 88 | + [1], |
| 89 | + createMessageBoundary("msg-a", 0), |
| 90 | + createMessageBoundary("msg-b", 1), |
| 91 | + summaryByBlockId, |
| 92 | + ) |
| 93 | + |
| 94 | + assert.deepEqual(missingBlockIds, [1]) |
| 95 | + |
| 96 | + const injected = injectBlockPlaceholders( |
| 97 | + summary, |
| 98 | + parsed, |
| 99 | + summaryByBlockId, |
| 100 | + createMessageBoundary("msg-a", 0), |
| 101 | + createMessageBoundary("msg-b", 1), |
| 102 | + ) |
| 103 | + const finalSummary = appendMissingBlockSummaries( |
| 104 | + injected.expandedSummary, |
| 105 | + missingBlockIds, |
| 106 | + summaryByBlockId, |
| 107 | + injected.consumedBlockIds, |
| 108 | + ) |
| 109 | + |
| 110 | + assert.match( |
| 111 | + finalSummary.expandedSummary, |
| 112 | + /The following previously compressed summaries were also part of this conversation section:/, |
| 113 | + ) |
| 114 | + assert.match(finalSummary.expandedSummary, /### \(b1\)/) |
| 115 | + assert.match(finalSummary.expandedSummary, /Recovered compressed summary/) |
| 116 | + assert.deepEqual(finalSummary.consumedBlockIds, [1]) |
| 117 | +}) |
0 commit comments