harnery 0.13.0 → 0.15.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/README.md +2 -0
  2. package/dist/commander.d.ts.map +1 -1
  3. package/dist/commander.js +4 -0
  4. package/dist/commands/supervisor.d.ts +4 -0
  5. package/dist/commands/supervisor.d.ts.map +1 -0
  6. package/dist/commands/supervisor.js +238 -0
  7. package/dist/commands/work.d.ts +4 -0
  8. package/dist/commands/work.d.ts.map +1 -0
  9. package/dist/commands/work.js +281 -0
  10. package/dist/core/supervisor/index.d.ts +3 -0
  11. package/dist/core/supervisor/index.d.ts.map +1 -0
  12. package/dist/core/supervisor/index.js +2 -0
  13. package/dist/core/supervisor/read.d.ts +2 -0
  14. package/dist/core/supervisor/read.d.ts.map +1 -0
  15. package/dist/core/supervisor/read.js +1 -0
  16. package/dist/core/supervisor/runner.d.ts +34 -0
  17. package/dist/core/supervisor/runner.d.ts.map +1 -0
  18. package/dist/core/supervisor/runner.js +198 -0
  19. package/dist/core/supervisor/state.d.ts +69 -0
  20. package/dist/core/supervisor/state.d.ts.map +1 -0
  21. package/dist/core/supervisor/state.js +413 -0
  22. package/dist/core/work/index.d.ts +3 -0
  23. package/dist/core/work/index.d.ts.map +1 -0
  24. package/dist/core/work/index.js +2 -0
  25. package/dist/core/work/read.d.ts +2 -0
  26. package/dist/core/work/read.d.ts.map +1 -0
  27. package/dist/core/work/read.js +1 -0
  28. package/dist/core/work/runner.d.ts +10 -0
  29. package/dist/core/work/runner.d.ts.map +1 -0
  30. package/dist/core/work/runner.js +63 -0
  31. package/dist/core/work/state.d.ts +109 -0
  32. package/dist/core/work/state.d.ts.map +1 -0
  33. package/dist/core/work/state.js +698 -0
  34. package/dist/core/workflow/engine.d.ts.map +1 -1
  35. package/dist/core/workflow/engine.js +41 -9
  36. package/dist/core/workflow/index.d.ts +1 -1
  37. package/dist/core/workflow/index.d.ts.map +1 -1
  38. package/dist/core/workflow/proof.d.ts +1 -0
  39. package/dist/core/workflow/proof.d.ts.map +1 -1
  40. package/dist/core/workflow/proof.js +2 -0
  41. package/dist/core/workflow/run-state.d.ts +3 -0
  42. package/dist/core/workflow/run-state.d.ts.map +1 -1
  43. package/dist/core/workflow/run-state.js +14 -0
  44. package/dist/core/workflow/specialists.d.ts +7 -0
  45. package/dist/core/workflow/specialists.d.ts.map +1 -0
  46. package/dist/core/workflow/specialists.js +78 -0
  47. package/dist/core/workflow/types.d.ts +24 -0
  48. package/dist/core/workflow/types.d.ts.map +1 -1
  49. package/package.json +21 -1
  50. package/src/commander.ts +4 -0
  51. package/src/commands/supervisor.ts +315 -0
  52. package/src/commands/work.ts +367 -0
  53. package/src/core/supervisor/index.ts +24 -0
  54. package/src/core/supervisor/read.ts +14 -0
  55. package/src/core/supervisor/runner.ts +278 -0
  56. package/src/core/supervisor/state.ts +552 -0
  57. package/src/core/work/index.ts +24 -0
  58. package/src/core/work/read.ts +15 -0
  59. package/src/core/work/runner.ts +81 -0
  60. package/src/core/work/state.ts +900 -0
  61. package/src/core/workflow/engine.ts +42 -9
  62. package/src/core/workflow/index.ts +1 -0
  63. package/src/core/workflow/proof.ts +3 -0
  64. package/src/core/workflow/run-state.ts +20 -0
  65. package/src/core/workflow/specialists.ts +97 -0
  66. package/src/core/workflow/types.ts +25 -0
