@terreno/ai 57.10.1 → 57.12.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.
Files changed (66) hide show
  1. package/dist/aiApp.d.ts +5 -0
  2. package/dist/aiApp.js +4 -2
  3. package/dist/harness/commit.d.ts +17 -1
  4. package/dist/harness/commit.js +86 -2
  5. package/dist/harness/errors.d.ts +6 -0
  6. package/dist/harness/errors.js +6 -0
  7. package/dist/harness/harness.d.ts +2 -2
  8. package/dist/harness/harness.js +4 -2
  9. package/dist/harness/models/harnessTask.js +5 -0
  10. package/dist/harness/routes/approvals.js +2 -2
  11. package/dist/harness/routes/conversations.js +1 -1
  12. package/dist/harness/routes/tasks.js +85 -3
  13. package/dist/harness/runners/inProcessRunner.d.ts +11 -4
  14. package/dist/harness/runners/inProcessRunner.js +44 -13
  15. package/dist/harness/runners/jobsRunner.d.ts +62 -0
  16. package/dist/harness/runners/jobsRunner.js +212 -0
  17. package/dist/harness/runtime.d.ts +20 -2
  18. package/dist/harness/runtime.js +91 -32
  19. package/dist/harness/subagent.d.ts +17 -2
  20. package/dist/harness/subagent.js +26 -2
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +3 -2
  23. package/dist/models/project.d.ts +13 -0
  24. package/dist/models/project.js +32 -2
  25. package/dist/observability/local/models/obsTrace.js +16 -2
  26. package/dist/observability/local/traceStore.d.ts +5 -0
  27. package/dist/observability/local/traceStore.js +5 -0
  28. package/dist/observability/routes/traces.js +2 -0
  29. package/dist/routes/projects.js +3 -3
  30. package/dist/service/chatTurn.js +1 -1
  31. package/dist/types/harness.d.ts +47 -3
  32. package/dist/types/observability.d.ts +3 -0
  33. package/package.json +65 -6
  34. package/src/aiApp.test.ts +15 -0
  35. package/src/aiApp.ts +9 -1
  36. package/src/harness/commit.ts +103 -1
  37. package/src/harness/concurrency.test.ts +155 -0
  38. package/src/harness/errors.ts +6 -0
  39. package/src/harness/events.test.ts +17 -5
  40. package/src/harness/harness.ts +14 -3
  41. package/src/harness/models/harnessTask.ts +5 -0
  42. package/src/harness/ownership.test.ts +9 -9
  43. package/src/harness/recovery.test.ts +2 -7
  44. package/src/harness/routes/approvals.ts +2 -2
  45. package/src/harness/routes/conversations.ts +1 -1
  46. package/src/harness/routes/tasks.test.ts +34 -0
  47. package/src/harness/routes/tasks.ts +98 -3
  48. package/src/harness/runners/inProcessRunner.test.ts +128 -0
  49. package/src/harness/runners/inProcessRunner.ts +54 -14
  50. package/src/harness/runners/jobsRunner.test.ts +709 -0
  51. package/src/harness/runners/jobsRunner.ts +252 -0
  52. package/src/harness/runtime.ts +125 -30
  53. package/src/harness/subagent.test.ts +124 -3
  54. package/src/harness/subagent.ts +43 -1
  55. package/src/index.ts +1 -1
  56. package/src/isolated/lazyProjectModel.isolated.ts +25 -0
  57. package/src/models/project.ts +31 -1
  58. package/src/observability/local/models/obsTrace.ts +22 -5
  59. package/src/observability/local/traceStore.test.ts +7 -0
  60. package/src/observability/local/traceStore.ts +12 -0
  61. package/src/observability/routes/traces.test.ts +15 -0
  62. package/src/observability/routes/traces.ts +2 -0
  63. package/src/routes/projects.ts +4 -4
  64. package/src/service/chatTurn.ts +2 -2
  65. package/src/types/harness.ts +56 -3
  66. package/src/types/observability.ts +3 -0
