From ef0f597d5413f5869cfb21a26f6df20938b189ec Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Sun, 14 Dec 2025 12:31:26 +0000 Subject: [PATCH] wip Signed-off-by: 35C4n0r --- .../modules/claude-code/scripts/start.sh | 91 ++++++++++--------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/registry/coder/modules/claude-code/scripts/start.sh b/registry/coder/modules/claude-code/scripts/start.sh index 9fd13956..3d3c620a 100644 --- a/registry/coder/modules/claude-code/scripts/start.sh +++ b/registry/coder/modules/claude-code/scripts/start.sh @@ -3,8 +3,9 @@ # Set strict error handling AFTER sourcing bashrc to avoid unbound variable errors from user dotfiles set -euo pipefail -# Redirect all script output to log file -exec >> "$HOME/start.log" 2>&1 +log() { + printf "%s" "$*" >> "${HOME}/start.log" +} command_exists() { command -v "$1" > /dev/null 2>&1 @@ -16,7 +17,7 @@ ARG_CONTINUE=${ARG_CONTINUE:-false} ARG_DANGEROUSLY_SKIP_PERMISSIONS=${ARG_DANGEROUSLY_SKIP_PERMISSIONS:-} ARG_PERMISSION_MODE=${ARG_PERMISSION_MODE:-} ARG_WORKDIR=${ARG_WORKDIR:-"$HOME"} -ARG_AI_PROMPT=$(echo -n "${ARG_AI_PROMPT:-}" | base64 -d) +ARG_AI_PROMPT=$(log -n "${ARG_AI_PROMPT:-}" | base64 -d) ARG_REPORT_TASKS=${ARG_REPORT_TASKS:-true} ARG_ENABLE_BOUNDARY=${ARG_ENABLE_BOUNDARY:-false} ARG_BOUNDARY_VERSION=${ARG_BOUNDARY_VERSION:-"main"} @@ -28,30 +29,30 @@ ARG_BOUNDARY_PPROF_PORT=${ARG_BOUNDARY_PPROF_PORT:-"6067"} ARG_COMPILE_FROM_SOURCE=${ARG_COMPILE_FROM_SOURCE:-false} ARG_CODER_HOST=${ARG_CODER_HOST:-} -echo "--------------------------------" +log "--------------------------------" -printf "ARG_MODEL: %s\n" "$ARG_MODEL" -printf "ARG_RESUME: %s\n" "$ARG_RESUME_SESSION_ID" -printf "ARG_CONTINUE: %s\n" "$ARG_CONTINUE" -printf "ARG_DANGEROUSLY_SKIP_PERMISSIONS: %s\n" "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" -printf "ARG_PERMISSION_MODE: %s\n" "$ARG_PERMISSION_MODE" -printf "ARG_AI_PROMPT: %s\n" "$ARG_AI_PROMPT" -printf "ARG_WORKDIR: %s\n" "$ARG_WORKDIR" -printf "ARG_REPORT_TASKS: %s\n" "$ARG_REPORT_TASKS" -printf "ARG_ENABLE_BOUNDARY: %s\n" "$ARG_ENABLE_BOUNDARY" -printf "ARG_BOUNDARY_VERSION: %s\n" "$ARG_BOUNDARY_VERSION" -printf "ARG_BOUNDARY_LOG_DIR: %s\n" "$ARG_BOUNDARY_LOG_DIR" -printf "ARG_BOUNDARY_LOG_LEVEL: %s\n" "$ARG_BOUNDARY_LOG_LEVEL" -printf "ARG_BOUNDARY_PROXY_PORT: %s\n" "$ARG_BOUNDARY_PROXY_PORT" -printf "ARG_COMPILE_FROM_SOURCE: %s\n" "$ARG_COMPILE_FROM_SOURCE" -printf "ARG_CODER_HOST: %s\n" "$ARG_CODER_HOST" +log "ARG_MODEL: %s\n" "$ARG_MODEL" +log "ARG_RESUME: %s\n" "$ARG_RESUME_SESSION_ID" +log "ARG_CONTINUE: %s\n" "$ARG_CONTINUE" +log "ARG_DANGEROUSLY_SKIP_PERMISSIONS: %s\n" "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" +log "ARG_PERMISSION_MODE: %s\n" "$ARG_PERMISSION_MODE" +log "ARG_AI_PROMPT: %s\n" "$ARG_AI_PROMPT" +log "ARG_WORKDIR: %s\n" "$ARG_WORKDIR" +log "ARG_REPORT_TASKS: %s\n" "$ARG_REPORT_TASKS" +log "ARG_ENABLE_BOUNDARY: %s\n" "$ARG_ENABLE_BOUNDARY" +log "ARG_BOUNDARY_VERSION: %s\n" "$ARG_BOUNDARY_VERSION" +log "ARG_BOUNDARY_LOG_DIR: %s\n" "$ARG_BOUNDARY_LOG_DIR" +log "ARG_BOUNDARY_LOG_LEVEL: %s\n" "$ARG_BOUNDARY_LOG_LEVEL" +log "ARG_BOUNDARY_PROXY_PORT: %s\n" "$ARG_BOUNDARY_PROXY_PORT" +log "ARG_COMPILE_FROM_SOURCE: %s\n" "$ARG_COMPILE_FROM_SOURCE" +log "ARG_CODER_HOST: %s\n" "$ARG_CODER_HOST" -echo "--------------------------------" +log "--------------------------------" function install_boundary() { if [ "${ARG_COMPILE_FROM_SOURCE:-false}" = "true" ]; then # Install boundary by compiling from source - echo "Compiling boundary from source (version: $ARG_BOUNDARY_VERSION)" + log "Compiling boundary from source (version: $ARG_BOUNDARY_VERSION)" git clone https://github.com/coder/boundary.git cd boundary git checkout "$ARG_BOUNDARY_VERSION" @@ -65,16 +66,16 @@ function install_boundary() { sudo chmod +x /usr/local/bin/boundary-run else # Install boundary using official install script - echo "Installing boundary using official install script (version: $ARG_BOUNDARY_VERSION)" + log "Installing boundary using official install script (version: $ARG_BOUNDARY_VERSION)" curl -fsSL https://raw.githubusercontent.com/coder/boundary/main/install.sh | bash -s -- --version "$ARG_BOUNDARY_VERSION" fi } function validate_claude_installation() { if command_exists claude; then - printf "Claude Code is installed\n" + log "Claude Code is installed\n" else - printf "Error: Claude Code is not installed. Please enable install_claude_code or install it manually\n" + log "Error: Claude Code is not installed. Please enable install_claude_code or install it manually\n" exit 1 fi } @@ -85,12 +86,12 @@ TASK_SESSION_ID="cd32e253-ca16-4fd3-9825-d837e74ae3c2" get_project_dir() { local workdir_normalized - workdir_normalized=$(echo "$ARG_WORKDIR" | tr '/' '-') - echo "$HOME/.claude/projects/${workdir_normalized}" + workdir_normalized=$(log "$ARG_WORKDIR" | tr '/' '-') + log "$HOME/.claude/projects/${workdir_normalized}" } get_task_session_file() { - echo "$(get_project_dir)/${TASK_SESSION_ID}.jsonl" + log "$(get_project_dir)/${TASK_SESSION_ID}.jsonl" } task_session_exists() { @@ -98,10 +99,10 @@ task_session_exists() { session_file=$(get_task_session_file) if [ -f "$session_file" ]; then - printf "Task session file found: %s\n" "$session_file" + log "Task session file found: %s\n" "$session_file" return 0 else - printf "Task session file not found: %s\n" "$session_file" + log "Task session file not found: %s\n" "$session_file" return 1 fi } @@ -112,12 +113,12 @@ is_valid_session() { # Check if file exists and is not empty # Empty files indicate the session was created but never used so they need to be removed if [ ! -f "$session_file" ]; then - printf "Session validation failed: file does not exist\n" + log "Session validation failed: file does not exist\n" return 1 fi if [ ! -s "$session_file" ]; then - printf "Session validation failed: file is empty, removing stale file\n" + log "Session validation failed: file is empty, removing stale file\n" rm -f "$session_file" return 1 fi @@ -127,7 +128,7 @@ is_valid_session() { local line_count line_count=$(wc -l < "$session_file") if [ "$line_count" -lt 2 ]; then - printf "Session validation failed: incomplete (only %s lines), removing incomplete file\n" "$line_count" + log "Session validation failed: incomplete (only %s lines), removing incomplete file\n" "$line_count" rm -f "$session_file" return 1 fi @@ -135,7 +136,7 @@ is_valid_session() { # Validate JSONL format by checking first 3 lines # Claude session files use JSONL (JSON Lines) format where each line is valid JSON if ! head -3 "$session_file" | jq empty 2> /dev/null; then - printf "Session validation failed: invalid JSONL format, removing corrupt file\n" + log "Session validation failed: invalid JSONL format, removing corrupt file\n" rm -f "$session_file" return 1 fi @@ -144,12 +145,12 @@ is_valid_session() { # This ensures the file structure matches Claude's session format if ! grep -q '"sessionId"' "$session_file" \ || ! grep -m 1 '"sessionId"' "$session_file" | jq -e '.sessionId' > /dev/null 2>&1; then - printf "Session validation failed: no valid sessionId found, removing malformed file\n" + log "Session validation failed: no valid sessionId found, removing malformed file\n" rm -f "$session_file" return 1 fi - printf "Session validation passed: %s\n" "$session_file" + log "Session validation passed: %s\n" "$session_file" return 0 } @@ -158,10 +159,10 @@ has_any_sessions() { project_dir=$(get_project_dir) if [ -d "$project_dir" ] && find "$project_dir" -maxdepth 1 -name "*.jsonl" -size +0c 2> /dev/null | grep -q .; then - printf "Sessions found in: %s\n" "$project_dir" + log "Sessions found in: %s\n" "$project_dir" return 0 else - printf "No sessions found in: %s\n" "$project_dir" + log "No sessions found in: %s\n" "$project_dir" return 1 fi } @@ -184,7 +185,7 @@ function start_agentapi() { fi if [ -n "$ARG_RESUME_SESSION_ID" ]; then - echo "Resuming specified session: $ARG_RESUME_SESSION_ID" + log "Resuming specified session: $ARG_RESUME_SESSION_ID" ARGS+=(--resume "$ARG_RESUME_SESSION_ID") [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ] && ARGS+=(--dangerously-skip-permissions) @@ -195,39 +196,39 @@ function start_agentapi() { session_file=$(get_task_session_file) if task_session_exists && is_valid_session "$session_file"; then - echo "Resuming task session: $TASK_SESSION_ID" + log "Resuming task session: $TASK_SESSION_ID" ARGS+=(--resume "$TASK_SESSION_ID" --dangerously-skip-permissions) else - echo "Starting new task session: $TASK_SESSION_ID" + log "Starting new task session: $TASK_SESSION_ID" ARGS+=(--session-id "$TASK_SESSION_ID" --dangerously-skip-permissions) [ -n "$ARG_AI_PROMPT" ] && ARGS+=(-- "$ARG_AI_PROMPT") fi else if has_any_sessions; then - echo "Continuing most recent standalone session" + log "Continuing most recent standalone session" ARGS+=(--continue) [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ] && ARGS+=(--dangerously-skip-permissions) else - echo "No sessions found, starting fresh standalone session" + log "No sessions found, starting fresh standalone session" [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ] && ARGS+=(--dangerously-skip-permissions) [ -n "$ARG_AI_PROMPT" ] && ARGS+=(-- "$ARG_AI_PROMPT") fi fi else - echo "Continue disabled, starting fresh session" + log "Continue disabled, starting fresh session" [ "$ARG_DANGEROUSLY_SKIP_PERMISSIONS" = "true" ] && ARGS+=(--dangerously-skip-permissions) [ -n "$ARG_AI_PROMPT" ] && ARGS+=(-- "$ARG_AI_PROMPT") fi - printf "Running claude code with args: %s\n" "$(printf '%q ' "${ARGS[@]}")" + log "Running claude code with args: %s\n" "$(log '%q ' "${ARGS[@]}")" if [ "${ARG_ENABLE_BOUNDARY:-false}" = "true" ]; then install_boundary mkdir -p "$ARG_BOUNDARY_LOG_DIR" - printf "Starting with coder boundary enabled\n" + log "Starting with coder boundary enabled\n" # Build boundary args with conditional --unprivileged flag BOUNDARY_ARGS=(--log-dir "$ARG_BOUNDARY_LOG_DIR")