From f37252180b3f3580de3f2d08acf93e333e0d034e Mon Sep 17 00:00:00 2001 From: Edward Lawford Date: Mon, 30 Nov 2026 21:40:10 +0000 Subject: [PATCH] fix(openclaw-gateway): keep whitespace on streamed assistant deltas (#14150) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The `openclaw_gateway` adapter streams the agent reply as `assistant` deltas and joins them into the run summary > - Each delta goes through `nonEmpty()`, which trims whitespace > - Deltas carry their word-boundary space (`" blocked"`), so the trim removes it and words run together > - The broken text is stored as the issue comment, so users see corrupted agent replies > - This pull request keeps each delta verbatim and skips only empty strings > - The benefit is that agent comments from the OpenClaw gateway are stored correctly ## Linked Issues or Issue Description Fixes #14149 ## What Changed - Add `readAssistantChunk()` in `execute.ts`. It returns a non-empty string `delta` without a trim. If there is no delta, it falls back to the trimmed `text` (the old behavior). - Use `readAssistantChunk()` in the `assistant` stream handler. - Add unit tests for whitespace-bearing, whitespace-only, empty, and non-string deltas. ## Verification - `npx vitest run src/server/execute.test.ts` in `packages/adapters/openclaw-gateway`: 12 tests pass. - `npx tsc --noEmit` in the same package: no errors. - Manual: the same one-line change is applied to a local 2026.916.1 install that showed the bug. ## Risks Low risk. The change affects only `assistant` deltas. The final `join("").trim()` still removes outer whitespace from the summary. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: EpaL <358229+EpaL@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 Co-authored-by: Andrew Aymeloglu --- .../src/server/execute-dispatch.test.ts | 23 +++++++++++++++++++ .../src/server/execute.test.ts | 23 ++++++++++++++++++- .../openclaw-gateway/src/server/execute.ts | 14 +++++------ 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/packages/adapters/openclaw-gateway/src/server/execute-dispatch.test.ts b/packages/adapters/openclaw-gateway/src/server/execute-dispatch.test.ts index 3c60b617c88..3a62cf2e651 100644 --- a/packages/adapters/openclaw-gateway/src/server/execute-dispatch.test.ts +++ b/packages/adapters/openclaw-gateway/src/server/execute-dispatch.test.ts @@ -8,6 +8,7 @@ const websocketState = vi.hoisted(() => ({ failAgentRequests: 0, events: [] as string[], messages: [] as string[], + assistantChunks: [] as Array<{ delta: string; text?: string }>, })); vi.mock("ws", async () => { @@ -51,6 +52,15 @@ vi.mock("ws", async () => { ? { protocol: 3 } : { status: "ok", runId: "remote-run-1", summary: "done" }; queueMicrotask(() => { + if (request.method === "agent") { + for (const data of websocketState.assistantChunks) { + this.emit("message", JSON.stringify({ + type: "event", + event: "agent", + payload: { runId: "run-1", stream: "assistant", data }, + })); + } + } this.emit("message", JSON.stringify({ type: "res", id: request.id, @@ -109,12 +119,25 @@ describe("openclaw_gateway execute dispatch boundary", () => { websocketState.failAgentRequests = 0; websocketState.events = []; websocketState.messages = []; + websocketState.assistantChunks = []; }); afterEach(() => { vi.useRealTimers(); }); + it("preserves a space delta when the event also includes accumulated text", async () => { + websocketState.assistantChunks = [ + { delta: "Hello", text: "Hello" }, + { delta: " ", text: "Hello " }, + { delta: "world", text: "Hello world" }, + ]; + + const result = await execute(createContext()); + expect(result.exitCode).toBe(0); + expect(result.summary).toBe("Hello world"); + }); + it.each([false, true])("sends conversation policy without the issue-completion workflow (resumed=%s)", async (resumed) => { const ctx = createContext(); const directive = "Chat directive: clarify goals and hand plans off to project tasks."; diff --git a/packages/adapters/openclaw-gateway/src/server/execute.test.ts b/packages/adapters/openclaw-gateway/src/server/execute.test.ts index 3689e412458..8a44d083ecb 100644 --- a/packages/adapters/openclaw-gateway/src/server/execute.test.ts +++ b/packages/adapters/openclaw-gateway/src/server/execute.test.ts @@ -1,5 +1,10 @@ import { describe, expect, it } from "vitest"; -import { buildAgentParams, resolveClaimedApiKeyPath, resolveSessionKey } from "./execute.js"; +import { + buildAgentParams, + readAssistantChunk, + resolveClaimedApiKeyPath, + resolveSessionKey, +} from "./execute.js"; describe("resolveSessionKey", () => { it("prefixes run-scoped session keys with the configured agent", () => { @@ -123,3 +128,19 @@ describe("resolveClaimedApiKeyPath", () => { expect(resolveClaimedApiKeyPath({})).toBe(DEFAULT_PATH); }); }); + +describe("readAssistantChunk", () => { + it("keeps leading and trailing whitespace on streamed deltas", () => { + const chunks = ["Hello", " world", " ", "is", " blocked."].map((delta) => + readAssistantChunk({ delta }), + ); + expect(chunks).toEqual(["Hello", " world", " ", "is", " blocked."]); + expect(chunks.join("")).toBe("Hello world is blocked."); + }); + + it("ignores empty or non-string deltas and falls back to text", () => { + expect(readAssistantChunk({ delta: "", text: " full reply " })).toBe("full reply"); + expect(readAssistantChunk({ delta: 42, text: "full reply" })).toBe("full reply"); + expect(readAssistantChunk({})).toBeNull(); + }); +}); diff --git a/packages/adapters/openclaw-gateway/src/server/execute.ts b/packages/adapters/openclaw-gateway/src/server/execute.ts index b8ada3c2e44..b526f5010e3 100644 --- a/packages/adapters/openclaw-gateway/src/server/execute.ts +++ b/packages/adapters/openclaw-gateway/src/server/execute.ts @@ -491,6 +491,11 @@ function joinWakePayloadSections(structuredWakePrompt: string, structuredWakeJso return sections.join("\n"); } +export function readAssistantChunk(data: Record): string | null { + if (typeof data.delta === "string" && data.delta.length > 0) return data.delta; + return nonEmpty(data.text); +} + export function buildAgentParams(input: { payloadTemplate: Record; message: string; @@ -1230,13 +1235,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise