Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesPull RequestsActionsSecurityInsightsSettings
✨ AI
More
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
agents.tsx22.6 KB · 739 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/**
 * Block K8 — Agent inbox + controls UI.
 *
 *   GET  /:owner/:repo/agents                     list recent agent runs (owner or public)
 *   GET  /:owner/:repo/agents/:id                 detail view with log + kill button
 *   POST /:owner/:repo/agents/:id/kill            owner-only; flips status to killed
 *   GET  /:owner/:repo/settings/agents            owner-only; per-repo toggles + budgets
 *   POST /:owner/:repo/settings/agents            owner-only; upserts repo_agent_settings
 *   POST /:owner/:repo/settings/agents/pause      owner-only; toggles paused flag
 *   GET  /admin/agents                             site-admin; recent runs across all repos
 *   POST /admin/agents/pause-all                   site-admin; sets system flag agents_paused=true
 */

import { Hono } from "hono";
import { and, desc, eq, sql } from "drizzle-orm";
import { db } from "../db";
import { agentRuns, repositories, users } from "../db/schema";
import type { AgentKind, AgentRunStatus } from "../lib/agent-runtime";
import { killAgentRun } from "../lib/agent-runtime";
import { isSiteAdmin, setFlag } from "../lib/admin";
import { audit } from "../lib/notify";
import { Layout } from "../views/layout";
import { RepoHeader, RepoNav } from "../views/components";
import { softAuth, requireAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";

const AGENT_KINDS: AgentKind[] = [
  "triage",
  "fix",
  "review_response",
  "deploy_watcher",
  "heal_bot",
  "custom",
];

export interface AgentSettingsInput {
  enabled_kinds?: string | string[];
  daily_budget_cents?: string;
  monthly_budget_cents?: string;
  max_runs_per_hour?: string;
  paused?: string;
}

export interface ParsedAgentSettings {
  enabledKinds: AgentKind[];
  dailyBudgetCents: number;
  monthlyBudgetCents: number;
  maxRunsPerHour: number;
  paused: boolean;
}

/** Pure form parser — defensive coercion + allowlist filtering. */
export function parseAgentSettingsForm(
  input: AgentSettingsInput
): ParsedAgentSettings {
  const rawKinds = input.enabled_kinds;
  const kindsArray = Array.isArray(rawKinds)
    ? rawKinds
    : typeof rawKinds === "string" && rawKinds.length > 0
    ? rawKinds.split(",")
    : [];
  const enabledKinds = kindsArray
    .map((k) => String(k).trim())
    .filter((k): k is AgentKind =>
      AGENT_KINDS.includes(k as AgentKind)
    );

  const clampInt = (v: string | undefined, def: number, max: number) => {
    const n = Number.parseInt(String(v ?? ""), 10);
    if (!Number.isFinite(n) || n < 0) return def;
    return Math.min(n, max);
  };

  return {
    enabledKinds,
    dailyBudgetCents: clampInt(input.daily_budget_cents, 100, 1_000_000),
    monthlyBudgetCents: clampInt(input.monthly_budget_cents, 2000, 50_000_000),
    maxRunsPerHour: clampInt(input.max_runs_per_hour, 20, 1000),
    paused: input.paused === "on" || input.paused === "true",
  };
}

async function loadRepo(ownerName: string, repoName: string) {
  try {
    const [row] = await db
      .select({
        id: repositories.id,
        name: repositories.name,
        ownerId: repositories.ownerId,
        isPrivate: repositories.isPrivate,
        starCount: repositories.starCount,
        forkCount: repositories.forkCount,
      })
      .from(repositories)
      .innerJoin(users, eq(repositories.ownerId, users.id))
      .where(
        and(eq(users.username, ownerName), eq(repositories.name, repoName))
      )
      .limit(1);
    return row || null;
  } catch {
    return null;
  }
}

async function loadSettings(repoId: string): Promise<ParsedAgentSettings> {
  try {
    const result = await db.execute(sql`
      SELECT enabled_kinds, daily_budget_cents, monthly_budget_cents,
             max_runs_per_hour, paused
      FROM repo_agent_settings
      WHERE repository_id = ${repoId}
      LIMIT 1
    `);
    const row = (result as unknown as { rows?: unknown[] }).rows?.[0] as
      | Record<string, unknown>
      | undefined;
    if (!row) {
      return {
        enabledKinds: [],
        dailyBudgetCents: 100,
        monthlyBudgetCents: 2000,
        maxRunsPerHour: 20,
        paused: false,
      };
    }
    let enabledKinds: AgentKind[] = [];
    try {
      const parsed = JSON.parse(String(row.enabled_kinds ?? "[]"));
      if (Array.isArray(parsed)) {
        enabledKinds = parsed.filter((k): k is AgentKind =>
          AGENT_KINDS.includes(k as AgentKind)
        );
      }
    } catch {
      /* keep empty */
    }
    return {
      enabledKinds,
      dailyBudgetCents: Number(row.daily_budget_cents ?? 100),
      monthlyBudgetCents: Number(row.monthly_budget_cents ?? 2000),
      maxRunsPerHour: Number(row.max_runs_per_hour ?? 20),
      paused: row.paused === true || row.paused === "t",
    };
  } catch {
    return {
      enabledKinds: [],
      dailyBudgetCents: 100,
      monthlyBudgetCents: 2000,
      maxRunsPerHour: 20,
      paused: false,
    };
  }
}

async function upsertSettings(repoId: string, s: ParsedAgentSettings) {
  await db.execute(sql`
    INSERT INTO repo_agent_settings
      (repository_id, enabled_kinds, daily_budget_cents,
       monthly_budget_cents, max_runs_per_hour, paused, updated_at)
    VALUES
      (${repoId}, ${JSON.stringify(s.enabledKinds)}, ${s.dailyBudgetCents},
       ${s.monthlyBudgetCents}, ${s.maxRunsPerHour}, ${s.paused}, now())
    ON CONFLICT (repository_id) DO UPDATE SET
      enabled_kinds = EXCLUDED.enabled_kinds,
      daily_budget_cents = EXCLUDED.daily_budget_cents,
      monthly_budget_cents = EXCLUDED.monthly_budget_cents,
      max_runs_per_hour = EXCLUDED.max_runs_per_hour,
      paused = EXCLUDED.paused,
      updated_at = now()
  `);
}

function statusClass(s: string): string {
  if (s === "succeeded") return "gate-status passed";
  if (s === "failed" || s === "timeout" || s === "killed")
    return "gate-status failed";
  if (s === "running") return "gate-status running";
  return "gate-status pending";
}

function formatCost(cents: number): string {
  return `$${(cents / 100).toFixed(2)}`;
}

function triggerLink(
  owner: string,
  repo: string,
  kind: string,
  ref: string | null
): string | null {
  if (!ref) return null;
  if (kind === "triage" || kind === "fix" || kind === "review_response") {
    const n = ref.split(":")[0];
    if (/^\d+$/.test(n)) return `/${owner}/${repo}/pulls/${n}`;
  }
  return null;
}

const agents = new Hono<AuthEnv>();
agents.use("*", softAuth);

// ---------- per-repo inbox ----------

agents.get("/:owner/:repo/agents", async (c) => {
  const user = c.get("user");
  const { owner, repo } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();
  if (repoRow.isPrivate && (!user || user.id !== repoRow.ownerId)) {
    return c.redirect("/login");
  }

  const kindFilter = c.req.query("kind") as AgentKind | undefined;
  const statusFilter = c.req.query("status") as AgentRunStatus | undefined;

  const conditions = [eq(agentRuns.repositoryId, repoRow.id)];
  if (kindFilter && AGENT_KINDS.includes(kindFilter)) {
    conditions.push(eq(agentRuns.kind, kindFilter));
  }
  if (statusFilter) {
    conditions.push(eq(agentRuns.status, statusFilter));
  }

  const runs = await db
    .select()
    .from(agentRuns)
    .where(and(...conditions))
    .orderBy(desc(agentRuns.createdAt))
    .limit(200);

  return c.html(
    <Layout title={`Agents — ${owner}/${repo}`} user={user ?? undefined}>
      <RepoHeader
        owner={owner}
        repo={repo}
        starCount={repoRow.starCount}
        forkCount={repoRow.forkCount}
        currentUser={user?.username}
      />
      <RepoNav owner={owner} repo={repo} active="agents" />

      <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px">
        <h3>Agent runs</h3>
        {user && user.id === repoRow.ownerId && (
          <a
            href={`/${owner}/${repo}/settings/agents`}
            class="btn btn-sm"
          >
            Settings
          </a>
        )}
      </div>

      <form method="GET" style="margin-bottom:16px;display:flex;gap:8px">
        <select name="kind">
          <option value="">All kinds</option>
          {AGENT_KINDS.map((k) => (
            <option value={k} selected={k === kindFilter}>
              {k}
            </option>
          ))}
        </select>
        <select name="status">
          <option value="">All statuses</option>
          {["queued", "running", "succeeded", "failed", "killed", "timeout"].map(
            (s) => (
              <option value={s} selected={s === statusFilter}>
                {s}
              </option>
            )
          )}
        </select>
        <button type="submit" class="btn btn-sm">
          Filter
        </button>
      </form>

      <div class="panel">
        {runs.length === 0 ? (
          <div class="panel-empty">No agent runs yet.</div>
        ) : (
          <table style="width:100%;font-size:13px">
            <thead>
              <tr>
                <th style="text-align:left;padding:8px">Kind</th>
                <th style="text-align:left;padding:8px">Trigger</th>
                <th style="text-align:left;padding:8px">Status</th>
                <th style="text-align:left;padding:8px">Ref</th>
                <th style="text-align:right;padding:8px">Cost</th>
                <th style="text-align:left;padding:8px">Started</th>
                <th></th>
              </tr>
            </thead>
            <tbody>
              {runs.map((r) => {
                const link = triggerLink(owner, repo, r.kind, r.triggerRef);
                return (
                  <tr>
                    <td style="padding:8px">
                      <code>{r.kind}</code>
                    </td>
                    <td style="padding:8px">{r.trigger}</td>
                    <td style="padding:8px">
                      <span class={statusClass(r.status)}>{r.status}</span>
                    </td>
                    <td style="padding:8px">
                      {link ? (
                        <a href={link}>{r.triggerRef}</a>
                      ) : (
                        r.triggerRef || "—"
                      )}
                    </td>
                    <td style="padding:8px;text-align:right">
                      {formatCost(r.costCents)}
                    </td>
                    <td style="padding:8px">
                      {r.createdAt?.toISOString().slice(0, 19) || "—"}
                    </td>
                    <td style="padding:8px">
                      <a
                        href={`/${owner}/${repo}/agents/${r.id}`}
                        class="btn btn-sm"
                      >
                        view
                      </a>
                    </td>
                  </tr>
                );
              })}
            </tbody>
          </table>
        )}
      </div>
    </Layout>
  );
});

agents.get("/:owner/:repo/agents/:id", async (c) => {
  const user = c.get("user");
  const { owner, repo, id } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();
  if (repoRow.isPrivate && (!user || user.id !== repoRow.ownerId)) {
    return c.redirect("/login");
  }

  const [run] = await db
    .select()
    .from(agentRuns)
    .where(
      and(eq(agentRuns.id, id), eq(agentRuns.repositoryId, repoRow.id))
    )
    .limit(1);
  if (!run) return c.notFound();

  const isOwner = user?.id === repoRow.ownerId;
  const canKill =
    isOwner && (run.status === "queued" || run.status === "running");

  const durationMs =
    run.startedAt && run.finishedAt
      ? run.finishedAt.getTime() - run.startedAt.getTime()
      : null;

  const link = triggerLink(owner, repo, run.kind, run.triggerRef);

  return c.html(
    <Layout title={`Agent run ${run.id}`} user={user ?? undefined}>
      <RepoHeader
        owner={owner}
        repo={repo}
        starCount={repoRow.starCount}
        forkCount={repoRow.forkCount}
        currentUser={user?.username}
      />
      <RepoNav owner={owner} repo={repo} active="agents" />

      <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px">
        <h3>
          Agent run · <code>{run.kind}</code>
        </h3>
        <a href={`/${owner}/${repo}/agents`} class="btn btn-sm">
          Back
        </a>
      </div>

      <div class="panel" style="padding:16px;margin-bottom:16px">
        <div>
          Status: <span class={statusClass(run.status)}>{run.status}</span>
        </div>
        <div>Trigger: {run.trigger}</div>
        {run.triggerRef && (
          <div>
            Ref: {link ? <a href={link}>{run.triggerRef}</a> : run.triggerRef}
          </div>
        )}
        {run.summary && <div>Summary: {run.summary}</div>}
        <div>
          Cost: {formatCost(run.costCents)} ({run.costInputTokens} in /{" "}
          {run.costOutputTokens} out tokens)
        </div>
        {durationMs !== null && (
          <div>Duration: {(durationMs / 1000).toFixed(1)}s</div>
        )}
        {run.errorMessage && (
          <div style="color:var(--red);margin-top:8px">
            <strong>Error:</strong>
            <pre style="white-space:pre-wrap;margin-top:4px">
              {run.errorMessage}
            </pre>
          </div>
        )}
      </div>

      {canKill && (
        <form
          method="POST"
          action={`/${owner}/${repo}/agents/${run.id}/kill`}
          style="margin-bottom:16px"
          onsubmit="return confirm('Kill this running agent?')"
        >
          <button type="submit" class="btn btn-danger">
            Kill run
          </button>
        </form>
      )}

      <div class="panel">
        <div class="panel-item" style="flex-direction:column;align-items:flex-start">
          <strong>Log</strong>
          <pre style="white-space:pre-wrap;font-size:12px;width:100%;margin-top:8px;max-height:400px;overflow:auto">
            {run.log || "(empty)"}
          </pre>
        </div>
      </div>
    </Layout>
  );
});

agents.post("/:owner/:repo/agents/:id/kill", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { owner, repo, id } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();
  if (repoRow.ownerId !== user.id) {
    return c.redirect(`/${owner}/${repo}/agents`);
  }
  await killAgentRun(id);
  await audit({
    userId: user.id,
    repositoryId: repoRow.id,
    action: "agent.kill",
    targetId: id,
  });
  return c.redirect(`/${owner}/${repo}/agents/${id}`);
});

