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
Showing only changes of commit 7342a9a394 - Show all commits

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() {