fix(server): preserve legacy context attribution

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-07-27 19:23:29 +02:00
1 parent 0d81444d7b
commit 1c90d03cc6
2 files changed
+53 -4

No files matched your search

@@ -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/);
}),
);
});
@@ -165,9 +165,9 @@ function makeLegacyImportSummary(items: ReadonlyArray<OrchestrationV2TurnItem>):
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<OrchestrationV2TurnItem>):
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;
}