chainlesschain 0.132.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 (135) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +230 -0
  3. package/src/commands/activitypub.js +191 -0
  4. package/src/commands/agent.js +601 -0
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +186 -0
  7. package/src/commands/bm25.js +162 -0
  8. package/src/commands/browse.js +225 -0
  9. package/src/commands/ccron.js +178 -0
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +420 -0
  12. package/src/commands/compt.js +176 -0
  13. package/src/commands/consol.js +237 -0
  14. package/src/commands/cowork.js +588 -0
  15. package/src/commands/crosschain.js +216 -0
  16. package/src/commands/dao.js +216 -0
  17. package/src/commands/dlp.js +206 -0
  18. package/src/commands/economy.js +211 -0
  19. package/src/commands/evolution.js +209 -0
  20. package/src/commands/evomap.js +216 -0
  21. package/src/commands/fflag.js +230 -0
  22. package/src/commands/git.js +185 -0
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +210 -0
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +178 -0
  27. package/src/commands/itbudget.js +161 -0
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +201 -0
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +214 -0
  32. package/src/commands/mcpscaf.js +153 -0
  33. package/src/commands/meminj.js +153 -0
  34. package/src/commands/nostr.js +213 -0
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +156 -0
  37. package/src/commands/pdfp.js +160 -0
  38. package/src/commands/perf.js +176 -0
  39. package/src/commands/perm.js +156 -0
  40. package/src/commands/pipeline.js +211 -0
  41. package/src/commands/planmode.js +154 -0
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +166 -0
  44. package/src/commands/recommend.js +185 -0
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +153 -0
  48. package/src/commands/seshsearch.js +149 -0
  49. package/src/commands/seshtail.js +152 -0
  50. package/src/commands/seshu.js +160 -0
  51. package/src/commands/sganal.js +172 -0
  52. package/src/commands/siem.js +207 -0
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +154 -0
  55. package/src/commands/social.js +159 -0
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +157 -0
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +183 -0
  60. package/src/commands/topiccls.js +158 -0
  61. package/src/commands/uprof.js +154 -0
  62. package/src/commands/vcheck.js +172 -0
  63. package/src/commands/webfetch.js +150 -0
  64. package/src/commands/zkp.js +218 -0
  65. package/src/harness/prompt-compressor.js +331 -0
  66. package/src/index.js +101 -1
  67. package/src/lib/a2a-protocol.js +373 -0
  68. package/src/lib/activitypub-bridge.js +343 -0
  69. package/src/lib/agent-economy.js +358 -0
  70. package/src/lib/app-builder.js +338 -0
  71. package/src/lib/audit-logger.js +321 -0
  72. package/src/lib/autonomous-agent.js +341 -0
  73. package/src/lib/bi-engine.js +339 -0
  74. package/src/lib/bm25-search.js +333 -0
  75. package/src/lib/browser-automation.js +352 -0
  76. package/src/lib/chat-core.js +336 -0
  77. package/src/lib/claude-code-bridge.js +341 -0
  78. package/src/lib/compliance-framework-reporter.js +359 -0
  79. package/src/lib/compliance-manager.js +330 -0
  80. package/src/lib/compression-telemetry.js +333 -0
  81. package/src/lib/content-recommender.js +370 -0
  82. package/src/lib/cowork-cron.js +330 -0
  83. package/src/lib/cowork-learning.js +333 -0
  84. package/src/lib/cowork-task-runner.js +362 -0
  85. package/src/lib/cowork-workflow.js +327 -0
  86. package/src/lib/cross-chain.js +365 -0
  87. package/src/lib/dao-governance.js +339 -0
  88. package/src/lib/dlp-engine.js +343 -0
  89. package/src/lib/evolution-system.js +336 -0
  90. package/src/lib/evomap-manager.js +339 -0
  91. package/src/lib/execution-backend.js +351 -0
  92. package/src/lib/feature-flags.js +330 -0
  93. package/src/lib/git-integration.js +343 -0
  94. package/src/lib/hardening-manager.js +341 -0
  95. package/src/lib/hierarchical-memory.js +341 -0
  96. package/src/lib/inference-network.js +362 -0
  97. package/src/lib/iteration-budget.js +357 -0
  98. package/src/lib/knowledge-graph.js +333 -0
  99. package/src/lib/matrix-bridge.js +339 -0
  100. package/src/lib/mcp-scaffold.js +345 -0
  101. package/src/lib/memory-injection.js +320 -0
  102. package/src/lib/nostr-bridge.js +342 -0
  103. package/src/lib/orchestrator.js +350 -0
  104. package/src/lib/pdf-parser.js +330 -0
  105. package/src/lib/perf-tuning.js +364 -0
  106. package/src/lib/permission-engine.js +319 -0
  107. package/src/lib/pipeline-orchestrator.js +345 -0
  108. package/src/lib/plan-mode.js +328 -0
  109. package/src/lib/privacy-computing.js +335 -0
  110. package/src/lib/prompt-compressor.js +1 -10
  111. package/src/lib/reputation-optimizer.js +340 -0
  112. package/src/lib/sandbox-v2.js +327 -0
  113. package/src/lib/service-container.js +342 -0
  114. package/src/lib/session-consolidator.js +352 -0
  115. package/src/lib/session-hooks.js +340 -0
  116. package/src/lib/session-search.js +334 -0
  117. package/src/lib/session-tail.js +320 -0
  118. package/src/lib/session-usage.js +329 -0
  119. package/src/lib/siem-exporter.js +352 -0
  120. package/src/lib/skill-marketplace.js +345 -0
  121. package/src/lib/sla-manager.js +341 -0
  122. package/src/lib/slot-filler.js +333 -0
  123. package/src/lib/social-graph-analytics.js +327 -0
  124. package/src/lib/social-graph.js +304 -0
  125. package/src/lib/stress-tester.js +342 -0
  126. package/src/lib/sub-agent-registry.js +359 -0
  127. package/src/lib/task-model-selector.js +333 -0
  128. package/src/lib/terraform-manager.js +333 -0
  129. package/src/lib/todo-manager.js +339 -0
  130. package/src/lib/token-incentive.js +341 -0
  131. package/src/lib/topic-classifier.js +353 -0
  132. package/src/lib/user-profile.js +325 -0
  133. package/src/lib/version-checker.js +335 -0
  134. package/src/lib/web-fetch.js +322 -0
  135. package/src/lib/zkp-engine.js +342 -0
