@noetic-tools/core 5.15.0 → 5.15.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.
@@ -233,7 +233,7 @@ export class StepLedger {
233
233
  /** Recovered entries, by path. */
234
234
  replayable = new Map();
235
235
  /** Sequence number holding each live entry, recovered or recorded, by path. */
236
- live;
236
+ live = new LiveEntries();
237
237
  /** Sequence number the next recorded entry takes. */
238
238
  seq;
239
239
  store;
@@ -250,7 +250,6 @@ export class StepLedger {
250
250
  this.executionId = opts.executionId;
251
251
  this.store = opts.store;
252
252
  this.retention = resolveStepLedgerRetention(opts.retention);
253
- this.live = new Map();
254
253
  this.seq = 0;
255
254
  if (opts.recovered) {
256
255
  this.adopt(opts.recovered);
@@ -342,7 +341,7 @@ export class StepLedger {
342
341
  await this.deleteSeq(replaced);
343
342
  }
344
343
  if (!entry.path.startsWith(REPLAY_KEY_PREFIX)) {
345
- await this.prune((path) => isBeneath(path, entry.path));
344
+ await this.prune(this.live.beneath(entry.path));
346
345
  }
347
346
  }
348
347
  /**
@@ -351,7 +350,7 @@ export class StepLedger {
351
350
  * `key:` entries stay — they are keyed by content, and a later run may ask the same.
352
351
  */
353
352
  async dropPaths() {
354
- await this.prune((path) => !path.startsWith(REPLAY_KEY_PREFIX));
353
+ await this.prune(this.live.paths().filter((path) => !path.startsWith(REPLAY_KEY_PREFIX)));
355
354
  for (const path of [
356
355
  ...this.replayable.keys(),
357
356
  ]) {
@@ -361,10 +360,9 @@ export class StepLedger {
361
360
  }
362
361
  }
363
362
  async prune(dead) {
364
- for (const [path, seq] of [
365
- ...this.live,
366
- ]) {
367
- if (!dead(path)) {
363
+ for (const path of dead) {
364
+ const seq = this.live.get(path);
365
+ if (seq === undefined) {
368
366
  continue;
369
367
  }
370
368
  this.live.delete(path);
@@ -414,13 +412,7 @@ export class StepLedger {
414
412
  if (this.live.size < this.retention.maxEntries || path.startsWith(REPLAY_KEY_PREFIX)) {
415
413
  return this.live.size;
416
414
  }
417
- let beneath = 0;
418
- for (const key of this.live.keys()) {
419
- if (isBeneath(key, path)) {
420
- beneath += 1;
421
- }
422
- }
423
- return this.live.size - beneath;
415
+ return this.live.size - this.live.beneath(path).length;
424
416
  }
425
417
  /** One warning per reason per execution — a long run would otherwise flood the log. */
426
418
  warnOnFirstDrop(count, message) {
@@ -440,4 +432,77 @@ function matches(entry, step) {
440
432
  function isBeneath(path, parent) {
441
433
  return path.startsWith(`${parent}/`) || path.startsWith(`${parent}[`);
442
434
  }
435
+ /**
436
+ * Live entries by path, with the paths also kept sorted. Everything beneath a path
437
+ * starts with `<path>/` or `<path>[`, so in sorted order each is one contiguous run,
438
+ * found by binary search. Scanning every live entry instead made each record cost the
439
+ * whole live set — in deep recursion, where the live set and the paths both grow with
440
+ * depth, that was cubic.
441
+ */
442
+ class LiveEntries {
443
+ seqs = new Map();
444
+ sorted = [];
445
+ get size() {
446
+ return this.seqs.size;
447
+ }
448
+ get(path) {
449
+ return this.seqs.get(path);
450
+ }
451
+ has(path) {
452
+ return this.seqs.has(path);
453
+ }
454
+ set(path, seq) {
455
+ if (!this.seqs.has(path)) {
456
+ this.sorted.splice(lowerBound(this.sorted, path), 0, path);
457
+ }
458
+ this.seqs.set(path, seq);
459
+ }
460
+ delete(path) {
461
+ if (!this.seqs.delete(path)) {
462
+ return;
463
+ }
464
+ this.sorted.splice(lowerBound(this.sorted, path), 1);
465
+ }
466
+ clear() {
467
+ this.seqs.clear();
468
+ this.sorted.length = 0;
469
+ }
470
+ paths() {
471
+ return [
472
+ ...this.sorted,
473
+ ];
474
+ }
475
+ /** Every live path beneath `parent`. */
476
+ beneath(parent) {
477
+ const found = [];
478
+ for (const prefix of [
479
+ `${parent}/`,
480
+ `${parent}[`,
481
+ ]) {
482
+ for (let i = lowerBound(this.sorted, prefix); i < this.sorted.length; i++) {
483
+ const path = this.sorted[i];
484
+ if (!path?.startsWith(prefix)) {
485
+ break;
486
+ }
487
+ found.push(path);
488
+ }
489
+ }
490
+ return found;
491
+ }
492
+ }
493
+ /** Index of the first element of sorted `list` not less than `value`. */
494
+ function lowerBound(list, value) {
495
+ let low = 0;
496
+ let high = list.length;
497
+ while (low < high) {
498
+ const mid = (low + high) >>> 1;
499
+ if ((list[mid] ?? '') < value) {
500
+ low = mid + 1;
501
+ }
502
+ else {
503
+ high = mid;
504
+ }
505
+ }
506
+ return low;
507
+ }
443
508
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/core",
3
- "version": "5.15.0",
3
+ "version": "5.15.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",