Skip to content

Commit dbec3e2

Browse files
committed
fix(server): persist tool updates whose visible output changes
Compare visible output text instead of its length, so same-length replacements and string rawOutput persist. Count every skipped update, including rawInput-only ones, toward the every-10th write. This replaces the reuse of decideToolCallUpdateEmission with a V2 rule.
1 parent e806fcf commit dbec3e2

3 files changed

Lines changed: 68 additions & 29 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,15 @@ describe("AcpAdapterV2", () => {
10831083
},
10841084
...streamedWrite("root-write"),
10851085
...streamedWrite("child-write", "child-a"),
1086+
// Same-length output replacements are still live output.
1087+
...Array.from({ length: 5 }, (_, index) => ({
1088+
sessionUpdate: "tool_call_update" as const,
1089+
toolCallId: "ticker",
1090+
title: "Watch ticks",
1091+
kind: "execute" as const,
1092+
status: "in_progress" as const,
1093+
rawOutput: `tick ${index + 1}`,
1094+
})),
10861095
{
10871096
sessionUpdate: "tool_call_update",
10881097
toolCallId: "child-a",
@@ -1139,11 +1148,18 @@ describe("AcpAdapterV2", () => {
11391148
).flatMap((event) => (event.type === "turn_item.updated" ? [event.turnItem] : []));
11401149
for (const toolCallId of ["root-write", "child-write"]) {
11411150
const writes = items.filter((item) => item.nativeItemRef?.nativeId?.endsWith(toolCallId));
1142-
assert.isAtLeast(writes.length, 2, toolCallId);
1151+
assert.isAtLeast(writes.length, chunks / 10, toolCallId);
11431152
assert.isAtMost(writes.length, chunks / 5, toolCallId);
11441153
assert.equal(writes.at(-1)?.status, "completed", toolCallId);
11451154
assert.include(JSON.stringify(writes.at(-1)), "END_OF_FILE", toolCallId);
11461155
}
1156+
const ticks = items.filter((item) => item.nativeItemRef?.nativeId?.endsWith("ticker"));
1157+
for (let tick = 1; tick <= 5; tick++) {
1158+
assert.isTrue(
1159+
ticks.some((item) => JSON.stringify(item).includes(`tick ${tick}`)),
1160+
`tick ${tick} must persist`,
1161+
);
1162+
}
11471163
}).pipe(Effect.provide(layerTest), Effect.scoped),
11481164
);
11491165

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts‎

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,12 @@ import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
6868
import {
6969
applyAcpAgentTerminalUpdate,
7070
acpContentBlockDisplayText,
71-
decideToolCallUpdateEmission,
7271
embeddedTerminalIdsFromSessionUpdate,
7372
extractMcpToolCallIdentity,
7473
mergeToolCallState,
7574
parsePermissionRequest,
7675
parseSessionUpdateEvent,
77-
toolCallProgressLength,
76+
toolCallVisibleOutputChanged,
7877
type AcpPlanUpdate,
7978
type AcpAgentTerminalState,
8079
type AcpSessionModeState,
@@ -1164,11 +1163,8 @@ interface ActiveAcpTurn {
11641163
contextUsage: ThreadTokenUsageSnapshot | null;
11651164
nativeMetadata: OrchestrationV2ProviderThreadNativeMetadata | null;
11661165
readonly tools: Map<string, AcpToolCallState>;
1167-
/** Streamed tool updates held back from persistence; see `decideToolCallUpdateEmission`. */
1168-
readonly toolEmissions: Map<
1169-
string,
1170-
{ readonly lastEmittedDetailLength: number | undefined; readonly skippedSinceEmit: number }
1171-
>;
1166+
/** Streamed tool updates skipped since the last persisted one; see `shouldPersistToolUpdate`. */
1167+
readonly toolUpdatesSkipped: Map<string, number>;
11721168
readonly toolStartedAt: Map<string, DateTime.Utc>;
11731169
readonly subagents: Map<string, ActiveAcpSubagent>;
11741170
readonly subagentsBySessionId: Map<string, ActiveAcpSubagent>;
@@ -1467,11 +1463,13 @@ interface SnapshotMessageState {
14671463
loadingIndex: number;
14681464
}
14691465

1466+
const TOOL_UPDATE_PERSIST_EVERY = 10;
1467+
14701468
/**
14711469
* Some agents stream a tool's arguments (a file write's diff, `rawInput`) and
1472-
* resend the whole call each time. Persist those with #7279's V1 rule. Output
1473-
* the user watches live (command stdout, monitor ticks) and the agent's own
1474-
* completed/failed always persist, even when a flavor normalizes the status.
1470+
* resend the whole call each time. Persist every 10th of those. Status, title,
1471+
* and output the user watches live always persist, as does the agent's own
1472+
* completed/failed when a flavor normalizes it to a non-terminal status.
14751473
*/
14761474
function shouldPersistToolUpdate(
14771475
context: ActiveAcpTurn,
@@ -1480,25 +1478,17 @@ function shouldPersistToolUpdate(
14801478
next: AcpToolCallState,
14811479
reportedStatus: AcpToolCallState["status"],
14821480
): boolean {
1483-
const emission = context.toolEmissions.get(key);
1484-
const progressLength = toolCallProgressLength(next);
1485-
const decision =
1481+
const skipped = context.toolUpdatesSkipped.get(key) ?? 0;
1482+
const persist =
14861483
reportedStatus === "completed" ||
14871484
reportedStatus === "failed" ||
1488-
emission === undefined ||
1489-
progressLength !== emission.lastEmittedDetailLength
1490-
? { emit: true, skippedSinceEmit: 0 }
1491-
: decideToolCallUpdateEmission({
1492-
previous,
1493-
next,
1494-
lastEmittedDetailLength: emission.lastEmittedDetailLength,
1495-
skippedSinceEmit: emission.skippedSinceEmit,
1496-
});
1497-
context.toolEmissions.set(key, {
1498-
lastEmittedDetailLength: decision.emit ? progressLength : emission?.lastEmittedDetailLength,
1499-
skippedSinceEmit: decision.skippedSinceEmit,
1500-
});
1501-
return decision.emit;
1485+
previous === undefined ||
1486+
previous.status !== next.status ||
1487+
previous.title !== next.title ||
1488+
toolCallVisibleOutputChanged(previous, next) ||
1489+
skipped + 1 >= TOOL_UPDATE_PERSIST_EVERY;
1490+
context.toolUpdatesSkipped.set(key, persist ? 0 : skipped + 1);
1491+
return persist;
15021492
}
15031493

15041494
export function makeAcpAdapterV2(
@@ -7010,7 +7000,7 @@ export function makeAcpAdapterV2(
70107000
contextUsage: rememberedContextUsage ?? turnInput.providerThread.contextUsage ?? null,
70117001
nativeMetadata: initialNativeMetadata,
70127002
tools: new Map(),
7013-
toolEmissions: new Map(),
7003+
toolUpdatesSkipped: new Map(),
70147004
toolStartedAt: new Map(),
70157005
subagents: new Map(),
70167006
subagentsBySessionId: new Map(),

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,39 @@ export function toolCallProgressLength(state: AcpToolCallState): number {
10001000
return Math.max(state.detail?.length ?? 0, contentChars, rawOutputChars);
10011001
}
10021002

1003+
function toolCallVisibleOutput(state: AcpToolCallState): ReadonlyArray<string | undefined> {
1004+
const texts: Array<string | undefined> = [state.detail];
1005+
const content = state.data.content;
1006+
if (Array.isArray(content)) {
1007+
for (const entry of content) {
1008+
texts.push(
1009+
isRecord(entry) ? toolCallContentText(entry as EffectAcpSchema.ToolCallContent) : undefined,
1010+
);
1011+
}
1012+
}
1013+
const rawOutput = state.data.rawOutput;
1014+
if (typeof rawOutput === "string") {
1015+
texts.push(rawOutput);
1016+
} else if (isRecord(rawOutput)) {
1017+
for (const field of RAW_OUTPUT_TEXT_FIELDS) {
1018+
const value = rawOutput[field];
1019+
texts.push(typeof value === "string" ? value : undefined);
1020+
}
1021+
}
1022+
return texts;
1023+
}
1024+
1025+
// The text a user watches: detail, text content, and raw output text. A
1026+
// streamed diff or `rawInput` is not part of it.
1027+
export function toolCallVisibleOutputChanged(
1028+
previous: AcpToolCallState,
1029+
next: AcpToolCallState,
1030+
): boolean {
1031+
const before = toolCallVisibleOutput(previous);
1032+
const after = toolCallVisibleOutput(next);
1033+
return before.length !== after.length || before.some((text, index) => text !== after[index]);
1034+
}
1035+
10031036
export function decideToolCallUpdateEmission(
10041037
input: AcpToolCallEmitDecisionInput,
10051038
): AcpToolCallEmitDecision {

0 commit comments

Comments
 (0)