diff --git a/apps/server/src/workflows/WorkflowStore.test.ts b/apps/server/src/workflows/WorkflowStore.test.ts index 7fa6daa8c0208..8931cb08105f8 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 17d2b4c4bf358..e73af2ba9bcf5 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}