Skip to content

Commit 7e2d2d5

Browse files
committed
fix(threads): keep blocking events visible after snooze
1 parent 843f461 commit 7e2d2d5

4 files changed

Lines changed: 108 additions & 2 deletions

File tree

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

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
NodeId,
66
type OrchestrationV2AppThread,
77
type OrchestrationV2Run,
8+
type OrchestrationV2RuntimeRequest,
89
ProjectId,
910
ProviderDriverKind,
1011
ProviderInstanceId,
@@ -23,7 +24,11 @@ import * as Statement from "effect/sql/Statement";
2324

2425
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2526
import * as ProjectionStore from "./ProjectionStore.ts";
26-
import { isAutoSettlementCandidate, resolveAutoSettlementAt } from "./ThreadSettlementService.ts";
27+
import {
28+
isAutoSettlementCandidate,
29+
isSnoozed,
30+
resolveAutoSettlementAt,
31+
} from "./ThreadSettlementService.ts";
2732

2833
const layerSql = ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory));
2934
const now = DateTime.makeUnsafe("2026-09-04T12:00:00Z");
@@ -140,6 +145,78 @@ const createItem = Effect.fn(function* (
140145
});
141146
});
142147

148+
it.effect.each([
149+
["sql", layerSql],
150+
["memory", ProjectionStore.layerMemory],
151+
] as const)(
152+
"%s: pending approvals wake a snoozed question even with older creation times",
153+
([, testLayer]) =>
154+
Effect.gen(function* () {
155+
const store = yield* ProjectionStore.ProjectionStoreV2;
156+
const threadId = yield* createThread("snoozed-question-approval", {
157+
snoozedAt: now,
158+
snoozedUntil: DateTime.add(now, { days: 1 }),
159+
});
160+
const question: OrchestrationV2RuntimeRequest = {
161+
id: RuntimeRequestId.make("question"),
162+
nodeId: NodeId.make("node:question"),
163+
providerTurnId: null,
164+
nativeRequestRef: null,
165+
kind: "user_input",
166+
status: "pending",
167+
responseCapability: { type: "message" },
168+
createdAt: DateTime.subtract(now, { hours: 1 }),
169+
resolvedAt: null,
170+
};
171+
const approval: OrchestrationV2RuntimeRequest = {
172+
...question,
173+
id: RuntimeRequestId.make("approval"),
174+
kind: "command",
175+
createdAt: old,
176+
};
177+
const newerApproval = {
178+
...approval,
179+
id: RuntimeRequestId.make("newer-approval"),
180+
createdAt: DateTime.add(old, { hours: 1 }),
181+
};
182+
const setRequest = (request: OrchestrationV2RuntimeRequest) =>
183+
store.apply({
184+
id: EventId.make(`event:${request.id}:${request.status}`),
185+
type: "runtime-request.updated",
186+
threadId,
187+
occurredAt: now,
188+
payload: request,
189+
});
190+
const assertPending = (expected: RuntimeRequestId, snoozed: boolean) =>
191+
Effect.gen(function* () {
192+
const snapshot = yield* store.getShellSnapshot();
193+
const shell = snapshot.threads.find((thread) => thread.id === threadId)!;
194+
const projection = yield* store.getThreadProjection(threadId);
195+
for (const candidate of [shell, ProjectionStore.threadShellFromProjection(projection)]) {
196+
assert.equal(candidate.pendingRuntimeRequest?.id, expected);
197+
assert.equal(isSnoozed(candidate, DateTime.toEpochMillis(now)), snoozed);
198+
}
199+
});
200+
yield* setRequest({
201+
...question,
202+
id: RuntimeRequestId.make("older-question"),
203+
createdAt: old,
204+
});
205+
yield* setRequest(question);
206+
yield* assertPending(question.id, true);
207+
208+
// A request becoming pending can retain a creation time older than the snooze.
209+
yield* setRequest(approval);
210+
yield* assertPending(approval.id, false);
211+
yield* setRequest(newerApproval);
212+
yield* assertPending(newerApproval.id, false);
213+
yield* setRequest({ ...newerApproval, status: "resolved", resolvedAt: now });
214+
yield* assertPending(approval.id, false);
215+
yield* setRequest({ ...approval, status: "resolved", resolvedAt: now });
216+
yield* assertPending(question.id, true);
217+
}).pipe(Effect.provide(testLayer)),
218+
);
219+
143220
it.effect.each([
144221
["sql", layerSql],
145222
["memory", ProjectionStore.layerMemory],

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,11 +1478,13 @@ export function threadShellFromProjection(
14781478
.filter(isActivityRunForShell)
14791479
.toSorted((left, right) => right.ordinal - left.ordinal)[0] ?? null;
14801480
const liveRunIds = new Set(projection.runs.filter(isActivityRunForShell).map((run) => run.id));
1481+
// A snoozable question must not hide an approval that needs attention.
14811482
const pendingRuntimeRequest =
14821483
projection.runtimeRequests
14831484
.filter((request) => request.status === "pending")
14841485
.toSorted(
14851486
(left, right) =>
1487+
Number(left.kind === "user_input") - Number(right.kind === "user_input") ||
14861488
DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),
14871489
)[0] ??
14881490
secretRequestAsPendingInput(
@@ -5388,7 +5390,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
53885390
FROM orchestration_v2_projection_runtime_requests request
53895391
WHERE request.thread_id = t.thread_id
53905392
AND request.status = 'pending'
5391-
ORDER BY request.created_at DESC, request.runtime_request_id DESC
5393+
ORDER BY (request.kind = 'user_input') ASC,
5394+
request.created_at DESC, request.runtime_request_id DESC
53925395
LIMIT 1
53935396
) AS pending_request_payload_json,
53945397
(

‎packages/client-runtime/src/state/threadSettled.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ export function threadWokeAt(
225225
shell.snoozedAt != null &&
226226
Date.parse(shell.pendingUserInputAt) > Date.parse(shell.snoozedAt)
227227
) {
228+
if (
229+
(runtime?.status === "error" || runtime?.status === "failed") &&
230+
runtime.updatedAt != null &&
231+
Date.parse(runtime.updatedAt) > Date.parse(shell.pendingUserInputAt)
232+
) {
233+
return runtime.updatedAt;
234+
}
228235
return shell.pendingUserInputAt;
229236
}
230237
return runtime?.updatedAt ?? shell.snoozedAt ?? null;

‎packages/client-runtime/src/state/threadSnoozed.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,25 @@ describe("threadWokeAt", () => {
330330
},
331331
);
332332

333+
it.each([
334+
["error", "2026-04-10T10:00:00.000Z", "2026-04-10T11:00:00.000Z"],
335+
["failed", "2026-04-10T10:00:00.000Z", "2026-04-10T11:00:00.000Z"],
336+
["failed", "2026-04-10T11:30:00.000Z", "2026-04-10T11:30:00.000Z"],
337+
["running", "2026-04-10T10:00:00.000Z", "2026-04-10T10:00:00.000Z"],
338+
] as const)(
339+
"uses the latest wake event for a %s runtime and a question at %s",
340+
(status, pendingUserInputAt, expectedWakeAt) => {
341+
const shell = {
342+
...makeShell({ snoozedUntil: FUTURE_WAKE, pending: "user-input", pendingUserInputAt }),
343+
runtime: { status, updatedAt: "2026-04-10T11:00:00.000Z" },
344+
};
345+
const lastVisitedAt = "2026-04-10T10:30:00.000Z";
346+
const wokeAt = threadWokeAt(shell, { now: NOW });
347+
expect(wokeAt).toBe(expectedWakeAt);
348+
expect(Date.parse(wokeAt!) > Date.parse(lastVisitedAt)).toBe(status !== "running");
349+
},
350+
);
351+
333352
it("keeps the early wake authoritative after the scheduled time passes", () => {
334353
// Woke early at 10:30 via run-completed; the scheduled wake (PAST_WAKE
335354
// 10:00 relative to a later now) has ALSO passed. Reporting the

0 commit comments

Comments
 (0)