@noetic-tools/core 5.19.1 → 5.19.3
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/dist/adapters/openrouter.js +5 -0
- package/dist/builders/control-flow-builders.d.ts +15 -6
- package/dist/builders/control-flow-builders.js +8 -0
- package/dist/builders/decide-builders.d.ts +2 -5
- package/dist/builders/dynamic-workflow.d.ts +54 -2
- package/dist/builders/dynamic-workflow.js +247 -12
- package/dist/builders/every.d.ts +3 -3
- package/dist/builders/every.js +2 -1
- package/dist/builders/mcp-url-pattern.d.ts +13 -0
- package/dist/builders/mcp-url-pattern.js +85 -0
- package/dist/builders/provide-builder.d.ts +3 -2
- package/dist/builders/provide-builder.js +2 -1
- package/dist/builders/sequence.d.ts +14 -0
- package/dist/builders/sequence.js +103 -0
- package/dist/builders/spawn-builder.d.ts +3 -2
- package/dist/builders/spawn-builder.js +3 -2
- package/dist/builders/step-builders.d.ts +80 -1
- package/dist/builders/step-builders.js +115 -1
- package/dist/builders/verify-builder.d.ts +79 -0
- package/dist/builders/verify-builder.js +69 -0
- package/dist/builders/workflow-acp-grants.d.ts +42 -0
- package/dist/builders/workflow-acp-grants.js +224 -0
- package/dist/builders/workflow-child-nodes.d.ts +21 -0
- package/dist/builders/workflow-child-nodes.js +336 -0
- package/dist/builders/workflow-connection-nodes.d.ts +25 -0
- package/dist/builders/workflow-connection-nodes.js +198 -0
- package/dist/builders/workflow-connections.d.ts +90 -0
- package/dist/builders/workflow-connections.js +560 -0
- package/dist/builders/workflow-control-nodes.d.ts +13 -0
- package/dist/builders/workflow-control-nodes.js +213 -0
- package/dist/builders/workflow-hydrator.d.ts +56 -3
- package/dist/builders/workflow-hydrator.js +53 -520
- package/dist/builders/workflow-mcp-servers.d.ts +24 -0
- package/dist/builders/workflow-mcp-servers.js +220 -0
- package/dist/builders/workflow-verify.d.ts +19 -0
- package/dist/builders/workflow-verify.js +194 -0
- package/dist/conditions/conditions.d.ts +7 -5
- package/dist/conditions/conditions.js +7 -4
- package/dist/connections/blobs.d.ts +33 -0
- package/dist/connections/blobs.js +77 -0
- package/dist/connections/cron-schedule.d.ts +32 -0
- package/dist/connections/cron-schedule.js +213 -0
- package/dist/connections/durability.d.ts +65 -0
- package/dist/connections/durability.js +54 -0
- package/dist/connections/errors.d.ts +30 -0
- package/dist/connections/errors.js +100 -0
- package/dist/connections/event-stream.d.ts +24 -0
- package/dist/connections/event-stream.js +176 -0
- package/dist/connections/gates.d.ts +94 -0
- package/dist/connections/gates.js +271 -0
- package/dist/connections/glob.d.ts +10 -0
- package/dist/connections/glob.js +46 -0
- package/dist/connections/handle.d.ts +22 -0
- package/dist/connections/handle.js +84 -0
- package/dist/connections/harness-wiring.d.ts +52 -0
- package/dist/connections/harness-wiring.js +143 -0
- package/dist/connections/in-memory-connection.d.ts +147 -0
- package/dist/connections/in-memory-connection.js +593 -0
- package/dist/connections/index.d.ts +34 -0
- package/dist/connections/index.js +23 -0
- package/dist/connections/json-schema.d.ts +28 -0
- package/dist/connections/json-schema.js +338 -0
- package/dist/connections/model-tools.d.ts +46 -0
- package/dist/connections/model-tools.js +466 -0
- package/dist/connections/negotiation.d.ts +30 -0
- package/dist/connections/negotiation.js +159 -0
- package/dist/connections/policy.d.ts +21 -0
- package/dist/connections/policy.js +34 -0
- package/dist/connections/pool.d.ts +111 -0
- package/dist/connections/pool.js +339 -0
- package/dist/connections/refs.d.ts +74 -0
- package/dist/connections/refs.js +316 -0
- package/dist/connections/registry-types.d.ts +141 -0
- package/dist/connections/registry-types.js +14 -0
- package/dist/connections/registry.d.ts +125 -0
- package/dist/connections/registry.js +775 -0
- package/dist/connections/secrets.d.ts +59 -0
- package/dist/connections/secrets.js +153 -0
- package/dist/connections/settle.d.ts +9 -0
- package/dist/connections/settle.js +26 -0
- package/dist/connections/sources.d.ts +27 -0
- package/dist/connections/sources.js +300 -0
- package/dist/connections/triggers.d.ts +78 -0
- package/dist/connections/triggers.js +310 -0
- package/dist/connections/values.d.ts +26 -0
- package/dist/connections/values.js +93 -0
- package/dist/connections/views.d.ts +31 -0
- package/dist/connections/views.js +176 -0
- package/dist/expression/check.js +80 -1
- package/dist/expression/jsonata.d.ts +14 -4
- package/dist/expression/jsonata.js +27 -9
- package/dist/expression/step-outputs.d.ts +25 -0
- package/dist/expression/step-outputs.js +78 -0
- package/dist/harness/agent-harness.d.ts +69 -166
- package/dist/harness/agent-harness.js +170 -262
- package/dist/harness/call-span-collector.d.ts +33 -0
- package/dist/harness/call-span-collector.js +83 -0
- package/dist/harness/connection-gate.d.ts +78 -0
- package/dist/harness/connection-gate.js +170 -0
- package/dist/harness/deps/interpreter.d.ts +1 -0
- package/dist/harness/deps/interpreter.js +1 -0
- package/dist/harness/deps/runtime.d.ts +1 -1
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/harness-options.d.ts +153 -0
- package/dist/harness/harness-options.js +36 -0
- package/dist/harness/harness-tracing.d.ts +16 -0
- package/dist/harness/harness-tracing.js +35 -0
- package/dist/harness/llm-client.d.ts +71 -0
- package/dist/harness/llm-client.js +218 -0
- package/dist/harness/model-call.d.ts +2 -7
- package/dist/harness/model-call.js +21 -549
- package/dist/harness/model-request.d.ts +42 -0
- package/dist/harness/model-request.js +159 -0
- package/dist/harness/model-stream.d.ts +39 -0
- package/dist/harness/model-stream.js +131 -0
- package/dist/harness/model-tool-round.d.ts +37 -0
- package/dist/harness/model-tool-round.js +197 -0
- package/dist/harness/spend.d.ts +2 -0
- package/dist/harness/spend.js +5 -1
- package/dist/index.d.ts +33 -8
- package/dist/index.js +18 -1
- package/dist/interpreter/connection-steps.d.ts +194 -0
- package/dist/interpreter/connection-steps.js +537 -0
- package/dist/interpreter/execute-action.d.ts +2 -8
- package/dist/interpreter/execute-action.js +3 -178
- package/dist/interpreter/execute-call.d.ts +19 -0
- package/dist/interpreter/execute-call.js +99 -0
- package/dist/interpreter/execute-control.d.ts +6 -1
- package/dist/interpreter/execute-control.js +14 -0
- package/dist/interpreter/execute-invoke-tool.d.ts +11 -0
- package/dist/interpreter/execute-invoke-tool.js +183 -0
- package/dist/interpreter/execute-verify.d.ts +21 -0
- package/dist/interpreter/execute-verify.js +391 -0
- package/dist/interpreter/execute-wait.d.ts +21 -0
- package/dist/interpreter/execute-wait.js +233 -0
- package/dist/interpreter/execute.js +24 -133
- package/dist/interpreter/step-kind-dispatch.d.ts +24 -0
- package/dist/interpreter/step-kind-dispatch.js +157 -0
- package/dist/interpreter/step-walk.d.ts +2 -0
- package/dist/interpreter/step-walk.js +24 -0
- package/dist/interpreter/verify-host.d.ts +40 -0
- package/dist/interpreter/verify-host.js +245 -0
- package/dist/runtime/durable/step-ledger.d.ts +31 -1
- package/dist/runtime/durable/step-ledger.js +87 -1
- package/dist/runtime/in-memory-artifact-store.d.ts +24 -0
- package/dist/runtime/in-memory-artifact-store.js +240 -0
- package/dist/schemas/workflow.d.ts +2 -2
- package/dist/schemas/workflow.js +1 -1
- package/dist/util/harness-internals.d.ts +25 -0
- package/dist/util/harness-internals.js +35 -0
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +1309 -180
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Five-field cron schedules (`minute hour day-of-month month day-of-week`) in
|
|
3
|
+
* an IANA timezone, for `cron()` (spec 35 § Triggers). Each field takes `*`,
|
|
4
|
+
* numbers, lists (`1,15`), ranges (`1-5`) and steps (`*\/15`, `0-30/10`).
|
|
5
|
+
* Day-of-week runs 0–6 from Sunday (7 is Sunday too). When both day fields
|
|
6
|
+
* are restricted, a time matches if either does, as in classic cron.
|
|
7
|
+
*/
|
|
8
|
+
import { invalidConnectionError } from './errors.js';
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region Constants
|
|
11
|
+
const MINUTE_FIELD = {
|
|
12
|
+
name: 'minute',
|
|
13
|
+
min: 0,
|
|
14
|
+
max: 59,
|
|
15
|
+
};
|
|
16
|
+
const HOUR_FIELD = {
|
|
17
|
+
name: 'hour',
|
|
18
|
+
min: 0,
|
|
19
|
+
max: 23,
|
|
20
|
+
};
|
|
21
|
+
const DAY_FIELD = {
|
|
22
|
+
name: 'day-of-month',
|
|
23
|
+
min: 1,
|
|
24
|
+
max: 31,
|
|
25
|
+
};
|
|
26
|
+
const MONTH_FIELD = {
|
|
27
|
+
name: 'month',
|
|
28
|
+
min: 1,
|
|
29
|
+
max: 12,
|
|
30
|
+
};
|
|
31
|
+
const WEEKDAY_FIELD = {
|
|
32
|
+
name: 'day-of-week',
|
|
33
|
+
min: 0,
|
|
34
|
+
max: 7,
|
|
35
|
+
};
|
|
36
|
+
const FIELD_COUNT = 5;
|
|
37
|
+
const WEEKDAYS = {
|
|
38
|
+
Sun: 0,
|
|
39
|
+
Mon: 1,
|
|
40
|
+
Tue: 2,
|
|
41
|
+
Wed: 3,
|
|
42
|
+
Thu: 4,
|
|
43
|
+
Fri: 5,
|
|
44
|
+
Sat: 6,
|
|
45
|
+
};
|
|
46
|
+
const MINUTE_MS = 6e4;
|
|
47
|
+
/** How far ahead `nextRun` searches before giving up (a little over four years, for Feb 29). */
|
|
48
|
+
const SEARCH_LIMIT_MS = 4 * 366 * 24 * 3600e3;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region Helpers
|
|
51
|
+
function badSchedule(source, detail) {
|
|
52
|
+
return invalidConnectionError(`cron schedule '${source}' is invalid: ${detail}.`, "Use five fields — minute hour day-of-month month day-of-week — e.g. '0 8 * * 1-5'.");
|
|
53
|
+
}
|
|
54
|
+
function parseNumber(text, field, source) {
|
|
55
|
+
if (!/^\d+$/.test(text)) {
|
|
56
|
+
throw badSchedule(source, `'${text}' in the ${field.name} field is not a number`);
|
|
57
|
+
}
|
|
58
|
+
const value = Number(text);
|
|
59
|
+
if (value < field.min || value > field.max) {
|
|
60
|
+
throw badSchedule(source, `${value} is outside ${field.min}-${field.max} for ${field.name}`);
|
|
61
|
+
}
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
function parsePart(part, field, source) {
|
|
65
|
+
const [rangeText = '', stepText] = part.split('/');
|
|
66
|
+
const step = stepText === undefined
|
|
67
|
+
? 1
|
|
68
|
+
: parseNumber(stepText, {
|
|
69
|
+
...field,
|
|
70
|
+
min: 1,
|
|
71
|
+
max: field.max,
|
|
72
|
+
}, source);
|
|
73
|
+
let start = field.min;
|
|
74
|
+
let end = field.max;
|
|
75
|
+
if (rangeText !== '*') {
|
|
76
|
+
const [from = '', to] = rangeText.split('-');
|
|
77
|
+
start = parseNumber(from, field, source);
|
|
78
|
+
end =
|
|
79
|
+
to === undefined
|
|
80
|
+
? stepText === undefined
|
|
81
|
+
? start
|
|
82
|
+
: field.max
|
|
83
|
+
: parseNumber(to, field, source);
|
|
84
|
+
}
|
|
85
|
+
if (start > end) {
|
|
86
|
+
throw badSchedule(source, `range ${start}-${end} in ${field.name} runs backwards`);
|
|
87
|
+
}
|
|
88
|
+
const values = [];
|
|
89
|
+
for (let v = start; v <= end; v += step) {
|
|
90
|
+
values.push(v);
|
|
91
|
+
}
|
|
92
|
+
return values;
|
|
93
|
+
}
|
|
94
|
+
function parseField(text, field, source) {
|
|
95
|
+
const values = new Set();
|
|
96
|
+
for (const part of text.split(',')) {
|
|
97
|
+
for (const value of parsePart(part, field, source)) {
|
|
98
|
+
values.add(field.name === 'day-of-week' && value === 7 ? 0 : value);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return values;
|
|
102
|
+
}
|
|
103
|
+
const formatters = new Map();
|
|
104
|
+
function formatterFor(timezone) {
|
|
105
|
+
const cached = formatters.get(timezone);
|
|
106
|
+
if (cached) {
|
|
107
|
+
return cached;
|
|
108
|
+
}
|
|
109
|
+
const formatter = new Intl.DateTimeFormat('en-US', {
|
|
110
|
+
timeZone: timezone,
|
|
111
|
+
hourCycle: 'h23',
|
|
112
|
+
minute: 'numeric',
|
|
113
|
+
hour: 'numeric',
|
|
114
|
+
day: 'numeric',
|
|
115
|
+
month: 'numeric',
|
|
116
|
+
weekday: 'short',
|
|
117
|
+
});
|
|
118
|
+
formatters.set(timezone, formatter);
|
|
119
|
+
return formatter;
|
|
120
|
+
}
|
|
121
|
+
function zonedParts(ms, timezone) {
|
|
122
|
+
const parts = {};
|
|
123
|
+
for (const part of formatterFor(timezone).formatToParts(ms)) {
|
|
124
|
+
parts[part.type] = part.value;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
minute: Number(parts.minute),
|
|
128
|
+
hour: Number(parts.hour),
|
|
129
|
+
day: Number(parts.day),
|
|
130
|
+
month: Number(parts.month),
|
|
131
|
+
weekday: WEEKDAYS[parts.weekday ?? 'Sun'] ?? 0,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function dayMatches(schedule, parts) {
|
|
135
|
+
const dayOk = schedule.days.has(parts.day);
|
|
136
|
+
const weekdayOk = schedule.weekdays.has(parts.weekday);
|
|
137
|
+
if (schedule.anyDay) {
|
|
138
|
+
return weekdayOk;
|
|
139
|
+
}
|
|
140
|
+
if (schedule.anyWeekday) {
|
|
141
|
+
return dayOk;
|
|
142
|
+
}
|
|
143
|
+
return dayOk || weekdayOk;
|
|
144
|
+
}
|
|
145
|
+
/** Minutes to skip from a non-matching time: to the next hour or the next day where possible. */
|
|
146
|
+
function skipMinutes(schedule, parts) {
|
|
147
|
+
if (!schedule.months.has(parts.month) || !dayMatches(schedule, parts)) {
|
|
148
|
+
return (23 - parts.hour) * 60 + (60 - parts.minute);
|
|
149
|
+
}
|
|
150
|
+
if (!schedule.hours.has(parts.hour)) {
|
|
151
|
+
return 60 - parts.minute;
|
|
152
|
+
}
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region Public API
|
|
157
|
+
/**
|
|
158
|
+
* Parses a five-field cron schedule.
|
|
159
|
+
*
|
|
160
|
+
* @throws NoeticConfigError `CONNECTION_INVALID` for a malformed schedule or timezone.
|
|
161
|
+
*/
|
|
162
|
+
export function parseCronSchedule(source, timezone = 'UTC') {
|
|
163
|
+
const fields = source.trim().split(/\s+/);
|
|
164
|
+
if (fields.length !== FIELD_COUNT) {
|
|
165
|
+
throw badSchedule(source, `expected 5 fields, got ${fields.length}`);
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
formatterFor(timezone);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
throw invalidConnectionError(`cron timezone '${timezone}' is not a known IANA timezone.`, "Use a timezone such as 'UTC' or 'America/New_York'.");
|
|
172
|
+
}
|
|
173
|
+
const [minute = '', hour = '', day = '', month = '', weekday = ''] = fields;
|
|
174
|
+
return {
|
|
175
|
+
source,
|
|
176
|
+
timezone,
|
|
177
|
+
minutes: parseField(minute, MINUTE_FIELD, source),
|
|
178
|
+
hours: parseField(hour, HOUR_FIELD, source),
|
|
179
|
+
days: parseField(day, DAY_FIELD, source),
|
|
180
|
+
months: parseField(month, MONTH_FIELD, source),
|
|
181
|
+
weekdays: parseField(weekday, WEEKDAY_FIELD, source),
|
|
182
|
+
anyDay: day === '*',
|
|
183
|
+
anyWeekday: weekday === '*',
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/** Whether the minute starting at `ms` matches the schedule. */
|
|
187
|
+
export function cronMatches(schedule, ms) {
|
|
188
|
+
const parts = zonedParts(ms, schedule.timezone);
|
|
189
|
+
return (schedule.minutes.has(parts.minute) &&
|
|
190
|
+
schedule.hours.has(parts.hour) &&
|
|
191
|
+
schedule.months.has(parts.month) &&
|
|
192
|
+
dayMatches(schedule, parts));
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The first scheduled minute strictly after `afterMs`, as epoch ms.
|
|
196
|
+
* @returns `undefined` when nothing matches within four years.
|
|
197
|
+
*/
|
|
198
|
+
export function nextCronRun(schedule, afterMs) {
|
|
199
|
+
let ms = Math.floor(afterMs / MINUTE_MS) * MINUTE_MS + MINUTE_MS;
|
|
200
|
+
const limit = afterMs + SEARCH_LIMIT_MS;
|
|
201
|
+
while (ms <= limit) {
|
|
202
|
+
const parts = zonedParts(ms, schedule.timezone);
|
|
203
|
+
if (schedule.minutes.has(parts.minute) &&
|
|
204
|
+
schedule.hours.has(parts.hour) &&
|
|
205
|
+
schedule.months.has(parts.month) &&
|
|
206
|
+
dayMatches(schedule, parts)) {
|
|
207
|
+
return ms;
|
|
208
|
+
}
|
|
209
|
+
ms += skipMinutes(schedule, parts) * MINUTE_MS;
|
|
210
|
+
}
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durability (spec 35 § Durability): idempotency keys, the pending ledger
|
|
3
|
+
* entry a `call` or `wait` writes before it reaches a server, and what a
|
|
4
|
+
* resumed run does with a connection that was replaced.
|
|
5
|
+
*
|
|
6
|
+
* If a connection's identity instance changed, results recorded against it
|
|
7
|
+
* describe a world that is gone. Its determinism decides, following OVP's
|
|
8
|
+
* resume strategy:
|
|
9
|
+
*
|
|
10
|
+
* | `replayPrefix` | Resume |
|
|
11
|
+
* |---|---|
|
|
12
|
+
* | `'free'` | Discard the ledger from the first step that used it; run live. |
|
|
13
|
+
* | `'costly'`, with `reset` | Reset the connection, then the same. |
|
|
14
|
+
* | `'unsafe'` | Refuse until `harness.restore(id, { rerun: [name] })`. |
|
|
15
|
+
*
|
|
16
|
+
* An epoch change alone (a hot reload) keeps refs and results. A connection
|
|
17
|
+
* that declares no determinism is treated as `'unsafe'`.
|
|
18
|
+
*/
|
|
19
|
+
import type { ConnectionIdentity, ConnectionLease, Determinism } from '@noetic-tools/types';
|
|
20
|
+
/**
|
|
21
|
+
* What a `call` or `wait` records before it reaches a server, so a resumed
|
|
22
|
+
* step against the same identity picks up where it was.
|
|
23
|
+
*/
|
|
24
|
+
export interface PendingConnectionEntry {
|
|
25
|
+
/** The call's idempotency key. */
|
|
26
|
+
readonly key: string;
|
|
27
|
+
readonly connection: string;
|
|
28
|
+
readonly identity: ConnectionIdentity;
|
|
29
|
+
/** The long operation, once the server returned one. */
|
|
30
|
+
readonly taskId?: string;
|
|
31
|
+
readonly lease?: ConnectionLease;
|
|
32
|
+
/** For waits: the cursor listening started from. */
|
|
33
|
+
readonly cursor?: string | null;
|
|
34
|
+
/** For waits: epoch ms the wait gives up. */
|
|
35
|
+
readonly deadline?: number;
|
|
36
|
+
}
|
|
37
|
+
/** What a resumed run does about one connection. */
|
|
38
|
+
export type ResumeDecision =
|
|
39
|
+
/** Same instance: replay recorded results. */
|
|
40
|
+
'keep'
|
|
41
|
+
/** Replaced: discard the ledger from the first step that used it and run live. */
|
|
42
|
+
| 'discard'
|
|
43
|
+
/** Replaced: reset the connection, then discard. */
|
|
44
|
+
| 'reset'
|
|
45
|
+
/** Replaced and unsafe to drive again: stop with `connection_resume_refused`. */
|
|
46
|
+
| 'refuse';
|
|
47
|
+
/** The idempotency key of a step's call: the run and the step's ledger key. */
|
|
48
|
+
export declare function stepIdempotencyKey(executionId: string, ledgerKey: string): string;
|
|
49
|
+
/** The idempotency key of a model tool call: the turn's execution and the tool call id. */
|
|
50
|
+
export declare function toolIdempotencyKey(executionId: string, toolCallId: string): string;
|
|
51
|
+
/** Whether `current` is the same thing `recorded` described (an epoch change alone keeps it). */
|
|
52
|
+
export declare function sameInstance(recorded: ConnectionIdentity, current: ConnectionIdentity): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Decides what a resumed run does about a connection whose results were
|
|
55
|
+
* recorded against `recorded`.
|
|
56
|
+
*/
|
|
57
|
+
export declare function resumeDecision(params: {
|
|
58
|
+
readonly recorded: ConnectionIdentity;
|
|
59
|
+
readonly current: ConnectionIdentity;
|
|
60
|
+
readonly determinism: Determinism | undefined;
|
|
61
|
+
/** `harness.restore(id, { rerun })` named this connection. */
|
|
62
|
+
readonly rerunAllowed: boolean;
|
|
63
|
+
/** The connection can be reset. */
|
|
64
|
+
readonly canReset: boolean;
|
|
65
|
+
}): ResumeDecision;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durability (spec 35 § Durability): idempotency keys, the pending ledger
|
|
3
|
+
* entry a `call` or `wait` writes before it reaches a server, and what a
|
|
4
|
+
* resumed run does with a connection that was replaced.
|
|
5
|
+
*
|
|
6
|
+
* If a connection's identity instance changed, results recorded against it
|
|
7
|
+
* describe a world that is gone. Its determinism decides, following OVP's
|
|
8
|
+
* resume strategy:
|
|
9
|
+
*
|
|
10
|
+
* | `replayPrefix` | Resume |
|
|
11
|
+
* |---|---|
|
|
12
|
+
* | `'free'` | Discard the ledger from the first step that used it; run live. |
|
|
13
|
+
* | `'costly'`, with `reset` | Reset the connection, then the same. |
|
|
14
|
+
* | `'unsafe'` | Refuse until `harness.restore(id, { rerun: [name] })`. |
|
|
15
|
+
*
|
|
16
|
+
* An epoch change alone (a hot reload) keeps refs and results. A connection
|
|
17
|
+
* that declares no determinism is treated as `'unsafe'`.
|
|
18
|
+
*/
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region Public API
|
|
21
|
+
/** The idempotency key of a step's call: the run and the step's ledger key. */
|
|
22
|
+
export function stepIdempotencyKey(executionId, ledgerKey) {
|
|
23
|
+
return `${executionId}:${ledgerKey}`;
|
|
24
|
+
}
|
|
25
|
+
/** The idempotency key of a model tool call: the turn's execution and the tool call id. */
|
|
26
|
+
export function toolIdempotencyKey(executionId, toolCallId) {
|
|
27
|
+
return `${executionId}:tool:${toolCallId}`;
|
|
28
|
+
}
|
|
29
|
+
/** Whether `current` is the same thing `recorded` described (an epoch change alone keeps it). */
|
|
30
|
+
export function sameInstance(recorded, current) {
|
|
31
|
+
return recorded.surface === current.surface && recorded.instance === current.instance;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Decides what a resumed run does about a connection whose results were
|
|
35
|
+
* recorded against `recorded`.
|
|
36
|
+
*/
|
|
37
|
+
export function resumeDecision(params) {
|
|
38
|
+
if (sameInstance(params.recorded, params.current)) {
|
|
39
|
+
return 'keep';
|
|
40
|
+
}
|
|
41
|
+
if (params.rerunAllowed) {
|
|
42
|
+
return 'discard';
|
|
43
|
+
}
|
|
44
|
+
const replay = params.determinism?.replayPrefix ?? 'unsafe';
|
|
45
|
+
if (replay === 'free') {
|
|
46
|
+
return 'discard';
|
|
47
|
+
}
|
|
48
|
+
const resettable = params.canReset && (params.determinism?.reset ?? 'none') !== 'none';
|
|
49
|
+
if (replay === 'costly' && resettable) {
|
|
50
|
+
return 'reset';
|
|
51
|
+
}
|
|
52
|
+
return 'refuse';
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error constructors for the connection runtime (spec 35 § Errors). Setup and
|
|
3
|
+
* validation problems are `NoeticConfigError`s with a `CONNECTION_*` code; run
|
|
4
|
+
* time problems are `NoeticErrorImpl`s with a `connection_*` kind, or
|
|
5
|
+
* `steering_denied` when a gate refused.
|
|
6
|
+
*/
|
|
7
|
+
import type { ConnectionErrorPayload, ConnectionInputRequest } from '@noetic-tools/types';
|
|
8
|
+
import { NoeticConfigError, NoeticErrorImpl } from '@noetic-tools/types';
|
|
9
|
+
/** `CONNECTION_UNKNOWN`: a handle, node, trigger or allowlist names a connection the harness does not have. */
|
|
10
|
+
export declare function unknownConnectionError(name: string, known: ReadonlyArray<string>): NoeticConfigError;
|
|
11
|
+
/** `CONNECTION_INVALID`: a manifest entry, handle or harness entry is malformed. */
|
|
12
|
+
export declare function invalidConnectionError(message: string, hint: string): NoeticConfigError;
|
|
13
|
+
/** `CONNECTION_NOT_ALLOWED`: a request asks for more than it is granted, or sets a host-only setup field. */
|
|
14
|
+
export declare function notAllowedConnectionError(message: string, hint: string): NoeticConfigError;
|
|
15
|
+
/** `CONNECTION_TOOL_COLLISION`: two operations would reach the model under one tool name. */
|
|
16
|
+
export declare function toolCollisionError(toolName: string, first: string, second: string): NoeticConfigError;
|
|
17
|
+
/** `connection_unavailable`. */
|
|
18
|
+
export declare function unavailableError(connection: string, reason: string): NoeticErrorImpl;
|
|
19
|
+
/** `connection_failed`. */
|
|
20
|
+
export declare function failedError(connection: string, op: string, error: ConnectionErrorPayload): NoeticErrorImpl;
|
|
21
|
+
/** `connection_ref_invalid`. */
|
|
22
|
+
export declare function refInvalidError(connection: string, reason: string): NoeticErrorImpl;
|
|
23
|
+
/** `connection_secret_refused`. */
|
|
24
|
+
export declare function secretRefusedError(connection: string, op: string): NoeticErrorImpl;
|
|
25
|
+
/** `connection_input_required`. */
|
|
26
|
+
export declare function inputRequiredError(connection: string, request: ConnectionInputRequest): NoeticErrorImpl;
|
|
27
|
+
/** `connection_resume_refused`. */
|
|
28
|
+
export declare function resumeRefusedError(connection: string, reason: string): NoeticErrorImpl;
|
|
29
|
+
/** `steering_denied`: a gate refused the call. */
|
|
30
|
+
export declare function deniedError(guidance: string): NoeticErrorImpl;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error constructors for the connection runtime (spec 35 § Errors). Setup and
|
|
3
|
+
* validation problems are `NoeticConfigError`s with a `CONNECTION_*` code; run
|
|
4
|
+
* time problems are `NoeticErrorImpl`s with a `connection_*` kind, or
|
|
5
|
+
* `steering_denied` when a gate refused.
|
|
6
|
+
*/
|
|
7
|
+
import { ConnectionConfigErrorCode, NoeticConfigError, NoeticErrorImpl } from '@noetic-tools/types';
|
|
8
|
+
//#region Config errors
|
|
9
|
+
/** `CONNECTION_UNKNOWN`: a handle, node, trigger or allowlist names a connection the harness does not have. */
|
|
10
|
+
export function unknownConnectionError(name, known) {
|
|
11
|
+
const list = known.length > 0 ? known.join(', ') : '(none)';
|
|
12
|
+
return new NoeticConfigError({
|
|
13
|
+
code: ConnectionConfigErrorCode.Unknown,
|
|
14
|
+
message: `The harness has no connection named '${name}'. Known: ${list}.`,
|
|
15
|
+
hint: `Add '${name}' to the harness \`connections\`, or point the handle at an existing one with \`from\`.`,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** `CONNECTION_INVALID`: a manifest entry, handle or harness entry is malformed. */
|
|
19
|
+
export function invalidConnectionError(message, hint) {
|
|
20
|
+
return new NoeticConfigError({
|
|
21
|
+
code: ConnectionConfigErrorCode.Invalid,
|
|
22
|
+
message,
|
|
23
|
+
hint,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** `CONNECTION_NOT_ALLOWED`: a request asks for more than it is granted, or sets a host-only setup field. */
|
|
27
|
+
export function notAllowedConnectionError(message, hint) {
|
|
28
|
+
return new NoeticConfigError({
|
|
29
|
+
code: ConnectionConfigErrorCode.NotAllowed,
|
|
30
|
+
message,
|
|
31
|
+
hint,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** `CONNECTION_TOOL_COLLISION`: two operations would reach the model under one tool name. */
|
|
35
|
+
export function toolCollisionError(toolName, first, second) {
|
|
36
|
+
return new NoeticConfigError({
|
|
37
|
+
code: ConnectionConfigErrorCode.ToolCollision,
|
|
38
|
+
message: `Tool name '${toolName}' would be used by both ${first} and ${second}.`,
|
|
39
|
+
hint: "Rename one connection, or set `expose: 'none'` (or `'eager'`) on one of them.",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region Run-time errors
|
|
44
|
+
/** `connection_unavailable`. */
|
|
45
|
+
export function unavailableError(connection, reason) {
|
|
46
|
+
return new NoeticErrorImpl({
|
|
47
|
+
kind: 'connection_unavailable',
|
|
48
|
+
connection,
|
|
49
|
+
reason,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** `connection_failed`. */
|
|
53
|
+
export function failedError(connection, op, error) {
|
|
54
|
+
return new NoeticErrorImpl({
|
|
55
|
+
kind: 'connection_failed',
|
|
56
|
+
connection,
|
|
57
|
+
op,
|
|
58
|
+
error,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** `connection_ref_invalid`. */
|
|
62
|
+
export function refInvalidError(connection, reason) {
|
|
63
|
+
return new NoeticErrorImpl({
|
|
64
|
+
kind: 'connection_ref_invalid',
|
|
65
|
+
connection,
|
|
66
|
+
reason,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/** `connection_secret_refused`. */
|
|
70
|
+
export function secretRefusedError(connection, op) {
|
|
71
|
+
return new NoeticErrorImpl({
|
|
72
|
+
kind: 'connection_secret_refused',
|
|
73
|
+
connection,
|
|
74
|
+
op,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** `connection_input_required`. */
|
|
78
|
+
export function inputRequiredError(connection, request) {
|
|
79
|
+
return new NoeticErrorImpl({
|
|
80
|
+
kind: 'connection_input_required',
|
|
81
|
+
connection,
|
|
82
|
+
request,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** `connection_resume_refused`. */
|
|
86
|
+
export function resumeRefusedError(connection, reason) {
|
|
87
|
+
return new NoeticErrorImpl({
|
|
88
|
+
kind: 'connection_resume_refused',
|
|
89
|
+
connection,
|
|
90
|
+
reason,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/** `steering_denied`: a gate refused the call. */
|
|
94
|
+
export function deniedError(guidance) {
|
|
95
|
+
return new NoeticErrorImpl({
|
|
96
|
+
kind: 'steering_denied',
|
|
97
|
+
guidance,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits listen in memory (spec 35 § Events): from the moment the server
|
|
3
|
+
* confirms the subscription, events are pumped into a buffer of up to
|
|
4
|
+
* `WAIT_BUFFER_LIMIT`. Overflow drops the oldest event and records a gap,
|
|
5
|
+
* which verification reports as a blind spot. Each event is transformed on the
|
|
6
|
+
* way in (stamped with the connection, refs sealed, secrets scrubbed).
|
|
7
|
+
*/
|
|
8
|
+
import type { ConnectionEvent, ConnectionEventInit, EventStream } from '@noetic-tools/types';
|
|
9
|
+
/** How a buffered stream treats what comes through it. */
|
|
10
|
+
export interface BufferedStreamOptions {
|
|
11
|
+
/** Default `WAIT_BUFFER_LIMIT`. */
|
|
12
|
+
readonly limit?: number;
|
|
13
|
+
/** Turns a provider event into the event the harness hands on. */
|
|
14
|
+
readonly transform: (event: ConnectionEventInit) => Promise<ConnectionEvent>;
|
|
15
|
+
/** Called for every gap (including overflow) and for the stream's end. */
|
|
16
|
+
readonly onChange?: (change: {
|
|
17
|
+
readonly change: 'gap' | 'ended';
|
|
18
|
+
readonly reason: string;
|
|
19
|
+
}) => void;
|
|
20
|
+
/** Called for every event delivered into the buffer. */
|
|
21
|
+
readonly onEvent?: (event: ConnectionEvent) => void;
|
|
22
|
+
}
|
|
23
|
+
/** Wraps a provider stream in a bounded, transforming buffer. */
|
|
24
|
+
export declare function bufferedEventStream(source: EventStream, options: BufferedStreamOptions): EventStream<ConnectionEvent>;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Waits listen in memory (spec 35 § Events): from the moment the server
|
|
3
|
+
* confirms the subscription, events are pumped into a buffer of up to
|
|
4
|
+
* `WAIT_BUFFER_LIMIT`. Overflow drops the oldest event and records a gap,
|
|
5
|
+
* which verification reports as a blind spot. Each event is transformed on the
|
|
6
|
+
* way in (stamped with the connection, refs sealed, secrets scrubbed).
|
|
7
|
+
*/
|
|
8
|
+
import { WAIT_BUFFER_LIMIT } from '@noetic-tools/types';
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region Buffer
|
|
11
|
+
function done() {
|
|
12
|
+
return {
|
|
13
|
+
value: undefined,
|
|
14
|
+
done: true,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function dropOldestEvents(state) {
|
|
18
|
+
let dropped = 0;
|
|
19
|
+
while (state.buffered > state.limit) {
|
|
20
|
+
const index = state.queue.findIndex((m) => m.type === 'event');
|
|
21
|
+
state.queue.splice(index, 1);
|
|
22
|
+
state.buffered -= 1;
|
|
23
|
+
dropped += 1;
|
|
24
|
+
}
|
|
25
|
+
return dropped;
|
|
26
|
+
}
|
|
27
|
+
/** Records `dropped` overflowed events as one gap at the head, merging with an unread one. */
|
|
28
|
+
function recordOverflow(state, dropped) {
|
|
29
|
+
const merging = state.overflowGap !== undefined && state.queue[0] === state.overflowGap;
|
|
30
|
+
state.droppedTotal = merging ? state.droppedTotal + dropped : dropped;
|
|
31
|
+
const reason = `wait buffer overflowed: ${state.droppedTotal} oldest event(s) dropped`;
|
|
32
|
+
const gap = {
|
|
33
|
+
type: 'gap',
|
|
34
|
+
reason,
|
|
35
|
+
};
|
|
36
|
+
if (merging) {
|
|
37
|
+
state.queue[0] = gap;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
state.queue.unshift(gap);
|
|
41
|
+
}
|
|
42
|
+
state.overflowGap = gap;
|
|
43
|
+
return reason;
|
|
44
|
+
}
|
|
45
|
+
function trim(state, onChange) {
|
|
46
|
+
const dropped = dropOldestEvents(state);
|
|
47
|
+
if (dropped === 0) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const reason = recordOverflow(state, dropped);
|
|
51
|
+
onChange?.({
|
|
52
|
+
change: 'gap',
|
|
53
|
+
reason,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function deliver(state, message, onChange) {
|
|
57
|
+
const waiter = state.waiters.shift();
|
|
58
|
+
if (waiter) {
|
|
59
|
+
waiter({
|
|
60
|
+
value: message,
|
|
61
|
+
done: false,
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
state.queue.push(message);
|
|
66
|
+
if (message.type === 'event') {
|
|
67
|
+
state.buffered += 1;
|
|
68
|
+
}
|
|
69
|
+
trim(state, onChange);
|
|
70
|
+
}
|
|
71
|
+
function finish(state) {
|
|
72
|
+
state.ended = true;
|
|
73
|
+
for (const waiter of state.waiters.splice(0)) {
|
|
74
|
+
waiter(done());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function takeNext(state) {
|
|
78
|
+
const message = state.queue.shift();
|
|
79
|
+
if (message?.type === 'event') {
|
|
80
|
+
state.buffered -= 1;
|
|
81
|
+
}
|
|
82
|
+
if (message) {
|
|
83
|
+
return Promise.resolve({
|
|
84
|
+
value: message,
|
|
85
|
+
done: false,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (state.ended) {
|
|
89
|
+
return Promise.resolve(done());
|
|
90
|
+
}
|
|
91
|
+
return new Promise((resolve) => {
|
|
92
|
+
state.waiters.push(resolve);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region Pump
|
|
97
|
+
/** Hands one source message on; returns whether the stream ended with it. */
|
|
98
|
+
async function forwardMessage(state, message, options) {
|
|
99
|
+
if (message.type === 'event') {
|
|
100
|
+
const event = await options.transform(message.event);
|
|
101
|
+
options.onEvent?.(event);
|
|
102
|
+
deliver(state, {
|
|
103
|
+
type: 'event',
|
|
104
|
+
event,
|
|
105
|
+
}, options.onChange);
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const change = message.type === 'gap' ? 'gap' : 'ended';
|
|
109
|
+
options.onChange?.({
|
|
110
|
+
change,
|
|
111
|
+
reason: message.reason,
|
|
112
|
+
});
|
|
113
|
+
deliver(state, message, options.onChange);
|
|
114
|
+
return change === 'ended';
|
|
115
|
+
}
|
|
116
|
+
async function pump(state, source, options) {
|
|
117
|
+
try {
|
|
118
|
+
for await (const message of source) {
|
|
119
|
+
if (state.closed || (await forwardMessage(state, message, options))) {
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
if (state.closed) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const reason = e instanceof Error ? e.message : String(e);
|
|
129
|
+
options.onChange?.({
|
|
130
|
+
change: 'ended',
|
|
131
|
+
reason,
|
|
132
|
+
});
|
|
133
|
+
deliver(state, {
|
|
134
|
+
type: 'terminated',
|
|
135
|
+
reason,
|
|
136
|
+
}, options.onChange);
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
finish(state);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region Public API
|
|
144
|
+
/** Wraps a provider stream in a bounded, transforming buffer. */
|
|
145
|
+
export function bufferedEventStream(source, options) {
|
|
146
|
+
const state = {
|
|
147
|
+
limit: options.limit ?? WAIT_BUFFER_LIMIT,
|
|
148
|
+
queue: [],
|
|
149
|
+
waiters: [],
|
|
150
|
+
ended: false,
|
|
151
|
+
closed: false,
|
|
152
|
+
buffered: 0,
|
|
153
|
+
droppedTotal: 0,
|
|
154
|
+
overflowGap: undefined,
|
|
155
|
+
};
|
|
156
|
+
const closeSource = async () => {
|
|
157
|
+
state.closed = true;
|
|
158
|
+
finish(state);
|
|
159
|
+
await source.close();
|
|
160
|
+
};
|
|
161
|
+
void source.ready.then(() => pump(state, source, options), () => finish(state));
|
|
162
|
+
return {
|
|
163
|
+
ready: source.ready,
|
|
164
|
+
close: closeSource,
|
|
165
|
+
[Symbol.asyncIterator]() {
|
|
166
|
+
return {
|
|
167
|
+
next: () => takeNext(state),
|
|
168
|
+
async return() {
|
|
169
|
+
await closeSource();
|
|
170
|
+
return done();
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
//#endregion
|