Skip to content

feat: offline context usage estimation for sessions - #448

Open
111-test-111 wants to merge 1 commit into
agegr:mainfrom
111-test-111:pr/offline-context-usage
Open

feat: offline context usage estimation for sessions#448
111-test-111 wants to merge 1 commit into
agegr:mainfrom
111-test-111:pr/offline-context-usage

Conversation

@111-test-111

Copy link
Copy Markdown

What

Estimates a session's context-window usage (percent + token counts) by reading the session .jsonl file directly — no live AgentSession required — and serves it from the session API routes:

  • GET /api/sessions/[id] and GET /api/sessions/[id]/context?leafId= responses gain a best-effort contextUsage field (failures never fail the response)
  • The hook consumes file-derived contextUsage on session/context load, so non-running sessions show usage immediately; the system-prompt panel is loaded on demand via get_state and refreshes live usage
  • Extracts the inline model loading from /api/models into lib/models-loader.ts (shared, adds contextWindow to modelList) since the estimator needs model context windows

Notes

  • Purely additive to server responses; no UI behavior changes for running sessions.
  • 9 new tests (7 lib + 1 hook + 1 route-wiring). Full suite: 432 pass / 0 fail; tsc --noEmit and eslint clean.
  • Heads-up: this touches app/api/sessions/[id]/route.ts imports/response, as does the companion branch-labels PR — the second one to land may need a trivial rebase.

Estimate a session's context token usage (percent of the model's context
window) by reading the .jsonl session file offline — no live agent
session needed. Mirrors AgentSession.getContextUsage(): same compaction
gate, same last-valid-usage + trailing-estimate formula, with the model
resolved from the branch's model_change entries.

The session detail and context API routes now return contextUsage using
the shared models cache; the models registry loader is extracted from
/api/models into lib/models-loader.ts so both can use it. The client
hook consumes the file-derived value in loadSession/loadContext so the
top-bar and session-stats panel show usage for sessions that are not
running, and the system panel fetches live state on demand.
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