fix(server): snoozed threads allow queued automatic wakes (#19099)

This commit is contained in:
Yash Singh authored and GitHub committed 2026-10-13 17:12:47 +00:00
1 parent 8d75232489
commit 05e4f888a0
7 files changed
+395 -10

No files matched your search

@@ -1,9 +1,22 @@
import {
latestProviderTurnForAttempt,
type MessageId,
type OrchestrationV2ConversationMessage,
type OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
/** A wake attaches to output held in provider memory, which Stop or process loss discards. */
export function isBufferedProviderNotification(
message: OrchestrationV2ConversationMessage,
): boolean {
return (
message.createdBy === "agent" &&
message.creationSource === "provider" &&
message.notification !== undefined &&
message.delegatedCompletion === undefined
);
}
/**
* A persisted steer without an acceptance receipt can be retried as a continuation.
* Delivery is at least once: provider acceptance and our receipt cannot commit
@@ -89,7 +89,10 @@ import { CommandReceiptStoreV2 } from "./CommandReceiptStore.ts";
import { ContextHandoffServiceV2 } from "./ContextHandoffService.ts";
import { notificationTurnItem } from "@t3tools/provider-core/server/notification";
import { isRestartNoteSource } from "./RestartBackgroundNote.ts";
import { isUndeliveredMailboxSteer } from "./NotificationMailbox.ts";
import {
isBufferedProviderNotification,
isUndeliveredMailboxSteer,
} from "./NotificationMailbox.ts";
import { EventSinkV2 } from "./EventSink.ts";
import * as EffectOutbox from "./EffectOutbox.ts";
import type { OrchestrationEffectRequestV2, PendingOrchestrationEffectV2 } from "./EffectOutbox.ts";
@@ -2798,7 +2801,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
cause: `Thread ${command.threadId} has a pending approval or user-input request and cannot be snoozed.`,
});
}
if (projection.runs.some((run) => run.status === "queued")) {
const queuedRuns = projection.runs.filter((run) => run.status === "queued");
const queuedMessages =
queuedRuns.length === 0
? []
: (yield* loadProjectionForCommand(command, ["messages"], {
messageIds: queuedRuns.map((run) => run.userMessageId),
})).messages;
// Automatic wakes are hidden from the user queue and must not block snooze.
// Snooze only changes visibility, so keep those deliveries intact.
const automaticMessageIds = new Set(
queuedMessages
.filter(
(message) =>
message.notification !== undefined || message.delegatedCompletion !== undefined,
)
.map((message) => message.id),
);
if (queuedRuns.some((run) => !automaticMessageIds.has(run.userMessageId))) {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
@@ -8797,9 +8817,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
});
/**
* What Stop holds besides the run it interrupts: queued runs wait for the user, the
* thread's pull request watches end, and every wake its delegated tasks still owe is
* dropped, since Stop stops those tasks too. Nothing automatic starts the thread again.
* What Stop holds besides the run it interrupts: durable queued messages wait for the
* user, buffered provider wakes are cancelled, and the thread's pull request watches end.
* Every wake its delegated tasks still owe is dropped, since Stop stops those tasks too.
* `cohortRunIds` are dropped even when they owe nothing, like a plain interrupt's.
*/
const holdStoppedThread = (input: {
@@ -8815,6 +8835,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
Effect.gen(function* () {
const emitEvent = emit(input.events, input.command);
const { thread, runs, subagents } = input.projection;
const queuedRuns = runs.filter((run) => run.status === "queued");
const queuedMessages =
queuedRuns.length === 0
? []
: (yield* projectionStore
.getThreadRecords(thread.id, ["messages"], {
messageIds: queuedRuns.map((run) => run.userMessageId),
})
.pipe(
Effect.mapError(
(cause) => new OrchestratorProjectionError({ threadId: thread.id, cause }),
),
)).messages;
const bufferedMessageIds = new Set(
queuedMessages.filter(isBufferedProviderNotification).map((message) => message.id),
);
for (const run of runs) {
// A resumed queue can have a lower ordinal than a pending continuation source.
// Stop must reach those sources even when interrupting the resumed run succeeds.
@@ -8824,7 +8860,21 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
) {
yield* markStoppedRun({ ...input, thread, run });
}
if (run.status !== "queued" || run.queueHeld === true) continue;
if (run.status !== "queued") continue;
// Stop discards the provider's wake buffer, so its queued placeholders cannot resume.
if (bufferedMessageIds.has(run.userMessageId)) {
yield* dispatchQueuedRunCancel(
{
type: "queued-run.cancel",
commandId: input.command.commandId,
threadId: thread.id,
runId: run.id,
},
input.events,
);
continue;
}
if (run.queueHeld === true) continue;
yield* emitEvent({
type: "run.updated",
threadId: thread.id,
@@ -23,14 +23,34 @@ import * as Layer from "effect/Layer";
import * as SqlClient from "effect/sql/SqlClient";
import * as SqlitePersistence from "../persistence/Sqlite.ts";
import * as ServerSettings from "../serverSettings.ts";
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
import * as EffectOutbox from "./EffectOutbox.ts";
import * as ProjectionStore from "./ProjectionStore.ts";
import * as EventSink from "./EventSink.ts";
import * as EventStore from "./EventStore.ts";
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
import * as ProviderRuntimeRecovery from "./ProviderRuntimeRecoveryService.ts";
import { restartContinuationRun } from "./RestartContinuation.ts";
const layerTest = Layer.mergeAll(ProjectionStore.layer, EffectOutbox.layer).pipe(
Layer.provideMerge(SqlitePersistence.layerMemory),
);
const layerRecoveryTest = ProviderRuntimeRecovery.layer.pipe(
Layer.provideMerge(
EventSink.layer.pipe(
Layer.provideMerge(
Layer.mergeAll(
EventStore.layer,
ProjectionStore.layer,
EffectOutbox.layer,
IdAllocator.layer,
).pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
),
),
),
Layer.provide(ServerSettings.layerTest()),
);
const providerInstanceId = ProviderInstanceId.make("codex");
const modelSelection = { instanceId: providerInstanceId, model: "gpt-5.4" };
const driver = ProviderDriverKind.make("codex");
@@ -113,6 +133,82 @@ const createRun = Effect.fn(function* (
return runId;
});
it.effect.each(["startup", "shutdown"] as const)(
"%s cancels queued buffered wakes and holds durable messages",
(trigger) =>
Effect.gen(function* () {
const projections = yield* ProjectionStore.ProjectionStoreV2;
const recovery = yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService;
const now = yield* DateTime.now;
const bufferedThreads = [] as ThreadId[];
const durableThreads = [] as ThreadId[];
for (const kind of ["buffered", "user", "notification", "delegated"] as const) {
const threadId = yield* createThread(`wake:${trigger}:${kind}`);
const buffered = kind === "buffered";
(buffered ? bufferedThreads : durableThreads).push(threadId);
const messageId = MessageId.make(`message:${threadId}`);
const runId = yield* createRun(threadId, "queued", {
userMessageId: messageId,
startedAt: null,
queuePosition: 1,
});
yield* createRun(threadId, "completed", { ordinal: 2 });
yield* projections.apply({
id: EventId.make(`message:${threadId}`),
type: "message.updated",
threadId,
runId,
occurredAt: now,
payload: {
id: messageId,
threadId,
runId,
nodeId: null,
role: "user",
text: "Queued work",
attachments: [],
streaming: false,
createdAt: now,
updatedAt: now,
createdBy: kind === "user" ? "user" : "agent",
creationSource: buffered ? "provider" : "server",
...(kind === "user" || kind === "delegated"
? {}
: {
notification: {
source: { kind: "background_task" as const },
outcome: "updated" as const,
summary: "Background activity updated",
},
}),
...(kind === "delegated"
? { delegatedCompletion: { parentRunId: runId, generation: 1, taskIds: [] } }
: {}),
},
});
}
assert.deepEqual(
new Set(yield* projections.getRecoveryThreadIds("runtime")),
new Set([...bufferedThreads, ...durableThreads]),
);
const summary = yield* recovery.reconcile(trigger);
assert.equal(summary.terminalizedRuns, 1);
for (const threadId of bufferedThreads) {
const projection = yield* projections.getThreadProjection(threadId);
assert.equal(projection.runs[0]?.status, "cancelled");
assert.isNotNull(projection.runs[0]?.completedAt);
assert.isNull(projection.runs[0]?.queuePosition);
assert.equal(projection.runs[1]?.status, "completed");
}
for (const threadId of durableThreads) {
const projection = yield* projections.getThreadProjection(threadId);
assert.equal(projection.runs[0]?.status, "queued");
assert.isTrue(projection.runs[0]?.queueHeld);
}
assert.deepEqual(yield* projections.getRecoveryThreadIds("runtime"), []);
}).pipe(Effect.provide(layerRecoveryTest)),
);
it.effect("selects unfinished recovery work without reading settled thread histories", () =>
Effect.gen(function* () {
const projections = yield* ProjectionStore.ProjectionStoreV2;
@@ -690,6 +690,7 @@ it.effect("holds accepted queued work without cancelling its execution state aft
Layer.mock(ProjectionStore.ProjectionStoreV2)({
getRecoveryThreadIds: () => Effect.succeed([threadId]),
getRuntimeRecoveryProjection: () => Effect.succeed(projection),
getThreadRecords: () => Effect.succeed(projection),
}),
Layer.mock(EventSink.EventSinkV2)({
commitCommand: (input) => {
@@ -22,6 +22,7 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
import * as ProjectionStore from "./ProjectionStore.ts";
import * as ServerSettings from "../serverSettings.ts";
import { restartContinuationRun } from "./RestartContinuation.ts";
import { isBufferedProviderNotification } from "./NotificationMailbox.ts";
import {
endOrphanedNativeSubagent,
endRunlessRootTurns,
@@ -284,10 +285,35 @@ export const make = Effect.gen(function* () {
cancelledBackgroundNativeIds.add(item.nativeItemRef.nativeId);
}
};
// Queued runs have not started provider work. Preserve their execution
// identities and order, but require explicit consent before draining them.
for (const run of projection.runs) {
if (run.status !== "queued" || run.queueHeld === true) continue;
const queuedRuns = projection.runs.filter((run) => run.status === "queued");
const queuedMessages =
queuedRuns.length === 0
? []
: (yield* projections
.getThreadRecords(projection.thread.id, ["messages"], {
messageIds: queuedRuns.map((run) => run.userMessageId),
})
.pipe(
Effect.mapError(
(cause) =>
new ProviderRuntimeRecoveryError({
operation: "read-projections",
threadId: projection.thread.id,
cause,
}),
),
)).messages;
const bufferedMessageIds = new Set(
queuedMessages.filter(isBufferedProviderNotification).map((message) => message.id),
);
// Buffered wakes lost their output with the process. Durable queued messages
// keep their execution identities and order until the user resumes them.
for (const run of queuedRuns) {
if (bufferedMessageIds.has(run.userMessageId)) {
runs.push(run);
continue;
}
if (run.queueHeld === true) continue;
events.push({
id: yield* allocateEventId(),
type: "run.updated",
@@ -225,6 +225,62 @@ it.effect("Stop ends watches, holds queues, and stops the delegated tasks under
}).pipe(Effect.provide(layerTest)),
);
it.effect.each(["thread.stop", "run.interrupt"] as const)(
"%s cancels buffered wakes and preserves durable queued messages",
(type) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const threadId = ThreadId.make(`thread:stop-buffered:${type}`);
yield* createWatchingThread(threadId, 5);
yield* send(threadId, "active", "start_immediately");
yield* send(threadId, "user follow-up", "queue_after_active");
for (const creationSource of ["provider", "server"] as const) {
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`notification:${type}:${creationSource}`),
threadId,
messageId: MessageId.make(`notification:${type}:${creationSource}`),
text: "Background activity updated",
attachments: [],
createdBy: "agent",
creationSource,
notification: {
source: { kind: "background_task" },
outcome: "updated",
summary: "Background activity updated",
},
dispatchMode: { type: "queue_after_active" },
});
}
const before = yield* orchestrator.getThreadProjection(threadId);
const buffered = before.runs.find((run) =>
before.messages.some(
(message) => message.id === run.userMessageId && message.creationSource === "provider",
),
)!;
const stop = { commandId: CommandId.make(`stop:${type}`), threadId };
yield* orchestrator.dispatch(
type === "run.interrupt"
? { ...stop, type, runId: before.runs[0]!.id, holdQueue: true }
: { ...stop, type },
);
const stopped = yield* orchestrator.getThreadProjection(threadId);
assert.equal(stopped.runs.find((run) => run.id === buffered.id)?.status, "cancelled");
assert.equal(
stopped.attempts.find((attempt) => attempt.runId === buffered.id)?.status,
"cancelled",
);
assert.equal(
stopped.nodes.find((node) => node.id === buffered.rootNodeId)?.status,
"cancelled",
);
assert.deepEqual(
stopped.runs.filter((run) => run.status === "queued").map((run) => run.queueHeld),
[true, true],
);
}).pipe(Effect.provide(layerTest)),
);
it.effect(
"thread.stop ends an idle thread's watches and accepts a thread with nothing to stop",
() =>
@@ -4516,6 +4516,149 @@ it.layer(layerSharedApplicationDataPlaneTest)("pending provider interruption", (
);
});
it.layer(layerTest)("snooze queued runs", (it) => {
it.effect.each(["notification", "delegated-completion", "user"] as const)(
"only user messages block snooze with a held %s run",
(kind) =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const eventSink = yield* EventSink.EventSinkV2;
const threadId = ThreadId.make(`snooze-queued:${kind}`);
const snoozedUntil = "2099-07-25T09:00:00.000Z";
yield* orchestrator.dispatch({
type: "thread.create",
commandId: CommandId.make(`create:${threadId}`),
threadId,
projectId: ProjectId.make("snooze-queued-project"),
title: "Snooze with held queued work",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: "/tmp/snooze-queued",
createdBy: "user",
creationSource: "web",
});
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`active:${threadId}`),
threadId,
messageId: MessageId.make(`active:${threadId}`),
text: "Active run",
attachments: [],
modelSelection,
dispatchMode: { type: "start_immediately" },
createdBy: "user",
creationSource: "web",
});
const activeRun = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
const now = yield* DateTime.now;
if (kind === "delegated-completion") {
yield* eventSink.write({
events: [
{
id: EventId.make(`completion:${threadId}`),
type: "run.updated",
threadId,
runId: activeRun.id,
occurredAt: now,
payload: {
...activeRun,
delegatedCompletion: {
disposition: "open",
nextGeneration: 2,
delivery: {
messageId: MessageId.make(`queued:${threadId}`),
generation: 1,
taskIds: [NodeId.make("snooze-queued-task")],
},
},
},
},
],
});
}
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`queued:${threadId}`),
threadId,
messageId: MessageId.make(`queued:${threadId}`),
text: "Queued work",
attachments: [],
modelSelection,
dispatchMode: { type: "queue_after_active" },
createdBy: kind === "user" ? "user" : "agent",
creationSource: kind === "user" ? "web" : "server",
...(kind === "notification"
? {
notification: {
source: { kind: "background_task" as const },
outcome: "updated" as const,
summary: "Background activity updated",
},
}
: kind === "delegated-completion"
? {
delegatedCompletion: {
parentRunId: activeRun.id,
generation: 1,
taskIds: [NodeId.make("snooze-queued-task")],
},
}
: {}),
});
// Recovery holds the queue while the thread's active run ends.
const beforeRecovery = yield* orchestrator.getThreadProjection(threadId);
yield* eventSink.commitCommand({
commandId: CommandId.make(`recovery:${threadId}`),
threadId,
commandType: "provider-runtime.reconcile",
acceptedAt: now,
events: beforeRecovery.runs.map((run) => ({
id: EventId.make(`recovery:${run.id}`),
type: "run.updated" as const,
threadId,
runId: run.id,
occurredAt: now,
payload:
run.status === "queued"
? { ...run, queueHeld: true }
: { ...run, status: "completed" as const, completedAt: now },
})),
effects: [],
});
const beforeSnooze = yield* orchestrator.getThreadProjection(threadId);
assert.isTrue(beforeSnooze.runs.some((run) => run.status === "queued" && run.queueHeld));
const snooze = orchestrator.dispatch({
type: "thread.snooze",
commandId: CommandId.make(`snooze:${threadId}`),
threadId,
snoozedUntil,
});
if (kind === "user") {
const error = yield* snooze.pipe(Effect.flip);
assert.equal(error._tag, "OrchestratorDispatchError");
assert.equal(error.cause, `Thread ${threadId} has a queued run and cannot be snoozed.`);
assert.isNull((yield* orchestrator.getThreadProjection(threadId)).thread.snoozedUntil);
} else {
yield* snooze;
const snoozed = yield* orchestrator.getThreadProjection(threadId);
assert.equal(DateTime.formatIso(snoozed.thread.snoozedUntil!), snoozedUntil);
assert.deepEqual(snoozed.runs, beforeSnooze.runs);
yield* orchestrator.dispatch({
type: "thread.unsnooze",
commandId: CommandId.make(`unsnooze:${threadId}`),
threadId,
reason: "user",
});
const awake = yield* orchestrator.getThreadProjection(threadId);
assert.isNull(awake.thread.snoozedUntil);
assert.deepEqual(awake.runs, beforeSnooze.runs);
}
}),
);
});
it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
it.effect("carries snooze state through the V2 shell projection", () =>
Effect.gen(function* () {