@app-vitals/agent-admin 1.103.0 → 1.105.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 +2 -2
- package/src/account-agent-create.ts +16 -0
- package/src/account-agent-create.unit.test.ts +29 -1
- package/src/admin-ui.smoke.test.ts +51 -0
- package/src/admin-ui.ts +4 -1
- package/src/agents-api.smoke.test.ts +16 -1
- package/src/agents-api.ts +5 -2
- package/src/caller-scope.integration.test.ts +35 -0
- package/src/caller-scope.ts +48 -1
- package/src/caller-scope.unit.test.ts +65 -1
- package/src/chat.smoke.test.ts +144 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.105.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.
|
|
47
|
+
"@app-vitals/agent-lib": "1.105.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 {
|
|
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
|
|
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(
|
|
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
|
-
:
|
|
2728
|
+
: createAccountIdFromScope(await callerScopeResolver(email, false));
|
|
2728
2729
|
if (callerAccountId === null) {
|
|
2729
|
-
throw new ForbiddenError(
|
|
2730
|
+
throw new ForbiddenError(
|
|
2731
|
+
"Owner or admin access required to create agent",
|
|
2732
|
+
);
|
|
2730
2733
|
}
|
|
2731
2734
|
accountId = callerAccountId;
|
|
2732
2735
|
}
|
|
@@ -11,8 +11,10 @@ import { AccountMemberService } from "./account-members.ts";
|
|
|
11
11
|
import { AccountService } from "./accounts.ts";
|
|
12
12
|
import { AgentMemberService } from "./agent-members.ts";
|
|
13
13
|
import {
|
|
14
|
+
agentRepoDepsFromPrisma,
|
|
14
15
|
callerScopeDepsFromServices,
|
|
15
16
|
resolveCallerScope,
|
|
17
|
+
resolveMemberRepoScope,
|
|
16
18
|
resolveViewerRole,
|
|
17
19
|
} from "./caller-scope.ts";
|
|
18
20
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
@@ -84,6 +86,7 @@ describeOrSkip("caller scope role resolver (integration)", () => {
|
|
|
84
86
|
kind: "scoped",
|
|
85
87
|
accountId,
|
|
86
88
|
agentIds: [sideAgentId],
|
|
89
|
+
role: "member",
|
|
87
90
|
});
|
|
88
91
|
expect(await scope("owner@acme.com", false)).toEqual({
|
|
89
92
|
kind: "scoped",
|
|
@@ -91,4 +94,36 @@ describeOrSkip("caller scope role resolver (integration)", () => {
|
|
|
91
94
|
agentIds: [],
|
|
92
95
|
});
|
|
93
96
|
});
|
|
97
|
+
|
|
98
|
+
it("unions and dedupes Agent.repos across the caller's agents", async () => {
|
|
99
|
+
await prisma.agent.update({
|
|
100
|
+
where: { id: accountAgentIds[0] as string },
|
|
101
|
+
data: { repos: ["o/x", "o/y"] },
|
|
102
|
+
});
|
|
103
|
+
await prisma.agent.update({
|
|
104
|
+
where: { id: accountAgentIds[1] as string },
|
|
105
|
+
data: { repos: ["o/y", "o/z"] },
|
|
106
|
+
});
|
|
107
|
+
await prisma.agent.update({
|
|
108
|
+
where: { id: sideAgentId },
|
|
109
|
+
data: { repos: ["o/side"] },
|
|
110
|
+
});
|
|
111
|
+
const repoDeps = agentRepoDepsFromPrisma(prisma);
|
|
112
|
+
const scope = (email: string) =>
|
|
113
|
+
resolveCallerScope({ email, isAdmin: false, flagEnabled: true }, deps);
|
|
114
|
+
|
|
115
|
+
const owner = await resolveMemberRepoScope(
|
|
116
|
+
await scope("owner@acme.com"),
|
|
117
|
+
repoDeps,
|
|
118
|
+
);
|
|
119
|
+
expect(owner?.sort()).toEqual(["o/x", "o/y", "o/z"]);
|
|
120
|
+
const member = await resolveMemberRepoScope(
|
|
121
|
+
await scope("member@acme.com"),
|
|
122
|
+
repoDeps,
|
|
123
|
+
);
|
|
124
|
+
expect(member).toEqual(["o/side"]);
|
|
125
|
+
expect(
|
|
126
|
+
await resolveMemberRepoScope(await scope("stranger@x.com"), repoDeps),
|
|
127
|
+
).toEqual([]);
|
|
128
|
+
});
|
|
94
129
|
});
|
package/src/caller-scope.ts
CHANGED
|
@@ -17,7 +17,13 @@ import type { AgentMemberService } from "./agent-members.ts";
|
|
|
17
17
|
|
|
18
18
|
export type CallerScope =
|
|
19
19
|
| { kind: "all" }
|
|
20
|
-
| {
|
|
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
|
|
|
@@ -125,6 +132,26 @@ export function taskStoreViewScope(scope: CallerScope): TaskStoreViewScope {
|
|
|
125
132
|
};
|
|
126
133
|
}
|
|
127
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Repos a caller's views may show (ACM-7.5): the deduplicated union of
|
|
137
|
+
* Agent.repos over the agents in their scope. `null` means unrestricted
|
|
138
|
+
* (admin). Fails closed: an empty scope or any lookup failure yields `[]`
|
|
139
|
+
* (no access), matching the task store's own agent-token scope.
|
|
140
|
+
*/
|
|
141
|
+
export async function resolveMemberRepoScope(
|
|
142
|
+
scope: CallerScope,
|
|
143
|
+
deps: { listAgentRepos(agentIds: string[]): Promise<string[][]> },
|
|
144
|
+
): Promise<string[] | null> {
|
|
145
|
+
if (scope.kind === "all") return null;
|
|
146
|
+
if (scope.agentIds.length === 0) return [];
|
|
147
|
+
try {
|
|
148
|
+
const lists = await deps.listAgentRepos(scope.agentIds);
|
|
149
|
+
return [...new Set(lists.flat())];
|
|
150
|
+
} catch {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
128
155
|
/** Per-request resolver with the flag already bound. */
|
|
129
156
|
export type CallerScopeResolver = (
|
|
130
157
|
email: string,
|
|
@@ -176,3 +203,23 @@ export function memberOnlyCallerScopeResolver(
|
|
|
176
203
|
false,
|
|
177
204
|
);
|
|
178
205
|
}
|
|
206
|
+
|
|
207
|
+
/** Wire the repo-scope resolver's I/O to Prisma. */
|
|
208
|
+
export function agentRepoDepsFromPrisma(prisma: {
|
|
209
|
+
agent: {
|
|
210
|
+
findMany(args: {
|
|
211
|
+
where: { id: { in: string[] } };
|
|
212
|
+
select: { repos: true };
|
|
213
|
+
}): Promise<{ repos: string[] }[]>;
|
|
214
|
+
};
|
|
215
|
+
}): Parameters<typeof resolveMemberRepoScope>[1] {
|
|
216
|
+
return {
|
|
217
|
+
listAgentRepos: async (agentIds) =>
|
|
218
|
+
(
|
|
219
|
+
await prisma.agent.findMany({
|
|
220
|
+
where: { id: { in: agentIds } },
|
|
221
|
+
select: { repos: true },
|
|
222
|
+
})
|
|
223
|
+
).map((a) => a.repos),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
type CallerScope,
|
|
4
4
|
type CallerScopeDeps,
|
|
5
5
|
resolveCallerScope,
|
|
6
|
+
resolveMemberRepoScope,
|
|
6
7
|
resolveViewerRole,
|
|
7
8
|
scopeIncludesAgent,
|
|
8
9
|
taskStoreViewScope,
|
|
@@ -60,6 +61,7 @@ describe("resolveCallerScope", () => {
|
|
|
60
61
|
kind: "scoped",
|
|
61
62
|
accountId: "acct-a",
|
|
62
63
|
agentIds: ["ag-1", "ag-2"],
|
|
64
|
+
role: "owner",
|
|
63
65
|
});
|
|
64
66
|
});
|
|
65
67
|
|
|
@@ -68,11 +70,13 @@ describe("resolveCallerScope", () => {
|
|
|
68
70
|
kind: "scoped",
|
|
69
71
|
accountId: "acct-a",
|
|
70
72
|
agentIds: [],
|
|
73
|
+
role: "member",
|
|
71
74
|
});
|
|
72
75
|
expect(await run("memboth@x.com")).toEqual({
|
|
73
76
|
kind: "scoped",
|
|
74
77
|
accountId: "acct-a",
|
|
75
78
|
agentIds: ["ag-9"],
|
|
79
|
+
role: "member",
|
|
76
80
|
});
|
|
77
81
|
});
|
|
78
82
|
|
|
@@ -212,7 +216,7 @@ describe("resolveCallerScope matrix: role x flag", () => {
|
|
|
212
216
|
];
|
|
213
217
|
for (const [email, flag, agentIds, accountId] of cases) {
|
|
214
218
|
it(`${email} flag=${flag}`, async () => {
|
|
215
|
-
expect(await run(email, false, flag)).
|
|
219
|
+
expect(await run(email, false, flag)).toMatchObject({
|
|
216
220
|
kind: "scoped",
|
|
217
221
|
accountId,
|
|
218
222
|
agentIds,
|
|
@@ -224,3 +228,63 @@ describe("resolveCallerScope matrix: role x flag", () => {
|
|
|
224
228
|
expect(await run("a@x.com", true, true)).toEqual({ kind: "all" });
|
|
225
229
|
});
|
|
226
230
|
});
|
|
231
|
+
|
|
232
|
+
describe("resolveMemberRepoScope", () => {
|
|
233
|
+
const scoped = (agentIds: string[]): CallerScope => ({
|
|
234
|
+
kind: "scoped",
|
|
235
|
+
accountId: null,
|
|
236
|
+
agentIds,
|
|
237
|
+
});
|
|
238
|
+
const repoDeps = (byAgent: Record<string, string[]>) => ({
|
|
239
|
+
listAgentRepos: async (ids: string[]) => ids.map((i) => byAgent[i] ?? []),
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("returns null for admins without a lookup", async () => {
|
|
243
|
+
const deps = {
|
|
244
|
+
listAgentRepos: async () => {
|
|
245
|
+
throw new Error("should not be called");
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
expect(await resolveMemberRepoScope({ kind: "all" }, deps)).toBeNull();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("unions repos across the caller's agents", async () => {
|
|
252
|
+
const r = await resolveMemberRepoScope(
|
|
253
|
+
scoped(["a1", "a2"]),
|
|
254
|
+
repoDeps({ a1: ["o/x"], a2: ["o/y"] }),
|
|
255
|
+
);
|
|
256
|
+
expect(r?.sort()).toEqual(["o/x", "o/y"]);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("dedupes overlapping repos", async () => {
|
|
260
|
+
const r = await resolveMemberRepoScope(
|
|
261
|
+
scoped(["a1", "a2"]),
|
|
262
|
+
repoDeps({ a1: ["o/x", "o/y"], a2: ["o/y", "o/z"] }),
|
|
263
|
+
);
|
|
264
|
+
expect(r?.sort()).toEqual(["o/x", "o/y", "o/z"]);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("returns [] for an empty agent set without a lookup", async () => {
|
|
268
|
+
const deps = {
|
|
269
|
+
listAgentRepos: async () => {
|
|
270
|
+
throw new Error("should not be called");
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
expect(await resolveMemberRepoScope(scoped([]), deps)).toEqual([]);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("returns [] when agents have no repos", async () => {
|
|
277
|
+
expect(await resolveMemberRepoScope(scoped(["a1"]), repoDeps({}))).toEqual(
|
|
278
|
+
[],
|
|
279
|
+
);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("fails closed with [] when the lookup throws", async () => {
|
|
283
|
+
const deps = {
|
|
284
|
+
listAgentRepos: async () => {
|
|
285
|
+
throw new Error("db down");
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
expect(await resolveMemberRepoScope(scoped(["a1"]), deps)).toEqual([]);
|
|
289
|
+
});
|
|
290
|
+
});
|
package/src/chat.smoke.test.ts
CHANGED
|
@@ -1800,6 +1800,150 @@ describe("chat routes — agent member access", () => {
|
|
|
1800
1800
|
});
|
|
1801
1801
|
});
|
|
1802
1802
|
|
|
1803
|
+
// ─── ACM-7.4: per-route scope matrix (member / owner / admin) ─────────────────
|
|
1804
|
+
|
|
1805
|
+
describe("chat routes — caller scope matrix (ACM-7.4)", () => {
|
|
1806
|
+
type Role = "member" | "owner" | "admin";
|
|
1807
|
+
const ROLE_EMAIL: Record<Role, string> = {
|
|
1808
|
+
member: "scope-member@example.com",
|
|
1809
|
+
owner: "scope-owner@example.com",
|
|
1810
|
+
admin: "admin@example.com",
|
|
1811
|
+
};
|
|
1812
|
+
const cookies = {} as Record<Role, string>;
|
|
1813
|
+
|
|
1814
|
+
beforeAll(async () => {
|
|
1815
|
+
for (const role of ["member", "owner", "admin"] as Role[]) {
|
|
1816
|
+
cookies[role] = await makeSessionCookie(
|
|
1817
|
+
SESSION_SECRET,
|
|
1818
|
+
`sub-scope-${role}`,
|
|
1819
|
+
ROLE_EMAIL[role],
|
|
1820
|
+
role === "admin",
|
|
1821
|
+
);
|
|
1822
|
+
}
|
|
1823
|
+
});
|
|
1824
|
+
|
|
1825
|
+
// Member: AgentMember-only scope. Owner: account-wide scope. Both contain
|
|
1826
|
+
// AGENT_ID and exclude OTHER_AGENT_ID.
|
|
1827
|
+
function scopedDeps(role: Role): AdminUIDeps {
|
|
1828
|
+
const base = makeBaseDeps();
|
|
1829
|
+
return makeBaseDeps({
|
|
1830
|
+
chatClient: makeMockChatClient({
|
|
1831
|
+
getThread: async (id: string) => ({
|
|
1832
|
+
...MOCK_THREAD,
|
|
1833
|
+
id,
|
|
1834
|
+
agentId: id.startsWith("other") ? OTHER_AGENT_ID : AGENT_ID,
|
|
1835
|
+
}),
|
|
1836
|
+
}),
|
|
1837
|
+
callerScopeResolver: async () =>
|
|
1838
|
+
role === "admin"
|
|
1839
|
+
? { kind: "all" }
|
|
1840
|
+
: {
|
|
1841
|
+
kind: "scoped",
|
|
1842
|
+
accountId: role === "owner" ? "acct-scope" : null,
|
|
1843
|
+
agentIds: [AGENT_ID],
|
|
1844
|
+
},
|
|
1845
|
+
agentService: {
|
|
1846
|
+
...base.agentService,
|
|
1847
|
+
listByIds: async (ids: string[]) =>
|
|
1848
|
+
ids.map((id) => ({
|
|
1849
|
+
id,
|
|
1850
|
+
name: `Agent ${id}`,
|
|
1851
|
+
slackId: null,
|
|
1852
|
+
createdAt: new Date("2024-01-01"),
|
|
1853
|
+
})) as never,
|
|
1854
|
+
},
|
|
1855
|
+
});
|
|
1856
|
+
}
|
|
1857
|
+
|
|
1858
|
+
const json = (body: unknown) => ({
|
|
1859
|
+
headers: { "Content-Type": "application/json" },
|
|
1860
|
+
body: JSON.stringify(body),
|
|
1861
|
+
});
|
|
1862
|
+
const form = () => {
|
|
1863
|
+
const fd = new FormData();
|
|
1864
|
+
fd.set("file", new File(["x"], "x.txt"));
|
|
1865
|
+
return { headers: {}, body: fd };
|
|
1866
|
+
};
|
|
1867
|
+
|
|
1868
|
+
const ROUTES: {
|
|
1869
|
+
name: string;
|
|
1870
|
+
method: "GET" | "POST";
|
|
1871
|
+
path: (agent: string, thread: string) => string;
|
|
1872
|
+
init?: () => { headers: Record<string, string>; body: BodyInit };
|
|
1873
|
+
}[] = [
|
|
1874
|
+
{ name: "thread view", method: "GET", path: (a, t) => `/admin/chat/${a}/threads/${t}` },
|
|
1875
|
+
{ name: "create thread", method: "POST", path: (a) => `/admin/chat/${a}/threads`, init: () => ({ headers: {}, body: new FormData() }) },
|
|
1876
|
+
{ name: "post message (form)", method: "POST", path: (a, t) => `/admin/chat/${a}/threads/${t}/messages`, init: () => ({ headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: "body=hi" }) },
|
|
1877
|
+
{ name: "upload", method: "POST", path: (a, t) => `/admin/chat/${a}/threads/${t}/messages/upload`, init: form },
|
|
1878
|
+
{ name: "attachment", method: "GET", path: (a, t) => `/admin/chat/${a}/threads/${t}/messages/m1/attachment` },
|
|
1879
|
+
{ name: "rename", method: "POST", path: (a, t) => `/admin/chat/${a}/threads/${t}/rename`, init: () => json({ title: "x" }) },
|
|
1880
|
+
{ name: "delete", method: "POST", path: (a, t) => `/admin/chat/${a}/threads/${t}/delete`, init: () => json({}) },
|
|
1881
|
+
{ name: "messages.json GET", method: "GET", path: (a, t) => `/admin/chat/${a}/threads/${t}/messages.json` },
|
|
1882
|
+
{ name: "messages.json POST", method: "POST", path: (a, t) => `/admin/chat/${a}/threads/${t}/messages.json`, init: () => json({ body: "hi" }) },
|
|
1883
|
+
{ name: "push subscribe", method: "POST", path: (a) => `/admin/chat/${a}/push/subscribe`, init: () => json({}) },
|
|
1884
|
+
{ name: "push unsubscribe", method: "POST", path: (a) => `/admin/chat/${a}/push/unsubscribe`, init: () => json({}) },
|
|
1885
|
+
];
|
|
1886
|
+
|
|
1887
|
+
async function call(
|
|
1888
|
+
role: Role,
|
|
1889
|
+
r: (typeof ROUTES)[number],
|
|
1890
|
+
agent: string,
|
|
1891
|
+
thread: string,
|
|
1892
|
+
) {
|
|
1893
|
+
const app = createAdminUIApp(scopedDeps(role));
|
|
1894
|
+
const init = r.init?.();
|
|
1895
|
+
return app.request(r.path(agent, thread), {
|
|
1896
|
+
method: r.method,
|
|
1897
|
+
headers: { ...init?.headers, Cookie: `admin_session=${cookies[role]}` },
|
|
1898
|
+
body: init?.body,
|
|
1899
|
+
});
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
for (const role of ["member", "owner"] as Role[]) {
|
|
1903
|
+
for (const r of ROUTES) {
|
|
1904
|
+
it(`${role}: ${r.name} denies an out-of-scope agent`, async () => {
|
|
1905
|
+
const res = await call(role, r, OTHER_AGENT_ID, "other-thread");
|
|
1906
|
+
expect(res.status).toBe(403);
|
|
1907
|
+
});
|
|
1908
|
+
it(`${role}: ${r.name} allows an in-scope agent`, async () => {
|
|
1909
|
+
const res = await call(role, r, AGENT_ID, THREAD_ID);
|
|
1910
|
+
expect(res.status).not.toBe(403);
|
|
1911
|
+
});
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
for (const r of ROUTES) {
|
|
1916
|
+
it(`admin: ${r.name} is allowed on any agent`, async () => {
|
|
1917
|
+
for (const agent of [AGENT_ID, OTHER_AGENT_ID]) {
|
|
1918
|
+
const res = await call(
|
|
1919
|
+
"admin",
|
|
1920
|
+
r,
|
|
1921
|
+
agent,
|
|
1922
|
+
agent === AGENT_ID ? THREAD_ID : "other-thread",
|
|
1923
|
+
);
|
|
1924
|
+
expect(res.status).not.toBe(403);
|
|
1925
|
+
}
|
|
1926
|
+
});
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
it("landing page lists only in-scope agents for member and owner, all for admin", async () => {
|
|
1930
|
+
for (const role of ["member", "owner", "admin"] as Role[]) {
|
|
1931
|
+
const app = createAdminUIApp(scopedDeps(role));
|
|
1932
|
+
const res = await app.request("/admin/chat", {
|
|
1933
|
+
headers: { Cookie: `admin_session=${cookies[role]}` },
|
|
1934
|
+
});
|
|
1935
|
+
expect(res.status).toBe(200);
|
|
1936
|
+
const html = await res.text();
|
|
1937
|
+
if (role === "admin") {
|
|
1938
|
+
expect(html).toContain("Test Agent");
|
|
1939
|
+
} else {
|
|
1940
|
+
expect(html).toContain(`Agent ${AGENT_ID}`);
|
|
1941
|
+
expect(html).not.toContain(OTHER_AGENT_ID);
|
|
1942
|
+
}
|
|
1943
|
+
}
|
|
1944
|
+
});
|
|
1945
|
+
});
|
|
1946
|
+
|
|
1803
1947
|
// ─── SSP-8.4: account-status send gate ────────────────────────────────────────
|
|
1804
1948
|
|
|
1805
1949
|
describe("account status gates chat message-send (SSP-8.4)", () => {
|