Skip to content
TypeScript

fix(server): restart inactivity after snoozed threads wake (#16674)

maria0a4d78912810
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 4f870a606e0..977b225f525 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -2831,6 +2831,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...thread,
snoozedUntil: null,
snoozedAt: null,
+ lastSnoozeWakeAt: alreadyAwake ? thread.lastSnoozeWakeAt : now,
updatedAt: alreadyAwake ? thread.updatedAt : now,
};
}
@@ -2862,6 +2863,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
settledAt: thread.settledOverride === "settled" ? null : thread.settledAt,
snoozedUntil: null,
snoozedAt: null,
+ lastSnoozeWakeAt: thread.snoozedUntil == null ? thread.lastSnoozeWakeAt : now,
updatedAt: alreadyPinned && !promotes ? thread.updatedAt : now,
};
}
@@ -2933,7 +2935,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
thread.snoozedUntil != null &&
DateTime.toEpochMillis(thread.snoozedUntil) ===
Date.parse(thread.limitRecovery.resetAt)
- ? { snoozedUntil: null, snoozedAt: null }
+ ? { snoozedUntil: null, snoozedAt: null, lastSnoozeWakeAt: now }
: {}),
...(command.branch === undefined ? {} : { branch: command.branch }),
...(command.worktreePath === undefined ? {} : { worktreePath: command.worktreePath }),
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index a6a092ff84b..8c33faef808 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -204,7 +204,10 @@ export type ProjectionSettlementCandidate = Pick<
| "activityRunStatus"
| "pendingRuntimeRequest"
| "pendingBackgroundTasks"
-> & { readonly latestUserAuthoredMessageAt: DateTime.Utc | null };
+> &
+ Pick<OrchestrationV2AppThread, "lastSnoozeWakeAt"> & {
+ readonly latestUserAuthoredMessageAt: DateTime.Utc | null;
+ };
const ProjectionCheckpointContext = Schema.Struct({
runs: Schema.Array(
@@ -6035,6 +6038,7 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
const shell = threadShellFromProjection(projection);
return {
...shell,
+ lastSnoozeWakeAt: projection.thread.lastSnoozeWakeAt ?? null,
latestUserAuthoredMessageAt: shell.latestUserAuthoredMessageAt ?? null,
};
})
diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
index 4b0e451e0ae..00daff5a8c8 100644
--- a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
@@ -276,6 +276,26 @@ describe("resolveAutoSettlementAt", () => {
).toBeNull();
});
+ it.each(["manual", "timer"])("restarts inactivity on a %s snooze wake", (wake) => {
+ const thread = {
+ ...shell({ latestRunCompletedAt: at(-4 * DAY_MS) }),
+ ...(wake === "manual"
+ ? { lastSnoozeWakeAt: at(-1_000) }
+ : { snoozedAt: at(-3 * DAY_MS), snoozedUntil: at(-1_000) }),
+ };
+ const input = {
+ thread,
+ pullRequest: null,
+ nowMs: NOW_MS,
+ autoSettleAfterDays: 1,
+ autoSettleOnMerge: false,
+ };
+ expect(ThreadSettlementService.resolveAutoSettlementAt(input)).toBeNull();
+ expect(
+ ThreadSettlementService.resolveAutoSettlementAt({ ...input, nowMs: NOW_MS + DAY_MS }),
+ ).toEqual(at(-1_000));
+ });
+
it("settles on merge only after the user's last action and preserves the activity time", () => {
const thread = shell({
latestUserMessageAt: at(-2 * 60 * 60 * 1_000),
diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.ts
index 5e3f7e79560..8b55e25cdc3 100644
--- a/apps/server/src/orchestration-v2/ThreadSettlementService.ts
+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.ts
@@ -237,8 +237,14 @@ export function resolveAutoSettlementAt(input: {
return activityAtMs === null ? thread.createdAt : DateTime.makeUnsafe(activityAtMs);
}
if (input.autoSettleAfterDays === null || activityAtMs === null) return null;
- return activityAtMs < input.nowMs - input.autoSettleAfterDays * DAY_MS
- ? DateTime.makeUnsafe(activityAtMs)
+ // A passed wake timer restarts inactivity. An earlier wake can only delay settlement.
+ const snoozedUntilMs = toMillis(thread.snoozedUntil);
+ const timerWakeAtMs =
+ snoozedUntilMs !== null && snoozedUntilMs <= input.nowMs ? snoozedUntilMs : null;
+ const inactivityAtMs =
+ latestMillis([activityAtMs, toMillis(thread.lastSnoozeWakeAt), timerWakeAtMs]) ?? activityAtMs;
+ return inactivityAtMs < input.nowMs - input.autoSettleAfterDays * DAY_MS
+ ? DateTime.makeUnsafe(inactivityAtMs)
: null;
}
diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
index d805bab92d8..b11184f4463 100644
--- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts
+++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
@@ -4548,6 +4548,24 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
assert.deepEqual(thread.snoozedAt, firstSnoozedAt);
assert.deepEqual(thread.updatedAt, firstUpdatedAt);
+ yield* orchestrator.dispatch({
+ type: "thread.unsnooze",
+ commandId: CommandId.make("runtime-layer-snoozed-thread-wake"),
+ threadId,
+ reason: "user",
+ });
+ const projections = yield* ProjectionStore.ProjectionStoreV2.pipe(
+ Effect.provide(ProjectionStore.layer),
+ );
+ const [candidate] = yield* projections.getSettlementCandidates(threadId);
+ assert.deepEqual(candidate?.lastSnoozeWakeAt, yield* DateTime.now);
+ yield* orchestrator.dispatch({
+ type: "thread.snooze",
+ commandId: CommandId.make("runtime-layer-snoozed-thread-snooze-after-wake"),
+ threadId,
+ snoozedUntil,
+ });
+
yield* orchestrator.dispatch({
type: "message.dispatch",
createdBy: "user",
diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts
index 7a1e56b6d5e..591420ce73c 100644
--- a/packages/contracts/src/orchestrationV2.ts
+++ b/packages/contracts/src/orchestrationV2.ts
@@ -404,6 +404,8 @@ export const OrchestrationV2AppThread = Schema.Struct({
unsettledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
snoozedUntil: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
snoozedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
+ /** Manual wakes restart inactivity without changing the sidebar's sort position. */
+ lastSnoozeWakeAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
limitRecovery: Schema.optional(Schema.NullOr(OrchestrationV2LimitRecovery)),
pinnedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
autoSettleDisabledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
@@ -1999,6 +2001,7 @@ export const OrchestrationV2AppThreadJson = OrchestrationV2AppThread.mapFields((
unsettledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
snoozedUntil: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
snoozedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
+ lastSnoozeWakeAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
pinnedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
autoSettleDisabledAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
lastVisitedAt: Schema.NullOr(Schema.DateTimeUtcFromString).pipe(