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
8 changes: 6 additions & 2 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ const make = Effect.gen(function* () {
const snoozeOutcome = Effect.fn("OrchestratorMcpService.snoozeOutcome")(function* (
threadId: ThreadId,
snoozedUntil: DateTime.Utc | null | undefined,
kept: "held" | "posted" | "steered",
kept: "held" | "posted" | "steered" | "delivered",
) {
const nowMs = yield* Clock.currentTimeMillis;
if (snoozedUntil == null || DateTime.toEpochMillis(snoozedUntil) <= nowMs) return {};
Expand Down Expand Up @@ -3539,7 +3539,11 @@ const make = Effect.gen(function* () {
...(yield* snoozeOutcome(
input.threadId,
target.thread.snoozedUntil,
result.run.snoozeDeferred === true ? "held" : "steered",
result.run.snoozeDeferred === true
? "held"
: result.delivery === "steered"
? "steered"
: "delivered",
)),
threadId: input.threadId,
link: threadLinkFor(scope, input.threadId, target.thread.title),
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/mcp/snoozeNotice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as DateTime from "effect/DateTime";

export type SnoozeOutcome = "held" | "posted" | "steered" | "woke";
export type SnoozeOutcome = "held" | "posted" | "steered" | "delivered" | "woke";

/** What a sender learns about a target's snooze; nothing when the thread was awake. */
export function snoozeNotice(
Expand All @@ -15,6 +15,7 @@ export function snoozeNotice(
held: `${prefix} The message waits, held, until it wakes. Pass respectSnooze: false to wake it now.`,
posted: `${prefix} The message was recorded and the thread stays snoozed.`,
steered: `${prefix} The message joined its running turn, and the thread stays snoozed.`,
delivered: `${prefix} It counts as awake for now (it never ran, or a request, guest message or error woke it), so the message went ahead as usual: started or queued. The snooze stays.`,
woke: `${prefix} This message ended the snooze.`,
}[outcome];
return { snoozedUntil: until, warning };
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/mcp/toolkits/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,7 @@ it.effect("schedules a model change on a busy thread and shows it until it appli
for (const [refused, code] of [
[{ runtimeMode: "approval-required", applyAt: "next_turn" }, "invalid_request"],
[{ applyAt: "next_turn" }, "invalid_request"],
[{ acknowledgeOnly: true, applyAt: "next_turn" }, "invalid_request"],
[
{ modelSelection: { instanceId: "claudeAgent", model: "opus" }, applyAt: "next_turn" },
"invalid_request",
Expand Down
9 changes: 7 additions & 2 deletions apps/server/src/mcp/toolkits/thread/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,18 @@ const configureAcknowledgeOnly = Effect.fn("mcp.configureAcknowledgeOnly")(funct
readonly acknowledgeOnly?: boolean | undefined;
readonly modelSelection?: unknown;
readonly runtimeMode?: unknown;
readonly applyAt?: unknown;
},
) {
yield* McpToolAccess.requireFullAccess("change who answers a thread");
if (input.modelSelection !== undefined || input.runtimeMode !== undefined) {
if (
input.modelSelection !== undefined ||
input.runtimeMode !== undefined ||
input.applyAt !== undefined
) {
return yield* new OrchestratorMcpFailure({
code: "invalid_request",
message: "Set acknowledgeOnly separately from modelSelection and runtimeMode.",
message: "Set acknowledgeOnly separately from modelSelection, runtimeMode and applyAt.",
});
}
const acknowledgeOnly = yield* required(input.acknowledgeOnly, "acknowledgeOnly");
Expand Down
75 changes: 58 additions & 17 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,20 +995,35 @@ function queuedBeforeScheduledSelection(
);
}

/** Drops a scheduled change once the run starting now leaves no run queued before it. */
/**
* A scheduled change lasts until a turn on the default model takes it with nothing queued before
* the change still waiting, explicit picks included. A model named after the change replaces it.
* A server continuation repeats an earlier run's model and leaves the change alone.
*/
function threadAtRunStart(
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "runs">,
startingRunId: OrchestrationV2Run["id"] | null,
starting: {
readonly runId: OrchestrationV2Run["id"] | null;
/** Null for a turn starting now, which is newer than any scheduled change. */
readonly ordinal: number | null;
readonly model: "default" | "named" | "server";
},
): OrchestrationV2ThreadProjection["thread"] {
const scheduled = projection.thread.scheduledModelSelection;
if (scheduled == null) return projection.thread;
if (scheduled == null || starting.model === "server") return projection.thread;
const cleared = { ...projection.thread, scheduledModelSelection: null };
if (starting.model === "named") {
return starting.ordinal !== null && starting.ordinal <= scheduled.queuedThroughOrdinal
? projection.thread
: cleared;
}
const waiting = projection.runs.some(
(run) =>
run.id !== startingRunId &&
run.id !== starting.runId &&
run.status === "queued" &&
queuedBeforeScheduledSelection(projection, run),
run.ordinal <= scheduled.queuedThroughOrdinal,
);
return waiting ? projection.thread : { ...projection.thread, scheduledModelSelection: null };
return waiting ? projection.thread : cleared;
}

function queuedRunFollowsThread(
Expand Down Expand Up @@ -1036,10 +1051,16 @@ function queuedRunModelSelection(
function queueTailModelSelection(
projection: Pick<OrchestrationV2ThreadProjection, "thread" | "runs" | "messages">,
): ModelSelection {
const scheduled = projection.thread.scheduledModelSelection;
// With a change pending, a queued server continuation's repeated model is no one's new pick.
const serverRepeat = (run: OrchestrationV2Run) =>
scheduled != null &&
run.explicitModelSelection === true &&
projection.messages.find((message) => message.id === run.userMessageId)?.creationSource ===
"server";
const tail = queuedRunsInDeliveryOrder(projection).findLast(
(run) => !queuedRunFollowsThread(projection, run),
(run) => !queuedRunFollowsThread(projection, run) && !serverRepeat(run),
);
const scheduled = projection.thread.scheduledModelSelection;
// A pick queued before a scheduled change is older than it; later sends take the change.
return tail !== undefined && (scheduled == null || tail.ordinal > scheduled.queuedThroughOrdinal)
? tail.modelSelection
Expand Down Expand Up @@ -2552,7 +2573,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
projection.thread.modelSelection,
queuedRun.modelSelection,
);
const threadAtStart = threadAtRunStart(projection, queuedRun.id);
const threadAtStart = threadAtRunStart(projection, {
runId: queuedRun.id,
ordinal: queuedRun.ordinal,
model:
queuedRun.explicitModelSelection !== true
? "default"
: queuedMessage?.creationSource === "server"
? "server"
: "named",
});
// The picker may already have moved ahead of the native conversation.
const switchFromActiveProvider =
followsCurrent &&
Expand Down Expand Up @@ -7045,20 +7075,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
command.notification !== undefined ||
command.delegatedCompletion !== undefined ||
serverNotice;
// A sender that respects the snooze leaves it: a post is only recorded, and a turn waits,
// held behind the thread's last run, until the thread wakes. A thread with no run wakes,
// and so does one the server snoozed until its usage limit resets: that is no one's hold.
// The snooze counts as the sidebar shows it: an error or a request has already woken it.
// A sender that respects the snooze keeps its dated hold (a usage-limit snooze is no one's).
// While the thread is snoozed as the sidebar shows it, a turn waits behind its last run; a
// thread that counts as awake, or never ran, gets the message now.
const snoozeHeldUntil = projection.thread.snoozedUntil;
const limitRecovery = projection.thread.limitRecovery;
const respectsSnooze =
const keepsSnooze =
command.respectSnooze === true &&
!automaticWake &&
snoozeHeldUntil != null &&
DateTime.toEpochMillis(snoozeHeldUntil) > DateTime.toEpochMillis(yield* DateTime.now) &&
!(
limitRecovery?.snooze === true &&
DateTime.toEpochMillis(snoozeHeldUntil) === Date.parse(limitRecovery.resetAt)
) &&
);
const respectsSnooze =
keepsSnooze &&
(command.postOnly === true || latestExecutedRun(projection.runs) != null) &&
isSnoozed(
(yield* projectionStore
Expand All @@ -7071,7 +7103,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
},
DateTime.toEpochMillis(yield* DateTime.now),
);
if (projection.thread.snoozedUntil != null && !automaticWake && !respectsSnooze) {
if (projection.thread.snoozedUntil != null && !automaticWake && !keepsSnooze) {
const now = yield* DateTime.now;
const thread: OrchestrationV2AppThread = {
...projection.thread,
Expand Down Expand Up @@ -8063,7 +8095,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const selectionChanged =
!modelSelectionsEqual(projection.thread.modelSelection, modelSelection) ||
projection.thread.providerInstanceId !== modelSelection.instanceId;
const threadAtStart = threadAtRunStart(projection, null);
const threadAtStart = threadAtRunStart(projection, {
runId: null,
ordinal: null,
model:
command.modelSelection === undefined
? "default"
: command.creationSource === "server"
? "server"
: "named",
});
if (selectionChanged || threadAtStart !== projection.thread) {
yield* emit(
events,
Expand Down
Loading
Loading