@app-vitals/agent-admin 1.43.0 → 1.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@app-vitals/agent-admin",
3
- "version": "1.43.0",
3
+ "version": "1.45.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@anthropic-ai/sdk": "0.135.0",
45
- "@app-vitals/agent-lib": "1.43.0",
45
+ "@app-vitals/agent-lib": "1.45.0",
46
46
  "@hono/zod-openapi": "^1.0.0",
47
47
  "@prisma/adapter-pg": "^7.10.0",
48
48
  "@prisma/client": "^7.10.0",
@@ -0,0 +1,77 @@
1
+ /**
2
+ * admin/src/admin-ui-settings-layout.ts
3
+ * Shared settings page layout (SGL-1.1): toolbar + left sidebar nav + content
4
+ * pane. Returns body markup — callers wrap it with renderAdminPage.
5
+ * Styles live in baseStyles() between the "Settings layout" markers.
6
+ */
7
+
8
+ import { escapeHtml, renderAdminToolbar } from "./admin-ui-styles.ts";
9
+
10
+ export interface SettingsNavItem {
11
+ label: string;
12
+ path: string;
13
+ adminOnly?: boolean;
14
+ }
15
+
16
+ export interface SettingsNavSection {
17
+ label: string;
18
+ items: SettingsNavItem[];
19
+ }
20
+
21
+ export interface SettingsNav {
22
+ sections: SettingsNavSection[];
23
+ }
24
+
25
+ export interface RenderSettingsLayoutOptions {
26
+ userName: string;
27
+ /** Raw title; rendered as the escaped H1 of the content pane. */
28
+ title: string;
29
+ nav: SettingsNav;
30
+ /** Path of the active nav item. */
31
+ active: string;
32
+ /** Pre-rendered content markup (caller-owned, not escaped). */
33
+ body: string;
34
+ /** Toolbar active path; defaults to `active`. */
35
+ toolbarPath?: string;
36
+ /** Show adminOnly items. Default false. */
37
+ isAdmin?: boolean;
38
+ }
39
+
40
+ export function renderSettingsLayout(
41
+ opts: RenderSettingsLayoutOptions,
42
+ ): string {
43
+ const isAdmin = opts.isAdmin ?? false;
44
+ const sections = opts.nav.sections
45
+ .map((s) => ({
46
+ label: s.label,
47
+ items: s.items.filter((i) => isAdmin || !i.adminOnly),
48
+ }))
49
+ .filter((s) => s.items.length > 0);
50
+
51
+ const link = (i: SettingsNavItem): string => {
52
+ const current = i.path === opts.active;
53
+ return `<a class="settings-nav-link" href="${escapeHtml(i.path)}"${current ? ' aria-current="page"' : ""}>${escapeHtml(i.label)}</a>`;
54
+ };
55
+ const sectionsHtml = sections
56
+ .map(
57
+ (s) =>
58
+ `<div class="settings-nav-section"><div class="settings-nav-label">${escapeHtml(s.label)}</div>${s.items.map(link).join("")}</div>`,
59
+ )
60
+ .join("");
61
+
62
+ const activeLabel =
63
+ sections.flatMap((s) => s.items).find((i) => i.path === opts.active)
64
+ ?.label ?? opts.title;
65
+
66
+ return `${renderAdminToolbar(opts.userName, opts.toolbarPath ?? opts.active)}
67
+ <div class="vos-page">
68
+ <div class="settings-layout">
69
+ <nav class="settings-sidebar" aria-label="Settings">${sectionsHtml}</nav>
70
+ <details class="settings-nav-mobile"><summary>${escapeHtml(activeLabel)}</summary>${sectionsHtml}</details>
71
+ <main class="settings-content">
72
+ <h1>${escapeHtml(opts.title)}</h1>
73
+ ${opts.body}
74
+ </main>
75
+ </div>
76
+ </div>`;
77
+ }
@@ -0,0 +1,106 @@
1
+ /**
2
+ * admin/src/admin-ui-settings-layout.unit.test.ts
3
+ * Pure string assertions for renderSettingsLayout (SGL-1.1).
4
+ */
5
+
6
+ import { describe, expect, test } from "bun:test";
7
+ import {
8
+ renderSettingsLayout,
9
+ type SettingsNav,
10
+ } from "./admin-ui-settings-layout.ts";
11
+
12
+ const nav: SettingsNav = {
13
+ sections: [
14
+ {
15
+ label: "Account",
16
+ items: [
17
+ { label: "Profile", path: "/admin/settings/profile" },
18
+ { label: "Tokens", path: "/admin/settings/tokens" },
19
+ ],
20
+ },
21
+ {
22
+ label: "Admin",
23
+ items: [
24
+ { label: "Users", path: "/admin/settings/users", adminOnly: true },
25
+ ],
26
+ },
27
+ ],
28
+ };
29
+
30
+ const render = (over: Record<string, unknown> = {}) =>
31
+ renderSettingsLayout({
32
+ userName: "dan",
33
+ title: "Profile",
34
+ nav,
35
+ active: "/admin/settings/profile",
36
+ body: "<p>BODY</p>",
37
+ ...over,
38
+ });
39
+
40
+ describe("renderSettingsLayout", () => {
41
+ test("marks the active item with aria-current and only that item", () => {
42
+ const html = render();
43
+ const matches = html.match(/aria-current="page"/g) ?? [];
44
+ // one in desktop nav, one in mobile details
45
+ expect(matches.length).toBe(2);
46
+ expect(html).toMatch(
47
+ /<a [^>]*href="\/admin\/settings\/profile"[^>]*aria-current="page"/,
48
+ );
49
+ expect(html).not.toMatch(
50
+ /href="\/admin\/settings\/tokens"[^>]*aria-current/,
51
+ );
52
+ });
53
+
54
+ test("renders section labels, title H1, body and toolbar", () => {
55
+ const html = render();
56
+ expect(html).toContain("Account");
57
+ expect(html).toContain("<h1");
58
+ expect(html).toContain("Profile</h1>");
59
+ expect(html).toContain("<p>BODY</p>");
60
+ expect(html).toContain('class="vos-page');
61
+ expect(html).toContain("vos-toolbar");
62
+ });
63
+
64
+ test("filters adminOnly items and drops empty sections unless isAdmin", () => {
65
+ const html = render();
66
+ expect(html).not.toContain("Users");
67
+ expect(html).not.toContain(">Admin<");
68
+ const admin = render({ isAdmin: true });
69
+ expect(admin).toContain("Users");
70
+ expect(admin).toContain(">Admin<");
71
+ });
72
+
73
+ test("escapes title, labels, paths and active", () => {
74
+ const evil: SettingsNav = {
75
+ sections: [
76
+ {
77
+ label: "<s>sec</s>",
78
+ items: [{ label: "<b>x</b>", path: '/a"onclick="y' }],
79
+ },
80
+ ],
81
+ };
82
+ const html = render({
83
+ title: "<script>t</script>",
84
+ nav: evil,
85
+ active: '/a"onclick="y',
86
+ });
87
+ expect(html).not.toContain("<script>t");
88
+ expect(html).not.toContain("<b>x</b>");
89
+ expect(html).not.toContain("<s>sec");
90
+ expect(html).not.toContain('"onclick="y');
91
+ expect(html).toContain("&lt;script&gt;t&lt;/script&gt;");
92
+ });
93
+
94
+ test("renders desktop nav and mobile details with active label summary", () => {
95
+ const html = render();
96
+ expect(html).toContain('<nav class="settings-sidebar"');
97
+ expect(html).toContain('<details class="settings-nav-mobile"');
98
+ expect(html).toMatch(/<summary[^>]*>Profile<\/summary>/);
99
+ expect(html.match(/href="\/admin\/settings\/tokens"/g)?.length).toBe(2);
100
+ });
101
+
102
+ test("toolbarPath defaults to active", () => {
103
+ const withPath = render({ toolbarPath: "/admin/settings" });
104
+ expect(withPath).toContain("vos-toolbar");
105
+ });
106
+ });
@@ -636,6 +636,95 @@ export function baseStyles(): string {
636
636
  }
