chainlesschain 0.81.0 → 0.132.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 (110) hide show
  1. package/bin/chainlesschain.js +0 -0
  2. package/package.json +1 -1
  3. package/src/commands/agent-network.js +254 -1
  4. package/src/commands/audit.js +302 -0
  5. package/src/commands/automation.js +271 -1
  6. package/src/commands/codegen.js +224 -0
  7. package/src/commands/collab.js +341 -0
  8. package/src/commands/compliance.js +1035 -0
  9. package/src/commands/cowork.js +221 -0
  10. package/src/commands/dbevo.js +284 -0
  11. package/src/commands/dev.js +252 -0
  12. package/src/commands/did.js +358 -0
  13. package/src/commands/encrypt.js +341 -0
  14. package/src/commands/export.js +256 -1
  15. package/src/commands/fusion.js +258 -0
  16. package/src/commands/governance.js +325 -0
  17. package/src/commands/hardening.js +411 -0
  18. package/src/commands/hook.js +148 -0
  19. package/src/commands/import.js +252 -0
  20. package/src/commands/incentive.js +322 -0
  21. package/src/commands/infra.js +244 -0
  22. package/src/commands/instinct.js +260 -0
  23. package/src/commands/ipfs.js +318 -0
  24. package/src/commands/kg.js +387 -0
  25. package/src/commands/llm.js +263 -0
  26. package/src/commands/mcp.js +221 -0
  27. package/src/commands/memory.js +248 -0
  28. package/src/commands/multimodal.js +296 -0
  29. package/src/commands/nlprog.js +356 -0
  30. package/src/commands/note.js +244 -0
  31. package/src/commands/ops.js +354 -0
  32. package/src/commands/orchestrate.js +166 -0
  33. package/src/commands/org.js +277 -0
  34. package/src/commands/p2p.js +390 -0
  35. package/src/commands/perception.js +290 -0
  36. package/src/commands/permmem.js +251 -0
  37. package/src/commands/plugin-ecosystem.js +273 -0
  38. package/src/commands/pqc.js +393 -0
  39. package/src/commands/quantization.js +351 -0
  40. package/src/commands/rcache.js +271 -0
  41. package/src/commands/recommend.js +340 -0
  42. package/src/commands/runtime.js +307 -0
  43. package/src/commands/scim.js +262 -0
  44. package/src/commands/session.js +258 -0
  45. package/src/commands/skill.js +267 -1
  46. package/src/commands/social.js +256 -0
  47. package/src/commands/sso.js +186 -1
  48. package/src/commands/sync.js +256 -0
  49. package/src/commands/tech.js +338 -0
  50. package/src/commands/tenant.js +351 -0
  51. package/src/commands/tokens.js +269 -0
  52. package/src/commands/trust.js +249 -0
  53. package/src/commands/wallet.js +277 -0
  54. package/src/commands/workflow.js +171 -0
  55. package/src/index.js +4 -0
  56. package/src/lib/agent-coordinator.js +325 -0
  57. package/src/lib/agent-network.js +387 -0
  58. package/src/lib/agent-router.js +395 -0
  59. package/src/lib/aiops.js +478 -0
  60. package/src/lib/audit-logger.js +379 -0
  61. package/src/lib/automation-engine.js +330 -0
  62. package/src/lib/autonomous-developer.js +350 -0
  63. package/src/lib/code-agent.js +323 -0
  64. package/src/lib/collaboration-governance.js +364 -0
  65. package/src/lib/community-governance.js +436 -0
  66. package/src/lib/compliance-manager.js +434 -0
  67. package/src/lib/content-recommendation.js +469 -0
  68. package/src/lib/crypto-manager.js +350 -0
  69. package/src/lib/dbevo.js +338 -0
  70. package/src/lib/decentral-infra.js +340 -0
  71. package/src/lib/did-manager.js +367 -0
  72. package/src/lib/hardening-manager.js +348 -0
  73. package/src/lib/hook-manager.js +380 -0
  74. package/src/lib/instinct-manager.js +332 -0
  75. package/src/lib/ipfs-storage.js +334 -0
  76. package/src/lib/knowledge-exporter.js +381 -0
  77. package/src/lib/knowledge-graph.js +432 -0
  78. package/src/lib/knowledge-importer.js +379 -0
  79. package/src/lib/llm-providers.js +391 -0
  80. package/src/lib/mcp-registry.js +333 -0
  81. package/src/lib/memory-manager.js +330 -0
  82. package/src/lib/multimodal.js +346 -0
  83. package/src/lib/nl-programming.js +343 -0
  84. package/src/lib/note-versioning.js +327 -0
  85. package/src/lib/org-manager.js +323 -0
  86. package/src/lib/p2p-manager.js +387 -0
  87. package/src/lib/perception.js +346 -0
  88. package/src/lib/perf-tuning.js +4 -1
  89. package/src/lib/permanent-memory.js +320 -0
  90. package/src/lib/plugin-ecosystem.js +377 -0
  91. package/src/lib/pqc-manager.js +368 -0
  92. package/src/lib/protocol-fusion.js +417 -0
  93. package/src/lib/quantization.js +325 -0
  94. package/src/lib/response-cache.js +327 -0
  95. package/src/lib/scim-manager.js +329 -0
  96. package/src/lib/session-manager.js +329 -0
  97. package/src/lib/skill-loader.js +377 -0
  98. package/src/lib/social-manager.js +326 -0
  99. package/src/lib/sso-manager.js +332 -0
  100. package/src/lib/sync-manager.js +326 -0
  101. package/src/lib/tech-learning-engine.js +369 -0
  102. package/src/lib/tenant-saas.js +460 -0
  103. package/src/lib/threat-intel.js +335 -0
  104. package/src/lib/token-incentive.js +293 -0
  105. package/src/lib/token-tracker.js +329 -0
  106. package/src/lib/trust-security.js +390 -0
  107. package/src/lib/ueba.js +389 -0
  108. package/src/lib/universal-runtime.js +325 -0
  109. package/src/lib/wallet-manager.js +326 -0
  110. package/src/lib/workflow-engine.js +322 -0
