Skip to content

fix(storage): persist raw messages before updating summaries - #724

Open
Elon-Jiang wants to merge 1 commit into
2FastLabs:mainfrom
Elon-Jiang:fix/persist-before-summary
Open

Elon-Jiang wants to merge 1 commit into
2FastLabs:mainfrom
Elon-Jiang:fix/persist-before-summary

Conversation

@Elon-Jiang

Copy link
Copy Markdown

Issue Link

Fixes #723

Summary

Persist raw chat messages before appending to the active summary buffer or invoking the summarizer in Python, TypeScript and Swift. Previously, a save-time summarizer exception prevented the raw write, while a failed raw write could leave an unsaved message in the buffer.

Changes

  • Reorder the existing five save paths: Python single/batch, TypeScript single, Swift single/batch. Preserve inner return values, arguments, error propagation, lazy activation and compression thresholds.
  • Add regressions for summarizer failure after a raw write and raw-store failure before a buffer update, covering single/batch methods where available.
  • Document the ordering and error timing in runtime docstrings and the language usage guides; update the Swift README as required.

User experience

With an active summary buffer, a summarizer exception no longer prevents saving the original message. If the inner write raises, the wrapper does not append that attempted message or invoke the summarizer.

A summarizer error can still propagate after the raw write commits. This is not a transactional or retry-idempotency guarantee. Existing concurrent-write coordination and same-role message filtering are outside this change.

Validation

Against main 729d5f52869c8280f599a9fa5cf27fc1df445602 before the production changes:

  • Python summary tests: 4 new failures, 12 passing controls.
  • TypeScript summary tests: 2 new failures, 11 passing controls.
  • Swift: both new parameterized regressions fail for single and batch saves (four cases).

After the changes:

  • Python 3.12.13 isolated editable install with the repository's test_requirements.txt: make test — 447 passed. Ruff checks on the full source tree and changed Python tests pass.
  • TypeScript locked npm ci: npm test -- --runInBand — 218 passed across 20 suites; npm run lint and npm run build pass. Two additional failure-path checks against the built JavaScript storage modules pass.
  • Swift 6.2.3: complete swift test --skip-update — 426 tests across 38 suites passed; summary regressions pass. No iOS build was run.
  • The standalone Python reproduction changes from raw history missing the new message to raw history containing it. git diff --check passes.

Offline runtime tests only: scripted summarizer failures, in-memory stores and explicit failing storage boundaries, no paid model or browser calls. Python dependency downloads timed out twice; the final install completed before the full successful suite. AWS variables in that test process use dummy credentials with metadata lookup disabled.

Contribution notes

All 86 open PR file lists were checked completely, alongside all-state summary-storage searches, the Python file history and the latest default branch. No overlapping failure-ordering fix was found. Root and Python contribution guides, the required issue link and the actual Apache-2.0 license were checked.

AI assistance (OpenAI Codex) was used for investigation, implementation and executed validation. No production incident or independent human review is claimed.

@cornelcroi

Copy link
Copy Markdown
Collaborator

@claude review this PR.

  1. Backward compatibility first: no removed or renamed public symbol, no breaking signature change, no behaviour change for users who don't opt in, optional dependencies stay optional. Any break is a blocker.
  2. Docs: every change needs its docs update, and new features need a usage example.
  3. Tests for the behaviour change, happy path and the edge case.
  4. Python and TypeScript parity: does the same bug or feature exist on the other side?
  5. Then correctness and the rest of the code.

Verdict first. Blockers in a numbered list, then non-blocking notes, kept separate. Plain, short words.

@claude

claude Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Claude finished @cornelcroi's task in 15s —— View job


Verdict: Approve (no blockers)

The change is small and safe. It fixes the same bug in Python, TypeScript and Swift. I read the diff only. I did not run the tests.

Blockers

None.

Checks against your list

  1. Backward compatibility: OK. No symbol was removed or renamed. No signature changed. Return values are the same. Only the order of calls inside the save methods changed. Nothing new is opt-in or optional.
  2. Docs: OK. Class docstrings (Python, TS, Swift), the three SKILL.md files and the Swift README are updated. This is a bug fix, so no new usage example is needed.
  3. Tests: OK. Two cases are covered, each for single and batch saves where a batch method exists:
    • The summarizer fails after the raw write. The raw message is still stored.
    • The raw write fails. The buffer is unchanged and the summarizer is not called.
  4. Python and TS parity: OK. TS has no batch save method (saveChatMessages is not in typescript/src/storage), so one fix is enough there. Swift is covered as well.
  5. Correctness: Good. Each method now calls the inner store first, then updates the buffer and compresses. The Python and Swift batch versions also appended after the write.

Non-blocking notes

  • Retry risk after summarizer failure: The raw write commits, then the summarizer error is raised. A caller who retries on that error will store the message twice. The PR text says this is not an idempotency guarantee. Consider one line in the user-facing guides saying "do not blindly retry on a summarizer error". Right now the warning is only in the docstrings.
  • Buffer skips the message when the summarizer fails: The message is appended to the buffer before compression, so it stays in the buffer. Behaviour here depends on whether _compress_if_needed keeps the buffer when it raises. I did not see a test for the buffer state after a summarizer failure. A test that checks fetch_chat after the failure would pin this down.
  • Existing race window: Concurrent saves to the same key can still interleave, as the PR says. That is out of scope.
  • PR body is long. The validation and contribution notes could be shorter. This has no effect on the code.

This branch has not been deployed

No deployments
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.

Bug: summary updates run before raw chat persistence

2 participants