Skip to content

feat(api): session and route at request start, a replayable in-flight snapshot, rule hit counts - #197

Merged
fylorn merged 1 commit into
mainfrom
feat/live-routing-facts
Sep 25, 2026
Merged

fylorn merged 1 commit into
mainfrom
feat/live-routing-facts

Conversation

@fylorn

@fylorn fylorn commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

The desktop UI's live views and routing map had to guess several facts about requests. Core now reports them.

Session at start

  • RequestStarted.session replaces session_fp: it is the session id the request is stored under (requests.session, SessionView.id).
  • The gateway decides it once, when the request starts (tw_gateway::session::Sessions: same fingerprint and no more than 30 minutes since that conversation's previous request). The tracker lives in AppState and survives config reloads; stale fingerprints are pruned. The recorder stores the event's value and no longer tracks sessions.
  • The fingerprint is computed once per request (it was computed twice: group affinity and the start event). WebSocket upgrades and bodies with nothing to recognise have no session.

Route and rule, from the start

  • Routing phase one finishes before the start, so RequestStarted carries route, rule, group and rewritten_by.
  • RequestRouted carries the final record: route, rule, group, rewritten_by (phase-two rewrites included), denied_by (a phase-two rule that denied the request; the denied hop is the last attempt, with outcome error).
  • RoutingView (stored routing JSON, request detail) has the same fields and is recorded from the start event too, so a request that ends before routing completes still says which route and rule it matched (empty attempts).
  • The engine's Decision.rewritten_by and Outcome2::Proceed { set, rewritten_by } name the rules that added a rewrite.

Requests a rule decided but no upstream received

Previously these produced no events and no row. Now they get RequestStarted (empty provider), RequestRouted with an empty chain, and RequestFailed:

  • denied by a phase-one rule (HTTP and WebSocket upgrades) — source denied
  • every upstream the rule chose cannot serve the request (disabled, out of scope, model not listed)
    A phase-two denial now emits RequestRouted as well (it returned before the event). Such requests now count in /summary requests and failures, like guard denials already did. Requests that fail before a rule decides (auth, model admission, no rule matches) are still not recorded.

In-flight snapshot and live view

  • GET /in-flight returns InFlight { now_ms, requests: [{ id, events }] }: core's clock plus every event the bus has seen for each running request, in order (start, headers, routed, translated, guard records; not QuotaSeen). A late joiner replays them through its normal reducer and computes elapsed time from core timestamps.
  • RunningView (GET /live) adds elapsed_ms (core's monotonic clock), session, route, rule, group, upstream (the attempt that served it, once routed).

Rule hit counts

New read-only GET /summary/routes (Window, default today) → Vec<RouteHits>: per route requests, failed, last_ms, and per rule decided, requests, failed, last_ms. A request counts under its deciding rule, every rule that rewrote it, and a phase-two rule that denied it (once each). Counted from the stored routing data, not from the current config; local answers are excluded.

Compatibility

Tests

Unit tests for the session tracker, engine rewrite bookkeeping, bus snapshot and running view, recorder session/routing, and route_hits; gateway integration tests (tests/routing_facts.rs, tests/ws.rs, tests/endings.rs) for session/route on events, rule denials, unservable choices, phase-two denial and rewrites; control-plane tests for /in-flight, /live and an end-to-end /summary/routes (real gateway, recorder task). The smoke script hits /in-flight, /live and /summary/routes.

cargo fmt --all --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace, the ts feature checks and scripts/smoke.sh pass locally.

🤖 Generated with Claude Code

…ayable in-flight snapshot, rule hit counts

The desktop UI had to guess several things about running requests. Core
now says them:

- RequestStarted carries the session id the request is stored under
  (`session`, replacing the `session_fp` fingerprint). The gateway assigns
  it once, when the request starts (tw_gateway::session::Sessions); the
  recorder stores the event's value instead of recomputing it.
- Phase one of routing is done before the start, so RequestStarted also
  carries `route`, `rule`, `group` and `rewritten_by`. RequestRouted
  carries the final record: `route`, `rule`, `group`, `rewritten_by`
  (phase-two rewrites included) and `denied_by` (a phase-two rule that
  denied the request; the denied hop is the last attempt). RoutingView,
  the stored routing JSON, has the same fields, and is recorded from the
  start event too, so a request that ends before routing finishes still
  says which route and rule it went through.
- Requests a rule decided but that reached no upstream now leave a row:
  denied by a rule (HTTP and WebSocket) or no chosen upstream can serve
  them. They get a start, a RequestRouted with an empty chain, and a
  failure. A phase-two denial now also emits RequestRouted.
- GET /in-flight returns InFlight { now_ms, requests: [{ id, events }] }:
  core's clock and every event the bus has seen for each running request
  (start, headers, routed, translated, guard records), in order, so a
  late joiner can replay them through its normal reducer.
- RunningView (GET /live) gains elapsed_ms (core-computed, monotonic),
  session, route, rule, group and upstream (the hop that served it).
- New GET /summary/routes: per route and per rule, requests matched,
  decided, failed and last hit, from stored routing data. A request counts
  under its deciding rule, every rule that rewrote it, and a phase-two
  rule that denied it.

The engine's Decision and Outcome2::Proceed name the rules that added a
rewrite. The stored routing JSON changed shape, so the request store
SCHEMA is 20. CONTROL_API_VERSION is 21.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 458f2fc into main Sep 25, 2026
4 checks passed
@fylorn
fylorn deleted the feat/live-routing-facts branch September 25, 2026 04:10
@fylorn fylorn mentioned this pull request Sep 25, 2026
fylorn added a commit that referenced this pull request Sep 25, 2026
v0.48.0 was published before the template landed, so its page holds
only GitHub's generated list. release-notes/0.48.0.md summarizes it
from #191-#198 so the page can be rewritten from the template:

- upgrade notes: CONTROL_API_VERSION 21, so ThinkWatch Lite 2026.9.16
  (core 0.47.0, protocol 20) does not connect to it and a server used
  with that app stays on 0.47.0; the request store's schema 20, which
  empties the request history on the first start (and again on the way
  back); the /in-flight shape, RequestStarted.session and the removed
  ChatgptUsage fields;
- session and route on the start event, requests a rule decided
  without an upstream, the replayable /in-flight snapshot and the new
  /live fields, and /summary/routes (#197);
- per-group unpriced and no-usage counts, and security log totals
  (#193);
- the signed-in account on a ChatGPT account upstream (#195);
- releases published from one job, the server guide and the crate
  metadata (#191), and the test port fix (#192).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fylorn added a commit that referenced this pull request Sep 25, 2026
v0.48.0 was published before the template landed, so its page holds
only GitHub's generated list. release-notes/0.48.0.md summarizes it
from #191-#198 so the page can be rewritten from the template:

- upgrade notes: CONTROL_API_VERSION 21, so ThinkWatch Lite 2026.9.16
  (core 0.47.0, protocol 20) does not connect to it and a server used
  with that app stays on 0.47.0; the request store's schema 20, which
  empties the request history on the first start (and again on the way
  back); the /in-flight shape, RequestStarted.session and the removed
  ChatgptUsage fields;
- session and route on the start event, requests a rule decided
  without an upstream, the replayable /in-flight snapshot and the new
  /live fields, and /summary/routes (#197);
- per-group unpriced and no-usage counts, and security log totals
  (#193);
- the signed-in account on a ChatGPT account upstream (#195);
- releases published from one job, the server guide and the crate
  metadata (#191), and the test port fix (#192).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fylorn added a commit that referenced this pull request Sep 25, 2026
…summary (#196)

* ci(release): write the release page from a template, with an English summary

#191 made the publish job write the release once, with GitHub's
generated list of pull requests as its only text and the tag as its
title. A release is now titled "ThinkWatch Core <version>", and its text
has, in order:

- an English summary from release-notes/<version>.md, when that file
  exists;
- a table of the files for each platform;
- the commands that install this version on a Linux server and switch
  an existing installation to it (install.sh --version, twcore upgrade
  --version --restart);
- how to verify a download against its .sha256;
- GitHub's generated list of pull requests.

scripts/release_notes.py builds the text; the publish job fetches the
generated list itself (releases/generate-notes) and hands the finished
text to action-gh-release, which no longer generates anything. The
"only once" rule from #191 stays: a release that already exists keeps
its text. A rehearsal (workflow_dispatch) now writes the text into the
run summary, using the version in Cargo.toml.

scripts/release_notes_test.py checks that the table links exactly the
files in release.yml's FILES list, that the install and upgrade options
exist, and that every file in release-notes/ renders; the Linux CI job
runs it before compiling.

release-notes/0.47.0.md summarizes 0.47.0 from #183-#190; the live
v0.47.0 release page now carries it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* release-notes: summarize 0.48.0

v0.48.0 was published before the template landed, so its page holds
only GitHub's generated list. release-notes/0.48.0.md summarizes it
from #191-#198 so the page can be rewritten from the template:

- upgrade notes: CONTROL_API_VERSION 21, so ThinkWatch Lite 2026.9.16
  (core 0.47.0, protocol 20) does not connect to it and a server used
  with that app stays on 0.47.0; the request store's schema 20, which
  empties the request history on the first start (and again on the way
  back); the /in-flight shape, RequestStarted.session and the removed
  ChatgptUsage fields;
- session and route on the start event, requests a rule decided
  without an upstream, the replayable /in-flight snapshot and the new
  /live fields, and /summary/routes (#197);
- per-group unpriced and no-usage counts, and security log totals
  (#193);
- the signed-in account on a ChatGPT account upstream (#195);
- releases published from one job, the server guide and the crate
  metadata (#191), and the test port fix (#192).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* release-notes(0.48.0): say where the key is kept without naming the keychain

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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