Files
PaperClipAI/ui/src/lib/company-routes.ts
T
DottaandClaude Opus 5.5 68b7852cd1 fix(ui): keep another company's prefix on task links in markdown (#21026)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Chat replies, comments and documents render Markdown through
`MarkdownBody`. Task links in that Markdown become company-aware links
> - `parseIssueReferenceFromHref` turned every task link into an
unprefixed `/issues/<id>` path. The router then added the viewer's
active company prefix
> - In chat-eval cohort 4 (`access.other-company`), a reply in company
COM linked `/BIR/issues/BIR-1`. The chat rendered it as
`/COM/issues/BIR-1`, which points into the wrong company
> - This pull request keeps the company that a task link names: an
explicit `/BIR/issues/...` prefix, or an identifier whose prefix belongs
to a known company
> - The benefit is that a link to another company's task opens that
company's task, not a same-numbered task or an error in the current
company

## Linked Issues or Issue Description

**What happened?**
A chat reply in company COM contained `[BIR-1](/BIR/issues/BIR-1)`. The
rendered link pointed to `/COM/issues/BIR-1`.

**Expected behavior**
A task link that names a company keeps that company: `/BIR/issues/BIR-1`
stays `/BIR/issues/BIR-1`. A bare `BIR-1` or `/issues/BIR-1` goes to BIR
when BIR is a company that the viewer knows.

**Steps to reproduce**
1. Have two companies, COM and BIR.
2. In a COM chat or comment, render the Markdown
`[BIR-1](/BIR/issues/BIR-1)`.
3. Inspect the link. Its `href` is `/COM/issues/BIR-1`.

**Paperclip version or commit**
master at f1aef0cb58.

## What Changed

- `ui/src/lib/issue-reference.ts`: a new `issueHref` builds the internal
link. It keeps the company prefix of an explicit `/PREFIX/issues/...`
path. When the path has no prefix, it uses the identifier's prefix if
that prefix is in the known company prefixes. Otherwise the link stays
`/issues/<id>` and resolves in the active company, as before.
- `ui/src/components/MarkdownBody.tsx`: passes the known company
prefixes to the explicit-link path too (the auto-link path already had
them).
- Tests: a red/green unit test with the recorded shape in
`issue-reference.test.ts`, and a rendered test in
`MarkdownBody.test.tsx` (COM and BIR known; explicit, bare and
unprefixed BIR links all render `/BIR/issues/BIR-1`). Existing
expectations that asserted the stripped `/issues/<id>` form now assert
the company-prefixed form.

## Verification

- `cd ui && npx vitest run src/components/MarkdownBody.test.tsx
src/lib/issue-reference.test.ts src/lib/company-routes.test.ts`: 105
tests pass.
- Red/green: with `issue-reference.ts` and `MarkdownBody.tsx` from
`origin/master`, the new tests fail (`/issues/BIR-1` instead of
`/BIR/issues/BIR-1`). With the change, they pass.
- `pnpm check:token-gates` is clean. `tsc -p ui/tsconfig.json` reports
only existing errors in
`ui/storybook/support/feature-map/FeatureOverview.tsx`, which this
change does not touch.
- A paid chat-eval run with this change is linked in a comment below.

## Risks

- Same-company links now carry the explicit prefix (`/COM/issues/COM-1`
instead of `/issues/COM-1` resolved to the same place). The route is the
same.
- An explicit link with a bare identifier as its target (`[x](JIRA-2)`)
is now gated by known company prefixes like auto-linked text. Such a
link to a non-Paperclip key stays an ordinary link.
- Low risk otherwise: no API, data or saved state changes.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code
execution in Claude Code.

## 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>
2026-11-11 23:51:54 -06:00

126 lines
3.8 KiB
TypeScript

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",
"artifacts",
"u",
"design-guide",
"search",
"settings",
"timeline",
]);
const GLOBAL_ROUTE_ROOTS = new Set(["auth", "invite", "board-claim", "cli-auth", "mcp-connect", "dot-connect", "assistant-connections", "docs", "instance"]);
export function normalizeCompanyPrefix(prefix: string): string {
return prefix.trim().toUpperCase();
}
function splitPath(path: string): { pathname: string; search: string; hash: string } {
const match = path.match(/^([^?#]*)(\?[^#]*)?(#.*)?$/);
return {
pathname: match?.[1] ?? path,
search: match?.[2] ?? "",
hash: match?.[3] ?? "",
};
}
function getRootSegment(pathname: string): string | null {
const segment = pathname.split("/").filter(Boolean)[0];
return segment ?? null;
}
export function isGlobalPath(pathname: string): boolean {
if (pathname === "/") return true;
const root = getRootSegment(pathname);
if (!root) return true;
return GLOBAL_ROUTE_ROOTS.has(root.toLowerCase());
}
export function isBoardPathWithoutPrefix(pathname: string): boolean {
const root = getRootSegment(pathname);
if (!root) return false;
return BOARD_ROUTE_ROOTS.has(root.toLowerCase());
}
export function extractCompanyPrefixFromPath(pathname: string): string | null {
const segments = pathname.split("/").filter(Boolean);
if (segments.length === 0) return null;
const first = segments[0]!.toLowerCase();
if (GLOBAL_ROUTE_ROOTS.has(first)) return null;
const companyTaskPath = first !== "issues" && segments[1]?.toLowerCase() === "issues";
if (BOARD_ROUTE_ROOTS.has(first) && !companyTaskPath) return null;
return normalizeCompanyPrefix(segments[0]!);
}
export function applyCompanyPrefix(path: string, companyPrefix: string | null | undefined): string {
const { pathname, search, hash } = splitPath(path);
if (!pathname.startsWith("/")) return path;
if (isGlobalPath(pathname)) return path;
if (!companyPrefix) return path;
const prefix = normalizeCompanyPrefix(companyPrefix);
const activePrefix = extractCompanyPrefixFromPath(pathname);
if (activePrefix) return path;
return `/${prefix}${pathname}${search}${hash}`;
}
/**
* Build a company-prefixed href for an experimental Cases route, e.g.
* `caseHref("PAP", "PAP-C5")` → `/PAP/cases/PAP-C5`.
*
* Case paths carry identifiers like `PAP-C5` in the first segment, which the
* generic {@link applyCompanyPrefix} mistakes for a company prefix ("CASES") and
* therefore leaves `/cases/...` unprefixed — every case link then only resolves
* via the PAP-13002 unprefixed→prefixed redirect. This builder emits the
* prefixed href directly so case-to-case navigation matches the rest of the app.
* Falls back to the unprefixed path (still valid via the redirect) when no
* company is active.
*/
export function caseHref(
companyPrefix: string | null | undefined,
...segments: string[]
): string {
const suffix = ["cases", ...segments].filter(Boolean).join("/");
if (!companyPrefix) return `/${suffix}`;
return `/${normalizeCompanyPrefix(companyPrefix)}/${suffix}`;
}
export function toCompanyRelativePath(path: string): string {
const { pathname, search, hash } = splitPath(path);
const segments = pathname.split("/").filter(Boolean);
if (segments.length >= 2) {
const second = segments[1]!.toLowerCase();
if (!GLOBAL_ROUTE_ROOTS.has(segments[0]!.toLowerCase()) && BOARD_ROUTE_ROOTS.has(second)) {
return `/${segments.slice(1).join("/")}${search}${hash}`;
}
}
return `${pathname}${search}${hash}`;
}