chainlesschain 0.145.0 → 0.156.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 (184) hide show
  1. package/README.md +52 -3
  2. package/package.json +1 -1
  3. package/src/commands/a2a.js +201 -0
  4. package/src/commands/activitypub.js +207 -0
  5. package/src/commands/agent-network.js +217 -0
  6. package/src/commands/agent.js +1250 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +203 -0
  9. package/src/commands/browse.js +213 -0
  10. package/src/commands/chat.js +605 -0
  11. package/src/commands/cli-anything.js +426 -0
  12. package/src/commands/codegen.js +207 -0
  13. package/src/commands/collab.js +211 -0
  14. package/src/commands/compliance.js +822 -0
  15. package/src/commands/config.js +213 -0
  16. package/src/commands/cowork.js +1666 -0
  17. package/src/commands/crosschain.js +203 -0
  18. package/src/commands/dao.js +203 -0
  19. package/src/commands/dbevo.js +227 -0
  20. package/src/commands/dev.js +207 -0
  21. package/src/commands/did-v2.js +217 -0
  22. package/src/commands/did.js +221 -0
  23. package/src/commands/dlp.js +213 -0
  24. package/src/commands/economy.js +199 -0
  25. package/src/commands/encrypt.js +201 -0
  26. package/src/commands/evolution.js +199 -0
  27. package/src/commands/evomap.js +830 -0
  28. package/src/commands/export.js +213 -0
  29. package/src/commands/federation.js +209 -0
  30. package/src/commands/fusion.js +205 -0
  31. package/src/commands/governance.js +209 -0
  32. package/src/commands/hmemory.js +203 -0
  33. package/src/commands/hook.js +209 -0
  34. package/src/commands/import.js +209 -0
  35. package/src/commands/inference.js +207 -0
  36. package/src/commands/infra.js +203 -0
  37. package/src/commands/instinct.js +209 -0
  38. package/src/commands/ipfs.js +207 -0
  39. package/src/commands/kg.js +195 -0
  40. package/src/commands/llm.js +426 -0
  41. package/src/commands/matrix.js +207 -0
  42. package/src/commands/mcp.js +217 -0
  43. package/src/commands/memory.js +412 -0
  44. package/src/commands/multimodal.js +203 -0
  45. package/src/commands/nlprog.js +225 -0
  46. package/src/commands/nostr.js +209 -0
  47. package/src/commands/note.js +205 -0
  48. package/src/commands/ops.js +219 -0
  49. package/src/commands/orchestrate.js +406 -0
  50. package/src/commands/org.js +209 -0
  51. package/src/commands/p2p.js +209 -0
  52. package/src/commands/perception.js +209 -0
  53. package/src/commands/permmem.js +203 -0
  54. package/src/commands/pipeline.js +199 -0
  55. package/src/commands/planmode.js +426 -0
  56. package/src/commands/plugin-ecosystem.js +209 -0
  57. package/src/commands/plugin.js +209 -0
  58. package/src/commands/pqc.js +213 -0
  59. package/src/commands/quantization.js +207 -0
  60. package/src/commands/rcache.js +205 -0
  61. package/src/commands/recommend.js +233 -0
  62. package/src/commands/runtime.js +205 -0
  63. package/src/commands/scim.js +209 -0
  64. package/src/commands/services.js +207 -0
  65. package/src/commands/session.js +209 -0
  66. package/src/commands/setup.js +205 -0
  67. package/src/commands/skill.js +414 -0
  68. package/src/commands/social.js +201 -0
  69. package/src/commands/sso.js +209 -0
  70. package/src/commands/start.js +209 -0
  71. package/src/commands/stream.js +213 -0
  72. package/src/commands/sync.js +209 -0
  73. package/src/commands/tech.js +209 -0
  74. package/src/commands/tenant.js +217 -0
  75. package/src/commands/tokens.js +209 -0
  76. package/src/commands/trust.js +217 -0
  77. package/src/commands/ui.js +225 -0
  78. package/src/commands/wallet.js +209 -0
  79. package/src/commands/workflow.js +412 -0
  80. package/src/index.js +252 -0
  81. package/src/lib/a2a-protocol.js +332 -0
  82. package/src/lib/activitypub-bridge.js +334 -0
  83. package/src/lib/agent-coordinator.js +334 -0
  84. package/src/lib/agent-economy.js +334 -0
  85. package/src/lib/agent-network.js +341 -0
  86. package/src/lib/agent-router.js +333 -0
  87. package/src/lib/aiops.js +346 -0
  88. package/src/lib/automation-engine.js +335 -0
  89. package/src/lib/autonomous-agent.js +332 -0
  90. package/src/lib/autonomous-developer.js +332 -0
  91. package/src/lib/bi-engine.js +333 -0
  92. package/src/lib/browser-automation.js +334 -0
  93. package/src/lib/chat-core.js +335 -0
  94. package/src/lib/cli-anything-bridge.js +341 -0
  95. package/src/lib/cli-context-engineering.js +351 -0
  96. package/src/lib/code-agent.js +339 -0
  97. package/src/lib/collaboration-governance.js +334 -0
  98. package/src/lib/community-governance.js +346 -0
  99. package/src/lib/compliance-manager.js +334 -0
  100. package/src/lib/content-recommendation.js +351 -0
  101. package/src/lib/cowork-adapter.js +336 -0
  102. package/src/lib/cowork-evomap-adapter.js +341 -0
  103. package/src/lib/cowork-mcp-tools.js +341 -0
  104. package/src/lib/cowork-observe-html.js +341 -0
  105. package/src/lib/cowork-observe.js +341 -0
  106. package/src/lib/cowork-share.js +338 -0
  107. package/src/lib/cowork-task-templates.js +342 -1
  108. package/src/lib/cowork-template-marketplace.js +340 -0
  109. package/src/lib/cross-chain.js +339 -0
  110. package/src/lib/crypto-manager.js +334 -0
  111. package/src/lib/dao-governance.js +339 -0
  112. package/src/lib/dbevo.js +351 -0
  113. package/src/lib/decentral-infra.js +330 -0
  114. package/src/lib/did-manager.js +341 -0
  115. package/src/lib/did-v2-manager.js +341 -0
  116. package/src/lib/dlp-engine.js +339 -0
  117. package/src/lib/downloader.js +334 -0
  118. package/src/lib/evolution-system.js +334 -0
  119. package/src/lib/evomap-client.js +342 -0
  120. package/src/lib/evomap-federation.js +338 -0
  121. package/src/lib/evomap-governance.js +334 -0
  122. package/src/lib/evomap-manager.js +330 -0
  123. package/src/lib/execution-backend.js +330 -0
  124. package/src/lib/federation-hardening.js +340 -0
  125. package/src/lib/hashline.js +338 -0
  126. package/src/lib/hierarchical-memory.js +334 -0
  127. package/src/lib/hook-manager.js +341 -0
  128. package/src/lib/inference-network.js +341 -0
  129. package/src/lib/instinct-manager.js +346 -0
  130. package/src/lib/interaction-adapter.js +330 -0
  131. package/src/lib/interactive-planner.js +354 -0
  132. package/src/lib/ipfs-storage.js +334 -0
  133. package/src/lib/knowledge-exporter.js +341 -0
  134. package/src/lib/knowledge-graph.js +331 -0
  135. package/src/lib/knowledge-importer.js +341 -0
  136. package/src/lib/llm-providers.js +346 -0
  137. package/src/lib/matrix-bridge.js +339 -0
  138. package/src/lib/mcp-registry.js +346 -0
  139. package/src/lib/memory-manager.js +336 -0
  140. package/src/lib/multimodal.js +330 -0
  141. package/src/lib/nl-programming.js +341 -0
  142. package/src/lib/nostr-bridge.js +336 -0
  143. package/src/lib/note-versioning.js +339 -0
  144. package/src/lib/org-manager.js +336 -0
  145. package/src/lib/p2p-manager.js +341 -0
  146. package/src/lib/perception.js +346 -0
  147. package/src/lib/permanent-memory.js +327 -0
  148. package/src/lib/pipeline-orchestrator.js +332 -0
  149. package/src/lib/plan-mode.js +336 -0
  150. package/src/lib/plugin-autodiscovery.js +334 -0
  151. package/src/lib/plugin-ecosystem.js +346 -0
  152. package/src/lib/pqc-manager.js +346 -0
  153. package/src/lib/process-manager.js +336 -0
  154. package/src/lib/protocol-fusion.js +338 -0
  155. package/src/lib/provider-options.js +346 -0
  156. package/src/lib/provider-stream.js +348 -0
  157. package/src/lib/quantization.js +337 -0
  158. package/src/lib/response-cache.js +333 -0
  159. package/src/lib/scim-manager.js +346 -0
  160. package/src/lib/service-manager.js +337 -0
  161. package/src/lib/session-core-singletons.js +341 -0
  162. package/src/lib/session-manager.js +334 -0
  163. package/src/lib/skill-loader.js +334 -0
  164. package/src/lib/skill-mcp.js +336 -0
  165. package/src/lib/social-manager.js +330 -0
  166. package/src/lib/sso-manager.js +340 -0
  167. package/src/lib/stix-parser.js +346 -0
  168. package/src/lib/sub-agent-context.js +343 -0
  169. package/src/lib/sub-agent-profiles.js +335 -0
  170. package/src/lib/sub-agent-registry.js +336 -0
  171. package/src/lib/sync-manager.js +336 -0
  172. package/src/lib/tech-learning-engine.js +341 -0
  173. package/src/lib/tenant-saas.js +341 -0
  174. package/src/lib/threat-intel.js +330 -0
  175. package/src/lib/todo-manager.js +336 -0
  176. package/src/lib/token-tracker.js +336 -0
  177. package/src/lib/trust-security.js +343 -0
  178. package/src/lib/ueba.js +340 -0
  179. package/src/lib/universal-runtime.js +330 -0
  180. package/src/lib/wallet-manager.js +336 -0
  181. package/src/lib/web-ui-server.js +348 -0
  182. package/src/lib/workflow-engine.js +330 -0
  183. package/src/lib/workflow-expr.js +346 -0
  184. package/src/lib/ws-chat-handler.js +337 -0
