Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

fix(cli): a paste in penguin chat while a Task runs is kept, not dropped

lmliheng755ea330dcf3
diff --git a/changelog/unreleased/2026-10-11-chat-paste-while-running.md b/changelog/unreleased/2026-10-11-chat-paste-while-running.md
new file mode 100644
index 00000000..2af8a27b
--- /dev/null
+++ b/changelog/unreleased/2026-10-11-chat-paste-while-running.md
@@ -0,0 +1,26 @@
+# A paste in `penguin chat` while a Task runs is kept, not dropped
+
+- **Date:** 2026-10-11
+- **Type:** fix
+- **Scope:** `cli`, `docs`
+
+[中文版](2026-10-11-chat-paste-while-running.zh.md)
+
+This is ported from upstream PenguinHarness (#1001, commit `8a1868cf`).
+
+On a terminal, `penguin chat` dropped a bracketed paste that arrived while a Task was running: the
+text never reached the model and nothing on screen said so, although a typed line in the same
+moment became steering. That included a paste made just as a Task ended, before the REPL was back
+at its prompt. A paste now behaves like typed text in every state that takes a message.
+
+## Details
+
+- While a Task ran, a paste was echoed through the renderer and waited for Enter, which sent it
+ as one steering message, all of its lines together. Streamed output was held while it waited,
+ as for a half-typed line, and stayed held after an approval question was answered meanwhile.
+- When the Task ended before Enter, the paste stayed under the continuation prompt and the next
+ Enter sent it as the next prompt; previously the return to the prompt cleared it.
+- A typed line ending in `\` continued the message mid-run as it already did at the prompt.
+- A paste during an approval question or the exit confirmation was still ignored: those take a
+ typed y/N answer.
+- The CLI page's REPL table described steering by paste, in both languages.
diff --git a/changelog/unreleased/2026-10-11-chat-paste-while-running.zh.md b/changelog/unreleased/2026-10-11-chat-paste-while-running.zh.md
new file mode 100644
index 00000000..20b9680d
--- /dev/null
+++ b/changelog/unreleased/2026-10-11-chat-paste-while-running.zh.md
@@ -0,0 +1,19 @@
+# `penguin chat` 在 Task 运行时的粘贴会被保留,不再丢失
+
+- **Date:** 2026-10-11
+- **Type:** fix
+- **Scope:** `cli`, `docs`
+
+[English](2026-10-11-chat-paste-while-running.md)
+
+移植自上游 PenguinHarness(#1001,提交 `8a1868cf`)。
+
+在终端里,`penguin chat` 会丢掉 Task 运行期间到达的括号粘贴(bracketed paste):文本没有送到模型,屏幕上也没有任何提示,而同一时刻键入的一行却会成为插话。Task 刚结束、REPL 还没回到提示符时的粘贴同样会丢失。此次改动让粘贴在所有接受消息的状态下都与键入的文本行为一致。
+
+## 细节
+
+- Task 运行时,粘贴的文本经渲染器回显并等待 Enter,按下后作为一条插话消息整段发出,所有行一起。等待期间流式输出暂停,与输入了一半的行相同;其间回答过审批询问后同样保持暂停。
+- 如果 Task 在按 Enter 之前结束,粘贴的文本保留在续行提示符下,下一次 Enter 把它作为下一条 Prompt 发送;此前回到提示符时会把它清掉。
+- 运行中键入以 `\` 结尾的行会继续当前消息,与在提示符下一致。
+- 审批询问或退出确认期间的粘贴仍被忽略:这两处只接受键入的 y/N 回答。
+- CLI 页面的 REPL 表格中英两版都补充了通过粘贴插话的说明。
diff --git a/packages/cli/src/commands/chat.ts b/packages/cli/src/commands/chat.ts
index 63944379..a929907d 100644
--- a/packages/cli/src/commands/chat.ts
+++ b/packages/cli/src/commands/chat.ts
@@ -226,7 +226,9 @@ export function registerChatCommand(program: Command, t: Messages): void {
// scribble over it — the renderer holds output while the input buffer is non-empty
// and flushes once the line is submitted or cleared. TTY only.
const syncInputHold = (): void => {
- renderer.setInputHold(state === "running" && rli.line.length > 0);
+ renderer.setInputHold(
+ state === "running" && (rli.line.length > 0 || composer.hasPending()),
+ );
};
if (isTTY) {
inputStream.on("data", () => setImmediate(syncInputHold));
@@ -267,11 +269,21 @@ export function registerChatCommand(program: Command, t: Messages): void {
process.once("exit", cleanup);
if (pasteFilter) {
+ // A paste is message text whether or not a Task runs: it waits in the composer for
+ // Enter, which sends it as the next prompt or, mid-run, as steering, the same as a
+ // typed line. Only the y/N answers (approval, exit confirmation) take no paste.
pasteFilter.on("paste", (text: string) => {
- if (state !== "idle") return; // ignore paste while running
+ if (state !== "idle" && state !== "running") return;
const { lineCount, normalized } = composer.pushPaste(text);
if (lineCount === 0) return;
- out.write(`${normalized}\n`);
+ if (state === "running") {
+ // Through the renderer, which finishes an open streamed line first; the hold then
+ // keeps further output off the pasted text until it is sent.
+ renderer.printLine(normalized);
+ syncInputHold();
+ } else {
+ out.write(`${normalized}\n`);
+ }
rl.setPrompt(CONT_PROMPT);
rl.prompt();
});
@@ -330,9 +342,16 @@ export function registerChatCommand(program: Command, t: Messages): void {
resolve(parseApprovalAnswer(line, "allow"));
return;
}
- // running: a non-empty line becomes a steering message for the running Task.
+ // running: a non-empty message becomes a steering message for the running Task, a
+ // paste waiting in the composer included (this Enter sends it whole).
if (state === "running") {
- const text = line.trim();
+ const { message } = composer.pushTypedLine(line);
+ if (message === undefined) {
+ rl.setPrompt(CONT_PROMPT);
+ rl.prompt();
+ return;
+ }
+ const text = message.trim();
if (text.length === 0) return;
steer(text);
}
@@ -397,6 +416,14 @@ export function registerChatCommand(program: Command, t: Messages): void {
}
state = "idle";
pendingLine = resolve;
+ // A paste made while the Task ran and not sent yet stays, under the continuation
+ // prompt: the next Enter sends it as this prompt.
+ if (composer.hasPending()) {
+ out.write("\n");
+ rl.setPrompt(CONT_PROMPT);
+ rl.prompt();
+ return;
+ }
composer.reset();
rli.line = "";
rli.cursor = 0;
@@ -412,6 +439,9 @@ export function registerChatCommand(program: Command, t: Messages): void {
state = "approving";
pendingApproval = (decision) => {
state = "running";
+ // A paste still waiting for Enter holds output again before the screen unlocks
+ // and drains what queued during the question.
+ syncInputHold();
resolve(decision);
};
rl.setPrompt(t.approvePrompt());
diff --git a/packages/cli/test/chat-paste.test.ts b/packages/cli/test/chat-paste.test.ts
new file mode 100644
index 00000000..64663c33
--- /dev/null
+++ b/packages/cli/test/chat-paste.test.ts
@@ -0,0 +1,180 @@
+/**
+ * chat: a bracketed paste on a terminal while a Task runs. Drives the real REPL over a fake
+ * TTY stdin (raw mode, bracketed paste) against the in-process fake server, whose Tasks run
+ * until the test settles them.
+ *
+ * - Given a Task running, when a multi-line text is pasted and Enter pressed, then the whole
+ * text is sent as one steering message and no new Task starts.
+ * - Given a paste made while a Task runs, when the Task ends before Enter, then the next Enter
+ * sends the pasted text as the next prompt: a paste landing as a Task ends is not lost.
+ * - Given a Task running, when Enter alone is pressed (nothing pasted or typed), then nothing
+ * is steered.
+ * - Given a paste waiting for Enter when an approval question comes and is answered, then the
+ * output that queued meanwhile stays off the screen until the paste is sent.
+ */
+import { PassThrough } from "node:stream";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { Command } from "commander";
+import { partialText, toolCall } from "@lmliheng/penguin-core";
+import { registerChatCommand } from "../src/commands/chat.js";
+import { getMessages } from "../src/i18n.js";
+import { FakeServer, type FakeSessionState } from "./fake-server.js";
+
+const PASTE_START = "\x1b[200~";
+const PASTE_END = "\x1b[201~";
+const ENTER = "\r";
+
+let server: FakeServer;
+let uninstall: () => void;
+
+beforeEach(() => {
+ server = new FakeServer();
+ server.hangTasks = true;
+ uninstall = server.install();
+});
+afterEach(() => {
+ uninstall();
+ vi.restoreAllMocks();
+});
+
+interface Tty {
+ type(text: string): void;
+ paste(text: string): void;
+ /** Resolves once the chat draws `prompt` after everything it had printed when called. */
+ nextPrompt(prompt: string): () => Promise<void>;
+ /** Everything the chat has written to the terminal so far. */
+ printed(): string;
+ /** The one Session the chat created. */
+ session(): FakeSessionState;
+ /** Ends the running Task the way the server does: the Session's stream reports idle. */
+ settle(): void;
+ /** Resolves when the chat has exited. */
+ done: Promise<unknown>;
+}
+
+/** Starts `penguin chat` on a fake terminal: stdin claims a TTY, so raw mode and bracketed paste are on. */
+function startChat(): Tty {
+ const stdin = new PassThrough() as PassThrough & {
+ isTTY: boolean;
+ setRawMode: (mode: boolean) => unknown;
+ };
+ stdin.isTTY = true;
+ stdin.setRawMode = () => stdin;
+ const realStdin = process.stdin;
+ Object.defineProperty(process, "stdin", { value: stdin, configurable: true });
+ let printed = "";
+ vi.spyOn(process.stdout, "write").mockImplementation((chunk) => {
+ printed += String(chunk);
+ return true;
+ });
+ const program = new Command();
+ program.exitOverride();
+ registerChatCommand(program, getMessages("en"));
+ const done = program.parseAsync(["node", "penguin", "chat"]).finally(() => {
+ Object.defineProperty(process, "stdin", { value: realStdin, configurable: true });
+ });
+ const session = (): FakeSessionState => {
+ const [only] = [...server.sessions.values()];
+ if (!only) throw new Error("no Session yet");
+ return only;
+ };
+ return {
+ type: (text) => stdin.write(text),
+ paste: (text) => stdin.write(`${PASTE_START}${text}${PASTE_END}`),
+ nextPrompt: (prompt) => {
+ const from = printed.length;
+ return () => vi.waitFor(() => expect(printed.slice(from)).toContain(prompt));
+ },
+ printed: () => printed,
+ session,
+ settle: () => {
+ const s = session();
+ s.status = "idle";
+ server.emitServerEvent(s.sessionId, { type: "task_state", state: "idle" });
+ },
+ done,
+ };
+}
+
+const taskTexts = (s: FakeSessionState): string[] =>
+ s.tasks.map((b) => (b.input as Array<{ text: string }>)[0]!.text);
+const steerTexts = (s: FakeSessionState): string[] => s.steers.map((b) => String(b.text));
+
+/** Starts the chat and its first Task; returns once that Task runs on the server. */
+async function chatWithRunningTask(): Promise<Tty> {
+ const tty = startChat();
+ await vi.waitFor(() => expect(tty.session()).toBeDefined());
+ tty.type(`first task${ENTER}`);
+ await vi.waitFor(() => expect(tty.session().status).toBe("running"));
+ return tty;
+}
+
+/** Ends the running Task, waits for the main prompt, and quits. */
+async function settleAndExit(tty: Tty): Promise<void> {
+ const prompt = tty.nextPrompt("> ");
+ tty.settle();
+ await prompt();
+ tty.type(`/exit${ENTER}`);
+ await tty.done;
+}
+
+describe("chat: a paste while a Task runs", () => {
+ it("a multi-line paste and Enter steer the running Task with the whole text", async () => {
+ const tty = await chatWithRunningTask();
+ tty.paste("look at\nboth lines");
+ tty.type(ENTER);
+ await vi.waitFor(() => expect(steerTexts(tty.session())).toEqual(["look at\nboth lines"]));
+ await settleAndExit(tty);
+ expect(taskTexts(tty.session())).toEqual(["first task"]);
+ });
+
+ it("a paste the Task ends before is sent as the next prompt by the next Enter", async () => {
+ const tty = await chatWithRunningTask();
+ tty.paste("the next prompt");
+ // The Task ends before Enter: the chat comes back with the paste still waiting.
+ const continuation = tty.nextPrompt("… ");
+ tty.settle();
+ await continuation();
+ tty.type(ENTER);
+ await vi.waitFor(() =>
+ expect(taskTexts(tty.session())).toEqual(["first task", "the next prompt"]),
+ );
+ await settleAndExit(tty);
+ expect(steerTexts(tty.session())).toEqual([]);
+ });
+
+ it("output queued during an approval question stays held while a paste waits for Enter", async () => {
+ const tty = await chatWithRunningTask();
+ const sessionId = tty.session().sessionId;
+ tty.paste("after the approval");
+ // A reply streams while the paste waits (held), then the Task asks for an approval.
+ server.emit(sessionId, partialText("start"));
+ server.emit(sessionId, partialText("delta", "reply held behind the paste"));
+ server.emit(sessionId, partialText("stop"));
+ const question = tty.nextPrompt("Approve this tool call?");
+ server.emitServerEvent(sessionId, {
+ type: "approval_request",
+ toolCall: toolCall({ name: "exec_command", arguments: '{"cmd":"ls"}', toolCallId: "c1" }),
+ });
+ await question();
+ // `y` and Enter arrive as two keystrokes, as a person types them.
+ tty.type("y");
+ await new Promise((resolve) => setImmediate(resolve));
+ tty.type(ENTER);
+ await vi.waitFor(() => expect(tty.printed()).toContain("[approved]"));
+ expect(tty.printed()).not.toContain("reply held behind the paste");
+
+ tty.type(ENTER);
+ await vi.waitFor(() => expect(steerTexts(tty.session())).toEqual(["after the approval"]));
+ await vi.waitFor(() => expect(tty.printed()).toContain("reply held behind the paste"));
+ await settleAndExit(tty);
+ });
+
+ it("Enter alone while a Task runs steers nothing", async () => {
+ const tty = await chatWithRunningTask();
+ tty.type(ENTER);
+ await settleAndExit(tty);
+ expect(steerTexts(tty.session())).toEqual([]);
+ expect(taskTexts(tty.session())).toEqual(["first task"]);
+ });
+});
diff --git a/packages/docs/content/cli.en.md b/packages/docs/content/cli.en.md
index 25ec6e4e..fcb5f8ab 100644
--- a/packages/docs/content/cli.en.md
+++ b/packages/docs/content/cli.en.md
@@ -95,7 +95,7 @@ With `--resume`, the original Session fixes the Workspace and model, so `--works
| Input | Behavior |
| --- | --- |
-| Any text while a Task runs | Mid-run steering. The line is queued and reaches the model between turns as a `[user_steering]` user message, and a `»` acknowledgment echoes the text. Rendering pauses while you type, so streamed output does not overwrite the line. If the Task finishes first, the line is sent as the next normal prompt. |
+| Any text while a Task runs, typed or pasted | Mid-run steering. Enter sends it, a multi-line paste as one message; the text is queued and reaches the model between turns as a `[user_steering]` user message, and a `»` acknowledgment echoes the text. Rendering pauses while you type, so streamed output does not overwrite the line. If the Task finishes first, the text is sent as the next normal prompt: a line already entered at once, a paste not yet entered by the next Enter. |
| `/goal[:<budget>] <objective>` | Runs goal mode on the objective. The optional budget is a token budget, such as `/goal:500k`. Ctrl-C aborts the whole goal. See [Goal mode](/goal-mode). |
| `/compact` | Compacts the current context now. |
| `/clear` | Starts a fresh blank Session in place, on the same Workspace and model. The old Session stays on the server and can be resumed with `--resume`. |
diff --git a/packages/docs/content/cli.zh.md b/packages/docs/content/cli.zh.md
index 9d755a0f..d75b28b1 100644
--- a/packages/docs/content/cli.zh.md
+++ b/packages/docs/content/cli.zh.md
@@ -95,7 +95,7 @@ penguin chat [options]
| 输入 | 行为 |
| --- | --- |
-| Task 运行期间输入的任意文本 | 运行中插话。这一行会排队,在两轮之间以 `[user_steering]` 用户消息的形式送达模型,同时一个 `»` 确认符会回显这段文本。你输入时渲染暂停,流式输出不会覆盖这一行。如果 Task 先结束,这一行会变成下一条普通 Prompt 发送出去。 |
+| Task 运行期间输入或粘贴的任意文本 | 运行中插话。按 Enter 发出,多行粘贴作为一条消息;文本会排队,在两轮之间以 `[user_steering]` 用户消息的形式送达模型,同时一个 `»` 确认符会回显这段文本。你输入时渲染暂停,流式输出不会覆盖这一行。如果 Task 先结束,文本会作为下一条普通 Prompt 发送:已按 Enter 的行立即发送,尚未按 Enter 的粘贴在下一次按 Enter 时发送。 |
| `/goal[:<budget>] <objective>` | 对给出的目标运行目标模式。可选的预算是 Token 预算,例如 `/goal:500k`。Ctrl-C 会中止整个目标。见[目标模式](/goal-mode)。 |
| `/compact` | 立即压缩当前上下文。 |
| `/clear` | 原地开启一个全新的空白 Session,仍用同一个 Workspace 和模型。旧 Session 保留在服务器上,之后可用 `--resume` 恢复。 |