@noetic-tools/core 5.21.0 → 5.22.0-next.2

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 (101) hide show
  1. package/dist/adapters/openrouter.d.ts +6 -1
  2. package/dist/adapters/openrouter.js +10 -0
  3. package/dist/builders/decide-builders.d.ts +7 -0
  4. package/dist/builders/tool-builder.d.ts +25 -1
  5. package/dist/builders/tool-builder.js +25 -1
  6. package/dist/builders/workflow-control-nodes.js +1 -0
  7. package/dist/builders/workflow-hydrator.d.ts +19 -0
  8. package/dist/builders/workflow-hydrator.js +19 -0
  9. package/dist/builders/workflow-step.js +7 -1
  10. package/dist/connections/cron-schedule.js +12 -3
  11. package/dist/connections/durability.d.ts +1 -2
  12. package/dist/connections/durability.js +1 -4
  13. package/dist/connections/gates.js +6 -2
  14. package/dist/connections/harness-wiring.d.ts +5 -0
  15. package/dist/connections/harness-wiring.js +76 -10
  16. package/dist/connections/in-memory-connection.d.ts +4 -0
  17. package/dist/connections/in-memory-connection.js +10 -0
  18. package/dist/connections/model-tools.js +5 -1
  19. package/dist/connections/registry.d.ts +6 -2
  20. package/dist/connections/registry.js +10 -0
  21. package/dist/connections/sources.js +261 -20
  22. package/dist/connections/triggers.d.ts +62 -2
  23. package/dist/connections/triggers.js +375 -35
  24. package/dist/harness/agent-harness.d.ts +317 -4
  25. package/dist/harness/agent-harness.js +1264 -96
  26. package/dist/harness/connection-gate.d.ts +3 -0
  27. package/dist/harness/connection-gate.js +4 -0
  28. package/dist/harness/decide-call.d.ts +15 -0
  29. package/dist/harness/decide-call.js +57 -7
  30. package/dist/harness/deps/runtime.d.ts +3 -0
  31. package/dist/harness/deps/runtime.js +2 -0
  32. package/dist/harness/flow-version.d.ts +13 -0
  33. package/dist/harness/flow-version.js +66 -0
  34. package/dist/harness/harness-options.d.ts +91 -0
  35. package/dist/harness/llm-client.d.ts +2 -0
  36. package/dist/harness/llm-client.js +9 -5
  37. package/dist/harness/model-call.js +8 -0
  38. package/dist/harness/model-rounds.d.ts +30 -0
  39. package/dist/harness/model-rounds.js +146 -1
  40. package/dist/harness/model-tool-round.d.ts +3 -0
  41. package/dist/harness/model-tool-round.js +249 -22
  42. package/dist/harness/park-watchers.d.ts +93 -0
  43. package/dist/harness/park-watchers.js +217 -0
  44. package/dist/harness/read-decision.d.ts +23 -0
  45. package/dist/harness/read-decision.js +100 -0
  46. package/dist/harness/spend.js +4 -0
  47. package/dist/harness/steer-inbox.js +3 -1
  48. package/dist/harness/thread-record.d.ts +77 -1
  49. package/dist/harness/thread-record.js +272 -13
  50. package/dist/index.d.ts +22 -8
  51. package/dist/index.js +12 -2
  52. package/dist/interpreter/acp-resume.d.ts +77 -0
  53. package/dist/interpreter/acp-resume.js +164 -0
  54. package/dist/interpreter/acp-tools.d.ts +11 -0
  55. package/dist/interpreter/acp-tools.js +94 -7
  56. package/dist/interpreter/connection-steps.d.ts +24 -1
  57. package/dist/interpreter/connection-steps.js +77 -2
  58. package/dist/interpreter/execute-acp-agent.js +111 -15
  59. package/dist/interpreter/execute-action.js +7 -0
  60. package/dist/interpreter/execute-call.js +30 -4
  61. package/dist/interpreter/execute-control.js +21 -0
  62. package/dist/interpreter/execute-decide.js +5 -0
  63. package/dist/interpreter/execute-invoke-tool.js +149 -5
  64. package/dist/interpreter/execute-wait.js +67 -9
  65. package/dist/interpreter/execute.js +4 -0
  66. package/dist/interpreter/turn-ending.d.ts +15 -0
  67. package/dist/interpreter/turn-ending.js +46 -0
  68. package/dist/portable/builders.d.ts +1 -1
  69. package/dist/portable/builders.js +1 -1
  70. package/dist/portable/runtime.d.ts +4 -0
  71. package/dist/portable/runtime.js +2 -0
  72. package/dist/runtime/durable/parking.d.ts +61 -0
  73. package/dist/runtime/durable/parking.js +137 -0
  74. package/dist/runtime/durable/session-store.d.ts +38 -0
  75. package/dist/runtime/durable/session-store.js +79 -1
  76. package/dist/runtime/durable/step-ledger.d.ts +121 -2
  77. package/dist/runtime/durable/step-ledger.js +352 -15
  78. package/dist/runtime/durable/turn-lease.d.ts +128 -0
  79. package/dist/runtime/durable/turn-lease.js +307 -0
  80. package/dist/runtime/event-broadcaster.d.ts +9 -1
  81. package/dist/runtime/event-broadcaster.js +16 -1
  82. package/dist/runtime/held-calls.d.ts +192 -0
  83. package/dist/runtime/held-calls.js +496 -0
  84. package/dist/runtime/in-memory-storage.d.ts +10 -1
  85. package/dist/runtime/in-memory-storage.js +20 -2
  86. package/dist/runtime/message-queue.d.ts +2 -0
  87. package/dist/runtime/message-queue.js +8 -0
  88. package/dist/runtime/output-store.d.ts +88 -0
  89. package/dist/runtime/output-store.js +241 -0
  90. package/dist/runtime/session-runner.d.ts +18 -2
  91. package/dist/runtime/session-runner.js +88 -3
  92. package/dist/runtime/session-streams.d.ts +28 -7
  93. package/dist/runtime/session-streams.js +112 -67
  94. package/dist/runtime/tool-approval.d.ts +46 -6
  95. package/dist/runtime/tool-approval.js +160 -38
  96. package/dist/runtime/tool-call.d.ts +34 -2
  97. package/dist/runtime/tool-call.js +182 -13
  98. package/dist/runtime/tool-context.d.ts +15 -1
  99. package/dist/runtime/tool-context.js +38 -2
  100. package/package.json +3 -3
  101. package/schema/noetic-workflow.schema.json +27 -0
