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
|
@@ -180,3 +180,344 @@ export async function mountTemplateMcpTools(template, opts = {}) {
|
|
|
180
180
|
cleanup,
|
|
181
181
|
};
|
|
182
182
|
}
|
|
183
|
+
|
|
184
|
+
// =====================================================================
|
|
185
|
+
// cowork-mcp-tools V2 governance overlay (iter27)
|
|
186
|
+
// =====================================================================
|
|
187
|
+
export const CMCPGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
188
|
+
PENDING: "pending",
|
|
189
|
+
ACTIVE: "active",
|
|
190
|
+
STALE: "stale",
|
|
191
|
+
ARCHIVED: "archived",
|
|
192
|
+
});
|
|
193
|
+
export const CMCPGOV_EXEC_LIFECYCLE_V2 = Object.freeze({
|
|
194
|
+
QUEUED: "queued",
|
|
195
|
+
RUNNING: "running",
|
|
196
|
+
COMPLETED: "completed",
|
|
197
|
+
FAILED: "failed",
|
|
198
|
+
CANCELLED: "cancelled",
|
|
199
|
+
});
|
|
200
|
+
const _cmcpgovPTrans = new Map([
|
|
201
|
+
[
|
|
202
|
+
CMCPGOV_PROFILE_MATURITY_V2.PENDING,
|
|
203
|
+
new Set([
|
|
204
|
+
CMCPGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
205
|
+
CMCPGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
206
|
+
]),
|
|
207
|
+
],
|
|
208
|
+
[
|
|
209
|
+
CMCPGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
210
|
+
new Set([
|
|
211
|
+
CMCPGOV_PROFILE_MATURITY_V2.STALE,
|
|
212
|
+
CMCPGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
213
|
+
]),
|
|
214
|
+
],
|
|
215
|
+
[
|
|
216
|
+
CMCPGOV_PROFILE_MATURITY_V2.STALE,
|
|
217
|
+
new Set([
|
|
218
|
+
CMCPGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
219
|
+
CMCPGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
220
|
+
]),
|
|
221
|
+
],
|
|
222
|
+
[CMCPGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
223
|
+
]);
|
|
224
|
+
const _cmcpgovPTerminal = new Set([CMCPGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
225
|
+
const _cmcpgovJTrans = new Map([
|
|
226
|
+
[
|
|
227
|
+
CMCPGOV_EXEC_LIFECYCLE_V2.QUEUED,
|
|
228
|
+
new Set([
|
|
229
|
+
CMCPGOV_EXEC_LIFECYCLE_V2.RUNNING,
|
|
230
|
+
CMCPGOV_EXEC_LIFECYCLE_V2.CANCELLED,
|
|
231
|
+
]),
|
|
232
|
+
],
|
|
233
|
+
[
|
|
234
|
+
CMCPGOV_EXEC_LIFECYCLE_V2.RUNNING,
|
|
235
|
+
new Set([
|
|
236
|
+
CMCPGOV_EXEC_LIFECYCLE_V2.COMPLETED,
|
|
237
|
+
CMCPGOV_EXEC_LIFECYCLE_V2.FAILED,
|
|
238
|
+
CMCPGOV_EXEC_LIFECYCLE_V2.CANCELLED,
|
|
239
|
+
]),
|
|
240
|
+
],
|
|
241
|
+
[CMCPGOV_EXEC_LIFECYCLE_V2.COMPLETED, new Set()],
|
|
242
|
+
[CMCPGOV_EXEC_LIFECYCLE_V2.FAILED, new Set()],
|
|
243
|
+
[CMCPGOV_EXEC_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
244
|
+
]);
|
|
245
|
+
const _cmcpgovPsV2 = new Map();
|
|
246
|
+
const _cmcpgovJsV2 = new Map();
|
|
247
|
+
let _cmcpgovMaxActive = 8,
|
|
248
|
+
_cmcpgovMaxPending = 20,
|
|
249
|
+
_cmcpgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
250
|
+
_cmcpgovStuckMs = 60 * 1000;
|
|
251
|
+
function _cmcpgovPos(n, label) {
|
|
252
|
+
const v = Math.floor(Number(n));
|
|
253
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
254
|
+
throw new Error(`${label} must be positive integer`);
|
|
255
|
+
return v;
|
|
256
|
+
}
|
|
257
|
+
function _cmcpgovCheckP(from, to) {
|
|
258
|
+
const a = _cmcpgovPTrans.get(from);
|
|
259
|
+
if (!a || !a.has(to))
|
|
260
|
+
throw new Error(`invalid cmcpgov profile transition ${from} → ${to}`);
|
|
261
|
+
}
|
|
262
|
+
function _cmcpgovCheckJ(from, to) {
|
|
263
|
+
const a = _cmcpgovJTrans.get(from);
|
|
264
|
+
if (!a || !a.has(to))
|
|
265
|
+
throw new Error(`invalid cmcpgov exec transition ${from} → ${to}`);
|
|
266
|
+
}
|
|
267
|
+
function _cmcpgovCountActive(owner) {
|
|
268
|
+
let c = 0;
|
|
269
|
+
for (const p of _cmcpgovPsV2.values())
|
|
270
|
+
if (p.owner === owner && p.status === CMCPGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
271
|
+
c++;
|
|
272
|
+
return c;
|
|
273
|
+
}
|
|
274
|
+
function _cmcpgovCountPending(profileId) {
|
|
275
|
+
let c = 0;
|
|
276
|
+
for (const j of _cmcpgovJsV2.values())
|
|
277
|
+
if (
|
|
278
|
+
j.profileId === profileId &&
|
|
279
|
+
(j.status === CMCPGOV_EXEC_LIFECYCLE_V2.QUEUED ||
|
|
280
|
+
j.status === CMCPGOV_EXEC_LIFECYCLE_V2.RUNNING)
|
|
281
|
+
)
|
|
282
|
+
c++;
|
|
283
|
+
return c;
|
|
284
|
+
}
|
|
285
|
+
export function setMaxActiveCmcpgovProfilesPerOwnerV2(n) {
|
|
286
|
+
_cmcpgovMaxActive = _cmcpgovPos(n, "maxActiveCmcpgovProfilesPerOwner");
|
|
287
|
+
}
|
|
288
|
+
export function getMaxActiveCmcpgovProfilesPerOwnerV2() {
|
|
289
|
+
return _cmcpgovMaxActive;
|
|
290
|
+
}
|
|
291
|
+
export function setMaxPendingCmcpgovExecsPerProfileV2(n) {
|
|
292
|
+
_cmcpgovMaxPending = _cmcpgovPos(n, "maxPendingCmcpgovExecsPerProfile");
|
|
293
|
+
}
|
|
294
|
+
export function getMaxPendingCmcpgovExecsPerProfileV2() {
|
|
295
|
+
return _cmcpgovMaxPending;
|
|
296
|
+
}
|
|
297
|
+
export function setCmcpgovProfileIdleMsV2(n) {
|
|
298
|
+
_cmcpgovIdleMs = _cmcpgovPos(n, "cmcpgovProfileIdleMs");
|
|
299
|
+
}
|
|
300
|
+
export function getCmcpgovProfileIdleMsV2() {
|
|
301
|
+
return _cmcpgovIdleMs;
|
|
302
|
+
}
|
|
303
|
+
export function setCmcpgovExecStuckMsV2(n) {
|
|
304
|
+
_cmcpgovStuckMs = _cmcpgovPos(n, "cmcpgovExecStuckMs");
|
|
305
|
+
}
|
|
306
|
+
export function getCmcpgovExecStuckMsV2() {
|
|
307
|
+
return _cmcpgovStuckMs;
|
|
308
|
+
}
|
|
309
|
+
export function _resetStateCoworkMcpToolsGovV2() {
|
|
310
|
+
_cmcpgovPsV2.clear();
|
|
311
|
+
_cmcpgovJsV2.clear();
|
|
312
|
+
_cmcpgovMaxActive = 8;
|
|
313
|
+
_cmcpgovMaxPending = 20;
|
|
314
|
+
_cmcpgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
315
|
+
_cmcpgovStuckMs = 60 * 1000;
|
|
316
|
+
}
|
|
317
|
+
export function registerCmcpgovProfileV2({
|
|
318
|
+
id,
|
|
319
|
+
owner,
|
|
320
|
+
toolset,
|
|
321
|
+
metadata,
|
|
322
|
+
} = {}) {
|
|
323
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
324
|
+
if (_cmcpgovPsV2.has(id))
|
|
325
|
+
throw new Error(`cmcpgov profile ${id} already exists`);
|
|
326
|
+
const now = Date.now();
|
|
327
|
+
const p = {
|
|
328
|
+
id,
|
|
329
|
+
owner,
|
|
330
|
+
toolset: toolset || "default",
|
|
331
|
+
status: CMCPGOV_PROFILE_MATURITY_V2.PENDING,
|
|
332
|
+
createdAt: now,
|
|
333
|
+
updatedAt: now,
|
|
334
|
+
lastTouchedAt: now,
|
|
335
|
+
activatedAt: null,
|
|
336
|
+
archivedAt: null,
|
|
337
|
+
metadata: { ...(metadata || {}) },
|
|
338
|
+
};
|
|
339
|
+
_cmcpgovPsV2.set(id, p);
|
|
340
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
341
|
+
}
|
|
342
|
+
export function activateCmcpgovProfileV2(id) {
|
|
343
|
+
const p = _cmcpgovPsV2.get(id);
|
|
344
|
+
if (!p) throw new Error(`cmcpgov profile ${id} not found`);
|
|
345
|
+
const isInitial = p.status === CMCPGOV_PROFILE_MATURITY_V2.PENDING;
|
|
346
|
+
_cmcpgovCheckP(p.status, CMCPGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
347
|
+
if (isInitial && _cmcpgovCountActive(p.owner) >= _cmcpgovMaxActive)
|
|
348
|
+
throw new Error(`max active cmcpgov profiles for owner ${p.owner} reached`);
|
|
349
|
+
const now = Date.now();
|
|
350
|
+
p.status = CMCPGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
351
|
+
p.updatedAt = now;
|
|
352
|
+
p.lastTouchedAt = now;
|
|
353
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
354
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
355
|
+
}
|
|
356
|
+
export function staleCmcpgovProfileV2(id) {
|
|
357
|
+
const p = _cmcpgovPsV2.get(id);
|
|
358
|
+
if (!p) throw new Error(`cmcpgov profile ${id} not found`);
|
|
359
|
+
_cmcpgovCheckP(p.status, CMCPGOV_PROFILE_MATURITY_V2.STALE);
|
|
360
|
+
p.status = CMCPGOV_PROFILE_MATURITY_V2.STALE;
|
|
361
|
+
p.updatedAt = Date.now();
|
|
362
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
363
|
+
}
|
|
364
|
+
export function archiveCmcpgovProfileV2(id) {
|
|
365
|
+
const p = _cmcpgovPsV2.get(id);
|
|
366
|
+
if (!p) throw new Error(`cmcpgov profile ${id} not found`);
|
|
367
|
+
_cmcpgovCheckP(p.status, CMCPGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
368
|
+
const now = Date.now();
|
|
369
|
+
p.status = CMCPGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
370
|
+
p.updatedAt = now;
|
|
371
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
372
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
373
|
+
}
|
|
374
|
+
export function touchCmcpgovProfileV2(id) {
|
|
375
|
+
const p = _cmcpgovPsV2.get(id);
|
|
376
|
+
if (!p) throw new Error(`cmcpgov profile ${id} not found`);
|
|
377
|
+
if (_cmcpgovPTerminal.has(p.status))
|
|
378
|
+
throw new Error(`cannot touch terminal cmcpgov profile ${id}`);
|
|
379
|
+
const now = Date.now();
|
|
380
|
+
p.lastTouchedAt = now;
|
|
381
|
+
p.updatedAt = now;
|
|
382
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
383
|
+
}
|
|
384
|
+
export function getCmcpgovProfileV2(id) {
|
|
385
|
+
const p = _cmcpgovPsV2.get(id);
|
|
386
|
+
if (!p) return null;
|
|
387
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
388
|
+
}
|
|
389
|
+
export function listCmcpgovProfilesV2() {
|
|
390
|
+
return [..._cmcpgovPsV2.values()].map((p) => ({
|
|
391
|
+
...p,
|
|
392
|
+
metadata: { ...p.metadata },
|
|
393
|
+
}));
|
|
394
|
+
}
|
|
395
|
+
export function createCmcpgovExecV2({ id, profileId, tool, metadata } = {}) {
|
|
396
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
397
|
+
if (_cmcpgovJsV2.has(id))
|
|
398
|
+
throw new Error(`cmcpgov exec ${id} already exists`);
|
|
399
|
+
if (!_cmcpgovPsV2.has(profileId))
|
|
400
|
+
throw new Error(`cmcpgov profile ${profileId} not found`);
|
|
401
|
+
if (_cmcpgovCountPending(profileId) >= _cmcpgovMaxPending)
|
|
402
|
+
throw new Error(
|
|
403
|
+
`max pending cmcpgov execs for profile ${profileId} reached`,
|
|
404
|
+
);
|
|
405
|
+
const now = Date.now();
|
|
406
|
+
const j = {
|
|
407
|
+
id,
|
|
408
|
+
profileId,
|
|
409
|
+
tool: tool || "",
|
|
410
|
+
status: CMCPGOV_EXEC_LIFECYCLE_V2.QUEUED,
|
|
411
|
+
createdAt: now,
|
|
412
|
+
updatedAt: now,
|
|
413
|
+
startedAt: null,
|
|
414
|
+
settledAt: null,
|
|
415
|
+
metadata: { ...(metadata || {}) },
|
|
416
|
+
};
|
|
417
|
+
_cmcpgovJsV2.set(id, j);
|
|
418
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
419
|
+
}
|
|
420
|
+
export function runningCmcpgovExecV2(id) {
|
|
421
|
+
const j = _cmcpgovJsV2.get(id);
|
|
422
|
+
if (!j) throw new Error(`cmcpgov exec ${id} not found`);
|
|
423
|
+
_cmcpgovCheckJ(j.status, CMCPGOV_EXEC_LIFECYCLE_V2.RUNNING);
|
|
424
|
+
const now = Date.now();
|
|
425
|
+
j.status = CMCPGOV_EXEC_LIFECYCLE_V2.RUNNING;
|
|
426
|
+
j.updatedAt = now;
|
|
427
|
+
if (!j.startedAt) j.startedAt = now;
|
|
428
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
429
|
+
}
|
|
430
|
+
export function completeExecCmcpgovV2(id) {
|
|
431
|
+
const j = _cmcpgovJsV2.get(id);
|
|
432
|
+
if (!j) throw new Error(`cmcpgov exec ${id} not found`);
|
|
433
|
+
_cmcpgovCheckJ(j.status, CMCPGOV_EXEC_LIFECYCLE_V2.COMPLETED);
|
|
434
|
+
const now = Date.now();
|
|
435
|
+
j.status = CMCPGOV_EXEC_LIFECYCLE_V2.COMPLETED;
|
|
436
|
+
j.updatedAt = now;
|
|
437
|
+
if (!j.settledAt) j.settledAt = now;
|
|
438
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
439
|
+
}
|
|
440
|
+
export function failCmcpgovExecV2(id, reason) {
|
|
441
|
+
const j = _cmcpgovJsV2.get(id);
|
|
442
|
+
if (!j) throw new Error(`cmcpgov exec ${id} not found`);
|
|
443
|
+
_cmcpgovCheckJ(j.status, CMCPGOV_EXEC_LIFECYCLE_V2.FAILED);
|
|
444
|
+
const now = Date.now();
|
|
445
|
+
j.status = CMCPGOV_EXEC_LIFECYCLE_V2.FAILED;
|
|
446
|
+
j.updatedAt = now;
|
|
447
|
+
if (!j.settledAt) j.settledAt = now;
|
|
448
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
449
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
450
|
+
}
|
|
451
|
+
export function cancelCmcpgovExecV2(id, reason) {
|
|
452
|
+
const j = _cmcpgovJsV2.get(id);
|
|
453
|
+
if (!j) throw new Error(`cmcpgov exec ${id} not found`);
|
|
454
|
+
_cmcpgovCheckJ(j.status, CMCPGOV_EXEC_LIFECYCLE_V2.CANCELLED);
|
|
455
|
+
const now = Date.now();
|
|
456
|
+
j.status = CMCPGOV_EXEC_LIFECYCLE_V2.CANCELLED;
|
|
457
|
+
j.updatedAt = now;
|
|
458
|
+
if (!j.settledAt) j.settledAt = now;
|
|
459
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
460
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
461
|
+
}
|
|
462
|
+
export function getCmcpgovExecV2(id) {
|
|
463
|
+
const j = _cmcpgovJsV2.get(id);
|
|
464
|
+
if (!j) return null;
|
|
465
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
466
|
+
}
|
|
467
|
+
export function listCmcpgovExecsV2() {
|
|
468
|
+
return [..._cmcpgovJsV2.values()].map((j) => ({
|
|
469
|
+
...j,
|
|
470
|
+
metadata: { ...j.metadata },
|
|
471
|
+
}));
|
|
472
|
+
}
|
|
473
|
+
export function autoStaleIdleCmcpgovProfilesV2({ now } = {}) {
|
|
474
|
+
const t = now ?? Date.now();
|
|
475
|
+
const flipped = [];
|
|
476
|
+
for (const p of _cmcpgovPsV2.values())
|
|
477
|
+
if (
|
|
478
|
+
p.status === CMCPGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
479
|
+
t - p.lastTouchedAt >= _cmcpgovIdleMs
|
|
480
|
+
) {
|
|
481
|
+
p.status = CMCPGOV_PROFILE_MATURITY_V2.STALE;
|
|
482
|
+
p.updatedAt = t;
|
|
483
|
+
flipped.push(p.id);
|
|
484
|
+
}
|
|
485
|
+
return { flipped, count: flipped.length };
|
|
486
|
+
}
|
|
487
|
+
export function autoFailStuckCmcpgovExecsV2({ now } = {}) {
|
|
488
|
+
const t = now ?? Date.now();
|
|
489
|
+
const flipped = [];
|
|
490
|
+
for (const j of _cmcpgovJsV2.values())
|
|
491
|
+
if (
|
|
492
|
+
j.status === CMCPGOV_EXEC_LIFECYCLE_V2.RUNNING &&
|
|
493
|
+
j.startedAt != null &&
|
|
494
|
+
t - j.startedAt >= _cmcpgovStuckMs
|
|
495
|
+
) {
|
|
496
|
+
j.status = CMCPGOV_EXEC_LIFECYCLE_V2.FAILED;
|
|
497
|
+
j.updatedAt = t;
|
|
498
|
+
if (!j.settledAt) j.settledAt = t;
|
|
499
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
500
|
+
flipped.push(j.id);
|
|
501
|
+
}
|
|
502
|
+
return { flipped, count: flipped.length };
|
|
503
|
+
}
|
|
504
|
+
export function getCoworkMcpToolsGovStatsV2() {
|
|
505
|
+
const profilesByStatus = {};
|
|
506
|
+
for (const v of Object.values(CMCPGOV_PROFILE_MATURITY_V2))
|
|
507
|
+
profilesByStatus[v] = 0;
|
|
508
|
+
for (const p of _cmcpgovPsV2.values()) profilesByStatus[p.status]++;
|
|
509
|
+
const execsByStatus = {};
|
|
510
|
+
for (const v of Object.values(CMCPGOV_EXEC_LIFECYCLE_V2))
|
|
511
|
+
execsByStatus[v] = 0;
|
|
512
|
+
for (const j of _cmcpgovJsV2.values()) execsByStatus[j.status]++;
|
|
513
|
+
return {
|
|
514
|
+
totalCmcpgovProfilesV2: _cmcpgovPsV2.size,
|
|
515
|
+
totalCmcpgovExecsV2: _cmcpgovJsV2.size,
|
|
516
|
+
maxActiveCmcpgovProfilesPerOwner: _cmcpgovMaxActive,
|
|
517
|
+
maxPendingCmcpgovExecsPerProfile: _cmcpgovMaxPending,
|
|
518
|
+
cmcpgovProfileIdleMs: _cmcpgovIdleMs,
|
|
519
|
+
cmcpgovExecStuckMs: _cmcpgovStuckMs,
|
|
520
|
+
profilesByStatus,
|
|
521
|
+
execsByStatus,
|
|
522
|
+
};
|
|
523
|
+
}
|
|
@@ -106,3 +106,344 @@ export function buildHtml(data) {
|
|
|
106
106
|
</body>
|
|
107
107
|
</html>`;
|
|
108
108
|
}
|
|
109
|
+
|
|
110
|
+
// =====================================================================
|
|
111
|
+
// cowork-observe-html V2 governance overlay (iter27)
|
|
112
|
+
// =====================================================================
|
|
113
|
+
export const COHTGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
114
|
+
PENDING: "pending",
|
|
115
|
+
ACTIVE: "active",
|
|
116
|
+
STALE: "stale",
|
|
117
|
+
ARCHIVED: "archived",
|
|
118
|
+
});
|
|
119
|
+
export const COHTGOV_RENDER_LIFECYCLE_V2 = Object.freeze({
|
|
120
|
+
QUEUED: "queued",
|
|
121
|
+
RENDERING: "rendering",
|
|
122
|
+
RENDERED: "rendered",
|
|
123
|
+
FAILED: "failed",
|
|
124
|
+
CANCELLED: "cancelled",
|
|
125
|
+
});
|
|
126
|
+
const _cohtgovPTrans = new Map([
|
|
127
|
+
[
|
|
128
|
+
COHTGOV_PROFILE_MATURITY_V2.PENDING,
|
|
129
|
+
new Set([
|
|
130
|
+
COHTGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
131
|
+
COHTGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
132
|
+
]),
|
|
133
|
+
],
|
|
134
|
+
[
|
|
135
|
+
COHTGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
136
|
+
new Set([
|
|
137
|
+
COHTGOV_PROFILE_MATURITY_V2.STALE,
|
|
138
|
+
COHTGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
139
|
+
]),
|
|
140
|
+
],
|
|
141
|
+
[
|
|
142
|
+
COHTGOV_PROFILE_MATURITY_V2.STALE,
|
|
143
|
+
new Set([
|
|
144
|
+
COHTGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
145
|
+
COHTGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
146
|
+
]),
|
|
147
|
+
],
|
|
148
|
+
[COHTGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
149
|
+
]);
|
|
150
|
+
const _cohtgovPTerminal = new Set([COHTGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
151
|
+
const _cohtgovJTrans = new Map([
|
|
152
|
+
[
|
|
153
|
+
COHTGOV_RENDER_LIFECYCLE_V2.QUEUED,
|
|
154
|
+
new Set([
|
|
155
|
+
COHTGOV_RENDER_LIFECYCLE_V2.RENDERING,
|
|
156
|
+
COHTGOV_RENDER_LIFECYCLE_V2.CANCELLED,
|
|
157
|
+
]),
|
|
158
|
+
],
|
|
159
|
+
[
|
|
160
|
+
COHTGOV_RENDER_LIFECYCLE_V2.RENDERING,
|
|
161
|
+
new Set([
|
|
162
|
+
COHTGOV_RENDER_LIFECYCLE_V2.RENDERED,
|
|
163
|
+
COHTGOV_RENDER_LIFECYCLE_V2.FAILED,
|
|
164
|
+
COHTGOV_RENDER_LIFECYCLE_V2.CANCELLED,
|
|
165
|
+
]),
|
|
166
|
+
],
|
|
167
|
+
[COHTGOV_RENDER_LIFECYCLE_V2.RENDERED, new Set()],
|
|
168
|
+
[COHTGOV_RENDER_LIFECYCLE_V2.FAILED, new Set()],
|
|
169
|
+
[COHTGOV_RENDER_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
170
|
+
]);
|
|
171
|
+
const _cohtgovPsV2 = new Map();
|
|
172
|
+
const _cohtgovJsV2 = new Map();
|
|
173
|
+
let _cohtgovMaxActive = 6,
|
|
174
|
+
_cohtgovMaxPending = 15,
|
|
175
|
+
_cohtgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
176
|
+
_cohtgovStuckMs = 60 * 1000;
|
|
177
|
+
function _cohtgovPos(n, label) {
|
|
178
|
+
const v = Math.floor(Number(n));
|
|
179
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
180
|
+
throw new Error(`${label} must be positive integer`);
|
|
181
|
+
return v;
|
|
182
|
+
}
|
|
183
|
+
function _cohtgovCheckP(from, to) {
|
|
184
|
+
const a = _cohtgovPTrans.get(from);
|
|
185
|
+
if (!a || !a.has(to))
|
|
186
|
+
throw new Error(`invalid cohtgov profile transition ${from} → ${to}`);
|
|
187
|
+
}
|
|
188
|
+
function _cohtgovCheckJ(from, to) {
|
|
189
|
+
const a = _cohtgovJTrans.get(from);
|
|
190
|
+
if (!a || !a.has(to))
|
|
191
|
+
throw new Error(`invalid cohtgov render transition ${from} → ${to}`);
|
|
192
|
+
}
|
|
193
|
+
function _cohtgovCountActive(owner) {
|
|
194
|
+
let c = 0;
|
|
195
|
+
for (const p of _cohtgovPsV2.values())
|
|
196
|
+
if (p.owner === owner && p.status === COHTGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
197
|
+
c++;
|
|
198
|
+
return c;
|
|
199
|
+
}
|
|
200
|
+
function _cohtgovCountPending(profileId) {
|
|
201
|
+
let c = 0;
|
|
202
|
+
for (const j of _cohtgovJsV2.values())
|
|
203
|
+
if (
|
|
204
|
+
j.profileId === profileId &&
|
|
205
|
+
(j.status === COHTGOV_RENDER_LIFECYCLE_V2.QUEUED ||
|
|
206
|
+
j.status === COHTGOV_RENDER_LIFECYCLE_V2.RENDERING)
|
|
207
|
+
)
|
|
208
|
+
c++;
|
|
209
|
+
return c;
|
|
210
|
+
}
|
|
211
|
+
export function setMaxActiveCohtgovProfilesPerOwnerV2(n) {
|
|
212
|
+
_cohtgovMaxActive = _cohtgovPos(n, "maxActiveCohtgovProfilesPerOwner");
|
|
213
|
+
}
|
|
214
|
+
export function getMaxActiveCohtgovProfilesPerOwnerV2() {
|
|
215
|
+
return _cohtgovMaxActive;
|
|
216
|
+
}
|
|
217
|
+
export function setMaxPendingCohtgovRendersPerProfileV2(n) {
|
|
218
|
+
_cohtgovMaxPending = _cohtgovPos(n, "maxPendingCohtgovRendersPerProfile");
|
|
219
|
+
}
|
|
220
|
+
export function getMaxPendingCohtgovRendersPerProfileV2() {
|
|
221
|
+
return _cohtgovMaxPending;
|
|
222
|
+
}
|
|
223
|
+
export function setCohtgovProfileIdleMsV2(n) {
|
|
224
|
+
_cohtgovIdleMs = _cohtgovPos(n, "cohtgovProfileIdleMs");
|
|
225
|
+
}
|
|
226
|
+
export function getCohtgovProfileIdleMsV2() {
|
|
227
|
+
return _cohtgovIdleMs;
|
|
228
|
+
}
|
|
229
|
+
export function setCohtgovRenderStuckMsV2(n) {
|
|
230
|
+
_cohtgovStuckMs = _cohtgovPos(n, "cohtgovRenderStuckMs");
|
|
231
|
+
}
|
|
232
|
+
export function getCohtgovRenderStuckMsV2() {
|
|
233
|
+
return _cohtgovStuckMs;
|
|
234
|
+
}
|
|
235
|
+
export function _resetStateCoworkObserveHtmlGovV2() {
|
|
236
|
+
_cohtgovPsV2.clear();
|
|
237
|
+
_cohtgovJsV2.clear();
|
|
238
|
+
_cohtgovMaxActive = 6;
|
|
239
|
+
_cohtgovMaxPending = 15;
|
|
240
|
+
_cohtgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
241
|
+
_cohtgovStuckMs = 60 * 1000;
|
|
242
|
+
}
|
|
243
|
+
export function registerCohtgovProfileV2({
|
|
244
|
+
id,
|
|
245
|
+
owner,
|
|
246
|
+
template,
|
|
247
|
+
metadata,
|
|
248
|
+
} = {}) {
|
|
249
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
250
|
+
if (_cohtgovPsV2.has(id))
|
|
251
|
+
throw new Error(`cohtgov profile ${id} already exists`);
|
|
252
|
+
const now = Date.now();
|
|
253
|
+
const p = {
|
|
254
|
+
id,
|
|
255
|
+
owner,
|
|
256
|
+
template: template || "default",
|
|
257
|
+
status: COHTGOV_PROFILE_MATURITY_V2.PENDING,
|
|
258
|
+
createdAt: now,
|
|
259
|
+
updatedAt: now,
|
|
260
|
+
lastTouchedAt: now,
|
|
261
|
+
activatedAt: null,
|
|
262
|
+
archivedAt: null,
|
|
263
|
+
metadata: { ...(metadata || {}) },
|
|
264
|
+
};
|
|
265
|
+
_cohtgovPsV2.set(id, p);
|
|
266
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
267
|
+
}
|
|
268
|
+
export function activateCohtgovProfileV2(id) {
|
|
269
|
+
const p = _cohtgovPsV2.get(id);
|
|
270
|
+
if (!p) throw new Error(`cohtgov profile ${id} not found`);
|
|
271
|
+
const isInitial = p.status === COHTGOV_PROFILE_MATURITY_V2.PENDING;
|
|
272
|
+
_cohtgovCheckP(p.status, COHTGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
273
|
+
if (isInitial && _cohtgovCountActive(p.owner) >= _cohtgovMaxActive)
|
|
274
|
+
throw new Error(`max active cohtgov profiles for owner ${p.owner} reached`);
|
|
275
|
+
const now = Date.now();
|
|
276
|
+
p.status = COHTGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
277
|
+
p.updatedAt = now;
|
|
278
|
+
p.lastTouchedAt = now;
|
|
279
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
280
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
281
|
+
}
|
|
282
|
+
export function staleCohtgovProfileV2(id) {
|
|
283
|
+
const p = _cohtgovPsV2.get(id);
|
|
284
|
+
if (!p) throw new Error(`cohtgov profile ${id} not found`);
|
|
285
|
+
_cohtgovCheckP(p.status, COHTGOV_PROFILE_MATURITY_V2.STALE);
|
|
286
|
+
p.status = COHTGOV_PROFILE_MATURITY_V2.STALE;
|
|
287
|
+
p.updatedAt = Date.now();
|
|
288
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
289
|
+
}
|
|
290
|
+
export function archiveCohtgovProfileV2(id) {
|
|
291
|
+
const p = _cohtgovPsV2.get(id);
|
|
292
|
+
if (!p) throw new Error(`cohtgov profile ${id} not found`);
|
|
293
|
+
_cohtgovCheckP(p.status, COHTGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
294
|
+
const now = Date.now();
|
|
295
|
+
p.status = COHTGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
296
|
+
p.updatedAt = now;
|
|
297
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
298
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
299
|
+
}
|
|
300
|
+
export function touchCohtgovProfileV2(id) {
|
|
301
|
+
const p = _cohtgovPsV2.get(id);
|
|
302
|
+
if (!p) throw new Error(`cohtgov profile ${id} not found`);
|
|
303
|
+
if (_cohtgovPTerminal.has(p.status))
|
|
304
|
+
throw new Error(`cannot touch terminal cohtgov profile ${id}`);
|
|
305
|
+
const now = Date.now();
|
|
306
|
+
p.lastTouchedAt = now;
|
|
307
|
+
p.updatedAt = now;
|
|
308
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
309
|
+
}
|
|
310
|
+
export function getCohtgovProfileV2(id) {
|
|
311
|
+
const p = _cohtgovPsV2.get(id);
|
|
312
|
+
if (!p) return null;
|
|
313
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
314
|
+
}
|
|
315
|
+
export function listCohtgovProfilesV2() {
|
|
316
|
+
return [..._cohtgovPsV2.values()].map((p) => ({
|
|
317
|
+
...p,
|
|
318
|
+
metadata: { ...p.metadata },
|
|
319
|
+
}));
|
|
320
|
+
}
|
|
321
|
+
export function createCohtgovRenderV2({ id, profileId, page, metadata } = {}) {
|
|
322
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
323
|
+
if (_cohtgovJsV2.has(id))
|
|
324
|
+
throw new Error(`cohtgov render ${id} already exists`);
|
|
325
|
+
if (!_cohtgovPsV2.has(profileId))
|
|
326
|
+
throw new Error(`cohtgov profile ${profileId} not found`);
|
|
327
|
+
if (_cohtgovCountPending(profileId) >= _cohtgovMaxPending)
|
|
328
|
+
throw new Error(
|
|
329
|
+
`max pending cohtgov renders for profile ${profileId} reached`,
|
|
330
|
+
);
|
|
331
|
+
const now = Date.now();
|
|
332
|
+
const j = {
|
|
333
|
+
id,
|
|
334
|
+
profileId,
|
|
335
|
+
page: page || "",
|
|
336
|
+
status: COHTGOV_RENDER_LIFECYCLE_V2.QUEUED,
|
|
337
|
+
createdAt: now,
|
|
338
|
+
updatedAt: now,
|
|
339
|
+
startedAt: null,
|
|
340
|
+
settledAt: null,
|
|
341
|
+
metadata: { ...(metadata || {}) },
|
|
342
|
+
};
|
|
343
|
+
_cohtgovJsV2.set(id, j);
|
|
344
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
345
|
+
}
|
|
346
|
+
export function renderingCohtgovRenderV2(id) {
|
|
347
|
+
const j = _cohtgovJsV2.get(id);
|
|
348
|
+
if (!j) throw new Error(`cohtgov render ${id} not found`);
|
|
349
|
+
_cohtgovCheckJ(j.status, COHTGOV_RENDER_LIFECYCLE_V2.RENDERING);
|
|
350
|
+
const now = Date.now();
|
|
351
|
+
j.status = COHTGOV_RENDER_LIFECYCLE_V2.RENDERING;
|
|
352
|
+
j.updatedAt = now;
|
|
353
|
+
if (!j.startedAt) j.startedAt = now;
|
|
354
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
355
|
+
}
|
|
356
|
+
export function completeRenderCohtgovV2(id) {
|
|
357
|
+
const j = _cohtgovJsV2.get(id);
|
|
358
|
+
if (!j) throw new Error(`cohtgov render ${id} not found`);
|
|
359
|
+
_cohtgovCheckJ(j.status, COHTGOV_RENDER_LIFECYCLE_V2.RENDERED);
|
|
360
|
+
const now = Date.now();
|
|
361
|
+
j.status = COHTGOV_RENDER_LIFECYCLE_V2.RENDERED;
|
|
362
|
+
j.updatedAt = now;
|
|
363
|
+
if (!j.settledAt) j.settledAt = now;
|
|
364
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
365
|
+
}
|
|
366
|
+
export function failCohtgovRenderV2(id, reason) {
|
|
367
|
+
const j = _cohtgovJsV2.get(id);
|
|
368
|
+
if (!j) throw new Error(`cohtgov render ${id} not found`);
|
|
369
|
+
_cohtgovCheckJ(j.status, COHTGOV_RENDER_LIFECYCLE_V2.FAILED);
|
|
370
|
+
const now = Date.now();
|
|
371
|
+
j.status = COHTGOV_RENDER_LIFECYCLE_V2.FAILED;
|
|
372
|
+
j.updatedAt = now;
|
|
373
|
+
if (!j.settledAt) j.settledAt = now;
|
|
374
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
375
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
376
|
+
}
|
|
377
|
+
export function cancelCohtgovRenderV2(id, reason) {
|
|
378
|
+
const j = _cohtgovJsV2.get(id);
|
|
379
|
+
if (!j) throw new Error(`cohtgov render ${id} not found`);
|
|
380
|
+
_cohtgovCheckJ(j.status, COHTGOV_RENDER_LIFECYCLE_V2.CANCELLED);
|
|
381
|
+
const now = Date.now();
|
|
382
|
+
j.status = COHTGOV_RENDER_LIFECYCLE_V2.CANCELLED;
|
|
383
|
+
j.updatedAt = now;
|
|
384
|
+
if (!j.settledAt) j.settledAt = now;
|
|
385
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
386
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
387
|
+
}
|
|
388
|
+
export function getCohtgovRenderV2(id) {
|
|
389
|
+
const j = _cohtgovJsV2.get(id);
|
|
390
|
+
if (!j) return null;
|
|
391
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
392
|
+
}
|
|
393
|
+
export function listCohtgovRendersV2() {
|
|
394
|
+
return [..._cohtgovJsV2.values()].map((j) => ({
|
|
395
|
+
...j,
|
|
396
|
+
metadata: { ...j.metadata },
|
|
397
|
+
}));
|
|
398
|
+
}
|
|
399
|
+
export function autoStaleIdleCohtgovProfilesV2({ now } = {}) {
|
|
400
|
+
const t = now ?? Date.now();
|
|
401
|
+
const flipped = [];
|
|
402
|
+
for (const p of _cohtgovPsV2.values())
|
|
403
|
+
if (
|
|
404
|
+
p.status === COHTGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
405
|
+
t - p.lastTouchedAt >= _cohtgovIdleMs
|
|
406
|
+
) {
|
|
407
|
+
p.status = COHTGOV_PROFILE_MATURITY_V2.STALE;
|
|
408
|
+
p.updatedAt = t;
|
|
409
|
+
flipped.push(p.id);
|
|
410
|
+
}
|
|
411
|
+
return { flipped, count: flipped.length };
|
|
412
|
+
}
|
|
413
|
+
export function autoFailStuckCohtgovRendersV2({ now } = {}) {
|
|
414
|
+
const t = now ?? Date.now();
|
|
415
|
+
const flipped = [];
|
|
416
|
+
for (const j of _cohtgovJsV2.values())
|
|
417
|
+
if (
|
|
418
|
+
j.status === COHTGOV_RENDER_LIFECYCLE_V2.RENDERING &&
|
|
419
|
+
j.startedAt != null &&
|
|
420
|
+
t - j.startedAt >= _cohtgovStuckMs
|
|
421
|
+
) {
|
|
422
|
+
j.status = COHTGOV_RENDER_LIFECYCLE_V2.FAILED;
|
|
423
|
+
j.updatedAt = t;
|
|
424
|
+
if (!j.settledAt) j.settledAt = t;
|
|
425
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
426
|
+
flipped.push(j.id);
|
|
427
|
+
}
|
|
428
|
+
return { flipped, count: flipped.length };
|
|
429
|
+
}
|
|
430
|
+
export function getCoworkObserveHtmlGovStatsV2() {
|
|
431
|
+
const profilesByStatus = {};
|
|
432
|
+
for (const v of Object.values(COHTGOV_PROFILE_MATURITY_V2))
|
|
433
|
+
profilesByStatus[v] = 0;
|
|
434
|
+
for (const p of _cohtgovPsV2.values()) profilesByStatus[p.status]++;
|
|
435
|
+
const rendersByStatus = {};
|
|
436
|
+
for (const v of Object.values(COHTGOV_RENDER_LIFECYCLE_V2))
|
|
437
|
+
rendersByStatus[v] = 0;
|
|
438
|
+
for (const j of _cohtgovJsV2.values()) rendersByStatus[j.status]++;
|
|
439
|
+
return {
|
|
440
|
+
totalCohtgovProfilesV2: _cohtgovPsV2.size,
|
|
441
|
+
totalCohtgovRendersV2: _cohtgovJsV2.size,
|
|
442
|
+
maxActiveCohtgovProfilesPerOwner: _cohtgovMaxActive,
|
|
443
|
+
maxPendingCohtgovRendersPerProfile: _cohtgovMaxPending,
|
|
444
|
+
cohtgovProfileIdleMs: _cohtgovIdleMs,
|
|
445
|
+
cohtgovRenderStuckMs: _cohtgovStuckMs,
|
|
446
|
+
profilesByStatus,
|
|
447
|
+
rendersByStatus,
|
|
448
|
+
};
|
|
449
|
+
}
|