@mcoda/mswarm 0.1.56 → 0.1.60

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 (250) hide show
  1. package/README.md +20 -1
  2. package/dist/codali-executor.d.ts +266 -0
  3. package/dist/codali-executor.d.ts.map +1 -0
  4. package/dist/codali-executor.js +227 -0
  5. package/dist/codali-executor.js.map +1 -0
  6. package/dist/runtime.d.ts +47 -1
  7. package/dist/runtime.d.ts.map +1 -1
  8. package/dist/runtime.js +248 -30
  9. package/dist/runtime.js.map +1 -1
  10. package/dist/server.d.ts.map +1 -1
  11. package/dist/server.js +83 -3
  12. package/dist/server.js.map +1 -1
  13. package/dist/vendor/codali/agents/AgentProtocol.d.ts +287 -0
  14. package/dist/vendor/codali/agents/AgentProtocol.d.ts.map +1 -0
  15. package/dist/vendor/codali/agents/AgentProtocol.js +365 -0
  16. package/dist/vendor/codali/agents/AgentResolver.d.ts +23 -0
  17. package/dist/vendor/codali/agents/AgentResolver.d.ts.map +1 -0
  18. package/dist/vendor/codali/agents/AgentResolver.js +77 -0
  19. package/dist/vendor/codali/agents/PhaseAgentSelector.d.ts +23 -0
  20. package/dist/vendor/codali/agents/PhaseAgentSelector.d.ts.map +1 -0
  21. package/dist/vendor/codali/agents/PhaseAgentSelector.js +287 -0
  22. package/dist/vendor/codali/cli/EvalCommand.d.ts +37 -0
  23. package/dist/vendor/codali/cli/EvalCommand.d.ts.map +1 -0
  24. package/dist/vendor/codali/cli/EvalCommand.js +333 -0
  25. package/dist/vendor/codali/cli/FeedbackCommand.d.ts +22 -0
  26. package/dist/vendor/codali/cli/FeedbackCommand.d.ts.map +1 -0
  27. package/dist/vendor/codali/cli/FeedbackCommand.js +163 -0
  28. package/dist/vendor/codali/cli/RunCommand.d.ts +78 -0
  29. package/dist/vendor/codali/cli/RunCommand.d.ts.map +1 -0
  30. package/dist/vendor/codali/cli/RunCommand.js +2261 -0
  31. package/dist/vendor/codali/cli.d.ts +3 -0
  32. package/dist/vendor/codali/cli.d.ts.map +1 -0
  33. package/dist/vendor/codali/cli.js +109 -0
  34. package/dist/vendor/codali/cognitive/ArchitectPlanner.d.ts +107 -0
  35. package/dist/vendor/codali/cognitive/ArchitectPlanner.d.ts.map +1 -0
  36. package/dist/vendor/codali/cognitive/ArchitectPlanner.js +1726 -0
  37. package/dist/vendor/codali/cognitive/BuilderOutputParser.d.ts +25 -0
  38. package/dist/vendor/codali/cognitive/BuilderOutputParser.d.ts.map +1 -0
  39. package/dist/vendor/codali/cognitive/BuilderOutputParser.js +164 -0
  40. package/dist/vendor/codali/cognitive/BuilderRunner.d.ts +76 -0
  41. package/dist/vendor/codali/cognitive/BuilderRunner.d.ts.map +1 -0
  42. package/dist/vendor/codali/cognitive/BuilderRunner.js +1159 -0
  43. package/dist/vendor/codali/cognitive/ContextAssembler.d.ts +91 -0
  44. package/dist/vendor/codali/cognitive/ContextAssembler.d.ts.map +1 -0
  45. package/dist/vendor/codali/cognitive/ContextAssembler.js +4547 -0
  46. package/dist/vendor/codali/cognitive/ContextBudget.d.ts +19 -0
  47. package/dist/vendor/codali/cognitive/ContextBudget.d.ts.map +1 -0
  48. package/dist/vendor/codali/cognitive/ContextBudget.js +35 -0
  49. package/dist/vendor/codali/cognitive/ContextFileLoader.d.ts +30 -0
  50. package/dist/vendor/codali/cognitive/ContextFileLoader.d.ts.map +1 -0
  51. package/dist/vendor/codali/cognitive/ContextFileLoader.js +307 -0
  52. package/dist/vendor/codali/cognitive/ContextManager.d.ts +47 -0
  53. package/dist/vendor/codali/cognitive/ContextManager.d.ts.map +1 -0
  54. package/dist/vendor/codali/cognitive/ContextManager.js +272 -0
  55. package/dist/vendor/codali/cognitive/ContextRedactor.d.ts +18 -0
  56. package/dist/vendor/codali/cognitive/ContextRedactor.d.ts.map +1 -0
  57. package/dist/vendor/codali/cognitive/ContextRedactor.js +53 -0
  58. package/dist/vendor/codali/cognitive/ContextSelector.d.ts +22 -0
  59. package/dist/vendor/codali/cognitive/ContextSelector.d.ts.map +1 -0
  60. package/dist/vendor/codali/cognitive/ContextSelector.js +431 -0
  61. package/dist/vendor/codali/cognitive/ContextSerializer.d.ts +8 -0
  62. package/dist/vendor/codali/cognitive/ContextSerializer.d.ts.map +1 -0
  63. package/dist/vendor/codali/cognitive/ContextSerializer.js +882 -0
  64. package/dist/vendor/codali/cognitive/ContextStore.d.ts +27 -0
  65. package/dist/vendor/codali/cognitive/ContextStore.d.ts.map +1 -0
  66. package/dist/vendor/codali/cognitive/ContextStore.js +79 -0
  67. package/dist/vendor/codali/cognitive/ContextSummarizer.d.ts +16 -0
  68. package/dist/vendor/codali/cognitive/ContextSummarizer.d.ts.map +1 -0
  69. package/dist/vendor/codali/cognitive/ContextSummarizer.js +45 -0
  70. package/dist/vendor/codali/cognitive/CostEstimator.d.ts +31 -0
  71. package/dist/vendor/codali/cognitive/CostEstimator.d.ts.map +1 -0
  72. package/dist/vendor/codali/cognitive/CostEstimator.js +66 -0
  73. package/dist/vendor/codali/cognitive/CriticEvaluator.d.ts +32 -0
  74. package/dist/vendor/codali/cognitive/CriticEvaluator.d.ts.map +1 -0
  75. package/dist/vendor/codali/cognitive/CriticEvaluator.js +297 -0
  76. package/dist/vendor/codali/cognitive/EvidenceGate.d.ts +9 -0
  77. package/dist/vendor/codali/cognitive/EvidenceGate.d.ts.map +1 -0
  78. package/dist/vendor/codali/cognitive/EvidenceGate.js +75 -0
  79. package/dist/vendor/codali/cognitive/GoldenExampleIndexer.d.ts +12 -0
  80. package/dist/vendor/codali/cognitive/GoldenExampleIndexer.d.ts.map +1 -0
  81. package/dist/vendor/codali/cognitive/GoldenExampleIndexer.js +34 -0
  82. package/dist/vendor/codali/cognitive/GoldenSetStore.d.ts +33 -0
  83. package/dist/vendor/codali/cognitive/GoldenSetStore.d.ts.map +1 -0
  84. package/dist/vendor/codali/cognitive/GoldenSetStore.js +159 -0
  85. package/dist/vendor/codali/cognitive/IntentSignals.d.ts +7 -0
  86. package/dist/vendor/codali/cognitive/IntentSignals.d.ts.map +1 -0
  87. package/dist/vendor/codali/cognitive/IntentSignals.js +285 -0
  88. package/dist/vendor/codali/cognitive/LearningGovernance.d.ts +100 -0
  89. package/dist/vendor/codali/cognitive/LearningGovernance.d.ts.map +1 -0
  90. package/dist/vendor/codali/cognitive/LearningGovernance.js +276 -0
  91. package/dist/vendor/codali/cognitive/MemoryWriteback.d.ts +64 -0
  92. package/dist/vendor/codali/cognitive/MemoryWriteback.d.ts.map +1 -0
  93. package/dist/vendor/codali/cognitive/MemoryWriteback.js +287 -0
  94. package/dist/vendor/codali/cognitive/PatchApplier.d.ts +49 -0
  95. package/dist/vendor/codali/cognitive/PatchApplier.d.ts.map +1 -0
  96. package/dist/vendor/codali/cognitive/PatchApplier.js +199 -0
  97. package/dist/vendor/codali/cognitive/PatchInterpreter.d.ts +35 -0
  98. package/dist/vendor/codali/cognitive/PatchInterpreter.d.ts.map +1 -0
  99. package/dist/vendor/codali/cognitive/PatchInterpreter.js +100 -0
  100. package/dist/vendor/codali/cognitive/PatchOutputNormalizer.d.ts +7 -0
  101. package/dist/vendor/codali/cognitive/PatchOutputNormalizer.d.ts.map +1 -0
  102. package/dist/vendor/codali/cognitive/PatchOutputNormalizer.js +59 -0
  103. package/dist/vendor/codali/cognitive/PostMortemAnalyzer.d.ts +17 -0
  104. package/dist/vendor/codali/cognitive/PostMortemAnalyzer.d.ts.map +1 -0
  105. package/dist/vendor/codali/cognitive/PostMortemAnalyzer.js +131 -0
  106. package/dist/vendor/codali/cognitive/PreferenceExtraction.d.ts +3 -0
  107. package/dist/vendor/codali/cognitive/PreferenceExtraction.d.ts.map +1 -0
  108. package/dist/vendor/codali/cognitive/PreferenceExtraction.js +85 -0
  109. package/dist/vendor/codali/cognitive/Prompts.d.ts +15 -0
  110. package/dist/vendor/codali/cognitive/Prompts.d.ts.map +1 -0
  111. package/dist/vendor/codali/cognitive/Prompts.js +326 -0
  112. package/dist/vendor/codali/cognitive/ProviderRouting.d.ts +16 -0
  113. package/dist/vendor/codali/cognitive/ProviderRouting.d.ts.map +1 -0
  114. package/dist/vendor/codali/cognitive/ProviderRouting.js +24 -0
  115. package/dist/vendor/codali/cognitive/QueryExtraction.d.ts +12 -0
  116. package/dist/vendor/codali/cognitive/QueryExtraction.d.ts.map +1 -0
  117. package/dist/vendor/codali/cognitive/QueryExtraction.js +262 -0
  118. package/dist/vendor/codali/cognitive/RunHistoryIndexer.d.ts +13 -0
  119. package/dist/vendor/codali/cognitive/RunHistoryIndexer.d.ts.map +1 -0
  120. package/dist/vendor/codali/cognitive/RunHistoryIndexer.js +125 -0
  121. package/dist/vendor/codali/cognitive/SmartPipeline.d.ts +92 -0
  122. package/dist/vendor/codali/cognitive/SmartPipeline.d.ts.map +1 -0
  123. package/dist/vendor/codali/cognitive/SmartPipeline.js +4804 -0
  124. package/dist/vendor/codali/cognitive/Types.d.ts +474 -0
  125. package/dist/vendor/codali/cognitive/Types.d.ts.map +1 -0
  126. package/dist/vendor/codali/cognitive/Types.js +7 -0
  127. package/dist/vendor/codali/cognitive/ValidationRunner.d.ts +57 -0
  128. package/dist/vendor/codali/cognitive/ValidationRunner.d.ts.map +1 -0
  129. package/dist/vendor/codali/cognitive/ValidationRunner.js +515 -0
  130. package/dist/vendor/codali/config/Config.d.ts +249 -0
  131. package/dist/vendor/codali/config/Config.d.ts.map +1 -0
  132. package/dist/vendor/codali/config/Config.js +200 -0
  133. package/dist/vendor/codali/config/ConfigLoader.d.ts +56 -0
  134. package/dist/vendor/codali/config/ConfigLoader.d.ts.map +1 -0
  135. package/dist/vendor/codali/config/ConfigLoader.js +1246 -0
  136. package/dist/vendor/codali/docdex/DocdexClient.d.ts +113 -0
  137. package/dist/vendor/codali/docdex/DocdexClient.d.ts.map +1 -0
  138. package/dist/vendor/codali/docdex/DocdexClient.js +524 -0
  139. package/dist/vendor/codali/eval/EvalRunner.d.ts +35 -0
  140. package/dist/vendor/codali/eval/EvalRunner.d.ts.map +1 -0
  141. package/dist/vendor/codali/eval/EvalRunner.js +38 -0
  142. package/dist/vendor/codali/eval/EvalTaskExecutor.d.ts +81 -0
  143. package/dist/vendor/codali/eval/EvalTaskExecutor.d.ts.map +1 -0
  144. package/dist/vendor/codali/eval/EvalTaskExecutor.js +371 -0
  145. package/dist/vendor/codali/eval/GateEvaluator.d.ts +31 -0
  146. package/dist/vendor/codali/eval/GateEvaluator.d.ts.map +1 -0
  147. package/dist/vendor/codali/eval/GateEvaluator.js +134 -0
  148. package/dist/vendor/codali/eval/MetricTypes.d.ts +28 -0
  149. package/dist/vendor/codali/eval/MetricTypes.d.ts.map +1 -0
  150. package/dist/vendor/codali/eval/MetricTypes.js +1 -0
  151. package/dist/vendor/codali/eval/MetricsAggregator.d.ts +4 -0
  152. package/dist/vendor/codali/eval/MetricsAggregator.d.ts.map +1 -0
  153. package/dist/vendor/codali/eval/MetricsAggregator.js +97 -0
  154. package/dist/vendor/codali/eval/RegressionComparator.d.ts +29 -0
  155. package/dist/vendor/codali/eval/RegressionComparator.d.ts.map +1 -0
  156. package/dist/vendor/codali/eval/RegressionComparator.js +155 -0
  157. package/dist/vendor/codali/eval/ReportInputAdapter.d.ts +52 -0
  158. package/dist/vendor/codali/eval/ReportInputAdapter.d.ts.map +1 -0
  159. package/dist/vendor/codali/eval/ReportInputAdapter.js +229 -0
  160. package/dist/vendor/codali/eval/ReportSerializer.d.ts +32 -0
  161. package/dist/vendor/codali/eval/ReportSerializer.d.ts.map +1 -0
  162. package/dist/vendor/codali/eval/ReportSerializer.js +33 -0
  163. package/dist/vendor/codali/eval/ReportStore.d.ts +18 -0
  164. package/dist/vendor/codali/eval/ReportStore.d.ts.map +1 -0
  165. package/dist/vendor/codali/eval/ReportStore.js +96 -0
  166. package/dist/vendor/codali/eval/SuiteLoader.d.ts +12 -0
  167. package/dist/vendor/codali/eval/SuiteLoader.d.ts.map +1 -0
  168. package/dist/vendor/codali/eval/SuiteLoader.js +51 -0
  169. package/dist/vendor/codali/eval/SuiteSchema.d.ts +56 -0
  170. package/dist/vendor/codali/eval/SuiteSchema.d.ts.map +1 -0
  171. package/dist/vendor/codali/eval/SuiteSchema.js +357 -0
  172. package/dist/vendor/codali/index.d.ts +11 -0
  173. package/dist/vendor/codali/index.d.ts.map +1 -0
  174. package/dist/vendor/codali/index.js +5 -0
  175. package/dist/vendor/codali/providers/CodexCliProvider.d.ts +8 -0
  176. package/dist/vendor/codali/providers/CodexCliProvider.d.ts.map +1 -0
  177. package/dist/vendor/codali/providers/CodexCliProvider.js +282 -0
  178. package/dist/vendor/codali/providers/OllamaRemoteProvider.d.ts +8 -0
  179. package/dist/vendor/codali/providers/OllamaRemoteProvider.d.ts.map +1 -0
  180. package/dist/vendor/codali/providers/OllamaRemoteProvider.js +300 -0
  181. package/dist/vendor/codali/providers/OpenAiCompatibleProvider.d.ts +8 -0
  182. package/dist/vendor/codali/providers/OpenAiCompatibleProvider.d.ts.map +1 -0
  183. package/dist/vendor/codali/providers/OpenAiCompatibleProvider.js +192 -0
  184. package/dist/vendor/codali/providers/ProviderRegistry.d.ts +12 -0
  185. package/dist/vendor/codali/providers/ProviderRegistry.d.ts.map +1 -0
  186. package/dist/vendor/codali/providers/ProviderRegistry.js +28 -0
  187. package/dist/vendor/codali/providers/ProviderTypes.d.ts +81 -0
  188. package/dist/vendor/codali/providers/ProviderTypes.d.ts.map +1 -0
  189. package/dist/vendor/codali/providers/ProviderTypes.js +1 -0
  190. package/dist/vendor/codali/runtime/CodaliRuntime.d.ts +183 -0
  191. package/dist/vendor/codali/runtime/CodaliRuntime.d.ts.map +1 -0
  192. package/dist/vendor/codali/runtime/CodaliRuntime.js +1363 -0
  193. package/dist/vendor/codali/runtime/DeepInvestigationErrors.d.ts +39 -0
  194. package/dist/vendor/codali/runtime/DeepInvestigationErrors.d.ts.map +1 -0
  195. package/dist/vendor/codali/runtime/DeepInvestigationErrors.js +57 -0
  196. package/dist/vendor/codali/runtime/RunContext.d.ts +27 -0
  197. package/dist/vendor/codali/runtime/RunContext.d.ts.map +1 -0
  198. package/dist/vendor/codali/runtime/RunContext.js +51 -0
  199. package/dist/vendor/codali/runtime/RunLogQuery.d.ts +48 -0
  200. package/dist/vendor/codali/runtime/RunLogQuery.d.ts.map +1 -0
  201. package/dist/vendor/codali/runtime/RunLogQuery.js +36 -0
  202. package/dist/vendor/codali/runtime/RunLogReader.d.ts +19 -0
  203. package/dist/vendor/codali/runtime/RunLogReader.d.ts.map +1 -0
  204. package/dist/vendor/codali/runtime/RunLogReader.js +361 -0
  205. package/dist/vendor/codali/runtime/RunLogger.d.ts +71 -0
  206. package/dist/vendor/codali/runtime/RunLogger.d.ts.map +1 -0
  207. package/dist/vendor/codali/runtime/RunLogger.js +100 -0
  208. package/dist/vendor/codali/runtime/RunTelemetryTypes.d.ts +117 -0
  209. package/dist/vendor/codali/runtime/RunTelemetryTypes.d.ts.map +1 -0
  210. package/dist/vendor/codali/runtime/RunTelemetryTypes.js +299 -0
  211. package/dist/vendor/codali/runtime/Runner.d.ts +66 -0
  212. package/dist/vendor/codali/runtime/Runner.d.ts.map +1 -0
  213. package/dist/vendor/codali/runtime/Runner.js +215 -0
  214. package/dist/vendor/codali/runtime/StoragePaths.d.ts +3 -0
  215. package/dist/vendor/codali/runtime/StoragePaths.d.ts.map +1 -0
  216. package/dist/vendor/codali/runtime/StoragePaths.js +19 -0
  217. package/dist/vendor/codali/runtime/WorkspaceLock.d.ts +30 -0
  218. package/dist/vendor/codali/runtime/WorkspaceLock.d.ts.map +1 -0
  219. package/dist/vendor/codali/runtime/WorkspaceLock.js +141 -0
  220. package/dist/vendor/codali/session/InstructionLoader.d.ts +14 -0
  221. package/dist/vendor/codali/session/InstructionLoader.d.ts.map +1 -0
  222. package/dist/vendor/codali/session/InstructionLoader.js +107 -0
  223. package/dist/vendor/codali/session/SessionStore.d.ts +81 -0
  224. package/dist/vendor/codali/session/SessionStore.d.ts.map +1 -0
  225. package/dist/vendor/codali/session/SessionStore.js +244 -0
  226. package/dist/vendor/codali/subagents/SubagentOrchestrator.d.ts +68 -0
  227. package/dist/vendor/codali/subagents/SubagentOrchestrator.d.ts.map +1 -0
  228. package/dist/vendor/codali/subagents/SubagentOrchestrator.js +150 -0
  229. package/dist/vendor/codali/tools/ToolRegistry.d.ts +9 -0
  230. package/dist/vendor/codali/tools/ToolRegistry.d.ts.map +1 -0
  231. package/dist/vendor/codali/tools/ToolRegistry.js +263 -0
  232. package/dist/vendor/codali/tools/ToolTypes.d.ts +66 -0
  233. package/dist/vendor/codali/tools/ToolTypes.d.ts.map +1 -0
  234. package/dist/vendor/codali/tools/ToolTypes.js +32 -0
  235. package/dist/vendor/codali/tools/diff/DiffTool.d.ts +3 -0
  236. package/dist/vendor/codali/tools/diff/DiffTool.d.ts.map +1 -0
  237. package/dist/vendor/codali/tools/diff/DiffTool.js +34 -0
  238. package/dist/vendor/codali/tools/docdex/DocdexTools.d.ts +4 -0
  239. package/dist/vendor/codali/tools/docdex/DocdexTools.d.ts.map +1 -0
  240. package/dist/vendor/codali/tools/docdex/DocdexTools.js +453 -0
  241. package/dist/vendor/codali/tools/filesystem/FileTools.d.ts +3 -0
  242. package/dist/vendor/codali/tools/filesystem/FileTools.d.ts.map +1 -0
  243. package/dist/vendor/codali/tools/filesystem/FileTools.js +141 -0
  244. package/dist/vendor/codali/tools/search/SearchTool.d.ts +3 -0
  245. package/dist/vendor/codali/tools/search/SearchTool.d.ts.map +1 -0
  246. package/dist/vendor/codali/tools/search/SearchTool.js +46 -0
  247. package/dist/vendor/codali/tools/shell/ShellTool.d.ts +3 -0
  248. package/dist/vendor/codali/tools/shell/ShellTool.d.ts.map +1 -0
  249. package/dist/vendor/codali/tools/shell/ShellTool.js +104 -0
  250. package/package.json +5 -3
