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
admin.tsx24.4 KB · 788 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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/**
 * Block F3 — Site admin panel.
 *
 *   GET  /admin                           — dashboard (counts + recent users)
 *   GET  /admin/users                     — user list + search
 *   POST /admin/users/:id/admin           — toggle site-admin flag
 *   GET  /admin/repos                     — repo list (including private)
 *   POST /admin/repos/:id/delete          — nuclear delete (audit-logged)
 *   GET  /admin/flags                     — site flags CRUD
 *   POST /admin/flags                     — set flag
 *
 * All routes gated by `isSiteAdmin`. First registered user is the bootstrap
 * admin. Site banner + registration lock are surfaced to the rest of the app
 * via `getFlag`.
 */

import { Hono } from "hono";
import { and, desc, eq, ilike, or, sql } from "drizzle-orm";
import { db } from "../db";
import { repositories, users } from "../db/schema";
import { Layout } from "../views/layout";
import { softAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import {
  grantSiteAdmin,
  isSiteAdmin,
  KNOWN_FLAGS,
  listFlags,
  listSiteAdmins,
  revokeSiteAdmin,
  setFlag,
} from "../lib/admin";
import { audit } from "../lib/notify";
import { sendDigestsToAll, sendDigestForUser } from "../lib/email-digest";
import {
  getLastTick,
  getTickCount,
  runAutopilotTick,
} from "../lib/autopilot";
import { ensureDemoContent, DEMO_USERNAME } from "../lib/demo-seed";

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

async function gate(c: any): Promise<{ user: any } | Response> {
  const user = c.get("user");
  if (!user) return c.redirect("/login?next=/admin");
  if (!(await isSiteAdmin(user.id))) {
    return c.html(
      <Layout title="Forbidden" user={user}>
        <div class="empty-state">
          <h2>403 — Not a site admin</h2>
          <p>You don't have permission to view this page.</p>
        </div>
      </Layout>,
      403
    );
  }
  return { user };
}

admin.get("/admin", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;

  const [uc] = await db.select({ n: sql<number>`count(*)::int` }).from(users);
  const [rc] = await db
    .select({ n: sql<number>`count(*)::int` })
    .from(repositories);

  const recent = await db
    .select({
      id: users.id,
      username: users.username,
      createdAt: users.createdAt,
    })
    .from(users)
    .orderBy(desc(users.createdAt))
    .limit(10);

  const admins = await listSiteAdmins();

  const msg = c.req.query("result") || c.req.query("error");
  const isErr = !!c.req.query("error");

  return c.html(
    <Layout title="Admin — Gluecron" user={user}>
      <h2>Site admin</h2>

      {msg && (
        <div
          class={isErr ? "auth-error" : "banner"}
          style="margin-bottom:16px"
        >
          {decodeURIComponent(msg)}
        </div>
      )}

      <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin-bottom:20px">
        <div class="panel" style="padding:12px;text-align:center">
          <div style="font-size:22px;font-weight:700">{Number(uc?.n || 0)}</div>
          <div style="font-size:11px;color:var(--text-muted);text-transform:uppercase">
            Users
          </div>
        </div>
        <div class="panel" style="padding:12px;text-align:center">
          <div style="font-size:22px;font-weight:700">{Number(rc?.n || 0)}</div>
          <div style="font-size:11px;color:var(--text-muted);text-transform:uppercase">
            Repos
          </div>
        </div>
        <div class="panel" style="padding:12px;text-align:center">
          <div style="font-size:22px;font-weight:700">{admins.length}</div>
          <div style="font-size:11px;color:var(--text-muted);text-transform:uppercase">
            Site admins
          </div>
        </div>
      </div>

      <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:8px;margin-bottom:20px">
        <a href="/admin/users" class="btn">
          Manage users
        </a>
        <a href="/admin/repos" class="btn">
          Manage repos
        </a>
        <a href="/admin/flags" class="btn">
          Site flags
        </a>
        <a href="/admin/digests" class="btn">
          Email digests
        </a>
        <a href="/admin/sso" class="btn">
          Enterprise SSO
        </a>
        <a href="/admin/autopilot" class="btn">
          Autopilot
        </a>
        <form
          method="post"
          action="/admin/demo/reseed"
          style="display:contents"
        >
          <button class="btn" type="submit" title="Idempotently (re)create demo user + 3 sample repos">
            Reseed demo
          </button>
        </form>
      </div>

      <h3>Recent signups</h3>
      <div class="panel" style="margin-bottom:20px">
        {recent.map((u) => (
          <div class="panel-item" style="justify-content:space-between">
            <a href={`/${u.username}`}>{u.username}</a>
            <span style="font-size:12px;color:var(--text-muted)">
              {u.createdAt
                ? new Date(u.createdAt as unknown as string).toLocaleString()
                : ""}
            </span>
          </div>
        ))}
      </div>

      <h3>Site admins</h3>
      <div class="panel">
        {admins.length === 0 ? (
          <div class="panel-empty">
            No admins (bootstrap mode — oldest user is admin).
          </div>
        ) : (
          admins.map((a) => (
            <div class="panel-item" style="justify-content:space-between">
              <a href={`/${a.username}`}>{a.username}</a>
              <span style="font-size:12px;color:var(--text-muted)">
                Granted{" "}
                {a.grantedAt
                  ? new Date(a.grantedAt as unknown as string).toLocaleDateString()
                  : ""}
              </span>
            </div>
          ))
        )}
      </div>
    </Layout>
  );
});

