πŸ“ OMP ↔ Langfuse Tracing Extension (Perfected SSOT)

Origin: ~/.omp/agent/extensions/langfuse.ts Status: 100% Codebase Verified & Omniscience Synthesized

1. πŸš€ Quick Start

The Langfuse extension provides zero-dependency, non-blocking telemetry for Oh My Pi (OMP) sessions. It relies entirely on Bun-native fetch and crypto.randomUUID().

Activation Environment Variables:

export LANGFUSE_PUBLIC_KEY="pk-lf-***"
export LANGFUSE_SECRET_KEY="sk-lf-***"
export LANGFUSE_HOST="https://cloud.langfuse.com"

2. 🧠 Architecture

The telemetry extension maps the native OMP event loop to Langfuse’s hierarchical trace structures.

graph TD
    S["Session (trace-create)"] -->|"starts"| T["Turn (span-create)"]
    T -->|"yields"| LLM["LLM Call (generation-create)"]
    T -->|"invokes"| Tool["Tool Call (span-create)"]
    T -->|"triggers"| Comp["Compaction (span-create)"]
    S -->|"ends"| S_End["Session Shutdown (trace-update)"]
    
    style S fill:#2d3748,stroke:#4a5568,color:#fff
    style T fill:#2b6cb0,stroke:#2c5282,color:#fff
    style LLM fill:#48bb78,stroke:#2f855a,color:#fff
    style Tool fill:#ed8936,stroke:#c05621,color:#fff

1. πŸ—ΊοΈ Event Map

  • Session: session_start βž” session_shutdown (Captures total turns).
  • Turns: turn_start βž” turn_end (Captures duration, context tokens, usage %).
  • LLM Generations: before_provider_request βž” after_provider_response (Captures input/output tokens, cost, cache read/write, stop reason).
  • Tool Spans: tool_call βž” tool_result (Captures sanitized I/O, duration, error state).
  • Annotations: session_compact, auto_retry_start, ttsr_triggered, goal_updated (Renames trace to active objective).

3. 🚨 Gotchas

TrapRiskEnforced Fix / Invariant
Payload LimitsLangfuse rejects batches > 3.5MB.Aggressively deep-truncates I/O to 64k-250k chars. Omits base64 images entirely.
Secret LeakageTokens in prompts sent to cloud.Auto-redacts regex-matched standard tokens + dynamic runtime ENV variables matching secret patterns.
Event Loop HangsNetwork failure crashes OMP.Batched async flushes strictly bounded by a 10s AbortSignal timeout.
Extension CrashesUncaught exceptions kill process.All event hooks wrapped in safeOn try-catch blocks.

4. πŸ”§ Configuration

VariableDescriptionDefault
LANGFUSE_FLUSH_MSBatch flush interval in milliseconds2000
LANGFUSE_MAX_QUEUEMax queued events before forced flush500
LANGFUSE_SAMPLE_RATE0.0-1.0 fraction of sessions to trace1.0
LANGFUSE_TAGSComma-separated extra tags (e.g., prod)""
LANGFUSE_DEBUGSet to "1" to log flush diagnostics"0"
LANGFUSE_UI_INDICATOREnable OMP UI tracing indicatortrue