// ---------- per-repo settings ----------

agents.get("/:owner/:repo/settings/agents", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { owner, repo } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();
  if (repoRow.ownerId !== user.id) {
    return c.redirect(`/${owner}/${repo}`);
  }
  const settings = await loadSettings(repoRow.id);

  return c.html(
    <Layout title={`Agent settings — ${repo}`} user={user}>
      <RepoHeader
        owner={owner}
        repo={repo}
        starCount={repoRow.starCount}
        forkCount={repoRow.forkCount}
        currentUser={user.username}
      />
      <RepoNav owner={owner} repo={repo} active="agents" />

      <h3>Agent settings</h3>
      <p style="font-size:13px;color:var(--text-muted);margin-bottom:16px">
        Enable specific agent kinds for this repository and set budget caps.
        Pausing stops all scheduled agents immediately.
      </p>

      <form
        method="POST"
        action={`/${owner}/${repo}/settings/agents`}
        class="panel"
        style="padding:16px"
      >
        <div class="form-group">
          <label>
            <strong>Enabled kinds</strong>
          </label>
          {AGENT_KINDS.map((k) => (
            <label style="display:block;margin:4px 0">
              <input
                type="checkbox"
                name="enabled_kinds"
                value={k}
                checked={settings.enabledKinds.includes(k)}
              />{" "}
              <code>{k}</code>
            </label>
          ))}
        </div>
        <div class="form-group">
          <label>Daily budget (cents)</label>
          <input
            type="number"
            name="daily_budget_cents"
            value={String(settings.dailyBudgetCents)}
            min="0"
            max="1000000"
          />
        </div>
        <div class="form-group">
          <label>Monthly budget (cents)</label>
          <input
            type="number"
            name="monthly_budget_cents"
            value={String(settings.monthlyBudgetCents)}
            min="0"
            max="50000000"
          />
        </div>
        <div class="form-group">
          <label>Max runs per hour</label>
          <input
            type="number"
            name="max_runs_per_hour"
            value={String(settings.maxRunsPerHour)}
            min="0"
            max="1000"
          />
        </div>
        <div class="form-group">
          <label>
            <input
              type="checkbox"
              name="paused"
              checked={settings.paused}
            />{" "}
            Paused (all agents disabled)
          </label>
        </div>
        <button type="submit" class="btn btn-primary">
          Save settings
        </button>
      </form>

      <form
        method="POST"
        action={`/${owner}/${repo}/settings/agents/pause`}
        style="margin-top:16px"
      >
        <button type="submit" class="btn btn-sm">
          Toggle paused
        </button>
      </form>
    </Layout>
  );
});