// ----- Users -----

admin.get("/admin/users", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const q = c.req.query("q") || "";
  const rows = await db
    .select({
      id: users.id,
      username: users.username,
      email: users.email,
      createdAt: users.createdAt,
    })
    .from(users)
    .where(
      q
        ? or(ilike(users.username, `%${q}%`), ilike(users.email, `%${q}%`))!
        : sql`1=1`
    )
    .orderBy(desc(users.createdAt))
    .limit(200);

  const adminIds = new Set((await listSiteAdmins()).map((a) => a.userId));

  return c.html(
    <Layout title="Admin — Users" user={user}>
      <h2>Users</h2>
      <form method="get" action="/admin/users" style="margin-bottom:16px">
        <input
          type="text"
          name="q"
          value={q}
          placeholder="Search username or email"
          style="width:320px"
        />{" "}
        <button type="submit" class="btn">
          Search
        </button>
        <a href="/admin" class="btn" style="margin-left:6px">
          Back
        </a>
      </form>
      <div class="panel">
        {rows.length === 0 ? (
          <div class="panel-empty">No users found.</div>
        ) : (
          rows.map((u) => {
            const isAdmin = adminIds.has(u.id);
            return (
              <div class="panel-item" style="justify-content:space-between">
                <div>
                  <a href={`/${u.username}`} style="font-weight:600">
                    {u.username}
                  </a>{" "}
                  <span style="color:var(--text-muted)">{u.email}</span>
                  {isAdmin && (
                    <span
                      style="margin-left:6px;font-size:11px;background:#8957e5;color:white;padding:2px 6px;border-radius:3px"
                    >
                      ADMIN
                    </span>
                  )}
                </div>
                <form
                  method="post"
                  action={`/admin/users/${u.id}/admin`}
                  onsubmit={
                    isAdmin
                      ? "return confirm('Revoke site admin?')"
                      : "return confirm('Grant site admin?')"
                  }
                >
                  <button type="submit" class="btn btn-sm">
                    {isAdmin ? "Revoke admin" : "Grant admin"}
                  </button>
                </form>
              </div>
            );
          })
        )}
      </div>
    </Layout>
  );
});

admin.post("/admin/users/:id/admin", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const id = c.req.param("id");
  const admins = await listSiteAdmins();
  const isAlready = admins.some((a) => a.userId === id);
  if (isAlready) {
    await revokeSiteAdmin(id);
    await audit({
      userId: user.id,
      action: "site_admin.revoke",
      targetType: "user",
      targetId: id,
    });
  } else {
    await grantSiteAdmin(id, user.id);
    await audit({
      userId: user.id,
      action: "site_admin.grant",
      targetType: "user",
      targetId: id,
    });
  }
  return c.redirect("/admin/users");
});

// ----- Repos -----

