Skip to content

Commit 1c443f6

Browse files
committed
fix(server): restart inactivity after snoozed threads wake
1 parent f4f148e commit 1c443f6

7 files changed

Lines changed: 138 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2808,6 +2808,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
28082808
...thread,
28092809
snoozedUntil: null,
28102810
snoozedAt: null,
2811+
lastSnoozeWakeAt: alreadyAwake ? thread.lastSnoozeWakeAt : now,
28112812
updatedAt: alreadyAwake ? thread.updatedAt : now,
28122813
};
28132814
}
@@ -2910,7 +2911,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
29102911
thread.snoozedUntil != null &&
29112912
DateTime.toEpochMillis(thread.snoozedUntil) ===
29122913
Date.parse(thread.limitRecovery.resetAt)
2913-
? { snoozedUntil: null, snoozedAt: null }
2914+
? { snoozedUntil: null, snoozedAt: null, lastSnoozeWakeAt: now }
29142915
: {}),
29152916
...(command.branch === undefined ? {} : { branch: command.branch }),
29162917
...(command.worktreePath === undefined ? {} : { worktreePath: command.worktreePath }),

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,35 @@ const createItem = Effect.fn(function* (
140140
});
141141
});
142142

143+
it.effect.each([
144+
["sql", layerSql],
145+
["memory", ProjectionStore.layerMemory],
146+
] as const)("%s: manual wakes restart the persisted inactivity clock", ([, testLayer]) =>
147+
Effect.gen(function* () {
148+
const store = yield* ProjectionStore.ProjectionStoreV2;
149+
const threadId = yield* createThread("manually-woken");
150+
yield* createRun(threadId);
151+
const before = (yield* store.getSettlementCandidates(threadId))[0]!;
152+
const settings = {
153+
pullRequest: null,
154+
nowMs: DateTime.toEpochMillis(now),
155+
autoSettleAfterDays: 1,
156+
autoSettleOnMerge: false,
157+
};
158+
assert.deepEqual(resolveAutoSettlementAt({ ...settings, thread: before }), old);
159+
yield* store.apply({
160+
id: EventId.make("event:settlement:manual-wake"),
161+
type: "thread.unsnoozed",
162+
threadId,
163+
occurredAt: now,
164+
payload: { ...(yield* store.getThread(threadId)), lastSnoozeWakeAt: now },
165+
});
166+
const after = (yield* store.getSettlementCandidates(threadId))[0]!;
167+
assert.deepEqual(after.lastSnoozeWakeAt, now);
168+
assert.isNull(resolveAutoSettlementAt({ ...settings, thread: after }));
169+
}).pipe(Effect.provide(testLayer)),
170+
);
171+
143172
it.effect.each([
144173
["sql", layerSql],
145174
["memory", ProjectionStore.layerMemory],

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ export type ProjectionSettlementCandidate = Pick<
191191
| "activityRunStatus"
192192
| "pendingRuntimeRequest"
193193
| "pendingBackgroundTasks"
194-
> & { readonly latestUserAuthoredMessageAt: DateTime.Utc | null };
194+
> &
195+
Pick<OrchestrationV2AppThread, "lastSnoozeWakeAt"> & {
196+
readonly latestUserAuthoredMessageAt: DateTime.Utc | null;
197+
};
195198

196199
const ProjectionCheckpointContext = Schema.Struct({
197200
runs: Schema.Array(
@@ -5289,6 +5292,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52895292
autoSettleDisabledAt: thread.autoSettleDisabledAt ?? null,
52905293
snoozedUntil: thread.snoozedUntil ?? null,
52915294
snoozedAt: thread.snoozedAt ?? null,
5295+
lastSnoozeWakeAt: thread.lastSnoozeWakeAt ?? null,
52925296
status,
52935297
latestRunId,
52945298
latestRunRequestedAt:
@@ -5802,6 +5806,7 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
58025806
const shell = threadShellFromProjection(projection);
58035807
return {
58045808
...shell,
5809+
lastSnoozeWakeAt: projection.thread.lastSnoozeWakeAt ?? null,
58055810
latestUserAuthoredMessageAt: shell.latestUserAuthoredMessageAt ?? null,
58065811
};
58075812
})

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,42 @@ describe("resolveAutoSettlementAt", () => {
275275
).toBeNull();
276276
});
277277

278+
it.each(["manual", "timer"])("restarts inactivity on a %s snooze wake", (wake) => {
279+
const thread = {
280+
...shell({ latestRunCompletedAt: at(-4 * DAY_MS) }),
281+
...(wake === "manual"
282+
? { lastSnoozeWakeAt: at(-1_000) }
283+
: { snoozedAt: at(-3 * DAY_MS), snoozedUntil: at(-1_000) }),
284+
};
285+
const input = {
286+
thread,
287+
pullRequest: null,
288+
nowMs: NOW_MS,
289+
autoSettleAfterDays: 1,
290+
autoSettleOnMerge: false,
291+
};
292+
expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toBeNull();
293+
expect(
294+
ThreadSettlementService.resolveAutoSettlementAt({ ...input, nowMs: NOW_MS + DAY_MS }),
295+
).toEqual(at(-1_000));
296+
});
297+
298+
it("does not restart inactivity at the timer after work woke the thread early", () => {
299+
expect(
300+
ThreadSettlementService.resolveAutoSettlementAt({
301+
thread: shell({
302+
snoozedAt: at(-5 * DAY_MS),
303+
latestRunCompletedAt: at(-4 * DAY_MS),
304+
snoozedUntil: at(-1_000),
305+
}),
306+
pullRequest: null,
307+
nowMs: NOW_MS,
308+
autoSettleAfterDays: 1,
309+
autoSettleOnMerge: false,
310+
}),
311+
).toEqual(at(-4 * DAY_MS));
312+
});
313+
278314
it("settles on merge only after the user's last action and preserves the activity time", () => {
279315
const thread = shell({
280316
latestUserMessageAt: at(-2 * 60 * 60 * 1_000),
@@ -291,6 +327,12 @@ describe("resolveAutoSettlementAt", () => {
291327
autoSettleOnMerge: true,
292328
};
293329
expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toEqual(at(-90 * 60 * 1_000));
330+
expect(
331+
ThreadSettlementService.resolveAutoSettlementAt({
332+
...input,
333+
thread: { ...thread, lastSnoozeWakeAt: at(-1_000) },
334+
}),
335+
).toEqual(at(-90 * 60 * 1_000));
294336
expect(
295337
ThreadSettlementService.resolveAutoSettlementAt({ ...input, autoSettleOnMerge: false }),
296338
).toBeNull();

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,24 @@ export function resolveAutoSettlementAt(input: {
203203
return activityAtMs === null ? thread.createdAt : DateTime.makeUnsafe(activityAtMs);
204204
}
205205
if (input.autoSettleAfterDays === null || activityAtMs === null) return null;
206-
return activityAtMs < input.nowMs - input.autoSettleAfterDays * DAY_MS
207-
? DateTime.makeUnsafe(activityAtMs)
206+
const snoozedUntilMs = toMillis(thread.snoozedUntil);
207+
const snoozedAtMs = toMillis(thread.snoozedAt);
208+
const completedAtMs = toMillis(thread.latestRunCompletedAt);
209+
// Timer wakes are derived. Work that completed after snoozing woke early,
210+
// so its original timer must not restart inactivity a second time.
211+
const timerWakeAtMs =
212+
snoozedUntilMs !== null &&
213+
snoozedUntilMs <= input.nowMs &&
214+
(snoozedAtMs === null || completedAtMs === null || completedAtMs <= snoozedAtMs)
215+
? snoozedUntilMs
216+
: null;
217+
const inactivityAtMs = Math.max(
218+
activityAtMs,
219+
toMillis(thread.lastSnoozeWakeAt) ?? activityAtMs,
220+
timerWakeAtMs ?? activityAtMs,
221+
);
222+
return inactivityAtMs < input.nowMs - input.autoSettleAfterDays * DAY_MS
223+
? DateTime.makeUnsafe(inactivityAtMs)
208224
: null;
209225
}
210226

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4473,6 +4473,40 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
44734473
assert.deepEqual(thread.snoozedAt, firstSnoozedAt);
44744474
assert.deepEqual(thread.updatedAt, firstUpdatedAt);
44754475

4476+
yield* orchestrator.dispatch({
4477+
type: "thread.unsnooze",
4478+
commandId: CommandId.make("runtime-layer-snoozed-thread-wake"),
4479+
threadId,
4480+
reason: "user",
4481+
});
4482+
const manuallyAwakened = yield* orchestrator.getThreadProjection(threadId);
4483+
const wakeAt = manuallyAwakened.thread.lastSnoozeWakeAt;
4484+
assert.isNotNull(wakeAt);
4485+
assert.isNull(manuallyAwakened.thread.snoozedUntil);
4486+
assert.deepEqual(manuallyAwakened.thread.unsettledAt, firstProjection.thread.unsettledAt);
4487+
const projections = yield* ProjectionStore.ProjectionStoreV2.pipe(
4488+
Effect.provide(ProjectionStore.layer),
4489+
);
4490+
const candidate = (yield* projections.getSettlementCandidates(threadId))[0];
4491+
assert.deepEqual(candidate?.lastSnoozeWakeAt, wakeAt);
4492+
yield* TestClock.adjust("10 seconds");
4493+
yield* orchestrator.dispatch({
4494+
type: "thread.unsnooze",
4495+
commandId: CommandId.make("runtime-layer-snoozed-thread-wake-again"),
4496+
threadId,
4497+
reason: "user",
4498+
});
4499+
assert.deepEqual(
4500+
(yield* orchestrator.getThreadProjection(threadId)).thread.lastSnoozeWakeAt,
4501+
wakeAt,
4502+
);
4503+
yield* orchestrator.dispatch({
4504+
type: "thread.snooze",
4505+
commandId: CommandId.make("runtime-layer-snoozed-thread-snooze-after-wake"),
4506+
threadId,
4507+
snoozedUntil,
4508+
});
4509+
44764510
yield* orchestrator.dispatch({
44774511
type: "message.dispatch",
44784512
createdBy: "user",
@@ -5055,6 +5089,10 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
50555089
} else {
50565090
assert.isNull(armedShell.snoozedUntil);
50575091
assert.isNull(armedShell.snoozedAt);
5092+
assert.deepEqual(
5093+
(yield* orchestrator.getThreadProjection(threadId)).thread.lastSnoozeWakeAt,
5094+
yield* DateTime.now,
5095+
);
50585096
assert.isTrue(armedShell.limitRecovery!.autoResume);
50595097
}
50605098
}

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ export const OrchestrationV2AppThread = Schema.Struct({
404404
unsettledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
405405
snoozedUntil: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
406406
snoozedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
407+
/** Manual wakes restart inactivity without changing the sidebar's sort position. */
408+
lastSnoozeWakeAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
407409
limitRecovery: Schema.optional(Schema.NullOr(OrchestrationV2LimitRecovery)),
408410
pinnedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
409411
autoSettleDisabledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
@@ -1996,6 +1998,7 @@ export const OrchestrationV2AppThreadJson = OrchestrationV2AppThread.mapFields((
19961998
unsettledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
19971999
snoozedUntil: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
19982000
snoozedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
2001+
lastSnoozeWakeAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
19992002
pinnedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
20002003
autoSettleDisabledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
20012004
lastVisitedAt: Schema.NullOr(Schema.DateTimeUtcFromString).pipe(

0 commit comments

Comments
 (0)