Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/compression/layers/observation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";

import { compressObservations } from "./observation.js";
import type { NormalizedMessage } from "../types.js";

describe("compressObservations large block deduplication", () => {
it("keeps distinct messages that share only a leading 200 characters", () => {
const sharedPrefix = "The quick brown fox jumps over the lazy dog. ".repeat(5);
const firstContent = `${sharedPrefix}\nALPHA_UNIQUE ${"a".repeat(400)}`;
const secondContent = `${sharedPrefix}\nBETA_UNIQUE ${"b".repeat(400)}`;
const first: NormalizedMessage = { role: "assistant", content: firstContent };
const second: NormalizedMessage = { role: "assistant", content: secondContent };

expect(firstContent.slice(0, 200)).toBe(secondContent.slice(0, 200));

const result = compressObservations([first, second]);

expect(result.messages[1].content).toContain("BETA_UNIQUE");
expect(result.messages[1].content).not.toContain("same content");
});

it("still drops a genuinely repeated large block", () => {
const sharedPrefix = "The quick brown fox jumps over the lazy dog. ".repeat(5);
const content = `${sharedPrefix}\nIDENTICAL ${"c".repeat(400)}`;
const first: NormalizedMessage = { role: "assistant", content };
const second: NormalizedMessage = { role: "assistant", content };

const result = compressObservations([first, second]);

expect(result.messages[1].content).toContain("same content");
});
});
6 changes: 4 additions & 2 deletions src/compression/layers/observation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ function deduplicateLargeBlocks(messages: NormalizedMessage[]): {
return msg;
}

// Hash first 200 chars as block identifier
const blockKey = msg.content.slice(0, 200);
// Use the full content as the block identifier. A 200-char prefix can match
// two messages whose tails differ, which would drop distinct information and
// emit a false "same content" pointer for the second message.
const blockKey = msg.content;

if (blockHashes.has(blockKey)) {
const firstIdx = blockHashes.get(blockKey)!;
Expand Down
Loading