fix(server): drop superseded tool updates from snapshots
A tool.updated row is the in-flight snapshot of a call; once the call completes, the tool.completed row carries the final state and both clients fold every matching update into it. Shipping the updates buys nothing: 47k such rows exist in one real database, and a single thread carries 3,291 of them. Filter them out of thread snapshots, mirroring the existing context-window dedup. Matching is per turn and only against a LATER completion, so a revert that discards the completing turn cannot leave a call unrepresented, and a later update under the same identity (the next call, still in flight) survives. Live events are untouched. Rows are matched on the same identity the clients collapse by: an explicit data.toolCallId when the adapter emits one, otherwise the itemType/title/detail triple. No tool lifecycle row in the real db carries a toolCallId, so the fallback does the work. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
64a3cd6d7d
commit
b7d1981b57
2 files changed
+242
-3
No files matched your search
@@ -365,6 +365,94 @@ function dropStaleContextWindowActivities(
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identity both clients use to fold a tool lifecycle row into the call it
|
||||
* belongs to (`deriveToolLifecycleCollapseKey` in web's `session-logic` and
|
||||
* mobile's `threadActivity`): an explicit `data.toolCallId` when the adapter
|
||||
* emits one, otherwise the itemType/title/detail triple. Returns null for rows
|
||||
* with no identity at all — those never collapse on the client either, so they
|
||||
* must not be dropped here.
|
||||
*/
|
||||
function toolLifecycleIdentity(activity: OrchestrationThreadActivity): string | null {
|
||||
const payload = asRecord(activity.payload);
|
||||
if (!payload) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const toolCallId = asTrimmedString(asRecord(payload.data)?.toolCallId);
|
||||
if (toolCallId) {
|
||||
return `id:${toolCallId}`;
|
||||
}
|
||||
|
||||
const itemType = asTrimmedString(payload.itemType) ?? "";
|
||||
// Mirrors the clients' `normalizeCompactToolLabel`: a completion's title may
|
||||
// gain a trailing "complete"/"completed" the in-flight updates lack.
|
||||
const label = (asTrimmedString(payload.title) ?? activity.summary)
|
||||
.replace(/\s+(?:complete|completed)\s*$/iu, "")
|
||||
.trim();
|
||||
const detail = asTrimmedString(payload.detail) ?? "";
|
||||
if (itemType.length === 0 && label.length === 0 && detail.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return [itemType, label, detail].join("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops `tool.updated` rows a `tool.completed` row already supersedes. An
|
||||
* update is the in-flight snapshot of a call; once the call completes, the
|
||||
* completion carries the final state and the clients fold every matching
|
||||
* update into it, so shipping the updates buys nothing — 47k such rows exist
|
||||
* in one real database, and a single thread carries 2,291 of them totalling
|
||||
* ~1MB post-slimming.
|
||||
*
|
||||
* Matching is per turn for the same reason `dropStaleContextWindowActivities`
|
||||
* retains per turn: a live `thread.reverted` makes the client discard whole
|
||||
* turns, so a completion in a different turn could vanish and leave the
|
||||
* dropped update unrepresented. The completion must also come *after* the
|
||||
* update within the turn — a later update belongs to a subsequent call that
|
||||
* reuses the same identity and is still in flight. Rows without a lifecycle
|
||||
* identity pass through, matching the clients, which never collapse them.
|
||||
* Live `thread.activity-appended` events are untouched: updates still stream
|
||||
* in real time and the completion supersedes them on the client as before.
|
||||
*/
|
||||
function dropSupersededToolUpdatedActivities(
|
||||
activities: ReadonlyArray<OrchestrationThreadActivity>,
|
||||
): ReadonlyArray<OrchestrationThreadActivity> {
|
||||
const completionIndicesByKey = new Map<string, number[]>();
|
||||
for (let index = 0; index < activities.length; index += 1) {
|
||||
const activity = activities[index]!;
|
||||
if (activity.kind !== "tool.completed") {
|
||||
continue;
|
||||
}
|
||||
const identity = toolLifecycleIdentity(activity);
|
||||
if (!identity) {
|
||||
continue;
|
||||
}
|
||||
const key = `${activity.turnId ?? ""} | ||||