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
32 changes: 32 additions & 0 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,36 @@ const make = Effect.gen(function* () {
yield* assertTargetWithinLimits(limits, target.thread);

const mode = input.mode ?? "auto";
// A message to another thread rides in that thread's next digest unless the sender
// names a class. A message to the sender's own thread keeps the mode it asked for.
const deliveryClass =
input.deliveryClass ??
(input.digestCategory !== undefined ||
(parent !== undefined && parent.thread.id !== input.threadId)
? "digest"
: undefined);
if (
deliveryClass === undefined &&
(input.coalesceKey !== undefined || input.expiresAt !== undefined)
) {
return yield* failure("invalid_request", "coalesceKey and expiresAt need a deliveryClass.");
}
const expiresAt =
input.expiresAt === undefined
? undefined
: Option.getOrUndefined(DateTime.make(input.expiresAt));
if (input.expiresAt !== undefined && expiresAt === undefined) {
return yield* failure("invalid_request", "expiresAt must be an ISO date-time.");
}
const delivery =
deliveryClass === undefined
? undefined
: {
class: deliveryClass,
...(input.coalesceKey === undefined ? {} : { coalesceKey: input.coalesceKey }),
...(expiresAt === undefined ? {} : { expiresAt: DateTime.toUtc(expiresAt) }),
...(input.digestCategory === undefined ? {} : { category: input.digestCategory }),
};
const key = yield* requestKey(input.clientRequestId);
const messageId = stableOperationMessageId({
scope,
Expand All @@ -3584,6 +3614,7 @@ const make = Effect.gen(function* () {
attachments: [],
mode,
...(input.freshContext ? { freshContext: true } : {}),
...(delivery === undefined ? {} : { delivery }),
createdBy: "agent",
creationSource: "mcp",
})
Expand Down Expand Up @@ -3626,6 +3657,7 @@ const make = Effect.gen(function* () {
...(result.run.delegatedTaskId === undefined ? {} : { taskId: result.run.delegatedTaskId }),
status: result.run.status,
delivery: result.delivery,
...(deliveryClass === undefined ? {} : { deliveryClass }),
} satisfies OrchestratorMcpThreadSendResult;
}),
waitForThread: Effect.fn("OrchestratorMcpService.waitForThread")(function* (scope, input) {
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/mcp/toolkits/orchestrator/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ const ThreadUpdateTool = Tool.make("t3_thread_update", {

const ThreadSendTool = Tool.make("t3_thread_send", {
description:
"Send a message to any thread. The target cannot run with broader modes than yours. `mode:'auto'` starts an idle thread, steers a running turn, or queues. `queue`: new turn. `steer`: mid-turn update. `restart`: interrupt and restart. A send to a finished child thread starts a fresh task and returns its `taskId`. In your reply, name it as its `link` verbatim.",
"Send a message to any thread. The target cannot run with broader modes than yours. `mode:'auto'` starts an idle thread, steers a running turn, or queues. `queue`: new turn. `steer`: mid-turn update. `restart`: interrupt and restart. `deliveryClass` overrides `mode`: `interrupt`, `steer_at_tool_boundary` (after the next tool result), `next_turn`, `when_idle` (once nothing runs), or `digest` (never starts a turn; rides with the next one, grouped by `digestCategory`). `coalesceKey` replaces an undelivered message with the same key; past `expiresAt` an undelivered one is dropped. A send to a finished child thread starts a fresh task and returns its `taskId`. In your reply, name it as its `link` verbatim.",
parameters: OrchestratorMcpThreadSendInput,
success: OrchestratorMcpThreadSendResult,
failure: OrchestratorMcpFailure,
Expand Down
12 changes: 12 additions & 0 deletions apps/server/src/mcp/toolkits/thread/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
OrchestratorMcpFailure,
type OrchestrationV2Command,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import { modelSelectionCommandType } from "@t3tools/shared/model";
Expand Down Expand Up @@ -42,6 +43,17 @@ function queueEntry(
queuedRunId: run.id,
text: characters.slice(0, limit).join(""),
truncated: characters.length > limit,
...(run.delivery === undefined
? {}
: {
deliveryClass: run.delivery.class,
...(run.delivery.coalesceKey === undefined
? {}
: { coalesceKey: run.delivery.coalesceKey }),
...(run.delivery.expiresAt === undefined
? {}
: { expiresAt: DateTime.formatIso(run.delivery.expiresAt) }),
}),
};
}
const dispatch = Effect.fn("mcp.dispatchThreadCommand")(function* (
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/mcp/toolkits/thread/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
IsoDateTime,
OrchestratorMcpFailure,
OrchestrationV2DispatchCommandResult,
OrchestrationV2DeliveryClass,
ThreadId,
RunId,
NonNegativeInt,
Expand Down Expand Up @@ -79,12 +80,15 @@ const queueEntry = Schema.Struct({
queuedRunId: RunId,
text: Schema.String,
truncated: Schema.Boolean,
deliveryClass: Schema.optional(OrchestrationV2DeliveryClass),
coalesceKey: Schema.optional(Schema.String),
expiresAt: Schema.optional(Schema.String),
});
const QUEUE_READ_ACTIONS = ["list", "read"] as const;
const QueueTool = Tool.make("t3_queue", {
...commandTool,
description:
"Manage a thread's queued messages. Omit `threadId` for this thread. list: send order, paged by `cursor` and `limit`. read: up to 16,000 chars of one. edit: replace its `text`; attachments stay. cancel. reorder: before `beforeRunId`, or to the end with null. promote_to_steer: send it now as steering to active `targetRunId`. Writes fail once the run is no longer queued.",
"Manage a thread's queued messages. Omit `threadId` for this thread. list: send order, paged by `cursor` and `limit`. read: up to 16,000 chars of one. edit: replace its `text`; attachments stay. cancel. reorder: before `beforeRunId`, or to the end with null. promote_to_steer: send it now as steering to active `targetRunId`. Entries show their `deliveryClass`, `coalesceKey` and `expiresAt`; a `digest` entry never starts a turn and goes out with the next one. Writes fail once the run is no longer queued.",
parameters: Schema.Struct({
action: Schema.Literals([
...QUEUE_READ_ACTIONS,
Expand Down
17 changes: 16 additions & 1 deletion apps/server/src/orchestration-v2/CommandPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,23 @@ export function followUpOwnerRun(
);
}

/** Steering a provider mid-compaction aborts the compaction, so steers into it wait. */
export function compactionHoldsSteer(
projection: Pick<
OrchestrationV2ThreadProjection,
"turnItems" | "providerThreads" | "providerSessions"
>,
run: OrchestrationV2Run,
): boolean {
return (
projection.turnItems.some(
(item) => item.runId === run.id && item.type === "compaction" && item.status === "running",
) && providerTurnCapabilities(projection, run)?.activeSteeringDuringCompaction !== true
);
}

function providerTurnCapabilities(
projection: OrchestrationV2ThreadProjection,
projection: Pick<OrchestrationV2ThreadProjection, "providerThreads" | "providerSessions">,
run: OrchestrationV2Run,
) {
const providerThread = projection.providerThreads.find(
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/orchestration-v2/IdleCompaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isListedThreadLineage, type OrchestrationV2ThreadProjection } from "@t3tools/contracts";
import { COMPACTION_SUGGESTION_MIN_TOKENS } from "@t3tools/shared/claudeCompaction";
import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
import { isWaitingDigestRun } from "@t3tools/shared/orchestrationV2ThreadError";
import * as DateTime from "effect/DateTime";

// Claude Code's idleCompaction defaults to 200K and won't go below 100K. Below
Expand Down Expand Up @@ -53,7 +54,9 @@ export function idleCompactionWindow(
if (!isListedThreadLineage(thread.lineage)) return null;
// A removed or steered queued message leaves a cancelled run that never started.
const latestRun = runs
.filter((run) => run.status !== "cancelled" || run.startedAt !== null)
.filter(
(run) => (run.status !== "cancelled" || run.startedAt !== null) && !isWaitingDigestRun(run),
)
.reduce<(typeof runs)[number] | null>(
(latest, run) => (latest === null || run.ordinal > latest.ordinal ? run : latest),
null,
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/orchestration-v2/IdleCompactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type RunId,
type ThreadId,
} from "@t3tools/contracts";
import { isWaitingDigestRun } from "@t3tools/shared/orchestrationV2ThreadError";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -89,7 +90,10 @@ export const make = Effect.gen(function* () {
)
return;
const latestRun = records.runs
.filter((run) => run.status !== "cancelled" || run.startedAt !== null)
.filter(
(run) =>
(run.status !== "cancelled" || run.startedAt !== null) && !isWaitingDigestRun(run),
)
.reduce<(typeof records.runs)[number] | undefined>(
(latest, run) => (latest === undefined || run.ordinal > latest.ordinal ? run : latest),
undefined,
Expand Down
81 changes: 76 additions & 5 deletions apps/server/src/orchestration-v2/ModelAlertReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isListedThreadLineage,
MessageId,
type ModelAlertWatcher,
type OrchestrationV2DigestCategory,
type OrchestrationV2DomainEvent,
type OrchestrationV2Run,
type OrchestrationV2ThreadShell,
Expand Down Expand Up @@ -124,6 +125,11 @@ const Delivery = Schema.Struct({
attempts: Schema.Int,
nextAttemptAt: Schema.Number,
createdAt: Schema.Number,
/**
* When the digest expires, fixed when the alert is queued. A retry must carry the same time,
* or the first send and the retry disagree and the retry is not a replay.
*/
digestExpiresAt: Schema.optional(Schema.Number),
});
type Delivery = typeof Delivery.Type;

Expand Down Expand Up @@ -306,6 +312,7 @@ function enqueueAlert(
attempts: 0,
nextAttemptAt: nowMs,
createdAt: nowMs,
digestExpiresAt: nowMs + ALERT_DIGEST_TTL_MS,
}));
return {
outbox: [...pruneOutbox(outbox, nowMs), ...added].slice(-MAX_OUTBOX),
Expand Down Expand Up @@ -577,6 +584,8 @@ const BUSY_WATCHER_STATUSES = new Set<string>([
/** Matches the flush interval, so a held alert is looked at again on the next flush. */
export const BUSY_WATCHER_RECHECK_MS = 30_000;
export const BUSY_WATCHER_MAX_HOLD_MS = 10 * 60 * 1000;
/** How long an undelivered alert stays in the watcher's digest before it is dropped. */
const ALERT_DIGEST_TTL_MS = 24 * 60 * 60 * 1000;

/**
* While the watcher is mid-turn, a sent alert only queues behind that turn and is judged stale or
Expand Down Expand Up @@ -682,6 +691,7 @@ export function recordUsageLimit(
attempts: 0,
nextAttemptAt: notBefore,
createdAt: nowMs,
digestExpiresAt: nowMs + ALERT_DIGEST_TTL_MS,
},
].slice(-MAX_OUTBOX);
}
Expand Down Expand Up @@ -736,6 +746,32 @@ export const watcherAlertMessage = (alert: WatcherAlert) => {
}
};


/** Alerts ride in the watcher's next digest; a newer alert on the same subject replaces an older one. */
function alertDelivery(alert: WatcherAlert): {
readonly coalesceKey: string;
readonly category: OrchestrationV2DigestCategory;
} {
switch (alert.type) {
case "peer-link-renewal":
return { coalesceKey: `${alert.type}:${alert.linkedEnvironmentId}`, category: "alert" };
case "usage-limit-incident":
return { coalesceKey: `${alert.type}:${alert.groupKey}`, category: "alert" };
case "model-degraded":
return { coalesceKey: `${alert.type}:${alert.threadId}`, category: "alert" };
case "thread-event":
return {
coalesceKey: `${alert.type}:${alert.kind}:${alert.threadId}`,
category:
alert.kind === "lane-ended"
? "lane_result"
: alert.kind === "needs-person"
? "decision_needed"
: "alert",
};
}
}

/**
* Puts an alert in the watcher thread, here or on the linked machine that owns it. The retry key is
* one per alert and watcher, so a retry after a lost answer sends once.
Expand All @@ -749,11 +785,16 @@ export const deliverAlertToWatcher = Effect.fnUntraced(function* (
alert: WatcherAlert,
watcher: ModelAlertWatcher,
here: EnvironmentId,
expiresAtMs?: number,
) {
const message = watcherAlertMessage(alert);
// Run ids can be long; a retry key must stay under the 256 characters the peer accepts.
const identity = `${alert.id}\n${watcher.environmentId}\n${watcher.threadId}`;
const key = `${alert.type === "model-degraded" ? "model-alert" : alert.type}:${yield* deps.sha256Hex(identity)}`;
const { coalesceKey, category } = alertDelivery(alert);
const expiresAt = DateTime.makeUnsafe(
expiresAtMs ?? DateTime.toEpochMillis(yield* DateTime.now) + ALERT_DIGEST_TTL_MS,
);
if (watcher.environmentId === here) {
yield* deps.dispatch({
type: "message.dispatch",
Expand All @@ -763,6 +804,7 @@ export const deliverAlertToWatcher = Effect.fnUntraced(function* (
text: message,
attachments: [],
dispatchMode: { type: "queue_after_active" },
delivery: { class: "digest", coalesceKey, expiresAt, category },
createdBy: "agent",
creationSource: "server",
});
Expand All @@ -771,7 +813,16 @@ export const deliverAlertToWatcher = Effect.fnUntraced(function* (
yield* deps.call({
environmentId: watcher.environmentId,
tool: "t3_thread_send",
arguments: { threadId: watcher.threadId, message, mode: "queue", clientRequestId: key },
arguments: {
threadId: watcher.threadId,
message,
mode: "queue",
clientRequestId: key,
deliveryClass: "digest",
coalesceKey,
expiresAt: DateTime.formatIso(expiresAt),
digestCategory: category,
},
limits: { runtimeMode: "full-access", interactionMode: "default" },
success: OrchestratorMcpThreadSendResult,
});
Expand Down Expand Up @@ -851,12 +902,18 @@ export const make = Effect.gen(function* () {
crypto
.digest(Digest.sha256, new TextEncoder().encode(identity))
.pipe(Effect.map(Hex.encode), Effect.orDie);
const deliverToWatcher = (alert: WatcherAlert, watcher: ModelAlertWatcher, here: EnvironmentId) =>
const deliverToWatcher = (
alert: WatcherAlert,
watcher: ModelAlertWatcher,
here: EnvironmentId,
expiresAtMs: number,
) =>
deliverAlertToWatcher(
{ dispatch: engine.dispatch, call: peers.call, sha256Hex },
alert,
watcher,
here,
expiresAtMs,
);

type WatcherView = {
Expand Down Expand Up @@ -993,8 +1050,15 @@ export const make = Effect.gen(function* () {
if (watcher.environmentId === here) {
const shell = yield* projections.getThreadShell(watcher.threadId);
if (shell === null) return null;
// A queued digest never starts a turn, so the shell hides it and reads idle. It is still a
// turn waiting to be judged, and another alert queueing behind it would be judged stale.
const digestWaiting =
shell.status === "idle" &&
(yield* projections.getThreadRecords(watcher.threadId, ["runs"])).runs.some(
(run) => run.status === "queued" && run.delivery?.class === "digest",
);
return {
status: shell.status as string,
status: (digestWaiting ? "queued" : shell.status) as string,
providerInstanceId: shell.providerInstanceId as string | null,
usageLimited: shell.lastErrorClass === "usage_limit",
resetAt: shell.usageLimitResetAt ?? null,
Expand Down Expand Up @@ -1088,7 +1152,12 @@ export const make = Effect.gen(function* () {
holdUntil(view, alert, nowMs) ??
busyWatcherHold(view?.status ?? null, entry.createdAt, nowMs);
if (until !== null) return { _tag: "held", until } satisfies DeliveryResult;
yield* deliverToWatcher(alert, target, here);
yield* deliverToWatcher(
alert,
target,
here,
entry.digestExpiresAt ?? nowMs + ALERT_DIGEST_TTL_MS,
);
return { _tag: "sent" } satisfies DeliveryResult;
});

Expand All @@ -1104,6 +1173,8 @@ export const make = Effect.gen(function* () {
return yield* readWatcher(watcher, yield* environment.getEnvironmentId);
}).pipe(Effect.catchCause(() => Effect.succeed(null))),
);
// One at a time: a delivery queues a turn on the watcher, and the next alert in this
// flush has to see that turn to hold instead of queueing behind it.
yield* Effect.forEach(
due,
(entry) =>
Expand Down Expand Up @@ -1131,7 +1202,7 @@ export const make = Effect.gen(function* () {
: afterDeliveryAttempt(current, entry, true, attemptedAt),
]);
}),
{ concurrency: 4, discard: true },
{ concurrency: 1, discard: true },
);
});

Expand Down
Loading
Loading