@@ -198,3 +198,332 @@ export function getTodayStats(db) {
198
198
  startDate: new Date().toISOString().slice(0, 10),
199
199
  });
200
200
  }
201
+
202
+ /* ═══════════════════════════════════════════════════════════════
203
+ * V2 Surface — Token tracker governance layer.
204
+ * Tracks per-owner budget maturity + per-budget usage-record lifecycle
205
+ * independent of legacy SQLite token_usage table.
206
+ * ═══════════════════════════════════════════════════════════════ */
207
+
208
+ export const BUDGET_MATURITY_V2 = Object.freeze({
209
+ PLANNING: "planning",
210
+ ACTIVE: "active",
211
+ SUSPENDED: "suspended",
212
+ ARCHIVED: "archived",
213
+ });
214
+
215
+ export const USAGE_RECORD_LIFECYCLE_V2 = Object.freeze({
216
+ PENDING: "pending",
217
+ RECORDED: "recorded",
218
+ BILLED: "billed",
219
+ REJECTED: "rejected",
220
+ REFUNDED: "refunded",
221
+ });
222
+
223
+ const BUDGET_TRANSITIONS_V2 = new Map([
224
+ ["planning", new Set(["active", "archived"])],
225
+ ["active", new Set(["suspended", "archived"])],
226
+ ["suspended", new Set(["active", "archived"])],
227
+ ["archived", new Set()],
228
+ ]);
229
+ const BUDGET_TERMINALS_V2 = new Set(["archived"]);
230
+
231
+ const RECORD_TRANSITIONS_V2 = new Map([
232
+ ["pending", new Set(["recorded", "rejected"])],
233
+ ["recorded", new Set(["billed", "rejected", "refunded"])],
234
+ ["billed", new Set()],
235
+ ["rejected", new Set()],
236
+ ["refunded", new Set()],
237
+ ]);
238
+ const RECORD_TERMINALS_V2 = new Set(["billed", "rejected", "refunded"]);
239
+
240
+ export const TOKEN_DEFAULT_MAX_ACTIVE_BUDGETS_PER_OWNER = 10;
241
+ export const TOKEN_DEFAULT_MAX_PENDING_RECORDS_PER_BUDGET = 500;
242
+ export const TOKEN_DEFAULT_BUDGET_IDLE_MS = 1000 * 60 * 60 * 24 * 30; // 30 days
243
+ export const TOKEN_DEFAULT_RECORD_STUCK_MS = 1000 * 60 * 60; // 1 hour
244
+
245
+ const _budgetsV2 = new Map();
246
+ const _recordsV2 = new Map();
247
+ let _maxActiveBudgetsPerOwnerV2 = TOKEN_DEFAULT_MAX_ACTIVE_BUDGETS_PER_OWNER;
248
+ let _maxPendingRecordsPerBudgetV2 =
249
+ TOKEN_DEFAULT_MAX_PENDING_RECORDS_PER_BUDGET;
250
+ let _budgetIdleMsV2 = TOKEN_DEFAULT_BUDGET_IDLE_MS;
251
+ let _recordStuckMsV2 = TOKEN_DEFAULT_RECORD_STUCK_MS;
252
+
253
+ function _posIntTokenV2(n, label) {
254
+ const v = Math.floor(Number(n));
255
+ if (!Number.isFinite(v) || v <= 0)
256
+ throw new Error(`${label} must be a positive integer`);
257
+ return v;
258
+ }
259
+
260
+ export function getMaxActiveBudgetsPerOwnerV2() {
261
+ return _maxActiveBudgetsPerOwnerV2;
262
+ }
263
+ export function setMaxActiveBudgetsPerOwnerV2(n) {
264
+ _maxActiveBudgetsPerOwnerV2 = _posIntTokenV2(n, "maxActiveBudgetsPerOwner");
265
+ }
266
+ export function getMaxPendingRecordsPerBudgetV2() {
267
+ return _maxPendingRecordsPerBudgetV2;
268
+ }
269
+ export function setMaxPendingRecordsPerBudgetV2(n) {
270
+ _maxPendingRecordsPerBudgetV2 = _posIntTokenV2(
271
+ n,
272
+ "maxPendingRecordsPerBudget",
273
+ );
274
+ }
275
+ export function getBudgetIdleMsV2() {
276
+ return _budgetIdleMsV2;
277
+ }
278
+ export function setBudgetIdleMsV2(n) {
279
+ _budgetIdleMsV2 = _posIntTokenV2(n, "budgetIdleMs");
280
+ }
281
+ export function getRecordStuckMsV2() {
282
+ return _recordStuckMsV2;
283
+ }
284
+ export function setRecordStuckMsV2(n) {
285
+ _recordStuckMsV2 = _posIntTokenV2(n, "recordStuckMs");
286
+ }
287
+
288
+ export function getActiveBudgetCountV2(ownerId) {
289
+ let n = 0;
290
+ for (const b of _budgetsV2.values()) {
291
+ if (b.ownerId === ownerId && b.status === "active") n += 1;
292
+ }
293
+ return n;
294
+ }
295
+
296
+ export function getPendingRecordCountV2(budgetId) {
297
+ let n = 0;
298
+ for (const r of _recordsV2.values()) {
299
+ if (
300
+ r.budgetId === budgetId &&
301
+ (r.status === "pending" || r.status === "recorded")
302
+ )
303
+ n += 1;
304
+ }
305
+ return n;
306
+ }
307
+
308
+ function _copyBudgetV2(b) {
309
+ return { ...b, metadata: { ...b.metadata } };
310
+ }
311
+ function _copyRecordV2(r) {
312
+ return { ...r, metadata: { ...r.metadata } };
313
+ }
314
+
315
+ export function registerBudgetV2(
316
+ id,
317
+ { ownerId, label, metadata = {}, now = Date.now() } = {},
318
+ ) {
319
+ if (!id || typeof id !== "string") throw new Error("id must be a string");
320
+ if (!ownerId || typeof ownerId !== "string")
321
+ throw new Error("ownerId must be a string");
322
+ if (!label || typeof label !== "string")
323
+ throw new Error("label must be a string");
324
+ if (_budgetsV2.has(id)) throw new Error(`budget ${id} already exists`);
325
+ const b = {
326
+ id,
327
+ ownerId,
328
+ label,
329
+ status: "planning",
330
+ createdAt: now,
331
+ lastSeenAt: now,
332
+ activatedAt: null,
333
+ archivedAt: null,
334
+ metadata: { ...metadata },
335
+ };
336
+ _budgetsV2.set(id, b);
337
+ return _copyBudgetV2(b);
338
+ }
339
+
340
+ export function getBudgetV2(id) {
341
+ const b = _budgetsV2.get(id);
342
+ return b ? _copyBudgetV2(b) : null;
343
+ }
344
+
345
+ export function listBudgetsV2({ ownerId, status } = {}) {
346
+ const out = [];
347
+ for (const b of _budgetsV2.values()) {
348
+ if (ownerId && b.ownerId !== ownerId) continue;
349
+ if (status && b.status !== status) continue;
350
+ out.push(_copyBudgetV2(b));
351
+ }
352
+ return out;
353
+ }
354
+
355
+ export function setBudgetStatusV2(id, next, { now = Date.now() } = {}) {
356
+ const b = _budgetsV2.get(id);
357
+ if (!b) throw new Error(`budget ${id} not found`);
358
+ if (!BUDGET_TRANSITIONS_V2.has(next))
359
+ throw new Error(`unknown budget status: ${next}`);
360
+ if (BUDGET_TERMINALS_V2.has(b.status))
361
+ throw new Error(`budget ${id} is in terminal state ${b.status}`);
362
+ const allowed = BUDGET_TRANSITIONS_V2.get(b.status);
363
+ if (!allowed.has(next))
364
+ throw new Error(`cannot transition budget from ${b.status} to ${next}`);
365
+ if (next === "active") {
366
+ if (b.status === "planning") {
367
+ const count = getActiveBudgetCountV2(b.ownerId);
368
+ if (count >= _maxActiveBudgetsPerOwnerV2)
369
+ throw new Error(
370
+ `owner ${b.ownerId} already at active-budget cap (${_maxActiveBudgetsPerOwnerV2})`,
371
+ );
372
+ }
373
+ if (!b.activatedAt) b.activatedAt = now;
374
+ }
375
+ if (next === "archived" && !b.archivedAt) b.archivedAt = now;
376
+ b.status = next;
377
+ b.lastSeenAt = now;
378
+ return _copyBudgetV2(b);
379
+ }
380
+
381
+ export function activateBudgetV2(id, opts) {
382
+ return setBudgetStatusV2(id, "active", opts);
383
+ }
384
+ export function suspendBudgetV2(id, opts) {
385
+ return setBudgetStatusV2(id, "suspended", opts);
386
+ }
387
+ export function archiveBudgetV2(id, opts) {
388
+ return setBudgetStatusV2(id, "archived", opts);
389
+ }
390
+
391
+ export function touchBudgetV2(id, { now = Date.now() } = {}) {
392
+ const b = _budgetsV2.get(id);
393
+ if (!b) throw new Error(`budget ${id} not found`);
394
+ b.lastSeenAt = now;
395
+ return _copyBudgetV2(b);
396
+ }
397
+
398
+ export function createUsageRecordV2(
399
+ id,
400
+ { budgetId, units, metadata = {}, now = Date.now() } = {},
401
+ ) {
402
+ if (!id || typeof id !== "string") throw new Error("id must be a string");
403
+ if (!budgetId || typeof budgetId !== "string")
404
+ throw new Error("budgetId must be a string");
405
+ if (!Number.isFinite(units) || units < 0)
406
+ throw new Error("units must be a non-negative finite number");
407
+ if (_recordsV2.has(id)) throw new Error(`record ${id} already exists`);
408
+ const count = getPendingRecordCountV2(budgetId);
409
+ if (count >= _maxPendingRecordsPerBudgetV2)
410
+ throw new Error(
411
+ `budget ${budgetId} already at pending-record cap (${_maxPendingRecordsPerBudgetV2})`,
412
+ );
413
+ const r = {
414
+ id,
415
+ budgetId,
416
+ units,
417
+ status: "pending",
418
+ createdAt: now,
419
+ lastSeenAt: now,
420
+ recordedAt: null,
421
+ settledAt: null,
422
+ metadata: { ...metadata },
423
+ };
424
+ _recordsV2.set(id, r);
425
+ return _copyRecordV2(r);
426
+ }
427
+
428
+ export function getUsageRecordV2(id) {
429
+ const r = _recordsV2.get(id);
430
+ return r ? _copyRecordV2(r) : null;
431
+ }
432
+
433
+ export function listUsageRecordsV2({ budgetId, status } = {}) {
434
+ const out = [];
435
+ for (const r of _recordsV2.values()) {
436
+ if (budgetId && r.budgetId !== budgetId) continue;
437
+ if (status && r.status !== status) continue;
438
+ out.push(_copyRecordV2(r));
439
+ }
440
+ return out;
441
+ }
442
+
443
+ export function setUsageRecordStatusV2(id, next, { now = Date.now() } = {}) {
444
+ const r = _recordsV2.get(id);
445
+ if (!r) throw new Error(`record ${id} not found`);
446
+ if (!RECORD_TRANSITIONS_V2.has(next))
447
+ throw new Error(`unknown record status: ${next}`);
448
+ if (RECORD_TERMINALS_V2.has(r.status))
449
+ throw new Error(`record ${id} is in terminal state ${r.status}`);
450
+ const allowed = RECORD_TRANSITIONS_V2.get(r.status);
451
+ if (!allowed.has(next))
452
+ throw new Error(`cannot transition record from ${r.status} to ${next}`);
453
+ if (next === "recorded" && !r.recordedAt) r.recordedAt = now;
454
+ if (RECORD_TERMINALS_V2.has(next) && !r.settledAt) r.settledAt = now;
455
+ r.status = next;
456
+ r.lastSeenAt = now;
457
+ return _copyRecordV2(r);
458
+ }
459
+
460
+ export function recordUsageV2(id, opts) {
461
+ return setUsageRecordStatusV2(id, "recorded", opts);
462
+ }
463
+ export function billUsageV2(id, opts) {
464
+ return setUsageRecordStatusV2(id, "billed", opts);
465
+ }
466
+ export function rejectUsageV2(id, opts) {
467
+ return setUsageRecordStatusV2(id, "rejected", opts);
468
+ }
469
+ export function refundUsageV2(id, opts) {
470
+ return setUsageRecordStatusV2(id, "refunded", opts);
471
+ }
472
+
473
+ export function autoSuspendIdleBudgetsV2({ now = Date.now() } = {}) {
474
+ const flipped = [];
475
+ for (const b of _budgetsV2.values()) {
476
+ if (b.status !== "active") continue;
477
+ if (now - b.lastSeenAt > _budgetIdleMsV2) {
478
+ b.status = "suspended";
479
+ b.lastSeenAt = now;
480
+ flipped.push(_copyBudgetV2(b));
481
+ }
482
+ }
483
+ return flipped;
484
+ }
485
+
486
+ export function autoRejectStaleRecordsV2({ now = Date.now() } = {}) {
487
+ const flipped = [];
488
+ for (const r of _recordsV2.values()) {
489
+ if (r.status !== "pending" && r.status !== "recorded") continue;
490
+ if (now - r.lastSeenAt > _recordStuckMsV2) {
491
+ r.status = "rejected";
492
+ r.lastSeenAt = now;
493
+ if (!r.settledAt) r.settledAt = now;
494
+ flipped.push(_copyRecordV2(r));
495
+ }
496
+ }
497
+ return flipped;
498
+ }
499
+
500
+ export function getTokenTrackerStatsV2() {
501
+ const budgetsByStatus = {};
502
+ for (const v of Object.values(BUDGET_MATURITY_V2)) budgetsByStatus[v] = 0;
503
+ for (const b of _budgetsV2.values()) budgetsByStatus[b.status] += 1;
504
+
505
+ const recordsByStatus = {};
506
+ for (const v of Object.values(USAGE_RECORD_LIFECYCLE_V2))
507
+ recordsByStatus[v] = 0;
508
+ for (const r of _recordsV2.values()) recordsByStatus[r.status] += 1;
509
+
510
+ return {
511
+ totalBudgetsV2: _budgetsV2.size,
512
+ totalRecordsV2: _recordsV2.size,
513
+ maxActiveBudgetsPerOwner: _maxActiveBudgetsPerOwnerV2,
514
+ maxPendingRecordsPerBudget: _maxPendingRecordsPerBudgetV2,
515
+ budgetIdleMs: _budgetIdleMsV2,
516
+ recordStuckMs: _recordStuckMsV2,
517
+ budgetsByStatus,
518
+ recordsByStatus,
519
+ };
520
+ }
521
+
522
+ export function _resetStateTokenTrackerV2() {
523
+ _budgetsV2.clear();
524
+ _recordsV2.clear();
525
+ _maxActiveBudgetsPerOwnerV2 = TOKEN_DEFAULT_MAX_ACTIVE_BUDGETS_PER_OWNER;
526
+ _maxPendingRecordsPerBudgetV2 = TOKEN_DEFAULT_MAX_PENDING_RECORDS_PER_BUDGET;
527
+ _budgetIdleMsV2 = TOKEN_DEFAULT_BUDGET_IDLE_MS;
528
+ _recordStuckMsV2 = TOKEN_DEFAULT_RECORD_STUCK_MS;
529
+ }