@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.
Files changed (146) hide show
  1. package/dist/adapters/openrouter.js +5 -0
  2. package/dist/builders/control-flow-builders.d.ts +15 -6
  3. package/dist/builders/control-flow-builders.js +8 -0
  4. package/dist/builders/decide-builders.d.ts +2 -5
  5. package/dist/builders/dynamic-workflow.d.ts +16 -1
  6. package/dist/builders/dynamic-workflow.js +222 -11
  7. package/dist/builders/every.d.ts +3 -3
  8. package/dist/builders/every.js +2 -1
  9. package/dist/builders/provide-builder.d.ts +3 -2
  10. package/dist/builders/provide-builder.js +2 -1
  11. package/dist/builders/sequence.d.ts +14 -0
  12. package/dist/builders/sequence.js +103 -0
  13. package/dist/builders/spawn-builder.d.ts +3 -2
  14. package/dist/builders/spawn-builder.js +3 -2
  15. package/dist/builders/step-builders.d.ts +80 -1
  16. package/dist/builders/step-builders.js +115 -1
  17. package/dist/builders/verify-builder.d.ts +79 -0
  18. package/dist/builders/verify-builder.js +69 -0
  19. package/dist/builders/workflow-child-nodes.d.ts +21 -0
  20. package/dist/builders/workflow-child-nodes.js +335 -0
  21. package/dist/builders/workflow-connection-nodes.d.ts +17 -0
  22. package/dist/builders/workflow-connection-nodes.js +160 -0
  23. package/dist/builders/workflow-connections.d.ts +90 -0
  24. package/dist/builders/workflow-connections.js +560 -0
  25. package/dist/builders/workflow-control-nodes.d.ts +13 -0
  26. package/dist/builders/workflow-control-nodes.js +213 -0
  27. package/dist/builders/workflow-hydrator.d.ts +44 -3
  28. package/dist/builders/workflow-hydrator.js +53 -495
  29. package/dist/builders/workflow-verify.d.ts +19 -0
  30. package/dist/builders/workflow-verify.js +194 -0
  31. package/dist/conditions/conditions.d.ts +7 -5
  32. package/dist/conditions/conditions.js +7 -4
  33. package/dist/connections/blobs.d.ts +33 -0
  34. package/dist/connections/blobs.js +77 -0
  35. package/dist/connections/cron-schedule.d.ts +32 -0
  36. package/dist/connections/cron-schedule.js +213 -0
  37. package/dist/connections/durability.d.ts +65 -0
  38. package/dist/connections/durability.js +54 -0
  39. package/dist/connections/errors.d.ts +30 -0
  40. package/dist/connections/errors.js +100 -0
  41. package/dist/connections/event-stream.d.ts +24 -0
  42. package/dist/connections/event-stream.js +176 -0
  43. package/dist/connections/gates.d.ts +94 -0
  44. package/dist/connections/gates.js +271 -0
  45. package/dist/connections/glob.d.ts +10 -0
  46. package/dist/connections/glob.js +46 -0
  47. package/dist/connections/handle.d.ts +22 -0
  48. package/dist/connections/handle.js +84 -0
  49. package/dist/connections/harness-wiring.d.ts +52 -0
  50. package/dist/connections/harness-wiring.js +143 -0
  51. package/dist/connections/in-memory-connection.d.ts +147 -0
  52. package/dist/connections/in-memory-connection.js +593 -0
  53. package/dist/connections/index.d.ts +34 -0
  54. package/dist/connections/index.js +23 -0
  55. package/dist/connections/json-schema.d.ts +28 -0
  56. package/dist/connections/json-schema.js +338 -0
  57. package/dist/connections/model-tools.d.ts +46 -0
  58. package/dist/connections/model-tools.js +466 -0
  59. package/dist/connections/negotiation.d.ts +30 -0
  60. package/dist/connections/negotiation.js +159 -0
  61. package/dist/connections/policy.d.ts +21 -0
  62. package/dist/connections/policy.js +34 -0
  63. package/dist/connections/pool.d.ts +111 -0
  64. package/dist/connections/pool.js +339 -0
  65. package/dist/connections/refs.d.ts +74 -0
  66. package/dist/connections/refs.js +316 -0
  67. package/dist/connections/registry-types.d.ts +141 -0
  68. package/dist/connections/registry-types.js +14 -0
  69. package/dist/connections/registry.d.ts +125 -0
  70. package/dist/connections/registry.js +775 -0
  71. package/dist/connections/secrets.d.ts +59 -0
  72. package/dist/connections/secrets.js +153 -0
  73. package/dist/connections/settle.d.ts +9 -0
  74. package/dist/connections/settle.js +26 -0
  75. package/dist/connections/sources.d.ts +27 -0
  76. package/dist/connections/sources.js +300 -0
  77. package/dist/connections/triggers.d.ts +78 -0
  78. package/dist/connections/triggers.js +310 -0
  79. package/dist/connections/values.d.ts +26 -0
  80. package/dist/connections/values.js +93 -0
  81. package/dist/connections/views.d.ts +31 -0
  82. package/dist/connections/views.js +176 -0
  83. package/dist/expression/check.js +80 -1
  84. package/dist/expression/jsonata.d.ts +14 -4
  85. package/dist/expression/jsonata.js +27 -9
  86. package/dist/expression/step-outputs.d.ts +25 -0
  87. package/dist/expression/step-outputs.js +78 -0
  88. package/dist/harness/agent-harness.d.ts +69 -166
  89. package/dist/harness/agent-harness.js +170 -262
  90. package/dist/harness/call-span-collector.d.ts +33 -0
  91. package/dist/harness/call-span-collector.js +83 -0
  92. package/dist/harness/connection-gate.d.ts +78 -0
  93. package/dist/harness/connection-gate.js +169 -0
  94. package/dist/harness/deps/interpreter.d.ts +1 -0
  95. package/dist/harness/deps/interpreter.js +1 -0
  96. package/dist/harness/deps/runtime.d.ts +1 -1
  97. package/dist/harness/deps/types.d.ts +1 -1
  98. package/dist/harness/harness-options.d.ts +153 -0
  99. package/dist/harness/harness-options.js +36 -0
  100. package/dist/harness/harness-tracing.d.ts +16 -0
  101. package/dist/harness/harness-tracing.js +35 -0
  102. package/dist/harness/llm-client.d.ts +71 -0
  103. package/dist/harness/llm-client.js +218 -0
  104. package/dist/harness/model-call.d.ts +2 -7
  105. package/dist/harness/model-call.js +21 -549
  106. package/dist/harness/model-request.d.ts +42 -0
  107. package/dist/harness/model-request.js +159 -0
  108. package/dist/harness/model-stream.d.ts +39 -0
  109. package/dist/harness/model-stream.js +131 -0
  110. package/dist/harness/model-tool-round.d.ts +37 -0
  111. package/dist/harness/model-tool-round.js +197 -0
  112. package/dist/harness/spend.d.ts +2 -0
  113. package/dist/harness/spend.js +5 -1
  114. package/dist/index.d.ts +31 -6
  115. package/dist/index.js +18 -1
  116. package/dist/interpreter/connection-steps.d.ts +194 -0
  117. package/dist/interpreter/connection-steps.js +537 -0
  118. package/dist/interpreter/execute-action.d.ts +2 -8
  119. package/dist/interpreter/execute-action.js +3 -178
  120. package/dist/interpreter/execute-call.d.ts +19 -0
  121. package/dist/interpreter/execute-call.js +99 -0
  122. package/dist/interpreter/execute-control.d.ts +6 -1
  123. package/dist/interpreter/execute-control.js +14 -0
  124. package/dist/interpreter/execute-invoke-tool.d.ts +11 -0
  125. package/dist/interpreter/execute-invoke-tool.js +183 -0
  126. package/dist/interpreter/execute-verify.d.ts +21 -0
  127. package/dist/interpreter/execute-verify.js +391 -0
  128. package/dist/interpreter/execute-wait.d.ts +21 -0
  129. package/dist/interpreter/execute-wait.js +233 -0
  130. package/dist/interpreter/execute.js +24 -133
  131. package/dist/interpreter/step-kind-dispatch.d.ts +24 -0
  132. package/dist/interpreter/step-kind-dispatch.js +157 -0
  133. package/dist/interpreter/step-walk.d.ts +2 -0
  134. package/dist/interpreter/step-walk.js +24 -0
  135. package/dist/interpreter/verify-host.d.ts +40 -0
  136. package/dist/interpreter/verify-host.js +245 -0
  137. package/dist/runtime/durable/step-ledger.d.ts +31 -1
  138. package/dist/runtime/durable/step-ledger.js +87 -1
  139. package/dist/runtime/in-memory-artifact-store.d.ts +24 -0
  140. package/dist/runtime/in-memory-artifact-store.js +240 -0
  141. package/dist/schemas/workflow.d.ts +2 -2
  142. package/dist/schemas/workflow.js +1 -1
  143. package/dist/util/harness-internals.d.ts +23 -0
  144. package/dist/util/harness-internals.js +35 -0
  145. package/package.json +3 -3
  146. package/schema/noetic-workflow.schema.json +1170 -92
