@app-vitals/agent-admin 1.107.5 → 1.108.1

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.
Files changed (40) hide show
  1. package/e2e/real/admin-access.real.e2e.ts +77 -68
  2. package/e2e/real/agent-management.real.e2e.ts +165 -143
  3. package/e2e/real/owner-access.real.e2e.ts +228 -212
  4. package/openapi.platform.json +127 -6
  5. package/package.json +2 -2
  6. package/src/account-created-notifier.ts +1 -1
  7. package/src/account-onboarding.integration.test.ts +69 -0
  8. package/src/account-onboarding.ts +29 -3
  9. package/src/account-repo-lookup-api.integration.test.ts +163 -0
  10. package/src/account-repo-lookup-api.ts +106 -0
  11. package/src/account-repo-lookup.ts +89 -0
  12. package/src/account-trial-expiry-sweeper.ts +1 -1
  13. package/src/accounts-api.integration.test.ts +5 -2
  14. package/src/accounts-api.smoke.test.ts +21 -5
  15. package/src/accounts-api.ts +12 -15
  16. package/src/admin-boot-config.ts +2 -0
  17. package/src/admin-boot-config.unit.test.ts +27 -0
  18. package/src/admin-boot.ts +18 -10
  19. package/src/admin-spec.ts +6 -0
  20. package/src/admin-ui-account.smoke.test.ts +7 -3
  21. package/src/admin-ui-account.ts +10 -15
  22. package/src/admin-ui-accounts.smoke.test.ts +4 -4
  23. package/src/admin-ui-accounts.ts +7 -11
  24. package/src/admin-ui-settings-nav-context.smoke.test.ts +24 -0
  25. package/src/admin-ui-settings.smoke.test.ts +17 -1
  26. package/src/admin-ui.smoke.test.ts +168 -2
  27. package/src/admin-ui.ts +39 -26
  28. package/src/agent-management-access.integration.test.ts +18 -6
  29. package/src/agent-manifest.ts +2 -1
  30. package/src/agent-provisioner.integration.test.ts +10 -10
  31. package/src/agent-provisioner.ts +10 -1
  32. package/src/agent-provisioner.unit.test.ts +110 -36
  33. package/src/agent-templates-api.integration.test.ts +23 -5
  34. package/src/agent-templates-api.ts +11 -8
  35. package/src/caller-scope.integration.test.ts +8 -6
  36. package/src/caller-scope.ts +32 -26
  37. package/src/caller-scope.unit.test.ts +15 -11
  38. package/src/openapi-schemas.ts +22 -0
  39. package/src/platform-routes.ts +14 -2
  40. package/src/platform-routes.unit.test.ts +2 -0
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * admin/src/agent-management-access.integration.test.ts
3
- * Agent management (JSON API) against a real PostgreSQL DB with self-serve on:
4
- * only platform admins and the owner of the agent's own account may manage an
3
+ * Agent management (JSON API) against a real PostgreSQL DB, with the
4
+ * self-serve setting off and on (it must not change the outcome): only
5
+ * platform admins and the owner of the agent's own account may manage an
5
6
  * agent; a member of the agent (or of its account) and an owner of another
6
7
  * account get 403, while read access is unchanged.
7
8
  *
@@ -145,7 +146,10 @@ const cookieFor = (email: string, isAdmin: boolean) =>
145
146
  "HS256",
146
147
  );
147
148
 
