Skip to content

fix(#63): make tj context render while tj serve is running - #390

Merged
anilmurty merged 2 commits into
mainfrom
ticket-63
Jul 3, 2026
Merged

anilmurty merged 2 commits into
mainfrom
ticket-63

Conversation

@anshss

@anshss anshss commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #63 — tj context (the launch-hero command) refused to run while tj serve daemon was up, telling users to stop the daemon on the exact command the launch drives them to.

  • Root cause: the diagnostic reads the raw attributes column for recurring-inclusion detection, which the API shim never exposed row-by-row. DuckDB refuses a concurrent read-only connection alongside the write lock, so a client-side workaround isn't available.
  • Fix: route the compute through the daemon (mirrors /api/v1/reuse/clusters [follow-up] tj report --reuse needs an HTTP fallback when daemon holds the DB lock #154 and /api/v1/optimize Pre-release testing findings (v0.3.0 sprint) — bugs and playbook fixes #68):
    • new GET /api/v1/context computes the diagnostic + plan-tier framing server-side (tokenjam/api/routes/context.py)
    • new diagnostic_from_dict inverse of diagnostic_to_dict in tokenjam/core/context_diagnostic.py so the CLI reconstructs the dataclass and renders identically
    • cmd_context.py: on ApiBackend (no .conn), fetch + render via _render_via_serve; on a truly non-conn/non-Api backend, still surface a clean error
    • ApiBackend.fetch_context_diagnostic HTTP client shim

Test plan

tests/integration/test_context_serve.py (all 4 pass):

  • endpoint computes diagnostic server-side — recurring inclusion from raw attributes surfaces
  • tj context renders through in-process ASGI-wired ApiBackend (the exact daemon-is-running state) — old refuse-to-run error is gone
  • tj context --json returns identical payload through serve
  • a non-conn/non-Api backend still errors cleanly, no traceback

Existing tests/unit/test_context_diagnostic.py (18 tests) still green.

🤖 Generated with Claude Code

`tj context` is the launch-hero command (issue #4 wedge), but the
composition diagnostic reads the raw `attributes` column for
recurring-inclusion detection — data the API shim never exposed. So
whenever `tj serve` held the DuckDB write lock, the CLI fell back to
`ApiBackend` (no `.conn`) and refused to run, telling users to stop the
daemon on the exact command the launch drives them to.

DuckDB permits only one writer OR many readers across processes (a
concurrent read-only conn alongside the serve write-lock raises an
IOException), so a client-side read-only workaround isn't available.
Route the compute through the daemon instead — which already owns the
direct connection — mirroring `/api/v1/reuse/clusters` (#154) and
`/api/v1/optimize` (#68):

  * new `GET /api/v1/context` endpoint computes the diagnostic
    server-side (incl. recurring inclusions from raw `attributes`) plus
    a plan-tier `framing` block, exactly as the CLI's direct-conn path
    does
  * new `diagnostic_from_dict` inverse of `diagnostic_to_dict` so the
    CLI reconstructs the dataclass and renders identically through serve
  * `cmd_context` shim path: on `ApiBackend` (no `.conn`), fetch +
    render via `_render_via_serve`; on a truly non-conn/non-Api backend,
    still surface a clean error (guards on `isinstance(db, ApiBackend)`)

Covered by `tests/integration/test_context_serve.py`:
  * endpoint computes diagnostic server-side, including the recurring
    file-read only surfaceable from raw `attributes`
  * `tj context` (plain + `--json`) renders through an in-process
    ASGI-wired `ApiBackend` — the exact daemon-is-running state
  * a non-conn/non-Api backend still errors cleanly (no traceback)

Co-Authored-By: Claude <[email protected]>
@anshss
anshss requested a review from anilmurty as a code owner July 3, 2026 03:40

@anilmurty anilmurty left a comment

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.

Approving per review — tj context serve-fallback mirrors the optimize/reuse pattern, parity + honesty preserved, both paths tested. Merging for a clean board.

@anilmurty
anilmurty merged commit e01748c into main Jul 3, 2026
4 checks passed
@anilmurty
anilmurty deleted the ticket-63 branch July 3, 2026 05:09
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.

2 participants