Compare commits

..

1 Commits

Author SHA1 Message Date
shokollm
de1b02c585 fix: improve worktree/session handling in cmd_start, cmd_continue, and cmd_init
cmd_continue:
- If worktree is missing but session exists, remove stale session and call cmd_start automatically
- This allows automatic recovery without user intervention

cmd_start:
- Check BOTH worktree AND session existence
- If only worktree exists (not session): remove worktree, recreate both
- If only session exists (not worktree): remove session, recreate both
- If both exist: tell user to use continue

cmd_init --force:
- Now actually destroys existing base and pm-agent sessions before reinitializing

Daemon:
- Fixed wrong path in check_task_completion ($HOME/.kugetsu-worktrees -> $WORKTREES_DIR)
2026-04-07 01:34:09 +00:00
3 changed files with 13 additions and 62 deletions

View File

@@ -32,7 +32,7 @@ if [ -f "$KUGETSU_DIR/config" ]; then
fi fi
mask_sensitive_vars() { mask_sensitive_vars() {
local line="${1:-}" local line="$1"
for var in GITEA_TOKEN GITHUB_TOKEN GITLAB_TOKEN API_KEY PASSWORD TOKEN SECRET; do for var in GITEA_TOKEN GITHUB_TOKEN GITLAB_TOKEN API_KEY PASSWORD TOKEN SECRET; do
if [[ "$line" =~ $var ]]; then if [[ "$line" =~ $var ]]; then
line=$(echo "$line" | sed -E "s/=.*/=***MASKED***/") line=$(echo "$line" | sed -E "s/=.*/=***MASKED***/")
@@ -41,11 +41,6 @@ mask_sensitive_vars() {
echo "$line" echo "$line"
} }
strip_ansi_codes() {
local line="${1:-}"
echo "$line" | sed 's/\x1b\[[0-9;]*m//g' | sed 's/\x1b\[[0-9;]*[a-zA-Z]//g'
}
load_agent_env() { load_agent_env() {
local agent_type="${1:-base}" local agent_type="${1:-base}"
local env_file="$ENV_DIR/${agent_type}.env" local env_file="$ENV_DIR/${agent_type}.env"

View File

@@ -24,9 +24,7 @@ cmd_logs() {
echo "" echo ""
echo "--- $log ---" echo "--- $log ---"
tail -20 "$LOGS_DIR/$log" | while read line; do tail -20 "$LOGS_DIR/$log" | while read line; do
line=$(strip_ansi_codes "$line") echo " $(mask_sensitive_vars "$line")"
line=$(mask_sensitive_vars "$line")
echo " $line"
done done
fi fi
done done

View File

@@ -83,10 +83,9 @@ EOF
if [ -n "$existing_base" ] && [ "$existing_base" != "null" ]; then if [ -n "$existing_base" ] && [ "$existing_base" != "null" ]; then
if [ "$force" = true ]; then if [ "$force" = true ]; then
echo "Warning: Reinitializing sessions (force mode)" >&2 echo "Warning: Reinitializing sessions (force mode)" >&2
echo "Destroying all sessions, worktrees, and logs..." >&2 echo "Destroying existing sessions..." >&2
cmd_destroy --base -y 2>/dev/null || true cmd_destroy --base -y 2>/dev/null || true
cmd_destroy --pm-agent -y 2>/dev/null || true cmd_destroy --pm-agent -y 2>/dev/null || true
rm -f "$LOGS_DIR"/*.log 2>/dev/null || true
else else
echo "Error: Base session already exists: $existing_base" >&2 echo "Error: Base session already exists: $existing_base" >&2
echo "Use --force to reinitialize" >&2 echo "Use --force to reinitialize" >&2
@@ -225,10 +224,7 @@ cmd_delegate() {
exit 1 exit 1
fi fi
local msg_file="$LOGS_DIR/msg-$new_session.txt" nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$new_session'" >> "$log_file" 2>&1 &
printf '%s' "$message" > "$msg_file"
nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '@$msg_file' --session '$new_session'" >> "$log_file" 2>&1 &
rm -f "$msg_file"
echo "Delegated to new session (logged to $(basename "$log_file"))" echo "Delegated to new session (logged to $(basename "$log_file"))"
} }
@@ -241,47 +237,24 @@ create_session() {
fi fi
local before_json=$(opencode session list --format=json 2>/dev/null) local before_json=$(opencode session list --format=json 2>/dev/null)
local before_set=$(echo "$before_json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); print('|'.join(s['id'] for s in sessions))" 2>/dev/null || echo "|") local before_ids=$(echo "$before_json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); print(' '.join(s['id'] for s in sessions))" 2>/dev/null || echo "")
opencode run --fork --session "$base_session" "new session" >/dev/null 2>&1 opencode run --fork --session "$base_session" "new session" 2>/dev/null
sleep 1
local after_json=$(opencode session list --format=json 2>/dev/null) local after_json=$(opencode session list --format=json 2>/dev/null)
local after_sessions=$(echo "$after_json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); [print(s['id']) for s in sessions]" 2>/dev/null || true) local after_ids=$(echo "$after_json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); print(' '.join(s['id'] for s in sessions))" 2>/dev/null || echo "")
local new_session_id="" local new_session_id=""
while IFS= read -r sess; do for sess in $after_ids; do
if [[ -n "$sess" ]] && [[ ! "$before_set" =~ \|${sess}\| ]]; then if [[ ! " $before_ids " =~ " $sess " ]] && [[ "$sess" != "$base_session" ]]; then
new_session_id="$sess" new_session_id="$sess"
break break
fi fi
done <<< "$after_sessions" done
echo "$new_session_id" echo "$new_session_id"
} }
load_session_context() {
local opencode_session_id="$1"
local sanitized_id=$(echo "$opencode_session_id" | sed 's/[^a-zA-Z0-9_-]/_/g')
local log_file="$LOGS_DIR/dev-$sanitized_id.log"
if [ ! -f "$log_file" ]; then
echo ""
return
fi
local context_summary="Previous session context (work was in progress, session may have crashed or been interrupted):\n\n"
context_summary+=$(tail -100 "$log_file" 2>/dev/null | while IFS= read -r line; do
line=$(strip_ansi_codes "$line")
echo " $line"
done | head -50)
context_summary+="\n\nPlease review this context and continue the work appropriately."
echo "$context_summary"
}
build_dev_agent_message() { build_dev_agent_message() {
local issue_ref="$1" local issue_ref="$1"
local user_message="${2:-}" local user_message="${2:-}"
@@ -399,11 +372,7 @@ cmd_start() {
load_agent_env "dev" load_agent_env "dev"
cd "$worktree_path" cd "$worktree_path"
local sanitized_id=$(echo "$new_session_id" | sed 's/[^a-zA-Z0-9_-]/_/g') nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$dev_message' --session '$new_session_id'" >> "$LOGS_DIR/dev-$new_session_id.log" 2>&1 &
local msg_file="$LOGS_DIR/msg-$sanitized_id.txt"
printf '%s' "$dev_message" > "$msg_file"
nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '@$msg_file' --session '$new_session_id'" >> "$LOGS_DIR/dev-$sanitized_id.log" 2>&1 &
rm -f "$msg_file"
echo "Session started for '$issue_ref': $new_session_id" echo "Session started for '$issue_ref': $new_session_id"
echo "Worktree: $worktree_path" echo "Worktree: $worktree_path"
@@ -454,16 +423,9 @@ cmd_continue() {
if [ -z "$worktree_path" ] || [ ! -d "$worktree_path" ]; then if [ -z "$worktree_path" ] || [ ! -d "$worktree_path" ]; then
echo "Warning: Worktree is missing for '$session_name'. Recovering..." >&2 echo "Warning: Worktree is missing for '$session_name'. Recovering..." >&2
local previous_context=$(load_session_context "$opencode_session_id")
rm -f "$session_path" rm -f "$session_path"
remove_issue_from_index "$session_name" remove_issue_from_index "$session_name"
if [ -n "$previous_context" ]; then echo "Calling cmd_start to create new session and worktree..." >&2
message="$previous_context"
if [ -n "$message" ]; then
message="$message"$'\n\n'"Additional user message: $message"
fi
fi
echo "Calling cmd_start to create new session and worktree with context..." >&2
cmd_start "$session_name" "$message" cmd_start "$session_name" "$message"
return $? return $?
fi fi
@@ -473,11 +435,7 @@ cmd_continue() {
fi fi
cd "$worktree_path" cd "$worktree_path"
local sanitized_id=$(echo "$opencode_session_id" | sed 's/[^a-zA-Z0-9_-]/_/g') nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 &
local msg_file="$LOGS_DIR/msg-$sanitized_id.txt"
printf '%s' "$message" > "$msg_file"
nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '@$msg_file' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$sanitized_id.log" 2>&1 &
rm -f "$msg_file"
} }
cmd_list() { cmd_list() {