From ad320aeb7191c7cb951efd62017cf43e60e698dc Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Thu, 12 Nov 2026 17:06:22 -0800 Subject: [PATCH 01/21] fix(native): wait for a dated provider limit reset and keep it out of Sentry (#19775) Keep native quota cooldowns tied to valid dated reset messages. Parse explicit years and strict month names while preserving already-expired dates. Keep typed native quota and rate-limit failures on their tasks in unexpected Sentry reporting mode. Preserve setup, process, malformed and all-mode reporting. Verified current-head CI, Greptile 5/5 at Medium impact, local parser/report red-green tests and PostgreSQL integration coverage. Co-Authored-By: Devin Foley Co-Authored-By: Paperclip --- doc/observability.md | 6 + ...-failure-report-native-usage-limit.test.ts | 141 ++++++++++++++++++ ...rovider-limit-cooldown.integration.test.ts | 84 +++++++++++ .../src/services/provider-quota-reset.test.ts | 65 ++++++++ server/src/services/provider-quota-reset.ts | 20 ++- server/src/services/run-failure-report.ts | 10 ++ 6 files changed, 318 insertions(+), 8 deletions(-) create mode 100644 server/src/services/__tests__/run-failure-report-native-usage-limit.test.ts create mode 100644 server/src/services/native-runtime/native-provider-limit-cooldown.integration.test.ts diff --git a/doc/observability.md b/doc/observability.md index de9d0666744..e2a8d6f8746 100644 --- a/doc/observability.md +++ b/doc/observability.md @@ -1126,6 +1126,12 @@ evidence, unknown binding reasons, secret-provider failures, ambiguous missing secret-definition lookups remain reportable. This filter does not change task recovery, credentials, or execution policy. +Native `native_provider_usage_limit` failures also stay on the task when their +typed provider failure is `quota_exhausted` or `rate_limit`. The task keeps its +blocked state and quota cooldown. Setup failures, interrupted processes, missing +or malformed provider evidence, and other failure categories still report. +`all` mode also reports typed native usage limits. + AI account selection also stays local when the selection service proves a missing responsible user, personal default, or selected connection; an incompatible selection; an account that needs reconnection; or an active diff --git a/server/src/services/__tests__/run-failure-report-native-usage-limit.test.ts b/server/src/services/__tests__/run-failure-report-native-usage-limit.test.ts new file mode 100644 index 00000000000..ddc866a02d9 --- /dev/null +++ b/server/src/services/__tests__/run-failure-report-native-usage-limit.test.ts @@ -0,0 +1,141 @@ +import { randomUUID } from "node:crypto"; +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { agents, applyPendingMigrations, closeRegisteredClients, companies, createDb, heartbeatRuns } from "@paperclipai/db"; +import { + getEmbeddedPostgresTestSupport, + startEmbeddedPostgresTestDatabase, +} from "../../__tests__/helpers/embedded-postgres.js"; + +const mockCaptureRunFailure = vi.hoisted(() => vi.fn()); + +vi.mock("../../sentry.js", () => ({ + captureRunFailure: mockCaptureRunFailure, +})); + +import { reportRunFailure } from "../run-failure-report.js"; + +const testDatabaseUrl = process.env.PAPERCLIP_TEST_DATABASE_URL; +const embeddedPostgresSupport = testDatabaseUrl ? { supported: true } : await getEmbeddedPostgresTestSupport(); +const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip; + +const limitFailure = { + schema: "paperclip.provider-failure.v1", + provider: "anthropic", + phase: "turn", + category: "quota_exhausted", + message: "ACP agent reported a terminal limit failure.\nYou've hit your weekly limit · resets Nov 10, 3pm (UTC)", + retryability: "requires_action", +}; + +describeEmbeddedPostgres("reportRunFailure native provider usage limits", () => { + let db!: ReturnType; + let tempDb: Awaited> | null = null; + let companyId!: string; + let agentId!: string; + let inheritedEnv: NodeJS.ProcessEnv; + + beforeAll(async () => { + if (testDatabaseUrl) { + await applyPendingMigrations(testDatabaseUrl); + db = createDb(testDatabaseUrl); + } else { + tempDb = await startEmbeddedPostgresTestDatabase("paperclip-run-failure-native-usage-limit-"); + db = createDb(tempDb.connectionString); + } + }, 20_000); + + beforeEach(async () => { + inheritedEnv = process.env; + process.env = Object.fromEntries( + ["PATH", "HOME", "USER", "USERNAME", "LOGNAME", "USERPROFILE", "TMPDIR", "TEMP", "TMP"] + .flatMap((key) => inheritedEnv[key] === undefined ? [] : [[key, inheritedEnv[key]]]), + ); + process.env.PAPERCLIP_SENTRY_RUN_FAILURES = "unexpected"; + companyId = randomUUID(); + agentId = randomUUID(); + await db.insert(companies).values({ + id: companyId, + name: "Paperclip", + issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`, + requireBoardApprovalForNewAgents: false, + defaultResponsibleUserId: "responsible-user", + }); + await db.insert(agents).values({ + id: agentId, + companyId, + name: "Runner", + role: "engineer", + status: "active", + adapterType: "paperclip_runner", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + }); + }); + + afterEach(async () => { + vi.clearAllMocks(); + vi.unstubAllEnvs(); + process.env = inheritedEnv; + await db.delete(heartbeatRuns); + await db.delete(agents); + await db.delete(companies); + }); + + afterAll(async () => { + if (testDatabaseUrl) await closeRegisteredClients(testDatabaseUrl); + await tempDb?.cleanup(); + }); + + /** The persisted shape of a native ACP turn that hit an exhausted provider allowance. */ + function nativeUsageLimitRun( + overrides: Partial = {}, + ): typeof heartbeatRuns.$inferSelect { + return { + id: randomUUID(), + companyId, + agentId, + status: "failed", + error: "ACP agent reported a terminal limit failure.", + errorCode: "native_provider_usage_limit", + runtimeMode: "native", + nativePhase: "terminal_failure", + nativeIssueId: randomUUID(), + exitCode: null, + signal: null, + contextSnapshot: null, + resultJson: { providerFailure: limitFailure }, + ...overrides, + } as unknown as typeof heartbeatRuns.$inferSelect; + } + + it.each(["quota_exhausted", "rate_limit"])("keeps a native %s usage limit on the task", async (category) => { + const run = nativeUsageLimitRun({ resultJson: { providerFailure: { ...limitFailure, category } } }); + const before = structuredClone(run); + for (const phase of [undefined, "execute"] as const) await reportRunFailure(db, run, { phase }); + expect(mockCaptureRunFailure).not.toHaveBeenCalled(); + expect(run).toEqual(before); + }); + + it.each([ + { status: "timed_out" }, { signal: "SIGTERM" }, { runtimeMode: "legacy" }, + { errorCode: "native_provider_terminal_failed" }, { errorCode: "native_session_retry_exhausted" }, + { resultJson: {} }, { resultJson: null }, + { resultJson: { providerFailure: { ...limitFailure, category: "unknown", retryability: "unknown" } } }, + { resultJson: { providerFailure: { category: "quota_exhausted" } } }, + ])("reports a native usage limit outcome without typed limit evidence: %j", async (overrides) => { + await reportRunFailure(db, nativeUsageLimitRun(overrides), { phase: "execute" }); + expect(mockCaptureRunFailure).toHaveBeenCalledTimes(1); + }); + + it("reports a native usage limit during setup", async () => { + await reportRunFailure(db, nativeUsageLimitRun(), { phase: "setup" }); + expect(mockCaptureRunFailure).toHaveBeenCalledTimes(1); + }); + + it("reports native usage limits when every run failure is reported", async () => { + vi.stubEnv("PAPERCLIP_SENTRY_RUN_FAILURES", "all"); + await reportRunFailure(db, nativeUsageLimitRun(), { phase: "execute" }); + expect(mockCaptureRunFailure).toHaveBeenCalledTimes(1); + }); +}); diff --git a/server/src/services/native-runtime/native-provider-limit-cooldown.integration.test.ts b/server/src/services/native-runtime/native-provider-limit-cooldown.integration.test.ts new file mode 100644 index 00000000000..55ea60a445e --- /dev/null +++ b/server/src/services/native-runtime/native-provider-limit-cooldown.integration.test.ts @@ -0,0 +1,84 @@ +import { randomUUID } from "node:crypto"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { agents, companies, createDb, heartbeatRuns } from "@paperclipai/db"; +import { startEmbeddedPostgresTestDatabase } from "../../__tests__/helpers/embedded-postgres.js"; +import { agentProviderLimitCooldown } from "./native-startup-retry.js"; + +const HOUR_MS = 60 * 60_000; +const DAY_MS = 24 * HOUR_MS; + +/** Claude ACP wording for an exhausted weekly allowance, such as "resets Nov 10, 3pm (UTC)". */ +function weeklyLimitMessage(at: Date) { + const month = at.toLocaleString("en-US", { month: "short", timeZone: "UTC" }); + const hour = at.getUTCHours(); + return `ACP agent reported a terminal limit failure.\nYou've hit your weekly limit · resets ${month} ${at.getUTCDate()}, ${ + hour % 12 || 12}${hour < 12 ? "am" : "pm"} (UTC)`; +} + +describe("native provider limit cooldown", () => { + let temporary: Awaited>; + let db: ReturnType; + const companyId = randomUUID(); + + beforeAll(async () => { + temporary = await startEmbeddedPostgresTestDatabase("native-provider-limit-cooldown-"); + db = createDb(temporary.connectionString); + await db.insert(companies).values({ id: companyId, name: "Native provider limit", issuePrefix: "NPL" }); + }); + + afterAll(async () => { + await temporary?.cleanup(); + }); + + /** An agent whose latest native run failed on an exhausted weekly allowance. */ + async function limitedAgent(resetAt: Date) { + const agentId = randomUUID(); + await db.insert(agents).values({ + id: agentId, + companyId, + name: `Limited agent ${agentId.slice(0, 8)}`, + status: "active", + adapterType: "paperclip_runner", + adapterConfig: { provider: "acpx", agent: "claude" }, + }); + const limitedAt = new Date(Date.now() - 5 * 60_000); + await db.insert(heartbeatRuns).values({ + id: randomUUID(), + companyId, + agentId, + runtimeMode: "native", + invocationSource: "assignment", + triggerDetail: "system", + status: "failed", + startedAt: new Date(limitedAt.getTime() - 60_000), + finishedAt: limitedAt, + errorCode: "native_provider_usage_limit", + resultJson: { + providerFailure: { + schema: "paperclip.provider-failure.v1", + provider: "anthropic", + phase: "turn", + category: "quota_exhausted", + message: weeklyLimitMessage(resetAt), + retryability: "requires_action", + }, + }, + }); + return agentId; + } + + it("holds an agent until a dated weekly-limit reset instead of the one-hour default", async () => { + const resetAt = new Date(Math.ceil((Date.now() + 20 * HOUR_MS) / HOUR_MS) * HOUR_MS); + const agentId = await limitedAgent(resetAt); + expect(await agentProviderLimitCooldown(db, { companyId, agentId, now: new Date() })) + .toMatchObject({ resetAt, resetReported: true, category: "quota_exhausted" }); + }); + + it("re-checks a dated reset days away within one day", async () => { + const agentId = await limitedAgent(new Date(Math.ceil((Date.now() + 3 * DAY_MS) / HOUR_MS) * HOUR_MS)); + const now = new Date(); + const cooldown = await agentProviderLimitCooldown(db, { companyId, agentId, now }); + expect(cooldown).toMatchObject({ resetReported: true }); + expect(cooldown!.resetAt.getTime()).toBe(now.getTime() + DAY_MS); + }); +}); diff --git a/server/src/services/provider-quota-reset.test.ts b/server/src/services/provider-quota-reset.test.ts index f3ba512145b..96b81178886 100644 --- a/server/src/services/provider-quota-reset.test.ts +++ b/server/src/services/provider-quota-reset.test.ts @@ -41,3 +41,68 @@ describe("parseProviderQuotaClockReset", () => { expect(parseProviderQuotaClockReset("Provider quota exceeded for this model.", now)).toBeNull(); }); }); + +const weeklyLimit = "ACP agent reported a terminal limit failure.\nYou've hit your weekly limit · resets Nov 10, 3pm (UTC)"; + +describe("dated provider quota reset compatibility", () => { + it("reads a dated reset as that day, not as an unparsed limit", () => { + expect(parseProviderQuotaClockReset(weeklyLimit, new Date("2026-11-07T00:50:37Z"))) + .toEqual(new Date("2026-11-10T15:00:00Z")); + }); + + it.each([ + ["resets Nov 10 at 3:30pm (UTC)", "2026-11-10T15:30:00Z"], + ["resets November 10, 3 PM UTC", "2026-11-10T15:00:00Z"], + ["resets Dec. 1, 9am (UTC)", "2026-12-01T09:00:00Z"], + ["resets Nov 10th, 2026 3pm", "2026-11-10T15:00:00Z"], + ["try again at Nov 10th, 2026 2:04 AM.", "2026-11-10T02:04:00Z"], + ["resets Nov 10, 15:00 (UTC)", "2026-11-10T15:00:00Z"], + ["resets Nov 10, 3pm (America/Chicago)", "2026-11-10T21:00:00Z"], + ])("reads %s", (wording, expected) => { + expect(parseProviderQuotaClockReset(`You've hit your weekly limit · ${wording}`, new Date("2026-11-07T00:50:37Z"))) + .toEqual(new Date(expected)); + }); + + it("rolls a dated reset without a year into the next year at the year boundary", () => { + expect(parseProviderQuotaClockReset("resets Jan 2, 3pm (UTC)", new Date("2026-12-30T12:00:00Z"))) + .toEqual(new Date("2027-01-02T15:00:00Z")); + }); + + it("keeps a recently passed dated reset in the past instead of a year later", () => { + expect(parseProviderQuotaClockReset("resets Nov 6, 3pm (UTC)", new Date("2026-11-07T00:50:37Z"))) + .toEqual(new Date("2026-11-06T15:00:00Z")); + }); + + it("keeps stale calendar dates expired and rejects a far-future explicit year", () => { + const now = new Date("2026-11-07T00:50:37Z"); + expect(parseProviderQuotaClockReset("resets Sept 1, 9am (UTC)", now)).toEqual(new Date("2026-09-01T09:00:00Z")); + expect(parseProviderQuotaClockReset("resets Nov 10, 2027 3pm (UTC)", now)).toBeNull(); + }); + + it.each([ + "resets Apr 31, 3pm (UTC)", + "resets Feb 29, 2027 3pm (UTC)", + "resets Nov 10, 2026", + "resets Nov 10, 2026 25pm (UTC)", + "resets Nov 0, 3pm (UTC)", + "resets Nov 10, 13pm (UTC)", + "resets Nov 10, 3:60pm (UTC)", + "resets Novembork 10, 3pm (UTC)", + "resets Nov 10, 3pm (Not/AZone)", + ])("rejects the invalid dated reset %s", (wording) => { + expect(parseProviderQuotaClockReset(wording, new Date("2026-11-07T00:50:37Z"))).toBeNull(); + }); + + it("keeps clock-only resets on the next matching wall clock", () => { + const now = new Date("2026-11-07T00:50:37Z"); + expect(parseProviderQuotaClockReset("You've hit your session limit · resets 2am (UTC)", now)) + .toEqual(new Date("2026-11-07T02:00:00Z")); + expect(parseProviderQuotaClockReset("try again at 4:30 PM (America/Chicago)", now)) + .toEqual(new Date("2026-11-07T22:30:00Z")); + expect(parseProviderQuotaClockReset("resets 12am", now)).toEqual(new Date("2026-11-08T00:00:00Z")); + }); + + it("returns null when no reset is stated", () => { + expect(parseProviderQuotaClockReset("ACP agent reported a terminal limit failure.", new Date())).toBeNull(); + }); +}); diff --git a/server/src/services/provider-quota-reset.ts b/server/src/services/provider-quota-reset.ts index 25e275ce9e0..c158244175a 100644 --- a/server/src/services/provider-quota-reset.ts +++ b/server/src/services/provider-quota-reset.ts @@ -1,9 +1,11 @@ const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; +const MONTH_NAMES = "jan(?:uary)?|feb(?:ruary)?|mar(?:ch)?|apr(?:il)?|may|june?|july?|aug(?:ust)?|sep(?:t(?:ember)?)?|oct(?:ober)?|nov(?:ember)?|dec(?:ember)?"; +const MAX_DATED_RESET_AHEAD_MS = 45 * 24 * 60 * 60_000; const RESET_PATTERN = new RegExp( "(?:try again at|resets?(?:\\s+(?:at|on))?)\\s+" + // Optional calendar date, as in "resets Nov 12, 6pm (UTC)" for a weekly limit. - `(?:(${MONTHS.join("|")})[a-z]*\\.?\\s+(\\d{1,2})(?:st|nd|rd|th)?,?\\s+(?:at\\s+)?)?` + - "(\\d{1,2})(?::(\\d{2}))?\\s*(?:([ap])\\.?\\s*m\\.?)?(?:\\s*\\(([^)]+)\\)|\\s+([A-Z]{2,5}))?", + `(?:(${MONTH_NAMES})\\b\\.?\\s+(\\d{1,2})(?:st|nd|rd|th)?(?:,?\\s+(\\d{4}))?,?\\s+(?:at\\s+)?)?` + + "(\\d{1,2})(?::(\\d{2}))?\\s*(?:([ap])\\.?\\s*m\\.?)?(?![\\d:])(?:\\s*\\(([^)]+)\\)|\\s+([A-Z]{2,5}))?", "i", ); @@ -20,10 +22,11 @@ export function parseProviderQuotaClockReset(error: string, now: Date) { const monthIndex = match[1] ? MONTHS.indexOf(match[1].slice(0, 3).toLowerCase()) : null; const dayOfMonth = match[2] ? Number.parseInt(match[2], 10) : null; + const statedYear = match[3] ? Number.parseInt(match[3], 10) : null; if (monthIndex !== null && (monthIndex < 0 || !dayOfMonth || dayOfMonth < 1 || dayOfMonth > 31)) return null; - const hourValue = Number.parseInt(match[3] ?? "", 10); - const minute = Number.parseInt(match[4] ?? "0", 10); - const meridiem = (match[5] ?? "").toLowerCase(); + const hourValue = Number.parseInt(match[4] ?? "", 10); + const minute = Number.parseInt(match[5] ?? "0", 10); + const meridiem = (match[6] ?? "").toLowerCase(); if (!Number.isInteger(hourValue)) return null; if ( meridiem ? hourValue < 1 || hourValue > 12 : hourValue < 0 || hourValue > 23 @@ -33,7 +36,7 @@ export function parseProviderQuotaClockReset(error: string, now: Date) { let hour = meridiem ? hourValue % 12 : hourValue; if (meridiem === "p") hour += 12; - const timeZone = (match[6] ?? match[7])?.trim() || "UTC"; + const timeZone = (match[7] ?? match[8])?.trim() || "UTC"; try { const wallClock = (date: Date) => @@ -79,12 +82,13 @@ export function parseProviderQuotaClockReset(error: string, now: Date) { // Dec 30 is next year's. A date read after it passed stays in the past, // so a caller reading an old message sees an expired reset rather than // one a year away. - const candidates = [year - 1, year, year + 1] + const candidates = (statedYear === null ? [year - 1, year, year + 1] : [statedYear]) .map((candidateYear) => instant(candidateYear, monthIndex, dayOfMonth)) .filter((candidate): candidate is Date => candidate !== null); if (!candidates.length) return null; - return candidates.reduce((nearest, candidate) => + const nearest = candidates.reduce((nearest, candidate) => Math.abs(candidate.getTime() - now.getTime()) < Math.abs(nearest.getTime() - now.getTime()) ? candidate : nearest); + return nearest.getTime() > now.getTime() + MAX_DATED_RESET_AHEAD_MS ? null : nearest; } const month = Number(nowParts.month) - 1; const day = Number(nowParts.day); diff --git a/server/src/services/run-failure-report.ts b/server/src/services/run-failure-report.ts index 1e0ceb7d4cc..79b8b2805aa 100644 --- a/server/src/services/run-failure-report.ts +++ b/server/src/services/run-failure-report.ts @@ -21,6 +21,7 @@ import { PLUGIN_RUNNER_AI_CONNECTION_REQUIRED } from "./plugin-runner-ai-connect import { NATIVE_PROVIDER_OVERLOADED_CODE } from "./native-runtime/native-provider-failure.js"; import { PROJECT_ACCESS_DENIED } from "./project-access-denial.js"; import { REVIEWED_CHAT_BINDING_NOT_READY } from "./reviewed-chat-binding-diagnostics.js"; +import { readProviderFailure } from "@paperclipai/shared"; type HeartbeatRun = typeof heartbeatRuns.$inferSelect; @@ -259,6 +260,14 @@ function isProviderQuotaFailure(run: HeartbeatRun, options: RunFailureReportOpti options.phase !== "setup" && run.signal == null; } +/** A typed native allowance failure stays on the task and its cooldown path. */ +function isNativeProviderUsageLimitFailure(run: HeartbeatRun, options: RunFailureReportOptions): boolean { + if (run.status !== "failed" || run.runtimeMode !== "native" || + run.errorCode !== "native_provider_usage_limit" || options.phase === "setup" || run.signal != null) return false; + const category = readProviderFailure(asRecord(run.resultJson)?.providerFailure)?.category; + return category === "quota_exhausted" || category === "rate_limit"; +} + function isCodexAcpModelCapacityFailure(run: HeartbeatRun, options: RunFailureReportOptions): boolean { const result = asRecord(run.resultJson); return run.status === "failed" && run.errorCode === "codex_transient_upstream" && @@ -364,6 +373,7 @@ export function reportRunFailure(db: Db, run: HeartbeatRun, options: RunFailureR isProjectAccessDenialBlocker(run, options) || isGitConnectionFailure(run, options) || isProviderQuotaFailure(run, options) || + isNativeProviderUsageLimitFailure(run, options) || isCodexAcpModelCapacityFailure(run, options) || isAcpProviderAccessFailure(run, options) || isScheduledReviewedChatRetry(run, options) || From b0233b5b3322abb82ef8a59c7c72f5e91e85d5db Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:07:31 -0600 Subject: [PATCH 02/21] fix(native): count a parent run's read of a finished child as delivery of its result (#21227) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - A parent task gets a wake when a child task completes. If the parent is running, that wake waits behind the run. > - At finalization, a parent's done claim stays open (`native_child_completion_pending`) while such a wake is still owed, so the parent never finishes without the child result. > - The server had no record of what a running parent actually read. A parent that read its finished children mid-turn and then finished the task still kept the wake owed. > - The task stayed `in_progress`, the wake started a second parent turn, and the agent wrote a second completion summary. > - This pull request records a run's read of a finished child and counts that read as delivery of the child result. > - The benefit is one completion turn when the parent already read its children, while unread child results still get a fresh turn. ## Linked Issues or Issue Description Refs #21189 (child results saved before the run started). Refs #21175 (the same pattern for comments: `task_history.read`). Refs #15372 (the pending child-completion guard). **What happened?** Restart-chaos wave `w-20261112T223803-s7101`, on a master build that includes #21189, task t006 (OpenCode, native runner, staging-mix parent with two subtasks): - Parent run `bfe9941c` started at 22:55:56. - Its children CHA-11 and CHA-12 finished at 23:07:53 and 23:11:38. - The child-completion intent and its deferred dispatch row were saved at 23:11:38, after the run started. So the #21189 rule counted them as new input. - At 23:26:41 the run's reasoning says "Both subtask ledgers exist … Both subtasks are done". The run then saved the CHILD hashes and posted DONE at 23:33:39. - At finalization, the done claim was committed as `native_child_completion_pending`. The deferred wake then started run `40928116`, which wrote a second completion summary. Root cause: `hasPendingNativeChildCompletion()` (`server/src/services/native-runtime/native-child-completion-delivery.ts`) proved delivery only by wake binding and timestamps. No durable record showed that the finishing run had read the completed child. **Expected behavior** A finishing run's recorded read of a completed child counts as that child result being delivered. A read while the child was open, a read by another run, or a failed event write does not count. An unread child result still keeps the task open and gets one fresh parent turn. **Steps to reproduce** 1. Start a native parent run. 2. Complete a child while the run is active. The child-completion wake waits behind the run. 3. Let the run read the child (`get_task_context`, or `GET /api/issues/:childId`), then claim `done`. 4. Finalize and release the run. On `master`, the decision is `native_child_completion_pending` and the wake becomes a second run. The same wave's t001 grade ("duplicate completion") is not this bug. That run only said it *will* post the DONE marker later, and the grader counts any mention. Its `subtasks_once` failure comes from the restart-recovery continuation. This PR does not change either one. ## What Changed - `native-child-completion-delivery.ts`: - New `recordChildTaskRead()` writes a `child_task.read` run-log event. The payload is `{ parentIssueId, children: { id: { status, updatedAt } } }`, and it lists only children in `done`. - `hasPendingNativeChildCompletion()` no longer counts a child-completion wake as pending when the finishing run recorded a read of that wake's completed child in `done` at or after the child's completion decision (`status_decisions.created_at`, or the wake's `createdAt` when the decision row is missing). - The #21189 start-time rule is unchanged. - `paperclip-runner-tool-authority.ts`: `get_task_context` records the finished child tasks it returns. A failed write is logged and leaves the result owed. - `routes/issues.ts`: `GET /issues/:id`, `GET /issues/:id/documents` and `GET /issues/:id/documents/:key` record the read when the request has all of these: - an agent actor with a run token - a child issue in `done` This runs after the existing access and company checks. So a board user, another company's agent, or a read of an open task writes nothing. A qualifying read adds one statement: the existing `appendHeartbeatRunEvent` fast path, a single `update … returning` / `insert` CTE bound to the run's id, company and agent. Every other read adds no query. - `doc/run-log-events.md`: documents `child_task.read` and how delivery uses it. - Tests: a finalization-to-release regression in `heartbeat-process-recovery.test.ts` and a new `native-child-task-read-routes.test.ts`. ## Verification - RED on `master`: in `heartbeat-process-recovery.test.ts` "counts a child result as read by the finishing parent run (saved after the run started, read: after the child finished)", the run reads the child through the real `get_task_context`, then finalization and the wake-queue release run. - On master: `native_child_completion_pending`, task `in_progress`, and the wake is promoted into a second run, the same as t006. - With this change: `done`, the wake is cancelled, and there is one run. - The other cases in the same test behave the same before and after this change: - saved before the run started: done - not read: pending, fresh turn with the child results - read while the child was open: pending - `native-child-task-read-routes.test.ts` (5 tests): - An agent read of a done child through each of the three routes records the read and clears the pending result. - An open child records nothing. - A board read records nothing. - Another company's agent gets no 200 and records nothing. - Neighbouring suites after merging `origin/master`: `heartbeat-process-recovery`, `muse-control`, `heartbeat-held-comment-wake`, `native-status-arbiter-corpus`, `heartbeat-dependency-scheduling`, `paperclip-runner-tool-authority`, `agent-task-hierarchy-depth-routes`, and the two new test files: 8 files, 982/982 tests pass. - `pnpm --filter @paperclipai/server typecheck` passes. **Not verified** - No chaos wave has run on this change yet. - In t006, the parent's tool calls fell in the 4,220 run events that the evidence omitted. Its reasoning text proves the read, but the exact tool (`get_task_context`, `get_task`, `read_task_document` or `call_api`) is not visible. - A read through `search_tasks`, or through a shell command that calls the API without the run token, is not recorded. Such a result stays owed and gets one fresh turn, which is the old behavior. ## Risks - A recorded read of a done child now lets the parent finish without another turn. If the agent read the child but ignored it, no second turn reminds it. A read while the child was still open never counts. - Each qualifying agent read of a done child appends one run-log row. Board reads and reads of open tasks write nothing. - No migration. Wakes saved by older builds work unchanged. Without a recorded read, the old behavior applies. - This is a run-log change only (instance database). It adds no Telemetry or OpenTelemetry export. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, via Claude Code with tool use (shell, file edits, test runs). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 --- doc/run-log-events.md | 23 ++++ .../heartbeat-process-recovery.test.ts | 28 +++- .../native-child-task-read-routes.test.ts | 127 ++++++++++++++++++ server/src/routes/issues.ts | 12 ++ .../native-child-completion-delivery.ts | 32 ++++- .../paperclip-runner-tool-authority.ts | 4 + 6 files changed, 219 insertions(+), 7 deletions(-) create mode 100644 server/src/__tests__/native-child-task-read-routes.test.ts diff --git a/doc/run-log-events.md b/doc/run-log-events.md index 5615b6851a4..be2c7c8d1e5 100644 --- a/doc/run-log-events.md +++ b/doc/run-log-events.md @@ -1024,6 +1024,29 @@ comments one run read, with each read's time and the comment revision it saw, whatever the run's outcome. A failure to write the event leaves the comment owed in the same way. +When a run reads a finished child task, the server writes one +`child_task.read` event (`stream: "system"`, level `info`) to that run's log. +The reads that count are `get_task_context` on the parent task, and a +successful agent request with a run token for `GET /api/issues/:id`, +`GET /api/issues/:id/documents` or `GET /api/issues/:id/documents/:key` of a +child in `done`. The route writes the event only after the requested content +has loaded, so a 400 or 404 records nothing. The payload has `parentIssueId` +and `children`, which maps each finished child ID to the revision the run +received: its `status`, `updatedAt` and `statusDecisionId`. It contains no +titles, summaries or document bodies. The event creates no Telemetry or +OpenTelemetry export. + +`hasPendingNativeChildCompletion` reads this event. A child-completion wake no +longer keeps the parent's done claim open when the finishing run received that +child in `done` at the completion the wake reports: the received +`statusDecisionId` is the wake's completion decision, or the received +`updatedAt` is at or after that decision was saved. The event's own write time +does not count, so a delayed write of an older revision cannot deliver a newer +completion. The wake is then cancelled at release instead of starting another +parent turn. A read while the child was still open, a read of an older +revision, a read by another run, or a failure to write the event leaves the +child result owed. + ## Managed Agent File Save Receipts The server writes `instruction_save` after managed file collection or a warm diff --git a/server/src/__tests__/heartbeat-process-recovery.test.ts b/server/src/__tests__/heartbeat-process-recovery.test.ts index ecb8058cfb0..b3b03482d04 100644 --- a/server/src/__tests__/heartbeat-process-recovery.test.ts +++ b/server/src/__tests__/heartbeat-process-recovery.test.ts @@ -265,6 +265,7 @@ import { } from "../services/native-runtime/native-restart-recovery.ts"; import { claimNativeSessionResumptions } from "../services/native-runtime/native-finalization-reconciler.ts"; import { PaperclipControlPlanePort } from "../services/native-runtime/paperclip-control-plane-port.js"; +import { PaperclipRunnerToolAuthority } from "../services/native-runtime/paperclip-runner-tool-authority.js"; import { finalizeNativeRun } from "../services/native-runtime/native-run-finalizer.js"; import { createWakeQueue } from "../modules/wake-queue/index.js"; import { recordNativeAttentionAssessment } from "../services/native-runtime/work-assessments.js"; @@ -15479,11 +15480,15 @@ describeEmbeddedPostgres("heartbeat orphaned process recovery", () => { }); it.each([ - ["before", "done", "completion_claim_policy_accepted"], - ["after", "in_progress", "native_child_completion_pending"], - ] as const)("treats a child result saved %s the parent run started as read when the run finishes the task", async ( - savedRelativeToStart, expectedStatus, expectedReason, - ) => { + { saved: "before", read: "none", delivered: true }, + { saved: "after", read: "none", delivered: false }, + { saved: "after", read: "after the child finished", delivered: true }, + { saved: "after", read: "while the child was open", delivered: false }, + ] as const)("counts a child result as read by the finishing parent run (saved $saved the run started, read: $read)", async ({ + saved: savedRelativeToStart, read, delivered, + }) => { + const expectedStatus = delivered ? "done" : "in_progress"; + const expectedReason = delivered ? "completion_claim_policy_accepted" : "native_child_completion_pending"; const fixture = await seedStrandedIssueFixture({ status: "in_progress", runStatus: "succeeded", livenessState: "advanced" }); const { companyId, agentId, issueId, runId } = fixture; const childId = randomUUID(); @@ -15516,6 +15521,17 @@ describeEmbeddedPostgres("heartbeat orphaned process recovery", () => { startedAt: runStartedAt, finishedAt: new Date(), }).where(eq(heartbeatRuns.id, runId)); await db.update(issues).set({ executionRunId: runId, executionLockedAt: runStartedAt }).where(eq(issues.id, issueId)); + if (read !== "none") { + if (read === "while the child was open") await db.update(issues).set({ status: "in_progress" }).where(eq(issues.id, childId)); + await db.update(heartbeatRuns).set({ status: "running", finishedAt: null }).where(eq(heartbeatRuns.id, runId)); + const reader = new PaperclipRunnerToolAuthority(db, { companyId, agentId, issueId, runId }); + try { + const context = await reader.execute({ tool: "get_task_context", callId: `context-${runId}`, arguments: {} }); + expect((context as { childTasks: Array<{ id: string }> }).childTasks.map(task => task.id)).toEqual([childId]); + } finally { reader.close(); } + await db.update(issues).set({ status: "done" }).where(eq(issues.id, childId)); + await db.update(heartbeatRuns).set({ status: "succeeded", finishedAt: new Date() }).where(eq(heartbeatRuns.id, runId)); + } await db.insert(issueComments).values({ companyId, issueId, authorType: "agent", authorAgentId: agentId, createdByRunId: runId, body: "Both children are done; merged their ledgers. Task complete." }); await new PaperclipControlPlanePort(db, { @@ -15550,7 +15566,7 @@ describeEmbeddedPostgres("heartbeat orphaned process recovery", () => { .toEqual([expect.objectContaining({ status: expectedStatus })]); const runs = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.companyId, companyId)); const [wake] = await db.select().from(agentWakeupRequests).where(eq(agentWakeupRequests.id, childResult!.id)); - if (savedRelativeToStart === "before") { + if (delivered) { expect(wake).toMatchObject({ status: "cancelled", runId: null }); expect(runs).toHaveLength(1); } else { diff --git a/server/src/__tests__/native-child-task-read-routes.test.ts b/server/src/__tests__/native-child-task-read-routes.test.ts new file mode 100644 index 00000000000..36fde389b0a --- /dev/null +++ b/server/src/__tests__/native-child-task-read-routes.test.ts @@ -0,0 +1,127 @@ +import { randomUUID } from "node:crypto"; +import express from "express"; +import request from "supertest"; +import { and, eq } from "drizzle-orm"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { agentWakeupRequests, agents, companies, createDb, heartbeatRunEvents, heartbeatRuns, issues } from "@paperclipai/db"; +import { getEmbeddedPostgresTestSupport, startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js"; +import { issueRoutes } from "../routes/issues.js"; +import { errorHandler } from "../middleware/index.js"; +import { documentService } from "../services/documents.js"; +import { CHILD_TASK_READ_EVENT, hasPendingNativeChildCompletion } from "../services/native-runtime/native-child-completion-delivery.js"; + +const support = await getEmbeddedPostgresTestSupport(); +const describePostgres = support.supported ? describe : describe.skip; + +describePostgres("a parent run reading a finished child through the issue routes", () => { + let db: ReturnType; + let database: Awaited>; + const companyId = randomUUID(); + const agentId = randomUUID(); + const otherCompanyId = randomUUID(); + const otherAgentId = randomUUID(); + const otherRunId = randomUUID(); + + beforeAll(async () => { + database = await startEmbeddedPostgresTestDatabase("paperclip-native-child-read-routes-"); + db = createDb(database.connectionString); + await db.insert(companies).values({ id: companyId, name: "Paperclip", issuePrefix: "CHR", defaultResponsibleUserId: "board-user", + requireBoardApprovalForNewAgents: false }); + await db.insert(agents).values({ id: agentId, companyId, name: "Coder", role: "engineer", adapterType: "codex_local", status: "active" }); + await db.insert(companies).values({ id: otherCompanyId, name: "Other", issuePrefix: "OTH", defaultResponsibleUserId: "board-user", + requireBoardApprovalForNewAgents: false }); + await db.insert(agents).values({ id: otherAgentId, companyId: otherCompanyId, name: "Outsider", role: "engineer", + adapterType: "codex_local", status: "active" }); + await db.insert(heartbeatRuns).values({ id: otherRunId, companyId: otherCompanyId, agentId: otherAgentId, status: "running", + runtimeMode: "native", startedAt: new Date(), contextSnapshot: {} }); + }, 30_000); + afterAll(async () => { await database?.cleanup(); }); + + function app(actor: Express.Request["actor"]) { + const instance = express(); + instance.use(express.json()); + instance.use((req, _res, next) => { req.actor = actor; next(); }); + instance.use("/api", issueRoutes(db, {} as never)); + instance.use(errorHandler); + return instance; + } + const agentActor = (runId: string) => ({ type: "agent", companyId, agentId, runId, source: "agent_jwt", + onBehalfOfUserId: null, onBehalfOfMemberships: [] }) as Express.Request["actor"]; + const boardActor = { type: "board", userId: "board-user", source: "local_implicit", isInstanceAdmin: true } as Express.Request["actor"]; + + async function seed(childStatus: "done" | "in_progress", issueNumber: number, options: { childRevisionAgeMs?: number; wakeAgeMs?: number } = {}) { + const runId = randomUUID(); + const childId = randomUUID(); + const parentId = randomUUID(); + await db.insert(issues).values({ id: parentId, companyId, title: "Merge the ledgers", identifier: `CHR-${issueNumber + 100}`, + issueNumber: issueNumber + 100, status: "in_progress", assigneeAgentId: agentId }); + await db.insert(heartbeatRuns).values({ id: runId, companyId, agentId, status: "running", runtimeMode: "native", + nativeIssueId: parentId, startedAt: new Date(Date.now() - 60_000), contextSnapshot: { issueId: parentId } }); + await db.insert(issues).values({ id: childId, companyId, parentId, title: "Child ledger", identifier: `CHR-${issueNumber}`, + issueNumber, status: childStatus, assigneeAgentId: agentId, + ...(options.childRevisionAgeMs ? { updatedAt: new Date(Date.now() - options.childRevisionAgeMs) } : {}) }); + await documentService(db).upsertIssueDocument({ issueId: childId, key: "ledger", format: "markdown", body: "ROUND 1" }); + if (options.childRevisionAgeMs) await db.update(issues).set({ updatedAt: new Date(Date.now() - options.childRevisionAgeMs) }) + .where(eq(issues.id, childId)); + const [wake] = await db.insert(agentWakeupRequests).values({ companyId, agentId, source: "automation", triggerDetail: "system", + reason: "issue_execution_deferred", requestedByActorType: "system", requestedByActorId: `native-status-wake-dispatch:${randomUUID()}`, + status: "deferred_issue_execution", createdAt: new Date(Date.now() - (options.wakeAgeMs ?? 30_000)), + payload: { issueId: parentId, _paperclipWakeContext: { issueId: parentId, wakeReason: "issue_children_completed", + nativeChildCompletionDecisionId: randomUUID(), completedChildIssueId: childId } } }).returning(); + return { runId, childId, parentId, wakeId: wake!.id }; + } + const reads = (runId: string) => db.select().from(heartbeatRunEvents) + .where(and(eq(heartbeatRunEvents.runId, runId), eq(heartbeatRunEvents.eventType, CHILD_TASK_READ_EVENT))); + const pending = (f: { runId: string; parentId: string }) => + hasPendingNativeChildCompletion(db, { companyId, issueId: f.parentId, agentId, runId: f.runId, sourceIntentId: null }); + + it.each(["", "/documents", "/documents/ledger"])("records the parent run's read of a finished child (GET /issues/:id%s)", async (suffix) => { + const f = await seed("done", 10 + suffix.length); + expect(await pending(f)).toBe(true); + expect((await request(app(agentActor(f.runId))).get(`/api/issues/${f.childId}${suffix}`)).status).toBe(200); + expect(await reads(f.runId)).toEqual([expect.objectContaining({ payload: { + parentIssueId: f.parentId, children: { [f.childId]: { status: "done", updatedAt: expect.any(String), statusDecisionId: null } } } })]); + expect(await pending(f)).toBe(false); + }); + + it("does not count a read of a child that is still open, or a board read", async () => { + const open = await seed("in_progress", 40); + expect((await request(app(agentActor(open.runId))).get(`/api/issues/${open.childId}`)).status).toBe(200); + expect(await reads(open.runId)).toEqual([]); + expect(await pending(open)).toBe(true); + + const board = await seed("done", 41); + expect((await request(app(boardActor)).get(`/api/issues/${board.childId}`)).status).toBe(200); + expect(await reads(board.runId)).toEqual([]); + expect(await pending(board)).toBe(true); + }); + + it("does not record a read by another company's agent", async () => { + const f = await seed("done", 42); + const outsider = { type: "agent", companyId: otherCompanyId, agentId: otherAgentId, runId: otherRunId, source: "agent_jwt", + onBehalfOfUserId: null, onBehalfOfMemberships: [] } as Express.Request["actor"]; + for (const suffix of ["", "/documents", "/documents/ledger"]) { + expect((await request(app(outsider)).get(`/api/issues/${f.childId}${suffix}`)).status).not.toBe(200); + } + expect(await db.select().from(heartbeatRunEvents).where(eq(heartbeatRunEvents.eventType, CHILD_TASK_READ_EVENT)) + .then(rows => rows.filter(row => row.runId === otherRunId || row.companyId === otherCompanyId))).toEqual([]); + expect(await pending(f)).toBe(true); + }); + + it.each([ + ["an invalid document key", "/documents/Not A Key!", 400], + ["a missing document", "/documents/missing", 404], + ])("does not count a failed request for %s as a read", async (_shape, suffix, status) => { + const f = await seed("done", 43 + suffix.length); + expect((await request(app(agentActor(f.runId))).get(`/api/issues/${f.childId}${encodeURI(suffix)}`)).status).toBe(status); + expect(await reads(f.runId)).toEqual([]); + expect(await pending(f)).toBe(true); + }); + + it("does not let a read of an older finished revision deliver a newer completion", async () => { + const f = await seed("done", 70, { childRevisionAgeMs: 60_000, wakeAgeMs: 10_000 }); + expect((await request(app(agentActor(f.runId))).get(`/api/issues/${f.childId}`)).status).toBe(200); + expect(await reads(f.runId)).toHaveLength(1); + expect(await pending(f)).toBe(true); + }); +}); diff --git a/server/src/routes/issues.ts b/server/src/routes/issues.ts index ce82b68095b..f4c944e3d5b 100644 --- a/server/src/routes/issues.ts +++ b/server/src/routes/issues.ts @@ -24,6 +24,7 @@ import { listIssueStatusBlockerAttention } from "../services/issue-status-presen import { assertNoCaseHandoffMutation } from "../services/heartbeat/case-handoff-lifecycle.js"; import { assertHandedOffRunTaskOwnership } from "../run-task-ownership.js"; import { recordNativeHumanSelfHandoff } from "../services/native-runtime/native-human-self-handoff.js"; +import { recordChildTaskRead } from "../services/native-runtime/native-child-completion-delivery.js"; import { runRetainsIssueIdentity } from "../services/task-repository-acquisition.js"; import { withConversationReadState } from "../services/agent-chat-read-state.js"; import { markIssueReadSchema } from "@paperclipai/shared"; @@ -3611,6 +3612,13 @@ function logIssueListRequest(input: { }); } +async function recordAgentChildTaskRead(db: Db, req: Request, issue: { id: string; companyId: string; parentId: string | null; status: string; updatedAt: Date; lastStatusDecisionId?: string | null }) { + if (req.actor.type !== "agent" || !req.actor.agentId || !req.actor.runId || !issue.parentId || issue.status !== "done") return; + await recordChildTaskRead(db, { companyId: issue.companyId, agentId: req.actor.agentId, runId: req.actor.runId, + parentIssueId: issue.parentId, children: [issue] }) + .catch(error => logger.warn({ err: error, runId: req.actor.runId }, "could not record the child task a run read")); +} + export function issueRoutes( db: Db, storage: StorageService, @@ -10079,6 +10087,7 @@ export function issueRoutes( // and related-task enrichment. This never refreshes external providers. if (req.query.view === "core") { const core = await readIssueCore(issue, timing.time); + await recordAgentChildTaskRead(db, req, issue); res.setHeader("Server-Timing", timing.header()); res.json(core); return; @@ -10160,6 +10169,7 @@ export function issueRoutes( const visibleMentionedProjects = mentionedProjects.filter((_, index) => mentionedProjectVisibility[index]); // Recovery revalidation may change the blocker; read it afterwards. const executionBlocker = await timing.time("execution_blocker", () => getExecutionBlocker(db, issue.companyId, issue.id)); + await recordAgentChildTaskRead(db, req, issue); res.setHeader("Server-Timing", timing.header()); res.json({ ...issue, @@ -11366,6 +11376,7 @@ export function issueRoutes( const docs = await documentsSvc.listIssueDocuments(issue.id, { includeSystem: req.query.includeSystem === "true", }); + await recordAgentChildTaskRead(db, req, issue); res.json(docs); }); @@ -11399,6 +11410,7 @@ export function issueRoutes( res.status(404).json({ error: "Document not found" }); return; } + await recordAgentChildTaskRead(db, req, issue); if (!shouldIncludeDocumentAnnotations(req)) { res.json(doc); return; diff --git a/server/src/services/native-runtime/native-child-completion-delivery.ts b/server/src/services/native-runtime/native-child-completion-delivery.ts index 2d0b79f1160..0ae2377fd5c 100644 --- a/server/src/services/native-runtime/native-child-completion-delivery.ts +++ b/server/src/services/native-runtime/native-child-completion-delivery.ts @@ -1,5 +1,6 @@ import { and, eq, inArray, isNull, ne, or, sql } from "drizzle-orm"; -import { agentWakeupRequests, heartbeatRuns, type Db } from "@paperclipai/db"; +import { agentWakeupRequests, heartbeatRunEvents, heartbeatRuns, statusDecisions, type Db } from "@paperclipai/db"; +import { appendHeartbeatRunEvent } from "../heartbeat-run-events.js"; export interface NativeChildCompletionRecipient { companyId: string; @@ -9,8 +10,25 @@ export interface NativeChildCompletionRecipient { sourceIntentId: string | null; } +export const CHILD_TASK_READ_EVENT = "child_task.read"; + +export async function recordChildTaskRead(db: Db, input: { + companyId: string; agentId: string; runId: string; parentIssueId: string; + children: ReadonlyArray<{ id: string; parentId: string | null; status: string; updatedAt: Date; lastStatusDecisionId?: string | null }>; +}): Promise { + const completed = input.children.filter(child => child.parentId === input.parentIssueId && child.status === "done"); + if (completed.length === 0) return; + await appendHeartbeatRunEvent(db, { companyId: input.companyId, runId: input.runId, agentId: input.agentId, + eventType: CHILD_TASK_READ_EVENT, stream: "system", level: "info", + payload: { parentIssueId: input.parentIssueId, + children: Object.fromEntries(completed.map(child => [child.id, { status: child.status, updatedAt: child.updatedAt.toISOString(), + statusDecisionId: child.lastStatusDecisionId ?? null }])) } }); +} + /** New child results belong to a fresh parent turn, including across a Done claim. */ export async function hasPendingNativeChildCompletion(db: Db, input: NativeChildCompletionRecipient) { + const completedChildId = sql`coalesce(${agentWakeupRequests.payload}->>'completedChildIssueId', + ${agentWakeupRequests.payload}->'_paperclipWakeContext'->>'completedChildIssueId')`; const [pending] = await db.select({ id: agentWakeupRequests.id }).from(agentWakeupRequests).where(and( eq(agentWakeupRequests.companyId, input.companyId), eq(agentWakeupRequests.agentId, input.agentId), @@ -26,6 +44,18 @@ export async function hasPendingNativeChildCompletion(db: Db, input: NativeChild or(isNull(agentWakeupRequests.runId), ne(agentWakeupRequests.runId, input.runId)), sql`${agentWakeupRequests.createdAt} >= coalesce((select ${heartbeatRuns.startedAt} from ${heartbeatRuns} where ${heartbeatRuns.id} = ${input.runId} and ${heartbeatRuns.companyId} = ${input.companyId}), '-infinity'::timestamptz)`, + sql`not exists (select 1 from ${heartbeatRunEvents} child_read + where child_read.company_id = ${input.companyId} and child_read.run_id = ${input.runId} + and child_read.event_type = ${CHILD_TASK_READ_EVENT} + and child_read.payload->>'parentIssueId' = ${input.issueId} + and child_read.payload->'children'->(${completedChildId})->>'status' = 'done' + and (child_read.payload->'children'->(${completedChildId})->>'statusDecisionId' + = ${agentWakeupRequests.payload}->'_paperclipWakeContext'->>'nativeChildCompletionDecisionId' + or (child_read.payload->'children'->(${completedChildId})->>'updatedAt')::timestamptz + >= date_trunc('milliseconds', coalesce((select ${statusDecisions.createdAt} from ${statusDecisions} + where ${statusDecisions.companyId} = ${input.companyId} + and ${statusDecisions.id}::text = ${agentWakeupRequests.payload}->'_paperclipWakeContext'->>'nativeChildCompletionDecisionId'), + ${agentWakeupRequests.createdAt}))))`, input.sourceIntentId ? ne(agentWakeupRequests.id, input.sourceIntentId) : undefined, )).limit(1); return Boolean(pending); diff --git a/server/src/services/native-runtime/paperclip-runner-tool-authority.ts b/server/src/services/native-runtime/paperclip-runner-tool-authority.ts index 96cbf77c25f..9e6495fdc06 100644 --- a/server/src/services/native-runtime/paperclip-runner-tool-authority.ts +++ b/server/src/services/native-runtime/paperclip-runner-tool-authority.ts @@ -139,6 +139,7 @@ import { type NativeChatAttachmentReadScope, } from "./chat-attachment-read.js"; import { recordTaskHistoryRead } from "../queued-comment-delivery.js"; +import { recordChildTaskRead } from "./native-child-completion-delivery.js"; import { logger } from "../../middleware/logger.js"; const EXTERNAL_WORKSPACE_OPERATIONS = new Set(["get_workspace", "list_workspaces", "select_workspace", "prepare_repository", "register_deliverable"]); @@ -947,6 +948,9 @@ export class PaperclipRunnerToolAuthority { const readableChildren = this.binding.museRuntime ? await this.#readableMuseTaskIds(context.run, childTasks.map(task => task.id)) : null; const visibleChildTasks = readableChildren ? childTasks.filter(task => readableChildren.has(task.id)) : childTasks; + await recordChildTaskRead(this.db, { companyId: this.binding.companyId, agentId: this.binding.agentId, + runId: this.binding.runId, parentIssueId: this.binding.issueId, children: visibleChildTasks.slice(0, 100) }).catch(error => + logger.warn({ err: error, runId: this.binding.runId }, "could not record the child tasks a run read")); return { company: { id: this.binding.companyId }, actor: redactedActor(context.actor), From 2de5d9795818b09fa3480d570e70b1947e9b9091 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:16:23 -0600 Subject: [PATCH 03/21] fix(startup): report ready without waiting for backlog first passes (#21247) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - A hosted instance upgrades by starting a new server beside the old server. The deployer sends traffic to the new server when `GET /api/ready` returns 200. > - `/api/ready` returns 200 only after the boot sequence in `server/src/index.ts` sets the phase to "ready". > - The boot sequence awaited the first pass of many delivery queues. Each pass grows with saved backlog or waits on outside services. > - On a busy instance, these passes took more than the deploy's health-check window. The deployer killed two new servers in a row before they became ready, including a rollback to an image that had started in 86 s one hour before. > - This pull request moves those first passes off the readiness path. Readiness now waits only on named, bounded preconditions, and each step reports its duration. > - The benefit: an upgrade or rollback becomes ready in boot time, not in backlog time. A slow start also names its slow step in one log line. ## Linked Issues or Issue Description **What happened?** On the hosted staging instance, two replacement servers did not report `/api/ready` and were killed after about 9 minutes. The first was a new image and the second was a rollback to the old image. At that time the instance had about 200 queued runs, about 80 running runs, a slow database, and many continuation runs. The failed server logged "managed environments ensured from managed config" (`index.ts` line 1284 on master). It never logged "worktree run-execution cutoff state" (line 1726), which the server always logs when the heartbeat service exists. Between those two lines, boot awaited only these steps: `tool_reviews` (four tool-review queue first passes), `gateway_sessions`, `delivery_workers` (budget enforcement, cost accounting, fast responses, chat completions, connection continuations, tool action receipts and question responses), the GitHub connection event and continuity workers, and `terminalWorkspaceWorker.ready`. Each step is a delivery-coordinator `register(...).ready`, and that promise resolves only after the queue's whole first pass. No queue except `mcp-client-mirror` (also failing on the healthy server) logged an error, so the cause was slow passes and not failures. The log had no per-step markers, so it does not show which of these awaits was still running at the kill. **Expected behavior** Readiness means "this server can serve requests and take over". It must not wait on work that grows with backlog or that depends on an outside service. Queues own that work and retry it on their own schedule after readiness. **Steps to reproduce** 1. Start the server while the first pass of any of the queues above does not finish. The new test in `server-startup-feedback-export.test.ts` does this for every queue. 2. On master, `startServer()` does not resolve and the phase stays "recovering". ## What Changed - `server/src/index.ts`: These first passes now start in the background as tracked scheduler work and do not hold readiness: tool reviews, gateway sessions, delivery workers, GitHub connection workers, the terminal workspace sweep, environment lease cleanup (scheduler-disabled path) and scheduled database backups. Shutdown and idle sleep still wait for them through `trackHeartbeatSchedulerWork`, and each queue keeps its retry schedule. - `server/src/index.ts`: The restart backup after an idle wake (a `pg_dump` whose time grows with the database) holds idle eligibility (`markIdleStartupComplete`), as before. It no longer holds readiness. - `server/src/index.ts`: Every remaining await on the boot path is a named `startupTimeline.precondition(...)`: `listen`, `runtime_control_operations`, `execution_policy`, `managed_environments`, `worktree_run_execution`, `scheduling_suppression`, `native_recovery`, `external_adapters` and `cloud_configuration`. These preconditions do not change. - `server/src/startup-timeline.ts`: Adds `precondition(step, work)` and `background(step, work)`. A precondition that is still running after 30 s is named in the warning "startup step is still holding readiness", which repeats every 30 s. A background step logs a warning once at 30 s and logs its duration when it finishes late. A background step that fails is logged with its step name. The line "startup timeline at readiness" now includes `readinessStepsMs`, `backgroundStepsMs` and `backgroundStepsOutstanding`. All timers are single unref'd timeouts and the module adds no queries. - `server/src/__tests__/startup-readiness-boundary.test.ts`: This guard test parses `index.ts` with `parseAst` from `vite`. The server already depends on `vite`, so this PR adds no dependency and no lockfile change. The test fails when an await on the boot path (between the phases "recovering" and "ready") is not a named precondition, or when the precondition names do not match a reviewed list that gives the reason for each name. - `server/src/services/terminal-workspace-work.ts`: Updates a comment. The first sweep no longer holds readiness, and it still skips compaction. ## Verification - Red/green regression test: `server-startup-feedback-export.test.ts`, "reports ready while backlog queues' first passes never finish". This test makes every first pass above never finish. With master `index.ts` it fails with `expected 'still starting' to be 'ready'` for both scheduler settings. With this change it passes. - Guard test: with master `index.ts`, `startup-readiness-boundary.test.ts` fails and lists 18 unnamed awaits, including `timeStartupRecovery("delivery_workers")` and `terminalWorkspaceWorker.ready`. With this change it passes. - `cd server && npx vitest run src/__tests__/server-startup-feedback-export.test.ts src/startup-timeline.test.ts src/__tests__/startup-readiness-boundary.test.ts src/startup-recovery-timing.test.ts src/__tests__/terminal-workspace-work.test.ts`: all tests pass. - `cd server && pnpm typecheck`: passes. - `pnpm verify` exited with code 2 on the first revision (a lockfile change that this PR no longer makes). Required CI supplies the broad coverage. - After deploy, run `grep "startup timeline at readiness"` on a server start. The line shows each precondition's duration and the background passes that are still running. ## Risks - The listed first passes now run at the same time as native run recovery and the requests that the server serves, not before them. Requests were already served during recovery. Commit-woken passes of the same queues, and the fresh queued-run dispatch, already ran at the same time as native recovery. The coordinator still serializes the passes of each queue. - `/api/ready` can now return 200 while a queue still has backlog. That is the intended meaning, and it matches the queued-run and recovery passes that already run after readiness (#21014). - Native run recovery still holds readiness, which is correct because adoption must happen before takeover. If that recovery is slow, the server now logs its step name every 30 s. - Low risk otherwise: no schema or API change. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Claude (Anthropic) via Claude Code, model ID `claude-opus-5-5` (commit trailer: Claude Fable 5.2). Tool use and code execution were enabled. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.2 --- .../server-startup-feedback-export.test.ts | 51 +++++++++++- .../startup-readiness-boundary.test.ts | 75 +++++++++++++++++ server/src/index.ts | 72 ++++++++++------ .../src/services/terminal-workspace-work.ts | 7 +- server/src/startup-timeline.test.ts | 81 ++++++++++++++++++ server/src/startup-timeline.ts | 83 +++++++++++++++++-- 6 files changed, 332 insertions(+), 37 deletions(-) create mode 100644 server/src/__tests__/startup-readiness-boundary.test.ts create mode 100644 server/src/startup-timeline.test.ts diff --git a/server/src/__tests__/server-startup-feedback-export.test.ts b/server/src/__tests__/server-startup-feedback-export.test.ts index 3da0d854021..d0816940929 100644 --- a/server/src/__tests__/server-startup-feedback-export.test.ts +++ b/server/src/__tests__/server-startup-feedback-export.test.ts @@ -1239,11 +1239,13 @@ describe("startServer feedback export wiring", () => { await markIdleBackupWakeRequired(directory); starting = startServer(); await vi.waitFor(() => expect(runDatabaseBackup).toHaveBeenCalledOnce()); + // The dump grows with the database: it holds idle eligibility, not readiness. + await starting; + expect(getStartupRecoveryState().phase).toBe("ready"); expect(eligible).not.toHaveBeenCalled(); expect(idleWorkSnapshot().active).toBeGreaterThan(0); finish(); - await starting; - expect(eligible).toHaveBeenCalledWith({ scheduledBackups: true }); + await vi.waitFor(() => expect(eligible).toHaveBeenCalledWith({ scheduledBackups: true })); const options = createAppMock.mock.calls[0]?.[1] as unknown as { prepareIdleDatabaseBackup?: unknown }; expect(Boolean(options.prepareIdleDatabaseBackup)).toBe(checkpointMode); expect(existsSync(idleBackupWakeMarker(directory))).toBe(fails); @@ -1668,6 +1670,51 @@ describe("startServer feedback export wiring", () => { expect(externalObjectsServiceMock.registerRefreshWork).toHaveBeenCalledOnce(); }); + // paperclip.staging, 2026-11-13: under a large backlog these first passes ran + // past the deploy health-check window, so two replacement servers in a row + // never reported ready and were killed. + it.each([true, false])("reports ready while backlog queues' first passes never finish (scheduler=%s)", async enabled => { + loadConfigMock.mockReturnValue(buildTestConfig({ heartbeatSchedulerEnabled: enabled })); + const held = new Set([ + DELIVERY_QUEUES.toolReviewExpiry, DELIVERY_QUEUES.toolReviewRecovery, DELIVERY_QUEUES.toolReviewInterruption, + DELIVERY_QUEUES.toolReviewProjection, DELIVERY_QUEUES.gatewayCredentialExpiry, + DELIVERY_QUEUES.budgetEnforcement, DELIVERY_QUEUES.accounting, DELIVERY_QUEUES.fastResponse, + DELIVERY_QUEUES.chatCompletion, DELIVERY_QUEUES.connection, DELIVERY_QUEUES.toolAction, DELIVERY_QUEUES.question, + DELIVERY_QUEUES.connectionEvents, DELIVERY_QUEUES.connectionContinuity, DELIVERY_QUEUES.terminalWorkspaces, + DELIVERY_QUEUES.environmentCleanup, DELIVERY_QUEUES.databaseBackups, + ]); + const original = deliveryWorkRegisterMock.getMockImplementation()!; + const releases: Array<() => void> = []; + deliveryWorkRegisterMock.mockImplementation((queue, task) => { + if (!held.has(queue)) return original(queue, task); + const pass = Promise.withResolvers(); + releases.push(pass.resolve); + return { ready: pass.promise, wake: vi.fn(), stop: vi.fn(async () => {}), + reconciliationSnapshot: () => ({ revision: `held-${queue}:1`, reconciled: false }) }; + }); + try { + const outcome = await Promise.race([ + startServer().then(() => "ready"), + new Promise(resolve => setTimeout(() => resolve("still starting"), 2_000)), + ]); + expect(outcome).toBe("ready"); + expect(getStartupRecoveryState().phase).toBe("ready"); + const readiness = vi.mocked(logger.info).mock.calls.find(([, message]) => message === "startup timeline at readiness"); + const fields = readiness![0] as unknown as { readinessStepsMs: Record; backgroundStepsOutstanding: string[] }; + expect(fields.backgroundStepsOutstanding).toEqual(expect.arrayContaining(enabled + ? ["tool_reviews", "gateway_sessions", "delivery_workers", "github_connections", "terminal_workspaces"] + // Without the scheduler, tool reviews and gateway sessions sweep once + // directly instead of registering a queue. + : ["delivery_workers", "github_connections", "environment_lease_cleanup"])); + expect(Object.keys(fields.readinessStepsMs)).toEqual(expect.arrayContaining(["listen", "external_adapters"])); + } finally { + deliveryWorkRegisterMock.mockImplementation(original); + // Held passes are tracked idle work; finish them so later tests start idle. + for (const release of releases) release(); + await settleStartupHousekeeping(); + } + }); + it("keeps external object refresh active when heartbeat scheduling is disabled", async () => { loadConfigMock.mockReturnValue(buildTestConfig({ heartbeatSchedulerEnabled: false, diff --git a/server/src/__tests__/startup-readiness-boundary.test.ts b/server/src/__tests__/startup-readiness-boundary.test.ts new file mode 100644 index 00000000000..4c14e72a404 --- /dev/null +++ b/server/src/__tests__/startup-readiness-boundary.test.ts @@ -0,0 +1,75 @@ +import { readFileSync } from "node:fs"; +import { fileURLToPath } from "node:url"; +import { parseAst } from "vite"; +import { expect, it } from "vitest"; + +// /api/ready means "this server can serve requests and take over". Each step +// that holds it must be named, bounded by this boot, and listed here with why +// it must finish first. Work that grows with saved backlog (a queue's first +// pass, a sweep, a dump) or waits on an outside service runs through +// startupTimeline.background instead. On 2026-11-13, backlog first passes +// awaited here kept two replacement servers on paperclip.staging from ever +// reporting ready. +const READINESS_PRECONDITIONS: Record = { + listen: "the HTTP/PRP listener must be bound before anything can be served", + runtime_control_operations: "releases control operations the previous process left running, bounded by its in-flight work", + execution_policy: "a bad forced execution policy must fail startup, before any run starts", + managed_environments: "declared environments must exist before queued runs resume (fails closed)", + worktree_run_execution: "one settings read that decides whether run execution is armed", + scheduling_suppression: "one read that decides whether this runtime schedules heartbeats at all", + native_recovery: "adoption of old runs and the gates that can refuse a start finish before taking over", + external_adapters: "adapter type validation rejects valid external adapters until they are loaded", + cloud_configuration: "an image whose saved configuration is not eligible must not take over", +}; + +type Node = { type: string; start: number; end: number; [key: string]: unknown }; + +function children(node: Node): Node[] { + return Object.values(node).flatMap(value => Array.isArray(value) ? value : [value]) + .filter((value): value is Node => Boolean(value) && typeof (value as Node).type === "string"); +} + +function walk(node: Node, functions: Node[], visit: (node: Node, owner: Node | undefined) => void) { + const scope = /Function|ArrowFunctionExpression/.test(node.type) ? [...functions, node] : functions; + visit(node, scope.at(-1)); + for (const child of children(node)) walk(child, scope, visit); +} + +it("holds readiness only on named, listed preconditions", () => { + const file = fileURLToPath(new URL("../index.ts", import.meta.url)); + const source = readFileSync(file, "utf8"); + // Vite's parser (already a server dependency) reads TypeScript as ESTree. + const program = parseAst(source, { lang: "ts" }); + + // The boot path is the function that moves the phase from "recovering" to + // "ready". Awaits inside nested closures run in the background. + let boot: Node | undefined; + let from = -1; + let to = -1; + walk(program as unknown as Node, [], (node, owner) => { + if (node.type !== "CallExpression") return; + const callee = node.callee as Node & { name?: string }; + const phase = ((node.arguments as Node[])[0] as Node & { value?: unknown } | undefined)?.value; + if (callee.name !== "setStartupRecoveryPhase") return; + if (phase === "recovering") { boot = owner; from = node.start; } + if (phase === "ready" && owner === boot) to = node.start; + }); + expect(boot).toBeDefined(); + expect(to).toBeGreaterThan(from); + + const unnamed: string[] = []; + const named: string[] = []; + walk(program as unknown as Node, [], (node, owner) => { + if (node.type !== "AwaitExpression" || owner !== boot || node.start < from || node.start > to) return; + const argument = node.argument as Node; + const callee = argument.type === "CallExpression" ? argument.callee as Node : undefined; + const isPrecondition = callee?.type === "MemberExpression" + && source.slice(callee.start, callee.end) === "startupTimeline.precondition"; + const step = isPrecondition ? ((argument.arguments as Node[])[0] as Node & { value?: unknown }).value : undefined; + if (typeof step === "string") named.push(step); + else unnamed.push(`line ${source.slice(0, node.start).split("\n").length}: ${source.slice(node.start, node.start + 80)}`); + }); + + expect(unnamed, "await on the boot path outside startupTimeline.precondition").toEqual([]); + expect([...new Set(named)].sort()).toEqual(Object.keys(READINESS_PRECONDITIONS).sort()); +}); diff --git a/server/src/index.ts b/server/src/index.ts index 28d568c59b7..76dbbbb71ca 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1099,7 +1099,7 @@ async function startServerWithDatabaseTeardown( // to this address; delaying listen until after orphan reconciliation makes // authenticated adoption impossible and turns a healthy process into a // duplicate-provider risk. - await new Promise((resolveListen, rejectListen) => { + await startupTimeline.precondition("listen", () => new Promise((resolveListen, rejectListen) => { const onError = (err: Error) => { server.off("error", onError); rejectListen(err); @@ -1112,7 +1112,7 @@ async function startServerWithDatabaseTeardown( ); resolveListen(); }); - }); + })); startupListenerBound = true; startupTimeline.mark("listening"); // A blocked event loop lapses every controller lease and freezes every @@ -1127,8 +1127,8 @@ async function startServerWithDatabaseTeardown( resolveLocalRuntimeApiUrl(); try { - const result = await workspaceOperationService(db as any) - .reconcileStaleRuntimeControlOperations(); + const result = await startupTimeline.precondition("runtime_control_operations", () => workspaceOperationService(db as any) + .reconcileStaleRuntimeControlOperations()); if (result.reconciled > 0) { logger.warn( { reconciled: result.reconciled, operationIds: result.operationIds }, @@ -1234,7 +1234,7 @@ async function startServerWithDatabaseTeardown( // PAPERCLIP_EXECUTION_MODE / PAPERCLIP_K8S_* value throws and fails startup // (fail-loud) rather than silently allowing local execution. try { - const policyResult = await bootstrapExecutionPolicyFromEnv(db as any); + const policyResult = await startupTimeline.precondition("execution_policy", () => bootstrapExecutionPolicyFromEnv(db as any)); if (policyResult) { logger.warn( { @@ -1274,12 +1274,12 @@ async function startServerWithDatabaseTeardown( const appLocals = (app as { locals?: { bundledPluginsStartup?: Promise; managedBundledPluginKeys?: readonly string[] }; }).locals; - const managedEnvironmentsResult = await applyManagedEnvironments(db as any, managedConfig, { + const managedEnvironmentsResult = await startupTimeline.precondition("managed_environments", () => applyManagedEnvironments(db as any, managedConfig, { recovery: managedEnvironmentRecovery, pluginsReady: appLocals?.bundledPluginsStartup, autoInstalledPluginKeys: appLocals?.managedBundledPluginKeys, workerManager: pluginWorkerManager, - }); + })); if (managedEnvironmentsResult) { logger.warn(managedEnvironmentsResult, "managed environments ensured from managed config"); } @@ -1561,7 +1561,14 @@ async function startServerWithDatabaseTeardown( if (githubCanRun()) await githubConnectionEvents.pollOnce({ forSetup: true }); })); - await timeStartupRecovery("tool_reviews", async () => { + // A queue's first pass scales with saved backlog and can wait on outside + // services, so none of them holds readiness: on a busy instance these passes + // took longer than a deploy's whole health-check window and the replacement + // server was killed before it reported ready. Each queue owns its work, is + // woken by commits and retries on its own schedule; a first pass that ends + // after ready is the same as any later pass. Requests are already served + // while they run, and shutdown and idle sleep wait for them as tracked work. + trackHeartbeatSchedulerWork(startupTimeline.background("tool_reviews", () => timeStartupRecovery("tool_reviews", async () => { if (config.heartbeatSchedulerEnabled) { await registerToolReviewExpiryWork(deliveryWork, app.locals.toolGateway, () => !isIdleTaskDrainActive() && !heartbeatSchedulerStopped && !isWarmStandby()).ready; @@ -1584,14 +1591,15 @@ async function startServerWithDatabaseTeardown( ? registerToolReviewProjectionWork(deliveryWork, { sweep: app.locals.toolGateway.repairActionReviewProjections, companies: app.locals.toolGateway.actionReviewProjectionCompanies }, () => !isIdleTaskDrainActive() && !heartbeatSchedulerStopped && !isWarmStandby()).ready : app.locals.toolGateway.repairActionReviewProjections(); - }).catch((err: unknown) => logger.error({ err }, "startup tool review recovery failed")); - await timeStartupRecovery("gateway_sessions", () => config.heartbeatSchedulerEnabled + }).catch((err: unknown) => logger.error({ err }, "startup tool review recovery failed")))); + trackHeartbeatSchedulerWork(startupTimeline.background("gateway_sessions", () => timeStartupRecovery("gateway_sessions", () => config.heartbeatSchedulerEnabled ? registerGatewayCredentialExpiryWork(deliveryWork, app.locals.toolGateway, () => !isIdleTaskDrainActive() && !heartbeatSchedulerStopped && !isWarmStandby()).ready - : app.locals.toolGateway.cleanupExpiredSessions()).catch((err: unknown) => logger.error({ err }, "startup gateway token cleanup failed")); - await timeStartupRecovery("delivery_workers", () => Promise.all([budgetEnforcementWorker, accountingWorker, fastResponseWorker, ...deliveryWorkers].map(worker => worker.ready))); - await Promise.all([githubEventWorker.ready, githubContinuityWorker.ready]); - startupTimeline.mark("delivery_first_passes"); + : app.locals.toolGateway.cleanupExpiredSessions()).catch((err: unknown) => logger.error({ err }, "startup gateway token cleanup failed")))); + trackHeartbeatSchedulerWork(startupTimeline.background("delivery_workers", () => timeStartupRecovery("delivery_workers", + () => Promise.all([budgetEnforcementWorker, accountingWorker, fastResponseWorker, ...deliveryWorkers].map(worker => worker.ready))))); + trackHeartbeatSchedulerWork(startupTimeline.background("github_connections", + () => Promise.all([githubEventWorker.ready, githubContinuityWorker.ready]))); if (heartbeat) { const secretProposals = createSecretProposalsService(db as any); @@ -1685,7 +1693,7 @@ async function startServerWithDatabaseTeardown( }, }, () => !isIdleTaskDrainActive() && !isWarmStandby() && !heartbeatSchedulerStopped); unregisterTerminalWorkspaceProbe = registerIdleLocalWorkProbe(() => terminalWorkspaceWorker.hasUnrecoveredWork() ? "present" : "none"); - await terminalWorkspaceWorker.ready; + trackHeartbeatSchedulerWork(startupTimeline.background("terminal_workspaces", () => terminalWorkspaceWorker.ready)); const adapterLoginReaper = createDeviceLoginReaper({ store: createDbAdapterAuthSessionStore(db as any), @@ -1720,9 +1728,9 @@ async function startServerWithDatabaseTeardown( } }; - const worktreeRunExecutionActivation = await resolveWorktreeRunExecutionActivationState({ + const worktreeRunExecutionActivation = await startupTimeline.precondition("worktree_run_execution", () => resolveWorktreeRunExecutionActivationState({ getExperimental: () => instanceSettingsService(db).getExperimental(), - }); + })); logger.info( { state: worktreeRunExecutionActivation.armed ? "armed" : "disarmed", @@ -1730,7 +1738,7 @@ async function startServerWithDatabaseTeardown( }, "worktree run-execution cutoff state", ); - const heartbeatSchedulingSuppression = await heartbeat.resolveSchedulingSuppression(); + const heartbeatSchedulingSuppression = await startupTimeline.precondition("scheduling_suppression", () => heartbeat.resolveSchedulingSuppression()); // Reap orphaned runs before timer ticks start so wakeups cannot coalesce // into a dead "running" row during startup recovery. @@ -1812,7 +1820,9 @@ async function startServerWithDatabaseTeardown( throw err; }); trackHeartbeatSchedulerWork(startupHeartbeatRecovery); - await startupHeartbeatRecovery; + // Adoption of old runs and the gates that can refuse a start finish + // before this server takes over (see the comment below). + await startupTimeline.precondition("native_recovery", () => startupHeartbeatRecovery); } startupTimeline.mark("native_recovery"); @@ -2051,9 +2061,10 @@ async function startServerWithDatabaseTeardown( // is still required. A failed acquire can leak a paid provider sandbox, so // this path registers the same startup recovery and pending-work deadlines // as the enabled path. - await startEnvironmentCleanup(); + trackHeartbeatSchedulerWork(startupTimeline.background("environment_lease_cleanup", startEnvironmentCleanup)); } + let idleWakeBackup: Promise | null = null; if (config.databaseBackupEnabled) { const backupIntervalMs = config.databaseBackupIntervalMinutes * 60 * 1000; @@ -2068,20 +2079,25 @@ async function startServerWithDatabaseTeardown( // The final pre-sleep dump remains on the volume throughout sleep. Take // a fresh backup on restart before idle eligibility is enabled, including // after rollback of the opt-in flag. Failure retains the marker for retry. + // A dump grows with the database, so it holds idle eligibility, not + // readiness. if (!isWarmStandby() && existsSync(idleBackupWakeMarker(config.databaseBackupDir))) { - await runServerDatabaseBackup("scheduled").catch(() => {}); + idleWakeBackup = startupTimeline.background("idle_wake_backup", () => runServerDatabaseBackup("scheduled").catch(() => {})); + trackHeartbeatSchedulerWork(idleWakeBackup); } - await registerDatabaseBackupWork(deliveryWork, { + trackHeartbeatSchedulerWork(startupTimeline.background("database_backups", () => registerDatabaseBackupWork(deliveryWork, { intervalMs: backupIntervalMs, run: () => runServerDatabaseBackup("scheduled"), - }).ready; + }).ready)); } // Wait for external adapters to finish loading before accepting requests. // Without this, adapter type validation (assertKnownAdapterType) would // reject valid external adapter types during the startup loading window. - const { waitForExternalAdapters } = await import("./adapters/registry.js"); - await waitForExternalAdapters(); + await startupTimeline.precondition("external_adapters", async () => { + const { waitForExternalAdapters } = await import("./adapters/registry.js"); + await waitForExternalAdapters(); + }); // Reconcile the agent-creation picker to the declaratively-configured adapter // set (PAPERCLIP_ADAPTERS). Must run after external adapters are loaded so the @@ -2096,12 +2112,14 @@ async function startServerWithDatabaseTeardown( } if (deploymentListenUrl !== null) { - const qualification = await qualifyCloudConfigurationForCurrentImage(db as any); + const qualification = await startupTimeline.precondition("cloud_configuration", () => qualifyCloudConfigurationForCurrentImage(db as any)); if (qualification && !qualification.configurationPackagesEligible) { throw new Error(`Volume-free server packages are not eligible: ${qualification.blockers.map(blocker => blocker.code).join(", ")}`); } } - markIdleStartupComplete({ scheduledBackups: config.databaseBackupEnabled }); + const markIdleStartup = () => markIdleStartupComplete({ scheduledBackups: config.databaseBackupEnabled }); + if (idleWakeBackup) void idleWakeBackup.then(markIdleStartup); + else markIdleStartup(); setStartupRecoveryPhase("ready"); startupTimeline.mark("ready"); startupTimeline.log("startup timeline at readiness"); diff --git a/server/src/services/terminal-workspace-work.ts b/server/src/services/terminal-workspace-work.ts index 6c86d1ac907..0a479d459f5 100644 --- a/server/src/services/terminal-workspace-work.ts +++ b/server/src/services/terminal-workspace-work.ts @@ -23,9 +23,10 @@ export function registerTerminalWorkspaceWork( ) { let next: number | null = Date.now(); let recovered = false; - // Server readiness waits for the first sweep. Compaction walks worktrees and - // deletes dependency trees, which can take seconds per workspace and is never - // needed to serve requests, so the first sweep skips it and later sweeps do it. + // The first sweep runs while the server finishes starting (it does not hold + // readiness). Compaction walks worktrees and deletes dependency trees, which + // can take seconds per workspace, so the first sweep skips it and later + // sweeps do it. let startupSweepStarted = false; const worker = coordinator.register(DELIVERY_QUEUES.terminalWorkspaces, { retryMs: TERMINAL_WORKSPACE_RETRY_MS, diff --git a/server/src/startup-timeline.test.ts b/server/src/startup-timeline.test.ts new file mode 100644 index 00000000000..037cc8b3d41 --- /dev/null +++ b/server/src/startup-timeline.test.ts @@ -0,0 +1,81 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { createStartupTimeline } from "./startup-timeline.js"; + +let uptime = 0; +const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn() }; +function deferred() { + let resolve!: (value: T) => void; + const promise = new Promise(done => { resolve = done; }); + return { promise, resolve }; +} +const timeline = () => createStartupTimeline({ uptimeMs: () => uptime, log: log as never, warnAfterMs: 30_000 }); + +beforeEach(() => { + uptime = 0; + vi.clearAllMocks(); + vi.useFakeTimers(); +}); +afterEach(() => vi.useRealTimers()); + +it("never waits for a background step and reports it outstanding at readiness", async () => { + const startup = timeline(); + const finished = vi.fn(); + void startup.background("delivery_workers", () => new Promise(() => {})).then(finished); + await startup.precondition("listen", async () => { uptime = 40; }); + uptime = 100; + startup.mark("ready"); + startup.log("startup timeline at readiness"); + expect(finished).not.toHaveBeenCalled(); + expect(log.info).toHaveBeenCalledExactlyOnceWith({ + processUptimeMs: 100, + startupSteps: { ready: 100 }, + readinessStepsMs: { listen: 40 }, + backgroundStepsMs: {}, + backgroundStepsOutstanding: ["delivery_workers"], + }, "startup timeline at readiness"); +}); + +it("names a precondition still holding readiness every interval until it ends", async () => { + const startup = timeline(); + const gate = deferred(); + const result = startup.precondition("native_recovery", () => gate.promise); + uptime = 30_000; + await vi.advanceTimersByTimeAsync(30_000); + uptime = 60_000; + await vi.advanceTimersByTimeAsync(30_000); + expect(log.warn.mock.calls).toEqual([ + [{ startupStep: "native_recovery", waitingMs: 30_000, holdsReadiness: true }, "startup step is still holding readiness"], + [{ startupStep: "native_recovery", waitingMs: 60_000, holdsReadiness: true }, "startup step is still holding readiness"], + ]); + gate.resolve("adopted"); + await expect(result).resolves.toBe("adopted"); + await vi.advanceTimersByTimeAsync(90_000); + expect(log.warn).toHaveBeenCalledTimes(2); +}); + +it("propagates a precondition failure and records its duration", async () => { + const startup = timeline(); + const error = new Error("ineligible"); + await expect(startup.precondition("cloud_configuration", async () => { uptime = 5; throw error; })).rejects.toBe(error); + startup.log("ready"); + expect(log.info.mock.calls[0]![0]).toMatchObject({ readinessStepsMs: { cloud_configuration: 5 } }); +}); + +it("logs a slow or failed background step by name and never rejects", async () => { + const startup = timeline(); + const slow = deferred(); + const slowStep = startup.background("terminal_workspaces", () => slow.promise); + const failed = startup.background("github_connections", async () => { throw new Error("GitHub unavailable"); }); + await expect(failed).resolves.toBeUndefined(); + expect(log.error).toHaveBeenCalledExactlyOnceWith( + { err: expect.any(Error), startupStep: "github_connections" }, "startup background step failed"); + uptime = 30_000; + await vi.advanceTimersByTimeAsync(30_000); + await vi.advanceTimersByTimeAsync(60_000); + expect(log.warn.mock.calls).toEqual([[{ startupStep: "terminal_workspaces", waitingMs: 30_000, holdsReadiness: false }, + "startup step is still running in the background"]]); + uptime = 95_000; + slow.resolve(undefined); + await slowStep; + expect(log.info).toHaveBeenCalledWith({ startupStep: "terminal_workspaces", durationMs: 95_000 }, "startup background step finished"); +}); diff --git a/server/src/startup-timeline.ts b/server/src/startup-timeline.ts index 777d8f67335..df4a8c3f2f6 100644 --- a/server/src/startup-timeline.ts +++ b/server/src/startup-timeline.ts @@ -2,22 +2,95 @@ import { logger } from "./middleware/logger.js"; export type StartupTimelineMark = { step: string; atMs: number; durationMs: number }; +/** A startup step still running after this long is named in a warning, so a + * slow start says which step holds it in one grep ("startup step"). */ +export const STARTUP_STEP_WARN_AFTER_MS = 30_000; + +type StartupTimelineLogger = Pick; + /** Boot-to-ready milestones, measured from process start. One log line at * readiness (and one when the background recovery passes finish) names the - * step that took the time, without an OpenTelemetry collector. */ -export function createStartupTimeline(uptimeMs: () => number = () => process.uptime() * 1000) { + * step that took the time, without an OpenTelemetry collector. + * + * Readiness means "this server can serve requests and take over". Only + * `precondition` steps hold it, and each one must be bounded by this boot, not + * by saved backlog or an outside service. Work that grows with backlog (a + * queue's first pass) goes through `background`: it starts at once, never + * holds readiness, and is reported at readiness as still outstanding. */ +export function createStartupTimeline(options: { + uptimeMs?: () => number; + log?: StartupTimelineLogger; + warnAfterMs?: number; +} = {}) { + const uptimeMs = options.uptimeMs ?? (() => process.uptime() * 1000); + const log = options.log ?? logger; + const warnAfterMs = options.warnAfterMs ?? STARTUP_STEP_WARN_AFTER_MS; const marks: StartupTimelineMark[] = []; + const preconditions = new Map(); + const background = new Map(); + const outstanding = new Map(); + const now = () => Math.round(uptimeMs()); + + function watch(step: string, holdsReadiness: boolean) { + const startedAt = now(); + outstanding.set(step, { startedAt, holdsReadiness }); + // A step that holds readiness is named every period until it ends; a + // background step is named once, since it does not delay anything. One + // unref'd timeout at a time: no interval outlives the step. + let timer: ReturnType | undefined; + const arm = () => { + timer = setTimeout(() => { + if (!outstanding.has(step)) return; + log.warn({ startupStep: step, waitingMs: now() - startedAt, holdsReadiness }, + holdsReadiness ? "startup step is still holding readiness" : "startup step is still running in the background"); + if (holdsReadiness) arm(); + }, warnAfterMs); + timer.unref?.(); + }; + arm(); + return () => { + clearTimeout(timer); + outstanding.delete(step); + return now() - startedAt; + }; + } + return { mark(step: string) { - const atMs = Math.round(uptimeMs()); + const atMs = now(); const previous = marks.at(-1)?.atMs ?? 0; marks.push({ step, atMs, durationMs: atMs - previous }); }, marks: () => marks.map(mark => ({ ...mark })), + /** A step readiness must wait for. Keep it bounded by this boot. */ + async precondition(step: string, work: () => Promise): Promise { + const finish = watch(step, true); + try { + return await work(); + } finally { + preconditions.set(step, finish()); + } + }, + /** Starts work that must not hold readiness. The returned promise never + * rejects (a failure is logged with the step's name), so callers can track + * it as scheduler work. */ + background(step: string, work: () => Promise): Promise { + const finish = watch(step, false); + return Promise.resolve().then(work).then(() => undefined, (err: unknown) => { + log.error({ err, startupStep: step }, "startup background step failed"); + }).finally(() => { + const durationMs = finish(); + background.set(step, durationMs); + if (durationMs >= warnAfterMs) log.info({ startupStep: step, durationMs }, "startup background step finished"); + }); + }, log(message: string) { - logger.info({ - processUptimeMs: Math.round(uptimeMs()), + log.info({ + processUptimeMs: now(), startupSteps: Object.fromEntries(marks.map(mark => [mark.step, mark.durationMs])), + readinessStepsMs: Object.fromEntries(preconditions), + backgroundStepsMs: Object.fromEntries(background), + backgroundStepsOutstanding: [...outstanding].filter(([, entry]) => !entry.holdsReadiness).map(([step]) => step), }, message); }, }; From 826d41e64678624a3dd1b3ed8672e1e44fdad2a1 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:17:41 -0600 Subject: [PATCH 04/21] =?UTF-8?q?feat(ui):=20redesign=20Settings=20?= =?UTF-8?q?=E2=86=92=20Secrets=20around=20what=20people=20manage=20(#21225?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agents need API keys and tokens. People manage them in Settings → Secrets. > - The page showed every credential in one table. Tokens that Paperclip saved for connections, chat channels and model sign-ins sat beside the few secrets a person added, many with record IDs as names. > - Each row repeated an always-true status, a version and a key. New secret opened on two stacked mode switches and two storage pickers. > - A person could not see which agents use a secret without opening it, and had to open it again after creation to give an agent access. > - This pull request redesigns the page, the New secret dialog, the details panel, the Vaults tab, the import dialog, the Proposals tab and My secrets. It keeps the existing data and API calls. > - The benefit is a list that shows what a person manages, a create flow with two fields, and a secret that an agent can use as soon as it is saved. ## Linked Issues or Issue Description **What existing behavior does this improve?** The Settings → Secrets page, its New secret dialog, the secret details panel, the Vaults tab, the import-from-vault dialog, the Proposals tab and the My secrets tab. **Subsystem affected** UI: `ui/src/pages/Secrets.tsx` and `ui/src/pages/secrets/*`. **Current behavior** - One table lists all secrets. On a busy organization most rows are credentials that Paperclip saved itself, and many have record IDs as names. - Each row shows Active, a version, a chip and the key. The row does not show which agents use the secret. - A three-line help banner is always visible. - New secret starts with two mode switches (Organization / Each user, Managed value / External reference), then a provider picker and a vault picker. The Create button is disabled without a reason. - The details panel has Details, Usage and Access events tabs. - The Vaults tab has one section per provider, with empty placeholder boxes and a status picker in the form. **Proposed behavior** - The list shows the secrets that the team added. Each row shows who provides the value (Organization, or Each user with a count) and which agents use it. - Credentials that Paperclip saved collapse into "Managed by Paperclip". They are grouped by origin and have readable names. - One Filter menu covers provided-by, storage and status. Active filters show as chips. Archived secrets stay hidden until a person asks for them. - New secret has a name, one explicit Organization / Each user choice, a masked value, an optional list of agents, and "Stored in" only when more than one storage place exists. Create stays enabled and says what is missing. A `NAME=value` pasted into Name fills both fields. - The details panel is one scrolling view: agents, other places the secret is set, details, and recent activity. - The Vaults tab is one flat list with health and secret counts. The connect dialog has two required fields. Paperclip checks a new vault immediately after it is saved. **Reason and benefit** A person can find their own secrets, see if a secret is used, and create a secret that an agent can use in one step. **Breaking changes** None. The API gains one optional query parameter (`order` on the board proposals list). The default order is unchanged. See Risks for the UI behaviors that changed. ## What Changed - **List.** Rewrote the Secrets tab in `Secrets.tsx`. Added `secret-list-parts.tsx` for the Provided by and Used by cells, the help popover and the managed groups. The Folders / Flat toggle is removed. Folders are always on and search covers all folders. - **Managed grouping.** Added `secret-origin.ts`. It infers the origin of a secret from the name and key conventions of the server modules that create secrets (`tool_app.*`, `tool_oauth.*`, `slack_manager.*`, `chat.*`, `ai_*`, `routine-*`, `agent-observability.*`). It is used for presentation only. - **Agent usage.** Added `agent-secret-access.ts`. It holds the grant and revoke logic that was inside the details panel, and builds one usage index from the agents list for all rows. - **New secret.** Rewrote the dialog. Added `buildCreateVaultOptions` and `parseEnvAssignment`. Agents selected in the dialog get an environment variable binding after the secret is saved. A failed grant shows an error and keeps the secret. - **Details.** Replaced the tabs with sections. "Also set in" lists the bindings that are not agents. Activity shows agent names and the last five entries, with "Show all activity". - **Vaults.** Flat list with a built-in Paperclip row, a Default badge that follows `getSelectableProviderConfig`, Check again, Enable, and an Add vault menu. The dialog has Name and AWS region, with naming and tags in a disclosure. The Status picker is removed. - **Import, Proposals, My secrets.** Restyled to the same design. Proposals adds a Recently decided list. The board list endpoint `GET /api/companies/{companyId}/secret-proposals` takes a new optional `order=resolved` to sort by decision time. - **Copy.** "Provider vault" is "vault". "Local encrypted" is "Paperclip" or "Paperclip-encrypted". Toasts and block reasons use plain words. - **Stories.** `secrets.stories.tsx` has new stories for the managed section, a folder, New secret, the details panel and Vaults. The Storybook usage fixture now returns the `target` field that the API returns. - **Tests.** Updated the page, import, proposals and My secrets tests. Added `secret-origin.test.ts`. Updated the runner E2E vault-reference journey and the Storybook visual render-state copy for the new labels. - **Docs.** `Secrets → Provider vaults` is now `Secrets → Vaults` in three documents. `Secrets.tsx` was 4,855 lines and is 4,951 lines. The redesign edits the page in place to keep its queries, mutations and deep links. Splitting the file is a separate change. ## Verification - `cd ui && node ../scripts/run-tsc.mjs -b`: no errors. - `cd ui && pnpm vitest run src/pages/secrets src/pages/Secrets.test.ts src/pages/Secrets.render.test.tsx src/components/AgentSecretAccessEditor.test.tsx src/components/AgentConfigForm src/components/IssueThreadInteractionCard storybook/map-tooling.test.ts`: 14 files, 467 tests passed. - `cd server && pnpm vitest run src/__tests__/secret-proposals-routes.test.ts -t "decision time"`: passed. - `pnpm vitest run --config tests/runner-e2e/vitest.config.ts chat-catalog-domains/external-vault-reference chat-catalog-domains/external-personal-secret-menu chat-catalog-domains/external-personal-secret-entry-dialog chat-catalog-domains/external-personal-secret.test`: 32 passed, 2 skipped. - `node scripts/check-token-gates.mjs`, `node scripts/check-maps.mjs`, `node scripts/check-module-boundaries.mjs`, `node scripts/storybook-catalog.mjs`: clean. - `pnpm verify`: exit 2. It asks for broader coverage because a runner E2E source file changed. Required CI supplies that coverage. - All 22 Secrets stories render and their play functions pass in headless Chromium against a local Storybook. - End to end, on an isolated local instance (`paperclipai worktree init --empty`, one company, two agents), with a headless Chromium script. 16 of 16 steps passed, with no page errors: - Empty state. Create explains missing fields. - Create an organization secret and give an agent access. The agent's saved config has the `secret_ref` binding. - Paste `NAME=value`. Create an each-user secret. Add a value in My secrets. The list shows "1 of 1 added". - Create a folder, create a secret in it, search across folders. - In details: add an agent, replace the value, remove the agent. The agent's saved config changes each time. - Disable, archive, and the Archived filter. - Managed secrets leave the team list and show under their groups. - Add a Paperclip-encrypted vault and an AWS vault. New secret then shows "Stored in". - The import dialog explains a denied vault. The Proposals tab shows its empty state. The list at 390 px width. - Not run: the paid runner E2E journeys. I updated `external-vault-reference.ts` for the new labels and could not execute it. A live AWS import, and a proposal from a real agent run, were not exercised. ## Risks - **Origin grouping is a heuristic.** It reads naming conventions that live in server modules. If a module changes its naming, its secrets show in the team list again. Nothing is hidden or blocked by it. A recorded origin on each secret is the durable fix. - **Removed UI.** The Folders / Flat toggle, the "View references" dialog, the vault Status picker, and creating draft GCP or HashiCorp vault entries from the Add vault menu. Existing draft vaults still show and can be edited. A disabled vault now has an Enable action. - **Default status filter.** The list now shows active and disabled secrets. Before, it showed active secrets only. - **Built-in storage in "Stored in".** A provider's built-in storage is not offered once that provider has a usable vault. The form already replaced that choice with the vault before this change. - **Agent access at creation.** Access is a second request for each agent after the secret is saved. If one fails, the secret exists without that access and an error says so. - **Automation contracts.** Labels that agents and the runner E2E journeys depend on are kept: the key in each My secrets row, "Set value", "Set my value", "Update my value", "Save value", and the import dialog test IDs. The import entry point moved into the "More secret actions" menu and the journey is updated for it. - **Value masking** uses `-webkit-text-security`. A browser without it shows the value as typed. ## Model Used - Claude Fable 5.2 (`claude-fable-5-2`) through Claude Code, with tool use, code execution and browser automation. Three delegated agents on the same model restyled the import dialog, the Proposals tab and My secrets. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.2 --- doc/CLI.md | 2 +- doc/DEVELOPING.md | 2 +- doc/SECRETS-AWS-PROVIDER.md | 2 +- .../__tests__/secret-proposals-routes.test.ts | 52 + server/src/routes/openapi.ts | 4 + server/src/routes/secrets.ts | 4 + server/src/services/secret-proposals.ts | 9 +- .../external-vault-reference.ts | 32 +- .../feature-gap-render-states.ts | 6 +- ui/src/api/secrets.ts | 10 +- ui/src/index.css | 7 +- ui/src/pages/Secrets.render.test.tsx | 2110 +++++---- ui/src/pages/Secrets.test.ts | 114 +- ui/src/pages/Secrets.tsx | 3784 +++++++++-------- .../secrets/ImportFromVaultDialog.test.tsx | 259 +- .../pages/secrets/ImportFromVaultDialog.tsx | 1338 +++--- .../pages/secrets/MyUserSecretsTab.test.tsx | 238 +- ui/src/pages/secrets/MyUserSecretsTab.tsx | 217 +- .../secrets/ProposalsTab.render.test.tsx | 401 +- ui/src/pages/secrets/ProposalsTab.tsx | 584 ++- .../pages/secrets/SetMyUserSecretDialog.tsx | 113 +- .../pages/secrets/agent-secret-access.test.ts | 73 + ui/src/pages/secrets/agent-secret-access.ts | 116 + ui/src/pages/secrets/proposal-review.tsx | 191 +- ui/src/pages/secrets/secret-list-parts.tsx | 106 + ui/src/pages/secrets/secret-origin.test.ts | 47 + ui/src/pages/secrets/secret-origin.ts | 74 + ui/storybook/.storybook/preview-base.tsx | 16 +- .../secrets/proposal-review.stories.tsx | 33 +- .../stories/secrets/secrets.stories.tsx | 105 +- 30 files changed, 6193 insertions(+), 3856 deletions(-) create mode 100644 ui/src/pages/secrets/agent-secret-access.test.ts create mode 100644 ui/src/pages/secrets/agent-secret-access.ts create mode 100644 ui/src/pages/secrets/secret-list-parts.tsx create mode 100644 ui/src/pages/secrets/secret-origin.test.ts create mode 100644 ui/src/pages/secrets/secret-origin.ts diff --git a/doc/CLI.md b/doc/CLI.md index c767a4a793b..67375e2665a 100644 --- a/doc/CLI.md +++ b/doc/CLI.md @@ -895,7 +895,7 @@ bootstrap credentials in Paperclip secrets. Per-company provider vaults (multiple vault instances per provider, default vault selection, coming-soon GCP/Vault) can be configured from the board UI under -`Organization Settings → Secrets → Provider vaults` or through the provider-config CLI +`Organization Settings → Secrets → Vaults` or through the provider-config CLI commands above. See the [secrets deploy guide](../docs/deploy/secrets.md#provider-vaults) and [API reference](../docs/api/secrets.md#provider-vaults) for the contract. diff --git a/doc/DEVELOPING.md b/doc/DEVELOPING.md index f17b66adec9..95a99fa0132 100644 --- a/doc/DEVELOPING.md +++ b/doc/DEVELOPING.md @@ -4271,7 +4271,7 @@ CLI configuration support: - Provider health is available at `GET /api/companies/:companyId/secret-providers/health` and reports local key permission warnings plus backup guidance. Per-company provider vaults are configured in the board UI under -`Company Settings → Secrets → Provider vaults`, backed by +`Company Settings → Secrets → Vaults`, backed by `/api/companies/{companyId}/secret-provider-configs`. The CLI does not own vault lifecycle today. See `docs/deploy/secrets.md` (`Provider Vaults` section) for the operator model. diff --git a/doc/SECRETS-AWS-PROVIDER.md b/doc/SECRETS-AWS-PROVIDER.md index 4f150dccdfe..83ec064ea1b 100644 --- a/doc/SECRETS-AWS-PROVIDER.md +++ b/doc/SECRETS-AWS-PROVIDER.md @@ -13,7 +13,7 @@ Operational contract for the hosted `aws_secrets_manager` secret provider used b copy plaintext into Paperclip. - Per-company AWS provider vaults (named instances of `aws_secrets_manager` with their own region, namespace, prefix, KMS key id, and tags) are managed - in the board UI under `Company Settings → Secrets → Provider vaults`. See + in the board UI under `Company Settings → Secrets → Vaults`. See [Provider Vaults](../docs/deploy/secrets.md#provider-vaults) for the operator model and [Provider Vaults API](../docs/api/secrets.md#provider-vaults) for the routes. The bootstrap trust model in this document still applies — vault diff --git a/server/src/__tests__/secret-proposals-routes.test.ts b/server/src/__tests__/secret-proposals-routes.test.ts index 1310dac8c9a..2ea6f49f634 100644 --- a/server/src/__tests__/secret-proposals-routes.test.ts +++ b/server/src/__tests__/secret-proposals-routes.test.ts @@ -1301,6 +1301,58 @@ describeEmbeddedPostgres("secret proposal routes", () => { expect(lastBoardPage.headers["x-next-offset"]).toBeUndefined(); }); + it("lists decided proposals by decision time when asked, and rejects an unknown order", async () => { + const fixture = await seedRun(); + const liveSecret = await secretService(db).create(fixture.companyId, { + name: "dev/order/source", + key: "ORDER_SOURCE", + provider: "local_encrypted", + value: "order-source-secret", + }); + await secretService(db).createBinding({ + companyId: fixture.companyId, + secretId: liveSecret.id, + targetType: "agent", + targetId: fixture.agentId, + configPath: "access.order_source", + }); + const agentApp = createAgentApp(fixture); + const created: string[] = []; + for (const index of [0, 1, 2]) { + const response = await request(agentApp) + .post("/api/agents/me/secret-proposals") + .send({ + kind: "binding", + sourceConfigPath: "access.order_source", + configPath: `env.ORDER_${index}`, + justification: "Exercise decision-time ordering", + }); + expect(response.status).toBe(201); + created.push(response.body.id); + } + // Decide them in the opposite order to their creation: the oldest request is decided last. + const decidedAt = [3, 2, 1].map((minutes) => new Date(Date.UTC(2026, 0, 1, 12, minutes))); + for (const [index, id] of created.entries()) { + await db.update(companySecretProposals) + .set({ status: "rejected", resolvedAt: decidedAt[index], createdAt: new Date(Date.UTC(2026, 0, 1, 10, index)) }) + .where(eq(companySecretProposals.id, id)); + } + const boardApp = createBoardApp(fixture); + const base = `/api/companies/${fixture.companyId}/secret-proposals?status=rejected`; + + const byDecision = await request(boardApp).get(`${base}&order=resolved`); + expect(byDecision.status).toBe(200); + expect(byDecision.body.map((row: { id: string }) => row.id)).toEqual(created); + + const byCreation = await request(boardApp).get(base); + expect(byCreation.body.map((row: { id: string }) => row.id)).toEqual([...created].reverse()); + + const topDecision = await request(boardApp).get(`${base}&order=resolved&limit=1`); + expect(topDecision.body.map((row: { id: string }) => row.id)).toEqual([created[0]]); + + expect((await request(boardApp).get(`${base}&order=newest`)).status).toBe(422); + }); + it("bounds each expiry sweep batch", async () => { const fixture = await seedRun(); const liveSecret = await secretService(db).create(fixture.companyId, { diff --git a/server/src/routes/openapi.ts b/server/src/routes/openapi.ts index d0c762172c2..b75442cc2f0 100644 --- a/server/src/routes/openapi.ts +++ b/server/src/routes/openapi.ts @@ -5697,6 +5697,10 @@ registry.registerPath({ status: z .enum(["pending", "approved", "rejected", "withdrawn", "expired"]) .optional(), + order: z + .enum(["created", "resolved"]) + .optional() + .describe("created (default) lists the newest proposals first; resolved lists the most recent decisions first."), }), }, responses: { 200: r.ok(), 401: r.unauthorized, 403: r.forbidden }, diff --git a/server/src/routes/secrets.ts b/server/src/routes/secrets.ts index fd6fce87b62..2250b3be7df 100644 --- a/server/src/routes/secrets.ts +++ b/server/src/routes/secrets.ts @@ -243,9 +243,13 @@ export function secretRoutes(db: Db, deps: SecretRoutesDeps = {}) { assertCompanyAccess(req, companyId); const status = typeof req.query.status === "string" ? req.query.status : null; const page = proposalListPage(req.query); + if (req.query.order !== undefined && req.query.order !== "created" && req.query.order !== "resolved") { + throw unprocessable("order must be created or resolved"); + } const rows = await proposals.listForBoard(companyId, status, { limit: page.limit + 1, offset: page.offset, + order: req.query.order === "resolved" ? "resolved" : "created", }); const hasMore = rows.length > page.limit; setProposalPaginationHeaders(res, page, hasMore); diff --git a/server/src/services/secret-proposals.ts b/server/src/services/secret-proposals.ts index 5291561ef79..b098946ce7f 100644 --- a/server/src/services/secret-proposals.ts +++ b/server/src/services/secret-proposals.ts @@ -549,12 +549,17 @@ export function createSecretProposalsService(db: Db) { async function listForBoard( companyId: string, status?: string | null, - options: { limit?: number; offset?: number } = {}, + options: { limit?: number; offset?: number; order?: "created" | "resolved" } = {}, ) { const rows = await db.select().from(companySecretProposals).where(and( eq(companySecretProposals.companyId, companyId), status ? eq(companySecretProposals.status, status) : undefined, - )).orderBy(desc(companySecretProposals.createdAt)) + )).orderBy( + // "resolved" puts the most recent decisions first; undecided rows follow by age. + ...(options.order === "resolved" + ? [sql`${companySecretProposals.resolvedAt} desc nulls last`, desc(companySecretProposals.createdAt)] + : [desc(companySecretProposals.createdAt)]), + ) .limit(options.limit ?? DEFAULT_PROPOSAL_LIST_LIMIT) .offset(options.offset ?? 0); return Promise.all(rows.map(enrich)); diff --git a/tests/runner-e2e/chat-catalog-domains/external-vault-reference.ts b/tests/runner-e2e/chat-catalog-domains/external-vault-reference.ts index 1451f382e93..958752978a2 100644 --- a/tests/runner-e2e/chat-catalog-domains/external-vault-reference.ts +++ b/tests/runner-e2e/chat-catalog-domains/external-vault-reference.ts @@ -536,6 +536,9 @@ export const vaultReferenceExternalCases = [ .locator(`[id^="comment-"] a[href=${JSON.stringify(href)}]`) .first() .click(); + await ctx.input.page + .getByRole("button", { name: "More secret actions", exact: true }) + .click(); await ctx.input.page.getByTestId("import-from-vault-button").click(); const dialog = ctx.input.page.getByTestId("import-from-vault-dialog"); await expect(dialog).toBeVisible(); @@ -553,7 +556,7 @@ export const vaultReferenceExternalCases = [ response.request().postDataJSON()?.query === query, ); await dialog - .getByLabel("Search remote secrets", { exact: true }) + .getByLabel("Search the vault", { exact: true }) .fill(query); const response = await previewResponse; state.operatorChannel = "public-vault-import-ui"; @@ -589,14 +592,15 @@ export const vaultReferenceExternalCases = [ "Setup blocker: designated AWS vault reference is not an eligible import candidate under the selected runtime; no other reference may substitute", ); await dialog - .getByLabel(`Select ${candidate.remoteName}`, { exact: true }) + .getByLabel(candidate.remoteName, { exact: true }) .check(); await dialog - .getByRole("button", { name: "Continue → Review", exact: true }) + .getByRole("button", { name: "Continue with 1", exact: true }) .click(); await dialog .getByTestId(`review-name-${f.arn}`) .fill("Deployment API key"); + await dialog.getByTestId("review-details-toggle").click(); await dialog .getByTestId(`review-key-${f.arn}`) .fill("deployment-api-key"); @@ -607,7 +611,7 @@ export const vaultReferenceExternalCases = [ response.request().method() === "POST", ); await dialog - .getByRole("button", { name: "Import 1", exact: true }) + .getByRole("button", { name: "Import 1 secret", exact: true }) .click(); const saved = await importedResponse; if (!saved.ok()) { @@ -622,7 +626,8 @@ export const vaultReferenceExternalCases = [ f.vault.id, ); await dialog - .getByRole("button", { name: "Cancel", exact: true }) + .getByRole("button", { name: "Close", exact: true }) + .first() .click(); await returnToSource(ctx, state, sourceURL); return; @@ -684,16 +689,20 @@ export const vaultReferenceExternalCases = [ .click(); await expect( ctx.input.page.getByRole("heading", { - name: "Agent access", + name: "Agents that can use it", exact: true, }), ).toBeVisible(); + await ctx.input.page + .getByTestId("secret-details") + .getByRole("button", { name: "Add agent", exact: true }) + .click(); await ctx.input.page.locator("#agent-access-agent").click(); await ctx.input.page .getByRole("button", { name: "Select Morgan", exact: true }) .click(); await ctx.input.page - .getByLabel("Env var", { exact: true }) + .getByLabel("Environment variable", { exact: true }) .fill("DEPLOYMENT_API_KEY"); const grant = ctx.input.page.waitForResponse( (response) => @@ -702,14 +711,7 @@ export const vaultReferenceExternalCases = [ response.ok(), ); await ctx.input.page - .locator("section") - .filter({ - has: ctx.input.page.getByRole("heading", { - name: "Agent access", - exact: true, - }), - }) - .getByRole("button", { name: "Add", exact: true }) + .getByRole("button", { name: "Give access", exact: true }) .click(); await grant; state.agentAccessUiCompleted = true; diff --git a/tests/storybook-visual/feature-gap-render-states.ts b/tests/storybook-visual/feature-gap-render-states.ts index 57518797d32..9d986d9961e 100644 --- a/tests/storybook-visual/feature-gap-render-states.ts +++ b/tests/storybook-visual/feature-gap-render-states.ts @@ -198,9 +198,9 @@ const states: Record> = { }, "feature-secret-proposal-review": { pending: { text: "dev/github/token" }, - loading: { text: "Loading proposals…" }, - empty: { text: "No pending proposals" }, - failed: { text: "Couldn’t load proposals. Try again." }, + loading: { text: "Loading requests…" }, + empty: { text: "Nothing to review" }, + failed: { text: "Couldn’t load requests. Try again." }, }, "feature-skill-test-runs": { "completed-test": { text: "succeeded" }, diff --git a/ui/src/api/secrets.ts b/ui/src/api/secrets.ts index 395ae41a59d..2bbcec4419d 100644 --- a/ui/src/api/secrets.ts +++ b/ui/src/api/secrets.ts @@ -238,9 +238,15 @@ export const secretsApi = { // --- Secret & binding proposals (PAP-14731) ----------------------------- // Board-facing review surface. Agents propose credentials/bindings; humans // approve or reject them here. Values are never returned by these routes. - listProposals: (companyId: string, status: SecretProposalStatus = "pending") => + listProposals: ( + companyId: string, + status: SecretProposalStatus = "pending", + options: { order?: "created" | "resolved"; limit?: number } = {}, + ) => api.get( - `/companies/${companyId}/secret-proposals?status=${encodeURIComponent(status)}`, + `/companies/${companyId}/secret-proposals?status=${encodeURIComponent(status)}${ + options.order ? `&order=${options.order}` : "" + }${options.limit ? `&limit=${options.limit}` : ""}`, ), approveProposal: (companyId: string, proposalId: string, data: ApproveSecretProposalInput = {}) => api.post( diff --git a/ui/src/index.css b/ui/src/index.css index 312e6cfb56a..d960c8e371a 100644 --- a/ui/src/index.css +++ b/ui/src/index.css @@ -3285,7 +3285,7 @@ span.paperclip-mention-chip[data-mention-kind="external-object"] { --gtc-40: minmax(0,1fr) 320px; /* Extracted from ui/src/pages/IssueChatLongThreadPerf.tsx (grid-cols-[minmax(0,1fr)_320px]). */ --gtc-52: minmax(0,1.4fr) minmax(220px,0.8fr); /* Extracted from ui/src/pages/PluginSettings.tsx (grid-cols-[minmax(0,1.4fr)_minmax(220px,0.8fr)]). */ --gtc-53: minmax(0,1.4fr) minmax(18rem,0.9fr); /* Extracted from ui/src/pages/ProjectWorkspaceDetail.tsx (grid-cols-[minmax(0,1.4fr)_minmax(18rem,0.9fr)]). */ - --gtc-54: minmax(12rem,2.4fr) minmax(5.25rem,0.75fr) minmax(7rem,1fr) minmax(5rem,0.7fr) 2.75rem; /* Extracted from ui/src/pages/Secrets.tsx (grid-cols-[minmax(12rem,2.4fr)_minmax(5.25rem,0.75fr)_minmax(7rem,1fr)_minmax(5rem,0.7fr)_2.75rem]). */ + --gtc-54: minmax(12rem,2.2fr) minmax(9rem,1.1fr) minmax(7rem,1fr) minmax(4.5rem,0.6fr) 2.75rem; /* Secrets list: name, provided by, used by, updated, actions (ui/src/pages/Secrets.tsx). */ --gtc-55: 8rem minmax(0,1fr); /* Extracted from ui/src/pages/Secrets.tsx (grid-cols-[8rem_minmax(0,1fr)]). */ --gtc-56: minmax(0,1fr) minmax(14rem,18rem); /* Extracted from ui/src/pages/TeamCatalog.tsx (grid-cols-[minmax(0,1fr)_minmax(14rem,18rem)]). */ --gtc-57: repeat(14,minmax(0,1fr)); /* Extracted from ui/src/pages/UserProfile.tsx (grid-cols-[repeat(14,minmax(0,1fr))]). */ @@ -5748,3 +5748,8 @@ html:has(.design-proposal-theme) .agent-markdown-name-field [data-slot="input"], .agents-org-toolbar .design-toolbar-actions { width: 100%; justify-content: flex-end; gap: var(--spacing); } .agents-org-toolbar .org-chart-controls { gap: var(--spacing); } } + +/* Secret values stay hidden while typed; a multi-line field cannot use type="password". */ +.secret-value-masked { + -webkit-text-security: disc; +} diff --git a/ui/src/pages/Secrets.render.test.tsx b/ui/src/pages/Secrets.render.test.tsx index 5c4b6bff700..b8a624878d6 100644 --- a/ui/src/pages/Secrets.render.test.tsx +++ b/ui/src/pages/Secrets.render.test.tsx @@ -1,13 +1,16 @@ // @vitest-environment jsdom +import type { ReactNode } from "react"; import { createRoot as createReactRoot, type Root } from "react-dom/client"; import { flushSync } from "react-dom"; import { MemoryRouter } from "react-router-dom"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import type { + Agent, CompanySecret, CompanySecretProviderConfig, RemoteSecretImportPreviewResult, + SecretAccessEvent, SecretProviderConfigDiscoveryPreviewResult, SecretProviderDescriptor, UserSecretCoverageSummary, @@ -18,6 +21,24 @@ import { ProviderVaultsTab, Secrets } from "./Secrets"; import { ApiError } from "../api/client"; import { queryKeys } from "../lib/queryKeys"; +// Counts origin classification, the per-row work of the managed grouping, and +// throws above a limit so a runaway render fails at once. +const originWork = vi.hoisted(() => ({ calls: 0, limit: Number.POSITIVE_INFINITY })); + +vi.mock("./secrets/secret-origin", async () => { + const actual = await vi.importActual("./secrets/secret-origin"); + return { + ...actual, + classifySecretOrigin: (input: Parameters[0]) => { + originWork.calls += 1; + if (originWork.calls > originWork.limit) { + throw new Error(`classifySecretOrigin ran ${originWork.calls} times; the limit is ${originWork.limit}`); + } + return actual.classifySecretOrigin(input); + }, + }; +}); + const mockSecretsApi = vi.hoisted(() => ({ list: vi.fn(), providers: vi.fn(), @@ -100,9 +121,41 @@ vi.mock("../context/SidebarContext", () => ({ }), })); +// Menus are Radix portals; render them inline so their items are reachable in jsdom. +vi.mock("@/components/ui/dropdown-menu", () => ({ + DropdownMenu: ({ children }: { children: ReactNode }) => <>{children}, + DropdownMenuTrigger: ({ children }: { children: ReactNode }) => <>{children}, + DropdownMenuContent: ({ children }: { children: ReactNode }) =>
{children}
, + DropdownMenuSeparator: () => null, + DropdownMenuItem: ({ + children, + onSelect, + disabled, + "data-testid": testId, + }: { + children: ReactNode; + onSelect?: () => void; + disabled?: boolean; + "data-testid"?: string; + }) => ( + + ), +})); + // eslint-disable-next-line @typescript-eslint/no-explicit-any (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true; +// Radix checkboxes and popovers measure themselves; jsdom has no ResizeObserver. +if (typeof globalThis.ResizeObserver === "undefined") { + globalThis.ResizeObserver = class { + observe() {} + unobserve() {} + disconnect() {} + } as unknown as typeof ResizeObserver; +} + const providers: SecretProviderDescriptor[] = [ { id: "local_encrypted", @@ -341,26 +394,224 @@ function setSelectValue(select: HTMLSelectElement, value: string) { select.dispatchEvent(new Event("change", { bubbles: true })); } -async function openAwsVaultDialog() { - const vaultTabButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.includes("Provider vaults"), - ) as HTMLButtonElement | undefined; - await act(async () => { - vaultTabButton?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); - vaultTabButton?.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Enter" })); - vaultTabButton?.click(); - }); - await flushReact(); +const bodyText = () => document.body.textContent ?? ""; +const byId = (id: string) => document.getElementById(id) as T | null; +const detailsPanel = () => document.querySelector('[data-testid="secret-details"]') as HTMLElement | null; +const openDialog = () => + [...document.querySelectorAll('[role="dialog"]')] + .filter( + (node) => + node.getAttribute("data-testid") !== "secret-details" && + node.getAttribute("data-slot") !== "popover-content", + ) + .at(-1) ?? null; +const teamTable = () => document.querySelector('[data-testid="secrets-table-view"]') as HTMLElement | null; - const addVaultButtons = [...document.querySelectorAll("button")].filter( - (button) => button.textContent?.includes("Add vault"), - ) as HTMLButtonElement[]; +/** A button, tab, radio or menu item by its visible text (exact unless `partial`). */ +function findControl(label: string, root: ParentNode | null = document.body, partial = false) { + return [...(root?.querySelectorAll('button, [role="tab"], [role="menuitem"]') ?? [])].find((node) => { + const text = node.textContent?.trim() ?? ""; + return partial ? text.includes(label) : text === label; + }); +} + +async function click(node: Element | null | undefined) { + expect(node, "expected something to click").toBeTruthy(); await act(async () => { - addVaultButtons[1]?.click(); + (node as HTMLElement).click(); }); await flushReact(); } +/** Radix tabs and triggers activate on pointerdown/keydown rather than click. */ +async function press(node: Element | null | undefined) { + expect(node, "expected something to press").toBeTruthy(); + await act(async () => { + node!.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); + node!.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Enter" })); + (node as HTMLElement).click(); + }); + await flushReact(); +} + +async function openTab(label: string) { + await press( + [...document.querySelectorAll('[role="tab"]')].find((tab) => tab.textContent?.startsWith(label)), + ); +} + +async function openNewSecret() { + await click(document.querySelector("button[data-toolbar-primary]")); +} + +async function typeInto(id: string, value: string) { + const field = byId(id); + expect(field, `expected a field with id ${id}`).not.toBeNull(); + await act(async () => { + if (field instanceof HTMLTextAreaElement) setTextareaValue(field, value); + else setInputValue(field as HTMLInputElement, value); + }); + await flushReact(); +} + +async function submitNewSecret(label = "Create secret") { + await click(findControl(label, openDialog())); + await flushReact(); +} + +async function chooseWhoProvides(label: "Organization" | "Each user") { + await click( + [...document.querySelectorAll('[role="radio"]')].find((radio) => radio.textContent?.includes(label)), + ); +} + +async function openMoreOptions() { + await click(findControl("More options", openDialog())); +} + +function teamRow(name: string) { + return [...(teamTable()?.querySelectorAll('[role="row"]') ?? [])].find((row) => + row.textContent?.includes(name), + ); +} + +async function chooseFilter(label: string) { + const option = () => + [...document.querySelectorAll("label")] + .find((node) => node.textContent?.trim() === label) + ?.querySelector('input[type="radio"]'); + if (!option()) await click(findControl("Filter")); + await click(option()); +} + +async function openAwsVaultDialog() { + await openTab("Vaults"); + await click( + [...document.querySelectorAll('[role="menuitem"]')].find( + (item) => item.textContent?.trim() === "AWS Secrets Manager", + ), + ); +} + +async function openNamingAndTags() { + await click(findControl("Naming and tags", openDialog())); +} + +function makeAgent(overrides: Partial & Pick): Agent { + return { status: "active", adapterConfig: {}, ...overrides } as Agent; +} + +function secretRef(secretId: string) { + return { type: "secret_ref", secretId }; +} + +function makeAccessEvent(index: number, overrides: Partial = {}): SecretAccessEvent { + return { + id: `event-${index}`, + companyId: "company-1", + secretId: "secret-openai", + userSecretDefinitionId: null, + secretScope: "company", + version: 1, + provider: "local_encrypted", + responsibleUserId: null, + credentialOwnerUserId: null, + credentialSubjectType: null, + credentialSubjectId: null, + actorType: "agent", + actorId: "agent-coder", + consumerType: "agent", + consumerId: "agent-coder", + configPath: "env.OPENAI_API_KEY", + issueId: null, + heartbeatRunId: null, + pluginId: null, + outcome: "success", + errorCode: null, + createdAt: new Date(Date.UTC(2026, 4, 6, 0, index)), + ...overrides, + } as SecretAccessEvent; +} + +function makeProviderConfig( + overrides: Partial & Pick, +): CompanySecretProviderConfig { + return { + companyId: "company-1", + displayName: overrides.id, + status: "ready", + isDefault: false, + config: {}, + healthStatus: null, + healthCheckedAt: null, + healthMessage: null, + healthDetails: null, + disabledAt: null, + createdByAgentId: null, + createdByUserId: null, + createdAt: new Date("2026-01-01T00:00:00Z"), + updatedAt: new Date("2026-01-01T00:00:00Z"), + ...overrides, + } as CompanySecretProviderConfig; +} + +function seedDefaults() { + mockSecretsApi.list.mockResolvedValue([]); + mockSecretsApi.providers.mockResolvedValue(providers); + mockSecretsApi.providerHealth.mockResolvedValue({ providers: [] }); + mockSecretsApi.providerConfigs.mockResolvedValue(providerConfigs); + mockSecretsApi.providerConfigDiscoveryPreview.mockResolvedValue(makeDiscoveryPreview()); + mockSecretsApi.checkProviderConfigHealth.mockResolvedValue({ status: "ok", message: "Reachable" }); + mockSecretsApi.remoteImportPreview.mockResolvedValue(makeRemoteImportPreview()); + mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([]); + mockSecretsApi.userSecretDefinitionCoverage.mockResolvedValue(userSecretCoverage); + mockSecretsApi.listMyUserSecrets.mockResolvedValue([]); + mockSecretsApi.listProposals.mockResolvedValue([]); + mockSecretsApi.usage.mockResolvedValue({ secretId: "secret-openai", bindings: [] }); + mockSecretsApi.accessEvents.mockResolvedValue([]); + mockAgentsApi.list.mockResolvedValue([]); +} + +/** Creating a secret also makes it show up in the next list read, as it would on the server. */ +function resolveCreateWith(secret: CompanySecret) { + mockSecretsApi.create.mockImplementation(async () => { + mockSecretsApi.list.mockResolvedValue([secret]); + return secret; + }); +} + +function renderVaultsTab( + container: HTMLDivElement, + props: Partial[0]> = {}, +) { + const handlers = { + onRetry: vi.fn(), + onCreate: vi.fn(), + onEdit: vi.fn(), + onDisable: vi.fn(), + onEnable: vi.fn(), + onRemove: vi.fn(), + onSetDefault: vi.fn(), + onHealthCheck: vi.fn(), + onImportSecrets: vi.fn(), + }; + const root = createRoot(container); + flushSync(() => { + root.render( + , + ); + }); + return { root, handlers }; +} + async function renderSecrets( container: HTMLDivElement, { @@ -389,9 +640,7 @@ describe("Secrets page layout", () => { beforeEach(() => { container = document.createElement("div"); document.body.appendChild(container); - - mockSecretsApi.list.mockResolvedValue([]); - mockSecretsApi.providers.mockResolvedValue(providers); + seedDefaults(); mockSecretsApi.providerHealth.mockResolvedValue({ providers: [ { @@ -402,228 +651,177 @@ describe("Secrets page layout", () => { }, ], }); - mockSecretsApi.providerConfigs.mockResolvedValue(providerConfigs); - mockSecretsApi.providerConfigDiscoveryPreview.mockResolvedValue(makeDiscoveryPreview()); - mockSecretsApi.remoteImportPreview.mockResolvedValue(makeRemoteImportPreview()); - mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([]); - mockSecretsApi.userSecretDefinitionCoverage.mockResolvedValue(userSecretCoverage); - mockSecretsApi.listMyUserSecrets.mockResolvedValue([]); - mockSecretsApi.listProposals.mockResolvedValue([]); - mockAgentsApi.list.mockResolvedValue([]); }); afterEach(() => { unmountActiveRoots(); container.remove(); document.body.innerHTML = ""; - vi.clearAllMocks(); + vi.resetAllMocks(); }); - it("uses the shared search/filter/tab affordances and keeps vault sections quiet", async () => { - const root = await renderSecrets(container); + it("uses the shared search/filter affordances and one line of help instead of a banner", async () => { + await renderSecrets(container); expect(container.querySelector('input[data-page-search-target="true"][aria-label="Search secrets"]')).not.toBeNull(); - expect(container.textContent).toContain("Use secrets by binding them to runtime environment variables."); - expect(container.textContent).toContain("GH_TOKEN"); + expect(container.querySelector('[data-canonical-toolbar="secrets"]')).not.toBeNull(); + expect(container.textContent).toContain("API keys and tokens your agents use while they work."); + expect(findControl("How secrets work", container, true)).toBeDefined(); + expect(container.textContent).not.toContain("Use secrets by binding them to runtime environment variables."); expect(container.querySelectorAll("select")).toHaveLength(0); expect(container.textContent).not.toContain("Provider warnings detected"); - expect(container.textContent).not.toContain("2/2 active"); + expect(findControl("Folders", container)).toBeUndefined(); + expect(findControl("Flat", container)).toBeUndefined(); - await act(async () => { - root.unmount(); - }); - - const vaultRoot = createRoot(container); - await act(async () => { - vaultRoot.render( - , - ); - }); - await flushReact(); - - expect(container.querySelector('a[href="#provider-vaults-local_encrypted"]')).not.toBeNull(); - expect(container.textContent).toContain("AWS production"); - expect(container.textContent).not.toContain("Managed writes"); - expect(container.textContent).not.toContain("External refs"); - - await act(async () => { - vaultRoot.unmount(); - }); + // New secret is the one primary action; folder and import live in the overflow menu. + expect(document.querySelector("button[data-toolbar-primary]")?.textContent).toContain("New secret"); + expect(findControl("New folder")?.getAttribute("role")).toBe("menuitem"); + expect(findControl("Import from a vault")?.getAttribute("role")).toBe("menuitem"); }); - it("refreshes existing AWS secrets from a provider vault card", async () => { - const root = await renderSecrets(container); + it("opens the import dialog from the overflow menu", async () => { + await renderSecrets(container); - const vaultTabButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.includes("Provider vaults"), - ) as HTMLButtonElement | undefined; - await act(async () => { - vaultTabButton?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); - vaultTabButton?.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Enter" })); - vaultTabButton?.click(); - }); + await click(findControl("Import from a vault")); await flushReact(); - const refreshButton = document.querySelector( - '[data-testid="provider-vault-refresh-secrets-vault-aws"]', - ) as HTMLButtonElement | null; - expect(refreshButton).not.toBeNull(); + expect(bodyText()).toContain("Import secrets from a vault"); + }); - await act(async () => { - refreshButton?.click(); - }); - await flushReact(); + it("imports existing AWS secrets from a vault's menu", async () => { + await renderSecrets(container); + await openTab("Vaults"); + + await click(document.querySelector('[data-testid="provider-vault-refresh-secrets-vault-aws"]')); await flushReact(); - expect(document.body.textContent).toContain("Import from AWS Secrets Manager"); + expect(bodyText()).toContain("Import secrets from a vault"); expect(mockSecretsApi.remoteImportPreview).toHaveBeenCalledWith("company-1", { providerConfigId: "vault-aws", query: null, nextToken: null, pageSize: 50, }); - - await act(async () => { - root.unmount(); - }); }); - it("warns that removing a provider vault only removes Paperclip config", async () => { + it("warns that removing a vault only removes it from Paperclip", async () => { mockSecretsApi.removeProviderConfig.mockResolvedValueOnce(providerConfigs[1]); - const root = await renderSecrets(container); + await renderSecrets(container); + await openTab("Vaults"); - const vaultTabButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.includes("Provider vaults"), - ) as HTMLButtonElement | undefined; - await act(async () => { - vaultTabButton?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); - vaultTabButton?.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Enter" })); - vaultTabButton?.click(); - }); - await flushReact(); + await click(findControl("Remove vault", document.querySelector('[data-testid="provider-vault-vault-aws"]'))); - const removeButtons = [...document.querySelectorAll("button")].filter( - (button) => button.textContent?.trim() === "Remove", - ) as HTMLButtonElement[]; - await act(async () => { - removeButtons[1]?.click(); - }); - await flushReact(); + const dialog = openDialog(); + expect(dialog?.textContent).toContain("Remove AWS production"); + expect(dialog?.textContent).toContain("from Paperclip only"); + expect(dialog?.textContent).toContain("Nothing is deleted in AWS."); - expect(document.body.textContent).toContain("Remove provider vault"); - expect(document.body.textContent).toContain("from Paperclip only"); - expect(document.body.textContent).toContain("does not delete"); - expect(document.body.textContent).toContain("AWS Secrets Manager"); - - const confirmButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.includes("Remove from Paperclip"), - ) as HTMLButtonElement | undefined; - await act(async () => { - confirmButton?.click(); - }); - await flushReact(); + await click(findControl("Remove vault", dialog)); expect(mockSecretsApi.removeProviderConfig).toHaveBeenCalledWith("vault-aws"); expect(mockSecretsApi.disableProviderConfig).not.toHaveBeenCalled(); - - await act(async () => { - root.unmount(); - }); }); - it("keeps references reachable from the compact secrets row and detail drawer", async () => { + it("shows agents, other places, details and activity in one scrolling panel", async () => { mockSecretsApi.list.mockResolvedValue([makeCompanySecret()]); + mockAgentsApi.list.mockResolvedValue([ + makeAgent({ id: "agent-1", name: "CodexCoder", adapterConfig: { env: { OPENAI_API_KEY: secretRef("secret-openai") } } }), + ]); + const binding = { + companyId: "company-1", + secretId: "secret-openai", + versionSelector: "latest", + required: true, + label: null, + createdAt: new Date("2026-05-06T00:00:00.000Z"), + updatedAt: new Date("2026-05-06T00:00:00.000Z"), + }; mockSecretsApi.usage.mockResolvedValue({ secretId: "secret-openai", bindings: [ { + ...binding, id: "binding-agent", - companyId: "company-1", - secretId: "secret-openai", targetType: "agent", targetId: "agent-1", configPath: "env.OPENAI_API_KEY", - versionSelector: "latest", - required: true, - label: null, - target: { - type: "agent", - id: "agent-1", - label: "CodexCoder", - href: "/agents/codexcoder", - status: "idle", - }, - createdAt: new Date("2026-05-06T00:00:00.000Z"), - updatedAt: new Date("2026-05-06T00:00:00.000Z"), + target: { type: "agent", id: "agent-1", label: "CodexCoder", href: "/agents/codexcoder", status: "idle" }, + }, + { + ...binding, + id: "binding-project", + targetType: "project", + targetId: "project-1", + configPath: "env.OPENAI_API_KEY", + target: { type: "project", id: "project-1", label: "Billing", href: "/projects/billing", status: null }, }, ], }); + await renderSecrets(container); - const root = await renderSecrets(container); - - const referencesButton = container.querySelector( - 'button[aria-label="Actions for OPENAI_API_KEY"]', - ) as HTMLButtonElement | null; - expect(referencesButton).not.toBeNull(); - - const companyRow = Array.from(container.querySelectorAll("[role='row']")).find( - (row) => row.textContent?.includes("OPENAI_API_KEY"), - ) as HTMLElement | undefined; - await act(async () => { - companyRow?.click(); - }); - await flushReact(); - - await waitForReact(() => document.body.textContent?.includes("View in Usage") ?? false); - const viewUsageButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.includes("View in Usage"), - ) as HTMLButtonElement | undefined; - await act(async () => { - viewUsageButton?.click(); - }); - await waitForReact(() => mockSecretsApi.usage.mock.calls.length > 0); + expect(container.querySelector('button[aria-label="Actions for OPENAI_API_KEY"]')).not.toBeNull(); + await click(teamRow("OPENAI_API_KEY")); + await waitForReact(() => detailsPanel()?.querySelector('[aria-label="Also set in"]') != null); + const panel = detailsPanel()!; expect(mockSecretsApi.usage).toHaveBeenCalledWith("secret-openai"); - expect(document.body.textContent).toContain("CodexCoder"); - expect(document.body.textContent).toContain("env.OPENAI_API_KEY"); + expect(panel.querySelectorAll('[role="tab"]')).toHaveLength(0); + expect(panel.textContent).not.toContain("View in Usage"); + expect([...panel.querySelectorAll("h3")].map((heading) => heading.textContent)).toEqual([ + "Agents that can use it", + "Also set in", + "Details", + "Activity", + ]); - await act(async () => { - root.unmount(); - }); + const agents = panel.querySelector('[aria-label="Agents that can use it"]')!; + expect(agents.textContent).toContain("CodexCoder"); + expect(agents.textContent).toContain("OPENAI_API_KEY"); + + // The agent is already listed above, so "Also set in" only names the rest. + const elsewhere = panel.querySelector('[aria-label="Also set in"]')!; + expect(elsewhere.textContent).toContain("Billing"); + expect(elsewhere.textContent).toContain("env.OPENAI_API_KEY"); + expect(elsewhere.textContent).not.toContain("CodexCoder"); + expect(elsewhere.textContent).not.toContain("project-1"); + }); + + it("caps activity at five entries until asked for the rest", async () => { + mockSecretsApi.list.mockResolvedValue([makeCompanySecret()]); + mockAgentsApi.list.mockResolvedValue([makeAgent({ id: "agent-coder", name: "CodexCoder" })]); + mockSecretsApi.accessEvents.mockResolvedValue([ + makeAccessEvent(0, { outcome: "not_allowed" }), + ...[1, 2, 3, 4, 5, 6].map((index) => makeAccessEvent(index)), + ]); + await renderSecrets(container, { path: "/company/settings/secrets?secret=secret-openai" }); + await waitForReact(() => findControl("Show all activity", detailsPanel()) != null); + + const activity = detailsPanel()!.querySelector('[aria-label="Activity"]')!; + expect(activity.querySelectorAll("li")).toHaveLength(5); + // Raw agent ids are resolved to names, and a refusal reads as one. + expect(activity.textContent).toContain("CodexCoder was refused: no access"); + expect(activity.textContent).toContain("CodexCoder used it"); + expect(activity.textContent).not.toContain("agent-coder"); + + await click(findControl("Show all activity", activity)); + + expect(activity.querySelectorAll("li")).toHaveLength(7); + expect(findControl("Show all activity", activity)).toBeUndefined(); }); it("opens the secret detail sheet from a ?secret= deep link", async () => { mockSecretsApi.list.mockResolvedValue([makeCompanySecret()]); - mockSecretsApi.usage.mockResolvedValue({ secretId: "secret-openai", bindings: [] }); - mockSecretsApi.accessEvents.mockResolvedValue([]); - const root = await renderSecrets(container, { path: "/company/settings/secrets?secret=secret-openai" }); + await renderSecrets(container, { path: "/company/settings/secrets?secret=secret-openai" }); // The detail sheet is open without any clicks: the deep link drives selection. - const copyLinkButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.includes("Copy link"), - ); - expect(copyLinkButton).not.toBeUndefined(); - const sheet = copyLinkButton?.closest("[role='dialog']"); - expect(sheet?.textContent).toContain("OPENAI_API_KEY"); - - await act(async () => { - root.unmount(); - }); + const panel = detailsPanel(); + expect(panel).not.toBeNull(); + expect(panel?.getAttribute("role")).toBe("dialog"); + expect(panel?.textContent).toContain("OPENAI_API_KEY"); + expect(panel?.textContent).toContain("openai_api_key"); + expect(findControl("Copy link", panel)).toBeDefined(); + expect(panel?.textContent).toContain("No agent can use this secret yet."); }); it("writes a new value through the provider for external reference secrets", async () => { @@ -637,102 +835,234 @@ describe("Secrets page layout", () => { providerConfigId: "vault-aws", }); mockSecretsApi.list.mockResolvedValue([externalSecret]); - mockSecretsApi.usage.mockResolvedValue({ secretId: "secret-neon", bindings: [] }); - mockSecretsApi.accessEvents.mockResolvedValue([]); mockSecretsApi.rotate.mockResolvedValue({ ...externalSecret, latestVersion: 2 }); - const root = await renderSecrets(container, { path: "/company/settings/secrets?secret=secret-neon" }); + await renderSecrets(container, { path: "/company/settings/secrets?secret=secret-neon" }); - // AWS supports write-through, so the primary action is a value update. - const updateValueButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Update value", - ) as HTMLButtonElement | undefined; - expect(updateValueButton).not.toBeUndefined(); - await act(async () => { - updateValueButton?.click(); - }); - await flushReact(); + // The panel names the vault the secret is linked from. + expect(detailsPanel()?.textContent).toContain("Linked from"); + expect(detailsPanel()?.textContent).toContain("AWS production · AWS Secrets Manager"); - // Both modes are offered; "Write new value" is the default. - expect(document.body.textContent).toContain("Write new value"); - expect(document.body.textContent).toContain("Change reference"); + // AWS supports write-through, so the primary action replaces the value. + await click(findControl("Replace value", detailsPanel())); - const valueTextarea = document.getElementById("rotate-value") as HTMLTextAreaElement | null; - expect(valueTextarea).not.toBeNull(); - setTextareaValue(valueTextarea!, "rotated-neon-admin-key"); - await flushReact(); + // Both modes are offered; replacing the value is the default. + const dialog = openDialog()!; + expect(dialog.textContent).toContain("Replace value"); + expect(dialog.textContent).toContain("Change reference"); - const dialog = valueTextarea!.closest("[role='dialog']") as HTMLElement; - const submitButton = Array.from(dialog.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Update value", - ) as HTMLButtonElement | undefined; + await typeInto("rotate-value", "rotated-neon-admin-key"); + const submitButton = findControl("Save new value", dialog) as HTMLButtonElement | undefined; expect(submitButton?.disabled).toBe(false); - await act(async () => { - submitButton?.click(); - }); - await flushReact(); + await click(submitButton); expect(mockSecretsApi.rotate).toHaveBeenCalledWith("secret-neon", { value: "rotated-neon-admin-key", providerConfigId: "vault-aws", }); - - await act(async () => { - root.unmount(); - }); }); it("merges company secrets and each-user definitions into the Secrets list", async () => { - mockSecretsApi.list.mockResolvedValue([makeCompanySecret()]); + mockSecretsApi.list.mockResolvedValue([makeCompanySecret({ referenceCount: 0 })]); mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([makeUserSecretDefinition()]); - const root = await renderSecrets(container); + await renderSecrets(container); - expect(container.textContent).toContain("OPENAI_API_KEY"); - expect(container.textContent).toContain("Personal GitHub token"); - expect(container.textContent).toContain("Organization"); - expect(container.textContent).toContain("Each user"); - expect(container.textContent).toContain("3/5 set"); + const tableView = teamTable()!; + expect([...tableView.querySelectorAll('[role="columnheader"]')].map((header) => header.textContent)).toEqual([ + "Name", + "Provided by", + "Used by", + "Updated", + "Actions", + ]); + expect(teamRow("OPENAI_API_KEY")?.textContent).toContain("Organization"); + expect(teamRow("Personal GitHub token")?.textContent).toContain("Each user · 3 of 5 added"); + // Rows no longer repeat the key, the always-on status, the version or the storage mode. + expect(tableView.textContent).not.toContain("openai_api_key"); + expect(tableView.textContent).not.toContain("PERSONAL_GH_TOKEN"); + expect(tableView.textContent).not.toContain("Active"); + expect(tableView.textContent).not.toContain("v1"); + expect(tableView.textContent).not.toContain("managed"); expect(container.textContent).not.toContain("User secret definitions"); const listContainer = container.querySelector('[data-testid="secrets-list-container"]'); - const tableView = container.querySelector('[data-testid="secrets-table-view"]'); expect(listContainer?.parentElement?.className).not.toContain("overflow-y-auto"); const cardView = container.querySelector('[data-testid="secrets-card-view"]'); expect(listContainer?.className).toContain("@container"); - expect(tableView?.className).toContain("@min-[40rem]:block"); - expect(tableView?.className).not.toContain("md:block"); - // Grid template lives in the token layer: --gtc-54 in ui/src/index.css - // carries the original minmax(12rem,2.4fr)... template verbatim. - expect(tableView?.querySelector("[role='row']")?.className).toContain("grid-cols-(--gtc-54)"); + expect(tableView.className).toContain("@min-[40rem]:block"); + expect(tableView.className).not.toContain("md:block"); + // Grid template lives in the token layer: --gtc-54 in ui/src/index.css. + expect(tableView.querySelector("[role='row']")?.className).toContain("grid-cols-(--gtc-54)"); expect(cardView?.className).toContain("@min-[40rem]:hidden"); expect(cardView?.className).not.toContain("md:hidden"); expect(mockSecretsApi.list).toHaveBeenCalledWith("company-1"); expect(mockSecretsApi.listUserSecretDefinitions).toHaveBeenCalledWith("company-1"); + }); - await act(async () => { - root.unmount(); - }); + it("shows who uses each secret from the agents' own configs", async () => { + mockSecretsApi.list.mockResolvedValue([ + makeCompanySecret({ id: "s-one", key: "one", name: "ONE_AGENT", referenceCount: 1 }), + makeCompanySecret({ id: "s-two", key: "two", name: "TWO_AGENTS", referenceCount: 2 }), + makeCompanySecret({ id: "s-none", key: "none", name: "UNUSED", referenceCount: 0 }), + makeCompanySecret({ id: "s-project", key: "project", name: "PROJECT_ONLY", referenceCount: 3 }), + makeCompanySecret({ id: "s-linked", key: "linked", name: "LINKED", provider: "aws_secrets_manager", + managedMode: "external_reference", externalRef: "arn:aws:secretsmanager:us-east-1:1:secret:x", + providerConfigId: "vault-aws", referenceCount: 0 }), + ]); + mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([makeUserSecretDefinition()]); + mockAgentsApi.list.mockResolvedValue([ + makeAgent({ + id: "agent-coder", + name: "CodexCoder", + adapterConfig: { + env: { + ONE_AGENT: secretRef("s-one"), + TWO_AGENTS: secretRef("s-two"), + GH: { type: "user_secret_ref", key: "PERSONAL_GH_TOKEN" }, + }, + }, + }), + makeAgent({ id: "agent-reviewer", name: "Reviewer", adapterConfig: { "access.SHARED": secretRef("s-two") } }), + makeAgent({ id: "agent-gone", name: "Retired", status: "terminated", adapterConfig: { env: { X: secretRef("s-none") } } }), + ]); + await renderSecrets(container); + + expect(mockAgentsApi.list).toHaveBeenCalledWith("company-1"); + expect(teamRow("ONE_AGENT")?.textContent).toContain("CodexCoder"); + expect(teamRow("TWO_AGENTS")?.textContent).toContain("2 agents"); + expect(teamRow("TWO_AGENTS")?.querySelectorAll('[data-slot="agent-avatar"]')).toHaveLength(2); + expect(teamRow("UNUSED")?.textContent).toContain("Not used yet"); + expect(teamRow("PROJECT_ONLY")?.textContent).toContain("Set in 3 places"); + expect(teamRow("Personal GitHub token")?.textContent).toContain("CodexCoder"); + // Only a secret kept outside Paperclip's own storage names its vault. + expect(teamRow("LINKED")?.textContent).toContain("Linked from AWS production · AWS Secrets Manager"); + expect(teamRow("UNUSED")?.textContent).not.toContain("Stored in"); + }); + + it("keeps Paperclip's own secrets out of the team list, grouped and collapsed", async () => { + mockSecretsApi.list.mockResolvedValue([ + makeCompanySecret(), + makeCompanySecret({ id: "s-chat", key: "chat_slack_5c860bfd_bottoken_565d9081", name: "chat.slack.5c860bfd.botToken.565d9081" }), + makeCompanySecret({ + id: "s-neon", + key: "tool_app.500df1ea-4b23-4c24-93a4-c9802c893448.oauth_access_token", + name: "Neon for the organization OAuth access token 80255b18", + }), + makeCompanySecret({ + id: "s-routine", + key: "routine-604ffeed-2bba-4ade-b87d-3db79f481370-99f65874bf29", + name: "routine-604ffeed-2bba-4ade-b87d-3db79f481370-99f65874bf29", + }), + ]); + mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([ + makeUserSecretDefinition({ + id: "def-github-oauth", + key: "tool_oauth.9ad19afd-323d-47c7-8b95-3d58fda94dcf.oauth_access_token", + name: "GitHub access token", + }), + ]); + await renderSecrets(container); + + expect(teamTable()?.textContent).toContain("OPENAI_API_KEY"); + expect(teamTable()?.textContent).not.toContain("chat.slack"); + expect(teamTable()?.textContent).not.toContain("Neon"); + expect(teamTable()?.textContent).not.toContain("GitHub access token"); + + const section = container.querySelector('[data-testid="managed-secrets"]') as HTMLElement; + const toggle = section.querySelector("button") as HTMLButtonElement; + expect(toggle.textContent).toContain("Managed by Paperclip"); + expect(toggle.textContent).toContain("4"); + expect(toggle.getAttribute("aria-expanded")).toBe("false"); + expect(section.querySelector('[data-testid^="managed-secret-group-"]')).toBeNull(); + + await click(toggle); + + expect( + [...section.querySelectorAll('[data-testid^="managed-secret-group-"]')].map((group) => + group.getAttribute("data-testid"), + ), + ).toEqual(["managed-secret-group-connections", "managed-secret-group-chat", "managed-secret-group-routines"]); + const connections = section.querySelector('[data-testid="managed-secret-group-connections"]') as HTMLElement; + expect(connections.textContent).toContain("2 secrets"); + expect(section.textContent).not.toContain("Slack bot token"); + + await click(section.querySelector('[data-testid="managed-secret-group-chat"] button')); + await click(connections.querySelector("button")); + await click(section.querySelector('[data-testid="managed-secret-group-routines"] button')); + + // Record ids give way to what each secret is for. + expect(section.textContent).toContain("Slack bot token"); + expect(section.textContent).toContain("Neon for the organization OAuth access token"); + expect(section.textContent).toContain("GitHub access token"); + expect(section.textContent).toContain("Routine webhook secret"); + expect(section.textContent).not.toContain("565d9081"); + expect(section.textContent).not.toContain("80255b18"); + expect(section.textContent).not.toContain("604ffeed"); + expect(connections.textContent).toContain("Each user"); + }); + + it("opens the managed section for a search and hides it inside a folder", async () => { + mockSecretsApi.list.mockResolvedValue([ + makeCompanySecret({ id: "s-dev", key: "dev_api", name: "dev/api" }), + makeCompanySecret({ id: "s-chat", key: "chat_slack_5c860bfd_bottoken_565d9081", name: "chat.slack.5c860bfd.botToken.565d9081" }), + ]); + const root = await renderSecrets(container); + + await act(async () => + setInputValue(container.querySelector('input[aria-label="Search secrets"]') as HTMLInputElement, "slack"), + ); + await flushReact(); + + const section = container.querySelector('[data-testid="managed-secrets"]') as HTMLElement; + expect(section.querySelector("button")?.getAttribute("aria-expanded")).toBe("true"); + expect(section.textContent).toContain("Slack bot token"); + expect(container.textContent).toContain("None of the secrets your team added match."); + + await act(async () => root.unmount()); + const inFolder = document.createElement("div"); + document.body.appendChild(inFolder); + await renderSecrets(inFolder, { path: "/?path=dev" }); + + expect(inFolder.querySelector('[data-testid="secrets-table-view"]')?.textContent).toContain("api"); + expect(inFolder.querySelector('[data-testid="managed-secrets"]')).toBeNull(); + }); + + it("lists active and disabled secrets by default and archived ones on request", async () => { + mockSecretsApi.list.mockResolvedValue([ + makeCompanySecret(), + makeCompanySecret({ id: "s-off", key: "legacy", name: "LEGACY_DSN", status: "disabled" }), + makeCompanySecret({ id: "s-old", key: "heroku", name: "HEROKU_API_KEY", status: "archived" }), + ]); + await renderSecrets(container); + + expect(teamRow("OPENAI_API_KEY")).toBeDefined(); + expect(teamRow("LEGACY_DSN")?.textContent).toContain("Disabled"); + expect(teamRow("HEROKU_API_KEY")).toBeUndefined(); + expect(container.querySelector('button[aria-label^="Remove filter"]')).toBeNull(); + + await chooseFilter("Archived"); + + expect(teamRow("HEROKU_API_KEY")?.textContent).toContain("Archived"); + expect(teamRow("OPENAI_API_KEY")).toBeUndefined(); + + await click(container.querySelector('button[aria-label="Remove filter Archived"]')); + + expect(teamRow("OPENAI_API_KEY")).toBeDefined(); + expect(teamRow("HEROKU_API_KEY")).toBeUndefined(); + + await chooseFilter("Each user"); + + expect(container.querySelector('button[aria-label="Remove filter Each user"]')).not.toBeNull(); + expect(container.textContent).toContain("No secrets match your filters."); }); it("keeps the loaded secrets list visible when a refetch fails during an outage", async () => { mockSecretsApi.list.mockResolvedValue([makeCompanySecret()]); - const root = createRoot(container); const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } }, }); - - await act(async () => { - root.render( - - - - - , - ); - }); - await flushReact(); - await flushReact(); + await renderSecrets(container, { queryClient }); expect(container.textContent).toContain("OPENAI_API_KEY"); mockSecretsApi.list.mockRejectedValue( @@ -748,10 +1078,6 @@ describe("Secrets page layout", () => { expect(container.textContent).toContain("OPENAI_API_KEY"); expect(container.querySelector('[data-query-view="error"]')).toBeNull(); expect(container.textContent).not.toContain("Paperclip is restarting."); - - await act(async () => { - root.unmount(); - }); }); it("shows readable copy and a Retry button when the secrets list fails to load", async () => { @@ -790,75 +1116,99 @@ describe("Secrets page layout", () => { expect(mockSecretsApi.list).toHaveBeenCalledTimes(2); expect(container.querySelector('[data-query-view="error"]')).toBeNull(); + }); + it("keeps Create enabled and says what is missing", async () => { + await renderSecrets(container); + await openNewSecret(); + + const dialog = openDialog()!; + const createButton = findControl("Create secret", dialog) as HTMLButtonElement; + expect(createButton.disabled).toBe(false); + expect(dialog.querySelector('[role="alert"]')).toBeNull(); + + await submitNewSecret(); + + expect(dialog.querySelector('[role="alert"]')?.textContent).toBe("Give the secret a name."); + expect(byId("new-secret-name")?.getAttribute("aria-invalid")).toBe("true"); + + await typeInto("new-secret-name", "Linear API key"); + // The name is not an environment variable, so the dialog says what agents will read. + expect(byId("new-secret-name-hint")?.textContent).toBe("Agents read it as LINEAR_API_KEY."); + await submitNewSecret(); + + expect(byId("new-secret-value-hint")?.textContent).toBe("Paste the value to store."); + expect(byId("new-secret-value-hint")?.getAttribute("role")).toBe("alert"); + expect(mockSecretsApi.create).not.toHaveBeenCalled(); + }); + + it("keeps a value pasted into Value as it is, even when it looks like NAME=value", async () => { + await renderSecrets(container); + await openNewSecret(); + await typeInto("new-secret-name", "Signing key"); + + // Base64 padding makes a raw credential look like an assignment. + const paste = new Event("paste", { bubbles: true, cancelable: true }); + Object.defineProperty(paste, "clipboardData", { value: { getData: () => "abc123==" } }); await act(async () => { - root.unmount(); + byId("new-secret-value")!.dispatchEvent(paste); }); + await flushReact(); + + expect(paste.defaultPrevented).toBe(false); + expect(byId("new-secret-name")?.value).toBe("Signing key"); + expect(byId("new-secret-value")?.value).toBe(""); + }); + + it("fills name and value from a pasted NAME=value and masks the value until shown", async () => { + await renderSecrets(container); + await openNewSecret(); + + const paste = new Event("paste", { bubbles: true, cancelable: true }); + Object.defineProperty(paste, "clipboardData", { + value: { getData: () => 'export GITHUB_TOKEN="ghp_example"' }, + }); + await act(async () => { + byId("new-secret-name")!.dispatchEvent(paste); + }); + await flushReact(); + + expect(paste.defaultPrevented).toBe(true); + expect(byId("new-secret-name")?.value).toBe("GITHUB_TOKEN"); + const value = byId("new-secret-value")!; + expect(value.value).toBe("ghp_example"); + + expect(value.className).toContain("secret-value-masked"); + const toggle = openDialog()!.querySelector('button[aria-label="Show value"]') as HTMLButtonElement; + expect(toggle.getAttribute("aria-pressed")).toBe("false"); + await click(toggle); + expect(value.className).not.toContain("secret-value-masked"); + expect(openDialog()!.querySelector('button[aria-label="Hide value"]')?.getAttribute("aria-pressed")).toBe("true"); }); it("creates an each-user secret from the unified New secret dialog", async () => { const definition = makeUserSecretDefinition({ name: "Personal GitHub token" }); mockSecretsApi.createUserSecretDefinition.mockResolvedValueOnce(definition); - const root = await renderSecrets(container); + await renderSecrets(container); + await openNewSecret(); - const newSecretButton = Array.from(container.querySelectorAll("button")).find( - (button) => button.textContent?.includes("New secret"), - ) as HTMLButtonElement | undefined; - await act(async () => { - newSecretButton?.click(); - }); - await flushReact(); + const radios = [...document.querySelectorAll('[role="radio"]')]; + expect(radios.map((radio) => radio.getAttribute("aria-checked"))).toEqual(["true", "false"]); + expect(byId("new-secret-value")).not.toBeNull(); - const companyKeyInput = document.getElementById("new-secret-key") as HTMLInputElement; - expect(companyKeyInput.readOnly).toBe(true); + await chooseWhoProvides("Each user"); - const editKeyButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Edit", - ) as HTMLButtonElement | undefined; - await act(async () => { - editKeyButton?.click(); - }); - await flushReact(); - expect(companyKeyInput.readOnly).toBe(false); + expect(radios.map((radio) => radio.getAttribute("aria-checked"))).toEqual(["false", "true"]); + expect(byId("new-secret-provider")).toBeNull(); + expect(byId("new-secret-vault")).toBeNull(); + expect(byId("new-secret-value")).toBeNull(); - const eachUserButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Each user", - ) as HTMLButtonElement | undefined; - await act(async () => { - eachUserButton?.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); - eachUserButton?.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Enter" })); - eachUserButton?.click(); - }); - await flushReact(); + await typeInto("new-secret-name", "Personal GitHub token"); + await typeInto("new-secret-usage-guidance", "Create a fine-grained PAT with repo read access."); + await openMoreOptions(); + expect(byId("new-secret-key")?.value).toBe("PERSONAL_GITHUB_TOKEN"); - const nameInput = document.getElementById("new-secret-name") as HTMLInputElement; - const keyInput = document.getElementById("new-secret-key") as HTMLInputElement; - const usageGuidance = document.getElementById("new-secret-usage-guidance") as HTMLTextAreaElement; - expect(keyInput.readOnly).toBe(true); - expect( - Array.from(document.body.querySelectorAll("button")).some( - (button) => button.textContent?.trim() === "Edit", - ), - ).toBe(true); - expect(document.getElementById("new-secret-provider")).toBeNull(); - expect(document.getElementById("new-secret-vault")).toBeNull(); - expect(document.getElementById("new-secret-value")).toBeNull(); - - await act(async () => { - setInputValue(nameInput, "Personal GitHub token"); - setTextareaValue(usageGuidance, "Create a fine-grained PAT with repo read access."); - }); - await flushReact(); - - expect(keyInput.value).toBe("PERSONAL_GITHUB_TOKEN"); - - const createButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.includes("Create user-provided secret"), - ) as HTMLButtonElement | undefined; - await act(async () => { - createButton?.click(); - }); - await flushReact(); + await submitNewSecret(); expect(mockSecretsApi.createUserSecretDefinition).toHaveBeenCalledWith("company-1", { name: "Personal GitHub token", @@ -868,121 +1218,303 @@ describe("Secrets page layout", () => { status: "active", }); expect(mockSecretsApi.create).not.toHaveBeenCalled(); - - await act(async () => { - root.unmount(); - }); }); it("opens the New secret dialog when provider queries fail", async () => { mockSecretsApi.providers.mockRejectedValueOnce(new ApiError("Providers unavailable", 403, null)); mockSecretsApi.providerConfigs.mockRejectedValueOnce(new ApiError("Provider vaults unavailable", 403, null)); - const root = await renderSecrets(container); + await renderSecrets(container); + await openNewSecret(); - const newSecretButton = Array.from(container.querySelectorAll("button")).find( - (button) => button.textContent?.includes("New secret"), - ) as HTMLButtonElement | undefined; - await act(async () => { - newSecretButton?.click(); - }); - await flushReact(); - - expect(document.body.textContent).toContain("Create secret"); - expect(document.body.textContent).toContain("Select a provider."); - - await act(async () => { - root.unmount(); - }); + expect(bodyText()).toContain("Create secret"); + expect(bodyText()).toContain("Select a provider."); }); - it.each([false, true])("opens the admin-visible each-user secure-entry surface (definition link: %s)", async (fromLink) => { + it("groups a large inventory with work proportional to its size, including while searching", async () => { + const managed = Array.from({ length: 1700 }, (_, index) => + makeCompanySecret({ + id: `managed-${index}`, + name: `chat.slack.${String(index).padStart(8, "0")}.botToken.565d9081`, + key: `chat_slack_${index}_bottoken`, + }), + ); + const team = Array.from({ length: 300 }, (_, index) => + makeCompanySecret({ id: `team-${index}`, name: `team/SERVICE_${index}_KEY`, key: `service-${index}-key` }), + ); + const total = managed.length + team.length; + mockSecretsApi.list.mockResolvedValue([...managed, ...team]); + mockAgentsApi.list.mockResolvedValue( + Array.from({ length: 200 }, (_, index) => + makeAgent({ + id: `agent-${index}`, + name: `Agent ${index}`, + adapterConfig: { env: Object.fromEntries(team.slice(index, index + 10).map((secret) => [`VAR_${secret.id}`, secretRef(secret.id)])) }, + }), + ), + ); + originWork.calls = 0; + // Each row is classified once per change to the filtered list. Loading settles in a few such changes. + originWork.limit = total * 4; + try { + await renderSecrets(container); + await waitForReact(() => document.querySelector('[data-testid="managed-secrets"]') !== null); + + expect(originWork.calls).toBeGreaterThanOrEqual(total); + expect(document.querySelector('[data-testid="managed-secrets"]')?.textContent).toContain("1700"); + + // A search that matches a handful of rows classifies only the rows that match. + originWork.calls = 0; + originWork.limit = total; + const search = document.querySelector('input[aria-label="Search secrets"]')!; + await act(async () => setInputValue(search, "SERVICE_29")); + await flushReact(); + + // SERVICE_29 and SERVICE_290 to SERVICE_299. + expect(bodyText()).toContain("11 matches across all folders"); + expect(originWork.calls).toBeLessThanOrEqual(total); + expect(document.querySelector('[data-testid="managed-secrets"]')).toBeNull(); + } finally { + originWork.limit = Number.POSITIVE_INFINITY; + } + }); + + it("offers Stored in only when there is more than one place to store a secret", async () => { + mockSecretsApi.providers.mockResolvedValue([providers[0], { ...providers[1], configured: false }]); + mockSecretsApi.providerConfigs.mockResolvedValue([]); + const root = await renderSecrets(container); + await openNewSecret(); + + expect(byId("new-secret-vault")).toBeNull(); + expect(byId("new-secret-value-hint")?.textContent).toContain("Encrypted and stored in Paperclip."); + + await act(async () => root.unmount()); + document.body.innerHTML = ""; + const second = document.createElement("div"); + document.body.appendChild(second); + mockSecretsApi.providers.mockResolvedValue(providers); + mockSecretsApi.providerConfigs.mockResolvedValue([ + ...providerConfigs, + makeProviderConfig({ + id: "vault-down", + provider: "aws_secrets_manager", + displayName: "staging-eu", + healthStatus: "error", + healthMessage: "AWS denied access.", + }), + ]); + await renderSecrets(second); + await openNewSecret(); + + const select = byId("new-secret-vault")!; + expect(select.value).toBe("local_encrypted:vault-local"); + expect([...select.options].map((option) => option.textContent)).toEqual([ + "Local default · Paperclip-encrypted (default)", + "AWS production · AWS Secrets Manager", + "staging-eu · AWS Secrets Manager — unavailable", + ]); + expect(byId("new-secret-value-hint")?.textContent).toBe("After saving it can be replaced, never viewed."); + }); + + it("refuses to create in storage that is unavailable and says why", async () => { + mockSecretsApi.providers.mockResolvedValue([providers[0]]); + mockSecretsApi.providerConfigs.mockResolvedValue([]); + mockSecretsApi.providerHealth.mockResolvedValue({ + providers: [{ provider: "local_encrypted", status: "error", message: "The encryption key is missing." }], + }); + await renderSecrets(container); + await openNewSecret(); + await typeInto("new-secret-name", "GITHUB_TOKEN"); + await typeInto("new-secret-value", "ghp_example"); + + expect(openDialog()?.querySelector('[role="alert"]')?.textContent).toBe( + "Local encrypted health check failed: The encryption key is missing.", + ); + const createButton = findControl("Create secret", openDialog()) as HTMLButtonElement; + expect(createButton.disabled).toBe(false); + await submitNewSecret(); + + expect(mockSecretsApi.create).not.toHaveBeenCalled(); + }); + + it("explains an unavailable vault in Stored in, blocks create, and leads to Vaults", async () => { + mockSecretsApi.providerConfigs.mockResolvedValue([ + ...providerConfigs, + makeProviderConfig({ + id: "vault-down", + provider: "aws_secrets_manager", + displayName: "staging-eu", + healthStatus: "error", + healthMessage: "AWS denied access.", + }), + ]); + await renderSecrets(container); + await openNewSecret(); + await typeInto("new-secret-name", "GITHUB_TOKEN"); + await typeInto("new-secret-value", "ghp_example"); + + await act(async () => setSelectValue(byId("new-secret-vault")!, "aws_secrets_manager:vault-down")); + await flushReact(); + + const select = byId("new-secret-vault")!; + expect(select.value).toBe("aws_secrets_manager:vault-down"); + expect(select.getAttribute("aria-invalid")).toBe("true"); + expect(byId("new-secret-vault-hint")?.textContent).toBe( + "AWS denied access. Choose another vault, or fix it in Vaults.", + ); + // Linking is not offered for a vault Paperclip cannot reach. + expect(findControl("Use a secret that already exists", openDialog(), true)).toBeUndefined(); + + await submitNewSecret(); + expect(mockSecretsApi.create).not.toHaveBeenCalled(); + + await click(findControl("fix it in Vaults", openDialog())); + + expect(openDialog()).toBeNull(); + expect(container.querySelector('[data-testid="provider-vault-vault-down"]')?.textContent).toContain( + "AWS denied access.", + ); + }); + + it("gives the chosen agents access as soon as the secret is created", async () => { + const coder = makeAgent({ id: "agent-coder", name: "CodexCoder", adapterConfig: { model: "x", env: { OTHER: "1" } } }); + mockAgentsApi.list.mockResolvedValue([coder, makeAgent({ id: "agent-gone", name: "Retired", status: "terminated" })]); + mockAgentsApi.get.mockResolvedValue(coder); + mockAgentsApi.update.mockResolvedValue(coder); + resolveCreateWith(makeCompanySecret({ id: "created", key: "linear-api-key", name: "Linear API key" })); + await renderSecrets(container); + await openNewSecret(); + await typeInto("new-secret-name", "Linear API key"); + await typeInto("new-secret-value", "lin_api_example"); + + await click(findControl("Add agent", openDialog(), true)); + expect(document.querySelector('[aria-label="Allow Retired"]')).toBeNull(); + await click(document.querySelector('[aria-label="Allow CodexCoder"]')); + expect(openDialog()?.querySelector('button[aria-label="Remove CodexCoder"]')).not.toBeNull(); + + await submitNewSecret(); + await waitForReact(() => mockAgentsApi.update.mock.calls.length > 0); + + expect(mockSecretsApi.create).toHaveBeenCalledWith( + "company-1", + expect.objectContaining({ name: "Linear API key", value: "lin_api_example", managedMode: "paperclip_managed" }), + ); + expect(mockAgentsApi.get).toHaveBeenCalledWith("agent-coder", "company-1"); + // The binding is merged into the agent's freshest config, named from the secret. + expect(mockAgentsApi.update).toHaveBeenCalledWith( + "agent-coder", + { + adapterConfig: { model: "x", env: { OTHER: "1", LINEAR_API_KEY: secretRef("created") } }, + replaceAdapterConfig: true, + }, + "company-1", + ); + // The new secret opens in its details panel; that is the confirmation, not a toast. + await waitForReact(() => detailsPanel() != null); + expect(mockPushToast).not.toHaveBeenCalled(); + }); + + it("keeps the new secret and reports agents that could not be given access", async () => { + const coder = makeAgent({ id: "agent-coder", name: "CodexCoder", adapterConfig: { env: { LINEAR_API_KEY: "taken" } } }); + mockAgentsApi.list.mockResolvedValue([coder]); + mockAgentsApi.get.mockResolvedValue(coder); + resolveCreateWith(makeCompanySecret({ id: "created", key: "linear-api-key", name: "Linear API key" })); + await renderSecrets(container); + await openNewSecret(); + await typeInto("new-secret-name", "Linear API key"); + await typeInto("new-secret-value", "lin_api_example"); + await click(findControl("Add agent", openDialog(), true)); + await click(document.querySelector('[aria-label="Allow CodexCoder"]')); + + await submitNewSecret(); + await waitForReact(() => mockPushToast.mock.calls.length > 0); + + expect(mockSecretsApi.create).toHaveBeenCalledTimes(1); + expect(mockAgentsApi.update).not.toHaveBeenCalled(); + expect(mockPushToast).toHaveBeenCalledWith( + expect.objectContaining({ + title: "Some agents were not given access", + body: expect.stringContaining("CodexCoder already has an env var named LINEAR_API_KEY."), + tone: "error", + }), + ); + await waitForReact(() => detailsPanel() != null); + expect(detailsPanel()?.textContent).toContain("Linear API key"); + }); + + it("asks for a usable name before giving agents access to one they could not read", async () => { + mockAgentsApi.list.mockResolvedValue([makeAgent({ id: "agent-coder", name: "CodexCoder" })]); + await renderSecrets(container); + await openNewSecret(); + await typeInto("new-secret-name", "2024 billing key"); + await typeInto("new-secret-value", "example"); + await click(findControl("Add agent", openDialog(), true)); + await click(document.querySelector('[aria-label="Allow CodexCoder"]')); + + await submitNewSecret(); + + expect(openDialog()?.textContent).toContain("Agents can only read names made of letters, digits and underscores"); + expect(mockSecretsApi.create).not.toHaveBeenCalled(); + }); + + it.each([false, true])("opens the admin-visible each-user details panel (definition link: %s)", async (fromLink) => { const definition = makeUserSecretDefinition(); mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([definition]); mockSecretsApi.listMyUserSecrets.mockResolvedValue([{ definition, secret: null }]); - const root = await renderSecrets(container, { + await renderSecrets(container, { path: fromLink ? `/company/settings/secrets?definition=${definition.id}` : "/company/settings/secrets", }); - const definitionRow = Array.from(container.querySelectorAll("[role='row']")).find( - (row) => row.textContent?.includes("Personal GitHub token"), - ) as HTMLElement | undefined; - if (!fromLink) await act(async () => { definitionRow?.click(); }); - await waitForReact(() => document.body.textContent?.includes("Details") ?? false); + if (!fromLink) await click(teamRow("Personal GitHub token")); + await waitForReact(() => detailsPanel()?.textContent?.includes("3 of 5") ?? false); - expect(document.body.textContent).toContain("Personal GitHub token"); - expect(document.body.textContent).toContain("Details"); - expect(document.body.textContent).toContain("Coverage"); - expect(document.body.textContent).toContain("Usage"); - expect(document.body.textContent).toContain("Access events"); + const panel = detailsPanel()!; + expect(panel.textContent).toContain("Personal GitHub token"); + expect(panel.textContent).toContain("PERSONAL_GH_TOKEN"); + expect(panel.querySelectorAll('[role="tab"]')).toHaveLength(0); + expect([...panel.querySelectorAll("h3")].map((heading) => heading.textContent)).toEqual([ + "Agents that can use it", + "Members", + "Details", + ]); + expect(panel.querySelector('[aria-label="Members"]')?.textContent).toContain( + "3 of 5 members have added their value.", + ); + expect(panel.querySelector('[aria-label="Details"]')?.textContent).toContain( + "Create a fine-grained PAT with repo read access.", + ); + expect(panel.textContent).not.toContain("View in Coverage"); - const viewCoverageButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.includes("View in Coverage"), - ) as HTMLButtonElement | undefined; - await act(async () => { - viewCoverageButton?.click(); - }); - await flushReact(); + await click(findControl("Set my value", panel)); - expect(document.body.textContent).toContain("3 of 5 set"); - expect(document.body.textContent).toContain("Secret values are never shown here"); - - const setValueButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.includes("Set my value"), - ) as HTMLButtonElement | undefined; - await act(async () => { - setValueButton?.click(); - }); - await flushReact(); - - expect(document.body.textContent).toContain("Set your value"); - expect(document.body.textContent).toContain("PERSONAL_GH_TOKEN"); - - await act(async () => { - root.unmount(); - }); + expect(bodyText()).toContain("Set your value"); }); it("opens the current person's Set value form through My secrets without admin definitions", async () => { const definition = makeUserSecretDefinition({ key: "PERSONAL_PUBLISHING_KEY", name: "Personal publishing credential" }); mockSecretsApi.listUserSecretDefinitions.mockRejectedValue(new ApiError("Forbidden", 403, null)); mockSecretsApi.listMyUserSecrets.mockResolvedValue([{ definition, secret: null }]); - const root = await renderSecrets(container, { path: "/company/settings/secrets" }); - const mine = [...container.querySelectorAll('[role="tab"]')].find(node => node.textContent === "My secrets") as HTMLElement | undefined; - expect(mine).toBeDefined(); - await act(async () => { mine!.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); mine!.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Enter" })); mine!.click(); }); - await flushReact(); - const row = [...container.querySelectorAll("li")].find(node => node.textContent?.includes("PERSONAL_PUBLISHING_KEY")); - const set = [...(row?.querySelectorAll("button") ?? [])].find(node => node.textContent === "Set value"); - expect(set).toBeDefined(); - await act(async () => { set!.click(); }); await flushReact(); - expect(document.body.textContent).toContain("Set your value"); - expect(document.body.querySelector('[role="dialog"]')?.textContent).toContain("Personal publishing credential"); + await renderSecrets(container, { path: "/company/settings/secrets" }); + + await openTab("My secrets"); + const row = [...container.querySelectorAll("li")].find((node) => + node.textContent?.includes("Personal publishing credential"), + ); + await click(findControl("Set value", row)); + + expect(bodyText()).toContain("Set your value"); + expect(openDialog()?.textContent).toContain("Personal publishing credential"); expect(mockSecretsApi.createMyUserSecret).not.toHaveBeenCalled(); - await act(async () => { root.unmount(); }); }); it("keeps the new secret value textarea width-constrained for long tokens", async () => { - const root = await renderSecrets(container); + await renderSecrets(container); + await openNewSecret(); - const newSecretButton = Array.from(container.querySelectorAll("button")).find( - (button) => button.textContent?.includes("New secret"), - ) as HTMLButtonElement | undefined; - expect(newSecretButton).toBeDefined(); - - await act(async () => { - newSecretButton?.click(); - }); - await flushReact(); - - const secretValueTextarea = document.body.querySelector("#new-secret-value") as HTMLTextAreaElement | null; + const secretValueTextarea = byId("new-secret-value"); expect(secretValueTextarea).not.toBeNull(); expect(secretValueTextarea?.className).toContain("min-w-0"); expect(secretValueTextarea?.className).toContain("overflow-x-hidden"); expect(secretValueTextarea?.className).toContain("break-all"); - - await act(async () => { - root.unmount(); - }); }); it("explains AWS managed secret creation failures with actionable safe details", async () => { @@ -1006,32 +1538,24 @@ describe("Secrets page layout", () => { }, }), ); - const root = await renderSecrets(container); + await renderSecrets(container); + await openNewSecret(); - const newSecretButton = Array.from(container.querySelectorAll("button")).find( - (button) => button.textContent?.includes("New secret"), - ) as HTMLButtonElement | undefined; - await act(async () => { - newSecretButton?.click(); - }); + await typeInto("new-secret-name", "AWS test token"); + await act(async () => setSelectValue(byId("new-secret-vault")!, "aws_secrets_manager:vault-aws")); await flushReact(); + await typeInto("new-secret-value", "secret-value"); - await act(async () => { - setInputValue(document.getElementById("new-secret-name") as HTMLInputElement, "AWS test token"); - setSelectValue(document.getElementById("new-secret-provider") as HTMLSelectElement, "aws_secrets_manager"); - setTextareaValue(document.getElementById("new-secret-value") as HTMLTextAreaElement, "secret-value"); - }); - await flushReact(); + // A vault that can hold existing secrets offers linking one instead. + expect(findControl("Use a secret that already exists in AWS production · AWS Secrets Manager instead", openDialog())).toBeDefined(); - const createButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Create secret", - ) as HTMLButtonElement | undefined; - await act(async () => { - createButton?.click(); - }); - await flushReact(); + await submitNewSecret(); await flushReact(); + expect(mockSecretsApi.create).toHaveBeenCalledWith( + "company-1", + expect.objectContaining({ provider: "aws_secrets_manager", providerConfigId: "vault-aws" }), + ); const errorBanner = document.querySelector('[data-testid="secret-create-error"]'); expect(errorBanner?.textContent).toContain("AWS secret creation needs CreateSecret permission"); expect(errorBanner?.textContent).toContain("secretsmanager:CreateSecret"); @@ -1040,50 +1564,251 @@ describe("Secrets page layout", () => { expect(errorBanner?.textContent).toContain("vault-aws"); expect(errorBanner?.textContent).not.toContain(rawProviderMessage); expect(errorBanner?.textContent).not.toContain("123456789012"); + // What was typed is kept so the person can try again. + expect(byId("new-secret-name")?.value).toBe("AWS test token"); + }); - await act(async () => { - root.unmount(); - }); + it("links an existing secret in a vault instead of storing a new value", async () => { + mockSecretsApi.create.mockResolvedValue( + makeCompanySecret({ id: "linked", name: "Production database", managedMode: "external_reference" }), + ); + await renderSecrets(container); + await openNewSecret(); + await typeInto("new-secret-name", "Production database"); + await act(async () => setSelectValue(byId("new-secret-vault")!, "aws_secrets_manager:vault-aws")); + await flushReact(); + + await click(findControl("Use a secret that already exists", openDialog(), true)); + + expect(byId("new-secret-value")).toBeNull(); + await submitNewSecret("Link secret"); + expect(byId("new-secret-ref-hint")?.textContent).toBe("Paste the secret's ARN or path."); + + await typeInto("new-secret-ref", "arn:aws:secretsmanager:us-east-1:1:secret:prod/db"); + await submitNewSecret("Link secret"); + + expect(mockSecretsApi.create).toHaveBeenCalledWith( + "company-1", + expect.objectContaining({ + name: "Production database", + managedMode: "external_reference", + externalRef: "arn:aws:secretsmanager:us-east-1:1:secret:prod/db", + providerConfigId: "vault-aws", + }), + ); }); it("renders generic secret creation failures with a stable selector", async () => { mockSecretsApi.create.mockRejectedValueOnce(new ApiError("Secret creation failed", 500, null)); - const root = await renderSecrets(container); + await renderSecrets(container); + await openNewSecret(); - const newSecretButton = Array.from(container.querySelectorAll("button")).find( - (button) => button.textContent?.includes("New secret"), - ) as HTMLButtonElement | undefined; - await act(async () => { - newSecretButton?.click(); - }); - await flushReact(); - - await act(async () => { - setInputValue(document.getElementById("new-secret-name") as HTMLInputElement, "Failed token"); - setTextareaValue(document.getElementById("new-secret-value") as HTMLTextAreaElement, "secret-value"); - }); - await flushReact(); - - const createButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Create secret", - ) as HTMLButtonElement | undefined; - await act(async () => { - createButton?.click(); - }); - await flushReact(); + await typeInto("new-secret-name", "Failed token"); + await typeInto("new-secret-value", "secret-value"); + await submitNewSecret(); await flushReact(); const errorBanner = document.querySelector('[data-testid="secret-create-error"]'); expect(errorBanner?.textContent).toBe("Secret creation failed"); - - await act(async () => { - root.unmount(); - }); }); - it("discovers AWS provider vault candidates and applies selected values as prefill", async () => { + it("lists vaults flat, with the default following where a new secret would land", async () => { + const { root } = renderVaultsTab(container, { secrets: [makeCompanySecret(), makeCompanySecret({ id: "s2", providerConfigId: "vault-aws" })] }); + + expect(container.querySelector('a[href="#provider-vaults-local_encrypted"]')).toBeNull(); + expect(container.querySelectorAll("ul > li")).toHaveLength(3); + expect(container.textContent).not.toContain("Managed writes"); + expect(container.textContent).not.toContain("External refs"); + expect(container.textContent).not.toContain("Local encrypted"); + + // A usable Paperclip-encrypted vault takes over as the default from built-in storage. + const builtIn = container.querySelector('[data-testid="provider-vault-built-in"]') as HTMLElement; + expect(builtIn.textContent).toContain("Paperclip"); + expect(builtIn.textContent).toContain("1 secret"); + expect(builtIn.textContent).not.toContain("Default"); + const local = container.querySelector('[data-testid="provider-vault-vault-local"]') as HTMLElement; + expect(local.textContent).toContain("Local default"); + expect(local.textContent).toContain("Paperclip-encrypted"); + expect(local.textContent).toContain("Default"); + const aws = container.querySelector('[data-testid="provider-vault-vault-aws"]') as HTMLElement; + expect(aws.textContent).toContain("AWS production"); + expect(aws.textContent).toContain("Not checked yet"); + expect(aws.textContent).toContain("1 secret"); + expect(aws.textContent).not.toContain("Default"); + + flushSync(() => root.unmount()); + const second = document.createElement("div"); + document.body.appendChild(second); + renderVaultsTab(second, { + providerConfigs: [makeProviderConfig({ id: "vault-aws", provider: "aws_secrets_manager", displayName: "AWS production", isDefault: true })], + }); + + expect(second.querySelector('[data-testid="provider-vault-built-in"]')?.textContent).toContain("Default"); + expect(second.querySelector('[data-testid="provider-vault-vault-aws"]')?.textContent).toContain( + "Default for AWS Secrets Manager", + ); + }); + + it("offers Check again on an unhealthy vault and Enable on a disabled one", async () => { + const { handlers } = renderVaultsTab(container, { + providerConfigs: [ + makeProviderConfig({ + id: "vault-down", + provider: "aws_secrets_manager", + displayName: "staging-eu", + healthStatus: "error", + healthMessage: "AWS denied access.", + healthDetails: { code: "access_denied", message: "AWS denied access.", guidance: ["Grant secretsmanager:ListSecrets."] }, + }), + makeProviderConfig({ id: "vault-off", provider: "aws_secrets_manager", displayName: "old-vault", status: "disabled" }), + ], + }); + + const down = container.querySelector('[data-testid="provider-vault-vault-down"]') as HTMLElement; + expect(down.querySelector('[role="alert"]')?.textContent).toContain("AWS denied access."); + expect(down.textContent).toContain("Grant secretsmanager:ListSecrets."); + // Importing from, or defaulting to, a vault that cannot be reached is not offered. + expect((down.querySelector('[data-testid="provider-vault-refresh-secrets-vault-down"]') as HTMLButtonElement).disabled).toBe(true); + expect(findControl("Make default", down)).toBeUndefined(); + await click(findControl("Check again", down)); + expect(handlers.onHealthCheck).toHaveBeenCalledWith(expect.objectContaining({ id: "vault-down" })); + + const off = container.querySelector('[data-testid="provider-vault-vault-off"]') as HTMLElement; + expect(off.textContent).toContain("Disabled"); + expect(findControl("Disable", off)).toBeUndefined(); + await click(findControl("Enable", off)); + expect(handlers.onEnable).toHaveBeenCalledWith(expect.objectContaining({ id: "vault-off" })); + expect(handlers.onDisable).not.toHaveBeenCalled(); + }); + + it("re-enables a disabled vault from the page", async () => { + const disabled = makeProviderConfig({ id: "vault-off", provider: "aws_secrets_manager", displayName: "old-vault", status: "disabled" }); + mockSecretsApi.providerConfigs.mockResolvedValue([disabled]); + mockSecretsApi.updateProviderConfig.mockResolvedValue({ ...disabled, status: "ready" }); + await renderSecrets(container); + await openTab("Vaults"); + + await click(findControl("Enable", document.querySelector('[data-testid="provider-vault-vault-off"]'))); + + expect(mockSecretsApi.updateProviderConfig).toHaveBeenCalledWith("vault-off", { status: "ready" }); + }); + + it("unlocks a vault's menu again once its action has finished", async () => { + const vault = makeProviderConfig({ id: "vault-aws", provider: "aws_secrets_manager", displayName: "prod-us-east" }); + mockSecretsApi.providerConfigs.mockResolvedValue([vault]); + await renderSecrets(container); + await openTab("Vaults"); + const row = () => document.querySelector('[data-testid="provider-vault-vault-aws"]'); + const menuButton = () => row()?.querySelector('button[aria-label="Actions for prod-us-east"]') as HTMLButtonElement; + + await click(findControl("Check connection", row())); + await waitForReact(() => mockPushToast.mock.calls.some(([toast]) => toast.title === "The vault is working")); + await flushReact(); + + expect(mockSecretsApi.checkProviderConfigHealth).toHaveBeenCalledWith("vault-aws"); + expect(menuButton().disabled).toBe(false); + expect((findControl("Edit connection", row()) as HTMLButtonElement).disabled).toBe(false); + }); + + it("returns a disabled draft vault to its draft status, which is the only one the server accepts for it", async () => { + const draft = makeProviderConfig({ id: "vault-gcp", provider: "gcp_secret_manager", displayName: "gcp-draft", status: "disabled" }); + mockSecretsApi.providerConfigs.mockResolvedValue([draft]); + mockSecretsApi.updateProviderConfig.mockResolvedValue({ ...draft, status: "coming_soon" }); + await renderSecrets(container); + await openTab("Vaults"); + + await click(findControl("Enable", document.querySelector('[data-testid="provider-vault-vault-gcp"]'))); + + expect(mockSecretsApi.updateProviderConfig).toHaveBeenCalledWith("vault-gcp", { status: "coming_soon" }); + }); + + it("says so when the check of a new vault could not run, and keeps the vault", async () => { + const saved = makeProviderConfig({ id: "vault-new", provider: "aws_secrets_manager", displayName: "prod-eu-west" }); + mockSecretsApi.createProviderConfig.mockResolvedValue(saved); + mockSecretsApi.checkProviderConfigHealth.mockRejectedValue(new ApiError("Service unavailable", 503, {})); + await renderSecrets(container); + await openAwsVaultDialog(); + await typeInto("vault-name", "prod-eu-west"); + await typeInto("provider-vault-aws-region", "eu-west-1"); + + await click(findControl("Connect vault", openDialog())); + await waitForReact(() => mockPushToast.mock.calls.some(([toast]) => toast.title === "Couldn't check prod-eu-west")); + + const toast = mockPushToast.mock.calls.map(([entry]) => entry).find((entry) => entry.title === "Couldn't check prod-eu-west"); + expect(toast.tone).toBe("error"); + expect(toast.body).toContain("The vault is saved. Use Check connection in its menu to try again."); + expect(mockSecretsApi.createProviderConfig).toHaveBeenCalledTimes(1); + }); + + it("offers the vault kinds Paperclip supports and marks the rest Coming soon", async () => { + const { handlers } = renderVaultsTab(container); + + const items = [...container.querySelectorAll('[role="menu"]')][0].querySelectorAll( + '[role="menuitem"]', + ); + expect([...items].map((item) => [item.textContent, item.disabled])).toEqual([ + ["Paperclip-encrypted vault", false], + ["AWS Secrets Manager", false], + ["GCP Secret ManagerComing soon", true], + ["VaultComing soon", true], + ]); + + await click(items[1]); + expect(handlers.onCreate).toHaveBeenCalledWith("aws_secrets_manager"); + }); + + it("validates the vault form with Connect enabled, then checks the new vault straight away", async () => { + const saved = makeProviderConfig({ id: "vault-new", provider: "aws_secrets_manager", displayName: "prod-eu-west" }); + mockSecretsApi.createProviderConfig.mockResolvedValue(saved); + mockSecretsApi.checkProviderConfigHealth.mockResolvedValue({ status: "error", message: "AWS denied access." }); + await renderSecrets(container); + await openAwsVaultDialog(); + + const dialog = openDialog()!; + expect(dialog.textContent).toContain("Connect AWS Secrets Manager"); + expect(byId("vault-status")).toBeNull(); + expect(byId("vault-provider")).toBeNull(); + const connect = findControl("Connect vault", dialog) as HTMLButtonElement; + expect(connect.disabled).toBe(false); + + await click(connect); + + expect([...dialog.querySelectorAll('[role="alert"]')].map((alert) => alert.textContent)).toEqual([ + "Give the vault a name your team will recognize.", + "Enter the AWS region, such as us-east-1.", + ]); + expect(mockSecretsApi.createProviderConfig).not.toHaveBeenCalled(); + + await typeInto("vault-name", " prod-eu-west "); + await typeInto("provider-vault-aws-region", "eu-west-1"); + await click(findControl("Connect vault", dialog)); + await waitForReact(() => mockSecretsApi.checkProviderConfigHealth.mock.calls.length > 0); + + expect(mockSecretsApi.createProviderConfig).toHaveBeenCalledWith("company-1", { + provider: "aws_secrets_manager", + displayName: "prod-eu-west", + status: "ready", + isDefault: false, + config: { + region: "eu-west-1", + namespace: null, + secretNamePrefix: null, + kmsKeyId: null, + ownerTag: null, + environmentTag: null, + }, + }); + expect(mockSecretsApi.checkProviderConfigHealth).toHaveBeenCalledWith("vault-new"); + await waitForReact(() => mockPushToast.mock.calls.length > 1); + expect(mockPushToast).toHaveBeenCalledWith(expect.objectContaining({ title: "Vault connected", body: "prod-eu-west" })); + expect(mockPushToast).toHaveBeenCalledWith( + expect.objectContaining({ title: "prod-eu-west can't be reached", body: "AWS denied access.", tone: "error" }), + ); + }); + + it("checks AWS access and applies suggested naming settings as prefill", async () => { mockSecretsApi.providerConfigDiscoveryPreview.mockResolvedValueOnce(makeDiscoveryPreview()); - const root = await renderSecrets(container); + await renderSecrets(container); await openAwsVaultDialog(); const discoveryButton = document.querySelector( @@ -1091,21 +1816,15 @@ describe("Secrets page layout", () => { ) as HTMLButtonElement | null; expect(discoveryButton).not.toBeNull(); expect(discoveryButton?.disabled).toBe(true); + // Naming settings stay tucked away until asked for. + expect(byId("provider-vault-secret-name-prefix")).toBeNull(); - const regionInput = document.getElementById("provider-vault-aws-region") as HTMLInputElement | null; - const prefixInput = document.getElementById("provider-vault-secret-name-prefix") as HTMLInputElement | null; - expect(regionInput).not.toBeNull(); - await act(async () => { - setInputValue(regionInput!, "us-east-1"); - setInputValue(prefixInput!, "paperclip"); - }); - await flushReact(); + await openNamingAndTags(); + await typeInto("provider-vault-aws-region", "us-east-1"); + await typeInto("provider-vault-secret-name-prefix", "paperclip"); expect(discoveryButton?.disabled).toBe(false); - await act(async () => { - discoveryButton?.click(); - }); - await flushReact(); + await click(discoveryButton); await flushReact(); expect(mockSecretsApi.providerConfigDiscoveryPreview).toHaveBeenCalledWith("company-1", { @@ -1121,30 +1840,33 @@ describe("Secrets page layout", () => { query: "paperclip", pageSize: 25, }); - expect(document.body.textContent).toContain("AWS production"); + expect(bodyText()).toContain("AWS production"); - const useValuesButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.includes("Use values"), - ) as HTMLButtonElement | undefined; - await act(async () => { - useValuesButton?.click(); - }); - await flushReact(); + await click(findControl("Use values", openDialog(), true)); - expect((document.getElementById("vault-name") as HTMLInputElement).value).toBe("AWS production"); - expect((document.getElementById("provider-vault-namespace") as HTMLInputElement).value).toBe("prod-use1"); - expect((document.getElementById("provider-vault-secret-name-prefix") as HTMLInputElement).value).toBe("paperclip"); - expect((document.getElementById("provider-vault-kms-key-id") as HTMLInputElement).value).toBe("alias/paperclip-secrets"); - expect((document.getElementById("provider-vault-owner-tag") as HTMLInputElement).value).toBe("platform"); - expect((document.getElementById("provider-vault-environment-tag") as HTMLInputElement).value).toBe("production"); + expect(byId("vault-name")?.value).toBe("AWS production"); + expect(byId("provider-vault-namespace")?.value).toBe("prod-use1"); + expect(byId("provider-vault-secret-name-prefix")?.value).toBe("paperclip"); + expect(byId("provider-vault-kms-key-id")?.value).toBe("alias/paperclip-secrets"); + expect(byId("provider-vault-owner-tag")?.value).toBe("platform"); + expect(byId("provider-vault-environment-tag")?.value).toBe("production"); expect(mockSecretsApi.createProviderConfig).not.toHaveBeenCalled(); - - await act(async () => { - root.unmount(); - }); }); - it("shows AWS discovery errors without replacing manual vault form values", async () => { + it("opens the naming settings when suggested values are applied with them closed", async () => { + mockSecretsApi.providerConfigDiscoveryPreview.mockResolvedValueOnce(makeDiscoveryPreview()); + await renderSecrets(container); + await openAwsVaultDialog(); + await typeInto("provider-vault-aws-region", "us-east-1"); + await click(document.querySelector('[data-testid="aws-vault-discovery-button"]')); + await flushReact(); + + await click(findControl("Use values", openDialog(), true)); + + expect(byId("provider-vault-namespace")?.value).toBe("prod-use1"); + }); + + it("shows AWS access errors without replacing manual vault form values", async () => { const rawProviderMessage = "AccessDeniedException: User: arn:aws:sts::123456789012:assumed-role/prod/Paperclip is not authorized"; mockSecretsApi.providerConfigDiscoveryPreview.mockRejectedValueOnce( @@ -1166,24 +1888,13 @@ describe("Secrets page layout", () => { }, }), ); - const root = await renderSecrets(container); + await renderSecrets(container); await openAwsVaultDialog(); + await openNamingAndTags(); - const regionInput = document.getElementById("provider-vault-aws-region") as HTMLInputElement; - const namespaceInput = document.getElementById("provider-vault-namespace") as HTMLInputElement; - await act(async () => { - setInputValue(regionInput, "us-west-2"); - setInputValue(namespaceInput, "manual-prod"); - }); - await flushReact(); - - const discoveryButton = document.querySelector( - '[data-testid="aws-vault-discovery-button"]', - ) as HTMLButtonElement | null; - await act(async () => { - discoveryButton?.click(); - }); - await flushReact(); + await typeInto("provider-vault-aws-region", "us-west-2"); + await typeInto("provider-vault-namespace", "manual-prod"); + await click(document.querySelector('[data-testid="aws-vault-discovery-button"]')); await flushReact(); const errorBanner = document.querySelector('[data-testid="aws-vault-discovery-error"]'); @@ -1200,12 +1911,8 @@ describe("Secrets page layout", () => { expect(errorBanner?.textContent).not.toContain(rawProviderMessage); expect(errorBanner?.textContent).not.toContain("arn:aws"); expect(errorBanner?.textContent).not.toContain("123456789012"); - expect(regionInput.value).toBe("us-west-2"); - expect(namespaceInput.value).toBe("manual-prod"); - - await act(async () => { - root.unmount(); - }); + expect(byId("provider-vault-aws-region")?.value).toBe("us-west-2"); + expect(byId("provider-vault-namespace")?.value).toBe("manual-prod"); }); it("keeps generic AWS discovery 403 errors on the generic failure path", async () => { @@ -1219,19 +1926,11 @@ describe("Secrets page layout", () => { }, }), ); - const root = await renderSecrets(container); + await renderSecrets(container); await openAwsVaultDialog(); - const regionInput = document.getElementById("provider-vault-aws-region") as HTMLInputElement; - await act(async () => { - setInputValue(regionInput, "us-west-1"); - }); - await flushReact(); - - await act(async () => { - (document.querySelector('[data-testid="aws-vault-discovery-button"]') as HTMLButtonElement | null)?.click(); - }); - await flushReact(); + await typeInto("provider-vault-aws-region", "us-west-1"); + await click(document.querySelector('[data-testid="aws-vault-discovery-button"]')); await flushReact(); const errorBanner = document.querySelector('[data-testid="aws-vault-discovery-error"]'); @@ -1240,86 +1939,50 @@ describe("Secrets page layout", () => { expect(errorBanner?.textContent).toContain("AWS discovery request failed before IAM evaluation."); expect(errorBanner?.textContent).toContain("proxy_forbidden"); expect(errorBanner?.textContent).not.toContain("AWS discovery needs ListSecrets permission"); - - await act(async () => { - root.unmount(); - }); }); - it("auto-generates the key from the name and keeps it read-only until Edit", async () => { - const root = await renderSecrets(container); + it("derives the key from the name under More options until the key is edited", async () => { + await renderSecrets(container); + await openNewSecret(); - const newSecretButton = Array.from(container.querySelectorAll("button")).find( - (button) => button.textContent?.includes("New secret"), - ) as HTMLButtonElement | undefined; - await act(async () => { - newSecretButton?.click(); - }); - await flushReact(); + const nameInput = byId("new-secret-name")!; + const valueTextarea = byId("new-secret-value")!; - const nameInput = document.getElementById("new-secret-name") as HTMLInputElement; - const keyInput = document.getElementById("new-secret-key") as HTMLInputElement; - const valueTextarea = document.getElementById("new-secret-value") as HTMLTextAreaElement; - - // Path-style placeholder and value directly after name for natural tab order. - expect(nameInput.placeholder).toBe("/dev/foo/bar"); + // The name reads like the environment variable people arrive with, and the value follows it. + expect(nameInput.placeholder).toBe("GITHUB_TOKEN"); expect( nameInput.compareDocumentPosition(valueTextarea) & Node.DOCUMENT_POSITION_FOLLOWING, ).toBeTruthy(); + // The key is an advanced detail: out of the way until More options is opened. + expect(byId("new-secret-key")).toBeNull(); + expect(findControl("Edit", openDialog())).toBeUndefined(); + + await typeInto("new-secret-name", "OpenAI API Key"); + await openMoreOptions(); + + const keyInput = byId("new-secret-key")!; + expect(keyInput.readOnly).toBe(false); + expect(keyInput.value).toBe("openai-api-key"); expect( valueTextarea.compareDocumentPosition(keyInput) & Node.DOCUMENT_POSITION_FOLLOWING, ).toBeTruthy(); + expect(byId("new-secret-description")).not.toBeNull(); - expect(keyInput.readOnly).toBe(true); - await act(async () => { - setInputValue(nameInput, "OpenAI API Key"); - }); - await flushReact(); - expect(keyInput.value).toBe("openai-api-key"); - - const editKeyButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Edit", - ) as HTMLButtonElement | undefined; - expect(editKeyButton).toBeDefined(); - await act(async () => { - editKeyButton?.click(); - }); - await flushReact(); - - expect((document.getElementById("new-secret-key") as HTMLInputElement).readOnly).toBe(false); - - await act(async () => { - setInputValue(document.getElementById("new-secret-key") as HTMLInputElement, "custom-key"); - setInputValue(nameInput, "OpenAI API Key v2"); - }); - await flushReact(); + await typeInto("new-secret-key", "custom-key"); + await typeInto("new-secret-name", "OpenAI API Key v2"); // Once edited, the key stops following the name. - expect((document.getElementById("new-secret-key") as HTMLInputElement).value).toBe("custom-key"); - - await act(async () => { - root.unmount(); - }); + expect(byId("new-secret-key")?.value).toBe("custom-key"); }); it("grants and revokes agent access from the secret detail sheet", async () => { mockSecretsApi.list.mockResolvedValue([makeCompanySecret()]); - mockSecretsApi.usage.mockResolvedValue({ secretId: "secret-openai", bindings: [] }); - mockSecretsApi.accessEvents.mockResolvedValue([]); - const coder = { - id: "agent-coder", - name: "CodexCoder", - status: "active", - adapterConfig: {}, - }; - const reviewer = { + const coder = makeAgent({ id: "agent-coder", name: "CodexCoder" }); + const reviewer = makeAgent({ id: "agent-reviewer", name: "Reviewer", - status: "active", - adapterConfig: { - env: { OPENAI_API_KEY: { type: "secret_ref", secretId: "secret-openai" } }, - }, - }; + adapterConfig: { env: { OPENAI_API_KEY: secretRef("secret-openai") } }, + }); mockAgentsApi.list.mockResolvedValue([coder, reviewer]); mockAgentsApi.get.mockImplementation(async (id: string) => id === "agent-coder" ? coder : reviewer, @@ -1327,67 +1990,43 @@ describe("Secrets page layout", () => { mockAgentsApi.update.mockImplementation(async (id: string) => id === "agent-coder" ? coder : reviewer, ); + await renderSecrets(container); - const root = await renderSecrets(container); + await click(teamRow("OPENAI_API_KEY")); + await waitForReact(() => detailsPanel()?.textContent?.includes("Reviewer") ?? false); - const companyRow = Array.from(container.querySelectorAll("[role='row']")).find( - (row) => row.textContent?.includes("OPENAI_API_KEY"), - ) as HTMLElement | undefined; - await act(async () => { - companyRow?.click(); - }); - await waitForReact(() => document.body.textContent?.includes("Reviewer") ?? false); + // Existing access is listed with the name the agent reads it under. + const access = detailsPanel()!.querySelector('[aria-label="Agents that can use it"]') as HTMLElement; + expect(access.textContent).toContain("Reviewer"); + expect(access.textContent).toContain("OPENAI_API_KEY"); + expect(byId("agent-access-agent")).toBeNull(); - // Existing access is listed right in the Details tab. - expect(document.body.textContent).toContain("Agent access"); - expect(document.body.textContent).toContain("Reviewer"); + await click(findControl("Add agent", access, true)); - const agentSelect = document.getElementById("agent-access-agent") as HTMLButtonElement; - const envKeyInput = document.getElementById("agent-access-env-key") as HTMLInputElement; - expect(envKeyInput.value).toBe("OPENAI_API_KEY"); + expect(byId("agent-access-env-key")?.value).toBe("OPENAI_API_KEY"); + const give = findControl("Give access") as HTMLButtonElement; + expect(give.disabled).toBe(true); - await act(async () => { - agentSelect.click(); - }); - await flushReact(); + await click(byId("agent-access-agent")); // Agents that already have access are not offered again. - expect(document.body.textContent).toContain("CodexCoder"); expect(document.body.querySelector('[aria-label="Select Reviewer"]')).toBeNull(); - - await act(async () => { - (document.body.querySelector('[aria-label="Select CodexCoder"]') as HTMLButtonElement | null)?.click(); - }); - await flushReact(); - - const addButton = Array.from(document.body.querySelectorAll("button")).find( - (button) => button.textContent?.trim() === "Add", - ) as HTMLButtonElement | undefined; - await act(async () => { - addButton?.click(); - }); - await flushReact(); + await click(document.body.querySelector('[aria-label="Select CodexCoder"]')); + await click(findControl("Give access")); await flushReact(); expect(mockAgentsApi.update).toHaveBeenCalledWith( "agent-coder", { adapterConfig: { - env: { OPENAI_API_KEY: { type: "secret_ref", secretId: "secret-openai" } }, + env: { OPENAI_API_KEY: secretRef("secret-openai") }, }, replaceAdapterConfig: true, }, "company-1", ); - const revokeButton = document.body.querySelector( - 'button[aria-label="Remove access for Reviewer"]', - ) as HTMLButtonElement | null; - expect(revokeButton).not.toBeNull(); - await act(async () => { - revokeButton?.click(); - }); - await flushReact(); + await click(document.body.querySelector('button[aria-label="Remove access for Reviewer"]')); await flushReact(); expect(mockAgentsApi.update).toHaveBeenCalledWith( @@ -1395,36 +2034,32 @@ describe("Secrets page layout", () => { { adapterConfig: { env: {} }, replaceAdapterConfig: true }, "company-1", ); - - await act(async () => { - root.unmount(); - }); }); - it("shows an empty AWS discovery result without blocking manual entry", async () => { + it("names the environment variable after the secret, not its folder", async () => { + mockSecretsApi.list.mockResolvedValue([makeCompanySecret({ id: "s-db", key: "staging-database-url", name: "staging/DATABASE_URL" })]); + mockAgentsApi.list.mockResolvedValue([makeAgent({ id: "agent-coder", name: "CodexCoder" })]); + await renderSecrets(container, { path: "/company/settings/secrets?secret=s-db" }); + await waitForReact(() => findControl("Add agent", detailsPanel(), true) != null); + + await click(findControl("Add agent", detailsPanel(), true)); + + expect(byId("agent-access-env-key")?.value).toBe("DATABASE_URL"); + }); + + it("says AWS was reached when there is nothing to suggest, without blocking manual entry", async () => { mockSecretsApi.providerConfigDiscoveryPreview.mockResolvedValueOnce( makeDiscoveryPreview({ candidates: [], sampledSecretCount: 0 }), ); - const root = await renderSecrets(container); + await renderSecrets(container); await openAwsVaultDialog(); - const regionInput = document.getElementById("provider-vault-aws-region") as HTMLInputElement; - await act(async () => { - setInputValue(regionInput, "us-east-2"); - }); - await flushReact(); - await act(async () => { - (document.querySelector('[data-testid="aws-vault-discovery-button"]') as HTMLButtonElement | null)?.click(); - }); - await flushReact(); + await typeInto("provider-vault-aws-region", "us-east-2"); + await click(document.querySelector('[data-testid="aws-vault-discovery-button"]')); await flushReact(); - expect(document.body.textContent).toContain("No AWS vault metadata candidates found"); - expect(regionInput.value).toBe("us-east-2"); - - await act(async () => { - root.unmount(); - }); + expect(bodyText()).toContain("Paperclip reached AWS. There was nothing to suggest"); + expect(byId("provider-vault-aws-region")?.value).toBe("us-east-2"); }); }); @@ -1432,19 +2067,13 @@ describe("Secrets folder view (PAP-14698)", () => { let container: HTMLDivElement; function seedFolderSecrets() { + seedDefaults(); mockSecretsApi.list.mockResolvedValue([ makeCompanySecret({ id: "s1", key: "dev_github_oauth_clientid", name: "dev/github/oauth/clientid" }), makeCompanySecret({ id: "s2", key: "dev_github_oauth_clientsecret", name: "dev/github/oauth/clientsecret" }), makeCompanySecret({ id: "s3", key: "prod_api_token", name: "prod/api/token" }), makeCompanySecret({ id: "s4", key: "standalone", name: "standalone" }), ]); - mockSecretsApi.providers.mockResolvedValue(providers); - mockSecretsApi.providerHealth.mockResolvedValue({ providers: [] }); - mockSecretsApi.providerConfigs.mockResolvedValue(providerConfigs); - mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([]); - mockSecretsApi.userSecretDefinitionCoverage.mockResolvedValue(userSecretCoverage); - mockSecretsApi.listMyUserSecrets.mockResolvedValue([]); - mockAgentsApi.list.mockResolvedValue([]); } function renderAt(path: string) { @@ -1466,13 +2095,13 @@ describe("Secrets folder view (PAP-14698)", () => { unmountActiveRoots(); container.remove(); document.body.innerHTML = ""; - vi.clearAllMocks(); + vi.resetAllMocks(); }); it("derives folders at the root with filtered counts and a flat standalone secret", async () => { - const root = await renderAt("/"); + await renderAt("/"); - const table = container.querySelector('[data-testid="secrets-table-view"]')!; + const table = teamTable()!; expect(table.textContent).toContain("dev"); expect(table.textContent).toContain("prod"); expect(table.textContent).toContain("standalone"); @@ -1482,200 +2111,132 @@ describe("Secrets folder view (PAP-14698)", () => { // Folder rows are real links carrying ?path=. const links = [...container.querySelectorAll("a")].map((a) => a.getAttribute("href") ?? ""); expect(links.some((href) => href.includes("path=dev"))).toBe(true); - - await act(async () => root.unmount()); + // The root needs no breadcrumb; it appears once a folder is open. + expect(container.querySelector('nav[aria-label="Breadcrumb"]')).toBeNull(); }); it("opens a deep ?path= link into the folder with breadcrumb, leaves, and an up affordance", async () => { - const root = await renderAt("/?path=dev/github/oauth"); + await renderAt("/?path=dev/github/oauth"); const breadcrumb = container.querySelector('nav[aria-label="Breadcrumb"]'); expect(breadcrumb).not.toBeNull(); const current = container.querySelector('[aria-current="page"]'); expect(current?.textContent).toContain("oauth"); - const table = container.querySelector('[data-testid="secrets-table-view"]')!; + const table = teamTable()!; expect(table.textContent).toContain("clientid"); expect(table.textContent).toContain("clientsecret"); expect(table.textContent).toContain("Up to github"); // Sibling trees are not shown while drilled in. expect(table.textContent).not.toContain("standalone"); - - await act(async () => root.unmount()); }); it("renders the empty-folder state (breadcrumb intact) for an unknown path", async () => { - const root = await renderAt("/?path=does/not/exist"); + await renderAt("/?path=does/not/exist"); expect(container.querySelector('nav[aria-label="Breadcrumb"]')).not.toBeNull(); expect(container.textContent).toContain("No secrets in this folder yet."); - const cta = [...container.querySelectorAll("button")].find((b) => - b.textContent?.includes("New secret here"), - ) as HTMLButtonElement; - expect(cta).toBeDefined(); - await act(async () => cta.click()); - await flushReact(); + await click(findControl("New secret here", container, true)); - expect(document.body.textContent).toContain("does/not/exist/"); - expect((document.getElementById("new-secret-name") as HTMLInputElement).value).toBe(""); + // The folder shows as a removable chip beside an empty name field. + expect(openDialog()?.textContent).toContain("does/not/exist"); + expect(byId("new-secret-name")?.value).toBe(""); expect(document.querySelector('button[aria-label="Remove folder prefix"]')).not.toBeNull(); - - await act(async () => root.unmount()); }); it("distinguishes a filtered-empty folder from a genuinely empty folder", async () => { - const root = await renderAt("/?path=dev/github/oauth"); - const filterButton = document.querySelector('button[title="Filter"]') as HTMLButtonElement; - await act(async () => filterButton.click()); - await flushReact(); + await renderAt("/?path=dev/github/oauth"); - const archivedLabel = [...document.querySelectorAll("label")].find( - (label) => label.textContent?.trim() === "Archived", - ) as HTMLLabelElement; - await act(async () => archivedLabel.click()); + await chooseFilter("Archived"); await waitForReact(() => container.textContent?.includes("No secrets match your filters.") ?? false); expect(container.textContent).toContain("No secrets match your filters."); expect(container.textContent).not.toContain("New secret here"); - - await act(async () => root.unmount()); }); it("creates a company secret from a folder prefix and derives the key from the full name", async () => { mockSecretsApi.create.mockResolvedValue( makeCompanySecret({ id: "created", name: "dev/github/oauth/clientsecret/deeper" }), ); - const root = await renderAt("/?path=dev/github/oauth"); + await renderAt("/?path=dev/github/oauth"); + await openNewSecret(); - const newSecretButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.trim() === "New secret", - ) as HTMLButtonElement; - await act(async () => newSecretButton.click()); - await flushReact(); + expect(openDialog()?.textContent).toContain("dev/github/oauth"); + expect(byId("new-secret-name")?.value).toBe(""); + await typeInto("new-secret-name", "clientsecret/deeper"); + // Agents read the secret's own name, not its folder path. + expect(byId("new-secret-name-hint")?.textContent).toBe("Agents read it as DEEPER."); + await openMoreOptions(); - expect(document.body.textContent).toContain("dev/github/oauth/"); - const nameInput = document.getElementById("new-secret-name") as HTMLInputElement; - expect(nameInput.placeholder).toBe("clientsecret"); - expect(nameInput.value).toBe(""); - await act(async () => setInputValue(nameInput, "clientsecret/deeper")); - await flushReact(); - - expect((document.getElementById("new-secret-key") as HTMLInputElement).value).toBe( - "dev-github-oauth-clientsecret-deeper", - ); - await act(async () => - setTextareaValue(document.getElementById("new-secret-value") as HTMLTextAreaElement, "secret-value"), - ); - const createButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.trim() === "Create secret", - ) as HTMLButtonElement; - await act(async () => createButton.click()); - await flushReact(); + expect(byId("new-secret-key")?.value).toBe("dev-github-oauth-clientsecret-deeper"); + await typeInto("new-secret-value", "secret-value"); + await submitNewSecret(); expect(mockSecretsApi.create).toHaveBeenCalledWith( "company-1", - expect.objectContaining({ name: "dev/github/oauth/clientsecret/deeper" }), + expect.objectContaining({ + name: "dev/github/oauth/clientsecret/deeper", + key: "dev-github-oauth-clientsecret-deeper", + }), ); - - await act(async () => root.unmount()); }); it("keeps the folder prefix for Each user and exposes the full name when the chip is removed", async () => { - const root = await renderAt("/?path=dev/github/oauth"); - const newSecretButton = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.trim() === "New secret", - ) as HTMLButtonElement; - await act(async () => newSecretButton.click()); - await flushReact(); + await renderAt("/?path=dev/github/oauth"); + await openNewSecret(); + await chooseWhoProvides("Each user"); - const eachUserTab = [...document.querySelectorAll("button")].find( - (button) => button.textContent?.trim() === "Each user", - ) as HTMLButtonElement; - await act(async () => { - eachUserTab.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true })); - eachUserTab.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Enter" })); - eachUserTab.click(); - }); - await flushReact(); + await typeInto("new-secret-name", "personal-token"); + await openMoreOptions(); + expect(byId("new-secret-key")?.value).toBe("DEV_GITHUB_OAUTH_PERSONAL_TOKEN"); - const nameInput = document.getElementById("new-secret-name") as HTMLInputElement; - await act(async () => setInputValue(nameInput, "personal-token")); - await flushReact(); - expect((document.getElementById("new-secret-key") as HTMLInputElement).value).toBe( - "DEV_GITHUB_OAUTH_PERSONAL_TOKEN", - ); + await click(document.querySelector('button[aria-label="Remove folder prefix"]')); - const removePrefix = document.querySelector( - 'button[aria-label="Remove folder prefix"]', - ) as HTMLButtonElement; - await act(async () => removePrefix.click()); - await flushReact(); - expect((document.getElementById("new-secret-name") as HTMLInputElement).value).toBe( - "dev/github/oauth/personal-token", - ); + // Removing the chip keeps the path and makes it editable in the name field. + expect(byId("new-secret-name")?.value).toBe("dev/github/oauth/personal-token"); + expect(byId("new-secret-key")?.value).toBe("DEV_GITHUB_OAUTH_PERSONAL_TOKEN"); + expect(openDialog()?.textContent).not.toContain("dev/github/oauth"); expect(document.querySelector('button[aria-label="Remove folder prefix"]')).toBeNull(); - - await act(async () => root.unmount()); }); it("validates New folder inline and stages the trimmed segment in the URL-backed folder view", async () => { - const root = await renderAt("/?path=dev/github/oauth"); - const newFolderButton = [...container.querySelectorAll("button")].find( - (button) => button.textContent?.trim() === "New folder", - ) as HTMLButtonElement; - await act(async () => newFolderButton.click()); - await flushReact(); + await renderAt("/?path=dev/github/oauth"); + await click(findControl("New folder")); const folderInput = container.querySelector('input[aria-label="Folder name"]') as HTMLInputElement; await act(async () => setInputValue(folderInput, "bad/name")); - const createFolderButton = [...container.querySelectorAll("button")].find( - (button) => button.textContent?.trim() === "Create folder", - ) as HTMLButtonElement; - await act(async () => createFolderButton.click()); - await flushReact(); + const createFolderButton = findControl("Create folder", container); + await click(createFolderButton); expect(container.querySelector('[role="alert"]')?.textContent).toContain( "Folder name cannot contain slashes.", ); await act(async () => setInputValue(folderInput, " staged ")); await flushReact(); - await act(async () => createFolderButton.click()); + await click(createFolderButton); await waitForReact(() => [...container.querySelectorAll('[aria-current="page"]')].some((node) => node.textContent?.includes("staged"), ), ); - expect( - [...container.querySelectorAll('[aria-current="page"]')].some((node) => - node.textContent?.includes("staged"), - ), - ).toBe(true); expect(container.textContent).toContain("No secrets in this folder yet."); expect(container.querySelector('input[aria-label="Folder name"]')).toBeNull(); - - await act(async () => root.unmount()); }); - it("Flat toggle reproduces the raw, ungrouped list", async () => { - const root = await renderAt("/"); + it("always groups by folder, whatever view an earlier visit stored", async () => { + window.localStorage.setItem("paperclip.secrets.viewMode", "flat"); + await renderAt("/"); - const flatButton = [...container.querySelectorAll("button")].find( - (b) => b.textContent?.trim().toLowerCase() === "flat", - ) as HTMLButtonElement | undefined; - expect(flatButton).toBeDefined(); - await act(async () => flatButton!.click()); - await flushReact(); - - const table = container.querySelector('[data-testid="secrets-table-view"]')!; - expect(table.textContent).toContain("dev/github/oauth/clientid"); - expect(table.textContent).not.toContain("2 secrets · 1 folder"); - - await act(async () => root.unmount()); + const table = teamTable()!; + expect(table.textContent).toContain("2 secrets · 2 folders"); + expect(table.textContent).not.toContain("dev/github/oauth/clientid"); + expect(container.querySelector('[role="group"][aria-label="View mode"]')).toBeNull(); + expect(findControl("Flat", container)).toBeUndefined(); }); it("search is global across folders and shows full muted-path names", async () => { - const root = await renderAt("/?path=dev/github/oauth"); + await renderAt("/?path=dev/github/oauth"); const input = container.querySelector( 'input[aria-label="Search secrets"]', @@ -1683,32 +2244,19 @@ describe("Secrets folder view (PAP-14698)", () => { await act(async () => setInputValue(input, "token")); await flushReact(); - expect(container.textContent).toContain("Search results"); - expect(container.textContent).toContain("across all folders"); - const table = container.querySelector('[data-testid="secrets-table-view"]')!; + expect(container.textContent).toContain("1 match across all folders"); + expect(container.querySelector('nav[aria-label="Breadcrumb"]')).toBeNull(); + const table = teamTable()!; // prod/api/token lives outside the current folder yet still matches. expect(table.textContent).toContain("prod/api/"); expect(table.textContent).toContain("token"); - - await act(async () => root.unmount()); + expect(table.textContent).not.toContain("clientid"); }); }); describe("Secrets operator-hidden tabs", () => { let container: HTMLDivElement; - function seedSecrets() { - mockSecretsApi.list.mockResolvedValue([makeCompanySecret({ id: "s1", key: "api_key", name: "api_key" })]); - mockSecretsApi.providers.mockResolvedValue(providers); - mockSecretsApi.providerHealth.mockResolvedValue({ providers: [] }); - mockSecretsApi.providerConfigs.mockResolvedValue(providerConfigs); - mockSecretsApi.listUserSecretDefinitions.mockResolvedValue([]); - mockSecretsApi.userSecretDefinitionCoverage.mockResolvedValue(userSecretCoverage); - mockSecretsApi.listMyUserSecrets.mockResolvedValue([]); - mockSecretsApi.listProposals.mockResolvedValue([]); - mockAgentsApi.list.mockResolvedValue([]); - } - function renderWithHiddenSettings(hiddenSettings: string[]) { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); queryClient.setQueryData(queryKeys.health, { hiddenSettings } as never); @@ -1718,40 +2266,44 @@ describe("Secrets operator-hidden tabs", () => { beforeEach(() => { container = document.createElement("div"); document.body.appendChild(container); - seedSecrets(); + seedDefaults(); + mockSecretsApi.list.mockResolvedValue([makeCompanySecret({ id: "s1", key: "api_key", name: "api_key" })]); }); afterEach(() => { unmountActiveRoots(); container.remove(); document.body.innerHTML = ""; - vi.clearAllMocks(); + vi.resetAllMocks(); }); - it("hides the Provider vaults and Proposals tabs and skips the proposals poll", async () => { - const root = await renderWithHiddenSettings([ + it("hides the Vaults and Proposals tabs and skips the proposals poll", async () => { + await renderWithHiddenSettings([ "company.secrets.vaults", "company.secrets.proposals", ]); const tabLabels = [...container.querySelectorAll('[role="tab"]')].map((tab) => tab.textContent); - expect(tabLabels.join(" ")).toContain("Secrets"); - expect(tabLabels.join(" ")).toContain("My secrets"); - expect(tabLabels.join(" ")).not.toContain("Provider vaults"); - expect(tabLabels.join(" ")).not.toContain("Proposals"); + expect(tabLabels).toEqual(["Secrets", "My secrets"]); expect(mockSecretsApi.listProposals).not.toHaveBeenCalled(); - - await act(async () => root.unmount()); }); it("keeps both tabs when nothing is hidden", async () => { - const root = await renderWithHiddenSettings([]); + await renderWithHiddenSettings([]); const tabLabels = [...container.querySelectorAll('[role="tab"]')].map((tab) => tab.textContent); - expect(tabLabels.join(" ")).toContain("Provider vaults"); - expect(tabLabels.join(" ")).toContain("Proposals"); + expect(tabLabels).toEqual(["Secrets", "My secrets", "Vaults", "Proposals"]); expect(mockSecretsApi.listProposals).toHaveBeenCalled(); + }); - await act(async () => root.unmount()); + it("does not offer a route to Vaults from a secret's details when the tab is hidden", async () => { + await renderWithHiddenSettings(["company.secrets.vaults"]); + + await click(teamRow("api_key")); + await waitForReact(() => detailsPanel() != null); + + const stored = [...detailsPanel()!.querySelectorAll("dt")].find((term) => term.textContent === "Stored in"); + expect(stored?.nextElementSibling?.textContent).toBe("Paperclip"); + expect(stored?.nextElementSibling?.querySelector("button")).toBeNull(); }); }); diff --git a/ui/src/pages/Secrets.test.ts b/ui/src/pages/Secrets.test.ts index 771babbca51..3febb2b8873 100644 --- a/ui/src/pages/Secrets.test.ts +++ b/ui/src/pages/Secrets.test.ts @@ -3,11 +3,13 @@ import { describe, expect, it } from "vitest"; import type { CompanySecretProviderConfig, SecretProviderDescriptor } from "@paperclipai/shared"; import { + buildCreateVaultOptions, findCreateProviderReplacement, getAwsManagedPathPreview, getCreateProviderBlockReason, getDefaultProviderConfigId, getProviderConfigBlockReason, + parseEnvAssignment, } from "./Secrets"; import type { SecretProviderHealthResponse } from "../api/secrets"; @@ -85,7 +87,7 @@ describe("Secrets page provider helpers", () => { null, ), ).toBe( - "Deployment default AWS Secrets Manager is not configured. Select a ready provider vault or configure the deployment default.", + "AWS Secrets Manager is not set up on this Paperclip server. Choose a vault that is ready, or ask your administrator to set it up.", ); }); @@ -108,7 +110,7 @@ describe("Secrets page provider helpers", () => { health, ), ).toBe( - "Deployment default AWS Secrets Manager is not configured. Select a ready provider vault or configure the deployment default. AWS Secrets Manager provider is not ready: missing PAPERCLIP_SECRETS_AWS_DEPLOYMENT_ID.", + "AWS Secrets Manager is not set up on this Paperclip server. Choose a vault that is ready, or ask your administrator to set it up. AWS Secrets Manager provider is not ready: missing PAPERCLIP_SECRETS_AWS_DEPLOYMENT_ID.", ); }); @@ -141,7 +143,7 @@ describe("Secrets page provider helpers", () => { status: "disabled", }), ), - ).toBe("This provider vault is disabled."); + ).toBe("This vault is disabled."); }); it("blocks provider modes the backend does not support", () => { @@ -203,6 +205,110 @@ describe("Secrets page provider helpers", () => { provider: "vault", status: "coming_soon", } as never), - ).toBe("This provider vault is saved as draft metadata only."); + ).toBe("Paperclip can't store or read secrets in this vault yet."); + }); + + it("falls back to a plain sentence when an unhealthy vault reports no message", () => { + expect( + getProviderConfigBlockReason( + providerConfig({ id: "aws-down", provider: "aws_secrets_manager", healthStatus: "error" }), + ), + ).toBe("Paperclip couldn't reach this vault."); + }); +}); + +describe("buildCreateVaultOptions", () => { + const build = ( + overrides: Partial[0]> = {}, + ) => + buildCreateVaultOptions({ + providers: [localProvider, awsProvider], + providerConfigs: [], + mode: "managed", + health: null, + ...overrides, + }); + + it("offers only Paperclip's own storage when nothing else is set up", () => { + expect(build({ providers: [localProvider, { ...awsProvider, configured: false }] })).toEqual([ + { + value: "local_encrypted:", + provider: "local_encrypted", + providerConfigId: "", + label: "Paperclip (default)", + blockReason: null, + }, + ]); + }); + + it("offers a provider's connected vaults in place of its built-in storage", () => { + const options = build({ + providerConfigs: [ + providerConfig({ id: "team", provider: "local_encrypted", displayName: "Team vault" }), + providerConfig({ id: "aws-prod", provider: "aws_secrets_manager", displayName: "prod-us-east", isDefault: true }), + ], + }); + + expect(options.map((option) => option.label)).toEqual([ + "Team vault · Paperclip-encrypted (default)", + "prod-us-east · AWS Secrets Manager", + ]); + expect(options.map((option) => option.value)).toEqual([ + "local_encrypted:team", + "aws_secrets_manager:aws-prod", + ]); + expect(options.every((option) => option.blockReason === null)).toBe(true); + }); + + it("keeps an unhealthy vault in the list with its reason and drops disabled and coming-soon ones", () => { + const options = build({ + providers: [localProvider, { ...awsProvider, configured: false }], + providerConfigs: [ + providerConfig({ + id: "aws-down", + provider: "aws_secrets_manager", + displayName: "staging-eu", + healthStatus: "error", + healthMessage: "AWS denied access.", + }), + providerConfig({ id: "aws-off", provider: "aws_secrets_manager", status: "disabled" }), + providerConfig({ id: "aws-draft", provider: "aws_secrets_manager", status: "coming_soon" }), + ], + }); + + expect(options.map((option) => option.value)).toEqual(["local_encrypted:", "aws_secrets_manager:aws-down"]); + expect(options[1].blockReason).toBe("AWS denied access."); + }); + + it("marks Paperclip's own storage as blocked for linking instead of hiding it", () => { + const options = build({ mode: "external" }); + + expect(options[0]).toMatchObject({ + value: "local_encrypted:", + blockReason: "Local encrypted (default) does not support linked external references.", + }); + expect(options[1]).toMatchObject({ value: "aws_secrets_manager:", blockReason: null }); + }); +}); + +describe("parseEnvAssignment", () => { + it("splits NAME=value, with or without export and quotes", () => { + expect(parseEnvAssignment("GITHUB_TOKEN=ghp_abc")).toEqual({ name: "GITHUB_TOKEN", value: "ghp_abc" }); + expect(parseEnvAssignment(' export LINEAR_API_KEY="lin_api_1" ')).toEqual({ + name: "LINEAR_API_KEY", + value: "lin_api_1", + }); + expect(parseEnvAssignment("DATABASE_URL='postgres://u:p@host/db?sslmode=require'")).toEqual({ + name: "DATABASE_URL", + value: "postgres://u:p@host/db?sslmode=require", + }); + }); + + it("leaves anything that is not a single assignment alone", () => { + expect(parseEnvAssignment("ghp_abc")).toBeNull(); + expect(parseEnvAssignment("A=1\nB=2")).toBeNull(); + expect(parseEnvAssignment("1BAD=value")).toBeNull(); + expect(parseEnvAssignment("EMPTY=")).toBeNull(); + expect(parseEnvAssignment('EMPTY=""')).toBeNull(); }); }); diff --git a/ui/src/pages/Secrets.tsx b/ui/src/pages/Secrets.tsx index c25a66dc08e..ba88cc60ecd 100644 --- a/ui/src/pages/Secrets.tsx +++ b/ui/src/pages/Secrets.tsx @@ -6,6 +6,7 @@ import { ArchiveRestore, Archive, Ban, + Building2, CheckCircle2, ChevronLeft, ChevronRight, @@ -14,9 +15,12 @@ import { Copy, Database, Edit3, + Eye, + EyeOff, ExternalLink, Folder, FolderOpen, + FolderPlus, KeyRound, Link2, Lock, @@ -37,6 +41,7 @@ import { } from "lucide-react"; import { Link, useSearchParams } from "react-router-dom"; import type { + Agent, CompanySecret, CompanySecretUsageBinding, CompanySecretProviderConfig, @@ -111,7 +116,25 @@ import { cn } from "../lib/utils"; import { copyTextToClipboard } from "../lib/clipboard"; import { useCopyAction } from "../lib/use-copy-action"; import { PageTabBar } from "../components/PageTabBar"; -import { AgentSelect } from "../components/AgentMultiSelect"; +import { AgentMultiSelect, AgentSelect } from "../components/AgentMultiSelect"; +import { AgentAvatar } from "../components/AgentAvatar"; +import { RadioCardGroup } from "@/components/ui/radio-card"; +import { + ENV_KEY_PATTERN, + apiAliasesReferencingSecret, + buildSecretAgentUsage, + envKeysReferencingSecret, + grantAgentSecretAccess, + revokeAgentSecretAccess, + type AgentAccessReference, +} from "./secrets/agent-secret-access"; +import { + SECRET_ORIGIN_GROUPS, + classifySecretOrigin, + managedSecretLabel, + type SecretOrigin, +} from "./secrets/secret-origin"; +import { HowSecretsWork, ManagedSecretGroup, SecretProvidedBy, SecretUsedBy } from "./secrets/secret-list-parts"; import { ImportFromVaultDialog } from "./secrets/ImportFromVaultDialog"; import { MyUserSecretsTab } from "./secrets/MyUserSecretsTab"; import { ProposalsTab } from "./secrets/ProposalsTab"; @@ -121,6 +144,7 @@ import { buildSecretPathListing, getSecretPathRowName, normalizeSecretPath, + splitSecretPath, validateSecretFolderSegment, type SecretPathFolder, } from "./secrets/secret-path"; @@ -139,17 +163,12 @@ type RotateMode = "value" | "reference"; type SecretValueProvider = "company" | "user"; type ProvidedByFilter = "all" | SecretValueProvider; type SecretsTab = "secrets" | "my-secrets" | "vaults" | "proposals"; -type SecretsViewMode = "folders" | "flat"; +/** "current" is everything still in play: active and disabled, not archived. */ +type SecretStatusFilter = "current" | "disabled" | "archived"; -const SECRETS_VIEW_MODE_STORAGE_KEY = "paperclip.secrets.viewMode"; - -function readStoredViewMode(): SecretsViewMode | null { - try { - const stored = window.localStorage.getItem(SECRETS_VIEW_MODE_STORAGE_KEY); - return stored === "folders" || stored === "flat" ? stored : null; - } catch { - return null; - } +/** The last segment of a slash-delimited secret name: what the secret is called inside its folder. */ +function secretLeafName(name: string): string { + return splitSecretPath(name).at(-1) ?? name; } /** "12 secrets · 3 folders" — folder part omitted when there are no subfolders. */ @@ -198,6 +217,7 @@ type SafeProviderErrorDetails = { }; const EMPTY_SECRETS: CompanySecret[] = []; +const EMPTY_AGENTS: Agent[] = []; const EMPTY_USER_SECRET_DEFINITIONS: UserSecretDefinition[] = []; const EMPTY_MY_USER_SECRETS: MyUserSecretEntry[] = []; const EMPTY_SECRET_PROVIDERS: SecretProviderDescriptor[] = []; @@ -325,6 +345,11 @@ function providerLabel(providers: SecretProviderDescriptor[] | undefined, id: Se return providers?.find((p) => p.id === id)?.label ?? id.replaceAll("_", " "); } +/** The provider as a person would name it: Paperclip's own storage is not "local encrypted". */ +function vaultKindLabel(providers: SecretProviderDescriptor[] | undefined, id: SecretProvider) { + return id === "local_encrypted" ? "Paperclip-encrypted" : providerLabel(providers, id); +} + function normalizeSecretKeyForPreview(input: string) { return input .trim() @@ -487,8 +512,8 @@ export function getCreateProviderBlockReason( if (selectedProviderConfigReady) return null; if (selectedProviderConfigBlockReason) return selectedProviderConfigBlockReason; const healthEntry = healthEntryForProvider(health, provider.id); - const deploymentMessage = `Deployment default ${provider.label} is not configured.`; - const nextStep = " Select a ready provider vault or configure the deployment default."; + const deploymentMessage = `${provider.label} is not set up on this Paperclip server.`; + const nextStep = " Choose a vault that is ready, or ask your administrator to set it up."; return healthEntry?.message ? `${deploymentMessage}${nextStep} ${healthEntry.message}` : `${deploymentMessage}${nextStep}`; @@ -500,25 +525,6 @@ export function getCreateProviderBlockReason( return null; } -function providerHealthText( - provider: SecretProviderDescriptor | null | undefined, - health: SecretProviderHealthResponse | null, - providerConfig?: CompanySecretProviderConfig | null, -) { - if (!provider) return null; - if ( - provider.configured === false && - providerConfig?.provider === provider.id && - !getProviderConfigBlockReason(providerConfig) - ) { - return `Using selected provider vault. Deployment default ${provider.label} is not configured.`; - } - const entry = healthEntryForProvider(health, provider.id); - if (!entry) return null; - const warnings = entry.warnings?.join(" "); - return [entry.message, warnings].filter(Boolean).join(" "); -} - function detailString(details: Record | undefined, key: string) { const value = details?.[key]; return typeof value === "string" && value.trim() ? value.trim() : null; @@ -528,10 +534,10 @@ export function getProviderConfigBlockReason( config: CompanySecretProviderConfig | null | undefined, ) { if (!config) return null; - if (config.status === "disabled") return "This provider vault is disabled."; - if (config.status === "coming_soon") return "This provider vault is saved as draft metadata only."; + if (config.status === "disabled") return "This vault is disabled."; + if (config.status === "coming_soon") return "Paperclip can't store or read secrets in this vault yet."; if (config.healthStatus === "error") { - return config.healthMessage ?? "This provider vault health check failed."; + return config.healthMessage ?? "Paperclip couldn't reach this vault."; } return null; } @@ -587,8 +593,76 @@ export function findCreateProviderReplacement({ ); } +export interface CreateVaultOption { + value: string; + provider: SecretProvider; + providerConfigId: string; + label: string; + blockReason: string | null; +} + +export function createVaultOptionValue(provider: SecretProvider, providerConfigId: string) { + return `${provider}:${providerConfigId}`; +} + +/** + * Every place a new secret can be stored, as one list: each provider's + * built-in storage plus each vault the organization connected. The dialog + * shows a picker only when this has more than one entry. + */ +export function buildCreateVaultOptions(input: { + providers: SecretProviderDescriptor[]; + providerConfigs: CompanySecretProviderConfig[]; + mode: CreateMode; + health: SecretProviderHealthResponse | null; +}): CreateVaultOption[] { + const options: CreateVaultOption[] = []; + const defaultLocalConfigId = getSelectableProviderConfig(input.providerConfigs, "local_encrypted")?.id ?? ""; + for (const provider of input.providers) { + const builtInBlockReason = getCreateProviderBlockReason(provider, input.mode, input.health, null); + // Once a provider has a usable vault, new secrets for it always go to a + // vault, so its built-in storage is not a choice the form can honor. + const hasUsableVault = Boolean(getSelectableProviderConfig(input.providerConfigs, provider.id)); + if (!hasUsableVault && (provider.id === "local_encrypted" || !builtInBlockReason)) { + options.push({ + value: createVaultOptionValue(provider.id, ""), + provider: provider.id, + providerConfigId: "", + label: + provider.id === "local_encrypted" + ? `Paperclip${defaultLocalConfigId ? "" : " (default)"}` + : provider.label, + blockReason: builtInBlockReason, + }); + } + for (const config of input.providerConfigs) { + if (config.provider !== provider.id || config.status === "disabled" || config.status === "coming_soon") continue; + options.push({ + value: createVaultOptionValue(provider.id, config.id), + provider: provider.id, + providerConfigId: config.id, + label: `${config.displayName} · ${vaultKindLabel(input.providers, provider.id)}${config.id === defaultLocalConfigId ? " (default)" : ""}`, + blockReason: + getProviderConfigBlockReason(config) ?? + getCreateProviderBlockReason(provider, input.mode, input.health, config), + }); + } + } + return options; +} + +/** Splits a pasted `NAME=value` (optionally `export NAME="value"`) into its two halves. */ +export function parseEnvAssignment(text: string): { name: string; value: string } | null { + const lines = text.trim().split("\n"); + if (lines.length !== 1) return null; + const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.+)$/.exec(lines[0].trim()); + if (!match) return null; + const value = match[2].trim().replace(/^(["'])(.*)\1$/, "$2"); + return value ? { name: match[1], value } : null; +} + function providerVaultLabel(configs: CompanySecretProviderConfig[], id: string | null | undefined) { - if (!id) return "Deployment default"; + if (!id) return "Paperclip's default"; return configs.find((config) => config.id === id)?.displayName ?? "Unknown vault"; } @@ -664,9 +738,10 @@ export function Secrets() { setActiveTab("secrets"); } }, [activeTab, hideVaultsTab, hideProposalsTab]); - const [secretDetailTab, setSecretDetailTab] = useState("details"); const [search, setSearch] = useState(""); - const [statusFilter, setStatusFilter] = useState("active"); + const [statusFilter, setStatusFilter] = useState("current"); + const [managedOpen, setManagedOpen] = useState(false); + const [openManagedGroups, setOpenManagedGroups] = useState>(() => new Set()); const [providerFilter, setProviderFilter] = useState("all"); const [providedByFilter, setProvidedByFilter] = useState("all"); const [searchParams, setSearchParams] = useSearchParams(); @@ -688,7 +763,6 @@ export function Secrets() { }, [setSearchParams], ); - const [usageDialogSecretId, setUsageDialogSecretId] = useState(null); const [createOpen, setCreateOpen] = useState(false); const [importOpen, setImportOpen] = useState(false); const [importInitialVaultId, setImportInitialVaultId] = useState(null); @@ -697,7 +771,10 @@ export function Secrets() { const [editingDefinition, setEditingDefinition] = useState(null); const [createNamePrefix, setCreateNamePrefix] = useState(null); const [createKeyDirty, setCreateKeyDirty] = useState(false); - const [createKeyEditable, setCreateKeyEditable] = useState(false); + const [createAgentIds, setCreateAgentIds] = useState>(() => new Set()); + const [createValueVisible, setCreateValueVisible] = useState(false); + const [createMoreOpen, setCreateMoreOpen] = useState(false); + const [createAttempted, setCreateAttempted] = useState(false); const [createForm, setCreateForm] = useState({ name: "", key: "", @@ -723,6 +800,8 @@ export function Secrets() { const [removeVaultConfirm, setRemoveVaultConfirm] = useState(null); const [vaultForm, setVaultForm] = useState(() => emptyProviderVaultForm()); const [vaultError, setVaultError] = useState(null); + const [vaultAttempted, setVaultAttempted] = useState(false); + const [vaultAdvancedOpen, setVaultAdvancedOpen] = useState(false); const [vaultDiscovery, setVaultDiscovery] = useState(null); const [vaultDiscoveryError, setVaultDiscoveryError] = useState(null); @@ -792,6 +871,16 @@ export function Secrets() { enabled: Boolean(selectedCompanyId) && !hideProposalsTab, }); + // One agents read answers "who uses this?" for every row and for the + // details panel; the per-secret usage endpoint is only hit when one is open. + const agentsQuery = useQuery({ + queryKey: selectedCompanyId ? queryKeys.agents.list(selectedCompanyId) : ["agents", "__disabled__"], + queryFn: () => agentsApi.list(selectedCompanyId!), + enabled: Boolean(selectedCompanyId), + staleTime: 30_000, + }); + const agentUsage = useMemo(() => buildSecretAgentUsage(agentsQuery.data ?? EMPTY_AGENTS), [agentsQuery.data]); + const secretsView = useQueryView(secretsQuery); const userDefinitionsView = useQueryView(userDefinitionsQuery); const providerConfigsView = useQueryView(providerConfigsQuery); @@ -830,18 +919,10 @@ export function Secrets() { secret: null, }; }, [myUserSecrets, selectedDefinition]); - const usageDialogSecret = useMemo( - () => secrets.find((secret) => secret.id === usageDialogSecretId) ?? null, - [secrets, usageDialogSecretId], - ); const selectedCreateProvider = useMemo( () => providers.find((provider) => provider.id === createForm.provider) ?? null, [providers, createForm.provider], ); - const createProviderConfigs = useMemo( - () => providerConfigs.filter((config) => config.provider === createForm.provider), - [createForm.provider, providerConfigs], - ); const selectedCreateProviderConfig = useMemo( () => providerConfigs.find((config) => config.id === createForm.providerConfigId) ?? null, [createForm.providerConfigId, providerConfigs], @@ -861,11 +942,6 @@ export function Secrets() { selectedCreateProviderConfig, ) ?? getProviderConfigBlockReason(selectedCreateProviderConfig); const rotateProviderBlockReason = getProviderConfigBlockReason(selectedRotateProviderConfig); - const createProviderHealthText = providerHealthText( - selectedCreateProvider, - providerHealthQuery.data ?? null, - selectedCreateProviderConfig, - ); const awsManagedPathPreview = getAwsManagedPathPreview({ provider: selectedCreateProvider, health: providerHealthQuery.data ?? null, @@ -873,6 +949,95 @@ export function Secrets() { secretKeySource: createForm.key.trim() || createForm.name, }); + const createVaultOptions = useMemo( + () => + buildCreateVaultOptions({ + providers, + providerConfigs, + mode: createMode, + health: providerHealthQuery.data ?? null, + }), + [createMode, providerConfigs, providerHealthQuery.data, providers], + ); + const createVaultValue = createVaultOptionValue(createForm.provider, createForm.providerConfigId); + const createVaultLabel = + createVaultOptions.find((option) => option.value === createVaultValue)?.label.replace(" (default)", "") ?? + vaultKindLabel(providers, createForm.provider); + const createCanLink = + createForm.provider !== "local_encrypted" && + selectedCreateProvider?.supportsExternalReferences !== false && + !createProviderBlockReason; + const createLeafName = createNamePrefix ? createForm.name.slice(createNamePrefix.length) : createForm.name; + // What an agent given access now will read: the user secret's own key, or + // the company secret's name as an environment variable. + const createEnvKey = + secretValueProvider === "user" ? createForm.key.trim() : envKeyFromSecretName(secretLeafName(createForm.name)); + const createNameError = !createLeafName.trim() ? "Give the secret a name." : null; + const createValueError = + secretValueProvider !== "company" + ? null + : createMode === "managed" + ? createForm.value + ? null + : "Paste the value to store." + : createForm.externalRef.trim() + ? null + : "Paste the secret's ARN or path."; + const createKeyError = + secretValueProvider === "user" && !editingDefinition && !createForm.key.trim() + ? "Add the environment variable name agents will read." + : null; + const createAccessError = + createAgentIds.size > 0 && !ENV_KEY_PATTERN.test(createEnvKey) + ? "Agents can only read names made of letters, digits and underscores that don't start with a digit. Rename the secret, or remove the agents and give access later." + : null; + const createBlocked = Boolean( + createNameError || + createValueError || + createKeyError || + createAccessError || + (secretValueProvider === "company" && createProviderBlockReason), + ); + function submitCreate() { + setCreateAttempted(true); + if (createKeyError) setCreateMoreOpen(true); + if (createBlocked || createMutation.isPending) return; + setCreateError(null); + createMutation.mutate(); + } + function setCreateName(name: string) { + setCreateForm((current) => ({ + ...current, + name, + key: createKeyDirty + ? current.key + : secretValueProvider === "user" + ? normalizeUserSecretKeyForPreview(name) + : normalizeSecretKeyForPreview(name), + })); + } + // Only the Name field splits a pasted NAME=value. A value can itself contain + // "=" (base64 padding, connection strings), so a paste into Value is kept as is. + function splitPastedAssignment(event: React.ClipboardEvent) { + if (secretValueProvider !== "company" || createMode !== "managed" || editingDefinition) return; + const pair = parseEnvAssignment(event.clipboardData.getData("text")); + if (!pair) return; + event.preventDefault(); + const name = `${createNamePrefix ?? ""}${pair.name}`; + setCreateForm((current) => ({ + ...current, + name, + value: pair.value, + key: createKeyDirty ? current.key : normalizeSecretKeyForPreview(name), + })); + } + + const vaultNameError = vaultForm.displayName.trim() ? null : "Give the vault a name your team will recognize."; + const vaultRegionError = + vaultForm.provider === "aws_secrets_manager" && !vaultForm.region.trim() + ? "Enter the AWS region, such as us-east-1." + : null; + const unifiedRows = useMemo( () => [ ...secrets.map((secret) => ({ id: `company:${secret.id}`, kind: "company" as const, secret })), @@ -891,7 +1056,7 @@ export function Secrets() { const providedBy: SecretValueProvider = row.kind === "company" ? "company" : "user"; const status = row.kind === "company" ? row.secret.status : row.definition.status; if (providedByFilter !== "all" && providedBy !== providedByFilter) return false; - if (statusFilter !== "all" && status !== statusFilter) return false; + if (statusFilter === "current" ? status !== "active" && status !== "disabled" : status !== statusFilter) return false; if (providerFilter !== "all" && row.kind === "company" && row.secret.provider !== providerFilter) { return false; } @@ -913,7 +1078,7 @@ export function Secrets() { }); }, [providedByFilter, providerFilter, search, statusFilter, unifiedRows]); const activeSecretFilterCount = - (statusFilter === "active" ? 0 : 1) + + (statusFilter === "current" ? 0 : 1) + (providerFilter === "all" ? 0 : 1) + (providedByFilter === "all" ? 0 : 1); @@ -925,17 +1090,8 @@ export function Secrets() { const folderPath = activeTab === "secrets" ? pathParam : ""; const searching = search.trim().length > 0; - const [storedViewMode, setStoredViewMode] = useState(readStoredViewMode); - const hasSlashNames = useMemo( - () => unifiedRows.some((row) => getSecretPathRowName(row).includes("/")), - [unifiedRows], - ); - // No explicit preference → default to Folders once any name has a slash. - const resolvedViewMode: SecretsViewMode = storedViewMode ?? (hasSlashNames ? "folders" : "flat"); - // A `?path=` deep link forces folder view for the visit even if the stored - // preference is Flat. Search always renders a flat global result set. - const effectiveViewMode: SecretsViewMode = folderPath ? "folders" : resolvedViewMode; - const showFolderView = effectiveViewMode === "folders" && !searching; + // Folders are always on; a search renders one flat result set across them. + const showFolderView = !searching; const goToFolder = useCallback( (path: string) => { @@ -970,23 +1126,32 @@ export function Secrets() { closeNewFolder(); } - const setViewMode = useCallback( - (mode: SecretsViewMode) => { - setStoredViewMode(mode); - try { - window.localStorage.setItem(SECRETS_VIEW_MODE_STORAGE_KEY, mode); - } catch { - // Ignore storage failures (private mode / disabled); view still works. + // Secrets Paperclip saved on someone's behalf (connections, sign-ins, …) + // sit apart from the ones the team named, so the list a person manages is + // not buried under tokens they never typed. + const { teamRows, managedGroups, managedCount } = useMemo(() => { + const team: UnifiedSecretRow[] = []; + const byOrigin = new Map(); + for (const row of filteredRows) { + const origin = classifySecretOrigin(row.kind === "company" ? row.secret : row.definition); + if (origin === "team") { + team.push(row); + continue; } - // Flat has no notion of a current folder — leaving it out of the URL. - if (mode === "flat") goToFolder(""); - }, - [goToFolder], - ); + const group = byOrigin.get(origin); + if (group) group.push(row); + else byOrigin.set(origin, [row]); + } + const groups = SECRET_ORIGIN_GROUPS.flatMap((group) => { + const rows = byOrigin.get(group.id); + return rows ? [{ group, rows }] : []; + }); + return { teamRows: team, managedGroups: groups, managedCount: filteredRows.length - team.length }; + }, [filteredRows]); const folderListing = useMemo( - () => buildSecretPathListing(filteredRows, folderPath), - [filteredRows, folderPath], + () => buildSecretPathListing(teamRows, folderPath), + [teamRows, folderPath], ); const breadcrumbs = useMemo(() => buildSecretPathBreadcrumbs(folderPath), [folderPath]); const parentFolderPath = useMemo(() => { @@ -997,7 +1162,9 @@ export function Secrets() { folderListing.secrets.length + folderListing.folders.reduce((total, folder) => total + folder.secretCount, 0); const folderRows = showFolderView ? folderListing.folders : []; - const secretRows = showFolderView ? folderListing.secrets : filteredRows; + const secretRows = showFolderView ? folderListing.secrets : teamRows; + const showManagedSection = managedCount > 0 && (searching || !folderPath); + const managedExpanded = managedOpen || searching; const showUpRow = showFolderView && folderPath.length > 0; const usageQuery = useQuery({ @@ -1013,14 +1180,11 @@ export function Secrets() { enabled: Boolean(selectedSecret), }); - const usageDialogQuery = useQuery({ - queryKey: usageDialogSecret - ? queryKeys.secrets.usage(usageDialogSecret.id) - : ["secrets", "usage-dialog", "__disabled__"], - queryFn: () => secretsApi.usage(usageDialogSecret!.id), - enabled: Boolean(usageDialogSecret), - }); - + // Agents are listed with their environment variable above; this is everything else. + const otherSecretUses = useMemo( + () => (usageQuery.data?.bindings ?? []).filter((binding) => binding.target.type !== "agent"), + [usageQuery.data], + ); function invalidateAll(extraIds: string[] = []) { if (!selectedCompanyId) return; queryClient.invalidateQueries({ queryKey: queryKeys.secrets.list(selectedCompanyId) }); @@ -1034,6 +1198,12 @@ export function Secrets() { } } + function resetCreateExtras() { + setCreateAgentIds(new Set()); + setCreateValueVisible(false); + setCreateMoreOpen(false); + setCreateAttempted(false); + } function openCreateSecret() { const prefix = folderPath ? `${folderPath}/` : null; setEditingDefinition(null); @@ -1041,8 +1211,8 @@ export function Secrets() { setSecretValueProvider("company"); setCreateMode("managed"); setCreateKeyDirty(false); - setCreateKeyEditable(false); setCreateError(null); + resetCreateExtras(); setCreateForm({ name: prefix ?? "", key: "", @@ -1062,8 +1232,8 @@ export function Secrets() { setSecretValueProvider("user"); setCreateMode("managed"); setCreateKeyDirty(true); - setCreateKeyEditable(false); setCreateError(null); + resetCreateExtras(); setCreateForm({ name: definition.name, key: definition.key, @@ -1090,15 +1260,30 @@ export function Secrets() { editingDefinition.id, sharedDefinitionPayload, ); - return { kind: "user" as const, item: definition, action: "updated" as const }; + return { kind: "user" as const, item: definition, action: "updated" as const, accessFailures: [] as string[] }; } + // Access is a second step on each chosen agent's own config. The secret + // is already saved if one of them fails, so failures are reported, not thrown. + const grantAccess = async (reference: AgentAccessReference, envKey: string) => { + const results = await Promise.allSettled( + [...createAgentIds].map((agentId) => + grantAgentSecretAccess({ companyId: selectedCompanyId!, agentId, envKey, reference }), + ), + ); + return results.flatMap((result) => (result.status === "rejected" ? [readableErrorMessage(result.reason)] : [])); + }; if (secretValueProvider === "user") { const definition = await secretsApi.createUserSecretDefinition(selectedCompanyId!, { ...sharedDefinitionPayload, key: createForm.key.trim(), status: "active", }); - return { kind: "user" as const, item: definition, action: "created" as const }; + return { + kind: "user" as const, + item: definition, + action: "created" as const, + accessFailures: await grantAccess({ kind: "user", definition }, definition.key), + }; } const input: CreateSecretInput = { @@ -1115,26 +1300,35 @@ export function Secrets() { input.externalRef = createForm.externalRef.trim(); } const secret = await secretsApi.create(selectedCompanyId!, input); - return { kind: "company" as const, item: secret, action: "created" as const }; + return { + kind: "company" as const, + item: secret, + action: "created" as const, + accessFailures: await grantAccess({ kind: "company", secret }, createEnvKey), + }; }, onSuccess: (result) => { - pushToast({ - title: - result.kind === "company" - ? "Secret created" - : result.action === "updated" - ? "User-provided secret updated" - : "User-provided secret created", - body: result.item.name, - tone: "success", - }); + // A new secret opens in its details panel, which is the confirmation. + if (result.action === "updated") { + pushToast({ title: "Secret updated", body: result.item.name, tone: "success" }); + } + if (result.accessFailures.length > 0) { + pushToast({ + title: "Some agents were not given access", + body: `${result.accessFailures.join(" ")} Add them from the secret's details.`, + tone: "error", + }); + } + if (createAgentIds.size > 0 && selectedCompanyId) { + queryClient.invalidateQueries({ queryKey: queryKeys.agents.list(selectedCompanyId) }); + } + resetCreateExtras(); setCreateOpen(false); setEditingDefinition(null); setCreateNamePrefix(null); setSecretValueProvider("company"); setCreateKeyDirty(false); - setCreateKeyEditable(false); - setCreateForm({ + setCreateForm({ name: "", key: "", value: "", @@ -1173,7 +1367,7 @@ export function Secrets() { }); }, onSuccess: (updated) => { - pushToast({ title: "Rotated", body: `${updated.name} → v${updated.latestVersion}`, tone: "success" }); + pushToast({ title: "Value replaced", body: `${updated.name} is now on version ${updated.latestVersion}`, tone: "success" }); setRotateOpen(false); setRotateValue(""); setRotateExternalRef(""); @@ -1216,7 +1410,7 @@ export function Secrets() { mutationFn: ({ definition, status }: { definition: UserSecretDefinition; status: SecretStatus }) => secretsApi.updateUserSecretDefinition(selectedCompanyId!, definition.id, { status }), onSuccess: (updated) => { - pushToast({ title: `User-provided secret ${updated.status}`, body: updated.name, tone: "info" }); + pushToast({ title: `Secret ${updated.status}`, body: updated.name, tone: "info" }); invalidateAll([updated.id]); }, onError: (error) => { @@ -1249,7 +1443,7 @@ export function Secrets() { mutationFn: (definition: UserSecretDefinition) => secretsApi.removeUserSecretDefinition(selectedCompanyId!, definition.id), onSuccess: (_response, definition) => { - pushToast({ title: "User-provided secret removed", body: definition.name, tone: "info" }); + pushToast({ title: "Secret deleted", body: definition.name, tone: "info" }); setDefinitionDeleteConfirm(null); if (selectedDefinitionId === definition.id) setDetailSelection(null); invalidateAll([definition.id]); @@ -1280,7 +1474,26 @@ export function Secrets() { } as CreateSecretProviderConfigInput); }, onSuccess: (saved) => { - pushToast({ title: editingVault ? "Provider vault updated" : "Provider vault created", body: saved.displayName, tone: "success" }); + pushToast({ title: editingVault ? "Vault updated" : "Vault connected", body: saved.displayName, tone: "success" }); + // A new connection is only useful if it works: check it straight away so + // the list shows a real result instead of "Not checked yet". + if (!editingVault && saved.status !== "coming_soon") { + void secretsApi + .checkProviderConfigHealth(saved.id) + .then((health) => { + if (health.status === "error") { + pushToast({ title: `${saved.displayName} can't be reached`, body: health.message, tone: "error" }); + } + }) + .catch((error) => { + pushToast({ + title: `Couldn't check ${saved.displayName}`, + body: `${describeError(error).body} The vault is saved. Use Check connection in its menu to try again.`, + tone: "error", + }); + }) + .finally(() => invalidateAll()); + } setVaultDialogOpen(false); setEditingVault(null); setVaultForm(emptyProviderVaultForm()); @@ -1313,7 +1526,7 @@ export function Secrets() { const disableVaultMutation = useMutation({ mutationFn: (id: string) => secretsApi.disableProviderConfig(id), onSuccess: (updated) => { - pushToast({ title: "Provider vault disabled", body: updated.displayName, tone: "info" }); + pushToast({ title: "Vault disabled", body: updated.displayName, tone: "info" }); invalidateAll(); }, onError: (error) => { @@ -1325,11 +1538,27 @@ export function Secrets() { }, }); + const enableVaultMutation = useMutation({ + // Providers Paperclip can't use yet only accept their draft status back. + mutationFn: (id: string) => { + const provider = providerConfigs.find((config) => config.id === id)?.provider; + return secretsApi.updateProviderConfig(id, { + status: provider ? defaultProviderVaultStatus(provider) : "ready", + }); + }, + onSuccess: (updated) => { + pushToast({ title: "Vault enabled", body: updated.displayName, tone: "success" }); + invalidateAll(); + }, + onError: (error) => { + pushToast({ title: "Enable failed", body: describeError(error).body, tone: "error" }); + }, + }); const removeVaultMutation = useMutation({ mutationFn: (id: string) => secretsApi.removeProviderConfig(id), onSuccess: (removed) => { pushToast({ - title: "Provider vault removed", + title: "Vault removed", body: `${removed.displayName} was removed from Paperclip only.`, tone: "info", }); @@ -1363,7 +1592,11 @@ export function Secrets() { const healthVaultMutation = useMutation({ mutationFn: (id: string) => secretsApi.checkProviderConfigHealth(id), onSuccess: (health) => { - pushToast({ title: "Health checked", body: health.message, tone: health.status === "error" ? "error" : "info" }); + pushToast({ + title: health.status === "error" ? "The vault can't be reached" : "The vault is working", + body: health.message, + tone: health.status === "error" ? "error" : "info", + }); invalidateAll(); }, onError: (error) => { @@ -1422,6 +1655,8 @@ export function Secrets() { function openCreateVault(provider: SecretProvider = "local_encrypted") { setEditingVault(null); setVaultForm(emptyProviderVaultForm(provider)); + setVaultAttempted(false); + setVaultAdvancedOpen(false); setVaultError(null); setVaultDiscovery(null); setVaultDiscoveryError(null); @@ -1431,6 +1666,8 @@ export function Secrets() { function openEditVault(config: CompanySecretProviderConfig) { setEditingVault(config); setVaultForm(providerVaultFormFromConfig(config)); + setVaultAttempted(false); + setVaultAdvancedOpen(false); setVaultError(null); setVaultDiscovery(null); setVaultDiscoveryError(null); @@ -1458,12 +1695,10 @@ export function Secrets() { } function openCompanySecret(secret: CompanySecret) { - setSecretDetailTab("details"); setDetailSelection(secret.id); } function openUserDefinition(definition: UserSecretDefinition) { - setSecretDetailTab("details"); setDetailSelection(null, definition.id); } @@ -1477,8 +1712,8 @@ export function Secrets() { function rotateActionLabel(secret: CompanySecret) { return secret.managedMode === "external_reference" && !secretSupportsExternalValueWrite(secret) - ? "Update reference" - : "Update value"; + ? "Change reference" + : "Replace value"; } function openRotateSecret(secret: CompanySecret) { @@ -1542,13 +1777,10 @@ export function Secrets() { else openUserDefinition(row.definition); }} > - View details + Details and agent access {row.kind === "company" ? ( <> - setUsageDialogSecretId(row.secret.id)}> - View references ({row.secret.referenceCount ?? 0}) - openRotateSecret(row.secret)}> {rotateActionLabel(row.secret)} @@ -1564,7 +1796,7 @@ export function Secrets() { } > {row.secret.status === "active" ? : } - {row.secret.status === "active" ? "Disable" : "Activate"} + {row.secret.status === "active" ? "Disable" : "Enable"} )} - {row.secret.status === "archived" ? "Unarchive" : "Archive"} + {row.secret.status === "archived" ? "Restore" : "Archive"} setDeleteConfirm(row.secret)}> @@ -1606,7 +1838,7 @@ export function Secrets() { : "Set my value"} openEditDefinition(row.definition)}> - Edit definition + Edit )} - {row.definition.status === "active" ? "Disable" : "Activate"} + {row.definition.status === "active" ? "Disable" : "Enable"} )} - {row.definition.status === "archived" ? "Unarchive" : "Archive"} + {row.definition.status === "archived" ? "Restore" : "Archive"} setDefinitionDeleteConfirm(row.definition)}> - Delete definition + Delete secret )} @@ -1700,7 +1932,7 @@ export function Secrets() {
@@ -1726,7 +1958,7 @@ export function Secrets() { "flex items-center gap-2 text-xs text-muted-foreground hover:bg-accent/40", variant === "table" ? "border-b border-border/60 px-3 py-2.5" - : "rounded-md border border-border bg-background px-3 py-2.5", + : "border-b border-border/60 px-1 py-2.5", )} > @@ -1804,6 +2036,158 @@ export function Secrets() { ); } + // Only an action still in flight locks a vault's menu. A mutation keeps its + // last variables after it settles, so those alone would lock it for good. + const pendingVaultActionId = + [disableVaultMutation, enableVaultMutation, removeVaultMutation, defaultVaultMutation, healthVaultMutation].find( + (mutation) => mutation.isPending, + )?.variables ?? null; + + function rowAgents(row: UnifiedSecretRow): readonly Agent[] { + return ( + (row.kind === "company" + ? agentUsage.bySecretId.get(row.secret.id) + : agentUsage.byUserSecretKey.get(row.definition.key)) ?? EMPTY_AGENTS + ); + } + /** Named only when the value lives somewhere other than Paperclip's own storage. */ + function secretVaultLine(secret: CompanySecret) { + const config = secret.providerConfigId + ? providerConfigs.find((candidate) => candidate.id === secret.providerConfigId) ?? null + : null; + const linked = secret.managedMode === "external_reference"; + if (!config && !linked && secret.provider === "local_encrypted") return null; + const provider = vaultKindLabel(providers, secret.provider); + return { + linked, + text: `${linked ? "Linked from" : "Stored in"} ${config ? `${config.displayName} · ${provider}` : provider}`, + unavailable: Boolean(getProviderConfigBlockReason(config)), + }; + } + function openRow(row: UnifiedSecretRow) { + if (row.kind === "company") openCompanySecret(row.secret); + else openUserDefinition(row.definition); + } + function isRowSelected(row: UnifiedSecretRow) { + return row.kind === "company" ? selectedSecretId === row.secret.id : selectedDefinitionId === row.definition.id; + } + function renderRowTitle(row: UnifiedSecretRow, label?: string) { + const status = row.kind === "company" ? row.secret.status : row.definition.status; + const vaultLine = row.kind === "company" ? secretVaultLine(row.secret) : null; + const VaultIcon = vaultLine?.linked ? Link2 : Cloud; + return ( + <> +
+ {label ? ( + {label} + ) : ( + renderSecretName(row.kind === "company" ? row.secret.name : row.definition.name) + )} + {status !== "active" ? : null} +
+ {vaultLine ? ( +
+ + {vaultLine.text} + {vaultLine.unavailable ? ( + + + Vault unavailable + + ) : null} +
+ ) : null} + + ); + } + function renderRowProvidedBy(row: UnifiedSecretRow) { + return ( + + ) : undefined + } + /> + ); + } + function renderRowUsedBy(row: UnifiedSecretRow) { + const agents = rowAgents(row); + return ( + + ); + } + function renderSecretTableRow(row: UnifiedSecretRow, label?: string) { + const updatedAt = row.kind === "company" ? row.secret.updatedAt : row.definition.updatedAt; + const updatedTooltip = + row.kind === "company" + ? [ + `Updated: ${formatRelative(row.secret.updatedAt)}`, + `Value replaced: ${formatRelative(row.secret.lastRotatedAt)}`, + `Last used: ${formatRelative(row.secret.lastResolvedAt)}`, + ].join("\n") + : `Updated: ${formatRelative(row.definition.updatedAt)}`; + return ( +
openRow(row)} + > +
{renderRowTitle(row, label)}
+
{renderRowProvidedBy(row)}
+
{renderRowUsedBy(row)}
+
+ +
+
event.stopPropagation()}> + {renderRowActions(row)} +
+
+ ); + } + function renderSecretCard(row: UnifiedSecretRow, label?: string) { + return ( +
openRow(row)} + > +
+
{renderRowTitle(row, label)}
+ {renderRowProvidedBy(row)} + {renderRowUsedBy(row)} +
+
event.stopPropagation()}>{renderRowActions(row)}
+
+ ); + } + const filterChips = [ + providedByFilter !== "all" + ? { label: providedByFilter === "user" ? "Each user" : "Organization", clear: () => setProvidedByFilter("all") } + : null, + providerFilter !== "all" + ? { + label: providerFilter === "local_encrypted" ? "Paperclip" : providerLabel(providers, providerFilter), + clear: () => setProviderFilter("all"), + } + : null, + statusFilter !== "current" + ? { label: statusFilter === "archived" ? "Archived" : "Disabled", clear: () => setStatusFilter("current") } + : null, + ].filter((chip): chip is { label: string; clear: () => void } => chip !== null); + const noTeamRows = folderRows.length === 0 && secretRows.length === 0; + if (!selectedCompanyId) { return (
Select an organization to manage secrets.
@@ -1813,9 +2197,44 @@ export function Secrets() { return (
-
- -

Secrets

+
+
+
+ +

Secrets

+
+
+ + + + + + { + setActiveTab("secrets"); + setSearch(""); + setNewFolderOpen(true); + setNewFolderError(null); + }} + > + New folder + + openImportFromVault()} data-testid="import-from-vault-button"> + Import from a vault + + + + +
+
+

+ API keys and tokens your agents use while they work. + +

-
setSearch(event.target.value)} - placeholder="Search by name, key, ref" + placeholder="Search all secrets" className="pl-7 text-xs sm:text-sm" aria-label="Search secrets" data-page-search-target="true" @@ -1873,59 +2291,23 @@ export function Secrets() { providerFilter={providerFilter} providedByFilter={providedByFilter} providers={providers} - activeFilterCount={activeSecretFilterCount} onStatusChange={setStatusFilter} onProviderChange={setProviderFilter} onProvidedByChange={setProvidedByFilter} /> -
- {(["folders", "flat"] as const).map((mode) => ( + {filterChips.map((chip) => ( + + {chip.label} - ))} -
- openImportFromVault()} - onManageVaults={hideVaultsTab ? undefined : () => setActiveTab("vaults")} - className="ml-auto" - /> - {showFolderView ? ( - - ) : null} - + + ))}
{newFolderOpen && showFolderView ? (
@@ -1977,13 +2359,14 @@ export function Secrets() { !(showFolderView && folderPath) ? ( ) : (
- {showFolderView ? ( + {showFolderView && folderPath ? (
{renderSecretsBreadcrumb()} @@ -1991,16 +2374,11 @@ export function Secrets() {
) : searching ? ( -
-
Search results
-
- {filteredRows.length} {filteredRows.length === 1 ? "match" : "matches"} across all - folders{folderPath ? ` · searching everywhere, not just ${folderPath}` : ""} -
+
+ {filteredRows.length} {filteredRows.length === 1 ? "match" : "matches"} across all folders
) : null} - - {folderRows.length === 0 && secretRows.length === 0 ? ( + {noTeamRows ? ( secretsListLoading ? ( )}
@@ -2217,18 +2515,14 @@ export function Secrets() { retrying={providerConfigsView.isFetching} onCreate={openCreateVault} onEdit={openEditVault} + secrets={secrets} onDisable={(config) => disableVaultMutation.mutate(config.id)} + onEnable={(config) => enableVaultMutation.mutate(config.id)} onRemove={(config) => setRemoveVaultConfirm(config)} onSetDefault={(config) => defaultVaultMutation.mutate(config.id)} onHealthCheck={(config) => healthVaultMutation.mutate(config.id)} onImportSecrets={openImportFromVault} - pendingActionId={ - disableVaultMutation.variables ?? - removeVaultMutation.variables ?? - defaultVaultMutation.variables ?? - healthVaultMutation.variables ?? - null - } + pendingActionId={pendingVaultActionId} /> )} @@ -2247,307 +2541,256 @@ export function Secrets() { if (!open && (selectedSecret || selectedDefinition)) setDetailSelection(null); }} > - + {selectedSecret ? ( <> - + - - {selectedSecret.name} - - - + {selectedSecret.name} + {selectedSecret.status !== "active" ? ( + + + + ) : null} {providerLabel(providers, selectedSecret.provider)} secret {selectedSecret.key} -
- - {selectedSecret.key} - +
+ Key + {selectedSecret.key}
-
- - Organization - - {modeLabel(selectedSecret.managedMode)} - {providerLabel(providers, selectedSecret.provider)} - v{selectedSecret.latestVersion} +
+ + + + + + + + Copy link + + + + statusMutation.mutate({ + id: selectedSecret.id, + status: selectedSecret.status === "active" ? "disabled" : "active", + }) + } + > + {selectedSecret.status === "active" ? ( + + ) : ( + + )} + {selectedSecret.status === "active" ? "Disable" : "Enable"} + + + statusMutation.mutate({ + id: selectedSecret.id, + status: selectedSecret.status === "archived" ? "active" : "archived", + }) + } + > + {selectedSecret.status === "archived" ? ( + + ) : ( + + )} + {selectedSecret.status === "archived" ? "Restore" : "Archive"} + + + setDeleteConfirm(selectedSecret)}> + Delete secret + + +
-
- - - - - - - - - statusMutation.mutate({ - id: selectedSecret.id, - status: selectedSecret.status === "active" ? "disabled" : "active", - }) - } - > - {selectedSecret.status === "active" ? ( - - ) : ( - - )} - {selectedSecret.status === "active" ? "Disable" : "Activate"} - - - statusMutation.mutate({ - id: selectedSecret.id, - status: selectedSecret.status === "archived" ? "active" : "archived", - }) - } - > - {selectedSecret.status === "archived" ? ( - - ) : ( - - )} - {selectedSecret.status === "archived" ? "Unarchive" : "Archive"} - - - setDeleteConfirm(selectedSecret)}> - Delete secret - - - -
- -
- + {selectedSecret.status !== "active" ? ( +

+ {selectedSecret.status === "disabled" + ? "Disabled secrets are not given to agents. A run that needs it will say so." + : "Archived secrets are not given to agents and stay out of the list."} +

+ ) : null} + + {otherSecretUses.length > 0 ? ( + + + + ) : null} + + { + setDetailSelection(null); + setActiveTab("vaults"); + } + } /> -
-
- -
- - setSecretDetailTab("usage")} - /> -
-
- - - - - - -
-
+ + + + +
) : selectedDefinition ? ( <> - + - - {selectedDefinition.name} - - - + {selectedDefinition.name} + {selectedDefinition.status !== "active" ? ( + + + + ) : null} - Each user secret definition {selectedDefinition.key} + Secret each user provides, {selectedDefinition.key} -
- - {selectedDefinition.key} - +
+ Key + {selectedDefinition.key}
-
- - - - +
+ + + + + + + openEditDefinition(selectedDefinition)}> + Edit + + + Copy link + + + + definitionStatusMutation.mutate({ + definition: selectedDefinition, + status: selectedDefinition.status === "active" ? "disabled" : "active", + }) + } + > + {selectedDefinition.status === "active" ? ( + + ) : ( + + )} + {selectedDefinition.status === "active" ? "Disable" : "Enable"} + + + definitionStatusMutation.mutate({ + definition: selectedDefinition, + status: selectedDefinition.status === "archived" ? "active" : "archived", + }) + } + > + {selectedDefinition.status === "archived" ? ( + + ) : ( + + )} + {selectedDefinition.status === "archived" ? "Restore" : "Archive"} + + + setDefinitionDeleteConfirm(selectedDefinition)}> + Delete secret + + +
-
- - - - - - - openEditDefinition(selectedDefinition)}> - Edit definition - - - - definitionStatusMutation.mutate({ - definition: selectedDefinition, - status: selectedDefinition.status === "active" ? "disabled" : "active", - }) - } - > - {selectedDefinition.status === "active" ? ( - - ) : ( - - )} - {selectedDefinition.status === "active" ? "Disable" : "Activate"} - - - definitionStatusMutation.mutate({ - definition: selectedDefinition, - status: selectedDefinition.status === "archived" ? "active" : "archived", - }) - } - > - {selectedDefinition.status === "archived" ? ( - - ) : ( - - )} - {selectedDefinition.status === "archived" ? "Unarchive" : "Archive"} - - - setDefinitionDeleteConfirm(selectedDefinition)}> - Delete definition - - - -
- -
- + {selectedDefinition.status !== "active" ? ( +

+ {selectedDefinition.status === "disabled" + ? "Disabled secrets are not given to agents, and members are not asked for a value." + : "Archived secrets are not given to agents and stay out of the list."} +

+ ) : null} + + + -
-
- -
- - setSecretDetailTab("coverage")} - /> -
-
- - - - - - - - - -
-
+ + + + +
) : null} - !open && setUsageDialogSecretId(null)} - > - - - Secret references - - {usageDialogSecret - ? `${usageDialogSecret.name} is referenced by ${usageDialogSecret.referenceCount ?? 0} ${ - (usageDialogSecret.referenceCount ?? 0) === 1 ? "place" : "places" - }.` - : null} - - - - - - {selectedCompanyId && ( { + if (createMutation.isPending) return; setCreateOpen(open); if (!open) setCreateNamePrefix(null); }} > - {editingDefinition ? "Edit user-provided secret" : "Create secret"} - - Choose who provides the value. Shared fields keep their values when you switch modes. + {editingDefinition ? `Edit ${editingDefinition.name}` : "New secret"} + + {editingDefinition ? "Change this secret's name, description and instructions." : "Add a secret your agents can use."} -
+
{ + event.preventDefault(); + submitCreate(); + }} + > +
+ +
+ {createNamePrefix && !editingDefinition ? ( + + + {createNamePrefix.replace(/\/$/, "")} + + + ) : null} + setCreateName(`${createNamePrefix && !editingDefinition ? createNamePrefix : ""}${event.target.value}`)} + onPaste={splitPastedAssignment} + placeholder={secretValueProvider === "user" ? "Personal GitHub token" : "GITHUB_TOKEN"} + autoComplete="off" + spellCheck={false} + aria-invalid={createAttempted && Boolean(createNameError)} + aria-describedby="new-secret-name-hint" + autoFocus + /> +
+

