fix(server): track provider-switch reservation separately from pending runs so waitForAgentEvent follows replacement

This commit is contained in:
Mohamed Boudra
2026-11-26 02:49:21 +01:00
parent 2ba6f0f3d4
commit af7d18f08e
5 changed files with 193 additions and 44 deletions
@@ -240,14 +240,14 @@ export interface SwitchAgentProviderInput {
messageId: string;
/**
* Optional hook invoked while the switch still holds exclusive admission on `agentId`,
* after the target session is registered. Callers use this to save a delivery receipt,
* call `releaseAdmissionLock()`, and immediately admit the first turn so `hasInFlightRun`
* is true before any overlapping switch can enter. Must not invoke lifecycle-mutating
* `AgentManager` methods on `agentId`.
* after the target session is registered. Callers pass `switchReservationToken` into
* `startAgentRun` / `streamAgent` so the first turn atomically swaps the switch reservation
* for its foreground run without waking `waitForAgentEvent` mid-handoff. Must not invoke
* lifecycle-mutating `AgentManager` methods on `agentId`.
*/
admitFirstTurn?: (
switched: SwitchAgentProviderResult,
releaseAdmissionLock: () => void,
switchReservationToken: string,
) => Promise<void>;
}
@@ -1183,6 +1183,10 @@ export class AgentManager {
>();
private readonly collectingRuntimes = new Set<string>();
private readonly switchingAgentCounts = new Map<string, number>();
private readonly providerSwitchReservations = new Map<
string,
{ token: string; promise: Promise<void>; resolve: () => void }
>();
private readonly lifecycleMutationTails = new Map<string, Promise<void>>();
private readonly pendingCancellations = new Map<string, Promise<unknown>>();
private readonly pendingSubmittedPrompts = new Map<
@@ -1497,7 +1501,36 @@ export class AgentManager {
return Boolean(this.agents.get(agentId)?.session?.steerActiveTurn);
}
hasInFlightRun(agentId: string): boolean {
private reserveProviderSwitch(agentId: string): { token: string; release: () => void } {
let resolveReservation!: () => void;
const promise = new Promise<void>((res) => {
resolveReservation = res;
});
const token = randomUUID();
const reservation = { token, promise, resolve: resolveReservation };
this.providerSwitchReservations.set(agentId, reservation);
return {
token,
release: () => this.releaseProviderSwitchReservation(agentId, token),
};
}
private releaseProviderSwitchReservation(agentId: string, token: string): void {
const current = this.providerSwitchReservations.get(agentId);
if (!current || current.token !== token) return;
this.providerSwitchReservations.delete(agentId);
current.resolve();
}
private hasOtherProviderSwitchReservation(agentId: string, ignoreToken?: string): boolean {
const current = this.providerSwitchReservations.get(agentId);
return current !== undefined && current.token !== ignoreToken;
}
hasInFlightRun(agentId: string, options?: { ignoreSwitchReservationToken?: string }): boolean {
if (this.hasOtherProviderSwitchReservation(agentId, options?.ignoreSwitchReservationToken)) {
return true;
}
const agent = this.agents.get(agentId);
if (!agent) {
return false;
@@ -2583,19 +2616,13 @@ export class AgentManager {
private async runWithSwitchAdmissionLock<T>(
agentId: string,
fn: (releaseAdmissionLock: () => void) => Promise<T>,
fn: (switchReservationToken: string) => Promise<T>,
): Promise<T> {
let lock: PendingForegroundRun | null = this.runs.createPendingRun(agentId);
const release = () => {
if (!lock) return;
const token = lock.token;
lock = null;
this.runs.settleForegroundRun(agentId, token);
};
const reservation = this.reserveProviderSwitch(agentId);
try {
return await fn(release);
return await fn(reservation.token);
} finally {
release();
reservation.release();
}
}
@@ -2694,13 +2721,7 @@ export class AgentManager {
let intent: TimelineProviderSwitchIntent | null = null;
let committed = false;
let handedToRegistration = false;
let switchRunLock: PendingForegroundRun | null = null;
const releaseSwitchRunLock = () => {
if (!switchRunLock) return;
const token = switchRunLock.token;
switchRunLock = null;
this.runs.settleForegroundRun(agentId, token);
};
let switchReservation: { token: string; release: () => void } | null = null;
try {
this.paseoToolPolicies.set(agentId, paseoToolPolicy);
@@ -2735,11 +2756,12 @@ export class AgentManager {
);
// Re-verify idle right before committing/closing the source session, then immediately
// reserve the foreground run slot without yielding so no turn or rewind can sneak in.
// reserve exclusive switch admission without yielding so no turn or rewind can sneak in
// and waitForAgentEvent follows this chat seamlessly through session replacement.
this.assertSourceAgentIdleForSwitch(agentId, existingLive);
switchRunLock = this.runs.createPendingRun(agentId);
switchReservation = this.reserveProviderSwitch(agentId);
// Read handoff AFTER reserving the foreground turn slot and settling any open rewind so
// Read handoff AFTER reserving switch admission and settling any open rewind so
// the marker context and first prompt reflect the exact active history.
const handoff = await this.buildActiveSwitchHandoff(
scope,
@@ -2760,10 +2782,6 @@ export class AgentManager {
});
await this.closeLiveSessionForSwitch(existingLive, agentId);
if (existingLive) {
// prepareAgentForClosure clears the run map; re-reserve until first-turn admission.
switchRunLock = this.runs.createPendingRun(agentId);
}
this.assertAcceptingAgentRegistrations();
const markerItem: ProviderSwitchTimelineItem = {
@@ -2815,7 +2833,7 @@ export class AgentManager {
context: handoff.summary,
};
if (input.admitFirstTurn) {
await input.admitFirstTurn(result, releaseSwitchRunLock);
await input.admitFirstTurn(result, switchReservation.token);
}
return result;
} catch (error) {
@@ -2824,7 +2842,7 @@ export class AgentManager {
}
throw error;
} finally {
releaseSwitchRunLock();
switchReservation?.release();
if (!handedToRegistration) {
this.restoreToolPolicyOnSwitchFailure(
agentId,
@@ -3195,6 +3213,7 @@ export class AgentManager {
!agent.internal &&
agent.activeForegroundTurnId === null &&
!this.runs.hasRun(agent.id) &&
!this.providerSwitchReservations.has(agent.id) &&
!agent.pendingReplacement &&
agent.pendingPermissions.size === 0 &&
agent.inFlightPermissionResponses.size === 0 &&
@@ -4562,7 +4581,12 @@ export class AgentManager {
},
"agent.manager.stream.request",
);
if (existingAgent.activeForegroundTurnId || this.runs.hasRun(agentId)) {
const switchReservationToken = promptStart?.switchReservationToken;
if (
existingAgent.activeForegroundTurnId ||
this.runs.hasRun(agentId) ||
this.hasOtherProviderSwitchReservation(agentId, switchReservationToken)
) {
this.logger.trace(
{
agentId,
@@ -4584,6 +4608,9 @@ export class AgentManager {
const pendingRun = this.runs.createPendingRun(agentId);
promptStart?.bind(pendingRun);
if (switchReservationToken) {
this.releaseProviderSwitchReservation(agentId, switchReservationToken);
}
const streamForwarder = async function* streamForwarder(this: AgentManager) {
let turnStream: ReturnType<AgentRunState["createTurnStream"]> | null = null;
@@ -5225,7 +5252,9 @@ export class AgentManager {
this.runs.getRun(agentId) ??
(agent.lifecycle === "running" ? this.runs.trackAutonomousRun(agentId, null) : null);
if (!run) {
return { status: "not_running" };
return this.providerSwitchReservations.has(agentId)
? { status: "refused" }
: { status: "not_running" };
}
if (cause === "stop") {
for (const listener of this.stopListeners) listener(agentId);
@@ -5578,12 +5607,27 @@ export class AgentManager {
return lastAssistantMessageText(timeline.map((item) => ({ item })));
}
/** Wait for an in-flight provider switch reservation and/or starting foreground run. */
private async waitForForegroundStart(agentId: string): Promise<void> {
while (true) {
const reservation = this.providerSwitchReservations.get(agentId);
if (reservation) {
await reservation.promise;
continue;
}
await this.runs.waitForForegroundStart(agentId);
if (!this.providerSwitchReservations.has(agentId)) {
return;
}
}
}
/** A provider start has no deadline of its own, so the caller's abort ends the wait for it. */
private async waitForForegroundStartUnlessAborted(
agentId: string,
signal: AbortSignal | undefined,
): Promise<void> {
if (!signal) return await this.runs.waitForForegroundStart(agentId);
if (!signal) return await this.waitForForegroundStart(agentId);
if (signal.aborted) throw createAbortError(signal, "wait_for_agent aborted");
let onAbort: () => void = () => undefined;
const aborted = new Promise<never>((_resolve, reject) => {
@@ -5591,7 +5635,7 @@ export class AgentManager {
signal.addEventListener("abort", onAbort, { once: true });
});
try {
await Promise.race([this.runs.waitForForegroundStart(agentId), aborted]);
await Promise.race([this.waitForForegroundStart(agentId), aborted]);
} finally {
signal.removeEventListener("abort", onAbort);
}
@@ -5601,11 +5645,12 @@ export class AgentManager {
agentId: string,
options?: WaitForAgentOptions,
): Promise<WaitForAgentResult> {
if (!this.getAgent(agentId)) {
if (!this.getAgent(agentId) && !this.providerSwitchReservations.has(agentId)) {
throw new Error(`Agent ${agentId} not found`);
}
// A starting run publishes its running state only once its turn starts. Until then the
// replayed state below is the idle state before the run, which would end the wait early.
// A provider switch or starting run publishes its running state only once the replacement
// session is registered and its turn starts. Until then the replayed state below is either
// absent or the idle state before the run, which would fail or end the wait early.
await this.waitForForegroundStartUnlessAborted(agentId, options?.signal);
const snapshot = this.getAgent(agentId);
if (!snapshot) {
@@ -45,6 +45,8 @@ export interface StartAgentRunOptions {
runOptions?: AgentRunOptions;
/** Ask the provider to deny permissions blocking this steer. */
clearPendingPermissions?: boolean;
/** Exclusive switch reservation token handed off from `switchAgentProvider` into `streamAgent`. */
switchReservationToken?: string;
}
export type PromptDispatchDisposition = "out_of_band" | "steered" | "turn_started";
@@ -171,7 +173,7 @@ export async function startAgentRun(
options?.onDispatch?.("out_of_band");
return { disposition: "out_of_band" };
}
const promptStart = new ForegroundPromptStart(agentId, admit);
const promptStart = new ForegroundPromptStart(agentId, admit, options?.switchReservationToken);
try {
return await startAgentRunWithPluginRecovery(
agentManager,
@@ -199,7 +201,12 @@ async function startAgentRunInner(
const steered = options?.idleOnly
? null
: await steerOrReplaceActiveRun(agentManager, agentId, prompt, options, promptStart);
if (options?.idleOnly && agentManager.hasInFlightRun(agentId)) {
if (
options?.idleOnly &&
agentManager.hasInFlightRun(agentId, {
ignoreSwitchReservationToken: options.switchReservationToken,
})
) {
throw new AgentTurnBusyError(`Agent ${agentId} is running`);
}
if (steered?.disposition === "steered") {
@@ -359,6 +359,7 @@ export class ForegroundPromptStart {
constructor(
private readonly agentId: string,
readonly admit?: () => boolean,
readonly switchReservationToken?: string,
) {
this.started = new Promise<void>((resolve, reject) => {
this.resolveStarted = resolve;
@@ -50,6 +50,7 @@ class FakeSession implements AgentSession {
prompt: AgentPromptInput,
options?: AgentRunOptions,
) => Promise<void> | void;
private readonly onClose?: () => Promise<void> | void;
private readonly persistAfterFirstTurn: boolean;
private readonly echoBeforeStartResolves: boolean;
@@ -64,6 +65,7 @@ class FakeSession implements AgentSession {
prompt: AgentPromptInput,
options?: AgentRunOptions,
) => Promise<void> | void;
onClose?: () => Promise<void> | void;
}) {
this.id = params.id;
this.provider = params.provider;
@@ -71,6 +73,7 @@ class FakeSession implements AgentSession {
this.persistAfterFirstTurn = params.persistAfterFirstTurn ?? false;
this.echoBeforeStartResolves = params.echoBeforeStartResolves ?? false;
this.onStartTurn = params.onStartTurn;
this.onClose = params.onClose;
}
emit(event: AgentStreamEvent): void {
@@ -166,6 +169,9 @@ class FakeSession implements AgentSession {
return this.persistence;
}
async close(): Promise<void> {
if (this.onClose) {
await this.onClose();
}
this.closed = true;
}
}
@@ -184,6 +190,7 @@ class FakeClient implements AgentClient {
prompt: AgentPromptInput,
options?: AgentRunOptions,
) => Promise<void> | void;
onClose?: () => Promise<void> | void;
constructor(readonly provider: AgentProvider) {}
@@ -206,7 +213,9 @@ class FakeClient implements AgentClient {
: { provider: this.provider, sessionId: id },
persistAfterFirstTurn: this.persistAfterFirstTurn,
echoBeforeStartResolves: this.echoBeforeStartResolves,
onStartTurn: this.onStartTurn,
onStartTurn: (activeSession, prompt, options) =>
this.onStartTurn?.(activeSession, prompt, options),
onClose: () => this.onClose?.(),
});
this.createdSessions.push(session);
return session;
@@ -546,6 +555,93 @@ describe("AgentManager provider switching", () => {
expect(afterRewindDelivered).toContain("Claude context turn");
expect(afterRewindDelivered).toContain("After rewind prompt");
});
it("keeps waitForAgentEvent following the same chat across provider-switch session replacement while blocking overlapping turns", async () => {
const { cwd, manager, claude, codex } = createHarness();
const logger = createTestLogger();
const created = await manager.createAgent({ provider: "claude", cwd }, randomUUID(), {});
await runAndWaitIdle(manager, created.id, "Before switch", "msg-before");
let notifySourceClosing!: () => void;
const sourceClosing = new Promise<void>((resolve) => {
notifySourceClosing = resolve;
});
let releaseSourceClose!: () => void;
const sourceCloseGate = new Promise<void>((resolve) => {
releaseSourceClose = resolve;
});
claude.onClose = async () => {
notifySourceClosing();
await sourceCloseGate;
};
let notifyTargetTurnStarting!: () => void;
const targetTurnStarting = new Promise<void>((resolve) => {
notifyTargetTurnStarting = resolve;
});
let releaseTargetTurn!: () => void;
const targetTurnGate = new Promise<void>((resolve) => {
releaseTargetTurn = resolve;
});
codex.onStartTurn = async () => {
notifyTargetTurnStarting();
await targetTurnGate;
};
const switchPromise = manager.switchAgentProvider(created.id, {
toProvider: "codex",
prompt: "After switch",
messageId: "msg-after",
admitFirstTurn: async (result, switchReservationToken) => {
const dispatch = await startAgentRun(manager, created.id, result.handoffPrompt, logger, {
idleOnly: true,
skipPluginReload: true,
switchReservationToken,
runOptions: { clientMessageId: "msg-after" },
});
expect(dispatch.disposition).toBe("turn_started");
},
});
// Wait until the switch holds its reservation and is closing the source session.
await sourceClosing;
// Overlap protection remains enforced while the switch reservation is held.
expect(manager.hasInFlightRun(created.id)).toBe(true);
expect(() => manager.streamAgent(created.id, "Overlapping prompt")).toThrow(
/already has an active run/,
);
await expect(manager.cancelAgentRun(created.id)).resolves.toEqual({ status: "refused" });
// Start waitForAgentEvent right before prepareAgentForClosure deletes the source session.
let waitSettled = false;
const waitResultPromise = manager.waitForAgentEvent(created.id).then((res) => {
waitSettled = true;
return res;
});
// Aborting another waiter during the switch reservation still rejects promptly.
const abortController = new AbortController();
const abortedWait = manager.waitForAgentEvent(created.id, { signal: abortController.signal });
abortController.abort();
await expect(abortedWait).rejects.toThrow(/aborted/i);
// Let source close finish, register Codex, and atomically hand off into Codex's first turn.
releaseSourceClose();
await targetTurnStarting;
// Waiter MUST NOT have thrown "Agent not found" or resolved early during session replacement.
await Promise.resolve();
expect(waitSettled).toBe(false);
// Complete Codex's turn and confirm the waiter receives Codex's reply on the same chat.
releaseTargetTurn();
await switchPromise;
const waitResult = await waitResultPromise;
expect(waitResult.status).toBe("idle");
expect(waitResult.lastMessage).toBe("codex reply 1");
});
});
// Touch AgentProviderSwitchStartError to verify export
+2 -2
View File
@@ -9304,10 +9304,9 @@ export class Session {
providerOptions: msg.providerOptions,
prompt,
messageId: msg.messageId,
admitFirstTurn: async (result, releaseAdmissionLock) => {
admitFirstTurn: async (result, switchReservationToken) => {
try {
await beforeDispatch();
releaseAdmissionLock();
admitted.dispatch = await startAgentRun(
this.agentManager,
agentId,
@@ -9316,6 +9315,7 @@ export class Session {
{
idleOnly: true,
skipPluginReload: true,
switchReservationToken,
runOptions: { clientMessageId: msg.messageId },
},
);