chainlesschain 0.143.0 → 0.152.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 (227) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +196 -28
  3. package/src/commands/activitypub.js +364 -27
  4. package/src/commands/agent-network.js +217 -0
  5. package/src/commands/agent.js +587 -103
  6. package/src/commands/audit.js +206 -0
  7. package/src/commands/automation.js +201 -0
  8. package/src/commands/bi.js +355 -27
  9. package/src/commands/bm25.js +111 -27
  10. package/src/commands/browse.js +403 -29
  11. package/src/commands/ccron.js +128 -28
  12. package/src/commands/chat.js +207 -0
  13. package/src/commands/codegen.js +207 -0
  14. package/src/commands/collab.js +211 -0
  15. package/src/commands/compliance.js +824 -34
  16. package/src/commands/compt.js +127 -29
  17. package/src/commands/consol.js +8 -2
  18. package/src/commands/cowork.js +783 -34
  19. package/src/commands/crosschain.js +182 -28
  20. package/src/commands/dao.js +182 -28
  21. package/src/commands/dbevo.js +227 -0
  22. package/src/commands/dev.js +207 -0
  23. package/src/commands/did-v2.js +217 -0
  24. package/src/commands/did.js +221 -0
  25. package/src/commands/dlp.js +385 -27
  26. package/src/commands/economy.js +205 -50
  27. package/src/commands/evolution.js +203 -50
  28. package/src/commands/evomap.js +387 -27
  29. package/src/commands/export.js +213 -0
  30. package/src/commands/federation.js +209 -0
  31. package/src/commands/fflag.js +74 -22
  32. package/src/commands/fusion.js +205 -0
  33. package/src/commands/git.js +177 -37
  34. package/src/commands/governance.js +209 -0
  35. package/src/commands/hardening.js +209 -0
  36. package/src/commands/hmemory.js +204 -50
  37. package/src/commands/hook.js +209 -0
  38. package/src/commands/import.js +209 -0
  39. package/src/commands/incentive.js +209 -0
  40. package/src/commands/inference.js +170 -34
  41. package/src/commands/infra.js +203 -0
  42. package/src/commands/instinct.js +209 -0
  43. package/src/commands/ipfs.js +207 -0
  44. package/src/commands/itbudget.js +149 -33
  45. package/src/commands/kg.js +206 -0
  46. package/src/commands/llm.js +217 -0
  47. package/src/commands/lowcode.js +195 -38
  48. package/src/commands/marketplace.js +206 -0
  49. package/src/commands/matrix.js +386 -27
  50. package/src/commands/mcp.js +217 -0
  51. package/src/commands/mcpscaf.js +145 -33
  52. package/src/commands/meminj.js +145 -33
  53. package/src/commands/memory.js +209 -0
  54. package/src/commands/multimodal.js +203 -0
  55. package/src/commands/nlprog.js +225 -0
  56. package/src/commands/nostr.js +387 -27
  57. package/src/commands/note.js +205 -0
  58. package/src/commands/ops.js +219 -0
  59. package/src/commands/orchestrate.js +217 -0
  60. package/src/commands/orchgov.js +144 -33
  61. package/src/commands/org.js +209 -0
  62. package/src/commands/p2p.js +209 -0
  63. package/src/commands/pdfp.js +109 -27
  64. package/src/commands/perception.js +209 -0
  65. package/src/commands/perf.js +169 -32
  66. package/src/commands/perm.js +144 -33
  67. package/src/commands/permmem.js +203 -0
  68. package/src/commands/pipeline.js +207 -52
  69. package/src/commands/planmode.js +141 -32
  70. package/src/commands/plugin-ecosystem.js +209 -0
  71. package/src/commands/pqc.js +213 -0
  72. package/src/commands/privacy.js +203 -0
  73. package/src/commands/promcomp.js +111 -27
  74. package/src/commands/quantization.js +207 -0
  75. package/src/commands/rcache.js +205 -0
  76. package/src/commands/recommend.js +410 -34
  77. package/src/commands/reputation.js +208 -0
  78. package/src/commands/runtime.js +205 -0
  79. package/src/commands/sandbox.js +206 -0
  80. package/src/commands/scim.js +209 -0
  81. package/src/commands/seshhook.js +145 -33
  82. package/src/commands/seshsearch.js +141 -33
  83. package/src/commands/seshtail.js +144 -33
  84. package/src/commands/seshu.js +152 -33
  85. package/src/commands/session.js +209 -0
  86. package/src/commands/sganal.js +123 -29
  87. package/src/commands/siem.js +201 -34
  88. package/src/commands/skill.js +207 -0
  89. package/src/commands/sla.js +212 -0
  90. package/src/commands/slotfill.js +146 -33
  91. package/src/commands/social.js +358 -32
  92. package/src/commands/sso.js +209 -0
  93. package/src/commands/stress.js +206 -0
  94. package/src/commands/svccont.js +145 -33
  95. package/src/commands/sync.js +209 -0
  96. package/src/commands/tech.js +209 -0
  97. package/src/commands/tenant.js +217 -0
  98. package/src/commands/terraform.js +206 -0
  99. package/src/commands/tms.js +171 -33
  100. package/src/commands/tokens.js +209 -0
  101. package/src/commands/topiccls.js +146 -33
  102. package/src/commands/trust.js +217 -0
  103. package/src/commands/uprof.js +141 -32
  104. package/src/commands/vcheck.js +122 -28
  105. package/src/commands/wallet.js +209 -0
  106. package/src/commands/webfetch.js +141 -32
  107. package/src/commands/workflow.js +203 -0
  108. package/src/commands/zkp.js +184 -28
  109. package/src/index.js +180 -0
  110. package/src/lib/a2a-protocol.js +319 -51
  111. package/src/lib/activitypub-bridge.js +622 -50
  112. package/src/lib/agent-economy.js +304 -51
  113. package/src/lib/agent-network.js +341 -0
  114. package/src/lib/aiops.js +346 -0
  115. package/src/lib/app-builder.js +279 -46
  116. package/src/lib/audit-logger.js +321 -0
  117. package/src/lib/automation-engine.js +335 -0
  118. package/src/lib/autonomous-agent.js +284 -48
  119. package/src/lib/autonomous-developer.js +332 -0
  120. package/src/lib/bi-engine.js +616 -49
  121. package/src/lib/bm25-search.js +301 -49
  122. package/src/lib/browser-automation.js +630 -49
  123. package/src/lib/chat-core.js +336 -0
  124. package/src/lib/claude-code-bridge.js +341 -0
  125. package/src/lib/code-agent.js +339 -0
  126. package/src/lib/collaboration-governance.js +334 -0
  127. package/src/lib/community-governance.js +346 -0
  128. package/src/lib/compliance-framework-reporter.js +305 -51
  129. package/src/lib/compliance-manager.js +330 -0
  130. package/src/lib/compression-telemetry.js +301 -49
  131. package/src/lib/content-recommendation.js +351 -0
  132. package/src/lib/content-recommender.js +317 -52
  133. package/src/lib/cowork-cron.js +298 -49
  134. package/src/lib/cowork-learning.js +333 -0
  135. package/src/lib/cowork-share.js +338 -0
  136. package/src/lib/cowork-task-runner.js +308 -51
  137. package/src/lib/cowork-workflow.js +327 -0
  138. package/src/lib/cross-chain.js +311 -51
  139. package/src/lib/dao-governance.js +280 -46
  140. package/src/lib/dbevo.js +351 -0
  141. package/src/lib/decentral-infra.js +330 -0
  142. package/src/lib/did-manager.js +341 -0
  143. package/src/lib/did-v2-manager.js +341 -0
  144. package/src/lib/dlp-engine.js +626 -49
  145. package/src/lib/evolution-system.js +278 -47
  146. package/src/lib/evomap-governance.js +334 -0
  147. package/src/lib/evomap-manager.js +280 -46
  148. package/src/lib/execution-backend.js +294 -48
  149. package/src/lib/feature-flags.js +294 -49
  150. package/src/lib/federation-hardening.js +340 -0
  151. package/src/lib/git-integration.js +285 -47
  152. package/src/lib/hardening-manager.js +341 -0
  153. package/src/lib/hierarchical-memory.js +284 -48
  154. package/src/lib/hook-manager.js +341 -0
  155. package/src/lib/inference-network.js +308 -51
  156. package/src/lib/instinct-manager.js +346 -0
  157. package/src/lib/ipfs-storage.js +334 -0
  158. package/src/lib/iteration-budget.js +302 -50
  159. package/src/lib/knowledge-exporter.js +341 -0
  160. package/src/lib/knowledge-graph.js +333 -0
  161. package/src/lib/knowledge-importer.js +341 -0
  162. package/src/lib/llm-providers.js +346 -0
  163. package/src/lib/matrix-bridge.js +620 -47
  164. package/src/lib/mcp-registry.js +346 -0
  165. package/src/lib/mcp-scaffold.js +318 -54
  166. package/src/lib/memory-injection.js +288 -49
  167. package/src/lib/memory-manager.js +336 -0
  168. package/src/lib/multimodal.js +330 -0
  169. package/src/lib/nl-programming.js +341 -0
  170. package/src/lib/nostr-bridge.js +622 -49
  171. package/src/lib/note-versioning.js +339 -0
  172. package/src/lib/orchestrator.js +293 -48
  173. package/src/lib/org-manager.js +336 -0
  174. package/src/lib/p2p-manager.js +341 -0
  175. package/src/lib/pdf-parser.js +298 -49
  176. package/src/lib/perception.js +346 -0
  177. package/src/lib/perf-tuning.js +309 -50
  178. package/src/lib/permanent-memory.js +327 -0
  179. package/src/lib/permission-engine.js +287 -49
  180. package/src/lib/pipeline-orchestrator.js +289 -49
  181. package/src/lib/plan-mode.js +298 -51
  182. package/src/lib/plugin-ecosystem.js +346 -0
  183. package/src/lib/pqc-manager.js +346 -0
  184. package/src/lib/privacy-computing.js +335 -0
  185. package/src/lib/protocol-fusion.js +338 -0
  186. package/src/lib/quantization.js +337 -0
  187. package/src/lib/reputation-optimizer.js +340 -0
  188. package/src/lib/response-cache.js +333 -0
  189. package/src/lib/sandbox-v2.js +327 -0
  190. package/src/lib/scim-manager.js +346 -0
  191. package/src/lib/service-container.js +313 -52
  192. package/src/lib/session-consolidator.js +296 -49
  193. package/src/lib/session-hooks.js +312 -53
  194. package/src/lib/session-manager.js +334 -0
  195. package/src/lib/session-search.js +304 -51
  196. package/src/lib/session-tail.js +288 -49
  197. package/src/lib/session-usage.js +298 -52
  198. package/src/lib/siem-exporter.js +298 -51
  199. package/src/lib/skill-loader.js +334 -0
  200. package/src/lib/skill-marketplace.js +345 -0
  201. package/src/lib/sla-manager.js +341 -0
  202. package/src/lib/slot-filler.js +303 -51
  203. package/src/lib/social-graph-analytics.js +295 -49
  204. package/src/lib/social-graph.js +272 -49
  205. package/src/lib/social-manager.js +330 -0
  206. package/src/lib/sso-manager.js +340 -0
  207. package/src/lib/stress-tester.js +342 -0
  208. package/src/lib/sub-agent-registry.js +302 -53
  209. package/src/lib/sync-manager.js +336 -0
  210. package/src/lib/task-model-selector.js +302 -50
  211. package/src/lib/tech-learning-engine.js +341 -0
  212. package/src/lib/tenant-saas.js +341 -0
  213. package/src/lib/terraform-manager.js +333 -0
  214. package/src/lib/threat-intel.js +330 -0
  215. package/src/lib/todo-manager.js +281 -47
  216. package/src/lib/token-incentive.js +341 -0
  217. package/src/lib/token-tracker.js +336 -0
  218. package/src/lib/topic-classifier.js +297 -49
  219. package/src/lib/trust-security.js +343 -0
  220. package/src/lib/ueba.js +340 -0
  221. package/src/lib/universal-runtime.js +330 -0
  222. package/src/lib/user-profile.js +294 -50
  223. package/src/lib/version-checker.js +304 -50
  224. package/src/lib/wallet-manager.js +336 -0
  225. package/src/lib/web-fetch.js +292 -51
  226. package/src/lib/workflow-engine.js +330 -0
  227. package/src/lib/zkp-engine.js +286 -49
