Skip to content

feat(dash): opt-in lifecycle signals (thread, wait cause inputs, gaps, granted TTL) - #447

Merged
OsherElhadad merged 1 commit into
mainfrom
feat/lifecycle-signals
Oct 11, 2026
Merged

OsherElhadad merged 1 commit into
mainfrom
feat/lifecycle-signals

Conversation

@OsherElhadad

Copy link
Copy Markdown
Collaborator

Refs #424, #427. Default OFF (--dashboard-lifecycle-signals); flag off writes nothing.

Adds side table request_lifecycle (additive DDL, FK cascade to requests):

  • thread id (hash of tenant+session+model+tools+system, billing-header nonce excluded; a message-count drop starts a new generation) and a parent hint when tools/system differ from the session's first thread
  • previous response: stop_reason (remembered per thread), tool_use names (metaEnum-redacted, max 8) and count; incoming request: tool_result count, total bytes, is_error flag
  • gap since previous request start and since previous response end on the same thread (-1 = unknown)
  • granted TTL tier (5m/1h/mixed) from the response's cache-write tiers
  • keep-alive pings already exist as requests rows (keepalive=1: ts, prefix = cache_read+cache_write, hit/miss, cost); only the missing thread link and ping ordinal are added

Privacy: no tool inputs, no tool_result text, only names/counts/sizes/hashes.

Limits: thread state is in memory (first request after restart has gap -1); ping rows link to the session's latest thread on that model because the keeper is per session on this base (per thread comes with #426); identical-prefix forks interleaving in one session share a thread; wake_cause string markers from #424 are not included.

Tests: unit (scan, redaction, nonce, thread/gap/ttl/parent, store round trip) and an end-to-end two-turn request through the test proxy, plus flag-off. Live: real claude -p session running Bash through a separate proxy on :4180 recorded the expected rows (turn 2: prev_stop tool_use, Bash x1, 1 result, gap 6514 ms, since-end 882 ms).

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: approved. CI all pass; go test ./dash (full, 64 s) and the proxy lifecycle tests green; gofmt clean.

Checked:

  • Privacy: ScanLifecycle reads tool_use names through metaEnum (credential shapes and anything not a short identifier become the redacted marker), stop reasons through metaEnum, and tool_result content only by byte length; tool inputs are never read. The redaction test is real: it feeds an sk-ant-... shaped name plus a SECRET-INPUT input and asserts the name is redacted; the proxy end-to-end test seeds secrets in inputs. Thread ids are truncated sha256 of the key (tenant/session inside the hash only).
  • Flag off: LifecycleOn() is false so no scan, no state, no row; the only footprint is an empty table.
  • DDL: CREATE TABLE/INDEX IF NOT EXISTS via additiveDDL (strictly new table, no schemaVersion bump, so no rename-aside of the 3.6 GB DB); the table is empty on open, so the index build is instant.
  • Retention/archive: request_id REFERENCES requests ON DELETE CASCADE with foreign_keys(1) in the DSN, so Prune, archive, janitor and tenant purge (all DELETE FROM requests) clear it; PK lookup, no scan.
  • Tenant scoping: no HTTP endpoint reads it. DB.Lifecycles() is unscoped but unexported to the UI and documented as analysis/test only; scope it by tenant before any endpoint uses it.
  • Hot path: gated; I measured ScanLifecycle at 2.5 ms for a 0.9 MB body (350 MB/s), done synchronously on the request path, plus one mutex section.

Non-blocking:

  1. Memory/eviction: thread state is capped at 20,000 and sessions at 20,000. Above the cap, every request iterates the whole thread map (O(n) under the mutex) and the session map is dropped wholesale, which loses first-thread and last-thread links (wrong parent hints until sessions are re-seen). Fine at today's scale (live threads within the 2 h stale window are far below 20k); evict by age or amortise before enabling on a much larger fleet.
  2. endTS is time.Now() at finish; for a long stream the 'since previous end' is correct, but document that gaps are measured by the proxy clock.

@OsherElhadad OsherElhadad added the ready-to-merge Program PR approved by the reviewer, CI green; needs maintainer approval label Oct 11, 2026
@amiddavid

Copy link
Copy Markdown
Collaborator

Release sync: #447 is in this release, after #453

Result: please wait before you start on this PR. It is blocked on #453 (the record half of #424). Start the rework below after #453 merges. I will post on this PR when it does.

