Skip to content

Commit 2b7bf60

Browse files
committed
fix(server): retain wake ingestion and retry committed deliveries
1 parent cdf2053 commit 2b7bf60

6 files changed

Lines changed: 55 additions & 18 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14677,6 +14677,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1467714677
? [
1467814678
{
1467914679
beforeWake: index < wakeStart,
14680+
pendingWork: event.pendingBackgroundWork === true,
1468014681
taskIds: (event.providerThread.pendingBackgroundTasks ?? []).map((task) => [
1468114682
task.taskId,
1468214683
task.awaited === true,
@@ -14689,6 +14690,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1468914690
rosters.filter((roster) => roster.beforeWake).map((roster) => roster.taskIds),
1469014691
rosters.filter((roster) => roster.beforeWake).map(() => [[WAKE_TASK_ID, true]]),
1469114692
);
14693+
assert.isTrue(
14694+
rosters.filter((roster) => roster.beforeWake).every((roster) => roster.pendingWork),
14695+
);
1469214696
assert.deepEqual(rosters.at(-1)?.taskIds, []);
1469314697
}).pipe(
1469414698
Effect.provide(

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6073,6 +6073,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
60736073
type: "provider_thread.updated",
60746074
driver: CLAUDE_PROVIDER,
60756075
providerThread,
6076+
pendingBackgroundWork: (input.owedWakeTasks?.length ?? 0) > 0,
60766077
});
60776078
});
60786079

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Exit from "effect/Exit";
12
import * as EffectOutbox from "./EffectOutbox.ts";
23
import * as EffectWorker from "./EffectWorker.ts";
34
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
@@ -2431,7 +2432,7 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
24312432
}),
24322433
);
24332434

