From 03551678cfc171c76331b66a43a325e61a393318 Mon Sep 17 00:00:00 2001 From: Badcuban <108198679+badcuban@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:44:17 -0500 Subject: [PATCH] perf(server): streaming replies no longer flood the server with garbage While agents streamed, the server did work per update that grew with the thread: Claude's per-request "requesting" status became a stored session event each time (about 1,700 in one long reply), and each made two reactors load the whole thread; a finished agent message loaded the whole thread too; every update read the whole message text back to append to it; phone push alerts woke on every event; and every traced span was built into a record before the filter dropped most of them. - Session updates that change nothing but their time are not stored. The idle-signal check compares with the latest unstored report, and errors are always stored, so turn state and unread failures behave as before. - A finished agent message reads that one message and asks whether its turn has a reply (ProjectionSnapshotQuery.getThreadMessage, hasAssistantMessageForTurn). - Streamed words are appended in SQL (appendStreamedText); an agent's reply no longer refreshes the thread's shell summary. - Push alerts wake only for events that can start or end an alert. - The trace filter decides from a cheap summary before a record is built. - Streaming markdown batching moved to streamingMarkdownFlush.ts and now carries open code blocks, tables and line starts across batches. A code fence split between batches no longer holds a whole code block back until it ends, and code no longer holds text on `[` or `*`. Measured on replayed sessions: garbage per streamed update 17.6 to 3.9 MB on a long working reply (full collections in its first 48 s: 14 to 3), and 1.85 to 1.26 MB with five short replies. Finished replies are identical to main's. --- .../Layers/CheckpointDiffQuery.test.ts | 12 + .../src/observability/Layers/Observability.ts | 16 +- .../Layers/OrchestrationEngine.test.ts | 2 + .../Layers/ProjectionPipeline.ts | 26 +- .../Layers/ProjectionSnapshotQuery.test.ts | 46 ++ .../Layers/ProjectionSnapshotQuery.ts | 65 +- .../Layers/ProviderRuntimeIngestion.test.ts | 101 +++ .../Layers/ProviderRuntimeIngestion.ts | 590 +++++------------- .../PullRequestAutomationWatcher.test.ts | 2 + .../Layers/ThreadAutoArchiveSweeper.test.ts | 2 + .../ThreadDiffStatBaselineReactor.test.ts | 2 + .../Services/ProjectionSnapshotQuery.ts | 17 + .../streamingMarkdownFlush.test.ts | 73 +++ .../orchestration/streamingMarkdownFlush.ts | 490 +++++++++++++++ .../Layers/ProjectionThreadMessages.test.ts | 43 ++ .../Layers/ProjectionThreadMessages.ts | 47 +- .../Services/ProjectionThreadMessages.ts | 9 + .../Layers/ProjectSetupScriptRunner.test.ts | 2 + .../Layers/ProviderSessionReaper.test.ts | 2 + .../ThreadContextSeedBuilder.test.ts | 4 + apps/server/src/pushAlerts/PushAlerts.ts | 9 +- apps/server/src/server.test.ts | 4 + apps/server/src/serverRuntimeStartup.test.ts | 6 + packages/shared/src/observability.ts | 44 +- 24 files changed, 1132 insertions(+), 482 deletions(-) create mode 100644 apps/server/src/orchestration/streamingMarkdownFlush.test.ts create mode 100644 apps/server/src/orchestration/streamingMarkdownFlush.ts diff --git a/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts b/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts index da3e1b227..2a8ef0a22 100644 --- a/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts +++ b/apps/server/src/checkpointing/Layers/CheckpointDiffQuery.test.ts @@ -135,6 +135,8 @@ describe("CheckpointDiffQueryLive", () => { listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), ), @@ -235,6 +237,8 @@ describe("CheckpointDiffQueryLive", () => { listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), ), @@ -367,6 +371,8 @@ describe("CheckpointDiffQueryLive", () => { listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), ), @@ -449,6 +455,8 @@ describe("CheckpointDiffQueryLive", () => { listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), ), @@ -527,6 +535,8 @@ describe("CheckpointDiffQueryLive", () => { listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), ), @@ -588,6 +598,8 @@ describe("CheckpointDiffQueryLive", () => { listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), ), diff --git a/apps/server/src/observability/Layers/Observability.ts b/apps/server/src/observability/Layers/Observability.ts index 29978141f..08f63c896 100644 --- a/apps/server/src/observability/Layers/Observability.ts +++ b/apps/server/src/observability/Layers/Observability.ts @@ -1,7 +1,7 @@ import { makeLocalFileTracer, makeTraceSink, - type TraceRecord, + type TraceRecordHead, } from "@threadlines/shared/observability"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; @@ -28,7 +28,7 @@ const PROVIDER_MESSAGE_TRACE_SAMPLE_MODULO = 100; // 4.1.2 a transaction's BEGIN, COMMIT and the like are spans of their own, so // any span the SQL client tags with its database is sampled too. const sampledSqlSpanNames = new Set(["sqlite", "sql.execute", "sql.transaction"]); -const isSqlSpan = (record: TraceRecord): boolean => +const isSqlSpan = (record: TraceRecordHead): boolean => sampledSqlSpanNames.has(record.name) || record.attributes["db.system.name"] !== undefined; const sampledProjectionSpanNames = new Set([ "applyPendingApprovalsProjection", @@ -70,11 +70,11 @@ function traceSampleHash(input: string): number { return hash >>> 0; } -function shouldKeepSampledTraceRecord(record: TraceRecord, modulo: number): boolean { +function shouldKeepSampledTraceRecord(record: TraceRecordHead, modulo: number): boolean { return traceSampleHash(`${record.traceId}:${record.spanId}:${record.name}`) % modulo === 0; } -function hasWarningOrErrorEvent(record: TraceRecord): boolean { +function hasWarningOrErrorEvent(record: TraceRecordHead): boolean { return record.events.some((event) => { const level = event.attributes["effect.logLevel"]; if (typeof level !== "string") { @@ -90,12 +90,16 @@ function hasWarningOrErrorEvent(record: TraceRecord): boolean { }); } -export function shouldRecordServerLocalTrace(record: TraceRecord): boolean { +export function shouldRecordServerLocalTrace(record: TraceRecordHead): boolean { if (record.durationMs >= SLOW_TRACE_RECORD_THRESHOLD_MS || hasWarningOrErrorEvent(record)) { return true; } - if (record.type === "effect-span" && record.exit._tag !== "Success") { + if ( + record.type === "effect-span" && + record.exit !== undefined && + record.exit._tag !== "Success" + ) { return true; } diff --git a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts index 837284fc2..823b44e05 100644 --- a/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts +++ b/apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts @@ -232,6 +232,8 @@ describe("OrchestrationEngine", () => { listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), ), diff --git a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts index 6b5fcc7b2..700915705 100644 --- a/apps/server/src/orchestration/Layers/ProjectionPipeline.ts +++ b/apps/server/src/orchestration/Layers/ProjectionPipeline.ts @@ -1340,6 +1340,12 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti } : {}), }); + // An agent's reply changes nothing the summary counts (the user's + // last message, plans, prompts, approvals), and while it streams it + // arrives many times a second. + if (event.type === "thread.message-sent" && event.payload.role === "assistant") { + return; + } yield* refreshThreadShellSummary(event.payload.threadId); return; } @@ -1583,16 +1589,19 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti } case "thread.message-sent": { - const existingMessage = yield* projectionThreadMessageRepository.getByMessageId({ - messageId: event.payload.messageId, - }); + // A streaming reply's words are added to the stored text where it + // lies, never read back: a long reply would otherwise be copied out + // and back in full every time a few words arrive. + const streamedOnly = event.payload.streaming; + const existingMessage = streamedOnly + ? Option.none() + : 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; } @@ -1606,7 +1615,10 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti }) : previousMessage?.attachments; const nextSkills = event.payload.skills ?? previousMessage?.skills; - yield* projectionThreadMessageRepository.upsert({ + const write = streamedOnly + ? projectionThreadMessageRepository.appendStreamedText + : projectionThreadMessageRepository.upsert; + yield* write({ messageId: event.payload.messageId, eventSequence: previousMessage?.eventSequence ?? event.sequence, threadId: event.payload.threadId, diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts index 1c972d6ed..a4ff70399 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts @@ -1807,6 +1807,52 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => { }), ); + it.effect("reads one message of a thread and whether a turn has a reply", () => + Effect.gen(function* () { + const snapshotQuery = yield* ProjectionSnapshotQuery; + const sql = yield* SqlClient.SqlClient; + + yield* sql`DELETE FROM projection_thread_messages`; + for (const [messageId, threadId, turnId, role] of [ + ["one-user", "thread-one", "turn-a", "user"], + ["one-reply", "thread-one", "turn-a", "assistant"], + ["one-question", "thread-one", "turn-b", "user"], + ["other-reply", "thread-other", "turn-b", "assistant"], + ]) { + yield* sql` + INSERT INTO projection_thread_messages ( + message_id, thread_id, turn_id, role, text, is_streaming, created_at, updated_at + ) VALUES ( + ${messageId}, ${threadId}, ${turnId}, ${role}, ${`${messageId} text`}, 0, + '2026-04-01T00:00:00.000Z', '2026-04-01T00:00:00.000Z' + ) + `; + } + const threadId = ThreadId.make("thread-one"); + + const reply = yield* snapshotQuery.getThreadMessage({ + threadId, + messageId: asMessageId("one-reply"), + }); + assert.equal(reply._tag === "Some" ? reply.value.text : null, "one-reply text"); + // Another thread's message is not this thread's. + const elsewhere = yield* snapshotQuery.getThreadMessage({ + threadId, + messageId: asMessageId("other-reply"), + }); + assert.equal(elsewhere._tag, "None"); + + assert.equal( + yield* snapshotQuery.hasAssistantMessageForTurn({ threadId, turnId: asTurnId("turn-a") }), + true, + ); + assert.equal( + yield* snapshotQuery.hasAssistantMessageForTurn({ threadId, turnId: asTurnId("turn-b") }), + false, + ); + }), + ); + it.effect("hydrates thread messages into the command read model", () => Effect.gen(function* () { const snapshotQuery = yield* ProjectionSnapshotQuery; diff --git a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts index b11f24947..d2eff010c 100644 --- a/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts @@ -1940,12 +1940,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { `, }); - const listThreadMessageRowsByThread = SqlSchema.findAll({ - Request: ThreadIdLookupInput, - Result: ProjectionThreadMessageDbRowSchema, - execute: ({ threadId }) => - sql` - SELECT + /** Every column a thread message row is read with. */ + const threadMessageColumns = sql` message_id AS "messageId", event_sequence AS "eventSequence", thread_id AS "threadId", @@ -1974,12 +1970,43 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { is_streaming AS "isStreaming", created_at AS "createdAt", updated_at AS "updatedAt" + `; + + const listThreadMessageRowsByThread = SqlSchema.findAll({ + Request: ThreadIdLookupInput, + Result: ProjectionThreadMessageDbRowSchema, + execute: ({ threadId }) => + sql` + SELECT ${threadMessageColumns} FROM projection_thread_messages WHERE thread_id = ${threadId} ORDER BY event_sequence ASC, created_at ASC, message_id ASC `, }); + const getThreadMessageRow = SqlSchema.findOneOption({ + Request: Schema.Struct({ threadId: ThreadId, messageId: MessageId }), + Result: ProjectionThreadMessageDbRowSchema, + execute: ({ threadId, messageId }) => + sql` + SELECT ${threadMessageColumns} + FROM projection_thread_messages + WHERE thread_id = ${threadId} AND message_id = ${messageId} + `, + }); + + const getAssistantMessageIdForTurnRow = SqlSchema.findOneOption({ + Request: Schema.Struct({ threadId: ThreadId, turnId: TurnId }), + Result: Schema.Struct({ messageId: MessageId }), + execute: ({ threadId, turnId }) => + sql` + SELECT message_id AS "messageId" + FROM projection_thread_messages + WHERE thread_id = ${threadId} AND turn_id = ${turnId} AND role = 'assistant' + LIMIT 1 + `, + }); + // Workflow runs: the run is stored as JSON (see ProjectionThreadWorkflowRuns). const listThreadWorkflowRunRows = SqlSchema.findAll({ Request: Schema.Void, @@ -3886,6 +3913,30 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { ); }); + const getThreadMessage: ProjectionSnapshotQueryShape["getThreadMessage"] = (input) => + getThreadMessageRow(input).pipe( + Effect.map(Option.map(mapThreadMessageRow)), + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.getThreadMessage:query", + "ProjectionSnapshotQuery.getThreadMessage:decodeRow", + ), + ), + ); + + const hasAssistantMessageForTurn: ProjectionSnapshotQueryShape["hasAssistantMessageForTurn"] = ( + input, + ) => + getAssistantMessageIdForTurnRow(input).pipe( + Effect.map(Option.isSome), + Effect.mapError( + toPersistenceSqlOrDecodeError( + "ProjectionSnapshotQuery.hasAssistantMessageForTurn:query", + "ProjectionSnapshotQuery.hasAssistantMessageForTurn:decodeRow", + ), + ), + ); + const getTurnActivities: ProjectionSnapshotQueryShape["getTurnActivities"] = (input) => listThreadActivityRowsByTurn(input).pipe( Effect.map((rows) => rows.map(mapThreadActivityRow)), @@ -4118,6 +4169,8 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () { getThreadShellById, getThreadDetailById, getTurnActivities, + getThreadMessage, + hasAssistantMessageForTurn, } satisfies ProjectionSnapshotQueryShape; }); diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts index 16ee4d302..dc5affd89 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts @@ -1198,6 +1198,70 @@ describe("ProviderRuntimeIngestion", () => { }); }); + it("does not let an idle signal older than an unstored running report settle the turn", async () => { + const harness = await createHarness(); + + harness.emit({ + type: "turn.started", + eventId: asEventId("evt-turn-started-before-repeat-running"), + provider: ProviderDriverKind.make("codex"), + threadId: asThreadId("thread-1"), + createdAt: "2026-01-01T00:00:01.000Z", + turnId: asTurnId("turn-repeat-running"), + }); + await waitForThread( + harness.readModel, + (thread) => thread.session?.activeTurnId === "turn-repeat-running", + ); + // Says nothing new, so it is not stored, but it is the latest word. + harness.emit({ + type: "session.state.changed", + eventId: asEventId("evt-repeat-running"), + provider: ProviderDriverKind.make("codex"), + threadId: asThreadId("thread-1"), + createdAt: "2026-01-01T00:00:03.000Z", + payload: { state: "running" }, + }); + harness.emit({ + type: "thread.state.changed", + eventId: asEventId("evt-idle-older-than-repeat"), + provider: ProviderDriverKind.make("codex"), + threadId: asThreadId("thread-1"), + createdAt: "2026-01-01T00:00:02.000Z", + payload: { state: "idle" }, + }); + await harness.drain(); + + const readModel = await harness.readModel(); + const thread = readModel.threads.find((entry) => entry.id === asThreadId("thread-1")); + expect(thread?.session).toMatchObject({ + status: "running", + activeTurnId: "turn-repeat-running", + }); + }); + + it("stores a repeated error, so its time says when it last failed", async () => { + const harness = await createHarness(); + const fail = (id: string, createdAt: string) => + harness.emit({ + type: "session.state.changed", + eventId: asEventId(id), + provider: ProviderDriverKind.make("codex"), + threadId: asThreadId("thread-1"), + createdAt, + payload: { state: "error", reason: "it broke" }, + }); + + fail("evt-error-first", "2026-01-01T00:00:01.000Z"); + await waitForThread(harness.readModel, (thread) => thread.session?.status === "error"); + fail("evt-error-again", "2026-01-01T00:00:03.000Z"); + const thread = await waitForThread( + harness.readModel, + (entry) => entry.session?.updatedAt === "2026-01-01T00:00:03.000Z", + ); + expect(thread.session?.status).toBe("error"); + }); + it("settles orphaned roster subagents when a fresh provider session starts", async () => { const harness = await createHarness(); @@ -2556,6 +2620,43 @@ describe("ProviderRuntimeIngestion", () => { ).toBe("running"); }); + it("stores no session update when a provider repeats the state it is in", async () => { + const harness = await createHarness(); + const report = (id: string, createdAt: string, state: "running" | "error") => + harness.emit({ + type: "session.state.changed", + eventId: asEventId(id), + provider: ProviderDriverKind.make("claudeAgent"), + threadId: asThreadId("thread-1"), + createdAt, + payload: { state, reason: state === "error" ? "it broke" : "status:requesting" }, + }); + + report("evt-repeat-running-1", "2026-01-01T00:00:01.000Z", "running"); + await waitForThread(harness.readModel, (entry) => entry.session?.status === "running"); + // Claude reports each request it makes; nothing changes with them. + report("evt-repeat-running-2", "2026-01-01T00:00:02.000Z", "running"); + report("evt-repeat-running-3", "2026-01-01T00:00:03.000Z", "running"); + report("evt-repeat-error", "2026-01-01T00:00:04.000Z", "error"); + await waitForThread(harness.readModel, (entry) => entry.session?.status === "error"); + + const sessionUpdates = Array.from( + await Effect.runPromise( + harness.engine.readEvents(0).pipe( + Stream.filter((event) => event.type === "thread.session-set"), + Stream.runCollect, + ), + ), + ) + .flatMap((event) => (event.type === "thread.session-set" ? [event.payload.session] : [])) + // The harness starts the thread with a session of its own. + .filter((session) => session.updatedAt >= "2026-01-01T00:00:01.000Z"); + expect(sessionUpdates.map((session) => [session.status, session.updatedAt])).toEqual([ + ["running", "2026-01-01T00:00:01.000Z"], + ["error", "2026-01-01T00:00:04.000Z"], + ]); + }); + it("applies provider session.state.changed transitions directly", async () => { const harness = await createHarness(); const waitingAt = "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 0a2e7460a..ac830dfc3 100644 --- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts @@ -8,6 +8,7 @@ import { MessageId, type OrchestrationCheckpointFile, type OrchestrationEvent, + type OrchestrationSession, type OrchestrationThreadActivity, type OrchestrationMessage, type OrchestrationProposedPlanId, @@ -77,6 +78,12 @@ import { childDeliveryForTurnEnd } from "../childThreadDelivery.ts"; import { RemoteRequestTurnEnds } from "../Services/RemoteRequestTurnEnds.ts"; import { ScheduledRunTurnEnds } from "../Services/ScheduledRunTurnEnds.ts"; import { RealtimeVoiceDevices } from "../Services/RealtimeVoiceDevices.ts"; +import { + splitStreamingAssistantMarkdownFlush, + STREAMING_MARKDOWN_START, + streamingMarkdownStateAfter, + type StreamingMarkdownState, +} from "../streamingMarkdownFlush.ts"; import { carriedBackgroundTasks, NO_BACKGROUND_TASKS, @@ -109,7 +116,6 @@ const STREAMING_ASSISTANT_DELTA_FLUSH_INTERVAL = Duration.millis( ); const SUBAGENT_RESULT_ACTIVITY_FLUSH_INTERVAL = Duration.millis(100); -const MARKDOWN_FENCE_INDENT_LIMIT = 3; type ContentDeltaStreamKind = Extract< ProviderRuntimeEvent, { type: "content.delta" } @@ -469,394 +475,31 @@ function maxCheckpointTurnCount( return maxTurnCount; } -function detectMarkdownFenceAtLineStart( - text: string, - index: number, -): { marker: "`" | "~"; length: number; end: number } | null { - if (index > 0 && text[index - 1] !== "\n") { - return null; - } - - let cursor = index; - let indent = 0; - while (text[cursor] === " " && indent < MARKDOWN_FENCE_INDENT_LIMIT + 1) { - cursor += 1; - indent += 1; - } - if (indent > MARKDOWN_FENCE_INDENT_LIMIT) { - return null; - } - - const marker = text[cursor]; - if (marker !== "`" && marker !== "~") { - return null; - } - - let end = cursor; - while (text[end] === marker) { - end += 1; - } - const length = end - cursor; - return length >= 3 ? { marker, length, end } : null; -} - -function isEscapedMarkdownCharacter(text: string, index: number): boolean { - let slashCount = 0; - for (let cursor = index - 1; cursor >= 0 && text[cursor] === "\\"; cursor -= 1) { - slashCount += 1; - } - return slashCount % 2 === 1; -} - -function isAsciiAlphaNumeric(char: string | undefined): boolean { - return char !== undefined && /^[A-Za-z0-9]$/u.test(char); -} - -function isMarkdownBoundaryBefore(char: string | undefined): boolean { - return ( - char === undefined || - /\s/u.test(char) || - char === "(" || - char === "[" || - char === "{" || - char === "<" || - char === '"' || - char === "'" - ); -} - -interface MarkdownLine { - readonly text: string; - readonly start: number; - readonly end: number; - readonly hasLineEnding: boolean; -} - -function splitMarkdownLines(text: string): MarkdownLine[] { - const lines: MarkdownLine[] = []; - let start = 0; - for (let index = 0; index < text.length; index += 1) { - if (text[index] !== "\n") { - continue; - } - lines.push({ - text: text.slice(start, index), - start, - end: index, - hasLineEnding: true, - }); - start = index + 1; - } - lines.push({ - text: text.slice(start), - start, - end: text.length, - hasLineEnding: false, - }); - return lines; -} - -function isSyntheticTrailingMarkdownLine(line: MarkdownLine, sourceText: string): boolean { - return !line.hasLineEnding && line.start === sourceText.length && line.text.length === 0; -} - -function stripOuterTablePipes(line: string): string { - let value = line.trim(); - if (value.startsWith("|")) { - value = value.slice(1); - } - if (value.endsWith("|")) { - value = value.slice(0, -1); - } - return value; -} - -function isPotentialMarkdownTableRow(line: string): boolean { - const trimmed = line.trim(); - if (!trimmed || trimmed.startsWith(">")) { - return false; - } - if (trimmed.startsWith("|")) { - return stripOuterTablePipes(trimmed).includes("|"); - } - const firstPipe = trimmed.indexOf("|"); - return firstPipe >= 0 && trimmed.indexOf("|", firstPipe + 1) >= 0; -} - -function isMarkdownTableSeparatorRow(line: string): boolean { - if (!isPotentialMarkdownTableRow(line)) { - return false; - } - const cells = stripOuterTablePipes(line) - .split("|") - .map((cell) => cell.trim()); - return cells.length >= 2 && cells.every((cell) => /^:?-{3,}:?$/u.test(cell)); -} - -function findPendingMarkdownTableStart( - text: string, - options?: { readonly continuingTable?: boolean }, -): number | null { - const lines = splitMarkdownLines(text); - let tableActive = options?.continuingTable === true; - - for (let index = 0; index < lines.length; index += 1) { - const line = lines[index]!; - const nextLine = lines[index + 1]; - if (isSyntheticTrailingMarkdownLine(line, text)) { - break; - } - - if (tableActive) { - if (!isPotentialMarkdownTableRow(line.text)) { - tableActive = false; - continue; - } - if (!line.hasLineEnding) { - return line.start; - } - continue; - } - - if (!isPotentialMarkdownTableRow(line.text)) { - continue; - } - - if (!nextLine) { - return line.start; - } - if (!nextLine.hasLineEnding && !isMarkdownTableSeparatorRow(nextLine.text)) { - return line.start; - } - if (!isMarkdownTableSeparatorRow(nextLine.text)) { - continue; - } - if (!nextLine.hasLineEnding) { - return line.start; - } - - tableActive = true; - index += 1; - } - - return null; -} - -function markdownTableContinuesAfterFlush( - text: string, - options?: { readonly continuingTable?: boolean }, +/** + * Whether a session update would leave `current` as it is apart from its + * time: every field the update sets already holds the same value there. + */ +function sessionUpdateChangesNothing( + current: OrchestrationSession | null | undefined, + next: Partial, ): boolean { - const lines = splitMarkdownLines(text); - let tableActive = options?.continuingTable === true; - - for (let index = 0; index < lines.length; index += 1) { - const line = lines[index]!; - if (isSyntheticTrailingMarkdownLine(line, text)) { - break; - } - - if (tableActive) { - if (isPotentialMarkdownTableRow(line.text)) { - continue; - } - tableActive = false; - continue; - } - - if (!isPotentialMarkdownTableRow(line.text)) { - continue; - } - - const nextLine = lines[index + 1]; - if ( - nextLine && - !isSyntheticTrailingMarkdownLine(nextLine, text) && - isMarkdownTableSeparatorRow(nextLine.text) - ) { - tableActive = true; - index += 1; - } - } - - return tableActive; -} - -function findClosingMarkdownParenthesis(text: string, startIndex: number): number | null { - let depth = 0; - for (let index = startIndex; index < text.length; index += 1) { - const char = text[index]; - if (isEscapedMarkdownCharacter(text, index)) { - continue; - } - if (char === "(") { - depth += 1; - continue; - } - if (char !== ")") { - continue; - } - if (depth === 0) { - return index; - } - depth -= 1; - } - return null; -} - -function findUnclosedInlineCodeSpanStart(text: string): number | null { - let inlineCode: { start: number; tickCount: number } | null = null; - let codeFence: { marker: "`" | "~"; length: number } | null = null; - - for (let index = 0; index < text.length; index += 1) { - if (inlineCode === null) { - const fence = detectMarkdownFenceAtLineStart(text, index); - if (fence) { - if (codeFence === null) { - codeFence = { marker: fence.marker, length: fence.length }; - } else if (codeFence.marker === fence.marker && fence.length >= codeFence.length) { - codeFence = null; - } - index = fence.end - 1; - continue; - } - } - - if (codeFence !== null) { - continue; - } - - const char = text[index]; - if (char === "\\") { - index += 1; - continue; - } - if (char !== "`") { - continue; - } - - const runStart = index; - let runEnd = index; - while (text[runEnd] === "`") { - runEnd += 1; - } - const tickCount = runEnd - runStart; - - if (inlineCode && tickCount === inlineCode.tickCount) { - inlineCode = null; - } else if (inlineCode === null) { - inlineCode = { start: runStart, tickCount }; - } - - index = runEnd - 1; - } - - return inlineCode?.start ?? null; -} - -function findUnclosedLinkStart(text: string): number | null { - const labelStack: number[] = []; - - for (let index = 0; index < text.length; index += 1) { - const char = text[index]; - if (isEscapedMarkdownCharacter(text, index)) { - continue; - } - - if (char === "[") { - labelStack.push(text[index - 1] === "!" ? index - 1 : index); - continue; - } - - if (char !== "]" || labelStack.length === 0) { - continue; - } - - const labelStart = labelStack.pop()!; - const nextChar = text[index + 1]; - if (nextChar === "(") { - const closeIndex = findClosingMarkdownParenthesis(text, index + 2); - if (closeIndex === null) { - return labelStart; - } - index = closeIndex; - continue; - } - - if (nextChar === "[") { - const closeReferenceIndex = text.indexOf("]", index + 2); - if (closeReferenceIndex < 0) { - return labelStart; - } - index = closeReferenceIndex; - } + if (current === null || current === undefined) { + return false; } - - return labelStack[0] ?? null; -} - -function findUnclosedEmphasisStart(text: string): number | null { - const markerStack: Array<{ marker: "*" | "~"; length: 1 | 2; start: number }> = []; - - for (let index = 0; index < text.length; index += 1) { - const marker = text[index]; - if ((marker !== "*" && marker !== "~") || isEscapedMarkdownCharacter(text, index)) { - continue; - } - - let runEnd = index; - while (text[runEnd] === marker) { - runEnd += 1; + return Object.entries(next).every(([key, value]) => { + if (key === "updatedAt") { + return true; } - const runLength = runEnd - index; - const length = Math.min(runLength, 2) as 1 | 2; - const previousChar = text[index - 1]; - const nextChar = text[index + length]; - const canOpen = - isMarkdownBoundaryBefore(previousChar) && - isAsciiAlphaNumeric(nextChar) && - (marker === "*" || length === 2); - // Closing only needs a non-space character before the run (CommonMark's - // right-flanking rule). Requiring more — e.g. an alphanumeric — misses - // closers like "**done.**" or "**Note:**", leaving a phantom open span - // that wedges the streaming flush until the message completes. - const canClose = previousChar !== undefined && !/\s/u.test(previousChar); - const openIndex = markerStack.findLastIndex( - (entry) => entry.marker === marker && entry.length === length, + const held: unknown = current[key as keyof OrchestrationSession]; + return ( + held === value || + (typeof held === "object" && + held !== null && + typeof value === "object" && + value !== null && + JSON.stringify(held) === JSON.stringify(value)) ); - - if (openIndex >= 0 && canClose) { - markerStack.splice(openIndex, 1); - } else if (canOpen) { - markerStack.push({ marker, length, start: index }); - } - - index = runEnd - 1; - } - - return markerStack.length > 0 ? Math.min(...markerStack.map((entry) => entry.start)) : null; -} - -function splitStreamingAssistantMarkdownFlush( - text: string, - options?: { readonly continuingTable?: boolean }, -): { - readyText: string; - pendingText: string; -} { - const pendingStarts = [ - findUnclosedInlineCodeSpanStart(text), - findUnclosedLinkStart(text), - findUnclosedEmphasisStart(text), - findPendingMarkdownTableStart(text, options), - ].filter((start): start is number => start !== null); - if (pendingStarts.length === 0) { - return { readyText: text, pendingText: "" }; - } - const pendingStart = Math.min(...pendingStarts); - return { - readyText: text.slice(0, pendingStart), - pendingText: text.slice(pendingStart), - }; + }); } function checkpointFileChangeActivity(input: { @@ -1326,7 +969,15 @@ const make = Effect.gen(function* () { const subagentResultFlushQueued = yield* Ref.make(false); const subagentResultFlushRequests = yield* Queue.unbounded(); const realtimeAssistantMessageIds = yield* Ref.make(new Map()); - const streamingAssistantTableMessageIds = yield* Ref.make(new Set()); + // When a provider last reported a session state that matched the stored one, + // for threads whose last reports were not stored (see the session update + // below). The stored session's own time is older then, and an idle signal + // must be compared with the latest report, not the latest stored one. + const unstoredSessionReportAt = yield* Ref.make(new Map()); + // What each streaming reply's sent batches left open (see streamingMarkdownFlush.ts). + const streamingMarkdownStateByMessageId = yield* Ref.make( + new Map(), + ); const assistantSegmentStateByTurnKey = yield* Cache.make({ capacity: TURN_MESSAGE_IDS_BY_TURN_CACHE_CAPACITY, @@ -1994,30 +1645,32 @@ const make = Effect.gen(function* () { return next; }); - const isStreamingAssistantTableActive = (messageId: MessageId) => - Ref.get(streamingAssistantTableMessageIds).pipe( - Effect.map((messageIds) => messageIds.has(messageId)), + const getStreamingMarkdownState = (messageId: MessageId) => + Ref.get(streamingMarkdownStateByMessageId).pipe( + Effect.map((states) => states.get(messageId) ?? STREAMING_MARKDOWN_START), ); - const setStreamingAssistantTableActive = (messageId: MessageId, active: boolean) => - Ref.update(streamingAssistantTableMessageIds, (messageIds) => { - if (active && messageIds.has(messageId)) { - return messageIds; - } - if (!active && !messageIds.has(messageId)) { - return messageIds; - } - const next = new Set(messageIds); - if (active) { - next.add(messageId); - } else { - next.delete(messageId); - } + /** Records that `sentText` went out for `messageId`, after what `state` describes. */ + const recordStreamingMarkdownSent = ( + messageId: MessageId, + state: StreamingMarkdownState, + sentText: string, + ) => + Ref.update(streamingMarkdownStateByMessageId, (states) => { + const next = new Map(states); + next.set(messageId, streamingMarkdownStateAfter(sentText, state)); return next; }); - const clearStreamingAssistantTableState = (messageId: MessageId) => - setStreamingAssistantTableActive(messageId, false); + const clearStreamingMarkdownState = (messageId: MessageId) => + Ref.update(streamingMarkdownStateByMessageId, (states) => { + if (!states.has(messageId)) { + return states; + } + const next = new Map(states); + next.delete(messageId); + return next; + }); const flushPendingStreamingAssistantMessage = ( input: PendingStreamingAssistantMessage, @@ -2025,10 +1678,11 @@ const make = Effect.gen(function* () { ) => Effect.gen(function* () { const bufferedText = yield* peekBufferedAssistantText(input.messageId); - const continuingTable = yield* isStreamingAssistantTableActive(input.messageId); - const { readyText, pendingText } = splitStreamingAssistantMarkdownFlush(bufferedText, { - continuingTable, - }); + const markdownState = yield* getStreamingMarkdownState(input.messageId); + const { readyText, pendingText } = splitStreamingAssistantMarkdownFlush( + bufferedText, + markdownState, + ); if ( !hasRenderableAssistantText(readyText) || (options?.onlyWithWords === true && !hasAssistantWords(readyText)) @@ -2037,10 +1691,7 @@ const make = Effect.gen(function* () { } yield* replaceBufferedAssistantText(input.messageId, pendingText); - yield* setStreamingAssistantTableActive( - input.messageId, - markdownTableContinuesAfterFlush(readyText, { continuingTable }), - ); + yield* recordStreamingMarkdownSent(input.messageId, markdownState, readyText); yield* dispatchAssistantDelta({ ...input, delta: readyText, @@ -2086,6 +1737,11 @@ const make = Effect.gen(function* () { const spillChunk = yield* appendBufferedAssistantText(input.messageId, input.delta); if (spillChunk.length > 0) { yield* clearPendingStreamingAssistantMessage(input.messageId); + yield* recordStreamingMarkdownSent( + input.messageId, + yield* getStreamingMarkdownState(input.messageId), + spillChunk, + ); yield* dispatchAssistantDelta({ ...input, delta: spillChunk, @@ -2193,7 +1849,7 @@ const make = Effect.gen(function* () { const clearAssistantMessageState = (messageId: MessageId) => clearPendingStreamingAssistantMessage(messageId).pipe( - Effect.andThen(clearStreamingAssistantTableState(messageId)), + Effect.andThen(clearStreamingMarkdownState(messageId)), Effect.andThen(clearBufferedAssistantText(messageId)), Effect.andThen(Cache.invalidate(streamedAssistantMessageIds, messageId)), ); @@ -3599,34 +3255,56 @@ const make = Effect.gen(function* () { ); } - yield* orchestrationEngine.dispatch({ - type: "thread.session.set", - commandId: providerCommandId(event, "thread-session-set"), + const session = { threadId: thread.id, - session: { + status, + providerName: event.provider, + ...(event.providerInstanceId !== undefined + ? { providerInstanceId: event.providerInstanceId } + : {}), + providerSessionId: thread.session?.providerSessionId ?? null, + providerThreadId: + event.type === "thread.started" + ? (event.payload?.providerThreadId ?? thread.session?.providerThreadId ?? null) + : (thread.session?.providerThreadId ?? null), + runtimeMode: thread.session?.runtimeMode ?? "full-access", + checkoutCwd: thread.session?.checkoutCwd ?? null, + activeTurnId: nextActiveTurnId, + ...(event.type === "session.started" || event.type === "session.exited" + ? NO_BACKGROUND_TASKS + : carriedBackgroundTasks(thread.session)), + lastError, + updatedAt: sessionUpdatedAt, + }; + // A provider repeats its state while a turn runs (Claude reports each + // request it makes, several a second), and an update that changes + // nothing but the time would still be stored, sent to every client + // and read by every reactor. + // An error is always stored: its time says whether the user has seen it. + if ( + status !== "error" && + failedUsageLimit === undefined && + sessionUpdateChangesNothing(thread.session, session) + ) { + yield* Ref.update(unstoredSessionReportAt, (reports) => + new Map(reports).set(thread.id, sessionUpdatedAt), + ); + } else { + yield* orchestrationEngine.dispatch({ + type: "thread.session.set", + commandId: providerCommandId(event, "thread-session-set"), threadId: thread.id, - status, - providerName: event.provider, - ...(event.providerInstanceId !== undefined - ? { providerInstanceId: event.providerInstanceId } - : {}), - providerSessionId: thread.session?.providerSessionId ?? null, - providerThreadId: - event.type === "thread.started" - ? (event.payload?.providerThreadId ?? thread.session?.providerThreadId ?? null) - : (thread.session?.providerThreadId ?? null), - runtimeMode: thread.session?.runtimeMode ?? "full-access", - checkoutCwd: thread.session?.checkoutCwd ?? null, - activeTurnId: nextActiveTurnId, - ...(event.type === "session.started" || event.type === "session.exited" - ? NO_BACKGROUND_TASKS - : carriedBackgroundTasks(thread.session)), - lastError, - updatedAt: sessionUpdatedAt, - }, - ...usageLimitFields, - createdAt: now, - }); + session, + ...usageLimitFields, + createdAt: now, + }); + yield* Ref.update(unstoredSessionReportAt, (reports) => { + if (!reports.has(thread.id)) return reports; + const next = new Map(reports); + next.delete(thread.id); + return next; + }); + } } } @@ -3634,12 +3312,21 @@ const make = Effect.gen(function* () { // turn/completed notification is lost under load, use a fresh idle // observation to settle the matching projected session instead of // leaving the UI's working timer running forever. + const latestSessionReportAt = (session: { readonly updatedAt: string }) => + Ref.get(unstoredSessionReportAt).pipe( + Effect.map((reports) => { + const unstored = reports.get(thread.id); + return unstored !== undefined && unstored > session.updatedAt + ? unstored + : session.updatedAt; + }), + ); if ( event.type === "thread.state.changed" && event.payload.state === "idle" && thread.session?.status === "running" && thread.session.activeTurnId !== null && - event.createdAt >= thread.session.updatedAt + event.createdAt >= (yield* latestSessionReportAt(thread.session)) ) { yield* orchestrationEngine.dispatch({ type: "thread.session.set", @@ -3922,27 +3609,34 @@ const make = Effect.gen(function* () { }); yield* Cache.set(settledSubagentResultActivityKeys, bufferKey, true); } else { - const detailedThread = yield* getLoadedThreadDetail(); - const messages = detailedThread?.messages ?? []; + // Every agent message that finishes lands here, many a turn while + // an agent works, so this reads the one message and asks one + // question of the turn instead of loading the thread's history. const turnId = toTurnId(event.turnId); const activeAssistantMessageId = turnId ? yield* getActiveAssistantMessageIdForTurn(thread.id, turnId) : Option.none(); - const hasAssistantMessagesForTurn = - turnId !== undefined ? hasAssistantMessageForTurn(messages, turnId) : false; const assistantMessageId = Option.getOrElse( activeAssistantMessageId, () => assistantCompletion.messageId, ); - const existingAssistantMessage = findMessageById(messages, assistantMessageId); + const existingAssistantMessage = Option.getOrUndefined( + yield* projectionSnapshotQuery.getThreadMessage({ + threadId: thread.id, + messageId: assistantMessageId, + }), + ); const shouldApplyFallbackCompletionText = !existingAssistantMessage || existingAssistantMessage.text.length === 0; const shouldSkipRedundantCompletion = Option.isNone(activeAssistantMessageId) && turnId !== undefined && - hasAssistantMessagesForTurn && - (assistantCompletion.fallbackText?.trim().length ?? 0) === 0; + (assistantCompletion.fallbackText?.trim().length ?? 0) === 0 && + (yield* projectionSnapshotQuery.hasAssistantMessageForTurn({ + threadId: thread.id, + turnId, + })); if (!shouldSkipRedundantCompletion) { if (turnId && Option.isNone(activeAssistantMessageId)) { diff --git a/apps/server/src/orchestration/Layers/PullRequestAutomationWatcher.test.ts b/apps/server/src/orchestration/Layers/PullRequestAutomationWatcher.test.ts index f5381e1f6..142c15e72 100644 --- a/apps/server/src/orchestration/Layers/PullRequestAutomationWatcher.test.ts +++ b/apps/server/src/orchestration/Layers/PullRequestAutomationWatcher.test.ts @@ -434,6 +434,8 @@ function makeSnapshotQuery(input: { return Option.fromUndefinedOr(input.readThreads().find((thread) => thread.id === threadId)); }), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }; } diff --git a/apps/server/src/orchestration/Layers/ThreadAutoArchiveSweeper.test.ts b/apps/server/src/orchestration/Layers/ThreadAutoArchiveSweeper.test.ts index 617355314..5f8485b7f 100644 --- a/apps/server/src/orchestration/Layers/ThreadAutoArchiveSweeper.test.ts +++ b/apps/server/src/orchestration/Layers/ThreadAutoArchiveSweeper.test.ts @@ -135,6 +135,8 @@ function makeSnapshotQuery( listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }; } diff --git a/apps/server/src/orchestration/Layers/ThreadDiffStatBaselineReactor.test.ts b/apps/server/src/orchestration/Layers/ThreadDiffStatBaselineReactor.test.ts index 5c831ba0c..3f8643ace 100644 --- a/apps/server/src/orchestration/Layers/ThreadDiffStatBaselineReactor.test.ts +++ b/apps/server/src/orchestration/Layers/ThreadDiffStatBaselineReactor.test.ts @@ -87,6 +87,8 @@ function makeSnapshotQuery( listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }; } diff --git a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts index 41854088a..f4348d587 100644 --- a/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts +++ b/apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts @@ -8,6 +8,8 @@ */ import type { CheckpointRef, + MessageId, + OrchestrationMessage, OrchestrationCheckpointSummary, OrchestrationProject, OrchestrationProjectCatalogSnapshot, @@ -312,6 +314,21 @@ export interface ProjectionSnapshotQueryShape { readonly threadId: ThreadId; readonly turnId: TurnId; }) => Effect.Effect, ProjectionRepositoryError>; + + /** + * Read one message of a thread, without the rest of its history: for work + * that runs on every event of a streaming reply. + */ + readonly getThreadMessage: (input: { + readonly threadId: ThreadId; + readonly messageId: MessageId; + }) => Effect.Effect, ProjectionRepositoryError>; + + /** Whether a turn has any message from the thread's agent yet. */ + readonly hasAssistantMessageForTurn: (input: { + readonly threadId: ThreadId; + readonly turnId: TurnId; + }) => Effect.Effect; } /** diff --git a/apps/server/src/orchestration/streamingMarkdownFlush.test.ts b/apps/server/src/orchestration/streamingMarkdownFlush.test.ts new file mode 100644 index 000000000..7543d650f --- /dev/null +++ b/apps/server/src/orchestration/streamingMarkdownFlush.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, it } from "vitest"; + +import { + splitStreamingAssistantMarkdownFlush, + STREAMING_MARKDOWN_START, + streamingMarkdownStateAfter, +} from "./streamingMarkdownFlush.ts"; + +/** Feeds `text` in `size`-character chunks the way ingestion batches it; reports what waited after each. */ +function stream(text: string, size: number) { + let state = STREAMING_MARKDOWN_START; + let buffered = ""; + let sent = ""; + const waiting: string[] = []; + for (let index = 0; index < text.length; index += size) { + buffered += text.slice(index, index + size); + const { readyText, pendingText } = splitStreamingAssistantMarkdownFlush(buffered, state); + if (readyText.length > 0) { + sent += readyText; + state = streamingMarkdownStateAfter(readyText, state); + buffered = pendingText; + } + waiting.push(buffered); + } + return { sent, buffered, waiting }; +} + +const REPLY = [ + "Change this:\n\n", + "```diff\n", + "- const end = start + pageSize;\n", + "+ const end = Math.min(items[start + pageSize], *limit);\n", + "```\n\n", + "```ts\n", + "const page = Number(url.searchParams.get(`page`) ?? 1);\n", + "```\n\n", + "Done **now**.", +].join(""); + +describe("splitStreamingAssistantMarkdownFlush", () => { + it("sends a code block as it streams, wherever the batches cut its fences", () => { + for (const size of [1, 2, 3, 5, 8]) { + const { sent, buffered, waiting } = stream(REPLY, size); + expect(sent + buffered).toBe(REPLY); + // Brackets, stars and backticks inside the blocks never hold the text after them. + const insideBlocks = waiting.slice( + Math.ceil(REPLY.indexOf("```diff") / size) + 2, + Math.floor(REPLY.indexOf("Done") / size), + ); + for (const held of insideBlocks) expect(held.length).toBeLessThanOrEqual(size + 3); + } + }); + + it("finds a fence whose indent ended the batch before it", () => { + let state = STREAMING_MARKDOWN_START; + let buffered = ""; + for (const batch of ["```js\nx\n ", "```\n", "**unfinished"]) { + buffered += batch; + const { readyText, pendingText } = splitStreamingAssistantMarkdownFlush(buffered, state); + state = streamingMarkdownStateAfter(readyText, state); + buffered = pendingText; + } + // The block closed, so the open emphasis after it waits. + expect(state.fence).toBeNull(); + expect(buffered).toBe("**unfinished"); + }); + + it("still holds a span opened after the code blocks until it closes", () => { + const text = REPLY.slice(0, REPLY.indexOf("now**") + 3); + const { buffered } = stream(text, 4); + expect(buffered.startsWith("**no")).toBe(true); + }); +}); diff --git a/apps/server/src/orchestration/streamingMarkdownFlush.ts b/apps/server/src/orchestration/streamingMarkdownFlush.ts new file mode 100644 index 000000000..974e01f26 --- /dev/null +++ b/apps/server/src/orchestration/streamingMarkdownFlush.ts @@ -0,0 +1,490 @@ +/** + * Where a streaming reply can be cut for the next batch the server sends. + * + * Text is held back while a span it opened might still close (inline code, a + * link, emphasis) or a table row is unfinished, so a user never sees markdown + * half drawn. Each batch is cut from the text received since the last one, + * so what the earlier batches left open travels in `StreamingMarkdownState`: + * a code block, a table, and whether the next batch starts a line. + * + * Inside a code block nothing is markdown, so only a last line that may still + * become its closing fence waits. A fenced block also ends any span before it, + * so the inline rules read only the text after the last block. + * + * @module streamingMarkdownFlush + */ + +const MARKDOWN_FENCE_INDENT_LIMIT = 3; + +interface MarkdownFence { + readonly marker: "`" | "~"; + readonly length: number; +} + +/** What the batches sent so far left open. */ +export interface StreamingMarkdownState { + /** The code block still open, if any. */ + readonly fence: MarkdownFence | null; + /** A table whose rows are still coming. */ + readonly continuingTable: boolean; + /** The text sent so far ends a line (or none was sent). */ + readonly atLineStart: boolean; +} + +/** A reply nothing has been sent of yet. */ +export const STREAMING_MARKDOWN_START: StreamingMarkdownState = { + fence: null, + continuingTable: false, + atLineStart: true, +}; + +/** A line made of up to three spaces, a run of one fence marker, and the rest. */ +function fenceRunOf(line: string): { marker: "`" | "~"; length: number; rest: string } | null { + let cursor = 0; + while (line[cursor] === " " && cursor <= MARKDOWN_FENCE_INDENT_LIMIT) { + cursor += 1; + } + if (cursor > MARKDOWN_FENCE_INDENT_LIMIT) { + return null; + } + const marker = line[cursor]; + if (marker !== "`" && marker !== "~") { + return null; + } + let end = cursor; + while (line[end] === marker) { + end += 1; + } + return { marker, length: end - cursor, rest: line.slice(end) }; +} + +/** + * The fenced code blocks in `text`, read on from `state`: the block still open + * at its end, where the text after the last block starts, and where a last + * unfinished line starts that is only a fence's markers so far (it may yet + * open or close a block, and is read once it has more). + */ +function scanFencedBlocks( + text: string, + state: StreamingMarkdownState, +): { fence: MarkdownFence | null; outsideStart: number; heldStart: number | null } { + let fence = state.fence; + let outsideStart = 0; + let lineStart = 0; + for (;;) { + const newline = text.indexOf("\n", lineStart); + const hasLineEnding = newline !== -1; + const lineEnd = hasLineEnding ? newline : text.length; + // The first line goes on with one an earlier batch began, unless that batch ended it. + const run = + lineStart === 0 && !state.atLineStart ? null : fenceRunOf(text.slice(lineStart, lineEnd)); + // An unfinished line that is so far only a fence's markers, or only the + // spaces a fence may be indented by, waits until it shows what it is. + const unfinishedOpening = + !hasLineEnding && + (lineStart > 0 || state.atLineStart) && + lineEnd > lineStart && + (run !== null ? run.rest.trim() === "" : /^ {1,3}$/u.test(text.slice(lineStart, lineEnd))); + if (unfinishedOpening) { + return { fence, outsideStart, heldStart: lineStart }; + } + if (fence === null) { + if (run !== null && run.length >= 3 && (run.marker === "~" || !run.rest.includes("`"))) { + fence = { marker: run.marker, length: run.length }; + } + } else if ( + run !== null && + run.marker === fence.marker && + run.length >= fence.length && + run.rest.trim() === "" + ) { + fence = null; + outsideStart = lineEnd + 1; + } + if (!hasLineEnding) { + return { fence, outsideStart, heldStart: null }; + } + lineStart = lineEnd + 1; + } +} + +function isEscapedMarkdownCharacter(text: string, index: number): boolean { + let slashCount = 0; + for (let cursor = index - 1; cursor >= 0 && text[cursor] === "\\"; cursor -= 1) { + slashCount += 1; + } + return slashCount % 2 === 1; +} + +function isAsciiAlphaNumeric(char: string | undefined): boolean { + return char !== undefined && /^[A-Za-z0-9]$/u.test(char); +} + +function isMarkdownBoundaryBefore(char: string | undefined): boolean { + return ( + char === undefined || + /\s/u.test(char) || + char === "(" || + char === "[" || + char === "{" || + char === "<" || + char === '"' || + char === "'" + ); +} + +interface MarkdownLine { + readonly text: string; + readonly start: number; + readonly end: number; + readonly hasLineEnding: boolean; +} + +function splitMarkdownLines(text: string): MarkdownLine[] { + const lines: MarkdownLine[] = []; + let start = 0; + for (let index = 0; index < text.length; index += 1) { + if (text[index] !== "\n") { + continue; + } + lines.push({ + text: text.slice(start, index), + start, + end: index, + hasLineEnding: true, + }); + start = index + 1; + } + lines.push({ + text: text.slice(start), + start, + end: text.length, + hasLineEnding: false, + }); + return lines; +} + +function isSyntheticTrailingMarkdownLine(line: MarkdownLine, sourceText: string): boolean { + return !line.hasLineEnding && line.start === sourceText.length && line.text.length === 0; +} + +function stripOuterTablePipes(line: string): string { + let value = line.trim(); + if (value.startsWith("|")) { + value = value.slice(1); + } + if (value.endsWith("|")) { + value = value.slice(0, -1); + } + return value; +} + +function isPotentialMarkdownTableRow(line: string): boolean { + const trimmed = line.trim(); + if (!trimmed || trimmed.startsWith(">")) { + return false; + } + if (trimmed.startsWith("|")) { + return stripOuterTablePipes(trimmed).includes("|"); + } + const firstPipe = trimmed.indexOf("|"); + return firstPipe >= 0 && trimmed.indexOf("|", firstPipe + 1) >= 0; +} + +function isMarkdownTableSeparatorRow(line: string): boolean { + if (!isPotentialMarkdownTableRow(line)) { + return false; + } + const cells = stripOuterTablePipes(line) + .split("|") + .map((cell) => cell.trim()); + return cells.length >= 2 && cells.every((cell) => /^:?-{3,}:?$/u.test(cell)); +} + +function findPendingMarkdownTableStart( + text: string, + options?: { readonly continuingTable?: boolean }, +): number | null { + const lines = splitMarkdownLines(text); + let tableActive = options?.continuingTable === true; + + for (let index = 0; index < lines.length; index += 1) { + const line = lines[index]!; + const nextLine = lines[index + 1]; + if (isSyntheticTrailingMarkdownLine(line, text)) { + break; + } + + if (tableActive) { + if (!isPotentialMarkdownTableRow(line.text)) { + tableActive = false; + continue; + } + if (!line.hasLineEnding) { + return line.start; + } + continue; + } + + if (!isPotentialMarkdownTableRow(line.text)) { + continue; + } + + if (!nextLine) { + return line.start; + } + if (!nextLine.hasLineEnding && !isMarkdownTableSeparatorRow(nextLine.text)) { + return line.start; + } + if (!isMarkdownTableSeparatorRow(nextLine.text)) { + continue; + } + if (!nextLine.hasLineEnding) { + return line.start; + } + + tableActive = true; + index += 1; + } + + return null; +} + +function markdownTableContinuesAfterFlush( + text: string, + options?: { readonly continuingTable?: boolean }, +): boolean { + const lines = splitMarkdownLines(text); + let tableActive = options?.continuingTable === true; + + for (let index = 0; index < lines.length; index += 1) { + const line = lines[index]!; + if (isSyntheticTrailingMarkdownLine(line, text)) { + break; + } + + if (tableActive) { + if (isPotentialMarkdownTableRow(line.text)) { + continue; + } + tableActive = false; + continue; + } + + if (!isPotentialMarkdownTableRow(line.text)) { + continue; + } + + const nextLine = lines[index + 1]; + if ( + nextLine && + !isSyntheticTrailingMarkdownLine(nextLine, text) && + isMarkdownTableSeparatorRow(nextLine.text) + ) { + tableActive = true; + index += 1; + } + } + + return tableActive; +} + +function findClosingMarkdownParenthesis(text: string, startIndex: number): number | null { + let depth = 0; + for (let index = startIndex; index < text.length; index += 1) { + const char = text[index]; + if (isEscapedMarkdownCharacter(text, index)) { + continue; + } + if (char === "(") { + depth += 1; + continue; + } + if (char !== ")") { + continue; + } + if (depth === 0) { + return index; + } + depth -= 1; + } + return null; +} + +/** The first inline code span left open. `text` holds no fenced code block. */ +function findUnclosedInlineCodeSpanStart(text: string): number | null { + let inlineCode: { start: number; tickCount: number } | null = null; + + for (let index = 0; index < text.length; index += 1) { + const char = text[index]; + if (char === "\\") { + index += 1; + continue; + } + if (char !== "`") { + continue; + } + + const runStart = index; + let runEnd = index; + while (text[runEnd] === "`") { + runEnd += 1; + } + const tickCount = runEnd - runStart; + + if (inlineCode && tickCount === inlineCode.tickCount) { + inlineCode = null; + } else if (inlineCode === null) { + inlineCode = { start: runStart, tickCount }; + } + + index = runEnd - 1; + } + + return inlineCode?.start ?? null; +} + +function findUnclosedLinkStart(text: string): number | null { + const labelStack: number[] = []; + + for (let index = 0; index < text.length; index += 1) { + const char = text[index]; + if (isEscapedMarkdownCharacter(text, index)) { + continue; + } + + if (char === "[") { + labelStack.push(text[index - 1] === "!" ? index - 1 : index); + continue; + } + + if (char !== "]" || labelStack.length === 0) { + continue; + } + + const labelStart = labelStack.pop()!; + const nextChar = text[index + 1]; + if (nextChar === "(") { + const closeIndex = findClosingMarkdownParenthesis(text, index + 2); + if (closeIndex === null) { + return labelStart; + } + index = closeIndex; + continue; + } + + if (nextChar === "[") { + const closeReferenceIndex = text.indexOf("]", index + 2); + if (closeReferenceIndex < 0) { + return labelStart; + } + index = closeReferenceIndex; + } + } + + return labelStack[0] ?? null; +} + +function findUnclosedEmphasisStart(text: string): number | null { + const markerStack: Array<{ marker: "*" | "~"; length: 1 | 2; start: number }> = []; + + for (let index = 0; index < text.length; index += 1) { + const marker = text[index]; + if ((marker !== "*" && marker !== "~") || isEscapedMarkdownCharacter(text, index)) { + continue; + } + + let runEnd = index; + while (text[runEnd] === marker) { + runEnd += 1; + } + const runLength = runEnd - index; + const length = Math.min(runLength, 2) as 1 | 2; + const previousChar = text[index - 1]; + const nextChar = text[index + length]; + // A run that ends the text may open a span once its next character + // arrives, so it waits for it. + const canOpen = + isMarkdownBoundaryBefore(previousChar) && + (isAsciiAlphaNumeric(nextChar) || runEnd === text.length) && + (marker === "*" || length === 2); + // Closing only needs a non-space character before the run (CommonMark's + // right-flanking rule). Requiring more — e.g. an alphanumeric — misses + // closers like "**done.**" or "**Note:**", leaving a phantom open span + // that wedges the streaming flush until the message completes. + const canClose = previousChar !== undefined && !/\s/u.test(previousChar); + const openIndex = markerStack.findLastIndex( + (entry) => entry.marker === marker && entry.length === length, + ); + + if (openIndex >= 0 && canClose) { + markerStack.splice(openIndex, 1); + } else if (canOpen) { + markerStack.push({ marker, length, start: index }); + } + + index = runEnd - 1; + } + + return markerStack.length > 0 ? Math.min(...markerStack.map((entry) => entry.start)) : null; +} + +/** + * Cuts `text`, everything received since the last batch, into what can be + * sent now and what waits for more. + */ +export function splitStreamingAssistantMarkdownFlush( + text: string, + state: StreamingMarkdownState, +): { + readyText: string; + pendingText: string; +} { + const blocks = scanFencedBlocks(text, state); + const end = blocks.heldStart ?? text.length; + const pendingStarts: number[] = blocks.heldStart === null ? [] : [blocks.heldStart]; + if (blocks.fence === null) { + const outside = text.slice(blocks.outsideStart, end); + for (const start of [ + findUnclosedInlineCodeSpanStart(outside), + findUnclosedLinkStart(outside), + findUnclosedEmphasisStart(outside), + findPendingMarkdownTableStart(outside, { + continuingTable: blocks.outsideStart === 0 && state.continuingTable, + }), + ]) { + if (start !== null) pendingStarts.push(blocks.outsideStart + start); + } + } + if (pendingStarts.length === 0) { + return { readyText: text, pendingText: "" }; + } + const pendingStart = Math.min(...pendingStarts); + return { + readyText: text.slice(0, pendingStart), + pendingText: text.slice(pendingStart), + }; +} + +/** What is left open once `sentText` has been sent after the batches `state` describes. */ +export function streamingMarkdownStateAfter( + sentText: string, + state: StreamingMarkdownState, +): StreamingMarkdownState { + if (sentText.length === 0) { + return state; + } + const blocks = scanFencedBlocks(sentText, state); + return { + fence: blocks.fence, + continuingTable: + blocks.fence === null && + markdownTableContinuesAfterFlush( + sentText.slice(blocks.outsideStart, blocks.heldStart ?? undefined), + { + continuingTable: blocks.outsideStart === 0 && state.continuingTable, + }, + ), + atLineStart: sentText.endsWith("\n"), + }; +} diff --git a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts index d2859e33e..c7fa4f4e8 100644 --- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts +++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.test.ts @@ -56,6 +56,49 @@ layer("ProjectionThreadMessageRepository", (it) => { }), ); + it.effect("adds streamed words to the stored text, keeping what the first write set", () => + Effect.gen(function* () { + const repository = yield* ProjectionThreadMessageRepository; + const threadId = ThreadId.make("thread-append-streamed"); + const messageId = MessageId.make("message-append-streamed"); + const attachments = [ + { + type: "image" as const, + id: "thread-append-streamed-att-1", + name: "example.png", + mimeType: "image/png", + sizeBytes: 5, + }, + ]; + const streamed = (text: string, at: string) => ({ + messageId, + threadId, + turnId: null, + role: "assistant" as const, + text, + isStreaming: true, + createdAt: at, + updatedAt: at, + }); + + yield* repository.appendStreamedText({ + ...streamed("Hello", "2026-11-16T10:00:00.000Z"), + attachments, + }); + yield* repository.appendStreamedText(streamed(", wor", "2026-11-16T10:00:01.000Z")); + yield* repository.appendStreamedText(streamed("ld", "2026-11-16T10:00:02.000Z")); + + const row = yield* repository.getByMessageId({ messageId }); + assert.equal(row._tag, "Some"); + if (row._tag === "Some") { + assert.equal(row.value.text, "Hello, world"); + assert.equal(row.value.createdAt, "2026-11-16T10:00:00.000Z"); + assert.equal(row.value.updatedAt, "2026-11-16T10:00:02.000Z"); + assert.deepEqual(row.value.attachments, attachments); + } + }), + ); + 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 d1f7516f5..e8b3e8d32 100644 --- a/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts +++ b/apps/server/src/persistence/Layers/ProjectionThreadMessages.ts @@ -104,13 +104,19 @@ function toProjectionThreadMessage( const makeProjectionThreadMessageRepository = Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; - const upsertProjectionThreadMessageRow = SqlSchema.void({ - Request: ProjectionThreadMessage, - execute: (row) => { - const nextAttachmentsJson = - row.attachments !== undefined ? JSON.stringify(row.attachments) : null; - const nextSkillsJson = row.skills !== undefined ? JSON.stringify(row.skills) : null; - return sql` + /** + * Writes a message row. With `appendText` an existing row gets `row.text` + * added to the end of its own text and keeps its creation time, so a + * streaming reply is never read back to grow it. + */ + const writeProjectionThreadMessageRow = ( + row: typeof ProjectionThreadMessage.Encoded, + appendText: boolean, + ) => { + const nextAttachmentsJson = + row.attachments !== undefined ? JSON.stringify(row.attachments) : null; + const nextSkillsJson = row.skills !== undefined ? JSON.stringify(row.skills) : null; + return sql` INSERT INTO projection_thread_messages ( message_id, event_sequence, @@ -190,7 +196,9 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () { thread_id = excluded.thread_id, turn_id = excluded.turn_id, role = excluded.role, - text = excluded.text, + text = CASE WHEN ${appendText ? 1 : 0} = 1 + THEN projection_thread_messages.text || excluded.text + ELSE excluded.text END, attachments_json = COALESCE( excluded.attachments_json, projection_thread_messages.attachments_json @@ -222,10 +230,21 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () { message_trigger = COALESCE(projection_thread_messages.message_trigger, excluded.message_trigger), not_sent = COALESCE(excluded.not_sent, projection_thread_messages.not_sent), is_streaming = excluded.is_streaming, - created_at = excluded.created_at, + created_at = CASE WHEN ${appendText ? 1 : 0} = 1 + THEN projection_thread_messages.created_at + ELSE excluded.created_at END, updated_at = excluded.updated_at `; - }, + }; + + const upsertProjectionThreadMessageRow = SqlSchema.void({ + Request: ProjectionThreadMessage, + execute: (row) => writeProjectionThreadMessageRow(row, false), + }); + + const appendProjectionThreadMessageText = SqlSchema.void({ + Request: ProjectionThreadMessage, + execute: (row) => writeProjectionThreadMessageRow(row, true), }); const getProjectionThreadMessageRow = SqlSchema.findOneOption({ @@ -345,6 +364,13 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () { Effect.mapError(toPersistenceSqlError("ProjectionThreadMessageRepository.upsert:query")), ); + const appendStreamedText: ProjectionThreadMessageRepositoryShape["appendStreamedText"] = (row) => + appendProjectionThreadMessageText(row).pipe( + Effect.mapError( + toPersistenceSqlError("ProjectionThreadMessageRepository.appendStreamedText:query"), + ), + ); + const getByMessageId: ProjectionThreadMessageRepositoryShape["getByMessageId"] = (input) => getProjectionThreadMessageRow(input).pipe( Effect.mapError( @@ -381,6 +407,7 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () { return { upsert, + appendStreamedText, getByMessageId, listByThreadId, getLatestUserWrittenAt, diff --git a/apps/server/src/persistence/Services/ProjectionThreadMessages.ts b/apps/server/src/persistence/Services/ProjectionThreadMessages.ts index 5a96bf9f7..27f59f4ad 100644 --- a/apps/server/src/persistence/Services/ProjectionThreadMessages.ts +++ b/apps/server/src/persistence/Services/ProjectionThreadMessages.ts @@ -115,6 +115,15 @@ export interface ProjectionThreadMessageRepositoryShape { message: ProjectionThreadMessage, ) => Effect.Effect; + /** + * A streaming message's next words: inserts the row when it is new, else + * adds `message.text` to the end of the stored text without reading it. + * The stored row keeps its creation time, sequence, media and skills. + */ + readonly appendStreamedText: ( + message: ProjectionThreadMessage, + ) => Effect.Effect; + /** * Read a projected thread message by id. */ diff --git a/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts b/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts index 10e6b8972..d36ff3280 100644 --- a/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts +++ b/apps/server/src/project/Layers/ProjectSetupScriptRunner.test.ts @@ -49,6 +49,8 @@ const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) => listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.die("unused"), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.die("unused"), }); diff --git a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts index 9748a2e17..0b47698ac 100644 --- a/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts +++ b/apps/server/src/provider/Layers/ProviderSessionReaper.test.ts @@ -321,6 +321,8 @@ describe("ProviderSessionReaper", () => { ); }, getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.die("unused"), }), ), diff --git a/apps/server/src/provider/contextSeed/ThreadContextSeedBuilder.test.ts b/apps/server/src/provider/contextSeed/ThreadContextSeedBuilder.test.ts index f22427a30..efcb2081c 100644 --- a/apps/server/src/provider/contextSeed/ThreadContextSeedBuilder.test.ts +++ b/apps/server/src/provider/contextSeed/ThreadContextSeedBuilder.test.ts @@ -61,6 +61,8 @@ function fakeSnapshotQuery(thread: OrchestrationThread | null): ProjectionSnapsh listThreadShells: () => Effect.succeed([]), getThreadShellById: unusedProjectionQueryMethod, getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(thread === null ? Option.none() : Option.some(thread)), }; @@ -90,6 +92,8 @@ function failingSnapshotQuery(): ProjectionSnapshotQueryShape { listThreadShells: () => Effect.succeed([]), getThreadShellById: unusedProjectionQueryMethod, getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.fail( new PersistenceSqlError({ diff --git a/apps/server/src/pushAlerts/PushAlerts.ts b/apps/server/src/pushAlerts/PushAlerts.ts index c68c48875..c332a6f82 100644 --- a/apps/server/src/pushAlerts/PushAlerts.ts +++ b/apps/server/src/pushAlerts/PushAlerts.ts @@ -555,8 +555,15 @@ const make = Effect.gen(function* () { const start: PushAlertsShape["start"] = () => Effect.gen(function* () { + // Woken only by events that can start or end an alert: a streaming + // reply sends many a second, and each wake reads the log and saves the + // cursor. The idle wake moves the cursor past the rest. yield* Effect.forkScoped( - Stream.runForEach(engine.streamDomainEvents, () => Queue.offer(discoveryBell, undefined)), + Stream.runForEach(engine.streamDomainEvents, (event) => + pushAlertActionsForEvent(event).length > 0 + ? Queue.offer(discoveryBell, undefined) + : Effect.void, + ), ); if (Option.isSome(scheduledTasks)) { yield* Effect.forkScoped( diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 1a2734e24..61c60c107 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -1061,6 +1061,8 @@ const buildAppUnderTest = (options?: { getProjectShellById: () => Effect.succeed(Option.none()), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }), getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()), @@ -4615,6 +4617,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => { projectionSnapshotQuery: { getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 600 }), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.sync(() => { threadDetailReads += 1; diff --git a/apps/server/src/serverRuntimeStartup.test.ts b/apps/server/src/serverRuntimeStartup.test.ts index a8f097a80..bfe0d6e75 100644 --- a/apps/server/src/serverRuntimeStartup.test.ts +++ b/apps/server/src/serverRuntimeStartup.test.ts @@ -118,6 +118,8 @@ it.effect("launchStartupHeartbeat does not block the caller while counts are loa listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.succeed(Option.none()), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.succeed(Option.none()), }), Effect.provideService(AnalyticsService, { @@ -192,6 +194,8 @@ it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and threa listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.die("unused"), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.die("unused"), }), Effect.provideService(OrchestrationEngineService, { @@ -247,6 +251,8 @@ it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when listThreadShells: () => Effect.succeed([]), getThreadShellById: () => Effect.die("unused"), getTurnActivities: () => Effect.succeed([]), + getThreadMessage: () => Effect.succeed(Option.none()), + hasAssistantMessageForTurn: () => Effect.succeed(false), getThreadDetailById: () => Effect.die("unused"), }), Effect.provideService(OrchestrationEngineService, { diff --git a/packages/shared/src/observability.ts b/packages/shared/src/observability.ts index a1c3347b8..51aeb7606 100644 --- a/packages/shared/src/observability.ts +++ b/packages/shared/src/observability.ts @@ -93,7 +93,24 @@ export interface TraceSink { close: () => Effect.Effect; } -export type TraceRecordFilter = (record: TraceRecord) => boolean; +/** + * What a trace filter decides from: the parts of a record that are cheap to + * read off a finished span, so a span the filter drops is never turned into a + * record. A whole `TraceRecord` is one too. + */ +export interface TraceRecordHead { + readonly type: TraceRecord["type"]; + readonly name: string; + readonly traceId: string; + readonly spanId: string; + readonly durationMs: number; + /** Absent on OTLP records. */ + readonly exit?: { readonly _tag: EffectTraceRecord["exit"]["_tag"] }; + readonly attributes: Readonly>; + readonly events: ReadonlyArray<{ readonly attributes: Readonly> }>; +} + +export type TraceRecordFilter = (record: TraceRecordHead) => boolean; export interface LocalFileTracerOptions extends TraceSinkOptions { readonly delegate?: Tracer.Tracer; @@ -200,6 +217,13 @@ export function compactTraceAttributes( ); } +function traceExitTag(exit: Exit.Exit): EffectTraceRecord["exit"]["_tag"] { + if (ExitRuntime.isSuccess(exit)) { + return "Success"; + } + return Cause.hasInterruptsOnly(exit.cause) ? "Interrupted" : "Failure"; +} + function formatTraceExit(exit: Exit.Exit): EffectTraceRecord["exit"] { if (ExitRuntime.isSuccess(exit)) { return { _tag: "Success" }; @@ -471,10 +495,22 @@ class LocalFileSpan implements Tracer.Span { }; this.delegate.end(endTime, exit); + // The filter drops most spans while agents stream (it samples database + // and per-message spans), so it reads a cheap summary first and only a + // kept span is turned into a record. if (this.sampled) { - const record = spanToTraceRecord(this); - if (this.shouldRecord(record)) { - this.push(record); + const head: TraceRecordHead = { + type: "effect-span", + name: this.name, + traceId: this.traceId, + spanId: this.spanId, + durationMs: Number(endTime - this.status.startTime) / 1_000_000, + exit: { _tag: traceExitTag(exit) }, + attributes: Object.fromEntries(this.attributes), + events: this.events.map(([, , attributes]) => ({ attributes })), + }; + if (this.shouldRecord(head)) { + this.push(spanToTraceRecord(this)); } } }