Skip to content

Commit e7a121b

Browse files
fix(server): finished delegates keep a process their background work needs
A delegate can finish with a result while background commands or monitors it did not await are still running, such as a dev server it reports to the parent. Releasing its session killed them. Skip the early release while any of the child's provider threads holds pending background tasks, so the idle timer and its pending-work pin decide. AI-assisted; verified with the tests and tools listed above.
1 parent 89c3300 commit e7a121b

2 files changed

Lines changed: 134 additions & 113 deletions

File tree

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

Lines changed: 127 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,124 +3707,139 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
37073707
);
37083708

37093709
it.layer(layerTest)("delegated tasks across a server restart", (it) => {
3710-
it.effect("queues a child result without creating a provider history handoff", () =>
3711-
Effect.gen(function* () {
3712-
const orchestrator = yield* Orchestrator.OrchestratorV2;
3713-
const eventSink = yield* EventSink.EventSinkV2;
3714-
const now = yield* DateTime.now;
3715-
const threadId = ThreadId.make("thread:result-message-parent");
3716-
const projectId = ProjectId.make("project:result-message");
3717-
const runId = RunId.make("run:result-message-parent");
3718-
const rootNodeId = NodeId.make("node:result-message-parent");
3719-
yield* seedParentWithTerminalTask({
3720-
threadId,
3721-
projectId,
3722-
runId,
3723-
rootNodeId,
3724-
taskId: NodeId.make("node:result-message-seed"),
3725-
deliveryState: "acknowledged",
3726-
now,
3727-
});
3728-
const child = yield* seedRestartCancelledChild({
3729-
parentThreadId: threadId,
3730-
projectId,
3731-
parentRunId: runId,
3732-
rootNodeId,
3733-
name: "result-message-child",
3734-
completionWake: "always",
3735-
continuationPending: false,
3736-
runStatus: "completed",
3737-
now,
3738-
});
3739-
const parent = yield* orchestrator.getThreadProjection(threadId);
3740-
const childProjection = yield* orchestrator.getThreadProjection(child.childThreadId);
3741-
const providerThreadId = ProviderThreadId.make("provider-thread:result-message-child");
3742-
yield* eventSink.write({
3743-
commandId: CommandId.make("command:result-message-child-provider"),
3744-
events: [
3745-
{
3746-
id: EventId.make("event:result-message-child-session"),
3747-
type: "provider-session.attached",
3748-
threadId: child.childThreadId,
3749-
occurredAt: now,
3750-
payload: {
3751-
id: ProviderSessionId.make("provider-session:result-message-child"),
3752-
driver,
3753-
providerInstanceId: modelSelection.instanceId,
3754-
status: "ready",
3755-
cwd: "/repo",
3756-
model: modelSelection.model,
3757-
capabilities: {
3758-
...CodexProviderCapabilitiesV2,
3759-
sessions: {
3760-
...CodexProviderCapabilitiesV2.sessions,
3761-
supportsMultipleProviderThreadsPerSession: false,
3710+
it.effect.each([
3711+
{ name: "releases", key: "", pendingBackgroundTasks: [], released: true },
3712+
{
3713+
name: "keeps while an unawaited background command runs",
3714+
key: "-background",
3715+
pendingBackgroundTasks: [{ taskId: "dev-server", kind: "command" as const }],
3716+
released: false,
3717+
},
3718+
])(
3719+
"queues a child result without creating a provider history handoff and $name its process",
3720+
({ key, pendingBackgroundTasks, released }) =>
3721+
Effect.gen(function* () {
3722+
const orchestrator = yield* Orchestrator.OrchestratorV2;
3723+
const eventSink = yield* EventSink.EventSinkV2;
3724+
const now = yield* DateTime.now;
3725+
const threadId = ThreadId.make(`thread:result-message${key}-parent`);
3726+
const projectId = ProjectId.make(`project:result-message${key}`);
3727+
const runId = RunId.make(`run:result-message${key}-parent`);
3728+
const rootNodeId = NodeId.make(`node:result-message${key}-parent`);
3729+
yield* seedParentWithTerminalTask({
3730+
threadId,
3731+
projectId,
3732+
runId,
3733+
rootNodeId,
3734+
taskId: NodeId.make(`node:result-message${key}-seed`),
3735+
deliveryState: "acknowledged",
3736+
now,
3737+
});
3738+
const child = yield* seedRestartCancelledChild({
3739+
parentThreadId: threadId,
3740+
projectId,
3741+
parentRunId: runId,
3742+
rootNodeId,
3743+
name: `result-message${key}-child`,
3744+
completionWake: "always",
3745+
continuationPending: false,
3746+
runStatus: "completed",
3747+
now,
3748+
});
3749+
const parent = yield* orchestrator.getThreadProjection(threadId);
3750+
const childProjection = yield* orchestrator.getThreadProjection(child.childThreadId);
3751+
const providerThreadId = ProviderThreadId.make(
3752+
`provider-thread:result-message${key}-child`,
3753+
);
3754+
yield* eventSink.write({
3755+
commandId: CommandId.make(`command:result-message${key}-child-provider`),
3756+
events: [
3757+
{
3758+
id: EventId.make(`event:result-message${key}-child-session`),
3759+
type: "provider-session.attached",
3760+
threadId: child.childThreadId,
3761+
occurredAt: now,
3762+
payload: {
3763+
id: ProviderSessionId.make(`provider-session:result-message${key}-child`),
3764+
driver,
3765+
providerInstanceId: modelSelection.instanceId,
3766+
status: "ready",
3767+
cwd: "/repo",
3768+
model: modelSelection.model,
3769+
capabilities: {
3770+
...CodexProviderCapabilitiesV2,
3771+
sessions: {
3772+
...CodexProviderCapabilitiesV2.sessions,
3773+
supportsMultipleProviderThreadsPerSession: false,
3774+
},
37623775
},
3776+
createdAt: now,
3777+
updatedAt: now,
3778+
lastError: null,
37633779
},
3764-
createdAt: now,
3765-
updatedAt: now,
3766-
lastError: null,
37673780
},
3768-
},
3769-
{
3770-
id: EventId.make("event:result-message-child-provider"),
3771-
type: "provider-thread.updated",
3772-
threadId: child.childThreadId,
3773-
occurredAt: now,
3774-
payload: {
3775-
...parent.providerThreads[0]!,
3776-
id: providerThreadId,
3777-
appThreadId: child.childThreadId,
3781+
{
3782+
id: EventId.make(`event:result-message${key}-child-provider`),
3783+
type: "provider-thread.updated",
3784+
threadId: child.childThreadId,
3785+
occurredAt: now,
3786+
payload: {
3787+
...parent.providerThreads[0]!,
3788+
id: providerThreadId,
3789+
appThreadId: child.childThreadId,
3790+
pendingBackgroundTasks,
3791+
},
37783792
},
3779-
},
3780-
{
3781-
id: EventId.make("event:result-message-child-run"),
3782-
type: "run.updated",
3783-
threadId: child.childThreadId,
3784-
runId: child.childRunId,
3785-
occurredAt: now,
3786-
payload: { ...childProjection.runs[0]!, providerThreadId },
3787-
},
3788-
],
3789-
});
3793+
{
3794+
id: EventId.make(`event:result-message${key}-child-run`),
3795+
type: "run.updated",
3796+
threadId: child.childThreadId,
3797+
runId: child.childRunId,
3798+
occurredAt: now,
3799+
payload: { ...childProjection.runs[0]!, providerThreadId },
3800+
},
3801+
],
3802+
});
37903803

3791-
yield* orchestrator.recoverDelegatedTasks;
3792-
const recovered = yield* orchestrator.getThreadProjection(threadId);
3793-
const task = recovered.subagents.find((candidate) => candidate.id === child.taskId)!;
3794-
assert.equal(task.status, "completed");
3795-
assert.isNotNull(task.result);
3796-
assert.isEmpty(recovered.contextHandoffs);
3797-
assert.equal(recovered.contextTransfers.length, 1);
3798-
// A one-process-per-thread child releases its process once its result is recorded.
3799-
assert.isEmpty(
3800-
(yield* orchestrator.getThreadProjection(child.childThreadId)).providerSessions,
3801-
);
3804+
yield* orchestrator.recoverDelegatedTasks;
3805+
const recovered = yield* orchestrator.getThreadProjection(threadId);
3806+
const task = recovered.subagents.find((candidate) => candidate.id === child.taskId)!;
3807+
assert.equal(task.status, "completed");
3808+
assert.isNotNull(task.result);
3809+
assert.isEmpty(recovered.contextHandoffs);
3810+
assert.equal(recovered.contextTransfers.length, 1);
3811+
// A one-process-per-thread child releases its process once its result is recorded,
3812+
// unless background work it did not await would die with it.
3813+
assert.equal(
3814+
(yield* orchestrator.getThreadProjection(child.childThreadId)).providerSessions.length,
3815+
released ? 0 : 1,
3816+
);
38023817

3803-
const delivery = recovered.runs.find((candidate) => candidate.id === runId)!
3804-
.delegatedCompletion!.delivery!;
3805-
const messageId = delivery.messageId;
3806-
yield* orchestrator.dispatch({
3807-
type: "message.dispatch",
3808-
commandId: CommandId.make("command:result-message-delivery"),
3809-
threadId,
3810-
messageId,
3811-
text: "Delegated task finished.",
3812-
attachments: [],
3813-
dispatchMode: { type: "queue_after_active" },
3814-
createdBy: "agent",
3815-
creationSource: "server",
3816-
delegatedCompletion: {
3817-
parentRunId: runId,
3818-
generation: delivery.generation,
3819-
taskIds: [child.taskId],
3820-
},
3821-
});
3822-
const queued = yield* orchestrator.getThreadProjection(threadId);
3823-
assert.include(
3824-
queued.messages.find((message) => message.id === messageId)?.text ?? "",
3825-
task.result!,
3826-
);
3827-
}),
3818+
const delivery = recovered.runs.find((candidate) => candidate.id === runId)!
3819+
.delegatedCompletion!.delivery!;
3820+
const messageId = delivery.messageId;
3821+
yield* orchestrator.dispatch({
3822+
type: "message.dispatch",
3823+
commandId: CommandId.make(`command:result-message${key}-delivery`),
3824+
threadId,
3825+
messageId,
3826+
text: "Delegated task finished.",
3827+
attachments: [],
3828+
dispatchMode: { type: "queue_after_active" },
3829+
createdBy: "agent",
3830+
creationSource: "server",
3831+
delegatedCompletion: {
3832+
parentRunId: runId,
3833+
generation: delivery.generation,
3834+
taskIds: [child.taskId],
3835+
},
3836+
});
3837+
const queued = yield* orchestrator.getThreadProjection(threadId);
3838+
assert.include(
3839+
queued.messages.find((message) => message.id === messageId)?.text ?? "",
3840+
task.result!,
3841+
);
3842+
}),
38283843
);
38293844

38303845
it.effect("holds a restart-cancelled child for its continuation's result", () =>

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13489,7 +13489,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1348913489
["runs", "messages", "subagents", "providerThreads", "providerSessions"],
1349013490
{ messageRoles: ["user"] },
1349113491
);
13492-
if (delegatedTaskProgress(child).state !== "result_available") return;
13492+
// Unawaited background tasks still allow a result but die with the process; the idle
13493+
// timer keeps their process pinned.
13494+
if (
13495+
delegatedTaskProgress(child).state !== "result_available" ||
13496+
child.providerThreads.some((thread) => (thread.pendingBackgroundTasks ?? []).length > 0)
13497+
)
13498+
return;
1349313499
for (const session of child.providerSessions) {
1349413500
// Shared processes unload their threads themselves; only a per-thread process lingers.
1349513501
if (

0 commit comments

Comments
 (0)