@@ -0,0 +1,272 @@
1
+ import { DEFAULT_CHAR_PER_TOKEN, DEFAULT_MODEL_TOKEN_LIMIT, estimateBudget, estimateMessagesTokens, resolveModelTokenLimit, } from "./ContextBudget.js";
2
+ export const buildLaneId = (scope) => {
3
+ const jobPart = scope.jobId ?? scope.runId ?? "run";
4
+ const taskPart = scope.taskId ?? scope.taskKey ?? "ad-hoc";
5
+ return `${jobPart}:${taskPart}:${scope.role}`;
6
+ };
7
+ const trimMessagesByBytes = (messages, maxBytes) => {
8
+ if (maxBytes <= 0)
9
+ return [];
10
+ let total = 0;
11
+ const kept = [];
12
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
13
+ const line = `${JSON.stringify(messages[index])}\n`;
14
+ const size = Buffer.byteLength(line, "utf8");
15
+ if (total + size > maxBytes) {
16
+ break;
17
+ }
18
+ total += size;
19
+ kept.push(messages[index]);
20
+ }
21
+ return kept.reverse();
22
+ };
23
+ export class ContextManager {
24
+ constructor(options) {
25
+ this.options = options;
26
+ this.lanes = new Map();
27
+ this.enabled = options.config.enabled;
28
+ this.charPerToken = options.charPerToken ?? DEFAULT_CHAR_PER_TOKEN;
29
+ }
30
+ async getLane(scope) {
31
+ const laneId = buildLaneId(scope);
32
+ const persisted = this.enabled && !scope.ephemeral;
33
+ const state = await this.ensureLane(laneId, scope.role, persisted);
34
+ return {
35
+ id: state.id,
36
+ role: state.role,
37
+ model: undefined,
38
+ messages: state.messages,
39
+ tokenEstimate: state.tokenEstimate,
40
+ updatedAt: state.updatedAt,
41
+ persisted: state.persisted,
42
+ };
43
+ }
44
+ async append(laneId, message, meta = {}) {
45
+ const persisted = meta.persisted ?? this.enabled;
46
+ const role = meta.role ?? "custom";
47
+ const state = await this.ensureLane(laneId, role, persisted);
48
+ if (!this.options.config.persistToolMessages && message.role === "tool") {
49
+ return;
50
+ }
51
+ let content = message.content ?? "";
52
+ let redactions = 0;
53
+ if (this.options.redactor) {
54
+ const redacted = this.options.redactor.redact(content);
55
+ content = redacted.content;
56
+ redactions = redacted.redactions;
57
+ }
58
+ const record = {
59
+ ...message,
60
+ content,
61
+ ts: Date.now(),
62
+ model: meta.model,
63
+ tokens: meta.tokens,
64
+ };
65
+ const messages = [...state.messages, record];
66
+ const tokenEstimate = estimateMessagesTokens(messages, this.charPerToken);
67
+ const updatedAt = Date.now();
68
+ const nextState = {
69
+ ...state,
70
+ messages,
71
+ tokenEstimate,
72
+ updatedAt,
73
+ redactions: state.redactions + redactions,
74
+ };
75
+ this.lanes.set(laneId, nextState);
76
+ if (!state.persisted) {
77
+ await this.logLaneUpdate(nextState);
78
+ return;
79
+ }
80
+ const snapshot = await this.options.store.append(laneId, record);
81
+ const limited = await this.enforceStorageLimits(laneId, snapshot, nextState.redactions);
82
+ this.updateStateFromSnapshot(laneId, limited, nextState.role, nextState.redactions);
83
+ const updated = this.lanes.get(laneId);
84
+ if (updated) {
85
+ await this.logLaneUpdate(updated);
86
+ }
87
+ }
88
+ async prepare(laneId, options = {}) {
89
+ const state = this.lanes.get(laneId) ?? (await this.ensureLane(laneId, "custom", this.enabled));
90
+ if (!state.persisted) {
91
+ return state.messages;
92
+ }
93
+ await this.summarizeIfNeeded(laneId, options);
94
+ const refreshed = this.lanes.get(laneId) ?? state;
95
+ return refreshed.messages;
96
+ }
97
+ async summarizeIfNeeded(laneId, options = {}) {
98
+ const state = this.lanes.get(laneId);
99
+ if (!state || !state.persisted || !this.enabled)
100
+ return;
101
+ if (!this.options.summarizer || !this.options.config.summarize.enabled)
102
+ return;
103
+ const modelLimit = resolveModelTokenLimit(options.model ?? "", this.options.config.modelTokenLimits, DEFAULT_MODEL_TOKEN_LIMIT);
104
+ const rawThreshold = this.options.config.summarize.thresholdPct ?? 0.9;
105
+ const thresholdPct = Number.isFinite(rawThreshold) && rawThreshold > 0 && rawThreshold <= 1 ? rawThreshold : 0.9;
106
+ const triggerLimit = Math.floor(modelLimit * thresholdPct);
107
+ const beforeMessages = state.messages.length;
108
+ const beforeTokens = estimateMessagesTokens(state.messages, this.charPerToken);
109
+ let messages = state.messages;
110
+ let estimate = estimateBudget({
111
+ systemPrompt: options.systemPrompt,
112
+ bundle: options.bundle,
113
+ history: messages,
114
+ charPerToken: this.charPerToken,
115
+ });
116
+ let iterations = 0;
117
+ let updated = false;
118
+ while (estimate.totalTokens > triggerLimit && messages.length > 1 && iterations < 5) {
119
+ const splitIndex = Math.max(1, Math.floor(messages.length / 2));
120
+ const summary = await this.options.summarizer.summarize(messages.slice(0, splitIndex));
121
+ const summaryRecord = { ...summary, ts: Date.now() };
122
+ messages = [summaryRecord, ...messages.slice(splitIndex)];
123
+ estimate = estimateBudget({
124
+ systemPrompt: options.systemPrompt,
125
+ bundle: options.bundle,
126
+ history: messages,
127
+ charPerToken: this.charPerToken,
128
+ });
129
+ iterations += 1;
130
+ updated = true;
131
+ }
132
+ if (!updated) {
133
+ return;
134
+ }
135
+ const snapshot = await this.options.store.replace(laneId, messages);
136
+ const limited = await this.enforceStorageLimits(laneId, snapshot, state.redactions);
137
+ this.updateStateFromSnapshot(laneId, limited, state.role, state.redactions);
138
+ const updatedLane = this.lanes.get(laneId);
139
+ if (updatedLane) {
140
+ await this.logLaneSummarized({
141
+ laneId,
142
+ role: updatedLane.role,
143
+ beforeMessages,
144
+ afterMessages: updatedLane.messages.length,
145
+ beforeTokens,
146
+ afterTokens: updatedLane.tokenEstimate,
147
+ iterations,
148
+ modelLimit,
149
+ redactions: updatedLane.redactions,
150
+ });
151
+ }
152
+ }
153
+ async flush(laneId) {
154
+ const state = this.lanes.get(laneId);
155
+ if (!state || !state.persisted)
156
+ return;
157
+ await this.options.store.replace(laneId, state.messages);
158
+ }
159
+ async ensureLane(laneId, role, persisted) {
160
+ const existing = this.lanes.get(laneId);
161
+ if (existing)
162
+ return existing;
163
+ let messages = [];
164
+ let updatedAt = Date.now();
165
+ if (persisted) {
166
+ const snapshot = await this.options.store.loadLane(laneId);
167
+ messages = snapshot.messages;
168
+ updatedAt = snapshot.updatedAt || updatedAt;
169
+ }
170
+ const tokenEstimate = estimateMessagesTokens(messages, this.charPerToken);
171
+ const state = {
172
+ id: laneId,
173
+ role,
174
+ persisted,
175
+ messages,
176
+ tokenEstimate,
177
+ updatedAt,
178
+ redactions: 0,
179
+ };
180
+ this.lanes.set(laneId, state);
181
+ return state;
182
+ }
183
+ async enforceStorageLimits(laneId, snapshot, redactions) {
184
+ const { maxMessages, maxBytesPerLane } = this.options.config;
185
+ const beforeMessages = snapshot.messages.length;
186
+ const beforeBytes = snapshot.byteSize;
187
+ let messages = snapshot.messages;
188
+ let trimmedByMessages = false;
189
+ let trimmedByBytes = false;
190
+ if (maxMessages >= 0 && messages.length > maxMessages) {
191
+ messages = messages.slice(-maxMessages);
192
+ trimmedByMessages = true;
193
+ }
194
+ if (maxBytesPerLane >= 0) {
195
+ const trimmed = trimMessagesByBytes(messages, maxBytesPerLane);
196
+ if (trimmed.length !== messages.length) {
197
+ trimmedByBytes = true;
198
+ }
199
+ messages = trimmed;
200
+ }
201
+ if (messages.length !== snapshot.messages.length) {
202
+ const replaced = await this.options.store.replace(laneId, messages);
203
+ this.updateStateFromSnapshot(laneId, replaced, this.lanes.get(laneId)?.role ?? "custom", redactions);
204
+ await this.logLaneTrimmed({
205
+ laneId,
206
+ role: this.lanes.get(laneId)?.role ?? "custom",
207
+ beforeMessages,
208
+ afterMessages: replaced.messages.length,
209
+ beforeBytes,
210
+ afterBytes: replaced.byteSize,
211
+ reasons: [
212
+ ...(trimmedByMessages ? ["max_messages"] : []),
213
+ ...(trimmedByBytes ? ["max_bytes"] : []),
214
+ ],
215
+ redactions,
216
+ });
217
+ return replaced;
218
+ }
219
+ return snapshot;
220
+ }
221
+ updateStateFromSnapshot(laneId, snapshot, role, redactions) {
222
+ this.lanes.set(laneId, {
223
+ id: laneId,
224
+ role,
225
+ persisted: true,
226
+ messages: snapshot.messages,
227
+ tokenEstimate: estimateMessagesTokens(snapshot.messages, this.charPerToken),
228
+ updatedAt: snapshot.updatedAt,
229
+ redactions,
230
+ });
231
+ }
232
+ async logLaneUpdate(state) {
233
+ if (!this.options.logger || !this.enabled || !state.persisted)
234
+ return;
235
+ await this.options.logger.log("context_lane_update", {
236
+ laneId: state.id,
237
+ role: state.role,
238
+ messageCount: state.messages.length,
239
+ tokenEstimate: state.tokenEstimate,
240
+ redactionCount: state.redactions,
241
+ });
242
+ }
243
+ async logLaneSummarized(params) {
244
+ if (!this.options.logger || !this.enabled)
245
+ return;
246
+ await this.options.logger.log("context_lane_summarized", {
247
+ laneId: params.laneId,
248
+ role: params.role,
249
+ beforeMessages: params.beforeMessages,
250
+ afterMessages: params.afterMessages,
251
+ beforeTokens: params.beforeTokens,
252
+ afterTokens: params.afterTokens,
253
+ iterations: params.iterations,
254
+ modelLimit: params.modelLimit,
255
+ redactionCount: params.redactions,
256
+ });
257
+ }
258
+ async logLaneTrimmed(params) {
259
+ if (!this.options.logger || !this.enabled)
260
+ return;
261
+ await this.options.logger.log("context_lane_trimmed", {
262
+ laneId: params.laneId,
263
+ role: params.role,
264
+ beforeMessages: params.beforeMessages,
265
+ afterMessages: params.afterMessages,
266
+ beforeBytes: params.beforeBytes,
267
+ afterBytes: params.afterBytes,
268
+ reasons: params.reasons,
269
+ redactionCount: params.redactions,
270
+ });
271
+ }
272
+ }
@@ -0,0 +1,18 @@
1
+ export interface ContextRedactorOptions {
2
+ workspaceRoot: string;
3
+ ignoreFilesFrom: string[];
4
+ redactPatterns: string[];
5
+ }
6
+ export declare class ContextRedactor {
7
+ private options;
8
+ private matchers;
9
+ private regexes;
10
+ constructor(options: ContextRedactorOptions);
11
+ loadIgnoreMatchers(): Promise<void>;
12
+ shouldIgnore(relativePath: string): boolean;
13
+ redact(content: string): {
14
+ content: string;
15
+ redactions: number;
16
+ };
17
+ }
18
+ //# sourceMappingURL=ContextRedactor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContextRedactor.d.ts","sourceRoot":"","sources":["../../src/cognitive/ContextRedactor.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,sBAAsB;IACrC,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,EAAE,MAAM,EAAE,CAAC;CAC1B;AAwBD,qBAAa,eAAe;IAId,OAAO,CAAC,OAAO;IAH3B,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,OAAO,CAAW;gBAEN,OAAO,EAAE,sBAAsB;IAI7C,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IAUzC,YAAY,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO;IAK3C,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE;CAWjE"}
@@ -0,0 +1,53 @@
1
+ import { promises as fs } from "node:fs";
2
+ import path from "node:path";
3
+ const toPosixPath = (input) => input.split(path.sep).join("/");
4
+ const globToRegex = (pattern) => {
5
+ const escaped = pattern
6
+ .replace(/[.+^${}()|[\]\\]/g, "\\$&")
7
+ .replace(/\*/g, ".*")
8
+ .replace(/\?/g, ".");
9
+ return new RegExp(`^${escaped}$`);
10
+ };
11
+ const parseIgnoreFile = async (filePath) => {
12
+ try {
13
+ const content = await fs.readFile(filePath, "utf8");
14
+ return content
15
+ .split(/\r?\n/)
16
+ .map((line) => line.trim())
17
+ .filter((line) => line.length > 0 && !line.startsWith("#") && !line.startsWith("!"));
18
+ }
19
+ catch {
20
+ return [];
21
+ }
22
+ };
23
+ export class ContextRedactor {
24
+ constructor(options) {
25
+ this.options = options;
26
+ this.matchers = [];
27
+ this.regexes = options.redactPatterns.map((pattern) => new RegExp(pattern, "g"));
28
+ }
29
+ async loadIgnoreMatchers() {
30
+ const patterns = [];
31
+ for (const ignoreFile of this.options.ignoreFilesFrom) {
32
+ const ignorePath = path.resolve(this.options.workspaceRoot, ignoreFile);
33
+ const entries = await parseIgnoreFile(ignorePath);
34
+ patterns.push(...entries);
35
+ }
36
+ this.matchers = patterns.map(globToRegex);
37
+ }
38
+ shouldIgnore(relativePath) {
39
+ const normalized = toPosixPath(relativePath);
40
+ return this.matchers.some((matcher) => matcher.test(normalized));
41
+ }
42
+ redact(content) {
43
+ let redactions = 0;
44
+ let output = content;
45
+ for (const regex of this.regexes) {
46
+ output = output.replace(regex, (match) => {
47
+ redactions += 1;
48
+ return "<redacted>";
49
+ });
50
+ }
51
+ return { content: output, redactions };
52
+ }
53
+ }
@@ -0,0 +1,22 @@
1
+ import type { ContextImpactSummary, ContextSelection } from "./Types.js";
2
+ import type { IntentSignals } from "./IntentSignals.js";
3
+ type Hit = {
4
+ path?: string;
5
+ score?: number;
6
+ };
7
+ export interface ContextSelectorOptions {
8
+ maxFiles: number;
9
+ focusCount?: number;
10
+ minHitCount?: number;
11
+ }
12
+ export interface ContextSelectorInput {
13
+ hits: Hit[];
14
+ impact: ContextImpactSummary[];
15
+ intent?: IntentSignals;
16
+ docTask?: boolean;
17
+ recentFiles?: string[];
18
+ preferredFiles?: string[];
19
+ }
20
+ export declare const selectContextFiles: (input: ContextSelectorInput, options: ContextSelectorOptions) => ContextSelection;
21
+ export {};
22
+ //# sourceMappingURL=ContextSelector.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContextSelector.d.ts","sourceRoot":"","sources":["../../src/cognitive/ContextSelector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,gBAAgB,EAKjB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAExD,KAAK,GAAG,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAmH7C,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,GAAG,EAAE,CAAC;IACZ,MAAM,EAAE,oBAAoB,EAAE,CAAC;IAC/B,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B;AAoGD,eAAO,MAAM,kBAAkB,GAC7B,OAAO,oBAAoB,EAC3B,SAAS,sBAAsB,KAC9B,gBAoPF,CAAC"}