chainlesschain 0.152.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.
- package/README.md +52 -3
- package/package.json +1 -1
- package/src/commands/a2a.js +201 -0
- package/src/commands/agent.js +1250 -0
- package/src/commands/chat.js +605 -0
- package/src/commands/cli-anything.js +426 -0
- package/src/commands/compliance.js +412 -0
- package/src/commands/config.js +213 -0
- package/src/commands/cowork.js +1463 -0
- package/src/commands/crosschain.js +203 -0
- package/src/commands/dao.js +203 -0
- package/src/commands/economy.js +199 -0
- package/src/commands/encrypt.js +201 -0
- package/src/commands/evolution.js +199 -0
- package/src/commands/evomap.js +625 -0
- package/src/commands/hmemory.js +203 -0
- package/src/commands/inference.js +207 -0
- package/src/commands/kg.js +195 -0
- package/src/commands/llm.js +209 -0
- package/src/commands/memory.js +203 -0
- package/src/commands/orchestrate.js +406 -0
- package/src/commands/pipeline.js +199 -0
- package/src/commands/planmode.js +426 -0
- package/src/commands/plugin.js +209 -0
- package/src/commands/services.js +207 -0
- package/src/commands/setup.js +205 -0
- package/src/commands/skill.js +207 -0
- package/src/commands/start.js +209 -0
- package/src/commands/stream.js +213 -0
- package/src/commands/ui.js +225 -0
- package/src/commands/workflow.js +209 -0
- package/src/index.js +112 -0
- package/src/lib/a2a-protocol.js +332 -0
- package/src/lib/agent-coordinator.js +334 -0
- package/src/lib/agent-economy.js +334 -0
- package/src/lib/agent-router.js +333 -0
- package/src/lib/autonomous-agent.js +332 -0
- package/src/lib/chat-core.js +335 -0
- package/src/lib/cli-anything-bridge.js +341 -0
- package/src/lib/cli-context-engineering.js +351 -0
- package/src/lib/compliance-manager.js +334 -0
- package/src/lib/cowork-adapter.js +336 -0
- package/src/lib/cowork-evomap-adapter.js +341 -0
- package/src/lib/cowork-mcp-tools.js +341 -0
- package/src/lib/cowork-observe-html.js +341 -0
- package/src/lib/cowork-observe.js +341 -0
- package/src/lib/cowork-task-templates.js +342 -1
- package/src/lib/cowork-template-marketplace.js +340 -0
- package/src/lib/cross-chain.js +339 -0
- package/src/lib/crypto-manager.js +334 -0
- package/src/lib/dao-governance.js +339 -0
- package/src/lib/downloader.js +334 -0
- package/src/lib/evolution-system.js +334 -0
- package/src/lib/evomap-client.js +342 -0
- package/src/lib/evomap-federation.js +338 -0
- package/src/lib/evomap-manager.js +330 -0
- package/src/lib/execution-backend.js +330 -0
- package/src/lib/hashline.js +338 -0
- package/src/lib/hierarchical-memory.js +334 -0
- package/src/lib/inference-network.js +341 -0
- package/src/lib/interaction-adapter.js +330 -0
- package/src/lib/interactive-planner.js +354 -0
- package/src/lib/knowledge-graph.js +331 -0
- package/src/lib/pipeline-orchestrator.js +332 -0
- package/src/lib/plan-mode.js +336 -0
- package/src/lib/plugin-autodiscovery.js +334 -0
- package/src/lib/process-manager.js +336 -0
- package/src/lib/provider-options.js +346 -0
- package/src/lib/provider-stream.js +348 -0
- package/src/lib/service-manager.js +337 -0
- package/src/lib/session-core-singletons.js +341 -0
- package/src/lib/skill-mcp.js +336 -0
- package/src/lib/stix-parser.js +346 -0
- package/src/lib/sub-agent-context.js +343 -0
- package/src/lib/sub-agent-profiles.js +335 -0
- package/src/lib/sub-agent-registry.js +336 -0
- package/src/lib/todo-manager.js +336 -0
- package/src/lib/web-ui-server.js +348 -0
- package/src/lib/workflow-expr.js +346 -0
- package/src/lib/ws-chat-handler.js +337 -0
package/src/lib/hashline.js
CHANGED
|
@@ -206,3 +206,341 @@ export const _internals = {
|
|
|
206
206
|
EMPTY_HASH,
|
|
207
207
|
SEPARATOR,
|
|
208
208
|
};
|
|
209
|
+
|
|
210
|
+
// =====================================================================
|
|
211
|
+
// hashline V2 governance overlay (iter26)
|
|
212
|
+
// =====================================================================
|
|
213
|
+
export const HLGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
214
|
+
PENDING: "pending",
|
|
215
|
+
ACTIVE: "active",
|
|
216
|
+
STALE: "stale",
|
|
217
|
+
ARCHIVED: "archived",
|
|
218
|
+
});
|
|
219
|
+
export const HLGOV_DIGEST_LIFECYCLE_V2 = Object.freeze({
|
|
220
|
+
QUEUED: "queued",
|
|
221
|
+
HASHING: "hashing",
|
|
222
|
+
HASHED: "hashed",
|
|
223
|
+
FAILED: "failed",
|
|
224
|
+
CANCELLED: "cancelled",
|
|
225
|
+
});
|
|
226
|
+
const _hlgovPTrans = new Map([
|
|
227
|
+
[
|
|
228
|
+
HLGOV_PROFILE_MATURITY_V2.PENDING,
|
|
229
|
+
new Set([
|
|
230
|
+
HLGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
231
|
+
HLGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
232
|
+
]),
|
|
233
|
+
],
|
|
234
|
+
[
|
|
235
|
+
HLGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
236
|
+
new Set([
|
|
237
|
+
HLGOV_PROFILE_MATURITY_V2.STALE,
|
|
238
|
+
HLGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
239
|
+
]),
|
|
240
|
+
],
|
|
241
|
+
[
|
|
242
|
+
HLGOV_PROFILE_MATURITY_V2.STALE,
|
|
243
|
+
new Set([
|
|
244
|
+
HLGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
245
|
+
HLGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
246
|
+
]),
|
|
247
|
+
],
|
|
248
|
+
[HLGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
249
|
+
]);
|
|
250
|
+
const _hlgovPTerminal = new Set([HLGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
251
|
+
const _hlgovJTrans = new Map([
|
|
252
|
+
[
|
|
253
|
+
HLGOV_DIGEST_LIFECYCLE_V2.QUEUED,
|
|
254
|
+
new Set([
|
|
255
|
+
HLGOV_DIGEST_LIFECYCLE_V2.HASHING,
|
|
256
|
+
HLGOV_DIGEST_LIFECYCLE_V2.CANCELLED,
|
|
257
|
+
]),
|
|
258
|
+
],
|
|
259
|
+
[
|
|
260
|
+
HLGOV_DIGEST_LIFECYCLE_V2.HASHING,
|
|
261
|
+
new Set([
|
|
262
|
+
HLGOV_DIGEST_LIFECYCLE_V2.HASHED,
|
|
263
|
+
HLGOV_DIGEST_LIFECYCLE_V2.FAILED,
|
|
264
|
+
HLGOV_DIGEST_LIFECYCLE_V2.CANCELLED,
|
|
265
|
+
]),
|
|
266
|
+
],
|
|
267
|
+
[HLGOV_DIGEST_LIFECYCLE_V2.HASHED, new Set()],
|
|
268
|
+
[HLGOV_DIGEST_LIFECYCLE_V2.FAILED, new Set()],
|
|
269
|
+
[HLGOV_DIGEST_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
270
|
+
]);
|
|
271
|
+
const _hlgovPsV2 = new Map();
|
|
272
|
+
const _hlgovJsV2 = new Map();
|
|
273
|
+
let _hlgovMaxActive = 8,
|
|
274
|
+
_hlgovMaxPending = 20,
|
|
275
|
+
_hlgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
276
|
+
_hlgovStuckMs = 60 * 1000;
|
|
277
|
+
function _hlgovPos(n, label) {
|
|
278
|
+
const v = Math.floor(Number(n));
|
|
279
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
280
|
+
throw new Error(`${label} must be positive integer`);
|
|
281
|
+
return v;
|
|
282
|
+
}
|
|
283
|
+
function _hlgovCheckP(from, to) {
|
|
284
|
+
const a = _hlgovPTrans.get(from);
|
|
285
|
+
if (!a || !a.has(to))
|
|
286
|
+
throw new Error(`invalid hlgov profile transition ${from} → ${to}`);
|
|
287
|
+
}
|
|
288
|
+
function _hlgovCheckJ(from, to) {
|
|
289
|
+
const a = _hlgovJTrans.get(from);
|
|
290
|
+
if (!a || !a.has(to))
|
|
291
|
+
throw new Error(`invalid hlgov digest transition ${from} → ${to}`);
|
|
292
|
+
}
|
|
293
|
+
function _hlgovCountActive(owner) {
|
|
294
|
+
let c = 0;
|
|
295
|
+
for (const p of _hlgovPsV2.values())
|
|
296
|
+
if (p.owner === owner && p.status === HLGOV_PROFILE_MATURITY_V2.ACTIVE) c++;
|
|
297
|
+
return c;
|
|
298
|
+
}
|
|
299
|
+
function _hlgovCountPending(profileId) {
|
|
300
|
+
let c = 0;
|
|
301
|
+
for (const j of _hlgovJsV2.values())
|
|
302
|
+
if (
|
|
303
|
+
j.profileId === profileId &&
|
|
304
|
+
(j.status === HLGOV_DIGEST_LIFECYCLE_V2.QUEUED ||
|
|
305
|
+
j.status === HLGOV_DIGEST_LIFECYCLE_V2.HASHING)
|
|
306
|
+
)
|
|
307
|
+
c++;
|
|
308
|
+
return c;
|
|
309
|
+
}
|
|
310
|
+
export function setMaxActiveHlgovProfilesPerOwnerV2(n) {
|
|
311
|
+
_hlgovMaxActive = _hlgovPos(n, "maxActiveHlgovProfilesPerOwner");
|
|
312
|
+
}
|
|
313
|
+
export function getMaxActiveHlgovProfilesPerOwnerV2() {
|
|
314
|
+
return _hlgovMaxActive;
|
|
315
|
+
}
|
|
316
|
+
export function setMaxPendingHlgovDigestsPerProfileV2(n) {
|
|
317
|
+
_hlgovMaxPending = _hlgovPos(n, "maxPendingHlgovDigestsPerProfile");
|
|
318
|
+
}
|
|
319
|
+
export function getMaxPendingHlgovDigestsPerProfileV2() {
|
|
320
|
+
return _hlgovMaxPending;
|
|
321
|
+
}
|
|
322
|
+
export function setHlgovProfileIdleMsV2(n) {
|
|
323
|
+
_hlgovIdleMs = _hlgovPos(n, "hlgovProfileIdleMs");
|
|
324
|
+
}
|
|
325
|
+
export function getHlgovProfileIdleMsV2() {
|
|
326
|
+
return _hlgovIdleMs;
|
|
327
|
+
}
|
|
328
|
+
export function setHlgovDigestStuckMsV2(n) {
|
|
329
|
+
_hlgovStuckMs = _hlgovPos(n, "hlgovDigestStuckMs");
|
|
330
|
+
}
|
|
331
|
+
export function getHlgovDigestStuckMsV2() {
|
|
332
|
+
return _hlgovStuckMs;
|
|
333
|
+
}
|
|
334
|
+
export function _resetStateHashlineGovV2() {
|
|
335
|
+
_hlgovPsV2.clear();
|
|
336
|
+
_hlgovJsV2.clear();
|
|
337
|
+
_hlgovMaxActive = 8;
|
|
338
|
+
_hlgovMaxPending = 20;
|
|
339
|
+
_hlgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
340
|
+
_hlgovStuckMs = 60 * 1000;
|
|
341
|
+
}
|
|
342
|
+
export function registerHlgovProfileV2({
|
|
343
|
+
id,
|
|
344
|
+
owner,
|
|
345
|
+
algorithm,
|
|
346
|
+
metadata,
|
|
347
|
+
} = {}) {
|
|
348
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
349
|
+
if (_hlgovPsV2.has(id)) throw new Error(`hlgov profile ${id} already exists`);
|
|
350
|
+
const now = Date.now();
|
|
351
|
+
const p = {
|
|
352
|
+
id,
|
|
353
|
+
owner,
|
|
354
|
+
algorithm: algorithm || "sha256",
|
|
355
|
+
status: HLGOV_PROFILE_MATURITY_V2.PENDING,
|
|
356
|
+
createdAt: now,
|
|
357
|
+
updatedAt: now,
|
|
358
|
+
lastTouchedAt: now,
|
|
359
|
+
activatedAt: null,
|
|
360
|
+
archivedAt: null,
|
|
361
|
+
metadata: { ...(metadata || {}) },
|
|
362
|
+
};
|
|
363
|
+
_hlgovPsV2.set(id, p);
|
|
364
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
365
|
+
}
|
|
366
|
+
export function activateHlgovProfileV2(id) {
|
|
367
|
+
const p = _hlgovPsV2.get(id);
|
|
368
|
+
if (!p) throw new Error(`hlgov profile ${id} not found`);
|
|
369
|
+
const isInitial = p.status === HLGOV_PROFILE_MATURITY_V2.PENDING;
|
|
370
|
+
_hlgovCheckP(p.status, HLGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
371
|
+
if (isInitial && _hlgovCountActive(p.owner) >= _hlgovMaxActive)
|
|
372
|
+
throw new Error(`max active hlgov profiles for owner ${p.owner} reached`);
|
|
373
|
+
const now = Date.now();
|
|
374
|
+
p.status = HLGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
375
|
+
p.updatedAt = now;
|
|
376
|
+
p.lastTouchedAt = now;
|
|
377
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
378
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
379
|
+
}
|
|
380
|
+
export function staleHlgovProfileV2(id) {
|
|
381
|
+
const p = _hlgovPsV2.get(id);
|
|
382
|
+
if (!p) throw new Error(`hlgov profile ${id} not found`);
|
|
383
|
+
_hlgovCheckP(p.status, HLGOV_PROFILE_MATURITY_V2.STALE);
|
|
384
|
+
p.status = HLGOV_PROFILE_MATURITY_V2.STALE;
|
|
385
|
+
p.updatedAt = Date.now();
|
|
386
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
387
|
+
}
|
|
388
|
+
export function archiveHlgovProfileV2(id) {
|
|
389
|
+
const p = _hlgovPsV2.get(id);
|
|
390
|
+
if (!p) throw new Error(`hlgov profile ${id} not found`);
|
|
391
|
+
_hlgovCheckP(p.status, HLGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
392
|
+
const now = Date.now();
|
|
393
|
+
p.status = HLGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
394
|
+
p.updatedAt = now;
|
|
395
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
396
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
397
|
+
}
|
|
398
|
+
export function touchHlgovProfileV2(id) {
|
|
399
|
+
const p = _hlgovPsV2.get(id);
|
|
400
|
+
if (!p) throw new Error(`hlgov profile ${id} not found`);
|
|
401
|
+
if (_hlgovPTerminal.has(p.status))
|
|
402
|
+
throw new Error(`cannot touch terminal hlgov profile ${id}`);
|
|
403
|
+
const now = Date.now();
|
|
404
|
+
p.lastTouchedAt = now;
|
|
405
|
+
p.updatedAt = now;
|
|
406
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
407
|
+
}
|
|
408
|
+
export function getHlgovProfileV2(id) {
|
|
409
|
+
const p = _hlgovPsV2.get(id);
|
|
410
|
+
if (!p) return null;
|
|
411
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
412
|
+
}
|
|
413
|
+
export function listHlgovProfilesV2() {
|
|
414
|
+
return [..._hlgovPsV2.values()].map((p) => ({
|
|
415
|
+
...p,
|
|
416
|
+
metadata: { ...p.metadata },
|
|
417
|
+
}));
|
|
418
|
+
}
|
|
419
|
+
export function createHlgovDigestV2({ id, profileId, content, metadata } = {}) {
|
|
420
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
421
|
+
if (_hlgovJsV2.has(id)) throw new Error(`hlgov digest ${id} already exists`);
|
|
422
|
+
if (!_hlgovPsV2.has(profileId))
|
|
423
|
+
throw new Error(`hlgov profile ${profileId} not found`);
|
|
424
|
+
if (_hlgovCountPending(profileId) >= _hlgovMaxPending)
|
|
425
|
+
throw new Error(
|
|
426
|
+
`max pending hlgov digests for profile ${profileId} reached`,
|
|
427
|
+
);
|
|
428
|
+
const now = Date.now();
|
|
429
|
+
const j = {
|
|
430
|
+
id,
|
|
431
|
+
profileId,
|
|
432
|
+
content: content || "",
|
|
433
|
+
status: HLGOV_DIGEST_LIFECYCLE_V2.QUEUED,
|
|
434
|
+
createdAt: now,
|
|
435
|
+
updatedAt: now,
|
|
436
|
+
startedAt: null,
|
|
437
|
+
settledAt: null,
|
|
438
|
+
metadata: { ...(metadata || {}) },
|
|
439
|
+
};
|
|
440
|
+
_hlgovJsV2.set(id, j);
|
|
441
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
442
|
+
}
|
|
443
|
+
export function hashingHlgovDigestV2(id) {
|
|
444
|
+
const j = _hlgovJsV2.get(id);
|
|
445
|
+
if (!j) throw new Error(`hlgov digest ${id} not found`);
|
|
446
|
+
_hlgovCheckJ(j.status, HLGOV_DIGEST_LIFECYCLE_V2.HASHING);
|
|
447
|
+
const now = Date.now();
|
|
448
|
+
j.status = HLGOV_DIGEST_LIFECYCLE_V2.HASHING;
|
|
449
|
+
j.updatedAt = now;
|
|
450
|
+
if (!j.startedAt) j.startedAt = now;
|
|
451
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
452
|
+
}
|
|
453
|
+
export function completeDigestHlgovV2(id) {
|
|
454
|
+
const j = _hlgovJsV2.get(id);
|
|
455
|
+
if (!j) throw new Error(`hlgov digest ${id} not found`);
|
|
456
|
+
_hlgovCheckJ(j.status, HLGOV_DIGEST_LIFECYCLE_V2.HASHED);
|
|
457
|
+
const now = Date.now();
|
|
458
|
+
j.status = HLGOV_DIGEST_LIFECYCLE_V2.HASHED;
|
|
459
|
+
j.updatedAt = now;
|
|
460
|
+
if (!j.settledAt) j.settledAt = now;
|
|
461
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
462
|
+
}
|
|
463
|
+
export function failHlgovDigestV2(id, reason) {
|
|
464
|
+
const j = _hlgovJsV2.get(id);
|
|
465
|
+
if (!j) throw new Error(`hlgov digest ${id} not found`);
|
|
466
|
+
_hlgovCheckJ(j.status, HLGOV_DIGEST_LIFECYCLE_V2.FAILED);
|
|
467
|
+
const now = Date.now();
|
|
468
|
+
j.status = HLGOV_DIGEST_LIFECYCLE_V2.FAILED;
|
|
469
|
+
j.updatedAt = now;
|
|
470
|
+
if (!j.settledAt) j.settledAt = now;
|
|
471
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
472
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
473
|
+
}
|
|
474
|
+
export function cancelHlgovDigestV2(id, reason) {
|
|
475
|
+
const j = _hlgovJsV2.get(id);
|
|
476
|
+
if (!j) throw new Error(`hlgov digest ${id} not found`);
|
|
477
|
+
_hlgovCheckJ(j.status, HLGOV_DIGEST_LIFECYCLE_V2.CANCELLED);
|
|
478
|
+
const now = Date.now();
|
|
479
|
+
j.status = HLGOV_DIGEST_LIFECYCLE_V2.CANCELLED;
|
|
480
|
+
j.updatedAt = now;
|
|
481
|
+
if (!j.settledAt) j.settledAt = now;
|
|
482
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
483
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
484
|
+
}
|
|
485
|
+
export function getHlgovDigestV2(id) {
|
|
486
|
+
const j = _hlgovJsV2.get(id);
|
|
487
|
+
if (!j) return null;
|
|
488
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
489
|
+
}
|
|
490
|
+
export function listHlgovDigestsV2() {
|
|
491
|
+
return [..._hlgovJsV2.values()].map((j) => ({
|
|
492
|
+
...j,
|
|
493
|
+
metadata: { ...j.metadata },
|
|
494
|
+
}));
|
|
495
|
+
}
|
|
496
|
+
export function autoStaleIdleHlgovProfilesV2({ now } = {}) {
|
|
497
|
+
const t = now ?? Date.now();
|
|
498
|
+
const flipped = [];
|
|
499
|
+
for (const p of _hlgovPsV2.values())
|
|
500
|
+
if (
|
|
501
|
+
p.status === HLGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
502
|
+
t - p.lastTouchedAt >= _hlgovIdleMs
|
|
503
|
+
) {
|
|
504
|
+
p.status = HLGOV_PROFILE_MATURITY_V2.STALE;
|
|
505
|
+
p.updatedAt = t;
|
|
506
|
+
flipped.push(p.id);
|
|
507
|
+
}
|
|
508
|
+
return { flipped, count: flipped.length };
|
|
509
|
+
}
|
|
510
|
+
export function autoFailStuckHlgovDigestsV2({ now } = {}) {
|
|
511
|
+
const t = now ?? Date.now();
|
|
512
|
+
const flipped = [];
|
|
513
|
+
for (const j of _hlgovJsV2.values())
|
|
514
|
+
if (
|
|
515
|
+
j.status === HLGOV_DIGEST_LIFECYCLE_V2.HASHING &&
|
|
516
|
+
j.startedAt != null &&
|
|
517
|
+
t - j.startedAt >= _hlgovStuckMs
|
|
518
|
+
) {
|
|
519
|
+
j.status = HLGOV_DIGEST_LIFECYCLE_V2.FAILED;
|
|
520
|
+
j.updatedAt = t;
|
|
521
|
+
if (!j.settledAt) j.settledAt = t;
|
|
522
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
523
|
+
flipped.push(j.id);
|
|
524
|
+
}
|
|
525
|
+
return { flipped, count: flipped.length };
|
|
526
|
+
}
|
|
527
|
+
export function getHashlineGovStatsV2() {
|
|
528
|
+
const profilesByStatus = {};
|
|
529
|
+
for (const v of Object.values(HLGOV_PROFILE_MATURITY_V2))
|
|
530
|
+
profilesByStatus[v] = 0;
|
|
531
|
+
for (const p of _hlgovPsV2.values()) profilesByStatus[p.status]++;
|
|
532
|
+
const digestsByStatus = {};
|
|
533
|
+
for (const v of Object.values(HLGOV_DIGEST_LIFECYCLE_V2))
|
|
534
|
+
digestsByStatus[v] = 0;
|
|
535
|
+
for (const j of _hlgovJsV2.values()) digestsByStatus[j.status]++;
|
|
536
|
+
return {
|
|
537
|
+
totalHlgovProfilesV2: _hlgovPsV2.size,
|
|
538
|
+
totalHlgovDigestsV2: _hlgovJsV2.size,
|
|
539
|
+
maxActiveHlgovProfilesPerOwner: _hlgovMaxActive,
|
|
540
|
+
maxPendingHlgovDigestsPerProfile: _hlgovMaxPending,
|
|
541
|
+
hlgovProfileIdleMs: _hlgovIdleMs,
|
|
542
|
+
hlgovDigestStuckMs: _hlgovStuckMs,
|
|
543
|
+
profilesByStatus,
|
|
544
|
+
digestsByStatus,
|
|
545
|
+
};
|
|
546
|
+
}
|
|
@@ -1409,3 +1409,337 @@ export function getHierarchicalMemoryGovStatsV2() {
|
|
|
1409
1409
|
promotionsByStatus,
|
|
1410
1410
|
};
|
|
1411
1411
|
}
|
|
1412
|
+
|
|
1413
|
+
// === Iter28 V2 governance overlay: Hmemgov ===
|
|
1414
|
+
export const HMEMGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
1415
|
+
PENDING: "pending",
|
|
1416
|
+
ACTIVE: "active",
|
|
1417
|
+
STALE: "stale",
|
|
1418
|
+
ARCHIVED: "archived",
|
|
1419
|
+
});
|
|
1420
|
+
export const HMEMGOV_RECALL_LIFECYCLE_V2 = Object.freeze({
|
|
1421
|
+
QUEUED: "queued",
|
|
1422
|
+
RECALLING: "recalling",
|
|
1423
|
+
RECALLED: "recalled",
|
|
1424
|
+
FAILED: "failed",
|
|
1425
|
+
CANCELLED: "cancelled",
|
|
1426
|
+
});
|
|
1427
|
+
const _hmemgovPTrans = new Map([
|
|
1428
|
+
[
|
|
1429
|
+
HMEMGOV_PROFILE_MATURITY_V2.PENDING,
|
|
1430
|
+
new Set([
|
|
1431
|
+
HMEMGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
1432
|
+
HMEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
1433
|
+
]),
|
|
1434
|
+
],
|
|
1435
|
+
[
|
|
1436
|
+
HMEMGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
1437
|
+
new Set([
|
|
1438
|
+
HMEMGOV_PROFILE_MATURITY_V2.STALE,
|
|
1439
|
+
HMEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
1440
|
+
]),
|
|
1441
|
+
],
|
|
1442
|
+
[
|
|
1443
|
+
HMEMGOV_PROFILE_MATURITY_V2.STALE,
|
|
1444
|
+
new Set([
|
|
1445
|
+
HMEMGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
1446
|
+
HMEMGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
1447
|
+
]),
|
|
1448
|
+
],
|
|
1449
|
+
[HMEMGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
1450
|
+
]);
|
|
1451
|
+
const _hmemgovPTerminal = new Set([HMEMGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
1452
|
+
const _hmemgovJTrans = new Map([
|
|
1453
|
+
[
|
|
1454
|
+
HMEMGOV_RECALL_LIFECYCLE_V2.QUEUED,
|
|
1455
|
+
new Set([
|
|
1456
|
+
HMEMGOV_RECALL_LIFECYCLE_V2.RECALLING,
|
|
1457
|
+
HMEMGOV_RECALL_LIFECYCLE_V2.CANCELLED,
|
|
1458
|
+
]),
|
|
1459
|
+
],
|
|
1460
|
+
[
|
|
1461
|
+
HMEMGOV_RECALL_LIFECYCLE_V2.RECALLING,
|
|
1462
|
+
new Set([
|
|
1463
|
+
HMEMGOV_RECALL_LIFECYCLE_V2.RECALLED,
|
|
1464
|
+
HMEMGOV_RECALL_LIFECYCLE_V2.FAILED,
|
|
1465
|
+
HMEMGOV_RECALL_LIFECYCLE_V2.CANCELLED,
|
|
1466
|
+
]),
|
|
1467
|
+
],
|
|
1468
|
+
[HMEMGOV_RECALL_LIFECYCLE_V2.RECALLED, new Set()],
|
|
1469
|
+
[HMEMGOV_RECALL_LIFECYCLE_V2.FAILED, new Set()],
|
|
1470
|
+
[HMEMGOV_RECALL_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
1471
|
+
]);
|
|
1472
|
+
const _hmemgovPsV2 = new Map();
|
|
1473
|
+
const _hmemgovJsV2 = new Map();
|
|
1474
|
+
let _hmemgovMaxActive = 10,
|
|
1475
|
+
_hmemgovMaxPending = 30,
|
|
1476
|
+
_hmemgovIdleMs = 2592000000,
|
|
1477
|
+
_hmemgovStuckMs = 60 * 1000;
|
|
1478
|
+
function _hmemgovPos(n, label) {
|
|
1479
|
+
const v = Math.floor(Number(n));
|
|
1480
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
1481
|
+
throw new Error(`${label} must be positive integer`);
|
|
1482
|
+
return v;
|
|
1483
|
+
}
|
|
1484
|
+
function _hmemgovCheckP(from, to) {
|
|
1485
|
+
const a = _hmemgovPTrans.get(from);
|
|
1486
|
+
if (!a || !a.has(to))
|
|
1487
|
+
throw new Error(`invalid hmemgov profile transition ${from} → ${to}`);
|
|
1488
|
+
}
|
|
1489
|
+
function _hmemgovCheckJ(from, to) {
|
|
1490
|
+
const a = _hmemgovJTrans.get(from);
|
|
1491
|
+
if (!a || !a.has(to))
|
|
1492
|
+
throw new Error(`invalid hmemgov recall transition ${from} → ${to}`);
|
|
1493
|
+
}
|
|
1494
|
+
function _hmemgovCountActive(owner) {
|
|
1495
|
+
let c = 0;
|
|
1496
|
+
for (const p of _hmemgovPsV2.values())
|
|
1497
|
+
if (p.owner === owner && p.status === HMEMGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
1498
|
+
c++;
|
|
1499
|
+
return c;
|
|
1500
|
+
}
|
|
1501
|
+
function _hmemgovCountPending(profileId) {
|
|
1502
|
+
let c = 0;
|
|
1503
|
+
for (const j of _hmemgovJsV2.values())
|
|
1504
|
+
if (
|
|
1505
|
+
j.profileId === profileId &&
|
|
1506
|
+
(j.status === HMEMGOV_RECALL_LIFECYCLE_V2.QUEUED ||
|
|
1507
|
+
j.status === HMEMGOV_RECALL_LIFECYCLE_V2.RECALLING)
|
|
1508
|
+
)
|
|
1509
|
+
c++;
|
|
1510
|
+
return c;
|
|
1511
|
+
}
|
|
1512
|
+
export function setMaxActiveHmemProfilesPerOwnerV2(n) {
|
|
1513
|
+
_hmemgovMaxActive = _hmemgovPos(n, "maxActiveHmemProfilesPerOwner");
|
|
1514
|
+
}
|
|
1515
|
+
export function getMaxActiveHmemProfilesPerOwnerV2() {
|
|
1516
|
+
return _hmemgovMaxActive;
|
|
1517
|
+
}
|
|
1518
|
+
export function setMaxPendingHmemRecallsPerProfileV2(n) {
|
|
1519
|
+
_hmemgovMaxPending = _hmemgovPos(n, "maxPendingHmemRecallsPerProfile");
|
|
1520
|
+
}
|
|
1521
|
+
export function getMaxPendingHmemRecallsPerProfileV2() {
|
|
1522
|
+
return _hmemgovMaxPending;
|
|
1523
|
+
}
|
|
1524
|
+
export function setHmemProfileIdleMsV2(n) {
|
|
1525
|
+
_hmemgovIdleMs = _hmemgovPos(n, "hmemgovProfileIdleMs");
|
|
1526
|
+
}
|
|
1527
|
+
export function getHmemProfileIdleMsV2() {
|
|
1528
|
+
return _hmemgovIdleMs;
|
|
1529
|
+
}
|
|
1530
|
+
export function setHmemRecallStuckMsV2(n) {
|
|
1531
|
+
_hmemgovStuckMs = _hmemgovPos(n, "hmemgovRecallStuckMs");
|
|
1532
|
+
}
|
|
1533
|
+
export function getHmemRecallStuckMsV2() {
|
|
1534
|
+
return _hmemgovStuckMs;
|
|
1535
|
+
}
|
|
1536
|
+
export function _resetStateHmemgovV2() {
|
|
1537
|
+
_hmemgovPsV2.clear();
|
|
1538
|
+
_hmemgovJsV2.clear();
|
|
1539
|
+
_hmemgovMaxActive = 10;
|
|
1540
|
+
_hmemgovMaxPending = 30;
|
|
1541
|
+
_hmemgovIdleMs = 2592000000;
|
|
1542
|
+
_hmemgovStuckMs = 60 * 1000;
|
|
1543
|
+
}
|
|
1544
|
+
export function registerHmemProfileV2({ id, owner, tier, metadata } = {}) {
|
|
1545
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
1546
|
+
if (_hmemgovPsV2.has(id))
|
|
1547
|
+
throw new Error(`hmemgov profile ${id} already exists`);
|
|
1548
|
+
const now = Date.now();
|
|
1549
|
+
const p = {
|
|
1550
|
+
id,
|
|
1551
|
+
owner,
|
|
1552
|
+
tier: tier || "short-term",
|
|
1553
|
+
status: HMEMGOV_PROFILE_MATURITY_V2.PENDING,
|
|
1554
|
+
createdAt: now,
|
|
1555
|
+
updatedAt: now,
|
|
1556
|
+
lastTouchedAt: now,
|
|
1557
|
+
activatedAt: null,
|
|
1558
|
+
archivedAt: null,
|
|
1559
|
+
metadata: { ...(metadata || {}) },
|
|
1560
|
+
};
|
|
1561
|
+
_hmemgovPsV2.set(id, p);
|
|
1562
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1563
|
+
}
|
|
1564
|
+
export function activateHmemProfileV2(id) {
|
|
1565
|
+
const p = _hmemgovPsV2.get(id);
|
|
1566
|
+
if (!p) throw new Error(`hmemgov profile ${id} not found`);
|
|
1567
|
+
const isInitial = p.status === HMEMGOV_PROFILE_MATURITY_V2.PENDING;
|
|
1568
|
+
_hmemgovCheckP(p.status, HMEMGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
1569
|
+
if (isInitial && _hmemgovCountActive(p.owner) >= _hmemgovMaxActive)
|
|
1570
|
+
throw new Error(`max active hmemgov profiles for owner ${p.owner} reached`);
|
|
1571
|
+
const now = Date.now();
|
|
1572
|
+
p.status = HMEMGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
1573
|
+
p.updatedAt = now;
|
|
1574
|
+
p.lastTouchedAt = now;
|
|
1575
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
1576
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1577
|
+
}
|
|
1578
|
+
export function staleHmemProfileV2(id) {
|
|
1579
|
+
const p = _hmemgovPsV2.get(id);
|
|
1580
|
+
if (!p) throw new Error(`hmemgov profile ${id} not found`);
|
|
1581
|
+
_hmemgovCheckP(p.status, HMEMGOV_PROFILE_MATURITY_V2.STALE);
|
|
1582
|
+
p.status = HMEMGOV_PROFILE_MATURITY_V2.STALE;
|
|
1583
|
+
p.updatedAt = Date.now();
|
|
1584
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1585
|
+
}
|
|
1586
|
+
export function archiveHmemProfileV2(id) {
|
|
1587
|
+
const p = _hmemgovPsV2.get(id);
|
|
1588
|
+
if (!p) throw new Error(`hmemgov profile ${id} not found`);
|
|
1589
|
+
_hmemgovCheckP(p.status, HMEMGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
1590
|
+
const now = Date.now();
|
|
1591
|
+
p.status = HMEMGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
1592
|
+
p.updatedAt = now;
|
|
1593
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
1594
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1595
|
+
}
|
|
1596
|
+
export function touchHmemProfileV2(id) {
|
|
1597
|
+
const p = _hmemgovPsV2.get(id);
|
|
1598
|
+
if (!p) throw new Error(`hmemgov profile ${id} not found`);
|
|
1599
|
+
if (_hmemgovPTerminal.has(p.status))
|
|
1600
|
+
throw new Error(`cannot touch terminal hmemgov profile ${id}`);
|
|
1601
|
+
const now = Date.now();
|
|
1602
|
+
p.lastTouchedAt = now;
|
|
1603
|
+
p.updatedAt = now;
|
|
1604
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1605
|
+
}
|
|
1606
|
+
export function getHmemProfileV2(id) {
|
|
1607
|
+
const p = _hmemgovPsV2.get(id);
|
|
1608
|
+
if (!p) return null;
|
|
1609
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
1610
|
+
}
|
|
1611
|
+
export function listHmemProfilesV2() {
|
|
1612
|
+
return [..._hmemgovPsV2.values()].map((p) => ({
|
|
1613
|
+
...p,
|
|
1614
|
+
metadata: { ...p.metadata },
|
|
1615
|
+
}));
|
|
1616
|
+
}
|
|
1617
|
+
export function createHmemRecallV2({ id, profileId, queryId, metadata } = {}) {
|
|
1618
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
1619
|
+
if (_hmemgovJsV2.has(id))
|
|
1620
|
+
throw new Error(`hmemgov recall ${id} already exists`);
|
|
1621
|
+
if (!_hmemgovPsV2.has(profileId))
|
|
1622
|
+
throw new Error(`hmemgov profile ${profileId} not found`);
|
|
1623
|
+
if (_hmemgovCountPending(profileId) >= _hmemgovMaxPending)
|
|
1624
|
+
throw new Error(
|
|
1625
|
+
`max pending hmemgov recalls for profile ${profileId} reached`,
|
|
1626
|
+
);
|
|
1627
|
+
const now = Date.now();
|
|
1628
|
+
const j = {
|
|
1629
|
+
id,
|
|
1630
|
+
profileId,
|
|
1631
|
+
queryId: queryId || "",
|
|
1632
|
+
status: HMEMGOV_RECALL_LIFECYCLE_V2.QUEUED,
|
|
1633
|
+
createdAt: now,
|
|
1634
|
+
updatedAt: now,
|
|
1635
|
+
startedAt: null,
|
|
1636
|
+
settledAt: null,
|
|
1637
|
+
metadata: { ...(metadata || {}) },
|
|
1638
|
+
};
|
|
1639
|
+
_hmemgovJsV2.set(id, j);
|
|
1640
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1641
|
+
}
|
|
1642
|
+
export function recallingHmemRecallV2(id) {
|
|
1643
|
+
const j = _hmemgovJsV2.get(id);
|
|
1644
|
+
if (!j) throw new Error(`hmemgov recall ${id} not found`);
|
|
1645
|
+
_hmemgovCheckJ(j.status, HMEMGOV_RECALL_LIFECYCLE_V2.RECALLING);
|
|
1646
|
+
const now = Date.now();
|
|
1647
|
+
j.status = HMEMGOV_RECALL_LIFECYCLE_V2.RECALLING;
|
|
1648
|
+
j.updatedAt = now;
|
|
1649
|
+
if (!j.startedAt) j.startedAt = now;
|
|
1650
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1651
|
+
}
|
|
1652
|
+
export function completeRecallHmemV2(id) {
|
|
1653
|
+
const j = _hmemgovJsV2.get(id);
|
|
1654
|
+
if (!j) throw new Error(`hmemgov recall ${id} not found`);
|
|
1655
|
+
_hmemgovCheckJ(j.status, HMEMGOV_RECALL_LIFECYCLE_V2.RECALLED);
|
|
1656
|
+
const now = Date.now();
|
|
1657
|
+
j.status = HMEMGOV_RECALL_LIFECYCLE_V2.RECALLED;
|
|
1658
|
+
j.updatedAt = now;
|
|
1659
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1660
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1661
|
+
}
|
|
1662
|
+
export function failHmemRecallV2(id, reason) {
|
|
1663
|
+
const j = _hmemgovJsV2.get(id);
|
|
1664
|
+
if (!j) throw new Error(`hmemgov recall ${id} not found`);
|
|
1665
|
+
_hmemgovCheckJ(j.status, HMEMGOV_RECALL_LIFECYCLE_V2.FAILED);
|
|
1666
|
+
const now = Date.now();
|
|
1667
|
+
j.status = HMEMGOV_RECALL_LIFECYCLE_V2.FAILED;
|
|
1668
|
+
j.updatedAt = now;
|
|
1669
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1670
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
1671
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1672
|
+
}
|
|
1673
|
+
export function cancelHmemRecallV2(id, reason) {
|
|
1674
|
+
const j = _hmemgovJsV2.get(id);
|
|
1675
|
+
if (!j) throw new Error(`hmemgov recall ${id} not found`);
|
|
1676
|
+
_hmemgovCheckJ(j.status, HMEMGOV_RECALL_LIFECYCLE_V2.CANCELLED);
|
|
1677
|
+
const now = Date.now();
|
|
1678
|
+
j.status = HMEMGOV_RECALL_LIFECYCLE_V2.CANCELLED;
|
|
1679
|
+
j.updatedAt = now;
|
|
1680
|
+
if (!j.settledAt) j.settledAt = now;
|
|
1681
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
1682
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1683
|
+
}
|
|
1684
|
+
export function getHmemRecallV2(id) {
|
|
1685
|
+
const j = _hmemgovJsV2.get(id);
|
|
1686
|
+
if (!j) return null;
|
|
1687
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
1688
|
+
}
|
|
1689
|
+
export function listHmemRecallsV2() {
|
|
1690
|
+
return [..._hmemgovJsV2.values()].map((j) => ({
|
|
1691
|
+
...j,
|
|
1692
|
+
metadata: { ...j.metadata },
|
|
1693
|
+
}));
|
|
1694
|
+
}
|
|
1695
|
+
export function autoStaleIdleHmemProfilesV2({ now } = {}) {
|
|
1696
|
+
const t = now ?? Date.now();
|
|
1697
|
+
const flipped = [];
|
|
1698
|
+
for (const p of _hmemgovPsV2.values())
|
|
1699
|
+
if (
|
|
1700
|
+
p.status === HMEMGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
1701
|
+
t - p.lastTouchedAt >= _hmemgovIdleMs
|
|
1702
|
+
) {
|
|
1703
|
+
p.status = HMEMGOV_PROFILE_MATURITY_V2.STALE;
|
|
1704
|
+
p.updatedAt = t;
|
|
1705
|
+
flipped.push(p.id);
|
|
1706
|
+
}
|
|
1707
|
+
return { flipped, count: flipped.length };
|
|
1708
|
+
}
|
|
1709
|
+
export function autoFailStuckHmemRecallsV2({ now } = {}) {
|
|
1710
|
+
const t = now ?? Date.now();
|
|
1711
|
+
const flipped = [];
|
|
1712
|
+
for (const j of _hmemgovJsV2.values())
|
|
1713
|
+
if (
|
|
1714
|
+
j.status === HMEMGOV_RECALL_LIFECYCLE_V2.RECALLING &&
|
|
1715
|
+
j.startedAt != null &&
|
|
1716
|
+
t - j.startedAt >= _hmemgovStuckMs
|
|
1717
|
+
) {
|
|
1718
|
+
j.status = HMEMGOV_RECALL_LIFECYCLE_V2.FAILED;
|
|
1719
|
+
j.updatedAt = t;
|
|
1720
|
+
if (!j.settledAt) j.settledAt = t;
|
|
1721
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
1722
|
+
flipped.push(j.id);
|
|
1723
|
+
}
|
|
1724
|
+
return { flipped, count: flipped.length };
|
|
1725
|
+
}
|
|
1726
|
+
export function getHmemgovStatsV2() {
|
|
1727
|
+
const profilesByStatus = {};
|
|
1728
|
+
for (const v of Object.values(HMEMGOV_PROFILE_MATURITY_V2))
|
|
1729
|
+
profilesByStatus[v] = 0;
|
|
1730
|
+
for (const p of _hmemgovPsV2.values()) profilesByStatus[p.status]++;
|
|
1731
|
+
const recallsByStatus = {};
|
|
1732
|
+
for (const v of Object.values(HMEMGOV_RECALL_LIFECYCLE_V2))
|
|
1733
|
+
recallsByStatus[v] = 0;
|
|
1734
|
+
for (const j of _hmemgovJsV2.values()) recallsByStatus[j.status]++;
|
|
1735
|
+
return {
|
|
1736
|
+
totalHmemProfilesV2: _hmemgovPsV2.size,
|
|
1737
|
+
totalHmemRecallsV2: _hmemgovJsV2.size,
|
|
1738
|
+
maxActiveHmemProfilesPerOwner: _hmemgovMaxActive,
|
|
1739
|
+
maxPendingHmemRecallsPerProfile: _hmemgovMaxPending,
|
|
1740
|
+
hmemgovProfileIdleMs: _hmemgovIdleMs,
|
|
1741
|
+
hmemgovRecallStuckMs: _hmemgovStuckMs,
|
|
1742
|
+
profilesByStatus,
|
|
1743
|
+
recallsByStatus,
|
|
1744
|
+
};
|
|
1745
|
+
}
|