mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-01 15:35:12 +01:00
<!-- entire-trail-link-start -->
https://entire.com/gh/paperclipai/-/trails/11/changes/paperclip/11
<!-- entire-trail-link-end -->
## Thinking Path
> - Paperclip is the open source app that people use to manage AI agents
for work.
> - Paperclip shows the status of external objects, such as GitHub pull
requests and issues.
> - A background sweep refreshes these objects and resolves a GitHub
credential.
> - The sweep read a credential only from a company secret with a
well-known name.
> - An instance without that secret made anonymous GitHub API calls.
Anonymous calls are limited to 60 per hour.
> - When the limit was reached, the sweep showed "unreachable" for
objects that it could not refresh.
> - A server environment token is instance-wide, so it must not expose a
private repository that a company cannot see.
> - This pull request reads a server token for public repositories,
reports a rate limit as its own state, and shows a warning on the pill.
> - The benefit is an accurate status, fewer misleading failures, and no
cross-company private data.
## Linked Issues or Issue Description
No public GitHub issue exists. The sections below describe the bug.
**What happened?**
The board showed "unreachable" for many GitHub links, even after a
related fix was deployed.
**Expected behavior**
The board should show "unreachable" only for real network failures. It
should show a rate limit as its own state and retry later.
**Steps to reproduce**
1. Use an instance with no GitHub token company secret and no server
token.
2. Let the external-object sweep make more than 60 anonymous GitHub
requests in one hour.
3. Open a task with GitHub links.
**Paperclip version or commit**
master (commit a67a18c at report time).
**Deployment mode**
Self-hosted server.
## What Changed
- Add `resolveGitHubApiCredential` in
`server/src/services/git-credentials.ts`. It reads a company secret with
a well-known name, then falls back to the server environment token
(`GITHUB_TOKEN`/`GH_TOKEN`), and it reports the credential `source`.
- Use the source in the GitHub external-object provider and in
`github-commit-details.ts`. A `server_env` token is limited to public
repositories. A private repository resolves only with a company
credential; otherwise the object reports `auth_required` and does not
fetch the private object.
- Add a `rate_limited` liveness state in
`packages/shared/src/constants.ts`.
- Classify a 403 with `x-ratelimit-remaining: 0`, a 429 response, and a
rate-limited repo probe as `rate_limited` instead of `unreachable`.
- Show `rate_limited` in the UI filter, icon, label, tone, overlay, and
badge maps.
- Make `ExternalObjectPill` and `ExternalObjectStatusIcon` prefer a
liveness warning icon and tone, so a rate-limited object shows a warning
even when the last status was healthy.
- Add tests for the credential source, the public-only server token, the
company-scoped private repository, and the rate-limited pill tone.
## Verification
- `vitest run server/src/__tests__/external-objects-service.test.ts
server/src/__tests__/git-credentials.test.ts
server/src/__tests__/github-commit-details.test.ts
server/src/__tests__/work-products.test.ts` — pass, with the new scoping
and credential-source tests.
- `vitest run ui/src/lib/external-objects.test.ts
ui/src/lib/issue-filters.test.ts
ui/src/components/ExternalObjectPill.test.tsx
ui/src/components/MarkdownExternalLink.test.tsx` — pass, with the new
rate-limited pill test.
- `pnpm --filter @paperclipai/shared typecheck` — pass. The full
monorepo `tsc` (server and ui) is killed by the 4GB memory cap of this
sandbox; the authoritative CI Typecheck gate runs it instead.
- One test in `git-credentials.test.ts` fails in this sandbox only. The
test "credential helper execution (real git, no network) ... env-carried
token" reads a global GitHub credential helper in the sandbox. The test
fails the same way on the base without this change.
## Risks
- Low risk. The change adds a liveness state and a credential source. It
does not change existing terminal verdicts.
- A server-environment token cannot resolve a private repository. This
is intentional. An operator who needs private repositories must add a
GitHub token as a company secret. The object then reports
`auth_required` with that instruction.
- `rate_limited` is a new value in `EXTERNAL_OBJECT_LIVENESS_STATES`. An
old client ignores an unknown value.
## Model Used
- Provider: DeepSeek (OpenRouter).
- Exact model ID: `openrouter/deepseek/deepseek-v4.1-flash`.
- Capabilities: tool use and code execution in the Paperclip runner.
## 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 similar open and closed PRs and
confirmed this is not a duplicate
- [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)
- [ ] 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
- [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
- [ ] I will address all Greptile and reviewer comments before
requesting merge
---------
Co-authored-by: devinfoley <139239+devinfoley@users.noreply.github.com>
246 lines
11 KiB
TypeScript
246 lines
11 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import type { ExternalObjectSummary } from "@paperclipai/shared";
|
|
import { externalObjectsApi } from "../api/externalObjects";
|
|
import {
|
|
EXTERNAL_OBJECT_SUMMARY_BATCH_SIZE,
|
|
fetchIssueExternalObjectSummariesInBatches,
|
|
} from "../hooks/useIssueExternalObjects";
|
|
import {
|
|
dominantExternalObjectTone,
|
|
externalObjectCategoryLabel,
|
|
externalObjectDisplayStatusLabel,
|
|
externalObjectDisplayLabel,
|
|
externalObjectDominantCount,
|
|
externalObjectFallbackTone,
|
|
externalObjectIconForCategory,
|
|
externalObjectIconForKey,
|
|
externalObjectIconForLiveness,
|
|
externalObjectLivenessLabel,
|
|
externalObjectProviderLabel,
|
|
externalObjectToneSeverity,
|
|
externalObjectTypeLabel,
|
|
sortExternalObjectsBySeverity,
|
|
} from "./external-objects";
|
|
import { normalizeExternalObjectHref } from "./external-object-href";
|
|
|
|
vi.mock("../api/externalObjects", () => ({
|
|
externalObjectsApi: {
|
|
getIssueSummaries: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
const emptySummary: ExternalObjectSummary = {
|
|
total: 0,
|
|
byStatusCategory: {},
|
|
byLiveness: {},
|
|
highestSeverity: "neutral",
|
|
staleCount: 0,
|
|
authRequiredCount: 0,
|
|
unreachableCount: 0,
|
|
objects: [],
|
|
};
|
|
|
|
describe("normalizeExternalObjectHref", () => {
|
|
it("lowercases the host (case preserving the path) and drops query/fragment", () => {
|
|
expect(
|
|
normalizeExternalObjectHref("HTTPS://Github.com/Acme/Web/pull/241?token=abc#frag"),
|
|
).toBe("https://github.com/Acme/Web/pull/241");
|
|
});
|
|
|
|
it("rejects non-http(s) and userinfo-bearing URLs", () => {
|
|
expect(normalizeExternalObjectHref("javascript:alert(1)")).toBeNull();
|
|
expect(normalizeExternalObjectHref("ftp://example.com/file")).toBeNull();
|
|
expect(normalizeExternalObjectHref("https://user:pass@example.com/")).toBeNull();
|
|
expect(normalizeExternalObjectHref(null)).toBeNull();
|
|
expect(normalizeExternalObjectHref(undefined)).toBeNull();
|
|
expect(normalizeExternalObjectHref("not a url")).toBeNull();
|
|
});
|
|
|
|
it("defaults pathless URLs to /", () => {
|
|
expect(normalizeExternalObjectHref("https://example.com")).toBe("https://example.com/");
|
|
});
|
|
});
|
|
|
|
describe("fetchIssueExternalObjectSummariesInBatches", () => {
|
|
afterEach(() => {
|
|
vi.mocked(externalObjectsApi.getIssueSummaries).mockReset();
|
|
});
|
|
|
|
it("chunks bulk summary requests below the server validation cap and merges results", async () => {
|
|
const issueIds = Array.from(
|
|
{ length: EXTERNAL_OBJECT_SUMMARY_BATCH_SIZE * 2 + 3 },
|
|
(_entry, index) => `issue-${index}`,
|
|
);
|
|
vi.mocked(externalObjectsApi.getIssueSummaries).mockImplementation(async (_companyId, ids) => ({
|
|
summaries: Object.fromEntries(ids.map((id) => [id, { ...emptySummary, total: id.endsWith("-0") ? 1 : 0 }])),
|
|
}));
|
|
|
|
const result = await fetchIssueExternalObjectSummariesInBatches("company-1", issueIds);
|
|
|
|
expect(externalObjectsApi.getIssueSummaries).toHaveBeenCalledTimes(3);
|
|
expect(vi.mocked(externalObjectsApi.getIssueSummaries).mock.calls.map((call) => call[1].length)).toEqual([
|
|
EXTERNAL_OBJECT_SUMMARY_BATCH_SIZE,
|
|
EXTERNAL_OBJECT_SUMMARY_BATCH_SIZE,
|
|
3,
|
|
]);
|
|
expect(Object.keys(result.summaries)).toHaveLength(issueIds.length);
|
|
expect(result.summaries["issue-0"]?.total).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe("external-objects helpers", () => {
|
|
it("labels categories with copy from the UX spec", () => {
|
|
expect(externalObjectCategoryLabel("auth_required")).toBe("Authorization required");
|
|
expect(externalObjectCategoryLabel("succeeded")).toBe("Succeeded");
|
|
expect(externalObjectCategoryLabel("unknown")).toBe("Not yet resolved");
|
|
});
|
|
|
|
it("labels liveness states with non-tech copy", () => {
|
|
expect(externalObjectLivenessLabel("stale")).toBe("Stale");
|
|
expect(externalObjectLivenessLabel("auth_required")).toBe("Requires auth");
|
|
expect(externalObjectLivenessLabel("fresh")).toBe("Fresh");
|
|
});
|
|
|
|
it("falls back to a humanised label for unknown providers and types", () => {
|
|
expect(externalObjectProviderLabel("github")).toBe("GitHub");
|
|
expect(externalObjectProviderLabel("hubspot_marketing")).toBe("Hubspot Marketing");
|
|
expect(externalObjectProviderLabel(null)).toBe("External");
|
|
expect(externalObjectTypeLabel("workflow_run")).toBe("workflow run");
|
|
expect(externalObjectTypeLabel("url_link")).toBe("URL");
|
|
expect(externalObjectTypeLabel(null)).toBe("object");
|
|
});
|
|
|
|
it("labels generic URL link objects as URL", () => {
|
|
expect(externalObjectDisplayLabel("url", "link")).toBe("URL");
|
|
expect(externalObjectDisplayLabel("url", "link", "Canonical URL")).toBe("Canonical URL");
|
|
expect(externalObjectDisplayLabel("github", "pull_request")).toBe("GitHub pull request");
|
|
});
|
|
|
|
it("labels unresolved known objects as not refreshed while preserving generic URL copy", () => {
|
|
expect(
|
|
externalObjectDisplayStatusLabel({
|
|
providerKey: "github",
|
|
objectType: "pull_request",
|
|
statusCategory: "unknown",
|
|
liveness: "unknown",
|
|
}),
|
|
).toBe("Not yet refreshed");
|
|
expect(
|
|
externalObjectDisplayStatusLabel({
|
|
providerKey: "url",
|
|
objectType: "link",
|
|
statusCategory: "unknown",
|
|
liveness: "unknown",
|
|
}),
|
|
).toBe("Not yet resolved");
|
|
expect(
|
|
externalObjectDisplayStatusLabel({
|
|
providerKey: "github",
|
|
objectType: "pull_request",
|
|
statusCategory: "unknown",
|
|
liveness: "fresh",
|
|
}),
|
|
).toBe("Status unavailable");
|
|
expect(
|
|
externalObjectDisplayStatusLabel({
|
|
providerKey: "github",
|
|
objectType: "pull_request",
|
|
statusCategory: "open",
|
|
liveness: "fresh",
|
|
statusLabel: "Open",
|
|
}),
|
|
).toBe("Open");
|
|
});
|
|
|
|
it("orders tones from danger down to muted", () => {
|
|
expect(externalObjectToneSeverity("danger")).toBeGreaterThan(externalObjectToneSeverity("warning"));
|
|
expect(externalObjectToneSeverity("warning")).toBeGreaterThan(externalObjectToneSeverity("info"));
|
|
expect(externalObjectToneSeverity("info")).toBeGreaterThan(externalObjectToneSeverity("success"));
|
|
expect(externalObjectToneSeverity("success")).toBeGreaterThan(externalObjectToneSeverity("muted"));
|
|
expect(externalObjectToneSeverity(null)).toBe(0);
|
|
expect(externalObjectToneSeverity("nonsense")).toBe(0);
|
|
});
|
|
|
|
it("maps every spec category to a fallback tone", () => {
|
|
expect(externalObjectFallbackTone("failed")).toBe("danger");
|
|
expect(externalObjectFallbackTone("waiting")).toBe("warning");
|
|
expect(externalObjectFallbackTone("running")).toBe("info");
|
|
expect(externalObjectFallbackTone("succeeded")).toBe("success");
|
|
expect(externalObjectFallbackTone("auth_required")).toBe("warning");
|
|
expect(externalObjectFallbackTone("unreachable")).toBe("danger");
|
|
});
|
|
|
|
it("returns the spec lucide icon names for every category", () => {
|
|
expect(externalObjectIconForCategory("succeeded").displayName ?? "").toMatch(/CheckCircle2|Check/);
|
|
expect(externalObjectIconForCategory("failed").displayName ?? "").toMatch(/XCircle|X/);
|
|
expect(externalObjectIconForCategory("auth_required").displayName ?? "").toMatch(/KeyRound|Key/);
|
|
expect(externalObjectIconForCategory("unreachable").displayName ?? "").toMatch(/CloudOff|Cloud/);
|
|
expect(externalObjectIconForCategory("running").displayName ?? "").toMatch(/Loader2|Loader/);
|
|
});
|
|
|
|
it("maps provider-controlled icon keys through host-owned icons", () => {
|
|
expect(externalObjectIconForKey("github")?.displayName ?? "").toMatch(/Github/i);
|
|
expect(externalObjectIconForKey("git-pull-request")?.displayName ?? "").toMatch(/GitPullRequest/i);
|
|
expect(externalObjectIconForKey("unknown-provider-icon")).toBeNull();
|
|
});
|
|
|
|
it("returns a liveness warning icon only for problem liveness states", () => {
|
|
expect(externalObjectIconForLiveness("rate_limited")?.displayName ?? "").toMatch(/AlertCircle|CircleAlert|Alert/);
|
|
expect(externalObjectIconForLiveness("unreachable")?.displayName ?? "").toMatch(/CloudOff|Cloud/);
|
|
expect(externalObjectIconForLiveness("auth_required")?.displayName ?? "").toMatch(/KeyRound|Key/);
|
|
expect(externalObjectIconForLiveness("fresh")).toBeNull();
|
|
expect(externalObjectIconForLiveness("unknown")).toBeNull();
|
|
expect(externalObjectIconForLiveness("stale")).toBeNull();
|
|
});
|
|
|
|
it("sorts items by severity first, preserving insertion order within a tone", () => {
|
|
const items = [
|
|
{ id: "a", statusTone: "info", providerKey: "github", objectType: "pull_request", displayTitle: null, statusCategory: "running", liveness: "fresh", isTerminal: false },
|
|
{ id: "b", statusTone: "danger", providerKey: "ci", objectType: "deployment", displayTitle: null, statusCategory: "failed", liveness: "fresh", isTerminal: false },
|
|
{ id: "c", statusTone: "warning", providerKey: "hubspot", objectType: "lead", displayTitle: null, statusCategory: "waiting", liveness: "fresh", isTerminal: false },
|
|
{ id: "d", statusTone: "danger", providerKey: "github", objectType: "issue", displayTitle: null, statusCategory: "blocked", liveness: "fresh", isTerminal: false },
|
|
] as const;
|
|
const sorted = sortExternalObjectsBySeverity(items as never);
|
|
expect(sorted.map((item) => item.id)).toEqual(["b", "d", "c", "a"]);
|
|
});
|
|
|
|
it("hides the rollup when every item is in a muted tone", () => {
|
|
const summary = {
|
|
total: 2,
|
|
byStatusCategory: { closed: 2 },
|
|
byLiveness: { fresh: 2 },
|
|
highestSeverity: "muted" as const,
|
|
staleCount: 0,
|
|
authRequiredCount: 0,
|
|
unreachableCount: 0,
|
|
objects: [
|
|
{ id: "a", providerKey: "x", objectType: "y", displayTitle: null, statusCategory: "closed" as const, statusTone: "muted" as const, liveness: "fresh" as const, isTerminal: true },
|
|
{ id: "b", providerKey: "x", objectType: "y", displayTitle: null, statusCategory: "closed" as const, statusTone: "muted" as const, liveness: "fresh" as const, isTerminal: true },
|
|
],
|
|
};
|
|
expect(dominantExternalObjectTone(summary)).toBeNull();
|
|
expect(externalObjectDominantCount(summary)).toBe(0);
|
|
});
|
|
|
|
it("counts only the dominant-severity items in the rollup", () => {
|
|
const summary = {
|
|
total: 5,
|
|
byStatusCategory: { failed: 3, succeeded: 2 },
|
|
byLiveness: { fresh: 5 },
|
|
highestSeverity: "danger" as const,
|
|
staleCount: 0,
|
|
authRequiredCount: 0,
|
|
unreachableCount: 0,
|
|
objects: [
|
|
{ id: "a", providerKey: "ci", objectType: "deployment", displayTitle: null, statusCategory: "failed" as const, statusTone: "danger" as const, liveness: "fresh" as const, isTerminal: false },
|
|
{ id: "b", providerKey: "ci", objectType: "deployment", displayTitle: null, statusCategory: "failed" as const, statusTone: "danger" as const, liveness: "fresh" as const, isTerminal: false },
|
|
{ id: "c", providerKey: "ci", objectType: "deployment", displayTitle: null, statusCategory: "failed" as const, statusTone: "danger" as const, liveness: "fresh" as const, isTerminal: false },
|
|
{ id: "d", providerKey: "github", objectType: "pull_request", displayTitle: null, statusCategory: "succeeded" as const, statusTone: "success" as const, liveness: "fresh" as const, isTerminal: true },
|
|
{ id: "e", providerKey: "github", objectType: "pull_request", displayTitle: null, statusCategory: "succeeded" as const, statusTone: "success" as const, liveness: "fresh" as const, isTerminal: true },
|
|
],
|
|
};
|
|
expect(dominantExternalObjectTone(summary)).toBe("danger");
|
|
expect(externalObjectDominantCount(summary)).toBe(3);
|
|
});
|
|
});
|