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
@@ -1069,25 +1069,53 @@ export function _resetV2State() {
1069
1069
  }
1070
1070
  }
1071
1071
 
1072
-
1073
1072
  // ===== V2 Surface: Hierarchical Memory governance overlay (CLI v0.137.0) =====
1074
1073
  export const HMEM_TIER_MATURITY_V2 = Object.freeze({
1075
- PENDING: "pending", ACTIVE: "active", DORMANT: "dormant", RETIRED: "retired",
1074
+ PENDING: "pending",
1075
+ ACTIVE: "active",
1076
+ DORMANT: "dormant",
1077
+ RETIRED: "retired",
1076
1078
  });
1077
1079
  export const HMEM_PROMOTION_LIFECYCLE_V2 = Object.freeze({
1078
- QUEUED: "queued", PROMOTING: "promoting", PROMOTED: "promoted", FAILED: "failed", CANCELLED: "cancelled",
1080
+ QUEUED: "queued",
1081
+ PROMOTING: "promoting",
1082
+ PROMOTED: "promoted",
1083
+ FAILED: "failed",
1084
+ CANCELLED: "cancelled",
1079
1085
  });
1080
1086
 
1081
1087
  const _hmemTierTrans = new Map([
1082
- [HMEM_TIER_MATURITY_V2.PENDING, new Set([HMEM_TIER_MATURITY_V2.ACTIVE, HMEM_TIER_MATURITY_V2.RETIRED])],
1083
- [HMEM_TIER_MATURITY_V2.ACTIVE, new Set([HMEM_TIER_MATURITY_V2.DORMANT, HMEM_TIER_MATURITY_V2.RETIRED])],
1084
- [HMEM_TIER_MATURITY_V2.DORMANT, new Set([HMEM_TIER_MATURITY_V2.ACTIVE, HMEM_TIER_MATURITY_V2.RETIRED])],
1088
+ [
1089
+ HMEM_TIER_MATURITY_V2.PENDING,
1090
+ new Set([HMEM_TIER_MATURITY_V2.ACTIVE, HMEM_TIER_MATURITY_V2.RETIRED]),
1091
+ ],
1092
+ [
1093
+ HMEM_TIER_MATURITY_V2.ACTIVE,
1094
+ new Set([HMEM_TIER_MATURITY_V2.DORMANT, HMEM_TIER_MATURITY_V2.RETIRED]),
1095
+ ],
1096
+ [
1097
+ HMEM_TIER_MATURITY_V2.DORMANT,
1098
+ new Set([HMEM_TIER_MATURITY_V2.ACTIVE, HMEM_TIER_MATURITY_V2.RETIRED]),
1099
+ ],
1085
1100
  [HMEM_TIER_MATURITY_V2.RETIRED, new Set()],
1086
1101
  ]);
1087
1102
  const _hmemTierTerminal = new Set([HMEM_TIER_MATURITY_V2.RETIRED]);