Why it is blocked

  1. feat(keepalive,dash): key cache attribution and keep-alive per thread #426 (merged) already defines the thread id. It uses the x-claude-code-agent-id header, falls back to prefix continuity for every other provider, and stores the result as requests.thread_id. feat(dash): opt-in lifecycle signals (thread, wait cause inputs, gaps, granted TTL) #447 computes its own thread id (a hash of tenant, session, model, tools and system). One release cannot have 2 thread ids that disagree.
  2. feat(dash,proxy): record wait cause and wake cause per request (#424 record half) #453 records some of the same signals. It adds these nullable columns to requests: waiting_for, wait_tool, wake_cause, open_background, open_teammate and monitor_expiry_ms. It builds them from the previous response's stop_reason and tool_use names, and from the Record why a thread waits, and set the keep-alive ping budget from it #424 string markers.
  3. The 2 PRs conflict in cmd/context-guru-proxy/main.go and dash/capture.go (checked with git merge-tree).

Proposed rework, after #453 merges

#447 field Proposal
Its own thread id and generation Drop it. Use requests.thread_id from #426.
Parent hint Keep it only if #426's thread data does not already give it. #426 knows which thread a fork comes from.
Previous stop_reason, tool_use names and count Drop them. #453's waiting_for and wait_tool cover these, and #453 already parses response tool calls in proxy/usage.go (Usage.ToolUseNames).
Gap since the previous request start, on the same thread Drop it. #426 stores it as SinceLastMs, per thread.
Gap since the previous response end Keep. It is new.
Granted TTL tier (5m / 1h / mixed) Keep. It is new.
Incoming tool_result count, bytes, is_error Keep, unless #453's columns already cover them.
Ping ordinal, and the ping's thread link Keep the ordinal. The thread link already exists: since #426, ping rows carry thread_id, because the keeper is per thread.

Also:

  • Read the conversation from apply.NormalizeMessages, as feat(dash,proxy): record wait cause and wake cause per request (#424 record half) #453 now does, not from a separate parser.
  • Note a known defect on main: the Anthropic branch of the normalizer drops the text blocks that share a user message with a tool_result, for example text the user typed mid-turn and system reminders. A separate fix is being decided. It changes what any field built from the normalized messages sees.

Merge order for this release

#431 (merged) → #414 → #456 → #425 → #453 → #447 (reworked) → #444 → integration check → release.

#444 does not conflict with #453 and is in this release too.

@amiddavid

Copy link
Copy Markdown
Collaborator

Unblocked: #453 is merged

#453 (the record half of #424) is on main. You can start the rework of #447 now. The plan is the one in my earlier comment above.

What is on main now:

Please keep only the new signals in #447: the gap since the previous response ended, the granted TTL tier, the incoming tool_result count, bytes and is_error, and the ping ordinal. Drop its own thread id, and the signals #453 already records.

Merge order: #447 (reworked) → #444 → integration check → release.

Behind --dashboard-lifecycle-signals (default off), a request_lifecycle side table
stores the gap from the previous response end on the same thread, the granted cache
TTL tier, the incoming tool_result count/bytes/error flag, and the keep-alive ping
ordinal. The thread id, previous stop reason and tool names are the requests columns
from #426 and #453 and are no longer duplicated. The body is read through
apply.NormalizeMessages on the dashboard writer goroutine; stale thread state is
evicted by age, amortised to once a minute; Lifecycles is scoped by tenant.

Signed-off-by: Osher-Elhadad <[email protected]>
@OsherElhadad
OsherElhadad force-pushed the feat/lifecycle-signals branch from db49994 to d83bfcc Compare October 11, 2026 18:42
@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Results: reworked on top of #453 as requested

What it does. With --dashboard-lifecycle-signals (default off) the proxy writes one row per request, and one per keep-alive ping, into a new side table request_lifecycle. The row holds only the facts that no requests column already has, so a keep-alive budget can be set from the cause of an idle gap.

What changed since review (following the release-sync plan).

Evidence. go test ./dash ./proxy ./cmd/... ./internal/... ./config pass, gofmt clean. Tests: gap measured from the previous end and per thread; TTL tiers; age eviction keeps live threads and drops stale ones; flag off writes no rows and no state; the normalized-body scan counts only results after the last assistant turn and handles sibling text blocks. An end-to-end test drives two turns through the real handler with the flag on and checks the stored rows (result count, bytes, error flag, gap, granted tier).

Not deployed; deployment is for the owners.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewer: APPROVAL UPDATED for the reworked head d83bfcc (earlier approval was for the old head and no longer applies). CI all pass; full go test ./dash -count=1 (67 s) green; proxy lifecycle/wait tests green; gofmt clean.

Checked: privacy - the row holds only counts, byte lengths, an is_error flag, the gap and a tier label; no names, no tool input or result text (thread id, stop reason, tool names now come from main's own columns). Flag off - LifecycleFn is only set when the flag is on, ObserveLifecycle/PingLifecycle return nil, and TestLifecycleOffWritesNoRows plus TestLifecycleOffAndPing assert no rows. Additive DDL - CREATE TABLE IF NOT EXISTS via additiveDDL, ON DELETE CASCADE from requests (foreign_keys on), empty table, no version bump. Hot path - scanning runs on the dashboard writer goroutine through apply.NormalizeMessages, not on the request path; recover() fails open. Memory - one int64 per live thread, age-based sweep at most once a minute (O(n) amortised, a live thread is never evicted). Scoping - DB.Lifecycles(tenant) joins requests on tenant_id. Tests: end-to-end with secret-bearing tool inputs, gap/TTL, age eviction.

Note (non-blocking): the end time is stamped when the proxy finishes the response and the writer applies events in finish order, so for concurrent requests on one thread the gap can be measured against the later-finishing one; fine for the keep-alive analysis it serves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge Program PR approved by the reviewer, CI green; needs maintainer approval

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants