Skip to content

Commit 4938a3c

Browse files
committed
fix(grok): preserve bounded tool content ordering
1 parent ee6c9d6 commit 4938a3c

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

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

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,9 @@ describe("AcpRuntimeModel", () => {
496496
expect(finalDetail?.endsWith(`frame 999: ${"#".repeat(50)}`)).toBe(true);
497497
});
498498

499-
it("keeps non-text tool call content entries when bounding an oversized text entry", () => {
500-
const hugeText = "y".repeat(50_000);
499+
it("keeps non-text tool call content entries in order when bounding oversized text", () => {
500+
const hugePrefix = "x".repeat(25_000);
501+
const hugeTail = "y".repeat(25_000);
501502
const { events } = parseSessionUpdateEvent({
502503
sessionId: "session-1",
503504
update: {
@@ -506,8 +507,11 @@ describe("AcpRuntimeModel", () => {
506507
kind: "edit",
507508
status: "in_progress",
508509
content: [
509-
{ type: "content", content: { type: "text", text: hugeText } },
510+
{ type: "content", content: { type: "text", text: hugePrefix } },
510511
{ type: "diff", path: "/repo/file.ts", oldText: "before", newText: "after" },
512+
{ type: "content", content: { type: "text", text: hugeTail } },
513+
{ type: "diff", path: "/repo/other.ts", oldText: "old", newText: "new" },
514+
{ type: "content", content: { type: "text", text: " " } },
511515
],
512516
},
513517
} satisfies EffectAcpSchema.SessionNotification);
@@ -517,18 +521,25 @@ describe("AcpRuntimeModel", () => {
517521
throw new Error("expected a ToolCallUpdated event");
518522
}
519523
const content = event.toolCall.data.content as ReadonlyArray<EffectAcpSchema.ToolCallContent>;
520-
expect(content).toHaveLength(2);
521-
expect(content[1]).toEqual({
524+
expect(content).toHaveLength(3);
525+
expect(content[0]).toEqual({
522526
type: "diff",
523527
path: "/repo/file.ts",
524528
oldText: "before",
525529
newText: "after",
526530
});
527-
const firstEntry = content[0];
528-
if (firstEntry?.type !== "content" || firstEntry.content.type !== "text") {
531+
const lastEntry = content[1];
532+
if (lastEntry?.type !== "content" || lastEntry.content.type !== "text") {
529533
throw new Error("expected a bounded text entry");
530534
}
531-
expect(firstEntry.content.text.length).toBeLessThan(8_100);
535+
expect(lastEntry.content.text.length).toBeLessThan(8_100);
536+
expect(lastEntry.content.text.endsWith(hugeTail.slice(-100))).toBe(true);
537+
expect(content[2]).toEqual({
538+
type: "diff",
539+
path: "/repo/other.ts",
540+
oldText: "old",
541+
newText: "new",
542+
});
532543
});
533544

534545
describe("decideToolCallUpdateEmission", () => {
@@ -611,5 +622,35 @@ describe("AcpRuntimeModel", () => {
611622
const emittedIndices = emissions.flatMap((emitted, index) => (emitted ? [index + 1] : []));
612623
expect(emittedIndices).toEqual([1, 11]);
613624
});
625+
626+
it("retains the latest replacement snapshot when equal-length updates are coalesced", () => {
627+
let previous: AcpToolCallState = toolCall("frame-a", "inProgress");
628+
const lastEmittedDetailLength = previous.detail?.length;
629+
let skippedSinceEmit = 0;
630+
631+
for (const detail of ["frame-b", "frame-c"]) {
632+
const next = mergeToolCallState(previous, toolCall(detail, "inProgress"));
633+
const decision = decideToolCallUpdateEmission({
634+
previous,
635+
next,
636+
lastEmittedDetailLength,
637+
skippedSinceEmit,
638+
});
639+
expect(decision.emit).toBe(false);
640+
skippedSinceEmit = decision.skippedSinceEmit;
641+
previous = next;
642+
}
643+
644+
const completed = mergeToolCallState(previous, toolCall(undefined, "completed"));
645+
expect(completed.detail).toBe("frame-c");
646+
expect(
647+
decideToolCallUpdateEmission({
648+
previous,
649+
next: completed,
650+
lastEmittedDetailLength,
651+
skippedSinceEmit,
652+
}),
653+
).toEqual({ emit: true, skippedSinceEmit: 0 });
654+
});
614655
});
615656
});

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,21 @@ function extractTextContentFromToolCallContent(
337337
return { text: joined, content };
338338
}
339339
const bounded = boundToolCallOutputText(joined);
340-
// Collapse the text entries into a single bounded one, in place, and leave every other
341-
// entry kind (diffs, images, resource links) alone so non-terminal tool calls from other
342-
// ACP agents keep rendering everything they sent.
343-
let collapsed = false;
344-
const boundedContent = content.flatMap((entry) => {
340+
// Collapse the text entries into a single bounded one at the final contributing text entry,
341+
// and leave every other entry kind (diffs, images, resource links) in its original relative
342+
// order. The retained tail came from that text entry, so placing it there also preserves its
343+
// ordering relative to interleaved non-text content and ignores later blank text entries.
344+
const lastContributingTextIndex = content.reduce(
345+
(lastIndex, entry, index) => (toolCallContentText(entry)?.trim() ? index : lastIndex),
346+
-1,
347+
);
348+
const boundedContent = content.flatMap((entry, index) => {
345349
if (toolCallContentText(entry) === undefined) {
346350
return [entry];
347351
}
348-
if (collapsed) {
352+
if (index !== lastContributingTextIndex) {
349353
return [];
350354
}
351-
collapsed = true;
352355
return [{ type: "content", content: { type: "text", text: bounded } } as const];
353356
});
354357
return { text: bounded, content: boundedContent };

0 commit comments

Comments
 (0)