Merge pull request 'fix(kugetsu): remove broken session existence check (#38)' (#40) from fix/issue-38-remove-broken-session-check into main

This commit was merged in pull request #40.
This commit is contained in:
2026-03-31 07:27:38 +02:00
2 changed files with 39 additions and 67 deletions

View File

@@ -475,11 +475,6 @@ cmd_status() {
return return
fi fi
if ! check_opencode_session_exists "$pm_agent"; then
echo "pm_agent_expired"
return
fi
echo "ok" echo "ok"
} }
@@ -498,11 +493,6 @@ cmd_delegate() {
exit 1 exit 1
fi fi
if ! check_opencode_session_exists "$pm_session"; then
echo "Error: PM agent session has expired. Run 'kugetsu init' again." >&2
exit 1
fi
opencode run --continue --session "$pm_session" "$message" 2>&1 opencode run --continue --session "$pm_session" "$message" 2>&1
} }
@@ -537,12 +527,6 @@ cmd_doctor() {
issues=$((issues + 1)) issues=$((issues + 1))
else else
echo "[OK] Base session: $base" echo "[OK] Base session: $base"
if check_opencode_session_exists "$base"; then
echo "[OK] Base session active"
else
echo "[ISSUE] Base session expired"
issues=$((issues + 1))
fi
fi fi
local pm_agent=$(get_pm_agent_session_id) local pm_agent=$(get_pm_agent_session_id)
@@ -551,12 +535,6 @@ cmd_doctor() {
issues=$((issues + 1)) issues=$((issues + 1))
else else
echo "[OK] PM agent: $pm_agent" echo "[OK] PM agent: $pm_agent"
if check_opencode_session_exists "$pm_agent"; then
echo "[OK] PM agent session active"
else
echo "[ISSUE] PM agent session expired"
issues=$((issues + 1))
fi
fi fi
local pm_context_file="${KUGETSU_DIR}/pm-agent.md" local pm_context_file="${KUGETSU_DIR}/pm-agent.md"
@@ -583,44 +561,40 @@ cmd_doctor() {
else else
local pm_agent=$(get_pm_agent_session_id) local pm_agent=$(get_pm_agent_session_id)
if [ -n "$pm_agent" ] && [ "$pm_agent" != "null" ] && [ "$pm_agent" != "None" ]; then if [ -n "$pm_agent" ] && [ "$pm_agent" != "null" ] && [ "$pm_agent" != "None" ]; then
if ! check_opencode_session_exists "$pm_agent"; then echo "[FIX] Recreating PM agent session..."
echo "[FIX] Recreating expired PM agent session..." local base=$(get_base_session_id)
local base=$(get_base_session_id) if [ -n "$base" ] && [ "$base" != "null" ]; then
if [ -n "$base" ] && [ "$base" != "null" ]; then rm -f "$SESSIONS_DIR/pm-agent.json"
rm -f "$SESSIONS_DIR/pm-agent.json"
local before_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
local before_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort) local before_set="${before_sessions//$'\n'/|}"
local before_set="${before_sessions//$'\n'/|}"
local pm_context=$(kugetsu_get_pm_context)
local pm_context=$(kugetsu_get_pm_context) if [ -n "$pm_context" ]; then
if [ -n "$pm_context" ]; then opencode run --fork --session "$base" "You are a PM (Project Manager) agent. Your role is to coordinate task delegation and review PRs. $pm_context" 2>&1 || true
opencode run --fork --session "$base" "You are a PM (Project Manager) agent. Your role is to coordinate task delegation and review PRs. $pm_context" 2>&1 || true
else
opencode run --fork --session "$base" "You are a PM (Project Manager) agent. Your role is to coordinate task delegation and review PRs. Wait for instructions." 2>&1 || true
fi
local after_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
local new_pm_session_id=""
while IFS= read -r sess; do
if [[ ! "$before_set" =~ \|${sess}\| ]] && [[ "$sess" != "$base" ]]; then
new_pm_session_id="$sess"
break
fi
done <<< "$after_sessions"
if [ -n "$new_pm_session_id" ]; then
printf '{"type": "pm_agent", "opencode_session_id": "%s", "created_at": "%s", "state": "idle"}\n' \
"$new_pm_session_id" "$(date -Iseconds)" > "$SESSIONS_DIR/pm-agent.json"
set_pm_agent_in_index "$new_pm_session_id"
echo "[FIX] PM agent recreated: $new_pm_session_id"
else
echo "[FIX] Warning: Could not detect new PM session ID"
fi
else else
echo "[FIX] Cannot recreate PM agent: base session missing" opencode run --fork --session "$base" "You are a PM (Project Manager) agent. Your role is to coordinate task delegation and review PRs. Wait for instructions." 2>&1 || true
fi
local after_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort)
local new_pm_session_id=""
while IFS= read -r sess; do
if [[ ! "$before_set" =~ \|${sess}\| ]] && [[ "$sess" != "$base" ]]; then
new_pm_session_id="$sess"
break
fi
done <<< "$after_sessions"
if [ -n "$new_pm_session_id" ]; then
printf '{"type": "pm_agent", "opencode_session_id": "%s", "created_at": "%s", "state": "idle"}\n' \
"$new_pm_session_id" "$(date -Iseconds)" > "$SESSIONS_DIR/pm-agent.json"
set_pm_agent_in_index "$new_pm_session_id"
echo "[FIX] PM agent recreated: $new_pm_session_id"
else
echo "[FIX] Warning: Could not detect new PM session ID"
fi fi
else else
echo "[FIX] PM agent is active, no fix needed" echo "[FIX] Cannot recreate PM agent: base session missing"
fi fi
else else
echo "[FIX] Cannot fix: PM agent not initialized. Run 'kugetsu init' first." echo "[FIX] Cannot fix: PM agent not initialized. Run 'kugetsu init' first."
@@ -867,11 +841,6 @@ cmd_continue() {
local opencode_session_id=$(python3 -c "import json; print(json.load(open('$session_path'))['opencode_session_id'])") local opencode_session_id=$(python3 -c "import json; print(json.load(open('$session_path'))['opencode_session_id'])")
local worktree_path=$(python3 -c "import json; print(json.load(open('$session_path')).get('worktree_path', ''))" 2>/dev/null || echo "") local worktree_path=$(python3 -c "import json; print(json.load(open('$session_path')).get('worktree_path', ''))" 2>/dev/null || echo "")
if ! check_opencode_session_exists "$opencode_session_id"; then
echo "Warning: Session may have expired in opencode" >&2
echo "Attempting to continue anyway..." >&2
fi
echo "Continuing session for '$session_name'..." echo "Continuing session for '$session_name'..."
if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then
echo "Using worktree: $worktree_path" echo "Using worktree: $worktree_path"

View File

@@ -417,14 +417,17 @@ else
fi fi
echo "" echo ""
# Test 25: status when all good (pm-agent in json but session expired) # Test 25: status when all good (pm-agent in json - no longer checks opencode)
echo "--- Test: status (session expired) ---" # Note: check_opencode_session_exists was removed because forked sessions
# don't appear in 'opencode session list'. Status now returns 'ok' if
# session is registered in kugetsu index, regardless of opencode state.
echo "--- Test: status (session registered) ---"
setup_mock_base setup_mock_base
OUTPUT=$($KUGETSU status 2>&1 || true) OUTPUT=$($KUGETSU status 2>&1 || true)
if [ "$OUTPUT" = "pm_agent_expired" ]; then if [ "$OUTPUT" = "ok" ]; then
pass "status returns pm_agent_expired when session not in opencode" pass "status returns ok when session is in kugetsu index"
else else
fail "status session expired: got '$OUTPUT', expected 'pm_agent_expired'" fail "status session registered: got '$OUTPUT', expected 'ok'"
fi fi
echo "" echo ""