GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): continue a provider session that already holds its histo… · patroza/t3code@5284632

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/orchestration-v2
    • ContextHandoffBudget.test.ts
    • ContextHandoffBudget.ts
    • Orchestrator.migration.test.ts
    • Orchestrator.ts
    • ProviderTurnStartService.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts
+123Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ProviderAdapterV2HistoricalContext } from "./ProviderAdapter.ts";
22
import { assert, describe, it } from "@effect/vitest";
33
import {
44
ContextHandoffId,
5+
MessageId,
56
PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
67
ProviderDriverKind,
78
ProviderInstanceId,
@@ -17,7 +18,10 @@ import * as DateTime from "effect/DateTime";
1718
import * as Effect from "effect/Effect";
1819
import * as Schema from "effect/Schema";
1920
import {
21+
completedCompactionRunOrdinal,
2022
contextUsageForHandoff,
23+
countsTowardNativeContextEstimate,
24+
handoffAlreadyInNativeSession,
2125
handoffBudget,
2226
historyCost,
2327
historyResponseItems,
@@ -412,6 +416,125 @@ describe("handoff budget", () => {
412416
});
413417
});
414418

419+
describe("native session already holds the history", () => {
420+
const compactMessageId = MessageId.make("message:compact");
421+
const providerThreadId = ProviderThreadId.make("provider-thread:grok");
422+
const delivered = {
423+
...handoff,
424+
strategy: "manual_context" as const,
425+
fromProviderThreadIds: [],
426+
toProviderThreadId: providerThreadId,
427+
coveredRunOrdinals: { from: 1, to: 1 },
428+
delivery: {
429+
nativeThreadId: "native:grok",
430+
status: "inline" as const,
431+
itemIds: [],
432+
},
433+
};
434+
435+
it("keeps a completed compact and ignores a later copy of a delivered import", () => {
436+
const ordinal = completedCompactionRunOrdinal({
437+
providerThreadId,
438+
compactUserMessageIds: new Set([compactMessageId]),
439+
runs: [
440+
{
441+
ordinal: 5,
442+
status: "failed",
443+
providerThreadId,
444+
userMessageId: MessageId.make("message:failed"),
445+
},
446+
{
447+
ordinal: 6,
448+
status: "completed",
449+
providerThreadId,
450+
userMessageId: compactMessageId,
451+
},
452+
{
453+
ordinal: 7,
454+
status: "completed",
455+
providerThreadId: ProviderThreadId.make("provider-thread:other"),
456+
userMessageId: compactMessageId,
457+
},
458+
],
459+
});
460+
assert.equal(ordinal, 6);
461+
assert.isFalse(
462+
countsTowardNativeContextEstimate({
463+
compactionRunOrdinal: ordinal,
464+
itemRunOrdinal: undefined,
465+
}),
466+
);
467+
assert.isFalse(
468+
countsTowardNativeContextEstimate({ compactionRunOrdinal: ordinal, itemRunOrdinal: 6 }),
469+
);
470+
assert.isTrue(
471+
countsTowardNativeContextEstimate({ compactionRunOrdinal: ordinal, itemRunOrdinal: 8 }),
472+
);
473+
assert.isTrue(
474+
countsTowardNativeContextEstimate({
475+
compactionRunOrdinal: undefined,
476+
itemRunOrdinal: undefined,
477+
}),
478+
);
479+
assert.equal(
480+
handoffBudget({
481+
tokenCap: 16_000,
482+
userText: "update to latest again",
483+
attachments: [],
484+
providerThread,
485+
nativeContextEstimate: 795_175,
486+
}),
487+
0,
488+
);
489+
assert.isAbove(
490+
handoffBudget({
491+
tokenCap: 16_000,
492+
userText: "update to latest again",
493+
attachments: [],
494+
providerThread,
495+
nativeContextEstimate: 0,
496+
modelContextWindow: 256_000,
497+
}),
498+
0,
499+
);
500+
501+
const duplicate = { ...delivered, delivery: undefined };
502+
assert.isTrue(
503+
handoffAlreadyInNativeSession({
504+
handoff: duplicate,
505+
nativeThreadId: "native:grok",
506+
sameNativeThread: true,
507+
delivered: [delivered],
508+
}),
509+
);
510+
// `/compact` defers an undelivered handoff for the next ordinary turn.
511+
assert.isFalse(
512+
handoffAlreadyInNativeSession({
513+
handoff: { ...delivered, coveredRunOrdinals: { from: 1, to: 6 } },
514+
nativeThreadId: "native:grok",
515+
sameNativeThread: true,
516+
delivered: [],
517+
}),
518+
);
519+
assert.isFalse(
520+
handoffAlreadyInNativeSession({
521+
handoff: { ...handoff, coveredRunOrdinals: { from: 1, to: 8 } },
522+
nativeThreadId: "native:grok",
523+
sameNativeThread: true,
524+
delivered: [delivered],
525+
}),
526+
);
527+
assert.isFalse(
528+
handoffAlreadyInNativeSession({
529+
handoff: duplicate,
530+
nativeThreadId: "native:replacement",
531+
sameNativeThread: false,
532+
delivered: [delivered],
533+
}),
534+
);
535+
});
536+
});
537+
415538
describe("handoff delivery", () => {
416539
it.effect.each([
417540
{ native: true, label: "injected" },
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ContextHandoffBudget.ts
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import type {
66
OrchestrationV2ContextHandoff,
77
OrchestrationV2HistoricalMessage,
88
OrchestrationV2ProviderThread,
9+
OrchestrationV2Run,
910
OrchestrationV2TurnItem,
11+
ProviderThreadId,
1012
} from "@t3tools/contracts";
1113

1214
import * as Config from "effect/Config";
@@ -91,6 +93,89 @@ export function contextUsageForHandoff(input: {
9193
};
9294
}
9395

96+
/**
97+
* Latest completed native `/compact` on this provider thread. Its summary
98+
* replaces the earlier transcript, so later handoffs must not replay it.
99+
*/
100+
export function completedCompactionRunOrdinal(input: {
101+
readonly runs: ReadonlyArray<
102+
Pick<OrchestrationV2Run, "ordinal" | "status" | "providerThreadId" | "userMessageId">
103+
>;
104+
readonly providerThreadId: ProviderThreadId;
105+
readonly compactUserMessageIds: ReadonlySet<OrchestrationV2Run["userMessageId"]>;
106+
}): number | undefined {
107+
let ordinal: number | undefined;
108+
for (const run of input.runs) {
109+
if (
110+
run.status === "completed" &&
111+
run.providerThreadId === input.providerThreadId &&
112+
input.compactUserMessageIds.has(run.userMessageId) &&
113+
(ordinal === undefined || run.ordinal > ordinal)
114+
) {
115+
ordinal = run.ordinal;
116+
}
117+
}
118+
return ordinal;
119+
}
120+
121+
/**
122+
* A completed compact folds imported rows and earlier runs into the native
123+
* summary. Counting those bytes as still-resident occupancy refuses a
124+
* follow-up the provider session has room for.
125+
*/
126+
export function countsTowardNativeContextEstimate(input: {
127+
readonly compactionRunOrdinal: number | undefined;
128+
readonly itemRunOrdinal: number | undefined;
129+
}): boolean {
130+
if (input.compactionRunOrdinal === undefined) return true;
131+
if (input.itemRunOrdinal === undefined) return false;
132+
return input.itemRunOrdinal > input.compactionRunOrdinal;
133+
}
134+
135+
function sameProviderThreadSet(
136+
left: ReadonlyArray<ProviderThreadId>,
137+
right: ReadonlyArray<ProviderThreadId>,
138+
): boolean {
139+
if (left.length !== right.length) return false;
140+
const seen = new Set<ProviderThreadId>(left);
141+
return right.every((id) => seen.has(id));
142+
}
143+
144+
/**
145+
* The native session already holds this history when an earlier handoff of
146+
* the same strategy and source set was delivered into it. A `/compact` that
147+
* cannot take an inline transcript leaves its handoff undelivered on purpose;
148+
* the next ordinary turn still sends that one.
149+
*/
150+
export function handoffAlreadyInNativeSession(input: {
151+
readonly handoff: Pick<
152+
OrchestrationV2ContextHandoff,
153+
"strategy" | "coveredRunOrdinals" | "fromProviderThreadIds"
154+
>;
155+
readonly nativeThreadId: string | null | undefined;
156+
readonly sameNativeThread: boolean;
157+
readonly delivered: ReadonlyArray<
158+
Pick<
159+
OrchestrationV2ContextHandoff,
160+
"strategy" | "coveredRunOrdinals" | "fromProviderThreadIds" | "delivery"
161+
>
162+
>;
163+
}): boolean {
164+
if (!input.sameNativeThread || input.nativeThreadId == null) return false;
165+
return input.delivered.some((delivered) => {
166+
const delivery = delivered.delivery;
167+
return (
168+
delivery !== undefined &&
169+
delivery.nativeThreadId === input.nativeThreadId &&
170+
delivery.status !== "pending" &&
171+
delivered.strategy === input.handoff.strategy &&
172+
sameProviderThreadSet(delivered.fromProviderThreadIds, input.handoff.fromProviderThreadIds) &&
173+
delivered.coveredRunOrdinals.from <= input.handoff.coveredRunOrdinals.from &&
174+
delivered.coveredRunOrdinals.to >= input.handoff.coveredRunOrdinals.to
175+
);
176+
});
177+
}
178+
94179
export function attachmentTokenAllowance(attachments: ReadonlyArray<ChatAttachment>): number {
95180
// Encoded image bytes are not model tokens. Without dimensions/detail metadata,
96181
// reserve 8k tokens per image, above typical resized Codex/Claude image costs.
Collapse file

‎apps/server/src/orchestration-v2/Orchestrator.migration.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.migration.test.ts
+50-1Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { assert, it } from "@effect/vitest";
22
import * as Schema from "effect/Schema";
3-
import { ContextHandoffId, OrchestrationV2Command, ThreadId } from "@t3tools/contracts";
3+
import {
4+
ContextHandoffId,
5+
OrchestrationV2Command,
6+
ProviderThreadId,
7+
ThreadId,
8+
} from "@t3tools/contracts";
49

510
import {
611
appendContextHandoffId,
712
canReplayCommandReceipt,
13+
legacyImportAlreadyInNativeThread,
814
shouldPrepareLegacyImportHandoff,
915
} from "./Orchestrator.ts";
1016

@@ -37,6 +43,49 @@ it("reissues imported context until a V2 run completes", () => {
3743
legacyImportItemCount: 0,
3844
}),
3945
);
46+
assert.isFalse(
47+
shouldPrepareLegacyImportHandoff({
48+
historyOrigin: "v1_import",
49+
hasCompletedRun: false,
50+
legacyImportItemCount: 2,
51+
alreadyInNativeThread: true,
52+
}),
53+
);
54+
});
55+
56+
it("treats an inlined v1 import as already in that native session", () => {
57+
const providerThreadId = ProviderThreadId.make("provider-thread:grok");
58+
const delivered = {
59+
strategy: "manual_context" as const,
60+
fromProviderThreadIds: [],
61+
toProviderThreadId: providerThreadId,
62+
delivery: {
63+
nativeThreadId: "native-1",
64+
status: "inline" as const,
65+
itemIds: [],
66+
},
67+
};
68+
assert.isTrue(
69+
legacyImportAlreadyInNativeThread({
70+
providerThreadId,
71+
nativeThreadId: "native-1",
72+
handoffs: [delivered],
73+
}),
74+
);
75+
assert.isFalse(
76+
legacyImportAlreadyInNativeThread({
77+
providerThreadId,
78+
nativeThreadId: "native-1",
79+
handoffs: [{ ...delivered, delivery: { ...delivered.delivery, status: "pending" } }],
80+
}),
81+
);
82+
assert.isFalse(
83+
legacyImportAlreadyInNativeThread({
84+
providerThreadId,
85+
nativeThreadId: undefined,
86+
handoffs: [delivered],
87+
}),
88+
);
4089
});
4190

4291
it("records a reissued legacy handoff on an existing provider thread", () => {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.