@@ -1,12 +1,9 @@
1
1
  /**
2
- * Action step handlers: run, llm, spawn, provide, tool.
2
+ * Action step handlers: run, llm, spawn, provide.
3
3
  */
4
- import { createMessage, estimateTokens, extractAssistantText, frameworkCast, isNoeticError, isOutputCodec, NoeticConfigError, NoeticErrorImpl, validateSchema, } from '@noetic-tools/types';
5
- import { ToolAttr } from '../observability/genai-attributes.js';
6
- import { askApproval } from '../runtime/tool-approval.js';
7
- import { emitToolUi } from '../runtime/tool-ui.js';
4
+ import { createMessage, estimateTokens, extractAssistantText, frameworkCast, isNoeticError, isOutputCodec, NoeticConfigError, NoeticErrorImpl, } from '@noetic-tools/types';
8
5
  import { chargeSpent } from '../util/spent.js';
9
- import { allocateBudgets, assembleView, buildToolExecutionContext, ContextImpl, commitLayerUsage, computeLayerUsage, contextToExecCtx, createContextCacheStore, DEFAULT_PROJECTION, defaultItemSchemaRegistry, disposeLayers, emitFrameworkEvent, getBroadcaster, INTERRUPT_REASON, initLayers, lineageKey, noteCacheOutcome, resolveCacheConfig, resolveLayerTools, returnLayers, shouldEmit, snapshotCwdState, spawnLayers, unpinLayers, } from './action-deps.js';
6
+ import { allocateBudgets, assembleView, ContextImpl, commitLayerUsage, computeLayerUsage, contextToExecCtx, createContextCacheStore, DEFAULT_PROJECTION, defaultItemSchemaRegistry, disposeLayers, emitFrameworkEvent, getBroadcaster, INTERRUPT_REASON, initLayers, lineageKey, noteCacheOutcome, resolveCacheConfig, resolveLayerTools, returnLayers, shouldEmit, snapshotCwdState, spawnLayers, unpinLayers, } from './action-deps.js';
10
7
  import { isServerToolSpec, SteeringAction } from './action-types.js';
