61 lines · 4.1 KBRaw
| 1 | import { now } from "./auth.ts"; |
| 2 | import type { Actor } from "./contract/actor.ts"; |
| 3 | import type { PermissionRequest } from "./contract/views.ts"; |
| 4 | import { recordEvent, repoStream } from "./events.ts"; |
| 5 | |
| 6 | /** How long a tool approval waits for its human before it expires as a denial. */ |
| 7 | export const APPROVAL_WAIT_SECONDS = 10 * 60; |
| 8 | |
| 9 | /** Where a tool approval ended up: a human's answer, the agent taking it back, or no answer in time. */ |
| 10 | export type Verdict = "allow" | "deny" | "withdrawn" | "expired"; |
| 11 | |
| 12 | /** Expires this repository's approvals that waited past their window, and says when the next pending one runs out. */ |
| 13 | export async function expirePermissions(db: D1Database, repoId: string, at: number): Promise<{ expired: { session_id: string; request_id: string }[]; next: number | null }> { |
| 14 | const [expired, pending] = await db.batch<{ session_id: string; request_id: string } | { created_at: number | null }>([ |
| 15 | db.prepare("UPDATE permission_requests SET behavior = 'expired', decided_at = ? WHERE repo_id = ? AND decided_at IS NULL AND created_at <= ? RETURNING session_id, request_id").bind(at, repoId, at - APPROVAL_WAIT_SECONDS), |
| 16 | db.prepare("SELECT min(created_at) AS created_at FROM permission_requests WHERE repo_id = ? AND decided_at IS NULL").bind(repoId), |
| 17 | ]); |
| 18 | const oldest = (pending.results[0] as { created_at: number | null } | undefined)?.created_at ?? null; |
| 19 | return { expired: expired.results as { session_id: string; request_id: string }[], next: oldest === null ? null : oldest + APPROVAL_WAIT_SECONDS }; |
| 20 | } |
| 21 | |
| 22 | /** Pending tool approvals the agent's human can answer from the portal: recent, unanswered, from live sessions. With `branch`, only those from sessions on that branch. */ |
| 23 | export async function pendingPermissions(db: D1Database, repoId: string, viewerId: string, branch?: string): Promise<PermissionRequest[]> { |
| 24 | const { results } = await db |
| 25 | .prepare( |
| 26 | `SELECT p.session_id AS session, p.request_id, p.tool, p.description, p.preview, p.created_at, s.runtime, s.user_id, coalesce(s.parent_session, s.id) AS shown, coalesce(s.branch, (SELECT g.branch FROM agent_sessions g WHERE g.id = s.parent_session)) AS branch, u.name AS person |
| 27 | FROM permission_requests p JOIN agent_sessions s ON s.id = p.session_id JOIN users u ON u.id = s.user_id |
| 28 | WHERE p.repo_id = ? AND p.decided_at IS NULL AND p.created_at > ? AND s.ended_at IS NULL AND s.expires_at > ? |
| 29 | ORDER BY p.created_at, p.rowid`, |
| 30 | ) |
| 31 | .bind(repoId, now() - APPROVAL_WAIT_SECONDS, now()) |
| 32 | .all<{ session: string; request_id: string; tool: string; description: string; preview: string; created_at: number; branch: string | null; runtime: string; user_id: string; shown: string; person: string }>(); |
| 33 | return results |
| 34 | .filter((row) => branch === undefined || row.branch === branch) |
| 35 | .map((row) => ({ |
| 36 | session: row.session, |
| 37 | agent: `${row.runtime} · ${row.person} · ${row.shown.slice(0, 4)}`, |
| 38 | requestId: row.request_id, |
| 39 | tool: row.tool, |
| 40 | description: row.description, |
| 41 | preview: row.preview, |
| 42 | at: row.created_at, |
| 43 | canDecide: row.user_id === viewerId, |
| 44 | })); |
| 45 | } |
| 46 | |
| 47 | /** The agent's own human answers a pending tool approval; `refs_approve`, waiting on it, hands the verdict back to Claude Code. */ |
| 48 | export async function decidePermission(db: D1Database, actor: Actor, repoId: string, session: string, requestId: string, behavior: "allow" | "deny"): Promise<boolean> { |
| 49 | if (actor.kind !== "human") return false; |
| 50 | const at = now(); |
| 51 | const { meta } = await db |
| 52 | .prepare( |
| 53 | `UPDATE permission_requests SET behavior = ?, decided_at = ? WHERE session_id = ? AND request_id = ? AND repo_id = ? AND decided_at IS NULL AND created_at > ? |
| 54 | AND EXISTS (SELECT 1 FROM agent_sessions s WHERE s.id = permission_requests.session_id AND s.user_id = ? AND s.ended_at IS NULL AND s.expires_at > ?)`, |
| 55 | ) |
| 56 | .bind(behavior, at, session, requestId, repoId, at - APPROVAL_WAIT_SECONDS, actor.person.id, at) |
| 57 | .run(); |
| 58 | if (!meta.changes) return false; |
| 59 | await recordEvent(db, actor, repoStream(repoId), "agent.permission_decided", { session, request_id: requestId, behavior }, at).run(); |
| 60 | return true; |
| 61 | } |