deepline 0.3.90 → 0.3.92

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 (87) hide show
  1. package/dist/bundling-sources/sdk/src/client.ts +101 -6
  2. package/dist/bundling-sources/sdk/src/monitor-fleet-contract.ts +2 -556
  3. package/dist/bundling-sources/sdk/src/monitor-fleets.ts +2 -2
  4. package/dist/bundling-sources/sdk/src/release.ts +7 -12
  5. package/dist/bundling-sources/sdk/src/types.ts +6 -1
  6. package/dist/bundling-sources/shared_libs/integration-contracts/monitor-fleet-contract.ts +549 -0
  7. package/dist/bundling-sources/shared_libs/monitors/fleets/errors.ts +96 -0
  8. package/dist/bundling-sources/shared_libs/monitors/fleets/limits.ts +8 -0
  9. package/dist/bundling-sources/shared_libs/monitors/fleets/plan.ts +320 -0
  10. package/dist/bundling-sources/shared_libs/monitors/fleets/verdict.ts +106 -0
  11. package/dist/bundling-sources/shared_libs/monitors/http-error-response.ts +5 -1
  12. package/dist/bundling-sources/shared_libs/monitors/monitor-fleet-limits.ts +2 -12
  13. package/dist/bundling-sources/shared_libs/monitors/package.json +107 -5
  14. package/dist/bundling-sources/shared_libs/monitors/provider-monitor-batch-operation.ts +12 -7
  15. package/dist/bundling-sources/shared_libs/observability/scheduled-jobs.json +48 -0
  16. package/dist/bundling-sources/shared_libs/play-runtime/app-runtime-api.ts +1 -1
  17. package/dist/bundling-sources/shared_libs/play-runtime/db-session-lease-store.ts +578 -0
  18. package/dist/bundling-sources/shared_libs/play-runtime/deployment/commands/roll-execution-relay.ts +521 -0
  19. package/dist/bundling-sources/shared_libs/play-runtime/deployment/commands/roll-receipt-gateway.ts +1378 -0
  20. package/dist/bundling-sources/shared_libs/play-runtime/deployment/commands/runtime-topology.ts +1024 -0
  21. package/dist/bundling-sources/shared_libs/play-runtime/deployment/fly-gateway-route-probe.ts +193 -0
  22. package/dist/bundling-sources/shared_libs/play-runtime/deployment/fly-machine-lifecycle.ts +104 -0
  23. package/dist/bundling-sources/shared_libs/play-runtime/deployment/fly-machines.ts +1844 -0
  24. package/dist/bundling-sources/shared_libs/play-runtime/deployment/fly-registry.ts +21 -0
  25. package/dist/bundling-sources/shared_libs/play-runtime/deployment/process-tree.ts +40 -0
  26. package/dist/bundling-sources/shared_libs/play-runtime/deployment/runtime-gateway-drain.ts +262 -0
  27. package/dist/bundling-sources/shared_libs/play-runtime/deployment/runtime-surface.ts +98 -0
  28. package/dist/bundling-sources/shared_libs/play-runtime/deployment/runtime-topology.ts +303 -0
  29. package/dist/bundling-sources/shared_libs/play-runtime/deployment/runtime-tuple-publication-guardian.ts +70 -0
  30. package/dist/bundling-sources/shared_libs/play-runtime/deployment/runtime-tuple-recovery.ts +519 -0
  31. package/dist/bundling-sources/shared_libs/play-runtime/deployment/vercel-fly-rollback-alignment.ts +280 -0
  32. package/dist/bundling-sources/shared_libs/play-runtime/deployment/vercel-production-gate.ts +159 -0
  33. package/dist/bundling-sources/shared_libs/play-runtime/integration-operation-reader-fence/memory-store.ts +114 -0
  34. package/dist/bundling-sources/shared_libs/play-runtime/run-ledger.ts +44 -5
  35. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/child-play-resolver.ts +26 -0
  36. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/index.ts +3662 -0
  37. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/progress-sidecar.ts +47 -0
  38. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-deadline.ts +84 -0
  39. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-gateway-transport.ts +91 -0
  40. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-heartbeat.ts +424 -0
  41. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-progress-push.ts +321 -0
  42. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-sheet-persistence-retry.ts +110 -0
  43. package/dist/bundling-sources/shared_libs/play-runtime/runner-app/runtime-terminal-push.ts +625 -0
  44. package/dist/bundling-sources/shared_libs/play-runtime/runtime-api.ts +46 -3
  45. package/dist/bundling-sources/shared_libs/play-runtime/scheduler-backends/receipt-gateway.ts +1 -1
  46. package/dist/bundling-sources/shared_libs/play-runtime/webhook-receipt-retry.ts +50 -0
  47. package/dist/bundling-sources/shared_libs/plays/bundling/repository-adapter.ts +24 -0
  48. package/dist/bundling-sources/shared_libs/plays/child-run-projection.ts +174 -0
  49. package/dist/bundling-sources/shared_libs/plays/deploy/api-client.ts +267 -0
  50. package/dist/bundling-sources/shared_libs/plays/deploy/control-plane-types.ts +73 -0
  51. package/dist/bundling-sources/shared_libs/plays/deploy/manifest.ts +162 -0
  52. package/dist/bundling-sources/shared_libs/plays/deploy/project-serializer.ts +87 -0
  53. package/dist/bundling-sources/shared_libs/plays/deploy/source-file-path.ts +26 -0
  54. package/dist/bundling-sources/shared_libs/plays/deploy/target-paths.ts +36 -0
  55. package/dist/bundling-sources/shared_libs/plays/launch/input-contract.ts +400 -0
  56. package/dist/bundling-sources/shared_libs/plays/launch/prepare.ts +96 -0
  57. package/dist/bundling-sources/shared_libs/plays/local-file-discovery.ts +385 -0
  58. package/dist/bundling-sources/shared_libs/plays/play-grep-search.ts +79 -6
  59. package/dist/bundling-sources/shared_libs/plays/play-reference.ts +117 -0
  60. package/dist/bundling-sources/shared_libs/plays/run-input-summary.ts +260 -0
  61. package/dist/bundling-sources/shared_libs/plays/run-summary-types.ts +49 -0
  62. package/dist/bundling-sources/shared_libs/plays/secrets/errors.ts +19 -0
  63. package/dist/bundling-sources/shared_libs/plays/sheet-filter.ts +347 -0
  64. package/dist/bundling-sources/shared_libs/plays/standard-webhooks.ts +117 -0
  65. package/dist/bundling-sources/shared_libs/plays/status-result.ts +388 -0
  66. package/dist/bundling-sources/shared_libs/plays/transform-factor-structure.ts +377 -0
  67. package/dist/bundling-sources/shared_libs/plays/webhook-payload.ts +113 -0
  68. package/dist/cli/index.js +369 -162
  69. package/dist/cli/index.mjs +369 -162
  70. package/dist/{compiler-manifest-BaJfE-5H.d.mts → compiler-manifest-x14KL83E.d.mts} +1 -1
  71. package/dist/{compiler-manifest-BaJfE-5H.d.ts → compiler-manifest-x14KL83E.d.ts} +1 -1
  72. package/dist/index.d.mts +45 -8
  73. package/dist/index.d.ts +45 -8
  74. package/dist/index.js +61 -14
  75. package/dist/index.mjs +61 -14
  76. package/dist/install-integrity.json +54 -2
  77. package/dist/monitor-fleet-contract.d.mts +3 -10
  78. package/dist/monitor-fleet-contract.d.ts +3 -10
  79. package/dist/monitor-fleet-contract.js +2 -0
  80. package/dist/monitor-fleet-contract.mjs +1 -1
  81. package/dist/plays/bundle-play-file.d.mts +2 -2
  82. package/dist/plays/bundle-play-file.d.ts +2 -2
  83. package/dist/release.d.mts +3 -3
  84. package/dist/release.d.ts +3 -3
  85. package/dist/release.js +7 -12
  86. package/dist/release.mjs +7 -12
  87. package/package.json +2 -2
