@app-vitals/agent-admin 1.108.0 → 1.109.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/Dockerfile +4 -0
- package/account-move.example.json +22 -0
- package/e2e/real/admin-access.real.e2e.ts +77 -68
- package/e2e/real/agent-management.real.e2e.ts +165 -143
- package/e2e/real/owner-access.real.e2e.ts +228 -212
- package/openapi.platform.json +11 -6
- package/package.json +2 -2
- package/src/account-agent-create.ts +18 -9
- package/src/account-agent-create.unit.test.ts +12 -14
- package/src/account-created-notifier.ts +1 -1
- package/src/account-move.integration.test.ts +594 -0
- package/src/account-move.ts +867 -0
- package/src/account-move.unit.test.ts +206 -0
- package/src/account-onboarding.integration.test.ts +69 -0
- package/src/account-onboarding.ts +29 -3
- package/src/account-trial-expiry-sweeper.ts +1 -1
- package/src/accounts-api.integration.test.ts +5 -2
- package/src/accounts-api.smoke.test.ts +21 -5
- package/src/accounts-api.ts +12 -15
- package/src/admin-boot-config.ts +2 -0
- package/src/admin-boot-config.unit.test.ts +27 -0
- package/src/admin-boot.ts +11 -10
- package/src/admin-local-agent.smoke.test.ts +65 -0
- package/src/admin-ui-account-pages.ts +2 -3
- package/src/admin-ui-account-pages.unit.test.ts +5 -3
- package/src/admin-ui-account.smoke.test.ts +7 -3
- package/src/admin-ui-account.ts +10 -15
- package/src/admin-ui-accounts.smoke.test.ts +4 -4
- package/src/admin-ui-accounts.ts +7 -11
- package/src/admin-ui-pages.ts +16 -5
- package/src/admin-ui-pages.unit.test.ts +25 -20
- package/src/admin-ui-settings-nav-context.smoke.test.ts +24 -0
- package/src/admin-ui-settings.smoke.test.ts +17 -1
- package/src/admin-ui.smoke.test.ts +167 -2
- package/src/admin-ui.ts +75 -50
- package/src/agent-management-access.integration.test.ts +18 -6
- package/src/agent-manifest.ts +2 -1
- package/src/agent-provisioner.integration.test.ts +10 -10
- package/src/agent-provisioner.ts +10 -1
- package/src/agent-provisioner.unit.test.ts +110 -36
- package/src/agent-templates-api.integration.test.ts +23 -5
- package/src/agent-templates-api.ts +11 -8
- package/src/agents-api.smoke.test.ts +106 -8
- package/src/agents-api.ts +5 -3
- package/src/agents.integration.test.ts +112 -2
- package/src/agents.ts +37 -6
- package/src/caller-scope.integration.test.ts +8 -6
- package/src/caller-scope.ts +32 -26
- package/src/caller-scope.unit.test.ts +15 -11
- package/src/github-enterprise-isolation.unit.test.ts +17 -0
- package/src/move-accounts.ts +201 -0
- package/src/openapi-schemas.ts +6 -0
- package/src/platform-routes.ts +2 -2
- package/src/self-serve-config.ts +8 -8
- package/src/self-serve-config.unit.test.ts +3 -10
|
@@ -86,11 +86,18 @@ describeOrSkip("/templates (integration)", () => {
|
|
|
86
86
|
callerScopeResolver: async (email, isAdmin) =>
|
|
87
87
|
isAdmin
|
|
88
88
|
? { kind: "all" }
|
|
89
|
-
:
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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.
|
|
8
|
-
*
|
|
9
|
-
* account's templates. Every other caller
|
|
10
|
-
* members without an account)
|
|
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 {
|
|
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
|
|
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
|
-
:
|
|
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
|
}
|
|
@@ -11,7 +11,7 @@ import { generateKeyPairSync } from "node:crypto";
|
|
|
11
11
|
import { readFileSync } from "node:fs";
|
|
12
12
|
import { sign } from "hono/jwt";
|
|
13
13
|
import type { Prisma } from "../prisma/client/client.ts";
|
|
14
|
-
import {
|
|
14
|
+
import { getOwnerAgentSpecialty } from "./account-agent-create.ts";
|
|
15
15
|
import type { PatchAgentCronRunInput } from "./agent-cron-runs.ts";
|
|
16
16
|
import { AgentEnvService } from "./agent-envs.ts";
|
|
17
17
|
import type {
|
|
@@ -68,7 +68,7 @@ function fakeSpecialtyTypes(
|
|
|
68
68
|
// (the platform test run points it at the fixture specialty, not "coding").
|
|
69
69
|
byType: Record<string, SpecialtyType> = {
|
|
70
70
|
coding: CODING_TYPE,
|
|
71
|
-
[
|
|
71
|
+
[getOwnerAgentSpecialty()]: CODING_TYPE,
|
|
72
72
|
},
|
|
73
73
|
): AdminDeps["specialtyRegistry"] {
|
|
74
74
|
return {
|
|
@@ -5392,7 +5392,7 @@ describe("admin API — account users create and delete agents (SSP-4.2)", () =>
|
|
|
5392
5392
|
},
|
|
5393
5393
|
});
|
|
5394
5394
|
|
|
5395
|
-
it("account user creates an agent stamped with their own accountId and the
|
|
5395
|
+
it("account user creates an agent stamped with their own accountId and the default type", async () => {
|
|
5396
5396
|
const { deps, created } = makeAccountDeps("acct-a");
|
|
5397
5397
|
const res = await post(
|
|
5398
5398
|
createAdminApp(deps),
|
|
@@ -5403,7 +5403,7 @@ describe("admin API — account users create and delete agents (SSP-4.2)", () =>
|
|
|
5403
5403
|
expect(created).toHaveLength(1);
|
|
5404
5404
|
expect(created[0]).toMatchObject({
|
|
5405
5405
|
accountId: "acct-a",
|
|
5406
|
-
specialty:
|
|
5406
|
+
specialty: getOwnerAgentSpecialty(),
|
|
5407
5407
|
});
|
|
5408
5408
|
});
|
|
5409
5409
|
|
|
@@ -5417,7 +5417,7 @@ describe("admin API — account users create and delete agents (SSP-4.2)", () =>
|
|
|
5417
5417
|
expect(res.status).toBe(200);
|
|
5418
5418
|
expect(created[0]).toMatchObject({
|
|
5419
5419
|
accountId: "acct-a",
|
|
5420
|
-
specialty:
|
|
5420
|
+
specialty: getOwnerAgentSpecialty(),
|
|
5421
5421
|
});
|
|
5422
5422
|
});
|
|
5423
5423
|
|
|
@@ -5443,17 +5443,115 @@ describe("admin API — account users create and delete agents (SSP-4.2)", () =>
|
|
|
5443
5443
|
expect(created).toHaveLength(0);
|
|
5444
5444
|
});
|
|
5445
5445
|
|
|
5446
|
-
|
|
5447
|
-
|
|
5446
|
+
// A transaction double answering the few queries createAgent() makes for
|
|
5447
|
+
// account assignment and the quota check.
|
|
5448
|
+
function fakeTx(accounts: string[], opts: { agents?: number } = {}) {
|
|
5449
|
+
return {
|
|
5450
|
+
account: {
|
|
5451
|
+
findMany: async () => accounts.map((id) => ({ id })),
|
|
5452
|
+
},
|
|
5453
|
+
$queryRaw: async () => [{ status: "active", maxAgents: 1 }],
|
|
5454
|
+
agent: { count: async () => opts.agents ?? 0 },
|
|
5455
|
+
agentEnv: { findMany: async () => [] },
|
|
5456
|
+
} as never;
|
|
5457
|
+
}
|
|
5458
|
+
|
|
5459
|
+
function depsWithAccounts(
|
|
5460
|
+
accounts: string[],
|
|
5461
|
+
opts: { agents?: number } = {},
|
|
5462
|
+
accountId: string | null = "acct-a",
|
|
5463
|
+
) {
|
|
5464
|
+
const made = makeAccountDeps(accountId);
|
|
5465
|
+
const tx = fakeTx(accounts, opts);
|
|
5466
|
+
made.deps = {
|
|
5467
|
+
...made.deps,
|
|
5468
|
+
agentService: {
|
|
5469
|
+
...made.deps.agentService,
|
|
5470
|
+
runTransaction: async (fn: (tx: never) => Promise<unknown>) => fn(tx),
|
|
5471
|
+
} as never,
|
|
5472
|
+
};
|
|
5473
|
+
return made;
|
|
5474
|
+
}
|
|
5475
|
+
|
|
5476
|
+
it("platform admin create with no accounts stays unassigned", async () => {
|
|
5477
|
+
const { deps, created } = depsWithAccounts([]);
|
|
5448
5478
|
const res = await post(
|
|
5449
5479
|
createAdminApp(deps),
|
|
5450
|
-
{ name: "Admin Made", specialty: "coding"
|
|
5480
|
+
{ name: "Admin Made", specialty: "coding" },
|
|
5451
5481
|
await makeSessionCookie(),
|
|
5452
5482
|
);
|
|
5453
5483
|
expect(res.status).toBe(200);
|
|
5454
5484
|
expect(created[0]?.accountId).toBeUndefined();
|
|
5455
5485
|
});
|
|
5456
5486
|
|
|
5487
|
+
it("platform admin create with one account assigns it without asking", async () => {
|
|
5488
|
+
const { deps, created } = depsWithAccounts(["acct-only"]);
|
|
5489
|
+
const res = await post(
|
|
5490
|
+
createAdminApp(deps),
|
|
5491
|
+
{ name: "Admin Made", specialty: "coding" },
|
|
5492
|
+
await makeSessionCookie(),
|
|
5493
|
+
);
|
|
5494
|
+
expect(res.status).toBe(200);
|
|
5495
|
+
expect(created[0]).toMatchObject({
|
|
5496
|
+
accountId: "acct-only",
|
|
5497
|
+
specialty: "coding",
|
|
5498
|
+
});
|
|
5499
|
+
});
|
|
5500
|
+
|
|
5501
|
+
it("platform admin create with several accounts and none named is a clear 400", async () => {
|
|
5502
|
+
const { deps, created } = depsWithAccounts(["acct-a", "acct-b"]);
|
|
5503
|
+
const res = await post(
|
|
5504
|
+
createAdminApp(deps),
|
|
5505
|
+
{ name: "Admin Made", specialty: "coding" },
|
|
5506
|
+
await makeSessionCookie(),
|
|
5507
|
+
);
|
|
5508
|
+
expect(res.status).toBe(400);
|
|
5509
|
+
expect(((await res.json()) as { error: string }).error).toContain(
|
|
5510
|
+
"Choose the account",
|
|
5511
|
+
);
|
|
5512
|
+
expect(created).toHaveLength(0);
|
|
5513
|
+
});
|
|
5514
|
+
|
|
5515
|
+
it("platform admin create with several accounts uses the named account and type", async () => {
|
|
5516
|
+
const { deps, created } = depsWithAccounts(["acct-a", "acct-b"]);
|
|
5517
|
+
const res = await post(
|
|
5518
|
+
createAdminApp(deps),
|
|
5519
|
+
{ name: "Admin Made", specialty: "coding", accountId: "acct-b" },
|
|
5520
|
+
await makeSessionCookie(),
|
|
5521
|
+
);
|
|
5522
|
+
expect(res.status).toBe(200);
|
|
5523
|
+
expect(created[0]).toMatchObject({
|
|
5524
|
+
accountId: "acct-b",
|
|
5525
|
+
specialty: "coding",
|
|
5526
|
+
});
|
|
5527
|
+
});
|
|
5528
|
+
|
|
5529
|
+
it("platform admin is not limited by the account's agent limit", async () => {
|
|
5530
|
+
const { deps, created } = depsWithAccounts(["acct-a"], { agents: 5 });
|
|
5531
|
+
const res = await post(
|
|
5532
|
+
createAdminApp(deps),
|
|
5533
|
+
{ name: "Admin Made", specialty: "coding" },
|
|
5534
|
+
await makeSessionCookie(),
|
|
5535
|
+
);
|
|
5536
|
+
expect(res.status).toBe(200);
|
|
5537
|
+
expect(created).toHaveLength(1);
|
|
5538
|
+
});
|
|
5539
|
+
|
|
5540
|
+
it("an owner at the limit gets the neutral limit message", async () => {
|
|
5541
|
+
const { deps, created } = depsWithAccounts(["acct-a"], { agents: 1 });
|
|
5542
|
+
const res = await post(
|
|
5543
|
+
createAdminApp(deps),
|
|
5544
|
+
{ name: "Mine", specialty: "coding" },
|
|
5545
|
+
await accountCookie(),
|
|
5546
|
+
);
|
|
5547
|
+
expect(res.status).toBe(403);
|
|
5548
|
+
const message = ((await res.json()) as { error: string }).error;
|
|
5549
|
+
expect(message).toContain("agent limit");
|
|
5550
|
+
expect(message.toLowerCase()).not.toContain("trial");
|
|
5551
|
+
expect(message).not.toContain("@");
|
|
5552
|
+
expect(created).toHaveLength(0);
|
|
5553
|
+
});
|
|
5554
|
+
|
|
5457
5555
|
it.each([
|
|
5458
5556
|
["quota_exceeded", "agent limit"],
|
|
5459
5557
|
["account_inactive", "not active"],
|
package/src/agents-api.ts
CHANGED
|
@@ -37,7 +37,7 @@ import {
|
|
|
37
37
|
ACCOUNT_CREATE_ERROR_MESSAGES,
|
|
38
38
|
accountIdFromScope,
|
|
39
39
|
createAccountIdFromScope,
|
|
40
|
-
|
|
40
|
+
getOwnerAgentSpecialty,
|
|
41
41
|
isAccountCreateErrorCode,
|
|
42
42
|
} from "./account-agent-create.ts";
|
|
43
43
|
import {
|
|
@@ -2716,7 +2716,9 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
2716
2716
|
// unassigned agents). A session user with an account (flag on) may create
|
|
2717
2717
|
// agents in THEIR account only: accountId comes from the resolved scope —
|
|
2718
2718
|
// never the body — and the type is forced to the self-serve specialty.
|
|
2719
|
-
|
|
2719
|
+
// Admins may name an account (body.accountId); with none named,
|
|
2720
|
+
// createAgent() picks the only account or asks for a choice.
|
|
2721
|
+
let accountId: string | undefined = body.accountId;
|
|
2720
2722
|
let memberCreator: MemberCreator = {
|
|
2721
2723
|
platformAdmin: true,
|
|
2722
2724
|
ownerOfAccountId: null,
|
|
@@ -2776,7 +2778,7 @@ export function createAdminApp(deps: AdminDeps): OpenAPIHono<AdminAuthEnv> {
|
|
|
2776
2778
|
{
|
|
2777
2779
|
name: body.name,
|
|
2778
2780
|
specialty:
|
|
2779
|
-
|
|
2781
|
+
c.get("isAdmin") === true ? body.specialty : getOwnerAgentSpecialty(),
|
|
2780
2782
|
...(accountId !== undefined ? { accountId } : {}),
|
|
2781
2783
|
// PAI-1.2: owner comes from the authenticated caller only; the body
|
|
2782
2784
|
// schema has no owner field, so a client-supplied one is stripped.
|
|
@@ -252,7 +252,7 @@ describeOrSkip("createAgent() — integration (real Postgres)", () => {
|
|
|
252
252
|
expect(await prisma.accountMember.findMany()).toEqual([]);
|
|
253
253
|
});
|
|
254
254
|
|
|
255
|
-
it("with several accounts and
|
|
255
|
+
it("with several accounts and an owner-less agent, a member email still fails and rolls back records made for earlier emails", async () => {
|
|
256
256
|
const acct = await prisma.account.create({ data: { name: "A" } });
|
|
257
257
|
await prisma.accountMember.create({
|
|
258
258
|
data: { accountId: acct.id, email: "known@example.com", role: "owner" },
|
|
@@ -262,7 +262,9 @@ describeOrSkip("createAgent() — integration (real Postgres)", () => {
|
|
|
262
262
|
deps(),
|
|
263
263
|
baseInput({
|
|
264
264
|
memberEmailsRaw: "known@example.com\nnew@example.com",
|
|
265
|
-
memberCreator:
|
|
265
|
+
// No memberCreator: no account is chosen for the agent, so the new
|
|
266
|
+
// email has nowhere to go.
|
|
267
|
+
memberCreator: { platformAdmin: false, ownerOfAccountId: null },
|
|
266
268
|
}),
|
|
267
269
|
);
|
|
268
270
|
expect(result.ok).toBe(false);
|
|
@@ -593,6 +595,114 @@ describeOrSkip("createAgent() — integration (real Postgres)", () => {
|
|
|
593
595
|
});
|
|
594
596
|
});
|
|
595
597
|
|
|
598
|
+
describe("account assignment for new agents", () => {
|
|
599
|
+
const admin = { platformAdmin: true, ownerOfAccountId: null };
|
|
600
|
+
|
|
601
|
+
it("with no accounts, an admin create stays unassigned", async () => {
|
|
602
|
+
const result = await createAgent(
|
|
603
|
+
deps(),
|
|
604
|
+
baseInput({ memberCreator: admin }),
|
|
605
|
+
);
|
|
606
|
+
if (!result.ok) throw new Error("expected success");
|
|
607
|
+
expect(result.agent.accountId).toBeNull();
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
it("with one account, an admin create is assigned to it without asking", async () => {
|
|
611
|
+
const acct = await prisma.account.create({ data: { name: "Solo" } });
|
|
612
|
+
const result = await createAgent(
|
|
613
|
+
deps(),
|
|
614
|
+
baseInput({ memberCreator: admin }),
|
|
615
|
+
);
|
|
616
|
+
if (!result.ok) throw new Error("expected success");
|
|
617
|
+
expect(result.agent.accountId).toBe(acct.id);
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
it("with one account, a new member email gets a membership in the agent's account", async () => {
|
|
621
|
+
const acct = await prisma.account.create({ data: { name: "Solo" } });
|
|
622
|
+
const result = await createAgent(
|
|
623
|
+
deps(),
|
|
624
|
+
baseInput({ memberCreator: admin, memberEmailsRaw: "new@example.com" }),
|
|
625
|
+
);
|
|
626
|
+
if (!result.ok) throw new Error("expected success");
|
|
627
|
+
const member = await prisma.accountMember.findUniqueOrThrow({
|
|
628
|
+
where: { email: "new@example.com" },
|
|
629
|
+
});
|
|
630
|
+
expect(member.accountId).toBe(acct.id);
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
it("with several accounts, an admin create without an account is rejected and writes nothing", async () => {
|
|
634
|
+
await prisma.account.create({ data: { name: "One" } });
|
|
635
|
+
await prisma.account.create({ data: { name: "Two" } });
|
|
636
|
+
const result = await createAgent(
|
|
637
|
+
deps(),
|
|
638
|
+
baseInput({ memberCreator: admin }),
|
|
639
|
+
);
|
|
640
|
+
expect(result.ok).toBe(false);
|
|
641
|
+
if (result.ok) throw new Error("expected failure");
|
|
642
|
+
expect(result.errorCode).toBe("account_required");
|
|
643
|
+
expect(result.errorMessage).toContain("Choose the account");
|
|
644
|
+
expect(await prisma.agent.count()).toBe(0);
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it("with several accounts, an admin create naming an account uses it and adds members there", async () => {
|
|
648
|
+
await prisma.account.create({ data: { name: "One" } });
|
|
649
|
+
const two = await prisma.account.create({ data: { name: "Two" } });
|
|
650
|
+
const result = await createAgent(
|
|
651
|
+
deps(),
|
|
652
|
+
baseInput({
|
|
653
|
+
memberCreator: admin,
|
|
654
|
+
accountId: two.id,
|
|
655
|
+
memberEmailsRaw: "new@example.com",
|
|
656
|
+
}),
|
|
657
|
+
);
|
|
658
|
+
if (!result.ok) throw new Error("expected success");
|
|
659
|
+
expect(result.agent.accountId).toBe(two.id);
|
|
660
|
+
expect(
|
|
661
|
+
(
|
|
662
|
+
await prisma.accountMember.findUniqueOrThrow({
|
|
663
|
+
where: { email: "new@example.com" },
|
|
664
|
+
})
|
|
665
|
+
).accountId,
|
|
666
|
+
).toBe(two.id);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it("an owner stays limited by maxAgents while an admin is not", async () => {
|
|
670
|
+
const acct = await prisma.account.create({
|
|
671
|
+
data: { name: "Capped", maxAgents: 1 },
|
|
672
|
+
});
|
|
673
|
+
const owner = { platformAdmin: false, ownerOfAccountId: acct.id };
|
|
674
|
+
const first = await createAgent(
|
|
675
|
+
deps(),
|
|
676
|
+
baseInput({ name: "first", accountId: acct.id, memberCreator: owner }),
|
|
677
|
+
);
|
|
678
|
+
expect(first.ok).toBe(true);
|
|
679
|
+
const blocked = await createAgent(
|
|
680
|
+
deps(),
|
|
681
|
+
baseInput({ name: "second", accountId: acct.id, memberCreator: owner }),
|
|
682
|
+
);
|
|
683
|
+
expect(blocked).toEqual({ ok: false, errorCode: "quota_exceeded" });
|
|
684
|
+
const byAdmin = await createAgent(
|
|
685
|
+
deps(),
|
|
686
|
+
baseInput({ name: "third", accountId: acct.id, memberCreator: admin }),
|
|
687
|
+
);
|
|
688
|
+
expect(byAdmin.ok).toBe(true);
|
|
689
|
+
expect(await prisma.agent.count({ where: { accountId: acct.id } })).toBe(
|
|
690
|
+
2,
|
|
691
|
+
);
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it("an admin cannot create in an inactive account", async () => {
|
|
695
|
+
const acct = await prisma.account.create({
|
|
696
|
+
data: { name: "Off", status: "suspended" },
|
|
697
|
+
});
|
|
698
|
+
const result = await createAgent(
|
|
699
|
+
deps(),
|
|
700
|
+
baseInput({ accountId: acct.id, memberCreator: admin }),
|
|
701
|
+
);
|
|
702
|
+
expect(result).toEqual({ ok: false, errorCode: "account_inactive" });
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
|
|
596
706
|
describe("account quota (SSP-4.1)", () => {
|
|
597
707
|
async function makeAccount(
|
|
598
708
|
overrides: { status?: string; maxAgents?: number } = {},
|
package/src/agents.ts
CHANGED
|
@@ -829,6 +829,7 @@ type CreateAgentErrorCode =
|
|
|
829
829
|
| "provision_failed"
|
|
830
830
|
| "quota_exceeded"
|
|
831
831
|
| "account_inactive"
|
|
832
|
+
| "account_required"
|
|
832
833
|
| "member_account_unresolved"
|
|
833
834
|
| "member_not_permitted"
|
|
834
835
|
| typeof ANTHROPIC_CREDENTIALS_BLOCKED_CODE;
|
|
@@ -909,16 +910,23 @@ function parseLines(
|
|
|
909
910
|
async function checkAccountQuota(
|
|
910
911
|
tx: PrismaTransactionClient,
|
|
911
912
|
accountId: string,
|
|
913
|
+
opts: { enforceQuota?: boolean } = {},
|
|
912
914
|
): Promise<"account_inactive" | "quota_exceeded" | null> {
|
|
913
915
|
const rows = await tx.$queryRaw<{ status: string; maxAgents: number }[]>`
|
|
914
916
|
SELECT "status", "maxAgents" FROM "Account" WHERE "id" = ${accountId} FOR UPDATE`;
|
|
915
917
|
const account = rows[0];
|
|
916
918
|
if (!account) return "account_inactive";
|
|
917
919
|
if (account.status !== "active") return "account_inactive";
|
|
920
|
+
// Platform admins are not limited by the per-account agent count.
|
|
921
|
+
if (opts.enforceQuota === false) return null;
|
|
918
922
|
const count = await tx.agent.count({ where: { accountId } });
|
|
919
923
|
return count >= account.maxAgents ? "quota_exceeded" : null;
|
|
920
924
|
}
|
|
921
925
|
|
|
926
|
+
/** Shown when an admin creates an agent without an account and several exist. */
|
|
927
|
+
export const ACCOUNT_REQUIRED_MESSAGE =
|
|
928
|
+
"More than one account exists. Choose the account for this agent.";
|
|
929
|
+
|
|
922
930
|
/** Thrown inside the create transaction so a member failure rolls everything back. */
|
|
923
931
|
class MemberEnsureFailure extends Error {
|
|
924
932
|
constructor(
|
|
@@ -1028,8 +1036,33 @@ export async function createAgent(
|
|
|
1028
1036
|
// Quota enforcement must precede the create so a rejected request
|
|
1029
1037
|
// writes nothing. Skipped when tx is undefined (admin-ui.ts test
|
|
1030
1038
|
// doubles without runTransaction pass no real transaction).
|
|
1031
|
-
|
|
1032
|
-
|
|
1039
|
+
// Every new agent gets an account when accounts exist: a platform
|
|
1040
|
+
// admin who names none gets the only account, or must choose one when
|
|
1041
|
+
// there are several. No account rows keeps the unassigned behavior.
|
|
1042
|
+
let accountId = input.accountId;
|
|
1043
|
+
if (
|
|
1044
|
+
accountId === undefined &&
|
|
1045
|
+
tx &&
|
|
1046
|
+
input.memberCreator?.platformAdmin
|
|
1047
|
+
) {
|
|
1048
|
+
const accounts = await tx.account.findMany({
|
|
1049
|
+
select: { id: true },
|
|
1050
|
+
take: 2,
|
|
1051
|
+
});
|
|
1052
|
+
if (accounts.length === 1) {
|
|
1053
|
+
accountId = (accounts[0] as { id: string }).id;
|
|
1054
|
+
} else if (accounts.length > 1) {
|
|
1055
|
+
return {
|
|
1056
|
+
ok: false,
|
|
1057
|
+
errorCode: "account_required",
|
|
1058
|
+
errorMessage: ACCOUNT_REQUIRED_MESSAGE,
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
if (accountId !== undefined && tx) {
|
|
1063
|
+
const denied = await checkAccountQuota(tx, accountId, {
|
|
1064
|
+
enforceQuota: input.memberCreator?.platformAdmin !== true,
|
|
1065
|
+
});
|
|
1033
1066
|
if (denied) return { ok: false, errorCode: denied };
|
|
1034
1067
|
}
|
|
1035
1068
|
|
|
@@ -1039,9 +1072,7 @@ export async function createAgent(
|
|
|
1039
1072
|
selfHosted: !inCluster,
|
|
1040
1073
|
specialty,
|
|
1041
1074
|
restrictSlackToMembers,
|
|
1042
|
-
...(
|
|
1043
|
-
? { accountId: input.accountId }
|
|
1044
|
-
: {}),
|
|
1075
|
+
...(accountId !== undefined ? { accountId } : {}),
|
|
1045
1076
|
...(ownerEmail !== undefined ? { ownerEmail } : {}),
|
|
1046
1077
|
...(input.templateDetached ? { templateDetached: true } : {}),
|
|
1047
1078
|
},
|
|
@@ -1178,7 +1209,7 @@ export async function createAgent(
|
|
|
1178
1209
|
if (tx) {
|
|
1179
1210
|
const ensured = await ensureAccountMember(tx, {
|
|
1180
1211
|
email,
|
|
1181
|
-
agentAccountId:
|
|
1212
|
+
agentAccountId: accountId ?? null,
|
|
1182
1213
|
...(input.memberCreator
|
|
1183
1214
|
? { creator: input.memberCreator }
|
|
1184
1215
|
: {}),
|
|
@@ -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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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 () => {
|
package/src/caller-scope.ts
CHANGED
|
@@ -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
|
|
11
|
-
*
|
|
12
|
-
* the
|
|
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
|
-
|
|
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
|
|
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 =
|
|
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.
|
|
118
|
-
* an
|
|
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
|
-
* (
|
|
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
|
|
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
|
-
|
|
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
|
-
/**
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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. */
|