637
637
  }
638
638
 
639
+ /* ─── Settings layout ─── */
640
+ .settings-layout {
641
+ display: flex;
642
+ align-items: flex-start;
643
+ gap: 32px;
644
+ }
645
+ .settings-sidebar {
646
+ flex: 0 0 260px;
647
+ width: 260px;
648
+ padding-right: 16px;
649
+ border-right: 1px solid #e8e8ee;
650
+ }
651
+ .settings-nav-mobile { display: none; }
652
+ .settings-nav-section { margin-bottom: 16px; }
653
+ .settings-nav-label {
654
+ font-size: 12px;
655
+ font-weight: 600;
656
+ color: #6b7280;
657
+ text-transform: uppercase;
658
+ letter-spacing: 0.04em;
659
+ padding: 0 12px 6px;
660
+ margin-bottom: 6px;
661
+ border-bottom: 1px solid #e8e8ee;
662
+ }
663
+ .settings-nav-link {
664
+ display: block;
665
+ padding: 6px 12px;
666
+ border-radius: 6px;
667
+ font-size: 14px;
668
+ color: #1a1a2e;
669
+ text-decoration: none;
670
+ }
671
+ .settings-nav-link:hover { background: #f7f7fb; }
672
+ .settings-nav-link[aria-current="page"] {
673
+ background: #eef2ff;
674
+ color: #4f46e5;
675
+ font-weight: 600;
676
+ }
677
+ .settings-content { flex: 1 1 auto; min-width: 0; }
678
+ .settings-content h1 {
679
+ font-size: 24px;
680
+ font-weight: 600;
681
+ color: #1a1a2e;
682
+ margin: 0 0 24px;
683
+ padding-bottom: 12px;
684
+ border-bottom: 1px solid #e8e8ee;
685
+ }
686
+ .settings-section-title {
687
+ font-size: 18px;
688
+ font-weight: 600;
689
+ color: #1a1a2e;
690
+ margin: 32px 0 16px;
691
+ padding-bottom: 8px;
692
+ border-bottom: 1px solid #e8e8ee;
693
+ }
694
+ .settings-danger-zone {
695
+ border: 1px solid #dc2626;
696
+ background: #fff;
697
+ border-radius: 8px;
698
+ padding: 16px 20px;
699
+ margin-top: 16px;
700
+ }
701
+ .settings-danger-zone > .settings-section-title {
702
+ color: #dc2626;
703
+ margin-top: 0;
704
+ border-bottom-color: #fee2e2;
705
+ }
706
+ @media (max-width: ${BREAKPOINT_MOBILE_MAX}px) {
707
+ .settings-layout { flex-direction: column; gap: 16px; }
708
+ .settings-sidebar { display: none; }
709
+ .settings-nav-mobile {
710
+ display: block;
711
+ width: 100%;
712
+ border: 1px solid #e8e8ee;
713
+ border-radius: 8px;
714
+ background: #fff;
715
+ }
716
+ .settings-nav-mobile summary {
717
+ padding: 12px;
718
+ min-height: 44px;
719
+ box-sizing: border-box;
720
+ cursor: pointer;
721
+ font-weight: 600;
722
+ color: #4f46e5;
723
+ }
724
+ .settings-nav-mobile .settings-nav-link { padding: 12px; }
725
+ }
726
+ /* ─── /Settings layout ─── */
727
+
639
728
  /* ─── Tablet band (641–${BREAKPOINT_TABLET_MAX}px) ──────────────── */
640
729
  @media (min-width: ${BREAKPOINT_MOBILE_MAX + 1}px) and (max-width: ${BREAKPOINT_TABLET_MAX}px) {
641
730
  .vos-page {
@@ -101,3 +101,75 @@ describe("baseStyles — AXR-1.1 shared explore board component classes", () =>
101
101
  expect(css).toContain(".badge-warning");
102
102
  });
103
103
  });
