@noetic-tools/core 5.19.3 → 5.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/dist/adapters/openrouter.d.ts +1 -48
  2. package/dist/adapters/openrouter.js +3 -204
  3. package/dist/builders/acp-agent-checks.d.ts +20 -0
  4. package/dist/builders/acp-agent-checks.js +68 -0
  5. package/dist/builders/acp-agent-tool.d.ts +4 -3
  6. package/dist/builders/acp-agent-tool.js +5 -2
  7. package/dist/builders/connection-ceiling.d.ts +13 -0
  8. package/dist/builders/connection-ceiling.js +17 -0
  9. package/dist/builders/document-entries.d.ts +13 -0
  10. package/dist/builders/document-entries.js +18 -0
  11. package/dist/builders/dynamic-workflow.d.ts +20 -12
  12. package/dist/builders/dynamic-workflow.js +28 -24
  13. package/dist/builders/step-builders.d.ts +14 -6
  14. package/dist/builders/step-builders.js +25 -2
  15. package/dist/builders/workflow-child-nodes.js +7 -0
  16. package/dist/builders/workflow-connection-nodes.d.ts +3 -1
  17. package/dist/builders/workflow-connection-nodes.js +94 -6
  18. package/dist/builders/workflow-connections.js +2 -1
  19. package/dist/builders/workflow-hydrator.d.ts +12 -1
  20. package/dist/builders/workflow-hydrator.js +7 -141
  21. package/dist/builders/workflow-mcp-servers.d.ts +34 -14
  22. package/dist/builders/workflow-mcp-servers.js +211 -104
  23. package/dist/builders/workflow-tool-entries.d.ts +84 -0
  24. package/dist/builders/workflow-tool-entries.js +216 -0
  25. package/dist/connections/durability.d.ts +11 -1
  26. package/dist/connections/durability.js +34 -0
  27. package/dist/connections/entries.d.ts +33 -0
  28. package/dist/connections/entries.js +118 -0
  29. package/dist/connections/errors.d.ts +6 -0
  30. package/dist/connections/errors.js +12 -0
  31. package/dist/connections/harness-wiring.d.ts +4 -4
  32. package/dist/connections/harness-wiring.js +1 -1
  33. package/dist/connections/in-memory-connection.d.ts +2 -0
  34. package/dist/connections/in-memory-connection.js +5 -0
  35. package/dist/connections/model-tools.d.ts +35 -12
  36. package/dist/connections/model-tools.js +348 -202
  37. package/dist/connections/registry-types.d.ts +7 -0
  38. package/dist/connections/registry.d.ts +35 -6
  39. package/dist/connections/registry.js +183 -39
  40. package/dist/connections/step-tools.d.ts +64 -0
  41. package/dist/connections/step-tools.js +154 -0
  42. package/dist/connections/views.d.ts +3 -2
  43. package/dist/connections/views.js +15 -6
  44. package/dist/harness/agent-harness.d.ts +11 -1
  45. package/dist/harness/agent-harness.js +69 -8
  46. package/dist/harness/connection-gate.d.ts +49 -8
  47. package/dist/harness/connection-gate.js +136 -26
  48. package/dist/harness/deps/interpreter.d.ts +3 -3
  49. package/dist/harness/deps/interpreter.js +3 -3
  50. package/dist/harness/harness-options.d.ts +4 -3
  51. package/dist/harness/model-request.js +3 -1
  52. package/dist/harness/model-schema.d.ts +1 -19
  53. package/dist/harness/model-schema.js +0 -28
  54. package/dist/harness/model-tool-round.js +2 -2
  55. package/dist/index.d.ts +5 -5
  56. package/dist/index.js +1 -1
  57. package/dist/interpreter/acp-events.d.ts +25 -1
  58. package/dist/interpreter/acp-events.js +53 -1
  59. package/dist/interpreter/acp-layers.d.ts +20 -0
  60. package/dist/interpreter/acp-layers.js +55 -0
  61. package/dist/interpreter/acp-tools.d.ts +108 -0
  62. package/dist/interpreter/acp-tools.js +462 -0
  63. package/dist/interpreter/collect-tools.d.ts +28 -0
  64. package/dist/interpreter/collect-tools.js +112 -15
  65. package/dist/interpreter/connection-steps.d.ts +0 -2
  66. package/dist/interpreter/connection-steps.js +30 -8
  67. package/dist/interpreter/execute-acp-agent.js +263 -72
  68. package/dist/interpreter/execute-action.d.ts +76 -1
  69. package/dist/interpreter/execute-action.js +35 -42
  70. package/dist/interpreter/execute.d.ts +2 -0
  71. package/dist/interpreter/execute.js +24 -1
  72. package/dist/interpreter/verify-host.js +2 -1
  73. package/dist/runtime/tool-call.d.ts +67 -0
  74. package/dist/runtime/tool-call.js +244 -0
  75. package/dist/runtime/tool-context.d.ts +1 -2
  76. package/dist/runtime/tool-context.js +6 -2
  77. package/dist/schemas/workflow.d.ts +1 -1
  78. package/dist/types/connection-entry.d.ts +22 -0
  79. package/dist/types/connection-entry.js +8 -0
  80. package/dist/util/root-context.d.ts +3 -0
  81. package/dist/util/root-context.js +8 -0
  82. package/dist/util/tool-name.d.ts +8 -0
  83. package/dist/util/tool-name.js +10 -0
  84. package/package.json +2 -2
  85. package/schema/noetic-workflow.schema.json +495 -176
