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

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.
This commit is contained in:
shokollm
2026-04-01 04:14:15 +00:00
parent bd4e8587b4
commit 7342a9a394

View File

@@ -48,6 +48,21 @@ release_agent_slot() {
) 200>"$AGENT_LOCK_FILE"
}
count_active_dev_sessions() {
local count=0
if [ -d "$SESSIONS_DIR" ]; then
for session_file in "$SESSIONS_DIR"/*.json; do
if [ -f "$session_file" ]; then
local filename=$(basename "$session_file")
if [ "$filename" != "base.json" ] && [ "$filename" != "pm-agent.json" ]; then
count=$((count + 1))
fi
fi
done
fi
echo "$count"
}
run_with_limit() {
local log_file="$1"
shift
@@ -852,19 +867,21 @@ cmd_start() {
local before_set="${before_sessions//$'\n'/|}"
echo "Forking session for '$issue_ref'..."
if ! acquire_agent_slot; then
echo "Error: Max concurrent agents ($MAX_CONCURRENT_AGENTS) reached. Try again later." >&2
# Session-counting: count actual dev sessions, reject if at limit
local active_count=$(count_active_dev_sessions)
if [ "$active_count" -ge "$MAX_CONCURRENT_AGENTS" ]; then
echo "Error: Max concurrent agents ($MAX_CONCURRENT_AGENTS) reached" >&2
echo "Active sessions: $active_count" >&2
remove_worktree_for_issue "$issue_ref"
exit 1
fi
if [ "$DEBUG_MODE" = true ]; then
opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1 | tee "$SESSIONS_DIR/$session_file.debug.log" &
else
opencode run "$message" --fork --session "$base_session_id" --dir "$worktree_path" 2>&1 &
fi
local child_pid=$!
wait $child_pid
release_agent_slot
local after_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
local new_session_id=""
@@ -933,10 +950,7 @@ cmd_continue() {
local worktree_path=$(python3 -c "import json; print(json.load(open('$session_path')).get('worktree_path', ''))" 2>/dev/null || echo "")
echo "Continuing session for '$session_name'..."
if ! acquire_agent_slot; then
echo "Error: Max concurrent agents ($MAX_CONCURRENT_AGENTS) reached. Try again later." >&2
exit 1
fi
# Note: --continue always allowed (existing sessions don't count toward limit)
if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then
echo "Using worktree: $worktree_path"
if [ "$DEBUG_MODE" = true ]; then
@@ -951,9 +965,6 @@ cmd_continue() {
opencode run "$message" --continue --session "$opencode_session_id" 2>&1 &
fi
fi
local child_pid=$!
wait $child_pid
release_agent_slot
}
cmd_list() {