avorelo 0.1.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 (258) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +56 -0
  3. package/bin/avorelo +9 -0
  4. package/package.json +135 -0
  5. package/scripts/README.md +40 -0
  6. package/scripts/cco-dashboard.js +252 -0
  7. package/scripts/cco-status.js +430 -0
  8. package/scripts/lib/activation/account-state.js +37 -0
  9. package/scripts/lib/activation/activation-runner.js +546 -0
  10. package/scripts/lib/activation/activation-self-healing.js +480 -0
  11. package/scripts/lib/activation/activation-state.js +83 -0
  12. package/scripts/lib/activation/activation-summary.js +191 -0
  13. package/scripts/lib/activation/adapters/claude-code.js +77 -0
  14. package/scripts/lib/activation/adapters/codex-cli.js +52 -0
  15. package/scripts/lib/activation/adapters/cursor.js +37 -0
  16. package/scripts/lib/activation/adapters/github-agent.js +39 -0
  17. package/scripts/lib/activation/adapters/terminal.js +42 -0
  18. package/scripts/lib/activation/adapters/vscode.js +39 -0
  19. package/scripts/lib/activation/adapters/windsurf.js +37 -0
  20. package/scripts/lib/activation/ai-surface-detector.js +151 -0
  21. package/scripts/lib/activation/connect-account.js +145 -0
  22. package/scripts/lib/activation/detect-environment.js +75 -0
  23. package/scripts/lib/activation/detect-hosts.js +62 -0
  24. package/scripts/lib/activation/format-activation-output.js +109 -0
  25. package/scripts/lib/activation/next-action.js +43 -0
  26. package/scripts/lib/activation/repair-engine.js +219 -0
  27. package/scripts/lib/activation-distribution-readiness.js +507 -0
  28. package/scripts/lib/adapter-conformance.js +176 -0
  29. package/scripts/lib/adapter-readiness.js +417 -0
  30. package/scripts/lib/adapter-safety-boundaries.js +335 -0
  31. package/scripts/lib/adapter-technical-readiness-gate.js +205 -0
  32. package/scripts/lib/agent-access-governance.js +455 -0
  33. package/scripts/lib/agent-enforcement.js +765 -0
  34. package/scripts/lib/agent-policy-profile.js +210 -0
  35. package/scripts/lib/agent-security/action-evaluator.js +507 -0
  36. package/scripts/lib/agent-security/adapter-registry.js +98 -0
  37. package/scripts/lib/agent-security/auto-policy.js +139 -0
  38. package/scripts/lib/agent-security/bounded-scan.js +93 -0
  39. package/scripts/lib/agent-security/enforcement-adapter.js +174 -0
  40. package/scripts/lib/agent-security/enforcement-engine.js +1129 -0
  41. package/scripts/lib/agent-security/file-write-adapter.js +183 -0
  42. package/scripts/lib/agent-security/file-write-rules.js +178 -0
  43. package/scripts/lib/agent-security/index.js +3342 -0
  44. package/scripts/lib/agent-security/instruction-risk.js +181 -0
  45. package/scripts/lib/agent-security/mcp-action-adapter.js +185 -0
  46. package/scripts/lib/agent-security/mcp-action-rules.js +184 -0
  47. package/scripts/lib/agent-security/package-action-adapter.js +175 -0
  48. package/scripts/lib/agent-security/package-action-rules.js +233 -0
  49. package/scripts/lib/agent-security/performance.js +148 -0
  50. package/scripts/lib/agent-security/permission-minimizer.js +403 -0
  51. package/scripts/lib/agent-security/scan-cache.js +74 -0
  52. package/scripts/lib/agent-security/source-trust.js +146 -0
  53. package/scripts/lib/ai-install-prompt.js +288 -0
  54. package/scripts/lib/ai-workspace-hygiene.js +1499 -0
  55. package/scripts/lib/alpha-activation.js +520 -0
  56. package/scripts/lib/alpha-feedback.js +263 -0
  57. package/scripts/lib/alpha-readiness-gate.js +332 -0
  58. package/scripts/lib/anti-gaming.js +169 -0
  59. package/scripts/lib/artifact-health.js +431 -0
  60. package/scripts/lib/attribution.js +180 -0
  61. package/scripts/lib/audit.js +289 -0
  62. package/scripts/lib/avorelo-skill-registry.js +810 -0
  63. package/scripts/lib/batch-jobs.js +71 -0
  64. package/scripts/lib/brain-pack.js +578 -0
  65. package/scripts/lib/brand-boundary.js +424 -0
  66. package/scripts/lib/brand.js +74 -0
  67. package/scripts/lib/browser-capability.js +1048 -0
  68. package/scripts/lib/browser-proof-preflight.js +321 -0
  69. package/scripts/lib/cache-readiness.js +187 -0
  70. package/scripts/lib/canonical-reentry.js +162 -0
  71. package/scripts/lib/capability-packs.js +314 -0
  72. package/scripts/lib/capability-recommender.js +512 -0
  73. package/scripts/lib/capability-registry.js +1059 -0
  74. package/scripts/lib/carry-forward-surfacing.js +194 -0
  75. package/scripts/lib/ccusage-adapter.js +188 -0
  76. package/scripts/lib/company-loop.js +1149 -0
  77. package/scripts/lib/config.js +637 -0
  78. package/scripts/lib/context-acquisition-plan.js +287 -0
  79. package/scripts/lib/context-budget-guard.js +170 -0
  80. package/scripts/lib/context-budget-scanner.js +257 -0
  81. package/scripts/lib/context-optimizer.js +715 -0
  82. package/scripts/lib/context-reduction-plan.js +178 -0
  83. package/scripts/lib/context-safety.js +88 -0
  84. package/scripts/lib/context-savings-engine.js +158 -0
  85. package/scripts/lib/cost-evidence.js +254 -0
  86. package/scripts/lib/cross-host-install-plan.js +308 -0
  87. package/scripts/lib/cross-host-install-readiness.js +237 -0
  88. package/scripts/lib/cross-host-value-flow.js +268 -0
  89. package/scripts/lib/dashboard.js +900 -0
  90. package/scripts/lib/design-partner-feedback.js +346 -0
  91. package/scripts/lib/entitlements.js +100 -0
  92. package/scripts/lib/execution-packet.js +559 -0
  93. package/scripts/lib/experimentation-events.js +547 -0
  94. package/scripts/lib/external-capability-compliance.js +107 -0
  95. package/scripts/lib/external-user-simulation.js +166 -0
  96. package/scripts/lib/failure-recovery-readiness.js +81 -0
  97. package/scripts/lib/failure-recovery.js +419 -0
  98. package/scripts/lib/feedback-intelligence.js +537 -0
  99. package/scripts/lib/feedback-signals.js +205 -0
  100. package/scripts/lib/file-integrity.js +68 -0
  101. package/scripts/lib/fsx.js +127 -0
  102. package/scripts/lib/full-readiness-gate.js +451 -0
  103. package/scripts/lib/guidance-builder.js +174 -0
  104. package/scripts/lib/hook-apply.js +1019 -0
  105. package/scripts/lib/hook-baseline.js +310 -0
  106. package/scripts/lib/hook-config-preview.js +275 -0
  107. package/scripts/lib/hook-contracts.js +290 -0
  108. package/scripts/lib/hook-safety-boundary-readiness.js +80 -0
  109. package/scripts/lib/host-capability-matrix.js +351 -0
  110. package/scripts/lib/host-support-context.js +254 -0
  111. package/scripts/lib/http-hook-action.js +538 -0
  112. package/scripts/lib/install-ai-readiness.js +84 -0
  113. package/scripts/lib/install-intake-risk.js +1037 -0
  114. package/scripts/lib/install-journey-intelligence.js +329 -0
  115. package/scripts/lib/intervention-guidance.js +57 -0
  116. package/scripts/lib/known-limitations.js +115 -0
  117. package/scripts/lib/l8-path-truth.js +146 -0
  118. package/scripts/lib/launch-hardening-gate.js +436 -0
  119. package/scripts/lib/launch-readiness.js +628 -0
  120. package/scripts/lib/learning-memory.js +686 -0
  121. package/scripts/lib/lifecycle-hooks.js +802 -0
  122. package/scripts/lib/local-package-smoke.js +423 -0
  123. package/scripts/lib/local-pricing.js +299 -0
  124. package/scripts/lib/mcp-enforcement.js +311 -0
  125. package/scripts/lib/mcp-least-privilege-policy.js +303 -0
  126. package/scripts/lib/mcp-tool-inventory.js +388 -0
  127. package/scripts/lib/mcp-tool-risk.js +0 -0
  128. package/scripts/lib/memory.js +335 -0
  129. package/scripts/lib/metrics.js +699 -0
  130. package/scripts/lib/micro-proof.js +133 -0
  131. package/scripts/lib/next-run-context.js +436 -0
  132. package/scripts/lib/operating-value.js +1648 -0
  133. package/scripts/lib/optimization-v3.js +122 -0
  134. package/scripts/lib/orchestration/adapters/_shared.js +49 -0
  135. package/scripts/lib/orchestration/adapters/aider.js +18 -0
  136. package/scripts/lib/orchestration/adapters/claude-code.js +35 -0
  137. package/scripts/lib/orchestration/adapters/codex.js +35 -0
  138. package/scripts/lib/orchestration/adapters/gemini-cli.js +18 -0
  139. package/scripts/lib/orchestration/adapters/git.js +25 -0
  140. package/scripts/lib/orchestration/adapters/index.js +31 -0
  141. package/scripts/lib/orchestration/adapters/lm-studio.js +18 -0
  142. package/scripts/lib/orchestration/adapters/ollama.js +18 -0
  143. package/scripts/lib/orchestration/adapters/opencode.js +18 -0
  144. package/scripts/lib/orchestration/adapters/openrouter.js +18 -0
  145. package/scripts/lib/orchestration/adapters/test-runner.js +25 -0
  146. package/scripts/lib/orchestration/cli.js +438 -0
  147. package/scripts/lib/orchestration/execution-manager.js +279 -0
  148. package/scripts/lib/orchestration/handoff.js +314 -0
  149. package/scripts/lib/orchestration/index.js +456 -0
  150. package/scripts/lib/orchestration/inventory.js +47 -0
  151. package/scripts/lib/orchestration/model-discovery.js +498 -0
  152. package/scripts/lib/orchestration/model-profiler.js +170 -0
  153. package/scripts/lib/orchestration/model-profiles.js +252 -0
  154. package/scripts/lib/orchestration/model-refresh-policy.js +72 -0
  155. package/scripts/lib/orchestration/proof-writer.js +349 -0
  156. package/scripts/lib/orchestration/provider-discovery/aider.js +49 -0
  157. package/scripts/lib/orchestration/provider-discovery/claude-code.js +56 -0
  158. package/scripts/lib/orchestration/provider-discovery/codex.js +49 -0
  159. package/scripts/lib/orchestration/provider-discovery/common.js +186 -0
  160. package/scripts/lib/orchestration/provider-discovery/gemini.js +106 -0
  161. package/scripts/lib/orchestration/provider-discovery/lm-studio.js +118 -0
  162. package/scripts/lib/orchestration/provider-discovery/models-dev.js +12 -0
  163. package/scripts/lib/orchestration/provider-discovery/ollama.js +100 -0
  164. package/scripts/lib/orchestration/provider-discovery/opencode.js +47 -0
  165. package/scripts/lib/orchestration/provider-discovery/openrouter.js +44 -0
  166. package/scripts/lib/orchestration/risk-classifier.js +130 -0
  167. package/scripts/lib/orchestration/routing-policy.js +486 -0
  168. package/scripts/lib/orchestration/settings.js +112 -0
  169. package/scripts/lib/orchestration/state.js +165 -0
  170. package/scripts/lib/orchestration/verification-manager.js +138 -0
  171. package/scripts/lib/output-profiles.js +146 -0
  172. package/scripts/lib/package-content-audit.js +368 -0
  173. package/scripts/lib/package-runtime.js +278 -0
  174. package/scripts/lib/plan-surface.js +53 -0
  175. package/scripts/lib/plans.js +2318 -0
  176. package/scripts/lib/policy-provider.js +27 -0
  177. package/scripts/lib/prelaunch-activation-readiness.js +409 -0
  178. package/scripts/lib/prelaunch-evidence-store.js +816 -0
  179. package/scripts/lib/prelaunch-intelligence.js +869 -0
  180. package/scripts/lib/pricing-experiment.js +118 -0
  181. package/scripts/lib/pro-moment-events.js +77 -0
  182. package/scripts/lib/pro-moment-state.js +227 -0
  183. package/scripts/lib/pro-moments.js +1216 -0
  184. package/scripts/lib/product-learning-events.js +629 -0
  185. package/scripts/lib/project-profile.js +555 -0
  186. package/scripts/lib/prompt-compiler.js +280 -0
  187. package/scripts/lib/prompt-lint.js +32 -0
  188. package/scripts/lib/prompt-suggestions.js +52 -0
  189. package/scripts/lib/proof-canonical.js +398 -0
  190. package/scripts/lib/proof-drilldown.js +383 -0
  191. package/scripts/lib/proof-events.js +342 -0
  192. package/scripts/lib/proof-history.js +243 -0
  193. package/scripts/lib/proof-metrics.js +296 -0
  194. package/scripts/lib/proof-outcome-evidence.js +134 -0
  195. package/scripts/lib/proof-receipt.js +335 -0
  196. package/scripts/lib/proof-record.js +461 -0
  197. package/scripts/lib/public-activation-distribution-gate.js +258 -0
  198. package/scripts/lib/public-cli.js +3891 -0
  199. package/scripts/lib/public-distribution-truth.js +211 -0
  200. package/scripts/lib/public-install-claim-checker.js +294 -0
  201. package/scripts/lib/publish-provenance-readiness.js +283 -0
  202. package/scripts/lib/readiness-delta.js +218 -0
  203. package/scripts/lib/readiness-evidence-closure.js +196 -0
  204. package/scripts/lib/reentry-memory-capture.js +241 -0
  205. package/scripts/lib/reentry-memory-retrieval.js +302 -0
  206. package/scripts/lib/reentry-memory-status.js +146 -0
  207. package/scripts/lib/reentry-memory-store.js +178 -0
  208. package/scripts/lib/reentry-state.js +66 -0
  209. package/scripts/lib/release-candidate-bundle.js +166 -0
  210. package/scripts/lib/remediation.js +81 -0
  211. package/scripts/lib/repo-map.js +391 -0
  212. package/scripts/lib/run-improvements-lifecycle.js +330 -0
  213. package/scripts/lib/run-improvements.js +789 -0
  214. package/scripts/lib/runtime-decision-policy.js +387 -0
  215. package/scripts/lib/safe-path-engine.js +705 -0
  216. package/scripts/lib/safe-run-controller.js +887 -0
  217. package/scripts/lib/score.js +262 -0
  218. package/scripts/lib/seamless-enforcement.js +329 -0
  219. package/scripts/lib/seamless-outcome.js +689 -0
  220. package/scripts/lib/seamless-reality-gate.js +5043 -0
  221. package/scripts/lib/security-risk-classifier.js +511 -0
  222. package/scripts/lib/security-scan.js +384 -0
  223. package/scripts/lib/session-context-optimizer.js +1211 -0
  224. package/scripts/lib/session-timing.js +315 -0
  225. package/scripts/lib/skill-hygiene.js +805 -0
  226. package/scripts/lib/skill-packs.js +161 -0
  227. package/scripts/lib/skills-operating-layer.js +580 -0
  228. package/scripts/lib/smart-work-routing.js +768 -0
  229. package/scripts/lib/source-catalog.js +700 -0
  230. package/scripts/lib/status-value-summary.js +32 -0
  231. package/scripts/lib/support-bundle.js +578 -0
  232. package/scripts/lib/task-continuation.js +440 -0
  233. package/scripts/lib/test-helpers.js +15 -0
  234. package/scripts/lib/tier.js +38 -0
  235. package/scripts/lib/token-context-quality-gate.js +370 -0
  236. package/scripts/lib/token-cost-capture.js +187 -0
  237. package/scripts/lib/token-cost-intelligence.js +358 -0
  238. package/scripts/lib/token-efficiency-evidence.js +213 -0
  239. package/scripts/lib/token-evidence.js +699 -0
  240. package/scripts/lib/tokenish.js +17 -0
  241. package/scripts/lib/tool-output-sandbox.js +304 -0
  242. package/scripts/lib/trust-audit.js +136 -0
  243. package/scripts/lib/unified-events.js +396 -0
  244. package/scripts/lib/upgrade-interruption-recovery.js +407 -0
  245. package/scripts/lib/usage-ledger.js +201 -0
  246. package/scripts/lib/value-ledger.js +130 -0
  247. package/scripts/lib/value-proof-calibration.js +531 -0
  248. package/scripts/lib/visual-qa.js +231 -0
  249. package/scripts/lib/voice-alpha.js +29 -0
  250. package/scripts/lib/work-aware-orchestration.js +976 -0
  251. package/scripts/lib/work-control-receipts.js +577 -0
  252. package/scripts/lib/work-ledger.js +1123 -0
  253. package/scripts/lib/work-panel-preview.js +352 -0
  254. package/scripts/lib/workflow-discipline.js +280 -0
  255. package/scripts/lib/workflow-signals.js +419 -0
  256. package/scripts/lib/workspace-map.js +281 -0
  257. package/scripts/lib/workspace-registry.js +1367 -0
  258. package/scripts/lib/workspace-resolver.js +480 -0