@@ -1030,3 +1030,344 @@ export function getHardeningStatsV2() {
1030
1030
  baselinesByStatus,
1031
1031
  };
1032
1032
  }
1033
+
1034
+ // =====================================================================
1035
+ // hardening-manager V2 governance overlay (iter17)
1036
+ // =====================================================================
1037
+ export const HARDGOV_PROFILE_MATURITY_V2 = Object.freeze({
1038
+ PENDING: "pending",
1039
+ ACTIVE: "active",
1040
+ DISABLED: "disabled",
1041
+ ARCHIVED: "archived",
1042
+ });
1043
+ export const HARDGOV_SCAN_LIFECYCLE_V2 = Object.freeze({
1044
+ QUEUED: "queued",
1045
+ SCANNING: "scanning",
1046
+ SCANNED: "scanned",
1047
+ FAILED: "failed",
1048
+ CANCELLED: "cancelled",
1049
+ });
1050
+ const _hardgovPTrans = new Map([
1051
+ [
1052
+ HARDGOV_PROFILE_MATURITY_V2.PENDING,
1053
+ new Set([
1054
+ HARDGOV_PROFILE_MATURITY_V2.ACTIVE,
1055
+ HARDGOV_PROFILE_MATURITY_V2.ARCHIVED,
1056
+ ]),
1057
+ ],
1058
+ [
1059
+ HARDGOV_PROFILE_MATURITY_V2.ACTIVE,
1060
+ new Set([
1061
+ HARDGOV_PROFILE_MATURITY_V2.DISABLED,
1062
+ HARDGOV_PROFILE_MATURITY_V2.ARCHIVED,
1063
+ ]),
1064
+ ],
1065
+ [
1066
+ HARDGOV_PROFILE_MATURITY_V2.DISABLED,
1067
+ new Set([
1068
+ HARDGOV_PROFILE_MATURITY_V2.ACTIVE,
1069
+ HARDGOV_PROFILE_MATURITY_V2.ARCHIVED,
1070
+ ]),
1071
+ ],
1072
+ [HARDGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1073
+ ]);
1074
+ const _hardgovPTerminal = new Set([HARDGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1075
+ const _hardgovJTrans = new Map([
1076
+ [
1077
+ HARDGOV_SCAN_LIFECYCLE_V2.QUEUED,
1078
+ new Set([
1079
+ HARDGOV_SCAN_LIFECYCLE_V2.SCANNING,
1080
+ HARDGOV_SCAN_LIFECYCLE_V2.CANCELLED,
1081
+ ]),
1082
+ ],
1083
+ [
1084
+ HARDGOV_SCAN_LIFECYCLE_V2.SCANNING,
1085
+ new Set([
1086
+ HARDGOV_SCAN_LIFECYCLE_V2.SCANNED,
1087
+ HARDGOV_SCAN_LIFECYCLE_V2.FAILED,
1088
+ HARDGOV_SCAN_LIFECYCLE_V2.CANCELLED,
1089
+ ]),
1090
+ ],
1091
+ [HARDGOV_SCAN_LIFECYCLE_V2.SCANNED, new Set()],
1092
+ [HARDGOV_SCAN_LIFECYCLE_V2.FAILED, new Set()],
1093
+ [HARDGOV_SCAN_LIFECYCLE_V2.CANCELLED, new Set()],
1094
+ ]);
1095
+ const _hardgovPsV2 = new Map();
1096
+ const _hardgovJsV2 = new Map();
1097
+ let _hardgovMaxActive = 8,
1098
+ _hardgovMaxPending = 20,
1099
+ _hardgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1100
+ _hardgovStuckMs = 60 * 1000;
1101
+ function _hardgovPos(n, label) {
1102
+ const v = Math.floor(Number(n));
1103
+ if (!Number.isFinite(v) || v <= 0)
1104
+ throw new Error(`${label} must be positive integer`);
1105
+ return v;
1106
+ }
1107
+ function _hardgovCheckP(from, to) {
1108
+ const a = _hardgovPTrans.get(from);
1109
+ if (!a || !a.has(to))
1110
+ throw new Error(`invalid hardgov profile transition ${from} → ${to}`);
1111
+ }
1112
+ function _hardgovCheckJ(from, to) {
1113
+ const a = _hardgovJTrans.get(from);
1114
+ if (!a || !a.has(to))
1115
+ throw new Error(`invalid hardgov scan transition ${from} → ${to}`);
1116
+ }
1117
+ function _hardgovCountActive(owner) {
1118
+ let c = 0;
1119
+ for (const p of _hardgovPsV2.values())
1120
+ if (p.owner === owner && p.status === HARDGOV_PROFILE_MATURITY_V2.ACTIVE)
1121
+ c++;
1122
+ return c;
1123
+ }
1124
+ function _hardgovCountPending(profileId) {
1125
+ let c = 0;
1126
+ for (const j of _hardgovJsV2.values())
1127
+ if (
1128
+ j.profileId === profileId &&
1129
+ (j.status === HARDGOV_SCAN_LIFECYCLE_V2.QUEUED ||
1130
+ j.status === HARDGOV_SCAN_LIFECYCLE_V2.SCANNING)
1131
+ )
1132
+ c++;
1133
+ return c;
1134
+ }
1135
+ export function setMaxActiveHardgovProfilesPerOwnerV2(n) {
1136
+ _hardgovMaxActive = _hardgovPos(n, "maxActiveHardgovProfilesPerOwner");
1137
+ }
1138
+ export function getMaxActiveHardgovProfilesPerOwnerV2() {
1139
+ return _hardgovMaxActive;
1140
+ }
1141
+ export function setMaxPendingHardgovScansPerProfileV2(n) {
1142
+ _hardgovMaxPending = _hardgovPos(n, "maxPendingHardgovScansPerProfile");
1143
+ }
1144
+ export function getMaxPendingHardgovScansPerProfileV2() {
1145
+ return _hardgovMaxPending;
1146
+ }
1147
+ export function setHardgovProfileIdleMsV2(n) {
1148
+ _hardgovIdleMs = _hardgovPos(n, "hardgovProfileIdleMs");
1149
+ }
1150
+ export function getHardgovProfileIdleMsV2() {
1151
+ return _hardgovIdleMs;
1152
+ }
1153
+ export function setHardgovScanStuckMsV2(n) {
1154
+ _hardgovStuckMs = _hardgovPos(n, "hardgovScanStuckMs");
1155
+ }
1156
+ export function getHardgovScanStuckMsV2() {
1157
+ return _hardgovStuckMs;
1158
+ }
1159
+ export function _resetStateHardeningManagerV2() {
1160
+ _hardgovPsV2.clear();
1161
+ _hardgovJsV2.clear();
1162
+ _hardgovMaxActive = 8;
1163
+ _hardgovMaxPending = 20;
1164
+ _hardgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1165
+ _hardgovStuckMs = 60 * 1000;
1166
+ }
1167
+ export function registerHardgovProfileV2({
1168
+ id,
1169
+ owner,
1170
+ category,
1171
+ metadata,
1172
+ } = {}) {
1173
+ if (!id || !owner) throw new Error("id and owner required");
1174
+ if (_hardgovPsV2.has(id))
1175
+ throw new Error(`hardgov profile ${id} already exists`);
1176
+ const now = Date.now();
1177
+ const p = {
1178
+ id,
1179
+ owner,
1180
+ category: category || "system",
1181
+ status: HARDGOV_PROFILE_MATURITY_V2.PENDING,
1182
+ createdAt: now,
1183
+ updatedAt: now,
1184
+ lastTouchedAt: now,
1185
+ activatedAt: null,
1186
+ archivedAt: null,
1187
+ metadata: { ...(metadata || {}) },
1188
+ };
1189
+ _hardgovPsV2.set(id, p);
1190
+ return { ...p, metadata: { ...p.metadata } };
1191
+ }
1192
+ export function activateHardgovProfileV2(id) {
1193
+ const p = _hardgovPsV2.get(id);
1194
+ if (!p) throw new Error(`hardgov profile ${id} not found`);
1195
+ const isInitial = p.status === HARDGOV_PROFILE_MATURITY_V2.PENDING;
1196
+ _hardgovCheckP(p.status, HARDGOV_PROFILE_MATURITY_V2.ACTIVE);
1197
+ if (isInitial && _hardgovCountActive(p.owner) >= _hardgovMaxActive)
1198
+ throw new Error(`max active hardgov profiles for owner ${p.owner} reached`);
1199
+ const now = Date.now();
1200
+ p.status = HARDGOV_PROFILE_MATURITY_V2.ACTIVE;
1201
+ p.updatedAt = now;
1202
+ p.lastTouchedAt = now;
1203
+ if (!p.activatedAt) p.activatedAt = now;
1204
+ return { ...p, metadata: { ...p.metadata } };
1205
+ }
1206
+ export function disableHardgovProfileV2(id) {
1207
+ const p = _hardgovPsV2.get(id);
1208
+ if (!p) throw new Error(`hardgov profile ${id} not found`);
1209
+ _hardgovCheckP(p.status, HARDGOV_PROFILE_MATURITY_V2.DISABLED);
1210
+ p.status = HARDGOV_PROFILE_MATURITY_V2.DISABLED;
1211
+ p.updatedAt = Date.now();
1212
+ return { ...p, metadata: { ...p.metadata } };
1213
+ }
1214
+ export function archiveHardgovProfileV2(id) {
1215
+ const p = _hardgovPsV2.get(id);
1216
+ if (!p) throw new Error(`hardgov profile ${id} not found`);
1217
+ _hardgovCheckP(p.status, HARDGOV_PROFILE_MATURITY_V2.ARCHIVED);
1218
+ const now = Date.now();
1219
+ p.status = HARDGOV_PROFILE_MATURITY_V2.ARCHIVED;
1220
+ p.updatedAt = now;
1221
+ if (!p.archivedAt) p.archivedAt = now;
1222
+ return { ...p, metadata: { ...p.metadata } };
1223
+ }
1224
+ export function touchHardgovProfileV2(id) {
1225
+ const p = _hardgovPsV2.get(id);
1226
+ if (!p) throw new Error(`hardgov profile ${id} not found`);
1227
+ if (_hardgovPTerminal.has(p.status))
1228
+ throw new Error(`cannot touch terminal hardgov profile ${id}`);
1229
+ const now = Date.now();
1230
+ p.lastTouchedAt = now;
1231
+ p.updatedAt = now;
1232
+ return { ...p, metadata: { ...p.metadata } };
1233
+ }
1234
+ export function getHardgovProfileV2(id) {
1235
+ const p = _hardgovPsV2.get(id);
1236
+ if (!p) return null;
1237
+ return { ...p, metadata: { ...p.metadata } };
1238
+ }
1239
+ export function listHardgovProfilesV2() {
1240
+ return [..._hardgovPsV2.values()].map((p) => ({
1241
+ ...p,
1242
+ metadata: { ...p.metadata },
1243
+ }));
1244
+ }
1245
+ export function createHardgovScanV2({ id, profileId, target, metadata } = {}) {
1246
+ if (!id || !profileId) throw new Error("id and profileId required");
1247
+ if (_hardgovJsV2.has(id))
1248
+ throw new Error(`hardgov scan ${id} already exists`);
1249
+ if (!_hardgovPsV2.has(profileId))
1250
+ throw new Error(`hardgov profile ${profileId} not found`);
1251
+ if (_hardgovCountPending(profileId) >= _hardgovMaxPending)
1252
+ throw new Error(
1253
+ `max pending hardgov scans for profile ${profileId} reached`,
1254
+ );
1255
+ const now = Date.now();
1256
+ const j = {
1257
+ id,
1258
+ profileId,
1259
+ target: target || "",
1260
+ status: HARDGOV_SCAN_LIFECYCLE_V2.QUEUED,
1261
+ createdAt: now,
1262
+ updatedAt: now,
1263
+ startedAt: null,
1264
+ settledAt: null,
1265
+ metadata: { ...(metadata || {}) },
1266
+ };
1267
+ _hardgovJsV2.set(id, j);
1268
+ return { ...j, metadata: { ...j.metadata } };
1269
+ }
1270
+ export function scanningHardgovScanV2(id) {
1271
+ const j = _hardgovJsV2.get(id);
1272
+ if (!j) throw new Error(`hardgov scan ${id} not found`);
1273
+ _hardgovCheckJ(j.status, HARDGOV_SCAN_LIFECYCLE_V2.SCANNING);
1274
+ const now = Date.now();
1275
+ j.status = HARDGOV_SCAN_LIFECYCLE_V2.SCANNING;
1276
+ j.updatedAt = now;
1277
+ if (!j.startedAt) j.startedAt = now;
1278
+ return { ...j, metadata: { ...j.metadata } };
1279
+ }
1280
+ export function completeScanHardgovV2(id) {
1281
+ const j = _hardgovJsV2.get(id);
1282
+ if (!j) throw new Error(`hardgov scan ${id} not found`);
1283
+ _hardgovCheckJ(j.status, HARDGOV_SCAN_LIFECYCLE_V2.SCANNED);
1284
+ const now = Date.now();
1285
+ j.status = HARDGOV_SCAN_LIFECYCLE_V2.SCANNED;
1286
+ j.updatedAt = now;
1287
+ if (!j.settledAt) j.settledAt = now;
1288
+ return { ...j, metadata: { ...j.metadata } };
1289
+ }
1290
+ export function failHardgovScanV2(id, reason) {
1291
+ const j = _hardgovJsV2.get(id);
1292
+ if (!j) throw new Error(`hardgov scan ${id} not found`);
1293
+ _hardgovCheckJ(j.status, HARDGOV_SCAN_LIFECYCLE_V2.FAILED);
1294
+ const now = Date.now();
1295
+ j.status = HARDGOV_SCAN_LIFECYCLE_V2.FAILED;
1296
+ j.updatedAt = now;
1297
+ if (!j.settledAt) j.settledAt = now;
1298
+ if (reason) j.metadata.failReason = String(reason);
1299
+ return { ...j, metadata: { ...j.metadata } };
1300
+ }
1301
+ export function cancelHardgovScanV2(id, reason) {
1302
+ const j = _hardgovJsV2.get(id);
1303
+ if (!j) throw new Error(`hardgov scan ${id} not found`);
1304
+ _hardgovCheckJ(j.status, HARDGOV_SCAN_LIFECYCLE_V2.CANCELLED);
1305
+ const now = Date.now();
1306
+ j.status = HARDGOV_SCAN_LIFECYCLE_V2.CANCELLED;
1307
+ j.updatedAt = now;
1308
+ if (!j.settledAt) j.settledAt = now;
1309
+ if (reason) j.metadata.cancelReason = String(reason);
1310
+ return { ...j, metadata: { ...j.metadata } };
1311
+ }
1312
+ export function getHardgovScanV2(id) {
1313
+ const j = _hardgovJsV2.get(id);
1314
+ if (!j) return null;
1315
+ return { ...j, metadata: { ...j.metadata } };
1316
+ }
1317
+ export function listHardgovScansV2() {
1318
+ return [..._hardgovJsV2.values()].map((j) => ({
1319
+ ...j,
1320
+ metadata: { ...j.metadata },
1321
+ }));
1322
+ }
1323
+ export function autoDisableIdleHardgovProfilesV2({ now } = {}) {
1324
+ const t = now ?? Date.now();
1325
+ const flipped = [];
1326
+ for (const p of _hardgovPsV2.values())
1327
+ if (
1328
+ p.status === HARDGOV_PROFILE_MATURITY_V2.ACTIVE &&
1329
+ t - p.lastTouchedAt >= _hardgovIdleMs
1330
+ ) {
1331
+ p.status = HARDGOV_PROFILE_MATURITY_V2.DISABLED;
1332
+ p.updatedAt = t;
1333
+ flipped.push(p.id);
1334
+ }
1335
+ return { flipped, count: flipped.length };
1336
+ }
1337
+ export function autoFailStuckHardgovScansV2({ now } = {}) {
1338
+ const t = now ?? Date.now();
1339
+ const flipped = [];
1340
+ for (const j of _hardgovJsV2.values())
1341
+ if (
1342
+ j.status === HARDGOV_SCAN_LIFECYCLE_V2.SCANNING &&
1343
+ j.startedAt != null &&
1344
+ t - j.startedAt >= _hardgovStuckMs
1345
+ ) {
1346
+ j.status = HARDGOV_SCAN_LIFECYCLE_V2.FAILED;
1347
+ j.updatedAt = t;
1348
+ if (!j.settledAt) j.settledAt = t;
1349
+ j.metadata.failReason = "auto-fail-stuck";
1350
+ flipped.push(j.id);
1351
+ }
1352
+ return { flipped, count: flipped.length };
1353
+ }
1354
+ export function getHardeningManagerGovStatsV2() {
1355
+ const profilesByStatus = {};
1356
+ for (const v of Object.values(HARDGOV_PROFILE_MATURITY_V2))
1357
+ profilesByStatus[v] = 0;
1358
+ for (const p of _hardgovPsV2.values()) profilesByStatus[p.status]++;
1359
+ const scansByStatus = {};
1360
+ for (const v of Object.values(HARDGOV_SCAN_LIFECYCLE_V2))
1361
+ scansByStatus[v] = 0;
1362
+ for (const j of _hardgovJsV2.values()) scansByStatus[j.status]++;
1363
+ return {
1364
+ totalHardgovProfilesV2: _hardgovPsV2.size,
1365
+ totalHardgovScansV2: _hardgovJsV2.size,
1366
+ maxActiveHardgovProfilesPerOwner: _hardgovMaxActive,
1367
+ maxPendingHardgovScansPerProfile: _hardgovMaxPending,
1368
+ hardgovProfileIdleMs: _hardgovIdleMs,
1369
+ hardgovScanStuckMs: _hardgovStuckMs,
1370
+ profilesByStatus,
1371
+ scansByStatus,
1372
+ };
1373
+ }
@@ -1068,3 +1068,344 @@ export function _resetV2State() {
1068
1068
  _consolidationTimer = null;
1069
1069
  }
1070
1070
  }
1071
+
1072
+ // ===== V2 Surface: Hierarchical Memory governance overlay (CLI v0.137.0) =====
1073
+ export const HMEM_TIER_MATURITY_V2 = Object.freeze({
1074
+ PENDING: "pending",
1075
+ ACTIVE: "active",
1076
+ DORMANT: "dormant",
1077
+ RETIRED: "retired",
1078
+ });
1079
+ export const HMEM_PROMOTION_LIFECYCLE_V2 = Object.freeze({
1080
+ QUEUED: "queued",
1081
+ PROMOTING: "promoting",
1082
+ PROMOTED: "promoted",
1083
+ FAILED: "failed",
1084
+ CANCELLED: "cancelled",
1085
+ });
1086
+
1087
+ const _hmemTierTrans = new Map([
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
+ ],
1100
+ [HMEM_TIER_MATURITY_V2.RETIRED, new Set()],
1101
+ ]);
1102
+ const _hmemTierTerminal = new Set([HMEM_TIER_MATURITY_V2.RETIRED]);
1103
+ const _hmemPromoTrans = new Map([
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
+ ],
1119
+ [HMEM_PROMOTION_LIFECYCLE_V2.PROMOTED, new Set()],
1120
+ [HMEM_PROMOTION_LIFECYCLE_V2.FAILED, new Set()],
1121
+ [HMEM_PROMOTION_LIFECYCLE_V2.CANCELLED, new Set()],
1122
+ ]);
1123
+
1124
+ const _hmemTiers = new Map();
1125
+ const _hmemPromos = new Map();
1126
+ let _hmemMaxActivePerOwner = 12;
1127
+ let _hmemMaxPendingPerTier = 30;
1128
+ let _hmemTierIdleMs = 30 * 24 * 60 * 60 * 1000;
1129
+ let _hmemPromoStuckMs = 5 * 60 * 1000;
1130
+
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
+ }
1137
+
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
+ }
1162
+
1163
+ export function _resetStateHierarchicalMemoryV2() {
1164
+ _hmemTiers.clear();
1165
+ _hmemPromos.clear();
1166
+ _hmemMaxActivePerOwner = 12;
1167
+ _hmemMaxPendingPerTier = 30;
1168
+ _hmemTierIdleMs = 30 * 24 * 60 * 60 * 1000;
1169
+ _hmemPromoStuckMs = 5 * 60 * 1000;
1170
+ }
1171
+
1172
+ export function registerHmemTierV2({ id, owner, level, metadata } = {}) {
1173
+ if (!id || typeof id !== "string") throw new Error("id is required");
1174
+ if (!owner || typeof owner !== "string") throw new Error("owner is required");
1175
+ if (_hmemTiers.has(id)) throw new Error(`hmem tier ${id} already registered`);
1176
+ const now = Date.now();
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
+ };
1189
+ _hmemTiers.set(id, t);
1190
+ return { ...t, metadata: { ...t.metadata } };
1191
+ }
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
+ }
1203
+
1204
+ export function activateHmemTierV2(id) {
1205
+ const t = _hmemTiers.get(id);
1206
+ if (!t) throw new Error(`hmem tier ${id} not found`);
1207
+ _hmemCheckT(t.status, HMEM_TIER_MATURITY_V2.ACTIVE);
1208
+ const recovery = t.status === HMEM_TIER_MATURITY_V2.DORMANT;
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;
1254
+ return { ...t, metadata: { ...t.metadata } };
1255
+ }
1256
+ export function listHmemTiersV2() {
1257
+ return [..._hmemTiers.values()].map((t) => ({
1258
+ ...t,
1259
+ metadata: { ...t.metadata },
1260
+ }));
1261
+ }
1262
+
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
+ }
1274
+
1275
+ export function createHmemPromotionV2({ id, tierId, itemKey, metadata } = {}) {
1276
+ if (!id || typeof id !== "string") throw new Error("id is required");
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`);
1281
+ if (!_hmemTiers.has(tierId)) throw new Error(`hmem tier ${tierId} not found`);
1282
+ const pending = _hmemCountPending(tierId);
1283
+ if (pending >= _hmemMaxPendingPerTier)
1284
+ throw new Error(
1285
+ `max pending hmem promotions per tier (${_hmemMaxPendingPerTier}) reached for ${tierId}`,
1286
+ );
1287
+ const now = Date.now();
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
+ };
1299
+ _hmemPromos.set(id, p);
1300
+ return { ...p, metadata: { ...p.metadata } };
1301
+ }
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
+ }
1360
+
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
+ }
1392
+
1393
+ export function getHierarchicalMemoryGovStatsV2() {
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
+ };
1411
+ }