148
- describeOrSkip("agent management access (integration)", () => {
149
+ /** Run with the self-serve setting off and on; the outcome must not differ. */
150
+ const SELF_SERVE = [false, true];
151
+
152
+ describeOrSkip.each(SELF_SERVE)("management, self-serve=%p", (enabled) => {
149
153
  let prisma: PrismaClient;
150
154
  let app: ReturnType<typeof createAdminApp>;
151
155
  let agentId: string;
@@ -180,8 +184,12 @@ describeOrSkip("agent management access (integration)", () => {
180
184
  await prisma.accountMember.deleteMany();
181
185
  await prisma.account.deleteMany();
182
186
 
183
- const one = await prisma.account.create({ data: { name: "Account One" } });
184
- const two = await prisma.account.create({ data: { name: "Account Two" } });
187
+ const one = await prisma.account.create({
188
+ data: { name: "Account One" },
189
+ });
190
+ const two = await prisma.account.create({
191
+ data: { name: "Account Two" },
192
+ });
185
193
  const mk = (accountId: string, email: string, role: string) =>
186
194
  prisma.accountMember.create({ data: { accountId, email, role } });
187
195
  await mk(one.id, "owner-one@example.com", "owner");
@@ -199,12 +207,16 @@ describeOrSkip("agent management access (integration)", () => {
199
207
  const deps = makeDeps(prisma);
200
208
  app = createAdminApp({
201
209
  ...deps,
210
+ selfServe: {
211
+ enabled,
212
+ defaultMaxAgents: 0,
213
+ contactEmail: "ops@example.com",
214
+ },
202
215
  callerScopeResolver: createCallerScopeResolver(
203
216
  callerScopeDepsFromServices({
204
217
  accountService: new AccountService(prisma),
205
218
  agentMemberService: new AgentMemberService(prisma),
206
219
  }),
207
- true,
208
220
  ),
209
221
  });
210
222
  });
@@ -262,7 +262,8 @@ export interface AgentDeploymentOpts {
262
262
  /**
263
263
  * Owning account. When set the pod is a tenant pod: the tenant label is added
264
264
  * to the Deployment and pod template, and a CPU limit is applied. Omit for
265
- * platform agents (manifest unchanged).
265
+ * platform agents (manifest unchanged). The Kubernetes provisioner passes it
266
+ * only when tenant pod restrictions are on (SHIPWRIGHT_SELF_SERVE_ENABLED).
266
267
  */
267
268
  accountId?: string | null;
268
269
  /**
@@ -407,18 +407,18 @@ describeOrSkip("KubernetesAgentProvisioner (integration)", () => {
407
407
  // ─── PVC name template ───────────────────────────────────────────────────────
408
408
 
409
409
  it("provision() uses slug for PVC name when pvcName template is configured", async () => {
410
- const agentId = await createAgent("OkWOW Agent");
410
+ const agentId = await createAgent("Widget Agent");
411
411
  const k8s = emptyClient();
412
412
  const provisioner = new KubernetesAgentProvisioner(k8s, tokens, {
413
413
  ...CONFIG,
414
414
  pvcName: (name) => `acme-agent-${name}-home`,
415
415
  });
416
416
 
417
- await provisioner.provision(agentId, { slug: "okwow" });
417
+ await provisioner.provision(agentId, { slug: "widget" });
418
418
 
419
419
  // PVC must use the slug, not the sanitized agentId.
420
420
  await expect(
421
- k8s.getPvc(NAMESPACE, "acme-agent-okwow-home"),
421
+ k8s.getPvc(NAMESPACE, "acme-agent-widget-home"),
422
422
  ).resolves.toBeDefined();
423
423
  // Default-named PVC must NOT exist.
424
424
  const resourceName = sanitizeAgentName(agentId);
@@ -477,13 +477,13 @@ describeOrSkip("KubernetesAgentProvisioner (integration)", () => {
477
477
 
478
478
  // Agent exists in DB, no k8s Deployment — reconcile should provision it.
479
479
  const result = await provisioner.reconcile([
480
- { id: agentId, slug: "okwow" },
480
+ { id: agentId, slug: "widget" },
481
481
  ]);
482
482
 
483
483
  expect(result.recreated).toEqual([agentId]);
484
484
  // PVC must use the slug passed through reconcile.
485
485
  await expect(
486
- k8s.getPvc(NAMESPACE, "acme-agent-okwow-home"),
486
+ k8s.getPvc(NAMESPACE, "acme-agent-widget-home"),
487
487
  ).resolves.toBeDefined();
488
488
  });
489
489
 
@@ -533,22 +533,22 @@ describeOrSkip("KubernetesAgentProvisioner (integration)", () => {
533
533
  });
534
534
 
535
535
  it("deprovision() deletes the templated PVC name derived from slug", async () => {
536
- const agentId = await createAgent("OkWOW Agent");
536
+ const agentId = await createAgent("Widget Agent");
537
537
  const k8s = emptyClient();
538
538
  const provisioner = new KubernetesAgentProvisioner(k8s, tokens, {
539
539
  ...CONFIG,
540
540
  pvcName: (name) => `acme-agent-${name}-home`,
541
541
  });
542
542
 
543
- await provisioner.provision(agentId, { slug: "okwow" });
543
+ await provisioner.provision(agentId, { slug: "widget" });
544
544
  await expect(
545
- k8s.getPvc(NAMESPACE, "acme-agent-okwow-home"),
545
+ k8s.getPvc(NAMESPACE, "acme-agent-widget-home"),
546
546
  ).resolves.toBeDefined();
547
547
 
548
- await provisioner.deprovision(agentId, { slug: "okwow" });
548
+ await provisioner.deprovision(agentId, { slug: "widget" });
549
549
 
550
550
  await expect(
551
- k8s.getPvc(NAMESPACE, "acme-agent-okwow-home"),
551
+ k8s.getPvc(NAMESPACE, "acme-agent-widget-home"),
552
552
  ).rejects.toThrow();
553
553
  });
554
554
 
@@ -241,6 +241,14 @@ export interface AgentProvisioner {
241
241
  export interface KubernetesAgentProvisionerConfig {
242
242
  /** Additional labels managed on new and existing agent pod templates. */
243
243
  podLabels?: Record<string, string>;
244
+ /**
245
+ * Tenant pod restrictions (SSP-7.1): when true, an agent with an account
246
+ * gets the tenant label (`shipwright.dev/tenant`, selected by the tenant
247
+ * NetworkPolicy) and a CPU limit, on provision and reconcile. Off when
248
+ * unset. Sourced from SHIPWRIGHT_SELF_SERVE_ENABLED in buildProvisioner, so
249
+ * the restrictions apply only on deployments with open signup.
250
+ */
251
+ tenantPodRestrictions?: boolean;
244
252
  /** Namespace the agent resources live in. */
245
253
  namespace: string;
246
254
  /** Agent container image (without tag). */
@@ -982,7 +990,8 @@ export class KubernetesAgentProvisioner implements AgentProvisioner {
982
990
  chatServiceUrl: this.config.chatServiceUrl,
983
991
  claudePolicy: this.config.claudePolicy,
984
992
  anthropicFederation: federation,
985
- accountId,
993
+ // The manifest treats an accountId as a tenant pod (label + CPU limit).
994
+ accountId: this.config.tenantPodRestrictions === true ? accountId : null,
986
995
  ...(this.config.podServiceAccount?.enabled && podServiceAccountName
987
996
  ? { podServiceAccountName }
988
997
  : {}),
@@ -125,7 +125,7 @@ describe("KubernetesAgentProvisioner.provision() — default PVC naming", () =>
125
125
  describe("KubernetesAgentProvisioner.provision() — templated PVC naming", () => {
126
126
  it("creates a PVC named by the template when slug is provided via opts", async () => {
127
127
  const agentId = "cmqalfjcm000m4101iharq28k";
128
- const slug = "okwow";
128
+ const slug = "widget";
129
129
 
130
130
  const config: KubernetesAgentProvisionerConfig = {
131
131
  ...BASE_CONFIG,
@@ -143,7 +143,7 @@ describe("KubernetesAgentProvisioner.provision() — templated PVC naming", () =
143
143
 
144
144
  // PVC must use the slug, not the sanitized agentId
145
145
  await expect(
146
- k8s.getPvc(NAMESPACE, "acme-agent-okwow-home"),
146
+ k8s.getPvc(NAMESPACE, "acme-agent-widget-home"),
147
147
  ).resolves.toBeDefined();
148
148
 
149
149
  // The default name must NOT exist
@@ -155,8 +155,8 @@ describe("KubernetesAgentProvisioner.provision() — templated PVC naming", () =
155
155
 
156
156
  it("Deployment is created alongside the template-derived PVC", async () => {
157
157
  const agentId = "cmqalfjcm000m4101iharq28k";
158
- const slug = "okwow";
159
- const expectedPvcName = "acme-agent-okwow-home";
158
+ const slug = "widget";
159
+ const expectedPvcName = "acme-agent-widget-home";
160
160
 
161
161
  const config: KubernetesAgentProvisionerConfig = {
162
162
  ...BASE_CONFIG,
@@ -973,8 +973,8 @@ describe("KubernetesAgentProvisioner.deprovision() — PVC deletion", () => {
973
973
 
974
974
  it("calls deletePvc with the templated name derived from slug", async () => {
975
975
  const agentId = "cmqalfjcm000m4101iharq28k";
976
- const slug = "okwow";
977
- const expectedPvcName = "acme-agent-okwow-home";
976
+ const slug = "widget";
977
+ const expectedPvcName = "acme-agent-widget-home";
978
978
 
979
979
  const config: KubernetesAgentProvisionerConfig = {
980
980
  ...BASE_CONFIG,
@@ -1172,11 +1172,12 @@ describe("canProvision", () => {
1172
1172
  });
1173
1173
  });
1174
1174
 
1175
- describe("KubernetesAgentProvisioner.reconcile() — tenant drift (SSP-7.1)", () => {
1175
+ describe("KubernetesAgentProvisioner — tenant pod restrictions (SSP-7.1)", () => {
1176
1176
  const agentId = "cmqalfjcm000m4101iharq28k";
1177
1177
  const resourceName = sanitizeAgentName(agentId);
1178
+ const TENANT = "shipwright.dev/tenant";
1178
1179
 
1179
- async function seededWithPlatformManifest() {
1180
+ function build(tenantPodRestrictions?: boolean) {
1180
1181
  const k8s = new RecordedKubernetesClient({
1181
1182
  deployments: {},
1182
1183
  secrets: {},
@@ -1185,43 +1186,116 @@ describe("KubernetesAgentProvisioner.reconcile() — tenant drift (SSP-7.1)", ()
1185
1186
  const provisioner = new KubernetesAgentProvisioner(
1186
1187
  k8s,
1187
1188
  stubTokens() as AgentTokenService,
1188
- BASE_CONFIG,
1189
+ {
1190
+ ...BASE_CONFIG,
1191
+ ...(tenantPodRestrictions === undefined
1192
+ ? {}
1193
+ : { tenantPodRestrictions }),
1194
+ },
1189
1195
  );
1190
- await provisioner.provision(agentId); // created as a platform agent
1191
1196
  return { k8s, provisioner };
1192
1197
  }
1193
1198
 
1194
- it("detects a missing tenant label + CPU limit as drift and patches", async () => {
1195
- const { k8s, provisioner } = await seededWithPlatformManifest();
1196
-
1197
- const result = await provisioner.reconcile([
1198
- { id: agentId, accountId: "acct_1" },
1199
- ]);
1199
+ async function seededWithPlatformManifest(tenantPodRestrictions?: boolean) {
1200
+ const built = build(tenantPodRestrictions);
1201
+ await built.provisioner.provision(agentId); // created as a platform agent
1202
+ return built;
1203
+ }
1200
1204
 
1201
- expect(result.updated).toEqual([agentId]);
1205
+ async function deployment(
1206
+ k8s: RecordedKubernetesClient,
1207
+ ): Promise<Record<string, string | undefined>> {
1202
1208
  const dep = await k8s.getDeployment(NAMESPACE, resourceName);
1203
- expect(dep.spec.template.metadata.labels["shipwright.dev/tenant"]).toBe(
1204
- "true",
1205
- );
1206
- expect(dep.spec.template.spec.containers[0]?.resources?.limits?.cpu).toBe(
1207
- "2",
1208
- );
1209
- });
1209
+ return {
1210
+ podLabel: dep.spec.template.metadata.labels[TENANT],
1211
+ deploymentLabel: dep.metadata.labels?.[TENANT],
1212
+ cpuLimit: dep.spec.template.spec.containers[0]?.resources?.limits?.cpu,
1213
+ };
1214
+ }
1210
1215
 
1211
- it("is a no-op once the tenant Deployment matches", async () => {
1212
- const { provisioner } = await seededWithPlatformManifest();
1213
- await provisioner.reconcile([{ id: agentId, accountId: "acct_1" }]);
1214
- const second = await provisioner.reconcile([
1215
- { id: agentId, accountId: "acct_1" },
1216
- ]);
1217
- expect(second.updated).toEqual([]);
1218
- });
1216
+ describe("restrictions on (self-serve signup enabled)", () => {
1217
+ it("provisions an account agent with the tenant label and a CPU limit", async () => {
1218
+ const { k8s, provisioner } = build(true);
1219
+ await provisioner.provision(agentId, { accountId: "acct_1" });
1220
+ expect(await deployment(k8s)).toEqual({
1221
+ podLabel: "true",
1222
+ deploymentLabel: "true",
1223
+ cpuLimit: "2",
1224
+ });
1225
+ });
1219
1226
 
1220
- it("does not touch platform agents (no accountId)", async () => {
1221
- const { provisioner } = await seededWithPlatformManifest();
1222
- const result = await provisioner.reconcile([{ id: agentId }]);
1223
- expect(result.updated).toEqual([]);
1227
+ it("detects a missing tenant label + CPU limit as drift and patches", async () => {
1228
+ const { k8s, provisioner } = await seededWithPlatformManifest(true);
1229
+
1230
+ const result = await provisioner.reconcile([
1231
+ { id: agentId, accountId: "acct_1" },
1232
+ ]);
1233
+
1234
+ expect(result.updated).toEqual([agentId]);
1235
+ const dep = await deployment(k8s);
1236
+ expect(dep.podLabel).toBe("true");
1237
+ expect(dep.cpuLimit).toBe("2");
1238
+ });
1239
+
1240
+ it("is a no-op once the tenant Deployment matches", async () => {
1241
+ const { provisioner } = await seededWithPlatformManifest(true);
1242
+ await provisioner.reconcile([{ id: agentId, accountId: "acct_1" }]);
1243
+ const second = await provisioner.reconcile([
1244
+ { id: agentId, accountId: "acct_1" },
1245
+ ]);
1246
+ expect(second.updated).toEqual([]);
1247
+ });
1248
+
1249
+ it("does not touch platform agents (no accountId)", async () => {
1250
+ const { provisioner } = await seededWithPlatformManifest(true);
1251
+ const result = await provisioner.reconcile([{ id: agentId }]);
1252
+ expect(result.updated).toEqual([]);
1253
+ });
1224
1254
  });
1255
+
1256
+ const offCases: Array<[string, boolean | undefined]> = [
1257
+ ["restrictions off", false],
1258
+ ["restrictions unset", undefined],
1259
+ ];
1260
+ for (const [label, setting] of offCases) {
1261
+ describe(`${label} (self-serve signup disabled)`, () => {
1262
+ it("provisions an account agent with no tenant label and no CPU limit", async () => {
1263
+ const { k8s, provisioner } = build(setting);
1264
+ await provisioner.provision(agentId, { accountId: "acct_1" });
1265
+ expect(await deployment(k8s)).toEqual({
1266
+ podLabel: undefined,
1267
+ deploymentLabel: undefined,
1268
+ cpuLimit: undefined,
1269
+ });
1270
+ });
1271
+
1272
+ it("reconciling an account agent adds no tenant label and no CPU limit", async () => {
1273
+ const { k8s, provisioner } = await seededWithPlatformManifest(setting);
1274
+ const result = await provisioner.reconcile([
1275
+ { id: agentId, accountId: "acct_1" },
1276
+ ]);
1277
+ expect(result.updated).toEqual([]);
1278
+ expect(await deployment(k8s)).toEqual({
1279
+ podLabel: undefined,
1280
+ deploymentLabel: undefined,
1281
+ cpuLimit: undefined,
1282
+ });
1283
+ });
1284
+
1285
+ it("recreating a missing account agent adds no tenant label and no CPU limit", async () => {
1286
+ const { k8s, provisioner } = build(setting);
1287
+ const result = await provisioner.reconcile([
1288
+ { id: agentId, accountId: "acct_1" },
1289
+ ]);
1290
+ expect(result.recreated).toEqual([agentId]);
1291
+ expect(await deployment(k8s)).toEqual({
1292
+ podLabel: undefined,
1293
+ deploymentLabel: undefined,
1294
+ cpuLimit: undefined,
1295
+ });
1296
+ });
1297
+ });
1298
+ }
1225
1299
  });
1226
1300
 
1227
1301
  // ─── Claude policy drift + reconcile patch ──────────────────────────────────
@@ -86,11 +86,18 @@ describeOrSkip("/templates (integration)", () => {
86
86
  callerScopeResolver: async (email, isAdmin) =>
87
87
  isAdmin
88
88
  ? { kind: "all" }
89
- : {
90
- kind: "scoped",
91
- accountId: accountByEmail[email] ?? null,
92
- agentIds: [],
93
- },
89
+ : email === "member-a@example.com"
90
+ ? {
91
+ kind: "scoped",
92
+ accountId: accountA,
93
+ agentIds: [],
94
+ role: "member",
95
+ }
96
+ : {
97
+ kind: "scoped",
98
+ accountId: accountByEmail[email] ?? null,
99
+ agentIds: [],
100
+ },
94
101
  sessionSecret: SECRET,
95
102
  adminApiKeys: parseAdminApiKeys("root:tok-admin:*,bot:tok-agent:agent-1"),
96
103
  agentTokenService: { validate: async () => null },
@@ -505,4 +512,15 @@ describeOrSkip("/templates (integration)", () => {
505
512
  expect(((await res.json()) as { accountId: null }).accountId).toBeNull();
506
513
  });
507
514
  });
515
+
516
+ it("an account member (not owner) is refused on read and write", async () => {
517
+ const member = await cookie("member-a@example.com");
518
+ expect((await call("GET", "/templates", member)).status).toBe(403);
519
+ const put = await call("PUT", "/templates", member, {
520
+ accountId: accountA,
521
+ items: [{ kind: "env", key: "FOO", payload: { value: "x" } }],
522
+ });
523
+ expect(put.status).toBe(403);
524
+ expect(await prisma.agentTemplate.count()).toBe(0);
525
+ });
508
526
  });
@@ -4,15 +4,16 @@
4
4
  * templates and their items.
5
5
  *
6
6
  * Auth: platform admins (admin key or admin session) manage every template,
7
- * including deployment-wide ones. A session user with an account (resolved
8
- * through the caller-scope resolver, never from the request) manages only that
9
- * account's templates. Every other caller (agent keys, platform.* keys,
10
- * members without an account) gets 403.
7
+ * including deployment-wide ones. An account owner session (resolved through
8
+ * the caller-scope resolver, never from the request) manages only that
9
+ * account's templates, like the Settings > Templates page. Every other caller
10
+ * (agent keys, platform.* keys, account members, users without an account)
11
+ * gets 403.
11
12
  */
12
13
 
13
14
  import { createRoute, OpenAPIHono } from "@hono/zod-openapi";
14
15
  import { HTTPException } from "hono/http-exception";
15
- import { accountIdFromScope } from "./account-agent-create.ts";
16
+ import { createAccountIdFromScope } from "./account-agent-create.ts";
16
17
  import type {
17
18
  AgentTemplateService,
18
19
  TemplateActor,
@@ -54,7 +55,7 @@ const errorResponses = {
54
55
  401: { description: "Unauthorized", ...jsonError },
55
56
  403: {
56
57
  description:
57
- "Forbidden (caller has no account, or names another account / the deployment scope)",
58
+ "Forbidden (caller is not an account owner, or names another account / the deployment scope)",
58
59
  ...jsonError,
59
60
  },
60
61
  404: {
@@ -231,12 +232,14 @@ export function createAgentTemplatesApp(
231
232
  }): Promise<TemplateActor> {
232
233
  if (c.get("isAdmin") === true) return { kind: "admin" };
233
234
  const email = c.get("callerEmail");
235
+ // Owners only (members use agents, they do not manage account settings):
236
+ // the same rule as agent creation.
234
237
  const accountId =
235
238
  email === undefined
236
239
  ? null
237
- : accountIdFromScope(await callerScopeResolver(email, false));
240
+ : createAccountIdFromScope(await callerScopeResolver(email, false));
238
241
  if (accountId === null) {
239
- throw new ForbiddenError("Admin or account access required");
242
+ throw new ForbiddenError("Admin or account owner access required");
240
243
  }
241
244
  return { kind: "account", accountId };
242
245
  }
@@ -72,7 +72,7 @@ describeOrSkip("caller scope role resolver (integration)", () => {
72
72
  expect(await role("stranger@x.com")).toBeNull();
73
73
  });
74
74
 
75
- it("owner gets account agents; member only AgentMember agents (flag on)", async () => {
75
+ it("owner gets account agents; member only AgentMember agents (flag off and on)", async () => {
76
76
  const scope = (email: string, flagEnabled: boolean) =>
77
77
  resolveCallerScope({ email, isAdmin: false, flagEnabled }, deps);
78
78
 
@@ -88,11 +88,13 @@ describeOrSkip("caller scope role resolver (integration)", () => {
88
88
  agentIds: [sideAgentId],
89
89
  role: "member",
90
90
  });
91
- expect(await scope("owner@acme.com", false)).toEqual({
92
- kind: "scoped",
93
- accountId: null,
94
- agentIds: [],
95
- });
91
+ // The self-serve flag no longer changes the scope.
92
+ expect(await scope("owner@acme.com", false)).toEqual(
93
+ await scope("owner@acme.com", true),
94
+ );
95
+ expect(await scope("member@acme.com", false)).toEqual(
96
+ await scope("member@acme.com", true),
97
+ );
96
98
  });
97
99
 
98
100
  it("unions and dedupes Agent.repos across the caller's agents", async () => {
@@ -7,9 +7,10 @@
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 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.
10
+ * immediately. Account agents contribute to the scope only for account
11
+ * owners; members get exactly the AgentMember-only scope. The self-serve
12
+ * setting does not change the scope (it controls only signup and the tenant
13
+ * pod restrictions).
13
14
  */
14
15
 
15
16
  import type { AccountService } from "./accounts.ts";
@@ -28,7 +29,11 @@ export type CallerScope =
28
29
  interface CallerScopeInput {
29
30
  email: string;
30
31
  isAdmin: boolean;
31
- flagEnabled: boolean;
32
+ /**
33
+ * @deprecated Ignored. The self-serve setting no longer changes the scope;
34
+ * kept optional so existing callers keep compiling.
35
+ */
36
+ flagEnabled?: boolean;
32
37
  }
33
38
 
34
39
  /** I/O boundary of the resolver — injected so the logic stays unit-testable. */
@@ -55,7 +60,7 @@ export type ViewerRole = "admin" | "owner" | "member";
55
60
  /**
56
61
  * The caller's role: `admin` for allowlisted callers, otherwise the
57
62
  * AccountMember.role (`owner` | `member`), or null when the caller has no
58
- * account. Independent of the self-serve flag.
63
+ * account. Independent of the self-serve setting.
59
64
  */
60
65
  export async function resolveViewerRole(
61
66
  input: { email: string; isAdmin: boolean },
@@ -78,9 +83,7 @@ export async function resolveCallerScope(
78
83
  if (input.isAdmin) return { kind: "all" };
79
84
  const email = input.email.toLowerCase();
80
85
 
81
- const accountId = input.flagEnabled
82
- ? await deps.getAccountIdByEmail(email)
83
- : null;
86
+ const accountId = await deps.getAccountIdByEmail(email);
84
87
  // Only owners get account-wide agents; with no role dep the role is unknown
85
88
  // and the legacy behavior (account agents contribute) is kept.
86
89
  const role = accountId
@@ -114,9 +117,8 @@ export function scopeIncludesAgent(
114
117
  * The single "may manage this agent" predicate (settings, members,
115
118
  * credentials, tools, crons, tokens, delete): platform admins, or the owner
116
119
  * of the agent's own account. Everything else (members, owners of another
117
- * account, callers with no account) only uses the agent. The scope carries
118
- * an accountId only when the self-serve flag is on, so flag-off owners never
119
- * manage. An unknown role or an agent with no account fails closed.
120
+ * account, callers with no account) only uses the agent. An unknown role or
121
+ * an agent with no account fails closed.
120
122
  */
121
123
  export function scopeCanManageAgent(
122
124
  scope: CallerScope,
@@ -134,7 +136,7 @@ export function scopeCanManageAgent(
134
136
  /**
135
137
  * How a caller may read the admin UI's task-store views — Tasks, PRs and
136
138
  * Sessions (SSP-6.8). Platform admins see every account; an account user
137
- * (self-serve flag on, so the resolver produced an accountId) sees exactly
139
+ * (the resolver produced an accountId) sees exactly
138
140
  * their account, filtered server-side via the task-store's `?accountId=`;
139
141
  * anyone else keeps today's admin-only gate.
140
142
  */
@@ -173,18 +175,22 @@ export async function resolveMemberRepoScope(
173
175
  }
174
176
  }
175
177
 
176
- /** Per-request resolver with the flag already bound. */
178
+ /** Per-request resolver for the caller's scope. */
177
179
  export type CallerScopeResolver = (
178
180
  email: string,
179
181
  isAdmin: boolean,
180
182
  ) => Promise<CallerScope>;
181
183
 
184
+ /**
185
+ * `_flagEnabled` is deprecated and ignored (the self-serve setting no longer
186
+ * changes the scope); it stays as an optional parameter so existing callers
187
+ * keep compiling.
188
+ */
182
189
  export function createCallerScopeResolver(
183
190
  deps: CallerScopeDeps,
184
- flagEnabled: boolean,
191
+ _flagEnabled?: boolean,
185
192
  ): CallerScopeResolver {
186
- return (email, isAdmin) =>
187
- resolveCallerScope({ email, isAdmin, flagEnabled }, deps);
193
+ return (email, isAdmin) => resolveCallerScope({ email, isAdmin }, deps);
188
194
  }
189
195
 
190
196
  /** Wire the resolver's I/O to the real services. */
@@ -210,19 +216,19 @@ export function callerScopeDepsFromServices(services: {
210
216
  };
211
217
  }
212
218
 
213
- /** Flag-off resolver backed only by AgentMember rows (today's behavior). */
219
+ /**
220
+ * Resolver backed only by AgentMember rows, for hosts without account
221
+ * services (no account ever contributes agents).
222
+ */
214
223
  export function memberOnlyCallerScopeResolver(
215
224
  agentMemberService: Pick<AgentMemberService, "listByEmail">,
216
225
  ): CallerScopeResolver {
217
- return createCallerScopeResolver(
218
- {
219
- getAccountIdByEmail: async () => null,
220
- listAgentIdsByAccount: async () => [],
221
- listMemberAgentIds: async (email) =>
222
- (await agentMemberService.listByEmail(email)).map((m) => m.agentId),
223
- },
224
- false,
225
- );
226
+ return createCallerScopeResolver({
227
+ getAccountIdByEmail: async () => null,
228
+ listAgentIdsByAccount: async () => [],
229
+ listMemberAgentIds: async (email) =>
230
+ (await agentMemberService.listByEmail(email)).map((m) => m.agentId),
231
+ });
226
232
  }
227
233
 
228
234
  /** Wire the repo-scope resolver's I/O to Prisma. */
@@ -2,6 +2,7 @@ import { describe, expect, it } from "bun:test";
2
2
  import {
3
3
  type CallerScope,
4
4
  type CallerScopeDeps,
5
+ createCallerScopeResolver,
5
6
  resolveCallerScope,
6
7
  resolveMemberRepoScope,
7
8
  resolveViewerRole,
@@ -120,16 +121,19 @@ describe("resolveCallerScope", () => {
120
121
  });
121
122
  });
122
123
 
123
- it("flag off: account membership contributes nothing", async () => {
124
- expect(await run("both@x.com", false, false)).toEqual({
125
- kind: "scoped",
126
- accountId: null,
127
- agentIds: ["ag-2", "ag-9"],
128
- });
129
- expect(await run("a@x.com", false, false)).toEqual({
124
+ it("the self-serve flag does not change the scope", async () => {
125
+ for (const email of ["a@x.com", "both@x.com", "mem@x.com", "m@x.com"]) {
126
+ expect(await run(email, false, false)).toEqual(await run(email));
127
+ }
128
+ });
129
+
130
+ it("createCallerScopeResolver works without a flag", async () => {
131
+ const resolve = createCallerScopeResolver(deps);
132
+ expect(await resolve("a@x.com", false)).toEqual({
130
133
  kind: "scoped",
131
- accountId: null,
132
- agentIds: [],
134
+ accountId: "acct-a",
135
+ agentIds: ["ag-1", "ag-2"],
136
+ role: "owner",
133
137
  });
134
138
  });
135
139
  });
@@ -209,9 +213,9 @@ describe("resolveViewerRole", () => {
209
213
  describe("resolveCallerScope matrix: role x flag", () => {
210
214
  const cases: Array<[string, boolean, string[], string | null]> = [
211
215
  ["a@x.com", true, ["ag-1", "ag-2"], "acct-a"], // owner, flag on
212
- ["a@x.com", false, [], null], // owner, flag off
216
+ ["a@x.com", false, ["ag-1", "ag-2"], "acct-a"], // owner, flag off
213
217
  ["memboth@x.com", true, ["ag-9"], "acct-a"], // member, flag on
214
- ["memboth@x.com", false, ["ag-9"], null], // member, flag off
218
+ ["memboth@x.com", false, ["ag-9"], "acct-a"], // member, flag off
215
219
  ["none@x.com", true, [], null], // no account
216
220
  ["none@x.com", false, [], null],
217
221
  ];