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): reduce streaming and tool update overhead (#143)

Leoe142a9f72f04
diff --git a/apps/server/src/orchestration/ActivityPayloadProjection.test.ts b/apps/server/src/orchestration/ActivityPayloadProjection.test.ts
index 8e3dd8bbd..c32843e93 100644
--- a/apps/server/src/orchestration/ActivityPayloadProjection.test.ts
+++ b/apps/server/src/orchestration/ActivityPayloadProjection.test.ts
@@ -75,6 +75,28 @@ describe("projectActivityPayload", () => {
expect(JSON.stringify(projected.payload).length).toBeLessThan(500);
});
+ it("scans tool output without retaining the full text behind its preview", () => {
+ const preview = projectActivityPayload(
+ activity({
+ itemType: "command_execution",
+ data: { rawOutput: `\`\`\`\n actual\tresult \n${"x".repeat(5000)}` },
+ }),
+ );
+ const fences = projectActivityPayload(
+ activity({
+ itemType: "command_execution",
+ data: { rawOutput: "```\r\n \t \n```\n" },
+ }),
+ );
+
+ expect((preview.payload as { data: { rawOutput: unknown } }).data.rawOutput).toEqual({
+ content: "actual result",
+ });
+ expect((fences.payload as { data: { rawOutput: unknown } }).data.rawOutput).toEqual({
+ content: "2 lines",
+ });
+ });
+
it("keeps bounded Claude and ACP command output summaries", () => {
const claude = projectActivityPayload(
activity({
@@ -201,6 +223,35 @@ describe("projectActivityPayload", () => {
expect(JSON.stringify(projected.payload).length).toBeLessThan(500);
});
+ it("bounds large MCP arguments and inputs in client projections", () => {
+ const oversized = "x".repeat(10_000_000);
+ const codex = projectActivityPayload(
+ activity({
+ itemType: "mcp_tool_call",
+ data: {
+ item: {
+ type: "mcpToolCall",
+ tool: "write_file",
+ arguments: { content: oversized },
+ },
+ },
+ }),
+ );
+ const claude = projectActivityPayload(
+ activity({
+ itemType: "mcp_tool_call",
+ data: { toolName: "mcp__files__write", input: { content: oversized } },
+ }),
+ );
+
+ expect(JSON.stringify(codex.payload).length).toBeLessThan(5_000);
+ expect(JSON.stringify(claude.payload).length).toBeLessThan(5_000);
+ expect(
+ (codex.payload as { data: { item: { arguments: { content: string } } } }).data.item.arguments
+ .content.length,
+ ).toBe(4_096);
+ });
+
it("passes task lifecycle payloads (no data field) through untouched", () => {
const source = activity({
taskId: "task-9",
diff --git a/apps/server/src/orchestration/ActivityPayloadProjection.ts b/apps/server/src/orchestration/ActivityPayloadProjection.ts
index 930934488..19ea14362 100644
--- a/apps/server/src/orchestration/ActivityPayloadProjection.ts
+++ b/apps/server/src/orchestration/ActivityPayloadProjection.ts
@@ -18,6 +18,42 @@ function asTrimmedString(value: unknown): string | null {
return trimmed.length > 0 ? trimmed : null;
}
+const MAX_PROJECTED_VALUE_DEPTH = 4;
+const MAX_PROJECTED_STRING_LENGTH = 4_096;
+const MAX_PROJECTED_ARRAY_LENGTH = 24;
+const MAX_PROJECTED_OBJECT_KEYS = 32;
+
+function copyTruncatedString(value: string): string {
+ if (value.length <= MAX_PROJECTED_STRING_LENGTH) {
+ return value;
+ }
+ const prefix = Array.from(value.slice(0, MAX_PROJECTED_STRING_LENGTH - 1)).join("");
+ return `${prefix}…`;
+}
+
+function projectBoundedValue(value: unknown, depth = 0): unknown {
+ if (typeof value === "string") {
+ return copyTruncatedString(value);
+ }
+ if (value === null || typeof value !== "object") {
+ return value;
+ }
+ if (depth >= MAX_PROJECTED_VALUE_DEPTH) {
+ return "[truncated]";
+ }
+ if (Array.isArray(value)) {
+ return value
+ .slice(0, MAX_PROJECTED_ARRAY_LENGTH)
+ .map((entry) => projectBoundedValue(entry, depth + 1));
+ }
+
+ const projected: Record<string, unknown> = {};
+ for (const [key, entry] of Object.entries(value).slice(0, MAX_PROJECTED_OBJECT_KEYS)) {
+ projected[key] = projectBoundedValue(entry, depth + 1);
+ }
+ return projected;
+}
+
function pushChangedFile(target: string[], seen: Set<string>, value: unknown): void {
const normalized = asTrimmedString(value);
if (!normalized || seen.has(normalized)) {
@@ -88,7 +124,7 @@ function projectCommandData(data: Record<string, unknown>): Record<string, unkno
const projectedItem: Record<string, unknown> = {};
if ("command" in item) {
- projectedItem.command = item.command;
+ projectedItem.command = projectBoundedValue(item.command);
}
const aggregatedOutput = asTrimmedString(item.aggregatedOutput);
@@ -101,14 +137,14 @@ function projectCommandData(data: Record<string, unknown>): Record<string, unkno
const input = asRecord(item.input);
if (input && "command" in input) {
- projectedItem.input = { command: input.command };
+ projectedItem.input = { command: projectBoundedValue(input.command) };
}
const result = asRecord(item.result);
if (result) {
const projectedResult: Record<string, unknown> = {};
if ("command" in result) {
- projectedResult.command = result.command;
+ projectedResult.command = projectBoundedValue(result.command);
}
const content = asTrimmedString(result.content);
if (content) {
@@ -149,22 +185,28 @@ function projectCommandValue(data: Record<string, unknown>): unknown {
}
function summarizeToolTextOutput(value: string): string | null {
- const lines: string[] = [];
- for (const rawLine of value.split(/\r?\n/u)) {
- const line = rawLine.replace(/\s+/g, " ").trim();
+ let meaningfulLineCount = 0;
+ let offset = 0;
+
+ while (offset <= value.length) {
+ const newlineIndex = value.indexOf("\n", offset);
+ const lineEnd = newlineIndex === -1 ? value.length : newlineIndex;
+ const line = value.slice(offset, lineEnd).replace(/\s+/g, " ").trim();
if (line.length > 0) {
- lines.push(line);
+ meaningfulLineCount += 1;
+ if (line !== "```") {
+ const summary = line.length <= 84 ? line : `${line.slice(0, 83).trimEnd()}…`;
+ // Copy the preview so V8 cannot retain the full tool output behind a slice.
+ return Array.from(summary).join("");
+ }
}
+ if (newlineIndex === -1) {
+ break;
+ }
+ offset = newlineIndex + 1;
}
- const firstLine = lines.find((line) => line !== "```");
- if (firstLine) {
- return firstLine.length <= 84 ? firstLine : `${firstLine.slice(0, 83).trimEnd()}…`;
- }
- if (lines.length > 1) {
- return `${lines.length.toLocaleString()} lines`;
- }
- return null;
+ return meaningfulLineCount > 1 ? `${meaningfulLineCount.toLocaleString()} lines` : null;
}
/**
@@ -236,7 +278,7 @@ function projectMcpToolCallData(data: Record<string, unknown>): Record<string, u
const projectedItem: Record<string, unknown> = {};
for (const key of MCP_ITEM_KEPT_FIELDS) {
if (key in item) {
- projectedItem[key] = item[key];
+ projectedItem[key] = projectBoundedValue(item[key]);
}
}
const result = summarizeMcpResult(item.result);
@@ -250,7 +292,7 @@ function projectMcpToolCallData(data: Record<string, unknown>): Record<string, u
projectedData.toolName = data.toolName;
}
if ("input" in data) {
- projectedData.input = data.input;
+ projectedData.input = projectBoundedValue(data.input);
}
if (!item) {
const result = summarizeMcpResult(data.result);
@@ -370,7 +412,7 @@ export function projectActivityPayload(
}
const command = projectCommandValue(data);
if (command !== undefined) {
- projectedData.command = command;
+ projectedData.command = projectBoundedValue(command);
}
const changedFiles: string[] = [];
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
index 3711e9750..c1e1f6741 100644
--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
@@ -1332,6 +1332,8 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
const eventStore = yield* OrchestrationEventStore;
const sql = yield* SqlClient.SqlClient;
const now = "2026-01-01T00:00:00.000Z";
+ const streamingAt = "2026-01-01T00:00:01.000Z";
+ const completedAt = "2026-01-01T00:00:02.000Z";
yield* eventStore.append({
type: "project.created",
@@ -1396,7 +1398,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
role: "assistant",
text: "hello",
turnId: null,
- streaming: false,
+ streaming: true,
createdAt: now,
updatedAt: now,
},
@@ -1409,7 +1411,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
eventId: EventId.make("evt-a4"),
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-a"),
- occurredAt: now,
+ occurredAt: streamingAt,
commandId: CommandId.make("cmd-a4"),
causationEventId: null,
correlationId: CorrelationId.make("cmd-a4"),
@@ -1421,18 +1423,61 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
text: " world",
turnId: null,
streaming: true,
- createdAt: now,
- updatedAt: now,
+ createdAt: streamingAt,
+ updatedAt: streamingAt,
},
});
yield* projectionPipeline.bootstrap;
yield* projectionPipeline.bootstrap;
- const messageRows = yield* sql<{ readonly text: string }>`
- SELECT text FROM projection_thread_messages WHERE message_id = 'message-a'
+ yield* eventStore.append({
+ type: "thread.message-sent",
+ eventId: EventId.make("evt-a5"),
+ aggregateKind: "thread",
+ aggregateId: ThreadId.make("thread-a"),
+ occurredAt: completedAt,
+ commandId: CommandId.make("cmd-a5"),
+ causationEventId: null,
+ correlationId: CorrelationId.make("cmd-a5"),
+ metadata: {},
+ payload: {
+ threadId: ThreadId.make("thread-a"),
+ messageId: MessageId.make("message-a"),
+ role: "assistant",
+ text: "",
+ turnId: null,
+ streaming: false,
+ createdAt: completedAt,
+ updatedAt: completedAt,
+ },
+ });
+
+ yield* projectionPipeline.bootstrap;
+ yield* projectionPipeline.bootstrap;
+
+ const messageRows = yield* sql<{
+ readonly text: string;
+ readonly isStreaming: number;
+ readonly createdAt: string;
+ readonly updatedAt: string;
+ }>`
+ SELECT
+ text,
+ is_streaming AS "isStreaming",
+ created_at AS "createdAt",
+ updated_at AS "updatedAt"
+ FROM projection_thread_messages
+ WHERE message_id = 'message-a'
`;
- assert.deepEqual(messageRows, [{ text: "hello world" }]);
+ assert.deepEqual(messageRows, [
+ {
+ text: "hello world",
+ isStreaming: 0,
+ createdAt: now,
+ updatedAt: completedAt,
+ },
+ ]);
const stateRows = yield* sql<{
readonly projector: string;
diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
index 1463d2356..24f91985f 100644
--- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
+++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts
@@ -1422,21 +1422,38 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
)(function* (event, attachmentSideEffects) {
switch (event.type) {
case "thread.message-sent": {
+ if (event.payload.streaming) {
+ const attachments =
+ event.payload.attachments !== undefined
+ ? yield* materializeAttachmentsForProjection({
+ attachments: event.payload.attachments,
+ })
+ : undefined;
+ yield* projectionThreadMessageRepository.appendStreaming({
+ messageId: event.payload.messageId,
+ threadId: event.payload.threadId,
+ turnId: event.payload.turnId,
+ respondingBotId: event.payload.respondingBotId ?? null,
+ authorPersonId: event.payload.authorPersonId ?? null,
+ authorDisplayName: event.payload.authorDisplayName ?? null,
+ channelOrigin: event.payload.channelOrigin ?? null,
+ role: event.payload.role,
+ text: event.payload.text,
+ ...(attachments !== undefined ? { attachments: [...attachments] } : {}),
+ createdAt: event.payload.createdAt,
+ updatedAt: event.payload.updatedAt,
+ });
+ return;
+ }
+
const existingMessage = yield* projectionThreadMessageRepository.getByMessageId({
messageId: event.payload.messageId,
});
const previousMessage = Option.getOrUndefined(existingMessage);
const nextText = Option.match(existingMessage, {
onNone: () => event.payload.text,
- onSome: (message) => {
- if (event.payload.streaming) {
- return `${message.text}${event.payload.text}`;
- }
- if (event.payload.text.length === 0) {
- return message.text;
- }
- return event.payload.text;
- },
+ onSome: (message) =>
+ event.payload.text.length === 0 ? message.text : event.payload.text,
});
const nextAttachments =
event.payload.attachments !== undefined
@@ -1458,7 +1475,7 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
text: nextText,
...(nextAttachments !== undefined ? { attachments: [...nextAttachments] } : {}),
reactions: previousMessage?.reactions ?? [],
- isStreaming: event.payload.streaming,
+ isStreaming: false,
createdAt: previousMessage?.createdAt ?? event.payload.createdAt,
updatedAt: event.payload.updatedAt,
});
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
index 3a396da57..cf9095722 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
@@ -1385,6 +1385,30 @@ describe("ProviderRuntimeIngestion", () => {
expect(message?.streaming).toBe(false);
});
+ it("ignores provider content deltas that cannot change thread state", async () => {
+ const harness = await createHarness();
+ const before = await harness.readModel();
+
+ for (const [index, streamKind] of (
+ ["command_output", "file_change_output", "reasoning_summary_text", "reasoning_text"] as const
+ ).entries()) {
+ harness.emit({
+ type: "content.delta",
+ eventId: asEventId(`evt-ignored-delta-${index}`),
+ provider: ProviderDriverKind.make("codex"),
+ createdAt: "2026-01-01T00:00:01.000Z",
+ threadId: asThreadId("thread-1"),
+ turnId: asTurnId("turn-ignored-deltas"),
+ itemId: asItemId(`item-ignored-delta-${index}`),
+ payload: { streamKind, delta: "large transient provider output" },
+ });
+ }
+
+ await harness.drain();
+
+ expect(await harness.readModel()).toEqual(before);
+ });
+
it("projects a persisted tool screenshot as an assistant image attachment", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
index e1aad7925..0e0e7fbfa 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
@@ -1625,6 +1625,9 @@ const make = Effect.gen(function* () {
const processRuntimeEvent = (event: ProviderRuntimeEvent) =>
Effect.gen(function* () {
+ if (event.type === "content.delta" && event.payload.streamKind !== "assistant_text") {
+ return;
+ }
const thread = yield* resolveThreadShell(event.threadId);
if (!thread) return;
if (event.type === "request.opened" || event.type === "request.resolved") {
@@ -1647,9 +1650,17 @@ const make = Effect.gen(function* () {
const activeTurnId = thread.session?.activeTurnId ?? null;
const conflictsWithActiveTurn =
activeTurnId !== null && eventTurnId !== undefined && !sameId(activeTurnId, eventTurnId);
- const pendingTurnStart = yield* projectionTurnRepository.getPendingTurnStartByThreadId({
- threadId: thread.id,
- });
+ const needsPendingTurnStart =
+ event.type === "session.started" ||
+ event.type === "session.state.changed" ||
+ event.type === "thread.started" ||
+ event.type === "thread.token-usage.updated" ||
+ event.type === "turn.started" ||
+ event.type === "turn.aborted" ||
+ event.type === "turn.completed";
+ const pendingTurnStart = needsPendingTurnStart
+ ? yield* projectionTurnRepository.getPendingTurnStartByThreadId({ threadId: thread.id })
+ : Option.none();
const expectedPendingTurnId = Option.isSome(pendingTurnStart)
? yield* getExpectedProviderTurnIdForThread(thread.id)
: undefined;
diff --git a/apps/server/src/orchestration/ThreadLiveEventCoalescer.test.ts b/apps/server/src/orchestration/ThreadLiveEventCoalescer.test.ts
new file mode 100644
index 000000000..0a9915294
--- /dev/null
+++ b/apps/server/src/orchestration/ThreadLiveEventCoalescer.test.ts
@@ -0,0 +1,171 @@
+import {
+ EventId,
+ MessageId,
+ ThreadId,
+ TurnId,
+ type OrchestrationEvent,
+ type OrchestrationThreadActivity,
+} from "@t3tools/contracts";
+import { it } from "@effect/vitest";
+import * as Clock from "effect/Clock";
+import * as Effect from "effect/Effect";
+import * as TestClock from "effect/testing/TestClock";
+import { describe, expect } from "vite-plus/test";
+
+import {
+ coalesceLiveToolUpdatedEvents,
+ makeThreadLiveEventCoalescer,
+} from "./ThreadLiveEventCoalescer.ts";
+
+const threadId = ThreadId.make("thread-coalescer-test");
+const turnId = TurnId.make("turn-coalescer-test");
+
+function makeToolActivity(
+ sequence: number,
+ options: {
+ readonly kind?: "tool.updated" | "tool.completed";
+ readonly toolCallId?: string;
+ readonly turnId?: TurnId;
+ } = {},
+): OrchestrationEvent {
+ const {
+ kind = "tool.updated",
+ toolCallId = "call-edit",
+ turnId: activityTurnId = turnId,
+ } = options;
+ const activity: OrchestrationThreadActivity = {
+ id: EventId.make(`activity-${sequence}`),
+ tone: "tool",
+ kind,
+ summary: "Editing app.ts",
+ payload: {
+ itemType: "file_change",
+ title: "Editing app.ts",
+ data: toolCallId ? { toolCallId } : {},
+ },
+ turnId: activityTurnId,
+ createdAt: "2026-01-01T00:00:01.000Z",
+ };
+ return {
+ sequence,
+ eventId: EventId.make(`event-${sequence}`),
+ aggregateKind: "thread",
+ aggregateId: threadId,
+ occurredAt: "2026-01-01T00:00:01.000Z",
+ commandId: null,
+ causationEventId: null,
+ correlationId: null,
+ metadata: {},
+ type: "thread.activity-appended",
+ payload: { threadId, activity },
+ };
+}
+
+function makeMessage(sequence: number): OrchestrationEvent {
+ return {
+ sequence,
+ eventId: EventId.make(`event-${sequence}`),
+ aggregateKind: "thread",
+ aggregateId: threadId,
+ occurredAt: "2026-01-01T00:00:02.000Z",
+ commandId: null,
+ causationEventId: null,
+ correlationId: null,
+ metadata: {},
+ type: "thread.message-sent",
+ payload: {
+ threadId,
+ messageId: MessageId.make(`message-${sequence}`),
+ role: "assistant",
+ text: "Still working",
+ turnId,
+ streaming: false,
+ createdAt: "2026-01-01T00:00:02.000Z",
+ updatedAt: "2026-01-01T00:00:02.000Z",
+ },
+ };
+}
+
+describe("ThreadLiveEventCoalescer", () => {
+ it("coalesces only calls with a stable toolCallId", () => {
+ const events = [
+ makeToolActivity(1, { toolCallId: "call-a" }),
+ makeToolActivity(2, { toolCallId: "call-b" }),
+ makeToolActivity(3, { toolCallId: "call-a" }),
+ ];
+
+ expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([2, 3]);
+ });
+
+ it("preserves parallel same-label calls without a stable toolCallId", () => {
+ const events = [
+ makeToolActivity(1, { toolCallId: "" }),
+ makeToolActivity(2, { toolCallId: "" }),
+ makeToolActivity(3, { kind: "tool.completed", toolCallId: "" }),
+ ];
+
+ expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([1, 2, 3]);
+ });
+
+ it("does not coalesce stable tool calls across turns", () => {
+ const events = [
+ makeToolActivity(1, { turnId: TurnId.make("turn-old") }),
+ makeToolActivity(2, { turnId: TurnId.make("turn-new") }),
+ ];
+
+ expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([1, 2]);
+ });
+
+ it("flushes a stable update run before a completion boundary", () => {
+ const events = [
+ makeToolActivity(1),
+ makeToolActivity(2),
+ makeToolActivity(3, { kind: "tool.completed" }),
+ makeToolActivity(4),
+ ];
+
+ expect(coalesceLiveToolUpdatedEvents(events).map((event) => event.sequence)).toEqual([2, 3, 4]);
+ });
+
+ it.effect("flushes pending tool updates as soon as an unrelated event arrives", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const coalescer = yield* makeThreadLiveEventCoalescer({ coalesceWindow: "500 millis" });
+ const startedAt = yield* Clock.currentTimeMillis;
+ yield* Effect.forEach(
+ Array.from({ length: 10 }, (_, index) => index + 2),
+ (sequence) =>
+ coalescer.offerAndWait({ kind: "event", event: makeToolActivity(sequence) }),
+ { discard: true },
+ );
+ yield* coalescer.offerAndWait({ kind: "event", event: makeMessage(12) });
+
+ expect(yield* Clock.currentTimeMillis).toBe(startedAt);
+ expect(
+ Array.from(yield* coalescer.takeAll).map((item) =>
+ item.kind === "event" ? item.event.sequence : item.kind,
+ ),
+ ).toEqual([11, 12]);
+ }),
+ ).pipe(Effect.provide(TestClock.layer())),
+ );
+
+ it.effect("flushes pending tool updates as soon as a synchronization marker arrives", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const coalescer = yield* makeThreadLiveEventCoalescer({ coalesceWindow: "500 millis" });
+ const startedAt = yield* Clock.currentTimeMillis;
+ yield* coalescer.offerAndWait({ kind: "event", event: makeToolActivity(2) });
+ yield* coalescer.offerAndWait({ kind: "event", event: makeToolActivity(3) });
+ yield* coalescer.offerAndWait({ kind: "synchronized" });
+
+ expect(yield* Clock.currentTimeMillis).toBe(startedAt);
+ expect(
+ Array.from(yield* coalescer.takeAll).map((item) =>
+ item.kind === "event" ? item.event.sequence : item.kind,
+ ),
+ ).toEqual([3, "synchronized"]);
+ }),
+ ).pipe(Effect.provide(TestClock.layer())),
+ );
+});
diff --git a/apps/server/src/orchestration/ThreadLiveEventCoalescer.ts b/apps/server/src/orchestration/ThreadLiveEventCoalescer.ts
new file mode 100644
index 000000000..f188deea8
--- /dev/null
+++ b/apps/server/src/orchestration/ThreadLiveEventCoalescer.ts
@@ -0,0 +1,203 @@
+import type { OrchestrationEvent, OrchestrationThreadStreamItem } from "@t3tools/contracts";
+import * as Deferred from "effect/Deferred";
+import * as Duration from "effect/Duration";
+import * as Effect from "effect/Effect";
+import * as Fiber from "effect/Fiber";
+import * as Predicate from "effect/Predicate";
+import * as Queue from "effect/Queue";
+import * as Semaphore from "effect/Semaphore";
+import * as Stream from "effect/Stream";
+
+import { projectActivityEvent } from "./ActivityPayloadProjection.ts";
+
+const COALESCE_WINDOW = Duration.millis(50);
+const MAX_PENDING_UPDATES = 512;
+
+export type ThreadLiveInput =
+ | { readonly kind: "event"; readonly event: OrchestrationEvent }
+ | { readonly kind: "synchronized" };
+
+function isToolUpdated(event: OrchestrationEvent): boolean {
+ return (
+ event.type === "thread.activity-appended" && event.payload.activity.kind === "tool.updated"
+ );
+}
+
+function asTrimmedString(value: unknown): string | null {
+ if (!Predicate.isString(value)) {
+ return null;
+ }
+ const trimmed = value.trim();
+ return trimmed.length > 0 ? trimmed : null;
+}
+
+function stableToolCallIdentity(event: OrchestrationEvent): string | null {
+ if (event.type !== "thread.activity-appended") {
+ return null;
+ }
+ const payload = event.payload.activity.payload;
+ if (!Predicate.isObject(payload)) {
+ return null;
+ }
+ const data = Predicate.isObject(payload.data) ? payload.data : null;
+ return asTrimmedString(payload.toolCallId) ?? asTrimmedString(data?.toolCallId);
+}
+
+/**
+ * Retain only the latest in-flight update for each stable tool-call id in a
+ * live run. Anonymous calls pass through because labels are not unique when
+ * tools execute in parallel. Survivors remain in sequence order.
+ */
+export function coalesceLiveToolUpdatedEvents(
+ events: ReadonlyArray<OrchestrationEvent>,
+): ReadonlyArray<OrchestrationEvent> {
+ const survivors: Array<OrchestrationEvent> = [];
+ let pendingUpdates: Array<OrchestrationEvent> = [];
+
+ const flushUpdates = () => {
+ const seen = new Set<string>();
+ const latestUpdates: Array<OrchestrationEvent> = [];
+ for (let index = pendingUpdates.length - 1; index >= 0; index -= 1) {
+ const event = pendingUpdates[index]!;
+ const identity = stableToolCallIdentity(event);
+ const activity =
+ event.type === "thread.activity-appended" ? event.payload.activity : undefined;
+ const key = identity ? `${activity?.turnId ?? ""}\u0000${identity}` : null;
+ if (key && seen.has(key)) {
+ continue;
+ }
+ if (key) {
+ seen.add(key);
+ }
+ latestUpdates.push(event);
+ }
+ latestUpdates.reverse();
+ survivors.push(...latestUpdates);
+ pendingUpdates = [];
+ };
+
+ for (const event of events) {
+ if (isToolUpdated(event)) {
+ pendingUpdates.push(event);
+ continue;
+ }
+ flushUpdates();
+ survivors.push(event);
+ }
+ flushUpdates();
+ return survivors;
+}
+
+export const makeThreadLiveEventCoalescer = Effect.fn("makeThreadLiveEventCoalescer")(
+ function* (options?: { readonly coalesceWindow?: Duration.Input }) {
+ const output = yield* Queue.unbounded<OrchestrationThreadStreamItem>();
+ const input = yield* Queue.unbounded<{
+ readonly value: ThreadLiveInput;
+ readonly processed?: Deferred.Deferred<void>;
+ }>();
+ const mutex = yield* Semaphore.make(1);
+ const coalesceWindow = options?.coalesceWindow ?? COALESCE_WINDOW;
+ let pendingUpdates: Array<OrchestrationEvent> = [];
+ let windowGeneration = 0;
+ let windowFiber: Fiber.Fiber<void, never> | null = null;
+
+ const cancelWindow = Effect.fn("ThreadLiveEventCoalescer.cancelWindow")(function* () {
+ const fiber = windowFiber;
+ if (!fiber) {
+ return;
+ }
+ windowFiber = null;
+ yield* Fiber.interrupt(fiber);
+ });
+
+ const flushPending = Effect.fn("ThreadLiveEventCoalescer.flushPending")(function* (
+ boundary?: OrchestrationEvent,
+ ) {
+ const events = boundary ? [...pendingUpdates, boundary] : pendingUpdates;
+ pendingUpdates = [];
+ if (events.length === 0) {
+ return;
+ }
+ yield* Queue.offerAll(
+ output,
+ coalesceLiveToolUpdatedEvents(events).map((event) => ({
+ kind: "event" as const,
+ event: projectActivityEvent(event),
+ })),
+ );
+ });
+
+ const flushWindow = (generation: number) =>
+ Effect.sleep(coalesceWindow).pipe(
+ Effect.andThen(
+ mutex.withPermits(1)(
+ Effect.suspend(() => (generation === windowGeneration ? flushPending() : Effect.void)),
+ ),
+ ),
+ Effect.ensuring(
+ Effect.sync(() => {
+ if (generation === windowGeneration) {
+ windowFiber = null;
+ }
+ }),
+ ),
+ );
+
+ const process = Effect.fn("ThreadLiveEventCoalescer.process")(function* (
+ input: ThreadLiveInput,
+ ) {
+ yield* mutex.withPermits(1)(
+ Effect.gen(function* () {
+ if (input.kind === "event" && isToolUpdated(input.event)) {
+ pendingUpdates.push(input.event);
+ if (pendingUpdates.length === 1) {
+ const generation = ++windowGeneration;
+ windowFiber = yield* Effect.forkScoped(flushWindow(generation));
+ }
+ if (pendingUpdates.length >= MAX_PENDING_UPDATES) {
+ yield* cancelWindow();
+ windowGeneration += 1;
+ yield* flushPending();
+ }
+ return;
+ }
+
+ yield* cancelWindow();
+ windowGeneration += 1;
+ if (input.kind === "event") {
+ yield* flushPending(input.event);
+ } else {
+ yield* flushPending();
+ yield* Queue.offer(output, { kind: "synchronized" });
+ }
+ }),
+ );
+ });
+
+ yield* Stream.fromQueue(input).pipe(
+ Stream.runForEach(({ value, processed }) =>
+ process(value).pipe(
+ Effect.andThen(processed ? Deferred.succeed(processed, undefined) : Effect.void),
+ ),
+ ),
+ Effect.forkScoped,
+ );
+
+ const offer = (value: ThreadLiveInput) => Queue.offer(input, { value }).pipe(Effect.asVoid);
+
+ const offerAndWait = Effect.fn("ThreadLiveEventCoalescer.offerAndWait")(function* (
+ value: ThreadLiveInput,
+ ) {
+ const processed = yield* Deferred.make<void>();
+ yield* Queue.offer(input, { value, processed });
+ yield* Deferred.await(processed);
+ });
+
+ return {
+ offer,
+ offerAndWait,
+ stream: Stream.fromQueue(output),
+ takeAll: Queue.takeAll(output),
+ } as const;
+ },
+);
diff --git a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts
index 0dfacd72e..0854ab96a 100644
--- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts
+++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts
@@ -1,4 +1,4 @@
-import { MessageId, ThreadId } from "@t3tools/contracts";
+import { BotId, MessageId, ThreadId } from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
@@ -12,6 +12,168 @@ const layer = it.layer(
);
layer("ProjectionThreadMessageRepository", (it) => {
+ it.effect("preserves streamed text when an empty message completes the stream", () =>
+ Effect.gen(function* () {
+ const repository = yield* ProjectionThreadMessageRepository;
+ const threadId = ThreadId.make("thread-streaming-completion");
+ const messageId = MessageId.make("message-streaming-completion");
+ const createdAt = "2026-09-01T17:00:00.000Z";
+
+ yield* repository.appendStreaming({
+ messageId,
+ threadId,
+ turnId: null,
+ role: "assistant",
+ text: "one",
+ createdAt,
+ updatedAt: createdAt,
+ });
+ yield* repository.appendStreaming({
+ messageId,
+ threadId,
+ turnId: null,
+ role: "assistant",
+ text: " two",
+ createdAt,
+ updatedAt: "2026-09-01T17:00:01.000Z",
+ });
+ yield* repository.appendStreaming({
+ messageId,
+ threadId,
+ turnId: null,
+ role: "assistant",
+ text: " three",
+ createdAt,
+ updatedAt: "2026-09-01T17:00:02.000Z",
+ });
+
+ yield* repository.upsert({
+ messageId,
+ threadId,
+ turnId: null,
+ role: "assistant",
+ text: "",
+ isStreaming: false,
+ createdAt,
+ updatedAt: "2026-09-01T17:00:03.000Z",
+ });
+
+ const result = yield* repository.getByMessageId({ messageId });
+ assert.equal(result._tag, "Some");
+ if (result._tag === "Some") {
+ assert.equal(result.value.text, "one two three");
+ assert.isFalse(result.value.isStreaming);
+ assert.equal(result.value.updatedAt, "2026-09-01T17:00:03.000Z");
+ }
+ }),
+ );
+
+ it.effect("allows empty text to replace a completed message", () =>
+ Effect.gen(function* () {
+ const repository = yield* ProjectionThreadMessageRepository;
+ const threadId = ThreadId.make("thread-empty-completed-message");
+ const messageId = MessageId.make("message-empty-completed-message");
+ const createdAt = "2026-09-01T17:10:00.000Z";
+
+ yield* repository.upsert({
+ messageId,
+ threadId,
+ turnId: null,
+ role: "assistant",
+ text: "replace me",
+ isStreaming: false,
+ createdAt,
+ updatedAt: createdAt,
+ });
+ yield* repository.upsert({
+ messageId,
+ threadId,
+ turnId: null,
+ role: "assistant",
+ text: "",
+ isStreaming: false,
+ createdAt,
+ updatedAt: "2026-09-01T17:10:01.000Z",
+ });
+
+ const result = yield* repository.getByMessageId({ messageId });
+ assert.equal(result._tag, "Some");
+ if (result._tag === "Some") {
+ assert.equal(result.value.text, "");
+ }
+ }),
+ );
+
+ it.effect("appends streaming text without replacing Akeru message metadata", () =>
+ Effect.gen(function* () {
+ const repository = yield* ProjectionThreadMessageRepository;
+ const threadId = ThreadId.make("thread-streaming-append");
+ const messageId = MessageId.make("message-streaming-append");
+ const respondingBotId = BotId.make("bot-streaming-append");
+ const createdAt = "2026-09-01T18:00:00.000Z";
+ const channelOrigin = {
+ provider: "telegram" as const,
+ externalThreadId: "telegram-chat-streaming",
+ externalSenderId: "telegram-user-streaming",
+ };
+ const attachments = [
+ {
+ type: "image" as const,
+ id: "streaming-attachment-1",
+ name: "streaming.png",
+ mimeType: "image/png",
+ sizeBytes: 5,
+ },
+ ];
+
+ yield* repository.appendStreaming({
+ messageId,
+ threadId,
+ turnId: null,
+ respondingBotId,
+ channelOrigin,
+ role: "assistant",
+ text: "hello",
+ attachments,
+ createdAt,
+ updatedAt: createdAt,
+ });
+
+ const initial = yield* repository.getByMessageId({ messageId });
+ assert.equal(initial._tag, "Some");
+ if (initial._tag === "Some") {
+ yield* repository.upsert({
+ ...initial.value,
+ reactions: [{ botId: respondingBotId, emoji: "eyes" }],
+ });
+ }
+
+ const updatedAt = "2026-09-01T18:00:01.000Z";
+ yield* repository.appendStreaming({
+ messageId,
+ threadId,
+ turnId: null,
+ role: "assistant",
+ text: " world",
+ createdAt: updatedAt,
+ updatedAt,
+ });
+
+ const result = yield* repository.getByMessageId({ messageId });
+ assert.equal(result._tag, "Some");
+ if (result._tag === "Some") {
+ assert.equal(result.value.text, "hello world");
+ assert.equal(result.value.respondingBotId, respondingBotId);
+ assert.deepEqual(result.value.channelOrigin, channelOrigin);
+ assert.deepEqual(result.value.attachments, attachments);
+ assert.deepEqual(result.value.reactions, [{ botId: respondingBotId, emoji: "eyes" }]);
+ assert.isTrue(result.value.isStreaming);
+ assert.equal(result.value.createdAt, createdAt);
+ assert.equal(result.value.updatedAt, updatedAt);
+ }
+ }),
+ );
+
it.effect("preserves existing attachments when upsert omits attachments", () =>
Effect.gen(function* () {
const repository = yield* ProjectionThreadMessageRepository;
diff --git a/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts b/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts
index c50bca708..9dcde65d0 100644
--- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts
+++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts
@@ -13,6 +13,7 @@ import {
import { toPersistenceSqlError } from "../Errors.ts";
import {
+ AppendStreamingProjectionThreadMessage,
GetProjectionThreadMessageInput,
ProjectionThreadMessageRepository,
type ProjectionThreadMessageRepositoryShape,
@@ -112,7 +113,13 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
projection_thread_messages.channel_origin_json
),
role = excluded.role,
- text = excluded.text,
+ text = CASE
+ WHEN projection_thread_messages.is_streaming = 1
+ AND excluded.is_streaming = 0
+ AND excluded.text = ''
+ THEN projection_thread_messages.text
+ ELSE excluded.text
+ END,
attachments_json = COALESCE(
excluded.attachments_json,
projection_thread_messages.attachments_json
@@ -125,6 +132,76 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
},
});
+ const appendStreamingProjectionThreadMessageRow = SqlSchema.void({
+ Request: AppendStreamingProjectionThreadMessage,
+ execute: (row) => {
+ const nextAttachmentsJson =
+ row.attachments !== undefined ? JSON.stringify(row.attachments) : null;
+ return sql`
+ INSERT INTO projection_thread_messages (
+ message_id,
+ thread_id,
+ turn_id,
+ responding_bot_id,
+ author_person_id,
+ author_display_name,
+ channel_origin_json,
+ role,
+ text,
+ attachments_json,
+ reactions_json,
+ is_streaming,
+ created_at,
+ updated_at
+ )
+ VALUES (
+ ${row.messageId},
+ ${row.threadId},
+ ${row.turnId},
+ ${row.respondingBotId ?? null},
+ ${row.authorPersonId ?? null},
+ ${row.authorDisplayName ?? null},
+ ${row.channelOrigin == null ? null : JSON.stringify(row.channelOrigin)},
+ ${row.role},
+ ${row.text},
+ ${nextAttachmentsJson},
+ '[]',
+ 1,
+ ${row.createdAt},
+ ${row.updatedAt}
+ )
+ ON CONFLICT (message_id)
+ DO UPDATE SET
+ thread_id = excluded.thread_id,
+ turn_id = excluded.turn_id,
+ responding_bot_id = COALESCE(
+ excluded.responding_bot_id,
+ projection_thread_messages.responding_bot_id
+ ),
+ author_person_id = COALESCE(
+ excluded.author_person_id,
+ projection_thread_messages.author_person_id
+ ),
+ author_display_name = COALESCE(
+ excluded.author_display_name,
+ projection_thread_messages.author_display_name
+ ),
+ channel_origin_json = COALESCE(
+ excluded.channel_origin_json,
+ projection_thread_messages.channel_origin_json
+ ),
+ role = excluded.role,
+ text = projection_thread_messages.text || excluded.text,
+ attachments_json = COALESCE(
+ excluded.attachments_json,
+ projection_thread_messages.attachments_json
+ ),
+ is_streaming = 1,
+ updated_at = excluded.updated_at
+ `;
+ },
+ });
+
const getProjectionThreadMessageRow = SqlSchema.findOneOption({
Request: GetProjectionThreadMessageInput,
Result: ProjectionThreadMessageDbRowSchema,
@@ -191,6 +268,13 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
Effect.mapError(toPersistenceSqlError("ProjectionThreadMessageRepository.upsert:query")),
);
+ const appendStreaming: ProjectionThreadMessageRepositoryShape["appendStreaming"] = (row) =>
+ appendStreamingProjectionThreadMessageRow(row).pipe(
+ Effect.mapError(
+ toPersistenceSqlError("ProjectionThreadMessageRepository.appendStreaming:query"),
+ ),
+ );
+
const getByMessageId: ProjectionThreadMessageRepositoryShape["getByMessageId"] = (input) =>
getProjectionThreadMessageRow(input).pipe(
Effect.mapError(
@@ -216,6 +300,7 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
return {
upsert,
+ appendStreaming,
getByMessageId,
listByThreadId,
deleteByThreadId,
diff --git a/apps/server/src/persistence/Services/ProjectionThreadMessages.ts b/apps/server/src/persistence/Services/ProjectionThreadMessages.ts
index 995d2a866..f89271c4f 100644
--- a/apps/server/src/persistence/Services/ProjectionThreadMessages.ts
+++ b/apps/server/src/persistence/Services/ProjectionThreadMessages.ts
@@ -20,6 +20,7 @@ import {
} from "@t3tools/contracts";
import * as Schema from "effect/Schema";
import * as Context from "effect/Context";
+import * as Struct from "effect/Struct";
import type * as Option from "effect/Option";
import * as Effect from "effect/Effect";
@@ -43,6 +44,12 @@ export const ProjectionThreadMessage = Schema.Struct({
});
export type ProjectionThreadMessage = typeof ProjectionThreadMessage.Type;
+export const AppendStreamingProjectionThreadMessage = Schema.Struct(
+ Struct.omit(ProjectionThreadMessage.fields, ["isStreaming", "reactions"]),
+);
+export type AppendStreamingProjectionThreadMessage =
+ typeof AppendStreamingProjectionThreadMessage.Type;
+
export const ListProjectionThreadMessagesInput = Schema.Struct({
threadId: ThreadId,
});
@@ -71,6 +78,11 @@ export interface ProjectionThreadMessageRepositoryShape {
message: ProjectionThreadMessage,
) => Effect.Effect<void, ProjectionRepositoryError>;
+ /** Insert a streaming message or append text to its existing row. */
+ readonly appendStreaming: (
+ message: AppendStreamingProjectionThreadMessage,
+ ) => Effect.Effect<void, ProjectionRepositoryError>;
+
/**
* Read a projected thread message by id.
*/
diff --git a/apps/server/src/project/RepositoryIdentityResolver.test.ts b/apps/server/src/project/RepositoryIdentityResolver.test.ts
index a997459e6..2b6534c46 100644
--- a/apps/server/src/project/RepositoryIdentityResolver.test.ts
+++ b/apps/server/src/project/RepositoryIdentityResolver.test.ts
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
+import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
import { TestClock } from "effect/testing";
import * as ProcessRunner from "../processRunner.ts";
@@ -35,6 +36,89 @@ const makeRepositoryIdentityResolverTestLayer = (options: {
).pipe(Layer.provide(ProcessRunner.layer));
it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
+ it.effect("reuses the cached Git root for repeated workspace lookups", () => {
+ const calls: Array<ReadonlyArray<string>> = [];
+ const processRunner = Layer.succeed(ProcessRunner.ProcessRunner, {
+ run: (input) =>
+ Effect.sync(() => {
+ calls.push(input.args);
+ return {
+ stdout: input.args.includes("rev-parse")
+ ? "/repo\n"
+ : "origin\[email protected]:OpenCoreDev/akeru-bot.git (fetch)\n",
+ stderr: "",
+ code: ChildProcessSpawner.ExitCode(0),
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ stdoutInvalidUtf8: false,
+ stderrInvalidUtf8: false,
+ };
+ }),
+ });
+ const resolverLayer = Layer.effect(
+ RepositoryIdentityResolver.RepositoryIdentityResolver,
+ RepositoryIdentityResolver.make(),
+ ).pipe(Layer.provide(processRunner));
+
+ return Effect.gen(function* () {
+ const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
+ const first = yield* resolver.resolve("/repo/packages/web");
+ const second = yield* resolver.resolve("/repo/packages/web");
+
+ expect(first?.canonicalKey).toBe("github.com/opencoredev/akeru-bot");
+ expect(second).toEqual(first);
+ expect(calls).toEqual([
+ ["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
+ ["-C", "/repo", "remote", "-v"],
+ ]);
+ }).pipe(Effect.provide(resolverLayer));
+ });
+
+ it.effect("retries Git root discovery after a failed lookup", () => {
+ const calls: Array<ReadonlyArray<string>> = [];
+ let rootAttempts = 0;
+ const processRunner = Layer.succeed(ProcessRunner.ProcessRunner, {
+ run: (input) =>
+ Effect.sync(() => {
+ calls.push(input.args);
+ const rootLookup = input.args.includes("rev-parse");
+ const failed = rootLookup && rootAttempts++ === 0;
+ return {
+ stdout: rootLookup
+ ? failed
+ ? ""
+ : "/repo\n"
+ : "origin\[email protected]:OpenCoreDev/akeru-bot.git (fetch)\n",
+ stderr: failed ? "temporary Git failure" : "",
+ code: ChildProcessSpawner.ExitCode(failed ? 1 : 0),
+ timedOut: false,
+ stdoutTruncated: false,
+ stderrTruncated: false,
+ stdoutInvalidUtf8: false,
+ stderrInvalidUtf8: false,
+ };
+ }),
+ });
+ const resolverLayer = Layer.effect(
+ RepositoryIdentityResolver.RepositoryIdentityResolver,
+ RepositoryIdentityResolver.make(),
+ ).pipe(Layer.provide(processRunner));
+
+ return Effect.gen(function* () {
+ const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
+ expect(yield* resolver.resolve("/repo/packages/web")).toBeNull();
+
+ const recovered = yield* resolver.resolve("/repo/packages/web");
+ expect(recovered?.rootPath).toBe("/repo");
+ expect(calls).toEqual([
+ ["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
+ ["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
+ ["-C", "/repo", "remote", "-v"],
+ ]);
+ }).pipe(Effect.provide(resolverLayer));
+ });
+
it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
diff --git a/apps/server/src/project/RepositoryIdentityResolver.ts b/apps/server/src/project/RepositoryIdentityResolver.ts
index b162e5fff..b6322d469 100644
--- a/apps/server/src/project/RepositoryIdentityResolver.ts
+++ b/apps/server/src/project/RepositoryIdentityResolver.ts
@@ -90,7 +90,6 @@ function buildRepositoryIdentity(input: {
const resolveRepositoryIdentityCacheKey = Effect.fn("RepositoryIdentityResolver.resolveCacheKey")(
function* (cwd: string) {
const processRunner = yield* ProcessRunner.ProcessRunner;
- let cacheKey = cwd;
// git is a real executable on every platform — no cmd.exe shell mode, which
// would split paths containing spaces during cmd's re-tokenization.
@@ -102,15 +101,11 @@ const resolveRepositoryIdentityCacheKey = Effect.fn("RepositoryIdentityResolver.
})
.pipe(Effect.option);
if (topLevelResult._tag === "None" || topLevelResult.value.code !== 0) {
- return cacheKey;
+ return null;
}
const candidate = topLevelResult.value.stdout.trim();
- if (candidate.length > 0) {
- cacheKey = candidate;
- }
-
- return cacheKey;
+ return candidate.length > 0 ? candidate : null;
},
);
@@ -139,6 +134,22 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
options: RepositoryIdentityResolverOptions = {},
) {
const processRunner = yield* ProcessRunner.ProcessRunner;
+ const cacheCapacity = options.cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY;
+
+ const repositoryRootCache = yield* Cache.makeWith<string, string | null>(
+ (cwd) =>
+ resolveRepositoryIdentityCacheKey(cwd).pipe(
+ Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
+ ),
+ {
+ capacity: cacheCapacity,
+ timeToLive: Exit.match({
+ onSuccess: (value) =>
+ value === null ? Duration.zero : (options.positiveCacheTtl ?? DEFAULT_POSITIVE_CACHE_TTL),
+ onFailure: () => Duration.zero,
+ }),
+ },
+ );
const repositoryIdentityCache = yield* Cache.makeWith<string, RepositoryIdentity | null>(
(cacheKey) =>
@@ -146,7 +157,7 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
),
{
- capacity: options.cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY,
+ capacity: cacheCapacity,
timeToLive: Exit.match({
onSuccess: (value) =>
value === null
@@ -160,9 +171,8 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
const resolve: RepositoryIdentityResolver["Service"]["resolve"] = Effect.fn(
"RepositoryIdentityResolver.resolve",
)(function* (cwd) {
- const cacheKey = yield* resolveRepositoryIdentityCacheKey(cwd).pipe(
- Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
- );
+ const cacheKey = yield* Cache.get(repositoryRootCache, cwd);
+ if (cacheKey === null) return null;
return yield* Cache.get(repositoryIdentityCache, cacheKey);
});
diff --git a/apps/server/src/provider/acp/AcpRuntimeModel.test.ts b/apps/server/src/provider/acp/AcpRuntimeModel.test.ts
index dc0dd6e04..c2f7612ad 100644
--- a/apps/server/src/provider/acp/AcpRuntimeModel.test.ts
+++ b/apps/server/src/provider/acp/AcpRuntimeModel.test.ts
@@ -1,3 +1,10 @@
+import { it as effectIt } from "@effect/vitest";
+import * as Duration from "effect/Duration";
+import * as Effect from "effect/Effect";
+import * as Fiber from "effect/Fiber";
+import * as Option from "effect/Option";
+import * as Ref from "effect/Ref";
+import { TestClock } from "effect/testing";
import { describe, expect, it } from "vite-plus/test";
import type * as EffectAcpSchema from "effect-acp/schema";
@@ -11,10 +18,33 @@ import {
parseSessionUpdateEvent,
sessionUpdateIsReplay,
syntheticLoadSessionResponseFromInitialize,
+ waitForSessionLoadReplayIdle,
+ type SessionLoadGate,
type AcpToolCallState,
} from "./AcpRuntimeModel.ts";
describe("AcpRuntimeModel", () => {
+ effectIt.effect("finishes session replay at the configured idle deadline", () =>
+ Effect.gen(function* () {
+ yield* TestClock.setTime(0);
+ const gateRef = yield* Ref.make<Option.Option<SessionLoadGate>>(
+ Option.some({
+ active: true,
+ lastActivityAtMillis: 0,
+ idleGap: Duration.millis(101),
+ initializeResult: { protocolVersion: 1 },
+ }),
+ );
+ const result = yield* waitForSessionLoadReplayIdle({ gateRef }).pipe(Effect.forkChild);
+
+ yield* TestClock.adjust(Duration.millis(101));
+
+ expect((yield* Fiber.join(result))._meta).toMatchObject({
+ t3SessionLoadReady: "replay_idle",
+ });
+ }),
+ );
+
it("parses session mode state from typed ACP session setup responses", () => {
const modeState = parseSessionModeState({
sessionId: "session-1",
diff --git a/apps/server/src/provider/acp/AcpRuntimeModel.ts b/apps/server/src/provider/acp/AcpRuntimeModel.ts
index 2e561c4cc..e03647a65 100644
--- a/apps/server/src/provider/acp/AcpRuntimeModel.ts
+++ b/apps/server/src/provider/acp/AcpRuntimeModel.ts
@@ -589,7 +589,6 @@ export const waitForSessionLoadReplayIdle = (input: {
readonly gateRef: Ref.Ref<Option.Option<SessionLoadGate>>;
}): Effect.Effect<EffectAcpSchema.LoadSessionResponse, never> =>
Effect.gen(function* () {
- const pollInterval = Duration.millis(25);
while (true) {
const gate = yield* Ref.get(input.gateRef);
if (
@@ -602,8 +601,14 @@ export const waitForSessionLoadReplayIdle = (input: {
if (nowMillis - gate.value.lastActivityAtMillis >= idleGapMillis) {
return syntheticLoadSessionResponseFromInitialize(gate.value.initializeResult);
}
+ yield* Effect.sleep(
+ Duration.millis(
+ Math.max(1, idleGapMillis - (nowMillis - gate.value.lastActivityAtMillis)),
+ ),
+ );
+ continue;
}
- yield* Effect.sleep(pollInterval);
+ yield* Effect.sleep(Duration.millis(25));
}
});
diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts
index c20f031a3..03aa7fced 100644
--- a/apps/server/src/server.test.ts
+++ b/apps/server/src/server.test.ts
@@ -19,6 +19,7 @@ import {
MessageId,
ExternalLauncherCommandNotFoundError,
OrchestrationThreadDetailSnapshot,
+ type OrchestrationThreadActivity,
type OrchestrationThreadStreamItem,
type OrchestrationThreadShell,
TerminalNotRunningError,
@@ -195,6 +196,38 @@ const defaultModelSelection = {
instanceId: ProviderInstanceId.make("codex"),
model: "gpt-5-codex",
} as const;
+
+const makeLiveToolActivityEvent = (
+ sequence: number,
+): Extract<OrchestrationEvent, { type: "thread.activity-appended" }> => {
+ const activity: OrchestrationThreadActivity = {
+ id: EventId.make(`activity-${sequence}`),
+ tone: "tool",
+ kind: "tool.updated",
+ summary: "Editing app.ts",
+ payload: {
+ itemType: "file_change",
+ title: "Editing app.ts",
+ data: { toolCallId: "call-edit", path: "src/app.ts" },
+ },
+ turnId: TurnId.make("turn-edit"),
+ createdAt: "2026-01-01T00:00:01.000Z",
+ };
+ return {
+ sequence,
+ eventId: EventId.make(`event-tool-${sequence}`),
+ aggregateKind: "thread",
+ aggregateId: defaultThreadId,
+ occurredAt: "2026-01-01T00:00:01.000Z",
+ commandId: null,
+ causationEventId: null,
+ correlationId: null,
+ metadata: {},
+ type: "thread.activity-appended",
+ payload: { threadId: defaultThreadId, activity },
+ };
+};
+
const testEnvironmentDescriptor = {
environmentId: EnvironmentId.make("environment-test"),
label: "Test environment",
@@ -4984,6 +5017,46 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
}).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
);
+ it.effect("coalesces buffered live tool updates before websocket delivery", () =>
+ Effect.gen(function* () {
+ const thread = makeDefaultOrchestrationReadModel().threads[0]!;
+ const liveEvents = yield* PubSub.unbounded<OrchestrationEvent>();
+
+ yield* buildAppUnderTest({
+ layers: {
+ orchestrationEngine: {
+ streamDomainEvents: Stream.fromPubSub(liveEvents),
+ },
+ projectionSnapshotQuery: {
+ getThreadDetailSnapshot: () =>
+ Effect.gen(function* () {
+ yield* Effect.sleep("25 millis");
+ yield* PubSub.publishAll(liveEvents, [
+ makeLiveToolActivityEvent(2),
+ makeLiveToolActivityEvent(3),
+ makeLiveToolActivityEvent(4),
+ ]);
+ return Option.some({ snapshotSequence: 1, thread });
+ }),
+ },
+ },
+ });
+
+ const wsUrl = yield* getWsServerUrl("/ws");
+ const items = yield* Effect.scoped(
+ withWsRpcClient(wsUrl, (client) =>
+ client[ORCHESTRATION_WS_METHODS.subscribeThread]({
+ threadId: defaultThreadId,
+ }).pipe(Stream.take(2), Stream.runCollect),
+ ),
+ ).pipe(Effect.timeout("2 seconds"));
+
+ assert.equal(items[0]?.kind, "snapshot");
+ assert.equal(items[1]?.kind, "event");
+ assert.equal(items[1]?.kind === "event" ? items[1].event.sequence : null, 4);
+ }).pipe(Effect.provide(NodeHttpServer.layerTest), TestClock.withLive),
+ );
+
it.effect("subscribeThread sends a fresh snapshot instead of replaying a large gap", () =>
Effect.gen(function* () {
let readEventsCalls = 0;
diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts
index d6497bac4..7c5a83c77 100644
--- a/apps/server/src/ws.ts
+++ b/apps/server/src/ws.ts
@@ -38,7 +38,6 @@ import {
type OrchestrationEvent,
type OrchestrationShellStreamEvent,
type OrchestrationShellStreamItem,
- type OrchestrationThreadStreamItem,
isGroupBotMember,
OrchestrationGetFullThreadDiffError,
OrchestrationGetSnapshotError,
@@ -99,6 +98,7 @@ import {
projectActivityEvent,
projectThreadDetailSnapshot,
} from "./orchestration/ActivityPayloadProjection.ts";
+import { makeThreadLiveEventCoalescer } from "./orchestration/ThreadLiveEventCoalescer.ts";
import {
applyAuthenticatedCommandActor,
applyKnownGroupPerson,
@@ -1981,17 +1981,15 @@ const makeWsRpcLayer = (
Stream.filter(isThisThreadDetailEvent),
Stream.map((event) => ({
kind: "event" as const,
- event: projectActivityEvent(event),
+ event,
})),
);
// Attach live delivery before reading either replay or snapshot state.
// Otherwise an event published while the snapshot is loading is lost.
- const liveBuffer = yield* Queue.unbounded<OrchestrationThreadStreamItem>();
- yield* Effect.forkScoped(
- liveStream.pipe(Stream.runForEach((item) => Queue.offer(liveBuffer, item))),
- );
- const bufferedLiveStream = Stream.fromQueue(liveBuffer);
+ const liveBuffer = yield* makeThreadLiveEventCoalescer();
+ yield* Effect.forkScoped(liveStream.pipe(Stream.runForEach(liveBuffer.offer)));
+ const bufferedLiveStream = liveBuffer.stream;
// When the client already loaded the snapshot over HTTP it passes
// that snapshot's sequence, and we resume the live subscription by
@@ -2040,8 +2038,10 @@ const makeWsRpcLayer = (
input.requestCompletionMarker === true
? Stream.concat(
Stream.fromEffect(
- Queue.offer(liveBuffer, { kind: "synchronized" as const }),
- ).pipe(Stream.drain),
+ liveBuffer
+ .offerAndWait({ kind: "synchronized" as const })
+ .pipe(Effect.andThen(liveBuffer.takeAll)),
+ ).pipe(Stream.flatMap((items) => Stream.fromIterable(items))),
bufferedLiveStream,
)
: bufferedLiveStream;
@@ -2082,8 +2082,10 @@ const makeWsRpcLayer = (
input.requestCompletionMarker === true
? Stream.concat(
Stream.fromEffect(
- Queue.offer(liveBuffer, { kind: "synchronized" as const }),
- ).pipe(Stream.drain),
+ liveBuffer
+ .offerAndWait({ kind: "synchronized" as const })
+ .pipe(Effect.andThen(liveBuffer.takeAll)),
+ ).pipe(Stream.flatMap((items) => Stream.fromIterable(items))),
bufferedLiveStream,
)
: bufferedLiveStream;