mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-08 06:54:24 +00:00
iMessage: hash full self-chat text
This commit is contained in:
@@ -58,15 +58,19 @@ describe("createSelfChatCache", () => {
|
||||
expect(cache.has({ ...directLookup, text: "message-512", createdAt: 512 })).toBe(true);
|
||||
});
|
||||
|
||||
it("handles long texts without requiring the full body in the cache key", () => {
|
||||
it("does not collide long texts that differ only in the middle", () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-03-07T00:00:00Z"));
|
||||
|
||||
const cache = createSelfChatCache();
|
||||
const longText = `${"a".repeat(800)}middle${"b".repeat(800)}`;
|
||||
const prefix = "a".repeat(256);
|
||||
const suffix = "b".repeat(256);
|
||||
const longTextA = `${prefix}${"x".repeat(300)}${suffix}`;
|
||||
const longTextB = `${prefix}${"y".repeat(300)}${suffix}`;
|
||||
|
||||
cache.remember({ ...directLookup, text: longText, createdAt: 123 });
|
||||
cache.remember({ ...directLookup, text: longTextA, createdAt: 123 });
|
||||
|
||||
expect(cache.has({ ...directLookup, text: longText, createdAt: 123 })).toBe(true);
|
||||
expect(cache.has({ ...directLookup, text: longTextA, createdAt: 123 })).toBe(true);
|
||||
expect(cache.has({ ...directLookup, text: longTextB, createdAt: 123 })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -21,8 +21,6 @@ export type SelfChatCache = {
|
||||
const SELF_CHAT_TTL_MS = 10_000;
|
||||
const MAX_SELF_CHAT_CACHE_ENTRIES = 512;
|
||||
const CLEANUP_MIN_INTERVAL_MS = 1_000;
|
||||
const DIGEST_TEXT_HEAD_CHARS = 256;
|
||||
const DIGEST_TEXT_TAIL_CHARS = 256;
|
||||
|
||||
function normalizeText(text: string | undefined): string | null {
|
||||
if (!text) {
|
||||
@@ -36,15 +34,8 @@ function isUsableTimestamp(createdAt: number | undefined): createdAt is number {
|
||||
return typeof createdAt === "number" && Number.isFinite(createdAt);
|
||||
}
|
||||
|
||||
function buildDigestSource(text: string): string {
|
||||
if (text.length <= DIGEST_TEXT_HEAD_CHARS + DIGEST_TEXT_TAIL_CHARS) {
|
||||
return text;
|
||||
}
|
||||
return `${text.slice(0, DIGEST_TEXT_HEAD_CHARS)}\u0000${text.length}\u0000${text.slice(-DIGEST_TEXT_TAIL_CHARS)}`;
|
||||
}
|
||||
|
||||
function digestText(text: string): string {
|
||||
return createHash("sha256").update(buildDigestSource(text)).digest("hex");
|
||||
return createHash("sha256").update(text).digest("hex");
|
||||
}
|
||||
|
||||
function buildScope(parts: SelfChatCacheKeyParts): string {
|
||||
|
||||
Reference in New Issue
Block a user