@app-vitals/agent-admin 1.107.1 → 1.107.2

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.107.1",
3
+ "version": "1.107.2",
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.107.1",
47
+ "@app-vitals/agent-lib": "1.107.2",
48
48
  "@hono/zod-openapi": "^1.0.0",
49
49
  "@prisma/adapter-pg": "^7.10.0",
50
50
  "@prisma/client": "^7.10.0",
@@ -228,16 +228,6 @@ describe("behaviour", () => {
228
228
  });
229
229
  });
230
230
 
231
- it("POST /accounts with an already-used owner email -> 409", async () => {
232
- const { app } = build();
233
- const res = await req(app, "POST", "/accounts", adminHeaders, {
234
- name: "N",
235
- ownerEmail: "taken@x.com",
236
- maxAgents: 1,
237
- });
238
- expect(res.status).toBe(409);
239
- });
240
-
241
231
  it("POST /accounts validation failure -> 400", async () => {
242
232
  const { app } = build();
243
233
  const res = await req(app, "POST", "/accounts", adminHeaders, {
@@ -255,7 +245,6 @@ describe("behaviour", () => {
255
245
  trialExpiresAt: null,
256
246
  });
257
247
  expect(res.status).toBe(200);
258
- expect(((await res.json()) as { maxAgents: number }).maxAgents).toBe(3);
259
248
  expect(calls[0]?.args[1]).toEqual({ maxAgents: 3, trialExpiresAt: null });
260
249
  });
261
250
 
@@ -355,8 +355,12 @@ describe("/admin/accounts", () => {
355
355
 
356
356
  it("suspend and reactivate go through the account lifecycle (SSP-8.2)", async () => {
357
357
  const { app, lifecycleCalls } = setup();
358
- await post(app, "/admin/accounts/a1/suspend", {});
359
- await post(app, "/admin/accounts/a1/reactivate", {});
358
+ expect((await post(app, "/admin/accounts/a1/suspend", {})).status).toBe(
359
+ 302,
360
+ );
361
+ expect((await post(app, "/admin/accounts/a1/reactivate", {})).status).toBe(
362
+ 302,
363
+ );
360
364
  expect(lifecycleCalls).toEqual([
361
365
  { id: "a1", data: { status: "suspended" } },
362
366
  { id: "a1", data: { status: "active" } },
@@ -372,18 +376,6 @@ describe("/admin/accounts", () => {
372
376
  expect(account.status).toBe("trial_expired");
373
377
  });
374
378
 
375
- it("suspend and reactivate set status", async () => {
376
- const { app, account } = setup();
377
- expect((await post(app, "/admin/accounts/a1/suspend", {})).status).toBe(
378
- 302,
379
- );
380
- expect(account.status).toBe("suspended");
381
- expect((await post(app, "/admin/accounts/a1/reactivate", {})).status).toBe(
382
- 302,
383
- );
384
- expect(account.status).toBe("active");
385
- });
386
-
387
379
  it("adds member and owner; rejects invalid and duplicate emails", async () => {
388
380
  const { app, members } = setup();
389
381
  expect(
@@ -9,7 +9,7 @@
9
9
  import { afterEach, beforeEach, describe, expect, it } from "bun:test";
10
10
  import type { PrismaClient } from "../prisma/client/client.ts";
11
11
  import { AgentMemberService } from "./agent-members.ts";
12
- import { resetMemberIdFixture } from "./member-id-test-support.ts";
12
+ import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
13
13
  import { createAdminPrismaClient } from "./prisma-client.ts";
14
14
 
15
15
  const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
@@ -0,0 +1,30 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { parseChatCredentialConfig } from "./chat-credential-config.ts";
3
+
4
+ describe("parseChatCredentialConfig", () => {
5
+ it("is enabled only for the exact value 'enabled'", () => {
6
+ expect(
7
+ parseChatCredentialConfig({ SHIPWRIGHT_CHAT_TOKEN_MINT_ENABLED: "enabled" }),
8
+ ).toEqual({ enabled: true });
9
+ });
10
+
11
+ it("is disabled when the flag is unset", () => {
12
+ expect(parseChatCredentialConfig({})).toEqual({ enabled: false });
13
+ });
14
+
15
+ // The parser never throws: any invalid/unrecognized value fails closed.
16
+ it.each(["true", "1", "ENABLED", "Enabled", " enabled", "disabled", ""])(
17
+ "fails closed (disabled) for invalid value %p",
18
+ (value) => {
19
+ expect(
20
+ parseChatCredentialConfig({ SHIPWRIGHT_CHAT_TOKEN_MINT_ENABLED: value }),
21
+ ).toEqual({ enabled: false });
22
+ },
23
+ );
24
+
25
+ it("ignores unrelated env vars", () => {
26
+ expect(parseChatCredentialConfig({ OTHER: "enabled" })).toEqual({
27
+ enabled: false,
28
+ });
29
+ });
30
+ });
@@ -0,0 +1,81 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { deleteMemberRows, stampMemberId } from "./member-id-stamp.ts";
3
+ import type { PrismaTransactionClient } from "./prisma-tx.ts";
4
+
5
+ const TABLES = [
6
+ "agentMember",
7
+ "chatThreadWatch",
8
+ "pushSubscription",
9
+ "userNotificationPrefs",
10
+ ] as const;
11
+
12
+ type Call = { table: string; op: string; args: any };
13
+
14
+ function fakeClient(counts: Partial<Record<(typeof TABLES)[number], number>> = {}) {
15
+ const calls: Call[] = [];
16
+ const client: any = {};
17
+ for (const t of TABLES) {
18
+ client[t] = {
19
+ updateMany: async (args: any) => {
20
+ calls.push({ table: t, op: "updateMany", args });
21
+ return { count: counts[t] ?? 0 };
22
+ },
23
+ deleteMany: async (args: any) => {
24
+ calls.push({ table: t, op: "deleteMany", args });
25
+ return { count: counts[t] ?? 0 };
26
+ },
27
+ };
28
+ }
29
+ return { client: client as PrismaTransactionClient, calls };
30
+ }
31
+
32
+ describe("stampMemberId", () => {
33
+ it("stamps null-memberId rows in all four tables with a normalized, case-insensitive email", async () => {
34
+ const { client, calls } = fakeClient();
35
+ await stampMemberId(client, " Bob@X.com ", "m1");
36
+ expect(calls.map((c) => c.table).sort()).toEqual([...TABLES].sort());
37
+ const email = { equals: "bob@x.com", mode: "insensitive" };
38
+ for (const c of calls) {
39
+ expect(c.op).toBe("updateMany");
40
+ expect(c.args.data).toEqual({ memberId: "m1" });
41
+ expect(c.args.where.memberId).toBeNull();
42
+ const field = c.table === "agentMember" ? "email" : "userEmail";
43
+ expect(c.args.where[field]).toEqual(email);
44
+ }
45
+ });
46
+
47
+ it("returns the summed count of changed rows", async () => {
48
+ const { client } = fakeClient({ agentMember: 2, pushSubscription: 3 });
49
+ expect(await stampMemberId(client, "a@b.c", "m1")).toBe(5);
50
+ });
51
+
52
+ it("returns 0 when nothing matches", async () => {
53
+ const { client } = fakeClient();
54
+ expect(await stampMemberId(client, "a@b.c", "m1")).toBe(0);
55
+ });
56
+ });
57
+
58
+ describe("deleteMemberRows", () => {
59
+ it("matches rows by memberId or by email with null memberId, leaving other members' rows", async () => {
60
+ const { client, calls } = fakeClient();
61
+ await deleteMemberRows(client, "Bob@X.com", "m1");
62
+ expect(calls.map((c) => c.table).sort()).toEqual([...TABLES].sort());
63
+ const email = { equals: "bob@x.com", mode: "insensitive" };
64
+ for (const c of calls) {
65
+ expect(c.op).toBe("deleteMany");
66
+ const field = c.table === "agentMember" ? "email" : "userEmail";
67
+ expect(c.args.where).toEqual({
68
+ OR: [{ memberId: "m1" }, { [field]: email, memberId: null }],
69
+ });
70
+ }
71
+ });
72
+
73
+ it("returns the summed count of deleted rows", async () => {
74
+ const { client } = fakeClient({
75
+ agentMember: 1,
76
+ chatThreadWatch: 1,
77
+ userNotificationPrefs: 4,
78
+ });
79
+ expect(await deleteMemberRows(client, "a@b.c", "m1")).toBe(6);
80
+ });
81
+ });
@@ -8,7 +8,7 @@
8
8
 
9
9
  import { afterEach, beforeEach, describe, expect, it } from "bun:test";
10
10
  import type { PrismaClient } from "../prisma/client/client.ts";
11
- import { resetMemberIdFixture } from "./member-id-test-support.ts";
11
+ import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
12
12
  import { createAdminPrismaClient } from "./prisma-client.ts";
13
13
  import { PushService } from "./push-service.ts";
14
14
 
@@ -0,0 +1,237 @@
1
+ /**
2
+ * admin/src/reconcile-open-work.integration.test.ts
3
+ * T-019: the OpenWorkPort contract driven through the real GithubSyncService
4
+ * repo-removal reconcile, with the real cache and a real GitHubReconcileAudit
5
+ * row in Postgres. Covers the repo-removal path (preview lists, apply blocks
6
+ * per repo with the lost-access reason) and partial-failure behaviour (a port
7
+ * failure becomes a warning, never an error; a failed apply blocks nothing).
8
+ *
9
+ * Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
10
+ */
11
+
12
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
13
+ import type { PrismaClient } from "../prisma/client/client.ts";
14
+ import { DEPLOYMENT_WIDE_ACCOUNT_KEY } from "./github-agent-installation-crawler.ts";
15
+ import { GitHubRepoCacheService } from "./github-repo-cache.ts";
16
+ import { GithubSyncService } from "./github-sync.ts";
17
+ import { PrismaInstallationAudit } from "./installation-ports.ts";
18
+ import { createAdminPrismaClient } from "./prisma-client.ts";
19
+ import type { OpenWorkItem, OpenWorkPort } from "./reconcile-open-work.ts";
20
+ import { repoLostAccessBlockReason } from "./repo-removal-open-work.ts";
21
+
22
+ const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
23
+ const describeOrSkip = TEST_DB ? describe : describe.skip;
24
+
25
+ const AGENT_ID = "oow-it-agent";
26
+ const ORG = "oow-eng";
27
+ const APP_ID = 888;
28
+ const KEEP = `${ORG}/keep`;
29
+ const GONE_A = `${ORG}/gone-a`;
30
+ const GONE_B = `${ORG}/gone-b`;
31
+ const ALL = [KEEP, GONE_A, GONE_B];
32
+
33
+ /** In-memory open-work port that records every call; `failOn` repos reject. */
34
+ function recordingPort(failOn: string[] = []) {
35
+ const work: OpenWorkItem[] = ALL.map((repo) => ({
36
+ id: `task-${repo.split("/")[1]}`,
37
+ repo,
38
+ }));
39
+ const blocked: { repos: readonly string[]; reason: string }[] = [];
40
+ const port: OpenWorkPort = {
41
+ listOpenWork: async (_agent, repos) =>
42
+ work.filter((w) => repos.includes(w.repo)),
43
+ blockOpenWork: async (_agent, repos, reason) => {
44
+ if (repos.some((r) => failOn.includes(r))) {
45
+ throw new Error(`store down for ${repos.join(",")}`);
46
+ }
47
+ blocked.push({ repos, reason });
48
+ },
49
+ };
50
+ return { port, blocked };
51
+ }
52
+
53
+ describeOrSkip("reconcile-open-work (integration)", () => {
54
+ let prisma: PrismaClient;
55
+ let cache: GitHubRepoCacheService;
56
+ const checkedAt = new Date(Date.now() - 60_000);
57
+
58
+ const clean = async () => {
59
+ await prisma.gitHubReconcileAudit.deleteMany({
60
+ where: { agentId: AGENT_ID },
61
+ });
62
+ await prisma.gitHubRepoAccess.deleteMany({
63
+ where: { accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY, org: ORG },
64
+ });
65
+ await prisma.gitHubCachedRepo.deleteMany();
66
+ await prisma.gitHubRepoSnapshot.deleteMany();
67
+ };
68
+
69
+ async function seed() {
70
+ const snap = await cache.upsertSnapshot(
71
+ {
72
+ accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
73
+ appKind: "agent" as const,
74
+ appRef: String(APP_ID),
75
+ org: ORG,
76
+ },
77
+ {
78
+ completedAt: checkedAt,
79
+ totalCount: ALL.length,
80
+ repositorySelection: "selected",
81
+ lastError: null,
82
+ },
83
+ );
84
+ await cache.upsertRepos(
85
+ snap.id,
86
+ ALL.map((fullName, i) => ({ githubId: i + 1, fullName })),
87
+ checkedAt,
88
+ );
89
+ for (const full of ALL) {
90
+ const canPush = full === KEEP;
91
+ await prisma.gitHubRepoAccess.create({
92
+ data: {
93
+ accountId: DEPLOYMENT_WIDE_ACCOUNT_KEY,
94
+ org: ORG,
95
+ repo: full.split("/")[1] as string,
96
+ username: "dev",
97
+ canPush,
98
+ level: canPush ? "write" : "read",
99
+ checkedAt,
100
+ status: "ok",
101
+ },
102
+ });
103
+ }
104
+ }
105
+
106
+ function service(openWork: OpenWorkPort | undefined, removeFails = false) {
107
+ const calls: string[] = [];
108
+ let repos = [...ALL];
109
+ const svc = new GithubSyncService({
110
+ agents: {
111
+ get: async () => ({
112
+ id: AGENT_ID,
113
+ githubUsername: "dev",
114
+ appId: APP_ID,
115
+ repos,
116
+ }),
117
+ setRepos: async (_id, next) => {
118
+ calls.push("setRepos");
119
+ repos = next;
120
+ },
121
+ },
122
+ access: async () => {
123
+ throw new Error("live resolver must not be used");
124
+ },
125
+ cache,
126
+ lookupRepos: async () => [
127
+ { org: ORG, completedAt: checkedAt, repos: ALL },
128
+ ],
129
+ verifyPush: async (_u, repo) => repo === KEEP,
130
+ installation: {
131
+ listInstalled: async () => ({
132
+ status: "found",
133
+ orgs: [{ org: ORG, repos: ALL }],
134
+ }),
135
+ addRepos: async () => {},
136
+ removeRepos: async () => {
137
+ calls.push("removeRepos");
138
+ if (removeFails) throw new Error("installation PATCH failed");
139
+ },
140
+ setRepositorySelection: async () => {},
141
+ },
142
+ audit: new PrismaInstallationAudit(prisma),
143
+ ...(openWork ? { openWork } : {}),
144
+ });
145
+ return { svc, calls };
146
+ }
147
+
148
+ const removeBoth = {
149
+ agentId: AGENT_ID,
150
+ caller: "root",
151
+ appAdds: [],
152
+ appRemoves: [GONE_A, GONE_B],
153
+ agentRemoves: [GONE_A, GONE_B],
154
+ };
155
+
156
+ beforeEach(async () => {
157
+ prisma = createAdminPrismaClient(TEST_DB as string);
158
+ cache = new GitHubRepoCacheService(prisma);
159
+ await clean();
160
+ await seed();
161
+ });
162
+ afterEach(async () => {
163
+ await clean();
164
+ await prisma.$disconnect();
165
+ });
166
+
167
+ it("preview lists the open work on exactly the repos the reconcile would remove", async () => {
168
+ const { port, blocked } = recordingPort();
169
+ const { svc } = service(port);
170
+ const r = await svc.preview(AGENT_ID);
171
+ if (r.status !== "previewed") throw new Error(r.status);
172
+ expect(r.plan.removedRepos.sort()).toEqual([GONE_A, GONE_B]);
173
+ expect(r.openWork.map((w) => w.repo).sort()).toEqual([GONE_A, GONE_B]);
174
+ expect(r.warnings).toEqual([]);
175
+ expect(blocked).toEqual([]); // preview is read-only
176
+ });
177
+
178
+ it("apply blocks each removed repo's work separately with the lost-access reason", async () => {
179
+ const { port, blocked } = recordingPort();
180
+ const { svc, calls } = service(port);
181
+ const r = await svc.apply(removeBoth);
182
+ if (r.status !== "applied") throw new Error(r.status);
183
+ expect(r.warnings).toEqual([]);
184
+ expect(calls).toEqual(["removeRepos", "setRepos"]);
185
+ expect(blocked).toEqual([
186
+ { repos: [GONE_A], reason: repoLostAccessBlockReason(GONE_A) },
187
+ { repos: [GONE_B], reason: repoLostAccessBlockReason(GONE_B) },
188
+ ]);
189
+ const rows = await prisma.gitHubReconcileAudit.findMany({
190
+ where: { agentId: AGENT_ID },
191
+ });
192
+ expect(rows).toHaveLength(1);
193
+ expect(rows[0]?.result).toEqual({ status: "applied" });
194
+ });
195
+
196
+ it("a port failure on one repo is a warning; the other repo is still blocked and the apply still succeeds", async () => {
197
+ const { port, blocked } = recordingPort([GONE_A]);
198
+ const { svc } = service(port);
199
+ const r = await svc.apply(removeBoth);
200
+ if (r.status !== "applied") throw new Error(r.status);
201
+ expect(r.warnings).toEqual([
202
+ `open work for ${GONE_A} was not blocked: store down for ${GONE_A}`,
203
+ ]);
204
+ expect(blocked.map((b) => b.repos)).toEqual([[GONE_B]]);
205
+ const rows = await prisma.gitHubReconcileAudit.findMany({
206
+ where: { agentId: AGENT_ID },
207
+ });
208
+ expect(rows[0]?.result).toEqual({ status: "applied" });
209
+ });
210
+
211
+ it("a failed apply blocks no work and records the failure in the audit row", async () => {
212
+ const { port, blocked } = recordingPort();
213
+ const { svc, calls } = service(port, true);
214
+ const r = await svc.apply(removeBoth);
215
+ if (r.status !== "failed") throw new Error(r.status);
216
+ expect(r.error).toBe("installation PATCH failed");
217
+ expect(calls).toEqual(["removeRepos"]);
218
+ expect(blocked).toEqual([]);
219
+ const rows = await prisma.gitHubReconcileAudit.findMany({
220
+ where: { agentId: AGENT_ID },
221
+ });
222
+ expect(rows[0]?.result).toEqual({
223
+ status: "failed",
224
+ error: "installation PATCH failed",
225
+ });
226
+ });
227
+
228
+ it("without a port the reconcile still applies and reports no open work or warnings", async () => {
229
+ const { svc } = service(undefined);
230
+ const p = await svc.preview(AGENT_ID);
231
+ if (p.status !== "previewed") throw new Error(p.status);
232
+ expect(p.openWork).toEqual([]);
233
+ const r = await svc.apply(removeBoth);
234
+ if (r.status !== "applied") throw new Error(r.status);
235
+ expect(r.warnings).toEqual([]);
236
+ });
237
+ });
@@ -1,11 +1,11 @@
1
1
  /**
2
- * admin/src/member-id-test-support.ts
2
+ * admin/src/test-support/member-id-test-support.ts
3
3
  * Shared seed helpers for the ACM-4.1 per-table memberId integration tests.
4
4
  * Wipes the four per-user tables plus accounts, then seeds one account with
5
5
  * two members and one agent.
6
6
  */
7
7
 
8
- import type { PrismaClient } from "../prisma/client/client.ts";
8
+ import type { PrismaClient } from "../../prisma/client/client.ts";
9
9
 
10
10
  export async function resetMemberIdFixture(prisma: PrismaClient) {
11
11
  await prisma.userNotificationPrefs.deleteMany();
@@ -8,7 +8,7 @@
8
8
 
9
9
  import { afterEach, beforeEach, describe, expect, it } from "bun:test";
10
10
  import type { PrismaClient } from "../prisma/client/client.ts";
11
- import { resetMemberIdFixture } from "./member-id-test-support.ts";
11
+ import { resetMemberIdFixture } from "./test-support/member-id-test-support.ts";
12
12
  import { createAdminPrismaClient } from "./prisma-client.ts";
13
13
  import { UserNotificationPrefsService } from "./user-notification-prefs.ts";
14
14