Skip to content
Merged
104 changes: 104 additions & 0 deletions packages/acp-bridge/src/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10301,3 +10301,107 @@ describe('close on last client detach', () => {
await bridge.shutdown();
});
});

/**
* `enqueueMidTurnMessage` backs the web-shell mid-turn drain: the browser
* pushes a message typed during a turn, the ACP child drains it via
* `craft/drainMidTurnQueue` (answered by BridgeClient.extMethod). The accept
* gate is the exactly-once linchpin — it must reject when the session is idle
* so the browser's own next-turn queue stays the single delivery path in the
* settle-window race.
*/
describe('createAcpSessionBridge — mid-turn message queue (enqueueMidTurnMessage)', () => {
function hangingPromptFactory(): {
factory: ChannelFactory;
release: () => void;
} {
let release: (() => void) | undefined;
const factory: ChannelFactory = async () =>
makeChannel({
promptImpl: async () => {
await new Promise<void>((res) => {
release = res;
});
return { stopReason: 'end_turn' };
},
}).channel;
return { factory, release: () => release?.() };
}

it('rejects (accepted:false) when the session is idle', async () => {
const bridge = makeBridge({
channelFactory: async () => makeChannel().channel,
});
const session = await bridge.spawnOrAttach({ workspaceCwd: WS_A });
expect(bridge.enqueueMidTurnMessage(session.sessionId, 'later')).toEqual({
accepted: false,
});
await bridge.shutdown();
});

it('accepts while a turn is in flight, then rejects again once it settles', async () => {
const { factory, release } = hangingPromptFactory();
const bridge = makeBridge({ channelFactory: factory });
const session = await bridge.spawnOrAttach({ workspaceCwd: WS_A });
const promptPromise = bridge
.sendPrompt(
session.sessionId,
{
sessionId: session.sessionId,
prompt: [{ type: 'text', text: 'run tools' }],
},
undefined,
{ clientId: session.clientId },
)
.catch(() => {});

// Let the FIFO worker pick up the (hanging) prompt before asserting.
await new Promise((r) => setTimeout(r, 10));
expect(
bridge.enqueueMidTurnMessage(session.sessionId, 'also check tests'),
).toEqual({ accepted: true });

// Settle the turn → the queue flips back to idle and the undrained copy is
// dropped server-side (the browser resends it as the next turn).
release();
await promptPromise;
expect(
bridge.enqueueMidTurnMessage(session.sessionId, 'next time'),
).toEqual({ accepted: false });
await bridge.shutdown();
});

it('rejects a whitespace-only message even while busy', async () => {
const { factory, release } = hangingPromptFactory();
const bridge = makeBridge({ channelFactory: factory });
const session = await bridge.spawnOrAttach({ workspaceCwd: WS_A });
const promptPromise = bridge
.sendPrompt(
session.sessionId,
{
sessionId: session.sessionId,
prompt: [{ type: 'text', text: 'x' }],
},
undefined,
{ clientId: session.clientId },
)
.catch(() => {});
await new Promise((r) => setTimeout(r, 10));
expect(bridge.enqueueMidTurnMessage(session.sessionId, ' ')).toEqual({
accepted: false,
});
release();
await promptPromise;
await bridge.shutdown();
});

it('throws SessionNotFoundError for an unknown session', async () => {
const bridge = makeBridge({
channelFactory: async () => makeChannel().channel,
});
expect(() => bridge.enqueueMidTurnMessage('nope', 'hi')).toThrow(
SessionNotFoundError,
);
await bridge.shutdown();
});
});
54 changes: 53 additions & 1 deletion packages/acp-bridge/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ interface SessionEntry {
promptQueue: Promise<void>;
/** Accepted prompts that have not settled yet (queued + active). */
pendingPromptCount: number;
/**
* Mid-turn user messages pushed by the browser (`POST
* /session/:id/mid-turn-message`) while a turn is running. The ACP child
* drains these between tool batches via the `craft/drainMidTurnQueue`
* ext-method so the model sees them before the turn ends. The queue is
* accepted into only while the session is busy (`pendingPromptCount > 0`)
* and emptied when the session next goes idle — see the settle handler in
* `sendPrompt`. The browser keeps its own copy as the next-turn fallback,
* so a message left undrained here is NOT lost: it is dropped server-side
* (preventing a stale next-turn re-injection) and resent by the browser as
* a fresh prompt.
*/
midTurnMessageQueue: string[];
/**
* Per-session model-change FIFO. Prevents two concurrent
* `applyModelServiceId` calls (e.g. simultaneous attach-with-different-
Expand Down Expand Up @@ -2002,6 +2015,7 @@ export function createAcpSessionBridge(opts: BridgeOptions): AcpSessionBridge {
events,
promptQueue: Promise.resolve(),
pendingPromptCount: 0,
midTurnMessageQueue: [],
modelChangeQueue: Promise.resolve(),
approvalModeQueue: Promise.resolve(),
modelPublishGeneration: 0,
Expand Down Expand Up @@ -2941,7 +2955,26 @@ export function createAcpSessionBridge(opts: BridgeOptions): AcpSessionBridge {
() => undefined,
() => undefined,
);
result.finally(releasePromptSlot).catch(() => {});
result
.finally(() => {
releasePromptSlot();
// Mid-turn messages are scoped to the turn the user typed them
// during. Once the session goes fully idle with some still
// undrained, drop the server-side copy: the browser still holds
// them in its own queue and resends them as the next turn. Leaving
// them here would let the NEXT turn's first tool batch inject a
// stale message the browser ALSO resends — double delivery. The
// `pendingPromptCount === 0` guard keeps queued messages intact
// across a back-to-back FIFO of prompts (still "one turn" to the
// user) and only clears at the true idle boundary.
if (
entry.pendingPromptCount === 0 &&
entry.midTurnMessageQueue.length > 0
) {
entry.midTurnMessageQueue.length = 0;
Comment thread
wenshao marked this conversation as resolved.
Resolved
}
})
.catch(() => {});
return result;
},

Expand Down Expand Up @@ -4032,6 +4065,25 @@ export function createAcpSessionBridge(opts: BridgeOptions): AcpSessionBridge {
};
},

enqueueMidTurnMessage(sessionId, message) {
Comment thread
wenshao marked this conversation as resolved.
Resolved
const entry = byId.get(sessionId);
if (!entry) throw new SessionNotFoundError(sessionId);
const trimmed = message.trim();
// Reject empty messages and — critically — messages that arrive while
// the session is idle. The browser only pushes here when it believes a
// turn is running, but the turn may have settled in the small window
// before its turn-complete frame landed. Accepting an idle message
// would strand it until the NEXT turn's first tool batch drained it,
// by which point the browser has already resent it as a fresh prompt —
// double delivery. Rejecting keeps the browser's next-turn fallback the
// single delivery path in that race.
if (trimmed.length === 0 || entry.pendingPromptCount === 0) {
Comment thread
wenshao marked this conversation as resolved.
Resolved
return { accepted: false };
}
entry.midTurnMessageQueue.push(trimmed);
return { accepted: true };
},

async generateSessionBtw(sessionId, question, signal, _context) {
const entry = byId.get(sessionId);
if (!entry) throw new SessionNotFoundError(sessionId);
Expand Down
112 changes: 112 additions & 0 deletions packages/acp-bridge/src/bridgeClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type {
WriteTextFileRequest,
WriteTextFileResponse,
} from '@agentclientprotocol/sdk';
import { RequestError } from '@agentclientprotocol/sdk';
import { BridgeClient } from './bridgeClient.js';
import type { BridgeFileSystem } from './bridgeFileSystem.js';
import { CancelSentinelCollisionError } from './bridgeErrors.js';
Expand Down Expand Up @@ -596,3 +597,114 @@ describe('BridgeClient — requestPermission pre-publish collision guard', () =>
expect(fakeEntry.pendingPermissionIds.size).toBe(0);
});
});

/**
* `extMethod` is the daemon's answer to the ACP child's
* `craft/drainMidTurnQueue` call (web-shell mid-turn drain). Desktop answers
* the same method from its own in-memory queue; in `qwen serve` the BridgeClient
* answers it from `SessionEntry.midTurnMessageQueue`. Without this the SDK's
* ClientSideConnection would reject the call with -32601 and the child would
* latch the drain as unavailable for the whole session.
*/
describe('BridgeClient — mid-turn queue drain (craft/drainMidTurnQueue)', () => {
const thrower = () => {
throw new Error('test: permission flow should not run');
};

function makeClientWithEntry(
sessionId: string,
entry:
| {
sessionId: string;
midTurnMessageQueue: string[];
events: { publish: ReturnType<typeof vi.fn> };
}
| undefined,
): BridgeClient {
return new BridgeClient(
((sid: string) => (sid === sessionId ? entry : undefined)) as never,
thrower as never,
{ request: thrower } as never,
0,
Infinity,
);
}

it('drains the queue, returns the messages, and publishes one injected frame', async () => {
const publish = vi.fn().mockReturnValue(true);
const entry = {
sessionId: 'sess:drain',
midTurnMessageQueue: ['first', 'second'],
events: { publish },
};
const client = makeClientWithEntry('sess:drain', entry);

const result = await client.extMethod('craft/drainMidTurnQueue', {
sessionId: 'sess:drain',
});

expect(result).toEqual({ messages: ['first', 'second'] });
// Queue emptied so the same messages can't be re-injected on the next batch.
expect(entry.midTurnMessageQueue).toEqual([]);
// Exactly one SSE frame carrying the drained text for the browser to dedupe.
expect(publish).toHaveBeenCalledTimes(1);
expect(publish.mock.calls[0][0]).toMatchObject({
type: 'mid_turn_message_injected',
data: { sessionId: 'sess:drain', messages: ['first', 'second'] },
});
});

it('returns an empty drain and publishes nothing when the queue is empty', async () => {
const publish = vi.fn().mockReturnValue(true);
const entry = {
sessionId: 'sess:empty',
midTurnMessageQueue: [] as string[],
events: { publish },
};
const client = makeClientWithEntry('sess:empty', entry);

const result = await client.extMethod('craft/drainMidTurnQueue', {
sessionId: 'sess:empty',
});

expect(result).toEqual({ messages: [] });
expect(publish).not.toHaveBeenCalled();
});

it('returns an empty drain for an unknown session without throwing', async () => {
const client = makeClientWithEntry('sess:known', undefined);
const result = await client.extMethod('craft/drainMidTurnQueue', {
sessionId: 'sess:absent',
});
expect(result).toEqual({ messages: [] });
});

it('short-circuits to an empty drain when no sessionId is supplied', async () => {
// resolveEntry(undefined) throws on a multi-session channel, so extMethod
// must answer before ever calling it when the sessionId is missing.
const resolveThatThrowsOnUndefined = (sid?: string) => {
if (!sid) {
throw new Error('resolveEntry must not run without a sessionId');
}
return undefined;
};
const client = new BridgeClient(
resolveThatThrowsOnUndefined as never,
thrower as never,
{ request: thrower } as never,
0,
Infinity,
);
const result = await client.extMethod('craft/drainMidTurnQueue', {});
expect(result).toEqual({ messages: [] });
});

it('rejects an unknown ext-method with JSON-RPC methodNotFound (-32601)', async () => {
const client = makeClientWithEntry('sess:x', undefined);
const err = await client
.extMethod('craft/somethingElse', { sessionId: 'sess:x' })
.catch((e: unknown) => e);
expect(err).toBeInstanceOf(RequestError);
expect((err as RequestError).code).toBe(-32601);
});
});
68 changes: 68 additions & 0 deletions packages/acp-bridge/src/bridgeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,34 @@ function sliceLineRange(
* `SessionEntry` is required to provide them — TS enforces the
* structural match at the callback signature).
*/
/**
* ACP ext-method the spawned `qwen --acp` child calls between tool batches to
* pull user messages the browser queued mid-turn. Must match the constant in
* `cli/src/acp-integration/session/Session.ts` (the caller). The desktop ACP
* client answers the same method from its own in-memory queue; in `qwen serve`
* the daemon answers it here, from `SessionEntry.midTurnMessageQueue`.
*/
const MID_TURN_QUEUE_DRAIN_METHOD = 'craft/drainMidTurnQueue';
Comment thread
wenshao marked this conversation as resolved.
Resolved
Comment thread
wenshao marked this conversation as resolved.
Resolved

/**
* SSE frame published when mid-turn messages are actually drained into the
* running turn. The browser consumes it to move those messages out of its
* pending queue (so they aren't resent as the next turn) and render them as
* sent. `data: { sessionId, messages: string[] }`.
*/
const MID_TURN_MESSAGE_INJECTED_EVENT = 'mid_turn_message_injected';
Comment thread
wenshao marked this conversation as resolved.
Resolved

export interface BridgeClientSessionEntry {
sessionId: string;
events: EventBus;
pendingPermissionIds: Set<string>;
/**
* Mid-turn user messages queued by the browser, drained here when the ACP
* child calls the `craft/drainMidTurnQueue` ext-method. Owned by the full
* `SessionEntry` in `bridge.ts`; surfaced on this narrowed view so
* `extMethod` can splice it. See `SessionEntry.midTurnMessageQueue`.
*/
midTurnMessageQueue: string[];
activePromptOriginatorClientId?: string;
/**
* True while the bridge drives a model roundtrip; the
Expand Down Expand Up @@ -507,6 +531,50 @@ export class BridgeClient implements Client {
*/
private readonly inFlightRestoreIds = new Set<string>();

/**
* Handle child->bridge ACP `extMethod` requests (calls that expect a
* response, unlike `extNotification`). The only method served today is
* `craft/drainMidTurnQueue`: the ACP child calls it between tool batches to
* pull any messages the browser queued mid-turn. We splice the per-session
* queue, return them to the child as the response, and — when non-empty —
* publish a `mid_turn_message_injected` SSE frame so the browser can move
* those messages out of its pending queue and render them as sent. Unknown
* methods reject with ACP `methodNotFound` (-32601), matching the SDK's
* default for an unimplemented client surface; the child's drain caller
* treats that as "drain unsupported" and stops asking.
*/
async extMethod(
method: string,
params: Record<string, unknown>,
): Promise<Record<string, unknown>> {
if (method !== MID_TURN_QUEUE_DRAIN_METHOD) {
throw RequestError.methodNotFound(method);
}
const sessionId =
typeof params['sessionId'] === 'string'
? (params['sessionId'] as string)
: undefined;
// The drain always carries a sessionId; without one we can't route it on a
// multi-session channel (and `resolveEntry(undefined)` would throw there),
// so answer with an empty drain rather than poisoning the turn.
if (!sessionId) return { messages: [] };
const entry = this.resolveEntry(sessionId);
if (!entry) return { messages: [] };
const messages = entry.midTurnMessageQueue.splice(0);
if (messages.length > 0) {
try {
entry.events.publish({
Comment thread
wenshao marked this conversation as resolved.
Resolved
type: MID_TURN_MESSAGE_INJECTED_EVENT,
data: { sessionId: entry.sessionId, messages },
});
} catch {
// Bus already closed (session tearing down). The child still receives
// the messages below; only the browser-side echo is lost.
}
}
return { messages };
}

/**
* Handle child->bridge ACP `extNotification` calls. Six methods are
* recognized — `qwen/notify/session/model-update`,
Expand Down
Loading
Loading