Skip to content

Commit 7d7c3a5

Browse files
only21milclaude
andcommitted
fix(server): the memory projection store also loads armed threads early
The SQL recovery query returns an armed thread before its reset so the sweep can see an allowance restored early. The memory store still kept the old reset deadline, so the two stores disagreed. Drop it there too and cover both stores with one test. The user guide now says the continuation starts early when usage shows the allowance back, and waits for the reset after another limit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e8b28d8 commit 7d7c3a5

3 files changed

Lines changed: 79 additions & 1 deletion

File tree

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,82 @@ it.effect("SQL projection keeps background thread updates out of updatedAt", ()
911911
backgroundThreadUpdatesKeepTimestamp.pipe(Effect.provide(layerTest)),
912912
);
913913

914+
it.effect.each([
915+
["memory", ProjectionStore.layerMemory],
916+
["SQL", layerTest],
917+
] as const)("%s limit recovery loads armed threads before their reset", ([, layer]) =>
918+
Effect.gen(function* () {
919+
const store = yield* ProjectionStore.ProjectionStoreV2;
920+
const threadId = yield* addRolledBackRecoveryCandidate("armed-before-reset");
921+
const projection = yield* store.getThreadProjection(threadId);
922+
const run = projection.runs[0]!;
923+
const now = yield* DateTime.now;
924+
const resetAt = "2099-01-01T00:00:00.000Z";
925+
yield* store.apply({
926+
id: EventId.make("event:armed-before-reset:error"),
927+
type: "turn-item.updated",
928+
threadId,
929+
occurredAt: now,
930+
payload: {
931+
id: TurnItemId.make("armed-before-reset:error"),
932+
threadId,
933+
runId: run.id,
934+
nodeId: run.rootNodeId,
935+
providerThreadId: null,
936+
providerTurnId: null,
937+
nativeItemRef: null,
938+
parentItemId: null,
939+
ordinal: 2,
940+
status: "failed",
941+
title: "Usage limit reached",
942+
startedAt: now,
943+
completedAt: now,
944+
updatedAt: now,
945+
type: "error",
946+
failure: {
947+
class: "usage_limit",
948+
message: "Plan limit reached.",
949+
resetAt,
950+
code: "usageLimitExceeded",
951+
retryable: null,
952+
},
953+
},
954+
});
955+
yield* store.apply({
956+
id: EventId.make("event:armed-before-reset:failed"),
957+
type: "run.updated",
958+
threadId,
959+
occurredAt: now,
960+
payload: { ...run, status: "failed" },
961+
});
962+
const recovery = {
963+
runId: run.id,
964+
resetAt,
965+
autoResume: true,
966+
requestId: CommandId.make("recovery:armed-before-reset"),
967+
};
968+
const updateThread = (suffix: string, patch: Partial<typeof projection.thread>) =>
969+
store.apply({
970+
id: EventId.make(`event:armed-before-reset:${suffix}`),
971+
type: "thread.metadata-updated",
972+
threadId,
973+
occurredAt: now,
974+
payload: { ...projection.thread, limitRecovery: recovery, ...patch },
975+
});
976+
const candidate = Effect.map(
977+
store.getLimitRecoveryCandidates({ now, autoResume: false, snooze: false }),
978+
(rows) => rows.find((row) => row.id === threadId),
979+
);
980+
981+
yield* updateThread("armed", {});
982+
assert.deepEqual((yield* candidate)?.limitRecovery, recovery);
983+
yield* updateThread("snoozed", { snoozedUntil: DateTime.makeUnsafe(resetAt) });
984+
assert.isUndefined(yield* candidate);
985+
yield* updateThread("cancelled", { limitRecovery: { ...recovery, autoResume: false } });
986+
assert.isUndefined(yield* candidate);
987+
}).pipe(Effect.provide(layer)),
988+
);
989+
914990
it.effect("memory recovery selection includes unfinished items from missing runs", () =>
915991
Effect.gen(function* () {
916992
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6809,7 +6809,6 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
68096809
}
68106810
return (
68116811
thread.limitRecovery.autoResume &&
6812-
resetMs <= nowMs &&
68136812
(thread.snoozedUntil == null ||
68146813
DateTime.toEpochMillis(thread.snoozedUntil) <= nowMs)
68156814
);

‎docs/user/thread-sidebar.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ When the provider reports a reset time, choose **Resume at reset** to schedule a
216216
continuation. You can cancel it from the thread. Enable **Auto-resume limited
217217
threads** in **Settings → General** on web and desktop, or **Settings → Thread
218218
behavior** on mobile, to schedule limit stops by default.
219+
If the provider's usage shows the allowance back before the reset, for example
220+
after you redeem a reset credit, the continuation starts then. A thread that hits
221+
the limit again before the reset waits for the reset.
219222
The environment must be running when the reset arrives; it resumes overdue
220223
continuations after a restart. Stopping the thread, sending a new message,
221224
archiving, or settling it prevents a pending continuation from starting.

0 commit comments

Comments
 (0)