Skip to content

fix(dash): price the Inventory tab on the requests that carried each declaration - #531

Merged
amiddavid merged 6 commits into
mainfrom
feat/inventory-carried-pricing
Oct 15, 2026
Merged

amiddavid merged 6 commits into
mainfrom
feat/inventory-carried-pricing

Conversation

@OsherElhadad

Copy link
Copy Markdown
Collaborator

Follow-up to #523. The dashboard's Inventory tab (GET /api/tools, and the suggestions on GET /api/toolfilter) now prices "unused" declarations the same way /api/adapt/tools does.

What changes

  • Before: a declaration was billed on every request of a session that declared it, at the session's first model. Sessions hold several declaration sets (a subagent with a narrower tool list, a server added mid-session) and often two models, so the figure overstated what was carried, most for skills, which subagent sets usually do not carry.
  • Now: each request is priced on the declaration set it carried, at its own model and cache tier: cache read, cache write (1h at 2x) or input. "Unused" keeps its per-session meaning.
  • Requests that recorded no declaration set (rows from before decl_digest) are matched by tool count. Each row and the totals carry unused_usd_low / unused_usd_high bounds, and the headline tile states the range when it matters.
  • carried in the report says how many requests were matched exactly, by tool count, or ambiguously.
  • One pricing core (carriedCost) now serves both /api/adapt/tools and the Inventory tab. It reads tool_declarations in concurrent rowid slices like scanDecls, keeps the sets compact (interned keys), and runs beside the inventory read.

Checked

  • Unit tests:
    • a subagent's set without the tool is not billed;
    • fixtures now record their set;
    • the golden files differ only by the new fields when every request recorded its set, which shows the old and new pricing agree where they should;
    • the race test passes.
  • On copies of real dashboard data, the Inventory figures for never-used items equal /api/adapt/tools to floating-point precision.
  • Performance on a copy of a full dashboard database: the cold whole-service view is slower but well within the route's bound, and peak memory is higher. Single-account views change little.

Not done

  • Keep-alive pings are still not priced (they record no tool count).

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Review: APPROVE at edb9d74. (Posted as a comment: GitHub does not let the author account approve.)

Verified:

  • Same pricing as /api/adapt/tools. carriedCost is the old per-request loop moved, not rewritten. It keeps the same tier rule, including the 5m write at the write rate (bd15bf4) and the 1h write at 2x input, plus the same latest-before set pick by tool count and the same bounds. Each of these mutations fails a test:
    • an off-by-one shard step (11 failures)
    • ntools overwritten instead of summed when a set straddles two slices
    • billing an item in a session that used it
    • dropping the 5m write rate
    • dropping the lower bound
  • Rowid slices. step = (hi-lo)/16 + 1 makes 16 half-open ranges that cover [lo, hi] exactly once; gaps and deletes are harmless. Rows inserted after the MIN/MAX read are left for the next refresh. A set that straddles two slices is merged (tool count summed, items appended, earliest first).
  • Interning. It is per call (local ids/keys), so nothing outlives a request. keyOf is read-only in Inventory, and AdaptTools guards its meta map with a mutex.
  • Per-session "unused". An item used in one session and not in another is billed only on the second session's carrying requests, which is what the tab has always meant. The two endpoints agree only on items that were never used, as the PR says.
  • Concurrency and memory. /api/tools stays behind the response cache (single-flight per scope key, stale-while-revalidate) and the heavy-route bound. The extra memory is small against the service's memory limit, and one fleet-view compute serves every concurrent reader of that key. Accepted.
  • Checks. gofmt and go vet are clean. The dash Tool|Adapt|Golden|Inventory|Suggest|Decl tests pass, including under -race. One run under host IO load failed TestAdaptToolsSkipsWhatIsAlreadyRemoved; it passed 3 runs alone and 2 full group reruns, and the same group passes on the base. The commit is signed off with no AI attribution, and the PR text has no figures.

