Skip to content

Commit 3789f8a

Browse files
fix(server): track automatic compaction history
Include provider-generated compaction runs in native transcript boundaries so older turn items are not mistaken for retained context. Implemented With: Codex via GPT-6 Luna on OpenAI through T3 Code
1 parent d57b784 commit 3789f8a

3 files changed

Lines changed: 94 additions & 12 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"
4848
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
4949
import * as ProjectionStore from "./ProjectionStore.ts";
5050
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
51-
import { itemIsInNativeThread as isItemInNativeThread } from "./nativeThreadHistory.ts";
51+
import {
52+
itemIsInNativeThread as isItemInNativeThread,
53+
latestNativeCompactionRunOrdinal,
54+
} from "./nativeThreadHistory.ts";
5255
import {
5356
knownProviderFailureMessage,
5457
makeProviderFailure,
@@ -1152,14 +1155,12 @@ export const layer: Layer.Layer<
11521155
sameNativeThread &&
11531156
!acceptedAttempts.some((source) => source.nativeThreadId !== undefined));
11541157
const runOrdinals = new Map(projection.runs.map((source) => [source.id, source.ordinal]));
1155-
const latestCompactionRunOrdinal = projection.runs.reduce<number | undefined>(
1156-
(latest, source) =>
1157-
source.providerThreadId === providerThread.id &&
1158-
compactionMessageIds.has(source.userMessageId)
1159-
? Math.max(latest ?? source.ordinal, source.ordinal)
1160-
: latest,
1161-
undefined,
1162-
);
1158+
const latestCompactionOrdinal = latestNativeCompactionRunOrdinal({
1159+
runs: projection.runs,
1160+
turnItems: projection.turnItems,
1161+
providerThreadId: providerThread.id,
1162+
compactionMessageIds,
1163+
});
11631164
// A turn in this native thread is not enough: another provider's handoff
11641165
// may contain work the native transcript has never seen.
11651166
const itemIsInNativeThread = (item: Parameters<typeof isItemInNativeThread>[0]["item"]) =>
@@ -1169,7 +1170,7 @@ export const layer: Layer.Layer<
11691170
providerThreadId: providerThread.id,
11701171
acceptedRunIds: nativeInputRunIds,
11711172
runOrdinals,
1172-
latestCompactionRunOrdinal,
1173+
latestCompactionRunOrdinal: latestCompactionOrdinal,
11731174
});
11741175
const handoffIsAlreadyInNativeThread = (
11751176
handoff: OrchestrationV2ContextHandoff,

‎apps/server/src/orchestration-v2/nativeThreadHistory.test.ts‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,59 @@
11
import { expect, it } from "vite-plus/test";
2-
import { ProviderThreadId, RunId, ThreadId } from "@t3tools/contracts";
2+
import { MessageId, ProviderThreadId, RunId, ThreadId } from "@t3tools/contracts";
33

4-
import { itemIsInNativeThread } from "./nativeThreadHistory.ts";
4+
import { itemIsInNativeThread, latestNativeCompactionRunOrdinal } from "./nativeThreadHistory.ts";
5+
6+
it("includes provider-generated compaction runs in the native history boundary", () => {
7+
const providerThreadId = ProviderThreadId.make("provider-thread:compaction-history");
8+
const otherProviderThreadId = ProviderThreadId.make("provider-thread:other-compaction");
9+
const explicitCompactMessageId = MessageId.make("message:explicit-compact");
10+
const runs = [
11+
{
12+
id: RunId.make("run:before-provider-compaction"),
13+
ordinal: 2,
14+
providerThreadId,
15+
userMessageId: MessageId.make("message:ordinary-1"),
16+
},
17+
{
18+
id: RunId.make("run:provider-compaction"),
19+
ordinal: 4,
20+
providerThreadId,
21+
userMessageId: MessageId.make("message:ordinary-2"),
22+
},
23+
{
24+
id: RunId.make("run:explicit-compaction"),
25+
ordinal: 6,
26+
providerThreadId,
27+
userMessageId: explicitCompactMessageId,
28+
},
29+
{
30+
id: RunId.make("run:other-compaction"),
31+
ordinal: 8,
32+
providerThreadId: otherProviderThreadId,
33+
userMessageId: MessageId.make("message:other-thread"),
34+
},
35+
];
36+
37+
expect(
38+
latestNativeCompactionRunOrdinal({
39+
runs,
40+
turnItems: [
41+
{
42+
type: "compaction",
43+
runId: runs[1]!.id,
44+
providerThreadId,
45+
},
46+
{
47+
type: "compaction",
48+
runId: runs[3]!.id,
49+
providerThreadId: otherProviderThreadId,
50+
},
51+
],
52+
providerThreadId,
53+
compactionMessageIds: new Set([explicitCompactMessageId]),
54+
}),
55+
).toBe(6);
56+
});
557

658
it("only treats retained provider input as native transcript history", () => {
759
const threadId = ThreadId.make("thread:native-history");

‎apps/server/src/orchestration-v2/nativeThreadHistory.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
import type {
22
OrchestrationV2HistoricalMessage,
3+
OrchestrationV2Run,
34
OrchestrationV2TurnItem,
45
ProviderThreadId,
56
RunId,
67
ThreadId,
78
} from "@t3tools/contracts";
89

10+
export const latestNativeCompactionRunOrdinal = (input: {
11+
readonly runs: ReadonlyArray<
12+
Pick<OrchestrationV2Run, "id" | "ordinal" | "providerThreadId" | "userMessageId">
13+
>;
14+
readonly turnItems: ReadonlyArray<
15+
Pick<OrchestrationV2TurnItem, "type" | "runId" | "providerThreadId">
16+
>;
17+
readonly providerThreadId: ProviderThreadId;
18+
readonly compactionMessageIds: ReadonlySet<OrchestrationV2Run["userMessageId"]>;
19+
}) => {
20+
const providerCompactionRunIds = new Set(
21+
input.turnItems
22+
.filter(
23+
(item) => item.type === "compaction" && item.providerThreadId === input.providerThreadId,
24+
)
25+
.flatMap((item) => (item.runId === null ? [] : [item.runId])),
26+
);
27+
28+
return input.runs.reduce<number | undefined>(
29+
(latest, run) =>
30+
run.providerThreadId === input.providerThreadId &&
31+
(input.compactionMessageIds.has(run.userMessageId) || providerCompactionRunIds.has(run.id))
32+
? Math.max(latest ?? run.ordinal, run.ordinal)
33+
: latest,
34+
undefined,
35+
);
36+
};
37+
938
type NativeHistoryItem = {
1039
readonly threadId: OrchestrationV2HistoricalMessage["threadId"];
1140
readonly providerThreadId?: OrchestrationV2TurnItem["providerThreadId"];

0 commit comments

Comments
 (0)