@@ -234,33 +234,77 @@ export function sanitizeAntigravityToolPayload(payload: unknown): unknown {
234234 return sanitizeToolValue ( payload , { nodes : 512 , text : 64_000 } , 0 ) ;
235235}
236236
237+ const OPEN_SYSTEM_MESSAGE_TAG = "<SYSTEM_MESSAGE>" ;
238+ const CLOSE_SYSTEM_MESSAGE_TAG = "</SYSTEM_MESSAGE>" ;
239+ const SYSTEM_MESSAGE_PREAMBLE = "The following is a <SYSTEM_MESSAGE> not actually sent by the user" ;
240+
241+ /**
242+ * Finds the length of the longest trailing suffix of `str` that matches
243+ * a non-empty prefix of any candidate string.
244+ */
245+ function findLongestCandidateSuffix ( str : string , candidates : readonly string [ ] ) : number {
246+ let longest = 0 ;
247+ for ( const candidate of candidates ) {
248+ const maxLen = Math . min ( str . length , candidate . length - 1 ) ;
249+ for ( let len = maxLen ; len > longest ; len -- ) {
250+ if ( str . endsWith ( candidate . slice ( 0 , len ) ) ) {
251+ longest = len ;
252+ break ;
253+ }
254+ }
255+ }
256+ return longest ;
257+ }
258+
259+ export interface AntigravityMessageFilter {
260+ ( text : string ) : string ;
261+ reset : ( ) => void ;
262+ }
263+
237264/**
238265 * Stateful streaming filter that discards internal Antigravity harness system messages
239266 * (e.g. `<SYSTEM_MESSAGE>...</SYSTEM_MESSAGE>` and the system preamble) that the model
240267 * may mistakenly echo back into its assistant message or thought stream.
268+ *
269+ * Partial opening tags, closing tags, and preamble markers are buffered across chunks
270+ * so split delimiters do not leak telemetry or swallow assistant answers.
241271 */
242- export function createAntigravityMessageFilter ( ) : ( text : string ) => string {
272+ export function createAntigravityMessageFilter ( ) : AntigravityMessageFilter {
243273 let inSystemMessage = false ;
274+ let pending = "" ;
275+
276+ const reset = ( ) : void => {
277+ inSystemMessage = false ;
278+ pending = "" ;
279+ } ;
280+
281+ const filter = ( text : string ) : string => {
282+ const textToProcess = pending + text ;
283+ pending = "" ;
244284
245- return ( text : string ) : string => {
246285 let result = "" ;
247286 let cursor = 0 ;
248287
249- while ( cursor < text . length ) {
288+ while ( cursor < textToProcess . length ) {
250289 if ( inSystemMessage ) {
251- const closeTagIndex = text . indexOf ( "</SYSTEM_MESSAGE>" , cursor ) ;
290+ const closeTagIndex = textToProcess . indexOf ( CLOSE_SYSTEM_MESSAGE_TAG , cursor ) ;
252291 if ( closeTagIndex === - 1 ) {
253- // Entire remainder of text is inside <SYSTEM_MESSAGE>
292+ // Entire remainder of text is inside <SYSTEM_MESSAGE>, except possibly
293+ // a trailing partial close tag that might be completed in the next chunk.
294+ const remainder = textToProcess . slice ( cursor ) ;
295+ const suffixLen = findLongestCandidateSuffix ( remainder , [ CLOSE_SYSTEM_MESSAGE_TAG ] ) ;
296+ if ( suffixLen > 0 ) {
297+ pending = remainder . slice ( remainder . length - suffixLen ) ;
298+ }
254299 break ;
255300 }
256- cursor = closeTagIndex + "</SYSTEM_MESSAGE>" . length ;
301+ cursor = closeTagIndex + CLOSE_SYSTEM_MESSAGE_TAG . length ;
257302 inSystemMessage = false ;
258303 continue ;
259304 }
260305
261- const preambleText = "The following is a <SYSTEM_MESSAGE> not actually sent by the user" ;
262- const preambleIndex = text . indexOf ( preambleText , cursor ) ;
263- const openTagIndex = text . indexOf ( "<SYSTEM_MESSAGE>" , cursor ) ;
306+ const preambleIndex = textToProcess . indexOf ( SYSTEM_MESSAGE_PREAMBLE , cursor ) ;
307+ const openTagIndex = textToProcess . indexOf ( OPEN_SYSTEM_MESSAGE_TAG , cursor ) ;
264308
265309 let nextIndex = - 1 ;
266310 let isPreamble = false ;
@@ -280,46 +324,85 @@ export function createAntigravityMessageFilter(): (text: string) => string {
280324 }
281325
282326 if ( nextIndex === - 1 ) {
283- result += text . slice ( cursor ) ;
327+ const remainder = textToProcess . slice ( cursor ) ;
328+ const suffixLen = findLongestCandidateSuffix ( remainder , [
329+ OPEN_SYSTEM_MESSAGE_TAG ,
330+ SYSTEM_MESSAGE_PREAMBLE ,
331+ ] ) ;
332+ if ( suffixLen > 0 ) {
333+ result += remainder . slice ( 0 , remainder . length - suffixLen ) ;
334+ pending = remainder . slice ( remainder . length - suffixLen ) ;
335+ } else {
336+ result += remainder ;
337+ }
284338 break ;
285339 }
286340
287- result += text . slice ( cursor , nextIndex ) ;
341+ result += textToProcess . slice ( cursor , nextIndex ) ;
288342
289343 if ( isPreamble ) {
290- const afterPreamble = text . indexOf ( "<SYSTEM_MESSAGE>" , nextIndex ) ;
344+ const afterPreamble = textToProcess . indexOf (
345+ OPEN_SYSTEM_MESSAGE_TAG ,
346+ nextIndex + SYSTEM_MESSAGE_PREAMBLE . length ,
347+ ) ;
291348 if ( afterPreamble !== - 1 ) {
292- cursor = afterPreamble + "<SYSTEM_MESSAGE>" . length ;
349+ cursor = afterPreamble + OPEN_SYSTEM_MESSAGE_TAG . length ;
293350 inSystemMessage = true ;
294351 } else {
295- const nextNewline = text . indexOf ( "\n" , nextIndex ) ;
352+ const nextNewline = textToProcess . indexOf ( "\n" , nextIndex ) ;
296353 if ( nextNewline !== - 1 ) {
297354 cursor = nextNewline + 1 ;
298355 } else {
299- cursor = text . length ;
356+ cursor = textToProcess . length ;
300357 }
301358 }
302359 } else {
303- cursor = nextIndex + "<SYSTEM_MESSAGE>" . length ;
360+ cursor = nextIndex + OPEN_SYSTEM_MESSAGE_TAG . length ;
304361 inSystemMessage = true ;
305362 }
306363 }
307364
308365 return result ;
309366 } ;
367+
368+ filter . reset = reset ;
369+ return filter ;
310370}
311371
312- export function makeAntigravitySessionUpdateTransformer ( ) : (
313- notification : EffectAcpSchema . SessionNotification ,
314- ) => EffectAcpSchema . SessionNotification {
372+ export interface AntigravitySessionUpdateTransformer {
373+ ( notification : EffectAcpSchema . SessionNotification ) : EffectAcpSchema . SessionNotification ;
374+ reset : ( ) => void ;
375+ }
376+
377+ /**
378+ * Creates a stateful session update transformer that sanitizes Antigravity tool payloads
379+ * and filters leaked system telemetry from assistant message and thought streams.
380+ * Resets per-channel streaming filters at prompt boundaries.
381+ */
382+ export function makeAntigravitySessionUpdateTransformer ( ) : AntigravitySessionUpdateTransformer {
315383 const filterMessage = createAntigravityMessageFilter ( ) ;
316384 const filterThought = createAntigravityMessageFilter ( ) ;
317385
318- return ( notification : EffectAcpSchema . SessionNotification ) : EffectAcpSchema . SessionNotification => {
386+ const reset = ( ) : void => {
387+ filterMessage . reset ( ) ;
388+ filterThought . reset ( ) ;
389+ } ;
390+
391+ const transform = (
392+ notification : EffectAcpSchema . SessionNotification ,
393+ ) : EffectAcpSchema . SessionNotification => {
319394 const update = notification . update ;
320- if ( update . sessionUpdate === "agent_message_chunk" || update . sessionUpdate === "agent_thought_chunk" ) {
395+ if ( update . sessionUpdate === "user_message_chunk" ) {
396+ reset ( ) ;
397+ return notification ;
398+ }
399+ if (
400+ update . sessionUpdate === "agent_message_chunk" ||
401+ update . sessionUpdate === "agent_thought_chunk"
402+ ) {
321403 if ( update . content . type === "text" ) {
322- const filter = update . sessionUpdate === "agent_message_chunk" ? filterMessage : filterThought ;
404+ const filter =
405+ update . sessionUpdate === "agent_message_chunk" ? filterMessage : filterThought ;
323406 const filtered = filter ( update . content . text ) ;
324407 if ( filtered !== update . content . text ) {
325408 return {
@@ -363,12 +446,14 @@ export function makeAntigravitySessionUpdateTransformer(): (
363446 } ,
364447 } ;
365448 } ;
449+
450+ transform . reset = reset ;
451+ return transform ;
366452}
367453
368454/** The runtime uses this before it retains tool state or dispatches raw callbacks. */
369- export const normalizeAntigravitySessionUpdate : (
370- notification : EffectAcpSchema . SessionNotification ,
371- ) => EffectAcpSchema . SessionNotification = makeAntigravitySessionUpdateTransformer ( ) ;
455+ export const normalizeAntigravitySessionUpdate : AntigravitySessionUpdateTransformer =
456+ makeAntigravitySessionUpdateTransformer ( ) ;
372457
373458function localImagePath ( imagePath : string | undefined ) : string | undefined {
374459 if ( ! imagePath || imagePath . length > TOOL_TEXT_LIMIT ) {
0 commit comments