dsh-taskbridge 1.5.0 → 1.5.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.
@@ -41,7 +41,7 @@
41
41
  // The clock is injected (`now`) so tests can simulate twenty minutes of real
42
42
  // progress deterministically instead of sleeping.
43
43
 
44
- import { createHash } from 'node:crypto';
44
+ import { createHash, randomBytes } from 'node:crypto';
45
45
  import { REPEATED_FAILURE_KIND } from './diagnostics.js';
46
46
 
47
47
  /**
@@ -518,6 +518,16 @@ export class RepeatedFailureTracker {
518
518
  const threshold = Number(options.threshold);
519
519
  this.threshold = Number.isSafeInteger(threshold) && threshold > 0 ? threshold : 0;
520
520
  this.digest = typeof options.digest === 'function' ? options.digest : sha256Hex;
521
+ // A per-monitor random nonce, folded into every eventId this monitor mints.
522
+ // Two tasks whose monitors freeze the same clock, run the same tool, the same
523
+ // operation and the same failure would otherwise hash identical episode
524
+ // identities to the SAME eventId — and then one task's ack could acknowledge
525
+ // (and silence) the other task's reminder. The nonce namespaces the event
526
+ // identity per monitor; it carries no content of its own. SIXTEEN random bytes:
527
+ // the collision space of two monitors in the same process must be far larger
528
+ // than any plausible task count, and a smaller nonce would shrink it for no
529
+ // benefit — the id is 16 hex chars of digest, not of entropy.
530
+ this.nonce = randomHex16();
521
531
  /** callId -> { name, operationDigest } for calls whose result has not arrived yet. */
522
532
  this.openCalls = new Map();
523
533
  /**
@@ -604,8 +614,15 @@ export class RepeatedFailureTracker {
604
614
  this.episode.lastSeenAt = at;
605
615
  if (this.episode.reported || this.episode.count < this.threshold) return null;
606
616
  this.episode.reported = true;
617
+ // The stable event identity of THIS reminder. It is a digest of the episode's
618
+ // own hashed identity and its internal sequence — never the raw arguments,
619
+ // the error prose or the result body, which exist only as hash input. The
620
+ // receiver acknowledges THIS id, so a lost answer can be recovered by
621
+ // replaying exactly this event and an ack can be bound to it precisely.
622
+ const eventId = this.digest(`event\u0000${this.nonce}\u0000${identity}\u0000${this.episode.sequence}\u0000${this.episode.firstSeenAt}`).slice(0, 16);
607
623
  const entry = {
608
624
  kind: REPEATED_FAILURE_KIND,
625
+ eventId,
609
626
  toolName: call.name,
610
627
  failureCode: code,
611
628
  operationDigest: call.operationDigest,
@@ -736,6 +753,15 @@ function sha256Hex(value) {
736
753
  return createHash('sha256').update(value, 'utf8').digest('hex');
737
754
  }
738
755
 
756
+ /**
757
+ * One 32-hex random nonce (16 random bytes), the per-monitor namespace of every
758
+ * eventId it mints.
759
+ * @returns {string} 32 lowercase hex characters.
760
+ */
761
+ function randomHex16() {
762
+ return randomBytes(16).toString('hex');
763
+ }
764
+
739
765
  /** Why one observation wait ended. */
