Skip to content

Commit 20081ef

Browse files
fix(server): subagent threads settle with their parent instead of sweeping on their own (pingdotgg#19665)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 303903a commit 20081ef

5 files changed

Lines changed: 556 additions & 53 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,45 @@ it.effect("sql: an unsettled-only shell read skips settled threads", () =>
478478
assert.equal(all.threads.length, 4);
479479
}).pipe(Effect.provide(layerSql)),
480480
);
481+
482+
it.effect("sql: a subagent thread becomes a candidate once its parent is done", () =>
483+
Effect.gen(function* () {
484+
const store = yield* ProjectionStore.ProjectionStoreV2;
485+
const parent = yield* createThread("subagent-parent");
486+
const child = yield* createThread("subagent-child", {
487+
lineage: { parentThreadId: parent, relationshipToParent: "subagent", rootThreadId: parent },
488+
});
489+
const ids = () =>
490+
store.getSettlementCandidates().pipe(Effect.map((rows) => rows.map((row) => row.id)));
491+
assert.deepEqual(yield* ids(), [parent]);
492+
493+
const parentThread = yield* store.getThread(parent);
494+
for (const [type, overrides] of [
495+
["thread.settled", { settledOverride: "settled", settledAt: now }],
496+
["thread.archived", { archivedAt: now }],
497+
["thread.deleted", { deletedAt: now }],
498+
] satisfies ReadonlyArray<
499+
readonly [
500+
"thread.settled" | "thread.archived" | "thread.deleted",
501+
Partial<OrchestrationV2AppThread>,
502+
]
503+
>) {
504+
yield* store.apply({
505+
id: EventId.make(`event:settlement:subagent-parent:${type}`),
506+
type,
507+
threadId: parent,
508+
occurredAt: now,
509+
payload: { ...parentThread, ...overrides, updatedAt: now },
510+
});
511+
assert.deepEqual(yield* ids(), [child]);
512+
}
513+
const orphan = yield* createThread("orphan", {
514+
lineage: {
515+
parentThreadId: ThreadId.make("missing-parent"),
516+
relationshipToParent: "subagent",
517+
rootThreadId: parent,
518+
},
519+
});
520+
assert.deepEqual(new Set(yield* ids()), new Set([child, orphan]));
521+
}).pipe(Effect.provide(layerSql)),
522+
);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export type ProjectionSettlementCandidate = Pick<
202202
| "projectId"
203203
| "branch"
204204
| "worktreePath"
205+
| "lineage"
205206
| "pullRequests"
206207
| "linkedPullRequest"
207208
| "branchPullRequest"
@@ -6757,6 +6758,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67576758
AND json_extract(t.payload_json, '$.settledOverride') IS NULL
67586759
AND json_extract(t.payload_json, '$.pinnedAt') IS NULL
67596760
AND json_extract(t.payload_json, '$.autoSettleDisabledAt') IS NULL
6761+
AND (
6762+
json_extract(t.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent'
6763+
OR NOT EXISTS (
6764+
SELECT 1 FROM orchestration_v2_projection_threads parent
6765+
WHERE parent.thread_id = json_extract(t.payload_json, '$.lineage.parentThreadId')
6766+
AND parent.deleted_at IS NULL
6767+
AND json_extract(parent.payload_json, '$.archivedAt') IS NULL
6768+
AND json_extract(parent.payload_json, '$.settledOverride') IS NOT 'settled'
6769+
)
6770+
)
67606771
AND NOT EXISTS (
67616772
SELECT 1 FROM orchestration_v2_projection_runs active
67626773
WHERE active.thread_id = t.thread_id

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ it("keeps a subagent child awake when its parent thread is snoozed", () => {
126126
});
127127

128128
it.effect.each(["provider", "mcp"] as const)(
129-
"a %s child does not inherit parent settlement protections and settles once idle",
129+
"a %s child does not inherit parent settlement protections and settles once its parent settles",
130130
(creationSource) =>
131131
Effect.gen(function* () {
132132
const store = yield* ProjectionStore.ProjectionStoreV2;
@@ -168,6 +168,21 @@ it.effect.each(["provider", "mcp"] as const)(
168168
assert.deepEqual(savedParent.pinnedAt, parentCreatedAt);
169169
assert.equal(savedParent.pinOrderKey, "a0");
170170
assert.deepEqual(savedParent.autoSettleDisabledAt, parentCreatedAt);
171+
// A subagent child waits for its parent, so settle the pinned parent first.
172+
assert.deepEqual(yield* store.getSettlementCandidates(), []);
173+
yield* store.apply({
174+
id: EventId.make(`event:${parentThreadId}:settled`),
175+
type: "thread.settled",
176+
threadId: parentThreadId,
177+
occurredAt: childCreatedAt,
178+
payload: {
179+
...savedParent,
180+
settledOverride: "settled",
181+
settledAt: childCreatedAt,
182+
pinnedAt: null,
183+
pinOrderKey: null,
184+
},
185+
});
171186
const candidates = yield* store.getSettlementCandidates();
172187
assert.deepEqual(
173188
candidates.map((candidate) => candidate.id),

0 commit comments

Comments
 (0)