@app-vitals/agent-admin 1.103.0 → 1.104.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.103.0",
3
+ "version": "1.104.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "dependencies": {
46
46
  "@anthropic-ai/sdk": "0.137.0",
47
- "@app-vitals/agent-lib": "1.103.0",
47
+ "@app-vitals/agent-lib": "1.104.0",
48
48
  "@hono/zod-openapi": "^1.0.0",
49
49
  "@prisma/adapter-pg": "^7.10.0",
50
50
  "@prisma/client": "^7.10.0",
@@ -40,3 +40,19 @@ export function isAccountCreateErrorCode(
40
40
  ): code is keyof typeof ACCOUNT_CREATE_ERROR_MESSAGES {
41
41
  return code in ACCOUNT_CREATE_ERROR_MESSAGES;
42
42
  }
43
+
44
+ /**
45
+ * Who may create agents (ACM-7.3): platform admins, and account owners (with
46
+ * an account). Members are denied. When the resolver could not determine a
47
+ * role (no membership dep), an account user keeps the legacy behavior.
48
+ */
49
+ export function canCreateAgent(scope: CallerScope): boolean {
50
+ if (scope.kind === "all") return true;
51
+ if (scope.accountId === null) return false;
52
+ return scope.role === undefined || scope.role === "owner";
53
+ }
54
+
55
+ /** Account to stamp on a new agent, or null when the caller may not create. */
56
+ export function createAccountIdFromScope(scope: CallerScope): string | null {
57
+ return canCreateAgent(scope) ? accountIdFromScope(scope) : null;
58
+ }
@@ -4,7 +4,12 @@
4
4
  */
5
5
 
6
6
  import { describe, expect, it } from "bun:test";
7
- import { getSelfServeSpecialty } from "./account-agent-create.ts";
7
+ import {
8
+ canCreateAgent,
9
+ createAccountIdFromScope,
10
+ getSelfServeSpecialty,
11
+ } from "./account-agent-create.ts";
12
+ import type { CallerScope } from "./caller-scope.ts";
8
13
 
9
14
  describe("getSelfServeSpecialty", () => {
10
15
  it("defaults to specialist", () => {
@@ -26,3 +31,26 @@ describe("getSelfServeSpecialty", () => {
26
31
  ).toBe("base");
27
32
  });
28
33
  });
34
+
35
+ describe("canCreateAgent / createAccountIdFromScope", () => {
36
+ const scoped = (
37
+ accountId: string | null,
38
+ role?: "owner" | "member" | null,
39
+ ): CallerScope => ({ kind: "scoped", accountId, agentIds: [], role });
40
+
41
+ it("allows platform admins", () => {
42
+ expect(canCreateAgent({ kind: "all" })).toBe(true);
43
+ expect(createAccountIdFromScope({ kind: "all" })).toBeNull();
44
+ });
45
+
46
+ it("allows owners and stamps their account", () => {
47
+ expect(canCreateAgent(scoped("a", "owner"))).toBe(true);
48
+ expect(createAccountIdFromScope(scoped("a", "owner"))).toBe("a");
49
+ });
50
+
51
+ it("denies members and callers without an account", () => {
52
+ expect(canCreateAgent(scoped("a", "member"))).toBe(false);
53
+ expect(createAccountIdFromScope(scoped("a", "member"))).toBeNull();
54
+ expect(canCreateAgent(scoped(null, "owner"))).toBe(false);
55
+ });
56
+ });
@@ -7941,3 +7941,54 @@ describe("admin UI — Anthropic federation credential guards (WIF-2.1)", () =>
7941
7941
  expect(html).not.toContain('name="claudeCodeOauthToken"');
7942
7942
  });
7943
7943
  });
7944
+
7945
+ describe("agent create requires owner or admin (ACM-7.3)", () => {
7946
+ const scoped = (role: "owner" | "member") => async () => ({
7947
+ kind: "scoped" as const,
7948
+ accountId: "acct-a",
7949
+ agentIds: [],
7950
+ role,
7951
+ });
7952
+ const userCookie = () =>
7953
+ makeSessionCookie(SESSION_SECRET, "google-sub-u", "u@example.com", false);
7954
+
7955
+ it("member gets 403 on the create form and POST; no agent is created", async () => {
7956
+ const created: unknown[] = [];
7957
+ const plain = makeMockDeps();
7958
+ const app = createAdminUIApp(
7959
+ makeMockDeps({
7960
+ callerScopeResolver: scoped("member"),
7961
+ agentService: {
7962
+ ...plain.agentService,
7963
+ create: async (
7964
+ ...a: Parameters<typeof plain.agentService.create>
7965
+ ) => {
7966
+ created.push(a[0]);
7967
+ return plain.agentService.create(...a);
7968
+ },
7969
+ },
7970
+ }),
7971
+ );
7972
+ const Cookie = `admin_session=${await userCookie()}`;
7973
+ expect(
7974
+ (await app.request("/admin/agents/new", { headers: { Cookie } })).status,
7975
+ ).toBe(403);
7976
+ const res = await app.request("/admin/agents", {
7977
+ method: "POST",
7978
+ body: new URLSearchParams({ name: "X", accountId: "acct-b" }).toString(),
7979
+ headers: { Cookie, "Content-Type": "application/x-www-form-urlencoded" },
7980
+ });
7981
+ expect(res.status).toBe(403);
7982
+ expect(created).toHaveLength(0);
7983
+ });
7984
+
7985
+ it("owner can open the create form", async () => {
7986
+ const app = createAdminUIApp(
7987
+ makeMockDeps({ callerScopeResolver: scoped("owner") }),
7988
+ );
7989
+ const res = await app.request("/admin/agents/new", {
7990
+ headers: { Cookie: `admin_session=${await userCookie()}` },
7991
+ });
7992
+ expect(res.status).toBe(200);
7993
+ });
7994
+ });
package/src/admin-ui.ts CHANGED
@@ -31,6 +31,7 @@ import { deleteCookie, getCookie, setCookie } from "hono/cookie";
31
31
  import { sign, verify } from "hono/jwt";
32
32
  import {
33
33
  accountIdFromScope,
34
+ createAccountIdFromScope,
34
35
  getSelfServeSpecialty,
35
36
  } from "./account-agent-create.ts";
36
37
  import type { AccountCreatedNotifier } from "./account-created-notifier.ts";
@@ -1644,7 +1645,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
1644
1645
  isAdmin: boolean,
1645
1646
  ): Promise<string | null> {
1646
1647
  if (isAdmin) return null;
1647
- return accountIdFromScope(await callerScopeResolver(userEmail, false));
1648
+ return createAccountIdFromScope(
1649
+ await callerScopeResolver(userEmail, false),
1650
+ );
1648
1651
  }
1649
1652
 
1650
1653
  // ATP-2.4: the template items a new agent in this scope would inherit,
@@ -5350,7 +5350,10 @@ describe("admin API — account users create and delete agents (SSP-4.2)", () =>
5350
5350
  );
5351
5351
  }
5352
5352
 
5353
- function makeAccountDeps(accountId: string | null): {
5353
+ function makeAccountDeps(
5354
+ accountId: string | null,
5355
+ role: "owner" | "member" = "owner",
5356
+ ): {
5354
5357
  deps: AdminDeps;
5355
5358
  created: Array<Record<string, unknown>>;
5356
5359
  } {
@@ -5362,6 +5365,7 @@ describe("admin API — account users create and delete agents (SSP-4.2)", () =>
5362
5365
  kind: "scoped",
5363
5366
  accountId,
5364
5367
  agentIds: accountId ? [ACCOUNT_AGENT_ID] : [],
5368
+ role,
5365
5369
  }),
5366
5370
  agentService: {
5367
5371
  ...base.agentService,
@@ -5417,6 +5421,17 @@ describe("admin API — account users create and delete agents (SSP-4.2)", () =>
5417
5421
  });
5418
5422
  });
5419
5423
 