1088
1103
  const _hmemPromoTrans = new Map([
1089
- [HMEM_PROMOTION_LIFECYCLE_V2.QUEUED, new Set([HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING, HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED])],
1090
- [HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING, new Set([HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED, HMEM_PROMOTION_LIFECYCLE_V2.FAILED, HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED])],
1104
+ [
1105
+ HMEM_PROMOTION_LIFECYCLE_V2.QUEUED,
1106
+ new Set([
1107
+ HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING,
1108
+ HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED,
1109
+ ]),
1110
+ ],
1111
+ [
1112
+ HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING,
1113
+ new Set([
1114
+ HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED,
1115
+ HMEM_PROMOTION_LIFECYCLE_V2.FAILED,
1116
+ HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED,
1117
+ ]),
1118
+ ],
1091
1119
  [HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED, new Set()],
1092
1120
  [HMEM_PROMOTION_LIFECYCLE_V2.FAILED, new Set()],
1093
1121
  [HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED, new Set()],
@@ -1100,21 +1128,45 @@ let _hmemMaxPendingPerTier = 30;
1100
1128
  let _hmemTierIdleMs = 30 * 24 * 60 * 60 * 1000;
1101
1129
  let _hmemPromoStuckMs = 5 * 60 * 1000;
1102
1130
 
1103
- function _hmemPos(n, lbl) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${lbl} must be positive integer`); return v; }
1131
+ function _hmemPos(n, lbl) {
1132
+ const v = Math.floor(Number(n));
1133
+ if (!Number.isFinite(v) || v <= 0)
1134
+ throw new Error(`${lbl} must be positive integer`);
1135
+ return v;
1136
+ }
1104
1137
 
1105
- export function setMaxActiveHmemTiersPerOwnerV2(n) { _hmemMaxActivePerOwner = _hmemPos(n, "maxActiveHmemTiersPerOwner"); }
1106
- export function getMaxActiveHmemTiersPerOwnerV2() { return _hmemMaxActivePerOwner; }
1107
- export function setMaxPendingHmemPromotionsPerTierV2(n) { _hmemMaxPendingPerTier = _hmemPos(n, "maxPendingHmemPromotionsPerTier"); }
1108
- export function getMaxPendingHmemPromotionsPerTierV2() { return _hmemMaxPendingPerTier; }
1109
- export function setHmemTierIdleMsV2(n) { _hmemTierIdleMs = _hmemPos(n, "hmemTierIdleMs"); }
1110
- export function getHmemTierIdleMsV2() { return _hmemTierIdleMs; }
1111
- export function setHmemPromotionStuckMsV2(n) { _hmemPromoStuckMs = _hmemPos(n, "hmemPromotionStuckMs"); }
1112
- export function getHmemPromotionStuckMsV2() { return _hmemPromoStuckMs; }
1138
+ export function setMaxActiveHmemTiersPerOwnerV2(n) {
1139
+ _hmemMaxActivePerOwner = _hmemPos(n, "maxActiveHmemTiersPerOwner");
1140
+ }
1141
+ export function getMaxActiveHmemTiersPerOwnerV2() {
1142
+ return _hmemMaxActivePerOwner;
1143
+ }
1144
+ export function setMaxPendingHmemPromotionsPerTierV2(n) {
1145
+ _hmemMaxPendingPerTier = _hmemPos(n, "maxPendingHmemPromotionsPerTier");
1146
+ }
1147
+ export function getMaxPendingHmemPromotionsPerTierV2() {
1148
+ return _hmemMaxPendingPerTier;
1149
+ }
1150
+ export function setHmemTierIdleMsV2(n) {
1151
+ _hmemTierIdleMs = _hmemPos(n, "hmemTierIdleMs");
1152
+ }
1153
+ export function getHmemTierIdleMsV2() {
1154
+ return _hmemTierIdleMs;
1155
+ }
1156
+ export function setHmemPromotionStuckMsV2(n) {
1157
+ _hmemPromoStuckMs = _hmemPos(n, "hmemPromotionStuckMs");
1158
+ }
1159
+ export function getHmemPromotionStuckMsV2() {
1160
+ return _hmemPromoStuckMs;
1161
+ }
1113
1162
 
1114
1163
  export function _resetStateHierarchicalMemoryV2() {
1115
- _hmemTiers.clear(); _hmemPromos.clear();
1116
- _hmemMaxActivePerOwner = 12; _hmemMaxPendingPerTier = 30;
1117
- _hmemTierIdleMs = 30 * 24 * 60 * 60 * 1000; _hmemPromoStuckMs = 5 * 60 * 1000;
1164
+ _hmemTiers.clear();
1165
+ _hmemPromos.clear();
1166
+ _hmemMaxActivePerOwner = 12;
1167
+ _hmemMaxPendingPerTier = 30;
1168
+ _hmemTierIdleMs = 30 * 24 * 60 * 60 * 1000;
1169
+ _hmemPromoStuckMs = 5 * 60 * 1000;
1118
1170
  }
1119
1171
 
1120
1172
  export function registerHmemTierV2({ id, owner, level, metadata } = {}) {
@@ -1122,54 +1174,238 @@ export function registerHmemTierV2({ id, owner, level, metadata } = {}) {
1122
1174
  if (!owner || typeof owner !== "string") throw new Error("owner is required");
1123
1175
  if (_hmemTiers.has(id)) throw new Error(`hmem tier ${id} already registered`);
1124
1176
  const now = Date.now();
1125
- const t = { id, owner, level: level || "short-term", status: HMEM_TIER_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, retiredAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
1177
+ const t = {
1178
+ id,
1179
+ owner,
1180
+ level: level || "short-term",
1181
+ status: HMEM_TIER_MATURITY_V2.PENDING,
1182
+ createdAt: now,
1183
+ updatedAt: now,
1184
+ activatedAt: null,
1185
+ retiredAt: null,
1186
+ lastTouchedAt: now,
1187
+ metadata: { ...(metadata || {}) },
1188
+ };
1126
1189
  _hmemTiers.set(id, t);
1127
1190
  return { ...t, metadata: { ...t.metadata } };
1128
1191
  }
1129
- function _hmemCheckT(from, to) { const a = _hmemTierTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid hmem tier transition ${from} → ${to}`); }
1130
- function _hmemCountActive(owner) { let n = 0; for (const t of _hmemTiers.values()) if (t.owner === owner && t.status === HMEM_TIER_MATURITY_V2.ACTIVE) n++; return n; }
1192
+ function _hmemCheckT(from, to) {
1193
+ const a = _hmemTierTrans.get(from);
1194
+ if (!a || !a.has(to))
1195
+ throw new Error(`invalid hmem tier transition ${from} → ${to}`);
1196
+ }
1197
+ function _hmemCountActive(owner) {
1198
+ let n = 0;
1199
+ for (const t of _hmemTiers.values())
1200
+ if (t.owner === owner && t.status === HMEM_TIER_MATURITY_V2.ACTIVE) n++;
1201
+ return n;
1202
+ }
1131
1203
 