@@ -1018,3 +1018,344 @@ export function _resetStateV2() {
1018
1018
  _tleProfileStaleMs = TLE_DEFAULT_PROFILE_STALE_MS;
1019
1019
  _tleRunStuckMs = TLE_DEFAULT_RUN_STUCK_MS;
1020
1020
  }
1021
+
1022
+ // =====================================================================
1023
+ // tech-learning-engine V2 governance overlay (iter23)
1024
+ // =====================================================================
1025
+ export const TECHGOV_PROFILE_MATURITY_V2 = Object.freeze({
1026
+ PENDING: "pending",
1027
+ ACTIVE: "active",
1028
+ STALE: "stale",
1029
+ ARCHIVED: "archived",
1030
+ });
1031
+ export const TECHGOV_LESSON_LIFECYCLE_V2 = Object.freeze({
1032
+ QUEUED: "queued",
1033
+ STUDYING: "studying",
1034
+ STUDIED: "studied",
1035
+ FAILED: "failed",
1036
+ CANCELLED: "cancelled",
1037
+ });
1038
+ const _techgovPTrans = new Map([
1039
+ [
1040
+ TECHGOV_PROFILE_MATURITY_V2.PENDING,
1041
+ new Set([
1042
+ TECHGOV_PROFILE_MATURITY_V2.ACTIVE,
1043
+ TECHGOV_PROFILE_MATURITY_V2.ARCHIVED,
1044
+ ]),
1045
+ ],
1046
+ [
1047
+ TECHGOV_PROFILE_MATURITY_V2.ACTIVE,
1048
+ new Set([
1049
+ TECHGOV_PROFILE_MATURITY_V2.STALE,
1050
+ TECHGOV_PROFILE_MATURITY_V2.ARCHIVED,
1051
+ ]),
1052
+ ],
1053
+ [
1054
+ TECHGOV_PROFILE_MATURITY_V2.STALE,
1055
+ new Set([
1056
+ TECHGOV_PROFILE_MATURITY_V2.ACTIVE,
1057
+ TECHGOV_PROFILE_MATURITY_V2.ARCHIVED,
1058
+ ]),
1059
+ ],
1060
+ [TECHGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1061
+ ]);
1062
+ const _techgovPTerminal = new Set([TECHGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1063
+ const _techgovJTrans = new Map([
1064
+ [
1065
+ TECHGOV_LESSON_LIFECYCLE_V2.QUEUED,
1066
+ new Set([
1067
+ TECHGOV_LESSON_LIFECYCLE_V2.STUDYING,
1068
+ TECHGOV_LESSON_LIFECYCLE_V2.CANCELLED,
1069
+ ]),
1070
+ ],
1071
+ [
1072
+ TECHGOV_LESSON_LIFECYCLE_V2.STUDYING,
1073
+ new Set([
1074
+ TECHGOV_LESSON_LIFECYCLE_V2.STUDIED,
1075
+ TECHGOV_LESSON_LIFECYCLE_V2.FAILED,
1076
+ TECHGOV_LESSON_LIFECYCLE_V2.CANCELLED,
1077
+ ]),
1078
+ ],
1079
+ [TECHGOV_LESSON_LIFECYCLE_V2.STUDIED, new Set()],
1080
+ [TECHGOV_LESSON_LIFECYCLE_V2.FAILED, new Set()],
1081
+ [TECHGOV_LESSON_LIFECYCLE_V2.CANCELLED, new Set()],
1082
+ ]);
1083
+ const _techgovPsV2 = new Map();
1084
+ const _techgovJsV2 = new Map();
1085
+ let _techgovMaxActive = 6,
1086
+ _techgovMaxPending = 12,
1087
+ _techgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1088
+ _techgovStuckMs = 60 * 1000;
1089
+ function _techgovPos(n, label) {
1090
+ const v = Math.floor(Number(n));
1091
+ if (!Number.isFinite(v) || v <= 0)
1092
+ throw new Error(`${label} must be positive integer`);
1093
+ return v;
1094
+ }
1095
+ function _techgovCheckP(from, to) {
1096
+ const a = _techgovPTrans.get(from);
1097
+ if (!a || !a.has(to))
1098
+ throw new Error(`invalid techgov profile transition ${from} → ${to}`);
1099
+ }
1100
+ function _techgovCheckJ(from, to) {
1101
+ const a = _techgovJTrans.get(from);
1102
+ if (!a || !a.has(to))
1103
+ throw new Error(`invalid techgov lesson transition ${from} → ${to}`);
1104
+ }
1105
+ function _techgovCountActive(owner) {
1106
+ let c = 0;
1107
+ for (const p of _techgovPsV2.values())
1108
+ if (p.owner === owner && p.status === TECHGOV_PROFILE_MATURITY_V2.ACTIVE)
1109
+ c++;
1110
+ return c;
1111
+ }
1112
+ function _techgovCountPending(profileId) {
1113
+ let c = 0;
1114
+ for (const j of _techgovJsV2.values())
1115
+ if (
1116
+ j.profileId === profileId &&
1117
+ (j.status === TECHGOV_LESSON_LIFECYCLE_V2.QUEUED ||
1118
+ j.status === TECHGOV_LESSON_LIFECYCLE_V2.STUDYING)
1119
+ )
1120
+ c++;
1121
+ return c;
1122
+ }
1123
+ export function setMaxActiveTechgovProfilesPerOwnerV2(n) {
1124
+ _techgovMaxActive = _techgovPos(n, "maxActiveTechgovProfilesPerOwner");
1125
+ }
1126
+ export function getMaxActiveTechgovProfilesPerOwnerV2() {
1127
+ return _techgovMaxActive;
1128
+ }
1129
+ export function setMaxPendingTechgovLessonsPerProfileV2(n) {
1130
+ _techgovMaxPending = _techgovPos(n, "maxPendingTechgovLessonsPerProfile");
1131
+ }
1132
+ export function getMaxPendingTechgovLessonsPerProfileV2() {
1133
+ return _techgovMaxPending;
1134
+ }
1135
+ export function setTechgovProfileIdleMsV2(n) {
1136
+ _techgovIdleMs = _techgovPos(n, "techgovProfileIdleMs");
1137
+ }
1138
+ export function getTechgovProfileIdleMsV2() {
1139
+ return _techgovIdleMs;
1140
+ }
1141
+ export function setTechgovLessonStuckMsV2(n) {
1142
+ _techgovStuckMs = _techgovPos(n, "techgovLessonStuckMs");
1143
+ }
1144
+ export function getTechgovLessonStuckMsV2() {
1145
+ return _techgovStuckMs;
1146
+ }
1147
+ export function _resetStateTechLearningEngineGovV2() {
1148
+ _techgovPsV2.clear();
1149
+ _techgovJsV2.clear();
1150
+ _techgovMaxActive = 6;
1151
+ _techgovMaxPending = 12;
1152
+ _techgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1153
+ _techgovStuckMs = 60 * 1000;
1154
+ }
1155
+ export function registerTechgovProfileV2({ id, owner, topic, metadata } = {}) {
1156
+ if (!id || !owner) throw new Error("id and owner required");
1157
+ if (_techgovPsV2.has(id))
1158
+ throw new Error(`techgov profile ${id} already exists`);
1159
+ const now = Date.now();
1160
+ const p = {
1161
+ id,
1162
+ owner,
1163
+ topic: topic || "general",
1164
+ status: TECHGOV_PROFILE_MATURITY_V2.PENDING,
1165
+ createdAt: now,
1166
+ updatedAt: now,
1167
+ lastTouchedAt: now,
1168
+ activatedAt: null,
1169
+ archivedAt: null,
1170
+ metadata: { ...(metadata || {}) },
1171
+ };
1172
+ _techgovPsV2.set(id, p);
1173
+ return { ...p, metadata: { ...p.metadata } };
1174
+ }
1175
+ export function activateTechgovProfileV2(id) {
1176
+ const p = _techgovPsV2.get(id);
1177
+ if (!p) throw new Error(`techgov profile ${id} not found`);
1178
+ const isInitial = p.status === TECHGOV_PROFILE_MATURITY_V2.PENDING;
1179
+ _techgovCheckP(p.status, TECHGOV_PROFILE_MATURITY_V2.ACTIVE);
1180
+ if (isInitial && _techgovCountActive(p.owner) >= _techgovMaxActive)
1181
+ throw new Error(`max active techgov profiles for owner ${p.owner} reached`);
1182
+ const now = Date.now();
1183
+ p.status = TECHGOV_PROFILE_MATURITY_V2.ACTIVE;
1184
+ p.updatedAt = now;
1185
+ p.lastTouchedAt = now;
1186
+ if (!p.activatedAt) p.activatedAt = now;
1187
+ return { ...p, metadata: { ...p.metadata } };
1188
+ }
1189
+ export function staleTechgovProfileV2(id) {
1190
+ const p = _techgovPsV2.get(id);
1191
+ if (!p) throw new Error(`techgov profile ${id} not found`);
1192
+ _techgovCheckP(p.status, TECHGOV_PROFILE_MATURITY_V2.STALE);
1193
+ p.status = TECHGOV_PROFILE_MATURITY_V2.STALE;
1194
+ p.updatedAt = Date.now();
1195
+ return { ...p, metadata: { ...p.metadata } };
1196
+ }
1197
+ export function archiveTechgovProfileV2(id) {
1198
+ const p = _techgovPsV2.get(id);
1199
+ if (!p) throw new Error(`techgov profile ${id} not found`);
1200
+ _techgovCheckP(p.status, TECHGOV_PROFILE_MATURITY_V2.ARCHIVED);
1201
+ const now = Date.now();
1202
+ p.status = TECHGOV_PROFILE_MATURITY_V2.ARCHIVED;
1203
+ p.updatedAt = now;
1204
+ if (!p.archivedAt) p.archivedAt = now;
1205
+ return { ...p, metadata: { ...p.metadata } };
1206
+ }
1207
+ export function touchTechgovProfileV2(id) {
1208
+ const p = _techgovPsV2.get(id);
1209
+ if (!p) throw new Error(`techgov profile ${id} not found`);
1210
+ if (_techgovPTerminal.has(p.status))
1211
+ throw new Error(`cannot touch terminal techgov profile ${id}`);
1212
+ const now = Date.now();
1213
+ p.lastTouchedAt = now;
1214
+ p.updatedAt = now;
1215
+ return { ...p, metadata: { ...p.metadata } };
1216
+ }
1217
+ export function getTechgovProfileV2(id) {
1218
+ const p = _techgovPsV2.get(id);
1219
+ if (!p) return null;
1220
+ return { ...p, metadata: { ...p.metadata } };
1221
+ }
1222
+ export function listTechgovProfilesV2() {
1223
+ return [..._techgovPsV2.values()].map((p) => ({
1224
+ ...p,
1225
+ metadata: { ...p.metadata },
1226
+ }));
1227
+ }
1228
+ export function createTechgovLessonV2({
1229
+ id,
1230
+ profileId,
1231
+ source,
1232
+ metadata,
1233
+ } = {}) {
1234
+ if (!id || !profileId) throw new Error("id and profileId required");
1235
+ if (_techgovJsV2.has(id))
1236
+ throw new Error(`techgov lesson ${id} already exists`);
1237
+ if (!_techgovPsV2.has(profileId))
1238
+ throw new Error(`techgov profile ${profileId} not found`);
1239
+ if (_techgovCountPending(profileId) >= _techgovMaxPending)
1240
+ throw new Error(
1241
+ `max pending techgov lessons for profile ${profileId} reached`,
1242
+ );
1243
+ const now = Date.now();
1244
+ const j = {
1245
+ id,
1246
+ profileId,
1247
+ source: source || "",
1248
+ status: TECHGOV_LESSON_LIFECYCLE_V2.QUEUED,
1249
+ createdAt: now,
1250
+ updatedAt: now,
1251
+ startedAt: null,
1252
+ settledAt: null,
1253
+ metadata: { ...(metadata || {}) },
1254
+ };
1255
+ _techgovJsV2.set(id, j);
1256
+ return { ...j, metadata: { ...j.metadata } };
1257
+ }
1258
+ export function studyingTechgovLessonV2(id) {
1259
+ const j = _techgovJsV2.get(id);
1260
+ if (!j) throw new Error(`techgov lesson ${id} not found`);
1261
+ _techgovCheckJ(j.status, TECHGOV_LESSON_LIFECYCLE_V2.STUDYING);
1262
+ const now = Date.now();
1263
+ j.status = TECHGOV_LESSON_LIFECYCLE_V2.STUDYING;
1264
+ j.updatedAt = now;
1265
+ if (!j.startedAt) j.startedAt = now;
1266
+ return { ...j, metadata: { ...j.metadata } };
1267
+ }
1268
+ export function completeLessonTechgovV2(id) {
1269
+ const j = _techgovJsV2.get(id);
1270
+ if (!j) throw new Error(`techgov lesson ${id} not found`);
1271
+ _techgovCheckJ(j.status, TECHGOV_LESSON_LIFECYCLE_V2.STUDIED);
1272
+ const now = Date.now();
1273
+ j.status = TECHGOV_LESSON_LIFECYCLE_V2.STUDIED;
1274
+ j.updatedAt = now;
1275
+ if (!j.settledAt) j.settledAt = now;
1276
+ return { ...j, metadata: { ...j.metadata } };
1277
+ }
1278
+ export function failTechgovLessonV2(id, reason) {
1279
+ const j = _techgovJsV2.get(id);
1280
+ if (!j) throw new Error(`techgov lesson ${id} not found`);
1281
+ _techgovCheckJ(j.status, TECHGOV_LESSON_LIFECYCLE_V2.FAILED);
1282
+ const now = Date.now();
1283
+ j.status = TECHGOV_LESSON_LIFECYCLE_V2.FAILED;
1284
+ j.updatedAt = now;
1285
+ if (!j.settledAt) j.settledAt = now;
1286
+ if (reason) j.metadata.failReason = String(reason);
1287
+ return { ...j, metadata: { ...j.metadata } };
1288
+ }
1289
+ export function cancelTechgovLessonV2(id, reason) {
1290
+ const j = _techgovJsV2.get(id);
1291
+ if (!j) throw new Error(`techgov lesson ${id} not found`);
1292
+ _techgovCheckJ(j.status, TECHGOV_LESSON_LIFECYCLE_V2.CANCELLED);
1293
+ const now = Date.now();
1294
+ j.status = TECHGOV_LESSON_LIFECYCLE_V2.CANCELLED;
1295
+ j.updatedAt = now;
1296
+ if (!j.settledAt) j.settledAt = now;
1297
+ if (reason) j.metadata.cancelReason = String(reason);
1298
+ return { ...j, metadata: { ...j.metadata } };
1299
+ }
1300
+ export function getTechgovLessonV2(id) {
1301
+ const j = _techgovJsV2.get(id);
1302
+ if (!j) return null;
1303
+ return { ...j, metadata: { ...j.metadata } };
1304
+ }
1305
+ export function listTechgovLessonsV2() {
1306
+ return [..._techgovJsV2.values()].map((j) => ({
1307
+ ...j,
1308
+ metadata: { ...j.metadata },
1309
+ }));
1310
+ }
1311
+ export function autoStaleIdleTechgovProfilesV2({ now } = {}) {
1312
+ const t = now ?? Date.now();
1313
+ const flipped = [];
1314
+ for (const p of _techgovPsV2.values())
1315
+ if (
1316
+ p.status === TECHGOV_PROFILE_MATURITY_V2.ACTIVE &&
1317
+ t - p.lastTouchedAt >= _techgovIdleMs
1318
+ ) {
1319
+ p.status = TECHGOV_PROFILE_MATURITY_V2.STALE;
1320
+ p.updatedAt = t;
1321
+ flipped.push(p.id);
1322
+ }
1323
+ return { flipped, count: flipped.length };
1324
+ }
1325
+ export function autoFailStuckTechgovLessonsV2({ now } = {}) {
1326
+ const t = now ?? Date.now();
1327
+ const flipped = [];
1328
+ for (const j of _techgovJsV2.values())
1329
+ if (
1330
+ j.status === TECHGOV_LESSON_LIFECYCLE_V2.STUDYING &&
1331
+ j.startedAt != null &&
1332
+ t - j.startedAt >= _techgovStuckMs
1333
+ ) {
1334
+ j.status = TECHGOV_LESSON_LIFECYCLE_V2.FAILED;
1335
+ j.updatedAt = t;
1336
+ if (!j.settledAt) j.settledAt = t;
1337
+ j.metadata.failReason = "auto-fail-stuck";
1338
+ flipped.push(j.id);
1339
+ }
1340
+ return { flipped, count: flipped.length };
1341
+ }
1342
+ export function getTechLearningEngineGovStatsV2() {
1343
+ const profilesByStatus = {};
1344
+ for (const v of Object.values(TECHGOV_PROFILE_MATURITY_V2))
1345
+ profilesByStatus[v] = 0;
1346
+ for (const p of _techgovPsV2.values()) profilesByStatus[p.status]++;
1347
+ const lessonsByStatus = {};
1348
+ for (const v of Object.values(TECHGOV_LESSON_LIFECYCLE_V2))
1349
+ lessonsByStatus[v] = 0;
1350
+ for (const j of _techgovJsV2.values()) lessonsByStatus[j.status]++;
1351
+ return {
1352
+ totalTechgovProfilesV2: _techgovPsV2.size,
1353
+ totalTechgovLessonsV2: _techgovJsV2.size,
1354
+ maxActiveTechgovProfilesPerOwner: _techgovMaxActive,
1355
+ maxPendingTechgovLessonsPerProfile: _techgovMaxPending,
1356
+ techgovProfileIdleMs: _techgovIdleMs,
1357
+ techgovLessonStuckMs: _techgovStuckMs,
1358
+ profilesByStatus,
1359
+ lessonsByStatus,
1360
+ };
1361
+ }
@@ -1289,3 +1289,344 @@ export function _resetStateV2() {
1289
1289
  _tenantIdleMsV2 = SAAS_DEFAULT_TENANT_IDLE_MS;
1290
1290
  _pastDueGraceMsV2 = SAAS_DEFAULT_PAST_DUE_GRACE_MS;
1291
1291
  }
1292
+
1293
+ // =====================================================================
1294
+ // tenant-saas V2 governance overlay (iter18)
1295
+ // =====================================================================
1296
+ export const TNSGOV_PROFILE_MATURITY_V2 = Object.freeze({
1297
+ PENDING: "pending",
1298
+ ACTIVE: "active",
1299
+ SUSPENDED: "suspended",
1300
+ ARCHIVED: "archived",
1301
+ });
1302
+ export const TNSGOV_ALLOCATION_LIFECYCLE_V2 = Object.freeze({
1303
+ QUEUED: "queued",
1304
+ PROVISIONING: "provisioning",
1305
+ PROVISIONED: "provisioned",
1306
+ FAILED: "failed",
1307
+ CANCELLED: "cancelled",
1308
+ });
1309
+ const _tnsgovPTrans = new Map([
1310
+ [
1311
+ TNSGOV_PROFILE_MATURITY_V2.PENDING,
1312
+ new Set([
1313
+ TNSGOV_PROFILE_MATURITY_V2.ACTIVE,
1314
+ TNSGOV_PROFILE_MATURITY_V2.ARCHIVED,
1315
+ ]),
1316
+ ],
1317
+ [
1318
+ TNSGOV_PROFILE_MATURITY_V2.ACTIVE,
1319
+ new Set([
1320
+ TNSGOV_PROFILE_MATURITY_V2.SUSPENDED,
1321
+ TNSGOV_PROFILE_MATURITY_V2.ARCHIVED,
1322
+ ]),
1323
+ ],
1324
+ [
1325
+ TNSGOV_PROFILE_MATURITY_V2.SUSPENDED,
1326
+ new Set([
1327
+ TNSGOV_PROFILE_MATURITY_V2.ACTIVE,
1328
+ TNSGOV_PROFILE_MATURITY_V2.ARCHIVED,
1329
+ ]),
1330
+ ],
1331
+ [TNSGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
1332
+ ]);
1333
+ const _tnsgovPTerminal = new Set([TNSGOV_PROFILE_MATURITY_V2.ARCHIVED]);
1334
+ const _tnsgovJTrans = new Map([
1335
+ [
1336
+ TNSGOV_ALLOCATION_LIFECYCLE_V2.QUEUED,
1337
+ new Set([
1338
+ TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONING,
1339
+ TNSGOV_ALLOCATION_LIFECYCLE_V2.CANCELLED,
1340
+ ]),
1341
+ ],
1342
+ [
1343
+ TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONING,
1344
+ new Set([
1345
+ TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONED,
1346
+ TNSGOV_ALLOCATION_LIFECYCLE_V2.FAILED,
1347
+ TNSGOV_ALLOCATION_LIFECYCLE_V2.CANCELLED,
1348
+ ]),
1349
+ ],
1350
+ [TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONED, new Set()],
1351
+ [TNSGOV_ALLOCATION_LIFECYCLE_V2.FAILED, new Set()],
1352
+ [TNSGOV_ALLOCATION_LIFECYCLE_V2.CANCELLED, new Set()],
1353
+ ]);
1354
+ const _tnsgovPsV2 = new Map();
1355
+ const _tnsgovJsV2 = new Map();
1356
+ let _tnsgovMaxActive = 10,
1357
+ _tnsgovMaxPending = 25,
1358
+ _tnsgovIdleMs = 30 * 24 * 60 * 60 * 1000,
1359
+ _tnsgovStuckMs = 60 * 1000;
1360
+ function _tnsgovPos(n, label) {
1361
+ const v = Math.floor(Number(n));
1362
+ if (!Number.isFinite(v) || v <= 0)
1363
+ throw new Error(`${label} must be positive integer`);
1364
+ return v;
1365
+ }
1366
+ function _tnsgovCheckP(from, to) {
1367
+ const a = _tnsgovPTrans.get(from);
1368
+ if (!a || !a.has(to))
1369
+ throw new Error(`invalid tnsgov profile transition ${from} → ${to}`);
1370
+ }
1371
+ function _tnsgovCheckJ(from, to) {
1372
+ const a = _tnsgovJTrans.get(from);
1373
+ if (!a || !a.has(to))
1374
+ throw new Error(`invalid tnsgov allocation transition ${from} → ${to}`);
1375
+ }
1376
+ function _tnsgovCountActive(owner) {
1377
+ let c = 0;
1378
+ for (const p of _tnsgovPsV2.values())
1379
+ if (p.owner === owner && p.status === TNSGOV_PROFILE_MATURITY_V2.ACTIVE)
1380
+ c++;
1381
+ return c;
1382
+ }
1383
+ function _tnsgovCountPending(profileId) {
1384
+ let c = 0;
1385
+ for (const j of _tnsgovJsV2.values())
1386
+ if (
1387
+ j.profileId === profileId &&
1388
+ (j.status === TNSGOV_ALLOCATION_LIFECYCLE_V2.QUEUED ||
1389
+ j.status === TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONING)
1390
+ )
1391
+ c++;
1392
+ return c;
1393
+ }
1394
+ export function setMaxActiveTnsgovProfilesPerOwnerV2(n) {
1395
+ _tnsgovMaxActive = _tnsgovPos(n, "maxActiveTnsgovProfilesPerOwner");
1396
+ }
1397
+ export function getMaxActiveTnsgovProfilesPerOwnerV2() {
1398
+ return _tnsgovMaxActive;
1399
+ }
1400
+ export function setMaxPendingTnsgovAllocationsPerProfileV2(n) {
1401
+ _tnsgovMaxPending = _tnsgovPos(n, "maxPendingTnsgovAllocationsPerProfile");
1402
+ }
1403
+ export function getMaxPendingTnsgovAllocationsPerProfileV2() {
1404
+ return _tnsgovMaxPending;
1405
+ }
1406
+ export function setTnsgovProfileIdleMsV2(n) {
1407
+ _tnsgovIdleMs = _tnsgovPos(n, "tnsgovProfileIdleMs");
1408
+ }
1409
+ export function getTnsgovProfileIdleMsV2() {
1410
+ return _tnsgovIdleMs;
1411
+ }
1412
+ export function setTnsgovAllocationStuckMsV2(n) {
1413
+ _tnsgovStuckMs = _tnsgovPos(n, "tnsgovAllocationStuckMs");
1414
+ }
1415
+ export function getTnsgovAllocationStuckMsV2() {
1416
+ return _tnsgovStuckMs;
1417
+ }
1418
+ export function _resetStateTenantSaasGovV2() {
1419
+ _tnsgovPsV2.clear();
1420
+ _tnsgovJsV2.clear();
1421
+ _tnsgovMaxActive = 10;
1422
+ _tnsgovMaxPending = 25;
1423
+ _tnsgovIdleMs = 30 * 24 * 60 * 60 * 1000;
1424
+ _tnsgovStuckMs = 60 * 1000;
1425
+ }
1426
+ export function registerTnsgovProfileV2({ id, owner, plan, metadata } = {}) {
1427
+ if (!id || !owner) throw new Error("id and owner required");
1428
+ if (_tnsgovPsV2.has(id))
1429
+ throw new Error(`tnsgov profile ${id} already exists`);
1430
+ const now = Date.now();
1431
+ const p = {
1432
+ id,
1433
+ owner,
1434
+ plan: plan || "free",
1435
+ status: TNSGOV_PROFILE_MATURITY_V2.PENDING,
1436
+ createdAt: now,
1437
+ updatedAt: now,
1438
+ lastTouchedAt: now,
1439
+ activatedAt: null,
1440
+ archivedAt: null,
1441
+ metadata: { ...(metadata || {}) },
1442
+ };
1443
+ _tnsgovPsV2.set(id, p);
1444
+ return { ...p, metadata: { ...p.metadata } };
1445
+ }
1446
+ export function activateTnsgovProfileV2(id) {
1447
+ const p = _tnsgovPsV2.get(id);
1448
+ if (!p) throw new Error(`tnsgov profile ${id} not found`);
1449
+ const isInitial = p.status === TNSGOV_PROFILE_MATURITY_V2.PENDING;
1450
+ _tnsgovCheckP(p.status, TNSGOV_PROFILE_MATURITY_V2.ACTIVE);
1451
+ if (isInitial && _tnsgovCountActive(p.owner) >= _tnsgovMaxActive)
1452
+ throw new Error(`max active tnsgov profiles for owner ${p.owner} reached`);
1453
+ const now = Date.now();
1454
+ p.status = TNSGOV_PROFILE_MATURITY_V2.ACTIVE;
1455
+ p.updatedAt = now;
1456
+ p.lastTouchedAt = now;
1457
+ if (!p.activatedAt) p.activatedAt = now;
1458
+ return { ...p, metadata: { ...p.metadata } };
1459
+ }
1460
+ export function suspendTnsgovProfileV2(id) {
1461
+ const p = _tnsgovPsV2.get(id);
1462
+ if (!p) throw new Error(`tnsgov profile ${id} not found`);
1463
+ _tnsgovCheckP(p.status, TNSGOV_PROFILE_MATURITY_V2.SUSPENDED);
1464
+ p.status = TNSGOV_PROFILE_MATURITY_V2.SUSPENDED;
1465
+ p.updatedAt = Date.now();
1466
+ return { ...p, metadata: { ...p.metadata } };
1467
+ }
1468
+ export function archiveTnsgovProfileV2(id) {
1469
+ const p = _tnsgovPsV2.get(id);
1470
+ if (!p) throw new Error(`tnsgov profile ${id} not found`);
1471
+ _tnsgovCheckP(p.status, TNSGOV_PROFILE_MATURITY_V2.ARCHIVED);
1472
+ const now = Date.now();
1473
+ p.status = TNSGOV_PROFILE_MATURITY_V2.ARCHIVED;
1474
+ p.updatedAt = now;
1475
+ if (!p.archivedAt) p.archivedAt = now;
1476
+ return { ...p, metadata: { ...p.metadata } };
1477
+ }
1478
+ export function touchTnsgovProfileV2(id) {
1479
+ const p = _tnsgovPsV2.get(id);
1480
+ if (!p) throw new Error(`tnsgov profile ${id} not found`);
1481
+ if (_tnsgovPTerminal.has(p.status))
1482
+ throw new Error(`cannot touch terminal tnsgov profile ${id}`);
1483
+ const now = Date.now();
1484
+ p.lastTouchedAt = now;
1485
+ p.updatedAt = now;
1486
+ return { ...p, metadata: { ...p.metadata } };
1487
+ }
1488
+ export function getTnsgovProfileV2(id) {
1489
+ const p = _tnsgovPsV2.get(id);
1490
+ if (!p) return null;
1491
+ return { ...p, metadata: { ...p.metadata } };
1492
+ }
1493
+ export function listTnsgovProfilesV2() {
1494
+ return [..._tnsgovPsV2.values()].map((p) => ({
1495
+ ...p,
1496
+ metadata: { ...p.metadata },
1497
+ }));
1498
+ }
1499
+ export function createTnsgovAllocationV2({
1500
+ id,
1501
+ profileId,
1502
+ resource,
1503
+ metadata,
1504
+ } = {}) {
1505
+ if (!id || !profileId) throw new Error("id and profileId required");
1506
+ if (_tnsgovJsV2.has(id))
1507
+ throw new Error(`tnsgov allocation ${id} already exists`);
1508
+ if (!_tnsgovPsV2.has(profileId))
1509
+ throw new Error(`tnsgov profile ${profileId} not found`);
1510
+ if (_tnsgovCountPending(profileId) >= _tnsgovMaxPending)
1511
+ throw new Error(
1512
+ `max pending tnsgov allocations for profile ${profileId} reached`,
1513
+ );
1514
+ const now = Date.now();
1515
+ const j = {
1516
+ id,
1517
+ profileId,
1518
+ resource: resource || "",
1519
+ status: TNSGOV_ALLOCATION_LIFECYCLE_V2.QUEUED,
1520
+ createdAt: now,
1521
+ updatedAt: now,
1522
+ startedAt: null,
1523
+ settledAt: null,
1524
+ metadata: { ...(metadata || {}) },
1525
+ };
1526
+ _tnsgovJsV2.set(id, j);
1527
+ return { ...j, metadata: { ...j.metadata } };
1528
+ }
1529
+ export function provisioningTnsgovAllocationV2(id) {
1530
+ const j = _tnsgovJsV2.get(id);
1531
+ if (!j) throw new Error(`tnsgov allocation ${id} not found`);
1532
+ _tnsgovCheckJ(j.status, TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONING);
1533
+ const now = Date.now();
1534
+ j.status = TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONING;
1535
+ j.updatedAt = now;
1536
+ if (!j.startedAt) j.startedAt = now;
1537
+ return { ...j, metadata: { ...j.metadata } };
1538
+ }
1539
+ export function completeAllocationTnsgovV2(id) {
1540
+ const j = _tnsgovJsV2.get(id);
1541
+ if (!j) throw new Error(`tnsgov allocation ${id} not found`);
1542
+ _tnsgovCheckJ(j.status, TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONED);
1543
+ const now = Date.now();
1544
+ j.status = TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONED;
1545
+ j.updatedAt = now;
1546
+ if (!j.settledAt) j.settledAt = now;
1547
+ return { ...j, metadata: { ...j.metadata } };
1548
+ }
1549
+ export function failTnsgovAllocationV2(id, reason) {
1550
+ const j = _tnsgovJsV2.get(id);
1551
+ if (!j) throw new Error(`tnsgov allocation ${id} not found`);
1552
+ _tnsgovCheckJ(j.status, TNSGOV_ALLOCATION_LIFECYCLE_V2.FAILED);
1553
+ const now = Date.now();
1554
+ j.status = TNSGOV_ALLOCATION_LIFECYCLE_V2.FAILED;
1555
+ j.updatedAt = now;
1556
+ if (!j.settledAt) j.settledAt = now;
1557
+ if (reason) j.metadata.failReason = String(reason);
1558
+ return { ...j, metadata: { ...j.metadata } };
1559
+ }
1560
+ export function cancelTnsgovAllocationV2(id, reason) {
1561
+ const j = _tnsgovJsV2.get(id);
1562
+ if (!j) throw new Error(`tnsgov allocation ${id} not found`);
1563
+ _tnsgovCheckJ(j.status, TNSGOV_ALLOCATION_LIFECYCLE_V2.CANCELLED);
1564
+ const now = Date.now();
1565
+ j.status = TNSGOV_ALLOCATION_LIFECYCLE_V2.CANCELLED;
1566
+ j.updatedAt = now;
1567
+ if (!j.settledAt) j.settledAt = now;
1568
+ if (reason) j.metadata.cancelReason = String(reason);
1569
+ return { ...j, metadata: { ...j.metadata } };
1570
+ }
1571
+ export function getTnsgovAllocationV2(id) {
1572
+ const j = _tnsgovJsV2.get(id);
1573
+ if (!j) return null;
1574
+ return { ...j, metadata: { ...j.metadata } };
1575
+ }
1576
+ export function listTnsgovAllocationsV2() {
1577
+ return [..._tnsgovJsV2.values()].map((j) => ({
1578
+ ...j,
1579
+ metadata: { ...j.metadata },
1580
+ }));
1581
+ }
1582
+ export function autoSuspendIdleTnsgovProfilesV2({ now } = {}) {
1583
+ const t = now ?? Date.now();
1584
+ const flipped = [];
1585
+ for (const p of _tnsgovPsV2.values())
1586
+ if (
1587
+ p.status === TNSGOV_PROFILE_MATURITY_V2.ACTIVE &&
1588
+ t - p.lastTouchedAt >= _tnsgovIdleMs
1589
+ ) {
1590
+ p.status = TNSGOV_PROFILE_MATURITY_V2.SUSPENDED;
1591
+ p.updatedAt = t;
1592
+ flipped.push(p.id);
1593
+ }
1594
+ return { flipped, count: flipped.length };
1595
+ }
1596
+ export function autoFailStuckTnsgovAllocationsV2({ now } = {}) {
1597
+ const t = now ?? Date.now();
1598
+ const flipped = [];
1599
+ for (const j of _tnsgovJsV2.values())
1600
+ if (
1601
+ j.status === TNSGOV_ALLOCATION_LIFECYCLE_V2.PROVISIONING &&
1602
+ j.startedAt != null &&
1603
+ t - j.startedAt >= _tnsgovStuckMs
1604
+ ) {
1605
+ j.status = TNSGOV_ALLOCATION_LIFECYCLE_V2.FAILED;
1606
+ j.updatedAt = t;
1607
+ if (!j.settledAt) j.settledAt = t;
1608
+ j.metadata.failReason = "auto-fail-stuck";
1609
+ flipped.push(j.id);
1610
+ }
1611
+ return { flipped, count: flipped.length };
1612
+ }
1613
+ export function getTenantSaasGovStatsV2() {
1614
+ const profilesByStatus = {};
1615
+ for (const v of Object.values(TNSGOV_PROFILE_MATURITY_V2))
1616
+ profilesByStatus[v] = 0;
1617
+ for (const p of _tnsgovPsV2.values()) profilesByStatus[p.status]++;
1618
+ const allocationsByStatus = {};
1619
+ for (const v of Object.values(TNSGOV_ALLOCATION_LIFECYCLE_V2))
1620
+ allocationsByStatus[v] = 0;
1621
+ for (const j of _tnsgovJsV2.values()) allocationsByStatus[j.status]++;
1622
+ return {
1623
+ totalTnsgovProfilesV2: _tnsgovPsV2.size,
1624
+ totalTnsgovAllocationsV2: _tnsgovJsV2.size,
1625
+ maxActiveTnsgovProfilesPerOwner: _tnsgovMaxActive,
1626
+ maxPendingTnsgovAllocationsPerProfile: _tnsgovMaxPending,
1627
+ tnsgovProfileIdleMs: _tnsgovIdleMs,
1628
+ tnsgovAllocationStuckMs: _tnsgovStuckMs,
1629
+ profilesByStatus,
1630
+ allocationsByStatus,
1631
+ };
1632
+ }