Skip to content
Closed
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
38 changes: 38 additions & 0 deletions apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,44 @@ describe("handoff delivery", () => {
}),
);

it.effect.each([
{ label: "a trimmed switch delta", from: ["provider-thread:other"], omitted: 366, fits: false },
{ label: "a whole switch delta", from: ["provider-thread:other"], omitted: 0, fits: true },
{
label: "a trimmed delta that also covers the target",
from: ["provider-thread:target", "provider-thread:other"],
omitted: 1,
fits: false,
},
{
label: "a trimmed same-thread retry delta",
from: ["provider-thread:target"],
omitted: 1,
fits: true,
},
])("treats a resumed native thread as stale only for $label", ({ from, omitted, fits }) =>
Effect.gen(function* () {
const result = yield* Effect.result(
deliverContextHandoffs({
handoffs: [
{
...handoff,
strategy: "delta_since_target_last_seen",
fromProviderThreadIds: from.map((id) => ProviderThreadId.make(id)),
history: { ...handoff.history!, omittedItems: omitted },
},
],
providerThread,
budget: 16_000,
requireWholeSwitchDelta: true,
alreadyDeliveredItemIds: new Set(),
persist: () => Effect.void,
}),
);
assert.equal(result._tag === "Success", fits);
}),
);

it.effect("persists successful injection before turn start and skips it on retry", () =>
Effect.gen(function* () {
let durable = handoff;
Expand Down
15 changes: 15 additions & 0 deletions apps/server/src/orchestration-v2/ContextHandoffDelivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
readonly providerThread: OrchestrationV2ProviderThread;
readonly budget: number | Effect.Effect<number, BudgetError>;
readonly deferInline?: boolean;
/** Refuse a trimmed delta from another provider thread into this resumed native thread. */
readonly requireWholeSwitchDelta?: boolean;
readonly alreadyDeliveredItemIds: ReadonlySet<string>;
readonly inject?: (
history: ProviderAdapter.ProviderAdapterV2HistoricalContext,
Expand Down Expand Up @@ -80,6 +82,19 @@ export const deliverContextHandoffs = Effect.fn("orchestrationV2.deliverContextH
return yield* new ContextHandoffBudgetError();
}
const omittedItemIds = new Set(selected.omittedItemIds);
// The resumed native history would outweigh what the trimmed delta dropped; the caller starts fresh.
if (
input.requireWholeSwitchDelta &&
pending.some(
(handoff) =>
handoff.strategy === "delta_since_target_last_seen" &&
(handoff.fromProviderThreadIds.length !== 1 ||
handoff.fromProviderThreadIds[0] !== handoff.toProviderThreadId) &&
((handoff.history?.omittedItems ?? 0) > 0 ||
handoff.history?.messages.some((message) => omittedItemIds.has(message.itemId))),
)
)
return yield* new ContextHandoffBudgetError();
const persist = (status: "pending" | "injected" | "inline") =>
Effect.forEach(
pending,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,7 @@ export const layer: Layer.Layer<
providerThread: { ...resumed.success, id: providerThread.id },
budget: context.budget,
deferInline: isCompactionCommandMessage(providerMessage) || goalCommand,
requireWholeSwitchDelta: true,
alreadyDeliveredItemIds: context.deliveredItemIds,
persist: () => Effect.void,
}).pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,19 @@ describe("orchestration v2 provider switching", () => {
}),
);

it.live("keeps a native session whose stored prose is far below an unreported window", () =>
it.live.each([
{
label: "keeps a native session whose stored prose is far below an unreported window",
sourceWork: "Source work",
generations: 1,
},
// The resumed native history would outweigh what a trimmed delta drops, so the session is stale.
{
label: "replaces the native session when the switch-back delta must be trimmed",
Comment on lines +1302 to +1303

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats the trimmed switch-delta case already covered by the delivery unit test, which fails at the lower layer on main. Could we remove this integration row and keep the unit test as the single-layer check?

Posted via Macroscope — Test Signal

sourceWork: "Newer decision. ".repeat(8_000),
generations: 2,
},
])("$label", ({ sourceWork, generations }) =>
Effect.gen(function* () {
const cwd = yield* checkpointWorkspace("handoff-estimate-headroom");
const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
Expand All @@ -1305,7 +1317,7 @@ describe("orchestration v2 provider switching", () => {
driver: CODEX_DRIVER,
capabilities: CodexProviderCapabilitiesV2,
modelSelection: CODEX_MODEL_SELECTION,
responseByRunOrdinal: { 2: "Source work" },
responseByRunOrdinal: { 2: sourceWork },
capturedTurns,
}),
// No window and no usage, like an ACP registry agent that sends no usage_update.
Expand Down Expand Up @@ -1370,8 +1382,8 @@ describe("orchestration v2 provider switching", () => {
(yield* orchestrator.getThreadProjection(threadId)).runs.at(-1)?.status,
"completed",
);
// A second generation means T3 discarded the native session as full.
assert.equal(yield* Ref.get(generation), 1);
// A second generation means T3 discarded the native session.
assert.equal(yield* Ref.get(generation), generations);
}).pipe(
Effect.provide(
ProviderReplayHarness.layerWithRegistry(
Expand Down
Loading