mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-26 10:07:24 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Sandboxed runs wrap `git` and `gh` with broker launchers so agents do not inherit ambient host credentials, and task feeds use `connection_intent` cards to let users connect services inline. > - When a workspace has no GitHub connection configured, anonymous sandbox launchers logged `Paperclip: GitHub capability_missing; continuing without managed credentials.` on every `git` or `gh` call, and failed local `gh --version`/`--help` commands. > - In addition, when an agent tried to clone a private GitHub repository, push to GitHub, or run `gh` without a GitHub connection configured, the task feed did not prompt the user with a GitHub connection card. > - Both issues hurt first-run clarity: noisy `capability_missing` warnings look like broken infrastructure, while unconfigured git auth failures leave users without an inline path to connect GitHub. > - This pull request marks intentional anonymous sandbox launchers silent, treats local `gh` version/help commands as local, and automatically attaches a `github` `connection_intent` card in the task feed when a run hits an unambiguous unconfigured GitHub auth failure. > - The benefit is clean anonymous git/gh execution and a one-click inline GitHub setup card when a task needs private GitHub access. ## Linked Issues or Issue Description **What happened?** In sandboxes without a GitHub connection configured, running `git` (such as `git --version` or public clones) logged `Paperclip: GitHub capability_missing; continuing without managed credentials.` and `gh --version` / `gh --help` attempted broker resolution. When an agent tried an authenticated GitHub operation (such as cloning a private repository, pushing, or running `gh`), the run failed or reported the auth error without showing a GitHub connection card in the task feed. **Expected behavior** Anonymous unconfigured sandbox launchers should run public/local `git` and local `gh` commands silently without emitting `capability_missing` warnings, while keeping `capability_missing` diagnostics for genuinely missing or failed-bridge broker configs. When an agent hits an unconfigured GitHub authentication failure during a task run or workspace clone, Paperclip should attach a `github` connection card in the task feed and mark run liveness as blocked waiting for the user. **Steps to reproduce** 1. Run an agent in a sandbox with no GitHub connection configured. 2. Execute `git --version`, `gh --version`, and `git clone https://github.com/<org>/<private-repo>.git`. 3. Observe `Paperclip: GitHub capability_missing; continuing without managed credentials.` on stdout/stderr and no GitHub connection card in the task feed after the private clone fails. **Paperclip version or commit** `a0d5f275af` (`master`) **Deployment mode** Other (sandboxed Paperclip Runner and local server) ## What Changed - Added explicit `anonymous` mode to GitHub broker launchers (`packages/adapter-utils/src/github-launcher.ts`, `packages/adapter-utils/src/execution-target.ts`, `server/src/services/heartbeat-github-launchers.ts`) and supported `"anonymous": true` in `broker.json` so intentionally unconfigured sandboxes stay quiet while missing/broken broker bridges still emit `capability_missing`. - Treated local `gh` commands (`--version`, `--help`, `-h`, `version`, `help`) as local in the `gh` launcher without contacting the credential broker. - Added `server/src/services/git-auth-connection-detection.ts` to detect unambiguous `github.com` auth failures in failed command execution events (`tool.execution.completed` and `item.completed`) and structured workspace clone failures while ignoring `exitCode === 0`, assistant prose, non-GitHub remotes, transient 5xx errors, and local non-auth git errors. - Added controller-only `connectionIntentService(db).requestForRunGitHubAuthFailure(runId, resultJsonOverride?)` in `server/src/services/connection-intents.ts` (with `completed_git_auth` run-context mode) to create a `github` `connection_intent` card in the task feed when GitHub is not already configured/usable, skipping `host` auth mode, closed tasks, non-latest runs, existing pending cards, and declined continuations. - Wired `requestForRunGitHubAuthFailure` into `classifyAndPersistRunLiveness` (`server/src/services/heartbeat/run-lifecycle.ts`) and added `githubConnectionRequested` support to `classifyRunLiveness` (`server/src/services/run-liveness.ts`) to classify the run as `blocked` (`Waiting for GitHub connection`). - Added unit and embedded-Postgres integration tests in `packages/adapter-utils/src/github-launcher.test.ts`, `server/src/services/heartbeat-github-launchers.test.ts`, and `server/src/__tests__/github-connection-auto-request.test.ts`. ## Verification - `pnpm vitest run packages/adapter-utils/src/github-launcher.test.ts` (41/41 passed) - `pnpm vitest run packages/adapter-utils/src/github-launcher-environment.test.ts --testTimeout=15000` (40 passed, 1 skipped) - `pnpm vitest run server/src/services/heartbeat-github-launchers.test.ts server/src/services/native-runtime/native-github-access.test.ts` (passed) - `pnpm vitest run server/src/__tests__/github-connection-auto-request.test.ts server/src/__tests__/connection-intents-service.test.ts server/src/__tests__/ai-auth-failure.test.ts server/src/services/heartbeat/run-comment-selection.test.ts` (86/86 passed) - `pnpm vitest run server/src/__tests__/run-liveness.test.ts` (21/21 passed) - `pnpm vitest run server/src/__tests__/heartbeat-process-recovery.test.ts -t "clone|connection|GitHub"` (6/6 passed) - `pnpm --filter @paperclipai/adapter-utils typecheck && pnpm --filter @paperclipai/server typecheck` (0 errors) ## Risks - Low risk. No database migrations or UI schema changes are required; the auto-created card reuses the existing `connection_intent` interaction kind (`serviceSlug: "github"`). - False-positive card creation is guarded by strict `github.com` host matching, non-zero command exit requirements, `githubAuthenticationMode !== "host"`, active-identity checks (`usableConnectionForAgent` and `resolveManagedGitHubIdentitySelection`), idempotent pending-card checks, and decline respect. ## Model Used - Google Gemini (Antigravity agent) with tool use and local code/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 - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge
688 lines
25 KiB
TypeScript
688 lines
25 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
agents,
|
|
authUsers,
|
|
companies,
|
|
companyMemberships,
|
|
connectionGrants,
|
|
createDb,
|
|
EMBEDDED_POSTGRES_TEST_TIMEOUT_MS,
|
|
goals,
|
|
heartbeatRunEvents,
|
|
heartbeatRuns,
|
|
issues,
|
|
issueThreadInteractions,
|
|
toolApplications,
|
|
toolCatalogEntries,
|
|
toolConnectionInstalls,
|
|
toolConnections,
|
|
} from "@paperclipai/db";
|
|
import {
|
|
detectUnconfiguredGitHubAuthFailureInEvents,
|
|
isGitHubWorkspaceCloneAuthFailure,
|
|
} from "../services/git-auth-connection-detection.js";
|
|
import { connectionIntentService } from "../services/connection-intents.js";
|
|
import { classifyRunLiveness } from "../services/run-liveness.js";
|
|
import { createHeartbeatLifecycle, type HeartbeatLifecycleDependencies } from "../services/heartbeat/run-lifecycle.js";
|
|
import { createHeartbeatRunPreparation } from "../services/heartbeat/run-preparation.js";
|
|
import { createHeartbeatRunState } from "../services/heartbeat/run-state.js";
|
|
import {
|
|
getEmbeddedPostgresTestSupport,
|
|
startEmbeddedPostgresTestDatabase,
|
|
} from "./helpers/embedded-postgres.js";
|
|
|
|
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
|
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
|
|
|
function toolExecutionEvent(options: {
|
|
output: string;
|
|
exitCode?: number | null;
|
|
status?: string;
|
|
name?: string;
|
|
subject?: string;
|
|
}) {
|
|
return {
|
|
eventType: "tool.execution.completed",
|
|
payload: {
|
|
prpEvent: {
|
|
schema: "paperclip.prp.event.v1",
|
|
eventType: "tool.execution.completed",
|
|
payload: {
|
|
schema: "paperclip.tool.execution.v1",
|
|
transport: "process",
|
|
operation: "shell",
|
|
name: options.name ?? "git clone https://github.com/acme/private-repo.git",
|
|
...(options.subject ? { subject: options.subject } : {}),
|
|
status: options.status ?? "failed",
|
|
exitCode: options.exitCode === undefined ? 128 : options.exitCode,
|
|
output: options.output,
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function commandItemEvent(options: {
|
|
command: string;
|
|
output: string;
|
|
exitCode?: number;
|
|
status?: string;
|
|
}) {
|
|
return {
|
|
eventType: "item.completed",
|
|
payload: {
|
|
prpEvent: {
|
|
schema: "paperclip.prp.event.v1",
|
|
eventType: "item.completed",
|
|
payload: {
|
|
item: {
|
|
type: "command_execution",
|
|
command: options.command,
|
|
exit_code: options.exitCode ?? 128,
|
|
status: options.status ?? "failed",
|
|
aggregated_output: options.output,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
function createTestLifecycle(db: ReturnType<typeof createDb>) {
|
|
const dependencies = {
|
|
...createHeartbeatRunState(db),
|
|
...createHeartbeatRunPreparation(db),
|
|
issuesSvc: { addComment: vi.fn(), listReviewAttention: vi.fn() },
|
|
recovery: { escalateStrandedAssignedIssue: vi.fn() },
|
|
companySkills: { completeTestRunForIssue: vi.fn() },
|
|
budgets: { getInvocationBlock: vi.fn(async () => null) },
|
|
treeControlSvc: { getActivePauseHoldGate: vi.fn(async () => null) },
|
|
enqueueWakeup: vi.fn(),
|
|
getCurrentUserRedactionOptions: vi.fn(async () => ({ enabled: false })),
|
|
getAgentInvokability: vi.fn<HeartbeatLifecycleDependencies["getAgentInvokability"]>(async () => ({ invokable: true })),
|
|
emitTerminalAgentTaskRun: vi.fn(),
|
|
budgetHooks: {},
|
|
} satisfies HeartbeatLifecycleDependencies;
|
|
return createHeartbeatLifecycle(db, dependencies);
|
|
}
|
|
|
|
describe("detectUnconfiguredGitHubAuthFailureInEvents", () => {
|
|
it.each([
|
|
[
|
|
"HTTPS private clone with disabled prompts",
|
|
toolExecutionEvent({
|
|
output: "Cloning into 'private-repo'...\nfatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
[
|
|
// paperclip:allow-git-push: synthetic command output fixture testing GitHub auth detection
|
|
"HTTPS git push authentication failed",
|
|
toolExecutionEvent({
|
|
// paperclip:allow-git-push: synthetic command fixture testing GitHub auth detection
|
|
name: "git push origin main",
|
|
output: "fatal: Authentication failed for 'https://github.com/acme/private-repo.git/'\n",
|
|
}),
|
|
],
|
|
[
|
|
"HTTPS git clone 403",
|
|
toolExecutionEvent({
|
|
output: "fatal: unable to access 'https://github.com/acme/private-repo.git/': The requested URL returned error: 403\n",
|
|
}),
|
|
],
|
|
[
|
|
"HTTPS private repository not found",
|
|
toolExecutionEvent({
|
|
output: "remote: Repository not found.\nfatal: repository 'https://github.com/acme/private-repo.git/' not found\n",
|
|
}),
|
|
],
|
|
[
|
|
"SSH permission denied against github.com",
|
|
commandItemEvent({
|
|
command: "git clone git@github.com:acme/private-repo.git",
|
|
output: "git@github.com: Permission denied (publickey).\nfatal: Could not read from remote repository.\n",
|
|
}),
|
|
],
|
|
[
|
|
"gh CLI not logged in",
|
|
toolExecutionEvent({
|
|
name: "gh pr create --fill",
|
|
exitCode: 4,
|
|
output: "To get started with GitHub CLI, please run: gh auth login\nAlternatively, populate the GH_TOKEN environment variable with a GitHub API authentication token.\n",
|
|
}),
|
|
],
|
|
[
|
|
"gh CLI not logged into any GitHub hosts",
|
|
commandItemEvent({
|
|
command: "gh repo view acme/private-repo",
|
|
exitCode: 1,
|
|
output: "You are not logged into any GitHub hosts. To log in, run: gh auth login\n",
|
|
}),
|
|
],
|
|
[
|
|
"OpenCode generic bash tool without command text",
|
|
toolExecutionEvent({
|
|
name: "bash",
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
[
|
|
"timeout-wrapped git clone",
|
|
toolExecutionEvent({
|
|
name: "timeout 60 git clone https://github.com/acme/private-repo.git",
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
[
|
|
"timeout-wrapped bash -lc compound git clone",
|
|
toolExecutionEvent({
|
|
name: "timeout -k 5 60 bash -lc 'cd /workspace && git clone https://github.com/acme/private-repo.git'",
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
[
|
|
"sudo-wrapped git clone",
|
|
toolExecutionEvent({
|
|
name: "sudo -u runner git clone https://github.com/acme/private-repo.git",
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
])("detects unambiguous GitHub auth failure: %s", (_label, event) => {
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([event])).toBe(true);
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"successful command quoting error text",
|
|
toolExecutionEvent({
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
[
|
|
"assistant prose quoting error text",
|
|
{
|
|
eventType: "item.completed",
|
|
payload: {
|
|
prpEvent: {
|
|
schema: "paperclip.prp.event.v1",
|
|
eventType: "item.completed",
|
|
payload: {
|
|
kind: "agentMessage",
|
|
text: "fatal: could not read Username for 'https://github.com': terminal prompts disabled",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
],
|
|
[
|
|
"non-GitHub HTTPS remote (GitLab)",
|
|
toolExecutionEvent({
|
|
name: "git clone https://gitlab.com/acme/private.git",
|
|
output: "fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
[
|
|
"non-GitHub SSH remote",
|
|
commandItemEvent({
|
|
command: "git clone git@gitlab.com:acme/private.git",
|
|
output: "git@gitlab.com: Permission denied (publickey).\nfatal: Could not read from remote repository.\n",
|
|
}),
|
|
],
|
|
[
|
|
"transient GitHub 503 outage",
|
|
toolExecutionEvent({
|
|
output: "fatal: unable to access 'https://github.com/acme/repo.git/': The requested URL returned error: 503\n",
|
|
}),
|
|
],
|
|
[
|
|
"local non-auth git error",
|
|
toolExecutionEvent({
|
|
name: "git status",
|
|
output: "fatal: not a git repository (or any of the parent directories): .git\n",
|
|
}),
|
|
],
|
|
[
|
|
"unrelated command printing git auth error text",
|
|
toolExecutionEvent({
|
|
name: "vitest run server/src/__tests__/auth.test.ts",
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
])("ignores non-matching or non-GitHub failure: %s", (_label, event) => {
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([event])).toBe(false);
|
|
});
|
|
|
|
it("clears detection only on matching authenticated recovery, not unrelated/local/other-repo commands", () => {
|
|
const authFailure = toolExecutionEvent({
|
|
name: "git clone https://github.com/acme/private-repo.git",
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
});
|
|
const localGitStatus = toolExecutionEvent({
|
|
name: "git status",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "On branch main\n",
|
|
});
|
|
const ghSubcommandHelp = toolExecutionEvent({
|
|
name: "gh pr create --help",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "Create a pull request on GitHub\n",
|
|
});
|
|
const echoCommand = toolExecutionEvent({
|
|
name: "echo gh auth status",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "gh auth status\n",
|
|
});
|
|
const gitlabLsRemote = toolExecutionEvent({
|
|
name: "git ls-remote https://gitlab.com/team/public.git",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "abc123\tHEAD\n",
|
|
});
|
|
const differentPublicRepoClone = toolExecutionEvent({
|
|
name: "git clone https://github.com/acme/public-mirror.git",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "Cloning into 'public-mirror'...\n",
|
|
});
|
|
const recoveredSameRepoClone = toolExecutionEvent({
|
|
name: "git -C /workspace clone https://github.com/acme/private-repo.git",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "Cloning into 'private-repo'...\n",
|
|
});
|
|
const recoveredGitPush = toolExecutionEvent({
|
|
// paperclip:allow-git-push: synthetic command fixture testing recovery detection
|
|
name: "git -C /workspace push origin main",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "To https://github.com/acme/private-repo.git\n",
|
|
});
|
|
|
|
const recoveredQuietGitPush = toolExecutionEvent({
|
|
// paperclip:allow-git-push: synthetic command fixture testing recovery detection
|
|
name: "git -C /workspace push origin main",
|
|
exitCode: 0,
|
|
status: "completed",
|
|
output: "Everything up-to-date\n",
|
|
});
|
|
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, localGitStatus])).toBe(true);
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, ghSubcommandHelp])).toBe(true);
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, echoCommand])).toBe(true);
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, gitlabLsRemote])).toBe(true);
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, differentPublicRepoClone])).toBe(true);
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, recoveredSameRepoClone])).toBe(false);
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, recoveredGitPush])).toBe(false);
|
|
expect(detectUnconfiguredGitHubAuthFailureInEvents([authFailure, recoveredQuietGitPush])).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("isGitHubWorkspaceCloneAuthFailure", () => {
|
|
it("detects structured git clone authentication failures targeting github.com", () => {
|
|
expect(isGitHubWorkspaceCloneAuthFailure({
|
|
resultJson: {
|
|
connectionFailure: { schemaVersion: 1, provider: "git", operation: "clone", reason: "authentication_failed" },
|
|
},
|
|
contextSnapshot: { workspaceRepoUrl: "https://github.com/acme/private-repo.git" },
|
|
})).toBe(true);
|
|
expect(isGitHubWorkspaceCloneAuthFailure({
|
|
resultJson: {
|
|
workspaceValidation: {
|
|
materializationFailures: [{
|
|
repoUrl: "git@github.com:acme/private-repo.git",
|
|
connectionFailure: { schemaVersion: 1, provider: "git", operation: "clone", reason: "repository_unavailable" },
|
|
}],
|
|
},
|
|
},
|
|
})).toBe(true);
|
|
});
|
|
|
|
it("ignores non-GitHub remotes and non-auth transport errors", () => {
|
|
expect(isGitHubWorkspaceCloneAuthFailure({
|
|
resultJson: {
|
|
connectionFailure: { schemaVersion: 1, provider: "git", operation: "clone", reason: "authentication_failed" },
|
|
},
|
|
contextSnapshot: { workspaceRepoUrl: "https://example.test/team/repo.git" },
|
|
})).toBe(false);
|
|
expect(isGitHubWorkspaceCloneAuthFailure({
|
|
resultJson: {
|
|
connectionFailure: { schemaVersion: 1, provider: "git", operation: "clone", reason: "dns_failure" },
|
|
},
|
|
contextSnapshot: { workspaceRepoUrl: "https://github.com/acme/private-repo.git" },
|
|
})).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("classifyRunLiveness with githubConnectionRequested", () => {
|
|
it("marks runs with an auto-attached GitHub connection card as blocked waiting for user", () => {
|
|
for (const runStatus of ["failed", "succeeded"] as const) {
|
|
expect(classifyRunLiveness({
|
|
runStatus,
|
|
issue: { status: "in_progress", title: "Clone private repo", description: null },
|
|
stdoutExcerpt: "I could not clone the private repo without GitHub access.",
|
|
githubConnectionRequested: true,
|
|
})).toMatchObject({
|
|
livenessState: "blocked",
|
|
livenessReason: "Waiting for GitHub connection",
|
|
nextAction: "Complete the connection request to continue this task",
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
describeEmbeddedPostgres("automatic GitHub connection card on unconfigured git auth failure", () => {
|
|
let db!: ReturnType<typeof createDb>;
|
|
let cleanup: (() => Promise<void>) | undefined;
|
|
const userId = `git-auto-user-${randomUUID()}`;
|
|
|
|
beforeAll(async () => {
|
|
const tempDb = await startEmbeddedPostgresTestDatabase("paperclip-git-auto-connect-");
|
|
cleanup = tempDb.cleanup;
|
|
db = createDb(tempDb.connectionString);
|
|
await db.insert(authUsers).values({
|
|
id: userId,
|
|
name: "Beta Tester",
|
|
email: "beta@example.test",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
});
|
|
}, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS);
|
|
|
|
afterAll(async () => {
|
|
await db?.$client.end();
|
|
await cleanup?.();
|
|
});
|
|
|
|
async function seedTaskAndRun(options: {
|
|
status: "succeeded" | "failed";
|
|
contextSnapshot?: Record<string, unknown>;
|
|
resultJson?: Record<string, unknown>;
|
|
events?: Array<{ eventType: string; payload: Record<string, unknown> }>;
|
|
}) {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const goalId = randomUUID();
|
|
const issueId = randomUUID();
|
|
const runId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "GitHub Auto Connect Test",
|
|
issuePrefix: `G${randomUUID().replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
|
requireBoardApprovalForNewAgents: false,
|
|
});
|
|
await db.insert(companyMemberships).values({
|
|
companyId,
|
|
principalType: "user",
|
|
principalId: userId,
|
|
status: "active",
|
|
membershipRole: "member",
|
|
});
|
|
await db.insert(goals).values({
|
|
id: goalId,
|
|
companyId,
|
|
title: "Clone and push code",
|
|
level: "task",
|
|
status: "active",
|
|
});
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "Codex Engineer",
|
|
role: "engineer",
|
|
status: "active",
|
|
adapterType: "paperclip_runner",
|
|
adapterConfig: { provider: "codex" },
|
|
runtimeConfig: {},
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values({
|
|
id: issueId,
|
|
companyId,
|
|
goalId,
|
|
title: "Clone private repo",
|
|
status: "in_progress",
|
|
assigneeAgentId: agentId,
|
|
});
|
|
const [run] = await db.insert(heartbeatRuns).values({
|
|
id: runId,
|
|
companyId,
|
|
agentId,
|
|
invocationSource: "assignment",
|
|
triggerDetail: "manual",
|
|
status: options.status,
|
|
runtimeMode: "native",
|
|
nativeIssueId: issueId,
|
|
responsibleUserId: userId,
|
|
contextSnapshot: {
|
|
issueId,
|
|
githubAuthenticationMode: "managed",
|
|
...(options.contextSnapshot ?? {}),
|
|
},
|
|
resultJson: options.resultJson ?? null,
|
|
stdoutExcerpt: "I tried to clone the repo but GitHub credentials are not configured.",
|
|
}).returning();
|
|
|
|
for (const [idx, ev] of (options.events ?? []).entries()) {
|
|
await db.insert(heartbeatRunEvents).values({
|
|
companyId,
|
|
runId,
|
|
agentId,
|
|
seq: idx + 1,
|
|
eventType: ev.eventType,
|
|
stream: "system",
|
|
level: "info",
|
|
payload: ev.payload,
|
|
});
|
|
}
|
|
return { companyId, agentId, issueId, runId, run: run! };
|
|
}
|
|
|
|
it("creates a pending GitHub connection card in the task feed when an agent fails git clone/push without GitHub configured", async () => {
|
|
const { companyId, agentId, issueId, runId, run } = await seedTaskAndRun({
|
|
status: "succeeded",
|
|
events: [
|
|
toolExecutionEvent({
|
|
name: "git clone https://github.com/acme/private-repo.git",
|
|
output: "Cloning into 'private-repo'...\nfatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
});
|
|
|
|
const lifecycle = createTestLifecycle(db);
|
|
const updatedRun = await lifecycle.classifyAndPersistRunLiveness(run);
|
|
expect(updatedRun?.livenessState).toBe("blocked");
|
|
expect(updatedRun?.livenessReason).toBe("Waiting for GitHub connection");
|
|
|
|
const cards = await db.select().from(issueThreadInteractions).where(and(
|
|
eq(issueThreadInteractions.companyId, companyId),
|
|
eq(issueThreadInteractions.issueId, issueId),
|
|
eq(issueThreadInteractions.kind, "connection_intent"),
|
|
));
|
|
expect(cards).toHaveLength(1);
|
|
expect(cards[0]).toMatchObject({
|
|
kind: "connection_intent",
|
|
status: "pending",
|
|
sourceRunId: runId,
|
|
addresseeUserId: userId,
|
|
payload: expect.objectContaining({
|
|
serviceSlug: "github",
|
|
serviceName: "GitHub",
|
|
requestingAgentId: agentId,
|
|
phase: "requested",
|
|
}),
|
|
});
|
|
|
|
// Calling again reuses the pending card without creating a duplicate and keeps liveness blocked.
|
|
const repeated = await connectionIntentService(db).requestForRunGitHubAuthFailure(runId);
|
|
expect(repeated?.interactionId).toBe(cards[0]!.id);
|
|
const recheckedRun = await lifecycle.classifyAndPersistRunLiveness(run);
|
|
expect(recheckedRun?.livenessState).toBe("blocked");
|
|
const afterRepeat = await db.select().from(issueThreadInteractions).where(and(
|
|
eq(issueThreadInteractions.companyId, companyId),
|
|
eq(issueThreadInteractions.issueId, issueId),
|
|
eq(issueThreadInteractions.kind, "connection_intent"),
|
|
));
|
|
expect(afterRepeat).toHaveLength(1);
|
|
});
|
|
|
|
it("creates a GitHub connection card when workspace setup fails to clone a private GitHub repository", async () => {
|
|
const { companyId, issueId, run } = await seedTaskAndRun({
|
|
status: "failed",
|
|
contextSnapshot: { workspaceRepoUrl: "https://github.com/acme/private-repo.git" },
|
|
resultJson: {
|
|
connectionFailure: {
|
|
schemaVersion: 1,
|
|
provider: "git",
|
|
operation: "clone",
|
|
reason: "authentication_failed",
|
|
},
|
|
},
|
|
});
|
|
|
|
const lifecycle = createTestLifecycle(db);
|
|
const updatedRun = await lifecycle.classifyAndPersistRunLiveness(run);
|
|
expect(updatedRun?.livenessState).toBe("blocked");
|
|
|
|
const cards = await db.select().from(issueThreadInteractions).where(and(
|
|
eq(issueThreadInteractions.companyId, companyId),
|
|
eq(issueThreadInteractions.issueId, issueId),
|
|
eq(issueThreadInteractions.kind, "connection_intent"),
|
|
));
|
|
expect(cards).toHaveLength(1);
|
|
expect(cards[0]?.payload).toMatchObject({ serviceSlug: "github", serviceName: "GitHub" });
|
|
});
|
|
|
|
it("does not create a second card after the user declines the GitHub connection request", async () => {
|
|
const first = await seedTaskAndRun({
|
|
status: "succeeded",
|
|
events: [
|
|
toolExecutionEvent({
|
|
// paperclip:allow-git-push: synthetic command fixture testing GitHub auth detection
|
|
name: "git push origin main",
|
|
output: "fatal: Authentication failed for 'https://github.com/acme/private-repo.git/'\n",
|
|
}),
|
|
],
|
|
});
|
|
const created = await connectionIntentService(db).requestForRunGitHubAuthFailure(first.runId);
|
|
expect(created?.interactionId).toBeTruthy();
|
|
|
|
await connectionIntentService(db).decline(created!.interactionId!, userId, "Not right now");
|
|
|
|
const continuationRunId = randomUUID();
|
|
const [continuationRun] = await db.insert(heartbeatRuns).values({
|
|
id: continuationRunId,
|
|
companyId: first.companyId,
|
|
agentId: first.agentId,
|
|
invocationSource: "assignment",
|
|
triggerDetail: "manual",
|
|
status: "succeeded",
|
|
runtimeMode: "native",
|
|
nativeIssueId: first.issueId,
|
|
responsibleUserId: userId,
|
|
contextSnapshot: {
|
|
issueId: first.issueId,
|
|
githubAuthenticationMode: "managed",
|
|
interactionId: created!.interactionId!,
|
|
},
|
|
stdoutExcerpt: "Understood, skipping GitHub push.",
|
|
}).returning();
|
|
await db.insert(heartbeatRunEvents).values({
|
|
companyId: first.companyId,
|
|
runId: continuationRunId,
|
|
agentId: first.agentId,
|
|
seq: 1,
|
|
eventType: "tool.execution.completed",
|
|
stream: "system",
|
|
level: "info",
|
|
payload: toolExecutionEvent({
|
|
// paperclip:allow-git-push: synthetic command fixture testing GitHub auth detection
|
|
name: "git push origin main",
|
|
output: "fatal: Authentication failed for 'https://github.com/acme/private-repo.git/'\n",
|
|
}).payload,
|
|
});
|
|
|
|
const retried = await connectionIntentService(db).requestForRunGitHubAuthFailure(continuationRun!.id);
|
|
expect(retried).toBeNull();
|
|
const cards = await db.select().from(issueThreadInteractions).where(and(
|
|
eq(issueThreadInteractions.companyId, first.companyId),
|
|
eq(issueThreadInteractions.issueId, first.issueId),
|
|
eq(issueThreadInteractions.kind, "connection_intent"),
|
|
));
|
|
expect(cards).toHaveLength(1);
|
|
expect(cards[0]?.status).toBe("rejected");
|
|
});
|
|
|
|
it("does not create a card when GitHub is already usable for the agent or when using host credentials", async () => {
|
|
const hostRun = await seedTaskAndRun({
|
|
status: "succeeded",
|
|
contextSnapshot: { githubAuthenticationMode: "host" },
|
|
events: [
|
|
toolExecutionEvent({
|
|
output: "fatal: could not read Username for 'https://github.com': terminal prompts disabled\n",
|
|
}),
|
|
],
|
|
});
|
|
expect(await connectionIntentService(db).requestForRunGitHubAuthFailure(hostRun.runId)).toBeNull();
|
|
|
|
const connected = await seedTaskAndRun({
|
|
status: "succeeded",
|
|
events: [
|
|
toolExecutionEvent({
|
|
output: "fatal: Authentication failed for 'https://github.com/acme/other-repo.git/'\n",
|
|
}),
|
|
],
|
|
});
|
|
const [application] = await db.insert(toolApplications).values({
|
|
companyId: connected.companyId,
|
|
applicationKey: randomUUID(),
|
|
name: "GitHub",
|
|
type: "mcp_http",
|
|
metadata: { sourceTemplateKey: "github" },
|
|
}).returning();
|
|
const [connection] = await db.insert(toolConnections).values({
|
|
companyId: connected.companyId,
|
|
applicationId: application!.id,
|
|
uid: `github-test-${randomUUID()}`,
|
|
name: "GitHub",
|
|
transport: "mcp_remote",
|
|
authKind: "oauth",
|
|
credentialPolicy: "per_user",
|
|
enabled: true,
|
|
status: "active",
|
|
healthStatus: "ok",
|
|
config: { sourceTemplateKey: "github" },
|
|
}).returning();
|
|
await db.insert(connectionGrants).values({
|
|
companyId: connected.companyId,
|
|
connectionId: connection!.id,
|
|
kind: "user",
|
|
subjectUserId: userId,
|
|
status: "active",
|
|
});
|
|
await db.insert(toolConnectionInstalls).values({
|
|
companyId: connected.companyId,
|
|
connectionId: connection!.id,
|
|
targetType: "company",
|
|
targetId: connected.companyId,
|
|
});
|
|
await db.insert(toolCatalogEntries).values({
|
|
companyId: connected.companyId,
|
|
connectionId: connection!.id,
|
|
name: "get_me",
|
|
toolName: "get_me",
|
|
riskLevel: "read",
|
|
versionHash: "v1",
|
|
entryKind: "tool",
|
|
status: "active",
|
|
});
|
|
|
|
expect(await connectionIntentService(db).requestForRunGitHubAuthFailure(connected.runId)).toBeNull();
|
|
});
|
|
});
|