diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000..75c5a36188 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,49 @@ +## Thinking Path + + + +> - Paperclip orchestrates AI agents for zero-human companies +> - [Which subsystem or capability is involved] +> - [What problem or gap exists] +> - [Why it needs to be addressed] +> - This pull request ... +> - The benefit is ... + +## What Changed + + + +- + +## Verification + + + +- + +## Risks + + + +- + +## Checklist + +- [ ] I have included a thinking path that traces from project context to this change +- [ ] I have run tests locally and they pass +- [ ] I have added or updated tests where applicable +- [ ] If this change affects the UI, I have included before/after screenshots +- [ ] I have updated relevant documentation to reflect my changes +- [ ] I have considered and documented any risks above +- [ ] I will address all Greptile and reviewer comments before requesting merge diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000000..490290c262 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,55 @@ +name: Docker + +on: + push: + branches: + - "master" + tags: + - "v*" + +permissions: + contents: read + packages: write + +jobs: + build-and-push: + runs-on: ubuntu-latest + timeout-minutes: 30 + concurrency: + group: docker-${{ github.ref }} + cancel-in-progress: true + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=raw,value=latest,enable={{is_default_branch}} + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=sha + + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + cache-from: type=gha + cache-to: type=gha,mode=max + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 8ec14f0d22..a45f392ebc 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -40,6 +40,46 @@ jobs: with: node-version: 24 + - name: Validate Dockerfile deps stage + run: | + missing=0 + + # Extract only the deps stage from the Dockerfile + deps_stage="$(awk '/^FROM .* AS deps$/{found=1; next} found && /^FROM /{exit} found{print}' Dockerfile)" + + if [ -z "$deps_stage" ]; then + echo "::error::Could not extract deps stage from Dockerfile (expected 'FROM ... AS deps')" + exit 1 + fi + + # Derive workspace search roots from pnpm-workspace.yaml (exclude dev-only packages) + search_roots="$(grep '^ *- ' pnpm-workspace.yaml | sed 's/^ *- //' | sed 's/\*$//' | grep -v 'examples' | grep -v 'create-paperclip-plugin' | tr '\n' ' ')" + + if [ -z "$search_roots" ]; then + echo "::error::Could not derive workspace roots from pnpm-workspace.yaml" + exit 1 + fi + + # Check all workspace package.json files are copied in the deps stage + for pkg in $(find $search_roots -maxdepth 2 -name package.json -not -path '*/examples/*' -not -path '*/create-paperclip-plugin/*' -not -path '*/node_modules/*' 2>/dev/null | sort -u); do + dir="$(dirname "$pkg")" + if ! echo "$deps_stage" | grep -q "^COPY ${dir}/package.json"; then + echo "::error::Dockerfile deps stage missing: COPY ${pkg} ${dir}/" + missing=1 + fi + done + + # Check patches directory is copied if it exists + if [ -d patches ] && ! echo "$deps_stage" | grep -q '^COPY patches/'; then + echo "::error::Dockerfile deps stage missing: COPY patches/ patches/" + missing=1 + fi + + if [ "$missing" -eq 1 ]; then + echo "Dockerfile deps stage is out of sync. Update it to include the missing files." + exit 1 + fi + - name: Validate dependency resolution when manifests change run: | changed="$(git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}")" diff --git a/Dockerfile b/Dockerfile index 014113e432..8b65b0e210 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,8 @@ COPY packages/adapters/gemini-local/package.json packages/adapters/gemini-local/ COPY packages/adapters/openclaw-gateway/package.json packages/adapters/openclaw-gateway/ COPY packages/adapters/opencode-local/package.json packages/adapters/opencode-local/ COPY packages/adapters/pi-local/package.json packages/adapters/pi-local/ +COPY packages/plugins/sdk/package.json packages/plugins/sdk/ +COPY patches/ patches/ RUN pnpm install --frozen-lockfile @@ -28,6 +30,7 @@ WORKDIR /app COPY --from=deps /app /app COPY . . RUN pnpm --filter @paperclipai/ui build +RUN pnpm --filter @paperclipai/plugin-sdk build RUN pnpm --filter @paperclipai/server build RUN test -f server/dist/index.js || (echo "ERROR: server build output missing" && exit 1) diff --git a/admin-final.png b/admin-final.png new file mode 100644 index 0000000000..49d9483c82 Binary files /dev/null and b/admin-final.png differ diff --git a/admin-page.png b/admin-page.png new file mode 100644 index 0000000000..49d9483c82 Binary files /dev/null and b/admin-page.png differ diff --git a/agent-running.png b/agent-running.png new file mode 100644 index 0000000000..183fd26bfe Binary files /dev/null and b/agent-running.png differ diff --git a/cli/src/__tests__/auth-command-registration.test.ts b/cli/src/__tests__/auth-command-registration.test.ts new file mode 100644 index 0000000000..a93d8fa7c6 --- /dev/null +++ b/cli/src/__tests__/auth-command-registration.test.ts @@ -0,0 +1,16 @@ +import { Command } from "commander"; +import { describe, expect, it } from "vitest"; +import { registerClientAuthCommands } from "../commands/client/auth.js"; + +describe("registerClientAuthCommands", () => { + it("registers auth commands without duplicate company-id flags", () => { + const program = new Command(); + const auth = program.command("auth"); + + expect(() => registerClientAuthCommands(auth)).not.toThrow(); + + const login = auth.commands.find((command) => command.name() === "login"); + expect(login).toBeDefined(); + expect(login?.options.filter((option) => option.long === "--company-id")).toHaveLength(1); + }); +}); diff --git a/cli/src/__tests__/board-auth.test.ts b/cli/src/__tests__/board-auth.test.ts new file mode 100644 index 0000000000..f86f539e90 --- /dev/null +++ b/cli/src/__tests__/board-auth.test.ts @@ -0,0 +1,53 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; +import { + getStoredBoardCredential, + readBoardAuthStore, + removeStoredBoardCredential, + setStoredBoardCredential, +} from "../client/board-auth.js"; + +function createTempAuthPath(): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-cli-auth-")); + return path.join(dir, "auth.json"); +} + +describe("board auth store", () => { + it("returns an empty store when the file does not exist", () => { + const authPath = createTempAuthPath(); + expect(readBoardAuthStore(authPath)).toEqual({ + version: 1, + credentials: {}, + }); + }); + + it("stores and retrieves credentials by normalized api base", () => { + const authPath = createTempAuthPath(); + setStoredBoardCredential({ + apiBase: "http://localhost:3100/", + token: "token-123", + userId: "user-1", + storePath: authPath, + }); + + expect(getStoredBoardCredential("http://localhost:3100", authPath)).toMatchObject({ + apiBase: "http://localhost:3100", + token: "token-123", + userId: "user-1", + }); + }); + + it("removes stored credentials", () => { + const authPath = createTempAuthPath(); + setStoredBoardCredential({ + apiBase: "http://localhost:3100", + token: "token-123", + storePath: authPath, + }); + + expect(removeStoredBoardCredential("http://localhost:3100", authPath)).toBe(true); + expect(getStoredBoardCredential("http://localhost:3100", authPath)).toBeNull(); + }); +}); diff --git a/cli/src/__tests__/company-import-export-e2e.test.ts b/cli/src/__tests__/company-import-export-e2e.test.ts new file mode 100644 index 0000000000..27334105a3 --- /dev/null +++ b/cli/src/__tests__/company-import-export-e2e.test.ts @@ -0,0 +1,543 @@ +import { execFile, spawn } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; +import net from "node:net"; +import os from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { promisify } from "node:util"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { createStoredZipArchive } from "./helpers/zip.js"; + +type EmbeddedPostgresInstance = { + initialise(): Promise; + start(): Promise; + stop(): Promise; +}; + +type EmbeddedPostgresCtor = new (opts: { + databaseDir: string; + user: string; + password: string; + port: number; + persistent: boolean; + initdbFlags?: string[]; + onLog?: (message: unknown) => void; + onError?: (message: unknown) => void; +}) => EmbeddedPostgresInstance; + +const execFileAsync = promisify(execFile); +type ServerProcess = ReturnType; + +async function getEmbeddedPostgresCtor(): Promise { + const mod = await import("embedded-postgres"); + return mod.default as EmbeddedPostgresCtor; +} + +async function getAvailablePort(): Promise { + return await new Promise((resolve, reject) => { + const server = net.createServer(); + server.unref(); + server.on("error", reject); + server.listen(0, "127.0.0.1", () => { + const address = server.address(); + if (!address || typeof address === "string") { + server.close(() => reject(new Error("Failed to allocate test port"))); + return; + } + const { port } = address; + server.close((error) => { + if (error) reject(error); + else resolve(port); + }); + }); + }); +} + +async function startTempDatabase() { + const dataDir = mkdtempSync(path.join(os.tmpdir(), "paperclip-company-cli-db-")); + const port = await getAvailablePort(); + const EmbeddedPostgres = await getEmbeddedPostgresCtor(); + const instance = new EmbeddedPostgres({ + databaseDir: dataDir, + user: "paperclip", + password: "paperclip", + port, + persistent: true, + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], + onLog: () => {}, + onError: () => {}, + }); + await instance.initialise(); + await instance.start(); + + const { applyPendingMigrations, ensurePostgresDatabase } = await import("@paperclipai/db"); + const adminConnectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/postgres`; + await ensurePostgresDatabase(adminConnectionString, "paperclip"); + const connectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/paperclip`; + await applyPendingMigrations(connectionString); + + return { connectionString, dataDir, instance }; +} + +function writeTestConfig(configPath: string, tempRoot: string, port: number, connectionString: string) { + const config = { + $meta: { + version: 1, + updatedAt: new Date().toISOString(), + source: "doctor", + }, + database: { + mode: "postgres", + connectionString, + embeddedPostgresDataDir: path.join(tempRoot, "embedded-db"), + embeddedPostgresPort: 54329, + backup: { + enabled: false, + intervalMinutes: 60, + retentionDays: 30, + dir: path.join(tempRoot, "backups"), + }, + }, + logging: { + mode: "file", + logDir: path.join(tempRoot, "logs"), + }, + server: { + deploymentMode: "local_trusted", + exposure: "private", + host: "127.0.0.1", + port, + allowedHostnames: [], + serveUi: false, + }, + auth: { + baseUrlMode: "auto", + disableSignUp: false, + }, + storage: { + provider: "local_disk", + localDisk: { + baseDir: path.join(tempRoot, "storage"), + }, + s3: { + bucket: "paperclip", + region: "us-east-1", + prefix: "", + forcePathStyle: false, + }, + }, + secrets: { + provider: "local_encrypted", + strictMode: false, + localEncrypted: { + keyFilePath: path.join(tempRoot, "secrets", "master.key"), + }, + }, + }; + + mkdirSync(path.dirname(configPath), { recursive: true }); + writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf8"); +} + +function createServerEnv(configPath: string, port: number, connectionString: string) { + const env = { ...process.env }; + for (const key of Object.keys(env)) { + if (key.startsWith("PAPERCLIP_")) { + delete env[key]; + } + } + delete env.DATABASE_URL; + delete env.PORT; + delete env.HOST; + delete env.SERVE_UI; + delete env.HEARTBEAT_SCHEDULER_ENABLED; + + env.PAPERCLIP_CONFIG = configPath; + env.DATABASE_URL = connectionString; + env.HOST = "127.0.0.1"; + env.PORT = String(port); + env.SERVE_UI = "false"; + env.PAPERCLIP_DB_BACKUP_ENABLED = "false"; + env.HEARTBEAT_SCHEDULER_ENABLED = "false"; + env.PAPERCLIP_MIGRATION_AUTO_APPLY = "true"; + env.PAPERCLIP_UI_DEV_MIDDLEWARE = "false"; + + return env; +} + +function createCliEnv() { + const env = { ...process.env }; + for (const key of Object.keys(env)) { + if (key.startsWith("PAPERCLIP_")) { + delete env[key]; + } + } + delete env.DATABASE_URL; + delete env.PORT; + delete env.HOST; + delete env.SERVE_UI; + delete env.PAPERCLIP_DB_BACKUP_ENABLED; + delete env.HEARTBEAT_SCHEDULER_ENABLED; + delete env.PAPERCLIP_MIGRATION_AUTO_APPLY; + delete env.PAPERCLIP_UI_DEV_MIDDLEWARE; + return env; +} + +function collectTextFiles(root: string, current: string, files: Record) { + for (const entry of readdirSync(current, { withFileTypes: true })) { + const absolutePath = path.join(current, entry.name); + if (entry.isDirectory()) { + collectTextFiles(root, absolutePath, files); + continue; + } + if (!entry.isFile()) continue; + const relativePath = path.relative(root, absolutePath).replace(/\\/g, "/"); + files[relativePath] = readFileSync(absolutePath, "utf8"); + } +} + +async function stopServerProcess(child: ServerProcess | null) { + if (!child || child.exitCode !== null) return; + child.kill("SIGTERM"); + await new Promise((resolve) => { + child.once("exit", () => resolve()); + setTimeout(() => { + if (child.exitCode === null) { + child.kill("SIGKILL"); + } + }, 5_000); + }); +} + +async function api(baseUrl: string, pathname: string, init?: RequestInit): Promise { + const res = await fetch(`${baseUrl}${pathname}`, init); + const text = await res.text(); + if (!res.ok) { + throw new Error(`Request failed ${res.status} ${pathname}: ${text}`); + } + return text ? JSON.parse(text) as T : (null as T); +} + +async function runCliJson(args: string[], opts: { apiBase: string; configPath: string }) { + const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../.."); + const result = await execFileAsync( + "pnpm", + ["--silent", "paperclipai", ...args, "--api-base", opts.apiBase, "--config", opts.configPath, "--json"], + { + cwd: repoRoot, + env: createCliEnv(), + maxBuffer: 10 * 1024 * 1024, + }, + ); + const stdout = result.stdout.trim(); + const jsonStart = stdout.search(/[\[{]/); + if (jsonStart === -1) { + throw new Error(`CLI did not emit JSON.\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`); + } + return JSON.parse(stdout.slice(jsonStart)) as T; +} + +async function waitForServer( + apiBase: string, + child: ServerProcess, + output: { stdout: string[]; stderr: string[] }, +) { + const startedAt = Date.now(); + while (Date.now() - startedAt < 30_000) { + if (child.exitCode !== null) { + throw new Error( + `paperclipai run exited before healthcheck succeeded.\nstdout:\n${output.stdout.join("")}\nstderr:\n${output.stderr.join("")}`, + ); + } + + try { + const res = await fetch(`${apiBase}/api/health`); + if (res.ok) return; + } catch { + // Server is still starting. + } + + await new Promise((resolve) => setTimeout(resolve, 250)); + } + + throw new Error( + `Timed out waiting for ${apiBase}/api/health.\nstdout:\n${output.stdout.join("")}\nstderr:\n${output.stderr.join("")}`, + ); +} + +describe("paperclipai company import/export e2e", () => { + let tempRoot = ""; + let configPath = ""; + let exportDir = ""; + let apiBase = ""; + let serverProcess: ServerProcess | null = null; + let dbDataDir = ""; + let dbInstance: EmbeddedPostgresInstance | null = null; + + beforeAll(async () => { + tempRoot = mkdtempSync(path.join(os.tmpdir(), "paperclip-company-cli-e2e-")); + configPath = path.join(tempRoot, "config", "config.json"); + exportDir = path.join(tempRoot, "exported-company"); + + const db = await startTempDatabase(); + dbDataDir = db.dataDir; + dbInstance = db.instance; + + const port = await getAvailablePort(); + writeTestConfig(configPath, tempRoot, port, db.connectionString); + apiBase = `http://127.0.0.1:${port}`; + + const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../.."); + const output = { stdout: [] as string[], stderr: [] as string[] }; + const child = spawn( + "pnpm", + ["paperclipai", "run", "--config", configPath], + { + cwd: repoRoot, + env: createServerEnv(configPath, port, db.connectionString), + stdio: ["ignore", "pipe", "pipe"], + }, + ); + serverProcess = child; + child.stdout?.on("data", (chunk) => { + output.stdout.push(String(chunk)); + }); + child.stderr?.on("data", (chunk) => { + output.stderr.push(String(chunk)); + }); + + await waitForServer(apiBase, child, output); + }, 60_000); + + afterAll(async () => { + await stopServerProcess(serverProcess); + await dbInstance?.stop(); + if (dbDataDir) { + rmSync(dbDataDir, { recursive: true, force: true }); + } + if (tempRoot) { + rmSync(tempRoot, { recursive: true, force: true }); + } + }); + + it("exports a company package and imports it into new and existing companies", async () => { + expect(serverProcess).not.toBeNull(); + + const sourceCompany = await api<{ id: string; name: string; issuePrefix: string }>(apiBase, "/api/companies", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ name: `CLI Export Source ${Date.now()}` }), + }); + + const sourceAgent = await api<{ id: string; name: string }>( + apiBase, + `/api/companies/${sourceCompany.id}/agents`, + { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "Export Engineer", + role: "engineer", + adapterType: "claude_local", + adapterConfig: { + promptTemplate: "You verify company portability.", + }, + }), + }, + ); + + const sourceProject = await api<{ id: string; name: string }>( + apiBase, + `/api/companies/${sourceCompany.id}/projects`, + { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + name: "Portability Verification", + status: "in_progress", + }), + }, + ); + + const largeIssueDescription = `Round-trip the company package through the CLI.\n\n${"portable-data ".repeat(12_000)}`; + + const sourceIssue = await api<{ id: string; title: string; identifier: string }>( + apiBase, + `/api/companies/${sourceCompany.id}/issues`, + { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + title: "Validate company import/export", + description: largeIssueDescription, + status: "todo", + projectId: sourceProject.id, + assigneeAgentId: sourceAgent.id, + }), + }, + ); + + const exportResult = await runCliJson<{ + ok: boolean; + out: string; + filesWritten: number; + }>( + [ + "company", + "export", + sourceCompany.id, + "--out", + exportDir, + "--include", + "company,agents,projects,issues", + ], + { apiBase, configPath }, + ); + + expect(exportResult.ok).toBe(true); + expect(exportResult.filesWritten).toBeGreaterThan(0); + expect(readFileSync(path.join(exportDir, "COMPANY.md"), "utf8")).toContain(sourceCompany.name); + expect(readFileSync(path.join(exportDir, ".paperclip.yaml"), "utf8")).toContain('schema: "paperclip/v1"'); + + const importedNew = await runCliJson<{ + company: { id: string; name: string; action: string }; + agents: Array<{ id: string | null; action: string; name: string }>; + }>( + [ + "company", + "import", + exportDir, + "--target", + "new", + "--new-company-name", + `Imported ${sourceCompany.name}`, + "--include", + "company,agents,projects,issues", + "--yes", + ], + { apiBase, configPath }, + ); + + expect(importedNew.company.action).toBe("created"); + expect(importedNew.agents).toHaveLength(1); + expect(importedNew.agents[0]?.action).toBe("created"); + + const importedAgents = await api>( + apiBase, + `/api/companies/${importedNew.company.id}/agents`, + ); + const importedProjects = await api>( + apiBase, + `/api/companies/${importedNew.company.id}/projects`, + ); + const importedIssues = await api>( + apiBase, + `/api/companies/${importedNew.company.id}/issues`, + ); + + expect(importedAgents.map((agent) => agent.name)).toContain(sourceAgent.name); + expect(importedProjects.map((project) => project.name)).toContain(sourceProject.name); + expect(importedIssues.map((issue) => issue.title)).toContain(sourceIssue.title); + + const previewExisting = await runCliJson<{ + errors: string[]; + plan: { + companyAction: string; + agentPlans: Array<{ action: string }>; + projectPlans: Array<{ action: string }>; + issuePlans: Array<{ action: string }>; + }; + }>( + [ + "company", + "import", + exportDir, + "--target", + "existing", + "--company-id", + importedNew.company.id, + "--include", + "company,agents,projects,issues", + "--collision", + "rename", + "--dry-run", + ], + { apiBase, configPath }, + ); + + expect(previewExisting.errors).toEqual([]); + expect(previewExisting.plan.companyAction).toBe("none"); + expect(previewExisting.plan.agentPlans.some((plan) => plan.action === "create")).toBe(true); + expect(previewExisting.plan.projectPlans.some((plan) => plan.action === "create")).toBe(true); + expect(previewExisting.plan.issuePlans.some((plan) => plan.action === "create")).toBe(true); + + const importedExisting = await runCliJson<{ + company: { id: string; action: string }; + agents: Array<{ id: string | null; action: string; name: string }>; + }>( + [ + "company", + "import", + exportDir, + "--target", + "existing", + "--company-id", + importedNew.company.id, + "--include", + "company,agents,projects,issues", + "--collision", + "rename", + "--yes", + ], + { apiBase, configPath }, + ); + + expect(importedExisting.company.action).toBe("unchanged"); + expect(importedExisting.agents.some((agent) => agent.action === "created")).toBe(true); + + const twiceImportedAgents = await api>( + apiBase, + `/api/companies/${importedNew.company.id}/agents`, + ); + const twiceImportedProjects = await api>( + apiBase, + `/api/companies/${importedNew.company.id}/projects`, + ); + const twiceImportedIssues = await api>( + apiBase, + `/api/companies/${importedNew.company.id}/issues`, + ); + + expect(twiceImportedAgents).toHaveLength(2); + expect(new Set(twiceImportedAgents.map((agent) => agent.name)).size).toBe(2); + expect(twiceImportedProjects).toHaveLength(2); + expect(twiceImportedIssues).toHaveLength(2); + + const zipPath = path.join(tempRoot, "exported-company.zip"); + const portableFiles: Record = {}; + collectTextFiles(exportDir, exportDir, portableFiles); + writeFileSync(zipPath, createStoredZipArchive(portableFiles, "paperclip-demo")); + + const importedFromZip = await runCliJson<{ + company: { id: string; name: string; action: string }; + agents: Array<{ id: string | null; action: string; name: string }>; + }>( + [ + "company", + "import", + zipPath, + "--target", + "new", + "--new-company-name", + `Zip Imported ${sourceCompany.name}`, + "--include", + "company,agents,projects,issues", + "--yes", + ], + { apiBase, configPath }, + ); + + expect(importedFromZip.company.action).toBe("created"); + expect(importedFromZip.agents.some((agent) => agent.action === "created")).toBe(true); + }, 60_000); +}); diff --git a/cli/src/__tests__/company-import-url.test.ts b/cli/src/__tests__/company-import-url.test.ts index a749d57e94..abc96f7da2 100644 --- a/cli/src/__tests__/company-import-url.test.ts +++ b/cli/src/__tests__/company-import-url.test.ts @@ -1,5 +1,10 @@ import { describe, expect, it } from "vitest"; -import { isHttpUrl, isGithubUrl } from "../commands/client/company.js"; +import { + isGithubShorthand, + isGithubUrl, + isHttpUrl, + normalizeGithubImportSource, +} from "../commands/client/company.js"; describe("isHttpUrl", () => { it("matches http URLs", () => { @@ -29,3 +34,41 @@ describe("isGithubUrl", () => { expect(isGithubUrl("/tmp/my-company")).toBe(false); }); }); + +describe("isGithubShorthand", () => { + it("matches owner/repo/path shorthands", () => { + expect(isGithubShorthand("paperclipai/companies/gstack")).toBe(true); + expect(isGithubShorthand("paperclipai/companies")).toBe(true); + }); + + it("rejects local-looking paths", () => { + expect(isGithubShorthand("./exports/acme")).toBe(false); + expect(isGithubShorthand("/tmp/acme")).toBe(false); + expect(isGithubShorthand("C:\\temp\\acme")).toBe(false); + }); +}); + +describe("normalizeGithubImportSource", () => { + it("normalizes shorthand imports to canonical GitHub sources", () => { + expect(normalizeGithubImportSource("paperclipai/companies/gstack")).toBe( + "https://github.com/paperclipai/companies?ref=main&path=gstack", + ); + }); + + it("applies --ref to shorthand imports", () => { + expect(normalizeGithubImportSource("paperclipai/companies/gstack", "feature/demo")).toBe( + "https://github.com/paperclipai/companies?ref=feature%2Fdemo&path=gstack", + ); + }); + + it("applies --ref to existing GitHub tree URLs without losing the package path", () => { + expect( + normalizeGithubImportSource( + "https://github.com/paperclipai/companies/tree/main/gstack", + "release/2026-03-23", + ), + ).toBe( + "https://github.com/paperclipai/companies?ref=release%2F2026-03-23&path=gstack", + ); + }); +}); diff --git a/cli/src/__tests__/company-import-zip.test.ts b/cli/src/__tests__/company-import-zip.test.ts new file mode 100644 index 0000000000..e2983e9a3a --- /dev/null +++ b/cli/src/__tests__/company-import-zip.test.ts @@ -0,0 +1,44 @@ +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { resolveInlineSourceFromPath } from "../commands/client/company.js"; +import { createStoredZipArchive } from "./helpers/zip.js"; + +const tempDirs: string[] = []; + +afterEach(async () => { + for (const dir of tempDirs.splice(0)) { + await rm(dir, { recursive: true, force: true }); + } +}); + +describe("resolveInlineSourceFromPath", () => { + it("imports portable files from a zip archive instead of scanning the parent directory", async () => { + const tempDir = await mkdtemp(path.join(os.tmpdir(), "paperclip-company-import-zip-")); + tempDirs.push(tempDir); + + const archivePath = path.join(tempDir, "paperclip-demo.zip"); + const archive = createStoredZipArchive( + { + "COMPANY.md": "# Company\n", + ".paperclip.yaml": "schema: paperclip/v1\n", + "agents/ceo/AGENT.md": "# CEO\n", + "notes/todo.txt": "ignore me\n", + }, + "paperclip-demo", + ); + await writeFile(archivePath, archive); + + const resolved = await resolveInlineSourceFromPath(archivePath); + + expect(resolved).toEqual({ + rootPath: "paperclip-demo", + files: { + "COMPANY.md": "# Company\n", + ".paperclip.yaml": "schema: paperclip/v1\n", + "agents/ceo/AGENT.md": "# CEO\n", + }, + }); + }); +}); diff --git a/cli/src/__tests__/company.test.ts b/cli/src/__tests__/company.test.ts new file mode 100644 index 0000000000..d74674b2f4 --- /dev/null +++ b/cli/src/__tests__/company.test.ts @@ -0,0 +1,587 @@ +import { describe, expect, it } from "vitest"; +import type { CompanyPortabilityPreviewResult } from "@paperclipai/shared"; +import { + buildCompanyDashboardUrl, + buildDefaultImportAdapterOverrides, + buildDefaultImportSelectionState, + buildImportSelectionCatalog, + buildSelectedFilesFromImportSelection, + renderCompanyImportPreview, + renderCompanyImportResult, + resolveCompanyImportApplyConfirmationMode, + resolveCompanyImportApiPath, +} from "../commands/client/company.js"; + +describe("resolveCompanyImportApiPath", () => { + it("uses company-scoped preview route for existing-company dry runs", () => { + expect( + resolveCompanyImportApiPath({ + dryRun: true, + targetMode: "existing_company", + companyId: "company-123", + }), + ).toBe("/api/companies/company-123/imports/preview"); + }); + + it("uses company-scoped apply route for existing-company imports", () => { + expect( + resolveCompanyImportApiPath({ + dryRun: false, + targetMode: "existing_company", + companyId: "company-123", + }), + ).toBe("/api/companies/company-123/imports/apply"); + }); + + it("keeps global routes for new-company imports", () => { + expect( + resolveCompanyImportApiPath({ + dryRun: true, + targetMode: "new_company", + }), + ).toBe("/api/companies/import/preview"); + + expect( + resolveCompanyImportApiPath({ + dryRun: false, + targetMode: "new_company", + }), + ).toBe("/api/companies/import"); + }); + + it("throws when an existing-company import is missing a company id", () => { + expect(() => + resolveCompanyImportApiPath({ + dryRun: true, + targetMode: "existing_company", + companyId: " ", + }) + ).toThrow(/require a companyId/i); + }); +}); + +describe("resolveCompanyImportApplyConfirmationMode", () => { + it("skips confirmation when --yes is set", () => { + expect( + resolveCompanyImportApplyConfirmationMode({ + yes: true, + interactive: false, + json: false, + }), + ).toBe("skip"); + }); + + it("prompts in interactive text mode when --yes is not set", () => { + expect( + resolveCompanyImportApplyConfirmationMode({ + yes: false, + interactive: true, + json: false, + }), + ).toBe("prompt"); + }); + + it("requires --yes for non-interactive apply", () => { + expect(() => + resolveCompanyImportApplyConfirmationMode({ + yes: false, + interactive: false, + json: false, + }) + ).toThrow(/non-interactive terminal requires --yes/i); + }); + + it("requires --yes for json apply", () => { + expect(() => + resolveCompanyImportApplyConfirmationMode({ + yes: false, + interactive: false, + json: true, + }) + ).toThrow(/with --json requires --yes/i); + }); +}); + +describe("buildCompanyDashboardUrl", () => { + it("preserves the configured base path when building a dashboard URL", () => { + expect(buildCompanyDashboardUrl("https://paperclip.example/app/", "PAP")).toBe( + "https://paperclip.example/app/PAP/dashboard", + ); + }); +}); + +describe("renderCompanyImportPreview", () => { + it("summarizes the preview with counts, selection info, and truncated examples", () => { + const preview: CompanyPortabilityPreviewResult = { + include: { + company: true, + agents: true, + projects: true, + issues: true, + skills: true, + }, + targetCompanyId: "company-123", + targetCompanyName: "Imported Co", + collisionStrategy: "rename", + selectedAgentSlugs: ["ceo", "cto", "eng-1", "eng-2", "eng-3", "eng-4", "eng-5"], + plan: { + companyAction: "update", + agentPlans: [ + { slug: "ceo", action: "create", plannedName: "CEO", existingAgentId: null, reason: null }, + { slug: "cto", action: "update", plannedName: "CTO", existingAgentId: "agent-2", reason: "replace strategy" }, + { slug: "eng-1", action: "skip", plannedName: "Engineer 1", existingAgentId: "agent-3", reason: "skip strategy" }, + { slug: "eng-2", action: "create", plannedName: "Engineer 2", existingAgentId: null, reason: null }, + { slug: "eng-3", action: "create", plannedName: "Engineer 3", existingAgentId: null, reason: null }, + { slug: "eng-4", action: "create", plannedName: "Engineer 4", existingAgentId: null, reason: null }, + { slug: "eng-5", action: "create", plannedName: "Engineer 5", existingAgentId: null, reason: null }, + ], + projectPlans: [ + { slug: "alpha", action: "create", plannedName: "Alpha", existingProjectId: null, reason: null }, + ], + issuePlans: [ + { slug: "kickoff", action: "create", plannedTitle: "Kickoff", reason: null }, + ], + }, + manifest: { + schemaVersion: 1, + generatedAt: "2026-03-23T17:00:00.000Z", + source: { + companyId: "company-src", + companyName: "Source Co", + }, + includes: { + company: true, + agents: true, + projects: true, + issues: true, + skills: true, + }, + company: { + path: "COMPANY.md", + name: "Source Co", + description: null, + brandColor: null, + logoPath: null, + requireBoardApprovalForNewAgents: false, + }, + sidebar: { + agents: ["ceo"], + projects: ["alpha"], + }, + agents: [ + { + slug: "ceo", + name: "CEO", + path: "agents/ceo/AGENT.md", + skills: [], + role: "ceo", + title: null, + icon: null, + capabilities: null, + reportsToSlug: null, + adapterType: "codex_local", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + budgetMonthlyCents: 0, + metadata: null, + }, + ], + skills: [ + { + key: "skill-a", + slug: "skill-a", + name: "Skill A", + path: "skills/skill-a/SKILL.md", + description: null, + sourceType: "inline", + sourceLocator: null, + sourceRef: null, + trustLevel: null, + compatibility: null, + metadata: null, + fileInventory: [], + }, + ], + projects: [ + { + slug: "alpha", + name: "Alpha", + path: "projects/alpha/PROJECT.md", + description: null, + ownerAgentSlug: null, + leadAgentSlug: null, + targetDate: null, + color: null, + status: null, + executionWorkspacePolicy: null, + workspaces: [], + metadata: null, + }, + ], + issues: [ + { + slug: "kickoff", + identifier: null, + title: "Kickoff", + path: "projects/alpha/issues/kickoff/TASK.md", + projectSlug: "alpha", + projectWorkspaceKey: null, + assigneeAgentSlug: "ceo", + description: null, + recurring: false, + routine: null, + legacyRecurrence: null, + status: null, + priority: null, + labelIds: [], + billingCode: null, + executionWorkspaceSettings: null, + assigneeAdapterOverrides: null, + metadata: null, + }, + ], + envInputs: [ + { + key: "OPENAI_API_KEY", + description: null, + agentSlug: "ceo", + kind: "secret", + requirement: "required", + defaultValue: null, + portability: "portable", + }, + ], + }, + files: { + "COMPANY.md": "# Source Co", + }, + envInputs: [ + { + key: "OPENAI_API_KEY", + description: null, + agentSlug: "ceo", + kind: "secret", + requirement: "required", + defaultValue: null, + portability: "portable", + }, + ], + warnings: ["One warning"], + errors: ["One error"], + }; + + const rendered = renderCompanyImportPreview(preview, { + sourceLabel: "GitHub: https://github.com/paperclipai/companies/demo", + targetLabel: "Imported Co (company-123)", + infoMessages: ["Using claude-local adapter"], + }); + + expect(rendered).toContain("Include"); + expect(rendered).toContain("company, projects, tasks, agents, skills"); + expect(rendered).toContain("7 agents total"); + expect(rendered).toContain("1 project total"); + expect(rendered).toContain("1 task total"); + expect(rendered).toContain("skills: 1 skill packaged"); + expect(rendered).toContain("+1 more"); + expect(rendered).toContain("Using claude-local adapter"); + expect(rendered).toContain("Warnings"); + expect(rendered).toContain("Errors"); + }); +}); + +describe("renderCompanyImportResult", () => { + it("summarizes import results with created, updated, and skipped counts", () => { + const rendered = renderCompanyImportResult( + { + company: { + id: "company-123", + name: "Imported Co", + action: "updated", + }, + agents: [ + { slug: "ceo", id: "agent-1", action: "created", name: "CEO", reason: null }, + { slug: "cto", id: "agent-2", action: "updated", name: "CTO", reason: "replace strategy" }, + { slug: "ops", id: null, action: "skipped", name: "Ops", reason: "skip strategy" }, + ], + projects: [ + { slug: "app", id: "project-1", action: "created", name: "App", reason: null }, + { slug: "ops", id: "project-2", action: "updated", name: "Operations", reason: "replace strategy" }, + { slug: "archive", id: null, action: "skipped", name: "Archive", reason: "skip strategy" }, + ], + envInputs: [], + warnings: ["Review API keys"], + }, + { + targetLabel: "Imported Co (company-123)", + companyUrl: "https://paperclip.example/PAP/dashboard", + infoMessages: ["Using claude-local adapter"], + }, + ); + + expect(rendered).toContain("Company"); + expect(rendered).toContain("https://paperclip.example/PAP/dashboard"); + expect(rendered).toContain("3 agents total (1 created, 1 updated, 1 skipped)"); + expect(rendered).toContain("3 projects total (1 created, 1 updated, 1 skipped)"); + expect(rendered).toContain("Agent results"); + expect(rendered).toContain("Project results"); + expect(rendered).toContain("Using claude-local adapter"); + expect(rendered).toContain("Review API keys"); + }); +}); + +describe("import selection catalog", () => { + it("defaults to everything and keeps project selection separate from task selection", () => { + const preview: CompanyPortabilityPreviewResult = { + include: { + company: true, + agents: true, + projects: true, + issues: true, + skills: true, + }, + targetCompanyId: "company-123", + targetCompanyName: "Imported Co", + collisionStrategy: "rename", + selectedAgentSlugs: ["ceo"], + plan: { + companyAction: "create", + agentPlans: [], + projectPlans: [], + issuePlans: [], + }, + manifest: { + schemaVersion: 1, + generatedAt: "2026-03-23T18:00:00.000Z", + source: { + companyId: "company-src", + companyName: "Source Co", + }, + includes: { + company: true, + agents: true, + projects: true, + issues: true, + skills: true, + }, + company: { + path: "COMPANY.md", + name: "Source Co", + description: null, + brandColor: null, + logoPath: "images/company-logo.png", + requireBoardApprovalForNewAgents: false, + }, + sidebar: { + agents: ["ceo"], + projects: ["alpha"], + }, + agents: [ + { + slug: "ceo", + name: "CEO", + path: "agents/ceo/AGENT.md", + skills: [], + role: "ceo", + title: null, + icon: null, + capabilities: null, + reportsToSlug: null, + adapterType: "codex_local", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + budgetMonthlyCents: 0, + metadata: null, + }, + ], + skills: [ + { + key: "skill-a", + slug: "skill-a", + name: "Skill A", + path: "skills/skill-a/SKILL.md", + description: null, + sourceType: "inline", + sourceLocator: null, + sourceRef: null, + trustLevel: null, + compatibility: null, + metadata: null, + fileInventory: [{ path: "skills/skill-a/helper.md", kind: "doc" }], + }, + ], + projects: [ + { + slug: "alpha", + name: "Alpha", + path: "projects/alpha/PROJECT.md", + description: null, + ownerAgentSlug: null, + leadAgentSlug: null, + targetDate: null, + color: null, + status: null, + executionWorkspacePolicy: null, + workspaces: [], + metadata: null, + }, + ], + issues: [ + { + slug: "kickoff", + identifier: null, + title: "Kickoff", + path: "projects/alpha/issues/kickoff/TASK.md", + projectSlug: "alpha", + projectWorkspaceKey: null, + assigneeAgentSlug: "ceo", + description: null, + recurring: false, + routine: null, + legacyRecurrence: null, + status: null, + priority: null, + labelIds: [], + billingCode: null, + executionWorkspaceSettings: null, + assigneeAdapterOverrides: null, + metadata: null, + }, + ], + envInputs: [], + }, + files: { + "COMPANY.md": "# Source Co", + "README.md": "# Readme", + ".paperclip.yaml": "schema: paperclip/v1\n", + "images/company-logo.png": { + encoding: "base64", + data: "", + contentType: "image/png", + }, + "projects/alpha/PROJECT.md": "# Alpha", + "projects/alpha/notes.md": "project notes", + "projects/alpha/issues/kickoff/TASK.md": "# Kickoff", + "projects/alpha/issues/kickoff/details.md": "task details", + "agents/ceo/AGENT.md": "# CEO", + "agents/ceo/prompt.md": "prompt", + "skills/skill-a/SKILL.md": "# Skill A", + "skills/skill-a/helper.md": "helper", + }, + envInputs: [], + warnings: [], + errors: [], + }; + + const catalog = buildImportSelectionCatalog(preview); + const state = buildDefaultImportSelectionState(catalog); + + expect(state.company).toBe(true); + expect(state.projects.has("alpha")).toBe(true); + expect(state.issues.has("kickoff")).toBe(true); + expect(state.agents.has("ceo")).toBe(true); + expect(state.skills.has("skill-a")).toBe(true); + + state.company = false; + state.issues.clear(); + state.agents.clear(); + state.skills.clear(); + + const selectedFiles = buildSelectedFilesFromImportSelection(catalog, state); + + expect(selectedFiles).toContain(".paperclip.yaml"); + expect(selectedFiles).toContain("projects/alpha/PROJECT.md"); + expect(selectedFiles).toContain("projects/alpha/notes.md"); + expect(selectedFiles).not.toContain("projects/alpha/issues/kickoff/TASK.md"); + expect(selectedFiles).not.toContain("projects/alpha/issues/kickoff/details.md"); + }); +}); + +describe("default adapter overrides", () => { + it("maps process-only imported agents to claude_local", () => { + const preview: CompanyPortabilityPreviewResult = { + include: { + company: false, + agents: true, + projects: false, + issues: false, + skills: false, + }, + targetCompanyId: null, + targetCompanyName: null, + collisionStrategy: "rename", + selectedAgentSlugs: ["legacy-agent", "explicit-agent"], + plan: { + companyAction: "none", + agentPlans: [], + projectPlans: [], + issuePlans: [], + }, + manifest: { + schemaVersion: 1, + generatedAt: "2026-03-23T18:20:00.000Z", + source: null, + includes: { + company: false, + agents: true, + projects: false, + issues: false, + skills: false, + }, + company: null, + sidebar: null, + agents: [ + { + slug: "legacy-agent", + name: "Legacy Agent", + path: "agents/legacy-agent/AGENT.md", + skills: [], + role: "agent", + title: null, + icon: null, + capabilities: null, + reportsToSlug: null, + adapterType: "process", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + budgetMonthlyCents: 0, + metadata: null, + }, + { + slug: "explicit-agent", + name: "Explicit Agent", + path: "agents/explicit-agent/AGENT.md", + skills: [], + role: "agent", + title: null, + icon: null, + capabilities: null, + reportsToSlug: null, + adapterType: "codex_local", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + budgetMonthlyCents: 0, + metadata: null, + }, + ], + skills: [], + projects: [], + issues: [], + envInputs: [], + }, + files: {}, + envInputs: [], + warnings: [], + errors: [], + }; + + expect(buildDefaultImportAdapterOverrides(preview)).toEqual({ + "legacy-agent": { + adapterType: "claude_local", + }, + }); + }); +}); diff --git a/cli/src/__tests__/helpers/zip.ts b/cli/src/__tests__/helpers/zip.ts new file mode 100644 index 0000000000..ef79b5beda --- /dev/null +++ b/cli/src/__tests__/helpers/zip.ts @@ -0,0 +1,87 @@ +function writeUint16(target: Uint8Array, offset: number, value: number) { + target[offset] = value & 0xff; + target[offset + 1] = (value >>> 8) & 0xff; +} + +function writeUint32(target: Uint8Array, offset: number, value: number) { + target[offset] = value & 0xff; + target[offset + 1] = (value >>> 8) & 0xff; + target[offset + 2] = (value >>> 16) & 0xff; + target[offset + 3] = (value >>> 24) & 0xff; +} + +function crc32(bytes: Uint8Array) { + let crc = 0xffffffff; + for (const byte of bytes) { + crc ^= byte; + for (let bit = 0; bit < 8; bit += 1) { + crc = (crc & 1) === 1 ? (crc >>> 1) ^ 0xedb88320 : crc >>> 1; + } + } + return (crc ^ 0xffffffff) >>> 0; +} + +export function createStoredZipArchive(files: Record, rootPath: string) { + const encoder = new TextEncoder(); + const localChunks: Uint8Array[] = []; + const centralChunks: Uint8Array[] = []; + let localOffset = 0; + let entryCount = 0; + + for (const [relativePath, content] of Object.entries(files).sort(([left], [right]) => left.localeCompare(right))) { + const fileName = encoder.encode(`${rootPath}/${relativePath}`); + const body = encoder.encode(content); + const checksum = crc32(body); + + const localHeader = new Uint8Array(30 + fileName.length); + writeUint32(localHeader, 0, 0x04034b50); + writeUint16(localHeader, 4, 20); + writeUint16(localHeader, 6, 0x0800); + writeUint16(localHeader, 8, 0); + writeUint32(localHeader, 14, checksum); + writeUint32(localHeader, 18, body.length); + writeUint32(localHeader, 22, body.length); + writeUint16(localHeader, 26, fileName.length); + localHeader.set(fileName, 30); + + const centralHeader = new Uint8Array(46 + fileName.length); + writeUint32(centralHeader, 0, 0x02014b50); + writeUint16(centralHeader, 4, 20); + writeUint16(centralHeader, 6, 20); + writeUint16(centralHeader, 8, 0x0800); + writeUint16(centralHeader, 10, 0); + writeUint32(centralHeader, 16, checksum); + writeUint32(centralHeader, 20, body.length); + writeUint32(centralHeader, 24, body.length); + writeUint16(centralHeader, 28, fileName.length); + writeUint32(centralHeader, 42, localOffset); + centralHeader.set(fileName, 46); + + localChunks.push(localHeader, body); + centralChunks.push(centralHeader); + localOffset += localHeader.length + body.length; + entryCount += 1; + } + + const centralDirectoryLength = centralChunks.reduce((sum, chunk) => sum + chunk.length, 0); + const archive = new Uint8Array( + localChunks.reduce((sum, chunk) => sum + chunk.length, 0) + centralDirectoryLength + 22, + ); + let offset = 0; + for (const chunk of localChunks) { + archive.set(chunk, offset); + offset += chunk.length; + } + const centralDirectoryOffset = offset; + for (const chunk of centralChunks) { + archive.set(chunk, offset); + offset += chunk.length; + } + writeUint32(archive, offset, 0x06054b50); + writeUint16(archive, offset + 8, entryCount); + writeUint16(archive, offset + 10, entryCount); + writeUint32(archive, offset + 12, centralDirectoryLength); + writeUint32(archive, offset + 16, centralDirectoryOffset); + + return archive; +} diff --git a/cli/src/__tests__/http.test.ts b/cli/src/__tests__/http.test.ts index 3681d798c1..0bacec7d64 100644 --- a/cli/src/__tests__/http.test.ts +++ b/cli/src/__tests__/http.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { ApiRequestError, PaperclipApiClient } from "../client/http.js"; +import { ApiConnectionError, ApiRequestError, PaperclipApiClient } from "../client/http.js"; describe("PaperclipApiClient", () => { afterEach(() => { @@ -58,4 +58,49 @@ describe("PaperclipApiClient", () => { details: { issueId: "1" }, } satisfies Partial); }); + + it("throws ApiConnectionError with recovery guidance when fetch fails", async () => { + const fetchMock = vi.fn().mockRejectedValue(new TypeError("fetch failed")); + vi.stubGlobal("fetch", fetchMock); + + const client = new PaperclipApiClient({ apiBase: "http://localhost:3100" }); + + await expect(client.post("/api/companies/import/preview", {})).rejects.toBeInstanceOf(ApiConnectionError); + await expect(client.post("/api/companies/import/preview", {})).rejects.toMatchObject({ + url: "http://localhost:3100/api/companies/import/preview", + method: "POST", + causeMessage: "fetch failed", + } satisfies Partial); + await expect(client.post("/api/companies/import/preview", {})).rejects.toThrow( + /Could not reach the Paperclip API\./, + ); + await expect(client.post("/api/companies/import/preview", {})).rejects.toThrow( + /curl http:\/\/localhost:3100\/api\/health/, + ); + await expect(client.post("/api/companies/import/preview", {})).rejects.toThrow( + /pnpm dev|pnpm paperclipai run/, + ); + }); + + it("retries once after interactive auth recovery", async () => { + const fetchMock = vi + .fn() + .mockResolvedValueOnce(new Response(JSON.stringify({ error: "Board access required" }), { status: 403 })) + .mockResolvedValueOnce(new Response(JSON.stringify({ ok: true }), { status: 200 })); + vi.stubGlobal("fetch", fetchMock); + + const recoverAuth = vi.fn().mockResolvedValue("board-token-123"); + const client = new PaperclipApiClient({ + apiBase: "http://localhost:3100", + recoverAuth, + }); + + const result = await client.post<{ ok: boolean }>("/api/test", { hello: "world" }); + + expect(result).toEqual({ ok: true }); + expect(recoverAuth).toHaveBeenCalledOnce(); + expect(fetchMock).toHaveBeenCalledTimes(2); + const retryHeaders = fetchMock.mock.calls[1]?.[1]?.headers as Record; + expect(retryHeaders.authorization).toBe("Bearer board-token-123"); + }); }); diff --git a/cli/src/__tests__/worktree-merge-history.test.ts b/cli/src/__tests__/worktree-merge-history.test.ts index 7a4d6b8b12..fa910872d4 100644 --- a/cli/src/__tests__/worktree-merge-history.test.ts +++ b/cli/src/__tests__/worktree-merge-history.test.ts @@ -115,6 +115,52 @@ function makeAttachment(overrides: Record = {}) { } as any; } +function makeProject(overrides: Record = {}) { + return { + id: "project-1", + companyId: "company-1", + goalId: null, + name: "Project", + description: null, + status: "in_progress", + leadAgentId: null, + targetDate: null, + color: "#22c55e", + pauseReason: null, + pausedAt: null, + executionWorkspacePolicy: null, + archivedAt: null, + createdAt: new Date("2026-03-20T00:00:00.000Z"), + updatedAt: new Date("2026-03-20T00:00:00.000Z"), + ...overrides, + } as any; +} + +function makeProjectWorkspace(overrides: Record = {}) { + return { + id: "workspace-1", + companyId: "company-1", + projectId: "project-1", + name: "Workspace", + sourceType: "local_path", + cwd: "/tmp/project", + repoUrl: "https://github.com/example/project.git", + repoRef: "main", + defaultRef: "main", + visibility: "default", + setupCommand: null, + cleanupCommand: null, + remoteProvider: null, + remoteWorkspaceRef: null, + sharedWorkspaceKey: null, + metadata: null, + isPrimary: true, + createdAt: new Date("2026-03-20T00:00:00.000Z"), + updatedAt: new Date("2026-03-20T00:00:00.000Z"), + ...overrides, + } as any; +} + describe("worktree merge history planner", () => { it("parses default scopes", () => { expect(parseWorktreeMergeScopes(undefined)).toEqual(["issues", "comments"]); @@ -236,6 +282,60 @@ describe("worktree merge history planner", () => { expect(insert.adjustments).toEqual(["clear_project_workspace"]); }); + it("plans selected project imports and preserves project workspace links", () => { + const sourceProject = makeProject({ + id: "source-project-1", + name: "Paperclip Evals", + goalId: "goal-1", + }); + const sourceWorkspace = makeProjectWorkspace({ + id: "source-workspace-1", + projectId: "source-project-1", + cwd: "/Users/dotta/paperclip-evals", + repoUrl: "https://github.com/paperclipai/paperclip-evals.git", + }); + + const plan = buildWorktreeMergePlan({ + companyId: "company-1", + companyName: "Paperclip", + issuePrefix: "PAP", + previewIssueCounterStart: 10, + scopes: ["issues"], + sourceIssues: [ + makeIssue({ + id: "issue-project-import", + identifier: "PAP-88", + projectId: "source-project-1", + projectWorkspaceId: "source-workspace-1", + }), + ], + targetIssues: [], + sourceComments: [], + targetComments: [], + sourceProjects: [sourceProject], + sourceProjectWorkspaces: [sourceWorkspace], + targetAgents: [], + targetProjects: [], + targetProjectWorkspaces: [], + targetGoals: [{ id: "goal-1" }] as any, + importProjectIds: ["source-project-1"], + }); + + expect(plan.counts.projectsToImport).toBe(1); + expect(plan.projectImports[0]).toMatchObject({ + source: { id: "source-project-1", name: "Paperclip Evals" }, + targetGoalId: "goal-1", + workspaces: [{ id: "source-workspace-1" }], + }); + + const insert = plan.issuePlans[0] as any; + expect(insert.targetProjectId).toBe("source-project-1"); + expect(insert.targetProjectWorkspaceId).toBe("source-workspace-1"); + expect(insert.projectResolution).toBe("imported"); + expect(insert.mappedProjectName).toBe("Paperclip Evals"); + expect(insert.adjustments).toEqual([]); + }); + it("imports comments onto shared or newly imported issues while skipping existing comments", () => { const sharedIssue = makeIssue({ id: "issue-a", identifier: "PAP-10" }); const newIssue = makeIssue({ diff --git a/cli/src/client/board-auth.ts b/cli/src/client/board-auth.ts new file mode 100644 index 0000000000..7c1121ec8b --- /dev/null +++ b/cli/src/client/board-auth.ts @@ -0,0 +1,282 @@ +import { spawn } from "node:child_process"; +import fs from "node:fs"; +import path from "node:path"; +import pc from "picocolors"; +import { buildCliCommandLabel } from "./command-label.js"; +import { resolveDefaultCliAuthPath } from "../config/home.js"; + +type RequestedAccess = "board" | "instance_admin_required"; + +interface BoardAuthCredential { + apiBase: string; + token: string; + createdAt: string; + updatedAt: string; + userId?: string | null; +} + +interface BoardAuthStore { + version: 1; + credentials: Record; +} + +interface CreateChallengeResponse { + id: string; + token: string; + boardApiToken: string; + approvalPath: string; + approvalUrl: string | null; + pollPath: string; + expiresAt: string; + suggestedPollIntervalMs: number; +} + +interface ChallengeStatusResponse { + id: string; + status: "pending" | "approved" | "cancelled" | "expired"; + command: string; + clientName: string | null; + requestedAccess: RequestedAccess; + requestedCompanyId: string | null; + requestedCompanyName: string | null; + approvedAt: string | null; + cancelledAt: string | null; + expiresAt: string; + approvedByUser: { id: string; name: string; email: string } | null; +} + +function defaultBoardAuthStore(): BoardAuthStore { + return { + version: 1, + credentials: {}, + }; +} + +function toStringOrNull(value: unknown): string | null { + return typeof value === "string" && value.trim().length > 0 ? value.trim() : null; +} + +function normalizeApiBase(apiBase: string): string { + return apiBase.trim().replace(/\/+$/, ""); +} + +export function resolveBoardAuthStorePath(overridePath?: string): string { + if (overridePath?.trim()) return path.resolve(overridePath.trim()); + if (process.env.PAPERCLIP_AUTH_STORE?.trim()) return path.resolve(process.env.PAPERCLIP_AUTH_STORE.trim()); + return resolveDefaultCliAuthPath(); +} + +export function readBoardAuthStore(storePath?: string): BoardAuthStore { + const filePath = resolveBoardAuthStorePath(storePath); + if (!fs.existsSync(filePath)) return defaultBoardAuthStore(); + + const raw = JSON.parse(fs.readFileSync(filePath, "utf8")) as Partial | null; + const credentials = raw?.credentials && typeof raw.credentials === "object" ? raw.credentials : {}; + const normalized: Record = {}; + + for (const [key, value] of Object.entries(credentials)) { + if (typeof value !== "object" || value === null) continue; + const record = value as unknown as Record; + const apiBase = toStringOrNull(record.apiBase); + const token = toStringOrNull(record.token); + const createdAt = toStringOrNull(record.createdAt); + const updatedAt = toStringOrNull(record.updatedAt); + if (!apiBase || !token || !createdAt || !updatedAt) continue; + normalized[normalizeApiBase(key)] = { + apiBase, + token, + createdAt, + updatedAt, + userId: toStringOrNull(record.userId), + }; + } + + return { + version: 1, + credentials: normalized, + }; +} + +export function writeBoardAuthStore(store: BoardAuthStore, storePath?: string): void { + const filePath = resolveBoardAuthStorePath(storePath); + fs.mkdirSync(path.dirname(filePath), { recursive: true }); + fs.writeFileSync(filePath, `${JSON.stringify(store, null, 2)}\n`, { mode: 0o600 }); +} + +export function getStoredBoardCredential(apiBase: string, storePath?: string): BoardAuthCredential | null { + const store = readBoardAuthStore(storePath); + return store.credentials[normalizeApiBase(apiBase)] ?? null; +} + +export function setStoredBoardCredential(input: { + apiBase: string; + token: string; + userId?: string | null; + storePath?: string; +}): BoardAuthCredential { + const normalizedApiBase = normalizeApiBase(input.apiBase); + const store = readBoardAuthStore(input.storePath); + const now = new Date().toISOString(); + const existing = store.credentials[normalizedApiBase]; + const credential: BoardAuthCredential = { + apiBase: normalizedApiBase, + token: input.token.trim(), + createdAt: existing?.createdAt ?? now, + updatedAt: now, + userId: input.userId ?? existing?.userId ?? null, + }; + store.credentials[normalizedApiBase] = credential; + writeBoardAuthStore(store, input.storePath); + return credential; +} + +export function removeStoredBoardCredential(apiBase: string, storePath?: string): boolean { + const normalizedApiBase = normalizeApiBase(apiBase); + const store = readBoardAuthStore(storePath); + if (!store.credentials[normalizedApiBase]) return false; + delete store.credentials[normalizedApiBase]; + writeBoardAuthStore(store, storePath); + return true; +} + +function sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +async function requestJson(url: string, init?: RequestInit): Promise { + const headers = new Headers(init?.headers ?? undefined); + if (init?.body !== undefined && !headers.has("content-type")) { + headers.set("content-type", "application/json"); + } + if (!headers.has("accept")) { + headers.set("accept", "application/json"); + } + + const response = await fetch(url, { + ...init, + headers, + }); + + if (!response.ok) { + const body = await response.json().catch(() => null); + const message = + body && typeof body === "object" && typeof (body as { error?: unknown }).error === "string" + ? (body as { error: string }).error + : `Request failed: ${response.status}`; + throw new Error(message); + } + + return response.json() as Promise; +} + +export function openUrl(url: string): boolean { + const platform = process.platform; + try { + if (platform === "darwin") { + const child = spawn("open", [url], { detached: true, stdio: "ignore" }); + child.unref(); + return true; + } + if (platform === "win32") { + const child = spawn("cmd", ["/c", "start", "", url], { detached: true, stdio: "ignore" }); + child.unref(); + return true; + } + const child = spawn("xdg-open", [url], { detached: true, stdio: "ignore" }); + child.unref(); + return true; + } catch { + return false; + } +} + +export async function loginBoardCli(params: { + apiBase: string; + requestedAccess: RequestedAccess; + requestedCompanyId?: string | null; + clientName?: string | null; + command?: string; + storePath?: string; + print?: boolean; +}): Promise<{ token: string; approvalUrl: string; userId?: string | null }> { + const apiBase = normalizeApiBase(params.apiBase); + const createUrl = `${apiBase}/api/cli-auth/challenges`; + const command = params.command?.trim() || buildCliCommandLabel(); + + const challenge = await requestJson(createUrl, { + method: "POST", + body: JSON.stringify({ + command, + clientName: params.clientName?.trim() || "paperclipai cli", + requestedAccess: params.requestedAccess, + requestedCompanyId: params.requestedCompanyId?.trim() || null, + }), + }); + + const approvalUrl = challenge.approvalUrl ?? `${apiBase}${challenge.approvalPath}`; + if (params.print !== false) { + console.error(pc.bold("Board authentication required")); + console.error(`Open this URL in your browser to approve CLI access:\n${approvalUrl}`); + } + + const opened = openUrl(approvalUrl); + if (params.print !== false && opened) { + console.error(pc.dim("Opened the approval page in your browser.")); + } + + const expiresAtMs = Date.parse(challenge.expiresAt); + const pollMs = Math.max(500, challenge.suggestedPollIntervalMs || 1000); + + while (Number.isFinite(expiresAtMs) ? Date.now() < expiresAtMs : true) { + const status = await requestJson( + `${apiBase}/api${challenge.pollPath}?token=${encodeURIComponent(challenge.token)}`, + ); + + if (status.status === "approved") { + const me = await requestJson<{ userId: string; user?: { id: string } | null }>( + `${apiBase}/api/cli-auth/me`, + { + headers: { + authorization: `Bearer ${challenge.boardApiToken}`, + }, + }, + ); + setStoredBoardCredential({ + apiBase, + token: challenge.boardApiToken, + userId: me.userId ?? me.user?.id ?? null, + storePath: params.storePath, + }); + return { + token: challenge.boardApiToken, + approvalUrl, + userId: me.userId ?? me.user?.id ?? null, + }; + } + + if (status.status === "cancelled") { + throw new Error("CLI auth challenge was cancelled."); + } + if (status.status === "expired") { + throw new Error("CLI auth challenge expired before approval."); + } + + await sleep(pollMs); + } + + throw new Error("CLI auth challenge expired before approval."); +} + +export async function revokeStoredBoardCredential(params: { + apiBase: string; + token: string; +}): Promise { + const apiBase = normalizeApiBase(params.apiBase); + await requestJson<{ revoked: boolean }>(`${apiBase}/api/cli-auth/revoke-current`, { + method: "POST", + headers: { + authorization: `Bearer ${params.token}`, + }, + body: JSON.stringify({}), + }); +} diff --git a/cli/src/client/command-label.ts b/cli/src/client/command-label.ts new file mode 100644 index 0000000000..21143b3b75 --- /dev/null +++ b/cli/src/client/command-label.ts @@ -0,0 +1,4 @@ +export function buildCliCommandLabel(): string { + const args = process.argv.slice(2); + return args.length > 0 ? `paperclipai ${args.join(" ")}` : "paperclipai"; +} diff --git a/cli/src/client/http.ts b/cli/src/client/http.ts index 60be8d2d53..27de5eb104 100644 --- a/cli/src/client/http.ts +++ b/cli/src/client/http.ts @@ -13,25 +13,54 @@ export class ApiRequestError extends Error { } } +export class ApiConnectionError extends Error { + url: string; + method: string; + causeMessage?: string; + + constructor(input: { + apiBase: string; + path: string; + method: string; + cause?: unknown; + }) { + const url = buildUrl(input.apiBase, input.path); + const causeMessage = formatConnectionCause(input.cause); + super(buildConnectionErrorMessage({ apiBase: input.apiBase, url, method: input.method, causeMessage })); + this.url = url; + this.method = input.method; + this.causeMessage = causeMessage; + } +} + interface RequestOptions { ignoreNotFound?: boolean; } +interface RecoverAuthInput { + path: string; + method: string; + error: ApiRequestError; +} + interface ApiClientOptions { apiBase: string; apiKey?: string; runId?: string; + recoverAuth?: (input: RecoverAuthInput) => Promise; } export class PaperclipApiClient { readonly apiBase: string; - readonly apiKey?: string; + apiKey?: string; readonly runId?: string; + readonly recoverAuth?: (input: RecoverAuthInput) => Promise; constructor(opts: ApiClientOptions) { this.apiBase = opts.apiBase.replace(/\/+$/, ""); this.apiKey = opts.apiKey?.trim() || undefined; this.runId = opts.runId?.trim() || undefined; + this.recoverAuth = opts.recoverAuth; } get(path: string, opts?: RequestOptions): Promise { @@ -56,8 +85,18 @@ export class PaperclipApiClient { return this.request(path, { method: "DELETE" }, opts); } - private async request(path: string, init: RequestInit, opts?: RequestOptions): Promise { + setApiKey(apiKey: string | undefined) { + this.apiKey = apiKey?.trim() || undefined; + } + + private async request( + path: string, + init: RequestInit, + opts?: RequestOptions, + hasRetriedAuth = false, + ): Promise { const url = buildUrl(this.apiBase, path); + const method = String(init.method ?? "GET").toUpperCase(); const headers: Record = { accept: "application/json", @@ -76,17 +115,39 @@ export class PaperclipApiClient { headers["x-paperclip-run-id"] = this.runId; } - const response = await fetch(url, { - ...init, - headers, - }); + let response: Response; + try { + response = await fetch(url, { + ...init, + headers, + }); + } catch (error) { + throw new ApiConnectionError({ + apiBase: this.apiBase, + path, + method, + cause: error, + }); + } if (opts?.ignoreNotFound && response.status === 404) { return null; } if (!response.ok) { - throw await toApiError(response); + const apiError = await toApiError(response); + if (!hasRetriedAuth && this.recoverAuth) { + const recoveredToken = await this.recoverAuth({ + path, + method, + error: apiError, + }); + if (recoveredToken) { + this.setApiKey(recoveredToken); + return this.request(path, init, opts, true); + } + } + throw apiError; } if (response.status === 204) { @@ -136,6 +197,50 @@ async function toApiError(response: Response): Promise { return new ApiRequestError(response.status, `Request failed with status ${response.status}`, undefined, parsed); } +function buildConnectionErrorMessage(input: { + apiBase: string; + url: string; + method: string; + causeMessage?: string; +}): string { + const healthUrl = buildHealthCheckUrl(input.url); + const lines = [ + "Could not reach the Paperclip API.", + "", + `Request: ${input.method} ${input.url}`, + ]; + if (input.causeMessage) { + lines.push(`Cause: ${input.causeMessage}`); + } + lines.push( + "", + "This usually means the Paperclip server is not running, the configured URL is wrong, or the request is being blocked before it reaches Paperclip.", + "", + "Try:", + "- Start Paperclip with `pnpm dev` or `pnpm paperclipai run`.", + `- Verify the server is reachable with \`curl ${healthUrl}\`.`, + `- If Paperclip is running elsewhere, pass \`--api-base ${input.apiBase.replace(/\/+$/, "")}\` or set \`PAPERCLIP_API_URL\`.`, + ); + return lines.join("\n"); +} + +function buildHealthCheckUrl(requestUrl: string): string { + const url = new URL(requestUrl); + url.pathname = `${url.pathname.replace(/\/+$/, "").replace(/\/api(?:\/.*)?$/, "")}/api/health`; + url.search = ""; + url.hash = ""; + return url.toString(); +} + +function formatConnectionCause(error: unknown): string | undefined { + if (!error) return undefined; + if (error instanceof Error) { + return error.message.trim() || error.name; + } + const message = String(error).trim(); + return message || undefined; +} + function toStringRecord(headers: HeadersInit | undefined): Record { if (!headers) return {}; if (Array.isArray(headers)) { diff --git a/cli/src/commands/client/auth.ts b/cli/src/commands/client/auth.ts new file mode 100644 index 0000000000..65f47610eb --- /dev/null +++ b/cli/src/commands/client/auth.ts @@ -0,0 +1,113 @@ +import type { Command } from "commander"; +import { + getStoredBoardCredential, + loginBoardCli, + removeStoredBoardCredential, + revokeStoredBoardCredential, +} from "../../client/board-auth.js"; +import { + addCommonClientOptions, + handleCommandError, + printOutput, + resolveCommandContext, + type BaseClientOptions, +} from "./common.js"; + +interface AuthLoginOptions extends BaseClientOptions { + instanceAdmin?: boolean; +} + +interface AuthLogoutOptions extends BaseClientOptions {} +interface AuthWhoamiOptions extends BaseClientOptions {} + +export function registerClientAuthCommands(auth: Command): void { + addCommonClientOptions( + auth + .command("login") + .description("Authenticate the CLI for board-user access") + .option("--instance-admin", "Request instance-admin approval instead of plain board access", false) + .action(async (opts: AuthLoginOptions) => { + try { + const ctx = resolveCommandContext(opts); + const login = await loginBoardCli({ + apiBase: ctx.api.apiBase, + requestedAccess: opts.instanceAdmin ? "instance_admin_required" : "board", + requestedCompanyId: ctx.companyId ?? null, + command: "paperclipai auth login", + }); + printOutput( + { + ok: true, + apiBase: ctx.api.apiBase, + userId: login.userId ?? null, + approvalUrl: login.approvalUrl, + }, + { json: ctx.json }, + ); + } catch (err) { + handleCommandError(err); + } + }), + { includeCompany: true }, + ); + + addCommonClientOptions( + auth + .command("logout") + .description("Remove the stored board-user credential for this API base") + .action(async (opts: AuthLogoutOptions) => { + try { + const ctx = resolveCommandContext(opts); + const credential = getStoredBoardCredential(ctx.api.apiBase); + if (!credential) { + printOutput({ ok: true, apiBase: ctx.api.apiBase, revoked: false, removedLocalCredential: false }, { json: ctx.json }); + return; + } + let revoked = false; + try { + await revokeStoredBoardCredential({ + apiBase: ctx.api.apiBase, + token: credential.token, + }); + revoked = true; + } catch { + // Remove the local credential even if the server-side revoke fails. + } + const removedLocalCredential = removeStoredBoardCredential(ctx.api.apiBase); + printOutput( + { + ok: true, + apiBase: ctx.api.apiBase, + revoked, + removedLocalCredential, + }, + { json: ctx.json }, + ); + } catch (err) { + handleCommandError(err); + } + }), + ); + + addCommonClientOptions( + auth + .command("whoami") + .description("Show the current board-user identity for this API base") + .action(async (opts: AuthWhoamiOptions) => { + try { + const ctx = resolveCommandContext(opts); + const me = await ctx.api.get<{ + user: { id: string; name: string; email: string } | null; + userId: string; + isInstanceAdmin: boolean; + companyIds: string[]; + source: string; + keyId: string | null; + }>("/api/cli-auth/me"); + printOutput(me, { json: ctx.json }); + } catch (err) { + handleCommandError(err); + } + }), + ); +} diff --git a/cli/src/commands/client/common.ts b/cli/src/commands/client/common.ts index 14de3ccf8f..db5f7dbcd3 100644 --- a/cli/src/commands/client/common.ts +++ b/cli/src/commands/client/common.ts @@ -1,5 +1,7 @@ import pc from "picocolors"; import type { Command } from "commander"; +import { getStoredBoardCredential, loginBoardCli } from "../../client/board-auth.js"; +import { buildCliCommandLabel } from "../../client/command-label.js"; import { readConfig } from "../../config/store.js"; import { readContext, resolveProfile, type ClientContextProfile } from "../../client/context.js"; import { ApiRequestError, PaperclipApiClient } from "../../client/http.js"; @@ -53,10 +55,12 @@ export function resolveCommandContext( profile.apiBase || inferApiBaseFromConfig(options.config); - const apiKey = + const explicitApiKey = options.apiKey?.trim() || process.env.PAPERCLIP_API_KEY?.trim() || readKeyFromProfileEnv(profile); + const storedBoardCredential = explicitApiKey ? null : getStoredBoardCredential(apiBase); + const apiKey = explicitApiKey || storedBoardCredential?.token; const companyId = options.companyId?.trim() || @@ -69,7 +73,27 @@ export function resolveCommandContext( ); } - const api = new PaperclipApiClient({ apiBase, apiKey }); + const api = new PaperclipApiClient({ + apiBase, + apiKey, + recoverAuth: explicitApiKey || !canAttemptInteractiveBoardAuth() + ? undefined + : async ({ error }) => { + const requestedAccess = error.message.includes("Instance admin required") + ? "instance_admin_required" + : "board"; + if (!shouldRecoverBoardAuth(error)) { + return null; + } + const login = await loginBoardCli({ + apiBase, + requestedAccess, + requestedCompanyId: companyId ?? null, + command: buildCliCommandLabel(), + }); + return login.token; + }, + }); return { api, companyId, @@ -79,6 +103,16 @@ export function resolveCommandContext( }; } +function shouldRecoverBoardAuth(error: ApiRequestError): boolean { + if (error.status === 401) return true; + if (error.status !== 403) return false; + return error.message.includes("Board access required") || error.message.includes("Instance admin required"); +} + +function canAttemptInteractiveBoardAuth(): boolean { + return Boolean(process.stdin.isTTY && process.stdout.isTTY); +} + export function printOutput(data: unknown, opts: { json?: boolean; label?: string } = {}): void { if (opts.json) { console.log(JSON.stringify(data, null, 2)); diff --git a/cli/src/commands/client/company.ts b/cli/src/commands/client/company.ts index 01de45486c..ac4fdc1c5f 100644 --- a/cli/src/commands/client/company.ts +++ b/cli/src/commands/client/company.ts @@ -2,6 +2,7 @@ import { Command } from "commander"; import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises"; import path from "node:path"; import * as p from "@clack/prompts"; +import pc from "picocolors"; import type { Company, CompanyPortabilityFileEntry, @@ -11,6 +12,8 @@ import type { CompanyPortabilityImportResult, } from "@paperclipai/shared"; import { ApiRequestError } from "../../client/http.js"; +import { openUrl } from "../../client/board-auth.js"; +import { binaryContentTypeByExtension, readZipArchive } from "./zip.js"; import { addCommonClientOptions, formatInlineRecord, @@ -42,23 +45,68 @@ interface CompanyExportOptions extends BaseClientOptions { } interface CompanyImportOptions extends BaseClientOptions { - from?: string; include?: string; target?: CompanyImportTargetMode; companyId?: string; newCompanyName?: string; agents?: string; collision?: CompanyCollisionMode; + ref?: string; + paperclipUrl?: string; + yes?: boolean; dryRun?: boolean; } -const binaryContentTypeByExtension: Record = { - ".gif": "image/gif", - ".jpeg": "image/jpeg", - ".jpg": "image/jpeg", - ".png": "image/png", - ".svg": "image/svg+xml", - ".webp": "image/webp", +const DEFAULT_EXPORT_INCLUDE: CompanyPortabilityInclude = { + company: true, + agents: true, + projects: false, + issues: false, + skills: false, +}; + +const DEFAULT_IMPORT_INCLUDE: CompanyPortabilityInclude = { + company: true, + agents: true, + projects: true, + issues: true, + skills: true, +}; + +const IMPORT_INCLUDE_OPTIONS: Array<{ + value: keyof CompanyPortabilityInclude; + label: string; + hint: string; +}> = [ + { value: "company", label: "Company", hint: "name, branding, and company settings" }, + { value: "projects", label: "Projects", hint: "projects and workspace metadata" }, + { value: "issues", label: "Tasks", hint: "tasks and recurring routines" }, + { value: "agents", label: "Agents", hint: "agent records and org structure" }, + { value: "skills", label: "Skills", hint: "company skill packages and references" }, +]; + +const IMPORT_PREVIEW_SAMPLE_LIMIT = 6; + +type ImportSelectableGroup = "projects" | "issues" | "agents" | "skills"; + +type ImportSelectionCatalog = { + company: { + includedByDefault: boolean; + files: string[]; + }; + projects: Array<{ key: string; label: string; hint?: string; files: string[] }>; + issues: Array<{ key: string; label: string; hint?: string; files: string[] }>; + agents: Array<{ key: string; label: string; hint?: string; files: string[] }>; + skills: Array<{ key: string; label: string; hint?: string; files: string[] }>; + extensionPath: string | null; +}; + +type ImportSelectionState = { + company: boolean; + projects: Set; + issues: Set; + agents: Set; + skills: Set; }; function readPortableFileEntry(filePath: string, contents: Buffer): CompanyPortabilityFileEntry { @@ -84,8 +132,11 @@ function normalizeSelector(input: string): string { return input.trim(); } -function parseInclude(input: string | undefined): CompanyPortabilityInclude { - if (!input || !input.trim()) return { company: true, agents: true, projects: false, issues: false, skills: false }; +function parseInclude( + input: string | undefined, + fallback: CompanyPortabilityInclude = DEFAULT_EXPORT_INCLUDE, +): CompanyPortabilityInclude { + if (!input || !input.trim()) return { ...fallback }; const values = input.split(",").map((part) => part.trim().toLowerCase()).filter(Boolean); const include = { company: values.includes("company"), @@ -114,6 +165,602 @@ function parseCsvValues(input: string | undefined): string[] { return Array.from(new Set(input.split(",").map((part) => part.trim()).filter(Boolean))); } +function isInteractiveTerminal(): boolean { + return Boolean(process.stdin.isTTY && process.stdout.isTTY); +} + +function resolveImportInclude(input: string | undefined): CompanyPortabilityInclude { + return parseInclude(input, DEFAULT_IMPORT_INCLUDE); +} + +function normalizePortablePath(filePath: string): string { + return filePath.replace(/\\/g, "/"); +} + +function shouldIncludePortableFile(filePath: string): boolean { + const baseName = path.basename(filePath); + const isMarkdown = baseName.endsWith(".md"); + const isPaperclipYaml = baseName === ".paperclip.yaml" || baseName === ".paperclip.yml"; + const contentType = binaryContentTypeByExtension[path.extname(baseName).toLowerCase()]; + return isMarkdown || isPaperclipYaml || Boolean(contentType); +} + +function findPortableExtensionPath(files: Record): string | null { + if (files[".paperclip.yaml"] !== undefined) return ".paperclip.yaml"; + if (files[".paperclip.yml"] !== undefined) return ".paperclip.yml"; + return Object.keys(files).find((entry) => entry.endsWith("/.paperclip.yaml") || entry.endsWith("/.paperclip.yml")) ?? null; +} + +function collectFilesUnderDirectory( + files: Record, + directory: string, + opts?: { excludePrefixes?: string[] }, +): string[] { + const normalizedDirectory = normalizePortablePath(directory).replace(/\/+$/, ""); + if (!normalizedDirectory) return []; + const prefix = `${normalizedDirectory}/`; + const excluded = (opts?.excludePrefixes ?? []).map((entry) => normalizePortablePath(entry).replace(/\/+$/, "")).filter(Boolean); + return Object.keys(files) + .map(normalizePortablePath) + .filter((filePath) => filePath.startsWith(prefix)) + .filter((filePath) => !excluded.some((excludePrefix) => filePath.startsWith(`${excludePrefix}/`))) + .sort((left, right) => left.localeCompare(right)); +} + +function collectEntityFiles( + files: Record, + entryPath: string, + opts?: { excludePrefixes?: string[] }, +): string[] { + const normalizedPath = normalizePortablePath(entryPath); + const directory = normalizedPath.includes("/") ? normalizedPath.slice(0, normalizedPath.lastIndexOf("/")) : ""; + const selected = new Set([normalizedPath]); + if (directory) { + for (const filePath of collectFilesUnderDirectory(files, directory, opts)) { + selected.add(filePath); + } + } + return Array.from(selected).sort((left, right) => left.localeCompare(right)); +} + +export function buildImportSelectionCatalog(preview: CompanyPortabilityPreviewResult): ImportSelectionCatalog { + const selectedAgentSlugs = new Set(preview.selectedAgentSlugs); + const companyFiles = new Set(); + const companyPath = preview.manifest.company?.path ? normalizePortablePath(preview.manifest.company.path) : null; + if (companyPath) { + companyFiles.add(companyPath); + } + const readmePath = Object.keys(preview.files).find((entry) => normalizePortablePath(entry) === "README.md"); + if (readmePath) { + companyFiles.add(normalizePortablePath(readmePath)); + } + const logoPath = preview.manifest.company?.logoPath ? normalizePortablePath(preview.manifest.company.logoPath) : null; + if (logoPath && preview.files[logoPath] !== undefined) { + companyFiles.add(logoPath); + } + + return { + company: { + includedByDefault: preview.include.company && preview.manifest.company !== null, + files: Array.from(companyFiles).sort((left, right) => left.localeCompare(right)), + }, + projects: preview.manifest.projects.map((project) => { + const projectPath = normalizePortablePath(project.path); + const projectDir = projectPath.includes("/") ? projectPath.slice(0, projectPath.lastIndexOf("/")) : ""; + return { + key: project.slug, + label: project.name, + hint: project.slug, + files: collectEntityFiles(preview.files, projectPath, { + excludePrefixes: projectDir ? [`${projectDir}/issues`] : [], + }), + }; + }), + issues: preview.manifest.issues.map((issue) => ({ + key: issue.slug, + label: issue.title, + hint: issue.identifier ?? issue.slug, + files: collectEntityFiles(preview.files, normalizePortablePath(issue.path)), + })), + agents: preview.manifest.agents + .filter((agent) => selectedAgentSlugs.size === 0 || selectedAgentSlugs.has(agent.slug)) + .map((agent) => ({ + key: agent.slug, + label: agent.name, + hint: agent.slug, + files: collectEntityFiles(preview.files, normalizePortablePath(agent.path)), + })), + skills: preview.manifest.skills.map((skill) => ({ + key: skill.slug, + label: skill.name, + hint: skill.slug, + files: collectEntityFiles(preview.files, normalizePortablePath(skill.path)), + })), + extensionPath: findPortableExtensionPath(preview.files), + }; +} + +function toKeySet(items: Array<{ key: string }>): Set { + return new Set(items.map((item) => item.key)); +} + +export function buildDefaultImportSelectionState(catalog: ImportSelectionCatalog): ImportSelectionState { + return { + company: catalog.company.includedByDefault, + projects: toKeySet(catalog.projects), + issues: toKeySet(catalog.issues), + agents: toKeySet(catalog.agents), + skills: toKeySet(catalog.skills), + }; +} + +function countSelected(state: ImportSelectionState, group: ImportSelectableGroup): number { + return state[group].size; +} + +function countTotal(catalog: ImportSelectionCatalog, group: ImportSelectableGroup): number { + return catalog[group].length; +} + +function summarizeGroupSelection(catalog: ImportSelectionCatalog, state: ImportSelectionState, group: ImportSelectableGroup): string { + return `${countSelected(state, group)}/${countTotal(catalog, group)} selected`; +} + +function getGroupLabel(group: ImportSelectableGroup): string { + switch (group) { + case "projects": + return "Projects"; + case "issues": + return "Tasks"; + case "agents": + return "Agents"; + case "skills": + return "Skills"; + } +} + +export function buildSelectedFilesFromImportSelection( + catalog: ImportSelectionCatalog, + state: ImportSelectionState, +): string[] { + const selected = new Set(); + + if (state.company) { + for (const filePath of catalog.company.files) { + selected.add(normalizePortablePath(filePath)); + } + } + + for (const group of ["projects", "issues", "agents", "skills"] as const) { + const selectedKeys = state[group]; + for (const item of catalog[group]) { + if (!selectedKeys.has(item.key)) continue; + for (const filePath of item.files) { + selected.add(normalizePortablePath(filePath)); + } + } + } + + if (selected.size > 0 && catalog.extensionPath) { + selected.add(normalizePortablePath(catalog.extensionPath)); + } + + return Array.from(selected).sort((left, right) => left.localeCompare(right)); +} + +export function buildDefaultImportAdapterOverrides( + preview: Pick, +): Record | undefined { + const selectedAgentSlugs = new Set(preview.selectedAgentSlugs); + const overrides = Object.fromEntries( + preview.manifest.agents + .filter((agent) => selectedAgentSlugs.size === 0 || selectedAgentSlugs.has(agent.slug)) + .filter((agent) => agent.adapterType === "process") + .map((agent) => [ + agent.slug, + { + // TODO: replace this temporary claude_local fallback with adapter selection in the import TUI. + adapterType: "claude_local", + }, + ]), + ); + return Object.keys(overrides).length > 0 ? overrides : undefined; +} + +function buildDefaultImportAdapterMessages( + overrides: Record | undefined, +): string[] { + if (!overrides) return []; + const adapterTypes = Array.from(new Set(Object.values(overrides).map((override) => override.adapterType))) + .map((adapterType) => adapterType.replace(/_/g, "-")); + const agentCount = Object.keys(overrides).length; + return [ + `Using ${adapterTypes.join(", ")} adapter${adapterTypes.length === 1 ? "" : "s"} for ${agentCount} imported ${pluralize(agentCount, "agent")} without an explicit adapter.`, + ]; +} + +async function promptForImportSelection(preview: CompanyPortabilityPreviewResult): Promise { + const catalog = buildImportSelectionCatalog(preview); + const state = buildDefaultImportSelectionState(catalog); + + while (true) { + const choice = await p.select({ + message: "Select what Paperclip should import", + options: [ + { + value: "company", + label: state.company ? "Company: included" : "Company: skipped", + hint: catalog.company.files.length > 0 ? "toggle company metadata" : "no company metadata in package", + }, + { + value: "projects", + label: "Select Projects", + hint: summarizeGroupSelection(catalog, state, "projects"), + }, + { + value: "issues", + label: "Select Tasks", + hint: summarizeGroupSelection(catalog, state, "issues"), + }, + { + value: "agents", + label: "Select Agents", + hint: summarizeGroupSelection(catalog, state, "agents"), + }, + { + value: "skills", + label: "Select Skills", + hint: summarizeGroupSelection(catalog, state, "skills"), + }, + { + value: "confirm", + label: "Confirm", + hint: `${buildSelectedFilesFromImportSelection(catalog, state).length} files selected`, + }, + ], + initialValue: "confirm", + }); + + if (p.isCancel(choice)) { + p.cancel("Import cancelled."); + process.exit(0); + } + + if (choice === "confirm") { + const selectedFiles = buildSelectedFilesFromImportSelection(catalog, state); + if (selectedFiles.length === 0) { + p.note("Select at least one import target before confirming.", "Nothing selected"); + continue; + } + return selectedFiles; + } + + if (choice === "company") { + if (catalog.company.files.length === 0) { + p.note("This package does not include company metadata to toggle.", "No company metadata"); + continue; + } + state.company = !state.company; + continue; + } + + const group = choice; + const groupItems = catalog[group]; + if (groupItems.length === 0) { + p.note(`This package does not include any ${getGroupLabel(group).toLowerCase()}.`, `No ${getGroupLabel(group)}`); + continue; + } + + const selection = await p.multiselect({ + message: `${getGroupLabel(group)} to import. Space toggles, enter returns to the main menu.`, + options: groupItems.map((item) => ({ + value: item.key, + label: item.label, + hint: item.hint, + })), + initialValues: Array.from(state[group]), + }); + + if (p.isCancel(selection)) { + p.cancel("Import cancelled."); + process.exit(0); + } + + state[group] = new Set(selection); + } +} + +function summarizeInclude(include: CompanyPortabilityInclude): string { + const labels = IMPORT_INCLUDE_OPTIONS + .filter((option) => include[option.value]) + .map((option) => option.label.toLowerCase()); + return labels.length > 0 ? labels.join(", ") : "nothing selected"; +} + +function formatSourceLabel(source: { type: "inline"; rootPath?: string | null } | { type: "github"; url: string }): string { + if (source.type === "github") { + return `GitHub: ${source.url}`; + } + return `Local package: ${source.rootPath?.trim() || "(current folder)"}`; +} + +function formatTargetLabel( + target: { mode: "existing_company"; companyId?: string | null } | { mode: "new_company"; newCompanyName?: string | null }, + preview?: CompanyPortabilityPreviewResult, +): string { + if (target.mode === "existing_company") { + const targetName = preview?.targetCompanyName?.trim(); + const targetId = preview?.targetCompanyId?.trim() || target.companyId?.trim() || "unknown-company"; + return targetName ? `${targetName} (${targetId})` : targetId; + } + return target.newCompanyName?.trim() || preview?.manifest.company?.name || "new company"; +} + +function pluralize(count: number, singular: string, plural = `${singular}s`): string { + return count === 1 ? singular : plural; +} + +function summarizePlanCounts( + plans: Array<{ action: "create" | "update" | "skip" }>, + noun: string, +): string { + if (plans.length === 0) return `0 ${pluralize(0, noun)} selected`; + const createCount = plans.filter((plan) => plan.action === "create").length; + const updateCount = plans.filter((plan) => plan.action === "update").length; + const skipCount = plans.filter((plan) => plan.action === "skip").length; + const parts: string[] = []; + if (createCount > 0) parts.push(`${createCount} create`); + if (updateCount > 0) parts.push(`${updateCount} update`); + if (skipCount > 0) parts.push(`${skipCount} skip`); + return `${plans.length} ${pluralize(plans.length, noun)} total (${parts.join(", ")})`; +} + +function summarizeImportAgentResults(agents: CompanyPortabilityImportResult["agents"]): string { + if (agents.length === 0) return "0 agents changed"; + const created = agents.filter((agent) => agent.action === "created").length; + const updated = agents.filter((agent) => agent.action === "updated").length; + const skipped = agents.filter((agent) => agent.action === "skipped").length; + const parts: string[] = []; + if (created > 0) parts.push(`${created} created`); + if (updated > 0) parts.push(`${updated} updated`); + if (skipped > 0) parts.push(`${skipped} skipped`); + return `${agents.length} ${pluralize(agents.length, "agent")} total (${parts.join(", ")})`; +} + +function summarizeImportProjectResults(projects: CompanyPortabilityImportResult["projects"]): string { + if (projects.length === 0) return "0 projects changed"; + const created = projects.filter((project) => project.action === "created").length; + const updated = projects.filter((project) => project.action === "updated").length; + const skipped = projects.filter((project) => project.action === "skipped").length; + const parts: string[] = []; + if (created > 0) parts.push(`${created} created`); + if (updated > 0) parts.push(`${updated} updated`); + if (skipped > 0) parts.push(`${skipped} skipped`); + return `${projects.length} ${pluralize(projects.length, "project")} total (${parts.join(", ")})`; +} + +function actionChip(action: string): string { + switch (action) { + case "create": + case "created": + return pc.green(action); + case "update": + case "updated": + return pc.yellow(action); + case "skip": + case "skipped": + case "none": + case "unchanged": + return pc.dim(action); + default: + return action; + } +} + +function appendPreviewExamples( + lines: string[], + title: string, + entries: Array<{ action: string; label: string; reason?: string | null }>, +): void { + if (entries.length === 0) return; + lines.push(""); + lines.push(pc.bold(title)); + const shown = entries.slice(0, IMPORT_PREVIEW_SAMPLE_LIMIT); + for (const entry of shown) { + const reason = entry.reason?.trim() ? pc.dim(` (${entry.reason.trim()})`) : ""; + lines.push(`- ${actionChip(entry.action)} ${entry.label}${reason}`); + } + if (entries.length > shown.length) { + lines.push(pc.dim(`- +${entries.length - shown.length} more`)); + } +} + +function appendMessageBlock(lines: string[], title: string, messages: string[]): void { + if (messages.length === 0) return; + lines.push(""); + lines.push(pc.bold(title)); + for (const message of messages) { + lines.push(`- ${message}`); + } +} + +export function renderCompanyImportPreview( + preview: CompanyPortabilityPreviewResult, + meta: { + sourceLabel: string; + targetLabel: string; + infoMessages?: string[]; + }, +): string { + const lines: string[] = [ + `${pc.bold("Source")} ${meta.sourceLabel}`, + `${pc.bold("Target")} ${meta.targetLabel}`, + `${pc.bold("Include")} ${summarizeInclude(preview.include)}`, + `${pc.bold("Mode")} ${preview.collisionStrategy} collisions`, + "", + pc.bold("Package"), + `- company: ${preview.manifest.company?.name ?? preview.manifest.source?.companyName ?? "not included"}`, + `- agents: ${preview.manifest.agents.length}`, + `- projects: ${preview.manifest.projects.length}`, + `- tasks: ${preview.manifest.issues.length}`, + `- skills: ${preview.manifest.skills.length}`, + ]; + + if (preview.envInputs.length > 0) { + const requiredCount = preview.envInputs.filter((item) => item.requirement === "required").length; + lines.push(`- env inputs: ${preview.envInputs.length} (${requiredCount} required)`); + } + + lines.push(""); + lines.push(pc.bold("Plan")); + lines.push(`- company: ${actionChip(preview.plan.companyAction === "none" ? "unchanged" : preview.plan.companyAction)}`); + lines.push(`- agents: ${summarizePlanCounts(preview.plan.agentPlans, "agent")}`); + lines.push(`- projects: ${summarizePlanCounts(preview.plan.projectPlans, "project")}`); + lines.push(`- tasks: ${summarizePlanCounts(preview.plan.issuePlans, "task")}`); + if (preview.include.skills) { + lines.push(`- skills: ${preview.manifest.skills.length} ${pluralize(preview.manifest.skills.length, "skill")} packaged`); + } + + appendPreviewExamples( + lines, + "Agent examples", + preview.plan.agentPlans.map((plan) => ({ + action: plan.action, + label: `${plan.slug} -> ${plan.plannedName}`, + reason: plan.reason, + })), + ); + appendPreviewExamples( + lines, + "Project examples", + preview.plan.projectPlans.map((plan) => ({ + action: plan.action, + label: `${plan.slug} -> ${plan.plannedName}`, + reason: plan.reason, + })), + ); + appendPreviewExamples( + lines, + "Task examples", + preview.plan.issuePlans.map((plan) => ({ + action: plan.action, + label: `${plan.slug} -> ${plan.plannedTitle}`, + reason: plan.reason, + })), + ); + + appendMessageBlock(lines, pc.cyan("Info"), meta.infoMessages ?? []); + appendMessageBlock(lines, pc.yellow("Warnings"), preview.warnings); + appendMessageBlock(lines, pc.red("Errors"), preview.errors); + + return lines.join("\n"); +} + +export function renderCompanyImportResult( + result: CompanyPortabilityImportResult, + meta: { targetLabel: string; companyUrl?: string; infoMessages?: string[] }, +): string { + const lines: string[] = [ + `${pc.bold("Target")} ${meta.targetLabel}`, + `${pc.bold("Company")} ${result.company.name} (${actionChip(result.company.action)})`, + `${pc.bold("Agents")} ${summarizeImportAgentResults(result.agents)}`, + `${pc.bold("Projects")} ${summarizeImportProjectResults(result.projects)}`, + ]; + + if (meta.companyUrl) { + lines.splice(1, 0, `${pc.bold("URL")} ${meta.companyUrl}`); + } + + appendPreviewExamples( + lines, + "Agent results", + result.agents.map((agent) => ({ + action: agent.action, + label: `${agent.slug} -> ${agent.name}`, + reason: agent.reason, + })), + ); + appendPreviewExamples( + lines, + "Project results", + result.projects.map((project) => ({ + action: project.action, + label: `${project.slug} -> ${project.name}`, + reason: project.reason, + })), + ); + + if (result.envInputs.length > 0) { + lines.push(""); + lines.push(pc.bold("Env inputs")); + lines.push( + `- ${result.envInputs.length} ${pluralize(result.envInputs.length, "input")} may need values after import`, + ); + } + + appendMessageBlock(lines, pc.cyan("Info"), meta.infoMessages ?? []); + appendMessageBlock(lines, pc.yellow("Warnings"), result.warnings); + + return lines.join("\n"); +} + +function printCompanyImportView(title: string, body: string, opts?: { interactive?: boolean }): void { + if (opts?.interactive) { + p.note(body, title); + return; + } + console.log(pc.bold(title)); + console.log(body); +} + +export function resolveCompanyImportApiPath(input: { + dryRun: boolean; + targetMode: "new_company" | "existing_company"; + companyId?: string | null; +}): string { + if (input.targetMode === "existing_company") { + const companyId = input.companyId?.trim(); + if (!companyId) { + throw new Error("Existing-company imports require a companyId to resolve the API route."); + } + return input.dryRun + ? `/api/companies/${companyId}/imports/preview` + : `/api/companies/${companyId}/imports/apply`; + } + + return input.dryRun ? "/api/companies/import/preview" : "/api/companies/import"; +} + +export function buildCompanyDashboardUrl(apiBase: string, issuePrefix: string): string { + const url = new URL(apiBase); + const normalizedPrefix = issuePrefix.trim().replace(/^\/+|\/+$/g, ""); + url.pathname = `${url.pathname.replace(/\/+$/, "")}/${normalizedPrefix}/dashboard`; + url.search = ""; + url.hash = ""; + return url.toString(); +} + +export function resolveCompanyImportApplyConfirmationMode(input: { + yes?: boolean; + interactive: boolean; + json: boolean; +}): "skip" | "prompt" { + if (input.yes) { + return "skip"; + } + if (input.json) { + throw new Error( + "Applying a company import with --json requires --yes. Use --dry-run first to inspect the preview.", + ); + } + if (!input.interactive) { + throw new Error( + "Applying a company import from a non-interactive terminal requires --yes. Use --dry-run first to inspect the preview.", + ); + } + return "prompt"; +} + export function isHttpUrl(input: string): boolean { return /^https?:\/\//i.test(input.trim()); } @@ -122,6 +769,112 @@ export function isGithubUrl(input: string): boolean { return /^https?:\/\/github\.com\//i.test(input.trim()); } +function isGithubSegment(input: string): boolean { + return /^[A-Za-z0-9._-]+$/.test(input); +} + +export function isGithubShorthand(input: string): boolean { + const trimmed = input.trim(); + if (!trimmed || isHttpUrl(trimmed)) return false; + if ( + trimmed.startsWith(".") || + trimmed.startsWith("/") || + trimmed.startsWith("~") || + trimmed.includes("\\") || + /^[A-Za-z]:/.test(trimmed) + ) { + return false; + } + + const segments = trimmed.split("/").filter(Boolean); + return segments.length >= 2 && segments.every(isGithubSegment); +} + +function normalizeGithubImportPath(input: string | null | undefined): string | null { + if (!input) return null; + const trimmed = input.trim().replace(/^\/+|\/+$/g, ""); + return trimmed || null; +} + +function buildGithubImportUrl(input: { + owner: string; + repo: string; + ref?: string | null; + path?: string | null; + companyPath?: string | null; +}): string { + const url = new URL(`https://github.com/${input.owner}/${input.repo.replace(/\.git$/i, "")}`); + const ref = input.ref?.trim(); + if (ref) { + url.searchParams.set("ref", ref); + } + const companyPath = normalizeGithubImportPath(input.companyPath); + if (companyPath) { + url.searchParams.set("companyPath", companyPath); + return url.toString(); + } + const sourcePath = normalizeGithubImportPath(input.path); + if (sourcePath) { + url.searchParams.set("path", sourcePath); + } + return url.toString(); +} + +export function normalizeGithubImportSource(input: string, refOverride?: string): string { + const trimmed = input.trim(); + const ref = refOverride?.trim(); + + if (isGithubShorthand(trimmed)) { + const [owner, repo, ...repoPath] = trimmed.split("/").filter(Boolean); + return buildGithubImportUrl({ + owner: owner!, + repo: repo!, + ref: ref || "main", + path: repoPath.join("/"), + }); + } + + if (!isGithubUrl(trimmed)) { + throw new Error("GitHub source must be a github.com URL or owner/repo[/path] shorthand."); + } + if (!ref) { + return trimmed; + } + + const url = new URL(trimmed); + const parts = url.pathname.split("/").filter(Boolean); + if (parts.length < 2) { + throw new Error("Invalid GitHub URL."); + } + + const owner = parts[0]!; + const repo = parts[1]!; + const existingPath = normalizeGithubImportPath(url.searchParams.get("path")); + const existingCompanyPath = normalizeGithubImportPath(url.searchParams.get("companyPath")); + if (existingCompanyPath) { + return buildGithubImportUrl({ owner, repo, ref, companyPath: existingCompanyPath }); + } + if (existingPath) { + return buildGithubImportUrl({ owner, repo, ref, path: existingPath }); + } + if (parts[2] === "tree") { + return buildGithubImportUrl({ owner, repo, ref, path: parts.slice(4).join("/") }); + } + if (parts[2] === "blob") { + return buildGithubImportUrl({ owner, repo, ref, companyPath: parts.slice(4).join("/") }); + } + return buildGithubImportUrl({ owner, repo, ref }); +} + +async function pathExists(inputPath: string): Promise { + try { + await stat(path.resolve(inputPath)); + return true; + } catch { + return false; + } +} + async function collectPackageFiles( root: string, current: string, @@ -136,21 +889,29 @@ async function collectPackageFiles( continue; } if (!entry.isFile()) continue; - const isMarkdown = entry.name.endsWith(".md"); - const isPaperclipYaml = entry.name === ".paperclip.yaml" || entry.name === ".paperclip.yml"; - const contentType = binaryContentTypeByExtension[path.extname(entry.name).toLowerCase()]; - if (!isMarkdown && !isPaperclipYaml && !contentType) continue; const relativePath = path.relative(root, absolutePath).replace(/\\/g, "/"); + if (!shouldIncludePortableFile(relativePath)) continue; files[relativePath] = readPortableFileEntry(relativePath, await readFile(absolutePath)); } } -async function resolveInlineSourceFromPath(inputPath: string): Promise<{ +export async function resolveInlineSourceFromPath(inputPath: string): Promise<{ rootPath: string; files: Record; }> { const resolved = path.resolve(inputPath); const resolvedStat = await stat(resolved); + if (resolvedStat.isFile() && path.extname(resolved).toLowerCase() === ".zip") { + const archive = await readZipArchive(await readFile(resolved)); + const filteredFiles = Object.fromEntries( + Object.entries(archive.files).filter(([relativePath]) => shouldIncludePortableFile(relativePath)), + ); + return { + rootPath: archive.rootPath ?? path.basename(resolved, ".zip"), + files: filteredFiles, + }; + } + const rootDir = resolvedStat.isDirectory() ? resolved : path.dirname(resolved); const files: Record = {}; await collectPackageFiles(rootDir, rootDir, files); @@ -390,23 +1151,30 @@ export function registerCompanyCommands(program: Command): void { company .command("import") .description("Import a portable markdown company package from local path, URL, or GitHub") - .requiredOption("--from ", "Source path or URL") - .option("--include ", "Comma-separated include set: company,agents,projects,issues,tasks,skills", "company,agents") + .argument("", "Source path or URL") + .option("--include ", "Comma-separated include set: company,agents,projects,issues,tasks,skills") .option("--target ", "Target mode: new | existing") .option("-C, --company-id ", "Existing target company ID") .option("--new-company-name ", "Name override for --target new") .option("--agents ", "Comma-separated agent slugs to import, or all", "all") .option("--collision ", "Collision strategy: rename | skip | replace", "rename") + .option("--ref ", "Git ref to use for GitHub imports (branch, tag, or commit)") + .option("--paperclip-url ", "Alias for --api-base on this command") + .option("--yes", "Accept default selection and skip the pre-import confirmation prompt", false) .option("--dry-run", "Run preview only without applying", false) - .action(async (opts: CompanyImportOptions) => { + .action(async (fromPathOrUrl: string, opts: CompanyImportOptions) => { try { + if (!opts.apiBase?.trim() && opts.paperclipUrl?.trim()) { + opts.apiBase = opts.paperclipUrl.trim(); + } const ctx = resolveCommandContext(opts); - const from = (opts.from ?? "").trim(); + const interactiveView = isInteractiveTerminal() && !ctx.json; + const from = fromPathOrUrl.trim(); if (!from) { - throw new Error("--from is required"); + throw new Error("Source path or URL is required."); } - const include = parseInclude(opts.include); + const include = resolveImportInclude(opts.include); const agents = parseAgents(opts.agents); const collision = (opts.collision ?? "rename").toLowerCase() as CompanyCollisionMode; if (!["rename", "skip", "replace"].includes(collision)) { @@ -439,15 +1207,21 @@ export function registerCompanyCommands(program: Command): void { | { type: "inline"; rootPath?: string | null; files: Record } | { type: "github"; url: string }; - if (isHttpUrl(from)) { - if (!isGithubUrl(from)) { + const treatAsLocalPath = !isHttpUrl(from) && await pathExists(from); + const isGithubSource = isGithubUrl(from) || (isGithubShorthand(from) && !treatAsLocalPath); + + if (isHttpUrl(from) || isGithubSource) { + if (!isGithubUrl(from) && !isGithubShorthand(from)) { throw new Error( "Only GitHub URLs and local paths are supported for import. " + "Generic HTTP URLs are not supported. Use a GitHub URL (https://github.com/...) or a local directory path.", ); } - sourcePayload = { type: "github", url: from }; + sourcePayload = { type: "github", url: normalizeGithubImportSource(from, opts.ref) }; } else { + if (opts.ref?.trim()) { + throw new Error("--ref is only supported for GitHub import sources."); + } const inline = await resolveInlineSourceFromPath(from); sourcePayload = { type: "inline", @@ -456,25 +1230,139 @@ export function registerCompanyCommands(program: Command): void { }; } - const payload = { + const sourceLabel = formatSourceLabel(sourcePayload); + const targetLabel = formatTargetLabel(targetPayload); + const previewApiPath = resolveCompanyImportApiPath({ + dryRun: true, + targetMode: targetPayload.mode, + companyId: targetPayload.mode === "existing_company" ? targetPayload.companyId : null, + }); + + let selectedFiles: string[] | undefined; + if (interactiveView && !opts.yes && !opts.include?.trim()) { + const initialPreview = await ctx.api.post(previewApiPath, { + source: sourcePayload, + include, + target: targetPayload, + agents, + collisionStrategy: collision, + }); + if (!initialPreview) { + throw new Error("Import preview returned no data."); + } + selectedFiles = await promptForImportSelection(initialPreview); + } + + const previewPayload = { source: sourcePayload, include, target: targetPayload, agents, collisionStrategy: collision, + selectedFiles, }; + const preview = await ctx.api.post(previewApiPath, previewPayload); + if (!preview) { + throw new Error("Import preview returned no data."); + } + const adapterOverrides = buildDefaultImportAdapterOverrides(preview); + const adapterMessages = buildDefaultImportAdapterMessages(adapterOverrides); if (opts.dryRun) { - const preview = await ctx.api.post( - "/api/companies/import/preview", - payload, - ); - printOutput(preview, { json: ctx.json }); + if (ctx.json) { + printOutput(preview, { json: true }); + } else { + printCompanyImportView( + "Import Preview", + renderCompanyImportPreview(preview, { + sourceLabel, + targetLabel: formatTargetLabel(targetPayload, preview), + infoMessages: adapterMessages, + }), + { interactive: interactiveView }, + ); + } return; } - const imported = await ctx.api.post("/api/companies/import", payload); - printOutput(imported, { json: ctx.json }); + if (!ctx.json) { + printCompanyImportView( + "Import Preview", + renderCompanyImportPreview(preview, { + sourceLabel, + targetLabel: formatTargetLabel(targetPayload, preview), + infoMessages: adapterMessages, + }), + { interactive: interactiveView }, + ); + } + + const confirmationMode = resolveCompanyImportApplyConfirmationMode({ + yes: opts.yes, + interactive: interactiveView, + json: ctx.json, + }); + if (confirmationMode === "prompt") { + const confirmed = await p.confirm({ + message: "Apply this import? (y/N)", + initialValue: false, + }); + if (p.isCancel(confirmed) || !confirmed) { + p.log.warn("Import cancelled."); + return; + } + } + + const importApiPath = resolveCompanyImportApiPath({ + dryRun: false, + targetMode: targetPayload.mode, + companyId: targetPayload.mode === "existing_company" ? targetPayload.companyId : null, + }); + const imported = await ctx.api.post(importApiPath, { + ...previewPayload, + adapterOverrides, + }); + if (!imported) { + throw new Error("Import request returned no data."); + } + let companyUrl: string | undefined; + if (!ctx.json) { + try { + const importedCompany = await ctx.api.get(`/api/companies/${imported.company.id}`); + const issuePrefix = importedCompany?.issuePrefix?.trim(); + if (issuePrefix) { + companyUrl = buildCompanyDashboardUrl(ctx.api.apiBase, issuePrefix); + } + } catch { + companyUrl = undefined; + } + } + if (ctx.json) { + printOutput(imported, { json: true }); + } else { + printCompanyImportView( + "Import Result", + renderCompanyImportResult(imported, { + targetLabel, + companyUrl, + infoMessages: adapterMessages, + }), + { interactive: interactiveView }, + ); + if (interactiveView && companyUrl) { + const openImportedCompany = await p.confirm({ + message: "Open the imported company in your browser?", + initialValue: true, + }); + if (!p.isCancel(openImportedCompany) && openImportedCompany) { + if (openUrl(companyUrl)) { + p.log.info(`Opened ${companyUrl}`); + } else { + p.log.warn(`Could not open your browser automatically. Open this URL manually:\n${companyUrl}`); + } + } + } + } } catch (err) { handleCommandError(err); } diff --git a/cli/src/commands/client/zip.ts b/cli/src/commands/client/zip.ts new file mode 100644 index 0000000000..b75935e953 --- /dev/null +++ b/cli/src/commands/client/zip.ts @@ -0,0 +1,129 @@ +import { inflateRawSync } from "node:zlib"; +import path from "node:path"; +import type { CompanyPortabilityFileEntry } from "@paperclipai/shared"; + +const textDecoder = new TextDecoder(); + +export const binaryContentTypeByExtension: Record = { + ".gif": "image/gif", + ".jpeg": "image/jpeg", + ".jpg": "image/jpeg", + ".png": "image/png", + ".svg": "image/svg+xml", + ".webp": "image/webp", +}; + +function normalizeArchivePath(pathValue: string) { + return pathValue + .replace(/\\/g, "/") + .split("/") + .filter(Boolean) + .join("/"); +} + +function readUint16(source: Uint8Array, offset: number) { + return source[offset]! | (source[offset + 1]! << 8); +} + +function readUint32(source: Uint8Array, offset: number) { + return ( + source[offset]! | + (source[offset + 1]! << 8) | + (source[offset + 2]! << 16) | + (source[offset + 3]! << 24) + ) >>> 0; +} + +function sharedArchiveRoot(paths: string[]) { + if (paths.length === 0) return null; + const firstSegments = paths + .map((entry) => normalizeArchivePath(entry).split("/").filter(Boolean)) + .filter((parts) => parts.length > 0); + if (firstSegments.length === 0) return null; + const candidate = firstSegments[0]![0]!; + return firstSegments.every((parts) => parts.length > 1 && parts[0] === candidate) + ? candidate + : null; +} + +function bytesToPortableFileEntry(pathValue: string, bytes: Uint8Array): CompanyPortabilityFileEntry { + const contentType = binaryContentTypeByExtension[path.extname(pathValue).toLowerCase()]; + if (!contentType) return textDecoder.decode(bytes); + return { + encoding: "base64", + data: Buffer.from(bytes).toString("base64"), + contentType, + }; +} + +async function inflateZipEntry(compressionMethod: number, bytes: Uint8Array) { + if (compressionMethod === 0) return bytes; + if (compressionMethod !== 8) { + throw new Error("Unsupported zip archive: only STORE and DEFLATE entries are supported."); + } + return new Uint8Array(inflateRawSync(bytes)); +} + +export async function readZipArchive(source: ArrayBuffer | Uint8Array): Promise<{ + rootPath: string | null; + files: Record; +}> { + const bytes = source instanceof Uint8Array ? source : new Uint8Array(source); + const entries: Array<{ path: string; body: CompanyPortabilityFileEntry }> = []; + let offset = 0; + + while (offset + 4 <= bytes.length) { + const signature = readUint32(bytes, offset); + if (signature === 0x02014b50 || signature === 0x06054b50) break; + if (signature !== 0x04034b50) { + throw new Error("Invalid zip archive: unsupported local file header."); + } + + if (offset + 30 > bytes.length) { + throw new Error("Invalid zip archive: truncated local file header."); + } + + const generalPurposeFlag = readUint16(bytes, offset + 6); + const compressionMethod = readUint16(bytes, offset + 8); + const compressedSize = readUint32(bytes, offset + 18); + const fileNameLength = readUint16(bytes, offset + 26); + const extraFieldLength = readUint16(bytes, offset + 28); + + if ((generalPurposeFlag & 0x0008) !== 0) { + throw new Error("Unsupported zip archive: data descriptors are not supported."); + } + + const nameOffset = offset + 30; + const bodyOffset = nameOffset + fileNameLength + extraFieldLength; + const bodyEnd = bodyOffset + compressedSize; + if (bodyEnd > bytes.length) { + throw new Error("Invalid zip archive: truncated file contents."); + } + + const rawArchivePath = textDecoder.decode(bytes.slice(nameOffset, nameOffset + fileNameLength)); + const archivePath = normalizeArchivePath(rawArchivePath); + const isDirectoryEntry = /\/$/.test(rawArchivePath.replace(/\\/g, "/")); + if (archivePath && !isDirectoryEntry) { + const entryBytes = await inflateZipEntry(compressionMethod, bytes.slice(bodyOffset, bodyEnd)); + entries.push({ + path: archivePath, + body: bytesToPortableFileEntry(archivePath, entryBytes), + }); + } + + offset = bodyEnd; + } + + const rootPath = sharedArchiveRoot(entries.map((entry) => entry.path)); + const files: Record = {}; + for (const entry of entries) { + const normalizedPath = + rootPath && entry.path.startsWith(`${rootPath}/`) + ? entry.path.slice(rootPath.length + 1) + : entry.path; + if (!normalizedPath) continue; + files[normalizedPath] = entry.body; + } + + return { rootPath, files }; +} diff --git a/cli/src/commands/worktree-merge-history-lib.ts b/cli/src/commands/worktree-merge-history-lib.ts index a55a22d373..6b16ecb897 100644 --- a/cli/src/commands/worktree-merge-history-lib.ts +++ b/cli/src/commands/worktree-merge-history-lib.ts @@ -50,7 +50,7 @@ export type PlannedIssueInsert = { targetProjectId: string | null; targetProjectWorkspaceId: string | null; targetGoalId: string | null; - projectResolution: "preserved" | "cleared" | "mapped"; + projectResolution: "preserved" | "cleared" | "mapped" | "imported"; mappedProjectName: string | null; adjustments: ImportAdjustment[]; }; @@ -173,17 +173,26 @@ export type PlannedAttachmentSkip = { action: "skip_existing" | "skip_missing_parent"; }; +export type PlannedProjectImport = { + source: ProjectRow; + targetLeadAgentId: string | null; + targetGoalId: string | null; + workspaces: ProjectWorkspaceRow[]; +}; + export type WorktreeMergePlan = { companyId: string; companyName: string; issuePrefix: string; previewIssueCounterStart: number; scopes: WorktreeMergeScope[]; + projectImports: PlannedProjectImport[]; issuePlans: Array; commentPlans: Array; documentPlans: Array; attachmentPlans: Array; counts: { + projectsToImport: number; issuesToInsert: number; issuesExisting: number; issueDrift: number; @@ -338,6 +347,8 @@ export function buildWorktreeMergePlan(input: { targetIssues: IssueRow[]; sourceComments: CommentRow[]; targetComments: CommentRow[]; + sourceProjects?: ProjectRow[]; + sourceProjectWorkspaces?: ProjectWorkspaceRow[]; sourceDocuments?: IssueDocumentRow[]; targetDocuments?: IssueDocumentRow[]; sourceDocumentRevisions?: DocumentRevisionRow[]; @@ -348,6 +359,7 @@ export function buildWorktreeMergePlan(input: { targetProjects: ProjectRow[]; targetProjectWorkspaces: ProjectWorkspaceRow[]; targetGoals: GoalRow[]; + importProjectIds?: Iterable; projectIdOverrides?: Record; }): WorktreeMergePlan { const targetIssuesById = new Map(input.targetIssues.map((issue) => [issue.id, issue])); @@ -357,6 +369,10 @@ export function buildWorktreeMergePlan(input: { const targetProjectsById = new Map(input.targetProjects.map((project) => [project.id, project])); const targetProjectWorkspaceIds = new Set(input.targetProjectWorkspaces.map((workspace) => workspace.id)); const targetGoalIds = new Set(input.targetGoals.map((goal) => goal.id)); + const sourceProjectsById = new Map((input.sourceProjects ?? []).map((project) => [project.id, project])); + const sourceProjectWorkspaces = input.sourceProjectWorkspaces ?? []; + const sourceProjectWorkspacesByProjectId = groupBy(sourceProjectWorkspaces, (workspace) => workspace.projectId); + const importProjectIds = new Set(input.importProjectIds ?? []); const scopes = new Set(input.scopes); const adjustmentCounts: Record = { @@ -371,6 +387,34 @@ export function buildWorktreeMergePlan(input: { clear_attachment_agent: 0, }; + const projectImports: PlannedProjectImport[] = []; + for (const projectId of importProjectIds) { + if (targetProjectIds.has(projectId)) continue; + const sourceProject = sourceProjectsById.get(projectId); + if (!sourceProject) continue; + projectImports.push({ + source: sourceProject, + targetLeadAgentId: + sourceProject.leadAgentId && targetAgentIds.has(sourceProject.leadAgentId) + ? sourceProject.leadAgentId + : null, + targetGoalId: + sourceProject.goalId && targetGoalIds.has(sourceProject.goalId) + ? sourceProject.goalId + : null, + workspaces: [...(sourceProjectWorkspacesByProjectId.get(projectId) ?? [])].sort((left, right) => { + const primaryDelta = Number(right.isPrimary) - Number(left.isPrimary); + if (primaryDelta !== 0) return primaryDelta; + const createdDelta = left.createdAt.getTime() - right.createdAt.getTime(); + if (createdDelta !== 0) return createdDelta; + return left.id.localeCompare(right.id); + }), + }); + } + const importedProjectWorkspaceIds = new Set( + projectImports.flatMap((project) => project.workspaces.map((workspace) => workspace.id)), + ); + const issuePlans: Array = []; let nextPreviewIssueNumber = input.previewIssueCounterStart; for (const issue of sortIssuesForImport(input.sourceIssues)) { @@ -409,6 +453,14 @@ export function buildWorktreeMergePlan(input: { projectResolution = "mapped"; mappedProjectName = targetProjectsById.get(overrideProjectId)?.name ?? null; } + if (!targetProjectId && issue.projectId && importProjectIds.has(issue.projectId)) { + const sourceProject = sourceProjectsById.get(issue.projectId); + if (sourceProject) { + targetProjectId = sourceProject.id; + projectResolution = "imported"; + mappedProjectName = sourceProject.name; + } + } if (issue.projectId && !targetProjectId) { adjustments.push("clear_project"); incrementAdjustment(adjustmentCounts, "clear_project"); @@ -418,7 +470,8 @@ export function buildWorktreeMergePlan(input: { targetProjectId && targetProjectId === issue.projectId && issue.projectWorkspaceId - && targetProjectWorkspaceIds.has(issue.projectWorkspaceId) + && (targetProjectWorkspaceIds.has(issue.projectWorkspaceId) + || importedProjectWorkspaceIds.has(issue.projectWorkspaceId)) ? issue.projectWorkspaceId : null; if (issue.projectWorkspaceId && !targetProjectWorkspaceId) { @@ -672,6 +725,7 @@ export function buildWorktreeMergePlan(input: { } const counts = { + projectsToImport: projectImports.length, issuesToInsert: issuePlans.filter((plan) => plan.action === "insert").length, issuesExisting: issuePlans.filter((plan) => plan.action === "skip_existing").length, issueDrift: issuePlans.filter((plan) => plan.action === "skip_existing" && plan.driftKeys.length > 0).length, @@ -699,6 +753,7 @@ export function buildWorktreeMergePlan(input: { issuePrefix: input.issuePrefix, previewIssueCounterStart: input.previewIssueCounterStart, scopes: input.scopes, + projectImports, issuePlans, commentPlans, documentPlans, diff --git a/cli/src/commands/worktree.ts b/cli/src/commands/worktree.ts index 57166d8f62..7a2bd127fd 100644 --- a/cli/src/commands/worktree.ts +++ b/cli/src/commands/worktree.ts @@ -756,7 +756,7 @@ async function ensureEmbeddedPostgres(dataDir: string, preferredPort: number): P password: "paperclip", port, persistent: true, - initdbFlags: ["--encoding=UTF8", "--locale=C"], + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], onLog: () => {}, onError: () => {}, }); @@ -1488,20 +1488,34 @@ function renderMergePlan(plan: Awaited>["pla `Target: ${extras.targetPath}`, `Company: ${plan.companyName} (${plan.issuePrefix})`, "", + "Projects", + `- import: ${plan.counts.projectsToImport}`, + "", "Issues", `- insert: ${plan.counts.issuesToInsert}`, `- already present: ${plan.counts.issuesExisting}`, `- shared/imported issues with drift: ${plan.counts.issueDrift}`, ]; + if (plan.projectImports.length > 0) { + lines.push(""); + lines.push("Planned project imports"); + for (const project of plan.projectImports) { + lines.push( + `- ${project.source.name} (${project.workspaces.length} workspace${project.workspaces.length === 1 ? "" : "s"})`, + ); + } + } + const issueInserts = plan.issuePlans.filter((item): item is PlannedIssueInsert => item.action === "insert"); if (issueInserts.length > 0) { lines.push(""); lines.push("Planned issue imports"); for (const issue of issueInserts) { const projectNote = - issue.projectResolution === "mapped" && issue.mappedProjectName - ? ` project->${issue.mappedProjectName}` + (issue.projectResolution === "mapped" || issue.projectResolution === "imported") + && issue.mappedProjectName + ? ` project->${issue.projectResolution === "imported" ? "import:" : ""}${issue.mappedProjectName}` : ""; const adjustments = issue.adjustments.length > 0 ? ` [${issue.adjustments.join(", ")}]` : ""; const prefix = `- ${issue.source.identifier ?? issue.source.id} -> ${issue.previewIdentifier} (${issue.targetStatus}${projectNote})`; @@ -1562,6 +1576,7 @@ async function collectMergePlan(input: { targetDb: ClosableDb; company: ResolvedMergeCompany; scopes: ReturnType; + importProjectIds?: Iterable; projectIdOverrides?: Record; }) { const companyId = input.company.id; @@ -1578,6 +1593,7 @@ async function collectMergePlan(input: { sourceAttachmentRows, targetAttachmentRows, sourceProjectsRows, + sourceProjectWorkspaceRows, targetProjectsRows, targetAgentsRows, targetProjectWorkspaceRows, @@ -1743,6 +1759,10 @@ async function collectMergePlan(input: { .select() .from(projects) .where(eq(projects.companyId, companyId)), + input.sourceDb + .select() + .from(projectWorkspaces) + .where(eq(projectWorkspaces.companyId, companyId)), input.targetDb .select() .from(projects) @@ -1779,6 +1799,8 @@ async function collectMergePlan(input: { targetIssues: targetIssuesRows, sourceComments: sourceCommentsRows, targetComments: targetCommentsRows, + sourceProjects: sourceProjectsRows, + sourceProjectWorkspaces: sourceProjectWorkspaceRows, sourceDocuments: sourceIssueDocumentsRows as IssueDocumentRow[], targetDocuments: targetIssueDocumentsRows as IssueDocumentRow[], sourceDocumentRevisions: sourceDocumentRevisionRows as DocumentRevisionRow[], @@ -1789,6 +1811,7 @@ async function collectMergePlan(input: { targetProjects: targetProjectsRows, targetProjectWorkspaces: targetProjectWorkspaceRows, targetGoals: targetGoalsRows, + importProjectIds: input.importProjectIds, projectIdOverrides: input.projectIdOverrides, }); @@ -1800,11 +1823,16 @@ async function collectMergePlan(input: { }; } +type ProjectMappingSelections = { + importProjectIds: string[]; + projectIdOverrides: Record; +}; + async function promptForProjectMappings(input: { plan: Awaited>["plan"]; sourceProjects: Awaited>["sourceProjects"]; targetProjects: Awaited>["targetProjects"]; -}): Promise> { +}): Promise { const missingProjectIds = [ ...new Set( input.plan.issuePlans @@ -1813,8 +1841,11 @@ async function promptForProjectMappings(input: { .map((plan) => plan.source.projectId as string), ), ]; - if (missingProjectIds.length === 0 || input.targetProjects.length === 0) { - return {}; + if (missingProjectIds.length === 0) { + return { + importProjectIds: [], + projectIdOverrides: {}, + }; } const sourceProjectsById = new Map(input.sourceProjects.map((project) => [project.id, project])); @@ -1827,15 +1858,22 @@ async function promptForProjectMappings(input: { })); const mappings: Record = {}; + const importProjectIds = new Set(); for (const sourceProjectId of missingProjectIds) { const sourceProject = sourceProjectsById.get(sourceProjectId); if (!sourceProject) continue; const nameMatch = input.targetProjects.find( (project) => project.name.trim().toLowerCase() === sourceProject.name.trim().toLowerCase(), ); + const importSelectionValue = `__import__:${sourceProjectId}`; const selection = await p.select({ message: `Project "${sourceProject.name}" is missing in target. How should ${input.plan.issuePrefix} imports handle it?`, options: [ + { + value: importSelectionValue, + label: `Import ${sourceProject.name}`, + hint: "Create the project and copy its workspace settings", + }, ...(nameMatch ? [{ value: nameMatch.id, @@ -1855,10 +1893,17 @@ async function promptForProjectMappings(input: { if (p.isCancel(selection)) { throw new Error("Project mapping cancelled."); } + if (selection === importSelectionValue) { + importProjectIds.add(sourceProjectId); + continue; + } mappings[sourceProjectId] = selection; } - return mappings; + return { + importProjectIds: [...importProjectIds], + projectIdOverrides: mappings, + }; } export async function worktreeListCommand(opts: WorktreeListOptions): Promise { @@ -1976,6 +2021,77 @@ async function applyMergePlan(input: { const companyId = input.company.id; return await input.targetDb.transaction(async (tx) => { + const importedProjectIds = input.plan.projectImports.map((project) => project.source.id); + const existingImportedProjectIds = importedProjectIds.length > 0 + ? new Set( + (await tx + .select({ id: projects.id }) + .from(projects) + .where(inArray(projects.id, importedProjectIds))) + .map((row) => row.id), + ) + : new Set(); + const projectImports = input.plan.projectImports.filter((project) => !existingImportedProjectIds.has(project.source.id)); + const importedWorkspaceIds = projectImports.flatMap((project) => project.workspaces.map((workspace) => workspace.id)); + const existingImportedWorkspaceIds = importedWorkspaceIds.length > 0 + ? new Set( + (await tx + .select({ id: projectWorkspaces.id }) + .from(projectWorkspaces) + .where(inArray(projectWorkspaces.id, importedWorkspaceIds))) + .map((row) => row.id), + ) + : new Set(); + + let insertedProjects = 0; + let insertedProjectWorkspaces = 0; + for (const project of projectImports) { + await tx.insert(projects).values({ + id: project.source.id, + companyId, + goalId: project.targetGoalId, + name: project.source.name, + description: project.source.description, + status: project.source.status, + leadAgentId: project.targetLeadAgentId, + targetDate: project.source.targetDate, + color: project.source.color, + pauseReason: project.source.pauseReason, + pausedAt: project.source.pausedAt, + executionWorkspacePolicy: project.source.executionWorkspacePolicy, + archivedAt: project.source.archivedAt, + createdAt: project.source.createdAt, + updatedAt: project.source.updatedAt, + }); + insertedProjects += 1; + + for (const workspace of project.workspaces) { + if (existingImportedWorkspaceIds.has(workspace.id)) continue; + await tx.insert(projectWorkspaces).values({ + id: workspace.id, + companyId, + projectId: project.source.id, + name: workspace.name, + sourceType: workspace.sourceType, + cwd: workspace.cwd, + repoUrl: workspace.repoUrl, + repoRef: workspace.repoRef, + defaultRef: workspace.defaultRef, + visibility: workspace.visibility, + setupCommand: workspace.setupCommand, + cleanupCommand: workspace.cleanupCommand, + remoteProvider: workspace.remoteProvider, + remoteWorkspaceRef: workspace.remoteWorkspaceRef, + sharedWorkspaceKey: workspace.sharedWorkspaceKey, + metadata: workspace.metadata, + isPrimary: workspace.isPrimary, + createdAt: workspace.createdAt, + updatedAt: workspace.updatedAt, + }); + insertedProjectWorkspaces += 1; + } + } + const issueCandidates = input.plan.issuePlans.filter( (plan): plan is PlannedIssueInsert => plan.action === "insert", ); @@ -2274,6 +2390,8 @@ async function applyMergePlan(input: { } return { + insertedProjects, + insertedProjectWorkspaces, insertedIssues, insertedComments, insertedDocuments, @@ -2330,18 +2448,22 @@ export async function worktreeMergeHistoryCommand(sourceArg: string | undefined, scopes, }); if (!opts.yes) { - const projectIdOverrides = await promptForProjectMappings({ + const projectSelections = await promptForProjectMappings({ plan: collected.plan, sourceProjects: collected.sourceProjects, targetProjects: collected.targetProjects, }); - if (Object.keys(projectIdOverrides).length > 0) { + if ( + projectSelections.importProjectIds.length > 0 + || Object.keys(projectSelections.projectIdOverrides).length > 0 + ) { collected = await collectMergePlan({ sourceDb: sourceHandle.db, targetDb: targetHandle.db, company, scopes, - projectIdOverrides, + importProjectIds: projectSelections.importProjectIds, + projectIdOverrides: projectSelections.projectIdOverrides, }); } } @@ -2381,7 +2503,7 @@ export async function worktreeMergeHistoryCommand(sourceArg: string | undefined, } p.outro( pc.green( - `Imported ${applied.insertedIssues} issues, ${applied.insertedComments} comments, ${applied.insertedDocuments} documents (${applied.insertedDocumentRevisions} revisions, ${applied.mergedDocuments} merged), and ${applied.insertedAttachments} attachments into ${company.issuePrefix}.`, + `Imported ${applied.insertedProjects} projects (${applied.insertedProjectWorkspaces} workspaces), ${applied.insertedIssues} issues, ${applied.insertedComments} comments, ${applied.insertedDocuments} documents (${applied.insertedDocumentRevisions} revisions, ${applied.mergedDocuments} merged), and ${applied.insertedAttachments} attachments into ${company.issuePrefix}.`, ), ); } finally { diff --git a/cli/src/config/home.ts b/cli/src/config/home.ts index b1fafd83e9..ef4d8e0938 100644 --- a/cli/src/config/home.ts +++ b/cli/src/config/home.ts @@ -33,6 +33,10 @@ export function resolveDefaultContextPath(): string { return path.resolve(resolvePaperclipHomeDir(), "context.json"); } +export function resolveDefaultCliAuthPath(): string { + return path.resolve(resolvePaperclipHomeDir(), "auth.json"); +} + export function resolveDefaultEmbeddedPostgresDir(instanceId?: string): string { return path.resolve(resolvePaperclipInstanceRoot(instanceId), "db"); } diff --git a/cli/src/index.ts b/cli/src/index.ts index 628cd7e724..828404e842 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -19,6 +19,7 @@ import { applyDataDirOverride, type DataDirOptionLike } from "./config/data-dir. import { loadPaperclipEnvFile } from "./config/env.js"; import { registerWorktreeCommands } from "./commands/worktree.js"; import { registerPluginCommands } from "./commands/client/plugin.js"; +import { registerClientAuthCommands } from "./commands/client/auth.js"; const program = new Command(); const DATA_DIR_OPTION_HELP = @@ -151,6 +152,8 @@ auth .option("--base-url ", "Public base URL used to print invite link") .action(bootstrapCeoInvite); +registerClientAuthCommands(auth); + program.parseAsync().catch((err) => { console.error(err instanceof Error ? err.message : String(err)); process.exit(1); diff --git a/doc/AGENTCOMPANIES_SPEC_INVENTORY.md b/doc/AGENTCOMPANIES_SPEC_INVENTORY.md index 0d62289032..99de314d05 100644 --- a/doc/AGENTCOMPANIES_SPEC_INVENTORY.md +++ b/doc/AGENTCOMPANIES_SPEC_INVENTORY.md @@ -28,7 +28,7 @@ These define the contract between server, CLI, and UI. | File | What it defines | |---|---| -| `packages/shared/src/types/company-portability.ts` | TypeScript interfaces: `CompanyPortabilityManifest`, `CompanyPortabilityFileEntry`, `CompanyPortabilityEnvInput`, export/import/preview request and result types, manifest entry types for agents, skills, projects, issues, companies. | +| `packages/shared/src/types/company-portability.ts` | TypeScript interfaces: `CompanyPortabilityManifest`, `CompanyPortabilityFileEntry`, `CompanyPortabilityEnvInput`, export/import/preview request and result types, manifest entry types for agents, skills, projects, issues, recurring routines, companies. | | `packages/shared/src/validators/company-portability.ts` | Zod schemas for all portability request/response shapes — used by both server routes and CLI. | | `packages/shared/src/types/index.ts` | Re-exports portability types. | | `packages/shared/src/validators/index.ts` | Re-exports portability validators. | @@ -37,7 +37,8 @@ These define the contract between server, CLI, and UI. | File | Responsibility | |---|---| -| `server/src/services/company-portability.ts` | **Core portability service.** Export (manifest generation, markdown file emission, `.paperclip.yaml` sidecars), import (graph resolution, collision handling, entity creation), preview (planned-action summary). Handles skill key derivation, task recurrence parsing, and package README generation. References `agentcompanies/v1` version string. | +| `server/src/services/company-portability.ts` | **Core portability service.** Export (manifest generation, markdown file emission, `.paperclip.yaml` sidecars), import (graph resolution, collision handling, entity creation), preview (planned-action summary). Handles skill key derivation, recurring task <-> routine mapping, legacy recurrence migration, and package README generation. References `agentcompanies/v1` version string. | +| `server/src/services/routines.ts` | Paperclip routine runtime service. Portability now exports routines as recurring `TASK.md` entries and imports recurring tasks back through this service. | | `server/src/services/company-export-readme.ts` | Generates `README.md` and Mermaid org-chart for exported company packages. | | `server/src/services/index.ts` | Re-exports `companyPortabilityService`. | @@ -60,7 +61,7 @@ Route registration lives in `server/src/app.ts` via `companyRoutes(db, storage)` | File | Commands | |---|---| -| `cli/src/commands/client/company.ts` | `company export` — exports a company package to disk (flags: `--out`, `--include`, `--projects`, `--issues`, `--projectIssues`).
`company import` — imports a company package from a file or folder (flags: `--from`, `--include`, `--target`, `--companyId`, `--newCompanyName`, `--agents`, `--collision`, `--dryRun`).
Reads/writes portable file entries and handles `.paperclip.yaml` filtering. | +| `cli/src/commands/client/company.ts` | `company export` — exports a company package to disk (flags: `--out`, `--include`, `--projects`, `--issues`, `--projectIssues`).
`company import ` — imports a company package from a file or folder (flags: positional source path/URL or GitHub shorthand, `--include`, `--target`, `--companyId`, `--newCompanyName`, `--agents`, `--collision`, `--ref`, `--dryRun`).
Reads/writes portable file entries and handles `.paperclip.yaml` filtering. | ## 7. UI — Pages @@ -106,7 +107,7 @@ Route registration lives in `server/src/app.ts` via `companyRoutes(db, storage)` | `PROJECT.md` frontmatter & body | `company-portability.ts` | | `TASK.md` frontmatter & body | `company-portability.ts` | | `SKILL.md` packages | `company-portability.ts`, `company-skills.ts` | -| `.paperclip.yaml` vendor sidecar | `company-portability.ts`, `CompanyExport.tsx`, `company.ts` (CLI) | +| `.paperclip.yaml` vendor sidecar | `company-portability.ts`, `routines.ts`, `CompanyExport.tsx`, `company.ts` (CLI) | | `manifest.json` | `company-portability.ts` (generation), shared types (schema) | | ZIP package format | `zip.ts` (UI), `company.ts` (CLI file I/O) | | Collision resolution | `company-portability.ts` (server), `CompanyImport.tsx` (UI) | diff --git a/doc/SPEC-implementation.md b/doc/SPEC-implementation.md index fd2c4842d5..b51a044708 100644 --- a/doc/SPEC-implementation.md +++ b/doc/SPEC-implementation.md @@ -860,11 +860,15 @@ Export/import behavior in V1: - export emits a clean vendor-neutral markdown package plus `.paperclip.yaml` - projects and starter tasks are opt-in export content rather than default package content -- export strips environment-specific paths (`cwd`, local instruction file paths, inline prompt duplication) +- recurring `TASK.md` entries use `recurring: true` in the base package and Paperclip routine fidelity in `.paperclip.yaml` +- Paperclip imports recurring task packages as routines instead of downgrading them to one-time issues +- export strips environment-specific paths (`cwd`, local instruction file paths, inline prompt duplication) while preserving portable project repo/workspace metadata such as `repoUrl`, refs, and workspace-policy references keyed in `.paperclip.yaml` - export never includes secret values; env inputs are reported as portable declarations instead - import supports target modes: - create a new company - import into an existing company +- import recreates exported project workspaces and remaps portable workspace keys back to target-local workspace ids +- import forces imported agent timer heartbeats off so packages never start scheduled runs implicitly - import supports collision strategies: `rename`, `skip`, `replace` - import supports preview (dry-run) before apply - GitHub imports warn on unpinned refs instead of blocking diff --git a/doc/plans/2026-03-13-company-import-export-v2.md b/doc/plans/2026-03-13-company-import-export-v2.md index 89d39d813b..bd26890c14 100644 --- a/doc/plans/2026-03-13-company-import-export-v2.md +++ b/doc/plans/2026-03-13-company-import-export-v2.md @@ -484,8 +484,8 @@ The CLI should continue to support direct import/export without a registry. Target commands: - `paperclipai company export --out ` -- `paperclipai company import --from --dry-run` -- `paperclipai company import --from --target existing -C ` +- `paperclipai company import --dry-run` +- `paperclipai company import --target existing -C ` Planned additions: diff --git a/docs/adapters/codex-local.md b/docs/adapters/codex-local.md index ad187f757d..ff30263b94 100644 --- a/docs/adapters/codex-local.md +++ b/docs/adapters/codex-local.md @@ -40,6 +40,12 @@ pnpm paperclipai agent local-cli codexcoder --company-id This installs any missing skills, creates an agent API key, and prints shell exports to run as that agent. +## Instructions Resolution + +If `instructionsFilePath` is configured, Paperclip reads that file and prepends it to the stdin prompt sent to `codex exec` on every run. + +This is separate from any workspace-level instruction discovery that Codex itself performs in the run `cwd`. Paperclip does not disable Codex-native repo instruction files, so a repo-local `AGENTS.md` may still be loaded by Codex in addition to the Paperclip-managed agent instructions. + ## Environment Test The environment test checks: diff --git a/docs/api/goals-and-projects.md b/docs/api/goals-and-projects.md index 35dd20d77b..c669c54dc4 100644 --- a/docs/api/goals-and-projects.md +++ b/docs/api/goals-and-projects.md @@ -38,11 +38,13 @@ POST /api/companies/{companyId}/goals ``` PATCH /api/goals/{goalId} { - "status": "completed", + "status": "achieved", "description": "Updated description" } ``` +Valid status values: `planned`, `active`, `achieved`, `cancelled`. + ## Projects Projects group related issues toward a deliverable. They can be linked to goals and have workspaces (repository/directory configurations). diff --git a/docs/api/issues.md b/docs/api/issues.md index ff4878df1d..12fb028b09 100644 --- a/docs/api/issues.md +++ b/docs/api/issues.md @@ -81,6 +81,19 @@ Atomically claims the task and transitions to `in_progress`. Returns `409 Confli Idempotent if you already own the task. +**Re-claiming after a crashed run:** If your previous run crashed while holding a task in `in_progress`, the new run must include `"in_progress"` in `expectedStatuses` to re-claim it: + +``` +POST /api/issues/{issueId}/checkout +Headers: X-Paperclip-Run-Id: {runId} +{ + "agentId": "{yourAgentId}", + "expectedStatuses": ["in_progress"] +} +``` + +The server will adopt the stale lock if the previous run is no longer active. **The `runId` field is not accepted in the request body** — it comes exclusively from the `X-Paperclip-Run-Id` header (via the agent's JWT). + ## Release Task ``` diff --git a/docs/api/routines.md b/docs/api/routines.md new file mode 100644 index 0000000000..eb6b9adc38 --- /dev/null +++ b/docs/api/routines.md @@ -0,0 +1,201 @@ +--- +title: Routines +summary: Recurring task scheduling, triggers, and run history +--- + +Routines are recurring tasks that fire on a schedule, webhook, or API call and create a heartbeat run for the assigned agent. + +## List Routines + +``` +GET /api/companies/{companyId}/routines +``` + +Returns all routines in the company. + +## Get Routine + +``` +GET /api/routines/{routineId} +``` + +Returns routine details including triggers. + +## Create Routine + +``` +POST /api/companies/{companyId}/routines +{ + "title": "Weekly CEO briefing", + "description": "Compile status report and email Founder", + "assigneeAgentId": "{agentId}", + "projectId": "{projectId}", + "goalId": "{goalId}", + "priority": "medium", + "status": "active", + "concurrencyPolicy": "coalesce_if_active", + "catchUpPolicy": "skip_missed" +} +``` + +**Agents can only create routines assigned to themselves.** Board operators can assign to any agent. + +Fields: + +| Field | Required | Description | +|-------|----------|-------------| +| `title` | yes | Routine name | +| `description` | no | Human-readable description of the routine | +| `assigneeAgentId` | yes | Agent who receives each run | +| `projectId` | yes | Project this routine belongs to | +| `goalId` | no | Goal to link runs to | +| `parentIssueId` | no | Parent issue for created run issues | +| `priority` | no | `critical`, `high`, `medium` (default), `low` | +| `status` | no | `active` (default), `paused`, `archived` | +| `concurrencyPolicy` | no | Behaviour when a run fires while a previous one is still active | +| `catchUpPolicy` | no | Behaviour for missed scheduled runs | + +**Concurrency policies:** + +| Value | Behaviour | +|-------|-----------| +| `coalesce_if_active` (default) | Incoming run is immediately finalised as `coalesced` and linked to the active run — no new issue is created | +| `skip_if_active` | Incoming run is immediately finalised as `skipped` and linked to the active run — no new issue is created | +| `always_enqueue` | Always create a new run regardless of active runs | + +**Catch-up policies:** + +| Value | Behaviour | +|-------|-----------| +| `skip_missed` (default) | Missed scheduled runs are dropped | +| `enqueue_missed_with_cap` | Missed runs are enqueued up to an internal cap | + +## Update Routine + +``` +PATCH /api/routines/{routineId} +{ + "status": "paused" +} +``` + +All fields from create are updatable. **Agents can only update routines assigned to themselves and cannot reassign a routine to another agent.** + +## Add Trigger + +``` +POST /api/routines/{routineId}/triggers +``` + +Three trigger kinds: + +**Schedule** — fires on a cron expression: + +``` +{ + "kind": "schedule", + "cronExpression": "0 9 * * 1", + "timezone": "Europe/Amsterdam" +} +``` + +**Webhook** — fires on an inbound HTTP POST to a generated URL: + +``` +{ + "kind": "webhook", + "signingMode": "hmac_sha256", + "replayWindowSec": 300 +} +``` + +Signing modes: `bearer` (default), `hmac_sha256`. Replay window range: 30–86400 seconds (default 300). + +**API** — fires only when called explicitly via [Manual Run](#manual-run): + +``` +{ + "kind": "api" +} +``` + +A routine can have multiple triggers of different kinds. + +## Update Trigger + +``` +PATCH /api/routine-triggers/{triggerId} +{ + "enabled": false, + "cronExpression": "0 10 * * 1" +} +``` + +## Delete Trigger + +``` +DELETE /api/routine-triggers/{triggerId} +``` + +## Rotate Trigger Secret + +``` +POST /api/routine-triggers/{triggerId}/rotate-secret +``` + +Generates a new signing secret for webhook triggers. The previous secret is immediately invalidated. + +## Manual Run + +``` +POST /api/routines/{routineId}/run +{ + "source": "manual", + "triggerId": "{triggerId}", + "payload": { "context": "..." }, + "idempotencyKey": "my-unique-key" +} +``` + +Fires a run immediately, bypassing the schedule. Concurrency policy still applies. + +`triggerId` is optional. When supplied, the server validates the trigger belongs to this routine (`403`) and is enabled (`409`), then records the run against that trigger and updates its `lastFiredAt`. Omit it for a generic manual run with no trigger attribution. + +## Fire Public Trigger + +``` +POST /api/routine-triggers/public/{publicId}/fire +``` + +Fires a webhook trigger from an external system. Requires a valid `Authorization` or `X-Paperclip-Signature` + `X-Paperclip-Timestamp` header pair matching the trigger's signing mode. + +## List Runs + +``` +GET /api/routines/{routineId}/runs?limit=50 +``` + +Returns recent run history for the routine. Defaults to 50 most recent runs. + +## Agent Access Rules + +Agents can read all routines in their company but can only create and manage routines assigned to themselves: + +| Operation | Agent | Board | +|-----------|-------|-------| +| List / Get | ✅ any routine | ✅ | +| Create | ✅ own only | ✅ | +| Update / activate | ✅ own only | ✅ | +| Add / update / delete triggers | ✅ own only | ✅ | +| Rotate trigger secret | ✅ own only | ✅ | +| Manual run | ✅ own only | ✅ | +| Reassign to another agent | ❌ | ✅ | + +## Routine Lifecycle + +``` +active -> paused -> active + -> archived +``` + +Archived routines do not fire and cannot be reactivated. diff --git a/docs/cli/control-plane-commands.md b/docs/cli/control-plane-commands.md index c0d2664cb6..80eb0edbfa 100644 --- a/docs/cli/control-plane-commands.md +++ b/docs/cli/control-plane-commands.md @@ -41,15 +41,16 @@ pnpm paperclipai company export --out ./exports/acme --include comp # Preview import (no writes) pnpm paperclipai company import \ - --from https://github.com///tree/main/ \ + // \ --target existing \ --company-id \ + --ref main \ --collision rename \ --dry-run # Apply import pnpm paperclipai company import \ - --from ./exports/acme \ + ./exports/acme \ --target new \ --new-company-name "Acme Imported" \ --include company,agents diff --git a/docs/companies/companies-spec.md b/docs/companies/companies-spec.md index 17fa8ef3b1..5f1327dbf1 100644 --- a/docs/companies/companies-spec.md +++ b/docs/companies/companies-spec.md @@ -253,17 +253,7 @@ owner: cto name: Monday Review assignee: ceo project: q2-launch -schedule: - timezone: America/Chicago - startsAt: 2026-03-16T09:00:00-05:00 - recurrence: - frequency: weekly - interval: 1 - weekdays: - - monday - time: - hour: 9 - minute: 0 +recurring: true ``` ### Semantics @@ -271,58 +261,30 @@ schedule: - body content is the canonical markdown task description - `assignee` should reference an agent slug inside the package - `project` should reference a project slug when the task belongs to a `PROJECT.md` -- tasks are intentionally basic seed work: title, markdown body, assignee, and optional recurrence +- `recurring: true` marks the task as ongoing recurring work instead of a one-time starter task +- tasks are intentionally basic seed work: title, markdown body, assignee, project linkage, and optional `recurring: true` - tools may also support optional fields like `priority`, `labels`, or `metadata`, but they should not require them in the base package -### Scheduling +### Recurring Tasks -The scheduling model is intentionally lightweight. It should cover common recurring patterns such as: +- the base package only needs to say whether a task is recurring +- vendors may attach the actual schedule / trigger / runtime fidelity in a vendor extension such as `.paperclip.yaml` +- this keeps `TASK.md` portable while still allowing richer runtime systems to round-trip their own automation details +- legacy packages may still use `schedule.recurrence` during transition, but exporters should prefer `recurring: true` -- every 6 hours -- every weekday at 9:00 -- every Monday morning -- every month on the 1st -- every first Monday of the month -- every year on January 1 - -Suggested shape: +Example Paperclip extension: ```yaml -schedule: - timezone: America/Chicago - startsAt: 2026-03-14T09:00:00-05:00 - recurrence: - frequency: hourly | daily | weekly | monthly | yearly - interval: 1 - weekdays: - - monday - - wednesday - monthDays: - - 1 - - 15 - ordinalWeekdays: - - weekday: monday - ordinal: 1 - months: - - 1 - - 6 - time: - hour: 9 - minute: 0 - until: 2026-12-31T23:59:59-06:00 - count: 10 +routines: + monday-review: + triggers: + - kind: schedule + cronExpression: "0 9 * * 1" + timezone: America/Chicago ``` -Rules: - -- `timezone` should use an IANA timezone like `America/Chicago` -- `startsAt` anchors the first occurrence -- `frequency` and `interval` are the only required recurrence fields -- `weekdays`, `monthDays`, `ordinalWeekdays`, and `months` are optional narrowing rules -- `ordinalWeekdays` uses `ordinal` values like `1`, `2`, `3`, `4`, or `-1` for “last” -- `time.hour` and `time.minute` keep common “morning / 9:00 / end of day” scheduling human-readable -- `until` and `count` are optional recurrence end bounds -- tools may accept richer calendar syntaxes such as RFC5545 `RRULE`, but exporters should prefer the structured form above +- vendors should ignore unknown recurring-task extensions they do not understand +- vendors importing legacy `schedule.recurrence` data may translate it into their own runtime trigger model, but new exports should prefer the simpler `recurring: true` base field ## 11. SKILL.md Compatibility @@ -449,7 +411,7 @@ Suggested import UI behavior: - selecting an agent auto-selects required docs and referenced skills - selecting a team auto-selects its subtree - selecting a project auto-selects its included tasks -- selecting a recurring task should surface its schedule before import +- selecting a recurring task should make it clear that the import target is a routine / automation, not a one-time task - selecting referenced third-party content shows attribution, license, and fetch policy ## 15. Vendor Extensions @@ -502,6 +464,12 @@ agents: kind: plain requirement: optional default: claude +routines: + monday-review: + triggers: + - kind: schedule + cronExpression: "0 9 * * 1" + timezone: America/Chicago ``` Additional rules for Paperclip exporters: @@ -520,7 +488,7 @@ A compliant exporter should: - omit machine-local ids and timestamps - omit secret values - omit machine-specific paths -- preserve task descriptions and recurrence definitions when exporting tasks +- preserve task descriptions and recurring-task declarations when exporting tasks - omit empty/default fields - default to the vendor-neutral base package - Paperclip exporters should emit `.paperclip.yaml` as a sidecar by default @@ -569,11 +537,11 @@ Paperclip can map this spec to its runtime model like this: - `TEAM.md` -> importable org subtree - `AGENTS.md` -> agent identity and instructions - `PROJECT.md` -> starter project definition - - `TASK.md` -> starter issue/task definition, or automation template when recurrence is present + - `TASK.md` -> starter issue/task definition, or recurring task template when `recurring: true` - `SKILL.md` -> imported skill package - `sources[]` -> provenance and pinned upstream refs - Paperclip extension: - - `.paperclip.yaml` -> adapter config, runtime config, env input declarations, permissions, budgets, and other Paperclip-specific fidelity + - `.paperclip.yaml` -> adapter config, runtime config, env input declarations, permissions, budgets, routine triggers, and other Paperclip-specific fidelity Inline Paperclip-only metadata that must live inside a shared markdown file should use: diff --git a/docs/docs.json b/docs/docs.json index 96b9f696bd..a13a1e7719 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -48,7 +48,8 @@ "guides/board-operator/managing-tasks", "guides/board-operator/approvals", "guides/board-operator/costs-and-budgets", - "guides/board-operator/activity-log" + "guides/board-operator/activity-log", + "guides/board-operator/importing-and-exporting" ] }, { diff --git a/docs/guides/board-operator/importing-and-exporting.md b/docs/guides/board-operator/importing-and-exporting.md new file mode 100644 index 0000000000..02c8cc132f --- /dev/null +++ b/docs/guides/board-operator/importing-and-exporting.md @@ -0,0 +1,203 @@ +--- +title: Importing & Exporting Companies +summary: Export companies to portable packages and import them from local paths or GitHub +--- + +Paperclip companies can be exported to portable markdown packages and imported from local directories or GitHub repositories. This lets you share company configurations, duplicate setups, and version-control your agent teams. + +## Package Format + +Exported packages follow the [Agent Companies specification](/companies/companies-spec) and use a markdown-first structure: + +```text +my-company/ +├── COMPANY.md # Company metadata +├── agents/ +│ ├── ceo/AGENT.md # Agent instructions + frontmatter +│ └── cto/AGENT.md +├── projects/ +│ └── main/PROJECT.md +├── skills/ +│ └── review/SKILL.md +├── tasks/ +│ └── onboarding/TASK.md +└── .paperclip.yaml # Adapter config, env inputs, routines +``` + +- **COMPANY.md** defines company name, description, and metadata. +- **AGENT.md** files contain agent identity, role, and instructions. +- **SKILL.md** files are compatible with the Agent Skills ecosystem. +- **.paperclip.yaml** holds Paperclip-specific config (adapter types, env inputs, budgets) as an optional sidecar. + +## Exporting a Company + +Export a company into a portable folder: + +```sh +paperclipai company export --out ./my-export +``` + +### Options + +| Option | Description | Default | +|--------|-------------|---------| +| `--out ` | Output directory (required) | — | +| `--include ` | Comma-separated set: `company`, `agents`, `projects`, `issues`, `tasks`, `skills` | `company,agents` | +| `--skills ` | Export only specific skill slugs | all | +| `--projects ` | Export only specific project shortnames or IDs | all | +| `--issues ` | Export specific issue identifiers or IDs | none | +| `--project-issues ` | Export issues belonging to specific projects | none | +| `--expand-referenced-skills` | Vendor skill file contents instead of keeping upstream references | `false` | + +### Examples + +```sh +# Export company with agents and projects +paperclipai company export abc123 --out ./backup --include company,agents,projects + +# Export everything including tasks and skills +paperclipai company export abc123 --out ./full-export --include company,agents,projects,tasks,skills + +# Export only specific skills +paperclipai company export abc123 --out ./skills-only --include skills --skills review,deploy +``` + +### What Gets Exported + +- Company name, description, and metadata +- Agent names, roles, reporting structure, and instructions +- Project definitions and workspace config +- Task/issue descriptions (when included) +- Skill packages (as references or vendored content) +- Adapter type and env input declarations in `.paperclip.yaml` + +Secret values, machine-local paths, and database IDs are **never** exported. + +## Importing a Company + +Import from a local directory, GitHub URL, or GitHub shorthand: + +```sh +# From a local folder +paperclipai company import ./my-export + +# From a GitHub URL +paperclipai company import https://github.com/org/repo + +# From a GitHub subfolder +paperclipai company import https://github.com/org/repo/tree/main/companies/acme + +# From GitHub shorthand +paperclipai company import org/repo +paperclipai company import org/repo/companies/acme +``` + +### Options + +| Option | Description | Default | +|--------|-------------|---------| +| `--target ` | `new` (create a new company) or `existing` (merge into existing) | inferred from context | +| `--company-id ` | Target company ID for `--target existing` | current context | +| `--new-company-name ` | Override company name for `--target new` | from package | +| `--include ` | Comma-separated set: `company`, `agents`, `projects`, `issues`, `tasks`, `skills` | auto-detected | +| `--agents ` | Comma-separated agent slugs to import, or `all` | `all` | +| `--collision ` | How to handle name conflicts: `rename`, `skip`, or `replace` | `rename` | +| `--ref ` | Git ref for GitHub imports (branch, tag, or commit) | default branch | +| `--dry-run` | Preview what would be imported without applying | `false` | +| `--yes` | Skip the interactive confirmation prompt | `false` | +| `--json` | Output result as JSON | `false` | + +### Target Modes + +- **`new`** — Creates a fresh company from the package. Good for duplicating a company template. +- **`existing`** — Merges the package into an existing company. Use `--company-id` to specify the target. + +If `--target` is not specified, Paperclip infers it: if a `--company-id` is provided (or one exists in context), it defaults to `existing`; otherwise `new`. + +### Collision Strategies + +When importing into an existing company, agent or project names may conflict with existing ones: + +- **`rename`** (default) — Appends a suffix to avoid conflicts (e.g., `ceo` becomes `ceo-2`). +- **`skip`** — Skips entities that already exist. +- **`replace`** — Overwrites existing entities. Only available for non-safe imports (not available through the CEO API). + +### Interactive Selection + +When running interactively (no `--yes` or `--json` flags), the import command shows a selection picker before applying. You can choose exactly which agents, projects, skills, and tasks to import using a checkbox interface. + +### Preview Before Applying + +Always preview first with `--dry-run`: + +```sh +paperclipai company import org/repo --target existing --company-id abc123 --dry-run +``` + +The preview shows: +- **Package contents** — How many agents, projects, tasks, and skills are in the source +- **Import plan** — What will be created, renamed, skipped, or replaced +- **Env inputs** — Environment variables that may need values after import +- **Warnings** — Potential issues like missing skills or unresolved references + +Imported agents always land with timer heartbeats disabled. Assignment/on-demand wake behavior from the package is preserved, but scheduled runs stay off until a board operator re-enables them. + +### Common Workflows + +**Clone a company template from GitHub:** + +```sh +paperclipai company import org/company-templates/engineering-team \ + --target new \ + --new-company-name "My Engineering Team" +``` + +**Add agents from a package into your existing company:** + +```sh +paperclipai company import ./shared-agents \ + --target existing \ + --company-id abc123 \ + --include agents \ + --collision rename +``` + +**Import a specific branch or tag:** + +```sh +paperclipai company import org/repo --ref v2.0.0 --dry-run +``` + +**Non-interactive import (CI/scripts):** + +```sh +paperclipai company import ./package \ + --target new \ + --yes \ + --json +``` + +## API Endpoints + +The CLI commands use these API endpoints under the hood: + +| Action | Endpoint | +|--------|----------| +| Export company | `POST /api/companies/{companyId}/export` | +| Preview import (existing company) | `POST /api/companies/{companyId}/imports/preview` | +| Apply import (existing company) | `POST /api/companies/{companyId}/imports/apply` | +| Preview import (new company) | `POST /api/companies/import/preview` | +| Apply import (new company) | `POST /api/companies/import` | + +CEO agents can also use the safe import routes (`/imports/preview` and `/imports/apply`) which enforce non-destructive rules: `replace` is rejected, collisions resolve with `rename` or `skip`, and issues are always created as new. + +## GitHub Sources + +Paperclip supports several GitHub URL formats: + +- Full URL: `https://github.com/org/repo` +- Subfolder URL: `https://github.com/org/repo/tree/main/path/to/company` +- Shorthand: `org/repo` +- Shorthand with path: `org/repo/path/to/company` + +Use `--ref` to pin to a specific branch, tag, or commit hash when importing from GitHub. diff --git a/docs/guides/board-operator/org-structure.md b/docs/guides/board-operator/org-structure.md index b074d312c6..43e36b614e 100644 --- a/docs/guides/board-operator/org-structure.md +++ b/docs/guides/board-operator/org-structure.md @@ -9,6 +9,7 @@ Paperclip enforces a strict organizational hierarchy. Every agent reports to exa - The **CEO** has no manager (reports to the board/human operator) - Every other agent has a `reportsTo` field pointing to their manager +- You can change an agent’s manager after creation from **Agent → Configuration → Reports to** (or via `PATCH /api/agents/{id}` with `reportsTo`) - Managers can create subtasks and delegate to their reports - Agents escalate blockers up the chain of command diff --git a/package.json b/package.json index 0f5c23ad07..749cc8d0cf 100644 --- a/package.json +++ b/package.json @@ -35,8 +35,8 @@ "test:release-smoke:headed": "npx playwright test --config tests/release-smoke/playwright.config.ts --headed" }, "devDependencies": { - "cross-env": "^10.1.0", "@playwright/test": "^1.58.2", + "cross-env": "^10.1.0", "esbuild": "^0.27.3", "typescript": "^5.7.3", "vitest": "^3.0.5" @@ -44,5 +44,10 @@ "engines": { "node": ">=20" }, - "packageManager": "pnpm@9.15.4" + "packageManager": "pnpm@9.15.4", + "pnpm": { + "patchedDependencies": { + "embedded-postgres@18.1.0-beta.16": "patches/embedded-postgres@18.1.0-beta.16.patch" + } + } } diff --git a/packages/adapters/claude-local/src/server/execute.ts b/packages/adapters/claude-local/src/server/execute.ts index 05b90a5513..8ac1d7ee6d 100644 --- a/packages/adapters/claude-local/src/server/execute.ts +++ b/packages/adapters/claude-local/src/server/execute.ts @@ -352,7 +352,6 @@ export async function execute(ctx: AdapterExecutionContext): Promise/companies//codex-home/skills/; when CODEX_HOME is explicitly overridden in adapter config, that override is used instead. - Unless explicitly overridden in adapter config, Paperclip runs Codex with a per-company managed CODEX_HOME under the active Paperclip instance and seeds auth/config from the shared Codex home (the CODEX_HOME env var, when set, or ~/.codex). - Some model/tool combinations reject certain effort levels (for example minimal with web search enabled). - When Paperclip realizes a workspace/runtime for a run, it injects PAPERCLIP_WORKSPACE_* and PAPERCLIP_RUNTIME_* env vars for agent-side tooling. diff --git a/packages/adapters/codex-local/src/server/execute.ts b/packages/adapters/codex-local/src/server/execute.ts index b6bda8dfa7..35c681ee8d 100644 --- a/packages/adapters/codex-local/src/server/execute.ts +++ b/packages/adapters/codex-local/src/server/execute.ts @@ -21,7 +21,7 @@ import { runChildProcess, } from "@paperclipai/adapter-utils/server-utils"; import { parseCodexJsonl, isCodexUnknownSessionError } from "./parse.js"; -import { pathExists, prepareManagedCodexHome, resolveManagedCodexHomeDir } from "./codex-home.js"; +import { pathExists, prepareManagedCodexHome, resolveManagedCodexHomeDir, resolveSharedCodexHomeDir } from "./codex-home.js"; import { resolveCodexDesiredSkillNames } from "./skills.js"; const __moduleDir = path.dirname(fileURLToPath(import.meta.url)); @@ -135,8 +135,8 @@ async function pruneBrokenUnavailablePaperclipSkillSymlinks( } } -function resolveCodexWorkspaceSkillsDir(cwd: string): string { - return path.join(cwd, ".agents", "skills"); +function resolveCodexSkillsDir(codexHome: string): string { + return path.join(codexHome, "skills"); } type EnsureCodexSkillsInjectedOptions = { @@ -157,7 +157,7 @@ export async function ensureCodexSkillsInjected( const skillsEntries = allSkillsEntries.filter((entry) => desiredSet.has(entry.key)); if (skillsEntries.length === 0) return; - const skillsHome = options.skillsHome ?? resolveCodexWorkspaceSkillsDir(process.cwd()); + const skillsHome = options.skillsHome ?? resolveCodexSkillsDir(resolveSharedCodexHomeDir()); await fs.mkdir(skillsHome, { recursive: true }); const linkSkill = options.linkSkill; for (const entry of skillsEntries) { @@ -273,11 +273,13 @@ export async function execute(ctx: AdapterExecutionContext): Promise { - if (!instructionsFilePath) return [] as string[]; + if (!instructionsFilePath) { + return [repoAgentsNote]; + } if (instructionsPrefix.length > 0) { return [ `Loaded agent instructions from ${instructionsFilePath}`, `Prepended instructions + path directive to stdin prompt (relative references from ${instructionsDir}).`, + repoAgentsNote, ]; } return [ `Configured instructionsFilePath ${instructionsFilePath}, but file could not be read; continuing without injected instructions.`, + repoAgentsNote, ]; })(); const bootstrapPromptTemplate = asString(config.bootstrapPromptTemplate, ""); diff --git a/packages/adapters/codex-local/src/server/quota.ts b/packages/adapters/codex-local/src/server/quota.ts index b51f6646f2..7bc771e46e 100644 --- a/packages/adapters/codex-local/src/server/quota.ts +++ b/packages/adapters/codex-local/src/server/quota.ts @@ -107,8 +107,8 @@ function parsePlanAndEmailFromToken(idToken: string | null, accessToken: string return { email: null, planType: null }; } -export async function readCodexAuthInfo(): Promise { - const authPath = path.join(codexHomeDir(), "auth.json"); +export async function readCodexAuthInfo(codexHome?: string): Promise { + const authPath = path.join(codexHome ?? codexHomeDir(), "auth.json"); let raw: string; try { raw = await fs.readFile(authPath, "utf8"); diff --git a/packages/adapters/codex-local/src/server/skills.ts b/packages/adapters/codex-local/src/server/skills.ts index 459a6cafb8..0916c0b74b 100644 --- a/packages/adapters/codex-local/src/server/skills.ts +++ b/packages/adapters/codex-local/src/server/skills.ts @@ -31,7 +31,7 @@ async function buildCodexSkillSnapshot( sourcePath: entry.source, targetPath: null, detail: desiredSet.has(entry.key) - ? "Will be linked into the workspace .agents/skills directory on the next run." + ? "Will be linked into the effective CODEX_HOME/skills/ directory on the next run." : null, required: Boolean(entry.required), requiredReason: entry.requiredReason ?? null, diff --git a/packages/adapters/codex-local/src/server/test.ts b/packages/adapters/codex-local/src/server/test.ts index 292e53eed0..64af601b7b 100644 --- a/packages/adapters/codex-local/src/server/test.ts +++ b/packages/adapters/codex-local/src/server/test.ts @@ -15,6 +15,7 @@ import { } from "@paperclipai/adapter-utils/server-utils"; import path from "node:path"; import { parseCodexJsonl } from "./parse.js"; +import { codexHomeDir, readCodexAuthInfo } from "./quota.js"; function summarizeStatus(checks: AdapterEnvironmentCheck[]): AdapterEnvironmentTestResult["status"] { if (checks.some((check) => check.level === "error")) return "fail"; @@ -108,12 +109,23 @@ export async function testEnvironment( detail: `Detected in ${source}.`, }); } else { - checks.push({ - code: "codex_openai_api_key_missing", - level: "warn", - message: "OPENAI_API_KEY is not set. Codex runs may fail until authentication is configured.", - hint: "Set OPENAI_API_KEY in adapter env, shell environment, or Codex auth configuration.", - }); + const codexHome = isNonEmpty(env.CODEX_HOME) ? env.CODEX_HOME : undefined; + const codexAuth = await readCodexAuthInfo(codexHome).catch(() => null); + if (codexAuth) { + checks.push({ + code: "codex_native_auth_present", + level: "info", + message: "Codex is authenticated via its own auth configuration.", + detail: codexAuth.email ? `Logged in as ${codexAuth.email}.` : `Credentials found in ${path.join(codexHome ?? codexHomeDir(), "auth.json")}.`, + }); + } else { + checks.push({ + code: "codex_openai_api_key_missing", + level: "warn", + message: "OPENAI_API_KEY is not set. Codex runs may fail until authentication is configured.", + hint: "Set OPENAI_API_KEY in adapter env, shell environment, or run `codex auth` to log in.", + }); + } } const canRunProbe = diff --git a/packages/adapters/cursor-local/src/server/execute.ts b/packages/adapters/cursor-local/src/server/execute.ts index 60fcab81ee..df33969001 100644 --- a/packages/adapters/cursor-local/src/server/execute.ts +++ b/packages/adapters/cursor-local/src/server/execute.ts @@ -307,10 +307,6 @@ export async function execute(ctx: AdapterExecutionContext): Promise max ? `${clean.slice(0, max - 1)}…` : clean; } +export interface CursorAuthInfo { + email: string | null; + displayName: string | null; + userId: number | null; +} + +export function cursorConfigPath(cursorHome?: string): string { + return path.join(cursorHome ?? path.join(os.homedir(), ".cursor"), "cli-config.json"); +} + +export async function readCursorAuthInfo(cursorHome?: string): Promise { + let raw: string; + try { + raw = await fs.readFile(cursorConfigPath(cursorHome), "utf8"); + } catch { + return null; + } + let parsed: unknown; + try { + parsed = JSON.parse(raw); + } catch { + return null; + } + if (typeof parsed !== "object" || parsed === null) return null; + const obj = parsed as Record; + const authInfo = obj.authInfo; + if (typeof authInfo !== "object" || authInfo === null) return null; + const info = authInfo as Record; + const email = typeof info.email === "string" && info.email.trim().length > 0 ? info.email.trim() : null; + const displayName = typeof info.displayName === "string" && info.displayName.trim().length > 0 ? info.displayName.trim() : null; + const userId = typeof info.userId === "number" ? info.userId : null; + if (!email && !displayName && userId == null) return null; + return { email, displayName, userId }; +} + const CURSOR_AUTH_REQUIRED_RE = /(?:authentication\s+required|not\s+authenticated|not\s+logged\s+in|unauthorized|invalid(?:\s+or\s+missing)?\s+api(?:[_\s-]?key)?|cursor[_\s-]?api[_\s-]?key|run\s+'?agent\s+login'?\s+first|api(?:[_\s-]?key)?(?:\s+is)?\s+required)/i; @@ -109,12 +146,25 @@ export async function testEnvironment( detail: `Detected in ${source}.`, }); } else { - checks.push({ - code: "cursor_api_key_missing", - level: "warn", - message: "CURSOR_API_KEY is not set. Cursor runs may fail until authentication is configured.", - hint: "Set CURSOR_API_KEY in adapter env or run `agent login`.", - }); + const cursorHome = isNonEmpty(env.CURSOR_HOME) ? env.CURSOR_HOME : undefined; + const cursorAuth = await readCursorAuthInfo(cursorHome).catch(() => null); + if (cursorAuth) { + checks.push({ + code: "cursor_native_auth_present", + level: "info", + message: "Cursor is authenticated via `agent login`.", + detail: cursorAuth.email + ? `Logged in as ${cursorAuth.email}.` + : `Credentials found in ${cursorConfigPath(cursorHome)}.`, + }); + } else { + checks.push({ + code: "cursor_api_key_missing", + level: "warn", + message: "CURSOR_API_KEY is not set. Cursor runs may fail until authentication is configured.", + hint: "Set CURSOR_API_KEY in adapter env or run `agent login`.", + }); + } } const canRunProbe = diff --git a/packages/adapters/gemini-local/src/server/execute.ts b/packages/adapters/gemini-local/src/server/execute.ts index 327ee95ec3..36b28ad8ba 100644 --- a/packages/adapters/gemini-local/src/server/execute.ts +++ b/packages/adapters/gemini-local/src/server/execute.ts @@ -253,10 +253,6 @@ export async function execute(ctx: AdapterExecutionContext): Promise check.code === "opencode_cwd_invalid"); diff --git a/packages/adapters/pi-local/src/server/execute.ts b/packages/adapters/pi-local/src/server/execute.ts index 9cea8089c9..96588f1890 100644 --- a/packages/adapters/pi-local/src/server/execute.ts +++ b/packages/adapters/pi-local/src/server/execute.ts @@ -266,10 +266,6 @@ export async function execute(ctx: AdapterExecutionContext): Promise { password: "paperclip", port, persistent: true, - initdbFlags: ["--encoding=UTF8", "--locale=C"], + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], onLog: () => {}, onError: () => {}, }); @@ -154,4 +154,78 @@ describe("applyPendingMigrations", () => { }, 20_000, ); + + it( + "replays migration 0044 safely when its schema changes already exist", + async () => { + const connectionString = await createTempDatabase(); + + await applyPendingMigrations(connectionString); + + const sql = postgres(connectionString, { max: 1, onnotice: () => {} }); + try { + const illegalToadHash = await migrationHash("0044_illegal_toad.sql"); + + await sql.unsafe( + `DELETE FROM "drizzle"."__drizzle_migrations" WHERE hash = '${illegalToadHash}'`, + ); + + const columns = await sql.unsafe<{ column_name: string }[]>( + ` + SELECT column_name + FROM information_schema.columns + WHERE table_schema = 'public' + AND table_name = 'instance_settings' + AND column_name = 'general' + `, + ); + expect(columns).toHaveLength(1); + } finally { + await sql.end(); + } + + const pendingState = await inspectMigrations(connectionString); + expect(pendingState).toMatchObject({ + status: "needsMigrations", + pendingMigrations: ["0044_illegal_toad.sql"], + reason: "pending-migrations", + }); + + await applyPendingMigrations(connectionString); + + const finalState = await inspectMigrations(connectionString); + expect(finalState.status).toBe("upToDate"); + }, + 20_000, + ); + + it( + "enforces a unique board_api_keys.key_hash after migration 0044", + async () => { + const connectionString = await createTempDatabase(); + + await applyPendingMigrations(connectionString); + + const sql = postgres(connectionString, { max: 1, onnotice: () => {} }); + try { + await sql.unsafe(` + INSERT INTO "user" ("id", "name", "email", "email_verified", "created_at", "updated_at") + VALUES ('user-1', 'User One', 'user@example.com', true, now(), now()) + `); + await sql.unsafe(` + INSERT INTO "board_api_keys" ("id", "user_id", "name", "key_hash", "created_at") + VALUES ('00000000-0000-0000-0000-000000000001', 'user-1', 'Key One', 'dup-hash', now()) + `); + await expect( + sql.unsafe(` + INSERT INTO "board_api_keys" ("id", "user_id", "name", "key_hash", "created_at") + VALUES ('00000000-0000-0000-0000-000000000002', 'user-1', 'Key Two', 'dup-hash', now()) + `), + ).rejects.toThrow(); + } finally { + await sql.end(); + } + }, + 20_000, + ); }); diff --git a/packages/db/src/migration-runtime.ts b/packages/db/src/migration-runtime.ts index 3b5921b14d..921de612a3 100644 --- a/packages/db/src/migration-runtime.ts +++ b/packages/db/src/migration-runtime.ts @@ -150,7 +150,7 @@ async function ensureEmbeddedPostgresConnection( password: "paperclip", port: selectedPort, persistent: true, - initdbFlags: ["--encoding=UTF8", "--locale=C"], + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], onLog: () => {}, onError: () => {}, }); diff --git a/packages/db/src/migrations/0044_illegal_toad.sql b/packages/db/src/migrations/0044_illegal_toad.sql new file mode 100644 index 0000000000..5f1f18bda4 --- /dev/null +++ b/packages/db/src/migrations/0044_illegal_toad.sql @@ -0,0 +1,56 @@ +CREATE TABLE IF NOT EXISTS "board_api_keys" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "user_id" text NOT NULL, + "name" text NOT NULL, + "key_hash" text NOT NULL, + "last_used_at" timestamp with time zone, + "revoked_at" timestamp with time zone, + "expires_at" timestamp with time zone, + "created_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE TABLE IF NOT EXISTS "cli_auth_challenges" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "secret_hash" text NOT NULL, + "command" text NOT NULL, + "client_name" text, + "requested_access" text DEFAULT 'board' NOT NULL, + "requested_company_id" uuid, + "pending_key_hash" text NOT NULL, + "pending_key_name" text NOT NULL, + "approved_by_user_id" text, + "board_api_key_id" uuid, + "approved_at" timestamp with time zone, + "cancelled_at" timestamp with time zone, + "expires_at" timestamp with time zone NOT NULL, + "created_at" timestamp with time zone DEFAULT now() NOT NULL, + "updated_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +ALTER TABLE "instance_settings" ADD COLUMN IF NOT EXISTS "general" jsonb DEFAULT '{}'::jsonb NOT NULL;--> statement-breakpoint +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'board_api_keys_user_id_user_id_fk') THEN + ALTER TABLE "board_api_keys" ADD CONSTRAINT "board_api_keys_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action; + END IF; +END $$;--> statement-breakpoint +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'cli_auth_challenges_requested_company_id_companies_id_fk') THEN + ALTER TABLE "cli_auth_challenges" ADD CONSTRAINT "cli_auth_challenges_requested_company_id_companies_id_fk" FOREIGN KEY ("requested_company_id") REFERENCES "public"."companies"("id") ON DELETE set null ON UPDATE no action; + END IF; +END $$;--> statement-breakpoint +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'cli_auth_challenges_approved_by_user_id_user_id_fk') THEN + ALTER TABLE "cli_auth_challenges" ADD CONSTRAINT "cli_auth_challenges_approved_by_user_id_user_id_fk" FOREIGN KEY ("approved_by_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action; + END IF; +END $$;--> statement-breakpoint +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'cli_auth_challenges_board_api_key_id_board_api_keys_id_fk') THEN + ALTER TABLE "cli_auth_challenges" ADD CONSTRAINT "cli_auth_challenges_board_api_key_id_board_api_keys_id_fk" FOREIGN KEY ("board_api_key_id") REFERENCES "public"."board_api_keys"("id") ON DELETE set null ON UPDATE no action; + END IF; +END $$;--> statement-breakpoint +DROP INDEX IF EXISTS "board_api_keys_key_hash_idx";--> statement-breakpoint +CREATE UNIQUE INDEX IF NOT EXISTS "board_api_keys_key_hash_idx" ON "board_api_keys" USING btree ("key_hash");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "board_api_keys_user_idx" ON "board_api_keys" USING btree ("user_id");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "cli_auth_challenges_secret_hash_idx" ON "cli_auth_challenges" USING btree ("secret_hash");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "cli_auth_challenges_approved_by_idx" ON "cli_auth_challenges" USING btree ("approved_by_user_id");--> statement-breakpoint +CREATE INDEX IF NOT EXISTS "cli_auth_challenges_requested_company_idx" ON "cli_auth_challenges" USING btree ("requested_company_id"); diff --git a/packages/db/src/migrations/meta/0044_snapshot.json b/packages/db/src/migrations/meta/0044_snapshot.json new file mode 100644 index 0000000000..5ca6f818a6 --- /dev/null +++ b/packages/db/src/migrations/meta/0044_snapshot.json @@ -0,0 +1,11701 @@ +{ + "id": "a7a034eb-984f-4884-b6e1-87c453404b4e", + "prevId": "c49c6ac1-3acd-4a7b-91e5-5ad193b154a5", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.activity_log": { + "name": "activity_log", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "actor_type": { + "name": "actor_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'system'" + }, + "actor_id": { + "name": "actor_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "action": { + "name": "action", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "entity_type": { + "name": "entity_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "entity_id": { + "name": "entity_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "run_id": { + "name": "run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "details": { + "name": "details", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "activity_log_company_created_idx": { + "name": "activity_log_company_created_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "activity_log_run_id_idx": { + "name": "activity_log_run_id_idx", + "columns": [ + { + "expression": "run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "activity_log_entity_type_id_idx": { + "name": "activity_log_entity_type_id_idx", + "columns": [ + { + "expression": "entity_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "entity_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "activity_log_company_id_companies_id_fk": { + "name": "activity_log_company_id_companies_id_fk", + "tableFrom": "activity_log", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "activity_log_agent_id_agents_id_fk": { + "name": "activity_log_agent_id_agents_id_fk", + "tableFrom": "activity_log", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "activity_log_run_id_heartbeat_runs_id_fk": { + "name": "activity_log_run_id_heartbeat_runs_id_fk", + "tableFrom": "activity_log", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.agent_api_keys": { + "name": "agent_api_keys", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "key_hash": { + "name": "key_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "last_used_at": { + "name": "last_used_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "revoked_at": { + "name": "revoked_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "agent_api_keys_key_hash_idx": { + "name": "agent_api_keys_key_hash_idx", + "columns": [ + { + "expression": "key_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agent_api_keys_company_agent_idx": { + "name": "agent_api_keys_company_agent_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "agent_api_keys_agent_id_agents_id_fk": { + "name": "agent_api_keys_agent_id_agents_id_fk", + "tableFrom": "agent_api_keys", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "agent_api_keys_company_id_companies_id_fk": { + "name": "agent_api_keys_company_id_companies_id_fk", + "tableFrom": "agent_api_keys", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.agent_config_revisions": { + "name": "agent_config_revisions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'patch'" + }, + "rolled_back_from_revision_id": { + "name": "rolled_back_from_revision_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "changed_keys": { + "name": "changed_keys", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "before_config": { + "name": "before_config", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "after_config": { + "name": "after_config", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "agent_config_revisions_company_agent_created_idx": { + "name": "agent_config_revisions_company_agent_created_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agent_config_revisions_agent_created_idx": { + "name": "agent_config_revisions_agent_created_idx", + "columns": [ + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "agent_config_revisions_company_id_companies_id_fk": { + "name": "agent_config_revisions_company_id_companies_id_fk", + "tableFrom": "agent_config_revisions", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "agent_config_revisions_agent_id_agents_id_fk": { + "name": "agent_config_revisions_agent_id_agents_id_fk", + "tableFrom": "agent_config_revisions", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "agent_config_revisions_created_by_agent_id_agents_id_fk": { + "name": "agent_config_revisions_created_by_agent_id_agents_id_fk", + "tableFrom": "agent_config_revisions", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.agent_runtime_state": { + "name": "agent_runtime_state", + "schema": "", + "columns": { + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "adapter_type": { + "name": "adapter_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "session_id": { + "name": "session_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "state_json": { + "name": "state_json", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "last_run_id": { + "name": "last_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "last_run_status": { + "name": "last_run_status", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "total_input_tokens": { + "name": "total_input_tokens", + "type": "bigint", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "total_output_tokens": { + "name": "total_output_tokens", + "type": "bigint", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "total_cached_input_tokens": { + "name": "total_cached_input_tokens", + "type": "bigint", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "total_cost_cents": { + "name": "total_cost_cents", + "type": "bigint", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "last_error": { + "name": "last_error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "agent_runtime_state_company_agent_idx": { + "name": "agent_runtime_state_company_agent_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agent_runtime_state_company_updated_idx": { + "name": "agent_runtime_state_company_updated_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "agent_runtime_state_agent_id_agents_id_fk": { + "name": "agent_runtime_state_agent_id_agents_id_fk", + "tableFrom": "agent_runtime_state", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "agent_runtime_state_company_id_companies_id_fk": { + "name": "agent_runtime_state_company_id_companies_id_fk", + "tableFrom": "agent_runtime_state", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.agent_task_sessions": { + "name": "agent_task_sessions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "adapter_type": { + "name": "adapter_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "task_key": { + "name": "task_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "session_params_json": { + "name": "session_params_json", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "session_display_id": { + "name": "session_display_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "last_run_id": { + "name": "last_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "last_error": { + "name": "last_error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "agent_task_sessions_company_agent_adapter_task_uniq": { + "name": "agent_task_sessions_company_agent_adapter_task_uniq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "adapter_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "task_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agent_task_sessions_company_agent_updated_idx": { + "name": "agent_task_sessions_company_agent_updated_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agent_task_sessions_company_task_updated_idx": { + "name": "agent_task_sessions_company_task_updated_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "task_key", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "agent_task_sessions_company_id_companies_id_fk": { + "name": "agent_task_sessions_company_id_companies_id_fk", + "tableFrom": "agent_task_sessions", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "agent_task_sessions_agent_id_agents_id_fk": { + "name": "agent_task_sessions_agent_id_agents_id_fk", + "tableFrom": "agent_task_sessions", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "agent_task_sessions_last_run_id_heartbeat_runs_id_fk": { + "name": "agent_task_sessions_last_run_id_heartbeat_runs_id_fk", + "tableFrom": "agent_task_sessions", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "last_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.agent_wakeup_requests": { + "name": "agent_wakeup_requests", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "trigger_detail": { + "name": "trigger_detail", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "payload": { + "name": "payload", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'queued'" + }, + "coalesced_count": { + "name": "coalesced_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "requested_by_actor_type": { + "name": "requested_by_actor_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "requested_by_actor_id": { + "name": "requested_by_actor_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "idempotency_key": { + "name": "idempotency_key", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "run_id": { + "name": "run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "requested_at": { + "name": "requested_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "claimed_at": { + "name": "claimed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "finished_at": { + "name": "finished_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "agent_wakeup_requests_company_agent_status_idx": { + "name": "agent_wakeup_requests_company_agent_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agent_wakeup_requests_company_requested_idx": { + "name": "agent_wakeup_requests_company_requested_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "requested_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agent_wakeup_requests_agent_requested_idx": { + "name": "agent_wakeup_requests_agent_requested_idx", + "columns": [ + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "requested_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "agent_wakeup_requests_company_id_companies_id_fk": { + "name": "agent_wakeup_requests_company_id_companies_id_fk", + "tableFrom": "agent_wakeup_requests", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "agent_wakeup_requests_agent_id_agents_id_fk": { + "name": "agent_wakeup_requests_agent_id_agents_id_fk", + "tableFrom": "agent_wakeup_requests", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.agents": { + "name": "agents", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'general'" + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'idle'" + }, + "reports_to": { + "name": "reports_to", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "capabilities": { + "name": "capabilities", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "adapter_type": { + "name": "adapter_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'process'" + }, + "adapter_config": { + "name": "adapter_config", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "runtime_config": { + "name": "runtime_config", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "budget_monthly_cents": { + "name": "budget_monthly_cents", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "spent_monthly_cents": { + "name": "spent_monthly_cents", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "pause_reason": { + "name": "pause_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "paused_at": { + "name": "paused_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "permissions": { + "name": "permissions", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "last_heartbeat_at": { + "name": "last_heartbeat_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "agents_company_status_idx": { + "name": "agents_company_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "agents_company_reports_to_idx": { + "name": "agents_company_reports_to_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "reports_to", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "agents_company_id_companies_id_fk": { + "name": "agents_company_id_companies_id_fk", + "tableFrom": "agents", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "agents_reports_to_agents_id_fk": { + "name": "agents_reports_to_agents_id_fk", + "tableFrom": "agents", + "tableTo": "agents", + "columnsFrom": [ + "reports_to" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.approval_comments": { + "name": "approval_comments", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "approval_id": { + "name": "approval_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "author_agent_id": { + "name": "author_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "author_user_id": { + "name": "author_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "body": { + "name": "body", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "approval_comments_company_idx": { + "name": "approval_comments_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "approval_comments_approval_idx": { + "name": "approval_comments_approval_idx", + "columns": [ + { + "expression": "approval_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "approval_comments_approval_created_idx": { + "name": "approval_comments_approval_created_idx", + "columns": [ + { + "expression": "approval_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "approval_comments_company_id_companies_id_fk": { + "name": "approval_comments_company_id_companies_id_fk", + "tableFrom": "approval_comments", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "approval_comments_approval_id_approvals_id_fk": { + "name": "approval_comments_approval_id_approvals_id_fk", + "tableFrom": "approval_comments", + "tableTo": "approvals", + "columnsFrom": [ + "approval_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "approval_comments_author_agent_id_agents_id_fk": { + "name": "approval_comments_author_agent_id_agents_id_fk", + "tableFrom": "approval_comments", + "tableTo": "agents", + "columnsFrom": [ + "author_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.approvals": { + "name": "approvals", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "requested_by_agent_id": { + "name": "requested_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "requested_by_user_id": { + "name": "requested_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "payload": { + "name": "payload", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "decision_note": { + "name": "decision_note", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "decided_by_user_id": { + "name": "decided_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "decided_at": { + "name": "decided_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "approvals_company_status_type_idx": { + "name": "approvals_company_status_type_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "approvals_company_id_companies_id_fk": { + "name": "approvals_company_id_companies_id_fk", + "tableFrom": "approvals", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "approvals_requested_by_agent_id_agents_id_fk": { + "name": "approvals_requested_by_agent_id_agents_id_fk", + "tableFrom": "approvals", + "tableTo": "agents", + "columnsFrom": [ + "requested_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.assets": { + "name": "assets", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "object_key": { + "name": "object_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "content_type": { + "name": "content_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "byte_size": { + "name": "byte_size", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "sha256": { + "name": "sha256", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "original_filename": { + "name": "original_filename", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "assets_company_created_idx": { + "name": "assets_company_created_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "assets_company_provider_idx": { + "name": "assets_company_provider_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "provider", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "assets_company_object_key_uq": { + "name": "assets_company_object_key_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "object_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "assets_company_id_companies_id_fk": { + "name": "assets_company_id_companies_id_fk", + "tableFrom": "assets", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "assets_created_by_agent_id_agents_id_fk": { + "name": "assets_created_by_agent_id_agents_id_fk", + "tableFrom": "assets", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.account": { + "name": "account", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "access_token": { + "name": "access_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "id_token": { + "name": "id_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "access_token_expires_at": { + "name": "access_token_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "refresh_token_expires_at": { + "name": "refresh_token_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "account_user_id_user_id_fk": { + "name": "account_user_id_user_id_fk", + "tableFrom": "account", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.session": { + "name": "session", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "ip_address": { + "name": "ip_address", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_agent": { + "name": "user_agent", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "session_user_id_user_id_fk": { + "name": "session_user_id_user_id_fk", + "tableFrom": "session", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email_verified": { + "name": "email_verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "image": { + "name": "image", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.verification": { + "name": "verification", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "identifier": { + "name": "identifier", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.board_api_keys": { + "name": "board_api_keys", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "key_hash": { + "name": "key_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "last_used_at": { + "name": "last_used_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "revoked_at": { + "name": "revoked_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "board_api_keys_key_hash_idx": { + "name": "board_api_keys_key_hash_idx", + "columns": [ + { + "expression": "key_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "board_api_keys_user_idx": { + "name": "board_api_keys_user_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "board_api_keys_user_id_user_id_fk": { + "name": "board_api_keys_user_id_user_id_fk", + "tableFrom": "board_api_keys", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.budget_incidents": { + "name": "budget_incidents", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "policy_id": { + "name": "policy_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "scope_type": { + "name": "scope_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope_id": { + "name": "scope_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "metric": { + "name": "metric", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "window_kind": { + "name": "window_kind", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "window_start": { + "name": "window_start", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "window_end": { + "name": "window_end", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "threshold_type": { + "name": "threshold_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "amount_limit": { + "name": "amount_limit", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "amount_observed": { + "name": "amount_observed", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'open'" + }, + "approval_id": { + "name": "approval_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "resolved_at": { + "name": "resolved_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "budget_incidents_company_status_idx": { + "name": "budget_incidents_company_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "budget_incidents_company_scope_idx": { + "name": "budget_incidents_company_scope_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "budget_incidents_policy_window_threshold_idx": { + "name": "budget_incidents_policy_window_threshold_idx", + "columns": [ + { + "expression": "policy_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "window_start", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "threshold_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "where": "\"budget_incidents\".\"status\" <> 'dismissed'", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "budget_incidents_company_id_companies_id_fk": { + "name": "budget_incidents_company_id_companies_id_fk", + "tableFrom": "budget_incidents", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "budget_incidents_policy_id_budget_policies_id_fk": { + "name": "budget_incidents_policy_id_budget_policies_id_fk", + "tableFrom": "budget_incidents", + "tableTo": "budget_policies", + "columnsFrom": [ + "policy_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "budget_incidents_approval_id_approvals_id_fk": { + "name": "budget_incidents_approval_id_approvals_id_fk", + "tableFrom": "budget_incidents", + "tableTo": "approvals", + "columnsFrom": [ + "approval_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.budget_policies": { + "name": "budget_policies", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "scope_type": { + "name": "scope_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope_id": { + "name": "scope_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "metric": { + "name": "metric", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'billed_cents'" + }, + "window_kind": { + "name": "window_kind", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "amount": { + "name": "amount", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "warn_percent": { + "name": "warn_percent", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 80 + }, + "hard_stop_enabled": { + "name": "hard_stop_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "notify_enabled": { + "name": "notify_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "is_active": { + "name": "is_active", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_by_user_id": { + "name": "updated_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "budget_policies_company_scope_active_idx": { + "name": "budget_policies_company_scope_active_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "is_active", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "budget_policies_company_window_idx": { + "name": "budget_policies_company_window_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "window_kind", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "metric", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "budget_policies_company_scope_metric_unique_idx": { + "name": "budget_policies_company_scope_metric_unique_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "metric", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "window_kind", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "budget_policies_company_id_companies_id_fk": { + "name": "budget_policies_company_id_companies_id_fk", + "tableFrom": "budget_policies", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.cli_auth_challenges": { + "name": "cli_auth_challenges", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "secret_hash": { + "name": "secret_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "command": { + "name": "command", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "client_name": { + "name": "client_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "requested_access": { + "name": "requested_access", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'board'" + }, + "requested_company_id": { + "name": "requested_company_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "pending_key_hash": { + "name": "pending_key_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "pending_key_name": { + "name": "pending_key_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "approved_by_user_id": { + "name": "approved_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "board_api_key_id": { + "name": "board_api_key_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "approved_at": { + "name": "approved_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "cancelled_at": { + "name": "cancelled_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "cli_auth_challenges_secret_hash_idx": { + "name": "cli_auth_challenges_secret_hash_idx", + "columns": [ + { + "expression": "secret_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cli_auth_challenges_approved_by_idx": { + "name": "cli_auth_challenges_approved_by_idx", + "columns": [ + { + "expression": "approved_by_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cli_auth_challenges_requested_company_idx": { + "name": "cli_auth_challenges_requested_company_idx", + "columns": [ + { + "expression": "requested_company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "cli_auth_challenges_requested_company_id_companies_id_fk": { + "name": "cli_auth_challenges_requested_company_id_companies_id_fk", + "tableFrom": "cli_auth_challenges", + "tableTo": "companies", + "columnsFrom": [ + "requested_company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "cli_auth_challenges_approved_by_user_id_user_id_fk": { + "name": "cli_auth_challenges_approved_by_user_id_user_id_fk", + "tableFrom": "cli_auth_challenges", + "tableTo": "user", + "columnsFrom": [ + "approved_by_user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "cli_auth_challenges_board_api_key_id_board_api_keys_id_fk": { + "name": "cli_auth_challenges_board_api_key_id_board_api_keys_id_fk", + "tableFrom": "cli_auth_challenges", + "tableTo": "board_api_keys", + "columnsFrom": [ + "board_api_key_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.companies": { + "name": "companies", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'active'" + }, + "pause_reason": { + "name": "pause_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "paused_at": { + "name": "paused_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "issue_prefix": { + "name": "issue_prefix", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'PAP'" + }, + "issue_counter": { + "name": "issue_counter", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "budget_monthly_cents": { + "name": "budget_monthly_cents", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "spent_monthly_cents": { + "name": "spent_monthly_cents", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "require_board_approval_for_new_agents": { + "name": "require_board_approval_for_new_agents", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "brand_color": { + "name": "brand_color", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "companies_issue_prefix_idx": { + "name": "companies_issue_prefix_idx", + "columns": [ + { + "expression": "issue_prefix", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.company_logos": { + "name": "company_logos", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "asset_id": { + "name": "asset_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "company_logos_company_uq": { + "name": "company_logos_company_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_logos_asset_uq": { + "name": "company_logos_asset_uq", + "columns": [ + { + "expression": "asset_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "company_logos_company_id_companies_id_fk": { + "name": "company_logos_company_id_companies_id_fk", + "tableFrom": "company_logos", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "company_logos_asset_id_assets_id_fk": { + "name": "company_logos_asset_id_assets_id_fk", + "tableFrom": "company_logos", + "tableTo": "assets", + "columnsFrom": [ + "asset_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.company_memberships": { + "name": "company_memberships", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "principal_type": { + "name": "principal_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "principal_id": { + "name": "principal_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'active'" + }, + "membership_role": { + "name": "membership_role", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "company_memberships_company_principal_unique_idx": { + "name": "company_memberships_company_principal_unique_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "principal_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "principal_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_memberships_principal_status_idx": { + "name": "company_memberships_principal_status_idx", + "columns": [ + { + "expression": "principal_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "principal_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_memberships_company_status_idx": { + "name": "company_memberships_company_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "company_memberships_company_id_companies_id_fk": { + "name": "company_memberships_company_id_companies_id_fk", + "tableFrom": "company_memberships", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.company_secret_versions": { + "name": "company_secret_versions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "secret_id": { + "name": "secret_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "version": { + "name": "version", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "material": { + "name": "material", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "value_sha256": { + "name": "value_sha256", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "revoked_at": { + "name": "revoked_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "company_secret_versions_secret_idx": { + "name": "company_secret_versions_secret_idx", + "columns": [ + { + "expression": "secret_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_secret_versions_value_sha256_idx": { + "name": "company_secret_versions_value_sha256_idx", + "columns": [ + { + "expression": "value_sha256", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_secret_versions_secret_version_uq": { + "name": "company_secret_versions_secret_version_uq", + "columns": [ + { + "expression": "secret_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "version", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "company_secret_versions_secret_id_company_secrets_id_fk": { + "name": "company_secret_versions_secret_id_company_secrets_id_fk", + "tableFrom": "company_secret_versions", + "tableTo": "company_secrets", + "columnsFrom": [ + "secret_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "company_secret_versions_created_by_agent_id_agents_id_fk": { + "name": "company_secret_versions_created_by_agent_id_agents_id_fk", + "tableFrom": "company_secret_versions", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.company_secrets": { + "name": "company_secrets", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'local_encrypted'" + }, + "external_ref": { + "name": "external_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "latest_version": { + "name": "latest_version", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 1 + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "company_secrets_company_idx": { + "name": "company_secrets_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_secrets_company_provider_idx": { + "name": "company_secrets_company_provider_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "provider", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_secrets_company_name_uq": { + "name": "company_secrets_company_name_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "company_secrets_company_id_companies_id_fk": { + "name": "company_secrets_company_id_companies_id_fk", + "tableFrom": "company_secrets", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "company_secrets_created_by_agent_id_agents_id_fk": { + "name": "company_secrets_created_by_agent_id_agents_id_fk", + "tableFrom": "company_secrets", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.company_skills": { + "name": "company_skills", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "key": { + "name": "key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "slug": { + "name": "slug", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "markdown": { + "name": "markdown", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "source_type": { + "name": "source_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'local_path'" + }, + "source_locator": { + "name": "source_locator", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "source_ref": { + "name": "source_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "trust_level": { + "name": "trust_level", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'markdown_only'" + }, + "compatibility": { + "name": "compatibility", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'compatible'" + }, + "file_inventory": { + "name": "file_inventory", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "company_skills_company_key_idx": { + "name": "company_skills_company_key_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "company_skills_company_name_idx": { + "name": "company_skills_company_name_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "company_skills_company_id_companies_id_fk": { + "name": "company_skills_company_id_companies_id_fk", + "tableFrom": "company_skills", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.cost_events": { + "name": "cost_events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "goal_id": { + "name": "goal_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "heartbeat_run_id": { + "name": "heartbeat_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "billing_code": { + "name": "billing_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "biller": { + "name": "biller", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'unknown'" + }, + "billing_type": { + "name": "billing_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'unknown'" + }, + "model": { + "name": "model", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "input_tokens": { + "name": "input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "cached_input_tokens": { + "name": "cached_input_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "output_tokens": { + "name": "output_tokens", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "cost_cents": { + "name": "cost_cents", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "occurred_at": { + "name": "occurred_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "cost_events_company_occurred_idx": { + "name": "cost_events_company_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cost_events_company_agent_occurred_idx": { + "name": "cost_events_company_agent_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cost_events_company_provider_occurred_idx": { + "name": "cost_events_company_provider_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "provider", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cost_events_company_biller_occurred_idx": { + "name": "cost_events_company_biller_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "biller", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "cost_events_company_heartbeat_run_idx": { + "name": "cost_events_company_heartbeat_run_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "heartbeat_run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "cost_events_company_id_companies_id_fk": { + "name": "cost_events_company_id_companies_id_fk", + "tableFrom": "cost_events", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cost_events_agent_id_agents_id_fk": { + "name": "cost_events_agent_id_agents_id_fk", + "tableFrom": "cost_events", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cost_events_issue_id_issues_id_fk": { + "name": "cost_events_issue_id_issues_id_fk", + "tableFrom": "cost_events", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cost_events_project_id_projects_id_fk": { + "name": "cost_events_project_id_projects_id_fk", + "tableFrom": "cost_events", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cost_events_goal_id_goals_id_fk": { + "name": "cost_events_goal_id_goals_id_fk", + "tableFrom": "cost_events", + "tableTo": "goals", + "columnsFrom": [ + "goal_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "cost_events_heartbeat_run_id_heartbeat_runs_id_fk": { + "name": "cost_events_heartbeat_run_id_heartbeat_runs_id_fk", + "tableFrom": "cost_events", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "heartbeat_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.document_revisions": { + "name": "document_revisions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "document_id": { + "name": "document_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "revision_number": { + "name": "revision_number", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "body": { + "name": "body", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "change_summary": { + "name": "change_summary", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "document_revisions_document_revision_uq": { + "name": "document_revisions_document_revision_uq", + "columns": [ + { + "expression": "document_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "revision_number", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "document_revisions_company_document_created_idx": { + "name": "document_revisions_company_document_created_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "document_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "document_revisions_company_id_companies_id_fk": { + "name": "document_revisions_company_id_companies_id_fk", + "tableFrom": "document_revisions", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "document_revisions_document_id_documents_id_fk": { + "name": "document_revisions_document_id_documents_id_fk", + "tableFrom": "document_revisions", + "tableTo": "documents", + "columnsFrom": [ + "document_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "document_revisions_created_by_agent_id_agents_id_fk": { + "name": "document_revisions_created_by_agent_id_agents_id_fk", + "tableFrom": "document_revisions", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.documents": { + "name": "documents", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "format": { + "name": "format", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'markdown'" + }, + "latest_body": { + "name": "latest_body", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "latest_revision_id": { + "name": "latest_revision_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "latest_revision_number": { + "name": "latest_revision_number", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 1 + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_by_agent_id": { + "name": "updated_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "updated_by_user_id": { + "name": "updated_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "documents_company_updated_idx": { + "name": "documents_company_updated_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "documents_company_created_idx": { + "name": "documents_company_created_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "documents_company_id_companies_id_fk": { + "name": "documents_company_id_companies_id_fk", + "tableFrom": "documents", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "documents_created_by_agent_id_agents_id_fk": { + "name": "documents_created_by_agent_id_agents_id_fk", + "tableFrom": "documents", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "documents_updated_by_agent_id_agents_id_fk": { + "name": "documents_updated_by_agent_id_agents_id_fk", + "tableFrom": "documents", + "tableTo": "agents", + "columnsFrom": [ + "updated_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.execution_workspaces": { + "name": "execution_workspaces", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "project_workspace_id": { + "name": "project_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "source_issue_id": { + "name": "source_issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "mode": { + "name": "mode", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "strategy_type": { + "name": "strategy_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'active'" + }, + "cwd": { + "name": "cwd", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "repo_url": { + "name": "repo_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "base_ref": { + "name": "base_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "branch_name": { + "name": "branch_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "provider_type": { + "name": "provider_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'local_fs'" + }, + "provider_ref": { + "name": "provider_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "derived_from_execution_workspace_id": { + "name": "derived_from_execution_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "last_used_at": { + "name": "last_used_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "opened_at": { + "name": "opened_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "closed_at": { + "name": "closed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "cleanup_eligible_at": { + "name": "cleanup_eligible_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "cleanup_reason": { + "name": "cleanup_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "execution_workspaces_company_project_status_idx": { + "name": "execution_workspaces_company_project_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "execution_workspaces_company_project_workspace_status_idx": { + "name": "execution_workspaces_company_project_workspace_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "execution_workspaces_company_source_issue_idx": { + "name": "execution_workspaces_company_source_issue_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "source_issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "execution_workspaces_company_last_used_idx": { + "name": "execution_workspaces_company_last_used_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "last_used_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "execution_workspaces_company_branch_idx": { + "name": "execution_workspaces_company_branch_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "branch_name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "execution_workspaces_company_id_companies_id_fk": { + "name": "execution_workspaces_company_id_companies_id_fk", + "tableFrom": "execution_workspaces", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "execution_workspaces_project_id_projects_id_fk": { + "name": "execution_workspaces_project_id_projects_id_fk", + "tableFrom": "execution_workspaces", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "execution_workspaces_project_workspace_id_project_workspaces_id_fk": { + "name": "execution_workspaces_project_workspace_id_project_workspaces_id_fk", + "tableFrom": "execution_workspaces", + "tableTo": "project_workspaces", + "columnsFrom": [ + "project_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "execution_workspaces_source_issue_id_issues_id_fk": { + "name": "execution_workspaces_source_issue_id_issues_id_fk", + "tableFrom": "execution_workspaces", + "tableTo": "issues", + "columnsFrom": [ + "source_issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "execution_workspaces_derived_from_execution_workspace_id_execution_workspaces_id_fk": { + "name": "execution_workspaces_derived_from_execution_workspace_id_execution_workspaces_id_fk", + "tableFrom": "execution_workspaces", + "tableTo": "execution_workspaces", + "columnsFrom": [ + "derived_from_execution_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.finance_events": { + "name": "finance_events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "goal_id": { + "name": "goal_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "heartbeat_run_id": { + "name": "heartbeat_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "cost_event_id": { + "name": "cost_event_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "billing_code": { + "name": "billing_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "event_kind": { + "name": "event_kind", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "direction": { + "name": "direction", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'debit'" + }, + "biller": { + "name": "biller", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "execution_adapter_type": { + "name": "execution_adapter_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "pricing_tier": { + "name": "pricing_tier", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "region": { + "name": "region", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "model": { + "name": "model", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "quantity": { + "name": "quantity", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "unit": { + "name": "unit", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "amount_cents": { + "name": "amount_cents", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "currency": { + "name": "currency", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'USD'" + }, + "estimated": { + "name": "estimated", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "external_invoice_id": { + "name": "external_invoice_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "metadata_json": { + "name": "metadata_json", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "occurred_at": { + "name": "occurred_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "finance_events_company_occurred_idx": { + "name": "finance_events_company_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "finance_events_company_biller_occurred_idx": { + "name": "finance_events_company_biller_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "biller", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "finance_events_company_kind_occurred_idx": { + "name": "finance_events_company_kind_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "event_kind", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "finance_events_company_direction_occurred_idx": { + "name": "finance_events_company_direction_occurred_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "direction", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "occurred_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "finance_events_company_heartbeat_run_idx": { + "name": "finance_events_company_heartbeat_run_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "heartbeat_run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "finance_events_company_cost_event_idx": { + "name": "finance_events_company_cost_event_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "cost_event_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "finance_events_company_id_companies_id_fk": { + "name": "finance_events_company_id_companies_id_fk", + "tableFrom": "finance_events", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "finance_events_agent_id_agents_id_fk": { + "name": "finance_events_agent_id_agents_id_fk", + "tableFrom": "finance_events", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "finance_events_issue_id_issues_id_fk": { + "name": "finance_events_issue_id_issues_id_fk", + "tableFrom": "finance_events", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "finance_events_project_id_projects_id_fk": { + "name": "finance_events_project_id_projects_id_fk", + "tableFrom": "finance_events", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "finance_events_goal_id_goals_id_fk": { + "name": "finance_events_goal_id_goals_id_fk", + "tableFrom": "finance_events", + "tableTo": "goals", + "columnsFrom": [ + "goal_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "finance_events_heartbeat_run_id_heartbeat_runs_id_fk": { + "name": "finance_events_heartbeat_run_id_heartbeat_runs_id_fk", + "tableFrom": "finance_events", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "heartbeat_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "finance_events_cost_event_id_cost_events_id_fk": { + "name": "finance_events_cost_event_id_cost_events_id_fk", + "tableFrom": "finance_events", + "tableTo": "cost_events", + "columnsFrom": [ + "cost_event_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.goals": { + "name": "goals", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "level": { + "name": "level", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'task'" + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'planned'" + }, + "parent_id": { + "name": "parent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "owner_agent_id": { + "name": "owner_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "goals_company_idx": { + "name": "goals_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "goals_company_id_companies_id_fk": { + "name": "goals_company_id_companies_id_fk", + "tableFrom": "goals", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "goals_parent_id_goals_id_fk": { + "name": "goals_parent_id_goals_id_fk", + "tableFrom": "goals", + "tableTo": "goals", + "columnsFrom": [ + "parent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "goals_owner_agent_id_agents_id_fk": { + "name": "goals_owner_agent_id_agents_id_fk", + "tableFrom": "goals", + "tableTo": "agents", + "columnsFrom": [ + "owner_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.heartbeat_run_events": { + "name": "heartbeat_run_events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "bigserial", + "primaryKey": true, + "notNull": true + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "run_id": { + "name": "run_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "seq": { + "name": "seq", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "event_type": { + "name": "event_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "stream": { + "name": "stream", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "level": { + "name": "level", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "color": { + "name": "color", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "payload": { + "name": "payload", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "heartbeat_run_events_run_seq_idx": { + "name": "heartbeat_run_events_run_seq_idx", + "columns": [ + { + "expression": "run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "seq", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "heartbeat_run_events_company_run_idx": { + "name": "heartbeat_run_events_company_run_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "heartbeat_run_events_company_created_idx": { + "name": "heartbeat_run_events_company_created_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "heartbeat_run_events_company_id_companies_id_fk": { + "name": "heartbeat_run_events_company_id_companies_id_fk", + "tableFrom": "heartbeat_run_events", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "heartbeat_run_events_run_id_heartbeat_runs_id_fk": { + "name": "heartbeat_run_events_run_id_heartbeat_runs_id_fk", + "tableFrom": "heartbeat_run_events", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "heartbeat_run_events_agent_id_agents_id_fk": { + "name": "heartbeat_run_events_agent_id_agents_id_fk", + "tableFrom": "heartbeat_run_events", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.heartbeat_runs": { + "name": "heartbeat_runs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "invocation_source": { + "name": "invocation_source", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'on_demand'" + }, + "trigger_detail": { + "name": "trigger_detail", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'queued'" + }, + "started_at": { + "name": "started_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "finished_at": { + "name": "finished_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "wakeup_request_id": { + "name": "wakeup_request_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "exit_code": { + "name": "exit_code", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "signal": { + "name": "signal", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "usage_json": { + "name": "usage_json", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "result_json": { + "name": "result_json", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "session_id_before": { + "name": "session_id_before", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "session_id_after": { + "name": "session_id_after", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "log_store": { + "name": "log_store", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "log_ref": { + "name": "log_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "log_bytes": { + "name": "log_bytes", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "log_sha256": { + "name": "log_sha256", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "log_compressed": { + "name": "log_compressed", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "stdout_excerpt": { + "name": "stdout_excerpt", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "stderr_excerpt": { + "name": "stderr_excerpt", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "error_code": { + "name": "error_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "external_run_id": { + "name": "external_run_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "process_pid": { + "name": "process_pid", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "process_started_at": { + "name": "process_started_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "retry_of_run_id": { + "name": "retry_of_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "process_loss_retry_count": { + "name": "process_loss_retry_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "context_snapshot": { + "name": "context_snapshot", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "heartbeat_runs_company_agent_started_idx": { + "name": "heartbeat_runs_company_agent_started_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "started_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "heartbeat_runs_company_id_companies_id_fk": { + "name": "heartbeat_runs_company_id_companies_id_fk", + "tableFrom": "heartbeat_runs", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "heartbeat_runs_agent_id_agents_id_fk": { + "name": "heartbeat_runs_agent_id_agents_id_fk", + "tableFrom": "heartbeat_runs", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "heartbeat_runs_wakeup_request_id_agent_wakeup_requests_id_fk": { + "name": "heartbeat_runs_wakeup_request_id_agent_wakeup_requests_id_fk", + "tableFrom": "heartbeat_runs", + "tableTo": "agent_wakeup_requests", + "columnsFrom": [ + "wakeup_request_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "heartbeat_runs_retry_of_run_id_heartbeat_runs_id_fk": { + "name": "heartbeat_runs_retry_of_run_id_heartbeat_runs_id_fk", + "tableFrom": "heartbeat_runs", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "retry_of_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.instance_settings": { + "name": "instance_settings", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "singleton_key": { + "name": "singleton_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'default'" + }, + "general": { + "name": "general", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "experimental": { + "name": "experimental", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "instance_settings_singleton_key_idx": { + "name": "instance_settings_singleton_key_idx", + "columns": [ + { + "expression": "singleton_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.instance_user_roles": { + "name": "instance_user_roles", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'instance_admin'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "instance_user_roles_user_role_unique_idx": { + "name": "instance_user_roles_user_role_unique_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "role", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "instance_user_roles_role_idx": { + "name": "instance_user_roles_role_idx", + "columns": [ + { + "expression": "role", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.invites": { + "name": "invites", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "invite_type": { + "name": "invite_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'company_join'" + }, + "token_hash": { + "name": "token_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "allowed_join_types": { + "name": "allowed_join_types", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'both'" + }, + "defaults_payload": { + "name": "defaults_payload", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "invited_by_user_id": { + "name": "invited_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "revoked_at": { + "name": "revoked_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "accepted_at": { + "name": "accepted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "invites_token_hash_unique_idx": { + "name": "invites_token_hash_unique_idx", + "columns": [ + { + "expression": "token_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "invites_company_invite_state_idx": { + "name": "invites_company_invite_state_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "invite_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "revoked_at", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "expires_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "invites_company_id_companies_id_fk": { + "name": "invites_company_id_companies_id_fk", + "tableFrom": "invites", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issue_approvals": { + "name": "issue_approvals", + "schema": "", + "columns": { + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "approval_id": { + "name": "approval_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "linked_by_agent_id": { + "name": "linked_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "linked_by_user_id": { + "name": "linked_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issue_approvals_issue_idx": { + "name": "issue_approvals_issue_idx", + "columns": [ + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_approvals_approval_idx": { + "name": "issue_approvals_approval_idx", + "columns": [ + { + "expression": "approval_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_approvals_company_idx": { + "name": "issue_approvals_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issue_approvals_company_id_companies_id_fk": { + "name": "issue_approvals_company_id_companies_id_fk", + "tableFrom": "issue_approvals", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issue_approvals_issue_id_issues_id_fk": { + "name": "issue_approvals_issue_id_issues_id_fk", + "tableFrom": "issue_approvals", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_approvals_approval_id_approvals_id_fk": { + "name": "issue_approvals_approval_id_approvals_id_fk", + "tableFrom": "issue_approvals", + "tableTo": "approvals", + "columnsFrom": [ + "approval_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_approvals_linked_by_agent_id_agents_id_fk": { + "name": "issue_approvals_linked_by_agent_id_agents_id_fk", + "tableFrom": "issue_approvals", + "tableTo": "agents", + "columnsFrom": [ + "linked_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "issue_approvals_pk": { + "name": "issue_approvals_pk", + "columns": [ + "issue_id", + "approval_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issue_attachments": { + "name": "issue_attachments", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "asset_id": { + "name": "asset_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "issue_comment_id": { + "name": "issue_comment_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issue_attachments_company_issue_idx": { + "name": "issue_attachments_company_issue_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_attachments_issue_comment_idx": { + "name": "issue_attachments_issue_comment_idx", + "columns": [ + { + "expression": "issue_comment_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_attachments_asset_uq": { + "name": "issue_attachments_asset_uq", + "columns": [ + { + "expression": "asset_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issue_attachments_company_id_companies_id_fk": { + "name": "issue_attachments_company_id_companies_id_fk", + "tableFrom": "issue_attachments", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issue_attachments_issue_id_issues_id_fk": { + "name": "issue_attachments_issue_id_issues_id_fk", + "tableFrom": "issue_attachments", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_attachments_asset_id_assets_id_fk": { + "name": "issue_attachments_asset_id_assets_id_fk", + "tableFrom": "issue_attachments", + "tableTo": "assets", + "columnsFrom": [ + "asset_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_attachments_issue_comment_id_issue_comments_id_fk": { + "name": "issue_attachments_issue_comment_id_issue_comments_id_fk", + "tableFrom": "issue_attachments", + "tableTo": "issue_comments", + "columnsFrom": [ + "issue_comment_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issue_comments": { + "name": "issue_comments", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "author_agent_id": { + "name": "author_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "author_user_id": { + "name": "author_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "body": { + "name": "body", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issue_comments_issue_idx": { + "name": "issue_comments_issue_idx", + "columns": [ + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_comments_company_idx": { + "name": "issue_comments_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_comments_company_issue_created_at_idx": { + "name": "issue_comments_company_issue_created_at_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_comments_company_author_issue_created_at_idx": { + "name": "issue_comments_company_author_issue_created_at_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "author_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issue_comments_company_id_companies_id_fk": { + "name": "issue_comments_company_id_companies_id_fk", + "tableFrom": "issue_comments", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issue_comments_issue_id_issues_id_fk": { + "name": "issue_comments_issue_id_issues_id_fk", + "tableFrom": "issue_comments", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issue_comments_author_agent_id_agents_id_fk": { + "name": "issue_comments_author_agent_id_agents_id_fk", + "tableFrom": "issue_comments", + "tableTo": "agents", + "columnsFrom": [ + "author_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issue_documents": { + "name": "issue_documents", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "document_id": { + "name": "document_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "key": { + "name": "key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issue_documents_company_issue_key_uq": { + "name": "issue_documents_company_issue_key_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_documents_document_uq": { + "name": "issue_documents_document_uq", + "columns": [ + { + "expression": "document_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_documents_company_issue_updated_idx": { + "name": "issue_documents_company_issue_updated_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issue_documents_company_id_companies_id_fk": { + "name": "issue_documents_company_id_companies_id_fk", + "tableFrom": "issue_documents", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issue_documents_issue_id_issues_id_fk": { + "name": "issue_documents_issue_id_issues_id_fk", + "tableFrom": "issue_documents", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_documents_document_id_documents_id_fk": { + "name": "issue_documents_document_id_documents_id_fk", + "tableFrom": "issue_documents", + "tableTo": "documents", + "columnsFrom": [ + "document_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issue_labels": { + "name": "issue_labels", + "schema": "", + "columns": { + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "label_id": { + "name": "label_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issue_labels_issue_idx": { + "name": "issue_labels_issue_idx", + "columns": [ + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_labels_label_idx": { + "name": "issue_labels_label_idx", + "columns": [ + { + "expression": "label_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_labels_company_idx": { + "name": "issue_labels_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issue_labels_issue_id_issues_id_fk": { + "name": "issue_labels_issue_id_issues_id_fk", + "tableFrom": "issue_labels", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_labels_label_id_labels_id_fk": { + "name": "issue_labels_label_id_labels_id_fk", + "tableFrom": "issue_labels", + "tableTo": "labels", + "columnsFrom": [ + "label_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_labels_company_id_companies_id_fk": { + "name": "issue_labels_company_id_companies_id_fk", + "tableFrom": "issue_labels", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "issue_labels_pk": { + "name": "issue_labels_pk", + "columns": [ + "issue_id", + "label_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issue_read_states": { + "name": "issue_read_states", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "last_read_at": { + "name": "last_read_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issue_read_states_company_issue_idx": { + "name": "issue_read_states_company_issue_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_read_states_company_user_idx": { + "name": "issue_read_states_company_user_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_read_states_company_issue_user_idx": { + "name": "issue_read_states_company_issue_user_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issue_read_states_company_id_companies_id_fk": { + "name": "issue_read_states_company_id_companies_id_fk", + "tableFrom": "issue_read_states", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issue_read_states_issue_id_issues_id_fk": { + "name": "issue_read_states_issue_id_issues_id_fk", + "tableFrom": "issue_read_states", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issue_work_products": { + "name": "issue_work_products", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "execution_workspace_id": { + "name": "execution_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "runtime_service_id": { + "name": "runtime_service_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "external_id": { + "name": "external_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "review_state": { + "name": "review_state", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'none'" + }, + "is_primary": { + "name": "is_primary", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "health_status": { + "name": "health_status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'unknown'" + }, + "summary": { + "name": "summary", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_by_run_id": { + "name": "created_by_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issue_work_products_company_issue_type_idx": { + "name": "issue_work_products_company_issue_type_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_work_products_company_execution_workspace_type_idx": { + "name": "issue_work_products_company_execution_workspace_type_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "execution_workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_work_products_company_provider_external_id_idx": { + "name": "issue_work_products_company_provider_external_id_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "provider", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "external_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issue_work_products_company_updated_idx": { + "name": "issue_work_products_company_updated_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issue_work_products_company_id_companies_id_fk": { + "name": "issue_work_products_company_id_companies_id_fk", + "tableFrom": "issue_work_products", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issue_work_products_project_id_projects_id_fk": { + "name": "issue_work_products_project_id_projects_id_fk", + "tableFrom": "issue_work_products", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "issue_work_products_issue_id_issues_id_fk": { + "name": "issue_work_products_issue_id_issues_id_fk", + "tableFrom": "issue_work_products", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "issue_work_products_execution_workspace_id_execution_workspaces_id_fk": { + "name": "issue_work_products_execution_workspace_id_execution_workspaces_id_fk", + "tableFrom": "issue_work_products", + "tableTo": "execution_workspaces", + "columnsFrom": [ + "execution_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "issue_work_products_runtime_service_id_workspace_runtime_services_id_fk": { + "name": "issue_work_products_runtime_service_id_workspace_runtime_services_id_fk", + "tableFrom": "issue_work_products", + "tableTo": "workspace_runtime_services", + "columnsFrom": [ + "runtime_service_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "issue_work_products_created_by_run_id_heartbeat_runs_id_fk": { + "name": "issue_work_products_created_by_run_id_heartbeat_runs_id_fk", + "tableFrom": "issue_work_products", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "created_by_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.issues": { + "name": "issues", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "project_workspace_id": { + "name": "project_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "goal_id": { + "name": "goal_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "parent_id": { + "name": "parent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'backlog'" + }, + "priority": { + "name": "priority", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'medium'" + }, + "assignee_agent_id": { + "name": "assignee_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "assignee_user_id": { + "name": "assignee_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "checkout_run_id": { + "name": "checkout_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "execution_run_id": { + "name": "execution_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "execution_agent_name_key": { + "name": "execution_agent_name_key", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "execution_locked_at": { + "name": "execution_locked_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "issue_number": { + "name": "issue_number", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "identifier": { + "name": "identifier", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "origin_kind": { + "name": "origin_kind", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'manual'" + }, + "origin_id": { + "name": "origin_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "origin_run_id": { + "name": "origin_run_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "request_depth": { + "name": "request_depth", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "billing_code": { + "name": "billing_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "assignee_adapter_overrides": { + "name": "assignee_adapter_overrides", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "execution_workspace_id": { + "name": "execution_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "execution_workspace_preference": { + "name": "execution_workspace_preference", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "execution_workspace_settings": { + "name": "execution_workspace_settings", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "started_at": { + "name": "started_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "completed_at": { + "name": "completed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "cancelled_at": { + "name": "cancelled_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "hidden_at": { + "name": "hidden_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "issues_company_status_idx": { + "name": "issues_company_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_company_assignee_status_idx": { + "name": "issues_company_assignee_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "assignee_agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_company_assignee_user_status_idx": { + "name": "issues_company_assignee_user_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "assignee_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_company_parent_idx": { + "name": "issues_company_parent_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "parent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_company_project_idx": { + "name": "issues_company_project_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_company_origin_idx": { + "name": "issues_company_origin_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "origin_kind", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "origin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_company_project_workspace_idx": { + "name": "issues_company_project_workspace_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_company_execution_workspace_idx": { + "name": "issues_company_execution_workspace_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "execution_workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_identifier_idx": { + "name": "issues_identifier_idx", + "columns": [ + { + "expression": "identifier", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "issues_open_routine_execution_uq": { + "name": "issues_open_routine_execution_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "origin_kind", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "origin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "where": "\"issues\".\"origin_kind\" = 'routine_execution'\n and \"issues\".\"origin_id\" is not null\n and \"issues\".\"hidden_at\" is null\n and \"issues\".\"execution_run_id\" is not null\n and \"issues\".\"status\" in ('backlog', 'todo', 'in_progress', 'in_review', 'blocked')", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "issues_company_id_companies_id_fk": { + "name": "issues_company_id_companies_id_fk", + "tableFrom": "issues", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issues_project_id_projects_id_fk": { + "name": "issues_project_id_projects_id_fk", + "tableFrom": "issues", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issues_project_workspace_id_project_workspaces_id_fk": { + "name": "issues_project_workspace_id_project_workspaces_id_fk", + "tableFrom": "issues", + "tableTo": "project_workspaces", + "columnsFrom": [ + "project_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "issues_goal_id_goals_id_fk": { + "name": "issues_goal_id_goals_id_fk", + "tableFrom": "issues", + "tableTo": "goals", + "columnsFrom": [ + "goal_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issues_parent_id_issues_id_fk": { + "name": "issues_parent_id_issues_id_fk", + "tableFrom": "issues", + "tableTo": "issues", + "columnsFrom": [ + "parent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issues_assignee_agent_id_agents_id_fk": { + "name": "issues_assignee_agent_id_agents_id_fk", + "tableFrom": "issues", + "tableTo": "agents", + "columnsFrom": [ + "assignee_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issues_checkout_run_id_heartbeat_runs_id_fk": { + "name": "issues_checkout_run_id_heartbeat_runs_id_fk", + "tableFrom": "issues", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "checkout_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "issues_execution_run_id_heartbeat_runs_id_fk": { + "name": "issues_execution_run_id_heartbeat_runs_id_fk", + "tableFrom": "issues", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "execution_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "issues_created_by_agent_id_agents_id_fk": { + "name": "issues_created_by_agent_id_agents_id_fk", + "tableFrom": "issues", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "issues_execution_workspace_id_execution_workspaces_id_fk": { + "name": "issues_execution_workspace_id_execution_workspaces_id_fk", + "tableFrom": "issues", + "tableTo": "execution_workspaces", + "columnsFrom": [ + "execution_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.join_requests": { + "name": "join_requests", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "invite_id": { + "name": "invite_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "request_type": { + "name": "request_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending_approval'" + }, + "request_ip": { + "name": "request_ip", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "requesting_user_id": { + "name": "requesting_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "request_email_snapshot": { + "name": "request_email_snapshot", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "agent_name": { + "name": "agent_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "adapter_type": { + "name": "adapter_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "capabilities": { + "name": "capabilities", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "agent_defaults_payload": { + "name": "agent_defaults_payload", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "claim_secret_hash": { + "name": "claim_secret_hash", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "claim_secret_expires_at": { + "name": "claim_secret_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "claim_secret_consumed_at": { + "name": "claim_secret_consumed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_agent_id": { + "name": "created_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "approved_by_user_id": { + "name": "approved_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "approved_at": { + "name": "approved_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "rejected_by_user_id": { + "name": "rejected_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "rejected_at": { + "name": "rejected_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "join_requests_invite_unique_idx": { + "name": "join_requests_invite_unique_idx", + "columns": [ + { + "expression": "invite_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "join_requests_company_status_type_created_idx": { + "name": "join_requests_company_status_type_created_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "request_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "join_requests_invite_id_invites_id_fk": { + "name": "join_requests_invite_id_invites_id_fk", + "tableFrom": "join_requests", + "tableTo": "invites", + "columnsFrom": [ + "invite_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "join_requests_company_id_companies_id_fk": { + "name": "join_requests_company_id_companies_id_fk", + "tableFrom": "join_requests", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "join_requests_created_agent_id_agents_id_fk": { + "name": "join_requests_created_agent_id_agents_id_fk", + "tableFrom": "join_requests", + "tableTo": "agents", + "columnsFrom": [ + "created_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.labels": { + "name": "labels", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "color": { + "name": "color", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "labels_company_idx": { + "name": "labels_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "labels_company_name_idx": { + "name": "labels_company_name_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "labels_company_id_companies_id_fk": { + "name": "labels_company_id_companies_id_fk", + "tableFrom": "labels", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_company_settings": { + "name": "plugin_company_settings", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "enabled": { + "name": "enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "settings_json": { + "name": "settings_json", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "last_error": { + "name": "last_error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_company_settings_company_idx": { + "name": "plugin_company_settings_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_company_settings_plugin_idx": { + "name": "plugin_company_settings_plugin_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_company_settings_company_plugin_uq": { + "name": "plugin_company_settings_company_plugin_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_company_settings_company_id_companies_id_fk": { + "name": "plugin_company_settings_company_id_companies_id_fk", + "tableFrom": "plugin_company_settings", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "plugin_company_settings_plugin_id_plugins_id_fk": { + "name": "plugin_company_settings_plugin_id_plugins_id_fk", + "tableFrom": "plugin_company_settings", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_config": { + "name": "plugin_config", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "config_json": { + "name": "config_json", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "last_error": { + "name": "last_error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_config_plugin_id_idx": { + "name": "plugin_config_plugin_id_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_config_plugin_id_plugins_id_fk": { + "name": "plugin_config_plugin_id_plugins_id_fk", + "tableFrom": "plugin_config", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_entities": { + "name": "plugin_entities", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "entity_type": { + "name": "entity_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope_kind": { + "name": "scope_kind", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope_id": { + "name": "scope_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "external_id": { + "name": "external_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "data": { + "name": "data", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_entities_plugin_idx": { + "name": "plugin_entities_plugin_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_entities_type_idx": { + "name": "plugin_entities_type_idx", + "columns": [ + { + "expression": "entity_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_entities_scope_idx": { + "name": "plugin_entities_scope_idx", + "columns": [ + { + "expression": "scope_kind", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_entities_external_idx": { + "name": "plugin_entities_external_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "entity_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "external_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_entities_plugin_id_plugins_id_fk": { + "name": "plugin_entities_plugin_id_plugins_id_fk", + "tableFrom": "plugin_entities", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_job_runs": { + "name": "plugin_job_runs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "job_id": { + "name": "job_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "trigger": { + "name": "trigger", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "duration_ms": { + "name": "duration_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "logs": { + "name": "logs", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "started_at": { + "name": "started_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "finished_at": { + "name": "finished_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_job_runs_job_idx": { + "name": "plugin_job_runs_job_idx", + "columns": [ + { + "expression": "job_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_job_runs_plugin_idx": { + "name": "plugin_job_runs_plugin_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_job_runs_status_idx": { + "name": "plugin_job_runs_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_job_runs_job_id_plugin_jobs_id_fk": { + "name": "plugin_job_runs_job_id_plugin_jobs_id_fk", + "tableFrom": "plugin_job_runs", + "tableTo": "plugin_jobs", + "columnsFrom": [ + "job_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "plugin_job_runs_plugin_id_plugins_id_fk": { + "name": "plugin_job_runs_plugin_id_plugins_id_fk", + "tableFrom": "plugin_job_runs", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_jobs": { + "name": "plugin_jobs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "job_key": { + "name": "job_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "schedule": { + "name": "schedule", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'active'" + }, + "last_run_at": { + "name": "last_run_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "next_run_at": { + "name": "next_run_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_jobs_plugin_idx": { + "name": "plugin_jobs_plugin_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_jobs_next_run_idx": { + "name": "plugin_jobs_next_run_idx", + "columns": [ + { + "expression": "next_run_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_jobs_unique_idx": { + "name": "plugin_jobs_unique_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "job_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_jobs_plugin_id_plugins_id_fk": { + "name": "plugin_jobs_plugin_id_plugins_id_fk", + "tableFrom": "plugin_jobs", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_logs": { + "name": "plugin_logs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "level": { + "name": "level", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'info'" + }, + "message": { + "name": "message", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "meta": { + "name": "meta", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_logs_plugin_time_idx": { + "name": "plugin_logs_plugin_time_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_logs_level_idx": { + "name": "plugin_logs_level_idx", + "columns": [ + { + "expression": "level", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_logs_plugin_id_plugins_id_fk": { + "name": "plugin_logs_plugin_id_plugins_id_fk", + "tableFrom": "plugin_logs", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_state": { + "name": "plugin_state", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "scope_kind": { + "name": "scope_kind", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope_id": { + "name": "scope_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "namespace": { + "name": "namespace", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'default'" + }, + "state_key": { + "name": "state_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "value_json": { + "name": "value_json", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_state_plugin_scope_idx": { + "name": "plugin_state_plugin_scope_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "scope_kind", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_state_plugin_id_plugins_id_fk": { + "name": "plugin_state_plugin_id_plugins_id_fk", + "tableFrom": "plugin_state", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "plugin_state_unique_entry_idx": { + "name": "plugin_state_unique_entry_idx", + "nullsNotDistinct": true, + "columns": [ + "plugin_id", + "scope_kind", + "scope_id", + "namespace", + "state_key" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugin_webhook_deliveries": { + "name": "plugin_webhook_deliveries", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "plugin_id": { + "name": "plugin_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "webhook_key": { + "name": "webhook_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "external_id": { + "name": "external_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "duration_ms": { + "name": "duration_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "payload": { + "name": "payload", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "headers": { + "name": "headers", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "started_at": { + "name": "started_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "finished_at": { + "name": "finished_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugin_webhook_deliveries_plugin_idx": { + "name": "plugin_webhook_deliveries_plugin_idx", + "columns": [ + { + "expression": "plugin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_webhook_deliveries_status_idx": { + "name": "plugin_webhook_deliveries_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugin_webhook_deliveries_key_idx": { + "name": "plugin_webhook_deliveries_key_idx", + "columns": [ + { + "expression": "webhook_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "plugin_webhook_deliveries_plugin_id_plugins_id_fk": { + "name": "plugin_webhook_deliveries_plugin_id_plugins_id_fk", + "tableFrom": "plugin_webhook_deliveries", + "tableTo": "plugins", + "columnsFrom": [ + "plugin_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.plugins": { + "name": "plugins", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "plugin_key": { + "name": "plugin_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "package_name": { + "name": "package_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "version": { + "name": "version", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "api_version": { + "name": "api_version", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 1 + }, + "categories": { + "name": "categories", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "manifest_json": { + "name": "manifest_json", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'installed'" + }, + "install_order": { + "name": "install_order", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "package_path": { + "name": "package_path", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "last_error": { + "name": "last_error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "installed_at": { + "name": "installed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "plugins_plugin_key_idx": { + "name": "plugins_plugin_key_idx", + "columns": [ + { + "expression": "plugin_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "plugins_status_idx": { + "name": "plugins_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.principal_permission_grants": { + "name": "principal_permission_grants", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "principal_type": { + "name": "principal_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "principal_id": { + "name": "principal_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "permission_key": { + "name": "permission_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope": { + "name": "scope", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "granted_by_user_id": { + "name": "granted_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "principal_permission_grants_unique_idx": { + "name": "principal_permission_grants_unique_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "principal_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "principal_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "permission_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "principal_permission_grants_company_permission_idx": { + "name": "principal_permission_grants_company_permission_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "permission_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "principal_permission_grants_company_id_companies_id_fk": { + "name": "principal_permission_grants_company_id_companies_id_fk", + "tableFrom": "principal_permission_grants", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.project_goals": { + "name": "project_goals", + "schema": "", + "columns": { + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "goal_id": { + "name": "goal_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "project_goals_project_idx": { + "name": "project_goals_project_idx", + "columns": [ + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "project_goals_goal_idx": { + "name": "project_goals_goal_idx", + "columns": [ + { + "expression": "goal_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "project_goals_company_idx": { + "name": "project_goals_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "project_goals_project_id_projects_id_fk": { + "name": "project_goals_project_id_projects_id_fk", + "tableFrom": "project_goals", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "project_goals_goal_id_goals_id_fk": { + "name": "project_goals_goal_id_goals_id_fk", + "tableFrom": "project_goals", + "tableTo": "goals", + "columnsFrom": [ + "goal_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "project_goals_company_id_companies_id_fk": { + "name": "project_goals_company_id_companies_id_fk", + "tableFrom": "project_goals", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "project_goals_project_id_goal_id_pk": { + "name": "project_goals_project_id_goal_id_pk", + "columns": [ + "project_id", + "goal_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.project_workspaces": { + "name": "project_workspaces", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "source_type": { + "name": "source_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'local_path'" + }, + "cwd": { + "name": "cwd", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "repo_url": { + "name": "repo_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "repo_ref": { + "name": "repo_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "default_ref": { + "name": "default_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "visibility": { + "name": "visibility", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'default'" + }, + "setup_command": { + "name": "setup_command", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "cleanup_command": { + "name": "cleanup_command", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "remote_provider": { + "name": "remote_provider", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "remote_workspace_ref": { + "name": "remote_workspace_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "shared_workspace_key": { + "name": "shared_workspace_key", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "is_primary": { + "name": "is_primary", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "project_workspaces_company_project_idx": { + "name": "project_workspaces_company_project_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "project_workspaces_project_primary_idx": { + "name": "project_workspaces_project_primary_idx", + "columns": [ + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "is_primary", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "project_workspaces_project_source_type_idx": { + "name": "project_workspaces_project_source_type_idx", + "columns": [ + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "source_type", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "project_workspaces_company_shared_key_idx": { + "name": "project_workspaces_company_shared_key_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "shared_workspace_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "project_workspaces_project_remote_ref_idx": { + "name": "project_workspaces_project_remote_ref_idx", + "columns": [ + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "remote_provider", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "remote_workspace_ref", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "project_workspaces_company_id_companies_id_fk": { + "name": "project_workspaces_company_id_companies_id_fk", + "tableFrom": "project_workspaces", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "project_workspaces_project_id_projects_id_fk": { + "name": "project_workspaces_project_id_projects_id_fk", + "tableFrom": "project_workspaces", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.projects": { + "name": "projects", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "goal_id": { + "name": "goal_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'backlog'" + }, + "lead_agent_id": { + "name": "lead_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "target_date": { + "name": "target_date", + "type": "date", + "primaryKey": false, + "notNull": false + }, + "color": { + "name": "color", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "pause_reason": { + "name": "pause_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "paused_at": { + "name": "paused_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "execution_workspace_policy": { + "name": "execution_workspace_policy", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "archived_at": { + "name": "archived_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "projects_company_idx": { + "name": "projects_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "projects_company_id_companies_id_fk": { + "name": "projects_company_id_companies_id_fk", + "tableFrom": "projects", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "projects_goal_id_goals_id_fk": { + "name": "projects_goal_id_goals_id_fk", + "tableFrom": "projects", + "tableTo": "goals", + "columnsFrom": [ + "goal_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "projects_lead_agent_id_agents_id_fk": { + "name": "projects_lead_agent_id_agents_id_fk", + "tableFrom": "projects", + "tableTo": "agents", + "columnsFrom": [ + "lead_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.routine_runs": { + "name": "routine_runs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "routine_id": { + "name": "routine_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "trigger_id": { + "name": "trigger_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "source": { + "name": "source", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'received'" + }, + "triggered_at": { + "name": "triggered_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "idempotency_key": { + "name": "idempotency_key", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "trigger_payload": { + "name": "trigger_payload", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "linked_issue_id": { + "name": "linked_issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "coalesced_into_run_id": { + "name": "coalesced_into_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "failure_reason": { + "name": "failure_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "completed_at": { + "name": "completed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "routine_runs_company_routine_idx": { + "name": "routine_runs_company_routine_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "routine_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routine_runs_trigger_idx": { + "name": "routine_runs_trigger_idx", + "columns": [ + { + "expression": "trigger_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routine_runs_linked_issue_idx": { + "name": "routine_runs_linked_issue_idx", + "columns": [ + { + "expression": "linked_issue_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routine_runs_trigger_idempotency_idx": { + "name": "routine_runs_trigger_idempotency_idx", + "columns": [ + { + "expression": "trigger_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "idempotency_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "routine_runs_company_id_companies_id_fk": { + "name": "routine_runs_company_id_companies_id_fk", + "tableFrom": "routine_runs", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "routine_runs_routine_id_routines_id_fk": { + "name": "routine_runs_routine_id_routines_id_fk", + "tableFrom": "routine_runs", + "tableTo": "routines", + "columnsFrom": [ + "routine_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "routine_runs_trigger_id_routine_triggers_id_fk": { + "name": "routine_runs_trigger_id_routine_triggers_id_fk", + "tableFrom": "routine_runs", + "tableTo": "routine_triggers", + "columnsFrom": [ + "trigger_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "routine_runs_linked_issue_id_issues_id_fk": { + "name": "routine_runs_linked_issue_id_issues_id_fk", + "tableFrom": "routine_runs", + "tableTo": "issues", + "columnsFrom": [ + "linked_issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.routine_triggers": { + "name": "routine_triggers", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "routine_id": { + "name": "routine_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "kind": { + "name": "kind", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "label": { + "name": "label", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "enabled": { + "name": "enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "cron_expression": { + "name": "cron_expression", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "timezone": { + "name": "timezone", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "next_run_at": { + "name": "next_run_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_fired_at": { + "name": "last_fired_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "public_id": { + "name": "public_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "secret_id": { + "name": "secret_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "signing_mode": { + "name": "signing_mode", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "replay_window_sec": { + "name": "replay_window_sec", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "last_rotated_at": { + "name": "last_rotated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_result": { + "name": "last_result", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_by_agent_id": { + "name": "updated_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "updated_by_user_id": { + "name": "updated_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "routine_triggers_company_routine_idx": { + "name": "routine_triggers_company_routine_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "routine_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routine_triggers_company_kind_idx": { + "name": "routine_triggers_company_kind_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "kind", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routine_triggers_next_run_idx": { + "name": "routine_triggers_next_run_idx", + "columns": [ + { + "expression": "next_run_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routine_triggers_public_id_idx": { + "name": "routine_triggers_public_id_idx", + "columns": [ + { + "expression": "public_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routine_triggers_public_id_uq": { + "name": "routine_triggers_public_id_uq", + "columns": [ + { + "expression": "public_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "routine_triggers_company_id_companies_id_fk": { + "name": "routine_triggers_company_id_companies_id_fk", + "tableFrom": "routine_triggers", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "routine_triggers_routine_id_routines_id_fk": { + "name": "routine_triggers_routine_id_routines_id_fk", + "tableFrom": "routine_triggers", + "tableTo": "routines", + "columnsFrom": [ + "routine_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "routine_triggers_secret_id_company_secrets_id_fk": { + "name": "routine_triggers_secret_id_company_secrets_id_fk", + "tableFrom": "routine_triggers", + "tableTo": "company_secrets", + "columnsFrom": [ + "secret_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "routine_triggers_created_by_agent_id_agents_id_fk": { + "name": "routine_triggers_created_by_agent_id_agents_id_fk", + "tableFrom": "routine_triggers", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "routine_triggers_updated_by_agent_id_agents_id_fk": { + "name": "routine_triggers_updated_by_agent_id_agents_id_fk", + "tableFrom": "routine_triggers", + "tableTo": "agents", + "columnsFrom": [ + "updated_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.routines": { + "name": "routines", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "goal_id": { + "name": "goal_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "parent_issue_id": { + "name": "parent_issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "assignee_agent_id": { + "name": "assignee_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "priority": { + "name": "priority", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'medium'" + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'active'" + }, + "concurrency_policy": { + "name": "concurrency_policy", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'coalesce_if_active'" + }, + "catch_up_policy": { + "name": "catch_up_policy", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'skip_missed'" + }, + "created_by_agent_id": { + "name": "created_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_by_agent_id": { + "name": "updated_by_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "updated_by_user_id": { + "name": "updated_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "last_triggered_at": { + "name": "last_triggered_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_enqueued_at": { + "name": "last_enqueued_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "routines_company_status_idx": { + "name": "routines_company_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routines_company_assignee_idx": { + "name": "routines_company_assignee_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "assignee_agent_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "routines_company_project_idx": { + "name": "routines_company_project_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "routines_company_id_companies_id_fk": { + "name": "routines_company_id_companies_id_fk", + "tableFrom": "routines", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "routines_project_id_projects_id_fk": { + "name": "routines_project_id_projects_id_fk", + "tableFrom": "routines", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "routines_goal_id_goals_id_fk": { + "name": "routines_goal_id_goals_id_fk", + "tableFrom": "routines", + "tableTo": "goals", + "columnsFrom": [ + "goal_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "routines_parent_issue_id_issues_id_fk": { + "name": "routines_parent_issue_id_issues_id_fk", + "tableFrom": "routines", + "tableTo": "issues", + "columnsFrom": [ + "parent_issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "routines_assignee_agent_id_agents_id_fk": { + "name": "routines_assignee_agent_id_agents_id_fk", + "tableFrom": "routines", + "tableTo": "agents", + "columnsFrom": [ + "assignee_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "routines_created_by_agent_id_agents_id_fk": { + "name": "routines_created_by_agent_id_agents_id_fk", + "tableFrom": "routines", + "tableTo": "agents", + "columnsFrom": [ + "created_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "routines_updated_by_agent_id_agents_id_fk": { + "name": "routines_updated_by_agent_id_agents_id_fk", + "tableFrom": "routines", + "tableTo": "agents", + "columnsFrom": [ + "updated_by_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.workspace_operations": { + "name": "workspace_operations", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "execution_workspace_id": { + "name": "execution_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "heartbeat_run_id": { + "name": "heartbeat_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "phase": { + "name": "phase", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "command": { + "name": "command", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "cwd": { + "name": "cwd", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'running'" + }, + "exit_code": { + "name": "exit_code", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "log_store": { + "name": "log_store", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "log_ref": { + "name": "log_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "log_bytes": { + "name": "log_bytes", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "log_sha256": { + "name": "log_sha256", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "log_compressed": { + "name": "log_compressed", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "stdout_excerpt": { + "name": "stdout_excerpt", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "stderr_excerpt": { + "name": "stderr_excerpt", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "metadata": { + "name": "metadata", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "started_at": { + "name": "started_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "finished_at": { + "name": "finished_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "workspace_operations_company_run_started_idx": { + "name": "workspace_operations_company_run_started_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "heartbeat_run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "started_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "workspace_operations_company_workspace_started_idx": { + "name": "workspace_operations_company_workspace_started_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "execution_workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "started_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "workspace_operations_company_id_companies_id_fk": { + "name": "workspace_operations_company_id_companies_id_fk", + "tableFrom": "workspace_operations", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "workspace_operations_execution_workspace_id_execution_workspaces_id_fk": { + "name": "workspace_operations_execution_workspace_id_execution_workspaces_id_fk", + "tableFrom": "workspace_operations", + "tableTo": "execution_workspaces", + "columnsFrom": [ + "execution_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "workspace_operations_heartbeat_run_id_heartbeat_runs_id_fk": { + "name": "workspace_operations_heartbeat_run_id_heartbeat_runs_id_fk", + "tableFrom": "workspace_operations", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "heartbeat_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.workspace_runtime_services": { + "name": "workspace_runtime_services", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "project_id": { + "name": "project_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "project_workspace_id": { + "name": "project_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "execution_workspace_id": { + "name": "execution_workspace_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "scope_type": { + "name": "scope_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope_id": { + "name": "scope_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "service_name": { + "name": "service_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "lifecycle": { + "name": "lifecycle", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "reuse_key": { + "name": "reuse_key", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "command": { + "name": "command", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "cwd": { + "name": "cwd", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "port": { + "name": "port", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "url": { + "name": "url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_ref": { + "name": "provider_ref", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "owner_agent_id": { + "name": "owner_agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "started_by_run_id": { + "name": "started_by_run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "last_used_at": { + "name": "last_used_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "started_at": { + "name": "started_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "stopped_at": { + "name": "stopped_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "stop_policy": { + "name": "stop_policy", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "health_status": { + "name": "health_status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'unknown'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "workspace_runtime_services_company_workspace_status_idx": { + "name": "workspace_runtime_services_company_workspace_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "workspace_runtime_services_company_execution_workspace_status_idx": { + "name": "workspace_runtime_services_company_execution_workspace_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "execution_workspace_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "workspace_runtime_services_company_project_status_idx": { + "name": "workspace_runtime_services_company_project_status_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "project_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "workspace_runtime_services_run_idx": { + "name": "workspace_runtime_services_run_idx", + "columns": [ + { + "expression": "started_by_run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "workspace_runtime_services_company_updated_idx": { + "name": "workspace_runtime_services_company_updated_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "updated_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "workspace_runtime_services_company_id_companies_id_fk": { + "name": "workspace_runtime_services_company_id_companies_id_fk", + "tableFrom": "workspace_runtime_services", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "workspace_runtime_services_project_id_projects_id_fk": { + "name": "workspace_runtime_services_project_id_projects_id_fk", + "tableFrom": "workspace_runtime_services", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "workspace_runtime_services_project_workspace_id_project_workspaces_id_fk": { + "name": "workspace_runtime_services_project_workspace_id_project_workspaces_id_fk", + "tableFrom": "workspace_runtime_services", + "tableTo": "project_workspaces", + "columnsFrom": [ + "project_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "workspace_runtime_services_execution_workspace_id_execution_workspaces_id_fk": { + "name": "workspace_runtime_services_execution_workspace_id_execution_workspaces_id_fk", + "tableFrom": "workspace_runtime_services", + "tableTo": "execution_workspaces", + "columnsFrom": [ + "execution_workspace_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "workspace_runtime_services_issue_id_issues_id_fk": { + "name": "workspace_runtime_services_issue_id_issues_id_fk", + "tableFrom": "workspace_runtime_services", + "tableTo": "issues", + "columnsFrom": [ + "issue_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "workspace_runtime_services_owner_agent_id_agents_id_fk": { + "name": "workspace_runtime_services_owner_agent_id_agents_id_fk", + "tableFrom": "workspace_runtime_services", + "tableTo": "agents", + "columnsFrom": [ + "owner_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "workspace_runtime_services_started_by_run_id_heartbeat_runs_id_fk": { + "name": "workspace_runtime_services_started_by_run_id_heartbeat_runs_id_fk", + "tableFrom": "workspace_runtime_services", + "tableTo": "heartbeat_runs", + "columnsFrom": [ + "started_by_run_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/packages/db/src/migrations/meta/_journal.json b/packages/db/src/migrations/meta/_journal.json index 7ceb306abb..c2d6ce0a0a 100644 --- a/packages/db/src/migrations/meta/_journal.json +++ b/packages/db/src/migrations/meta/_journal.json @@ -309,6 +309,13 @@ "when": 1774008910991, "tag": "0043_reflective_captain_universe", "breakpoints": true + }, + { + "idx": 44, + "version": "7", + "when": 1774269579794, + "tag": "0044_illegal_toad", + "breakpoints": true } ] -} +} \ No newline at end of file diff --git a/packages/db/src/schema/board_api_keys.ts b/packages/db/src/schema/board_api_keys.ts new file mode 100644 index 0000000000..e786760ff3 --- /dev/null +++ b/packages/db/src/schema/board_api_keys.ts @@ -0,0 +1,20 @@ +import { pgTable, uuid, text, timestamp, index, uniqueIndex } from "drizzle-orm/pg-core"; +import { authUsers } from "./auth.js"; + +export const boardApiKeys = pgTable( + "board_api_keys", + { + id: uuid("id").primaryKey().defaultRandom(), + userId: text("user_id").notNull().references(() => authUsers.id, { onDelete: "cascade" }), + name: text("name").notNull(), + keyHash: text("key_hash").notNull(), + lastUsedAt: timestamp("last_used_at", { withTimezone: true }), + revokedAt: timestamp("revoked_at", { withTimezone: true }), + expiresAt: timestamp("expires_at", { withTimezone: true }), + createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), + }, + (table) => ({ + keyHashIdx: uniqueIndex("board_api_keys_key_hash_idx").on(table.keyHash), + userIdx: index("board_api_keys_user_idx").on(table.userId), + }), +); diff --git a/packages/db/src/schema/cli_auth_challenges.ts b/packages/db/src/schema/cli_auth_challenges.ts new file mode 100644 index 0000000000..5c6335cab6 --- /dev/null +++ b/packages/db/src/schema/cli_auth_challenges.ts @@ -0,0 +1,30 @@ +import { pgTable, uuid, text, timestamp, index } from "drizzle-orm/pg-core"; +import { authUsers } from "./auth.js"; +import { companies } from "./companies.js"; +import { boardApiKeys } from "./board_api_keys.js"; + +export const cliAuthChallenges = pgTable( + "cli_auth_challenges", + { + id: uuid("id").primaryKey().defaultRandom(), + secretHash: text("secret_hash").notNull(), + command: text("command").notNull(), + clientName: text("client_name"), + requestedAccess: text("requested_access").notNull().default("board"), + requestedCompanyId: uuid("requested_company_id").references(() => companies.id, { onDelete: "set null" }), + pendingKeyHash: text("pending_key_hash").notNull(), + pendingKeyName: text("pending_key_name").notNull(), + approvedByUserId: text("approved_by_user_id").references(() => authUsers.id, { onDelete: "set null" }), + boardApiKeyId: uuid("board_api_key_id").references(() => boardApiKeys.id, { onDelete: "set null" }), + approvedAt: timestamp("approved_at", { withTimezone: true }), + cancelledAt: timestamp("cancelled_at", { withTimezone: true }), + expiresAt: timestamp("expires_at", { withTimezone: true }).notNull(), + createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), + updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), + }, + (table) => ({ + secretHashIdx: index("cli_auth_challenges_secret_hash_idx").on(table.secretHash), + approvedByIdx: index("cli_auth_challenges_approved_by_idx").on(table.approvedByUserId), + requestedCompanyIdx: index("cli_auth_challenges_requested_company_idx").on(table.requestedCompanyId), + }), +); diff --git a/packages/db/src/schema/index.ts b/packages/db/src/schema/index.ts index 20a3df1267..a411ddcbec 100644 --- a/packages/db/src/schema/index.ts +++ b/packages/db/src/schema/index.ts @@ -4,6 +4,8 @@ export { authUsers, authSessions, authAccounts, authVerifications } from "./auth export { instanceSettings } from "./instance_settings.js"; export { instanceUserRoles } from "./instance_user_roles.js"; export { agents } from "./agents.js"; +export { boardApiKeys } from "./board_api_keys.js"; +export { cliAuthChallenges } from "./cli_auth_challenges.js"; export { companyMemberships } from "./company_memberships.js"; export { principalPermissionGrants } from "./principal_permission_grants.js"; export { invites } from "./invites.js"; diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index c1f5c1df53..f214f6fcd9 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -254,9 +254,13 @@ export type { CompanyPortabilityEnvInput, CompanyPortabilityFileEntry, CompanyPortabilityCompanyManifestEntry, + CompanyPortabilitySidebarOrder, CompanyPortabilityAgentManifestEntry, CompanyPortabilitySkillManifestEntry, CompanyPortabilityProjectManifestEntry, + CompanyPortabilityProjectWorkspaceManifestEntry, + CompanyPortabilityIssueRoutineTriggerManifestEntry, + CompanyPortabilityIssueRoutineManifestEntry, CompanyPortabilityIssueManifestEntry, CompanyPortabilityManifest, CompanyPortabilityExportResult, @@ -445,6 +449,9 @@ export { acceptInviteSchema, listJoinRequestsQuerySchema, claimJoinRequestApiKeySchema, + boardCliAuthAccessLevelSchema, + createCliAuthChallengeSchema, + resolveCliAuthChallengeSchema, updateMemberPermissionsSchema, updateUserCompanyAccessSchema, type CreateCostEvent, @@ -456,6 +463,9 @@ export { type AcceptInvite, type ListJoinRequestsQuery, type ClaimJoinRequestApiKey, + type BoardCliAuthAccessLevel, + type CreateCliAuthChallenge, + type ResolveCliAuthChallenge, type UpdateMemberPermissions, type UpdateUserCompanyAccess, companySkillSourceTypeSchema, @@ -479,6 +489,7 @@ export { portabilityIncludeSchema, portabilityEnvInputSchema, portabilityCompanyManifestEntrySchema, + portabilitySidebarOrderSchema, portabilityAgentManifestEntrySchema, portabilityManifestSchema, portabilitySourceSchema, @@ -530,10 +541,15 @@ export { API_PREFIX, API } from "./api.js"; export { normalizeAgentUrlKey, deriveAgentUrlKey, isUuidLike } from "./agent-url-key.js"; export { deriveProjectUrlKey, normalizeProjectUrlKey } from "./project-url-key.js"; export { + AGENT_MENTION_SCHEME, PROJECT_MENTION_SCHEME, + buildAgentMentionHref, buildProjectMentionHref, + extractAgentMentionIds, + parseAgentMentionHref, parseProjectMentionHref, extractProjectMentionIds, + type ParsedAgentMention, type ParsedProjectMention, } from "./project-mentions.js"; diff --git a/packages/shared/src/project-mentions.test.ts b/packages/shared/src/project-mentions.test.ts new file mode 100644 index 0000000000..55f27369b8 --- /dev/null +++ b/packages/shared/src/project-mentions.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; +import { + buildAgentMentionHref, + buildProjectMentionHref, + extractAgentMentionIds, + extractProjectMentionIds, + parseAgentMentionHref, + parseProjectMentionHref, +} from "./project-mentions.js"; + +describe("project-mentions", () => { + it("round-trips project mentions with color metadata", () => { + const href = buildProjectMentionHref("project-123", "#336699"); + expect(parseProjectMentionHref(href)).toEqual({ + projectId: "project-123", + color: "#336699", + }); + expect(extractProjectMentionIds(`[@Paperclip App](${href})`)).toEqual(["project-123"]); + }); + + it("round-trips agent mentions with icon metadata", () => { + const href = buildAgentMentionHref("agent-123", "code"); + expect(parseAgentMentionHref(href)).toEqual({ + agentId: "agent-123", + icon: "code", + }); + expect(extractAgentMentionIds(`[@CodexCoder](${href})`)).toEqual(["agent-123"]); + }); +}); diff --git a/packages/shared/src/project-mentions.ts b/packages/shared/src/project-mentions.ts index 2c1675179d..66be89483a 100644 --- a/packages/shared/src/project-mentions.ts +++ b/packages/shared/src/project-mentions.ts @@ -1,16 +1,24 @@ export const PROJECT_MENTION_SCHEME = "project://"; +export const AGENT_MENTION_SCHEME = "agent://"; const HEX_COLOR_RE = /^[0-9a-f]{6}$/i; const HEX_COLOR_SHORT_RE = /^[0-9a-f]{3}$/i; const HEX_COLOR_WITH_HASH_RE = /^#[0-9a-f]{6}$/i; const HEX_COLOR_SHORT_WITH_HASH_RE = /^#[0-9a-f]{3}$/i; const PROJECT_MENTION_LINK_RE = /\[[^\]]*]\((project:\/\/[^)\s]+)\)/gi; +const AGENT_MENTION_LINK_RE = /\[[^\]]*]\((agent:\/\/[^)\s]+)\)/gi; +const AGENT_ICON_NAME_RE = /^[a-z0-9-]+$/i; export interface ParsedProjectMention { projectId: string; color: string | null; } +export interface ParsedAgentMention { + agentId: string; + icon: string | null; +} + function normalizeHexColor(input: string | null | undefined): string | null { if (!input) return null; const trimmed = input.trim(); @@ -65,6 +73,36 @@ export function parseProjectMentionHref(href: string): ParsedProjectMention | nu }; } +export function buildAgentMentionHref(agentId: string, icon?: string | null): string { + const trimmedAgentId = agentId.trim(); + const normalizedIcon = normalizeAgentIcon(icon ?? null); + if (!normalizedIcon) { + return `${AGENT_MENTION_SCHEME}${trimmedAgentId}`; + } + return `${AGENT_MENTION_SCHEME}${trimmedAgentId}?i=${encodeURIComponent(normalizedIcon)}`; +} + +export function parseAgentMentionHref(href: string): ParsedAgentMention | null { + if (!href.startsWith(AGENT_MENTION_SCHEME)) return null; + + let url: URL; + try { + url = new URL(href); + } catch { + return null; + } + + if (url.protocol !== "agent:") return null; + + const agentId = `${url.hostname}${url.pathname}`.replace(/^\/+/, "").trim(); + if (!agentId) return null; + + return { + agentId, + icon: normalizeAgentIcon(url.searchParams.get("i") ?? url.searchParams.get("icon")), + }; +} + export function extractProjectMentionIds(markdown: string): string[] { if (!markdown) return []; const ids = new Set(); @@ -76,3 +114,22 @@ export function extractProjectMentionIds(markdown: string): string[] { } return [...ids]; } + +export function extractAgentMentionIds(markdown: string): string[] { + if (!markdown) return []; + const ids = new Set(); + const re = new RegExp(AGENT_MENTION_LINK_RE); + let match: RegExpExecArray | null; + while ((match = re.exec(markdown)) !== null) { + const parsed = parseAgentMentionHref(match[1]); + if (parsed) ids.add(parsed.agentId); + } + return [...ids]; +} + +function normalizeAgentIcon(input: string | null | undefined): string | null { + if (!input) return null; + const trimmed = input.trim().toLowerCase(); + if (!trimmed || !AGENT_ICON_NAME_RE.test(trimmed)) return null; + return trimmed; +} diff --git a/packages/shared/src/types/company-portability.ts b/packages/shared/src/types/company-portability.ts index 260888310e..63016e933a 100644 --- a/packages/shared/src/types/company-portability.ts +++ b/packages/shared/src/types/company-portability.ts @@ -33,6 +33,11 @@ export interface CompanyPortabilityCompanyManifestEntry { requireBoardApprovalForNewAgents: boolean; } +export interface CompanyPortabilitySidebarOrder { + agents: string[]; + projects: string[]; +} + export interface CompanyPortabilityProjectManifestEntry { slug: string; name: string; @@ -44,18 +49,52 @@ export interface CompanyPortabilityProjectManifestEntry { color: string | null; status: string | null; executionWorkspacePolicy: Record | null; + workspaces: CompanyPortabilityProjectWorkspaceManifestEntry[]; metadata: Record | null; } +export interface CompanyPortabilityProjectWorkspaceManifestEntry { + key: string; + name: string; + sourceType: string | null; + repoUrl: string | null; + repoRef: string | null; + defaultRef: string | null; + visibility: string | null; + setupCommand: string | null; + cleanupCommand: string | null; + metadata: Record | null; + isPrimary: boolean; +} + +export interface CompanyPortabilityIssueRoutineTriggerManifestEntry { + kind: string; + label: string | null; + enabled: boolean; + cronExpression: string | null; + timezone: string | null; + signingMode: string | null; + replayWindowSec: number | null; +} + +export interface CompanyPortabilityIssueRoutineManifestEntry { + concurrencyPolicy: string | null; + catchUpPolicy: string | null; + triggers: CompanyPortabilityIssueRoutineTriggerManifestEntry[]; +} + export interface CompanyPortabilityIssueManifestEntry { slug: string; identifier: string | null; title: string; path: string; projectSlug: string | null; + projectWorkspaceKey: string | null; assigneeAgentSlug: string | null; description: string | null; - recurrence: Record | null; + recurring: boolean; + routine: CompanyPortabilityIssueRoutineManifestEntry | null; + legacyRecurrence: Record | null; status: string | null; priority: string | null; labelIds: string[]; @@ -110,6 +149,7 @@ export interface CompanyPortabilityManifest { } | null; includes: CompanyPortabilityInclude; company: CompanyPortabilityCompanyManifestEntry | null; + sidebar: CompanyPortabilitySidebarOrder | null; agents: CompanyPortabilityAgentManifestEntry[]; skills: CompanyPortabilitySkillManifestEntry[]; projects: CompanyPortabilityProjectManifestEntry[]; @@ -245,6 +285,13 @@ export interface CompanyPortabilityImportResult { name: string; reason: string | null; }[]; + projects: { + slug: string; + id: string | null; + action: "created" | "updated" | "skipped"; + name: string; + reason: string | null; + }[]; envInputs: CompanyPortabilityEnvInput[]; warnings: string[]; } @@ -258,4 +305,5 @@ export interface CompanyPortabilityExportRequest { projectIssues?: string[]; selectedFiles?: string[]; expandReferencedSkills?: boolean; + sidebarOrder?: Partial; } diff --git a/packages/shared/src/types/index.ts b/packages/shared/src/types/index.ts index e6ae52026e..dd615c4c65 100644 --- a/packages/shared/src/types/index.ts +++ b/packages/shared/src/types/index.ts @@ -144,9 +144,13 @@ export type { CompanyPortabilityEnvInput, CompanyPortabilityFileEntry, CompanyPortabilityCompanyManifestEntry, + CompanyPortabilitySidebarOrder, CompanyPortabilityAgentManifestEntry, CompanyPortabilitySkillManifestEntry, CompanyPortabilityProjectManifestEntry, + CompanyPortabilityProjectWorkspaceManifestEntry, + CompanyPortabilityIssueRoutineTriggerManifestEntry, + CompanyPortabilityIssueRoutineManifestEntry, CompanyPortabilityIssueManifestEntry, CompanyPortabilityManifest, CompanyPortabilityExportResult, diff --git a/packages/shared/src/validators/access.ts b/packages/shared/src/validators/access.ts index 75b317092d..126a084386 100644 --- a/packages/shared/src/validators/access.ts +++ b/packages/shared/src/validators/access.ts @@ -52,6 +52,28 @@ export const claimJoinRequestApiKeySchema = z.object({ export type ClaimJoinRequestApiKey = z.infer; +export const boardCliAuthAccessLevelSchema = z.enum([ + "board", + "instance_admin_required", +]); + +export type BoardCliAuthAccessLevel = z.infer; + +export const createCliAuthChallengeSchema = z.object({ + command: z.string().min(1).max(240), + clientName: z.string().max(120).optional().nullable(), + requestedAccess: boardCliAuthAccessLevelSchema.default("board"), + requestedCompanyId: z.string().uuid().optional().nullable(), +}); + +export type CreateCliAuthChallenge = z.infer; + +export const resolveCliAuthChallengeSchema = z.object({ + token: z.string().min(16).max(256), +}); + +export type ResolveCliAuthChallenge = z.infer; + export const updateMemberPermissionsSchema = z.object({ grants: z.array( z.object({ diff --git a/packages/shared/src/validators/agent.ts b/packages/shared/src/validators/agent.ts index d72a76a29b..8c29150b35 100644 --- a/packages/shared/src/validators/agent.ts +++ b/packages/shared/src/validators/agent.ts @@ -73,6 +73,7 @@ export const updateAgentSchema = createAgentSchema .partial() .extend({ permissions: z.never().optional(), + replaceAdapterConfig: z.boolean().optional(), status: z.enum(AGENT_STATUSES).optional(), spentMonthlyCents: z.number().int().nonnegative().optional(), }); diff --git a/packages/shared/src/validators/company-portability.ts b/packages/shared/src/validators/company-portability.ts index cae50e89a3..7cbd4884c5 100644 --- a/packages/shared/src/validators/company-portability.ts +++ b/packages/shared/src/validators/company-portability.ts @@ -38,6 +38,11 @@ export const portabilityCompanyManifestEntrySchema = z.object({ requireBoardApprovalForNewAgents: z.boolean(), }); +export const portabilitySidebarOrderSchema = z.object({ + agents: z.array(z.string().min(1)).default([]), + projects: z.array(z.string().min(1)).default([]), +}); + export const portabilityAgentManifestEntrySchema = z.object({ slug: z.string().min(1), name: z.string().min(1), @@ -85,18 +90,50 @@ export const portabilityProjectManifestEntrySchema = z.object({ color: z.string().nullable(), status: z.string().nullable(), executionWorkspacePolicy: z.record(z.unknown()).nullable(), + workspaces: z.array(z.object({ + key: z.string().min(1), + name: z.string().min(1), + sourceType: z.string().nullable(), + repoUrl: z.string().nullable(), + repoRef: z.string().nullable(), + defaultRef: z.string().nullable(), + visibility: z.string().nullable(), + setupCommand: z.string().nullable(), + cleanupCommand: z.string().nullable(), + metadata: z.record(z.unknown()).nullable(), + isPrimary: z.boolean(), + })).default([]), metadata: z.record(z.unknown()).nullable(), }); +export const portabilityIssueRoutineTriggerManifestEntrySchema = z.object({ + kind: z.string().min(1), + label: z.string().nullable(), + enabled: z.boolean(), + cronExpression: z.string().nullable(), + timezone: z.string().nullable(), + signingMode: z.string().nullable(), + replayWindowSec: z.number().int().nullable(), +}); + +export const portabilityIssueRoutineManifestEntrySchema = z.object({ + concurrencyPolicy: z.string().nullable(), + catchUpPolicy: z.string().nullable(), + triggers: z.array(portabilityIssueRoutineTriggerManifestEntrySchema).default([]), +}); + export const portabilityIssueManifestEntrySchema = z.object({ slug: z.string().min(1), identifier: z.string().min(1).nullable(), title: z.string().min(1), path: z.string().min(1), projectSlug: z.string().min(1).nullable(), + projectWorkspaceKey: z.string().min(1).nullable(), assigneeAgentSlug: z.string().min(1).nullable(), description: z.string().nullable(), - recurrence: z.record(z.unknown()).nullable(), + recurring: z.boolean().default(false), + routine: portabilityIssueRoutineManifestEntrySchema.nullable(), + legacyRecurrence: z.record(z.unknown()).nullable(), status: z.string().nullable(), priority: z.string().nullable(), labelIds: z.array(z.string().min(1)).default([]), @@ -123,6 +160,7 @@ export const portabilityManifestSchema = z.object({ skills: z.boolean(), }), company: portabilityCompanyManifestEntrySchema.nullable(), + sidebar: portabilitySidebarOrderSchema.nullable(), agents: z.array(portabilityAgentManifestEntrySchema), skills: z.array(portabilitySkillManifestEntrySchema).default([]), projects: z.array(portabilityProjectManifestEntrySchema).default([]), @@ -169,6 +207,7 @@ export const companyPortabilityExportSchema = z.object({ projectIssues: z.array(z.string().min(1)).optional(), selectedFiles: z.array(z.string().min(1)).optional(), expandReferencedSkills: z.boolean().optional(), + sidebarOrder: portabilitySidebarOrderSchema.partial().optional(), }); export type CompanyPortabilityExport = z.infer; diff --git a/packages/shared/src/validators/index.ts b/packages/shared/src/validators/index.ts index ce6e701aec..3f33bcebf0 100644 --- a/packages/shared/src/validators/index.ts +++ b/packages/shared/src/validators/index.ts @@ -60,6 +60,7 @@ export { portabilityIncludeSchema, portabilityEnvInputSchema, portabilityCompanyManifestEntrySchema, + portabilitySidebarOrderSchema, portabilityAgentManifestEntrySchema, portabilitySkillManifestEntrySchema, portabilityManifestSchema, @@ -226,6 +227,9 @@ export { acceptInviteSchema, listJoinRequestsQuerySchema, claimJoinRequestApiKeySchema, + boardCliAuthAccessLevelSchema, + createCliAuthChallengeSchema, + resolveCliAuthChallengeSchema, updateMemberPermissionsSchema, updateUserCompanyAccessSchema, type CreateCompanyInvite, @@ -233,6 +237,9 @@ export { type AcceptInvite, type ListJoinRequestsQuery, type ClaimJoinRequestApiKey, + type BoardCliAuthAccessLevel, + type CreateCliAuthChallenge, + type ResolveCliAuthChallenge, type UpdateMemberPermissions, type UpdateUserCompanyAccess, } from "./access.js"; diff --git a/patches/embedded-postgres@18.1.0-beta.16.patch b/patches/embedded-postgres@18.1.0-beta.16.patch new file mode 100644 index 0000000000..0030bd9987 --- /dev/null +++ b/patches/embedded-postgres@18.1.0-beta.16.patch @@ -0,0 +1,30 @@ +diff --git a/dist/index.js b/dist/index.js +--- a/dist/index.js ++++ b/dist/index.js +@@ -23,7 +23,7 @@ + * for a particular string, we need to force that string into the right locale. + * @see https://github.com/leinelissen/embedded-postgres/issues/15 + */ +-const LC_MESSAGES_LOCALE = 'en_US.UTF-8'; ++const LC_MESSAGES_LOCALE = 'C'; + // The default configuration options for the class + const defaults = { + databaseDir: path.join(process.cwd(), 'data', 'db'), +@@ -133,7 +133,7 @@ + `--pwfile=${passwordFile}`, + `--lc-messages=${LC_MESSAGES_LOCALE}`, + ...this.options.initdbFlags, +- ], Object.assign(Object.assign({}, permissionIds), { env: { LC_MESSAGES: LC_MESSAGES_LOCALE } })); ++ ], Object.assign(Object.assign({}, permissionIds), { env: Object.assign(Object.assign({}, globalThis.process.env), { LC_MESSAGES: LC_MESSAGES_LOCALE }) })); + // Connect to stderr, as that is where the messages get sent + (_a = process.stdout) === null || _a === void 0 ? void 0 : _a.on('data', (chunk) => { + // Parse the data as a string and log it +@@ -177,7 +177,7 @@ + '-p', + this.options.port.toString(), + ...this.options.postgresFlags, +- ], Object.assign(Object.assign({}, permissionIds), { env: { LC_MESSAGES: LC_MESSAGES_LOCALE } })); ++ ], Object.assign(Object.assign({}, permissionIds), { env: Object.assign(Object.assign({}, globalThis.process.env), { LC_MESSAGES: LC_MESSAGES_LOCALE }) })); + // Connect to stderr, as that is where the messages get sent + (_a = this.process.stderr) === null || _a === void 0 ? void 0 : _a.on('data', (chunk) => { + // Parse the data as a string and log it diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3bfba71162..98b6f32717 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,11 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +patchedDependencies: + embedded-postgres@18.1.0-beta.16: + hash: 55uhvnotpqyiy37rn3pqpukhei + path: patches/embedded-postgres@18.1.0-beta.16.patch + importers: .: @@ -73,7 +78,7 @@ importers: version: 0.38.4(@electric-sql/pglite@0.3.15)(@types/react@19.2.14)(kysely@0.28.11)(pg@8.18.0)(postgres@3.4.8)(react@19.2.4) embedded-postgres: specifier: ^18.1.0-beta.16 - version: 18.1.0-beta.16 + version: 18.1.0-beta.16(patch_hash=55uhvnotpqyiy37rn3pqpukhei) picocolors: specifier: ^1.1.1 version: 1.1.1 @@ -225,7 +230,7 @@ importers: version: 0.38.4(@electric-sql/pglite@0.3.15)(@types/react@19.2.14)(kysely@0.28.11)(pg@8.18.0)(postgres@3.4.8)(react@19.2.4) embedded-postgres: specifier: ^18.1.0-beta.16 - version: 18.1.0-beta.16 + version: 18.1.0-beta.16(patch_hash=55uhvnotpqyiy37rn3pqpukhei) postgres: specifier: ^3.4.5 version: 3.4.8 @@ -497,7 +502,7 @@ importers: version: 0.38.4(@electric-sql/pglite@0.3.15)(@types/react@19.2.14)(kysely@0.28.11)(pg@8.18.0)(postgres@3.4.8)(react@19.2.4) embedded-postgres: specifier: ^18.1.0-beta.16 - version: 18.1.0-beta.16 + version: 18.1.0-beta.16(patch_hash=55uhvnotpqyiy37rn3pqpukhei) express: specifier: ^5.1.0 version: 5.2.1 @@ -586,6 +591,9 @@ importers: '@dnd-kit/utilities': specifier: ^3.2.2 version: 3.2.2(react@19.2.4) + '@lexical/link': + specifier: 0.35.0 + version: 0.35.0 '@mdxeditor/editor': specifier: ^3.52.4 version: 3.52.4(@codemirror/language@6.12.1)(@lezer/highlight@1.2.3)(@types/react-dom@19.2.3(@types/react@19.2.14))(@types/react@19.2.14)(react-dom@19.2.4(react@19.2.4))(react@19.2.4)(yjs@13.6.29) @@ -634,6 +642,9 @@ importers: cmdk: specifier: ^1.1.1 version: 1.1.1(@types/react-dom@19.2.3(@types/react@19.2.14))(@types/react@19.2.14)(react-dom@19.2.4(react@19.2.4))(react@19.2.4) + lexical: + specifier: 0.35.0 + version: 0.35.0 lucide-react: specifier: ^0.574.0 version: 0.574.0(react@19.2.4) @@ -9979,7 +9990,7 @@ snapshots: electron-to-chromium@1.5.286: {} - embedded-postgres@18.1.0-beta.16: + embedded-postgres@18.1.0-beta.16(patch_hash=55uhvnotpqyiy37rn3pqpukhei): dependencies: async-exit-hook: 2.0.1 pg: 8.18.0 diff --git a/releases/v2026.325.0.md b/releases/v2026.325.0.md new file mode 100644 index 0000000000..21cd49d33a --- /dev/null +++ b/releases/v2026.325.0.md @@ -0,0 +1,77 @@ +# v2026.325.0 + +> Released: 2026-03-25 + +## Highlights + +- **Company import/export** — Full company portability with a file-browser UX for importing and exporting agent companies. Includes rich frontmatter preview, nested file picker, merge-history support, GitHub shorthand refs, and CLI `company import`/`company export` commands. Imported companies open automatically after import, and heartbeat timers are disabled for imported agents by default. ([#840](https://github.com/paperclipai/paperclip/pull/840), [#1631](https://github.com/paperclipai/paperclip/pull/1631), [#1632](https://github.com/paperclipai/paperclip/pull/1632), [#1655](https://github.com/paperclipai/paperclip/pull/1655)) +- **Company skills library** — New company-scoped skills system with a skills UI, agent skill sync across all local adapters (Claude, Codex, Pi, Gemini), pinned GitHub skills with update checks, and built-in skill support. ([#1346](https://github.com/paperclipai/paperclip/pull/1346)) +- **Routines and recurring tasks** — Full routines engine with triggers, routine runs, coalescing, and recurring task portability. Includes API documentation and routine export support. ([#1351](https://github.com/paperclipai/paperclip/pull/1351), [#1622](https://github.com/paperclipai/paperclip/pull/1622), @aronprins) + +## Improvements + +- **Inline join requests in inbox** — Join requests now render inline in the inbox alongside approvals and other work items. +- **Onboarding seeding** — New projects and issues are seeded with goal context during onboarding for a better first-run experience. +- **Agent instructions recovery** — Managed agent instructions are recovered from disk on startup; instructions are preserved across adapter switches. +- **Heartbeats settings page** — Shows all agents regardless of interval config; added a "Disable All" button for quick bulk control. +- **Agent history via participation** — Agent issue history now uses participation records instead of direct assignment lookups. +- **Alphabetical agent sorting** — Agents are sorted alphabetically by name across all views. +- **Company org chart assets** — Improved generated org chart visuals for companies. +- **Improved CLI API connection errors** — Better error messages when the CLI cannot reach the Paperclip API. +- **Markdown mention links** — Custom URL schemes are now allowed in Lexical LinkNode, enabling mention pills with proper linking behavior. Atomic deletion of mention pills works correctly. +- **Issue workspace reuse** — Workspaces are correctly reused after isolation runs. +- **Failed-run session resume** — Explicit failed-run sessions can now be resumed via honor flag. +- **Docker image CI** — Added Docker image build and deploy workflow. ([#542](https://github.com/paperclipai/paperclip/pull/542), @albttx) +- **Project filter on issues** — Issues list can now be filtered by project. ([#552](https://github.com/paperclipai/paperclip/pull/552), @mvanhorn) +- **Inline comment image attachments** — Uploaded images are now embedded inline in comments. ([#551](https://github.com/paperclipai/paperclip/pull/551), @mvanhorn) +- **AGENTS.md fallback** — Claude-local adapter gracefully falls back when AGENTS.md is missing. ([#550](https://github.com/paperclipai/paperclip/pull/550), @mvanhorn) +- **Company-creator skill** — New skill for scaffolding agent company packages from scratch. +- **Reports page rename** — Reports section renamed for clarity. ([#1380](https://github.com/paperclipai/paperclip/pull/1380), @DanielSousa) +- **Eval framework bootstrap** — Promptfoo-based evaluation framework with YAML test cases for systematic agent behavior testing. ([#832](https://github.com/paperclipai/paperclip/pull/832), @mvanhorn) +- **Board CLI authentication** — Browser-based auth flow for the CLI so board users can authenticate without manually copying API keys. ([#1635](https://github.com/paperclipai/paperclip/pull/1635)) + +## Fixes + +- **Embedded Postgres initdb in Docker slim** — Fixed initdb failure in slim containers by adding proper initdbFlags types. ([#737](https://github.com/paperclipai/paperclip/pull/737), @alaa-alghazouli) +- **OpenClaw gateway crash** — Fixed unhandled rejection when challengePromise fails. ([#743](https://github.com/paperclipai/paperclip/pull/743), @Sigmabrogz) +- **Agent mention pill alignment** — Fixed vertical misalignment between agent mention pills and project mention pills. +- **Task assignment grants** — Preserved task assignment grants for agents that have already joined. +- **Instructions tab state** — Fixed tab state not updating correctly when switching between agents. +- **Imported agent bundle frontmatter** — Fixed frontmatter leakage in imported agent bundles. +- **Login form 1Password detection** — Fixed login form not being detected by password managers; Enter key now submits correctly. ([#1014](https://github.com/paperclipai/paperclip/pull/1014)) +- **Pill contrast (WCAG)** — Improved mention pill contrast using WCAG contrast ratios on composited backgrounds. +- **Documents horizontal scroll** — Prevented documents row from causing horizontal scroll on mobile. +- **Toggle switch sizing** — Fixed oversized toggle switches on mobile; added missing `data-slot` attributes. +- **Agent instructions tab responsive** — Made agent instructions tab responsive on mobile. +- **Monospace font sizing** — Adjusted inline code font size and added dark mode background. +- **Priority icon removal** — Removed priority icon from issue rows for a cleaner list view. +- **Same-page issue toasts** — Suppressed redundant toasts when navigating to an issue already on screen. +- **Noisy adapter log** — Removed noisy "Loaded agent instructions file" log message from all adapters. +- **Pi local adapter** — Fixed Pi adapter missing from `isLocal` check. ([#1382](https://github.com/paperclipai/paperclip/pull/1382), @lucas-stellet) +- **CLI auth migration idempotency** — Made migration 0044 idempotent to avoid failures on re-run. +- **Dev restart tracking** — `.paperclip` and test-only paths are now ignored in dev restart detection. +- **Duplicate CLI auth flag** — Fixed duplicate `--company` flag on `auth login`. +- **Gemini local execution** — Fixed Gemini local adapter execution and diagnostics. +- **Sidebar ordering** — Preserved sidebar ordering during company portability operations. +- **Company skill deduplication** — Fixed duplicate skill inventory refreshes. +- **Worktree merge-history migrations** — Fixed migration handling in worktree contexts. ([#1385](https://github.com/paperclipai/paperclip/pull/1385)) + +## Upgrade Guide + +Seven new database migrations (`0038`–`0044`) will run automatically on startup: + +- **Migration 0038** adds process tracking columns to heartbeat runs (PID, started-at, retry tracking). +- **Migration 0039** adds the routines engine tables (routines, triggers, routine runs). +- **Migrations 0040–0042** extend company skills, recurring tasks, and portability metadata. +- **Migration 0043** adds the Codex managed-home and agent instructions recovery columns. +- **Migration 0044** adds board API keys and CLI auth challenge tables for browser-based CLI auth. + +All migrations are additive (new tables and columns) — no existing data is modified. Standard `paperclipai` startup will apply them automatically. + +If you use the company import/export feature, note that imported companies have heartbeat timers disabled by default. Re-enable them manually from the Heartbeats settings page after verifying adapter configuration. + +## Contributors + +Thank you to everyone who contributed to this release! + +@alaa-alghazouli, @albttx, @AOrobator, @aronprins, @cryppadotta, @DanielSousa, @lucas-stellet, @mvanhorn, @richardanaya, @Sigmabrogz diff --git a/scripts/dev-runner-paths.mjs b/scripts/dev-runner-paths.mjs new file mode 100644 index 0000000000..efea8f51c4 --- /dev/null +++ b/scripts/dev-runner-paths.mjs @@ -0,0 +1,38 @@ +const testDirectoryNames = new Set([ + "__tests__", + "_tests", + "test", + "tests", +]); + +const ignoredTestConfigBasenames = new Set([ + "jest.config.cjs", + "jest.config.js", + "jest.config.mjs", + "jest.config.ts", + "playwright.config.ts", + "vitest.config.ts", +]); + +export function shouldTrackDevServerPath(relativePath) { + const normalizedPath = String(relativePath).replaceAll("\\", "/").replace(/^\.\/+/, ""); + if (normalizedPath.length === 0) return false; + + const segments = normalizedPath.split("/"); + const basename = segments.at(-1) ?? normalizedPath; + + if (segments.includes(".paperclip")) { + return false; + } + if (ignoredTestConfigBasenames.has(basename)) { + return false; + } + if (segments.some((segment) => testDirectoryNames.has(segment))) { + return false; + } + if (/\.(test|spec)\.[^/]+$/i.test(basename)) { + return false; + } + + return true; +} diff --git a/scripts/dev-runner.mjs b/scripts/dev-runner.mjs index a0910430f7..091dbb19f6 100644 --- a/scripts/dev-runner.mjs +++ b/scripts/dev-runner.mjs @@ -5,6 +5,7 @@ import path from "node:path"; import { createInterface } from "node:readline/promises"; import { stdin, stdout } from "node:process"; import { fileURLToPath } from "node:url"; +import { shouldTrackDevServerPath } from "./dev-runner-paths.mjs"; const mode = process.argv[2] === "watch" ? "watch" : "dev"; const cliArgs = process.argv.slice(3); @@ -16,7 +17,6 @@ const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".." const devServerStatusFilePath = path.join(repoRoot, ".paperclip", "dev-server-status.json"); const watchedDirectories = [ - ".paperclip", "cli", "scripts", "server", @@ -165,6 +165,7 @@ function readSignature(absolutePath) { function addFileToSnapshot(snapshot, absolutePath) { const relativePath = toRelativePath(absolutePath); if (ignoredRelativePaths.has(relativePath)) return; + if (!shouldTrackDevServerPath(relativePath)) return; snapshot.set(relativePath, readSignature(absolutePath)); } diff --git a/scripts/generate-company-assets.ts b/scripts/generate-company-assets.ts new file mode 100644 index 0000000000..46c6abc7f4 --- /dev/null +++ b/scripts/generate-company-assets.ts @@ -0,0 +1,364 @@ +#!/usr/bin/env npx tsx +/** + * Generate org chart images and READMEs for agent company packages. + * + * Reads company packages from a directory, builds manifest-like data, + * then uses the existing server-side SVG renderer (sharp, no browser) + * and README generator. + * + * Usage: + * npx tsx scripts/generate-company-assets.ts /path/to/companies-repo + * + * Processes each subdirectory that contains a COMPANY.md file. + */ +import * as fs from "fs"; +import * as path from "path"; +import { renderOrgChartPng, type OrgNode, type OrgChartOverlay } from "../server/src/routes/org-chart-svg.js"; +import { generateReadme } from "../server/src/services/company-export-readme.js"; +import type { CompanyPortabilityManifest } from "@paperclipai/shared"; + +// ── YAML frontmatter parser (minimal, no deps) ────────────────── + +function parseFrontmatter(content: string): { data: Record; body: string } { + const match = content.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/); + if (!match) return { data: {}, body: content }; + const yamlStr = match[1]; + const body = match[2]; + const data: Record = {}; + + let currentKey: string | null = null; + let currentValue: string | string[] | null = null; + let inList = false; + + for (const line of yamlStr.split("\n")) { + // List item + if (inList && /^\s+-\s+/.test(line)) { + const val = line.replace(/^\s+-\s+/, "").trim(); + (currentValue as string[]).push(val); + continue; + } + + // Save previous key + if (currentKey !== null && currentValue !== null) { + data[currentKey] = currentValue; + } + inList = false; + + // Key: value line + const kvMatch = line.match(/^(\w[\w-]*)\s*:\s*(.*)$/); + if (kvMatch) { + currentKey = kvMatch[1]; + let val = kvMatch[2].trim(); + + if (val === "" || val === ">") { + // Could be a multi-line value or list — peek ahead handled by next iterations + currentValue = ""; + continue; + } + + if (val === "null" || val === "~") { + currentValue = null; + data[currentKey] = null; + currentKey = null; + currentValue = null; + continue; + } + + // Remove surrounding quotes + if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) { + val = val.slice(1, -1); + } + + currentValue = val; + } else if (currentKey !== null && line.match(/^\s+-\s+/)) { + // Start of list + inList = true; + currentValue = []; + const val = line.replace(/^\s+-\s+/, "").trim(); + (currentValue as string[]).push(val); + } else if (currentKey !== null && line.match(/^\s+\S/)) { + // Continuation of multi-line scalar + const trimmed = line.trim(); + if (typeof currentValue === "string") { + currentValue = currentValue ? `${currentValue} ${trimmed}` : trimmed; + } + } + } + + // Save last key + if (currentKey !== null && currentValue !== null) { + data[currentKey] = currentValue; + } + + return { data, body }; +} + +// ── Slug to role mapping ───────────────────────────────────────── + +const SLUG_TO_ROLE: Record = { + ceo: "ceo", + cto: "cto", + cmo: "cmo", + cfo: "cfo", + coo: "coo", +}; + +function inferRole(slug: string, title: string | null): string { + // Check direct slug match first + if (SLUG_TO_ROLE[slug]) return SLUG_TO_ROLE[slug]; + + // Check title for C-suite + const t = (title || "").toLowerCase(); + if (t.includes("chief executive")) return "ceo"; + if (t.includes("chief technology")) return "cto"; + if (t.includes("chief marketing")) return "cmo"; + if (t.includes("chief financial")) return "cfo"; + if (t.includes("chief operating")) return "coo"; + if (t.includes("vp") || t.includes("vice president")) return "vp"; + if (t.includes("manager")) return "manager"; + if (t.includes("qa") || t.includes("quality")) return "engineer"; + + // Default to engineer + return "engineer"; +} + +// ── Parse a company package directory ──────────────────────────── + +interface CompanyPackage { + dir: string; + name: string; + description: string | null; + slug: string; + agents: CompanyPortabilityManifest["agents"]; + skills: CompanyPortabilityManifest["skills"]; +} + +function parseCompanyPackage(companyDir: string): CompanyPackage | null { + const companyMdPath = path.join(companyDir, "COMPANY.md"); + if (!fs.existsSync(companyMdPath)) return null; + + const companyMd = fs.readFileSync(companyMdPath, "utf-8"); + const { data: companyData } = parseFrontmatter(companyMd); + + const name = (companyData.name as string) || path.basename(companyDir); + const description = (companyData.description as string) || null; + const slug = (companyData.slug as string) || path.basename(companyDir); + + // Parse agents + const agentsDir = path.join(companyDir, "agents"); + const agents: CompanyPortabilityManifest["agents"] = []; + if (fs.existsSync(agentsDir)) { + for (const agentSlug of fs.readdirSync(agentsDir)) { + const agentMdName = fs.existsSync(path.join(agentsDir, agentSlug, "AGENT.md")) + ? "AGENT.md" + : fs.existsSync(path.join(agentsDir, agentSlug, "AGENTS.md")) + ? "AGENTS.md" + : null; + if (!agentMdName) continue; + const agentMdPath = path.join(agentsDir, agentSlug, agentMdName); + + const agentMd = fs.readFileSync(agentMdPath, "utf-8"); + const { data: agentData } = parseFrontmatter(agentMd); + + const agentName = (agentData.name as string) || agentSlug; + const title = (agentData.title as string) || null; + const reportsTo = agentData.reportsTo as string | null; + const skills = (agentData.skills as string[]) || []; + const role = inferRole(agentSlug, title); + + agents.push({ + slug: agentSlug, + name: agentName, + path: `agents/${agentSlug}/${agentMdName}`, + skills, + role, + title, + icon: null, + capabilities: null, + reportsToSlug: reportsTo || null, + adapterType: "claude_local", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + budgetMonthlyCents: 0, + metadata: null, + }); + } + } + + // Parse skills + const skillsDir = path.join(companyDir, "skills"); + const skills: CompanyPortabilityManifest["skills"] = []; + if (fs.existsSync(skillsDir)) { + for (const skillSlug of fs.readdirSync(skillsDir)) { + const skillMdPath = path.join(skillsDir, skillSlug, "SKILL.md"); + if (!fs.existsSync(skillMdPath)) continue; + + const skillMd = fs.readFileSync(skillMdPath, "utf-8"); + const { data: skillData } = parseFrontmatter(skillMd); + + const skillName = (skillData.name as string) || skillSlug; + const skillDesc = (skillData.description as string) || null; + + // Extract source info from metadata + let sourceType = "local"; + let sourceLocator: string | null = null; + const metadata = skillData.metadata as Record | undefined; + if (metadata) { + // metadata.sources is parsed as a nested structure, but our simple parser + // doesn't handle it well. Check for github repo in the raw SKILL.md instead. + const repoMatch = skillMd.match(/repo:\s*(.+)/); + const pathMatch = skillMd.match(/path:\s*(.+)/); + if (repoMatch) { + sourceType = "github"; + const repo = repoMatch[1].trim(); + const filePath = pathMatch ? pathMatch[1].trim() : ""; + sourceLocator = `https://github.com/${repo}/blob/main/${filePath}`; + } + } + + skills.push({ + key: skillSlug, + slug: skillSlug, + name: skillName, + path: `skills/${skillSlug}/SKILL.md`, + description: skillDesc, + sourceType, + sourceLocator, + sourceRef: null, + trustLevel: null, + compatibility: null, + metadata: null, + fileInventory: [{ path: `skills/${skillSlug}/SKILL.md`, kind: "skill" }], + }); + } + } + + return { dir: companyDir, name, description, slug, agents, skills }; +} + +// ── Build OrgNode tree from agents ─────────────────────────────── + +const ROLE_LABELS: Record = { + ceo: "Chief Executive", + cto: "Technology", + cmo: "Marketing", + cfo: "Finance", + coo: "Operations", + vp: "VP", + manager: "Manager", + engineer: "Engineer", + agent: "Agent", +}; + +function buildOrgTree(agents: CompanyPortabilityManifest["agents"]): OrgNode[] { + const bySlug = new Map(agents.map((a) => [a.slug, a])); + const childrenOf = new Map(); + for (const a of agents) { + const parent = a.reportsToSlug ?? null; + const list = childrenOf.get(parent) ?? []; + list.push(a); + childrenOf.set(parent, list); + } + const build = (parentSlug: string | null): OrgNode[] => { + const members = childrenOf.get(parentSlug) ?? []; + return members.map((m) => ({ + id: m.slug, + name: m.name, + role: ROLE_LABELS[m.role] ?? m.role, + status: "active", + reports: build(m.slug), + })); + }; + const roots = agents.filter((a) => !a.reportsToSlug || !bySlug.has(a.reportsToSlug)); + const tree = build(null); + for (const root of roots) { + if (root.reportsToSlug && !bySlug.has(root.reportsToSlug)) { + tree.push({ + id: root.slug, + name: root.name, + role: ROLE_LABELS[root.role] ?? root.role, + status: "active", + reports: build(root.slug), + }); + } + } + return tree; +} + +// ── Main ───────────────────────────────────────────────────────── + +async function main() { + const companiesDir = process.argv[2]; + if (!companiesDir) { + console.error("Usage: npx tsx scripts/generate-company-assets.ts "); + process.exit(1); + } + + const resolvedDir = path.resolve(companiesDir); + if (!fs.existsSync(resolvedDir)) { + console.error(`Directory not found: ${resolvedDir}`); + process.exit(1); + } + + const entries = fs.readdirSync(resolvedDir, { withFileTypes: true }); + let processed = 0; + + for (const entry of entries) { + if (!entry.isDirectory()) continue; + const companyDir = path.join(resolvedDir, entry.name); + const pkg = parseCompanyPackage(companyDir); + if (!pkg) continue; + + console.log(`\n── ${pkg.name} (${pkg.slug}) ──`); + console.log(` ${pkg.agents.length} agents, ${pkg.skills.length} skills`); + + // Generate org chart PNG + if (pkg.agents.length > 0) { + const orgTree = buildOrgTree(pkg.agents); + console.log(` Org tree roots: ${orgTree.map((n) => n.name).join(", ")}`); + + const overlay: OrgChartOverlay = { + companyName: pkg.name, + stats: `Agents: ${pkg.agents.length}, Skills: ${pkg.skills.length}`, + }; + const pngBuffer = await renderOrgChartPng(orgTree, "warmth", overlay); + const imagesDir = path.join(companyDir, "images"); + fs.mkdirSync(imagesDir, { recursive: true }); + const pngPath = path.join(imagesDir, "org-chart.png"); + fs.writeFileSync(pngPath, pngBuffer); + console.log(` ✓ ${path.relative(resolvedDir, pngPath)} (${(pngBuffer.length / 1024).toFixed(1)}kb)`); + } + + // Generate README + const manifest: CompanyPortabilityManifest = { + schemaVersion: 1, + generatedAt: new Date().toISOString(), + source: null, + includes: { company: true, agents: true, projects: false, issues: false, skills: true }, + company: null, + agents: pkg.agents, + skills: pkg.skills, + projects: [], + issues: [], + envInputs: [], + }; + + const readme = generateReadme(manifest, { + companyName: pkg.name, + companyDescription: pkg.description, + }); + const readmePath = path.join(companyDir, "README.md"); + fs.writeFileSync(readmePath, readme); + console.log(` ✓ ${path.relative(resolvedDir, readmePath)}`); + + processed++; + } + + console.log(`\n✓ Processed ${processed} companies.`); +} + +main().catch((e) => { + console.error(e); + process.exit(1); +}); diff --git a/server/src/__tests__/agent-instructions-routes.test.ts b/server/src/__tests__/agent-instructions-routes.test.ts index 4f6ca414f6..16b16ca38a 100644 --- a/server/src/__tests__/agent-instructions-routes.test.ts +++ b/server/src/__tests__/agent-instructions-routes.test.ts @@ -197,4 +197,122 @@ describe("agent instructions bundle routes", () => { expect.any(Object), ); }); + + it("preserves managed instructions config when switching adapters", async () => { + mockAgentService.getById.mockResolvedValue({ + ...makeAgent(), + adapterType: "codex_local", + adapterConfig: { + instructionsBundleMode: "managed", + instructionsRootPath: "/tmp/agent-1", + instructionsEntryFile: "AGENTS.md", + instructionsFilePath: "/tmp/agent-1/AGENTS.md", + model: "gpt-5.4", + }, + }); + + const res = await request(createApp()) + .patch("/api/agents/11111111-1111-4111-8111-111111111111?companyId=company-1") + .send({ + adapterType: "claude_local", + adapterConfig: { + model: "claude-sonnet-4", + }, + }); + + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(mockAgentService.update).toHaveBeenCalledWith( + "11111111-1111-4111-8111-111111111111", + expect.objectContaining({ + adapterType: "claude_local", + adapterConfig: expect.objectContaining({ + model: "claude-sonnet-4", + instructionsBundleMode: "managed", + instructionsRootPath: "/tmp/agent-1", + instructionsEntryFile: "AGENTS.md", + instructionsFilePath: "/tmp/agent-1/AGENTS.md", + }), + }), + expect.any(Object), + ); + }); + + it("merges same-adapter config patches so instructions metadata is not dropped", async () => { + mockAgentService.getById.mockResolvedValue({ + ...makeAgent(), + adapterType: "codex_local", + adapterConfig: { + instructionsBundleMode: "managed", + instructionsRootPath: "/tmp/agent-1", + instructionsEntryFile: "AGENTS.md", + instructionsFilePath: "/tmp/agent-1/AGENTS.md", + model: "gpt-5.4", + }, + }); + + const res = await request(createApp()) + .patch("/api/agents/11111111-1111-4111-8111-111111111111?companyId=company-1") + .send({ + adapterConfig: { + command: "codex --profile engineer", + }, + }); + + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(mockAgentService.update).toHaveBeenCalledWith( + "11111111-1111-4111-8111-111111111111", + expect.objectContaining({ + adapterConfig: expect.objectContaining({ + command: "codex --profile engineer", + model: "gpt-5.4", + instructionsBundleMode: "managed", + instructionsRootPath: "/tmp/agent-1", + instructionsEntryFile: "AGENTS.md", + instructionsFilePath: "/tmp/agent-1/AGENTS.md", + }), + }), + expect.any(Object), + ); + }); + + it("replaces adapter config when replaceAdapterConfig is true", async () => { + mockAgentService.getById.mockResolvedValue({ + ...makeAgent(), + adapterType: "codex_local", + adapterConfig: { + instructionsBundleMode: "managed", + instructionsRootPath: "/tmp/agent-1", + instructionsEntryFile: "AGENTS.md", + instructionsFilePath: "/tmp/agent-1/AGENTS.md", + model: "gpt-5.4", + }, + }); + + const res = await request(createApp()) + .patch("/api/agents/11111111-1111-4111-8111-111111111111?companyId=company-1") + .send({ + replaceAdapterConfig: true, + adapterConfig: { + command: "codex --profile engineer", + }, + }); + + expect(res.status, JSON.stringify(res.body)).toBe(200); + expect(mockAgentService.update).toHaveBeenCalledWith( + "11111111-1111-4111-8111-111111111111", + expect.objectContaining({ + adapterConfig: expect.objectContaining({ + command: "codex --profile engineer", + }), + }), + expect.any(Object), + ); + expect(res.body.adapterConfig).toMatchObject({ + command: "codex --profile engineer", + }); + expect(res.body.adapterConfig.instructionsBundleMode).toBeUndefined(); + expect(res.body.adapterConfig.instructionsRootPath).toBeUndefined(); + expect(res.body.adapterConfig.instructionsEntryFile).toBeUndefined(); + expect(res.body.adapterConfig.instructionsFilePath).toBeUndefined(); + }); }); diff --git a/server/src/__tests__/agent-instructions-service.test.ts b/server/src/__tests__/agent-instructions-service.test.ts index 0e0d9d3915..67eea3ca85 100644 --- a/server/src/__tests__/agent-instructions-service.test.ts +++ b/server/src/__tests__/agent-instructions-service.test.ts @@ -161,4 +161,201 @@ describe("agent instructions service", () => { "docs/TOOLS.md", ]); }); + + it("recovers a managed bundle from disk when bundle config metadata is missing", async () => { + const paperclipHome = await makeTempDir("paperclip-agent-instructions-recover-"); + cleanupDirs.add(paperclipHome); + process.env.PAPERCLIP_HOME = paperclipHome; + process.env.PAPERCLIP_INSTANCE_ID = "test-instance"; + + const managedRoot = path.join( + paperclipHome, + "instances", + "test-instance", + "companies", + "company-1", + "agents", + "agent-1", + "instructions", + ); + await fs.mkdir(managedRoot, { recursive: true }); + await fs.writeFile(path.join(managedRoot, "AGENTS.md"), "# Recovered Agent\n", "utf8"); + + const svc = agentInstructionsService(); + const agent = makeAgent({}); + + const bundle = await svc.getBundle(agent); + const exported = await svc.exportFiles(agent); + + expect(bundle.mode).toBe("managed"); + expect(bundle.rootPath).toBe(managedRoot); + expect(bundle.files.map((file) => file.path)).toEqual(["AGENTS.md"]); + expect(exported.files).toEqual({ "AGENTS.md": "# Recovered Agent\n" }); + }); + + it("prefers the managed bundle on disk when managed metadata points at a stale root", async () => { + const paperclipHome = await makeTempDir("paperclip-agent-instructions-stale-managed-"); + const staleRoot = await makeTempDir("paperclip-agent-instructions-stale-root-"); + cleanupDirs.add(paperclipHome); + cleanupDirs.add(staleRoot); + process.env.PAPERCLIP_HOME = paperclipHome; + process.env.PAPERCLIP_INSTANCE_ID = "test-instance"; + + const managedRoot = path.join( + paperclipHome, + "instances", + "test-instance", + "companies", + "company-1", + "agents", + "agent-1", + "instructions", + ); + await fs.mkdir(managedRoot, { recursive: true }); + await fs.writeFile(path.join(managedRoot, "AGENTS.md"), "# Managed Agent\n", "utf8"); + + const svc = agentInstructionsService(); + const agent = makeAgent({ + instructionsBundleMode: "managed", + instructionsRootPath: staleRoot, + instructionsEntryFile: "docs/MISSING.md", + instructionsFilePath: path.join(staleRoot, "docs", "MISSING.md"), + }); + + const bundle = await svc.getBundle(agent); + const exported = await svc.exportFiles(agent); + + expect(bundle.mode).toBe("managed"); + expect(bundle.rootPath).toBe(managedRoot); + expect(bundle.entryFile).toBe("AGENTS.md"); + expect(bundle.files.map((file) => file.path)).toEqual(["AGENTS.md"]); + expect(bundle.warnings).toEqual([ + `Recovered managed instructions from disk at ${managedRoot}; ignoring stale configured root ${staleRoot}.`, + "Recovered managed instructions entry file from disk as AGENTS.md; previous entry docs/MISSING.md was missing.", + ]); + expect(exported.files).toEqual({ "AGENTS.md": "# Managed Agent\n" }); + }); + + it("heals stale managed metadata when writing bundle files", async () => { + const paperclipHome = await makeTempDir("paperclip-agent-instructions-heal-write-"); + const staleRoot = await makeTempDir("paperclip-agent-instructions-heal-write-stale-"); + cleanupDirs.add(paperclipHome); + cleanupDirs.add(staleRoot); + process.env.PAPERCLIP_HOME = paperclipHome; + process.env.PAPERCLIP_INSTANCE_ID = "test-instance"; + + const managedRoot = path.join( + paperclipHome, + "instances", + "test-instance", + "companies", + "company-1", + "agents", + "agent-1", + "instructions", + ); + await fs.mkdir(path.join(managedRoot, "docs"), { recursive: true }); + await fs.writeFile(path.join(managedRoot, "AGENTS.md"), "# Managed Agent\n", "utf8"); + + const svc = agentInstructionsService(); + const agent = makeAgent({ + instructionsBundleMode: "managed", + instructionsRootPath: staleRoot, + instructionsEntryFile: "docs/MISSING.md", + instructionsFilePath: path.join(staleRoot, "docs", "MISSING.md"), + }); + + const result = await svc.writeFile(agent, "docs/TOOLS.md", "## Tools\n"); + + expect(result.adapterConfig).toMatchObject({ + instructionsBundleMode: "managed", + instructionsRootPath: managedRoot, + instructionsEntryFile: "AGENTS.md", + instructionsFilePath: path.join(managedRoot, "AGENTS.md"), + }); + await expect(fs.readFile(path.join(managedRoot, "docs", "TOOLS.md"), "utf8")).resolves.toBe("## Tools\n"); + }); + + it("heals stale managed metadata when deleting bundle files", async () => { + const paperclipHome = await makeTempDir("paperclip-agent-instructions-heal-delete-"); + const staleRoot = await makeTempDir("paperclip-agent-instructions-heal-delete-stale-"); + cleanupDirs.add(paperclipHome); + cleanupDirs.add(staleRoot); + process.env.PAPERCLIP_HOME = paperclipHome; + process.env.PAPERCLIP_INSTANCE_ID = "test-instance"; + + const managedRoot = path.join( + paperclipHome, + "instances", + "test-instance", + "companies", + "company-1", + "agents", + "agent-1", + "instructions", + ); + await fs.mkdir(path.join(managedRoot, "docs"), { recursive: true }); + await fs.writeFile(path.join(managedRoot, "AGENTS.md"), "# Managed Agent\n", "utf8"); + await fs.writeFile(path.join(managedRoot, "docs", "TOOLS.md"), "## Tools\n", "utf8"); + + const svc = agentInstructionsService(); + const agent = makeAgent({ + instructionsBundleMode: "managed", + instructionsRootPath: staleRoot, + instructionsEntryFile: "docs/MISSING.md", + instructionsFilePath: path.join(staleRoot, "docs", "MISSING.md"), + }); + + const result = await svc.deleteFile(agent, "docs/TOOLS.md"); + + expect(result.adapterConfig).toMatchObject({ + instructionsBundleMode: "managed", + instructionsRootPath: managedRoot, + instructionsEntryFile: "AGENTS.md", + instructionsFilePath: path.join(managedRoot, "AGENTS.md"), + }); + await expect(fs.stat(path.join(managedRoot, "docs", "TOOLS.md"))).rejects.toThrow(); + expect(result.bundle.files.map((file) => file.path)).toEqual(["AGENTS.md"]); + }); + + it("recovers the managed bundle when stale root metadata is present but mode is missing", async () => { + const paperclipHome = await makeTempDir("paperclip-agent-instructions-partial-managed-"); + const staleRoot = await makeTempDir("paperclip-agent-instructions-partial-root-"); + cleanupDirs.add(paperclipHome); + cleanupDirs.add(staleRoot); + process.env.PAPERCLIP_HOME = paperclipHome; + process.env.PAPERCLIP_INSTANCE_ID = "test-instance"; + + const managedRoot = path.join( + paperclipHome, + "instances", + "test-instance", + "companies", + "company-1", + "agents", + "agent-1", + "instructions", + ); + await fs.mkdir(managedRoot, { recursive: true }); + await fs.writeFile(path.join(managedRoot, "AGENTS.md"), "# Managed Agent\n", "utf8"); + + const svc = agentInstructionsService(); + const agent = makeAgent({ + instructionsRootPath: staleRoot, + instructionsEntryFile: "docs/MISSING.md", + }); + + const bundle = await svc.getBundle(agent); + const exported = await svc.exportFiles(agent); + + expect(bundle.mode).toBe("managed"); + expect(bundle.rootPath).toBe(managedRoot); + expect(bundle.entryFile).toBe("AGENTS.md"); + expect(bundle.files.map((file) => file.path)).toEqual(["AGENTS.md"]); + expect(bundle.warnings).toEqual([ + `Recovered managed instructions from disk at ${managedRoot}; ignoring stale configured root ${staleRoot}.`, + "Recovered managed instructions entry file from disk as AGENTS.md; previous entry docs/MISSING.md was missing.", + ]); + expect(exported.files).toEqual({ "AGENTS.md": "# Managed Agent\n" }); + }); }); diff --git a/server/src/__tests__/board-mutation-guard.test.ts b/server/src/__tests__/board-mutation-guard.test.ts index aff95a7d58..62e1e68e39 100644 --- a/server/src/__tests__/board-mutation-guard.test.ts +++ b/server/src/__tests__/board-mutation-guard.test.ts @@ -3,7 +3,10 @@ import express from "express"; import request from "supertest"; import { boardMutationGuard } from "../middleware/board-mutation-guard.js"; -function createApp(actorType: "board" | "agent", boardSource: "session" | "local_implicit" = "session") { +function createApp( + actorType: "board" | "agent", + boardSource: "session" | "local_implicit" | "board_key" = "session", +) { const app = express(); app.use(express.json()); app.use((req, _res, next) => { @@ -29,11 +32,26 @@ describe("boardMutationGuard", () => { expect(res.status).toBe(204); }); - it("blocks board mutations without trusted origin", async () => { - const app = createApp("board"); - const res = await request(app).post("/mutate").send({ ok: true }); - expect(res.status).toBe(403); - expect(res.body).toEqual({ error: "Board mutation requires trusted browser origin" }); + it("blocks board mutations without trusted origin", () => { + const middleware = boardMutationGuard(); + const req = { + method: "POST", + actor: { type: "board", userId: "board", source: "session" }, + header: () => undefined, + } as any; + const res = { + status: vi.fn().mockReturnThis(), + json: vi.fn(), + } as any; + const next = vi.fn(); + + middleware(req, res, next); + + expect(next).not.toHaveBeenCalled(); + expect(res.status).toHaveBeenCalledWith(403); + expect(res.json).toHaveBeenCalledWith({ + error: "Board mutation requires trusted browser origin", + }); }); it("allows local implicit board mutations without origin", async () => { @@ -42,6 +60,12 @@ describe("boardMutationGuard", () => { expect(res.status).toBe(204); }); + it("allows board bearer-key mutations without origin", async () => { + const app = createApp("board", "board_key"); + const res = await request(app).post("/mutate").send({ ok: true }); + expect(res.status).toBe(204); + }); + it("allows board mutations from trusted origin", async () => { const app = createApp("board"); const res = await request(app) diff --git a/server/src/__tests__/cli-auth-routes.test.ts b/server/src/__tests__/cli-auth-routes.test.ts new file mode 100644 index 0000000000..d24916730d --- /dev/null +++ b/server/src/__tests__/cli-auth-routes.test.ts @@ -0,0 +1,230 @@ +import express from "express"; +import request from "supertest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const mockAccessService = vi.hoisted(() => ({ + isInstanceAdmin: vi.fn(), + hasPermission: vi.fn(), + canUser: vi.fn(), +})); + +const mockAgentService = vi.hoisted(() => ({ + getById: vi.fn(), +})); + +const mockBoardAuthService = vi.hoisted(() => ({ + createCliAuthChallenge: vi.fn(), + describeCliAuthChallenge: vi.fn(), + approveCliAuthChallenge: vi.fn(), + cancelCliAuthChallenge: vi.fn(), + resolveBoardAccess: vi.fn(), + resolveBoardActivityCompanyIds: vi.fn(), + assertCurrentBoardKey: vi.fn(), + revokeBoardApiKey: vi.fn(), +})); + +const mockLogActivity = vi.hoisted(() => vi.fn()); + +vi.mock("../services/index.js", () => ({ + accessService: () => mockAccessService, + agentService: () => mockAgentService, + boardAuthService: () => mockBoardAuthService, + logActivity: mockLogActivity, + notifyHireApproved: vi.fn(), + deduplicateAgentName: vi.fn((name: string) => name), +})); + +function createApp(actor: any) { + const app = express(); + app.use(express.json()); + app.use((req, _res, next) => { + req.actor = actor; + next(); + }); + return import("../routes/access.js").then(({ accessRoutes }) => + import("../middleware/index.js").then(({ errorHandler }) => { + app.use( + "/api", + accessRoutes({} as any, { + deploymentMode: "authenticated", + deploymentExposure: "private", + bindHost: "127.0.0.1", + allowedHostnames: [], + }), + ); + app.use(errorHandler); + return app; + }) + ); +} + +describe("cli auth routes", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("creates a CLI auth challenge with approval metadata", async () => { + mockBoardAuthService.createCliAuthChallenge.mockResolvedValue({ + challenge: { + id: "challenge-1", + expiresAt: new Date("2026-03-23T13:00:00.000Z"), + }, + challengeSecret: "pcp_cli_auth_secret", + pendingBoardToken: "pcp_board_token", + }); + + const app = await createApp({ type: "none", source: "none" }); + const res = await request(app) + .post("/api/cli-auth/challenges") + .send({ + command: "paperclipai company import", + clientName: "paperclipai cli", + requestedAccess: "board", + }); + + expect(res.status).toBe(201); + expect(res.body).toMatchObject({ + id: "challenge-1", + token: "pcp_cli_auth_secret", + boardApiToken: "pcp_board_token", + approvalPath: "/cli-auth/challenge-1?token=pcp_cli_auth_secret", + pollPath: "/cli-auth/challenges/challenge-1", + expiresAt: "2026-03-23T13:00:00.000Z", + }); + expect(res.body.approvalUrl).toContain("/cli-auth/challenge-1?token=pcp_cli_auth_secret"); + }); + + it("marks challenge status as requiring sign-in for anonymous viewers", async () => { + mockBoardAuthService.describeCliAuthChallenge.mockResolvedValue({ + id: "challenge-1", + status: "pending", + command: "paperclipai company import", + clientName: "paperclipai cli", + requestedAccess: "board", + requestedCompanyId: null, + requestedCompanyName: null, + approvedAt: null, + cancelledAt: null, + expiresAt: "2026-03-23T13:00:00.000Z", + approvedByUser: null, + }); + + const app = await createApp({ type: "none", source: "none" }); + const res = await request(app).get("/api/cli-auth/challenges/challenge-1?token=pcp_cli_auth_secret"); + + expect(res.status).toBe(200); + expect(res.body.requiresSignIn).toBe(true); + expect(res.body.canApprove).toBe(false); + }); + + it("approves a CLI auth challenge for a signed-in board user", async () => { + mockBoardAuthService.approveCliAuthChallenge.mockResolvedValue({ + status: "approved", + challenge: { + id: "challenge-1", + boardApiKeyId: "board-key-1", + requestedAccess: "board", + requestedCompanyId: "company-1", + expiresAt: new Date("2026-03-23T13:00:00.000Z"), + }, + }); + mockBoardAuthService.resolveBoardAccess.mockResolvedValue({ + user: { id: "user-1", name: "User One", email: "user@example.com" }, + companyIds: ["company-1"], + isInstanceAdmin: false, + }); + mockBoardAuthService.resolveBoardActivityCompanyIds.mockResolvedValue(["company-1"]); + + const app = await createApp({ + type: "board", + userId: "user-1", + source: "session", + isInstanceAdmin: false, + companyIds: ["company-1"], + }); + const res = await request(app) + .post("/api/cli-auth/challenges/challenge-1/approve") + .send({ token: "pcp_cli_auth_secret" }); + + expect(res.status).toBe(200); + expect(res.body).toEqual({ + approved: true, + status: "approved", + userId: "user-1", + keyId: "board-key-1", + expiresAt: "2026-03-23T13:00:00.000Z", + }); + expect(mockLogActivity).toHaveBeenCalledTimes(1); + expect(mockLogActivity).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + companyId: "company-1", + action: "board_api_key.created", + }), + ); + }); + + it("logs approve activity for instance admins without company memberships", async () => { + mockBoardAuthService.approveCliAuthChallenge.mockResolvedValue({ + status: "approved", + challenge: { + id: "challenge-2", + boardApiKeyId: "board-key-2", + requestedAccess: "instance_admin_required", + requestedCompanyId: null, + expiresAt: new Date("2026-03-23T13:00:00.000Z"), + }, + }); + mockBoardAuthService.resolveBoardActivityCompanyIds.mockResolvedValue(["company-a", "company-b"]); + + const app = await createApp({ + type: "board", + userId: "admin-1", + source: "session", + isInstanceAdmin: true, + companyIds: [], + }); + const res = await request(app) + .post("/api/cli-auth/challenges/challenge-2/approve") + .send({ token: "pcp_cli_auth_secret" }); + + expect(res.status).toBe(200); + expect(mockBoardAuthService.resolveBoardActivityCompanyIds).toHaveBeenCalledWith({ + userId: "admin-1", + requestedCompanyId: null, + boardApiKeyId: "board-key-2", + }); + expect(mockLogActivity).toHaveBeenCalledTimes(2); + }); + + it("logs revoke activity with resolved audit company ids", async () => { + mockBoardAuthService.assertCurrentBoardKey.mockResolvedValue({ + id: "board-key-3", + userId: "admin-2", + }); + mockBoardAuthService.resolveBoardActivityCompanyIds.mockResolvedValue(["company-z"]); + + const app = await createApp({ + type: "board", + userId: "admin-2", + keyId: "board-key-3", + source: "board_key", + isInstanceAdmin: true, + companyIds: [], + }); + const res = await request(app).post("/api/cli-auth/revoke-current").send({}); + + expect(res.status).toBe(200); + expect(mockBoardAuthService.resolveBoardActivityCompanyIds).toHaveBeenCalledWith({ + userId: "admin-2", + boardApiKeyId: "board-key-3", + }); + expect(mockLogActivity).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + companyId: "company-z", + action: "board_api_key.revoked", + }), + ); + }); +}); diff --git a/server/src/__tests__/codex-local-adapter-environment.test.ts b/server/src/__tests__/codex-local-adapter-environment.test.ts index a9201c9828..ba92a22454 100644 --- a/server/src/__tests__/codex-local-adapter-environment.test.ts +++ b/server/src/__tests__/codex-local-adapter-environment.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; @@ -7,6 +7,12 @@ import { testEnvironment } from "@paperclipai/adapter-codex-local/server"; const itWindows = process.platform === "win32" ? it : it.skip; describe("codex_local environment diagnostics", () => { + beforeEach(() => { + vi.stubEnv("OPENAI_API_KEY", ""); + }); + afterEach(() => { + vi.unstubAllEnvs(); + }); it("creates a missing working directory when cwd is absolute", async () => { const cwd = path.join( os.tmpdir(), @@ -32,6 +38,67 @@ describe("codex_local environment diagnostics", () => { await fs.rm(path.dirname(cwd), { recursive: true, force: true }); }); + it("emits codex_native_auth_present when ~/.codex/auth.json exists and OPENAI_API_KEY is unset", async () => { + const root = path.join( + os.tmpdir(), + `paperclip-codex-auth-${Date.now()}-${Math.random().toString(16).slice(2)}`, + ); + const codexHome = path.join(root, ".codex"); + const cwd = path.join(root, "workspace"); + + try { + await fs.mkdir(codexHome, { recursive: true }); + await fs.writeFile( + path.join(codexHome, "auth.json"), + JSON.stringify({ accessToken: "fake-token", accountId: "acct-1" }), + ); + + const result = await testEnvironment({ + companyId: "company-1", + adapterType: "codex_local", + config: { + command: process.execPath, + cwd, + env: { CODEX_HOME: codexHome }, + }, + }); + + expect(result.checks.some((check) => check.code === "codex_native_auth_present")).toBe(true); + expect(result.checks.some((check) => check.code === "codex_openai_api_key_missing")).toBe(false); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } + }); + + it("emits codex_openai_api_key_missing when neither env var nor native auth exists", async () => { + const root = path.join( + os.tmpdir(), + `paperclip-codex-noauth-${Date.now()}-${Math.random().toString(16).slice(2)}`, + ); + const codexHome = path.join(root, ".codex"); + const cwd = path.join(root, "workspace"); + + try { + await fs.mkdir(codexHome, { recursive: true }); + // No auth.json written + + const result = await testEnvironment({ + companyId: "company-1", + adapterType: "codex_local", + config: { + command: process.execPath, + cwd, + env: { CODEX_HOME: codexHome }, + }, + }); + + expect(result.checks.some((check) => check.code === "codex_openai_api_key_missing")).toBe(true); + expect(result.checks.some((check) => check.code === "codex_native_auth_present")).toBe(false); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } + }); + itWindows("runs the hello probe when Codex is available via a Windows .cmd wrapper", async () => { const root = path.join( os.tmpdir(), diff --git a/server/src/__tests__/codex-local-execute.test.ts b/server/src/__tests__/codex-local-execute.test.ts index 3f1f15dfdd..b83e3db7bd 100644 --- a/server/src/__tests__/codex-local-execute.test.ts +++ b/server/src/__tests__/codex-local-execute.test.ts @@ -139,6 +139,62 @@ describe("codex execute", () => { } }); + it("emits a command note that Codex auto-applies repo-scoped AGENTS.md files", async () => { + const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-codex-execute-notes-")); + const workspace = path.join(root, "workspace"); + const commandPath = path.join(root, "codex"); + const capturePath = path.join(root, "capture.json"); + await fs.mkdir(workspace, { recursive: true }); + await writeFakeCodexCommand(commandPath); + + const previousHome = process.env.HOME; + process.env.HOME = root; + + let commandNotes: string[] = []; + try { + const result = await execute({ + runId: "run-notes", + agent: { + id: "agent-1", + companyId: "company-1", + name: "Codex Coder", + adapterType: "codex_local", + adapterConfig: {}, + }, + runtime: { + sessionId: null, + sessionParams: null, + sessionDisplayId: null, + taskKey: null, + }, + config: { + command: commandPath, + cwd: workspace, + env: { + PAPERCLIP_TEST_CAPTURE_PATH: capturePath, + }, + promptTemplate: "Follow the paperclip heartbeat.", + }, + context: {}, + authToken: "run-jwt-token", + onLog: async () => {}, + onMeta: async (meta) => { + commandNotes = Array.isArray(meta.commandNotes) ? meta.commandNotes : []; + }, + }); + + expect(result.exitCode).toBe(0); + expect(result.errorMessage).toBeNull(); + expect(commandNotes).toContain( + "Codex exec automatically applies repo-scoped AGENTS.md instructions from the current workspace; Paperclip does not currently suppress that discovery.", + ); + } finally { + if (previousHome === undefined) delete process.env.HOME; + else process.env.HOME = previousHome; + await fs.rm(root, { recursive: true, force: true }); + } + }); + it("uses a worktree-isolated CODEX_HOME while preserving shared auth and config", async () => { const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-codex-execute-")); const workspace = path.join(root, "workspace"); @@ -154,7 +210,7 @@ describe("codex execute", () => { "company-1", "codex-home", ); - const workspaceSkill = path.join(workspace, ".agents", "skills", "paperclip"); + const homeSkill = path.join(isolatedCodexHome, "skills", "paperclip"); await fs.mkdir(workspace, { recursive: true }); await fs.mkdir(sharedCodexHome, { recursive: true }); await fs.writeFile(path.join(sharedCodexHome, "auth.json"), '{"token":"shared"}\n', "utf8"); @@ -228,7 +284,7 @@ describe("codex execute", () => { expect(await fs.realpath(isolatedAuth)).toBe(await fs.realpath(path.join(sharedCodexHome, "auth.json"))); expect((await fs.lstat(isolatedConfig)).isFile()).toBe(true); expect(await fs.readFile(isolatedConfig, "utf8")).toBe('model = "codex-mini-latest"\n'); - expect((await fs.lstat(workspaceSkill)).isSymbolicLink()).toBe(true); + expect((await fs.lstat(homeSkill)).isSymbolicLink()).toBe(true); expect(logs).toContainEqual( expect.objectContaining({ stream: "stdout", @@ -315,7 +371,7 @@ describe("codex execute", () => { const capture = JSON.parse(await fs.readFile(capturePath, "utf8")) as CapturePayload; expect(capture.codexHome).toBe(explicitCodexHome); - expect((await fs.lstat(path.join(workspace, ".agents", "skills", "paperclip"))).isSymbolicLink()).toBe(true); + expect((await fs.lstat(path.join(explicitCodexHome, "skills", "paperclip"))).isSymbolicLink()).toBe(true); await expect(fs.lstat(path.join(paperclipHome, "instances", "worktree-1", "codex-home"))).rejects.toThrow(); } finally { if (previousHome === undefined) delete process.env.HOME; diff --git a/server/src/__tests__/codex-local-skill-sync.test.ts b/server/src/__tests__/codex-local-skill-sync.test.ts index b809ebf89b..0205f22d77 100644 --- a/server/src/__tests__/codex-local-skill-sync.test.ts +++ b/server/src/__tests__/codex-local-skill-sync.test.ts @@ -43,7 +43,7 @@ describe("codex local skill sync", () => { expect(before.desiredSkills).toContain(paperclipKey); expect(before.entries.find((entry) => entry.key === paperclipKey)?.required).toBe(true); expect(before.entries.find((entry) => entry.key === paperclipKey)?.state).toBe("configured"); - expect(before.entries.find((entry) => entry.key === paperclipKey)?.detail).toContain(".agents/skills"); + expect(before.entries.find((entry) => entry.key === paperclipKey)?.detail).toContain("CODEX_HOME/skills/"); }); it("does not persist Paperclip skills into CODEX_HOME during sync", async () => { diff --git a/server/src/__tests__/company-portability.test.ts b/server/src/__tests__/company-portability.test.ts index ff019530c2..a3410df6ad 100644 --- a/server/src/__tests__/company-portability.test.ts +++ b/server/src/__tests__/company-portability.test.ts @@ -1,3 +1,7 @@ +import { execFileSync } from "node:child_process"; +import { promises as fs } from "node:fs"; +import os from "node:os"; +import path from "node:path"; import { Readable } from "node:stream"; import { beforeEach, describe, expect, it, vi } from "vitest"; import type { CompanyPortabilityFileEntry } from "@paperclipai/shared"; @@ -25,6 +29,8 @@ const projectSvc = { list: vi.fn(), create: vi.fn(), update: vi.fn(), + createWorkspace: vi.fn(), + listWorkspaces: vi.fn(), }; const issueSvc = { @@ -34,6 +40,13 @@ const issueSvc = { create: vi.fn(), }; +const routineSvc = { + list: vi.fn(), + getDetail: vi.fn(), + create: vi.fn(), + createTrigger: vi.fn(), +}; + const companySkillSvc = { list: vi.fn(), listFull: vi.fn(), @@ -71,6 +84,10 @@ vi.mock("../services/issues.js", () => ({ issueService: () => issueSvc, })); +vi.mock("../services/routines.js", () => ({ + routineService: () => routineSvc, +})); + vi.mock("../services/company-skills.js", () => ({ companySkillService: () => companySkillSvc, })); @@ -87,7 +104,7 @@ vi.mock("../routes/org-chart-svg.js", () => ({ renderOrgChartPng: vi.fn(async () => Buffer.from("png")), })); -const { companyPortabilityService } = await import("../services/company-portability.js"); +const { companyPortabilityService, parseGitHubSourceUrl } = await import("../services/company-portability.js"); function asTextFile(entry: CompanyPortabilityFileEntry | undefined) { expect(typeof entry).toBe("string"); @@ -184,9 +201,62 @@ describe("company portability", () => { }, ]); projectSvc.list.mockResolvedValue([]); + projectSvc.createWorkspace.mockResolvedValue(null); + projectSvc.listWorkspaces.mockResolvedValue([]); issueSvc.list.mockResolvedValue([]); issueSvc.getById.mockResolvedValue(null); issueSvc.getByIdentifier.mockResolvedValue(null); + routineSvc.list.mockResolvedValue([]); + routineSvc.getDetail.mockImplementation(async (id: string) => { + const rows = await routineSvc.list(); + return rows.find((row: { id: string }) => row.id === id) ?? null; + }); + routineSvc.create.mockImplementation(async (_companyId: string, input: Record) => ({ + id: "routine-created", + companyId: "company-1", + projectId: input.projectId, + goalId: null, + parentIssueId: null, + title: input.title, + description: input.description ?? null, + assigneeAgentId: input.assigneeAgentId, + priority: input.priority ?? "medium", + status: input.status ?? "active", + concurrencyPolicy: input.concurrencyPolicy ?? "coalesce_if_active", + catchUpPolicy: input.catchUpPolicy ?? "skip_missed", + createdByAgentId: null, + createdByUserId: null, + updatedByAgentId: null, + updatedByUserId: null, + lastTriggeredAt: null, + lastEnqueuedAt: null, + createdAt: new Date(), + updatedAt: new Date(), + })); + routineSvc.createTrigger.mockImplementation(async (_routineId: string, input: Record) => ({ + id: "trigger-created", + companyId: "company-1", + routineId: "routine-created", + kind: input.kind, + label: input.label ?? null, + enabled: input.enabled ?? true, + cronExpression: input.kind === "schedule" ? input.cronExpression ?? null : null, + timezone: input.kind === "schedule" ? input.timezone ?? null : null, + nextRunAt: null, + lastFiredAt: null, + publicId: null, + secretId: null, + signingMode: input.kind === "webhook" ? input.signingMode ?? "bearer" : null, + replayWindowSec: input.kind === "webhook" ? input.replayWindowSec ?? 300 : null, + lastRotatedAt: null, + lastResult: null, + createdByAgentId: null, + createdByUserId: null, + updatedByAgentId: null, + updatedByUserId: null, + createdAt: new Date(), + updatedAt: new Date(), + })); const companySkills = [ { id: "skill-1", @@ -301,6 +371,32 @@ describe("company portability", () => { })); }); + it("parses canonical GitHub import URLs with explicit ref and package path", () => { + expect( + parseGitHubSourceUrl("https://github.com/paperclipai/companies?ref=feature%2Fdemo&path=gstack"), + ).toEqual({ + owner: "paperclipai", + repo: "companies", + ref: "feature/demo", + basePath: "gstack", + companyPath: "gstack/COMPANY.md", + }); + }); + + it("parses canonical GitHub import URLs with explicit companyPath", () => { + expect( + parseGitHubSourceUrl( + "https://github.com/paperclipai/companies?ref=abc123&companyPath=gstack%2FCOMPANY.md", + ), + ).toEqual({ + owner: "paperclipai", + repo: "companies", + ref: "abc123", + basePath: "gstack", + companyPath: "gstack/COMPANY.md", + }); + }); + it("exports referenced skills as stubs by default with sanitized Paperclip extension data", async () => { const portability = companyPortabilityService({} as any); @@ -344,6 +440,64 @@ describe("company portability", () => { expect(exported.warnings).toContain("Agent claudecoder PATH override was omitted from export because it is system-dependent."); }); + it("exports default sidebar order into the Paperclip extension and manifest", async () => { + const portability = companyPortabilityService({} as any); + + projectSvc.list.mockResolvedValue([ + { + id: "project-2", + companyId: "company-1", + name: "Zulu", + urlKey: "zulu", + description: null, + leadAgentId: null, + targetDate: null, + color: null, + status: "planned", + executionWorkspacePolicy: null, + archivedAt: null, + workspaces: [], + }, + { + id: "project-1", + companyId: "company-1", + name: "Alpha", + urlKey: "alpha", + description: null, + leadAgentId: null, + targetDate: null, + color: null, + status: "planned", + executionWorkspacePolicy: null, + archivedAt: null, + workspaces: [], + }, + ]); + + const exported = await portability.exportBundle("company-1", { + include: { + company: true, + agents: true, + projects: true, + issues: false, + }, + }); + + expect(asTextFile(exported.files[".paperclip.yaml"])).toContain([ + "sidebar:", + " agents:", + ' - "claudecoder"', + ' - "cmo"', + " projects:", + ' - "alpha"', + ' - "zulu"', + ].join("\n")); + expect(exported.manifest.sidebar).toEqual({ + agents: ["claudecoder", "cmo"], + projects: ["alpha", "zulu"], + }); + }); + it("expands referenced skills when requested", async () => { const portability = companyPortabilityService({} as any); @@ -573,6 +727,388 @@ describe("company portability", () => { expect(preview.fileInventory.some((entry) => entry.path.startsWith("tasks/"))).toBe(false); }); + it("exports portable project workspace metadata and remaps it on import", async () => { + const portability = companyPortabilityService({} as any); + + projectSvc.list.mockResolvedValue([ + { + id: "project-1", + name: "Launch", + urlKey: "launch", + description: "Ship it", + leadAgentId: "agent-1", + targetDate: "2026-03-31", + color: "#123456", + status: "planned", + executionWorkspacePolicy: { + enabled: true, + defaultMode: "shared_workspace", + defaultProjectWorkspaceId: "workspace-1", + workspaceStrategy: { + type: "project_primary", + }, + }, + workspaces: [ + { + id: "workspace-1", + companyId: "company-1", + projectId: "project-1", + name: "Main Repo", + sourceType: "git_repo", + cwd: "/Users/dotta/paperclip", + repoUrl: "https://github.com/paperclipai/paperclip.git", + repoRef: "main", + defaultRef: "main", + visibility: "default", + setupCommand: "pnpm install", + cleanupCommand: "rm -rf .paperclip-tmp", + remoteProvider: null, + remoteWorkspaceRef: null, + sharedWorkspaceKey: null, + metadata: { + language: "typescript", + }, + isPrimary: true, + createdAt: new Date("2026-03-01T00:00:00Z"), + updatedAt: new Date("2026-03-01T00:00:00Z"), + }, + { + id: "workspace-2", + companyId: "company-1", + projectId: "project-1", + name: "Local Scratch", + sourceType: "local_path", + cwd: "/tmp/paperclip-local", + repoUrl: null, + repoRef: null, + defaultRef: null, + visibility: "advanced", + setupCommand: null, + cleanupCommand: null, + remoteProvider: null, + remoteWorkspaceRef: null, + sharedWorkspaceKey: null, + metadata: null, + isPrimary: false, + createdAt: new Date("2026-03-01T00:00:00Z"), + updatedAt: new Date("2026-03-01T00:00:00Z"), + }, + ], + archivedAt: null, + }, + ]); + issueSvc.list.mockResolvedValue([ + { + id: "issue-1", + identifier: "PAP-1", + title: "Write launch task", + description: "Task body", + projectId: "project-1", + projectWorkspaceId: "workspace-1", + assigneeAgentId: "agent-1", + status: "todo", + priority: "medium", + labelIds: [], + billingCode: null, + executionWorkspaceSettings: { + mode: "shared_workspace", + }, + assigneeAdapterOverrides: null, + }, + ]); + + const exported = await portability.exportBundle("company-1", { + include: { + company: true, + agents: false, + projects: true, + issues: true, + }, + }); + + const extension = asTextFile(exported.files[".paperclip.yaml"]); + expect(extension).toContain("workspaces:"); + expect(extension).toContain("main-repo:"); + expect(extension).toContain('repoUrl: "https://github.com/paperclipai/paperclip.git"'); + expect(extension).toContain('defaultProjectWorkspaceKey: "main-repo"'); + expect(extension).toContain('projectWorkspaceKey: "main-repo"'); + expect(extension).not.toContain("/Users/dotta/paperclip"); + expect(extension).not.toContain("workspace-1"); + expect(exported.warnings).toContain("Project launch workspace Local Scratch was omitted from export because it does not have a portable repoUrl."); + + companySvc.create.mockResolvedValue({ + id: "company-imported", + name: "Imported Paperclip", + }); + accessSvc.ensureMembership.mockResolvedValue(undefined); + agentSvc.list.mockResolvedValue([]); + projectSvc.list.mockResolvedValue([]); + projectSvc.create.mockResolvedValue({ + id: "project-imported", + name: "Launch", + urlKey: "launch", + }); + projectSvc.update.mockImplementation(async (projectId: string, data: Record) => ({ + id: projectId, + name: "Launch", + urlKey: "launch", + ...data, + })); + projectSvc.createWorkspace.mockImplementation(async (projectId: string, data: Record) => ({ + id: "workspace-imported", + companyId: "company-imported", + projectId, + name: `${data.name ?? "Workspace"}`, + sourceType: `${data.sourceType ?? "git_repo"}`, + cwd: null, + repoUrl: typeof data.repoUrl === "string" ? data.repoUrl : null, + repoRef: typeof data.repoRef === "string" ? data.repoRef : null, + defaultRef: typeof data.defaultRef === "string" ? data.defaultRef : null, + visibility: `${data.visibility ?? "default"}`, + setupCommand: typeof data.setupCommand === "string" ? data.setupCommand : null, + cleanupCommand: typeof data.cleanupCommand === "string" ? data.cleanupCommand : null, + remoteProvider: null, + remoteWorkspaceRef: null, + sharedWorkspaceKey: null, + metadata: (data.metadata as Record | null | undefined) ?? null, + isPrimary: Boolean(data.isPrimary), + createdAt: new Date("2026-03-02T00:00:00Z"), + updatedAt: new Date("2026-03-02T00:00:00Z"), + })); + issueSvc.create.mockResolvedValue({ + id: "issue-imported", + title: "Write launch task", + }); + + await portability.importBundle({ + source: { + type: "inline", + rootPath: exported.rootPath, + files: exported.files, + }, + include: { + company: true, + agents: false, + projects: true, + issues: true, + }, + target: { + mode: "new_company", + newCompanyName: "Imported Paperclip", + }, + collisionStrategy: "rename", + }, "user-1"); + + expect(projectSvc.createWorkspace).toHaveBeenCalledWith("project-imported", expect.objectContaining({ + name: "Main Repo", + sourceType: "git_repo", + repoUrl: "https://github.com/paperclipai/paperclip.git", + repoRef: "main", + defaultRef: "main", + visibility: "default", + })); + expect(projectSvc.update).toHaveBeenCalledWith("project-imported", expect.objectContaining({ + executionWorkspacePolicy: expect.objectContaining({ + enabled: true, + defaultMode: "shared_workspace", + defaultProjectWorkspaceId: "workspace-imported", + }), + })); + expect(issueSvc.create).toHaveBeenCalledWith("company-imported", expect.objectContaining({ + projectId: "project-imported", + projectWorkspaceId: "workspace-imported", + title: "Write launch task", + })); + }); + + it("infers portable git metadata from a local checkout without task warning fan-out", async () => { + const portability = companyPortabilityService({} as any); + const repoDir = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-portability-git-")); + execFileSync("git", ["init"], { cwd: repoDir, stdio: "ignore" }); + execFileSync("git", ["checkout", "-b", "main"], { cwd: repoDir, stdio: "ignore" }); + execFileSync("git", ["remote", "add", "origin", "https://github.com/paperclipai/paperclip.git"], { + cwd: repoDir, + stdio: "ignore", + }); + + projectSvc.list.mockResolvedValue([ + { + id: "project-1", + name: "Paperclip App", + urlKey: "paperclip-app", + description: "Ship it", + leadAgentId: null, + targetDate: null, + color: null, + status: "planned", + executionWorkspacePolicy: { + enabled: true, + defaultMode: "shared_workspace", + defaultProjectWorkspaceId: "workspace-1", + }, + workspaces: [ + { + id: "workspace-1", + companyId: "company-1", + projectId: "project-1", + name: "paperclip", + sourceType: "local_path", + cwd: repoDir, + repoUrl: null, + repoRef: null, + defaultRef: null, + visibility: "default", + setupCommand: null, + cleanupCommand: null, + remoteProvider: null, + remoteWorkspaceRef: null, + sharedWorkspaceKey: null, + metadata: null, + isPrimary: true, + createdAt: new Date("2026-03-01T00:00:00Z"), + updatedAt: new Date("2026-03-01T00:00:00Z"), + }, + ], + archivedAt: null, + }, + ]); + issueSvc.list.mockResolvedValue([ + { + id: "issue-1", + identifier: "PAP-1", + title: "Task one", + description: "Task body", + projectId: "project-1", + projectWorkspaceId: "workspace-1", + assigneeAgentId: null, + status: "todo", + priority: "medium", + labelIds: [], + billingCode: null, + executionWorkspaceSettings: null, + assigneeAdapterOverrides: null, + }, + ]); + + const exported = await portability.exportBundle("company-1", { + include: { + company: false, + agents: false, + projects: true, + issues: true, + }, + }); + + const extension = asTextFile(exported.files[".paperclip.yaml"]); + expect(extension).toContain('repoUrl: "https://github.com/paperclipai/paperclip.git"'); + expect(extension).toContain('projectWorkspaceKey: "paperclip"'); + expect(exported.warnings).not.toContainEqual(expect.stringContaining("does not have a portable repoUrl")); + expect(exported.warnings).not.toContainEqual(expect.stringContaining("reference workspace workspace-1")); + }); + + it("collapses repeated task workspace warnings into one summary per missing workspace", async () => { + const portability = companyPortabilityService({} as any); + + projectSvc.list.mockResolvedValue([ + { + id: "project-1", + name: "Launch", + urlKey: "launch", + description: "Ship it", + leadAgentId: null, + targetDate: null, + color: null, + status: "planned", + executionWorkspacePolicy: null, + workspaces: [ + { + id: "workspace-1", + companyId: "company-1", + projectId: "project-1", + name: "Local Scratch", + sourceType: "local_path", + cwd: "/tmp/local-only", + repoUrl: null, + repoRef: null, + defaultRef: null, + visibility: "default", + setupCommand: null, + cleanupCommand: null, + remoteProvider: null, + remoteWorkspaceRef: null, + sharedWorkspaceKey: null, + metadata: null, + isPrimary: true, + createdAt: new Date("2026-03-01T00:00:00Z"), + updatedAt: new Date("2026-03-01T00:00:00Z"), + }, + ], + archivedAt: null, + }, + ]); + issueSvc.list.mockResolvedValue([ + { + id: "issue-1", + identifier: "PAP-1", + title: "Task one", + description: null, + projectId: "project-1", + projectWorkspaceId: "workspace-1", + assigneeAgentId: null, + status: "todo", + priority: "medium", + labelIds: [], + billingCode: null, + executionWorkspaceSettings: null, + assigneeAdapterOverrides: null, + }, + { + id: "issue-2", + identifier: "PAP-2", + title: "Task two", + description: null, + projectId: "project-1", + projectWorkspaceId: "workspace-1", + assigneeAgentId: null, + status: "todo", + priority: "medium", + labelIds: [], + billingCode: null, + executionWorkspaceSettings: null, + assigneeAdapterOverrides: null, + }, + { + id: "issue-3", + identifier: "PAP-3", + title: "Task three", + description: null, + projectId: "project-1", + projectWorkspaceId: "workspace-1", + assigneeAgentId: null, + status: "todo", + priority: "medium", + labelIds: [], + billingCode: null, + executionWorkspaceSettings: null, + assigneeAdapterOverrides: null, + }, + ]); + + const exported = await portability.exportBundle("company-1", { + include: { + company: false, + agents: false, + projects: true, + issues: true, + }, + }); + + expect(exported.warnings).toContain("Project launch workspace Local Scratch was omitted from export because it does not have a portable repoUrl."); + expect(exported.warnings).toContain("Tasks pap-1, pap-2, pap-3 reference workspace workspace-1, but that workspace could not be exported portably."); + expect(exported.warnings.filter((warning) => warning.includes("workspace reference workspace-1 was omitted from export"))).toHaveLength(0); + expect(exported.warnings.filter((warning) => warning.includes("could not be exported portably"))).toHaveLength(1); + }); + it("reads env inputs back from .paperclip.yaml during preview import", async () => { const portability = companyPortabilityService({} as any); @@ -628,6 +1164,360 @@ describe("company portability", () => { ]); }); + it("exports routines as recurring task packages with Paperclip routine extensions", async () => { + const portability = companyPortabilityService({} as any); + + projectSvc.list.mockResolvedValue([ + { + id: "project-1", + name: "Launch", + urlKey: "launch", + description: "Ship it", + leadAgentId: "agent-1", + targetDate: null, + color: null, + status: "planned", + executionWorkspacePolicy: null, + archivedAt: null, + }, + ]); + routineSvc.list.mockResolvedValue([ + { + id: "routine-1", + companyId: "company-1", + projectId: "project-1", + goalId: null, + parentIssueId: null, + title: "Monday Review", + description: "Review pipeline health", + assigneeAgentId: "agent-1", + priority: "high", + status: "paused", + concurrencyPolicy: "always_enqueue", + catchUpPolicy: "enqueue_missed_with_cap", + createdByAgentId: null, + createdByUserId: null, + updatedByAgentId: null, + updatedByUserId: null, + lastTriggeredAt: null, + lastEnqueuedAt: null, + createdAt: new Date(), + updatedAt: new Date(), + triggers: [ + { + id: "trigger-1", + companyId: "company-1", + routineId: "routine-1", + kind: "schedule", + label: "Weekly cadence", + enabled: true, + cronExpression: "0 9 * * 1", + timezone: "America/Chicago", + nextRunAt: null, + lastFiredAt: null, + publicId: "public-1", + secretId: "secret-1", + signingMode: null, + replayWindowSec: null, + lastRotatedAt: null, + lastResult: null, + createdByAgentId: null, + createdByUserId: null, + updatedByAgentId: null, + updatedByUserId: null, + createdAt: new Date(), + updatedAt: new Date(), + }, + { + id: "trigger-2", + companyId: "company-1", + routineId: "routine-1", + kind: "webhook", + label: "External nudge", + enabled: false, + cronExpression: null, + timezone: null, + nextRunAt: null, + lastFiredAt: null, + publicId: "public-2", + secretId: "secret-2", + signingMode: "hmac_sha256", + replayWindowSec: 120, + lastRotatedAt: null, + lastResult: null, + createdByAgentId: null, + createdByUserId: null, + updatedByAgentId: null, + updatedByUserId: null, + createdAt: new Date(), + updatedAt: new Date(), + }, + ], + lastRun: null, + activeIssue: null, + }, + ]); + + const exported = await portability.exportBundle("company-1", { + include: { + company: true, + agents: true, + projects: true, + issues: true, + skills: false, + }, + }); + + expect(asTextFile(exported.files["tasks/monday-review/TASK.md"])).toContain('recurring: true'); + const extension = asTextFile(exported.files[".paperclip.yaml"]); + expect(extension).toContain("routines:"); + expect(extension).toContain("monday-review:"); + expect(extension).toContain('cronExpression: "0 9 * * 1"'); + expect(extension).toContain('signingMode: "hmac_sha256"'); + expect(extension).not.toContain("secretId"); + expect(extension).not.toContain("publicId"); + expect(exported.manifest.issues).toEqual([ + expect.objectContaining({ + slug: "monday-review", + recurring: true, + status: "paused", + priority: "high", + routine: expect.objectContaining({ + concurrencyPolicy: "always_enqueue", + catchUpPolicy: "enqueue_missed_with_cap", + triggers: expect.arrayContaining([ + expect.objectContaining({ kind: "schedule", cronExpression: "0 9 * * 1", timezone: "America/Chicago" }), + expect.objectContaining({ kind: "webhook", enabled: false, signingMode: "hmac_sha256", replayWindowSec: 120 }), + ]), + }), + }), + ]); + }); + + it("imports recurring task packages as routines instead of one-time issues", async () => { + const portability = companyPortabilityService({} as any); + + companySvc.create.mockResolvedValue({ + id: "company-imported", + name: "Imported Paperclip", + }); + accessSvc.ensureMembership.mockResolvedValue(undefined); + agentSvc.create.mockResolvedValue({ + id: "agent-created", + name: "ClaudeCoder", + }); + projectSvc.create.mockResolvedValue({ + id: "project-created", + name: "Launch", + urlKey: "launch", + }); + agentSvc.list.mockResolvedValue([]); + projectSvc.list.mockResolvedValue([]); + + const files = { + "COMPANY.md": [ + "---", + 'schema: "agentcompanies/v1"', + 'name: "Imported Paperclip"', + "---", + "", + ].join("\n"), + "agents/claudecoder/AGENTS.md": [ + "---", + 'name: "ClaudeCoder"', + "---", + "", + "You write code.", + "", + ].join("\n"), + "projects/launch/PROJECT.md": [ + "---", + 'name: "Launch"', + "---", + "", + ].join("\n"), + "tasks/monday-review/TASK.md": [ + "---", + 'name: "Monday Review"', + 'project: "launch"', + 'assignee: "claudecoder"', + "recurring: true", + "---", + "", + "Review pipeline health.", + "", + ].join("\n"), + ".paperclip.yaml": [ + 'schema: "paperclip/v1"', + "routines:", + " monday-review:", + ' status: "paused"', + ' priority: "high"', + ' concurrencyPolicy: "always_enqueue"', + ' catchUpPolicy: "enqueue_missed_with_cap"', + " triggers:", + " - kind: schedule", + ' cronExpression: "0 9 * * 1"', + ' timezone: "America/Chicago"', + ' - kind: webhook', + ' enabled: false', + ' signingMode: "hmac_sha256"', + ' replayWindowSec: 120', + "", + ].join("\n"), + }; + + const preview = await portability.previewImport({ + source: { type: "inline", rootPath: "paperclip-demo", files }, + include: { company: true, agents: true, projects: true, issues: true, skills: false }, + target: { mode: "new_company", newCompanyName: "Imported Paperclip" }, + agents: "all", + collisionStrategy: "rename", + }); + + expect(preview.errors).toEqual([]); + expect(preview.plan.issuePlans).toEqual([ + expect.objectContaining({ + slug: "monday-review", + reason: "Recurring task will be imported as a routine.", + }), + ]); + + await portability.importBundle({ + source: { type: "inline", rootPath: "paperclip-demo", files }, + include: { company: true, agents: true, projects: true, issues: true, skills: false }, + target: { mode: "new_company", newCompanyName: "Imported Paperclip" }, + agents: "all", + collisionStrategy: "rename", + }, "user-1"); + + expect(routineSvc.create).toHaveBeenCalledWith("company-imported", expect.objectContaining({ + projectId: "project-created", + title: "Monday Review", + assigneeAgentId: "agent-created", + priority: "high", + status: "paused", + concurrencyPolicy: "always_enqueue", + catchUpPolicy: "enqueue_missed_with_cap", + }), expect.any(Object)); + expect(routineSvc.createTrigger).toHaveBeenCalledTimes(2); + expect(routineSvc.createTrigger).toHaveBeenCalledWith("routine-created", expect.objectContaining({ + kind: "schedule", + cronExpression: "0 9 * * 1", + timezone: "America/Chicago", + }), expect.any(Object)); + expect(routineSvc.createTrigger).toHaveBeenCalledWith("routine-created", expect.objectContaining({ + kind: "webhook", + enabled: false, + signingMode: "hmac_sha256", + replayWindowSec: 120, + }), expect.any(Object)); + expect(issueSvc.create).not.toHaveBeenCalled(); + }); + + it("migrates legacy schedule.recurrence imports into routine triggers", async () => { + const portability = companyPortabilityService({} as any); + + companySvc.create.mockResolvedValue({ + id: "company-imported", + name: "Imported Paperclip", + }); + accessSvc.ensureMembership.mockResolvedValue(undefined); + agentSvc.create.mockResolvedValue({ + id: "agent-created", + name: "ClaudeCoder", + }); + projectSvc.create.mockResolvedValue({ + id: "project-created", + name: "Launch", + urlKey: "launch", + }); + agentSvc.list.mockResolvedValue([]); + projectSvc.list.mockResolvedValue([]); + + const files = { + "COMPANY.md": ['---', 'schema: "agentcompanies/v1"', 'name: "Imported Paperclip"', "---", ""].join("\n"), + "agents/claudecoder/AGENTS.md": ['---', 'name: "ClaudeCoder"', "---", "", "You write code.", ""].join("\n"), + "projects/launch/PROJECT.md": ['---', 'name: "Launch"', "---", ""].join("\n"), + "tasks/monday-review/TASK.md": [ + "---", + 'name: "Monday Review"', + 'project: "launch"', + 'assignee: "claudecoder"', + "schedule:", + ' timezone: "America/Chicago"', + ' startsAt: "2026-03-16T09:00:00-05:00"', + " recurrence:", + ' frequency: "weekly"', + " interval: 1", + " weekdays:", + ' - "monday"', + "---", + "", + "Review pipeline health.", + "", + ].join("\n"), + }; + + const preview = await portability.previewImport({ + source: { type: "inline", rootPath: "paperclip-demo", files }, + include: { company: true, agents: true, projects: true, issues: true, skills: false }, + target: { mode: "new_company", newCompanyName: "Imported Paperclip" }, + agents: "all", + collisionStrategy: "rename", + }); + + expect(preview.errors).toEqual([]); + expect(preview.manifest.issues[0]).toEqual(expect.objectContaining({ + recurring: true, + legacyRecurrence: expect.objectContaining({ frequency: "weekly" }), + })); + + await portability.importBundle({ + source: { type: "inline", rootPath: "paperclip-demo", files }, + include: { company: true, agents: true, projects: true, issues: true, skills: false }, + target: { mode: "new_company", newCompanyName: "Imported Paperclip" }, + agents: "all", + collisionStrategy: "rename", + }, "user-1"); + + expect(routineSvc.createTrigger).toHaveBeenCalledWith("routine-created", expect.objectContaining({ + kind: "schedule", + cronExpression: "0 9 * * 1", + timezone: "America/Chicago", + }), expect.any(Object)); + expect(issueSvc.create).not.toHaveBeenCalled(); + }); + + it("flags recurring task imports that are missing routine-required fields", async () => { + const portability = companyPortabilityService({} as any); + + const preview = await portability.previewImport({ + source: { + type: "inline", + rootPath: "paperclip-demo", + files: { + "COMPANY.md": ['---', 'schema: "agentcompanies/v1"', 'name: "Imported Paperclip"', "---", ""].join("\n"), + "tasks/monday-review/TASK.md": [ + "---", + 'name: "Monday Review"', + "recurring: true", + "---", + "", + "Review pipeline health.", + "", + ].join("\n"), + }, + }, + include: { company: true, agents: false, projects: false, issues: true, skills: false }, + target: { mode: "new_company", newCompanyName: "Imported Paperclip" }, + collisionStrategy: "rename", + }); + + expect(preview.errors).toContain("Recurring task monday-review must declare a project to import as a routine."); + expect(preview.errors).toContain("Recurring task monday-review must declare an assignee to import as a routine."); + }); + it("imports a vendor-neutral package without .paperclip.yaml", async () => { const portability = companyPortabilityService({} as any); @@ -1000,6 +1890,61 @@ describe("company portability", () => { }); }); + it("disables timer heartbeats on imported agents", async () => { + const portability = companyPortabilityService({} as any); + + companySvc.create.mockResolvedValue({ + id: "company-imported", + name: "Imported Paperclip", + }); + agentSvc.create.mockImplementation(async (_companyId: string, input: Record) => ({ + id: `agent-${String(input.name).toLowerCase()}`, + name: input.name, + adapterConfig: input.adapterConfig, + runtimeConfig: input.runtimeConfig, + })); + + const exported = await portability.exportBundle("company-1", { + include: { + company: true, + agents: true, + projects: false, + issues: false, + }, + }); + + agentSvc.list.mockResolvedValue([]); + + await portability.importBundle({ + source: { + type: "inline", + rootPath: exported.rootPath, + files: exported.files, + }, + include: { + company: true, + agents: true, + projects: false, + issues: false, + }, + target: { + mode: "new_company", + newCompanyName: "Imported Paperclip", + }, + agents: "all", + collisionStrategy: "rename", + }, "user-1"); + + const createdClaude = agentSvc.create.mock.calls.find(([, input]) => input.name === "ClaudeCoder"); + expect(createdClaude?.[1]).toMatchObject({ + runtimeConfig: { + heartbeat: { + enabled: false, + }, + }, + }); + }); + it("imports only selected files and leaves unchecked company metadata alone", async () => { const portability = companyPortabilityService({} as any); @@ -1070,6 +2015,11 @@ describe("company portability", () => { expect(agentSvc.create).toHaveBeenCalledTimes(1); expect(agentSvc.create).toHaveBeenCalledWith("company-1", expect.objectContaining({ name: "CMO", + runtimeConfig: { + heartbeat: { + enabled: false, + }, + }, })); expect(result.company.action).toBe("unchanged"); expect(result.agents).toEqual([ @@ -1158,5 +2108,78 @@ describe("company portability", () => { replaceExisting: true, }), ); + const materializedFiles = agentInstructionsSvc.materializeManagedBundle.mock.calls[0]?.[1] as Record; + expect(materializedFiles["AGENTS.md"]).not.toMatch(/^---\n/); + expect(materializedFiles["AGENTS.md"]).not.toContain('name: "ClaudeCoder"'); + }); + + it("strips root AGENTS frontmatter when importing a nested agent entry path", async () => { + const portability = companyPortabilityService({} as any); + + companySvc.create.mockResolvedValue({ + id: "company-imported", + name: "Imported Paperclip", + }); + accessSvc.ensureMembership.mockResolvedValue(undefined); + agentSvc.create.mockResolvedValue({ + id: "agent-created", + name: "ClaudeCoder", + }); + + const exported = await portability.exportBundle("company-1", { + include: { + company: true, + agents: true, + projects: false, + issues: false, + }, + }); + const originalAgentsMarkdown = exported.files["agents/claudecoder/AGENTS.md"]; + expect(typeof originalAgentsMarkdown).toBe("string"); + + const files = { + ...exported.files, + "agents/claudecoder/nested/AGENTS.md": originalAgentsMarkdown!, + }; + + agentSvc.list.mockResolvedValue([]); + + await portability.importBundle({ + source: { + type: "inline", + rootPath: exported.rootPath, + files, + }, + include: { + company: true, + agents: true, + projects: false, + issues: false, + }, + target: { + mode: "new_company", + newCompanyName: "Imported Paperclip", + }, + agents: ["claudecoder"], + collisionStrategy: "rename", + adapterOverrides: { + claudecoder: { + adapterType: "codex_local", + adapterConfig: { + dangerouslyBypassApprovalsAndSandbox: true, + }, + }, + }, + }, "user-1"); + + const nestedMaterializedFiles = agentInstructionsSvc.materializeManagedBundle.mock.calls + .map(([, filesArg]) => filesArg as Record) + .find((filesArg) => typeof filesArg["nested/AGENTS.md"] === "string"); + + expect(nestedMaterializedFiles).toBeDefined(); + expect(nestedMaterializedFiles?.["nested/AGENTS.md"]).toContain("You are ClaudeCoder."); + expect(nestedMaterializedFiles?.["AGENTS.md"]).toContain("You are ClaudeCoder."); + expect(nestedMaterializedFiles?.["AGENTS.md"]).not.toMatch(/^---\n/); + expect(nestedMaterializedFiles?.["AGENTS.md"]).not.toContain('name: "ClaudeCoder"'); }); }); diff --git a/server/src/__tests__/company-skills.test.ts b/server/src/__tests__/company-skills.test.ts index 17da78042d..bcc173d8be 100644 --- a/server/src/__tests__/company-skills.test.ts +++ b/server/src/__tests__/company-skills.test.ts @@ -5,6 +5,7 @@ import { afterEach, describe, expect, it } from "vitest"; import { discoverProjectWorkspaceSkillDirectories, findMissingLocalSkillIds, + normalizeGitHubSkillDirectory, parseSkillImportSourceInput, readLocalSkillImportFromDirectory, } from "../services/company-skills.js"; @@ -86,6 +87,13 @@ describe("company skill import source parsing", () => { }); describe("project workspace skill discovery", () => { + it("normalizes GitHub skill directories for blob imports and legacy metadata", () => { + expect(normalizeGitHubSkillDirectory("retro/.", "retro")).toBe("retro"); + expect(normalizeGitHubSkillDirectory("retro/SKILL.md", "retro")).toBe("retro"); + expect(normalizeGitHubSkillDirectory("SKILL.md", "root-skill")).toBe(""); + expect(normalizeGitHubSkillDirectory("", "fallback-skill")).toBe("fallback-skill"); + }); + it("finds bounded skill roots under supported workspace paths", async () => { const workspace = await makeTempDir("paperclip-skill-workspace-"); await writeSkillDir(workspace, "Workspace Root"); diff --git a/server/src/__tests__/cursor-local-adapter-environment.test.ts b/server/src/__tests__/cursor-local-adapter-environment.test.ts index e68922595a..c873d34e58 100644 --- a/server/src/__tests__/cursor-local-adapter-environment.test.ts +++ b/server/src/__tests__/cursor-local-adapter-environment.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; @@ -28,6 +28,13 @@ console.log(JSON.stringify({ } describe("cursor environment diagnostics", () => { + beforeEach(() => { + vi.stubEnv("CURSOR_API_KEY", ""); + }); + afterEach(() => { + vi.unstubAllEnvs(); + }); + it("creates a missing working directory when cwd is absolute", async () => { const cwd = path.join( os.tmpdir(), @@ -116,4 +123,73 @@ describe("cursor environment diagnostics", () => { expect(args).not.toContain("--trust"); await fs.rm(root, { recursive: true, force: true }); }); + + it("emits cursor_native_auth_present when cli-config.json has authInfo and CURSOR_API_KEY is unset", async () => { + const root = path.join( + os.tmpdir(), + `paperclip-cursor-auth-${Date.now()}-${Math.random().toString(16).slice(2)}`, + ); + const cursorHome = path.join(root, ".cursor"); + const cwd = path.join(root, "workspace"); + + try { + await fs.mkdir(cursorHome, { recursive: true }); + await fs.writeFile( + path.join(cursorHome, "cli-config.json"), + JSON.stringify({ + authInfo: { + email: "test@example.com", + displayName: "Test User", + userId: 12345, + }, + }), + ); + + const result = await testEnvironment({ + companyId: "company-1", + adapterType: "cursor", + config: { + command: process.execPath, + cwd, + env: { CURSOR_HOME: cursorHome }, + }, + }); + + expect(result.checks.some((check) => check.code === "cursor_native_auth_present")).toBe(true); + expect(result.checks.some((check) => check.code === "cursor_api_key_missing")).toBe(false); + const authCheck = result.checks.find((check) => check.code === "cursor_native_auth_present"); + expect(authCheck?.detail).toContain("test@example.com"); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } + }); + + it("emits cursor_api_key_missing when neither env var nor native auth exists", async () => { + const root = path.join( + os.tmpdir(), + `paperclip-cursor-noauth-${Date.now()}-${Math.random().toString(16).slice(2)}`, + ); + const cursorHome = path.join(root, ".cursor"); + const cwd = path.join(root, "workspace"); + + try { + await fs.mkdir(cursorHome, { recursive: true }); + // No cli-config.json written + + const result = await testEnvironment({ + companyId: "company-1", + adapterType: "cursor", + config: { + command: process.execPath, + cwd, + env: { CURSOR_HOME: cursorHome }, + }, + }); + + expect(result.checks.some((check) => check.code === "cursor_api_key_missing")).toBe(true); + expect(result.checks.some((check) => check.code === "cursor_native_auth_present")).toBe(false); + } finally { + await fs.rm(root, { recursive: true, force: true }); + } + }); }); diff --git a/server/src/__tests__/dev-runner-paths.test.ts b/server/src/__tests__/dev-runner-paths.test.ts new file mode 100644 index 0000000000..6f9a5b8085 --- /dev/null +++ b/server/src/__tests__/dev-runner-paths.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vitest"; +import { shouldTrackDevServerPath } from "../../../scripts/dev-runner-paths.mjs"; + +describe("shouldTrackDevServerPath", () => { + it("ignores repo-local Paperclip state and common test file paths", () => { + expect( + shouldTrackDevServerPath( + ".paperclip/worktrees/PAP-712-for-project-configuration-get-rid-of-the-overview-tab-for-now/.agents/skills/paperclip", + ), + ).toBe(false); + expect(shouldTrackDevServerPath("server/src/__tests__/health.test.ts")).toBe(false); + expect(shouldTrackDevServerPath("packages/shared/src/lib/foo.test.ts")).toBe(false); + expect(shouldTrackDevServerPath("packages/shared/src/lib/foo.spec.tsx")).toBe(false); + expect(shouldTrackDevServerPath("packages/shared/_tests/helpers.ts")).toBe(false); + expect(shouldTrackDevServerPath("packages/shared/tests/helpers.ts")).toBe(false); + expect(shouldTrackDevServerPath("packages/shared/test/helpers.ts")).toBe(false); + expect(shouldTrackDevServerPath("vitest.config.ts")).toBe(false); + }); + + it("keeps runtime paths restart-relevant", () => { + expect(shouldTrackDevServerPath("server/src/routes/health.ts")).toBe(true); + expect(shouldTrackDevServerPath("packages/shared/src/index.ts")).toBe(true); + expect(shouldTrackDevServerPath("server/src/testing/runtime.ts")).toBe(true); + }); +}); diff --git a/server/src/__tests__/execution-workspace-policy.test.ts b/server/src/__tests__/execution-workspace-policy.test.ts index a52fba4e81..ecb5f76e7d 100644 --- a/server/src/__tests__/execution-workspace-policy.test.ts +++ b/server/src/__tests__/execution-workspace-policy.test.ts @@ -3,6 +3,7 @@ import { buildExecutionWorkspaceAdapterConfig, defaultIssueExecutionWorkspaceSettingsForProject, gateProjectExecutionWorkspacePolicy, + issueExecutionWorkspaceModeForPersistedWorkspace, parseIssueExecutionWorkspaceSettings, parseProjectExecutionWorkspacePolicy, resolveExecutionWorkspaceMode, @@ -142,6 +143,16 @@ describe("execution workspace policy helpers", () => { }); }); + it("maps persisted execution workspace modes back to issue settings", () => { + expect(issueExecutionWorkspaceModeForPersistedWorkspace("isolated_workspace")).toBe("isolated_workspace"); + expect(issueExecutionWorkspaceModeForPersistedWorkspace("operator_branch")).toBe("operator_branch"); + expect(issueExecutionWorkspaceModeForPersistedWorkspace("shared_workspace")).toBe("shared_workspace"); + expect(issueExecutionWorkspaceModeForPersistedWorkspace("adapter_managed")).toBe("agent_default"); + expect(issueExecutionWorkspaceModeForPersistedWorkspace("cloud_sandbox")).toBe("agent_default"); + expect(issueExecutionWorkspaceModeForPersistedWorkspace(null)).toBe("agent_default"); + expect(issueExecutionWorkspaceModeForPersistedWorkspace(undefined)).toBe("agent_default"); + }); + it("disables project execution workspace policy when the instance flag is off", () => { expect( gateProjectExecutionWorkspacePolicy( diff --git a/server/src/__tests__/heartbeat-process-recovery.test.ts b/server/src/__tests__/heartbeat-process-recovery.test.ts index a5742f420e..d0e3cc31a0 100644 --- a/server/src/__tests__/heartbeat-process-recovery.test.ts +++ b/server/src/__tests__/heartbeat-process-recovery.test.ts @@ -72,7 +72,7 @@ async function startTempDatabase() { password: "paperclip", port, persistent: true, - initdbFlags: ["--encoding=UTF8", "--locale=C"], + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], onLog: () => {}, onError: () => {}, }); diff --git a/server/src/__tests__/heartbeat-workspace-session.test.ts b/server/src/__tests__/heartbeat-workspace-session.test.ts index 79d781e954..7fab2b429d 100644 --- a/server/src/__tests__/heartbeat-workspace-session.test.ts +++ b/server/src/__tests__/heartbeat-workspace-session.test.ts @@ -1,7 +1,9 @@ import { describe, expect, it } from "vitest"; import type { agents } from "@paperclipai/db"; +import { sessionCodec as codexSessionCodec } from "@paperclipai/adapter-codex-local/server"; import { resolveDefaultAgentWorkspaceDir } from "../home-paths.js"; import { + buildExplicitResumeSessionOverride, formatRuntimeWorkspaceWarningLog, prioritizeProjectWorkspaceCandidatesForRun, parseSessionCompactionPolicy, @@ -182,6 +184,57 @@ describe("shouldResetTaskSessionForWake", () => { }); }); +describe("buildExplicitResumeSessionOverride", () => { + it("reuses saved task session params when they belong to the selected failed run", () => { + const result = buildExplicitResumeSessionOverride({ + resumeFromRunId: "run-1", + resumeRunSessionIdBefore: "session-before", + resumeRunSessionIdAfter: "session-after", + taskSession: { + sessionParamsJson: { + sessionId: "session-after", + cwd: "/tmp/project", + }, + sessionDisplayId: "session-after", + lastRunId: "run-1", + }, + sessionCodec: codexSessionCodec, + }); + + expect(result).toEqual({ + sessionDisplayId: "session-after", + sessionParams: { + sessionId: "session-after", + cwd: "/tmp/project", + }, + }); + }); + + it("falls back to the selected run session id when no matching task session params are available", () => { + const result = buildExplicitResumeSessionOverride({ + resumeFromRunId: "run-1", + resumeRunSessionIdBefore: "session-before", + resumeRunSessionIdAfter: "session-after", + taskSession: { + sessionParamsJson: { + sessionId: "other-session", + cwd: "/tmp/project", + }, + sessionDisplayId: "other-session", + lastRunId: "run-2", + }, + sessionCodec: codexSessionCodec, + }); + + expect(result).toEqual({ + sessionDisplayId: "session-after", + sessionParams: { + sessionId: "session-after", + }, + }); + }); +}); + describe("formatRuntimeWorkspaceWarningLog", () => { it("emits informational workspace warnings on stdout", () => { expect(formatRuntimeWorkspaceWarningLog("Using fallback workspace")).toEqual({ diff --git a/server/src/__tests__/invite-join-grants.test.ts b/server/src/__tests__/invite-join-grants.test.ts new file mode 100644 index 0000000000..7dd342677f --- /dev/null +++ b/server/src/__tests__/invite-join-grants.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, it } from "vitest"; +import { agentJoinGrantsFromDefaults } from "../routes/access.js"; + +describe("agentJoinGrantsFromDefaults", () => { + it("adds tasks:assign when invite defaults do not specify agent grants", () => { + expect(agentJoinGrantsFromDefaults(null)).toEqual([ + { + permissionKey: "tasks:assign", + scope: null, + }, + ]); + }); + + it("preserves invite agent grants and appends tasks:assign", () => { + expect( + agentJoinGrantsFromDefaults({ + agent: { + grants: [ + { + permissionKey: "agents:create", + scope: null, + }, + ], + }, + }), + ).toEqual([ + { + permissionKey: "agents:create", + scope: null, + }, + { + permissionKey: "tasks:assign", + scope: null, + }, + ]); + }); + + it("does not duplicate tasks:assign when invite defaults already include it", () => { + expect( + agentJoinGrantsFromDefaults({ + agent: { + grants: [ + { + permissionKey: "tasks:assign", + scope: { projectId: "project-1" }, + }, + ], + }, + }), + ).toEqual([ + { + permissionKey: "tasks:assign", + scope: { projectId: "project-1" }, + }, + ]); + }); +}); diff --git a/server/src/__tests__/issue-goal-fallback.test.ts b/server/src/__tests__/issue-goal-fallback.test.ts index cae1b8ab44..43ccb5f743 100644 --- a/server/src/__tests__/issue-goal-fallback.test.ts +++ b/server/src/__tests__/issue-goal-fallback.test.ts @@ -20,16 +20,29 @@ describe("issue goal fallback", () => { resolveIssueGoalId({ projectId: null, goalId: "goal-2", + projectGoalId: "goal-3", defaultGoalId: "goal-1", }), ).toBe("goal-2"); }); - it("does not force a company goal when the issue belongs to a project", () => { + it("inherits the project goal when creating a project-linked issue", () => { expect( resolveIssueGoalId({ projectId: "project-1", goalId: null, + projectGoalId: "goal-2", + defaultGoalId: "goal-1", + }), + ).toBe("goal-2"); + }); + + it("does not force a company goal when the project has no goal", () => { + expect( + resolveIssueGoalId({ + projectId: "project-1", + goalId: null, + projectGoalId: null, defaultGoalId: "goal-1", }), ).toBeNull(); @@ -40,20 +53,47 @@ describe("issue goal fallback", () => { resolveNextIssueGoalId({ currentProjectId: null, currentGoalId: null, + currentProjectGoalId: null, defaultGoalId: "goal-1", }), ).toBe("goal-1"); }); - it("clears the fallback when a project is added later", () => { + it("switches from the company fallback to the project goal when a project is added later", () => { expect( resolveNextIssueGoalId({ currentProjectId: null, currentGoalId: "goal-1", + currentProjectGoalId: null, projectId: "project-1", goalId: null, + projectGoalId: "goal-2", defaultGoalId: "goal-1", }), - ).toBeNull(); + ).toBe("goal-2"); + }); + + it("backfills the project goal for legacy project-linked issues on update", () => { + expect( + resolveNextIssueGoalId({ + currentProjectId: "project-1", + currentGoalId: null, + currentProjectGoalId: "goal-2", + defaultGoalId: "goal-1", + }), + ).toBe("goal-2"); + }); + + it("preserves an explicit goal across project fallback changes", () => { + expect( + resolveNextIssueGoalId({ + currentProjectId: "project-1", + currentGoalId: "goal-explicit", + currentProjectGoalId: "goal-2", + projectId: "project-2", + projectGoalId: "goal-3", + defaultGoalId: "goal-1", + }), + ).toBe("goal-explicit"); }); }); diff --git a/server/src/__tests__/issues-goal-context-routes.test.ts b/server/src/__tests__/issues-goal-context-routes.test.ts new file mode 100644 index 0000000000..b4611d39ca --- /dev/null +++ b/server/src/__tests__/issues-goal-context-routes.test.ts @@ -0,0 +1,187 @@ +import express from "express"; +import request from "supertest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { issueRoutes } from "../routes/issues.js"; +import { errorHandler } from "../middleware/index.js"; + +const mockIssueService = vi.hoisted(() => ({ + getById: vi.fn(), + getAncestors: vi.fn(), + findMentionedProjectIds: vi.fn(), + getCommentCursor: vi.fn(), + getComment: vi.fn(), +})); + +const mockProjectService = vi.hoisted(() => ({ + getById: vi.fn(), + listByIds: vi.fn(), +})); + +const mockGoalService = vi.hoisted(() => ({ + getById: vi.fn(), + getDefaultCompanyGoal: vi.fn(), +})); + +vi.mock("../services/index.js", () => ({ + accessService: () => ({ + canUser: vi.fn(), + hasPermission: vi.fn(), + }), + agentService: () => ({ + getById: vi.fn(), + }), + documentService: () => ({ + getIssueDocumentPayload: vi.fn(async () => ({})), + }), + executionWorkspaceService: () => ({ + getById: vi.fn(), + }), + goalService: () => mockGoalService, + heartbeatService: () => ({ + wakeup: vi.fn(async () => undefined), + reportRunActivity: vi.fn(async () => undefined), + }), + issueApprovalService: () => ({}), + issueService: () => mockIssueService, + logActivity: vi.fn(async () => undefined), + projectService: () => mockProjectService, + routineService: () => ({ + syncRunStatusForIssue: vi.fn(async () => undefined), + }), + workProductService: () => ({ + listForIssue: vi.fn(async () => []), + }), +})); + +function createApp() { + const app = express(); + app.use(express.json()); + app.use((req, _res, next) => { + (req as any).actor = { + type: "board", + userId: "local-board", + companyIds: ["company-1"], + source: "local_implicit", + isInstanceAdmin: false, + }; + next(); + }); + app.use("/api", issueRoutes({} as any, {} as any)); + app.use(errorHandler); + return app; +} + +const legacyProjectLinkedIssue = { + id: "11111111-1111-4111-8111-111111111111", + companyId: "company-1", + identifier: "PAP-581", + title: "Legacy onboarding task", + description: "Seed the first CEO task", + status: "todo", + priority: "medium", + projectId: "22222222-2222-4222-8222-222222222222", + goalId: null, + parentId: null, + assigneeAgentId: "33333333-3333-4333-8333-333333333333", + assigneeUserId: null, + updatedAt: new Date("2026-03-24T12:00:00Z"), + executionWorkspaceId: null, + labels: [], + labelIds: [], +}; + +const projectGoal = { + id: "44444444-4444-4444-8444-444444444444", + companyId: "company-1", + title: "Launch the company", + description: null, + level: "company", + status: "active", + parentId: null, + ownerAgentId: null, + createdAt: new Date("2026-03-20T00:00:00Z"), + updatedAt: new Date("2026-03-20T00:00:00Z"), +}; + +describe("issue goal context routes", () => { + beforeEach(() => { + vi.clearAllMocks(); + mockIssueService.getById.mockResolvedValue(legacyProjectLinkedIssue); + mockIssueService.getAncestors.mockResolvedValue([]); + mockIssueService.findMentionedProjectIds.mockResolvedValue([]); + mockIssueService.getCommentCursor.mockResolvedValue({ + totalComments: 0, + latestCommentId: null, + latestCommentAt: null, + }); + mockIssueService.getComment.mockResolvedValue(null); + mockProjectService.getById.mockResolvedValue({ + id: legacyProjectLinkedIssue.projectId, + companyId: "company-1", + urlKey: "onboarding", + goalId: projectGoal.id, + goalIds: [projectGoal.id], + goals: [{ id: projectGoal.id, title: projectGoal.title }], + name: "Onboarding", + description: null, + status: "in_progress", + leadAgentId: null, + targetDate: null, + color: null, + pauseReason: null, + pausedAt: null, + executionWorkspacePolicy: null, + codebase: { + workspaceId: null, + repoUrl: null, + repoRef: null, + defaultRef: null, + repoName: null, + localFolder: null, + managedFolder: "/tmp/company-1/project-1", + effectiveLocalFolder: "/tmp/company-1/project-1", + origin: "managed_checkout", + }, + workspaces: [], + primaryWorkspace: null, + archivedAt: null, + createdAt: new Date("2026-03-20T00:00:00Z"), + updatedAt: new Date("2026-03-20T00:00:00Z"), + }); + mockProjectService.listByIds.mockResolvedValue([]); + mockGoalService.getById.mockImplementation(async (id: string) => + id === projectGoal.id ? projectGoal : null, + ); + mockGoalService.getDefaultCompanyGoal.mockResolvedValue(null); + }); + + it("surfaces the project goal from GET /issues/:id when the issue has no direct goal", async () => { + const res = await request(createApp()).get("/api/issues/11111111-1111-4111-8111-111111111111"); + + expect(res.status).toBe(200); + expect(res.body.goalId).toBe(projectGoal.id); + expect(res.body.goal).toEqual( + expect.objectContaining({ + id: projectGoal.id, + title: projectGoal.title, + }), + ); + expect(mockGoalService.getDefaultCompanyGoal).not.toHaveBeenCalled(); + }); + + it("surfaces the project goal from GET /issues/:id/heartbeat-context", async () => { + const res = await request(createApp()).get( + "/api/issues/11111111-1111-4111-8111-111111111111/heartbeat-context", + ); + + expect(res.status).toBe(200); + expect(res.body.issue.goalId).toBe(projectGoal.id); + expect(res.body.goal).toEqual( + expect.objectContaining({ + id: projectGoal.id, + title: projectGoal.title, + }), + ); + expect(mockGoalService.getDefaultCompanyGoal).not.toHaveBeenCalled(); + }); +}); diff --git a/server/src/__tests__/issues-service.test.ts b/server/src/__tests__/issues-service.test.ts new file mode 100644 index 0000000000..ba27866f54 --- /dev/null +++ b/server/src/__tests__/issues-service.test.ts @@ -0,0 +1,284 @@ +import { randomUUID } from "node:crypto"; +import fs from "node:fs"; +import net from "node:net"; +import os from "node:os"; +import path from "node:path"; +import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; +import { + activityLog, + agents, + applyPendingMigrations, + companies, + createDb, + ensurePostgresDatabase, + issueComments, + issues, +} from "@paperclipai/db"; +import { issueService } from "../services/issues.ts"; + +type EmbeddedPostgresInstance = { + initialise(): Promise; + start(): Promise; + stop(): Promise; +}; + +type EmbeddedPostgresCtor = new (opts: { + databaseDir: string; + user: string; + password: string; + port: number; + persistent: boolean; + initdbFlags?: string[]; + onLog?: (message: unknown) => void; + onError?: (message: unknown) => void; +}) => EmbeddedPostgresInstance; + +async function getEmbeddedPostgresCtor(): Promise { + const mod = await import("embedded-postgres"); + return mod.default as EmbeddedPostgresCtor; +} + +async function getAvailablePort(): Promise { + return await new Promise((resolve, reject) => { + const server = net.createServer(); + server.unref(); + server.on("error", reject); + server.listen(0, "127.0.0.1", () => { + const address = server.address(); + if (!address || typeof address === "string") { + server.close(() => reject(new Error("Failed to allocate test port"))); + return; + } + const { port } = address; + server.close((error) => { + if (error) reject(error); + else resolve(port); + }); + }); + }); +} + +async function startTempDatabase() { + const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-issues-service-")); + const port = await getAvailablePort(); + const EmbeddedPostgres = await getEmbeddedPostgresCtor(); + const instance = new EmbeddedPostgres({ + databaseDir: dataDir, + user: "paperclip", + password: "paperclip", + port, + persistent: true, + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], + onLog: () => {}, + onError: () => {}, + }); + await instance.initialise(); + await instance.start(); + + const adminConnectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/postgres`; + await ensurePostgresDatabase(adminConnectionString, "paperclip"); + const connectionString = `postgres://paperclip:paperclip@127.0.0.1:${port}/paperclip`; + await applyPendingMigrations(connectionString); + return { connectionString, dataDir, instance }; +} + +describe("issueService.list participantAgentId", () => { + let db!: ReturnType; + let svc!: ReturnType; + let instance: EmbeddedPostgresInstance | null = null; + let dataDir = ""; + + beforeAll(async () => { + const started = await startTempDatabase(); + db = createDb(started.connectionString); + svc = issueService(db); + instance = started.instance; + dataDir = started.dataDir; + }, 20_000); + + afterEach(async () => { + await db.delete(issueComments); + await db.delete(activityLog); + await db.delete(issues); + await db.delete(agents); + await db.delete(companies); + }); + + afterAll(async () => { + await instance?.stop(); + if (dataDir) { + fs.rmSync(dataDir, { recursive: true, force: true }); + } + }); + + it("returns issues an agent participated in across the supported signals", async () => { + const companyId = randomUUID(); + const agentId = randomUUID(); + const otherAgentId = randomUUID(); + + await db.insert(companies).values({ + id: companyId, + name: "Paperclip", + issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`, + requireBoardApprovalForNewAgents: false, + }); + + await db.insert(agents).values([ + { + id: agentId, + companyId, + name: "CodexCoder", + role: "engineer", + status: "active", + adapterType: "codex_local", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + }, + { + id: otherAgentId, + companyId, + name: "OtherAgent", + role: "engineer", + status: "active", + adapterType: "codex_local", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + }, + ]); + + const assignedIssueId = randomUUID(); + const createdIssueId = randomUUID(); + const commentedIssueId = randomUUID(); + const activityIssueId = randomUUID(); + const excludedIssueId = randomUUID(); + + await db.insert(issues).values([ + { + id: assignedIssueId, + companyId, + title: "Assigned issue", + status: "todo", + priority: "medium", + assigneeAgentId: agentId, + createdByAgentId: otherAgentId, + }, + { + id: createdIssueId, + companyId, + title: "Created issue", + status: "todo", + priority: "medium", + createdByAgentId: agentId, + }, + { + id: commentedIssueId, + companyId, + title: "Commented issue", + status: "todo", + priority: "medium", + createdByAgentId: otherAgentId, + }, + { + id: activityIssueId, + companyId, + title: "Activity issue", + status: "todo", + priority: "medium", + createdByAgentId: otherAgentId, + }, + { + id: excludedIssueId, + companyId, + title: "Excluded issue", + status: "todo", + priority: "medium", + createdByAgentId: otherAgentId, + assigneeAgentId: otherAgentId, + }, + ]); + + await db.insert(issueComments).values({ + companyId, + issueId: commentedIssueId, + authorAgentId: agentId, + body: "Investigating this issue.", + }); + + await db.insert(activityLog).values({ + companyId, + actorType: "agent", + actorId: agentId, + action: "issue.updated", + entityType: "issue", + entityId: activityIssueId, + agentId, + details: { changed: true }, + }); + + const result = await svc.list(companyId, { participantAgentId: agentId }); + const resultIds = new Set(result.map((issue) => issue.id)); + + expect(resultIds).toEqual(new Set([ + assignedIssueId, + createdIssueId, + commentedIssueId, + activityIssueId, + ])); + expect(resultIds.has(excludedIssueId)).toBe(false); + }); + + it("combines participation filtering with search", async () => { + const companyId = randomUUID(); + const agentId = randomUUID(); + + await db.insert(companies).values({ + id: companyId, + name: "Paperclip", + issuePrefix: `T${companyId.replace(/-/g, "").slice(0, 6).toUpperCase()}`, + requireBoardApprovalForNewAgents: false, + }); + + await db.insert(agents).values({ + id: agentId, + companyId, + name: "CodexCoder", + role: "engineer", + status: "active", + adapterType: "codex_local", + adapterConfig: {}, + runtimeConfig: {}, + permissions: {}, + }); + + const matchedIssueId = randomUUID(); + const otherIssueId = randomUUID(); + + await db.insert(issues).values([ + { + id: matchedIssueId, + companyId, + title: "Invoice reconciliation", + status: "todo", + priority: "medium", + createdByAgentId: agentId, + }, + { + id: otherIssueId, + companyId, + title: "Weekly planning", + status: "todo", + priority: "medium", + createdByAgentId: agentId, + }, + ]); + + const result = await svc.list(companyId, { + participantAgentId: agentId, + q: "invoice", + }); + + expect(result.map((issue) => issue.id)).toEqual([matchedIssueId]); + }); +}); diff --git a/server/src/__tests__/normalize-agent-mention-token.test.ts b/server/src/__tests__/normalize-agent-mention-token.test.ts new file mode 100644 index 0000000000..b8a33d1d87 --- /dev/null +++ b/server/src/__tests__/normalize-agent-mention-token.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from "vitest"; +import { normalizeAgentMentionToken } from "../services/issues.ts"; + +describe("normalizeAgentMentionToken", () => { + it("decodes hex numeric entities such as space ( )", () => { + expect(normalizeAgentMentionToken("Baba ")).toBe("Baba"); + }); + + it("decodes decimal numeric entities", () => { + expect(normalizeAgentMentionToken("Baba ")).toBe("Baba"); + }); + + it("decodes common named whitespace entities", () => { + expect(normalizeAgentMentionToken("Baba ")).toBe("Baba"); + }); + + // Mid-token entity (review asked for this shape); we decode &→&, not strip to "Baba" (that broke M&M). + it("decodes a named entity in the middle of the token", () => { + expect(normalizeAgentMentionToken("Ba&ba")).toBe("Ba&ba"); + }); + + it("decodes & so agent names with ampersands still match", () => { + expect(normalizeAgentMentionToken("M&M")).toBe("M&M"); + }); + + it("decodes additional named entities used in rich text (e.g. ©)", () => { + expect(normalizeAgentMentionToken("Agent©Name")).toBe("Agent©Name"); + }); + + it("leaves unknown semicolon-terminated named references unchanged", () => { + expect(normalizeAgentMentionToken("Baba¬arealentity;")).toBe("Baba¬arealentity;"); + }); + + it("returns plain names unchanged", () => { + expect(normalizeAgentMentionToken("Baba")).toBe("Baba"); + }); + + it("trims after decoding entities", () => { + expect(normalizeAgentMentionToken("Baba ")).toBe("Baba"); + }); +}); diff --git a/server/src/__tests__/openclaw-invite-prompt-route.test.ts b/server/src/__tests__/openclaw-invite-prompt-route.test.ts index 68cb8759aa..189126f92c 100644 --- a/server/src/__tests__/openclaw-invite-prompt-route.test.ts +++ b/server/src/__tests__/openclaw-invite-prompt-route.test.ts @@ -23,11 +23,22 @@ const mockAgentService = vi.hoisted(() => ({ getById: vi.fn(), })); +const mockBoardAuthService = vi.hoisted(() => ({ + createCliAuthChallenge: vi.fn(), + describeCliAuthChallenge: vi.fn(), + approveCliAuthChallenge: vi.fn(), + cancelCliAuthChallenge: vi.fn(), + resolveBoardAccess: vi.fn(), + assertCurrentBoardKey: vi.fn(), + revokeBoardApiKey: vi.fn(), +})); + const mockLogActivity = vi.hoisted(() => vi.fn()); vi.mock("../services/index.js", () => ({ accessService: () => mockAccessService, agentService: () => mockAgentService, + boardAuthService: () => mockBoardAuthService, deduplicateAgentName: vi.fn(), logActivity: mockLogActivity, notifyHireApproved: vi.fn(), diff --git a/server/src/__tests__/routines-e2e.test.ts b/server/src/__tests__/routines-e2e.test.ts index 301f045f4f..836897243c 100644 --- a/server/src/__tests__/routines-e2e.test.ts +++ b/server/src/__tests__/routines-e2e.test.ts @@ -130,7 +130,7 @@ async function startTempDatabase() { password: "paperclip", port, persistent: true, - initdbFlags: ["--encoding=UTF8", "--locale=C"], + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], onLog: () => {}, onError: () => {}, }); diff --git a/server/src/__tests__/routines-service.test.ts b/server/src/__tests__/routines-service.test.ts index ee2e261ed3..d59542468f 100644 --- a/server/src/__tests__/routines-service.test.ts +++ b/server/src/__tests__/routines-service.test.ts @@ -76,7 +76,7 @@ async function startTempDatabase() { password: "paperclip", port, persistent: true, - initdbFlags: ["--encoding=UTF8", "--locale=C"], + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], onLog: () => {}, onError: () => {}, }); diff --git a/server/src/app.ts b/server/src/app.ts index 8032d00464..b4b20de7ef 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -81,6 +81,8 @@ export async function createApp( const app = express(); app.use(express.json({ + // Company import/export payloads can inline full portable packages. + limit: "10mb", verify: (req, _res, buf) => { (req as unknown as { rawBody: Buffer }).rawBody = buf; }, diff --git a/server/src/index.ts b/server/src/index.ts index 380e2a9f91..31c5b7508e 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -94,8 +94,8 @@ export async function startServer(): Promise { } async function promptApplyMigrations(migrations: string[]): Promise { - if (process.env.PAPERCLIP_MIGRATION_PROMPT === "never") return false; if (process.env.PAPERCLIP_MIGRATION_AUTO_APPLY === "true") return true; + if (process.env.PAPERCLIP_MIGRATION_PROMPT === "never") return false; if (!stdin.isTTY || !stdout.isTTY) return true; const prompt = createInterface({ input: stdin, output: stdout }); @@ -347,7 +347,7 @@ export async function startServer(): Promise { password: "paperclip", port, persistent: true, - initdbFlags: ["--encoding=UTF8", "--locale=C"], + initdbFlags: ["--encoding=UTF8", "--locale=C", "--lc-messages=C"], onLog: appendEmbeddedPostgresLog, onError: appendEmbeddedPostgresLog, }); diff --git a/server/src/middleware/auth.ts b/server/src/middleware/auth.ts index cbd49709ad..b523932d10 100644 --- a/server/src/middleware/auth.ts +++ b/server/src/middleware/auth.ts @@ -7,6 +7,7 @@ import { verifyLocalAgentJwt } from "../agent-auth-jwt.js"; import type { DeploymentMode } from "@paperclipai/shared"; import type { BetterAuthSessionResult } from "../auth/better-auth.js"; import { logger } from "./logger.js"; +import { boardAuthService } from "../services/board-auth.js"; function hashToken(token: string) { return createHash("sha256").update(token).digest("hex"); @@ -18,6 +19,7 @@ interface ActorMiddlewareOptions { } export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHandler { + const boardAuth = boardAuthService(db); return async (req, _res, next) => { if (opts.deploymentMode === "local_trusted") { req.actor = { type: "board", userId: "local-board", isInstanceAdmin: true, source: "local_implicit" }; @@ -111,6 +113,25 @@ export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHa return; } + const boardKey = await boardAuth.findBoardApiKeyByToken(token); + if (boardKey) { + const access = await boardAuth.resolveBoardAccess(boardKey.userId); + if (access.user) { + await boardAuth.touchBoardApiKey(boardKey.id); + req.actor = { + type: "board", + userId: boardKey.userId, + companyIds: access.companyIds, + isInstanceAdmin: access.isInstanceAdmin, + keyId: boardKey.id, + runId: runIdHeader || undefined, + source: "board_key", + }; + next(); + return; + } + } + const tokenHash = hashToken(token); const key = await db .select() diff --git a/server/src/middleware/board-mutation-guard.ts b/server/src/middleware/board-mutation-guard.ts index 7e037ddedb..fb1ca48a3a 100644 --- a/server/src/middleware/board-mutation-guard.ts +++ b/server/src/middleware/board-mutation-guard.ts @@ -49,10 +49,9 @@ export function boardMutationGuard(): RequestHandler { return; } - // Local-trusted mode uses an implicit board actor for localhost-only development. - // In this mode, origin/referer headers can be omitted by some clients for multipart - // uploads; do not block those mutations. - if (req.actor.source === "local_implicit") { + // Local-trusted mode and board bearer keys are not browser-session requests. + // In these modes, origin/referer headers can be absent; do not block those mutations. + if (req.actor.source === "local_implicit" || req.actor.source === "board_key") { next(); return; } diff --git a/server/src/routes/access.ts b/server/src/routes/access.ts index 3e29bb47e7..7d7dfe2b2b 100644 --- a/server/src/routes/access.ts +++ b/server/src/routes/access.ts @@ -19,10 +19,12 @@ import { } from "@paperclipai/db"; import { acceptInviteSchema, + createCliAuthChallengeSchema, claimJoinRequestApiKeySchema, createCompanyInviteSchema, createOpenClawInvitePromptSchema, listJoinRequestsQuerySchema, + resolveCliAuthChallengeSchema, updateMemberPermissionsSchema, updateUserCompanyAccessSchema, PERMISSION_KEYS @@ -40,6 +42,7 @@ import { validate } from "../middleware/validate.js"; import { accessService, agentService, + boardAuthService, deduplicateAgentName, logActivity, notifyHireApproved @@ -95,6 +98,10 @@ function requestBaseUrl(req: Request) { return `${proto}://${host}`; } +function buildCliAuthApprovalPath(challengeId: string, token: string) { + return `/cli-auth/${challengeId}?token=${encodeURIComponent(token)}`; +} + function readSkillMarkdown(skillName: string): string | null { const normalized = skillName.trim().toLowerCase(); if ( @@ -1404,6 +1411,25 @@ function grantsFromDefaults( return result; } +export function agentJoinGrantsFromDefaults( + defaultsPayload: Record | null | undefined +): Array<{ + permissionKey: (typeof PERMISSION_KEYS)[number]; + scope: Record | null; +}> { + const grants = grantsFromDefaults(defaultsPayload, "agent"); + if (grants.some((grant) => grant.permissionKey === "tasks:assign")) { + return grants; + } + return [ + ...grants, + { + permissionKey: "tasks:assign", + scope: null + } + ]; +} + type JoinRequestManagerCandidate = { id: string; role: string; @@ -1537,6 +1563,7 @@ export function accessRoutes( ) { const router = Router(); const access = accessService(db); + const boardAuth = boardAuthService(db); const agents = agentService(db); async function assertInstanceAdmin(req: Request) { @@ -1594,6 +1621,166 @@ export function accessRoutes( throw conflict("Board claim challenge is no longer available"); }); + router.post( + "/cli-auth/challenges", + validate(createCliAuthChallengeSchema), + async (req, res) => { + const created = await boardAuth.createCliAuthChallenge(req.body); + const approvalPath = buildCliAuthApprovalPath( + created.challenge.id, + created.challengeSecret, + ); + const baseUrl = requestBaseUrl(req); + res.status(201).json({ + id: created.challenge.id, + token: created.challengeSecret, + boardApiToken: created.pendingBoardToken, + approvalPath, + approvalUrl: baseUrl ? `${baseUrl}${approvalPath}` : null, + pollPath: `/cli-auth/challenges/${created.challenge.id}`, + expiresAt: created.challenge.expiresAt.toISOString(), + suggestedPollIntervalMs: 1000, + }); + }, + ); + + router.get("/cli-auth/challenges/:id", async (req, res) => { + const id = (req.params.id as string).trim(); + const token = + typeof req.query.token === "string" ? req.query.token.trim() : ""; + if (!id || !token) throw notFound("CLI auth challenge not found"); + const challenge = await boardAuth.describeCliAuthChallenge(id, token); + if (!challenge) throw notFound("CLI auth challenge not found"); + + const isSignedInBoardUser = + req.actor.type === "board" && + (req.actor.source === "session" || isLocalImplicit(req)) && + Boolean(req.actor.userId); + const canApprove = + isSignedInBoardUser && + (challenge.requestedAccess !== "instance_admin_required" || + isLocalImplicit(req) || + Boolean(req.actor.isInstanceAdmin)); + + res.json({ + ...challenge, + requiresSignIn: !isSignedInBoardUser, + canApprove, + currentUserId: req.actor.type === "board" ? req.actor.userId ?? null : null, + }); + }); + + router.post( + "/cli-auth/challenges/:id/approve", + validate(resolveCliAuthChallengeSchema), + async (req, res) => { + const id = (req.params.id as string).trim(); + if ( + req.actor.type !== "board" || + (!req.actor.userId && !isLocalImplicit(req)) + ) { + throw unauthorized("Sign in before approving CLI access"); + } + + const userId = req.actor.userId ?? "local-board"; + const approved = await boardAuth.approveCliAuthChallenge( + id, + req.body.token, + userId, + ); + + if (approved.status === "approved") { + const companyIds = await boardAuth.resolveBoardActivityCompanyIds({ + userId, + requestedCompanyId: approved.challenge.requestedCompanyId, + boardApiKeyId: approved.challenge.boardApiKeyId, + }); + for (const companyId of companyIds) { + await logActivity(db, { + companyId, + actorType: "user", + actorId: userId, + action: "board_api_key.created", + entityType: "user", + entityId: userId, + details: { + boardApiKeyId: approved.challenge.boardApiKeyId, + requestedAccess: approved.challenge.requestedAccess, + requestedCompanyId: approved.challenge.requestedCompanyId, + challengeId: approved.challenge.id, + }, + }); + } + } + + res.json({ + approved: approved.status === "approved", + status: approved.status, + userId, + keyId: approved.challenge.boardApiKeyId ?? null, + expiresAt: approved.challenge.expiresAt.toISOString(), + }); + }, + ); + + router.post( + "/cli-auth/challenges/:id/cancel", + validate(resolveCliAuthChallengeSchema), + async (req, res) => { + const id = (req.params.id as string).trim(); + const cancelled = await boardAuth.cancelCliAuthChallenge(id, req.body.token); + res.json({ + status: cancelled.status, + cancelled: cancelled.status === "cancelled", + }); + }, + ); + + router.get("/cli-auth/me", async (req, res) => { + if (req.actor.type !== "board" || !req.actor.userId) { + throw unauthorized("Board authentication required"); + } + const accessSnapshot = await boardAuth.resolveBoardAccess(req.actor.userId); + res.json({ + user: accessSnapshot.user, + userId: req.actor.userId, + isInstanceAdmin: accessSnapshot.isInstanceAdmin, + companyIds: accessSnapshot.companyIds, + source: req.actor.source ?? "none", + keyId: req.actor.source === "board_key" ? req.actor.keyId ?? null : null, + }); + }); + + router.post("/cli-auth/revoke-current", async (req, res) => { + if (req.actor.type !== "board" || req.actor.source !== "board_key") { + throw badRequest("Current board API key context is required"); + } + const key = await boardAuth.assertCurrentBoardKey( + req.actor.keyId, + req.actor.userId, + ); + await boardAuth.revokeBoardApiKey(key.id); + const companyIds = await boardAuth.resolveBoardActivityCompanyIds({ + userId: key.userId, + boardApiKeyId: key.id, + }); + for (const companyId of companyIds) { + await logActivity(db, { + companyId, + actorType: "user", + actorId: key.userId, + action: "board_api_key.revoked", + entityType: "user", + entityId: key.userId, + details: { + boardApiKeyId: key.id, + revokedVia: "cli_auth_logout", + }, + }); + } + res.json({ revoked: true, keyId: key.id }); + }); + async function assertCompanyPermission( req: Request, companyId: string, @@ -2450,17 +2637,8 @@ export function accessRoutes( "member", "active" ); - await access.setPrincipalPermission( - companyId, - "agent", - created.id, - "tasks:assign", - true, - req.actor.userId ?? null - ); - const grants = grantsFromDefaults( - invite.defaultsPayload as Record | null, - "agent" + const grants = agentJoinGrantsFromDefaults( + invite.defaultsPayload as Record | null ); await access.setPrincipalGrants( companyId, diff --git a/server/src/routes/agents.ts b/server/src/routes/agents.ts index af5a6574e2..f642eb10f5 100644 --- a/server/src/routes/agents.ts +++ b/server/src/routes/agents.ts @@ -43,7 +43,7 @@ import { workspaceOperationService, } from "../services/index.js"; import { conflict, forbidden, notFound, unprocessable } from "../errors.js"; -import { assertBoard, assertCompanyAccess, getActorInfo } from "./authz.js"; +import { assertBoard, assertCompanyAccess, assertInstanceAdmin, getActorInfo } from "./authz.js"; import { findServerAdapter, listAdapterModels } from "../adapters/index.js"; import { redactEventPayload } from "../redaction.js"; import { redactCurrentUserValue } from "../log-redaction.js"; @@ -73,6 +73,13 @@ export function agentRoutes(db: Db) { }; const DEFAULT_MANAGED_INSTRUCTIONS_ADAPTER_TYPES = new Set(Object.keys(DEFAULT_INSTRUCTIONS_PATH_KEYS)); const KNOWN_INSTRUCTIONS_PATH_KEYS = new Set(["instructionsFilePath", "agentsMdPath"]); + const KNOWN_INSTRUCTIONS_BUNDLE_KEYS = [ + "instructionsBundleMode", + "instructionsRootPath", + "instructionsEntryFile", + "instructionsFilePath", + "agentsMdPath", + ] as const; const router = Router(); const svc = agentService(db); @@ -303,6 +310,24 @@ export function agentRoutes(db: Db) { return trimmed.length > 0 ? trimmed : null; } + function preserveInstructionsBundleConfig( + existingAdapterConfig: Record, + nextAdapterConfig: Record, + ) { + const nextKeys = new Set(Object.keys(nextAdapterConfig)); + if (KNOWN_INSTRUCTIONS_BUNDLE_KEYS.some((key) => nextKeys.has(key))) { + return nextAdapterConfig; + } + + const merged = { ...nextAdapterConfig }; + for (const key of KNOWN_INSTRUCTIONS_BUNDLE_KEYS) { + if (merged[key] === undefined && existingAdapterConfig[key] !== undefined) { + merged[key] = existingAdapterConfig[key]; + } + } + return merged; + } + function parseBooleanLike(value: unknown): boolean | null { if (typeof value === "boolean") return value; if (typeof value === "number") { @@ -830,17 +855,7 @@ export function agentRoutes(db: Db) { }); router.get("/instance/scheduler-heartbeats", async (req, res) => { - assertBoard(req); - - const accessConditions = []; - if (req.actor.source !== "local_implicit" && !req.actor.isInstanceAdmin) { - const allowedCompanyIds = req.actor.companyIds ?? []; - if (allowedCompanyIds.length === 0) { - res.json([]); - return; - } - accessConditions.push(inArray(agentsTable.companyId, allowedCompanyIds)); - } + assertInstanceAdmin(req); const rows = await db .select({ @@ -858,7 +873,6 @@ export function agentRoutes(db: Db) { }) .from(agentsTable) .innerJoin(companies, eq(agentsTable.companyId, companies.id)) - .where(accessConditions.length > 0 ? and(...accessConditions) : undefined) .orderBy(companies.name, agentsTable.name); const items: InstanceSchedulerHeartbeatAgent[] = rows @@ -887,7 +901,6 @@ export function agentRoutes(db: Db) { }; }) .filter((item) => - item.intervalSec > 0 && item.status !== "paused" && item.status !== "terminated" && item.status !== "pending_approval", @@ -1689,6 +1702,8 @@ export function agentRoutes(db: Db) { } const patchData = { ...(req.body as Record) }; + const replaceAdapterConfig = patchData.replaceAdapterConfig === true; + delete patchData.replaceAdapterConfig; if (Object.prototype.hasOwnProperty.call(patchData, "adapterConfig")) { const adapterConfig = asRecord(patchData.adapterConfig); if (!adapterConfig) { @@ -1710,9 +1725,31 @@ export function agentRoutes(db: Db) { Object.prototype.hasOwnProperty.call(patchData, "adapterType") || Object.prototype.hasOwnProperty.call(patchData, "adapterConfig"); if (touchesAdapterConfiguration) { - const rawEffectiveAdapterConfig = Object.prototype.hasOwnProperty.call(patchData, "adapterConfig") + const existingAdapterConfig = asRecord(existing.adapterConfig) ?? {}; + const changingAdapterType = + typeof patchData.adapterType === "string" && patchData.adapterType !== existing.adapterType; + const requestedAdapterConfig = Object.prototype.hasOwnProperty.call(patchData, "adapterConfig") ? (asRecord(patchData.adapterConfig) ?? {}) - : (asRecord(existing.adapterConfig) ?? {}); + : null; + if ( + requestedAdapterConfig + && replaceAdapterConfig + && KNOWN_INSTRUCTIONS_BUNDLE_KEYS.some((key) => + existingAdapterConfig[key] !== undefined && requestedAdapterConfig[key] === undefined, + ) + ) { + await assertCanManageInstructionsPath(req, existing); + } + let rawEffectiveAdapterConfig = requestedAdapterConfig ?? existingAdapterConfig; + if (requestedAdapterConfig && !changingAdapterType && !replaceAdapterConfig) { + rawEffectiveAdapterConfig = { ...existingAdapterConfig, ...requestedAdapterConfig }; + } + if (changingAdapterType) { + rawEffectiveAdapterConfig = preserveInstructionsBundleConfig( + existingAdapterConfig, + rawEffectiveAdapterConfig, + ); + } const effectiveAdapterConfig = applyCreateDefaultsByAdapterType( requestedAdapterType, rawEffectiveAdapterConfig, diff --git a/server/src/routes/authz.ts b/server/src/routes/authz.ts index 4782489b4d..a881d4ff05 100644 --- a/server/src/routes/authz.ts +++ b/server/src/routes/authz.ts @@ -7,6 +7,14 @@ export function assertBoard(req: Request) { } } +export function assertInstanceAdmin(req: Request) { + assertBoard(req); + if (req.actor.source === "local_implicit" || req.actor.isInstanceAdmin) { + return; + } + throw forbidden("Instance admin access required"); +} + export function assertCompanyAccess(req: Request, companyId: string) { if (req.actor.type === "none") { throw unauthorized(); diff --git a/server/src/routes/issues.ts b/server/src/routes/issues.ts index 43eebe66d2..794cda334c 100644 --- a/server/src/routes/issues.ts +++ b/server/src/routes/issues.ts @@ -171,6 +171,33 @@ export function issueRoutes(db: Db, storage: StorageService) { return rawId; } + async function resolveIssueProjectAndGoal(issue: { + companyId: string; + projectId: string | null; + goalId: string | null; + }) { + const projectPromise = issue.projectId ? projectsSvc.getById(issue.projectId) : Promise.resolve(null); + const directGoalPromise = issue.goalId ? goalsSvc.getById(issue.goalId) : Promise.resolve(null); + const [project, directGoal] = await Promise.all([projectPromise, directGoalPromise]); + + if (directGoal) { + return { project, goal: directGoal }; + } + + const projectGoalId = project?.goalId ?? project?.goalIds[0] ?? null; + if (projectGoalId) { + const projectGoal = await goalsSvc.getById(projectGoalId); + return { project, goal: projectGoal }; + } + + if (!issue.projectId) { + const defaultGoal = await goalsSvc.getDefaultCompanyGoal(issue.companyId); + return { project, goal: defaultGoal }; + } + + return { project, goal: null }; + } + // Resolve issue identifiers (e.g. "PAP-39") to UUIDs for all /issues/:id routes router.param("id", async (req, res, next, rawId) => { try { @@ -233,6 +260,7 @@ export function issueRoutes(db: Db, storage: StorageService) { const result = await svc.list(companyId, { status: req.query.status as string | undefined, assigneeAgentId: req.query.assigneeAgentId as string | undefined, + participantAgentId: req.query.participantAgentId as string | undefined, assigneeUserId, touchedByUserId, unreadForUserId, @@ -310,14 +338,9 @@ export function issueRoutes(db: Db, storage: StorageService) { return; } assertCompanyAccess(req, issue.companyId); - const [ancestors, project, goal, mentionedProjectIds, documentPayload] = await Promise.all([ + const [{ project, goal }, ancestors, mentionedProjectIds, documentPayload] = await Promise.all([ + resolveIssueProjectAndGoal(issue), svc.getAncestors(issue.id), - issue.projectId ? projectsSvc.getById(issue.projectId) : null, - issue.goalId - ? goalsSvc.getById(issue.goalId) - : !issue.projectId - ? goalsSvc.getDefaultCompanyGoal(issue.companyId) - : null, svc.findMentionedProjectIds(issue.id), documentsSvc.getIssueDocumentPayload(issue), ]); @@ -355,14 +378,9 @@ export function issueRoutes(db: Db, storage: StorageService) { ? req.query.wakeCommentId.trim() : null; - const [ancestors, project, goal, commentCursor, wakeComment] = await Promise.all([ + const [{ project, goal }, ancestors, commentCursor, wakeComment] = await Promise.all([ + resolveIssueProjectAndGoal(issue), svc.getAncestors(issue.id), - issue.projectId ? projectsSvc.getById(issue.projectId) : null, - issue.goalId - ? goalsSvc.getById(issue.goalId) - : !issue.projectId - ? goalsSvc.getDefaultCompanyGoal(issue.companyId) - : null, svc.getCommentCursor(issue.id), wakeCommentId ? svc.getComment(wakeCommentId) : null, ]); diff --git a/server/src/routes/org-chart-svg.ts b/server/src/routes/org-chart-svg.ts index cf8d1951d1..af3bddaf20 100644 --- a/server/src/routes/org-chart-svg.ts +++ b/server/src/routes/org-chart-svg.ts @@ -10,6 +10,8 @@ export interface OrgNode { role: string; status: string; reports: OrgNode[]; + /** Populated by collapseTree: the flattened list of hidden descendants for avatar grid rendering. */ + collapsedReports?: OrgNode[]; } export type OrgChartStyle = "monochrome" | "nebula" | "circuit" | "warmth" | "schematic"; @@ -321,6 +323,12 @@ const CARD_PAD_X = 22; const AVATAR_SIZE = 34; const GAP_X = 24; const GAP_Y = 56; + +// ── Collapsed avatar grid constants ───────────────────────────── +const MINI_AVATAR_SIZE = 14; +const MINI_AVATAR_GAP = 6; +const MINI_AVATAR_PADDING = 10; +const MINI_AVATAR_MAX_COLS = 8; // max avatars per row in the grid const PADDING = 48; const LOGO_PADDING = 16; @@ -330,11 +338,42 @@ function measureText(text: string, fontSize: number): number { return text.length * fontSize * 0.58; } +/** Calculate how many rows the avatar grid needs. */ +function avatarGridRows(count: number): number { + return Math.ceil(count / MINI_AVATAR_MAX_COLS); +} + +/** Width needed for the avatar grid. */ +function avatarGridWidth(count: number): number { + const cols = Math.min(count, MINI_AVATAR_MAX_COLS); + return cols * (MINI_AVATAR_SIZE + MINI_AVATAR_GAP) - MINI_AVATAR_GAP + MINI_AVATAR_PADDING * 2; +} + +/** Height of the avatar grid area. */ +function avatarGridHeight(count: number): number { + if (count === 0) return 0; + const rows = avatarGridRows(count); + return rows * (MINI_AVATAR_SIZE + MINI_AVATAR_GAP) - MINI_AVATAR_GAP + MINI_AVATAR_PADDING * 2; +} + function cardWidth(node: OrgNode): number { - const { roleLabel } = getRoleInfo(node); + const { roleLabel: defaultRoleLabel } = getRoleInfo(node); + const roleLabel = node.role.startsWith("×") ? node.role : defaultRoleLabel; const nameW = measureText(node.name, 14) + CARD_PAD_X * 2; const roleW = measureText(roleLabel, 11) + CARD_PAD_X * 2; - return Math.max(CARD_MIN_W, Math.max(nameW, roleW)); + let w = Math.max(CARD_MIN_W, Math.max(nameW, roleW)); + // Widen for avatar grid if needed + if (node.collapsedReports && node.collapsedReports.length > 0) { + w = Math.max(w, avatarGridWidth(node.collapsedReports.length)); + } + return w; +} + +function cardHeight(node: OrgNode): number { + if (node.collapsedReports && node.collapsedReports.length > 0) { + return CARD_H + avatarGridHeight(node.collapsedReports.length); + } + return CARD_H; } // ── Tree layout (top-down, centered) ───────────────────────────── @@ -354,18 +393,19 @@ function layoutTree(node: OrgNode, x: number, y: number): LayoutNode { const sw = subtreeWidth(node); const cardX = x + (sw - w) / 2; + const h = cardHeight(node); const layoutNode: LayoutNode = { node, x: cardX, y, width: w, - height: CARD_H, + height: h, children: [], }; if (node.reports && node.reports.length > 0) { let childX = x; - const childY = y + CARD_H + GAP_Y; + const childY = y + h + GAP_Y; for (let i = 0; i < node.reports.length; i++) { const child = node.reports[i]; const childSW = subtreeWidth(child); @@ -394,7 +434,19 @@ function renderEmojiAvatar(cx: number, cy: number, radius: number, bgFill: strin } function defaultRenderCard(ln: LayoutNode, theme: StyleTheme): string { - const { roleLabel, bg, emojiSvg } = getRoleInfo(ln.node); + // Overflow placeholder card: just shows "+N more" text, no avatar + if (ln.node.role === "overflow") { + const cx = ln.x + ln.width / 2; + const cy = ln.y + ln.height / 2; + return ` + + ${escapeXml(ln.node.name)} + `; + } + + const { roleLabel: defaultRoleLabel, bg, emojiSvg } = getRoleInfo(ln.node); + // Use node.role directly when it's a collapse badge (e.g. "×15 reports") + const roleLabel = ln.node.role.startsWith("×") ? ln.node.role : defaultRoleLabel; const cx = ln.x + ln.width / 2; const avatarCY = ln.y + 27; @@ -417,12 +469,33 @@ function defaultRenderCard(ln: LayoutNode, theme: StyleTheme): string { const avatarBg = isLight ? bg : "rgba(255,255,255,0.06)"; const avatarStroke = isLight ? undefined : "rgba(255,255,255,0.08)"; + // Render collapsed avatar grid if this node has hidden reports + let avatarGridSvg = ""; + const collapsed = ln.node.collapsedReports; + if (collapsed && collapsed.length > 0) { + const gridTop = ln.y + CARD_H + MINI_AVATAR_PADDING; + const cols = Math.min(collapsed.length, MINI_AVATAR_MAX_COLS); + const gridTotalW = cols * (MINI_AVATAR_SIZE + MINI_AVATAR_GAP) - MINI_AVATAR_GAP; + const gridStartX = ln.x + (ln.width - gridTotalW) / 2; + + for (let i = 0; i < collapsed.length; i++) { + const col = i % MINI_AVATAR_MAX_COLS; + const row = Math.floor(i / MINI_AVATAR_MAX_COLS); + const dotCx = gridStartX + col * (MINI_AVATAR_SIZE + MINI_AVATAR_GAP) + MINI_AVATAR_SIZE / 2; + const dotCy = gridTop + row * (MINI_AVATAR_SIZE + MINI_AVATAR_GAP) + MINI_AVATAR_SIZE / 2; + const { bg: dotBg } = getRoleInfo(collapsed[i]); + const dotFill = isLight ? dotBg : "rgba(255,255,255,0.1)"; + avatarGridSvg += ``; + } + } + return ` ${shadowDef} ${renderEmojiAvatar(cx, avatarCY, AVATAR_SIZE / 2, avatarBg, emojiSvg, avatarStroke)} ${escapeXml(ln.node.name)} ${escapeXml(roleLabel)} + ${avatarGridSvg} `; } @@ -496,19 +569,154 @@ const PAPERCLIP_LOGO_SVG = ` const TARGET_W = 1280; const TARGET_H = 640; -export function renderOrgChartSvg(orgTree: OrgNode[], style: OrgChartStyle = "warmth"): string { +export interface OrgChartOverlay { + /** Company name displayed top-left */ + companyName?: string; + /** Summary stats displayed bottom-right, e.g. "Agents: 5, Skills: 8" */ + stats?: string; +} + +/** Count total nodes in a tree. */ +function countNodes(nodes: OrgNode[]): number { + let count = 0; + for (const n of nodes) { + count += 1 + countNodes(n.reports ?? []); + } + return count; +} + +/** Threshold: auto-collapse orgs larger than this. */ +const COLLAPSE_THRESHOLD = 20; +/** Max cards that can fit across the 1280px image. */ +const MAX_LEVEL_WIDTH = 8; +/** Max children shown per parent before truncation with "and N more". */ +const MAX_CHILDREN_SHOWN = 6; + +/** Flatten all descendants of a node into a single list. */ +function flattenDescendants(nodes: OrgNode[]): OrgNode[] { + const result: OrgNode[] = []; + for (const n of nodes) { + result.push(n); + result.push(...flattenDescendants(n.reports ?? [])); + } + return result; +} + +/** Collect all nodes at a given depth in the tree. */ +function nodesAtDepth(nodes: OrgNode[], depth: number): OrgNode[] { + if (depth === 0) return nodes; + const result: OrgNode[] = []; + for (const n of nodes) { + result.push(...nodesAtDepth(n.reports ?? [], depth - 1)); + } + return result; +} + +/** + * Estimate how many cards would be shown at the next level if we expand, + * considering truncation (each parent shows at most MAX_CHILDREN_SHOWN + 1 placeholder). + */ +function estimateNextLevelWidth(parentNodes: OrgNode[]): number { + let total = 0; + for (const p of parentNodes) { + const childCount = (p.reports ?? []).length; + if (childCount === 0) continue; + total += Math.min(childCount, MAX_CHILDREN_SHOWN + 1); // +1 for "and N more" placeholder + } + return total; +} + +/** + * Collapse a node's children to avatar dots (for wide levels that can't expand). + */ +function collapseToAvatars(node: OrgNode): OrgNode { + const childCount = countNodes(node.reports ?? []); + if (childCount === 0) return node; + return { + ...node, + role: `×${childCount} reports`, + collapsedReports: flattenDescendants(node.reports ?? []), + reports: [], + }; +} + +/** + * Truncate a node's children: keep first MAX_CHILDREN_SHOWN, replace rest with + * a summary "and N more" placeholder node (rendered as a count card). + */ +function truncateChildren(node: OrgNode): OrgNode { + const children = node.reports ?? []; + if (children.length <= MAX_CHILDREN_SHOWN) return node; + const kept = children.slice(0, MAX_CHILDREN_SHOWN); + const hiddenCount = children.length - MAX_CHILDREN_SHOWN; + const placeholder: OrgNode = { + id: `${node.id}-more`, + name: `+${hiddenCount} more`, + role: "overflow", + status: "active", + reports: [], + }; + return { ...node, reports: [...kept, placeholder] }; +} + +/** + * Adaptive collapse: expands levels as long as they fit, truncates or collapses + * when a level is too wide. + */ +function smartCollapseTree(roots: OrgNode[]): OrgNode[] { + // Deep clone so we can mutate + const clone = (nodes: OrgNode[]): OrgNode[] => + nodes.map((n) => ({ ...n, reports: clone(n.reports ?? []) })); + const tree = clone(roots); + + // Walk levels from root down + for (let depth = 0; depth < 10; depth++) { + const parents = nodesAtDepth(tree, depth); + const parentsWithChildren = parents.filter((p) => (p.reports ?? []).length > 0); + if (parentsWithChildren.length === 0) break; + + const nextWidth = estimateNextLevelWidth(parentsWithChildren); + if (nextWidth <= MAX_LEVEL_WIDTH) { + // Next level fits with truncation — truncate oversized parents, then continue deeper + for (const p of parentsWithChildren) { + if ((p.reports ?? []).length > MAX_CHILDREN_SHOWN) { + const truncated = truncateChildren(p); + p.reports = truncated.reports; + } + } + continue; + } + + // Next level is too wide — collapse all children at this level to avatars + for (const p of parentsWithChildren) { + const collapsed = collapseToAvatars(p); + p.role = collapsed.role; + p.collapsedReports = collapsed.collapsedReports; + p.reports = []; + } + break; + } + + return tree; +} + +export function renderOrgChartSvg(orgTree: OrgNode[], style: OrgChartStyle = "warmth", overlay?: OrgChartOverlay): string { const theme = THEMES[style] || THEMES.warmth; + // Auto-collapse large orgs to keep the chart readable + const totalNodes = countNodes(orgTree); + const effectiveTree = totalNodes > COLLAPSE_THRESHOLD ? smartCollapseTree(orgTree) : orgTree; + let root: OrgNode; - if (orgTree.length === 1) { - root = orgTree[0]; + if (effectiveTree.length === 1) { + root = effectiveTree[0]; } else { root = { id: "virtual-root", name: "Organization", role: "Root", status: "active", - reports: orgTree, + reports: effectiveTree, }; } @@ -529,6 +737,14 @@ export function renderOrgChartSvg(orgTree: OrgNode[], style: OrgChartStyle = "wa const logoX = TARGET_W - 110 - LOGO_PADDING; const logoY = LOGO_PADDING; + // Optional overlay elements + const overlayNameSvg = overlay?.companyName + ? `${svgEscape(overlay.companyName)}` + : ""; + const overlayStatsSvg = overlay?.stats + ? `${svgEscape(overlay.stats)}` + : ""; + return ` ${theme.defs(TARGET_W, TARGET_H)} @@ -536,6 +752,8 @@ export function renderOrgChartSvg(orgTree: OrgNode[], style: OrgChartStyle = "wa ${PAPERCLIP_LOGO_SVG} + ${overlayNameSvg} + ${overlayStatsSvg} ${renderConnectors(layout, theme)} ${renderCards(layout, theme)} @@ -543,8 +761,12 @@ export function renderOrgChartSvg(orgTree: OrgNode[], style: OrgChartStyle = "wa `; } -export async function renderOrgChartPng(orgTree: OrgNode[], style: OrgChartStyle = "warmth"): Promise { - const svg = renderOrgChartSvg(orgTree, style); +function svgEscape(s: string): string { + return s.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); +} + +export async function renderOrgChartPng(orgTree: OrgNode[], style: OrgChartStyle = "warmth", overlay?: OrgChartOverlay): Promise { + const svg = renderOrgChartSvg(orgTree, style, overlay); const sharpModule = await import("sharp"); const sharp = sharpModule.default; // Render at 2x density for retina quality, resize to exact target dimensions diff --git a/server/src/services/agent-instructions.ts b/server/src/services/agent-instructions.ts index d3fc70084d..231ed839e4 100644 --- a/server/src/services/agent-instructions.ts +++ b/server/src/services/agent-instructions.ts @@ -272,6 +272,62 @@ function deriveBundleState(agent: AgentLike): BundleState { }; } +async function recoverManagedBundleState(agent: AgentLike, state: BundleState): Promise { + const managedRootPath = resolveManagedInstructionsRoot(agent); + const stat = await statIfExists(managedRootPath); + if (!stat?.isDirectory()) return state; + + const files = await listFilesRecursive(managedRootPath); + if (files.length === 0) return state; + + const recoveredEntryFile = files.includes(state.entryFile) + ? state.entryFile + : files.includes(ENTRY_FILE_DEFAULT) + ? ENTRY_FILE_DEFAULT + : files[0]!; + + if (!state.rootPath) { + return { + ...state, + mode: "managed", + rootPath: managedRootPath, + entryFile: recoveredEntryFile, + resolvedEntryPath: path.resolve(managedRootPath, recoveredEntryFile), + }; + } + + if (state.mode === "external") return state; + + const resolvedConfiguredRoot = path.resolve(state.rootPath); + const configuredRootMatchesManaged = resolvedConfiguredRoot === managedRootPath; + const hasEntryMismatch = recoveredEntryFile !== state.entryFile; + + if (configuredRootMatchesManaged && !hasEntryMismatch) { + return state; + } + + const warnings = [...state.warnings]; + if (!configuredRootMatchesManaged) { + warnings.push( + `Recovered managed instructions from disk at ${managedRootPath}; ignoring stale configured root ${state.rootPath}.`, + ); + } + if (hasEntryMismatch) { + warnings.push( + `Recovered managed instructions entry file from disk as ${recoveredEntryFile}; previous entry ${state.entryFile} was missing.`, + ); + } + + return { + ...state, + mode: "managed", + rootPath: managedRootPath, + entryFile: recoveredEntryFile, + resolvedEntryPath: path.resolve(managedRootPath, recoveredEntryFile), + warnings, + }; +} + function toBundle(agent: AgentLike, state: BundleState, files: AgentInstructionsFileSummary[]): AgentInstructionsBundle { const nextFiles = [...files]; if (state.legacyPromptTemplateActive && !nextFiles.some((file) => file.path === LEGACY_PROMPT_TEMPLATE_PATH)) { @@ -327,6 +383,36 @@ function applyBundleConfig( return next; } +function buildPersistedBundleConfig( + derived: BundleState, + current: BundleState, + options?: { clearLegacyPromptTemplate?: boolean }, +): Record { + const currentRootPath = current.rootPath ? path.resolve(current.rootPath) : null; + const derivedRootPath = derived.rootPath ? path.resolve(derived.rootPath) : null; + const configMatchesRecoveredState = + derived.mode === current.mode + && derivedRootPath !== null + && currentRootPath !== null + && derivedRootPath === currentRootPath + && derived.entryFile === current.entryFile; + + if (configMatchesRecoveredState && !options?.clearLegacyPromptTemplate) { + return current.config; + } + + if (!current.rootPath || !current.mode) { + return current.config; + } + + return applyBundleConfig(current.config, { + mode: current.mode, + rootPath: current.rootPath, + entryFile: current.entryFile, + clearLegacyPromptTemplate: options?.clearLegacyPromptTemplate, + }); +} + async function writeBundleFiles( rootPath: string, files: Record, @@ -366,7 +452,7 @@ export function syncInstructionsBundleConfigFromFilePath( export function agentInstructionsService() { async function getBundle(agent: AgentLike): Promise { - const state = deriveBundleState(agent); + const state = await recoverManagedBundleState(agent, deriveBundleState(agent)); if (!state.rootPath) return toBundle(agent, state, []); const stat = await statIfExists(state.rootPath); if (!stat?.isDirectory()) { @@ -381,7 +467,7 @@ export function agentInstructionsService() { } async function readFile(agent: AgentLike, relativePath: string): Promise { - const state = deriveBundleState(agent); + const state = await recoverManagedBundleState(agent, deriveBundleState(agent)); if (relativePath === LEGACY_PROMPT_TEMPLATE_PATH) { const content = asString(state.config[PROMPT_KEY]); if (content === null) throw notFound("Instructions file not found"); @@ -422,9 +508,14 @@ export function agentInstructionsService() { agent: AgentLike, options?: { clearLegacyPromptTemplate?: boolean }, ): Promise<{ adapterConfig: Record; state: BundleState }> { - const current = deriveBundleState(agent); + const derived = deriveBundleState(agent); + const current = await recoverManagedBundleState(agent, derived); if (current.rootPath && current.mode) { - return { adapterConfig: current.config, state: current }; + const adapterConfig = buildPersistedBundleConfig(derived, current, options); + return { + adapterConfig, + state: deriveBundleState({ ...agent, adapterConfig }), + }; } const managedRoot = resolveManagedInstructionsRoot(agent); @@ -462,7 +553,7 @@ export function agentInstructionsService() { clearLegacyPromptTemplate?: boolean; }, ): Promise<{ bundle: AgentInstructionsBundle; adapterConfig: Record }> { - const state = deriveBundleState(agent); + const state = await recoverManagedBundleState(agent, deriveBundleState(agent)); const nextMode = input.mode ?? state.mode ?? "managed"; const nextEntryFile = input.entryFile ? normalizeRelativeFilePath(input.entryFile) : state.entryFile; let nextRootPath: string; @@ -544,7 +635,8 @@ export function agentInstructionsService() { bundle: AgentInstructionsBundle; adapterConfig: Record; }> { - const state = deriveBundleState(agent); + const derived = deriveBundleState(agent); + const state = await recoverManagedBundleState(agent, derived); if (relativePath === LEGACY_PROMPT_TEMPLATE_PATH) { throw unprocessable("Cannot delete the legacy promptTemplate pseudo-file"); } @@ -555,8 +647,9 @@ export function agentInstructionsService() { } const absolutePath = resolvePathWithinRoot(state.rootPath, normalizedPath); await fs.rm(absolutePath, { force: true }); - const bundle = await getBundle(agent); - return { bundle, adapterConfig: state.config }; + const adapterConfig = buildPersistedBundleConfig(derived, state); + const bundle = await getBundle({ ...agent, adapterConfig }); + return { bundle, adapterConfig }; } async function exportFiles(agent: AgentLike): Promise<{ @@ -564,7 +657,7 @@ export function agentInstructionsService() { entryFile: string; warnings: string[]; }> { - const state = deriveBundleState(agent); + const state = await recoverManagedBundleState(agent, deriveBundleState(agent)); if (state.rootPath) { const stat = await statIfExists(state.rootPath); if (stat?.isDirectory()) { diff --git a/server/src/services/board-auth.ts b/server/src/services/board-auth.ts new file mode 100644 index 0000000000..19e533c116 --- /dev/null +++ b/server/src/services/board-auth.ts @@ -0,0 +1,354 @@ +import { createHash, randomBytes, timingSafeEqual } from "node:crypto"; +import { and, eq, isNull, sql } from "drizzle-orm"; +import type { Db } from "@paperclipai/db"; +import { + authUsers, + boardApiKeys, + cliAuthChallenges, + companies, + companyMemberships, + instanceUserRoles, +} from "@paperclipai/db"; +import { conflict, forbidden, notFound } from "../errors.js"; + +export const BOARD_API_KEY_TTL_MS = 30 * 24 * 60 * 60 * 1000; +export const CLI_AUTH_CHALLENGE_TTL_MS = 10 * 60 * 1000; + +export type CliAuthChallengeStatus = "pending" | "approved" | "cancelled" | "expired"; + +export function hashBearerToken(token: string) { + return createHash("sha256").update(token).digest("hex"); +} + +export function tokenHashesMatch(left: string, right: string) { + const leftBytes = Buffer.from(left, "utf8"); + const rightBytes = Buffer.from(right, "utf8"); + return leftBytes.length === rightBytes.length && timingSafeEqual(leftBytes, rightBytes); +} + +export function createBoardApiToken() { + return `pcp_board_${randomBytes(24).toString("hex")}`; +} + +export function createCliAuthSecret() { + return `pcp_cli_auth_${randomBytes(24).toString("hex")}`; +} + +export function boardApiKeyExpiresAt(nowMs: number = Date.now()) { + return new Date(nowMs + BOARD_API_KEY_TTL_MS); +} + +export function cliAuthChallengeExpiresAt(nowMs: number = Date.now()) { + return new Date(nowMs + CLI_AUTH_CHALLENGE_TTL_MS); +} + +function challengeStatusForRow(row: typeof cliAuthChallenges.$inferSelect): CliAuthChallengeStatus { + if (row.cancelledAt) return "cancelled"; + if (row.expiresAt.getTime() <= Date.now()) return "expired"; + if (row.approvedAt && row.boardApiKeyId) return "approved"; + return "pending"; +} + +export function boardAuthService(db: Db) { + async function resolveBoardAccess(userId: string) { + const [user, memberships, adminRole] = await Promise.all([ + db + .select({ + id: authUsers.id, + name: authUsers.name, + email: authUsers.email, + }) + .from(authUsers) + .where(eq(authUsers.id, userId)) + .then((rows) => rows[0] ?? null), + db + .select({ companyId: companyMemberships.companyId }) + .from(companyMemberships) + .where( + and( + eq(companyMemberships.principalType, "user"), + eq(companyMemberships.principalId, userId), + eq(companyMemberships.status, "active"), + ), + ) + .then((rows) => rows.map((row) => row.companyId)), + db + .select({ id: instanceUserRoles.id }) + .from(instanceUserRoles) + .where(and(eq(instanceUserRoles.userId, userId), eq(instanceUserRoles.role, "instance_admin"))) + .then((rows) => rows[0] ?? null), + ]); + + return { + user, + companyIds: memberships, + isInstanceAdmin: Boolean(adminRole), + }; + } + + async function resolveBoardActivityCompanyIds(input: { + userId: string; + requestedCompanyId?: string | null; + boardApiKeyId?: string | null; + }) { + const access = await resolveBoardAccess(input.userId); + const companyIds = new Set(access.companyIds); + + if (companyIds.size === 0 && input.requestedCompanyId?.trim()) { + companyIds.add(input.requestedCompanyId.trim()); + } + + if (companyIds.size === 0 && input.boardApiKeyId?.trim()) { + const challengeCompanyIds = await db + .select({ requestedCompanyId: cliAuthChallenges.requestedCompanyId }) + .from(cliAuthChallenges) + .where(eq(cliAuthChallenges.boardApiKeyId, input.boardApiKeyId.trim())) + .then((rows) => + rows + .map((row) => row.requestedCompanyId?.trim() ?? null) + .filter((value): value is string => Boolean(value)), + ); + for (const companyId of challengeCompanyIds) { + companyIds.add(companyId); + } + } + + if (companyIds.size === 0 && access.isInstanceAdmin) { + const allCompanyIds = await db + .select({ id: companies.id }) + .from(companies) + .then((rows) => rows.map((row) => row.id)); + for (const companyId of allCompanyIds) { + companyIds.add(companyId); + } + } + + return Array.from(companyIds); + } + + async function findBoardApiKeyByToken(token: string) { + const tokenHash = hashBearerToken(token); + const now = new Date(); + return db + .select() + .from(boardApiKeys) + .where( + and( + eq(boardApiKeys.keyHash, tokenHash), + isNull(boardApiKeys.revokedAt), + ), + ) + .then((rows) => rows.find((row) => !row.expiresAt || row.expiresAt.getTime() > now.getTime()) ?? null); + } + + async function touchBoardApiKey(id: string) { + await db.update(boardApiKeys).set({ lastUsedAt: new Date() }).where(eq(boardApiKeys.id, id)); + } + + async function revokeBoardApiKey(id: string) { + const now = new Date(); + return db + .update(boardApiKeys) + .set({ revokedAt: now, lastUsedAt: now }) + .where(and(eq(boardApiKeys.id, id), isNull(boardApiKeys.revokedAt))) + .returning() + .then((rows) => rows[0] ?? null); + } + + async function createCliAuthChallenge(input: { + command: string; + clientName?: string | null; + requestedAccess: "board" | "instance_admin_required"; + requestedCompanyId?: string | null; + }) { + const challengeSecret = createCliAuthSecret(); + const pendingBoardToken = createBoardApiToken(); + const expiresAt = cliAuthChallengeExpiresAt(); + const labelBase = input.clientName?.trim() || "paperclipai cli"; + const pendingKeyName = + input.requestedAccess === "instance_admin_required" + ? `${labelBase} (instance admin)` + : `${labelBase} (board)`; + + const created = await db + .insert(cliAuthChallenges) + .values({ + secretHash: hashBearerToken(challengeSecret), + command: input.command.trim(), + clientName: input.clientName?.trim() || null, + requestedAccess: input.requestedAccess, + requestedCompanyId: input.requestedCompanyId?.trim() || null, + pendingKeyHash: hashBearerToken(pendingBoardToken), + pendingKeyName, + expiresAt, + }) + .returning() + .then((rows) => rows[0]); + + return { + challenge: created, + challengeSecret, + pendingBoardToken, + }; + } + + async function getCliAuthChallenge(id: string) { + return db + .select() + .from(cliAuthChallenges) + .where(eq(cliAuthChallenges.id, id)) + .then((rows) => rows[0] ?? null); + } + + async function getCliAuthChallengeBySecret(id: string, token: string) { + const challenge = await getCliAuthChallenge(id); + if (!challenge) return null; + if (!tokenHashesMatch(challenge.secretHash, hashBearerToken(token))) return null; + return challenge; + } + + async function describeCliAuthChallenge(id: string, token: string) { + const challenge = await getCliAuthChallengeBySecret(id, token); + if (!challenge) return null; + + const [company, approvedBy] = await Promise.all([ + challenge.requestedCompanyId + ? db + .select({ id: companies.id, name: companies.name }) + .from(companies) + .where(eq(companies.id, challenge.requestedCompanyId)) + .then((rows) => rows[0] ?? null) + : Promise.resolve(null), + challenge.approvedByUserId + ? db + .select({ id: authUsers.id, name: authUsers.name, email: authUsers.email }) + .from(authUsers) + .where(eq(authUsers.id, challenge.approvedByUserId)) + .then((rows) => rows[0] ?? null) + : Promise.resolve(null), + ]); + + return { + id: challenge.id, + status: challengeStatusForRow(challenge), + command: challenge.command, + clientName: challenge.clientName ?? null, + requestedAccess: challenge.requestedAccess as "board" | "instance_admin_required", + requestedCompanyId: challenge.requestedCompanyId ?? null, + requestedCompanyName: company?.name ?? null, + approvedAt: challenge.approvedAt?.toISOString() ?? null, + cancelledAt: challenge.cancelledAt?.toISOString() ?? null, + expiresAt: challenge.expiresAt.toISOString(), + approvedByUser: approvedBy + ? { + id: approvedBy.id, + name: approvedBy.name, + email: approvedBy.email, + } + : null, + }; + } + + async function approveCliAuthChallenge(id: string, token: string, userId: string) { + const access = await resolveBoardAccess(userId); + return db.transaction(async (tx) => { + await tx.execute( + sql`select ${cliAuthChallenges.id} from ${cliAuthChallenges} where ${cliAuthChallenges.id} = ${id} for update`, + ); + + const challenge = await tx + .select() + .from(cliAuthChallenges) + .where(eq(cliAuthChallenges.id, id)) + .then((rows) => rows[0] ?? null); + if (!challenge || !tokenHashesMatch(challenge.secretHash, hashBearerToken(token))) { + throw notFound("CLI auth challenge not found"); + } + + const status = challengeStatusForRow(challenge); + if (status === "expired") return { status, challenge }; + if (status === "cancelled") return { status, challenge }; + + if (challenge.requestedAccess === "instance_admin_required" && !access.isInstanceAdmin) { + throw forbidden("Instance admin required"); + } + + let boardKeyId = challenge.boardApiKeyId; + if (!boardKeyId) { + const createdKey = await tx + .insert(boardApiKeys) + .values({ + userId, + name: challenge.pendingKeyName, + keyHash: challenge.pendingKeyHash, + expiresAt: boardApiKeyExpiresAt(), + }) + .returning() + .then((rows) => rows[0]); + boardKeyId = createdKey.id; + } + + const approvedAt = challenge.approvedAt ?? new Date(); + const updated = await tx + .update(cliAuthChallenges) + .set({ + approvedByUserId: userId, + boardApiKeyId: boardKeyId, + approvedAt, + updatedAt: new Date(), + }) + .where(eq(cliAuthChallenges.id, challenge.id)) + .returning() + .then((rows) => rows[0] ?? challenge); + + return { status: "approved" as const, challenge: updated }; + }); + } + + async function cancelCliAuthChallenge(id: string, token: string) { + const challenge = await getCliAuthChallengeBySecret(id, token); + if (!challenge) throw notFound("CLI auth challenge not found"); + + const status = challengeStatusForRow(challenge); + if (status === "approved") return { status, challenge }; + if (status === "expired") return { status, challenge }; + if (status === "cancelled") return { status, challenge }; + + const updated = await db + .update(cliAuthChallenges) + .set({ + cancelledAt: new Date(), + updatedAt: new Date(), + }) + .where(eq(cliAuthChallenges.id, challenge.id)) + .returning() + .then((rows) => rows[0] ?? challenge); + + return { status: "cancelled" as const, challenge: updated }; + } + + async function assertCurrentBoardKey(keyId: string | undefined, userId: string | undefined) { + if (!keyId || !userId) throw conflict("Board API key context is required"); + const key = await db + .select() + .from(boardApiKeys) + .where(and(eq(boardApiKeys.id, keyId), eq(boardApiKeys.userId, userId))) + .then((rows) => rows[0] ?? null); + if (!key || key.revokedAt) throw notFound("Board API key not found"); + return key; + } + + return { + resolveBoardAccess, + findBoardApiKeyByToken, + touchBoardApiKey, + revokeBoardApiKey, + createCliAuthChallenge, + getCliAuthChallengeBySecret, + describeCliAuthChallenge, + approveCliAuthChallenge, + cancelCliAuthChallenge, + assertCurrentBoardKey, + resolveBoardActivityCompanyIds, + }; +} diff --git a/server/src/services/company-portability.ts b/server/src/services/company-portability.ts index d063fd36db..7cfe8ffabc 100644 --- a/server/src/services/company-portability.ts +++ b/server/src/services/company-portability.ts @@ -20,7 +20,11 @@ import type { CompanyPortabilityPreviewAgentPlan, CompanyPortabilityPreviewResult, CompanyPortabilityProjectManifestEntry, + CompanyPortabilityProjectWorkspaceManifestEntry, + CompanyPortabilityIssueRoutineManifestEntry, + CompanyPortabilityIssueRoutineTriggerManifestEntry, CompanyPortabilityIssueManifestEntry, + CompanyPortabilitySidebarOrder, CompanyPortabilitySkillManifestEntry, CompanySkill, } from "@paperclipai/shared"; @@ -28,6 +32,11 @@ import { ISSUE_PRIORITIES, ISSUE_STATUSES, PROJECT_STATUSES, + ROUTINE_CATCH_UP_POLICIES, + ROUTINE_CONCURRENCY_POLICIES, + ROUTINE_STATUSES, + ROUTINE_TRIGGER_KINDS, + ROUTINE_TRIGGER_SIGNING_MODES, deriveProjectUrlKey, normalizeAgentUrlKey, } from "@paperclipai/shared"; @@ -45,8 +54,10 @@ import { generateReadme } from "./company-export-readme.js"; import { renderOrgChartPng, type OrgNode } from "../routes/org-chart-svg.js"; import { companySkillService } from "./company-skills.js"; import { companyService } from "./companies.js"; +import { validateCron } from "./cron.js"; import { issueService } from "./issues.js"; import { projectService } from "./projects.js"; +import { routineService } from "./routines.js"; /** Build OrgNode tree from manifest agent list (slug + reportsToSlug). */ function buildOrgTreeFromManifest(agents: CompanyPortabilityManifest["agents"]): OrgNode[] { @@ -395,6 +406,7 @@ type PaperclipExtensionDoc = { agents?: Record> | null; projects?: Record> | null; tasks?: Record> | null; + routines?: Record> | null; }; type ProjectLike = { @@ -406,6 +418,20 @@ type ProjectLike = { color: string | null; status: string; executionWorkspacePolicy: Record | null; + workspaces?: Array<{ + id: string; + name: string; + sourceType: string; + cwd: string | null; + repoUrl: string | null; + repoRef: string | null; + defaultRef: string | null; + visibility: string; + setupCommand: string | null; + cleanupCommand: string | null; + metadata?: Record | null; + isPrimary: boolean; + }>; metadata?: Record | null; }; @@ -415,6 +441,7 @@ type IssueLike = { title: string; description: string | null; projectId: string | null; + projectWorkspaceId: string | null; assigneeAgentId: string | null; status: string; priority: string; @@ -424,6 +451,8 @@ type IssueLike = { assigneeAdapterOverrides: Record | null; }; +type RoutineLike = NonNullable["getDetail"]>>>; + type ImportPlanInternal = { preview: CompanyPortabilityPreviewResult; source: ResolvedSource; @@ -515,6 +544,595 @@ function asString(value: unknown): string | null { return trimmed.length > 0 ? trimmed : null; } +function asBoolean(value: unknown): boolean | null { + return typeof value === "boolean" ? value : null; +} + +function asInteger(value: unknown): number | null { + return typeof value === "number" && Number.isInteger(value) ? value : null; +} + +function normalizeRoutineTriggerExtension(value: unknown): CompanyPortabilityIssueRoutineTriggerManifestEntry | null { + if (!isPlainRecord(value)) return null; + const kind = asString(value.kind); + if (!kind) return null; + return { + kind, + label: asString(value.label), + enabled: asBoolean(value.enabled) ?? true, + cronExpression: asString(value.cronExpression), + timezone: asString(value.timezone), + signingMode: asString(value.signingMode), + replayWindowSec: asInteger(value.replayWindowSec), + }; +} + +function normalizeRoutineExtension(value: unknown): CompanyPortabilityIssueRoutineManifestEntry | null { + if (!isPlainRecord(value)) return null; + const triggers = Array.isArray(value.triggers) + ? value.triggers + .map((entry) => normalizeRoutineTriggerExtension(entry)) + .filter((entry): entry is CompanyPortabilityIssueRoutineTriggerManifestEntry => entry !== null) + : []; + const routine = { + concurrencyPolicy: asString(value.concurrencyPolicy), + catchUpPolicy: asString(value.catchUpPolicy), + triggers, + }; + return stripEmptyValues(routine) ? routine : null; +} + +function buildRoutineManifestFromLiveRoutine(routine: RoutineLike): CompanyPortabilityIssueRoutineManifestEntry { + return { + concurrencyPolicy: routine.concurrencyPolicy, + catchUpPolicy: routine.catchUpPolicy, + triggers: routine.triggers.map((trigger) => ({ + kind: trigger.kind, + label: trigger.label ?? null, + enabled: Boolean(trigger.enabled), + cronExpression: trigger.kind === "schedule" ? trigger.cronExpression ?? null : null, + timezone: trigger.kind === "schedule" ? trigger.timezone ?? null : null, + signingMode: trigger.kind === "webhook" ? trigger.signingMode ?? null : null, + replayWindowSec: trigger.kind === "webhook" ? trigger.replayWindowSec ?? null : null, + })), + }; +} + +function containsAbsolutePathFragment(value: string) { + return /(^|\s)(\/[^/\s]|[A-Za-z]:[\\/])/.test(value); +} + +function containsSystemDependentPathValue(value: unknown): boolean { + if (typeof value === "string") { + return path.isAbsolute(value) || /^[A-Za-z]:[\\/]/.test(value) || containsAbsolutePathFragment(value); + } + if (Array.isArray(value)) { + return value.some((entry) => containsSystemDependentPathValue(entry)); + } + if (isPlainRecord(value)) { + return Object.values(value).some((entry) => containsSystemDependentPathValue(entry)); + } + return false; +} + +function clonePortableRecord(value: unknown) { + if (!isPlainRecord(value)) return null; + return structuredClone(value) as Record; +} + +function disableImportedTimerHeartbeat(runtimeConfig: unknown) { + const next = clonePortableRecord(runtimeConfig) ?? {}; + const heartbeat = isPlainRecord(next.heartbeat) ? { ...next.heartbeat } : {}; + heartbeat.enabled = false; + next.heartbeat = heartbeat; + return next; +} + +function normalizePortableProjectWorkspaceExtension( + workspaceKey: string, + value: unknown, +): CompanyPortabilityProjectWorkspaceManifestEntry | null { + if (!isPlainRecord(value)) return null; + const normalizedKey = normalizeAgentUrlKey(workspaceKey) ?? workspaceKey.trim(); + if (!normalizedKey) return null; + return { + key: normalizedKey, + name: asString(value.name) ?? normalizedKey, + sourceType: asString(value.sourceType), + repoUrl: asString(value.repoUrl), + repoRef: asString(value.repoRef), + defaultRef: asString(value.defaultRef), + visibility: asString(value.visibility), + setupCommand: asString(value.setupCommand), + cleanupCommand: asString(value.cleanupCommand), + metadata: isPlainRecord(value.metadata) ? value.metadata : null, + isPrimary: asBoolean(value.isPrimary) ?? false, + }; +} + +function derivePortableProjectWorkspaceKey( + workspace: NonNullable[number], + usedKeys: Set, +) { + const baseKey = + normalizeAgentUrlKey(workspace.name) + ?? normalizeAgentUrlKey(asString(workspace.repoUrl)?.split("/").pop()?.replace(/\.git$/i, "") ?? "") + ?? "workspace"; + return uniqueSlug(baseKey, usedKeys); +} + +function exportPortableProjectExecutionWorkspacePolicy( + projectSlug: string, + policy: unknown, + workspaceKeyById: Map, + warnings: string[], +) { + const next = clonePortableRecord(policy); + if (!next) return null; + const defaultWorkspaceId = asString(next.defaultProjectWorkspaceId); + if (defaultWorkspaceId) { + const defaultWorkspaceKey = workspaceKeyById.get(defaultWorkspaceId); + if (defaultWorkspaceKey) { + next.defaultProjectWorkspaceKey = defaultWorkspaceKey; + } else { + warnings.push(`Project ${projectSlug} default workspace ${defaultWorkspaceId} was omitted from export because that workspace is not portable.`); + } + delete next.defaultProjectWorkspaceId; + } + const cleaned = stripEmptyValues(next); + return isPlainRecord(cleaned) ? cleaned : null; +} + +function importPortableProjectExecutionWorkspacePolicy( + projectSlug: string, + policy: Record | null | undefined, + workspaceIdByKey: Map, + warnings: string[], +) { + const next = clonePortableRecord(policy); + if (!next) return null; + const defaultWorkspaceKey = asString(next.defaultProjectWorkspaceKey); + if (defaultWorkspaceKey) { + const defaultWorkspaceId = workspaceIdByKey.get(defaultWorkspaceKey); + if (defaultWorkspaceId) { + next.defaultProjectWorkspaceId = defaultWorkspaceId; + } else { + warnings.push(`Project ${projectSlug} references missing workspace key ${defaultWorkspaceKey}; imported execution workspace policy without a default workspace.`); + } + } + delete next.defaultProjectWorkspaceKey; + const cleaned = stripEmptyValues(next); + return isPlainRecord(cleaned) ? cleaned : null; +} + +function stripPortableProjectExecutionWorkspaceRefs(policy: Record | null | undefined) { + const next = clonePortableRecord(policy); + if (!next) return null; + delete next.defaultProjectWorkspaceId; + delete next.defaultProjectWorkspaceKey; + const cleaned = stripEmptyValues(next); + return isPlainRecord(cleaned) ? cleaned : null; +} + +async function readGitOutput(cwd: string, args: string[]) { + const { stdout } = await execFileAsync("git", ["-C", cwd, ...args], { cwd }); + const trimmed = stdout.trim(); + return trimmed.length > 0 ? trimmed : null; +} + +async function inferPortableWorkspaceGitMetadata(workspace: NonNullable[number]) { + const cwd = asString(workspace.cwd); + if (!cwd) { + return { + repoUrl: null, + repoRef: null, + defaultRef: null, + }; + } + + let repoUrl: string | null = null; + try { + repoUrl = await readGitOutput(cwd, ["remote", "get-url", "origin"]); + } catch { + try { + const firstRemote = await readGitOutput(cwd, ["remote"]); + const remoteName = firstRemote?.split("\n").map((entry) => entry.trim()).find(Boolean) ?? null; + if (remoteName) { + repoUrl = await readGitOutput(cwd, ["remote", "get-url", remoteName]); + } + } catch { + repoUrl = null; + } + } + + let repoRef: string | null = null; + try { + repoRef = await readGitOutput(cwd, ["branch", "--show-current"]); + } catch { + repoRef = null; + } + + let defaultRef: string | null = null; + try { + const remoteHead = await readGitOutput(cwd, ["symbolic-ref", "--quiet", "--short", "refs/remotes/origin/HEAD"]); + defaultRef = remoteHead?.startsWith("origin/") ? remoteHead.slice("origin/".length) : remoteHead; + } catch { + defaultRef = null; + } + + return { + repoUrl, + repoRef, + defaultRef, + }; +} + +async function buildPortableProjectWorkspaces( + projectSlug: string, + workspaces: ProjectLike["workspaces"] | undefined, + warnings: string[], +) { + const exportedWorkspaces: Record> = {}; + const manifestWorkspaces: CompanyPortabilityProjectWorkspaceManifestEntry[] = []; + const workspaceKeyById = new Map(); + const workspaceKeyBySignature = new Map(); + const manifestWorkspaceByKey = new Map(); + const usedKeys = new Set(); + + for (const workspace of workspaces ?? []) { + const inferredGitMetadata = + !asString(workspace.repoUrl) || !asString(workspace.repoRef) || !asString(workspace.defaultRef) + ? await inferPortableWorkspaceGitMetadata(workspace) + : { repoUrl: null, repoRef: null, defaultRef: null }; + const repoUrl = asString(workspace.repoUrl) ?? inferredGitMetadata.repoUrl; + if (!repoUrl) { + warnings.push(`Project ${projectSlug} workspace ${workspace.name} was omitted from export because it does not have a portable repoUrl.`); + continue; + } + const repoRef = asString(workspace.repoRef) ?? inferredGitMetadata.repoRef; + const defaultRef = asString(workspace.defaultRef) ?? inferredGitMetadata.defaultRef ?? repoRef; + const workspaceSignature = JSON.stringify({ + name: workspace.name, + repoUrl, + repoRef, + defaultRef, + }); + const existingWorkspaceKey = workspaceKeyBySignature.get(workspaceSignature); + if (existingWorkspaceKey) { + workspaceKeyById.set(workspace.id, existingWorkspaceKey); + const existingManifestWorkspace = manifestWorkspaceByKey.get(existingWorkspaceKey); + if (existingManifestWorkspace && workspace.isPrimary) { + existingManifestWorkspace.isPrimary = true; + const existingExtensionWorkspace = exportedWorkspaces[existingWorkspaceKey]; + if (isPlainRecord(existingExtensionWorkspace)) existingExtensionWorkspace.isPrimary = true; + } + continue; + } + + const workspaceKey = derivePortableProjectWorkspaceKey(workspace, usedKeys); + workspaceKeyById.set(workspace.id, workspaceKey); + workspaceKeyBySignature.set(workspaceSignature, workspaceKey); + + let setupCommand = asString(workspace.setupCommand); + if (setupCommand && containsAbsolutePathFragment(setupCommand)) { + warnings.push(`Project ${projectSlug} workspace ${workspaceKey} setupCommand was omitted from export because it is system-dependent.`); + setupCommand = null; + } + + let cleanupCommand = asString(workspace.cleanupCommand); + if (cleanupCommand && containsAbsolutePathFragment(cleanupCommand)) { + warnings.push(`Project ${projectSlug} workspace ${workspaceKey} cleanupCommand was omitted from export because it is system-dependent.`); + cleanupCommand = null; + } + + const metadata = isPlainRecord(workspace.metadata) && !containsSystemDependentPathValue(workspace.metadata) + ? workspace.metadata + : null; + if (isPlainRecord(workspace.metadata) && metadata == null) { + warnings.push(`Project ${projectSlug} workspace ${workspaceKey} metadata was omitted from export because it contains system-dependent paths.`); + } + + const portableWorkspace = stripEmptyValues({ + name: workspace.name, + sourceType: workspace.sourceType, + repoUrl, + repoRef, + defaultRef, + visibility: asString(workspace.visibility), + setupCommand, + cleanupCommand, + metadata, + isPrimary: workspace.isPrimary ? true : undefined, + }); + if (!isPlainRecord(portableWorkspace)) continue; + + exportedWorkspaces[workspaceKey] = portableWorkspace; + const manifestWorkspace = { + key: workspaceKey, + name: workspace.name, + sourceType: asString(workspace.sourceType), + repoUrl, + repoRef, + defaultRef, + visibility: asString(workspace.visibility), + setupCommand, + cleanupCommand, + metadata, + isPrimary: workspace.isPrimary, + }; + manifestWorkspaces.push(manifestWorkspace); + manifestWorkspaceByKey.set(workspaceKey, manifestWorkspace); + } + + return { + extension: Object.keys(exportedWorkspaces).length > 0 ? exportedWorkspaces : undefined, + manifest: manifestWorkspaces, + workspaceKeyById, + }; +} + +const WEEKDAY_TO_CRON: Record = { + sunday: "0", + monday: "1", + tuesday: "2", + wednesday: "3", + thursday: "4", + friday: "5", + saturday: "6", +}; + +function readZonedDateParts(startsAt: string, timeZone: string) { + try { + const date = new Date(startsAt); + if (Number.isNaN(date.getTime())) return null; + const formatter = new Intl.DateTimeFormat("en-US", { + timeZone, + hour12: false, + weekday: "long", + month: "numeric", + day: "numeric", + hour: "numeric", + minute: "numeric", + }); + const parts = Object.fromEntries( + formatter + .formatToParts(date) + .filter((entry) => entry.type !== "literal") + .map((entry) => [entry.type, entry.value]), + ) as Record; + const weekday = WEEKDAY_TO_CRON[parts.weekday?.toLowerCase() ?? ""]; + const month = Number(parts.month); + const day = Number(parts.day); + const hour = Number(parts.hour); + const minute = Number(parts.minute); + if (!weekday || !Number.isFinite(month) || !Number.isFinite(day) || !Number.isFinite(hour) || !Number.isFinite(minute)) { + return null; + } + return { weekday, month, day, hour, minute }; + } catch { + return null; + } +} + +function normalizeCronList(values: string[]) { + return Array.from(new Set(values)).sort((left, right) => Number(left) - Number(right)).join(","); +} + +function buildLegacyRoutineTriggerFromRecurrence( + issue: Pick, + scheduleValue: unknown, +) { + const warnings: string[] = []; + const errors: string[] = []; + if (!issue.legacyRecurrence || !isPlainRecord(issue.legacyRecurrence)) { + return { trigger: null, warnings, errors }; + } + + const schedule = isPlainRecord(scheduleValue) ? scheduleValue : null; + const frequency = asString(issue.legacyRecurrence.frequency); + const interval = asInteger(issue.legacyRecurrence.interval) ?? 1; + if (!frequency) { + errors.push(`Recurring task ${issue.slug} uses legacy recurrence without frequency; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + if (interval < 1) { + errors.push(`Recurring task ${issue.slug} uses legacy recurrence with an invalid interval; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + + const timezone = asString(schedule?.timezone) ?? "UTC"; + const startsAt = asString(schedule?.startsAt); + const zonedStartsAt = startsAt ? readZonedDateParts(startsAt, timezone) : null; + if (startsAt && !zonedStartsAt) { + errors.push(`Recurring task ${issue.slug} has an invalid legacy startsAt/timezone combination; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + + const time = isPlainRecord(issue.legacyRecurrence.time) ? issue.legacyRecurrence.time : null; + const hour = asInteger(time?.hour) ?? zonedStartsAt?.hour ?? 0; + const minute = asInteger(time?.minute) ?? zonedStartsAt?.minute ?? 0; + if (hour < 0 || hour > 23 || minute < 0 || minute > 59) { + errors.push(`Recurring task ${issue.slug} uses legacy recurrence with an invalid time; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + + if (issue.legacyRecurrence.until != null || issue.legacyRecurrence.count != null) { + warnings.push(`Recurring task ${issue.slug} uses legacy recurrence end bounds; Paperclip will import the routine trigger without those limits.`); + } + + let cronExpression: string | null = null; + + if (frequency === "hourly") { + const hourField = interval === 1 + ? "*" + : zonedStartsAt + ? `${zonedStartsAt.hour}-23/${interval}` + : `*/${interval}`; + cronExpression = `${minute} ${hourField} * * *`; + } else if (frequency === "daily") { + if (Array.isArray(issue.legacyRecurrence.weekdays) || Array.isArray(issue.legacyRecurrence.monthDays) || Array.isArray(issue.legacyRecurrence.months)) { + errors.push(`Recurring task ${issue.slug} uses unsupported legacy daily recurrence constraints; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + const dayField = interval === 1 ? "*" : `*/${interval}`; + cronExpression = `${minute} ${hour} ${dayField} * *`; + } else if (frequency === "weekly") { + if (interval !== 1) { + errors.push(`Recurring task ${issue.slug} uses legacy weekly recurrence with interval > 1; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + const weekdays = Array.isArray(issue.legacyRecurrence.weekdays) + ? issue.legacyRecurrence.weekdays + .map((entry) => asString(entry)) + .filter((entry): entry is string => Boolean(entry)) + : []; + const cronWeekdays = weekdays + .map((entry) => WEEKDAY_TO_CRON[entry.toLowerCase()]) + .filter((entry): entry is string => Boolean(entry)); + if (cronWeekdays.length === 0 && zonedStartsAt?.weekday) { + cronWeekdays.push(zonedStartsAt.weekday); + } + if (cronWeekdays.length === 0) { + errors.push(`Recurring task ${issue.slug} uses legacy weekly recurrence without weekdays; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + cronExpression = `${minute} ${hour} * * ${normalizeCronList(cronWeekdays)}`; + } else if (frequency === "monthly") { + if (interval !== 1) { + errors.push(`Recurring task ${issue.slug} uses legacy monthly recurrence with interval > 1; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + if (Array.isArray(issue.legacyRecurrence.ordinalWeekdays) && issue.legacyRecurrence.ordinalWeekdays.length > 0) { + errors.push(`Recurring task ${issue.slug} uses legacy ordinal monthly recurrence; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + const monthDays = Array.isArray(issue.legacyRecurrence.monthDays) + ? issue.legacyRecurrence.monthDays + .map((entry) => asInteger(entry)) + .filter((entry): entry is number => entry != null && entry >= 1 && entry <= 31) + : []; + if (monthDays.length === 0 && zonedStartsAt?.day) { + monthDays.push(zonedStartsAt.day); + } + if (monthDays.length === 0) { + errors.push(`Recurring task ${issue.slug} uses legacy monthly recurrence without monthDays; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + const months = Array.isArray(issue.legacyRecurrence.months) + ? issue.legacyRecurrence.months + .map((entry) => asInteger(entry)) + .filter((entry): entry is number => entry != null && entry >= 1 && entry <= 12) + : []; + const monthField = months.length > 0 ? normalizeCronList(months.map(String)) : "*"; + cronExpression = `${minute} ${hour} ${normalizeCronList(monthDays.map(String))} ${monthField} *`; + } else if (frequency === "yearly") { + if (interval !== 1) { + errors.push(`Recurring task ${issue.slug} uses legacy yearly recurrence with interval > 1; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + const months = Array.isArray(issue.legacyRecurrence.months) + ? issue.legacyRecurrence.months + .map((entry) => asInteger(entry)) + .filter((entry): entry is number => entry != null && entry >= 1 && entry <= 12) + : []; + if (months.length === 0 && zonedStartsAt?.month) { + months.push(zonedStartsAt.month); + } + const monthDays = Array.isArray(issue.legacyRecurrence.monthDays) + ? issue.legacyRecurrence.monthDays + .map((entry) => asInteger(entry)) + .filter((entry): entry is number => entry != null && entry >= 1 && entry <= 31) + : []; + if (monthDays.length === 0 && zonedStartsAt?.day) { + monthDays.push(zonedStartsAt.day); + } + if (months.length === 0 || monthDays.length === 0) { + errors.push(`Recurring task ${issue.slug} uses legacy yearly recurrence without month/monthDay anchors; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + cronExpression = `${minute} ${hour} ${normalizeCronList(monthDays.map(String))} ${normalizeCronList(months.map(String))} *`; + } else { + errors.push(`Recurring task ${issue.slug} uses unsupported legacy recurrence frequency "${frequency}"; add .paperclip.yaml routines.${issue.slug}.triggers.`); + return { trigger: null, warnings, errors }; + } + + return { + trigger: { + kind: "schedule", + label: "Migrated legacy recurrence", + enabled: true, + cronExpression, + timezone, + signingMode: null, + replayWindowSec: null, + } satisfies CompanyPortabilityIssueRoutineTriggerManifestEntry, + warnings, + errors, + }; +} + +function resolvePortableRoutineDefinition( + issue: Pick, + scheduleValue: unknown, +) { + const warnings: string[] = []; + const errors: string[] = []; + if (!issue.recurring) { + return { routine: null, warnings, errors }; + } + + const routine = issue.routine + ? { + concurrencyPolicy: issue.routine.concurrencyPolicy, + catchUpPolicy: issue.routine.catchUpPolicy, + triggers: [...issue.routine.triggers], + } + : { + concurrencyPolicy: null, + catchUpPolicy: null, + triggers: [] as CompanyPortabilityIssueRoutineTriggerManifestEntry[], + }; + + if (routine.concurrencyPolicy && !ROUTINE_CONCURRENCY_POLICIES.includes(routine.concurrencyPolicy as any)) { + errors.push(`Recurring task ${issue.slug} uses unsupported routine concurrencyPolicy "${routine.concurrencyPolicy}".`); + } + if (routine.catchUpPolicy && !ROUTINE_CATCH_UP_POLICIES.includes(routine.catchUpPolicy as any)) { + errors.push(`Recurring task ${issue.slug} uses unsupported routine catchUpPolicy "${routine.catchUpPolicy}".`); + } + + for (const trigger of routine.triggers) { + if (!ROUTINE_TRIGGER_KINDS.includes(trigger.kind as any)) { + errors.push(`Recurring task ${issue.slug} uses unsupported trigger kind "${trigger.kind}".`); + continue; + } + if (trigger.kind === "schedule") { + if (!trigger.cronExpression || !trigger.timezone) { + errors.push(`Recurring task ${issue.slug} has a schedule trigger missing cronExpression/timezone.`); + continue; + } + const cronError = validateCron(trigger.cronExpression); + if (cronError) { + errors.push(`Recurring task ${issue.slug} has an invalid schedule trigger: ${cronError}`); + } + continue; + } + if (trigger.kind === "webhook" && trigger.signingMode && !ROUTINE_TRIGGER_SIGNING_MODES.includes(trigger.signingMode as any)) { + errors.push(`Recurring task ${issue.slug} uses unsupported webhook signingMode "${trigger.signingMode}".`); + } + } + + if (routine.triggers.length === 0 && issue.legacyRecurrence) { + const migrated = buildLegacyRoutineTriggerFromRecurrence(issue, scheduleValue); + warnings.push(...migrated.warnings); + errors.push(...migrated.errors); + if (migrated.trigger) { + routine.triggers.push(migrated.trigger); + } + } + + return { routine, warnings, errors }; +} + function toSafeSlug(input: string, fallback: string) { return normalizeAgentUrlKey(input) ?? fallback; } @@ -701,79 +1319,103 @@ function collectSelectedExportSlugs(selectedFiles: Set) { const taskMatch = filePath.match(/^tasks\/([^/]+)\//); if (taskMatch) tasks.add(taskMatch[1]!); } - return { agents, projects, tasks }; + return { agents, projects, tasks, routines: new Set(tasks) }; } -function filterPortableExtensionYaml(yaml: string, selectedFiles: Set) { - const selected = collectSelectedExportSlugs(selectedFiles); - const lines = yaml.split("\n"); - const out: string[] = []; - const filterableSections = new Set(["agents", "projects", "tasks"]); - - let currentSection: string | null = null; - let currentEntry: string | null = null; - let includeEntry = true; - let sectionHeaderLine: string | null = null; - let sectionBuffer: string[] = []; - - const flushSection = () => { - if (sectionHeaderLine !== null && sectionBuffer.length > 0) { - out.push(sectionHeaderLine); - out.push(...sectionBuffer); - } - sectionHeaderLine = null; - sectionBuffer = []; +function normalizePortableSlugList(value: unknown) { + if (!Array.isArray(value)) return []; + const seen = new Set(); + const normalized: string[] = []; + for (const entry of value) { + if (typeof entry !== "string") continue; + const trimmed = entry.trim(); + if (!trimmed || seen.has(trimmed)) continue; + seen.add(trimmed); + normalized.push(trimmed); + } + return normalized; +} + +function normalizePortableSidebarOrder(value: unknown): CompanyPortabilitySidebarOrder | null { + if (!isPlainRecord(value)) return null; + const sidebar = { + agents: normalizePortableSlugList(value.agents), + projects: normalizePortableSlugList(value.projects), }; + return sidebar.agents.length > 0 || sidebar.projects.length > 0 ? sidebar : null; +} - for (const line of lines) { - const topMatch = line.match(/^([a-zA-Z_][\w-]*):\s*(.*)$/); - if (topMatch && !line.startsWith(" ")) { - flushSection(); - currentEntry = null; - includeEntry = true; - - const key = topMatch[1]!; - if (filterableSections.has(key)) { - currentSection = key; - sectionHeaderLine = line; - continue; - } +function sortAgentsBySidebarOrder(agents: T[]) { + if (agents.length === 0) return []; - currentSection = null; - out.push(line); - continue; - } + const byId = new Map(agents.map((agent) => [agent.id, agent])); + const childrenOf = new Map(); + for (const agent of agents) { + const parentId = agent.reportsTo && byId.has(agent.reportsTo) ? agent.reportsTo : null; + const siblings = childrenOf.get(parentId) ?? []; + siblings.push(agent); + childrenOf.set(parentId, siblings); + } - if (currentSection && filterableSections.has(currentSection)) { - const entryMatch = line.match(/^ ([\w][\w-]*):\s*(.*)$/); - if (entryMatch && !line.startsWith(" ")) { - const slug = entryMatch[1]!; - currentEntry = slug; - const sectionSlugs = selected[currentSection as keyof typeof selected]; - includeEntry = sectionSlugs.has(slug); - if (includeEntry) sectionBuffer.push(line); - continue; - } + for (const siblings of childrenOf.values()) { + siblings.sort((left, right) => left.name.localeCompare(right.name)); + } - if (currentEntry !== null) { - if (includeEntry) sectionBuffer.push(line); - continue; - } + const sorted: T[] = []; + const queue = [...(childrenOf.get(null) ?? [])]; + while (queue.length > 0) { + const agent = queue.shift(); + if (!agent) continue; + sorted.push(agent); + const children = childrenOf.get(agent.id); + if (children) queue.push(...children); + } - sectionBuffer.push(line); - continue; + return sorted; +} + +function filterPortableExtensionYaml(yaml: string, selectedFiles: Set) { + const selected = collectSelectedExportSlugs(selectedFiles); + const parsed = parseYamlFile(yaml); + for (const section of ["agents", "projects", "tasks", "routines"] as const) { + const sectionValue = parsed[section]; + if (!isPlainRecord(sectionValue)) continue; + const sectionSlugs = selected[section]; + const filteredEntries = Object.fromEntries( + Object.entries(sectionValue).filter(([slug]) => sectionSlugs.has(slug)), + ); + if (Object.keys(filteredEntries).length > 0) { + parsed[section] = filteredEntries; + } else { + delete parsed[section]; } + } - out.push(line); + const companySection = parsed.company; + if (isPlainRecord(companySection)) { + const logoPath = asString(companySection.logoPath) ?? asString(companySection.logo); + if (logoPath && !selectedFiles.has(logoPath)) { + delete companySection.logoPath; + delete companySection.logo; + } } - flushSection(); - let filtered = out.join("\n"); - const logoPathMatch = filtered.match(/^\s{2}logoPath:\s*["']?([^"'\n]+)["']?\s*$/m); - if (logoPathMatch && !selectedFiles.has(logoPathMatch[1]!)) { - filtered = filtered.replace(/^\s{2}logoPath:\s*["']?([^"'\n]+)["']?\s*\n?/m, ""); + const sidebarOrder = normalizePortableSidebarOrder(parsed.sidebar); + if (sidebarOrder) { + const filteredSidebar = stripEmptyValues({ + agents: sidebarOrder.agents.filter((slug) => selected.agents.has(slug)), + projects: sidebarOrder.projects.filter((slug) => selected.projects.has(slug)), + }); + if (isPlainRecord(filteredSidebar)) { + parsed.sidebar = filteredSidebar; + } else { + delete parsed.sidebar; + } + } else { + delete parsed.sidebar; } - return filtered; + + return buildYamlFile(parsed, { preserveEmptyStrings: true }); } function filterExportFiles( @@ -1601,9 +2243,11 @@ function buildManifestFromPackageFiles( ? parseYamlFile(readPortableTextFile(normalizedFiles, paperclipExtensionPath) ?? "") : {}; const paperclipCompany = isPlainRecord(paperclipExtension.company) ? paperclipExtension.company : {}; + const paperclipSidebar = normalizePortableSidebarOrder(paperclipExtension.sidebar); const paperclipAgents = isPlainRecord(paperclipExtension.agents) ? paperclipExtension.agents : {}; const paperclipProjects = isPlainRecord(paperclipExtension.projects) ? paperclipExtension.projects : {}; const paperclipTasks = isPlainRecord(paperclipExtension.tasks) ? paperclipExtension.tasks : {}; + const paperclipRoutines = isPlainRecord(paperclipExtension.routines) ? paperclipExtension.routines : {}; const companyName = asString(companyFrontmatter.name) ?? opts?.sourceLabel?.companyName @@ -1644,7 +2288,7 @@ function buildManifestFromPackageFiles( const skillPaths = Array.from(new Set([...referencedSkillPaths, ...discoveredSkillPaths])).sort(); const manifest: CompanyPortabilityManifest = { - schemaVersion: 3, + schemaVersion: 4, generatedAt: new Date().toISOString(), source: opts?.sourceLabel ?? null, includes: { @@ -1665,6 +2309,7 @@ function buildManifestFromPackageFiles( ? paperclipCompany.requireBoardApprovalForNewAgents : readCompanyApprovalDefault(companyFrontmatter), }, + sidebar: paperclipSidebar, agents: [], skills: [], projects: [], @@ -1824,6 +2469,10 @@ function buildManifestFromPackageFiles( ); const slug = asString(frontmatter.slug) ?? fallbackSlug; const extension = isPlainRecord(paperclipProjects[slug]) ? paperclipProjects[slug] : {}; + const workspaceExtensions = isPlainRecord(extension.workspaces) ? extension.workspaces : {}; + const workspaces = Object.entries(workspaceExtensions) + .map(([workspaceKey, entry]) => normalizePortableProjectWorkspaceExtension(workspaceKey, entry)) + .filter((entry): entry is CompanyPortabilityProjectWorkspaceManifestEntry => entry !== null); manifest.projects.push({ slug, name: asString(frontmatter.name) ?? slug, @@ -1837,6 +2486,7 @@ function buildManifestFromPackageFiles( executionWorkspacePolicy: isPlainRecord(extension.executionWorkspacePolicy) ? extension.executionWorkspacePolicy : null, + workspaces, metadata: isPlainRecord(extension.metadata) ? extension.metadata : null, }); if (frontmatter.kind && frontmatter.kind !== "project") { @@ -1855,23 +2505,32 @@ function buildManifestFromPackageFiles( const fallbackSlug = normalizeAgentUrlKey(path.posix.basename(path.posix.dirname(taskPath))) ?? "task"; const slug = asString(frontmatter.slug) ?? fallbackSlug; const extension = isPlainRecord(paperclipTasks[slug]) ? paperclipTasks[slug] : {}; + const routineExtension = normalizeRoutineExtension(paperclipRoutines[slug]); + const routineExtensionRaw = isPlainRecord(paperclipRoutines[slug]) ? paperclipRoutines[slug] : {}; const schedule = isPlainRecord(frontmatter.schedule) ? frontmatter.schedule : null; - const recurrence = schedule && isPlainRecord(schedule.recurrence) + const legacyRecurrence = schedule && isPlainRecord(schedule.recurrence) ? schedule.recurrence : isPlainRecord(extension.recurrence) ? extension.recurrence : null; + const recurring = + asBoolean(frontmatter.recurring) === true + || routineExtension !== null + || legacyRecurrence !== null; manifest.issues.push({ slug, identifier: asString(extension.identifier), title: asString(frontmatter.name) ?? asString(frontmatter.title) ?? slug, path: taskPath, projectSlug: asString(frontmatter.project), + projectWorkspaceKey: asString(extension.projectWorkspaceKey), assigneeAgentSlug: asString(frontmatter.assignee), description: taskDoc.body || asString(frontmatter.description), - recurrence, - status: asString(extension.status), - priority: asString(extension.priority), + recurring, + routine: routineExtension, + legacyRecurrence, + status: asString(extension.status) ?? asString(routineExtensionRaw.status), + priority: asString(extension.priority) ?? asString(routineExtensionRaw.priority), labelIds: Array.isArray(extension.labelIds) ? extension.labelIds.filter((entry): entry is string => typeof entry === "string") : [], @@ -1898,7 +2557,12 @@ function buildManifestFromPackageFiles( } -function parseGitHubSourceUrl(rawUrl: string) { +function normalizeGitHubSourcePath(value: string | null | undefined) { + if (!value) return ""; + return value.trim().replace(/\\/g, "/").replace(/^\/+|\/+$/g, ""); +} + +export function parseGitHubSourceUrl(rawUrl: string) { const url = new URL(rawUrl); if (url.hostname !== "github.com") { throw unprocessable("GitHub source must use github.com URL"); @@ -1909,6 +2573,24 @@ function parseGitHubSourceUrl(rawUrl: string) { } const owner = parts[0]!; const repo = parts[1]!.replace(/\.git$/i, ""); + const queryRef = url.searchParams.get("ref")?.trim(); + const queryPath = normalizeGitHubSourcePath(url.searchParams.get("path")); + const queryCompanyPath = normalizeGitHubSourcePath(url.searchParams.get("companyPath")); + if (queryRef || queryPath || queryCompanyPath) { + const companyPath = queryCompanyPath || [queryPath, "COMPANY.md"].filter(Boolean).join("/") || "COMPANY.md"; + let basePath = queryPath; + if (!basePath && companyPath !== "COMPANY.md") { + basePath = path.posix.dirname(companyPath); + if (basePath === ".") basePath = ""; + } + return { + owner, + repo, + ref: queryRef || "main", + basePath, + companyPath, + }; + } let ref = "main"; let basePath = ""; let companyPath = "COMPANY.md"; @@ -2056,6 +2738,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { const files: Record = {}; const warnings: string[] = []; const envInputs: CompanyPortabilityManifest["envInputs"] = []; + const requestedSidebarOrder = normalizePortableSidebarOrder(input.sidebarOrder); const rootPath = normalizeAgentUrlKey(company.name) ?? "company-package"; let companyLogoPath: string | null = null; @@ -2111,8 +2794,10 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { const projectsSvc = projectService(db); const issuesSvc = issueService(db); + const routinesSvc = routineService(db); const allProjectsRaw = include.projects || include.issues ? await projectsSvc.list(companyId) : []; const allProjects = allProjectsRaw.filter((project) => !project.archivedAt); + const allRoutines = include.issues ? await routinesSvc.list(companyId) : []; const projectById = new Map(allProjects.map((project) => [project.id, project])); const projectByReference = new Map(); for (const project of allProjects) { @@ -2132,6 +2817,8 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { } const selectedIssues = new Map>>(); + const selectedRoutines = new Map(); + const routineById = new Map(allRoutines.map((routine) => [routine.id, routine])); const resolveIssueBySelector = async (selector: string) => { const trimmed = selector.trim(); if (!trimmed) return null; @@ -2142,6 +2829,15 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { for (const selector of input.issues ?? []) { const issue = await resolveIssueBySelector(selector); if (!issue || issue.companyId !== companyId) { + const routine = routineById.get(selector.trim()); + if (routine) { + selectedRoutines.set(routine.id, routine); + if (routine.projectId) { + const parentProject = projectById.get(routine.projectId); + if (parentProject) selectedProjects.set(parentProject.id, parentProject); + } + continue; + } warnings.push(`Issue selector "${selector}" was not found and was skipped.`); continue; } @@ -2163,6 +2859,9 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { for (const issue of projectIssues) { selectedIssues.set(issue.id, issue); } + for (const routine of allRoutines.filter((entry) => entry.projectId === match.id)) { + selectedRoutines.set(routine.id, routine); + } } if (include.projects && selectedProjects.size === 0) { @@ -2180,6 +2879,15 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { if (parentProject) selectedProjects.set(parentProject.id, parentProject); } } + if (selectedRoutines.size === 0) { + for (const routine of allRoutines) { + selectedRoutines.set(routine.id, routine); + if (routine.projectId) { + const parentProject = projectById.get(routine.projectId); + if (parentProject) selectedProjects.set(parentProject.id, parentProject); + } + } + } } const selectedProjectRows = Array.from(selectedProjects.values()) @@ -2187,20 +2895,39 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { const selectedIssueRows = Array.from(selectedIssues.values()) .filter((issue): issue is NonNullable => issue != null) .sort((left, right) => (left.identifier ?? left.title).localeCompare(right.identifier ?? right.title)); + const selectedRoutineSummaries = Array.from(selectedRoutines.values()) + .sort((left, right) => left.title.localeCompare(right.title)); + const selectedRoutineRows = ( + await Promise.all(selectedRoutineSummaries.map((routine) => routinesSvc.getDetail(routine.id))) + ).filter((routine): routine is RoutineLike => routine !== null); const taskSlugByIssueId = new Map(); + const taskSlugByRoutineId = new Map(); const usedTaskSlugs = new Set(); for (const issue of selectedIssueRows) { const baseSlug = normalizeAgentUrlKey(issue.identifier ?? issue.title) ?? "task"; taskSlugByIssueId.set(issue.id, uniqueSlug(baseSlug, usedTaskSlugs)); } + for (const routine of selectedRoutineRows) { + const baseSlug = normalizeAgentUrlKey(routine.title) ?? "task"; + taskSlugByRoutineId.set(routine.id, uniqueSlug(baseSlug, usedTaskSlugs)); + } const projectSlugById = new Map(); + const projectWorkspaceKeyByProjectId = new Map>(); const usedProjectSlugs = new Set(); for (const project of selectedProjectRows) { const baseSlug = deriveProjectUrlKey(project.name, project.name); projectSlugById.set(project.id, uniqueSlug(baseSlug, usedProjectSlugs)); } + const sidebarOrder = requestedSidebarOrder ?? stripEmptyValues({ + agents: sortAgentsBySidebarOrder(Array.from(selectedAgents.values())) + .map((agent) => idToSlug.get(agent.id)) + .filter((slug): slug is string => Boolean(slug)), + projects: selectedProjectRows + .map((project) => projectSlugById.get(project.id)) + .filter((slug): slug is string => Boolean(slug)), + }); const companyPath = "COMPANY.md"; files[companyPath] = buildMarkdown( @@ -2236,6 +2963,8 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { const paperclipAgentsOut: Record> = {}; const paperclipProjectsOut: Record> = {}; const paperclipTasksOut: Record> = {}; + const unportableTaskWorkspaceRefs = new Map(); + const paperclipRoutinesOut: Record> = {}; const skillByReference = new Map(); for (const skill of companySkillRows) { @@ -2368,6 +3097,8 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { for (const project of selectedProjectRows) { const slug = projectSlugById.get(project.id)!; const projectPath = `projects/${slug}/PROJECT.md`; + const portableWorkspaces = await buildPortableProjectWorkspaces(slug, project.workspaces, warnings); + projectWorkspaceKeyByProjectId.set(project.id, portableWorkspaces.workspaceKeyById); files[projectPath] = buildMarkdown( { name: project.name, @@ -2381,7 +3112,13 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { targetDate: project.targetDate ?? null, color: project.color ?? null, status: project.status, - executionWorkspacePolicy: project.executionWorkspacePolicy ?? undefined, + executionWorkspacePolicy: exportPortableProjectExecutionWorkspacePolicy( + slug, + project.executionWorkspacePolicy, + portableWorkspaces.workspaceKeyById, + warnings, + ) ?? undefined, + workspaces: portableWorkspaces.extension, }); paperclipProjectsOut[slug] = isPlainRecord(extension) ? extension : {}; } @@ -2392,6 +3129,21 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { // All tasks go in top-level tasks/ folder, never nested under projects/ const taskPath = `tasks/${taskSlug}/TASK.md`; const assigneeSlug = issue.assigneeAgentId ? (idToSlug.get(issue.assigneeAgentId) ?? null) : null; + const projectWorkspaceKey = issue.projectId && issue.projectWorkspaceId + ? projectWorkspaceKeyByProjectId.get(issue.projectId)?.get(issue.projectWorkspaceId) ?? null + : null; + if (issue.projectWorkspaceId && !projectWorkspaceKey) { + const aggregateKey = `${issue.projectId ?? "no-project"}:${issue.projectWorkspaceId}`; + const existing = unportableTaskWorkspaceRefs.get(aggregateKey); + if (existing) { + existing.taskSlugs.push(taskSlug); + } else { + unportableTaskWorkspaceRefs.set(aggregateKey, { + workspaceId: issue.projectWorkspaceId, + taskSlugs: [taskSlug], + }); + } + } files[taskPath] = buildMarkdown( { name: issue.title, @@ -2406,12 +3158,53 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { priority: issue.priority, labelIds: issue.labelIds ?? undefined, billingCode: issue.billingCode ?? null, + projectWorkspaceKey: projectWorkspaceKey ?? undefined, executionWorkspaceSettings: issue.executionWorkspaceSettings ?? undefined, assigneeAdapterOverrides: issue.assigneeAdapterOverrides ?? undefined, }); paperclipTasksOut[taskSlug] = isPlainRecord(extension) ? extension : {}; } + for (const { workspaceId, taskSlugs } of unportableTaskWorkspaceRefs.values()) { + const preview = taskSlugs.slice(0, 4).join(", "); + const remainder = taskSlugs.length > 4 ? ` and ${taskSlugs.length - 4} more` : ""; + warnings.push(`Tasks ${preview}${remainder} reference workspace ${workspaceId}, but that workspace could not be exported portably.`); + } + + for (const routine of selectedRoutineRows) { + const taskSlug = taskSlugByRoutineId.get(routine.id)!; + const projectSlug = projectSlugById.get(routine.projectId) ?? null; + const taskPath = `tasks/${taskSlug}/TASK.md`; + const assigneeSlug = idToSlug.get(routine.assigneeAgentId) ?? null; + files[taskPath] = buildMarkdown( + { + name: routine.title, + project: projectSlug, + assignee: assigneeSlug, + recurring: true, + }, + routine.description ?? "", + ); + const extension = stripEmptyValues({ + status: routine.status !== "active" ? routine.status : undefined, + priority: routine.priority !== "medium" ? routine.priority : undefined, + concurrencyPolicy: routine.concurrencyPolicy !== "coalesce_if_active" ? routine.concurrencyPolicy : undefined, + catchUpPolicy: routine.catchUpPolicy !== "skip_missed" ? routine.catchUpPolicy : undefined, + triggers: routine.triggers.map((trigger) => stripEmptyValues({ + kind: trigger.kind, + label: trigger.label ?? null, + enabled: trigger.enabled ? undefined : false, + cronExpression: trigger.kind === "schedule" ? trigger.cronExpression ?? null : undefined, + timezone: trigger.kind === "schedule" ? trigger.timezone ?? null : undefined, + signingMode: trigger.kind === "webhook" && trigger.signingMode !== "bearer" ? trigger.signingMode ?? null : undefined, + replayWindowSec: trigger.kind === "webhook" && trigger.replayWindowSec !== 300 + ? trigger.replayWindowSec ?? null + : undefined, + })), + }); + paperclipRoutinesOut[taskSlug] = isPlainRecord(extension) ? extension : {}; + } + const paperclipExtensionPath = ".paperclip.yaml"; const paperclipAgents = Object.fromEntries( Object.entries(paperclipAgentsOut).filter(([, value]) => isPlainRecord(value) && Object.keys(value).length > 0), @@ -2422,6 +3215,9 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { const paperclipTasks = Object.fromEntries( Object.entries(paperclipTasksOut).filter(([, value]) => isPlainRecord(value) && Object.keys(value).length > 0), ); + const paperclipRoutines = Object.fromEntries( + Object.entries(paperclipRoutinesOut).filter(([, value]) => isPlainRecord(value) && Object.keys(value).length > 0), + ); files[paperclipExtensionPath] = buildYamlFile( { schema: "paperclip/v1", @@ -2430,9 +3226,11 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { logoPath: companyLogoPath, requireBoardApprovalForNewAgents: company.requireBoardApprovalForNewAgents ? undefined : false, }), + sidebar: stripEmptyValues(sidebarOrder), agents: Object.keys(paperclipAgents).length > 0 ? paperclipAgents : undefined, projects: Object.keys(paperclipProjects).length > 0 ? paperclipProjects : undefined, tasks: Object.keys(paperclipTasks).length > 0 ? paperclipTasks : undefined, + routines: Object.keys(paperclipRoutines).length > 0 ? paperclipRoutines : undefined, }, { preserveEmptyStrings: true }, ); @@ -2621,6 +3419,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { } if (include.issues) { + const projectBySlug = new Map(manifest.projects.map((project) => [project.slug, project])); for (const issue of manifest.issues) { const markdown = readPortableTextFile(source.files, ensureMarkdownPath(issue.path)); if (typeof markdown !== "string") { @@ -2631,8 +3430,24 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { if (parsed.frontmatter.kind && parsed.frontmatter.kind !== "task") { warnings.push(`Task markdown ${issue.path} does not declare kind: task in frontmatter.`); } - if (issue.recurrence) { - warnings.push(`Task ${issue.slug} has recurrence metadata; Paperclip will import it as a one-time issue for now.`); + if (issue.projectWorkspaceKey) { + const project = issue.projectSlug ? projectBySlug.get(issue.projectSlug) ?? null : null; + if (!project) { + warnings.push(`Task ${issue.slug} references workspace key ${issue.projectWorkspaceKey}, but its project is not present in the package.`); + } else if (!project.workspaces.some((workspace) => workspace.key === issue.projectWorkspaceKey)) { + warnings.push(`Task ${issue.slug} references missing project workspace key ${issue.projectWorkspaceKey}.`); + } + } + if (issue.recurring) { + if (!issue.projectSlug) { + errors.push(`Recurring task ${issue.slug} must declare a project to import as a routine.`); + } + if (!issue.assigneeAgentSlug) { + errors.push(`Recurring task ${issue.slug} must declare an assignee to import as a routine.`); + } + const resolvedRoutine = resolvePortableRoutineDefinition(issue, parsed.frontmatter.schedule); + warnings.push(...resolvedRoutine.warnings); + errors.push(...resolvedRoutine.errors); } } } @@ -2824,7 +3639,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { slug: manifestIssue.slug, action: "create", plannedTitle: manifestIssue.title, - reason: manifestIssue.recurrence ? "Recurrence will not be activated on import." : null, + reason: manifestIssue.recurring ? "Recurring task will be imported as a routine." : null, }); } } @@ -2994,6 +3809,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { } const resultAgents: CompanyPortabilityImportResult["agents"] = []; + const resultProjects: CompanyPortabilityImportResult["projects"] = []; const importedSlugToAgentId = new Map(); const existingSlugToAgentId = new Map(); const existingAgents = await agents.list(targetCompany.id); @@ -3001,6 +3817,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { existingSlugToAgentId.set(normalizeAgentUrlKey(existing.name) ?? existing.id, existing.id); } const importedSlugToProjectId = new Map(); + const importedProjectWorkspaceIdByProjectSlug = new Map>(); const existingProjectSlugToId = new Map(); const existingProjects = await projects.list(targetCompany.id); for (const existing of existingProjects) { @@ -3047,6 +3864,16 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { : []), ); const markdownRaw = bundleFiles["AGENTS.md"] ?? readPortableTextFile(plan.source.files, manifestAgent.path); + const entryRelativePath = normalizePortablePath(manifestAgent.path).startsWith(bundlePrefix) + ? normalizePortablePath(manifestAgent.path).slice(bundlePrefix.length) + : "AGENTS.md"; + if (typeof markdownRaw === "string") { + const importedInstructionsBody = parseFrontmatterMarkdown(markdownRaw).body; + bundleFiles[entryRelativePath] = importedInstructionsBody; + if (entryRelativePath !== "AGENTS.md") { + bundleFiles["AGENTS.md"] = importedInstructionsBody; + } + } const fallbackPromptTemplate = asString((manifestAgent.adapterConfig as Record).promptTemplate) || ""; if (!markdownRaw && fallbackPromptTemplate) { bundleFiles["AGENTS.md"] = fallbackPromptTemplate; @@ -3082,7 +3909,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { reportsTo: null, adapterType: effectiveAdapterType, adapterConfig: adapterConfigWithSkills, - runtimeConfig: manifestAgent.runtimeConfig, + runtimeConfig: disableImportedTimerHeartbeat(manifestAgent.runtimeConfig), budgetMonthlyCents: manifestAgent.budgetMonthlyCents, permissions: manifestAgent.permissions, metadata: manifestAgent.metadata, @@ -3172,13 +3999,23 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { for (const planProject of plan.preview.plan.projectPlans) { const manifestProject = sourceManifest.projects.find((project) => project.slug === planProject.slug); if (!manifestProject) continue; - if (planProject.action === "skip") continue; + if (planProject.action === "skip") { + resultProjects.push({ + slug: planProject.slug, + id: planProject.existingProjectId, + action: "skipped", + name: planProject.plannedName, + reason: planProject.reason, + }); + continue; + } const projectLeadAgentId = manifestProject.leadAgentSlug ? importedSlugToAgentId.get(manifestProject.leadAgentSlug) ?? existingSlugToAgentId.get(manifestProject.leadAgentSlug) ?? null : null; + const projectWorkspaceIdByKey = new Map(); const projectPatch = { name: planProject.plannedName, description: manifestProject.description, @@ -3188,27 +4025,86 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { status: manifestProject.status && PROJECT_STATUSES.includes(manifestProject.status as any) ? manifestProject.status as typeof PROJECT_STATUSES[number] : "backlog", - executionWorkspacePolicy: manifestProject.executionWorkspacePolicy, + executionWorkspacePolicy: stripPortableProjectExecutionWorkspaceRefs(manifestProject.executionWorkspacePolicy), }; + let projectId: string | null = null; if (planProject.action === "update" && planProject.existingProjectId) { const updated = await projects.update(planProject.existingProjectId, projectPatch); if (!updated) { warnings.push(`Skipped update for missing project ${planProject.existingProjectId}.`); + resultProjects.push({ + slug: planProject.slug, + id: null, + action: "skipped", + name: planProject.plannedName, + reason: "Existing target project not found.", + }); continue; } + projectId = updated.id; importedSlugToProjectId.set(planProject.slug, updated.id); existingProjectSlugToId.set(updated.urlKey, updated.id); - continue; + resultProjects.push({ + slug: planProject.slug, + id: updated.id, + action: "updated", + name: updated.name, + reason: planProject.reason, + }); + } else { + const created = await projects.create(targetCompany.id, projectPatch); + projectId = created.id; + importedSlugToProjectId.set(planProject.slug, created.id); + existingProjectSlugToId.set(created.urlKey, created.id); + resultProjects.push({ + slug: planProject.slug, + id: created.id, + action: "created", + name: created.name, + reason: planProject.reason, + }); } - const created = await projects.create(targetCompany.id, projectPatch); - importedSlugToProjectId.set(planProject.slug, created.id); - existingProjectSlugToId.set(created.urlKey, created.id); + if (!projectId) continue; + + for (const workspace of manifestProject.workspaces) { + const createdWorkspace = await projects.createWorkspace(projectId, { + name: workspace.name, + sourceType: workspace.sourceType ?? undefined, + repoUrl: workspace.repoUrl ?? undefined, + repoRef: workspace.repoRef ?? undefined, + defaultRef: workspace.defaultRef ?? undefined, + visibility: workspace.visibility ?? undefined, + setupCommand: workspace.setupCommand ?? undefined, + cleanupCommand: workspace.cleanupCommand ?? undefined, + metadata: workspace.metadata ?? undefined, + isPrimary: workspace.isPrimary, + }); + if (!createdWorkspace) { + warnings.push(`Project ${planProject.slug} workspace ${workspace.key} could not be created during import.`); + continue; + } + projectWorkspaceIdByKey.set(workspace.key, createdWorkspace.id); + } + importedProjectWorkspaceIdByProjectSlug.set(planProject.slug, projectWorkspaceIdByKey); + + const hydratedProjectExecutionWorkspacePolicy = importPortableProjectExecutionWorkspacePolicy( + planProject.slug, + manifestProject.executionWorkspacePolicy, + projectWorkspaceIdByKey, + warnings, + ); + if (hydratedProjectExecutionWorkspacePolicy) { + await projects.update(projectId, { + executionWorkspacePolicy: hydratedProjectExecutionWorkspacePolicy, + }); + } } } if (include.issues) { + const routines = routineService(db); for (const manifestIssue of sourceManifest.issues) { const markdownRaw = readPortableTextFile(plan.source.files, manifestIssue.path); const parsed = markdownRaw ? parseFrontmatterMarkdown(markdownRaw) : null; @@ -3223,8 +4119,95 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { ?? existingProjectSlugToId.get(manifestIssue.projectSlug) ?? null : null; + const projectWorkspaceId = manifestIssue.projectSlug && manifestIssue.projectWorkspaceKey + ? importedProjectWorkspaceIdByProjectSlug.get(manifestIssue.projectSlug)?.get(manifestIssue.projectWorkspaceKey) ?? null + : null; + if (manifestIssue.projectWorkspaceKey && !projectWorkspaceId) { + warnings.push(`Task ${manifestIssue.slug} references workspace key ${manifestIssue.projectWorkspaceKey}, but that workspace was not imported.`); + } + if (manifestIssue.recurring) { + if (!projectId || !assigneeAgentId) { + throw unprocessable(`Recurring task ${manifestIssue.slug} is missing the project or assignee required to create a routine.`); + } + const resolvedRoutine = resolvePortableRoutineDefinition(manifestIssue, parsed?.frontmatter.schedule); + if (resolvedRoutine.errors.length > 0) { + throw unprocessable(`Recurring task ${manifestIssue.slug} could not be imported as a routine: ${resolvedRoutine.errors.join("; ")}`); + } + warnings.push(...resolvedRoutine.warnings); + const routineDefinition = resolvedRoutine.routine ?? { + concurrencyPolicy: null, + catchUpPolicy: null, + triggers: [], + }; + const createdRoutine = await routines.create(targetCompany.id, { + projectId, + goalId: null, + parentIssueId: null, + title: manifestIssue.title, + description, + assigneeAgentId, + priority: manifestIssue.priority && ISSUE_PRIORITIES.includes(manifestIssue.priority as any) + ? manifestIssue.priority as typeof ISSUE_PRIORITIES[number] + : "medium", + status: manifestIssue.status && ROUTINE_STATUSES.includes(manifestIssue.status as any) + ? manifestIssue.status as typeof ROUTINE_STATUSES[number] + : "active", + concurrencyPolicy: + routineDefinition.concurrencyPolicy && ROUTINE_CONCURRENCY_POLICIES.includes(routineDefinition.concurrencyPolicy as any) + ? routineDefinition.concurrencyPolicy as typeof ROUTINE_CONCURRENCY_POLICIES[number] + : "coalesce_if_active", + catchUpPolicy: + routineDefinition.catchUpPolicy && ROUTINE_CATCH_UP_POLICIES.includes(routineDefinition.catchUpPolicy as any) + ? routineDefinition.catchUpPolicy as typeof ROUTINE_CATCH_UP_POLICIES[number] + : "skip_missed", + }, { + agentId: null, + userId: actorUserId ?? null, + }); + for (const trigger of routineDefinition.triggers) { + if (trigger.kind === "schedule") { + await routines.createTrigger(createdRoutine.id, { + kind: "schedule", + label: trigger.label, + enabled: trigger.enabled, + cronExpression: trigger.cronExpression!, + timezone: trigger.timezone!, + }, { + agentId: null, + userId: actorUserId ?? null, + }); + continue; + } + if (trigger.kind === "webhook") { + await routines.createTrigger(createdRoutine.id, { + kind: "webhook", + label: trigger.label, + enabled: trigger.enabled, + signingMode: + trigger.signingMode && ROUTINE_TRIGGER_SIGNING_MODES.includes(trigger.signingMode as any) + ? trigger.signingMode as typeof ROUTINE_TRIGGER_SIGNING_MODES[number] + : "bearer", + replayWindowSec: trigger.replayWindowSec ?? 300, + }, { + agentId: null, + userId: actorUserId ?? null, + }); + continue; + } + await routines.createTrigger(createdRoutine.id, { + kind: "api", + label: trigger.label, + enabled: trigger.enabled, + }, { + agentId: null, + userId: actorUserId ?? null, + }); + } + continue; + } await issues.create(targetCompany.id, { projectId, + projectWorkspaceId, title: manifestIssue.title, description, assigneeAgentId, @@ -3239,9 +4222,6 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { executionWorkspaceSettings: manifestIssue.executionWorkspaceSettings, labelIds: [], }); - if (manifestIssue.recurrence) { - warnings.push(`Imported task ${manifestIssue.slug} as a one-time issue; recurrence metadata was not activated.`); - } } } @@ -3252,6 +4232,7 @@ export function companyPortabilityService(db: Db, storage?: StorageService) { action: companyAction, }, agents: resultAgents, + projects: resultProjects, envInputs: sourceManifest.envInputs ?? [], warnings, }; diff --git a/server/src/services/company-skills.ts b/server/src/services/company-skills.ts index 19aeab0458..2b97da208c 100644 --- a/server/src/services/company-skills.ts +++ b/server/src/services/company-skills.ts @@ -99,6 +99,8 @@ type RuntimeSkillEntryOptions = { materializeMissing?: boolean; }; +const skillInventoryRefreshPromises = new Map>(); + const PROJECT_SCAN_DIRECTORY_ROOTS = [ "skills", "skills/.curated", @@ -188,6 +190,18 @@ function normalizeSkillKey(value: string | null | undefined) { return segments.length > 0 ? segments.join("/") : null; } +export function normalizeGitHubSkillDirectory( + value: string | null | undefined, + fallback: string, +) { + const normalized = normalizePortablePath(value ?? ""); + if (!normalized) return normalizePortablePath(fallback); + if (path.posix.basename(normalized).toLowerCase() === "skill.md") { + return normalizePortablePath(path.posix.dirname(normalized)); + } + return normalized; +} + function hashSkillValue(value: string) { return createHash("sha256").update(value).digest("hex").slice(0, 10); } @@ -1017,7 +1031,10 @@ async function readUrlSkillImports( repo: parsed.repo, ref: ref, trackingRef, - repoSkillDir: basePrefix ? `${basePrefix}${skillDir}` : skillDir, + repoSkillDir: normalizeGitHubSkillDirectory( + basePrefix ? `${basePrefix}${skillDir}` : skillDir, + slug, + ), }; const inventory = filteredPaths .filter((entry) => entry === relativeSkillPath || entry.startsWith(`${skillDir}/`)) @@ -1474,8 +1491,25 @@ export function companySkillService(db: Db) { } async function ensureSkillInventoryCurrent(companyId: string) { - await ensureBundledSkills(companyId); - await pruneMissingLocalPathSkills(companyId); + const existingRefresh = skillInventoryRefreshPromises.get(companyId); + if (existingRefresh) { + await existingRefresh; + return; + } + + const refreshPromise = (async () => { + await ensureBundledSkills(companyId); + await pruneMissingLocalPathSkills(companyId); + })(); + + skillInventoryRefreshPromises.set(companyId, refreshPromise); + try { + await refreshPromise; + } finally { + if (skillInventoryRefreshPromises.get(companyId) === refreshPromise) { + skillInventoryRefreshPromises.delete(companyId); + } + } } async function list(companyId: string): Promise { @@ -1646,7 +1680,7 @@ export function companySkillService(db: Db) { const owner = asString(metadata.owner); const repo = asString(metadata.repo); const ref = skill.sourceRef ?? asString(metadata.ref) ?? "main"; - const repoSkillDir = normalizePortablePath(asString(metadata.repoSkillDir) ?? skill.slug); + const repoSkillDir = normalizeGitHubSkillDirectory(asString(metadata.repoSkillDir), skill.slug); if (!owner || !repo) { throw unprocessable("Skill source metadata is incomplete."); } diff --git a/server/src/services/execution-workspace-policy.ts b/server/src/services/execution-workspace-policy.ts index 53487324e5..bb5ef76df2 100644 --- a/server/src/services/execution-workspace-policy.ts +++ b/server/src/services/execution-workspace-policy.ts @@ -132,6 +132,21 @@ export function defaultIssueExecutionWorkspaceSettingsForProject( }; } +export function issueExecutionWorkspaceModeForPersistedWorkspace( + mode: string | null | undefined, +): IssueExecutionWorkspaceSettings["mode"] { + if (mode === null || mode === undefined) { + return "agent_default"; + } + if (mode === "isolated_workspace" || mode === "operator_branch" || mode === "shared_workspace") { + return mode; + } + if (mode === "adapter_managed" || mode === "cloud_sandbox") { + return "agent_default"; + } + return "shared_workspace"; +} + export function resolveExecutionWorkspaceMode(input: { projectPolicy: ProjectExecutionWorkspacePolicy | null; issueSettings: IssueExecutionWorkspaceSettings | null; diff --git a/server/src/services/heartbeat.ts b/server/src/services/heartbeat.ts index 0694efed98..c909b9b77c 100644 --- a/server/src/services/heartbeat.ts +++ b/server/src/services/heartbeat.ts @@ -45,6 +45,7 @@ import { workspaceOperationService } from "./workspace-operations.js"; import { buildExecutionWorkspaceAdapterConfig, gateProjectExecutionWorkspacePolicy, + issueExecutionWorkspaceModeForPersistedWorkspace, parseIssueExecutionWorkspaceSettings, parseProjectExecutionWorkspacePolicy, resolveExecutionWorkspaceMode, @@ -325,6 +326,51 @@ async function resolveLedgerScopeForRun( }; } +type ResumeSessionRow = { + sessionParamsJson: Record | null; + sessionDisplayId: string | null; + lastRunId: string | null; +}; + +export function buildExplicitResumeSessionOverride(input: { + resumeFromRunId: string; + resumeRunSessionIdBefore: string | null; + resumeRunSessionIdAfter: string | null; + taskSession: ResumeSessionRow | null; + sessionCodec: AdapterSessionCodec; +}) { + const desiredDisplayId = truncateDisplayId( + input.resumeRunSessionIdAfter ?? input.resumeRunSessionIdBefore, + ); + const taskSessionParams = normalizeSessionParams( + input.sessionCodec.deserialize(input.taskSession?.sessionParamsJson ?? null), + ); + const taskSessionDisplayId = truncateDisplayId( + input.taskSession?.sessionDisplayId ?? + (input.sessionCodec.getDisplayId ? input.sessionCodec.getDisplayId(taskSessionParams) : null) ?? + readNonEmptyString(taskSessionParams?.sessionId), + ); + const canReuseTaskSessionParams = + input.taskSession != null && + ( + input.taskSession.lastRunId === input.resumeFromRunId || + (!!desiredDisplayId && taskSessionDisplayId === desiredDisplayId) + ); + const sessionParams = + canReuseTaskSessionParams + ? taskSessionParams + : desiredDisplayId + ? { sessionId: desiredDisplayId } + : null; + const sessionDisplayId = desiredDisplayId ?? (canReuseTaskSessionParams ? taskSessionDisplayId : null); + + if (!sessionDisplayId && !sessionParams) return null; + return { + sessionDisplayId, + sessionParams, + }; +} + function normalizeUsageTotals(usage: UsageSummary | null | undefined): UsageTotals | null { if (!usage) return null; return { @@ -977,6 +1023,57 @@ export function heartbeatService(db: Db) { return runtimeForRun?.sessionId ?? null; } + async function resolveExplicitResumeSessionOverride( + agent: typeof agents.$inferSelect, + payload: Record | null, + taskKey: string | null, + ) { + const resumeFromRunId = readNonEmptyString(payload?.resumeFromRunId); + if (!resumeFromRunId) return null; + + const resumeRun = await db + .select({ + id: heartbeatRuns.id, + contextSnapshot: heartbeatRuns.contextSnapshot, + sessionIdBefore: heartbeatRuns.sessionIdBefore, + sessionIdAfter: heartbeatRuns.sessionIdAfter, + }) + .from(heartbeatRuns) + .where( + and( + eq(heartbeatRuns.id, resumeFromRunId), + eq(heartbeatRuns.companyId, agent.companyId), + eq(heartbeatRuns.agentId, agent.id), + ), + ) + .then((rows) => rows[0] ?? null); + if (!resumeRun) return null; + + const resumeContext = parseObject(resumeRun.contextSnapshot); + const resumeTaskKey = deriveTaskKey(resumeContext, null) ?? taskKey; + const resumeTaskSession = resumeTaskKey + ? await getTaskSession(agent.companyId, agent.id, agent.adapterType, resumeTaskKey) + : null; + const sessionCodec = getAdapterSessionCodec(agent.adapterType); + const sessionOverride = buildExplicitResumeSessionOverride({ + resumeFromRunId, + resumeRunSessionIdBefore: resumeRun.sessionIdBefore, + resumeRunSessionIdAfter: resumeRun.sessionIdAfter, + taskSession: resumeTaskSession, + sessionCodec, + }); + if (!sessionOverride) return null; + + return { + resumeFromRunId, + taskKey: resumeTaskKey, + issueId: readNonEmptyString(resumeContext.issueId), + taskId: readNonEmptyString(resumeContext.taskId) ?? readNonEmptyString(resumeContext.issueId), + sessionDisplayId: sessionOverride.sessionDisplayId, + sessionParams: sessionOverride.sessionParams, + }; + } + async function resolveWorkspaceForRun( agent: typeof agents.$inferSelect, context: Record, @@ -1920,9 +2017,18 @@ export function heartbeatService(db: Db) { const resetTaskSession = shouldResetTaskSessionForWake(context); const sessionResetReason = describeSessionResetReason(context); const taskSessionForRun = resetTaskSession ? null : taskSession; - const previousSessionParams = normalizeSessionParams( - sessionCodec.deserialize(taskSessionForRun?.sessionParamsJson ?? null), + const explicitResumeSessionParams = normalizeSessionParams( + sessionCodec.deserialize(parseObject(context.resumeSessionParams)), + ); + const explicitResumeSessionDisplayId = truncateDisplayId( + readNonEmptyString(context.resumeSessionDisplayId) ?? + (sessionCodec.getDisplayId ? sessionCodec.getDisplayId(explicitResumeSessionParams) : null) ?? + readNonEmptyString(explicitResumeSessionParams?.sessionId), ); + const previousSessionParams = + explicitResumeSessionParams ?? + (explicitResumeSessionDisplayId ? { sessionId: explicitResumeSessionDisplayId } : null) ?? + normalizeSessionParams(sessionCodec.deserialize(taskSessionForRun?.sessionParamsJson ?? null)); const config = parseObject(agent.adapterConfig); const executionWorkspaceMode = resolveExecutionWorkspaceMode({ projectPolicy: projectExecutionWorkspacePolicy, @@ -2098,11 +2204,29 @@ export function heartbeatService(db: Db) { cleanupReason: null, }); } - if (issueId && persistedExecutionWorkspace && issueRef?.executionWorkspaceId !== persistedExecutionWorkspace.id) { - await issuesSvc.update(issueId, { - executionWorkspaceId: persistedExecutionWorkspace.id, - ...(resolvedProjectWorkspaceId ? { projectWorkspaceId: resolvedProjectWorkspaceId } : {}), - }); + if (issueId && persistedExecutionWorkspace) { + const nextIssueWorkspaceMode = issueExecutionWorkspaceModeForPersistedWorkspace(persistedExecutionWorkspace.mode); + const shouldSwitchIssueToExistingWorkspace = + issueRef?.executionWorkspacePreference === "reuse_existing" || + executionWorkspaceMode === "isolated_workspace" || + executionWorkspaceMode === "operator_branch"; + const nextIssuePatch: Record = {}; + if (issueRef?.executionWorkspaceId !== persistedExecutionWorkspace.id) { + nextIssuePatch.executionWorkspaceId = persistedExecutionWorkspace.id; + } + if (resolvedProjectWorkspaceId && issueRef?.projectWorkspaceId !== resolvedProjectWorkspaceId) { + nextIssuePatch.projectWorkspaceId = resolvedProjectWorkspaceId; + } + if (shouldSwitchIssueToExistingWorkspace) { + nextIssuePatch.executionWorkspacePreference = "reuse_existing"; + nextIssuePatch.executionWorkspaceSettings = { + ...(issueExecutionWorkspaceSettings ?? {}), + mode: nextIssueWorkspaceMode, + }; + } + if (Object.keys(nextIssuePatch).length > 0) { + await issuesSvc.update(issueId, nextIssuePatch); + } } if (persistedExecutionWorkspace) { context.executionWorkspaceId = persistedExecutionWorkspace.id; @@ -2171,7 +2295,8 @@ export function heartbeatService(db: Db) { } const runtimeSessionFallback = taskKey || resetTaskSession ? null : runtime.sessionId; let previousSessionDisplayId = truncateDisplayId( - taskSessionForRun?.sessionDisplayId ?? + explicitResumeSessionDisplayId ?? + taskSessionForRun?.sessionDisplayId ?? (sessionCodec.getDisplayId ? sessionCodec.getDisplayId(runtimeSessionParams) : null) ?? readNonEmptyString(runtimeSessionParams?.sessionId) ?? runtimeSessionFallback, @@ -2782,7 +2907,9 @@ export function heartbeatService(db: Db) { payload: promotedPayload, }); - const sessionBefore = await resolveSessionBeforeForWakeup(deferredAgent, promotedTaskKey); + const sessionBefore = + readNonEmptyString(promotedContextSnapshot.resumeSessionDisplayId) ?? + await resolveSessionBeforeForWakeup(deferredAgent, promotedTaskKey); const now = new Date(); const newRun = await tx .insert(heartbeatRuns) @@ -2861,10 +2988,30 @@ export function heartbeatService(db: Db) { triggerDetail, payload, }); - const issueId = readNonEmptyString(enrichedContextSnapshot.issueId) ?? issueIdFromPayload; + let issueId = readNonEmptyString(enrichedContextSnapshot.issueId) ?? issueIdFromPayload; const agent = await getAgent(agentId); if (!agent) throw notFound("Agent not found"); + const explicitResumeSession = await resolveExplicitResumeSessionOverride(agent, payload, taskKey); + if (explicitResumeSession) { + enrichedContextSnapshot.resumeFromRunId = explicitResumeSession.resumeFromRunId; + enrichedContextSnapshot.resumeSessionDisplayId = explicitResumeSession.sessionDisplayId; + enrichedContextSnapshot.resumeSessionParams = explicitResumeSession.sessionParams; + if (!readNonEmptyString(enrichedContextSnapshot.issueId) && explicitResumeSession.issueId) { + enrichedContextSnapshot.issueId = explicitResumeSession.issueId; + } + if (!readNonEmptyString(enrichedContextSnapshot.taskId) && explicitResumeSession.taskId) { + enrichedContextSnapshot.taskId = explicitResumeSession.taskId; + } + if (!readNonEmptyString(enrichedContextSnapshot.taskKey) && explicitResumeSession.taskKey) { + enrichedContextSnapshot.taskKey = explicitResumeSession.taskKey; + } + issueId = readNonEmptyString(enrichedContextSnapshot.issueId) ?? issueId; + } + const effectiveTaskKey = readNonEmptyString(enrichedContextSnapshot.taskKey) ?? taskKey; + const sessionBefore = + explicitResumeSession?.sessionDisplayId ?? + await resolveSessionBeforeForWakeup(agent, effectiveTaskKey); const writeSkippedRequest = async (skipReason: string) => { await db.insert(agentWakeupRequests).values({ @@ -2928,7 +3075,6 @@ export function heartbeatService(db: Db) { if (issueId && !bypassIssueExecutionLock) { const agentNameKey = normalizeAgentNameKey(agent.name); - const sessionBefore = await resolveSessionBeforeForWakeup(agent, taskKey); const outcome = await db.transaction(async (tx) => { await tx.execute( @@ -3279,8 +3425,6 @@ export function heartbeatService(db: Db) { .returning() .then((rows) => rows[0]); - const sessionBefore = await resolveSessionBeforeForWakeup(agent, taskKey); - const newRun = await db .insert(heartbeatRuns) .values({ diff --git a/server/src/services/index.ts b/server/src/services/index.ts index d6c5f9050a..fccd6c7ff7 100644 --- a/server/src/services/index.ts +++ b/server/src/services/index.ts @@ -19,6 +19,7 @@ export { heartbeatService } from "./heartbeat.js"; export { dashboardService } from "./dashboard.js"; export { sidebarBadgeService } from "./sidebar-badges.js"; export { accessService } from "./access.js"; +export { boardAuthService } from "./board-auth.js"; export { instanceSettingsService } from "./instance-settings.js"; export { companyPortabilityService } from "./company-portability.js"; export { executionWorkspaceService } from "./execution-workspaces.js"; diff --git a/server/src/services/issue-goal-fallback.ts b/server/src/services/issue-goal-fallback.ts index fe48f0a12a..91693d54bb 100644 --- a/server/src/services/issue-goal-fallback.ts +++ b/server/src/services/issue-goal-fallback.ts @@ -3,28 +3,54 @@ type MaybeId = string | null | undefined; export function resolveIssueGoalId(input: { projectId: MaybeId; goalId: MaybeId; + projectGoalId?: MaybeId; defaultGoalId: MaybeId; }): string | null { - if (!input.projectId && !input.goalId) { - return input.defaultGoalId ?? null; - } - return input.goalId ?? null; + if (input.goalId) return input.goalId; + if (input.projectId) return input.projectGoalId ?? null; + return input.defaultGoalId ?? null; } export function resolveNextIssueGoalId(input: { currentProjectId: MaybeId; currentGoalId: MaybeId; + currentProjectGoalId?: MaybeId; projectId?: MaybeId; goalId?: MaybeId; + projectGoalId?: MaybeId; defaultGoalId: MaybeId; }): string | null { const projectId = input.projectId !== undefined ? input.projectId : input.currentProjectId; - const goalId = - input.goalId !== undefined ? input.goalId : input.currentGoalId; + const projectGoalId = + input.projectGoalId !== undefined + ? input.projectGoalId + : projectId + ? input.currentProjectGoalId + : null; - if (!projectId && !goalId) { + const resolveFallbackGoalId = (targetProjectId: MaybeId, targetProjectGoalId: MaybeId) => { + if (targetProjectId) return targetProjectGoalId ?? null; return input.defaultGoalId ?? null; + }; + + if (input.goalId !== undefined) { + return input.goalId ?? resolveFallbackGoalId(projectId, projectGoalId); } - return goalId ?? null; + + const currentFallbackGoalId = resolveFallbackGoalId( + input.currentProjectId, + input.currentProjectGoalId, + ); + const nextFallbackGoalId = resolveFallbackGoalId(projectId, projectGoalId); + + if (!input.currentGoalId) { + return nextFallbackGoalId; + } + + if (input.currentGoalId === currentFallbackGoalId) { + return nextFallbackGoalId; + } + + return input.currentGoalId; } diff --git a/server/src/services/issues.ts b/server/src/services/issues.ts index 681da27d70..a728cfe022 100644 --- a/server/src/services/issues.ts +++ b/server/src/services/issues.ts @@ -1,6 +1,7 @@ import { and, asc, desc, eq, inArray, isNull, ne, or, sql } from "drizzle-orm"; import type { Db } from "@paperclipai/db"; import { + activityLog, agents, assets, companies, @@ -19,7 +20,7 @@ import { projectWorkspaces, projects, } from "@paperclipai/db"; -import { extractProjectMentionIds } from "@paperclipai/shared"; +import { extractAgentMentionIds, extractProjectMentionIds } from "@paperclipai/shared"; import { conflict, notFound, unprocessable } from "../errors.js"; import { defaultIssueExecutionWorkspaceSettingsForProject, @@ -62,6 +63,7 @@ function applyStatusSideEffects( export interface IssueFilters { status?: string; assigneeAgentId?: string; + participantAgentId?: string; assigneeUserId?: string; touchedByUserId?: string; unreadForUserId?: string; @@ -99,6 +101,7 @@ type IssueUserContextInput = { createdAt: Date | string; updatedAt: Date | string; }; +type ProjectGoalReader = Pick; function sameRunLock(checkoutRunId: string | null, actorRunId: string | null) { if (actorRunId) return checkoutRunId === actorRunId; @@ -111,6 +114,20 @@ function escapeLikePattern(value: string): string { return value.replace(/[\\%_]/g, "\\$&"); } +async function getProjectDefaultGoalId( + db: ProjectGoalReader, + companyId: string, + projectId: string | null | undefined, +) { + if (!projectId) return null; + const row = await db + .select({ goalId: projects.goalId }) + .from(projects) + .where(and(eq(projects.id, projectId), eq(projects.companyId, companyId))) + .then((rows) => rows[0] ?? null); + return row?.goalId ?? null; +} + function touchedByUserCondition(companyId: string, userId: string) { return sql` ( @@ -134,6 +151,30 @@ function touchedByUserCondition(companyId: string, userId: string) { `; } +function participatedByAgentCondition(companyId: string, agentId: string) { + return sql` + ( + ${issues.createdByAgentId} = ${agentId} + OR ${issues.assigneeAgentId} = ${agentId} + OR EXISTS ( + SELECT 1 + FROM ${issueComments} + WHERE ${issueComments.issueId} = ${issues.id} + AND ${issueComments.companyId} = ${companyId} + AND ${issueComments.authorAgentId} = ${agentId} + ) + OR EXISTS ( + SELECT 1 + FROM ${activityLog} + WHERE ${activityLog.companyId} = ${companyId} + AND ${activityLog.entityType} = 'issue' + AND ${activityLog.entityId} = ${issues.id}::text + AND ${activityLog.agentId} = ${agentId} + ) + ) + `; +} + function myLastCommentAtExpr(companyId: string, userId: string) { return sql` ( @@ -192,6 +233,41 @@ function unreadForUserCondition(companyId: string, userId: string) { `; } +/** Named entities commonly emitted in saved issue bodies; unknown `&name;` sequences are left unchanged. */ +const WELL_KNOWN_NAMED_HTML_ENTITIES: Readonly> = { + amp: "&", + apos: "'", + copy: "\u00A9", + gt: ">", + lt: "<", + nbsp: "\u00A0", + quot: '"', + ensp: "\u2002", + emsp: "\u2003", + thinsp: "\u2009", +}; + +function decodeNumericHtmlEntity(digits: string, radix: 16 | 10): string | null { + const n = Number.parseInt(digits, radix); + if (Number.isNaN(n) || n < 0 || n > 0x10ffff) return null; + try { + return String.fromCodePoint(n); + } catch { + return null; + } +} + +/** Decodes HTML character references in a raw @mention capture so UI-encoded bodies match agent names. */ +export function normalizeAgentMentionToken(raw: string): string { + let s = raw.replace(/&#x([0-9a-fA-F]+);/gi, (full, hex: string) => decodeNumericHtmlEntity(hex, 16) ?? full); + s = s.replace(/&#([0-9]+);/g, (full, dec: string) => decodeNumericHtmlEntity(dec, 10) ?? full); + s = s.replace(/&([a-z][a-z0-9]*);/gi, (full, name: string) => { + const decoded = WELL_KNOWN_NAMED_HTML_ENTITIES[name.toLowerCase()]; + return decoded !== undefined ? decoded : full; + }); + return s.trim(); +} + export function deriveIssueUserContext( issue: IssueUserContextInput, userId: string, @@ -508,6 +584,9 @@ export function issueService(db: Db) { if (filters?.assigneeAgentId) { conditions.push(eq(issues.assigneeAgentId, filters.assigneeAgentId)); } + if (filters?.participantAgentId) { + conditions.push(participatedByAgentCondition(companyId, filters.participantAgentId)); + } if (filters?.assigneeUserId) { conditions.push(eq(issues.assigneeUserId, filters.assigneeUserId)); } @@ -715,6 +794,7 @@ export function issueService(db: Db) { } return db.transaction(async (tx) => { const defaultCompanyGoal = await getDefaultCompanyGoal(tx, companyId); + const projectGoalId = await getProjectDefaultGoalId(tx, companyId, issueData.projectId); let executionWorkspaceSettings = (issueData.executionWorkspaceSettings as Record | null | undefined) ?? null; if (executionWorkspaceSettings == null && issueData.projectId) { @@ -766,6 +846,7 @@ export function issueService(db: Db) { goalId: resolveIssueGoalId({ projectId: issueData.projectId, goalId: issueData.goalId, + projectGoalId, defaultGoalId: defaultCompanyGoal?.id ?? null, }), ...(projectWorkspaceId ? { projectWorkspaceId } : {}), @@ -866,11 +947,21 @@ export function issueService(db: Db) { return db.transaction(async (tx) => { const defaultCompanyGoal = await getDefaultCompanyGoal(tx, existing.companyId); + const [currentProjectGoalId, nextProjectGoalId] = await Promise.all([ + getProjectDefaultGoalId(tx, existing.companyId, existing.projectId), + getProjectDefaultGoalId( + tx, + existing.companyId, + issueData.projectId !== undefined ? issueData.projectId : existing.projectId, + ), + ]); patch.goalId = resolveNextIssueGoalId({ currentProjectId: existing.projectId, currentGoalId: existing.goalId, + currentProjectGoalId, projectId: issueData.projectId, goalId: issueData.goalId, + projectGoalId: nextProjectGoalId, defaultGoalId: defaultCompanyGoal?.id ?? null, }); const updated = await tx @@ -1461,11 +1552,22 @@ export function issueService(db: Db) { const re = /\B@([^\s@,!?.]+)/g; const tokens = new Set(); let m: RegExpExecArray | null; - while ((m = re.exec(body)) !== null) tokens.add(m[1].toLowerCase()); - if (tokens.size === 0) return []; + while ((m = re.exec(body)) !== null) { + const normalized = normalizeAgentMentionToken(m[1]); + if (normalized) tokens.add(normalized.toLowerCase()); + } + + const explicitAgentMentionIds = extractAgentMentionIds(body); + if (tokens.size === 0 && explicitAgentMentionIds.length === 0) return []; const rows = await db.select({ id: agents.id, name: agents.name }) .from(agents).where(eq(agents.companyId, companyId)); - return rows.filter(a => tokens.has(a.name.toLowerCase())).map(a => a.id); + const resolved = new Set(explicitAgentMentionIds); + for (const agent of rows) { + if (tokens.has(agent.name.toLowerCase())) { + resolved.add(agent.id); + } + } + return [...resolved]; }, findMentionedProjectIds: async (issueId: string) => { diff --git a/server/src/types/express.d.ts b/server/src/types/express.d.ts index 2dac6c39c9..9f107d62a4 100644 --- a/server/src/types/express.d.ts +++ b/server/src/types/express.d.ts @@ -12,7 +12,7 @@ declare global { isInstanceAdmin?: boolean; keyId?: string; runId?: string; - source?: "local_implicit" | "session" | "hosted_proxy" | "agent_key" | "agent_jwt" | "none"; + source?: "local_implicit" | "session" | "hosted_proxy" | "board_key" | "agent_key" | "agent_jwt" | "none"; }; } } diff --git a/skills/paperclip/SKILL.md b/skills/paperclip/SKILL.md index ee5ac2aea9..407f08da8a 100644 --- a/skills/paperclip/SKILL.md +++ b/skills/paperclip/SKILL.md @@ -330,7 +330,7 @@ Use this when validating Paperclip itself (assignment flow, checkouts, run visib 1. Create a throwaway issue assigned to a known local agent (`claudecoder` or `codexcoder`): ```bash -pnpm paperclipai issue create \ +npx paperclipai issue create \ --company-id "$PAPERCLIP_COMPANY_ID" \ --title "Self-test: assignment/watch flow" \ --description "Temporary validation issue" \ @@ -341,19 +341,19 @@ pnpm paperclipai issue create \ 2. Trigger and watch a heartbeat for that assignee: ```bash -pnpm paperclipai heartbeat run --agent-id "$PAPERCLIP_AGENT_ID" +npx paperclipai heartbeat run --agent-id "$PAPERCLIP_AGENT_ID" ``` 3. Verify the issue transitions (`todo -> in_progress -> done` or `blocked`) and that comments are posted: ```bash -pnpm paperclipai issue get +npx paperclipai issue get ``` 4. Reassignment test (optional): move the same issue between `claudecoder` and `codexcoder` and confirm wake/run behavior: ```bash -pnpm paperclipai issue update --assignee-agent-id --status todo +npx paperclipai issue update --assignee-agent-id --status todo ``` 5. Cleanup: mark temporary issues done/cancelled with a clear note. diff --git a/ui/package.json b/ui/package.json index 5ce1555337..a02ddb121e 100644 --- a/ui/package.json +++ b/ui/package.json @@ -13,14 +13,16 @@ "@dnd-kit/core": "^6.3.1", "@dnd-kit/sortable": "^10.0.0", "@dnd-kit/utilities": "^3.2.2", + "@lexical/link": "0.35.0", + "lexical": "0.35.0", "@mdxeditor/editor": "^3.52.4", "@paperclipai/adapter-claude-local": "workspace:*", "@paperclipai/adapter-codex-local": "workspace:*", "@paperclipai/adapter-cursor-local": "workspace:*", "@paperclipai/adapter-gemini-local": "workspace:*", + "@paperclipai/adapter-openclaw-gateway": "workspace:*", "@paperclipai/adapter-opencode-local": "workspace:*", "@paperclipai/adapter-pi-local": "workspace:*", - "@paperclipai/adapter-openclaw-gateway": "workspace:*", "@paperclipai/adapter-utils": "workspace:*", "@paperclipai/shared": "workspace:*", "@radix-ui/react-slot": "^1.2.4", diff --git a/ui/src/App.tsx b/ui/src/App.tsx index 4af984c9ce..ec4676f83c 100644 --- a/ui/src/App.tsx +++ b/ui/src/App.tsx @@ -40,6 +40,7 @@ import { OrgChart } from "./pages/OrgChart"; import { NewAgent } from "./pages/NewAgent"; import { AuthPage } from "./pages/Auth"; import { BoardClaimPage } from "./pages/BoardClaim"; +import { CliAuthPage } from "./pages/CliAuth"; import { InviteLandingPage } from "./pages/InviteLanding"; import { NotFoundPage } from "./pages/NotFound"; import { queryKeys } from "./lib/queryKeys"; @@ -360,6 +361,7 @@ export function App() { } /> } /> + } /> } /> }> diff --git a/ui/src/adapters/codex-local/config-fields.tsx b/ui/src/adapters/codex-local/config-fields.tsx index 125630bae3..86bef6009b 100644 --- a/ui/src/adapters/codex-local/config-fields.tsx +++ b/ui/src/adapters/codex-local/config-fields.tsx @@ -11,7 +11,7 @@ import { LocalWorkspaceRuntimeFields } from "../local-workspace-runtime-fields"; const inputClass = "w-full rounded-md border border-border px-2.5 py-1.5 bg-transparent outline-none text-sm font-mono placeholder:text-muted-foreground/40"; const instructionsFileHint = - "Absolute path to a markdown file (e.g. AGENTS.md) that defines this agent's behavior. Injected into the system prompt at runtime."; + "Absolute path to a markdown file (e.g. AGENTS.md) that defines this agent's behavior. Injected into the system prompt at runtime. Note: Codex may still auto-apply repo-scoped AGENTS.md files from the workspace."; export function CodexLocalConfigFields({ mode, diff --git a/ui/src/api/access.ts b/ui/src/api/access.ts index ce565f6d25..90afd1dd68 100644 --- a/ui/src/api/access.ts +++ b/ui/src/api/access.ts @@ -64,6 +64,23 @@ type BoardClaimStatus = { claimedByUserId: string | null; }; +type CliAuthChallengeStatus = { + id: string; + status: "pending" | "approved" | "cancelled" | "expired"; + command: string; + clientName: string | null; + requestedAccess: "board" | "instance_admin_required"; + requestedCompanyId: string | null; + requestedCompanyName: string | null; + approvedAt: string | null; + cancelledAt: string | null; + expiresAt: string; + approvedByUser: { id: string; name: string; email: string } | null; + requiresSignIn: boolean; + canApprove: boolean; + currentUserId: string | null; +}; + type CompanyInviteCreated = { id: string; token: string; @@ -127,4 +144,16 @@ export const accessApi = { claimBoard: (token: string, code: string) => api.post<{ claimed: true; userId: string }>(`/board-claim/${token}/claim`, { code }), + + getCliAuthChallenge: (id: string, token: string) => + api.get(`/cli-auth/challenges/${id}?token=${encodeURIComponent(token)}`), + + approveCliAuthChallenge: (id: string, token: string) => + api.post<{ approved: boolean; status: string; userId: string; keyId: string | null; expiresAt: string }>( + `/cli-auth/challenges/${id}/approve`, + { token }, + ), + + cancelCliAuthChallenge: (id: string, token: string) => + api.post<{ cancelled: boolean; status: string }>(`/cli-auth/challenges/${id}/cancel`, { token }), }; diff --git a/ui/src/api/companies.ts b/ui/src/api/companies.ts index 60a41742c2..82d2e54e8b 100644 --- a/ui/src/api/companies.ts +++ b/ui/src/api/companies.ts @@ -1,5 +1,6 @@ import type { Company, + CompanyPortabilityExportRequest, CompanyPortabilityExportPreviewResult, CompanyPortabilityExportResult, CompanyPortabilityImportRequest, @@ -37,41 +38,17 @@ export const companiesApi = { remove: (companyId: string) => api.delete<{ ok: true }>(`/companies/${companyId}`), exportBundle: ( companyId: string, - data: { - include?: { company?: boolean; agents?: boolean; projects?: boolean; issues?: boolean }; - agents?: string[]; - skills?: string[]; - projects?: string[]; - issues?: string[]; - projectIssues?: string[]; - selectedFiles?: string[]; - }, + data: CompanyPortabilityExportRequest, ) => api.post(`/companies/${companyId}/export`, data), exportPreview: ( companyId: string, - data: { - include?: { company?: boolean; agents?: boolean; projects?: boolean; issues?: boolean }; - agents?: string[]; - skills?: string[]; - projects?: string[]; - issues?: string[]; - projectIssues?: string[]; - selectedFiles?: string[]; - }, + data: CompanyPortabilityExportRequest, ) => api.post(`/companies/${companyId}/exports/preview`, data), exportPackage: ( companyId: string, - data: { - include?: { company?: boolean; agents?: boolean; projects?: boolean; issues?: boolean }; - agents?: string[]; - skills?: string[]; - projects?: string[]; - issues?: string[]; - projectIssues?: string[]; - selectedFiles?: string[]; - }, + data: CompanyPortabilityExportRequest, ) => api.post(`/companies/${companyId}/exports`, data), importPreview: (data: CompanyPortabilityPreviewRequest) => diff --git a/ui/src/api/issues.ts b/ui/src/api/issues.ts index 308028b363..62cb347cfe 100644 --- a/ui/src/api/issues.ts +++ b/ui/src/api/issues.ts @@ -18,6 +18,7 @@ export const issuesApi = { status?: string; projectId?: string; assigneeAgentId?: string; + participantAgentId?: string; assigneeUserId?: string; touchedByUserId?: string; unreadForUserId?: string; @@ -32,6 +33,7 @@ export const issuesApi = { if (filters?.status) params.set("status", filters.status); if (filters?.projectId) params.set("projectId", filters.projectId); if (filters?.assigneeAgentId) params.set("assigneeAgentId", filters.assigneeAgentId); + if (filters?.participantAgentId) params.set("participantAgentId", filters.participantAgentId); if (filters?.assigneeUserId) params.set("assigneeUserId", filters.assigneeUserId); if (filters?.touchedByUserId) params.set("touchedByUserId", filters.touchedByUserId); if (filters?.unreadForUserId) params.set("unreadForUserId", filters.unreadForUserId); diff --git a/ui/src/components/AgentConfigForm.tsx b/ui/src/components/AgentConfigForm.tsx index a2b66225e2..46587a5276 100644 --- a/ui/src/components/AgentConfigForm.tsx +++ b/ui/src/components/AgentConfigForm.tsx @@ -44,6 +44,7 @@ import { ClaudeLocalAdvancedFields } from "../adapters/claude-local/config-field import { MarkdownEditor } from "./MarkdownEditor"; import { ChoosePathButton } from "./PathInstructionsModal"; import { OpenCodeLogoIcon } from "./OpenCodeLogoIcon"; +import { ReportsToPicker } from "./ReportsToPicker"; import { shouldShowLegacyWorkingDirectoryField } from "../lib/legacy-agent-config"; /* ---- Create mode values ---- */ @@ -317,6 +318,12 @@ export function AgentConfigForm(props: AgentConfigFormProps) { }); const models = fetchedModels ?? externalModels ?? []; + const { data: companyAgents = [] } = useQuery({ + queryKey: selectedCompanyId ? queryKeys.agents.list(selectedCompanyId) : ["agents", "none", "list"], + queryFn: () => agentsApi.list(selectedCompanyId!), + enabled: Boolean(!isCreate && selectedCompanyId), + }); + /** Props passed to adapter-specific config field components */ const adapterFieldProps = { mode, @@ -464,6 +471,15 @@ export function AgentConfigForm(props: AgentConfigFormProps) { placeholder="e.g. VP of Engineering" /> + + mark("identity", "reportsTo", id)} + excludeAgentIds={[props.agent.id]} + chooseLabel="Choose manager…" + /> + = { - bot: Bot, - cpu: Cpu, - brain: Brain, - zap: Zap, - rocket: Rocket, - code: Code, - terminal: Terminal, - shield: Shield, - eye: Eye, - search: Search, - wrench: Wrench, - hammer: Hammer, - lightbulb: Lightbulb, - sparkles: Sparkles, - star: Star, - heart: Heart, - flame: Flame, - bug: Bug, - cog: Cog, - database: Database, - globe: Globe, - lock: Lock, - mail: Mail, - "message-square": MessageSquare, - "file-code": FileCode, - "git-branch": GitBranch, - package: Package, - puzzle: Puzzle, - target: Target, - wand: Wand2, - atom: Atom, - "circuit-board": CircuitBoard, - radar: Radar, - swords: Swords, - telescope: Telescope, - microscope: Microscope, - crown: Crown, - gem: Gem, - hexagon: Hexagon, - pentagon: Pentagon, - fingerprint: Fingerprint, -}; +import { AGENT_ICONS, getAgentIcon } from "../lib/agent-icons"; const DEFAULT_ICON: AgentIconName = "bot"; -export function getAgentIcon(iconName: string | null | undefined): LucideIcon { - if (iconName && AGENT_ICON_NAMES.includes(iconName as AgentIconName)) { - return AGENT_ICONS[iconName as AgentIconName]; - } - return AGENT_ICONS[DEFAULT_ICON]; -} - interface AgentIconProps { icon: string | null | undefined; className?: string; diff --git a/ui/src/components/CommentThread.tsx b/ui/src/components/CommentThread.tsx index eda2851867..cdf0ddd24c 100644 --- a/ui/src/components/CommentThread.tsx +++ b/ui/src/components/CommentThread.tsx @@ -311,8 +311,11 @@ export function CommentThread({ return Array.from(agentMap.values()) .filter((a) => a.status !== "terminated") .map((a) => ({ - id: a.id, + id: `agent:${a.id}`, name: a.name, + kind: "agent", + agentId: a.id, + agentIcon: a.icon, })); }, [agentMap, providedMentions]); diff --git a/ui/src/components/IssueDocumentsSection.tsx b/ui/src/components/IssueDocumentsSection.tsx index 0f112062af..f61ddd7585 100644 --- a/ui/src/components/IssueDocumentsSection.tsx +++ b/ui/src/components/IssueDocumentsSection.tsx @@ -519,21 +519,23 @@ export function IssueDocumentsSection({ return (
{isEmpty && !draft?.isNew ? ( -
+
{extraActions} -
) : ( -
-

Documents

-
+
+

Documents

+
{extraActions} -
@@ -634,29 +636,29 @@ export function IssueDocumentsSection({ >
-
+
- + {doc.key} rev {doc.latestRevisionNumber} • updated {relativeTime(doc.updatedAt)}
{showTitle &&

{doc.title}

}
-
+
diff --git a/ui/src/components/MarkdownEditor.tsx b/ui/src/components/MarkdownEditor.tsx index 29a57a3a0e..342a74de54 100644 --- a/ui/src/components/MarkdownEditor.tsx +++ b/ui/src/components/MarkdownEditor.tsx @@ -6,7 +6,6 @@ import { useMemo, useRef, useState, - type CSSProperties, type DragEvent, } from "react"; import { @@ -27,7 +26,11 @@ import { thematicBreakPlugin, type RealmPlugin, } from "@mdxeditor/editor"; -import { buildProjectMentionHref, parseProjectMentionHref } from "@paperclipai/shared"; +import { buildAgentMentionHref, buildProjectMentionHref } from "@paperclipai/shared"; +import { AgentIcon } from "./AgentIconPicker"; +import { applyMentionChipDecoration, clearMentionChipDecoration, parseMentionChipHref } from "../lib/mention-chips"; +import { MentionAwareLinkNode, mentionAwareLinkNodeReplacement } from "../lib/mention-aware-link-node"; +import { mentionDeletionPlugin } from "../lib/mention-deletion"; import { cn } from "../lib/utils"; /* ---- Mention types ---- */ @@ -36,6 +39,8 @@ export interface MentionOption { id: string; name: string; kind?: "agent" | "project"; + agentId?: string; + agentIcon?: string | null; projectId?: string; projectColor?: string | null; } @@ -65,6 +70,12 @@ function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } +function isSafeMarkdownLinkUrl(url: string): boolean { + const trimmed = url.trim(); + if (!trimmed) return true; + return !/^(javascript|data|vbscript):/i.test(trimmed); +} + /* ---- Mention detection helpers ---- */ interface MentionState { @@ -154,7 +165,8 @@ function mentionMarkdown(option: MentionOption): string { if (option.kind === "project" && option.projectId) { return `[@${option.name}](${buildProjectMentionHref(option.projectId, option.projectColor ?? null)}) `; } - return `@${option.name} `; + const agentId = option.agentId ?? option.id.replace(/^agent:/, ""); + return `[@${option.name}](${buildAgentMentionHref(agentId, option.agentIcon ?? null)}) `; } /** Replace `@` in the markdown string with the selected mention token. */ @@ -166,31 +178,6 @@ function applyMention(markdown: string, query: string, option: MentionOption): s return markdown.slice(0, idx) + replacement + markdown.slice(idx + search.length); } -function hexToRgb(hex: string): { r: number; g: number; b: number } | null { - const trimmed = hex.trim(); - const match = /^#([0-9a-f]{6})$/i.exec(trimmed); - if (!match) return null; - const value = match[1]; - return { - r: parseInt(value.slice(0, 2), 16), - g: parseInt(value.slice(2, 4), 16), - b: parseInt(value.slice(4, 6), 16), - }; -} - -function mentionChipStyle(color: string | null): CSSProperties | undefined { - if (!color) return undefined; - const rgb = hexToRgb(color); - if (!rgb) return undefined; - const luminance = (0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b) / 255; - const textColor = luminance > 0.55 ? "#111827" : "#f8fafc"; - return { - borderColor: color, - backgroundColor: `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 0.22)`, - color: textColor, - }; -} - /* ---- Component ---- */ export const MarkdownEditor = forwardRef(function MarkdownEditor({ @@ -221,11 +208,15 @@ export const MarkdownEditor = forwardRef const mentionStateRef = useRef(null); const [mentionIndex, setMentionIndex] = useState(0); const mentionActive = mentionState !== null && mentions && mentions.length > 0; - const projectColorById = useMemo(() => { - const map = new Map(); + const mentionOptionByKey = useMemo(() => { + const map = new Map(); for (const mention of mentions ?? []) { + if (mention.kind === "agent") { + const agentId = mention.agentId ?? mention.id.replace(/^agent:/, ""); + map.set(`agent:${agentId}`, mention); + } if (mention.kind === "project" && mention.projectId) { - map.set(mention.projectId, mention.projectColor ?? null); + map.set(`project:${mention.projectId}`, mention); } } return map; @@ -286,8 +277,9 @@ export const MarkdownEditor = forwardRef listsPlugin(), quotePlugin(), tablePlugin(), - linkPlugin(), + linkPlugin({ validateUrl: isSafeMarkdownLinkUrl }), linkDialogPlugin(), + mentionDeletionPlugin(), thematicBreakPlugin(), codeBlockPlugin({ defaultCodeBlockLanguage: "txt", @@ -315,31 +307,28 @@ export const MarkdownEditor = forwardRef const links = editable.querySelectorAll("a"); for (const node of links) { const link = node as HTMLAnchorElement; - const parsed = parseProjectMentionHref(link.getAttribute("href") ?? ""); + const parsed = parseMentionChipHref(link.getAttribute("href") ?? ""); if (!parsed) { - if (link.dataset.projectMention === "true") { - link.dataset.projectMention = "false"; - link.classList.remove("paperclip-project-mention-chip"); - link.removeAttribute("contenteditable"); - link.style.removeProperty("border-color"); - link.style.removeProperty("background-color"); - link.style.removeProperty("color"); - } + clearMentionChipDecoration(link); continue; } - const color = parsed.color ?? projectColorById.get(parsed.projectId) ?? null; - link.dataset.projectMention = "true"; - link.classList.add("paperclip-project-mention-chip"); - link.setAttribute("contenteditable", "false"); - const style = mentionChipStyle(color); - if (style) { - link.style.borderColor = style.borderColor ?? ""; - link.style.backgroundColor = style.backgroundColor ?? ""; - link.style.color = style.color ?? ""; + if (parsed.kind === "project") { + const option = mentionOptionByKey.get(`project:${parsed.projectId}`); + applyMentionChipDecoration(link, { + ...parsed, + color: parsed.color ?? option?.projectColor ?? null, + }); + continue; } + + const option = mentionOptionByKey.get(`agent:${parsed.agentId}`); + applyMentionChipDecoration(link, { + ...parsed, + icon: parsed.icon ?? option?.agentIcon ?? null, + }); } - }, [projectColorById]); + }, [mentionOptionByKey]); // Mention detection: listen for selection changes and input events const checkMention = useCallback(() => { @@ -395,94 +384,67 @@ export const MarkdownEditor = forwardRef // update state between the last render and this callback firing). const state = mentionStateRef.current; if (!state) return; + const current = latestValueRef.current; + const next = applyMention(current, state.query, option); + if (next !== current) { + latestValueRef.current = next; + ref.current?.setMarkdown(next); + onChange(next); + } - if (option.kind === "project" && option.projectId) { - const current = latestValueRef.current; - const next = applyMention(current, state.query, option); - if (next !== current) { - latestValueRef.current = next; - ref.current?.setMarkdown(next); - onChange(next); - } + requestAnimationFrame(() => { requestAnimationFrame(() => { - ref.current?.focus(undefined, { defaultSelection: "rootEnd" }); + const editable = containerRef.current?.querySelector('[contenteditable="true"]'); + if (!(editable instanceof HTMLElement)) return; decorateProjectMentions(); - }); - mentionStateRef.current = null; - setMentionState(null); - return; - } + editable.focus(); - const replacement = mentionMarkdown(option); - - // Replace @query directly via DOM selection so the cursor naturally - // lands after the inserted text. Lexical picks up the change through - // its normal input-event handling. - const sel = window.getSelection(); - if (sel && state.textNode.isConnected) { - const range = document.createRange(); - range.setStart(state.textNode, state.atPos); - range.setEnd(state.textNode, state.endPos); - sel.removeAllRanges(); - sel.addRange(range); - document.execCommand("insertText", false, replacement); - - // After Lexical reconciles the DOM, the cursor position set by - // execCommand may be lost. Explicitly reposition it after the - // inserted mention text. - const cursorTarget = state.atPos + replacement.length; - requestAnimationFrame(() => { - const newSel = window.getSelection(); - if (!newSel) return; - // Try the original text node first (it may still be valid) - if (state.textNode.isConnected) { - const len = state.textNode.textContent?.length ?? 0; - if (cursorTarget <= len) { - const r = document.createRange(); - r.setStart(state.textNode, cursorTarget); - r.collapse(true); - newSel.removeAllRanges(); - newSel.addRange(r); + const mentionHref = option.kind === "project" && option.projectId + ? buildProjectMentionHref(option.projectId, option.projectColor ?? null) + : buildAgentMentionHref( + option.agentId ?? option.id.replace(/^agent:/, ""), + option.agentIcon ?? null, + ); + const matchingMentions = Array.from(editable.querySelectorAll("a")) + .filter((node): node is HTMLAnchorElement => node instanceof HTMLAnchorElement) + .filter((link) => { + const href = link.getAttribute("href") ?? ""; + return href === mentionHref && link.textContent === `@${option.name}`; + }); + const containerRect = containerRef.current?.getBoundingClientRect(); + const target = matchingMentions.sort((a, b) => { + const rectA = a.getBoundingClientRect(); + const rectB = b.getBoundingClientRect(); + const leftA = containerRect ? rectA.left - containerRect.left : rectA.left; + const topA = containerRect ? rectA.top - containerRect.top : rectA.top; + const leftB = containerRect ? rectB.left - containerRect.left : rectB.left; + const topB = containerRect ? rectB.top - containerRect.top : rectB.top; + const distA = Math.hypot(leftA - state.left, topA - state.top); + const distB = Math.hypot(leftB - state.left, topB - state.top); + return distA - distB; + })[0] ?? null; + if (!target) return; + + const selection = window.getSelection(); + if (!selection) return; + const range = document.createRange(); + const nextSibling = target.nextSibling; + if (nextSibling?.nodeType === Node.TEXT_NODE) { + const text = nextSibling.textContent ?? ""; + if (text.startsWith(" ")) { + range.setStart(nextSibling, 1); + range.collapse(true); + selection.removeAllRanges(); + selection.addRange(range); return; } } - // Fallback: search for the replacement in text nodes - const editable = containerRef.current?.querySelector('[contenteditable="true"]'); - if (!editable) return; - const walker = document.createTreeWalker(editable, NodeFilter.SHOW_TEXT); - let node: Text | null; - while ((node = walker.nextNode() as Text | null)) { - const text = node.textContent ?? ""; - const idx = text.indexOf(replacement); - if (idx !== -1) { - const pos = idx + replacement.length; - if (pos <= text.length) { - const r = document.createRange(); - r.setStart(node, pos); - r.collapse(true); - newSel.removeAllRanges(); - newSel.addRange(r); - return; - } - } - } - }); - } else { - // Fallback: full markdown replacement when DOM node is stale - const current = latestValueRef.current; - const next = applyMention(current, state.query, option); - if (next !== current) { - latestValueRef.current = next; - ref.current?.setMarkdown(next); - onChange(next); - } - requestAnimationFrame(() => { - ref.current?.focus(undefined, { defaultSelection: "rootEnd" }); - }); - } - requestAnimationFrame(() => { - decorateProjectMentions(); + range.setStartAfter(target); + range.collapse(true); + selection.removeAllRanges(); + selection.addRange(range); + }); }); mentionStateRef.current = null; @@ -588,6 +550,7 @@ export const MarkdownEditor = forwardRef "paperclip-mdxeditor-content focus:outline-none [&_ul]:list-disc [&_ul]:pl-5 [&_ol]:list-decimal [&_ol]:pl-5 [&_li]:list-item", contentClassName, )} + additionalLexicalNodes={[MentionAwareLinkNode, mentionAwareLinkNodeReplacement]} plugins={plugins} /> @@ -616,7 +579,10 @@ export const MarkdownEditor = forwardRef style={{ backgroundColor: option.projectColor ?? "#64748b" }} /> ) : ( - @ + )} {option.name} {option.kind === "project" && option.projectId && ( diff --git a/ui/src/components/NewIssueDialog.tsx b/ui/src/components/NewIssueDialog.tsx index 9e309b8c82..6753bc5408 100644 --- a/ui/src/components/NewIssueDialog.tsx +++ b/ui/src/components/NewIssueDialog.tsx @@ -1,5 +1,6 @@ import { useState, useEffect, useRef, useCallback, useMemo, type ChangeEvent, type DragEvent } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; +import { pickTextColorForSolidBg } from "@/lib/color-contrast"; import { useDialog } from "../context/DialogContext"; import { useCompany } from "../context/CompanyContext"; import { executionWorkspacesApi } from "../api/execution-workspaces"; @@ -57,15 +58,6 @@ import { InlineEntitySelector, type InlineEntityOption } from "./InlineEntitySel const DRAFT_KEY = "paperclip:issue-draft"; const DEBOUNCE_MS = 800; -/** Return black or white hex based on background luminance (WCAG perceptual weights). */ -function getContrastTextColor(hexColor: string): string { - const hex = hexColor.replace("#", ""); - const r = parseInt(hex.substring(0, 2), 16); - const g = parseInt(hex.substring(2, 4), 16); - const b = parseInt(hex.substring(4, 6), 16); - const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255; - return luminance > 0.5 ? "#000000" : "#ffffff"; -} interface IssueDraft { title: string; @@ -384,6 +376,8 @@ export function NewIssueDialog() { id: `agent:${agent.id}`, name: agent.name, kind: "agent", + agentId: agent.id, + agentIcon: agent.icon, }); } for (const project of orderedProjects) { @@ -921,7 +915,7 @@ export function NewIssueDialog() { dialogCompany?.brandColor ? { backgroundColor: dialogCompany.brandColor, - color: getContrastTextColor(dialogCompany.brandColor), + color: pickTextColorForSolidBg(dialogCompany.brandColor), } : undefined } @@ -951,7 +945,7 @@ export function NewIssueDialog() { c.brandColor ? { backgroundColor: c.brandColor, - color: getContrastTextColor(c.brandColor), + color: pickTextColorForSolidBg(c.brandColor), } : undefined } @@ -1219,6 +1213,7 @@ export function NewIssueDialog() {
Enable Chrome (--chrome)
{onUpdate || onFieldUpdate ? (
+ + + + {terminatedManager && ( +
+ + + Current: {current.name} (terminated) + +
+ )} + {unknownManager && ( +
+ Saved manager is missing from this company. Choose a new manager or clear. +
+ )} + {rows.map((a) => ( + + ))} +
+ + ); +} diff --git a/ui/src/components/SidebarAgents.tsx b/ui/src/components/SidebarAgents.tsx index 43a8883c04..46515afeed 100644 --- a/ui/src/components/SidebarAgents.tsx +++ b/ui/src/components/SidebarAgents.tsx @@ -7,9 +7,11 @@ import { useDialog } from "../context/DialogContext"; import { useSidebar } from "../context/SidebarContext"; import { agentsApi } from "../api/agents"; import { healthApi } from "../api/health"; +import { authApi } from "../api/auth"; import { heartbeatsApi } from "../api/heartbeats"; import { queryKeys } from "../lib/queryKeys"; import { cn, agentRouteRef, agentUrl } from "../lib/utils"; +import { useAgentOrder } from "../hooks/useAgentOrder"; import { AgentIcon } from "./AgentIconPicker"; import { BudgetSidebarMarker } from "./BudgetSidebarMarker"; import { @@ -18,28 +20,6 @@ import { CollapsibleTrigger, } from "@/components/ui/collapsible"; import type { Agent } from "@paperclipai/shared"; - -/** BFS sort: roots first (no reportsTo), then their direct reports, etc. */ -function sortByHierarchy(agents: Agent[]): Agent[] { - const byId = new Map(agents.map((a) => [a.id, a])); - const childrenOf = new Map(); - for (const a of agents) { - const parent = a.reportsTo && byId.has(a.reportsTo) ? a.reportsTo : null; - const list = childrenOf.get(parent) ?? []; - list.push(a); - childrenOf.set(parent, list); - } - const sorted: Agent[] = []; - const queue = childrenOf.get(null) ?? []; - while (queue.length > 0) { - const agent = queue.shift()!; - sorted.push(agent); - const children = childrenOf.get(agent.id); - if (children) queue.push(...children); - } - return sorted; -} - export function SidebarAgents() { const [open, setOpen] = useState(true); const { selectedCompanyId } = useCompany(); @@ -59,6 +39,10 @@ export function SidebarAgents() { queryFn: () => agentsApi.list(selectedCompanyId!), enabled: !!selectedCompanyId, }); + const { data: session } = useQuery({ + queryKey: queryKeys.auth.session, + queryFn: () => authApi.getSession(), + }); const { data: liveRuns } = useQuery({ queryKey: queryKeys.liveRuns(selectedCompanyId!), @@ -79,8 +63,14 @@ export function SidebarAgents() { const filtered = (agents ?? []).filter( (a: Agent) => a.status !== "terminated" ); - return sortByHierarchy(filtered); + return filtered; }, [agents]); + const currentUserId = session?.user?.id ?? session?.session?.userId ?? null; + const { orderedAgents } = useAgentOrder({ + agents: visibleAgents, + companyId: selectedCompanyId, + userId: currentUserId, + }); const agentMatch = location.pathname.match(/^\/(?:[^/]+\/)?agents\/([^/]+)(?:\/([^/]+))?/); const activeAgentId = agentMatch?.[1] ?? null; @@ -117,7 +107,7 @@ export function SidebarAgents() {
- {visibleAgents.map((agent: Agent) => { + {orderedAgents.map((agent: Agent) => { const runCount = liveCountByAgent.get(agent.id) ?? 0; return ( }
- )} +
+ {!showNewFileInput && ( + + )} + {isMobile && ( + + )} +
{showNewFileInput && (
@@ -2120,6 +2168,7 @@ function PromptsTab({ onSelectFile={(filePath) => { setSelectedFile(filePath); if (!fileOptions.includes(filePath)) setDraft(""); + if (isMobile) setShowFilePanel(false); }} onToggleCheck={() => {}} showCheckboxes={false} @@ -2150,22 +2199,37 @@ function PromptsTab({
{/* Draggable separator */} -
+ {!isMobile && ( +
+ )} -
+
-
-

{selectedOrEntryFile}

-

- {selectedFileExists - ? selectedFileSummary?.deprecated - ? "Deprecated virtual file" - : `${selectedFileDetail?.language ?? "text"} file` - : "New file in this bundle"} -

+
+ {isMobile && ( + + )} +
+

{selectedOrEntryFile}

+

+ {selectedFileExists + ? selectedFileSummary?.deprecated + ? "Deprecated virtual file" + : `${selectedFileDetail?.language ?? "text"} file` + : "New file in this bundle"} +

+
{selectedFileExists && !selectedFileSummary?.deprecated && selectedOrEntryFile !== currentEntryFile && ( +
+
+ ); + } + + return ( +
+
+

Approve Paperclip CLI access

+

+ A local Paperclip CLI process is requesting board access to this instance. +

+ +
+
+
Command
+
{challenge.command}
+
+
+
Client
+
{challenge.clientName ?? "paperclipai cli"}
+
+
+
Requested access
+
+ {challenge.requestedAccess === "instance_admin_required" ? "Instance admin" : "Board"} +
+
+ {challenge.requestedCompanyName && ( +
+
Requested company
+
{challenge.requestedCompanyName}
+
+ )} +
+ + {(approveMutation.error || cancelMutation.error) && ( +

+ {(approveMutation.error ?? cancelMutation.error) instanceof Error + ? ((approveMutation.error ?? cancelMutation.error) as Error).message + : "Failed to update CLI auth challenge"} +

+ )} + + {!challenge.canApprove && ( +

+ This challenge requires instance-admin access. Sign in with an instance admin account to approve it. +

+ )} + +
+ + +
+
+
+ ); +} diff --git a/ui/src/pages/CompanyExport.tsx b/ui/src/pages/CompanyExport.tsx index 5ed8f640c4..e82aeafbf0 100644 --- a/ui/src/pages/CompanyExport.tsx +++ b/ui/src/pages/CompanyExport.tsx @@ -1,22 +1,32 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react"; -import { useMutation } from "@tanstack/react-query"; +import { useMutation, useQuery } from "@tanstack/react-query"; import type { + Agent, CompanyPortabilityFileEntry, CompanyPortabilityExportPreviewResult, CompanyPortabilityExportResult, CompanyPortabilityManifest, + Project, } from "@paperclipai/shared"; import { useNavigate, useLocation } from "@/lib/router"; import { useCompany } from "../context/CompanyContext"; import { useBreadcrumbs } from "../context/BreadcrumbContext"; import { useToast } from "../context/ToastContext"; +import { agentsApi } from "../api/agents"; +import { authApi } from "../api/auth"; import { companiesApi } from "../api/companies"; +import { projectsApi } from "../api/projects"; import { Button } from "@/components/ui/button"; import { EmptyState } from "../components/EmptyState"; import { PageSkeleton } from "../components/PageSkeleton"; import { MarkdownBody } from "../components/MarkdownBody"; import { cn } from "../lib/utils"; +import { queryKeys } from "../lib/queryKeys"; import { createZipArchive } from "../lib/zip"; +import { buildInitialExportCheckedFiles } from "../lib/company-export-selection"; +import { useAgentOrder } from "../hooks/useAgentOrder"; +import { useProjectOrder } from "../hooks/useProjectOrder"; +import { buildPortableSidebarOrder } from "../lib/company-portability-sidebar"; import { getPortableFileDataUrl, getPortableFileText, isPortableImageFile } from "../lib/portable-files"; import { Download, @@ -34,11 +44,6 @@ import { PackageFileTree, } from "../components/PackageFileTree"; -/** Returns true if the path looks like a task file (e.g. tasks/slug/TASK.md or projects/x/tasks/slug/TASK.md) */ -function isTaskPath(filePath: string): boolean { - return /(?:^|\/)tasks\//.test(filePath); -} - /** * Extract the set of agent/project/task slugs that are "checked" based on * which file paths are in the checked set. @@ -50,6 +55,7 @@ function checkedSlugs(checkedFiles: Set): { agents: Set; projects: Set; tasks: Set; + routines: Set; } { const agents = new Set(); const projects = new Set(); @@ -62,7 +68,7 @@ function checkedSlugs(checkedFiles: Set): { const taskMatch = p.match(/^tasks\/([^/]+)\//); if (taskMatch) tasks.add(taskMatch[1]); } - return { agents, projects, tasks }; + return { agents, projects, tasks, routines: new Set(tasks) }; } /** @@ -77,16 +83,30 @@ function filterPaperclipYaml(yaml: string, checkedFiles: Set): string { const out: string[] = []; // Sections whose entries are slug-keyed and should be filtered - const filterableSections = new Set(["agents", "projects", "tasks"]); + const filterableSections = new Set(["agents", "projects", "tasks", "routines"]); + const sidebarSections = new Set(["agents", "projects"]); let currentSection: string | null = null; // top-level key (e.g. "agents") let currentEntry: string | null = null; // slug under that section let includeEntry = true; + let currentSidebarList: string | null = null; + let currentSidebarHeaderLine: string | null = null; + let currentSidebarBuffer: string[] = []; // Collect entries per section so we can omit empty section headers let sectionHeaderLine: string | null = null; let sectionBuffer: string[] = []; + function flushSidebarSection() { + if (currentSidebarHeaderLine !== null && currentSidebarBuffer.length > 0) { + sectionBuffer.push(currentSidebarHeaderLine); + sectionBuffer.push(...currentSidebarBuffer); + } + currentSidebarHeaderLine = null; + currentSidebarBuffer = []; + } + function flushSection() { + flushSidebarSection(); if (sectionHeaderLine !== null && sectionBuffer.length > 0) { out.push(sectionHeaderLine); out.push(...sectionBuffer); @@ -109,6 +129,11 @@ function filterPaperclipYaml(yaml: string, checkedFiles: Set): string { currentSection = key; sectionHeaderLine = line; continue; + } else if (key === "sidebar") { + currentSection = key; + currentSidebarList = null; + sectionHeaderLine = line; + continue; } else { currentSection = null; out.push(line); @@ -116,6 +141,32 @@ function filterPaperclipYaml(yaml: string, checkedFiles: Set): string { } } + if (currentSection === "sidebar") { + const sidebarMatch = line.match(/^ ([\w-]+):\s*$/); + if (sidebarMatch && !line.startsWith(" ")) { + flushSidebarSection(); + const sidebarKey = sidebarMatch[1]; + currentSidebarList = sidebarKey && sidebarSections.has(sidebarKey) ? sidebarKey : null; + currentSidebarHeaderLine = currentSidebarList ? line : null; + continue; + } + + const sidebarEntryMatch = line.match(/^ - ["']?([^"'\n]+)["']?\s*$/); + if (sidebarEntryMatch && currentSidebarList) { + const slug = sidebarEntryMatch[1]; + const sectionSlugs = slugs[currentSidebarList as keyof typeof slugs]; + if (slug && sectionSlugs.has(slug)) { + currentSidebarBuffer.push(line); + } + continue; + } + + if (currentSidebarList) { + currentSidebarBuffer.push(line); + continue; + } + } + // Inside a filterable section if (currentSection && filterableSections.has(currentSection)) { // 2-space indented key = entry slug (slugs may start with digits/hyphens) @@ -532,6 +583,20 @@ export function CompanyExport() { const { pushToast } = useToast(); const navigate = useNavigate(); const location = useLocation(); + const { data: session, isFetched: isSessionFetched } = useQuery({ + queryKey: queryKeys.auth.session, + queryFn: () => authApi.getSession(), + }); + const { data: agents = [], isFetched: areAgentsFetched } = useQuery({ + queryKey: queryKeys.agents.list(selectedCompanyId!), + queryFn: () => agentsApi.list(selectedCompanyId!), + enabled: !!selectedCompanyId, + }); + const { data: projects = [], isFetched: areProjectsFetched } = useQuery({ + queryKey: queryKeys.projects.list(selectedCompanyId!), + queryFn: () => projectsApi.list(selectedCompanyId!), + enabled: !!selectedCompanyId, + }); const [exportData, setExportData] = useState(null); const [selectedFile, setSelectedFile] = useState(null); @@ -541,6 +606,38 @@ export function CompanyExport() { const [taskLimit, setTaskLimit] = useState(TASKS_PAGE_SIZE); const savedExpandedRef = useRef | null>(null); const initialFileFromUrl = useRef(filePathFromLocation(location.pathname)); + const currentUserId = session?.user?.id ?? session?.session?.userId ?? null; + const visibleAgents = useMemo( + () => agents.filter((agent: Agent) => agent.status !== "terminated"), + [agents], + ); + const visibleProjects = useMemo( + () => projects.filter((project: Project) => !project.archivedAt), + [projects], + ); + const { orderedAgents } = useAgentOrder({ + agents: visibleAgents, + companyId: selectedCompanyId, + userId: currentUserId, + }); + const { orderedProjects } = useProjectOrder({ + projects: visibleProjects, + companyId: selectedCompanyId, + userId: currentUserId, + }); + const sidebarOrder = useMemo( + () => buildPortableSidebarOrder({ + agents: visibleAgents, + orderedAgents, + projects: visibleProjects, + orderedProjects, + }), + [orderedAgents, orderedProjects, visibleAgents, visibleProjects], + ); + const sidebarOrderKey = useMemo( + () => JSON.stringify(sidebarOrder ?? null), + [sidebarOrder], + ); // Navigate-aware file selection: updates state + URL without page reload. // `replace` = true skips history entry (used for initial load); false = pushes (used for clicks). @@ -584,17 +681,17 @@ export function CompanyExport() { mutationFn: () => companiesApi.exportPreview(selectedCompanyId!, { include: { company: true, agents: true, projects: true, issues: true }, + sidebarOrder, }), onSuccess: (result) => { setExportData(result); - setCheckedFiles((prev) => { - const next = new Set(); - for (const filePath of Object.keys(result.files)) { - if (prev.has(filePath)) next.add(filePath); - else if (!isTaskPath(filePath)) next.add(filePath); - } - return next; - }); + setCheckedFiles((prev) => + buildInitialExportCheckedFiles( + Object.keys(result.files), + result.manifest.issues, + prev, + ), + ); // Expand top-level dirs (except tasks — collapsed by default) const tree = buildFileTree(result.files); const topDirs = new Set(); @@ -633,6 +730,7 @@ export function CompanyExport() { companiesApi.exportPackage(selectedCompanyId!, { include: { company: true, agents: true, projects: true, issues: true }, selectedFiles: Array.from(checkedFiles).sort(), + sidebarOrder, }), onSuccess: (result) => { const resultCheckedFiles = new Set(Object.keys(result.files)); @@ -654,10 +752,11 @@ export function CompanyExport() { useEffect(() => { if (!selectedCompanyId || exportPreviewMutation.isPending) return; + if (!isSessionFetched || !areAgentsFetched || !areProjectsFetched) return; setExportData(null); exportPreviewMutation.mutate(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [selectedCompanyId]); + }, [selectedCompanyId, isSessionFetched, areAgentsFetched, areProjectsFetched, sidebarOrderKey]); const tree = useMemo( () => (exportData ? buildFileTree(exportData.files) : []), diff --git a/ui/src/pages/CompanyImport.tsx b/ui/src/pages/CompanyImport.tsx index c185615d06..60765138d4 100644 --- a/ui/src/pages/CompanyImport.tsx +++ b/ui/src/pages/CompanyImport.tsx @@ -10,9 +10,12 @@ import type { import { useCompany } from "../context/CompanyContext"; import { useBreadcrumbs } from "../context/BreadcrumbContext"; import { useToast } from "../context/ToastContext"; +import { authApi } from "../api/auth"; import { companiesApi } from "../api/companies"; import { agentsApi } from "../api/agents"; import { queryKeys } from "../lib/queryKeys"; +import { getAgentOrderStorageKey, writeAgentOrder } from "../lib/agent-order"; +import { getProjectOrderStorageKey, writeProjectOrder } from "../lib/project-order"; import { MarkdownBody } from "../components/MarkdownBody"; import { Button } from "@/components/ui/button"; import { EmptyState } from "../components/EmptyState"; @@ -342,6 +345,45 @@ function prefixedName(prefix: string | null, originalName: string): string { return `${prefix}-${originalName}`; } +function applyImportedSidebarOrder( + preview: CompanyPortabilityPreviewResult | null, + result: { + company: { id: string }; + agents: Array<{ slug: string; id: string | null }>; + projects: Array<{ slug: string; id: string | null }>; + }, + userId: string | null | undefined, +) { + const sidebar = preview?.manifest.sidebar; + if (!sidebar) return; + if (!userId?.trim()) return; + + const agentIdBySlug = new Map( + result.agents + .filter((agent): agent is { slug: string; id: string } => typeof agent.id === "string" && agent.id.length > 0) + .map((agent) => [agent.slug, agent.id]), + ); + const projectIdBySlug = new Map( + result.projects + .filter((project): project is { slug: string; id: string } => typeof project.id === "string" && project.id.length > 0) + .map((project) => [project.slug, project.id]), + ); + + const orderedAgentIds = sidebar.agents + .map((slug) => agentIdBySlug.get(slug)) + .filter((id): id is string => Boolean(id)); + const orderedProjectIds = sidebar.projects + .map((slug) => projectIdBySlug.get(slug)) + .filter((id): id is string => Boolean(id)); + + if (orderedAgentIds.length > 0) { + writeAgentOrder(getAgentOrderStorageKey(result.company.id, userId), orderedAgentIds); + } + if (orderedProjectIds.length > 0) { + writeProjectOrder(getProjectOrderStorageKey(result.company.id, userId), orderedProjectIds); + } +} + // ── Conflict resolution UI ─────────────────────────────────────────── function ConflictResolutionList({ @@ -611,6 +653,11 @@ export function CompanyImport() { const { pushToast } = useToast(); const queryClient = useQueryClient(); const packageInputRef = useRef(null); + const { data: session } = useQuery({ + queryKey: queryKeys.auth.session, + queryFn: () => authApi.getSession(), + }); + const currentUserId = session?.user?.id ?? session?.session?.userId ?? null; // Source state const [sourceMode, setSourceMode] = useState<"github" | "local">("github"); @@ -800,6 +847,18 @@ export function CompanyImport() { onSuccess: async (result) => { await queryClient.invalidateQueries({ queryKey: queryKeys.companies.all }); const importedCompany = await companiesApi.get(result.company.id); + const refreshedSession = currentUserId + ? null + : await queryClient.fetchQuery({ + queryKey: queryKeys.auth.session, + queryFn: () => authApi.getSession(), + }); + const sidebarOrderUserId = + currentUserId + ?? refreshedSession?.user?.id + ?? refreshedSession?.session?.userId + ?? null; + applyImportedSidebarOrder(importPreview, result, sidebarOrderUserId); setSelectedCompanyId(importedCompany.id); pushToast({ tone: "success", diff --git a/ui/src/pages/Dashboard.tsx b/ui/src/pages/Dashboard.tsx index 45613380cf..c30f3bb4f1 100644 --- a/ui/src/pages/Dashboard.tsx +++ b/ui/src/pages/Dashboard.tsx @@ -14,7 +14,7 @@ import { queryKeys } from "../lib/queryKeys"; import { MetricCard } from "../components/MetricCard"; import { EmptyState } from "../components/EmptyState"; import { StatusIcon } from "../components/StatusIcon"; -import { PriorityIcon } from "../components/PriorityIcon"; + import { ActivityRow } from "../components/ActivityRow"; import { Identity } from "../components/Identity"; import { timeAgo } from "../lib/timeAgo"; @@ -356,7 +356,6 @@ export function Dashboard() { {issue.title} - {issue.identifier ?? issue.id.slice(0, 8)} diff --git a/ui/src/pages/Inbox.tsx b/ui/src/pages/Inbox.tsx index ae4086250b..a91b56fd26 100644 --- a/ui/src/pages/Inbox.tsx +++ b/ui/src/pages/Inbox.tsx @@ -16,7 +16,7 @@ import { createIssueDetailLocationState } from "../lib/issueDetailBreadcrumb"; import { EmptyState } from "../components/EmptyState"; import { PageSkeleton } from "../components/PageSkeleton"; import { IssueRow } from "../components/IssueRow"; -import { PriorityIcon } from "../components/PriorityIcon"; + import { StatusIcon } from "../components/StatusIcon"; import { StatusBadge } from "../components/StatusBadge"; import { approvalLabel, defaultTypeIcon, typeIcon } from "../components/ApprovalPayload"; @@ -37,6 +37,7 @@ import { XCircle, X, RotateCcw, + UserPlus, } from "lucide-react"; import { PageTabBar } from "../components/PageTabBar"; import type { Approval, HeartbeatRun, Issue, JoinRequest } from "@paperclipai/shared"; @@ -62,7 +63,6 @@ type InboxCategoryFilter = | "alerts"; type SectionKey = | "work_items" - | "join_requests" | "alerts"; function firstNonEmptyLine(value: string | null | undefined): string | null { @@ -282,6 +282,84 @@ function ApprovalInboxRow({ ); } +function JoinRequestInboxRow({ + joinRequest, + onApprove, + onReject, + isPending, +}: { + joinRequest: JoinRequest; + onApprove: () => void; + onReject: () => void; + isPending: boolean; +}) { + const label = + joinRequest.requestType === "human" + ? "Human join request" + : `Agent join request${joinRequest.agentName ? `: ${joinRequest.agentName}` : ""}`; + + return ( +
+
+
+
+
+ + +
+
+
+ + +
+
+ ); +} + export function Inbox() { const { selectedCompanyId } = useCompany(); const { setBreadcrumbs } = useBreadcrumbs(); @@ -439,14 +517,22 @@ export function Inbox() { return failedRuns; }, [failedRuns, tab, showFailedRunsCategory]); + const joinRequestsForTab = useMemo(() => { + if (tab === "all" && !showJoinRequestsCategory) return []; + if (tab === "recent") return joinRequests; + if (tab === "unread") return joinRequests; + return joinRequests; + }, [joinRequests, tab, showJoinRequestsCategory]); + const workItemsToRender = useMemo( () => getInboxWorkItems({ issues: tab === "all" && !showTouchedCategory ? [] : issuesToRender, approvals: tab === "all" && !showApprovalsCategory ? [] : approvalsToRender, failedRuns: failedRunsForTab, + joinRequests: joinRequestsForTab, }), - [approvalsToRender, issuesToRender, showApprovalsCategory, showTouchedCategory, tab, failedRunsForTab], + [approvalsToRender, issuesToRender, showApprovalsCategory, showTouchedCategory, tab, failedRunsForTab, joinRequestsForTab], ); const agentName = (id: string | null) => { @@ -610,10 +696,7 @@ export function Inbox() { dashboard.costs.monthUtilizationPercent >= 80 && !dismissed.has("alert:budget"); const hasAlerts = showAggregateAgentError || showBudgetAlert; - const hasJoinRequests = joinRequests.length > 0; const showWorkItemsSection = workItemsToRender.length > 0; - const showJoinRequestsSection = - tab === "all" ? showJoinRequestsCategory && hasJoinRequests : tab === "unread" && hasJoinRequests; const showAlertsSection = shouldShowInboxSection({ tab, hasItems: hasAlerts, @@ -624,7 +707,6 @@ export function Inbox() { const visibleSections = [ showAlertsSection ? "alerts" : null, - showJoinRequestsSection ? "join_requests" : null, showWorkItemsSection ? "work_items" : null, ].filter((key): key is SectionKey => key !== null); @@ -765,6 +847,18 @@ export function Inbox() { ); } + if (item.kind === "join_request") { + return ( + approveJoinMutation.mutate(item.joinRequest)} + onReject={() => rejectJoinMutation.mutate(item.joinRequest)} + isPending={approveJoinMutation.isPending || rejectJoinMutation.isPending} + /> + ); + } + const issue = item.issue; const isUnread = issue.isUnreadForMe && !fadingOutIssues.has(issue.id); const isFading = fadingOutIssues.has(issue.id); @@ -775,9 +869,6 @@ export function Inbox() { issueLinkState={issueLinkState} desktopMetaLeading={( <> - - - @@ -817,61 +908,6 @@ export function Inbox() { )} - {showJoinRequestsSection && ( - <> - {showSeparatorBefore("join_requests") && } -
-

- Join Requests -

-
- {joinRequests.map((joinRequest) => ( -
-
-
-

- {joinRequest.requestType === "human" - ? "Human join request" - : `Agent join request${joinRequest.agentName ? `: ${joinRequest.agentName}` : ""}`} -

-

- requested {timeAgo(joinRequest.createdAt)} from IP {joinRequest.requestIp} -

- {joinRequest.requestEmailSnapshot && ( -

- email: {joinRequest.requestEmailSnapshot} -

- )} - {!isHosted && joinRequest.adapterType && ( -

adapter: {joinRequest.adapterType}

- )} -
-
- - -
-
-
- ))} -
-
- - )} - - {showAlertsSection && ( <> {showSeparatorBefore("alerts") && } diff --git a/ui/src/pages/InstanceExperimentalSettings.tsx b/ui/src/pages/InstanceExperimentalSettings.tsx index 07728a63c8..050166ffe6 100644 --- a/ui/src/pages/InstanceExperimentalSettings.tsx +++ b/ui/src/pages/InstanceExperimentalSettings.tsx @@ -84,6 +84,7 @@ export function InstanceExperimentalSettings() {
-
+
{activeCount} active {disabledCount} disabled {grouped.length} {grouped.length === 1 ? "company" : "companies"} + {anyEnabled && ( + + )}
{actionError && ( diff --git a/ui/src/pages/InviteLanding.tsx b/ui/src/pages/InviteLanding.tsx index a9ee85b9f8..e1c4c53e46 100644 --- a/ui/src/pages/InviteLanding.tsx +++ b/ui/src/pages/InviteLanding.tsx @@ -17,13 +17,14 @@ const adapterLabels: Record = { codex_local: "Codex (local)", gemini_local: "Gemini CLI (local)", opencode_local: "OpenCode (local)", + pi_local: "Pi (local)", openclaw_gateway: "OpenClaw Gateway", cursor: "Cursor (local)", process: "Process", http: "HTTP", }; -const ENABLED_INVITE_ADAPTERS = new Set(["claude_local", "codex_local", "gemini_local", "opencode_local", "cursor"]); +const ENABLED_INVITE_ADAPTERS = new Set(["claude_local", "codex_local", "gemini_local", "opencode_local", "pi_local", "cursor"]); function dateTime(value: string) { return new Date(value).toLocaleString(); diff --git a/ui/src/pages/IssueDetail.tsx b/ui/src/pages/IssueDetail.tsx index 2c6ae1043c..ed23b055c9 100644 --- a/ui/src/pages/IssueDetail.tsx +++ b/ui/src/pages/IssueDetail.tsx @@ -1,4 +1,5 @@ import { useEffect, useMemo, useRef, useState, type ChangeEvent, type DragEvent } from "react"; +import { pickTextColorForPillBg } from "@/lib/color-contrast"; import { Link, useLocation, useNavigate, useParams } from "@/lib/router"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { issuesApi } from "../api/issues"; @@ -341,6 +342,8 @@ export function IssueDetail() { id: `agent:${agent.id}`, name: agent.name, kind: "agent", + agentId: agent.id, + agentIcon: agent.icon, }); } for (const project of orderedProjects) { @@ -670,7 +673,12 @@ export function IssueDetail() { )} > - {uploadAttachment.isPending || importMarkdownDocument.isPending ? "Uploading..." : "Upload attachment"} + {uploadAttachment.isPending || importMarkdownDocument.isPending ? "Uploading..." : ( + <> + Upload attachment + Upload + + )} ); @@ -760,7 +768,7 @@ export function IssueDetail() { className="inline-flex items-center rounded-full border px-2 py-0.5 text-[10px] font-medium" style={{ borderColor: label.color, - color: label.color, + color: pickTextColorForPillBg(label.color, 0.12), backgroundColor: `${label.color}1f`, }} > diff --git a/ui/src/pages/Issues.tsx b/ui/src/pages/Issues.tsx index bc5131e782..ee3d64b096 100644 --- a/ui/src/pages/Issues.tsx +++ b/ui/src/pages/Issues.tsx @@ -21,6 +21,7 @@ export function Issues() { const queryClient = useQueryClient(); const initialSearch = searchParams.get("q") ?? ""; + const participantAgentId = searchParams.get("participantAgentId") ?? undefined; const debounceRef = useRef>(undefined); const handleSearchChange = useCallback((search: string) => { clearTimeout(debounceRef.current); @@ -86,8 +87,8 @@ export function Issues() { }, [setBreadcrumbs]); const { data: issues, isLoading, error } = useQuery({ - queryKey: queryKeys.issues.list(selectedCompanyId!), - queryFn: () => issuesApi.list(selectedCompanyId!), + queryKey: [...queryKeys.issues.list(selectedCompanyId!), "participant-agent", participantAgentId ?? "__all__"], + queryFn: () => issuesApi.list(selectedCompanyId!, { participantAgentId }), enabled: !!selectedCompanyId, }); @@ -117,6 +118,7 @@ export function Issues() { initialSearch={initialSearch} onSearchChange={handleSearchChange} onUpdateIssue={(id, data) => updateIssue.mutate({ id, data })} + searchFilters={participantAgentId ? { participantAgentId } : undefined} /> ); } diff --git a/ui/src/pages/MyIssues.tsx b/ui/src/pages/MyIssues.tsx index ea717c6d41..301c526f3e 100644 --- a/ui/src/pages/MyIssues.tsx +++ b/ui/src/pages/MyIssues.tsx @@ -5,7 +5,7 @@ import { useCompany } from "../context/CompanyContext"; import { useBreadcrumbs } from "../context/BreadcrumbContext"; import { queryKeys } from "../lib/queryKeys"; import { StatusIcon } from "../components/StatusIcon"; -import { PriorityIcon } from "../components/PriorityIcon"; + import { EntityRow } from "../components/EntityRow"; import { EmptyState } from "../components/EmptyState"; import { PageSkeleton } from "../components/PageSkeleton"; @@ -56,10 +56,7 @@ export function MyIssues() { title={issue.title} to={`/issues/${issue.identifier ?? issue.id}`} leading={ - <> - - - + } trailing={ diff --git a/ui/src/pages/NewAgent.tsx b/ui/src/pages/NewAgent.tsx index 4e37fb4bcc..e4c5f7dc38 100644 --- a/ui/src/pages/NewAgent.tsx +++ b/ui/src/pages/NewAgent.tsx @@ -15,13 +15,13 @@ import { PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; -import { Shield, User } from "lucide-react"; +import { Shield } from "lucide-react"; import { cn, agentUrl } from "../lib/utils"; import { roleLabels } from "../components/agent-config-primitives"; import { AgentConfigForm, type CreateConfigValues } from "../components/AgentConfigForm"; import { defaultCreateValues } from "../components/agent-config-defaults"; import { getUIAdapter } from "../adapters"; -import { AgentIcon } from "../components/AgentIconPicker"; +import { ReportsToPicker } from "../components/ReportsToPicker"; import { DEFAULT_CODEX_LOCAL_BYPASS_APPROVALS_AND_SANDBOX, DEFAULT_CODEX_LOCAL_MODEL, @@ -69,11 +69,10 @@ export function NewAgent() { const [name, setName] = useState(""); const [title, setTitle] = useState(""); const [role, setRole] = useState("general"); - const [reportsTo, setReportsTo] = useState(""); + const [reportsTo, setReportsTo] = useState(null); const [configValues, setConfigValues] = useState(defaultCreateValues); const [selectedSkillKeys, setSelectedSkillKeys] = useState([]); const [roleOpen, setRoleOpen] = useState(false); - const [reportsToOpen, setReportsToOpen] = useState(false); const [formError, setFormError] = useState(null); const healthQuery = useQuery({ @@ -207,7 +206,6 @@ export function NewAgent() { }); } - const currentReportsTo = (agents ?? []).find((a) => a.id === reportsTo); const availableSkills = (companySkills ?? []).filter((skill) => !skill.key.startsWith("paperclipai/paperclip/")); function toggleSkill(key: string, checked: boolean) { @@ -281,54 +279,12 @@ export function NewAgent() { - - - - - - - {(agents ?? []).map((a) => ( - - ))} - - +
{/* Shared config form */} diff --git a/ui/src/pages/RoutineDetail.tsx b/ui/src/pages/RoutineDetail.tsx index f357878d3c..387e31f738 100644 --- a/ui/src/pages/RoutineDetail.tsx +++ b/ui/src/pages/RoutineDetail.tsx @@ -647,6 +647,7 @@ export function RoutineDetail() {