@@ -0,0 +1,715 @@
1
+ "use strict";
2
+
3
+ // ── Prompt / Context / Token Optimization Foundation ─────────────────────────
4
+ //
5
+ // Builds stage-specific context packs for Avorelo's seamless orchestration.
6
+ // Each pack is compact, deterministic, local-only, and safety-bounded.
7
+ //
8
+ // Contract: avorelo.contextOptimization.v1
9
+ //
10
+ // Non-goals: generic prompt optimizer, RAG, vector search, guaranteed savings,
11
+ // full cost analytics, external API calls, LLM calls.
12
+
13
+ const fs = require("fs");
14
+ const path = require("path");
15
+ const crypto = require("crypto");
16
+ const { ensureCcoDirs, nowIso } = require("./fsx");
17
+
18
+ function safeWriteJson(absPath, obj) {
19
+ fs.mkdirSync(path.dirname(absPath), { recursive: true });
20
+ fs.writeFileSync(absPath, JSON.stringify(obj, null, 2), "utf8");
21
+ }
22
+
23
+ function safeReadJson(absPath, fallback) {
24
+ try {
25
+ return JSON.parse(fs.readFileSync(absPath, "utf8").replace(/^/, ""));
26
+ } catch {
27
+ return fallback !== undefined ? fallback : null;
28
+ }
29
+ }
30
+ const { appendProductLearningEvent } = require("./product-learning-events");
31
+
32
+ const SCHEMA_VERSION = 1;
33
+ const CONTRACT = "avorelo.contextOptimization.v1";
34
+
35
+ const CONTEXT_OPT_DIR_REL = ".claude/cco/orchestration/context-optimizer";
36
+ const LATEST_PACK_REL = ".claude/cco/orchestration/context-optimizer/latest-pack.json";
37
+ const LATEST_RECEIPT_REL = ".claude/cco/orchestration/context-optimizer/latest-receipt.json";
38
+
39
+ // ── Token estimation ──────────────────────────────────────────────────────────
40
+
41
+ const GPT_TOKENIZER_VENDOR_PATH = path.resolve(
42
+ __dirname, "../vendor/node_modules/gpt-tokenizer/cjs/main.js"
43
+ );
44
+
45
+ let _tokenizer = null;
46
+ function tryLoadTokenizer() {
47
+ if (_tokenizer !== null) return _tokenizer;
48
+ try {
49
+ if (fs.existsSync(GPT_TOKENIZER_VENDOR_PATH)) {
50
+ const mod = require(GPT_TOKENIZER_VENDOR_PATH);
51
+ _tokenizer = mod;
52
+ return _tokenizer;
53
+ }
54
+ } catch {}
55
+ _tokenizer = false;
56
+ return false;
57
+ }
58
+
59
+ function estimateTokens(text) {
60
+ if (!text) return { estimatedTokens: 0, estimatedChars: 0, estimateMethod: "heuristic" };
61
+ const s = String(text);
62
+ const chars = s.length;
63
+ const tok = tryLoadTokenizer();
64
+ if (tok) {
65
+ try {
66
+ const encode = tok.encode || (tok.default && tok.default.encode);
67
+ if (typeof encode === "function") {
68
+ const tokens = encode(s).length;
69
+ return { estimatedTokens: tokens, estimatedChars: chars, estimateMethod: "local_tokenizer" };
70
+ }
71
+ } catch {}
72
+ }
73
+ return {
74
+ estimatedTokens: Math.ceil(chars / 4),
75
+ estimatedChars: chars,
76
+ estimateMethod: "heuristic",
77
+ };
78
+ }
79
+
80
+ function tokenWarning(estimatedTokens, maxTokens) {
81
+ if (!maxTokens) return "unknown";
82
+ const ratio = estimatedTokens / maxTokens;
83
+ if (ratio <= 0.7) return "within_budget";
84
+ if (ratio <= 1.0) return "near_budget";
85
+ return "over_budget";
86
+ }
87
+
88
+ // ── Stage budget map ──────────────────────────────────────────────────────────
89
+ // Conservative token caps. Local models get the small cap regardless of stage.
90
+
91
+ const STAGE_BUDGETS = {
92
+ inspect: { budgetClass: "small", maxInputTokens: 4000 },
93
+ summarize: { budgetClass: "small", maxInputTokens: 4000 },
94
+ plan: { budgetClass: "medium", maxInputTokens: 8000 },
95
+ implement: { budgetClass: "large", maxInputTokens: 16000 },
96
+ verify: { budgetClass: "small", maxInputTokens: 4000 },
97
+ security_review:{ budgetClass: "medium", maxInputTokens: 8000 },
98
+ review: { budgetClass: "medium", maxInputTokens: 8000 },
99
+ handoff: { budgetClass: "small", maxInputTokens: 4000 },
100
+ final_review: { budgetClass: "medium", maxInputTokens: 8000 },
101
+ };
102
+
103
+ const LOCAL_WORKER_IDS = new Set(["lm_studio", "ollama"]);
104
+
105
+ function getStageBudget(stageId, workerToolId) {
106
+ const base = STAGE_BUDGETS[stageId] || { budgetClass: "medium", maxInputTokens: 8000 };
107
+ const isLocal = LOCAL_WORKER_IDS.has(workerToolId);
108
+ if (isLocal) {
109
+ return { budgetClass: "small", maxInputTokens: 4000, localWorkerCapped: true };
110
+ }
111
+ return { ...base, localWorkerCapped: false };
112
+ }
113
+
114
+ // ── Safety boundary helpers ───────────────────────────────────────────────────
115
+
116
+ const SECRET_PATTERN = /(?:sk-[A-Za-z0-9_-]{16,}|ghp_[A-Za-z0-9]{20,}|AKIA[0-9A-Z]{16}|(?:api[_ -]?key|token|password|secret|authorization)\s*[:=]\s*\S+)/i;
117
+
118
+ function containsSecret(text) {
119
+ return SECRET_PATTERN.test(String(text || ""));
120
+ }
121
+
122
+ function redactIfSecret(text) {
123
+ if (!text) return text;
124
+ return containsSecret(text) ? "[REDACTED]" : String(text);
125
+ }
126
+
127
+ const EXCLUDED_PATH_PATTERNS = [
128
+ /node_modules/,
129
+ /\.git[\/\\]/,
130
+ /\.env(\.|$)/,
131
+ /dist[\/\\]/,
132
+ /build[\/\\]/,
133
+ /coverage[\/\\]/,
134
+ /\.next[\/\\]/,
135
+ /\.claude[\/\\]cco[\/\\]events/,
136
+ ];
137
+
138
+ function shouldExcludePath(filePath) {
139
+ const normalized = String(filePath || "").replace(/\\/g, "/");
140
+ return EXCLUDED_PATH_PATTERNS.some((pat) => pat.test(normalized));
141
+ }
142
+
143
+ // ── Context item classification ───────────────────────────────────────────────
144
+
145
+ function buildSafetyBoundaries(stage) {
146
+ const isSecurityStage = stage === "security_review";
147
+ return {
148
+ noSecrets: true,
149
+ noRawPrompt: true,
150
+ noRawThirdPartySkillBodies: true,
151
+ localOnly: false,
152
+ approvalRequired: isSecurityStage,
153
+ };
154
+ }
155
+
156
+ // ── Context selection logic ───────────────────────────────────────────────────
157
+
158
+ function selectContextItems(options, budget) {
159
+ const {
160
+ changedFiles = [],
161
+ relevantFiles = [],
162
+ skillSummaries = [],
163
+ safePathConstraints = [],
164
+ intakeWarnings = [],
165
+ governanceSummary = null,
166
+ testCommands = [],
167
+ proofSummary = null,
168
+ taskSummary = "",
169
+ } = options;
170
+
171
+ const included = [];
172
+ const excluded = [];
173
+ let usedTokens = 0;
174
+ const maxTokens = budget.maxInputTokens;
175
+
176
+ // 1. Task summary (always included, compact)
177
+ if (taskSummary) {
178
+ const est = estimateTokens(taskSummary.slice(0, 400));
179
+ included.push({ type: "task_summary", ref: "task", tokenEstimate: est.estimatedTokens });
180
+ usedTokens += est.estimatedTokens;
181
+ }
182
+
183
+ // 2. Changed files (compact list, not full contents)
184
+ const safeChangedFiles = changedFiles
185
+ .map((f) => (typeof f === "string" ? f : f.path || f.file || String(f)))
186
+ .filter((f) => !shouldExcludePath(f) && !containsSecret(f));
187
+
188
+ if (safeChangedFiles.length) {
189
+ const snippet = safeChangedFiles.slice(0, 20).join("\n");
190
+ const est = estimateTokens(snippet);
191
+ if (usedTokens + est.estimatedTokens <= maxTokens) {
192
+ included.push({ type: "changed_files", count: safeChangedFiles.length, tokenEstimate: est.estimatedTokens });
193
+ usedTokens += est.estimatedTokens;
194
+ } else {
195
+ excluded.push({ reason: "over_budget", itemType: "changed_files", itemRef: `${safeChangedFiles.length} files` });
196
+ }
197
+ }
198
+
199
+ // 3. Relevant file paths (not full contents — summaries/paths only)
200
+ const safeRelevantFiles = relevantFiles
201
+ .map((f) => (typeof f === "string" ? f : f.path || String(f)))
202
+ .filter((f) => !shouldExcludePath(f) && !containsSecret(f));
203
+
204
+ if (safeRelevantFiles.length) {
205
+ const snippet = safeRelevantFiles.slice(0, 15).join("\n");
206
+ const est = estimateTokens(snippet);
207
+ if (usedTokens + est.estimatedTokens <= maxTokens) {
208
+ included.push({ type: "relevant_files", count: safeRelevantFiles.length, tokenEstimate: est.estimatedTokens });
209
+ usedTokens += est.estimatedTokens;
210
+ } else {
211
+ excluded.push({ reason: "over_budget", itemType: "relevant_files", itemRef: `${safeRelevantFiles.length} files` });
212
+ }
213
+ }
214
+
215
+ // 4. Skill summaries (lazy — only selected summaries, no bodies)
216
+ const safeSkillSummaries = skillSummaries
217
+ .filter((s) => !containsSecret(String(s.summary || s.id || "")))
218
+ .slice(0, 5);
219
+
220
+ if (safeSkillSummaries.length) {
221
+ const snippet = safeSkillSummaries.map((s) => `${s.id || "skill"}: ${(s.summary || "").slice(0, 80)}`).join("\n");
222
+ const est = estimateTokens(snippet);
223
+ if (usedTokens + est.estimatedTokens <= maxTokens) {
224
+ included.push({ type: "skill_summaries", count: safeSkillSummaries.length, tokenEstimate: est.estimatedTokens });
225
+ usedTokens += est.estimatedTokens;
226
+ } else {
227
+ excluded.push({ reason: "over_budget", itemType: "skill_summaries", itemRef: `${safeSkillSummaries.length} skills` });
228
+ }
229
+ }
230
+
231
+ // 5. Safe path / intake / governance constraints (always included if present)
232
+ if (safePathConstraints.length || intakeWarnings.length || governanceSummary) {
233
+ const snippets = [
234
+ ...safePathConstraints.slice(0, 3).map((c) => redactIfSecret(c)),
235
+ ...intakeWarnings.slice(0, 3).map((w) => redactIfSecret(w)),
236
+ governanceSummary ? redactIfSecret(String(governanceSummary).slice(0, 200)) : null,
237
+ ].filter(Boolean);
238
+ const snippet = snippets.join("\n");
239
+ const est = estimateTokens(snippet);
240
+ if (usedTokens + est.estimatedTokens <= maxTokens) {
241
+ included.push({ type: "constraints", count: snippets.length, tokenEstimate: est.estimatedTokens });
242
+ usedTokens += est.estimatedTokens;
243
+ } else {
244
+ excluded.push({ reason: "over_budget", itemType: "constraints", itemRef: "safe_path/intake/governance" });
245
+ }
246
+ }
247
+
248
+ // 6. Test commands (small, always useful for verify/implement stages)
249
+ if (testCommands.length) {
250
+ const snippet = testCommands.slice(0, 5).join("\n");
251
+ const est = estimateTokens(snippet);
252
+ if (usedTokens + est.estimatedTokens <= maxTokens) {
253
+ included.push({ type: "test_commands", count: testCommands.length, tokenEstimate: est.estimatedTokens });
254
+ usedTokens += est.estimatedTokens;
255
+ } else {
256
+ excluded.push({ reason: "over_budget", itemType: "test_commands", itemRef: `${testCommands.length} commands` });
257
+ }
258
+ }
259
+
260
+ // 7. Proof summary reference (path + compact summary, not full payload)
261
+ if (proofSummary) {
262
+ const snippet = String(proofSummary).slice(0, 300);
263
+ const est = estimateTokens(snippet);
264
+ if (usedTokens + est.estimatedTokens <= maxTokens) {
265
+ included.push({ type: "proof_summary", tokenEstimate: est.estimatedTokens });
266
+ usedTokens += est.estimatedTokens;
267
+ } else {
268
+ excluded.push({ reason: "over_budget", itemType: "proof_summary", itemRef: "proof" });
269
+ }
270
+ }
271
+
272
+ const compactionUsed = usedTokens < (estimateTokens([
273
+ taskSummary,
274
+ ...safeChangedFiles,
275
+ ...safeRelevantFiles,
276
+ ...safeSkillSummaries.map((s) => s.summary || ""),
277
+ ...safePathConstraints,
278
+ ].join("\n")).estimatedTokens);
279
+
280
+ return {
281
+ included,
282
+ excluded,
283
+ estimatedTokens: usedTokens,
284
+ compactionUsed,
285
+ };
286
+ }
287
+
288
+ // ── Context pack builder ──────────────────────────────────────────────────────
289
+
290
+ function createContextPackId() {
291
+ return `ctx-${Date.now()}-${crypto.randomBytes(3).toString("hex")}`;
292
+ }
293
+
294
+ /**
295
+ * Builds a stage-specific context pack for the given task/stage.
296
+ *
297
+ * options:
298
+ * taskSummary, stageId, stageRole, selectedWorker (toolId), selectedModel,
299
+ * contextNeed ("small"|"medium"|"large"), reasoningNeed ("low"|"medium"|"high"),
300
+ * routeId, changedFiles, relevantFiles, skillSummaries,
301
+ * safePathConstraints, intakeWarnings, governanceSummary,
302
+ * testCommands, proofSummary, nextAction
303
+ */
304
+ function buildContextPack(cwd, options = {}) {
305
+ const {
306
+ taskSummary = "Unknown task",
307
+ stageId = "implement",
308
+ stageRole = "implementation",
309
+ selectedWorker = null,
310
+ selectedModel = null,
311
+ contextNeed = "medium",
312
+ reasoningNeed = "medium",
313
+ routeId = null,
314
+ nextAction = null,
315
+ } = options;
316
+
317
+ const contextPackId = createContextPackId();
318
+ const budget = getStageBudget(stageId, selectedWorker);
319
+
320
+ // Override budget class if contextNeed signals it
321
+ const effectiveBudget = { ...budget };
322
+ if (contextNeed === "small" && budget.budgetClass !== "small") {
323
+ effectiveBudget.budgetClass = "small";
324
+ effectiveBudget.maxInputTokens = Math.min(budget.maxInputTokens, 4000);
325
+ }
326
+ if (contextNeed === "large" && budget.budgetClass === "small" && !budget.localWorkerCapped) {
327
+ effectiveBudget.budgetClass = "medium";
328
+ effectiveBudget.maxInputTokens = 8000;
329
+ }
330
+
331
+ const selection = selectContextItems(options, effectiveBudget);
332
+ const tokenWarningLevel = tokenWarning(selection.estimatedTokens, effectiveBudget.maxInputTokens);
333
+ const safetyBoundaries = buildSafetyBoundaries(stageId);
334
+
335
+ const reductionSummary = {
336
+ compactionUsed: selection.compactionUsed,
337
+ includedCount: selection.included.length,
338
+ excludedCount: selection.excluded.length,
339
+ estimatedTokens: selection.estimatedTokens,
340
+ maxTokens: effectiveBudget.maxInputTokens,
341
+ budgetWarning: tokenWarningLevel,
342
+ };
343
+
344
+ const pack = {
345
+ schemaVersion: SCHEMA_VERSION,
346
+ contract: CONTRACT,
347
+ contextPackId,
348
+ generatedAt: nowIso(),
349
+ taskSummary: redactIfSecret(String(taskSummary).slice(0, 300)),
350
+ routeId: routeId || null,
351
+ stageId,
352
+ stageRole,
353
+ selectedWorker: selectedWorker || null,
354
+ selectedModel: selectedModel || null,
355
+ contextNeed,
356
+ reasoningNeed,
357
+ targetBudget: {
358
+ estimatedInputTokens: selection.estimatedTokens,
359
+ maxInputTokens: effectiveBudget.maxInputTokens,
360
+ budgetClass: effectiveBudget.budgetClass,
361
+ },
362
+ includedContext: selection.included,
363
+ excludedContext: selection.excluded,
364
+ safetyBoundaries,
365
+ tokenEstimate: {
366
+ estimatedTokens: selection.estimatedTokens,
367
+ maxInputTokens: effectiveBudget.maxInputTokens,
368
+ warning: tokenWarningLevel,
369
+ },
370
+ reductionSummary,
371
+ evidenceRequirements: [
372
+ "No secrets in context pack",
373
+ "No full unselected skill bodies",
374
+ "No .env or raw credential files",
375
+ ],
376
+ nextAction: nextAction ? redactIfSecret(String(nextAction).slice(0, 200)) : null,
377
+ redacted: true,
378
+ localWorkerCapped: effectiveBudget.localWorkerCapped,
379
+ };
380
+
381
+ return pack;
382
+ }
383
+
384
+ // ── Persistence ───────────────────────────────────────────────────────────────
385
+
386
+ function ensureContextOptimizerDirs(cwd) {
387
+ ensureCcoDirs(cwd);
388
+ const dir = path.join(cwd, CONTEXT_OPT_DIR_REL);
389
+ fs.mkdirSync(dir, { recursive: true });
390
+ return dir;
391
+ }
392
+
393
+ function writeContextPack(cwd, pack, options = {}) {
394
+ ensureContextOptimizerDirs(cwd);
395
+ const latestPath = path.join(cwd, LATEST_PACK_REL);
396
+ safeWriteJson(latestPath, pack);
397
+
398
+ // Also write timestamped copy for history
399
+ if (!options.noHistory) {
400
+ const histDir = path.join(cwd, CONTEXT_OPT_DIR_REL, "history");
401
+ fs.mkdirSync(histDir, { recursive: true });
402
+ const histPath = path.join(histDir, `${pack.contextPackId}.json`);
403
+ safeWriteJson(histPath, pack);
404
+ }
405
+
406
+ return {
407
+ contextPackId: pack.contextPackId,
408
+ latestPackPath: LATEST_PACK_REL,
409
+ };
410
+ }
411
+
412
+ function readLatestContextPack(cwd) {
413
+ return safeReadJson(path.join(cwd, LATEST_PACK_REL), null);
414
+ }
415
+
416
+ // ── Receipt ───────────────────────────────────────────────────────────────────
417
+
418
+ function buildContextOptimizationReceipt(cwd, pack) {
419
+ return {
420
+ schemaVersion: SCHEMA_VERSION,
421
+ contract: "avorelo.contextOptimizationReceipt.v1",
422
+ generatedAt: nowIso(),
423
+ latestContextPackPath: LATEST_PACK_REL,
424
+ latestContextPackId: pack.contextPackId,
425
+ stageId: pack.stageId,
426
+ budgetClass: pack.targetBudget.budgetClass,
427
+ estimatedTokens: pack.tokenEstimate.estimatedTokens,
428
+ maxInputTokens: pack.tokenEstimate.maxInputTokens,
429
+ budgetWarning: pack.tokenEstimate.warning,
430
+ includedCount: pack.includedContext.length,
431
+ excludedCount: pack.excludedContext.length,
432
+ compactionUsed: pack.reductionSummary.compactionUsed,
433
+ localWorkerCapped: pack.localWorkerCapped,
434
+ safetyBoundaries: pack.safetyBoundaries,
435
+ noSecretsConfirmed: pack.safetyBoundaries.noSecrets,
436
+ nextAction: pack.nextAction,
437
+ redacted: true,
438
+ };
439
+ }
440
+
441
+ function writeContextOptimizationReceipt(cwd, receipt) {
442
+ ensureContextOptimizerDirs(cwd);
443
+ const latestPath = path.join(cwd, LATEST_RECEIPT_REL);
444
+ safeWriteJson(latestPath, receipt);
445
+ return { latestReceiptPath: LATEST_RECEIPT_REL };
446
+ }
447
+
448
+ function readLatestContextOptimizationReceipt(cwd) {
449
+ return safeReadJson(path.join(cwd, LATEST_RECEIPT_REL), null);
450
+ }
451
+
452
+ // ── Product learning events ───────────────────────────────────────────────────
453
+
454
+ function emitContextPackEvent(cwd, pack) {
455
+ try {
456
+ appendProductLearningEvent(cwd, {
457
+ eventName: "context_pack_generated",
458
+ category: "context_optimization",
459
+ surface: "local",
460
+ status: "observed",
461
+ payload: {
462
+ stageId: pack.stageId,
463
+ budgetClass: pack.targetBudget.budgetClass,
464
+ estimatedTokens: pack.tokenEstimate.estimatedTokens,
465
+ maxInputTokens: pack.tokenEstimate.maxInputTokens,
466
+ budgetWarning: pack.tokenEstimate.warning,
467
+ includedCount: pack.includedContext.length,
468
+ excludedCount: pack.excludedContext.length,
469
+ compactionUsed: pack.reductionSummary.compactionUsed,
470
+ localWorkerCapped: pack.localWorkerCapped,
471
+ contextNeed: pack.contextNeed,
472
+ reasoningNeed: pack.reasoningNeed,
473
+ },
474
+ });
475
+ if (pack.tokenEstimate.warning === "over_budget") {
476
+ appendProductLearningEvent(cwd, {
477
+ eventName: "context_over_budget",
478
+ category: "context_optimization",
479
+ surface: "local",
480
+ status: "observed",
481
+ payload: {
482
+ stageId: pack.stageId,
483
+ estimatedTokens: pack.tokenEstimate.estimatedTokens,
484
+ maxInputTokens: pack.tokenEstimate.maxInputTokens,
485
+ budgetClass: pack.targetBudget.budgetClass,
486
+ },
487
+ });
488
+ } else if (pack.tokenEstimate.warning === "near_budget") {
489
+ appendProductLearningEvent(cwd, {
490
+ eventName: "context_budget_warning",
491
+ category: "context_optimization",
492
+ surface: "local",
493
+ status: "observed",
494
+ payload: {
495
+ stageId: pack.stageId,
496
+ estimatedTokens: pack.tokenEstimate.estimatedTokens,
497
+ maxInputTokens: pack.tokenEstimate.maxInputTokens,
498
+ },
499
+ });
500
+ }
501
+ if (pack.reductionSummary.compactionUsed) {
502
+ appendProductLearningEvent(cwd, {
503
+ eventName: "context_compaction_used",
504
+ category: "context_optimization",
505
+ surface: "local",
506
+ status: "observed",
507
+ payload: {
508
+ stageId: pack.stageId,
509
+ excludedCount: pack.reductionSummary.excludedCount,
510
+ },
511
+ });
512
+ }
513
+ if (pack.localWorkerCapped) {
514
+ appendProductLearningEvent(cwd, {
515
+ eventName: "local_worker_context_boundary_used",
516
+ category: "context_optimization",
517
+ surface: "local",
518
+ status: "observed",
519
+ payload: {
520
+ stageId: pack.stageId,
521
+ worker: pack.selectedWorker,
522
+ budgetClass: pack.targetBudget.budgetClass,
523
+ },
524
+ });
525
+ }
526
+ if (pack.safetyBoundaries.noSecrets) {
527
+ appendProductLearningEvent(cwd, {
528
+ eventName: "no_secrets_context_boundary_applied",
529
+ category: "context_optimization",
530
+ surface: "local",
531
+ status: "observed",
532
+ payload: {
533
+ stageId: pack.stageId,
534
+ },
535
+ });
536
+ }
537
+ if (pack.excludedContext.length > 0) {
538
+ pack.excludedContext.slice(0, 3).forEach((item) => {
539
+ appendProductLearningEvent(cwd, {
540
+ eventName: "context_item_excluded",
541
+ category: "context_optimization",
542
+ surface: "local",
543
+ status: "observed",
544
+ payload: {
545
+ reason: item.reason,
546
+ itemType: item.itemType,
547
+ },
548
+ });
549
+ });
550
+ }
551
+ } catch {
552
+ // product learning is best-effort
553
+ }
554
+ }
555
+
556
+ // ── Build + write shortcut ────────────────────────────────────────────────────
557
+
558
+ function buildAndWriteContextPack(cwd, options = {}) {
559
+ const pack = buildContextPack(cwd, options);
560
+ const writeResult = writeContextPack(cwd, pack);
561
+ const receipt = buildContextOptimizationReceipt(cwd, pack);
562
+ writeContextOptimizationReceipt(cwd, receipt);
563
+ emitContextPackEvent(cwd, pack);
564
+
565
+ try {
566
+ appendProductLearningEvent(cwd, {
567
+ eventName: "context_optimization_receipt_written",
568
+ category: "context_optimization",
569
+ surface: "local",
570
+ status: "observed",
571
+ payload: {
572
+ stageId: pack.stageId,
573
+ budgetClass: pack.targetBudget.budgetClass,
574
+ estimatedTokens: pack.tokenEstimate.estimatedTokens,
575
+ },
576
+ });
577
+ appendProductLearningEvent(cwd, {
578
+ eventName: "context_budget_estimated",
579
+ category: "context_optimization",
580
+ surface: "local",
581
+ status: "observed",
582
+ payload: {
583
+ stageId: pack.stageId,
584
+ budgetClass: pack.targetBudget.budgetClass,
585
+ estimatedTokens: pack.tokenEstimate.estimatedTokens,
586
+ maxInputTokens: pack.tokenEstimate.maxInputTokens,
587
+ estimateMethod: "heuristic",
588
+ },
589
+ });
590
+ } catch {}
591
+
592
+ return {
593
+ pack,
594
+ receipt,
595
+ contextPackId: pack.contextPackId,
596
+ latestPackPath: writeResult.latestPackPath,
597
+ latestReceiptPath: LATEST_RECEIPT_REL,
598
+ };
599
+ }
600
+
601
+ // ── Dashboard surface ─────────────────────────────────────────────────────────
602
+
603
+ function buildContextOptimizerSurface(cwd) {
604
+ const receipt = readLatestContextOptimizationReceipt(cwd);
605
+ const pack = readLatestContextPack(cwd);
606
+
607
+ if (!receipt || !pack) {
608
+ return {
609
+ status: "not_run",
610
+ latestReceiptPath: null,
611
+ latestContextPackPath: null,
612
+ stageBudgets: null,
613
+ lastEstimateMethod: null,
614
+ lastEstimatedTokens: null,
615
+ budgetWarning: null,
616
+ includedItems: null,
617
+ excludedItems: null,
618
+ compactionUsed: null,
619
+ nextAction: "Run `avorelo context --task \"<task>\" --json` to generate a stage context pack.",
620
+ };
621
+ }
622
+
623
+ const status = receipt.budgetWarning === "over_budget" ? "over_budget"
624
+ : receipt.budgetWarning === "near_budget" ? "near_budget"
625
+ : "ok";
626
+
627
+ return {
628
+ status,
629
+ latestReceiptPath: LATEST_RECEIPT_REL,
630
+ latestContextPackPath: LATEST_PACK_REL,
631
+ stageId: receipt.stageId,
632
+ budgetClass: receipt.budgetClass,
633
+ lastEstimateMethod: "heuristic",
634
+ lastEstimatedTokens: receipt.estimatedTokens,
635
+ maxInputTokens: receipt.maxInputTokens,
636
+ budgetWarning: receipt.budgetWarning,
637
+ includedItems: receipt.includedCount,
638
+ excludedItems: receipt.excludedCount,
639
+ compactionUsed: receipt.compactionUsed,
640
+ localWorkerCapped: receipt.localWorkerCapped,
641
+ noSecretsConfirmed: receipt.noSecretsConfirmed,
642
+ nextAction: receipt.nextAction || "Context pack is ready for handoff.",
643
+ };
644
+ }
645
+
646
+ // ── Handoff integration helper ────────────────────────────────────────────────
647
+
648
+ function buildHandoffContextPackSummary(cwd) {
649
+ const pack = readLatestContextPack(cwd);
650
+ if (!pack) return null;
651
+
652
+ try {
653
+ appendProductLearningEvent(cwd, {
654
+ eventName: "handoff_context_pack_attached",
655
+ category: "handoff",
656
+ surface: "local",
657
+ status: "observed",
658
+ payload: {
659
+ stageId: pack.stageId,
660
+ budgetClass: pack.targetBudget.budgetClass,
661
+ estimatedTokens: pack.tokenEstimate.estimatedTokens,
662
+ },
663
+ });
664
+ } catch {}
665
+
666
+ return {
667
+ contextPackPath: LATEST_PACK_REL,
668
+ contextPackId: pack.contextPackId,
669
+ stageId: pack.stageId,
670
+ budgetClass: pack.targetBudget.budgetClass,
671
+ tokenEstimate: pack.tokenEstimate.estimatedTokens,
672
+ includedCount: pack.includedContext.length,
673
+ excludedCount: pack.excludedContext.length,
674
+ budgetWarning: pack.tokenEstimate.warning,
675
+ nextAction: pack.nextAction,
676
+ };
677
+ }
678
+
679
+ // ── Skill lazy-loading signal ─────────────────────────────────────────────────
680
+
681
+ function emitSkillLazyLoadEvent(cwd, skillId, loadedTokens) {
682
+ try {
683
+ appendProductLearningEvent(cwd, {
684
+ eventName: "skill_context_lazy_loaded",
685
+ category: "context_optimization",
686
+ surface: "local",
687
+ status: "observed",
688
+ payload: {
689
+ skillId: String(skillId || "unknown").slice(0, 60),
690
+ loadedTokens,
691
+ },
692
+ });
693
+ } catch {}
694
+ }
695
+
696
+ module.exports = {
697
+ CONTRACT,
698
+ SCHEMA_VERSION,
699
+ LATEST_PACK_REL,
700
+ LATEST_RECEIPT_REL,
701
+ STAGE_BUDGETS,
702
+ estimateTokens,
703
+ tokenWarning,
704
+ getStageBudget,
705
+ buildContextPack,
706
+ writeContextPack,
707
+ readLatestContextPack,
708
+ buildContextOptimizationReceipt,
709
+ writeContextOptimizationReceipt,
710
+ readLatestContextOptimizationReceipt,
711
+ buildAndWriteContextPack,
712
+ buildContextOptimizerSurface,
713
+ buildHandoffContextPackSummary,
714
+ emitSkillLazyLoadEvent,
715
+ };