Skip to content
261 changes: 247 additions & 14 deletions apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
DEFAULT_SERVER_SETTINGS,
ProjectId,
EventId,
NodeId,
ProviderDriverKind,
ProviderInstanceId,
MessageId,
RuntimeRequestId,
Expand Down Expand Up @@ -135,6 +137,22 @@ function persistedLink(
};
}

function mergedLink(): NonNullable<SettlementShell["pullRequests"]>[number] {
return {
...persistedLink("open", DateTime.makeUnsafe("2026-08-20T00:00:00.000Z")),
snapshot: {
state: "merged",
title: "Pull request",
headBranch: "feature",
baseBranch: "main",
isDraft: false,
updatedAt: NOW,
syncedAt: NOW,
mergedAt: NOW,
},
};
}

describe("isAutoSettlementCandidate", () => {
it("settles a thread whose only background work is a command left running", () => {
expect(
Expand Down Expand Up @@ -214,6 +232,39 @@ describe("isAutoSettlementCandidate", () => {
});
});

describe("awaitingAgentWake", () => {
const endedAtMs = NOW_MS;

it("holds through the grace after awaited work ends, then lets go", () => {
assert.isTrue(ThreadSettlementService.awaitingAgentWake(shell(), endedAtMs, endedAtMs + 1_000));
assert.isFalse(
ThreadSettlementService.awaitingAgentWake(shell(), endedAtMs, endedAtMs + 2 * 60 * 1_000),
);
assert.isFalse(ThreadSettlementService.awaitingAgentWake(shell(), undefined, endedAtMs));
});

it("holds a queued or running wake run past the grace, until it completes", () => {
const later = endedAtMs + 10 * 60 * 1_000;
const queued = shell({ latestRunRequestedAt: at(100), latestRunCompletedAt: null });
assert.isTrue(ThreadSettlementService.awaitingAgentWake(queued, endedAtMs, later));
const done = shell({ latestRunRequestedAt: at(100), latestRunCompletedAt: at(5_000) });
assert.isFalse(ThreadSettlementService.awaitingAgentWake(done, endedAtMs, endedAtMs + 6_000));
});

it("holds a wake run requested just before a sweep saw the work end, past the grace", () => {
// The sweep records the end when it looks, which can trail the wake run's request.
const queued = shell({ latestRunRequestedAt: at(-50), latestRunCompletedAt: null });
assert.isTrue(
ThreadSettlementService.awaitingAgentWake(queued, endedAtMs, endedAtMs + 10 * 60 * 1_000),
);
});

it("ignores a run that finished before the work ended", () => {
const earlier = shell({ latestRunRequestedAt: at(-60_000), latestRunCompletedAt: at(-30_000) });
assert.isTrue(ThreadSettlementService.awaitingAgentWake(earlier, endedAtMs, endedAtMs + 1_000));
});
});

describe("resolveAutoSettlementAt", () => {
it("keeps threads with a PR from inactivity settlement only when asked", () => {
const input = {
Expand Down Expand Up @@ -1479,21 +1530,8 @@ describe("ThreadSettlementServiceV2 worker", () => {
const releasePeriodicLookup = yield* Deferred.make<void>();
const settled = yield* Deferred.make<void>();
const branchLookupCount = yield* Ref.make(0);
const mergedLink: NonNullable<SettlementShell["pullRequests"]>[number] = {
...persistedLink("open", DateTime.makeUnsafe("2026-08-20T00:00:00.000Z")),
snapshot: {
state: "merged",
title: "Pull request",
headBranch: "feature",
baseBranch: "main",
isDraft: false,
updatedAt: NOW,
syncedAt: NOW,
mergedAt: NOW,
},
};
const waiting = makeThread("waiting-on-subagent", {
pullRequests: [mergedLink],
pullRequests: [mergedLink()],
pendingBackgroundTasks: [{ taskId: "child", kind: "subagent", description: "Review" }],
});
const slow = makeThread("slow-periodic-lookup", {
Expand Down Expand Up @@ -1546,6 +1584,201 @@ describe("ThreadSettlementServiceV2 worker", () => {
}),
);

it.effect(
"waits for the agent's wake before settling a merged thread whose awaited work ended",
() =>
Effect.gen(function* () {
yield* TestClock.setTime(Date.parse(NOW));
const settled = yield* Deferred.make<void>();
const waiting = makeThread("awaiting-deploy", {
pullRequests: [mergedLink()],
pendingBackgroundTasks: [
{ taskId: "deploy", kind: "command", description: "Wait for deploy", awaited: true },
],
});
const fixture = yield* makeHarness({
snapshot: makeSnapshot([waiting]),
onDispatch: () => Deferred.succeed(settled, undefined),
});
const at = DateTime.makeUnsafe(NOW);
const runId = RunId.make("run:awaiting-deploy");

yield* Effect.gen(function* () {
const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);

// The deploy wait finished; its agent is now being woken to carry on.
yield* Ref.set(
fixture.snapshots,
makeSnapshot([{ ...waiting, pendingBackgroundTasks: [] }]),
);
yield* fixture.publishEvent({
type: "turn-item.updated",
id: EventId.make("event:awaiting-deploy:command"),
threadId: waiting.id,
runId,
occurredAt: at,
payload: {
id: TurnItemId.make("item:awaiting-deploy:command"),
threadId: waiting.id,
runId,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: 1,
status: "completed",
title: null,
startedAt: at,
completedAt: at,
updatedAt: at,
type: "command_execution",
input: "gh run watch 42",
},
});
yield* Queue.take(fixture.snapshotReads);
yield* reactor.drain;
assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);

// The minute's full sweep still holds it inside the grace.
yield* TestClock.adjust("61 seconds");
yield* Queue.take(fixture.snapshotReads);
yield* reactor.drain;
assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);

// No wake run came, so the thread settles once the grace has passed.
yield* TestClock.adjust("59 seconds");
yield* Deferred.await(settled);
assert.deepStrictEqual(
[...new Set((yield* Ref.get(fixture.commands)).map((command) => command.threadId))],
[waiting.id],
);
}).pipe(Effect.provide(fixture.layer));
}),
);

it.effect("restarts the wake grace for a subagent that starts and ends between sweeps", () =>
Effect.gen(function* () {
yield* TestClock.setTime(Date.parse(NOW));
const settled = yield* Deferred.make<void>();
const waiting = makeThread("second-subagent", {
pullRequests: [mergedLink()],
pendingBackgroundTasks: [
{ taskId: "deploy", kind: "command", description: "Wait for deploy", awaited: true },
],
});
const fixture = yield* makeHarness({
snapshot: makeSnapshot([waiting]),
onDispatch: () => Deferred.succeed(settled, undefined),
});
const subagent = (status: "running" | "completed") =>
({
type: "subagent.updated",
id: EventId.make(`event:second-subagent:${status}`),
threadId: waiting.id,
occurredAt: DateTime.makeUnsafe(NOW),
payload: {
id: NodeId.make("node:second-subagent"),
threadId: waiting.id,
runId: null,
parentNodeId: NodeId.make("node:root"),
origin: "provider_native",
createdBy: "agent",
driver: ProviderDriverKind.make("claudeAgent"),
providerInstanceId: ProviderInstanceId.make("claudeAgent"),
providerThreadId: null,
childThreadId: null,
nativeTaskRef: null,
prompt: "Review",
title: null,
model: null,
status,
result: null,
startedAt: null,
completedAt: null,
updatedAt: DateTime.makeUnsafe(NOW),
},
}) satisfies OrchestrationV2DomainEvent;

yield* Effect.gen(function* () {
const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);

// The awaited command ends: the first wake grace starts.
yield* Ref.set(
fixture.snapshots,
makeSnapshot([{ ...waiting, pendingBackgroundTasks: [] }]),
);
yield* fixture.publishEvent(subagent("completed"));
yield* Queue.take(fixture.snapshotReads);
yield* reactor.drain;

// A second subagent starts and ends before any sweep sees it running.
yield* TestClock.adjust("30 seconds");
yield* fixture.publishEvent(subagent("running"));
yield* TestClock.adjust("20 seconds");
yield* fixture.publishEvent(subagent("completed"));
yield* Queue.take(fixture.snapshotReads);
yield* reactor.drain;

// The first grace has passed, but the second subagent's wake is still due.
yield* TestClock.adjust("80 seconds");
yield* reactor.drain;
assert.deepStrictEqual(yield* Ref.get(fixture.commands), []);

yield* TestClock.adjust("40 seconds");
yield* Deferred.await(settled);
assert.deepStrictEqual(
[...new Set((yield* Ref.get(fixture.commands)).map((command) => command.threadId))],
[waiting.id],
);
}).pipe(Effect.provide(fixture.layer));
}),
);

it.effect("settles at once when the agent's awaited work is stopped instead of ending", () =>
Effect.gen(function* () {
yield* TestClock.setTime(Date.parse(NOW));
const settled = yield* Deferred.make<void>();
const waiting = makeThread("stopped-deploy", {
pullRequests: [mergedLink()],
pendingBackgroundTasks: [
{ taskId: "deploy", kind: "command", description: "Wait for deploy", awaited: true },
],
});
const fixture = yield* makeHarness({
snapshot: makeSnapshot([waiting]),
onDispatch: () => Deferred.succeed(settled, undefined),
});

yield* Effect.gen(function* () {
const reactor = yield* ThreadSettlementService.ThreadSettlementServiceV2;
yield* startHarness(reactor, fixture.activation, fixture.snapshotReads);
yield* Ref.set(
fixture.snapshots,
makeSnapshot([{ ...waiting, pendingBackgroundTasks: [] }]),
);
yield* fixture.publishEvent({
type: "run.background-work-cancelled",
id: EventId.make("event:stopped-deploy"),
threadId: waiting.id,
occurredAt: DateTime.makeUnsafe(NOW),
payload: {
runId: RunId.make("run:stopped-deploy"),
restartCancelledBackgroundWork: [],
},
});
yield* Deferred.await(settled);
assert.deepStrictEqual(
(yield* Ref.get(fixture.commands)).map((command) => command.threadId),
[waiting.id],
);
}).pipe(Effect.provide(fixture.layer));
}),
);

it.effect("settles a stopped thread while another thread's lookup is slow", () =>
Effect.gen(function* () {
yield* TestClock.setTime(Date.parse(NOW));
Expand Down
Loading
Loading