@@ -0,0 +1,549 @@
1
+ /**
2
+ * The canonical Fleet authoring contract, shared by the SDK and server.
3
+ * Monitor transaction admission reuses this ceiling; it must never drift
4
+ * from the limit admitted when a customer authors a Fleet.
5
+ */
6
+ export const MONITOR_FLEET_MAX_MEMBERS = 1_000;
7
+
8
+ export const MONITOR_FLEET_FRONTIER_MAX_ROWS = 10_000;
9
+ /**
10
+ * Fleet definitions are admitted once and executed many times. Keep the
11
+ * edition beside the definition just as Plays keep their authoring-contract
12
+ * edition: a future syntax change can add a reader without reinterpreting a
13
+ * stored Fleet under new rules.
14
+ */
15
+ export const MONITOR_FLEET_AUTHORING_CONTRACT_EDITION = 1 as const;
16
+ export const SUPPORTED_MONITOR_FLEET_AUTHORING_CONTRACT_EDITIONS = [
17
+ MONITOR_FLEET_AUTHORING_CONTRACT_EDITION,
18
+ ] as const;
19
+
20
+ export type MonitorFleetAuthoringContractEdition =
21
+ (typeof SUPPORTED_MONITOR_FLEET_AUTHORING_CONTRACT_EDITIONS)[number];
22
+
23
+ export const MONITOR_FLEET_AUTHORING_CONTRACT_CHANGELOG = [
24
+ {
25
+ edition: 1,
26
+ changed:
27
+ 'Initial Fleet JSON contract: bounded sticky table membership, one daily sync, and a seven-day removal grace.',
28
+ compatibilityOwner: 'Monitors Runtime',
29
+ newWritesEnd: null,
30
+ readerRemoval: null,
31
+ },
32
+ ] as const;
33
+ /**
34
+ * Fleets are deliberately opinionated in beta: a member absent from the
35
+ * source remains intact for seven days, then its ordinary monitor is
36
+ * deactivated. This prevents transient source refreshes from deleting a
37
+ * provider resource while keeping the customer contract small.
38
+ */
39
+ export const MONITOR_FLEET_REMOVAL_GRACE_MS = 7 * 24 * 60 * 60_000;
40
+
41
+ /**
42
+ * Public, machine-readable guidance for every Monitor Fleet authoring
43
+ * surface. SDK clients may render this directly; the CLI uses the same fixed
44
+ * policy values in its help rather than maintaining another contract.
45
+ */
46
+ export const MONITOR_FLEET_DOCUMENTATION = {
47
+ summary:
48
+ 'A Monitor Fleet keeps one bounded, sticky set of ordinary monitors aligned with Customer DB rows.',
49
+ authoredFields: {
50
+ id: 'Stable lowercase fleet id used by sync, get, and deactivate.',
51
+ source:
52
+ 'Customer DB table, unique row key, and optional equality filters that select candidate rows.',
53
+ member:
54
+ 'Ordinary monitor tool, stable key template, and payload evaluated for each selected source row.',
55
+ selection:
56
+ 'Deterministic ranking and an active-member limit between 1 and 1,000.',
57
+ },
58
+ fixedPolicy: {
59
+ cadence: 'daily',
60
+ membership: 'sticky',
61
+ removalGrace: '7d',
62
+ onRemoved: 'deactivate',
63
+ ownership:
64
+ 'A Fleet may adopt a same-tool ordinary monitor with its deterministic member key. The first Fleet claim wins; another Fleet or a different tool receives a conflict.',
65
+ billing:
66
+ 'No Fleet fee, permit, or renewal. Dry-run reports ordinary monitor lifecycle charges due now.',
67
+ },
68
+ stickyMembership:
69
+ 'Ranking fills vacancies but never replaces a current member merely because another row ranks higher.',
70
+ columnExpression:
71
+ 'Use { "$fleet": "column", "name": "column_name" } in a member key or payload to read a value from each source row.',
72
+ } as const;
73
+
74
+ export type MonitorFleetColumn = {
75
+ $fleet: 'column';
76
+ name: string;
77
+ };
78
+
79
+ export type MonitorFleetTemplate = {
80
+ $fleet: 'template';
81
+ parts: Array<string | MonitorFleetColumn>;
82
+ };
83
+
84
+ export type MonitorFleetExpression = MonitorFleetColumn | MonitorFleetTemplate;
85
+
86
+ export type MonitorFleetDefinition = {
87
+ /** Stable lowercase fleet id, used by CLI and API lifecycle commands. */
88
+ id: string;
89
+ /** Customer DB rows that may become members. */
90
+ source: {
91
+ kind: 'customer_db_table';
92
+ schema: string;
93
+ table: string;
94
+ key: { column: string; type: 'text' | 'uuid' | 'int4' | 'int8' };
95
+ where?: Record<string, string | number | boolean | null>;
96
+ };
97
+ /** One ordinary monitor definition, evaluated once for each selected row. */
98
+ member: {
99
+ tool: string;
100
+ key: MonitorFleetTemplate;
101
+ payload: Record<string, unknown>;
102
+ };
103
+ /**
104
+ * Bounded deterministic candidate selection. Membership is intentionally
105
+ * sticky: ranking chooses vacant slots but does not replace existing members
106
+ * merely because another row's score changed.
107
+ */
108
+ selection: {
109
+ limit: number;
110
+ orderBy: Array<{
111
+ column: string;
112
+ direction: 'asc' | 'desc';
113
+ }>;
114
+ membership: 'sticky';
115
+ };
116
+ };
117
+
118
+ export type MonitorFleetContractIssue = {
119
+ path: string;
120
+ code: string;
121
+ message: string;
122
+ };
123
+
124
+ /** The durable, validated snapshot that a Fleet sync is allowed to execute. */
125
+ export type AdmittedMonitorFleetAuthoringContract = {
126
+ edition: MonitorFleetAuthoringContractEdition;
127
+ definition: MonitorFleetDefinition;
128
+ };
129
+
130
+ export type MonitorFleetAuthoringContractResult = {
131
+ valid: boolean;
132
+ contract?: AdmittedMonitorFleetAuthoringContract;
133
+ issues: MonitorFleetContractIssue[];
134
+ };
135
+
136
+ function record(value: unknown): Record<string, unknown> | null {
137
+ return value && typeof value === 'object' && !Array.isArray(value)
138
+ ? (value as Record<string, unknown>)
139
+ : null;
140
+ }
141
+
142
+ function identifier(value: unknown): value is string {
143
+ return typeof value === 'string' && /^[A-Za-z_][A-Za-z0-9_]*$/.test(value);
144
+ }
145
+
146
+ function exactKeys(
147
+ input: Record<string, unknown> | null,
148
+ keys: readonly string[],
149
+ path: string,
150
+ issues: MonitorFleetContractIssue[],
151
+ ): void {
152
+ if (!input) return;
153
+ for (const key of Object.keys(input)) {
154
+ if (keys.includes(key)) continue;
155
+ issues.push({
156
+ path: path ? `${path}.${key}` : key,
157
+ code: 'unknown_fleet_field',
158
+ message: `${path || 'Fleet'} does not accept '${key}'.`,
159
+ });
160
+ }
161
+ }
162
+
163
+ function jsonValue(
164
+ value: unknown,
165
+ path: string,
166
+ issues: MonitorFleetContractIssue[],
167
+ ): void {
168
+ if (
169
+ value === null ||
170
+ typeof value === 'string' ||
171
+ typeof value === 'boolean' ||
172
+ (typeof value === 'number' && Number.isFinite(value))
173
+ ) {
174
+ return;
175
+ }
176
+ if (Array.isArray(value)) {
177
+ value.forEach((item, index) => jsonValue(item, `${path}.${index}`, issues));
178
+ return;
179
+ }
180
+ const input = record(value);
181
+ if (!input) {
182
+ issues.push({
183
+ path,
184
+ code: 'invalid_fleet_json_value',
185
+ message: 'Fleet payload values must be JSON values.',
186
+ });
187
+ return;
188
+ }
189
+ for (const [key, item] of Object.entries(input)) {
190
+ jsonValue(item, `${path}.${key}`, issues);
191
+ }
192
+ }
193
+
194
+ export function fleetColumn(name: string): MonitorFleetColumn {
195
+ if (!identifier(name)) throw new Error(`Invalid fleet column '${name}'.`);
196
+ return { $fleet: 'column', name };
197
+ }
198
+
199
+ export function fleetKey(
200
+ ...parts: Array<string | MonitorFleetColumn>
201
+ ): MonitorFleetTemplate {
202
+ if (parts.length === 0)
203
+ throw new Error('A fleet key needs at least one part.');
204
+ return { $fleet: 'template', parts };
205
+ }
206
+
207
+ export function defineMonitorFleet<const T extends MonitorFleetDefinition>(
208
+ definition: T,
209
+ ): T {
210
+ return definition;
211
+ }
212
+
213
+ function validateExpression(
214
+ value: unknown,
215
+ path: string,
216
+ issues: MonitorFleetContractIssue[],
217
+ ): void {
218
+ const input = record(value);
219
+ if (!input || input.$fleet !== 'column' || !identifier(input.name)) {
220
+ issues.push({
221
+ path,
222
+ code: 'invalid_fleet_expression',
223
+ message: 'Fleet expressions must be an exact tagged column reference.',
224
+ });
225
+ return;
226
+ }
227
+ exactKeys(input, ['$fleet', 'name'], path, issues);
228
+ }
229
+
230
+ function walkPayload(
231
+ value: unknown,
232
+ path: string,
233
+ issues: MonitorFleetContractIssue[],
234
+ ): void {
235
+ if (Array.isArray(value)) {
236
+ value.forEach((item, index) =>
237
+ walkPayload(item, `${path}.${index}`, issues),
238
+ );
239
+ return;
240
+ }
241
+ const input = record(value);
242
+ if (!input) return;
243
+ if ('$fleet' in input) {
244
+ validateExpression(input, path, issues);
245
+ return;
246
+ }
247
+ for (const [key, item] of Object.entries(input)) {
248
+ walkPayload(item, `${path}.${key}`, issues);
249
+ }
250
+ }
251
+
252
+ export function validateMonitorFleetDefinition(value: unknown): {
253
+ valid: boolean;
254
+ definition?: MonitorFleetDefinition;
255
+ issues: MonitorFleetContractIssue[];
256
+ } {
257
+ const issues: MonitorFleetContractIssue[] = [];
258
+ const input = record(value);
259
+ if (!input) {
260
+ return {
261
+ valid: false,
262
+ issues: [
263
+ {
264
+ path: '',
265
+ code: 'invalid_fleet',
266
+ message: 'Fleet must be an object.',
267
+ },
268
+ ],
269
+ };
270
+ }
271
+ exactKeys(input, ['id', 'source', 'member', 'selection'], '', issues);
272
+ if (
273
+ typeof input.id !== 'string' ||
274
+ !/^[a-z][a-z0-9-]{0,199}$/.test(input.id)
275
+ ) {
276
+ issues.push({
277
+ path: 'id',
278
+ code: 'invalid_fleet_id',
279
+ message: 'Fleet id must be lowercase kebab-case.',
280
+ });
281
+ }
282
+ const source = record(input.source);
283
+ exactKeys(
284
+ source,
285
+ ['kind', 'schema', 'table', 'key', 'where'],
286
+ 'source',
287
+ issues,
288
+ );
289
+ if (!source || source.kind !== 'customer_db_table') {
290
+ issues.push({
291
+ path: 'source.kind',
292
+ code: 'invalid_fleet_source',
293
+ message: 'Beta fleets require customer_db_table.',
294
+ });
295
+ }
296
+ if (!identifier(source?.schema) || !identifier(source?.table)) {
297
+ issues.push({
298
+ path: 'source',
299
+ code: 'invalid_fleet_table',
300
+ message: 'Source schema and table must be SQL identifiers.',
301
+ });
302
+ }
303
+ const sourceKey = record(source?.key);
304
+ exactKeys(sourceKey, ['column', 'type'], 'source.key', issues);
305
+ if (
306
+ !sourceKey ||
307
+ !identifier(sourceKey.column) ||
308
+ !['text', 'uuid', 'int4', 'int8'].includes(String(sourceKey.type))
309
+ ) {
310
+ issues.push({
311
+ path: 'source.key',
312
+ code: 'invalid_fleet_source_key',
313
+ message: 'Source key must name a supported typed column.',
314
+ });
315
+ }
316
+ if (source?.where !== undefined) {
317
+ const where = record(source.where);
318
+ if (!where) {
319
+ issues.push({
320
+ path: 'source.where',
321
+ code: 'invalid_fleet_where',
322
+ message: 'source.where must be an object of equality values.',
323
+ });
324
+ } else {
325
+ for (const [column, expected] of Object.entries(where)) {
326
+ if (!identifier(column)) {
327
+ issues.push({
328
+ path: `source.where.${column}`,
329
+ code: 'invalid_fleet_where_column',
330
+ message: 'source.where keys must be SQL identifiers.',
331
+ });
332
+ }
333
+ if (
334
+ expected !== null &&
335
+ typeof expected !== 'string' &&
336
+ typeof expected !== 'boolean' &&
337
+ !(typeof expected === 'number' && Number.isFinite(expected))
338
+ ) {
339
+ issues.push({
340
+ path: `source.where.${column}`,
341
+ code: 'invalid_fleet_where_value',
342
+ message: 'source.where values must be finite JSON scalars or null.',
343
+ });
344
+ }
345
+ }
346
+ }
347
+ }
348
+ const member = record(input.member);
349
+ exactKeys(member, ['tool', 'key', 'payload'], 'member', issues);
350
+ const key = record(member?.key);
351
+ if (!member || typeof member.tool !== 'string' || !member.tool.trim()) {
352
+ issues.push({
353
+ path: 'member.tool',
354
+ code: 'invalid_fleet_tool',
355
+ message: 'Member tool is required.',
356
+ });
357
+ }
358
+ if (
359
+ !key ||
360
+ key.$fleet !== 'template' ||
361
+ !Array.isArray(key.parts) ||
362
+ key.parts.length === 0
363
+ ) {
364
+ issues.push({
365
+ path: 'member.key',
366
+ code: 'invalid_fleet_key',
367
+ message: 'Member key must be a tagged fleet template.',
368
+ });
369
+ } else {
370
+ exactKeys(key, ['$fleet', 'parts'], 'member.key', issues);
371
+ for (const [index, part] of key.parts.entries()) {
372
+ if (typeof part !== 'string')
373
+ validateExpression(part, `member.key.parts.${index}`, issues);
374
+ }
375
+ if (
376
+ sourceKey &&
377
+ !key.parts.some(
378
+ (part) =>
379
+ record(part)?.$fleet === 'column' &&
380
+ record(part)?.name === sourceKey.column,
381
+ )
382
+ ) {
383
+ issues.push({
384
+ path: 'member.key.parts',
385
+ code: 'fleet_key_missing_source_key',
386
+ message: 'Member key must include the source key column.',
387
+ });
388
+ }
389
+ }
390
+ if (!member || !('payload' in member)) {
391
+ issues.push({
392
+ path: 'member.payload',
393
+ code: 'invalid_fleet_payload',
394
+ message: 'Member payload is required.',
395
+ });
396
+ } else {
397
+ jsonValue(member.payload, 'member.payload', issues);
398
+ }
399
+ walkPayload(member?.payload, 'member.payload', issues);
400
+ const selection = record(input.selection);
401
+ exactKeys(selection, ['limit', 'orderBy', 'membership'], 'selection', issues);
402
+ if (
403
+ !selection ||
404
+ !Number.isSafeInteger(selection.limit) ||
405
+ Number(selection.limit) < 1 ||
406
+ Number(selection.limit) > MONITOR_FLEET_MAX_MEMBERS
407
+ ) {
408
+ issues.push({
409
+ path: 'selection.limit',
410
+ code: 'MONITOR_FLEET_LIMIT_EXCEEDED',
411
+ message: 'selection.limit must be between 1 and 1,000.',
412
+ });
413
+ }
414
+ if (selection?.membership !== 'sticky') {
415
+ issues.push({
416
+ path: 'selection.membership',
417
+ code: 'invalid_fleet_membership',
418
+ message: `Fleet membership is fixed to ${MONITOR_FLEET_DOCUMENTATION.fixedPolicy.membership}. ${MONITOR_FLEET_DOCUMENTATION.stickyMembership}`,
419
+ });
420
+ }
421
+ const orderBy = selection?.orderBy;
422
+ if (!Array.isArray(orderBy) || orderBy.length === 0) {
423
+ issues.push({
424
+ path: 'selection.orderBy',
425
+ code: 'invalid_fleet_order',
426
+ message: 'At least one deterministic order field is required.',
427
+ });
428
+ } else {
429
+ const orderColumns = new Set<string>();
430
+ for (const [index, order] of orderBy.entries()) {
431
+ const item = record(order);
432
+ exactKeys(
433
+ item,
434
+ ['column', 'direction'],
435
+ `selection.orderBy.${index}`,
436
+ issues,
437
+ );
438
+ if (
439
+ !item ||
440
+ !identifier(item.column) ||
441
+ !['asc', 'desc'].includes(String(item.direction))
442
+ ) {
443
+ issues.push({
444
+ path: `selection.orderBy.${index}`,
445
+ code: 'invalid_fleet_order',
446
+ message: 'Order fields need a column and asc/desc direction.',
447
+ });
448
+ } else if (orderColumns.has(item.column as string)) {
449
+ issues.push({
450
+ path: `selection.orderBy.${index}.column`,
451
+ code: 'duplicate_fleet_order_column',
452
+ message: 'Each selection order column may appear only once.',
453
+ });
454
+ } else {
455
+ orderColumns.add(item.column as string);
456
+ }
457
+ }
458
+ if (sourceKey && record(orderBy.at(-1))?.column !== sourceKey.column) {
459
+ issues.push({
460
+ path: 'selection.orderBy',
461
+ code: 'fleet_order_missing_key',
462
+ message: 'The final order field must be the source key.',
463
+ });
464
+ }
465
+ }
466
+ for (const removedOption of ['spendLimits', 'lifecycle', 'reconciliation']) {
467
+ if (removedOption in input) {
468
+ issues.push({
469
+ path: removedOption,
470
+ code: 'unsupported_fleet_option',
471
+ message:
472
+ `${removedOption} is fixed by Monitor Fleets and must be omitted. ` +
473
+ `Fleets sync ${MONITOR_FLEET_DOCUMENTATION.fixedPolicy.cadence} and ${MONITOR_FLEET_DOCUMENTATION.fixedPolicy.onRemoved} members after ${MONITOR_FLEET_DOCUMENTATION.fixedPolicy.removalGrace} absent from the source.`,
474
+ });
475
+ }
476
+ }
477
+ return issues.length === 0
478
+ ? { valid: true, definition: value as MonitorFleetDefinition, issues }
479
+ : { valid: false, issues };
480
+ }
481
+
482
+ /**
483
+ * Compile JSON into the exact authoring snapshot persisted by a Fleet. This
484
+ * does not inspect the Customer DB or provider; use
485
+ * `fleets sync --file <fleet.json> --dry-run` for that environment-aware
486
+ * preflight, which returns the real plan and the credits due.
487
+ */
488
+ export function admitMonitorFleetAuthoringContract(
489
+ value: unknown,
490
+ edition: number = MONITOR_FLEET_AUTHORING_CONTRACT_EDITION,
491
+ ): MonitorFleetAuthoringContractResult {
492
+ if (
493
+ !SUPPORTED_MONITOR_FLEET_AUTHORING_CONTRACT_EDITIONS.includes(
494
+ edition as MonitorFleetAuthoringContractEdition,
495
+ )
496
+ ) {
497
+ return {
498
+ valid: false,
499
+ issues: [
500
+ {
501
+ path: 'authoring_contract_edition',
502
+ code: 'unsupported_fleet_authoring_contract_edition',
503
+ message: `Monitor Fleet authoring contract edition ${edition} is not supported.`,
504
+ },
505
+ ],
506
+ };
507
+ }
508
+ const result = validateMonitorFleetDefinition(value);
509
+ return result.valid && result.definition
510
+ ? {
511
+ valid: true,
512
+ contract: {
513
+ edition: edition as MonitorFleetAuthoringContractEdition,
514
+ // JSON cloning prevents later caller mutation from changing the
515
+ // admitted snapshot.
516
+ definition: JSON.parse(
517
+ JSON.stringify(result.definition),
518
+ ) as MonitorFleetDefinition,
519
+ },
520
+ issues: [],
521
+ }
522
+ : { valid: false, issues: result.issues };
523
+ }
524
+
525
+ export function evaluateMonitorFleetValue(
526
+ value: unknown,
527
+ row: Record<string, unknown>,
528
+ ): unknown {
529
+ if (Array.isArray(value))
530
+ return value.map((item) => evaluateMonitorFleetValue(item, row));
531
+ const input = record(value);
532
+ if (!input) return value;
533
+ if (input.$fleet === 'column') return row[String(input.name)];
534
+ if (input.$fleet === 'template' && Array.isArray(input.parts)) {
535
+ return input.parts
536
+ .map((part) =>
537
+ typeof part === 'string'
538
+ ? part
539
+ : String(evaluateMonitorFleetValue(part, row) ?? ''),
540
+ )
541
+ .join('');
542
+ }
543
+ return Object.fromEntries(
544
+ Object.entries(input).map(([key, item]) => [
545
+ key,
546
+ evaluateMonitorFleetValue(item, row),
547
+ ]),
548
+ );
549
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * The fleet error contract.
3
+ *
4
+ * Every failure a customer or an agent can see is one of these codes, carries
5
+ * a message that names what refused it, and carries `next` - a command that
6
+ * can be run as-is. An error without a runnable next step makes an agent
7
+ * guess, and guessing at a control-plane mutation is how duplicates happen.
8
+ */
9
+ export const MONITOR_FLEET_ERROR_CODE = {
10
+ invalidDefinition: 'MONITOR_FLEET_INVALID_DEFINITION',
11
+ notFound: 'MONITOR_FLEET_NOT_FOUND',
12
+ conflict: 'MONITOR_FLEET_CONFLICT',
13
+ deactivated: 'MONITOR_FLEET_DEACTIVATED',
14
+ failed: 'MONITOR_FLEET_FAILED',
15
+ } as const;
16
+
17
+ /** Every fleet command is idempotent, so a retry is always the safe answer. */
18
+ export const MONITOR_FLEET_RETRY_NOTE =
19
+ 'Safe to retry — every fleet command is idempotent.';
20
+
21
+ export function fleetGetCommand(fleetId: string): string {
22
+ return `deepline monitors fleets get ${fleetId} --json`;
23
+ }
24
+
25
+ export function fleetDriftCommand(fleetId: string): string {
26
+ return `deepline monitors fleets get ${fleetId} --drift --json`;
27
+ }
28
+
29
+ /** There is no `fleets list` verb: `get` with no id is the list. */
30
+ export function fleetListCommand(): string {
31
+ return 'deepline monitors fleets get --json';
32
+ }
33
+
34
+ /** Re-plans the stored definition. `--file` would apply a new one instead. */
35
+ export function fleetSyncCommand(fleetId: string): string {
36
+ return `deepline monitors fleets sync ${fleetId} --json`;
37
+ }
38
+
39
+ export class MonitorFleetError extends Error {
40
+ constructor(
41
+ readonly code: string,
42
+ message: string,
43
+ readonly status: number,
44
+ /** A command the caller can run right now. Never optional in practice. */
45
+ readonly next: string,
46
+ readonly details: Record<string, unknown> = {},
47
+ ) {
48
+ super(message);
49
+ this.name = 'MonitorFleetError';
50
+ }
51
+ }
52
+
53
+ export function invalidFleetDefinition(
54
+ message: string,
55
+ details: Record<string, unknown> = {},
56
+ ): MonitorFleetError {
57
+ return new MonitorFleetError(
58
+ MONITOR_FLEET_ERROR_CODE.invalidDefinition,
59
+ message,
60
+ 400,
61
+ 'Fix the fleet file, then run `deepline monitors fleets sync --file <fleet-file> --dry-run --json`.',
62
+ details,
63
+ );
64
+ }
65
+
66
+ export function fleetNotFound(fleetId: string): MonitorFleetError {
67
+ return new MonitorFleetError(
68
+ MONITOR_FLEET_ERROR_CODE.notFound,
69
+ `Monitor Fleet '${fleetId}' was not found.`,
70
+ 404,
71
+ fleetListCommand(),
72
+ );
73
+ }
74
+
75
+ export function fleetConflict(
76
+ message: string,
77
+ fleetId: string,
78
+ details: Record<string, unknown> = {},
79
+ ): MonitorFleetError {
80
+ return new MonitorFleetError(
81
+ MONITOR_FLEET_ERROR_CODE.conflict,
82
+ message,
83
+ 409,
84
+ fleetGetCommand(fleetId),
85
+ details,
86
+ );
87
+ }
88
+
89
+ export function fleetDeactivated(fleetId: string): MonitorFleetError {
90
+ return new MonitorFleetError(
91
+ MONITOR_FLEET_ERROR_CODE.deactivated,
92
+ `Monitor Fleet '${fleetId}' is deactivated and admits no new plan.`,
93
+ 409,
94
+ fleetGetCommand(fleetId),
95
+ );
96
+ }
@@ -0,0 +1,8 @@
1
+ /** One wire vocabulary and transaction-size contract for Fleet writers. */
2
+ export const FLEET_BLOCKED_BY = {
3
+ watchedEntities: 'monitor.watched_entities',
4
+ monitorsPerEntity: 'monitor.monitors_per_entity',
5
+ rolloutAccess: 'monitor.rollout_access',
6
+ } as const;
7
+ export const MAX_MEMBER_DEFINITION_BYTES = 2_048;
8
+ export const MAX_FLEET_SETTLE_ITEMS = 100;