@@ -0,0 +1,155 @@
1
+ import {afterEach, beforeAll, beforeEach, describe, expect, it} from "bun:test";
2
+
3
+ import {createLocalObservabilityPlugin} from "../observability/local/localPlugin";
4
+ import {registerObsSpan} from "../observability/local/models/obsSpan";
5
+ import {registerObsTrace} from "../observability/local/models/obsTrace";
6
+ import type {ObsSpanModel, ObsTraceModel} from "../types/observability";
7
+ import {type AnyHarnessTaskDefinition, defineTask, Harness, InProcessRunner} from "./harness";
8
+ import {registerHarnessOwner} from "./models/harnessOwner";
9
+ import {registerHarnessTask} from "./models/harnessTask";
10
+
11
+ const TaskModel = registerHarnessTask();
12
+ const OwnerModel = registerHarnessOwner();
13
+ let SpanModel: ObsSpanModel;
14
+ let TraceModel: ObsTraceModel;
15
+
16
+ const pause = (ms: number): Promise<void> => new Promise((done) => setTimeout(done, ms));
17
+
18
+ const openHarnesses: Harness[] = [];
19
+ const releases: Array<() => void> = [];
20
+
21
+ const openHarness = async ({
22
+ concurrency,
23
+ registry,
24
+ }: {
25
+ concurrency: number;
26
+ registry: ReadonlyArray<AnyHarnessTaskDefinition>;
27
+ }): Promise<Harness> => {
28
+ const harness = await Harness.open({
29
+ registry,
30
+ runner: new InProcessRunner({concurrency, pollInterval: {milliseconds: 20}}),
31
+ });
32
+ openHarnesses.push(harness);
33
+ await harness.start();
34
+ return harness;
35
+ };
36
+
37
+ describe("InProcessRunner concurrency against Mongo", () => {
38
+ beforeAll(() => {
39
+ createLocalObservabilityPlugin();
40
+ SpanModel = registerObsSpan();
41
+ TraceModel = registerObsTrace();
42
+ });
43
+
44
+ beforeEach(async () => {
45
+ await Promise.all([
46
+ TaskModel.deleteMany({}),
47
+ OwnerModel.deleteMany({}),
48
+ SpanModel.deleteMany({}),
49
+ TraceModel.deleteMany({}),
50
+ ]);
51
+ });
52
+
53
+ afterEach(async () => {
54
+ for (const release of releases.splice(0)) {
55
+ release();
56
+ }
57
+ await Promise.all(openHarnesses.splice(0).map((harness) => harness.stop()));
58
+ });
59
+
60
+ it("completes a second task while the first is blocked on a gate", async () => {
61
+ let releaseGate: () => void = () => {};
62
+ const gated = new Promise<void>((done) => {
63
+ releaseGate = done;
64
+ });
65
+ releases.push(releaseGate);
66
+ const definition = defineTask<{blocked: boolean}, Record<string, never>, {status: string}>({
67
+ initial: () => ({phase: "work", state: {}}),
68
+ name: "test.concurrencyGate",
69
+ phases: {
70
+ work: {
71
+ run: async (task, rt) => {
72
+ if (task.input.blocked) {
73
+ await gated;
74
+ }
75
+ await rt.commit({terminal: {result: {status: "done"}, status: "completed"}});
76
+ },
77
+ },
78
+ },
79
+ version: 1,
80
+ }) as unknown as AnyHarnessTaskDefinition;
81
+ const harness = await openHarness({concurrency: 2, registry: [definition]});
82
+
83
+ const blocked = await harness.createTask(definition as never, {blocked: true});
84
+ const free = await harness.createTask(definition as never, {blocked: false});
85
+ const freeDone = await harness.waitForTask(free._id, {timeout: {seconds: 5}});
86
+
87
+ expect(freeDone.status).toBe("completed");
88
+ expect((await TaskModel.findExactlyOne({_id: blocked._id})).status).toBe("running");
89
+ releaseGate();
90
+ expect((await harness.waitForTask(blocked._id, {timeout: {seconds: 5}})).status).toBe(
91
+ "completed"
92
+ );
93
+ });
94
+
95
+ it("never runs one task twice at once and never exceeds the concurrency", async () => {
96
+ const activeByTask = new Map<string, number>();
97
+ let active = 0;
98
+ let peak = 0;
99
+ let peakPerTask = 0;
100
+ const enter = (taskId: string): void => {
101
+ active += 1;
102
+ peak = Math.max(peak, active);
103
+ const count = (activeByTask.get(taskId) ?? 0) + 1;
104
+ activeByTask.set(taskId, count);
105
+ peakPerTask = Math.max(peakPerTask, count);
106
+ };
107
+ const leave = (taskId: string): void => {
108
+ active -= 1;
109
+ activeByTask.set(taskId, (activeByTask.get(taskId) ?? 1) - 1);
110
+ };
111
+ // The first phase holds until three run at once (or 2 s pass), so the peak does not
112
+ // depend on how fast CI Mongo claims.
113
+ const fullHouse = async (): Promise<void> => {
114
+ for (let i = 0; i < 200 && active < 3; i++) {
115
+ await pause(10);
116
+ }
117
+ };
118
+ const definition = defineTask<{n: number}, Record<string, never>, {status: string}>({
119
+ initial: () => ({phase: "first", state: {}}),
120
+ name: "test.concurrencyPeak",
121
+ phases: {
122
+ first: {
123
+ run: async (task, rt) => {
124
+ enter(task.id);
125
+ await fullHouse();
126
+ await pause(30);
127
+ leave(task.id);
128
+ await rt.commit({phase: "second"});
129
+ },
130
+ },
131
+ second: {
132
+ run: async (task, rt) => {
133
+ enter(task.id);
134
+ await pause(30);
135
+ leave(task.id);
136
+ await rt.commit({terminal: {result: {status: "done"}, status: "completed"}});
137
+ },
138
+ },
139
+ },
140
+ version: 1,
141
+ }) as unknown as AnyHarnessTaskDefinition;
142
+ const harness = await openHarness({concurrency: 3, registry: [definition]});
143
+
144
+ const created = await Promise.all(
145
+ [1, 2, 3, 4, 5, 6, 7].map((n) => harness.createTask(definition as never, {n}))
146
+ );
147
+ const done = await Promise.all(
148
+ created.map((task) => harness.waitForTask(task._id, {timeout: {seconds: 10}}))
149
+ );
150
+
151
+ expect(done.map((task) => task.status)).toEqual(Array(7).fill("completed"));
152
+ expect(peakPerTask).toBe(1);
153
+ expect(peak).toBe(3);
154
+ });
155
+ });
@@ -73,6 +73,12 @@ export const HARNESS_ERRORS = {
73
73
  status: 409,
74
74
  title: "requestId is already in use",
75
75
  },
