Files
PaperClipAI/ui/src/lib/remark-workspace-file-refs.test.ts
T
DottaandPaperclip 7ea2068ef8 fix(files): only highlight accessible workspace file links (#11090)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Task comments can contain references to files in project and
execution workspaces.
> - Paperclip detected path-shaped inline code and showed it as an
actionable file chip.
> - The UI did not first confirm that the current board session could
open the file.
> - Missing, denied, ambiguous, remote, and unsupported files therefore
looked actionable and failed after a click.
> - This pull request adds an issue-scoped availability check and
promotes only confirmed files to chips.
> - The benefit is that the task thread shows a file action only when
that action can succeed.

## Linked Issues or Issue Description

**What happened?**

Task comments promoted path-shaped inline code to file chips before
Paperclip checked the file. A chip could point to a missing, denied,
ambiguous, remote, or non-previewable file. The action then failed after
the user selected it.

**Expected behavior**

Paperclip must show a file chip only after the server confirms that the
current board session can open the exact file reference. All other
path-shaped text must stay ordinary inline code.

**Steps to reproduce**

1. Add a task comment that contains inline code with a missing or
inaccessible workspace path.
2. Open the task thread as a board user.
3. Observe that the path looks like an actionable file chip.
4. Select the chip and observe that the file cannot open.

**Paperclip version or commit**

`19be4cf927` and earlier.

**Deployment mode**

Local dev and self-hosted server.

**Access context**

Board user.

## What Changed

- Added shared request, response, and validation contracts for batched
workspace-file availability checks.
- Added an issue-scoped server endpoint that resolves file references
with company, issue, workspace, and preview-access checks.
- Added bounded batch concurrency and tests for missing, denied,
ambiguous, remote, unsupported, and available files.
- Added an issue-scoped UI availability registry that deduplicates,
batches, caches, and invalidates file checks.
- Changed task-comment markdown rendering so only confirmed files get
chip styling and file-viewer behavior.
- Bound each chip to the exact workspace target that passed the
availability check.

## Verification

- `pnpm exec vitest run
packages/shared/src/workspace-file-resource.test.ts
server/src/__tests__/file-resources.test.ts
ui/src/components/MarkdownBody.test.tsx
ui/src/components/WorkspaceFileMarkdownBody.availability.test.tsx
ui/src/lib/remark-workspace-file-refs.test.ts
ui/src/lib/workspace-file-availability.test.ts` — 93 passed, 35 skipped.
- `pnpm check:token-gates` — clean.
- `pnpm -r typecheck` — passed.
- `pnpm build` — passed.
- `pnpm test:run` — all server and UI groups passed. One unchanged CLI
test saw the run-injected static AWS credentials and expected only its
local `AWS_PROFILE`. The same test passed, 8 of 8, after removing only
`AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` from its process
environment.

## Risks

- File chips now appear after an asynchronous availability check, so
path-shaped text can briefly render as inline code.
- Availability results use the existing 30-second file-resource cache
window. File-resource invalidation forces a new check.
- The endpoint limits each request to 100 references and the client
chunks larger sets.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- OpenAI Codex with GPT-5. The service did not expose a more specific
model ID or context-window size. The agent used high-reasoning mode,
repository tools, command execution, and test 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
- [x] 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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-11 12:11:45 -04:00

274 lines
9.1 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
buildWorkspaceFileHref,
createRemarkWorkspaceFileRefs,
parseWorkspaceFileHref,
type WorkspaceFileRefResolver,
} from "./remark-workspace-file-refs";
import type { WorkspaceFileAvailabilityTarget } from "./workspace-file-availability";
type MarkdownNode = {
type: string;
value?: string;
url?: string;
children?: MarkdownNode[];
};
const AUTO_TARGET: WorkspaceFileAvailabilityTarget = {
workspace: "auto",
projectId: null,
workspaceId: null,
projectName: null,
};
/** Resolver standing in for "the server confirmed every reference is openable". */
const resolveAllOpenable: WorkspaceFileRefResolver = () => AUTO_TARGET;
/** Fail-closed resolver: nothing is openable. */
const resolveNoneOpenable: WorkspaceFileRefResolver = () => null;
function textNode(value: string): MarkdownNode {
return { type: "text", value };
}
function inlineCode(value: string): MarkdownNode {
return { type: "inlineCode", value };
}
function paragraph(children: MarkdownNode[]): MarkdownNode {
return { type: "paragraph", children };
}
function runPlugin(tree: MarkdownNode, resolve: WorkspaceFileRefResolver = resolveAllOpenable): MarkdownNode {
const transform = createRemarkWorkspaceFileRefs(resolve)();
transform(tree);
return tree;
}
describe("remarkWorkspaceFileRefs", () => {
it("converts a matching inline code span into a workspace-file link", () => {
const tree = paragraph([
textNode("Check "),
inlineCode("ui/src/pages/IssueDetail.tsx:42"),
textNode(" please."),
]);
runPlugin(tree);
expect(tree.children).toHaveLength(3);
const link = tree.children![1];
expect(link.type).toBe("link");
expect(link.url?.startsWith("workspace-file:")).toBe(true);
const parsed = parseWorkspaceFileHref(link.url);
expect(parsed?.path).toBe("ui/src/pages/IssueDetail.tsx");
expect(parsed?.resourceKind).toBe("file");
expect(parsed?.line).toBe(42);
});
it("does not linkify plain text path mentions outside inline code", () => {
const tree = paragraph([textNode("see ui/src/pages/IssueDetail.tsx for details")]);
runPlugin(tree);
expect(tree.children).toHaveLength(1);
expect(tree.children![0].type).toBe("text");
});
it("does not linkify prose words that look like filenames", () => {
const tree = paragraph([inlineCode("README.md")]);
runPlugin(tree);
expect(tree.children![0].type).toBe("inlineCode");
});
it("round-trips workspace file hrefs", () => {
const href = buildWorkspaceFileHref({ path: "a/b.ts", line: 5, column: 2, raw: "a/b.ts:5:2" });
const parsed = parseWorkspaceFileHref(href);
expect(parsed?.path).toBe("a/b.ts");
expect(parsed?.line).toBe(5);
expect(parsed?.column).toBe(2);
});
it("round-trips workspace folder hrefs", () => {
const targetPath = "content-os/cases/active/2026-06-06-pap-10199-bundled-skills/";
const href = buildWorkspaceFileHref({
path: targetPath,
resourceKind: "directory",
line: null,
column: null,
raw: targetPath,
projectId: "17acae7d-9d0c-46bf-9c82-be9694ac3461",
workspaceId: "0de5f74f-a7d4-4f73-a9a0-455a2b968cf2",
});
const parsed = parseWorkspaceFileHref(href);
expect(parsed).toMatchObject({
path: targetPath,
resourceKind: "directory",
line: null,
column: null,
projectId: "17acae7d-9d0c-46bf-9c82-be9694ac3461",
workspaceId: "0de5f74f-a7d4-4f73-a9a0-455a2b968cf2",
});
});
it("converts trailing-slash inline code into workspace folder links", () => {
const tree = paragraph([
textNode("Open "),
inlineCode("content-os/cases/active/2026-06-06-pap-10199-bundled-skills/"),
textNode("."),
]);
runPlugin(tree);
const link = tree.children![1];
expect(link.type).toBe("link");
expect(parseWorkspaceFileHref(link.url)).toMatchObject({
path: "content-os/cases/active/2026-06-06-pap-10199-bundled-skills/",
resourceKind: "directory",
});
});
it("round-trips explicit project workspace identity", () => {
const targetPath = "content-os/cases/active/2026-06-06-pap-10199-bundled-skills/README.md";
const href = buildWorkspaceFileHref({
path: targetPath,
line: 5,
column: null,
raw: `${targetPath}:5`,
projectId: "17acae7d-9d0c-46bf-9c82-be9694ac3461",
workspaceId: "0de5f74f-a7d4-4f73-a9a0-455a2b968cf2",
projectName: "Paperclip Content",
});
const parsed = parseWorkspaceFileHref(href);
expect(parsed).toMatchObject({
path: targetPath,
line: 5,
column: null,
projectId: "17acae7d-9d0c-46bf-9c82-be9694ac3461",
workspaceId: "0de5f74f-a7d4-4f73-a9a0-455a2b968cf2",
projectName: "Paperclip Content",
});
});
it("converts linked inline-code file paths into workspace-file links", () => {
const tree: MarkdownNode = {
type: "paragraph",
children: [
{
type: "link",
url: "/PAP/issues/PAP-10306",
children: [inlineCode("ui/src/a.ts:1")],
},
],
};
runPlugin(tree);
const link = tree.children![0];
expect(link.type).toBe("link");
expect(link.url?.startsWith("workspace-file:")).toBe(true);
expect(parseWorkspaceFileHref(link.url)?.path).toBe("ui/src/a.ts");
});
it("does not descend into existing links with mixed labels", () => {
const tree: MarkdownNode = {
type: "paragraph",
children: [
{
type: "link",
url: "https://example.com",
children: [textNode("see "), inlineCode("ui/src/a.ts:1")],
},
],
};
runPlugin(tree);
const link = tree.children![0];
expect(link.url).toBe("https://example.com");
expect(link.children![1]?.type).toBe("inlineCode");
});
describe("availability gating", () => {
it("leaves unresolved inline code as plain code", () => {
const tree = paragraph([
textNode("Check "),
inlineCode("ui/src/pages/IssueDetail.tsx:42"),
textNode(" please."),
]);
runPlugin(tree, resolveNoneOpenable);
expect(tree.children).toHaveLength(3);
expect(tree.children![1]).toEqual(inlineCode("ui/src/pages/IssueDetail.tsx:42"));
});
it("leaves an ordinary markdown link untouched when the ref is not openable", () => {
const tree: MarkdownNode = {
type: "paragraph",
children: [
{
type: "link",
url: "/PAP/issues/PAP-10306",
children: [inlineCode("ui/src/a.ts:1")],
},
],
};
runPlugin(tree, resolveNoneOpenable);
expect(tree.children![0].url).toBe("/PAP/issues/PAP-10306");
expect(tree.children![0].children![0]?.type).toBe("inlineCode");
});
it("promotes only the references the resolver confirms", () => {
const tree = paragraph([
inlineCode("ui/src/present.ts:1"),
textNode(" and "),
inlineCode("ui/src/missing.ts:2"),
]);
runPlugin(tree, (ref) => (ref.path === "ui/src/present.ts" ? AUTO_TARGET : null));
expect(tree.children![0].type).toBe("link");
expect(tree.children![2].type).toBe("inlineCode");
});
it("binds the resolved workspace target to the generated viewer href", () => {
const tree = paragraph([inlineCode("ui/src/a.ts:7")]);
runPlugin(tree, () => ({
workspace: "project",
projectId: "17acae7d-9d0c-46bf-9c82-be9694ac3461",
workspaceId: "0de5f74f-a7d4-4f73-a9a0-455a2b968cf2",
projectName: "Paperclip Content",
}));
expect(parseWorkspaceFileHref(tree.children![0].url)).toMatchObject({
path: "ui/src/a.ts",
line: 7,
workspace: "project",
projectId: "17acae7d-9d0c-46bf-9c82-be9694ac3461",
workspaceId: "0de5f74f-a7d4-4f73-a9a0-455a2b968cf2",
});
});
it("binds an execution-workspace target by selector without ids", () => {
const tree = paragraph([inlineCode("ui/src/a.ts:7")]);
runPlugin(tree, () => ({
workspace: "execution",
projectId: null,
workspaceId: null,
projectName: null,
}));
const parsed = parseWorkspaceFileHref(tree.children![0].url);
expect(parsed?.workspace).toBe("execution");
expect(parsed?.projectId).toBeNull();
expect(parsed?.workspaceId).toBeNull();
});
it("asks the resolver once per candidate reference and never for non-paths", () => {
const resolve = vi.fn(resolveAllOpenable);
const tree = paragraph([
inlineCode("ui/src/a.ts:1"),
textNode(" then "),
inlineCode("pnpm test"),
]);
runPlugin(tree, resolve);
expect(resolve).toHaveBeenCalledTimes(1);
expect(resolve.mock.calls[0]![0]).toMatchObject({ path: "ui/src/a.ts" });
});
it("does not consult the resolver inside fenced code blocks", () => {
const resolve = vi.fn(resolveAllOpenable);
const tree: MarkdownNode = {
type: "root",
children: [{ type: "code", value: "ui/src/a.ts:1", children: [inlineCode("ui/src/a.ts:1")] }],
};
runPlugin(tree, resolve);
expect(resolve).not.toHaveBeenCalled();
});
});
});