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
18 changes: 15 additions & 3 deletions lib/session/session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@
// 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;
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

function longestSharedPrefixLength(previous: InputItem[], current: InputItem[]): number {
Expand All @@ -28,7 +40,7 @@
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;
Expand Down Expand Up @@ -95,7 +107,7 @@
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;
}
}
Expand Down Expand Up @@ -571,7 +583,7 @@
}

private resetSessionInternal(sessionId: string, forceRandomKey = false): SessionState | undefined {
const existing = this.sessions.get(sessionId);

Check warning on line 586 in lib/session/session-manager.ts

View workflow job for this annotation

GitHub Actions / Lint & Typecheck

File has too many lines (517). Maximum allowed is 500

Check warning on line 586 in lib/session/session-manager.ts

View workflow job for this annotation

GitHub Actions / Lint & Typecheck

File has too many lines (517). Maximum allowed is 500
const keySeed = existing?.id ?? sessionId;
const promptCacheKey = forceRandomKey
? `cache_${randomUUID()}`
Expand Down
26 changes: 26 additions & 0 deletions spec/session-manager-items-equality.md
Original file line number Diff line number Diff line change
@@ -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
Loading