@hotmeshio/hotmesh 0.28.1 → 0.29.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.
@@ -7,6 +7,7 @@ const hotmesh_1 = require("../hotmesh");
7
7
  const types_1 = require("../../types");
8
8
  const stream_1 = require("../../types/stream");
9
9
  const factory_1 = require("../durable/schemas/factory");
10
+ const accumulate_1 = require("./accumulate");
10
11
  /**
11
12
  * Reserved signal-payload key carrying resolution provenance
12
13
  * (`EscalationResolution`) to the waiting workflow. The `$` prefix marks the
@@ -70,9 +71,7 @@ class EscalationClientService {
70
71
  if (!this._events?.publish)
71
72
  return;
72
73
  const ts = new Date().toISOString();
73
- const updatedAt = entry.updated_at
74
- ? (0, utils_1.formatISODate)(entry.updated_at)
75
- : ts;
74
+ const updatedAt = entry.updated_at ? (0, utils_1.formatISODate)(entry.updated_at) : ts;
76
75
  const event = {
77
76
  event_id: `${entry.id}:${verb}:${updatedAt}`,
78
77
  type: `system.escalation.${entry.id}.${verb}`,
@@ -85,10 +84,14 @@ class EscalationClientService {
85
84
  parent_id: entry.parent_id ?? undefined,
86
85
  trace_id: entry.trace_id ?? undefined,
87
86
  span_id: entry.span_id ?? undefined,
88
- ...(assignedAtCreation !== undefined ? { assigned_at_creation: assignedAtCreation } : {}),
87
+ ...(assignedAtCreation !== undefined
88
+ ? { assigned_at_creation: assignedAtCreation }
89
+ : {}),
89
90
  data: entry,
90
91
  };
91
- void Promise.resolve(this._events.publish(event)).catch(() => { });
92
+ void Promise.resolve(this._events.publish(event)).catch(() => {
93
+ /* best-effort */
94
+ });
92
95
  }
93
96
  /**
94
97
  * Fires per-row events for bulk operations. Skipped rows are not emitted.
@@ -158,7 +161,9 @@ class EscalationClientService {
158
161
  await tc.engine.signal(topic, signalPayload, types_1.StreamStatus.SUCCESS, 200);
159
162
  return true;
160
163
  }
161
- catch { /* topic not currently registered — fall through */ }
164
+ catch {
165
+ /* topic not currently registered — fall through */
166
+ }
162
167
  }
163
168
  let delivered = false;
