@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/protocol.ts CHANGED
@@ -1,7 +1,18 @@
1
+ import { AgentFlowError } from "./errors";
2
+ import { assertSerializable } from "./json";
1
3
  import type {
4
+ AmendPreview,
5
+ AmendRunRequest,
2
6
  AppendRequest,
7
+ ContinueRunOptions,
8
+ DrainSummary,
3
9
  EventOptions,
10
+ LimitRunOptions,
11
+ OperationReceipt,
12
+ PauseRunOptions,
13
+ RecoveryItem,
4
14
  ResumeOptions,
15
+ RetryRunOptions,
5
16
  RunRequest,
6
17
  SendOptions,
7
18
  } from "./types";
@@ -19,6 +30,14 @@ export const RPC_ACTIONS = [
19
30
  "resume",
20
31
  "limits",
21
32
  "output",
33
+ "pauseRun",
34
+ "continueRun",
35
+ "limitRun",
36
+ "retryRun",
37
+ "amendRun",
38
+ "drain",
39
+ "operations",
40
+ "recovery",
22
41
  ] as const;
23
42
 
24
43
  export type RpcAction = (typeof RPC_ACTIONS)[number];
@@ -52,6 +71,152 @@ export interface RpcActionArgs {
52
71
  resume: { id: string; agentId: string } & ResumeOptions;
53
72
  limits: Record<string, never>;
54
73
  output: { id: string; artifactId: string };
74
+ pauseRun: { id: string } & PauseRunOptions;
75
+ continueRun: { id: string } & ContinueRunOptions;
76
+ limitRun: { id: string; maxConcurrent: number } & LimitRunOptions;
77
+ retryRun: { id: string } & RetryRunOptions;
78
+ amendRun: { id: string; amendment: AmendRunRequest };
79
+ /** Daemon-level drain. Carries no run identity. */
80
+ drain: { graceMs?: number };
81
+ operations: { id: string } & OperatorOptions;
82
+ recovery: { id: string } & OperatorOptions;
83
+ }
84
+
85
+ /** Shared `actionId`, `actor` and `reason` fields of an operator action. */
86
+ export type OperatorOptions = PauseRunOptions;
87
+
88
+ /**
89
+ * Operator result kinds. Named aliases keep the server, the client and tests
90
+ * on the exact wire shapes the engine already declares in `types.ts`.
91
+ */
92
+ export type OperatorReceipt = OperationReceipt;
93
+ export type OperatorAmendPreview = AmendPreview;
94
+ export type OperatorDrainSummary = DrainSummary;
95
+ export type OperatorRecoveryItem = RecoveryItem;
96
+
97
+ /** Client-side types for the additive operator RPCs. */
98
+ export type AmendRunPayload = AmendRunRequest;
99
+ export type DrainOptions = { graceMs?: number };
100
+ export type DrainReport = DrainSummary;
101
+
102
+ /** Compatible aliases kept for the package export surface. */
103
+ export type AmendRunArgs = AmendRunRequest;
104
+ export type DrainOutcome = DrainSummary["outcome"];
105
+
106
+ /**
107
+ * Graceful daemon drain exposed by the server handle. The server owns the
108
+ * listener, the lock and the reply ordering; the engine owns the durable
109
+ * `DrainSummary` and its recovery reasons.
110
+ */
111
+ export interface Drainer {
112
+ drain(options?: DrainOptions): Promise<DrainReport>;
113
+ }
114
+
115
+ /**
116
+ * Actions admitted while the daemon drains. They are read-only or they finish
117
+ * the drain itself; every other mutation is rejected.
118
+ */
119
+ export const DRAIN_READONLY_ACTIONS: ReadonlySet<string> = new Set([
120
+ "ping",
121
+ "runs",
122
+ "status",
123
+ "events",
124
+ "limits",
125
+ "output",
126
+ "operations",
127
+ "recovery",
128
+ "drain",
129
+ ]);
130
+
131
+ export function isDrainReadOnlyAction(action: string): boolean {
132
+ return DRAIN_READONLY_ACTIONS.has(action);
133
+ }
134
+
135
+ /**
136
+ * Parses the `amendRun` payload. Validation is shallow by design: the engine
137
+ * owns plan semantics and returns the canonical error for a bad plan.
138
+ */
139
+ export function parseAmendRunArgs(args: Record<string, unknown>): {
140
+ id: string;
141
+ amendment: AmendRunRequest;
142
+ } {
143
+ const raw = args.amendment;
144
+ if (!isRecord(raw)) {
145
+ throw new AgentFlowError(
146
+ "ipc",
147
+ "amendRun args.amendment must be an object",
148
+ );
149
+ }
150
+ if (!("input" in raw) || raw.input === undefined) {
151
+ throw new AgentFlowError(
152
+ "ipc",
153
+ "amendRun args.amendment.input is required",
154
+ );
155
+ }
156
+ try {
157
+ assertSerializable(raw.input, "amendRun args.amendment.input");
158
+ } catch (error) {
159
+ throw new AgentFlowError(
160
+ "ipc",
161
+ `amendRun args.amendment.input is not serializable: ${(error as Error).message}`,
162
+ );
163
+ }
164
+ const amendment: AmendRunRequest = { input: raw.input };
165
+ const newRunId = optionalAmendField(raw, "newRunId");
166
+ if (newRunId !== undefined) amendment.newRunId = newRunId;
167
+ const strategy = optionalAmendField(raw, "strategy");
168
+ if (strategy !== undefined) amendment.strategy = strategy;
169
+ const apply = raw.apply;
170
+ if (apply !== undefined) {
171
+ if (typeof apply !== "boolean") {
172
+ throw new AgentFlowError(
173
+ "ipc",
174
+ "amendRun args.amendment.apply must be a boolean",
175
+ );
176
+ }
177
+ amendment.apply = apply;
178
+ }
179
+ Object.assign(amendment, operatorOptions(raw, "amendRun"));
180
+ return { id: typeof args.id === "string" ? args.id : "", amendment };
181
+ }
182
+
183
+ /** Shared `actionId`, `actor` and `reason` fields of every operator action. */
184
+ export function operatorOptions(
185
+ args: Record<string, unknown>,
186
+ action: string,
187
+ ): PauseRunOptions {
188
+ const options: PauseRunOptions = {};
189
+ for (const key of ["actionId", "actor", "reason"] as const) {
190
+ const value = args[key];
191
+ if (value === undefined) continue;
192
+ if (typeof value !== "string" || value === "") {
193
+ throw new AgentFlowError(
194
+ "ipc",
195
+ `${action} args.${key} must be a non-empty string`,
196
+ );
197
+ }
198
+ options[key] = value;
199
+ }
200
+ return options;
201
+ }
202
+
203
+ function optionalAmendField(
204
+ args: Record<string, unknown>,
205
+ key: string,
206
+ ): string | undefined {
207
+ const value = args[key];
208
+ if (value === undefined) return undefined;
209
+ if (typeof value !== "string" || value === "") {
210
+ throw new AgentFlowError(
211
+ "ipc",
212
+ `amendRun args.amendment.${key} must be a non-empty string`,
213
+ );
214
+ }
215
+ return value;
216
+ }
217
+
218
+ function isRecord(value: unknown): value is Record<string, unknown> {
219
+ return value !== null && typeof value === "object" && !Array.isArray(value);
55
220
  }
56
221
 
57
222
  export type RpcActionHandlers = {