@@ -16,8 +16,8 @@ import * as Ref from "effect/Ref";
1616
1717import type { McpProviderSessionConfig } from "./mcpSession.ts" ;
1818
19- // Agents that report results never take their pages, so only the most recent
20- // pages are kept. A page is taken as soon as its call completes.
19+ // Agents that report results never take their pages, so only each thread's
20+ // most recent pages are kept. A page is taken as soon as its call completes.
2121const MAX_KEPT_HTML_RENDERS = 64 ;
2222
2323// Antigravity bounds each long string of a call's input to its last 8,000
@@ -29,7 +29,6 @@ const BOUNDED_TEXT_MARKER = "[Earlier output truncated]\n\n";
2929const KEPT_HTML_END_LENGTH = 8_000 ;
3030
3131interface KeptHtmlRender {
32- readonly threadId : ThreadId ;
3332 readonly title : string ;
3433 readonly height : number ;
3534 readonly htmlLength : number ;
@@ -82,7 +81,6 @@ export class McpProviderSessions extends Context.Service<
8281/** The one kept page these arguments match, or -1 when they match none or several. */
8382const matchingPageIndex = (
8483 kept : ReadonlyArray < KeptHtmlRender > ,
85- threadId : ThreadId ,
8684 input : { readonly html ?: unknown ; readonly title ?: unknown ; readonly height ?: unknown } ,
8785) : number => {
8886 const html = input . html ;
@@ -95,20 +93,15 @@ const matchingPageIndex = (
9593 ? Number ( input . height )
9694 : input . height ;
9795 const matches = kept . flatMap ( ( page , index ) =>
98- page . threadId === threadId &&
99- page . title === input . title &&
100- page . height === height &&
101- isPageHtml ( page , html )
102- ? [ index ]
103- : [ ] ,
96+ page . title === input . title && page . height === height && isPageHtml ( page , html ) ? [ index ] : [ ] ,
10497 ) ;
10598 // Pages the arguments cannot tell apart could belong to any of their calls.
10699 return matches . length === 1 ? matches [ 0 ] ! : - 1 ;
107100} ;
108101
109102const make = Effect . gen ( function * ( ) {
110103 const sessions = yield * Ref . make ( new Map < ThreadId , McpProviderSessionConfig > ( ) ) ;
111- const htmlRenders = yield * Ref . make < ReadonlyArray < KeptHtmlRender > > ( [ ] ) ;
104+ const htmlRenders = yield * Ref . make ( new Map < ThreadId , ReadonlyArray < KeptHtmlRender > > ( ) ) ;
112105 return McpProviderSessions . of ( {
113106 set : ( config ) =>
114107 Ref . update ( sessions , ( current ) => new Map ( current ) . set ( config . threadId , config ) ) ,
@@ -120,36 +113,47 @@ const make = Effect.gen(function* () {
120113 return next ;
121114 } ) . pipe (
122115 Effect . andThen (
123- Ref . update ( htmlRenders , ( kept ) => kept . filter ( ( page ) => page . threadId !== threadId ) ) ,
116+ Ref . update ( htmlRenders , ( current ) => {
117+ const next = new Map ( current ) ;
118+ next . delete ( threadId ) ;
119+ return next ;
120+ } ) ,
124121 ) ,
125122 ) ,
126123 recordHtmlRender : ( threadId , input , reference ) =>
127- Ref . update ( htmlRenders , ( kept ) =>
128- [
129- ...kept ,
130- {
131- threadId,
132- title : input . title ,
133- height : input . height ,
134- htmlLength : input . html . length ,
135- htmlEnd : copyText ( input . html . slice ( - KEPT_HTML_END_LENGTH ) ) ,
136- reference,
137- } ,
138- ] . slice ( - MAX_KEPT_HTML_RENDERS ) ,
124+ Ref . update ( htmlRenders , ( current ) =>
125+ new Map ( current ) . set (
126+ threadId ,
127+ [
128+ ...( current . get ( threadId ) ?? [ ] ) ,
129+ {
130+ title : input . title ,
131+ height : input . height ,
132+ htmlLength : input . html . length ,
133+ htmlEnd : copyText ( input . html . slice ( - KEPT_HTML_END_LENGTH ) ) ,
134+ reference,
135+ } ,
136+ ] . slice ( - MAX_KEPT_HTML_RENDERS ) ,
137+ ) ,
139138 ) ,
140139 takeHtmlRender : ( threadId , input , reported ) =>
141- Ref . modify ( htmlRenders , ( kept ) => {
140+ Ref . modify ( htmlRenders , ( current ) => {
141+ const kept = current . get ( threadId ) ?? [ ] ;
142142 const index =
143143 reported === undefined
144- ? matchingPageIndex ( kept , threadId , input )
145- : kept . findIndex (
146- ( page ) =>
147- page . threadId === threadId &&
148- page . reference . attachmentId === reported . attachmentId ,
149- ) ;
150- return index === - 1
151- ? [ undefined , kept ]
152- : [ kept [ index ] ! . reference , kept . filter ( ( _ , keptIndex ) => keptIndex !== index ) ] ;
144+ ? matchingPageIndex ( kept , input )
145+ : kept . findIndex ( ( page ) => page . reference . attachmentId === reported . attachmentId ) ;
146+ if ( index === - 1 ) {
147+ return [ undefined , current ] ;
148+ }
149+ const rest = kept . filter ( ( _ , keptIndex ) => keptIndex !== index ) ;
150+ const next = new Map ( current ) ;
151+ if ( rest . length === 0 ) {
152+ next . delete ( threadId ) ;
153+ } else {
154+ next . set ( threadId , rest ) ;
155+ }
156+ return [ kept [ index ] ! . reference , next ] ;
153157 } ) ,
154158 } ) ;
155159} ) ;
0 commit comments