1132
1204
  export function activateHmemTierV2(id) {
1133
- const t = _hmemTiers.get(id); if (!t) throw new Error(`hmem tier ${id} not found`);
1205
+ const t = _hmemTiers.get(id);
1206
+ if (!t) throw new Error(`hmem tier ${id} not found`);
1134
1207
  _hmemCheckT(t.status, HMEM_TIER_MATURITY_V2.ACTIVE);
1135
1208
  const recovery = t.status === HMEM_TIER_MATURITY_V2.DORMANT;
1136
- if (!recovery) { const c = _hmemCountActive(t.owner); if (c >= _hmemMaxActivePerOwner) throw new Error(`max active hmem tiers per owner (${_hmemMaxActivePerOwner}) reached for ${t.owner}`); }
1137
- const now = Date.now(); t.status = HMEM_TIER_MATURITY_V2.ACTIVE; t.updatedAt = now; t.lastTouchedAt = now; if (!t.activatedAt) t.activatedAt = now;
1209
+ if (!recovery) {
1210
+ const c = _hmemCountActive(t.owner);
1211
+ if (c >= _hmemMaxActivePerOwner)
1212
+ throw new Error(
1213
+ `max active hmem tiers per owner (${_hmemMaxActivePerOwner}) reached for ${t.owner}`,
1214
+ );
1215
+ }
1216
+ const now = Date.now();
1217
+ t.status = HMEM_TIER_MATURITY_V2.ACTIVE;
1218
+ t.updatedAt = now;
1219
+ t.lastTouchedAt = now;
1220
+ if (!t.activatedAt) t.activatedAt = now;
1221
+ return { ...t, metadata: { ...t.metadata } };
1222
+ }
1223
+ export function dormantHmemTierV2(id) {
1224
+ const t = _hmemTiers.get(id);
1225
+ if (!t) throw new Error(`hmem tier ${id} not found`);
1226
+ _hmemCheckT(t.status, HMEM_TIER_MATURITY_V2.DORMANT);
1227
+ t.status = HMEM_TIER_MATURITY_V2.DORMANT;
1228
+ t.updatedAt = Date.now();
1229
+ return { ...t, metadata: { ...t.metadata } };
1230
+ }
1231
+ export function retireHmemTierV2(id) {
1232
+ const t = _hmemTiers.get(id);
1233
+ if (!t) throw new Error(`hmem tier ${id} not found`);
1234
+ _hmemCheckT(t.status, HMEM_TIER_MATURITY_V2.RETIRED);
1235
+ const now = Date.now();
1236
+ t.status = HMEM_TIER_MATURITY_V2.RETIRED;
1237
+ t.updatedAt = now;
1238
+ if (!t.retiredAt) t.retiredAt = now;
1239
+ return { ...t, metadata: { ...t.metadata } };
1240
+ }
1241
+ export function touchHmemTierV2(id) {
1242
+ const t = _hmemTiers.get(id);
1243
+ if (!t) throw new Error(`hmem tier ${id} not found`);
1244
+ if (_hmemTierTerminal.has(t.status))
1245
+ throw new Error(`cannot touch terminal hmem tier ${id}`);
1246
+ const now = Date.now();
1247
+ t.lastTouchedAt = now;
1248
+ t.updatedAt = now;
1249
+ return { ...t, metadata: { ...t.metadata } };
1250
+ }
1251
+ export function getHmemTierV2(id) {
1252
+ const t = _hmemTiers.get(id);
1253
+ if (!t) return null;
1138
1254
  return { ...t, metadata: { ...t.metadata } };
1139
1255
  }
