chainlesschain 0.132.0 → 0.145.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/package.json +1 -1
  2. package/src/commands/a2a.js +230 -0
  3. package/src/commands/activitypub.js +191 -0
  4. package/src/commands/agent.js +601 -0
  5. package/src/commands/audit.js +206 -0
  6. package/src/commands/bi.js +186 -0
  7. package/src/commands/bm25.js +162 -0
  8. package/src/commands/browse.js +225 -0
  9. package/src/commands/ccron.js +178 -0
  10. package/src/commands/chat.js +207 -0
  11. package/src/commands/compliance.js +420 -0
  12. package/src/commands/compt.js +176 -0
  13. package/src/commands/consol.js +237 -0
  14. package/src/commands/cowork.js +588 -0
  15. package/src/commands/crosschain.js +216 -0
  16. package/src/commands/dao.js +216 -0
  17. package/src/commands/dlp.js +206 -0
  18. package/src/commands/economy.js +211 -0
  19. package/src/commands/evolution.js +209 -0
  20. package/src/commands/evomap.js +216 -0
  21. package/src/commands/fflag.js +230 -0
  22. package/src/commands/git.js +185 -0
  23. package/src/commands/hardening.js +209 -0
  24. package/src/commands/hmemory.js +210 -0
  25. package/src/commands/incentive.js +209 -0
  26. package/src/commands/inference.js +178 -0
  27. package/src/commands/itbudget.js +161 -0
  28. package/src/commands/kg.js +206 -0
  29. package/src/commands/lowcode.js +201 -0
  30. package/src/commands/marketplace.js +206 -0
  31. package/src/commands/matrix.js +214 -0
  32. package/src/commands/mcpscaf.js +153 -0
  33. package/src/commands/meminj.js +153 -0
  34. package/src/commands/nostr.js +213 -0
  35. package/src/commands/orchestrate.js +217 -0
  36. package/src/commands/orchgov.js +156 -0
  37. package/src/commands/pdfp.js +160 -0
  38. package/src/commands/perf.js +176 -0
  39. package/src/commands/perm.js +156 -0
  40. package/src/commands/pipeline.js +211 -0
  41. package/src/commands/planmode.js +154 -0
  42. package/src/commands/privacy.js +203 -0
  43. package/src/commands/promcomp.js +166 -0
  44. package/src/commands/recommend.js +185 -0
  45. package/src/commands/reputation.js +208 -0
  46. package/src/commands/sandbox.js +206 -0
  47. package/src/commands/seshhook.js +153 -0
  48. package/src/commands/seshsearch.js +149 -0
  49. package/src/commands/seshtail.js +152 -0
  50. package/src/commands/seshu.js +160 -0
  51. package/src/commands/sganal.js +172 -0
  52. package/src/commands/siem.js +207 -0
  53. package/src/commands/sla.js +212 -0
  54. package/src/commands/slotfill.js +154 -0
  55. package/src/commands/social.js +159 -0
  56. package/src/commands/stress.js +206 -0
  57. package/src/commands/svccont.js +157 -0
  58. package/src/commands/terraform.js +206 -0
  59. package/src/commands/tms.js +183 -0
  60. package/src/commands/topiccls.js +158 -0
  61. package/src/commands/uprof.js +154 -0
  62. package/src/commands/vcheck.js +172 -0
  63. package/src/commands/webfetch.js +150 -0
  64. package/src/commands/zkp.js +218 -0
  65. package/src/harness/prompt-compressor.js +331 -0
  66. package/src/index.js +101 -1
  67. package/src/lib/a2a-protocol.js +373 -0
  68. package/src/lib/activitypub-bridge.js +343 -0
  69. package/src/lib/agent-economy.js +358 -0
  70. package/src/lib/app-builder.js +338 -0
  71. package/src/lib/audit-logger.js +321 -0
  72. package/src/lib/autonomous-agent.js +341 -0
  73. package/src/lib/bi-engine.js +339 -0
  74. package/src/lib/bm25-search.js +333 -0
  75. package/src/lib/browser-automation.js +352 -0
  76. package/src/lib/chat-core.js +336 -0
  77. package/src/lib/claude-code-bridge.js +341 -0
  78. package/src/lib/compliance-framework-reporter.js +359 -0
  79. package/src/lib/compliance-manager.js +330 -0
  80. package/src/lib/compression-telemetry.js +333 -0
  81. package/src/lib/content-recommender.js +370 -0
  82. package/src/lib/cowork-cron.js +330 -0
  83. package/src/lib/cowork-learning.js +333 -0
  84. package/src/lib/cowork-task-runner.js +362 -0
  85. package/src/lib/cowork-workflow.js +327 -0
  86. package/src/lib/cross-chain.js +365 -0
  87. package/src/lib/dao-governance.js +339 -0
  88. package/src/lib/dlp-engine.js +343 -0
  89. package/src/lib/evolution-system.js +336 -0
  90. package/src/lib/evomap-manager.js +339 -0
  91. package/src/lib/execution-backend.js +351 -0
  92. package/src/lib/feature-flags.js +330 -0
  93. package/src/lib/git-integration.js +343 -0
  94. package/src/lib/hardening-manager.js +341 -0
  95. package/src/lib/hierarchical-memory.js +341 -0
  96. package/src/lib/inference-network.js +362 -0
  97. package/src/lib/iteration-budget.js +357 -0
  98. package/src/lib/knowledge-graph.js +333 -0
  99. package/src/lib/matrix-bridge.js +339 -0
  100. package/src/lib/mcp-scaffold.js +345 -0
  101. package/src/lib/memory-injection.js +320 -0
  102. package/src/lib/nostr-bridge.js +342 -0
  103. package/src/lib/orchestrator.js +350 -0
  104. package/src/lib/pdf-parser.js +330 -0
  105. package/src/lib/perf-tuning.js +364 -0
  106. package/src/lib/permission-engine.js +319 -0
  107. package/src/lib/pipeline-orchestrator.js +345 -0
  108. package/src/lib/plan-mode.js +328 -0
  109. package/src/lib/privacy-computing.js +335 -0
  110. package/src/lib/prompt-compressor.js +1 -10
  111. package/src/lib/reputation-optimizer.js +340 -0
  112. package/src/lib/sandbox-v2.js +327 -0
  113. package/src/lib/service-container.js +342 -0
  114. package/src/lib/session-consolidator.js +352 -0
  115. package/src/lib/session-hooks.js +340 -0
  116. package/src/lib/session-search.js +334 -0
  117. package/src/lib/session-tail.js +320 -0
  118. package/src/lib/session-usage.js +329 -0
  119. package/src/lib/siem-exporter.js +352 -0
  120. package/src/lib/skill-marketplace.js +345 -0
  121. package/src/lib/sla-manager.js +341 -0
  122. package/src/lib/slot-filler.js +333 -0
  123. package/src/lib/social-graph-analytics.js +327 -0
  124. package/src/lib/social-graph.js +304 -0
  125. package/src/lib/stress-tester.js +342 -0
  126. package/src/lib/sub-agent-registry.js +359 -0
  127. package/src/lib/task-model-selector.js +333 -0
  128. package/src/lib/terraform-manager.js +333 -0
  129. package/src/lib/todo-manager.js +339 -0
  130. package/src/lib/token-incentive.js +341 -0
  131. package/src/lib/topic-classifier.js +353 -0
  132. package/src/lib/user-profile.js +325 -0
  133. package/src/lib/version-checker.js +335 -0
  134. package/src/lib/web-fetch.js +322 -0
  135. package/src/lib/zkp-engine.js +342 -0
