@app-vitals/agent-admin 1.41.0 → 1.43.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/prisma/client/internal/class.ts +3 -3
- package/prisma/client/internal/prismaNamespace.ts +1 -0
- package/prisma/client/internal/prismaNamespaceBrowser.ts +1 -0
- package/prisma/client/models/AgentEnv.ts +36 -1
- package/prisma/migrations/20261114000000_add_agent_env_enabled/migration.sql +3 -0
- package/prisma/schema/schema.prisma +1 -0
- package/src/admin-boot.ts +7 -5
- package/src/admin-ui-github-enterprise-pages.ts +1 -1
- package/src/admin-ui-templates-pages.ts +155 -0
- package/src/admin-ui-templates-pages.unit.test.ts +54 -0
- package/src/admin-ui-templates.smoke.test.ts +292 -0
- package/src/admin-ui-templates.ts +316 -0
- package/src/admin-ui.ts +24 -0
- package/src/agent-cron-jobs.integration.test.ts +77 -0
- package/src/agent-cron-jobs.ts +66 -10
- package/src/agent-deletion.ts +2 -2
- package/src/agent-envs.integration.test.ts +108 -0
- package/src/agent-envs.ts +95 -19
- package/src/agent-plugins.integration.test.ts +52 -0
- package/src/agent-plugins.ts +51 -4
- package/src/agent-tools.integration.test.ts +55 -0
- package/src/agent-tools.ts +35 -4
- package/src/agents-api.smoke.test.ts +1 -0
- package/src/agents.ts +1 -1
|
@@ -676,4 +676,81 @@ describeOrSkip("AgentCronJobService specialtyDefaults (integration)", () => {
|
|
|
676
676
|
expect(loopAfter?.enabled).toBe(false);
|
|
677
677
|
expect(after.some((j) => j.name === "stale-system")).toBe(false);
|
|
678
678
|
});
|
|
679
|
+
|
|
680
|
+
// ─── ATP-1.2: source / tombstones ──────────────────────────────────────────
|
|
681
|
+
|
|
682
|
+
const BASE = { schedule: "0 9 * * *", prompt: "hi", channel: "C1" };
|
|
683
|
+
|
|
684
|
+
it("create() stamps source=custom; createFromTemplate() stamps template", async () => {
|
|
685
|
+
const agentId = await createAgent(prisma);
|
|
686
|
+
expect((await service.create(agentId, BASE)).source).toBe("custom");
|
|
687
|
+
const t = await service.createFromTemplate(agentId, {
|
|
688
|
+
...BASE,
|
|
689
|
+
name: "tpl",
|
|
690
|
+
});
|
|
691
|
+
expect(t.source).toBe("template");
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it("update(), updatePreCheck(), updateDelivery() and setEnabled() turn a template row custom", async () => {
|
|
695
|
+
const agentId = await createAgent(prisma);
|
|
696
|
+
const mk = () =>
|
|
697
|
+
service.createFromTemplate(agentId, {
|
|
698
|
+
...BASE,
|
|
699
|
+
name: `t${Math.random()}`,
|
|
700
|
+
});
|
|
701
|
+
const a = await mk();
|
|
702
|
+
expect((await service.update(agentId, a.id, BASE)).source).toBe("custom");
|
|
703
|
+
const b = await mk();
|
|
704
|
+
expect((await service.updatePreCheck(agentId, b.id, "x.ts")).source).toBe(
|
|
705
|
+
"custom",
|
|
706
|
+
);
|
|
707
|
+
const c = await mk();
|
|
708
|
+
expect(
|
|
709
|
+
(await service.updateDelivery(agentId, c.id, { channel: "C2" })).source,
|
|
710
|
+
).toBe("custom");
|
|
711
|
+
const d = await mk();
|
|
712
|
+
expect((await service.setEnabled(agentId, d.id, false)).source).toBe(
|
|
713
|
+
"custom",
|
|
714
|
+
);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
it("a trial lockdown disable does not flip source", async () => {
|
|
718
|
+
const agentId = await createAgent(prisma);
|
|
719
|
+
const a = await service.createFromTemplate(agentId, BASE);
|
|
720
|
+
const off = await service.setEnabled(agentId, a.id, false, {
|
|
721
|
+
lockdown: true,
|
|
722
|
+
});
|
|
723
|
+
expect(off.source).toBe("template");
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
it("updateFromTemplate() edits a row without flipping source", async () => {
|
|
727
|
+
const agentId = await createAgent(prisma);
|
|
728
|
+
const a = await service.createFromTemplate(agentId, BASE);
|
|
729
|
+
const up = await service.updateFromTemplate(agentId, a.id, {
|
|
730
|
+
...BASE,
|
|
731
|
+
prompt: "new",
|
|
732
|
+
});
|
|
733
|
+
expect(up.prompt).toBe("new");
|
|
734
|
+
expect(up.source).toBe("template");
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
it("delete() of a template row leaves a disabled custom tombstone", async () => {
|
|
738
|
+
const agentId = await createAgent(prisma);
|
|
739
|
+
const a = await service.createFromTemplate(agentId, BASE);
|
|
740
|
+
await service.delete(agentId, a.id);
|
|
741
|
+
const rows = await prisma.agentCronJob.findMany({ where: { agentId } });
|
|
742
|
+
expect(rows).toHaveLength(1);
|
|
743
|
+
expect(rows[0]?.enabled).toBe(false);
|
|
744
|
+
expect(rows[0]?.source).toBe("custom");
|
|
745
|
+
expect(await service.listEnabled()).toEqual([]);
|
|
746
|
+
});
|
|
747
|
+
|
|
748
|
+
it("delete() of a custom row removes it", async () => {
|
|
749
|
+
const agentId = await createAgent(prisma);
|
|
750
|
+
const a = await service.create(agentId, BASE);
|
|
751
|
+
await service.delete(agentId, a.id);
|
|
752
|
+
expect(await prisma.agentCronJob.findMany({ where: { agentId } })).toEqual(
|
|
753
|
+
[],
|
|
754
|
+
);
|
|
755
|
+
});
|
|
679
756
|
});
|
package/src/agent-cron-jobs.ts
CHANGED
|
@@ -51,6 +51,16 @@ export interface CreateAgentCronJobInput {
|
|
|
51
51
|
* Each field may contain digits, *, /, -, and comma (e.g. a minute field of
|
|
52
52
|
* "0,30" for a twice-hourly, staggered schedule is valid).
|
|
53
53
|
*/
|
|
54
|
+
export interface UpdateAgentCronJobInput {
|
|
55
|
+
schedule: string;
|
|
56
|
+
prompt: string;
|
|
57
|
+
channel?: string | null;
|
|
58
|
+
user?: string | null;
|
|
59
|
+
silent?: boolean;
|
|
60
|
+
preCheck?: string | null;
|
|
61
|
+
enabled?: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
54
64
|
export function isValidCron(schedule: string): boolean {
|
|
55
65
|
const fields = schedule.trim().split(/\s+/);
|
|
56
66
|
if (fields.length !== 5) return false;
|
|
@@ -248,6 +258,25 @@ export class AgentCronJobService {
|
|
|
248
258
|
async create(
|
|
249
259
|
agentId: string,
|
|
250
260
|
input: CreateAgentCronJobInput,
|
|
261
|
+
): Promise<AgentCronJob> {
|
|
262
|
+
return this.createWithSource(agentId, input, "custom");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Internal reconcile write path (ATP-1.2); never exposed over HTTP.
|
|
267
|
+
* Same validation as create() but stamps source="template".
|
|
268
|
+
*/
|
|
269
|
+
async createFromTemplate(
|
|
270
|
+
agentId: string,
|
|
271
|
+
input: CreateAgentCronJobInput,
|
|
272
|
+
): Promise<AgentCronJob> {
|
|
273
|
+
return this.createWithSource(agentId, input, "template");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
private async createWithSource(
|
|
277
|
+
agentId: string,
|
|
278
|
+
input: CreateAgentCronJobInput,
|
|
279
|
+
source: "template" | "custom",
|
|
251
280
|
): Promise<AgentCronJob> {
|
|
252
281
|
if (!isValidCron(input.schedule)) {
|
|
253
282
|
throw new UnprocessableEntityError(
|
|
@@ -273,6 +302,7 @@ export class AgentCronJobService {
|
|
|
273
302
|
preCheck: input.preCheck ?? null,
|
|
274
303
|
name: input.name ?? null,
|
|
275
304
|
system: input.system ?? false,
|
|
305
|
+
source,
|
|
276
306
|
},
|
|
277
307
|
});
|
|
278
308
|
}
|
|
@@ -290,6 +320,15 @@ export class AgentCronJobService {
|
|
|
290
320
|
throw new NotFoundError(`cron job ${cronId} not found`);
|
|
291
321
|
}
|
|
292
322
|
|
|
323
|
+
// ATP-1.2: deleting a template row writes a tombstone so a later
|
|
324
|
+
// reconcile does not re-add it.
|
|
325
|
+
if (existing.source === "template") {
|
|
326
|
+
await this.prisma.agentCronJob.update({
|
|
327
|
+
where: { id: cronId },
|
|
328
|
+
data: { enabled: false, source: "custom", lockdownDisabledAt: null },
|
|
329
|
+
});
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
293
332
|
await this.prisma.agentCronJob.delete({ where: { id: cronId } });
|
|
294
333
|
}
|
|
295
334
|
|
|
@@ -305,15 +344,28 @@ export class AgentCronJobService {
|
|
|
305
344
|
async update(
|
|
306
345
|
agentId: string,
|
|
307
346
|
cronId: string,
|
|
308
|
-
input:
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
347
|
+
input: UpdateAgentCronJobInput,
|
|
348
|
+
): Promise<AgentCronJob> {
|
|
349
|
+
return this.updateWithSource(agentId, cronId, input, "custom");
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Internal reconcile write path (ATP-1.2); never exposed over HTTP.
|
|
354
|
+
* Same validation as update() but leaves `source` untouched.
|
|
355
|
+
*/
|
|
356
|
+
async updateFromTemplate(
|
|
357
|
+
agentId: string,
|
|
358
|
+
cronId: string,
|
|
359
|
+
input: UpdateAgentCronJobInput,
|
|
360
|
+
): Promise<AgentCronJob> {
|
|
361
|
+
return this.updateWithSource(agentId, cronId, input, undefined);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
private async updateWithSource(
|
|
365
|
+
agentId: string,
|
|
366
|
+
cronId: string,
|
|
367
|
+
input: UpdateAgentCronJobInput,
|
|
368
|
+
source: "custom" | undefined,
|
|
317
369
|
): Promise<AgentCronJob> {
|
|
318
370
|
await this.get(agentId, cronId);
|
|
319
371
|
|
|
@@ -337,6 +389,7 @@ export class AgentCronJobService {
|
|
|
337
389
|
channel,
|
|
338
390
|
user,
|
|
339
391
|
silent,
|
|
392
|
+
...(source && { source }),
|
|
340
393
|
...(input.preCheck !== undefined && { preCheck: input.preCheck }),
|
|
341
394
|
// Any explicit enabled change is a user decision — clear the lockdown
|
|
342
395
|
// marker so restoreLockdownDisabled() never overrides it.
|
|
@@ -361,7 +414,7 @@ export class AgentCronJobService {
|
|
|
361
414
|
await this.get(agentId, cronId);
|
|
362
415
|
return this.prisma.agentCronJob.update({
|
|
363
416
|
where: { id: cronId },
|
|
364
|
-
data: { preCheck },
|
|
417
|
+
data: { preCheck, source: "custom" },
|
|
365
418
|
});
|
|
366
419
|
}
|
|
367
420
|
|
|
@@ -384,6 +437,7 @@ export class AgentCronJobService {
|
|
|
384
437
|
return this.prisma.agentCronJob.update({
|
|
385
438
|
where: { id: cronId },
|
|
386
439
|
data: {
|
|
440
|
+
source: "custom",
|
|
387
441
|
...(input.channel !== undefined && { channel: input.channel }),
|
|
388
442
|
...(input.user !== undefined && { user: input.user }),
|
|
389
443
|
...(input.silent !== undefined && { silent: input.silent }),
|
|
@@ -413,6 +467,8 @@ export class AgentCronJobService {
|
|
|
413
467
|
data: {
|
|
414
468
|
enabled,
|
|
415
469
|
lockdownDisabledAt: !enabled && opts?.lockdown ? new Date() : null,
|
|
470
|
+
// A lockdown disable is system-driven, not an admin edit.
|
|
471
|
+
...(!opts?.lockdown && { source: "custom" }),
|
|
416
472
|
},
|
|
417
473
|
});
|
|
418
474
|
}
|
package/src/agent-deletion.ts
CHANGED
|
@@ -119,7 +119,7 @@ interface DeleteAgentPrisma {
|
|
|
119
119
|
};
|
|
120
120
|
agentEnv: {
|
|
121
121
|
findMany(args: {
|
|
122
|
-
where: { agentId: string };
|
|
122
|
+
where: { agentId: string; enabled: true };
|
|
123
123
|
select: { key: true; value: true; secret: true };
|
|
124
124
|
}): Promise<{ key: string; value: string; secret: boolean }[]>;
|
|
125
125
|
};
|
|
@@ -199,7 +199,7 @@ export async function deleteAgentFully(
|
|
|
199
199
|
throw new NotFoundError(`agent ${agentId} not found`);
|
|
200
200
|
}
|
|
201
201
|
const envRows = await deps.prisma.agentEnv.findMany({
|
|
202
|
-
where: { agentId },
|
|
202
|
+
where: { agentId, enabled: true },
|
|
203
203
|
select: { key: true, value: true, secret: true },
|
|
204
204
|
});
|
|
205
205
|
|
|
@@ -321,3 +321,111 @@ describeOrSkip(
|
|
|
321
321
|
});
|
|
322
322
|
},
|
|
323
323
|
);
|
|
324
|
+
|
|
325
|
+
describeOrSkip("AgentEnvService source and tombstones (integration)", () => {
|
|
326
|
+
let prisma: PrismaClient;
|
|
327
|
+
let service: AgentEnvService;
|
|
328
|
+
|
|
329
|
+
beforeEach(async () => {
|
|
330
|
+
prisma = makePrisma();
|
|
331
|
+
await prisma.agentToken.deleteMany();
|
|
332
|
+
await prisma.agentCronJob.deleteMany();
|
|
333
|
+
await prisma.agentTool.deleteMany();
|
|
334
|
+
await prisma.agentEnv.deleteMany();
|
|
335
|
+
await prisma.agent.deleteMany();
|
|
336
|
+
service = new AgentEnvService(prisma, identityCrypto);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
afterEach(async () => {
|
|
340
|
+
await prisma.$disconnect();
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
const rowsOf = (agentId: string) =>
|
|
344
|
+
prisma.agentEnv.findMany({ where: { agentId }, orderBy: { key: "asc" } });
|
|
345
|
+
|
|
346
|
+
it("patch() and upsert() stamp source=custom", async () => {
|
|
347
|
+
const agentId = await createAgent(prisma);
|
|
348
|
+
await service.patch(agentId, { A: "1" });
|
|
349
|
+
expect((await rowsOf(agentId)).map((r) => r.source)).toEqual(["custom"]);
|
|
350
|
+
await service.upsert(agentId, { B: "2" });
|
|
351
|
+
expect((await rowsOf(agentId)).map((r) => r.source)).toEqual(["custom"]);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("patchFromTemplate() writes source=template", async () => {
|
|
355
|
+
const agentId = await createAgent(prisma);
|
|
356
|
+
await service.patchFromTemplate(agentId, { A: "1" });
|
|
357
|
+
const rows = await rowsOf(agentId);
|
|
358
|
+
expect(rows[0]?.source).toBe("template");
|
|
359
|
+
expect(rows[0]?.enabled).toBe(true);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it("patch() of a template-sourced key turns it custom", async () => {
|
|
363
|
+
const agentId = await createAgent(prisma);
|
|
364
|
+
await service.patchFromTemplate(agentId, { A: "1" });
|
|
365
|
+
await service.patch(agentId, { A: "2" });
|
|
366
|
+
const rows = await rowsOf(agentId);
|
|
367
|
+
expect(rows[0]?.source).toBe("custom");
|
|
368
|
+
expect((await service.getByAgentId(agentId))?.env).toEqual({ A: "2" });
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it("patchFromTemplate() does not overwrite a custom or tombstoned key", async () => {
|
|
372
|
+
const agentId = await createAgent(prisma);
|
|
373
|
+
await service.patch(agentId, { C: "mine" });
|
|
374
|
+
await service.patchFromTemplate(agentId, { C: "tpl" });
|
|
375
|
+
expect((await service.getByAgentId(agentId))?.env).toEqual({ C: "mine" });
|
|
376
|
+
await service.patchFromTemplate(agentId, { T: "tpl" });
|
|
377
|
+
await service.deleteKey(agentId, "T");
|
|
378
|
+
await service.patchFromTemplate(agentId, { T: "tpl" });
|
|
379
|
+
const t = (await rowsOf(agentId)).find((r) => r.key === "T");
|
|
380
|
+
expect(t?.enabled).toBe(false);
|
|
381
|
+
expect(t?.source).toBe("custom");
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("deleteKey() of a template key leaves a tombstone that no read injects", async () => {
|
|
385
|
+
const agentId = await createAgent(prisma);
|
|
386
|
+
await service.patchFromTemplate(agentId, { T: "tpl" });
|
|
387
|
+
await service.patch(agentId, { K: "keep" });
|
|
388
|
+
await service.deleteKey(agentId, "T");
|
|
389
|
+
const t = (await rowsOf(agentId)).find((r) => r.key === "T");
|
|
390
|
+
expect(t?.enabled).toBe(false);
|
|
391
|
+
expect(t?.source).toBe("custom");
|
|
392
|
+
expect((await service.getByAgentId(agentId))?.env).toEqual({ K: "keep" });
|
|
393
|
+
expect((await service.getConfigBundle(agentId))?.env).toEqual({
|
|
394
|
+
K: "keep",
|
|
395
|
+
});
|
|
396
|
+
expect((await service.listAll())[0]?.env).toEqual({ K: "keep" });
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it("a tombstone-only agent reads as having no env", async () => {
|
|
400
|
+
const agentId = await createAgent(prisma);
|
|
401
|
+
await service.patchFromTemplate(agentId, { T: "tpl" });
|
|
402
|
+
await service.deleteKey(agentId, "T");
|
|
403
|
+
expect(await service.getByAgentId(agentId)).toBeNull();
|
|
404
|
+
expect(await service.getConfigBundle(agentId)).toBeNull();
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("deleteKey() of a custom key removes the row", async () => {
|
|
408
|
+
const agentId = await createAgent(prisma);
|
|
409
|
+
await service.patch(agentId, { K: "v" });
|
|
410
|
+
await service.deleteKey(agentId, "K");
|
|
411
|
+
expect(await rowsOf(agentId)).toEqual([]);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("patch() re-enables a tombstoned key", async () => {
|
|
415
|
+
const agentId = await createAgent(prisma);
|
|
416
|
+
await service.patchFromTemplate(agentId, { T: "tpl" });
|
|
417
|
+
await service.deleteKey(agentId, "T");
|
|
418
|
+
await service.patch(agentId, { T: "again" });
|
|
419
|
+
expect((await service.getByAgentId(agentId))?.env).toEqual({ T: "again" });
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it("upsert() tombstones template keys it omits", async () => {
|
|
423
|
+
const agentId = await createAgent(prisma);
|
|
424
|
+
await service.patchFromTemplate(agentId, { T: "tpl" });
|
|
425
|
+
await service.upsert(agentId, { K: "v" });
|
|
426
|
+
const t = (await rowsOf(agentId)).find((r) => r.key === "T");
|
|
427
|
+
expect(t?.enabled).toBe(false);
|
|
428
|
+
expect(t?.source).toBe("custom");
|
|
429
|
+
expect((await service.getByAgentId(agentId))?.env).toEqual({ K: "v" });
|
|
430
|
+
});
|
|
431
|
+
});
|
package/src/agent-envs.ts
CHANGED
|
@@ -84,24 +84,32 @@ export class AgentEnvService {
|
|
|
84
84
|
this.assertNoBlockedCredentials(agent, env);
|
|
85
85
|
|
|
86
86
|
const now = this.clock.now();
|
|
87
|
-
const
|
|
88
|
-
agentId,
|
|
89
|
-
key,
|
|
90
|
-
value: this.crypto.encrypt(value),
|
|
91
|
-
secret: secretKeys?.has(key) ?? false,
|
|
92
|
-
updatedAt: now,
|
|
93
|
-
}));
|
|
87
|
+
const keys = Object.keys(env);
|
|
94
88
|
|
|
89
|
+
// ATP-1.2: a template row the caller omits is a deletion, so it becomes a
|
|
90
|
+
// tombstone; tombstones the caller does not mention are kept.
|
|
95
91
|
await this.prisma.$transaction([
|
|
96
|
-
this.prisma.agentEnv.
|
|
97
|
-
|
|
92
|
+
this.prisma.agentEnv.updateMany({
|
|
93
|
+
where: {
|
|
94
|
+
agentId,
|
|
95
|
+
source: "template",
|
|
96
|
+
enabled: true,
|
|
97
|
+
key: { notIn: keys },
|
|
98
|
+
},
|
|
99
|
+
data: { enabled: false, source: "custom", updatedAt: now },
|
|
100
|
+
}),
|
|
101
|
+
this.prisma.agentEnv.deleteMany({
|
|
102
|
+
where: { agentId, OR: [{ enabled: true }, { key: { in: keys } }] },
|
|
103
|
+
}),
|
|
104
|
+
...Object.entries(env).map(([key, value]) =>
|
|
98
105
|
this.prisma.agentEnv.create({
|
|
99
106
|
data: {
|
|
100
|
-
agentId
|
|
101
|
-
key
|
|
102
|
-
value:
|
|
103
|
-
secret:
|
|
104
|
-
|
|
107
|
+
agentId,
|
|
108
|
+
key,
|
|
109
|
+
value: this.crypto.encrypt(value),
|
|
110
|
+
secret: secretKeys?.has(key) ?? false,
|
|
111
|
+
source: "custom",
|
|
112
|
+
updatedAt: now,
|
|
105
113
|
},
|
|
106
114
|
}),
|
|
107
115
|
),
|
|
@@ -137,11 +145,15 @@ export class AgentEnvService {
|
|
|
137
145
|
key,
|
|
138
146
|
value: this.crypto.encrypt(value),
|
|
139
147
|
secret: secretKeys?.has(key) ?? false,
|
|
148
|
+
source: "custom",
|
|
140
149
|
updatedAt: now,
|
|
141
150
|
},
|
|
142
151
|
update: {
|
|
143
152
|
value: this.crypto.encrypt(value),
|
|
144
153
|
secret: secretKeys?.has(key) ?? false,
|
|
154
|
+
// An admin edit takes ownership and re-enables a tombstone.
|
|
155
|
+
source: "custom",
|
|
156
|
+
enabled: true,
|
|
145
157
|
updatedAt: now,
|
|
146
158
|
},
|
|
147
159
|
}),
|
|
@@ -149,13 +161,63 @@ export class AgentEnvService {
|
|
|
149
161
|
);
|
|
150
162
|
}
|
|
151
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Internal reconcile write path (ATP-1.2); never exposed over HTTP.
|
|
166
|
+
* Upserts template-owned keys with source="template" without flipping
|
|
167
|
+
* source. Keys that are already custom (user edits or tombstones of deleted
|
|
168
|
+
* template rows) are left untouched so a reconcile never overrides or
|
|
169
|
+
* re-adds them.
|
|
170
|
+
*/
|
|
171
|
+
async patchFromTemplate(
|
|
172
|
+
agentId: string,
|
|
173
|
+
env: Record<string, string>,
|
|
174
|
+
secretKeys?: Set<string>,
|
|
175
|
+
): Promise<void> {
|
|
176
|
+
const agent = await this.prisma.agent.findUnique({
|
|
177
|
+
where: { id: agentId },
|
|
178
|
+
});
|
|
179
|
+
if (!agent) {
|
|
180
|
+
throw new UnprocessableEntityError("agent not found");
|
|
181
|
+
}
|
|
182
|
+
this.assertNoBlockedCredentials(agent, env);
|
|
183
|
+
|
|
184
|
+
const customKeys = new Set(
|
|
185
|
+
(
|
|
186
|
+
await this.prisma.agentEnv.findMany({
|
|
187
|
+
where: { agentId, source: "custom" },
|
|
188
|
+
select: { key: true },
|
|
189
|
+
})
|
|
190
|
+
).map((r) => r.key),
|
|
191
|
+
);
|
|
192
|
+
const now = this.clock.now();
|
|
193
|
+
await this.prisma.$transaction(
|
|
194
|
+
Object.entries(env)
|
|
195
|
+
.filter(([key]) => !customKeys.has(key))
|
|
196
|
+
.map(([key, value]) => {
|
|
197
|
+
const data = {
|
|
198
|
+
value: this.crypto.encrypt(value),
|
|
199
|
+
secret: secretKeys?.has(key) ?? false,
|
|
200
|
+
source: "template",
|
|
201
|
+
updatedAt: now,
|
|
202
|
+
};
|
|
203
|
+
return this.prisma.agentEnv.upsert({
|
|
204
|
+
where: { agentId_key: { agentId, key } },
|
|
205
|
+
create: { agentId, key, ...data },
|
|
206
|
+
update: data,
|
|
207
|
+
});
|
|
208
|
+
}),
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
152
212
|
/**
|
|
153
213
|
* Get all env vars for an agent.
|
|
154
214
|
* Secret values are masked as "***"; their keys are listed in secretKeys.
|
|
155
215
|
* Returns null if the agent has no env vars set.
|
|
156
216
|
*/
|
|
157
217
|
async getByAgentId(agentId: string): Promise<AgentEnvResult | null> {
|
|
158
|
-
const rows = await this.prisma.agentEnv.findMany({
|
|
218
|
+
const rows = await this.prisma.agentEnv.findMany({
|
|
219
|
+
where: { agentId, enabled: true },
|
|
220
|
+
});
|
|
159
221
|
if (rows.length === 0) return null;
|
|
160
222
|
|
|
161
223
|
const env: Record<string, string> = {};
|
|
@@ -181,7 +243,9 @@ export class AgentEnvService {
|
|
|
181
243
|
* Throws 500 if decryption fails.
|
|
182
244
|
*/
|
|
183
245
|
async getConfigBundle(agentId: string): Promise<AgentEnvBundle | null> {
|
|
184
|
-
const rows = await this.prisma.agentEnv.findMany({
|
|
246
|
+
const rows = await this.prisma.agentEnv.findMany({
|
|
247
|
+
where: { agentId, enabled: true },
|
|
248
|
+
});
|
|
185
249
|
if (rows.length === 0) return null;
|
|
186
250
|
|
|
187
251
|
let env: Record<string, string>;
|
|
@@ -206,17 +270,29 @@ export class AgentEnvService {
|
|
|
206
270
|
|
|
207
271
|
/**
|
|
208
272
|
* Delete a single env var key for the given agent.
|
|
209
|
-
* No-ops if the key does not exist.
|
|
273
|
+
* No-ops if the key does not exist. A template-sourced row becomes a
|
|
274
|
+
* disabled custom tombstone (ATP-1.2) so a later reconcile does not re-add
|
|
275
|
+
* it; a custom row is removed.
|
|
210
276
|
*/
|
|
211
277
|
async deleteKey(agentId: string, key: string): Promise<void> {
|
|
212
|
-
await this.prisma
|
|
278
|
+
await this.prisma.$transaction([
|
|
279
|
+
this.prisma.agentEnv.updateMany({
|
|
280
|
+
where: { agentId, key, source: "template" },
|
|
281
|
+
data: { enabled: false, source: "custom", updatedAt: this.clock.now() },
|
|
282
|
+
}),
|
|
283
|
+
this.prisma.agentEnv.deleteMany({
|
|
284
|
+
where: { agentId, key, source: "custom", enabled: true },
|
|
285
|
+
}),
|
|
286
|
+
]);
|
|
213
287
|
}
|
|
214
288
|
|
|
215
289
|
/**
|
|
216
290
|
* List all agents with their decrypted env vars.
|
|
217
291
|
*/
|
|
218
292
|
async listAll(): Promise<AgentEnvEntry[]> {
|
|
219
|
-
const rows = await this.prisma.agentEnv.findMany(
|
|
293
|
+
const rows = await this.prisma.agentEnv.findMany({
|
|
294
|
+
where: { enabled: true },
|
|
295
|
+
});
|
|
220
296
|
|
|
221
297
|
// Group by agentId
|
|
222
298
|
const byAgent = new Map<string, typeof rows>();
|
|
@@ -154,4 +154,56 @@ describeOrSkip("AgentPluginService (integration)", () => {
|
|
|
154
154
|
service.removeByName(agentId2, "shipwright@shipwright"),
|
|
155
155
|
).rejects.toBeInstanceOf(NotFoundError);
|
|
156
156
|
});
|
|
157
|
+
|
|
158
|
+
// ─── ATP-1.2: source / tombstones ──────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
it("add() stamps source=custom, including on a template row", async () => {
|
|
161
|
+
const agentId = await createAgent(prisma);
|
|
162
|
+
expect((await service.add(agentId, "a@m")).source).toBe("custom");
|
|
163
|
+
await service.addFromTemplate(agentId, "b@m", "1.0.0");
|
|
164
|
+
const edited = await service.add(agentId, "b@m", "2.0.0");
|
|
165
|
+
expect(edited.source).toBe("custom");
|
|
166
|
+
expect(edited.version).toBe("2.0.0");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("addFromTemplate() writes source=template and keeps it on re-run", async () => {
|
|
170
|
+
const agentId = await createAgent(prisma);
|
|
171
|
+
const first = await service.addFromTemplate(agentId, "a@m", "1.0.0");
|
|
172
|
+
expect(first.source).toBe("template");
|
|
173
|
+
const again = await service.addFromTemplate(agentId, "a@m", "1.1.0");
|
|
174
|
+
expect(again.source).toBe("template");
|
|
175
|
+
expect(again.version).toBe("1.1.0");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("addFromTemplate() does not resurrect a tombstone", async () => {
|
|
179
|
+
const agentId = await createAgent(prisma);
|
|
180
|
+
const p = await service.addFromTemplate(agentId, "a@m");
|
|
181
|
+
await service.remove(agentId, p.id);
|
|
182
|
+
const after = await service.addFromTemplate(agentId, "a@m");
|
|
183
|
+
expect(after.enabled).toBe(false);
|
|
184
|
+
expect(after.source).toBe("custom");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("remove() of a template row leaves a disabled custom tombstone hidden from listEnabled()", async () => {
|
|
188
|
+
const agentId = await createAgent(prisma);
|
|
189
|
+
const p = await service.addFromTemplate(agentId, "a@m");
|
|
190
|
+
await service.remove(agentId, p.id);
|
|
191
|
+
const rows = await prisma.agentPlugin.findMany({ where: { agentId } });
|
|
192
|
+
expect(rows).toHaveLength(1);
|
|
193
|
+
expect(rows[0]?.enabled).toBe(false);
|
|
194
|
+
expect(rows[0]?.source).toBe("custom");
|
|
195
|
+
expect(await service.listEnabled(agentId)).toEqual([]);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("removeByName() of a template row leaves a tombstone; of a custom row deletes it", async () => {
|
|
199
|
+
const agentId = await createAgent(prisma);
|
|
200
|
+
await service.addFromTemplate(agentId, "t@m");
|
|
201
|
+
await service.add(agentId, "c@m");
|
|
202
|
+
await service.removeByName(agentId, "t@m");
|
|
203
|
+
await service.removeByName(agentId, "c@m");
|
|
204
|
+
const rows = await prisma.agentPlugin.findMany({ where: { agentId } });
|
|
205
|
+
expect(rows.map((r) => [r.name, r.enabled, r.source])).toEqual([
|
|
206
|
+
["t@m", false, "custom"],
|
|
207
|
+
]);
|
|
208
|
+
});
|
|
157
209
|
});
|
package/src/agent-plugins.ts
CHANGED
|
@@ -56,11 +56,58 @@ export class AgentPluginService {
|
|
|
56
56
|
): Promise<AgentPlugin> {
|
|
57
57
|
return client.agentPlugin.upsert({
|
|
58
58
|
where: { agentId_name: { agentId, name } },
|
|
59
|
-
create: {
|
|
60
|
-
|
|
59
|
+
create: {
|
|
60
|
+
agentId,
|
|
61
|
+
name,
|
|
62
|
+
version: version ?? null,
|
|
63
|
+
enabled: true,
|
|
64
|
+
source: "custom",
|
|
65
|
+
},
|
|
66
|
+
update: { version: version ?? null, enabled: true, source: "custom" },
|
|
61
67
|
});
|
|
62
68
|
}
|
|
63
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Internal reconcile write path (ATP-1.2); never exposed over HTTP.
|
|
72
|
+
* Creates a template-sourced plugin, or refreshes one that is still
|
|
73
|
+
* template-owned. A custom row (user edit or tombstone) is returned
|
|
74
|
+
* untouched so a reconcile neither overrides nor resurrects it.
|
|
75
|
+
*/
|
|
76
|
+
async addFromTemplate(
|
|
77
|
+
agentId: string,
|
|
78
|
+
name: string,
|
|
79
|
+
version?: string | null,
|
|
80
|
+
client: PrismaTransactionClient = this.prisma,
|
|
81
|
+
): Promise<AgentPlugin> {
|
|
82
|
+
const existing = await client.agentPlugin.findUnique({
|
|
83
|
+
where: { agentId_name: { agentId, name } },
|
|
84
|
+
});
|
|
85
|
+
if (existing?.source === "custom") return existing;
|
|
86
|
+
return client.agentPlugin.upsert({
|
|
87
|
+
where: { agentId_name: { agentId, name } },
|
|
88
|
+
create: {
|
|
89
|
+
agentId,
|
|
90
|
+
name,
|
|
91
|
+
version: version ?? null,
|
|
92
|
+
enabled: true,
|
|
93
|
+
source: "template",
|
|
94
|
+
},
|
|
95
|
+
update: { version: version ?? null, enabled: true, source: "template" },
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Delete the row, or tombstone it when it is template-sourced (ATP-1.2). */
|
|
100
|
+
private async removeRow(row: AgentPlugin): Promise<void> {
|
|
101
|
+
if (row.source === "template") {
|
|
102
|
+
await this.prisma.agentPlugin.update({
|
|
103
|
+
where: { id: row.id },
|
|
104
|
+
data: { enabled: false, source: "custom" },
|
|
105
|
+
});
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
await this.prisma.agentPlugin.delete({ where: { id: row.id } });
|
|
109
|
+
}
|
|
110
|
+
|
|
64
111
|
/**
|
|
65
112
|
* Remove a plugin by ID. Verifies agentId ownership before deleting.
|
|
66
113
|
* Throws NotFoundError if the pluginId doesn't exist or belongs to a different agent.
|
|
@@ -74,7 +121,7 @@ export class AgentPluginService {
|
|
|
74
121
|
throw new NotFoundError(`plugin ${pluginId} not found`);
|
|
75
122
|
}
|
|
76
123
|
|
|
77
|
-
await this.
|
|
124
|
+
await this.removeRow(existing);
|
|
78
125
|
}
|
|
79
126
|
|
|
80
127
|
/**
|
|
@@ -90,6 +137,6 @@ export class AgentPluginService {
|
|
|
90
137
|
throw new NotFoundError(`plugin ${name} not found`);
|
|
91
138
|
}
|
|
92
139
|
|
|
93
|
-
await this.
|
|
140
|
+
await this.removeRow(existing);
|
|
94
141
|
}
|
|
95
142
|
}
|