Skip to content

fix: preserve import session key for existing jsonl sessions - #777

Closed
mturac wants to merge 1 commit into
rohitg00:mainfrom
mturac:fix/issue-775-import-jsonl-session-key
Closed

mturac wants to merge 1 commit into
rohitg00:mainfrom
mturac:fix/issue-775-import-jsonl-session-key

Conversation

@mturac

@mturac mturac commented Jun 2, 2026 •

Copy link
Copy Markdown

Summary

  • update existing imported sessions using the parsed session id instead of existing.id
  • keep malformed stored session records from re-keying state::set with an undefined key
  • add a regression test for importing a JSONL file when the existing session record has no id

Closes #775

Tests

  • npm test -- test/replay.test.ts
  • npm run build

Summary by CodeRabbit

  • Bug Fixes

    • Fixed session persistence during JSONL imports so existing sessions are correctly updated and finalized.
  • Tests

    • Expanded tests for JSONL session import using filesystem-backed fixtures and in-memory mocks; verifies session updates (observation count, completed status, and import tag).

@vercel

vercel Bot commented Jun 2, 2026

Copy link
Copy Markdown

@mturac 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

coderabbitai Bot commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0a663c25-7c74-47ec-99d2-d95a8eaf0284

📥 Commits

Reviewing files that changed from the base of the PR and between eac3ac8 and f4d33ee.

📒 Files selected for processing (2)
  • src/functions/replay.ts
  • test/replay.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/functions/replay.ts
  • test/replay.test.ts

📝 Walkthrough

Walkthrough

Fix import-jsonl so existing sessions are upserted using parsed.sessionId (filling existing.id if missing), and add async filesystem-based tests with in-memory mockKV/mockSdk and a JSONL import test validating session updates.

Changes

Session KV key fix for import-jsonl

Layer / File(s) Summary
KV session write key fix
src/functions/replay.ts
Ensure existing.id is initialized from parsed.sessionId when missing and persist the updated session with kv.set(KV.sessions, parsed.sessionId, existing) instead of using existing.id.
Test infrastructure and import-jsonl test
test/replay.test.ts
Switch tests to async node:fs/promises (mkdtemp, rm, writeFile), add mockKV() and mockSdk() in-memory helpers, register replay functions on the mock SDK, write a session.jsonl fixture to a temp dir, trigger mem::replay::import-jsonl, and assert the import result and updated stored session fields (observationCount, status, tags).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 In a patch both tidy and spry,
Parsed IDs now never run dry.
Mocks and JSONL lead the test race,
Sessions saved to the proper place —
Hooray, imports hop on by!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 clearly summarizes the main code change: preserving the import session key for existing JSONL sessions by using parsed.sessionId instead of existing.id.
Linked Issues check ✅ Passed The PR addresses issue #775 by replacing existing.id with parsed.sessionId in the KV write operation, preventing undefined keys and serialization errors during JSONL imports of existing sessions. A regression test confirms the fix.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing issue #775: the key change in replay.ts and the corresponding regression test in replay.test.ts are both necessary and on-topic.

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

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

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/functions/replay.ts (1)

394-409: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Heal the malformed record by setting existing.id.

The key fix is correct — writing under parsed.sessionId matches the read key on Line 394 and avoids the undefined-key serialization failure. However, the upserted record itself still has id undefined when the stored session was malformed. Any later consumer that reads this session (e.g. mem::replay::sessions / mem::replay::load) and re-keys on session.id will hit the same class of bug. Assigning the id here repairs the underlying broken state rather than only the write key.

🛠️ Proposed fix to heal the record
         const existing = await kv.get<Session>(KV.sessions, parsed.sessionId);
         if (existing) {
+          existing.id = parsed.sessionId;
           existing.observationCount =
             (existing.observationCount || 0) + parsed.observations.length;
🤖 Prompt for 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.

In `@src/functions/replay.ts` around lines 394 - 409, The upsert heals the store
key but leaves a malformed Session without an id; before calling
kv.set(KV.sessions, parsed.sessionId, existing) ensure the Session record has
its id set to the storage key by assigning existing.id = parsed.sessionId when
missing (check existing.id falsy), then proceed with updating observationCount,
endedAt, status, tags, firstPrompt and persisting via kv.set so downstream
consumers (e.g., mem::replay::sessions / mem::replay::load) won't reintroduce
the undefined-id bug.
🤖 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.

Outside diff comments:
In `@src/functions/replay.ts`:
- Around line 394-409: The upsert heals the store key but leaves a malformed
Session without an id; before calling kv.set(KV.sessions, parsed.sessionId,
existing) ensure the Session record has its id set to the storage key by
assigning existing.id = parsed.sessionId when missing (check existing.id falsy),
then proceed with updating observationCount, endedAt, status, tags, firstPrompt
and persisting via kv.set so downstream consumers (e.g., mem::replay::sessions /
mem::replay::load) won't reintroduce the undefined-id bug.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e3c30072-8ff8-4492-a1ab-d66b0034b648

📥 Commits

Reviewing files that changed from the base of the PR and between 4b593e0 and eac3ac8.

📒 Files selected for processing (2)
  • src/functions/replay.ts
  • test/replay.test.ts

@mturac
mturac force-pushed the fix/issue-775-import-jsonl-session-key branch from eac3ac8 to f4d33ee Compare June 2, 2026 11:45
@rohitg00

rohitg00 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Thanks @mturac — same fix shipped in v0.9.25 (see PR #791 + release notes). #776 + this PR + my own diagnosis all converged on the same root cause; credit goes to whoever filed first. Closing because the issue is resolved on main.

@rohitg00 rohitg00 closed this Jun 2, 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.

import-jsonl aborts the entire batch ("missing field key") — existing-session branch re-keys on existing.id instead of parsed.sessionId

2 participants