@hasna/loops 0.3.2 → 0.3.4

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.
@@ -1,10 +1,13 @@
1
- import type { ExecutableTarget, ExecutorResult, Loop, LoopRun } from "../types.js";
2
- export interface ExecuteOptions {
1
+ import type { ExecutableTarget, ExecutorResult, Loop, LoopMachineRef, LoopRun, PersistGuardOptions } from "../types.js";
2
+ export interface ExecuteOptions extends PersistGuardOptions {
3
3
  maxOutputBytes?: number;
4
4
  env?: NodeJS.ProcessEnv;
5
5
  log?: (message: string) => void;
6
6
  signal?: AbortSignal;
7
7
  onSpawn?: (pid: number) => void;
8
+ machine?: LoopMachineRef;
9
+ machineResolver?: (machine: LoopMachineRef) => LoopMachineRef;
10
+ machineCommandResolver?: (machineId: string, command: string) => MachineCommandPlan;
8
11
  }
9
12
  export interface ExecutionMetadata {
10
13
  loopId?: string;
@@ -21,6 +24,12 @@ export interface PreflightResult {
21
24
  accountProfile?: string;
22
25
  accountTool?: string;
23
26
  }
27
+ interface MachineCommandPlan {
28
+ command: string;
29
+ args: string[];
30
+ source: string;
31
+ }
24
32
  export declare function preflightTarget(target: ExecutableTarget, metadata?: ExecutionMetadata, opts?: ExecuteOptions): PreflightResult;
25
33
  export declare function executeTarget(target: ExecutableTarget, metadata?: ExecutionMetadata, opts?: ExecuteOptions): Promise<ExecutorResult>;
26
34
  export declare function executeLoop(loop: Loop, run: LoopRun, opts?: ExecuteOptions): Promise<ExecutorResult>;
35
+ export {};
@@ -1,10 +1,11 @@
1
- import type { Loop, LoopRun, WorkflowEvent, WorkflowRun, WorkflowSpec, WorkflowStepRun } from "../types.js";
1
+ import type { ExecutorResult, Loop, LoopRun, WorkflowEvent, WorkflowRun, WorkflowSpec, WorkflowStepRun } from "../types.js";
2
2
  export declare function redact(value: string | undefined, visible?: number): string | undefined;
3
3
  export declare function textOutputBlocks(value: Pick<LoopRun | WorkflowStepRun, "stdout" | "stderr">, opts?: {
4
4
  indent?: string;
5
5
  }): string[];
6
6
  export declare function publicLoop(loop: Loop): Record<string, unknown>;
7
7
  export declare function publicRun(run: LoopRun, showOutput?: boolean): Record<string, unknown>;
8
+ export declare function publicExecutorResult(result: ExecutorResult, showOutput?: boolean): Record<string, unknown>;
8
9
  export declare function publicWorkflow(workflow: WorkflowSpec): Record<string, unknown>;
9
10
  export declare function publicWorkflowRun(run: WorkflowRun): Record<string, unknown>;
10
11
  export declare function publicWorkflowStepRun(run: WorkflowStepRun, showOutput?: boolean): Record<string, unknown>;
@@ -0,0 +1,16 @@
1
+ import type { LoopMachineRef } from "../types.js";
2
+ export interface OpenMachineSummary {
3
+ id: string;
4
+ hostname?: string;
5
+ platform?: string;
6
+ user?: string;
7
+ workspacePath?: string;
8
+ route?: string;
9
+ local: boolean;
10
+ heartbeatStatus?: string;
11
+ tailscaleOnline?: boolean | null;
12
+ tags: string[];
13
+ }
14
+ export declare function listOpenMachines(): OpenMachineSummary[];
15
+ export declare function resolveLoopMachine(machineId: string): LoopMachineRef;
16
+ export declare function refreshLoopMachine(machine: LoopMachineRef): LoopMachineRef;
@@ -4,6 +4,9 @@ export interface SchedulerDeps {
4
4
  store: Store;
5
5
  runnerId: string;
6
6
  now?: () => Date;
7
+ beforeRun?: (loop: Loop, scheduledFor: string) => void;
8
+ beforeFinalize?: (loop: Loop, run: LoopRun) => void;
9
+ daemonLeaseId?: string;
7
10
  execute?: (loop: Loop, run: LoopRun) => Promise<ExecutorResult>;
8
11
  onError?: (loop: Loop, error: unknown) => void;
9
12
  onRun?: (run: LoopRun) => void;
@@ -17,13 +20,19 @@ export interface TickResult {
17
20
  }
18
21
  export declare function manualRunScheduledFor(loop: Loop, now?: Date): string;
19
22
  export declare function shouldAdvanceManualRun(loop: Loop, scheduledFor: string, now?: Date): boolean;
20
- export declare function advanceLoop(store: Store, loop: Loop, run: LoopRun, finishedAt: Date, succeeded: boolean): void;
23
+ export type ManualRunSource = "ad_hoc" | "due_slot" | "retry_slot";
24
+ export declare function manualRunSource(loop: Loop, scheduledFor: string, now?: Date): ManualRunSource;
25
+ export declare function advanceLoop(store: Store, loop: Loop, run: LoopRun, finishedAt: Date, succeeded: boolean, opts?: {
26
+ daemonLeaseId?: string;
27
+ }): void;
21
28
  export declare function executeClaimedRun(deps: {
22
29
  store: Store;
23
30
  runnerId: string;
24
31
  loop: Loop;
25
32
  run: LoopRun;
26
33
  now?: () => Date;
34
+ beforeFinalize?: (loop: Loop, run: LoopRun) => void;
35
+ daemonLeaseId?: string;
27
36
  execute?: (loop: Loop, run: LoopRun) => Promise<ExecutorResult>;
28
37
  onError?: (loop: Loop, error: unknown) => void;
29
38
  }): Promise<LoopRun>;
@@ -1,4 +1,8 @@
1
1
  import type { CreateLoopInput, CreateWorkflowInput, Loop, LoopRun, LoopStatus, RunStatus, WorkflowEvent, WorkflowRun, WorkflowRunStatus, WorkflowSpec, WorkflowStepRun } from "../types.js";
2
+ interface DaemonLeaseFence {
3
+ daemonLeaseId?: string;
4
+ now?: Date;
5
+ }
2
6
  export interface DaemonLease {
3
7
  id: string;
4
8
  pid: number;
@@ -18,11 +22,13 @@ export interface CreateWorkflowRunInput {
18
22
  loopRun?: LoopRun;
19
23
  scheduledFor?: string;
20
24
  idempotencyKey?: string;
25
+ daemonLeaseId?: string;
21
26
  }
22
27
  export declare class Store {
23
28
  private db;
24
29
  constructor(path?: string);
25
30
  private migrate;
31
+ private assertDaemonLeaseFence;
26
32
  createLoop(input: CreateLoopInput, from?: Date): Loop;
27
33
  getLoop(id: string): Loop | undefined;
28
34
  findLoopByName(name: string): Loop | undefined;
@@ -32,7 +38,7 @@ export declare class Store {
32
38
  limit?: number;
33
39
  }): Loop[];
34
40
  dueLoops(now: Date): Loop[];
35
- updateLoop(id: string, patch: Partial<Pick<Loop, "status" | "nextRunAt" | "retryScheduledFor" | "expiresAt">>): Loop;
41
+ updateLoop(id: string, patch: Partial<Pick<Loop, "status" | "nextRunAt" | "retryScheduledFor" | "expiresAt">>, opts?: DaemonLeaseFence): Loop;
36
42
  deleteLoop(idOrName: string): boolean;
37
43
  createWorkflow(input: CreateWorkflowInput): WorkflowSpec;
38
44
  getWorkflow(id: string): WorkflowSpec | undefined;
@@ -54,37 +60,39 @@ export declare class Store {
54
60
  listWorkflowStepRuns(workflowRunId: string): WorkflowStepRun[];
55
61
  getWorkflowStepRun(workflowRunId: string, stepId: string): WorkflowStepRun | undefined;
56
62
  isWorkflowRunTerminal(workflowRunId: string): boolean;
57
- startWorkflowStepRun(workflowRunId: string, stepId: string): WorkflowStepRun;
58
- markWorkflowStepPid(workflowRunId: string, stepId: string, pid: number): WorkflowStepRun;
63
+ startWorkflowStepRun(workflowRunId: string, stepId: string, opts?: DaemonLeaseFence): WorkflowStepRun;
64
+ markWorkflowStepPid(workflowRunId: string, stepId: string, pid: number, opts?: DaemonLeaseFence): WorkflowStepRun;
59
65
  recoverWorkflowRun(workflowRunId: string, reason?: string): {
60
66
  run: WorkflowRun;
61
67
  recoveredSteps: WorkflowStepRun[];
62
68
  };
63
- finalizeWorkflowStepRun(workflowRunId: string, stepId: string, patch: Pick<WorkflowStepRun, "status" | "finishedAt" | "durationMs" | "stdout" | "stderr"> & Partial<Pick<WorkflowStepRun, "exitCode" | "error">>): WorkflowStepRun;
64
- skipWorkflowStepRun(workflowRunId: string, stepId: string, reason: string): WorkflowStepRun;
65
- finalizeWorkflowRun(workflowRunId: string, status: WorkflowRunStatus, patch?: Partial<Pick<WorkflowRun, "finishedAt" | "durationMs" | "error">>): WorkflowRun;
69
+ finalizeWorkflowStepRun(workflowRunId: string, stepId: string, patch: Pick<WorkflowStepRun, "status" | "finishedAt" | "durationMs" | "stdout" | "stderr"> & Partial<Pick<WorkflowStepRun, "exitCode" | "error">>, opts?: DaemonLeaseFence): WorkflowStepRun;
70
+ skipWorkflowStepRun(workflowRunId: string, stepId: string, reason: string, opts?: DaemonLeaseFence): WorkflowStepRun;
71
+ finalizeWorkflowRun(workflowRunId: string, status: WorkflowRunStatus, patch?: Partial<Pick<WorkflowRun, "finishedAt" | "durationMs" | "error">>, opts?: DaemonLeaseFence): WorkflowRun;
66
72
  cancelWorkflowRun(workflowRunId: string, reason?: string): WorkflowRun;
67
73
  appendWorkflowEvent(workflowRunId: string, eventType: string, stepId?: string, payload?: Record<string, unknown>): WorkflowEvent;
68
74
  listWorkflowEvents(workflowRunId: string, limit?: number): WorkflowEvent[];
69
75
  hasRunningRun(loopId: string): boolean;
70
- markRunPid(id: string, pid: number, claimedBy?: string): LoopRun | undefined;
76
+ markRunPid(id: string, pid: number, claimedBy?: string, opts?: DaemonLeaseFence): LoopRun | undefined;
71
77
  private hasLiveWorkflowStepProcesses;
72
- createSkippedRun(loop: Loop, scheduledFor: string, reason: string): LoopRun;
78
+ createSkippedRun(loop: Loop, scheduledFor: string, reason: string, opts?: DaemonLeaseFence): LoopRun;
73
79
  getRun(id: string): LoopRun | undefined;
74
80
  getRunBySlot(loopId: string, scheduledFor: string): LoopRun | undefined;
75
- claimRun(loop: Loop, scheduledFor: string, runnerId: string, now?: Date): ClaimRunResult | undefined;
81
+ nextRetryableRun(loopId: string, maxAttempts: number, afterScheduledFor?: string): LoopRun | undefined;
82
+ claimRun(loop: Loop, scheduledFor: string, runnerId: string, now?: Date, opts?: DaemonLeaseFence): ClaimRunResult | undefined;
76
83
  finalizeRun(id: string, patch: Pick<LoopRun, "status" | "finishedAt" | "durationMs" | "stdout" | "stderr"> & Partial<Pick<LoopRun, "exitCode" | "error" | "pid">>, opts?: {
77
84
  claimedBy?: string;
78
85
  now?: Date;
86
+ daemonLeaseId?: string;
79
87
  }): LoopRun;
80
- heartbeatRunLease(id: string, claimedBy: string, leaseMs: number, now?: Date): LoopRun | undefined;
88
+ heartbeatRunLease(id: string, claimedBy: string, leaseMs: number, now?: Date, opts?: DaemonLeaseFence): LoopRun | undefined;
81
89
  listRuns(opts?: {
82
90
  loopId?: string;
83
91
  status?: RunStatus;
84
92
  limit?: number;
85
93
  }): LoopRun[];
86
- recoverExpiredRunLeases(now?: Date): LoopRun[];
87
- expireLoops(now?: Date): Loop[];
94
+ recoverExpiredRunLeases(now?: Date, opts?: DaemonLeaseFence): LoopRun[];
95
+ expireLoops(now?: Date, opts?: DaemonLeaseFence): Loop[];
88
96
  countLoops(status?: LoopStatus): number;
89
97
  countRuns(status?: RunStatus): number;
90
98
  acquireDaemonLease(input: {
@@ -99,3 +107,4 @@ export declare class Store {
99
107
  getDaemonLease(): DaemonLease | undefined;
100
108
  close(): void;
101
109
  }
110
+ export {};