Skip to content

ZSH widget: Add timing and trace modes for debugging/performance analysis #84

@noahgift

Description

@noahgift

Feature Request

Add environment variable toggles for debugging and performance analysis.

Proposed Environment Variables

Variable Purpose
APRENDER_TIMING=1 Show suggestion latency
APRENDER_TRACE=1 Log all suggestions to file
APRENDER_TRACE_FILE Custom trace file path (default: /tmp/aprender-trace.log)

Use Cases

  1. Performance debugging - Identify slow suggestions causing shell lag
  2. Model quality analysis - Review what suggestions are being made
  3. Bug reports - Users can attach trace logs

Suggested Implementation

_aprender_suggest() {
    [[ "$APRENDER_DISABLED" == "1" ]] && return
    [[ ${#BUFFER} -lt 2 ]] && { POSTDISPLAY=""; region_highlight=(); return; }

    local suggestion start_ms end_ms elapsed_ms
    
    # Timing
    [[ "$APRENDER_TIMING" == "1" ]] && start_ms=$(($(date +%s%N)/1000000))
    
    suggestion=$(timeout 0.1 aprender-shell suggest "$BUFFER" 2>/dev/null | head -1 | cut -f1)
    
    # Timing output
    if [[ "$APRENDER_TIMING" == "1" ]]; then
        end_ms=$(($(date +%s%N)/1000000))
        elapsed_ms=$((end_ms - start_ms))
        # Show in right prompt or log
        export APRENDER_LAST_MS=$elapsed_ms
    fi
    
    # Trace logging
    if [[ "$APRENDER_TRACE" == "1" ]]; then
        local trace_file="${APRENDER_TRACE_FILE:-/tmp/aprender-trace.log}"
        printf "%s\t%s\t%s\t%sms\n" "$(date -Iseconds)" "$BUFFER" "$suggestion" "${elapsed_ms:-?}" >> "$trace_file"
    fi

    if [[ -n "$suggestion" && "$suggestion" != "$BUFFER" ]]; then
        POSTDISPLAY=" ${suggestion#$BUFFER}"
        region_highlight=("${#BUFFER} $((${#BUFFER} + ${#POSTDISPLAY})) fg=8")
    else
        POSTDISPLAY=""
        region_highlight=()
    fi
}

Trace Output Format

2025-01-15T10:30:45-05:00	git sta	git status	12ms
2025-01-15T10:30:46-05:00	git status && 	git status && git push	8ms
2025-01-15T10:30:50-05:00	cargo t	cargo test	5ms

Optional: RPROMPT Integration

Show last suggestion latency in right prompt:

RPROMPT='${APRENDER_TIMING:+${APRENDER_LAST_MS}ms}'

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions