@app-vitals/agent-admin 1.101.3 → 1.102.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.101.3",
3
+ "version": "1.102.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.101.3",
47
+ "@app-vitals/agent-lib": "1.102.0",
48
48
  "@hono/zod-openapi": "^1.0.0",
49
49
  "@prisma/adapter-pg": "^7.10.0",
50
50
  "@prisma/client": "^7.10.0",
package/src/accounts.ts CHANGED
@@ -71,6 +71,17 @@ export class AccountService {
71
71
  return member?.account ?? null;
72
72
  }
73
73
 
74
+ /** Account id + role of the member with this email, or null. */
75
+ async getMembershipByEmail(
76
+ email: string,
77
+ ): Promise<{ accountId: string; role: string } | null> {
78
+ const member = await this.prisma.accountMember.findUnique({
79
+ where: { email: normalizeEmail(email) },
80
+ select: { accountId: true, role: true },
81
+ });
82
+ return member ?? null;
83
+ }
84
+
74
85
  async list(): Promise<Account[]> {
75
86
  return this.prisma.account.findMany({ orderBy: { createdAt: "asc" } });
76
87
  }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * admin/src/caller-scope.integration.test.ts
3
+ * Role resolver + role-aware scope against real Postgres (ACM-7.1).
4
+ *
5
+ * Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
6
+ */
7
+
8
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
9
+ import type { PrismaClient } from "../prisma/client/client.ts";
10
+ import { AccountMemberService } from "./account-members.ts";
11
+ import { AccountService } from "./accounts.ts";
12
+ import { AgentMemberService } from "./agent-members.ts";
13
+ import {
14
+ callerScopeDepsFromServices,
15
+ resolveCallerScope,
16
+ resolveViewerRole,
17
+ } from "./caller-scope.ts";
18
+ import { createAdminPrismaClient } from "./prisma-client.ts";
19
+
20
+ const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
21
+ const describeOrSkip = TEST_DB ? describe : describe.skip;
22
+
23
+ describeOrSkip("caller scope role resolver (integration)", () => {
24
+ let prisma: PrismaClient;
25
+ let deps: ReturnType<typeof callerScopeDepsFromServices>;
26
+ let accountId: string;
27
+ let accountAgentIds: string[];
28
+ let sideAgentId: string;
29
+
30
+ beforeEach(async () => {
31
+ prisma = createAdminPrismaClient(TEST_DB as string);
32
+ await prisma.agentMember.deleteMany();
33
+ await prisma.accountInvite.deleteMany();
34
+ await prisma.accountMember.deleteMany();
35
+ await prisma.agent.deleteMany();
36
+ await prisma.account.deleteMany();
37
+ const accountService = new AccountService(prisma);
38
+ const agentMemberService = new AgentMemberService(prisma);
39
+ deps = callerScopeDepsFromServices({ accountService, agentMemberService });
40
+
41
+ const account = await accountService.create("Acme", "Owner@Acme.com");
42
+ accountId = account.id;
43
+ await new AccountMemberService(prisma).add(
44
+ account.id,
45
+ "member@acme.com",
46
+ "member",
47
+ );
48
+ const a1 = await prisma.agent.create({
49
+ data: { name: "a1", accountId: account.id },
50
+ });
51
+ const a2 = await prisma.agent.create({
52
+ data: { name: "a2", accountId: account.id },
53
+ });
54
+ accountAgentIds = [a1.id, a2.id];
55
+ const side = await prisma.agent.create({ data: { name: "side" } });
56
+ sideAgentId = side.id;
57
+ await agentMemberService.add(side.id, "member@acme.com");
58
+ });
59
+
60
+ afterEach(async () => {
61
+ await prisma.$disconnect();
62
+ });
63
+
64
+ it("resolves admin / owner / member / null", async () => {
65
+ const role = (email: string, isAdmin = false) =>
66
+ resolveViewerRole({ email, isAdmin }, deps);
67
+ expect(await role("anyone@x.com", true)).toBe("admin");
68
+ expect(await role("OWNER@acme.com")).toBe("owner");
69
+ expect(await role("member@acme.com")).toBe("member");
70
+ expect(await role("stranger@x.com")).toBeNull();
71
+ });
72
+
73
+ it("owner gets account agents; member only AgentMember agents (flag on)", async () => {
74
+ const scope = (email: string, flagEnabled: boolean) =>
75
+ resolveCallerScope({ email, isAdmin: false, flagEnabled }, deps);
76
+
77
+ const owner = await scope("owner@acme.com", true);
78
+ expect(owner.kind === "scoped" && owner.accountId).toBe(accountId);
79
+ expect(owner.kind === "scoped" && owner.agentIds.sort()).toEqual(
80
+ [...accountAgentIds].sort(),
81
+ );
82
+
83
+ expect(await scope("member@acme.com", true)).toEqual({
84
+ kind: "scoped",
85
+ accountId,
86
+ agentIds: [sideAgentId],
87
+ });
88
+ expect(await scope("owner@acme.com", false)).toEqual({
89
+ kind: "scoped",
90
+ accountId: null,
91
+ agentIds: [],
92
+ });
93
+ });
94
+ });
@@ -7,8 +7,9 @@
7
7
  *
8
8
  * Resolved per request from the caller's email — the account is deliberately
9
9
  * NOT stored in the session JWT, so membership changes take effect
10
- * immediately. Account membership contributes agents only when the self-serve
11
- * flag is on; with it off the scope is exactly the AgentMember-only scope.
10
+ * immediately. Account agents contribute to the scope only for account owners
11
+ * and only when the self-serve flag is on; members (and flag off) get exactly
12
+ * the AgentMember-only scope.
12
13
  */
13
14
 
14
15
  import type { AccountService } from "./accounts.ts";
@@ -32,6 +33,36 @@ export interface CallerScopeDeps {
32
33
  listAgentIdsByAccount(accountId: string): Promise<string[]>;
33
34
  /** Ids of agents the email holds an AgentMember row for. */
34
35
  listMemberAgentIds(email: string): Promise<string[]>;
36
+ /**
37
+ * Account id + AccountMember.role for the email, or null. Optional so
38
+ * existing consumers keep compiling; when absent, the role is unknown and
39
+ * account agents contribute as before.
40
+ */
41
+ getAccountMembershipByEmail?(
42
+ email: string,
43
+ ): Promise<{ accountId: string; role: string } | null>;
44
+ }
45
+
46
+ /** How the caller relates to the platform/account: see resolveViewerRole. */
47
+ export type ViewerRole = "admin" | "owner" | "member";
48
+
49
+ /**
50
+ * The caller's role: `admin` for allowlisted callers, otherwise the
51
+ * AccountMember.role (`owner` | `member`), or null when the caller has no
52
+ * account. Independent of the self-serve flag.
53
+ */
54
+ export async function resolveViewerRole(
55
+ input: { email: string; isAdmin: boolean },
56
+ deps: Pick<CallerScopeDeps, "getAccountMembershipByEmail">,
57
+ ): Promise<ViewerRole | null> {
58
+ if (input.isAdmin) return "admin";
59
+ const membership = await deps.getAccountMembershipByEmail?.(
60
+ input.email.toLowerCase(),
61
+ );
62
+ if (membership?.role === "owner" || membership?.role === "member") {
63
+ return membership.role;
64
+ }
65
+ return null;
35
66
  }
36
67
 
