fix(hermes): keep inline diff gutter lines out of the run result (#18947)

## 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 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-12-05 03:23:32 -08:00
1 parent 7612e79215
commit fdbd586130
6 files changed
+127

No files matched your search

@@ -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<typeof import("@paperclipai/adapter-utils/server-utils")>();
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<typeof execute>[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.",
);
});
});
@@ -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;
@@ -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;" }]);
});
});
@@ -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 [
@@ -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." },
]);
});
});
+4
View File
@@ -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, "") }];
}