Skip to content

Show active (compute) time alongside wall-clock in Status (#147) - #164

Merged
anilmurty merged 1 commit into
mainfrom
feat/147-active-time
Jun 19, 2026
Merged

anilmurty merged 1 commit into
mainfrom
feat/147-active-time

Conversation

@anilmurty

Copy link
Copy Markdown
Contributor

Closes #147.

Problem

The Status tile showed only wall-clock Duration (ended_at − started_at). Claude Code sessions resume across days, so a tile reading Duration: 3087m (~51h) looks like a runaway session when the agent was actually active for minutes. Active time (Σ span duration_ms) already exists in the data — it just wasn't surfaced.

Fix

Surface active (compute) time alongside the wall-clock value across every Status surface:

  • core/db.py — session_active_seconds(conn, session_id): sum of span durations in seconds; returns None when the session has no timed spans (so callers omit rather than show a misleading 0).
  • /api/v1/status — payload now carries active_seconds next to duration_seconds.
  • UI status tile (ui/index.html) — new Active row (compute time) + the wall-clock row relabeled Elapsed, each with an explanatory tooltip. Added fmtDurLong so multi-day spans render 2d 3h instead of 3087m.
  • CLI tj status — replaced the misleading inline (3087m 0s) with a Duration: active 12m 3s · elapsed 2d 3h row; both fields included in --json.

Example CLI output for a resumed session (12m active, 51h elapsed):

● claude-code   active
  ...
  Duration:       active 12m 3s · elapsed 2d 3h

Scope note

The issue scoped UI + API; I also applied it to tj status (CLI) since it had the identical defect. MCP get_status proxies the API and maps explicit fields, so it's unaffected (the new field is simply not mapped) — left out of scope.

Verification

  • ruff check tokenjam/ ✅ · mypy ✅ · full suite 768 passed ✅
  • Tests added: core helper unit tests (sum / no-spans / unknown-session), /api/v1/status active_seconds, CLI --json fields, and a Lens UI regression pinning the Active/Elapsed labels + fmtDurLong (and asserting the bare "Duration" label is gone).
  • UI offline regression (test_ui_offline.py) still green — no external loads added.

🤖 Generated with Claude Code

@anilmurty anilmurty left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review: ✅ Approve pending rebase + CI

Implementation is sound. Please rebase first — PR #163 merged a few minutes ago and touched both ui/index.html and tests/unit/test_lens_ui_regression.py, the same files this PR edits. CI hasn't run yet because of the conflict state; once rebased and re-pushed, CI should go green and this is ready to merge.

Strengths

Honest semantic split. Active = compute work (Σ span durations); Elapsed = wall-clock. The UI tooltip copy explicitly explains "Resumed Claude Code sessions span days, so this can be far larger than Active time." That's exactly the framing #147 (and #141's finding #18) asked for — no more "3087m" reading as a runaway session.

None not 0 when there are no spans. session_active_seconds() returns None for the missing-data case so callers can omit the field rather than display a misleading zero. The docstring explicitly justifies this — good honesty discipline.

Layered test coverage — every layer doing a different job, no redundancy:

  • tests/unit/test_db_helpers.py (NEW) — the SQL helper itself, including "no spans" and "unknown session_id" branches
  • tests/integration/test_api.py — API contract (active_seconds in payload, distinct from duration_seconds)
  • tests/integration/test_cli.py — CLI JSON shape carries both fields
  • tests/unit/test_lens_ui_regression.py — locks Active/Elapsed labels in, asserts the misleading bare Duration label is gone

fmtDurLong coarse formatter does its job. 2d 3h instead of 3087m for resumed sessions. The threshold (≥1h shows hours-up; otherwise minutes-and-seconds) means a real 12-minute Active span still renders fine-grained while a 51-hour Elapsed span reads naturally.

MCP was correctly left out of scope. get_status proxies the API and reads explicit fields — the new active_seconds field passes through transparently. Good situational awareness.

Built in an isolated worktree (/tmp/tj-147-wt). CLAUDE.md Rule 20 / Working-with-Concurrent-Agents guidance followed.

Two non-blocking nits

  1. CLI label could read Time: for parity. The CLI now renders Duration: active 12m 3s · elapsed 2d 3h. Both are durations of different kinds, so the label is technically fine — but Time: would be slightly more honest given the line shows two semantically-distinct measures. Pure copy nit.

  2. JS fmtDurLong vs Python _fmt_dur(coarse=True). Two helpers doing the same conceptual job. JS could mirror the Python pattern (fmtDur(ms, { coarse: true }) instead of a separate function). Minor symmetry thing, not blocking.

To merge

  • Rebase / merge main into the branch (#163's ui/index.html and test_lens_ui_regression.py changes are now in main and will overlap)
  • Re-push, wait for CI
  • The conflict resolution should be mechanical — both PRs only add new functions/tests to those files, not modify existing lines

Ready when CI's green.

The Status surfaces showed only wall-clock `Duration` (ended_at − started_at),
so a resumed Claude Code session read as a 51h runaway. Active time (Σ span
duration_ms) already lives in the data — now surfaced everywhere Status renders.

- core/db.py: `session_active_seconds(conn, session_id)` — sum of span
  durations in seconds; None when the session has no timed spans.
- /api/v1/status: payload carries `active_seconds` next to `duration_seconds`.
- UI status tile: new "Active" row (compute time) + the wall-clock row
  relabeled "Elapsed", each with an explanatory tooltip. Added `fmtDurLong`
  so multi-day spans render "2d 3h" instead of "3087m".
- CLI `tj status`: replaced the misleading inline "(3087m 0s)" with a
  "Duration: active 12m 3s · elapsed 2d 3h" row; both fields in --json.

Tests: core helper unit tests, /api/v1/status active_seconds, CLI --json
fields, and a Lens UI regression pinning the Active/Elapsed labels + fmtDurLong.
ruff + mypy clean; full suite 768 passed.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TAC3wG18qq93ec7NFHCeRM
@anilmurty
anilmurty force-pushed the feat/147-active-time branch from 29485b2 to efe4648 Compare June 19, 2026 23:58
@anilmurty

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main (now atop #163) and force-pushed.

The only conflict was in tests/unit/test_lens_ui_regression.py — both PRs appended new test functions at the same spot. Resolved by keeping both sets (#139/#124 tests + the #147 Active/Elapsed test). ui/index.html auto-merged cleanly (both only add new functions: my fmtDurLong + status-tile rows coexist with #163's _BUCKET_LADDER/coarsening).

ruff clean; full suite 772 passed (includes #163's tests). Branch now sits directly on top of origin/main — should be a clean merge.

(Left the non-blocking JS/Python helper-symmetry nit as-is per your "not blocking".)

@anilmurty
anilmurty merged commit f3bf4ba into main Jun 19, 2026
4 checks passed
@anilmurty
anilmurty deleted the feat/147-active-time branch June 19, 2026 23:59
@anilmurty anilmurty mentioned this pull request Jun 20, 2026
HoomanDgtl pushed a commit to HoomanDigital/tokenjam that referenced this pull request Jun 20, 2026
TokenJam v0.4.1 — concurrency + cycle-aware run-rate + Reuse HTTP fallback +
quality follow-ups across the Lens UI.

## What's in 0.4.1

Six issues closed, all branched off main as separate PRs that landed cleanly:

| Issue | PR | What |
|---|---|---|
| Metabuilder-Labs#138 | Metabuilder-Labs#158 | Run-rate honors `[budget.<provider>] cycle_start_day` (per-tier `cycle_bounds` extracted into `core/cycle.py`, shared with `budget_projection`) |
| Metabuilder-Labs#124 | Metabuilder-Labs#161 | Daemon DB concurrency: `DuckDBBackend.conn` is now a per-thread DuckDB cursor (`threading.local`) over one shared database. Cursors are independent connections safe for concurrent use, so the Overview fan-out + sync read routes in Starlette's threadpool no longer crash under load. |
| Metabuilder-Labs#139 | Metabuilder-Labs#163 | `buildCostSeries` coarsens up an `hour → day → week` ladder instead of silently emptying long windows (closed-form bucket count, never allocates an oversized array). Same PR reverts the Overview's sequential-fetch workaround to `Promise.all` with deliberately-asymmetric error handling (`/cost` no-catch; the other five panels carry per-call fallbacks). |
| Metabuilder-Labs#147 | Metabuilder-Labs#164 | `tj status` shows active (compute) time alongside wall-clock elapsed (`session_active_seconds` = Σ span durations). Both CLI and Lens Status tile rendered, plus `--json` carries both fields. `fmtDurLong` reads multi-day spans as `2d 3h` instead of `3087m`. |
| Metabuilder-Labs#154 | Metabuilder-Labs#165 | New `GET /api/v1/reuse/clusters` endpoint + `ApiBackend.fetch_reuse_clusters` lets `tj report --reuse` work when the daemon holds the DB lock. Dedicated endpoint (not bolted onto `/optimize`) so per-cluster planning text isn't paid for on every 30s Overview poll. |
| Metabuilder-Labs#162 | Metabuilder-Labs#166 | Recoverable Waste tile consistency: title-cased fallback for unknown analyzer names (`reuse` now renders as `Reuse`), em-dash dropped from the not-ready content line, regression guard locks all three states so the bold-title issue can't recur. |

## Pre-release verification

Walked the focused 10-step v0.4.1 checklist via a sub-agent (`tests/agent-pre-release-v0.4.1.md`). Result: **10/10 PASS, 0 FAIL, 0 UNCLEAR**. Full log committed at
`tests/results/agent-pre-release-v0.4.1-20260620T002546Z.md`.

Validated end-to-end against the live daemon:

- 90 concurrent reads against the Overview endpoint set: no SIGABRT, no 500s (was the Metabuilder-Labs#124 reproduction)
- `tj cost` cache columns show non-zero values with daemon up (was 0 pre-Metabuilder-Labs#153)
- `/api/v1/cost` returns a `cycle` block; UI consumes it
- `tj status` shows `active 12m 3s · elapsed 2d 3h`; JSON carries both fields
- `tj report --reuse` writes HTML + Markdown with the daemon running (was the Metabuilder-Labs#154 error)
- Recoverable Waste tile titles render uniformly and "Reuse" is title-cased

## What's in this commit

- `pyproject.toml`: `version = "0.4.1"`
- `sdk-ts/package.json`: `"version": "0.4.1"`
- `tests/agent-pre-release-v0.4.1.md`: the focused 10-step checklist used to validate this release
- `tests/results/agent-pre-release-v0.4.1-20260620T002546Z.md`: the agent-run log proving 10/10 PASS

Tests: 689 pass locally; mypy + ruff clean.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.

[enhancement] Status 'Duration' is wall-clock; show active time too

1 participant