Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,22 @@ describe("handoff delivery", () => {
}).pipe(Effect.result);
assert.equal(result._tag, "Failure");
assert.equal(calls, 0);

const remaining = yield* deliverContextHandoffs({
handoffs: [handoff],
providerThread,
budget: 0,
fallbackBudget: 9_000,
sameNativeThread: true,
alreadyDeliveredItemIds: new Set(),
inject: (history) => {
assert.deepEqual(history.messages, []);
assert.include(history.context, "t3_thread_read");
return Effect.succeed(true);
},
persist: () => Effect.void,
}).pipe(Effect.result);
assert.equal(remaining._tag, "Success");
}),
);
});
35 changes: 32 additions & 3 deletions apps/server/src/orchestration-v2/ContextHandoffDelivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
readonly handoffs: ReadonlyArray<OrchestrationV2ContextHandoff>;
readonly providerThread: OrchestrationV2ProviderThread;
readonly budget: number | Effect.Effect<number, BudgetError>;
readonly fallbackBudget?: number | Effect.Effect<number, BudgetError>;
readonly sameNativeThread?: boolean;
readonly deferInline?: boolean;
readonly alreadyDeliveredItemIds: ReadonlySet<string>;
readonly inject?: (
Expand Down Expand Up @@ -68,15 +70,42 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
oldContext && historyCost([], `${coverage}\n${oldContext}`) + 512 <= budget
? `${coverage}\n${oldContext}`
: coverage;
const selected = selectHistory({
let selected = selectHistory({
messages,
coverage: fullCoverage,
omittedItems: pending.reduce((sum, handoff) => sum + (handoff.history?.omittedItems ?? 0), 0),
budget,
});
if (historyCost(selected.messages, selected.context) > budget) {
if (input.deferInline) return { context: "", delivered: Effect.void, unsent: Effect.void };
return yield* new ContextHandoffBudgetError();
if (
nativeThreadId !== undefined &&
input.sameNativeThread &&
input.fallbackBudget !== undefined
) {
const fallbackBudget =
typeof input.fallbackBudget === "number"
? input.fallbackBudget
: yield* input.fallbackBudget;
const pointer = selectHistory({
messages: [],
coverage,
omittedItems: pending.reduce(
(sum, handoff) =>
sum + (handoff.history?.omittedItems ?? 0) + (handoff.history?.messages.length ?? 0),
0,
),
budget: fallbackBudget,
});
if (historyCost(pointer.messages, pointer.context) <= fallbackBudget)
selected = {
...pointer,
omittedItemIds: messages.map((message) => message.itemId),
};
else if (!input.deferInline) return yield* new ContextHandoffBudgetError();
else return { context: "", delivered: Effect.void, unsent: Effect.void };
} else if (input.deferInline)
return { context: "", delivered: Effect.void, unsent: Effect.void };
else return yield* new ContextHandoffBudgetError();
}
const omittedItemIds = new Set(selected.omittedItemIds);
const persist = (status: "pending" | "injected" | "inline") =>
Expand Down
17 changes: 17 additions & 0 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
handoffBudget,
attachmentTokenAllowance,
contextUsageForHandoff,
handoffRemainingWindow,
historicalMessage,
latestNativeContextUsage,
} from "@t3tools/provider-core/server/handoffBudget";
Expand Down Expand Up @@ -1011,6 +1012,18 @@ export const layer: Layer.Layer<
legacyInputRunIds,
missedRuns,
missedRunIds,
fallbackBudget: Effect.gen(function* () {
return handoffRemainingWindow({
modelContextWindow,
userText: restartNote === "" ? userText : `${restartNote}\n\n${userText}`,
attachments: message.attachments,
providerThread: budgetProviderThread,
nativeContextEstimate:
budgetProviderThread.contextUsage?.usedTokens === undefined
? yield* nativeContextEstimate
: 0,
});
}),
budget: Effect.gen(function* () {
return handoffBudget({
tokenCap,
Expand Down Expand Up @@ -1283,6 +1296,8 @@ export const layer: Layer.Layer<
handoffs: [...handoffs, ...resumedRetryHandoff],
providerThread: { ...resumed.success, id: providerThread.id },
budget: context.budget,
fallbackBudget: context.fallbackBudget,
sameNativeThread: context.sameNativeThread,
deferInline: isCompactionCommandMessage(message) || goalCommand,
alreadyDeliveredItemIds: context.deliveredItemIds,
persist: () => Effect.void,
Expand Down Expand Up @@ -1579,7 +1594,9 @@ export const layer: Layer.Layer<
const delivery = yield* deliverContextHandoffs({
handoffs: [...effectiveHandoffs, ...retryHandoff],
deferInline: compact || goalCommand,
sameNativeThread,
providerThread: runningProviderThread,
fallbackBudget: handoffContext.fallbackBudget,
budget,
alreadyDeliveredItemIds: deliveredItemIds,
...(session.injectHistory === undefined
Expand Down
18 changes: 18 additions & 0 deletions packages/provider-core/src/server/handoffBudget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ export function handoffBudget(input: {
);
}

export function handoffRemainingWindow(input: {
readonly userText: string;
readonly attachments: ReadonlyArray<ChatAttachment>;
readonly providerThread: OrchestrationV2ProviderThread;
readonly nativeContextEstimate: number;
readonly modelContextWindow?: number | undefined;
}): number {
const usage = input.providerThread.contextUsage;
const window = Math.min(
input.modelContextWindow ?? usage?.maxTokens ?? 128_000,
usage?.maxTokens ?? Infinity,
);
const native = usage?.usedTokens ?? input.nativeContextEstimate;
const current =
Buffer.byteLength(JSON.stringify(input.userText)) + attachmentTokenAllowance(input.attachments);
return Math.max(0, window - native - current);
}

export function historicalMessage(
item: OrchestrationV2TurnItem,
): OrchestrationV2HistoricalMessage | null {
Expand Down