@nones/agentflow-core 0.1.0 → 0.6.1

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.
package/src/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { HistoryCapability } from "./history";
1
2
  /**
2
3
  * Shared contracts for AgentFlow.
3
4
  *
@@ -5,6 +6,8 @@
5
6
  * Strategies and providers should import them with `import type`.
6
7
  */
7
8
 
9
+ import type { DrainOptions, DrainReport } from "./protocol";
10
+
8
11
  export interface Profile {
9
12
  id: string;
10
13
  adapter: string;
@@ -16,6 +19,20 @@ export interface Profile {
16
19
  startsPerSecond?: number;
17
20
  }
18
21
 
22
+ /**
23
+ * Server-side defaults for one agent. The daemon merges these before an agent
24
+ * is admitted, so `skillDirectories` and `contextFiles` inside `settings` are
25
+ * applied exactly like values an agent declared in its script.
26
+ *
27
+ * Precedence: explicit agent script fields, then the named preset selected by
28
+ * `AgentSpec.preset`, then the global `agentDefaults`.
29
+ */
30
+ export interface AgentDefaults {
31
+ profile?: string;
32
+ model?: string;
33
+ settings?: Record<string, unknown>;
34
+ }
35
+
19
36
  export interface AgentSpec {
20
37
  id: string;
21
38
  prompt: string;
@@ -23,6 +40,11 @@ export interface AgentSpec {
23
40
  profile?: string;
24
41
  model?: string;
25
42
  settings?: Record<string, unknown>;
43
+ /**
44
+ * Name of a configured subagent preset. Presets are never inferred; an agent
45
+ * opts in explicitly and an unknown name is rejected before admission.
46
+ */
47
+ preset?: string;
26
48
  strategy?: string;
27
49
  strategyInput?: unknown;
28
50
  }
@@ -41,11 +63,131 @@ export interface ArtifactDeclaration {
41
63
  schema?: Record<string, unknown>;
42
64
  }
43
65
 
66
+ /**
67
+ * Engine address of a non-agent step. It is `waveId/stepId`; a `/` is never an
68
+ * engine agent ID, so step coordinates and agent IDs can never collide.
69
+ */
70
+ export interface PlanStepRef {
71
+ waveId: string;
72
+ id: string;
73
+ }
74
+
75
+ /**
76
+ * One non-agent step the engine binds to a trusted registry handler. The plan
77
+ * only ever carries the handler *name* (and optional version pin); executable
78
+ * text kept in a template source is rejected by plan validation.
79
+ */
80
+ export interface PlanHandlerStep {
81
+ /** Stable engine step ID, unique within its wave. */
82
+ id: string;
83
+ kind: "script" | "gate";
84
+ /** Registry handler name; never executable source. */
85
+ handler: string;
86
+ /** When set, the registered handler must report exactly this version. */
87
+ handlerVersion?: string;
88
+ /** Steps that must settle before this one is admitted. */
89
+ dependsOn?: PlanStepRef[];
90
+ /** Prose only: a reusable, non-executable note for operators. */
91
+ description?: string;
92
+ waveId: string;
93
+ cwd?: string;
94
+ }
95
+
44
96
  export interface RunPlan {
45
97
  waves: WaveSpec[];
46
98
  profile?: string;
47
99
  artifacts?: ArtifactDeclaration[];
48
100
  sealed?: boolean;
101
+ /** Trusted script steps, keyed by their wave. */
102
+ scripts?: PlanHandlerStep[];
103
+ /** Trusted verification gates, keyed by their wave. */
104
+ gates?: PlanHandlerStep[];
105
+ /** Persisted plan metadata (phases, rounds, steps, strategy envelope). */
106
+ metadata?: RunPlanMetadata;
107
+ }
108
+
109
+ /**
110
+ * Trusted context for one script or gate handler invocation. Non-agent steps do
111
+ * not consume provider quota and are never re-admitted after an unknown effect
112
+ * without an explicit operator decision.
113
+ */
114
+ export interface PlanHandlerContext {
115
+ runId: string;
116
+ waveId: string;
117
+ stepId: string;
118
+ kind: "script" | "gate";
119
+ handler: string;
120
+ handlerVersion: string;
121
+ /** The step's declared cwd (the round's phase cwd). */
122
+ cwd: string;
123
+ /** Steps discovered by the persisted plan that precede this one. */
124
+ dependsOn: readonly PlanStepRef[];
125
+ /** Known results of this run's steps that settled before admission. */
126
+ upstream: ReadonlyMap<string, unknown>;
127
+ signal?: AbortSignal;
128
+ }
129
+
130
+ /**
131
+ * Raw handler output. `artifacts` uses plan artifact IDs; `data` is a bounded,
132
+ * JSON-serializable value that becomes the result a gate verifies.
133
+ */
134
+ export interface PlanHandlerOutput {
135
+ status: "succeeded" | "failed";
136
+ /** Gate verdict only. A script ignores it; a gate defaults to passed. */
137
+ passed?: boolean;
138
+ data?: unknown;
139
+ artifacts?: Record<string, unknown>;
140
+ reason?: string;
141
+ }
142
+
143
+ /** One trusted, versioned handler the daemon may run for a plan step. */
144
+ export interface PlanHandler {
145
+ /** Exact handler name referenced by `PlanHandlerStep.handler`. */
146
+ name: string;
147
+ /** Monotonic handler revision. Recorded in every result. */
148
+ version: string;
149
+ kind: "script" | "gate";
150
+ run: (
151
+ context: PlanHandlerContext,
152
+ ) => PlanHandlerOutput | Promise<PlanHandlerOutput>;
153
+ }
154
+
155
+ /** Durable state of one persisted non-agent step. */
156
+ export type PlanStepStatus =
157
+ | "pending"
158
+ | "running"
159
+ | "succeeded"
160
+ | "failed"
161
+ | "interrupted"
162
+ | "blocked"
163
+ | "cancelled";
164
+
165
+ /**
166
+ * One durable result row for a script or gate. A running row without a result
167
+ * is an unknown effect: recovery pauses it instead of replaying it.
168
+ */
169
+ export interface PlanStepResult {
170
+ runId: string;
171
+ waveId: string;
172
+ stepId: string;
173
+ kind: "script" | "gate";
174
+ handler: string;
175
+ handlerVersion: string;
176
+ status: PlanStepStatus;
177
+ passed?: boolean;
178
+ data?: unknown;
179
+ reason?: string;
180
+ error?: string;
181
+ /** Declared working directory of the step. */
182
+ cwd?: string;
183
+ /** Step coordinates this step waited on, as declared by the plan. */
184
+ dependsOn?: PlanStepRef[];
185
+ /** Immutable attempt that produced the row. */
186
+ attemptId: string;
187
+ startedAt: number;
188
+ finishedAt?: number;
189
+ /** Source run of a reused result, when a successor kept it. */
190
+ reusedFrom?: string;
49
191
  }
50
192
 
51
193
  export interface RunRequest {
@@ -53,6 +195,23 @@ export interface RunRequest {
53
195
  strategy: string;
54
196
  input: unknown;
55
197
  profile?: string;
198
+ /** Optional mutable display name persisted with the run. Names may repeat. */
199
+ name?: string;
200
+ /** Canonical project directory; `projectId` is derived from it when omitted. */
201
+ projectDir?: string;
202
+ /** Canonical project identity; defaults to `canonicalProjectId(projectDir)`. */
203
+ projectId?: string;
204
+ /**
205
+ * SHA-256 of the exact script entry bytes that issued this start. Set by the
206
+ * CLI's snapshot step; absent for embedded clients and legacy scripts.
207
+ */
208
+ scriptDigest?: string;
209
+ /**
210
+ * Identity of one script invocation. Every run created by the same
211
+ * invocation shares it, so several runs stay distinct while their shared
212
+ * provenance is still discoverable.
213
+ */
214
+ invocationId?: string;
56
215
  }
57
216
 
58
217
  export type TurnStatus =
@@ -74,6 +233,11 @@ export interface TurnResult {
74
233
  artifacts?: Record<string, unknown>;
75
234
  }
76
235
 
236
+ export interface ProviderEvent {
237
+ type: string;
238
+ data?: unknown;
239
+ }
240
+
77
241
  export interface TurnRequest {
78
242
  sessionId: string;
79
243
  turnId: string;
@@ -82,8 +246,9 @@ export interface TurnRequest {
82
246
  prompt: string;
83
247
  signal?: AbortSignal;
84
248
  receipt?: string;
85
- onReceipt?: (id: string) => void;
86
- onEvent?: (event: { type: string; data?: unknown }) => void;
249
+ onReceipt?: (id: string, nativeSessionId?: string) => void;
250
+ sessionConfiguration?: SessionConfiguration;
251
+ onEvent?: (event: ProviderEvent) => void;
87
252
  }
88
253
 
89
254
  export interface SendRequest {
@@ -121,14 +286,25 @@ export interface SessionConfiguration {
121
286
  permissions?: readonly SessionPermission[];
122
287
  }
123
288
 
289
+ export interface ProviderModel {
290
+ id: string;
291
+ label?: string;
292
+ provider?: string;
293
+ description?: string;
294
+ default?: boolean;
295
+ reasoningEfforts?: string[];
296
+ }
297
+
124
298
  export interface ProviderAdapter {
125
299
  id: string;
300
+ listModels?(profile: Profile): Promise<readonly ProviderModel[]>;
126
301
  open(
127
302
  agent: AgentSpec,
128
303
  profile: Profile,
129
304
  session?: SessionConfiguration,
130
305
  ): Promise<string>;
131
306
  turn(request: TurnRequest): Promise<TurnResult>;
307
+ createHistory?(profile: Profile): HistoryCapability | "disabled";
132
308
  reconcile?(request: TurnRequest): Promise<TurnResult | undefined>;
133
309
  send(request: SendRequest): Promise<{ receipt?: string }>;
134
310
  cancel(request: CancelRequest): Promise<void>;
@@ -269,7 +445,37 @@ export interface RunSnapshot {
269
445
  id: string;
270
446
  status: RunStatus;
271
447
  strategy: string;
448
+ /** Mutable display name; absent for legacy or unnamed runs. */
449
+ name?: string;
450
+ /**
451
+ * Operator pause persisted across daemon generations. A stopped run admits no
452
+ * new work until `continueRun` clears it; `status` may still read `paused`
453
+ * for other reasons (quota, recovery, scheduler error).
454
+ */
455
+ stopped?: boolean;
456
+ /** Per-run admission ceiling set by `limitRun`; unset means the group only. */
457
+ maxConcurrent?: number;
458
+ /** Number of in-flight native turns counted against `maxConcurrent`. */
459
+ activeTurns?: number;
460
+ /** Amendment lineage. Immutable once set. */
461
+ predecessorId?: string;
462
+ successorId?: string;
463
+ /** Highest applied plan revision; absent when no amendment was applied. */
464
+ revision?: number;
272
465
  profile?: string;
466
+ /** Canonical project identity captured at start. */
467
+ projectId?: string;
468
+ /** Canonical project directory captured at start. */
469
+ projectDir?: string;
470
+ /**
471
+ * SHA-256 of the script entry that issued the start, as recorded on the run.
472
+ * Absent when the client did not supply one or the run predates the column.
473
+ * The digest is a value the client already read; reading the script source
474
+ * is never needed to expose it.
475
+ */
476
+ scriptDigest?: string;
477
+ /** Script invocation that created this run; shared by every run it started. */
478
+ invocationId?: string;
273
479
  adapter: string;
274
480
  sealed: boolean;
275
481
  waves: WaveSnapshot[];
@@ -288,6 +494,21 @@ export interface EngineEvent {
288
494
  waveId?: string;
289
495
  agentId?: string;
290
496
  data?: unknown;
497
+ /**
498
+ * Durable provenance of the write that produced the event: the daemon
499
+ * generation and, when applicable, the operator action receipt or plan
500
+ * revision it belongs to.
501
+ */
502
+ origin?: EventOrigin;
503
+ }
504
+
505
+ export interface EventOrigin {
506
+ /** Daemon generation that persisted the event. */
507
+ daemon: number;
508
+ /** Operator action receipt that caused the event. */
509
+ actionId?: string;
510
+ /** Plan revision in effect when the event was written. */
511
+ revision?: number;
291
512
  }
292
513
 
293
514
  export interface EventOptions {
@@ -358,6 +579,191 @@ export interface LimitsSnapshot {
358
579
  at: number;
359
580
  }
360
581
 
582
+ // ------------------------------------------------------- operator operations
583
+
584
+ export type OperatorActionKind =
585
+ "pause" | "continue" | "limit" | "retry" | "amend" | "drain" | "recover";
586
+
587
+ /**
588
+ * One durable operator action. Duplicate calls with the same `id` return this
589
+ * record instead of applying the change twice.
590
+ */
591
+ export interface OperationReceipt {
592
+ id: string;
593
+ runId: string;
594
+ kind: OperatorActionKind;
595
+ actor: string;
596
+ reason?: string;
597
+ at: number;
598
+ /** The value before the change (for example the previous concurrency cap). */
599
+ oldValue?: unknown;
600
+ newValue?: unknown;
601
+ /** True when an identical earlier request already applied the change. */
602
+ duplicate: boolean;
603
+ }
604
+
605
+ export interface PauseRunOptions {
606
+ /** Stable receipt id; a repeat of the same id is idempotent. */
607
+ actionId?: string;
608
+ actor?: string;
609
+ reason?: string;
610
+ }
611
+
612
+ export type ContinueRunOptions = PauseRunOptions;
613
+
614
+ export type LimitRunOptions = PauseRunOptions;
615
+
616
+ export interface RetryRunOptions extends PauseRunOptions {
617
+ /** Restrict the retry to these stable step (agent) keys. */
618
+ steps?: string[];
619
+ }
620
+
621
+ /**
622
+ * Plan amendment. The successor run is created from the amended plan and the
623
+ * predecessor keeps its record, results and a forward `successorId` link.
624
+ */
625
+ export interface AmendRunRequest extends PauseRunOptions {
626
+ /** Replacement plan. Validated exactly like a start request plan. */
627
+ input: unknown;
628
+ /** Reuse the source run's strategy by default. */
629
+ strategy?: string;
630
+ /** Explicit successor id; generated when omitted. */
631
+ newRunId?: string;
632
+ /** Defaults to `false`: a preview that persists nothing. */
633
+ apply?: boolean;
634
+ }
635
+
636
+ /** A failed step eligible for `retryRun`. */
637
+ export interface RetryCandidate {
638
+ agentId: string;
639
+ waveId: string;
640
+ status: AgentStatus;
641
+ /** `failed` retries its deterministic failure; `blocked` never ran. */
642
+ kind: "failed" | "blocked";
643
+ /** False when an unknown side effect must be resolved first. */
644
+ eligible: boolean;
645
+ blocker?: string;
646
+ }
647
+
648
+ export interface PlanDiffEntry {
649
+ change: "added" | "removed" | "changed";
650
+ scope: "wave" | "agent" | "artifact" | "profile";
651
+ id: string;
652
+ /** Steps whose retained result stays valid. */
653
+ keep: boolean;
654
+ }
655
+
656
+ export interface AmendPreview {
657
+ runId: string;
658
+ revision: number;
659
+ applied: boolean;
660
+ successorId?: string;
661
+ diff: PlanDiffEntry[];
662
+ /** Completed steps whose result would be reused by the successor. */
663
+ reused: string[];
664
+ /** Pending steps invalidated by a semantic change. */
665
+ invalidated: string[];
666
+ }
667
+
668
+ /** One visible outcome of startup reconciliation. */
669
+ export interface RecoveryItem {
670
+ runId: string;
671
+ /** Monotonic per-run sequence number; stable across restarts. */
672
+ seq: number;
673
+ waveId?: string;
674
+ agentId?: string;
675
+ /** Non-agent step whose effect is unknown or whose verdict is pending. */
676
+ stepId?: string;
677
+ attemptId?: string;
678
+ /** Daemon generation that observed the recovery decision. */
679
+ daemon: number;
680
+ action: "requeued" | "reconciled" | "interrupted" | "unchanged";
681
+ reason: string;
682
+ /**
683
+ * Set when the step must not be retried automatically because a native
684
+ * effect may have occurred. Present means "review before retry".
685
+ */
686
+ blocker?: string;
687
+ at: number;
688
+ }
689
+
690
+ /** Bounded drain outcome. Concurrent or repeated calls share one record. */
691
+ export interface DrainSummary {
692
+ id: string;
693
+ /** `complete` when every admitted step settled inside the grace period. */
694
+ outcome: "complete" | "timeout";
695
+ graceMs: number;
696
+ startedAt: number;
697
+ finishedAt: number;
698
+ /** Default surface: everything admitted by this engine. */
699
+ scope: "daemon" | "run";
700
+ runIds: string[];
701
+ settled: number;
702
+ interrupted: number;
703
+ /** True once the store is released; a second call sees the same summary. */
704
+ closed: boolean;
705
+ }
706
+
707
+ /** Extensible, storage-level lifecycle state for a workflow plan. */
708
+ export type PlanPhaseMode = "wave" | "sequence" | "parallel";
709
+
710
+ export interface PlanPhaseDescriptor {
711
+ id: string;
712
+ index: number;
713
+ /** Name used for inspection; defaults to `id`. */
714
+ name?: string;
715
+ mode: PlanPhaseMode;
716
+ waveIds: string[];
717
+ }
718
+
719
+ export interface PlanRoundDescriptor {
720
+ phaseId: string;
721
+ round: number;
722
+ /** Persisted round decision when a goal gate gated the round. */
723
+ goal?: string;
724
+ reason?: string;
725
+ nextRound?: number;
726
+ evidence?: unknown;
727
+ }
728
+
729
+ export interface PlanStepDescriptor {
730
+ /** Stable step identity: an agent ID or a `<waveId>/<stepId>` step key. */
731
+ id: string;
732
+ phaseId: string;
733
+ waveId: string;
734
+ kind: "agent" | "script" | "gate";
735
+ /** Local step ID for non-agent steps (unique within its wave). */
736
+ stepId?: string;
737
+ /** Steps that must settle before admission. */
738
+ dependsOn?: PlanStepRef[];
739
+ /** Registry handler name and version for a script or gate step. */
740
+ handler?: string;
741
+ handlerVersion?: string;
742
+ /** Declared cwd for a non-agent step. */
743
+ cwd?: string;
744
+ profile?: string;
745
+ model?: string;
746
+ }
747
+
748
+ /**
749
+ * Adapter-neutral strategy envelope persisted with a durable plan. The engine
750
+ * never interprets `payload`; it only round-trips it so recovery can rebind a
751
+ * registered handler by `id` and `version`.
752
+ */
753
+ export interface PlanStrategyEnvelope {
754
+ id: string;
755
+ version: number;
756
+ payload?: unknown;
757
+ }
758
+
759
+ export interface RunPlanMetadata {
760
+ version: number;
761
+ phases: PlanPhaseDescriptor[];
762
+ rounds: PlanRoundDescriptor[];
763
+ steps: PlanStepDescriptor[];
764
+ envelope?: PlanStrategyEnvelope;
765
+ }
766
+
361
767
  export interface ArtifactOutput {
362
768
  id: string;
363
769
  type: ArtifactType;
@@ -377,11 +783,27 @@ export interface EngineOptions {
377
783
  quota?: QuotaOptions;
378
784
  /** Trusted opt-in modules. Omitted or empty means no module surface at all. */
379
785
  modules?: AgentFlowModule[];
786
+ /**
787
+ * Defaults applied by the daemon to every admitted agent before its effective
788
+ * spec is persisted. Omitted means no defaults.
789
+ */
790
+ agentDefaults?: AgentDefaults;
791
+ /**
792
+ * Named subagent presets, selected explicitly by `AgentSpec.preset`. Applied
793
+ * after `agentDefaults` and before the agent's own fields.
794
+ */
795
+ subagents?: Record<string, AgentDefaults>;
380
796
  /**
381
797
  * Optional lifetime module call cap per agent. Omitted means unlimited.
382
798
  * A configured cap still pauses visibly when reached.
383
799
  */
384
800
  moduleCallLimit?: number;
801
+ /**
802
+ * Trusted script and gate handlers, keyed by exact `name@version`. A plan that
803
+ * references an unregistered handler is rejected before admission; the engine
804
+ * never executes executable text carried by a template.
805
+ */
806
+ handlers?: PlanHandler[];
385
807
  }
386
808
 
387
809
  export interface Engine {
@@ -407,6 +829,36 @@ export interface Engine {
407
829
  options?: ResumeOptions,
408
830
  ): Promise<RunSnapshot>;
409
831
 
832
+ // ------------------------------------------------- operator lifecycle (RPC)
833
+ /** Persist an operator stop. Known in-flight turns settle by default. */
834
+ pauseRun(id: string, options?: PauseRunOptions): Promise<OperationReceipt>;
835
+ /** Reopen admission and continue persisted ready work. */
836
+ continueRun(
837
+ id: string,
838
+ options?: ContinueRunOptions,
839
+ ): Promise<OperationReceipt>;
840
+ /** Set the per-run admission ceiling without interrupting admitted turns. */
841
+ limitRun(
842
+ id: string,
843
+ maxConcurrent: number,
844
+ options?: LimitRunOptions,
845
+ ): Promise<OperationReceipt>;
846
+ /** Retry eligible failed/blocked steps; never replays unknown effects. */
847
+ retryRun(id: string, options?: RetryRunOptions): Promise<OperationReceipt>;
848
+ /** Preview or apply a plan amendment and link the successor run. */
849
+ amendRun(id: string, request: AmendRunRequest): Promise<AmendPreview>;
850
+ /** Bounded graceful drain; a second call returns the same summary. */
851
+ drain(graceMs?: number): Promise<DrainSummary>;
852
+
853
+ /** Durable operator receipts, oldest first. */
854
+ operations(id: string): OperationReceipt[];
855
+ /** Startup reconciliation items for one run (empty before recovery). */
856
+ recovery(id: string): RecoveryItem[];
857
+ /** Persisted plan metadata for a run. */
858
+ plan(id: string): RunPlanMetadata | undefined;
859
+ /** Durable script and gate results for a run, in execution order. */
860
+ steps(id: string): PlanStepResult[];
861
+
410
862
  limits(): LimitsSnapshot;
411
863
  output(id: string, artifactId: string): ArtifactOutput;
412
864
  close(): Promise<void>;
@@ -421,11 +873,18 @@ export interface ServeOptions extends EngineOptions {
421
873
  rpc?: {
422
874
  maxRequestBytes?: number;
423
875
  idleTimeoutMs?: number;
876
+ /** Grace used by a drain that does not carry its own deadline. */
877
+ drainGraceMs?: number;
424
878
  };
425
879
  }
426
880
 
427
881
  export interface ServeHandle {
428
882
  socketPath: string;
883
+ /**
884
+ * Graceful drain: starts or joins the single shutdown operation, releases
885
+ * the socket path and the lock, and resolves with the drain report.
886
+ */
887
+ drain(options?: DrainOptions): Promise<DrainReport>;
429
888
  close(): Promise<void>;
430
889
  }
431
890
 
@@ -463,5 +922,28 @@ export interface Client {
463
922
 
464
923
  limits(): Promise<LimitsSnapshot>;
465
924
  output(id: string, artifactId: string): Promise<ArtifactOutput>;
925
+
926
+ /**
927
+ * Provisional lifecycle operators. They resolve against the durable engine
928
+ * and fail with `capability` while the daemon does not implement them.
929
+ */
930
+ pauseRun(id: string, options?: PauseRunOptions): Promise<OperationReceipt>;
931
+ continueRun(
932
+ id: string,
933
+ options?: ContinueRunOptions,
934
+ ): Promise<OperationReceipt>;
935
+ limitRun(
936
+ id: string,
937
+ maxConcurrent: number,
938
+ options?: LimitRunOptions,
939
+ ): Promise<OperationReceipt>;
940
+ retryRun(id: string, options?: RetryRunOptions): Promise<OperationReceipt>;
941
+ amendRun(id: string, amendment: AmendRunRequest): Promise<AmendPreview>;
942
+ operations(id: string): Promise<OperationReceipt[]>;
943
+ recovery(id: string): Promise<RecoveryItem[]>;
944
+
945
+ /** Asks the daemon to drain gracefully and returns its report. */
946
+ drain(options?: DrainOptions): Promise<DrainReport>;
947
+
466
948
  close(): Promise<void>;
467
949
  }