From 2e03db3e961a4875d2cf98a7bc84647bb9784fda Mon Sep 17 00:00:00 2001 From: "bunny.fae.mom" Date: Mon, 16 Nov 2026 04:10:43 -0500 Subject: [PATCH] fix(context): size the last call's rebuild by the input its provider reported The context inspector rebuilds the last turn's request between turns through loop.rebuild_observed, which projected it with no provider numbers, so the observation showed an estimate of a request whose size the provider had reported. session_last_call now passes the turn row's reported input, and the rebuild projects once to learn what that call sent, then again with the reported size for exactly that projection; anything else the strategy sizes (the prompt alone, a candidate fold), and the rows, stay estimated. Other rebuilds (the start-of-turn usage check, cache warming) pass none, as before. Reported by gentle. --- src/albedo/daemon/session_last_call.gleam | 1 + src/albedo/harness/loop.gleam | 55 ++++++++++++++++++----- src/albedo/harness/runtime.gleam | 6 ++- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/albedo/daemon/session_last_call.gleam b/src/albedo/daemon/session_last_call.gleam index 6b58ee4f9..8fe768fec 100644 --- a/src/albedo/daemon/session_last_call.gleam +++ b/src/albedo/daemon/session_last_call.gleam @@ -119,6 +119,7 @@ fn rebuild( state.pin, upstream, list.reverse(inputs), + turn.input_tokens, ), ) #( diff --git a/src/albedo/harness/loop.gleam b/src/albedo/harness/loop.gleam index 4bdeb5057..1bbc3ca8c 100644 --- a/src/albedo/harness/loop.gleam +++ b/src/albedo/harness/loop.gleam @@ -354,6 +354,7 @@ fn restored_usage( state.pin, state.upstream, list.reverse(inputs), + None, ) |> result.replace_error(Nil), ) @@ -398,6 +399,7 @@ fn restored_usage( prompt_text(request_prompt(state)), original, state.upstream.images, + None, ) |> result.replace_error(Nil), ) @@ -2066,12 +2068,14 @@ pub fn rebuild( upstream: extension.Upstream, original: List(transcript.Entry), ) -> Result(#(types.Request, requests.Prefix), String) { - rebuild_observed(host, kernel, model, effort, pin, upstream, original) + rebuild_observed(host, kernel, model, effort, pin, upstream, original, None) |> result.map(fn(rebuilt) { #(rebuilt.0, rebuilt.1) }) } /// `rebuild` with what the strategy observed preparing the request, which an -/// inspector shows beside it. +/// inspector shows beside it. `reported` is the input the provider reported +/// for the request being rebuilt, when the caller knows it: the observation +/// then sizes it by that number instead of an estimate. pub fn rebuild_observed( host: runtime.Runtime, kernel: runtime.Prepared, @@ -2080,21 +2084,48 @@ pub fn rebuild_observed( pin: Pin, upstream: extension.Upstream, original_entries: List(transcript.Entry), + reported: Option(Int), ) -> Result( #(types.Request, requests.Prefix, Option(compaction.Observation)), String, ) { let original = list.map(original_entries, fn(entry) { entry.input }) - use prepared <- result.map(runtime.project_view_scoped( - host, - kernel, - model, - request_source(upstream, model), - extension.clean_endpoint(upstream.endpoint), - prompt_text(prompt_for(pin, kernel)), - original, - upstream.images, - )) + let project = fn(sizing) { + runtime.project_view_scoped( + host, + kernel, + model, + request_source(upstream, model), + extension.clean_endpoint(upstream.endpoint), + prompt_text(prompt_for(pin, kernel)), + original, + upstream.images, + sizing, + ) + } + use prepared <- result.try(project(None)) + // The reported input measured the projection that call sent, and nothing + // else a strategy sizes while projecting (the prompt alone, a candidate + // fold): those stay estimated, so projecting again sizes them apart. + use prepared <- result.map(case reported { + None -> Ok(prepared) + Some(tokens) -> { + let estimated = + compaction.estimated_sizing( + size.Route(upstream.endpoint, model), + prompt_text(prompt_for(pin, kernel)), + runtime.tools(kernel), + ) + project(Some( + compaction.Sizing(..estimated, request: fn(inputs) { + case inputs == prepared.inputs { + True -> size.total([size.Measured(tokens, size.ReportedUsage)]) + False -> estimated.request(inputs) + } + }), + )) + } + }) let history = prepared.inputs let prompt = prompt_for(settled(pin, original, history), kernel) let request = diff --git a/src/albedo/harness/runtime.gleam b/src/albedo/harness/runtime.gleam index 7c504f645..a36896ca2 100644 --- a/src/albedo/harness/runtime.gleam +++ b/src/albedo/harness/runtime.gleam @@ -2649,7 +2649,8 @@ pub fn prepare_view_scoped( /// The request the active strategy's saved state makes of `history`, the /// way `prepare_view_scoped` would send it, except that it never compacts: -/// it writes nothing and calls no model. +/// it writes nothing and calls no model. `sizing` is None for a caller with +/// no provider numbers, whose sizes are then all estimated. pub fn project_view_scoped( runtime: Runtime, prepared: Prepared, @@ -2659,6 +2660,7 @@ pub fn project_view_scoped( instructions: String, history: List(types.Input), images: types.ImageLimits, + sizing: Option(compaction.Sizing), ) -> Result(compaction.Prepared, String) { view_scoped( runtime, @@ -2674,7 +2676,7 @@ pub fn project_view_scoped( fn(_) { Nil }, compaction.unprojected, fn() { False }, - None, + sizing, compaction.project, ) } -- knotmirror 0.0.1