@app-vitals/agent-admin 1.37.0 → 1.38.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.37.0",
3
+ "version": "1.38.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@anthropic-ai/sdk": "0.135.0",
45
- "@app-vitals/agent-lib": "1.37.0",
45
+ "@app-vitals/agent-lib": "1.38.0",
46
46
  "@hono/zod-openapi": "^1.0.0",
47
47
  "@prisma/adapter-pg": "^7.10.0",
48
48
  "@prisma/client": "^7.10.0",
@@ -959,6 +959,92 @@ describeOrSkip("AgentCronRunService (integration)", () => {
959
959
  expect(items.some((r) => r.id === skippedWithOutcome.id)).toBe(true);
960
960
  });
961
961
 
962
+ describe("outcomes filter", () => {
963
+ async function seed() {
964
+ const agentId = await createAgent(prisma);
965
+ const cronId = await createCron(cronJobService, agentId);
966
+ const mk = (day: string, skipped: boolean, outcome: string | null) =>
967
+ runService.create(cronId, agentId, {
968
+ startedAt: new Date(`2026-01-${day}T08:00:00Z`),
969
+ skipped,
970
+ outcome,
971
+ });
972
+ const skipped = await mk("01", true, null);
973
+ const unknown = await mk("02", false, null);
974
+ const completed = await mk("03", false, "completed");
975
+ const failed = await mk("04", false, "failed");
976
+ return { agentId, ids: { skipped, unknown, completed, failed } };
977
+ }
978
+
979
+ const idsOf = (items: { id: string }[]) => items.map((r) => r.id).sort();
980
+
981
+ it("listForAgent() single outcomes value selects just that bucket", async () => {
982
+ const { agentId, ids } = await seed();
983
+ for (const key of [
984
+ "skipped",
985
+ "unknown",
986
+ "completed",
987
+ "failed",
988
+ ] as const) {
989
+ const { items, total } = await runService.listForAgent(agentId, {
990
+ outcomes: [key],
991
+ });
992
+ expect(total).toBe(1);
993
+ expect(idsOf(items)).toEqual([ids[key].id]);
994
+ }
995
+ });
996
+
997
+ it("listForAgent() multiple outcomes values are OR'd and total matches", async () => {
998
+ const { agentId, ids } = await seed();
999
+ const { items, total } = await runService.listForAgent(agentId, {
1000
+ outcomes: ["skipped", "unknown", "failed"],
1001
+ limit: 2,
1002
+ });
1003
+ expect(total).toBe(3);
1004
+ expect(items).toHaveLength(2);
1005
+ const all = await runService.listForAgent(agentId, {
1006
+ outcomes: ["skipped", "unknown", "failed"],
1007
+ });
1008
+ expect(idsOf(all.items)).toEqual(
1009
+ [ids.skipped.id, ids.unknown.id, ids.failed.id].sort(),
1010
+ );
1011
+ });
1012
+
1013
+ it("listForAgent() empty outcomes is no filter; outcomes beats outcome", async () => {
1014
+ const { agentId, ids } = await seed();
1015
+ const none = await runService.listForAgent(agentId, { outcomes: [] });
1016
+ expect(none.total).toBe(4);
1017
+ const both = await runService.listForAgent(agentId, {
1018
+ outcome: "completed",
1019
+ outcomes: ["failed"],
1020
+ });
1021
+ expect(idsOf(both.items)).toEqual([ids.failed.id]);
1022
+ });
1023
+
1024
+ it("listForAgent() legacy outcome still works", async () => {
1025
+ const { agentId, ids } = await seed();
1026
+ const { items, total } = await runService.listForAgent(agentId, {
1027
+ outcome: "completed",
1028
+ });
1029
+ expect(total).toBe(1);
1030
+ expect(idsOf(items)).toEqual([ids.completed.id]);
1031
+ });
1032
+
1033
+ it("listAcrossAgents() applies outcomes with matching total", async () => {
1034
+ const { agentId, ids } = await seed();
1035
+ const { items, total } = await runService.listAcrossAgents([agentId], {
1036
+ outcomes: ["unknown", "completed"],
1037
+ });
1038
+ expect(total).toBe(2);
1039
+ expect(idsOf(items)).toEqual([ids.unknown.id, ids.completed.id].sort());
1040
+ const single = await runService.listAcrossAgents([agentId], {
1041
+ outcomes: ["skipped"],
1042
+ });
1043
+ expect(single.total).toBe(1);
1044
+ expect(single.items[0].id).toBe(ids.skipped.id);
1045
+ });
1046
+ });
1047
+
962
1048
  it("listForAgent() never returns a second agent's runs", async () => {
963
1049
  const agentId1 = await createAgent(prisma, "Agent 1");
964
1050
  const agentId2 = await createAgent(prisma, "Agent 2");
@@ -146,6 +146,13 @@ interface ListForAgentOptions {
146
146
  * WHERE skipped = false AND outcome = value. Omitted means no filter.
147
147
  */
148
148
  outcome?: string;
149
+ /**
150
+ * Multi-select variant of `outcome`; values are OR'd. "skipped" ->
151
+ * skipped = true; "unknown" -> skipped = false AND outcome IS NULL; any
152
+ * other value -> skipped = false AND outcome = value. An empty array is no
153
+ * filter. When given, `outcomes` wins over `outcome`.
154
+ */
155
+ outcomes?: string[];
149
156
  limit?: number;
150
157
  offset?: number;
151
158
  }
@@ -171,10 +178,45 @@ interface ListAcrossAgentsOptions {
171
178
  * WHERE skipped = false AND outcome = value. Omitted means no filter.
172
179
  */
173
180
  outcome?: string;
181
+ /**
182
+ * Multi-select variant of `outcome`; values are OR'd. "skipped" ->
183
+ * skipped = true; "unknown" -> skipped = false AND outcome IS NULL; any
184
+ * other value -> skipped = false AND outcome = value. An empty array is no
185
+ * filter. When given, `outcomes` wins over `outcome`.
186
+ */
187
+ outcomes?: string[];
174
188
  limit?: number;
175
189
  offset?: number;
176
190
  }
177
191
 
192
+ /** Where-clause fragment for one outcome filter value (see `outcomes`). */
193
+ function outcomeClause(value: string): Prisma.AgentCronRunWhereInput {
194
+ if (value === "skipped") return { skipped: true };
195
+ if (value === "unknown") return { skipped: false, outcome: null };
196
+ return { skipped: false, outcome: value };
197
+ }
198
+
199
+ /**
200
+ * Translate the `outcome` / `outcomes` list options into a where fragment.
201
+ * `outcomes` (OR'd) wins over the legacy single `outcome`; an empty `outcomes`
202
+ * array is no filter (it does not fall back to `outcome`).
203
+ */
204
+ export function buildOutcomeWhere(opts?: {
205
+ outcome?: string;
206
+ outcomes?: string[];
207
+ }): Prisma.AgentCronRunWhereInput {
208
+ if (opts?.outcomes !== undefined) {
209
+ return opts.outcomes.length > 0
210
+ ? { OR: opts.outcomes.map(outcomeClause) }
211
+ : {};
212
+ }
213
+ if (opts?.outcome === "skipped") return { skipped: true };
214
+ if (opts?.outcome !== undefined) {
215
+ return { skipped: false, outcome: opts.outcome };
216
+ }
217
+ return {};
218
+ }
219
+
178
220
  export class AgentCronRunService {
179
221
  constructor(private prisma: PrismaClient) {}
180
222
 
@@ -450,12 +492,7 @@ export class AgentCronRunService {
450
492
  if (opts?.cronId) {
451
493
  where.cronId = opts.cronId;
452
494
  }
453
- if (opts?.outcome === "skipped") {
454
- where.skipped = true;
455
- } else if (opts?.outcome !== undefined) {
456
- where.skipped = false;
457
- where.outcome = opts.outcome;
458
- }
495
+ Object.assign(where, buildOutcomeWhere(opts));
459
496
 
460
497
  const [items, total] = await this.prisma.$transaction([
461
498
  this.prisma.agentCronRun.findMany({
@@ -494,12 +531,7 @@ export class AgentCronRunService {
494
531
  if (opts?.cronId) {
495
532
  where.cronId = opts.cronId;
496
533
  }
497
- if (opts?.outcome === "skipped") {
498
- where.skipped = true;
499
- } else if (opts?.outcome !== undefined) {
500
- where.skipped = false;
501
- where.outcome = opts.outcome;
502
- }
534
+ Object.assign(where, buildOutcomeWhere(opts));
503
535
 
504
536
  const [items, total] = await this.prisma.$transaction([
505
537
  this.prisma.agentCronRun.findMany({
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Unit tests for agent-cron-runs.ts's pure outcome-filter mapping
3
+ * (buildOutcomeWhere): no DB, just the where-clause shape.
4
+ */
5
+ import { describe, expect, test } from "bun:test";
6
+ import { buildOutcomeWhere } from "./agent-cron-runs.ts";
7
+
8
+ describe("buildOutcomeWhere", () => {
9
+ test("no options yields no filter", () => {
10
+ expect(buildOutcomeWhere()).toEqual({});
11
+ expect(buildOutcomeWhere({})).toEqual({});
12
+ });
13
+
14
+ test("legacy outcome: skipped matches the boolean flag only", () => {
15
+ expect(buildOutcomeWhere({ outcome: "skipped" })).toEqual({
16
+ skipped: true,
17
+ });
18
+ });
19
+
20
+ test("legacy outcome: other value requires skipped=false and equality", () => {
21
+ expect(buildOutcomeWhere({ outcome: "success" })).toEqual({
22
+ skipped: false,
23
+ outcome: "success",
24
+ });
25
+ });
26
+
27
+ test("outcomes: each value maps to its clause", () => {
28
+ expect(buildOutcomeWhere({ outcomes: ["skipped"] })).toEqual({
29
+ OR: [{ skipped: true }],
30
+ });
31
+ expect(buildOutcomeWhere({ outcomes: ["unknown"] })).toEqual({
32
+ OR: [{ skipped: false, outcome: null }],
33
+ });
34
+ expect(buildOutcomeWhere({ outcomes: ["failed"] })).toEqual({
35
+ OR: [{ skipped: false, outcome: "failed" }],
36
+ });
37
+ });
38
+
39
+ test("outcomes: multiple values are OR'd in order", () => {
40
+ expect(
41
+ buildOutcomeWhere({ outcomes: ["skipped", "unknown", "completed"] }),
42
+ ).toEqual({
43
+ OR: [
44
+ { skipped: true },
45
+ { skipped: false, outcome: null },
46
+ { skipped: false, outcome: "completed" },
47
+ ],
48
+ });
49
+ });
50
+
51
+ test("outcomes: empty array is no filter", () => {
52
+ expect(buildOutcomeWhere({ outcomes: [] })).toEqual({});
53
+ });
54
+
55
+ test("outcomes wins over outcome when both are passed", () => {
56
+ expect(
57
+ buildOutcomeWhere({ outcome: "success", outcomes: ["failed"] }),
58
+ ).toEqual({ OR: [{ skipped: false, outcome: "failed" }] });
59
+ });
60
+
61
+ test("empty outcomes still wins over outcome (no filter)", () => {
62
+ expect(buildOutcomeWhere({ outcome: "success", outcomes: [] })).toEqual({});
63
+ });
64
+ });