@@ -24,7 +24,6 @@ import { Bus } from "../bus.js"
2424import { Database } from "../database/database.js"
2525import { llmClient } from "../effect/app-node-platform.js"
2626import { Model } from "../model.js"
27- import { PluginHooks } from "../plugin/hooks.js"
2827import { State } from "../state.js"
2928import { Token } from "../util/token.js"
3029import type { SessionContext } from "./context.js"
@@ -253,7 +252,6 @@ export const layer = Layer.effect(
253252 const models = yield * SessionRunnerModel . Service
254253 const db = ( yield * Database . Service ) . db
255254 const requests = yield * SessionModelRequest . Service
256- const hooks = yield * PluginHooks . Service
257255
258256 const scope = yield * Scope . Scope
259257 const precomputed = new Map < SessionContext . Loaded [ "session" ] [ "id" ] , Precomputed > ( )
@@ -469,13 +467,11 @@ export const layer = Layer.effect(
469467 const headings = SUMMARY_TEMPLATE . split ( "\n" ) . filter ( ( line ) => line . startsWith ( "##" ) )
470468 const filled = ( text : string ) => text . split ( "\n" ) . some ( ( line ) => headings . includes ( line . trim ( ) ) )
471469 const prepared = yield * prepare ( context , messages , budget )
472- // The characters in a conversation overstate its size when the provider drops old reasoning, so the last
473- // response's count sizes the unchanged request instead. A compaction hook can change the request, and an
474- // overflow just proved the count low, so both keep the character estimate.
475- const measured =
476- trigger . reason === "overflow" || ( yield * hooks . has ( "session" , "compaction" , context . model . ref . providerID ) )
477- ? undefined
478- : measuredPrompt ( { ...context , messages } )
470+ // Size the unchanged request with the number that decided to compact: the last response's count plus an
471+ // estimate of what came after it. Characters overstate a conversation whose old reasoning the provider drops.
472+ // A hook can still make this request larger than that count; the provider then rejects it and it is resent as
473+ // text. An overflow already proved the count low, so it keeps the character estimate.
474+ const measured = trigger . reason === "overflow" ? undefined : measuredPrompt ( { ...context , messages } )
479475
480476 // Hooks saw the request without the summary prompt, so it is appended here. A reply that ignores the
481477 // template gets one reminder before it counts as a failure.
@@ -512,7 +508,10 @@ export const layer = Layer.effect(
512508 // which still reuses the cache for that shorter prefix, and as text otherwise. Dropping exchanges to make room
513509 // for the tail would leave them in neither the summary nor the tail. Where 15% of the window is under
514510 // `RESERVE_MIN`, the ceiling equals `summaryRoom`, so automatic compaction always takes this fallback.
515- const fit = trigger . reason === "overflow" ? budget : Math . max ( budget , summaryRoom ( context . model . limit ) )
511+ const fit =
512+ trigger . reason === "overflow"
513+ ? budget
514+ : Math . max ( budget , summaryRoom ( context . model . limit , prepared . request . generation ?. maxTokens ) )
516515 const result = yield * deliver (
517516 trigger ,
518517 prepared ,
@@ -942,16 +941,7 @@ export const layer = Layer.effect(
942941export const node = makeLocationNode ( {
943942 service : Service ,
944943 layer,
945- deps : [
946- Agent . node ,
947- Bus . node ,
948- Database . node ,
949- llmClient ,
950- Model . node ,
951- PluginHooks . node ,
952- SessionModelRequest . node ,
953- SessionRunnerModel . node ,
954- ] ,
944+ deps : [ Agent . node , Bus . node , Database . node , llmClient , Model . node , SessionModelRequest . node , SessionRunnerModel . node ] ,
955945} )
956946
957947/**
@@ -1150,9 +1140,12 @@ const calculateCeiling = (limit: SessionContext.Loaded["model"]["limit"], buffer
11501140 return window - Math . max ( Math . floor ( window * RESERVE_SHARE ) , window >= 2 * RESERVE_MIN ? RESERVE_MIN : 0 )
11511141}
11521142
1153- /** The largest summary request that leaves `RESERVE_MIN` of the window for the summary. Zero for an unknown window. */
1154- const summaryRoom = ( limit : SessionContext . Loaded [ "model" ] [ "limit" ] ) =>
1155- Math . max ( 0 , ( limit . input || limit . context ) - RESERVE_MIN )
1143+ /**
1144+ * The largest summary request that leaves `RESERVE_MIN` of the window for the summary, or less when the request
1145+ * cannot generate that much. Zero for an unknown window.
1146+ */
1147+ const summaryRoom = ( limit : SessionContext . Loaded [ "model" ] [ "limit" ] , output = RESERVE_MIN ) =>
1148+ Math . max ( 0 , ( limit . input || limit . context ) - Math . min ( RESERVE_MIN , output ) )
11561149
11571150/**
11581151 * Another provider's count may describe a native window this model cannot replay. History then holds the original
0 commit comments