From fdbd586130015e92e81fabdab4dbbdc9de0f7a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20=F0=9D=95=8Denturini?= Date: Sat, 5 Dec 2026 12:23:32 +0100 Subject: [PATCH] fix(hermes): keep inline diff gutter lines out of the run result (#18947) 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 `hermes_local` adapter runs the Hermes CLI and reads its stdout > - The adapter uses the stdout before `session_id:` as the run result. Paperclip posts this result as an auto-comment when the agent does not post its own comment. > - The Hermes gateway client prints the inline diff of each edit behind a `│` gutter. It does this also in quiet mode (`-Q`) and with `display.inline_diffs: false`. > - `cleanResponse()` does not remove these lines. The run summary and the auto-comment then contain only code diffs, cut at 2,000 characters. > - The live issue chat uses `parseHermesStdoutLine()`. This parser shows the same lines as assistant text, so the agent appears to "speak" diffs. > - This pull request removes the gutter lines from the result and classifies them as stdout in both UI parsers (TypeScript and `ui-parser.cjs`). > - The benefit is that auto-comments and the issue chat show what the agent wrote, not raw diffs. ## Linked Issues or Issue Description No issue exists. I searched open and closed issues and PRs for "hermes diff", "hermes cleanResponse" and Hermes adapter PRs. I found no duplicate. **What happened?** An agent on `hermes_local` ran through the Hermes gateway client. When a run ended without an agent comment (timeout or interrupted run), Paperclip posted an auto-comment (`authorizationReason: internal_agent_write`). The comment contained only diff lines such as `│ apps/web/... │`, `│ @@ -1,2 +1,3 @@`, `│ +...`. The live issue chat showed the same lines as assistant messages. **Expected behavior** The auto-comment contains the agent's final text only. The issue chat shows diff lines as tool output, not as assistant speech. **Steps to reproduce** 1. Configure an agent with `hermes_local` and a Hermes setup that uses the gateway client. 2. Give the agent a task that edits files. 3. Let the run end without an agent comment (for example, a timeout). 4. Look at the auto-comment and at the live issue chat during the run. **Agent adapter(s) involved** Hermes ## What Changed - `packages/adapters/hermes/src/server/execute.ts`: `cleanResponse()` removes lines that start with `│` after ANSI codes are removed. - `packages/adapters/hermes/src/ui/parse-stdout.ts`: `parseHermesStdoutLine()` returns `stdout` for lines that start with `│`. Before, they became `assistant`. - `packages/adapters/hermes/ui-parser.cjs`: the same rule for the CJS parser. Externally installed adapters load this file. - New test `src/ui/ui-parser-cjs.test.ts`: tests the exported `parseStdoutLine` from `ui-parser.cjs`. - New test `src/server/execute.response.test.ts`: runs `execute()` with Hermes stdout that has gutter diff lines. It checks `summary` and `resultJson.result`. - New tests in `src/ui/parse-stdout.test.ts` for plain and colored gutter lines. ## Verification - `pnpm --filter @paperclipai/hermes-paperclip-adapter test`: 12 files, 180 tests pass. - `pnpm --filter @paperclipai/hermes-paperclip-adapter typecheck`: pass. - The five new parser and execute tests fail without the source change and pass with it. - Manual: I applied the same change to the built adapter on a live instance (2026.1023.0). After a restart, auto-comments from a Hermes agent contained the final text only. The issue chat stopped showing diff lines as assistant text. ## Risks - Low risk. The change affects only lines whose first visible character is `│` (U+2502). - If an agent writes a final answer line that starts with `│`, the adapter now removes that line from the result. This is unlikely in normal prose. The `┊` (U+250A) tool lines are not affected. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, test runs). The model found the cause, wrote the change and the tests, and wrote this description. A human reviewed and approved the work. ## 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 (not applicable: no documented behavior changes) - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] 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: Thomas Venturini <4708070+tjventurini@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- .../src/server/execute.response.test.ts | 77 +++++++++++++++++++ .../adapters/hermes/src/server/execute.ts | 1 + .../hermes/src/ui/parse-stdout.test.ts | 12 +++ .../adapters/hermes/src/ui/parse-stdout.ts | 4 + .../hermes/src/ui/ui-parser-cjs.test.ts | 29 +++++++ packages/adapters/hermes/ui-parser.cjs | 4 + 6 files changed, 127 insertions(+) create mode 100644 packages/adapters/hermes/src/server/execute.response.test.ts create mode 100644 packages/adapters/hermes/src/ui/ui-parser-cjs.test.ts diff --git a/packages/adapters/hermes/src/server/execute.response.test.ts b/packages/adapters/hermes/src/server/execute.response.test.ts new file mode 100644 index 00000000000..1807c99c195 --- /dev/null +++ b/packages/adapters/hermes/src/server/execute.response.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, it, vi } from "vitest"; + +const HERMES_STDOUT = [ + "│ apps/web/src/page.tsx │", + "│ @@ -1,2 +1,3 @@", + "\x1b[2m│\x1b[0m \x1b[32m+const x = 1;\x1b[0m", + "Implemented the page and ran the tests.", + "", + "session_id: 20261103_120000_abcdef", +].join("\n"); + +vi.mock("@paperclipai/adapter-utils/server-utils", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + runChildProcess: vi.fn(async () => ({ + exitCode: 0, + signal: null, + timedOut: false, + stdout: HERMES_STDOUT, + stderr: "", + })), + }; +}); + +vi.mock("node:fs/promises", () => ({ + readFile: vi.fn(async () => ""), + writeFile: vi.fn(async () => undefined), + mkdir: vi.fn(async () => undefined), + rm: vi.fn(async () => undefined), + access: vi.fn(async () => undefined), + readdir: vi.fn(async () => []), + stat: vi.fn(async () => ({ isFile: () => true, isDirectory: () => false })), +})); + +import { execute } from "./execute.js"; + +describe("hermes_local execute — response cleaning", () => { + it("drops inline diff gutter lines from the summary and result", async () => { + const ctx = { + runId: "test-run-1", + agent: { + id: "agent-1", + companyId: "company-1", + name: "Hermes", + adapterType: "hermes_local", + adapterConfig: {}, + }, + runtime: { + sessionId: null, + sessionParams: null, + sessionDisplayId: null, + taskKey: null, + }, + config: { + command: "/usr/bin/hermes", + timeoutSec: 60, + graceSec: 5, + }, + context: { + issueId: "issue-1", + wakeReason: "manual", + paperclipWake: null, + }, + onLog: vi.fn(async () => undefined), + onMeta: vi.fn(async () => undefined), + onSpawn: vi.fn(async () => undefined), + }; + + const result = await execute(ctx as unknown as Parameters[0]); + + expect(result.summary).toBe("Implemented the page and ran the tests."); + expect((result.resultJson as { result: string }).result).toBe( + "Implemented the page and ran the tests.", + ); + }); +}); diff --git a/packages/adapters/hermes/src/server/execute.ts b/packages/adapters/hermes/src/server/execute.ts index a9568010d57..f962621dc23 100644 --- a/packages/adapters/hermes/src/server/execute.ts +++ b/packages/adapters/hermes/src/server/execute.ts @@ -259,6 +259,7 @@ function cleanResponse(raw: string): string { if (!t) return true; // keep blank lines for paragraph separation if (t.startsWith("[tool]") || t.startsWith("[hermes]") || t.startsWith("[paperclip]")) return false; if (t.startsWith("session_id:")) return false; + if (t.replace(/\x1b\[[0-9;]*m/g, "").startsWith("│")) return false; if (/^\[\d{4}-\d{2}-\d{2}T/.test(t)) return false; if (/^\[done\]\s*┊/.test(t)) return false; if (/^┊\s*[\p{Emoji_Presentation}]/u.test(t) && !/^┊\s*💬/.test(t)) return false; diff --git a/packages/adapters/hermes/src/ui/parse-stdout.test.ts b/packages/adapters/hermes/src/ui/parse-stdout.test.ts index 8e25cf8cd76..3739a118935 100644 --- a/packages/adapters/hermes/src/ui/parse-stdout.test.ts +++ b/packages/adapters/hermes/src/ui/parse-stdout.test.ts @@ -79,3 +79,15 @@ describe("parseHermesStdoutLine — ANSI stripping", () => { expect(result).toHaveLength(0); }); }); + +describe("parseHermesStdoutLine — inline diff gutter", () => { + it("classifies a gutter diff line as stdout, not assistant", () => { + const result = parseHermesStdoutLine("│ @@ -1,3 +1,4 @@", TS); + expect(result).toEqual([{ kind: "stdout", ts: TS, text: "│ @@ -1,3 +1,4 @@" }]); + }); + + it("classifies a colored gutter diff line as stdout", () => { + const result = parseHermesStdoutLine("\x1b[2m│\x1b[0m \x1b[32m+const x = 1;\x1b[0m", TS); + expect(result).toEqual([{ kind: "stdout", ts: TS, text: "│ +const x = 1;" }]); + }); +}); diff --git a/packages/adapters/hermes/src/ui/parse-stdout.ts b/packages/adapters/hermes/src/ui/parse-stdout.ts index 1589843ed95..eed2306d66b 100644 --- a/packages/adapters/hermes/src/ui/parse-stdout.ts +++ b/packages/adapters/hermes/src/ui/parse-stdout.ts @@ -268,6 +268,10 @@ export function parseHermesStdoutLine( return [{ kind: "stdout", ts, text: stripped }]; } + if (trimmed.startsWith("│")) { + return [{ kind: "stdout", ts, text: trimmed }]; + } + // ── Thinking blocks ──────────────────────────────────────────────────── if (isThinkingLine(trimmed)) { return [ diff --git a/packages/adapters/hermes/src/ui/ui-parser-cjs.test.ts b/packages/adapters/hermes/src/ui/ui-parser-cjs.test.ts new file mode 100644 index 00000000000..20b56c15241 --- /dev/null +++ b/packages/adapters/hermes/src/ui/ui-parser-cjs.test.ts @@ -0,0 +1,29 @@ +import { createRequire } from "node:module"; +import { describe, expect, it } from "vitest"; + +const require = createRequire(import.meta.url); +const { parseStdoutLine } = require("../../ui-parser.cjs") as { + parseStdoutLine: (line: string, ts: string) => Array<{ kind: string; text?: string }>; +}; + +const TS = "2026-11-04T00:00:00.000Z"; + +describe("ui-parser.cjs parseStdoutLine — inline diff gutter", () => { + it("classifies a gutter diff line as stdout, not assistant", () => { + expect(parseStdoutLine("│ @@ -1,3 +1,4 @@", TS)).toEqual([ + { kind: "stdout", ts: TS, text: "│ @@ -1,3 +1,4 @@" }, + ]); + }); + + it("classifies a colored gutter diff line as stdout", () => { + expect(parseStdoutLine("\x1b[2m│\x1b[0m \x1b[32m+const x = 1;\x1b[0m", TS)).toEqual([ + { kind: "stdout", ts: TS, text: "│ +const x = 1;" }, + ]); + }); + + it("keeps regular text as assistant", () => { + expect(parseStdoutLine("Implemented the page.", TS)).toEqual([ + { kind: "assistant", ts: TS, text: "Implemented the page." }, + ]); + }); +}); diff --git a/packages/adapters/hermes/ui-parser.cjs b/packages/adapters/hermes/ui-parser.cjs index 48d1ac9062a..f18d7a9fac8 100644 --- a/packages/adapters/hermes/ui-parser.cjs +++ b/packages/adapters/hermes/ui-parser.cjs @@ -181,6 +181,10 @@ function parseStdoutLine(line, ts) { return [{ kind: "stdout", ts, text: stripped }]; } + if (trimmed.startsWith("\u2502")) { + return [{ kind: "stdout", ts, text: trimmed }]; + } + if (isThinkingLine(trimmed)) { return [{ kind: "thinking", ts, text: trimmed.replace(/^\u{1f4ad}\s*/u, "") }]; }