Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
...thread,
snoozedUntil: null,
snoozedAt: null,
lastSnoozeWakeAt: alreadyAwake ? thread.lastSnoozeWakeAt : now,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
updatedAt: alreadyAwake ? thread.updatedAt : now,
};
}
Expand Down Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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 }),
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
};
})
Expand Down
20 changes: 20 additions & 0 deletions apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 8 additions & 2 deletions apps/server/src/orchestration-v2/ThreadSettlementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return inactivityAtMs < input.nowMs - input.autoSettleAfterDays * DAY_MS
? DateTime.makeUnsafe(inactivityAtMs)
: null;
}

Expand Down
18 changes: 18 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/src/orchestrationV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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(
Expand Down
Loading