diff --git a/lib/session/session-manager.ts b/lib/session/session-manager.ts index c616d24..4dc0ad4 100644 --- a/lib/session/session-manager.ts +++ b/lib/session/session-manager.ts @@ -16,7 +16,19 @@ export interface SessionManagerOptions { // Clone utilities now imported from ../utils/clone.ts function computeHash(items: InputItem[]): string { - return createHash("sha1").update(JSON.stringify(items)).digest("hex"); + try { + return createHash("sha1").update(JSON.stringify(items)).digest("hex"); + } catch { + return createHash("sha1").update(`fallback_${items.length}`).digest("hex"); + } +} + +function itemsEqual(a: InputItem | undefined, b: InputItem | undefined): boolean { + try { + return JSON.stringify(a) === JSON.stringify(b); + } catch { + return false; + } } function longestSharedPrefixLength(previous: InputItem[], current: InputItem[]): number { @@ -28,7 +40,7 @@ function longestSharedPrefixLength(previous: InputItem[], current: InputItem[]): let length = 0; for (let i = 0; i < limit; i += 1) { - if (JSON.stringify(previous[i]) !== JSON.stringify(current[i])) { + if (!itemsEqual(previous[i], current[i])) { break; } length += 1; @@ -95,7 +107,7 @@ function findSuffixReuseStart(previous: InputItem[], current: InputItem[]): numb const start = previous.length - current.length; for (let index = 0; index < current.length; index += 1) { const prevItem = previous[start + index]; - if (JSON.stringify(prevItem) !== JSON.stringify(current[index])) { + if (!itemsEqual(prevItem, current[index])) { return null; } } diff --git a/spec/session-manager-items-equality.md b/spec/session-manager-items-equality.md new file mode 100644 index 0000000..39b5639 --- /dev/null +++ b/spec/session-manager-items-equality.md @@ -0,0 +1,26 @@ +# Session Manager item equality helper + +## Scope and references + +- lib/session/session-manager.ts: longestSharedPrefixLength (around lines 22-37) uses JSON.stringify comparison +- lib/session/session-manager.ts: findSuffixReuseStart (around lines 92-103) uses JSON.stringify comparison + +## Existing issues + +- None observed related to this refactor (no issue referenced in request) + +## Existing PRs + +- None observed; task requested directly by review comment + +## Definition of done + +- Shared helper (e.g., itemsEqual) added to centralize equality check with safe JSON stringify in try/catch +- longestSharedPrefixLength and findSuffixReuseStart use the helper instead of inline JSON.stringify comparisons +- Build/test commands remain passing or noted if not run + +## Requirements + +- Avoid duplicated JSON.stringify comparisons; centralize error handling for failed stringify +- Apply helper in both functions mentioned in review +- Create new branch and open PR targeting dev when changes are complete