@@ -0,0 +1,150 @@
1
+ /**
2
+ * `cc webfetch` — Web Fetch V2 governance overlay (in-memory, atop lib/web-fetch.js).
3
+ */
4
+ import {
5
+ WFET_TARGET_MATURITY_V2,
6
+ WFET_JOB_LIFECYCLE_V2,
7
+ setMaxActiveWfetTargetsPerOwnerV2,
8
+ getMaxActiveWfetTargetsPerOwnerV2,
9
+ setMaxPendingWfetJobsPerTargetV2,
10
+ getMaxPendingWfetJobsPerTargetV2,
11
+ setWfetTargetIdleMsV2,
12
+ getWfetTargetIdleMsV2,
13
+ setWfetJobStuckMsV2,
14
+ getWfetJobStuckMsV2,
15
+ registerWfetTargetV2,
16
+ activateWfetTargetV2,
17
+ degradeWfetTargetV2,
18
+ retireWfetTargetV2,
19
+ touchWfetTargetV2,
20
+ getWfetTargetV2,
21
+ listWfetTargetsV2,
22
+ createWfetJobV2,
23
+ fetchingWfetJobV2,
24
+ succeedWfetJobV2,
25
+ failWfetJobV2,
26
+ cancelWfetJobV2,
27
+ getWfetJobV2,
28
+ listWfetJobsV2,
29
+ autoDegradeIdleWfetTargetsV2,
30
+ autoFailStuckWfetJobsV2,
31
+ getWebFetchGovStatsV2,
32
+ _resetStateWebFetchV2,
33
+ } from "../lib/web-fetch.js";
34
+
35
+ export function registerWebfetchCommand(program) {
36
+ const wf = program.command("webfetch").description("Web Fetch V2 governance");
37
+ wf.command("enums-v2").action(() =>
38
+ console.log(
39
+ JSON.stringify(
40
+ {
41
+ targetMaturity: WFET_TARGET_MATURITY_V2,
42
+ jobLifecycle: WFET_JOB_LIFECYCLE_V2,
43
+ },
44
+ null,
45
+ 2,
46
+ ),
47
+ ),
48
+ );
49
+ wf.command("config-v2").action(() =>
50
+ console.log(
51
+ JSON.stringify(
52
+ {
53
+ maxActiveWfetTargetsPerOwner: getMaxActiveWfetTargetsPerOwnerV2(),
54
+ maxPendingWfetJobsPerTarget: getMaxPendingWfetJobsPerTargetV2(),
55
+ wfetTargetIdleMs: getWfetTargetIdleMsV2(),
56
+ wfetJobStuckMs: getWfetJobStuckMsV2(),
57
+ },
58
+ null,
59
+ 2,
60
+ ),
61
+ ),
62
+ );
63
+ wf.command("set-max-active-v2 <n>").action((n) => {
64
+ setMaxActiveWfetTargetsPerOwnerV2(Number(n));
65
+ console.log("ok");
66
+ });
67
+ wf.command("set-max-pending-v2 <n>").action((n) => {
68
+ setMaxPendingWfetJobsPerTargetV2(Number(n));
69
+ console.log("ok");
70
+ });
71
+ wf.command("set-idle-ms-v2 <n>").action((n) => {
72
+ setWfetTargetIdleMsV2(Number(n));
73
+ console.log("ok");
74
+ });
75
+ wf.command("set-stuck-ms-v2 <n>").action((n) => {
76
+ setWfetJobStuckMsV2(Number(n));
77
+ console.log("ok");
78
+ });
79
+ wf.command("register-target-v2 <id> <owner>")
80
+ .option("--baseUrl <u>", "baseUrl")
81
+ .action((id, owner, o) =>
82
+ console.log(
83
+ JSON.stringify(
84
+ registerWfetTargetV2({ id, owner, baseUrl: o.baseUrl }),
85
+ null,
86
+ 2,
87
+ ),
88
+ ),
89
+ );
90
+ wf.command("activate-target-v2 <id>").action((id) =>
91
+ console.log(JSON.stringify(activateWfetTargetV2(id), null, 2)),
92
+ );
93
+ wf.command("degrade-target-v2 <id>").action((id) =>
94
+ console.log(JSON.stringify(degradeWfetTargetV2(id), null, 2)),
95
+ );
96
+ wf.command("retire-target-v2 <id>").action((id) =>
97
+ console.log(JSON.stringify(retireWfetTargetV2(id), null, 2)),
98
+ );
99
+ wf.command("touch-target-v2 <id>").action((id) =>
100
+ console.log(JSON.stringify(touchWfetTargetV2(id), null, 2)),
101
+ );
102
+ wf.command("get-target-v2 <id>").action((id) =>
103
+ console.log(JSON.stringify(getWfetTargetV2(id), null, 2)),
104
+ );
105
+ wf.command("list-targets-v2").action(() =>
106
+ console.log(JSON.stringify(listWfetTargetsV2(), null, 2)),
107
+ );
108
+ wf.command("create-job-v2 <id> <targetId>")
109
+ .option("--kind <k>", "kind", "GET")
110
+ .action((id, targetId, o) =>
111
+ console.log(
112
+ JSON.stringify(
113
+ createWfetJobV2({ id, targetId, kind: o.kind }),
114
+ null,
115
+ 2,
116
+ ),
117
+ ),
118
+ );
119
+ wf.command("fetching-job-v2 <id>").action((id) =>
120
+ console.log(JSON.stringify(fetchingWfetJobV2(id), null, 2)),
121
+ );
122
+ wf.command("succeed-job-v2 <id>").action((id) =>
123
+ console.log(JSON.stringify(succeedWfetJobV2(id), null, 2)),
124
+ );
125
+ wf.command("fail-job-v2 <id> [reason]").action((id, reason) =>
126
+ console.log(JSON.stringify(failWfetJobV2(id, reason), null, 2)),
127
+ );
128
+ wf.command("cancel-job-v2 <id> [reason]").action((id, reason) =>
129
+ console.log(JSON.stringify(cancelWfetJobV2(id, reason), null, 2)),
130
+ );
131
+ wf.command("get-job-v2 <id>").action((id) =>
132
+ console.log(JSON.stringify(getWfetJobV2(id), null, 2)),
133
+ );
134
+ wf.command("list-jobs-v2").action(() =>
135
+ console.log(JSON.stringify(listWfetJobsV2(), null, 2)),
136
+ );
137
+ wf.command("auto-degrade-idle-v2").action(() =>
138
+ console.log(JSON.stringify(autoDegradeIdleWfetTargetsV2(), null, 2)),
139
+ );
140
+ wf.command("auto-fail-stuck-v2").action(() =>
141
+ console.log(JSON.stringify(autoFailStuckWfetJobsV2(), null, 2)),
142
+ );
143
+ wf.command("gov-stats-v2").action(() =>
144
+ console.log(JSON.stringify(getWebFetchGovStatsV2(), null, 2)),
145
+ );
146
+ wf.command("reset-state-v2").action(() => {
147
+ _resetStateWebFetchV2();
148
+ console.log(JSON.stringify({ ok: true }, null, 2));
149
+ });
150
+ }
@@ -835,4 +835,222 @@ export function registerZkpCommand(program) {
835
835
  process.exit(1);
836
836
  }