@@ -6,6 +6,7 @@
6
6
  import { frameworkCast, isStandardJsonSchema, isZodSchema, NoeticConfigError, SteeringAction, validateSchema, } from '@noetic-tools/types';
7
7
  import { z } from 'zod';
8
8
  import { sanitizeToolNameForWire } from '../util/tool-name.js';
9
+ import { HoldSignal, heldArgsHash, heldCallsOf, heldScopeOf, isOverdue, mayWriteHolds, toHeldCall, } from './held-calls.js';
9
10
  import { askApproval } from './tool-approval.js';
10
11
  import { buildToolExecutionContext } from './tool-context.js';
11
12
  import { emitToolUi } from './tool-ui.js';
@@ -59,6 +60,10 @@ function sanitizeJsonSchemaValue(value) {
59
60
  }
60
61
  return value;
61
62
  }
63
+ /** The id a call's durable records go under. */
64
+ function recordIdOf(params) {
65
+ return params.recordId ?? params.callId;
66
+ }
62
67
  function isAsyncGenerator(value) {
63
68
  if (typeof value !== 'object' || value === null) {
64
69
  return false;
@@ -84,7 +89,12 @@ export async function executeToolCall(params) {
84
89
  error: true,
85
90
  };
86
91
  }
87
- if (params.layers && params.layers.length > 0) {
92
+ // A call that was held got past these layers before it was held.
93
+ const holds = heldCallsOf(params.harness);
94
+ const scope = heldScopeOf(params.context);
95
+ const recordId = recordIdOf(params);
96
+ const prior = recordId === undefined ? undefined : await holds.load(scope, recordId);
97
+ if (!prior && params.layers && params.layers.length > 0) {
88
98
  const decision = await params.harness.beforeToolCall(params.layers, params.toolName, params.args, params.context, params.callId);
89
99
  if (decision.action !== SteeringAction.Allow) {
90
100
  return refusedResult(decision);
@@ -98,27 +108,148 @@ export async function executeToolCall(params) {
98
108
  };
99
109
  }
100
110
  const parsedArgs = validated.value;
101
- const approval = matchedTool.needsApproval
102
- ? await askApproval({
103
- harness: params.harness,
104
- toolName: matchedTool.name,
105
- toolArgs: parsedArgs,
106
- callId: params.callId,
107
- ctx: params.context,
108
- })
109
- : undefined;
111
+ // A record for other arguments is for another call: this one starts over.
112
+ const held = prior?.argsHash === heldArgsHash(matchedTool.name, parsedArgs) ? prior : undefined;
113
+ const wasHeld = held !== undefined;
114
+ // A person may answer an unknown outcome with "run it again".
115
+ const rerun = held?.reason === 'outcome' && held.resolution?.action === SteeringAction.Allow;
116
+ if (held && held.reason !== 'approval' && !rerun) {
117
+ if (held.resolution?.hasOutput) {
118
+ return {
119
+ output: toolOutputText(held.resolution.output),
120
+ result: held.resolution.output,
121
+ settled: true,
122
+ };
123
+ }
124
+ const refusal = held.resolution?.action;
125
+ if (refusal !== undefined) {
126
+ return {
127
+ ...refusedResult({
128
+ action: refusal,
129
+ guidance: held.resolution?.guidance,
130
+ }),
131
+ settled: true,
132
+ };
133
+ }
134
+ if (isOverdue(held, holds.now())) {
135
+ return {
136
+ output: `Error: nobody answered '${matchedTool.name}' before the hold ran out`,
137
+ error: true,
138
+ settled: true,
139
+ };
140
+ }
141
+ return {
142
+ output: '',
143
+ held: toHeldCall(held),
144
+ };
145
+ }
146
+ let approval;
147
+ try {
148
+ // A call a person just said to run again was approved the first time it ran.
149
+ approval =
150
+ matchedTool.needsApproval && !rerun
151
+ ? await askApproval({
152
+ harness: params.harness,
153
+ toolName: matchedTool.name,
154
+ toolArgs: parsedArgs,
155
+ callId: params.callId,
156
+ recordId,
157
+ ctx: params.context,
158
+ })
159
+ : undefined;
160
+ }
161
+ catch (e) {
162
+ if (!(e instanceof HoldSignal) || recordId === undefined) {
163
+ throw e;
164
+ }
165
+ return holdCall(params, {
166
+ callId: recordId,
167
+ tool: matchedTool,
168
+ args: parsedArgs,
169
+ reason: 'approval',
170
+ due: e.due,
171
+ });
172
+ }
110
173
  if (approval && approval.decision.action !== SteeringAction.Allow) {
111
174
  return {
112
175
  ...refusedResult(approval.decision),
113
176
  approval: approval.outcome,
177
+ settled: wasHeld,
114
178
  };
115
179
  }
180
+ // A durable tool must not be repeated unasked. An intent record says it started;
181
+ // finding one with no result means a host died while it ran, and only a person
182
+ // can say whether its effect landed.
183
+ const armed = matchedTool.durable === true && recordId !== undefined;
184
+ if (armed && recordId !== undefined) {
185
+ const argsHash = heldArgsHash(matchedTool.name, parsedArgs);
186
+ if (!rerun && (await holds.wasStarted(scope, recordId, argsHash))) {
187
+ return holdCall(params, {
188
+ callId: recordId,
189
+ tool: matchedTool,
190
+ args: parsedArgs,
191
+ reason: 'outcome',
192
+ due: {},
193
+ });
194
+ }
195
+ if (mayWriteHolds(params.context)) {
196
+ if (rerun) {
197
+ // Answered: if this attempt is cut off too, the question is asked afresh.
198
+ await holds.close(scope, recordId);
199
+ }
200
+ await holds.arm(scope, recordId, argsHash);
201
+ }
202
+ }
116
203
  const executed = await runMatchedTool(params, matchedTool, parsedArgs);
117
204
  return {
118
205
  ...executed,
119
206
  approval: approval?.outcome,
207
+ settled: wasHeld && !rerun && executed.held === undefined,
208
+ armed,
120
209
  };
121
210
  }
211
+ /** Write the record of a call that is now held, and report it as held. */
212
+ async function holdCall(params, call) {
213
+ // A harness that was replaced writes nothing; the call still reads as held, and
214
+ // this turn is about to stop.
215
+ const holds = heldCallsOf(params.harness);
216
+ const open = mayWriteHolds(params.context)
217
+ ? (record) => holds.open(heldScopeOf(params.context), record)
218
+ : async (record) => record;
219
+ const record = await open({
220
+ callId: call.callId,
221
+ name: call.tool.name,
222
+ args: call.args,
223
+ argsHash: heldArgsHash(call.tool.name, call.args),
224
+ reason: call.reason,
225
+ ...(call.due.dueAt === undefined
226
+ ? {}
227
+ : {
228
+ dueAt: call.due.dueAt,
229
+ }),
230
+ ...(call.due.onDue === undefined
231
+ ? {}
232
+ : {
233
+ onDue: call.due.onDue,
234
+ }),
235
+ ...(call.request === undefined
236
+ ? {}
237
+ : {
238
+ request: call.request,
239
+ }),
240
+ });
241
+ return {
242
+ output: '',
243
+ held: toHeldCall(record),
244
+ };
245
+ }
246
+ /** True for what `hold()` returns: a tool leaving its result open. */
247
+ export function isToolHold(value) {
248
+ return (typeof value === 'object' &&
249
+ value !== null &&
250
+ 'kind' in value &&
251
+ value.kind === 'noetic/tool-hold');
252
+ }
122
253
  function refusedResult(decision) {
123
254
  if (decision.action === SteeringAction.Guide) {
124
255
  return {
@@ -132,8 +263,14 @@ function refusedResult(decision) {
132
263
  };
133
264
  }
134
265
  async function runMatchedTool(params, matchedTool, parsedArgs) {
135
- const toolCtx = buildToolExecutionContext(params.context, params.harness, params.callId);
266
+ const toolCtx = buildToolExecutionContext(params.context, params.harness, {
267
+ callId: params.callId,
268
+ recordId: recordIdOf(params),
269
+ keyScope: params.keyScope,
270
+ });
136
271
  const callId = params.callId;
272
+ // Where the call's held record goes: the id that survives a resume.
273
+ const holdId = recordIdOf(params);
137
274
  const uiBase = callId !== undefined
138
275
  ? {
139
276
  ctx: params.context,
@@ -174,6 +311,26 @@ async function runMatchedTool(params, matchedTool, parsedArgs) {
174
311
  else {
175
312
  result = await executionResult;
176
313
  }
314
+ // The tool did its part and left the result for a person to give.
315
+ if (isToolHold(result)) {
316
+ if (holdId === undefined) {
317
+ return {
318
+ output: `Error: '${matchedTool.name}' cannot be held: the call has no id`,
319
+ error: true,
320
+ };
321
+ }
322
+ return holdCall(params, {
323
+ callId: holdId,
324
+ tool: matchedTool,
325
+ args: parsedArgs,
326
+ reason: 'result',
327
+ due: {
328
+ dueAt: result.dueAt,
329
+ onDue: result.onDue,
330
+ },
331
+ request: result.request,
332
+ });
333
+ }
177
334
  if (uiBase) {
178
335
  emitToolUi({
179
336
  ...uiBase,
@@ -184,9 +341,20 @@ async function runMatchedTool(params, matchedTool, parsedArgs) {
184
341
  return {
185
342
  output: toolOutputText(result),
186
343
  result,
344
+ ran: true,
187
345
  };
188
346
  }
189
347
  catch (e) {
348
+ // An approval seam inside the tool (a connection's gates) held the call.
349
+ if (e instanceof HoldSignal && holdId !== undefined) {
350
+ return holdCall(params, {
351
+ callId: holdId,
352
+ tool: matchedTool,
353
+ args: parsedArgs,
354
+ reason: 'approval',
355
+ due: e.due,
356
+ });
357
+ }
190
358
  if (uiBase) {
191
359
  emitToolUi({
192
360
  ...uiBase,
@@ -197,6 +365,7 @@ async function runMatchedTool(params, matchedTool, parsedArgs) {
197
365
  return {
198
366
  output: `Error: ${e instanceof Error ? e.message : String(e)}`,
199
367
  error: true,
368
+ ran: true,
200
369
  };
201
370
  }
202
371
  }
@@ -221,9 +390,9 @@ function toolOutputText(result) {
221
390
  * `toolResults` schemas never reject a sibling tool's result items. Tools
222
391
  * without `itemSchemas` fall back to the base structural parse.
223
392
  */
224
- export function createToolResultItem({ output, callId, roundItemSchemas, tool, callItem, args, result, error, }) {
393
+ export function createToolResultItem({ id, output, callId, roundItemSchemas, tool, callItem, args, result, error, }) {
225
394
  const baseItem = {
226
- id: crypto.randomUUID(),
395
+ id: id ?? crypto.randomUUID(),
227
396
  status: 'completed',
228
397
  type: 'function_call_output',
229
398
  callId,
@@ -1,2 +1,16 @@
1
1
  import type { AgentHarnessContract, Context, ToolExecutionContext } from '@noetic-tools/types';
2
- export declare function buildToolExecutionContext(ctx: Context, harness: AgentHarnessContract, callId?: string): ToolExecutionContext;
2
+ /**
3
+ * The idempotency key of a tool call: the turn's root execution and the call's id.
4
+ * With `scope`, where in the run the call was made, since a call id is only as
5
+ * unique as the provider that made it up.
6
+ */
7
+ export declare function toolIdempotencyKey(executionId: string, toolCallId: string, scope?: string): string;
8
+ /** @internal The scope `toolCtx`'s idempotency key was made with, if it had one. */
9
+ export declare function keyScopeOf(toolCtx: ToolExecutionContext): string | undefined;
10
+ export declare function buildToolExecutionContext(ctx: Context, harness: AgentHarnessContract, call?: string | {
11
+ callId?: string;
12
+ /** The id the idempotency key is made from, when it is not `callId`. */
13
+ recordId?: string;
14
+ /** Where in the run the call was made (see `toolIdempotencyKey`). */
15
+ keyScope?: string;
16
+ }): ToolExecutionContext;
@@ -1,3 +1,10 @@
1
+ function rootIdOf(ctx) {
2
+ let node = ctx;
3
+ while (node.parent) {
4
+ node = node.parent;
5
+ }
6
+ return node.id;
7
+ }
1
8
  function buildToolContext(harness, ctx) {
2
9
  return {
3
10
  get(layerId) {
@@ -8,9 +15,28 @@ function buildToolContext(harness, ctx) {
8
15
  },
9
16
  };
10
17
  }
11
- export function buildToolExecutionContext(ctx, harness, callId) {
18
+ /**
19
+ * The idempotency key of a tool call: the turn's root execution and the call's id.
20
+ * With `scope`, where in the run the call was made, since a call id is only as
21
+ * unique as the provider that made it up.
22
+ */
23
+ export function toolIdempotencyKey(executionId, toolCallId, scope) {
24
+ return scope === undefined
25
+ ? `${executionId}:tool:${toolCallId}`
26
+ : `${executionId}:tool:${scope}:${toolCallId}`;
27
+ }
28
+ /** Where in the run each tool context's call was made, for those built knowing it. */
29
+ const keyScopes = new WeakMap();
30
+ /** @internal The scope `toolCtx`'s idempotency key was made with, if it had one. */
31
+ export function keyScopeOf(toolCtx) {
32
+ return keyScopes.get(toolCtx);
33
+ }
34
+ export function buildToolExecutionContext(ctx, harness, call) {
35
+ const callId = typeof call === 'string' ? call : call?.callId;
36
+ const recordId = typeof call === 'string' ? call : (call?.recordId ?? call?.callId);
37
+ const keyScope = typeof call === 'string' ? undefined : call?.keyScope;
12
38
  const toolContext = buildToolContext(harness, ctx);
13
- return {
39
+ const built = {
14
40
  ctx,
15
41
  harness,
16
42
  fs: harness.fs,
@@ -23,5 +49,15 @@ export function buildToolExecutionContext(ctx, harness, callId) {
23
49
  : {
24
50
  callId,
25
51
  }),
52
+ ...(recordId === undefined
53
+ ? {}
54
+ : {
55
+ // The root execution's id: a child context gets a new id on every resume.
56
+ idempotencyKey: toolIdempotencyKey(rootIdOf(ctx), recordId, keyScope),
57
+ }),
26
58
  };
59
+ if (keyScope !== undefined) {
60
+ keyScopes.set(built, keyScope);
61
+ }
62
+ return built;
27
63
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/core",
3
- "version": "5.21.0",
3
+ "version": "5.22.0-next.2",
4
4
  "type": "module",
5
5
  "description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
6
6
  "license": "Apache-2.0",
@@ -68,8 +68,8 @@
68
68
  }
69
69
  },
70
70
  "dependencies": {
71
- "@noetic-tools/context": "^1.11.1",
72
- "@noetic-tools/types": "^3.18.0",
71
+ "@noetic-tools/context": "^2.0.0-next.1",
72
+ "@noetic-tools/types": "^3.19.0-next.1",
73
73
  "@openrouter/agent": "^0.11.0",
74
74
  "@openrouter/sdk": "1.4.25",
75
75
  "jsonata": "2.2.2",
@@ -1373,6 +1373,15 @@
1373
1373
  "type": "integer",
1374
1374
  "exclusiveMinimum": 0,
1375
1375
  "maximum": 9007199254740991
1376
+ },
1377
+ "headers": {
1378
+ "type": "object",
1379
+ "propertyNames": {
1380
+ "type": "string"
1381
+ },
1382
+ "additionalProperties": {
1383
+ "type": "string"
1384
+ }
1376
1385
  }
1377
1386
  },
1378
1387
  "required": [
@@ -1738,6 +1747,15 @@
1738
1747
  "type": "integer",
1739
1748
  "exclusiveMinimum": 0,
1740
1749
  "maximum": 9007199254740991
1750
+ },
1751
+ "headers": {
1752
+ "type": "object",
1753
+ "propertyNames": {
1754
+ "type": "string"
1755
+ },
1756
+ "additionalProperties": {
1757
+ "type": "string"
1758
+ }
1741
1759
  }
1742
1760
  },
1743
1761
  "required": [
@@ -4463,6 +4481,15 @@
4463
4481
  "type": "integer",
4464
4482
  "exclusiveMinimum": 0,
4465
4483
  "maximum": 9007199254740991
4484
+ },
4485
+ "headers": {
4486
+ "type": "object",
4487
+ "propertyNames": {
4488
+ "type": "string"
4489
+ },
4490
+ "additionalProperties": {
4491
+ "type": "string"
4492
+ }
4466
4493
  }
4467
4494
  },
4468
4495
  "required": [