agents.post("/:owner/:repo/settings/agents", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { owner, repo } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();
  if (repoRow.ownerId !== user.id) {
    return c.redirect(`/${owner}/${repo}`);
  }
  const body = await c.req.parseBody({ all: true });
  const parsed = parseAgentSettingsForm(
    body as unknown as AgentSettingsInput
  );

  try {
    await upsertSettings(repoRow.id, parsed);
  } catch (err) {
    console.error("[agents] upsertSettings:", err);
  }

  await audit({
    userId: user.id,
    repositoryId: repoRow.id,
    action: "agent.settings.update",
    metadata: {
      enabledKinds: parsed.enabledKinds,
      daily: parsed.dailyBudgetCents,
      monthly: parsed.monthlyBudgetCents,
    },
  });

  return c.redirect(`/${owner}/${repo}/settings/agents`);
});

agents.post("/:owner/:repo/settings/agents/pause", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { owner, repo } = c.req.param();
  const repoRow = await loadRepo(owner, repo);
  if (!repoRow) return c.notFound();
  if (repoRow.ownerId !== user.id) {
    return c.redirect(`/${owner}/${repo}`);
  }
  const cur = await loadSettings(repoRow.id);
  try {
    await upsertSettings(repoRow.id, { ...cur, paused: !cur.paused });
  } catch (err) {
    console.error("[agents] pause:", err);
  }
  await audit({
    userId: user.id,
    repositoryId: repoRow.id,
    action: "agent.settings.pause",
    metadata: { paused: !cur.paused },
  });
  return c.redirect(`/${owner}/${repo}/settings/agents`);
});

