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
37 changes: 23 additions & 14 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5550,15 +5550,16 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
}),
);

it.effect("does not freshly arm limit recovery for a subagent of a settled parent", () =>
it.effect("does not freshly arm limit recovery for a subagent of a put-away parent", () =>
Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const now = yield* DateTime.now;
const resetAt = DateTime.formatIso(DateTime.add(now, { hours: 1 }));
type ParentState = "settled" | "archived" | "deleted" | null;
const threadPayload = (
threadId: ThreadId,
parentThreadId: ThreadId | null,
settledOverride: "settled" | null,
state: ParentState,
) =>
({
createdBy: "user",
Expand All @@ -5581,30 +5582,30 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
forkedFrom: null,
createdAt: now,
updatedAt: now,
archivedAt: null,
settledOverride,
settledAt: settledOverride === null ? null : now,
archivedAt: state === "archived" ? now : null,
settledOverride: state === "settled" ? ("settled" as const) : null,
settledAt: state === "settled" ? now : null,
lastVisitedAt: null,
deletedAt: null,
deletedAt: state === "deleted" ? now : null,
}) as const;
const createThread = (
threadId: ThreadId,
parentThreadId: ThreadId | null,
settledOverride: "settled" | null,
state: ParentState,
) =>
store.apply({
id: EventId.make(`event:${threadId}:created`),
type: "thread.created",
threadId,
occurredAt: now,
payload: threadPayload(threadId, parentThreadId, settledOverride),
payload: threadPayload(threadId, parentThreadId, state),
});
const addLimitedChild = Effect.fnUntraced(function* (name: string, parentSettled: boolean) {
const addLimitedChild = Effect.fnUntraced(function* (name: string, parentState: ParentState) {
const parentId = ThreadId.make(`thread:settled-parent-limit:${name}:parent`);
const childId = ThreadId.make(`thread:settled-parent-limit:${name}:child`);
const runId = RunId.make(`run:settled-parent-limit:${name}`);
const rootNodeId = NodeId.make(`node:settled-parent-limit:${name}`);
yield* createThread(parentId, null, parentSettled ? "settled" : null);
yield* createThread(parentId, null, parentState);
yield* createThread(childId, parentId, null);
const run = {
id: runId,
Expand Down Expand Up @@ -5666,14 +5667,22 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
});
return childId;
});
const settledParentChild = yield* addLimitedChild("settled", true);
const activeParentChild = yield* addLimitedChild("active", false);
const settledParentChild = yield* addLimitedChild("settled", "settled");
const archivedParentChild = yield* addLimitedChild("archived", "archived");
const deletedParentChild = yield* addLimitedChild("deleted", "deleted");
const activeParentChild = yield* addLimitedChild("active", null);
const children = [
settledParentChild,
archivedParentChild,
deletedParentChild,
activeParentChild,
];
for (const options of [
{ now, autoResume: true, snooze: false },
{ now, autoResume: false, snooze: true },
]) {
const armed = (yield* store.getLimitRecoveryCandidates(options))
.filter((row) => row.id === settledParentChild || row.id === activeParentChild)
.filter((row) => children.includes(row.id))
.filter(
(row) =>
limitRecoveryCommand(
Expand All @@ -5687,7 +5696,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
assert.deepEqual(
armed,
[activeParentChild],
`only the child of the unsettled parent is armed (${JSON.stringify(options)})`,
`only the child of the open parent is armed (${JSON.stringify(options)})`,
);
}
// A recovery armed before the parent settled still resumes at its reset.
Expand Down
8 changes: 6 additions & 2 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3979,12 +3979,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
${booleanInt(options.autoResume)}
OR (${booleanInt(options.snooze)} AND julianday(json_extract(item.payload_json, '$.failure.resetAt')) > julianday(${DateTime.formatIso(options.now)}))
)
-- A subagent of a settled parent is abandoned work; resuming it would wake the parent.
-- A subagent of a settled, archived or deleted parent is abandoned work.
AND NOT EXISTS (
SELECT 1 FROM orchestration_v2_projection_threads parent
WHERE json_extract(t.payload_json, '$.lineage.relationshipToParent') = 'subagent'
AND parent.thread_id = json_extract(t.payload_json, '$.lineage.parentThreadId')
AND json_extract(parent.payload_json, '$.settledOverride') = 'settled'
AND (
json_extract(parent.payload_json, '$.settledOverride') = 'settled'
OR json_extract(parent.payload_json, '$.archivedAt') IS NOT NULL
OR parent.deleted_at IS NOT NULL
)
)
)
)
Expand Down
Loading