Skip to content

fix(deriver): clear dangling session hint on pending dreams at session delete - #992

Draft
Rajat-Ahuja1997 wants to merge 1 commit into
mainfrom
rajat/delete-session-dream-queue-items
Draft

fix(deriver): clear dangling session hint on pending dreams at session delete#992
Rajat-Ahuja1997 wants to merge 1 commit into
mainfrom
rajat/delete-session-dream-queue-items

Conversation

@Rajat-Ahuja1997

@Rajat-Ahuja1997 Rajat-Ahuja1997 commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

What

delete_session now strips payload.session_name from pending dream queue items that reference the session being deleted.

Why

Surfaced while reviewing #801 / #799. delete_session already deletes the session's QueueItem and ActiveQueueSession rows, but dreams slip through both:

  • Dream records carry session_id=None (src/deriver/enqueue.py:441), so the session_id delete misses them.
  • The dream work-unit key is dream:{dream_type}:{workspace}:{observer}:{observed} — no session component — so the split_part(key, ':', 3) == session_name match misses them too.

A dream isn't scoped to a session in the first place: it consolidates the whole (observer, observed) collection. payload.session_name is only a config/context hint, set by DreamScheduler._execute_dream to the session of the most recent explicit document for that pair (src/dreamer/dream_scheduler.py:195-205).

So the stale row isn't stale work — it's a stale tag. Left alone, run_dream resolves that session for configuration (src/dreamer/orchestrator.py:112), raises ResourceNotFoundException, and the dream errors out for no good reason.

How

Clear the hint rather than delete the dream, leaving it workspace-scoped — a state the orchestrator already handles (session_name is None → workspace config), and one the manual dream route produces whenever session_id is omitted (src/routers/workspaces.py:232).

Scoped to processed = false; historical rows are left to the reconciler.

Testing

tests/crud/test_session.py::TestSessionCRUD::test_delete_session_clears_dangling_dream_session_hint — three dreams (one on the deleted session, one on another session, one workspace-wide); asserts all three survive and only the first loses its hint. Fails without the fix.

uv run pytest tests/crud/ tests/routes/test_sessions.py → 116 passed.

Summary by CodeRabbit

  • Bug Fixes

    • Deleting a session now removes its reference from related unprocessed dream queue items without deleting those items.
    • Unrelated session-linked and global dream items remain unchanged.
  • Tests

    • Added regression coverage to verify queue item preservation and correct cleanup of deleted session references.

…n delete

A dream consolidates an entire (observer, observed) collection, not a
single session — its payload.session_name is a config/context hint set to
the session of the most recent explicit document (see
DreamScheduler._execute_dream). Dream queue items therefore carry
session_id=None and a work_unit_key with no session component, so neither
delete in delete_session reaches them.

That left pending dreams pointing at a deleted session, where run_dream's
config resolution raises ResourceNotFoundException and the task errors out
for no good reason. Strip the stale hint instead of dropping the dream, so
it runs workspace-scoped — a state the orchestrator already supports (the
manual dream route enqueues it whenever session_id is omitted).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Session deletion now removes payload.session_name from matching unprocessed workspace-scoped dream queue items. The queue items remain. A regression test verifies that unrelated dream records are unchanged.

Changes

Session deletion cleanup

Layer / File(s) Summary
Queue payload cleanup
src/crud/session.py
Session deletion updates matching unprocessed dream queue item payloads and removes session_name without deleting the items.
Cleanup regression test
tests/crud/test_session.py
The asynchronous test verifies selective cleanup across two sessions and three dream queue items.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I’m a rabbit guarding the queue,
Session hints vanish, but dreams stay true.
One trail clears, the others remain,
No dangling names hop back again.
Thump, thump—the test confirms the change!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: clearing dangling session hints from pending dreams when a session is deleted.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch rajat/delete-session-dream-queue-items

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
tests/crud/test_session.py (1)

200-209: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover processed records and JSON key removal.

Lines 202-209 treat a missing session_name key and session_name: None as the same result. Add a processed dream with test_session.name and assert that it retains its hint. Assert that "session_name" is absent from the unprocessed target payload. This validates the required preservation of historical rows and the JSONB key-removal behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/crud/test_session.py` around lines 200 - 209, Extend the dream
fixture/assertion coverage around the existing payload-comprehension assertion
to include a processed dream referencing test_session.name and verify that its
session hint remains unchanged. Separately assert that the unprocessed
deleted-session dream’s payload does not contain the “session_name” key,
distinguishing JSONB key removal from a present None value.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@tests/crud/test_session.py`:
- Around line 200-209: Extend the dream fixture/assertion coverage around the
existing payload-comprehension assertion to include a processed dream
referencing test_session.name and verify that its session hint remains
unchanged. Separately assert that the unprocessed deleted-session dream’s
payload does not contain the “session_name” key, distinguishing JSONB key
removal from a present None value.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 95e99290-3f98-49ca-8c3e-44813314a6e9

📥 Commits

Reviewing files that changed from the base of the PR and between d191c10 and b13e4f7.

📒 Files selected for processing (2)
  • src/crud/session.py
  • tests/crud/test_session.py

@Rajat-Ahuja1997
Rajat-Ahuja1997 marked this pull request as draft August 7, 2026 18:50
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.

1 participant