837
837
  });
838
+ registerZkpV2Command(zkp);
839
+ }
840
+
841
+ import {
842
+ ZKP_CIRCUIT_MATURITY_V2,
843
+ ZKP_PROOF_LIFECYCLE_V2,
844
+ registerZkpCircuitV2,
845
+ activateZkpCircuitV2,
846
+ deprecateZkpCircuitV2,
847
+ archiveZkpCircuitV2,
848
+ touchZkpCircuitV2,
849
+ getZkpCircuitV2,
850
+ listZkpCircuitsV2,
851
+ createZkpProofV2,
852
+ startZkpProofV2,
853
+ verifyZkpProofV2,
854
+ failZkpProofV2,
855
+ cancelZkpProofV2,
856
+ getZkpProofV2,
857
+ listZkpProofsV2,
858
+ setMaxActiveZkpCircuitsPerOwnerV2,
859
+ getMaxActiveZkpCircuitsPerOwnerV2,
860
+ setMaxPendingZkpProofsPerCircuitV2,
861
+ getMaxPendingZkpProofsPerCircuitV2,
862
+ setZkpCircuitIdleMsV2,
863
+ getZkpCircuitIdleMsV2,
864
+ setZkpProofStuckMsV2,
865
+ getZkpProofStuckMsV2,
866
+ autoDeprecateIdleZkpCircuitsV2,
867
+ autoFailStuckZkpProofsV2,
868
+ getZkpEngineGovStatsV2,
869
+ } from "../lib/zkp-engine.js";
870
+
871
+ export function registerZkpV2Command(zkp) {
872
+ zkp
873
+ .command("enums-v2")
874
+ .description("Show V2 governance enums")
875
+ .action(() => {
876
+ console.log(
877
+ JSON.stringify(
878
+ { ZKP_CIRCUIT_MATURITY_V2, ZKP_PROOF_LIFECYCLE_V2 },
879
+ null,
880
+ 2,
881
+ ),
882
+ );
883
+ });
884
+ zkp
885
+ .command("register-circuit-v2")
886
+ .description("Register a zkp circuit profile (pending)")
887
+ .requiredOption("--id <id>")
888
+ .requiredOption("--owner <owner>")
889
+ .option("--scheme <scheme>")
890
+ .action((o) => {
891
+ console.log(
892
+ JSON.stringify(
893
+ registerZkpCircuitV2({ id: o.id, owner: o.owner, scheme: o.scheme }),
894
+ null,
895
+ 2,
896
+ ),
897
+ );
898
+ });
899
+ zkp
900
+ .command("activate-circuit-v2 <id>")
901
+ .description("Activate circuit")
902
+ .action((id) => {
903
+ console.log(JSON.stringify(activateZkpCircuitV2(id), null, 2));
904
+ });
905
+ zkp
906
+ .command("deprecate-circuit-v2 <id>")
907
+ .description("Deprecate circuit")
908
+ .action((id) => {
909
+ console.log(JSON.stringify(deprecateZkpCircuitV2(id), null, 2));
910
+ });
911
+ zkp
912
+ .command("archive-circuit-v2 <id>")
913
+ .description("Archive circuit (terminal)")
914
+ .action((id) => {
915
+ console.log(JSON.stringify(archiveZkpCircuitV2(id), null, 2));
916
+ });
917
+ zkp
918
+ .command("touch-circuit-v2 <id>")
919
+ .description("Refresh lastTouchedAt")
920
+ .action((id) => {
921
+ console.log(JSON.stringify(touchZkpCircuitV2(id), null, 2));
922
+ });
923
+ zkp
924
+ .command("get-circuit-v2 <id>")
925
+ .description("Get circuit")
926
+ .action((id) => {
927
+ console.log(JSON.stringify(getZkpCircuitV2(id), null, 2));
928
+ });
929
+ zkp
930
+ .command("list-circuits-v2")
931
+ .description("List circuits")
932
+ .action(() => {
933
+ console.log(JSON.stringify(listZkpCircuitsV2(), null, 2));
934
+ });
935
+ zkp
936
+ .command("create-proof-v2")
937
+ .description("Create a zkp proof (queued)")
938
+ .requiredOption("--id <id>")
939
+ .requiredOption("--circuit-id <circuitId>")
940
+ .option("--inputs <inputs>")
941
+ .action((o) => {
942
+ console.log(
943
+ JSON.stringify(
944
+ createZkpProofV2({
945
+ id: o.id,
946
+ circuitId: o.circuitId,
947
+ inputs: o.inputs,
948
+ }),
949
+ null,
950
+ 2,
951
+ ),
952
+ );
953
+ });
954
+ zkp
955
+ .command("start-proof-v2 <id>")
956
+ .description("Transition proof to proving")
957
+ .action((id) => {
958
+ console.log(JSON.stringify(startZkpProofV2(id), null, 2));
959
+ });
960
+ zkp
961
+ .command("verify-proof-v2 <id>")
962
+ .description("Transition proof to verified")
963
+ .action((id) => {
964
+ console.log(JSON.stringify(verifyZkpProofV2(id), null, 2));
965
+ });
966
+ zkp
967
+ .command("fail-proof-v2 <id>")
968
+ .description("Fail proof")
969
+ .option("--reason <r>")
970
+ .action((id, o) => {
971
+ console.log(JSON.stringify(failZkpProofV2(id, o.reason), null, 2));
972
+ });
973
+ zkp
974
+ .command("cancel-proof-v2 <id>")
975
+ .description("Cancel proof")
976
+ .option("--reason <r>")
977
+ .action((id, o) => {
978
+ console.log(JSON.stringify(cancelZkpProofV2(id, o.reason), null, 2));
979
+ });
980
+ zkp
981
+ .command("get-proof-v2 <id>")
982
+ .description("Get proof")
983
+ .action((id) => {
984
+ console.log(JSON.stringify(getZkpProofV2(id), null, 2));
985
+ });
986
+ zkp
987
+ .command("list-proofs-v2")
988
+ .description("List proofs")
989
+ .action(() => {
990
+ console.log(JSON.stringify(listZkpProofsV2(), null, 2));
991
+ });
992
+ zkp
993
+ .command("set-max-active-circuits-v2 <n>")
994
+ .description("Set per-owner active cap")
995
+ .action((n) => {
996
+ setMaxActiveZkpCircuitsPerOwnerV2(Number(n));
997
+ console.log(
998
+ JSON.stringify(
999
+ { maxActiveZkpCircuitsPerOwner: getMaxActiveZkpCircuitsPerOwnerV2() },
1000
+ null,
1001
+ 2,
1002
+ ),
1003
+ );
1004
+ });
1005
+ zkp
1006
+ .command("set-max-pending-proofs-v2 <n>")
1007
+ .description("Set per-circuit pending cap")
1008
+ .action((n) => {
1009
+ setMaxPendingZkpProofsPerCircuitV2(Number(n));
1010
+ console.log(
1011
+ JSON.stringify(
1012
+ {
1013
+ maxPendingZkpProofsPerCircuit: getMaxPendingZkpProofsPerCircuitV2(),
1014
+ },
1015
+ null,
1016
+ 2,
1017
+ ),
1018
+ );
1019
+ });
1020
+ zkp
1021
+ .command("set-circuit-idle-ms-v2 <n>")
1022
+ .description("Set idle threshold")
1023
+ .action((n) => {
1024
+ setZkpCircuitIdleMsV2(Number(n));
1025
+ console.log(
1026
+ JSON.stringify({ zkpCircuitIdleMs: getZkpCircuitIdleMsV2() }, null, 2),
1027
+ );
1028
+ });
1029
+ zkp
1030
+ .command("set-proof-stuck-ms-v2 <n>")
1031
+ .description("Set stuck threshold")
1032
+ .action((n) => {
1033
+ setZkpProofStuckMsV2(Number(n));
1034
+ console.log(
1035
+ JSON.stringify({ zkpProofStuckMs: getZkpProofStuckMsV2() }, null, 2),
1036
+ );
1037
+ });
1038
+ zkp
1039
+ .command("auto-deprecate-idle-circuits-v2")
1040
+ .description("Auto-deprecate idle circuits")
1041
+ .action(() => {
1042
+ console.log(JSON.stringify(autoDeprecateIdleZkpCircuitsV2(), null, 2));
1043
+ });
1044
+ zkp
1045
+ .command("auto-fail-stuck-proofs-v2")
1046
+ .description("Auto-fail stuck proving proofs")
1047
+ .action(() => {
1048
+ console.log(JSON.stringify(autoFailStuckZkpProofsV2(), null, 2));
1049
+ });
1050
+ zkp
1051
+ .command("gov-stats-v2")
1052
+ .description("V2 governance aggregate stats")
1053
+ .action(() => {
1054
+ console.log(JSON.stringify(getZkpEngineGovStatsV2(), null, 2));
1055
+ });
838
1056
  }