164
169
  try {
@@ -391,7 +396,7 @@ class EscalationClientService {
391
396
  * pre-assignments (no window) resolve normally.
392
397
  */
393
398
  async resolve(params, namespace) {
394
- const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
399
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
395
400
  const hm = await this._engine(null, ns);
396
401
  const store = hm.engine.store;
397
402
  //pre-build the wake so it commits INSIDE the resolve transaction; the
@@ -401,15 +406,21 @@ class EscalationClientService {
401
406
  if (preview?.signal_key) {
402
407
  wakeCommand = await this._buildWakeCommand(ns, preview.topic, preview.signal_key, this._signalData(params.resolverPayload, params.id, params.resolvedBy));
403
408
  }
404
- const dbResult = await store.resolveEscalation({ id: params.id, resolverPayload: params.resolverPayload, metadata: params.metadata, assertClaim: params.assertClaim }, wakeCommand ?? undefined);
409
+ const dbResult = await store.resolveEscalation({
410
+ id: params.id,
411
+ resolverPayload: params.resolverPayload,
412
+ metadata: params.metadata,
413
+ assertClaim: params.assertClaim,
414
+ }, wakeCommand ?? undefined, this._batchResolutionJson(params.id, params.resolvedBy));
405
415
  if (!dbResult.ok)
406
416
  return dbResult;
407
417
  if (dbResult.signalKey && !dbResult.wakeEnqueued) {
408
418
  //the wake was not part of the commit (no hook rule found, or the
409
- //enqueue was rolled back to its savepoint) — deliver post-commit
419
+ //enqueue was rolled back to its savepoint) — deliver post-commit from
420
+ //the committed payload (an accumulator row stores its collection)
410
421
  await this._deliverEscalationSignal(ns, dbResult.topic, {
411
422
  id: dbResult.signalKey,
412
- data: this._signalData(params.resolverPayload, params.id, params.resolvedBy),
423
+ data: this._signalData(dbResult.entry.resolver_payload ?? {}, params.id, params.resolvedBy),
413
424
  });
414
425
  }
415
426
  this._emit('resolved', dbResult.entry);
@@ -424,7 +435,7 @@ class EscalationClientService {
424
435
  * GIN-indexed `metadata` in the same atomic UPDATE. See {@link resolve}.
425
436
  */
426
437
  async resolveByMetadata(params, namespace) {
427
- const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
438
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
428
439
  const hm = await this._engine(null, ns);
429
440
  const store = hm.engine.store;
430
441
  //peek the row the resolve is expected to lock and pre-build its wake;
@@ -442,13 +453,19 @@ class EscalationClientService {
442
453
  //delivery where the winner's own id is used
443
454
  wakeCommand = await this._buildWakeCommand(ns, preview.topic, preview.signalKey, this._signalData(params.resolverPayload, preview.id, params.resolvedBy));
444
455
  }
445
- const dbResult = await store.resolveEscalationByMetadata({ key: params.key, value: params.value, resolverPayload: params.resolverPayload, roles: params.roles, metadata: params.metadata }, wakeCommand ?? undefined);
456
+ const dbResult = await store.resolveEscalationByMetadata({
457
+ key: params.key,
458
+ value: params.value,
459
+ resolverPayload: params.resolverPayload,
460
+ roles: params.roles,
461
+ metadata: params.metadata,
462
+ }, wakeCommand ?? undefined, preview ? this._batchResolutionJson(preview.id, params.resolvedBy) : null);
446
463
  if (!dbResult.ok)
447
464
  return dbResult;
448
465
  if (dbResult.signalKey && !dbResult.wakeEnqueued) {
449
466
  await this._deliverEscalationSignal(ns, dbResult.topic, {
450
467
  id: dbResult.signalKey,
451
- data: this._signalData(params.resolverPayload, dbResult.entry.id, params.resolvedBy),
468
+ data: this._signalData(dbResult.entry.resolver_payload ?? {}, dbResult.entry.id, params.resolvedBy),
452
469
  });
453
470
  }
454
471
  this._emit('resolved', dbResult.entry);
@@ -491,7 +508,12 @@ class EscalationClientService {
491
508
  else {
492
509
  this._emit('batch-item', dbResult.entry);
493
510
  }
494
- return { ok: true, outcome: dbResult.outcome, remaining: dbResult.remaining, entry: dbResult.entry };
511
+ return {
512
+ ok: true,
513
+ outcome: dbResult.outcome,
514
+ remaining: dbResult.remaining,
515
+ entry: dbResult.entry,
516
+ };
495
517
  }
496
518
  /**
497
519
  * Fills ONE declared item of a batch escalation (a wait created with
@@ -516,7 +538,7 @@ class EscalationClientService {
516
538
  * webhook retries.
517
539
  */
518
540
  async resolveBatchItem(params, namespace) {
519
- const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
541
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
520
542
  const hm = await this._engine(null, ns);
521
543
  const store = hm.engine.store;
522
544
  //pre-build the wake with a placeholder payload — the store overwrites
@@ -539,7 +561,7 @@ class EscalationClientService {
539
561
  * for the fill contract.
540
562
  */
541
563
  async resolveBatchItemByMetadata(params, namespace) {
542
- const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
564
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
543
565
  const hm = await this._engine(null, ns);
544
566
  const store = hm.engine.store;
545
567
  //peek the row the fill is expected to lock; forSignalKey pins the wake
@@ -558,12 +580,191 @@ class EscalationClientService {
558
580
  const dbResult = await store.resolveEscalationBatchItemByMetadata(params, wakeCommand ?? undefined, preview ? this._batchResolutionJson(preview.id, params.resolvedBy) : null);
559
581
  return this._settleBatchItemResult(ns, dbResult, params.resolvedBy);
560
582
  }
583
+ // ─── Open accumulation ──────────────────────────────────────────────────────
584
+ async _previewAccumulateRow(store, selector, namespace) {
585
+ if (selector.id) {
586
+ const row = await store.getEscalation(selector.id, namespace);
587
+ return row
588
+ ? { id: row.id, signalKey: row.signal_key, topic: row.topic }
589
+ : null;
590
+ }
591
+ if (selector.signalKey) {
592
+ const row = await store.getEscalationBySignalKey(selector.signalKey, namespace);
593
+ return row
594
+ ? { id: row.id, signalKey: row.signal_key, topic: row.topic }
595
+ : null;
596
+ }
597
+ if (selector.key !== undefined) {
598
+ const peek = await store.peekEscalationByMetadata({
599
+ key: selector.key,
600
+ value: selector.value,
601
+ roles: selector.roles,
602
+ namespace,
603
+ });
604
+ return peek
605
+ ? { id: peek.id, signalKey: peek.signalKey, topic: peek.topic }
606
+ : null;
607
+ }
608
+ return null;
609
+ }
610
+ /**
611
+ * Pre-builds one side's wake with a placeholder payload; the store
612
+ * rewrites the `{data,data}` slot from the committed collection inside
613
+ * the add statement, so a completing side's wake commits WITH the add.
614
+ */
615
+ async _accumulateWake(ns, preview, resolvedBy) {
616
+ if (!preview?.signalKey)
617
+ return null;
618
+ const command = await this._buildWakeCommand(ns, preview.topic, preview.signalKey, {});
619
+ if (!command)
620
+ return null;
621
+ return {
622
+ command,
623
+ resolutionJson: this._batchResolutionJson(preview.id, resolvedBy),
624
+ };
625
+ }
626
+ _assertAccumulatePatch(metadata) {
627
+ if (!metadata)
628
+ return;
629
+ for (const reserved of accumulate_1.ACCUMULATE_RESERVED_METADATA_KEYS) {
630
+ if (reserved in metadata) {
631
+ throw new Error(`metadata key '${reserved}' is reserved for accumulate state`);
632
+ }
633
+ }
634
+ }
635
+ /**
636
+ * Shared post-statement handling for both add forms: post-commit wake
637
+ * fallback per completed side (the committed `resolver_payload` is the
638
+ * recovery record) and lifecycle events (`accumulated` on an interim
639
+ * add, `resolved` on completion) for the container and the reciprocal.
640
+ */
641
+ async _settleAccumulateResult(ns, dbResult, resolvedBy) {
642
+ if (!dbResult.ok)
643
+ return dbResult;
644
+ const { wake, ...result } = dbResult;
645
+ const settleSide = async (entry, side) => {
646
+ if (entry.status !== 'resolved') {
647
+ this._emit('accumulated', entry);
648
+ return;
649
+ }
650
+ if (entry.signal_key && !side?.wakeEnqueued) {
651
+ await this._deliverEscalationSignal(ns, entry.topic, {
652
+ id: entry.signal_key,
653
+ data: this._signalData(entry.resolver_payload ?? {}, entry.id, resolvedBy),
654
+ });
655
+ }
656
+ this._emit('resolved', entry);
657
+ };
658
+ await settleSide(result.entry, wake?.primary);
659
+ if (result.reciprocal)
660
+ await settleSide(result.reciprocal.entry, wake?.reciprocal);
661
+ return result;
662
+ }
663
+ async _accumulateWakes(ns, store, primary, reciprocal, namespace, resolvedBy) {
664
+ const wakes = [];
665
+ const first = await this._accumulateWake(ns, await this._previewAccumulateRow(store, primary, namespace), resolvedBy);
666
+ if (first)
667
+ wakes.push(first);
668
+ if (reciprocal) {
669
+ const second = await this._accumulateWake(ns, await this._previewAccumulateRow(store, reciprocal, namespace), resolvedBy);
670
+ if (second)
671
+ wakes.push(second);
672
+ }
673
+ return wakes;
674
+ }
675
+ /**
676
+ * Adds ONE item to an accumulator escalation (a wait created with
677
+ * `condition(signalId, { accumulate: {...} })` or a standalone `create()`
678
+ * with `accumulate`). One atomic statement: the entry lands under
679
+ * `envelope.accumulate_items[itemKey]` with a database-clock `at`, the
680
+ * `accumulate_keys` / `accumulate_count` facets recompute, and, when the
681
+ * add reaches `max` with `resolveAtMax`, the row resolves with the
682
+ * ordered collection (`{ $accumulated, $trigger: 'count' }`) as its
683
+ * `resolver_payload` and the waiting workflow's wake commits WITH the
684
+ * add. The caller that completes the row learns it from
685
+ * `outcome: 'completed'`.
686
+ *
687
+ * Pass `reciprocal` to write a second accumulator row in the same
688
+ * statement, both or neither: the reciprocal holds the container's id as
689
+ * its item key, each entry carries the other row's id as `reciprocalId`,
690
+ * and a member row declared `accumulate: { max: 1 }` completes and wakes
691
+ * here too. A blocked reciprocal leaves the container untouched and
692
+ * answers `reciprocal-*`.
693
+ *
694
+ * Adds are claim-agnostic by default; pass `assertClaim` for the same
695
+ * claim-lock assertion as `resolve()` on the container row. Pass
696
+ * `metadata` to merge an outcome patch into the container's GIN-indexed
697
+ * metadata in the same UPDATE (reserved accumulate keys are rejected).
698
+ * A repeated key answers `duplicate-item` without touching the row
699
+ * unless the accumulator was declared `unique: false`, which replaces
700
+ * the entry in place.
701
+ */
702
+ async accumulateItem(params, namespace) {
703
+ if ((params.id ? 1 : 0) + (params.signalKey ? 1 : 0) !== 1) {
704
+ throw new Error('accumulateItem requires exactly one of `id` or `signalKey`');
705
+ }
706
+ (0, accumulate_1.assertAccumulateItemKey)(params.itemKey);
707
+ this._assertAccumulatePatch(params.metadata);
708
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
709
+ const hm = await this._engine(null, ns);
710
+ const store = hm.engine.store;
711
+ const wakes = await this._accumulateWakes(ns, store, params, params.reciprocal, params.namespace, params.resolvedBy);
712
+ const dbResult = await store.accumulateEscalationItem(params, wakes);
713
+ return this._settleAccumulateResult(ns, dbResult, params.resolvedBy);
714
+ }
715
+ /**
716
+ * Add selecting the container by metadata facet: the highest priority
717
+ * pending row whose `metadata` contains the key/value, mirroring
718
+ * `resolveByMetadata()`'s selector. See {@link accumulateItem} for the
719
+ * add contract.
720
+ */
721
+ async accumulateItemByMetadata(params, namespace) {
722
+ (0, accumulate_1.assertAccumulateItemKey)(params.itemKey);
723
+ this._assertAccumulatePatch(params.metadata);
724
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
725
+ const hm = await this._engine(null, ns);
726
+ const store = hm.engine.store;
727
+ const wakes = await this._accumulateWakes(ns, store, params, params.reciprocal, params.namespace, params.resolvedBy);
728
+ const dbResult = await store.accumulateEscalationItemByMetadata(params, wakes);
729
+ return this._settleAccumulateResult(ns, dbResult, params.resolvedBy);
730
+ }
731
+ _settleRemoveResult(dbResult) {
732
+ if (!dbResult.ok)
733
+ return dbResult;
734
+ this._emit('removed', dbResult.entry);
735
+ if (dbResult.reciprocal)
736
+ this._emit('removed', dbResult.reciprocal.entry);
737
+ return dbResult;
738
+ }
739
+ /**
740
+ * Removes ONE held item from a pending accumulator escalation in one
741
+ * guarded statement: the entry leaves `accumulate_items`, the facets
742
+ * recompute, the row stays `pending`, and the waiter is never woken.
743
+ * Pass `reciprocal` to also remove the container's id from that row,
744
+ * both or neither. A key that is not held answers `item-absent`.
745
+ */
746
+ async removeAccumulatedItem(params, namespace) {
747
+ if ((params.id ? 1 : 0) + (params.signalKey ? 1 : 0) !== 1) {
748
+ throw new Error('removeAccumulatedItem requires exactly one of `id` or `signalKey`');
749
+ }
750
+ (0, accumulate_1.assertAccumulateItemKey)(params.itemKey);
751
+ const hm = await this._engine(null, params.namespace ?? namespace ?? factory_1.APP_ID);
752
+ const dbResult = await hm.engine.store.removeAccumulatedEscalationItem(params);
753
+ return this._settleRemoveResult(dbResult);
754
+ }
755
+ /** Removal selecting the container by metadata facet. See {@link removeAccumulatedItem}. */
756
+ async removeAccumulatedItemByMetadata(params, namespace) {
757
+ (0, accumulate_1.assertAccumulateItemKey)(params.itemKey);
758
+ const hm = await this._engine(null, params.namespace ?? namespace ?? factory_1.APP_ID);
759
+ const dbResult = await hm.engine.store.removeAccumulatedEscalationItemByMetadata(params);
760
+ return this._settleRemoveResult(dbResult);
761
+ }
561
762
  /**
562
763
  * Full-fidelity migration: inserts an escalation row preserving the original
563
764
  * UUID and lifecycle state. Returns `null` on duplicate (idempotent).
564
765
  */
565
766
  async migrate(params, namespace) {
566
- const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
767
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
567
768
  const hm = await this._engine(null, ns);
568
769
  return hm.engine.store.createEscalationForMigration(params);
569
770
  }
@@ -657,7 +858,7 @@ class EscalationClientService {
657
858
  * `items` array returns `ok: true` with no entries.
658
859
  */
659
860
  async resolveAllOrNone(params, namespace) {
660
- const ns = (params.namespace ?? namespace) ?? factory_1.APP_ID;
861
+ const ns = params.namespace ?? namespace ?? factory_1.APP_ID;
661
862
  const items = params.items ?? [];
662
863
  if (!items.length)
663
864
  return { ok: true, entries: [] };
@@ -676,29 +877,33 @@ class EscalationClientService {
676
877
  namespace: params.namespace,
677
878
  });
678
879
  const wakeCommands = [];
880
+ const resolutionJsons = [];
679
881
  for (const row of previews) {
680
882
  if (!row.signal_key)
681
883
  continue;
682
884
  const cmd = await this._buildWakeCommand(ns, row.topic, row.signal_key, this._signalData(payloadById.get(row.id), row.id, params.resolvedBy));
683
- if (cmd)
885
+ if (cmd) {
684
886
  wakeCommands.push(cmd);
887
+ resolutionJsons.push(this._batchResolutionJson(row.id, params.resolvedBy));
888
+ }
685
889
  }
686
890
  const dbResult = await store.resolveAllOrNoneEscalations({
687
891
  items,
688
892
  namespace: params.namespace,
689
893
  metadata: params.metadata,
690
894
  assertAssignee: params.assertAssignee,
691
- }, wakeCommands);
895
+ }, wakeCommands, resolutionJsons);
692
896
  if (!dbResult.ok)
693
897
  return { ok: false, failed: dbResult.failed };
694
898
  //rows whose wake was not part of the commit (no hook rule deployed, or
695
- //signal routing enriched after the preview) — deliver post-commit
899
+ //signal routing enriched after the preview) — deliver post-commit from
900
+ //the committed payload (an accumulator row stores its collection)
696
901
  const enqueuedKeys = new Set(wakeCommands.map((w) => w.forSignalKey));
697
902
  for (const entry of dbResult.entries) {
698
903
  if (entry.signal_key && !enqueuedKeys.has(entry.signal_key)) {
699
904
  await this._deliverEscalationSignal(ns, entry.topic, {
700
905
  id: entry.signal_key,
701
- data: this._signalData(payloadById.get(entry.id), entry.id, params.resolvedBy),
906
+ data: this._signalData(entry.resolver_payload ?? {}, entry.id, params.resolvedBy),
702
907
  });
703
908
  }
704
909
  }
@@ -0,0 +1,16 @@
1
+ import { AccumulateConfig } from '../../types/hmsh_escalations';
2
+ type Foldable = {
3
+ batch?: string[];
4
+ partialOnTimeout?: boolean;
5
+ accumulate?: AccumulateConfig;
6
+ metadata?: Record<string, unknown>;
7
+ envelope?: Record<string, unknown>;
8
+ };
9
+ /**
10
+ * Folds whichever accumulator declaration a creation config carries
11
+ * (`batch` or `accumulate`) into its storage shape, and passes every other
12
+ * config through untouched. The single entry point for both the
13
+ * `condition()` path and standalone `create()`.
14
+ */
15
+ export declare function foldEscalationConfig<T extends Foldable>(config: T): Omit<T, 'batch' | 'partialOnTimeout' | 'accumulate'>;
16
+ export {};
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.foldEscalationConfig = void 0;
4
+ const accumulate_1 = require("./accumulate");
5
+ const batch_1 = require("./batch");
6
+ /**
7
+ * Folds whichever accumulator declaration a creation config carries
8
+ * (`batch` or `accumulate`) into its storage shape, and passes every other
9
+ * config through untouched. The single entry point for both the
10
+ * `condition()` path and standalone `create()`.
11
+ */
12
+ function foldEscalationConfig(config) {
13
+ const { batch, partialOnTimeout, accumulate, ...rest } = config;
14
+ if (batch) {
15
+ if (accumulate) {
16
+ throw new Error('accumulate and batch are mutually exclusive');
17
+ }
18
+ return (0, batch_1.foldBatchConfig)({ ...rest, batch, partialOnTimeout });
19
+ }
20
+ if (accumulate) {
21
+ return (0, accumulate_1.foldAccumulateConfig)({ ...rest, accumulate });
22
+ }
23
+ if (partialOnTimeout !== undefined) {
24
+ throw new Error('partialOnTimeout requires a batch declaration');
25
+ }
26
+ return rest;
27
+ }
28
+ exports.foldEscalationConfig = foldEscalationConfig;
@@ -66,4 +66,6 @@ export { EscalationsClass as Escalations };
66
66
  export { EscalationClientService };
67
67
  export { ESCALATION_RESOLUTION_KEY } from './client';
68
68
  export { foldBatchConfig } from './batch';
69
- export { ESCALATION_BATCH_PENDING_KEY, ESCALATION_BATCH_COUNT_KEY, ESCALATION_BATCH_KEYS_KEY, ESCALATION_BATCH_ITEMS_KEY, ESCALATION_BATCH_FILLED_AT_KEY, ESCALATION_BATCH_ITEM_KEY_MAX_LENGTH, } from '../../types/hmsh_escalations';
69
+ export { foldAccumulateConfig, assertAccumulateItemKey } from './accumulate';
70
+ export { foldEscalationConfig } from './fold';
71
+ export { ESCALATION_BATCH_PENDING_KEY, ESCALATION_BATCH_COUNT_KEY, ESCALATION_BATCH_KEYS_KEY, ESCALATION_BATCH_ITEMS_KEY, ESCALATION_BATCH_FILLED_AT_KEY, ESCALATION_BATCH_PARTIAL_ON_TIMEOUT_KEY, ESCALATION_BATCH_ITEM_KEY_MAX_LENGTH, ESCALATION_ACCUMULATE_COUNT_KEY, ESCALATION_ACCUMULATE_MAX_KEY, ESCALATION_ACCUMULATE_KEYS_KEY, ESCALATION_ACCUMULATE_ITEMS_KEY, ESCALATION_ACCUMULATE_CONFIG_KEY, ESCALATION_ACCUMULATED_KEY, ESCALATION_TRIGGER_KEY, } from '../../types/hmsh_escalations';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ESCALATION_BATCH_ITEM_KEY_MAX_LENGTH = exports.ESCALATION_BATCH_FILLED_AT_KEY = exports.ESCALATION_BATCH_ITEMS_KEY = exports.ESCALATION_BATCH_KEYS_KEY = exports.ESCALATION_BATCH_COUNT_KEY = exports.ESCALATION_BATCH_PENDING_KEY = exports.foldBatchConfig = exports.ESCALATION_RESOLUTION_KEY = exports.EscalationClientService = exports.Escalations = void 0;
3
+ exports.ESCALATION_TRIGGER_KEY = exports.ESCALATION_ACCUMULATED_KEY = exports.ESCALATION_ACCUMULATE_CONFIG_KEY = exports.ESCALATION_ACCUMULATE_ITEMS_KEY = exports.ESCALATION_ACCUMULATE_KEYS_KEY = exports.ESCALATION_ACCUMULATE_MAX_KEY = exports.ESCALATION_ACCUMULATE_COUNT_KEY = exports.ESCALATION_BATCH_ITEM_KEY_MAX_LENGTH = exports.ESCALATION_BATCH_PARTIAL_ON_TIMEOUT_KEY = exports.ESCALATION_BATCH_FILLED_AT_KEY = exports.ESCALATION_BATCH_ITEMS_KEY = exports.ESCALATION_BATCH_KEYS_KEY = exports.ESCALATION_BATCH_COUNT_KEY = exports.ESCALATION_BATCH_PENDING_KEY = exports.foldEscalationConfig = exports.assertAccumulateItemKey = exports.foldAccumulateConfig = exports.foldBatchConfig = exports.ESCALATION_RESOLUTION_KEY = exports.EscalationClientService = exports.Escalations = void 0;
4
4
  const utils_1 = require("../../modules/utils");
5
5
  const client_1 = require("./client");
6
6
  Object.defineProperty(exports, "EscalationClientService", { enumerable: true, get: function () { return client_1.EscalationClientService; } });
@@ -73,10 +73,23 @@ var client_2 = require("./client");
73
73
  Object.defineProperty(exports, "ESCALATION_RESOLUTION_KEY", { enumerable: true, get: function () { return client_2.ESCALATION_RESOLUTION_KEY; } });
74
74
  var batch_1 = require("./batch");
75
75
  Object.defineProperty(exports, "foldBatchConfig", { enumerable: true, get: function () { return batch_1.foldBatchConfig; } });
76
+ var accumulate_1 = require("./accumulate");
77
+ Object.defineProperty(exports, "foldAccumulateConfig", { enumerable: true, get: function () { return accumulate_1.foldAccumulateConfig; } });
78
+ Object.defineProperty(exports, "assertAccumulateItemKey", { enumerable: true, get: function () { return accumulate_1.assertAccumulateItemKey; } });
79
+ var fold_1 = require("./fold");
80
+ Object.defineProperty(exports, "foldEscalationConfig", { enumerable: true, get: function () { return fold_1.foldEscalationConfig; } });
76
81
  var hmsh_escalations_1 = require("../../types/hmsh_escalations");
77
82
  Object.defineProperty(exports, "ESCALATION_BATCH_PENDING_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_BATCH_PENDING_KEY; } });
78
83
  Object.defineProperty(exports, "ESCALATION_BATCH_COUNT_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_BATCH_COUNT_KEY; } });
79
84
  Object.defineProperty(exports, "ESCALATION_BATCH_KEYS_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_BATCH_KEYS_KEY; } });
80
85
  Object.defineProperty(exports, "ESCALATION_BATCH_ITEMS_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_BATCH_ITEMS_KEY; } });
81
86
  Object.defineProperty(exports, "ESCALATION_BATCH_FILLED_AT_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_BATCH_FILLED_AT_KEY; } });
87
+ Object.defineProperty(exports, "ESCALATION_BATCH_PARTIAL_ON_TIMEOUT_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_BATCH_PARTIAL_ON_TIMEOUT_KEY; } });
82
88
  Object.defineProperty(exports, "ESCALATION_BATCH_ITEM_KEY_MAX_LENGTH", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_BATCH_ITEM_KEY_MAX_LENGTH; } });
89
+ Object.defineProperty(exports, "ESCALATION_ACCUMULATE_COUNT_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_ACCUMULATE_COUNT_KEY; } });
90
+ Object.defineProperty(exports, "ESCALATION_ACCUMULATE_MAX_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_ACCUMULATE_MAX_KEY; } });
91
+ Object.defineProperty(exports, "ESCALATION_ACCUMULATE_KEYS_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_ACCUMULATE_KEYS_KEY; } });
92
+ Object.defineProperty(exports, "ESCALATION_ACCUMULATE_ITEMS_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_ACCUMULATE_ITEMS_KEY; } });
93
+ Object.defineProperty(exports, "ESCALATION_ACCUMULATE_CONFIG_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_ACCUMULATE_CONFIG_KEY; } });
94
+ Object.defineProperty(exports, "ESCALATION_ACCUMULATED_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_ACCUMULATED_KEY; } });
95
+ Object.defineProperty(exports, "ESCALATION_TRIGGER_KEY", { enumerable: true, get: function () { return hmsh_escalations_1.ESCALATION_TRIGGER_KEY; } });
@@ -0,0 +1,96 @@
1
+ import { AccumulatorTrigger, EscalationWakeCommand } from '../../../../types/hmsh_escalations';
2
+ /**
3
+ * SQL builders for the accumulator store operations. Every builder is a
4
+ * pure function of parameter positions; the provider owns the connection
5
+ * and the result mapping. Two rows (container and reciprocal) are locked
6
+ * in one ordered CTE and written by one UPDATE gated on both being
7
+ * eligible, so a reciprocal add is both-or-neither by construction.
8
+ */
9
+ /** Collects statement parameters and hands back their `$n` placeholders. */
10
+ export declare class SqlParams {
11
+ readonly values: unknown[];
12
+ add(value: unknown): string;
13
+ }
14
+ export type AccumulateRowSelector = {
15
+ kind: 'id';
16
+ id: string;
17
+ } | {
18
+ kind: 'signalKey';
19
+ signalKey: string;
20
+ } | {
21
+ kind: 'metadata';
22
+ key: string;
23
+ value: unknown;
24
+ roles?: string[];
25
+ };
26
+ export interface AccumulateWake {
27
+ command: EscalationWakeCommand;
28
+ /** Pre-serialized `$resolution` object merged into the delivered
29
+ * collection only, never into the stored `resolver_payload`. */
30
+ resolutionJson: string | null;
31
+ }
32
+ /** True when the row's envelope carries an accumulator item store. */
33
+ export declare function isAccumulatorSql(envelopeExpr: string): string;
34
+ /**
35
+ * The delivered collection for an item store expression: `$accumulated`
36
+ * ordered by each entry's database-clock `at`, plus the `$trigger` that
37
+ * ended the wait.
38
+ */
39
+ export declare function accumulatedCollectionSql(itemsExpr: string, trigger: AccumulatorTrigger): string;
40
+ /**
41
+ * The stored `resolver_payload` for a manual resolve: the collection merged
42
+ * with the resolver's payload on an accumulator row, the payload alone on
43
+ * any other row.
44
+ */
45
+ export declare function resolvePayloadSql(rowAlias: string, payloadParam: string): string;
46
+ /**
47
+ * The `resolver_payload` written by the timeout path: an accumulator
48
+ * delivers its collection, a batch row with `partialOnTimeout` delivers the
49
+ * items filled so far, every other row keeps its column untouched.
50
+ */
51
+ export declare function timeoutPayloadSql(rowAlias: string): string;
52
+ /**
53
+ * The wake message for a resolve statement: on an accumulator row the
54
+ * `{data,data}` slot is rewritten from the committed `resolver_payload`
55
+ * (plus `$resolution`), so the waiter receives exactly what the row stores.
56
+ */
57
+ export declare function resolveWakeMessageSql(fromCTE: string, messageParam: string, resolutionParam: string): string;
58
+ export interface AccumulateStatementInput {
59
+ primary: AccumulateRowSelector;
60
+ reciprocal: AccumulateRowSelector | null;
61
+ namespace?: string;
62
+ itemKey: string;
63
+ payload: Record<string, unknown> | null;
64
+ reciprocalPayload: Record<string, unknown> | null;
65
+ metadata: Record<string, unknown> | null;
66
+ actor: string | null;
67
+ assertClaim: string | null;
68
+ wakes: AccumulateWake[];
69
+ appId: string;
70
+ schemaName: string;
71
+ }
72
+ /**
73
+ * One statement: guarded append of one item on the container (and the
74
+ * container's id on the reciprocal), facet recompute, resolve-at-max with
75
+ * the ordered collection as `resolver_payload`, per-side wake enqueue, and
76
+ * per-side outcome classification. Both writes or neither.
77
+ */
78
+ export declare function buildAccumulateStatement(input: AccumulateStatementInput): {
79
+ sql: string;
80
+ values: unknown[];
81
+ };
82
+ export interface RemoveStatementInput {
83
+ primary: AccumulateRowSelector;
84
+ reciprocal: AccumulateRowSelector | null;
85
+ namespace?: string;
86
+ itemKey: string;
87
+ }
88
+ /**
89
+ * One statement: guarded removal of one held item from the container (and
90
+ * of the container's id from the reciprocal), facet recompute, no wake and
91
+ * no status change. Both writes or neither.
92
+ */
93
+ export declare function buildRemoveStatement(input: RemoveStatementInput): {
94
+ sql: string;
95
+ values: unknown[];
96
+ };