From ab0e1006fbb8394d5e8763f6993ae3e43afcbb27 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Tue, 7 Apr 2026 02:43:53 +0000 Subject: [PATCH] fix: suppress opencode fork stdout and strip ANSI codes from logs 1. Fix create_session: suppress stdout from 'opencode run --fork' - Previously stdout was showing session title which corrupted session ID storage 2. Add strip_ansi_codes() function to clean ANSI escape sequences 3. Update cmd_logs to strip ANSI codes before displaying 4. Fix mask_sensitive_vars to handle empty input gracefully --- skills/kugetsu/scripts/kugetsu-config.sh | 7 ++++++- skills/kugetsu/scripts/kugetsu-log.sh | 4 +++- skills/kugetsu/scripts/kugetsu-session.sh | 24 +++++++++++++---------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/skills/kugetsu/scripts/kugetsu-config.sh b/skills/kugetsu/scripts/kugetsu-config.sh index 8efbcbf..8b20235 100755 --- a/skills/kugetsu/scripts/kugetsu-config.sh +++ b/skills/kugetsu/scripts/kugetsu-config.sh @@ -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" diff --git a/skills/kugetsu/scripts/kugetsu-log.sh b/skills/kugetsu/scripts/kugetsu-log.sh index 84586b3..e4d9782 100755 --- a/skills/kugetsu/scripts/kugetsu-log.sh +++ b/skills/kugetsu/scripts/kugetsu-log.sh @@ -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 diff --git a/skills/kugetsu/scripts/kugetsu-session.sh b/skills/kugetsu/scripts/kugetsu-session.sh index e1a7b9f..c63066d 100755 --- a/skills/kugetsu/scripts/kugetsu-session.sh +++ b/skills/kugetsu/scripts/kugetsu-session.sh @@ -241,20 +241,22 @@ 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" } @@ -376,9 +378,10 @@ cmd_start() { load_agent_env "dev" cd "$worktree_path" - local msg_file="$LOGS_DIR/msg-$new_session_id.txt" + 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-$new_session_id.log" 2>&1 & + 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" @@ -442,9 +445,10 @@ cmd_continue() { fi cd "$worktree_path" - local msg_file="$LOGS_DIR/msg-$opencode_session_id.txt" + 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-$opencode_session_id.log" 2>&1 & + 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" } -- 2.49.1