@noetic-tools/core 5.14.0 → 5.14.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.
@@ -735,7 +735,7 @@ export async function executeSpawn(step, input, ctx, executeStep, opts) {
735
735
  id: childId,
736
736
  // The spawn's own path prefixes the child's, so a resumed run lines its steps up
737
737
  // with the ones recorded inside the same spawn rather than at the root.
738
- pathPrefix: baseCtx instanceof ContextImpl ? baseCtx.currentPath() : '',
738
+ path: baseCtx instanceof ContextImpl ? baseCtx.branchPath('') : undefined,
739
739
  ledger: baseCtx instanceof ContextImpl ? baseCtx.ledger : undefined,
740
740
  // The child's steps checkpoint the root, as an inParallel path's do.
741
741
  checkpointFn: () => baseCtx.checkpoint(),
@@ -3,7 +3,7 @@
3
3
  */
4
4
  import { createMessage, frameworkCast, isNoeticConfigError, isNoeticError, NoeticErrorImpl, } from '@noetic-tools/types';
5
5
  import { NoeticAttr, NoeticEvent } from '../observability/genai-attributes.js';
6
- import { ContextImpl } from '../runtime/context-impl.js';
6
+ import { ContextImpl, PathNode } from '../runtime/context-impl.js';
7
7
  import { snapshotCwdState } from '../runtime/cwd-helpers.js';
8
8
  import { toUntil } from '../until/decider.js';
9
9
  import { contextToExecCtx, returnLayers, spawnLayers } from './action-deps.js';
@@ -27,7 +27,6 @@ function createChildContexts(ctx, paths, stepId) {
27
27
  const threadId = isContextImpl(ctx) ? ctx.threadId : crypto.randomUUID();
28
28
  const resourceId = isContextImpl(ctx) ? ctx.resourceId : undefined;
29
29
  const channelStore = getContextChannelStore(ctx);
30
- const parentPath = isContextImpl(ctx) ? ctx.currentPath() : '';
31
30
  const parentLedger = isContextImpl(ctx) ? ctx.ledger : undefined;
32
31
  return paths.map((path, index) => new ContextImpl({
33
32
  harness: ctx.harness,
@@ -40,7 +39,9 @@ function createChildContexts(ctx, paths, stepId) {
40
39
  /* Ledger paths must stay unique across sibling paths: a child ContextImpl
41
40
  * starts with an empty frontier, so without a prefix every path would
42
41
  * restart at the root and collide with its siblings. */
43
- pathPrefix: `${parentPath}[${index}]`,
42
+ path: isContextImpl(ctx)
43
+ ? ctx.branchPath(`[${index}]`)
44
+ : new PathNode(undefined, `[${index}]`),
44
45
  ledger: parentLedger,
45
46
  // A path's steps checkpoint the root: its usage, state, and layer folds roll
46
47
  // into what a resumed root restores.
@@ -1,4 +1,11 @@
1
1
  import type { Span } from '@noetic-tools/types';
2
+ /**
3
+ * Most events one span keeps. An uncapped loop adds a `loop.verdict` event to its
4
+ * enclosing span every iteration, and a long-lived span would hold them all, so a
5
+ * span keeps the latest events and counts the rest. 128 matches OpenTelemetry's
6
+ * default event count limit.
7
+ */
8
+ export declare const MAX_SPAN_EVENTS = 128;
2
9
  export declare class SpanImpl implements Span {
3
10
  readonly traceId: string;
4
11
  readonly spanId: string;
@@ -12,6 +19,8 @@ export declare class SpanImpl implements Span {
12
19
  timestamp: number;
13
20
  attributes?: Record<string, string | number | boolean>;
14
21
  }>;
22
+ /** Events dropped, oldest first, once the span held `MAX_SPAN_EVENTS`. */
23
+ droppedEvents: number;
15
24
  constructor(name: string, parent: Span | null, traceId?: string);
16
25
  setAttribute(key: string, value: string | number | boolean): void;
17
26
  addEvent(name: string, attributes?: Record<string, string | number | boolean>): void;
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Most events one span keeps. An uncapped loop adds a `loop.verdict` event to its
3
+ * enclosing span every iteration, and a long-lived span would hold them all, so a
4
+ * span keeps the latest events and counts the rest. 128 matches OpenTelemetry's
5
+ * default event count limit.
6
+ */
7
+ export const MAX_SPAN_EVENTS = 128;
1
8
  export class SpanImpl {
2
9
  traceId;
3
10
  spanId;
@@ -7,6 +14,8 @@ export class SpanImpl {
7
14
  endTime;
8
15
  attributes = new Map();
9
16
  events = [];
17
+ /** Events dropped, oldest first, once the span held `MAX_SPAN_EVENTS`. */
18
+ droppedEvents = 0;
10
19
  constructor(name, parent, traceId) {
11
20
  this.name = name;
12
21
  this.traceId = traceId ?? parent?.traceId ?? crypto.randomUUID();
@@ -23,6 +32,10 @@ export class SpanImpl {
23
32
  timestamp: Date.now(),
24
33
  attributes,
25
34
  });
35
+ if (this.events.length > MAX_SPAN_EVENTS) {
36
+ this.events.shift();
37
+ this.droppedEvents += 1;
38
+ }
26
39
  }
27
40
  end() {
28
41
  this.endTime = Date.now();
@@ -80,19 +80,28 @@ export declare class ContextImpl implements Context<ContextData> {
80
80
  */
81
81
  private readonly _inFlight;
82
82
  /**
83
- * Execution path of the step whose lane this is. On the context itself it is the
84
- * prefix its steps hang under: empty on a root context, the parent's path plus a
85
- * discriminator on an inParallel/spawn child, so a path stays unique across the whole
86
- * step tree. A lane (see `enterStep`) reads its own step's path here instead.
83
+ * Execution path node of the step whose lane this is. On the context itself it is
84
+ * the prefix its steps hang under: empty on a root context, the parent's path plus
85
+ * a discriminator on an inParallel/spawn child, so a path stays unique across the
86
+ * whole step tree. A lane (see `enterStep`) reads its own step's node here instead.
87
+ * Each node counts the dispatches beneath it, so a loop body re-entered on each
88
+ * iteration gets `#0`, `#1`, … instead of colliding on one path.
87
89
  * @internal
88
90
  */
89
- private readonly _path;
91
+ private readonly _node;
90
92
  /**
91
- * Dispatch counts per `<parent path>/<step id>`, so a loop body re-entered on each
92
- * iteration gets `#0`, `#1`, … instead of colliding on one key. Shared by every lane.
93
+ * Nodes on this context holding dispatch counts, so a run baseline can list every
94
+ * live count. Shared by every lane.
93
95
  * @internal
94
96
  */
95
- private readonly _occurrences;
97
+ private readonly _counted;
98
+ /**
99
+ * Dispatch counts restored from a checkpoint, keyed `<parent path>/<step id>`, not
100
+ * yet taken by a dispatch. A dispatch moves its count into its parent node; until
101
+ * this empties, each dispatch builds its parent's path to look its count up.
102
+ * @internal
103
+ */
104
+ private readonly _restored;
96
105
  /**
97
106
  * Completion ledger for the execution this context belongs to. Shared by reference
98
107
  * with inParallel/spawn children — one execution, one ledger.
@@ -126,8 +135,8 @@ export declare class ContextImpl implements Context<ContextData> {
126
135
  span?: Span;
127
136
  channelStore?: ChannelStore;
128
137
  checkpointFn?: (ctx: Context) => Promise<void>;
129
- /** Path this context's steps hang under (inParallel/spawn children inherit one). */
130
- pathPrefix?: string;
138
+ /** Path node this context's steps hang under (inParallel/spawn children inherit one; see `branchPath`). */
139
+ path?: PathNode;
131
140
  /** The execution's completion ledger, shared with children by reference. */
132
141
  ledger?: StepLedger;
133
142
  layers?: ContextLayer[];
@@ -222,6 +231,12 @@ export declare class ContextImpl implements Context<ContextData> {
222
231
  * what lets a resumed run line recorded outputs up with the steps that produced them.
223
232
  */
224
233
  currentPath(): string;
234
+ /**
235
+ * @internal
236
+ * A path node under this lane's path, for an inParallel/spawn child context to hang
237
+ * its steps under. The child's path string is built only if something reads it.
238
+ */
239
+ branchPath(segment: string): PathNode;
225
240
  /**
226
241
  * @internal
227
242
  * Items appended to the item log through this lane, including by steps it
@@ -290,6 +305,13 @@ export declare class ContextImpl implements Context<ContextData> {
290
305
  * its dispatches where the recorded run did rather than from zero.
291
306
  */
292
307
  restoreOccurrences(occurrences: Record<string, number>): void;
308
+ /**
309
+ * Every live dispatch count, keyed `<parent path>/<step id>` — the form checkpoints
310
+ * persist and `restoreOccurrences` reads back.
311
+ */
312
+ private liveCounts;
313
+ /** The restored count for `stepId` under `parent`, consumed on first use; 0 when none. */
314
+ private takeRestored;
293
315
  /**
294
316
  * @internal
295
317
  * Return a defensive copy of the current frontier. Consumed by the
@@ -302,6 +324,24 @@ export interface RunBaseline {
302
324
  items: Item[];
303
325
  occurrences: Record<string, number>;
304
326
  }
327
+ /**
328
+ * @internal
329
+ * One point in the execution path tree: a context's root, or one dispatch. It
330
+ * holds the dispatch counts for steps directly beneath it, keyed by step id, and
331
+ * builds its path string only when something reads it. Deep recursion makes
332
+ * paths long — each level adds a segment and its ids grow — so building every
333
+ * dispatch's path eagerly cost time cubic in depth even when no ledger read it.
334
+ */
335
+ export declare class PathNode {
336
+ private readonly parent;
337
+ private readonly segment;
338
+ /** Dispatches beneath this path, by step id; the count is the next ordinal. */
339
+ readonly counts: Map<string, number>;
340
+ private text?;
341
+ constructor(parent: PathNode | undefined, segment: string);
342
+ /** The full path: the parent's path plus this node's segment. Built once, on first read. */
343
+ get path(): string;
344
+ }
305
345
  /**
306
346
  * @internal
307
347
  * The context a lane views, or `ctx` itself when it is not a lane. For per-execution
@@ -111,19 +111,28 @@ export class ContextImpl {
111
111
  */
112
112
  _inFlight = new Set();
113
113
  /**
114
- * Execution path of the step whose lane this is. On the context itself it is the
115
- * prefix its steps hang under: empty on a root context, the parent's path plus a
116
- * discriminator on an inParallel/spawn child, so a path stays unique across the whole
117
- * step tree. A lane (see `enterStep`) reads its own step's path here instead.
114
+ * Execution path node of the step whose lane this is. On the context itself it is
115
+ * the prefix its steps hang under: empty on a root context, the parent's path plus
116
+ * a discriminator on an inParallel/spawn child, so a path stays unique across the
117
+ * whole step tree. A lane (see `enterStep`) reads its own step's node here instead.
118
+ * Each node counts the dispatches beneath it, so a loop body re-entered on each
119
+ * iteration gets `#0`, `#1`, … instead of colliding on one path.
118
120
  * @internal
119
121
  */
120
- _path;
122
+ _node;
121
123
  /**
122
- * Dispatch counts per `<parent path>/<step id>`, so a loop body re-entered on each
123
- * iteration gets `#0`, `#1`, … instead of colliding on one key. Shared by every lane.
124
+ * Nodes on this context holding dispatch counts, so a run baseline can list every
125
+ * live count. Shared by every lane.
124
126
  * @internal
125
127
  */
126
- _occurrences = new Map();
128
+ _counted = new Set();
129
+ /**
130
+ * Dispatch counts restored from a checkpoint, keyed `<parent path>/<step id>`, not
131
+ * yet taken by a dispatch. A dispatch moves its count into its parent node; until
132
+ * this empties, each dispatch builds its parent's path to look its count up.
133
+ * @internal
134
+ */
135
+ _restored = new Map();
127
136
  /**
128
137
  * Completion ledger for the execution this context belongs to. Shared by reference
129
138
  * with inParallel/spawn children — one execution, one ledger.
@@ -158,7 +167,7 @@ export class ContextImpl {
158
167
  this.resourceId = opts.resourceId;
159
168
  this.channelStore = opts.channelStore;
160
169
  this._checkpointFn = opts.checkpointFn;
161
- this._path = opts.pathPrefix ?? '';
170
+ this._node = opts.path ?? new PathNode(undefined, '');
162
171
  this.ledger = opts.ledger;
163
172
  this.layers = opts.layers;
164
173
  this.unifiedTools = opts.unifiedTools;
@@ -321,13 +330,14 @@ export class ContextImpl {
321
330
  * interleave. The occurrence ordinal is taken here, at dispatch, never at settle.
322
331
  */
323
332
  enterStep(frame) {
324
- const key = `${this._path}/${frame.stepId}`;
325
- const occurrence = this._occurrences.get(key) ?? 0;
326
- this._occurrences.set(key, occurrence + 1);
333
+ const parent = this._node;
334
+ const occurrence = parent.counts.get(frame.stepId) ?? this.takeRestored(parent, frame.stepId);
335
+ parent.counts.set(frame.stepId, occurrence + 1);
336
+ this._counted.add(parent);
327
337
  this._inFlight.add(frame);
328
338
  return laneOf(this, {
329
- path: `${key}#${occurrence}`,
330
- key,
339
+ node: new PathNode(parent, `/${frame.stepId}#${occurrence}`),
340
+ parent,
331
341
  occurrence,
332
342
  stepId: frame.stepId,
333
343
  inputHash: this.ledger ? hashStepInput(frame.input) : undefined,
@@ -355,7 +365,7 @@ export class ContextImpl {
355
365
  const lane = this._lane;
356
366
  return lane?.recorded
357
367
  ? {
358
- path: lane.path,
368
+ path: lane.node.path,
359
369
  stepId: lane.stepId,
360
370
  inputHash: lane.inputHash,
361
371
  }
@@ -368,7 +378,15 @@ export class ContextImpl {
368
378
  * what lets a resumed run line recorded outputs up with the steps that produced them.
369
379
  */
370
380
  currentPath() {
371
- return this._path;
381
+ return this._node.path;
382
+ }
383
+ /**
384
+ * @internal
385
+ * A path node under this lane's path, for an inParallel/spawn child context to hang
386
+ * its steps under. The child's path string is built only if something reads it.
387
+ */
388
+ branchPath(segment) {
389
+ return new PathNode(this._node, segment);
372
390
  }
373
391
  /**
374
392
  * @internal
@@ -414,6 +432,14 @@ export class ContextImpl {
414
432
  */
415
433
  leaveStep(frame) {
416
434
  this._inFlight.delete(frame);
435
+ // Nothing dispatches under a finished step's path again, so the counts beneath it
436
+ // are dead weight. Its own count lives in its parent node and stays: the next
437
+ // dispatch of the same step id needs it.
438
+ const lane = this._lane;
439
+ if (lane) {
440
+ lane.node.counts.clear();
441
+ this._counted.delete(lane.node);
442
+ }
417
443
  }
418
444
  /**
419
445
  * @internal
@@ -425,15 +451,21 @@ export class ContextImpl {
425
451
  */
426
452
  releaseDispatch() {
427
453
  const lane = this._lane;
428
- if (!lane || this._occurrences.get(lane.key) !== lane.occurrence + 1) {
454
+ if (!lane || lane.parent.counts.get(lane.stepId) !== lane.occurrence + 1) {
455
+ return;
456
+ }
457
+ lane.parent.counts.set(lane.stepId, lane.occurrence);
458
+ lane.node.counts.clear();
459
+ this._counted.delete(lane.node);
460
+ if (this._restored.size === 0) {
429
461
  return;
430
462
  }
431
- this._occurrences.set(lane.key, lane.occurrence);
463
+ const beneath = `${lane.node.path}/`;
432
464
  for (const key of [
433
- ...this._occurrences.keys(),
465
+ ...this._restored.keys(),
434
466
  ]) {
435
- if (key.startsWith(`${lane.path}/`)) {
436
- this._occurrences.delete(key);
467
+ if (key.startsWith(beneath)) {
468
+ this._restored.delete(key);
437
469
  }
438
470
  }
439
471
  }
@@ -446,7 +478,7 @@ export class ContextImpl {
446
478
  items: [
447
479
  ...this.itemLog.items,
448
480
  ],
449
- occurrences: Object.fromEntries(this._occurrences),
481
+ occurrences: this.liveCounts(),
450
482
  };
451
483
  }
452
484
  /**
@@ -465,7 +497,7 @@ export class ContextImpl {
465
497
  items: [
466
498
  ...this.itemLog.items,
467
499
  ],
468
- occurrences: Object.fromEntries(this._occurrences),
500
+ occurrences: this.liveCounts(),
469
501
  });
470
502
  }
471
503
  /**
@@ -484,7 +516,11 @@ export class ContextImpl {
484
516
  */
485
517
  rebase(baseline) {
486
518
  this.log.replace(baseline.items);
487
- this._occurrences.clear();
519
+ for (const node of this._counted) {
520
+ node.counts.clear();
521
+ }
522
+ this._counted.clear();
523
+ this._restored.clear();
488
524
  this.restoreOccurrences(baseline.occurrences);
489
525
  }
490
526
  /**
@@ -494,9 +530,35 @@ export class ContextImpl {
494
530
  */
495
531
  restoreOccurrences(occurrences) {
496
532
  for (const [key, count] of Object.entries(occurrences)) {
497
- this._occurrences.set(key, count);
533
+ this._restored.set(key, count);
498
534
  }
499
535
  }
536
+ /**
537
+ * Every live dispatch count, keyed `<parent path>/<step id>` — the form checkpoints
538
+ * persist and `restoreOccurrences` reads back.
539
+ */
540
+ liveCounts() {
541
+ const counts = Object.fromEntries(this._restored);
542
+ for (const node of this._counted) {
543
+ for (const [stepId, count] of node.counts) {
544
+ counts[`${node.path}/${stepId}`] = count;
545
+ }
546
+ }
547
+ return counts;
548
+ }
549
+ /** The restored count for `stepId` under `parent`, consumed on first use; 0 when none. */
550
+ takeRestored(parent, stepId) {
551
+ if (this._restored.size === 0) {
552
+ return 0;
553
+ }
554
+ const key = `${parent.path}/${stepId}`;
555
+ const count = this._restored.get(key);
556
+ if (count === undefined) {
557
+ return 0;
558
+ }
559
+ this._restored.delete(key);
560
+ return count;
561
+ }
500
562
  /**
501
563
  * @internal
502
564
  * Return a defensive copy of the current frontier. Consumed by the
@@ -512,6 +574,32 @@ export class ContextImpl {
512
574
  }));
513
575
  }
514
576
  }
577
+ /**
578
+ * @internal
579
+ * One point in the execution path tree: a context's root, or one dispatch. It
580
+ * holds the dispatch counts for steps directly beneath it, keyed by step id, and
581
+ * builds its path string only when something reads it. Deep recursion makes
582
+ * paths long — each level adds a segment and its ids grow — so building every
583
+ * dispatch's path eagerly cost time cubic in depth even when no ledger read it.
584
+ */
585
+ export class PathNode {
586
+ parent;
587
+ segment;
588
+ /** Dispatches beneath this path, by step id; the count is the next ordinal. */
589
+ counts = new Map();
590
+ text;
591
+ constructor(parent, segment) {
592
+ this.parent = parent;
593
+ this.segment = segment;
594
+ }
595
+ /** The full path: the parent's path plus this node's segment. Built once, on first read. */
596
+ get path() {
597
+ if (this.text === undefined) {
598
+ this.text = (this.parent?.path ?? '') + this.segment;
599
+ }
600
+ return this.text;
601
+ }
602
+ }
515
603
  /**
516
604
  * Items appended through one lane. Each record also feeds its parent lane's, so a
517
605
  * composite step's record holds everything appended beneath it on the same context.
@@ -556,8 +644,8 @@ function laneOf(ctx, lane) {
556
644
  };
557
645
  const view = new Proxy(target, {
558
646
  get(base, prop, receiver) {
559
- if (prop === '_path') {
560
- return lane.path;
647
+ if (prop === '_node') {
648
+ return lane.node;
561
649
  }
562
650
  if (prop === '_lane') {
563
651
  return lane;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/core",
3
- "version": "5.14.0",
3
+ "version": "5.14.1",
4
4
  "type": "module",
5
5
  "description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
6
6
  "license": "Apache-2.0",