5424
+ it("an account member (not owner) gets 403 on create", async () => {
5425
+ const { deps, created } = makeAccountDeps("acct-a", "member");
5426
+ const res = await post(
5427
+ createAdminApp(deps),
5428
+ { name: "Nope", specialty: "coding", accountId: "acct-a" },
5429
+ await accountCookie(),
5430
+ );
5431
+ expect(res.status).toBe(403);
5432
+ expect(created).toHaveLength(0);
5433
+ });
5434
+
5420
5435
  it("non-admin without an account (flag off / no membership) gets 403 on create", async () => {
5421
5436
  const { deps, created } = makeAccountDeps(null);
5422
5437
  const res = await post(
package/src/agents-api.ts CHANGED
@@ -36,6 +36,7 @@ import type { PrismaClient } from "../prisma/client/client.ts";
36
36
  import {
37
37
  ACCOUNT_CREATE_ERROR_MESSAGES,
38
38
  accountIdFromScope,
39
+ createAccountIdFromScope,
39
40
  getSelfServeSpecialty,
40
41
  isAccountCreateErrorCode,
41
42
  } from "./account-agent-create.ts";
@@ -2724,9 +2725,11 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
2724
2725
  const callerAccountId =
2725
2726
  email === undefined
2726
2727
  ? null
2727
- : accountIdFromScope(await callerScopeResolver(email, false));
2728
+ : createAccountIdFromScope(await callerScopeResolver(email, false));
2728
2729
  if (callerAccountId === null) {
2729
- throw new ForbiddenError("Admin access required to create agent");
2730
+ throw new ForbiddenError(
2731
+ "Owner or admin access required to create agent",
2732
+ );
2730
2733
  }
2731
2734
  accountId = callerAccountId;
2732
2735
  }
@@ -84,6 +84,7 @@ describeOrSkip("caller scope role resolver (integration)", () => {
84
84
  kind: "scoped",
85
85
  accountId,
86
86
  agentIds: [sideAgentId],
87
+ role: "member",
87
88
  });
88
89
  expect(await scope("owner@acme.com", false)).toEqual({
89
90
  kind: "scoped",
@@ -17,7 +17,13 @@ import type { AgentMemberService } from "./agent-members.ts";
17
17
 
18
18
  export type CallerScope =
19
19
  | { kind: "all" }
20
- | { kind: "scoped"; accountId: string | null; agentIds: string[] };
20
+ | {
21
+ kind: "scoped";
22
+ accountId: string | null;
23
+ agentIds: string[];
24
+ /** AccountMember.role when known (resolver has the membership dep). */
25
+ role?: ViewerRole | null;
26
+ };
21
27
 
22
28
  interface CallerScopeInput {
23
29
  email: string;
@@ -93,6 +99,7 @@ export async function resolveCallerScope(
93
99
  kind: "scoped",
94
100
  accountId,
95
101
  agentIds: [...new Set([...accountAgentIds, ...memberAgentIds])],
102
+ ...(role ? { role } : {}),
96
103
  };
97
104
  }
98
105
 
@@ -60,6 +60,7 @@ describe("resolveCallerScope", () => {
60
60
  kind: "scoped",
61
61
  accountId: "acct-a",
62
62
  agentIds: ["ag-1", "ag-2"],
63
+ role: "owner",
63
64
  });
64
65
  });
65
66
 
@@ -68,11 +69,13 @@ describe("resolveCallerScope", () => {
68
69
  kind: "scoped",
69
70
  accountId: "acct-a",
70
71
  agentIds: [],
72
+ role: "member",
71
73
  });
72
74
  expect(await run("memboth@x.com")).toEqual({
73
75
  kind: "scoped",
74
76
  accountId: "acct-a",
75
77
  agentIds: ["ag-9"],
78
+ role: "member",
76
79
  });
77
80
  });
78
81
 
@@ -212,7 +215,7 @@ describe("resolveCallerScope matrix: role x flag", () => {
212
215
  ];
213
216
  for (const [email, flag, agentIds, accountId] of cases) {
214
217
  it(`${email} flag=${flag}`, async () => {
215
- expect(await run(email, false, flag)).toEqual({
218
+ expect(await run(email, false, flag)).toMatchObject({
216
219
  kind: "scoped",
217
220
  accountId,
218
221
  agentIds,