chainlesschain 0.143.0 → 0.145.0

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.
Files changed (133) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +157 -27
  4. package/src/commands/agent.js +587 -103
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +152 -27
  7. package/src/commands/bm25.js +111 -27
  8. package/src/commands/browse.js +190 -29
  9. package/src/commands/ccron.js +128 -28
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +414 -34
  12. package/src/commands/compt.js +127 -29
  13. package/src/commands/consol.js +8 -2
  14. package/src/commands/cowork.js +580 -34
  15. package/src/commands/crosschain.js +182 -28
  16. package/src/commands/dao.js +182 -28
  17. package/src/commands/dlp.js +172 -27
  18. package/src/commands/economy.js +205 -50
  19. package/src/commands/evolution.js +203 -50
  20. package/src/commands/evomap.js +182 -27
  21. package/src/commands/fflag.js +74 -22
  22. package/src/commands/git.js +177 -37
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +204 -50
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +170 -34
  27. package/src/commands/itbudget.js +149 -33
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +195 -38
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +179 -27
  32. package/src/commands/mcpscaf.js +145 -33
  33. package/src/commands/meminj.js +145 -33
  34. package/src/commands/nostr.js +178 -27
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +144 -33
  37. package/src/commands/pdfp.js +109 -27
  38. package/src/commands/perf.js +169 -32
  39. package/src/commands/perm.js +144 -33
  40. package/src/commands/pipeline.js +207 -52
  41. package/src/commands/planmode.js +141 -32
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +111 -27
  44. package/src/commands/recommend.js +177 -34
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +145 -33
  48. package/src/commands/seshsearch.js +141 -33
  49. package/src/commands/seshtail.js +144 -33
  50. package/src/commands/seshu.js +152 -33
  51. package/src/commands/sganal.js +123 -29
  52. package/src/commands/siem.js +201 -34
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +146 -33
  55. package/src/commands/social.js +157 -32
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +145 -33
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +171 -33
  60. package/src/commands/topiccls.js +146 -33
  61. package/src/commands/uprof.js +141 -32
  62. package/src/commands/vcheck.js +122 -28
  63. package/src/commands/webfetch.js +141 -32
  64. package/src/commands/zkp.js +184 -28
  65. package/src/index.js +40 -0
  66. package/src/lib/a2a-protocol.js +319 -51
  67. package/src/lib/activitypub-bridge.js +288 -50
  68. package/src/lib/agent-economy.js +304 -51
  69. package/src/lib/app-builder.js +279 -46
  70. package/src/lib/audit-logger.js +321 -0
  71. package/src/lib/autonomous-agent.js +284 -48
  72. package/src/lib/bi-engine.js +283 -49
  73. package/src/lib/bm25-search.js +301 -49
  74. package/src/lib/browser-automation.js +296 -49
  75. package/src/lib/chat-core.js +336 -0
  76. package/src/lib/claude-code-bridge.js +341 -0
  77. package/src/lib/compliance-framework-reporter.js +305 -51
  78. package/src/lib/compliance-manager.js +330 -0
  79. package/src/lib/compression-telemetry.js +301 -49
  80. package/src/lib/content-recommender.js +317 -52
  81. package/src/lib/cowork-cron.js +298 -49
  82. package/src/lib/cowork-learning.js +333 -0
  83. package/src/lib/cowork-task-runner.js +308 -51
  84. package/src/lib/cowork-workflow.js +327 -0
  85. package/src/lib/cross-chain.js +311 -51
  86. package/src/lib/dao-governance.js +280 -46
  87. package/src/lib/dlp-engine.js +287 -49
  88. package/src/lib/evolution-system.js +278 -47
  89. package/src/lib/evomap-manager.js +280 -46
  90. package/src/lib/execution-backend.js +294 -48
  91. package/src/lib/feature-flags.js +294 -49
  92. package/src/lib/git-integration.js +285 -47
  93. package/src/lib/hardening-manager.js +341 -0
  94. package/src/lib/hierarchical-memory.js +284 -48
  95. package/src/lib/inference-network.js +308 -51
  96. package/src/lib/iteration-budget.js +302 -50
  97. package/src/lib/knowledge-graph.js +333 -0
  98. package/src/lib/matrix-bridge.js +281 -47
  99. package/src/lib/mcp-scaffold.js +318 -54
  100. package/src/lib/memory-injection.js +288 -49
  101. package/src/lib/nostr-bridge.js +286 -49
  102. package/src/lib/orchestrator.js +293 -48
  103. package/src/lib/pdf-parser.js +298 -49
  104. package/src/lib/perf-tuning.js +309 -50
  105. package/src/lib/permission-engine.js +287 -49
  106. package/src/lib/pipeline-orchestrator.js +289 -49
  107. package/src/lib/plan-mode.js +298 -51
  108. package/src/lib/privacy-computing.js +335 -0
  109. package/src/lib/reputation-optimizer.js +340 -0
  110. package/src/lib/sandbox-v2.js +327 -0
  111. package/src/lib/service-container.js +313 -52
  112. package/src/lib/session-consolidator.js +296 -49
  113. package/src/lib/session-hooks.js +312 -53
  114. package/src/lib/session-search.js +304 -51
  115. package/src/lib/session-tail.js +288 -49
  116. package/src/lib/session-usage.js +298 -52
  117. package/src/lib/siem-exporter.js +298 -51
  118. package/src/lib/skill-marketplace.js +345 -0
  119. package/src/lib/sla-manager.js +341 -0
  120. package/src/lib/slot-filler.js +303 -51
  121. package/src/lib/social-graph-analytics.js +295 -49
  122. package/src/lib/social-graph.js +272 -49
  123. package/src/lib/stress-tester.js +342 -0
  124. package/src/lib/sub-agent-registry.js +302 -53
  125. package/src/lib/task-model-selector.js +302 -50
  126. package/src/lib/terraform-manager.js +333 -0
  127. package/src/lib/todo-manager.js +281 -47
  128. package/src/lib/token-incentive.js +341 -0
  129. package/src/lib/topic-classifier.js +297 -49
  130. package/src/lib/user-profile.js +294 -50
  131. package/src/lib/version-checker.js +304 -50
  132. package/src/lib/web-fetch.js +292 -51
  133. package/src/lib/zkp-engine.js +286 -49
