chainlesschain 0.152.0 → 0.156.2
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
|
@@ -158,3 +158,344 @@ export function aggregate(cwd, { windowDays = 7 } = {}) {
|
|
|
158
158
|
},
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
|
+
|
|
162
|
+
// =====================================================================
|
|
163
|
+
// cowork-observe V2 governance overlay (iter27)
|
|
164
|
+
// =====================================================================
|
|
165
|
+
export const COBSGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
166
|
+
PENDING: "pending",
|
|
167
|
+
ACTIVE: "active",
|
|
168
|
+
MUTED: "muted",
|
|
169
|
+
ARCHIVED: "archived",
|
|
170
|
+
});
|
|
171
|
+
export const COBSGOV_EVENT_LIFECYCLE_V2 = Object.freeze({
|
|
172
|
+
QUEUED: "queued",
|
|
173
|
+
RECORDING: "recording",
|
|
174
|
+
RECORDED: "recorded",
|
|
175
|
+
FAILED: "failed",
|
|
176
|
+
CANCELLED: "cancelled",
|
|
177
|
+
});
|
|
178
|
+
const _cobsgovPTrans = new Map([
|
|
179
|
+
[
|
|
180
|
+
COBSGOV_PROFILE_MATURITY_V2.PENDING,
|
|
181
|
+
new Set([
|
|
182
|
+
COBSGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
183
|
+
COBSGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
184
|
+
]),
|
|
185
|
+
],
|
|
186
|
+
[
|
|
187
|
+
COBSGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
188
|
+
new Set([
|
|
189
|
+
COBSGOV_PROFILE_MATURITY_V2.MUTED,
|
|
190
|
+
COBSGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
191
|
+
]),
|
|
192
|
+
],
|
|
193
|
+
[
|
|
194
|
+
COBSGOV_PROFILE_MATURITY_V2.MUTED,
|
|
195
|
+
new Set([
|
|
196
|
+
COBSGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
197
|
+
COBSGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
198
|
+
]),
|
|
199
|
+
],
|
|
200
|
+
[COBSGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
201
|
+
]);
|
|
202
|
+
const _cobsgovPTerminal = new Set([COBSGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
203
|
+
const _cobsgovJTrans = new Map([
|
|
204
|
+
[
|
|
205
|
+
COBSGOV_EVENT_LIFECYCLE_V2.QUEUED,
|
|
206
|
+
new Set([
|
|
207
|
+
COBSGOV_EVENT_LIFECYCLE_V2.RECORDING,
|
|
208
|
+
COBSGOV_EVENT_LIFECYCLE_V2.CANCELLED,
|
|
209
|
+
]),
|
|
210
|
+
],
|
|
211
|
+
[
|
|
212
|
+
COBSGOV_EVENT_LIFECYCLE_V2.RECORDING,
|
|
213
|
+
new Set([
|
|
214
|
+
COBSGOV_EVENT_LIFECYCLE_V2.RECORDED,
|
|
215
|
+
COBSGOV_EVENT_LIFECYCLE_V2.FAILED,
|
|
216
|
+
COBSGOV_EVENT_LIFECYCLE_V2.CANCELLED,
|
|
217
|
+
]),
|
|
218
|
+
],
|
|
219
|
+
[COBSGOV_EVENT_LIFECYCLE_V2.RECORDED, new Set()],
|
|
220
|
+
[COBSGOV_EVENT_LIFECYCLE_V2.FAILED, new Set()],
|
|
221
|
+
[COBSGOV_EVENT_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
222
|
+
]);
|
|
223
|
+
const _cobsgovPsV2 = new Map();
|
|
224
|
+
const _cobsgovJsV2 = new Map();
|
|
225
|
+
let _cobsgovMaxActive = 10,
|
|
226
|
+
_cobsgovMaxPending = 25,
|
|
227
|
+
_cobsgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
228
|
+
_cobsgovStuckMs = 60 * 1000;
|
|
229
|
+
function _cobsgovPos(n, label) {
|
|
230
|
+
const v = Math.floor(Number(n));
|
|
231
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
232
|
+
throw new Error(`${label} must be positive integer`);
|
|
233
|
+
return v;
|
|
234
|
+
}
|
|
235
|
+
function _cobsgovCheckP(from, to) {
|
|
236
|
+
const a = _cobsgovPTrans.get(from);
|
|
237
|
+
if (!a || !a.has(to))
|
|
238
|
+
throw new Error(`invalid cobsgov profile transition ${from} → ${to}`);
|
|
239
|
+
}
|
|
240
|
+
function _cobsgovCheckJ(from, to) {
|
|
241
|
+
const a = _cobsgovJTrans.get(from);
|
|
242
|
+
if (!a || !a.has(to))
|
|
243
|
+
throw new Error(`invalid cobsgov event transition ${from} → ${to}`);
|
|
244
|
+
}
|
|
245
|
+
function _cobsgovCountActive(owner) {
|
|
246
|
+
let c = 0;
|
|
247
|
+
for (const p of _cobsgovPsV2.values())
|
|
248
|
+
if (p.owner === owner && p.status === COBSGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
249
|
+
c++;
|
|
250
|
+
return c;
|
|
251
|
+
}
|
|
252
|
+
function _cobsgovCountPending(profileId) {
|
|
253
|
+
let c = 0;
|
|
254
|
+
for (const j of _cobsgovJsV2.values())
|
|
255
|
+
if (
|
|
256
|
+
j.profileId === profileId &&
|
|
257
|
+
(j.status === COBSGOV_EVENT_LIFECYCLE_V2.QUEUED ||
|
|
258
|
+
j.status === COBSGOV_EVENT_LIFECYCLE_V2.RECORDING)
|
|
259
|
+
)
|
|
260
|
+
c++;
|
|
261
|
+
return c;
|
|
262
|
+
}
|
|
263
|
+
export function setMaxActiveCobsgovProfilesPerOwnerV2(n) {
|
|
264
|
+
_cobsgovMaxActive = _cobsgovPos(n, "maxActiveCobsgovProfilesPerOwner");
|
|
265
|
+
}
|
|
266
|
+
export function getMaxActiveCobsgovProfilesPerOwnerV2() {
|
|
267
|
+
return _cobsgovMaxActive;
|
|
268
|
+
}
|
|
269
|
+
export function setMaxPendingCobsgovEventsPerProfileV2(n) {
|
|
270
|
+
_cobsgovMaxPending = _cobsgovPos(n, "maxPendingCobsgovEventsPerProfile");
|
|
271
|
+
}
|
|
272
|
+
export function getMaxPendingCobsgovEventsPerProfileV2() {
|
|
273
|
+
return _cobsgovMaxPending;
|
|
274
|
+
}
|
|
275
|
+
export function setCobsgovProfileIdleMsV2(n) {
|
|
276
|
+
_cobsgovIdleMs = _cobsgovPos(n, "cobsgovProfileIdleMs");
|
|
277
|
+
}
|
|
278
|
+
export function getCobsgovProfileIdleMsV2() {
|
|
279
|
+
return _cobsgovIdleMs;
|
|
280
|
+
}
|
|
281
|
+
export function setCobsgovEventStuckMsV2(n) {
|
|
282
|
+
_cobsgovStuckMs = _cobsgovPos(n, "cobsgovEventStuckMs");
|
|
283
|
+
}
|
|
284
|
+
export function getCobsgovEventStuckMsV2() {
|
|
285
|
+
return _cobsgovStuckMs;
|
|
286
|
+
}
|
|
287
|
+
export function _resetStateCoworkObserveGovV2() {
|
|
288
|
+
_cobsgovPsV2.clear();
|
|
289
|
+
_cobsgovJsV2.clear();
|
|
290
|
+
_cobsgovMaxActive = 10;
|
|
291
|
+
_cobsgovMaxPending = 25;
|
|
292
|
+
_cobsgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
293
|
+
_cobsgovStuckMs = 60 * 1000;
|
|
294
|
+
}
|
|
295
|
+
export function registerCobsgovProfileV2({
|
|
296
|
+
id,
|
|
297
|
+
owner,
|
|
298
|
+
channel,
|
|
299
|
+
metadata,
|
|
300
|
+
} = {}) {
|
|
301
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
302
|
+
if (_cobsgovPsV2.has(id))
|
|
303
|
+
throw new Error(`cobsgov profile ${id} already exists`);
|
|
304
|
+
const now = Date.now();
|
|
305
|
+
const p = {
|
|
306
|
+
id,
|
|
307
|
+
owner,
|
|
308
|
+
channel: channel || "default",
|
|
309
|
+
status: COBSGOV_PROFILE_MATURITY_V2.PENDING,
|
|
310
|
+
createdAt: now,
|
|
311
|
+
updatedAt: now,
|
|
312
|
+
lastTouchedAt: now,
|
|
313
|
+
activatedAt: null,
|
|
314
|
+
archivedAt: null,
|
|
315
|
+
metadata: { ...(metadata || {}) },
|
|
316
|
+
};
|
|
317
|
+
_cobsgovPsV2.set(id, p);
|
|
318
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
319
|
+
}
|
|
320
|
+
export function activateCobsgovProfileV2(id) {
|
|
321
|
+
const p = _cobsgovPsV2.get(id);
|
|
322
|
+
if (!p) throw new Error(`cobsgov profile ${id} not found`);
|
|
323
|
+
const isInitial = p.status === COBSGOV_PROFILE_MATURITY_V2.PENDING;
|
|
324
|
+
_cobsgovCheckP(p.status, COBSGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
325
|
+
if (isInitial && _cobsgovCountActive(p.owner) >= _cobsgovMaxActive)
|
|
326
|
+
throw new Error(`max active cobsgov profiles for owner ${p.owner} reached`);
|
|
327
|
+
const now = Date.now();
|
|
328
|
+
p.status = COBSGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
329
|
+
p.updatedAt = now;
|
|
330
|
+
p.lastTouchedAt = now;
|
|
331
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
332
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
333
|
+
}
|
|
334
|
+
export function muteCobsgovProfileV2(id) {
|
|
335
|
+
const p = _cobsgovPsV2.get(id);
|
|
336
|
+
if (!p) throw new Error(`cobsgov profile ${id} not found`);
|
|
337
|
+
_cobsgovCheckP(p.status, COBSGOV_PROFILE_MATURITY_V2.MUTED);
|
|
338
|
+
p.status = COBSGOV_PROFILE_MATURITY_V2.MUTED;
|
|
339
|
+
p.updatedAt = Date.now();
|
|
340
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
341
|
+
}
|
|
342
|
+
export function archiveCobsgovProfileV2(id) {
|
|
343
|
+
const p = _cobsgovPsV2.get(id);
|
|
344
|
+
if (!p) throw new Error(`cobsgov profile ${id} not found`);
|
|
345
|
+
_cobsgovCheckP(p.status, COBSGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
346
|
+
const now = Date.now();
|
|
347
|
+
p.status = COBSGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
348
|
+
p.updatedAt = now;
|
|
349
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
350
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
351
|
+
}
|
|
352
|
+
export function touchCobsgovProfileV2(id) {
|
|
353
|
+
const p = _cobsgovPsV2.get(id);
|
|
354
|
+
if (!p) throw new Error(`cobsgov profile ${id} not found`);
|
|
355
|
+
if (_cobsgovPTerminal.has(p.status))
|
|
356
|
+
throw new Error(`cannot touch terminal cobsgov profile ${id}`);
|
|
357
|
+
const now = Date.now();
|
|
358
|
+
p.lastTouchedAt = now;
|
|
359
|
+
p.updatedAt = now;
|
|
360
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
361
|
+
}
|
|
362
|
+
export function getCobsgovProfileV2(id) {
|
|
363
|
+
const p = _cobsgovPsV2.get(id);
|
|
364
|
+
if (!p) return null;
|
|
365
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
366
|
+
}
|
|
367
|
+
export function listCobsgovProfilesV2() {
|
|
368
|
+
return [..._cobsgovPsV2.values()].map((p) => ({
|
|
369
|
+
...p,
|
|
370
|
+
metadata: { ...p.metadata },
|
|
371
|
+
}));
|
|
372
|
+
}
|
|
373
|
+
export function createCobsgovEventV2({ id, profileId, kind, metadata } = {}) {
|
|
374
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
375
|
+
if (_cobsgovJsV2.has(id))
|
|
376
|
+
throw new Error(`cobsgov event ${id} already exists`);
|
|
377
|
+
if (!_cobsgovPsV2.has(profileId))
|
|
378
|
+
throw new Error(`cobsgov profile ${profileId} not found`);
|
|
379
|
+
if (_cobsgovCountPending(profileId) >= _cobsgovMaxPending)
|
|
380
|
+
throw new Error(
|
|
381
|
+
`max pending cobsgov events for profile ${profileId} reached`,
|
|
382
|
+
);
|
|
383
|
+
const now = Date.now();
|
|
384
|
+
const j = {
|
|
385
|
+
id,
|
|
386
|
+
profileId,
|
|
387
|
+
kind: kind || "",
|
|
388
|
+
status: COBSGOV_EVENT_LIFECYCLE_V2.QUEUED,
|
|
389
|
+
createdAt: now,
|
|
390
|
+
updatedAt: now,
|
|
391
|
+
startedAt: null,
|
|
392
|
+
settledAt: null,
|
|
393
|
+
metadata: { ...(metadata || {}) },
|
|
394
|
+
};
|
|
395
|
+
_cobsgovJsV2.set(id, j);
|
|
396
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
397
|
+
}
|
|
398
|
+
export function recordingCobsgovEventV2(id) {
|
|
399
|
+
const j = _cobsgovJsV2.get(id);
|
|
400
|
+
if (!j) throw new Error(`cobsgov event ${id} not found`);
|
|
401
|
+
_cobsgovCheckJ(j.status, COBSGOV_EVENT_LIFECYCLE_V2.RECORDING);
|
|
402
|
+
const now = Date.now();
|
|
403
|
+
j.status = COBSGOV_EVENT_LIFECYCLE_V2.RECORDING;
|
|
404
|
+
j.updatedAt = now;
|
|
405
|
+
if (!j.startedAt) j.startedAt = now;
|
|
406
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
407
|
+
}
|
|
408
|
+
export function completeEventCobsgovV2(id) {
|
|
409
|
+
const j = _cobsgovJsV2.get(id);
|
|
410
|
+
if (!j) throw new Error(`cobsgov event ${id} not found`);
|
|
411
|
+
_cobsgovCheckJ(j.status, COBSGOV_EVENT_LIFECYCLE_V2.RECORDED);
|
|
412
|
+
const now = Date.now();
|
|
413
|
+
j.status = COBSGOV_EVENT_LIFECYCLE_V2.RECORDED;
|
|
414
|
+
j.updatedAt = now;
|
|
415
|
+
if (!j.settledAt) j.settledAt = now;
|
|
416
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
417
|
+
}
|
|
418
|
+
export function failCobsgovEventV2(id, reason) {
|
|
419
|
+
const j = _cobsgovJsV2.get(id);
|
|
420
|
+
if (!j) throw new Error(`cobsgov event ${id} not found`);
|
|
421
|
+
_cobsgovCheckJ(j.status, COBSGOV_EVENT_LIFECYCLE_V2.FAILED);
|
|
422
|
+
const now = Date.now();
|
|
423
|
+
j.status = COBSGOV_EVENT_LIFECYCLE_V2.FAILED;
|
|
424
|
+
j.updatedAt = now;
|
|
425
|
+
if (!j.settledAt) j.settledAt = now;
|
|
426
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
427
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
428
|
+
}
|
|
429
|
+
export function cancelCobsgovEventV2(id, reason) {
|
|
430
|
+
const j = _cobsgovJsV2.get(id);
|
|
431
|
+
if (!j) throw new Error(`cobsgov event ${id} not found`);
|
|
432
|
+
_cobsgovCheckJ(j.status, COBSGOV_EVENT_LIFECYCLE_V2.CANCELLED);
|
|
433
|
+
const now = Date.now();
|
|
434
|
+
j.status = COBSGOV_EVENT_LIFECYCLE_V2.CANCELLED;
|
|
435
|
+
j.updatedAt = now;
|
|
436
|
+
if (!j.settledAt) j.settledAt = now;
|
|
437
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
438
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
439
|
+
}
|
|
440
|
+
export function getCobsgovEventV2(id) {
|
|
441
|
+
const j = _cobsgovJsV2.get(id);
|
|
442
|
+
if (!j) return null;
|
|
443
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
444
|
+
}
|
|
445
|
+
export function listCobsgovEventsV2() {
|
|
446
|
+
return [..._cobsgovJsV2.values()].map((j) => ({
|
|
447
|
+
...j,
|
|
448
|
+
metadata: { ...j.metadata },
|
|
449
|
+
}));
|
|
450
|
+
}
|
|
451
|
+
export function autoMuteIdleCobsgovProfilesV2({ now } = {}) {
|
|
452
|
+
const t = now ?? Date.now();
|
|
453
|
+
const flipped = [];
|
|
454
|
+
for (const p of _cobsgovPsV2.values())
|
|
455
|
+
if (
|
|
456
|
+
p.status === COBSGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
457
|
+
t - p.lastTouchedAt >= _cobsgovIdleMs
|
|
458
|
+
) {
|
|
459
|
+
p.status = COBSGOV_PROFILE_MATURITY_V2.MUTED;
|
|
460
|
+
p.updatedAt = t;
|
|
461
|
+
flipped.push(p.id);
|
|
462
|
+
}
|
|
463
|
+
return { flipped, count: flipped.length };
|
|
464
|
+
}
|
|
465
|
+
export function autoFailStuckCobsgovEventsV2({ now } = {}) {
|
|
466
|
+
const t = now ?? Date.now();
|
|
467
|
+
const flipped = [];
|
|
468
|
+
for (const j of _cobsgovJsV2.values())
|
|
469
|
+
if (
|
|
470
|
+
j.status === COBSGOV_EVENT_LIFECYCLE_V2.RECORDING &&
|
|
471
|
+
j.startedAt != null &&
|
|
472
|
+
t - j.startedAt >= _cobsgovStuckMs
|
|
473
|
+
) {
|
|
474
|
+
j.status = COBSGOV_EVENT_LIFECYCLE_V2.FAILED;
|
|
475
|
+
j.updatedAt = t;
|
|
476
|
+
if (!j.settledAt) j.settledAt = t;
|
|
477
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
478
|
+
flipped.push(j.id);
|
|
479
|
+
}
|
|
480
|
+
return { flipped, count: flipped.length };
|
|
481
|
+
}
|
|
482
|
+
export function getCoworkObserveGovStatsV2() {
|
|
483
|
+
const profilesByStatus = {};
|
|
484
|
+
for (const v of Object.values(COBSGOV_PROFILE_MATURITY_V2))
|
|
485
|
+
profilesByStatus[v] = 0;
|
|
486
|
+
for (const p of _cobsgovPsV2.values()) profilesByStatus[p.status]++;
|
|
487
|
+
const eventsByStatus = {};
|
|
488
|
+
for (const v of Object.values(COBSGOV_EVENT_LIFECYCLE_V2))
|
|
489
|
+
eventsByStatus[v] = 0;
|
|
490
|
+
for (const j of _cobsgovJsV2.values()) eventsByStatus[j.status]++;
|
|
491
|
+
return {
|
|
492
|
+
totalCobsgovProfilesV2: _cobsgovPsV2.size,
|
|
493
|
+
totalCobsgovEventsV2: _cobsgovJsV2.size,
|
|
494
|
+
maxActiveCobsgovProfilesPerOwner: _cobsgovMaxActive,
|
|
495
|
+
maxPendingCobsgovEventsPerProfile: _cobsgovMaxPending,
|
|
496
|
+
cobsgovProfileIdleMs: _cobsgovIdleMs,
|
|
497
|
+
cobsgovEventStuckMs: _cobsgovStuckMs,
|
|
498
|
+
profilesByStatus,
|
|
499
|
+
eventsByStatus,
|
|
500
|
+
};
|
|
501
|
+
}
|
|
@@ -651,7 +651,11 @@ export function getTemplatesForUI() {
|
|
|
651
651
|
const userInstalled = Object.values(_userTemplates).map((tpl) => ({
|
|
652
652
|
tpl,
|
|
653
653
|
// User templates carry their own ui metadata inline
|
|
654
|
-
ui: {
|
|
654
|
+
ui: {
|
|
655
|
+
icon: tpl.icon,
|
|
656
|
+
description: tpl.description,
|
|
657
|
+
examples: tpl.examples,
|
|
658
|
+
},
|
|
655
659
|
source: "user",
|
|
656
660
|
}));
|
|
657
661
|
return [...builtIn, ...userInstalled].map(({ tpl, ui, source }) => {
|
|
@@ -684,3 +688,340 @@ export function getTemplatesForUI() {
|
|
|
684
688
|
};
|
|
685
689
|
});
|
|
686
690
|
}
|
|
691
|
+
|
|
692
|
+
// =====================================================================
|
|
693
|
+
// cowork-task-templates V2 governance overlay (iter25)
|
|
694
|
+
// =====================================================================
|
|
695
|
+
export const CTTGOV_PROFILE_MATURITY_V2 = Object.freeze({
|
|
696
|
+
PENDING: "pending",
|
|
697
|
+
ACTIVE: "active",
|
|
698
|
+
STALE: "stale",
|
|
699
|
+
ARCHIVED: "archived",
|
|
700
|
+
});
|
|
701
|
+
export const CTTGOV_USE_LIFECYCLE_V2 = Object.freeze({
|
|
702
|
+
QUEUED: "queued",
|
|
703
|
+
APPLYING: "applying",
|
|
704
|
+
APPLIED: "applied",
|
|
705
|
+
FAILED: "failed",
|
|
706
|
+
CANCELLED: "cancelled",
|
|
707
|
+
});
|
|
708
|
+
const _cttgovPTrans = new Map([
|
|
709
|
+
[
|
|
710
|
+
CTTGOV_PROFILE_MATURITY_V2.PENDING,
|
|
711
|
+
new Set([
|
|
712
|
+
CTTGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
713
|
+
CTTGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
714
|
+
]),
|
|
715
|
+
],
|
|
716
|
+
[
|
|
717
|
+
CTTGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
718
|
+
new Set([
|
|
719
|
+
CTTGOV_PROFILE_MATURITY_V2.STALE,
|
|
720
|
+
CTTGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
721
|
+
]),
|
|
722
|
+
],
|
|
723
|
+
[
|
|
724
|
+
CTTGOV_PROFILE_MATURITY_V2.STALE,
|
|
725
|
+
new Set([
|
|
726
|
+
CTTGOV_PROFILE_MATURITY_V2.ACTIVE,
|
|
727
|
+
CTTGOV_PROFILE_MATURITY_V2.ARCHIVED,
|
|
728
|
+
]),
|
|
729
|
+
],
|
|
730
|
+
[CTTGOV_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
|
|
731
|
+
]);
|
|
732
|
+
const _cttgovPTerminal = new Set([CTTGOV_PROFILE_MATURITY_V2.ARCHIVED]);
|
|
733
|
+
const _cttgovJTrans = new Map([
|
|
734
|
+
[
|
|
735
|
+
CTTGOV_USE_LIFECYCLE_V2.QUEUED,
|
|
736
|
+
new Set([
|
|
737
|
+
CTTGOV_USE_LIFECYCLE_V2.APPLYING,
|
|
738
|
+
CTTGOV_USE_LIFECYCLE_V2.CANCELLED,
|
|
739
|
+
]),
|
|
740
|
+
],
|
|
741
|
+
[
|
|
742
|
+
CTTGOV_USE_LIFECYCLE_V2.APPLYING,
|
|
743
|
+
new Set([
|
|
744
|
+
CTTGOV_USE_LIFECYCLE_V2.APPLIED,
|
|
745
|
+
CTTGOV_USE_LIFECYCLE_V2.FAILED,
|
|
746
|
+
CTTGOV_USE_LIFECYCLE_V2.CANCELLED,
|
|
747
|
+
]),
|
|
748
|
+
],
|
|
749
|
+
[CTTGOV_USE_LIFECYCLE_V2.APPLIED, new Set()],
|
|
750
|
+
[CTTGOV_USE_LIFECYCLE_V2.FAILED, new Set()],
|
|
751
|
+
[CTTGOV_USE_LIFECYCLE_V2.CANCELLED, new Set()],
|
|
752
|
+
]);
|
|
753
|
+
const _cttgovPsV2 = new Map();
|
|
754
|
+
const _cttgovJsV2 = new Map();
|
|
755
|
+
let _cttgovMaxActive = 8,
|
|
756
|
+
_cttgovMaxPending = 20,
|
|
757
|
+
_cttgovIdleMs = 30 * 24 * 60 * 60 * 1000,
|
|
758
|
+
_cttgovStuckMs = 60 * 1000;
|
|
759
|
+
function _cttgovPos(n, label) {
|
|
760
|
+
const v = Math.floor(Number(n));
|
|
761
|
+
if (!Number.isFinite(v) || v <= 0)
|
|
762
|
+
throw new Error(`${label} must be positive integer`);
|
|
763
|
+
return v;
|
|
764
|
+
}
|
|
765
|
+
function _cttgovCheckP(from, to) {
|
|
766
|
+
const a = _cttgovPTrans.get(from);
|
|
767
|
+
if (!a || !a.has(to))
|
|
768
|
+
throw new Error(`invalid cttgov profile transition ${from} → ${to}`);
|
|
769
|
+
}
|
|
770
|
+
function _cttgovCheckJ(from, to) {
|
|
771
|
+
const a = _cttgovJTrans.get(from);
|
|
772
|
+
if (!a || !a.has(to))
|
|
773
|
+
throw new Error(`invalid cttgov use transition ${from} → ${to}`);
|
|
774
|
+
}
|
|
775
|
+
function _cttgovCountActive(owner) {
|
|
776
|
+
let c = 0;
|
|
777
|
+
for (const p of _cttgovPsV2.values())
|
|
778
|
+
if (p.owner === owner && p.status === CTTGOV_PROFILE_MATURITY_V2.ACTIVE)
|
|
779
|
+
c++;
|
|
780
|
+
return c;
|
|
781
|
+
}
|
|
782
|
+
function _cttgovCountPending(profileId) {
|
|
783
|
+
let c = 0;
|
|
784
|
+
for (const j of _cttgovJsV2.values())
|
|
785
|
+
if (
|
|
786
|
+
j.profileId === profileId &&
|
|
787
|
+
(j.status === CTTGOV_USE_LIFECYCLE_V2.QUEUED ||
|
|
788
|
+
j.status === CTTGOV_USE_LIFECYCLE_V2.APPLYING)
|
|
789
|
+
)
|
|
790
|
+
c++;
|
|
791
|
+
return c;
|
|
792
|
+
}
|
|
793
|
+
export function setMaxActiveCttgovProfilesPerOwnerV2(n) {
|
|
794
|
+
_cttgovMaxActive = _cttgovPos(n, "maxActiveCttgovProfilesPerOwner");
|
|
795
|
+
}
|
|
796
|
+
export function getMaxActiveCttgovProfilesPerOwnerV2() {
|
|
797
|
+
return _cttgovMaxActive;
|
|
798
|
+
}
|
|
799
|
+
export function setMaxPendingCttgovUsesPerProfileV2(n) {
|
|
800
|
+
_cttgovMaxPending = _cttgovPos(n, "maxPendingCttgovUsesPerProfile");
|
|
801
|
+
}
|
|
802
|
+
export function getMaxPendingCttgovUsesPerProfileV2() {
|
|
803
|
+
return _cttgovMaxPending;
|
|
804
|
+
}
|
|
805
|
+
export function setCttgovProfileIdleMsV2(n) {
|
|
806
|
+
_cttgovIdleMs = _cttgovPos(n, "cttgovProfileIdleMs");
|
|
807
|
+
}
|
|
808
|
+
export function getCttgovProfileIdleMsV2() {
|
|
809
|
+
return _cttgovIdleMs;
|
|
810
|
+
}
|
|
811
|
+
export function setCttgovUseStuckMsV2(n) {
|
|
812
|
+
_cttgovStuckMs = _cttgovPos(n, "cttgovUseStuckMs");
|
|
813
|
+
}
|
|
814
|
+
export function getCttgovUseStuckMsV2() {
|
|
815
|
+
return _cttgovStuckMs;
|
|
816
|
+
}
|
|
817
|
+
export function _resetStateCoworkTaskTemplatesGovV2() {
|
|
818
|
+
_cttgovPsV2.clear();
|
|
819
|
+
_cttgovJsV2.clear();
|
|
820
|
+
_cttgovMaxActive = 8;
|
|
821
|
+
_cttgovMaxPending = 20;
|
|
822
|
+
_cttgovIdleMs = 30 * 24 * 60 * 60 * 1000;
|
|
823
|
+
_cttgovStuckMs = 60 * 1000;
|
|
824
|
+
}
|
|
825
|
+
export function registerCttgovProfileV2({
|
|
826
|
+
id,
|
|
827
|
+
owner,
|
|
828
|
+
category,
|
|
829
|
+
metadata,
|
|
830
|
+
} = {}) {
|
|
831
|
+
if (!id || !owner) throw new Error("id and owner required");
|
|
832
|
+
if (_cttgovPsV2.has(id))
|
|
833
|
+
throw new Error(`cttgov profile ${id} already exists`);
|
|
834
|
+
const now = Date.now();
|
|
835
|
+
const p = {
|
|
836
|
+
id,
|
|
837
|
+
owner,
|
|
838
|
+
category: category || "general",
|
|
839
|
+
status: CTTGOV_PROFILE_MATURITY_V2.PENDING,
|
|
840
|
+
createdAt: now,
|
|
841
|
+
updatedAt: now,
|
|
842
|
+
lastTouchedAt: now,
|
|
843
|
+
activatedAt: null,
|
|
844
|
+
archivedAt: null,
|
|
845
|
+
metadata: { ...(metadata || {}) },
|
|
846
|
+
};
|
|
847
|
+
_cttgovPsV2.set(id, p);
|
|
848
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
849
|
+
}
|
|
850
|
+
export function activateCttgovProfileV2(id) {
|
|
851
|
+
const p = _cttgovPsV2.get(id);
|
|
852
|
+
if (!p) throw new Error(`cttgov profile ${id} not found`);
|
|
853
|
+
const isInitial = p.status === CTTGOV_PROFILE_MATURITY_V2.PENDING;
|
|
854
|
+
_cttgovCheckP(p.status, CTTGOV_PROFILE_MATURITY_V2.ACTIVE);
|
|
855
|
+
if (isInitial && _cttgovCountActive(p.owner) >= _cttgovMaxActive)
|
|
856
|
+
throw new Error(`max active cttgov profiles for owner ${p.owner} reached`);
|
|
857
|
+
const now = Date.now();
|
|
858
|
+
p.status = CTTGOV_PROFILE_MATURITY_V2.ACTIVE;
|
|
859
|
+
p.updatedAt = now;
|
|
860
|
+
p.lastTouchedAt = now;
|
|
861
|
+
if (!p.activatedAt) p.activatedAt = now;
|
|
862
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
863
|
+
}
|
|
864
|
+
export function staleCttgovProfileV2(id) {
|
|
865
|
+
const p = _cttgovPsV2.get(id);
|
|
866
|
+
if (!p) throw new Error(`cttgov profile ${id} not found`);
|
|
867
|
+
_cttgovCheckP(p.status, CTTGOV_PROFILE_MATURITY_V2.STALE);
|
|
868
|
+
p.status = CTTGOV_PROFILE_MATURITY_V2.STALE;
|
|
869
|
+
p.updatedAt = Date.now();
|
|
870
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
871
|
+
}
|
|
872
|
+
export function archiveCttgovProfileV2(id) {
|
|
873
|
+
const p = _cttgovPsV2.get(id);
|
|
874
|
+
if (!p) throw new Error(`cttgov profile ${id} not found`);
|
|
875
|
+
_cttgovCheckP(p.status, CTTGOV_PROFILE_MATURITY_V2.ARCHIVED);
|
|
876
|
+
const now = Date.now();
|
|
877
|
+
p.status = CTTGOV_PROFILE_MATURITY_V2.ARCHIVED;
|
|
878
|
+
p.updatedAt = now;
|
|
879
|
+
if (!p.archivedAt) p.archivedAt = now;
|
|
880
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
881
|
+
}
|
|
882
|
+
export function touchCttgovProfileV2(id) {
|
|
883
|
+
const p = _cttgovPsV2.get(id);
|
|
884
|
+
if (!p) throw new Error(`cttgov profile ${id} not found`);
|
|
885
|
+
if (_cttgovPTerminal.has(p.status))
|
|
886
|
+
throw new Error(`cannot touch terminal cttgov profile ${id}`);
|
|
887
|
+
const now = Date.now();
|
|
888
|
+
p.lastTouchedAt = now;
|
|
889
|
+
p.updatedAt = now;
|
|
890
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
891
|
+
}
|
|
892
|
+
export function getCttgovProfileV2(id) {
|
|
893
|
+
const p = _cttgovPsV2.get(id);
|
|
894
|
+
if (!p) return null;
|
|
895
|
+
return { ...p, metadata: { ...p.metadata } };
|
|
896
|
+
}
|
|
897
|
+
export function listCttgovProfilesV2() {
|
|
898
|
+
return [..._cttgovPsV2.values()].map((p) => ({
|
|
899
|
+
...p,
|
|
900
|
+
metadata: { ...p.metadata },
|
|
901
|
+
}));
|
|
902
|
+
}
|
|
903
|
+
export function createCttgovUseV2({ id, profileId, context, metadata } = {}) {
|
|
904
|
+
if (!id || !profileId) throw new Error("id and profileId required");
|
|
905
|
+
if (_cttgovJsV2.has(id)) throw new Error(`cttgov use ${id} already exists`);
|
|
906
|
+
if (!_cttgovPsV2.has(profileId))
|
|
907
|
+
throw new Error(`cttgov profile ${profileId} not found`);
|
|
908
|
+
if (_cttgovCountPending(profileId) >= _cttgovMaxPending)
|
|
909
|
+
throw new Error(`max pending cttgov uses for profile ${profileId} reached`);
|
|
910
|
+
const now = Date.now();
|
|
911
|
+
const j = {
|
|
912
|
+
id,
|
|
913
|
+
profileId,
|
|
914
|
+
context: context || "",
|
|
915
|
+
status: CTTGOV_USE_LIFECYCLE_V2.QUEUED,
|
|
916
|
+
createdAt: now,
|
|
917
|
+
updatedAt: now,
|
|
918
|
+
startedAt: null,
|
|
919
|
+
settledAt: null,
|
|
920
|
+
metadata: { ...(metadata || {}) },
|
|
921
|
+
};
|
|
922
|
+
_cttgovJsV2.set(id, j);
|
|
923
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
924
|
+
}
|
|
925
|
+
export function applyingCttgovUseV2(id) {
|
|
926
|
+
const j = _cttgovJsV2.get(id);
|
|
927
|
+
if (!j) throw new Error(`cttgov use ${id} not found`);
|
|
928
|
+
_cttgovCheckJ(j.status, CTTGOV_USE_LIFECYCLE_V2.APPLYING);
|
|
929
|
+
const now = Date.now();
|
|
930
|
+
j.status = CTTGOV_USE_LIFECYCLE_V2.APPLYING;
|
|
931
|
+
j.updatedAt = now;
|
|
932
|
+
if (!j.startedAt) j.startedAt = now;
|
|
933
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
934
|
+
}
|
|
935
|
+
export function completeUseCttgovV2(id) {
|
|
936
|
+
const j = _cttgovJsV2.get(id);
|
|
937
|
+
if (!j) throw new Error(`cttgov use ${id} not found`);
|
|
938
|
+
_cttgovCheckJ(j.status, CTTGOV_USE_LIFECYCLE_V2.APPLIED);
|
|
939
|
+
const now = Date.now();
|
|
940
|
+
j.status = CTTGOV_USE_LIFECYCLE_V2.APPLIED;
|
|
941
|
+
j.updatedAt = now;
|
|
942
|
+
if (!j.settledAt) j.settledAt = now;
|
|
943
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
944
|
+
}
|
|
945
|
+
export function failCttgovUseV2(id, reason) {
|
|
946
|
+
const j = _cttgovJsV2.get(id);
|
|
947
|
+
if (!j) throw new Error(`cttgov use ${id} not found`);
|
|
948
|
+
_cttgovCheckJ(j.status, CTTGOV_USE_LIFECYCLE_V2.FAILED);
|
|
949
|
+
const now = Date.now();
|
|
950
|
+
j.status = CTTGOV_USE_LIFECYCLE_V2.FAILED;
|
|
951
|
+
j.updatedAt = now;
|
|
952
|
+
if (!j.settledAt) j.settledAt = now;
|
|
953
|
+
if (reason) j.metadata.failReason = String(reason);
|
|
954
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
955
|
+
}
|
|
956
|
+
export function cancelCttgovUseV2(id, reason) {
|
|
957
|
+
const j = _cttgovJsV2.get(id);
|
|
958
|
+
if (!j) throw new Error(`cttgov use ${id} not found`);
|
|
959
|
+
_cttgovCheckJ(j.status, CTTGOV_USE_LIFECYCLE_V2.CANCELLED);
|
|
960
|
+
const now = Date.now();
|
|
961
|
+
j.status = CTTGOV_USE_LIFECYCLE_V2.CANCELLED;
|
|
962
|
+
j.updatedAt = now;
|
|
963
|
+
if (!j.settledAt) j.settledAt = now;
|
|
964
|
+
if (reason) j.metadata.cancelReason = String(reason);
|
|
965
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
966
|
+
}
|
|
967
|
+
export function getCttgovUseV2(id) {
|
|
968
|
+
const j = _cttgovJsV2.get(id);
|
|
969
|
+
if (!j) return null;
|
|
970
|
+
return { ...j, metadata: { ...j.metadata } };
|
|
971
|
+
}
|
|
972
|
+
export function listCttgovUsesV2() {
|
|
973
|
+
return [..._cttgovJsV2.values()].map((j) => ({
|
|
974
|
+
...j,
|
|
975
|
+
metadata: { ...j.metadata },
|
|
976
|
+
}));
|
|
977
|
+
}
|
|
978
|
+
export function autoStaleIdleCttgovProfilesV2({ now } = {}) {
|
|
979
|
+
const t = now ?? Date.now();
|
|
980
|
+
const flipped = [];
|
|
981
|
+
for (const p of _cttgovPsV2.values())
|
|
982
|
+
if (
|
|
983
|
+
p.status === CTTGOV_PROFILE_MATURITY_V2.ACTIVE &&
|
|
984
|
+
t - p.lastTouchedAt >= _cttgovIdleMs
|
|
985
|
+
) {
|
|
986
|
+
p.status = CTTGOV_PROFILE_MATURITY_V2.STALE;
|
|
987
|
+
p.updatedAt = t;
|
|
988
|
+
flipped.push(p.id);
|
|
989
|
+
}
|
|
990
|
+
return { flipped, count: flipped.length };
|
|
991
|
+
}
|
|
992
|
+
export function autoFailStuckCttgovUsesV2({ now } = {}) {
|
|
993
|
+
const t = now ?? Date.now();
|
|
994
|
+
const flipped = [];
|
|
995
|
+
for (const j of _cttgovJsV2.values())
|
|
996
|
+
if (
|
|
997
|
+
j.status === CTTGOV_USE_LIFECYCLE_V2.APPLYING &&
|
|
998
|
+
j.startedAt != null &&
|
|
999
|
+
t - j.startedAt >= _cttgovStuckMs
|
|
1000
|
+
) {
|
|
1001
|
+
j.status = CTTGOV_USE_LIFECYCLE_V2.FAILED;
|
|
1002
|
+
j.updatedAt = t;
|
|
1003
|
+
if (!j.settledAt) j.settledAt = t;
|
|
1004
|
+
j.metadata.failReason = "auto-fail-stuck";
|
|
1005
|
+
flipped.push(j.id);
|
|
1006
|
+
}
|
|
1007
|
+
return { flipped, count: flipped.length };
|
|
1008
|
+
}
|
|
1009
|
+
export function getCoworkTaskTemplatesGovStatsV2() {
|
|
1010
|
+
const profilesByStatus = {};
|
|
1011
|
+
for (const v of Object.values(CTTGOV_PROFILE_MATURITY_V2))
|
|
1012
|
+
profilesByStatus[v] = 0;
|
|
1013
|
+
for (const p of _cttgovPsV2.values()) profilesByStatus[p.status]++;
|
|
1014
|
+
const usesByStatus = {};
|
|
1015
|
+
for (const v of Object.values(CTTGOV_USE_LIFECYCLE_V2)) usesByStatus[v] = 0;
|
|
1016
|
+
for (const j of _cttgovJsV2.values()) usesByStatus[j.status]++;
|
|
1017
|
+
return {
|
|
1018
|
+
totalCttgovProfilesV2: _cttgovPsV2.size,
|
|
1019
|
+
totalCttgovUsesV2: _cttgovJsV2.size,
|
|
1020
|
+
maxActiveCttgovProfilesPerOwner: _cttgovMaxActive,
|
|
1021
|
+
maxPendingCttgovUsesPerProfile: _cttgovMaxPending,
|
|
1022
|
+
cttgovProfileIdleMs: _cttgovIdleMs,
|
|
1023
|
+
cttgovUseStuckMs: _cttgovStuckMs,
|
|
1024
|
+
profilesByStatus,
|
|
1025
|
+
usesByStatus,
|
|
1026
|
+
};
|
|
1027
|
+
}
|