admin.get("/admin/repos", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const rows = await db
    .select({
      id: repositories.id,
      name: repositories.name,
      ownerUsername: users.username,
      isPrivate: repositories.isPrivate,
      createdAt: repositories.createdAt,
      starCount: repositories.starCount,
    })
    .from(repositories)
    .innerJoin(users, eq(repositories.ownerId, users.id))
    .orderBy(desc(repositories.createdAt))
    .limit(200);

  return c.html(
    <Layout title="Admin — Repos" user={user}>
      <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px">
        <h2>Repositories</h2>
        <a href="/admin" class="btn btn-sm">
          Back
        </a>
      </div>
      <div class="panel">
        {rows.length === 0 ? (
          <div class="panel-empty">No repositories.</div>
        ) : (
          rows.map((r) => (
            <div class="panel-item" style="justify-content:space-between">
              <div>
                <a
                  href={`/${r.ownerUsername}/${r.name}`}
                  style="font-weight:600"
                >
                  {r.ownerUsername}/{r.name}
                </a>
                <span
                  style="margin-left:6px;font-size:11px;color:var(--text-muted);text-transform:uppercase"
                >
                  {r.isPrivate ? "private" : "public"}
                </span>
                <div style="font-size:12px;color:var(--text-muted);margin-top:2px">
                  {r.starCount} stars ·{" "}
                  {r.createdAt
                    ? new Date(r.createdAt as unknown as string).toLocaleDateString()
                    : ""}
                </div>
              </div>
              <form
                method="post"
                action={`/admin/repos/${r.id}/delete`}
                onsubmit="return confirm('Delete repository permanently? This cannot be undone.')"
              >
                <button type="submit" class="btn btn-sm btn-danger">
                  Delete
                </button>
              </form>
            </div>
          ))
        )}
      </div>
    </Layout>
  );
});

admin.post("/admin/repos/:id/delete", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const id = c.req.param("id");
  try {
    await db.delete(repositories).where(eq(repositories.id, id));
  } catch (err) {
    console.error("[admin] repo delete:", err);
  }
  await audit({
    userId: user.id,
    action: "admin.repo.delete",
    targetType: "repository",
    targetId: id,
  });
  return c.redirect("/admin/repos");
});

// ----- Flags -----

admin.get("/admin/flags", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;

  const existing = await listFlags();
  const existingMap = new Map(existing.map((f) => [f.key, f.value]));
  const keys = Object.keys(KNOWN_FLAGS) as Array<keyof typeof KNOWN_FLAGS>;

  return c.html(
    <Layout title="Admin — Flags" user={user}>
      <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px">
        <h2>Site flags</h2>
        <a href="/admin" class="btn btn-sm">
          Back
        </a>
      </div>
      <form
        method="post"
        action="/admin/flags"
        class="panel"
        style="padding:16px"
      >
        {keys.map((k) => {
          const current = existingMap.get(k) ?? (KNOWN_FLAGS as any)[k];
          return (
            <div class="form-group">
              <label>{k}</label>
              <input
                type="text"
                name={k}
                value={current}
                style="font-family:var(--font-mono)"
              />
              <div
                style="font-size:11px;color:var(--text-muted);margin-top:2px"
              >
                default: <code>{(KNOWN_FLAGS as any)[k] || "(empty)"}</code>
              </div>
            </div>
          );
        })}
        <button type="submit" class="btn btn-primary">
          Save
        </button>
      </form>
    </Layout>
  );
});

admin.post("/admin/flags", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const body = await c.req.parseBody();
  const keys = Object.keys(KNOWN_FLAGS) as Array<keyof typeof KNOWN_FLAGS>;
  for (const k of keys) {
    const v = String(body[k] ?? "");
    await setFlag(k, v, user.id);
  }
  await audit({ userId: user.id, action: "admin.flags.save" });
  return c.redirect("/admin/flags");
});

// ----- Email digests (Block I7) -----

