Repository navigation
fix(chat): self-task inject_result silently no-ops — dict access on Pydantic session (#1457) - #1462
Merged
Merged
Conversation
…on (#1457) `_finalize_self_task` validated chat-session ownership with dict access (`session.get("user_id")`) on the value from `db.get_chat_session()`. After the #1093 SQLAlchemy-Core rewrite that accessor returns `Optional[ChatSession]` — a Pydantic model with no `.get()` — so the check raised `AttributeError`, was swallowed by the surrounding `except Exception`, and the self-task result was silently never injected into the chat session. Switch to attribute access (`session.user_id == user_id`), matching the sibling call-sites. Add a fast unit regression guard: a SUCCESS self-task with `inject_result=true` and an owned `chat_session_id` writes an `assistant` message with `source="self_task"`; a foreign session does not inject. Related to #1457 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
5 tasks
|
Resolve by running |
…ute-access fix The #1457 fix switched `_finalize_self_task` from `session.get("user_id")` to `session.user_id` (db.get_chat_session returns a ChatSession pydantic, not a dict). But this characterization test still mocked get_chat_session to return a plain dict `{"user_id": ...}`, which has no `.user_id` — so injection silently no-op'd and `test_self_task_injects_result_into_session` newly failed under HEAD (caught by the regression-diff gate; green on the buggy base only because a dict has `.get`). Return a SimpleNamespace(user_id=...) instead — attribute access, no `.get`, faithfully mirroring the ChatSession contract the fix relies on. Both the inject and the not-owned cases updated. Related to #1457
vybe
approved these changes
Jul 6, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
Validated via /validate-pr: fix verified against db layer (get_chat_session returns Pydantic ChatSession; add_chat_message accepts source kwarg on the newly-reachable path), sibling call-site audit confirmed (line 1274 was the sole .get() offender), both touched test files run locally on the PR head (10 passed), and red-verification confirmed the regression test fails on the buggy code. Security greps clean.
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.
Problem
_finalize_self_task(src/backend/routers/chat.py) validated chat-session ownership with dict access on the value fromdb.get_chat_session():After the #1093 SQLAlchemy-Core rewrite,
db.get_chat_session()returnsOptional[ChatSession](a PydanticBaseModel), not a dict.session.get(...)raisedAttributeError, caught by the surroundingexcept Exceptionand logged as a warning. Net effect: withinject_result=true, the self-task result was never written into the chat session and the ownership check never ran.Fix
One line — attribute access, matching the sibling call-sites (
chat.py:1016,:2321,:2352):Regression test
tests/unit/test_1457_selftask_inject_ownership.py(pure unit, mirrorstest_1332_*):inject_result=True→assistantmessage withsource="self_task"ChatSessionexposes.user_idand has no.get(old dict access was structurally wrong)Verified red on the buggy code, green on the fix.
3 passed.Provenance
Found during #1444 review; same class as #1444 — a swallowing
exceptturns the regression into a silent no-op.Fixes #1457
Also fixes #1451 (duplicate of #1457)
#1451 reports the identical defect. Its extra acceptance criterion — audit sibling
get_chat_sessionconsumers — done, all clean (attribute access, no dict.get()):routers/public.py:1110→session.agent_name/session.user_id✅routers/voice.py:54→chat_session.id✅routers/chat.py:1011(if not session),:2285,:2317→ attribute access ✅services/agent_client.pysession.get(...)is a stats dict, not aChatSession— out of scope)_finalize_self_taskwas the sole offender.Related to #1451