@noetic-tools/core 5.19.1 → 5.20.0-next.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/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 +16 -1
- package/dist/builders/dynamic-workflow.js +222 -11
- package/dist/builders/every.d.ts +3 -3
- package/dist/builders/every.js +2 -1
- 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-child-nodes.d.ts +21 -0
- package/dist/builders/workflow-child-nodes.js +335 -0
- package/dist/builders/workflow-connection-nodes.d.ts +17 -0
- package/dist/builders/workflow-connection-nodes.js +160 -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 +44 -3
- package/dist/builders/workflow-hydrator.js +53 -495
- 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 +169 -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 +31 -6
- 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 +23 -0
- package/dist/util/harness-internals.js +35 -0
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +1170 -92
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs a `verify` step (spec 37 § How it runs). Core resolves the subject and
|
|
3
|
+
* witnesses through the harness registry, checks that `act` acts, and hands the
|
|
4
|
+
* registered `Verifier` a host; the verifier does the rest in order — declare,
|
|
5
|
+
* check before, listen, act, close, judge, record. A `no` or an `unknown` is an
|
|
6
|
+
* answer: the step only fails when `act` threw (`verify_act_failed`, after the
|
|
7
|
+
* run is recorded) or the verify is misconfigured.
|
|
8
|
+
*/
|
|
9
|
+
import { frameworkCast, isNoeticConfigError, isNoeticError, NoeticConfigError, NoeticErrorImpl, } from '@noetic-tools/types';
|
|
10
|
+
import { VERIFICATION_RUN_ITEM_TYPE, VerifyConfigErrorCode } from '@noetic-tools/types/verify';
|
|
11
|
+
import { emitFrameworkEvent, getBroadcaster } from '../runtime/broadcaster-utils.js';
|
|
12
|
+
import { ContextImpl } from '../runtime/context-impl.js';
|
|
13
|
+
import { connectionRegistryOf, evaluateTarget, requirementFor, scopeFor, signalOf, withWitnessGuard, } from './connection-steps.js';
|
|
14
|
+
import { isStepList, stepChildren } from './step-walk.js';
|
|
15
|
+
import { participantOf, runAct, storeRun, verificationOf, verifyConnectionOf } from './verify-host.js';
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region Constants
|
|
18
|
+
/** Outcomes from best to worst, for a list subject's worst outcome. */
|
|
19
|
+
const OUTCOME_RANK = {
|
|
20
|
+
pass: 0,
|
|
21
|
+
unknown: 1,
|
|
22
|
+
partial: 2,
|
|
23
|
+
fail: 3,
|
|
24
|
+
};
|
|
25
|
+
/** Step kinds that act on their own: model calls. */
|
|
26
|
+
const MODEL_KINDS = new Set([
|
|
27
|
+
'callModel',
|
|
28
|
+
'decide',
|
|
29
|
+
'acp-agent',
|
|
30
|
+
]);
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region Helpers
|
|
33
|
+
function isVerifier(value) {
|
|
34
|
+
return (typeof value === 'object' &&
|
|
35
|
+
value !== null &&
|
|
36
|
+
'prepare' in value &&
|
|
37
|
+
typeof value.prepare === 'function' &&
|
|
38
|
+
'run' in value &&
|
|
39
|
+
typeof value.run === 'function');
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The harness's verifier.
|
|
43
|
+
*
|
|
44
|
+
* @throws `NoeticConfigError` `VERIFY_NOT_CONFIGURED` when the harness has none.
|
|
45
|
+
*/
|
|
46
|
+
function verifierOf(ctx, stepId) {
|
|
47
|
+
const registered = verificationOf(ctx);
|
|
48
|
+
if (isVerifier(registered)) {
|
|
49
|
+
return registered;
|
|
50
|
+
}
|
|
51
|
+
throw new NoeticConfigError({
|
|
52
|
+
code: VerifyConfigErrorCode.NotConfigured,
|
|
53
|
+
message: `Verify '${stepId}' ran, but the harness has no verifier.`,
|
|
54
|
+
hint: "Register one: new AgentHarness({ verification: verifier() }) with verifier from '@noetic-tools/verify'.",
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function verifyInvalid(stepId, message, hint) {
|
|
58
|
+
return new NoeticConfigError({
|
|
59
|
+
code: VerifyConfigErrorCode.Invalid,
|
|
60
|
+
message: `Verify '${stepId}' ${message}`,
|
|
61
|
+
hint,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** The steps `act` runs on `subject`: a function of the subject is called with its name. */
|
|
65
|
+
function actSteps(step, subject) {
|
|
66
|
+
const act = typeof step.act === 'function' ? step.act(subject) : step.act;
|
|
67
|
+
return isStepList(act)
|
|
68
|
+
? act
|
|
69
|
+
: [
|
|
70
|
+
act,
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A list subject runs one verify per connection, so `act` must act on each:
|
|
75
|
+
* a fixed `act` that names one of the subjects would act on that one only while
|
|
76
|
+
* every other verify watched a connection nothing touched.
|
|
77
|
+
*
|
|
78
|
+
* @throws `NoeticConfigError` `VERIFY_INVALID` for such an `act`.
|
|
79
|
+
*/
|
|
80
|
+
function checkListAct(step, subjects) {
|
|
81
|
+
if (typeof step.act === 'function' || subjects.length < 2) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const all = flattenSteps(frameworkCast(actSteps(step, '')));
|
|
85
|
+
const named = all
|
|
86
|
+
.filter((s) => s.kind === 'call')
|
|
87
|
+
.map((s) => staticName(frameworkCast(s.connection)))
|
|
88
|
+
.find((name) => name !== undefined && subjects.includes(name));
|
|
89
|
+
if (named === undefined) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
throw verifyInvalid(step.id, `has a list subject, but act always acts on '${named}'.`, 'Make act a function of the subject: act: (subject) => step.call({ connection: subject, ... }).');
|
|
93
|
+
}
|
|
94
|
+
/** Every step in `act`, nested ones included, as far as they are statically known. */
|
|
95
|
+
function flattenSteps(steps) {
|
|
96
|
+
return steps.flatMap((s) => [
|
|
97
|
+
s,
|
|
98
|
+
...flattenSteps(stepChildren(s)),
|
|
99
|
+
]);
|
|
100
|
+
}
|
|
101
|
+
/** A `call` step's connection name when it is static; `undefined` when an expression or function picks it. */
|
|
102
|
+
function staticName(target) {
|
|
103
|
+
if (typeof target === 'string') {
|
|
104
|
+
return target;
|
|
105
|
+
}
|
|
106
|
+
if (typeof target === 'function') {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
return target.name;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Checks `act` against the participants' manifests: it must contain an
|
|
113
|
+
* operation with effects or a model call, and must not ask a witness to act.
|
|
114
|
+
*
|
|
115
|
+
* @throws `NoeticConfigError` `VERIFY_INVALID` for either.
|
|
116
|
+
*/
|
|
117
|
+
function checkAct(opts) {
|
|
118
|
+
const { step, act, subject, witnesses } = opts;
|
|
119
|
+
const all = flattenSteps(act);
|
|
120
|
+
const touched = new Set();
|
|
121
|
+
let acts = all.some((s) => MODEL_KINDS.has(s.kind));
|
|
122
|
+
for (const s of all) {
|
|
123
|
+
if (s.kind !== 'call') {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const name = staticName(frameworkCast(s.connection));
|
|
127
|
+
const witness = witnesses.find((w) => w.name === name);
|
|
128
|
+
const info = (witness ?? (name === subject.name ? subject : undefined))?.manifest.operations[s.op];
|
|
129
|
+
if (witness && (info === undefined || info.effects === true)) {
|
|
130
|
+
throw verifyInvalid(step.id, `asks witness '${witness.name}' to run '${s.op}' in act.`, 'A witness can look and cannot touch: act on the subject, or make the connection the subject.');
|
|
131
|
+
}
|
|
132
|
+
const effects = name === undefined || info === undefined || info.effects === true;
|
|
133
|
+
if (!effects) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
acts = true;
|
|
137
|
+
if (name !== undefined) {
|
|
138
|
+
touched.add(name);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (!acts) {
|
|
142
|
+
throw verifyInvalid(step.id, 'has nothing in act that acts.', 'Give act an operation with effects (e.g. ui.tap) or a model call; reads alone cannot cause what the verify checks.');
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
touched: [
|
|
146
|
+
...touched,
|
|
147
|
+
],
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
async function resolveOne(opts) {
|
|
151
|
+
const { ctx, target } = opts;
|
|
152
|
+
const registry = connectionRegistryOf(ctx, target.name);
|
|
153
|
+
const options = target.handle?.options;
|
|
154
|
+
const connection = await registry.resolve({
|
|
155
|
+
name: target.name,
|
|
156
|
+
scope: scopeFor(ctx, options?.fresh),
|
|
157
|
+
requirement: requirementFor(target, {}),
|
|
158
|
+
...(options?.fresh
|
|
159
|
+
? {
|
|
160
|
+
fresh: options.fresh,
|
|
161
|
+
}
|
|
162
|
+
: {}),
|
|
163
|
+
...(options?.from
|
|
164
|
+
? {
|
|
165
|
+
from: options.from,
|
|
166
|
+
}
|
|
167
|
+
: {}),
|
|
168
|
+
...(options?.setup
|
|
169
|
+
? {
|
|
170
|
+
setup: options.setup,
|
|
171
|
+
}
|
|
172
|
+
: {}),
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
target,
|
|
176
|
+
connection,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function specOf(step, id) {
|
|
180
|
+
return {
|
|
181
|
+
id,
|
|
182
|
+
...(step.intent === undefined
|
|
183
|
+
? {}
|
|
184
|
+
: {
|
|
185
|
+
intent: step.intent,
|
|
186
|
+
}),
|
|
187
|
+
...(step.expect === undefined
|
|
188
|
+
? {}
|
|
189
|
+
: {
|
|
190
|
+
expect: step.expect,
|
|
191
|
+
}),
|
|
192
|
+
...(step.never === undefined
|
|
193
|
+
? {}
|
|
194
|
+
: {
|
|
195
|
+
never: step.never,
|
|
196
|
+
}),
|
|
197
|
+
...(step.claims === undefined
|
|
198
|
+
? {}
|
|
199
|
+
: {
|
|
200
|
+
claims: step.claims,
|
|
201
|
+
}),
|
|
202
|
+
...(step.constraints === undefined
|
|
203
|
+
? {}
|
|
204
|
+
: {
|
|
205
|
+
constraints: step.constraints,
|
|
206
|
+
}),
|
|
207
|
+
...(step.close === undefined
|
|
208
|
+
? {}
|
|
209
|
+
: {
|
|
210
|
+
close: step.close,
|
|
211
|
+
}),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/** Appends the `noetic:verification_run` item (spec 34) and streams it. */
|
|
215
|
+
function emitRunItem(opts) {
|
|
216
|
+
const { ctx, stepId, subject, output } = opts;
|
|
217
|
+
const item = frameworkCast({
|
|
218
|
+
id: `item-${crypto.randomUUID()}`,
|
|
219
|
+
type: VERIFICATION_RUN_ITEM_TYPE,
|
|
220
|
+
stepId,
|
|
221
|
+
subject,
|
|
222
|
+
outcome: output.outcome,
|
|
223
|
+
verdicts: output.verdicts,
|
|
224
|
+
violations: output.violations.length,
|
|
225
|
+
gaps: output.gaps.length,
|
|
226
|
+
run: output.run,
|
|
227
|
+
});
|
|
228
|
+
if (ctx instanceof ContextImpl) {
|
|
229
|
+
ctx.allowItemTypes([
|
|
230
|
+
VERIFICATION_RUN_ITEM_TYPE,
|
|
231
|
+
]);
|
|
232
|
+
ctx.appendEmitted(item);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
ctx.itemLog.append(item);
|
|
236
|
+
}
|
|
237
|
+
emitFrameworkEvent({
|
|
238
|
+
broadcaster: getBroadcaster(ctx),
|
|
239
|
+
agentName: ctx.harness.config.name,
|
|
240
|
+
eventType: 'item_appended',
|
|
241
|
+
data: {
|
|
242
|
+
item,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/** The `VERIFY_INVALID` an act failure stands for, through any `step_failed` wrapping. */
|
|
247
|
+
function verifyInvalidCause(error) {
|
|
248
|
+
let current = error;
|
|
249
|
+
while (isNoeticError(current) && current.noeticError.kind === 'step_failed') {
|
|
250
|
+
current = current.noeticError.cause;
|
|
251
|
+
}
|
|
252
|
+
return isNoeticConfigError(current) && current.code === VerifyConfigErrorCode.Invalid
|
|
253
|
+
? current
|
|
254
|
+
: undefined;
|
|
255
|
+
}
|
|
256
|
+
function worstOutcome(outputs) {
|
|
257
|
+
return outputs.reduce((worst, o) => (OUTCOME_RANK[o.outcome] > OUTCOME_RANK[worst] ? o.outcome : worst), 'pass');
|
|
258
|
+
}
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region Run one subject
|
|
261
|
+
async function verifySubject(opts) {
|
|
262
|
+
const { step, id, subject, input, ctx, executeStep } = opts;
|
|
263
|
+
const baseCtx = frameworkCast(ctx);
|
|
264
|
+
const erased = frameworkCast(step);
|
|
265
|
+
const verifier = verifierOf(baseCtx, id);
|
|
266
|
+
const resolvedSubject = await resolveOne({
|
|
267
|
+
ctx: baseCtx,
|
|
268
|
+
stepId: id,
|
|
269
|
+
target: subject,
|
|
270
|
+
});
|
|
271
|
+
const witnessTargets = await Promise.all((step.witnesses ?? []).map((w) => evaluateTarget(w, input, ctx, id)));
|
|
272
|
+
const witnesses = await Promise.all(witnessTargets.map((target) => resolveOne({
|
|
273
|
+
ctx: baseCtx,
|
|
274
|
+
stepId: id,
|
|
275
|
+
target,
|
|
276
|
+
})));
|
|
277
|
+
const act = actSteps(step, subject.name);
|
|
278
|
+
const { touched } = checkAct({
|
|
279
|
+
step: erased,
|
|
280
|
+
act: frameworkCast(act),
|
|
281
|
+
subject: resolvedSubject.connection,
|
|
282
|
+
witnesses: witnesses.map((w) => w.connection),
|
|
283
|
+
});
|
|
284
|
+
const prepared = verifier.prepare(specOf(erased, id), [
|
|
285
|
+
participantOf(resolvedSubject.connection, 'subject'),
|
|
286
|
+
...witnesses.map((w) => participantOf(w.connection, 'witness')),
|
|
287
|
+
]);
|
|
288
|
+
let actError;
|
|
289
|
+
const output = await verifier.run(prepared, {
|
|
290
|
+
signal: signalOf(baseCtx),
|
|
291
|
+
now: () => Date.now(),
|
|
292
|
+
subject: verifyConnectionOf({
|
|
293
|
+
resolved: resolvedSubject.connection,
|
|
294
|
+
role: 'subject',
|
|
295
|
+
ctx: baseCtx,
|
|
296
|
+
stepId: id,
|
|
297
|
+
}),
|
|
298
|
+
witnesses: Object.fromEntries(witnesses.map((w) => [
|
|
299
|
+
w.connection.name,
|
|
300
|
+
verifyConnectionOf({
|
|
301
|
+
resolved: w.connection,
|
|
302
|
+
role: 'witness',
|
|
303
|
+
ctx: baseCtx,
|
|
304
|
+
stepId: id,
|
|
305
|
+
}),
|
|
306
|
+
])),
|
|
307
|
+
act: async () => {
|
|
308
|
+
const result = await withWitnessGuard(baseCtx, witnesses.map((w) => w.connection.name), () => runAct({
|
|
309
|
+
steps: act,
|
|
310
|
+
input,
|
|
311
|
+
ctx,
|
|
312
|
+
executeStep,
|
|
313
|
+
touched,
|
|
314
|
+
}));
|
|
315
|
+
actError = result.error;
|
|
316
|
+
return result;
|
|
317
|
+
},
|
|
318
|
+
record: (run) => storeRun({
|
|
319
|
+
ctx: baseCtx,
|
|
320
|
+
stepId: id,
|
|
321
|
+
run,
|
|
322
|
+
}),
|
|
323
|
+
});
|
|
324
|
+
emitRunItem({
|
|
325
|
+
ctx: baseCtx,
|
|
326
|
+
stepId: id,
|
|
327
|
+
subject: resolvedSubject.connection.name,
|
|
328
|
+
output,
|
|
329
|
+
});
|
|
330
|
+
// A witness asked to act is a malformed verify, not a failed action.
|
|
331
|
+
const invalid = verifyInvalidCause(actError);
|
|
332
|
+
if (invalid) {
|
|
333
|
+
throw invalid;
|
|
334
|
+
}
|
|
335
|
+
if (actError) {
|
|
336
|
+
throw new NoeticErrorImpl({
|
|
337
|
+
kind: 'verify_act_failed',
|
|
338
|
+
stepId: id,
|
|
339
|
+
cause: actError,
|
|
340
|
+
run: output.run,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return output;
|
|
344
|
+
}
|
|
345
|
+
//#endregion
|
|
346
|
+
//#region Public API
|
|
347
|
+
/**
|
|
348
|
+
* Executes a `verify` step. With a list subject, runs one verify per connection
|
|
349
|
+
* in parallel (ids `<id>.<name>`) and returns each output with the worst outcome.
|
|
350
|
+
*
|
|
351
|
+
* @throws `NoeticConfigError` `VERIFY_NOT_CONFIGURED` when the harness has no verifier.
|
|
352
|
+
* @throws `NoeticConfigError` `VERIFY_INVALID` when `act` contains nothing that acts, a
|
|
353
|
+
* matcher cannot be parsed, or a witness is asked to act.
|
|
354
|
+
* @throws `NoeticConfigError` `CONNECTION_UNKNOWN`, `CONNECTION_UNSATISFIED` (a sense or
|
|
355
|
+
* event no participant offers), `CONNECTION_NOT_ALLOWED`, `CONNECTION_INVALID` from resolution.
|
|
356
|
+
* @throws `NoeticErrorImpl` `verify_act_failed` when `act` threw; the run is recorded first.
|
|
357
|
+
*/
|
|
358
|
+
export async function executeVerify(step, input, ctx, executeStep) {
|
|
359
|
+
const erased = frameworkCast(step);
|
|
360
|
+
if (!isStepList(step.subject)) {
|
|
361
|
+
const subject = await evaluateTarget(step.subject, input, ctx, step.id);
|
|
362
|
+
const output = await verifySubject({
|
|
363
|
+
step: erased,
|
|
364
|
+
id: step.id,
|
|
365
|
+
subject,
|
|
366
|
+
input,
|
|
367
|
+
ctx,
|
|
368
|
+
executeStep,
|
|
369
|
+
});
|
|
370
|
+
return frameworkCast(output);
|
|
371
|
+
}
|
|
372
|
+
const targets = await Promise.all(step.subject.map((s) => evaluateTarget(s, input, ctx, step.id)));
|
|
373
|
+
checkListAct(frameworkCast(step), targets.map((t) => t.name));
|
|
374
|
+
const outputs = await Promise.all(targets.map((subject) => verifySubject({
|
|
375
|
+
step: erased,
|
|
376
|
+
id: `${step.id}.${subject.name}`,
|
|
377
|
+
subject,
|
|
378
|
+
input,
|
|
379
|
+
ctx,
|
|
380
|
+
executeStep,
|
|
381
|
+
})));
|
|
382
|
+
const result = {
|
|
383
|
+
outcome: worstOutcome(outputs),
|
|
384
|
+
subjects: Object.fromEntries(targets.map((t, i) => [
|
|
385
|
+
t.name,
|
|
386
|
+
outputs[i],
|
|
387
|
+
])),
|
|
388
|
+
};
|
|
389
|
+
return frameworkCast(result);
|
|
390
|
+
}
|
|
391
|
+
//#endregion
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs a `wait` step: listens for a connection event, runs `around` once the
|
|
3
|
+
* server confirms the subscription, then waits for the first event the filter
|
|
4
|
+
* accepts (spec 35 § Steps). Listening starts before `around`, so the event an
|
|
5
|
+
* action causes cannot slip past. A pending ledger entry records the start
|
|
6
|
+
* cursor and the deadline, so a resumed run listens again from where it was
|
|
7
|
+
* (spec 23a § Connection Steps).
|
|
8
|
+
*/
|
|
9
|
+
import type { Context, ExecuteStepFn, StepWait } from '@noetic-tools/types';
|
|
10
|
+
/** Options the interpreter passes in. */
|
|
11
|
+
export interface ExecuteWaitOptions {
|
|
12
|
+
/** The step's ledger key, from which its pending entry derives. */
|
|
13
|
+
ledgerKey?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Executes a `wait` step and returns `{ event, around }`.
|
|
17
|
+
*
|
|
18
|
+
* @throws `NoeticConfigError` `CONNECTION_UNKNOWN`, `CONNECTION_UNSATISFIED`, `CONNECTION_NOT_ALLOWED` or `CONNECTION_INVALID` from resolution.
|
|
19
|
+
* @throws `NoeticErrorImpl` `connection_unavailable` when the event stream ends, `connection_resume_refused` on a resume against a replaced connection, `step_failed` when `timeoutMs` passes, `cancelled` when the run is aborted.
|
|
20
|
+
*/
|
|
21
|
+
export declare function executeWait<TContext, I, O>(step: StepWait<TContext, I, O>, input: I, ctx: Context<TContext>, executeStep: ExecuteStepFn, options?: ExecuteWaitOptions): Promise<O>;
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs a `wait` step: listens for a connection event, runs `around` once the
|
|
3
|
+
* server confirms the subscription, then waits for the first event the filter
|
|
4
|
+
* accepts (spec 35 § Steps). Listening starts before `around`, so the event an
|
|
5
|
+
* action causes cannot slip past. A pending ledger entry records the start
|
|
6
|
+
* cursor and the deadline, so a resumed run listens again from where it was
|
|
7
|
+
* (spec 23a § Connection Steps).
|
|
8
|
+
*/
|
|
9
|
+
import { frameworkCast, NoeticErrorImpl } from '@noetic-tools/types';
|
|
10
|
+
import { evaluateTarget, reconcilePending, resolveStepConnection, signalOf, } from './connection-steps.js';
|
|
11
|
+
import { isStepList } from './step-walk.js';
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region Helpers
|
|
14
|
+
function aroundSteps(step) {
|
|
15
|
+
if (step.around === undefined) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
if (isStepList(step.around)) {
|
|
19
|
+
return step.around;
|
|
20
|
+
}
|
|
21
|
+
return [
|
|
22
|
+
step.around,
|
|
23
|
+
];
|
|
24
|
+
}
|
|
25
|
+
/** Runs `around` in sequence, each step's output the next one's input. */
|
|
26
|
+
async function runAround(steps, input, ctx, executeStep) {
|
|
27
|
+
let current = input;
|
|
28
|
+
for (const child of steps) {
|
|
29
|
+
current = await executeStep(child, frameworkCast(current), ctx);
|
|
30
|
+
}
|
|
31
|
+
return current;
|
|
32
|
+
}
|
|
33
|
+
/** Consumes the stream until an event the filter accepts arrives, or the stream ends. */
|
|
34
|
+
async function listenForMatch(listen) {
|
|
35
|
+
const { step, input, ctx, stream } = listen;
|
|
36
|
+
// The registry's stream emits `connection_event` and `stream_changed` (once each).
|
|
37
|
+
for await (const message of stream) {
|
|
38
|
+
if (message.type === 'gap') {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (message.type === 'terminated') {
|
|
42
|
+
return {
|
|
43
|
+
kind: 'ended',
|
|
44
|
+
reason: message.reason,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const event = message.event;
|
|
48
|
+
if (event.type !== step.event) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (step.filter && !(await step.filter(event, input, ctx))) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
kind: 'matched',
|
|
56
|
+
event,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
kind: 'ended',
|
|
61
|
+
reason: 'the event stream closed',
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Resolves `undefined` once `deadline` passes or `signal` aborts; `clear` cancels the timer. */
|
|
65
|
+
function deadlineTimer(deadline, signal) {
|
|
66
|
+
let timer;
|
|
67
|
+
let onAbort;
|
|
68
|
+
const promise = new Promise((resolve) => {
|
|
69
|
+
timer = setTimeout(() => resolve(undefined), Math.max(0, deadline - Date.now()));
|
|
70
|
+
onAbort = () => resolve(undefined);
|
|
71
|
+
signal.addEventListener('abort', onAbort, {
|
|
72
|
+
once: true,
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
promise,
|
|
77
|
+
clear: () => {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
if (onAbort) {
|
|
80
|
+
signal.removeEventListener('abort', onAbort);
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function endedError(connection, stepId, reason) {
|
|
86
|
+
return new NoeticErrorImpl({
|
|
87
|
+
kind: 'connection_unavailable',
|
|
88
|
+
connection,
|
|
89
|
+
reason: `the event stream wait '${stepId}' was listening on ended: ${reason}`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
function timeoutError(step) {
|
|
93
|
+
return new NoeticErrorImpl({
|
|
94
|
+
kind: 'step_failed',
|
|
95
|
+
stepId: step.id,
|
|
96
|
+
cause: new Error(`wait '${step.id}' saw no matching '${step.event}' event within ${step.timeoutMs} ms.`),
|
|
97
|
+
retriesExhausted: true,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/** Waits for the listener or the deadline, whichever comes first; `undefined` means the deadline. */
|
|
101
|
+
async function awaitOutcome(matched, deadline, signal) {
|
|
102
|
+
const timer = deadlineTimer(deadline, signal);
|
|
103
|
+
try {
|
|
104
|
+
return await Promise.race([
|
|
105
|
+
matched,
|
|
106
|
+
timer.promise,
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
timer.clear();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/** Records the pending entry once the server confirms the subscription; resolves the deadline. */
|
|
114
|
+
async function recordListening(listen, prior) {
|
|
115
|
+
const { step, connection, stream } = listen;
|
|
116
|
+
const { cursor } = await stream.ready;
|
|
117
|
+
const deadline = prior?.deadline ?? Date.now() + step.timeoutMs;
|
|
118
|
+
await connection.pending?.write({
|
|
119
|
+
stepId: step.id,
|
|
120
|
+
kind: 'wait',
|
|
121
|
+
connection: connection.resolved.name,
|
|
122
|
+
key: prior?.key ?? connection.idempotencyKey,
|
|
123
|
+
identity: connection.resolved.identity(),
|
|
124
|
+
cursor: prior?.cursor ?? cursor,
|
|
125
|
+
deadline,
|
|
126
|
+
...(connection.resolved.lease
|
|
127
|
+
? {
|
|
128
|
+
leaseId: connection.resolved.lease.id,
|
|
129
|
+
}
|
|
130
|
+
: {}),
|
|
131
|
+
});
|
|
132
|
+
return deadline;
|
|
133
|
+
}
|
|
134
|
+
/** Listens, runs `around`, then waits for the outcome and turns it into the step's output. */
|
|
135
|
+
async function waitOnStream(opts) {
|
|
136
|
+
const { listen, prior, executeStep, runSignal } = opts;
|
|
137
|
+
const { step, input, ctx } = listen;
|
|
138
|
+
const deadline = await recordListening(listen, prior);
|
|
139
|
+
const matched = listenForMatch(listen);
|
|
140
|
+
// A listener that fails while `around` runs is reported after it.
|
|
141
|
+
matched.catch(() => undefined);
|
|
142
|
+
const around = step.around === undefined
|
|
143
|
+
? undefined
|
|
144
|
+
: await runAround(aroundSteps(step), input, ctx, executeStep);
|
|
145
|
+
const outcome = await awaitOutcome(matched, deadline, runSignal);
|
|
146
|
+
const event = matchedEvent(listen, outcome, runSignal);
|
|
147
|
+
await listen.connection.pending?.clear();
|
|
148
|
+
return {
|
|
149
|
+
event,
|
|
150
|
+
...(step.around === undefined
|
|
151
|
+
? {}
|
|
152
|
+
: {
|
|
153
|
+
around,
|
|
154
|
+
}),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The event that ended the wait (the registry already emitted it as `connection_event`).
|
|
159
|
+
*
|
|
160
|
+
* @throws `NoeticErrorImpl` `cancelled`, `step_failed` (timeout) or `connection_unavailable` (stream ended).
|
|
161
|
+
*/
|
|
162
|
+
function matchedEvent(listen, outcome, runSignal) {
|
|
163
|
+
const { step, connection } = listen;
|
|
164
|
+
const baseCtx = frameworkCast(listen.ctx);
|
|
165
|
+
if (runSignal.aborted) {
|
|
166
|
+
throw new NoeticErrorImpl({
|
|
167
|
+
kind: 'cancelled',
|
|
168
|
+
reason: baseCtx.abortReason ?? 'context aborted',
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
if (outcome === undefined) {
|
|
172
|
+
throw timeoutError(step);
|
|
173
|
+
}
|
|
174
|
+
if (outcome.kind === 'ended') {
|
|
175
|
+
throw endedError(connection.resolved.name, step.id, outcome.reason);
|
|
176
|
+
}
|
|
177
|
+
return outcome.event;
|
|
178
|
+
}
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region Public API
|
|
181
|
+
/**
|
|
182
|
+
* Executes a `wait` step and returns `{ event, around }`.
|
|
183
|
+
*
|
|
184
|
+
* @throws `NoeticConfigError` `CONNECTION_UNKNOWN`, `CONNECTION_UNSATISFIED`, `CONNECTION_NOT_ALLOWED` or `CONNECTION_INVALID` from resolution.
|
|
185
|
+
* @throws `NoeticErrorImpl` `connection_unavailable` when the event stream ends, `connection_resume_refused` on a resume against a replaced connection, `step_failed` when `timeoutMs` passes, `cancelled` when the run is aborted.
|
|
186
|
+
*/
|
|
187
|
+
export async function executeWait(step, input, ctx, executeStep, options = {}) {
|
|
188
|
+
const baseCtx = frameworkCast(ctx);
|
|
189
|
+
const erased = frameworkCast(step);
|
|
190
|
+
const target = await evaluateTarget(step.connection, input, ctx, step.id);
|
|
191
|
+
const connection = await resolveStepConnection({
|
|
192
|
+
ctx: baseCtx,
|
|
193
|
+
stepId: step.id,
|
|
194
|
+
target,
|
|
195
|
+
use: {
|
|
196
|
+
event: step.event,
|
|
197
|
+
},
|
|
198
|
+
ledgerKey: options.ledgerKey,
|
|
199
|
+
});
|
|
200
|
+
const runSignal = signalOf(baseCtx);
|
|
201
|
+
const prior = await reconcilePending(connection, runSignal, baseCtx);
|
|
202
|
+
const listening = new AbortController();
|
|
203
|
+
const stopListening = () => listening.abort();
|
|
204
|
+
runSignal.addEventListener('abort', stopListening, {
|
|
205
|
+
once: true,
|
|
206
|
+
});
|
|
207
|
+
const stream = connection.resolved.listen({
|
|
208
|
+
event: step.event,
|
|
209
|
+
cursor: prior?.cursor ?? null,
|
|
210
|
+
signal: listening.signal,
|
|
211
|
+
});
|
|
212
|
+
try {
|
|
213
|
+
const output = await waitOnStream({
|
|
214
|
+
listen: {
|
|
215
|
+
step: erased,
|
|
216
|
+
input,
|
|
217
|
+
ctx,
|
|
218
|
+
connection,
|
|
219
|
+
stream,
|
|
220
|
+
},
|
|
221
|
+
prior,
|
|
222
|
+
executeStep,
|
|
223
|
+
runSignal,
|
|
224
|
+
});
|
|
225
|
+
return frameworkCast(output);
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
runSignal.removeEventListener('abort', stopListening);
|
|
229
|
+
listening.abort();
|
|
230
|
+
await stream.close().catch(() => undefined);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
//#endregion
|