mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 09:05:15 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents post chat replies and task comments that link board pages and
attachments
> - Some models invent a hostname for these links, such as
`https://paperclip.local/COM/agents/<id>` or
`https://paperclip/api/attachments/<id>/content`
> - Those hosts do not exist, so the person clicks a broken link
> - The chat guidance already says never to invent a hostname, but a
model can still do it
> - This pull request rewrites such links to relative app routes before
an agent comment is saved, and adds the rule to the Paperclip skill
> - The benefit is that every board and attachment link an agent writes
opens, on whatever origin the reader uses
## Linked Issues or Issue Description
**What happened?**
Chat-eval cohort 16 (OpenCode with
`openrouter/deepseek/deepseek-v4.1-flash-1017`, master 819f61f1fd). In
`agents.edit-instructions`, the reply linked Morgan as
`[Morgan](https://paperclip.local/COM/agents/<id>)`. In
`onboarding.first-work`, the worker's summary linked its attachments as
`https://paperclip/api/attachments/<id>/content`. Neither host exists.
**Expected behavior**
An agent's link to a Paperclip board or API route opens. Genuine
external links, the instance's own origin, and links inside code stay as
written.
**Steps to reproduce**
Post an agent comment that contains
`[Morgan](https://paperclip.local/<PREFIX>/agents/<id>)`. Before this
change it is stored as written. After it, the stored body links
`/<PREFIX>/agents/<id>`.
## What Changed
- New `server/src/services/agent-invented-host-links.ts`. It rewrites an
absolute link to a relative route only when all of these are true:
- The host is made up: `paperclip`, or `paperclip.<reserved TLD>`
(`local`, `localhost`, `internal`, `test`, `example`, `invalid`, `lan`),
or a loopback host.
- The path is a known board route (`/<PREFIX>/<board root>…`) or, on a
made-up Paperclip host only, `/api/…`.
- A loopback host (`localhost`, `127.x`, `[::1]`) qualifies only for a
board route under the comment's own company prefix. A local app that the
agent built keeps its link, including its `/api` paths.
- The origin is not this instance's configured public origin.
- The link is outside fenced and inline code.
- Markdown link targets keep their place. Bare links and autolinks
become Markdown links, so they stay clickable.
- `issueService.addComment` applies it to agent-authored comments,
before the redaction and dedupe steps, so a retried identical comment
still deduplicates. The run-presentation check still compares the text
that the agent wrote. People's comments are not changed.
- `skills/paperclip/SKILL.md` now tells agents to write board and
attachment links as relative paths and never add a hostname.
## Verification
- `pnpm vitest run
server/src/services/agent-invented-host-links.test.ts` (19 tests):
recorded cohort 16 links are rewritten. Negative controls stay
unchanged: external links, a production Paperclip host, the instance's
own origin, local app links, another prefix on localhost, a non-route
path, another single-label host, inline code, a fence, and relative
links.
- `server/src/__tests__/issues-service.test.ts` new case: an agent
comment is stored with the relative route, and a person's identical
comment is stored as written. It fails on master and passes with this
change.
- Per-message cost (local tsx micro-benchmark): the gate on a comment
with no candidate host takes 0.3 µs (500 chars), 1.2 µs (2 KB) and 4.7
µs (8 KB). Only when a candidate exists does it read the company prefix
(one indexed select) and the public origin (config read once per
process), and rewrite (4–40 µs). Nothing runs before a turn starts.
- `server` typecheck passes.
## Risks
- A link on a made-up host to a board route of another company prefix
becomes relative. It opens that route on the reader's instance, where
access checks still apply.
- External channels that cannot resolve relative links already get
relative links from agents today. This change does not alter their
delivery.
## Model Used
- Claude, `claude-opus-5-5` (Claude Code agent 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
- [x] 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: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
5.4 KiB
TypeScript
75 lines
5.4 KiB
TypeScript
import { readFileSync } from "node:fs";
|
|
import { describe, expect, it } from "vitest";
|
|
import { BOARD_ROUTE_ROOTS, mayContainInventedHostLink, relativizeInventedHostLinks, type InventedHostLinkContext } from "./agent-invented-host-links.js";
|
|
|
|
const context: InventedHostLinkContext = { companyPrefix: "COM", ownOrigin: "https://board.example.com" };
|
|
const rewrite = (text: string, overrides: Partial<InventedHostLinkContext> = {}) =>
|
|
relativizeInventedHostLinks(text, { ...context, ...overrides });
|
|
|
|
describe("invented-host Paperclip links", () => {
|
|
it("rewrites the recorded cohort 16 reply link to a relative route", () => {
|
|
// Recorded from chat-eval cohort 16 (agents.edit-instructions).
|
|
const reply = "Done — I added the rule to [Morgan](https://paperclip.local/COM/agents/1fffd0bf-177a-4f4f-bb58-008160e407d5)'s persistent instructions in AGENTS.md.";
|
|
expect(rewrite(reply)).toBe("Done — I added the rule to [Morgan](/COM/agents/1fffd0bf-177a-4f4f-bb58-008160e407d5)'s persistent instructions in AGENTS.md.");
|
|
});
|
|
|
|
it("rewrites the recorded cohort 16 attachment link on host paperclip", () => {
|
|
// Recorded from chat-eval cohort 16 (onboarding.first-work worker comment).
|
|
expect(rewrite("[index.html](https://paperclip/api/attachments/29ddbfd9-3dfc-4798-87d4-84fb99e68764/content)"))
|
|
.toBe("[index.html](/api/attachments/29ddbfd9-3dfc-4798-87d4-84fb99e68764/content)");
|
|
});
|
|
|
|
it.each([
|
|
["a bare link with sentence punctuation", "See https://paperclip.local/COM/issues/COM-2.", "See [/COM/issues/COM-2](/COM/issues/COM-2)."],
|
|
["an autolink", "Open <https://paperclip/COM/approvals/a1>", "Open [/COM/approvals/a1](/COM/approvals/a1)"],
|
|
["a query and hash", "[plan](http://paperclip.internal:3100/COM/issues/COM-2#document-plan)", "[plan](/COM/issues/COM-2#document-plan)"],
|
|
["a localhost board link under this company's prefix", "[COM-2](http://localhost:3100/COM/issues/COM-2)", "[COM-2](/COM/issues/COM-2)"],
|
|
["an angle-bracket link destination", "[Morgan](<https://paperclip.local/COM/agents/morgan>)", "[Morgan](</COM/agents/morgan>)"],
|
|
["a reference definition", "See [Morgan].\n\n[Morgan]: https://paperclip.local/COM/agents/morgan", "See [Morgan].\n\n[Morgan]: /COM/agents/morgan"],
|
|
["an angle-bracket reference definition", "[Morgan]: <https://paperclip/COM/agents/morgan>", "[Morgan]: </COM/agents/morgan>"],
|
|
["an IPv6 loopback link", "[COM-2](http://[::1]:3100/COM/issues/COM-2)", "[COM-2](/COM/issues/COM-2)"],
|
|
["a profile page", "[Morgan](https://paperclip.local/COM/u/morgan)", "[Morgan](/COM/u/morgan)"],
|
|
["a link after quoted prose", "> Morgan said so.\n\nSee [COM-2](https://paperclip/COM/issues/COM-2).", "> Morgan said so.\n\nSee [COM-2](/COM/issues/COM-2)."],
|
|
["a loopback link under this company's prefix in lower case", "[case](http://127.0.0.1:3100/com/cases/COM-C1)", "[case](/com/cases/COM-C1)"],
|
|
])("rewrites %s", (_label, text, expected) => {
|
|
expect(rewrite(text)).toBe(expected);
|
|
});
|
|
|
|
it.each([
|
|
["a genuine external link", "[docs](https://docs.github.com/COM/issues/1)"],
|
|
["a production Paperclip host", "[COM-2](https://paperclip.staging.paperclip.app/COM/issues/COM-2)"],
|
|
["this instance's own origin", "[COM-2](https://board.example.com/COM/issues/COM-2)"],
|
|
["a local app the agent built", "Open http://localhost:5173/ and call http://localhost:3000/api/hello."],
|
|
["a localhost route under another prefix", "[x](http://localhost:3100/V1/projects)"],
|
|
["a non-route path on an invented host", "[x](https://paperclip.local/about)"],
|
|
["a single-label host other than paperclip", "[build](http://jenkins/api/json)"],
|
|
["a link in inline code", "Run `curl https://paperclip/api/issues/COM-1` to check."],
|
|
["a link in a fence", "```sh\ncurl https://paperclip.local/COM/issues/COM-1\n```"],
|
|
["a link in a fence inside a block quote", "> ```sh\n> curl https://paperclip/api/issues/COM-1\n> ```"],
|
|
["a link in a fence inside a list item", "1. Run:\n ```sh\n curl https://paperclip/api/issues/COM-1\n ```"],
|
|
["a link in an indented code block", "Run this:\n\n curl https://paperclip/api/issues/COM-1\n echo done"],
|
|
["a link in inline code across lines", "Run `curl\nhttps://paperclip/api/issues/COM-1` to check."],
|
|
["a link in double-backtick code", "Run ``curl https://paperclip/api/issues/COM-1`` now."],
|
|
["a relative link", "[COM-2](/COM/issues/COM-2)"],
|
|
])("leaves %s alone", (_label, text) => {
|
|
expect(rewrite(text)).toBe(text);
|
|
});
|
|
|
|
it("knows every board route root the app serves", () => {
|
|
const source = readFileSync(new URL("../../../ui/src/lib/company-routes.ts", import.meta.url), "utf8");
|
|
const block = /const BOARD_ROUTE_ROOTS = new Set\(\[([\s\S]*?)\]\)/.exec(source)?.[1] ?? "";
|
|
const roots = [...block.matchAll(/"([^"]+)"/g)].map(match => match[1]!);
|
|
expect(roots.length).toBeGreaterThan(10);
|
|
expect(roots.filter(root => !BOARD_ROUTE_ROOTS.has(root))).toEqual([]);
|
|
});
|
|
|
|
it("keeps a localhost link when the company prefix is unknown", () => {
|
|
expect(rewrite("[COM-2](http://localhost:3100/COM/issues/COM-2)", { companyPrefix: null })).toBe("[COM-2](http://localhost:3100/COM/issues/COM-2)");
|
|
});
|
|
|
|
it("skips ordinary prose without parsing", () => {
|
|
expect(mayContainInventedHostLink("Saved [COM-2](/COM/issues/COM-2) and https://github.com/x/y.")).toBe(false);
|
|
expect(mayContainInventedHostLink("see https://paperclip/api/x")).toBe(true);
|
|
});
|
|
});
|