Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
agent-identity.ts14.3 KB · 476 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
 * Block K2 — Agent identities + scoped permissions.
 *
 * Thin wrapper over Block H's marketplace.ts that lets the K-agent layer
 * spin up auditable "bot" apps per agent kind. Every K-agent runs as an
 * app with slug `agent-<kind>` and a matching `app_bots` row named
 * `agent-<kind>[bot]`. Installations are per-repository, permissions are
 * scoped at install time and revocable, and every token is minted through
 * marketplace.ts's `ghi_`-prefixed install-token issuer.
 *
 * No new tables — K2 reuses H1/H2's `apps`, `app_installations`, `app_bots`,
 * `app_install_tokens`, `app_events`.
 *
 * Design rules:
 *  - Do not duplicate primitives (hashing, token gen, bot username). Import
 *    from marketplace.ts.
 *  - Do not modify marketplace.ts. If we need extra vocabulary, layer it.
 *  - `AGENT_PERMISSIONS` is a superset of marketplace `KNOWN_PERMISSIONS`
 *    and adds `agent:invoke` so one agent can trigger another.
 *  - All DB helpers return null / false on error — never throw. This mirrors
 *    marketplace.ts's style and keeps callers out of try/catch ceremony.
 */

import { and, asc, eq, isNull } from "drizzle-orm";
import { db } from "../db";
import {
  apps,
  appBots,
  appEvents,
  appInstallations,
  appInstallTokens,
  users,
  type App,
  type AppInstallation,
} from "../db/schema";
import {
  botUsername,
  hasPermission,
  hashBearer,
  issueInstallToken,
  KNOWN_PERMISSIONS,
  verifyInstallToken,
  type Permission,
} from "./marketplace";

// ---------------------------------------------------------------------------
// Permission vocabulary
// ---------------------------------------------------------------------------

/**
 * Vocabulary agents are allowed to request. Superset of marketplace
 * `KNOWN_PERMISSIONS` + the new `agent:invoke` which lets one K-agent spawn
 * another.
 *
 * Only the marketplace-known subset can be persisted through `createApp` /
 * `installApp`. `agent:invoke` is stored by writing straight to `apps.permissions`
 * / `app_installations.granted_permissions` via this module, bypassing the
 * `normalisePermissions` filter in marketplace.ts (which would drop unknowns).
 */
export const AGENT_PERMISSIONS = [
  "contents:read",
  "contents:write",
  "issues:read",
  "issues:write",
  "pulls:read",
  "pulls:write",
  "checks:read",
  "checks:write",
  "deployments:read",
  "deployments:write",
  "metadata:read",
  "agent:invoke",
] as const;

export type AgentPermission = (typeof AGENT_PERMISSIONS)[number];

/** Which of the agent perms are also understood by marketplace.ts. */
const MARKETPLACE_PERMS: ReadonlySet<string> = new Set(
  KNOWN_PERMISSIONS as readonly string[]
);

/** All agent slugs are prefixed with `agent-`. */
export const AGENT_SLUG_PREFIX = "agent-";

// ---------------------------------------------------------------------------
// Pure helpers
// ---------------------------------------------------------------------------

/** Is this string a valid agent permission in our vocabulary? */
export function isAgentPermission(p: string): p is AgentPermission {
  return (AGENT_PERMISSIONS as readonly string[]).includes(p);
}

/** Drop unknowns, de-duplicate, preserve order of first appearance. */
export function normaliseAgentPermissions(
  input: readonly string[]
): AgentPermission[] {
  const seen = new Set<string>();
  const out: AgentPermission[] = [];
  for (const p of input) {
    if (isAgentPermission(p) && !seen.has(p)) {
      seen.add(p);
      out.push(p);
    }
  }
  return out;
}

/** Parse JSON permissions column, filtered to the agent vocabulary. */
export function parseAgentPermissions(
  raw: string | null | undefined
): AgentPermission[] {
  if (!raw) return [];
  try {
    const parsed = JSON.parse(raw);
    if (!Array.isArray(parsed)) return [];
    return normaliseAgentPermissions(parsed);
  } catch {
    return [];
  }
}

/** Derive the canonical agent slug for a kind (e.g. "reviewer" → "agent-reviewer"). */
export function agentSlug(kind: string): string {
  const trimmed = (kind || "").toLowerCase().replace(/[^a-z0-9]+/g, "-");
  const stripped = trimmed.replace(/^-+|-+$/g, "").slice(0, 32);
  const base = stripped || "unknown";
  return base.startsWith(AGENT_SLUG_PREFIX) ? base : AGENT_SLUG_PREFIX + base;
}

/** Guard: does this bot username belong to a K-agent? */
export function isAgentBotUsername(name: string | null | undefined): boolean {
  if (!name) return false;
  return name.startsWith(AGENT_SLUG_PREFIX) && name.endsWith("[bot]");
}