@@ -414,3 +414,334 @@ export class PromptCompressor {
414
414
  return result;
415
415
  }
416
416
  }
417
+
418
+
419
+ // =====================================================================
420
+ // Prompt Compressor V2 governance overlay
421
+ // =====================================================================
422
+ export const PCOMP_PROFILE_MATURITY_V2 = Object.freeze({
423
+ PENDING: "pending",
424
+ ACTIVE: "active",
425
+ STALE: "stale",
426
+ ARCHIVED: "archived",
427
+ });
428
+ export const PCOMP_RUN_LIFECYCLE_V2 = Object.freeze({
429
+ QUEUED: "queued",
430
+ COMPRESSING: "compressing",
431
+ COMPRESSED: "compressed",
432
+ FAILED: "failed",
433
+ CANCELLED: "cancelled",
434
+ });
435
+ const _pcompPTrans = new Map([
436
+ [
437
+ PCOMP_PROFILE_MATURITY_V2.PENDING,
438
+ new Set([
439
+ PCOMP_PROFILE_MATURITY_V2.ACTIVE,
440
+ PCOMP_PROFILE_MATURITY_V2.ARCHIVED,
441
+ ]),
442
+ ],
443
+ [
444
+ PCOMP_PROFILE_MATURITY_V2.ACTIVE,
445
+ new Set([
446
+ PCOMP_PROFILE_MATURITY_V2.STALE,
447
+ PCOMP_PROFILE_MATURITY_V2.ARCHIVED,
448
+ ]),
449
+ ],
450
+ [
451
+ PCOMP_PROFILE_MATURITY_V2.STALE,
452
+ new Set([
453
+ PCOMP_PROFILE_MATURITY_V2.ACTIVE,
454
+ PCOMP_PROFILE_MATURITY_V2.ARCHIVED,
455
+ ]),
456
+ ],
457
+ [PCOMP_PROFILE_MATURITY_V2.ARCHIVED, new Set()],
458
+ ]);
459
+ const _pcompPTerminal = new Set([PCOMP_PROFILE_MATURITY_V2.ARCHIVED]);
460
+ const _pcompRTrans = new Map([
461
+ [
462
+ PCOMP_RUN_LIFECYCLE_V2.QUEUED,
463
+ new Set([
464
+ PCOMP_RUN_LIFECYCLE_V2.COMPRESSING,
465
+ PCOMP_RUN_LIFECYCLE_V2.CANCELLED,
466
+ ]),
467
+ ],
468
+ [
469
+ PCOMP_RUN_LIFECYCLE_V2.COMPRESSING,
470
+ new Set([
471
+ PCOMP_RUN_LIFECYCLE_V2.COMPRESSED,
472
+ PCOMP_RUN_LIFECYCLE_V2.FAILED,
473
+ PCOMP_RUN_LIFECYCLE_V2.CANCELLED,
474
+ ]),
475
+ ],
476
+ [PCOMP_RUN_LIFECYCLE_V2.COMPRESSED, new Set()],
477
+ [PCOMP_RUN_LIFECYCLE_V2.FAILED, new Set()],
478
+ [PCOMP_RUN_LIFECYCLE_V2.CANCELLED, new Set()],
479
+ ]);
480
+ const _pcompPsV2 = new Map();
481
+ const _pcompRsV2 = new Map();
482
+ let _pcompMaxActive = 8,
483
+ _pcompMaxPending = 20,
484
+ _pcompIdleMs = 30 * 24 * 60 * 60 * 1000,
485
+ _pcompStuckMs = 60 * 1000;
486
+ function _pcompPos(n, label) {
487
+ const v = Math.floor(Number(n));
488
+ if (!Number.isFinite(v) || v <= 0)
489
+ throw new Error(`${label} must be positive integer`);
490
+ return v;
491
+ }
492
+ function _pcompCheckP(from, to) {
493
+ const a = _pcompPTrans.get(from);
494
+ if (!a || !a.has(to))
495
+ throw new Error(`invalid pcomp profile transition ${from} → ${to}`);
496
+ }
497
+ function _pcompCheckR(from, to) {
498
+ const a = _pcompRTrans.get(from);
499
+ if (!a || !a.has(to))
500
+ throw new Error(`invalid pcomp run transition ${from} → ${to}`);
501
+ }
502
+ function _pcompCountActive(owner) {
503
+ let c = 0;
504
+ for (const p of _pcompPsV2.values())
505
+ if (p.owner === owner && p.status === PCOMP_PROFILE_MATURITY_V2.ACTIVE) c++;
506
+ return c;
507
+ }
508
+ function _pcompCountPending(profileId) {
509
+ let c = 0;
510
+ for (const r of _pcompRsV2.values())
511
+ if (
512
+ r.profileId === profileId &&
513
+ (r.status === PCOMP_RUN_LIFECYCLE_V2.QUEUED ||
514
+ r.status === PCOMP_RUN_LIFECYCLE_V2.COMPRESSING)
515
+ )
516
+ c++;
517
+ return c;
518
+ }
519
+ export function setMaxActivePcompProfilesPerOwnerV2(n) {
520
+ _pcompMaxActive = _pcompPos(n, "maxActivePcompProfilesPerOwner");
521
+ }
522
+ export function getMaxActivePcompProfilesPerOwnerV2() {
523
+ return _pcompMaxActive;
524
+ }
525
+ export function setMaxPendingPcompRunsPerProfileV2(n) {
526
+ _pcompMaxPending = _pcompPos(n, "maxPendingPcompRunsPerProfile");
527
+ }
528
+ export function getMaxPendingPcompRunsPerProfileV2() {
529
+ return _pcompMaxPending;
530
+ }
531
+ export function setPcompProfileIdleMsV2(n) {
532
+ _pcompIdleMs = _pcompPos(n, "pcompProfileIdleMs");
533
+ }
534
+ export function getPcompProfileIdleMsV2() {
535
+ return _pcompIdleMs;
536
+ }
537
+ export function setPcompRunStuckMsV2(n) {
538
+ _pcompStuckMs = _pcompPos(n, "pcompRunStuckMs");
539
+ }
540
+ export function getPcompRunStuckMsV2() {
541
+ return _pcompStuckMs;
542
+ }
543
+ export function _resetStatePromptCompressorV2() {
544
+ _pcompPsV2.clear();
545
+ _pcompRsV2.clear();
546
+ _pcompMaxActive = 8;
547
+ _pcompMaxPending = 20;
548
+ _pcompIdleMs = 30 * 24 * 60 * 60 * 1000;
549
+ _pcompStuckMs = 60 * 1000;
550
+ }
551
+ export function registerPcompProfileV2({ id, owner, variant, metadata } = {}) {
552
+ if (!id || !owner) throw new Error("id and owner required");
553
+ if (_pcompPsV2.has(id)) throw new Error(`pcomp profile ${id} already exists`);
554
+ const now = Date.now();
555
+ const p = {
556
+ id,
557
+ owner,
558
+ variant: variant || "default",
559
+ status: PCOMP_PROFILE_MATURITY_V2.PENDING,
560
+ createdAt: now,
561
+ updatedAt: now,
562
+ lastTouchedAt: now,
563
+ activatedAt: null,
564
+ archivedAt: null,
565
+ metadata: { ...(metadata || {}) },
566
+ };
567
+ _pcompPsV2.set(id, p);
568
+ return { ...p, metadata: { ...p.metadata } };
569
+ }
570
+ export function activatePcompProfileV2(id) {
571
+ const p = _pcompPsV2.get(id);
572
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
573
+ const isInitial = p.status === PCOMP_PROFILE_MATURITY_V2.PENDING;
574
+ _pcompCheckP(p.status, PCOMP_PROFILE_MATURITY_V2.ACTIVE);
575
+ if (isInitial && _pcompCountActive(p.owner) >= _pcompMaxActive)
576
+ throw new Error(`max active pcomp profiles for owner ${p.owner} reached`);
577
+ const now = Date.now();
578
+ p.status = PCOMP_PROFILE_MATURITY_V2.ACTIVE;
579
+ p.updatedAt = now;
580
+ p.lastTouchedAt = now;
581
+ if (!p.activatedAt) p.activatedAt = now;
582
+ return { ...p, metadata: { ...p.metadata } };
583
+ }
584
+ export function stalePcompProfileV2(id) {
585
+ const p = _pcompPsV2.get(id);
586
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
587
+ _pcompCheckP(p.status, PCOMP_PROFILE_MATURITY_V2.STALE);
588
+ p.status = PCOMP_PROFILE_MATURITY_V2.STALE;
589
+ p.updatedAt = Date.now();
590
+ return { ...p, metadata: { ...p.metadata } };
591
+ }
592
+ export function archivePcompProfileV2(id) {
593
+ const p = _pcompPsV2.get(id);
594
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
595
+ _pcompCheckP(p.status, PCOMP_PROFILE_MATURITY_V2.ARCHIVED);
596
+ const now = Date.now();
597
+ p.status = PCOMP_PROFILE_MATURITY_V2.ARCHIVED;
598
+ p.updatedAt = now;
599
+ if (!p.archivedAt) p.archivedAt = now;
600
+ return { ...p, metadata: { ...p.metadata } };
601
+ }
602
+ export function touchPcompProfileV2(id) {
603
+ const p = _pcompPsV2.get(id);
604
+ if (!p) throw new Error(`pcomp profile ${id} not found`);
605
+ if (_pcompPTerminal.has(p.status))
606
+ throw new Error(`cannot touch terminal pcomp profile ${id}`);
607
+ const now = Date.now();
608
+ p.lastTouchedAt = now;
609
+ p.updatedAt = now;
610
+ return { ...p, metadata: { ...p.metadata } };
611
+ }
612
+ export function getPcompProfileV2(id) {
613
+ const p = _pcompPsV2.get(id);
614
+ if (!p) return null;
615
+ return { ...p, metadata: { ...p.metadata } };
616
+ }
617
+ export function listPcompProfilesV2() {
618
+ return [..._pcompPsV2.values()].map((p) => ({
619
+ ...p,
620
+ metadata: { ...p.metadata },
621
+ }));
622
+ }
623
+ export function createPcompRunV2({ id, profileId, input, metadata } = {}) {
624
+ if (!id || !profileId) throw new Error("id and profileId required");
625
+ if (_pcompRsV2.has(id)) throw new Error(`pcomp run ${id} already exists`);
626
+ if (!_pcompPsV2.has(profileId))
627
+ throw new Error(`pcomp profile ${profileId} not found`);
628
+ if (_pcompCountPending(profileId) >= _pcompMaxPending)
629
+ throw new Error(`max pending pcomp runs for profile ${profileId} reached`);
630
+ const now = Date.now();
631
+ const r = {
632
+ id,
633
+ profileId,
634
+ input: input || "",
635
+ status: PCOMP_RUN_LIFECYCLE_V2.QUEUED,
636
+ createdAt: now,
637
+ updatedAt: now,
638
+ startedAt: null,
639
+ settledAt: null,
640
+ metadata: { ...(metadata || {}) },
641
+ };
642
+ _pcompRsV2.set(id, r);
643
+ return { ...r, metadata: { ...r.metadata } };
644
+ }
645
+ export function compressingPcompRunV2(id) {
646
+ const r = _pcompRsV2.get(id);
647
+ if (!r) throw new Error(`pcomp run ${id} not found`);
648
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.COMPRESSING);
649
+ const now = Date.now();
650
+ r.status = PCOMP_RUN_LIFECYCLE_V2.COMPRESSING;
651
+ r.updatedAt = now;
652
+ if (!r.startedAt) r.startedAt = now;
653
+ return { ...r, metadata: { ...r.metadata } };
654
+ }
655
+ export function compressPcompRunV2(id) {
656
+ const r = _pcompRsV2.get(id);
657
+ if (!r) throw new Error(`pcomp run ${id} not found`);
658
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.COMPRESSED);
659
+ const now = Date.now();
660
+ r.status = PCOMP_RUN_LIFECYCLE_V2.COMPRESSED;
661
+ r.updatedAt = now;
662
+ if (!r.settledAt) r.settledAt = now;
663
+ return { ...r, metadata: { ...r.metadata } };
664
+ }
665
+ export function failPcompRunV2(id, reason) {
666
+ const r = _pcompRsV2.get(id);
667
+ if (!r) throw new Error(`pcomp run ${id} not found`);
668
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.FAILED);
669
+ const now = Date.now();
670
+ r.status = PCOMP_RUN_LIFECYCLE_V2.FAILED;
671
+ r.updatedAt = now;
672
+ if (!r.settledAt) r.settledAt = now;
673
+ if (reason) r.metadata.failReason = String(reason);
674
+ return { ...r, metadata: { ...r.metadata } };
675
+ }
676
+ export function cancelPcompRunV2(id, reason) {
677
+ const r = _pcompRsV2.get(id);
678
+ if (!r) throw new Error(`pcomp run ${id} not found`);
679
+ _pcompCheckR(r.status, PCOMP_RUN_LIFECYCLE_V2.CANCELLED);
680
+ const now = Date.now();
681
+ r.status = PCOMP_RUN_LIFECYCLE_V2.CANCELLED;
682
+ r.updatedAt = now;
683
+ if (!r.settledAt) r.settledAt = now;
684
+ if (reason) r.metadata.cancelReason = String(reason);
685
+ return { ...r, metadata: { ...r.metadata } };
686
+ }
687
+ export function getPcompRunV2(id) {
688
+ const r = _pcompRsV2.get(id);
689
+ if (!r) return null;
690
+ return { ...r, metadata: { ...r.metadata } };
691
+ }
692
+ export function listPcompRunsV2() {
693
+ return [..._pcompRsV2.values()].map((r) => ({
694
+ ...r,
695
+ metadata: { ...r.metadata },
696
+ }));
697
+ }
698
+ export function autoStaleIdlePcompProfilesV2({ now } = {}) {
699
+ const t = now ?? Date.now();
700
+ const flipped = [];
701
+ for (const p of _pcompPsV2.values())
702
+ if (
703
+ p.status === PCOMP_PROFILE_MATURITY_V2.ACTIVE &&
704
+ t - p.lastTouchedAt >= _pcompIdleMs
705
+ ) {
706
+ p.status = PCOMP_PROFILE_MATURITY_V2.STALE;
707
+ p.updatedAt = t;
708
+ flipped.push(p.id);
709
+ }
710
+ return { flipped, count: flipped.length };
711
+ }
712
+ export function autoFailStuckPcompRunsV2({ now } = {}) {
713
+ const t = now ?? Date.now();
714
+ const flipped = [];
715
+ for (const r of _pcompRsV2.values())
716
+ if (
717
+ r.status === PCOMP_RUN_LIFECYCLE_V2.COMPRESSING &&
718
+ r.startedAt != null &&
719
+ t - r.startedAt >= _pcompStuckMs
720
+ ) {
721
+ r.status = PCOMP_RUN_LIFECYCLE_V2.FAILED;
722
+ r.updatedAt = t;
723
+ if (!r.settledAt) r.settledAt = t;
724
+ r.metadata.failReason = "auto-fail-stuck";
725
+ flipped.push(r.id);
726
+ }
727
+ return { flipped, count: flipped.length };
728
+ }
729
+ export function getPromptCompressorGovStatsV2() {
730
+ const profilesByStatus = {};
731
+ for (const v of Object.values(PCOMP_PROFILE_MATURITY_V2))
732
+ profilesByStatus[v] = 0;
733
+ for (const p of _pcompPsV2.values()) profilesByStatus[p.status]++;
734
+ const runsByStatus = {};
735
+ for (const v of Object.values(PCOMP_RUN_LIFECYCLE_V2)) runsByStatus[v] = 0;
736
+ for (const r of _pcompRsV2.values()) runsByStatus[r.status]++;
737
+ return {
738
+ totalPcompProfilesV2: _pcompPsV2.size,
739
+ totalPcompRunsV2: _pcompRsV2.size,
740
+ maxActivePcompProfilesPerOwner: _pcompMaxActive,
741
+ maxPendingPcompRunsPerProfile: _pcompMaxPending,
742
+ pcompProfileIdleMs: _pcompIdleMs,
743
+ pcompRunStuckMs: _pcompStuckMs,
744
+ profilesByStatus,
745
+ runsByStatus,
746
+ };
747
+ }