2434-
it.effect.each(["live", "checkpoint-retry"] as const)(
2435+
it.effect.each(["live", "checkpoint-retry", "checkpoint-offer-retry"] as const)(
24352436
"delivers the run a background command's wake starts after the original result (%s)",
24362437
(delivery) =>
24372438
Effect.gen(function* () {
@@ -2521,7 +2522,7 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
25212522
delivery === "live"
25222523
? CommandId.make(`command:background-wake-finished-${delivery}`)
25232524
: reconcileCommandId(`background-wake-finished-${delivery}`);
2524-
if (delivery === "checkpoint-retry") {
2525+
if (delivery !== "live") {
25252526
const outbox = yield* EffectOutbox.EffectOutboxV2;
25262527
yield* outbox.cancelUnsettled({
25272528
threadId: child.childThreadId,
@@ -2585,9 +2586,25 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
25852586
},
25862587
],
25872588
});
2588-
if (delivery === "checkpoint-retry") {
2589+
if (delivery !== "live") {
25892590
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
2590-
yield* worker.drain();
2591+
if (delivery === "checkpoint-offer-retry") {
2592+
using offer = vi
2593+
.spyOn(requests, "offer")
2594+
.mockImplementationOnce(() => Effect.die("wake offer failed"));
2595+
const first = yield* Effect.exit(
2596+
orchestrator.recoverDelegatedTask(child.childThreadId),
2597+
);
2598+
assert.isTrue(Exit.isFailure(first));
2599+
const committed = (yield* orchestrator.getThreadProjection(
2600+
parentThreadId,
2601+
)).subagents.find((task) => task.id === run.delegatedTaskId)!;
2602+
assert.equal(committed.result, "The background command finished cleanly.");
2603+
yield* worker.drain();
2604+
assert.lengthOf(offer.mock.calls, 2);
2605+
} else {
2606+
yield* worker.drain();
2607+
}
25912608
} else {
25922609
yield* eventSink.stream({ afterSequence, eventType: "subagent.updated" }).pipe(
25932610
Stream.filter(
@@ -2600,9 +2617,16 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
26002617
Stream.runDrain,
26012618
);
26022619
}
2603-
const followup = (yield* orchestrator.getThreadProjection(parentThreadId)).subagents.find(
2604-
(task) => task.id === run.delegatedTaskId,
2605-
)!;
2620+
const completed = yield* orchestrator.getThreadProjection(parentThreadId);
2621+
const followup = completed.subagents.find((task) => task.id === run.delegatedTaskId)!;
2622+
assert.lengthOf(
2623+
completed.contextTransfers.filter(
2624+
(transfer) =>
2625+
transfer.type === "subagent_result" &&
2626+
transfer.sourceThreadId === child.childThreadId,
2627+
),
2628+
2,
2629+
);
26062630
assert.equal(followup.status, "completed");
26072631
assert.equal(followup.result, "The background command finished cleanly.");
26082632
assert.equal(followup.completionDelivery?.state, "claimed");

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15062,14 +15062,18 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1506215062
if (parentThreadId === undefined) return;
1506315063
if (
1506415064
sourceRunId === undefined &&
15065-
!(yield* projectionStore.hasPendingDelegatedResult(threadId))
15065+
!(yield* projectionStore.hasPendingDelegatedCompletion(threadId))
1506615066
)
1506715067
return;
1506815068
yield* threadDispatch.withLock(
1506915069
parentThreadId,
15070-
finalizeAppOwnedSubagent(
15071-
threadId,
15072-
sourceRunId === undefined ? undefined : { settledContinuationOf: sourceRunId },
15070+
offerDelegatedCompletionDeliveries(parentThreadId).pipe(
15071+
Effect.andThen(
15072+
finalizeAppOwnedSubagent(
15073+
threadId,
15074+
sourceRunId === undefined ? undefined : { settledContinuationOf: sourceRunId },
15075+
),
15076+
),
1507315077
),
1507415078
);
1507515079
},

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ export interface ProjectionStoreV2Shape {
477477
readonly listAppOwnedChildThreadIds: (
478478
threadId: ThreadId,
479479
) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;
480-
readonly hasPendingDelegatedResult: (
480+
readonly hasPendingDelegatedCompletion: (
481481
childThreadId: ThreadId,
482482
) => Effect.Effect<boolean, ProjectionStoreV2Error>;
483483
readonly getNextTurnItemOrdinal: (
@@ -5812,12 +5812,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58125812
Effect.map((rows) => [...new Set(rows.map((row) => ThreadId.make(row.child_thread_id)))]),
58135813
Effect.mapError(controlReadError(threadId)),
58145814
);
5815-
const hasPendingDelegatedResult: ProjectionStoreV2Shape["hasPendingDelegatedResult"] = (
5815+
const hasPendingDelegatedCompletion: ProjectionStoreV2Shape["hasPendingDelegatedCompletion"] = (
58165816
childThreadId,
58175817
) =>
5818-
sql`SELECT 1 FROM orchestration_v2_projection_subagents
5819-
WHERE child_thread_id = ${childThreadId} AND origin = 'app_owned'
5820-
AND json_extract(payload_json, '$.result') IS NULL
5818+
sql`SELECT 1 FROM orchestration_v2_projection_subagents AS task
5819+
LEFT JOIN orchestration_v2_projection_runs AS owner ON owner.run_id = task.run_id
5820+
WHERE task.child_thread_id = ${childThreadId} AND task.origin = 'app_owned'
5821+
AND (json_extract(task.payload_json, '$.result') IS NULL
5822+
OR (task.completion_delivery_state = 'claimed'
5823+
AND json_extract(owner.payload_json, '$.delegatedCompletion.disposition') = 'open'
5824+
AND json_type(owner.payload_json, '$.delegatedCompletion.delivery') = 'object'))
58215825
LIMIT 1`.pipe(
58225826
Effect.map((rows) => rows.length > 0),
58235827
Effect.mapError(controlReadError(childThreadId)),
@@ -7845,7 +7849,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
78457849
getMessageCount: readOnReader(getMessageCount),
78467850
getSubagentCount: readOnReader(getSubagentCount),
78477851
listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
7848-
hasPendingDelegatedResult: readOnReader(hasPendingDelegatedResult),
7852+
hasPendingDelegatedCompletion: readOnReader(hasPendingDelegatedCompletion),
78497853
getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),
78507854
getTurnItem: readOnReader(getTurnItem),
78517855
getMessagesById: readOnReader(getMessagesById),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ it.effect(
239239
getThreadHistoryPage: () => Effect.die("unused"),
240240
getTimelinePage: () => Effect.die("Unused timeline read"),
241241
getMessageCount: () => Effect.die("unused message count"),
242-
hasPendingDelegatedResult: () => Effect.die("unused hasPendingDelegatedResult"),
242+
hasPendingDelegatedCompletion: () => Effect.die("unused hasPendingDelegatedCompletion"),
243243
getSubagentCount: () => Effect.die("unused subagent count"),
244244
listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"),
245245
getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"),

0 commit comments

Comments
 (0)