@app-vitals/agent-admin 1.104.0 → 1.106.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.
|
|
3
|
+
"version": "1.106.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.106.0",
|
|
48
48
|
"@hono/zod-openapi": "^1.0.0",
|
|
49
49
|
"@prisma/adapter-pg": "^7.10.0",
|
|
50
50
|
"@prisma/client": "^7.10.0",
|
|
@@ -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";
|
|
@@ -92,4 +94,36 @@ describeOrSkip("caller scope role resolver (integration)", () => {
|
|
|
92
94
|
agentIds: [],
|
|
93
95
|
});
|
|
94
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
|
+
});
|
|
95
129
|
});
|
package/src/caller-scope.ts
CHANGED
|
@@ -132,6 +132,26 @@ export function taskStoreViewScope(scope: CallerScope): TaskStoreViewScope {
|
|
|
132
132
|
};
|
|
133
133
|
}
|
|
134
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
|
+
|
|
135
155
|
/** Per-request resolver with the flag already bound. */
|
|
136
156
|
export type CallerScopeResolver = (
|
|
137
157
|
email: string,
|
|
@@ -183,3 +203,23 @@ export function memberOnlyCallerScopeResolver(
|
|
|
183
203
|
false,
|
|
184
204
|
);
|
|
185
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,
|
|
@@ -227,3 +228,63 @@ describe("resolveCallerScope matrix: role x flag", () => {
|
|
|
227
228
|
expect(await run("a@x.com", true, true)).toEqual({ kind: "all" });
|
|
228
229
|
});
|
|
229
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)", () => {
|