Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

feat(session-collaboration): let a send choose immediate or queued delivery

Tioit-Wanga28bc2eb1ad0
diff --git a/apps/desktop/electron/main/services/session-collaboration.ts b/apps/desktop/electron/main/services/session-collaboration.ts
index a20413d18..5af28cb80 100644
--- a/apps/desktop/electron/main/services/session-collaboration.ts
+++ b/apps/desktop/electron/main/services/session-collaboration.ts
@@ -62,6 +62,18 @@ function messageKind(value: unknown): "task" | "message" {
return value;
}
+/**
+ * Delivery order for one send. `queue` is the historical behaviour: the message
+ * appends behind whatever the target already queued. `immediate` promotes it so a
+ * busy target runs it at the next turn boundary (ADR 0265). It never preempts a
+ * running turn, because steering stays restricted to a paired caller.
+ */
+function deliveryMode(value: unknown): "queue" | "immediate" {
+ if (value === undefined) return "queue";
+ if (value !== "queue" && value !== "immediate") fail("INVALID_ARGUMENT", "delivery must be queue or immediate");
+ return value;
+}
+
function errorCode(error: unknown): string | undefined {
if (!error || typeof error !== "object") return undefined;
const value = error as { code?: string; errorCode?: string; data?: { errorCode?: string } };
@@ -113,7 +125,11 @@ export function createSessionCollaborationService(deps: SessionCollaborationDepe
return { pluginId: context.pluginId, sourceSessionId: context.sessionId, sourceTurnId: context.turnId };
}
- async function dispatch(message: SessionCollaborationMessage, signal?: AbortSignal): Promise<SessionCollaborationDelivery> {
+ async function dispatch(
+ message: SessionCollaborationMessage,
+ signal?: AbortSignal,
+ delivery_: "queue" | "immediate" = "queue",
+ ): Promise<SessionCollaborationDelivery> {
const existing = dispatching.get(message.id);
if (existing) return existing;
const operation = (async () => {
@@ -142,12 +158,13 @@ export function createSessionCollaborationService(deps: SessionCollaborationDepe
context: { requestId: `session-message:${current.id}` },
});
checkCurrent(host);
- // A worker's report is not user input that was queued a minute ago. When
- // the target is busy, the receipt must run at the very next turn boundary
- // (promoted, ADR 0265) instead of behind everything the user already
- // queued. A turn that already started, or a queue move that lost a race,
- // is not an error.
- if (current.kind === "completion" && started.turn.status === "queued") {
+ // A worker's report is not user input that was queued a minute ago, and a
+ // caller may ask for one specific follow-up to jump the queue the same way
+ // (`delivery: "immediate"`, ADR 0265). Either way a busy target runs it at
+ // the very next turn boundary; a turn that already started, or a queue move
+ // that lost a race, is not an error.
+ const promote = delivery_ === "immediate" || current.kind === "completion";
+ if (promote && started.turn.status === "queued") {
await bridge.queue.prioritize(started.turn.id).catch((error: unknown) => {
if (errorCode(error) !== "CONFLICT") {
deps.log("Session completion delivery could not be prioritized", {
@@ -291,6 +308,10 @@ export function createSessionCollaborationService(deps: SessionCollaborationDepe
const content = text(data, spawn ? "task" : "content");
const notify = data.notifyOnCompletion;
if (notify !== undefined && typeof notify !== "boolean") fail("INVALID_ARGUMENT", "notifyOnCompletion must be a boolean");
+ // Delivery order belongs to the follow-up a caller sends; a spawn's first
+ // task always takes the normal queue.
+ if (spawn && data.delivery !== undefined) fail("INVALID_ARGUMENT", "delivery is only valid for send");
+ const deliveryHint = spawn ? "queue" : deliveryMode(data.delivery);
let model: { providerId: string; modelId: string } | null = null;
if (spawn) {
const [listed, settings] = await Promise.all([
@@ -327,7 +348,7 @@ export function createSessionCollaborationService(deps: SessionCollaborationDepe
...(spawn ? { ...model, title: text(data, "title", false) }
: { sessionId: text(data, "sessionId"), kind: messageKind(data.kind) }),
});
- return dispatch(response.message, input.signal);
+ return dispatch(response.message, input.signal, deliveryHint);
}
default: fail("NOT_FOUND", "Unknown session collaboration operation");
}
diff --git a/apps/desktop/electron/main/session-collaboration-control.ts b/apps/desktop/electron/main/session-collaboration-control.ts
index 705e46b25..5386ec571 100644
--- a/apps/desktop/electron/main/session-collaboration-control.ts
+++ b/apps/desktop/electron/main/session-collaboration-control.ts
@@ -17,7 +17,7 @@ export const SESSION_COLLABORATION_OPERATIONS: McpControlOperation[] = [
{
id: "session/collaboration/send", channel: "internal:session-collaboration", risk: "write",
description: "Send a task or message to an existing Session ID, preserving context and permissions.",
- argumentShape: ["{sessionId,content,kind?,notifyOnCompletion?,idempotencyKey?}"],
+ argumentShape: ["{sessionId,content,kind?,notifyOnCompletion?,delivery?,idempotencyKey?}"],
pluginOnly: true,
},
{
diff --git a/docs/spec/07-plugins/03-plugin-api.md b/docs/spec/07-plugins/03-plugin-api.md
index 2e5469603..539544de9 100644
--- a/docs/spec/07-plugins/03-plugin-api.md
+++ b/docs/spec/07-plugins/03-plugin-api.md
@@ -1020,7 +1020,12 @@ the source delivery and is created at most once after the actual target turn
settles, including a delivery fenced as `interrupted` by an application restart.
A completion receipt is promoted in the target session's turn queue, so a busy
target runs it at the next turn boundary ahead of input the user queued earlier
-(ADR 0265); it is never injected into another session's running turn.
+A completion receipt is promoted in the target session's turn queue, so a busy
+target runs it at the next turn boundary ahead of input the user queued earlier
+(ADR 0265); it is never injected into another session's running turn. A `send`
+may ask for the same treatment with `delivery: "immediate"`; the default
+`queue` appends behind whatever the target already queued.
+
The callback is session data, not a new user authorization, and completion
messages do not trigger another callback.
diff --git a/docs/zh-CN/spec/07-plugins/03-plugin-api.md b/docs/zh-CN/spec/07-plugins/03-plugin-api.md
index 62df3f258..d60cbcce9 100644
--- a/docs/zh-CN/spec/07-plugins/03-plugin-api.md
+++ b/docs/zh-CN/spec/07-plugins/03-plugin-api.md
@@ -842,8 +842,10 @@ Session ID,并复用该会话的项目、模型、上下文和权限配置;`
最多创建一次;被应用重启栅栏标记为 `interrupted` 的投递也一样会生成该回执。
完成回执会在目标会话的回合队列中被提升,因此繁忙的目标会在下一个回合边界优先执行它,
而不是排在用户早先排队的输入之后(ADR 0265);它永远不会被注入另一个会话正在运行的回合。
+完成回执会在目标会话的回合队列中被提升,因此繁忙的目标会在下一个回合边界优先执行它,
+而不是排在用户早先排队的输入之后(ADR 0265);它永远不会被注入另一个会话正在运行的回合。
+`send` 可以用 `delivery: "immediate"` 请求同样的处理;默认的 `queue` 会排到目标已排队输入之后。
回调是会话数据,不是新的用户授权;完成消息不会触发另一个回调。
-
渲染器可以读取单独的侧边栏协作投影,但插件面板不能绕过此网关调用变更操作。
### agent.complete(需要 `agent.complete`)