Add top-level session_id field to messages#126
Open
nanxingw wants to merge 1 commit into
Open
Conversation
Introduce a session_id column on messages so callers can group messages
into logical conversation sessions independent of agent_id. The column
is optional (nullable), validated at the application layer (regex
[A-Za-z0-9_-]+, max length 64) and backed by a Postgres CHECK constraint
plus a covering index (agent_id, session_id, created_at).
Touch points:
- ORM/schema: mirix/orm/message.py adds the column, ix_messages_agent_session_created_at
index, and ck_messages_session_id_format check (Postgres-only); shared
pattern/length constants live in mirix/schemas/message.py.
- Validation: MessageCreate / MessageUpdate / Message all enforce the same
rule via a shared _validate_session_id helper.
- Queue: message.proto gets field 8 (session_id, optional string);
message_pb2.py / message_pb2.pyi regenerated with grpcio-tools (protobuf
floor bumped to 5.27.2 in pyproject.toml + requirements.txt to match the
gencode header). New `make proto` target keeps regen reproducible.
- Plumbing: queue_util / worker propagate session_id end-to-end;
message_helpers.prepare_input_message_create forwards it; agent.py
inherits the triggering input's session_id onto every synthesized
assistant/tool/heartbeat/summary message in the same step (via a
step-level _current_step_session_id stash that step_user_message
save/restores so summarize_messages_inplace gets the right value).
- API: SendMessageRequest and AddMemoryRequest gain session_id with the
same validator; AddMemoryRequest enforces agreement between top-level
session_id and filter_tags["session_id"] when both are set.
- Filtering: MessageManager.list_messages_for_{agent,user} accept
session_id and translate to a column-equality filter.
- Migrations: scripts/migrate_add_message_session_id.sql adds the column
+ index + check; _phase2.sql is the follow-up backfill/tighten step.
- Tests: tests/test_session_id.py (DB-free unit, ~470 lines covering
schema, validator, helper, queue serialization, ORM column shape) and
tests/test_session_id_integration.py (Postgres round-trip via
MessageManager confirming session-scoped filtering).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Introduce an optional
session_idcolumn on themessagestable so callers can group messages into logical conversation sessions independent ofagent_id. Validated at three layers (Pydantic, REST request models, Postgres CHECK), propagated end-to-end through the agent's synthesized messages, and exposed throughMessageManager.list_messages_for_*(session_id=...)for filtering.Behavior
NULL. Old rows stayNULLafter migration.[A-Za-z0-9_-]{1,64}. Rejected at Pydantic construction, REST request validation, and Postgres CHECK constraint.MessageManager.list_messages_for_agent(session_id="...")andlist_messages_for_user(session_id="...")return only that session's messages.add_memorymirrors the batch-levelsession_idintofilter_tags["session_id"]so memories created from that batch inherit the tag, making them retrievable via the existing/memory/retrieve/conversationendpoint without API surface changes.session_idvia a_current_step_session_idstash onself, save/restored withtry/finallyso it cannot leak across calls.Test plan
tests/test_session_id.py(DB-free): schema, validator, helper, REST request schemas, queue proto serialization, ORM column shape and indexing, agent-source lint for propagation sites, dialect-gated CHECK, constants-in-sync.tests/test_session_id_integration.py— auto-skips withoutMIRIX_PG_URI(mirrorstests/test_agent_trigger_state_integration.pypattern).server.send_messages → _stepwith LLM-generated tool call. All three synthesized messages (user + assistant + tool result) inheritedsession_idcorrectly; negative control verified no cross-session leak.step()stash leak); fixed withtry/finally+ regression testtest_step_seeds_and_restores_stash. Second finding was a pre-existing unrelated bug — see [bug] Agent.step_user_message → inner_step missing required first_input_messge arg #127.Reviewer hints
git diff -wonmirix/agent/agent.py— the bulk of that file's diff is body re-indent to wrapstep()intry/finally. The actual session_id additions are ~70 lines; the rest is the indent shift.mirix/orm/message.pyusesddl_if(dialect="postgresql")because SQLite has no~regex operator. SQLite path verified to suppress the constraint (test_check_is_suppressed_for_sqlite).mirix/schemas/message.pyare the single source of truth — the migration SQL and ORM CHECK both reference them, withtest_migration_sql_uses_shared_pattern_and_lengthkeeping them in sync.Out of scope
Agent.step_user_message → inner_stepmissingfirst_input_messgearg bug — see [bug] Agent.step_user_message → inner_step missing required first_input_messge arg #127. Surfaced during E2E validation but not introduced by this PR; production REST path is unaffected because it goes throughserver.send_messages → _step.🤖 Generated with Claude Code