Skip to content

fix: guard against null sessionIds on Memory objects#644

Closed
washi4 wants to merge 1 commit into
rohitg00:mainfrom
washi4:fix/sessionIds-null-guard
Closed

fix: guard against null sessionIds on Memory objects#644
washi4 wants to merge 1 commit into
rohitg00:mainfrom
washi4:fix/sessionIds-null-guard

Conversation

@washi4
Copy link
Copy Markdown

@washi4 washi4 commented May 25, 2026

Problem

memory.sessionIds[0] crashes at runtime when sessionIds is null or undefined. This happens when Memory objects are loaded from KV store via import, migration, or corrupted data — the TypeScript type declares sessionIds: string[] but runtime doesn't enforce this.

Error:

TypeError: Cannot read properties of null (reading '0')

Root Cause

mem::import writes memory objects directly to KV without normalizing the sessionIds field. External data or older export formats may have sessionIds: null instead of sessionIds: [].

Fix

Two-pronged approach:

  1. Defensive guards at all 3 direct access points using (memory.sessionIds ?? [])[0] ?? "memory" pattern
  2. Root cause prevention — normalize sessionIds during import to prevent null from persisting in the KV store

Changed Files

File Change
src/state/memory-utils.ts Guard in memoryToObservation()
src/functions/search.ts Guard in rebuildIndex()
src/functions/remember.ts Guard in vectorIndexAddGuarded call
src/functions/export-import.ts Normalize sessionIds during import

Testing

  • Build passes (npm run build)
  • All existing tests pass (7 pre-existing failures in embedding-provider tests unrelated to this change)

Summary by CodeRabbit

  • Bug Fixes
    • Improved robustness of data import to normalize missing session IDs and avoid errors with incomplete or legacy data.
    • Enhanced defensive handling of session identifiers across memory, search, and indexing flows to prevent failures when session info is absent.

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented May 25, 2026

@washi4 is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fe5ff27a-406d-4c26-a013-aedd505f29bd

📥 Commits

Reviewing files that changed from the base of the PR and between 3b15154 and e5eedc6.

📒 Files selected for processing (4)
  • src/functions/export-import.ts
  • src/functions/remember.ts
  • src/functions/search.ts
  • src/state/memory-utils.ts

📝 Walkthrough

Walkthrough

Normalize incoming memory.sessionIds to an array during import and read sessionId defensively across utilities, remember, and search code paths, defaulting to "memory" when none exists.

Changes

SessionIds Field Robustness

Layer / File(s) Summary
Import normalization for sessionIds
src/functions/export-import.ts
Memory import forces memory.sessionIds to an array ([]) when the incoming sessionIds is missing or not an array before persisting.
Safe sessionId access in core functions
src/state/memory-utils.ts, src/functions/remember.ts, src/functions/search.ts
Derive sessionId with (memory.sessionIds ?? [])[0] (or equivalent) and fallback to "memory" when no session id is available, avoiding direct indexing of a potentially missing array.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A careful rabbit hops through code so sweet,

checks each session-nest to make it neat,
turns missing nests into tidy arrays,
so indexing never trips its ways,
and memories rest safe in ordered bays.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main objective of the PR: adding defensive guards against null/undefined sessionIds on Memory objects across multiple files and normalizing sessionIds during import.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/functions/export-import.ts`:
- Around line 378-379: Remove the WHAT-style inline comment above the sessionIds
normalization; instead either delete the comment entirely or replace it with a
brief WHY-focused note (e.g., "Ensure downstream code can safely iterate
sessionIds from older exports") near the sessionIds normalization logic in
export-import.ts so the code relies on clear naming (sessionIds,
normalizeSessionIds or the surrounding block) rather than explaining what the
code is doing.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 25b4c11e-fbc7-4c76-9448-75431fcecd13

📥 Commits

Reviewing files that changed from the base of the PR and between d38a1c6 and 3b15154.

📒 Files selected for processing (4)
  • src/functions/export-import.ts
  • src/functions/remember.ts
  • src/functions/search.ts
  • src/state/memory-utils.ts

Comment thread src/functions/export-import.ts Outdated
Memory.sessionIds can be null/undefined when loaded from KV store
(via import, migration, or corrupted data), causing runtime crashes
on `memory.sessionIds[0]` access.

Changes:
- src/state/memory-utils.ts: guard in memoryToObservation()
- src/functions/search.ts: guard in rebuildIndex()
- src/functions/remember.ts: guard in vectorIndexAddGuarded call
- src/functions/export-import.ts: normalize sessionIds during import

Signed-off-by: washi4 <wayne.shi1@ingka.ikea.com>
@washi4 washi4 force-pushed the fix/sessionIds-null-guard branch from 3b15154 to e5eedc6 Compare May 25, 2026 12:38
@rohitg00
Copy link
Copy Markdown
Owner

Thanks @washi4 — during bug-fix triage we landed an equivalent guard in #684. Closing as superseded.

@rohitg00 rohitg00 closed this May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants