Skip to content

[bugfix] agent: avoid orphan Agent session lease on empty queue dispatch - #520

Draft
yefuyou wants to merge 1 commit into
ThinkFlowLab:mainfrom
yefuyou:fix/509-empty-dispatch-lease
Draft

yefuyou wants to merge 1 commit into
ThinkFlowLab:mainfrom
yefuyou:fix/509-empty-dispatch-lease

Conversation

@yefuyou

@yefuyou yefuyou commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #509

Summary

Avoid allocating an Agent session lease when queue dispatch finds no queued message.

Root cause

After a successful turn, run_chain() calls Conversations.dispatch() even when the queue may be empty. Previously, dispatch() started a project Agent session before checking for a queued message. When no queued message existed, it returned without an executing turn ever taking ownership of that newly created session, leaving an orphan agent: session row until lease expiry.

The isolated-main reproduction confirmed an extra session row after a normal completed turn with no queued follow-up. During its lifetime, that row could cause project rename, deletion, or cleanup operations to be rejected as if another window still had the project open.

Change

Move start_agent_session() until after dispatch has identified an actual queued message, while keeping the operation inside the existing transaction and admission-lock guarantees.

This avoids empty-dispatch leases and preserves the existing queue/admission behavior introduced by #381.

Tests and verification

  • Added 5 focused tests in test_assistant_dispatch_lease.py.
  • Three tests reproduce the defect on the clean-main baseline; two protect existing behavior.
  • Fixed tests passed 5/5 across five repeated runs.
  • Affected assistant/agent test modules: 156 passed.
  • Full-suite comparison reported no new failures.
  • Independent review: PASS.
  • No paid provider calls.

Scope

This is a minimal Agent session lease lifecycle fix. It does not change queue ordering, turn execution semantics, or the admission-lock behavior from #381.

Related: #381, #379, #502.

This branch has not been deployed

No deployments
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.

[bugfix] agent: empty queue dispatch can create an orphan Agent session lease

2 participants