cognitivebc 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +70 -0
- package/README.md +98 -4
- package/SECURITY.md +29 -0
- package/TERMS_OF_SERVICE.md +55 -0
- package/bin/cbc-cloud-bootstrap.js +108 -0
- package/bin/cbc-seed-projects.js +29 -0
- package/bin/cbc-server.js +45 -0
- package/bin/cognitivebc.js +251 -0
- package/docs/ACCESS_CONTROL.md +111 -0
- package/docs/ARTIFACT_AUDIT.md +36 -0
- package/docs/CONNECT_CLIENTS.md +116 -0
- package/docs/DEPLOY_AZURE.md +136 -0
- package/docs/INGESTION.md +29 -0
- package/docs/OPERATIONS.md +85 -0
- package/docs/PROJECT_IDENTITY.md +35 -0
- package/docs/RESOLVER.md +86 -0
- package/docs/SDK.md +96 -0
- package/docs/STORAGE.md +63 -0
- package/docs/TECHNICAL_SPECIFICATION.md +365 -0
- package/docs/TELEMETRY.md +119 -0
- package/index.js +27 -2
- package/package.json +45 -5
- package/packages/cbc-compiler/index.js +357 -0
- package/packages/cbc-compiler/sanitizer.js +43 -0
- package/packages/cbc-identity/index.js +92 -0
- package/packages/cbc-mcp/http-backend.js +138 -0
- package/packages/cbc-mcp/http-route.js +64 -0
- package/packages/cbc-mcp/index.js +50 -0
- package/packages/cbc-mcp/tools.js +122 -0
- package/packages/cbc-resolver/catalog-linker.js +215 -0
- package/packages/cbc-resolver/graph-builder.js +533 -0
- package/packages/cbc-resolver/index.js +548 -0
- package/packages/cbc-resolver/obsidian-bridge.js +564 -0
- package/packages/cbc-schema/index.js +194 -0
- package/packages/cbc-sdk-js/client.js +162 -0
- package/services/cbc-api/access-policy.js +130 -0
- package/services/cbc-api/artifact-audit.js +167 -0
- package/services/cbc-api/cloud.js +56 -0
- package/services/cbc-api/context-service.js +48 -0
- package/services/cbc-api/ingestion-service.js +234 -0
- package/services/cbc-api/pg-store.js +172 -0
- package/services/cbc-api/project-registry.js +322 -0
- package/services/cbc-api/public/app.html +123 -0
- package/services/cbc-api/public/graph.html +1168 -0
- package/services/cbc-api/public/graph2.html +455 -0
- package/services/cbc-api/public/graph3.html +503 -0
- package/services/cbc-api/public/index.html +78 -0
- package/services/cbc-api/public/vendor/cbc-app.js +209 -0
- package/services/cbc-api/public/vendor/cbc-auth.js +49 -0
- package/services/cbc-api/public/vendor/cbc-site.css +101 -0
- package/services/cbc-api/public/vendor/cose-base.js +3214 -0
- package/services/cbc-api/public/vendor/cytoscape-dagre.js +397 -0
- package/services/cbc-api/public/vendor/cytoscape-fcose.js +1549 -0
- package/services/cbc-api/public/vendor/cytoscape.min.js +32 -0
- package/services/cbc-api/public/vendor/dagre.min.js +3809 -0
- package/services/cbc-api/public/vendor/graphology-library.min.js +2 -0
- package/services/cbc-api/public/vendor/graphology.umd.min.js +2 -0
- package/services/cbc-api/public/vendor/layout-base.js +5230 -0
- package/services/cbc-api/public/vendor/pf-graph.css +86 -0
- package/services/cbc-api/public/vendor/pf-telemetry.js +176 -0
- package/services/cbc-api/public/vendor/sigma.min.js +1351 -0
- package/services/cbc-api/runtime-info.js +39 -0
- package/services/cbc-api/server.js +642 -0
- package/services/cbc-api/store-loader.js +269 -0
- package/services/cbc-api/telemetry.js +148 -0
- package/services/cbc-api/token-service.js +61 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hyperparameter profiles for JIT Context Resolution (Domain-Agnostic)
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_HYPERPARAMETERS = {
|
|
9
|
+
profile: 'baseline',
|
|
10
|
+
defaultMaxTokens: 600,
|
|
11
|
+
roleMaxTokens: {},
|
|
12
|
+
priorityOrder: ['constraints', 'principles', 'tenant', 'heuristics', 'experience'],
|
|
13
|
+
packingStrategy: 'section_block', // 'section_block' | 'item_granular'
|
|
14
|
+
compressionLevel: 'standard', // 'standard' | 'compact'
|
|
15
|
+
minEvidenceCount: 1,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const TUNED_HYPERPARAMETERS = {
|
|
19
|
+
profile: 'pareto_optimal',
|
|
20
|
+
defaultMaxTokens: 500,
|
|
21
|
+
roleMaxTokens: {},
|
|
22
|
+
priorityOrder: ['constraints', 'principles', 'tenant', 'heuristics', 'experience'],
|
|
23
|
+
packingStrategy: 'item_granular',
|
|
24
|
+
compressionLevel: 'compact',
|
|
25
|
+
minEvidenceCount: 1,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Fast approximate token estimation for English/Portuguese mixed text.
|
|
30
|
+
* Standard heuristic: ~3.8 to 4 characters per token.
|
|
31
|
+
*/
|
|
32
|
+
function estimateTokens(text) {
|
|
33
|
+
if (!text) return 0;
|
|
34
|
+
return Math.ceil(text.length / 3.8);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Compacts markdown text by removing redundant whitespace and verbose labels
|
|
39
|
+
* while preserving 100% of domain rules, constraints, and technical values.
|
|
40
|
+
*/
|
|
41
|
+
function compactText(text) {
|
|
42
|
+
if (!text) return '';
|
|
43
|
+
return text
|
|
44
|
+
.replace(/\r\n/g, '\n')
|
|
45
|
+
.replace(/<!--.*?-->/g, '')
|
|
46
|
+
.replace(/[ \t]+/g, ' ')
|
|
47
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
48
|
+
.trim();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const COMMON_STOPWORDS = new Set([
|
|
52
|
+
'the', 'and', 'for', 'that', 'this', 'with', 'from', 'when', 'are', 'not', 'but', 'have', 'has',
|
|
53
|
+
'was', 'were', 'will', 'would', 'should', 'could', 'can', 'into', 'only', 'also', 'more', 'some',
|
|
54
|
+
'such', 'than', 'then', 'them', 'they', 'their', 'there', 'where', 'which', 'while', 'who',
|
|
55
|
+
'what', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'most', 'other', 'does', 'did',
|
|
56
|
+
'using', 'used', 'use', 'make', 'made', 'like', 'just', 'after', 'before', 'between', 'under',
|
|
57
|
+
'over', 'again', 'further', 'once', 'here', 'very', 'same', 'own', 'too', 'now', 'para', 'com',
|
|
58
|
+
'uma', 'por', 'mais', 'como', 'mas', 'foi', 'ao', 'ele', 'das', 'tem', 'seu', 'sua', 'ou',
|
|
59
|
+
'ser', 'quando', 'muito', 'nos', 'est', 'tamb', 'pelo', 'pela', 'at', 'isso', 'ela', 'entre',
|
|
60
|
+
'depois', 'sem', 'mesmo', 'aos', 'ter', 'seus', 'quem', 'nas', 'esse', 'eles', 'estava', 'foram',
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Tokenizes a query into weighted unique terms, giving higher weight to technical identifiers
|
|
65
|
+
* (snake_case, camelCase, file extensions, paths, and specific domain symbols).
|
|
66
|
+
*/
|
|
67
|
+
function parseQueryTokens(query) {
|
|
68
|
+
if (!query) return [];
|
|
69
|
+
const rawTokens = String(query)
|
|
70
|
+
.split(/[^a-zA-Z0-9_./\u00C0-\u017F-]+/)
|
|
71
|
+
.filter(t => t.length >= 3);
|
|
72
|
+
|
|
73
|
+
const tokenMap = new Map();
|
|
74
|
+
for (const raw of rawTokens) {
|
|
75
|
+
const clean = raw.replace(/^[./-]+|[./-]+$/g, '');
|
|
76
|
+
if (clean.length < 3) continue;
|
|
77
|
+
const lower = clean.toLowerCase();
|
|
78
|
+
if (COMMON_STOPWORDS.has(lower)) continue;
|
|
79
|
+
|
|
80
|
+
const isCodeIdentifier =
|
|
81
|
+
clean.includes('_') ||
|
|
82
|
+
clean.includes('/') ||
|
|
83
|
+
clean.includes('.') ||
|
|
84
|
+
/[a-z][A-Z]/.test(clean);
|
|
85
|
+
|
|
86
|
+
const weight = isCodeIdentifier ? 3.5 : lower.length >= 6 ? 1.5 : 1.0;
|
|
87
|
+
if (!tokenMap.has(lower) || tokenMap.get(lower) < weight) {
|
|
88
|
+
tokenMap.set(lower, weight);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Also index sub-parts of paths or dotted identifiers (e.g. models/fields.py -> fields)
|
|
92
|
+
if (clean.includes('/') || clean.includes('.')) {
|
|
93
|
+
const parts = lower.split(/[/.]+/).filter(p => p.length >= 3 && !COMMON_STOPWORDS.has(p));
|
|
94
|
+
for (const p of parts) {
|
|
95
|
+
if (!tokenMap.has(p) || tokenMap.get(p) < 2.5) {
|
|
96
|
+
tokenMap.set(p, 2.5);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return Array.from(tokenMap.entries());
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Computes lexical & identifier relevance score between a task query and an item's text
|
|
107
|
+
*/
|
|
108
|
+
function computeQueryRelevance(query, itemText, precomputedTokens = null) {
|
|
109
|
+
if (!itemText) return 0;
|
|
110
|
+
const qEntries = precomputedTokens || parseQueryTokens(query);
|
|
111
|
+
if (qEntries.length === 0) return 0;
|
|
112
|
+
return scoreQueryPlan(compileQueryPlan(qEntries), itemText);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function compileQueryPlan(entries) {
|
|
116
|
+
return {
|
|
117
|
+
totalWeight: entries.reduce((sum, [, weight]) => sum + weight, 0),
|
|
118
|
+
terms: entries.map(([token, weight]) => ({ token, weight,
|
|
119
|
+
boundaries: token.length >= 4 ? [`/${token}.`, `/${token}/`, ` ${token},`, `: ${token}`] : [],
|
|
120
|
+
})),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function scoreQueryPlan(plan, itemText) {
|
|
125
|
+
const targetLower = itemText.toLowerCase();
|
|
126
|
+
let matchedWeight = 0;
|
|
127
|
+
for (const { token, weight, boundaries } of plan.terms) {
|
|
128
|
+
if (targetLower.includes(token)) {
|
|
129
|
+
// Bonus if the token matches a file stem (e.g., /query.py) or explicit symbol boundary in the rule
|
|
130
|
+
const isStructuralHit =
|
|
131
|
+
boundaries.some(boundary => targetLower.includes(boundary));
|
|
132
|
+
matchedWeight += isStructuralHit ? weight * 2.2 : weight;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return plan.totalWeight > 0 ? matchedWeight / plan.totalWeight : 0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Extracts sections from a Tenant Map markdown document that apply to `role`
|
|
140
|
+
* via declarative `<!-- roles: role1, role2 -->` or `[roles: role1, role2]` annotations.
|
|
141
|
+
*/
|
|
142
|
+
function extractTenantSectionsForRole(mapContent, role) {
|
|
143
|
+
if (!mapContent || typeof mapContent !== 'string') return '';
|
|
144
|
+
// An annotation applies to its heading block (including descendants), or to
|
|
145
|
+
// the preamble when no heading exists. Never reopen a denied parent block.
|
|
146
|
+
const annotation = /<!--\s*roles:\s*([\s\S]*?)\s*-->|\[\s*roles:\s*([^\]]+?)\s*\]/gi;
|
|
147
|
+
const blocks = [];
|
|
148
|
+
let lines = [];
|
|
149
|
+
let fence = null;
|
|
150
|
+
for (const line of mapContent.split('\n')) {
|
|
151
|
+
const marker = line.match(/^\s{0,3}(`{3,}|~{3,})/);
|
|
152
|
+
if (!fence && /^#{1,6}\s+/.test(line) && lines.length) {
|
|
153
|
+
blocks.push(lines.join('\n'));
|
|
154
|
+
lines = [];
|
|
155
|
+
}
|
|
156
|
+
lines.push(line);
|
|
157
|
+
if (marker) {
|
|
158
|
+
if (!fence) fence = marker[1];
|
|
159
|
+
else if (marker[1][0] === fence[0] && marker[1].length >= fence.length && /^\s*$/.test(line.slice(marker[0].length))) fence = null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (lines.length) blocks.push(lines.join('\n'));
|
|
163
|
+
const parents = [];
|
|
164
|
+
const output = [];
|
|
165
|
+
const roleLower = String(role).toLowerCase();
|
|
166
|
+
for (const block of blocks) {
|
|
167
|
+
const level = block.match(/^(#{1,6})\s+/)?.[1].length || 0;
|
|
168
|
+
while (parents.length && parents[parents.length - 1].level >= level) parents.pop();
|
|
169
|
+
let allowed = parents.every(p => p.allowed);
|
|
170
|
+
const cleaned = block.replace(annotation, (_, comment, bracket) => {
|
|
171
|
+
const roles = (comment ?? bracket).split(',').map(r => r.trim().toLowerCase());
|
|
172
|
+
allowed = allowed && (roles.includes('*') || roles.includes(roleLower));
|
|
173
|
+
return '';
|
|
174
|
+
});
|
|
175
|
+
parents.push({ level, allowed });
|
|
176
|
+
if (allowed && cleaned.trim()) output.push(cleaned.trim());
|
|
177
|
+
}
|
|
178
|
+
return output.join('\n\n');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Resolves context JIT for a given role within scope constraints (Domain-Agnostic)
|
|
183
|
+
*/
|
|
184
|
+
function resolveContextForRole({
|
|
185
|
+
tenant = null,
|
|
186
|
+
squad = null,
|
|
187
|
+
area = null,
|
|
188
|
+
role,
|
|
189
|
+
stacks = [],
|
|
190
|
+
subscopes = [],
|
|
191
|
+
query = null,
|
|
192
|
+
maxTokens = null,
|
|
193
|
+
bytecodes = [],
|
|
194
|
+
maps = {},
|
|
195
|
+
skills = {},
|
|
196
|
+
hyperparameters = null,
|
|
197
|
+
}) {
|
|
198
|
+
if (!role) {
|
|
199
|
+
throw new Error('resolveContextForRole: "role" is required');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Isolamento de tenant: bytecode de nível tenant (scope.tenant preenchido) só serve ao próprio tenant.
|
|
203
|
+
// Sem isto, regras de um cliente vazariam para papéis de outro que compartilham o mesmo nome de papel.
|
|
204
|
+
bytecodes = (bytecodes || []).filter(bc => !bc?.scope?.tenant || bc.scope.tenant === tenant);
|
|
205
|
+
|
|
206
|
+
const baseProfile =
|
|
207
|
+
hyperparameters?.profile === 'baseline' ? DEFAULT_HYPERPARAMETERS : TUNED_HYPERPARAMETERS;
|
|
208
|
+
|
|
209
|
+
const hp = {
|
|
210
|
+
...baseProfile,
|
|
211
|
+
...(hyperparameters || {}),
|
|
212
|
+
roleMaxTokens: {
|
|
213
|
+
...baseProfile.roleMaxTokens,
|
|
214
|
+
...(hyperparameters?.roleMaxTokens || {}),
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Discover role budget declaratively from matching bytecodes if not explicitly overridden
|
|
219
|
+
let declarativeRoleBudget = null;
|
|
220
|
+
for (const bc of bytecodes) {
|
|
221
|
+
if (stacks.length > 0 && bc.scope?.stack && !stacks.includes(bc.scope.stack)) continue;
|
|
222
|
+
if (bc.scope?.role_budgets && typeof bc.scope.role_budgets[role] === 'number') {
|
|
223
|
+
declarativeRoleBudget = bc.scope.role_budgets[role];
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const requestedBudget = [maxTokens, hp.roleMaxTokens[role], declarativeRoleBudget,
|
|
229
|
+
hp.roleMaxTokens.default, hp.defaultMaxTokens, 600]
|
|
230
|
+
.find(value => typeof value === 'number' && Number.isFinite(value));
|
|
231
|
+
const effectiveMaxTokens = Math.min(2000, Math.max(100, Math.floor(requestedBudget)));
|
|
232
|
+
|
|
233
|
+
const isCompact = hp.compressionLevel === 'compact';
|
|
234
|
+
const isGranular = hp.packingStrategy === 'item_granular';
|
|
235
|
+
|
|
236
|
+
const principles = [];
|
|
237
|
+
const constraints = [];
|
|
238
|
+
const heuristics = [];
|
|
239
|
+
const experienceItems = [];
|
|
240
|
+
const mcpResources = [];
|
|
241
|
+
|
|
242
|
+
if (squad && area) {
|
|
243
|
+
mcpResources.push(`cbc://skills/${squad}/${area}`);
|
|
244
|
+
}
|
|
245
|
+
if (stacks && stacks.length > 0) {
|
|
246
|
+
stacks.forEach(s => mcpResources.push(`cbc://bytecode/${s}`));
|
|
247
|
+
}
|
|
248
|
+
if (tenant) {
|
|
249
|
+
mcpResources.push(`cbc://map/${tenant}/facts`);
|
|
250
|
+
mcpResources.push(`cbc://map/${tenant}/verified-claims`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 1. Process Bytecodes for target_roles
|
|
254
|
+
for (const bc of bytecodes) {
|
|
255
|
+
if (stacks.length > 0 && bc.scope?.stack && !stacks.includes(bc.scope.stack)) {
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (Array.isArray(bc.decision_principles)) {
|
|
260
|
+
for (const dp of bc.decision_principles) {
|
|
261
|
+
if (!dp.target_roles || dp.target_roles.includes(role) || dp.target_roles.includes('*')) {
|
|
262
|
+
principles.push(dp);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (Array.isArray(bc.agent_constraints)) {
|
|
268
|
+
for (const ac of bc.agent_constraints) {
|
|
269
|
+
if (!ac.target_roles || ac.target_roles.includes(role) || ac.target_roles.includes('*')) {
|
|
270
|
+
constraints.push(ac);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (Array.isArray(bc.agent_heuristics)) {
|
|
276
|
+
for (const ah of bc.agent_heuristics) {
|
|
277
|
+
const evCount = ah.evidence_count ?? 1;
|
|
278
|
+
if (evCount < hp.minEvidenceCount) continue;
|
|
279
|
+
if (!ah.target_roles || ah.target_roles.includes(role) || ah.target_roles.includes('*')) {
|
|
280
|
+
heuristics.push(ah);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (Array.isArray(bc.experience_graph)) {
|
|
286
|
+
for (const eg of bc.experience_graph) {
|
|
287
|
+
if (!eg.target_roles || eg.target_roles.includes(role) || eg.target_roles.includes('*')) {
|
|
288
|
+
experienceItems.push(eg);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const hasSubscopes = Array.isArray(subscopes) && subscopes.length > 0;
|
|
295
|
+
const subscopesLower = hasSubscopes ? subscopes.map(s => String(s).toLowerCase()) : [];
|
|
296
|
+
const matchesSubscope = item => {
|
|
297
|
+
if (!hasSubscopes || !item?.scope) return false;
|
|
298
|
+
const itemScopeLower = String(item.scope).toLowerCase();
|
|
299
|
+
return subscopesLower.some(s => itemScopeLower.includes(s));
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const qEntries = query ? parseQueryTokens(query) : [];
|
|
303
|
+
const queryPlan = compileQueryPlan(qEntries);
|
|
304
|
+
const scoreCache = new WeakMap();
|
|
305
|
+
const getItemScore = (item, textFn) => {
|
|
306
|
+
if (!hasSubscopes && qEntries.length === 0) return 0;
|
|
307
|
+
if (scoreCache.has(item)) return scoreCache.get(item);
|
|
308
|
+
let score = 0;
|
|
309
|
+
if (qEntries.length > 0) {
|
|
310
|
+
score += scoreQueryPlan(queryPlan, textFn(item));
|
|
311
|
+
}
|
|
312
|
+
if (hasSubscopes && matchesSubscope(item)) {
|
|
313
|
+
score += qEntries.length > 0 ? 0.12 : 1.0;
|
|
314
|
+
}
|
|
315
|
+
scoreCache.set(item, score);
|
|
316
|
+
return score;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
// Sort items by: 1) combined sub-scope + query relevance score, 2) evidence_count descending
|
|
320
|
+
const rankByRelevanceAndEvidence = (a, b, textFn) => {
|
|
321
|
+
if (hasSubscopes || qEntries.length > 0) {
|
|
322
|
+
const scoreA = getItemScore(a, textFn);
|
|
323
|
+
const scoreB = getItemScore(b, textFn);
|
|
324
|
+
if (scoreB !== scoreA) return scoreB - scoreA;
|
|
325
|
+
}
|
|
326
|
+
return (b.evidence_count || 1) - (a.evidence_count || 1);
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
constraints.sort((a, b) => rankByRelevanceAndEvidence(a, b, x => `${x.id} ${x.rule}`));
|
|
330
|
+
principles.sort((a, b) => rankByRelevanceAndEvidence(a, b, x => `${x.id} ${x.rule}`));
|
|
331
|
+
heuristics.sort((a, b) => rankByRelevanceAndEvidence(a, b, x => `${x.id} ${x.when} ${x.then} ${x.scope || ''}`));
|
|
332
|
+
experienceItems.sort((a, b) =>
|
|
333
|
+
rankByRelevanceAndEvidence(a, b, x => `${x.problem_id} ${x.symptom} ${x.root_cause}`)
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
// 2. Add Tenant Map relevant section declaratively if tenant map is available
|
|
337
|
+
let tenantSection = '';
|
|
338
|
+
if (tenant && maps[tenant]) {
|
|
339
|
+
const extracted = extractTenantSectionsForRole(maps[tenant], role);
|
|
340
|
+
if (extracted) {
|
|
341
|
+
tenantSection = isCompact
|
|
342
|
+
? `\n### Tenant (${tenant})\n${compactText(extracted)}\n`
|
|
343
|
+
: `\n### Regras do Tenant (${tenant})\n${extracted}\n`;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 3. Assemble Push Markdown within token budget
|
|
348
|
+
const priorityMap = {};
|
|
349
|
+
hp.priorityOrder.forEach((name, idx) => {
|
|
350
|
+
priorityMap[name] = idx + 1;
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const sections = [];
|
|
354
|
+
|
|
355
|
+
if (constraints.length > 0) {
|
|
356
|
+
const header = isCompact ? '### Constraints\n' : '### Restrições Rígidas (agent_constraints)\n';
|
|
357
|
+
const items = constraints.map(c => ({
|
|
358
|
+
id: c.id,
|
|
359
|
+
scope: c.scope || null,
|
|
360
|
+
score: scoreCache.get(c) ?? getItemScore(c, x => `${x.id} ${x.rule}`),
|
|
361
|
+
source: c,
|
|
362
|
+
}));
|
|
363
|
+
sections.push({
|
|
364
|
+
name: 'constraints',
|
|
365
|
+
render: c => `- **[${c.id}]** ${isCompact ? compactText(c.rule) : c.rule}\n`,
|
|
366
|
+
header,
|
|
367
|
+
items,
|
|
368
|
+
priority: priorityMap.constraints || 1,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (principles.length > 0) {
|
|
373
|
+
const header = isCompact ? '### Principles\n' : '### Princípios Decisórios (decision_principles)\n';
|
|
374
|
+
const items = principles.map(p => ({
|
|
375
|
+
id: p.id,
|
|
376
|
+
scope: p.scope || null,
|
|
377
|
+
score: scoreCache.get(p) ?? getItemScore(p, x => `${x.id} ${x.rule}`),
|
|
378
|
+
source: p,
|
|
379
|
+
}));
|
|
380
|
+
sections.push({
|
|
381
|
+
name: 'principles',
|
|
382
|
+
render: p => `- **[${p.id}]** ${isCompact ? compactText(p.rule) : p.rule}\n`,
|
|
383
|
+
header,
|
|
384
|
+
items,
|
|
385
|
+
priority: priorityMap.principles || 2,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (tenantSection) {
|
|
390
|
+
sections.push({
|
|
391
|
+
name: 'tenant',
|
|
392
|
+
header: '',
|
|
393
|
+
items: [{ id: null, scope: null, score: 1, line: tenantSection }],
|
|
394
|
+
priority: priorityMap.tenant || 3,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (heuristics.length > 0) {
|
|
399
|
+
const header = isCompact ? '### Heuristics\n' : '### Heurísticas Operacionais (agent_heuristics)\n';
|
|
400
|
+
const items = heuristics.map(h => ({
|
|
401
|
+
id: h.id,
|
|
402
|
+
scope: h.scope || null,
|
|
403
|
+
score: scoreCache.get(h) ?? getItemScore(h, x => `${x.id} ${x.when} ${x.then} ${x.scope || ''}`),
|
|
404
|
+
source: h,
|
|
405
|
+
}));
|
|
406
|
+
sections.push({
|
|
407
|
+
name: 'heuristics',
|
|
408
|
+
render: h => isCompact
|
|
409
|
+
? `- **[${h.id}]** ${compactText(h.when)} => ${compactText(h.then)}\n`
|
|
410
|
+
: `- **[${h.id}]** Quando: ${h.when} → Então: ${h.then}\n`,
|
|
411
|
+
header,
|
|
412
|
+
items,
|
|
413
|
+
priority: priorityMap.heuristics || 4,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (experienceItems.length > 0) {
|
|
418
|
+
const header = isCompact ? '### Experience Graph\n' : '### Casos Históricos (experience_graph)\n';
|
|
419
|
+
const items = experienceItems.map(eg => {
|
|
420
|
+
return {
|
|
421
|
+
id: eg.problem_id,
|
|
422
|
+
scope: eg.scope || null,
|
|
423
|
+
score: scoreCache.get(eg) ?? getItemScore(eg, x => `${x.problem_id} ${x.symptom} ${x.root_cause}`),
|
|
424
|
+
source: eg,
|
|
425
|
+
};
|
|
426
|
+
});
|
|
427
|
+
sections.push({
|
|
428
|
+
name: 'experience',
|
|
429
|
+
render: eg => {
|
|
430
|
+
const sol = eg.solutions_evaluated?.find(s => s.outcome === 'adopted')?.option || eg.root_cause;
|
|
431
|
+
return isCompact
|
|
432
|
+
? `- **[${eg.problem_id}]** ${compactText(eg.symptom)} => Fix: ${compactText(sol)}\n`
|
|
433
|
+
: `- **[${eg.problem_id}]** Sintoma: ${eg.symptom} | Solução: ${sol}\n`;
|
|
434
|
+
},
|
|
435
|
+
header,
|
|
436
|
+
items,
|
|
437
|
+
priority: priorityMap.experience || 5,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
sections.sort((a, b) => a.priority - b.priority);
|
|
442
|
+
|
|
443
|
+
let baseHeader =
|
|
444
|
+
(isCompact ? `# CBC JIT [${role}]\n` : `# Cognitive Bytecode JIT — Papel: ${role}\n`) +
|
|
445
|
+
(!isCompact && stacks.length > 0 ? `> Stacks: ${stacks.join(', ')} | Squad: ${squad || 'geral'}\n\n` : '');
|
|
446
|
+
|
|
447
|
+
// Caller-controlled role/stack names must not consume an unbounded budget.
|
|
448
|
+
if (estimateTokens(baseHeader) > effectiveMaxTokens) baseHeader = '# CBC JIT\n';
|
|
449
|
+
|
|
450
|
+
let assembledText = baseHeader;
|
|
451
|
+
const packedIds = [];
|
|
452
|
+
const omissions = [];
|
|
453
|
+
const getLine = (item, section) => item.line ?? (item.line = section.render(item.source));
|
|
454
|
+
|
|
455
|
+
if (!isGranular) {
|
|
456
|
+
for (const sec of sections) {
|
|
457
|
+
const candidate = assembledText + sec.header + sec.items.map(item => getLine(item, sec)).join('') + '\n';
|
|
458
|
+
if (estimateTokens(candidate) <= effectiveMaxTokens) {
|
|
459
|
+
assembledText = candidate;
|
|
460
|
+
sec.items.forEach(item => {
|
|
461
|
+
item._packed = true;
|
|
462
|
+
if (item.id) packedIds.push(item.id);
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
} else {
|
|
467
|
+
// Incremental O(N) granular packing:
|
|
468
|
+
// Pass 1: Items with positive combined relevance/sub-scope score (ordered by section priority & score)
|
|
469
|
+
// Pass 2: Remaining items (ordered by section priority & evidence_count) if budget allows
|
|
470
|
+
const packedBySection = new Map();
|
|
471
|
+
sections.forEach(sec => packedBySection.set(sec.name, []));
|
|
472
|
+
|
|
473
|
+
let currentCharLen = baseHeader.length;
|
|
474
|
+
const maxCharBudget = Math.floor(effectiveMaxTokens * 3.8);
|
|
475
|
+
|
|
476
|
+
const passes = hasSubscopes || qEntries.length > 0 ? ['scored_only', 'fallback_rest'] : ['fallback_rest'];
|
|
477
|
+
|
|
478
|
+
for (const pass of passes) {
|
|
479
|
+
for (const sec of sections) {
|
|
480
|
+
const currentList = packedBySection.get(sec.name);
|
|
481
|
+
for (const item of sec.items) {
|
|
482
|
+
if (item._packed) continue;
|
|
483
|
+
if (pass === 'scored_only' && item.id && item.score <= 0) continue;
|
|
484
|
+
|
|
485
|
+
// Even an empty rule needs its ID marker. This conservative lower
|
|
486
|
+
// bound skips rendering only when fitting is provably impossible.
|
|
487
|
+
const headerChars = currentList.length === 0 ? sec.header.length + 1 : 0;
|
|
488
|
+
if (item.id && currentCharLen + headerChars + String(item.id).length + 10 > maxCharBudget) continue;
|
|
489
|
+
|
|
490
|
+
const extraChars =
|
|
491
|
+
headerChars + getLine(item, sec).length;
|
|
492
|
+
|
|
493
|
+
if (Math.ceil((currentCharLen + extraChars) / 3.8) <= effectiveMaxTokens && currentCharLen + extraChars <= maxCharBudget + 3) {
|
|
494
|
+
currentList.push(item);
|
|
495
|
+
currentCharLen += extraChars;
|
|
496
|
+
item._packed = true;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
assembledText = baseHeader;
|
|
503
|
+
for (const s of sections) {
|
|
504
|
+
const sItems = packedBySection.get(s.name);
|
|
505
|
+
if (sItems.length > 0) {
|
|
506
|
+
assembledText += s.header + sItems.map(i => i.line).join('') + '\n';
|
|
507
|
+
sItems.forEach(item => { if (item.id) packedIds.push(item.id); });
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
for (const sec of sections) {
|
|
513
|
+
for (const item of sec.items) {
|
|
514
|
+
if (!item._packed) omissions.push({ section: sec.name, id: item.id, reason: 'budget' });
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const appliedIds = packedIds;
|
|
518
|
+
assembledText = assembledText.trim();
|
|
519
|
+
const tokenCount = estimateTokens(assembledText);
|
|
520
|
+
const digestSha256 = crypto.createHash('sha256').update(assembledText, 'utf8').digest('hex');
|
|
521
|
+
|
|
522
|
+
return {
|
|
523
|
+
push_markdown: assembledText.trim(),
|
|
524
|
+
token_count: tokenCount,
|
|
525
|
+
max_tokens_budget: effectiveMaxTokens,
|
|
526
|
+
hyperparameter_profile: hp.profile,
|
|
527
|
+
digest_sha256: digestSha256,
|
|
528
|
+
applied_ids: appliedIds,
|
|
529
|
+
omitted_ids: omissions.filter(item => item.id).map(item => item.id),
|
|
530
|
+
omissions,
|
|
531
|
+
mcp_resources: mcpResources,
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const { buildSemanticGraph } = require('./graph-builder');
|
|
536
|
+
const { exportToObsidianVault, importFromObsidianVault } = require('./obsidian-bridge');
|
|
537
|
+
|
|
538
|
+
module.exports = {
|
|
539
|
+
DEFAULT_HYPERPARAMETERS,
|
|
540
|
+
TUNED_HYPERPARAMETERS,
|
|
541
|
+
estimateTokens,
|
|
542
|
+
computeQueryRelevance,
|
|
543
|
+
extractTenantSectionsForRole,
|
|
544
|
+
resolveContextForRole,
|
|
545
|
+
buildSemanticGraph,
|
|
546
|
+
exportToObsidianVault,
|
|
547
|
+
importFromObsidianVault,
|
|
548
|
+
};
|