Skip to content

fix(chat): self-task inject_result silently no-ops — dict access on Pydantic session (#1457) - #1462

Merged
vybe merged 2 commits into
devfrom
feature/1457-selftask-inject-pydantic
Jul 6, 2026
Merged

vybe merged 2 commits into
devfrom
feature/1457-selftask-inject-pydantic

Conversation

@dolho

@dolho dolho commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

_finalize_self_task (src/backend/routers/chat.py) validated chat-session ownership with dict access on the value from db.get_chat_session():

if session and session.get("user_id") == user_id:   # ChatSession has no .get()

After the #1093 SQLAlchemy-Core rewrite, db.get_chat_session() returns Optional[ChatSession] (a Pydantic BaseModel), not a dict. session.get(...) raised AttributeError, caught by the surrounding except Exception and logged as a warning. Net effect: with inject_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):

if session and session.user_id == user_id:

Regression test

tests/unit/test_1457_selftask_inject_ownership.py (pure unit, mirrors test_1332_*):

  • owned session + SUCCESS + inject_result=True → assistant message with source="self_task"
  • foreign-owned session → no injection
  • guard: ChatSession exposes .user_id and 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 except turns 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_session consumers — 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.py session.get(...) is a stats dict, not a ChatSession — out of scope)

_finalize_self_task was the sole offender.

Related to #1451

…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]>
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

…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 vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@vybe
vybe merged commit ac90263 into dev Jul 6, 2026
18 checks passed
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.

2 participants