1140
- export function dormantHmemTierV2(id) { const t = _hmemTiers.get(id); if (!t) throw new Error(`hmem tier ${id} not found`); _hmemCheckT(t.status, HMEM_TIER_MATURITY_V2.DORMANT); t.status = HMEM_TIER_MATURITY_V2.DORMANT; t.updatedAt = Date.now(); return { ...t, metadata: { ...t.metadata } }; }
1141
- export function retireHmemTierV2(id) { const t = _hmemTiers.get(id); if (!t) throw new Error(`hmem tier ${id} not found`); _hmemCheckT(t.status, HMEM_TIER_MATURITY_V2.RETIRED); const now = Date.now(); t.status = HMEM_TIER_MATURITY_V2.RETIRED; t.updatedAt = now; if (!t.retiredAt) t.retiredAt = now; return { ...t, metadata: { ...t.metadata } }; }
1142
- export function touchHmemTierV2(id) { const t = _hmemTiers.get(id); if (!t) throw new Error(`hmem tier ${id} not found`); if (_hmemTierTerminal.has(t.status)) throw new Error(`cannot touch terminal hmem tier ${id}`); const now = Date.now(); t.lastTouchedAt = now; t.updatedAt = now; return { ...t, metadata: { ...t.metadata } }; }
1143
- export function getHmemTierV2(id) { const t = _hmemTiers.get(id); if (!t) return null; return { ...t, metadata: { ...t.metadata } }; }
1144
- export function listHmemTiersV2() { return [..._hmemTiers.values()].map((t) => ({ ...t, metadata: { ...t.metadata } })); }
1256
+ export function listHmemTiersV2() {
1257
+ return [..._hmemTiers.values()].map((t) => ({
1258
+ ...t,
1259
+ metadata: { ...t.metadata },
1260
+ }));
1261
+ }
1145
1262
 
