Skip to content

Commit b4f01a0

Browse files
committed
Merge remote-tracking branch 'origin/main' into prlean/28353
2 parents 9b18dc5 + 0a9abdd commit b4f01a0

9 files changed

Lines changed: 563 additions & 82 deletions

‎apps/server/scripts/migrate-dev-db.test.ts‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,33 @@ const createFixtureSource = Effect.fn("createMigrateDevDbFixtureSource")(functio
3838
'{"lineage":{"parentThreadId":"stopped-thread","relationshipToParent":"fork","rootThreadId":"stopped-thread"}}';
3939
const subagentPayload =
4040
'{"lineage":{"parentThreadId":"subagent-parent","relationshipToParent":"subagent","rootThreadId":"subagent-parent"},"forkedFrom":{"type":"node","nodeId":"node-1"}}';
41+
const watchedPayload = JSON.stringify({
42+
pullRequests: [
43+
{
44+
host: "github.com",
45+
repository: "owner/repo",
46+
number: 7,
47+
url: "https://github.com/owner/repo/pull/7",
48+
source: "agent",
49+
linkedAt: "2026-08-01T00:00:00.000Z",
50+
snapshot: null,
51+
stack: null,
52+
watch: {
53+
startedAt: "2026-08-01T00:00:00.000Z",
54+
headSha: null,
55+
failedChecks: [],
56+
passed: false,
57+
passedChecks: [],
58+
remarksThrough: "2026-08-01T00:00:00.000Z",
59+
remarkIds: [],
60+
conflicting: false,
61+
wakes: 0,
62+
},
63+
},
64+
],
65+
});
4166
const threads = [
42-
["stopped-thread", "project-kept", "completed", "{}", "2026-08-01"],
67+
["stopped-thread", "project-kept", "completed", watchedPayload, "2026-08-01"],
4368
["fork-thread", "project-kept", "completed", forkPayload, "2026-08-02"],
4469
["running-thread", "project-kept", "running", "{}", "2026-08-05"],
4570
["settled-thread", "project-kept", "completed", '{"settledAt":"2026-08-01"}', "2026-08-05"],
@@ -129,6 +154,11 @@ it.layer(NodeServices.layer)("migrate-dev-db", (it) => {
129154
SELECT stream_id FROM orchestration_events ORDER BY stream_id`;
130155
const sessions = yield* sql<{ provider_session_id: string }>`
131156
SELECT provider_session_id FROM orchestration_v2_projection_provider_sessions`;
157+
const [pullRequest] = yield* sql<{ url: string | null; watch: string | null }>`
158+
SELECT
159+
json_extract(payload_json, '$.pullRequests[0].url') AS url,
160+
json_extract(payload_json, '$.pullRequests[0].watch') AS watch
161+
FROM orchestration_v2_projection_threads WHERE thread_id = 'stopped-thread'`;
132162
const [leftovers] = yield* sql<{
133163
auth: number;
134164
tasks: number;
@@ -143,7 +173,7 @@ it.layer(NodeServices.layer)("migrate-dev-db", (it) => {
143173
(SELECT COUNT(*) FROM scheduled_task_webhook_pending) AS webhooks,
144174
(SELECT COUNT(*) FROM orchestration_v2_projection_context_transfers) AS transfers,
145175
(SELECT COUNT(*) FROM peer_environment_links) AS links`;
146-
return { threads, events, sessions, leftovers };
176+
return { threads, events, sessions, pullRequest, leftovers };
147177
}),
148178
);
149179
assert.deepStrictEqual(
@@ -158,6 +188,11 @@ it.layer(NodeServices.layer)("migrate-dev-db", (it) => {
158188
kept.sessions.map((row) => row.provider_session_id),
159189
["session-shared"],
160190
);
191+
assert.deepStrictEqual(
192+
kept.pullRequest,
193+
{ url: "https://github.com/owner/repo/pull/7", watch: null },
194+
"copied pull request links keep their history but stop being watched",
195+
);
161196
assert.deepStrictEqual(kept.leftovers, {
162197
auth: 0,
163198
tasks: 0,

‎apps/server/scripts/migrate-dev-db.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ const pruneSnapshot = Effect.fn("pruneDevDbSnapshot")(function* (input: RunMigra
381381
AND stream_id NOT IN (SELECT thread_id FROM kept_threads))
382382
OR (aggregate_kind = 'project'
383383
AND stream_id NOT IN (SELECT project_id FROM kept_projects))`;
384+
yield* sql`UPDATE orchestration_v2_projection_threads
385+
SET payload_json = json_set(payload_json, '$.pullRequests', (
386+
SELECT json_group_array(json_remove(link.value, '$.watch') ORDER BY link.key)
387+
FROM json_each(payload_json, '$.pullRequests') AS link))
388+
WHERE EXISTS (
389+
SELECT 1 FROM json_each(payload_json, '$.pullRequests') AS link
390+
WHERE json_extract(link.value, '$.watch') IS NOT NULL)`;
384391
for (const table of CLEARED_TABLES) {
385392
yield* sql`DELETE FROM ${sql(table)}`;
386393
}

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

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,145 @@ const seedRestartCancelledChild = (input: {
13801380
return { taskId, childThreadId, childRunId };
13811381
});
13821382

1383+
// Parent spawned two always-wake tasks: the incoming one's completion wake is queued behind a busy
1384+
// run while the sibling's child run is still going.
1385+
const queueCompletionWakeBesideRunningSibling = (key: string) =>
1386+
Effect.gen(function* () {
1387+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1388+
const sink = yield* EventSink.EventSinkV2;
1389+
const now = yield* DateTime.now;
1390+
const threadId = ThreadId.make(`${key}-parent`);
1391+
const originRunId = RunId.make(`${key}-origin`);
1392+
const originRootId = NodeId.make(`${key}-origin-root`);
1393+
const incomingTaskId = NodeId.make(`${key}-incoming`);
1394+
yield* seedParentWithTerminalTask({
1395+
threadId,
1396+
projectId: ProjectId.make(`${key}-project`),
1397+
runId: originRunId,
1398+
rootNodeId: originRootId,
1399+
taskId: incomingTaskId,
1400+
deliveryState: "claimed",
1401+
deliveryTaskIds: [incomingTaskId],
1402+
completionWake: "always",
1403+
now,
1404+
});
1405+
yield* sink.write({
1406+
events: [
1407+
{
1408+
id: EventId.make(`event:${key}-origin-root`),
1409+
type: "node.updated",
1410+
threadId,
1411+
runId: originRunId,
1412+
nodeId: originRootId,
1413+
occurredAt: now,
1414+
payload: {
1415+
id: originRootId,
1416+
threadId,
1417+
runId: originRunId,
1418+
parentNodeId: null,
1419+
rootNodeId: originRootId,
1420+
kind: "root_turn",
1421+
status: "running",
1422+
countsForRun: true,
1423+
providerThreadId: null,
1424+
providerTurnId: null,
1425+
nativeItemRef: null,
1426+
runtimeRequestId: null,
1427+
checkpointScopeId: null,
1428+
startedAt: now,
1429+
completedAt: null,
1430+
},
1431+
},
1432+
],
1433+
});
1434+
yield* orchestrator.dispatch({
1435+
type: "delegated_task.request",
1436+
createdBy: "agent",
1437+
creationSource: "mcp",
1438+
commandId: CommandId.make(`${key}-sibling`),
1439+
parentThreadId: threadId,
1440+
parentRunId: originRunId,
1441+
parentNodeId: originRootId,
1442+
task: "Keep working",
1443+
modelSelection,
1444+
runtimeMode: "full-access",
1445+
interactionMode: "default",
1446+
completionWake: "always",
1447+
});
1448+
const seeded = yield* orchestrator.getThreadProjection(threadId);
1449+
const origin = seeded.runs.find((run) => run.id === originRunId)!;
1450+
const sibling = seeded.subagents.find((task) => task.id !== incomingTaskId)!;
1451+
// The coordinator finished its spawning turn but is busy with another run, so the wake queues.
1452+
yield* sink.write({
1453+
events: [
1454+
{
1455+
...runEvent({ threadId, runId: originRunId, ordinal: 1, status: "completed", now }),
1456+
payload: { ...origin, status: "completed", completedAt: now },
1457+
},
1458+
runEvent({
1459+
threadId,
1460+
runId: RunId.make(`${key}-busy`),
1461+
ordinal: 2,
1462+
status: "running",
1463+
now,
1464+
}),
1465+
],
1466+
});
1467+
const delivery = origin.delegatedCompletion!.delivery!;
1468+
yield* orchestrator.dispatch({
1469+
type: "message.dispatch",
1470+
createdBy: "agent",
1471+
creationSource: "server",
1472+
commandId: CommandId.make(`${key}-wake`),
1473+
threadId,
1474+
messageId: delivery.messageId,
1475+
text: "Delegated task completed",
1476+
attachments: [],
1477+
modelSelection,
1478+
dispatchMode: { type: "queue_after_active" },
1479+
delegatedCompletion: {
1480+
parentRunId: originRunId,
1481+
generation: delivery.generation,
1482+
taskIds: delivery.taskIds,
1483+
},
1484+
});
1485+
const wake = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
1486+
(run) => run.userMessageId === delivery.messageId,
1487+
)!;
1488+
assert.equal(wake.status, "queued");
1489+
const completeSibling = Effect.gen(function* () {
1490+
const childRun = (yield* orchestrator.getThreadProjection(sibling.childThreadId!)).runs[0]!;
1491+
const afterSequence = yield* sink.latestSequence();
1492+
yield* sink.write({
1493+
events: [
1494+
{
1495+
...runEvent({
1496+
threadId: sibling.childThreadId!,
1497+
runId: childRun.id,
1498+
ordinal: childRun.ordinal,
1499+
status: "completed",
1500+
now,
1501+
}),
1502+
payload: { ...childRun, status: "completed", completedAt: now },
1503+
},
1504+
],
1505+
});
1506+
return afterSequence;
1507+
});
1508+
return {
1509+
orchestrator,
1510+
sink,
1511+
now,
1512+
threadId,
1513+
originRunId,
1514+
incomingTaskId,
1515+
sibling,
1516+
delivery,
1517+
wake,
1518+
completeSibling,
1519+
};
1520+
});
1521+
13831522
it.layer(layerContinuationWorkerTest)("interrupted completion wakes", (it) => {
13841523
it.effect.each([
13851524
{ name: "an interrupt keeps", holdQueue: false, siblingFinishesFirst: false },
@@ -1623,6 +1762,137 @@ it.layer(layerContinuationWorkerTest)("interrupted completion wakes", (it) => {
16231762
);
16241763
}),
16251764
);
1765+
1766+
it.effect("cancelling a queued completion wake keeps its other siblings", () =>
1767+
Effect.gen(function* () {
1768+
const {
1769+
orchestrator,
1770+
sink,
1771+
threadId,
1772+
originRunId,
1773+
incomingTaskId,
1774+
sibling,
1775+
delivery,
1776+
wake,
1777+
completeSibling,
1778+
} = yield* queueCompletionWakeBesideRunningSibling("sibling-queued-cancel");
1779+
const cancelled = yield* orchestrator.dispatch({
1780+
type: "queued-run.cancel",
1781+
commandId: CommandId.make("sibling-queued-cancel-cancel"),
1782+
threadId,
1783+
runId: wake.id,
1784+
});
1785+
const cancelDelivery = new Map(
1786+
cancelled.storedEvents.flatMap(({ event }) =>
1787+
event.type === "subagent.updated"
1788+
? [[event.payload.id, event.payload.completionDelivery?.state] as const]
1789+
: [],
1790+
),
1791+
);
1792+
assert.equal(cancelDelivery.get(incomingTaskId), "disposed");
1793+
assert.notEqual(
1794+
cancelDelivery.get(sibling.id),
1795+
"disposed",
1796+
"cancelling the wake must not dispose a still-running sibling it did not carry",
1797+
);
1798+
const afterCancel = yield* orchestrator.getThreadProjection(threadId);
1799+
assert.equal(
1800+
afterCancel.runs.find((run) => run.id === originRunId)?.delegatedCompletion?.disposition,
1801+
"open",
1802+
);
1803+
1804+
const afterSequence = yield* completeSibling;
1805+
yield* sink.stream({ afterSequence, eventType: "subagent.updated" }).pipe(
1806+
Stream.filter(
1807+
(stored) =>
1808+
stored.event.type === "subagent.updated" &&
1809+
stored.event.payload.id === sibling.id &&
1810+
stored.event.payload.completionDelivery?.state === "claimed",
1811+
),
1812+
Stream.take(1),
1813+
Stream.runDrain,
1814+
);
1815+
const successor = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
1816+
(run) => run.id === originRunId,
1817+
)?.delegatedCompletion?.delivery;
1818+
assert.deepEqual(successor?.taskIds, [sibling.id]);
1819+
assert.notEqual(successor?.messageId, delivery.messageId);
1820+
}),
1821+
);
1822+
1823+
it.effect("settling with a queued completion wake disposes its running siblings", () =>
1824+
Effect.gen(function* () {
1825+
const { orchestrator, sink, now, threadId, originRunId, sibling, wake, completeSibling } =
1826+
yield* queueCompletionWakeBesideRunningSibling("sibling-settle");
1827+
const busy = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
1828+
(run) => run.status === "running",
1829+
)!;
1830+
yield* sink.write({
1831+
events: [
1832+
{
1833+
...runEvent({
1834+
threadId,
1835+
runId: busy.id,
1836+
ordinal: busy.ordinal,
1837+
status: "cancelled",
1838+
now,
1839+
}),
1840+
payload: { ...busy, status: "cancelled", completedAt: now },
1841+
},
1842+
{
1843+
...runEvent({ threadId, runId: wake.id, ordinal: wake.ordinal, status: "queued", now }),
1844+
payload: { ...wake, queueHeld: true },
1845+
},
1846+
],
1847+
});
1848+
1849+
const settled = yield* orchestrator.dispatch({
1850+
type: "thread.settle",
1851+
commandId: CommandId.make("sibling-settle-settle"),
1852+
threadId,
1853+
});
1854+
assert.deepEqual(
1855+
settled.storedEvents.flatMap(({ event }) =>
1856+
event.type === "run.updated" && event.payload.id === wake.id
1857+
? [event.payload.status]
1858+
: [],
1859+
),
1860+
["cancelled"],
1861+
);
1862+
const afterSettle = yield* orchestrator.getThreadProjection(threadId);
1863+
assert.equal(
1864+
afterSettle.subagents.find((task) => task.id === sibling.id)?.completionDelivery?.state,
1865+
"disposed",
1866+
);
1867+
1868+
const afterSequence = yield* completeSibling;
1869+
yield* sink.stream({ afterSequence, eventType: "subagent.updated" }).pipe(
1870+
Stream.filter(
1871+
(stored) =>
1872+
stored.event.type === "subagent.updated" &&
1873+
stored.event.payload.id === sibling.id &&
1874+
stored.event.payload.status === "completed",
1875+
),
1876+
Stream.take(1),
1877+
Stream.runDrain,
1878+
);
1879+
const afterLateResult = yield* orchestrator.getThreadProjection(threadId);
1880+
assert.equal(
1881+
afterLateResult.subagents.find((task) => task.id === sibling.id)?.completionDelivery?.state,
1882+
"disposed",
1883+
);
1884+
assert.deepEqual(
1885+
afterLateResult.runs.map((run) => run.id),
1886+
afterSettle.runs.map((run) => run.id),
1887+
);
1888+
assert.equal(
1889+
afterLateResult.runs.find((run) => run.id === originRunId)?.delegatedCompletion
1890+
?.disposition,
1891+
"disposed",
1892+
);
1893+
assert.equal(afterLateResult.thread.settledOverride, "settled");
1894+
}),
1895+
);
16261896
});
16271897

16281898
it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(

0 commit comments

Comments
 (0)