@forwardimpact/libbridge 0.1.13 → 0.1.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forwardimpact/libbridge",
3
- "version": "0.1.13",
3
+ "version": "0.1.14",
4
4
  "description": "Threaded-channel bridge primitives — relay messages between human channels (GitHub Discussions, Microsoft Teams) and the Kata agent team.",
5
5
  "keywords": [
6
6
  "bridge",
@@ -1,6 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
 
3
3
  const DEFAULT_TTL_MS = 2 * 60 * 60 * 1000;
4
+ const DEFAULT_SWEEP_INTERVAL_MS = 10 * 60 * 1000;
4
5
 
5
6
  /**
6
7
  * In-memory registry of pending bridge → workflow callbacks. Hosts persist
@@ -17,6 +18,7 @@ export class CallbackRegistry {
17
18
  #ttlMs;
18
19
  #clock;
19
20
  #entries = new Map();
21
+ #sweepTimer = null;
20
22
 
21
23
  /**
22
24
  * @param {object} [options]
@@ -56,8 +58,20 @@ export class CallbackRegistry {
56
58
  }
57
59
 
58
60
  /**
59
- * Atomic lookup + delete. Returns null when the token is unknown or when
60
- * the supplied `tenant_id` does not match the stored binding.
61
+ * Whether an entry's TTL has elapsed; expired entries are dropped at the
62
+ * lookup that observes them so a stale token stops being a credential
63
+ * even when no sweep has run yet.
64
+ * @param {{createdAt: number}} entry
65
+ * @param {number} now
66
+ * @returns {boolean}
67
+ */
68
+ #expired(entry, now) {
69
+ return now - entry.createdAt > this.#ttlMs;
70
+ }
71
+
72
+ /**
73
+ * Atomic lookup + delete. Returns null when the token is unknown, expired,
74
+ * or when the supplied `tenant_id` does not match the stored binding.
61
75
  * @param {string} token
62
76
  * @param {{tenant_id: string}} bind
63
77
  * @returns {{correlationId: string, meta: object, createdAt: number} | null}
@@ -68,6 +82,10 @@ export class CallbackRegistry {
68
82
  }
69
83
  const entry = this.#entries.get(token);
70
84
  if (!entry) return null;
85
+ if (this.#expired(entry, this.#clock.now())) {
86
+ this.#entries.delete(token);
87
+ return null;
88
+ }
71
89
  if (entry.meta.tenant_id !== bind.tenant_id) return null;
72
90
  this.#entries.delete(token);
73
91
  return entry;
@@ -87,6 +105,10 @@ export class CallbackRegistry {
87
105
  }
88
106
  const entry = this.#entries.get(token);
89
107
  if (!entry) return null;
108
+ if (this.#expired(entry, this.#clock.now())) {
109
+ this.#entries.delete(token);
110
+ return null;
111
+ }
90
112
  if (entry.meta.tenant_id !== bind.tenant_id) return null;
91
113
  return { ...entry };
92
114
  }
@@ -102,10 +124,14 @@ export class CallbackRegistry {
102
124
  */
103
125
  tenantOf(correlationId) {
104
126
  if (typeof correlationId !== "string" || !correlationId) return null;
105
- for (const entry of this.#entries.values()) {
106
- if (entry.correlationId === correlationId) {
107
- return entry.meta.tenant_id;
127
+ const now = this.#clock.now();
128
+ for (const [token, entry] of this.#entries) {
129
+ if (entry.correlationId !== correlationId) continue;
130
+ if (this.#expired(entry, now)) {
131
+ this.#entries.delete(token);
132
+ continue;
108
133
  }
134
+ return entry.meta.tenant_id;
109
135
  }
110
136
  return null;
111
137
  }
@@ -119,11 +145,32 @@ export class CallbackRegistry {
119
145
  sweep(now = this.#clock.now()) {
120
146
  let evicted = 0;
121
147
  for (const [token, entry] of this.#entries) {
122
- if (now - entry.createdAt > this.#ttlMs) {
148
+ if (this.#expired(entry, now)) {
123
149
  this.#entries.delete(token);
124
150
  evicted++;
125
151
  }
126
152
  }
127
153
  return evicted;
128
154
  }
155
+
156
+ /**
157
+ * Start the periodic sweep so tokens whose dispatch never calls back are
158
+ * reclaimed instead of accumulating for the life of the process. Idempotent;
159
+ * the handle is unref'd so it never holds the process open.
160
+ * @param {number} [intervalMs]
161
+ */
162
+ startSweepTimer(intervalMs = DEFAULT_SWEEP_INTERVAL_MS) {
163
+ if (this.#sweepTimer) return;
164
+ this.#sweepTimer = this.#clock.setInterval(() => this.sweep(), intervalMs);
165
+ this.#sweepTimer.unref?.();
166
+ }
167
+
168
+ /**
169
+ * Stop the periodic sweep. Safe to call when no timer is running.
170
+ */
171
+ stopSweepTimer() {
172
+ if (!this.#sweepTimer) return;
173
+ this.#clock.clearInterval(this.#sweepTimer);
174
+ this.#sweepTimer = null;
175
+ }
129
176
  }