@@ -129,81 +129,320 @@ export async function* followSession(sessionId, options = {}) {
129
129
 
130
130
  // ===== V2 Surface: Session Tail governance overlay (CLI v0.142.0) =====
131
131
  export const STAIL_SUB_MATURITY_V2 = Object.freeze({
132
- PENDING: "pending", ACTIVE: "active", PAUSED: "paused", CLOSED: "closed",
132
+ PENDING: "pending",
133
+ ACTIVE: "active",
134
+ PAUSED: "paused",
135
+ CLOSED: "closed",
133
136
  });
134
137
  export const STAIL_EVENT_LIFECYCLE_V2 = Object.freeze({
135
- QUEUED: "queued", TAILING: "tailing", COMPLETED: "completed", FAILED: "failed", CANCELLED: "cancelled",
138
+ QUEUED: "queued",
139
+ TAILING: "tailing",
140
+ COMPLETED: "completed",
141
+ FAILED: "failed",
142
+ CANCELLED: "cancelled",
136
143
  });
137
144
  const _stailSTrans = new Map([
138
- [STAIL_SUB_MATURITY_V2.PENDING, new Set([STAIL_SUB_MATURITY_V2.ACTIVE, STAIL_SUB_MATURITY_V2.CLOSED])],
139
- [STAIL_SUB_MATURITY_V2.ACTIVE, new Set([STAIL_SUB_MATURITY_V2.PAUSED, STAIL_SUB_MATURITY_V2.CLOSED])],
140
- [STAIL_SUB_MATURITY_V2.PAUSED, new Set([STAIL_SUB_MATURITY_V2.ACTIVE, STAIL_SUB_MATURITY_V2.CLOSED])],
145
+ [
146
+ STAIL_SUB_MATURITY_V2.PENDING,
147
+ new Set([STAIL_SUB_MATURITY_V2.ACTIVE, STAIL_SUB_MATURITY_V2.CLOSED]),
148
+ ],
149
+ [
150
+ STAIL_SUB_MATURITY_V2.ACTIVE,
151
+ new Set([STAIL_SUB_MATURITY_V2.PAUSED, STAIL_SUB_MATURITY_V2.CLOSED]),
152
+ ],
153
+ [
154
+ STAIL_SUB_MATURITY_V2.PAUSED,
155
+ new Set([STAIL_SUB_MATURITY_V2.ACTIVE, STAIL_SUB_MATURITY_V2.CLOSED]),
156
+ ],
141
157
  [STAIL_SUB_MATURITY_V2.CLOSED, new Set()],
142
158
  ]);
143
159
  const _stailSTerminal = new Set([STAIL_SUB_MATURITY_V2.CLOSED]);
144
160
  const _stailETrans = new Map([
145
- [STAIL_EVENT_LIFECYCLE_V2.QUEUED, new Set([STAIL_EVENT_LIFECYCLE_V2.TAILING, STAIL_EVENT_LIFECYCLE_V2.CANCELLED])],
146
- [STAIL_EVENT_LIFECYCLE_V2.TAILING, new Set([STAIL_EVENT_LIFECYCLE_V2.COMPLETED, STAIL_EVENT_LIFECYCLE_V2.FAILED, STAIL_EVENT_LIFECYCLE_V2.CANCELLED])],
161
+ [
162
+ STAIL_EVENT_LIFECYCLE_V2.QUEUED,
163
+ new Set([
164
+ STAIL_EVENT_LIFECYCLE_V2.TAILING,
165
+ STAIL_EVENT_LIFECYCLE_V2.CANCELLED,
166
+ ]),
167
+ ],
168
+ [
169
+ STAIL_EVENT_LIFECYCLE_V2.TAILING,
170
+ new Set([
171
+ STAIL_EVENT_LIFECYCLE_V2.COMPLETED,
172
+ STAIL_EVENT_LIFECYCLE_V2.FAILED,
173
+ STAIL_EVENT_LIFECYCLE_V2.CANCELLED,
174
+ ]),
175
+ ],
147
176
  [STAIL_EVENT_LIFECYCLE_V2.COMPLETED, new Set()],
148
177
  [STAIL_EVENT_LIFECYCLE_V2.FAILED, new Set()],
149
178
  [STAIL_EVENT_LIFECYCLE_V2.CANCELLED, new Set()],
150
179
  ]);
151
180
  const _stailSsV2 = new Map();
152
181
  const _stailEsV2 = new Map();
153
- let _stailMaxActive = 10, _stailMaxPending = 30, _stailIdleMs = 24 * 60 * 60 * 1000, _stailStuckMs = 60 * 1000;
154
- function _stailPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
155
- function _stailCheckS(from, to) { const a = _stailSTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid stail subscription transition ${from} → ${to}`); }
156
- function _stailCheckE(from, to) { const a = _stailETrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid stail event transition ${from} → ${to}`); }
157
- export function setMaxActiveStailSubsPerOwnerV2(n) { _stailMaxActive = _stailPos(n, "maxActiveStailSubsPerOwner"); }
158
- export function getMaxActiveStailSubsPerOwnerV2() { return _stailMaxActive; }
159
- export function setMaxPendingStailEventsPerSubV2(n) { _stailMaxPending = _stailPos(n, "maxPendingStailEventsPerSub"); }
160
- export function getMaxPendingStailEventsPerSubV2() { return _stailMaxPending; }
161
- export function setStailSubIdleMsV2(n) { _stailIdleMs = _stailPos(n, "stailSubIdleMs"); }
162
- export function getStailSubIdleMsV2() { return _stailIdleMs; }
163
- export function setStailEventStuckMsV2(n) { _stailStuckMs = _stailPos(n, "stailEventStuckMs"); }
164
- export function getStailEventStuckMsV2() { return _stailStuckMs; }
165
- export function _resetStateSessionTailV2() { _stailSsV2.clear(); _stailEsV2.clear(); _stailMaxActive = 10; _stailMaxPending = 30; _stailIdleMs = 24 * 60 * 60 * 1000; _stailStuckMs = 60 * 1000; }
182
+ let _stailMaxActive = 10,
183
+ _stailMaxPending = 30,
184
+ _stailIdleMs = 24 * 60 * 60 * 1000,
185
+ _stailStuckMs = 60 * 1000;
186
+ function _stailPos(n, label) {
187
+ const v = Math.floor(Number(n));
188
+ if (!Number.isFinite(v) || v <= 0)
189
+ throw new Error(`${label} must be positive integer`);
190
+ return v;
191
+ }
192
+ function _stailCheckS(from, to) {
193
+ const a = _stailSTrans.get(from);
194
+ if (!a || !a.has(to))
195
+ throw new Error(`invalid stail subscription transition ${from} → ${to}`);
196
+ }
197
+ function _stailCheckE(from, to) {
198
+ const a = _stailETrans.get(from);
199
+ if (!a || !a.has(to))
200
+ throw new Error(`invalid stail event transition ${from} → ${to}`);
201
+ }
202
+ export function setMaxActiveStailSubsPerOwnerV2(n) {
203
+ _stailMaxActive = _stailPos(n, "maxActiveStailSubsPerOwner");
204
+ }
205
+ export function getMaxActiveStailSubsPerOwnerV2() {
206
+ return _stailMaxActive;
207
+ }
208
+ export function setMaxPendingStailEventsPerSubV2(n) {
209
+ _stailMaxPending = _stailPos(n, "maxPendingStailEventsPerSub");
210
+ }
211
+ export function getMaxPendingStailEventsPerSubV2() {
212
+ return _stailMaxPending;
213
+ }
214
+ export function setStailSubIdleMsV2(n) {
215
+ _stailIdleMs = _stailPos(n, "stailSubIdleMs");
216
+ }
217
+ export function getStailSubIdleMsV2() {
218
+ return _stailIdleMs;
219
+ }
220
+ export function setStailEventStuckMsV2(n) {
221
+ _stailStuckMs = _stailPos(n, "stailEventStuckMs");
222
+ }
223
+ export function getStailEventStuckMsV2() {
224
+ return _stailStuckMs;
225
+ }
226
+ export function _resetStateSessionTailV2() {
227
+ _stailSsV2.clear();
228
+ _stailEsV2.clear();
229
+ _stailMaxActive = 10;
230
+ _stailMaxPending = 30;
231
+ _stailIdleMs = 24 * 60 * 60 * 1000;
232
+ _stailStuckMs = 60 * 1000;
233
+ }
166
234
  export function registerStailSubV2({ id, owner, sessionId, metadata } = {}) {
167
- if (!id) throw new Error("stail sub id required"); if (!owner) throw new Error("stail sub owner required");
235
+ if (!id) throw new Error("stail sub id required");
236
+ if (!owner) throw new Error("stail sub owner required");
168
237
  if (_stailSsV2.has(id)) throw new Error(`stail sub ${id} already registered`);
169
238
  const now = Date.now();
170
- const s = { id, owner, sessionId: sessionId || "*", status: STAIL_SUB_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, closedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
171
- _stailSsV2.set(id, s); return { ...s, metadata: { ...s.metadata } };
239
+ const s = {
240
+ id,
241
+ owner,
242
+ sessionId: sessionId || "*",
243
+ status: STAIL_SUB_MATURITY_V2.PENDING,
244
+ createdAt: now,
245
+ updatedAt: now,
246
+ activatedAt: null,
247
+ closedAt: null,
248
+ lastTouchedAt: now,
249
+ metadata: { ...(metadata || {}) },
250
+ };
251
+ _stailSsV2.set(id, s);
252
+ return { ...s, metadata: { ...s.metadata } };
253
+ }
254
+ function _stailCountActive(owner) {
255
+ let n = 0;
256
+ for (const s of _stailSsV2.values())
257
+ if (s.owner === owner && s.status === STAIL_SUB_MATURITY_V2.ACTIVE) n++;
258
+ return n;
172
259
  }
173
- function _stailCountActive(owner) { let n = 0; for (const s of _stailSsV2.values()) if (s.owner === owner && s.status === STAIL_SUB_MATURITY_V2.ACTIVE) n++; return n; }
174
260
  export function activateStailSubV2(id) {
175
- const s = _stailSsV2.get(id); if (!s) throw new Error(`stail sub ${id} not found`);
261
+ const s = _stailSsV2.get(id);
262
+ if (!s) throw new Error(`stail sub ${id} not found`);
176
263
  _stailCheckS(s.status, STAIL_SUB_MATURITY_V2.ACTIVE);
177
264
  const recovery = s.status === STAIL_SUB_MATURITY_V2.PAUSED;
178
- if (!recovery && _stailCountActive(s.owner) >= _stailMaxActive) throw new Error(`max active stail subs for owner ${s.owner} reached`);
179
- const now = Date.now(); s.status = STAIL_SUB_MATURITY_V2.ACTIVE; s.updatedAt = now; s.lastTouchedAt = now; if (!s.activatedAt) s.activatedAt = now;
265
+ if (!recovery && _stailCountActive(s.owner) >= _stailMaxActive)
266
+ throw new Error(`max active stail subs for owner ${s.owner} reached`);
267
+ const now = Date.now();
268
+ s.status = STAIL_SUB_MATURITY_V2.ACTIVE;
269
+ s.updatedAt = now;
270
+ s.lastTouchedAt = now;
271
+ if (!s.activatedAt) s.activatedAt = now;
272
+ return { ...s, metadata: { ...s.metadata } };
273
+ }
274
+ export function pauseStailSubV2(id) {
275
+ const s = _stailSsV2.get(id);
276
+ if (!s) throw new Error(`stail sub ${id} not found`);
277
+ _stailCheckS(s.status, STAIL_SUB_MATURITY_V2.PAUSED);
278
+ s.status = STAIL_SUB_MATURITY_V2.PAUSED;
279
+ s.updatedAt = Date.now();
280
+ return { ...s, metadata: { ...s.metadata } };
281
+ }
282
+ export function closeStailSubV2(id) {
283
+ const s = _stailSsV2.get(id);
284
+ if (!s) throw new Error(`stail sub ${id} not found`);
285
+ _stailCheckS(s.status, STAIL_SUB_MATURITY_V2.CLOSED);
286
+ const now = Date.now();
287
+ s.status = STAIL_SUB_MATURITY_V2.CLOSED;
288
+ s.updatedAt = now;
289
+ if (!s.closedAt) s.closedAt = now;
290
+ return { ...s, metadata: { ...s.metadata } };
291
+ }
292
+ export function touchStailSubV2(id) {
293
+ const s = _stailSsV2.get(id);
294
+ if (!s) throw new Error(`stail sub ${id} not found`);
295
+ if (_stailSTerminal.has(s.status))
296
+ throw new Error(`cannot touch terminal stail sub ${id}`);
297
+ const now = Date.now();
298
+ s.lastTouchedAt = now;
299
+ s.updatedAt = now;
300
+ return { ...s, metadata: { ...s.metadata } };
301
+ }
302
+ export function getStailSubV2(id) {
303
+ const s = _stailSsV2.get(id);
304
+ if (!s) return null;
180
305
  return { ...s, metadata: { ...s.metadata } };
181
306
  }
182
- export function pauseStailSubV2(id) { const s = _stailSsV2.get(id); if (!s) throw new Error(`stail sub ${id} not found`); _stailCheckS(s.status, STAIL_SUB_MATURITY_V2.PAUSED); s.status = STAIL_SUB_MATURITY_V2.PAUSED; s.updatedAt = Date.now(); return { ...s, metadata: { ...s.metadata } }; }
183
- export function closeStailSubV2(id) { const s = _stailSsV2.get(id); if (!s) throw new Error(`stail sub ${id} not found`); _stailCheckS(s.status, STAIL_SUB_MATURITY_V2.CLOSED); const now = Date.now(); s.status = STAIL_SUB_MATURITY_V2.CLOSED; s.updatedAt = now; if (!s.closedAt) s.closedAt = now; return { ...s, metadata: { ...s.metadata } }; }
184
- export function touchStailSubV2(id) { const s = _stailSsV2.get(id); if (!s) throw new Error(`stail sub ${id} not found`); if (_stailSTerminal.has(s.status)) throw new Error(`cannot touch terminal stail sub ${id}`); const now = Date.now(); s.lastTouchedAt = now; s.updatedAt = now; return { ...s, metadata: { ...s.metadata } }; }
185
- export function getStailSubV2(id) { const s = _stailSsV2.get(id); if (!s) return null; return { ...s, metadata: { ...s.metadata } }; }
186
- export function listStailSubsV2() { return [..._stailSsV2.values()].map((s) => ({ ...s, metadata: { ...s.metadata } })); }
187
- function _stailCountPending(subId) { let n = 0; for (const e of _stailEsV2.values()) if (e.subId === subId && (e.status === STAIL_EVENT_LIFECYCLE_V2.QUEUED || e.status === STAIL_EVENT_LIFECYCLE_V2.TAILING)) n++; return n; }
307
+ export function listStailSubsV2() {
308
+ return [..._stailSsV2.values()].map((s) => ({
309
+ ...s,
310
+ metadata: { ...s.metadata },
311
+ }));
312
+ }
313
+ function _stailCountPending(subId) {
314
+ let n = 0;
315
+ for (const e of _stailEsV2.values())
316
+ if (
317
+ e.subId === subId &&
318
+ (e.status === STAIL_EVENT_LIFECYCLE_V2.QUEUED ||
319
+ e.status === STAIL_EVENT_LIFECYCLE_V2.TAILING)
320
+ )
321
+ n++;
322
+ return n;
323
+ }
188
324
  export function createStailEventV2({ id, subId, cursor, metadata } = {}) {
189
- if (!id) throw new Error("stail event id required"); if (!subId) throw new Error("stail event subId required");
325
+ if (!id) throw new Error("stail event id required");
326
+ if (!subId) throw new Error("stail event subId required");
190
327
  if (_stailEsV2.has(id)) throw new Error(`stail event ${id} already exists`);
191
328
  if (!_stailSsV2.has(subId)) throw new Error(`stail sub ${subId} not found`);
192
- if (_stailCountPending(subId) >= _stailMaxPending) throw new Error(`max pending stail events for sub ${subId} reached`);
329
+ if (_stailCountPending(subId) >= _stailMaxPending)
330
+ throw new Error(`max pending stail events for sub ${subId} reached`);
193
331
  const now = Date.now();
194
- const e = { id, subId, cursor: cursor || "0", status: STAIL_EVENT_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
195
- _stailEsV2.set(id, e); return { ...e, metadata: { ...e.metadata } };
196
- }
197
- export function tailingStailEventV2(id) { const e = _stailEsV2.get(id); if (!e) throw new Error(`stail event ${id} not found`); _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.TAILING); const now = Date.now(); e.status = STAIL_EVENT_LIFECYCLE_V2.TAILING; e.updatedAt = now; if (!e.startedAt) e.startedAt = now; return { ...e, metadata: { ...e.metadata } }; }
198
- export function completeStailEventV2(id) { const e = _stailEsV2.get(id); if (!e) throw new Error(`stail event ${id} not found`); _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.COMPLETED); const now = Date.now(); e.status = STAIL_EVENT_LIFECYCLE_V2.COMPLETED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; return { ...e, metadata: { ...e.metadata } }; }
199
- export function failStailEventV2(id, reason) { const e = _stailEsV2.get(id); if (!e) throw new Error(`stail event ${id} not found`); _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.FAILED); const now = Date.now(); e.status = STAIL_EVENT_LIFECYCLE_V2.FAILED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.failReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
200
- export function cancelStailEventV2(id, reason) { const e = _stailEsV2.get(id); if (!e) throw new Error(`stail event ${id} not found`); _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.CANCELLED); const now = Date.now(); e.status = STAIL_EVENT_LIFECYCLE_V2.CANCELLED; e.updatedAt = now; if (!e.settledAt) e.settledAt = now; if (reason) e.metadata.cancelReason = String(reason); return { ...e, metadata: { ...e.metadata } }; }
201
- export function getStailEventV2(id) { const e = _stailEsV2.get(id); if (!e) return null; return { ...e, metadata: { ...e.metadata } }; }
202
- export function listStailEventsV2() { return [..._stailEsV2.values()].map((e) => ({ ...e, metadata: { ...e.metadata } })); }
203
- export function autoPauseIdleStailSubsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const s of _stailSsV2.values()) if (s.status === STAIL_SUB_MATURITY_V2.ACTIVE && (t - s.lastTouchedAt) >= _stailIdleMs) { s.status = STAIL_SUB_MATURITY_V2.PAUSED; s.updatedAt = t; flipped.push(s.id); } return { flipped, count: flipped.length }; }
204
- export function autoFailStuckStailEventsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const e of _stailEsV2.values()) if (e.status === STAIL_EVENT_LIFECYCLE_V2.TAILING && e.startedAt != null && (t - e.startedAt) >= _stailStuckMs) { e.status = STAIL_EVENT_LIFECYCLE_V2.FAILED; e.updatedAt = t; if (!e.settledAt) e.settledAt = t; e.metadata.failReason = "auto-fail-stuck"; flipped.push(e.id); } return { flipped, count: flipped.length }; }
332
+ const e = {
333
+ id,
334
+ subId,
335
+ cursor: cursor || "0",
336
+ status: STAIL_EVENT_LIFECYCLE_V2.QUEUED,
337
+ createdAt: now,
338
+ updatedAt: now,
339
+ startedAt: null,
340
+ settledAt: null,
341
+ metadata: { ...(metadata || {}) },
342
+ };
343
+ _stailEsV2.set(id, e);
344
+ return { ...e, metadata: { ...e.metadata } };
345
+ }
346
+ export function tailingStailEventV2(id) {
347
+ const e = _stailEsV2.get(id);
348
+ if (!e) throw new Error(`stail event ${id} not found`);
349
+ _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.TAILING);
350
+ const now = Date.now();
351
+ e.status = STAIL_EVENT_LIFECYCLE_V2.TAILING;
352
+ e.updatedAt = now;
353
+ if (!e.startedAt) e.startedAt = now;
354
+ return { ...e, metadata: { ...e.metadata } };
355
+ }
356
+ export function completeStailEventV2(id) {
357
+ const e = _stailEsV2.get(id);
358
+ if (!e) throw new Error(`stail event ${id} not found`);
359
+ _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.COMPLETED);
360
+ const now = Date.now();
361
+ e.status = STAIL_EVENT_LIFECYCLE_V2.COMPLETED;
362
+ e.updatedAt = now;
363
+ if (!e.settledAt) e.settledAt = now;
364
+ return { ...e, metadata: { ...e.metadata } };
365
+ }
366
+ export function failStailEventV2(id, reason) {
367
+ const e = _stailEsV2.get(id);
368
+ if (!e) throw new Error(`stail event ${id} not found`);
369
+ _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.FAILED);
370
+ const now = Date.now();
371
+ e.status = STAIL_EVENT_LIFECYCLE_V2.FAILED;
372
+ e.updatedAt = now;
373
+ if (!e.settledAt) e.settledAt = now;
374
+ if (reason) e.metadata.failReason = String(reason);
375
+ return { ...e, metadata: { ...e.metadata } };
376
+ }
377
+ export function cancelStailEventV2(id, reason) {
378
+ const e = _stailEsV2.get(id);
379
+ if (!e) throw new Error(`stail event ${id} not found`);
380
+ _stailCheckE(e.status, STAIL_EVENT_LIFECYCLE_V2.CANCELLED);
381
+ const now = Date.now();
382
+ e.status = STAIL_EVENT_LIFECYCLE_V2.CANCELLED;
383
+ e.updatedAt = now;
384
+ if (!e.settledAt) e.settledAt = now;
385
+ if (reason) e.metadata.cancelReason = String(reason);
386
+ return { ...e, metadata: { ...e.metadata } };
387
+ }
388
+ export function getStailEventV2(id) {
389
+ const e = _stailEsV2.get(id);
390
+ if (!e) return null;
391
+ return { ...e, metadata: { ...e.metadata } };
392
+ }
393
+ export function listStailEventsV2() {
394
+ return [..._stailEsV2.values()].map((e) => ({
395
+ ...e,
396
+ metadata: { ...e.metadata },
397
+ }));
398
+ }
399
+ export function autoPauseIdleStailSubsV2({ now } = {}) {
400
+ const t = now ?? Date.now();
401
+ const flipped = [];
402
+ for (const s of _stailSsV2.values())
403
+ if (
404
+ s.status === STAIL_SUB_MATURITY_V2.ACTIVE &&
405
+ t - s.lastTouchedAt >= _stailIdleMs
406
+ ) {
407
+ s.status = STAIL_SUB_MATURITY_V2.PAUSED;
408
+ s.updatedAt = t;
409
+ flipped.push(s.id);
410
+ }
411
+ return { flipped, count: flipped.length };
412
+ }
413
+ export function autoFailStuckStailEventsV2({ now } = {}) {
414
+ const t = now ?? Date.now();
415
+ const flipped = [];
416
+ for (const e of _stailEsV2.values())
417
+ if (
418
+ e.status === STAIL_EVENT_LIFECYCLE_V2.TAILING &&
419
+ e.startedAt != null &&
420
+ t - e.startedAt >= _stailStuckMs
421
+ ) {
422
+ e.status = STAIL_EVENT_LIFECYCLE_V2.FAILED;
423
+ e.updatedAt = t;
424
+ if (!e.settledAt) e.settledAt = t;
425
+ e.metadata.failReason = "auto-fail-stuck";
426
+ flipped.push(e.id);
427
+ }
428
+ return { flipped, count: flipped.length };
429
+ }
205
430
  export function getSessionTailGovStatsV2() {
206
- const subsByStatus = {}; for (const v of Object.values(STAIL_SUB_MATURITY_V2)) subsByStatus[v] = 0; for (const s of _stailSsV2.values()) subsByStatus[s.status]++;
207
- const eventsByStatus = {}; for (const v of Object.values(STAIL_EVENT_LIFECYCLE_V2)) eventsByStatus[v] = 0; for (const e of _stailEsV2.values()) eventsByStatus[e.status]++;
208
- return { totalStailSubsV2: _stailSsV2.size, totalStailEventsV2: _stailEsV2.size, maxActiveStailSubsPerOwner: _stailMaxActive, maxPendingStailEventsPerSub: _stailMaxPending, stailSubIdleMs: _stailIdleMs, stailEventStuckMs: _stailStuckMs, subsByStatus, eventsByStatus };
431
+ const subsByStatus = {};
432
+ for (const v of Object.values(STAIL_SUB_MATURITY_V2)) subsByStatus[v] = 0;
433
+ for (const s of _stailSsV2.values()) subsByStatus[s.status]++;
434
+ const eventsByStatus = {};
435
+ for (const v of Object.values(STAIL_EVENT_LIFECYCLE_V2))
436
+ eventsByStatus[v] = 0;
437
+ for (const e of _stailEsV2.values()) eventsByStatus[e.status]++;
438
+ return {
439
+ totalStailSubsV2: _stailSsV2.size,
440
+ totalStailEventsV2: _stailEsV2.size,
441
+ maxActiveStailSubsPerOwner: _stailMaxActive,
442
+ maxPendingStailEventsPerSub: _stailMaxPending,
443
+ stailSubIdleMs: _stailIdleMs,
444
+ stailEventStuckMs: _stailStuckMs,
445
+ subsByStatus,
446
+ eventsByStatus,
447
+ };
209
448
  }