Skip to content

Fix #653: cap + strip attributes from trace-detail payload (large traces never loaded) - #659

Merged
anilmurty merged 2 commits into
mainfrom
fix/653-trace-detail-payload
Jul 30, 2026
Merged

anilmurty merged 2 commits into
mainfrom
fix/653-trace-detail-payload

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

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-content attributes dict, uncapped — a measured 951 MB / 13.4s response for a 45,384-span trace, which the browser could neither fetch nor render.

Summary

  • Waterfall payload is now attribute-free (structure/timing/tokens/cost/ids only) and capped at 2000 spans — removes ~all of the ~1 GB.
  • New lazy endpoint GET /traces/{trace_id}/spans/{span_id} returns one span with its attributes, so the span-detail-on-expand feature is preserved.
  • SPA hardened: fetch timeout + error/retry state (skeleton always clears), truncation notice, rendered-row cap, lazy attributes fetch on span select.

API (tokenjam/api/routes/traces.py)

  • get_trace: waterfall payload drops attributes; caps at TRACE_SPAN_CAP = 2000; returns true span_count, returned_count, truncated, and span_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 cheap has_attributes boolean so the UI knows whether a lazy fetch is worth making.
  • New get_trace_span route returns a single span with full attributes.

SPA (tokenjam/ui/index.html, TraceDetailView)

  • Promise.race fetch timeout (20s) + loadState error branch with a Retry button — the skeleton can never hang.
  • Renders truncated as a visible "Showing N of M spans" notice (honest disclosure, Rule 14 — no silent cap).
  • RENDER_ROW_CAP = 1000 on rendered DOM rows so a big tree can't freeze the tab; the remainder is disclosed in the same notice.
  • Selected span's captured content fetched lazily via the new per-span endpoint (was rendered straight from the waterfall payload). No new external HTTP (Rule 18).

Tests / Verification

  • tests/integration/test_trace_detail_payload.py — a 3000-span trace returns a capped, attribute-free, <5 MB payload with truncated: true and correct total span_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 old JSON.stringify(sel.attributes is gone), render-row cap present.
  • test_ui_offline.py green; node --check on the extracted module script passes; ruff + mypy clean on touched files; storage/data-access parity tests pass.
  • Before/after (measured): a 5000-span trace with 8KB/span captured content went from 43.3 MB (old) to 1.06 MB in 185ms — 41x smaller. The real 45k-span trace shrinks far more.

What's NOT in this PR

  • No true windowed virtualization (react-window etc.) — deferred; the server cap + render cap already keep the DOM bounded, and adding a vendored virtual-list lib is out of scope for a bug fix.
  • No ApiBackend/serve-shim change — this is a route-level fix over the existing get_trace_spans protocol method; the lazy per-span endpoint reuses it.

Closes #653

🤖 Generated with Claude Code

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]>
Comment thread tokenjam/api/routes/traces.py Resolved
Comment thread tokenjam/api/routes/traces.py Resolved
Comment thread tokenjam/ui/index.html Resolved
@greptile-apps

greptile-apps Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR reduces large Lens trace-detail payloads while preserving on-demand span inspection.

  • Adds an attribute-free, capped waterfall response with truncation metadata and retained costliest spans.
  • Adds targeted single-span storage and HTTP lookups for lazy attribute loading.
  • Adds UI timeout, retry, row-capping, truncation disclosure, and costliest-row pinning behavior.
  • Expands backend parity and trace-detail regression coverage.

Confidence Score: 3/5

The PR is not yet safe to merge because complete-span consumers still silently lose spans from traces exceeding the waterfall cap.

The default endpoint used by ApiBackend.get_trace_spans restores attributes but unconditionally truncates the span list, so exports and other complete-trace consumers receive incomplete data.

Files Needing Attention: tokenjam/api/routes/traces.py, tokenjam/core/api_backend.py

Important Files Changed

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
Loading
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]>
@anilmurty

Copy link
Copy Markdown
Contributor Author

Addressed all three Greptile P1s (commit 4de8a2a).

P1-1 — Waterfall response erases exported attributes (traces.py:186)
The attribute-free payload is now opt-in, not the default. GET /traces/{id} defaults to attributes=true (FULL spans), so ApiBackend.get_trace_spans and every complete-span consumer (exports, backfill) are unchanged. The Lens waterfall fetch now passes ?attributes=false for the light payload. Response gained an attributes_included flag. The storage-backend parity test was tightened: seeded spans now carry captured content and _proj_spans includes attributes, so get_trace_spans full-attribute behavior across the shim is parity-tested (it was silently uncovered before).

P1-2 — Lazy lookup loads entire trace (traces.py:210)
Added DuckDBBackend.get_span(trace_id, span_id) — a parameterised WHERE trace_id=? AND span_id=? single-row lookup (DuckDB-only) — plus ApiBackend.get_span (hits the single-span endpoint). The route uses get_span instead of scanning + deserializing the whole trace on every expand. 404-on-unknown preserved and scoped to the trace. get_span is a new StorageBackend protocol method, classified in test_storage_backend_parity.py with a spec + attributes assertion.

P1-3 — Costliest badges target hidden rows (index.html:5859)
Beyond-cap costliest spans (badge targets) are now pinned into the rendered set, and each badge additionally gates on a renderedRowIds set, so a jump badge can never point at a row hidden by RENDER_ROW_CAP. No dead badges.

Tests / checks

  • New: default full-attrs vs opt-in light payload; single-span targeted-fetch (spy asserts get_trace_spans is not called) + 404 + trace-scoping; get_span parity; Lens static-grep guards for the ?attributes=false fetch and rendered-only badges.
  • Existing test_trace_detail_payload.py adjusted for the opt-in param.
  • ruff + mypy clean on touched files; node --check clean on the extracted module; integration (656) + both parity tests + lens-unit all pass locally.

Not merging — leaving for review.

Comment on lines +184 to +196
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

@anilmurty
anilmurty merged commit a5a6810 into main Jul 30, 2026
5 checks passed
@anilmurty
anilmurty deleted the fix/653-trace-detail-payload branch July 30, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lens: trace detail never loads for large traces (/traces/{id} returns full span attributes uncapped — ~1GB payload)

1 participant