Skip to content

Commit 8f006a0

Browse files
committed
fix(acp): buffer split telemetry delimiters, fix preamble search, and reset filters at prompt boundaries
1 parent 1db2682 commit 8f006a0

4 files changed

Lines changed: 177 additions & 28 deletions

File tree

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,12 @@ function messageStreamRoleOf(messageId: MessageId): MessageStreamRole {
297297
return messageId.startsWith(REASONING_MESSAGE_ID_PREFIX) ? "reasoning" : "assistant";
298298
}
299299

300+
/** Ensures an assistant message identifier carries exactly one `assistant:` prefix. */
300301
export function canonicalAssistantMessageId(rawId: string): MessageId {
301302
return MessageId.make(rawId.startsWith("assistant:") ? rawId : `assistant:${rawId}`);
302303
}
303304

305+
/** Formats a unique segment identifier for assistant or reasoning message parts. */
304306
export function assistantSegmentMessageId(
305307
baseKey: string,
306308
segmentIndex: number,
@@ -309,7 +311,9 @@ export function assistantSegmentMessageId(
309311
const prefix = role === "reasoning" ? REASONING_MESSAGE_ID_PREFIX : "assistant:";
310312
const normalizedBaseKey = baseKey.startsWith(prefix) ? baseKey.slice(prefix.length) : baseKey;
311313
return MessageId.make(
312-
segmentIndex === 0 ? `${prefix}${normalizedBaseKey}` : `${prefix}${normalizedBaseKey}:segment:${segmentIndex}`,
314+
segmentIndex === 0
315+
? `${prefix}${normalizedBaseKey}`
316+
: `${prefix}${normalizedBaseKey}:segment:${segmentIndex}`,
313317
);
314318
}
315319

‎apps/server/src/provider/acp/AcpSessionRuntime.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ export interface AcpSessionRuntimeOptions {
100100
/** Transforms provider stdout before protocol parsing and protocol logging. */
101101
readonly transformStdout?: EffectAcpClient.AcpClientOptions["transformStdout"];
102102
/** Normalizes provider-specific fields before notification queues or runtime state retain them. */
103-
readonly transformSessionUpdate?: (
103+
readonly transformSessionUpdate?: ((
104104
notification: EffectAcpSchema.SessionNotification,
105-
) => EffectAcpSchema.SessionNotification;
105+
) => EffectAcpSchema.SessionNotification) & {
106+
readonly reset?: () => void;
107+
};
106108
/** Receives bounded stderr chunks. Redact secrets before logging. A failure closes the runtime. */
107109
readonly onStderr?: (text: string) => Effect.Effect<void, EffectAcpErrors.AcpError>;
108110
readonly requestLogger?: (event: AcpSessionRequestLogEvent) => Effect.Effect<void, never>;
@@ -1032,6 +1034,7 @@ export const make = (
10321034
const started = yield* getStartedState;
10331035
yield* closeActiveAssistantSegment({ queue: eventQueue, assistantSegmentRef });
10341036
yield* Ref.set(assistantUpdatesOpenRef, true);
1037+
options.transformSessionUpdate?.reset?.();
10351038
const requestPayload = {
10361039
sessionId: started.sessionId,
10371040
...payload,
@@ -1081,6 +1084,7 @@ export const make = (
10811084
yield* Fiber.interrupt(activePrompt.fiber).pipe(Effect.ignore);
10821085
yield* Ref.set(activePromptRef, Option.none());
10831086
yield* Deferred.succeed(activePrompt.completed, undefined);
1087+
options.transformSessionUpdate?.reset?.();
10841088
}),
10851089
),
10861090
),

‎apps/server/src/provider/acp/AntigravityProtocol.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,4 +513,60 @@ describe("Antigravity tool results", () => {
513513
expect(filter("\npart 2\nOutput: [mobile] OK\n")).toBe("");
514514
expect(filter("</SYSTEM_MESSAGE> After")).toBe(" After");
515515
});
516+
517+
it("handles closing delimiter split across chunks without dropping the assistant answer", () => {
518+
const filter = createAntigravityMessageFilter();
519+
expect(filter("<SYSTEM_MESSAGE>secret</SYSTEM_MESS")).toBe("");
520+
expect(filter("AGE> real answer")).toBe(" real answer");
521+
});
522+
523+
it("handles opening delimiter split across chunks without leaking telemetry", () => {
524+
const filter = createAntigravityMessageFilter();
525+
expect(filter("Normal text <SYSTEM_")).toBe("Normal text ");
526+
expect(filter("MESSAGE>secret</SYSTEM_MESSAGE> more text")).toBe(" more text");
527+
});
528+
529+
it("handles preamble split across chunks without leaking telemetry", () => {
530+
const filter = createAntigravityMessageFilter();
531+
expect(filter("Answer: The following is a <SYSTEM_")).toBe("Answer: ");
532+
expect(
533+
filter(
534+
"MESSAGE> not actually sent by the user.\n\n<SYSTEM_MESSAGE>secret</SYSTEM_MESSAGE>Done",
535+
),
536+
).toBe("Done");
537+
});
538+
539+
it("discards through newline when preamble has no subsequent opening tag", () => {
540+
const filter = createAntigravityMessageFilter();
541+
expect(
542+
filter(
543+
"The following is a <SYSTEM_MESSAGE> not actually sent by the user\nHere is the answer.",
544+
),
545+
).toBe("Here is the answer.");
546+
});
547+
548+
it("resets filter state at prompt boundary so incomplete prior turns do not swallow text", () => {
549+
const transformer = makeAntigravitySessionUpdateTransformer();
550+
// Prompt 1 ends unexpectedly while inside <SYSTEM_MESSAGE>
551+
transformer({
552+
sessionId: "session-1",
553+
update: {
554+
sessionUpdate: "agent_message_chunk",
555+
content: { type: "text", text: "<SYSTEM_MESSAGE>unclosed secret" },
556+
},
557+
});
558+
559+
// Reset at prompt boundary via user_message_chunk or explicit reset
560+
transformer.reset();
561+
562+
// Prompt 2 assistant text should not be swallowed
563+
const nextResponse = transformer({
564+
sessionId: "session-1",
565+
update: {
566+
sessionUpdate: "agent_message_chunk",
567+
content: { type: "text", text: "Legitimate prompt 2 answer." },
568+
},
569+
});
570+
expect((nextResponse.update as any).content.text).toBe("Legitimate prompt 2 answer.");
571+
});
516572
});

‎apps/server/src/provider/acp/AntigravityProtocol.ts‎

Lines changed: 110 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

373458
function localImagePath(imagePath: string | undefined): string | undefined {
374459
if (!imagePath || imagePath.length > TOOL_TEXT_LIMIT) {

0 commit comments

Comments
 (0)