Skip to content

Commit a5636e3

Browse files
0bkevinUbuntu
authored andcommitted
fix(server): settle snoozed threads immediately
1 parent 7b38fb5 commit a5636e3

2 files changed

Lines changed: 102 additions & 13 deletions

File tree

‎apps/server/src/orchestration/decider.settled.test.ts‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ function makeReadModel(
2424
session: OrchestrationSession | null = null,
2525
activities: OrchestrationThread["activities"] = [],
2626
messages: OrchestrationThread["messages"] = [],
27+
lifecycle: {
28+
readonly pinnedAt?: string | null;
29+
readonly snoozedUntil?: string | null;
30+
readonly snoozedAt?: string | null;
31+
} = {},
2732
): OrchestrationReadModel {
2833
return {
2934
snapshotSequence: 0,
@@ -44,6 +49,9 @@ function makeReadModel(
4449
archivedAt,
4550
settledOverride,
4651
settledAt: settledOverride === "settled" ? SETTLED_AT : null,
52+
snoozedUntil: lifecycle.snoozedUntil ?? null,
53+
snoozedAt: lifecycle.snoozedAt ?? (lifecycle.snoozedUntil != null ? SETTLED_AT : null),
54+
pinnedAt: lifecycle.pinnedAt ?? null,
4755
deletedAt: null,
4856
messages,
4957
proposedPlans: [],
@@ -69,7 +77,7 @@ function makeSession(status: OrchestrationSession["status"]): OrchestrationSessi
6977
}
7078

7179
it.layer(NodeServices.layer)("settled thread decider", (it) => {
72-
it.effect("settles active threads and re-emits idempotently for settled ones", () =>
80+
it.effect("settles awake threads without a redundant wake and re-emits idempotently", () =>
7381
Effect.gen(function* () {
7482
const event = yield* decideOrchestrationCommand({
7583
command: {
@@ -108,6 +116,75 @@ it.layer(NodeServices.layer)("settled thread decider", (it) => {
108116
}),
109117
);
110118

119+
it.effect("settling a snoozed thread also wakes it", () =>
120+
Effect.gen(function* () {
121+
const result = yield* decideOrchestrationCommand({
122+
command: {
123+
type: "thread.settle",
124+
commandId: CommandId.make("cmd-settle-snoozed"),
125+
threadId: ThreadId.make("thread-1"),
126+
},
127+
readModel: makeReadModel(null, null, null, [], [], {
128+
snoozedUntil: "1970-01-02T09:00:00.000Z",
129+
}),
130+
});
131+
const events = Array.isArray(result) ? result : [result];
132+
expect(events.map((entry) => entry.type)).toEqual(["thread.settled", "thread.unsnoozed"]);
133+
const settled = events.find((entry) => entry.type === "thread.settled");
134+
const unsnoozed = events.find((entry) => entry.type === "thread.unsnoozed");
135+
if (settled?.type === "thread.settled" && unsnoozed?.type === "thread.unsnoozed") {
136+
expect(unsnoozed.payload.reason).toBe("user");
137+
expect(unsnoozed.payload.updatedAt).toBe(settled.payload.updatedAt);
138+
}
139+
}),
140+
);
141+
142+
it.effect("repeated settle repairs legacy settled and snoozed state", () =>
143+
Effect.gen(function* () {
144+
const result = yield* decideOrchestrationCommand({
145+
command: {
146+
type: "thread.settle",
147+
commandId: CommandId.make("cmd-settle-snoozed-again"),
148+
threadId: ThreadId.make("thread-1"),
149+
},
150+
readModel: makeReadModel("settled", null, null, [], [], {
151+
snoozedUntil: "1970-01-02T09:00:00.000Z",
152+
}),
153+
});
154+
const events = Array.isArray(result) ? result : [result];
155+
expect(events.map((entry) => entry.type)).toEqual(["thread.settled", "thread.unsnoozed"]);
156+
const settled = events.find((entry) => entry.type === "thread.settled");
157+
const unsnoozed = events.find((entry) => entry.type === "thread.unsnoozed");
158+
if (settled?.type === "thread.settled" && unsnoozed?.type === "thread.unsnoozed") {
159+
expect(settled.payload.settledAt).toBe(SETTLED_AT);
160+
expect(settled.payload.updatedAt).toBe(NOW);
161+
expect(unsnoozed.payload.updatedAt).not.toBe(NOW);
162+
}
163+
}),
164+
);
165+
166+
it.effect("settling a pinned and snoozed thread clears the pin and snooze", () =>
167+
Effect.gen(function* () {
168+
const result = yield* decideOrchestrationCommand({
169+
command: {
170+
type: "thread.settle",
171+
commandId: CommandId.make("cmd-settle-pinned-snoozed"),
172+
threadId: ThreadId.make("thread-1"),
173+
},
174+
readModel: makeReadModel(null, null, null, [], [], {
175+
pinnedAt: SETTLED_AT,
176+
snoozedUntil: "1970-01-02T09:00:00.000Z",
177+
}),
178+
});
179+
const events = Array.isArray(result) ? result : [result];
180+
expect(events.map((entry) => entry.type)).toEqual([
181+
"thread.settled",
182+
"thread.unpinned",
183+
"thread.unsnoozed",
184+
]);
185+
}),
186+
);
187+
111188
it.effect("rejects settling a thread with a live session", () =>
112189
Effect.gen(function* () {
113190
for (const status of ["starting", "running"] as const) {

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,11 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
504504
updatedAt: alreadySettled ? thread.updatedAt : occurredAt,
505505
},
506506
};
507-
// Settling is "I'm done with this": it clears a pin the same way it
508-
// parks the thread. Without this, settling a pinned thread would only
509-
// stamp invisible state — the pin would hold the card in place until
510-
// a separate unpin.
511-
if (thread.pinnedAt == null) {
512-
return settledEvent;
513-
}
514-
return [
515-
settledEvent,
516-
{
507+
// Settling is "I'm done with this": clear states that would keep the
508+
// row pinned or snoozed instead of showing the new settled state.
509+
const companionEvents: Array<Omit<OrchestrationEvent, "sequence">> = [];
510+
if (thread.pinnedAt != null) {
511+
companionEvents.push({
517512
...(yield* withEventBase({
518513
aggregateKind: "thread",
519514
aggregateId: command.threadId,
@@ -525,8 +520,25 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
525520
threadId: command.threadId,
526521
updatedAt: occurredAt,
527522
},
528-
},
529-
];
523+
});
524+
}
525+
if (thread.snoozedUntil != null) {
526+
companionEvents.push({
527+
...(yield* withEventBase({
528+
aggregateKind: "thread",
529+
aggregateId: command.threadId,
530+
occurredAt,
531+
commandId: command.commandId,
532+
})),
533+
type: "thread.unsnoozed",
534+
payload: {
535+
threadId: command.threadId,
536+
reason: "user",
537+
updatedAt: occurredAt,
538+
},
539+
});
540+
}
541+
return companionEvents.length > 0 ? [settledEvent, ...companionEvents] : settledEvent;
530542
}
531543

532544
case "thread.unsettle": {

0 commit comments

Comments
 (0)