From 1c90d03cc69922646e4eb8731fd05e62787d34be Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 27 Jul 2026 18:28:51 +0200 Subject: [PATCH] fix(server): preserve legacy context attribution Co-authored-by: codex --- .../ContextHandoffService.test.ts | 25 +++++++++++++++ .../orchestration-v2/ContextHandoffService.ts | 32 ++++++++++++++++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/ContextHandoffService.test.ts b/apps/server/src/orchestration-v2/ContextHandoffService.test.ts index 3031d0346c..992c757cc4 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffService.test.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffService.test.ts @@ -104,4 +104,29 @@ it.layer(TestLayer)("ContextHandoffService legacy import", (it) => { assert.include(providerMessage, "User message:\nContinue from there."); }), ); + + it.effect("preserves role attribution when truncating imported history", () => + Effect.gen(function* () { + const service = yield* ContextHandoffServiceV2; + const handoff = yield* service.prepareLegacyImport({ + threadId: ThreadId.make("thread:legacy-context"), + targetRunId: RunId.make("run:first-v2"), + toProviderThreadId: ProviderThreadId.make("provider-thread:first-v2"), + toProviderInstanceId: ProviderInstanceId.make("codex"), + items: [ + importedItem({ + role: "user", + id: "long", + text: `${"x".repeat(35_000)} retained final words`, + ordinal: 1, + }), + ], + createdAt: DateTime.makeUnsafe("2026-01-02T00:00:00.000Z"), + }); + + assert.isAtMost(handoff.summaryText.length, 32_000); + assert.include(handoff.summaryText, "User:\n... retained final words"); + assert.notMatch(handoff.summaryText, /\n+x+ retained final words/); + }), + ); }); diff --git a/apps/server/src/orchestration-v2/ContextHandoffService.ts b/apps/server/src/orchestration-v2/ContextHandoffService.ts index 9dd8b343d0..8b993fa0d0 100644 --- a/apps/server/src/orchestration-v2/ContextHandoffService.ts +++ b/apps/server/src/orchestration-v2/ContextHandoffService.ts @@ -165,9 +165,9 @@ function makeLegacyImportSummary(items: ReadonlyArray): const sections = items.flatMap((item) => { switch (item.type) { case "user_message": - return [`User:\n${item.text}`]; + return [{ label: "User", body: item.text }]; case "assistant_message": - return [`Assistant:\n${item.text}`]; + return [{ label: "Assistant", body: item.text }]; default: return []; } @@ -179,8 +179,32 @@ function makeLegacyImportSummary(items: ReadonlyArray): let remaining = maxChars - header.length - 2; for (const section of sections.toReversed()) { if (remaining <= 0) break; - const suffix = - section.length <= remaining ? section : section.slice(section.length - remaining); + const fullSection = `${section.label}:\n${section.body}`; + if (fullSection.length <= remaining) { + selected.unshift(fullSection); + remaining -= fullSection.length + 2; + continue; + } + + const prefix = `${section.label}:\n... `; + const bodyBudget = remaining - prefix.length; + if (bodyBudget <= 0) continue; + + const initialStart = section.body.length - bodyBudget; + let bodyStart = Math.max(0, initialStart); + if ( + bodyStart > 0 && + !/\s/.test(section.body[bodyStart - 1] ?? "") && + !/\s/.test(section.body[bodyStart] ?? "") + ) { + const nextWhitespace = section.body.slice(bodyStart).search(/\s/); + if (nextWhitespace === -1) continue; + bodyStart += nextWhitespace; + } + const bodySuffix = section.body.slice(bodyStart).trimStart(); + if (bodySuffix.length === 0) continue; + + const suffix = `${prefix}${bodySuffix}`; selected.unshift(suffix); remaining -= suffix.length + 2; }