@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
package/dist/aiApp.d.ts CHANGED
@@ -34,6 +34,11 @@ export interface AiAppOptions {
34
34
  mcpService?: MCPService;
35
35
  /** OpenAPI options passed through to route builders for spec generation. */
36
36
  openApiOptions?: Record<string, unknown>;
37
+ /**
38
+ * Mount the `/gpt/projects` routes, which register the `Project` model. Defaults to `true`;
39
+ * pass `false` when the app owns a model named `Project`.
40
+ */
41
+ projects?: boolean;
37
42
  /** Tool choice strategy for chat requests. Defaults to "auto" when tools are present. */
38
43
  toolChoice?: "auto" | "none" | "required";
39
44
  /** Cheap model ID used for generating conversation titles (e.g. "gemini-3.5-flash-lite"). Falls back to the main model if not set. */
package/dist/aiApp.js CHANGED
@@ -40,7 +40,7 @@ class AiApp {
40
40
  }
41
41
  register(app) {
42
42
  const router = app;
43
- const { aiService, asks, createModelFn, createServerModelFn, demoMode, fileStorageService, fileUploadsEnabled, gcsBucket, maxSteps, mcpService, openApiOptions, titleModelId, toolChoice, tools, } = this.options;
43
+ const { aiService, asks, createModelFn, createServerModelFn, demoMode, fileStorageService, fileUploadsEnabled, gcsBucket, maxSteps, mcpService, openApiOptions, projects, titleModelId, toolChoice, tools, } = this.options;
44
44
  const hasFileRoutes = Boolean(fileStorageService && gcsBucket);
45
45
  const chat = {
46
46
  aiService,
@@ -61,7 +61,9 @@ class AiApp {
61
61
  (0, gptHistories_1.addGptHistoryRoutes)(router, { chat, openApiOptions });
62
62
  (0, gpt_1.addGptRoutes)(router, chat);
63
63
  (0, aiRequestsExplorer_1.addAiRequestsExplorerRoutes)(router, { openApiOptions });
64
- (0, projects_1.addProjectRoutes)(router, { openApiOptions });
64
+ if (projects !== false) {
65
+ (0, projects_1.addProjectRoutes)(router, { openApiOptions });
66
+ }
65
67
  if (fileStorageService && gcsBucket) {
66
68
  (0, files_1.addFileRoutes)(router, {
67
69
  fileStorageService,
@@ -1,7 +1,7 @@
1
1
  import { APIError } from "@terreno/api";
2
2
  import { DateTime } from "luxon";
3
3
  import mongoose, { type ClientSession } from "mongoose";
4
- import type { HarnessApprovalModel, HarnessChildTaskOptions, HarnessCommit, HarnessConversationModel, HarnessCreateTaskOptions, HarnessEventModel, HarnessEventStreamModel, HarnessInboxEventModel, HarnessLeaseSettings, HarnessMemoModel, HarnessMessageModel, HarnessOwnerModel, HarnessOwnership, HarnessResolveInterruptedOptions, HarnessTaskDefinition, HarnessTaskDocument, HarnessTaskModel, HarnessTaskSpanKind, HarnessTestHooks, HarnessWaitCall, HarnessWaiting, HarnessWaitKind } from "../types/harness";
4
+ import type { HarnessApprovalModel, HarnessChildTaskOptions, HarnessCommit, HarnessConversationModel, HarnessCreateTaskOptions, HarnessEventModel, HarnessEventStreamModel, HarnessInboxEventModel, HarnessLeaseSettings, HarnessMemoModel, HarnessMessageModel, HarnessOwnerModel, HarnessOwnership, HarnessPromptRef, HarnessResolveInterruptedOptions, HarnessTaskDefinition, HarnessTaskDocument, HarnessTaskModel, HarnessTaskSpanKind, HarnessTestHooks, HarnessWaitCall, HarnessWaiting, HarnessWaitKind } from "../types/harness";
5
5
  import type { ObsSpanModel, ObsTraceModel } from "../types/observability";
6
6
  export interface HarnessModels {
7
7
  approval: HarnessApprovalModel;
@@ -41,6 +41,22 @@ export declare const newTaskLease: (lease: HarnessLeaseSettings) => {
41
41
  owner: string;
42
42
  token: string;
43
43
  };
44
+ /**
45
+ * Prompt refs as stored on `ObsTrace.prompts` (`{label?, name, version}`; a registry
46
+ * `body` is dropped), once each. Throws `invalidRequest` for a ref without a name, with a
47
+ * non-string label, or without a positive integer version.
48
+ */
49
+ export declare const tracePromptRefs: (refs: HarnessPromptRef[] | undefined) => HarnessPromptRef[];
50
+ /**
51
+ * Record `refs` on `traceId`'s `ObsTrace.prompts`, once each. Pass `session` to write
52
+ * inside a commit's transaction.
53
+ */
54
+ export declare const addTracePrompts: ({ models, refs, session, traceId, }: {
55
+ models: HarnessModels;
56
+ refs: HarnessPromptRef[];
57
+ session?: ClientSession;
58
+ traceId: mongoose.Types.ObjectId;
59
+ }) => Promise<void>;
44
60
  /**
45
61
  * Insert a root task with its `ObsTrace` and root `CHAIN` span in one transaction.
46
62
  * A repeated `requestId` hits the unique index, rolls the whole transaction back (so no
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.commitResolution = exports.commitInterruption = exports.expiredLeaseFilter = exports.commitAbort = exports.claimAbortHandler = exports.fenceForAbort = exports.wakeWaitingTask = exports.commitWaitResolution = exports.commitWaiting = exports.commitRetry = exports.commitPhase = exports.createChildTaskRecords = exports.createTaskRecords = exports.newTaskLease = exports.isDuplicateKeyError = exports.HarnessCommitConflictError = void 0;
6
+ exports.commitResolution = exports.commitInterruption = exports.expiredLeaseFilter = exports.commitAbort = exports.claimAbortHandler = exports.fenceForAbort = exports.wakeWaitingTask = exports.commitWaitResolution = exports.commitWaiting = exports.commitRetry = exports.commitPhase = exports.createChildTaskRecords = exports.createTaskRecords = exports.addTracePrompts = exports.tracePromptRefs = exports.newTaskLease = exports.isDuplicateKeyError = exports.HarnessCommitConflictError = void 0;
7
7
  const node_crypto_1 = require("node:crypto");
8
8
  const api_1 = require("@terreno/api");
9
9
  const luxon_1 = require("luxon");
@@ -75,6 +75,77 @@ const findByRequestId = async ({ definition, models, requestId, userId, }) => {
75
75
  }
76
76
  return existing;
77
77
  };
78
+ /**
79
+ * Prompt refs as stored on `ObsTrace.prompts` (`{label?, name, version}`; a registry
80
+ * `body` is dropped), once each. Throws `invalidRequest` for a ref without a name, with a
81
+ * non-string label, or without a positive integer version.
82
+ */
83
+ const tracePromptRefs = (refs) => {
84
+ if (refs === undefined) {
85
+ return [];
86
+ }
87
+ if (!Array.isArray(refs)) {
88
+ throw (0, errors_1.harnessError)({ detail: "prompts must be an array", kind: "invalidRequest" });
89
+ }
90
+ const unique = new Map();
91
+ for (const ref of refs) {
92
+ if (typeof ref?.name !== "string" || !ref.name.trim()) {
93
+ throw (0, errors_1.harnessError)({ detail: "Each prompt ref needs a name", kind: "invalidRequest" });
94
+ }
95
+ if (!Number.isInteger(ref.version) || ref.version < 1) {
96
+ throw (0, errors_1.harnessError)({
97
+ detail: `Prompt ref "${ref.name}" needs a positive integer version`,
98
+ kind: "invalidRequest",
99
+ });
100
+ }
101
+ if (ref.label !== undefined && typeof ref.label !== "string") {
102
+ throw (0, errors_1.harnessError)({
103
+ detail: `Prompt ref "${ref.name}" label must be a string`,
104
+ kind: "invalidRequest",
105
+ });
106
+ }
107
+ const normalized = {
108
+ ...(ref.label !== undefined ? { label: ref.label } : {}),
109
+ name: ref.name,
110
+ version: ref.version,
111
+ };
112
+ unique.set(JSON.stringify(normalized), normalized);
113
+ }
114
+ return [...unique.values()];
115
+ };
116
+ exports.tracePromptRefs = tracePromptRefs;
117
+ /** The `scope` and `tags` written onto a root task's `ObsTrace`. */
118
+ const traceFields = (trace) => {
119
+ if (trace === undefined) {
120
+ return {};
121
+ }
122
+ const { scope, tags } = trace;
123
+ if (scope !== undefined && (typeof scope !== "string" || !scope.trim())) {
124
+ throw (0, errors_1.harnessError)({ detail: "trace.scope must be a non-empty string", kind: "invalidRequest" });
125
+ }
126
+ const isBlankTag = (tag) => typeof tag !== "string" || !tag.trim();
127
+ if (tags !== undefined && (!Array.isArray(tags) || tags.some(isBlankTag))) {
128
+ throw (0, errors_1.harnessError)({
129
+ detail: "trace.tags must be an array of non-empty strings",
130
+ kind: "invalidRequest",
131
+ });
132
+ }
133
+ return {
134
+ ...(scope !== undefined ? { scope } : {}),
135
+ ...(tags !== undefined ? { tags: [...new Set(tags)] } : {}),
136
+ };
137
+ };
138
+ /**
139
+ * Record `refs` on `traceId`'s `ObsTrace.prompts`, once each. Pass `session` to write
140
+ * inside a commit's transaction.
141
+ */
142
+ const addTracePrompts = async ({ models, refs, session, traceId, }) => {
143
+ if (refs.length === 0) {
144
+ return;
145
+ }
146
+ await models.trace.updateOne({ _id: traceId }, { $addToSet: { prompts: { $each: refs } } }, session ? { session } : {});
147
+ };
148
+ exports.addTracePrompts = addTracePrompts;
78
149
  /**
79
150
  * Insert a root task with its `ObsTrace` and root `CHAIN` span in one transaction.
80
151
  * A repeated `requestId` hits the unique index, rolls the whole transaction back (so no
@@ -92,9 +163,22 @@ const createTaskRecords = async ({ definition, input, models, options, ownership
92
163
  const startedAt = traceStartedAt.toJSDate();
93
164
  const userId = toObjectId(options.userId);
94
165
  const spanIdentity = taskSpanIdentity(definition, input);
166
+ const prompts = (0, exports.tracePromptRefs)(options.prompts);
167
+ const traceExtras = traceFields(options.trace);
95
168
  try {
96
169
  return await (0, transaction_1.inTransaction)(async (session) => {
97
- await models.trace.create([{ _id: traceId, input, name: definition.key, startedAt, status: "ok", userId }], { session });
170
+ await models.trace.create([
171
+ {
172
+ _id: traceId,
173
+ input,
174
+ name: definition.key,
175
+ prompts,
176
+ startedAt,
177
+ status: "ok",
178
+ userId,
179
+ ...traceExtras,
180
+ },
181
+ ], { session });
98
182
  await models.span.create([
99
183
  {
100
184
  _id: rootSpanId,
@@ -87,6 +87,12 @@ export declare const HARNESS_ERRORS: {
87
87
  readonly status: 409;
88
88
  readonly title: "requestId is already in use";
89
89
  };
90
+ /** A `JobsRunner` phase job ran on an instance whose harness is not started. */
91
+ readonly runnerStopped: {
92
+ readonly code: "harness-runner-stopped";
93
+ readonly status: 503;
94
+ readonly title: "Harness runner is not running";
95
+ };
90
96
  /** `retry` on a task whose abort is still running. */
91
97
  readonly taskAborting: {
92
98
  readonly code: "harness-task-aborting";
@@ -65,6 +65,12 @@ exports.HARNESS_ERRORS = {
65
65
  status: 409,
66
66
  title: "requestId is already in use",
67
67
  },
68
+ /** A `JobsRunner` phase job ran on an instance whose harness is not started. */
69
+ runnerStopped: {
70
+ code: "harness-runner-stopped",
71
+ status: 503,
72
+ title: "Harness runner is not running",
73
+ },
68
74
  /** `retry` on a task whose abort is still running. */
69
75
  taskAborting: { code: "harness-task-aborting", status: 409, title: "Task is being aborted" },
70
76
  /** `resolveInterrupted` on a task that is not interrupted. */
@@ -6,7 +6,7 @@ import type { HarnessAbortOptions, HarnessAgentDefinition, HarnessApprovalDocume
6
6
  import { HarnessConversationHandle } from "./conversation";
7
7
  import type { ExecutionEnv } from "./executionEnv";
8
8
  import { type HarnessRegistryEntry } from "./registry";
9
- export type { AnyHarnessToolDefinition, HarnessAbortOptions, HarnessAbortRuntime, HarnessAgentDefinition, HarnessAgentDefinitionInput, HarnessApprovalDocument, HarnessApprovalOptions, HarnessApprovalPolicy, HarnessApprovalRequest, HarnessApprovalResult, HarnessApprovalStatus, HarnessApprover, HarnessBeforeToolResult, HarnessChildOutcome, HarnessChildTaskOptions, HarnessCommit, HarnessConversationAgent, HarnessConversationDocument, HarnessConversationStatus, HarnessCreateTaskOptions, HarnessDecideApprovalOptions, HarnessEventDocument, HarnessEventType, HarnessExtensionDefinition, HarnessExtensionDefinitionInput, HarnessHook, HarnessHookApi, HarnessHookHandlers, HarnessHookKind, HarnessInboxEventDocument, HarnessInterruptAction, HarnessLeaseSettings, HarnessMemo, HarnessMemoDocument, HarnessMessageDocument, HarnessMessagePart, HarnessMessageRole, HarnessModelRef, HarnessModelRequest, HarnessModelResolver, HarnessOutcome, HarnessPhaseCommit, HarnessPhaseDefinition, HarnessReplayPolicy, HarnessResolveAction, HarnessResolveInterruptedOptions, HarnessRetryPolicy, HarnessRunAgentOptions, HarnessRunner, HarnessRunnerContext, HarnessSection, HarnessSectionInput, HarnessSendEventOptions, HarnessSendOptions, HarnessStreamingOptions, HarnessSubmitDisposition, HarnessSubmitOptions, HarnessSubmitResult, HarnessSystemPromptPart, HarnessTaskDefinition, HarnessTaskDefinitionInput, HarnessTaskDocument, HarnessTaskRuntime, HarnessTaskSpanKind, HarnessTaskStatus, HarnessTaskView, HarnessTerminalCommit, HarnessTestHooks, HarnessTextPart, HarnessToolApi, HarnessToolCall, HarnessToolCallPart, HarnessToolDefinition, HarnessToolDefinitionInput, HarnessToolHookApi, HarnessToolResultPart, HarnessToolWrap, HarnessTurnResult, HarnessWaitCall, HarnessWaitForOptions, HarnessWaitForTasksOptions, HarnessWaiting, HarnessWaitKind, HarnessWaitPolicy, HarnessWaitResolution, HarnessWhenBusy, } from "../types/harness";
9
+ export type { AnyHarnessToolDefinition, HarnessAbortOptions, HarnessAbortRuntime, HarnessAgentDefinition, HarnessAgentDefinitionInput, HarnessApprovalDocument, HarnessApprovalOptions, HarnessApprovalPolicy, HarnessApprovalRequest, HarnessApprovalResult, HarnessApprovalStatus, HarnessApprover, HarnessBeforeToolResult, HarnessChildOutcome, HarnessChildTaskOptions, HarnessCommit, HarnessConversationAgent, HarnessConversationDocument, HarnessConversationStatus, HarnessCreateTaskOptions, HarnessDecideApprovalOptions, HarnessEventDocument, HarnessEventType, HarnessExtensionDefinition, HarnessExtensionDefinitionInput, HarnessHook, HarnessHookApi, HarnessHookHandlers, HarnessHookKind, HarnessInboxEventDocument, HarnessInterruptAction, HarnessLeaseSettings, HarnessMemo, HarnessMemoDocument, HarnessMessageDocument, HarnessMessagePart, HarnessMessageRole, HarnessModelRef, HarnessModelRequest, HarnessModelResolver, HarnessOutcome, HarnessPhaseCommit, HarnessPhaseDefinition, HarnessReplayPolicy, HarnessResolveAction, HarnessResolveInterruptedOptions, HarnessRetryPolicy, HarnessRunAgentOptions, HarnessRunnableTask, HarnessRunner, HarnessRunnerContext, HarnessRunTaskOptions, HarnessSection, HarnessSectionInput, HarnessSendEventOptions, HarnessSendOptions, HarnessStreamingOptions, HarnessSubmitDisposition, HarnessSubmitOptions, HarnessSubmitResult, HarnessSystemPromptPart, HarnessTaskDefinition, HarnessTaskDefinitionInput, HarnessTaskDocument, HarnessTaskRuntime, HarnessTaskSpanKind, HarnessTaskStatus, HarnessTaskView, HarnessTerminalCommit, HarnessTestHooks, HarnessTextPart, HarnessToolApi, HarnessToolCall, HarnessToolCallPart, HarnessToolDefinition, HarnessToolDefinitionInput, HarnessToolHookApi, HarnessToolResultPart, HarnessToolWrap, HarnessTurnResult, HarnessWaitCall, HarnessWaitForOptions, HarnessWaitForTasksOptions, HarnessWaiting, HarnessWaitKind, HarnessWaitPolicy, HarnessWaitResolution, HarnessWhenBusy, } from "../types/harness";
10
10
  export { HARNESS_AGENT_DEFAULT_MAX_STEPS, HARNESS_APPROVAL_STATUSES, HARNESS_CONVERSATION_STATUSES, HARNESS_EVENT_TYPES, HARNESS_HOOK_KINDS, HARNESS_INTERRUPT_ACTIONS, HARNESS_MESSAGE_ROLES, HARNESS_MODEL_RETRY_DEFAULTS, HARNESS_RESOLVE_ACTIONS, HARNESS_RETRY_DEFAULTS, HARNESS_SUBMIT_DISPOSITIONS, HARNESS_TASK_STATUSES, HARNESS_WAIT_KINDS, HARNESS_WAIT_POLICIES, HARNESS_WAIT_RESOLUTIONS, HARNESS_WHEN_BUSY, } from "../types/harness";
11
11
  export { AI_HARNESS_GROUP, HARNESS_APPROVALS_SCREEN, harnessAdminScreens } from "./adminScreens";
12
12
  export { AGENT_TOOL_TASK_NAME, AGENT_TURN_TASK_NAME } from "./agentTaskNames";
@@ -89,7 +89,7 @@ export declare class Harness {
89
89
  * process died between a turn's end and starting the next queued turn) and claim again.
90
90
  */
91
91
  private claimNextOrSweep;
92
- /** Stop claiming work and wait for the phase in flight to settle. */
92
+ /** Stop claiming work and wait for every phase in flight to settle. */
93
93
  stop(): Promise<void>;
94
94
  /**
95
95
  * Create a root task pinned to `definition`'s `name@version`. With `requestId`, a
@@ -234,9 +234,11 @@ class Harness {
234
234
  await this.runner.start({
235
235
  acquireOwnerLease: (lease) => (0, leases_1.acquireOwnerLease)({ lease, models, testHooks }),
236
236
  claimNext: (lease) => this.claimNextOrSweep(lease),
237
+ claimTask: (taskId, lease) => (0, runtime_1.claimTaskById)({ definitions, lease, models, taskId }),
238
+ listRunnable: (limit) => (0, runtime_1.listRunnableTasks)({ definitions, limit, models }),
237
239
  recoverExpired: () => (0, runtime_1.recoverExpiredTasks)(engine),
238
240
  releaseOwnerLease: (lease) => (0, leases_1.releaseOwnerLease)({ lease, models }),
239
- runTask: (task, lease) => (0, runtime_1.runClaimedTask)({ engine, lease, task }),
241
+ runTask: (task, lease, options) => (0, runtime_1.runClaimedTask)({ engine, lease, maxPhases: options?.maxPhases, task }),
240
242
  });
241
243
  }
242
244
  /**
@@ -256,7 +258,7 @@ class Harness {
256
258
  }
257
259
  return (0, runtime_1.claimNextTask)({ definitions, lease, models });
258
260
  }
259
- /** Stop claiming work and wait for the phase in flight to settle. */
261
+ /** Stop claiming work and wait for every phase in flight to settle. */
260
262
  async stop() {
261
263
  if (!this.isStarted) {
262
264
  return;
@@ -37,6 +37,11 @@ const harnessTaskSchema = new mongoose_1.default.Schema({
37
37
  description: "When true, the task outlives its owning conversation turn",
38
38
  type: Boolean,
39
39
  },
40
+ claims: {
41
+ default: 0,
42
+ description: "Times a runner claimed the task; numbers each runnable visit for job dispatch",
43
+ type: Number,
44
+ },
40
45
  eventSeq: {
41
46
  default: 0,
42
47
  description: "Events received by harness.sendEvent; numbers the task's inbox",
@@ -43,7 +43,7 @@ const addHarnessApprovalRoutes = (router, { basePath, harness, openApi }) => {
43
43
  method: "POST",
44
44
  permissions: [api_1.Permissions.IsAuthenticated, canApprove],
45
45
  summary: "Approve a harness approval",
46
- tag: "harness",
46
+ tag: "harnessapprovals",
47
47
  },
48
48
  reject: {
49
49
  body: rejectBody,
@@ -52,7 +52,7 @@ const addHarnessApprovalRoutes = (router, { basePath, harness, openApi }) => {
52
52
  method: "POST",
53
53
  permissions: [api_1.Permissions.IsAuthenticated, canApprove],
54
54
  summary: "Reject a harness approval",
55
- tag: "harness",
55
+ tag: "harnessapprovals",
56
56
  },
57
57
  },
58
58
  permissions: {
@@ -48,7 +48,7 @@ const addHarnessConversationRoutes = (router, { basePath, harness, openApi }) =>
48
48
  method: "POST",
49
49
  permissions: [api_1.Permissions.IsAuthenticated, ownerOnly],
50
50
  summary: "Submit a message to a harness conversation",
51
- tag: "harness",
51
+ tag: "harnessconversations",
52
52
  },
53
53
  },
54
54
  permissions: {
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.addHarnessTaskRoutes = void 0;
4
4
  const api_1 = require("@terreno/api");
5
+ /** OpenAPI tag shared with the task read query, so mutations refresh that cache. */
6
+ const TASK_OPENAPI_TAG = "harnesstasks";
5
7
  const harness_1 = require("../../types/harness");
6
8
  const commit_1 = require("../commit");
7
9
  const errors_1 = require("../errors");
@@ -28,6 +30,81 @@ const ownerOrAdmin = (_method, user, task) => task ? (0, events_1.isOwnerOrAdmin
28
30
  // Checked against the document so a non-admin gets 403, not the pre-document 405.
29
31
  const adminOnly = (_method, user, task) => task ? Boolean(user?.admin) : true;
30
32
  const isCommitConflict = (error) => error instanceof commit_1.HarnessCommitConflictError;
33
+ const withoutLease = (value) => {
34
+ const doc = value;
35
+ const json = typeof doc.toObject === "function" ? doc.toObject() : { ...value };
36
+ const { lease: _lease, ...rest } = json;
37
+ return rest;
38
+ };
39
+ /** HTTP task reads omit the lease. The fencing token is a runner secret, not a client field. */
40
+ const taskResponseHandler = async (value) => {
41
+ if (Array.isArray(value)) {
42
+ return value.map((doc) => withoutLease(doc));
43
+ }
44
+ return withoutLease(value);
45
+ };
46
+ const stripLeaseFromOpenApiSpec = (spec) => {
47
+ if (!spec || typeof spec !== "object" || !("responses" in spec)) {
48
+ return spec;
49
+ }
50
+ const responses = spec.responses;
51
+ if (!responses || typeof responses !== "object") {
52
+ return spec;
53
+ }
54
+ const nextResponses = {};
55
+ for (const [status, response] of Object.entries(responses)) {
56
+ const content = response && typeof response === "object"
57
+ ? response.content
58
+ : undefined;
59
+ const json = content?.["application/json"];
60
+ const properties = json?.schema?.properties;
61
+ if (!properties || !("lease" in properties)) {
62
+ nextResponses[status] = response;
63
+ continue;
64
+ }
65
+ const { lease: _lease, ...rest } = properties;
66
+ const required = json?.schema?.required;
67
+ nextResponses[status] = {
68
+ ...response,
69
+ content: {
70
+ ...content,
71
+ "application/json": {
72
+ ...json,
73
+ schema: {
74
+ ...json?.schema,
75
+ properties: rest,
76
+ ...(Array.isArray(required)
77
+ ? { required: required.filter((field) => field !== "lease") }
78
+ : {}),
79
+ },
80
+ },
81
+ },
82
+ };
83
+ }
84
+ return { ...spec, responses: nextResponses };
85
+ };
86
+ /** Task OpenAPI schemas must not publish `lease`, including its fencing token. */
87
+ const openApiWithoutTaskLease = (openApi) => {
88
+ // The registrar is a middleware function with a `path` property, not a plain object.
89
+ if (!openApi ||
90
+ (typeof openApi !== "object" && typeof openApi !== "function") ||
91
+ !("path" in openApi)) {
92
+ return openApi;
93
+ }
94
+ const registrar = openApi;
95
+ return new Proxy(registrar, {
96
+ get(target, prop, receiver) {
97
+ if (prop === "path") {
98
+ return (spec) => target.path(stripLeaseFromOpenApiSpec(spec));
99
+ }
100
+ const value = Reflect.get(target, prop, receiver);
101
+ if (typeof value === "function") {
102
+ return value.bind(target);
103
+ }
104
+ return value;
105
+ },
106
+ });
107
+ };
31
108
  /**
32
109
  * Mount `{basePath}/tasks`: read (owner or admin), plus the `abort` (owner or admin) and
33
110
  * `resolveInterrupted` (admin) instance actions. Tasks are not listed or edited over HTTP.
@@ -75,7 +152,11 @@ const addHarnessTaskRoutes = (router, { basePath, harness, openApi }) => {
75
152
  }
76
153
  };
77
154
  router.use(`${basePath}/tasks`, (0, api_1.modelRouter)(model, {
78
- ...(openApi ? { openApi: openApi } : {}),
155
+ ...(openApi
156
+ ? {
157
+ openApi: openApiWithoutTaskLease(openApi),
158
+ }
159
+ : {}),
79
160
  instanceActions: {
80
161
  abort: {
81
162
  body: abortBody,
@@ -84,7 +165,7 @@ const addHarnessTaskRoutes = (router, { basePath, harness, openApi }) => {
84
165
  method: "POST",
85
166
  permissions: [api_1.Permissions.IsAuthenticated, ownerOrAdmin],
86
167
  summary: "Abort a harness task",
87
- tag: "harness",
168
+ tag: TASK_OPENAPI_TAG,
88
169
  },
89
170
  resolveInterrupted: {
90
171
  body: resolveBody,
@@ -93,7 +174,7 @@ const addHarnessTaskRoutes = (router, { basePath, harness, openApi }) => {
93
174
  method: "POST",
94
175
  permissions: [api_1.Permissions.IsAuthenticated, adminOnly],
95
176
  summary: "Resolve an interrupted harness task",
96
- tag: "harness",
177
+ tag: TASK_OPENAPI_TAG,
97
178
  },
98
179
  },
99
180
  permissions: {
@@ -103,6 +184,7 @@ const addHarnessTaskRoutes = (router, { basePath, harness, openApi }) => {
103
184
  read: [api_1.Permissions.IsAuthenticated, ownerOrAdmin],
104
185
  update: [],
105
186
  },
187
+ responseHandler: taskResponseHandler,
106
188
  }));
107
189
  };
108
190
  exports.addHarnessTaskRoutes = addHarnessTaskRoutes;
@@ -1,6 +1,8 @@
1
1
  import { type DurationLike } from "luxon";
2
2
  import type { HarnessRunner, HarnessRunnerContext } from "../../types/harness";
3
3
  export interface InProcessRunnerOptions {
4
+ /** Most claimed tasks this runner executes at once. Default 8. */
5
+ concurrency?: number;
4
6
  /** How often the runner renews its owner lease and its running task's lease. */
5
7
  heartbeatInterval?: DurationLike;
6
8
  /** How long the owner lease and each task lease live without a heartbeat. */
@@ -18,16 +20,19 @@ export declare const IN_PROCESS_RUNNER_ROLES: {
18
20
  };
19
21
  export type InProcessRunnerRole = (typeof IN_PROCESS_RUNNER_ROLES)[keyof typeof IN_PROCESS_RUNNER_ROLES];
20
22
  /**
21
- * Executes runnable tasks inside this process, one at a time, until each is terminal.
23
+ * Executes runnable tasks inside this process, up to `concurrency` at a time, each until it
24
+ * stops (terminal, retry, waiting, or aborted).
22
25
  * Only the process holding the singleton `HarnessOwner` lease drains; every other
23
26
  * process stays on standby and takes over once that lease expires. On becoming owner,
24
27
  * and on every heartbeat while owner, it recovers tasks whose lease expired.
25
28
  */
26
29
  export declare class InProcessRunner implements HarnessRunner {
30
+ private readonly concurrency;
27
31
  private context;
28
32
  private heartbeatInFlight;
29
33
  private heartbeatTimer;
30
34
  private hasPendingWake;
35
+ private readonly inFlight;
31
36
  private isHeartbeatStopped;
32
37
  private isStopping;
33
38
  private readonly lease;
@@ -41,15 +46,17 @@ export declare class InProcessRunner implements HarnessRunner {
41
46
  /** Id written into every lease this runner holds. */
42
47
  get ownerId(): string;
43
48
  start(context: HarnessRunnerContext): Promise<void>;
44
- /** Stop claiming new work, wait for the task in flight, then release the owner lease. */
49
+ /** Stop claiming new work, wait for every task in flight, then release the owner lease. */
45
50
  stop(): Promise<void>;
46
51
  /** Skip the rest of the idle sleep, e.g. right after a task is created. */
47
52
  wake(): void;
48
53
  private drain;
49
54
  /** Become owner when the lease is free or expired, then recover abandoned tasks. */
50
55
  private tryTakeOwnership;
51
- /** Run one claimed task; returns true when nothing was runnable. */
52
- private runNext;
56
+ /** Claim and start tasks until every slot is busy or nothing is runnable. */
57
+ private fillSlots;
58
+ /** Run one claimed task to a stop; its own task lease and heartbeat guard its commits. */
59
+ private runOne;
53
60
  private scheduleHeartbeat;
54
61
  /** Renew the owner lease and sweep for expired task leases; demote on a lost lease. */
55
62
  private heartbeat;
@@ -13,16 +13,19 @@ exports.IN_PROCESS_RUNNER_ROLES = {
13
13
  stopped: "stopped",
14
14
  };
15
15
  /**
16
- * Executes runnable tasks inside this process, one at a time, until each is terminal.
16
+ * Executes runnable tasks inside this process, up to `concurrency` at a time, each until it
17
+ * stops (terminal, retry, waiting, or aborted).
17
18
  * Only the process holding the singleton `HarnessOwner` lease drains; every other
18
19
  * process stays on standby and takes over once that lease expires. On becoming owner,
19
20
  * and on every heartbeat while owner, it recovers tasks whose lease expired.
20
21
  */
21
22
  class InProcessRunner {
23
+ concurrency;
22
24
  context;
23
25
  heartbeatInFlight = Promise.resolve();
24
26
  heartbeatTimer;
25
27
  hasPendingWake = false;
28
+ inFlight = new Set();
26
29
  isHeartbeatStopped = false;
27
30
  isStopping = false;
28
31
  lease;
@@ -39,6 +42,14 @@ class InProcessRunner {
39
42
  kind: "configInvalid",
40
43
  });
41
44
  }
45
+ const concurrency = options.concurrency ?? 8;
46
+ if (!Number.isInteger(concurrency) || concurrency < 1) {
47
+ throw (0, errors_1.harnessError)({
48
+ detail: "InProcessRunner concurrency must be a positive integer",
49
+ kind: "configInvalid",
50
+ });
51
+ }
52
+ this.concurrency = concurrency;
42
53
  this.lease = {
43
54
  duration,
44
55
  heartbeat,
@@ -65,14 +76,14 @@ class InProcessRunner {
65
76
  this.loop = this.drain();
66
77
  this.scheduleHeartbeat();
67
78
  }
68
- /** Stop claiming new work, wait for the task in flight, then release the owner lease. */
79
+ /** Stop claiming new work, wait for every task in flight, then release the owner lease. */
69
80
  async stop() {
70
81
  if (this.loop === undefined) {
71
82
  return;
72
83
  }
73
84
  this.isStopping = true;
74
85
  this.wake();
75
- // Keep renewing the owner lease until the task in flight settles, so a standby does
86
+ // Keep renewing the owner lease until the tasks in flight settle, so a standby does
76
87
  // not take over while this process is still working.
77
88
  await this.loop;
78
89
  this.isHeartbeatStopped = true;
@@ -109,11 +120,13 @@ class InProcessRunner {
109
120
  continue;
110
121
  }
111
122
  }
112
- const isIdle = await this.runNext(this.context);
113
- if (isIdle && !this.isStopping) {
123
+ await this.fillSlots(this.context);
124
+ if (!this.isStopping) {
125
+ // Woken early by new work or by a task that frees a slot.
114
126
  await this.sleep(this.pollIntervalMs);
115
127
  }
116
128
  }
129
+ await Promise.allSettled([...this.inFlight]);
117
130
  }
118
131
  /** Become owner when the lease is free or expired, then recover abandoned tasks. */
119
132
  async tryTakeOwnership(context) {
@@ -131,19 +144,37 @@ class InProcessRunner {
131
144
  return this.currentRole === exports.IN_PROCESS_RUNNER_ROLES.owner;
132
145
  }
133
146
  }
134
- /** Run one claimed task; returns true when nothing was runnable. */
135
- async runNext(context) {
136
- try {
137
- const task = await context.claimNext(this.lease);
147
+ /** Claim and start tasks until every slot is busy or nothing is runnable. */
148
+ async fillSlots(context) {
149
+ while (this.inFlight.size < this.concurrency &&
150
+ !this.isStopping &&
151
+ this.currentRole === exports.IN_PROCESS_RUNNER_ROLES.owner) {
152
+ let task;
153
+ try {
154
+ task = await context.claimNext(this.lease);
155
+ }
156
+ catch (error) {
157
+ api_1.logger.error(`InProcessRunner iteration failed: ${(0, errors_1.errorMessage)(error)}`);
158
+ return;
159
+ }
138
160
  if (!task) {
139
- return true;
161
+ return;
140
162
  }
163
+ const run = this.runOne(context, task);
164
+ this.inFlight.add(run);
165
+ void run.finally(() => {
166
+ this.inFlight.delete(run);
167
+ this.wake();
168
+ });
169
+ }
170
+ }
171
+ /** Run one claimed task to a stop; its own task lease and heartbeat guard its commits. */
172
+ async runOne(context, task) {
173
+ try {
141
174
  await context.runTask(task, this.lease);
142
- return false;
143
175
  }
144
176
  catch (error) {
145
- api_1.logger.error(`InProcessRunner iteration failed: ${(0, errors_1.errorMessage)(error)}`);
146
- return true;
177
+ api_1.logger.error(`InProcessRunner task ${task._id} failed to run: ${(0, errors_1.errorMessage)(error)}`);
147
178
  }
148
179
  }
149
180
  scheduleHeartbeat() {
@@ -0,0 +1,62 @@
1
+ import { type JobsApp } from "@terreno/jobs";
2
+ import { type DurationLike } from "luxon";
3
+ import type { HarnessRunner, HarnessRunnerContext } from "../../types/harness";
4
+ /** Job that runs one phase of one harness task. */
5
+ export declare const HARNESS_PHASE_JOB_NAME = "terreno.harness.phase";
6
+ export interface JobsRunnerOptions {
7
+ /** How many runnable tasks one dispatch tick enqueues at most. */
8
+ dispatchBatchSize?: number;
9
+ /** How often a running phase renews its task lease, and how often expired leases are swept. */
10
+ heartbeatInterval?: DurationLike;
11
+ /** The registered `JobsApp`; the runner defines `terreno.harness.phase` on it. */
12
+ jobs: JobsApp;
13
+ /** How long a task lease lives without a heartbeat. */
14
+ leaseDuration?: DurationLike;
15
+ /** Runner instance id written into task leases. Defaults to `hostname:pid:uuid`. */
16
+ ownerId?: string;
17
+ /** How long the dispatcher sleeps between scans for runnable tasks. */
18
+ pollInterval?: DurationLike;
19
+ }
20
+ /**
21
+ * Executes harness phases as `@terreno/jobs` jobs so any number of instances share the
22
+ * work. Every instance runs a dispatcher that enqueues one `terreno.harness.phase` job per
23
+ * runnable task (deduplicated by idempotency key) and sweeps expired task leases. The job
24
+ * handler claims its task under a fenced lease, runs one phase while heartbeating, and
25
+ * hands a still-running task back as `pending` for the next job. The task lease, not the
26
+ * job lock, decides who may commit: a duplicate or late job finds the task claimed and
27
+ * does nothing, and a stale lease holder's commit is rejected.
28
+ */
29
+ export declare class JobsRunner implements HarnessRunner {
30
+ private context;
31
+ private readonly dispatchBatchSize;
32
+ private hasPendingWake;
33
+ private readonly inFlight;
34
+ private isStopping;
35
+ private readonly lease;
36
+ private loop;
37
+ private nextSweepAt;
38
+ private readonly pollIntervalMs;
39
+ private wakeSleeper;
40
+ constructor(options: JobsRunnerOptions);
41
+ /** Id written into every task lease this runner holds. */
42
+ get ownerId(): string;
43
+ start(context: HarnessRunnerContext): Promise<void>;
44
+ /** Stop dispatching, then wait for every phase this instance is running. */
45
+ stop(): Promise<void>;
46
+ /** Dispatch now instead of after the rest of the idle sleep. */
47
+ wake(): void;
48
+ /** Job handler: claim the task, run one phase, and hand it back if it still runs. */
49
+ private runPhaseJob;
50
+ private claimAndRunPhase;
51
+ private dispatchLoop;
52
+ /** Recover expired leases once per heartbeat, then enqueue a job per runnable task. */
53
+ private tick;
54
+ /**
55
+ * Enqueue the job for this runnable visit. When the job with that key already ended
56
+ * without claiming the task (its handler kept failing, or it ran where the task was not
57
+ * runnable), follow or extend the chain `<key>:<endedJobId>` until a job is in flight, so
58
+ * the task is not stranded. A newly created job is always in flight, so this ends.
59
+ */
60
+ private dispatch;
61
+ private sleep;
62
+ }