11
8
  import { openChildSpan, traceChild } from './child-span.js';
12
9
  import { cloneWithGuard } from './clone-guard.js';
@@ -970,175 +967,3 @@ export async function executeWithContext(step, input, ctx, executeStep, opts) {
970
967
  }
971
968
  }
972
969
  //#endregion
973
- //#region tool
974
- function isAsyncGenerator(value) {
975
- if (typeof value !== 'object' || value === null) {
976
- return false;
977
- }
978
- return Symbol.asyncIterator in value;
979
- }
980
- async function consumeToolGenerator(params) {
981
- const broadcaster = getBroadcaster(params.ctx);
982
- const agentName = params.ctx.harness.config.name;
983
- const events = [];
984
- while (true) {
985
- const next = await params.generator.next();
986
- if (next.done) {
987
- return next.value;
988
- }
989
- events.push(next.value);
990
- emitFrameworkEvent({
991
- broadcaster,
992
- agentName,
993
- eventType: 'tool_progress',
994
- data: {
995
- stepId: params.stepId,
996
- toolName: params.tool.name,
997
- event: next.value,
998
- },
999
- });
1000
- // A direct `invokeTool` has no model call id — the step id keys its region.
1001
- emitToolUi({
1002
- ctx: params.ctx,
1003
- tool: params.tool,
1004
- callId: params.stepId,
1005
- phase: 'progress',
1006
- args: params.args,
1007
- events,
1008
- });
1009
- }
1010
- }
1011
- /**
1012
- * Runs an `invokeTool` step inside a `tool.call` span under `ctx.span`. The span
1013
- * covers validation, the steering gate, and execution, so a denied or failing
1014
- * call is traced with its error.
1015
- */
1016
- export async function executeInvokeTool(step, input, ctx, harness, layers) {
1017
- const baseCtx = frameworkCast(ctx);
1018
- const span = openChildSpan(baseCtx, 'tool.call', step.id);
1019
- span.setAttribute(ToolAttr.NAME, step.tool.name);
1020
- span.setAttribute(ToolAttr.NEEDS_APPROVAL, step.tool.needsApproval ?? false);
1021
- return traceChild(baseCtx, span, () => runInvokeTool(step, input, baseCtx, harness, layers, span));
1022
- }
1023
- /**
1024
- * Gates run in order — input validation, the `beforeToolCall` layers, then the
1025
- * harness approver for a tool that declares `needsApproval` — and a refusal at
1026
- * any gate throws `steering_denied` before the tool runs.
1027
- */
1028
- async function runInvokeTool(step, input, baseCtx, harness, layers, span) {
1029
- const args = step.args ? Object.assign({}, input, step.args) : input;
1030
- const parseResult = await validateSchema(step.tool.input, args);
1031
- if (!parseResult.success) {
1032
- throw new NoeticErrorImpl({
1033
- kind: 'step_failed',
1034
- stepId: step.id,
1035
- cause: new Error(`Tool input validation failed: ${parseResult.zodError.message}`),
1036
- retriesExhausted: false,
1037
- });
1038
- }
1039
- const parsedArgs = parseResult.value;
1040
- // The started event precedes the gate so a parked call (e.g. one awaiting an
1041
- // external permission decision) is visible as pending rather than invisible
1042
- // until approved — the same ordering the model-driven path guarantees.
1043
- const broadcaster = getBroadcaster(baseCtx);
1044
- const agentName = baseCtx.harness.config.name;
1045
- emitFrameworkEvent({
1046
- broadcaster,
1047
- agentName,
1048
- eventType: 'tool_call_started',
1049
- data: {
1050
- name: step.tool.name,
1051
- callId: step.id,
1052
- },
1053
- });
1054
- const emitCompleted = (error, output) => {
1055
- emitFrameworkEvent({
1056
- broadcaster,
1057
- agentName,
1058
- eventType: 'tool_call_completed',
1059
- data: {
1060
- name: step.tool.name,
1061
- callId: step.id,
1062
- error,
1063
- output,
1064
- },
1065
- });
1066
- };
1067
- const refuse = (decision) => {
1068
- emitCompleted(true, `Tool call denied: ${decision.guidance ?? 'steering rule violation'}`);
1069
- throw new NoeticErrorImpl({
1070
- kind: 'steering_denied',
1071
- guidance: decision.guidance,
1072
- });
1073
- };
1074
- if (layers && layers.length > 0) {
1075
- const decision = await harness.beforeToolCall(layers, step.tool.name, parsedArgs, baseCtx, step.id);
1076
- if (decision.action !== SteeringAction.Allow) {
1077
- refuse(decision);
1078
- }
1079
- }
1080
- if (step.tool.needsApproval) {
1081
- const approval = await askApproval({
1082
- harness,
1083
- toolName: step.tool.name,
1084
- toolArgs: parsedArgs,
1085
- callId: step.id,
1086
- ctx: baseCtx,
1087
- });
1088
- span.setAttribute(ToolAttr.APPROVAL, approval.outcome);
1089
- if (approval.decision.action !== SteeringAction.Allow) {
1090
- refuse(approval.decision);
1091
- }
1092
- }
1093
- emitToolUi({
1094
- ctx: baseCtx,
1095
- tool: step.tool,
1096
- callId: step.id,
1097
- phase: 'call',
1098
- args: parsedArgs,
1099
- });
1100
- try {
1101
- const toolCtx = buildToolExecutionContext(baseCtx, harness);
1102
- const execResult = step.tool.execute(parsedArgs, toolCtx);
1103
- const result = isAsyncGenerator(execResult)
1104
- ? await consumeToolGenerator({
1105
- generator: execResult,
1106
- stepId: step.id,
1107
- tool: step.tool,
1108
- ctx: baseCtx,
1109
- args: parsedArgs,
1110
- })
1111
- : await execResult;
1112
- emitToolUi({
1113
- ctx: baseCtx,
1114
- tool: step.tool,
1115
- callId: step.id,
1116
- phase: 'result',
1117
- args: parsedArgs,
1118
- output: result,
1119
- });
1120
- emitCompleted(false, typeof result === 'string' ? result : JSON.stringify(result));
1121
- return frameworkCast(result);
1122
- }
1123
- catch (e) {
1124
- emitToolUi({
1125
- ctx: baseCtx,
1126
- tool: step.tool,
1127
- callId: step.id,
1128
- phase: 'error',
1129
- args: parsedArgs,
1130
- error: e,
1131
- });
1132
- emitCompleted(true, e instanceof Error ? e.message : String(e));
1133
- if (e instanceof NoeticErrorImpl) {
1134
- throw e;
1135
- }
1136
- throw new NoeticErrorImpl({
1137
- kind: 'step_failed',
1138
- stepId: step.id,
1139
- cause: e instanceof Error ? e : new Error(String(e)),
1140
- retriesExhausted: false,
1141
- });
1142
- }
1143
- }
1144
- //#endregion
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Runs a `call` step: one connection operation through the harness registry's
3
+ * gates, waiting on long operations (spec 35 § Steps). A pending ledger entry
4
+ * is written before the call reaches the server, so a resumed run re-sends with
5
+ * the same idempotency key instead of acting twice (spec 23a § Connection Steps).
6
+ */
7
+ import type { Context, StepCall } from '@noetic-tools/types';
8
+ /** Options the interpreter passes in. */
9
+ export interface ExecuteCallOptions {
10
+ /** The step's ledger key, from which its idempotency key and pending entry derive. */
11
+ ledgerKey?: string;
12
+ }
13
+ /**
14
+ * Executes a `call` step and returns the operation's output.
15
+ *
16
+ * @throws `NoeticConfigError` `CONNECTION_UNKNOWN`, `CONNECTION_UNSATISFIED`, `CONNECTION_NOT_ALLOWED` or `CONNECTION_INVALID` from resolution.
17
+ * @throws `NoeticErrorImpl` `steering_denied` when a gate refuses, or `connection_failed`, `connection_unavailable`, `connection_ref_invalid`, `connection_secret_refused`, `connection_input_required`, `connection_resume_refused`.
18
+ */
19
+ export declare function executeCall<TContext, I, O>(step: StepCall<TContext, I, O>, input: I, ctx: Context<TContext>, options?: ExecuteCallOptions): Promise<O>;
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Runs a `call` step: one connection operation through the harness registry's
3
+ * gates, waiting on long operations (spec 35 § Steps). A pending ledger entry
4
+ * is written before the call reaches the server, so a resumed run re-sends with
5
+ * the same idempotency key instead of acting twice (spec 23a § Connection Steps).
6
+ */
7
+ import { frameworkCast } from '@noetic-tools/types';
8
+ import { evaluateTarget, reconcilePending, refuseWitnessAct, resolveStepConnection, signalOf, } from './connection-steps.js';
9
+ //#endregion
10
+ //#region Helpers
11
+ async function resolveArgs(step, input, ctx) {
12
+ if (typeof step.args === 'function') {
13
+ return await step.args(input, ctx);
14
+ }
15
+ return step.args ?? {};
16
+ }
17
+ function baseEntry(opts) {
18
+ const { connection, stepId, key, prior } = opts;
19
+ return {
20
+ stepId,
21
+ kind: 'call',
22
+ connection: connection.resolved.name,
23
+ key,
24
+ identity: connection.resolved.identity(),
25
+ ...(prior?.taskId
26
+ ? {
27
+ taskId: prior.taskId,
28
+ }
29
+ : {}),
30
+ ...(connection.resolved.lease
31
+ ? {
32
+ leaseId: connection.resolved.lease.id,
33
+ }
34
+ : {}),
35
+ };
36
+ }
37
+ //#endregion
38
+ //#region Public API
39
+ /**
40
+ * Executes a `call` step and returns the operation's output.
41
+ *
42
+ * @throws `NoeticConfigError` `CONNECTION_UNKNOWN`, `CONNECTION_UNSATISFIED`, `CONNECTION_NOT_ALLOWED` or `CONNECTION_INVALID` from resolution.
43
+ * @throws `NoeticErrorImpl` `steering_denied` when a gate refuses, or `connection_failed`, `connection_unavailable`, `connection_ref_invalid`, `connection_secret_refused`, `connection_input_required`, `connection_resume_refused`.
44
+ */
45
+ export async function executeCall(step, input, ctx, options = {}) {
46
+ const baseCtx = frameworkCast(ctx);
47
+ const target = await evaluateTarget(step.connection, input, ctx, step.id);
48
+ const args = await resolveArgs(step, input, ctx);
49
+ const connection = await resolveStepConnection({
50
+ ctx: baseCtx,
51
+ stepId: step.id,
52
+ target,
53
+ use: {
54
+ op: step.op,
55
+ },
56
+ ledgerKey: options.ledgerKey,
57
+ });
58
+ refuseWitnessAct(baseCtx, connection.resolved, step.op);
59
+ const signal = signalOf(baseCtx);
60
+ const prior = await reconcilePending(connection, signal, baseCtx);
61
+ const key = prior?.key ?? connection.idempotencyKey;
62
+ const entry = baseEntry({
63
+ connection,
64
+ stepId: step.id,
65
+ key,
66
+ prior,
67
+ });
68
+ await connection.pending?.write(entry);
69
+ const document = frameworkCast(step);
70
+ const callOptions = {
71
+ signal,
72
+ caller: document._caller ?? 'step',
73
+ idempotencyKey: key,
74
+ stepId: step.id,
75
+ declaredOps: document._declaredOps ?? [
76
+ step.op,
77
+ ],
78
+ ...(document._documentSecrets
79
+ ? {
80
+ documentSecrets: document._documentSecrets,
81
+ }
82
+ : {}),
83
+ ctx: baseCtx,
84
+ // The registry emits `connection_task` to this run's stream (the call carries ctx).
85
+ onTask: (status) => {
86
+ if (entry.taskId !== status.taskId) {
87
+ entry.taskId = status.taskId;
88
+ void connection.pending?.write(entry);
89
+ }
90
+ },
91
+ };
92
+ // A previous attempt's long operation is waited on, not started again (spec 35 § Durability).
93
+ const output = prior?.taskId && connection.resolved.awaitTask
94
+ ? await connection.resolved.awaitTask(prior.taskId, step.op, callOptions)
95
+ : await connection.resolved.call(step.op, args, callOptions);
96
+ await connection.pending?.clear();
97
+ return frameworkCast(output);
98
+ }
99
+ //#endregion
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Control-flow step handlers: conditional, inParallel, loop, schedule.
3
3
  */
