@app-vitals/agent-admin 1.41.0 → 1.42.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/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 rows = Object.entries(env).map(([key, value]) => ({
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.deleteMany({ where: { agentId } }),
97
- ...rows.map((row) =>
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: row.agentId,
101
- key: row.key,
102
- value: row.value,
103
- secret: row.secret,
104
- updatedAt: row.updatedAt,
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({ where: { agentId } });
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({ where: { agentId } });
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.agentEnv.deleteMany({ where: { agentId, key } });
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
  });
@@ -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: { agentId, name, version: version ?? null, enabled: true },
60
- update: { version: version ?? null, enabled: true },
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.prisma.agentPlugin.delete({ where: { id: pluginId } });
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.prisma.agentPlugin.delete({ where: { id: existing.id } });
140
+ await this.removeRow(existing);
94
141
  }
95
142
  }
@@ -126,4 +126,59 @@ describeOrSkip("AgentToolService (integration)", () => {
126
126
  service.updatePattern(agentId, "nonexistent", "Read"),
127
127
  ).rejects.toBeInstanceOf(NotFoundError);
128
128
  });
129
+
130
+ // ─── ATP-1.2: source / tombstones ──────────────────────────────────────────
131
+
132
+ it("add() stamps source=custom, including on a template row", async () => {
133
+ const agentId = await createAgent(prisma);
134
+ const fresh = await service.add(agentId, "Read");
135
+ expect(fresh.source).toBe("custom");
136
+ await service.addFromTemplate(agentId, "Bash");
137
+ const edited = await service.add(agentId, "Bash");
138
+ expect(edited.source).toBe("custom");
139
+ });
140
+
141
+ it("addFromTemplate() writes source=template and keeps it on re-run", async () => {
142
+ const agentId = await createAgent(prisma);
143
+ const first = await service.addFromTemplate(agentId, "Read");
144
+ expect(first.source).toBe("template");
145
+ expect(first.enabled).toBe(true);
146
+ const again = await service.addFromTemplate(agentId, "Read");
147
+ expect(again.source).toBe("template");
148
+ });
149
+
150
+ it("addFromTemplate() does not resurrect a tombstone or reclaim a custom row", async () => {
151
+ const agentId = await createAgent(prisma);
152
+ const t = await service.addFromTemplate(agentId, "Read");
153
+ await service.remove(agentId, t.id);
154
+ const after = await service.addFromTemplate(agentId, "Read");
155
+ expect(after.enabled).toBe(false);
156
+ expect(after.source).toBe("custom");
157
+ });
158
+
159
+ it("toggle() and updatePattern() turn a template row custom", async () => {
160
+ const agentId = await createAgent(prisma);
161
+ const a = await service.addFromTemplate(agentId, "Read");
162
+ expect((await service.toggle(agentId, a.id, false)).source).toBe("custom");
163
+ const b = await service.addFromTemplate(agentId, "Bash");
164
+ const updated = await service.updatePattern(agentId, b.id, "Bash(ls)");
165
+ expect(updated.source).toBe("custom");
166
+ });
167
+
168
+ it("remove() of a template row leaves a disabled custom tombstone", async () => {
169
+ const agentId = await createAgent(prisma);
170
+ const t = await service.addFromTemplate(agentId, "Read");
171
+ await service.remove(agentId, t.id);
172
+ const rows = await prisma.agentTool.findMany({ where: { agentId } });
173
+ expect(rows).toHaveLength(1);
174
+ expect(rows[0]?.enabled).toBe(false);
175
+ expect(rows[0]?.source).toBe("custom");
176
+ });
177
+
178
+ it("remove() of a custom row deletes it", async () => {
179
+ const agentId = await createAgent(prisma);
180
+ const t = await service.add(agentId, "Read");
181
+ await service.remove(agentId, t.id);
182
+ expect(await prisma.agentTool.findMany({ where: { agentId } })).toEqual([]);
183
+ });
129
184
  });
@@ -41,8 +41,30 @@ export class AgentToolService {
41
41
  ): Promise<AgentTool> {
42
42
  return client.agentTool.upsert({
43
43
  where: { agentId_pattern: { agentId, pattern } },
44
- create: { agentId, pattern, enabled: true },
45
- update: { enabled: true },
44
+ create: { agentId, pattern, enabled: true, source: "custom" },
45
+ update: { enabled: true, source: "custom" },
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Internal reconcile write path (ATP-1.2); never exposed over HTTP.
51
+ * Creates a template-sourced pattern, or refreshes one that is still
52
+ * template-owned. A custom row (user edit or tombstone) is returned
53
+ * untouched so a reconcile neither overrides nor resurrects it.
54
+ */
55
+ async addFromTemplate(
56
+ agentId: string,
57
+ pattern: string,
58
+ client: PrismaTransactionClient = this.prisma,
59
+ ): Promise<AgentTool> {
60
+ const existing = await client.agentTool.findUnique({
61
+ where: { agentId_pattern: { agentId, pattern } },
62
+ });
63
+ if (existing?.source === "custom") return existing;
64
+ return client.agentTool.upsert({
65
+ where: { agentId_pattern: { agentId, pattern } },
66
+ create: { agentId, pattern, enabled: true, source: "template" },
67
+ update: { enabled: true, source: "template" },
46
68
  });
47
69
  }
48
70
 
@@ -59,6 +81,15 @@ export class AgentToolService {
59
81
  throw new NotFoundError(`tool ${toolId} not found`);
60
82
  }
61
83
 
84
+ // ATP-1.2: deleting a template row writes a tombstone so a later
85
+ // reconcile does not re-add it.
86
+ if (existing.source === "template") {
87
+ await this.prisma.agentTool.update({
88
+ where: { id: toolId },
89
+ data: { enabled: false, source: "custom" },
90
+ });
91
+ return;
92
+ }
62
93
  await this.prisma.agentTool.delete({ where: { id: toolId } });
63
94
  }
64
95
 
@@ -81,7 +112,7 @@ export class AgentToolService {
81
112
 
82
113
  return this.prisma.agentTool.update({
83
114
  where: { id: toolId },
84
- data: { enabled },
115
+ data: { enabled, source: "custom" },
85
116
  });
86
117
  }
87
118
 
@@ -105,7 +136,7 @@ export class AgentToolService {
105
136
 
106
137
  return this.prisma.agentTool.update({
107
138
  where: { id: toolId },
108
- data: { pattern },
139
+ data: { pattern, source: "custom" },
109
140
  });
110
141
  }
111
142
  }
@@ -5778,6 +5778,7 @@ describe("admin API — Anthropic credential guards (WIF-2.1)", () => {
5778
5778
  findUnique: async () => ({ id: AGENT_ID, selfHosted: opts.selfHosted }),
5779
5779
  },
5780
5780
  agentEnv: {
5781
+ updateMany: async () => ({ count: 0 }),
5781
5782
  deleteMany: async () => ({ count: 0 }),
5782
5783
  create: async ({ data }: { data: { key: string } }) => {
5783
5784
  writes.push(data.key);
package/src/agents.ts CHANGED
@@ -441,7 +441,7 @@ export class AgentService {
441
441
 
442
442
  // Key names only — never select/decrypt AgentEnv.value here.
443
443
  const presentEnvRows = await client.agentEnv.findMany({
444
- where: { agentId },
444
+ where: { agentId, enabled: true },
445
445
  select: { key: true },
446
446
  });
447
447
  const presentKeys = new Set(presentEnvRows.map((r) => r.key));