@@ -0,0 +1,552 @@
1
+ import { randomBytes, randomUUID } from "node:crypto";
2
+ import {
3
+ chmodSync,
4
+ closeSync,
5
+ existsSync,
6
+ mkdirSync,
7
+ openSync,
8
+ readdirSync,
9
+ readFileSync,
10
+ statSync,
11
+ unlinkSync,
12
+ writeFileSync,
13
+ } from "node:fs";
14
+ import { hostname } from "node:os";
15
+ import { dirname, join, resolve } from "node:path";
16
+ import { assertWorkId, readWorkItem, type WorkRecord } from "../work/read.ts";
17
+ import { normalizeWorkflowSpecialists } from "../workflow/specialists.ts";
18
+ import type { WorkflowSpecialistProfile } from "../workflow/types.ts";
19
+
20
+ export const SUPERVISOR_INTENT_SCHEMA_VERSION = 1 as const;
21
+
22
+ const GOAL_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
23
+ const MAX_TITLE = 200;
24
+ const MAX_INTENT_BYTES = 256 * 1024;
25
+ const FOREIGN_LEASE_STALE_MS = 24 * 60 * 60 * 1_000;
26
+
27
+ export interface SupervisorLimits {
28
+ max_cycles: number;
29
+ max_runtime_ms: number;
30
+ max_parallel_work: number;
31
+ max_total_attempts: number;
32
+ max_agents_per_work: number;
33
+ agent_concurrency: number;
34
+ }
35
+
36
+ export interface SupervisorAutomationPolicy {
37
+ accept_passing_proof: boolean;
38
+ resume_approved: boolean;
39
+ retry_blocked: boolean;
40
+ }
41
+
42
+ export interface SupervisorIntent {
43
+ schema_version: typeof SUPERVISOR_INTENT_SCHEMA_VERSION;
44
+ id: string;
45
+ title: string;
46
+ root_work_id: string;
47
+ specialists: Record<string, WorkflowSpecialistProfile>;
48
+ limits: SupervisorLimits;
49
+ automation: SupervisorAutomationPolicy;
50
+ created_at: string;
51
+ }
52
+
53
+ export type SupervisorState =
54
+ | "ready"
55
+ | "running"
56
+ | "awaiting_attention"
57
+ | "blocked"
58
+ | "budget_exhausted"
59
+ | "succeeded";
60
+
61
+ export type SupervisorNextAction =
62
+ | "run"
63
+ | "wait_for_run"
64
+ | "resolve_approval"
65
+ | "review"
66
+ | "retry"
67
+ | "none";
68
+
69
+ export interface SupervisorProjection {
70
+ id: string;
71
+ title: string;
72
+ root_work_id: string;
73
+ state: SupervisorState;
74
+ reason: string;
75
+ next_action: SupervisorNextAction;
76
+ work_ids: string[];
77
+ ready_work: string[];
78
+ resumable_work: string[];
79
+ retryable_work: string[];
80
+ attention_work: string[];
81
+ attempts_used: number;
82
+ attempts_remaining: number;
83
+ specialists: string[];
84
+ }
85
+
86
+ export interface SupervisorRecord {
87
+ intent: SupervisorIntent;
88
+ projection: SupervisorProjection;
89
+ work: WorkRecord[];
90
+ }
91
+
92
+ export interface CreateSupervisorInput {
93
+ coordRoot: string;
94
+ rootWorkId: string;
95
+ specialists: Readonly<Record<string, WorkflowSpecialistProfile>>;
96
+ title?: string;
97
+ id?: string;
98
+ limits?: Partial<SupervisorLimits>;
99
+ automation?: Partial<SupervisorAutomationPolicy>;
100
+ }
101
+
102
+ export function createSupervisor(input: CreateSupervisorInput): SupervisorRecord {
103
+ const coordRoot = resolve(input.coordRoot);
104
+ assertWorkId(input.rootWorkId);
105
+ const root = readWorkItem(coordRoot, input.rootWorkId);
106
+ const id = input.id ?? newSupervisorId();
107
+ assertSupervisorId(id);
108
+ const intent: SupervisorIntent = {
109
+ schema_version: SUPERVISOR_INTENT_SCHEMA_VERSION,
110
+ id,
111
+ title: bounded(input.title ?? root.intent.title, "supervisor title", MAX_TITLE),
112
+ root_work_id: input.rootWorkId,
113
+ specialists: normalizeWorkflowSpecialists(input.specialists),
114
+ limits: normalizeLimits(input.limits),
115
+ automation: normalizeAutomation(input.automation),
116
+ created_at: new Date().toISOString(),
117
+ };
118
+ const path = supervisorIntentPath(coordRoot, id);
119
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
120
+ chmodSync(dirname(path), 0o700);
121
+ writePrivateJson(path, intent);
122
+ return readSupervisor(coordRoot, id);
123
+ }
124
+
125
+ export function readSupervisor(coordRoot: string, goalId: string): SupervisorRecord {
126
+ return readSupervisorInternal(coordRoot, goalId, false);
127
+ }
128
+
129
+ /** @internal Runner seam; ignores the lease held by this supervisor process. */
130
+ export function readSupervisorIgnoringLease(coordRoot: string, goalId: string): SupervisorRecord {
131
+ return readSupervisorInternal(coordRoot, goalId, true);
132
+ }
133
+
134
+ export function listSupervisors(coordRoot: string): SupervisorRecord[] {
135
+ const base = join(resolve(coordRoot), ".harnery", "supervisors");
136
+ if (!existsSync(base)) return [];
137
+ const records: SupervisorRecord[] = [];
138
+ for (const name of readdirSync(base)) {
139
+ if (!GOAL_ID.test(name) || !existsSync(supervisorIntentPath(coordRoot, name))) continue;
140
+ records.push(readSupervisor(coordRoot, name));
141
+ }
142
+ return records.sort((left, right) =>
143
+ right.intent.created_at.localeCompare(left.intent.created_at),
144
+ );
145
+ }
146
+
147
+ export function collectSupervisorWork(coordRoot: string, rootWorkId: string): WorkRecord[] {
148
+ const records = new Map<string, WorkRecord>();
149
+ const visiting = new Set<string>();
150
+ const visit = (workId: string): void => {
151
+ if (records.has(workId)) return;
152
+ if (visiting.has(workId))
153
+ throw new Error(`supervisor work graph contains a cycle at ${workId}`);
154
+ visiting.add(workId);
155
+ const record = readWorkItem(coordRoot, workId);
156
+ for (const dependency of record.intent.dependencies) visit(dependency);
157
+ visiting.delete(workId);
158
+ records.set(workId, record);
159
+ };
160
+ visit(rootWorkId);
161
+ return Array.from(records.values());
162
+ }
163
+
164
+ export function assertSupervisorId(goalId: string): void {
165
+ if (!GOAL_ID.test(goalId)) throw new Error(`invalid supervisor id ${JSON.stringify(goalId)}`);
166
+ }
167
+
168
+ export function newSupervisorId(): string {
169
+ return `goal-${new Date().toISOString().replace(/[:.]/g, "-")}-${randomBytes(3).toString("hex")}`;
170
+ }
171
+
172
+ /** @internal Runner seam; one exclusive crash-recoverable supervisor lease. */
173
+ export function acquireSupervisorLease(coordRoot: string, goalId: string): () => void {
174
+ readSupervisorIntent(coordRoot, goalId);
175
+ const path = join(supervisorDir(coordRoot, goalId), "lease.json");
176
+ const owner = {
177
+ pid: process.pid,
178
+ host: hostname(),
179
+ created_at: new Date().toISOString(),
180
+ nonce: randomUUID(),
181
+ };
182
+ const acquire = (): boolean => {
183
+ try {
184
+ const fd = openSync(path, "wx", 0o600);
185
+ try {
186
+ writeFileSync(fd, `${JSON.stringify(owner)}\n`, "utf8");
187
+ } finally {
188
+ closeSync(fd);
189
+ }
190
+ chmodSync(path, 0o600);
191
+ return true;
192
+ } catch (error) {
193
+ if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
194
+ return false;
195
+ }
196
+ };
197
+ if (!acquire()) {
198
+ const existing = readLease(path);
199
+ if (existing && leaseIsLive(existing)) {
200
+ throw new Error(
201
+ `supervisor ${goalId} is already running under pid ${existing.pid} on ${existing.host}`,
202
+ );
203
+ }
204
+ unlinkSync(path);
205
+ if (!acquire()) throw new Error(`supervisor ${goalId} lease raced with another process`);
206
+ }
207
+ return () => {
208
+ try {
209
+ const existing = readLease(path);
210
+ if (existing?.nonce === owner.nonce) unlinkSync(path);
211
+ } catch {
212
+ // A stale private lease is recoverable by the next explicit run.
213
+ }
214
+ };
215
+ }
216
+
217
+ function readSupervisorInternal(
218
+ coordRootRaw: string,
219
+ goalId: string,
220
+ ignoreLease: boolean,
221
+ ): SupervisorRecord {
222
+ const coordRoot = resolve(coordRootRaw);
223
+ const intent = readSupervisorIntent(coordRoot, goalId);
224
+ const work = collectSupervisorWork(coordRoot, intent.root_work_id);
225
+ return {
226
+ intent,
227
+ work,
228
+ projection: deriveProjection(coordRoot, intent, work, ignoreLease),
229
+ };
230
+ }
231
+
232
+ function deriveProjection(
233
+ coordRoot: string,
234
+ intent: SupervisorIntent,
235
+ work: WorkRecord[],
236
+ ignoreLease: boolean,
237
+ ): SupervisorProjection {
238
+ const root = work.find((record) => record.intent.id === intent.root_work_id);
239
+ if (!root) throw new Error(`supervisor ${intent.id} root work is missing`);
240
+ const attemptsUsed = work.reduce((sum, record) => sum + record.projection.attempts_used, 0);
241
+ const readyWork = work
242
+ .filter((record) => record.projection.state === "ready")
243
+ .map((record) => record.intent.id);
244
+ const resumableWork = work
245
+ .filter(
246
+ (record) =>
247
+ record.projection.state === "awaiting_approval" &&
248
+ record.projection.next_action === "resume",
249
+ )
250
+ .map((record) => record.intent.id);
251
+ const retryableWork = work
252
+ .filter(
253
+ (record) =>
254
+ record.projection.state === "blocked" && record.projection.next_action === "retry",
255
+ )
256
+ .map((record) => record.intent.id);
257
+ const pendingApproval = work
258
+ .filter(
259
+ (record) =>
260
+ record.projection.state === "awaiting_approval" &&
261
+ record.projection.next_action === "resolve_approval",
262
+ )
263
+ .map((record) => record.intent.id);
264
+ const reviews = work
265
+ .filter((record) => record.projection.state === "in_review")
266
+ .map((record) => record.intent.id);
267
+ const cancelled = work
268
+ .filter((record) => record.projection.state === "cancelled")
269
+ .map((record) => record.intent.id);
270
+ const terminalBlocked = work
271
+ .filter(
272
+ (record) => record.projection.state === "blocked" && record.projection.next_action === "none",
273
+ )
274
+ .map((record) => record.intent.id);
275
+ const attentionWork = unique([
276
+ ...pendingApproval,
277
+ ...(intent.automation.accept_passing_proof ? [] : reviews),
278
+ ...(intent.automation.resume_approved ? [] : resumableWork),
279
+ ...(intent.automation.retry_blocked ? [] : retryableWork),
280
+ ...cancelled,
281
+ ...terminalBlocked,
282
+ ]);
283
+ const base = {
284
+ id: intent.id,
285
+ title: intent.title,
286
+ root_work_id: intent.root_work_id,
287
+ work_ids: work.map((record) => record.intent.id),
288
+ ready_work: readyWork,
289
+ resumable_work: resumableWork,
290
+ retryable_work: retryableWork,
291
+ attention_work: attentionWork,
292
+ attempts_used: attemptsUsed,
293
+ attempts_remaining: Math.max(0, intent.limits.max_total_attempts - attemptsUsed),
294
+ specialists: Object.keys(intent.specialists),
295
+ };
296
+ if (!ignoreLease && supervisorLeaseIsLive(coordRoot, intent.id)) {
297
+ return {
298
+ ...base,
299
+ state: "running",
300
+ reason: "the foreground supervisor holds the goal lease",
301
+ next_action: "wait_for_run",
302
+ };
303
+ }
304
+ if (root.projection.state === "succeeded") {
305
+ return {
306
+ ...base,
307
+ state: "succeeded",
308
+ reason: "root work was explicitly accepted",
309
+ next_action: "none",
310
+ };
311
+ }
312
+ const resumableDispatchable = intent.automation.resume_approved ? resumableWork : [];
313
+ const attemptDispatchable = [
314
+ ...readyWork,
315
+ ...(intent.automation.retry_blocked ? retryableWork : []),
316
+ ];
317
+ const dispatchable = [...resumableDispatchable, ...attemptDispatchable];
318
+ if (intent.automation.accept_passing_proof && reviews.length > 0) {
319
+ return {
320
+ ...base,
321
+ state: "ready",
322
+ reason: `${reviews.length} passing work item${reviews.length === 1 ? "" : "s"} may be accepted by frozen policy`,
323
+ next_action: "run",
324
+ };
325
+ }
326
+ if (resumableDispatchable.length > 0) {
327
+ return {
328
+ ...base,
329
+ state: "ready",
330
+ reason: `${resumableDispatchable.length} parked run${resumableDispatchable.length === 1 ? " is" : "s are"} resumable`,
331
+ next_action: "run",
332
+ };
333
+ }
334
+ if (attemptsUsed >= intent.limits.max_total_attempts && attemptDispatchable.length > 0) {
335
+ return {
336
+ ...base,
337
+ state: "budget_exhausted",
338
+ reason: `goal exhausted its ${intent.limits.max_total_attempts} total attempts`,
339
+ next_action: "none",
340
+ };
341
+ }
342
+ if (dispatchable.length > 0) {
343
+ return {
344
+ ...base,
345
+ state: "ready",
346
+ reason: `${dispatchable.length} work item${dispatchable.length === 1 ? " is" : "s are"} dispatchable`,
347
+ next_action: "run",
348
+ };
349
+ }
350
+ if (pendingApproval.length > 0) {
351
+ return {
352
+ ...base,
353
+ state: "awaiting_attention",
354
+ reason: `${pendingApproval.length} work item${pendingApproval.length === 1 ? " needs" : "s need"} approval`,
355
+ next_action: "resolve_approval",
356
+ };
357
+ }
358
+ if (reviews.length > 0) {
359
+ return {
360
+ ...base,
361
+ state: "awaiting_attention",
362
+ reason: `${reviews.length} work item${reviews.length === 1 ? " awaits" : "s await"} explicit review`,
363
+ next_action: "review",
364
+ };
365
+ }
366
+ if (resumableWork.length > 0) {
367
+ return {
368
+ ...base,
369
+ state: "awaiting_attention",
370
+ reason: `${resumableWork.length} resolved approval${resumableWork.length === 1 ? " requires" : "s require"} explicit resume`,
371
+ next_action: "run",
372
+ };
373
+ }
374
+ if (retryableWork.length > 0) {
375
+ return {
376
+ ...base,
377
+ state: "blocked",
378
+ reason: `${retryableWork.length} blocked work item${retryableWork.length === 1 ? " requires" : "s require"} explicit retry`,
379
+ next_action: "retry",
380
+ };
381
+ }
382
+ return {
383
+ ...base,
384
+ state: "blocked",
385
+ reason:
386
+ attentionWork.length > 0
387
+ ? `${attentionWork.length} work item${attentionWork.length === 1 ? " needs" : "s need"} intervention`
388
+ : "goal graph has no legal progress action",
389
+ next_action: "none",
390
+ };
391
+ }
392
+
393
+ function readSupervisorIntent(coordRoot: string, goalId: string): SupervisorIntent {
394
+ assertSupervisorId(goalId);
395
+ const path = supervisorIntentPath(coordRoot, goalId);
396
+ if (!existsSync(path)) throw new Error(`supervisor ${goalId} does not exist`);
397
+ const size = statSync(path).size;
398
+ if (size <= 0 || size > MAX_INTENT_BYTES) {
399
+ throw new Error(`supervisor intent has invalid size ${size}`);
400
+ }
401
+ let intent: SupervisorIntent;
402
+ try {
403
+ intent = JSON.parse(readFileSync(path, "utf8")) as SupervisorIntent;
404
+ } catch (error) {
405
+ throw new Error(`cannot parse supervisor intent ${goalId}: ${(error as Error).message}`);
406
+ }
407
+ validateIntent(intent, goalId);
408
+ return intent;
409
+ }
410
+
411
+ function validateIntent(intent: SupervisorIntent, goalId: string): void {
412
+ if (
413
+ intent.schema_version !== SUPERVISOR_INTENT_SCHEMA_VERSION ||
414
+ intent.id !== goalId ||
415
+ !validTimestamp(intent.created_at)
416
+ ) {
417
+ throw new Error(`supervisor intent ${goalId} has an unsupported or mismatched schema`);
418
+ }
419
+ bounded(intent.title, "supervisor title", MAX_TITLE);
420
+ assertWorkId(intent.root_work_id);
421
+ const specialists = normalizeWorkflowSpecialists(intent.specialists);
422
+ if (JSON.stringify(specialists) !== JSON.stringify(intent.specialists)) {
423
+ throw new Error(`supervisor intent ${goalId} specialists are not canonical`);
424
+ }
425
+ if (JSON.stringify(normalizeLimits(intent.limits)) !== JSON.stringify(intent.limits)) {
426
+ throw new Error(`supervisor intent ${goalId} limits are not canonical`);
427
+ }
428
+ if (
429
+ JSON.stringify(normalizeAutomation(intent.automation)) !== JSON.stringify(intent.automation)
430
+ ) {
431
+ throw new Error(`supervisor intent ${goalId} automation policy is not canonical`);
432
+ }
433
+ }
434
+
435
+ function normalizeLimits(input: Partial<SupervisorLimits> | undefined): SupervisorLimits {
436
+ return {
437
+ max_cycles: positive(input?.max_cycles ?? 50, "supervisor max_cycles", 1_000),
438
+ max_runtime_ms: positive(
439
+ input?.max_runtime_ms ?? 4 * 60 * 60 * 1_000,
440
+ "supervisor max_runtime_ms",
441
+ 7 * 24 * 60 * 60 * 1_000,
442
+ ),
443
+ max_parallel_work: positive(input?.max_parallel_work ?? 1, "supervisor max_parallel_work", 20),
444
+ max_total_attempts: positive(
445
+ input?.max_total_attempts ?? 100,
446
+ "supervisor max_total_attempts",
447
+ 10_000,
448
+ ),
449
+ max_agents_per_work: positive(
450
+ input?.max_agents_per_work ?? 20,
451
+ "supervisor max_agents_per_work",
452
+ 1_000,
453
+ ),
454
+ agent_concurrency: positive(input?.agent_concurrency ?? 4, "supervisor agent_concurrency", 100),
455
+ };
456
+ }
457
+
458
+ function normalizeAutomation(
459
+ input: Partial<SupervisorAutomationPolicy> | undefined,
460
+ ): SupervisorAutomationPolicy {
461
+ return {
462
+ accept_passing_proof: boolean(input?.accept_passing_proof ?? false, "accept_passing_proof"),
463
+ resume_approved: boolean(input?.resume_approved ?? true, "resume_approved"),
464
+ retry_blocked: boolean(input?.retry_blocked ?? false, "retry_blocked"),
465
+ };
466
+ }
467
+
468
+ function supervisorDir(coordRoot: string, goalId: string): string {
469
+ assertSupervisorId(goalId);
470
+ return join(resolve(coordRoot), ".harnery", "supervisors", goalId);
471
+ }
472
+
473
+ function supervisorIntentPath(coordRoot: string, goalId: string): string {
474
+ return join(supervisorDir(coordRoot, goalId), "intent.json");
475
+ }
476
+
477
+ function supervisorLeaseIsLive(coordRoot: string, goalId: string): boolean {
478
+ const lease = readLease(join(supervisorDir(coordRoot, goalId), "lease.json"));
479
+ return lease ? leaseIsLive(lease) : false;
480
+ }
481
+
482
+ function readLease(
483
+ path: string,
484
+ ): { pid: number; host: string; created_at: string; nonce?: string } | null {
485
+ try {
486
+ const value = JSON.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
487
+ return typeof value.pid === "number" &&
488
+ Number.isSafeInteger(value.pid) &&
489
+ value.pid > 0 &&
490
+ typeof value.host === "string" &&
491
+ typeof value.created_at === "string"
492
+ ? {
493
+ pid: value.pid,
494
+ host: value.host,
495
+ created_at: value.created_at,
496
+ nonce: typeof value.nonce === "string" ? value.nonce : undefined,
497
+ }
498
+ : null;
499
+ } catch {
500
+ return null;
501
+ }
502
+ }
503
+
504
+ function leaseIsLive(lease: { pid: number; host: string; created_at: string }): boolean {
505
+ if (lease.host !== hostname()) {
506
+ const age = Date.now() - Date.parse(lease.created_at);
507
+ return Number.isFinite(age) && age < FOREIGN_LEASE_STALE_MS;
508
+ }
509
+ try {
510
+ process.kill(lease.pid, 0);
511
+ return true;
512
+ } catch (error) {
513
+ return (error as NodeJS.ErrnoException).code === "EPERM";
514
+ }
515
+ }
516
+
517
+ function writePrivateJson(path: string, value: unknown): void {
518
+ const body = `${JSON.stringify(value, null, 2)}\n`;
519
+ if (Buffer.byteLength(body) > MAX_INTENT_BYTES) {
520
+ throw new Error(`supervisor intent exceeds ${MAX_INTENT_BYTES} bytes`);
521
+ }
522
+ writeFileSync(path, body, { encoding: "utf8", flag: "wx", mode: 0o600 });
523
+ chmodSync(path, 0o600);
524
+ }
525
+
526
+ function bounded(value: unknown, field: string, max: number): string {
527
+ if (typeof value !== "string") throw new Error(`${field} must be a string`);
528
+ const normalized = value.trim();
529
+ if (!normalized) throw new Error(`${field} must not be empty`);
530
+ if (normalized.length > max) throw new Error(`${field} exceeds ${max} characters`);
531
+ return normalized;
532
+ }
533
+
534
+ function positive(value: unknown, field: string, max: number): number {
535
+ if (!Number.isSafeInteger(value) || (value as number) < 1 || (value as number) > max) {
536
+ throw new Error(`${field} must be an integer from 1 to ${max}`);
537
+ }
538
+ return value as number;
539
+ }
540
+
541
+ function boolean(value: unknown, field: string): boolean {
542
+ if (typeof value !== "boolean") throw new Error(`supervisor ${field} must be boolean`);
543
+ return value;
544
+ }
545
+
546
+ function validTimestamp(value: unknown): value is string {
547
+ return typeof value === "string" && value.length <= 40 && Number.isFinite(Date.parse(value));
548
+ }
549
+
550
+ function unique(values: string[]): string[] {
551
+ return [...new Set(values)];
552
+ }
@@ -0,0 +1,24 @@
1
+ export { type RunWorkItemInput, runWorkItem } from "./runner.ts";
2
+ export {
3
+ acceptWorkItem,
4
+ assertWorkId,
5
+ type CreateWorkItemInput,
6
+ cancelWorkItem,
7
+ createWorkItem,
8
+ listWorkItems,
9
+ newWorkId,
10
+ readWorkItem,
11
+ reconcileAllWorkItems,
12
+ reconcileWorkItem,
13
+ reopenWorkItem,
14
+ WORK_EVENT_SCHEMA_VERSION,
15
+ WORK_INTENT_SCHEMA_VERSION,
16
+ type WorkAttempt,
17
+ type WorkEvent,
18
+ type WorkEventType,
19
+ type WorkIntent,
20
+ type WorkNextAction,
21
+ type WorkProjection,
22
+ type WorkRecord,
23
+ type WorkState,
24
+ } from "./state.ts";
@@ -0,0 +1,15 @@
1
+ export {
2
+ assertWorkId,
3
+ listWorkItems,
4
+ readWorkItem,
5
+ WORK_EVENT_SCHEMA_VERSION,
6
+ WORK_INTENT_SCHEMA_VERSION,
7
+ type WorkAttempt,
8
+ type WorkEvent,
9
+ type WorkEventType,
10
+ type WorkIntent,
11
+ type WorkNextAction,
12
+ type WorkProjection,
13
+ type WorkRecord,
14
+ type WorkState,
15
+ } from "./state.ts";
@@ -0,0 +1,81 @@
1
+ import { resolve } from "node:path";
2
+ import { runWorkflow } from "../workflow/engine.ts";
3
+ import type { EngineOpts, RunReport } from "../workflow/types.ts";
4
+ import {
5
+ acquireWorkLease,
6
+ appendReconcileIfChanged,
7
+ appendWorkEvent,
8
+ assertWorkflowUnchanged,
9
+ boundedActor,
10
+ newWorkflowRunId,
11
+ readWorkItemIgnoringLease,
12
+ } from "./state.ts";
13
+
14
+ export interface RunWorkItemInput {
15
+ coordRoot: string;
16
+ workId: string;
17
+ engine: Omit<EngineOpts, "coordRoot" | "runId" | "resumeRunId" | "workItemId">;
18
+ retry?: boolean;
19
+ actor?: string;
20
+ }
21
+
22
+ export async function runWorkItem(input: RunWorkItemInput): Promise<RunReport> {
23
+ const coordRoot = resolve(input.coordRoot);
24
+ const release = acquireWorkLease(coordRoot, input.workId);
25
+ try {
26
+ const record = readWorkItemIgnoringLease(coordRoot, input.workId);
27
+ assertWorkflowUnchanged(record.intent);
28
+ const latest = record.projection.attempts.at(-1);
29
+ if (record.projection.state === "awaiting_approval" && latest) {
30
+ if (record.projection.next_action !== "resume") {
31
+ throw new Error(`work item ${input.workId} is waiting for approval ${latest.approval_id}`);
32
+ }
33
+ appendWorkEvent(coordRoot, input.workId, {
34
+ event: "attempt.resumed",
35
+ actor: boundedActor(input.actor),
36
+ reason: "resuming the parked workflow attempt",
37
+ run_id: latest.run_id,
38
+ attempt: latest.number,
39
+ });
40
+ return await runWorkflow(record.intent.workflow.path, {
41
+ ...input.engine,
42
+ coordRoot,
43
+ resumeRunId: latest.run_id,
44
+ workItemId: input.workId,
45
+ });
46
+ }
47
+ if (record.projection.state === "blocked" && !input.retry) {
48
+ throw new Error(`work item ${input.workId} is blocked; use an explicit retry`);
49
+ }
50
+ if (!(record.projection.state === "ready" || record.projection.state === "blocked")) {
51
+ throw new Error(`work item ${input.workId} cannot run from state ${record.projection.state}`);
52
+ }
53
+ if (record.projection.attempts_used >= record.intent.max_attempts) {
54
+ throw new Error(
55
+ `work item ${input.workId} exhausted its ${record.intent.max_attempts} attempts`,
56
+ );
57
+ }
58
+ const attempt = record.projection.attempts_used + 1;
59
+ const runId = newWorkflowRunId();
60
+ appendWorkEvent(coordRoot, input.workId, {
61
+ event: "attempt.started",
62
+ actor: boundedActor(input.actor),
63
+ reason: input.retry ? "explicit retry started" : "workflow attempt started",
64
+ run_id: runId,
65
+ attempt,
66
+ });
67
+ return await runWorkflow(record.intent.workflow.path, {
68
+ ...input.engine,
69
+ coordRoot,
70
+ runId,
71
+ workItemId: input.workId,
72
+ });
73
+ } finally {
74
+ try {
75
+ const current = readWorkItemIgnoringLease(coordRoot, input.workId);
76
+ appendReconcileIfChanged(coordRoot, current, boundedActor(input.actor, "work-runner"));
77
+ } finally {
78
+ release();
79
+ }
80
+ }
81
+ }