Repository navigation
Fix #653: cap + strip attributes from trace-detail payload (large traces never loaded) - #659
Conversation
Lens → Traces → clicking a large trace hung on the skeleton loaders forever
(small traces loaded fine). GET /api/v1/traces/{trace_id} returned EVERY span
with its FULL `attributes` dict (captured prompt/completion/tool content) with
no cap. Measured on a live serve, a 45,384-span trace produced a 951 MB JSON
response in 13.4s — the browser could neither fetch, parse, nor render it, so
the skeleton never cleared.
API (routes/traces.py):
- The waterfall payload is now attribute-free — it carries only the structure /
timing / tokens / cost / name / kind / status / ids the tree needs, not the
captured content of every span. That alone removes ~all of the ~1 GB.
- Captured content is fetched lazily per span via a new endpoint
GET /traces/{trace_id}/spans/{span_id}, so the span-detail-on-expand feature
is preserved — the cost is paid per-expand for one span, not upfront for all.
- Span count is capped at 2000 (TRACE_SPAN_CAP). The response returns the true
total `span_count`, `returned_count`, and a `truncated` flag so the UI can say
"showing N of M spans" — an honest disclosure, never a silent drop (Rule 14).
The 5 costliest spans are always included so the "jump to costliest" badges
resolve even in a truncated trace.
SPA (ui/index.html, TraceDetailView):
- Fetch timeout + explicit error state with retry — the skeleton ALWAYS clears
now, never spins forever.
- Renders the `truncated` flag as a visible "Showing N of M spans" notice.
- Caps rendered DOM rows (RENDER_ROW_CAP) so thousands of rows can't freeze the
tab; the remainder is disclosed in the same notice.
- Fetches the selected span's attributes lazily on expand (no captured content
in the waterfall payload any more). No new external HTTP (Rule 18).
Measured after: a 5000-span trace with modest 8KB/span content went from
43.3 MB (old) to 1.06 MB in 185ms (41x smaller); the real 45k-span trace shrinks
far more.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
| Filename | Overview |
|---|---|
| tokenjam/api/routes/traces.py | Adds capped/light trace responses and targeted span lookup, but applies the cap to complete-span API consumers as well as the Lens waterfall. |
| tokenjam/core/api_backend.py | Adds targeted get_span support and preserves attributes, but get_trace_spans cannot recover spans omitted by the route cap. |
| tokenjam/core/db.py | Adds a trace-scoped, targeted single-span lookup consistently across the storage implementations. |
| tokenjam/ui/index.html | Adds bounded waterfall rendering, error recovery, lazy attribute loading, and valid rendered targets for costliest-span navigation. |
| tests/integration/test_trace_detail_payload.py | Covers light-payload capping and targeted lookup, but does not exercise default complete-span responses above the cap. |
| tests/integration/test_storage_backend_parity.py | Extends parity checks to attributes and get_span, though its small trace cannot detect capped get_trace_spans results. |
| tests/unit/test_lens_ui_regression.py | Adds static guards for the new Lens timeout, retry, truncation, lazy-fetch, and row-targeting behavior. |
Sequence Diagram
sequenceDiagram
participant UI as Lens Trace Detail
participant API as Trace API
participant DB as StorageBackend
participant Detail as Span Detail
UI->>API: "GET /traces/{id}?attributes=false"
API->>DB: get_trace_spans(id)
DB-->>API: complete trace
API-->>UI: capped attribute-free waterfall
UI->>API: "GET /traces/{id}/spans/{span_id}"
API->>DB: get_span(id, span_id)
DB-->>API: one complete span
API-->>Detail: span with attributes
Prompt To Fix All With AI
### Issue 1
tokenjam/api/routes/traces.py:184-196
**Complete trace responses remain capped**
When an `ApiBackend`-backed export, CLI trace view, or MCP request reads a trace with more than 2000 spans, the default full-attributes endpoint still applies `TRACE_SPAN_CAP`. `get_trace_spans` treats that truncated list as complete, causing exports to silently omit spans and other complete-trace consumers to receive incomplete data.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Reviews (2): Last reviewed commit: "Fix #659 P1s: full-attrs default, target..." | Re-trigger Greptile
…adges Three Greptile P1 findings on the #653 trace-detail-payload PR: P1-1 — the attribute-free waterfall payload was the DEFAULT of /traces/{id}, so ApiBackend.get_trace_spans (and every complete-span consumer: exports, backfill re-reads) silently received attributes={}. The light payload is now OPT-IN via ?attributes=false; the default keeps FULL attributes so the shim and existing consumers are unchanged. The Lens waterfall fetch passes ?attributes=false; the detail panel still fetches captured content lazily per span. Parity guard extended: the seeded spans now carry captured content and _proj_spans includes attributes, so get_trace_spans full-attribute behavior is parity-tested. P1-2 — GET /traces/{id}/spans/{span_id} loaded the entire trace and deserialized every attribute to pick one span (O(trace) per expand). Added DuckDBBackend.get_span(trace_id, span_id) — a parameterised WHERE span_id=? lookup — plus ApiBackend.get_span (single-span endpoint), and the route uses it. get_span is classified in the parity test with a spec. 404-on-unknown preserved (scoped to the trace). P1-3 — a retained costliest span whose depth-first row index fell past RENDER_ROW_CAP got a jump badge (from the full byId map) but no rendered row, so the jump was dead. Beyond-cap costliest spans are now pinned into the rendered set, and badges gate on the rendered-row id set so a badge can never target a hidden row. Tests: default full-attrs vs opt-in light payload; single-span targeted fetch + 404; get_span parity; Lens static-grep guards for the light-payload param and rendered-only badges. ruff + mypy clean on touched files; node --check clean; integration + parity + lens-unit suites pass. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed all three Greptile P1s (commit 4de8a2a). P1-1 — Waterfall response erases exported attributes ( P1-2 — Lazy lookup loads entire trace ( P1-3 — Costliest badges target hidden rows ( Tests / checks
Not merging — leaving for review. |
| truncated = total > TRACE_SPAN_CAP | ||
| if truncated: | ||
| # Keep the tree navigable: always include the costliest spans (so the | ||
| # "jump to costliest" badges resolve), plus the head of the trace up to | ||
| # the cap. Ordering is otherwise the DB's (chronological), which is what | ||
| # the waterfall expects. | ||
| keep_ids = set(top_cost_span_ids) | ||
| capped: list = [] | ||
| for s in spans: | ||
| if len(capped) >= TRACE_SPAN_CAP and s.span_id not in keep_ids: | ||
| continue | ||
| capped.append(s) | ||
| spans = capped |
There was a problem hiding this comment.
Complete trace responses remain capped
When an ApiBackend-backed export, CLI trace view, or MCP request reads a trace with more than 2000 spans, the default full-attributes endpoint still applies TRACE_SPAN_CAP. get_trace_spans treats that truncated list as complete, causing exports to silently omit spans and other complete-trace consumers to receive incomplete data.
Knowledge Base Used: API Server
Prompt To Fix With AI
This is a comment left during a code review.
Path: tokenjam/api/routes/traces.py
Line: 184-196
Comment:
**Complete trace responses remain capped**
When an `ApiBackend`-backed export, CLI trace view, or MCP request reads a trace with more than 2000 spans, the default full-attributes endpoint still applies `TRACE_SPAN_CAP`. `get_trace_spans` treats that truncated list as complete, causing exports to silently omit spans and other complete-trace consumers to receive incomplete data.
**Knowledge Base Used:** [API Server](https://app.greptile.com/metabldr/-/custom-context/knowledge-base/metabuilder-labs/tokenjam/-/docs/api-server.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Clicking a large trace in Lens → Traces spun on the skeleton loaders forever.
GET /api/v1/traces/{trace_id}returned every span with its full captured-contentattributesdict, uncapped — a measured 951 MB / 13.4s response for a 45,384-span trace, which the browser could neither fetch nor render.Summary
GET /traces/{trace_id}/spans/{span_id}returns one span with its attributes, so the span-detail-on-expand feature is preserved.API (
tokenjam/api/routes/traces.py)get_trace: waterfall payload dropsattributes; caps atTRACE_SPAN_CAP = 2000; returns truespan_count,returned_count,truncated, andspan_cap. The 5 costliest spans are always included even when truncated so the "jump to costliest" badges resolve._span_to_dict(..., include_attributes=False)— attributes only when explicitly requested; adds a cheaphas_attributesboolean so the UI knows whether a lazy fetch is worth making.get_trace_spanroute returns a single span with full attributes.SPA (
tokenjam/ui/index.html,TraceDetailView)Promise.racefetch timeout (20s) +loadStateerror branch with a Retry button — the skeleton can never hang.truncatedas a visible "Showing N of M spans" notice (honest disclosure, Rule 14 — no silent cap).RENDER_ROW_CAP = 1000on rendered DOM rows so a big tree can't freeze the tab; the remainder is disclosed in the same notice.Tests / Verification
tests/integration/test_trace_detail_payload.py— a 3000-span trace returns a capped, attribute-free, <5 MB payload withtruncated: trueand correct totalspan_count; small traces aren't truncated; the per-span endpoint returns attributes for one span and 404s for an unknown span.tests/unit/test_lens_ui_regression.py— static-grep guards: load-error/retry state present, fetch timeout present, truncation notice present, lazy per-span attributes fetch present (and the oldJSON.stringify(sel.attributesis gone), render-row cap present.test_ui_offline.pygreen;node --checkon the extracted module script passes;ruff+mypyclean on touched files; storage/data-access parity tests pass.What's NOT in this PR
ApiBackend/serve-shim change — this is a route-level fix over the existingget_trace_spansprotocol method; the lazy per-span endpoint reuses it.Closes #653
🤖 Generated with Claude Code