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
16 changes: 16 additions & 0 deletions src/memory/__tests__/ranking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,20 @@ describe("memory ranking", () => {
expect(shouldIncludeEpisodeInContext(staleWeak)).toBe(false);
expect(shouldIncludeEpisodeInContext(durableRepeat)).toBe(true);
});

test("invalid timestamps degrade gracefully", () => {
const score = calculateEpisodeRecallScore(
0.5,
{
importance: 0.6,
accessCount: 2,
startedAt: "not-a-date",
lastAccessedAt: "still-not-a-date",
decayRate: 1,
},
"metadata",
);

expect(score).toBeFinite();
});
});
14 changes: 6 additions & 8 deletions src/memory/ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function calculateEpisodeContextScore(episode: Episode): number {
decayRate: episode.decay_rate,
});

return weightedAverage(signals.durability, signals.recency, 0, 0.6, 0.4, 0);
return weightedAverage2(signals.durability, signals.recency, 0.6, 0.4);
}

export function shouldIncludeEpisodeInContext(episode: Episode): boolean {
Expand Down Expand Up @@ -75,6 +75,10 @@ function weightedAverage(a: number, b: number, c: number, aWeight: number, bWeig
return a * aWeight + b * bWeight + c * cWeight;
}

function weightedAverage2(a: number, b: number, aWeight: number, bWeight: number): number {
return a * aWeight + b * bWeight;
}

function exponentialDecay(ageHours: number, halfLifeHours: number, decayRate: number): number {
if (!Number.isFinite(ageHours) || ageHours < 0) return 1;
return Math.exp(-((ageHours / halfLifeHours) * decayRate));
Expand All @@ -83,13 +87,7 @@ function exponentialDecay(ageHours: number, halfLifeHours: number, decayRate: nu
function hoursSince(value?: number | string): number {
if (value == null) return Number.POSITIVE_INFINITY;

const timestamp =
typeof value === "number"
? value
: (() => {
const parsed = Date.parse(value);
return Number.isNaN(parsed) ? Number.NaN : parsed;
})();
const timestamp = typeof value === "number" ? value : Date.parse(value);

if (!Number.isFinite(timestamp)) return Number.POSITIVE_INFINITY;

Expand Down
Loading