mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-22 11:54:35 +01:00
feat: Claude login on the new-agent page before agent creation (#11347)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The Claude local adapter supports subscription login through a sandbox > - The new-agent page must show login before the user creates an agent > - Test results must not expose raw sandbox diagnostics or secret values > - This pull request adds the login UI to both Test lanes and closes the diagnostic boundary > - The branch also adds durable cleanup recovery for failed sandbox teardown > - Reusable sandboxes must retain both their recorded teardown configuration and a valid lifecycle path until destruction succeeds > - The benefit is a usable login flow with fixed public checks, redacted server logs, and recoverable sandbox cleanup ## Linked Issues or Issue Description Related public work: [#9488](https://github.com/paperclipai/paperclip/pull/9488) adds first-class recognition for `CLAUDE_CODE_OAUTH_TOKEN` in headless and remote runs. Related public issue: [#2681](https://github.com/paperclipai/paperclip/issues/2681) requests Claude Code subscription support. This pull request adds the login transport and new-agent UI flow that those changes do not provide. **Subsystem affected:** Claude local adapter, server login probes, sandbox provider setup, cleanup recovery, and the new-agent UI. **Problem or motivation:** The Test lanes did not show the sandbox login panel in all supported cases. Test results also exposed raw probe diagnostics, and JSON escapes could end secret redaction early. **Proposed solution:** Surface the login capability through the bundled provider manifest. Prepare the same probe runtime in the ACP lane. Send diagnostics only to redacted server logs. Keep Test checks on fixed public messages. Normalize login URL hints to allowlisted HTTPS Claude and Anthropic hosts. Consume JSON escapes during redaction. Preserve failed sandbox cleanup state across retries and restarts, and prevent deletion from severing the lifecycle context of a live reusable sandbox. **Alternatives considered:** Keep raw diagnostics in Test checks or trust login URL text from the sandbox. Both choices increase information exposure. Keep separate probe behavior in the ACP lane. That choice would leave the two Test lanes inconsistent. ## What Changed - Surface the sandbox login panel on both Test lanes. - Reconcile the bundled Daytona plugin manifest so `supportsSetupTokenLogin` reaches the UI capability gate. - Prepare the ACP Test lane with the same probe runtime as the CLI Test lane. - Add the `claude_acp_login_probe_unavailable` warning when the ACP probe cannot run. - Send raw sandbox diagnostics only to redacted server logs. - Keep Test checks on fixed public messages in the ACP, managed-config, and CLI paths. - Normalize login URL hints to allowlisted HTTPS Claude and Anthropic hosts. - Redact JSON and escaped-JSON secret values, including escaped quotes and backslashes. - Preserve orphan cleanup records across provider failures, restarts, and unavailable plugins. - Atomically block environment deletion while a live reusable sandbox lease still depends on it. - Verify pending cleanup destroys plugin sandboxes with the provider configuration recorded on the lease, even after the current environment configuration changes. ## Verification - Head under review: `506b7fa2d83c36bfa5fd722ee9d95b0c7431c241`. - Focused environment route/service/runtime coverage passes: 196 tests across 3 files. - `pnpm -r typecheck` passes. - `pnpm build` passes. - The full Vitest run completed with 4,754 passing and 28 failing tests. All 23 source-test failures reproduce unchanged on parent head `58cfe61a33191ce03d965d65085d26064b4888ba`; the other 5 are duplicate executions from stale `server/dist` output. The failures are unrelated macOS path/listener and scheduler-fixture failures, so there is no new bad commit for bisect to localize. - All required CI checks pass for the current head, including build, typecheck/release registry, all server and workspace shards, serialized server suites, canary, and e2e. - A fresh Greptile review for `506b7fa2d83c36bfa5fd722ee9d95b0c7431c241` reports 5/5, “safe to merge,” with no blocking failure remaining. ## Risks - A probe or redaction change could hide useful server diagnostics. - An allowlist change could reject a valid Claude login URL. - Cleanup recovery changes could affect provider teardown ordering. - An environment with a live reusable sandbox can no longer be deleted until the owning issue or execution workspace completes teardown. - The implementation keeps public Test messages fixed and sends detail to redacted server logs. ## Model Used OpenAI GPT-5 via Codex — exact model ID: GPT-5; tool use and code execution enabled; extended reasoning enabled. The implementation author used AI-assisted development. ## 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 documented the result - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation or confirmed no separate documentation change is needed - [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 --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
3061ce6901
commit
c1c46f1e4e
101 files changed
+99708
-1031
No files matched your search
@@ -354,6 +354,58 @@ describe("agent routes adapter validation", () => {
|
||||
expect(env.CODEX_HOME).toBeUndefined();
|
||||
});
|
||||
|
||||
it("forwards a claude_local→process adapter move that drops the OAuth binding to the service unchanged", async () => {
|
||||
// The agent has the fixed Claude Code OAuth binding on the claude_local
|
||||
// adapter. A PATCH moves the agent to the process adapter and sends an empty
|
||||
// env in the same request. The route must forward the new adapter type and
|
||||
// the dropped binding to the service without a re-injection, so the
|
||||
// service-enforced binding invariant sees the removal and rejects it.
|
||||
const agentId = "11111111-1111-4111-8111-111111111111";
|
||||
mockAgentService.getById.mockResolvedValue({
|
||||
id: agentId,
|
||||
companyId: "company-1",
|
||||
name: "Claude",
|
||||
urlKey: "claude",
|
||||
role: "engineer",
|
||||
title: null,
|
||||
icon: null,
|
||||
status: "idle",
|
||||
reportsTo: null,
|
||||
capabilities: null,
|
||||
adapterType: "claude_local",
|
||||
adapterConfig: { env: { CLAUDE_CODE_OAUTH_TOKEN: { type: "user_secret_ref", key: "CLAUDE_CODE_OAUTH_TOKEN" } } },
|
||||
runtimeConfig: {},
|
||||
budgetMonthlyCents: 0,
|
||||
spentMonthlyCents: 0,
|
||||
pauseReason: null,
|
||||
pausedAt: null,
|
||||
permissions: { canCreateAgents: false },
|
||||
lastHeartbeatAt: null,
|
||||
metadata: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
const app = await createApp();
|
||||
const res = await requestApp(app, (baseUrl) =>
|
||||
request(baseUrl)
|
||||
.patch(`/api/agents/${agentId}`)
|
||||
.send({
|
||||
adapterType: "process",
|
||||
adapterConfig: { env: {} },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(res.status, JSON.stringify(res.body)).toBe(200);
|
||||
const patch = mockAgentService.update.mock.calls.at(-1)?.[1] as Record<string, unknown>;
|
||||
// The route forwards the requested adapter type, so the service can see the
|
||||
// adapter move.
|
||||
expect(patch.adapterType).toBe("process");
|
||||
// The route does not re-inject the fixed binding from the prior config, so
|
||||
// the service invariant sees the removal.
|
||||
const env = ((patch.adapterConfig as Record<string, unknown>).env as Record<string, unknown> | undefined) ?? {};
|
||||
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBeUndefined();
|
||||
});
|
||||
|
||||
it("isolates CODEX_HOME when updating a codex_local agent to set its own OPENAI_API_KEY", async () => {
|
||||
const agentId = "11111111-1111-4111-8111-111111111111";
|
||||
const app = await createApp();
|
||||
|
||||
@@ -61,6 +61,9 @@ const mockSecretService = vi.hoisted(() => ({
|
||||
const mockEnvironmentService = vi.hoisted(() => ({
|
||||
getById: vi.fn(),
|
||||
releaseLease: vi.fn(),
|
||||
// The login guard reads the companies that own the environment. An empty
|
||||
// list marks an instance-global environment, so the guard stays open.
|
||||
listBoundCompanyIds: vi.fn(async () => [] as string[]),
|
||||
}));
|
||||
|
||||
const mockEnvironmentRuntime = vi.hoisted(() => ({
|
||||
|
||||
@@ -864,6 +864,7 @@ describe.sequential("agent permission routes", () => {
|
||||
expect.objectContaining({
|
||||
status: "idle",
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "agent-admin-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
expect(mockAccessService.setPrincipalPermission).toHaveBeenCalledWith(
|
||||
companyId,
|
||||
@@ -996,6 +997,7 @@ describe.sequential("agent permission routes", () => {
|
||||
},
|
||||
},
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "board-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1058,6 +1060,7 @@ describe.sequential("agent permission routes", () => {
|
||||
},
|
||||
},
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "board-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
} finally {
|
||||
unregisterServerAdapter("failing_profile_discovery");
|
||||
@@ -1096,6 +1099,7 @@ describe.sequential("agent permission routes", () => {
|
||||
model: DEFAULT_OPENCODE_LOCAL_MODEL,
|
||||
}),
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "board-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1133,6 +1137,7 @@ describe.sequential("agent permission routes", () => {
|
||||
model: "anthropic/claude-sonnet-4-5",
|
||||
}),
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "board-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1174,6 +1179,7 @@ describe.sequential("agent permission routes", () => {
|
||||
},
|
||||
},
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "board-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1397,6 +1403,7 @@ describe.sequential("agent permission routes", () => {
|
||||
expect.objectContaining({
|
||||
defaultEnvironmentId: environmentId,
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "board-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1482,6 +1489,7 @@ describe.sequential("agent permission routes", () => {
|
||||
adapterType: adapterCase.adapterType,
|
||||
defaultEnvironmentId: environmentId,
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "board-user", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -945,6 +945,7 @@ describe.sequential("agent skill routes", () => {
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "local-board", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
expect(mockTrackAgentCreated).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
@@ -994,6 +995,7 @@ describe.sequential("agent skill routes", () => {
|
||||
expect.objectContaining({
|
||||
role: "security",
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "local-board", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
expect(mockTrackAgentCreated).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
@@ -1217,6 +1219,7 @@ describe.sequential("agent skill routes", () => {
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
{ claudeLogin: { storedSessionId: null, ownerUserId: "local-board", applyExistingWithoutClaim: false } },
|
||||
);
|
||||
expect(mockApprovalService.create).toHaveBeenCalledWith(
|
||||
"company-1",
|
||||
|
||||
@@ -0,0 +1,948 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { mkdirSync, rmSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import {
|
||||
agents,
|
||||
claudeSetupTokenSessions,
|
||||
companies,
|
||||
companySecretBindings,
|
||||
companySecretProviderConfigs,
|
||||
companySecretVersions,
|
||||
companySecrets,
|
||||
createDb,
|
||||
environments,
|
||||
userSecretDeclarations,
|
||||
userSecretDefinitions,
|
||||
} from "@paperclipai/db";
|
||||
import {
|
||||
getEmbeddedPostgresTestSupport,
|
||||
startEmbeddedPostgresTestDatabase,
|
||||
} from "./helpers/embedded-postgres.js";
|
||||
import { agentService } from "../services/agents.ts";
|
||||
import {
|
||||
assertClaudeOAuthBindingInvariant,
|
||||
CLAUDE_OAUTH_CLAIM_REJECTED,
|
||||
CLAUDE_OAUTH_CREDENTIAL_CONFLICT,
|
||||
claudeOAuthClaimRejectedError,
|
||||
isFixedClaudeOAuthBinding,
|
||||
secretService,
|
||||
} from "../services/secrets.js";
|
||||
import {
|
||||
createDbSetupTokenCleanupStore,
|
||||
type SetupTokenSessionScope,
|
||||
} from "../services/setup-token-session.js";
|
||||
import { createSetupTokenSecretWriter } from "../services/setup-token-transport-binding.js";
|
||||
|
||||
// The fixed Claude Code OAuth binding. It is a user-secret reference to the
|
||||
// fixed key. Any other shape is a replacement or a weaker binding.
|
||||
const FIXED_BINDING = { type: "user_secret_ref", key: "CLAUDE_CODE_OAUTH_TOKEN" } as const;
|
||||
const withEnv = (env: Record<string, unknown>) => ({ env });
|
||||
|
||||
// --- The pure server-enforced binding invariant (no database) ----------------
|
||||
|
||||
describe("assertClaudeOAuthBindingInvariant", () => {
|
||||
it("reports that a create introduces the fixed binding", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
priorConfig: null,
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: true, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("reports that a write keeps an existing fixed binding", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
priorConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: true });
|
||||
});
|
||||
|
||||
it("permits a normal removal of the fixed binding", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({}),
|
||||
priorConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("permits a normal plain-value replacement of the fixed binding", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: { type: "plain", value: "sk-fake" } }),
|
||||
priorConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("permits a re-point to a different user-secret key", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: { type: "user_secret_ref", key: "OTHER" } }),
|
||||
priorConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("permits a re-point to a company-secret reference", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({
|
||||
CLAUDE_CODE_OAUTH_TOKEN: { type: "secret_ref", secretId: randomUUID(), version: "latest" },
|
||||
}),
|
||||
priorConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("permits a removal of the fixed binding by a move to another adapter type", () => {
|
||||
// The prior config has the fixed binding on claude_local. The write moves
|
||||
// the agent to the process adapter and drops the binding in the same write.
|
||||
// The binding behaves like a normal environment variable, so the write is
|
||||
// allowed and reports no fixed binding.
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "process",
|
||||
nextConfig: withEnv({}),
|
||||
priorConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING }),
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("does nothing for a non-claude_local create with no prior fixed binding", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "process",
|
||||
nextConfig: withEnv({}),
|
||||
priorConfig: withEnv({ SOME_OTHER_KEY: { type: "plain", value: "x" } }),
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("rejects the fixed binding together with a plain ANTHROPIC_API_KEY and names no value", () => {
|
||||
let thrown: Error | null = null;
|
||||
try {
|
||||
assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({
|
||||
CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING,
|
||||
ANTHROPIC_API_KEY: { type: "plain", value: "sk-ant-secret" },
|
||||
}),
|
||||
priorConfig: null,
|
||||
});
|
||||
} catch (error) {
|
||||
thrown = error as Error;
|
||||
}
|
||||
expect(thrown?.message).toBe(CLAUDE_OAUTH_CREDENTIAL_CONFLICT);
|
||||
expect(thrown?.message).not.toContain("sk-ant-secret");
|
||||
expect(thrown?.message).not.toContain("ANTHROPIC_API_KEY");
|
||||
});
|
||||
|
||||
it("rejects the fixed binding together with an ANTHROPIC_API_KEY secret reference", () => {
|
||||
expect(() =>
|
||||
assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({
|
||||
CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING,
|
||||
ANTHROPIC_API_KEY: { type: "secret_ref", secretId: randomUUID(), version: "latest" },
|
||||
}),
|
||||
priorConfig: null,
|
||||
}),
|
||||
).toThrowError(CLAUDE_OAUTH_CREDENTIAL_CONFLICT);
|
||||
});
|
||||
|
||||
it("ignores an empty ANTHROPIC_API_KEY plain value", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "claude_local",
|
||||
nextConfig: withEnv({
|
||||
CLAUDE_CODE_OAUTH_TOKEN: FIXED_BINDING,
|
||||
ANTHROPIC_API_KEY: { type: "plain", value: " " },
|
||||
}),
|
||||
priorConfig: null,
|
||||
});
|
||||
expect(decision.introducesBinding).toBe(true);
|
||||
});
|
||||
|
||||
it("does nothing for a non-claude_local adapter", () => {
|
||||
const decision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: "codex_local",
|
||||
nextConfig: withEnv({ CLAUDE_CODE_OAUTH_TOKEN: { type: "plain", value: "anything" } }),
|
||||
priorConfig: null,
|
||||
});
|
||||
expect(decision).toEqual({ introducesBinding: false, keepsBinding: false });
|
||||
});
|
||||
|
||||
it("recognizes only the exact fixed binding shape", () => {
|
||||
expect(isFixedClaudeOAuthBinding(FIXED_BINDING)).toBe(true);
|
||||
expect(isFixedClaudeOAuthBinding({ type: "plain", value: "x" })).toBe(false);
|
||||
expect(isFixedClaudeOAuthBinding({ type: "user_secret_ref", key: "OTHER" })).toBe(false);
|
||||
expect(isFixedClaudeOAuthBinding(null)).toBe(false);
|
||||
});
|
||||
|
||||
it("raises a fixed 409 claim error", () => {
|
||||
const error = claudeOAuthClaimRejectedError();
|
||||
expect(error.status).toBe(409);
|
||||
expect(error.message).toBe(CLAUDE_OAUTH_CLAIM_REJECTED);
|
||||
});
|
||||
});
|
||||
|
||||
// --- The stored-session claim on the create and hire paths (Postgres) --------
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
|
||||
if (!embeddedPostgresSupport.supported) {
|
||||
console.warn(
|
||||
`Skipping Claude OAuth binding claim tests on this host: ${
|
||||
embeddedPostgresSupport.reason ?? "unsupported environment"
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
describeEmbeddedPostgres("agent service Claude OAuth binding claim", () => {
|
||||
let stopDb: (() => Promise<void>) | null = null;
|
||||
let connectionString!: string;
|
||||
let db!: ReturnType<typeof createDb>;
|
||||
const previousKeyFile = process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE;
|
||||
const secretsTmpDir = path.join(os.tmpdir(), `paperclip-claude-oauth-binding-${randomUUID()}`);
|
||||
|
||||
beforeAll(async () => {
|
||||
mkdirSync(secretsTmpDir, { recursive: true });
|
||||
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = path.join(secretsTmpDir, "master.key");
|
||||
const started = await startEmbeddedPostgresTestDatabase("claude-oauth-binding");
|
||||
stopDb = started.cleanup;
|
||||
connectionString = started.connectionString;
|
||||
db = createDb(connectionString);
|
||||
}, 20_000);
|
||||
|
||||
afterEach(async () => {
|
||||
await db.delete(companySecretBindings);
|
||||
await db.delete(companySecretVersions);
|
||||
await db.delete(companySecrets);
|
||||
await db.delete(companySecretProviderConfigs);
|
||||
await db.delete(userSecretDeclarations);
|
||||
await db.delete(userSecretDefinitions);
|
||||
await db.delete(claudeSetupTokenSessions);
|
||||
await db.delete(agents);
|
||||
await db.delete(environments);
|
||||
await db.delete(companies);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await stopDb?.();
|
||||
if (previousKeyFile === undefined) {
|
||||
delete process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE;
|
||||
} else {
|
||||
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = previousKeyFile;
|
||||
}
|
||||
rmSync(secretsTmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
interface Scope {
|
||||
companyId: string;
|
||||
environmentId: string;
|
||||
ownerUserId: string;
|
||||
}
|
||||
|
||||
async function seedScope(): Promise<Scope> {
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Paperclip",
|
||||
issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`,
|
||||
requireBoardApprovalForNewAgents: false,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: `sandbox-${environmentId.slice(0, 8)}`,
|
||||
driver: "sandbox",
|
||||
status: "active",
|
||||
config: { provider: "fake" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
return { companyId, environmentId, ownerUserId: `user-${randomUUID().slice(0, 8)}` };
|
||||
}
|
||||
|
||||
async function seedStoredClaim(scope: Scope, deadlineMs: number, state = "stored"): Promise<string> {
|
||||
const sessionId = randomUUID();
|
||||
await createDbSetupTokenCleanupStore(db).record({
|
||||
sessionId,
|
||||
companyId: scope.companyId,
|
||||
ownerUserId: scope.ownerUserId,
|
||||
adapterType: "claude_local",
|
||||
environmentId: scope.environmentId,
|
||||
leaseId: "lease-1",
|
||||
deadline: deadlineMs,
|
||||
state: state as never,
|
||||
boundAt: null,
|
||||
});
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
function createInput(scope: Scope, extraEnv: Record<string, unknown> = {}) {
|
||||
return {
|
||||
name: "Claude Login Agent",
|
||||
role: "engineer",
|
||||
status: "idle" as const,
|
||||
adapterType: "claude_local",
|
||||
defaultEnvironmentId: scope.environmentId,
|
||||
adapterConfig: { env: { CLAUDE_CODE_OAUTH_TOKEN: { ...FIXED_BINDING }, ...extraEnv } },
|
||||
runtimeConfig: {},
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
async function readClaim(sessionId: string) {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(claudeSetupTokenSessions)
|
||||
.where(eq(claudeSetupTokenSessions.sessionId, sessionId));
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
async function countAgents(companyId: string): Promise<number> {
|
||||
const rows = await db.select().from(agents).where(eq(agents.companyId, companyId));
|
||||
return rows.length;
|
||||
}
|
||||
|
||||
// Seed one stored owner value for the fixed Claude OAuth definition. The
|
||||
// apply-existing path binds the fixed reference only when this value exists.
|
||||
async function seedStoredOwnerValue(scope: Scope, value = "sk-owner-token"): Promise<void> {
|
||||
await secretService(db).completeClaudeOAuthUserSecret(scope.companyId, scope.ownerUserId, {
|
||||
sessionId: randomUUID(),
|
||||
mode: "first_write",
|
||||
value,
|
||||
});
|
||||
}
|
||||
|
||||
async function countUserSecretDefinitions(companyId: string): Promise<number> {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(userSecretDefinitions)
|
||||
.where(eq(userSecretDefinitions.companyId, companyId));
|
||||
return rows.length;
|
||||
}
|
||||
|
||||
async function countDeclarationsForAgent(agentId: string): Promise<number> {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(userSecretDeclarations)
|
||||
.where(eq(userSecretDeclarations.targetId, agentId));
|
||||
return rows.length;
|
||||
}
|
||||
|
||||
it("inserts the fixed binding and consumes a valid claim exactly once", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
|
||||
const created = await agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
});
|
||||
|
||||
const persisted = created.adapterConfig as { env: Record<string, unknown> };
|
||||
expect(persisted.env.CLAUDE_CODE_OAUTH_TOKEN).toMatchObject({
|
||||
type: "user_secret_ref",
|
||||
key: "CLAUDE_CODE_OAUTH_TOKEN",
|
||||
});
|
||||
// The claim is consumed once.
|
||||
expect((await readClaim(sessionId))?.boundAt).not.toBeNull();
|
||||
|
||||
// A replay of the same claim inserts no second agent.
|
||||
await expect(
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
expect(await countAgents(scope.companyId)).toBe(1);
|
||||
});
|
||||
|
||||
it("rejects a missing claim and inserts no binding", async () => {
|
||||
const scope = await seedScope();
|
||||
await expect(
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: undefined, ownerUserId: scope.ownerUserId },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
expect(await countAgents(scope.companyId)).toBe(0);
|
||||
});
|
||||
|
||||
it("rejects a foreign-owner claim and leaves it unconsumed", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
await expect(
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: "intruder" },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
expect((await readClaim(sessionId))?.boundAt).toBeNull();
|
||||
expect(await countAgents(scope.companyId)).toBe(0);
|
||||
});
|
||||
|
||||
it("rejects an expired claim and leaves it unconsumed", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() - 1_000);
|
||||
await expect(
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
expect((await readClaim(sessionId))?.boundAt).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects a non-stored claim and leaves it unconsumed", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000, "awaiting_code");
|
||||
await expect(
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
expect((await readClaim(sessionId))?.boundAt).toBeNull();
|
||||
});
|
||||
|
||||
it("returns a byte-identical error for every reject reason", async () => {
|
||||
const messages: string[] = [];
|
||||
// Missing claim.
|
||||
const missingScope = await seedScope();
|
||||
messages.push(
|
||||
await agentService(db)
|
||||
.create(missingScope.companyId, createInput(missingScope), {
|
||||
claudeLogin: { storedSessionId: undefined, ownerUserId: missingScope.ownerUserId },
|
||||
})
|
||||
.then(() => "no-error")
|
||||
.catch((error: Error) => error.message),
|
||||
);
|
||||
// Foreign owner.
|
||||
const foreignScope = await seedScope();
|
||||
const foreignSession = await seedStoredClaim(foreignScope, Date.now() + 60_000);
|
||||
messages.push(
|
||||
await agentService(db)
|
||||
.create(foreignScope.companyId, createInput(foreignScope), {
|
||||
claudeLogin: { storedSessionId: foreignSession, ownerUserId: "intruder" },
|
||||
})
|
||||
.then(() => "no-error")
|
||||
.catch((error: Error) => error.message),
|
||||
);
|
||||
// Expired.
|
||||
const expiredScope = await seedScope();
|
||||
const expiredSession = await seedStoredClaim(expiredScope, Date.now() - 1_000);
|
||||
messages.push(
|
||||
await agentService(db)
|
||||
.create(expiredScope.companyId, createInput(expiredScope), {
|
||||
claudeLogin: { storedSessionId: expiredSession, ownerUserId: expiredScope.ownerUserId },
|
||||
})
|
||||
.then(() => "no-error")
|
||||
.catch((error: Error) => error.message),
|
||||
);
|
||||
// Non-stored.
|
||||
const nonStoredScope = await seedScope();
|
||||
const nonStoredSession = await seedStoredClaim(nonStoredScope, Date.now() + 60_000, "submitting");
|
||||
messages.push(
|
||||
await agentService(db)
|
||||
.create(nonStoredScope.companyId, createInput(nonStoredScope), {
|
||||
claudeLogin: { storedSessionId: nonStoredSession, ownerUserId: nonStoredScope.ownerUserId },
|
||||
})
|
||||
.then(() => "no-error")
|
||||
.catch((error: Error) => error.message),
|
||||
);
|
||||
// Already consumed.
|
||||
const consumedScope = await seedScope();
|
||||
const consumedSession = await seedStoredClaim(consumedScope, Date.now() + 60_000);
|
||||
await agentService(db).create(consumedScope.companyId, createInput(consumedScope), {
|
||||
claudeLogin: { storedSessionId: consumedSession, ownerUserId: consumedScope.ownerUserId },
|
||||
});
|
||||
messages.push(
|
||||
await agentService(db)
|
||||
.create(consumedScope.companyId, createInput(consumedScope), {
|
||||
claudeLogin: { storedSessionId: consumedSession, ownerUserId: consumedScope.ownerUserId },
|
||||
})
|
||||
.then(() => "no-error")
|
||||
.catch((error: Error) => error.message),
|
||||
);
|
||||
|
||||
expect(new Set(messages)).toEqual(new Set([CLAUDE_OAUTH_CLAIM_REJECTED]));
|
||||
});
|
||||
|
||||
it("consumes one claim exactly once under two concurrent creates", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
|
||||
const results = await Promise.allSettled([
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
}),
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
}),
|
||||
]);
|
||||
|
||||
const fulfilled = results.filter((result) => result.status === "fulfilled");
|
||||
const rejected = results.filter((result) => result.status === "rejected");
|
||||
expect(fulfilled).toHaveLength(1);
|
||||
expect(rejected).toHaveLength(1);
|
||||
expect((rejected[0] as PromiseRejectedResult).reason.message).toBe(CLAUDE_OAUTH_CLAIM_REJECTED);
|
||||
expect(await countAgents(scope.companyId)).toBe(1);
|
||||
});
|
||||
|
||||
it("inserts no binding when the claim row lock holds until after the deadline", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 3_600_000);
|
||||
|
||||
const lockDb = createDb(connectionString);
|
||||
let signalLocked!: () => void;
|
||||
const locked = new Promise<void>((resolve) => {
|
||||
signalLocked = resolve;
|
||||
});
|
||||
let releaseGate!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
releaseGate = resolve;
|
||||
});
|
||||
const lockHeld = lockDb.transaction(async (tx) => {
|
||||
await tx.execute(
|
||||
sql`SELECT bound_at FROM claude_setup_token_sessions WHERE session_id = ${sessionId} FOR UPDATE`,
|
||||
);
|
||||
await tx.execute(
|
||||
sql`UPDATE claude_setup_token_sessions SET deadline_at = clock_timestamp() + interval '300 milliseconds' WHERE session_id = ${sessionId}`,
|
||||
);
|
||||
signalLocked();
|
||||
await gate;
|
||||
});
|
||||
|
||||
await locked;
|
||||
const createPromise = agentService(db)
|
||||
.create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
})
|
||||
.then(() => "created")
|
||||
.catch((error: Error) => error.message);
|
||||
await new Promise((resolve) => setTimeout(resolve, 600));
|
||||
releaseGate();
|
||||
await lockHeld;
|
||||
|
||||
expect(await createPromise).toBe(CLAUDE_OAUTH_CLAIM_REJECTED);
|
||||
expect((await readClaim(sessionId))?.boundAt).toBeNull();
|
||||
expect(await countAgents(scope.companyId)).toBe(0);
|
||||
await lockDb.$client.end();
|
||||
});
|
||||
|
||||
it("removes the fixed binding on a normal update", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
const created = await agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
});
|
||||
|
||||
const updated = await agentService(db).update(created.id, { adapterConfig: { env: {} } });
|
||||
|
||||
const updatedEnv = (updated?.adapterConfig as { env: Record<string, unknown> }).env;
|
||||
expect(updatedEnv.CLAUDE_CODE_OAUTH_TOKEN).toBeUndefined();
|
||||
});
|
||||
|
||||
it("re-points the binding to a plain value on a normal update", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
const created = await agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
});
|
||||
|
||||
const updated = await agentService(db).update(created.id, {
|
||||
adapterConfig: { env: { CLAUDE_CODE_OAUTH_TOKEN: { type: "plain", value: "sk-fake" } } },
|
||||
});
|
||||
|
||||
const updatedEnv = (updated?.adapterConfig as { env: Record<string, unknown> }).env;
|
||||
expect(updatedEnv.CLAUDE_CODE_OAUTH_TOKEN).toMatchObject({ type: "plain", value: "sk-fake" });
|
||||
});
|
||||
|
||||
it("removes the binding by a move to another adapter type on a normal update", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
const created = await agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
});
|
||||
|
||||
// The write moves the agent to the process adapter and drops the binding in
|
||||
// the same PATCH. The binding is a normal environment variable, so the write
|
||||
// succeeds and drops both the fixed binding and the claude_local adapter.
|
||||
const updated = await agentService(db).update(created.id, {
|
||||
adapterType: "process",
|
||||
adapterConfig: { env: {} },
|
||||
});
|
||||
|
||||
expect(updated?.adapterType).toBe("process");
|
||||
const updatedEnv = (updated?.adapterConfig as { env: Record<string, unknown> }).env;
|
||||
expect(updatedEnv.CLAUDE_CODE_OAUTH_TOKEN).toBeUndefined();
|
||||
});
|
||||
|
||||
it("keeps an existing binding when the update changes another field", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
const created = await agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { storedSessionId: sessionId, ownerUserId: scope.ownerUserId },
|
||||
});
|
||||
|
||||
const updated = await agentService(db).update(created.id, {
|
||||
adapterConfig: { model: "claude-opus", env: { CLAUDE_CODE_OAUTH_TOKEN: { ...FIXED_BINDING } } },
|
||||
});
|
||||
const env = (updated?.adapterConfig as { env: Record<string, unknown> }).env;
|
||||
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toMatchObject(FIXED_BINDING);
|
||||
});
|
||||
|
||||
it("rejects a newly introduced binding on update, because it carries no claim", async () => {
|
||||
const scope = await seedScope();
|
||||
const created = await agentService(db).create(scope.companyId, {
|
||||
name: "Api Key Agent",
|
||||
role: "engineer",
|
||||
status: "idle",
|
||||
adapterType: "claude_local",
|
||||
defaultEnvironmentId: scope.environmentId,
|
||||
adapterConfig: { env: {} },
|
||||
runtimeConfig: {},
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
});
|
||||
|
||||
await expect(
|
||||
agentService(db).update(created.id, {
|
||||
adapterConfig: { env: { CLAUDE_CODE_OAUTH_TOKEN: { ...FIXED_BINDING } } },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
|
||||
const reloaded = await agentService(db).getById(created.id);
|
||||
const reloadedEnv = (reloaded?.adapterConfig as { env: Record<string, unknown> }).env;
|
||||
expect(reloadedEnv.CLAUDE_CODE_OAUTH_TOKEN).toBeUndefined();
|
||||
});
|
||||
|
||||
// --- The user-actor apply-existing path (no login round trip) --------------
|
||||
|
||||
it("binds the fixed reference from a stored owner value with no claim", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope);
|
||||
|
||||
const created = await agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { ownerUserId: scope.ownerUserId, applyExistingWithoutClaim: true },
|
||||
});
|
||||
|
||||
const persisted = created.adapterConfig as { env: Record<string, unknown> };
|
||||
expect(persisted.env.CLAUDE_CODE_OAUTH_TOKEN).toMatchObject({
|
||||
type: "user_secret_ref",
|
||||
key: "CLAUDE_CODE_OAUTH_TOKEN",
|
||||
});
|
||||
// The response carries the reference only. It carries no token value.
|
||||
expect(JSON.stringify(created)).not.toContain("sk-owner-token");
|
||||
expect(await countAgents(scope.companyId)).toBe(1);
|
||||
// The path consumes no setup-token claim, so it needs no login round trip.
|
||||
const claims = await db.select().from(claudeSetupTokenSessions);
|
||||
expect(claims).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("binds on a hire-shaped create that needs board approval", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope);
|
||||
|
||||
const created = await agentService(
|
||||
db,
|
||||
).create(scope.companyId, { ...createInput(scope), status: "pending_approval" }, {
|
||||
claudeLogin: { ownerUserId: scope.ownerUserId, applyExistingWithoutClaim: true },
|
||||
});
|
||||
|
||||
expect(created.status).toBe("pending_approval");
|
||||
const persisted = created.adapterConfig as { env: Record<string, unknown> };
|
||||
expect(persisted.env.CLAUDE_CODE_OAUTH_TOKEN).toMatchObject(FIXED_BINDING);
|
||||
});
|
||||
|
||||
it("binds the fixed reference on an update from a stored owner value with no claim", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope);
|
||||
const created = await agentService(db).create(scope.companyId, {
|
||||
name: "Claude Agent",
|
||||
role: "engineer",
|
||||
status: "idle",
|
||||
adapterType: "claude_local",
|
||||
defaultEnvironmentId: scope.environmentId,
|
||||
adapterConfig: { env: {} },
|
||||
runtimeConfig: {},
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
});
|
||||
|
||||
const updated = await agentService(db).update(
|
||||
created.id,
|
||||
{ adapterConfig: { env: { CLAUDE_CODE_OAUTH_TOKEN: { ...FIXED_BINDING } } } },
|
||||
{ claudeLogin: { ownerUserId: scope.ownerUserId, applyExistingWithoutClaim: true } },
|
||||
);
|
||||
|
||||
const env = (updated?.adapterConfig as { env: Record<string, unknown> }).env;
|
||||
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toMatchObject(FIXED_BINDING);
|
||||
});
|
||||
|
||||
it("rejects apply-existing when the owner has no stored value and binds nothing", async () => {
|
||||
const scope = await seedScope();
|
||||
|
||||
await expect(
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { ownerUserId: scope.ownerUserId, applyExistingWithoutClaim: true },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
|
||||
// A rejected bind creates neither the agent nor the fixed definition.
|
||||
expect(await countAgents(scope.companyId)).toBe(0);
|
||||
expect(await countUserSecretDefinitions(scope.companyId)).toBe(0);
|
||||
});
|
||||
|
||||
it("rejects apply-existing when no owner is derived (an agent actor)", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope);
|
||||
|
||||
// The route sets no owner for an agent actor. The gate rejects the no-claim
|
||||
// bind, so an agent actor never binds the fixed reference.
|
||||
await expect(
|
||||
agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { ownerUserId: null, applyExistingWithoutClaim: true },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
expect(await countAgents(scope.companyId)).toBe(0);
|
||||
});
|
||||
|
||||
it("rejects apply-existing when the stored value belongs to another company", async () => {
|
||||
const ownerScope = await seedScope();
|
||||
await seedStoredOwnerValue(ownerScope);
|
||||
// The same owner, a different company. The owner value is company-scoped, so
|
||||
// the status read finds nothing for the foreign company.
|
||||
const foreignScope = await seedScope();
|
||||
foreignScope.ownerUserId = ownerScope.ownerUserId;
|
||||
|
||||
await expect(
|
||||
agentService(db).create(foreignScope.companyId, createInput(foreignScope), {
|
||||
claudeLogin: { ownerUserId: foreignScope.ownerUserId, applyExistingWithoutClaim: true },
|
||||
}),
|
||||
).rejects.toMatchObject({ message: CLAUDE_OAUTH_CLAIM_REJECTED });
|
||||
expect(await countAgents(foreignScope.companyId)).toBe(0);
|
||||
});
|
||||
|
||||
it("binds nothing from the flag alone when the config carries no fixed binding", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope);
|
||||
|
||||
// The apply-existing flag is set, but the config carries no fixed binding. The
|
||||
// gate mints the fixed reference only when the client presents the exact fixed
|
||||
// binding, so the flag alone binds nothing and creates no fixed definition.
|
||||
const created = await agentService(db).create(
|
||||
scope.companyId,
|
||||
{
|
||||
name: "Plain Agent",
|
||||
role: "engineer",
|
||||
status: "idle",
|
||||
adapterType: "claude_local",
|
||||
defaultEnvironmentId: scope.environmentId,
|
||||
adapterConfig: { env: {} },
|
||||
runtimeConfig: {},
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
},
|
||||
{ claudeLogin: { ownerUserId: scope.ownerUserId, applyExistingWithoutClaim: true } },
|
||||
);
|
||||
|
||||
const env = (created.adapterConfig as { env: Record<string, unknown> }).env;
|
||||
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBeUndefined();
|
||||
expect(await countDeclarationsForAgent(created.id)).toBe(0);
|
||||
});
|
||||
|
||||
it("derives the persisted binding shape, so a client secret selector never rides through", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope);
|
||||
|
||||
// A client adds a secret id and a foreign owner to the binding. The persist
|
||||
// normalization keeps only the fixed reference fields, so the extra selectors
|
||||
// never reach the stored binding. The runtime derives the owner from context.
|
||||
const created = await agentService(db).create(
|
||||
scope.companyId,
|
||||
createInput(scope, {
|
||||
CLAUDE_CODE_OAUTH_TOKEN: {
|
||||
type: "user_secret_ref",
|
||||
key: "CLAUDE_CODE_OAUTH_TOKEN",
|
||||
secretId: "attacker-secret",
|
||||
ownerUserId: "intruder",
|
||||
},
|
||||
}),
|
||||
{ claudeLogin: { ownerUserId: scope.ownerUserId, applyExistingWithoutClaim: true } },
|
||||
);
|
||||
|
||||
const binding = (created.adapterConfig as { env: Record<string, Record<string, unknown>> }).env
|
||||
.CLAUDE_CODE_OAUTH_TOKEN;
|
||||
expect(binding).toMatchObject({ type: "user_secret_ref", key: "CLAUDE_CODE_OAUTH_TOKEN" });
|
||||
expect(binding.secretId).toBeUndefined();
|
||||
expect(binding.ownerUserId).toBeUndefined();
|
||||
});
|
||||
|
||||
it("resolves the bound reference to the stored value without leaking it in a log", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope, "sk-secret-resolve");
|
||||
const created = await agentService(db).create(scope.companyId, createInput(scope), {
|
||||
claudeLogin: { ownerUserId: scope.ownerUserId, applyExistingWithoutClaim: true },
|
||||
});
|
||||
|
||||
// The successful bind writes a declaration for the agent, so the fixed
|
||||
// reference resolves at runtime. The declaration and the definition carry no
|
||||
// token value.
|
||||
expect(await countDeclarationsForAgent(created.id)).toBe(1);
|
||||
const definitions = await db.select().from(userSecretDefinitions);
|
||||
expect(JSON.stringify(definitions)).not.toContain("sk-secret-resolve");
|
||||
});
|
||||
|
||||
// --- The atomic credential-claim writer (item 2) ---------------------------
|
||||
|
||||
function claimScope(scope: Scope): SetupTokenSessionScope {
|
||||
return {
|
||||
companyId: scope.companyId,
|
||||
ownerUserId: scope.ownerUserId,
|
||||
targetAgentId: null,
|
||||
adapterType: "claude_local",
|
||||
environmentId: scope.environmentId,
|
||||
};
|
||||
}
|
||||
|
||||
it("commits the stored transition and the secret together on a first write", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000, "submitting");
|
||||
const writer = createSetupTokenSecretWriter({ db });
|
||||
|
||||
await writer({ scope: claimScope(scope), sessionId, token: "sk-ant-oat01-FIRSTWRITE" });
|
||||
|
||||
// The durable row transitioned to `stored` and stays unconsumed.
|
||||
const claim = await readClaim(sessionId);
|
||||
expect(claim?.state).toBe("stored");
|
||||
expect(claim?.boundAt).toBeNull();
|
||||
// The owner value committed in the same transaction.
|
||||
const status = await secretService(db).readClaudeOAuthUserSecretStatus(
|
||||
scope.companyId,
|
||||
scope.ownerUserId,
|
||||
);
|
||||
expect(status).not.toBeNull();
|
||||
expect(status?.latestVersion).toBe(1);
|
||||
});
|
||||
|
||||
it("rolls back the transition and writes no secret when the secret write fails", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000, "submitting");
|
||||
// Inject a completion that fails after the transition. The whole transaction
|
||||
// rolls back, so neither the claim nor the secret commits.
|
||||
const writer = createSetupTokenSecretWriter({
|
||||
db,
|
||||
completionForTx: () => ({
|
||||
async completeClaudeOAuthUserSecret() {
|
||||
throw new Error("storage down");
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
await expect(
|
||||
writer({ scope: claimScope(scope), sessionId, token: "sk-ant-oat01-ROLLBACK" }),
|
||||
).rejects.toThrow();
|
||||
|
||||
const claim = await readClaim(sessionId);
|
||||
expect(claim?.state).toBe("submitting");
|
||||
expect(claim?.boundAt).toBeNull();
|
||||
expect(
|
||||
await secretService(db).readClaudeOAuthUserSecretStatus(scope.companyId, scope.ownerUserId),
|
||||
).toBeNull();
|
||||
expect(await countUserSecretDefinitions(scope.companyId)).toBe(0);
|
||||
});
|
||||
|
||||
it("writes no secret for an expired claim", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() - 1_000, "submitting");
|
||||
const writer = createSetupTokenSecretWriter({ db });
|
||||
|
||||
await expect(
|
||||
writer({ scope: claimScope(scope), sessionId, token: "sk-ant-oat01-EXPIRED" }),
|
||||
).rejects.toThrow();
|
||||
|
||||
expect((await readClaim(sessionId))?.state).toBe("submitting");
|
||||
expect(
|
||||
await secretService(db).readClaudeOAuthUserSecretStatus(scope.companyId, scope.ownerUserId),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("writes no secret for an already-consumed claim", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
// Consume the stored claim first, so `bound_at` is set and the predecessor
|
||||
// predicate no longer matches.
|
||||
await createDbSetupTokenCleanupStore(db).consumeStoredClaim({
|
||||
sessionId,
|
||||
companyId: scope.companyId,
|
||||
ownerUserId: scope.ownerUserId,
|
||||
adapterType: "claude_local",
|
||||
environmentId: scope.environmentId,
|
||||
});
|
||||
const writer = createSetupTokenSecretWriter({ db });
|
||||
|
||||
await expect(
|
||||
writer({ scope: claimScope(scope), sessionId, token: "sk-ant-oat01-BOUND" }),
|
||||
).rejects.toThrow();
|
||||
|
||||
expect(
|
||||
await secretService(db).readClaudeOAuthUserSecretStatus(scope.companyId, scope.ownerUserId),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("rotates the stored value under the captured version on a confirmed overwrite", async () => {
|
||||
const scope = await seedScope();
|
||||
await seedStoredOwnerValue(scope, "sk-old-value");
|
||||
const before = await secretService(db).readClaudeOAuthUserSecretStatus(
|
||||
scope.companyId,
|
||||
scope.ownerUserId,
|
||||
);
|
||||
expect(before).not.toBeNull();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000, "submitting");
|
||||
const writer = createSetupTokenSecretWriter({ db });
|
||||
|
||||
await writer({
|
||||
scope: {
|
||||
...claimScope(scope),
|
||||
confirmedOverwrite: {
|
||||
expectedSecretId: before!.secretId,
|
||||
expectedLatestVersion: before!.latestVersion,
|
||||
},
|
||||
},
|
||||
sessionId,
|
||||
token: "sk-ant-oat01-ROTATED",
|
||||
});
|
||||
|
||||
// The claim transitioned and the stored value rotated to a new version.
|
||||
expect((await readClaim(sessionId))?.state).toBe("stored");
|
||||
const after = await secretService(db).readClaudeOAuthUserSecretStatus(
|
||||
scope.companyId,
|
||||
scope.ownerUserId,
|
||||
);
|
||||
expect(after?.secretId).toBe(before!.secretId);
|
||||
expect(after?.latestVersion).toBe(before!.latestVersion + 1);
|
||||
});
|
||||
|
||||
it("deletes a cleanup row only under the full owner scope", async () => {
|
||||
const scope = await seedScope();
|
||||
const sessionId = await seedStoredClaim(scope, Date.now() + 60_000);
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
const identity = {
|
||||
sessionId,
|
||||
companyId: scope.companyId,
|
||||
ownerUserId: scope.ownerUserId,
|
||||
adapterType: "claude_local",
|
||||
environmentId: scope.environmentId,
|
||||
};
|
||||
|
||||
// A foreign-owner identity with the right session id does not delete the row.
|
||||
await store.remove({ ...identity, ownerUserId: "intruder" });
|
||||
expect(await readClaim(sessionId)).toBeTruthy();
|
||||
|
||||
// The full-scope identity deletes the row.
|
||||
await store.remove(identity);
|
||||
expect(await readClaim(sessionId)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -196,32 +196,72 @@ describe("resolveBundledCatalogRoot", () => {
|
||||
// ensureBundledPlugins (fail-safe installer)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type LooseRow = {
|
||||
id: string;
|
||||
pluginKey: string;
|
||||
status: string;
|
||||
version?: string;
|
||||
manifestJson?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
// Build a minimal manifest for a persisted row or a shipped bundle. The reconcile
|
||||
// step compares the bundle version with the persisted version.
|
||||
function makeManifest(pluginKey: string, version: string) {
|
||||
return { id: pluginKey, apiVersion: 1, version } as unknown as import("@paperclipai/shared").PaperclipPluginManifestV1;
|
||||
}
|
||||
|
||||
function makeDeps(overrides?: {
|
||||
rows?: Record<string, { id: string; pluginKey: string; status: string } | null>;
|
||||
rows?: Record<string, LooseRow | null>;
|
||||
bundleManifestExists?: (localPath: string) => boolean;
|
||||
installError?: Error;
|
||||
/** Version the shipped bundle manifest reports, keyed by pluginKey. */
|
||||
bundleVersionByKey?: Record<string, string>;
|
||||
}) {
|
||||
const rows = overrides?.rows ?? {};
|
||||
const installedRows = new Map(Object.entries(rows));
|
||||
const normalize = (row: LooseRow | null): (LooseRow & { version: string; manifestJson: Record<string, unknown> }) | null => {
|
||||
if (!row) return null;
|
||||
const version = row.version ?? "0.1.0";
|
||||
return {
|
||||
...row,
|
||||
version,
|
||||
manifestJson: row.manifestJson ?? { id: row.pluginKey, apiVersion: 1, version },
|
||||
};
|
||||
};
|
||||
const installedRows = new Map(
|
||||
Object.entries(rows).map(([key, row]) => [key, normalize(row)] as const),
|
||||
);
|
||||
const installPlugin = vi.fn(async ({ localPath }: { localPath: string }) => {
|
||||
if (overrides?.installError) throw overrides.installError;
|
||||
const entry = BUNDLED_PLUGIN_CATALOG.find((candidate) =>
|
||||
localPath.endsWith(candidate.relativePath),
|
||||
);
|
||||
const pluginKey = entry?.pluginKey ?? "unknown";
|
||||
installedRows.set(pluginKey, { id: `id-${pluginKey}`, pluginKey, status: "installed" });
|
||||
installedRows.set(pluginKey, normalize({ id: `id-${pluginKey}`, pluginKey, status: "installed" }));
|
||||
return { manifest: { id: pluginKey } };
|
||||
});
|
||||
const update = vi.fn(async () => undefined);
|
||||
const loadManifest = vi.fn(async (localPath: string) => {
|
||||
const entry = BUNDLED_PLUGIN_CATALOG.find((candidate) =>
|
||||
localPath.endsWith(candidate.relativePath),
|
||||
);
|
||||
const pluginKey = entry?.pluginKey ?? "unknown";
|
||||
const persisted = installedRows.get(pluginKey);
|
||||
// Default: the bundle version matches the persisted version, so no
|
||||
// reconcile fires unless a test overrides `bundleVersionByKey`.
|
||||
const version = overrides?.bundleVersionByKey?.[pluginKey] ?? persisted?.version ?? "0.1.0";
|
||||
return makeManifest(pluginKey, version);
|
||||
});
|
||||
const deps: BundledPluginProvisionerDeps = {
|
||||
registry: {
|
||||
getByKey: vi.fn(async (pluginKey: string) => installedRows.get(pluginKey) ?? null),
|
||||
},
|
||||
loader: { installPlugin } as unknown as BundledPluginProvisionerDeps["loader"],
|
||||
update,
|
||||
} as unknown as BundledPluginProvisionerDeps["registry"],
|
||||
loader: { installPlugin, loadManifest } as unknown as BundledPluginProvisionerDeps["loader"],
|
||||
lifecycle: { load: vi.fn(async () => undefined) },
|
||||
logger: { info: vi.fn(), error: vi.fn() },
|
||||
bundleManifestExists: overrides?.bundleManifestExists ?? (() => true),
|
||||
};
|
||||
return { deps, installPlugin };
|
||||
return { deps, installPlugin, update, loadManifest };
|
||||
}
|
||||
|
||||
const K8S: ResolvedBundledPlugin = {
|
||||
@@ -258,6 +298,64 @@ describe("ensureBundledPlugins", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("reconciles the persisted manifest of a present plugin when the bundle version changed", async () => {
|
||||
const { deps, installPlugin, update } = makeDeps({
|
||||
rows: {
|
||||
[DAYTONA.pluginKey]: {
|
||||
id: "row-daytona",
|
||||
pluginKey: DAYTONA.pluginKey,
|
||||
status: "ready",
|
||||
version: "0.1.1",
|
||||
},
|
||||
},
|
||||
bundleVersionByKey: { [DAYTONA.pluginKey]: "0.1.2" },
|
||||
});
|
||||
await ensureBundledPlugins([DAYTONA], deps, { reinstallUninstalled: false });
|
||||
// The plugin stays present, so it is never re-installed.
|
||||
expect(installPlugin).not.toHaveBeenCalled();
|
||||
// The persisted manifest is refreshed to the shipped bundle version, so a
|
||||
// capability added to the bundle reaches the existing install.
|
||||
expect(update).toHaveBeenCalledWith(
|
||||
"row-daytona",
|
||||
expect.objectContaining({ version: "0.1.2" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("does not reconcile when the persisted version already matches the bundle", async () => {
|
||||
const { deps, update } = makeDeps({
|
||||
rows: {
|
||||
[DAYTONA.pluginKey]: {
|
||||
id: "row-daytona",
|
||||
pluginKey: DAYTONA.pluginKey,
|
||||
status: "ready",
|
||||
version: "0.1.2",
|
||||
},
|
||||
},
|
||||
bundleVersionByKey: { [DAYTONA.pluginKey]: "0.1.2" },
|
||||
});
|
||||
await ensureBundledPlugins([DAYTONA], deps, { reinstallUninstalled: false });
|
||||
expect(update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("swallows a reconcile error and continues boot", async () => {
|
||||
const { deps, update } = makeDeps({
|
||||
rows: {
|
||||
[DAYTONA.pluginKey]: {
|
||||
id: "row-daytona",
|
||||
pluginKey: DAYTONA.pluginKey,
|
||||
status: "ready",
|
||||
version: "0.1.1",
|
||||
},
|
||||
},
|
||||
bundleVersionByKey: { [DAYTONA.pluginKey]: "0.1.2" },
|
||||
});
|
||||
(update as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error("db down"));
|
||||
await expect(
|
||||
ensureBundledPlugins([DAYTONA], deps, { reinstallUninstalled: false }),
|
||||
).resolves.toBeUndefined();
|
||||
expect(deps.logger.error).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reinstalls a soft-uninstalled plugin in managed mode", async () => {
|
||||
const { deps, installPlugin } = makeDeps({
|
||||
rows: {
|
||||
|
||||
@@ -39,6 +39,7 @@ const mockEnvironmentService = vi.hoisted(() => ({
|
||||
update: vi.fn(),
|
||||
removeIfDeletable: vi.fn(),
|
||||
getDeleteBlastRadius: vi.fn(),
|
||||
hasUnresolvedPendingCleanupLeases: vi.fn(),
|
||||
listLeases: vi.fn(),
|
||||
getLeaseById: vi.fn(),
|
||||
}));
|
||||
@@ -150,6 +151,8 @@ function createDeleteBlastRadius(overrides: Partial<{
|
||||
secretBindingCount: number;
|
||||
activeLeaseCount: number;
|
||||
activeCustomImageSetupSessionCount: number;
|
||||
pendingCleanupLeaseCount: number;
|
||||
reusableSandboxLeaseCount: number;
|
||||
}> = {}) {
|
||||
const staticReferences = {
|
||||
isManagedLocal: overrides.isManagedLocal ?? false,
|
||||
@@ -167,14 +170,20 @@ function createDeleteBlastRadius(overrides: Partial<{
|
||||
(overrides.activeLeaseCount ?? 0) > 0
|
||||
|| (overrides.activeCustomImageSetupSessionCount ?? 0) > 0,
|
||||
};
|
||||
const pendingCleanupLeaseCount = overrides.pendingCleanupLeaseCount ?? 0;
|
||||
const reusableSandboxLeaseCount = overrides.reusableSandboxLeaseCount ?? 0;
|
||||
const deleteBlockedReasons = [
|
||||
...(staticReferences.isManagedLocal ? ["managed_local" as const] : []),
|
||||
...(staticReferences.isInstanceDefault ? ["instance_default" as const] : []),
|
||||
...(pendingCleanupLeaseCount > 0 ? ["pending_sandbox_cleanup" as const] : []),
|
||||
...(reusableSandboxLeaseCount > 0 ? ["reusable_sandbox_lease" as const] : []),
|
||||
];
|
||||
return {
|
||||
environmentId: "env-1",
|
||||
canDelete: deleteBlockedReasons.length === 0,
|
||||
deleteBlockedReasons,
|
||||
pendingCleanupLeaseCount,
|
||||
reusableSandboxLeaseCount,
|
||||
staticReferences,
|
||||
activeRuntimeUse,
|
||||
};
|
||||
@@ -255,6 +264,8 @@ describe("environment routes", () => {
|
||||
mockEnvironmentService.update.mockReset();
|
||||
mockEnvironmentService.removeIfDeletable.mockReset();
|
||||
mockEnvironmentService.getDeleteBlastRadius.mockReset();
|
||||
mockEnvironmentService.hasUnresolvedPendingCleanupLeases.mockReset();
|
||||
mockEnvironmentService.hasUnresolvedPendingCleanupLeases.mockResolvedValue(false);
|
||||
mockEnvironmentService.listLeases.mockReset();
|
||||
mockEnvironmentService.getLeaseById.mockReset();
|
||||
mockExecutionWorkspaceService.clearEnvironmentSelection.mockReset();
|
||||
@@ -1078,6 +1089,8 @@ describe("environment routes", () => {
|
||||
environmentId: "env-1",
|
||||
canDelete: true,
|
||||
deleteBlockedReasons: [],
|
||||
pendingCleanupLeaseCount: 0,
|
||||
reusableSandboxLeaseCount: 0,
|
||||
staticReferences: {
|
||||
isManagedLocal: false,
|
||||
isInstanceDefault: false,
|
||||
@@ -1522,6 +1535,153 @@ describe("environment routes", () => {
|
||||
expect(mockEnvironmentService.removeIfDeletable).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects deleting an environment with a pending sandbox cleanup", async () => {
|
||||
const environment = {
|
||||
...createEnvironment(),
|
||||
driver: "ssh" as const,
|
||||
name: "SSH Fixture",
|
||||
config: {
|
||||
host: "ssh.example.test",
|
||||
port: 22,
|
||||
username: "ssh-user",
|
||||
remoteWorkspacePath: "/srv/paperclip/workspace",
|
||||
privateKey: null,
|
||||
privateKeySecretRef: null,
|
||||
knownHosts: null,
|
||||
strictHostKeyChecking: true,
|
||||
},
|
||||
};
|
||||
mockEnvironmentService.getById.mockResolvedValue(environment);
|
||||
mockEnvironmentService.getDeleteBlastRadius.mockResolvedValue(createDeleteBlastRadius({
|
||||
pendingCleanupLeaseCount: 1,
|
||||
}));
|
||||
const app = createApp({
|
||||
type: "board",
|
||||
userId: "admin-1",
|
||||
source: "local_implicit",
|
||||
});
|
||||
|
||||
const res = await request(app).delete("/api/environments/env-1");
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error).toBe(
|
||||
"Cannot delete this environment while a sandbox cleanup is pending. Wait for the cleanup sweep to destroy the orphan sandbox, then retry.",
|
||||
);
|
||||
expect(res.body.details).toEqual({ deleteBlockedReasons: ["pending_sandbox_cleanup"] });
|
||||
expect(mockEnvironmentService.removeIfDeletable).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects deleting an environment with a live reusable sandbox lease", async () => {
|
||||
const environment = {
|
||||
...createEnvironment(),
|
||||
driver: "sandbox" as const,
|
||||
name: "Reusable Sandbox Fixture",
|
||||
config: {
|
||||
provider: "fake-plugin",
|
||||
image: "fixture:test",
|
||||
reuseLease: true,
|
||||
},
|
||||
};
|
||||
mockEnvironmentService.getById.mockResolvedValue(environment);
|
||||
mockEnvironmentService.getDeleteBlastRadius.mockResolvedValue(createDeleteBlastRadius({
|
||||
activeLeaseCount: 1,
|
||||
reusableSandboxLeaseCount: 1,
|
||||
}));
|
||||
const app = createApp({
|
||||
type: "board",
|
||||
userId: "admin-1",
|
||||
source: "local_implicit",
|
||||
});
|
||||
|
||||
const res = await request(app).delete("/api/environments/env-1");
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error).toBe(
|
||||
"Cannot delete this environment while it has a reusable sandbox lease. Remove the associated execution workspace or issue so Paperclip can destroy the sandbox, then retry.",
|
||||
);
|
||||
expect(res.body.details).toEqual({ deleteBlockedReasons: ["reusable_sandbox_lease"] });
|
||||
expect(mockEnvironmentService.removeIfDeletable).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects a driver or provider config change while a sandbox cleanup is pending", async () => {
|
||||
const environment = {
|
||||
...createEnvironment(),
|
||||
id: "env-ssh",
|
||||
driver: "ssh" as const,
|
||||
name: "SSH Fixture",
|
||||
config: {
|
||||
host: "ssh.example.test",
|
||||
port: 22,
|
||||
username: "ssh-user",
|
||||
remoteWorkspacePath: "/srv/paperclip/workspace",
|
||||
privateKey: null,
|
||||
privateKeySecretRef: null,
|
||||
knownHosts: null,
|
||||
strictHostKeyChecking: true,
|
||||
},
|
||||
};
|
||||
mockEnvironmentService.getById.mockResolvedValue(environment);
|
||||
mockEnvironmentService.hasUnresolvedPendingCleanupLeases.mockResolvedValue(true);
|
||||
const app = createApp({
|
||||
type: "board",
|
||||
userId: "admin-1",
|
||||
source: "local_implicit",
|
||||
});
|
||||
|
||||
const res = await request(app)
|
||||
.patch("/api/environments/env-ssh")
|
||||
.send({
|
||||
config: {
|
||||
host: "changed.example.test",
|
||||
port: 22,
|
||||
username: "ssh-user",
|
||||
remoteWorkspacePath: "/srv/paperclip/workspace",
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error).toBe(
|
||||
"Cannot change the driver or provider config while a sandbox cleanup is pending. Wait for the cleanup sweep to destroy the orphan sandbox, then retry.",
|
||||
);
|
||||
expect(res.body.details).toEqual({ code: "environment_pending_sandbox_cleanup" });
|
||||
expect(mockEnvironmentService.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows a non-provider update while a sandbox cleanup is pending", async () => {
|
||||
const environment = {
|
||||
...createEnvironment(),
|
||||
id: "env-ssh",
|
||||
driver: "ssh" as const,
|
||||
name: "SSH Fixture",
|
||||
config: {
|
||||
host: "ssh.example.test",
|
||||
port: 22,
|
||||
username: "ssh-user",
|
||||
remoteWorkspacePath: "/srv/paperclip/workspace",
|
||||
privateKey: null,
|
||||
privateKeySecretRef: null,
|
||||
knownHosts: null,
|
||||
strictHostKeyChecking: true,
|
||||
},
|
||||
};
|
||||
mockEnvironmentService.getById.mockResolvedValue(environment);
|
||||
mockEnvironmentService.hasUnresolvedPendingCleanupLeases.mockResolvedValue(true);
|
||||
mockEnvironmentService.update.mockResolvedValue({ ...environment, description: "Updated" });
|
||||
const app = createApp({
|
||||
type: "board",
|
||||
userId: "admin-1",
|
||||
source: "local_implicit",
|
||||
});
|
||||
|
||||
const res = await request(app)
|
||||
.patch("/api/environments/env-ssh")
|
||||
.send({ description: "Updated" });
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(mockEnvironmentService.hasUnresolvedPendingCleanupLeases).not.toHaveBeenCalled();
|
||||
expect(mockEnvironmentService.update).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("describes an environment's secret refs with owner metadata", async () => {
|
||||
const secretId = "22222222-2222-2222-2222-222222222222";
|
||||
mockEnvironmentService.getById.mockResolvedValue({
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -397,6 +397,8 @@ describeEmbeddedPostgres("environmentService leases", () => {
|
||||
environmentId,
|
||||
canDelete: false,
|
||||
deleteBlockedReasons: ["instance_default"],
|
||||
pendingCleanupLeaseCount: 0,
|
||||
reusableSandboxLeaseCount: 0,
|
||||
staticReferences: {
|
||||
isManagedLocal: false,
|
||||
isInstanceDefault: true,
|
||||
@@ -488,6 +490,374 @@ describeEmbeddedPostgres("environmentService leases", () => {
|
||||
expect(deletedRows).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("blocks delete while a pending_cleanup lease still holds the sandbox reference", async () => {
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
const now = new Date();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: "Pending Cleanup Guard",
|
||||
driver: "ssh",
|
||||
status: "active",
|
||||
config: {
|
||||
host: "pending.example.test",
|
||||
port: 22,
|
||||
username: "fixture",
|
||||
remoteWorkspacePath: "/srv/paperclip",
|
||||
},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await svc.insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
provider: "fake-plugin",
|
||||
providerLeaseId: "sandbox-1",
|
||||
failureReason: "teardown_failed",
|
||||
});
|
||||
|
||||
const impact = await svc.getDeleteBlastRadius(environmentId);
|
||||
expect(impact?.canDelete).toBe(false);
|
||||
expect(impact?.deleteBlockedReasons).toContain("pending_sandbox_cleanup");
|
||||
expect(impact?.pendingCleanupLeaseCount).toBe(1);
|
||||
expect(await svc.hasUnresolvedPendingCleanupLeases(environmentId)).toBe(true);
|
||||
|
||||
const removed = await svc.removeIfDeletable(environmentId);
|
||||
const rows = await db.select().from(environments).where(eq(environments.id, environmentId));
|
||||
expect(removed).toBeNull();
|
||||
expect(rows).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("atomically blocks delete while a reusable sandbox lease is still live", async () => {
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
const now = new Date();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: "Reusable Sandbox Guard",
|
||||
driver: "sandbox",
|
||||
status: "active",
|
||||
config: {
|
||||
provider: "fake",
|
||||
image: "ubuntu:24.04",
|
||||
reuseLease: true,
|
||||
},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
const lease = await svc.acquireLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
leasePolicy: "reuse_by_environment",
|
||||
provider: "fake",
|
||||
providerLeaseId: "sandbox-reusable-1",
|
||||
metadata: {
|
||||
driver: "sandbox",
|
||||
provider: "fake",
|
||||
image: "ubuntu:24.04",
|
||||
reuseLease: true,
|
||||
},
|
||||
});
|
||||
|
||||
const activeImpact = await svc.getDeleteBlastRadius(environmentId);
|
||||
expect(activeImpact?.canDelete).toBe(false);
|
||||
expect(activeImpact?.deleteBlockedReasons).toContain("reusable_sandbox_lease");
|
||||
expect(activeImpact?.reusableSandboxLeaseCount).toBe(1);
|
||||
expect(await svc.removeIfDeletable(environmentId)).toBeNull();
|
||||
|
||||
// A released reusable lease still owns a provider sandbox that may be
|
||||
// resumed, so it must remain protected until scoped cleanup destroys it.
|
||||
await svc.releaseLease(lease.id, "released");
|
||||
const releasedImpact = await svc.getDeleteBlastRadius(environmentId);
|
||||
expect(releasedImpact?.canDelete).toBe(false);
|
||||
expect(releasedImpact?.reusableSandboxLeaseCount).toBe(1);
|
||||
expect(await svc.removeIfDeletable(environmentId)).toBeNull();
|
||||
|
||||
const rows = await db.select().from(environments).where(eq(environments.id, environmentId));
|
||||
expect(rows).toHaveLength(1);
|
||||
const storedLease = await svc.getLeaseById(lease.id);
|
||||
expect(storedLease?.environmentId).toBe(environmentId);
|
||||
});
|
||||
|
||||
it("allows delete with an active ephemeral lease", async () => {
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
const now = new Date();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: "Ephemeral Sandbox",
|
||||
driver: "sandbox",
|
||||
status: "active",
|
||||
config: { provider: "fake", image: "ubuntu:24.04" },
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await svc.acquireLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
leasePolicy: "ephemeral",
|
||||
provider: "fake",
|
||||
providerLeaseId: "sandbox-ephemeral-1",
|
||||
});
|
||||
|
||||
const impact = await svc.getDeleteBlastRadius(environmentId);
|
||||
expect(impact?.activeRuntimeUse.activeLeaseCount).toBe(1);
|
||||
expect(impact?.reusableSandboxLeaseCount).toBe(0);
|
||||
expect(impact?.canDelete).toBe(true);
|
||||
expect((await svc.removeIfDeletable(environmentId))?.id).toBe(environmentId);
|
||||
});
|
||||
|
||||
it("allows delete once the pending_cleanup lease resolves", async () => {
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
const now = new Date();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: "Resolved Cleanup Guard",
|
||||
driver: "ssh",
|
||||
status: "active",
|
||||
config: {
|
||||
host: "resolved.example.test",
|
||||
port: 22,
|
||||
username: "fixture",
|
||||
remoteWorkspacePath: "/srv/paperclip",
|
||||
},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
const lease = await svc.insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
provider: "fake-plugin",
|
||||
providerLeaseId: "sandbox-1",
|
||||
failureReason: "teardown_failed",
|
||||
});
|
||||
// The sweep marks a cleaned orphan `expired`, so it leaves the terminal
|
||||
// `pending_cleanup` state and no longer blocks the delete.
|
||||
await svc.releaseLease(lease.id, "expired", { cleanupStatus: "success" });
|
||||
|
||||
const impact = await svc.getDeleteBlastRadius(environmentId);
|
||||
expect(impact?.canDelete).toBe(true);
|
||||
expect(impact?.pendingCleanupLeaseCount).toBe(0);
|
||||
expect(impact?.reusableSandboxLeaseCount).toBe(0);
|
||||
expect(await svc.hasUnresolvedPendingCleanupLeases(environmentId)).toBe(false);
|
||||
|
||||
const removed = await svc.removeIfDeletable(environmentId);
|
||||
const rows = await db.select().from(environments).where(eq(environments.id, environmentId));
|
||||
expect(removed?.id).toBe(environmentId);
|
||||
expect(rows).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("records the orphan with a null reference when a delete already removed the environment", async () => {
|
||||
// A delete wins the untracked window before the orphan record lands. The
|
||||
// record must still persist, so the sweep keeps a durable handle to the
|
||||
// remote sandbox.
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
const now = new Date();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: "Deleted Before Record",
|
||||
driver: "ssh",
|
||||
status: "active",
|
||||
config: {
|
||||
host: "deleted.example.test",
|
||||
port: 22,
|
||||
username: "fixture",
|
||||
remoteWorkspacePath: "/srv/paperclip",
|
||||
},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
|
||||
const removed = await svc.removeIfDeletable(environmentId);
|
||||
expect(removed?.id).toBe(environmentId);
|
||||
|
||||
// The foreign key does not reject the insert. The insert reads the absent
|
||||
// environment and stores a null reference with the immutable provider
|
||||
// metadata, so the teardown still runs later.
|
||||
const lease = await svc.insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
provider: "fake-plugin",
|
||||
providerLeaseId: "sandbox-orphan-1",
|
||||
metadata: { provider: "fake-plugin", config: { provider: "fake-plugin" } },
|
||||
failureReason: "acquire_rejected_teardown_failed",
|
||||
});
|
||||
|
||||
expect(lease.environmentId).toBeNull();
|
||||
expect(lease.status).toBe("pending_cleanup");
|
||||
expect(lease.provider).toBe("fake-plugin");
|
||||
expect(lease.providerLeaseId).toBe("sandbox-orphan-1");
|
||||
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, lease.id));
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]?.environmentId).toBeNull();
|
||||
expect(rows[0]?.metadata).toMatchObject({ provider: "fake-plugin" });
|
||||
});
|
||||
|
||||
it("keeps the orphan when an environment delete and the orphan record race", async () => {
|
||||
// Race the delete against the orphan record. The record locks the
|
||||
// environment row `for update`, and the delete refuses while a
|
||||
// `pending_cleanup` lease exists, so the two writes serialize into one of
|
||||
// two safe outcomes. The orphan row persists in both.
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
const now = new Date();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: "Delete Vs Record Race",
|
||||
driver: "ssh",
|
||||
status: "active",
|
||||
config: {
|
||||
host: "race.example.test",
|
||||
port: 22,
|
||||
username: "fixture",
|
||||
remoteWorkspacePath: "/srv/paperclip",
|
||||
},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
|
||||
const [removed, lease] = await Promise.all([
|
||||
svc.removeIfDeletable(environmentId),
|
||||
svc.insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
provider: "fake-plugin",
|
||||
providerLeaseId: "sandbox-orphan-2",
|
||||
metadata: { provider: "fake-plugin", config: { provider: "fake-plugin" } },
|
||||
failureReason: "acquire_rejected_teardown_failed",
|
||||
}),
|
||||
]);
|
||||
|
||||
// The orphan row always persists, so no ordering strands the sandbox.
|
||||
const leaseRows = await db
|
||||
.select()
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.status, "pending_cleanup"));
|
||||
expect(leaseRows).toHaveLength(1);
|
||||
expect(leaseRows[0]?.id).toBe(lease.id);
|
||||
expect(leaseRows[0]?.provider).toBe("fake-plugin");
|
||||
|
||||
const environmentRows = await db
|
||||
.select()
|
||||
.from(environments)
|
||||
.where(eq(environments.id, environmentId));
|
||||
if (removed) {
|
||||
// The delete won. The environment is gone, and the orphan keeps a null
|
||||
// reference with its immutable provider metadata.
|
||||
expect(environmentRows).toHaveLength(0);
|
||||
expect(leaseRows[0]?.environmentId).toBeNull();
|
||||
} else {
|
||||
// The record won. The orphan keeps the environment reference, and the
|
||||
// delete refused because the `pending_cleanup` lease now exists.
|
||||
expect(environmentRows).toHaveLength(1);
|
||||
expect(leaseRows[0]?.environmentId).toBe(environmentId);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the recorded provider immutable when a provider change and the orphan record race", async () => {
|
||||
// Race a provider change against the orphan record for the original
|
||||
// provider. The recorded lease must keep the original provider and its
|
||||
// immutable teardown metadata, so a later retry targets the original
|
||||
// provider, never the reconfigured one.
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
const now = new Date();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: "Provider Change Vs Record Race",
|
||||
driver: "sandbox",
|
||||
status: "active",
|
||||
config: { provider: "provider-a" },
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
});
|
||||
|
||||
const [, lease] = await Promise.all([
|
||||
svc.update(environmentId, {
|
||||
driver: "sandbox",
|
||||
config: { provider: "provider-b" },
|
||||
}),
|
||||
svc.insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
provider: "provider-a",
|
||||
providerLeaseId: "sandbox-orphan-3",
|
||||
metadata: { provider: "provider-a", config: { provider: "provider-a" } },
|
||||
failureReason: "acquire_rejected_teardown_failed",
|
||||
}),
|
||||
]);
|
||||
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, lease.id));
|
||||
expect(rows[0]?.provider).toBe("provider-a");
|
||||
expect(rows[0]?.metadata).toMatchObject({ provider: "provider-a" });
|
||||
|
||||
// The environment now points at the reconfigured provider, but the recorded
|
||||
// teardown context stays independent of it.
|
||||
const environmentRows = await db
|
||||
.select()
|
||||
.from(environments)
|
||||
.where(eq(environments.id, environmentId));
|
||||
expect((environmentRows[0]?.config as { provider?: string } | null)?.provider).toBe("provider-b");
|
||||
});
|
||||
|
||||
it("creates and then reuses the default local environment for a company", async () => {
|
||||
const companyId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
@@ -783,6 +1153,47 @@ describeEmbeddedPostgres("environmentService leases", () => {
|
||||
expect(rows).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("lists the companies that own a managed sandbox environment", async () => {
|
||||
const companyA = randomUUID();
|
||||
const companyB = randomUUID();
|
||||
await db.insert(companies).values([
|
||||
{ id: companyA, name: "Company A", status: "active", issuePrefix: "CA", createdAt: new Date(), updatedAt: new Date() },
|
||||
{ id: companyB, name: "Company B", status: "active", issuePrefix: "CB", createdAt: new Date(), updatedAt: new Date() },
|
||||
]);
|
||||
// Two companies provision the same managed sandbox slot, so both bind to the
|
||||
// one environment row.
|
||||
const created = await svc.ensureManagedSandboxEnvironment({
|
||||
companyId: companyA,
|
||||
name: "Daytona",
|
||||
provider: "daytona",
|
||||
config: { target: "us" },
|
||||
});
|
||||
const environmentId = created.environment.id;
|
||||
await svc.ensureManagedSandboxEnvironment({
|
||||
companyId: companyB,
|
||||
name: "Daytona",
|
||||
provider: "daytona",
|
||||
config: { target: "us" },
|
||||
});
|
||||
|
||||
const owners = await svc.listBoundCompanyIds(environmentId);
|
||||
expect(owners.slice().sort()).toEqual([companyA, companyB].sort());
|
||||
|
||||
// An operator-created environment has no managed binding, so it lists no
|
||||
// owner and stays instance-global.
|
||||
const unboundId = randomUUID();
|
||||
await db.insert(environments).values({
|
||||
id: unboundId,
|
||||
name: "Operator sandbox",
|
||||
driver: "sandbox",
|
||||
status: "active",
|
||||
config: { provider: "kubernetes" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
expect(await svc.listBoundCompanyIds(unboundId)).toEqual([]);
|
||||
});
|
||||
|
||||
it("preserves tenant env vars across managed sandbox boot reconciles", async () => {
|
||||
// The cloud contract lets tenants add env vars — and only env vars —
|
||||
// to the managed sandbox environment. The provisioner reconciles
|
||||
@@ -1348,4 +1759,104 @@ describeEmbeddedPostgres("environmentService leases", () => {
|
||||
const rows = await db.select().from(environments);
|
||||
expect(rows.filter((row) => row.driver === "ssh")).toHaveLength(2);
|
||||
});
|
||||
|
||||
describe("acquireLease company-binding guard", () => {
|
||||
async function seedSandboxEnvironment(name = "Managed Sandbox") {
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name,
|
||||
status: "active",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name,
|
||||
driver: "sandbox",
|
||||
status: "active",
|
||||
config: { provider: "daytona" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
return { companyId, environmentId };
|
||||
}
|
||||
|
||||
async function bindEnvironmentToCompany(environmentId: string, companyId: string) {
|
||||
await db.insert(builtInManagedResources).values({
|
||||
companyId,
|
||||
bundleKey: "managed-environment",
|
||||
resourceKind: "environment",
|
||||
resourceKey: "managed-sandbox",
|
||||
resourceId: environmentId,
|
||||
stockVersion: "1",
|
||||
stockHash: "hash",
|
||||
});
|
||||
}
|
||||
|
||||
it("rejects an acquire when a bind lands after the route check", async () => {
|
||||
const { companyId, environmentId } = await seedSandboxEnvironment();
|
||||
// The other company owns a managed binding that a reconciliation committed
|
||||
// after the route guard read an empty binding list.
|
||||
const otherCompanyId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: otherCompanyId,
|
||||
name: "Other Co",
|
||||
issuePrefix: "OTA",
|
||||
status: "active",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
await bindEnvironmentToCompany(environmentId, otherCompanyId);
|
||||
|
||||
await expect(
|
||||
svc.acquireLease({ companyId, environmentId, assertCompanyBinding: true }),
|
||||
).rejects.toMatchObject({
|
||||
status: 403,
|
||||
details: { code: "environment_company_mismatch" },
|
||||
});
|
||||
// The insert never ran, so the caller holds no lease.
|
||||
const leaseRows = await db
|
||||
.select()
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.environmentId, environmentId));
|
||||
expect(leaseRows).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("acquires when the environment is unbound (instance-global)", async () => {
|
||||
const { companyId, environmentId } = await seedSandboxEnvironment("Unbound Sandbox");
|
||||
|
||||
const lease = await svc.acquireLease({ companyId, environmentId, assertCompanyBinding: true });
|
||||
expect(lease.status).toBe("active");
|
||||
expect(lease.companyId).toBe(companyId);
|
||||
});
|
||||
|
||||
it("acquires when the environment is bound to the caller company", async () => {
|
||||
const { companyId, environmentId } = await seedSandboxEnvironment("Own Sandbox");
|
||||
await bindEnvironmentToCompany(environmentId, companyId);
|
||||
|
||||
const lease = await svc.acquireLease({ companyId, environmentId, assertCompanyBinding: true });
|
||||
expect(lease.status).toBe("active");
|
||||
});
|
||||
|
||||
it("does not check the binding for callers that omit the flag", async () => {
|
||||
const { companyId, environmentId } = await seedSandboxEnvironment("Legacy Path Sandbox");
|
||||
const otherCompanyId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: otherCompanyId,
|
||||
name: "Other Co 2",
|
||||
issuePrefix: "OTB",
|
||||
status: "active",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
await bindEnvironmentToCompany(environmentId, otherCompanyId);
|
||||
|
||||
// The heartbeat run path does not set the flag, so the binding check does
|
||||
// not run and the lease acquires as before.
|
||||
const lease = await svc.acquireLease({ companyId, environmentId });
|
||||
expect(lease.status).toBe("active");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,161 @@
|
||||
// Test worker fixture for the host-owned setup-token login pseudo-terminal route
|
||||
// gate. The fixture drives the manager route state machine through
|
||||
// the four typed methods (open, input, stop, close) and the output and exit
|
||||
// notifications.
|
||||
//
|
||||
// The manager allowlists `command` to the fixed `CLAUDE_SETUP_TOKEN_COMMAND`. The
|
||||
// test encodes a JSON directive in the forwarded `providerLeaseId`, so one fixture
|
||||
// serves every route-gate case:
|
||||
// - `mode`: "normal" | "malformed-open" | "no-open-reply" | "duplicate-open-reply"
|
||||
// - `workerSessionId`: the worker session id the open reply returns (default "ws-1")
|
||||
// - `outputs`: an array of `{ chunk, sid? }`. The fixture emits each as an output
|
||||
// notification after the open reply. `sid` defaults to the real worker session
|
||||
// id; a test sets a wrong `sid` to prove the host drops a mismatched
|
||||
// notification.
|
||||
// - `exitCode`: when set, the fixture emits an exit notification after the outputs.
|
||||
// - `closeMode`: "ack" | "bad-ack" | "no-ack" (default "ack"). It controls the
|
||||
// close reply, so a test proves the host retires the worker on an unconfirmed
|
||||
// close.
|
||||
const readline = require("node:readline");
|
||||
|
||||
function send(message) {
|
||||
process.stdout.write(`${JSON.stringify(message)}\n`);
|
||||
}
|
||||
|
||||
// The registered terminals, keyed by the host route id. Each entry records the
|
||||
// bound worker session id and the close directive.
|
||||
const routes = new Map();
|
||||
|
||||
function parseDirective(raw) {
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
return parsed && typeof parsed === "object" ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
|
||||
|
||||
rl.on("line", (line) => {
|
||||
if (!line.trim()) return;
|
||||
const message = JSON.parse(line);
|
||||
const method = message && typeof message.method === "string" ? message.method : null;
|
||||
const params = message.params ?? {};
|
||||
|
||||
if (method === "initialize") {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id: message.id,
|
||||
result: {
|
||||
ok: true,
|
||||
supportedMethods: [
|
||||
"setupTokenPtyOpen",
|
||||
"setupTokenPtyInput",
|
||||
"setupTokenPtyStop",
|
||||
"setupTokenPtyClose",
|
||||
],
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === "setupTokenPtyOpen") {
|
||||
const directive = parseDirective(params.providerLeaseId);
|
||||
const mode = directive.mode ?? "normal";
|
||||
const workerSessionId = directive.workerSessionId ?? "ws-1";
|
||||
const closeMode = directive.closeMode ?? "ack";
|
||||
routes.set(params.hostRouteId, { workerSessionId, closeMode });
|
||||
|
||||
if (mode === "no-open-reply") {
|
||||
// Never reply, so the host open call times out.
|
||||
return;
|
||||
}
|
||||
if (mode === "malformed-open") {
|
||||
// Reply with no worker session id, so the host terminalizes the route.
|
||||
send({ jsonrpc: "2.0", id: message.id, result: {} });
|
||||
return;
|
||||
}
|
||||
|
||||
const reply = () =>
|
||||
send({ jsonrpc: "2.0", id: message.id, result: { workerSessionId } });
|
||||
reply();
|
||||
if (mode === "duplicate-open-reply") {
|
||||
// Send a second open reply for the same request id. The host drops it.
|
||||
reply();
|
||||
}
|
||||
|
||||
// Emit the scripted output and the exit after the open reply, so the host
|
||||
// binds the route first.
|
||||
setImmediate(() => {
|
||||
const outputs = Array.isArray(directive.outputs) ? directive.outputs : [];
|
||||
for (const entry of outputs) {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
method: "setupTokenPty.output",
|
||||
params: {
|
||||
workerSessionId: entry.sid ?? workerSessionId,
|
||||
chunk: entry.chunk,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (typeof directive.exitCode === "number") {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
method: "setupTokenPty.exit",
|
||||
params: { workerSessionId, exitCode: directive.exitCode },
|
||||
});
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === "setupTokenPtyInput") {
|
||||
// Echo the input back as one output notification for the bound session, so a
|
||||
// test proves the input reaches the worker and the output routes back.
|
||||
for (const entry of routes.values()) {
|
||||
if (entry.workerSessionId === params.workerSessionId) {
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
method: "setupTokenPty.output",
|
||||
params: { workerSessionId: entry.workerSessionId, chunk: `echo:${params.data}` },
|
||||
});
|
||||
}
|
||||
}
|
||||
send({ jsonrpc: "2.0", id: message.id, result: null });
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === "setupTokenPtyStop") {
|
||||
send({ jsonrpc: "2.0", id: message.id, result: null });
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === "setupTokenPtyClose") {
|
||||
const entry = routes.get(params.hostRouteId);
|
||||
routes.delete(params.hostRouteId);
|
||||
const closeMode = entry ? entry.closeMode : "ack";
|
||||
if (closeMode === "no-ack") {
|
||||
// Never reply, so the host close call times out and the host retires us.
|
||||
return;
|
||||
}
|
||||
if (closeMode === "bad-ack") {
|
||||
send({ jsonrpc: "2.0", id: message.id, result: { hostRouteId: "mismatched-route" } });
|
||||
return;
|
||||
}
|
||||
send({ jsonrpc: "2.0", id: message.id, result: { hostRouteId: params.hostRouteId } });
|
||||
return;
|
||||
}
|
||||
|
||||
if (method === "shutdown") {
|
||||
send({ jsonrpc: "2.0", id: message.id, result: {} });
|
||||
setImmediate(() => process.exit(0));
|
||||
return;
|
||||
}
|
||||
|
||||
send({
|
||||
jsonrpc: "2.0",
|
||||
id: message.id,
|
||||
error: { code: -32601, message: `Unhandled method: ${method}` },
|
||||
});
|
||||
});
|
||||
@@ -49,6 +49,8 @@ if (!embeddedPostgresSupport.supported) {
|
||||
const ATTEMPTS_KEY = "pendingCleanupRetryAttempts";
|
||||
const CAP_WARNED_KEY = "pendingCleanupRetryCapWarned";
|
||||
const ATTEMPT_CAP = 5;
|
||||
// The sweep reads at most this many oldest pending_cleanup rows per tick.
|
||||
const SWEEP_PAGE_SIZE = 20;
|
||||
|
||||
describeEmbeddedPostgres("heartbeat sweepPendingCleanupLeases", () => {
|
||||
let db!: ReturnType<typeof createDb>;
|
||||
@@ -98,7 +100,7 @@ describeEmbeddedPostgres("heartbeat sweepPendingCleanupLeases", () => {
|
||||
|
||||
async function insertPendingCleanupLease(input: {
|
||||
companyId: string;
|
||||
environmentId: string;
|
||||
environmentId: string | null;
|
||||
updatedAt: Date;
|
||||
metadata?: Record<string, unknown>;
|
||||
}): Promise<string> {
|
||||
@@ -128,6 +130,34 @@ describeEmbeddedPostgres("heartbeat sweepPendingCleanupLeases", () => {
|
||||
return { destroyRunLease } as unknown as HeartbeatEnvironmentRuntime;
|
||||
}
|
||||
|
||||
// An orphan ephemeral lease that a failed acquire records. The sweep tears it
|
||||
// down through retryPendingSandboxTeardown, not through destroyRunLease.
|
||||
async function insertOrphanEphemeralLease(input: {
|
||||
companyId: string;
|
||||
environmentId: string | null;
|
||||
updatedAt: Date;
|
||||
metadata?: Record<string, unknown>;
|
||||
}): Promise<string> {
|
||||
const id = randomUUID();
|
||||
await db.insert(environmentLeases).values({
|
||||
id,
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environmentId,
|
||||
status: "pending_cleanup",
|
||||
leasePolicy: "ephemeral",
|
||||
provider: "fake",
|
||||
providerLeaseId: `sandbox://fake/${id}`,
|
||||
cleanupStatus: "failed",
|
||||
metadata: { driver: "sandbox", provider: "fake", ...(input.metadata ?? {}) },
|
||||
acquiredAt: input.updatedAt,
|
||||
lastUsedAt: input.updatedAt,
|
||||
releasedAt: input.updatedAt,
|
||||
createdAt: input.updatedAt,
|
||||
updatedAt: input.updatedAt,
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
it("test_pending_cleanup_sweep_retries_and_destroys_lease", async () => {
|
||||
const { companyId, environmentId } = await seedCompanyAndEnvironment();
|
||||
const leaseId = await insertPendingCleanupLease({
|
||||
@@ -301,6 +331,345 @@ describeEmbeddedPostgres("heartbeat sweepPendingCleanupLeases", () => {
|
||||
expect(metadata?.status).toBe("pending_cleanup");
|
||||
});
|
||||
|
||||
// Two unified sweeps can overlap on one orphan ephemeral lease. The master
|
||||
// sweep is the single owner of the pending_cleanup rows. Its atomic per-attempt
|
||||
// claim must let only one sweep tear the sandbox down, and the winning sweep
|
||||
// must leave the lease in a final successful state.
|
||||
it("test_concurrent_unified_sweeps_tear_an_orphan_down_once", async () => {
|
||||
const { companyId, environmentId } = await seedCompanyAndEnvironment();
|
||||
const leaseId = await insertOrphanEphemeralLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
updatedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||||
});
|
||||
|
||||
// The orphan teardown succeeds and does not release the lease; the sweep
|
||||
// releases it. The counter records how many sweeps reached the teardown.
|
||||
let teardownCount = 0;
|
||||
const retryPendingSandboxTeardown = vi.fn(async () => {
|
||||
teardownCount += 1;
|
||||
});
|
||||
const runtime = { retryPendingSandboxTeardown } as unknown as HeartbeatEnvironmentRuntime;
|
||||
|
||||
// Two sweeps run on two separate database clients, so they truly overlap.
|
||||
// A single client serializes the queries on one connection and hides the
|
||||
// race. The second client connects to the same embedded database.
|
||||
const dbB = createDb(tempDb!.connectionString);
|
||||
try {
|
||||
// Warm up the second connection first, so the two sweeps start together.
|
||||
await dbB.select({ id: environmentLeases.id }).from(environmentLeases).limit(1);
|
||||
|
||||
const heartbeatA = heartbeatService(db, { environmentRuntime: runtime });
|
||||
const heartbeatB = heartbeatService(dbB, { environmentRuntime: runtime });
|
||||
|
||||
const backoffMs = 5 * 60 * 1000;
|
||||
const [first, second] = await Promise.all([
|
||||
heartbeatA.sweepPendingCleanupLeases({ backoffMs }),
|
||||
heartbeatB.sweepPendingCleanupLeases({ backoffMs }),
|
||||
]);
|
||||
|
||||
// Only one sweep claimed the attempt and tore the sandbox down.
|
||||
expect(teardownCount).toBe(1);
|
||||
expect(first.destroyed + second.destroyed).toBe(1);
|
||||
} finally {
|
||||
await (dbB as unknown as { $client: { end: () => Promise<void> } }).$client.end();
|
||||
}
|
||||
|
||||
// The winning sweep left the lease in a final successful state and advanced
|
||||
// the attempt count by exactly one.
|
||||
const finalRow = await db
|
||||
.select({
|
||||
status: environmentLeases.status,
|
||||
cleanupStatus: environmentLeases.cleanupStatus,
|
||||
metadata: environmentLeases.metadata,
|
||||
})
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, leaseId))
|
||||
.then((rows) => rows[0]);
|
||||
expect(finalRow?.status).toBe("expired");
|
||||
expect(finalRow?.cleanupStatus).toBe("success");
|
||||
expect((finalRow?.metadata as Record<string, unknown> | null)?.[ATTEMPTS_KEY]).toBe(1);
|
||||
});
|
||||
|
||||
it("test_pending_cleanup_sweep_tears_down_reusable_lease_after_environment_delete", async () => {
|
||||
const { companyId } = await seedCompanyAndEnvironment();
|
||||
// A reuse_by_environment lease whose environment a delete removed. The
|
||||
// schema sets the environment reference to null and preserves the
|
||||
// pending_cleanup row, so the lease still points at a live provider sandbox.
|
||||
const leaseId = await insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId: null,
|
||||
updatedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||||
});
|
||||
|
||||
// The environment row is gone, so the sweep must tear the sandbox down from
|
||||
// the recorded lease data through retryPendingSandboxTeardown. It must not
|
||||
// call destroyRunLease, which needs the environment. The teardown succeeds
|
||||
// and does not release the lease; the sweep releases it.
|
||||
const retryPendingSandboxTeardown = vi.fn(async (input: { environment: unknown }) => {
|
||||
expect(input.environment).toBeNull();
|
||||
});
|
||||
const destroyRunLease = vi.fn(async () => null);
|
||||
const runtime = { retryPendingSandboxTeardown, destroyRunLease } as unknown as HeartbeatEnvironmentRuntime;
|
||||
const heartbeat = heartbeatService(db, { environmentRuntime: runtime });
|
||||
|
||||
const result = await heartbeat.sweepPendingCleanupLeases({ backoffMs: 0 });
|
||||
|
||||
expect(result).toEqual({ swept: 1, destroyed: 1, capped: 0 });
|
||||
expect(retryPendingSandboxTeardown).toHaveBeenCalledTimes(1);
|
||||
expect(retryPendingSandboxTeardown.mock.calls[0]?.[0]).toMatchObject({
|
||||
lease: expect.objectContaining({ id: leaseId }),
|
||||
});
|
||||
expect(destroyRunLease).not.toHaveBeenCalled();
|
||||
|
||||
const finalRow = await db
|
||||
.select({ status: environmentLeases.status, cleanupStatus: environmentLeases.cleanupStatus })
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, leaseId))
|
||||
.then((rows) => rows[0]);
|
||||
expect(finalRow?.status).toBe("expired");
|
||||
expect(finalRow?.cleanupStatus).toBe("success");
|
||||
});
|
||||
|
||||
it("test_pending_cleanup_sweep_keeps_reusable_lease_when_recorded_teardown_fails", async () => {
|
||||
const { companyId } = await seedCompanyAndEnvironment();
|
||||
const leaseId = await insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId: null,
|
||||
updatedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||||
});
|
||||
|
||||
// The recorded-data teardown throws, so the sweep reverts the lease to
|
||||
// pending_cleanup for a later retry. The claimed attempt still counts
|
||||
// against the cap, so the retries stay bounded.
|
||||
const retryPendingSandboxTeardown = vi.fn(async () => {
|
||||
throw new Error("provider teardown failed");
|
||||
});
|
||||
const destroyRunLease = vi.fn(async () => null);
|
||||
const runtime = { retryPendingSandboxTeardown, destroyRunLease } as unknown as HeartbeatEnvironmentRuntime;
|
||||
const heartbeat = heartbeatService(db, { environmentRuntime: runtime });
|
||||
|
||||
const result = await heartbeat.sweepPendingCleanupLeases({ backoffMs: 0 });
|
||||
|
||||
expect(result).toEqual({ swept: 1, destroyed: 0, capped: 0 });
|
||||
expect(retryPendingSandboxTeardown).toHaveBeenCalledTimes(1);
|
||||
expect(destroyRunLease).not.toHaveBeenCalled();
|
||||
|
||||
const finalRow = await db
|
||||
.select({
|
||||
status: environmentLeases.status,
|
||||
cleanupStatus: environmentLeases.cleanupStatus,
|
||||
metadata: environmentLeases.metadata,
|
||||
})
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, leaseId))
|
||||
.then((rows) => rows[0]);
|
||||
expect(finalRow?.status).toBe("pending_cleanup");
|
||||
expect(finalRow?.cleanupStatus).toBe("failed");
|
||||
expect((finalRow?.metadata as Record<string, unknown> | null)?.[ATTEMPTS_KEY]).toBe(1);
|
||||
});
|
||||
|
||||
it("test_pending_cleanup_sweep_skips_lease_while_plugin_worker_down_then_retries", async () => {
|
||||
const { companyId } = await seedCompanyAndEnvironment();
|
||||
// An orphan ephemeral lease that a failed plugin acquire recorded. The sweep
|
||||
// tears it down through retryPendingSandboxTeardown.
|
||||
const leaseId = await insertOrphanEphemeralLease({
|
||||
companyId,
|
||||
environmentId: null,
|
||||
updatedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||||
});
|
||||
|
||||
// The plugin worker is down at first, then recovers on the second sweep.
|
||||
let workerReady = false;
|
||||
const isPendingCleanupWorkerReady = vi.fn(async () => workerReady);
|
||||
const retryPendingSandboxTeardown = vi.fn(async () => {});
|
||||
const destroyRunLease = vi.fn(async () => null);
|
||||
const runtime = {
|
||||
isPendingCleanupWorkerReady,
|
||||
retryPendingSandboxTeardown,
|
||||
destroyRunLease,
|
||||
} as unknown as HeartbeatEnvironmentRuntime;
|
||||
const heartbeat = heartbeatService(db, { environmentRuntime: runtime });
|
||||
|
||||
// First sweep: the worker is down, so the sweep skips the lease. It never
|
||||
// claims an attempt and never runs the teardown.
|
||||
const first = await heartbeat.sweepPendingCleanupLeases({ backoffMs: 0 });
|
||||
expect(first).toEqual({ swept: 1, destroyed: 0, capped: 0 });
|
||||
expect(isPendingCleanupWorkerReady).toHaveBeenCalledTimes(1);
|
||||
expect(retryPendingSandboxTeardown).not.toHaveBeenCalled();
|
||||
// The skipped lease consumed no finite attempt.
|
||||
expect(await readAttempts(leaseId)).toBe(0);
|
||||
const afterSkip = await db
|
||||
.select({ status: environmentLeases.status })
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, leaseId))
|
||||
.then((rows) => rows[0]?.status);
|
||||
expect(afterSkip).toBe("pending_cleanup");
|
||||
|
||||
// Second sweep: the worker recovered, so the sweep claims an attempt and
|
||||
// tears the sandbox down.
|
||||
workerReady = true;
|
||||
const second = await heartbeat.sweepPendingCleanupLeases({ backoffMs: 0 });
|
||||
expect(second).toEqual({ swept: 1, destroyed: 1, capped: 0 });
|
||||
expect(retryPendingSandboxTeardown).toHaveBeenCalledTimes(1);
|
||||
|
||||
const finalRow = await db
|
||||
.select({ status: environmentLeases.status, cleanupStatus: environmentLeases.cleanupStatus })
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, leaseId))
|
||||
.then((rows) => rows[0]);
|
||||
expect(finalRow?.status).toBe("expired");
|
||||
expect(finalRow?.cleanupStatus).toBe("success");
|
||||
});
|
||||
|
||||
it("test_pending_cleanup_sweep_never_caps_while_provider_unavailable_then_cleans_up", async () => {
|
||||
const { companyId } = await seedCompanyAndEnvironment();
|
||||
// An orphan ephemeral lease that a failed plugin acquire recorded. The sweep
|
||||
// tears it down through retryPendingSandboxTeardown.
|
||||
const leaseId = await insertOrphanEphemeralLease({
|
||||
companyId,
|
||||
environmentId: null,
|
||||
updatedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||||
});
|
||||
|
||||
// The provider stays unavailable for more sweeps than the finite attempt
|
||||
// cap. A long plugin reload or a long worker restart looks like this. The
|
||||
// probe reports not ready every time, so no sweep claims an attempt.
|
||||
let providerReady = false;
|
||||
const isPendingCleanupWorkerReady = vi.fn(async () => providerReady);
|
||||
const retryPendingSandboxTeardown = vi.fn(async () => {});
|
||||
const runtime = {
|
||||
isPendingCleanupWorkerReady,
|
||||
retryPendingSandboxTeardown,
|
||||
} as unknown as HeartbeatEnvironmentRuntime;
|
||||
const heartbeat = heartbeatService(db, { environmentRuntime: runtime });
|
||||
|
||||
// Run one more sweep than the cap. A per-sweep claim would exhaust the cap
|
||||
// and strand the sandbox, so this proves the unavailable provider never
|
||||
// burns an attempt.
|
||||
for (let sweep = 0; sweep < ATTEMPT_CAP + 1; sweep += 1) {
|
||||
const result = await heartbeat.sweepPendingCleanupLeases({ backoffMs: 0 });
|
||||
// The lease is swept and skipped every time; it never caps and never
|
||||
// tears down while the provider is unavailable.
|
||||
expect(result).toEqual({ swept: 1, destroyed: 0, capped: 0 });
|
||||
}
|
||||
expect(retryPendingSandboxTeardown).not.toHaveBeenCalled();
|
||||
// The unavailable provider consumed no finite attempt across every sweep.
|
||||
expect(await readAttempts(leaseId)).toBe(0);
|
||||
const afterUnavailable = await db
|
||||
.select({ status: environmentLeases.status })
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, leaseId))
|
||||
.then((rows) => rows[0]?.status);
|
||||
expect(afterUnavailable).toBe("pending_cleanup");
|
||||
|
||||
// The provider recovers, so the next sweep claims the first attempt and
|
||||
// tears the sandbox down.
|
||||
providerReady = true;
|
||||
const recovered = await heartbeat.sweepPendingCleanupLeases({ backoffMs: 0 });
|
||||
expect(recovered).toEqual({ swept: 1, destroyed: 1, capped: 0 });
|
||||
expect(retryPendingSandboxTeardown).toHaveBeenCalledTimes(1);
|
||||
|
||||
const finalRow = await db
|
||||
.select({ status: environmentLeases.status, cleanupStatus: environmentLeases.cleanupStatus })
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, leaseId))
|
||||
.then((rows) => rows[0]);
|
||||
expect(finalRow?.status).toBe("expired");
|
||||
expect(finalRow?.cleanupStatus).toBe("success");
|
||||
});
|
||||
|
||||
it("test_pending_cleanup_sweep_does_not_let_unavailable_leases_starve_ready_leases", async () => {
|
||||
const { companyId, environmentId } = await seedCompanyAndEnvironment();
|
||||
|
||||
// Fill one whole page with the oldest leases, and make every one of them
|
||||
// unavailable. Their providers are not ready, so the sweep must not tear
|
||||
// them down.
|
||||
const unavailableIds = new Set<string>();
|
||||
const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000);
|
||||
for (let index = 0; index < SWEEP_PAGE_SIZE; index += 1) {
|
||||
const id = await insertOrphanEphemeralLease({
|
||||
companyId,
|
||||
environmentId: null,
|
||||
updatedAt: twoHoursAgo,
|
||||
});
|
||||
unavailableIds.add(id);
|
||||
}
|
||||
|
||||
// Add one newer lease with a ready provider. It sorts after the page of
|
||||
// unavailable leases, so the first sweep never reaches it.
|
||||
const readyLeaseId = await insertPendingCleanupLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
updatedAt: new Date(Date.now() - 30 * 60 * 1000),
|
||||
});
|
||||
|
||||
// The probe reports the ready lease's provider ready and every unavailable
|
||||
// lease's provider not ready.
|
||||
const isPendingCleanupWorkerReady = vi.fn(
|
||||
async ({ lease }: { lease: { id: string } }) => !unavailableIds.has(lease.id),
|
||||
);
|
||||
const retryPendingSandboxTeardown = vi.fn(async () => {});
|
||||
const destroyRunLease = vi.fn(async ({ lease }: { lease: { id: string } }) => {
|
||||
const now = new Date();
|
||||
const row = await db
|
||||
.update(environmentLeases)
|
||||
.set({ status: "expired", cleanupStatus: "success", updatedAt: now })
|
||||
.where(eq(environmentLeases.id, lease.id))
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
return row ? { ...row, status: "expired" as const } : null;
|
||||
});
|
||||
const runtime = {
|
||||
isPendingCleanupWorkerReady,
|
||||
retryPendingSandboxTeardown,
|
||||
destroyRunLease,
|
||||
} as unknown as HeartbeatEnvironmentRuntime;
|
||||
const heartbeat = heartbeatService(db, { environmentRuntime: runtime });
|
||||
|
||||
// A five-minute backoff keeps every seeded lease eligible, because each one
|
||||
// is older than the backoff. A deferred lease bumps its updatedAt to now,
|
||||
// so the backoff then excludes it from the next page.
|
||||
const backoffMs = 5 * 60 * 1000;
|
||||
|
||||
// First sweep: the page holds only the unavailable leases. The sweep defers
|
||||
// all of them and tears none down. The ready lease is not in this page.
|
||||
const first = await heartbeat.sweepPendingCleanupLeases({ backoffMs });
|
||||
expect(first).toEqual({ swept: SWEEP_PAGE_SIZE, destroyed: 0, capped: 0 });
|
||||
expect(retryPendingSandboxTeardown).not.toHaveBeenCalled();
|
||||
expect(destroyRunLease).not.toHaveBeenCalled();
|
||||
// The deferred leases consumed no finite attempt.
|
||||
for (const id of unavailableIds) {
|
||||
expect(await readAttempts(id)).toBe(0);
|
||||
}
|
||||
|
||||
// Second sweep: the deferred unavailable leases now sit inside the backoff
|
||||
// window, so the sweep skips them. The ready lease takes the page slot and
|
||||
// tears down. This proves the unavailable leases never starve the ready
|
||||
// lease.
|
||||
const second = await heartbeat.sweepPendingCleanupLeases({ backoffMs });
|
||||
expect(second.destroyed).toBe(1);
|
||||
expect(destroyRunLease).toHaveBeenCalledTimes(1);
|
||||
expect(destroyRunLease.mock.calls[0]?.[0]).toMatchObject({
|
||||
lease: expect.objectContaining({ id: readyLeaseId }),
|
||||
});
|
||||
|
||||
const readyStatus = await db
|
||||
.select({ status: environmentLeases.status })
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.id, readyLeaseId))
|
||||
.then((rows) => rows[0]?.status);
|
||||
expect(readyStatus).toBe("expired");
|
||||
|
||||
// The unavailable leases stay in pending_cleanup for a later retry.
|
||||
const stillPending = await db
|
||||
.select({ id: environmentLeases.id })
|
||||
.from(environmentLeases)
|
||||
.where(eq(environmentLeases.status, "pending_cleanup"))
|
||||
.then((rows) => rows.map((row) => row.id));
|
||||
expect(new Set(stillPending)).toEqual(unavailableIds);
|
||||
});
|
||||
|
||||
// Read the current retry attempt count from a lease's metadata.
|
||||
async function readAttempts(leaseId: string): Promise<number> {
|
||||
const metadata = await db
|
||||
@@ -938,4 +1307,40 @@ describeEmbeddedPostgres("heartbeat sweepPendingCleanupLeases", () => {
|
||||
const arrayMetadata = await readMetadata(arrayLeaseId);
|
||||
expect(arrayMetadata?.[ATTEMPTS_KEY]).toBe(1);
|
||||
});
|
||||
|
||||
it("flushes the in-process orphan buffer before the read, so a freshly landed row is swept the same tick", async () => {
|
||||
const { companyId, environmentId } = await seedCompanyAndEnvironment();
|
||||
|
||||
// The flush lands one durable orphan row, exactly as the runtime buffer does
|
||||
// after the database recovers. The sweep must run this flush before it reads
|
||||
// the rows, so the same tick tears the freshly-landed orphan down.
|
||||
const flushDeferredOrphanCleanups = vi.fn(async () => {
|
||||
await insertOrphanEphemeralLease({
|
||||
companyId,
|
||||
environmentId,
|
||||
updatedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||||
});
|
||||
return { recovered: 1, pending: 0 };
|
||||
});
|
||||
// The recorded-data teardown succeeds, so the sweep releases the lease.
|
||||
const retryPendingSandboxTeardown = vi.fn(async () => {});
|
||||
const runtime = {
|
||||
flushDeferredOrphanCleanups,
|
||||
retryPendingSandboxTeardown,
|
||||
} as unknown as HeartbeatEnvironmentRuntime;
|
||||
const heartbeat = heartbeatService(db, { environmentRuntime: runtime });
|
||||
|
||||
const result = await heartbeat.sweepPendingCleanupLeases({ backoffMs: 0 });
|
||||
|
||||
// The flush ran once before the read, so the row it landed is visible to the
|
||||
// same sweep and tears down through the recorded-data teardown path.
|
||||
expect(flushDeferredOrphanCleanups).toHaveBeenCalledTimes(1);
|
||||
expect(retryPendingSandboxTeardown).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual({ swept: 1, destroyed: 1, capped: 0 });
|
||||
|
||||
const rows = await db.select().from(environmentLeases);
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0]?.status).toBe("expired");
|
||||
expect(rows[0]?.cleanupStatus).toBe("success");
|
||||
});
|
||||
});
|
||||
@@ -74,6 +74,12 @@ const explicitOpenApiCoverageExclusions = new Set([
|
||||
"smoke-lab.ts",
|
||||
]);
|
||||
|
||||
// The set of contract-first routes whose OpenAPI document leads the mounted
|
||||
// request handler. The company-and-environment Claude setup-token login routes
|
||||
// now have request handlers, so the set is empty. A new contract-first route
|
||||
// belongs here only until its handler lands.
|
||||
const specOnlyContractFirstRoutes = new Set<string>([]);
|
||||
|
||||
function createApp() {
|
||||
const app = express();
|
||||
app.use("/api", openApiRoutes());
|
||||
@@ -253,7 +259,9 @@ describe("openapi routes", () => {
|
||||
const { routes: specRoutes } = loadSpecRoutes();
|
||||
|
||||
const missingInSpec = [...actualRoutes].filter((route) => !specRoutes.has(route)).sort();
|
||||
const extraInSpec = [...specRoutes].filter((route) => !actualRoutes.has(route)).sort();
|
||||
const extraInSpec = [...specRoutes]
|
||||
.filter((route) => !actualRoutes.has(route) && !specOnlyContractFirstRoutes.has(route))
|
||||
.sort();
|
||||
|
||||
expect({ unknownRouteFiles, missingInSpec, extraInSpec }).toEqual({
|
||||
unknownRouteFiles: [],
|
||||
@@ -288,4 +296,74 @@ describe("openapi routes", () => {
|
||||
expect(spec.paths["/api/board-api-keys"].post.responses["201"]).toBeDefined();
|
||||
expect(spec.paths["/api/companies/import"].post.responses["202"]).toBeDefined();
|
||||
});
|
||||
|
||||
it("publishes the Claude browser-code grammar and strict setup-token response shapes", () => {
|
||||
const { spec } = loadSpecRoutes();
|
||||
const base = "/api/companies/{companyId}/setup-token-login-sessions";
|
||||
|
||||
// The submitted browser code carries the bounded printable-ASCII grammar.
|
||||
const codeBody =
|
||||
spec.paths[`${base}/{sessionId}/code`].post.requestBody.content["application/json"].schema;
|
||||
const browserCode = codeBody.properties.browserCode;
|
||||
expect(browserCode.minLength).toBe(1);
|
||||
expect(browserCode.maxLength).toBe(512);
|
||||
expect(typeof browserCode.pattern).toBe("string");
|
||||
expect(browserCode.pattern.length).toBeGreaterThan(0);
|
||||
|
||||
// Every Claude request object forbids an unknown property.
|
||||
const startBody =
|
||||
spec.paths[base].post.requestBody.content["application/json"].schema;
|
||||
expect(startBody.additionalProperties).toBe(false);
|
||||
expect(codeBody.additionalProperties).toBe(false);
|
||||
|
||||
// The four contract-first routes carry typed strict response schemas.
|
||||
const responseSchemas: Record<string, Record<string, unknown>> = {
|
||||
start: spec.paths[base].post.responses["201"].content["application/json"].schema,
|
||||
status: spec.paths[`${base}/{sessionId}`].get.responses["200"].content["application/json"].schema,
|
||||
prompt: spec.paths[`${base}/{sessionId}/prompt`].get.responses["200"].content["application/json"].schema,
|
||||
code: spec.paths[`${base}/{sessionId}/code`].post.responses["200"].content["application/json"].schema,
|
||||
};
|
||||
const forbiddenProperties = ["token", "accountId", "leaseId"];
|
||||
for (const [name, schema] of Object.entries(responseSchemas)) {
|
||||
expect(schema.type, `${name} response is a typed object`).toBe("object");
|
||||
expect(schema.additionalProperties, `${name} response is strict`).toBe(false);
|
||||
const properties = (schema.properties ?? {}) as Record<string, unknown>;
|
||||
expect(Object.keys(properties).length, `${name} response lists properties`).toBeGreaterThan(0);
|
||||
for (const forbidden of forbiddenProperties) {
|
||||
expect(properties[forbidden], `${name} response hides ${forbidden}`).toBeUndefined();
|
||||
}
|
||||
// No property name looks like a raw prompt secret or a token.
|
||||
for (const property of Object.keys(properties)) {
|
||||
expect(/token|secret|accountId|leaseId/i.test(property), `${name}.${property} is not secret-adjacent`).toBe(
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// The status and code routes share the public response; it hides the prompt.
|
||||
expect(responseSchemas.status.properties).toEqual(responseSchemas.code.properties);
|
||||
expect((responseSchemas.status.properties as Record<string, unknown>).prompt).toBeUndefined();
|
||||
// The owner start response adds the panel mode and the one-time prompt.
|
||||
expect((responseSchemas.start.properties as Record<string, unknown>).panelMode).toBeDefined();
|
||||
expect((responseSchemas.start.properties as Record<string, unknown>).prompt).toBeDefined();
|
||||
// The prompt route returns the authorization URL and the optional transport
|
||||
// advisory. The advisory is present on a non-confidential transport, so the
|
||||
// client can show a non-blocking disclaimer.
|
||||
expect(Object.keys(responseSchemas.prompt.properties as Record<string, unknown>)).toEqual([
|
||||
"authorizationUrl",
|
||||
"transportAdvisory",
|
||||
]);
|
||||
});
|
||||
|
||||
it("documents the 404 non-member gate on the Claude setup-token cancel route", () => {
|
||||
const { spec } = loadSpecRoutes();
|
||||
const cancel =
|
||||
spec.paths["/api/companies/{companyId}/setup-token-login-sessions/{sessionId}/cancel"].post;
|
||||
// The 404 is reachable at run time. The company-access gate returns a fixed
|
||||
// 404 for a non-member before the cancel logic runs, so the spec declares
|
||||
// it. The idempotent cancel still returns 200 for an owner-scoped missing,
|
||||
// terminal, or foreign session id.
|
||||
const codes = Object.keys(cancel.responses).sort();
|
||||
expect(codes).toEqual(["200", "401", "403", "404"]);
|
||||
});
|
||||
});
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
type HostServices,
|
||||
type HostToWorkerMethods,
|
||||
} from "@paperclipai/plugin-sdk";
|
||||
import { CLAUDE_SETUP_TOKEN_COMMAND } from "@paperclipai/adapter-claude-local/server";
|
||||
import {
|
||||
appendStderrExcerpt,
|
||||
createPluginWorkerHandle,
|
||||
@@ -1054,3 +1055,262 @@ describe("plugin worker manager execute.log route", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Host-owned setup-token login pseudo-terminal route gate
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const SETUP_TOKEN_PTY_WORKER_ENTRYPOINT = path.join(
|
||||
FIXTURES_DIR,
|
||||
"plugin-worker-setup-token-pty.cjs",
|
||||
);
|
||||
|
||||
function makeSetupTokenPtyHandle(extra?: Record<string, unknown>) {
|
||||
return createPluginWorkerHandle("test.plugin", {
|
||||
entrypointPath: SETUP_TOKEN_PTY_WORKER_ENTRYPOINT,
|
||||
manifest: TEST_MANIFEST,
|
||||
config: {},
|
||||
instanceInfo: { instanceId: "instance-1", hostVersion: "1.0.0" },
|
||||
apiVersion: 1,
|
||||
hostHandlers: {},
|
||||
...extra,
|
||||
});
|
||||
}
|
||||
|
||||
function ptyOpenInput(directive: unknown) {
|
||||
return {
|
||||
driverKey: "daytona",
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
// The test directive rides in `providerLeaseId`, an opaque field the manager
|
||||
// forwards to the worker unchanged. The manager allowlists `command`, so the
|
||||
// command stays the fixed `CLAUDE_SETUP_TOKEN_COMMAND` for every route-gate
|
||||
// case.
|
||||
providerLeaseId: JSON.stringify(directive),
|
||||
command: CLAUDE_SETUP_TOKEN_COMMAND,
|
||||
};
|
||||
}
|
||||
|
||||
describe("plugin worker manager setup-token pty route gate", () => {
|
||||
it("rejects a command that is not the allowlisted setup-token command before the worker call", async () => {
|
||||
const handle = makeSetupTokenPtyHandle();
|
||||
try {
|
||||
await handle.start();
|
||||
// A caller passes a command other than the fixed `CLAUDE_SETUP_TOKEN_COMMAND`.
|
||||
// The manager rejects it with one fixed non-secret error before the worker
|
||||
// call, so no arbitrary process spawns in the sandbox pseudo-terminal.
|
||||
await expect(
|
||||
handle.openSetupTokenPtySession({
|
||||
driverKey: "daytona",
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: JSON.stringify({ mode: "normal" }),
|
||||
command: "rm -rf /",
|
||||
}),
|
||||
).rejects.toThrow("SETUP_TOKEN_PTY_COMMAND_NOT_ALLOWED");
|
||||
// The rejected open never consumed the single route, so a later open with
|
||||
// the allowlisted command still succeeds.
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({ mode: "normal" }),
|
||||
);
|
||||
expect(session).toBeDefined();
|
||||
await session.close();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("permits one active credential pseudo-terminal per worker", async () => {
|
||||
const handle = makeSetupTokenPtyHandle();
|
||||
try {
|
||||
await handle.start();
|
||||
const first = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({ mode: "normal" }),
|
||||
);
|
||||
// A second open while the first route is not closed rejects with one fixed
|
||||
// non-secret error before it reaches the worker.
|
||||
await expect(
|
||||
handle.openSetupTokenPtySession(ptyOpenInput({ mode: "normal" })),
|
||||
).rejects.toThrow("SETUP_TOKEN_PTY_ROUTE_BUSY");
|
||||
await first.close();
|
||||
// After the first route closes and the worker acknowledges the close, a new
|
||||
// open is admitted.
|
||||
const second = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({ mode: "normal" }),
|
||||
);
|
||||
await second.close();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("delivers output only for the exact bound worker session id and drops a mismatch", async () => {
|
||||
const handle = makeSetupTokenPtyHandle();
|
||||
try {
|
||||
await handle.start();
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({
|
||||
workerSessionId: "ws-A",
|
||||
outputs: [
|
||||
{ chunk: "good-1" },
|
||||
{ chunk: "forged", sid: "ws-EVIL" },
|
||||
{ chunk: "good-2" },
|
||||
],
|
||||
exitCode: 0,
|
||||
}),
|
||||
);
|
||||
const chunks: string[] = [];
|
||||
session.onData((chunk) => chunks.push(chunk));
|
||||
await expect(session.wait()).resolves.toEqual({ exitCode: 0 });
|
||||
// The forged notification carries a wrong worker session id, so the host
|
||||
// drops it. Only the two bound chunks reach the listener, in order.
|
||||
expect(chunks).toEqual(["good-1", "good-2"]);
|
||||
await session.close();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("routes delayed input to the worker and back to the listener", async () => {
|
||||
const handle = makeSetupTokenPtyHandle();
|
||||
try {
|
||||
await handle.start();
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({ workerSessionId: "ws-A" }),
|
||||
);
|
||||
const chunks: string[] = [];
|
||||
session.onData((chunk) => chunks.push(chunk));
|
||||
session.write("browser-code");
|
||||
// The worker echoes the input as one output notification for the bound
|
||||
// session, so the listener receives it.
|
||||
await vi.waitFor(() => expect(chunks).toContain("echo:browser-code"));
|
||||
await session.close();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("terminalizes the route when the cumulative output passes the per-route bound", async () => {
|
||||
const handle = makeSetupTokenPtyHandle({
|
||||
setupTokenPtyLimits: { maxTotalChars: 10 },
|
||||
});
|
||||
try {
|
||||
await handle.start();
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({
|
||||
outputs: [
|
||||
{ chunk: "aaaaa" }, // total 5 → delivered
|
||||
{ chunk: "bbbbb" }, // total 10 → delivered
|
||||
{ chunk: "ccccc" }, // total 15 > 10 → terminalize
|
||||
],
|
||||
}),
|
||||
);
|
||||
const chunks: string[] = [];
|
||||
session.onData((chunk) => chunks.push(chunk));
|
||||
// The per-route bound terminalizes the route, so the login wait resolves
|
||||
// with a null exit code and the third chunk never reaches the listener.
|
||||
await expect(session.wait()).resolves.toEqual({ exitCode: null });
|
||||
expect(chunks).toEqual(["aaaaa", "bbbbb"]);
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("terminalizes and fails closed on a malformed open reply, then admits a later open", async () => {
|
||||
const handle = makeSetupTokenPtyHandle();
|
||||
try {
|
||||
await handle.start();
|
||||
await expect(
|
||||
handle.openSetupTokenPtySession(ptyOpenInput({ mode: "malformed-open" })),
|
||||
).rejects.toThrow("SETUP_TOKEN_PTY_OPEN_FAILED");
|
||||
// The terminalize closed the route by the host route id and the worker
|
||||
// acknowledged the close, so a later open is admitted.
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({ mode: "normal" }),
|
||||
);
|
||||
expect(session).toBeDefined();
|
||||
await session.close();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("terminalizes the route on an open timeout", async () => {
|
||||
const handle = makeSetupTokenPtyHandle({
|
||||
setupTokenPtyLimits: { openTimeoutMs: 200 },
|
||||
});
|
||||
try {
|
||||
await handle.start();
|
||||
await expect(
|
||||
handle.openSetupTokenPtySession(ptyOpenInput({ mode: "no-open-reply" })),
|
||||
).rejects.toThrow();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("binds the worker session id one time and ignores a duplicate open reply", async () => {
|
||||
const handle = makeSetupTokenPtyHandle();
|
||||
try {
|
||||
await handle.start();
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({
|
||||
mode: "duplicate-open-reply",
|
||||
workerSessionId: "ws-A",
|
||||
outputs: [{ chunk: "hello" }],
|
||||
exitCode: 0,
|
||||
}),
|
||||
);
|
||||
const chunks: string[] = [];
|
||||
session.onData((chunk) => chunks.push(chunk));
|
||||
// The duplicate open reply never rebinds or reopens the route, so the
|
||||
// session runs normally on the one bind.
|
||||
await expect(session.wait()).resolves.toEqual({ exitCode: 0 });
|
||||
expect(chunks).toEqual(["hello"]);
|
||||
await session.close();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("closes the route with a fixed exit when the worker exits", async () => {
|
||||
const handle = makeSetupTokenPtyHandle();
|
||||
try {
|
||||
await handle.start();
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({ mode: "normal" }),
|
||||
);
|
||||
const waitResult = session.wait();
|
||||
await handle.stop();
|
||||
// A worker exit closes the one route and resolves the login wait with the
|
||||
// fixed non-secret exit.
|
||||
await expect(waitResult).resolves.toEqual({ exitCode: null });
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
|
||||
it("retires the worker on an unconfirmed close acknowledgement", async () => {
|
||||
const handle = makeSetupTokenPtyHandle({
|
||||
setupTokenPtyLimits: { closeTimeoutMs: 200 },
|
||||
});
|
||||
try {
|
||||
await handle.start();
|
||||
const exited = new Promise<void>((resolve) => {
|
||||
handle.on("exit", () => resolve());
|
||||
});
|
||||
const session = await handle.openSetupTokenPtySession(
|
||||
ptyOpenInput({ mode: "normal", closeMode: "bad-ack" }),
|
||||
);
|
||||
await session.close();
|
||||
// The close acknowledgement carried a mismatched host route id, so the host
|
||||
// fails closed and retires the worker before any reuse.
|
||||
await exited;
|
||||
await expect(
|
||||
handle.openSetupTokenPtySession(ptyOpenInput({ mode: "normal" })),
|
||||
).rejects.toThrow();
|
||||
} finally {
|
||||
await handle.stop().catch(() => undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
import { mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
createSandboxOrphanCleanupSpool,
|
||||
type DeferredOrphanCleanupRecord,
|
||||
} from "../services/sandbox-orphan-cleanup-spool.ts";
|
||||
|
||||
function buildRecord(overrides: Partial<DeferredOrphanCleanupRecord> = {}): DeferredOrphanCleanupRecord {
|
||||
return {
|
||||
companyId: "company-1",
|
||||
environmentId: "environment-1",
|
||||
executionWorkspaceId: null,
|
||||
issueId: null,
|
||||
heartbeatRunId: "run-1",
|
||||
provider: "fake",
|
||||
providerLeaseId: "sandbox://fake/lease-1",
|
||||
metadata: { driver: "sandbox", pluginKey: "example" },
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("createSandboxOrphanCleanupSpool", () => {
|
||||
let dir!: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
dir = await mkdtemp(path.join(os.tmpdir(), "orphan-spool-unit-"));
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await rm(dir, { recursive: true, force: true }).catch(() => undefined);
|
||||
});
|
||||
|
||||
it("persists a record and loads it back after a restart", async () => {
|
||||
const writer = createSandboxOrphanCleanupSpool(dir);
|
||||
const record = buildRecord();
|
||||
expect(await writer.append(record)).toBe(true);
|
||||
|
||||
// A fresh spool over the same directory models a process restart. The record
|
||||
// survives, so the load returns it with every field intact.
|
||||
const reader = createSandboxOrphanCleanupSpool(dir);
|
||||
const loaded = await reader.load();
|
||||
expect(loaded).toHaveLength(1);
|
||||
expect(loaded[0]).toEqual(record);
|
||||
});
|
||||
|
||||
it("removes a record so a later load returns nothing", async () => {
|
||||
const spool = createSandboxOrphanCleanupSpool(dir);
|
||||
const record = buildRecord();
|
||||
await spool.append(record);
|
||||
await spool.remove(record);
|
||||
expect(await spool.load()).toEqual([]);
|
||||
expect((await readdir(dir)).filter((name) => name.endsWith(".json"))).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("keeps one file when the same orphan is appended twice", async () => {
|
||||
const spool = createSandboxOrphanCleanupSpool(dir);
|
||||
const record = buildRecord();
|
||||
await spool.append(record);
|
||||
await spool.append(record);
|
||||
// The file name keys on the provider lease id, so the second append overwrites
|
||||
// the first file instead of persisting a duplicate.
|
||||
const loaded = await spool.load();
|
||||
expect(loaded).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("persists a record with a null provider lease id", async () => {
|
||||
const spool = createSandboxOrphanCleanupSpool(dir);
|
||||
const record = buildRecord({ providerLeaseId: null });
|
||||
expect(await spool.append(record)).toBe(true);
|
||||
const loaded = await spool.load();
|
||||
expect(loaded).toHaveLength(1);
|
||||
expect(loaded[0]?.providerLeaseId).toBeNull();
|
||||
});
|
||||
|
||||
it("skips a malformed spool file and returns the other records", async () => {
|
||||
const spool = createSandboxOrphanCleanupSpool(dir);
|
||||
await spool.append(buildRecord());
|
||||
// A torn or hand-edited file cannot authorize a teardown, so the load skips it
|
||||
// and keeps the valid record.
|
||||
await writeFile(path.join(dir, "broken.json"), "{ not json", "utf8");
|
||||
await writeFile(path.join(dir, "wrong-shape.json"), JSON.stringify({ companyId: 1 }), "utf8");
|
||||
const loaded = await spool.load();
|
||||
expect(loaded).toHaveLength(1);
|
||||
expect(loaded[0]?.providerLeaseId).toBe("sandbox://fake/lease-1");
|
||||
});
|
||||
|
||||
it("returns an empty list when the spool directory does not exist", async () => {
|
||||
const spool = createSandboxOrphanCleanupSpool(path.join(dir, "missing"));
|
||||
expect(await spool.load()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,610 @@
|
||||
// The narrow Claude Code OAuth secret helper and the owner-bound compare-and-set
|
||||
// live in the secret service, not in a route. These tests exercise the real
|
||||
// service against an embedded Postgres database, so the partial unique index,
|
||||
// the expected-version predicate, and the session-id idempotency all run against
|
||||
// real constraints. The route-level tests stay in secrets-routes.test.ts, which
|
||||
// mocks the service and cannot exercise the database behavior.
|
||||
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { mkdirSync, rmSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
activityLog,
|
||||
companies,
|
||||
companyMemberships,
|
||||
companySecretBindings,
|
||||
companySecretVersions,
|
||||
companySecrets,
|
||||
createDb,
|
||||
secretAccessEvents,
|
||||
userSecretDeclarations,
|
||||
userSecretDefinitions,
|
||||
} from "@paperclipai/db";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { HttpError } from "../errors.js";
|
||||
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
|
||||
import { secretService } from "../services/secrets.js";
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
|
||||
if (!embeddedPostgresSupport.supported) {
|
||||
console.warn(
|
||||
`Skipping Claude OAuth secret service tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
|
||||
);
|
||||
}
|
||||
|
||||
// The fixed key the narrow helper owns. A caller never selects it.
|
||||
const CLAUDE_KEY = "CLAUDE_CODE_OAUTH_TOKEN";
|
||||
const CLAUDE_NAME = "Claude Code OAuth token";
|
||||
|
||||
describeEmbeddedPostgres("secretService Claude Code OAuth helper and compare-and-set", () => {
|
||||
let stopDb: (() => Promise<void>) | null = null;
|
||||
let db!: ReturnType<typeof createDb>;
|
||||
const previousKeyFile = process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE;
|
||||
const secretsTmpDir = path.join(os.tmpdir(), `paperclip-claude-oauth-${randomUUID()}`);
|
||||
|
||||
beforeAll(async () => {
|
||||
mkdirSync(secretsTmpDir, { recursive: true });
|
||||
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = path.join(secretsTmpDir, "master.key");
|
||||
const started = await startEmbeddedPostgresTestDatabase("claude-oauth-secrets");
|
||||
stopDb = started.cleanup;
|
||||
db = createDb(started.connectionString);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await db.delete(activityLog);
|
||||
await db.delete(secretAccessEvents);
|
||||
await db.delete(userSecretDeclarations);
|
||||
await db.delete(companySecretBindings);
|
||||
await db.delete(companySecretVersions);
|
||||
await db.delete(companySecrets);
|
||||
await db.delete(userSecretDefinitions);
|
||||
await db.delete(companyMemberships);
|
||||
await db.delete(companies);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (stopDb) await stopDb();
|
||||
if (previousKeyFile === undefined) {
|
||||
delete process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE;
|
||||
} else {
|
||||
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = previousKeyFile;
|
||||
}
|
||||
rmSync(secretsTmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
async function seedCompany(name = "Acme") {
|
||||
const companyId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name,
|
||||
issuePrefix: `T${companyId.slice(0, 7)}`.toUpperCase(),
|
||||
status: "active",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
return companyId;
|
||||
}
|
||||
|
||||
async function seedCompanyMember(companyId: string, userId: string) {
|
||||
await db.insert(companyMemberships).values({
|
||||
companyId,
|
||||
principalType: "user",
|
||||
principalId: userId,
|
||||
status: "active",
|
||||
membershipRole: "owner",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
async function countVersions(secretId: string) {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(companySecretVersions)
|
||||
.where(eq(companySecretVersions.secretId, secretId));
|
||||
return rows.length;
|
||||
}
|
||||
|
||||
// --- the narrow secret-definition helper ------------------------
|
||||
|
||||
it("creates the fixed Claude OAuth definition with the compile-time key and fixed properties", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const definition = await svc.ensureClaudeOAuthUserSecretDefinition(companyId, { userId: "user-1", agentId: null });
|
||||
|
||||
expect(definition.key).toBe(CLAUDE_KEY);
|
||||
expect(definition.name).toBe(CLAUDE_NAME);
|
||||
expect(definition.provider).toBe("local_encrypted");
|
||||
expect(definition.managedMode).toBe("paperclip_managed");
|
||||
expect(definition.status).toBe("active");
|
||||
});
|
||||
|
||||
it("reuses an exact compatible definition without a second row", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.ensureClaudeOAuthUserSecretDefinition(companyId);
|
||||
const second = await svc.ensureClaudeOAuthUserSecretDefinition(companyId);
|
||||
|
||||
expect(second.id).toBe(first.id);
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(userSecretDefinitions)
|
||||
.where(eq(userSecretDefinitions.companyId, companyId));
|
||||
expect(rows).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("rejects a conflicting definition with 409 and does not mutate it", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
// A pre-existing definition holds the fixed key but a different name. It is
|
||||
// not the exact fixed shape, so the helper must reject it and leave it alone.
|
||||
const conflicting = await svc.createUserSecretDefinition(companyId, {
|
||||
key: CLAUDE_KEY,
|
||||
name: "Some other Claude token",
|
||||
provider: "local_encrypted",
|
||||
});
|
||||
|
||||
await expect(svc.ensureClaudeOAuthUserSecretDefinition(companyId)).rejects.toMatchObject({ status: 409 });
|
||||
|
||||
const row = await db
|
||||
.select()
|
||||
.from(userSecretDefinitions)
|
||||
.where(eq(userSecretDefinitions.id, conflicting.id))
|
||||
.then((rows) => rows[0]);
|
||||
expect(row?.name).toBe("Some other Claude token");
|
||||
});
|
||||
|
||||
// --- the owner-bound compare-and-set ----------------------------
|
||||
|
||||
it("first_write creates the owner value and stores the session id", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const result = await svc.completeClaudeOAuthUserSecret(
|
||||
companyId,
|
||||
"user-1",
|
||||
{ sessionId: "session-a", mode: "first_write", value: "oauth-token-value" },
|
||||
{ userId: "user-1", agentId: null },
|
||||
);
|
||||
|
||||
expect(result.secretId).toBeTruthy();
|
||||
expect(result.latestVersion).toBe(1);
|
||||
expect(await countVersions(result.secretId)).toBe(1);
|
||||
});
|
||||
|
||||
it("first_write conflicts with 409 when a different session already wrote the owner value", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
await expect(
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "first_write",
|
||||
value: "another-token-value",
|
||||
}),
|
||||
).rejects.toMatchObject({ status: 409 });
|
||||
});
|
||||
|
||||
it("first_write is idempotent for the same session id and creates no new version", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
const repeat = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
expect(repeat.secretId).toBe(first.secretId);
|
||||
expect(repeat.latestVersion).toBe(1);
|
||||
expect(await countVersions(first.secretId)).toBe(1);
|
||||
});
|
||||
|
||||
it("confirmed_rotation requires expectedSecretId and expectedLatestVersion", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
await expect(
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
}),
|
||||
).rejects.toMatchObject({ status: 422 });
|
||||
});
|
||||
|
||||
it("confirmed_rotation rotates to the next version when the expected version matches", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
const rotated = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: first.latestVersion,
|
||||
});
|
||||
|
||||
expect(rotated.secretId).toBe(first.secretId);
|
||||
expect(rotated.latestVersion).toBe(2);
|
||||
expect(await countVersions(first.secretId)).toBe(2);
|
||||
});
|
||||
|
||||
it("confirmed_rotation returns a fixed stale-confirmation 409 for a wrong expected version", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
// A first confirmed rotation moves the value to version 2.
|
||||
await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
});
|
||||
|
||||
// A later confirmation still carries the stale version 1 and must conflict.
|
||||
await expect(
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-c",
|
||||
mode: "confirmed_rotation",
|
||||
value: "third-token-value",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
}),
|
||||
).rejects.toMatchObject({ status: 409 });
|
||||
|
||||
// The stale confirmation created no new version.
|
||||
expect(await countVersions(first.secretId)).toBe(2);
|
||||
});
|
||||
|
||||
it("confirmed_rotation is idempotent for the same session id and creates no new version", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
const rotated = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
});
|
||||
|
||||
const repeat = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
});
|
||||
|
||||
expect(repeat.secretId).toBe(rotated.secretId);
|
||||
expect(repeat.latestVersion).toBe(2);
|
||||
expect(await countVersions(first.secretId)).toBe(2);
|
||||
});
|
||||
|
||||
it("two concurrent confirmed rotations return one fixed stale 409 and add one version", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
expect(first.latestVersion).toBe(1);
|
||||
|
||||
// Both confirmations read the same latest version 1 and both target the next
|
||||
// version 2. The unique index on (secretId, version) lets one insert win. The
|
||||
// loser must return the same fixed stale 409, not a raw database error.
|
||||
const [a, b] = await Promise.allSettled([
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-b",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
}),
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-c",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-c",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
}),
|
||||
]);
|
||||
|
||||
const fulfilled = [a, b].filter((r) => r.status === "fulfilled");
|
||||
const rejected = [a, b].filter((r) => r.status === "rejected");
|
||||
expect(fulfilled).toHaveLength(1);
|
||||
expect(rejected).toHaveLength(1);
|
||||
|
||||
// The loser returns the one fixed stale-confirmation 409. The text is the
|
||||
// same for every stale race, so it discloses no owner-value state.
|
||||
const loser = (rejected[0] as PromiseRejectedResult).reason;
|
||||
expect(loser).toBeInstanceOf(HttpError);
|
||||
expect((loser as HttpError).status).toBe(409);
|
||||
expect((loser as HttpError).message).toContain("The Claude login confirmation is stale");
|
||||
|
||||
const winner = fulfilled[0] as PromiseFulfilledResult<{ latestVersion: number }>;
|
||||
expect(winner.value.latestVersion).toBe(2);
|
||||
|
||||
// Only the two winning versions remain. The loser left no extra version row.
|
||||
expect(await countVersions(first.secretId)).toBe(2);
|
||||
});
|
||||
|
||||
it("normalizes a next-version unique collision to the fixed stale 409 and adds no row", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
expect(first.latestVersion).toBe(1);
|
||||
|
||||
// Reproduce the exact race window. A concurrent winner inserted the next
|
||||
// version row but has not yet committed its owner-bound compare-and-set, so
|
||||
// the latest version still reads 1. A second confirmation that carries the
|
||||
// same expected version 1 passes the pre-check, then hits the (secretId,
|
||||
// version) unique index on its own insert. That one collision must return the
|
||||
// fixed stale 409, not a raw database error.
|
||||
await db.insert(companySecretVersions).values({
|
||||
secretId: first.secretId,
|
||||
version: 2,
|
||||
material: {},
|
||||
valueSha256: "placeholder-sha256",
|
||||
fingerprintSha256: "placeholder-sha256",
|
||||
status: "disabled",
|
||||
});
|
||||
expect(await countVersions(first.secretId)).toBe(2);
|
||||
|
||||
let caught: unknown;
|
||||
try {
|
||||
await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
});
|
||||
} catch (error) {
|
||||
caught = error;
|
||||
}
|
||||
|
||||
expect(caught).toBeInstanceOf(HttpError);
|
||||
expect((caught as HttpError).status).toBe(409);
|
||||
expect((caught as HttpError).message).toContain("The Claude login confirmation is stale");
|
||||
|
||||
// The failed insert added no new row and did not advance the latest version.
|
||||
expect(await countVersions(first.secretId)).toBe(2);
|
||||
const row = await db
|
||||
.select()
|
||||
.from(companySecrets)
|
||||
.where(eq(companySecrets.id, first.secretId))
|
||||
.then((rows) => rows[0]);
|
||||
expect(row?.latestVersion).toBe(1);
|
||||
});
|
||||
|
||||
it("two concurrent first writes leave exactly one owner value through the partial unique index", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const [a, b] = await Promise.allSettled([
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "token-a",
|
||||
}),
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "first_write",
|
||||
value: "token-b",
|
||||
}),
|
||||
]);
|
||||
|
||||
const fulfilled = [a, b].filter((r) => r.status === "fulfilled");
|
||||
const rejected = [a, b].filter((r) => r.status === "rejected");
|
||||
expect(fulfilled).toHaveLength(1);
|
||||
expect(rejected).toHaveLength(1);
|
||||
|
||||
const values = await db
|
||||
.select()
|
||||
.from(companySecrets)
|
||||
.where(and(eq(companySecrets.companyId, companyId), eq(companySecrets.scope, "user")));
|
||||
expect(values).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("confirmed_rotation on a value from another owner returns not-found", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
await seedCompanyMember(companyId, "user-2");
|
||||
const svc = secretService(db);
|
||||
|
||||
const owned = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
// user-2 points at user-1's secret id. The owner-scoped lookup fails closed.
|
||||
await expect(
|
||||
svc.completeClaudeOAuthUserSecret(companyId, "user-2", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: owned.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
}),
|
||||
).rejects.toMatchObject({ status: 404 });
|
||||
});
|
||||
|
||||
it("confirmed_rotation on a value from another company returns not-found", async () => {
|
||||
const companyA = await seedCompany("Acme");
|
||||
const companyB = await seedCompany("Globex");
|
||||
await seedCompanyMember(companyA, "user-1");
|
||||
await seedCompanyMember(companyB, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const owned = await svc.completeClaudeOAuthUserSecret(companyA, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
await expect(
|
||||
svc.completeClaudeOAuthUserSecret(companyB, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: owned.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
}),
|
||||
).rejects.toBeInstanceOf(HttpError);
|
||||
});
|
||||
|
||||
it("readClaudeOAuthUserSecretStatus returns the secret id and the version for the owner value", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
const rotated = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-token-value",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
});
|
||||
|
||||
const status = await svc.readClaudeOAuthUserSecretStatus(companyId, "user-1");
|
||||
expect(status).toEqual({ secretId: first.secretId, latestVersion: rotated.latestVersion });
|
||||
// The status carries no token value.
|
||||
expect(JSON.stringify(status)).not.toContain("oauth-token-value");
|
||||
expect(JSON.stringify(status)).not.toContain("rotated-token-value");
|
||||
});
|
||||
|
||||
it("readClaudeOAuthUserSecretStatus returns null when the owner has no value", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
await seedCompanyMember(companyId, "user-2");
|
||||
const svc = secretService(db);
|
||||
|
||||
// user-1 has a value; user-2 does not. The reader is owner-scoped, so it
|
||||
// returns null for user-2 and the metadata for user-1.
|
||||
await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
expect(await svc.readClaudeOAuthUserSecretStatus(companyId, "user-2")).toBeNull();
|
||||
// No definition-only or empty company also returns null (the reader never
|
||||
// creates the definition).
|
||||
const emptyCompany = await seedCompany("Globex");
|
||||
await seedCompanyMember(emptyCompany, "user-1");
|
||||
expect(await svc.readClaudeOAuthUserSecretStatus(emptyCompany, "user-1")).toBeNull();
|
||||
});
|
||||
|
||||
it("readClaudeOAuthUserSecretStatus is owner-scoped and company-scoped", async () => {
|
||||
const companyA = await seedCompany("Acme");
|
||||
const companyB = await seedCompany("Globex");
|
||||
await seedCompanyMember(companyA, "user-1");
|
||||
await seedCompanyMember(companyB, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
await svc.completeClaudeOAuthUserSecret(companyA, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: "oauth-token-value",
|
||||
});
|
||||
|
||||
// The same user in another company has no value there.
|
||||
expect(await svc.readClaudeOAuthUserSecretStatus(companyB, "user-1")).toBeNull();
|
||||
// Another owner in the same company has no value.
|
||||
await seedCompanyMember(companyA, "user-2");
|
||||
expect(await svc.readClaudeOAuthUserSecretStatus(companyA, "user-2")).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps the token out of every activity detail", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
const token = "super-secret-oauth-token";
|
||||
|
||||
const first = await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-a",
|
||||
mode: "first_write",
|
||||
value: token,
|
||||
});
|
||||
await svc.completeClaudeOAuthUserSecret(companyId, "user-1", {
|
||||
sessionId: "session-b",
|
||||
mode: "confirmed_rotation",
|
||||
value: "rotated-secret-oauth-token",
|
||||
expectedSecretId: first.secretId,
|
||||
expectedLatestVersion: 1,
|
||||
});
|
||||
|
||||
const activity = await db.select().from(activityLog);
|
||||
expect(JSON.stringify(activity)).not.toContain(token);
|
||||
expect(JSON.stringify(activity)).not.toContain("rotated-secret-oauth-token");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,190 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { mkdirSync, rmSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import {
|
||||
activityLog,
|
||||
companies,
|
||||
companyMemberships,
|
||||
companySecretBindings,
|
||||
companySecretVersions,
|
||||
companySecrets,
|
||||
createDb,
|
||||
secretAccessEvents,
|
||||
userSecretDeclarations,
|
||||
userSecretDefinitions,
|
||||
} from "@paperclipai/db";
|
||||
import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
|
||||
import { secretService } from "../services/secrets.js";
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
|
||||
if (!embeddedPostgresSupport.supported) {
|
||||
console.warn(
|
||||
`Skipping dangling user-secret tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
|
||||
);
|
||||
}
|
||||
|
||||
// A user can delete a user-secret definition while an agent config still
|
||||
// references it. Later, a direct-resolution path (Test or save) resolves that
|
||||
// dangling `user_secret_ref` binding and fails. These tests assert the failure
|
||||
// names the environment variable, the agent id, and the unresolved definition
|
||||
// key, and that it never leaks a secret value.
|
||||
describeEmbeddedPostgres("secretService dangling user_secret_ref resolution", () => {
|
||||
let stopDb: (() => Promise<void>) | null = null;
|
||||
let db!: ReturnType<typeof createDb>;
|
||||
const previousKeyFile = process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE;
|
||||
const secretsTmpDir = path.join(os.tmpdir(), `paperclip-dangling-secret-${randomUUID()}`);
|
||||
|
||||
beforeAll(async () => {
|
||||
mkdirSync(secretsTmpDir, { recursive: true });
|
||||
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = path.join(secretsTmpDir, "master.key");
|
||||
const started = await startEmbeddedPostgresTestDatabase("dangling-user-secret");
|
||||
stopDb = started.cleanup;
|
||||
db = createDb(started.connectionString);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await db.delete(activityLog);
|
||||
await db.delete(secretAccessEvents);
|
||||
await db.delete(userSecretDeclarations);
|
||||
await db.delete(companySecretBindings);
|
||||
await db.delete(companySecretVersions);
|
||||
await db.delete(companySecrets);
|
||||
await db.delete(userSecretDefinitions);
|
||||
await db.delete(companyMemberships);
|
||||
await db.delete(companies);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (stopDb) await stopDb();
|
||||
if (previousKeyFile === undefined) {
|
||||
delete process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE;
|
||||
} else {
|
||||
process.env.PAPERCLIP_SECRETS_MASTER_KEY_FILE = previousKeyFile;
|
||||
}
|
||||
rmSync(secretsTmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
async function seedCompany(name = "Acme") {
|
||||
const companyId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name,
|
||||
issuePrefix: `T${companyId.slice(0, 7)}`.toUpperCase(),
|
||||
status: "active",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
return companyId;
|
||||
}
|
||||
|
||||
async function seedCompanyMember(companyId: string, userId: string) {
|
||||
await db.insert(companyMemberships).values({
|
||||
companyId,
|
||||
principalType: "user",
|
||||
principalId: userId,
|
||||
status: "active",
|
||||
membershipRole: "owner",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
}
|
||||
|
||||
it("save path (syncEnvBindingsForTarget) names the env variable, the agent id, and the deleted definition key", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
const definition = await svc.createUserSecretDefinition(companyId, {
|
||||
key: "github_token",
|
||||
name: "GitHub token",
|
||||
provider: "local_encrypted",
|
||||
});
|
||||
|
||||
// Delete the definition, then keep an agent config that still references it.
|
||||
await svc.removeUserSecretDefinition(companyId, definition.id);
|
||||
|
||||
const agentId = randomUUID();
|
||||
const envValue = {
|
||||
GH_TOKEN: {
|
||||
type: "user_secret_ref" as const,
|
||||
key: "github_token",
|
||||
version: "latest" as const,
|
||||
required: true,
|
||||
},
|
||||
};
|
||||
|
||||
await expect(
|
||||
svc.syncEnvBindingsForTarget(companyId, { targetType: "agent", targetId: agentId }, envValue),
|
||||
).rejects.toMatchObject({
|
||||
status: 404,
|
||||
message: expect.stringContaining("User secret definition not found"),
|
||||
});
|
||||
|
||||
// The message must name the env variable, the agent id, and the definition key.
|
||||
const error = await svc
|
||||
.syncEnvBindingsForTarget(companyId, { targetType: "agent", targetId: agentId }, envValue)
|
||||
.catch((caught: unknown) => caught as Error);
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect(error.message).toContain('environment variable "GH_TOKEN"');
|
||||
expect(error.message).toContain(`agent ${agentId}`);
|
||||
expect(error.message).toContain('definition key "github_token"');
|
||||
});
|
||||
|
||||
it("Test path (resolveUserSecretValue) names the env variable, the agent id, and the deleted definition key without leaking the value", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
const definition = await svc.createUserSecretDefinition(companyId, {
|
||||
key: "github_token",
|
||||
name: "GitHub token",
|
||||
provider: "local_encrypted",
|
||||
});
|
||||
const secretValue = "ghp_super_secret_value";
|
||||
await svc.createCurrentUserSecretValue(companyId, "user-1", {
|
||||
definitionId: definition.id,
|
||||
value: secretValue,
|
||||
});
|
||||
|
||||
// Delete the definition after the value exists. The binding now dangles.
|
||||
await svc.removeUserSecretDefinition(companyId, definition.id);
|
||||
|
||||
const agentId = randomUUID();
|
||||
const error = await svc
|
||||
.resolveUserSecretValue(
|
||||
companyId,
|
||||
{ definitionKey: "github_token", responsibleUserId: "user-1", required: true },
|
||||
{
|
||||
consumerType: "agent",
|
||||
consumerId: agentId,
|
||||
configPath: "env.GH_TOKEN",
|
||||
responsibleUserId: "user-1",
|
||||
},
|
||||
)
|
||||
.catch((caught: unknown) => caught as Error);
|
||||
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
expect((error as { status?: number }).status).toBe(404);
|
||||
expect(error.message).toContain("User secret definition not found");
|
||||
expect(error.message).toContain('environment variable "GH_TOKEN"');
|
||||
expect(error.message).toContain(`agent ${agentId}`);
|
||||
expect(error.message).toContain('definition key "github_token"');
|
||||
// The enriched message must never include the secret value.
|
||||
expect(error.message).not.toContain(secretValue);
|
||||
});
|
||||
|
||||
it("optional binding still resolves to null when the definition is missing (enrichment keeps the 404 status)", async () => {
|
||||
const companyId = await seedCompany();
|
||||
await seedCompanyMember(companyId, "user-1");
|
||||
const svc = secretService(db);
|
||||
|
||||
const resolved = await svc.resolveUserSecretValue(
|
||||
companyId,
|
||||
{ definitionKey: "never_created", responsibleUserId: "user-1", required: false },
|
||||
{ consumerType: "agent", consumerId: randomUUID(), configPath: "env.GH_TOKEN" },
|
||||
);
|
||||
expect(resolved).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -68,6 +68,7 @@ const {
|
||||
reconcileTaskWatchdogs: vi.fn(async () => ({ triggered: 0 })),
|
||||
scanSilentActiveRuns: vi.fn(async () => ({ created: 0, escalated: 0 })),
|
||||
sweepStaleIssueLocks: vi.fn(async () => ({ cleared: 0 })),
|
||||
sweepPendingCleanupLeases: vi.fn(async () => ({ swept: 0, destroyed: 0, capped: 0 })),
|
||||
reconcileProductivityReviews: vi.fn(async () => ({ created: 0, updated: 0, failed: 0 })),
|
||||
sweepExpiredRuntimeStatuses: vi.fn(() => 0),
|
||||
tickTimers: vi.fn(async () => ({ checked: 0, enqueued: 0, skipped: 0 })),
|
||||
@@ -522,7 +523,11 @@ describe("startServer feedback export wiring", () => {
|
||||
try {
|
||||
await startServer();
|
||||
|
||||
expect(heartbeatServiceFactoryMock).not.toHaveBeenCalled();
|
||||
// The disabled path still creates one heartbeat runtime. This runtime owns
|
||||
// the orphan-sandbox cleanup sweep, so a leaked provider sandbox is still
|
||||
// reaped at startup and on the interval.
|
||||
expect(heartbeatServiceFactoryMock).toHaveBeenCalledTimes(1);
|
||||
expect(heartbeatServiceMock.sweepPendingCleanupLeases).toHaveBeenCalled();
|
||||
expect(intervalCallback).not.toBeNull();
|
||||
intervalCallback?.();
|
||||
await Promise.resolve();
|
||||
|
||||
+81
-26
@@ -31,6 +31,15 @@ import { statusCardRoutes } from "./routes/status-cards.js";
|
||||
import { teamsCatalogRoutes } from "./routes/teams-catalog.js";
|
||||
import { agentRoutes } from "./routes/agents.js";
|
||||
import type { SetupTokenSessionService } from "./services/setup-token-session.js";
|
||||
import {
|
||||
buildSetupTokenLoginTransport,
|
||||
createProductionSetupTokenSandboxProvider,
|
||||
createProductionSetupTokenCleanupStore,
|
||||
createSetupTokenSecretWriter,
|
||||
createWorkerBoundSetupTokenPtyOpener,
|
||||
} from "./services/setup-token-transport-binding.js";
|
||||
import { environmentService } from "./services/environments.js";
|
||||
import { environmentRuntimeService } from "./services/environment-runtime.js";
|
||||
import { projectRoutes } from "./routes/projects.js";
|
||||
import { issueRoutes } from "./routes/issues.js";
|
||||
import { issueTreeControlRoutes } from "./routes/issue-tree-control.js";
|
||||
@@ -426,18 +435,52 @@ export async function createApp(
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter((entry) => entry.length > 0);
|
||||
// The production server does not bind `setupTokenLogin` yet, so the start route
|
||||
// fails closed with a fixed no-secret 503 and the login never spawns a process
|
||||
// or holds a lease. This is a deliberate staged rollout: the live transport
|
||||
// needs a real sandbox-lease manager, a live pseudo-terminal factory over the
|
||||
// sandbox provider, and a durable cleanup store (the in-router default store is
|
||||
// in-memory only). Each of those is a separate follow-up that goes through its
|
||||
// own security review before the production server binds `setupTokenLogin`.
|
||||
// Bind the production setup-token login transport. It carries the live lease
|
||||
// manager, the login-process factory over the sandbox pseudo-terminal, and the
|
||||
// durable cleanup store. The factory passes only the fixed command
|
||||
// `CLAUDE_SETUP_TOKEN_COMMAND`; it never reads a command from a
|
||||
// route, a request body, or an adapter configuration. The durable store and the
|
||||
// startup reaper are live now, so a restart reaps a leftover lease.
|
||||
//
|
||||
// The live sandbox pseudo-terminal opener binds inside the sandbox provider
|
||||
// worker, so the server process does not hold the raw sandbox process. The
|
||||
// opener drives the worker through the plugin worker manager route gate.
|
||||
// The manager mints a host-owned route identifier, permits one
|
||||
// active credential pseudo-terminal per worker, binds the worker session
|
||||
// identifier one time for output only, and terminalizes the route on every open
|
||||
// failure path. With the opener supplied, the provider acquires a lease and the
|
||||
// start route drives a live login instead of the fixed 503.
|
||||
const setupTokenLoginTransport = buildSetupTokenLoginTransport({
|
||||
sandbox: createProductionSetupTokenSandboxProvider({
|
||||
environments: environmentService(db),
|
||||
environmentRuntime: environmentRuntimeService(db, { pluginWorkerManager: workerManager }),
|
||||
openLivePtySession: createWorkerBoundSetupTokenPtyOpener({
|
||||
workerManager,
|
||||
environments: environmentService(db),
|
||||
log: (line) => logger.info(line),
|
||||
}),
|
||||
log: (line) => logger.info(line),
|
||||
}),
|
||||
store: createProductionSetupTokenCleanupStore(db),
|
||||
// Bind the atomic credential-claim writer, so a completed login transitions
|
||||
// the durable row to `stored` and stores the minted token in one control-plane
|
||||
// transaction. The writer reads the company and the owner only from the
|
||||
// immutable session scope. The confirm-replacement flow owns rotation. Without
|
||||
// this writer the router falls back to the deferred, fail-closed 503 and never
|
||||
// stores the token.
|
||||
completeCredential: createSetupTokenSecretWriter({ db }),
|
||||
// Forward the login runner diagnostic lines to the server logger. The
|
||||
// runner is the sole producer, and every line is a fixed, non-secret
|
||||
// literal. Without this sink the diagnostics fall back to a no-op in
|
||||
// production, so a failed login leaves no log trail.
|
||||
log: (line) => logger.info(line),
|
||||
});
|
||||
api.use(
|
||||
agentRoutes(db, {
|
||||
pluginWorkerManager: workerManager,
|
||||
deploymentMode: opts.deploymentMode,
|
||||
confidentialProxyAllowlist: setupTokenLoginProxyAllowlist,
|
||||
setupTokenLogin: setupTokenLoginTransport,
|
||||
onSetupTokenLoginService: (service) => {
|
||||
setupTokenLoginService = service;
|
||||
// Startup reaper (SR-4): release any lease whose login session is
|
||||
@@ -865,28 +908,40 @@ export async function createApp(
|
||||
logger.error({ err }, "Failed to load ready plugins on startup");
|
||||
});
|
||||
app.locals.bundledPluginsStartup = bundledPluginsStartup;
|
||||
let appServicesShutdown = false;
|
||||
const shutdownAppServices = () => {
|
||||
if (appServicesShutdown) return;
|
||||
appServicesShutdown = true;
|
||||
disableFeedbackExportFlushes();
|
||||
if (importTransferSweepTimer) {
|
||||
clearInterval(importTransferSweepTimer);
|
||||
importTransferSweepTimer = null;
|
||||
}
|
||||
devWatcher?.close();
|
||||
viteHtmlRenderer?.dispose();
|
||||
void viteDevServer?.close().catch(() => undefined);
|
||||
viteHmrServer?.close();
|
||||
hostServiceCleanup.disposeAll();
|
||||
hostServiceCleanup.teardown();
|
||||
// Cancel every live setup-token login session, so each direct child stops
|
||||
// before the server releases each lease (SR-4).
|
||||
void setupTokenLoginService?.shutdown();
|
||||
// The shutdown hook runs at most once. It caches the in-flight promise, so a
|
||||
// second caller (for example the `exit` handler) awaits the same completion
|
||||
// instead of starting a second teardown.
|
||||
let appServicesShutdown: Promise<void> | null = null;
|
||||
const shutdownAppServices = (): Promise<void> => {
|
||||
if (appServicesShutdown) return appServicesShutdown;
|
||||
appServicesShutdown = (async () => {
|
||||
disableFeedbackExportFlushes();
|
||||
if (importTransferSweepTimer) {
|
||||
clearInterval(importTransferSweepTimer);
|
||||
importTransferSweepTimer = null;
|
||||
}
|
||||
devWatcher?.close();
|
||||
viteHtmlRenderer?.dispose();
|
||||
void viteDevServer?.close().catch(() => undefined);
|
||||
viteHmrServer?.close();
|
||||
hostServiceCleanup.disposeAll();
|
||||
hostServiceCleanup.teardown();
|
||||
// Cancel every live setup-token login session and AWAIT the cancellation,
|
||||
// so each direct child stops and the server releases each lease before the
|
||||
// caller stops the database and the provider. A lease release that
|
||||
// fails stays a durable record for the startup reaper.
|
||||
await setupTokenLoginService?.shutdown();
|
||||
})();
|
||||
return appServicesShutdown;
|
||||
};
|
||||
app.locals.paperclipShutdown = shutdownAppServices;
|
||||
|
||||
process.once("exit", shutdownAppServices);
|
||||
// The `exit` event is synchronous. It cannot await the teardown, so it runs
|
||||
// the best-effort cleanup and drops the returned promise. The orderly signal
|
||||
// path awaits `shutdownAppServices` in full before the process exits.
|
||||
process.once("exit", () => {
|
||||
void shutdownAppServices();
|
||||
});
|
||||
process.once("beforeExit", () => {
|
||||
void flushPluginLogBuffer();
|
||||
});
|
||||
|
||||
+68
-14
@@ -91,6 +91,7 @@ import { ensureDecisionSigningSecret } from "./services/decision-signing.js";
|
||||
import { createDecisionRetentionNotifyOriginAgent, createDecisionWakeOriginAgent } from "./services/decision-wakeup.js";
|
||||
import {
|
||||
coordinateHeartbeatSchedulerShutdown,
|
||||
finalizeServerShutdown,
|
||||
loadWithoutCoordinatedShutdownSignalHooks,
|
||||
} from "./shutdown.js";
|
||||
import { systemdNotify } from "./services/systemd-notify.js";
|
||||
@@ -991,6 +992,46 @@ export async function startServer(): Promise<StartedServer> {
|
||||
}));
|
||||
};
|
||||
|
||||
// The retry backstop for orphan sandboxes. An acquire that rejects a
|
||||
// foreign-company insert tears the provisioned sandbox down. If that teardown
|
||||
// also fails, the acquire records a lease-less `pending_cleanup` lease row. No
|
||||
// other path releases that sandbox, so the master pending-cleanup sweep retries
|
||||
// the provider teardown and releases the orphan. The sweep runs on startup and
|
||||
// on the scheduler interval.
|
||||
//
|
||||
// This backstop is independent of the heartbeat scheduler toggle. A leaked
|
||||
// provider sandbox costs money whether or not the instance schedules
|
||||
// heartbeats, so both the enabled and the disabled path run the sweep. A
|
||||
// disabled heartbeat scheduler must not strand a paid sandbox forever.
|
||||
//
|
||||
// The master pending-cleanup sweep is the single owner of these rows. Its
|
||||
// atomic per-attempt claim makes two overlapping sweeps safe, so the enabled
|
||||
// path can also run the sweep from the orphaned-run reaper without a second
|
||||
// teardown. The heartbeat scheduler owns the sweep when it is enabled; the
|
||||
// disabled path creates its own runtime to own the same sweep.
|
||||
// The interval sweep waits this long after a lease's last write before it
|
||||
// retries the teardown. The window matches the orphaned-run reaper staleness,
|
||||
// so a just-failed lease does not draw a retry on every tick. The startup
|
||||
// sweep passes zero, so a restart retries a stranded orphan at once.
|
||||
const ENVIRONMENT_LEASE_CLEANUP_SWEEP_BACKOFF_MS = 5 * 60 * 1000;
|
||||
const environmentLeaseCleanupHeartbeat =
|
||||
heartbeat ?? heartbeatService(db as any, { pluginWorkerManager });
|
||||
const runEnvironmentLeaseCleanupSweep = (backoffMs: number) =>
|
||||
environmentLeaseCleanupHeartbeat
|
||||
.sweepPendingCleanupLeases({ backoffMs })
|
||||
.then((result) => {
|
||||
if (result.destroyed > 0 || result.capped > 0) {
|
||||
logger.info(result, "environment lease cleanup sweep retried orphan sandbox teardowns");
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.error({ err }, "environment lease cleanup sweep failed");
|
||||
});
|
||||
const scheduleEnvironmentLeaseCleanupSweep = () => {
|
||||
if (heartbeatSchedulerStopped) return;
|
||||
trackHeartbeatSchedulerWork(runEnvironmentLeaseCleanupSweep(ENVIRONMENT_LEASE_CLEANUP_SWEEP_BACKOFF_MS));
|
||||
};
|
||||
|
||||
if (heartbeat) {
|
||||
const secretProposals = createSecretProposalsService(db as any);
|
||||
const decisionExecutor = decisionService(db as any, decisionServiceOptions);
|
||||
@@ -1086,6 +1127,7 @@ export async function startServer(): Promise<StartedServer> {
|
||||
logger.error({ err }, "adapter login reaper sweep failed");
|
||||
}));
|
||||
};
|
||||
|
||||
const tools = toolAccessService(db as any, {
|
||||
deploymentMode: config.deploymentMode,
|
||||
deploymentExposure: config.deploymentExposure,
|
||||
@@ -1223,6 +1265,10 @@ export async function startServer(): Promise<StartedServer> {
|
||||
logger.error({ err }, "startup adapter login reaper sweep failed");
|
||||
});
|
||||
|
||||
// Retry any orphan sandbox teardown left by a failed acquire before a server
|
||||
// restart, so a leaked sandbox does not stay allocated across the restart.
|
||||
await runEnvironmentLeaseCleanupSweep(0);
|
||||
|
||||
const runRetentionSweep = async () => {
|
||||
const activeCompanies = await db.select({ id: companies.id }).from(companies).where(eq(companies.status, "active"));
|
||||
let archived = 0;
|
||||
@@ -1282,6 +1328,7 @@ export async function startServer(): Promise<StartedServer> {
|
||||
scheduleMergedPullRequestConfirmationSweep();
|
||||
scheduleTerminalWorkspaceSweep();
|
||||
scheduleAdapterLoginReaperSweep();
|
||||
scheduleEnvironmentLeaseCleanupSweep();
|
||||
|
||||
if (heartbeatSchedulerStopped) return;
|
||||
trackHeartbeatSchedulerWork(routines
|
||||
@@ -1419,8 +1466,14 @@ export async function startServer(): Promise<StartedServer> {
|
||||
}));
|
||||
});
|
||||
} else {
|
||||
// The heartbeat scheduler is disabled, but the orphan-sandbox cleanup sweep
|
||||
// is still required. A failed acquire can leak a paid provider sandbox, so
|
||||
// this path retries the teardown at startup and on the interval, exactly as
|
||||
// the enabled path does.
|
||||
await runEnvironmentLeaseCleanupSweep(0);
|
||||
startHeartbeatSchedulerInterval(() => {
|
||||
scheduleExternalObjectRefreshSweep(new Date());
|
||||
scheduleEnvironmentLeaseCleanupSweep();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1577,21 +1630,22 @@ export async function startServer(): Promise<StartedServer> {
|
||||
logger.error({ err, signal }, "run-log in-flight mirror flush failed");
|
||||
}
|
||||
|
||||
const appShutdown = (app as { locals?: { paperclipShutdown?: () => void } }).locals?.paperclipShutdown;
|
||||
appShutdown?.();
|
||||
const appShutdown = (app as { locals?: { paperclipShutdown?: () => Promise<void> } }).locals
|
||||
?.paperclipShutdown;
|
||||
const embeddedPostgresToStop =
|
||||
embeddedPostgres && embeddedPostgresStartedByThisProcess ? embeddedPostgres : null;
|
||||
|
||||
if (embeddedPostgres && embeddedPostgresStartedByThisProcess) {
|
||||
logger.info({ signal }, "Stopping embedded PostgreSQL");
|
||||
try {
|
||||
await embeddedPostgres?.stop();
|
||||
} catch (err) {
|
||||
logger.error({ err }, "Failed to stop embedded PostgreSQL cleanly");
|
||||
}
|
||||
}
|
||||
|
||||
// Flush buffered OTel spans before the process goes away; without this
|
||||
// await the exporter's final batch is dropped on exit.
|
||||
await shutdownInstrumentation();
|
||||
// Await the ordered application teardown before the process exits. A live
|
||||
// setup-token login session must stop and release its sandbox lease before
|
||||
// the database and the provider stop, so an orderly shutdown never leaves a
|
||||
// sandbox lease or confidential login state alive past the process exit.
|
||||
await finalizeServerShutdown({
|
||||
signal,
|
||||
shutdownAppServices: appShutdown,
|
||||
stopEmbeddedPostgres: embeddedPostgresToStop ? () => embeddedPostgresToStop.stop() : null,
|
||||
shutdownInstrumentation,
|
||||
log: logger,
|
||||
});
|
||||
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
+548
-128
@@ -29,6 +29,8 @@ import {
|
||||
updateAgentSchema,
|
||||
supportedEnvironmentDriversForAdapter,
|
||||
LOW_TRUST_REVIEW_PRESET,
|
||||
startClaudeSetupTokenSessionRequestSchema,
|
||||
submitBrowserCodeRequestSchema,
|
||||
} from "@paperclipai/shared";
|
||||
import {
|
||||
isForbiddenConfigEnvKey,
|
||||
@@ -58,7 +60,7 @@ import {
|
||||
} from "../services/index.js";
|
||||
import { badRequest, conflict, forbidden, HttpError, notFound, unprocessable } from "../errors.js";
|
||||
import { createRunSecretRedactionRegistry } from "../services/run-secret-redaction.js";
|
||||
import { assertBoard, assertCompanyAccess, assertInstanceAdmin, buildActorSecretContext, getAccessibleResource, getActorInfo, hasCompanyAccess } from "./authz.js";
|
||||
import { assertAuthenticated, assertBoard, assertCompanyAccess, assertInstanceAdmin, buildActorSecretContext, getAccessibleResource, getActorInfo, hasCompanyAccess } from "./authz.js";
|
||||
import {
|
||||
assertNoAgentHostWorkspaceCommandMutation,
|
||||
collectAgentAdapterWorkspaceCommandPaths,
|
||||
@@ -67,6 +69,7 @@ import type { PluginWorkerManager } from "../services/plugin-worker-manager.js";
|
||||
import { environmentService } from "../services/environments.js";
|
||||
import { resolveEnvironmentExecutionTarget } from "../services/environment-execution-target.js";
|
||||
import { environmentRuntimeService } from "../services/environment-runtime.js";
|
||||
import { resolvePluginSandboxProviderDriverByKey } from "../services/plugin-environment-driver.js";
|
||||
import type { AdapterExecutionTarget } from "@paperclipai/adapter-utils/execution-target";
|
||||
import type {
|
||||
AdapterEnvironmentCheck,
|
||||
@@ -103,16 +106,33 @@ import {
|
||||
assessConfidentialStartup,
|
||||
evaluateConfidentialTransport,
|
||||
SETUP_TOKEN_START_FAILED,
|
||||
SETUP_TOKEN_TRANSPORT_INSECURE,
|
||||
SETUP_TOKEN_SESSION_NOT_FOUND,
|
||||
SETUP_TOKEN_PROVIDER_UNSUPPORTED,
|
||||
SETUP_TOKEN_PROVIDER_UNSUPPORTED_CODE,
|
||||
type ConfidentialTransportConfig,
|
||||
type SetupTokenCleanupRecord,
|
||||
type SetupTokenCleanupStore,
|
||||
type SetupTokenLease,
|
||||
type SetupTokenLeaseManager,
|
||||
type SetupTokenLoginProcessFactory,
|
||||
type SetupTokenSecretWriter,
|
||||
type SetupTokenSessionScope,
|
||||
type SetupTokenSessionState,
|
||||
type SetupTokenSessionDescriptor,
|
||||
} from "../services/setup-token-session.js";
|
||||
import type { DeploymentMode } from "@paperclipai/shared";
|
||||
import type {
|
||||
DeploymentMode,
|
||||
AdapterAuthSessionStatus,
|
||||
AdapterAuthSessionFailure,
|
||||
ClaudeSetupTokenSessionResponse,
|
||||
ClaudeSetupTokenSessionOwnerResponse,
|
||||
ClaudeSetupTokenSessionPrompt,
|
||||
ClaudeSetupTokenCompletionResponse,
|
||||
ClaudeOAuthTokenStatusResponse,
|
||||
ClaudeSetupTokenOverwrite,
|
||||
SetupTokenTransportAdvisory,
|
||||
} from "@paperclipai/shared";
|
||||
import { SETUP_TOKEN_TRANSPORT_ADVISORY_CODE } from "@paperclipai/shared";
|
||||
import { DEFAULT_CODEX_LOCAL_BYPASS_APPROVALS_AND_SANDBOX } from "@paperclipai/adapter-codex-local";
|
||||
import {
|
||||
checkStagedCredentialReadiness,
|
||||
@@ -233,6 +253,11 @@ export function agentRoutes(
|
||||
leases: SetupTokenLeaseManager;
|
||||
/** The durable cleanup store. Defaults to the in-memory record store. */
|
||||
store?: SetupTokenCleanupStore;
|
||||
/**
|
||||
* The owner-bound secret writer. When the caller omits it, the completion
|
||||
* fails closed, because the secret sink is not bound yet.
|
||||
*/
|
||||
completeCredential?: SetupTokenSecretWriter;
|
||||
};
|
||||
} = {},
|
||||
) {
|
||||
@@ -325,20 +350,47 @@ export function agentRoutes(
|
||||
// The in-memory non-secret cleanup record store. It is the default store when a
|
||||
// caller does not inject a durable database-backed store.
|
||||
const setupTokenCleanupRows = new Map<string, SetupTokenCleanupRecord>();
|
||||
const scopeMatchesRow = (row: SetupTokenCleanupRecord, identity: {
|
||||
companyId: string;
|
||||
ownerUserId: string;
|
||||
adapterType: string;
|
||||
environmentId: string;
|
||||
}): boolean =>
|
||||
row.companyId === identity.companyId &&
|
||||
row.ownerUserId === identity.ownerUserId &&
|
||||
row.adapterType === identity.adapterType &&
|
||||
row.environmentId === identity.environmentId;
|
||||
const inMemorySetupTokenCleanupStore: SetupTokenCleanupStore = {
|
||||
async record(record): Promise<void> {
|
||||
setupTokenCleanupRows.set(record.sessionId, { ...record });
|
||||
},
|
||||
async markState(sessionId, state): Promise<void> {
|
||||
const row = setupTokenCleanupRows.get(sessionId);
|
||||
if (row) row.state = state;
|
||||
async markState(identity, state): Promise<void> {
|
||||
const row = setupTokenCleanupRows.get(identity.sessionId);
|
||||
if (row && scopeMatchesRow(row, identity)) row.state = state;
|
||||
},
|
||||
async remove(sessionId): Promise<void> {
|
||||
setupTokenCleanupRows.delete(sessionId);
|
||||
async remove(identity): Promise<void> {
|
||||
// The delete matches the full owner scope, so it never removes a row by the
|
||||
// session id alone.
|
||||
const row = setupTokenCleanupRows.get(identity.sessionId);
|
||||
if (row && scopeMatchesRow(row, identity)) setupTokenCleanupRows.delete(identity.sessionId);
|
||||
},
|
||||
async listReapable(): Promise<SetupTokenCleanupRecord[]> {
|
||||
return [];
|
||||
},
|
||||
async consumeStoredClaim(identity): Promise<SetupTokenCleanupRecord | null> {
|
||||
const row = setupTokenCleanupRows.get(identity.sessionId);
|
||||
if (
|
||||
!row ||
|
||||
!scopeMatchesRow(row, identity) ||
|
||||
row.state !== "stored" ||
|
||||
row.boundAt !== null ||
|
||||
row.deadline <= Date.now()
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
row.boundAt = Date.now();
|
||||
return { ...row };
|
||||
},
|
||||
};
|
||||
|
||||
const deferredSetupTokenLoginFactory: SetupTokenLoginProcessFactory = () => {
|
||||
@@ -347,19 +399,49 @@ export function agentRoutes(
|
||||
throw new SetupTokenSessionError(503, SETUP_TOKEN_START_FAILED);
|
||||
};
|
||||
|
||||
// Resolve the transport: use the injected factory, lease manager, and store
|
||||
// when a caller binds them; otherwise use the deferred, fail-closed defaults.
|
||||
const deferredSetupTokenSecretWriter: SetupTokenSecretWriter = async () => {
|
||||
// The owner-bound secret writer arrives through `options.setupTokenLogin`.
|
||||
// Until then the completion fails closed, so the session never reports a
|
||||
// stored credential without a real secret write.
|
||||
throw new SetupTokenSessionError(503, SETUP_TOKEN_START_FAILED);
|
||||
};
|
||||
|
||||
// Resolve the transport: use the injected factory, lease manager, store, and
|
||||
// secret writer when a caller binds them; otherwise use the deferred,
|
||||
// fail-closed defaults.
|
||||
const setupTokenLoginFactory =
|
||||
options.setupTokenLogin?.factory ?? deferredSetupTokenLoginFactory;
|
||||
const setupTokenLeaseManager =
|
||||
options.setupTokenLogin?.leases ?? deferredSetupTokenLeaseManager;
|
||||
const setupTokenCleanupStore =
|
||||
options.setupTokenLogin?.store ?? inMemorySetupTokenCleanupStore;
|
||||
const setupTokenSecretWriter =
|
||||
options.setupTokenLogin?.completeCredential ?? deferredSetupTokenSecretWriter;
|
||||
|
||||
// Re-check the environment company binding at lease acquisition. The start
|
||||
// route runs `assertSandboxLoginEnvironment` before the session begins, but
|
||||
// managed-environment reconciliation can bind the sandbox to another company
|
||||
// between that guard and the lease acquire. This wrapper re-runs the same
|
||||
// guard at acquire time and fails closed with the 403
|
||||
// `environment_company_mismatch` before the transport provisions a sandbox.
|
||||
// The lease insert transaction re-checks the binding once more inside the
|
||||
// insert, so a bind that lands during the provider call still holds no lease.
|
||||
const guardedSetupTokenLeaseManager: SetupTokenLeaseManager = {
|
||||
async acquire(input): Promise<SetupTokenLease> {
|
||||
await assertSandboxLoginEnvironment(input.scope.companyId, input.scope.environmentId, {
|
||||
requireSetupTokenLoginProvider: true,
|
||||
});
|
||||
return setupTokenLeaseManager.acquire(input);
|
||||
},
|
||||
release: (lease) => setupTokenLeaseManager.release(lease),
|
||||
releaseById: (leaseId) => setupTokenLeaseManager.releaseById(leaseId),
|
||||
};
|
||||
|
||||
const setupTokenLoginService = new SetupTokenSessionService({
|
||||
factory: setupTokenLoginFactory,
|
||||
leases: setupTokenLeaseManager,
|
||||
leases: guardedSetupTokenLeaseManager,
|
||||
store: setupTokenCleanupStore,
|
||||
completeCredential: setupTokenSecretWriter,
|
||||
rateLimiter: setupTokenRateLimiter,
|
||||
});
|
||||
|
||||
@@ -1088,13 +1170,69 @@ export function agentRoutes(
|
||||
// only in an active sandbox environment. This reuses the shared environment
|
||||
// selection guard, so it rejects a missing, archived (inactive), local, SSH, or
|
||||
// plugin environment the same way the agent configuration routes do.
|
||||
//
|
||||
// The execution environment catalog is instance-scoped, not company-owned. PR
|
||||
// #8375 moved the catalog to one shared instance catalog, so an environment row
|
||||
// carries no single owning company. The shared selection guard therefore checks
|
||||
// only the driver and the status. The company-binding check below then rejects
|
||||
// an environment that binds to other companies. The route caller is already
|
||||
// bound to the path company by `assertCompanyAccess`, and the acquired lease
|
||||
// records that same company, so the login stays attributed to the caller.
|
||||
async function assertSandboxLoginEnvironment(
|
||||
companyId: string,
|
||||
environmentId: string,
|
||||
options?: { requireSetupTokenLoginProvider?: boolean },
|
||||
): Promise<void> {
|
||||
await assertEnvironmentSelectionForCompany(environmentsSvc, companyId, environmentId, {
|
||||
allowedDrivers: ["sandbox"],
|
||||
});
|
||||
// Reject an environment that another company owns. A managed sandbox
|
||||
// environment binds to the companies that the instance provisions it for.
|
||||
// When the environment binds to companies but not the request company, the
|
||||
// environment belongs to another company. A login there runs the process in
|
||||
// a foreign company sandbox, so the guard fails closed. An environment with
|
||||
// no company binding is instance-global and stays open to every member.
|
||||
const boundCompanyIds = await environmentsSvc.listBoundCompanyIds(environmentId);
|
||||
if (boundCompanyIds.length > 0 && !boundCompanyIds.includes(companyId)) {
|
||||
throw forbidden("The selected environment belongs to another company.", {
|
||||
code: "environment_company_mismatch",
|
||||
});
|
||||
}
|
||||
// Gate the Claude setup-token login on the provider capability. Only a
|
||||
// sandbox provider that advertises the setup-token login capability
|
||||
// implements the setup-token pseudo-terminal methods. The setup-token start
|
||||
// routes pass this option, so an unsupported provider fails closed here
|
||||
// before the session starts. The lease guard passes it too, so a
|
||||
// reconciliation that rebinds the environment to an unsupported provider
|
||||
// still fails closed before the lease and the pseudo-terminal.
|
||||
if (options?.requireSetupTokenLoginProvider) {
|
||||
await assertSetupTokenLoginProviderCapability(environmentId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fails closed when the environment provider does not advertise the Claude
|
||||
* setup-token login capability. It resolves the effective provider from the
|
||||
* environment config, then reads the static capability from the provider
|
||||
* plugin manifest. It never checks the provider by name. A missing plugin, a
|
||||
* non-plugin provider, and a provider without the flag all fail closed with
|
||||
* the fixed, typed error, so no session row, lease, or pseudo-terminal starts.
|
||||
*/
|
||||
async function assertSetupTokenLoginProviderCapability(environmentId: string): Promise<void> {
|
||||
const environment = await environmentsSvc.getById(environmentId);
|
||||
const config =
|
||||
environment?.config && typeof environment.config === "object"
|
||||
? (environment.config as Record<string, unknown>)
|
||||
: {};
|
||||
const provider = typeof config.provider === "string" ? config.provider : "";
|
||||
const resolved = provider
|
||||
? await resolvePluginSandboxProviderDriverByKey({ db, driverKey: provider })
|
||||
: null;
|
||||
if (!resolved?.driver.supportsSetupTokenLogin) {
|
||||
throw unprocessable(SETUP_TOKEN_PROVIDER_UNSUPPORTED, {
|
||||
code: SETUP_TOKEN_PROVIDER_UNSUPPORTED_CODE,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Read a login session for its owner. The durable row is the authority for the
|
||||
@@ -2976,6 +3114,13 @@ export function agentRoutes(
|
||||
instructionsBundle,
|
||||
sourceIssueId: _sourceIssueId,
|
||||
sourceIssueIds: _sourceIssueIds,
|
||||
// The stored-session claim is not an agent column. The server derives the
|
||||
// owner from the authenticated actor and consumes the claim in the create
|
||||
// transaction, so it never reaches the insert values.
|
||||
storedSessionId: hireStoredSessionId,
|
||||
// The apply-existing flag is not an agent column. The server binds the
|
||||
// fixed reference to the owner stored value with no login round trip.
|
||||
applyStoredClaudeLogin: hireApplyStoredClaudeLogin,
|
||||
...hireInput
|
||||
} = req.body;
|
||||
hireInput.adapterType = assertSelectableAdapterType(hireInput.adapterType);
|
||||
@@ -3032,13 +3177,26 @@ export function agentRoutes(
|
||||
|
||||
const requiresApproval = company.requireBoardApprovalForNewAgents;
|
||||
const status = requiresApproval ? "pending_approval" : "idle";
|
||||
const createdAgent = await svc.create(companyId, {
|
||||
id: hiredAgentId,
|
||||
...normalizedHireInput,
|
||||
status,
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
});
|
||||
const createdAgent = await svc.create(
|
||||
companyId,
|
||||
{
|
||||
id: hiredAgentId,
|
||||
...normalizedHireInput,
|
||||
status,
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
},
|
||||
{
|
||||
claudeLogin: {
|
||||
storedSessionId: hireStoredSessionId ?? null,
|
||||
ownerUserId: req.actor.type === "agent" ? null : (req.actor.userId ?? null),
|
||||
// The apply-existing path runs only for a user actor. The owner comes
|
||||
// from the actor, so an agent actor never reaches the no-claim bind.
|
||||
applyExistingWithoutClaim:
|
||||
req.actor.type !== "agent" && hireApplyStoredClaudeLogin === true,
|
||||
},
|
||||
},
|
||||
);
|
||||
const agent = await materializeDefaultInstructionsBundleForNewAgent(createdAgent, instructionsBundle);
|
||||
|
||||
let approval: Awaited<ReturnType<typeof approvalsSvc.getById>> | null = null;
|
||||
@@ -3172,6 +3330,13 @@ export function agentRoutes(
|
||||
const {
|
||||
desiredSkills: requestedDesiredSkills,
|
||||
instructionsBundle,
|
||||
// The stored-session claim is not an agent column. The server derives the
|
||||
// owner from the authenticated actor and consumes the claim in the create
|
||||
// transaction, so it never reaches the insert values.
|
||||
storedSessionId: createStoredSessionId,
|
||||
// The apply-existing flag is not an agent column. The server binds the
|
||||
// fixed reference to the owner stored value with no login round trip.
|
||||
applyStoredClaudeLogin: createApplyStoredClaudeLogin,
|
||||
...createInput
|
||||
} = req.body;
|
||||
createInput.adapterType = assertSelectableAdapterType(createInput.adapterType);
|
||||
@@ -3216,15 +3381,28 @@ export function agentRoutes(
|
||||
allowedSandboxProviders: allowedSandboxProvidersForAgent(createInput.adapterType),
|
||||
});
|
||||
|
||||
const createdAgent = await svc.create(companyId, {
|
||||
id: agentId,
|
||||
...createInput,
|
||||
adapterConfig: normalizedAdapterConfig,
|
||||
runtimeConfig: normalizedRuntimeConfig,
|
||||
status: "idle",
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
});
|
||||
const createdAgent = await svc.create(
|
||||
companyId,
|
||||
{
|
||||
id: agentId,
|
||||
...createInput,
|
||||
adapterConfig: normalizedAdapterConfig,
|
||||
runtimeConfig: normalizedRuntimeConfig,
|
||||
status: "idle",
|
||||
spentMonthlyCents: 0,
|
||||
lastHeartbeatAt: null,
|
||||
},
|
||||
{
|
||||
claudeLogin: {
|
||||
storedSessionId: createStoredSessionId ?? null,
|
||||
ownerUserId: req.actor.type === "agent" ? null : (req.actor.userId ?? null),
|
||||
// The apply-existing path runs only for a user actor. The owner comes
|
||||
// from the actor, so an agent actor never reaches the no-claim bind.
|
||||
applyExistingWithoutClaim:
|
||||
req.actor.type !== "agent" && createApplyStoredClaudeLogin === true,
|
||||
},
|
||||
},
|
||||
);
|
||||
const agent = await materializeDefaultInstructionsBundleForNewAgent(createdAgent, instructionsBundle);
|
||||
|
||||
const actor = getActorInfo(req);
|
||||
@@ -3572,6 +3750,11 @@ export function agentRoutes(
|
||||
const patchData = { ...(req.body as Record<string, unknown>) };
|
||||
const replaceAdapterConfig = patchData.replaceAdapterConfig === true;
|
||||
delete patchData.replaceAdapterConfig;
|
||||
// The apply-existing flag is not an agent column. The server binds the fixed
|
||||
// reference to the owner stored value with no login round trip. Remove it
|
||||
// from the patch so it never reaches the update values.
|
||||
const applyStoredClaudeLogin = patchData.applyStoredClaudeLogin === true;
|
||||
delete patchData.applyStoredClaudeLogin;
|
||||
if (hasOwn(patchData, "adapterConfig")) {
|
||||
const adapterConfig = asRecord(patchData.adapterConfig);
|
||||
if (!adapterConfig) {
|
||||
@@ -3698,6 +3881,13 @@ export function agentRoutes(
|
||||
createdByUserId: actor.actorType === "user" ? actor.actorId : null,
|
||||
source: "patch",
|
||||
},
|
||||
claudeLogin: {
|
||||
ownerUserId: req.actor.type === "agent" ? null : (req.actor.userId ?? null),
|
||||
// The apply-existing path runs only for a user actor. The owner comes
|
||||
// from the actor, so an agent actor never reaches the no-claim bind.
|
||||
applyExistingWithoutClaim:
|
||||
req.actor.type !== "agent" && applyStoredClaudeLogin,
|
||||
},
|
||||
});
|
||||
if (!agent) {
|
||||
res.status(404).json({ error: "Agent not found" });
|
||||
@@ -4229,11 +4419,10 @@ export function agentRoutes(
|
||||
|
||||
// --- Setup-token login session routes --------------------------------------
|
||||
//
|
||||
// The routes give the harness the operations against one live login session.
|
||||
// Every operation verifies the company, the owner user, and the target agent
|
||||
// through the session scope. A missing session and a cross-scope session both
|
||||
// return the same 404. The confidential responses (read-prompt and
|
||||
// receive-token) pass through the fail-closed transport guard and set
|
||||
// The routes give the UI operations against one live login session. Every
|
||||
// operation verifies the company and owner user through the session scope. A
|
||||
// missing session and a cross-scope session both return the same 404. The
|
||||
// confidential responses pass through the transport assessment and set
|
||||
// `Cache-Control: no-store`. The routes write no prompt, code, token, or raw
|
||||
// process chunk to a log or an activity detail, and they return fixed error
|
||||
// text only.
|
||||
@@ -4251,28 +4440,63 @@ export function agentRoutes(
|
||||
// test with an unresolved path, so the routes repeat the base path instead.
|
||||
|
||||
/**
|
||||
* Builds the immutable session scope from the authenticated owner user and the
|
||||
* target agent (SR-3, FU-1). Only a user actor owns a login session; the route
|
||||
* uses this dedicated login scope, not the broad agent-manage permission.
|
||||
* Derives the immutable owner of a setup-token login session from the actor.
|
||||
* Only a board user owns a login session. It returns the owner id, or it
|
||||
* throws a forbidden error. The owner is never a client field; it comes only
|
||||
* from the authenticated actor.
|
||||
*/
|
||||
const buildSetupTokenScope = (
|
||||
req: Request,
|
||||
agent: { id: string; companyId: string },
|
||||
): SetupTokenSessionScope => {
|
||||
const deriveSetupTokenOwnerUserId = (req: Request): string => {
|
||||
const actor = getActorInfo(req);
|
||||
if (actor.actorType !== "user") {
|
||||
throw forbidden("A user must own a setup-token login session.");
|
||||
}
|
||||
return { companyId: agent.companyId, ownerUserId: actor.actorId, targetAgentId: agent.id };
|
||||
return actor.actorId;
|
||||
};
|
||||
|
||||
/**
|
||||
* Applies the fail-closed confidential transport guard (SR-6, SR-7). It reads
|
||||
* the raw socket TLS bit and the immediate peer address, so the global
|
||||
* `trust proxy` setting cannot influence the decision. It returns false and
|
||||
* sends the fixed no-secret error when the transport is not confidential.
|
||||
* Read-access gate for the company-scoped setup-token session routes. It runs
|
||||
* before a route resolves a session. The session id is an opaque secret-bearing
|
||||
* reference, so a cross-company reference must fail closed like a missing
|
||||
* session. This gate returns the same fixed not-found error for a cross-company
|
||||
* reference by an authenticated non-member as for a missing session, so the
|
||||
* route is not a company-membership oracle. It keeps the not-found equivalence
|
||||
* the session lookups use.
|
||||
*
|
||||
* The gate keeps the actor rules unchanged. It throws 401 for an unauthenticated
|
||||
* caller and 403 for a non-user actor through the owner derivation. For an
|
||||
* authorized member it runs the full `assertCompanyAccess` write-path checks and
|
||||
* returns the owner user id. For a non-member it sends the fixed 404 and returns
|
||||
* null; the route must stop.
|
||||
*/
|
||||
const enforceSetupTokenTransport = (req: Request, res: Response): boolean => {
|
||||
const resolveCompanySessionOwner = (
|
||||
req: Request,
|
||||
companyId: string,
|
||||
res: Response,
|
||||
): string | null => {
|
||||
assertAuthenticated(req);
|
||||
const ownerUserId = deriveSetupTokenOwnerUserId(req);
|
||||
if (!hasCompanyAccess(req, companyId)) {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.status(404).json({ error: SETUP_TOKEN_SESSION_NOT_FOUND });
|
||||
return null;
|
||||
}
|
||||
assertCompanyAccess(req, companyId);
|
||||
return ownerUserId;
|
||||
};
|
||||
|
||||
/**
|
||||
* Assesses the setup-token confidential transport. The product
|
||||
* owner set a non-negotiable requirement: do not force TLS. Many users run
|
||||
* Paperclip over plain HTTP on a home server or a Tailscale tailnet. So the
|
||||
* route does not block a non-confidential transport. It returns a non-blocking
|
||||
* advisory instead, and the route attaches it to the confidential response.
|
||||
* The client shows a visible disclaimer and lets the login proceed. The
|
||||
* function reads the raw socket TLS bit and the immediate peer address, so the
|
||||
* global `trust proxy` setting cannot change the result. It returns null when
|
||||
* the transport is confidential (direct TLS, a local-trusted loopback, or an
|
||||
* allowlisted TLS proxy), so a confidential response shows no disclaimer.
|
||||
*/
|
||||
const assessSetupTokenTransport = (req: Request): SetupTokenTransportAdvisory | null => {
|
||||
const socket = req.socket as { encrypted?: boolean; remoteAddress?: string };
|
||||
const forwardedProto = req.headers["x-forwarded-proto"];
|
||||
const decision = evaluateConfidentialTransport(setupTokenConfidentialConfig, {
|
||||
@@ -4280,12 +4504,7 @@ export function agentRoutes(
|
||||
remoteAddress: socket?.remoteAddress,
|
||||
forwardedProto: Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto,
|
||||
});
|
||||
if (!decision.allowed) {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.status(403).json({ error: SETUP_TOKEN_TRANSPORT_INSECURE });
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return decision.allowed ? null : { code: SETUP_TOKEN_TRANSPORT_ADVISORY_CODE };
|
||||
};
|
||||
|
||||
const sendSetupTokenError = (res: Response, err: unknown): void => {
|
||||
@@ -4296,123 +4515,324 @@ export function agentRoutes(
|
||||
throw err;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves the target agent for a login-session route and verifies it is a
|
||||
* `claude_local` agent. Returns null and sends the response when the agent is
|
||||
* missing, inaccessible, or the wrong adapter type.
|
||||
*/
|
||||
const resolveSetupTokenAgent = async (req: Request, res: Response) => {
|
||||
const id = req.params.id as string;
|
||||
const agent = await getAccessibleResource(req, res, svc.getById(id), "Agent not found");
|
||||
if (!agent) return null;
|
||||
if (agent.adapterType !== "claude_local") {
|
||||
res.status(400).json({ error: "Login is only supported for claude_local agents" });
|
||||
return null;
|
||||
// --- Company-and-environment setup-token login routes ----------------------
|
||||
//
|
||||
// These routes serve the agentless Claude login. The scope binds one login to
|
||||
// one company, one owner user, one adapter, and one environment. The scope
|
||||
// carries no agent id, so a hire flow starts one login before an agent exists.
|
||||
//
|
||||
// Object-level authorization: every action derives the owner from
|
||||
// the authenticated actor, fixes the adapter to `claude_local`, and resolves
|
||||
// the environment server-side. The lookup scopes by the immutable tuple
|
||||
// company, owner, adapter, environment, and session. A foreign session returns
|
||||
// the same not-found error as a missing session, so a caller cannot enumerate
|
||||
// a session across a company, an owner, an adapter, or an environment.
|
||||
//
|
||||
// Each route writes its full path as a plain string literal, so the static
|
||||
// OpenAPI coverage test can read the path from the source text.
|
||||
|
||||
// The company-and-environment login serves only the Claude adapter.
|
||||
const CLAUDE_SETUP_TOKEN_ADAPTER_TYPE = "claude_local";
|
||||
|
||||
// Maps the internal session state to the public login status. The public union
|
||||
// carries no server-only state, so the route never returns the internal
|
||||
// `submitting` or `stored` state to a client.
|
||||
const toClaudeLoginStatus = (state: SetupTokenSessionState): AdapterAuthSessionStatus => {
|
||||
switch (state) {
|
||||
case "starting":
|
||||
return "starting";
|
||||
case "awaiting_code":
|
||||
case "submitting":
|
||||
case "stored":
|
||||
return "waiting_for_user";
|
||||
case "completed":
|
||||
return "authenticated";
|
||||
case "failed":
|
||||
return "failed";
|
||||
case "timed_out":
|
||||
return "timed_out";
|
||||
case "cancelled":
|
||||
return "cancelled";
|
||||
}
|
||||
return agent;
|
||||
};
|
||||
|
||||
router.post("/agents/:id/setup-token-login-sessions", async (req, res) => {
|
||||
const agent = await resolveSetupTokenAgent(req, res);
|
||||
if (!agent) return;
|
||||
const scope = buildSetupTokenScope(req, agent);
|
||||
// Builds the fixed, non-secret failure for a terminal failure state. A live or
|
||||
// a completed session has no failure. The failure carries a stable reason and
|
||||
// no secret detail.
|
||||
const toClaudeLoginFailure = (state: SetupTokenSessionState): AdapterAuthSessionFailure | null => {
|
||||
switch (state) {
|
||||
case "failed":
|
||||
return { reason: "failed", message: null };
|
||||
case "timed_out":
|
||||
return { reason: "timed_out", message: null };
|
||||
case "cancelled":
|
||||
return { reason: "cancelled", message: null };
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// The public login-session response. It carries no prompt and no secret.
|
||||
const toClaudePublicResponse = (
|
||||
descriptor: SetupTokenSessionDescriptor,
|
||||
): ClaudeSetupTokenSessionResponse => ({
|
||||
sessionId: descriptor.sessionId,
|
||||
environmentId: descriptor.environmentId,
|
||||
status: toClaudeLoginStatus(descriptor.state),
|
||||
expiresAt: new Date(descriptor.deadline).toISOString(),
|
||||
failure: toClaudeLoginFailure(descriptor.state),
|
||||
});
|
||||
|
||||
// The company-and-environment login key the non-start routes derive. The route
|
||||
// path gives the company, the actor gives the owner, and the route fixes the
|
||||
// adapter. The service matches this key and the agentless marker.
|
||||
const companySetupTokenKey = (companyId: string, ownerUserId: string) => ({
|
||||
companyId,
|
||||
ownerUserId,
|
||||
adapterType: CLAUDE_SETUP_TOKEN_ADAPTER_TYPE,
|
||||
});
|
||||
|
||||
// The stored Claude OAuth token status read. It returns
|
||||
// only the secret id and the latest version of the owner value; it returns no
|
||||
// token. The client reads the version, applies the stored token first, and
|
||||
// captures the version for a later confirmed overwrite. The route derives the
|
||||
// owner only from the authenticated actor and reads the fixed Claude
|
||||
// definition; it accepts no owner, no definition, and no secret id as input.
|
||||
//
|
||||
// The route returns the same fixed 404 for a missing owner value as the
|
||||
// company gate returns for a non-member, so it discloses no existence
|
||||
// distinction across owners or companies. It sets `Cache-Control: no-store`,
|
||||
// so no cache holds the metadata.
|
||||
router.get("/companies/:companyId/claude-oauth-token-status", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
const ownerUserId = resolveCompanySessionOwner(req, companyId, res);
|
||||
if (ownerUserId === null) return;
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
const status = await secretsSvc.readClaudeOAuthUserSecretStatus(companyId, ownerUserId);
|
||||
if (!status) {
|
||||
// A missing owner value returns the same fixed not-found as the non-member
|
||||
// gate, so a member without a value and a non-member look the same.
|
||||
res.status(404).json({ error: SETUP_TOKEN_SESSION_NOT_FOUND });
|
||||
return;
|
||||
}
|
||||
const body: ClaudeOAuthTokenStatusResponse = status;
|
||||
res.json(body);
|
||||
});
|
||||
|
||||
router.post("/companies/:companyId/setup-token-login-sessions", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
assertCompanyAccess(req, companyId);
|
||||
const ownerUserId = deriveSetupTokenOwnerUserId(req);
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
|
||||
// Parse the request with the shared strict validator before any session,
|
||||
// lease, or pseudo-terminal side effect. `.strict()` rejects an unknown field,
|
||||
// including a legacy `ttlSeconds`, with a fixed 400. The route echoes no input.
|
||||
const parsed = startClaudeSetupTokenSessionRequestSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
res.status(400).json({ error: "The Claude login start request is invalid." });
|
||||
return;
|
||||
}
|
||||
const { environmentId, adapterType } = parsed.data;
|
||||
const confirmedOverwrite: ClaudeSetupTokenOverwrite | null = parsed.data.overwrite ?? null;
|
||||
|
||||
// The company-and-environment login serves only the Claude adapter. A
|
||||
// different adapter type gets a fixed 400.
|
||||
if (adapterType !== CLAUDE_SETUP_TOKEN_ADAPTER_TYPE) {
|
||||
res.status(400).json({ error: "Only the claude_local adapter supports a setup-token login." });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!SETUP_TOKEN_LOGIN_TRANSPORT_READY) {
|
||||
// The live login transport binds at a later call site. Fail closed with the
|
||||
// fixed no-secret error until then.
|
||||
res.status(503).json({ error: SETUP_TOKEN_START_FAILED });
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve the environment server-side to a live instance sandbox. It fails
|
||||
// closed on a missing, archived, non-sandbox, or fake-provider environment, so
|
||||
// the route never persists an empty or invalid environment scope. It runs
|
||||
// before the session starts, so no store holds a rejected environment. It
|
||||
// also rejects an environment that another company owns (see
|
||||
// `assertSandboxLoginEnvironment`), so a login never runs in a foreign
|
||||
// company sandbox. It also fails closed when the provider does not advertise
|
||||
// the setup-token login capability, so an unsupported provider never reaches
|
||||
// a session row, a lease, or a pseudo-terminal.
|
||||
await assertSandboxLoginEnvironment(companyId, environmentId, {
|
||||
requireSetupTokenLoginProvider: true,
|
||||
});
|
||||
|
||||
const scope: SetupTokenSessionScope = {
|
||||
companyId,
|
||||
ownerUserId,
|
||||
targetAgentId: null,
|
||||
adapterType,
|
||||
environmentId,
|
||||
confirmedOverwrite,
|
||||
};
|
||||
try {
|
||||
const started = await setupTokenLoginService.start(scope);
|
||||
res.status(201).json({ sessionId: started.sessionId, state: started.state });
|
||||
const descriptor = setupTokenLoginService.describeOwned(started.sessionId, scope);
|
||||
// The start response carries the panel mode, so the client renders the
|
||||
// browser-code panel. The full login URL rides only through the
|
||||
// guarded prompt read, not the start response, so the prompt is null here.
|
||||
// The client reads the prompt route for the login URL.
|
||||
const body: ClaudeSetupTokenSessionOwnerResponse = {
|
||||
...toClaudePublicResponse(descriptor),
|
||||
panelMode: "submitted_browser_code",
|
||||
prompt: null,
|
||||
};
|
||||
res.status(201).json(body);
|
||||
} catch (err) {
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/agents/:id/setup-token-login-sessions/:sessionId/prompt", async (req, res) => {
|
||||
const agent = await resolveSetupTokenAgent(req, res);
|
||||
if (!agent) return;
|
||||
const scope = buildSetupTokenScope(req, agent);
|
||||
router.get("/companies/:companyId/setup-token-login-sessions/:sessionId", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
const ownerUserId = resolveCompanySessionOwner(req, companyId, res);
|
||||
if (ownerUserId === null) return;
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
// SR-6 and SR-7: the full login URL is a confidential response.
|
||||
if (!enforceSetupTokenTransport(req, res)) return;
|
||||
try {
|
||||
const view = setupTokenLoginService.readPrompt(req.params.sessionId as string, scope);
|
||||
// SR-5: the full login URL rides only in this authorized owner response.
|
||||
res.json({ state: view.state, loginUrl: view.loginUrl });
|
||||
const sessionId = req.params.sessionId as string;
|
||||
const scope = setupTokenLoginService.resolveCompanyScope(
|
||||
sessionId,
|
||||
companySetupTokenKey(companyId, ownerUserId),
|
||||
);
|
||||
const descriptor = setupTokenLoginService.describeOwned(sessionId, scope);
|
||||
// The status response is public. It carries no prompt and no secret.
|
||||
res.json(toClaudePublicResponse(descriptor));
|
||||
} catch (err) {
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/agents/:id/setup-token-login-sessions/:sessionId/code", async (req, res) => {
|
||||
const agent = await resolveSetupTokenAgent(req, res);
|
||||
if (!agent) return;
|
||||
const scope = buildSetupTokenScope(req, agent);
|
||||
const browserCode = typeof req.body?.browserCode === "string" ? req.body.browserCode : null;
|
||||
router.get("/companies/:companyId/setup-token-login-sessions/:sessionId/prompt", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
const ownerUserId = resolveCompanySessionOwner(req, companyId, res);
|
||||
if (ownerUserId === null) return;
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
// SR-6 and SR-7: the browser code is the confidential OAuth authorization
|
||||
// secret. Enforce the fail-closed confidential transport guard before the
|
||||
// route reads it, so the code never rides an untrusted transport.
|
||||
if (!enforceSetupTokenTransport(req, res)) return;
|
||||
if (!browserCode) {
|
||||
// The route echoes no input; it returns fixed error text only (SR-1).
|
||||
res.status(400).json({ error: "A browserCode is required." });
|
||||
// The full login URL is a confidential response. The route
|
||||
// does not force TLS. It attaches a non-blocking advisory instead.
|
||||
const transportAdvisory = assessSetupTokenTransport(req);
|
||||
try {
|
||||
const sessionId = req.params.sessionId as string;
|
||||
const scope = setupTokenLoginService.resolveCompanyScope(
|
||||
sessionId,
|
||||
companySetupTokenKey(companyId, ownerUserId),
|
||||
);
|
||||
const descriptor = setupTokenLoginService.describeOwned(sessionId, scope);
|
||||
if (!descriptor.loginUrl) {
|
||||
// The prompt has not surfaced yet. Return the same not-found error as a
|
||||
// missing or a foreign session, so the route never confirms the session
|
||||
// exists before the URL is ready.
|
||||
res.status(404).json({ error: SETUP_TOKEN_SESSION_NOT_FOUND });
|
||||
return;
|
||||
}
|
||||
// The full login URL rides only in this authorized owner response.
|
||||
const body: ClaudeSetupTokenSessionPrompt = {
|
||||
authorizationUrl: descriptor.loginUrl,
|
||||
transportAdvisory,
|
||||
};
|
||||
res.json(body);
|
||||
} catch (err) {
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/companies/:companyId/setup-token-login-sessions/:sessionId/code", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
const ownerUserId = resolveCompanySessionOwner(req, companyId, res);
|
||||
if (ownerUserId === null) return;
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
// The browser code is the confidential OAuth authorization
|
||||
// secret. The route does not force TLS. It attaches a non-blocking advisory
|
||||
// to the response instead, so the client can show a disclaimer.
|
||||
const transportAdvisory = assessSetupTokenTransport(req);
|
||||
// Parse the request with the shared strict validator before the route forwards
|
||||
// the code to the live process. `.strict()` rejects an unknown field, and the
|
||||
// grammar rejects an empty, an oversized, or a control-byte code. The route
|
||||
// echoes no input; it returns fixed error text only.
|
||||
const parsed = submitBrowserCodeRequestSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
res.status(400).json({ error: "A valid browser code is required." });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const result = setupTokenLoginService.submitCode(req.params.sessionId as string, scope, browserCode);
|
||||
res.json({ state: result.state });
|
||||
const sessionId = req.params.sessionId as string;
|
||||
const scope = setupTokenLoginService.resolveCompanyScope(
|
||||
sessionId,
|
||||
companySetupTokenKey(companyId, ownerUserId),
|
||||
);
|
||||
setupTokenLoginService.submitCode(sessionId, scope, parsed.data.browserCode);
|
||||
const descriptor = setupTokenLoginService.describeOwned(sessionId, scope);
|
||||
const body: ClaudeSetupTokenSessionResponse = {
|
||||
...toClaudePublicResponse(descriptor),
|
||||
transportAdvisory,
|
||||
};
|
||||
res.json(body);
|
||||
} catch (err) {
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/agents/:id/setup-token-login-sessions/:sessionId/cancel", async (req, res) => {
|
||||
const agent = await resolveSetupTokenAgent(req, res);
|
||||
if (!agent) return;
|
||||
const scope = buildSetupTokenScope(req, agent);
|
||||
try {
|
||||
const result = await setupTokenLoginService.cancel(req.params.sessionId as string, scope);
|
||||
res.json({ state: result.state });
|
||||
} catch (err) {
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/agents/:id/setup-token-login-sessions/:sessionId/expire", async (req, res) => {
|
||||
const agent = await resolveSetupTokenAgent(req, res);
|
||||
if (!agent) return;
|
||||
const scope = buildSetupTokenScope(req, agent);
|
||||
try {
|
||||
const result = await setupTokenLoginService.expire(req.params.sessionId as string, scope);
|
||||
res.json({ state: result.state });
|
||||
} catch (err) {
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/agents/:id/setup-token-login-sessions/:sessionId/token", async (req, res) => {
|
||||
const agent = await resolveSetupTokenAgent(req, res);
|
||||
if (!agent) return;
|
||||
const scope = buildSetupTokenScope(req, agent);
|
||||
router.post("/companies/:companyId/setup-token-login-sessions/:sessionId/completion", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
const ownerUserId = resolveCompanySessionOwner(req, companyId, res);
|
||||
if (ownerUserId === null) return;
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
// SR-6 and SR-7: the token is a confidential response.
|
||||
if (!enforceSetupTokenTransport(req, res)) return;
|
||||
try {
|
||||
// The service returns the token one time from a completed session. It
|
||||
// returns the fixed unavailable error when the token is not ready or the
|
||||
// owner already received it. The full token rides only in this authorized
|
||||
// owner response over the confidential transport (SR-5, SR-6, SR-7).
|
||||
const result = setupTokenLoginService.receiveToken(req.params.sessionId as string, scope);
|
||||
res.json({ token: result.token });
|
||||
const sessionId = req.params.sessionId as string;
|
||||
const scope = setupTokenLoginService.resolveCompanyScope(
|
||||
sessionId,
|
||||
companySetupTokenKey(companyId, ownerUserId),
|
||||
);
|
||||
// The service returns the non-secret `storedSessionId` claim from a
|
||||
// completed session whose owner-bound secret write succeeded. The response
|
||||
// carries no token.
|
||||
const result = setupTokenLoginService.completeSession(sessionId, scope);
|
||||
const body: ClaudeSetupTokenCompletionResponse = { storedSessionId: result.storedSessionId };
|
||||
res.json(body);
|
||||
} catch (err) {
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/companies/:companyId/setup-token-login-sessions/:sessionId/cancel", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
const ownerUserId = resolveCompanySessionOwner(req, companyId, res);
|
||||
if (ownerUserId === null) return;
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
const sessionId = req.params.sessionId as string;
|
||||
try {
|
||||
const scope = setupTokenLoginService.resolveCompanyScope(
|
||||
sessionId,
|
||||
companySetupTokenKey(companyId, ownerUserId),
|
||||
);
|
||||
await setupTokenLoginService.cancel(sessionId, scope);
|
||||
res.status(200).json({});
|
||||
} catch (err) {
|
||||
// Cancel is idempotent. The service removes a session when it reaches a
|
||||
// terminal state, so a repeat cancel, a cancel after a timeout, or a
|
||||
// cancel of an unknown session finds no record and throws the fixed
|
||||
// not-found error. Return the same success as an active cancel, so the
|
||||
// client stops the poll and returns to its start state.
|
||||
//
|
||||
// This keeps the not-found uniform. The 200 response is identical
|
||||
// for a missing session, an already-terminal session, and a foreign
|
||||
// session, so the route never confirms a session exists and cancels
|
||||
// nothing for a foreign id. A non-member still fails closed with a 404 at
|
||||
// the company-access gate above, before this handler runs. A non-404
|
||||
// error still surfaces.
|
||||
if (err instanceof SetupTokenSessionError && err.status === 404) {
|
||||
res.status(200).json({});
|
||||
return;
|
||||
}
|
||||
sendSetupTokenError(res, err);
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/companies/:companyId/heartbeat-runs", async (req, res) => {
|
||||
const companyId = req.params.companyId as string;
|
||||
assertCompanyAccess(req, companyId);
|
||||
|
||||
@@ -583,6 +583,12 @@ export function environmentRoutes(
|
||||
if (impact.staticReferences.isInstanceDefault) {
|
||||
return "Cannot delete the current instance default environment. Set a new default environment before deleting this one.";
|
||||
}
|
||||
if (impact.pendingCleanupLeaseCount > 0) {
|
||||
return "Cannot delete this environment while a sandbox cleanup is pending. Wait for the cleanup sweep to destroy the orphan sandbox, then retry.";
|
||||
}
|
||||
if (impact.reusableSandboxLeaseCount > 0) {
|
||||
return "Cannot delete this environment while it has a reusable sandbox lease. Remove the associated execution workspace or issue so Paperclip can destroy the sandbox, then retry.";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -735,6 +741,7 @@ export function environmentRoutes(
|
||||
templateRefKind: driver.templateRefKind,
|
||||
templateConfigBinding: driver.templateConfigBinding,
|
||||
supportsTemplateDelete: driver.supportsTemplateDelete,
|
||||
supportsSetupTokenLogin: driver.supportsSetupTokenLogin ?? false,
|
||||
displayName: driver.displayName,
|
||||
description: driver.description,
|
||||
source: "plugin" as const,
|
||||
@@ -1089,6 +1096,27 @@ export function environmentRoutes(
|
||||
}),
|
||||
});
|
||||
assertNoClientPlatformProvisionedMarkers(req.body.metadata);
|
||||
// The durable `pending_cleanup` lease row stores the provider, the provider
|
||||
// lease id, and the immutable config metadata for an orphan sandbox. The
|
||||
// teardown retry reads that row alone and never reads the current environment
|
||||
// provider. So a provider change or an environment delete after the record
|
||||
// lands cannot strand the teardown. That immutable record is the correctness
|
||||
// invariant.
|
||||
//
|
||||
// This pre-transaction check is a best-effort fast-fail only. It rejects a
|
||||
// provider change while a known `pending_cleanup` lease exists, so the
|
||||
// operator resolves the cleanup first. An orphan record that lands after this
|
||||
// check still carries its own immutable teardown context, so the
|
||||
// time-of-check-to-time-of-use window here cannot strand a sandbox.
|
||||
const changesProviderTarget =
|
||||
(req.body.driver !== undefined && req.body.driver !== existing.driver) ||
|
||||
req.body.config !== undefined;
|
||||
if (changesProviderTarget && (await svc.hasUnresolvedPendingCleanupLeases(existing.id))) {
|
||||
throw conflict(
|
||||
"Cannot change the driver or provider config while a sandbox cleanup is pending. Wait for the cleanup sweep to destroy the orphan sandbox, then retry.",
|
||||
{ code: "environment_pending_sandbox_cleanup" },
|
||||
);
|
||||
}
|
||||
const actor = getActorInfo(req);
|
||||
const nextDriver = req.body.driver ?? existing.driver;
|
||||
const nextName = req.body.name ?? existing.name;
|
||||
|
||||
@@ -224,6 +224,13 @@ import {
|
||||
importMcpJsonSchema,
|
||||
toolPolicyTestRequestSchema,
|
||||
createToolMcpGatewaySchema,
|
||||
startClaudeSetupTokenSessionRequestSchema,
|
||||
submitBrowserCodeRequestSchema,
|
||||
claudeSetupTokenSessionResponseSchema,
|
||||
claudeSetupTokenSessionPromptSchema,
|
||||
claudeSetupTokenSessionOwnerResponseSchema,
|
||||
claudeSetupTokenCompletionResponseSchema,
|
||||
claudeOAuthTokenStatusResponseSchema,
|
||||
} from "@paperclipai/shared";
|
||||
import {
|
||||
COMPANY_IMPORT_TRANSFERS_API_PATH,
|
||||
@@ -385,6 +392,10 @@ function zodToOpenApiSchema(schema: z.ZodTypeAny): JsonSchema {
|
||||
}
|
||||
const jsonSchema: JsonSchema = { type: "object", properties };
|
||||
if (required.length > 0) jsonSchema.required = required;
|
||||
// A `.strict()` Zod object forbids an unknown key. Publish that constraint
|
||||
// as `additionalProperties: false`, so a client, a gateway, or a handler
|
||||
// that treats the contract as authoritative rejects an extra property too.
|
||||
if (unwrapped._def.unknownKeys === "strict") jsonSchema.additionalProperties = false;
|
||||
return jsonSchema;
|
||||
}
|
||||
|
||||
@@ -4498,18 +4509,44 @@ registry.registerPath({
|
||||
responses: { 200: r.ok(), 401: r.unauthorized },
|
||||
});
|
||||
|
||||
// Setup-token login session routes. The owner user starts one session, reads the
|
||||
// login prompt, submits the browser code, and receives the token. The prompt,
|
||||
// code, and token responses require a confidential transport; the guard returns
|
||||
// 403 when the transport is not confidential.
|
||||
// Company-and-environment Claude setup-token login session routes. The owner
|
||||
// user starts one session for one adapter in one environment. The scope carries
|
||||
// no agent id, so a hire flow with no agent still starts one session. The owner
|
||||
// reads the login prompt, submits the browser code from the browser, and reads
|
||||
// the completion claim. The prompt, code, and completion responses require a
|
||||
// confidential transport; the guard returns 403 when the transport is not
|
||||
// confidential. No response carries a token; the completion returns the
|
||||
// non-secret `storedSessionId` claim.
|
||||
//
|
||||
// The stored-token status read returns only the secret id and the latest version
|
||||
// of the owner value; it returns no token. The server derives the owner from the
|
||||
// authenticated actor. A missing or a foreign value returns the same fixed 404,
|
||||
// so the read discloses no existence distinction. The response is `no-store`.
|
||||
registry.registerPath({
|
||||
method: "get",
|
||||
path: "/api/companies/{companyId}/claude-oauth-token-status",
|
||||
tags: ["companies"],
|
||||
summary: "Read the stored Claude OAuth token status for the authenticated owner",
|
||||
request: { params: z.object({ companyId: z.string() }) },
|
||||
responses: {
|
||||
200: r.ok(claudeOAuthTokenStatusResponseSchema),
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
404: r.notFound,
|
||||
},
|
||||
});
|
||||
|
||||
registry.registerPath({
|
||||
method: "post",
|
||||
path: "/api/agents/{id}/setup-token-login-sessions",
|
||||
tags: ["agents"],
|
||||
summary: "Start a setup-token login session for an agent",
|
||||
request: { params: z.object({ id: z.string() }) },
|
||||
path: "/api/companies/{companyId}/setup-token-login-sessions",
|
||||
tags: ["companies"],
|
||||
summary: "Start a company-and-environment Claude setup-token login session",
|
||||
request: {
|
||||
params: z.object({ companyId: z.string() }),
|
||||
body: jsonBody(startClaudeSetupTokenSessionRequestSchema),
|
||||
},
|
||||
responses: {
|
||||
201: r.ok(),
|
||||
201: r.ok(claudeSetupTokenSessionOwnerResponseSchema),
|
||||
400: r.badRequest,
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
@@ -4520,12 +4557,26 @@ registry.registerPath({
|
||||
|
||||
registry.registerPath({
|
||||
method: "get",
|
||||
path: "/api/agents/{id}/setup-token-login-sessions/{sessionId}/prompt",
|
||||
tags: ["agents"],
|
||||
summary: "Read the login prompt for a setup-token login session",
|
||||
request: { params: z.object({ id: z.string(), sessionId: z.string() }) },
|
||||
path: "/api/companies/{companyId}/setup-token-login-sessions/{sessionId}",
|
||||
tags: ["companies"],
|
||||
summary: "Read the status of a Claude setup-token login session",
|
||||
request: { params: z.object({ companyId: z.string(), sessionId: z.string() }) },
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
200: r.ok(claudeSetupTokenSessionResponseSchema),
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
404: r.notFound,
|
||||
},
|
||||
});
|
||||
|
||||
registry.registerPath({
|
||||
method: "get",
|
||||
path: "/api/companies/{companyId}/setup-token-login-sessions/{sessionId}/prompt",
|
||||
tags: ["companies"],
|
||||
summary: "Read the login prompt for a Claude setup-token login session",
|
||||
request: { params: z.object({ companyId: z.string(), sessionId: z.string() }) },
|
||||
responses: {
|
||||
200: r.ok(claudeSetupTokenSessionPromptSchema),
|
||||
400: r.badRequest,
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
@@ -4535,12 +4586,15 @@ registry.registerPath({
|
||||
|
||||
registry.registerPath({
|
||||
method: "post",
|
||||
path: "/api/agents/{id}/setup-token-login-sessions/{sessionId}/code",
|
||||
tags: ["agents"],
|
||||
summary: "Submit the browser code for a setup-token login session",
|
||||
request: { params: z.object({ id: z.string(), sessionId: z.string() }) },
|
||||
path: "/api/companies/{companyId}/setup-token-login-sessions/{sessionId}/code",
|
||||
tags: ["companies"],
|
||||
summary: "Submit the browser code for a Claude setup-token login session",
|
||||
request: {
|
||||
params: z.object({ companyId: z.string(), sessionId: z.string() }),
|
||||
body: jsonBody(submitBrowserCodeRequestSchema),
|
||||
},
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
200: r.ok(claudeSetupTokenSessionResponseSchema),
|
||||
400: r.badRequest,
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
@@ -4550,40 +4604,12 @@ registry.registerPath({
|
||||
|
||||
registry.registerPath({
|
||||
method: "post",
|
||||
path: "/api/agents/{id}/setup-token-login-sessions/{sessionId}/cancel",
|
||||
tags: ["agents"],
|
||||
summary: "Cancel a setup-token login session",
|
||||
request: { params: z.object({ id: z.string(), sessionId: z.string() }) },
|
||||
path: "/api/companies/{companyId}/setup-token-login-sessions/{sessionId}/completion",
|
||||
tags: ["companies"],
|
||||
summary: "Read the completion claim of a Claude setup-token login session",
|
||||
request: { params: z.object({ companyId: z.string(), sessionId: z.string() }) },
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
404: r.notFound,
|
||||
},
|
||||
});
|
||||
|
||||
registry.registerPath({
|
||||
method: "post",
|
||||
path: "/api/agents/{id}/setup-token-login-sessions/{sessionId}/expire",
|
||||
tags: ["agents"],
|
||||
summary: "Expire a setup-token login session",
|
||||
request: { params: z.object({ id: z.string(), sessionId: z.string() }) },
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
404: r.notFound,
|
||||
},
|
||||
});
|
||||
|
||||
registry.registerPath({
|
||||
method: "post",
|
||||
path: "/api/agents/{id}/setup-token-login-sessions/{sessionId}/token",
|
||||
tags: ["agents"],
|
||||
summary: "Receive the token from a completed setup-token login session",
|
||||
request: { params: z.object({ id: z.string(), sessionId: z.string() }) },
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
200: r.ok(claudeSetupTokenCompletionResponseSchema),
|
||||
400: r.badRequest,
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
@@ -4591,6 +4617,25 @@ registry.registerPath({
|
||||
},
|
||||
});
|
||||
|
||||
registry.registerPath({
|
||||
method: "post",
|
||||
path: "/api/companies/{companyId}/setup-token-login-sessions/{sessionId}/cancel",
|
||||
tags: ["companies"],
|
||||
summary: "Cancel a Claude setup-token login session",
|
||||
// The 404 is reserved for the pre-scope non-member gate. The company-access
|
||||
// gate runs before the cancel logic and returns a fixed 404 for a non-member.
|
||||
// Cancel itself is idempotent and stays uniform: a same-company owner-scoped
|
||||
// missing, terminal, or foreign session id all return 200. So the route never
|
||||
// confirms a session exists for the owner.
|
||||
request: { params: z.object({ companyId: z.string(), sessionId: z.string() }) },
|
||||
responses: {
|
||||
200: r.ok(),
|
||||
401: r.unauthorized,
|
||||
403: r.forbidden,
|
||||
404: r.notFound,
|
||||
},
|
||||
});
|
||||
|
||||
// ─── Issue interactions & tree ───────────────────────────────────────────────
|
||||
|
||||
registry.registerPath({
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -29,7 +29,14 @@ import { conflict, notFound, unprocessable } from "../errors.js";
|
||||
import { syncAgentAdapterEnvBindings } from "./agent-secret-bindings.js";
|
||||
import { normalizeAgentPermissions } from "./agent-permissions.js";
|
||||
import { REDACTED_EVENT_VALUE, sanitizeRecord } from "../redaction.js";
|
||||
import { secretService } from "./secrets.js";
|
||||
import {
|
||||
assertClaudeOAuthBindingInvariant,
|
||||
claudeOAuthClaimRejectedError,
|
||||
CLAUDE_LOCAL_ADAPTER_TYPE,
|
||||
secretService,
|
||||
type ClaudeOAuthBindingInvariantDecision,
|
||||
} from "./secrets.js";
|
||||
import { createDbSetupTokenCleanupStore } from "./setup-token-session.js";
|
||||
import {
|
||||
builtInAgentMarkersEqual,
|
||||
readBuiltInAgentMarker,
|
||||
@@ -69,14 +76,36 @@ interface RevisionMetadata {
|
||||
rolledBackFromRevisionId?: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Claude login context for an agent write. The route derives the owner user
|
||||
* from the authenticated actor, not from the request body, and forwards the
|
||||
* non-secret `storedSessionId` claim from a completed Claude login session. A
|
||||
* controlled internal override permits a migration or an administrator repair to
|
||||
* bind or unbind the fixed OAuth token without a claim.
|
||||
*
|
||||
* The `applyExistingWithoutClaim` field is the user-actor apply-existing path.
|
||||
* The route sets it only for an authenticated user actor and derives the owner
|
||||
* from that actor. The path binds the fixed reference to the owner stored value
|
||||
* with no login round trip. It is distinct from `allowInternalBindingOverride`,
|
||||
* which does no ownership check.
|
||||
*/
|
||||
interface ClaudeLoginContext {
|
||||
storedSessionId?: string | null;
|
||||
ownerUserId?: string | null;
|
||||
allowInternalBindingOverride?: boolean;
|
||||
applyExistingWithoutClaim?: boolean;
|
||||
}
|
||||
|
||||
interface UpdateAgentOptions {
|
||||
recordRevision?: RevisionMetadata;
|
||||
allowBuiltInAgentMetadata?: boolean;
|
||||
allowPendingApprovalConfigUpdate?: boolean;
|
||||
claudeLogin?: ClaudeLoginContext;
|
||||
}
|
||||
|
||||
interface CreateAgentOptions {
|
||||
allowBuiltInAgentMetadata?: boolean;
|
||||
claudeLogin?: ClaudeLoginContext;
|
||||
}
|
||||
|
||||
interface AgentShortnameRow {
|
||||
@@ -447,6 +476,83 @@ export function agentService(db: Db) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Enforces the Claude OAuth binding claim inside a write transaction. It runs
|
||||
* after {@link assertClaudeOAuthBindingInvariant} decided that the write
|
||||
* introduces or keeps the fixed binding.
|
||||
*
|
||||
* When the write introduces the fixed binding:
|
||||
* * A create or hire path (`consume: true`) consumes a stored-session claim
|
||||
* with one conditional write. It builds the claim scope from the company,
|
||||
* the owner user, the fixed adapter, the environment, and the
|
||||
* `storedSessionId`. It inserts the binding only when the write returns one
|
||||
* row; otherwise it raises the fixed claim error, which rolls back the
|
||||
* whole transaction and inserts no binding.
|
||||
* * An update, approval, or rollback path (`consume: false`) carries no
|
||||
* claim, so it raises the same fixed claim error at once.
|
||||
*
|
||||
* The user-actor apply-existing path (`applyExistingWithoutClaim`) binds the
|
||||
* fixed reference with no login round trip. The route sets the flag only for
|
||||
* an authenticated user actor and derives the owner from that actor. The gate
|
||||
* permits the no-claim bind only when the owner already has a stored value for
|
||||
* the company. It reads the owner value status; it reads no token. A missing
|
||||
* owner or a missing stored value raises the same fixed claim error, so the
|
||||
* caller cannot tell the reasons apart.
|
||||
*
|
||||
* A controlled internal override skips the claim for a migration or an
|
||||
* administrator repair. The function creates the fixed user-secret definition
|
||||
* before the caller runs the declaration synchronization, so the synchronized
|
||||
* declaration always references an existing definition.
|
||||
*/
|
||||
async function enforceClaudeOAuthBindingClaim(
|
||||
txDb: Db,
|
||||
input: {
|
||||
companyId: string;
|
||||
decision: ClaudeOAuthBindingInvariantDecision;
|
||||
consume: boolean;
|
||||
environmentId: string | null;
|
||||
claudeLogin?: ClaudeLoginContext;
|
||||
},
|
||||
): Promise<void> {
|
||||
const ownerUserId = input.claudeLogin?.ownerUserId ?? null;
|
||||
if (input.decision.introducesBinding && !input.claudeLogin?.allowInternalBindingOverride) {
|
||||
if (input.claudeLogin?.applyExistingWithoutClaim) {
|
||||
// The user-actor apply-existing path. The route derived the owner from
|
||||
// the authenticated user actor. The gate binds the fixed reference only
|
||||
// when that owner already has a stored value. It reads no token.
|
||||
if (!ownerUserId) {
|
||||
throw claudeOAuthClaimRejectedError();
|
||||
}
|
||||
const stored = await secretService(txDb).readClaudeOAuthUserSecretStatus(
|
||||
input.companyId,
|
||||
ownerUserId,
|
||||
);
|
||||
if (!stored) {
|
||||
throw claudeOAuthClaimRejectedError();
|
||||
}
|
||||
} else if (!input.consume) {
|
||||
throw claudeOAuthClaimRejectedError();
|
||||
} else {
|
||||
const consumed = await createDbSetupTokenCleanupStore(txDb).consumeStoredClaim({
|
||||
sessionId: input.claudeLogin?.storedSessionId ?? "",
|
||||
companyId: input.companyId,
|
||||
ownerUserId: ownerUserId ?? "",
|
||||
adapterType: CLAUDE_LOCAL_ADAPTER_TYPE,
|
||||
environmentId: input.environmentId ?? "",
|
||||
});
|
||||
if (!consumed) {
|
||||
throw claudeOAuthClaimRejectedError();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (input.decision.introducesBinding || input.decision.keepsBinding) {
|
||||
// Create the fixed definition before declaration synchronization.
|
||||
await secretService(txDb).ensureClaudeOAuthUserSecretDefinition(input.companyId, {
|
||||
userId: ownerUserId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function assertBuiltInAgentMetadataMutationAllowed(
|
||||
beforeMetadata: unknown,
|
||||
afterMetadata: unknown,
|
||||
@@ -526,6 +632,17 @@ export function agentService(db: Db) {
|
||||
{ adapterType: (normalizedPatch.adapterType ?? existing.adapterType) as string },
|
||||
);
|
||||
}
|
||||
// Run the server-enforced binding invariant when the patch touches the
|
||||
// adapter config. The update, approval, and rollback paths keep an existing
|
||||
// fixed binding but reject a newly introduced binding, because they carry no
|
||||
// stored-session claim.
|
||||
const bindingDecision = Object.prototype.hasOwnProperty.call(normalizedPatch, "adapterConfig")
|
||||
? assertClaudeOAuthBindingInvariant({
|
||||
adapterType: (normalizedPatch.adapterType ?? existing.adapterType) as string,
|
||||
nextConfig: normalizedPatch.adapterConfig,
|
||||
priorConfig: existing.adapterConfig,
|
||||
})
|
||||
: null;
|
||||
|
||||
const shouldRecordRevision = Boolean(options?.recordRevision) && hasConfigPatchFields(normalizedPatch);
|
||||
const beforeConfig = shouldRecordRevision ? buildConfigSnapshot(existing) : null;
|
||||
@@ -541,6 +658,15 @@ export function agentService(db: Db) {
|
||||
if (!updated) return null;
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(normalizedPatch, "adapterConfig")) {
|
||||
if (bindingDecision) {
|
||||
await enforceClaudeOAuthBindingClaim(txDb, {
|
||||
companyId: existing.companyId,
|
||||
decision: bindingDecision,
|
||||
consume: false,
|
||||
environmentId: null,
|
||||
claudeLogin: options?.claudeLogin,
|
||||
});
|
||||
}
|
||||
await syncAgentSecretBindings(updated, txDb);
|
||||
}
|
||||
|
||||
@@ -612,8 +738,25 @@ export function agentService(db: Db) {
|
||||
const adapterConfig = isPlainRecord(data.adapterConfig)
|
||||
? await secretsSvc.normalizeAdapterConfigForPersistence(companyId, data.adapterConfig, { adapterType })
|
||||
: {};
|
||||
// Run the server-enforced binding invariant after generic normalization
|
||||
// and before any database write. A create has no prior config.
|
||||
const bindingDecision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType,
|
||||
nextConfig: adapterConfig,
|
||||
priorConfig: null,
|
||||
});
|
||||
return db.transaction(async (tx) => {
|
||||
const txDb = tx as unknown as Db;
|
||||
// Consume the stored-session claim and create the fixed definition inside
|
||||
// the same transaction that inserts the binding. A rejected claim rolls
|
||||
// back the whole transaction and inserts no binding.
|
||||
await enforceClaudeOAuthBindingClaim(txDb, {
|
||||
companyId,
|
||||
decision: bindingDecision,
|
||||
consume: true,
|
||||
environmentId: (data.defaultEnvironmentId as string | null | undefined) ?? null,
|
||||
claudeLogin: options?.claudeLogin,
|
||||
});
|
||||
const created = await tx
|
||||
.insert(agents)
|
||||
.values({
|
||||
@@ -790,6 +933,7 @@ export function agentService(db: Db) {
|
||||
if (!existing || existing.status !== "pending_approval") return null;
|
||||
const approvedPatch = approvedPayload ? configPatchFromApprovalPayload(approvedPayload) : {};
|
||||
let patch = { ...approvedPatch } as Partial<typeof agents.$inferInsert>;
|
||||
let approvalBindingDecision: ClaudeOAuthBindingInvariantDecision | null = null;
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(patch, "adapterConfig") &&
|
||||
isPlainRecord(patch.adapterConfig)
|
||||
@@ -799,6 +943,13 @@ export function agentService(db: Db) {
|
||||
patch.adapterConfig,
|
||||
{ adapterType: (patch.adapterType ?? existing.adapterType) as string },
|
||||
);
|
||||
// The approval activation keeps an existing fixed binding but rejects a
|
||||
// newly introduced binding, because it carries no stored-session claim.
|
||||
approvalBindingDecision = assertClaudeOAuthBindingInvariant({
|
||||
adapterType: (patch.adapterType ?? existing.adapterType) as string,
|
||||
nextConfig: patch.adapterConfig,
|
||||
priorConfig: existing.adapterConfig,
|
||||
});
|
||||
}
|
||||
if (patch.permissions !== undefined) {
|
||||
patch.permissions = normalizeAgentPermissions(
|
||||
@@ -813,6 +964,14 @@ export function agentService(db: Db) {
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (!updated) return null;
|
||||
if (approvalBindingDecision) {
|
||||
await enforceClaudeOAuthBindingClaim(txDb, {
|
||||
companyId: existing.companyId,
|
||||
decision: approvalBindingDecision,
|
||||
consume: false,
|
||||
environmentId: null,
|
||||
});
|
||||
}
|
||||
await syncAgentSecretBindings(updated, txDb);
|
||||
const agent = await agentService(txDb).getById(updated.id);
|
||||
if (!agent) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import type { PaperclipPluginManifestV1 } from "@paperclipai/shared";
|
||||
|
||||
/**
|
||||
* Bundled plugin auto-provisioning.
|
||||
@@ -190,16 +191,23 @@ interface RegistryPluginRow {
|
||||
id: string;
|
||||
pluginKey: string;
|
||||
status: string;
|
||||
version: string;
|
||||
manifestJson: PaperclipPluginManifestV1;
|
||||
}
|
||||
|
||||
export interface BundledPluginProvisionerDeps {
|
||||
registry: {
|
||||
getByKey(pluginKey: string): Promise<RegistryPluginRow | null>;
|
||||
update(
|
||||
id: string,
|
||||
data: { version?: string; manifest?: PaperclipPluginManifestV1 },
|
||||
): Promise<unknown>;
|
||||
};
|
||||
loader: {
|
||||
installPlugin(options: { localPath: string }): Promise<{
|
||||
manifest: { id: string } | null;
|
||||
}>;
|
||||
loadManifest(packagePath: string): Promise<PaperclipPluginManifestV1 | null>;
|
||||
};
|
||||
lifecycle: {
|
||||
load(pluginId: string): Promise<unknown>;
|
||||
@@ -216,6 +224,49 @@ function defaultBundleManifestExists(localPath: string): boolean {
|
||||
return fs.existsSync(path.join(localPath, "dist", "manifest.js"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconcile a present bundled plugin's persisted manifest with the shipped
|
||||
* bundle. The bundle is part of the release image, so its manifest is the
|
||||
* source of truth. When the bundle declares a version that differs from the
|
||||
* persisted version, update the stored manifest and version. This propagates
|
||||
* a manifest change (for example a new driver capability) to an existing
|
||||
* install that the auto-install path skips.
|
||||
*
|
||||
* The step is fail-safe. A missing bundle, a manifest read error, or a
|
||||
* database error is caught, logged, and swallowed, so boot always completes.
|
||||
* The step updates only the stored manifest row; it never restarts the worker.
|
||||
*/
|
||||
async function reconcileBundledPluginManifest(
|
||||
existing: RegistryPluginRow,
|
||||
install: ResolvedBundledPlugin,
|
||||
deps: BundledPluginProvisionerDeps,
|
||||
bundleManifestExists: (localPath: string) => boolean,
|
||||
): Promise<void> {
|
||||
try {
|
||||
if (!bundleManifestExists(install.localPath)) return;
|
||||
const bundleManifest = await deps.loader.loadManifest(install.localPath);
|
||||
if (!bundleManifest) return;
|
||||
if (bundleManifest.version === existing.version) return;
|
||||
await deps.registry.update(existing.id, {
|
||||
version: bundleManifest.version,
|
||||
manifest: bundleManifest,
|
||||
});
|
||||
deps.logger.info(
|
||||
{
|
||||
pluginKey: install.pluginKey,
|
||||
fromVersion: existing.version,
|
||||
toVersion: bundleManifest.version,
|
||||
},
|
||||
"reconciled bundled plugin manifest to the shipped bundle version",
|
||||
);
|
||||
} catch (err) {
|
||||
deps.logger.error(
|
||||
{ err, pluginKey: install.pluginKey },
|
||||
"Failed to reconcile bundled plugin manifest; continuing boot with the stored manifest",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure each resolved bundled plugin is installed and loaded.
|
||||
*
|
||||
@@ -226,7 +277,9 @@ function defaultBundleManifestExists(localPath: string): boolean {
|
||||
*
|
||||
* Skip semantics:
|
||||
* - A plugin present in any non-uninstalled state is skipped, so an
|
||||
* operator-disabled plugin is not silently re-enabled on reboot.
|
||||
* operator-disabled plugin is not silently re-enabled on reboot. Before the
|
||||
* skip, the persisted manifest is reconciled to the shipped bundle version
|
||||
* (see `reconcileBundledPluginManifest`).
|
||||
* - A soft-uninstalled plugin is reinstalled only when
|
||||
* `reinstallUninstalled` is set (managed mode, where the control plane
|
||||
* owns provisioning). Self-hosted keeps the pre-refactor behavior of
|
||||
@@ -242,6 +295,14 @@ export async function ensureBundledPlugins(
|
||||
try {
|
||||
const existing = await deps.registry.getByKey(install.pluginKey);
|
||||
if (existing && (existing.status !== "uninstalled" || !opts.reinstallUninstalled)) {
|
||||
// The bundle ships with the release image, so its manifest is the
|
||||
// source of truth for a present plugin. Reconcile the persisted
|
||||
// manifest when the shipped bundle declares a newer version. Without
|
||||
// this step a manifest capability added to a bundle never reaches an
|
||||
// existing install, because the auto-install below skips a present
|
||||
// plugin. The reconcile updates only the stored manifest row; the
|
||||
// running worker already runs the shipped code.
|
||||
await reconcileBundledPluginManifest(existing, install, deps, bundleManifestExists);
|
||||
deps.logger.info(
|
||||
{ pluginKey: install.pluginKey, status: existing.status },
|
||||
"bundled plugin already present; skipping auto-install",
|
||||
|
||||
@@ -714,6 +714,45 @@ export async function resolveEnvironmentDriverConfigForRuntime(
|
||||
return parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the connection secrets of a recorded sandbox config for a durable
|
||||
* orphan-sandbox teardown. The retry reads the recorded config from the durable
|
||||
* `pending_cleanup` lease row, not from the current environment. So this
|
||||
* resolver must not require the environment binding: a delete removed the
|
||||
* environment, or a provider change replaced the binding. It resolves each
|
||||
* schema-declared secret ref by id at the latest version through
|
||||
* `resolveSecretValueForSandboxCleanup`, which authorizes the read from the
|
||||
* durable orphan record and skips the binding check. The resolved values are
|
||||
* used once for the teardown RPC and never persisted.
|
||||
*/
|
||||
export async function resolveSandboxCleanupConfigSecrets(
|
||||
db: Db,
|
||||
companyId: string,
|
||||
config: SandboxEnvironmentConfig,
|
||||
context?: { issueId?: string | null; heartbeatRunId?: string | null },
|
||||
): Promise<SandboxEnvironmentConfig> {
|
||||
if (config.provider === "fake") return config;
|
||||
const schema = await getSandboxProviderConfigSchema(db, config.provider);
|
||||
const secrets = secretService(db);
|
||||
let nextConfig = { ...config } as Record<string, unknown>;
|
||||
for (const path of collectSecretRefPaths(schema)) {
|
||||
const current = canonicalizeSecretRefValue(readConfigValueAtPath(nextConfig, path), path);
|
||||
if (typeof current !== "string") continue;
|
||||
const trimmed = current.trim();
|
||||
if (!isUuidSecretRef(trimmed)) continue;
|
||||
nextConfig = writeConfigValueAtPath(
|
||||
nextConfig,
|
||||
path,
|
||||
await secrets.resolveSecretValueForSandboxCleanup(companyId, trimmed, "latest", {
|
||||
configPath: path,
|
||||
issueId: context?.issueId ?? null,
|
||||
heartbeatRunId: context?.heartbeatRunId ?? null,
|
||||
}),
|
||||
);
|
||||
}
|
||||
return nextConfig as SandboxEnvironmentConfig;
|
||||
}
|
||||
|
||||
export function readSshEnvironmentPrivateKeySecretId(
|
||||
environment: Pick<Environment, "driver" | "config">,
|
||||
): string | null {
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -30,7 +30,7 @@ import {
|
||||
type EnvironmentLeaseStatus,
|
||||
type UpdateEnvironment,
|
||||
} from "@paperclipai/shared";
|
||||
import { conflict } from "../errors.js";
|
||||
import { conflict, forbidden } from "../errors.js";
|
||||
import { logActivity } from "./activity-log.js";
|
||||
import { isCloudManagedInstance } from "./cloud-instance.js";
|
||||
import {
|
||||
@@ -822,6 +822,26 @@ export function environmentService(db: Db) {
|
||||
return row ? toEnvironment(row) : null;
|
||||
},
|
||||
|
||||
/**
|
||||
* List the companies that own an environment through a built-in managed
|
||||
* resource binding. A managed sandbox row binds to each company that the
|
||||
* instance provisions it for. An operator-created environment has no
|
||||
* binding, so this returns an empty list. The caller treats an empty list
|
||||
* as an instance-global environment with no company owner.
|
||||
*/
|
||||
listBoundCompanyIds: async (environmentId: string): Promise<string[]> => {
|
||||
const rows = await db
|
||||
.select({ companyId: builtInManagedResources.companyId })
|
||||
.from(builtInManagedResources)
|
||||
.where(
|
||||
and(
|
||||
eq(builtInManagedResources.resourceKind, MANAGED_ENVIRONMENT_RESOURCE_KIND),
|
||||
eq(builtInManagedResources.resourceId, environmentId),
|
||||
),
|
||||
);
|
||||
return Array.from(new Set(rows.map((row) => row.companyId)));
|
||||
},
|
||||
|
||||
getLeaseById: async (id: string): Promise<EnvironmentLease | null> => {
|
||||
const row = await db
|
||||
.select()
|
||||
@@ -1089,6 +1109,29 @@ export function environmentService(db: Db) {
|
||||
select 1 from ${instanceSettings}
|
||||
where ${instanceSettings.defaultEnvironmentId} = ${environments.id}
|
||||
)`,
|
||||
// A `pending_cleanup` lease is the durable teardown reference for an
|
||||
// orphan sandbox. The environment foreign key uses
|
||||
// `on delete set null`, so a delete keeps the lease row but drops its
|
||||
// environment reference. This predicate refuses the delete while such
|
||||
// a lease exists, so the operator resolves the cleanup first and the
|
||||
// lease keeps its environment link. It runs in the same statement as
|
||||
// the delete, so it also closes the check-to-delete race.
|
||||
sql`not exists (
|
||||
select 1 from ${environmentLeases}
|
||||
where ${environmentLeases.environmentId} = ${environments.id}
|
||||
and ${environmentLeases.status} = 'pending_cleanup'
|
||||
)`,
|
||||
// A reusable lease keeps a live provider sandbox after a run
|
||||
// releases it. Deleting the environment would set its reference to
|
||||
// null, and both the normal release path and scoped reusable cleanup
|
||||
// require that environment context. Refuse the delete atomically
|
||||
// until the owning issue/workspace destroys the reusable sandbox.
|
||||
sql`not exists (
|
||||
select 1 from ${environmentLeases}
|
||||
where ${environmentLeases.environmentId} = ${environments.id}
|
||||
and ${environmentLeases.leasePolicy} = 'reuse_by_environment'
|
||||
and ${environmentLeases.status} in ('active', 'released', 'retained')
|
||||
)`,
|
||||
),
|
||||
)
|
||||
.returning()
|
||||
@@ -1096,6 +1139,26 @@ export function environmentService(db: Db) {
|
||||
return row ? toEnvironment(row) : null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Return true when the environment has one or more leases in the terminal
|
||||
* `pending_cleanup` state. Each such lease is the only durable provider
|
||||
* reference for an orphan sandbox that a teardown retry must destroy. The
|
||||
* delete guard and the provider-change guard call this to protect that
|
||||
* reference.
|
||||
*/
|
||||
hasUnresolvedPendingCleanupLeases: async (environmentId: string): Promise<boolean> => {
|
||||
const rows = await db
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
.from(environmentLeases)
|
||||
.where(
|
||||
and(
|
||||
eq(environmentLeases.environmentId, environmentId),
|
||||
eq(environmentLeases.status, "pending_cleanup"),
|
||||
),
|
||||
);
|
||||
return countFromRows(rows) > 0;
|
||||
},
|
||||
|
||||
getDeleteBlastRadius: async (id: string): Promise<EnvironmentDeleteBlastRadius | null> => {
|
||||
const environment = await db
|
||||
.select({
|
||||
@@ -1115,6 +1178,8 @@ export function environmentService(db: Db) {
|
||||
projectRows,
|
||||
secretBindingRows,
|
||||
activeLeaseRows,
|
||||
pendingCleanupLeaseRows,
|
||||
reusableSandboxLeaseRows,
|
||||
activeSetupRows,
|
||||
] = await Promise.all([
|
||||
db
|
||||
@@ -1155,6 +1220,25 @@ export function environmentService(db: Db) {
|
||||
eq(environmentLeases.status, "active"),
|
||||
),
|
||||
),
|
||||
db
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
.from(environmentLeases)
|
||||
.where(
|
||||
and(
|
||||
eq(environmentLeases.environmentId, id),
|
||||
eq(environmentLeases.status, "pending_cleanup"),
|
||||
),
|
||||
),
|
||||
db
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
.from(environmentLeases)
|
||||
.where(
|
||||
and(
|
||||
eq(environmentLeases.environmentId, id),
|
||||
eq(environmentLeases.leasePolicy, "reuse_by_environment"),
|
||||
inArray(environmentLeases.status, ["active", "released", "retained"]),
|
||||
),
|
||||
),
|
||||
db
|
||||
.select({ count: sql<number>`count(*)::int` })
|
||||
.from(environmentCustomImageSetupSessions)
|
||||
@@ -1168,9 +1252,18 @@ export function environmentService(db: Db) {
|
||||
|
||||
const isManagedLocal = environment.driver === "local";
|
||||
const isInstanceDefault = countFromRows(instanceDefaultRows) > 0;
|
||||
const pendingCleanupLeaseCount = countFromRows(pendingCleanupLeaseRows);
|
||||
const reusableSandboxLeaseCount = countFromRows(reusableSandboxLeaseRows);
|
||||
const deleteBlockedReasons: EnvironmentDeleteBlockedReason[] = [];
|
||||
if (isManagedLocal) deleteBlockedReasons.push("managed_local");
|
||||
if (isInstanceDefault) deleteBlockedReasons.push("instance_default");
|
||||
// A `pending_cleanup` lease is the durable teardown reference for an orphan
|
||||
// sandbox. The environment foreign key uses `on delete set null`, so a
|
||||
// delete keeps the lease but drops its environment reference. Block the
|
||||
// delete until the sweep resolves the lease, so the operator resolves the
|
||||
// cleanup first and the lease keeps its environment link.
|
||||
if (pendingCleanupLeaseCount > 0) deleteBlockedReasons.push("pending_sandbox_cleanup");
|
||||
if (reusableSandboxLeaseCount > 0) deleteBlockedReasons.push("reusable_sandbox_lease");
|
||||
const activeLeaseCount = countFromRows(activeLeaseRows);
|
||||
const activeCustomImageSetupSessionCount = countFromRows(activeSetupRows);
|
||||
|
||||
@@ -1178,6 +1271,8 @@ export function environmentService(db: Db) {
|
||||
environmentId: id,
|
||||
canDelete: deleteBlockedReasons.length === 0,
|
||||
deleteBlockedReasons,
|
||||
pendingCleanupLeaseCount,
|
||||
reusableSandboxLeaseCount,
|
||||
staticReferences: {
|
||||
isManagedLocal,
|
||||
isInstanceDefault,
|
||||
@@ -1222,32 +1317,80 @@ export function environmentService(db: Db) {
|
||||
providerLeaseId?: string | null;
|
||||
expiresAt?: Date | null;
|
||||
metadata?: Record<string, unknown> | null;
|
||||
/**
|
||||
* Re-check the environment company binding inside the lease insert
|
||||
* transaction. The login routes set this to close the check-to-lease
|
||||
* race: managed reconciliation can bind a sandbox to another company
|
||||
* between the route guard and this acquire. When the environment is
|
||||
* bound to a company other than `companyId`, the insert throws the 403
|
||||
* `environment_company_mismatch` and no lease row is created (fail
|
||||
* closed). An unbound (instance-global) environment stays open to every
|
||||
* member. All other callers keep the plain, non-transactional insert.
|
||||
*/
|
||||
assertCompanyBinding?: boolean;
|
||||
}): Promise<EnvironmentLease> => {
|
||||
const now = new Date();
|
||||
const row = await db
|
||||
.insert(environmentLeases)
|
||||
.values({
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environmentId,
|
||||
executionWorkspaceId: input.executionWorkspaceId ?? null,
|
||||
issueId: input.issueId ?? null,
|
||||
heartbeatRunId: input.heartbeatRunId ?? null,
|
||||
status: "active",
|
||||
leasePolicy: input.leasePolicy ?? "ephemeral",
|
||||
provider: input.provider ?? null,
|
||||
providerLeaseId: input.providerLeaseId ?? null,
|
||||
acquiredAt: now,
|
||||
lastUsedAt: now,
|
||||
expiresAt: input.expiresAt ?? null,
|
||||
releasedAt: null,
|
||||
failureReason: null,
|
||||
cleanupStatus: null,
|
||||
metadata: input.metadata ?? null,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
const values = {
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environmentId,
|
||||
executionWorkspaceId: input.executionWorkspaceId ?? null,
|
||||
issueId: input.issueId ?? null,
|
||||
heartbeatRunId: input.heartbeatRunId ?? null,
|
||||
status: "active" as const,
|
||||
leasePolicy: input.leasePolicy ?? "ephemeral",
|
||||
provider: input.provider ?? null,
|
||||
providerLeaseId: input.providerLeaseId ?? null,
|
||||
acquiredAt: now,
|
||||
lastUsedAt: now,
|
||||
expiresAt: input.expiresAt ?? null,
|
||||
releasedAt: null,
|
||||
failureReason: null,
|
||||
cleanupStatus: null,
|
||||
metadata: input.metadata ?? null,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
};
|
||||
const row = input.assertCompanyBinding
|
||||
? await db.transaction(async (tx) => {
|
||||
// Lock the environment row first. Managed reconciliation locks the
|
||||
// same sandbox environment rows with `for update` before it writes a
|
||||
// company binding, so this lock serializes the two transactions on
|
||||
// this row and closes the time-of-check to time-of-use window.
|
||||
await tx
|
||||
.select({ id: environments.id })
|
||||
.from(environments)
|
||||
.where(eq(environments.id, input.environmentId))
|
||||
.for("update");
|
||||
// Re-read the company binding inside the locked transaction. A
|
||||
// binding a reconciliation committed after the route guard now
|
||||
// appears here. Reject a foreign-company environment before the
|
||||
// insert, so the login holds no lease.
|
||||
const boundRows = await tx
|
||||
.select({ companyId: builtInManagedResources.companyId })
|
||||
.from(builtInManagedResources)
|
||||
.where(
|
||||
and(
|
||||
eq(builtInManagedResources.resourceKind, MANAGED_ENVIRONMENT_RESOURCE_KIND),
|
||||
eq(builtInManagedResources.resourceId, input.environmentId),
|
||||
),
|
||||
);
|
||||
const boundCompanyIds = Array.from(new Set(boundRows.map((boundRow) => boundRow.companyId)));
|
||||
if (boundCompanyIds.length > 0 && !boundCompanyIds.includes(input.companyId)) {
|
||||
throw forbidden("The selected environment belongs to another company.", {
|
||||
code: "environment_company_mismatch",
|
||||
});
|
||||
}
|
||||
return tx
|
||||
.insert(environmentLeases)
|
||||
.values(values)
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
})
|
||||
: await db
|
||||
.insert(environmentLeases)
|
||||
.values(values)
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (!row) {
|
||||
throw new Error("Failed to acquire environment lease");
|
||||
}
|
||||
@@ -1279,6 +1422,82 @@ export function environmentService(db: Db) {
|
||||
return row ? toEnvironmentLease(row) : null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Record a lease-less orphan sandbox directly in the terminal
|
||||
* `pending_cleanup` state with one atomic insert. The sweep reads a row with
|
||||
* status `pending_cleanup` and cleanup status `failed`, so this insert makes
|
||||
* the orphan visible to recovery immediately. It never passes through the
|
||||
* `active` state, so a process or database crash cannot strand the row in an
|
||||
* intermediate state that no sweep finds. The insert skips the company
|
||||
* binding assertion, so it records the orphan even for a foreign-bound
|
||||
* environment.
|
||||
*
|
||||
* The insert runs in a transaction that first locks the environment row with
|
||||
* `for update`. The delete path (`removeIfDeletable`) refuses a delete while a
|
||||
* `pending_cleanup` lease exists, so this lock serializes the two writes:
|
||||
*
|
||||
* - The environment still exists: the insert records the orphan with the
|
||||
* environment reference. A concurrent delete blocks on the lock, then its
|
||||
* `not exists (pending_cleanup)` predicate fails, so the delete refuses.
|
||||
* - The environment is already gone (a delete won the race before this
|
||||
* insert): the insert records the orphan with a null environment
|
||||
* reference. The row carries the immutable provider metadata the sweep
|
||||
* needs, so the teardown still runs. This closes the window the finding
|
||||
* describes, where the foreign-key insert fails after a delete.
|
||||
*/
|
||||
insertPendingCleanupLease: async (input: {
|
||||
companyId: string;
|
||||
environmentId: string;
|
||||
executionWorkspaceId?: string | null;
|
||||
issueId?: string | null;
|
||||
heartbeatRunId?: string | null;
|
||||
provider?: string | null;
|
||||
providerLeaseId?: string | null;
|
||||
metadata?: Record<string, unknown> | null;
|
||||
failureReason: string;
|
||||
}): Promise<EnvironmentLease> => {
|
||||
const now = new Date();
|
||||
const row = await db.transaction(async (tx) => {
|
||||
// Lock the environment row so a concurrent delete cannot commit between
|
||||
// this read and the insert. An absent row means a delete already removed
|
||||
// the environment, so record the orphan with a null reference.
|
||||
const environmentRows = await tx
|
||||
.select({ id: environments.id })
|
||||
.from(environments)
|
||||
.where(eq(environments.id, input.environmentId))
|
||||
.for("update");
|
||||
const environmentIdForRow = environmentRows[0]?.id ?? null;
|
||||
return tx
|
||||
.insert(environmentLeases)
|
||||
.values({
|
||||
companyId: input.companyId,
|
||||
environmentId: environmentIdForRow,
|
||||
executionWorkspaceId: input.executionWorkspaceId ?? null,
|
||||
issueId: input.issueId ?? null,
|
||||
heartbeatRunId: input.heartbeatRunId ?? null,
|
||||
status: "pending_cleanup" as const,
|
||||
leasePolicy: "ephemeral",
|
||||
provider: input.provider ?? null,
|
||||
providerLeaseId: input.providerLeaseId ?? null,
|
||||
acquiredAt: now,
|
||||
lastUsedAt: now,
|
||||
expiresAt: null,
|
||||
releasedAt: now,
|
||||
failureReason: input.failureReason,
|
||||
cleanupStatus: "failed",
|
||||
metadata: input.metadata ?? null,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
});
|
||||
if (!row) {
|
||||
throw new Error("Failed to record pending sandbox cleanup lease");
|
||||
}
|
||||
return toEnvironmentLease(row);
|
||||
},
|
||||
|
||||
updateLeaseMetadata: async (
|
||||
id: string,
|
||||
metadata: Record<string, unknown> | null,
|
||||
|
||||
@@ -13261,6 +13261,28 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
return claimed.length > 0;
|
||||
}
|
||||
|
||||
// Defer a pending_cleanup lease whose provider plugin is not ready this tick.
|
||||
// The sweep reads one page of the oldest rows, ordered by `updatedAt`. A lease
|
||||
// that the sweep only skips keeps its old `updatedAt`, so it stays the oldest
|
||||
// and refills the page on every tick. That starves a newer lease whose
|
||||
// provider is ready. The defer bumps `updatedAt` to now, so the unavailable
|
||||
// lease moves to the back of the queue and a ready lease takes its page slot.
|
||||
// The defer never writes the attempt count, so a long provider outage never
|
||||
// consumes a finite retry. The status guard keeps the write on a lease that is
|
||||
// still pending_cleanup.
|
||||
async function deferPendingCleanupLease(leaseId: string): Promise<void> {
|
||||
const now = new Date();
|
||||
await db
|
||||
.update(environmentLeases)
|
||||
.set({ updatedAt: now })
|
||||
.where(
|
||||
and(
|
||||
eq(environmentLeases.id, leaseId),
|
||||
eq(environmentLeases.status, "pending_cleanup"),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Retry the leases stranded in "pending_cleanup". A failed destroy leaves a
|
||||
// lease in that state forever without this sweep. The reaper tick runs the
|
||||
// sweep. The backoff equals the reaper staleness threshold, so a lease waits
|
||||
@@ -13276,6 +13298,27 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
const now = new Date();
|
||||
const cutoff = new Date(now.getTime() - backoffMs);
|
||||
|
||||
// Flush the in-process orphan-cleanup buffer first. A failed acquire buffers
|
||||
// an orphan there when every synchronous pending-cleanup write failed after a
|
||||
// failed teardown. The flush re-inserts each buffered record, so a durable
|
||||
// `pending_cleanup` row lands once the database recovers. The flush runs
|
||||
// before the read below, so this same tick tears down a freshly-landed row.
|
||||
try {
|
||||
const flushed = await environmentRuntime.flushDeferredOrphanCleanups?.();
|
||||
if (flushed && (flushed.recovered > 0 || flushed.pending > 0)) {
|
||||
logger.info(
|
||||
{ recovered: flushed.recovered, pending: flushed.pending },
|
||||
"flushed the in-process orphan sandbox cleanup buffer to the database",
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
// A flush failure never stops the sweep. The buffer keeps the orphan for a
|
||||
// later tick, and the database rows below still need this sweep. The caught
|
||||
// exception never enters the log, because a write error can carry a
|
||||
// credential in its message, code, cause, or stack.
|
||||
logger.warn("orphan sandbox cleanup buffer flush failed; the sweep continues");
|
||||
}
|
||||
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(environmentLeases)
|
||||
@@ -13310,28 +13353,94 @@ export function heartbeatService(db: Db, options: HeartbeatServiceOptions = {})
|
||||
continue;
|
||||
}
|
||||
|
||||
const environment = await environmentsSvc.getById(row.environmentId);
|
||||
const environment = row.environmentId
|
||||
? await environmentsSvc.getById(row.environmentId)
|
||||
: null;
|
||||
const lease = await environmentsSvc.getLeaseById(row.id);
|
||||
if (!environment || !lease) continue;
|
||||
if (!lease) continue;
|
||||
|
||||
// An orphan ephemeral lease keeps its provider, its provider lease id, and
|
||||
// its sandbox config in the lease row. A failed acquire records it, and its
|
||||
// environment row may be gone or foreign-bound. A reuse_by_environment lease
|
||||
// whose environment a delete removed keeps the same recorded data, because
|
||||
// the schema sets the environment reference to null on delete and preserves
|
||||
// the row. Both leases tear down from the recorded lease data through
|
||||
// `retryPendingSandboxTeardown`, which never reads the environment row. So
|
||||
// the sweep uses that path whenever the lease is an orphan ephemeral lease
|
||||
// or its environment row is gone. A reuse_by_environment lease whose
|
||||
// environment still exists tears down through `destroyRunLease`. That
|
||||
// path uses the provider and configuration recorded on the lease first;
|
||||
// the environment is lifecycle context and only a legacy fallback.
|
||||
const isOrphanEphemeralLease = lease.leasePolicy === "ephemeral";
|
||||
const useRecordedTeardown = isOrphanEphemeralLease || !environment;
|
||||
|
||||
// Do not consume a finite cleanup attempt while the provider plugin is
|
||||
// briefly unavailable. A plugin worker restart, a plugin reload, or a
|
||||
// plugin reinstall makes the provider unavailable for a short window. The
|
||||
// plugin can be missing or not ready in that window. A teardown then throws,
|
||||
// and the atomic claim below would count that throw against the cap, so a
|
||||
// long restart or reload could exhaust the retries and strand a live
|
||||
// sandbox. So probe the provider first, and defer the lease this tick when
|
||||
// the provider is not ready. The sweep preserves the pending_cleanup row,
|
||||
// and a later sweep retries after the provider recovers. The probe reports
|
||||
// ready only for a permanent condition (a missing provider string, a
|
||||
// built-in provider, or no worker manager), so a genuine teardown failure
|
||||
// still runs, throws, and counts toward the cap. A runtime with no probe
|
||||
// method treats the lease as ready, so the sweep keeps its earlier
|
||||
// behavior.
|
||||
const workerReady = environmentRuntime.isPendingCleanupWorkerReady
|
||||
? await environmentRuntime.isPendingCleanupWorkerReady({ environment, lease })
|
||||
: true;
|
||||
if (!workerReady) {
|
||||
// Move the unavailable lease to the back of the sweep queue. Otherwise
|
||||
// the oldest unavailable rows refill the page on every tick and starve a
|
||||
// newer lease that has a ready provider. The defer bumps `updatedAt`
|
||||
// only, so it consumes no finite retry attempt.
|
||||
await deferPendingCleanupLease(row.id);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Atomically claim the attempt before the retry. Only the winning sweep
|
||||
// increments the count and destroys the lease, so an overlapping sweep
|
||||
// never destroys the same lease twice or exceeds the attempt cap. The
|
||||
// increments the count and tears the sandbox down, so an overlapping sweep
|
||||
// never tears the same sandbox down twice or exceeds the attempt cap. The
|
||||
// claim records the attempt before the retry, so a thrown driver error
|
||||
// still counts against the cap.
|
||||
const claimed = await claimPendingCleanupRetryAttempt(row.id, attempts);
|
||||
if (!claimed) continue;
|
||||
|
||||
try {
|
||||
const result = await environmentRuntime.destroyRunLease({
|
||||
environment,
|
||||
lease,
|
||||
failureReason: "pending_cleanup_retry",
|
||||
});
|
||||
if (result && result.status !== "pending_cleanup") {
|
||||
if (useRecordedTeardown) {
|
||||
// Tear the sandbox down from the recorded provider config and the
|
||||
// cleanup-authorized secret versions. The teardown returns no value
|
||||
// and throws on failure, so the sweep releases the lease itself.
|
||||
await environmentRuntime.retryPendingSandboxTeardown({ environment, lease });
|
||||
await environmentsSvc.releaseLease(lease.id, "expired", {
|
||||
cleanupStatus: "success",
|
||||
failureReason: "pending_cleanup_retry",
|
||||
});
|
||||
destroyed += 1;
|
||||
} else if (environment) {
|
||||
const result = await environmentRuntime.destroyRunLease({
|
||||
environment,
|
||||
lease,
|
||||
failureReason: "pending_cleanup_retry",
|
||||
});
|
||||
if (result && result.status !== "pending_cleanup") {
|
||||
destroyed += 1;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// The recorded-data teardown throws on failure, so revert the lease to
|
||||
// pending_cleanup for a later sweep. The claimed attempt still counts
|
||||
// against the cap, so the retries stay bounded. The `destroyRunLease`
|
||||
// path reverts the lease itself, so this revert only runs for the
|
||||
// recorded-data teardown path.
|
||||
if (useRecordedTeardown) {
|
||||
await environmentsSvc.releaseLease(lease.id, "pending_cleanup", {
|
||||
cleanupStatus: "failed",
|
||||
failureReason: "pending_cleanup_retry",
|
||||
});
|
||||
}
|
||||
// Log a constant errorKind only. The exception can carry a credential in
|
||||
// its name, code, message, cause, or stack, so the sweep never reads it.
|
||||
logger.warn(
|
||||
|
||||
@@ -78,6 +78,7 @@ export interface ReadyPluginEnvironmentDriver {
|
||||
templateRefKind?: PluginEnvironmentDriverDeclaration["templateRefKind"];
|
||||
templateConfigBinding?: PluginEnvironmentDriverDeclaration["templateConfigBinding"];
|
||||
supportsTemplateDelete?: PluginEnvironmentDriverDeclaration["supportsTemplateDelete"];
|
||||
supportsSetupTokenLogin?: PluginEnvironmentDriverDeclaration["supportsSetupTokenLogin"];
|
||||
}
|
||||
|
||||
export function pluginDriverProviderKey(config: Pick<PluginEnvironmentConfig, "pluginKey" | "driverKey">): string {
|
||||
@@ -256,6 +257,7 @@ export async function listReadyPluginEnvironmentDrivers(input: {
|
||||
templateRefKind: driver.templateRefKind,
|
||||
templateConfigBinding: driver.templateConfigBinding,
|
||||
supportsTemplateDelete: driver.supportsTemplateDelete,
|
||||
supportsSetupTokenLogin: driver.supportsSetupTokenLogin,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -37,6 +37,8 @@ import {
|
||||
isJsonRpcSuccessResponse,
|
||||
JsonRpcParseError,
|
||||
JsonRpcCallError,
|
||||
SETUP_TOKEN_PTY_OUTPUT_NOTIFICATION,
|
||||
SETUP_TOKEN_PTY_EXIT_NOTIFICATION,
|
||||
} from "@paperclipai/plugin-sdk";
|
||||
import type {
|
||||
JsonRpcId,
|
||||
@@ -53,6 +55,7 @@ import type {
|
||||
InitializeParams,
|
||||
} from "@paperclipai/plugin-sdk";
|
||||
import { getActiveStepContext } from "@paperclipai/adapter-utils/acpx-engine/startup-timing";
|
||||
import { CLAUDE_SETUP_TOKEN_COMMAND } from "@paperclipai/adapter-claude-local/server";
|
||||
import { logger } from "../middleware/logger.js";
|
||||
import { traceparentFromContextToken } from "../instrumentation.js";
|
||||
|
||||
@@ -133,6 +136,26 @@ const MAX_WORKER_MESSAGE_CHARS = 128 * 1024 * 1024;
|
||||
*/
|
||||
const MAX_EXECUTE_LOG_TOTAL_CHARS = 128 * 1024 * 1024;
|
||||
|
||||
/** Maximum characters for one live login pseudo-terminal output notification. */
|
||||
const MAX_SETUP_TOKEN_PTY_CHUNK_CHARS = 1_000_000;
|
||||
/** Maximum cumulative output characters for one login pseudo-terminal route. */
|
||||
const MAX_SETUP_TOKEN_PTY_TOTAL_CHARS = 8 * 1024 * 1024;
|
||||
/** The default open timeout for one login pseudo-terminal route, in milliseconds. */
|
||||
const SETUP_TOKEN_PTY_OPEN_TIMEOUT_MS = 30_000;
|
||||
/** The default close timeout for one login pseudo-terminal route, in milliseconds. */
|
||||
const SETUP_TOKEN_PTY_CLOSE_TIMEOUT_MS = 10_000;
|
||||
/**
|
||||
* The fixed non-secret error a disallowed login command returns. The manager
|
||||
* forwards only the compile-time `CLAUDE_SETUP_TOKEN_COMMAND` to the worker
|
||||
* pseudo-terminal. It rejects any other command before the worker call, so a
|
||||
* future caller cannot spawn an arbitrary process in the sandbox.
|
||||
*/
|
||||
const SETUP_TOKEN_PTY_COMMAND_NOT_ALLOWED = "SETUP_TOKEN_PTY_COMMAND_NOT_ALLOWED";
|
||||
/** The fixed non-secret error a rejected second credential open returns. */
|
||||
const SETUP_TOKEN_PTY_ROUTE_BUSY = "SETUP_TOKEN_PTY_ROUTE_BUSY";
|
||||
/** The fixed non-secret error a failed open returns. */
|
||||
const SETUP_TOKEN_PTY_OPEN_FAILED = "SETUP_TOKEN_PTY_OPEN_FAILED";
|
||||
|
||||
/** Minimum time between two dropped-`execute.log` debug records. The router
|
||||
* rate-limits the record so a flood of dropped chunks writes at most one line
|
||||
* per window with a running count. */
|
||||
@@ -282,6 +305,23 @@ export interface WorkerStartOptions {
|
||||
/** Max total characters one execute call may stream through `execute.log`. */
|
||||
maxTotalCharsPerExecute?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Bounds and timeouts for the login pseudo-terminal route. The
|
||||
* defaults bound one output notification, the cumulative output per route, and
|
||||
* the open and the close timeouts. A test overrides them to exercise the
|
||||
* terminalize paths without huge inputs or long waits.
|
||||
*/
|
||||
setupTokenPtyLimits?: {
|
||||
/** Max characters for one login pseudo-terminal output notification. */
|
||||
maxChunkChars?: number;
|
||||
/** Max cumulative output characters for one login pseudo-terminal route. */
|
||||
maxTotalChars?: number;
|
||||
/** The open timeout for one login pseudo-terminal route, in milliseconds. */
|
||||
openTimeoutMs?: number;
|
||||
/** The close timeout for one login pseudo-terminal route, in milliseconds. */
|
||||
closeTimeoutMs?: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -322,6 +362,37 @@ export type ExecuteLogSink = (
|
||||
chunk: string,
|
||||
) => void | Promise<void>;
|
||||
|
||||
/**
|
||||
* The input the manager needs to open one live login pseudo-terminal route
|
||||
* The manager mints the host route identifier; the caller supplies
|
||||
* only the sandbox scope, the provider lease id, and the fixed command.
|
||||
*/
|
||||
export interface SetupTokenPtyOpenInput {
|
||||
driverKey: string;
|
||||
companyId: string;
|
||||
environmentId: string;
|
||||
providerLeaseId: string;
|
||||
command: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* One live login pseudo-terminal session the manager hands to the login
|
||||
* transport. The shape matches the sandbox provider setup-token
|
||||
* pseudo-terminal session, so the transport consumes it with no adapter.
|
||||
*/
|
||||
export interface SetupTokenPtyHostSession {
|
||||
/** Registers the one output listener. The session streams each raw chunk in order. */
|
||||
onData(listener: (chunk: string) => void): void;
|
||||
/** Writes raw input bytes to the pseudo-terminal. */
|
||||
write(data: string): void;
|
||||
/** Resolves with the child exit code when the command ends or the route terminalizes. */
|
||||
wait(): Promise<{ exitCode: number | null }>;
|
||||
/** Stops the child process. Safe to call more than one time. */
|
||||
kill(): void;
|
||||
/** Closes the route and releases the terminal. Safe to call more than one time. */
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Host-owned route for one active execute call. The host mints the invocation
|
||||
* id and stores the exact company id and log sink here. A worker never selects
|
||||
@@ -403,6 +474,16 @@ export interface PluginWorkerHandle {
|
||||
*/
|
||||
notify(method: string, params: unknown): void;
|
||||
|
||||
/**
|
||||
* Open one live login pseudo-terminal route on this worker. The
|
||||
* manager mints the host route identifier, reserves the route, drives the open,
|
||||
* binds the worker session identifier one time, and returns a session the login
|
||||
* transport drives. It permits one active credential pseudo-terminal per worker.
|
||||
*/
|
||||
openSetupTokenPtySession(
|
||||
input: SetupTokenPtyOpenInput,
|
||||
): Promise<SetupTokenPtyHostSession>;
|
||||
|
||||
/**
|
||||
* Authorize the set of companies this worker may act on from proactive
|
||||
* (non-invocation) context. Replaces any previously-authorized set. See the
|
||||
@@ -506,6 +587,17 @@ export interface PluginWorkerManager {
|
||||
timeoutMs?: number,
|
||||
executeLogSink?: ExecuteLogSink,
|
||||
): Promise<HostToWorkerMethods[M][1]>;
|
||||
|
||||
/**
|
||||
* Open one live login pseudo-terminal route on a specific plugin worker
|
||||
* See {@link PluginWorkerHandle.openSetupTokenPtySession}.
|
||||
*
|
||||
* @throws if the worker is not registered.
|
||||
*/
|
||||
openSetupTokenPtySession(
|
||||
pluginId: string,
|
||||
input: SetupTokenPtyOpenInput,
|
||||
): Promise<SetupTokenPtyHostSession>;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -564,6 +656,17 @@ export function createPluginWorkerHandle(
|
||||
const maxExecuteLogTotalChars =
|
||||
options.executeLogLimits?.maxTotalCharsPerExecute ?? MAX_EXECUTE_LOG_TOTAL_CHARS;
|
||||
|
||||
// Bounds and timeouts for the login pseudo-terminal route. A caller
|
||||
// (a test) can lower them to exercise the terminalize paths.
|
||||
const maxSetupTokenPtyChunkChars =
|
||||
options.setupTokenPtyLimits?.maxChunkChars ?? MAX_SETUP_TOKEN_PTY_CHUNK_CHARS;
|
||||
const maxSetupTokenPtyTotalChars =
|
||||
options.setupTokenPtyLimits?.maxTotalChars ?? MAX_SETUP_TOKEN_PTY_TOTAL_CHARS;
|
||||
const setupTokenPtyOpenTimeoutMs =
|
||||
options.setupTokenPtyLimits?.openTimeoutMs ?? SETUP_TOKEN_PTY_OPEN_TIMEOUT_MS;
|
||||
const setupTokenPtyCloseTimeoutMs =
|
||||
options.setupTokenPtyLimits?.closeTimeoutMs ?? SETUP_TOKEN_PTY_CLOSE_TIMEOUT_MS;
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Proactive company scopes (LOOA-629)
|
||||
// ------------------------------------------------------------------
|
||||
@@ -920,6 +1023,251 @@ export function createPluginWorkerHandle(
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Host-owned setup-token login pseudo-terminal route gate
|
||||
// -----------------------------------------------------------------------
|
||||
// The manager owns one live login pseudo-terminal route per worker. It mints a
|
||||
// host-owned opaque route identifier, carries it in the open call, and keys the
|
||||
// close on it, so it closes a worker-created terminal even when the open reply
|
||||
// was lost and no worker session identifier arrived. It binds the worker
|
||||
// session identifier one time while the route is `opening`, for output only. It
|
||||
// never trusts a worker-supplied identifier as proof of origin: it delivers
|
||||
// output only while the route is `open` and the notification carries the exact
|
||||
// bound identifier and valid bounded bytes, and it never logs the raw bytes. It
|
||||
// terminalizes the route exactly once on every open failure path, closes the
|
||||
// terminal by the host route identifier, and admits a new open only after it
|
||||
// verifies a close acknowledgement bound to that identifier; it retires the
|
||||
// worker on an unconfirmed close.
|
||||
|
||||
type SetupTokenPtyRouteState = "reserved" | "opening" | "open" | "closed";
|
||||
interface SetupTokenPtyRoute {
|
||||
hostRouteId: string;
|
||||
state: SetupTokenPtyRouteState;
|
||||
workerSessionId: string | null;
|
||||
listener: ((chunk: string) => void) | null;
|
||||
buffered: string[];
|
||||
deliveredChars: number;
|
||||
terminalized: boolean;
|
||||
settleWait: (value: { exitCode: number | null }) => void;
|
||||
}
|
||||
// At most one active credential pseudo-terminal per worker. A non-null route
|
||||
// blocks a second open until the manager confirms the first route's close.
|
||||
let setupTokenPtyRoute: SetupTokenPtyRoute | null = null;
|
||||
|
||||
function settleSetupTokenPtyWait(
|
||||
route: SetupTokenPtyRoute,
|
||||
value: { exitCode: number | null },
|
||||
): void {
|
||||
const settle = route.settleWait;
|
||||
route.settleWait = () => {};
|
||||
settle(value);
|
||||
}
|
||||
|
||||
// Close the worker terminal by the host route identifier and verify the bound
|
||||
// acknowledgement. Return true only when the worker returns an acknowledgement
|
||||
// that carries the exact host route identifier. An absent, malformed,
|
||||
// mismatched, or timed-out acknowledgement returns false, so the caller fails
|
||||
// closed.
|
||||
async function closeSetupTokenPtyTerminal(hostRouteId: string): Promise<boolean> {
|
||||
try {
|
||||
const ack = await callInternal(
|
||||
"setupTokenPtyClose",
|
||||
{ hostRouteId },
|
||||
setupTokenPtyCloseTimeoutMs,
|
||||
);
|
||||
return isRecord(ack) && readNonEmptyString(ack.hostRouteId) === hostRouteId;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Terminalize the route exactly once. Resolve the login wait, close the worker
|
||||
// terminal by the host route identifier, and free the per-worker slot only
|
||||
// after the close resolves. Retire the worker when the close is unconfirmed.
|
||||
async function terminalizeSetupTokenPtyRoute(route: SetupTokenPtyRoute): Promise<void> {
|
||||
if (route.terminalized) return;
|
||||
route.terminalized = true;
|
||||
route.state = "closed";
|
||||
route.listener = null;
|
||||
route.buffered = [];
|
||||
// A terminalized route reports a null exit code, which the runner treats as a
|
||||
// failure.
|
||||
settleSetupTokenPtyWait(route, { exitCode: null });
|
||||
const confirmed = await closeSetupTokenPtyTerminal(route.hostRouteId);
|
||||
if (setupTokenPtyRoute === route) setupTokenPtyRoute = null;
|
||||
if (!confirmed) {
|
||||
// The worker did not acknowledge the close, so the host cannot prove the
|
||||
// terminal is gone. Fail closed: retire the worker before any reuse.
|
||||
log.error(
|
||||
{ pluginId },
|
||||
"setup-token login pseudo-terminal close not acknowledged; retiring worker",
|
||||
);
|
||||
void killProcess();
|
||||
}
|
||||
}
|
||||
|
||||
// Route one login pseudo-terminal output notification to the per-session
|
||||
// listener. Deliver only while the route is `open` and the notification carries
|
||||
// the exact bound worker session identifier and valid bounded bytes. Drop an
|
||||
// unknown, late, malformed, or mismatched notification. Never log the raw bytes.
|
||||
function routeSetupTokenPtyOutput(notification: JsonRpcNotification): void {
|
||||
const route = setupTokenPtyRoute;
|
||||
if (!route || route.state !== "open") return;
|
||||
const params = isRecord(notification.params) ? notification.params : {};
|
||||
const workerSessionId = readNonEmptyString(params.workerSessionId);
|
||||
if (!workerSessionId || workerSessionId !== route.workerSessionId) return;
|
||||
const chunk = params.chunk;
|
||||
if (
|
||||
typeof chunk !== "string" ||
|
||||
chunk.length === 0 ||
|
||||
chunk.length > maxSetupTokenPtyChunkChars
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (route.deliveredChars + chunk.length > maxSetupTokenPtyTotalChars) {
|
||||
// The cumulative output passed the per-route bound. Terminalize the route.
|
||||
void terminalizeSetupTokenPtyRoute(route);
|
||||
return;
|
||||
}
|
||||
route.deliveredChars += chunk.length;
|
||||
if (route.listener) route.listener(chunk);
|
||||
else route.buffered.push(chunk);
|
||||
}
|
||||
|
||||
// Route one login pseudo-terminal exit notification to the login wait. Resolve
|
||||
// only while the route is `open` and the notification carries the exact bound
|
||||
// worker session identifier.
|
||||
function routeSetupTokenPtyExit(notification: JsonRpcNotification): void {
|
||||
const route = setupTokenPtyRoute;
|
||||
if (!route || route.state !== "open") return;
|
||||
const params = isRecord(notification.params) ? notification.params : {};
|
||||
const workerSessionId = readNonEmptyString(params.workerSessionId);
|
||||
if (!workerSessionId || workerSessionId !== route.workerSessionId) return;
|
||||
const exitCode = typeof params.exitCode === "number" ? params.exitCode : null;
|
||||
settleSetupTokenPtyWait(route, { exitCode });
|
||||
}
|
||||
|
||||
// Close the one route on a worker exit. The worker is gone, so the manager
|
||||
// resolves the login wait with the fixed non-secret exit and clears the route
|
||||
// one time. The pending pseudo-terminal calls reject through `rejectAllPending`.
|
||||
function closeSetupTokenPtyRouteOnWorkerExit(): void {
|
||||
const route = setupTokenPtyRoute;
|
||||
if (!route) return;
|
||||
setupTokenPtyRoute = null;
|
||||
route.terminalized = true;
|
||||
route.state = "closed";
|
||||
route.listener = null;
|
||||
route.buffered = [];
|
||||
settleSetupTokenPtyWait(route, { exitCode: null });
|
||||
}
|
||||
|
||||
// Open one live login pseudo-terminal route. Reserve the route
|
||||
// before the open call, bind the worker session identifier one time on the
|
||||
// first successful open reply, and return a session the login transport drives.
|
||||
// Terminalize the route on every open failure path.
|
||||
async function openSetupTokenPtySession(
|
||||
input: SetupTokenPtyOpenInput,
|
||||
): Promise<SetupTokenPtyHostSession> {
|
||||
if (input.command !== CLAUDE_SETUP_TOKEN_COMMAND) {
|
||||
// Allowlist the login command. Only the fixed `CLAUDE_SETUP_TOKEN_COMMAND`
|
||||
// may run in the sandbox pseudo-terminal. Reject any other command with one
|
||||
// fixed non-secret error before the worker call, so a caller cannot spawn
|
||||
// an arbitrary process in the sandbox pseudo-terminal.
|
||||
throw new Error(SETUP_TOKEN_PTY_COMMAND_NOT_ALLOWED);
|
||||
}
|
||||
if (setupTokenPtyRoute) {
|
||||
// A route for this worker is not yet closed and confirmed. Reject the
|
||||
// second open with one fixed non-secret error before it reaches the worker.
|
||||
throw new Error(SETUP_TOKEN_PTY_ROUTE_BUSY);
|
||||
}
|
||||
const hostRouteId = randomUUID();
|
||||
let settleWait: (value: { exitCode: number | null }) => void = () => {};
|
||||
const waitPromise = new Promise<{ exitCode: number | null }>((resolve) => {
|
||||
settleWait = resolve;
|
||||
});
|
||||
const route: SetupTokenPtyRoute = {
|
||||
hostRouteId,
|
||||
state: "reserved",
|
||||
workerSessionId: null,
|
||||
listener: null,
|
||||
buffered: [],
|
||||
deliveredChars: 0,
|
||||
terminalized: false,
|
||||
settleWait,
|
||||
};
|
||||
setupTokenPtyRoute = route;
|
||||
|
||||
route.state = "opening";
|
||||
let openResult: HostToWorkerMethods["setupTokenPtyOpen"][1];
|
||||
try {
|
||||
openResult = await callInternal(
|
||||
"setupTokenPtyOpen",
|
||||
{
|
||||
hostRouteId,
|
||||
driverKey: input.driverKey,
|
||||
companyId: input.companyId,
|
||||
environmentId: input.environmentId,
|
||||
providerLeaseId: input.providerLeaseId,
|
||||
command: input.command,
|
||||
},
|
||||
setupTokenPtyOpenTimeoutMs,
|
||||
);
|
||||
} catch (err) {
|
||||
// A send failure, an RPC rejection, or an open timeout. Terminalize the
|
||||
// route exactly once and fail closed.
|
||||
await terminalizeSetupTokenPtyRoute(route);
|
||||
throw err instanceof Error ? err : new Error(SETUP_TOKEN_PTY_OPEN_FAILED);
|
||||
}
|
||||
|
||||
const workerSessionId = readNonEmptyString(
|
||||
isRecord(openResult) ? openResult.workerSessionId : null,
|
||||
);
|
||||
if (!workerSessionId || route.state !== "opening" || route.terminalized) {
|
||||
// A malformed reply, or a route that already left `opening`. A late or a
|
||||
// duplicate reply never binds, revives, or reopens a route.
|
||||
await terminalizeSetupTokenPtyRoute(route);
|
||||
throw new Error(SETUP_TOKEN_PTY_OPEN_FAILED);
|
||||
}
|
||||
// Bind the worker session identifier one time and move the route to `open`.
|
||||
route.workerSessionId = workerSessionId;
|
||||
route.state = "open";
|
||||
|
||||
return {
|
||||
onData(listener: (chunk: string) => void): void {
|
||||
route.listener = listener;
|
||||
if (route.buffered.length > 0) {
|
||||
const pending = route.buffered;
|
||||
route.buffered = [];
|
||||
for (const chunk of pending) listener(chunk);
|
||||
}
|
||||
},
|
||||
write(data: string): void {
|
||||
const sid = route.workerSessionId;
|
||||
if (route.state !== "open" || !sid) return;
|
||||
void callInternal(
|
||||
"setupTokenPtyInput",
|
||||
{ workerSessionId: sid, data },
|
||||
setupTokenPtyOpenTimeoutMs,
|
||||
).catch(() => {});
|
||||
},
|
||||
wait(): Promise<{ exitCode: number | null }> {
|
||||
return waitPromise;
|
||||
},
|
||||
kill(): void {
|
||||
const sid = route.workerSessionId;
|
||||
if (!sid) return;
|
||||
void callInternal(
|
||||
"setupTokenPtyStop",
|
||||
{ workerSessionId: sid },
|
||||
setupTokenPtyOpenTimeoutMs,
|
||||
).catch(() => {});
|
||||
},
|
||||
async close(): Promise<void> {
|
||||
await terminalizeSetupTokenPtyRoute(route);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the single company a worker→host call references, mirroring the SDK
|
||||
* governed-access gate's own derivation (host-client-factory.ts
|
||||
@@ -1080,6 +1428,18 @@ export function createPluginWorkerHandle(
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup-token login pseudo-terminal notifications: deliver output
|
||||
// and the exit to the one host-owned login route, bound by the worker session
|
||||
// identifier while the route is open.
|
||||
if (notification.method === SETUP_TOKEN_PTY_OUTPUT_NOTIFICATION) {
|
||||
routeSetupTokenPtyOutput(notification);
|
||||
return;
|
||||
}
|
||||
if (notification.method === SETUP_TOKEN_PTY_EXIT_NOTIFICATION) {
|
||||
routeSetupTokenPtyExit(notification);
|
||||
return;
|
||||
}
|
||||
|
||||
// Stream notifications: forward to the stream bus via callback
|
||||
if (
|
||||
notification.method === "streams.open" ||
|
||||
@@ -1225,6 +1585,11 @@ export function createPluginWorkerHandle(
|
||||
)),
|
||||
);
|
||||
|
||||
// Close the one login pseudo-terminal route with a fixed non-secret exit and
|
||||
// clear the route one time. The pending pseudo-terminal calls
|
||||
// already rejected through `rejectAllPending`.
|
||||
closeSetupTokenPtyRouteOnWorkerExit();
|
||||
|
||||
// Emit synthetic close for any orphaned stream channels so SSE clients
|
||||
// are notified instead of hanging indefinitely.
|
||||
if (openStreamChannels.size > 0 && options.onStreamNotification) {
|
||||
@@ -1687,6 +2052,17 @@ export function createPluginWorkerHandle(
|
||||
return callInternal(method, params, timeoutMs, executeLogSink);
|
||||
},
|
||||
|
||||
openSetupTokenPtySession(input: SetupTokenPtyOpenInput) {
|
||||
if (status !== "running" && status !== "starting") {
|
||||
return Promise.reject(
|
||||
new Error(
|
||||
`Cannot open a login pseudo-terminal — worker for "${pluginId}" is ${status}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
return openSetupTokenPtySession(input);
|
||||
},
|
||||
|
||||
notify(method: string, params: unknown) {
|
||||
if (status !== "running") return;
|
||||
const invocationScope = deriveInvocationScope(method, params);
|
||||
@@ -1925,5 +2301,15 @@ export function createPluginWorkerManager(
|
||||
}
|
||||
return handle.call(method, params, timeoutMs, executeLogSink);
|
||||
},
|
||||
|
||||
openSetupTokenPtySession(pluginId: string, input: SetupTokenPtyOpenInput) {
|
||||
const handle = workers.get(pluginId);
|
||||
if (!handle) {
|
||||
return Promise.reject(
|
||||
new Error(`No worker registered for plugin "${pluginId}"`),
|
||||
);
|
||||
}
|
||||
return handle.openSetupTokenPtySession(input);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
import { promises as fs } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { resolvePaperclipInstanceRoot } from "../home-paths.js";
|
||||
|
||||
/**
|
||||
* One orphan pending-cleanup record. The acquire builds it when the conditional
|
||||
* lease insert rejects a live sandbox. The record carries the same fields a
|
||||
* `pending_cleanup` lease row needs, so a later flush re-inserts it without
|
||||
* loss. The `metadata` field holds the sanitized lease metadata: it carries
|
||||
* secret references, not secret values, so the record is safe to persist next to
|
||||
* the other durable local state. A restart-recovered record still authorizes the
|
||||
* teardown, because it keeps the immutable provider, provider lease id, and
|
||||
* recorded config the teardown needs.
|
||||
*/
|
||||
export interface DeferredOrphanCleanupRecord {
|
||||
companyId: string;
|
||||
environmentId: string;
|
||||
executionWorkspaceId: string | null;
|
||||
issueId: string | null;
|
||||
heartbeatRunId: string | null;
|
||||
provider: string;
|
||||
providerLeaseId: string | null;
|
||||
metadata: Record<string, unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* A durable, restart-safe store for orphan pending-cleanup records. The acquire
|
||||
* appends a record here when the conditional lease insert rejects a live
|
||||
* sandbox, the compensating teardown fails, and every synchronous
|
||||
* `pending_cleanup` database write also fails. The database is the only
|
||||
* automated way to find a leaked sandbox, but the database is down at that
|
||||
* instant, so the record has nowhere durable to land. The in-process buffer
|
||||
* alone loses the record on a restart. This spool keeps the record on the local
|
||||
* disk, next to the other durable server state, so a restart still finds it. A
|
||||
* later cleanup sweep loads the spool, re-inserts each record as a durable
|
||||
* `pending_cleanup` row, and removes the spooled copy.
|
||||
*/
|
||||
export interface SandboxOrphanCleanupSpool {
|
||||
/**
|
||||
* Persist one orphan record durably. Report `true` when the record reached the
|
||||
* disk, or `false` on any failure, so the caller keeps the in-process buffer
|
||||
* and the error log as the fallback handles. This method never throws.
|
||||
*/
|
||||
append(record: DeferredOrphanCleanupRecord): Promise<boolean>;
|
||||
/**
|
||||
* Load every persisted orphan record. The cleanup sweep calls this once after
|
||||
* a restart, so the records a previous process could not land re-enter the
|
||||
* flush path. This method never throws; it returns an empty list on any read
|
||||
* failure and skips a malformed file.
|
||||
*/
|
||||
load(): Promise<DeferredOrphanCleanupRecord[]>;
|
||||
/**
|
||||
* Remove one persisted orphan record after its durable `pending_cleanup` row
|
||||
* lands. The removal runs after the database insert succeeds, so a crash
|
||||
* between the insert and this removal leaves the spooled copy for a later
|
||||
* flush. That later flush re-inserts a duplicate row, which the idempotent
|
||||
* teardown handles. This order never loses the orphan. This method never
|
||||
* throws.
|
||||
*/
|
||||
remove(record: DeferredOrphanCleanupRecord): Promise<void>;
|
||||
}
|
||||
|
||||
// The spool writes one file per orphan record, so a removal never rewrites a
|
||||
// shared file and never races a concurrent append. The file name is a stable
|
||||
// hash of the record identity, so a repeated append for the same orphan
|
||||
// overwrites the same file and never persists a duplicate.
|
||||
const RECORD_FILE_SUFFIX = ".json";
|
||||
|
||||
// A unique temp-file counter, so two concurrent appends for two different
|
||||
// records never share a temp path. The atomic rename then publishes each record
|
||||
// without a torn write.
|
||||
let tempFileCounter = 0;
|
||||
|
||||
function resolveDefaultSpoolDir(): string {
|
||||
return (
|
||||
process.env.SANDBOX_ORPHAN_CLEANUP_SPOOL_DIR ??
|
||||
path.resolve(resolvePaperclipInstanceRoot(), "data", "sandbox-orphan-cleanup")
|
||||
);
|
||||
}
|
||||
|
||||
// Build the stable file name for one record. Key on the provider lease id, the
|
||||
// value the teardown needs, so a repeated append for the same orphan reuses the
|
||||
// same file. A null provider lease id carries no identity, so fall back to a
|
||||
// hash of the whole record. The hash keeps the name on the safe file-name
|
||||
// charset and hides the raw identifiers from a directory listing.
|
||||
function recordFileName(record: DeferredOrphanCleanupRecord): string {
|
||||
const identity =
|
||||
record.providerLeaseId !== null
|
||||
? `lease:${record.providerLeaseId}`
|
||||
: `record:${JSON.stringify(record)}`;
|
||||
const hash = createHash("sha256").update(identity).digest("hex");
|
||||
return `${hash}${RECORD_FILE_SUFFIX}`;
|
||||
}
|
||||
|
||||
function isDeferredOrphanCleanupRecord(value: unknown): value is DeferredOrphanCleanupRecord {
|
||||
if (typeof value !== "object" || value === null) return false;
|
||||
const candidate = value as Record<string, unknown>;
|
||||
const stringOrNull = (field: unknown) => field === null || typeof field === "string";
|
||||
return (
|
||||
typeof candidate.companyId === "string" &&
|
||||
typeof candidate.environmentId === "string" &&
|
||||
stringOrNull(candidate.executionWorkspaceId) &&
|
||||
stringOrNull(candidate.issueId) &&
|
||||
stringOrNull(candidate.heartbeatRunId) &&
|
||||
typeof candidate.provider === "string" &&
|
||||
stringOrNull(candidate.providerLeaseId) &&
|
||||
typeof candidate.metadata === "object" &&
|
||||
candidate.metadata !== null
|
||||
);
|
||||
}
|
||||
|
||||
// Write one file durably. Write the bytes to a unique temp file, flush the file
|
||||
// to the disk, rename it onto the final path, then flush the directory. The
|
||||
// rename is atomic on POSIX, so a reader never sees a torn record, and a crash
|
||||
// after the directory flush still leaves the whole record on the disk.
|
||||
async function writeFileDurable(absPath: string, data: string): Promise<void> {
|
||||
tempFileCounter += 1;
|
||||
const tempPath = `${absPath}.${process.pid}.${tempFileCounter}.tmp`;
|
||||
const handle = await fs.open(tempPath, "w");
|
||||
try {
|
||||
await handle.writeFile(data, "utf8");
|
||||
await handle.sync();
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
try {
|
||||
await fs.rename(tempPath, absPath);
|
||||
} catch (error) {
|
||||
// The rename failed, so drop the temp file. Do not leave it behind, or a
|
||||
// later load reads a half-published record.
|
||||
await fs.rm(tempPath, { force: true }).catch(() => undefined);
|
||||
throw error;
|
||||
}
|
||||
const dirHandle = await fs.open(path.dirname(absPath), "r");
|
||||
try {
|
||||
await dirHandle.sync();
|
||||
} finally {
|
||||
await dirHandle.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a local-disk orphan-cleanup spool. The spool writes one file per record
|
||||
* under the spool directory. The default directory sits next to the other
|
||||
* durable server state under the instance root. A caller can override the
|
||||
* directory for a test.
|
||||
*/
|
||||
export function createSandboxOrphanCleanupSpool(spoolDir?: string): SandboxOrphanCleanupSpool {
|
||||
const dir = spoolDir ?? resolveDefaultSpoolDir();
|
||||
|
||||
return {
|
||||
async append(record) {
|
||||
const absPath = path.join(dir, recordFileName(record));
|
||||
try {
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
// Serialize the record only. A caught write error never enters a log,
|
||||
// because it can carry a credential in its message, code, cause, or
|
||||
// stack. So this method reports a boolean and never logs.
|
||||
await writeFileDurable(absPath, JSON.stringify(record));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
async load() {
|
||||
let names: string[];
|
||||
try {
|
||||
names = await fs.readdir(dir);
|
||||
} catch {
|
||||
// The directory does not exist yet, or the read failed. Either way, no
|
||||
// record waits on the disk.
|
||||
return [];
|
||||
}
|
||||
const records: DeferredOrphanCleanupRecord[] = [];
|
||||
for (const name of names) {
|
||||
if (!name.endsWith(RECORD_FILE_SUFFIX)) continue;
|
||||
try {
|
||||
const raw = await fs.readFile(path.join(dir, name), "utf8");
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
if (isDeferredOrphanCleanupRecord(parsed)) {
|
||||
records.push(parsed);
|
||||
}
|
||||
} catch {
|
||||
// Skip a malformed or unreadable file. A partial file cannot authorize
|
||||
// a teardown, so the load ignores it and keeps the other records.
|
||||
}
|
||||
}
|
||||
return records;
|
||||
},
|
||||
|
||||
async remove(record) {
|
||||
const absPath = path.join(dir, recordFileName(record));
|
||||
await fs.rm(absPath, { force: true }).catch(() => undefined);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -20,6 +20,14 @@ export interface AcquireSandboxLeaseInput {
|
||||
issueId: string | null;
|
||||
agentId?: string | null;
|
||||
executionWorkspaceId?: string | null;
|
||||
/**
|
||||
* The absolute latest time the acquired lease may stay active, as an ISO 8601
|
||||
* timestamp. A caller with an independent deadline sets it. The provider must
|
||||
* configure a provider-side expiry at or before this time, and return the real
|
||||
* provider expiry in `SandboxLeaseHandle.expiresAt`. When omitted, the provider
|
||||
* keeps its default lifetime.
|
||||
*/
|
||||
requestedExpiresAt?: string | null;
|
||||
}
|
||||
|
||||
export interface ResumeSandboxLeaseInput {
|
||||
@@ -63,6 +71,12 @@ export interface SandboxExecuteInput {
|
||||
export interface SandboxLeaseHandle {
|
||||
providerLeaseId: string;
|
||||
metadata: Record<string, unknown>;
|
||||
/**
|
||||
* The real provider-side expiry the provider granted, as an ISO 8601 timestamp.
|
||||
* A provider that honors a requested deadline returns the actual expiry it
|
||||
* configured. Absent when the provider granted no bounded lifetime.
|
||||
*/
|
||||
expiresAt?: string | null;
|
||||
}
|
||||
|
||||
export interface PreparedSandboxWorkspace {
|
||||
@@ -336,6 +350,7 @@ export async function acquireSandboxProviderLease(input: {
|
||||
agentId?: string | null;
|
||||
executionWorkspaceId?: string | null;
|
||||
reusableProviderLeaseId?: string | null;
|
||||
requestedExpiresAt?: string | null;
|
||||
}): Promise<SandboxLeaseHandle> {
|
||||
const provider = requireSandboxProvider(input.config.provider);
|
||||
if (provider.supportsReusableLeases && input.config.reuseLease && input.reusableProviderLeaseId) {
|
||||
@@ -355,6 +370,7 @@ export async function acquireSandboxProviderLease(input: {
|
||||
issueId: input.issueId,
|
||||
agentId: input.agentId,
|
||||
executionWorkspaceId: input.executionWorkspaceId,
|
||||
requestedExpiresAt: input.requestedExpiresAt,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,10 @@ import { logActivity } from "./activity-log.js";
|
||||
|
||||
const ENV_KEY_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
||||
const AGENT_ACCESS_CONFIG_PATH_PREFIX = "access.";
|
||||
// System consumer id for a durable orphan-sandbox teardown. The cleanup sweep
|
||||
// resolves the recorded connection secret under this id so the audit trail
|
||||
// marks the read as an orphan-sandbox teardown, not a normal environment read.
|
||||
export const SANDBOX_CLEANUP_CONSUMER_ID = "environment-sandbox-cleanup";
|
||||
const SENSITIVE_ENV_KEY_RE =
|
||||
/(api[-_]?key|access[-_]?token|auth(?:_?token)?|authorization|bearer|secret|passwd|password|credential|jwt|private[-_]?key|cookie|connectionstring)/i;
|
||||
const REDACTED_SENTINEL = "***REDACTED***";
|
||||
@@ -80,6 +84,180 @@ const FALLBACK_ADAPTER_SCHEMA_SECRET_FIELDS: Readonly<Record<string, readonly st
|
||||
};
|
||||
const USER_SECRET_DEFINITION_KEY_UNIQUE_CONSTRAINT = "user_secret_definitions_company_key_uq";
|
||||
const USER_SECRET_VALUE_UNIQUE_CONSTRAINT = "company_secrets_user_definition_owner_uq";
|
||||
// The unique index on (secretId, version). A concurrent rotation that inserts the
|
||||
// same next version first makes the loser's insert fail this constraint.
|
||||
const COMPANY_SECRET_VERSION_UNIQUE_CONSTRAINT = "company_secret_versions_secret_version_uq";
|
||||
// The one stale-rotation conflict text. The rotate function returns it for every
|
||||
// stale race, so the caller sees one fixed 409 and no owner-value state.
|
||||
const SECRET_VERSION_STALE_CONFLICT = "The secret version is stale. Reload and confirm the rotation again.";
|
||||
|
||||
// The fixed Claude Code OAuth user-secret definition. The Claude login flow owns
|
||||
// only this compile-time key and these fixed properties. A caller never selects
|
||||
// the key, the name, the provider, the mode, or the status.
|
||||
const CLAUDE_CODE_OAUTH_TOKEN_KEY = "CLAUDE_CODE_OAUTH_TOKEN";
|
||||
const CLAUDE_CODE_OAUTH_DEFINITION = {
|
||||
key: CLAUDE_CODE_OAUTH_TOKEN_KEY,
|
||||
name: "Claude Code OAuth token",
|
||||
provider: "local_encrypted",
|
||||
managedMode: "paperclip_managed",
|
||||
status: "active",
|
||||
} as const;
|
||||
// The fixed, non-secret conflict text. The helper returns it when a stored
|
||||
// definition for the fixed key does not match the fixed shape. The text echoes
|
||||
// no caller input.
|
||||
const CLAUDE_OAUTH_DEFINITION_CONFLICT =
|
||||
"A conflicting Claude Code OAuth token definition already exists.";
|
||||
// The fixed, non-secret text for a stale confirmed rotation. The text is the
|
||||
// same for every stale reason, so it discloses no owner-value state.
|
||||
const CLAUDE_OAUTH_STALE_CONFIRMATION =
|
||||
"The Claude login confirmation is stale. Reload the page and confirm again.";
|
||||
// The fixed, non-secret text for a first write that finds an existing value. The
|
||||
// caller must confirm a replacement to rotate it.
|
||||
const CLAUDE_OAUTH_VALUE_EXISTS =
|
||||
"A Claude login value already exists. Confirm a replacement to rotate it.";
|
||||
// The metadata field that records the setup-token session id on the owner value.
|
||||
// It is the idempotency key for one completion. It is not a secret.
|
||||
const CLAUDE_OAUTH_SESSION_METADATA_FIELD = "claudeSetupTokenSessionId";
|
||||
|
||||
/** The stored result of one owner-bound Claude OAuth completion. It holds no secret. */
|
||||
export interface ClaudeOAuthUserSecretResult {
|
||||
secretId: string;
|
||||
latestVersion: number;
|
||||
definitionId: string;
|
||||
}
|
||||
|
||||
// --- The server-enforced Claude OAuth binding invariant ------------
|
||||
|
||||
/** The adapter that owns the fixed Claude Code OAuth token binding. */
|
||||
export const CLAUDE_LOCAL_ADAPTER_TYPE = "claude_local";
|
||||
|
||||
// The one fixed, non-secret error for every rejected stored-session claim. The
|
||||
// text is byte-identical for a missing, foreign, cross-company, cross-owner,
|
||||
// cross-adapter, cross-environment, expired, non-stored, or already-consumed
|
||||
// claim, so a caller cannot tell the reasons apart.
|
||||
export const CLAUDE_OAUTH_CLAIM_REJECTED =
|
||||
"The Claude login binding requires a valid stored-session claim.";
|
||||
|
||||
// The generic credential-conflict text. It names no token value and no owner
|
||||
// configuration. It tells the caller only that a higher-priority credential is
|
||||
// configured together with the Claude login token.
|
||||
export const CLAUDE_OAUTH_CREDENTIAL_CONFLICT =
|
||||
"A higher-priority Claude credential is configured. Remove it to use the Claude login token.";
|
||||
|
||||
const ANTHROPIC_API_KEY_ENV = "ANTHROPIC_API_KEY";
|
||||
|
||||
/** Reads the `env` record of an adapter config, or an empty record. */
|
||||
function readAdapterEnvRecord(config: unknown): Record<string, unknown> {
|
||||
if (typeof config !== "object" || config === null || Array.isArray(config)) return {};
|
||||
const env = (config as Record<string, unknown>).env;
|
||||
if (typeof env !== "object" || env === null || Array.isArray(env)) return {};
|
||||
return env as Record<string, unknown>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when a binding is the exact fixed Claude Code OAuth user-secret
|
||||
* reference. The fixed binding is a `user_secret_ref` whose key is the fixed
|
||||
* key. Any other shape (a plain value, a company secret reference, or a
|
||||
* different user-secret key) is a replacement or a weaker binding.
|
||||
*/
|
||||
export function isFixedClaudeOAuthBinding(binding: unknown): boolean {
|
||||
if (typeof binding !== "object" || binding === null) return false;
|
||||
const record = binding as Record<string, unknown>;
|
||||
return record.type === "user_secret_ref" && record.key === CLAUDE_CODE_OAUTH_TOKEN_KEY;
|
||||
}
|
||||
|
||||
/** True when the config carries the exact fixed OAuth binding. */
|
||||
function hasFixedClaudeOAuthBinding(config: unknown): boolean {
|
||||
return isFixedClaudeOAuthBinding(readAdapterEnvRecord(config)[CLAUDE_CODE_OAUTH_TOKEN_KEY]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when the config delivers a non-empty ANTHROPIC_API_KEY. A plain
|
||||
* binding counts only when it has a non-empty value. A company or user secret
|
||||
* reference always counts, because it resolves to a value at runtime.
|
||||
*/
|
||||
function hasAnthropicApiKeyCredential(config: unknown): boolean {
|
||||
const binding = readAdapterEnvRecord(config)[ANTHROPIC_API_KEY_ENV];
|
||||
if (typeof binding === "string") return binding.trim().length > 0;
|
||||
if (typeof binding !== "object" || binding === null) return false;
|
||||
const record = binding as Record<string, unknown>;
|
||||
if (record.type === "plain") {
|
||||
return typeof record.value === "string" && record.value.trim().length > 0;
|
||||
}
|
||||
return record.type === "secret_ref" || record.type === "user_secret_ref";
|
||||
}
|
||||
|
||||
export interface ClaudeOAuthBindingInvariantInput {
|
||||
/** The effective adapter type of the write. */
|
||||
adapterType: string | null | undefined;
|
||||
/** The normalized adapter config the write persists. */
|
||||
nextConfig: unknown;
|
||||
/** The stored adapter config before the write. Null on a create. */
|
||||
priorConfig?: unknown;
|
||||
}
|
||||
|
||||
export interface ClaudeOAuthBindingInvariantDecision {
|
||||
/** True when the write adds the fixed binding that the prior config lacked. */
|
||||
introducesBinding: boolean;
|
||||
/** True when the write keeps an existing fixed binding unchanged. */
|
||||
keepsBinding: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Claude OAuth binding check. It runs after generic normalization and
|
||||
* before every database write on a `claude_local` create, hire, update,
|
||||
* approval activation, and configuration rollback path.
|
||||
*
|
||||
* The `CLAUDE_CODE_OAUTH_TOKEN` binding behaves like a normal environment
|
||||
* variable. A normal write can remove the fixed binding, or re-point it to a
|
||||
* plain value, a company-secret reference, or a different user-secret key. The
|
||||
* function no longer locks a prior fixed binding against removal or replacement.
|
||||
*
|
||||
* A write to a non-claude_local adapter that has no prior fixed binding stays
|
||||
* outside the Claude login flow. A prior fixed binding keeps the function active
|
||||
* for the write, so the function still reports whether the write keeps that
|
||||
* binding, independently of the destination adapter type.
|
||||
*
|
||||
* The precedence policy runs on every path: the fixed binding together with a
|
||||
* non-empty ANTHROPIC_API_KEY is a conflict. The function rejects that conflict
|
||||
* with a generic message that names no token value and no owner configuration.
|
||||
*
|
||||
* The function returns whether the write introduces the fixed binding or keeps
|
||||
* an existing one. The create and hire paths consume a stored-session claim when
|
||||
* the write introduces the binding. The update, approval, and rollback paths
|
||||
* reject a newly introduced binding, because they carry no claim.
|
||||
*/
|
||||
export function assertClaudeOAuthBindingInvariant(
|
||||
input: ClaudeOAuthBindingInvariantInput,
|
||||
): ClaudeOAuthBindingInvariantDecision {
|
||||
const isClaudeLocal = input.adapterType === CLAUDE_LOCAL_ADAPTER_TYPE;
|
||||
const nextIsFixed = hasFixedClaudeOAuthBinding(input.nextConfig);
|
||||
const priorIsFixed = hasFixedClaudeOAuthBinding(input.priorConfig);
|
||||
|
||||
// A write to a non-claude_local adapter that has no prior fixed binding is a
|
||||
// normal non-Claude configuration. It stays outside the Claude login flow. A
|
||||
// prior fixed binding always keeps the function active, so a write still
|
||||
// reports the binding state after a move to another adapter type.
|
||||
if (!isClaudeLocal && !priorIsFixed) {
|
||||
return { introducesBinding: false, keepsBinding: false };
|
||||
}
|
||||
|
||||
if (nextIsFixed && hasAnthropicApiKeyCredential(input.nextConfig)) {
|
||||
throw new HttpError(409, CLAUDE_OAUTH_CREDENTIAL_CONFLICT, {
|
||||
code: "claude_oauth_credential_conflict",
|
||||
});
|
||||
}
|
||||
return {
|
||||
introducesBinding: nextIsFixed && !priorIsFixed,
|
||||
keepsBinding: nextIsFixed && priorIsFixed,
|
||||
};
|
||||
}
|
||||
|
||||
/** The fixed error the create and hire paths raise for a rejected claim. */
|
||||
export function claudeOAuthClaimRejectedError(): HttpError {
|
||||
return new HttpError(409, CLAUDE_OAUTH_CLAIM_REJECTED, { code: "claude_oauth_claim_rejected" });
|
||||
}
|
||||
|
||||
type DbTransaction = Parameters<Parameters<Db["transaction"]>[0]>[0];
|
||||
type SecretBindingDb = Pick<Db | DbTransaction, "select" | "delete" | "insert">;
|
||||
|
||||
@@ -624,7 +802,9 @@ function secretResolutionErrorCode(error: unknown): SecretResolutionErrorCode {
|
||||
if (error.message === "Responsible user is required for user secret resolution") {
|
||||
return "responsible_user_missing";
|
||||
}
|
||||
if (error.message === "User secret definition not found") return "user_secret_definition_missing";
|
||||
if (error.message.startsWith("User secret definition not found")) {
|
||||
return "user_secret_definition_missing";
|
||||
}
|
||||
if (error.message === "User secret definition is not active") return "user_secret_definition_inactive";
|
||||
if (error.message === "User-scoped secrets must be resolved through user secret declarations") {
|
||||
return "secret_scope_invalid";
|
||||
@@ -682,6 +862,47 @@ function missingUserSecretDefinitionRuntimeBinding(
|
||||
};
|
||||
}
|
||||
|
||||
// A direct-resolution path (Test or save) resolves one user-secret binding at a
|
||||
// time. When the definition is gone, this builder names the environment
|
||||
// variable, the consumer, and the unresolved definition so the actor can find
|
||||
// the dangling binding. The message keeps the exact "User secret definition not
|
||||
// found" prefix so `secretResolutionErrorCode` still maps it to
|
||||
// `user_secret_definition_missing`. It never includes a secret value.
|
||||
type UserSecretDefinitionResolutionContext = {
|
||||
envKey?: string | null;
|
||||
configPath?: string | null;
|
||||
consumerType?: string | null;
|
||||
consumerId?: string | null;
|
||||
};
|
||||
|
||||
function envKeyFromConfigPath(configPath?: string | null): string | null {
|
||||
if (!configPath) return null;
|
||||
const separatorIndex = configPath.lastIndexOf(".");
|
||||
const key = separatorIndex >= 0 ? configPath.slice(separatorIndex + 1) : configPath;
|
||||
return key.length > 0 ? key : null;
|
||||
}
|
||||
|
||||
const BASE_USER_SECRET_DEFINITION_NOT_FOUND_MESSAGE = "User secret definition not found";
|
||||
|
||||
function userSecretDefinitionNotFoundMessage(
|
||||
input: { definitionId?: string | null; definitionKey?: string | null },
|
||||
context?: UserSecretDefinitionResolutionContext,
|
||||
): string {
|
||||
const parts: string[] = [];
|
||||
const envKey = context?.envKey ?? envKeyFromConfigPath(context?.configPath);
|
||||
if (envKey) parts.push(`environment variable "${envKey}"`);
|
||||
if (context?.consumerId) {
|
||||
parts.push(`${context.consumerType ?? "consumer"} ${context.consumerId}`);
|
||||
}
|
||||
if (input.definitionKey) {
|
||||
parts.push(`definition key "${input.definitionKey}"`);
|
||||
} else if (input.definitionId) {
|
||||
parts.push(`definition id "${input.definitionId}"`);
|
||||
}
|
||||
if (parts.length === 0) return BASE_USER_SECRET_DEFINITION_NOT_FOUND_MESSAGE;
|
||||
return `${BASE_USER_SECRET_DEFINITION_NOT_FOUND_MESSAGE} for ${parts.join(", ")}`;
|
||||
}
|
||||
|
||||
function assertSelectableProviderConfig(config: {
|
||||
provider: string;
|
||||
status: string;
|
||||
@@ -779,6 +1000,7 @@ export function secretService(db: Db) {
|
||||
companyId: string,
|
||||
input: { definitionId?: string | null; definitionKey?: string | null },
|
||||
source: Pick<Db | DbTransaction, "select"> = db,
|
||||
context?: UserSecretDefinitionResolutionContext,
|
||||
) {
|
||||
const definition = input.definitionId
|
||||
? await getUserSecretDefinitionById(companyId, input.definitionId, source)
|
||||
@@ -786,7 +1008,7 @@ export function secretService(db: Db) {
|
||||
? await getUserSecretDefinitionByKey(companyId, input.definitionKey, source)
|
||||
: null;
|
||||
if (!definition || definition.deletedAt || definition.status === "deleted") {
|
||||
throw notFound("User secret definition not found");
|
||||
throw notFound(userSecretDefinitionNotFoundMessage(input, context));
|
||||
}
|
||||
if (definition.companyId !== companyId) {
|
||||
throw unprocessable("User secret definition must belong to same company");
|
||||
@@ -1248,6 +1470,44 @@ export function secretService(db: Db) {
|
||||
})).value;
|
||||
}
|
||||
|
||||
// Resolve a connection secret for a durable orphan-sandbox teardown.
|
||||
//
|
||||
// The retry destroys a remote sandbox that a failed acquire left allocated.
|
||||
// The teardown needs the same connection secret the acquire used. But the
|
||||
// environment binding may be gone: a delete removed the environment, or a
|
||||
// provider change replaced the binding. So this path authorizes the read from
|
||||
// the durable `pending_cleanup` lease row, not from the environment binding,
|
||||
// and it never checks the binding. The caller passes only a secret id that the
|
||||
// durable row recorded, so the scope stays narrow. The resolution records an
|
||||
// access event for audit, and the value never enters lease metadata.
|
||||
async function resolveSecretValueForSandboxCleanup(
|
||||
companyId: string,
|
||||
secretId: string,
|
||||
version: number | "latest",
|
||||
context: {
|
||||
configPath: string;
|
||||
issueId?: string | null;
|
||||
heartbeatRunId?: string | null;
|
||||
},
|
||||
): Promise<string> {
|
||||
return (
|
||||
await resolveSecretValueInternal(companyId, secretId, version, {
|
||||
// Audit-only access context. No `bindingContext`, so the resolver never
|
||||
// asserts the environment binding that a delete or a provider change may
|
||||
// have removed.
|
||||
accessContext: {
|
||||
consumerType: "system",
|
||||
consumerId: SANDBOX_CLEANUP_CONSUMER_ID,
|
||||
actorType: "system",
|
||||
actorId: null,
|
||||
configPath: context.configPath,
|
||||
issueId: context.issueId ?? null,
|
||||
heartbeatRunId: context.heartbeatRunId ?? null,
|
||||
},
|
||||
})
|
||||
).value;
|
||||
}
|
||||
|
||||
async function resolveSecretValueForAgentAccess(
|
||||
companyId: string,
|
||||
secretId: string,
|
||||
@@ -2243,6 +2503,218 @@ export function secretService(db: Db) {
|
||||
}
|
||||
}
|
||||
|
||||
// --- Claude Code OAuth login: narrow definition and owner-bound write -------
|
||||
|
||||
type UserSecretDefinitionRow = typeof userSecretDefinitions.$inferSelect;
|
||||
type CompanySecretRow = typeof companySecrets.$inferSelect;
|
||||
|
||||
/** True only when a stored definition matches the fixed Claude OAuth shape. */
|
||||
function isCompatibleClaudeOAuthDefinition(definition: UserSecretDefinitionRow) {
|
||||
return (
|
||||
definition.key === CLAUDE_CODE_OAUTH_DEFINITION.key &&
|
||||
definition.name === CLAUDE_CODE_OAUTH_DEFINITION.name &&
|
||||
definition.provider === CLAUDE_CODE_OAUTH_DEFINITION.provider &&
|
||||
definition.managedMode === CLAUDE_CODE_OAUTH_DEFINITION.managedMode &&
|
||||
definition.status === CLAUDE_CODE_OAUTH_DEFINITION.status
|
||||
);
|
||||
}
|
||||
|
||||
/** Reads the recorded setup-token session id from an owner value, or null. */
|
||||
function readClaudeOAuthSessionId(secret: CompanySecretRow): string | null {
|
||||
const metadata = secret.providerMetadata;
|
||||
if (!metadata || typeof metadata !== "object") return null;
|
||||
const value = (metadata as Record<string, unknown>)[CLAUDE_OAUTH_SESSION_METADATA_FIELD];
|
||||
return typeof value === "string" ? value : null;
|
||||
}
|
||||
|
||||
/** Records the setup-token session id on the owner value. Not a secret. */
|
||||
async function stampClaudeOAuthSessionId(
|
||||
secret: CompanySecretRow,
|
||||
sessionId: string,
|
||||
): Promise<CompanySecretRow> {
|
||||
const nextMetadata: Record<string, unknown> = {
|
||||
...(secret.providerMetadata ?? {}),
|
||||
[CLAUDE_OAUTH_SESSION_METADATA_FIELD]: sessionId,
|
||||
};
|
||||
return db
|
||||
.update(companySecrets)
|
||||
.set({ providerMetadata: nextMetadata, updatedAt: new Date() })
|
||||
.where(eq(companySecrets.id, secret.id))
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? secret);
|
||||
}
|
||||
|
||||
function toClaudeOAuthResult(secret: CompanySecretRow): ClaudeOAuthUserSecretResult {
|
||||
return {
|
||||
secretId: secret.id,
|
||||
latestVersion: secret.latestVersion,
|
||||
definitionId: secret.userSecretDefinitionId ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the fixed Claude Code OAuth user-secret definition for a company. The
|
||||
* helper accepts no key, name, provider, mode, or status from a caller. It
|
||||
* reads the existing definition by the company and the fixed key. It returns an
|
||||
* exact compatible definition. It rejects a conflicting definition with 409 and
|
||||
* does not mutate it. After a uniqueness conflict it re-reads the row and
|
||||
* compares the fixed fields before it returns.
|
||||
*/
|
||||
async function ensureClaudeOAuthUserSecretDefinitionInternal(
|
||||
companyId: string,
|
||||
actor?: { userId?: string | null; agentId?: string | null },
|
||||
): Promise<UserSecretDefinitionRow> {
|
||||
const existing = await getUserSecretDefinitionByKey(companyId, CLAUDE_CODE_OAUTH_DEFINITION.key);
|
||||
if (existing) {
|
||||
if (!isCompatibleClaudeOAuthDefinition(existing)) {
|
||||
throw conflict(CLAUDE_OAUTH_DEFINITION_CONFLICT);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
try {
|
||||
return await db
|
||||
.insert(userSecretDefinitions)
|
||||
.values({
|
||||
companyId,
|
||||
key: CLAUDE_CODE_OAUTH_DEFINITION.key,
|
||||
name: CLAUDE_CODE_OAUTH_DEFINITION.name,
|
||||
description: null,
|
||||
status: CLAUDE_CODE_OAUTH_DEFINITION.status,
|
||||
provider: CLAUDE_CODE_OAUTH_DEFINITION.provider,
|
||||
providerConfigId: null,
|
||||
managedMode: CLAUDE_CODE_OAUTH_DEFINITION.managedMode,
|
||||
providerMetadata: null,
|
||||
usageGuidance: null,
|
||||
createdByAgentId: actor?.agentId ?? null,
|
||||
createdByUserId: actor?.userId ?? null,
|
||||
updatedByAgentId: actor?.agentId ?? null,
|
||||
updatedByUserId: actor?.userId ?? null,
|
||||
})
|
||||
.returning()
|
||||
.then((rows) => rows[0]);
|
||||
} catch (error) {
|
||||
if (isUniqueConstraintViolation(error, USER_SECRET_DEFINITION_KEY_UNIQUE_CONSTRAINT)) {
|
||||
// A concurrent create won the race. Re-read and compare the fixed fields.
|
||||
const raced = await getUserSecretDefinitionByKey(companyId, CLAUDE_CODE_OAUTH_DEFINITION.key);
|
||||
if (raced && isCompatibleClaudeOAuthDefinition(raced)) return raced;
|
||||
throw conflict(CLAUDE_OAUTH_DEFINITION_CONFLICT);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The owner-bound compare-and-set for the Claude Code OAuth value. It has two
|
||||
* modes. `first_write` creates a value only when no owner value exists.
|
||||
* `confirmed_rotation` rotates only after confirmation with the expected secret
|
||||
* id and the expected latest version. The session id is the idempotency key: a
|
||||
* repeated successful completion returns the stored result and creates no new
|
||||
* version.
|
||||
*/
|
||||
async function completeClaudeOAuthUserSecretInternal(
|
||||
companyId: string,
|
||||
ownerUserId: string,
|
||||
input: {
|
||||
sessionId: string;
|
||||
mode: "first_write" | "confirmed_rotation";
|
||||
value: string;
|
||||
expectedSecretId?: string | null;
|
||||
expectedLatestVersion?: number | null;
|
||||
},
|
||||
actor?: { userId?: string | null; agentId?: string | null },
|
||||
): Promise<ClaudeOAuthUserSecretResult> {
|
||||
const sessionId = input.sessionId?.trim();
|
||||
if (!sessionId) throw unprocessable("A Claude login session id is required");
|
||||
const value = input.value?.trim();
|
||||
if (!value) throw unprocessable("A Claude login token value is required");
|
||||
|
||||
const definition = await ensureClaudeOAuthUserSecretDefinitionInternal(companyId, actor);
|
||||
|
||||
// Idempotency: a prior successful completion for this session returns the
|
||||
// stored result and creates no new version.
|
||||
const existing = await getUserSecretValue({ companyId, ownerUserId, definitionId: definition.id });
|
||||
if (existing && readClaudeOAuthSessionId(existing) === sessionId) {
|
||||
return toClaudeOAuthResult(existing);
|
||||
}
|
||||
|
||||
if (input.mode === "first_write") {
|
||||
if (existing) throw conflict(CLAUDE_OAUTH_VALUE_EXISTS);
|
||||
let created: CompanySecretRow;
|
||||
try {
|
||||
created = await createUserSecretValueInternal(
|
||||
companyId,
|
||||
ownerUserId,
|
||||
{ definitionId: definition.id, value },
|
||||
actor,
|
||||
);
|
||||
} catch (error) {
|
||||
// Two concurrent first writes race the partial unique index. Re-read and
|
||||
// return the stored result only when this session already won.
|
||||
if (error instanceof HttpError && error.status === 409) {
|
||||
const raced = await getUserSecretValue({ companyId, ownerUserId, definitionId: definition.id });
|
||||
if (raced && readClaudeOAuthSessionId(raced) === sessionId) {
|
||||
return toClaudeOAuthResult(raced);
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const stamped = await stampClaudeOAuthSessionId(created, sessionId);
|
||||
return toClaudeOAuthResult(stamped);
|
||||
}
|
||||
|
||||
// confirmed_rotation.
|
||||
if (!input.expectedSecretId || input.expectedLatestVersion == null) {
|
||||
throw unprocessable("A confirmed rotation requires expectedSecretId and expectedLatestVersion");
|
||||
}
|
||||
// The owner-scoped lookup fails closed for a cross-owner or cross-company id.
|
||||
const current = await getUserSecretValueById(companyId, ownerUserId, input.expectedSecretId);
|
||||
if (current.userSecretDefinitionId !== definition.id) {
|
||||
throw notFound("User secret value not found");
|
||||
}
|
||||
if (current.latestVersion !== input.expectedLatestVersion) {
|
||||
throw conflict(CLAUDE_OAUTH_STALE_CONFIRMATION);
|
||||
}
|
||||
let rotated: CompanySecretRow;
|
||||
try {
|
||||
rotated = await secretService(db).rotate(
|
||||
current.id,
|
||||
{ value, expectedLatestVersion: input.expectedLatestVersion },
|
||||
actor,
|
||||
);
|
||||
} catch (error) {
|
||||
if (error instanceof HttpError && error.status === 409) {
|
||||
// A concurrent rotation bumped the version between the read and the
|
||||
// predicate. Return the same fixed stale-confirmation conflict.
|
||||
throw conflict(CLAUDE_OAUTH_STALE_CONFIRMATION);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
const stamped = await stampClaudeOAuthSessionId(rotated, sessionId);
|
||||
return toClaudeOAuthResult(stamped);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the stored Claude Code OAuth value metadata for one owner. It returns
|
||||
* only the secret id and the latest version, never the token. The client uses
|
||||
* the version as the expected version of a later confirmed rotation.
|
||||
*
|
||||
* The reader derives no definition, no owner, and no secret id from a caller.
|
||||
* It reads the fixed definition by the company and the fixed key. It reads the
|
||||
* value by the company, the owner, and that definition. It returns null when no
|
||||
* definition or no owner value exists, so a foreign value and a missing value
|
||||
* look the same to the caller. It never creates the definition.
|
||||
*/
|
||||
async function readClaudeOAuthUserSecretStatusInternal(
|
||||
companyId: string,
|
||||
ownerUserId: string,
|
||||
): Promise<{ secretId: string; latestVersion: number } | null> {
|
||||
const definition = await getUserSecretDefinitionByKey(companyId, CLAUDE_CODE_OAUTH_DEFINITION.key);
|
||||
if (!definition || !isCompatibleClaudeOAuthDefinition(definition)) return null;
|
||||
const existing = await getUserSecretValue({ companyId, ownerUserId, definitionId: definition.id });
|
||||
if (!existing) return null;
|
||||
return { secretId: existing.id, latestVersion: existing.latestVersion };
|
||||
}
|
||||
|
||||
async function removeSecretInternal(secretId: string) {
|
||||
const secret = await getById(secretId);
|
||||
if (!secret) return null;
|
||||
@@ -2821,6 +3293,25 @@ export function secretService(db: Db) {
|
||||
|
||||
createCurrentUserSecretValue: createUserSecretValueInternal,
|
||||
|
||||
// The narrow Claude Code OAuth definition helper. A caller passes
|
||||
// no key, name, provider, mode, or status. The route calls it only after the
|
||||
// authenticated board-user, company, and sandbox checks pass.
|
||||
ensureClaudeOAuthUserSecretDefinition: (
|
||||
companyId: string,
|
||||
actor?: { userId?: string | null; agentId?: string | null },
|
||||
) => ensureClaudeOAuthUserSecretDefinitionInternal(companyId, actor),
|
||||
|
||||
// The owner-bound Claude Code OAuth compare-and-set. It creates a
|
||||
// first value or rotates after a confirmed expected version. The session id
|
||||
// is the idempotency key for one completion.
|
||||
completeClaudeOAuthUserSecret: completeClaudeOAuthUserSecretInternal,
|
||||
|
||||
// The owner-bound Claude Code OAuth status read. It returns only
|
||||
// the secret id and the latest version for the owner value, or null. It never
|
||||
// returns the token and never creates the definition. The status route reads
|
||||
// the expected version from it before it captures the confirmed rotation.
|
||||
readClaudeOAuthUserSecretStatus: readClaudeOAuthUserSecretStatusInternal,
|
||||
|
||||
rotateCurrentUserSecretValue: async (
|
||||
companyId: string,
|
||||
ownerUserId: string,
|
||||
@@ -2916,6 +3407,12 @@ export function secretService(db: Db) {
|
||||
companyId,
|
||||
{ definitionKey: ref.definitionKey },
|
||||
targetDb,
|
||||
{
|
||||
envKey: ref.envKey,
|
||||
configPath: ref.configPath,
|
||||
consumerType: target.targetType,
|
||||
consumerId: target.targetId,
|
||||
},
|
||||
);
|
||||
normalizedRefs.push({
|
||||
definitionId: definition.id,
|
||||
@@ -2989,7 +3486,11 @@ export function secretService(db: Db) {
|
||||
const optionalBinding = input.allowMissingOverride || input.required === false;
|
||||
let definition: typeof userSecretDefinitions.$inferSelect;
|
||||
try {
|
||||
definition = await resolveUserSecretDefinition(companyId, input);
|
||||
definition = await resolveUserSecretDefinition(companyId, input, db, {
|
||||
configPath: context?.configPath ?? null,
|
||||
consumerType: context?.consumerType ?? null,
|
||||
consumerId: context?.consumerId ?? null,
|
||||
});
|
||||
} catch (error) {
|
||||
if (optionalBinding && error instanceof HttpError && error.status === 404) return null;
|
||||
throw error;
|
||||
@@ -3370,6 +3871,7 @@ export function secretService(db: Db) {
|
||||
resolveSecretValueForAgentAccess,
|
||||
listAgentSecretAccess,
|
||||
resolveSecretValueForEphemeralAccess,
|
||||
resolveSecretValueForSandboxCleanup,
|
||||
|
||||
create: async (
|
||||
companyId: string,
|
||||
@@ -3591,12 +4093,19 @@ export function secretService(db: Db) {
|
||||
externalRef?: string | null;
|
||||
providerVersionRef?: string | null;
|
||||
providerConfigId?: string | null;
|
||||
// The optional owner-bound compare-and-set guard. When set, the final
|
||||
// update matches the latest version, so a concurrent rotation between the
|
||||
// read and the write cannot pass. A mismatch throws a 409 conflict.
|
||||
expectedLatestVersion?: number;
|
||||
},
|
||||
actor?: { userId?: string | null; agentId?: string | null },
|
||||
) => {
|
||||
const secret = await getById(secretId);
|
||||
if (!secret) throw notFound("Secret not found");
|
||||
if (secret.status !== "active") throw unprocessable("Cannot rotate a non-active secret");
|
||||
if (input.expectedLatestVersion !== undefined && secret.latestVersion !== input.expectedLatestVersion) {
|
||||
throw conflict(SECRET_VERSION_STALE_CONFLICT);
|
||||
}
|
||||
const providerId = secret.provider as SecretProvider;
|
||||
const provider = getSecretProvider(providerId);
|
||||
const providerConfigId =
|
||||
@@ -3698,6 +4207,17 @@ export function secretService(db: Db) {
|
||||
operation: "rotate.prepare_rollback",
|
||||
});
|
||||
}
|
||||
// A guarded concurrent rotation inserted the same next version first, so
|
||||
// this insert fails the (secretId, version) unique index. The loser never
|
||||
// reaches the compare-and-set guard below. Normalize only that one
|
||||
// collision to the same stale conflict, so a guarded rotation always
|
||||
// returns one fixed 409. Re-throw every other error unchanged.
|
||||
if (
|
||||
input.expectedLatestVersion !== undefined &&
|
||||
isUniqueConstraintViolation(error, COMPANY_SECRET_VERSION_UNIQUE_CONSTRAINT)
|
||||
) {
|
||||
throw conflict(SECRET_VERSION_STALE_CONFLICT);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -3727,11 +4247,25 @@ export function secretService(db: Db) {
|
||||
lastRotatedAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(companySecrets.id, secret.id))
|
||||
.where(and(
|
||||
eq(companySecrets.id, secret.id),
|
||||
// The compare-and-set guard. It matches the latest version, so a
|
||||
// concurrent rotation that already bumped it fails this predicate.
|
||||
input.expectedLatestVersion === undefined
|
||||
? undefined
|
||||
: eq(companySecrets.latestVersion, input.expectedLatestVersion),
|
||||
))
|
||||
.returning()
|
||||
.then((rows) => rows[0] ?? null);
|
||||
|
||||
if (!updated) throw notFound("Secret not found");
|
||||
if (!updated) {
|
||||
// The predicate matched no row. A supplied expected version means a
|
||||
// concurrent rotation won the race; return the stale conflict. An
|
||||
// unguarded rotation means the secret is gone.
|
||||
throw input.expectedLatestVersion === undefined
|
||||
? notFound("Secret not found")
|
||||
: conflict(SECRET_VERSION_STALE_CONFLICT);
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -4209,7 +4743,12 @@ export function secretService(db: Db) {
|
||||
if (!parsed.success) continue;
|
||||
const binding = canonicalizeBinding(parsed.data as EnvBinding);
|
||||
if (binding.type === "user_secret_ref") {
|
||||
await resolveUserSecretDefinition(companyId, { definitionKey: binding.key }, bindingDb);
|
||||
await resolveUserSecretDefinition(companyId, { definitionKey: binding.key }, bindingDb, {
|
||||
envKey: key,
|
||||
configPath: `${pathPrefix}.${key}`,
|
||||
consumerType: target.targetType,
|
||||
consumerId: target.targetId,
|
||||
});
|
||||
userRefs.push({
|
||||
definitionKey: binding.key,
|
||||
configPath: `${pathPrefix}.${key}`,
|
||||
@@ -4279,7 +4818,17 @@ export function secretService(db: Db) {
|
||||
if (userRefs.length === 0) return;
|
||||
const definitions = new Map<string, string>();
|
||||
for (const ref of userRefs) {
|
||||
const definition = await resolveUserSecretDefinition(companyId, { definitionKey: ref.definitionKey }, targetDb);
|
||||
const definition = await resolveUserSecretDefinition(
|
||||
companyId,
|
||||
{ definitionKey: ref.definitionKey },
|
||||
targetDb,
|
||||
{
|
||||
envKey: ref.envKey,
|
||||
configPath: ref.configPath,
|
||||
consumerType: target.targetType,
|
||||
consumerId: target.targetId,
|
||||
},
|
||||
);
|
||||
definitions.set(ref.definitionKey, definition.id);
|
||||
}
|
||||
await targetDb.insert(userSecretDeclarations).values(
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import {
|
||||
claudeSetupTokenSessions,
|
||||
companies,
|
||||
createDb,
|
||||
environments,
|
||||
getEmbeddedPostgresTestSupport,
|
||||
startEmbeddedPostgresTestDatabase,
|
||||
} from "@paperclipai/db";
|
||||
import {
|
||||
createDbSetupTokenCleanupStore,
|
||||
SetupTokenSessionService,
|
||||
SetupTokenSessionError,
|
||||
assessConfidentialStartup,
|
||||
@@ -11,6 +22,8 @@ import {
|
||||
SETUP_TOKEN_RATE_LIMITED,
|
||||
SETUP_TOKEN_CAP_EXCEEDED,
|
||||
SETUP_TOKEN_TOKEN_UNAVAILABLE,
|
||||
SETUP_TOKEN_STORAGE_FAILED,
|
||||
type SetupTokenCleanupIdentity,
|
||||
type SetupTokenCleanupRecord,
|
||||
type SetupTokenCleanupStore,
|
||||
type SetupTokenCredentialSink,
|
||||
@@ -21,6 +34,7 @@ import {
|
||||
type SetupTokenLoginProcessFactory,
|
||||
type SetupTokenPromptSink,
|
||||
type SetupTokenRateLimiter,
|
||||
type SetupTokenSecretWriter,
|
||||
type SetupTokenSessionScope,
|
||||
} from "./setup-token-session.js";
|
||||
import { redactSensitive } from "../middleware/redact-sensitive.js";
|
||||
@@ -37,6 +51,8 @@ const OWNER_SCOPE: SetupTokenSessionScope = {
|
||||
companyId: "company-1",
|
||||
ownerUserId: "user-1",
|
||||
targetAgentId: "agent-1",
|
||||
adapterType: "claude_local",
|
||||
environmentId: "env-1",
|
||||
};
|
||||
|
||||
/** A controllable fake login process. It records the call order into `events`. */
|
||||
@@ -59,8 +75,10 @@ class FakeProcess implements SetupTokenLoginProcess {
|
||||
surfacePrompt(url: string): void {
|
||||
this.onPrompt({ url });
|
||||
}
|
||||
surfaceCredential(token: string): void {
|
||||
this.onCredential(token);
|
||||
// The credential sink is asynchronous: it awaits the owner-bound secret write.
|
||||
// The test awaits this call so the write settles before it asserts.
|
||||
async surfaceCredential(token: string): Promise<void> {
|
||||
await this.onCredential(token);
|
||||
}
|
||||
finish(outcome: SetupTokenLoginOutcome): void {
|
||||
this.resolveDone(outcome);
|
||||
@@ -103,61 +121,168 @@ class FakeLeaseManager implements SetupTokenLeaseManager {
|
||||
}
|
||||
}
|
||||
|
||||
// A lease manager whose `acquire` stays in flight until the test resolves it.
|
||||
// The test uses it to hold the first start suspended on the lease acquire, then
|
||||
// prove a concurrent start for the same owner fails closed before it reaches the
|
||||
// provider. It can also fail the next acquire, to drive the acquire-failure
|
||||
// rollback path.
|
||||
class DeferredLeaseManager implements SetupTokenLeaseManager {
|
||||
acquireCalls = 0;
|
||||
released: string[] = [];
|
||||
releaseByIdCalls: string[] = [];
|
||||
failNextAcquire = false;
|
||||
private counter = 0;
|
||||
private readonly pending: Array<{ resolve: (lease: SetupTokenLease) => void; id: string }> = [];
|
||||
async acquire(): Promise<SetupTokenLease> {
|
||||
this.acquireCalls += 1;
|
||||
if (this.failNextAcquire) {
|
||||
this.failNextAcquire = false;
|
||||
throw new Error("lease acquire failed");
|
||||
}
|
||||
const id = `lease-${(this.counter += 1)}`;
|
||||
return new Promise<SetupTokenLease>((resolve) => {
|
||||
this.pending.push({ resolve, id });
|
||||
});
|
||||
}
|
||||
/** Resolves the oldest in-flight acquire with its lease. */
|
||||
resolveNextAcquire(): void {
|
||||
const next = this.pending.shift();
|
||||
if (next) next.resolve({ id: next.id });
|
||||
}
|
||||
/** The count of acquires that are in flight. */
|
||||
pendingCount(): number {
|
||||
return this.pending.length;
|
||||
}
|
||||
async release(lease: SetupTokenLease): Promise<void> {
|
||||
this.released.push(lease.id);
|
||||
}
|
||||
async releaseById(leaseId: string): Promise<void> {
|
||||
this.releaseByIdCalls.push(leaseId);
|
||||
this.released.push(leaseId);
|
||||
}
|
||||
}
|
||||
|
||||
// Builds the durable-record identity from a session scope and the session id. The
|
||||
// fake writers use it to simulate the atomic durable `stored` transition that the
|
||||
// production writer performs inside the credential transaction.
|
||||
function identityFromScope(scope: SetupTokenSessionScope, sessionId: string): SetupTokenCleanupIdentity {
|
||||
return {
|
||||
sessionId,
|
||||
companyId: scope.companyId,
|
||||
ownerUserId: scope.ownerUserId,
|
||||
adapterType: scope.adapterType,
|
||||
environmentId: scope.environmentId,
|
||||
};
|
||||
}
|
||||
|
||||
function identityMatchesRow(row: SetupTokenCleanupRecord, identity: SetupTokenCleanupIdentity): boolean {
|
||||
return (
|
||||
row.companyId === identity.companyId &&
|
||||
row.ownerUserId === identity.ownerUserId &&
|
||||
row.adapterType === identity.adapterType &&
|
||||
row.environmentId === identity.environmentId
|
||||
);
|
||||
}
|
||||
|
||||
class FakeStore implements SetupTokenCleanupStore {
|
||||
rows = new Map<string, SetupTokenCleanupRecord>();
|
||||
async record(record: SetupTokenCleanupRecord): Promise<void> {
|
||||
this.rows.set(record.sessionId, { ...record });
|
||||
}
|
||||
async markState(sessionId: string, state: SetupTokenCleanupRecord["state"]): Promise<void> {
|
||||
const row = this.rows.get(sessionId);
|
||||
if (row) row.state = state;
|
||||
async markState(identity: SetupTokenCleanupIdentity, state: SetupTokenCleanupRecord["state"]): Promise<void> {
|
||||
const row = this.rows.get(identity.sessionId);
|
||||
// The write matches the full owner scope, so it never updates a row by the
|
||||
// session id alone.
|
||||
if (row && identityMatchesRow(row, identity)) row.state = state;
|
||||
}
|
||||
async remove(sessionId: string): Promise<void> {
|
||||
this.rows.delete(sessionId);
|
||||
async remove(identity: SetupTokenCleanupIdentity): Promise<void> {
|
||||
// The delete matches the full owner scope, so it never removes a row by the
|
||||
// session id alone.
|
||||
const row = this.rows.get(identity.sessionId);
|
||||
if (row && identityMatchesRow(row, identity)) this.rows.delete(identity.sessionId);
|
||||
}
|
||||
async listReapable(now: number): Promise<SetupTokenCleanupRecord[]> {
|
||||
return [...this.rows.values()].filter(
|
||||
(row) => isTerminalSessionState(row.state) || row.deadline <= now,
|
||||
(row) => isTerminalSessionState(row.state) || row.deadline <= now || row.boundAt !== null,
|
||||
);
|
||||
}
|
||||
async consumeStoredClaim(identity: SetupTokenCleanupIdentity): Promise<SetupTokenCleanupRecord | null> {
|
||||
const row = this.rows.get(identity.sessionId);
|
||||
if (
|
||||
!row ||
|
||||
!identityMatchesRow(row, identity) ||
|
||||
row.state !== "stored" ||
|
||||
row.boundAt !== null ||
|
||||
row.deadline <= Date.now()
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
row.boundAt = Date.now();
|
||||
return { ...row };
|
||||
}
|
||||
}
|
||||
|
||||
function allowAllRateLimiter(): SetupTokenRateLimiter {
|
||||
return { consume: () => ({ allowed: true, retryAfterSeconds: 0 }) };
|
||||
}
|
||||
|
||||
function buildService(overrides: {
|
||||
// One recorded owner-bound secret write. The fake writer records only the
|
||||
// non-secret ids and the token, so a test can prove the token reached the writer
|
||||
// and never the store or a log.
|
||||
interface RecordedSecretWrite {
|
||||
scope: SetupTokenSessionScope;
|
||||
sessionId: string;
|
||||
token: string;
|
||||
}
|
||||
|
||||
function buildService<L extends SetupTokenLeaseManager = FakeLeaseManager>(overrides: {
|
||||
events?: string[];
|
||||
leases?: FakeLeaseManager;
|
||||
leases?: L;
|
||||
store?: FakeStore;
|
||||
rateLimiter?: SetupTokenRateLimiter;
|
||||
caps?: { perOwner: number; perAgent: number; perCompany: number };
|
||||
ttlMs?: number;
|
||||
tokenRetentionMs?: number;
|
||||
now?: () => number;
|
||||
completeCredential?: SetupTokenSecretWriter;
|
||||
factory?: SetupTokenLoginProcessFactory;
|
||||
} = {}) {
|
||||
const events = overrides.events ?? [];
|
||||
const processes: FakeProcess[] = [];
|
||||
let processCounter = 0;
|
||||
const factory: SetupTokenLoginProcessFactory = ({ onPrompt, onCredential }) => {
|
||||
processCounter += 1;
|
||||
const process = new FakeProcess(`p${processCounter}`, onPrompt, onCredential, events);
|
||||
processes.push(process);
|
||||
return process;
|
||||
};
|
||||
const leases = overrides.leases ?? new FakeLeaseManager(events);
|
||||
const factory: SetupTokenLoginProcessFactory =
|
||||
overrides.factory ??
|
||||
(({ onPrompt, onCredential }) => {
|
||||
processCounter += 1;
|
||||
const process = new FakeProcess(`p${processCounter}`, onPrompt, onCredential, events);
|
||||
processes.push(process);
|
||||
return process;
|
||||
});
|
||||
const leases: L = overrides.leases ?? (new FakeLeaseManager(events) as unknown as L);
|
||||
const store = overrides.store ?? new FakeStore();
|
||||
const secretWrites: RecordedSecretWrite[] = [];
|
||||
const completeCredential: SetupTokenSecretWriter =
|
||||
overrides.completeCredential ??
|
||||
(async (input) => {
|
||||
secretWrites.push({ scope: input.scope, sessionId: input.sessionId, token: input.token });
|
||||
// Simulate the atomic durable `stored` transition the production writer runs
|
||||
// inside the credential transaction, so the store reflects the claim.
|
||||
await store.markState(identityFromScope(input.scope, input.sessionId), "stored");
|
||||
});
|
||||
const logs: string[] = [];
|
||||
const service = new SetupTokenSessionService({
|
||||
factory,
|
||||
leases,
|
||||
store,
|
||||
completeCredential,
|
||||
rateLimiter: overrides.rateLimiter ?? allowAllRateLimiter(),
|
||||
caps: overrides.caps ?? { perOwner: 5, perAgent: 5, perCompany: 5 },
|
||||
ttlMs: overrides.ttlMs ?? 60_000,
|
||||
tokenRetentionMs: overrides.tokenRetentionMs,
|
||||
now: overrides.now,
|
||||
log: (line) => logs.push(line),
|
||||
});
|
||||
return { service, processes, leases, store, events };
|
||||
return { service, processes, leases, store, events, secretWrites, logs };
|
||||
}
|
||||
|
||||
describe("SetupTokenSessionService.start", () => {
|
||||
@@ -206,6 +331,140 @@ describe("SetupTokenSessionService.start", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("SetupTokenSessionService.start cap reservation (concurrency)", () => {
|
||||
const capsPerOwnerOne = { perOwner: 1, perAgent: 5, perCompany: 5 };
|
||||
|
||||
it("reserves the owner slot synchronously, so a concurrent start fails closed with 429", async () => {
|
||||
const leases = new DeferredLeaseManager();
|
||||
const { service } = buildService({ leases, caps: capsPerOwnerOne });
|
||||
|
||||
// Start the first session. It reserves the owner slot synchronously, then it
|
||||
// suspends on the deferred lease acquire. The acquire reached the provider and
|
||||
// has not resolved yet.
|
||||
const firstStart = service.start(OWNER_SCOPE);
|
||||
await flush();
|
||||
expect(leases.acquireCalls).toBe(1);
|
||||
expect(leases.pendingCount()).toBe(1);
|
||||
|
||||
// The second start for the same owner runs while the first acquire is in
|
||||
// flight. The synchronous reservation already holds the one owner slot, so the
|
||||
// second start fails closed with the fixed 429 cap error before it reaches the
|
||||
// provider. Only one acquire ever reaches the provider.
|
||||
await expect(service.start(OWNER_SCOPE)).rejects.toMatchObject({
|
||||
status: 429,
|
||||
message: SETUP_TOKEN_CAP_EXCEEDED,
|
||||
});
|
||||
expect(leases.acquireCalls).toBe(1);
|
||||
|
||||
// Let the first start finish. It holds the one live session.
|
||||
leases.resolveNextAcquire();
|
||||
const first = await firstStart;
|
||||
expect(first.sessionId).toBeTruthy();
|
||||
expect(service.activeSessionCount()).toBe(1);
|
||||
});
|
||||
|
||||
it("releases the owner reservation after cleanup, so the owner can start again", async () => {
|
||||
const leases = new DeferredLeaseManager();
|
||||
const { service } = buildService({ leases, caps: capsPerOwnerOne });
|
||||
|
||||
const firstStart = service.start(OWNER_SCOPE);
|
||||
await flush();
|
||||
leases.resolveNextAcquire();
|
||||
const first = await firstStart;
|
||||
|
||||
// Cancel the live session. The cleanup releases the owner reservation.
|
||||
await service.cancel(first.sessionId, OWNER_SCOPE);
|
||||
expect(service.activeSessionCount()).toBe(0);
|
||||
|
||||
// A new start for the same owner reserves the slot again and starts, so the
|
||||
// cleanup released the reservation exactly once and left no slot held.
|
||||
const secondStart = service.start(OWNER_SCOPE);
|
||||
await flush();
|
||||
leases.resolveNextAcquire();
|
||||
const second = await secondStart;
|
||||
expect(second.sessionId).toBeTruthy();
|
||||
expect(second.sessionId).not.toBe(first.sessionId);
|
||||
expect(service.activeSessionCount()).toBe(1);
|
||||
});
|
||||
|
||||
it("releases the owner reservation when the lease acquire fails, so a retry can start", async () => {
|
||||
const leases = new DeferredLeaseManager();
|
||||
leases.failNextAcquire = true;
|
||||
const { service } = buildService({ leases, caps: capsPerOwnerOne });
|
||||
|
||||
// The first start reserves the owner slot, then the lease acquire rejects. The
|
||||
// failure path rolls back the reservation and holds no durable state.
|
||||
await expect(service.start(OWNER_SCOPE)).rejects.toThrow("lease acquire failed");
|
||||
expect(service.activeSessionCount()).toBe(0);
|
||||
expect(leases.released).toEqual([]);
|
||||
|
||||
// A retry for the same owner reserves the slot again and starts. The failed
|
||||
// start left no reservation behind.
|
||||
const retry = service.start(OWNER_SCOPE);
|
||||
await flush();
|
||||
expect(leases.acquireCalls).toBe(2);
|
||||
leases.resolveNextAcquire();
|
||||
const result = await retry;
|
||||
expect(result.sessionId).toBeTruthy();
|
||||
expect(service.activeSessionCount()).toBe(1);
|
||||
});
|
||||
|
||||
it("releases the reservation and the lease when the durable record write fails", async () => {
|
||||
const leases = new FakeLeaseManager();
|
||||
const store = new FakeStore();
|
||||
let failRecord = true;
|
||||
const recordOriginal = store.record.bind(store);
|
||||
store.record = async (record) => {
|
||||
if (failRecord) {
|
||||
failRecord = false;
|
||||
throw new Error("durable write failed");
|
||||
}
|
||||
return recordOriginal(record);
|
||||
};
|
||||
const { service } = buildService({ leases, store, caps: capsPerOwnerOne });
|
||||
|
||||
// The durable write fails. The failure path releases the acquired lease and
|
||||
// rolls back the reservation.
|
||||
await expect(service.start(OWNER_SCOPE)).rejects.toThrow("durable write failed");
|
||||
expect(leases.released).toEqual(["lease-1"]);
|
||||
expect(service.activeSessionCount()).toBe(0);
|
||||
|
||||
// The reservation rolled back, so a retry for the same owner starts.
|
||||
const retry = await service.start(OWNER_SCOPE);
|
||||
expect(retry.sessionId).toBeTruthy();
|
||||
expect(service.activeSessionCount()).toBe(1);
|
||||
});
|
||||
|
||||
it("releases the reservation and the lease when the factory throws, so a retry can start", async () => {
|
||||
const leases = new FakeLeaseManager();
|
||||
let failFactory = true;
|
||||
const factory: SetupTokenLoginProcessFactory = () => {
|
||||
if (failFactory) {
|
||||
failFactory = false;
|
||||
throw new Error("factory failed");
|
||||
}
|
||||
// A minimal live process for the retry. It never ends on its own.
|
||||
return {
|
||||
done: new Promise<SetupTokenLoginOutcome>(() => {}),
|
||||
submitCode: () => {},
|
||||
stop: () => {},
|
||||
};
|
||||
};
|
||||
const { service } = buildService({ leases, factory, caps: capsPerOwnerOne });
|
||||
|
||||
// The factory throws. The start releases the lease, drops the durable record,
|
||||
// rolls back the reservation, and returns the fixed 503 start error.
|
||||
await expect(service.start(OWNER_SCOPE)).rejects.toMatchObject({ status: 503 });
|
||||
expect(leases.released).toEqual(["lease-1"]);
|
||||
expect(service.activeSessionCount()).toBe(0);
|
||||
|
||||
// The reservation rolled back, so a retry for the same owner starts.
|
||||
const retry = await service.start(OWNER_SCOPE);
|
||||
expect(retry.sessionId).toBeTruthy();
|
||||
expect(service.activeSessionCount()).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SetupTokenSessionService.readPrompt", () => {
|
||||
it("returns the full login URL to the authorized owner once the prompt surfaces", async () => {
|
||||
const { service, processes } = buildService();
|
||||
@@ -248,16 +507,26 @@ describe("SetupTokenSessionService authorization boundary", () => {
|
||||
const crossCompany: SetupTokenSessionScope = { ...OWNER_SCOPE, companyId: "company-2" };
|
||||
const sameCompanyOtherUser: SetupTokenSessionScope = { ...OWNER_SCOPE, ownerUserId: "user-9" };
|
||||
const otherAgent: SetupTokenSessionScope = { ...OWNER_SCOPE, targetAgentId: "agent-9" };
|
||||
const otherAdapter: SetupTokenSessionScope = { ...OWNER_SCOPE, adapterType: "codex_local" };
|
||||
const otherEnvironment: SetupTokenSessionScope = { ...OWNER_SCOPE, environmentId: "env-9" };
|
||||
|
||||
it("returns the same not-found for a cross-scope caller on every operation", async () => {
|
||||
const { service, processes } = buildService();
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
|
||||
for (const scope of [crossCompany, sameCompanyOtherUser, otherAgent]) {
|
||||
// The full-scope match rejects a mismatch in any of the five scope fields:
|
||||
// the company, the owner, the agent, the adapter, and the environment.
|
||||
for (const scope of [
|
||||
crossCompany,
|
||||
sameCompanyOtherUser,
|
||||
otherAgent,
|
||||
otherAdapter,
|
||||
otherEnvironment,
|
||||
]) {
|
||||
expect(() => service.readPrompt(sessionId, scope)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
expect(() => service.submitCode(sessionId, scope, "code")).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
expect(() => service.receiveToken(sessionId, scope)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
expect(() => service.completeSession(sessionId, scope)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
await expect(service.cancel(sessionId, scope)).rejects.toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
await expect(service.expire(sessionId, scope)).rejects.toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
}
|
||||
@@ -269,6 +538,57 @@ describe("SetupTokenSessionService authorization boundary", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("SetupTokenSessionService company-and-environment scope", () => {
|
||||
// The agentless company scope. It carries a null agent id.
|
||||
const COMPANY_SCOPE: SetupTokenSessionScope = { ...OWNER_SCOPE, targetAgentId: null };
|
||||
const companyKey = {
|
||||
companyId: COMPANY_SCOPE.companyId,
|
||||
ownerUserId: COMPANY_SCOPE.ownerUserId,
|
||||
adapterType: COMPANY_SCOPE.adapterType,
|
||||
};
|
||||
|
||||
it("resolves an agentless session by the company key and returns the intrinsic environment", async () => {
|
||||
const { service, processes } = buildService();
|
||||
const { sessionId } = await service.start(COMPANY_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
|
||||
const scope = service.resolveCompanyScope(sessionId, companyKey);
|
||||
expect(scope.targetAgentId).toBeNull();
|
||||
expect(scope.environmentId).toBe(COMPANY_SCOPE.environmentId);
|
||||
|
||||
const descriptor = service.describeOwned(sessionId, scope);
|
||||
expect(descriptor.sessionId).toBe(sessionId);
|
||||
expect(descriptor.environmentId).toBe(COMPANY_SCOPE.environmentId);
|
||||
expect(descriptor.loginUrl).toBe(FULL_LOGIN_URL);
|
||||
});
|
||||
|
||||
it("rejects an agent-scoped session through the company key", async () => {
|
||||
const { service } = buildService();
|
||||
// The session carries a non-null agent id. The company key requires the
|
||||
// agentless marker, so a company route cannot reach an agent session.
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
expect(() => service.resolveCompanyScope(sessionId, companyKey)).toThrow(
|
||||
SETUP_TOKEN_SESSION_NOT_FOUND,
|
||||
);
|
||||
});
|
||||
|
||||
it("returns the same not-found for a foreign company key", async () => {
|
||||
const { service } = buildService();
|
||||
const { sessionId } = await service.start(COMPANY_SCOPE);
|
||||
// A cross-company, a cross-owner, and a cross-adapter key each return the
|
||||
// same not-found error as a missing session.
|
||||
for (const key of [
|
||||
{ ...companyKey, companyId: "company-2" },
|
||||
{ ...companyKey, ownerUserId: "user-9" },
|
||||
{ ...companyKey, adapterType: "codex_local" },
|
||||
]) {
|
||||
expect(() => service.resolveCompanyScope(sessionId, key)).toThrow(
|
||||
SETUP_TOKEN_SESSION_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("SetupTokenSessionService cleanup order", () => {
|
||||
it("stops the direct child before the lease release on cancel", async () => {
|
||||
const events: string[] = [];
|
||||
@@ -313,10 +633,12 @@ describe("SetupTokenSessionService durable reaper", () => {
|
||||
sessionId: "orphan-1",
|
||||
companyId: "company-1",
|
||||
ownerUserId: "user-1",
|
||||
targetAgentId: "agent-1",
|
||||
adapterType: "claude_local",
|
||||
environmentId: "env-1",
|
||||
leaseId: "lease-orphan",
|
||||
deadline: 1_000,
|
||||
state: "awaiting_code",
|
||||
boundAt: null,
|
||||
});
|
||||
const { service } = buildService({ store, leases, now: () => 5_000 });
|
||||
const summary = await service.reap(5_000);
|
||||
@@ -331,10 +653,12 @@ describe("SetupTokenSessionService durable reaper", () => {
|
||||
sessionId: "orphan-2",
|
||||
companyId: "company-1",
|
||||
ownerUserId: "user-1",
|
||||
targetAgentId: "agent-1",
|
||||
adapterType: "claude_local",
|
||||
environmentId: "env-1",
|
||||
leaseId: "lease-orphan-2",
|
||||
deadline: 1_000,
|
||||
state: "failed",
|
||||
boundAt: null,
|
||||
});
|
||||
const leases = new FakeLeaseManager();
|
||||
leases.releaseById = async () => {
|
||||
@@ -347,43 +671,228 @@ describe("SetupTokenSessionService durable reaper", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("SetupTokenSessionService.receiveToken", () => {
|
||||
it("returns the token once to the authorized owner after a successful login", async () => {
|
||||
const { service, processes, leases } = buildService();
|
||||
describe("SetupTokenSessionService.completeSession", () => {
|
||||
it("returns the non-secret storedSessionId after a successful secret write, and no token", async () => {
|
||||
const { service, processes, leases, store, secretWrites, logs } = buildService();
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "code");
|
||||
processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
// The session awaits the owner-bound secret write before it completes.
|
||||
await processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
processes[0].finish("success");
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
// The service releases the sandbox lease at once, but it retains the token
|
||||
// for the owner to receive it one time.
|
||||
|
||||
// The token reached the owner-bound writer, once, with the owner scope.
|
||||
expect(secretWrites).toEqual([{ scope: OWNER_SCOPE, sessionId, token: SYNTH_TOKEN }]);
|
||||
// The service releases the sandbox lease at once.
|
||||
expect(leases.released).toEqual(["lease-1"]);
|
||||
const received = service.receiveToken(sessionId, OWNER_SCOPE);
|
||||
expect(received.token).toBe(SYNTH_TOKEN);
|
||||
// One-shot: a second receive returns the same not-found as a missing session.
|
||||
expect(() => service.receiveToken(sessionId, OWNER_SCOPE)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
// The durable row stays as the stored-session claim, not removed.
|
||||
expect(store.rows.get(sessionId)?.state).toBe("stored");
|
||||
|
||||
// The completion returns the non-secret storedSessionId and carries no token.
|
||||
const completion = service.completeSession(sessionId, OWNER_SCOPE);
|
||||
expect(completion.storedSessionId).toBe(sessionId);
|
||||
expect(completion).not.toHaveProperty("token");
|
||||
|
||||
// No response, log, or durable record contains the token after success.
|
||||
const haystack = `${logs.join("\n")}\n${JSON.stringify(completion)}\n${JSON.stringify([
|
||||
...store.rows.values(),
|
||||
])}`;
|
||||
expect(haystack).not.toContain(SYNTH_TOKEN);
|
||||
});
|
||||
|
||||
it("returns the fixed unavailable error before the token is delivered", async () => {
|
||||
it("returns the fixed unavailable error before the secret write completes", async () => {
|
||||
const { service, processes } = buildService();
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "code");
|
||||
// The login has not delivered the token yet, so receive-token is unavailable.
|
||||
expect(() => service.receiveToken(sessionId, OWNER_SCOPE)).toThrow(SETUP_TOKEN_TOKEN_UNAVAILABLE);
|
||||
// The secret write has not run, so the completion is unavailable.
|
||||
expect(() => service.completeSession(sessionId, OWNER_SCOPE)).toThrow(SETUP_TOKEN_TOKEN_UNAVAILABLE);
|
||||
});
|
||||
|
||||
it("purges the retained token when the retention window ends", async () => {
|
||||
it("reaches failed with no binding and no completion when the secret write errors", async () => {
|
||||
const store = new FakeStore();
|
||||
const completeCredential: SetupTokenSecretWriter = async () => {
|
||||
throw new Error("storage down");
|
||||
};
|
||||
const { service, processes, leases, logs } = buildService({ store, completeCredential });
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "code");
|
||||
|
||||
// The sink rejects with the fixed, non-secret storage error. It does not
|
||||
// swallow the failure.
|
||||
await expect(processes[0].surfaceCredential(SYNTH_TOKEN)).rejects.toMatchObject({
|
||||
message: SETUP_TOKEN_STORAGE_FAILED,
|
||||
});
|
||||
// Let the terminal-state handler run.
|
||||
processes[0].finish("failure");
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
|
||||
// The session failed: it holds no live session and released the lease.
|
||||
expect(service.activeSessionCount()).toBe(0);
|
||||
expect(leases.released).toEqual(["lease-1"]);
|
||||
// No stored-session claim exists: the durable row never reached stored.
|
||||
expect(store.rows.has(sessionId)).toBe(false);
|
||||
// The completion is gone, so a read returns the same not-found error.
|
||||
expect(() => service.completeSession(sessionId, OWNER_SCOPE)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
// No log line contains the token after the failure.
|
||||
expect(logs.join("\n")).not.toContain(SYNTH_TOKEN);
|
||||
});
|
||||
|
||||
it("purges the retained completion when the retention window ends", async () => {
|
||||
const { service, processes } = buildService({ tokenRetentionMs: 5 });
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "code");
|
||||
processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
await processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
processes[0].finish("success");
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
// The retention timer purged the token, so the session is gone.
|
||||
expect(() => service.receiveToken(sessionId, OWNER_SCOPE)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
// The retention timer dropped the in-memory session, so a read is not found.
|
||||
expect(() => service.completeSession(sessionId, OWNER_SCOPE)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
});
|
||||
});
|
||||
|
||||
// A deferred owner-bound secret writer. The test controls when the write
|
||||
// settles, so it can hold the write in flight and drive a cancel or an expiry
|
||||
// into the exact window that used to race the write. It records only the
|
||||
// non-secret ids and the token, and it counts how many times the writer started.
|
||||
function buildDeferredWriter(store: FakeStore) {
|
||||
const calls: RecordedSecretWrite[] = [];
|
||||
let started = 0;
|
||||
let pendingInput: { scope: SetupTokenSessionScope; sessionId: string } | null = null;
|
||||
let resolveWrite: (() => void) | null = null;
|
||||
let rejectWrite: ((error: Error) => void) | null = null;
|
||||
const writer: SetupTokenSecretWriter = (input) => {
|
||||
started += 1;
|
||||
pendingInput = { scope: input.scope, sessionId: input.sessionId };
|
||||
calls.push({ scope: input.scope, sessionId: input.sessionId, token: input.token });
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
resolveWrite = resolve;
|
||||
rejectWrite = reject;
|
||||
});
|
||||
};
|
||||
return {
|
||||
writer,
|
||||
calls,
|
||||
startedCount: () => started,
|
||||
// The commit resolves the write. It first simulates the atomic durable
|
||||
// `stored` transition the production writer runs inside the same transaction.
|
||||
resolve: async () => {
|
||||
if (pendingInput) {
|
||||
await store.markState(identityFromScope(pendingInput.scope, pendingInput.sessionId), "stored");
|
||||
}
|
||||
resolveWrite?.();
|
||||
},
|
||||
reject: () => rejectWrite?.(new Error("storage down")),
|
||||
};
|
||||
}
|
||||
|
||||
// Yields to the microtask queue, so a scheduled sink or lock step can run.
|
||||
const flush = () => new Promise((resolve) => setImmediate(resolve));
|
||||
|
||||
describe("SetupTokenSessionService terminal-race credential write", () => {
|
||||
for (const variant of [
|
||||
{ label: "cancel", run: (service: SetupTokenSessionService, sessionId: string) => service.cancel(sessionId, OWNER_SCOPE), terminalState: "cancelled" as const },
|
||||
{ label: "expiry", run: (service: SetupTokenSessionService, sessionId: string) => service.expire(sessionId, OWNER_SCOPE), terminalState: "timed_out" as const },
|
||||
]) {
|
||||
it(`writes no secret when a ${variant.label} wins the race, and leaves no stored claim`, async () => {
|
||||
const store = new FakeStore();
|
||||
const deferred = buildDeferredWriter(store);
|
||||
const { service, processes, leases } = buildService({ store, completeCredential: deferred.writer });
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "code");
|
||||
|
||||
// The terminal transition wins: it reaches the terminal state before the
|
||||
// credential arrives. The cleanup removes the durable row.
|
||||
await variant.run(service, sessionId);
|
||||
expect(store.rows.has(sessionId)).toBe(false);
|
||||
|
||||
// The credential arrives after the terminal transition. The sink fails
|
||||
// closed: it never calls the writer and it rejects with the fixed,
|
||||
// non-secret error.
|
||||
await expect(processes[0].surfaceCredential(SYNTH_TOKEN)).rejects.toMatchObject({
|
||||
message: SETUP_TOKEN_STORAGE_FAILED,
|
||||
});
|
||||
|
||||
// The writer never committed, so no unintended secret write happened.
|
||||
expect(deferred.startedCount()).toBe(0);
|
||||
expect(deferred.calls).toHaveLength(0);
|
||||
// The durable row holds no stored claim.
|
||||
expect(store.rows.has(sessionId)).toBe(false);
|
||||
// The completion has no success: a read returns the same not-found error.
|
||||
expect(() => service.completeSession(sessionId, OWNER_SCOPE)).toThrow(SETUP_TOKEN_SESSION_NOT_FOUND);
|
||||
// The session released its lease and holds no live session.
|
||||
expect(leases.released).toEqual(["lease-1"]);
|
||||
expect(service.activeSessionCount()).toBe(0);
|
||||
});
|
||||
|
||||
it(`does not erase the stored claim when the write wins the race, then a ${variant.label} arrives`, async () => {
|
||||
const store = new FakeStore();
|
||||
const deferred = buildDeferredWriter(store);
|
||||
const { service, processes, leases } = buildService({ store, completeCredential: deferred.writer });
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "code");
|
||||
|
||||
// Start the credential write. It is in flight and holds the session lock, so
|
||||
// a terminal transition that arrives now must wait for the write to settle.
|
||||
const credentialSettled = processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
await flush();
|
||||
expect(deferred.startedCount()).toBe(1);
|
||||
|
||||
// The terminal transition arrives while the write is in flight. It blocks on
|
||||
// the lock; it does not interleave with the write.
|
||||
const terminalSettled = variant.run(service, sessionId);
|
||||
await flush();
|
||||
|
||||
// The write wins the serialized ordering: it commits, then the service
|
||||
// records the non-secret markers and releases the lock.
|
||||
await deferred.resolve();
|
||||
await credentialSettled;
|
||||
await terminalSettled;
|
||||
|
||||
// The writer committed exactly once. No duplicate write is possible.
|
||||
expect(deferred.startedCount()).toBe(1);
|
||||
expect(deferred.calls).toEqual([{ scope: OWNER_SCOPE, sessionId, token: SYNTH_TOKEN }]);
|
||||
// The terminal transition did not erase the claim: the durable row stays
|
||||
// `stored` and the terminal API reports the completed state.
|
||||
expect(store.rows.get(sessionId)?.state).toBe("stored");
|
||||
const completion = service.completeSession(sessionId, OWNER_SCOPE);
|
||||
expect(completion.storedSessionId).toBe(sessionId);
|
||||
// The service released the sandbox lease at once.
|
||||
expect(leases.released).toEqual(["lease-1"]);
|
||||
});
|
||||
}
|
||||
|
||||
it("does not erase the claim when the process reports success after the write wins a cancel race", async () => {
|
||||
// This proves the natural success path stays intact when a cancel loses the
|
||||
// race: the write commits, the cancel completes the session, and the later
|
||||
// process-done success transition is an idempotent no-op.
|
||||
const store = new FakeStore();
|
||||
const deferred = buildDeferredWriter(store);
|
||||
const { service, processes } = buildService({ store, completeCredential: deferred.writer });
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "code");
|
||||
|
||||
const credentialSettled = processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
await flush();
|
||||
const cancelSettled = service.cancel(sessionId, OWNER_SCOPE);
|
||||
await flush();
|
||||
await deferred.resolve();
|
||||
await credentialSettled;
|
||||
await cancelSettled;
|
||||
|
||||
// The process reports success after the sink resolved. The transition finds a
|
||||
// completed, cleaned-up session and does nothing.
|
||||
processes[0].finish("success");
|
||||
await flush();
|
||||
|
||||
expect(deferred.startedCount()).toBe(1);
|
||||
expect(store.rows.get(sessionId)?.state).toBe("stored");
|
||||
expect(service.completeSession(sessionId, OWNER_SCOPE).storedSessionId).toBe(sessionId);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -394,7 +903,7 @@ describe("no secret reaches a sink (SR-1, SR-5)", () => {
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
service.submitCode(sessionId, OWNER_SCOPE, "SECRETCODE123");
|
||||
processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
await processes[0].surfaceCredential(SYNTH_TOKEN);
|
||||
const serialized = JSON.stringify([...store.rows.values()]);
|
||||
expect(serialized).not.toContain("SECRETCODE123");
|
||||
expect(serialized).not.toContain("STATEVALUE");
|
||||
@@ -545,3 +1054,255 @@ describe("confidential transport guard (SR-6, SR-7)", () => {
|
||||
expect(decision.allowed).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SetupTokenSessionService durable state scope", () => {
|
||||
it("marks the durable record state by the full owner scope", async () => {
|
||||
const store = new FakeStore();
|
||||
const { service, processes } = buildService({ store });
|
||||
const { sessionId } = await service.start(OWNER_SCOPE);
|
||||
processes[0].surfacePrompt(FULL_LOGIN_URL);
|
||||
await new Promise((resolve) => setImmediate(resolve));
|
||||
expect(store.rows.get(sessionId)?.state).toBe("awaiting_code");
|
||||
|
||||
// A foreign-scope mark leaves the row unchanged, so a write never updates a
|
||||
// row by the session id alone.
|
||||
await store.markState(
|
||||
{
|
||||
sessionId,
|
||||
companyId: OWNER_SCOPE.companyId,
|
||||
ownerUserId: "intruder",
|
||||
adapterType: OWNER_SCOPE.adapterType,
|
||||
environmentId: OWNER_SCOPE.environmentId,
|
||||
},
|
||||
"submitting",
|
||||
);
|
||||
expect(store.rows.get(sessionId)?.state).toBe("awaiting_code");
|
||||
});
|
||||
});
|
||||
|
||||
// --- The durable, database-backed cleanup store ------------------------------
|
||||
|
||||
const delay = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
const embeddedPostgresSupport = await getEmbeddedPostgresTestSupport();
|
||||
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
||||
|
||||
if (!embeddedPostgresSupport.supported) {
|
||||
console.warn(
|
||||
`Skipping durable setup-token cleanup store tests on this host: ${
|
||||
embeddedPostgresSupport.reason ?? "unsupported environment"
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
describeEmbeddedPostgres("durable setup-token cleanup store (embedded postgres)", () => {
|
||||
let stopDb: (() => Promise<void>) | undefined;
|
||||
let connectionString!: string;
|
||||
let db!: ReturnType<typeof createDb>;
|
||||
|
||||
beforeAll(async () => {
|
||||
const started = await startEmbeddedPostgresTestDatabase("claude-setup-token-store");
|
||||
stopDb = started.cleanup;
|
||||
connectionString = started.connectionString;
|
||||
db = createDb(connectionString);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await db.delete(claudeSetupTokenSessions);
|
||||
await db.delete(environments);
|
||||
await db.delete(companies);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await stopDb?.();
|
||||
});
|
||||
|
||||
// Seed a company and an environment, then return one fresh record identity.
|
||||
async function seedScope(): Promise<SetupTokenCleanupIdentity> {
|
||||
const companyId = randomUUID();
|
||||
const environmentId = randomUUID();
|
||||
await db.insert(companies).values({
|
||||
id: companyId,
|
||||
name: "Acme",
|
||||
status: "active",
|
||||
// A unique issue prefix per company; the column has a unique index.
|
||||
issuePrefix: companyId.slice(0, 8),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
await db.insert(environments).values({
|
||||
id: environmentId,
|
||||
name: `sandbox-${environmentId.slice(0, 8)}`,
|
||||
driver: "sandbox",
|
||||
status: "active",
|
||||
config: { provider: "fake" },
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
return {
|
||||
sessionId: randomUUID(),
|
||||
companyId,
|
||||
ownerUserId: `user-${randomUUID().slice(0, 8)}`,
|
||||
adapterType: "claude_local",
|
||||
environmentId,
|
||||
};
|
||||
}
|
||||
|
||||
async function insertRecord(
|
||||
identity: SetupTokenCleanupIdentity,
|
||||
state: SetupTokenCleanupRecord["state"],
|
||||
deadlineMs: number,
|
||||
): Promise<void> {
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
await store.record({
|
||||
...identity,
|
||||
leaseId: "lease-1",
|
||||
deadline: deadlineMs,
|
||||
state,
|
||||
boundAt: null,
|
||||
});
|
||||
}
|
||||
|
||||
async function readRow(sessionId: string) {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(claudeSetupTokenSessions)
|
||||
.where(eq(claudeSetupTokenSessions.sessionId, sessionId));
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
it("records the non-secret cleanup record and reads it back", async () => {
|
||||
const identity = await seedScope();
|
||||
await insertRecord(identity, "starting", Date.now() + 60_000);
|
||||
const row = await readRow(identity.sessionId);
|
||||
expect(row?.companyId).toBe(identity.companyId);
|
||||
expect(row?.ownerUserId).toBe(identity.ownerUserId);
|
||||
expect(row?.adapterType).toBe(identity.adapterType);
|
||||
expect(row?.environmentId).toBe(identity.environmentId);
|
||||
expect(row?.state).toBe("starting");
|
||||
expect(row?.boundAt).toBeNull();
|
||||
});
|
||||
|
||||
it("consumes a stored claim once with one conditional write", async () => {
|
||||
const identity = await seedScope();
|
||||
await insertRecord(identity, "stored", Date.now() + 60_000);
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
|
||||
const first = await store.consumeStoredClaim(identity);
|
||||
expect(first).not.toBeNull();
|
||||
expect(first?.sessionId).toBe(identity.sessionId);
|
||||
expect(first?.boundAt).not.toBeNull();
|
||||
expect((await readRow(identity.sessionId))?.boundAt).not.toBeNull();
|
||||
|
||||
// A second consume finds the claim already consumed and returns no row.
|
||||
const second = await store.consumeStoredClaim(identity);
|
||||
expect(second).toBeNull();
|
||||
});
|
||||
|
||||
it("returns no row for a foreign-scope consume and leaves the claim unconsumed", async () => {
|
||||
const identity = await seedScope();
|
||||
await insertRecord(identity, "stored", Date.now() + 60_000);
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
|
||||
const foreign = await store.consumeStoredClaim({ ...identity, ownerUserId: "intruder" });
|
||||
expect(foreign).toBeNull();
|
||||
expect((await readRow(identity.sessionId))?.boundAt).toBeNull();
|
||||
});
|
||||
|
||||
it("returns no row for an expired claim", async () => {
|
||||
const identity = await seedScope();
|
||||
await insertRecord(identity, "stored", Date.now() - 1_000);
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
|
||||
const consumed = await store.consumeStoredClaim(identity);
|
||||
expect(consumed).toBeNull();
|
||||
expect((await readRow(identity.sessionId))?.boundAt).toBeNull();
|
||||
});
|
||||
|
||||
it("returns no row for a non-stored claim", async () => {
|
||||
const identity = await seedScope();
|
||||
await insertRecord(identity, "submitting", Date.now() + 60_000);
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
|
||||
const consumed = await store.consumeStoredClaim(identity);
|
||||
expect(consumed).toBeNull();
|
||||
expect((await readRow(identity.sessionId))?.boundAt).toBeNull();
|
||||
});
|
||||
|
||||
it("lists terminal, expired, and consumed records but not a live stored claim", async () => {
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
const now = Date.now();
|
||||
|
||||
const terminal = await seedScope();
|
||||
await insertRecord(terminal, "failed", now + 60_000);
|
||||
|
||||
const expired = await seedScope();
|
||||
await insertRecord(expired, "awaiting_code", now - 1_000);
|
||||
|
||||
const consumed = await seedScope();
|
||||
await insertRecord(consumed, "stored", now + 60_000);
|
||||
await store.consumeStoredClaim(consumed);
|
||||
|
||||
const liveStored = await seedScope();
|
||||
await insertRecord(liveStored, "stored", now + 60_000);
|
||||
|
||||
const reapable = await store.listReapable(now);
|
||||
const ids = new Set(reapable.map((record) => record.sessionId));
|
||||
expect(ids.has(terminal.sessionId)).toBe(true);
|
||||
expect(ids.has(expired.sessionId)).toBe(true);
|
||||
expect(ids.has(consumed.sessionId)).toBe(true);
|
||||
// A live, unexpired, unconsumed stored claim is not reapable.
|
||||
expect(ids.has(liveStored.sessionId)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns no row when the claim row lock holds until after the deadline", async () => {
|
||||
// This test proves the consume uses `clock_timestamp()`, not
|
||||
// `transaction_timestamp()`. The row starts with a far-future deadline, so
|
||||
// the consume treats it as a valid candidate and waits for the row lock. A
|
||||
// second connection holds the lock, sets a near deadline inside the locked
|
||||
// transaction, and commits after that deadline passes. The consume then
|
||||
// re-checks the predicate against the committed row with the current
|
||||
// database time, so the expired claim returns no row. A
|
||||
// `transaction_timestamp()` predicate would use the stale start time and
|
||||
// wrongly consume the claim.
|
||||
const identity = await seedScope();
|
||||
await insertRecord(identity, "stored", Date.now() + 3_600_000);
|
||||
const store = createDbSetupTokenCleanupStore(db);
|
||||
|
||||
const lockDb = createDb(connectionString);
|
||||
let signalLocked!: () => void;
|
||||
const locked = new Promise<void>((resolve) => {
|
||||
signalLocked = resolve;
|
||||
});
|
||||
let releaseGate!: () => void;
|
||||
const gate = new Promise<void>((resolve) => {
|
||||
releaseGate = resolve;
|
||||
});
|
||||
const lockHeld = lockDb.transaction(async (tx) => {
|
||||
await tx.execute(
|
||||
sql`SELECT bound_at FROM claude_setup_token_sessions WHERE session_id = ${identity.sessionId} FOR UPDATE`,
|
||||
);
|
||||
// Set a near deadline inside the locked transaction. The consume cannot see
|
||||
// this value until the transaction commits.
|
||||
await tx.execute(
|
||||
sql`UPDATE claude_setup_token_sessions SET deadline_at = clock_timestamp() + interval '300 milliseconds' WHERE session_id = ${identity.sessionId}`,
|
||||
);
|
||||
signalLocked();
|
||||
await gate;
|
||||
});
|
||||
|
||||
// Wait until the lock is truly held, then fire the consume. The consume
|
||||
// blocks on the row lock; its initial scan sees the far-future deadline.
|
||||
await locked;
|
||||
const consumePromise = store.consumeStoredClaim(identity);
|
||||
// Hold the lock until the near deadline passes, then commit.
|
||||
await delay(600);
|
||||
releaseGate();
|
||||
await lockHeld;
|
||||
|
||||
const consumed = await consumePromise;
|
||||
expect(consumed).toBeNull();
|
||||
expect((await readRow(identity.sessionId))?.boundAt).toBeNull();
|
||||
await lockDb.$client.end();
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,892 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
buildClaudeOAuthWriteInput,
|
||||
buildSetupTokenLoginTransport,
|
||||
createProductionSetupTokenSandboxProvider,
|
||||
createSetupTokenSecretWriter,
|
||||
createWorkerBoundSetupTokenPtyOpener,
|
||||
type SetupTokenSandboxProvider,
|
||||
} from "./setup-token-transport-binding.js";
|
||||
import {
|
||||
SetupTokenSessionService,
|
||||
isTerminalSessionState,
|
||||
type SetupTokenCleanupIdentity,
|
||||
type SetupTokenCleanupRecord,
|
||||
type SetupTokenCleanupStore,
|
||||
type SetupTokenLoginProcess,
|
||||
type SetupTokenSessionScope,
|
||||
type SetupTokenSessionState,
|
||||
} from "./setup-token-session.js";
|
||||
import type { SetupTokenPtySessionOpener } from "@paperclipai/adapter-utils/setup-token-transport";
|
||||
import {
|
||||
CLAUDE_SETUP_TOKEN_COMMAND,
|
||||
SETUP_TOKEN_AFTER_ANCHOR,
|
||||
SETUP_TOKEN_BEFORE_ANCHOR,
|
||||
} from "@paperclipai/adapter-claude-local/server";
|
||||
|
||||
// The owner scope for one login session. The per-owner session cap is one, so one
|
||||
// scope holds one live session.
|
||||
const SCOPE: SetupTokenSessionScope = {
|
||||
companyId: "company-1",
|
||||
ownerUserId: "user-1",
|
||||
targetAgentId: null,
|
||||
adapterType: "claude_local",
|
||||
environmentId: "env-1",
|
||||
};
|
||||
|
||||
/** Waits for the pending microtasks and macrotasks to settle. */
|
||||
function flush(): Promise<void> {
|
||||
return new Promise((resolve) => setImmediate(resolve));
|
||||
}
|
||||
|
||||
/**
|
||||
* A controllable fake sandbox. It records the opened command and every released
|
||||
* lease id. It drives the login process to a terminal outcome through the exit
|
||||
* promise, so a test simulates a process failure, a timeout, or a cancel.
|
||||
*/
|
||||
function createFakeSandbox() {
|
||||
const openedCommands: string[] = [];
|
||||
const releasedLeaseIds: string[] = [];
|
||||
let acquireCount = 0;
|
||||
let killed = false;
|
||||
let resolveExit: (value: { exitCode: number | null }) => void = () => {};
|
||||
const exit = new Promise<{ exitCode: number | null }>((resolve) => {
|
||||
resolveExit = resolve;
|
||||
});
|
||||
|
||||
const openPtySession: SetupTokenPtySessionOpener = async (command) => {
|
||||
openedCommands.push(command);
|
||||
return {
|
||||
onData(): void {},
|
||||
write(): void {},
|
||||
wait: () => exit,
|
||||
kill(): void {
|
||||
killed = true;
|
||||
resolveExit({ exitCode: 137 });
|
||||
},
|
||||
async close(): Promise<void> {},
|
||||
};
|
||||
};
|
||||
|
||||
const provider: SetupTokenSandboxProvider = {
|
||||
async acquire() {
|
||||
acquireCount += 1;
|
||||
return { leaseId: `lease-${acquireCount}`, openPtySession };
|
||||
},
|
||||
async release(leaseId) {
|
||||
releasedLeaseIds.push(leaseId);
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
provider,
|
||||
openedCommands,
|
||||
releasedLeaseIds,
|
||||
get acquireCount() {
|
||||
return acquireCount;
|
||||
},
|
||||
get killed() {
|
||||
return killed;
|
||||
},
|
||||
finishProcess(exitCode: number | null): void {
|
||||
resolveExit({ exitCode });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An in-memory cleanup store that records every write. It stands in for the
|
||||
* durable database-backed store, so a test asserts the service runs its cleanup
|
||||
* against the injected store.
|
||||
*/
|
||||
function createRecordingStore() {
|
||||
const rows = new Map<string, SetupTokenCleanupRecord>();
|
||||
const removed: string[] = [];
|
||||
const stateWrites: Array<{ sessionId: string; state: SetupTokenSessionState }> = [];
|
||||
let reapable: SetupTokenCleanupRecord[] = [];
|
||||
|
||||
const store: SetupTokenCleanupStore = {
|
||||
async record(record) {
|
||||
rows.set(record.sessionId, { ...record });
|
||||
},
|
||||
async markState(identity: SetupTokenCleanupIdentity, state) {
|
||||
stateWrites.push({ sessionId: identity.sessionId, state });
|
||||
const row = rows.get(identity.sessionId);
|
||||
if (row) row.state = state;
|
||||
},
|
||||
async remove(identity) {
|
||||
removed.push(identity.sessionId);
|
||||
rows.delete(identity.sessionId);
|
||||
},
|
||||
async listReapable() {
|
||||
return reapable;
|
||||
},
|
||||
async consumeStoredClaim() {
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
store,
|
||||
rows,
|
||||
removed,
|
||||
stateWrites,
|
||||
setReapable(records: SetupTokenCleanupRecord[]): void {
|
||||
reapable = records;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A permissive rate limiter, so the start path never rate-limits in a test. */
|
||||
const allowAllRateLimiter = { consume: () => ({ allowed: true, retryAfterSeconds: 0 }) };
|
||||
|
||||
function buildService(input: {
|
||||
sandbox: SetupTokenSandboxProvider;
|
||||
store: SetupTokenCleanupStore;
|
||||
ttlMs?: number;
|
||||
}) {
|
||||
const transport = buildSetupTokenLoginTransport({
|
||||
sandbox: input.sandbox,
|
||||
store: input.store,
|
||||
});
|
||||
const service = new SetupTokenSessionService({
|
||||
factory: transport.factory,
|
||||
leases: transport.leases,
|
||||
store: transport.store,
|
||||
// The secret write stays a fail-closed default here. This phase does not bind
|
||||
// the owner-bound secret writer.
|
||||
completeCredential: async () => {
|
||||
throw new Error("The credential write is not bound in this phase.");
|
||||
},
|
||||
rateLimiter: allowAllRateLimiter,
|
||||
ttlMs: input.ttlMs ?? 5 * 60_000,
|
||||
});
|
||||
return { service, transport };
|
||||
}
|
||||
|
||||
describe("setup-token production transport binding", () => {
|
||||
it("creates a live session with the transport bound and opens only the fixed command", async () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
const { service } = buildService({ sandbox: sandbox.provider, store: store.store });
|
||||
|
||||
const started = await service.start(SCOPE);
|
||||
await flush();
|
||||
|
||||
// The start returned a session id instead of the fixed 503.
|
||||
expect(typeof started.sessionId).toBe("string");
|
||||
expect(started.sessionId.length).toBeGreaterThan(0);
|
||||
// The lease manager acquired one production lease.
|
||||
expect(sandbox.acquireCount).toBe(1);
|
||||
// The factory opened the pseudo-terminal with only the fixed command.
|
||||
expect(sandbox.openedCommands).toEqual([CLAUDE_SETUP_TOKEN_COMMAND]);
|
||||
// The service persisted the non-secret cleanup record at start.
|
||||
expect(store.rows.get(started.sessionId)?.leaseId).toBe("lease-1");
|
||||
|
||||
await service.shutdown();
|
||||
});
|
||||
|
||||
it("releases the lease and cleans the durable store on a process failure", async () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
const { service } = buildService({ sandbox: sandbox.provider, store: store.store });
|
||||
|
||||
const started = await service.start(SCOPE);
|
||||
await flush();
|
||||
|
||||
// The login process ends with a non-zero exit code.
|
||||
sandbox.finishProcess(1);
|
||||
await flush();
|
||||
await flush();
|
||||
|
||||
expect(sandbox.releasedLeaseIds).toEqual(["lease-1"]);
|
||||
expect(store.removed).toContain(started.sessionId);
|
||||
expect(store.rows.has(started.sessionId)).toBe(false);
|
||||
});
|
||||
|
||||
it("releases the lease and cleans the durable store on a cancel", async () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
const { service } = buildService({ sandbox: sandbox.provider, store: store.store });
|
||||
|
||||
const started = await service.start(SCOPE);
|
||||
await flush();
|
||||
|
||||
await service.cancel(started.sessionId, SCOPE);
|
||||
await flush();
|
||||
|
||||
expect(sandbox.releasedLeaseIds).toEqual(["lease-1"]);
|
||||
expect(sandbox.killed).toBe(true);
|
||||
expect(store.removed).toContain(started.sessionId);
|
||||
});
|
||||
|
||||
it("releases the lease and cleans the durable store on a timeout expiry", async () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
const { service } = buildService({ sandbox: sandbox.provider, store: store.store });
|
||||
|
||||
const started = await service.start(SCOPE);
|
||||
await flush();
|
||||
|
||||
await service.expire(started.sessionId, SCOPE);
|
||||
await flush();
|
||||
|
||||
expect(sandbox.releasedLeaseIds).toEqual(["lease-1"]);
|
||||
expect(store.removed).toContain(started.sessionId);
|
||||
});
|
||||
|
||||
it("cancels every live session and releases the lease on shutdown", async () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
const { service } = buildService({ sandbox: sandbox.provider, store: store.store });
|
||||
|
||||
const started = await service.start(SCOPE);
|
||||
await flush();
|
||||
|
||||
await service.shutdown();
|
||||
await flush();
|
||||
|
||||
expect(sandbox.releasedLeaseIds).toEqual(["lease-1"]);
|
||||
expect(store.removed).toContain(started.sessionId);
|
||||
expect(service.activeSessionCount()).toBe(0);
|
||||
});
|
||||
|
||||
it("ignores a runtime-supplied command and always opens the fixed command", async () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
// A caller forces an alternate command through the dependency object. The
|
||||
// dependency type carries no command field, so this cast simulates a
|
||||
// runtime-supplied value from a route, a body, or a configuration. The
|
||||
// binding must ignore it and open only the fixed command.
|
||||
const deps = {
|
||||
sandbox: sandbox.provider,
|
||||
store: store.store,
|
||||
command: "echo pwned",
|
||||
} as unknown as Parameters<typeof buildSetupTokenLoginTransport>[0];
|
||||
const transport = buildSetupTokenLoginTransport(deps);
|
||||
const service = new SetupTokenSessionService({
|
||||
factory: transport.factory,
|
||||
leases: transport.leases,
|
||||
store: transport.store,
|
||||
completeCredential: async () => {
|
||||
throw new Error("The credential write is not bound in this phase.");
|
||||
},
|
||||
rateLimiter: allowAllRateLimiter,
|
||||
ttlMs: 5 * 60_000,
|
||||
});
|
||||
|
||||
const started = await service.start(SCOPE);
|
||||
await flush();
|
||||
|
||||
// The factory opened the pseudo-terminal with only the fixed command. The
|
||||
// supplied alternate value never reached the command.
|
||||
expect(sandbox.openedCommands).toEqual([CLAUDE_SETUP_TOKEN_COMMAND]);
|
||||
expect(sandbox.openedCommands).not.toContain("echo pwned");
|
||||
|
||||
void started;
|
||||
await service.shutdown();
|
||||
});
|
||||
|
||||
it("keeps the durable cleanup record reapable when a restart release fails", async () => {
|
||||
// The production provider drives a restart release by id. The provider driver
|
||||
// release rejects, so the reaper must keep the durable cleanup record.
|
||||
const releaseRunLease = vi.fn(async () => {
|
||||
throw new Error("remote release failed");
|
||||
});
|
||||
const provider = createProductionSetupTokenSandboxProvider({
|
||||
environments: {
|
||||
getById: async () => ({ id: "env-1", driver: "sandbox", status: "active" }) as never,
|
||||
getLeaseById: async () => ({ id: "lease-orphan", environmentId: "env-1" }) as never,
|
||||
releaseLease: async () => ({}) as never,
|
||||
},
|
||||
environmentRuntime: {
|
||||
acquireRunLease: vi.fn(),
|
||||
getDriver: () => ({ releaseRunLease }) as never,
|
||||
},
|
||||
openLivePtySession: async () => {
|
||||
throw new Error("must not open a pty for a restart release");
|
||||
},
|
||||
});
|
||||
const store = createRecordingStore();
|
||||
const { service } = buildService({ sandbox: provider, store: store.store });
|
||||
|
||||
// A crash left a durable record whose lease the reaper must free. The
|
||||
// in-memory lease map is empty, as after a restart.
|
||||
const orphan: SetupTokenCleanupRecord = {
|
||||
sessionId: "orphan-session",
|
||||
companyId: SCOPE.companyId,
|
||||
ownerUserId: SCOPE.ownerUserId,
|
||||
adapterType: SCOPE.adapterType,
|
||||
environmentId: SCOPE.environmentId,
|
||||
leaseId: "lease-orphan",
|
||||
deadline: 0,
|
||||
state: "failed",
|
||||
boundAt: null,
|
||||
};
|
||||
store.rows.set(orphan.sessionId, { ...orphan });
|
||||
store.setReapable([orphan]);
|
||||
|
||||
const result = await service.reap(Date.now());
|
||||
|
||||
// The remote release ran and failed, so the reaper counts a failure and keeps
|
||||
// the durable cleanup record for a later retry.
|
||||
expect(releaseRunLease).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual({ released: 0, failed: 1 });
|
||||
expect(store.removed).not.toContain("orphan-session");
|
||||
expect(store.rows.has("orphan-session")).toBe(true);
|
||||
});
|
||||
|
||||
it("releases a leftover lease by id on a restart reap", async () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
const { service } = buildService({ sandbox: sandbox.provider, store: store.store });
|
||||
|
||||
// A crash left a durable record whose lease the reaper must free after a
|
||||
// restart. No live session references it.
|
||||
const orphan: SetupTokenCleanupRecord = {
|
||||
sessionId: "orphan-session",
|
||||
companyId: SCOPE.companyId,
|
||||
ownerUserId: SCOPE.ownerUserId,
|
||||
adapterType: SCOPE.adapterType,
|
||||
environmentId: SCOPE.environmentId,
|
||||
leaseId: "lease-orphan",
|
||||
deadline: 0,
|
||||
state: "failed",
|
||||
boundAt: null,
|
||||
};
|
||||
store.setReapable([orphan]);
|
||||
|
||||
const result = await service.reap(Date.now());
|
||||
|
||||
expect(result).toEqual({ released: 1, failed: 0 });
|
||||
expect(sandbox.releasedLeaseIds).toEqual(["lease-orphan"]);
|
||||
expect(store.removed).toContain("orphan-session");
|
||||
});
|
||||
});
|
||||
|
||||
describe("production sandbox provider", () => {
|
||||
it("fails closed before it acquires a lease when the live opener is not bound", async () => {
|
||||
const acquireRunLease = vi.fn();
|
||||
const provider = createProductionSetupTokenSandboxProvider({
|
||||
environments: {
|
||||
getById: async () => ({ id: "env-1", driver: "sandbox", status: "active" }) as never,
|
||||
getLeaseById: async () => null,
|
||||
releaseLease: async () => ({}) as never,
|
||||
},
|
||||
environmentRuntime: {
|
||||
acquireRunLease,
|
||||
getDriver: () => null,
|
||||
},
|
||||
// No `openLivePtySession`: the live path lands with the
|
||||
// characterization test.
|
||||
});
|
||||
|
||||
await expect(provider.acquire({ scope: SCOPE, deadline: Date.now() + 1000 })).rejects.toMatchObject({
|
||||
status: 503,
|
||||
});
|
||||
// The provider held no lease, so it never acquired one.
|
||||
expect(acquireRunLease).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails closed for a local environment", async () => {
|
||||
const provider = createProductionSetupTokenSandboxProvider({
|
||||
environments: {
|
||||
getById: async () => ({ id: "env-1", driver: "local", status: "active" }) as never,
|
||||
getLeaseById: async () => null,
|
||||
releaseLease: async () => ({}) as never,
|
||||
},
|
||||
environmentRuntime: {
|
||||
acquireRunLease: vi.fn(),
|
||||
getDriver: () => null,
|
||||
},
|
||||
openLivePtySession: async () => {
|
||||
throw new Error("must not reach the opener for a local environment");
|
||||
},
|
||||
});
|
||||
|
||||
await expect(provider.acquire({ scope: SCOPE, deadline: Date.now() + 1000 })).rejects.toMatchObject({
|
||||
status: 503,
|
||||
});
|
||||
});
|
||||
|
||||
it("acquires a lease and binds the live opener when it is present", async () => {
|
||||
const openPtySession: SetupTokenPtySessionOpener = async () => ({
|
||||
onData(): void {},
|
||||
write(): void {},
|
||||
wait: async () => ({ exitCode: 0 }),
|
||||
kill(): void {},
|
||||
async close(): Promise<void> {},
|
||||
});
|
||||
const releaseRunLease = vi.fn(async () => null);
|
||||
const deadline = Date.now() + 1000;
|
||||
const provider = createProductionSetupTokenSandboxProvider({
|
||||
environments: {
|
||||
getById: async () => ({ id: "env-1", name: "Sandbox", driver: "sandbox", status: "active" }) as never,
|
||||
getLeaseById: async () => null,
|
||||
releaseLease: async () => ({}) as never,
|
||||
},
|
||||
environmentRuntime: {
|
||||
acquireRunLease: async () =>
|
||||
({
|
||||
environment: { id: "env-1", driver: "sandbox" },
|
||||
// The runtime bounds the lease expiry to the session deadline.
|
||||
lease: { id: "lease-live", expiresAt: new Date(deadline - 1) },
|
||||
leaseContext: {},
|
||||
}) as never,
|
||||
getDriver: () => ({ releaseRunLease }) as never,
|
||||
},
|
||||
openLivePtySession: async () => openPtySession,
|
||||
});
|
||||
|
||||
const acquired = await provider.acquire({ scope: SCOPE, deadline });
|
||||
expect(acquired.leaseId).toBe("lease-live");
|
||||
|
||||
await provider.release(acquired.leaseId);
|
||||
expect(releaseRunLease).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("forwards the session deadline and records a lease expiry at or before it", async () => {
|
||||
const openPtySession: SetupTokenPtySessionOpener = async () => ({
|
||||
onData(): void {},
|
||||
write(): void {},
|
||||
wait: async () => ({ exitCode: 0 }),
|
||||
kill(): void {},
|
||||
async close(): Promise<void> {},
|
||||
});
|
||||
const releaseRunLease = vi.fn(async () => null);
|
||||
const deadline = Date.now() + 60_000;
|
||||
let forwardedExpiresAt: Date | null | undefined;
|
||||
const provider = createProductionSetupTokenSandboxProvider({
|
||||
environments: {
|
||||
getById: async () => ({ id: "env-1", name: "Sandbox", driver: "sandbox", status: "active" }) as never,
|
||||
getLeaseById: async () => null,
|
||||
releaseLease: async () => ({}) as never,
|
||||
},
|
||||
environmentRuntime: {
|
||||
// The runtime records the provider-attested expiry. Here the provider
|
||||
// grants an expiry at or before the requested deadline.
|
||||
acquireRunLease: async (input: { requestedExpiresAt?: Date | null }) => {
|
||||
forwardedExpiresAt = input.requestedExpiresAt;
|
||||
const bounded = new Date(Math.min(deadline, deadline + 5_000));
|
||||
return {
|
||||
environment: { id: "env-1", driver: "sandbox" },
|
||||
lease: { id: "lease-live", expiresAt: bounded },
|
||||
leaseContext: {},
|
||||
} as never;
|
||||
},
|
||||
getDriver: () => ({ releaseRunLease }) as never,
|
||||
},
|
||||
openLivePtySession: async () => openPtySession,
|
||||
});
|
||||
|
||||
const acquired = await provider.acquire({ scope: SCOPE, deadline });
|
||||
|
||||
// The provider forwarded the session deadline to the runtime acquire.
|
||||
expect(forwardedExpiresAt).toBeInstanceOf(Date);
|
||||
expect((forwardedExpiresAt as Date).getTime()).toBe(deadline);
|
||||
expect(acquired.leaseId).toBe("lease-live");
|
||||
// The lease held no expiry after the deadline.
|
||||
expect(releaseRunLease).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("releases the remote lease and fails closed when the acquired expiry is absent, invalid, or later", async () => {
|
||||
const deadline = Date.now() + 60_000;
|
||||
const cases: Array<{ label: string; expiresAt: unknown }> = [
|
||||
{ label: "absent", expiresAt: null },
|
||||
{ label: "invalid", expiresAt: new Date("not-a-date") },
|
||||
{ label: "later", expiresAt: new Date(deadline + 1) },
|
||||
];
|
||||
|
||||
for (const testCase of cases) {
|
||||
const releaseRunLease = vi.fn(async () => null);
|
||||
const openLivePtySession = vi.fn(async () => {
|
||||
throw new Error("must not open a pty for an unbounded lease");
|
||||
});
|
||||
const provider = createProductionSetupTokenSandboxProvider({
|
||||
environments: {
|
||||
getById: async () => ({ id: "env-1", name: "Sandbox", driver: "sandbox", status: "active" }) as never,
|
||||
getLeaseById: async () => null,
|
||||
releaseLease: async () => ({}) as never,
|
||||
},
|
||||
environmentRuntime: {
|
||||
acquireRunLease: async () =>
|
||||
({
|
||||
environment: { id: "env-1", driver: "sandbox" },
|
||||
lease: { id: "lease-unbounded", expiresAt: testCase.expiresAt },
|
||||
leaseContext: {},
|
||||
}) as never,
|
||||
getDriver: () => ({ releaseRunLease }) as never,
|
||||
},
|
||||
openLivePtySession,
|
||||
});
|
||||
|
||||
await expect(provider.acquire({ scope: SCOPE, deadline })).rejects.toMatchObject({
|
||||
status: 503,
|
||||
});
|
||||
// The provider released the acquired remote lease through the driver.
|
||||
expect(releaseRunLease, testCase.label).toHaveBeenCalledTimes(1);
|
||||
// The provider failed closed before it opened the login pseudo-terminal.
|
||||
expect(openLivePtySession, testCase.label).not.toHaveBeenCalled();
|
||||
}
|
||||
});
|
||||
|
||||
it("runs the provider driver release by id for a fresh instance with an empty lease map", async () => {
|
||||
// A fresh provider instance holds no in-memory lease record, as after a
|
||||
// restart. The release must resolve the stored lease and its environment and
|
||||
// run the provider driver release, not only the database row write.
|
||||
const releaseRunLease = vi.fn(async () => null);
|
||||
const releaseLease = vi.fn(async () => ({}) as never);
|
||||
const getLeaseById = vi.fn(async () => ({ id: "lease-restart", environmentId: "env-1" }) as never);
|
||||
const getById = vi.fn(async () => ({ id: "env-1", driver: "sandbox", status: "active" }) as never);
|
||||
const provider = createProductionSetupTokenSandboxProvider({
|
||||
environments: { getById, getLeaseById, releaseLease },
|
||||
environmentRuntime: {
|
||||
acquireRunLease: vi.fn(),
|
||||
getDriver: () => ({ releaseRunLease }) as never,
|
||||
},
|
||||
openLivePtySession: async () => {
|
||||
throw new Error("must not open a pty for a restart release");
|
||||
},
|
||||
});
|
||||
|
||||
await provider.release("lease-restart");
|
||||
|
||||
// The provider resolved the lease by id and released through the driver.
|
||||
expect(getLeaseById).toHaveBeenCalledWith("lease-restart");
|
||||
expect(releaseRunLease).toHaveBeenCalledTimes(1);
|
||||
// The provider tore down the remote sandbox through the driver, not only the
|
||||
// database lease row.
|
||||
expect(releaseLease).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("worker-bound live pseudo-terminal opener", () => {
|
||||
it("resolves the lease to its provider lease id and drives the worker route gate", async () => {
|
||||
const session = {
|
||||
onData: () => {},
|
||||
write: () => {},
|
||||
wait: async () => ({ exitCode: 0 }),
|
||||
kill: () => {},
|
||||
close: async () => {},
|
||||
};
|
||||
const openSetupTokenPtySession = vi.fn(async () => session);
|
||||
const getLeaseById = vi.fn(async () => ({
|
||||
providerLeaseId: "provider-lease-9",
|
||||
metadata: { pluginId: "paperclip.daytona", provider: "daytona" },
|
||||
}));
|
||||
|
||||
const openLivePtySession = createWorkerBoundSetupTokenPtyOpener({
|
||||
workerManager: { openSetupTokenPtySession },
|
||||
environments: { getLeaseById },
|
||||
});
|
||||
|
||||
const opener = await openLivePtySession({
|
||||
scope: SCOPE,
|
||||
environmentId: "env-1",
|
||||
leaseId: "lease-42",
|
||||
});
|
||||
// The opener resolved the server lease id to the provider lease id.
|
||||
expect(getLeaseById).toHaveBeenCalledWith("lease-42");
|
||||
|
||||
const opened = await opener("claude setup-token");
|
||||
// The opener drove the manager route gate with the resolved worker target and
|
||||
// the fixed command. The manager mints the host route id, so the opener passes
|
||||
// none.
|
||||
expect(openSetupTokenPtySession).toHaveBeenCalledWith("paperclip.daytona", {
|
||||
driverKey: "daytona",
|
||||
companyId: "company-1",
|
||||
environmentId: "env-1",
|
||||
providerLeaseId: "provider-lease-9",
|
||||
command: "claude setup-token",
|
||||
});
|
||||
expect(opened).toBe(session);
|
||||
});
|
||||
|
||||
it("fails closed when the lease carries no sandbox worker binding", async () => {
|
||||
const openSetupTokenPtySession = vi.fn();
|
||||
const openLivePtySession = createWorkerBoundSetupTokenPtyOpener({
|
||||
workerManager: { openSetupTokenPtySession },
|
||||
// The lease resolves but carries no provider lease id or plugin id.
|
||||
environments: {
|
||||
getLeaseById: async () => ({ providerLeaseId: null, metadata: {} }),
|
||||
},
|
||||
});
|
||||
|
||||
await expect(
|
||||
openLivePtySession({ scope: SCOPE, environmentId: "env-1", leaseId: "lease-42" }),
|
||||
).rejects.toMatchObject({ status: 503 });
|
||||
// The opener never reached the worker manager.
|
||||
expect(openSetupTokenPtySession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fails closed when the lease is missing", async () => {
|
||||
const openLivePtySession = createWorkerBoundSetupTokenPtyOpener({
|
||||
workerManager: { openSetupTokenPtySession: vi.fn() },
|
||||
environments: { getLeaseById: async () => null },
|
||||
});
|
||||
|
||||
await expect(
|
||||
openLivePtySession({ scope: SCOPE, environmentId: "env-1", leaseId: "missing" }),
|
||||
).rejects.toMatchObject({ status: 503 });
|
||||
});
|
||||
});
|
||||
|
||||
// The fixed, closed set of diagnostic lines the login runner emits. The runner
|
||||
// is the sole producer. Every line is an approved non-secret literal. The
|
||||
// application binding forwards each line verbatim, so a captured line must be a
|
||||
// member of this set.
|
||||
const DIAGNOSTIC_ALLOWLIST = new Set<string>([
|
||||
"[paperclip] Setup-token login: the process stop step errored.",
|
||||
"[paperclip] Setup-token login: the driver dispose step errored.",
|
||||
"[paperclip] Setup-token login: the code input step errored.",
|
||||
"[paperclip] Setup-token login: sent the browser code to the prompt.",
|
||||
"[paperclip] Setup-token login: delivered the credential to the sink.",
|
||||
"[paperclip] Setup-token login: the credential delivery step errored.",
|
||||
"[paperclip] Setup-token login cancelled before start.",
|
||||
"[paperclip] Setup-token login timed out; stopping the process.",
|
||||
"[paperclip] Setup-token login cancelled; stopping the process.",
|
||||
"[paperclip] Setup-token login command ended with a non-zero exit code.",
|
||||
"[paperclip] Setup-token login: the credential did not land; treating the run as a failure.",
|
||||
"[paperclip] Setup-token login: surfaced the sign-in prompt.",
|
||||
"[paperclip] Setup-token login command ended successfully.",
|
||||
]);
|
||||
|
||||
// Synthetic sentinels. No real secret is present. The tests assert that no
|
||||
// captured diagnostic line carries any of these values, so the binding never
|
||||
// leaks the token, the browser code, or the authorization URL to a log.
|
||||
const TOKEN_SENTINEL = "sk-ant-oat01-SENTINELTOKENAAAAAAAAAAAA";
|
||||
const BROWSER_CODE_SENTINEL = "SENTINEL-BROWSER-CODE";
|
||||
const URL_CLIENT_SENTINEL = "sentinelurlclient";
|
||||
// Each query value satisfies the shape contract the parser enforces: a 43-to-128
|
||||
// character `code_challenge`, a 16-to-256 character `state`, and the one pinned
|
||||
// `redirect_uri`. The `client_id` stays the sentinel, so the leak checks still
|
||||
// prove the client id never reaches a log line.
|
||||
const AUTHORIZATION_URL_SENTINEL =
|
||||
"https://claude.com/cai/oauth/authorize" +
|
||||
`?client_id=${URL_CLIENT_SENTINEL}` +
|
||||
"&code=redacted" +
|
||||
"&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM" +
|
||||
"&code_challenge_method=S256" +
|
||||
"&redirect_uri=https%3A%2F%2Fplatform.claude.com%2Foauth%2Fcode%2Fcallback" +
|
||||
"&response_type=code" +
|
||||
"&scope=user%3Ainference" +
|
||||
"&state=Xy7Kd2Pq9Rn4Vb8Lf1Mw6Zc3Hj0Tg5Us";
|
||||
|
||||
// A complete sign-in prompt block. The parser binds the authorization URL to
|
||||
// the preamble and the prompt line after it.
|
||||
const PROMPT_OUTPUT = [
|
||||
"Browser didn’t open? Use the url below to sign in (c to copy)",
|
||||
AUTHORIZATION_URL_SENTINEL,
|
||||
"Paste code here if prompted >",
|
||||
].join("\n");
|
||||
|
||||
// A complete success record. The parser binds the token between the two
|
||||
// anchors.
|
||||
const CREDENTIAL_OUTPUT = [
|
||||
"✓ Long-lived authentication token created successfully!",
|
||||
"",
|
||||
SETUP_TOKEN_BEFORE_ANCHOR,
|
||||
"",
|
||||
TOKEN_SENTINEL,
|
||||
"",
|
||||
SETUP_TOKEN_AFTER_ANCHOR,
|
||||
].join("\n");
|
||||
|
||||
/**
|
||||
* A fake sandbox that streams controllable output. The test emits the prompt
|
||||
* block and the credential block on demand, and it ends the login process with
|
||||
* a chosen exit code. The runner drives its full diagnostic path over this
|
||||
* fake, so the test captures every line the binding forwards.
|
||||
*/
|
||||
function createStreamingSandbox() {
|
||||
let listener: ((chunk: string) => void) | null = null;
|
||||
let resolveExit: (value: { exitCode: number | null }) => void = () => {};
|
||||
const exit = new Promise<{ exitCode: number | null }>((resolve) => {
|
||||
resolveExit = resolve;
|
||||
});
|
||||
const writes: string[] = [];
|
||||
|
||||
const openPtySession: SetupTokenPtySessionOpener = async () => ({
|
||||
onData(next): void {
|
||||
listener = next;
|
||||
},
|
||||
write(data): void {
|
||||
writes.push(data);
|
||||
},
|
||||
wait: () => exit,
|
||||
kill(): void {
|
||||
resolveExit({ exitCode: 137 });
|
||||
},
|
||||
async close(): Promise<void> {},
|
||||
});
|
||||
|
||||
const provider: SetupTokenSandboxProvider = {
|
||||
async acquire() {
|
||||
return { leaseId: "lease-1", openPtySession };
|
||||
},
|
||||
async release() {},
|
||||
};
|
||||
|
||||
return {
|
||||
provider,
|
||||
emit(chunk: string): void {
|
||||
listener?.(chunk);
|
||||
},
|
||||
finishProcess(exitCode: number | null): void {
|
||||
resolveExit({ exitCode });
|
||||
},
|
||||
get writes() {
|
||||
return writes;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** Waits `ms` milliseconds of real time, so the code-submit settle can pass. */
|
||||
function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* Drives one login run through the binding factory and returns every captured
|
||||
* diagnostic line. The caller controls the run through `drive`, so one call
|
||||
* exercises the success path and another exercises the failure path. The `drive`
|
||||
* callback receives the sandbox and the process handle.
|
||||
*/
|
||||
async function captureDiagnostics(
|
||||
drive: (
|
||||
sandbox: ReturnType<typeof createStreamingSandbox>,
|
||||
process: SetupTokenLoginProcess,
|
||||
) => Promise<void>,
|
||||
): Promise<string[]> {
|
||||
const captured: string[] = [];
|
||||
const sandbox = createStreamingSandbox();
|
||||
const store = createRecordingStore();
|
||||
const transport = buildSetupTokenLoginTransport({
|
||||
sandbox: sandbox.provider,
|
||||
store: store.store,
|
||||
log: (line) => captured.push(line),
|
||||
});
|
||||
|
||||
await transport.leases.acquire({ scope: SCOPE, deadline: Date.now() + 60_000 });
|
||||
const controller = new AbortController();
|
||||
const process = transport.factory({
|
||||
scope: SCOPE,
|
||||
onPrompt: () => {},
|
||||
onCredential: async () => {},
|
||||
timeoutMs: 60_000,
|
||||
signal: controller.signal,
|
||||
});
|
||||
// Let the transport open the session and register the output listener before
|
||||
// the driver emits any output.
|
||||
await flush();
|
||||
|
||||
await drive(sandbox, process);
|
||||
await process.done;
|
||||
await flush();
|
||||
return captured;
|
||||
}
|
||||
|
||||
/** Asserts a captured diagnostic set is allowlisted and carries no secret. */
|
||||
function expectSafeDiagnostics(captured: string[]): void {
|
||||
for (const line of captured) {
|
||||
expect(DIAGNOSTIC_ALLOWLIST.has(line), line).toBe(true);
|
||||
}
|
||||
const joined = captured.join("\n");
|
||||
expect(joined).not.toContain(TOKEN_SENTINEL);
|
||||
expect(joined).not.toContain(BROWSER_CODE_SENTINEL);
|
||||
expect(joined).not.toContain(AUTHORIZATION_URL_SENTINEL);
|
||||
expect(joined).not.toContain(URL_CLIENT_SENTINEL);
|
||||
}
|
||||
|
||||
describe("setup-token production transport binding diagnostics", () => {
|
||||
it("forwards only allowlisted, non-secret diagnostics on a success path", async () => {
|
||||
const captured = await captureDiagnostics(async (sandbox, process) => {
|
||||
// Surface the prompt. The runner logs the prompt line and starts the code
|
||||
// input step, which waits for the browser code.
|
||||
sandbox.emit(PROMPT_OUTPUT);
|
||||
await flush();
|
||||
// Submit the browser code. The runner writes the code, lets the paste
|
||||
// buffer settle, writes the Enter byte, and logs the code-sent line.
|
||||
process.submitCode(BROWSER_CODE_SENTINEL);
|
||||
await delay(250);
|
||||
// Deliver the token, then end the process cleanly. The runner binds the
|
||||
// token, delivers it to the sink, and logs the success line.
|
||||
sandbox.emit(CREDENTIAL_OUTPUT);
|
||||
sandbox.finishProcess(0);
|
||||
});
|
||||
|
||||
// The run reached the success lines, so the capture is not empty.
|
||||
expect(captured).toContain("[paperclip] Setup-token login: surfaced the sign-in prompt.");
|
||||
expect(captured).toContain(
|
||||
"[paperclip] Setup-token login: delivered the credential to the sink.",
|
||||
);
|
||||
expect(captured).toContain("[paperclip] Setup-token login command ended successfully.");
|
||||
// The runner sent the browser code, so the code-input path ran. The code
|
||||
// still never reaches a log line.
|
||||
expect(captured).toContain("[paperclip] Setup-token login: sent the browser code to the prompt.");
|
||||
expectSafeDiagnostics(captured);
|
||||
});
|
||||
|
||||
it("forwards only allowlisted, non-secret diagnostics on a failure path", async () => {
|
||||
const captured = await captureDiagnostics(async (sandbox, process) => {
|
||||
void process;
|
||||
// The login process ends with a non-zero exit code before any prompt.
|
||||
sandbox.finishProcess(1);
|
||||
});
|
||||
|
||||
expect(captured).toContain(
|
||||
"[paperclip] Setup-token login command ended with a non-zero exit code.",
|
||||
);
|
||||
expectSafeDiagnostics(captured);
|
||||
});
|
||||
});
|
||||
|
||||
// A compile-time guard: a terminal state helper stays importable for the tests.
|
||||
void isTerminalSessionState;
|
||||
|
||||
// A synthetic token. No real token is present. The tests assert the token never
|
||||
// reaches a log or an error message.
|
||||
const SYNTH_TOKEN = "sk-ant-oat01-SYNTHETICSYNTHETICSYNTHETIC01";
|
||||
|
||||
describe("buildClaudeOAuthWriteInput", () => {
|
||||
it("maps a scope with no overwrite to a first_write input", () => {
|
||||
const input = buildClaudeOAuthWriteInput(SCOPE, "session-7", SYNTH_TOKEN);
|
||||
expect(input).toEqual({ sessionId: "session-7", mode: "first_write", value: SYNTH_TOKEN });
|
||||
});
|
||||
|
||||
it("maps a scope with the overwrite capture to a confirmed_rotation input", () => {
|
||||
const overwriteScope: SetupTokenSessionScope = {
|
||||
...SCOPE,
|
||||
confirmedOverwrite: { expectedSecretId: "secret-9", expectedLatestVersion: 4 },
|
||||
};
|
||||
const input = buildClaudeOAuthWriteInput(overwriteScope, "session-8", SYNTH_TOKEN);
|
||||
expect(input).toEqual({
|
||||
sessionId: "session-8",
|
||||
mode: "confirmed_rotation",
|
||||
value: SYNTH_TOKEN,
|
||||
expectedSecretId: "secret-9",
|
||||
expectedLatestVersion: 4,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("createSetupTokenSecretWriter wiring", () => {
|
||||
it("carries the writer through the transport binding to the router", () => {
|
||||
const sandbox = createFakeSandbox();
|
||||
const store = createRecordingStore();
|
||||
// The writer never runs here; a stub db is enough to assert the binding
|
||||
// forwards the writer reference to the router.
|
||||
const writer = createSetupTokenSecretWriter({ db: {} as never });
|
||||
|
||||
const transport = buildSetupTokenLoginTransport({
|
||||
sandbox: sandbox.provider,
|
||||
store: store.store,
|
||||
completeCredential: writer,
|
||||
});
|
||||
|
||||
// The binding forwards the writer, so the router uses the real atomic write
|
||||
// instead of the deferred fail-closed default.
|
||||
expect(transport.completeCredential).toBe(writer);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,611 @@
|
||||
// The production transport binding for the Claude setup-token login session.
|
||||
//
|
||||
// The session service (`setup-token-session.ts`) drives the login flow over
|
||||
// three injected components: a lease manager, a login-process factory, and a
|
||||
// durable cleanup store. The router (`agents.ts`) accepts these components as an
|
||||
// optional `setupTokenLogin` transport and falls back to a deferred, fail-closed
|
||||
// default when a caller omits it. This module builds the production transport, so
|
||||
// `app.ts` binds it one time at startup.
|
||||
//
|
||||
// The binding passes only the fixed command `CLAUDE_SETUP_TOKEN_COMMAND` to the
|
||||
// login runner. It never accepts a command from a route, a request
|
||||
// body, or an adapter configuration.
|
||||
//
|
||||
// The live pseudo-terminal opener is a separate seam. The Daytona sandbox
|
||||
// provider runs as a plugin worker, so the server process does not hold the raw
|
||||
// sandbox process. The real opener binds inside the worker; this module accepts
|
||||
// it through `openLivePtySession`. A test injects a fake sandbox opener to drive
|
||||
// the full session path. The live opener lands with the characterization
|
||||
// test against a real sandbox.
|
||||
|
||||
import {
|
||||
SetupTokenSessionError,
|
||||
SETUP_TOKEN_START_FAILED,
|
||||
SETUP_TOKEN_STORAGE_FAILED,
|
||||
createDbSetupTokenCleanupStore,
|
||||
transitionSetupTokenSessionToStored,
|
||||
type SetupTokenCleanupStore,
|
||||
type SetupTokenLease,
|
||||
type SetupTokenLeaseManager,
|
||||
type SetupTokenLoginOutcome,
|
||||
type SetupTokenLoginProcess,
|
||||
type SetupTokenLoginProcessFactory,
|
||||
type SetupTokenSecretWriter,
|
||||
type SetupTokenSessionScope,
|
||||
} from "./setup-token-session.js";
|
||||
import { secretService } from "./secrets.js";
|
||||
import type { environmentService } from "./environments.js";
|
||||
import type { environmentRuntimeService } from "./environment-runtime.js";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import {
|
||||
createSetupTokenPtyTransport,
|
||||
type SetupTokenPtySession,
|
||||
type SetupTokenPtySessionOpener,
|
||||
} from "@paperclipai/adapter-utils/setup-token-transport";
|
||||
import {
|
||||
runSetupTokenLogin,
|
||||
CLAUDE_SETUP_TOKEN_COMMAND,
|
||||
} from "@paperclipai/adapter-claude-local/server";
|
||||
|
||||
/**
|
||||
* The sandbox provider for one login session. It acquires a fresh sandbox lease
|
||||
* and returns the lease id plus the pseudo-terminal opener bound to that lease's
|
||||
* process. It releases the lease by id. The production provider wraps the
|
||||
* environment runtime. A test injects a fake provider.
|
||||
*/
|
||||
export interface SetupTokenSandboxProvider {
|
||||
/**
|
||||
* Acquires a fresh sandbox lease for a login session. It returns the lease id
|
||||
* and the pseudo-terminal opener bound to the acquired lease's process. It
|
||||
* throws a fail-closed error when it cannot acquire the lease or bind the
|
||||
* opener; the caller then returns the fixed start error.
|
||||
*/
|
||||
acquire(input: {
|
||||
scope: SetupTokenSessionScope;
|
||||
deadline: number;
|
||||
}): Promise<{ leaseId: string; openPtySession: SetupTokenPtySessionOpener }>;
|
||||
/** Releases the lease by id. The service and the startup reaper call it. */
|
||||
release(leaseId: string): Promise<void>;
|
||||
}
|
||||
|
||||
/** The dependencies the production transport binding needs. */
|
||||
export interface SetupTokenLoginTransportDeps {
|
||||
/** The sandbox provider. The production provider wraps the environment runtime. */
|
||||
sandbox: SetupTokenSandboxProvider;
|
||||
/** The durable cleanup store. Defaults to the database-backed store over `db`. */
|
||||
store: SetupTokenCleanupStore;
|
||||
/**
|
||||
* The owner-bound secret writer. The binding forwards it to the router, so a
|
||||
* completed login stores the minted token in the secret store. When a caller
|
||||
* omits it, the router keeps its deferred, fail-closed writer.
|
||||
*/
|
||||
completeCredential?: SetupTokenSecretWriter;
|
||||
/** A non-leaking status sink. It receives only fixed status lines. */
|
||||
log?: (line: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* The production transport the router binds through `options.setupTokenLogin`. It
|
||||
* carries the live lease manager, the login-process factory, the durable cleanup
|
||||
* store, and the owner-bound secret writer. The router uses the writer for the
|
||||
* final secret write; when the binding omits it, the router keeps its deferred,
|
||||
* fail-closed writer.
|
||||
*/
|
||||
export interface SetupTokenLoginTransportBinding {
|
||||
factory: SetupTokenLoginProcessFactory;
|
||||
leases: SetupTokenLeaseManager;
|
||||
store: SetupTokenCleanupStore;
|
||||
completeCredential?: SetupTokenSecretWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
* The narrow secret-completion surface the production writer needs. The secrets
|
||||
* service exposes `completeClaudeOAuthUserSecret` as the owner-bound
|
||||
* compare-and-set for the Claude Code OAuth value. The writer forwards only the
|
||||
* scope owner, the session id, the mode, and the token to it.
|
||||
*/
|
||||
export interface SetupTokenSecretCompletion {
|
||||
completeClaudeOAuthUserSecret(
|
||||
companyId: string,
|
||||
ownerUserId: string,
|
||||
input: {
|
||||
sessionId: string;
|
||||
mode: "first_write" | "confirmed_rotation";
|
||||
value: string;
|
||||
expectedSecretId?: string | null;
|
||||
expectedLatestVersion?: number | null;
|
||||
},
|
||||
actor?: { userId?: string | null; agentId?: string | null },
|
||||
): Promise<unknown>;
|
||||
}
|
||||
|
||||
/** The owner-bound compare-and-set input the secrets service consumes. */
|
||||
export type ClaudeOAuthWriteInput =
|
||||
| { sessionId: string; mode: "first_write"; value: string }
|
||||
| {
|
||||
sessionId: string;
|
||||
mode: "confirmed_rotation";
|
||||
value: string;
|
||||
expectedSecretId: string;
|
||||
expectedLatestVersion: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Maps the session writer input to the secrets service compare-and-set input. The
|
||||
* session scope selects the mode. A scope with no overwrite capture writes with
|
||||
* `mode: "first_write"`, so the login creates the first owner value only. A scope
|
||||
* that carries the confirmed-overwrite capture writes with
|
||||
* `mode: "confirmed_rotation"`, so the login rotates the stored value under the
|
||||
* captured `expectedSecretId` and `expectedLatestVersion`. The overwrite capture
|
||||
* carries no owner; the owner still comes from the scope.
|
||||
*/
|
||||
export function buildClaudeOAuthWriteInput(
|
||||
scope: SetupTokenSessionScope,
|
||||
sessionId: string,
|
||||
token: string,
|
||||
): ClaudeOAuthWriteInput {
|
||||
const overwrite = scope.confirmedOverwrite ?? null;
|
||||
return overwrite
|
||||
? {
|
||||
sessionId,
|
||||
mode: "confirmed_rotation",
|
||||
value: token,
|
||||
expectedSecretId: overwrite.expectedSecretId,
|
||||
expectedLatestVersion: overwrite.expectedLatestVersion,
|
||||
}
|
||||
: { sessionId, mode: "first_write", value: token };
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the production atomic credential-claim writer for the login session. It
|
||||
* runs one control-plane transaction. Inside the transaction it first transitions
|
||||
* the exact durable session row to `stored` with a full-scope conditional update,
|
||||
* then writes or rotates the owner-bound secret on the same transaction handle.
|
||||
* The commit establishes the encrypted secret and the `stored` claim together, so
|
||||
* a crash between the two steps cannot leave a stored token with no valid claim.
|
||||
*
|
||||
* A zero-row transition means no claimable row exists in this scope. The writer
|
||||
* rolls back and rejects with the fixed, non-secret storage error, so the session
|
||||
* marks the login failed. A storage failure rejects and rolls back the whole
|
||||
* transaction, so neither the secret nor the claim commits. A concurrent change
|
||||
* fails the compare-and-set with the fixed stale conflict, so the write leaves no
|
||||
* partial state.
|
||||
*
|
||||
* It reads the company and the owner only from the immutable session scope, so a
|
||||
* request cannot redirect the write to another owner. It never logs
|
||||
* the token and never puts the token in an error; it lets the secrets service
|
||||
* error propagate unchanged, and the session maps a rejection to the fixed,
|
||||
* non-secret storage error.
|
||||
*/
|
||||
export function createSetupTokenSecretWriter(deps: {
|
||||
db: Db;
|
||||
/**
|
||||
* Builds the transaction-bound owner-bound completion. It defaults to
|
||||
* `secretService(tx)`, so the compare-and-set runs on the passed transaction
|
||||
* handle. A test injects a fake to force a storage failure after the transition.
|
||||
*/
|
||||
completionForTx?: (tx: Db) => SetupTokenSecretCompletion;
|
||||
}): SetupTokenSecretWriter {
|
||||
const completionForTx = deps.completionForTx ?? ((tx) => secretService(tx));
|
||||
return async ({ scope, sessionId, token }) => {
|
||||
const input = buildClaudeOAuthWriteInput(scope, sessionId, token);
|
||||
await deps.db.transaction(async (tx) => {
|
||||
const txDb = tx as unknown as Db;
|
||||
// Step 1: lock and conditionally transition the exact durable row to
|
||||
// `stored`. The predicate matches the full scope, an allowed predecessor
|
||||
// state, an unexpired deadline, and an unconsumed claim. It requires exactly
|
||||
// one returned row.
|
||||
const transitioned = await transitionSetupTokenSessionToStored(txDb, {
|
||||
sessionId,
|
||||
companyId: scope.companyId,
|
||||
ownerUserId: scope.ownerUserId,
|
||||
adapterType: scope.adapterType,
|
||||
environmentId: scope.environmentId,
|
||||
});
|
||||
if (!transitioned) {
|
||||
// Zero-row result: roll back the whole transaction. The session then marks
|
||||
// the login failed and returns the fixed no-secret error.
|
||||
throw new SetupTokenSessionError(500, SETUP_TOKEN_STORAGE_FAILED);
|
||||
}
|
||||
// Step 2: write or rotate the secret on the same transaction handle. A
|
||||
// failure here rolls back the transition too, so the commit is all-or-none.
|
||||
await completionForTx(txDb).completeClaudeOAuthUserSecret(
|
||||
scope.companyId,
|
||||
scope.ownerUserId,
|
||||
input,
|
||||
{ userId: scope.ownerUserId },
|
||||
);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/** The immutable scope key that correlates one acquire with one factory call. */
|
||||
function scopeKey(scope: SetupTokenSessionScope): string {
|
||||
return [scope.companyId, scope.ownerUserId, scope.adapterType, scope.environmentId].join("\u0000");
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the production setup-token login transport. The lease manager acquires a
|
||||
* sandbox lease through the injected provider and hands the pseudo-terminal
|
||||
* opener to the factory. The factory drives the login runner over the transport
|
||||
* and passes only the fixed command. The lease manager releases the lease on
|
||||
* every terminal path, so the service runs its cleanup against the durable store.
|
||||
*/
|
||||
export function buildSetupTokenLoginTransport(
|
||||
deps: SetupTokenLoginTransportDeps,
|
||||
): SetupTokenLoginTransportBinding {
|
||||
// The binding fixes the login command. It never reads a command from a route, a
|
||||
// request body, an adapter configuration, or the dependency object, so a
|
||||
// runtime-supplied value cannot change the pseudo-terminal command.
|
||||
const command = CLAUDE_SETUP_TOKEN_COMMAND;
|
||||
const log = deps.log ?? (() => {});
|
||||
|
||||
// The pseudo-terminal opener that one acquire hands to the next factory call.
|
||||
// The service calls `leases.acquire` and then `factory` in sequence for one
|
||||
// session, so a per-scope handoff correlates them. The per-owner session cap is
|
||||
// one, so one scope holds at most one live acquire.
|
||||
const pendingOpeners = new Map<string, { leaseId: string; openPtySession: SetupTokenPtySessionOpener }>();
|
||||
// The reverse map releases a handoff by lease id when the factory never
|
||||
// consumes it (a factory error path).
|
||||
const leaseScopeKeys = new Map<string, string>();
|
||||
|
||||
const leases: SetupTokenLeaseManager = {
|
||||
async acquire({ scope, deadline }): Promise<SetupTokenLease> {
|
||||
const acquired = await deps.sandbox.acquire({ scope, deadline });
|
||||
const key = scopeKey(scope);
|
||||
pendingOpeners.set(key, acquired);
|
||||
leaseScopeKeys.set(acquired.leaseId, key);
|
||||
return { id: acquired.leaseId };
|
||||
},
|
||||
async release(lease): Promise<void> {
|
||||
dropHandoff(lease.id);
|
||||
await deps.sandbox.release(lease.id);
|
||||
},
|
||||
async releaseById(leaseId): Promise<void> {
|
||||
dropHandoff(leaseId);
|
||||
await deps.sandbox.release(leaseId);
|
||||
},
|
||||
};
|
||||
|
||||
function dropHandoff(leaseId: string): void {
|
||||
const key = leaseScopeKeys.get(leaseId);
|
||||
if (key !== undefined) {
|
||||
const pending = pendingOpeners.get(key);
|
||||
if (pending && pending.leaseId === leaseId) pendingOpeners.delete(key);
|
||||
leaseScopeKeys.delete(leaseId);
|
||||
}
|
||||
}
|
||||
|
||||
const factory: SetupTokenLoginProcessFactory = ({ scope, onPrompt, onCredential, timeoutMs, signal }): SetupTokenLoginProcess => {
|
||||
const key = scopeKey(scope);
|
||||
const pending = pendingOpeners.get(key);
|
||||
if (!pending) {
|
||||
// The lease manager did not hand off an opener for this scope. Fail closed.
|
||||
throw new SetupTokenSessionError(503, SETUP_TOKEN_START_FAILED);
|
||||
}
|
||||
pendingOpeners.delete(key);
|
||||
leaseScopeKeys.delete(pending.leaseId);
|
||||
|
||||
const transport = createSetupTokenPtyTransport(pending.openPtySession);
|
||||
|
||||
// Bridge the browser code. The service calls `submitCode` one time after the
|
||||
// single `awaiting_code` transition wins. The runner calls `provideCode` one
|
||||
// time after it surfaces the prompt. A resolved promise hands the code from
|
||||
// the service call to the runner call.
|
||||
let deliverCode: ((code: string) => void) | null = null;
|
||||
const codeReady = new Promise<string>((resolve, reject) => {
|
||||
deliverCode = resolve;
|
||||
// A cancel or an expiry aborts the signal. Reject the pending code, so the
|
||||
// runner stops waiting and ends the run.
|
||||
if (signal.aborted) {
|
||||
reject(new SetupTokenSessionError(499, SETUP_TOKEN_START_FAILED));
|
||||
return;
|
||||
}
|
||||
signal.addEventListener(
|
||||
"abort",
|
||||
() => reject(new SetupTokenSessionError(499, SETUP_TOKEN_START_FAILED)),
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
// The runner rejects the run when `provideCode` rejects. Consume the pending
|
||||
// rejection here too, so it never becomes an unhandled rejection when the
|
||||
// runner ends on the timeout or the signal before it reads the code.
|
||||
codeReady.catch(() => {});
|
||||
|
||||
const done: Promise<SetupTokenLoginOutcome> = runSetupTokenLogin(transport, {
|
||||
command,
|
||||
timeoutMs,
|
||||
signal,
|
||||
onPrompt: (prompt) => onPrompt({ url: prompt.url }),
|
||||
provideCode: () => codeReady,
|
||||
onCredential: async (authBytes) => {
|
||||
// Forward the minted token to the owner-bound secret write. The service
|
||||
// holds the token only for this call and never stores it.
|
||||
await onCredential(authBytes.toString("utf8"));
|
||||
},
|
||||
log,
|
||||
}).then((result) => result.outcome);
|
||||
|
||||
return {
|
||||
done,
|
||||
submitCode(code): void {
|
||||
deliverCode?.(code);
|
||||
},
|
||||
stop(): void {
|
||||
// Stop the direct child. The service releases the lease after this call.
|
||||
transport.stop();
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
return { factory, leases, store: deps.store, completeCredential: deps.completeCredential };
|
||||
}
|
||||
|
||||
/** The dependencies the production sandbox provider needs. */
|
||||
export interface ProductionSetupTokenSandboxProviderDeps {
|
||||
environments: Pick<ReturnType<typeof environmentService>, "getById" | "getLeaseById" | "releaseLease">;
|
||||
environmentRuntime: Pick<ReturnType<typeof environmentRuntimeService>, "acquireRunLease" | "getDriver">;
|
||||
/**
|
||||
* Opens the live pseudo-terminal for the acquired lease. The Daytona provider
|
||||
* runs as a plugin worker, so the real opener binds `sandbox.process` inside
|
||||
* the worker. When a caller omits it, the provider fails closed before it
|
||||
* acquires a lease, and the live path lands with the characterization
|
||||
* test.
|
||||
*/
|
||||
openLivePtySession?: (input: {
|
||||
scope: SetupTokenSessionScope;
|
||||
environmentId: string;
|
||||
leaseId: string;
|
||||
}) => Promise<SetupTokenPtySessionOpener>;
|
||||
/** A non-leaking status sink. It receives only fixed status lines. */
|
||||
log?: (line: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the production sandbox provider over the environment runtime. It
|
||||
* acquires a fresh sandbox lease for one login session, binds the live
|
||||
* pseudo-terminal opener to that lease, and releases the lease by id. It fails
|
||||
* closed for a local environment, an SSH environment, or a missing live opener.
|
||||
*/
|
||||
export function createProductionSetupTokenSandboxProvider(
|
||||
deps: ProductionSetupTokenSandboxProviderDeps,
|
||||
): SetupTokenSandboxProvider {
|
||||
const log = deps.log ?? (() => {});
|
||||
// The acquired lease records, keyed by lease id. The provider releases a lease
|
||||
// through its driver. The startup reaper releases a lease by id after a
|
||||
// restart, when this map is empty; the provider then falls back to a direct
|
||||
// database release.
|
||||
const leaseRecords = new Map<
|
||||
string,
|
||||
Awaited<ReturnType<ReturnType<typeof environmentRuntimeService>["acquireRunLease"]>>
|
||||
>();
|
||||
|
||||
const failClosed = (): never => {
|
||||
throw new SetupTokenSessionError(503, SETUP_TOKEN_START_FAILED);
|
||||
};
|
||||
|
||||
return {
|
||||
async acquire({ scope, deadline }) {
|
||||
const environment = await deps.environments.getById(scope.environmentId);
|
||||
if (!environment) {
|
||||
log("[paperclip] Setup-token login: the selected environment is not found.");
|
||||
return failClosed();
|
||||
}
|
||||
if (environment.driver === "local" || environment.driver === "ssh") {
|
||||
// The login pseudo-terminal needs a sandbox. A local or an SSH
|
||||
// environment has no sandbox process to run the login command in.
|
||||
log("[paperclip] Setup-token login: the selected environment has no sandbox.");
|
||||
return failClosed();
|
||||
}
|
||||
if (!deps.openLivePtySession) {
|
||||
// The live sandbox pseudo-terminal opener is not bound yet. Fail closed
|
||||
// before the acquire, so the login holds no lease. The live opener lands
|
||||
// with the characterization test against a real sandbox.
|
||||
log(
|
||||
"[paperclip] Setup-token login: the live sandbox pseudo-terminal transport is not bound.",
|
||||
);
|
||||
return failClosed();
|
||||
}
|
||||
|
||||
const leaseRecord = await deps.environmentRuntime.acquireRunLease({
|
||||
companyId: scope.companyId,
|
||||
environment,
|
||||
issueId: null,
|
||||
agentId: scope.targetAgentId ?? null,
|
||||
// A null heartbeat run and a null execution workspace disable lease
|
||||
// reuse, so the login session always runs in a fresh sandbox.
|
||||
heartbeatRunId: null,
|
||||
persistedExecutionWorkspace: null,
|
||||
adapterType: scope.adapterType,
|
||||
// Apply the active custom-image template, so the sandbox binds to the
|
||||
// trusted image and runtime identity.
|
||||
applyCustomImageTemplate: true,
|
||||
// Re-check the environment company binding inside the lease insert
|
||||
// transaction. The route guard ran earlier, so a managed reconciliation
|
||||
// can bind this sandbox to another company between the guard and this
|
||||
// acquire. The lease insert then rejects a foreign-company environment
|
||||
// with the 403 `environment_company_mismatch` and holds no lease.
|
||||
assertCompanyBinding: true,
|
||||
// Bound the lease expiry to the session deadline. The runtime records
|
||||
// the earlier of this deadline and the provider expiry on the lease row.
|
||||
requestedExpiresAt: new Date(deadline),
|
||||
});
|
||||
const leaseId = leaseRecord.lease.id;
|
||||
leaseRecords.set(leaseId, leaseRecord);
|
||||
|
||||
// Enforce an independent, provider-backed expiry at or before the session
|
||||
// deadline. A crash or an outage stops the in-process cleanup, so the
|
||||
// lease row must carry a durable expiry that a lease reaper acts on. When
|
||||
// the acquired lease has no expiry, an invalid expiry, or an expiry after
|
||||
// the deadline, the server cannot guarantee the hard stop. Release the
|
||||
// remote lease and fail closed, so the login holds no lease and leaves no
|
||||
// durable cleanup row.
|
||||
const expiresAt = leaseRecord.lease.expiresAt;
|
||||
const expiresAtMs = expiresAt instanceof Date ? expiresAt.getTime() : Number.NaN;
|
||||
if (!Number.isFinite(expiresAtMs) || expiresAtMs > deadline) {
|
||||
await this.release(leaseId);
|
||||
log("[paperclip] Setup-token login: the acquired lease expiry does not bound the session deadline.");
|
||||
return failClosed();
|
||||
}
|
||||
|
||||
try {
|
||||
const openPtySession = await deps.openLivePtySession({
|
||||
scope,
|
||||
environmentId: scope.environmentId,
|
||||
leaseId,
|
||||
});
|
||||
return { leaseId, openPtySession };
|
||||
} catch (err) {
|
||||
// The opener bind failed. Release the lease and fail closed, so the login
|
||||
// holds no lease.
|
||||
await this.release(leaseId);
|
||||
log("[paperclip] Setup-token login: the live pseudo-terminal bind failed.");
|
||||
void err;
|
||||
return failClosed();
|
||||
}
|
||||
},
|
||||
|
||||
async release(leaseId) {
|
||||
const leaseRecord = leaseRecords.get(leaseId);
|
||||
if (leaseRecord) {
|
||||
leaseRecords.delete(leaseId);
|
||||
const driver = deps.environmentRuntime.getDriver(leaseRecord.environment.driver);
|
||||
if (driver) {
|
||||
await driver.releaseRunLease({
|
||||
environment: leaseRecord.environment,
|
||||
lease: leaseRecord.lease,
|
||||
status: "released",
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
// A restart cleared the in-memory record. Release the lease through the
|
||||
// provider driver, not only the database row, so the release tears down the
|
||||
// remote sandbox that holds the login state.
|
||||
await releaseLeaseById(leaseId);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Releases a lease by id after a restart, when the in-memory record is gone. It
|
||||
* resolves the stored lease and its environment, then releases the lease through
|
||||
* the provider driver, so the release tears down the remote sandbox and not only
|
||||
* the database row. It fails loud when it resolves a live lease that it cannot
|
||||
* release, so the reaper keeps the durable cleanup record and retries it. It
|
||||
* returns without an error only when the lease row is already gone, because then
|
||||
* no sandbox remains to release.
|
||||
*/
|
||||
async function releaseLeaseById(leaseId: string): Promise<void> {
|
||||
const lease = await deps.environments.getLeaseById(leaseId);
|
||||
if (!lease) {
|
||||
// The lease row is already gone. No remote sandbox remains to release, so
|
||||
// the release is idempotent and the reaper may drop the cleanup record.
|
||||
return;
|
||||
}
|
||||
const environment = lease.environmentId
|
||||
? await deps.environments.getById(lease.environmentId)
|
||||
: null;
|
||||
if (!environment) {
|
||||
// The lease resolves but its environment is gone, so no driver can tear down
|
||||
// the remote sandbox. Fail loud, so the reaper keeps the cleanup record.
|
||||
log("[paperclip] Setup-token login: the lease environment is not found for a restart release.");
|
||||
return failClosed();
|
||||
}
|
||||
const driver = deps.environmentRuntime.getDriver(environment.driver);
|
||||
if (!driver) {
|
||||
// No driver resolves the remote sandbox. Fail loud, so the reaper keeps the
|
||||
// cleanup record and retries the release.
|
||||
log("[paperclip] Setup-token login: no driver resolves the lease for a restart release.");
|
||||
return failClosed();
|
||||
}
|
||||
await driver.releaseRunLease({ environment, lease, status: "released" });
|
||||
}
|
||||
}
|
||||
|
||||
/** Builds the durable, database-backed cleanup store for the production binding. */
|
||||
export function createProductionSetupTokenCleanupStore(db: Db): SetupTokenCleanupStore {
|
||||
return createDbSetupTokenCleanupStore(db);
|
||||
}
|
||||
|
||||
/**
|
||||
* The narrow plugin worker manager surface the live opener needs. The manager
|
||||
* owns the host route gate: it mints the host route identifier,
|
||||
* reserves one route per worker, drives the open, binds the worker session
|
||||
* identifier one time, routes output, and terminalizes the route.
|
||||
*/
|
||||
export interface SetupTokenPtyWorkerManagerLike {
|
||||
openSetupTokenPtySession(
|
||||
pluginId: string,
|
||||
input: {
|
||||
driverKey: string;
|
||||
companyId: string;
|
||||
environmentId: string;
|
||||
providerLeaseId: string;
|
||||
command: string;
|
||||
},
|
||||
): Promise<SetupTokenPtySession>;
|
||||
}
|
||||
|
||||
/** The narrow lease-lookup surface the live opener needs. */
|
||||
export interface SetupTokenPtyLeaseLookup {
|
||||
getLeaseById(leaseId: string): Promise<
|
||||
| {
|
||||
providerLeaseId: string | null;
|
||||
metadata: Record<string, unknown> | null | undefined;
|
||||
}
|
||||
| null
|
||||
>;
|
||||
}
|
||||
|
||||
/** The dependencies the worker-bound live pseudo-terminal opener needs. */
|
||||
export interface WorkerBoundSetupTokenPtyOpenerDeps {
|
||||
/** The plugin worker manager that owns the host route gate. */
|
||||
workerManager: SetupTokenPtyWorkerManagerLike;
|
||||
/** The environment lease lookup that resolves the worker target for a lease. */
|
||||
environments: SetupTokenPtyLeaseLookup;
|
||||
/** A non-leaking status sink. It receives only fixed status lines. */
|
||||
log?: (line: string) => void;
|
||||
}
|
||||
|
||||
function readLeaseMetaString(value: unknown): string | null {
|
||||
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the production `openLivePtySession`. It resolves the server lease to its
|
||||
* `providerLeaseId` and the sandbox worker plugin id, then returns an opener that
|
||||
* drives the worker through the manager route gate. The manager mints the host
|
||||
* route identifier and owns the route lifecycle. The opener fails closed when the
|
||||
* lease carries no sandbox worker binding.
|
||||
*/
|
||||
export function createWorkerBoundSetupTokenPtyOpener(
|
||||
deps: WorkerBoundSetupTokenPtyOpenerDeps,
|
||||
): NonNullable<ProductionSetupTokenSandboxProviderDeps["openLivePtySession"]> {
|
||||
const log = deps.log ?? (() => {});
|
||||
return async ({ scope, environmentId, leaseId }) => {
|
||||
const lease = await deps.environments.getLeaseById(leaseId);
|
||||
const providerLeaseId =
|
||||
typeof lease?.providerLeaseId === "string" && lease.providerLeaseId.length > 0
|
||||
? lease.providerLeaseId
|
||||
: null;
|
||||
const metadata =
|
||||
lease && typeof lease.metadata === "object" && lease.metadata !== null
|
||||
? (lease.metadata as Record<string, unknown>)
|
||||
: {};
|
||||
const pluginId = readLeaseMetaString(metadata.pluginId);
|
||||
const driverKey =
|
||||
readLeaseMetaString(metadata.provider) ?? readLeaseMetaString(metadata.driver);
|
||||
if (!providerLeaseId || !pluginId || !driverKey) {
|
||||
log("[paperclip] Setup-token login: the lease carries no sandbox worker binding.");
|
||||
throw new SetupTokenSessionError(503, SETUP_TOKEN_START_FAILED);
|
||||
}
|
||||
return (command: string) =>
|
||||
deps.workerManager.openSetupTokenPtySession(pluginId, {
|
||||
driverKey,
|
||||
companyId: scope.companyId,
|
||||
environmentId,
|
||||
providerLeaseId,
|
||||
command,
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -2,9 +2,140 @@ import { EventEmitter } from "node:events";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
coordinateHeartbeatSchedulerShutdown,
|
||||
finalizeServerShutdown,
|
||||
loadWithoutCoordinatedShutdownSignalHooks,
|
||||
} from "./shutdown.js";
|
||||
|
||||
function deferred<T = void>() {
|
||||
let resolve!: (value: T) => void;
|
||||
let reject!: (reason?: unknown) => void;
|
||||
const promise = new Promise<T>((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
function stubLogger() {
|
||||
return { info: vi.fn(), error: vi.fn() };
|
||||
}
|
||||
|
||||
describe("finalizeServerShutdown", () => {
|
||||
it("awaits the setup-token cleanup before the database stop and the process exit", async () => {
|
||||
const order: string[] = [];
|
||||
// The held promise models the setup-token session cancellation and its
|
||||
// sandbox lease release. The teardown must not continue while it is pending.
|
||||
const release = deferred();
|
||||
const shutdownAppServices = vi.fn(async () => {
|
||||
order.push("appServices:start");
|
||||
await release.promise;
|
||||
order.push("appServices:settled");
|
||||
});
|
||||
const stopEmbeddedPostgres = vi.fn(async () => {
|
||||
order.push("postgres:stop");
|
||||
});
|
||||
const shutdownInstrumentation = vi.fn(async () => {
|
||||
order.push("instrumentation:flush");
|
||||
});
|
||||
|
||||
let exited = false;
|
||||
const finalize = finalizeServerShutdown({
|
||||
signal: "SIGTERM",
|
||||
shutdownAppServices,
|
||||
stopEmbeddedPostgres,
|
||||
shutdownInstrumentation,
|
||||
log: stubLogger(),
|
||||
}).then(() => {
|
||||
// This models the caller's `process.exit(0)` continuation.
|
||||
exited = true;
|
||||
order.push("exit");
|
||||
});
|
||||
|
||||
// The cleanup is in flight. The database stop, the instrumentation flush,
|
||||
// and the process exit continuation must all wait for it to settle.
|
||||
await vi.waitFor(() => expect(shutdownAppServices).toHaveBeenCalledOnce());
|
||||
expect(stopEmbeddedPostgres).not.toHaveBeenCalled();
|
||||
expect(shutdownInstrumentation).not.toHaveBeenCalled();
|
||||
expect(exited).toBe(false);
|
||||
|
||||
release.resolve();
|
||||
await finalize;
|
||||
|
||||
expect(exited).toBe(true);
|
||||
expect(order).toEqual([
|
||||
"appServices:start",
|
||||
"appServices:settled",
|
||||
"postgres:stop",
|
||||
"instrumentation:flush",
|
||||
"exit",
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps the teardown durable and still exits when the setup-token release fails", async () => {
|
||||
const order: string[] = [];
|
||||
// The held promise rejects, which models a lease release that failed. The
|
||||
// reaper owns the durable retry, so the teardown must log the failure and
|
||||
// continue rather than swallow it or block the exit.
|
||||
const release = deferred();
|
||||
const releaseError = new Error("lease release failed");
|
||||
const shutdownAppServices = vi.fn(async () => {
|
||||
await release.promise;
|
||||
});
|
||||
const stopEmbeddedPostgres = vi.fn(async () => {
|
||||
order.push("postgres:stop");
|
||||
});
|
||||
const shutdownInstrumentation = vi.fn(async () => {
|
||||
order.push("instrumentation:flush");
|
||||
});
|
||||
const log = stubLogger();
|
||||
|
||||
let exited = false;
|
||||
const finalize = finalizeServerShutdown({
|
||||
signal: "SIGTERM",
|
||||
shutdownAppServices,
|
||||
stopEmbeddedPostgres,
|
||||
shutdownInstrumentation,
|
||||
log,
|
||||
}).then(() => {
|
||||
exited = true;
|
||||
});
|
||||
|
||||
await vi.waitFor(() => expect(shutdownAppServices).toHaveBeenCalledOnce());
|
||||
expect(stopEmbeddedPostgres).not.toHaveBeenCalled();
|
||||
expect(exited).toBe(false);
|
||||
|
||||
release.reject(releaseError);
|
||||
await finalize;
|
||||
|
||||
// The teardown surfaced the failure in the log, then finished the ordered
|
||||
// teardown and reached the exit continuation.
|
||||
expect(log.error).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ err: releaseError, signal: "SIGTERM" }),
|
||||
expect.any(String),
|
||||
);
|
||||
expect(order).toEqual(["postgres:stop", "instrumentation:flush"]);
|
||||
expect(exited).toBe(true);
|
||||
});
|
||||
|
||||
it("skips the database stop when no embedded PostgreSQL runs in this process", async () => {
|
||||
const shutdownAppServices = vi.fn(async () => undefined);
|
||||
const shutdownInstrumentation = vi.fn(async () => undefined);
|
||||
const log = stubLogger();
|
||||
|
||||
await finalizeServerShutdown({
|
||||
signal: "SIGINT",
|
||||
shutdownAppServices,
|
||||
stopEmbeddedPostgres: null,
|
||||
shutdownInstrumentation,
|
||||
log,
|
||||
});
|
||||
|
||||
expect(shutdownAppServices).toHaveBeenCalledOnce();
|
||||
expect(shutdownInstrumentation).toHaveBeenCalledOnce();
|
||||
expect(log.info).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("loadWithoutCoordinatedShutdownSignalHooks", () => {
|
||||
it("removes the eager signal handlers from the real embedded-postgres import", async () => {
|
||||
const before = {
|
||||
|
||||
@@ -2,6 +2,56 @@ type HotRestartShutdownPreparation = {
|
||||
skipDrain: boolean;
|
||||
};
|
||||
|
||||
type ShutdownLogger = {
|
||||
info(obj: object, msg: string): void;
|
||||
error(obj: object, msg: string): void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Runs the final, ordered teardown of the server. It awaits the application
|
||||
* service cleanup first, so a live setup-token login session stops and releases
|
||||
* its sandbox lease before the database and the provider stop. The caller runs
|
||||
* `process.exit(0)` only after this helper resolves, so an orderly shutdown
|
||||
* never leaves a sandbox lease or confidential login state alive past the
|
||||
* process exit.
|
||||
*
|
||||
* A step that rejects does not stop the teardown. The helper logs the error and
|
||||
* continues to the next step. A failed setup-token lease release stays a
|
||||
* durable record for the startup reaper; the helper surfaces it in the log
|
||||
* instead of blocking the exit path.
|
||||
*/
|
||||
export async function finalizeServerShutdown(input: {
|
||||
signal: "SIGINT" | "SIGTERM";
|
||||
shutdownAppServices: (() => Promise<void>) | undefined;
|
||||
stopEmbeddedPostgres: (() => Promise<void>) | null;
|
||||
shutdownInstrumentation: () => Promise<void>;
|
||||
log: ShutdownLogger;
|
||||
}): Promise<void> {
|
||||
const { signal } = input;
|
||||
|
||||
// Await the application service cleanup, so a live setup-token login session
|
||||
// releases its sandbox lease before the database and the provider stop. A
|
||||
// rejected cleanup stays durable for the reaper; it does not block the exit.
|
||||
try {
|
||||
await input.shutdownAppServices?.();
|
||||
} catch (err) {
|
||||
input.log.error({ err, signal }, "Application service shutdown failed");
|
||||
}
|
||||
|
||||
if (input.stopEmbeddedPostgres) {
|
||||
input.log.info({ signal }, "Stopping embedded PostgreSQL");
|
||||
try {
|
||||
await input.stopEmbeddedPostgres();
|
||||
} catch (err) {
|
||||
input.log.error({ err }, "Failed to stop embedded PostgreSQL cleanly");
|
||||
}
|
||||
}
|
||||
|
||||
// Flush buffered OTel spans before the process goes away; without this await
|
||||
// the exporter's final batch is dropped on exit.
|
||||
await input.shutdownInstrumentation();
|
||||
}
|
||||
|
||||
const COORDINATED_SHUTDOWN_SIGNALS = ["SIGINT", "SIGTERM"] as const;
|
||||
|
||||
type ShutdownSignalTarget = {
|
||||
|
||||
Reference in new issue
Block a user