fix(kugetsu): prevent excess agent spawning with flock + sequential processing #147

Merged
shoko merged 1 commits from fix/issue-queue-daemon-excess-agents into main 2026-04-05 10:49:25 +02:00
Owner

Summary

Fixes queue daemon spawning excess agents due to race condition when enqueueing multiple tasks.

Changes

  • count_active_dev_sessions(): Now excludes pm-agent.json from count, allowing MAX_CONCURRENT_AGENTS=3 to mean 3 dev agents (PM doesn't count)

  • process_queue(): Completely rewritten to:

    • Call kugetsu start directly instead of opencode run (bypasses concurrency logic)
    • Use dynamic batch size = available_slots (removed QUEUE_DAEMON_BATCH_SIZE)
    • Sequential processing via foreground execution
    • Retry logic (max 3 attempts) on failure
  • cmd_start(): Added flock around critical section to prevent race conditions when multiple processes check/create sessions

  • Notifications: Added 5 new notification types:

    • task_queued: When task is enqueued
    • task_dequeued: When daemon picks up task
    • task_started: When session is created
    • task_completed: When task completes
    • task_error: When task errors/times out
  • Config: Removed QUEUE_DAEMON_BATCH_SIZE (no longer needed)

Issue

Fixes #146

## Summary Fixes queue daemon spawning excess agents due to race condition when enqueueing multiple tasks. ### Changes - **count_active_dev_sessions()**: Now excludes pm-agent.json from count, allowing MAX_CONCURRENT_AGENTS=3 to mean 3 dev agents (PM doesn't count) - **process_queue()**: Completely rewritten to: - Call `kugetsu start` directly instead of `opencode run` (bypasses concurrency logic) - Use dynamic batch size = available_slots (removed QUEUE_DAEMON_BATCH_SIZE) - Sequential processing via foreground execution - Retry logic (max 3 attempts) on failure - **cmd_start()**: Added flock around critical section to prevent race conditions when multiple processes check/create sessions - **Notifications**: Added 5 new notification types: - `task_queued`: When task is enqueued - `task_dequeued`: When daemon picks up task - `task_started`: When session is created - `task_completed`: When task completes - `task_error`: When task errors/times out - **Config**: Removed QUEUE_DAEMON_BATCH_SIZE (no longer needed) ### Issue Fixes #146
shoko added 1 commit 2026-04-05 10:44:17 +02:00
- count_active_dev_sessions() now excludes pm-agent.json from count
- process_queue() now calls kugetsu start directly (not opencode run)
- process_queue() uses dynamic batch size = available_slots
- process_queue() has retry logic (max 3 attempts) on failure
- cmd_start() now uses flock around critical section
- Added notification types: task_queued, task_dequeued, task_started, task_completed, task_error
- Removed QUEUE_DAEMON_BATCH_SIZE config (no longer needed)

Fixes issue #146
shoko force-pushed fix/issue-queue-daemon-excess-agents from bda762b774 to 54aa6419eb 2026-04-05 10:44:54 +02:00 Compare
han approved these changes 2026-04-05 10:48:56 +02:00
han left a comment
First-time contributor

lgtm

lgtm
shoko merged commit da0fa302de into main 2026-04-05 10:49:25 +02:00
Sign in to join this conversation.