admin.get("/admin/digests", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;

  const [optedRow] = await db
    .select({ n: sql<number>`count(*)::int` })
    .from(users)
    .where(eq(users.notifyEmailDigestWeekly, true));
  const opted = Number(optedRow?.n || 0);

  const recentlySent = await db
    .select({
      id: users.id,
      username: users.username,
      lastDigestSentAt: users.lastDigestSentAt,
    })
    .from(users)
    .where(sql`${users.lastDigestSentAt} is not null`)
    .orderBy(desc(users.lastDigestSentAt))
    .limit(20);

  const result = c.req.query("result");
  const error = c.req.query("error");

  return c.html(
    <Layout title="Admin — Digests" user={user}>
      <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px">
        <h2>Email digests</h2>
        <a href="/admin" class="btn btn-sm">
          Back
        </a>
      </div>

      {result && (
        <div class="auth-success">{decodeURIComponent(result)}</div>
      )}
      {error && (
        <div class="auth-error">{decodeURIComponent(error)}</div>
      )}

      <div class="panel" style="padding:16px;margin-bottom:20px">
        <div style="font-size:13px;color:var(--text-muted);margin-bottom:8px">
          {opted} user{opted === 1 ? "" : "s"} opted into the weekly digest.
        </div>
        <form method="post" action="/admin/digests/run" style="margin-bottom:8px">
          <button
            type="submit"
            class="btn btn-primary"
            onclick="return confirm('Send weekly digest to all opted-in users now?')"
          >
            Send digests now
          </button>
        </form>
        <form method="post" action="/admin/digests/preview" style="display:flex;gap:6px;align-items:center">
          <input
            type="text"
            name="username"
            placeholder="username"
            required
            style="width:240px"
          />
          <button type="submit" class="btn btn-sm">
            Send to one user
          </button>
        </form>
      </div>

      <h3>Recently sent</h3>
      <div class="panel">
        {recentlySent.length === 0 ? (
          <div class="panel-empty">No digests have been sent yet.</div>
        ) : (
          recentlySent.map((u) => (
            <div class="panel-item" style="justify-content:space-between">
              <a href={`/${u.username}`}>{u.username}</a>
              <span style="font-size:12px;color:var(--text-muted)">
                {u.lastDigestSentAt
                  ? new Date(
                      u.lastDigestSentAt as unknown as string
                    ).toLocaleString()
                  : ""}
              </span>
            </div>
          ))
        )}
      </div>
    </Layout>
  );
});

admin.post("/admin/digests/run", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const results = await sendDigestsToAll();
  const sent = results.filter((r) => r.ok).length;
  const skipped = results.length - sent;
  await audit({
    userId: user.id,
    action: "admin.digests.run",
    metadata: { sent, skipped, total: results.length },
  });
  return c.redirect(
    `/admin/digests?result=${encodeURIComponent(
      `Processed ${results.length} opted-in users: ${sent} sent, ${skipped} skipped.`
    )}`
  );
});

admin.post("/admin/digests/preview", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const body = await c.req.parseBody();
  const username = String(body.username || "").trim();
  if (!username) {
    return c.redirect("/admin/digests?error=Username+required");
  }
  const [target] = await db
    .select({ id: users.id, username: users.username })
    .from(users)
    .where(eq(users.username, username))
    .limit(1);
  if (!target) {
    return c.redirect("/admin/digests?error=User+not+found");
  }
  const result = await sendDigestForUser(target.id);
  await audit({
    userId: user.id,
    action: "admin.digests.preview",
    targetType: "user",
    targetId: target.id,
    metadata: {
      ok: result.ok,
      skipped: "skipped" in result ? result.skipped : null,
    },
  });
  if (result.ok) {
    return c.redirect(
      `/admin/digests?result=${encodeURIComponent(
        `Digest sent to ${target.username}.`
      )}`
    );
  }
  return c.redirect(
    `/admin/digests?error=${encodeURIComponent(
      `Not sent: ${"skipped" in result ? result.skipped : "unknown reason"}`
    )}`
  );
});

