diff --git a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts index 1352e17585bf..b289682fce6d 100644 --- a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts @@ -72,6 +72,8 @@ const createRun = Effect.fn(function* ( threadId: ThreadId, status: OrchestrationV2Run["status"] = "completed", ordinal = 1, + at = old, + queueHeld = false, ) { const store = yield* ProjectionStore.ProjectionStoreV2; const runId = RunId.make(`run:${threadId}:${ordinal}`); @@ -79,7 +81,7 @@ const createRun = Effect.fn(function* ( id: EventId.make(`event:${runId}`), type: "run.created", threadId, - occurredAt: old, + occurredAt: at, payload: { id: runId, threadId, @@ -91,9 +93,10 @@ const createRun = Effect.fn(function* ( rootNodeId: null, activeAttemptId: null, status, - requestedAt: old, - startedAt: status === "queued" ? null : old, - completedAt: ["completed", "rolled_back", "failed"].includes(status) ? old : null, + queueHeld, + requestedAt: at, + startedAt: status === "queued" ? null : at, + completedAt: ["completed", "rolled_back", "failed"].includes(status) ? at : null, checkpointId: null, contextHandoffId: null, }, @@ -136,6 +139,41 @@ const createItem = Effect.fn(function* ( }); }); +it.effect("sql: a failed latest run blocks merge settlement until a successful follow-up", () => + Effect.gen(function* () { + const store = yield* ProjectionStore.ProjectionStoreV2; + const threadId = yield* createThread("failed-after-merge"); + const failedRunId = yield* createRun( + threadId, + "failed", + 1, + DateTime.subtract(now, { minutes: 1 }), + ); + const resolve = Effect.gen(function* () { + const candidates = yield* store.getSettlementCandidates(threadId); + assert.equal(candidates.length, 1); + return resolveAutoSettlementAt({ + thread: candidates[0]!, + pullRequest: { + state: "merged", + mergedAt: DateTime.formatIso(DateTime.subtract(now, { minutes: 5 })), + }, + nowMs: DateTime.toEpochMillis(now), + autoSettleAfterDays: null, + autoSettleOnMerge: true, + }); + }); + assert.isNull(yield* resolve); + yield* createRun(threadId, "queued", 2, now, true); + const [heldCandidate] = yield* store.getSettlementCandidates(threadId); + assert.equal(heldCandidate?.latestRunId, failedRunId); + assert.equal(heldCandidate?.status, "failed"); + assert.isNull(yield* resolve); + yield* createRun(threadId, "completed", 3, now); + assert.deepEqual(yield* resolve, now); + }).pipe(Effect.provide(layerSql)), +); + it.effect( "sql: discovers settlement work with the same activity and background semantics as the shell", () => @@ -250,7 +288,7 @@ it.effect( ); assert.deepEqual( new Set(eligible.map((thread) => thread.id)), - new Set([idle, completed, queued, woke, background, persistent, rolledBack]), + new Set([idle, completed, queued, background, persistent, rolledBack]), ); assert.deepEqual( new Set(eligible.map((thread) => thread.id)), diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index b24de7faf90f..ee6d0de4a4b8 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -6314,6 +6314,10 @@ export const layer: Layer.Layer = LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = ( SELECT latest.run_id FROM orchestration_v2_projection_runs latest WHERE latest.thread_id = t.thread_id + AND NOT ( + latest.status = 'queued' + AND json_extract(latest.payload_json, '$.queueHeld') IS 1 + ) AND json_extract(latest.payload_json, '$.delegationAdmission') IS NULL AND NOT ${isSilentShellRun("latest")} ORDER BY latest.ordinal DESC, latest.run_id DESC diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts index 012e3d2f234c..03cc8afbe03e 100644 --- a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts @@ -164,12 +164,13 @@ describe("isAutoSettlementCandidate", () => { snoozedAt: at(-60 * 60 * 1_000), }); expect(ThreadSettlementService.isAutoSettlementCandidate(snoozed, NOW_MS)).toBe(false); - expect( - ThreadSettlementService.isAutoSettlementCandidate( - shell({ ...snoozed, status: "failed", latestRunCompletedAt: at(-30 * 60 * 1_000) }), - NOW_MS, - ), - ).toBe(true); + const failed = shell({ + ...snoozed, + status: "failed", + latestRunCompletedAt: at(-30 * 60 * 1_000), + }); + expect(ThreadSettlementService.isSnoozed(failed, NOW_MS)).toBe(false); + expect(ThreadSettlementService.isAutoSettlementCandidate(failed, NOW_MS)).toBe(false); expect( ThreadSettlementService.isAutoSettlementCandidate( shell({ ...snoozed, status: "failed", latestRunCompletedAt: at(-2 * 60 * 60 * 1_000) }), @@ -198,6 +199,33 @@ describe("isAutoSettlementCandidate", () => { }); describe("resolveAutoSettlementAt", () => { + it.each(["merged", "closed", "inactivity"] as const)( + "keeps a failed run visible instead of settling on %s", + (reason) => { + const input = { + thread: shell({ status: "failed", latestRunCompletedAt: at(-4 * DAY_MS) }), + pullRequest: + reason === "inactivity" + ? null + : { + state: reason, + mergedAt: DateTime.formatIso(at(-1)), + closedAt: DateTime.formatIso(at(-1)), + }, + nowMs: NOW_MS, + autoSettleAfterDays: reason === "inactivity" ? 3 : null, + autoSettleOnMerge: true, + }; + expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toBeNull(); + expect( + ThreadSettlementService.resolveAutoSettlementAt({ + ...input, + thread: { ...input.thread, status: "completed" }, + }), + ).toEqual(at(-4 * DAY_MS)); + }, + ); + it("keeps threads with a PR from inactivity settlement only when asked", () => { const input = { thread: shell({ @@ -746,11 +774,13 @@ const startHarness = Effect.fn("startThreadSettlementHarness")(function* ( }); describe("ThreadSettlementServiceV2 worker", () => { - it.effect("settles a merged pull request stored only in the thread links", () => + it.effect("only settles a merged pull request when the latest run has not failed", () => Effect.scoped( Effect.gen(function* () { yield* TestClock.setTime(Date.parse(NOW)); const thread = makeThread("merged-link", { + status: "completed", + latestRunCompletedAt: DateTime.makeUnsafe(NOW), pullRequests: [ { host: "example.test", @@ -773,8 +803,13 @@ describe("ThreadSettlementServiceV2 worker", () => { }, ], }); + const failedThread = shell({ + ...thread, + id: ThreadId.make("failed-after-merge"), + status: "failed", + }); const fixture = yield* makeHarness({ - snapshot: makeSnapshot([thread]), + snapshot: makeSnapshot([thread, failedThread]), settings: { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: null }, }); diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.ts index 509dcd0307dc..adef725515c3 100644 --- a/apps/server/src/orchestration-v2/ThreadSettlementService.ts +++ b/apps/server/src/orchestration-v2/ThreadSettlementService.ts @@ -148,6 +148,7 @@ export function isAutoSettlementCandidate( ): boolean { if (thread.archivedAt !== null || thread.settledOverride !== null) return false; if (thread.pinnedAt != null || thread.autoSettleDisabledAt != null) return false; + if (thread.status === "failed") return false; if (thread.pendingRuntimeRequest !== null) return false; // A guest is waiting on the owner; settling would hide the Request. if ((thread.pendingGuestMessageCount ?? 0) > 0) return false; diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.md index ac74e6553f46..3869b3143e9f 100644 --- a/docs/user/thread-sidebar.md +++ b/docs/user/thread-sidebar.md @@ -180,7 +180,7 @@ Press `Escape` while dragging to cancel. By default, environments settle inactive threads after three days and settle threads whose pull request merged. A closed pull request can also settle an idle thread. An empty thread's inactivity counts from when it was created. -Work in progress, pending questions or approvals, and background work the agent +Failed runs, work in progress, pending questions or approvals, and background work the agent is waiting on prevent automatic settlement. An open pull request does not prevent inactivity settlement unless you turn on **Keep threads with a PR** in **Settings → Threads**, but an old closed or merged pull request does not settle work you