mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 09:50:48 +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>
185 lines
7.9 KiB
TypeScript
185 lines
7.9 KiB
TypeScript
/**
|
|
* Agents sometimes write a Paperclip link with a hostname they made up, such
|
|
* as https://paperclip.local/COM/agents/<id> or https://paperclip/api/...
|
|
* Neither host exists, so the link does not open. When the path is a known
|
|
* board or API route, the relative form opens on whatever origin the reader
|
|
* already uses. Every other link, and anything inside code, is left alone.
|
|
*/
|
|
import { loadConfig } from "../config.js";
|
|
|
|
// Board route roots after the company prefix: every root in
|
|
// ui/src/lib/company-routes.ts (a test keeps them aligned), plus the case and
|
|
// status pages agents are told to link.
|
|
export const BOARD_ROUTE_ROOTS = new Set([
|
|
"dashboard", "companies", "company", "skills", "teams-catalog", "org", "agents", "chats", "apps",
|
|
"projects", "workspaces", "execution-workspaces", "issues", "tasks", "routines", "goals", "artifacts",
|
|
"tools", "approvals", "costs", "usage", "activity", "audit", "decisions", "inbox", "board-chat", "u",
|
|
"design-guide", "search", "settings", "timeline", "cases", "status", "status-cards",
|
|
]);
|
|
|
|
const URL_PATTERN = /https?:\/\/(?:\[[0-9A-Fa-f:.]+\](?::\d+)?|[^\s/?#()<>[\]"'`]+)[^\s()<>[\]"'`]*/gi;
|
|
// A cheap test before any parsing: the text links one of the hosts below.
|
|
const CANDIDATE_PATTERN = /https?:\/\/(?:[^\s/@]*@)?(?:[a-z0-9-]+\.)*(?:paperclip(?:[.:/]|\s|$)|localhost|127\.|0\.0\.0\.0|\[::1\])/i;
|
|
const RESERVED_TLDS = new Set(["local", "localhost", "internal", "test", "example", "invalid", "lan"]);
|
|
|
|
export type InventedHostLinkContext = {
|
|
/** This company's issue prefix, such as COM. */
|
|
companyPrefix: string | null;
|
|
/** This instance's own public origin; its links are never rewritten. */
|
|
ownOrigin: string | null;
|
|
};
|
|
|
|
/** A host that only ever names Paperclip and resolves nowhere public. */
|
|
function isInventedPaperclipHost(hostname: string): boolean {
|
|
if (hostname === "paperclip") return true;
|
|
const labels = hostname.split(".");
|
|
return labels.length >= 2 && labels.at(-2) === "paperclip" && RESERVED_TLDS.has(labels.at(-1)!);
|
|
}
|
|
|
|
function isLoopbackHost(hostname: string): boolean {
|
|
return hostname === "localhost" || hostname.endsWith(".localhost") || hostname === "[::1]"
|
|
|| hostname === "0.0.0.0" || /^127(?:\.\d{1,3}){3}$/.test(hostname);
|
|
}
|
|
|
|
export function mayContainInventedHostLink(text: string): boolean {
|
|
return text.includes("://") && CANDIDATE_PATTERN.test(text);
|
|
}
|
|
|
|
/**
|
|
* The relative app route for an absolute link on a made-up Paperclip host, or
|
|
* null to leave the link alone. A loopback host can be a real local app the
|
|
* agent built, so it qualifies only for a board route under this company's
|
|
* own prefix, never for an /api path.
|
|
*/
|
|
function relativeRoute(raw: string, context: InventedHostLinkContext): string | null {
|
|
let url: URL;
|
|
try {
|
|
url = new URL(raw);
|
|
} catch {
|
|
return null;
|
|
}
|
|
const hostname = url.hostname.toLowerCase();
|
|
const invented = isInventedPaperclipHost(hostname);
|
|
if (!invented && !isLoopbackHost(hostname)) return null;
|
|
if (context.ownOrigin && url.origin === context.ownOrigin) return null;
|
|
const [, prefix = "", rootSegment = ""] = url.pathname.split("/");
|
|
const route = `${url.pathname}${url.search}${url.hash}`;
|
|
if (prefix === "api") return invented && rootSegment ? route : null;
|
|
if (!/^[A-Za-z][A-Za-z0-9]{0,15}$/.test(prefix) || !BOARD_ROUTE_ROOTS.has(rootSegment.toLowerCase())) return null;
|
|
if (invented) return route;
|
|
return context.companyPrefix && prefix.toUpperCase() === context.companyPrefix.toUpperCase() ? route : null;
|
|
}
|
|
|
|
// A Markdown reference definition up to its destination: `[id]: <`.
|
|
const REFERENCE_DEFINITION = /(?:^|\n) {0,3}\[[^\]\n]+\]:[ \t]*<?$/;
|
|
|
|
/**
|
|
* The text with every Markdown code region blanked, keeping offsets: fenced
|
|
* blocks (also inside block quotes or list items), indented code blocks, and
|
|
* inline code spans, which may cross lines within a paragraph.
|
|
*/
|
|
function maskCode(text: string): string {
|
|
const lines = text.split("\n");
|
|
let fence: { char: string; length: number } | null = null;
|
|
let previousBlank = true;
|
|
let inIndentedBlock = false;
|
|
const masked = lines.map(line => {
|
|
const body = line.replace(/^(?:[ \t]*>)+[ \t]?/, "");
|
|
const marker = /^[ \t]*(`{3,}|~{3,})(.*)$/.exec(body);
|
|
const blank = !body.trim();
|
|
let code = false;
|
|
if (fence) {
|
|
code = true;
|
|
if (marker && marker[1]![0] === fence.char && marker[1]!.length >= fence.length && !marker[2]!.trim()) fence = null;
|
|
} else if (marker && !(marker[1]![0] === "`" && marker[2]!.includes("`"))) {
|
|
fence = { char: marker[1]![0]!, length: marker[1]!.length };
|
|
code = true;
|
|
} else if (!blank && /^(?: {4}|\t)/.test(body) && (previousBlank || inIndentedBlock)) {
|
|
code = true;
|
|
}
|
|
inIndentedBlock = code && !fence && !marker ? true : blank ? inIndentedBlock : false;
|
|
previousBlank = blank;
|
|
return code ? " ".repeat(line.length) : line;
|
|
}).join("\n");
|
|
// Inline spans: a backtick run closes at the next run of the same length in
|
|
// its paragraph; an escaped backtick opens nothing.
|
|
const chars = masked.split("");
|
|
for (let index = 0; index < masked.length; index += 1) {
|
|
if (masked[index] === "\\") {
|
|
index += 1;
|
|
continue;
|
|
}
|
|
if (masked[index] !== "`") continue;
|
|
let length = 1;
|
|
while (masked[index + length] === "`") length += 1;
|
|
const paragraphEnd = masked.indexOf("\n\n", index);
|
|
const limit = paragraphEnd < 0 ? masked.length : paragraphEnd;
|
|
let close = -1;
|
|
for (let at = index + length; at < limit; at += 1) {
|
|
if (masked[at] !== "`") continue;
|
|
let run = 1;
|
|
while (masked[at + run] === "`") run += 1;
|
|
if (run === length) {
|
|
close = at;
|
|
break;
|
|
}
|
|
at += run - 1;
|
|
}
|
|
if (close < 0) {
|
|
index += length - 1;
|
|
continue;
|
|
}
|
|
for (let at = index; at < close + length; at += 1) if (chars[at] !== "\n") chars[at] = " ";
|
|
index = close + length - 1;
|
|
}
|
|
return chars.join("");
|
|
}
|
|
|
|
/** Rewrite made-up-host Paperclip links outside Markdown code. */
|
|
export function relativizeInventedHostLinks(text: string, context: InventedHostLinkContext): string {
|
|
if (!mayContainInventedHostLink(text)) return text;
|
|
const masked = maskCode(text);
|
|
let result = "";
|
|
let cursor = 0;
|
|
for (const match of masked.matchAll(URL_PATTERN)) {
|
|
const offset = match.index!;
|
|
// Sentence punctuation after a bare link is not part of it.
|
|
const raw = match[0].replace(/[.,;:!?]+$/, "");
|
|
const route = relativeRoute(raw, context);
|
|
if (!route) continue;
|
|
const before = text.slice(Math.max(0, offset - 3), offset);
|
|
const end = offset + raw.length;
|
|
// A link destination (inline, angle-bracketed or a reference definition),
|
|
// link text, or an attribute value keeps its place. An autolink or a bare
|
|
// link becomes a Markdown link so it stays clickable.
|
|
const autolink = before.endsWith("<") && text[end] === ">" && !before.endsWith("](<") && !REFERENCE_DEFINITION.test(text.slice(0, offset));
|
|
const inPlace = !autolink && (before.endsWith("](") || before.endsWith("](<") || before.endsWith("[")
|
|
|| /["'=]$/.test(before) || REFERENCE_DEFINITION.test(text.slice(Math.max(0, offset - 300), offset)));
|
|
if (autolink) {
|
|
result += `${text.slice(cursor, offset - 1)}[${route}](${route})`;
|
|
cursor = end + 1;
|
|
} else {
|
|
result += `${text.slice(cursor, offset)}${inPlace ? route : `[${route}](${route})`}`;
|
|
cursor = end;
|
|
}
|
|
}
|
|
return cursor === 0 ? text : result + text.slice(cursor);
|
|
}
|
|
|
|
let cachedOwnOrigin: string | null | undefined;
|
|
|
|
/**
|
|
* This instance's configured public origin. Read once per process, and only
|
|
* when a candidate link appears, because loading the config reads its file.
|
|
*/
|
|
export function instancePublicOrigin(): string | null {
|
|
if (cachedOwnOrigin !== undefined) return cachedOwnOrigin;
|
|
try {
|
|
const base = loadConfig().authPublicBaseUrl;
|
|
cachedOwnOrigin = base ? new URL(base).origin : null;
|
|
} catch {
|
|
cachedOwnOrigin = null;
|
|
}
|
|
return cachedOwnOrigin;
|
|
}
|