admin.get("/admin/autopilot", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  const tick = getLastTick();
  const total = getTickCount();
  const disabled = process.env.AUTOPILOT_DISABLED === "1";
  const intervalRaw = process.env.AUTOPILOT_INTERVAL_MS;
  const intervalMs =
    intervalRaw && Number.isFinite(Number(intervalRaw)) && Number(intervalRaw) > 0
      ? Number(intervalRaw)
      : 5 * 60 * 1000;
  const msg = c.req.query("result") || c.req.query("error");
  const isErr = !!c.req.query("error");
  return c.html(
    <Layout title="Autopilot — admin" user={user}>
      <div style="max-width: 960px; margin: 0 auto; padding: 24px 16px">
        <h1 style="margin-bottom: 8px">Autopilot</h1>
        <p style="color: var(--text-muted); margin-bottom: 24px">
          Periodic platform-maintenance loop — mirror sync, merge-queue
          progress, weekly digests, advisory rescans.
        </p>
        {msg && (
          <div
            class={isErr ? "auth-error" : "banner"}
            style="margin-bottom: 16px"
          >
            {decodeURIComponent(msg)}
          </div>
        )}
        <div
          style="display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 24px"
        >
          <div class="stat-card">
            <div class="stat-label">Status</div>
            <div class="stat-value">
              {disabled ? "disabled" : "running"}
            </div>
          </div>
          <div class="stat-card">
            <div class="stat-label">Interval</div>
            <div class="stat-value">{Math.round(intervalMs / 1000)}s</div>
          </div>
          <div class="stat-card">
            <div class="stat-label">Ticks this process</div>
            <div class="stat-value">{total}</div>
          </div>
          <div class="stat-card">
            <div class="stat-label">Last tick</div>
            <div class="stat-value" style="font-size: 14px">
              {tick ? tick.finishedAt : "never"}
            </div>
          </div>
        </div>
        <form
          method="post"
          action="/admin/autopilot/run"
          style="margin-bottom: 24px"
        >
          <button class="btn btn-primary" type="submit">
            Run tick now
          </button>
          <span style="color: var(--text-muted); margin-left: 12px; font-size: 13px">
            Executes all sub-tasks synchronously and records the result.
          </span>
        </form>
        <h2 style="margin-bottom: 12px">Last tick tasks</h2>
        {tick ? (
          <table class="table" style="width: 100%">
            <thead>
              <tr>
                <th style="text-align: left">Task</th>
                <th style="text-align: left">Status</th>
                <th style="text-align: right">Duration</th>
                <th style="text-align: left">Error</th>
              </tr>
            </thead>
            <tbody>
              {tick.tasks.map((t) => (
                <tr>
                  <td>
                    <code>{t.name}</code>
                  </td>
                  <td
                    style={
                      t.ok
                        ? "color: var(--green)"
                        : "color: var(--red)"
                    }
                  >
                    {t.ok ? "ok" : "failed"}
                  </td>
                  <td style="text-align: right">{t.durationMs}ms</td>
                  <td style="color: var(--text-muted); font-size: 13px">
                    {t.error || ""}
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        ) : (
          <p style="color: var(--text-muted)">
            No ticks have run yet. The first tick fires after the interval
            elapses. Click "Run tick now" to fire one immediately.
          </p>
        )}
        <p style="margin-top: 32px; color: var(--text-muted); font-size: 13px">
          Opt out with env <code>AUTOPILOT_DISABLED=1</code>. Adjust cadence
          with <code>AUTOPILOT_INTERVAL_MS</code> (milliseconds).
        </p>
      </div>
    </Layout>
  );
});

admin.post("/admin/demo/reseed", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  try {
    const result = await ensureDemoContent({ force: true });
    const summary = `Demo reseed: user=${result.created.user ? "created" : "existed"}, repos=${result.created.repos.length}, issues=${result.created.issues}, prs=${result.created.prs}${result.errors.length ? `, errors=${result.errors.length}` : ""}`;
    await audit({
      userId: user.id,
      action: "admin.demo.reseed",
      targetType: "user",
      targetId: result.demoUser?.id ?? "demo",
      metadata: {
        createdUser: result.created.user,
        createdRepos: result.created.repos,
        createdIssues: result.created.issues,
        createdPrs: result.created.prs,
        errors: result.errors.slice(0, 5),
      },
    });
    return c.redirect(`/admin?result=${encodeURIComponent(summary)}`);
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    return c.redirect(
      `/admin?error=${encodeURIComponent("Demo reseed failed: " + message)}`
    );
  }
});

// Public jump-to-demo — redirects to the first demo repo if present,
// otherwise to /explore. Useful as a landing-page-linkable "try it" URL.
admin.get("/demo", (c) => {
  return c.redirect(`/${DEMO_USERNAME}/hello-python`);
});

admin.post("/admin/autopilot/run", async (c) => {
  const g = await gate(c);
  if (g instanceof Response) return g;
  const { user } = g;
  let summary = "";
  try {
    const result = await runAutopilotTick();
    const ok = result.tasks.filter((t) => t.ok).length;
    summary = `Tick complete: ${ok}/${result.tasks.length} tasks ok.`;
    await audit({
      userId: user.id,
      action: "admin.autopilot.run",
      targetType: "system",
      targetId: "autopilot",
      metadata: { ok, total: result.tasks.length },
    });
    return c.redirect(
      `/admin/autopilot?result=${encodeURIComponent(summary)}`
    );
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    return c.redirect(
      `/admin/autopilot?error=${encodeURIComponent("Tick failed: " + message)}`
    );
  }
});

export default admin;