4
- import type { Context, ExecuteStepFn, StepConditional, StepInParallel, StepLoop, StepSchedule } from '@noetic-tools/types';
4
+ import type { Context, ExecuteStepFn, StepConditional, StepInParallel, StepLoop, StepSchedule, StepSequence } from '@noetic-tools/types';
5
5
  import type { ItemSchemaRegistry, LayerStateStore } from './action-deps.js';
6
6
  export declare function executeConditional<TContext, I, O>(step: StepConditional<TContext, I, O>, input: I, ctx: Context<TContext>, executeStep: ExecuteStepFn): Promise<O>;
7
7
  /** Layer-boundary wiring supplied by the interpreter (see `buildSpawnOpts`). */
@@ -11,6 +11,11 @@ export interface ExecuteForkOpts {
11
11
  }
12
12
  export declare function executeInParallel<TContext, I, O>(step: StepInParallel<TContext, I, O>, input: I, ctx: Context<TContext>, rawExecuteStep: ExecuteStepFn, opts?: ExecuteForkOpts): Promise<O>;
13
13
  export declare function executeLoop<TContext, I, O>(step: StepLoop<TContext, I, O>, input: I, ctx: Context<TContext>, executeStep: ExecuteStepFn): Promise<O>;
14
+ /**
15
+ * Runs a `sequence` step: each child through the interpreter, in order, each
16
+ * output the next child's input. The last child's output is the step's output.
17
+ */
18
+ export declare function executeSequence<TContext, I, O>(step: StepSequence<TContext, I, O>, input: I, ctx: Context<TContext>, executeStep: ExecuteStepFn): Promise<O>;
14
19
  /**
15
20
  * Executes a `schedule` step: runs the body step forever, paced by `ms ± jitter`,
16
21
  * woken early by `inbox`. Throws `cancelled` when the context is aborted.
@@ -9,6 +9,7 @@ import { toUntil } from '../until/decider.js';
9
9
  import { contextToExecCtx, returnLayers, spawnLayers } from './action-deps.js';
10
10
  import { closeUnstartedSpan, openChildSpan, traceChild } from './child-span.js';
11
11
  import { cloneWithGuard } from './clone-guard.js';
12
+ import { releasePathLeases } from './connection-steps.js';
12
13
  import { forgetAnswers } from './execute-decide.js';
13
14
  import { getContextChannelStore, isContextImpl, isMutableContext } from './typeguards.js';
14
15
  //#region conditional
@@ -219,6 +220,8 @@ export async function executeInParallel(step, input, ctx, rawExecuteStep, opts)
219
220
  child.detachFromParent();
220
221
  }
221
222
  await traced.closeUnstarted();
223
+ // Each path's `fresh: 'path'` connections go back to their pools as the path's scope ends.
224
+ await releasePathLeases(childContexts);
222
225
  }
223
226
  }
224
227
  async function runWithConcurrency(tasks, concurrency) {
@@ -733,6 +736,17 @@ function throwCancelled(reason) {
733
736
  reason: reason ?? 'context aborted',
734
737
  });
735
738
  }
739
+ /**
740
+ * Runs a `sequence` step: each child through the interpreter, in order, each
741
+ * output the next child's input. The last child's output is the step's output.
742
+ */
743
+ export async function executeSequence(step, input, ctx, executeStep) {
744
+ let current = input;
745
+ for (const child of step.steps) {
746
+ current = await executeStep(frameworkCast(child), current, ctx);
747
+ }
748
+ return frameworkCast(current);
749
+ }
736
750
  /**
737
751
  * Executes a `schedule` step: runs the body step forever, paced by `ms ± jitter`,
738
752
  * woken early by `inbox`. Throws `cancelled` when the context is aborted.
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The `invokeTool` step handler: validate the arguments, pass the steering and
3
+ * approval gates, run the tool (streaming generator progress), and trace it.
4
+ */
5
+ import type { AgentHarnessContract, Context, ContextLayer, StepInvokeTool } from './action-types.js';
6
+ /**
7
+ * Runs an `invokeTool` step inside a `tool.call` span under `ctx.span`. The span
8
+ * covers validation, the steering gate, and execution, so a denied or failing
9
+ * call is traced with its error.
10
+ */
11
+ export declare function executeInvokeTool<TContext, I, O>(step: StepInvokeTool<TContext, I, O>, input: I, ctx: Context<TContext>, harness: AgentHarnessContract, layers?: ContextLayer[]): Promise<O>;
@@ -0,0 +1,183 @@
1
+ /**
2
+ * The `invokeTool` step handler: validate the arguments, pass the steering and
3
+ * approval gates, run the tool (streaming generator progress), and trace it.
4
+ */
5
+ import { frameworkCast, NoeticErrorImpl, validateSchema } from '@noetic-tools/types';
6
+ import { ToolAttr } from '../observability/genai-attributes.js';
7
+ import { askApproval } from '../runtime/tool-approval.js';
8
+ import { emitToolUi } from '../runtime/tool-ui.js';
9
+ import { buildToolExecutionContext, emitFrameworkEvent, getBroadcaster } from './action-deps.js';
10
+ import { SteeringAction } from './action-types.js';
11
+ import { openChildSpan, traceChild } from './child-span.js';
12
+ //#region tool
13
+ function isAsyncGenerator(value) {
14
+ if (typeof value !== 'object' || value === null) {
15
+ return false;
16
+ }
17
+ return Symbol.asyncIterator in value;
18
+ }
19
+ async function consumeToolGenerator(params) {
20
+ const broadcaster = getBroadcaster(params.ctx);
21
+ const agentName = params.ctx.harness.config.name;
22
+ const events = [];
23
+ while (true) {
24
+ const next = await params.generator.next();
25
+ if (next.done) {
26
+ return next.value;
27
+ }
28
+ events.push(next.value);
29
+ emitFrameworkEvent({
30
+ broadcaster,
31
+ agentName,
32
+ eventType: 'tool_progress',
33
+ data: {
34
+ stepId: params.stepId,
35
+ toolName: params.tool.name,
36
+ event: next.value,
37
+ },
38
+ });
39
+ // A direct `invokeTool` has no model call id — the step id keys its region.
40
+ emitToolUi({
41
+ ctx: params.ctx,
42
+ tool: params.tool,
43
+ callId: params.stepId,
44
+ phase: 'progress',
45
+ args: params.args,
46
+ events,
47
+ });
48
+ }
49
+ }
50
+ /**
51
+ * Runs an `invokeTool` step inside a `tool.call` span under `ctx.span`. The span
52
+ * covers validation, the steering gate, and execution, so a denied or failing
53
+ * call is traced with its error.
54
+ */
55
+ export async function executeInvokeTool(step, input, ctx, harness, layers) {
56
+ const baseCtx = frameworkCast(ctx);
57
+ const span = openChildSpan(baseCtx, 'tool.call', step.id);
58
+ span.setAttribute(ToolAttr.NAME, step.tool.name);
59
+ span.setAttribute(ToolAttr.NEEDS_APPROVAL, step.tool.needsApproval ?? false);
60
+ return traceChild(baseCtx, span, () => runInvokeTool(step, input, baseCtx, harness, layers, span));
61
+ }
62
+ /**
63
+ * Gates run in order — input validation, the `beforeToolCall` layers, then the
64
+ * harness approver for a tool that declares `needsApproval` — and a refusal at
65
+ * any gate throws `steering_denied` before the tool runs.
66
+ */
67
+ async function runInvokeTool(step, input, baseCtx, harness, layers, span) {
68
+ const args = step.args ? Object.assign({}, input, step.args) : input;
69
+ const parseResult = await validateSchema(step.tool.input, args);
70
+ if (!parseResult.success) {
71
+ throw new NoeticErrorImpl({
72
+ kind: 'step_failed',
73
+ stepId: step.id,
74
+ cause: new Error(`Tool input validation failed: ${parseResult.zodError.message}`),
75
+ retriesExhausted: false,
76
+ });
77
+ }
78
+ const parsedArgs = parseResult.value;
79
+ // The started event precedes the gate so a parked call (e.g. one awaiting an
80
+ // external permission decision) is visible as pending rather than invisible
81
+ // until approved — the same ordering the model-driven path guarantees.
82
+ const broadcaster = getBroadcaster(baseCtx);
83
+ const agentName = baseCtx.harness.config.name;
84
+ emitFrameworkEvent({
85
+ broadcaster,
86
+ agentName,
87
+ eventType: 'tool_call_started',
88
+ data: {
89
+ name: step.tool.name,
90
+ callId: step.id,
91
+ },
92
+ });
93
+ const emitCompleted = (error, output) => {
94
+ emitFrameworkEvent({
95
+ broadcaster,
96
+ agentName,
97
+ eventType: 'tool_call_completed',
98
+ data: {
99
+ name: step.tool.name,
100
+ callId: step.id,
101
+ error,
102
+ output,
103
+ },
104
+ });
105
+ };
106
+ const refuse = (decision) => {
107
+ emitCompleted(true, `Tool call denied: ${decision.guidance ?? 'steering rule violation'}`);
108
+ throw new NoeticErrorImpl({
109
+ kind: 'steering_denied',
110
+ guidance: decision.guidance,
111
+ });
112
+ };
113
+ if (layers && layers.length > 0) {
114
+ const decision = await harness.beforeToolCall(layers, step.tool.name, parsedArgs, baseCtx, step.id);
115
+ if (decision.action !== SteeringAction.Allow) {
116
+ refuse(decision);
117
+ }
118
+ }
119
+ if (step.tool.needsApproval) {
120
+ const approval = await askApproval({
121
+ harness,
122
+ toolName: step.tool.name,
123
+ toolArgs: parsedArgs,
124
+ callId: step.id,
125
+ ctx: baseCtx,
126
+ });
127
+ span.setAttribute(ToolAttr.APPROVAL, approval.outcome);
128
+ if (approval.decision.action !== SteeringAction.Allow) {
129
+ refuse(approval.decision);
130
+ }
131
+ }
132
+ emitToolUi({
133
+ ctx: baseCtx,
134
+ tool: step.tool,
135
+ callId: step.id,
136
+ phase: 'call',
137
+ args: parsedArgs,
138
+ });
139
+ try {
140
+ const toolCtx = buildToolExecutionContext(baseCtx, harness);
141
+ const execResult = step.tool.execute(parsedArgs, toolCtx);
142
+ const result = isAsyncGenerator(execResult)
143
+ ? await consumeToolGenerator({
144
+ generator: execResult,
145
+ stepId: step.id,
146
+ tool: step.tool,
147
+ ctx: baseCtx,
148
+ args: parsedArgs,
149
+ })
150
+ : await execResult;
151
+ emitToolUi({
152
+ ctx: baseCtx,
153
+ tool: step.tool,
154
+ callId: step.id,
155
+ phase: 'result',
156
+ args: parsedArgs,
157
+ output: result,
158
+ });
159
+ emitCompleted(false, typeof result === 'string' ? result : JSON.stringify(result));
160
+ return frameworkCast(result);
161
+ }
162
+ catch (e) {
163
+ emitToolUi({
164
+ ctx: baseCtx,
165
+ tool: step.tool,
166
+ callId: step.id,
167
+ phase: 'error',
168
+ args: parsedArgs,
169
+ error: e,
170
+ });
171
+ emitCompleted(true, e instanceof Error ? e.message : String(e));
172
+ if (e instanceof NoeticErrorImpl) {
173
+ throw e;
174
+ }
175
+ throw new NoeticErrorImpl({
176
+ kind: 'step_failed',
177
+ stepId: step.id,
178
+ cause: e instanceof Error ? e : new Error(String(e)),
179
+ retriesExhausted: false,
180
+ });
181
+ }
182
+ }
183
+ //#endregion
@@ -0,0 +1,21 @@
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 type { Context, ExecuteStepFn, StepVerify } from '@noetic-tools/types';
10
+ /**
11
+ * Executes a `verify` step. With a list subject, runs one verify per connection
12
+ * in parallel (ids `<id>.<name>`) and returns each output with the worst outcome.
13
+ *
14
+ * @throws `NoeticConfigError` `VERIFY_NOT_CONFIGURED` when the harness has no verifier.
15
+ * @throws `NoeticConfigError` `VERIFY_INVALID` when `act` contains nothing that acts, a
16
+ * matcher cannot be parsed, or a witness is asked to act.
17
+ * @throws `NoeticConfigError` `CONNECTION_UNKNOWN`, `CONNECTION_UNSATISFIED` (a sense or
18
+ * event no participant offers), `CONNECTION_NOT_ALLOWED`, `CONNECTION_INVALID` from resolution.
19
+ * @throws `NoeticErrorImpl` `verify_act_failed` when `act` threw; the run is recorded first.
20
+ */
21
+ export declare function executeVerify<TContext, I, O>(step: StepVerify<TContext, I, O>, input: I, ctx: Context<TContext>, executeStep: ExecuteStepFn): Promise<O>;