+ {createAttempted && createNameError ? ( + createNameError + ) : createEnvKey && createEnvKey !== createLeafName.trim() ? ( + <> + Agents read it as {createEnvKey}. + + ) : editingDefinition || secretValueProvider !== "company" || createMode !== "managed" ? ( + "Use a slash to put it in a folder, like staging/API_KEY." + ) : ( + "Tip: paste NAME=value and both fields fill in." + )} +

+
+ {!editingDefinition ? ( -
-

Who provides the value?

- +

Who provides the value

+ { const next = value as SecretValueProvider; setSecretValueProvider(next); - setCreateKeyEditable(false); setCreateForm((current) => ({ ...current, key: createKeyDirty @@ -2607,300 +2912,319 @@ export function Secrets() { : normalizeSecretKeyForPreview(current.name), })); }} - > - - Organization - Each user - -
-

- Organization stores one shared value. Each user lets every member supply their own value under My secrets. -

+ options={[ + { + value: "company", + title: "Organization", + description: "One value, shared by every agent you choose.", + icon: , + }, + { + value: "user", + title: "Each user", + description: "Every member adds their own under My secrets.", + icon: , + }, + ]} + />
) : null} - {secretValueProvider === "company" && !editingDefinition ? ( - setCreateMode(value as CreateMode)}> - - Managed value - External reference - - - ) : null} - -
- - {createNamePrefix && !editingDefinition ? ( -
- - {createNamePrefix} - - - { - const name = createNamePrefix + event.target.value; - setCreateForm((current) => ({ - ...current, - name, - key: createKeyDirty - ? current.key - : secretValueProvider === "user" - ? normalizeUserSecretKeyForPreview(name) - : normalizeSecretKeyForPreview(name), - })); - }} - placeholder="clientsecret" - autoFocus - /> -
- ) : ( - { - const name = event.target.value; - setCreateForm((current) => ({ - ...current, - name, - key: createKeyDirty - ? current.key - : secretValueProvider === "user" - ? normalizeUserSecretKeyForPreview(name) - : normalizeSecretKeyForPreview(name), - })); - }} - placeholder={secretValueProvider === "user" ? "Personal GitHub token" : "/dev/foo/bar"} - autoFocus - /> - )} - {createNamePrefix && !editingDefinition ? ( -

- Creating in {folderPath} — remove the chip to type a different path. -

- ) : null} -
- {secretValueProvider === "company" && createMode === "managed" ? ( -
- -