// ---------------------------------------------------------------------------
// DB helpers — defensive, null-returning on error.
// ---------------------------------------------------------------------------

/** Pick the oldest user as "system" for creator_id bootstrapping. */
async function resolveSystemUserId(): Promise<string | null> {
  try {
    const [first] = await db
      .select({ id: users.id })
      .from(users)
      .orderBy(asc(users.createdAt))
      .limit(1);
    return first?.id || null;
  } catch {
    return null;
  }
}

/**
 * Idempotent: ensure an app row with the given slug exists, plus its
 * `app_bots` row. Returns the app, or null on failure (e.g. no users yet).
 *
 * `slug` is rewritten to have the `agent-` prefix if missing. Permissions
 * are stored as the full agent vocabulary (including `agent:invoke`),
 * bypassing marketplace's filter.
 */
export async function ensureAgentApp(
  slug: string,
  displayName: string,
  permissions: readonly string[]
): Promise<App | null> {
  const finalSlug = slug.startsWith(AGENT_SLUG_PREFIX)
    ? slug
    : AGENT_SLUG_PREFIX + slug;
  const perms = normaliseAgentPermissions(permissions);
  try {
    const [existing] = await db
      .select()
      .from(apps)
      .where(eq(apps.slug, finalSlug))
      .limit(1);
    if (existing) return existing;
    const creatorId = await resolveSystemUserId();
    if (!creatorId) {
      console.error(
        "[agent-identity] ensureAgentApp: no users exist yet; cannot bootstrap"
      );
      return null;
    }
    const [row] = await db
      .insert(apps)
      .values({
        slug: finalSlug,
        name: displayName,
        description: `K-agent: ${displayName}`,
        creatorId,
        permissions: JSON.stringify(perms),
        defaultEvents: "[]",
        isPublic: false,
      })
      .returning();
    if (!row) return null;
    // Matching bot account — unique on username + appId.
    try {
      await db.insert(appBots).values({
        appId: row.id,
        username: botUsername(finalSlug),
        displayName: `${displayName} (agent bot)`,
      });
    } catch (err) {
      // Duplicate from a prior partial run is fine; anything else is logged.
      if (!String((err as Error)?.message || "").includes("duplicate")) {
        console.error("[agent-identity] ensureAgentApp bot insert:", err);
      }
    }
    return row;
  } catch (err) {
    console.error("[agent-identity] ensureAgentApp:", err);
    return null;
  }
}

/**
 * Install an agent for a single repository. Idempotent — if a non-uninstalled
 * installation exists, the granted permissions are overwritten to the new
 * set. Audit trail is recorded via `app_events`.
 *
 * `grantedPermissions` is filtered through the agent vocabulary AND against
 * the permissions the app originally declared — you cannot grant more than
 * the agent asked for. Returns the installation row, or null on failure.
 */
export async function installAgentForRepo(
  agentSlug: string,
  repoId: string,
  installerUserId: string,
  grantedPermissions: readonly string[]
): Promise<AppInstallation | null> {
  try {
    const [app] = await db
      .select()
      .from(apps)
      .where(eq(apps.slug, agentSlug))
      .limit(1);
    if (!app) return null;
    const appPerms = parseAgentPermissions(app.permissions);
    const requested = normaliseAgentPermissions(grantedPermissions);
    const filtered = requested.filter((p) => appPerms.includes(p));
    const [existing] = await db
      .select()
      .from(appInstallations)
      .where(
        and(
          eq(appInstallations.appId, app.id),
          eq(appInstallations.targetType, "repository"),
          eq(appInstallations.targetId, repoId),
          isNull(appInstallations.uninstalledAt)
        )
      )
      .limit(1);
    if (existing) {
      await db
        .update(appInstallations)
        .set({ grantedPermissions: JSON.stringify(filtered) })
        .where(eq(appInstallations.id, existing.id));
      try {
        await db.insert(appEvents).values({
          appId: app.id,
          installationId: existing.id,
          kind: "installed",
          payload: JSON.stringify({ updated: true, agent: agentSlug }),
        });
      } catch {
        /* audit best-effort */
      }
      return existing;
    }
    const [row] = await db
      .insert(appInstallations)
      .values({
        appId: app.id,
        installedBy: installerUserId,
        targetType: "repository",
        targetId: repoId,
        grantedPermissions: JSON.stringify(filtered),
      })
      .returning();
    if (row) {
      try {
        await db.insert(appEvents).values({
          appId: app.id,
          installationId: row.id,
          kind: "installed",
          payload: JSON.stringify({ agent: agentSlug, repoId }),
        });
      } catch {
        /* audit best-effort */
      }
    }
    return row || null;
  } catch (err) {
    console.error("[agent-identity] installAgentForRepo:", err);
    return null;
  }
}

/**
 * Mint a short-lived bearer for an agent scoped to a single repo.
 * Reuses marketplace.ts's `issueInstallToken` — we do not duplicate
 * token generation here.
 */