Non-blocking:

  1. carriedSets (16 slices) runs beside scanDecls (its own concurrent pass), so one cold fleet view runs roughly twice as many SQLite readers at once as before. Different scopes are not globally bounded; that predates this PR for /api/tools. Consider taking the dashboard's heavy slot (acquireKVCache) around the compute, or sharing one slice pass.
  2. The headline range covers mapped requests only. Requests that match no stored set are in neither bound. /api/adapt/tools warns about them, but the Inventory tile does not. A short "N requests not matched" from carried would keep the range honest.
  3. usedBySession keys usage by bare session_id, so in a fleet view two accounts with the same session id would share "used". This predates the PR and is unlikely with UUIDs, but non-Claude-Code agents may reuse ids.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-review: APPROVE at 367302e. (Posted as a comment: GitHub does not let the author account approve.)

  • carriedSets now runs one pass at a time across the process.
  • A narrow scope reads in one query, which removes the per-slice connections behind the SQLITE_BUSY flake. The sharded path is still covered by TestInventoryTablePassEqualsPerSessionProbe.
  • The headline now states how many requests matched no declaration set.
  • gofmt and go vet are clean, and go test -race ./dash -run 'Tool|Adapt|Inventory|Decl' passes. Both commits are signed off with no AI attribution.

Non-blocking: carriedSlot <- struct{}{} does not watch the request context. A tenant view queues behind a cold fleet pass, and a client that has gone keeps waiting for the slot. A select on d.readCtx() or the request's Done() would release it.

Approved.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-review: APPROVE at 277b1c8. The carriedSlot wait now selects on d.readCtx().Done(). That context is the request's when the DB comes from a.db(r) (WithContext), so an abandoned request stops waiting. A shared background refresh keeps waiting, which is correct. Signed off. The approval stands.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Note on 277b1c8: the change is right, but no test pins it. 277b1c8 touches only dash/adapttools.go, and no test references carriedSlot. Please add one: hold carriedSlot, call the carried pricing with a DB whose context is already cancelled (WithContext), and assert it returns context.Canceled promptly (within, say, 1 s) without taking the slot. That test would fail on the old blocking send. Approval stands once it's in.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-review: APPROVE at 37a3519. TestCarriedSetsStopsWaitingWhenCancelled holds carriedSlot and expects a request with a 50 ms deadline to return promptly. It passes, and with the old blocking send restored it hangs until the test timeout. Signed off, no AI attribution. This approval covers 37a3519.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-review: APPROVE at e925f27. This is a test-only change: the cancellation test now uses an already-cancelled context and asserts context.Canceled with the slot still held. It passes, and go vet is clean. Signed off. This approval covers e925f27.

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-confirm: APPROVE at d592326 (rebased onto #528's 9496607; was e925f27). git range-diff d6b3147c..e925f27d 9496607d..d592326a shows 21 of 22 commits identical. The only changed patch is 352d2b7 → 5cdab08, and its sole difference is context lines in cmd/context-guru-proxy/main.go from the squashed base; no code change. CI is green. This approval covers d592326.

@amiddavid
amiddavid force-pushed the feat/adapt-tools branch 2 times, most recently from fd300e3 to 9b489bf Compare October 15, 2026 10:39
@OsherElhadad
OsherElhadad changed the base branch from feat/adapt-tools to main October 15, 2026 10:55
Osher-Elhadad added 6 commits October 15, 2026 13:56
…declaration

The tools report billed a declaration on every request of a session that
declared it, at the session's first model. Sessions hold several
declaration sets (subagents, servers added mid-session) and often two
models, so it overstated what was carried. It now uses the same
per-request pricing as /api/adapt/tools (carriedCost), with low/high
bounds for requests that recorded no declaration set, and the headline
states the range. The pricing pass reads tool_declarations in concurrent
rowid slices like scanDecls and runs beside the inventory read.

Signed-off-by: Osher-Elhadad <[email protected]>
…matched no declaration set

Signed-off-by: Osher-Elhadad <[email protected]>
…en connections

Each slice is a new connection running the connection pragmas, which on a
small database with a background writer waited out the busy timeout. The
sharded path stays for broad scopes and is covered by the existing
table-pass equivalence test.

Signed-off-by: Osher-Elhadad <[email protected]>
@amiddavid
amiddavid force-pushed the feat/inventory-carried-pricing branch from d592326 to 4dcef47 Compare October 15, 2026 10:57
@amiddavid
amiddavid merged commit fbc808f into main Oct 15, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 15, 2026

@OsherElhadad OsherElhadad left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Re-confirm: APPROVE at 4dcef47 (rebased onto main after #523 merged). git range-diff d6b3147c..d592326a origin/main..4dcef47d shows the PR's 6 own commits identical (=). The 16 #523 commits drop out because they are on main now. CI is green (build-test, purego, trivy, DCO). This approval covers 4dcef47.

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants