@hivemagnus/ardent 3.1.3 → 3.2.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.
- package/README.md +504 -16
- package/dist/a2a-client.d.ts +26 -4
- package/dist/a2a-client.js +91 -23
- package/dist/a2a-push-inbox.d.ts +2 -0
- package/dist/a2a-push-inbox.js +7 -0
- package/dist/a2a-push.d.ts +1 -1
- package/dist/a2a-push.js +26 -18
- package/dist/a2a-server.d.ts +17 -0
- package/dist/a2a-server.js +91 -14
- package/dist/a2a.d.ts +19 -1
- package/dist/a2a.js +197 -27
- package/dist/api.d.ts +2 -0
- package/dist/api.js +160 -22
- package/dist/artifact-manifests.d.ts +344 -5
- package/dist/artifact-manifests.js +413 -20
- package/dist/artifact-transfer.d.ts +113 -0
- package/dist/artifact-transfer.js +517 -0
- package/dist/artifacts.d.ts +86 -0
- package/dist/artifacts.js +249 -1
- package/dist/backend-events.d.ts +2 -1
- package/dist/backend-events.js +1 -1
- package/dist/channel-catchup.d.ts +67 -0
- package/dist/channel-catchup.js +108 -0
- package/dist/channels.d.ts +35 -0
- package/dist/channels.js +152 -4
- package/dist/cli.js +28 -3
- package/dist/codex-protocol.d.ts +22 -0
- package/dist/codex-protocol.js +19 -0
- package/dist/codex.d.ts +32 -3
- package/dist/codex.js +91 -17
- package/dist/config.d.ts +25 -2
- package/dist/config.js +41 -4
- package/dist/development.d.ts +6 -6
- package/dist/execution-state.d.ts +245 -0
- package/dist/execution-state.js +236 -0
- package/dist/execution.d.ts +135 -0
- package/dist/execution.js +178 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +5 -3
- package/dist/job-engine.d.ts +2 -1
- package/dist/job-engine.js +33 -4
- package/dist/lineage.d.ts +106 -0
- package/dist/lineage.js +145 -0
- package/dist/llamacpp.d.ts +32 -2
- package/dist/llamacpp.js +69 -33
- package/dist/mattermost.d.ts +8 -0
- package/dist/mattermost.js +250 -36
- package/dist/mission-tools.js +41 -5
- package/dist/missions.d.ts +5 -1
- package/dist/missions.js +10 -5
- package/dist/plugin-evidence.js +2 -1
- package/dist/plugin-sdk.d.ts +3 -0
- package/dist/plugin-sdk.js +3 -0
- package/dist/plugins.d.ts +108 -2
- package/dist/plugins.js +131 -1
- package/dist/preview.d.ts +1 -0
- package/dist/preview.js +4 -1
- package/dist/recovery.d.ts +155 -0
- package/dist/recovery.js +216 -0
- package/dist/research.d.ts +1 -1
- package/dist/retry.d.ts +143 -0
- package/dist/retry.js +127 -0
- package/dist/runtime.d.ts +56 -4
- package/dist/runtime.js +727 -125
- package/dist/store.d.ts +62 -7
- package/dist/store.js +86 -15
- package/dist/team-artifacts.d.ts +130 -112
- package/dist/team-artifacts.js +123 -151
- package/dist/team-child.d.ts +4 -1
- package/dist/team-child.js +21 -2
- package/dist/team-contract.d.ts +1 -1
- package/dist/telegram.d.ts +2 -0
- package/dist/telegram.js +83 -47
- package/dist/tool-scheduler.d.ts +84 -0
- package/dist/tool-scheduler.js +226 -0
- package/dist/tools.d.ts +78 -4
- package/dist/tools.js +34 -7
- package/package.json +16 -9
- package/profiles/assistant/config.yaml +2 -1
package/dist/plugins.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { ArdentRuntime, TaskEvidence } from './runtime.js';
|
|
|
5
5
|
import type { ResearchBrief } from './research.js';
|
|
6
6
|
import type { SkillInfo } from './skills.js';
|
|
7
7
|
import type { Task, TaskEvent } from './store.js';
|
|
8
|
-
import { defineTool, ToolRegistry, type Tool, type ToolResult } from './tools.js';
|
|
8
|
+
import { defineTool, ToolRegistry, type ExecutionContext, type Tool, type ToolEffect, type ToolResult } from './tools.js';
|
|
9
9
|
import { TeamEnvelope } from './team-contract.js';
|
|
10
10
|
import type { HermesTeamCard } from './hermes-compat.js';
|
|
11
11
|
/** Everything a plugin may use. Plugins receive no authority beyond the profile's own runtime. */
|
|
@@ -53,6 +53,59 @@ export type ArdentTaskPluginContext = ArdentPluginContext & {
|
|
|
53
53
|
task: Task;
|
|
54
54
|
envelope?: TeamEnvelope;
|
|
55
55
|
baseTools: readonly Tool[];
|
|
56
|
+
/** Attribution for the causal tree this task belongs to (absent for tasks accepted before ancestry was recorded). Never grants access. */
|
|
57
|
+
execution?: ExecutionContext;
|
|
58
|
+
};
|
|
59
|
+
/** An authenticated, claimed channel message (a frozen copy). The text is not included. */
|
|
60
|
+
export type ArdentChannelMessage = {
|
|
61
|
+
platform: 'telegram' | 'mattermost';
|
|
62
|
+
account: string;
|
|
63
|
+
update_id: string;
|
|
64
|
+
channel_type: string;
|
|
65
|
+
/** `<platform>:<account>:<user>`, the task caller for this message. */
|
|
66
|
+
caller: string;
|
|
67
|
+
/** `<platform>:<account>:<conversation>:<thread>`. */
|
|
68
|
+
session_key: string;
|
|
69
|
+
received_ms: number;
|
|
70
|
+
execution?: ExecutionContext;
|
|
71
|
+
};
|
|
72
|
+
/** A channel reply recorded for delivery (a frozen copy). */
|
|
73
|
+
export type ArdentChannelDelivery = {
|
|
74
|
+
delivery_id: string;
|
|
75
|
+
task_id: string | null;
|
|
76
|
+
platform: 'telegram' | 'mattermost';
|
|
77
|
+
account: string;
|
|
78
|
+
update_id: string;
|
|
79
|
+
text: string;
|
|
80
|
+
/** 1: Ardent records a delivery once and never resends it automatically. */
|
|
81
|
+
attempt: number;
|
|
82
|
+
};
|
|
83
|
+
export type ArdentDeliveryOutcome = {
|
|
84
|
+
state: 'sent' | 'failed' | 'unknown';
|
|
85
|
+
external_id: string | null;
|
|
86
|
+
};
|
|
87
|
+
/** One model tool call after context scoping and argument validation (a frozen copy). */
|
|
88
|
+
export type ArdentToolCall = {
|
|
89
|
+
task_id: string;
|
|
90
|
+
call_id: string;
|
|
91
|
+
name: string;
|
|
92
|
+
/** Validated arguments. */
|
|
93
|
+
arguments: unknown;
|
|
94
|
+
effect: ToolEffect;
|
|
95
|
+
/** `sha256("<task_id>:<call_id>")`, the key the tool receives. */
|
|
96
|
+
idempotency_key: string;
|
|
97
|
+
execution?: ExecutionContext;
|
|
98
|
+
};
|
|
99
|
+
/** The only result `beforeToolCall` may return. Anything else is ignored. */
|
|
100
|
+
export type ArdentToolDenial = {
|
|
101
|
+
deny: {
|
|
102
|
+
code: string;
|
|
103
|
+
message: string;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
export type ArdentToolCancellation = {
|
|
107
|
+
dispatched: boolean;
|
|
108
|
+
effect: 'none' | 'unknown';
|
|
56
109
|
};
|
|
57
110
|
export type ArdentPromptInput = {
|
|
58
111
|
prompt: string;
|
|
@@ -142,8 +195,26 @@ export interface ArdentPlugin {
|
|
|
142
195
|
systemPrompt?(ctx: ArdentTaskPluginContext, input: ArdentPromptInput): Promise<string> | string;
|
|
143
196
|
/** Restrict an already admitted memory read; this cannot grant another scope. */
|
|
144
197
|
allowMemory?(ctx: ArdentTaskPluginContext, scope: 'private' | 'shared', projectId?: string): boolean;
|
|
145
|
-
/** Observe an actual tool result, retaining evidence within this task only. */
|
|
198
|
+
/** Observe an actual tool result, retaining evidence within this task only. A throw is journaled and ignored. */
|
|
146
199
|
observeToolResult?(ctx: ArdentTaskPluginContext, name: string, result: ToolResult, evidence: TaskEvidence): void;
|
|
200
|
+
/** Observe a claimed channel message before any command or task handling. */
|
|
201
|
+
messageReceived?(ctx: ArdentPluginContext, message: ArdentChannelMessage): void | Promise<void>;
|
|
202
|
+
/** Observe a channel message once its task is accepted and bound to it. */
|
|
203
|
+
messagePersisted?(ctx: ArdentPluginContext, message: ArdentChannelMessage, task: Task): void | Promise<void>;
|
|
204
|
+
/** Rewrite a reply's text before it is recorded and sent (1 byte to 64 KiB). Any failure sends the original text. */
|
|
205
|
+
transformOutbound?(ctx: ArdentPluginContext, delivery: ArdentChannelDelivery): {
|
|
206
|
+
text: string;
|
|
207
|
+
} | void | Promise<{
|
|
208
|
+
text: string;
|
|
209
|
+
} | void>;
|
|
210
|
+
/** Observe the recorded outcome of a channel delivery. */
|
|
211
|
+
messageDelivered?(ctx: ArdentPluginContext, delivery: ArdentChannelDelivery, outcome: ArdentDeliveryOutcome): void | Promise<void>;
|
|
212
|
+
/** Deny a validated tool call before it is invoked. A throw or a 5 s timeout denies it. */
|
|
213
|
+
beforeToolCall?(ctx: ArdentTaskPluginContext, call: ArdentToolCall): void | ArdentToolDenial | Promise<void | ArdentToolDenial>;
|
|
214
|
+
/** Observe a journaled tool result, successful or not. */
|
|
215
|
+
afterToolCall?(ctx: ArdentTaskPluginContext, call: ArdentToolCall, result: ToolResult): void | Promise<void>;
|
|
216
|
+
/** Clean up after a call stopped by cancellation, queued (`dispatched: false`) or in flight. */
|
|
217
|
+
toolCallCancelled?(ctx: ArdentTaskPluginContext, call: ArdentToolCall, outcome: ArdentToolCancellation): void | Promise<void>;
|
|
147
218
|
/** Additional validation before terminal success; throwing prevents completion. */
|
|
148
219
|
beforeFinish?(ctx: ArdentTaskPluginContext, input: ArdentCompletionInput): Promise<void> | void;
|
|
149
220
|
/** Release per-task resources after execution, including failed/cancelled tasks. */
|
|
@@ -188,3 +259,38 @@ export declare function pluginJobHandlers(plugins: readonly ArdentPlugin[], ctx:
|
|
|
188
259
|
export declare function pluginChannelAdapter(plugins: readonly ArdentPlugin[], ctx: ArdentPluginContext, channel: 'telegram' | 'mattermost', jobs?: JobEngine): ArdentChannelAdapter | undefined;
|
|
189
260
|
/** Task additions cannot bypass context, preview effect, peer grant or restrictive filters. */
|
|
190
261
|
export declare function pluginTaskTools(plugins: readonly ArdentPlugin[], ctx: ArdentTaskPluginContext): ToolRegistry;
|
|
262
|
+
/** The longest a lifecycle hook may run before it counts as failed. */
|
|
263
|
+
export declare const PLUGIN_HOOK_TIMEOUT_MS = 5000;
|
|
264
|
+
/** The largest text `transformOutbound` may return. */
|
|
265
|
+
export declare const MAX_OUTBOUND_TEXT_BYTES: number;
|
|
266
|
+
export type PluginHookName = 'observeToolResult' | 'messageReceived' | 'messagePersisted' | 'transformOutbound' | 'messageDelivered' | 'beforeToolCall' | 'afterToolCall' | 'toolCallCancelled';
|
|
267
|
+
/** Records a failed hook (`plugin_hook_failed`); it must not throw. */
|
|
268
|
+
export type PluginHookFailureSink = (plugin: string, hook: PluginHookName) => void;
|
|
269
|
+
/** A deep-frozen copy, so a hook cannot change what the runtime keeps using. */
|
|
270
|
+
export declare function frozenCopy<T>(value: T): T;
|
|
271
|
+
/**
|
|
272
|
+
* Run one observer hook on every plugin, in registration order (reverse for
|
|
273
|
+
* cleanup). A throw, rejection or timeout is reported and never propagates.
|
|
274
|
+
*/
|
|
275
|
+
export declare function runPluginObservers(plugins: readonly ArdentPlugin[], hook: PluginHookName, invoke: (plugin: ArdentPlugin) => unknown, onFailure: PluginHookFailureSink, options?: {
|
|
276
|
+
reverse?: boolean;
|
|
277
|
+
}): Promise<void>;
|
|
278
|
+
/** Synchronous `observeToolResult`: a throw is reported and ignored. */
|
|
279
|
+
export declare function observePluginToolResult(plugins: readonly ArdentPlugin[], ctx: ArdentTaskPluginContext, name: string, result: ToolResult, evidence: TaskEvidence, onFailure: PluginHookFailureSink): void;
|
|
280
|
+
/**
|
|
281
|
+
* Ask every plugin, in registration order, whether a validated call may run.
|
|
282
|
+
* The first denial wins. A throw, timeout or malformed denial denies the call.
|
|
283
|
+
*/
|
|
284
|
+
export declare function pluginBeforeToolCall(plugins: readonly ArdentPlugin[], ctx: ArdentTaskPluginContext, call: ArdentToolCall, onFailure: PluginHookFailureSink): Promise<{
|
|
285
|
+
plugin: string;
|
|
286
|
+
code: string;
|
|
287
|
+
message: string;
|
|
288
|
+
} | undefined>;
|
|
289
|
+
/**
|
|
290
|
+
* Chain `transformOutbound` in registration order. Any throw, timeout or
|
|
291
|
+
* invalid text (empty or over 64 KiB) fails closed to the original text.
|
|
292
|
+
*/
|
|
293
|
+
export declare function pluginTransformOutbound(plugins: readonly ArdentPlugin[], ctx: ArdentPluginContext, delivery: ArdentChannelDelivery, onFailure: PluginHookFailureSink): Promise<{
|
|
294
|
+
text: string;
|
|
295
|
+
plugins: string[];
|
|
296
|
+
}>;
|
package/dist/plugins.js
CHANGED
|
@@ -17,7 +17,7 @@ function isRecord(value) {
|
|
|
17
17
|
function assertPlugin(plugin, source) {
|
|
18
18
|
if (!isRecord(plugin) || typeof plugin.name !== 'string' || !pluginName.test(plugin.name))
|
|
19
19
|
throw new Error(`Plugin ${source} must export an object with a lowercase name`);
|
|
20
|
-
for (const hook of ['registerTools', 'routes', 'jobHandlers', 'jobOwnership', 'peerTool', 'taskTools', 'filterTool', 'teamEnvelope', 'admitTeamEnvelope', 'channelAdapter', 'httpRoutes', 'systemPrompt', 'allowMemory', 'observeToolResult', 'beforeFinish', 'taskSettled', 'taskExecutor', 'inputImages', 'teamEvidence', 'teamCard', 'prepareGateway', 'start', 'drain', 'close'])
|
|
20
|
+
for (const hook of ['registerTools', 'routes', 'jobHandlers', 'jobOwnership', 'peerTool', 'taskTools', 'filterTool', 'teamEnvelope', 'admitTeamEnvelope', 'channelAdapter', 'httpRoutes', 'systemPrompt', 'allowMemory', 'observeToolResult', 'messageReceived', 'messagePersisted', 'transformOutbound', 'messageDelivered', 'beforeToolCall', 'afterToolCall', 'toolCallCancelled', 'beforeFinish', 'taskSettled', 'taskExecutor', 'inputImages', 'teamEvidence', 'teamCard', 'prepareGateway', 'start', 'drain', 'close'])
|
|
21
21
|
if (plugin[hook] !== undefined && typeof plugin[hook] !== 'function')
|
|
22
22
|
throw new Error(`Plugin ${plugin.name} hook ${hook} must be a function`);
|
|
23
23
|
if (plugin.cliCommands !== undefined) {
|
|
@@ -200,3 +200,133 @@ export function pluginTaskTools(plugins, ctx) {
|
|
|
200
200
|
registry.register(tool);
|
|
201
201
|
return registry;
|
|
202
202
|
}
|
|
203
|
+
/** The longest a lifecycle hook may run before it counts as failed. */
|
|
204
|
+
export const PLUGIN_HOOK_TIMEOUT_MS = 5_000;
|
|
205
|
+
/** The largest text `transformOutbound` may return. */
|
|
206
|
+
export const MAX_OUTBOUND_TEXT_BYTES = 64 * 1024;
|
|
207
|
+
/** A deep-frozen copy, so a hook cannot change what the runtime keeps using. */
|
|
208
|
+
export function frozenCopy(value) {
|
|
209
|
+
const copy = structuredClone(value);
|
|
210
|
+
const freeze = (item) => {
|
|
211
|
+
if (!item || typeof item !== 'object' || Object.isFrozen(item))
|
|
212
|
+
return;
|
|
213
|
+
if (ArrayBuffer.isView(item))
|
|
214
|
+
return; // Typed arrays cannot be frozen; they are already copies.
|
|
215
|
+
Object.freeze(item);
|
|
216
|
+
for (const key of Reflect.ownKeys(item))
|
|
217
|
+
freeze(Reflect.get(item, key));
|
|
218
|
+
};
|
|
219
|
+
freeze(copy);
|
|
220
|
+
return copy;
|
|
221
|
+
}
|
|
222
|
+
async function bounded(run, timeoutMs = PLUGIN_HOOK_TIMEOUT_MS) {
|
|
223
|
+
let timer;
|
|
224
|
+
try {
|
|
225
|
+
return await Promise.race([
|
|
226
|
+
Promise.resolve().then(run),
|
|
227
|
+
new Promise((_resolve, reject) => { timer = setTimeout(() => reject(new Error('Plugin hook timed out')), timeoutMs); })
|
|
228
|
+
]);
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
if (timer)
|
|
232
|
+
clearTimeout(timer);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Run one observer hook on every plugin, in registration order (reverse for
|
|
237
|
+
* cleanup). A throw, rejection or timeout is reported and never propagates.
|
|
238
|
+
*/
|
|
239
|
+
export async function runPluginObservers(plugins, hook, invoke, onFailure, options = {}) {
|
|
240
|
+
for (const plugin of options.reverse ? [...plugins].reverse() : plugins) {
|
|
241
|
+
if (typeof plugin[hook] !== 'function')
|
|
242
|
+
continue;
|
|
243
|
+
try {
|
|
244
|
+
await bounded(() => invoke(plugin));
|
|
245
|
+
}
|
|
246
|
+
catch {
|
|
247
|
+
try {
|
|
248
|
+
onFailure(plugin.name, hook);
|
|
249
|
+
}
|
|
250
|
+
catch { /* A failure record must not fail the caller. */ }
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/** Synchronous `observeToolResult`: a throw is reported and ignored. */
|
|
255
|
+
export function observePluginToolResult(plugins, ctx, name, result, evidence, onFailure) {
|
|
256
|
+
for (const plugin of plugins) {
|
|
257
|
+
if (!plugin.observeToolResult)
|
|
258
|
+
continue;
|
|
259
|
+
try {
|
|
260
|
+
plugin.observeToolResult(ctx, name, result, evidence);
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
try {
|
|
264
|
+
onFailure(plugin.name, 'observeToolResult');
|
|
265
|
+
}
|
|
266
|
+
catch { /* ignored */ }
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const denialCode = /^[A-Z][A-Z0-9_]{1,63}$/;
|
|
271
|
+
/**
|
|
272
|
+
* Ask every plugin, in registration order, whether a validated call may run.
|
|
273
|
+
* The first denial wins. A throw, timeout or malformed denial denies the call.
|
|
274
|
+
*/
|
|
275
|
+
export async function pluginBeforeToolCall(plugins, ctx, call, onFailure) {
|
|
276
|
+
for (const plugin of plugins) {
|
|
277
|
+
const hook = plugin.beforeToolCall;
|
|
278
|
+
if (!hook)
|
|
279
|
+
continue;
|
|
280
|
+
let verdict;
|
|
281
|
+
try {
|
|
282
|
+
verdict = await bounded(() => hook.call(plugin, ctx, frozenCopy(call)));
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
try {
|
|
286
|
+
onFailure(plugin.name, 'beforeToolCall');
|
|
287
|
+
}
|
|
288
|
+
catch { /* ignored */ }
|
|
289
|
+
return { plugin: plugin.name, code: 'TOOL_DENIED_BY_PLUGIN', message: `Plugin ${plugin.name} could not approve this call` };
|
|
290
|
+
}
|
|
291
|
+
if (!verdict || typeof verdict !== 'object' || !('deny' in verdict))
|
|
292
|
+
continue;
|
|
293
|
+
const parsed = z.object({ deny: z.object({ code: z.string().regex(denialCode), message: z.string().min(1).max(1000) }) }).safeParse(verdict);
|
|
294
|
+
return parsed.success ? { plugin: plugin.name, ...parsed.data.deny }
|
|
295
|
+
: { plugin: plugin.name, code: 'TOOL_DENIED_BY_PLUGIN', message: `Plugin ${plugin.name} denied this call` };
|
|
296
|
+
}
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Chain `transformOutbound` in registration order. Any throw, timeout or
|
|
301
|
+
* invalid text (empty or over 64 KiB) fails closed to the original text.
|
|
302
|
+
*/
|
|
303
|
+
export async function pluginTransformOutbound(plugins, ctx, delivery, onFailure) {
|
|
304
|
+
let text = delivery.text;
|
|
305
|
+
const applied = [];
|
|
306
|
+
for (const plugin of plugins) {
|
|
307
|
+
const hook = plugin.transformOutbound;
|
|
308
|
+
if (!hook)
|
|
309
|
+
continue;
|
|
310
|
+
let output;
|
|
311
|
+
try {
|
|
312
|
+
output = await bounded(() => hook.call(plugin, ctx, frozenCopy({ ...delivery, text })));
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
output = null;
|
|
316
|
+
}
|
|
317
|
+
if (output === undefined)
|
|
318
|
+
continue;
|
|
319
|
+
const parsed = z.object({ text: z.string().refine(value => value.trim().length > 0 && Buffer.byteLength(value) <= MAX_OUTBOUND_TEXT_BYTES) }).safeParse(output);
|
|
320
|
+
if (!parsed.success) {
|
|
321
|
+
try {
|
|
322
|
+
onFailure(plugin.name, 'transformOutbound');
|
|
323
|
+
}
|
|
324
|
+
catch { /* ignored */ }
|
|
325
|
+
return { text: delivery.text, plugins: [] };
|
|
326
|
+
}
|
|
327
|
+
if (parsed.data.text !== text)
|
|
328
|
+
applied.push(plugin.name);
|
|
329
|
+
text = parsed.data.text;
|
|
330
|
+
}
|
|
331
|
+
return { text, plugins: applied };
|
|
332
|
+
}
|
package/dist/preview.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export declare const PreviewSubmit: z.ZodObject<{
|
|
|
20
20
|
}, z.core.$strict>>;
|
|
21
21
|
session_id: z.ZodOptional<z.ZodString>;
|
|
22
22
|
idempotency_key: z.ZodOptional<z.ZodString>;
|
|
23
|
+
execution: z.ZodOptional<z.ZodUnknown>;
|
|
23
24
|
}, z.core.$strict>;
|
|
24
25
|
export type PreviewSubmit = z.infer<typeof PreviewSubmit>;
|
|
25
26
|
/** Accept a preview task; its overrides are journaled atomically with it. */
|
package/dist/preview.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { ExecutionInput } from './execution.js';
|
|
2
3
|
/** Artifacts a preview produces expire after this long. */
|
|
3
4
|
export const PREVIEW_ARTIFACT_TTL_SECONDS = 24 * 3600;
|
|
4
5
|
const PREVIEW_EVENT = 'preview';
|
|
@@ -13,7 +14,9 @@ export const PreviewSubmit = z.object({
|
|
|
13
14
|
prompt: z.string().trim().min(1).max(16000),
|
|
14
15
|
overrides: PreviewOverrides.default({}),
|
|
15
16
|
session_id: z.string().guid().optional(),
|
|
16
|
-
idempotency_key: z.string().min(1).max(128).optional()
|
|
17
|
+
idempotency_key: z.string().min(1).max(128).optional(),
|
|
18
|
+
/** Optional execution context v1 (attribution only). An invalid value is treated as absent. */
|
|
19
|
+
execution: ExecutionInput.optional()
|
|
17
20
|
}).strict();
|
|
18
21
|
/** Accept a preview task; its overrides are journaled atomically with it. */
|
|
19
22
|
export function acceptPreview(accept, store, input) {
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { RecoveryAction, RecoveryCheckpoint, RecoveryDecision, RecoveryReason, RecoveryStatus, type ExecutionState, type TaskRecoveryRow, type ToolInvocation } from './execution-state.js';
|
|
3
|
+
import type { Task, TaskStore } from './store.js';
|
|
4
|
+
import type { Tool } from './tools.js';
|
|
5
|
+
/**
|
|
6
|
+
* Execution recovery: what happens to a task that was `running` when the
|
|
7
|
+
* process stopped. The shared constraint is that an unknown effect needs
|
|
8
|
+
* reconciliation; it is never permission to retry. A write is never repeated
|
|
9
|
+
* merely because execution resumed.
|
|
10
|
+
*
|
|
11
|
+
* The planner runs before any queued work is admitted and applies these rules
|
|
12
|
+
* in order to each interrupted task:
|
|
13
|
+
*
|
|
14
|
+
* 1. `cancel_requested` journaled: `cancelled` when no effect is uncertain,
|
|
15
|
+
* otherwise held (`CANCELLED_WITH_UNSETTLED_EFFECT`).
|
|
16
|
+
* 2. Deadline passed during the outage: `failed` when no effect is uncertain,
|
|
17
|
+
* otherwise held. The deadline is never extended automatically.
|
|
18
|
+
* 3. Recovery disabled: `unknown`, as before recovery existed (`RECOVERY_DISABLED`).
|
|
19
|
+
* 4. An extension executor with a possible effect ran, or a preview task: held, unsupported.
|
|
20
|
+
* 5. Codex: resume a verified durable thread, restart when no effectful tool
|
|
21
|
+
* was ever dispatched, otherwise held (`CODEX_THREAD_NOT_VERIFIABLE`).
|
|
22
|
+
* 6. llama.cpp without a checkpoint: restart when no effectful tool was
|
|
23
|
+
* dispatched (nothing happened), otherwise held (`NO_EXECUTION_CHECKPOINT`).
|
|
24
|
+
* 7. Every dispatched call settled, or the unsettled ones are reads or
|
|
25
|
+
* idempotent: `ready`, resuming at the next step; unsettled reads and
|
|
26
|
+
* idempotent calls are re-dispatched with their idempotency key.
|
|
27
|
+
* 8. An unsettled call whose tool can reconcile: `reconciling`; reconciliation
|
|
28
|
+
* runs first when the task resumes.
|
|
29
|
+
* 9. Any other unsettled effectful call: held, awaiting an operator decision
|
|
30
|
+
* (`UNSETTLED_EXTERNAL_WRITE`).
|
|
31
|
+
*
|
|
32
|
+
* A held task is `unknown`, which stays terminal; an operator decision
|
|
33
|
+
* continues its work as a linked continuation task.
|
|
34
|
+
*/
|
|
35
|
+
export type RecoveryView = {
|
|
36
|
+
status: RecoveryStatus;
|
|
37
|
+
reason: RecoveryReason;
|
|
38
|
+
attempt: number;
|
|
39
|
+
checkpoint?: RecoveryCheckpoint;
|
|
40
|
+
actions: RecoveryAction[];
|
|
41
|
+
continuation_task_id?: string;
|
|
42
|
+
decided?: RecoveryDecision;
|
|
43
|
+
};
|
|
44
|
+
export declare const RecoveryViewSchema: z.ZodObject<{
|
|
45
|
+
status: z.ZodEnum<{
|
|
46
|
+
ready: "ready";
|
|
47
|
+
reconciling: "reconciling";
|
|
48
|
+
resumed: "resumed";
|
|
49
|
+
awaiting_decision: "awaiting_decision";
|
|
50
|
+
unsupported: "unsupported";
|
|
51
|
+
resolved: "resolved";
|
|
52
|
+
}>;
|
|
53
|
+
reason: z.ZodObject<{
|
|
54
|
+
code: z.ZodString;
|
|
55
|
+
message: z.ZodString;
|
|
56
|
+
}, z.core.$strip>;
|
|
57
|
+
attempt: z.ZodNumber;
|
|
58
|
+
checkpoint: z.ZodOptional<z.ZodObject<{
|
|
59
|
+
step: z.ZodNumber;
|
|
60
|
+
completed_calls: z.ZodNumber;
|
|
61
|
+
pending_calls: z.ZodArray<z.ZodObject<{
|
|
62
|
+
call_id: z.ZodString;
|
|
63
|
+
tool: z.ZodString;
|
|
64
|
+
effect: z.ZodString;
|
|
65
|
+
dispatched_at: z.ZodNullable<z.ZodString>;
|
|
66
|
+
reconcilable: z.ZodBoolean;
|
|
67
|
+
}, z.core.$strip>>;
|
|
68
|
+
}, z.core.$strip>>;
|
|
69
|
+
actions: z.ZodArray<z.ZodEnum<{
|
|
70
|
+
mark_applied: "mark_applied";
|
|
71
|
+
mark_not_applied: "mark_not_applied";
|
|
72
|
+
retry_with_new_deadline: "retry_with_new_deadline";
|
|
73
|
+
abandon: "abandon";
|
|
74
|
+
}>>;
|
|
75
|
+
continuation_task_id: z.ZodOptional<z.ZodString>;
|
|
76
|
+
decided: z.ZodOptional<z.ZodObject<{
|
|
77
|
+
action: z.ZodEnum<{
|
|
78
|
+
mark_applied: "mark_applied";
|
|
79
|
+
mark_not_applied: "mark_not_applied";
|
|
80
|
+
retry_with_new_deadline: "retry_with_new_deadline";
|
|
81
|
+
abandon: "abandon";
|
|
82
|
+
}>;
|
|
83
|
+
by: z.ZodString;
|
|
84
|
+
at: z.ZodString;
|
|
85
|
+
note: z.ZodOptional<z.ZodString>;
|
|
86
|
+
}, z.core.$strip>>;
|
|
87
|
+
}, z.core.$strip>;
|
|
88
|
+
/** `POST /api/v1/tasks/{id}/recovery`. */
|
|
89
|
+
export declare const RecoveryDecisionInput: z.ZodObject<{
|
|
90
|
+
action: z.ZodEnum<{
|
|
91
|
+
mark_applied: "mark_applied";
|
|
92
|
+
mark_not_applied: "mark_not_applied";
|
|
93
|
+
retry_with_new_deadline: "retry_with_new_deadline";
|
|
94
|
+
abandon: "abandon";
|
|
95
|
+
}>;
|
|
96
|
+
call_id: z.ZodOptional<z.ZodString>;
|
|
97
|
+
result: z.ZodOptional<z.ZodUnknown>;
|
|
98
|
+
note: z.ZodOptional<z.ZodString>;
|
|
99
|
+
deadline_seconds: z.ZodOptional<z.ZodNumber>;
|
|
100
|
+
}, z.core.$strict>;
|
|
101
|
+
export type RecoveryDecisionInput = z.infer<typeof RecoveryDecisionInput>;
|
|
102
|
+
export declare const MAX_OPERATOR_RESULT_BYTES: number;
|
|
103
|
+
export declare const RECOVERY_REASONS: {
|
|
104
|
+
readonly CANCELLED_DURING_OUTAGE: "Cancellation was requested before the restart and no effect was uncertain, so the task was cancelled.";
|
|
105
|
+
readonly CANCELLED_WITH_UNSETTLED_EFFECT: "Cancellation was requested, but a tool call with a possible external effect had not settled. Confirm whether it applied.";
|
|
106
|
+
readonly DEADLINE_ELAPSED_DURING_OUTAGE: "The task deadline passed while the runtime was stopped. It is never extended automatically.";
|
|
107
|
+
readonly RECOVERY_DISABLED: "Execution recovery is disabled (execution.recovery.enabled: false); interrupted tasks are held as unknown.";
|
|
108
|
+
readonly EXTENSION_EXECUTION_NOT_RESUMABLE: "An extension task executor with a possible external effect was running; extension execution is not resumable.";
|
|
109
|
+
readonly PREVIEW_NOT_RESUMABLE: "Preview tasks are not resumed; submit the preview again.";
|
|
110
|
+
readonly RESTART_NO_EFFECT: "The task stopped before any effectful tool was dispatched; it restarts from the beginning.";
|
|
111
|
+
readonly NO_EXECUTION_CHECKPOINT: "The task has no execution checkpoint (it started before checkpoints were recorded, or stopped before the first one) and dispatched an effectful tool.";
|
|
112
|
+
readonly CHECKPOINT_INVALID: "The execution checkpoint failed verification and is not used.";
|
|
113
|
+
readonly RESUME_FROM_CHECKPOINT: "Every interrupted tool call is settled, a read or idempotent; the task resumes at its next step.";
|
|
114
|
+
readonly TOOL_CHECK_PENDING: "An interrupted tool call is checked against its tool (idempotency or reconciliation) before the task resumes.";
|
|
115
|
+
readonly RECONCILE_INTERRUPTED_CALL: "An interrupted tool call can be reconciled with its service before the task resumes.";
|
|
116
|
+
readonly UNSETTLED_EXTERNAL_WRITE: "A tool call with a possible external effect was dispatched but its outcome was not recorded, and the tool cannot prove it. Confirm whether it applied.";
|
|
117
|
+
readonly RECONCILIATION_INCONCLUSIVE: "Reconciliation could not prove whether an interrupted tool call applied. Confirm whether it applied.";
|
|
118
|
+
readonly CODEX_THREAD_RESUME: "The durable Codex thread is resumed after thread/read verifies every tool call in it.";
|
|
119
|
+
readonly CODEX_RESTART_NO_EFFECT: "The Codex task never dispatched an effectful tool; it restarts on a fresh thread.";
|
|
120
|
+
readonly CODEX_THREAD_NOT_VERIFIABLE: "The Codex task dispatched an effectful tool and its thread cannot be verified for resumption.";
|
|
121
|
+
};
|
|
122
|
+
export type RecoveryReasonCode = keyof typeof RECOVERY_REASONS;
|
|
123
|
+
export declare function reason(code: RecoveryReasonCode, detail?: string): RecoveryReason;
|
|
124
|
+
/** How a resumed task treats one unsettled call, decided from its current tool definition. */
|
|
125
|
+
export type PendingVerdict = 'redispatch' | 'reconcile' | 'hold';
|
|
126
|
+
/** Classify an unsettled call. Read and idempotent calls are re-dispatched; reconcilable ones are asked; the rest are held. */
|
|
127
|
+
export declare function classifyPending(invocation: Pick<ToolInvocation, 'effect'>, tool: Pick<Tool, 'effect' | 'idempotent' | 'reconcile'> | undefined): PendingVerdict;
|
|
128
|
+
export type RecoveryPlanOptions = {
|
|
129
|
+
enabled?: boolean;
|
|
130
|
+
now?: number;
|
|
131
|
+
/** The tool a task would call by this name now. Without it, calls that need a tool check stay `reconciling` until the runtime checks them. */
|
|
132
|
+
toolFor?: (task: Task, name: string) => Tool | undefined;
|
|
133
|
+
};
|
|
134
|
+
export type RecoveryOutcome = {
|
|
135
|
+
resumed: string[];
|
|
136
|
+
held: string[];
|
|
137
|
+
};
|
|
138
|
+
/** The attempt that was interrupted. */
|
|
139
|
+
export declare function interruptedAttempt(state: ExecutionState | undefined, recovery: TaskRecoveryRow | undefined): number;
|
|
140
|
+
/**
|
|
141
|
+
* Plan every task that was `running`. Returns the ids left running for
|
|
142
|
+
* resumption and the ids held as `unknown`.
|
|
143
|
+
*/
|
|
144
|
+
export declare function planRecovery(store: TaskStore, options?: RecoveryPlanOptions): RecoveryOutcome;
|
|
145
|
+
/**
|
|
146
|
+
* Re-check a `reconciling` task whose calls still need a tool check, now that
|
|
147
|
+
* its tools are known. Holds it when a call cannot be proven.
|
|
148
|
+
*/
|
|
149
|
+
export declare function refinePlan(store: TaskStore, task: Task, toolFor: (name: string) => Tool | undefined, enabled: boolean): 'ready' | 'reconciling' | 'held' | 'unchanged';
|
|
150
|
+
/** Whether a continuation can be seeded from the task's checkpoint. */
|
|
151
|
+
export declare function continuable(store: TaskStore, task: Task): boolean;
|
|
152
|
+
/** The decisions valid now for a recovery. Only `awaiting_decision` takes a decision. */
|
|
153
|
+
export declare function validActions(store: TaskStore, task: Task, row: TaskRecoveryRow, now?: number): RecoveryAction[];
|
|
154
|
+
/** The C3 recovery object of a task, when it was ever interrupted. */
|
|
155
|
+
export declare function recoveryView(store: TaskStore, task: Task, row?: TaskRecoveryRow | undefined, now?: number): RecoveryView | undefined;
|