Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

perf(server): persist the wire projection for streaming tool.updated data (#6675)

Martin Bergof075a58119f3
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.activity.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.activity.test.ts
index 936041038..27564eb57 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.activity.test.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.activity.test.ts
@@ -82,3 +82,63 @@ describe("runtimeEventToActivities task progress", () => {
expect(usagePayload).not.toHaveProperty("status");
});
});
+describe("runtimeEventToActivities tool streaming persistence", () => {
+ const accumulatedStdout = [
+ "first line of output",
+ ...Array.from({ length: 500 }, (_, index) => `Capturing frame ${index}/9028`),
+ ].join("\n");
+ const streamingData = {
+ toolCallId: "tool-call-1",
+ kind: "execute",
+ command: "blender --render",
+ rawOutput: { stdout: accumulatedStdout },
+ content: [{ type: "content", content: { type: "text", text: accumulatedStdout } }],
+ };
+
+ it("persists tool.updated with the wire projection of data, not the accumulated stream", () => {
+ const event = {
+ ...base,
+ type: "item.updated",
+ eventId: EventId.make("evt-tool-streaming-updated"),
+ payload: {
+ itemType: "command_execution",
+ status: "inProgress",
+ title: "Render",
+ detail: accumulatedStdout,
+ data: streamingData,
+ },
+ } satisfies ProviderRuntimeEvent;
+
+ const activities = runtimeEventToActivities(event);
+
+ expect(activities).toHaveLength(1);
+ const payload = activities[0]?.payload as Record<string, unknown>;
+ const data = payload.data as Record<string, unknown>;
+ expect(payload.status).toBe("inProgress");
+ expect(data.toolCallId).toBe("tool-call-1");
+ expect(data.command).toBe("blender --render");
+ expect(data.rawOutput).toEqual({ content: "first line of output" });
+ expect(data.content).toBeUndefined();
+ expect(JSON.stringify(data).length).toBeLessThan(1_000);
+ });
+
+ it("persists the full terminal payload on tool.completed", () => {
+ const event = {
+ ...base,
+ type: "item.completed",
+ eventId: EventId.make("evt-tool-streaming-completed"),
+ payload: {
+ itemType: "command_execution",
+ status: "completed",
+ title: "Render",
+ data: streamingData,
+ },
+ } satisfies ProviderRuntimeEvent;
+
+ const activities = runtimeEventToActivities(event);
+
+ expect(activities).toHaveLength(1);
+ const payload = activities[0]?.payload as Record<string, unknown>;
+ expect(payload.data).toEqual(streamingData);
+ });
+});
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
index c942960f3..e24825d3d 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
@@ -41,6 +41,7 @@ import {
ProviderRuntimeIngestionService,
type ProviderRuntimeIngestionShape,
} from "../Services/ProviderRuntimeIngestion.ts";
+import { projectActivityPayload } from "../ActivityPayloadProjection.ts";
import { forkParked } from "../../serverActivation.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { canReplaceThreadTitle } from "../threadTitles.ts";
@@ -786,8 +787,15 @@ export function runtimeEventToActivities(
if (!isToolLifecycleItemType(event.payload.itemType)) {
return [];
}
+ // A streaming update's `data` carries the full tool output accumulated
+ // so far (adapters merge state forward), and a new activity is emitted
+ // per chunk, so persisting `data` verbatim writes O(N²) bytes per tool
+ // call into both the event store and the projection table. No reader
+ // needs it: ws.ts and http.ts apply `projectActivityPayload` before any
+ // payload reaches a client. Persist the projected form for non-terminal
+ // updates; `item.completed` below still persists the full payload.
return [
- {
+ projectActivityPayload({
id: event.eventId,
createdAt: event.createdAt,
tone: "tool",
@@ -805,7 +813,7 @@ export function runtimeEventToActivities(
},
turnId: toTurnId(event.turnId) ?? null,
...maybeSequence,
- },
+ }),
];
}