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

Co-authored-by: maria-rcks <[email protected]>
This commit is contained in:
mariaandmaria-rcks authored and GitHub committed 2026-10-09 16:22:38 -03:00
1 parent 5722496b79
commit 0a4d789128
6 files changed
+57 -4

No files matched your search

@@ -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 }),
@@ -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,
};
})
@@ -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),
@@ -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;
}
@@ -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",
@@ -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(