37
68
  export async function resolveCallerScope(
@@ -44,8 +75,18 @@ export async function resolveCallerScope(
44
75
  const accountId = input.flagEnabled
45
76
  ? await deps.getAccountIdByEmail(email)
46
77
  : null;
78
+ // Only owners get account-wide agents; with no role dep the role is unknown
79
+ // and the legacy behavior (account agents contribute) is kept.
80
+ const role = accountId
81
+ ? await resolveViewerRole({ email, isAdmin: false }, deps)
82
+ : null;
83
+ const includeAccountAgents =
84
+ accountId !== null &&
85
+ (role === "owner" || !deps.getAccountMembershipByEmail);
47
86
  const [accountAgentIds, memberAgentIds] = await Promise.all([
48
- accountId ? deps.listAgentIdsByAccount(accountId) : [],
87
+ includeAccountAgents && accountId
88
+ ? deps.listAgentIdsByAccount(accountId)
89
+ : [],
49
90
  deps.listMemberAgentIds(email),
50
91
  ]);
51
92
  return {
@@ -100,14 +141,20 @@ export function createCallerScopeResolver(
100
141
 
101
142
  /** Wire the resolver's I/O to the real services. */
102
143
  export function callerScopeDepsFromServices(services: {
103
- accountService: Pick<AccountService, "getByMemberEmail" | "listAgentIds">;
144
+ accountService: Pick<AccountService, "getByMemberEmail" | "listAgentIds"> &
145
+ Partial<Pick<AccountService, "getMembershipByEmail">>;
104
146
  agentMemberService: Pick<AgentMemberService, "listByEmail">;
105
147
  }): CallerScopeDeps {
148
+ const getMembership = services.accountService.getMembershipByEmail?.bind(
149
+ services.accountService,
150
+ );
106
151
  return {
107
152
  getAccountIdByEmail: async (email) =>
108
153
  (await services.accountService.getByMemberEmail(email))?.id ?? null,
109
154
  listAgentIdsByAccount: (accountId) =>
110
155
  services.accountService.listAgentIds(accountId),
156
+ getAccountMembershipByEmail:
157
+ getMembership && ((email) => getMembership(email)),
111
158
  listMemberAgentIds: async (email) =>
112
159
  (await services.agentMemberService.listByEmail(email)).map(
113
160
  (m) => m.agentId,
@@ -3,6 +3,7 @@ import {
3
3
  type CallerScope,
4
4
  type CallerScopeDeps,
5
5
  resolveCallerScope,
6
+ resolveViewerRole,
6
7
  scopeIncludesAgent,
7
8
  taskStoreViewScope,
8
9
  } from "./caller-scope.ts";
@@ -11,18 +12,39 @@ function makeDeps(opts: {
11
12
  account?: Record<string, string>;
12
13
  accountAgents?: Record<string, string[]>;
13
14
  members?: Record<string, string[]>;
15
+ roles?: Record<string, string>;
14
16
  }): CallerScopeDeps {
15
17
  return {
16
18
  getAccountIdByEmail: async (e) => opts.account?.[e] ?? null,
17
19
  listAgentIdsByAccount: async (id) => opts.accountAgents?.[id] ?? [],
18
20
  listMemberAgentIds: async (e) => opts.members?.[e] ?? [],
21
+ getAccountMembershipByEmail: async (e) => {
22
+ const accountId = opts.account?.[e];
23
+ const role = opts.roles?.[e];
24
+ return accountId && role ? { accountId, role } : null;
25
+ },
19
26
  };
20
27
  }
21
28
 
22
29
  const deps = makeDeps({
23
- account: { "a@x.com": "acct-a", "both@x.com": "acct-a" },
30
+ account: {
31
+ "a@x.com": "acct-a",
32
+ "both@x.com": "acct-a",
33
+ "mem@x.com": "acct-a",
34
+ "memboth@x.com": "acct-a",
35
+ },
36
+ roles: {
37
+ "a@x.com": "owner",
38
+ "both@x.com": "owner",
39
+ "mem@x.com": "member",
40
+ "memboth@x.com": "member",
41
+ },
24
42
  accountAgents: { "acct-a": ["ag-1", "ag-2"] },
25
- members: { "m@x.com": ["ag-9"], "both@x.com": ["ag-2", "ag-9"] },
43
+ members: {
44
+ "m@x.com": ["ag-9"],
45
+ "both@x.com": ["ag-2", "ag-9"],
46
+ "memboth@x.com": ["ag-9"],
47
+ },
26
48
  });
27
49
 
28
50
  const run = (email: string, isAdmin = false, flagEnabled = true) =>
@@ -33,7 +55,7 @@ describe("resolveCallerScope", () => {
33
55
  expect(await run("a@x.com", true)).toEqual({ kind: "all" });
34
56
  });
35
57
 
36
- it("account member gets every account agent", async () => {
58
+ it("account owner gets every account agent", async () => {
37
59
  expect(await run("a@x.com")).toEqual({
38
60
  kind: "scoped",
39
61
  accountId: "acct-a",
@@ -41,6 +63,33 @@ describe("resolveCallerScope", () => {
41
63
  });
42
64
  });
43
65
 
66
+ it("account member (flag on) gets only AgentMember agents, keeps accountId", async () => {
67
+ expect(await run("mem@x.com")).toEqual({
68
+ kind: "scoped",
69
+ accountId: "acct-a",
70
+ agentIds: [],
71
+ });
72
+ expect(await run("memboth@x.com")).toEqual({
73
+ kind: "scoped",
74
+ accountId: "acct-a",
75
+ agentIds: ["ag-9"],
76
+ });
77
+ });
78
+
79
+ it("deps without the role lookup keep the legacy account-agent behavior", async () => {
80
+ const { getAccountMembershipByEmail: _omit, ...legacy } = deps;
81
+ expect(
82
+ await resolveCallerScope(
83
+ { email: "mem@x.com", isAdmin: false, flagEnabled: true },
84
+ legacy,
85
+ ),
86
+ ).toEqual({
87
+ kind: "scoped",
88
+ accountId: "acct-a",
89
+ agentIds: ["ag-1", "ag-2"],
90
+ });
91
+ });
92
+
44
93
  it("explicit member gets only AgentMember agents", async () => {
45
94
  expect(await run("m@x.com")).toEqual({
46
95
  kind: "scoped",
@@ -114,3 +163,64 @@ describe("taskStoreViewScope (SSP-6.8)", () => {
114
163
  ).toEqual({ kind: "none" });
115
164
  });
116
165
  });
166
+
167
+ describe("resolveViewerRole", () => {
168
+ const role = (email: string, isAdmin = false) =>
169
+ resolveViewerRole({ email, isAdmin }, deps);
170
+
171
+ it("admin for allowlisted callers, even with a member row", async () => {
172
+ expect(await role("mem@x.com", true)).toBe("admin");
173
+ expect(await role("nobody@x.com", true)).toBe("admin");
174
+ });
175
+
176
+ it("owner or member from the account role, case-insensitive", async () => {
177
+ expect(await role("A@x.com")).toBe("owner");
178
+ expect(await role("mem@x.com")).toBe("member");
179
+ });
180
+
181
+ it("null for a caller with no account", async () => {
182
+ expect(await role("none@x.com")).toBeNull();
183
+ expect(await role("m@x.com")).toBeNull();
184
+ });
185
+
186
+ it("null for an unknown role value or deps without the lookup", async () => {
187
+ expect(
188
+ await resolveViewerRole(
189
+ { email: "x@x.com", isAdmin: false },
190
+ {
191
+ getAccountMembershipByEmail: async () => ({
192
+ accountId: "a",
193
+ role: "weird",
194
+ }),
195
+ },
196
+ ),
197
+ ).toBeNull();
198
+ expect(
199
+ await resolveViewerRole({ email: "x@x.com", isAdmin: false }, {}),
200
+ ).toBeNull();
201
+ });
202
+ });
203
+
204
+ describe("resolveCallerScope matrix: role x flag", () => {
205
+ const cases: Array<[string, boolean, string[], string | null]> = [
206
+ ["a@x.com", true, ["ag-1", "ag-2"], "acct-a"], // owner, flag on
207
+ ["a@x.com", false, [], null], // owner, flag off
208
+ ["memboth@x.com", true, ["ag-9"], "acct-a"], // member, flag on
209
+ ["memboth@x.com", false, ["ag-9"], null], // member, flag off
210
+ ["none@x.com", true, [], null], // no account
211
+ ["none@x.com", false, [], null],
212
+ ];
213
+ for (const [email, flag, agentIds, accountId] of cases) {
214
+ it(`${email} flag=${flag}`, async () => {
215
+ expect(await run(email, false, flag)).toEqual({
216
+ kind: "scoped",
217
+ accountId,
218
+ agentIds,
219
+ });
220
+ });
221
+ }
222
+ it("admin is all regardless of flag", async () => {
223
+ expect(await run("a@x.com", true, false)).toEqual({ kind: "all" });
224
+ expect(await run("a@x.com", true, true)).toEqual({ kind: "all" });
225
+ });
226
+ });