diff --git a/apps/server/src/provider/acp/AntigravityChildWork.test.ts b/apps/server/src/provider/acp/AntigravityChildWork.test.ts index 26a3fec67aba..21783cbae1d3 100644 --- a/apps/server/src/provider/acp/AntigravityChildWork.test.ts +++ b/apps/server/src/provider/acp/AntigravityChildWork.test.ts @@ -235,6 +235,31 @@ it.layer(NodeServices.layer)("readAntigravityChildWork", (it) => { expect(child?.steps?.length).toBeLessThan(40); expect(child?.steps?.at(-1)?.text).toBe("marker 4242 found"); expect(child?.steps?.[0]?.text?.length).toBeLessThanOrEqual(4_001); + + const lastLine = JSON.stringify(transcriptLines[3]) + "\n"; + const step = { step_index: 2, type: "VIEW_FILE", content: "" }; + const remaining = 256 * 1024 - Buffer.byteLength(JSON.stringify(step) + "\n" + lastLine); + const firstLine = JSON.stringify({ + ...step, + content: "é".repeat(Math.floor(remaining / 2)) + "x".repeat(remaining % 2), + }); + const aligned = yield* makeProfile({ + steps: autoApproved, + transcript: `${filler}\n${firstLine}\n${lastLine}`, + }); + const [alignedChild] = yield* read(aligned.profile, `${ROOT}:1`); + expect(alignedChild).toMatchObject({ + truncated: true, + steps: [{ index: 2 }, { index: 3 }], + }); + + const paddedLine = firstLine.padEnd(firstLine.length + Buffer.byteLength(lastLine) + 1); + const undelimited = yield* makeProfile({ + steps: autoApproved, + transcript: filler + paddedLine, + }); + const [undelimitedChild] = yield* read(undelimited.profile, `${ROOT}:1`); + expect(undelimitedChild).toMatchObject({ truncated: true, steps: [] }); }), ); diff --git a/apps/server/src/provider/acp/AntigravityChildWork.ts b/apps/server/src/provider/acp/AntigravityChildWork.ts index f016eeeaece5..625062b5368b 100644 --- a/apps/server/src/provider/acp/AntigravityChildWork.ts +++ b/apps/server/src/provider/acp/AntigravityChildWork.ts @@ -405,14 +405,20 @@ async function readChildTranscript( try { const stat = await handle.stat(); if (!stat.isFile()) return undefined; - const length = Math.min(stat.size, byteCap); - const buffer = Buffer.alloc(length); - const { bytesRead } = await handle.read(buffer, 0, length, stat.size - length); - const truncated = stat.size > length; - let contents = buffer.subarray(0, bytesRead).toString("utf8"); - // A tail read starts mid-line. - if (truncated) contents = contents.slice(contents.indexOf("\n") + 1); - return { steps: transcriptSteps(contents, prompt), truncated }; + const tailStart = Math.max(0, stat.size - byteCap); + // One extra byte tells whether the retained tail starts on a line boundary. + const readStart = Math.max(0, tailStart - 1); + const buffer = Buffer.alloc(stat.size - readStart); + const { bytesRead } = await handle.read(buffer, 0, buffer.length, readStart); + const bytes = buffer.subarray(0, bytesRead); + let lineStart = 0; + if (tailStart > 0) { + const newline = bytes.indexOf(0x0a); + if (newline === -1) return { steps: [], truncated: true }; + lineStart = newline + 1; + } + const contents = bytes.toString("utf8", lineStart); + return { steps: transcriptSteps(contents, prompt), truncated: tailStart > 0 }; } finally { await handle.close(); }