Compare commits

...
Author SHA1 Message Date
paseo-bot[bot]andjsilhan cf1fed7b67 fix(pi): wait for runs started by extension commands
Pi acknowledges extension input and slash commands as handled. Check runtime state before completing a handled prompt so a command that launches a model still waits for its reply.

Co-authored-by: jsilhan <jan.silhan@bighub.cz>
2026-11-23 04:45:33 +00:00
jsilhanandCopilot 7317ee1c7f fix(pi): complete turns for prompts pi reports as handled
pi reports a prompt's fate in the RPC response as data.disposition.
"handled" means an extension consumed the prompt and no run starts, so
no agent_settled follows. The connector only read agentInvoked (the Oh My
Pi field) and waited forever. Map disposition to agentInvoked.

Fixes #10548

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-22 21:40:55 +00:00
4 changed files with 107 additions and 8 deletions
@@ -1325,7 +1325,6 @@ export class PiRpcAgentSession implements AgentSession {
if (this.closed) throw new Error("Pi session is closed");
if (this.activeTurnId !== turnId) return { turnId };
this.activeNoTurnPromptText = payload.text;
const shouldProbeForNoTurnPrompt = this.parseSlashCommandInput(payload.text) !== null;
void (async () => {
try {
@@ -1351,7 +1350,11 @@ export class PiRpcAgentSession implements AgentSession {
await this.completeNoTurnPrompt(turnId);
return;
}
if (agentInvoked === undefined && shouldProbeForNoTurnPrompt) {
// Pi can acknowledge a prompt as handled even when the extension starts a run.
if (
agentInvoked === undefined &&
(ack.handledByExtension || this.parseSlashCommandInput(payload.text) !== null)
) {
await this.completePromptIfHandledWithoutTurn(turnId);
}
} catch (error) {
@@ -3,9 +3,11 @@ import path from "node:path";
import type { ChildProcessWithoutNullStreams } from "node:child_process";
import { EventEmitter } from "node:events";
import { PassThrough } from "node:stream";
import { setImmediate as waitForImmediate } from "node:timers/promises";
import pino from "pino";
import { describe, expect, test, vi } from "vitest";
import { describe, expect, onTestFinished, test, vi } from "vitest";
import { PiRpcAgentClient } from "./agent.js";
import { PiCliRuntime } from "./cli-runtime.js";
import type { PiRuntimeLaunch } from "./runtime.js";
@@ -417,6 +419,97 @@ describe("PiCliRuntime", () => {
expect(command.images).toBeUndefined();
});
test.each([
{ disposition: "started", agentInvoked: true },
{ disposition: "queued", agentInvoked: true },
])(
"maps a $disposition prompt disposition to agentInvoked $agentInvoked",
async ({ disposition, agentInvoked }) => {
const child = createPiChild();
replyToCommands(child, (command) => (command.type === "prompt" ? { disposition } : {}));
const session = await createRuntime(child).startSession({ cwd: "/workspace/project" });
await expect(session.prompt("summarize README.md")).resolves.toMatchObject({ agentInvoked });
},
);
test("preserves an explicit agentInvoked result in a prompt response", async () => {
const child = createPiChild();
replyToCommands(child, () => ({ disposition: "started", agentInvoked: false }));
const session = await createRuntime(child).startSession({ cwd: "/workspace/project" });
await expect(session.prompt("hello")).resolves.toEqual({
requestId: expect.any(String),
agentInvoked: false,
});
});
test("completes when an input extension handles a prompt without a model run", async () => {
const child = createPiChild();
replyToCommands(child, (command) => {
if (command.type === "prompt") {
const notification = {
type: "extension_ui_request",
id: "handled",
method: "notify",
message: "Handled by extension",
};
child.stdout.write(`${JSON.stringify(notification)}\n`);
return { disposition: "handled" };
}
return { sessionId: "pi-session-1", isStreaming: false };
});
const client = new PiRpcAgentClient({
logger: pino({ level: "silent" }),
runtime: createRuntime(child),
});
const session = await client.createSession({ provider: "pi", cwd: "/workspace/project" });
onTestFinished(() => session.close());
await expect(session.run("hello")).resolves.toMatchObject({
timeline: expect.arrayContaining([
expect.objectContaining({ type: "notification", message: "Handled by extension" }),
]),
});
});
test("waits for the reply when a handled slash command starts a model run", async () => {
const child = createPiChild();
let isStreaming = false;
replyToCommands(child, (command) => {
if (command.type === "prompt") {
isStreaming = true;
return { disposition: "handled" };
}
return { sessionId: "pi-session-1", isStreaming };
});
const client = new PiRpcAgentClient({
logger: pino({ level: "silent" }),
runtime: createRuntime(child),
});
const session = await client.createSession({ provider: "pi", cwd: "/workspace/project" });
onTestFinished(() => session.close());
const reply = session.run("/ask hello");
// Pi acknowledges the command while its model run is starting, before agent_start.
await waitForImmediate();
await waitForImmediate();
child.stdout.write(`${JSON.stringify({ type: "agent_start" })}\n`);
const message = { role: "assistant", content: [{ type: "text", text: "Hello" }] };
const update = {
type: "message_update",
message,
assistantMessageEvent: { type: "text_delta", delta: "Hello" },
};
child.stdout.write(`${JSON.stringify(update)}\n`);
child.stdout.write(
`${JSON.stringify({ type: "agent_end", messages: [message], willRetry: false })}\n`,
);
child.stdout.write(`${JSON.stringify({ type: "agent_settled" })}\n`);
await expect(reply).resolves.toMatchObject({ finalText: "Hello" });
});
test("sends the clear_queue RPC frame", async () => {
const child = createPiChild();
replyToCommands(child, () => ({}));
@@ -130,10 +130,16 @@ class PiCliRuntimeSession implements PiRuntimeSession {
});
const data = await promise;
if (typeof data === "object" && data !== null && !Array.isArray(data)) {
const { agentInvoked } = data as Record<string, unknown>;
const { agentInvoked, disposition } = data as Record<string, unknown>;
if (typeof agentInvoked === "boolean") {
return { requestId, agentInvoked };
}
if (disposition === "handled") {
return { requestId, handledByExtension: true };
}
if (disposition === "started" || disposition === "queued") {
return { requestId, agentInvoked: true };
}
}
return { requestId };
}
@@ -5,13 +5,10 @@ export interface PiImageContent {
data: string;
mimeType: string;
}
export interface PiPromptAck {
agentInvoked?: boolean;
}
export interface PiPromptAck {
requestId?: string;
agentInvoked?: boolean;
handledByExtension?: true;
}
export interface PiTextContent {