fix: implement session-counting for MAX_CONCURRENT_AGENTS limit (fixes #63) #65

Merged
shoko merged 6 commits from fix/issue-63-session-counting into main 2026-04-01 07:40:00 +02:00
Owner

Summary

Replaced broken slot-based mechanism with session-counting approach per comment #687.

Changes

  • Added count_active_dev_sessions() - counts actual session files in ~/.kugetsu/sessions/, excluding base.json and pm-agent.json
  • Modified cmd_start() - checks session count before creating new session, rejects if at limit
  • Removed wait $child_pid - since opencode run --fork returns immediately, not when child completes
  • Modified cmd_continue() - no longer counts toward limit (existing sessions can always continue)

How it works

# Count logic
active_count=$(ls ~/.kugetsu/sessions/*.json 2>/dev/null | grep -v -E "(base|pm-agent)" | wc -l)
if [ "$active_count" -ge "$MAX_CONCURRENT_AGENTS" ]; then
    echo "Error: Max concurrent agents reached"
    exit 1
fi

Rules

  1. NEW session creation - check count, reject if at limit
  2. Existing session continue - ALWAYS allowed (--continue does not count toward limit)
  3. PM cleanup - destroy session when task done (per issue #687 recommendation)

Testing

With MAX=3 and 6 existing sessions:

Active sessions: 6
MAX: 3
Would REJECT new session (at limit)

Fixes #63

## Summary Replaced broken slot-based mechanism with session-counting approach per comment #687. ### Changes - **Added `count_active_dev_sessions()`** - counts actual session files in `~/.kugetsu/sessions/`, excluding `base.json` and `pm-agent.json` - **Modified `cmd_start()`** - checks session count before creating new session, rejects if at limit - **Removed `wait $child_pid`** - since `opencode run --fork` returns immediately, not when child completes - **Modified `cmd_continue()`** - no longer counts toward limit (existing sessions can always continue) ### How it works ```bash # Count logic active_count=$(ls ~/.kugetsu/sessions/*.json 2>/dev/null | grep -v -E "(base|pm-agent)" | wc -l) if [ "$active_count" -ge "$MAX_CONCURRENT_AGENTS" ]; then echo "Error: Max concurrent agents reached" exit 1 fi ``` ### Rules 1. **NEW session creation** - check count, reject if at limit 2. **Existing session continue** - ALWAYS allowed (`--continue` does not count toward limit) 3. **PM cleanup** - destroy session when task done (per issue #687 recommendation) ### Testing With MAX=3 and 6 existing sessions: ``` Active sessions: 6 MAX: 3 Would REJECT new session (at limit) ``` ## Fixes #63
shoko added 3 commits 2026-04-01 06:14:33 +02:00
- Fixed cmd_start() and cmd_continue() to wait for forked child
- Capture child PID with $! and wait before release_agent_slot
- This ensures slot is only released after child process completes
Replaced broken slot-based mechanism with session-counting:

- Added count_active_dev_sessions() function that counts actual
  session files in ~/.kugetsu/sessions/, excluding base.json and pm-agent.json
- Modified cmd_start() to check session count before creating new session:
  - If count >= MAX_CONCURRENT_AGENTS, reject with error
  - Otherwise allow new session creation
- Removed wait  since --fork returns immediately
- cmd_continue() no longer counts toward limit (existing sessions
  can always continue via --continue)

This properly enforces MAX_CONCURRENT_AGENTS while preserving --fork
functionality. The slot mechanism didn't work because opencode run
--fork returns immediately after forking, not after child completes.
shoko added 1 commit 2026-04-01 06:57:55 +02:00
shoko added 1 commit 2026-04-01 07:03:46 +02:00
shoko added 1 commit 2026-04-01 07:18:31 +02:00
han approved these changes 2026-04-01 07:38:58 +02:00
han left a comment
First-time contributor

lgtm

lgtm
shoko merged commit 21e4054634 into main 2026-04-01 07:40:00 +02:00
Sign in to join this conversation.