export async function issueAgentToken(
  agentSlug: string,
  repoId: string,
  ttlSeconds = 3600
): Promise<{ token: string; expiresAt: Date } | null> {
  try {
    const [app] = await db
      .select()
      .from(apps)
      .where(eq(apps.slug, agentSlug))
      .limit(1);
    if (!app) return null;
    const [install] = await db
      .select()
      .from(appInstallations)
      .where(
        and(
          eq(appInstallations.appId, app.id),
          eq(appInstallations.targetType, "repository"),
          eq(appInstallations.targetId, repoId),
          isNull(appInstallations.uninstalledAt)
        )
      )
      .limit(1);
    if (!install) return null;
    return await issueInstallToken(install.id, ttlSeconds);
  } catch (err) {
    console.error("[agent-identity] issueAgentToken:", err);
    return null;
  }
}

/** What `verifyAgentToken` returns when a token is valid + agent-shaped. */
export interface AgentTokenContext {
  agentSlug: string;
  repoId: string;
  botUsername: string;
  permissions: AgentPermission[];
  installationId: string;
}

/**
 * Verify a bearer. Returns null when:
 *  - the token is not `ghi_`-prefixed,
 *  - the underlying install-token is unknown / expired / revoked / uninstalled,
 *  - the bot behind the token is not an `agent-*` bot.
 */
export async function verifyAgentToken(
  token: string
): Promise<AgentTokenContext | null> {
  if (!token || !token.startsWith("ghi_")) return null;
  const ctx = await verifyInstallToken(token);
  if (!ctx) return null;
  if (!isAgentBotUsername(ctx.botUsername)) return null;
  if (ctx.installation.targetType !== "repository") return null;
  return {
    agentSlug: ctx.app.slug,
    repoId: ctx.installation.targetId,
    botUsername: ctx.botUsername,
    permissions: parseAgentPermissions(ctx.installation.grantedPermissions),
    installationId: ctx.installation.id,
  };
}

/**
 * Verify + assert a required permission. Returns the context on success,
 * throws `Error` on failure. Handlers should wrap with try/catch and map
 * to a 401 / 403 response.
 */
export async function requireAgentPermission(
  token: string,
  permission: AgentPermission | string
): Promise<AgentTokenContext> {
  const ctx = await verifyAgentToken(token);
  if (!ctx) throw new Error("agent token invalid or expired");
  if (!hasPermission(ctx.permissions as readonly string[], permission)) {
    throw new Error(`agent token missing permission: ${permission}`);
  }
  return ctx;
}

/** Revoke a single token by its stored hash. Idempotent. */
export async function revokeAgentToken(tokenHash: string): Promise<boolean> {
  if (!tokenHash) return false;
  try {
    const [row] = await db
      .update(appInstallTokens)
      .set({ revokedAt: new Date() })
      .where(
        and(
          eq(appInstallTokens.tokenHash, tokenHash),
          isNull(appInstallTokens.revokedAt)
        )
      )
      .returning();
    return !!row;
  } catch (err) {
    console.error("[agent-identity] revokeAgentToken:", err);
    return false;
  }
}

/** Convenience: hash + revoke in one go (when you only have the raw token). */
export async function revokeAgentTokenByRaw(token: string): Promise<boolean> {
  if (!token) return false;
  return revokeAgentToken(hashBearer(token));
}

/**
 * Soft-uninstall an agent for a repo. Sets `uninstalledAt` and revokes any
 * outstanding tokens for that installation. Idempotent — returns false if
 * no matching install was found.
 */
export async function uninstallAgent(
  agentSlug: string,
  repoId: string
): Promise<boolean> {
  try {
    const [app] = await db
      .select()
      .from(apps)
      .where(eq(apps.slug, agentSlug))
      .limit(1);
    if (!app) return false;
    const [updated] = await db
      .update(appInstallations)
      .set({ uninstalledAt: new Date() })
      .where(
        and(
          eq(appInstallations.appId, app.id),
          eq(appInstallations.targetType, "repository"),
          eq(appInstallations.targetId, repoId),
          isNull(appInstallations.uninstalledAt)
        )
      )
      .returning();
    if (!updated) return false;
    // Revoke every live token.
    try {
      await db
        .update(appInstallTokens)
        .set({ revokedAt: new Date() })
        .where(
          and(
            eq(appInstallTokens.installationId, updated.id),
            isNull(appInstallTokens.revokedAt)
          )
        );
    } catch {
      /* best-effort */
    }
    try {
      await db.insert(appEvents).values({
        appId: app.id,
        installationId: updated.id,
        kind: "uninstalled",
        payload: JSON.stringify({ agent: agentSlug, repoId }),
      });
    } catch {
      /* audit best-effort */
    }
    return true;
  } catch (err) {
    console.error("[agent-identity] uninstallAgent:", err);
    return false;
  }
}

// Types re-exported for callers that only import from this module.
export type { Permission };