1146
- function _hmemCountPending(tid) { let n = 0; for (const p of _hmemPromos.values()) if (p.tierId === tid && (p.status === HMEM_PROMOTION_LIFECYCLE_V2.QUEUED || p.status === HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING)) n++; return n; }
1263
+ function _hmemCountPending(tid) {
1264
+ let n = 0;
1265
+ for (const p of _hmemPromos.values())
1266
+ if (
1267
+ p.tierId === tid &&
1268
+ (p.status === HMEM_PROMOTION_LIFECYCLE_V2.QUEUED ||
1269
+ p.status === HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING)
1270
+ )
1271
+ n++;
1272
+ return n;
1273
+ }
1147
1274
 
1148
1275
  export function createHmemPromotionV2({ id, tierId, itemKey, metadata } = {}) {
1149
1276
  if (!id || typeof id !== "string") throw new Error("id is required");
1150
- if (!tierId || typeof tierId !== "string") throw new Error("tierId is required");
1151
- if (_hmemPromos.has(id)) throw new Error(`hmem promotion ${id} already exists`);
1277
+ if (!tierId || typeof tierId !== "string")
1278
+ throw new Error("tierId is required");
1279
+ if (_hmemPromos.has(id))
1280
+ throw new Error(`hmem promotion ${id} already exists`);
1152
1281
  if (!_hmemTiers.has(tierId)) throw new Error(`hmem tier ${tierId} not found`);
1153
1282
  const pending = _hmemCountPending(tierId);
1154
- if (pending >= _hmemMaxPendingPerTier) throw new Error(`max pending hmem promotions per tier (${_hmemMaxPendingPerTier}) reached for ${tierId}`);
1283
+ if (pending >= _hmemMaxPendingPerTier)
1284
+ throw new Error(
1285
+ `max pending hmem promotions per tier (${_hmemMaxPendingPerTier}) reached for ${tierId}`,
1286
+ );
1155
1287
  const now = Date.now();
1156
- const p = { id, tierId, itemKey: itemKey || "", status: HMEM_PROMOTION_LIFECYCLE_V2.QUEUED, createdAt: now, updatedAt: now, startedAt: null, settledAt: null, metadata: { ...(metadata || {}) } };
1288
+ const p = {
1289
+ id,
1290
+ tierId,
1291
+ itemKey: itemKey || "",
1292
+ status: HMEM_PROMOTION_LIFECYCLE_V2.QUEUED,
1293
+ createdAt: now,
1294
+ updatedAt: now,
1295
+ startedAt: null,
1296
+ settledAt: null,
1297
+ metadata: { ...(metadata || {}) },
1298
+ };
1157
1299
  _hmemPromos.set(id, p);
1158
1300
  return { ...p, metadata: { ...p.metadata } };
1159
1301
  }
