From 3029ff8d71d837b8388f0d01fd4c2fa5d947442d Mon Sep 17 00:00:00 2001 From: "Nick T." <73005126+tiliakoos@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:42:35 -0500 Subject: [PATCH] fix(server): workflow step results no longer end in half an emoji A step result or agent reply cut at 8,000 units could end in a lone surrogate when the cut fell inside a surrogate pair. The cut now stops one unit earlier. --- .../src/workflows/WorkflowStore.test.ts | 25 +++++++++++++++++++ apps/server/src/workflows/WorkflowStore.ts | 14 +++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/apps/server/src/workflows/WorkflowStore.test.ts b/apps/server/src/workflows/WorkflowStore.test.ts index 7fa6daa8c020..8931cb08105f 100644 --- a/apps/server/src/workflows/WorkflowStore.test.ts +++ b/apps/server/src/workflows/WorkflowStore.test.ts @@ -224,6 +224,31 @@ it.effect("counts what waits on a person, and finds the run's latest agent threa ), ); +it.effect("a long step result is cut without leaving half an emoji", () => + withStore( + Effect.gen(function* () { + const store = yield* WorkflowStore.WorkflowStore; + yield* store.stepStarted("r1", "write", "step"); + yield* store.stepResult("r1", "write", `${"a".repeat(7_998)}\u{1F680}tail`); + const [step] = yield* store.steps("r1"); + // The cut is of the JSON text, which opens with a quote. + assert.equal(WorkflowStore.jsonValue(step?.result_json ?? "null"), `"${"a".repeat(7_998)}…`); + }), + ), +); + +it.effect("what a long agent step said is cut without leaving half an emoji", () => + withStore( + Effect.gen(function* () { + const store = yield* WorkflowStore.WorkflowStore; + yield* store.stepStarted("r1", "write", "agent"); + yield* store.stepResult("r1", "write", null, `${"a".repeat(7_999)}\u{1F680}tail`); + const [step] = yield* store.steps("r1"); + assert.equal(step?.said, `${"a".repeat(7_999)}…`); + }), + ), +); + it.effect("one buffered signal goes to one wait, and a claimed wait keeps what woke it", () => withStore( Effect.gen(function* () { diff --git a/apps/server/src/workflows/WorkflowStore.ts b/apps/server/src/workflows/WorkflowStore.ts index 17d2b4c4bf35..e73af2ba9bcf 100644 --- a/apps/server/src/workflows/WorkflowStore.ts +++ b/apps/server/src/workflows/WorkflowStore.ts @@ -7,6 +7,7 @@ import { WorkflowRunStatus, WorkflowStep, } from "@t3tools/contracts"; +import { splitsSurrogatePair } from "@t3tools/shared/assistantCitations"; import * as Context from "effect/Context"; import * as DateTime from "effect/DateTime"; import * as Duration from "effect/Duration"; @@ -130,6 +131,10 @@ const WAKE_REDONE_AFTER = Duration.seconds(30); // Long enough for a sender's retries, short enough that the table stays small. // Enough to read what a step said; the page that shows it is not a place for a whole report. const STEP_RESULT_KEPT = 8_000; +const stepResultCut = (text: string) => { + const end = splitsSurrogatePair(text, STEP_RESULT_KEPT) ? STEP_RESULT_KEPT - 1 : STEP_RESULT_KEPT; + return `${text.slice(0, end)}…`; +}; const SIGNAL_IDS_KEPT = Duration.days(7); const REFERENCE_TITLE_KEPT = 300; const idsKeptUntil = Effect.map(DateTime.now, (at) => @@ -909,15 +914,10 @@ export const make = Effect.gen(function* () { ), stepResult: Effect.fn("WorkflowStore.stepResult")(function* (runId, name, value, said) { const full = jsonText(value); - const kept = - full.length <= STEP_RESULT_KEPT ? full : jsonText(`${full.slice(0, STEP_RESULT_KEPT)}…`); + const kept = full.length <= STEP_RESULT_KEPT ? full : jsonText(stepResultCut(full)); const words = said?.trim() ?? ""; const keptSaid = - words === "" - ? null - : words.length <= STEP_RESULT_KEPT - ? words - : `${words.slice(0, STEP_RESULT_KEPT)}…`; + words === "" ? null : words.length <= STEP_RESULT_KEPT ? words : stepResultCut(words); yield* sql` UPDATE workflow_steps SET result_json = ${kept}, said = ${keptSaid} WHERE run_id = ${runId} AND name = ${name}