Bug: MAX_CONCURRENT_AGENTS limit not enforced #63
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Bug
The MAX_CONCURRENT_AGENTS limit is not enforced because slots are released immediately after forking, not after the child process completes.
Root Cause
In cmd_start():
Impact
MAX_CONCURRENT_AGENTS limit is non-functional. Any number of agents can run.
Issue with This Fix
The fix waits for
opencode run --forkto complete, butopencode run --forkreturns immediately after FORKING the child process - it does NOT wait for the agent session to finish.Tested: 6
kugetsu startcommands ran sequentially, all succeeded because each finished in ~1 sec and released its slot.The Real Issue
opencode run --forkforks a child process for the agent, then immediately returns.wait $child_pidonly waits for the parent opencode to finish creating the session, not for the actual agent to finish.Options
--forkso opencode waits for session completionopencode session listbefore starting new oneOption 2 is best - count actual active sessions instead of using slots.
Update: pgrep approach won't work
Tested
pgrep opencode- it returns 0 even with active sessions. Reason:opencode run --forkcreates session on opencode SERVERopencode session list has no filtering
Options found:
--max-count N- limit to N most recent--format json|table- change output formatNo filter for state/activity. All sessions shown, no way to distinguish active vs idle vs stale.
Conclusion
Option C (PM cleanup) is the only viable approach.
PM should destroy sessions at natural breakpoints:
This avoids false positives and doesn't add latency since user is already waiting for response.
Recommendation: Close this PR and implement Option C instead - add session cleanup to PM workflow.
Session-Counting Approach (Recommended Fix)
The slot mechanism doesn't work because
opencode run --forkreturns immediately after forking, not after child completes. We need a different approach.New Proposal: Session Counting
Count actual sessions from
~/.kugetsu/sessions/:Session Files
Count Logic
Rules
Flow Example (limit=3)
kugetsu init→ base + PM sessions createdBenefits
This properly enforces MAX_CONCURRENT_AGENTS while preserving --fork functionality.
Should I implement this?
Fix Implemented: Session-Counting Approach
PR created with session-counting implementation:
PR: #65
Changes Made
Added
count_active_dev_sessions()- counts actual session files in~/.kugetsu/sessions/, excludingbase.jsonandpm-agent.jsonModified
cmd_start()- checks session count before creating new session, rejects if at limitRemoved
wait $child_pid- sinceopencode run --forkreturns immediately, not when child completesModified
cmd_continue()- no longer counts toward limit (existing sessions can always continue via--continue)Rules
--continuedoes not count toward limit)Testing
With MAX=3 and 6 existing sessions:
This properly enforces MAX_CONCURRENT_AGENTS while preserving --fork functionality.