104
+
105
+ describe("baseStyles — SGL-1.1 settings layout", () => {
106
+ const css = baseStyles();
107
+ const start = css.indexOf("/* ─── Settings layout ─── */");
108
+ const end = css.indexOf("/* ─── /Settings layout ─── */");
109
+ const block = css.slice(start, end);
110
+
111
+ test("marker-delimited block exists with the new classes", () => {
112
+ expect(start).toBeGreaterThan(-1);
113
+ expect(end).toBeGreaterThan(start);
114
+ for (const cls of [
115
+ ".settings-sidebar",
116
+ ".settings-nav-mobile",
117
+ ".settings-content",
118
+ ".settings-section-title",
119
+ ".settings-danger-zone",
120
+ ]) {
121
+ expect(block).toContain(cls);
122
+ }
123
+ });
124
+
125
+ test("mobile media block references the settings classes", () => {
126
+ const media = block.slice(
127
+ block.indexOf(`@media (max-width: ${BREAKPOINT_MOBILE_MAX}px)`),
128
+ );
129
+ expect(media).toContain(".settings-sidebar");
130
+ expect(media).toContain(".settings-nav-mobile");
131
+ });
132
+
133
+ test("uses only allowed hex colors", () => {
134
+ const allowed = new Set(
135
+ [
136
+ "#1a1a2e",
137
+ "#4f46e5",
138
+ "#eef2ff",
139
+ "#e8e8ee",
140
+ "#f7f7fb",
141
+ "#dc2626",
142
+ "#fee2e2",
143
+ "#6b7280",
144
+ "#fff",
145
+ "#f3f4f6",
146
+ "#065f46",
147
+ "#0f766e",
148
+ "#22c55e",
149
+ "#2d2d4e",
150
+ "#374151",
151
+ "#4338ca",
152
+ "#5b21b6",
153
+ "#92400e",
154
+ "#9ca3af",
155
+ "#a7f3d0",
156
+ "#c7d2fe",
157
+ "#ccfbf1",
158
+ "#d1d5db",
159
+ "#d1fae5",
160
+ "#e5e7eb",
161
+ "#ede9fe",
162
+ "#ef4444",
163
+ "#f59e0b",
164
+ "#f9fafb",
165
+ "#fafafa",
166
+ "#fde68a",
167
+ "#fecaca",
168
+ "#fef3c7",
169
+ ].map((c) => c.toLowerCase()),
170
+ );
171
+ const found = block.match(/#[0-9a-fA-F]{3,8}\b/g) ?? [];
172
+ expect(found.length).toBeGreaterThan(0);
173
+ for (const c of found) expect(allowed.has(c.toLowerCase())).toBe(true);
174
+ });
175
+ });
@@ -10,6 +10,7 @@
10
10
  import type { AgentCronJob, PrismaClient } from "../prisma/client/client.ts";
11
11
  import { type Clock, SystemClock } from "./clock.ts";
12
12
  import { NotFoundError, UnprocessableEntityError } from "./errors.ts";
13
+ import type { PrismaTransactionClient } from "./prisma-tx.ts";
13
14
  import type {
14
15
  SpecialtyDefaultCron,
15
16
  SpecialtyDefaultsResolver,
@@ -560,101 +561,117 @@ export class AgentCronJobService {
560
561
  where: { agentId, system: true },
561
562
  });
562
563
 
563
- // Build a map of name → existing cron
564
- const existingByName = new Map<string, AgentCronJob>();
565
- for (const cron of existingSystemCrons) {
566
- if (cron.name) {
567
- existingByName.set(cron.name, cron);
568
- }
569
- }
570
-
571
- // Build set of valid default cron names for orphan detection
572
- const systemCronNames = new Set(defaultCrons.map((c) => c.name));
573
-
574
- let created = 0;
575
- let updated = 0;
576
- let deleted = 0;
577
-
578
564
  // Wrap all mutations in a transaction so a crash mid-loop cannot leave
579
565
  // the agent missing required system crons (e.g. shipwright-dev-task).
580
- await this.prisma.$transaction(async (tx) => {
581
- // Pass 1: reconcile each default cron entry, recording name → row id.
582
- const idByName = new Map<string, string>();
583
- for (const systemCron of defaultCrons) {
584
- const existing = existingByName.get(systemCron.name);
585
-
586
- if (existing) {
587
- // Update in place — preserves id (and thus AgentCronRun history,
588
- // which cascade-deletes when its AgentCronJob is deleted) and the
589
- // existing enabled state.
590
- await tx.agentCronJob.update({
591
- where: { id: existing.id },
592
- data: {
593
- schedule: systemCron.schedule,
594
- prompt: systemCron.prompt,
595
- silent: systemCron.silent ?? false,
596
- preCheck: systemCron.preCheck ?? null,
597
- },
598
- });
599
- idByName.set(systemCron.name, existing.id);
600
- updated++;
601
- } else {
602
- const createdCron = await tx.agentCronJob.create({
603
- data: {
604
- agentId,
605
- name: systemCron.name,
606
- system: true,
607
- schedule: systemCron.schedule,
608
- prompt: systemCron.prompt,
609
- silent: systemCron.silent ?? false,
610
- preCheck: systemCron.preCheck ?? null,
611
- channel: null,
612
- user: null,
613
- enabled: systemCron.enabled ?? true,
614
- },
615
- });
616
- idByName.set(systemCron.name, createdCron.id);
617
- created++;
618
- }
619
- }
566
+ return this.prisma.$transaction((tx) =>
567
+ applySystemCronDefaults(tx, agentId, defaultCrons, existingSystemCrons),
568
+ );
569
+ }
570
+ }
620
571
 
621
- // Pass 2: link declared parent/child crons now that every row in this
622
- // reconcile has a known id, regardless of array order or first-boot.
623
- // Also clears parentCronId back to null when an entry no longer
624
- // declares a resolvable parentCron, so the link self-heals in both
625
- // directions rather than only null→set.
626
- for (const systemCron of defaultCrons) {
627
- const childId = idByName.get(systemCron.name);
628
- if (!childId) continue;
629
- const parentId = systemCron.parentCron
630
- ? idByName.get(systemCron.parentCron)
631
- : undefined;
632
-
633
- if (parentId) {
634
- await tx.agentCronJob.update({
635
- where: { id: childId },
636
- data: { parentCronId: parentId },
637
- });
638
- } else {
639
- const existing = existingByName.get(systemCron.name);
640
- if (existing && existing.parentCronId !== null) {
641
- await tx.agentCronJob.update({
642
- where: { id: childId },
643
- data: { parentCronId: null },
644
- });
645
- }
646
- }
647
- }
572
+ /**
573
+ * The mutation half of reconcileSystemCrons(), shared with the template
574
+ * reconcile (agent-template-reconcile.ts) so both run it inside their own
575
+ * transaction: pass 1 updates/creates each default cron by name, pass 2
576
+ * links parentCron, the orphan pass deletes system crons no longer listed.
577
+ * `existingSystemCrons` are the agent's rows with system=true.
578
+ */
579
+ export async function applySystemCronDefaults(
580
+ tx: Pick<PrismaTransactionClient, "agentCronJob">,
581
+ agentId: string,
582
+ defaultCrons: readonly SpecialtyDefaultCron[],
583
+ existingSystemCrons: readonly AgentCronJob[],
584
+ ): Promise<{ created: number; updated: number; deleted: number }> {
585
+ // Build a map of name → existing cron
586
+ const existingByName = new Map<string, AgentCronJob>();
587
+ for (const cron of existingSystemCrons) {
588
+ if (cron.name) {
589
+ existingByName.set(cron.name, cron);
590
+ }
591
+ }
648
592
 
649
- // Orphan pass: delete any system cron whose name is not in the defaults.
650
- for (const cron of existingSystemCrons) {
651
- if (!cron.name || !systemCronNames.has(cron.name)) {
652
- await tx.agentCronJob.delete({ where: { id: cron.id } });
653
- deleted++;
654
- }
593
+ // Build set of valid default cron names for orphan detection
594
+ const systemCronNames = new Set(defaultCrons.map((c) => c.name));
595
+
596
+ let created = 0;
597
+ let updated = 0;
598
+ let deleted = 0;
599
+
600
+ // Pass 1: reconcile each default cron entry, recording name → row id.
601
+ const idByName = new Map<string, string>();
602
+ for (const systemCron of defaultCrons) {
603
+ const existing = existingByName.get(systemCron.name);
604
+
605
+ if (existing) {
606
+ // Update in place — preserves id (and thus AgentCronRun history,
607
+ // which cascade-deletes when its AgentCronJob is deleted) and the
608
+ // existing enabled state.
609
+ await tx.agentCronJob.update({
610
+ where: { id: existing.id },
611
+ data: {
612
+ schedule: systemCron.schedule,
613
+ prompt: systemCron.prompt,
614
+ silent: systemCron.silent ?? false,
615
+ preCheck: systemCron.preCheck ?? null,
616
+ },
617
+ });
618
+ idByName.set(systemCron.name, existing.id);
619
+ updated++;
620
+ } else {
621
+ const createdCron = await tx.agentCronJob.create({
622
+ data: {
623
+ agentId,
624
+ name: systemCron.name,
625
+ system: true,
626
+ schedule: systemCron.schedule,
627
+ prompt: systemCron.prompt,
628
+ silent: systemCron.silent ?? false,
629
+ preCheck: systemCron.preCheck ?? null,
630
+ channel: null,
631
+ user: null,
632
+ enabled: systemCron.enabled ?? true,
633
+ },
634
+ });
635
+ idByName.set(systemCron.name, createdCron.id);
636
+ created++;
637
+ }
638
+ }
639
+
640
+ // Pass 2: link declared parent/child crons now that every row in this
641
+ // reconcile has a known id, regardless of array order or first-boot.
642
+ // Also clears parentCronId back to null when an entry no longer
643
+ // declares a resolvable parentCron, so the link self-heals in both
644
+ // directions rather than only null→set.
645
+ for (const systemCron of defaultCrons) {
646
+ const childId = idByName.get(systemCron.name);
647
+ if (!childId) continue;
648
+ const parentId = systemCron.parentCron
649
+ ? idByName.get(systemCron.parentCron)
650
+ : undefined;
651
+
652
+ if (parentId) {
653
+ await tx.agentCronJob.update({
654
+ where: { id: childId },
655
+ data: { parentCronId: parentId },
656
+ });
657
+ } else {
658
+ const existing = existingByName.get(systemCron.name);
659
+ if (existing && existing.parentCronId !== null) {
660
+ await tx.agentCronJob.update({
661
+ where: { id: childId },
662
+ data: { parentCronId: null },
663
+ });
655
664
  }
656
- });
665
+ }
666
+ }
657
667
 
658
- return { created, updated, deleted };
668
+ // Orphan pass: delete any system cron whose name is not in the defaults.
669
+ for (const cron of existingSystemCrons) {
670
+ if (!cron.name || !systemCronNames.has(cron.name)) {
671
+ await tx.agentCronJob.delete({ where: { id: cron.id } });
672
+ deleted++;
673
+ }
659
674
  }
675
+
676
+ return { created, updated, deleted };
660
677
  }