740
766
  export const OBSERVE_REASONS = Object.freeze({
741
767
  heartbeat: 'heartbeat',
@@ -825,13 +851,24 @@ export class TaskMonitor {
825
851
  */
826
852
  this.failures = new RepeatedFailureTracker({ threshold: options.repeatFailureThreshold });
827
853
  /**
828
- * The one failure reminder waiting to be delivered, or null.
854
+ * The one failure reminder waiting to be DELIVERED AND ACKNOWLEDGED, or null.
829
855
  *
830
856
  * A single slot, not a queue: the reminder is about the CURRENT episode, and an
831
- * undelivered older one is superseded rather than replayed.
857
+ * undelivered older one is superseded rather than replayed. Unlike a take-once
858
+ * claim, this reminder STAYS parked until a receiver acknowledges it by its
859
+ * `eventId`: the server's having written one answer out is not proof that the
860
+ * receiver ever read it, and an answer lost on the wire must be recoverable by
861
+ * the client's own retry — the retry observes the SAME event, not a heartbeat.
832
862
  * @type {object | null}
833
863
  */
834
864
  this.pendingFailure = null;
865
+ /**
866
+ * The eventId of the most recently acknowledged reminder, kept so a repeated
867
+ * (idempotent) ack of a cleared event still answers success, and so an ack
868
+ * can be checked against what this monitor actually delivered.
869
+ * @type {string | null}
870
+ */
871
+ this.ackedEventId = null;
835
872
  /** Episode identity: bumped whenever genuine activity ends the idle episode. */
836
873
  this.episode = 0;
837
874
  /** The episode the self-check already ran for, or null. */
@@ -923,15 +960,18 @@ export class TaskMonitor {
923
960
  /**
924
961
  * Take the failure reminder that is waiting to be delivered, if any.
925
962
  *
926
- * Claimed once, like the attention latch: the diagnostic reaches exactly one
927
- * observation, so a run is not woken on every subsequent identical failure. A reminder
928
- * whose episode has ended or changed since it was parked is DROPPED rather than
929
- * delivered: the run already recovered or moved on, so it is history, not news.
930
- * @returns {object | null} the diagnostic, or null.
963
+ * The reminder is NOT consumed here: it stays parked until it is acknowledged by
964
+ * its `eventId` through {@link ackFailureReport}. That is what makes a lost HTTP
965
+ * answer recoverable — the client's retry observes the same reminder again,
966
+ * because the server's having generated one answer proves nothing about what the
967
+ * receiver got. A reminder whose episode has ended or changed since it was parked
968
+ * is DROPPED rather than delivered: the run already recovered or moved on, so it
969
+ * is history, not news. Every observation returns the SAME event (same `eventId`),
970
+ * so one repeated failure is one notification identity, never several.
971
+ * @returns {object | null} the published diagnostic, or null.
931
972
  */
932
973
  claimFailureReport() {
933
974
  const report = this.pendingFailure;
934
- this.pendingFailure = null;
935
975
  if (!this.failures.isCurrent(report)) return null;
936
976
  // Published form: the internal episode sequence does not leave this module.
937
977
  const { episodeSequence, ...published } = report;
@@ -939,6 +979,47 @@ export class TaskMonitor {
939
979
  return published;
940
980
  }
941
981
 
982
+ /**
983
+ * Acknowledge one delivered failure reminder by its exact event identity.
984
+ *
985
+ * The ack is bound to what THIS monitor parked: a matching `eventId` clears the
986
+ * reminder, a second ack of the same (now cleared) event is an idempotent
987
+ * success, and anything else — an unknown id, a stale one, garbage — leaves the
988
+ * pending reminder untouched. A NEW episode keeps parking its own reminder, so
989
+ * clearing one event can never silence the next notification.
990
+ * @param {unknown} eventId - the `eventId` the receiver observed.
991
+ * @returns {{ acknowledged: boolean, eventId: string | null, reason: string | null }} ack result.
992
+ */
993
+ ackFailureReport(eventId) {
994
+ const requested = typeof eventId === 'string' && eventId !== '' ? eventId : null;
995
+ let result;
996
+ if (requested === null) {
997
+ result = { acknowledged: false, eventId: null, reason: 'event-id-required' };
998
+ } else if (requested === this.ackedEventId) {
999
+ // Idempotent: the receiver repeats an ack that already landed.
1000
+ result = { acknowledged: true, eventId: requested, reason: null };
1001
+ } else {
1002
+ const report = this.pendingFailure;
1003
+ if (report === null || report.eventId !== requested) {
1004
+ // Wrong, unknown or stale id. The pending reminder (if any) is NOT cleared:
1005
+ // a bad ack must not silence a live notification.
1006
+ result = { acknowledged: false, eventId: requested, reason: 'unknown-event' };
1007
+ } else if (!this.failures.isCurrent(report)) {
1008
+ // The episode ended before the ack arrived (recovery, or a different
1009
+ // failure took over). The reminder is dropped here — it was superseded,
1010
+ // and a late ack must not resurrect it — but it is honest that nothing
1011
+ // pending was acknowledged.
1012
+ this.pendingFailure = null;
1013
+ result = { acknowledged: false, eventId: requested, reason: 'superseded' };
1014
+ } else {
1015
+ this.pendingFailure = null;
1016
+ this.ackedEventId = requested;
1017
+ result = { acknowledged: true, eventId: requested, reason: null };
1018
+ }
1019
+ }
1020
+ return { ...result };
1021
+ }
1022
+
942
1023
  /**
943
1024
  * The instant the current idle episode began.
944
1025
  *
@@ -1114,6 +1195,7 @@ export class TaskMonitor {
1114
1195
  // a stored record or a client.
1115
1196
  reported: this.failures.reported.map((entry) => ({
1116
1197
  kind: entry.kind,
1198
+ eventId: entry.eventId,
1117
1199
  toolName: entry.toolName,
1118
1200
  failureCode: entry.failureCode,
1119
1201
  operationDigest: entry.operationDigest,
@@ -1290,6 +1372,7 @@ export class TaskMonitor {
1290
1372
  // trail stays as bounded evidence. The undelivered reminder is dropped with it.
1291
1373
  this.failures.dispose();
1292
1374
  this.pendingFailure = null;
1375
+ this.ackedEventId = null;
1293
1376
  this.releaseWaiters();
1294
1377
  }
1295
1378
  }