@@ -173,81 +173,325 @@ export const MAX_USER_PROFILE_LENGTH = MAX_PROFILE_LENGTH;
173
173
 
174
174
  // ===== V2 Surface: User Profile governance overlay (CLI v0.141.0) =====
175
175
  export const USER_PROFILE_MATURITY_V2 = Object.freeze({
176
- PENDING: "pending", ACTIVE: "active", DORMANT: "dormant", ARCHIVED: "archived",
176
+ PENDING: "pending",
177
+ ACTIVE: "active",
178
+ DORMANT: "dormant",
179
+ ARCHIVED: "archived",
177
180
  });
178
181
  export const USER_PREF_LIFECYCLE_V2 = Object.freeze({
179
- PROPOSED: "proposed", APPLIED: "applied", REJECTED: "rejected", SUPERSEDED: "superseded", CANCELLED: "cancelled",
182
+ PROPOSED: "proposed",
183
+ APPLIED: "applied",
184
+ REJECTED: "rejected",
185
+ SUPERSEDED: "superseded",
186
+ CANCELLED: "cancelled",
180
187
  });
181
188
  const _upTrans = new Map([
182
- [USER_PROFILE_MATURITY_V2.PENDING, new Set([USER_PROFILE_MATURITY_V2.ACTIVE, USER_PROFILE_MATURITY_V2.ARCHIVED])],
183
- [USER_PROFILE_MATURITY_V2.ACTIVE, new Set([USER_PROFILE_MATURITY_V2.DORMANT, USER_PROFILE_MATURITY_V2.ARCHIVED])],
184
- [USER_PROFILE_MATURITY_V2.DORMANT, new Set([USER_PROFILE_MATURITY_V2.ACTIVE, USER_PROFILE_MATURITY_V2.ARCHIVED])],
189
+ [
190
+ USER_PROFILE_MATURITY_V2.PENDING,
191
+ new Set([
192
+ USER_PROFILE_MATURITY_V2.ACTIVE,
193
+ USER_PROFILE_MATURITY_V2.ARCHIVED,
194
+ ]),
195
+ ],
196
+ [
197
+ USER_PROFILE_MATURITY_V2.ACTIVE,
198
+ new Set([
199
+ USER_PROFILE_MATURITY_V2.DORMANT,
200
+ USER_PROFILE_MATURITY_V2.ARCHIVED,
201
+ ]),
202
+ ],
203
+ [
204
+ USER_PROFILE_MATURITY_V2.DORMANT,
205
+ new Set([
206
+ USER_PROFILE_MATURITY_V2.ACTIVE,
207
+ USER_PROFILE_MATURITY_V2.ARCHIVED,
208
+ ]),
209
+ ],
185
210
  [USER_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
186
211
  ]);
187
212
  const _upTerminal = new Set([USER_PROFILE_MATURITY_V2.ARCHIVED]);
188
213
  const _uprefTrans = new Map([
189
- [USER_PREF_LIFECYCLE_V2.PROPOSED, new Set([USER_PREF_LIFECYCLE_V2.APPLIED, USER_PREF_LIFECYCLE_V2.REJECTED, USER_PREF_LIFECYCLE_V2.CANCELLED])],
190
- [USER_PREF_LIFECYCLE_V2.APPLIED, new Set([USER_PREF_LIFECYCLE_V2.SUPERSEDED])],
214
+ [
215
+ USER_PREF_LIFECYCLE_V2.PROPOSED,
216
+ new Set([
217
+ USER_PREF_LIFECYCLE_V2.APPLIED,
218
+ USER_PREF_LIFECYCLE_V2.REJECTED,
219
+ USER_PREF_LIFECYCLE_V2.CANCELLED,
220
+ ]),
221
+ ],
222
+ [
223
+ USER_PREF_LIFECYCLE_V2.APPLIED,
224
+ new Set([USER_PREF_LIFECYCLE_V2.SUPERSEDED]),
225
+ ],
191
226
  [USER_PREF_LIFECYCLE_V2.REJECTED, new Set()],
192
227
  [USER_PREF_LIFECYCLE_V2.SUPERSEDED, new Set()],
193
228
  [USER_PREF_LIFECYCLE_V2.CANCELLED, new Set()],
194
229
  ]);
195
230
  const _upsV2 = new Map();
196
231
  const _uprefsV2 = new Map();
197
- let _upMaxActivePerOwner = 5, _upMaxPendingPrefsPerProfile = 20, _upIdleMs = 90 * 24 * 60 * 60 * 1000, _uprefStuckMs = 7 * 24 * 60 * 60 * 1000;
198
- function _upPos(n, label) { const v = Math.floor(Number(n)); if (!Number.isFinite(v) || v <= 0) throw new Error(`${label} must be positive integer`); return v; }
199
- function _upCheckP(from, to) { const a = _upTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid user profile transition ${from} → ${to}`); }
200
- function _uprefCheck(from, to) { const a = _uprefTrans.get(from); if (!a || !a.has(to)) throw new Error(`invalid user pref transition ${from} → ${to}`); }
201
- export function setMaxActiveUserProfilesPerOwnerV2(n) { _upMaxActivePerOwner = _upPos(n, "maxActiveUserProfilesPerOwner"); }
202
- export function getMaxActiveUserProfilesPerOwnerV2() { return _upMaxActivePerOwner; }
203
- export function setMaxPendingUserPrefsPerProfileV2(n) { _upMaxPendingPrefsPerProfile = _upPos(n, "maxPendingUserPrefsPerProfile"); }
204
- export function getMaxPendingUserPrefsPerProfileV2() { return _upMaxPendingPrefsPerProfile; }
205
- export function setUserProfileIdleMsV2(n) { _upIdleMs = _upPos(n, "userProfileIdleMs"); }
206
- export function getUserProfileIdleMsV2() { return _upIdleMs; }
207
- export function setUserPrefStuckMsV2(n) { _uprefStuckMs = _upPos(n, "userPrefStuckMs"); }
208
- export function getUserPrefStuckMsV2() { return _uprefStuckMs; }
209
- export function _resetStateUserProfileV2() { _upsV2.clear(); _uprefsV2.clear(); _upMaxActivePerOwner = 5; _upMaxPendingPrefsPerProfile = 20; _upIdleMs = 90 * 24 * 60 * 60 * 1000; _uprefStuckMs = 7 * 24 * 60 * 60 * 1000; }
232
+ let _upMaxActivePerOwner = 5,
233
+ _upMaxPendingPrefsPerProfile = 20,
234
+ _upIdleMs = 90 * 24 * 60 * 60 * 1000,
235
+ _uprefStuckMs = 7 * 24 * 60 * 60 * 1000;
236
+ function _upPos(n, label) {
237
+ const v = Math.floor(Number(n));
238
+ if (!Number.isFinite(v) || v <= 0)
239
+ throw new Error(`${label} must be positive integer`);
240
+ return v;
241
+ }
242
+ function _upCheckP(from, to) {
243
+ const a = _upTrans.get(from);
244
+ if (!a || !a.has(to))
245
+ throw new Error(`invalid user profile transition ${from} → ${to}`);
246
+ }
247
+ function _uprefCheck(from, to) {
248
+ const a = _uprefTrans.get(from);
249
+ if (!a || !a.has(to))
250
+ throw new Error(`invalid user pref transition ${from} → ${to}`);
251
+ }
252
+ export function setMaxActiveUserProfilesPerOwnerV2(n) {
253
+ _upMaxActivePerOwner = _upPos(n, "maxActiveUserProfilesPerOwner");
254
+ }
255
+ export function getMaxActiveUserProfilesPerOwnerV2() {
256
+ return _upMaxActivePerOwner;
257
+ }
258
+ export function setMaxPendingUserPrefsPerProfileV2(n) {
259
+ _upMaxPendingPrefsPerProfile = _upPos(n, "maxPendingUserPrefsPerProfile");
260
+ }
261
+ export function getMaxPendingUserPrefsPerProfileV2() {
262
+ return _upMaxPendingPrefsPerProfile;
263
+ }
264
+ export function setUserProfileIdleMsV2(n) {
265
+ _upIdleMs = _upPos(n, "userProfileIdleMs");
266
+ }
267
+ export function getUserProfileIdleMsV2() {
268
+ return _upIdleMs;
269
+ }
270
+ export function setUserPrefStuckMsV2(n) {
271
+ _uprefStuckMs = _upPos(n, "userPrefStuckMs");
272
+ }
273
+ export function getUserPrefStuckMsV2() {
274
+ return _uprefStuckMs;
275
+ }
276
+ export function _resetStateUserProfileV2() {
277
+ _upsV2.clear();
278
+ _uprefsV2.clear();
279
+ _upMaxActivePerOwner = 5;
280
+ _upMaxPendingPrefsPerProfile = 20;
281
+ _upIdleMs = 90 * 24 * 60 * 60 * 1000;
282
+ _uprefStuckMs = 7 * 24 * 60 * 60 * 1000;
283
+ }
210
284
  export function registerUserProfileV2({ id, owner, handle, metadata } = {}) {
211
- if (!id) throw new Error("user profile id required"); if (!owner) throw new Error("user profile owner required");
285
+ if (!id) throw new Error("user profile id required");
286
+ if (!owner) throw new Error("user profile owner required");
212
287
  if (_upsV2.has(id)) throw new Error(`user profile ${id} already registered`);
213
288
  const now = Date.now();
214
- const p = { id, owner, handle: handle || id, status: USER_PROFILE_MATURITY_V2.PENDING, createdAt: now, updatedAt: now, activatedAt: null, archivedAt: null, lastTouchedAt: now, metadata: { ...(metadata || {}) } };
215
- _upsV2.set(id, p); return { ...p, metadata: { ...p.metadata } };
289
+ const p = {
290
+ id,
291
+ owner,
292
+ handle: handle || id,
293
+ status: USER_PROFILE_MATURITY_V2.PENDING,
294
+ createdAt: now,
295
+ updatedAt: now,
296
+ activatedAt: null,
297
+ archivedAt: null,
298
+ lastTouchedAt: now,
299
+ metadata: { ...(metadata || {}) },
300
+ };
301
+ _upsV2.set(id, p);
302
+ return { ...p, metadata: { ...p.metadata } };
303
+ }
304
+ function _upCountActive(owner) {
305
+ let n = 0;
306
+ for (const p of _upsV2.values())
307
+ if (p.owner === owner && p.status === USER_PROFILE_MATURITY_V2.ACTIVE) n++;
308
+ return n;
216
309
  }
217
- function _upCountActive(owner) { let n = 0; for (const p of _upsV2.values()) if (p.owner === owner && p.status === USER_PROFILE_MATURITY_V2.ACTIVE) n++; return n; }
218
310
  export function activateUserProfileV2(id) {
219
- const p = _upsV2.get(id); if (!p) throw new Error(`user profile ${id} not found`);
311
+ const p = _upsV2.get(id);
312
+ if (!p) throw new Error(`user profile ${id} not found`);
220
313
  _upCheckP(p.status, USER_PROFILE_MATURITY_V2.ACTIVE);
221
314
  const recovery = p.status === USER_PROFILE_MATURITY_V2.DORMANT;
222
- if (!recovery && _upCountActive(p.owner) >= _upMaxActivePerOwner) throw new Error(`max active user profiles for owner ${p.owner} reached`);
223
- const now = Date.now(); p.status = USER_PROFILE_MATURITY_V2.ACTIVE; p.updatedAt = now; p.lastTouchedAt = now; if (!p.activatedAt) p.activatedAt = now;
315
+ if (!recovery && _upCountActive(p.owner) >= _upMaxActivePerOwner)
316
+ throw new Error(`max active user profiles for owner ${p.owner} reached`);
317
+ const now = Date.now();
318
+ p.status = USER_PROFILE_MATURITY_V2.ACTIVE;
319
+ p.updatedAt = now;
320
+ p.lastTouchedAt = now;
321
+ if (!p.activatedAt) p.activatedAt = now;
322
+ return { ...p, metadata: { ...p.metadata } };
323
+ }
324
+ export function dormantUserProfileV2(id) {
325
+ const p = _upsV2.get(id);
326
+ if (!p) throw new Error(`user profile ${id} not found`);
327
+ _upCheckP(p.status, USER_PROFILE_MATURITY_V2.DORMANT);
328
+ p.status = USER_PROFILE_MATURITY_V2.DORMANT;
329
+ p.updatedAt = Date.now();
330
+ return { ...p, metadata: { ...p.metadata } };
331
+ }
332
+ export function archiveUserProfileV2(id) {
333
+ const p = _upsV2.get(id);
334
+ if (!p) throw new Error(`user profile ${id} not found`);
335
+ _upCheckP(p.status, USER_PROFILE_MATURITY_V2.ARCHIVED);
336
+ const now = Date.now();
337
+ p.status = USER_PROFILE_MATURITY_V2.ARCHIVED;
338
+ p.updatedAt = now;
339
+ if (!p.archivedAt) p.archivedAt = now;
340
+ return { ...p, metadata: { ...p.metadata } };
341
+ }
342
+ export function touchUserProfileV2(id) {
343
+ const p = _upsV2.get(id);
344
+ if (!p) throw new Error(`user profile ${id} not found`);
345
+ if (_upTerminal.has(p.status))
346
+ throw new Error(`cannot touch terminal user profile ${id}`);
347
+ const now = Date.now();
348
+ p.lastTouchedAt = now;
349
+ p.updatedAt = now;
350
+ return { ...p, metadata: { ...p.metadata } };
351
+ }
352
+ export function getUserProfileV2(id) {
353
+ const p = _upsV2.get(id);
354
+ if (!p) return null;
224
355
  return { ...p, metadata: { ...p.metadata } };
225
356
  }
226
- export function dormantUserProfileV2(id) { const p = _upsV2.get(id); if (!p) throw new Error(`user profile ${id} not found`); _upCheckP(p.status, USER_PROFILE_MATURITY_V2.DORMANT); p.status = USER_PROFILE_MATURITY_V2.DORMANT; p.updatedAt = Date.now(); return { ...p, metadata: { ...p.metadata } }; }
227
- export function archiveUserProfileV2(id) { const p = _upsV2.get(id); if (!p) throw new Error(`user profile ${id} not found`); _upCheckP(p.status, USER_PROFILE_MATURITY_V2.ARCHIVED); const now = Date.now(); p.status = USER_PROFILE_MATURITY_V2.ARCHIVED; p.updatedAt = now; if (!p.archivedAt) p.archivedAt = now; return { ...p, metadata: { ...p.metadata } }; }
228
- export function touchUserProfileV2(id) { const p = _upsV2.get(id); if (!p) throw new Error(`user profile ${id} not found`); if (_upTerminal.has(p.status)) throw new Error(`cannot touch terminal user profile ${id}`); const now = Date.now(); p.lastTouchedAt = now; p.updatedAt = now; return { ...p, metadata: { ...p.metadata } }; }
229
- export function getUserProfileV2(id) { const p = _upsV2.get(id); if (!p) return null; return { ...p, metadata: { ...p.metadata } }; }
230
- export function listUserProfilesV2() { return [..._upsV2.values()].map((p) => ({ ...p, metadata: { ...p.metadata } })); }
231
- function _uprefCountPending(profileId) { let n = 0; for (const r of _uprefsV2.values()) if (r.profileId === profileId && r.status === USER_PREF_LIFECYCLE_V2.PROPOSED) n++; return n; }
357
+ export function listUserProfilesV2() {
358
+ return [..._upsV2.values()].map((p) => ({
359
+ ...p,
360
+ metadata: { ...p.metadata },
361
+ }));
362
+ }
363
+ function _uprefCountPending(profileId) {
364
+ let n = 0;
365
+ for (const r of _uprefsV2.values())
366
+ if (
367
+ r.profileId === profileId &&
368
+ r.status === USER_PREF_LIFECYCLE_V2.PROPOSED
369
+ )
370
+ n++;
371
+ return n;
372
+ }
232
373
  export function createUserPrefV2({ id, profileId, key, metadata } = {}) {
233
- if (!id) throw new Error("user pref id required"); if (!profileId) throw new Error("user pref profileId required");
374
+ if (!id) throw new Error("user pref id required");
375
+ if (!profileId) throw new Error("user pref profileId required");
234
376
  if (_uprefsV2.has(id)) throw new Error(`user pref ${id} already exists`);
235
- if (!_upsV2.has(profileId)) throw new Error(`user profile ${profileId} not found`);
236
- if (_uprefCountPending(profileId) >= _upMaxPendingPrefsPerProfile) throw new Error(`max pending user prefs for profile ${profileId} reached`);
377
+ if (!_upsV2.has(profileId))
378
+ throw new Error(`user profile ${profileId} not found`);
379
+ if (_uprefCountPending(profileId) >= _upMaxPendingPrefsPerProfile)
380
+ throw new Error(`max pending user prefs for profile ${profileId} reached`);
381
+ const now = Date.now();
382
+ const r = {
383
+ id,
384
+ profileId,
385
+ key: key || "",
386
+ status: USER_PREF_LIFECYCLE_V2.PROPOSED,
387
+ createdAt: now,
388
+ updatedAt: now,
389
+ startedAt: now,
390
+ settledAt: null,
391
+ metadata: { ...(metadata || {}) },
392
+ };
393
+ _uprefsV2.set(id, r);
394
+ return { ...r, metadata: { ...r.metadata } };
395
+ }
396
+ export function applyUserPrefV2(id) {
397
+ const r = _uprefsV2.get(id);
398
+ if (!r) throw new Error(`user pref ${id} not found`);
399
+ _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.APPLIED);
400
+ const now = Date.now();
401
+ r.status = USER_PREF_LIFECYCLE_V2.APPLIED;
402
+ r.updatedAt = now;
403
+ if (!r.settledAt) r.settledAt = now;
404
+ return { ...r, metadata: { ...r.metadata } };
405
+ }
406
+ export function rejectUserPrefV2(id, reason) {
407
+ const r = _uprefsV2.get(id);
408
+ if (!r) throw new Error(`user pref ${id} not found`);
409
+ _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.REJECTED);
410
+ const now = Date.now();
411
+ r.status = USER_PREF_LIFECYCLE_V2.REJECTED;
412
+ r.updatedAt = now;
413
+ if (!r.settledAt) r.settledAt = now;
414
+ if (reason) r.metadata.rejectReason = String(reason);
415
+ return { ...r, metadata: { ...r.metadata } };
416
+ }
417
+ export function supersedeUserPrefV2(id) {
418
+ const r = _uprefsV2.get(id);
419
+ if (!r) throw new Error(`user pref ${id} not found`);
420
+ _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.SUPERSEDED);
237
421
  const now = Date.now();
238
- const r = { id, profileId, key: key || "", status: USER_PREF_LIFECYCLE_V2.PROPOSED, createdAt: now, updatedAt: now, startedAt: now, settledAt: null, metadata: { ...(metadata || {}) } };
239
- _uprefsV2.set(id, r); return { ...r, metadata: { ...r.metadata } };
240
- }
241
- export function applyUserPrefV2(id) { const r = _uprefsV2.get(id); if (!r) throw new Error(`user pref ${id} not found`); _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.APPLIED); const now = Date.now(); r.status = USER_PREF_LIFECYCLE_V2.APPLIED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; return { ...r, metadata: { ...r.metadata } }; }
242
- export function rejectUserPrefV2(id, reason) { const r = _uprefsV2.get(id); if (!r) throw new Error(`user pref ${id} not found`); _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.REJECTED); const now = Date.now(); r.status = USER_PREF_LIFECYCLE_V2.REJECTED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; if (reason) r.metadata.rejectReason = String(reason); return { ...r, metadata: { ...r.metadata } }; }
243
- export function supersedeUserPrefV2(id) { const r = _uprefsV2.get(id); if (!r) throw new Error(`user pref ${id} not found`); _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.SUPERSEDED); const now = Date.now(); r.status = USER_PREF_LIFECYCLE_V2.SUPERSEDED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; return { ...r, metadata: { ...r.metadata } }; }
244
- export function cancelUserPrefV2(id, reason) { const r = _uprefsV2.get(id); if (!r) throw new Error(`user pref ${id} not found`); _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.CANCELLED); const now = Date.now(); r.status = USER_PREF_LIFECYCLE_V2.CANCELLED; r.updatedAt = now; if (!r.settledAt) r.settledAt = now; if (reason) r.metadata.cancelReason = String(reason); return { ...r, metadata: { ...r.metadata } }; }
245
- export function getUserPrefV2(id) { const r = _uprefsV2.get(id); if (!r) return null; return { ...r, metadata: { ...r.metadata } }; }
246
- export function listUserPrefsV2() { return [..._uprefsV2.values()].map((r) => ({ ...r, metadata: { ...r.metadata } })); }
247
- export function autoDormantIdleUserProfilesV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const p of _upsV2.values()) if (p.status === USER_PROFILE_MATURITY_V2.ACTIVE && (t - p.lastTouchedAt) >= _upIdleMs) { p.status = USER_PROFILE_MATURITY_V2.DORMANT; p.updatedAt = t; flipped.push(p.id); } return { flipped, count: flipped.length }; }
248
- export function autoCancelStaleUserPrefsV2({ now } = {}) { const t = now ?? Date.now(); const flipped = []; for (const r of _uprefsV2.values()) if (r.status === USER_PREF_LIFECYCLE_V2.PROPOSED && (t - r.startedAt) >= _uprefStuckMs) { r.status = USER_PREF_LIFECYCLE_V2.CANCELLED; r.updatedAt = t; if (!r.settledAt) r.settledAt = t; r.metadata.cancelReason = "auto-cancel-stale"; flipped.push(r.id); } return { flipped, count: flipped.length }; }
422
+ r.status = USER_PREF_LIFECYCLE_V2.SUPERSEDED;
423
+ r.updatedAt = now;
424
+ if (!r.settledAt) r.settledAt = now;
425
+ return { ...r, metadata: { ...r.metadata } };
426
+ }
427
+ export function cancelUserPrefV2(id, reason) {
428
+ const r = _uprefsV2.get(id);
429
+ if (!r) throw new Error(`user pref ${id} not found`);
430
+ _uprefCheck(r.status, USER_PREF_LIFECYCLE_V2.CANCELLED);
431
+ const now = Date.now();
432
+ r.status = USER_PREF_LIFECYCLE_V2.CANCELLED;
433
+ r.updatedAt = now;
434
+ if (!r.settledAt) r.settledAt = now;
435
+ if (reason) r.metadata.cancelReason = String(reason);
436
+ return { ...r, metadata: { ...r.metadata } };
437
+ }
438
+ export function getUserPrefV2(id) {
439
+ const r = _uprefsV2.get(id);
440
+ if (!r) return null;
441
+ return { ...r, metadata: { ...r.metadata } };
442
+ }
443
+ export function listUserPrefsV2() {
444
+ return [..._uprefsV2.values()].map((r) => ({
445
+ ...r,
446
+ metadata: { ...r.metadata },
447
+ }));
448
+ }
449
+ export function autoDormantIdleUserProfilesV2({ now } = {}) {
450
+ const t = now ?? Date.now();
451
+ const flipped = [];
452
+ for (const p of _upsV2.values())
453
+ if (
454
+ p.status === USER_PROFILE_MATURITY_V2.ACTIVE &&
455
+ t - p.lastTouchedAt >= _upIdleMs
456
+ ) {
457
+ p.status = USER_PROFILE_MATURITY_V2.DORMANT;
458
+ p.updatedAt = t;
459
+ flipped.push(p.id);
460
+ }
461
+ return { flipped, count: flipped.length };
462
+ }
463
+ export function autoCancelStaleUserPrefsV2({ now } = {}) {
464
+ const t = now ?? Date.now();
465
+ const flipped = [];
466
+ for (const r of _uprefsV2.values())
467
+ if (
468
+ r.status === USER_PREF_LIFECYCLE_V2.PROPOSED &&
469
+ t - r.startedAt >= _uprefStuckMs
470
+ ) {
471
+ r.status = USER_PREF_LIFECYCLE_V2.CANCELLED;
472
+ r.updatedAt = t;
473
+ if (!r.settledAt) r.settledAt = t;
474
+ r.metadata.cancelReason = "auto-cancel-stale";
475
+ flipped.push(r.id);
476
+ }
477
+ return { flipped, count: flipped.length };
478
+ }
249
479
  export function getUserProfileGovStatsV2() {
250
- const profilesByStatus = {}; for (const v of Object.values(USER_PROFILE_MATURITY_V2)) profilesByStatus[v] = 0; for (const p of _upsV2.values()) profilesByStatus[p.status]++;
251
- const prefsByStatus = {}; for (const v of Object.values(USER_PREF_LIFECYCLE_V2)) prefsByStatus[v] = 0; for (const r of _uprefsV2.values()) prefsByStatus[r.status]++;
252
- return { totalUserProfilesV2: _upsV2.size, totalUserPrefsV2: _uprefsV2.size, maxActiveUserProfilesPerOwner: _upMaxActivePerOwner, maxPendingUserPrefsPerProfile: _upMaxPendingPrefsPerProfile, userProfileIdleMs: _upIdleMs, userPrefStuckMs: _uprefStuckMs, profilesByStatus, prefsByStatus };
480
+ const profilesByStatus = {};
481
+ for (const v of Object.values(USER_PROFILE_MATURITY_V2))
482
+ profilesByStatus[v] = 0;
483
+ for (const p of _upsV2.values()) profilesByStatus[p.status]++;
484
+ const prefsByStatus = {};
485
+ for (const v of Object.values(USER_PREF_LIFECYCLE_V2)) prefsByStatus[v] = 0;
486
+ for (const r of _uprefsV2.values()) prefsByStatus[r.status]++;
487
+ return {
488
+ totalUserProfilesV2: _upsV2.size,
489
+ totalUserPrefsV2: _uprefsV2.size,
490
+ maxActiveUserProfilesPerOwner: _upMaxActivePerOwner,
491
+ maxPendingUserPrefsPerProfile: _upMaxPendingPrefsPerProfile,
492
+ userProfileIdleMs: _upIdleMs,
493
+ userPrefStuckMs: _uprefStuckMs,
494
+ profilesByStatus,
495
+ prefsByStatus,
496
+ };
253
497
  }