Skip to content

fix(memorize): drop resent messages a memcell already holds - #609

Merged
gloryfromca merged 3 commits into
mainfrom
fix/resend-duplicate-memcells
Nov 20, 2026
Merged

gloryfromca merged 3 commits into
mainfrom
fix/resend-duplicate-memcells

Conversation

@gloryfromca

Copy link
Copy Markdown
Member

Summary

Fixes #274: a client that retries /memory/add after a timeout produced a second memcell (and episode) holding the same messages.

Cause. Deduplication only compared fresh messages with the session buffer (merge_dedupe_sort). A cut moves messages out of the buffer into a memcell, so a resend of those messages looked new and was cut again.

Fix.

  • The boundary stage (buffer and detect) drops fresh messages that a memcell of the session already lists (SessionBufferStore.already_cut). A buffered message is kept, since the tail may share rows with the last cell.
  • No new table: the memcell rows are the record. The cut inserts them in the same transaction that empties the buffer (fix(storage): make session writes atomic under contention #579), and a session delete removes them, so a resend after /memory/delete with that session_id is memorized again.
  • Message ids no longer depend on the message's position in the request: m_<session>_<ts>_<n>_<digest>. n counts the request's messages sharing that timestamp (so they keep request order when rows sort by (timestamp, id)), and digest is 8 hex chars of SHA-256 over sender_id, role, content, tool_call_id and tool_calls (sender_name excluded). A split or regrouped resend therefore keeps its ids, and two different messages sent in separate requests with one timestamp no longer share an id; on main the second one was silently dropped.

How this covers the review points on #495 (#495 (comment)):

Point Here
① independent of the #537 input log uses only the memcell rows and the buffer
② message identity: position change on resend, different messages in one millisecond id = timestamp + per-timestamp index + content digest, no request position
③ dedupe record in the same transaction as the cut the memcell insert is the record, already inside the cut transaction
④ reproduce on latest main first, with a regression test reproduced below; three integration tests added

Thanks to @flyto168 for #495, which this supersedes.

Area

  • Architecture method

Verification

Real `everos server start`, fake upstream whose boundary reply cuts before the 3rd message;
the same POST /memory/add (4 user messages) sent twice; memcell rows read from system.db.

                 2nd /add             memcells                          episodes
main c2c8d6fc    200 "extracted"      2, both [m..._000, m..._001]      2
this branch      200 "accumulated"    1                                 1   (log: boundary_resent_messages_dropped)

New tests, each red with its part of the fix removed:
  test_resent_batch_is_not_cut_twice                         red without the already_cut filter
  test_regrouped_resend_keeps_ids_and_drops_what_was_cut     red without the already_cut filter
  test_two_messages_with_one_millisecond_in_two_requests_both_stay   red without the digest
  + tests/unit/.../test_ingest/test_id_gen.py, test_stores.py::test_already_cut_names_what_this_sessions_memcells_hold

pytest tests/unit/test_memory/test_extract tests/unit/test_infra/test_sqlite tests/unit/test_service
       tests/unit/test_properties/test_boundary_stateful.py tests/unit/test_memory/test_search/test_manager.py
       tests/integration/test_memorize_agent_mode.py tests/integration/test_memorize_window_segmentation.py
  -> all pass after updating the tests that pinned the old id format
make lint -> pass

Tests changed because they demanded the old behavior: test_quarantine.py pinned literal m_s1_<ts>_000 ids and now derives them with message_ids; test_boundary_stateful.py documented the same-millisecond collision as a known property of the old scheme, which this removes.

Checklist

  • I kept the change scoped to the relevant area.
  • I am opening this from a separate branch, not pushing directly to main.
  • I updated docs, examples, or setup notes when behavior changed.
  • I added or updated tests when the change affects behavior.
  • I did not commit secrets, .env files, dependency folders, or generated output.
  • Active relative links in Markdown files resolve.

Notes for Reviewers

  • Upgrade: messages buffered by an earlier version keep their old ids, so a batch sent before the upgrade and resent after it is not recognized as a resend (one-time; noted in CHANGELOG).
  • already_cut scans the session's memcells with json_each (marked ponytail:); a (session, message_id) table is the upgrade path if one session ever holds enough memcells for it to show.
  • Identical messages (same sender, role, content, timestamp) sent in separate requests are treated as one; within one request they stay distinct through n.
  • The CHANGELOG.md ### Fixed section will conflict textually with fix(memorize): bound the bodies failed async batches keep #607 (whichever lands second rebases).

By submitting this pull request, I agree that my contribution is licensed under
the Apache License 2.0.

🤖 Generated with Claude Code

@gloryfromca gloryfromca left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Blocking: split retries can duplicate same-timestamp messages, a public import is removed, and the unit suite fails.

I found three concrete issues, marked inline.

Coverage performed: repository rules in CLAUDE.md and all path-scoped .claude/rules; the complete diff; boundary/service/persistence callers; relevant history; backward compatibility; test changes for weakened or missing coverage; and architecture constraints. The architecture contracts and all lint/policy/OpenAPI checks pass. I also tried to refute each finding with the implementation and tests before retaining it.

Verification:

  • make ci could not start because make is unavailable in the environment, so its non-mutating commands were run directly with uv.
  • Full unit run: 4,101 passed, 4 skipped, 11 failed. Ten failures were only missing declared NLTK data; after installing punkt_tab and stopwords, all 13 tokenizer tests pass. The remaining concurrency property failure is deterministic and branch-caused, as noted inline.
  • Integration: 230 passed, 9 skipped, 7 deselected.
  • Direct identity reproduction: initial same-timestamp [A, B] gives B ..._001_<digest>; retrying [B] gives ..._000_<digest>.

@gloryfromca gloryfromca left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Blocking: the split-retry identity and public import compatibility issues still have to change.

The new commit fixes the stale concurrency property model; that thread is now resolved. No additional finding emerged from the new delta. The two earlier code findings remain unchanged and open: direct checks still show a same-timestamp split resend changing the message ID, and gen_message_id still raises ImportError from the advertised facade.

Coverage this round: repository guidance, the complete new delta and full diff, affected ingest/boundary/persistence callers, relevant history, backward compatibility, test-strength changes, and architecture constraints. Verification: Ruff check and format check passed; 21 targeted unit/property/persistence tests passed, including the formerly failing concurrency property. The previous revision's full integration run remains applicable because this revision changes only that unit test.

@Kendrick-Song

Copy link
Copy Markdown
Collaborator

Approved. One edge case, found by reading id_gen.py (I did not run it), that I'd either fix or note in the docs. It is not a blocker.

The summary says a split or regrouped resend keeps its ids. That holds unless several messages in the request share a timestamp. nth is the message's index among the same-timestamp messages of that request, so it still depends on which messages the request contains:

  • First request: [A(ts1), B(ts1)] gives B = m_<s>_<ts1>_001_<digB>.
  • A resend that carries only B gives B = m_<s>_<ts1>_000_<digB>.

The two ids differ, so already_cut does not find the original, and B can be cut a second time. A whole-batch resend, the common retry, is not affected. It needs same-millisecond messages plus a resend that drops some of them.

Options: (a) accept it and say in docs/api.md that the guarantee covers resends whose same-timestamp messages are unchanged, or (b) number duplicates by their digest instead, e.g. nth counts earlier messages with the same timestamp and the same digest, so it only separates truly identical messages.

Option (b) keeps the case this PR targets (identical messages in one request stay distinct) and removes the dependence on the other messages in the request.

gloryfromca and others added 3 commits November 20, 2026 16:42
A client that retries /memory/add after a timeout resends messages the
first call may have cut into a memcell. Deduplication only looked at the
session buffer, which a cut empties, so the retry cut them again: a
second memcell and episode with the same messages (#274).

The boundary stage now drops fresh messages that a memcell of the
session already lists. The memcell rows are the record: the cut inserts
them in the same transaction that empties the buffer, and a session
delete removes them, so no new table or extra write is needed.

Message ids no longer depend on where a message sits in its request:
m_<session>_<ts>_<n>_<digest>, n counting the request's messages that
share the timestamp (so they keep their order) and digest covering
sender_id, role, content, tool_call_id and tool_calls. A regrouped
resend keeps its ids, and two different messages sent in separate
requests with one timestamp no longer share an id (the second used to
be dropped).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The concurrency jitter property test built message ids by hand in the
old m_<session>_<ts>_<idx> format; it now asks ingest.message_ids.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The id carried the message's position among same-millisecond messages of
its request, so resending [A, B] as [B] gave B a new id and it could be
cut into a second memcell. The id is now session + timestamp + a digest
of who said what; a verbatim repeat within one request adds its repeat
count to the digest. Order among same-millisecond messages is kept by a
stable timestamp sort and by reading buffered rows in write order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gloryfromca
gloryfromca force-pushed the fix/resend-duplicate-memcells branch from 946a27d to 52dfa7e Compare November 20, 2026 08:43

@gloryfromca gloryfromca left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No blockers; this can merge as far as I am concerned.

The same-timestamp split-resend defect is fixed by content-derived IDs plus stable timestamp ordering, and its focused unit/integration regressions pass. The earlier compatibility concern is withdrawn after checking the clarified internal API boundary, documented replacement, and repository-wide consumers. All three threads opened in prior rounds are resolved, and no new finding survived review.

Coverage: repository guidance; the live-base full diff and new revision delta; ingest, boundary, persistence and service callers; relevant history; backward compatibility; tests for weakening or missing coverage; and architecture constraints. Verification: all lint, formatting, import-architecture, repository-policy and OpenAPI checks passed; unit 4,153 passed / 4 skipped; integration 231 passed / 9 skipped / 7 deselected.

@gloryfromca
gloryfromca merged commit 0a50cc6 into main Nov 20, 2026
18 of 19 checks passed
@gloryfromca
gloryfromca deleted the fix/resend-duplicate-memcells branch November 20, 2026 10:04
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.

Client timeout + retry can duplicate memcells for identical session/messages payload

2 participants