76
+ /** A `JobsRunner` phase job ran on an instance whose harness is not started. */
77
+ runnerStopped: {
78
+ code: "harness-runner-stopped",
79
+ status: 503,
80
+ title: "Harness runner is not running",
81
+ },
76
82
  /** `retry` on a task whose abort is still running. */
77
83
  taskAborting: {code: "harness-task-aborting", status: 409, title: "Task is being aborted"},
78
84
  /** `resolveInterrupted` on a task that is not interrupted. */
@@ -577,21 +577,33 @@ describe("Harness event stream", () => {
577
577
  const who = frame.data.taskId === String(created._id) ? "parent" : "child";
578
578
  return `${who} ${frame.event} ${payload.status ?? payload.text ?? ""}`.trim();
579
579
  };
580
- expect(parentStream.frames.map(describe)).toEqual([
580
+ // Parent and child run concurrently, so only each task's own order is fixed.
581
+ const lines = parentStream.frames.map(describe);
582
+ expect(lines.filter((line) => line.startsWith("parent"))).toEqual([
581
583
  "parent task.status pending",
582
584
  "parent output parent starting",
583
- "child task.status pending",
584
585
  "parent task.status waiting",
586
+ // Each phase re-runs after its wait, so its output is sent again.
587
+ "parent output parent starting",
588
+ "parent task.status completed",
589
+ ]);
590
+ expect(lines.filter((line) => line.startsWith("child"))).toEqual([
591
+ "child task.status pending",
585
592
  "child output child 1 working",
586
593
  "child approval.requested pending",
587
594
  "child task.status waiting",
588
595
  "child approval.decided approved",
589
- // Each phase re-runs after its wait, so its output is sent again.
590
596
  "child output child 1 working",
591
597
  "child task.status completed",
592
- "parent output parent starting",
593
- "parent task.status completed",
594
598
  ]);
599
+ // The child is created inside the parent's first phase, and the parent resumes only
600
+ // after the child completes.
601
+ expect(lines.indexOf("child task.status pending")).toBeLessThan(
602
+ lines.indexOf("parent task.status waiting")
603
+ );
604
+ expect(lines.indexOf("child task.status completed")).toBeLessThan(
605
+ lines.lastIndexOf("parent output parent starting")
606
+ );
595
607
  // The child's stream is the same log narrowed to the child: no parent events.
596
608
  expect(childStream.frames.map(describe)).toEqual(
597
609
  parentStream.frames.map(describe).filter((line) => line.startsWith("child"))
@@ -69,7 +69,13 @@ import {
69
69
  splitRegistry,
70
70
  } from "./registry";
71
71
  import {InProcessRunner} from "./runners/inProcessRunner";
72
- import {claimNextTask, recoverExpiredTasks, runClaimedTask} from "./runtime";
72
+ import {
73
+ claimNextTask,
74
+ claimTaskById,
75
+ listRunnableTasks,
76
+ recoverExpiredTasks,
77
+ runClaimedTask,
78
+ } from "./runtime";
73
79
  import {sendEventRecords} from "./waits";
74
80
 
75
81
  export type {
@@ -121,8 +127,10 @@ export type {
121
127
  HarnessResolveInterruptedOptions,
122
128
  HarnessRetryPolicy,
123
129
  HarnessRunAgentOptions,
130
+ HarnessRunnableTask,
124
131
  HarnessRunner,
125
132
  HarnessRunnerContext,
133
+ HarnessRunTaskOptions,
126
134
  HarnessSection,
127
135
  HarnessSectionInput,
128
136
  HarnessSendEventOptions,
@@ -426,9 +434,12 @@ export class Harness {
426
434
  await this.runner.start({
427
435
  acquireOwnerLease: (lease) => acquireOwnerLease({lease, models, testHooks}),
428
436
  claimNext: (lease) => this.claimNextOrSweep(lease),
437
+ claimTask: (taskId, lease) => claimTaskById({definitions, lease, models, taskId}),
438
+ listRunnable: (limit) => listRunnableTasks({definitions, limit, models}),
429
439
  recoverExpired: () => recoverExpiredTasks(engine),
430
440
  releaseOwnerLease: (lease) => releaseOwnerLease({lease, models}),
431
- runTask: (task, lease) => runClaimedTask({engine, lease, task}),
441
+ runTask: (task, lease, options) =>
442
+ runClaimedTask({engine, lease, maxPhases: options?.maxPhases, task}),
432
443
  });
433
444
  }
434
445
 
@@ -450,7 +461,7 @@ export class Harness {
450
461
  return claimNextTask({definitions, lease, models});
451
462
  }
452
463
 
453
- /** Stop claiming work and wait for the phase in flight to settle. */
464
+ /** Stop claiming work and wait for every phase in flight to settle. */
454
465
  async stop(): Promise<void> {
455
466
  if (!this.isStarted) {
456
467
  return;
@@ -39,6 +39,11 @@ const harnessTaskSchema = new mongoose.Schema<HarnessTaskDocument, HarnessTaskMo
39
39
  description: "When true, the task outlives its owning conversation turn",
40
40
  type: Boolean,
41
41
  },
42
+ claims: {
43
+ default: 0,
44
+ description: "Times a runner claimed the task; numbers each runnable visit for job dispatch",
45
+ type: Number,
46
+ },
42
47
  eventSeq: {
43
48
  default: 0,
44
49
  description: "Events received by harness.sendEvent; numbers the task's inbox",
@@ -43,11 +43,12 @@ const waitUntil = async (predicate: () => Promise<boolean>, label: string): Prom
43
43
 
44
44
  const openHarness = async (
45
45
  registry: ReadonlyArray<AnyHarnessTaskDefinition>,
46
- testHooks?: HarnessTestHooks
46
+ testHooks?: HarnessTestHooks,
47
+ concurrency?: number
47
48
  ): Promise<Harness> => {
48
49
  const harness = await Harness.open({
49
50
  registry,
50
- runner: new InProcessRunner({pollInterval: {milliseconds: 20}}),
51
+ runner: new InProcessRunner({concurrency, pollInterval: {milliseconds: 20}}),
51
52
  testHooks,
52
53
  });
53
54
  openHarnesses.push(harness);
@@ -500,13 +501,12 @@ describe("Harness ownership tree", () => {
500
501
  policy: "failFast",
501
502
  runs,
502
503
  });
503
- const harness = await openHarness([
504
- parent,
505
- failing,
506
- asAny(waitingSibling),
507
- asAny(pendingSibling),
508
- asAny(grandchild),
509
- ]);
504
+ // One task at a time keeps the last sibling unstarted (pending) when the failure lands.
505
+ const harness = await openHarness(
506
+ [parent, failing, asAny(waitingSibling), asAny(pendingSibling), asAny(grandchild)],
507
+ undefined,
508
+ 1
509
+ );
510
510
  await harness.start();
511
511
 
512
512
  const done = await harness.waitForTask((await harness.createTask(parent, {}))._id);
@@ -838,10 +838,8 @@ describe("Harness leases and crash recovery", () => {
838
838
  });
839
839
 
840
840
  describe("owner lease", () => {
841
- it("never lets two live harnesses run tasks at the same time", async () => {
841
+ it("lets only the owner of two live harnesses run tasks, each task once", async () => {
842
842
  const activeByTask = new Map<string, number>();
843
- let maxConcurrent = 0;
844
- let active = 0;
845
843
  const ranBy: string[] = [];
846
844
  const counted = defineTask<unknown, unknown, unknown>({
847
845
  initial: () => ({phase: "work"}),
@@ -850,12 +848,9 @@ describe("Harness leases and crash recovery", () => {
850
848
  work: {
851
849
  replay: "safe",
852
850
  run: async (task, rt) => {
853
- active += 1;
854
851
  activeByTask.set(task.id, (activeByTask.get(task.id) ?? 0) + 1);
855
- maxConcurrent = Math.max(maxConcurrent, active);
856
852
  ranBy.push(String((await TaskModel.findExactlyOne({_id: task.id})).lease?.owner));
857
853
  await pause(15);
858
- active -= 1;
859
854
  await rt.commit({terminal: {status: "completed"}});
860
855
  },
861
856
  },
@@ -882,7 +877,7 @@ describe("Harness leases and crash recovery", () => {
882
877
  created.map((task) => a.harness.waitForTask(task._id, {timeout: {seconds: 5}}))
883
878
  );
884
879
 
885
- expect(maxConcurrent).toBe(1);
880
+ // Only the owner runs work (concurrently, up to its limit); each task runs once.
886
881
  expect([...activeByTask.values()]).toEqual([1, 1, 1, 1, 1, 1]);
887
882
  expect(new Set(ranBy).size).toBe(1);
888
883
  expect([a.runner.role, b.runner.role].sort()).toEqual(["owner", "standby"]);
@@ -73,7 +73,7 @@ export const addHarnessApprovalRoutes = (
73
73
  method: "POST",
74
74
  permissions: [Permissions.IsAuthenticated, canApprove as PermissionMethod<unknown>],
75
75
  summary: "Approve a harness approval",
76
- tag: "harness",
76
+ tag: "harnessapprovals",
77
77
  },
78
78
  reject: {
79
79
  body: rejectBody,
@@ -82,7 +82,7 @@ export const addHarnessApprovalRoutes = (
82
82
  method: "POST",
83
83
  permissions: [Permissions.IsAuthenticated, canApprove as PermissionMethod<unknown>],
84
84
  summary: "Reject a harness approval",
85
- tag: "harness",
85
+ tag: "harnessapprovals",
86
86
  },
87
87
  },
88
88
  permissions: {
@@ -84,7 +84,7 @@ export const addHarnessConversationRoutes = (
84
84
  method: "POST",
85
85
  permissions: [Permissions.IsAuthenticated, ownerOnly as PermissionMethod<unknown>],
86
86
  summary: "Submit a message to a harness conversation",
87
- tag: "harness",
87
+ tag: "harnessconversations",
88
88
  },
89
89
  },
90
90
  permissions: {
@@ -102,6 +102,40 @@ describe("Harness task routes", () => {
102
102
  await owner.patch(`/harness/tasks/${task._id}`).send({name: "x"}).expect(405);
103
103
  });
104
104
 
105
+ it("omits the lease from the task read and tags mutations with the task collection", async () => {
106
+ const {app, harness, login, ownerId} = await setup();
107
+ const task = await harness.createTask(parked, {}, {userId: ownerId});
108
+ await TaskModel.updateOne(
109
+ {_id: task._id},
110
+ {
111
+ $set: {
112
+ lease: {
113
+ owner: "runner-1",
114
+ token: "fence-secret",
115
+ },
116
+ },
117
+ }
118
+ );
119
+ const owner = await login(OWNER);
120
+ const read = await owner.get(`/harness/tasks/${task._id}`).expect(200);
121
+ expect(read.body.data.lease).toBeUndefined();
122
+ expect(read.body.data.name).toBe("test.parked");
123
+ expect((await TaskModel.findExactlyOne({_id: task._id})).lease?.token).toBe("fence-secret");
124
+
125
+ const spec = await supertest(app).get("/openapi.json").expect(200);
126
+ const taskRead = spec.body.paths["/harness/tasks/{id}"].get;
127
+ const taskSchema = taskRead.responses["200"].content["application/json"].schema;
128
+ expect(taskSchema.properties.lease).toBeUndefined();
129
+ expect(taskRead.tags).toEqual(["harnesstasks"]);
130
+ expect(spec.body.paths["/harness/tasks/{id}/abort"].post.tags).toEqual(["harnesstasks"]);
131
+ expect(spec.body.paths["/harness/approvals/{id}/approve"].post.tags).toEqual([
132
+ "harnessapprovals",
133
+ ]);
134
+ expect(spec.body.paths["/harness/conversations/{id}/submit"].post.tags).toEqual([
135
+ "harnessconversations",
136
+ ]);
137
+ });
138
+
105
139
  it("abort: the owner or an admin aborts with a reason; others get 403; ended tasks 409", async () => {
106
140
  const {harness, login, ownerId} = await setup({start: true});
107
141
  const first = await harness.createTask(parked, {}, {userId: ownerId});
@@ -8,6 +8,9 @@ import {
8
8
  } from "@terreno/api";
9
9
  import type express from "express";
10
10
 
11
+ /** OpenAPI tag shared with the task read query, so mutations refresh that cache. */
12
+ const TASK_OPENAPI_TAG = "harnesstasks";
13
+
11
14
  import type {HarnessTaskDocument} from "../../types/harness";
12
15
  import {
13
16
  HARNESS_RESOLVE_ACTIONS,
@@ -48,6 +51,91 @@ const adminOnly: PermissionMethod<HarnessTaskDocument> = (_method, user, task) =
48
51
 
49
52
  const isCommitConflict = (error: unknown): boolean => error instanceof HarnessCommitConflictError;
50
53
 
54
+ const withoutLease = (value: unknown): Record<string, unknown> => {
55
+ const doc = value as {toObject?: () => Record<string, unknown>};
56
+ const json =
57
+ typeof doc.toObject === "function" ? doc.toObject() : {...(value as Record<string, unknown>)};
58
+ const {lease: _lease, ...rest} = json;
59
+ return rest;
60
+ };
61
+
62
+ /** HTTP task reads omit the lease. The fencing token is a runner secret, not a client field. */
63
+ const taskResponseHandler: NonNullable<
64
+ ModelRouterOptions<HarnessTaskDocument>["responseHandler"]
65
+ > = async (value) => {
66
+ if (Array.isArray(value)) {
67
+ return value.map((doc) => withoutLease(doc)) as never;
68
+ }
69
+ return withoutLease(value) as never;
70
+ };
71
+
72
+ const stripLeaseFromOpenApiSpec = (spec: unknown): unknown => {
73
+ if (!spec || typeof spec !== "object" || !("responses" in spec)) {
74
+ return spec;
75
+ }
76
+ const responses = (spec as {responses?: unknown}).responses;
77
+ if (!responses || typeof responses !== "object") {
78
+ return spec;
79
+ }
80
+ const nextResponses: Record<string, unknown> = {};
81
+ for (const [status, response] of Object.entries(responses as Record<string, unknown>)) {
82
+ const content =
83
+ response && typeof response === "object"
84
+ ? (response as {content?: Record<string, unknown>}).content
85
+ : undefined;
86
+ const json = content?.["application/json"] as {schema?: Record<string, unknown>} | undefined;
87
+ const properties = json?.schema?.properties as Record<string, unknown> | undefined;
88
+ if (!properties || !("lease" in properties)) {
89
+ nextResponses[status] = response;
90
+ continue;
91
+ }
92
+ const {lease: _lease, ...rest} = properties;
93
+ const required = json?.schema?.required;
94
+ nextResponses[status] = {
95
+ ...(response as Record<string, unknown>),
96
+ content: {
97
+ ...content,
98
+ "application/json": {
99
+ ...json,
100
+ schema: {
101
+ ...json?.schema,
102
+ properties: rest,
103
+ ...(Array.isArray(required)
104
+ ? {required: required.filter((field) => field !== "lease")}
105
+ : {}),
106
+ },
107
+ },
108
+ },
109
+ };
110
+ }
111
+ return {...(spec as Record<string, unknown>), responses: nextResponses};
112
+ };
113
+
114
+ /** Task OpenAPI schemas must not publish `lease`, including its fencing token. */
115
+ const openApiWithoutTaskLease = (openApi: unknown): unknown => {
116
+ // The registrar is a middleware function with a `path` property, not a plain object.
117
+ if (
118
+ !openApi ||
119
+ (typeof openApi !== "object" && typeof openApi !== "function") ||
120
+ !("path" in openApi)
121
+ ) {
122
+ return openApi;
123
+ }
124
+ const registrar = openApi as {path: (spec: unknown) => unknown};
125
+ return new Proxy(registrar, {
126
+ get(target, prop, receiver) {
127
+ if (prop === "path") {
128
+ return (spec: unknown): unknown => target.path(stripLeaseFromOpenApiSpec(spec));
129
+ }
130
+ const value = Reflect.get(target, prop, receiver);
131
+ if (typeof value === "function") {
132
+ return value.bind(target);
133
+ }
134
+ return value;
135
+ },
136
+ });
137
+ };
138
+
51
139
  /**
52
140
  * Mount `{basePath}/tasks`: read (owner or admin), plus the `abort` (owner or admin) and
53
141
  * `resolveInterrupted` (admin) instance actions. Tasks are not listed or edited over HTTP.
@@ -111,7 +199,13 @@ export const addHarnessTaskRoutes = (
111
199
  router.use(
112
200
  `${basePath}/tasks`,
113
201
  modelRouter(model, {
114
- ...(openApi ? {openApi: openApi as ModelRouterOptions<HarnessTaskDocument>["openApi"]} : {}),
202
+ ...(openApi
203
+ ? {
204
+ openApi: openApiWithoutTaskLease(
205
+ openApi
206
+ ) as ModelRouterOptions<HarnessTaskDocument>["openApi"],
207
+ }
208
+ : {}),
115
209
  instanceActions: {
116
210
  abort: {
117
211
  body: abortBody,
@@ -121,7 +215,7 @@ export const addHarnessTaskRoutes = (
121
215
  method: "POST",
122
216
  permissions: [Permissions.IsAuthenticated, ownerOrAdmin as PermissionMethod<unknown>],
123
217
  summary: "Abort a harness task",
124
- tag: "harness",
218
+ tag: TASK_OPENAPI_TAG,
125
219
  },
126
220
  resolveInterrupted: {
127
221
  body: resolveBody,
@@ -131,7 +225,7 @@ export const addHarnessTaskRoutes = (
131
225
  method: "POST",
132
226
  permissions: [Permissions.IsAuthenticated, adminOnly as PermissionMethod<unknown>],
133
227
  summary: "Resolve an interrupted harness task",
134
- tag: "harness",
228
+ tag: TASK_OPENAPI_TAG,
135
229
  },
136
230
  },
137
231
  permissions: {
@@ -141,6 +235,7 @@ export const addHarnessTaskRoutes = (
141
235
  read: [Permissions.IsAuthenticated, ownerOrAdmin],
142
236
  update: [],
143
237
  },
238
+ responseHandler: taskResponseHandler,
144
239
  })
145
240
  );
146
241
  };