Skip to content

Fix SDK DuckDB lock fallback and LiteLLM model prefix - #13

Merged
anilmurty merged 1 commit into
mainfrom
fix-litellm-prefix-and-bootstrap
Apr 8, 2026
Merged

anilmurty merged 1 commit into
mainfrom
fix-litellm-prefix-and-bootstrap

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

Summary

  • SDK DuckDB lock fix: Bootstrap now detects if ocw serve is running (via GET /api/v1/status) and routes spans over HTTP instead of opening DuckDB directly. Also falls back to HTTP when a DuckDB lock error occurs. Adds OcwHttpExporter(SpanExporter) that POSTs OTLP JSON to /api/v1/spans.
  • LiteLLM model prefix fix: Strips provider prefix from model names before setting the span attribute (openai/gpt-4o-mini → gpt-4o-mini), consistent with patch_openai. Fixes double-prefix in ocw cost pricing lookups (openai/openai/gpt-4o-mini).

Test plan

  • All 232 tests pass
  • ruff check clean
  • Manual: run ocw serve &, then python3 examples/single_provider/anthropic_agent.py — spans should appear (no DuckDB lock error)
  • Manual: run litellm example, then ocw cost — model names show gpt-4o-mini not openai/gpt-4o-mini

🤖 Generated with Claude Code

Two fixes:

1. SDK bootstrap now detects if ocw serve is running (via /api/v1/status)
   and sends spans over HTTP instead of opening DuckDB directly. Falls back
   to HTTP on DuckDB lock errors too. This fixes the most common usage
   pattern: run ocw serve, then run your agent.

2. LiteLLM patch strips provider prefix from model names before setting
   the span attribute (openai/gpt-4o-mini -> gpt-4o-mini), consistent
   with patch_openai. Fixes double-prefix in pricing lookups.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@anilmurty
anilmurty merged commit 00de32a into main Apr 8, 2026
4 checks passed
@anilmurty
anilmurty deleted the fix-litellm-prefix-and-bootstrap branch April 8, 2026 16:38
anilmurty added a commit that referenced this pull request Jun 19, 2026
…#10,#13)

Smoke-test playbook (tests/manual-new-release-tests.md):

- #3 (and #8/#9/#11/#12/#14/#16 — same root cause): code blocks are now
  command-only; the inline "# comments" and "# [ ]" checklists that the markdown
  copy button was pulling into copied commands moved to prose / "- [ ]" lists
  around each block. Added a note explaining the convention.
- #10: corrected the Section 4 trim expectation — with [capture] prompts=false
  (default), trim checks the capture prereq first and prints the capture hint,
  NOT the tokenjam[bloat] install hint (only reachable once capture is on).
- #13: export file is "claude-code-<date>.jsonc", not ".json".

Doc-only; no code changes.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
anilmurty added a commit that referenced this pull request Jun 19, 2026
docs: smoke-test playbook — command-only blocks + trim/jsonc fixes (#3, #10, #13)
anilmurty pushed a commit that referenced this pull request Jul 2, 2026
The zero-install first run (`tj quickstart` / bare `tj`) backfills
~/.claude/projects/*.jsonl into a transient in-memory DB before printing
the headline. Profiling a 3000-session synthetic history showed the
backfill dominates (99.7s) while the diagnostic (0.45s) and timeline
(0.04s) are trivial — the cost is the per-span SELECT+INSERT round-trips
in `_insert_session_idempotent` (120k spans => 240k DuckDB statements).
A first run over a large history blew well past the <30s time-to-first-
value goal #6 targets.

Add a most-recent-N-sessions cap to the first-run path: `iter_claude_code_
sessions` gains an optional `max_sessions` that walks files mtime-desc and
stops once the cap is reached, bounding both parse and insert work
regardless of how large ~/.claude is. `ingest_claude_code` threads it
through and sets `BackfillResult.limit_reached`. `cmd_quickstart` caps at
the most-recent 300 sessions by default (≈9s even on an 8000-session
history), exposes `--full` to lift the cap, and discloses the truncation
honestly ("Showing your most-recent N sessions … run tj quickstart --full
or tj context for your full history") so it never reads as "this is
everything". The full `tj backfill claude-code` path passes no cap and is
byte-for-byte unchanged.

Tests assert BOUNDED WORK (only N sessions/rows ingested on a large
synthetic history, the cap keeps the freshest sessions, the limit flag and
CLI disclosure render) rather than a flaky wall-clock number.

Co-Authored-By: Claude <[email protected]>
anilmurty pushed a commit that referenced this pull request Jul 2, 2026
The Claude Code backfill ran one existence-check SELECT plus one INSERT per
span in `_insert_session_idempotent` — ~2 DuckDB round-trips per span, so a
large history (~120k spans) paid ~240k statements (~100s). #13 bounded only
the quickstart first-run path; the full `tj backfill claude-code` / daemon
path still paid the full per-span cost.

Replace the per-span loop with a bulk path that processes a whole session in
a BOUNDED number of statements regardless of span count:

- Partition new-vs-existing span_ids in ONE chunked `WHERE span_id IN (...)`
  query (`_existing_span_ids`) instead of N existence SELECTs.
- Bulk-insert the new spans in a single `executemany` (`_SPAN_INSERT_SQL` +
  `_span_insert_params`, kept in lock-step with `DuckDBBackend.insert_span`).
- On `--reingest`, batch-load the existing rows' attributes in ONE chunked
  query (`_load_attrs_bulk`), compute the per-key attribute merge in Python,
  then apply the updates in a single `executemany`.

The #10 idempotency + reingest contract is preserved exactly: new spans
insert; existing spans without `--reingest` are skipped untouched; existing
spans with `--reingest` get `sub_agent_id` updated and `attributes` per-key
merged (overlay `{**stored, **parsed}`, parsed wins, never wipes a stored
key). The no-`conn` fallback path is unchanged. Drops the now-unused
single-span `_load_attrs` helper.

Tests: all existing #3/#10/#13 backfill tests pass unchanged. Adds four #15
tests — bulk insert lands every new span; existing spans are skipped without
`--reingest`; `--reingest` still merges attributes (overlay, no wipe) +
updates sub_agent_id; and a counting-connection test asserting a 400-span
session inserts in a BOUNDED number of statements (well under N execute()
calls, via executemany) rather than ~2 per span.

Co-Authored-By: Claude <[email protected]>
(cherry picked from commit 6f94fe0)
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.

1 participant