Skip to content

Costs: a filtered summary reads only the filtered visits' records - #79

Merged
gedaiu merged 1 commit into
mainfrom
cost-query-scoped
Nov 14, 2026
Merged

gedaiu merged 1 commit into
mainfrom
cost-query-scoped

Conversation

@gedaiu

@gedaiu gedaiu commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

GET /assembly-runs/{id} took 2 to 7 seconds because the cost summary aggregated every llm_call record on the floor before filtering to the one run. Every reader of a single run paid it, and Lore's run page asks for a run about ten times per load.

The summary now builds a scoped CTE first (the visits the filter keeps) and the totals, unpriced and billing CTEs read only those visits' records, through the table's primary key. Results are unchanged: the 18 existing cost tests pass untouched, plus one new test with two runs.

Measured on a seeded database (150 runs, 6,000 records), one run's summary:

before after
station_run_records access Seq Scan, 6,000 rows Bitmap Index Scan on the primary key, 40 rows

The /metrics rollups (costSeries, missingCostByLine) are unfiltered by design and their SQL is unchanged.

Closes #52
Related: re-cinq/lore run page speed work

🤖 Generated with Claude Code

The totals, unpriced and billing CTEs aggregated every record the floor
holds before the filter applied, so the cost of one run took seconds and
grew with the floor's history. The filter now picks the visits first
(scoped) and the record CTEs read only those visits by primary key.
@lore-agent

lore-agent Bot commented Nov 14, 2026

Copy link
Copy Markdown

Lore is reviewing this PR — ea22228e-f7cc-46c0-bed2-18c82f5df791.

Submit a review that requests changes to have Lore push a fix for it. Comment @lore review to re-run the review.

@lore-agent lore-agent Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Lore review — Approved

Scopes CostsStore.summary record CTEs to the filtered visits first so GET /assembly-runs/{id} and filtered GET /costs queries read only matching visits' station_run_records.

Must fix (0) · Consider (0) · Nits (0)

Reviewed by gemini-4-argon

Submit a review that requests changes to have Lore push a fix for it. Comment @lore review to re-run the review.

Posted by floor, visit f1728f07-2274-45a1-9293-e408c22a3181.

@gedaiu
gedaiu merged commit b61db57 into main Nov 14, 2026
16 checks passed
@gedaiu
gedaiu deleted the cost-query-scoped branch November 14, 2026 12:19
gedaiu added a commit to re-cinq/lore that referenced this pull request Nov 14, 2026
…ecked in lore-api (#4617)

First half of making the run page, the run list and the Loops board
fast. Measured on production on 2026-11-14: a run page's transcript and
inspector arrive 11 to 26 seconds after the click, because every browser
read goes through a Next proxy that re-reads the whole run (cost
included) only to learn its repo. A read that takes 0.1 s at lore-api
takes 8.4 s through the proxy.

This PR is the lore-api side and changes nothing a person sees yet. The
web UI moves onto it in a second PR, once this one is deployed and
checked.

**What lore-api gains**

- The live socket (`/api/ws`) reads the `lore_session` cookie at the
upgrade and knows the person. A cookie socket from any origin but the
UI's is refused with 403.
- A `request` / `response` message pair: the browser asks for a route by
method and path and lore-api runs it in-process as that person, so the
route's own bearer check, scope, validation and rate limit apply.
`/api/auth/`, `/api/webhook`, `/api/ws` and any path that leaves `/api/`
are answered 404.
- `run` and `runs` channels open without a minted token for a person who
may read the run's repository.
- Run routes (`/api/assembly-runs/{id}/…` and the older
`/api/assembly-lines/{id}/…`) refuse a person whose session does not
list the run's repository. This is the check the Next proxy made, and
the follow-up ADR-053 named.
- `GET /api/assembly-runs/{id}/pull-files`: a run's changed files by run
id, so the browser never names a repo and pull request itself.
- Cancel and run-node take the person from the session, not from the
body.
- One floor read serves a burst: `floor.runs.get` is shared for 3
seconds, so a page's run, nodes and upgrade reads make one floor
request.
- The Loops board's GitHub reads answer the kept value at once for up to
10 minutes while one refresh runs in the background. Cold, they held the
board for 4 to 6 seconds.
- A new ingress entry routes `lore.gcp.re-cinq.com/api/ws` to lore-api.
Nothing uses it until the second PR points `LORE_WS_URL` there.

**Left out on purpose**

- Repo checks on lore-api routes other than the run routes. Over the
socket a person can ask for any route, exactly as their token already
can over HTTP.
- Session tokens still carry the `admin` scope (ADR-053).
- The token-based channel opens stay, for tabs loaded before the second
PR.

**To check after deploy**

- TLS on `lore.gcp.re-cinq.com`: the new ingress is in the `lore-api`
namespace, so cert-manager issues a second certificate for the same
host.

Related: re-cinq/floor#79 (the cost query that made a single run read
slow, released as floor v0.5.2).

🤖 Generated with [Claude Code](https://claude.com/claude-code)
gedaiu added a commit to re-cinq/lore that referenced this pull request Nov 14, 2026
#4622)

Second half of the run page speed work; #4617 was the lore-api side and
is deployed.

The browser's reads on the run page, the run list and the Loops board's
live follower now go over the tab's one live socket straight to
lore-api, as the signed-in person. Before, each read went through a Next
proxy route that re-read the whole run first to check access, which
turned a 0.1 s read into 8 s when the page fired four at once.

**What changes**

- The socket client gains `request(method, path, body)`: it sends a
`request`, resolves on the matching `response`, waits for the connection
when it is not up, sends a pending GET again after a reconnect, rejects
any other method on a drop, and times out after 30 s.
- `liveFetch(path)` wraps that as a `Response`, so callers changed one
line. A 401 sends the person to sign in.
- Every browser caller of `/api/assembly-runs/…` uses it: events,
transcript, bag, definition of done, visit events and model calls, blob
previews, node logs, the diff drawer, and the cancel, run-node and
upgrade buttons.
- `run` and `runs` channels open on the person's session. The server
actions that minted their tokens are gone (the run one took 11 s).
- The run list loads its next page over the socket.
- The run page starts its run, nodes and upgrade reads together.
- `LORE_WS_URL` is the UI's own host, so the session cookie rides the
upgrade. The ingress entry for it shipped in #4617.

**Deleted**

- The 12 Next routes under `app/api/assembly-runs/[id]/` and their
tests, and the helpers only they used.

**Behaviour to know**

- With no live socket configured, the run page shows its server-rendered
graph marked Offline and reads no history: there is no HTTP fallback any
more. The fallback that polled history after the socket gave up now asks
over the socket too, which wakes it again.
- Callers see lore-api's real 401 and 403; the proxy used to turn both
into 502.

**Left out on purpose**

- The other 13 Next proxy routes, the plan page (its channels keep their
minted tokens) and every server-rendered page.

Related: #4617, re-cinq/floor#79

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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.

GET /assembly-runs/{id} takes 2 to 7 seconds per run while the list and the visits take milliseconds

1 participant