@@ -41,6 +41,13 @@ export interface ResolveRequest {
41
41
  readonly profile?: AnyProfile;
42
42
  /** The context resolving it, so gates and framework events reach its run. */
43
43
  readonly ctx?: Context;
44
+ /**
45
+ * Who resolves it. A step's own entry is reached only by the model tools
46
+ * built from it (`'model'`); handles reach harness and turn connections.
47
+ */
48
+ readonly caller?: ConnectionCaller;
49
+ /** The turn whose attached connections the name may name, when there is no `ctx` to find it from. */
50
+ readonly rootId?: string;
44
51
  }
45
52
  /** Options for one gated call. */
46
53
  export interface GatedCallOptions {
@@ -11,8 +11,14 @@
11
11
  * reconnected with backoff while every other connection keeps working. When a
12
12
  * connection's manifest changes it is renegotiated against what the harness
13
13
  * graph requires; one that no longer satisfies it becomes unavailable.
14
+ *
15
+ * A turn attaches its own entries — the turn's `connections` and the entries
16
+ * its steps list — with `attach(ctx, …)` before its tool set is built, and
17
+ * `detach(id)` releases them when it ends. They are looked up by name from
18
+ * any context under the turn's root, after the harness's own.
14
19
  */
15
- import type { AnyProfile, ArtifactStore, Connection, ConnectionChangedEventData, ConnectionConfig, ConnectionEventEventData, ConnectionFrameworkEvent, ConnectionIdentity, ConnectionRequirement, ConnectionsConfig, ConnectionTaskEventData, Context, EventStream, SecretResolver, StorageAdapter, StreamChangedEventData } from '@noetic-tools/types';
20
+ import type { AnyProfile, ArtifactStore, Connection, ConnectionChangedEventData, ConnectionConfig, ConnectionEntries, ConnectionEventEventData, ConnectionFrameworkEvent, ConnectionIdentity, ConnectionRequirement, ConnectionTaskEventData, Context, EventStream, SecretResolver, StorageAdapter, StreamChangedEventData } from '@noetic-tools/types';
21
+ import type { AttachedEntry, AttachedScope } from '../types/connection-entry.js';
16
22
  import type { ResumeDecision } from './durability.js';
17
23
  import type { GateHooks } from './gates.js';
18
24
  import type { ConnectionRegistry, ResolvedConnection, ResolveRequest } from './registry-types.js';
@@ -25,7 +31,7 @@ export interface ConnectionFrameworkEventRecord {
25
31
  }
26
32
  /** Options for `createConnectionRuntime`. */
27
33
  export interface ConnectionRuntimeOptions {
28
- readonly connections?: ConnectionsConfig;
34
+ readonly connections?: ConnectionEntries;
29
35
  readonly storage: StorageAdapter;
30
36
  readonly artifacts: ArtifactStore;
31
37
  readonly secrets?: SecretResolver;
@@ -50,6 +56,29 @@ export interface NeedOptions {
50
56
  }
51
57
  export interface ConnectionRuntime extends ConnectionRegistry {
52
58
  resolve(request: RuntimeResolveRequest): Promise<ResolvedConnection>;
59
+ /**
60
+ * Attaches a turn's entries under its root context and connects them. An
61
+ * entry whose name the harness or the turn already holds with the same
62
+ * provider and options is that connection; it is not connected twice.
63
+ *
64
+ * @throws NoeticConfigError `CONNECTION_NAME_TAKEN` when a name is held with a
65
+ * different provider or options, `CONNECTION_INVALID` for a malformed entry
66
+ * or one that sets `pool`.
67
+ */
68
+ attach(root: Context, entries: ReadonlyArray<AttachedEntry>): Promise<void>;
69
+ /** Releases what a turn attached, once nothing holds it (see `hold`). */
70
+ detach(rootId: string): Promise<void>;
71
+ /**
72
+ * Keeps a turn's entries attached past the turn — for a session that outlives
73
+ * it — until the returned function is called.
74
+ */
75
+ hold(rootId: string): () => void;
76
+ /** Harness names, then the names a turn attached. */
77
+ visibleNames(rootId?: string): ReadonlyArray<string>;
78
+ /** Where `name` lives for a turn: `'harness'`, or the scope it was attached at. */
79
+ scopeOf(name: string, rootId?: string): 'harness' | AttachedScope | undefined;
80
+ /** Whether a view layer renders the connection's `'turn'` and `'effect'` reads. */
81
+ viewed(name: string, rootId?: string): boolean;
53
82
  /** Connects every provider and starts every pool. Idempotent. */
54
83
  start(): Promise<void>;
55
84
  /**
@@ -61,12 +90,12 @@ export interface ConnectionRuntime extends ConnectionRegistry {
61
90
  rerunAllowed(executionId: string, name: string): boolean;
62
91
  /** Releases every connection and lease. */
63
92
  close(): Promise<void>;
64
- /** The harness configuration of a connection. */
65
- config(name: string): ConnectionConfig | undefined;
93
+ /** The configuration of a connection: the harness's, or one a turn attached. */
94
+ config(name: string, rootId?: string): ConnectionConfig | undefined;
66
95
  /** The live provider connection behind a non-pool name, when it is ready. */
67
- current(name: string): Connection | undefined;
96
+ current(name: string, rootId?: string): Connection | undefined;
68
97
  /** Bumped every time an operation with effects runs on the connection. */
69
- effectCount(name: string): number;
98
+ effectCount(name: string, rootId?: string): number;
70
99
  /** Records what the harness graph requires of a connection; renegotiated on manifest changes. */
71
100
  require(name: string, requirement: ConnectionRequirement, options?: NeedOptions): void;
72
101
  /**
@@ -11,14 +11,19 @@
11
11
  * reconnected with backoff while every other connection keeps working. When a
12
12
  * connection's manifest changes it is renegotiated against what the harness
13
13
  * graph requires; one that no longer satisfies it becomes unavailable.
14
+ *
15
+ * A turn attaches its own entries — the turn's `connections` and the entries
16
+ * its steps list — with `attach(ctx, …)` before its tool set is built, and
17
+ * `detach(id)` releases them when it ends. They are looked up by name from
18
+ * any context under the turn's root, after the harness's own.
14
19
  */
15
- import { isConnectionProvider, normalizeConnectionEntry } from '@noetic-tools/types';
20
+ import { rootOf } from '../util/root-context.js';
16
21
  import { withholdSensitiveBlobs } from './blobs.js';
17
22
  import { resumeDecision } from './durability.js';
18
- import { invalidConnectionError, notAllowedConnectionError, resumeRefusedError, unavailableError, unknownConnectionError, } from './errors.js';
23
+ import { checkEntry, namedEntries, sameConfig } from './entries.js';
24
+ import { invalidConnectionError, nameTakenError, notAllowedConnectionError, resumeRefusedError, unavailableError, unknownConnectionError, } from './errors.js';
19
25
  import { bufferedEventStream } from './event-stream.js';
20
26
  import { admitIncoming, gatedAwaitTask, gatedCall } from './gates.js';
21
- import { CONNECTION_NAME_PATTERN } from './handle.js';
22
27
  import { checkRequirement } from './negotiation.js';
23
28
  import { ConnectionPool } from './pool.js';
24
29
  import { kindInfoFrom, RefSealer, refKindsOf } from './refs.js';
@@ -26,11 +31,6 @@ import { SecretScrubber } from './secrets.js';
26
31
  import { runEvery } from './settle.js';
27
32
  //#endregion
28
33
  //#region Helpers
29
- const EXPOSE_VALUES = new Set([
30
- 'eager',
31
- 'all',
32
- 'none',
33
- ]);
34
34
  function defaultReconnectDelay(attempt) {
35
35
  return Math.min(3e4, 1e3 * 2 ** attempt);
36
36
  }
@@ -42,17 +42,6 @@ function unrefTimeout(timer) {
42
42
  timer.unref();
43
43
  }
44
44
  }
45
- function validateEntry(name, config) {
46
- if (!CONNECTION_NAME_PATTERN.test(name)) {
47
- throw invalidConnectionError(`Connection name '${name}' is not valid.`, 'Start with a letter and use only letters, digits, _ and -.');
48
- }
49
- if (!isConnectionProvider(config.provider)) {
50
- throw invalidConnectionError(`Connection '${name}' has no provider.`, 'Pass mcp(…), a source, or any ConnectionProvider (an object with connect()).');
51
- }
52
- if (config.expose !== undefined && !EXPOSE_VALUES.has(config.expose)) {
53
- throw invalidConnectionError(`Connection '${name}' has expose '${String(config.expose)}'.`, "Use expose: 'eager', 'all' or 'none'.");
54
- }
55
- }
56
45
  function hostOnlyViolation(profile, setup) {
57
46
  const hostOnly = profile?.definition.setup?.hostOnly ?? [];
58
47
  return hostOnly.find((field) => setup !== undefined && field in setup);
@@ -76,8 +65,14 @@ function validateSetup(name, profile, setup) {
76
65
  class Runtime {
77
66
  options;
78
67
  entries = new Map();
68
+ /** What each live turn attached, by its root context id. */
69
+ attached = new Map();
79
70
  sealer;
80
71
  scrubber = new SecretScrubber();
72
+ /** Handed to providers so the credentials they hold are scrubbed like resolved secrets. */
73
+ redact = (value) => {
74
+ this.scrubber.add(value);
75
+ };
81
76
  refKinds = new Map();
82
77
  /** Every profile handles name, by id, handed to providers when they connect. */
83
78
  profiles = new Map();
@@ -92,12 +87,14 @@ class Runtime {
92
87
  for (const profile of options.profiles ?? []) {
93
88
  this.registerProfile(profile);
94
89
  }
95
- for (const [name, raw] of Object.entries(options.connections ?? {})) {
96
- const config = normalizeConnectionEntry(raw);
97
- validateEntry(name, config);
90
+ for (const { name, config } of namedEntries(options.connections, 'The harness connections')) {
91
+ checkEntry(name, config);
98
92
  this.entries.set(name, {
99
93
  name,
100
94
  config,
95
+ scope: 'harness',
96
+ home: this.entries,
97
+ viewed: !config.pool && config.expose !== 'call',
101
98
  pool: config.pool ? this.createPool(name, config) : undefined,
102
99
  state: 'connecting',
103
100
  effects: 0,
@@ -106,6 +103,25 @@ class Runtime {
106
103
  });
107
104
  }
108
105
  }
106
+ /** The entry `name` names for a turn: the harness's, else one the turn attached. */
107
+ lookup(name, rootId) {
108
+ const own = this.entries.get(name);
109
+ if (own || rootId === undefined) {
110
+ return own;
111
+ }
112
+ return this.attached.get(rootId)?.entries.get(name);
113
+ }
114
+ /**
115
+ * The entry a request resolves. A step's entry is reachable only through the
116
+ * model tools the turn built from it; handles reach harness and turn entries.
117
+ */
118
+ lookupFor(request, target) {
119
+ const entry = this.lookup(target, request.ctx ? rootOf(request.ctx).id : request.rootId);
120
+ if (entry?.scope === 'step' && request.caller !== 'model') {
121
+ return undefined;
122
+ }
123
+ return entry;
124
+ }
109
125
  //#region Registry contract
110
126
  names() {
111
127
  return [
@@ -114,7 +130,7 @@ class Runtime {
114
130
  }
115
131
  async resolve(request) {
116
132
  const target = request.from ?? request.name;
117
- const entry = this.entries.get(target);
133
+ const entry = this.lookupFor(request, target);
118
134
  if (!entry) {
119
135
  throw unknownConnectionError(target, this.names());
120
136
  }
@@ -191,16 +207,28 @@ class Runtime {
191
207
  async close() {
192
208
  this.controller.abort();
193
209
  // Every entry is closed even when one throws; the first failure is rethrown.
210
+ const attachedEntries = [
211
+ ...this.attached.values(),
212
+ ].flatMap((attachment) => [
213
+ ...attachment.entries.values(),
214
+ ]);
215
+ this.attached.clear();
194
216
  await runEvery([
195
217
  ...this.entries.values(),
196
- ].map((entry) => () => this.closeEntry(entry)));
218
+ ...attachedEntries,
219
+ ].map((entry) => () => this.closeEntry(entry, 'the harness closed')));
197
220
  }
198
- async closeEntry(entry) {
221
+ async closeEntry(entry, reason) {
199
222
  if (entry.reconnectTimer) {
200
223
  clearTimeout(entry.reconnectTimer);
224
+ entry.reconnectTimer = undefined;
201
225
  }
202
226
  const connection = entry.connection;
227
+ const connecting = entry.connecting;
228
+ this.setState(entry, 'closed', reason);
203
229
  try {
230
+ // A connect in flight sees the entry closed and releases what it got.
231
+ await connecting?.catch(() => undefined);
204
232
  await runEvery([
205
233
  () => entry.unsubscribe?.(),
206
234
  () => entry.pool?.close(),
@@ -208,19 +236,115 @@ class Runtime {
208
236
  }
209
237
  finally {
210
238
  entry.connection = undefined;
211
- this.setState(entry, 'closed', 'the harness closed');
212
239
  await connection?.release().catch(() => undefined);
213
240
  }
214
241
  }
215
- config(name) {
216
- return this.entries.get(name)?.config;
242
+ config(name, rootId) {
243
+ return this.lookup(name, rootId)?.config;
217
244
  }
218
- current(name) {
219
- const entry = this.entries.get(name);
245
+ current(name, rootId) {
246
+ const entry = this.lookup(name, rootId);
220
247
  return entry?.state === 'ready' ? entry.connection : undefined;
221
248
  }
222
- effectCount(name) {
223
- return this.entries.get(name)?.effects ?? 0;
249
+ effectCount(name, rootId) {
250
+ return this.lookup(name, rootId)?.effects ?? 0;
251
+ }
252
+ visibleNames(rootId) {
253
+ const own = rootId === undefined ? undefined : this.attached.get(rootId);
254
+ return [
255
+ ...this.entries.keys(),
256
+ ...(own ? own.entries.keys() : []),
257
+ ];
258
+ }
259
+ scopeOf(name, rootId) {
260
+ return this.lookup(name, rootId)?.scope;
261
+ }
262
+ viewed(name, rootId) {
263
+ return this.lookup(name, rootId)?.viewed ?? false;
264
+ }
265
+ //#endregion
266
+ //#region Attaching
267
+ async attach(root, entries) {
268
+ const attachment = this.attached.get(root.id) ?? {
269
+ entries: new Map(),
270
+ holds: 0,
271
+ released: false,
272
+ };
273
+ const added = entries.flatMap((entry) => this.admitAttached(root, attachment, entry));
274
+ if (added.length === 0) {
275
+ return;
276
+ }
277
+ attachment.released = false;
278
+ this.attached.set(root.id, attachment);
279
+ await Promise.all(added.map((entry) => this.ensureConnected(entry).catch(() => undefined)));
280
+ }
281
+ /**
282
+ * Adds one entry to a turn's attachment, or nothing when its name already
283
+ * names the same connection there or on the harness.
284
+ */
285
+ admitAttached(root, attachment, entry) {
286
+ checkEntry(entry.name, entry.config);
287
+ if (entry.config.pool) {
288
+ throw invalidConnectionError(`${entry.where} gives connection '${entry.name}' a pool.`, 'Pools stay at the harness: move the entry to new AgentHarness({ connections }).');
289
+ }
290
+ const held = this.entries.get(entry.name) ?? attachment.entries.get(entry.name);
291
+ if (held) {
292
+ if (!sameConfig(held.config, entry.config)) {
293
+ throw nameTakenError(entry.name, entry.where, held.scope);
294
+ }
295
+ return [];
296
+ }
297
+ const created = {
298
+ name: entry.name,
299
+ config: entry.config,
300
+ scope: entry.scope,
301
+ home: attachment.entries,
302
+ root,
303
+ viewed: entry.viewed === true && entry.config.expose !== 'call',
304
+ state: 'connecting',
305
+ effects: 0,
306
+ attempts: 0,
307
+ requirements: [],
308
+ };
309
+ attachment.entries.set(entry.name, created);
310
+ return [
311
+ created,
312
+ ];
313
+ }
314
+ async detach(rootId) {
315
+ const attachment = this.attached.get(rootId);
316
+ if (!attachment) {
317
+ return;
318
+ }
319
+ attachment.released = true;
320
+ if (attachment.holds > 0) {
321
+ return;
322
+ }
323
+ this.attached.delete(rootId);
324
+ await runEvery([
325
+ ...attachment.entries.values(),
326
+ ].map((entry) => () => this.closeEntry(entry, 'the turn ended')));
327
+ }
328
+ hold(rootId) {
329
+ // A hold may come before the turn attaches anything; what it attaches later is held too.
330
+ const attachment = this.attached.get(rootId) ?? {
331
+ entries: new Map(),
332
+ holds: 0,
333
+ released: false,
334
+ };
335
+ this.attached.set(rootId, attachment);
336
+ attachment.holds += 1;
337
+ let done = false;
338
+ return () => {
339
+ if (done) {
340
+ return;
341
+ }
342
+ done = true;
343
+ attachment.holds -= 1;
344
+ if (attachment.released || (attachment.holds === 0 && attachment.entries.size === 0)) {
345
+ void this.detach(rootId);
346
+ }
347
+ };
224
348
  }
225
349
  require(name, requirement, options) {
226
350
  const entry = this.entries.get(name);
@@ -268,7 +392,7 @@ class Runtime {
268
392
  this.reruns.set(executionId, set);
269
393
  }
270
394
  async checkResume(params) {
271
- const entry = this.entries.get(params.name);
395
+ const entry = this.lookup(params.name, params.executionId);
272
396
  if (!entry) {
273
397
  throw unknownConnectionError(params.name, this.names());
274
398
  }
@@ -408,15 +532,24 @@ class Runtime {
408
532
  profiles: [
409
533
  ...this.profiles.values(),
410
534
  ],
535
+ redact: this.redact,
536
+ secrets: this.options.secrets,
411
537
  });
538
+ if (entry.state === 'closed') {
539
+ await runEvery(connections.map((connection) => () => connection.release()));
540
+ return;
541
+ }
412
542
  const [first, ...extra] = connections;
413
543
  if (!first) {
414
544
  throw new Error('the provider returned no connections');
415
545
  }
416
- this.attach(entry, first);
546
+ this.attachConnection(entry, first);
417
547
  this.attachExtra(entry, extra);
418
548
  }
419
549
  catch (e) {
550
+ if (entry.state === 'closed') {
551
+ return;
552
+ }
420
553
  this.setState(entry, 'unavailable', errorReason(e));
421
554
  this.scheduleReconnect(entry);
422
555
  throw unavailableError(entry.name, errorReason(e));
@@ -426,20 +559,24 @@ class Runtime {
426
559
  attachExtra(entry, extra) {
427
560
  for (const connection of extra) {
428
561
  const name = `${entry.name}-${connection.id.replace(/[^A-Za-z0-9_-]/g, '_')}`;
429
- const existing = this.entries.get(name);
562
+ const existing = entry.home.get(name);
430
563
  const sibling = existing ?? {
431
564
  name,
432
565
  config: entry.config,
566
+ scope: entry.scope,
567
+ home: entry.home,
568
+ root: entry.root,
569
+ viewed: false,
433
570
  state: 'connecting',
434
571
  effects: 0,
435
572
  attempts: 0,
436
573
  requirements: [],
437
574
  };
438
- this.entries.set(name, sibling);
439
- this.attach(sibling, connection);
575
+ entry.home.set(name, sibling);
576
+ this.attachConnection(sibling, connection);
440
577
  }
441
578
  }
442
- attach(entry, connection) {
579
+ attachConnection(entry, connection) {
443
580
  entry.unsubscribe?.();
444
581
  entry.connection = connection;
445
582
  entry.attempts = 0;
@@ -535,6 +672,8 @@ class Runtime {
535
672
  profiles: [
536
673
  ...this.profiles.values(),
537
674
  ],
675
+ redact: this.redact,
676
+ secrets: this.options.secrets,
538
677
  }),
539
678
  onLease: (lease, change) => this.emitLease(name, lease, change),
540
679
  });
@@ -708,12 +847,17 @@ class Runtime {
708
847
  }
709
848
  setState(entry, state, reason) {
710
849
  entry.state = state;
711
- entry.reason = reason;
850
+ entry.reason = reason === undefined ? undefined : this.scrubber.scrubText(reason);
712
851
  this.emitChanged(entry);
713
852
  }
714
853
  emitChanged(entry) {
715
854
  this.emit({
716
855
  type: 'connection_changed',
856
+ ...(entry.root
857
+ ? {
858
+ ctx: entry.root,
859
+ }
860
+ : {}),
717
861
  data: {
718
862
  connection: entry.name,
719
863
  state: entry.state,
@@ -0,0 +1,64 @@
1
+ /**
2
+ * What a model step is offered of a turn's connection tools (spec 35 § Step
3
+ * tools). Every model call sends the turn's whole tool set and narrows it by
4
+ * name, so a step's `tools` decide what it may run:
5
+ *
6
+ * - With no `tools`, a step is offered every tool except those of connections
7
+ * only some step listed.
8
+ * - A step that lists a connection entry is offered that connection's tools;
9
+ * one that lists a `ConnectionPick` is offered the ones matching its `ops`.
10
+ *
11
+ * Each tool is cut to what the step may use, so a merged `ui_tap`, `ops` or
12
+ * `call` reaches only the connections the step lists and a `<name>_call` only
13
+ * the operations and reads it picked. A cut tool keeps its name and stands in
14
+ * the step's request in place of the turn's.
15
+ */
16
+ import type { ConnectionPick, Context, ServerToolSpec, StepToolEntry, Tool } from '@noetic-tools/types';
17
+ import type { ConnectionRuntime } from './registry.js';
18
+ /** A turn's connections, as a step sees them. */
19
+ export interface StepConnections {
20
+ readonly runtime: ConnectionRuntime;
21
+ readonly rootId: string;
22
+ }
23
+ /** A step's `tools`, split by kind. */
24
+ export interface SplitStepTools {
25
+ readonly tools: Tool[];
26
+ readonly serverTools: ServerToolSpec[];
27
+ /** The connections it lists: entries (no `ops`) and picks. */
28
+ readonly picks: ConnectionPick[];
29
+ /**
30
+ * The picks are a ceiling, not a list: a document node with no `tools` may
31
+ * reach only these connections, and keeps every other tool a step with no
32
+ * `tools` is offered.
33
+ */
34
+ readonly ceiling?: true;
35
+ }
36
+ /** The turn's connections, as a step's context sees them; undefined when the harness has no registry. */
37
+ export declare function stepConnectionsOf(ctx: Context): StepConnections | undefined;
38
+ /**
39
+ * The split a step with no `tools` stands for when a document node built it
40
+ * under a connections allowlist: its ceiling, as picks. Undefined otherwise.
41
+ */
42
+ export declare function splitCeiling(step: object): SplitStepTools | undefined;
43
+ /** Splits a step's `tools` into client tools, server tools and connection picks. */
44
+ export declare function splitStepTools(entries: ReadonlyArray<StepToolEntry>): SplitStepTools;
45
+ /**
46
+ * The tools of `unified` a step offers, each connection tool cut to what the
47
+ * step may use. Returns undefined for a step with no `tools` when nothing
48
+ * needs holding back, so its call stays unrestricted.
49
+ */
50
+ export declare function offeredTools(unified: ReadonlyArray<Tool>, listed: SplitStepTools | undefined, turn: StepConnections | undefined): Tool[] | undefined;
51
+ /**
52
+ * The tool set a step's request sends: the turn's, with each tool the step
53
+ * was offered cut standing in its place.
54
+ */
55
+ export declare function withOffered(unified: ReadonlyArray<Tool>, offered: ReadonlyArray<Tool> | undefined): Tool[];
56
+ /**
57
+ * Checks the entries a function-form `tools` returned: each must name a
58
+ * connection the harness or the turn already attached, with the same provider
59
+ * and options, because the walk that connects step entries cannot see inside
60
+ * a function.
61
+ *
62
+ * @throws NoeticConfigError `CONNECTION_INVALID` for an entry nothing attached.
63
+ */
64
+ export declare function checkLazyEntries(stepId: string, listed: ReadonlyArray<StepToolEntry>, turn: StepConnections | undefined): void;
@@ -0,0 +1,154 @@
1
+ /**
2
+ * What a model step is offered of a turn's connection tools (spec 35 § Step
3
+ * tools). Every model call sends the turn's whole tool set and narrows it by
4
+ * name, so a step's `tools` decide what it may run:
5
+ *
6
+ * - With no `tools`, a step is offered every tool except those of connections
7
+ * only some step listed.
8
+ * - A step that lists a connection entry is offered that connection's tools;
9
+ * one that lists a `ConnectionPick` is offered the ones matching its `ops`.
10
+ *
11
+ * Each tool is cut to what the step may use, so a merged `ui_tap`, `ops` or
12
+ * `call` reaches only the connections the step lists and a `<name>_call` only
13
+ * the operations and reads it picked. A cut tool keeps its name and stands in
14
+ * the step's request in place of the turn's.
15
+ */
16
+ import { isConnectionEntry, isConnectionPick, isServerToolSpec } from '@noetic-tools/types';
17
+ import { connectionCeilingOf } from '../builders/connection-ceiling.js';
18
+ import { rootOf } from '../util/root-context.js';
19
+ import { namedFromList, sameConfig } from './entries.js';
20
+ import { invalidConnectionError } from './errors.js';
21
+ import { matchesAnyGlob } from './glob.js';
22
+ import { cutForStep, runtimeOf } from './model-tools.js';
23
+ //#endregion
24
+ //#region Public API
25
+ /** The turn's connections, as a step's context sees them; undefined when the harness has no registry. */
26
+ export function stepConnectionsOf(ctx) {
27
+ const runtime = runtimeOf(ctx.harness);
28
+ if (!runtime) {
29
+ return undefined;
30
+ }
31
+ return {
32
+ runtime,
33
+ rootId: rootOf(ctx).id,
34
+ };
35
+ }
36
+ /**
37
+ * The split a step with no `tools` stands for when a document node built it
38
+ * under a connections allowlist: its ceiling, as picks. Undefined otherwise.
39
+ */
40
+ export function splitCeiling(step) {
41
+ const ceiling = connectionCeilingOf(step);
42
+ return ceiling
43
+ ? {
44
+ tools: [],
45
+ serverTools: [],
46
+ picks: [
47
+ ...ceiling,
48
+ ],
49
+ ceiling: true,
50
+ }
51
+ : undefined;
52
+ }
53
+ /** Splits a step's `tools` into client tools, server tools and connection picks. */
54
+ export function splitStepTools(entries) {
55
+ const split = {
56
+ tools: [],
57
+ serverTools: [],
58
+ picks: [],
59
+ };
60
+ for (const entry of entries) {
61
+ if (isServerToolSpec(entry)) {
62
+ split.serverTools.push(entry);
63
+ continue;
64
+ }
65
+ if (isConnectionPick(entry)) {
66
+ split.picks.push(entry);
67
+ continue;
68
+ }
69
+ if (isConnectionEntry(entry)) {
70
+ split.picks.push(pickOfEntry(namedFromList(entry, 'A step')));
71
+ continue;
72
+ }
73
+ split.tools.push(entry);
74
+ }
75
+ return split;
76
+ }
77
+ /**
78
+ * The tools of `unified` a step offers, each connection tool cut to what the
79
+ * step may use. Returns undefined for a step with no `tools` when nothing
80
+ * needs holding back, so its call stays unrestricted.
81
+ */
82
+ export function offeredTools(unified, listed, turn) {
83
+ const allows = listed ? pickedBy(listed.picks) : sharedIn(turn);
84
+ const names = listed && !listed.ceiling ? new Set(listed.tools.map((tool) => tool.name)) : undefined;
85
+ const offered = unified.flatMap((tool) => {
86
+ const cut = cutForStep(tool, allows);
87
+ if (cut === 'not-connection') {
88
+ return !names || names.has(tool.name)
89
+ ? [
90
+ tool,
91
+ ]
92
+ : [];
93
+ }
94
+ return cut
95
+ ? [
96
+ cut,
97
+ ]
98
+ : [];
99
+ });
100
+ const whole = offered.length === unified.length && offered.every((t, i) => t === unified[i]);
101
+ return (!listed || listed.ceiling) && whole ? undefined : offered;
102
+ }
103
+ /**
104
+ * The tool set a step's request sends: the turn's, with each tool the step
105
+ * was offered cut standing in its place.
106
+ */
107
+ export function withOffered(unified, offered) {
108
+ const byName = new Map((offered ?? []).map((tool) => [
109
+ tool.name,
110
+ tool,
111
+ ]));
112
+ return unified.map((tool) => byName.get(tool.name) ?? tool);
113
+ }
114
+ /**
115
+ * Checks the entries a function-form `tools` returned: each must name a
116
+ * connection the harness or the turn already attached, with the same provider
117
+ * and options, because the walk that connects step entries cannot see inside
118
+ * a function.
119
+ *
120
+ * @throws NoeticConfigError `CONNECTION_INVALID` for an entry nothing attached.
121
+ */
122
+ export function checkLazyEntries(stepId, listed, turn) {
123
+ for (const raw of listed) {
124
+ const entry = isConnectionPick(raw) ? raw.entry : isConnectionEntry(raw) ? raw : undefined;
125
+ if (entry === undefined) {
126
+ continue;
127
+ }
128
+ const named = namedFromList(entry, `callModel('${stepId}') tools`);
129
+ const held = turn?.runtime.config(named.name, turn.rootId);
130
+ const scope = turn?.runtime.scopeOf(named.name, turn.rootId);
131
+ if (held && scope !== 'step' && sameConfig(held, named.config)) {
132
+ continue;
133
+ }
134
+ throw invalidConnectionError(`callModel('${stepId}') returns connection '${named.name}' from a function-form tools, which the turn never connected.`, 'Connections in a tools function must already be attached: move the entry to new AgentHarness({ connections }) or execute(input, { connections }), or list it in an eager tools array.');
135
+ }
136
+ }
137
+ //#endregion
138
+ //#region Helpers
139
+ function pickOfEntry(named) {
140
+ return {
141
+ kind: 'connection-pick',
142
+ connection: named.name,
143
+ };
144
+ }
145
+ /** What a step that lists `picks` may use: what some pick names, its `ops` globs matching operations and reads alike. */
146
+ function pickedBy(picks) {
147
+ return (connection, name) => picks.some((pick) => pick.connection === connection &&
148
+ (pick.ops === undefined || matchesAnyGlob(pick.ops, name)));
149
+ }
150
+ /** What a step with no `tools` may use: every connection but those only some step lists. */
151
+ function sharedIn(turn) {
152
+ return (connection) => turn?.runtime.scopeOf(connection, turn.rootId) !== 'step';
153
+ }
154
+ //#endregion
@@ -24,8 +24,9 @@ export interface ConnectionViewState {
24
24
  export declare function lineDiff(before: string, after: string): string;
25
25
  /**
26
26
  * The view layer for one connection. It renders nothing while the connection
27
- * is not ready or has no `'turn'`/`'effect'` reads.
27
+ * is not ready or has no `'turn'`/`'effect'` reads. For a connection a turn
28
+ * attaches, `rootOf` names the turn once its context exists.
28
29
  */
29
- export declare function connectionViewLayer(runtime: ConnectionRuntime, name: string): ContextLayer<ConnectionViewState>;
30
+ export declare function connectionViewLayer(runtime: ConnectionRuntime, name: string, rootOf?: () => string | undefined): ContextLayer<ConnectionViewState>;
30
31
  /** A view layer for every non-pool connection in `runtime`. */
31
32
  export declare function connectionViewLayers(runtime: ConnectionRuntime): ContextLayer<ConnectionViewState>[];