Skip to content
Open
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
45 changes: 45 additions & 0 deletions src/memory/__tests__/consolidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,51 @@ describe("consolidateSession", () => {
expect(storedFacts.length).toBe(0);
});

test("duplicate identical user messages produce a single fact", async () => {
const { memory, storedFacts } = createMockMemory();
const data = makeTestSessionData({
userMessages: [
"Actually, the staging server is on port 3001 not 3000",
"Actually, the staging server is on port 3001 not 3000",
"Actually, the staging server is on port 3001 not 3000",
],
});

const result = await consolidateSession(memory, data);

expect(result.factsExtracted).toBe(1);
expect(storedFacts.length).toBe(1);
});

test("a message matching both correction and preference patterns produces a single fact", async () => {
const { memory, storedFacts } = createMockMemory();
const data = makeTestSessionData({
userMessages: ["No, make sure to use feature branches from now on"],
});

const result = await consolidateSession(memory, data);

expect(result.factsExtracted).toBe(1);
expect(storedFacts.length).toBe(1);
expect(storedFacts[0].tags).toContain("correction");
});

test("dedup is case- and whitespace-insensitive within a session", async () => {
const { memory, storedFacts } = createMockMemory();
const data = makeTestSessionData({
userMessages: [
"I prefer PRs over direct pushes",
" I PREFER PRs over direct pushes ",
"i prefer PRs over direct pushes",
],
});

const result = await consolidateSession(memory, data);

expect(result.factsExtracted).toBe(1);
expect(storedFacts.length).toBe(1);
});

test("episode detail includes tools and files", async () => {
const { memory, storedEpisodes } = createMockMemory();
const data = makeTestSessionData({
Expand Down
8 changes: 8 additions & 0 deletions src/memory/consolidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ function calculateImportance(data: SessionData): number {
function extractFactsFromSession(data: SessionData, episodeId: string): SemanticFact[] {
const facts: SemanticFact[] = [];
const now = new Date().toISOString();
const seenKeys = new Set<string>();

for (const message of data.userMessages) {
const key = message.toLowerCase().trim();
if (seenKeys.has(key)) {
continue;
}
const lower = message.toLowerCase();

if (matchesCorrectionPattern(lower)) {
Expand All @@ -125,6 +130,8 @@ function extractFactsFromSession(data: SessionData, episodeId: string): Semantic
category: "user_preference",
tags: ["correction"],
});
seenKeys.add(key);
continue;
}

if (matchesPreferencePattern(lower)) {
Expand All @@ -143,6 +150,7 @@ function extractFactsFromSession(data: SessionData, episodeId: string): Semantic
category: "user_preference",
tags: ["preference"],
});
seenKeys.add(key);
}
}

Expand Down
Loading