1160
- function _hmemCheckP(from, to) { const a = _hmemPromoTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid hmem promotion transition ${from} → ${to}`); }
1161
- export function startHmemPromotionV2(id) { const p = _hmemPromos.get(id); if (!p) throw new Error(`hmem promotion ${id} not found`); _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING); const now = Date.now(); p.status = HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING; p.updatedAt = now; if (!p.startedAt) p.startedAt = now; return { ...p, metadata: { ...p.metadata } }; }
1162
- export function completeHmemPromotionV2(id) { const p = _hmemPromos.get(id); if (!p) throw new Error(`hmem promotion ${id} not found`); _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED); const now = Date.now(); p.status = HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED; p.updatedAt = now; if (!p.settledAt) p.settledAt = now; return { ...p, metadata: { ...p.metadata } }; }
1163
- export function failHmemPromotionV2(id, reason) { const p = _hmemPromos.get(id); if (!p) throw new Error(`hmem promotion ${id} not found`); _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.FAILED); const now = Date.now(); p.status = HMEM_PROMOTION_LIFECYCLE_V2.FAILED; p.updatedAt = now; if (!p.settledAt) p.settledAt = now; if (reason) p.metadata.failReason = String(reason); return { ...p, metadata: { ...p.metadata } }; }
1164
- export function cancelHmemPromotionV2(id, reason) { const p = _hmemPromos.get(id); if (!p) throw new Error(`hmem promotion ${id} not found`); _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED); const now = Date.now(); p.status = HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED; p.updatedAt = now; if (!p.settledAt) p.settledAt = now; if (reason) p.metadata.cancelReason = String(reason); return { ...p, metadata: { ...p.metadata } }; }
1165
- export function getHmemPromotionV2(id) { const p = _hmemPromos.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
1166
- export function listHmemPromotionsV2() { return [..._hmemPromos.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
1302
+ function _hmemCheckP(from, to) {
1303
+ const a = _hmemPromoTrans.get(from);
1304
+ if (!a || !a.has(to))
1305
+ throw new Error(`invalid hmem promotion transition ${from} ${to}`);
1306
+ }
1307
+ export function startHmemPromotionV2(id) {
1308
+ const p = _hmemPromos.get(id);
1309
+ if (!p) throw new Error(`hmem promotion ${id} not found`);
1310
+ _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING);
1311
+ const now = Date.now();
1312
+ p.status = HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING;
1313
+ p.updatedAt = now;
1314
+ if (!p.startedAt) p.startedAt = now;
1315
+ return { ...p, metadata: { ...p.metadata } };
1316
+ }
1317
+ export function completeHmemPromotionV2(id) {
1318
+ const p = _hmemPromos.get(id);
1319
+ if (!p) throw new Error(`hmem promotion ${id} not found`);
1320
+ _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED);
1321
+ const now = Date.now();
1322
+ p.status = HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED;
1323
+ p.updatedAt = now;
1324
+ if (!p.settledAt) p.settledAt = now;
1325
+ return { ...p, metadata: { ...p.metadata } };
1326
+ }
1327
+ export function failHmemPromotionV2(id, reason) {
1328
+ const p = _hmemPromos.get(id);
1329
+ if (!p) throw new Error(`hmem promotion ${id} not found`);
1330
+ _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.FAILED);
1331
+ const now = Date.now();
1332
+ p.status = HMEM_PROMOTION_LIFECYCLE_V2.FAILED;
1333
+ p.updatedAt = now;
1334
+ if (!p.settledAt) p.settledAt = now;
1335
+ if (reason) p.metadata.failReason = String(reason);
1336
+ return { ...p, metadata: { ...p.metadata } };
1337
+ }
1338
+ export function cancelHmemPromotionV2(id, reason) {
1339
+ const p = _hmemPromos.get(id);
1340
+ if (!p) throw new Error(`hmem promotion ${id} not found`);
1341
+ _hmemCheckP(p.status, HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED);
1342
+ const now = Date.now();
1343
+ p.status = HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED;
1344
+ p.updatedAt = now;
1345
+ if (!p.settledAt) p.settledAt = now;
1346
+ if (reason) p.metadata.cancelReason = String(reason);
1347
+ return { ...p, metadata: { ...p.metadata } };
1348
+ }
1349
+ export function getHmemPromotionV2(id) {
1350
+ const p = _hmemPromos.get(id);
1351
+ if (!p) return null;
1352
+ return { ...p, metadata: { ...p.metadata } };
1353
+ }
1354
+ export function listHmemPromotionsV2() {
1355
+ return [..._hmemPromos.values()].map((p) => ({
1356
+ ...p,
1357
+ metadata: { ...p.metadata },
1358
+ }));
1359
+ }
1167
1360
 
1168
- export function autoDormantIdleHmemTiersV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const tier of _hmemTiers.values()) if (tier.status === HMEM_TIER_MATURITY_V2.ACTIVE && (t - tier.lastTouchedAt) >= _hmemTierIdleMs) { tier.status = HMEM_TIER_MATURITY_V2.DORMANT; tier.updatedAt = t; flipped.push(tier.id); } return { flipped, count: flipped.length }; }
1169
- export function autoFailStuckHmemPromotionsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _hmemPromos.values()) if (p.status === HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING && p.startedAt != null && (t - p.startedAt) >= _hmemPromoStuckMs) { p.status = HMEM_PROMOTION_LIFECYCLE_V2.FAILED; p.updatedAt = t; if (!p.settledAt) p.settledAt = t; p.metadata.failReason = "auto-fail-stuck"; flipped.push(p.id); } return { flipped, count: flipped.length }; }
1361
+ export function autoDormantIdleHmemTiersV2({ now } = {}) {
1362
+ const t = now ?? Date.now();
1363
+ const flipped = [];
1364
+ for (const tier of _hmemTiers.values())
1365
+ if (
1366
+ tier.status === HMEM_TIER_MATURITY_V2.ACTIVE &&
1367
+ t - tier.lastTouchedAt >= _hmemTierIdleMs
1368
+ ) {
1369
+ tier.status = HMEM_TIER_MATURITY_V2.DORMANT;
1370
+ tier.updatedAt = t;
1371
+ flipped.push(tier.id);
1372
+ }
1373
+ return { flipped, count: flipped.length };
1374
+ }
1375
+ export function autoFailStuckHmemPromotionsV2({ now } = {}) {
1376
+ const t = now ?? Date.now();
1377
+ const flipped = [];
1378
+ for (const p of _hmemPromos.values())
1379
+ if (
1380
+ p.status === HMEM_PROMOTION_LIFECYCLE_V2.PROMOTING &&
1381
+ p.startedAt != null &&
1382
+ t - p.startedAt >= _hmemPromoStuckMs
1383
+ ) {
1384
+ p.status = HMEM_PROMOTION_LIFECYCLE_V2.FAILED;
1385
+ p.updatedAt = t;
1386
+ if (!p.settledAt) p.settledAt = t;
1387
+ p.metadata.failReason = "auto-fail-stuck";
1388
+ flipped.push(p.id);
1389
+ }
1390
+ return { flipped, count: flipped.length };
1391
+ }
1170
1392
 
1171
1393
  export function getHierarchicalMemoryGovStatsV2() {
1172
- const tiersByStatus = {}; for (const s of Object.values(HMEM_TIER_MATURITY_V2)) tiersByStatus[s] = 0; for (const t of _hmemTiers.values()) tiersByStatus[t.status]++;
1173
- const promotionsByStatus = {}; for (const s of Object.values(HMEM_PROMOTION_LIFECYCLE_V2)) promotionsByStatus[s] = 0; for (const p of _hmemPromos.values()) promotionsByStatus[p.status]++;
1174
- return { totalTiersV2: _hmemTiers.size, totalPromotionsV2: _hmemPromos.size, maxActiveHmemTiersPerOwner: _hmemMaxActivePerOwner, maxPendingHmemPromotionsPerTier: _hmemMaxPendingPerTier, hmemTierIdleMs: _hmemTierIdleMs, hmemPromotionStuckMs: _hmemPromoStuckMs, tiersByStatus, promotionsByStatus };
1394
+ const tiersByStatus = {};
1395
+ for (const s of Object.values(HMEM_TIER_MATURITY_V2)) tiersByStatus[s] = 0;
1396
+ for (const t of _hmemTiers.values()) tiersByStatus[t.status]++;
1397
+ const promotionsByStatus = {};
1398
+ for (const s of Object.values(HMEM_PROMOTION_LIFECYCLE_V2))
1399
+ promotionsByStatus[s] = 0;
1400
+ for (const p of _hmemPromos.values()) promotionsByStatus[p.status]++;
1401
+ return {
1402
+ totalTiersV2: _hmemTiers.size,
1403
+ totalPromotionsV2: _hmemPromos.size,
1404
+ maxActiveHmemTiersPerOwner: _hmemMaxActivePerOwner,
1405
+ maxPendingHmemPromotionsPerTier: _hmemMaxPendingPerTier,
1406
+ hmemTierIdleMs: _hmemTierIdleMs,
1407
+ hmemPromotionStuckMs: _hmemPromoStuckMs,
1408
+ tiersByStatus,
1409
+ promotionsByStatus,
1410
+ };
1175
1411
  }