Skip to content

Commit 1c2122b

Browse files
committed
fix(server): stopped usage-limit threads no longer auto-resume
1 parent ea97e8b commit 1c2122b

5 files changed

Lines changed: 68 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4540,7 +4540,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
45404540
projection.thread.providerInstanceId !== run.providerInstanceId ||
45414541
projection.runtimeRequests.some((request) => request.status === "pending") ||
45424542
(projection.thread.snoozedUntil != null &&
4543-
DateTime.toEpochMillis(projection.thread.snoozedUntil) > DateTime.toEpochMillis(now))
4543+
DateTime.toEpochMillis(projection.thread.snoozedUntil) > DateTime.toEpochMillis(now)) ||
4544+
(yield* stopReachedRun(command, command.threadId, run.id))
45444545
) {
45454546
yield* emit(
45464547
events,

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,6 +3596,37 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
35963596
now: reset,
35973597
})).find((row) => row.id === threadId)!;
35983598
assert.deepEqual(due.limitRecovery, recovery);
3599+
const interruptRequestId = TurnItemId.make("limit-shell:interrupt-request");
3600+
yield* store.apply({
3601+
id: EventId.make("event:limit-shell:interrupt-request"),
3602+
type: "turn-item.updated",
3603+
threadId,
3604+
occurredAt: now,
3605+
payload: {
3606+
id: interruptRequestId,
3607+
threadId,
3608+
runId: original.id,
3609+
nodeId: original.rootNodeId,
3610+
providerThreadId: null,
3611+
providerTurnId: null,
3612+
nativeItemRef: null,
3613+
parentItemId: null,
3614+
ordinal: 3,
3615+
status: "completed",
3616+
title: "Interrupt requested",
3617+
startedAt: now,
3618+
completedAt: now,
3619+
updatedAt: now,
3620+
type: "run_interrupt_request",
3621+
message: "Interrupt requested",
3622+
},
3623+
});
3624+
assert.isUndefined(
3625+
(yield* store.getLimitRecoveryCandidates({ ...recoveryOptions, now: reset })).find(
3626+
(row) => row.id === threadId,
3627+
),
3628+
);
3629+
yield* sql`DELETE FROM orchestration_v2_projection_turn_items WHERE turn_item_id = ${interruptRequestId}`;
35993630
yield* sql`INSERT INTO orchestration_v2_projection_runtime_requests
36003631
(runtime_request_id, thread_id, node_id, kind, status, created_at, payload_json)
36013632
VALUES ('limit-shell:pending-request', ${threadId}, ${original.rootNodeId}, 'approval', 'pending', ${DateTime.formatIso(now)}, '{}')`;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,6 +3611,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
36113611
SELECT 1 FROM orchestration_v2_projection_runtime_requests request
36123612
WHERE request.thread_id = t.thread_id AND request.status = 'pending'
36133613
)
3614+
-- Stop reached this run: only a fresh user action may resume it.
3615+
AND NOT EXISTS (
3616+
SELECT 1 FROM orchestration_v2_projection_turn_items stop
3617+
WHERE stop.thread_id = t.thread_id AND stop.run_id = r.run_id
3618+
AND stop.type = 'run_interrupt_request'
3619+
)
36143620
ORDER BY t.thread_id
36153621
`;
36163622
const candidates: Array<ProjectionLimitRecoveryCandidate> = [];

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5249,6 +5249,7 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
52495249
"independent-patches",
52505250
"expired-snooze",
52515251
"wake",
5252+
"stop",
52525253
] as const)("guards a scheduled usage-limit continuation against %s", (scenario) =>
52535254
Effect.gen(function* () {
52545255
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -5619,6 +5620,12 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
56195620
commandId: CommandId.make(`recovery:settle:${scenario}`),
56205621
threadId,
56215622
});
5623+
if (scenario === "stop")
5624+
yield* orchestrator.dispatch({
5625+
type: "thread.stop",
5626+
commandId: CommandId.make(`recovery:stop:${scenario}`),
5627+
threadId,
5628+
});
56225629
if (scenario === "replacement") {
56235630
const current = yield* orchestrator.getThreadProjection(threadId);
56245631
const error = current.turnItems.find((item) => item.type === "error")!;
@@ -5697,6 +5704,26 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
56975704
? 1
56985705
: 0),
56995706
);
5707+
if (scenario === "stop") {
5708+
assert.isTrue(
5709+
after.turnItems.some(
5710+
(item) => item.type === "run_interrupt_request" && item.runId === run.id,
5711+
),
5712+
);
5713+
yield* orchestrator.dispatch({
5714+
type: "message.dispatch",
5715+
commandId: CommandId.make("recovery:manual-resume:stop"),
5716+
messageId: MessageId.make("recovery:manual-resume:stop"),
5717+
threadId,
5718+
manualContinuationOfRunId: run.id,
5719+
text: "Continue where you left off.",
5720+
attachments: [],
5721+
dispatchMode: { type: "start_immediately" },
5722+
createdBy: "user",
5723+
creationSource: "web",
5724+
});
5725+
assert.lengthOf((yield* orchestrator.getThreadProjection(threadId)).runs, 2);
5726+
}
57005727
if (scenario === "queued-resume") {
57015728
assert.equal(after.runs[1]?.status, "queued");
57025729
assert.isTrue(after.runs[1]?.queueHeld);

‎docs/user/thread-sidebar.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ continuation. You can cancel it from the thread. Enable **Auto-resume limited
205205
threads** in **Settings → General** on web and desktop, or **Settings → Thread
206206
behavior** on mobile, to schedule limit stops by default.
207207
The environment must be running when the reset arrives; it resumes overdue
208-
continuations after a restart. Sending a new message, archiving, or settling the
209-
thread prevents a pending continuation from starting.
208+
continuations after a restart. Stopping the thread, sending a new message,
209+
archiving, or settling it prevents a pending continuation from starting.
210210

211211
Choose **Snooze until reset** to hide the thread until its allowance returns.
212212
Snooze and auto-resume are independent: snooze alone wakes the thread without

0 commit comments

Comments
 (0)