Repository navigation
fix(memorize): drop resent messages a memcell already holds #609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,17 @@ | ||
| """Ingest Gateway — normalise external input into canonical form. | ||
|
|
||
| External usage: | ||
| from everos.memory.extract.ingest import process, gen_message_id | ||
| from everos.memory.extract.ingest import process, message_ids | ||
|
|
||
| The current implementation only handles text content; non-text | ||
| ``ContentItem`` entries are dropped with a warning. The parser hook | ||
| (``memory/extract/parser/``) is reserved for future milestones. | ||
| """ | ||
|
|
||
| from .id_gen import gen_message_id as gen_message_id | ||
| from .id_gen import message_ids as message_ids | ||
| from .service import process as process | ||
|
|
||
| __all__ = [ | ||
| "gen_message_id", | ||
| "message_ids", | ||
| "process", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,49 @@ | ||
| """Deterministic, human-readable ``message_id`` generation. | ||
|
|
||
| Format: ``m_<session_id>_<timestamp_ms>_<idx:03d>``. | ||
| Format: ``m_<session_id>_<timestamp_ms>_<digest>``. | ||
|
|
||
| Human-readable so logs / debugging / md entries stay greppable. Deterministic | ||
| so caller retries (same payload) produce the same ids — pipeline merge | ||
| naturally dedupes via the message_id PK in ``unprocessed_buffer``. | ||
| ``digest`` is 8 hex chars of SHA-256 over who said what (``sender_id``, ``role``, | ||
| ``content``, ``tool_call_id``, ``tool_calls``). Nothing in the id depends on where | ||
| the message sits in its request, so a client that resends a batch -- whole, split | ||
| or regrouped -- produces the same ids, and the boundary stage drops what it | ||
| already buffered or cut. Two different messages in one millisecond get two ids. | ||
|
|
||
| A message repeated verbatim within one request (same sender, words and | ||
| millisecond) is told apart by how many identical ones came before it in that | ||
| request; only then does the count enter the digest. Order among messages that | ||
| share a millisecond is not in the id: the boundary stage keeps arrival order. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| _IDX_PAD = 3 # caller batches are capped at 500 messages (DTO limit), 3 digits cover it | ||
| import hashlib | ||
| import json | ||
| from collections import Counter | ||
| from collections.abc import Mapping, Sequence | ||
| from typing import Any | ||
|
|
||
| _IDENTITY_FIELDS = ("sender_id", "role", "content", "tool_call_id", "tool_calls") | ||
|
|
||
|
|
||
| def _digest(message: Mapping[str, Any], repeat: int) -> str: | ||
| basis: list[Any] = [message.get(field) for field in _IDENTITY_FIELDS] | ||
| if repeat: | ||
| basis.append(repeat) | ||
| raw = json.dumps(basis, sort_keys=True, ensure_ascii=False, default=str) | ||
| return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:8] | ||
|
|
||
|
|
||
| def gen_message_id(session_id: str, timestamp_ms: int, idx: int) -> str: | ||
| """Return ``m_<session_id>_<timestamp_ms>_<idx:03d>``.""" | ||
| return f"m_{session_id}_{timestamp_ms}_{idx:0{_IDX_PAD}d}" | ||
| def message_ids(session_id: str, messages: Sequence[Mapping[str, Any]]) -> list[str]: | ||
| """The id of each message of one request, in order.""" | ||
| seen: Counter[str] = Counter() | ||
| ids = [] | ||
| for message in messages: | ||
| ts_ms = int(message["timestamp"]) | ||
| first = f"m_{session_id}_{ts_ms}_{_digest(message, 0)}" | ||
| ids.append( | ||
| first | ||
| if not seen[first] | ||
| else f"m_{session_id}_{ts_ms}_{_digest(message, seen[first])}" | ||
| ) | ||
| seen[first] += 1 | ||
| return ids |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.