Skip to content
Merged
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
196 changes: 196 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionSettlement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,199 @@ it.effect("sql: a subagent thread becomes a candidate once its parent is done",
assert.deepEqual(new Set(yield* ids()), new Set([child, orphan]));
}).pipe(Effect.provide(layerSql)),
);

it.effect("sql: a subagent is a candidate once its parent has its result", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const parent = yield* createThread("delivered-parent");
const child = yield* createThread("delivered-child", {
lineage: { parentThreadId: parent, relationshipToParent: "subagent", rootThreadId: parent },
});
const ids = () =>
store.getSettlementCandidates().pipe(Effect.map((rows) => rows.map((row) => row.id)));
assert.deepEqual(yield* ids(), [parent]);
const taskId = NodeId.make("node:delivered-child");
const record = (delivery: "pending" | "claimed" | "delivered", result: string | null) =>
store.apply({
id: EventId.make(`event:delivered-child:${delivery}:${result ?? "none"}`),
type: "subagent.updated",
threadId: parent,
nodeId: taskId,
driver: ProviderDriverKind.make("codex"),
providerInstanceId,
occurredAt: now,
payload: {
id: taskId,
threadId: parent,
runId: null,
parentNodeId: taskId,
origin: "app_owned",
createdBy: "agent",
driver: ProviderDriverKind.make("codex"),
providerInstanceId,
providerThreadId: null,
childThreadId: child,
nativeTaskRef: null,
prompt: "Do the work",
title: "child",
model: null,
completionDelivery: { state: delivery, observedByRunId: null },
status: "completed",
result,
startedAt: old,
completedAt: old,
updatedAt: now,
},
});
yield* record("delivered", null);
assert.deepEqual(yield* ids(), [parent]);
yield* record("pending", "done");
assert.deepEqual(yield* ids(), [parent]);
yield* record("claimed", "done");
assert.deepEqual(yield* ids(), [parent]);
yield* record("delivered", "done");
assert.deepEqual(new Set(yield* ids()), new Set([parent, child]));
}).pipe(Effect.provide(layerSql)),
);

it.effect("sql: a running provider subagent result does not make the child a candidate", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const parent = yield* createThread("running-parent");
const child = yield* createThread("running-child", {
lineage: { parentThreadId: parent, relationshipToParent: "subagent", rootThreadId: parent },
});
const ids = () =>
store.getSettlementCandidates().pipe(Effect.map((rows) => rows.map((row) => row.id)));
const taskId = NodeId.make("node:running-child");
const record = (status: "running" | "completed") =>
store.apply({
id: EventId.make(`event:running-child:${status}`),
type: "subagent.updated",
threadId: parent,
nodeId: taskId,
driver: ProviderDriverKind.make("codex"),
providerInstanceId,
occurredAt: now,
payload: {
id: taskId,
threadId: parent,
runId: null,
parentNodeId: taskId,
origin: "provider_native",
createdBy: "agent",
driver: ProviderDriverKind.make("codex"),
providerInstanceId,
providerThreadId: null,
childThreadId: child,
nativeTaskRef: null,
prompt: "Do the work",
title: "child",
model: null,
status,
result: "still going",
startedAt: old,
completedAt: status === "completed" ? now : null,
updatedAt: now,
},
});
yield* record("running");
assert.deepEqual(yield* ids(), [parent]);
yield* record("completed");
assert.deepEqual(new Set(yield* ids()), new Set([parent, child]));
}).pipe(Effect.provide(layerSql)),
);

