Compare commits

..

10 Commits

Author SHA1 Message Date
shokollm
c54906be4b feat: load previous dev log as context when cmd_continue recovers 2026-04-07 02:58:27 +00:00
aafdebb6c6 fix: suppress opencode fork stdout and strip ANSI codes from logs (#197) 2026-04-07 04:58:15 +02:00
e666f4dffb Merge pull request 'fix: use temp file for message to avoid shell parsing issues' (#196) from fix/issue-message-encoding into main 2026-04-07 04:27:50 +02:00
shokollm
a130a79bd7 fix: use temp file for message to avoid shell parsing issues
The message passed to opencode run contains newlines and special
characters (parentheses, etc.) which break shell parsing when passed
directly in double quotes.

Fix by writing message to temp file and using '@msg_file' syntax
to pass to opencode run. This handles any characters in the message.
2026-04-07 01:55:34 +00:00
478f7ceeba Merge pull request 'fix: improve worktree/session handling in cmd_start and cmd_continue' (#195) from fix/issue-daemon-worktree-session-handling into main 2026-04-07 03:41:27 +02:00
shokollm
9d0bcef465 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 destroys ALL sessions, worktrees, and logs for a clean slate
- Destroys base, pm-agent, all forked sessions, all worktrees, all logs

Daemon:
- Fixed wrong path in check_task_completion ($HOME/.kugetsu-worktrees -> $WORKTREES_DIR)
2026-04-07 01:40:20 +00:00
bb2add2e1a Merge pull request 'fix: properly quote base and pm_agent in write_index calls' (#194) from fix/issue-write-index-quoting into main 2026-04-07 02:50:32 +02:00
shokollm
6a8fa563dd fix: properly quote base and pm_agent in write_index calls
When base or pm_agent are not null, they need to be quoted with escaped quotes ("") in write_index calls.
This fixes 'write_index would create malformed JSON' error during init.
2026-04-07 00:48:06 +00:00
8729321922 Merge pull request 'fix: use ${GITEA_TOKEN:-} to handle unset token' (#193) from fix/issue-cmd-destroy-unbound-var into main 2026-04-07 02:41:17 +02:00
cf8b003d2f Merge pull request 'fix: cmd_destroy unbound variable $2' (#192) from fix/issue-cmd-destroy-unbound-var into main 2026-04-06 11:30:04 +02:00
5 changed files with 139 additions and 32 deletions

View File

@@ -32,7 +32,7 @@ if [ -f "$KUGETSU_DIR/config" ]; then
fi
mask_sensitive_vars() {
local line="$1"
local line="${1:-}"
for var in GITEA_TOKEN GITHUB_TOKEN GITLAB_TOKEN API_KEY PASSWORD TOKEN SECRET; do
if [[ "$line" =~ $var ]]; then
line=$(echo "$line" | sed -E "s/=.*/=***MASKED***/")
@@ -41,6 +41,11 @@ mask_sensitive_vars() {
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() {
local agent_type="${1:-base}"
local env_file="$ENV_DIR/${agent_type}.env"

View File

@@ -50,7 +50,11 @@ set_base_in_index() {
if [ "$session_id" = "null" ]; then
write_index "null" "$pm_agent" "$issues"
else
write_index "\"$session_id\"" "$pm_agent" "$issues"
if [ "$pm_agent" = "null" ]; then
write_index "\"$session_id\"" "null" "$issues"
else
write_index "\"$session_id\"" "\"$pm_agent\"" "$issues"
fi
fi
}
@@ -63,7 +67,11 @@ set_pm_agent_in_index() {
if [ "$session_id" = "null" ]; then
write_index "$base" "null" "$issues"
else
write_index "$base" "\"$session_id\"" "$issues"
if [ "$base" = "null" ]; then
write_index "null" "\"$session_id\"" "$issues"
else
write_index "\"$base\"" "\"$session_id\"" "$issues"
fi
fi
}
@@ -79,7 +87,19 @@ add_issue_to_index() {
issues=$(python3 -c "import sys, json; d=json.load(sys.stdin); d['$issue_ref']='$session_file'; print(json.dumps(d))" <<< "$issues")
write_index "$base" "$pm_agent" "$issues"
if [ "$base" = "null" ]; then
if [ "$pm_agent" = "null" ]; then
write_index "null" "null" "$issues"
else
write_index "null" "\"$pm_agent\"" "$issues"
fi
else
if [ "$pm_agent" = "null" ]; then
write_index "\"$base\"" "null" "$issues"
else
write_index "\"$base\"" "\"$pm_agent\"" "$issues"
fi
fi
}
remove_issue_from_index() {
@@ -93,7 +113,19 @@ remove_issue_from_index() {
issues=$(python3 -c "import sys, json; d=json.load(sys.stdin); d.pop('$issue_ref', None); print(json.dumps(d))" <<< "$issues")
write_index "$base" "$pm_agent" "$issues"
if [ "$base" = "null" ]; then
if [ "$pm_agent" = "null" ]; then
write_index "null" "null" "$issues"
else
write_index "null" "\"$pm_agent\"" "$issues"
fi
else
if [ "$pm_agent" = "null" ]; then
write_index "\"$base\"" "null" "$issues"
else
write_index "\"$base\"" "\"$pm_agent\"" "$issues"
fi
fi
}
validate_issue_ref() {

View File

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

View File

@@ -44,7 +44,7 @@ check_task_completion() {
if [ -n "$pid" ] && [ "$pid" != "None" ]; then
if ! kill -0 "$pid" 2>/dev/null; then
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees")
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$WORKTREES_DIR")
local has_commits=false
if [ -d "$worktree_path" ] && [ -d "$worktree_path/.git" ]; then
@@ -64,7 +64,7 @@ check_task_completion() {
fi
else
if [ -n "$session_id" ] && ! opencode session list 2>/dev/null | grep -q "$session_id"; then
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$HOME/.kugetsu-worktrees")
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref" "$WORKTREES_DIR")
local has_commits=false
if [ -d "$worktree_path" ] && [ -d "$worktree_path/.git" ]; then

View File

@@ -83,6 +83,10 @@ EOF
if [ -n "$existing_base" ] && [ "$existing_base" != "null" ]; then
if [ "$force" = true ]; then
echo "Warning: Reinitializing sessions (force mode)" >&2
echo "Destroying all sessions, worktrees, and logs..." >&2
cmd_destroy --base -y 2>/dev/null || true
cmd_destroy --pm-agent -y 2>/dev/null || true
rm -f "$LOGS_DIR"/*.log 2>/dev/null || true
else
echo "Error: Base session already exists: $existing_base" >&2
echo "Use --force to reinitialize" >&2
@@ -221,7 +225,10 @@ cmd_delegate() {
exit 1
fi
nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$new_session'" >> "$log_file" 2>&1 &
local msg_file="$LOGS_DIR/msg-$new_session.txt"
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"))"
}
@@ -234,24 +241,47 @@ create_session() {
fi
local before_json=$(opencode session list --format=json 2>/dev/null)
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 "")
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 "|")
opencode run --fork --session "$base_session" "new session" 2>/dev/null
opencode run --fork --session "$base_session" "new session" >/dev/null 2>&1
sleep 1
local after_json=$(opencode session list --format=json 2>/dev/null)
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 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 new_session_id=""
for sess in $after_ids; do
if [[ ! " $before_ids " =~ " $sess " ]] && [[ "$sess" != "$base_session" ]]; then
while IFS= read -r sess; do
if [[ -n "$sess" ]] && [[ ! "$before_set" =~ \|${sess}\| ]]; then
new_session_id="$sess"
break
fi
done
done <<< "$after_sessions"
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() {
local issue_ref="$1"
local user_message="${2:-}"
@@ -309,26 +339,44 @@ cmd_start() {
exit 1
fi
local session_file=$(issue_ref_to_filename "$issue_ref")
local session_path="$SESSIONS_DIR/$session_file"
local worktree_exists=false
if worktree_exists "$issue_ref"; then
echo "Issue '$issue_ref' already has a worktree. Use 'kugetsu continue' instead."
worktree_exists=true
fi
local session_exists=false
if [ -f "$session_path" ]; then
session_exists=true
fi
if $worktree_exists && $session_exists; then
echo "Issue '$issue_ref' already has a worktree and session." >&2
echo "Use 'kugetsu continue $issue_ref' to continue work." >&2
exit 1
fi
if $worktree_exists && ! $session_exists; then
echo "Warning: Worktree exists but session is missing. Removing worktree to recreate both..." >&2
remove_worktree_for_issue "$issue_ref"
worktree_exists=false
fi
if ! $worktree_exists && $session_exists; then
echo "Warning: Session exists but worktree is missing. Removing stale session to recreate both..." >&2
rm -f "$session_path"
remove_issue_from_index "$issue_ref"
session_exists=false
fi
local active_count=$(count_active_dev_sessions)
if [ "$active_count" -ge "${MAX_CONCURRENT_AGENTS:-3}" ]; then
echo "Error: Max concurrent agents (${MAX_CONCURRENT_AGENTS:-3}) reached. Use 'kugetsu continue' or wait for an agent to finish." >&2
exit 1
fi
local session_file=$(issue_ref_to_filename "$issue_ref")
local session_path="$SESSIONS_DIR/$session_file"
if [ -f "$session_path" ]; then
echo "Session file already exists: $session_file"
echo "Use 'kugetsu continue $issue_ref' to continue work."
exit 1
fi
create_worktree "$issue_ref" "$WORKTREES_DIR"
local new_session_id=$(create_session "$base_session_id")
@@ -351,7 +399,11 @@ cmd_start() {
load_agent_env "dev"
cd "$worktree_path"
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 sanitized_id=$(echo "$new_session_id" | sed 's/[^a-zA-Z0-9_-]/_/g')
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 "Worktree: $worktree_path"
@@ -400,16 +452,32 @@ cmd_continue() {
local worktree_path=$(python3 -c "import json; print(json.load(open('$session_path')).get('worktree_path', ''))" 2>/dev/null || echo "")
local issue_ref=$(python3 -c "import json; print(json.load(open('$session_path')).get('issue_ref', ''))" 2>/dev/null || echo "")
if [ -z "$worktree_path" ] || [ ! -d "$worktree_path" ]; then
echo "Warning: Worktree is missing for '$session_name'. Recovering..." >&2
local previous_context=$(load_session_context "$opencode_session_id")
rm -f "$session_path"
remove_issue_from_index "$session_name"
if [ -n "$previous_context" ]; then
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"
return $?
fi
if [ -z "$message" ]; then
message=$(build_dev_agent_message "$issue_ref" "")
fi
if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then
cd "$worktree_path"
nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 &
else
nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 &
fi
cd "$worktree_path"
local sanitized_id=$(echo "$opencode_session_id" | sed 's/[^a-zA-Z0-9_-]/_/g')
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() {