// ---------- site-admin ----------

agents.get("/admin/agents", requireAuth, async (c) => {
  const user = c.get("user")!;
  if (!(await isSiteAdmin(user.id))) return c.redirect("/");

  const runs = await db
    .select({
      id: agentRuns.id,
      kind: agentRuns.kind,
      trigger: agentRuns.trigger,
      status: agentRuns.status,
      costCents: agentRuns.costCents,
      createdAt: agentRuns.createdAt,
      repoId: agentRuns.repositoryId,
      repoName: repositories.name,
      ownerUsername: users.username,
    })
    .from(agentRuns)
    .leftJoin(repositories, eq(repositories.id, agentRuns.repositoryId))
    .leftJoin(users, eq(users.id, repositories.ownerId))
    .orderBy(desc(agentRuns.createdAt))
    .limit(500);

  const todayCostCents = runs
    .filter((r) => {
      const d = r.createdAt ? new Date(r.createdAt) : null;
      if (!d) return false;
      const dayAgo = Date.now() - 24 * 60 * 60 * 1000;
      return d.getTime() >= dayAgo;
    })
    .reduce((sum, r) => sum + (r.costCents || 0), 0);

  return c.html(
    <Layout title="Admin — Agents" user={user}>
      <h2>Admin · Agents</h2>
      <div class="panel" style="padding:16px;margin-bottom:16px">
        <div>
          <strong>24h spend:</strong> {formatCost(todayCostCents)}
        </div>
        <div>
          <strong>Recent runs:</strong> {runs.length}
        </div>
      </div>

      <form
        method="POST"
        action="/admin/agents/pause-all"
        style="margin-bottom:16px"
        onsubmit="return confirm('Pause ALL agents platform-wide?')"
      >
        <button type="submit" class="btn btn-danger">
          Pause all agents (platform kill-switch)
        </button>
      </form>

      <div class="panel">
        {runs.length === 0 ? (
          <div class="panel-empty">No agent runs.</div>
        ) : (
          <table style="width:100%;font-size:13px">
            <thead>
              <tr>
                <th style="text-align:left;padding:8px">Repo</th>
                <th style="text-align:left;padding:8px">Kind</th>
                <th style="text-align:left;padding:8px">Status</th>
                <th style="text-align:right;padding:8px">Cost</th>
                <th style="text-align:left;padding:8px">Created</th>
              </tr>
            </thead>
            <tbody>
              {runs.map((r) => (
                <tr>
                  <td style="padding:8px">
                    {r.ownerUsername && r.repoName ? (
                      <a href={`/${r.ownerUsername}/${r.repoName}/agents/${r.id}`}>
                        {r.ownerUsername}/{r.repoName}
                      </a>
                    ) : (
                      "—"
                    )}
                  </td>
                  <td style="padding:8px">
                    <code>{r.kind}</code>
                  </td>
                  <td style="padding:8px">
                    <span class={statusClass(r.status)}>{r.status}</span>
                  </td>
                  <td style="padding:8px;text-align:right">
                    {formatCost(r.costCents || 0)}
                  </td>
                  <td style="padding:8px">
                    {r.createdAt?.toISOString().slice(0, 19) || "—"}
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        )}
      </div>
    </Layout>
  );
});

agents.post("/admin/agents/pause-all", requireAuth, async (c) => {
  const user = c.get("user")!;
  if (!(await isSiteAdmin(user.id))) return c.redirect("/");
  await setFlag("agents_paused", "true", user.id);
  await audit({
    userId: user.id,
    action: "admin.agents.pause_all",
  });
  return c.redirect("/admin/agents");
});

export default agents;