it.effect("sql: an open follow-up keeps a delivered subagent out of the candidates", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const parent = yield* createThread("follow-up-parent");
const child = yield* createThread("follow-up-child", {
lineage: { parentThreadId: parent, relationshipToParent: "subagent", rootThreadId: parent },
});
const ids = () =>
store.getSettlementCandidates().pipe(Effect.map((rows) => rows.map((row) => row.id)));
const task = (
name: string,
status: "completed" | "pending",
delivery: "delivered" | "pending",
result: string | null,
) =>
store.apply({
id: EventId.make(`event:follow-up:${name}`),
type: "subagent.updated",
threadId: parent,
nodeId: NodeId.make(`node:${name}`),
driver: ProviderDriverKind.make("codex"),
providerInstanceId,
occurredAt: now,
payload: {
id: NodeId.make(`node:${name}`),
threadId: parent,
runId: null,
parentNodeId: NodeId.make(`node:${name}`),
origin: "app_owned",
createdBy: "agent",
driver: ProviderDriverKind.make("codex"),
providerInstanceId,
providerThreadId: null,
childThreadId: child,
nativeTaskRef: null,
prompt: "Do the work",
title: name,
model: null,
completionDelivery: { state: delivery, observedByRunId: null },
status,
result,
startedAt: old,
completedAt: status === "completed" ? old : null,
updatedAt: now,
},
});
yield* task("follow-up-done", "completed", "delivered", "done");
yield* task("follow-up-open", "pending", "pending", null);
assert.deepEqual(yield* ids(), [parent]);
yield* task("follow-up-open", "completed", "delivered", "also done");
assert.deepEqual(new Set(yield* ids()), new Set([parent, child]));
}).pipe(Effect.provide(layerSql)),
);

it.effect("sql: a runless subagent still reports a provider monitor", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const parent = yield* createThread("monitor-parent", { settledOverride: "settled" });
const child = yield* createThread("monitor-child", {
lineage: { parentThreadId: parent, relationshipToParent: "subagent", rootThreadId: parent },
});
yield* store.apply({
id: EventId.make("event:monitor-child:provider"),
type: "provider-thread.updated",
threadId: child,
occurredAt: now,
payload: {
id: ProviderThreadId.make("provider-thread:monitor-child"),
appThreadId: child,
ownerNodeId: null,
driver: ProviderDriverKind.make("codex"),
providerInstanceId,
providerSessionId: null,
nativeThreadRef: null,
nativeConversationHeadRef: null,
status: "idle",
firstRunOrdinal: null,
lastRunOrdinal: null,
handoffIds: [],
forkedFrom: null,
createdAt: old,
updatedAt: now,
pendingBackgroundTasks: [{ taskId: "tail", kind: "monitor", description: "logs" }],
},
});
const candidates = yield* store.getSettlementCandidates();
const row = candidates.find((candidate) => candidate.id === child);
assert.equal(row === undefined, false);
assert.deepEqual(row?.pendingBackgroundTasks, [
{ taskId: "tail", kind: "monitor", description: "logs" },
]);
}).pipe(Effect.provide(layerSql)),
);
30 changes: 27 additions & 3 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8447,6 +8447,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
AND json_extract(parent.payload_json, '$.archivedAt') IS NULL
AND json_extract(parent.payload_json, '$.settledOverride') IS NOT 'settled'
)
OR (
EXISTS (
SELECT 1 FROM orchestration_v2_projection_subagents child_task
WHERE child_task.child_thread_id = t.thread_id
AND child_task.status IN ('completed', 'interrupted', 'cancelled')
AND json_type(child_task.payload_json, '$.result') = 'text'
AND COALESCE(child_task.completion_delivery_state, '') NOT IN ('pending', 'claimed')
)
AND NOT EXISTS (
SELECT 1 FROM orchestration_v2_projection_subagents open_task
WHERE open_task.child_thread_id = t.thread_id
AND (
open_task.status IN ('idle', 'pending', 'running', 'waiting')
OR COALESCE(open_task.completion_delivery_state, '') IN ('pending', 'claimed')
)
)
)
)
AND NOT EXISTS (
SELECT 1 FROM orchestration_v2_projection_runs active
Expand Down Expand Up @@ -8531,9 +8548,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
pendingGuestMessageCount: pendingGuestMessageCount(thread.guestShare),
pendingBackgroundTasks: derivePendingBackgroundWork({
latestRun:
latestRunId === null || status === "idle"
? null
: { id: latestRunId, ordinal: 0, status },
thread.lineage.relationshipToParent === "subagent" &&
(latestRunId === null || status === "idle")
? {
id: RunId.make(`run:subagent:${thread.id}`),
ordinal: 0,
status: "completed",
}
: latestRunId === null || status === "idle"
? null
: { id: latestRunId, ordinal: 0, status },
providerThreads: providerThreadsByThreadId.get(thread.id) ?? [],
turnItems: pendingTurnItemsByThreadId.get(thread.id) ?? [],
activeProviderThreadId: thread.activeProviderThreadId,
Expand Down
Loading
Loading