cognitivebc 0.0.0-stage → 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 +99 -2
- 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 -0
- package/package.json +59 -4
- 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,269 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const crypto = require('crypto');
|
|
7
|
+
const YAML = require('yaml');
|
|
8
|
+
const { validateBytecode, calculateSha256 } = require('../../packages/cbc-schema');
|
|
9
|
+
const DEFAULT_PRIVATE_STORE = path.join(os.homedir(), '.cognitivebc', 'private-store');
|
|
10
|
+
const TYPES = { bytecode: ['bytecodes', '.cbc.yaml'], skill: ['skills', '.SKILL.md'], map: ['maps', '.md'] };
|
|
11
|
+
const PREFIX = '__cbc_id_';
|
|
12
|
+
function resolvePrivateDirs(option) {
|
|
13
|
+
if (option === false) return [];
|
|
14
|
+
if (Array.isArray(option)) return option;
|
|
15
|
+
const env = typeof option === 'string' ? option : process.env.COGNITIVEBC_PRIVATE_STORE;
|
|
16
|
+
if (env === 'off') return [];
|
|
17
|
+
return env ? env.split(path.delimiter).filter(Boolean) : [DEFAULT_PRIVATE_STORE];
|
|
18
|
+
}
|
|
19
|
+
function identifier(id) {
|
|
20
|
+
if (typeof id !== 'string' || !id.trim() || Buffer.byteLength(id, 'utf8') > 96 || Buffer.from(id).toString('utf8') !== id || /[\x00-\x1f\x7f]/.test(id)) throw new Error('Invalid artifact ID');
|
|
21
|
+
return id;
|
|
22
|
+
}
|
|
23
|
+
function filename(id) { return PREFIX + Buffer.from(identifier(id)).toString('hex'); }
|
|
24
|
+
function decode(id) { return id.startsWith(PREFIX) ? Buffer.from(id.slice(PREFIX.length), 'hex').toString() : id; }
|
|
25
|
+
function stable(value) {
|
|
26
|
+
if (Array.isArray(value)) return value.map(stable);
|
|
27
|
+
if (value && typeof value === 'object') return Object.fromEntries(Object.keys(value).sort().map(k => [k, stable(value[k])]));
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function tenantScope(scope) {
|
|
31
|
+
if (!scope || scope.level !== 'tenant') throw new Error('Only tenant publication is permitted; global/shared requires a sanitization policy');
|
|
32
|
+
identifier(scope.tenant);
|
|
33
|
+
return scope;
|
|
34
|
+
}
|
|
35
|
+
// Reject symlinks at every existing component, including the configured root.
|
|
36
|
+
function safePath(file) {
|
|
37
|
+
const absolute = path.resolve(file);
|
|
38
|
+
let current = path.parse(absolute).root;
|
|
39
|
+
for (const part of absolute.slice(current.length).split(path.sep)) {
|
|
40
|
+
current = path.join(current, part);
|
|
41
|
+
try { if (fs.lstatSync(current).isSymbolicLink()) throw new Error(`Symlink forbidden: ${current}`); }
|
|
42
|
+
catch (err) { if (err.code !== 'ENOENT') throw err; }
|
|
43
|
+
}
|
|
44
|
+
return absolute;
|
|
45
|
+
}
|
|
46
|
+
function atomicWrite(file, raw, privateFile = false) {
|
|
47
|
+
safePath(file);
|
|
48
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
49
|
+
const temp = `${file}.${crypto.randomBytes(12).toString('hex')}.tmp`;
|
|
50
|
+
let fd;
|
|
51
|
+
try {
|
|
52
|
+
fd = fs.openSync(temp, 'wx', privateFile ? 0o600 : 0o644);
|
|
53
|
+
fs.writeFileSync(fd, raw, 'utf8');
|
|
54
|
+
fs.fsyncSync(fd);
|
|
55
|
+
fs.closeSync(fd); fd = undefined;
|
|
56
|
+
fs.renameSync(temp, file);
|
|
57
|
+
} finally {
|
|
58
|
+
if (fd !== undefined) fs.closeSync(fd);
|
|
59
|
+
try { fs.unlinkSync(temp); } catch (err) { if (err.code !== 'ENOENT') throw err; }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
class CognitiveStore {
|
|
63
|
+
constructor(storeDir, options = {}) {
|
|
64
|
+
this.storeDir = path.resolve(storeDir || path.resolve(__dirname, '../../store'));
|
|
65
|
+
const opt = options.privateDirs !== undefined ? options.privateDirs : (storeDir ? false : undefined);
|
|
66
|
+
this.privateDirs = resolvePrivateDirs(opt).map(d => path.resolve(d)).filter(d => d !== this.storeDir);
|
|
67
|
+
this.bytecodes = new Map(); this.skills = new Map(); this.maps = new Map();
|
|
68
|
+
this.loadErrors = [];
|
|
69
|
+
}
|
|
70
|
+
roots() { return [{ dir: this.storeDir, private: false }, ...this.privateDirs.map(dir => ({ dir, private: true }))]; }
|
|
71
|
+
_collection(type) {
|
|
72
|
+
if (!Object.hasOwn(TYPES, type)) throw new Error('Unknown artifact type');
|
|
73
|
+
return type === 'bytecode' ? this.bytecodes : type === 'skill' ? this.skills : this.maps;
|
|
74
|
+
}
|
|
75
|
+
_prepare(type, id, content) {
|
|
76
|
+
this._collection(type); identifier(id);
|
|
77
|
+
let raw, data;
|
|
78
|
+
if (type === 'bytecode') {
|
|
79
|
+
raw = typeof content === 'string' ? content : YAML.stringify(content);
|
|
80
|
+
data = YAML.parse(raw);
|
|
81
|
+
const validation = validateBytecode(data);
|
|
82
|
+
if (!validation.valid) throw new Error(`Invalid bytecode: ${validation.errors.join(', ')}`);
|
|
83
|
+
if (data.id !== id) throw new Error('Bytecode ID mismatch');
|
|
84
|
+
if (['tenant', 'role'].includes(data.scope.level)) identifier(data.scope.tenant);
|
|
85
|
+
} else {
|
|
86
|
+
if (typeof content !== 'string' || !content.trim()) throw new Error('Markdown artifact must be a nonempty string');
|
|
87
|
+
raw = content;
|
|
88
|
+
const tenant = type === 'map' && raw.match(/tenant:\s*([^\n\r]+)/i);
|
|
89
|
+
if (tenant && tenant[1].trim() !== id) throw new Error('Map tenant mismatch');
|
|
90
|
+
}
|
|
91
|
+
return { id, raw, data, sha256: calculateSha256(raw) };
|
|
92
|
+
}
|
|
93
|
+
_install(type, prepared, location) {
|
|
94
|
+
const { id, raw, data, sha256 } = prepared;
|
|
95
|
+
const entry = type === 'bytecode' ? { id, data, raw, sha256 } : type === 'skill' ? { id, content: raw, sha256 } : { tenant: id, content: raw, sha256 };
|
|
96
|
+
if (data) Object.defineProperty(data, '_file_mtime', { value: location.mtime || new Date().toISOString(), enumerable: false });
|
|
97
|
+
this._collection(type).set(id, { ...entry, ...location });
|
|
98
|
+
}
|
|
99
|
+
loadAll() {
|
|
100
|
+
this.bytecodes.clear(); this.skills.clear(); this.maps.clear(); this.loadErrors = [];
|
|
101
|
+
for (const root of this.roots()) {
|
|
102
|
+
this._loadRoot(root);
|
|
103
|
+
const directory = path.join(root.dir, '.candidates');
|
|
104
|
+
this._scan(directory, '.json', file => {
|
|
105
|
+
const record = this._readCandidate(file);
|
|
106
|
+
if (record.status === 'promoted') this._publish(record, root.dir, file);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return { bytecodesCount: this.bytecodes.size, skillsCount: this.skills.size, mapsCount: this.maps.size,
|
|
110
|
+
privateRoots: this.privateDirs.filter(d => fs.existsSync(d)), integrity: this.loadErrors.length === 0,
|
|
111
|
+
errors: this.loadErrors.slice() };
|
|
112
|
+
}
|
|
113
|
+
_scan(dir, extension, visit) {
|
|
114
|
+
try {
|
|
115
|
+
safePath(dir);
|
|
116
|
+
if (!fs.existsSync(dir)) return;
|
|
117
|
+
for (const name of fs.readdirSync(dir).sort()) {
|
|
118
|
+
if (!(extension instanceof RegExp ? extension.test(name) : name.endsWith(extension))) continue;
|
|
119
|
+
const file = path.join(dir, name);
|
|
120
|
+
try { safePath(file); visit(file, name); }
|
|
121
|
+
catch (err) { this.loadErrors.push({ file, message: err.message, code: err.code || 'INVALID_ARTIFACT' }); }
|
|
122
|
+
}
|
|
123
|
+
} catch (err) { this.loadErrors.push({ file: dir, message: err.message, code: err.code || 'INVALID_PATH' }); }
|
|
124
|
+
}
|
|
125
|
+
_loadRoot(root) {
|
|
126
|
+
for (const [type, [directory]] of Object.entries(TYPES)) this._scan(path.join(root.dir, directory), type === 'bytecode' ? /\.ya?ml$/ : '.md', (file, name) => {
|
|
127
|
+
const raw = fs.readFileSync(file, 'utf8');
|
|
128
|
+
let id;
|
|
129
|
+
if (type === 'bytecode') id = YAML.parse(raw)?.id;
|
|
130
|
+
else if (type === 'skill') id = decode(name.replace(/(?:\.SKILL)?\.md$/, ''));
|
|
131
|
+
else id = raw.match(/tenant:\s*([^\n\r]+)/i)?.[1].trim() || decode(name.replace(/^MAP_/, '').replace(/\.md$/, ''));
|
|
132
|
+
const prepared = this._prepare(type, id, raw);
|
|
133
|
+
const previous = this._collection(type).get(id);
|
|
134
|
+
if (previous && previous.root === root.dir) throw new Error(`Duplicate ${type} ID: ${id}`);
|
|
135
|
+
this._install(type, prepared, { root: root.dir, private: root.private, file, mtime: fs.statSync(file).mtime.toISOString() });
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
getBytecodes() { return [...this.bytecodes.values()].map(b => b.data); }
|
|
139
|
+
getPublicBytecodes() { return [...this.bytecodes.values()].filter(b => !b.private).map(b => b.data); }
|
|
140
|
+
getBytecode(id) { return this.bytecodes.get(id); }
|
|
141
|
+
getSkill(id) { return this.skills.get(id); }
|
|
142
|
+
getMap(tenant) { return this.maps.get(tenant)?.content || null; }
|
|
143
|
+
getMapsDict() { return Object.fromEntries([...this.maps].map(([id, v]) => [id, v.content])); }
|
|
144
|
+
getPublicMapsDict() { return Object.fromEntries([...this.maps].filter(([, v]) => !v.private).map(([id, v]) => [id, v.content])); }
|
|
145
|
+
isPrivate(type, id) { return Boolean(this._collection(type).get(id)?.private); }
|
|
146
|
+
saveArtifact(type, id, content) {
|
|
147
|
+
const prepared = this._prepare(type, id, content);
|
|
148
|
+
const prev = this._collection(type).get(id);
|
|
149
|
+
if (prev?.candidateId) throw new Error('Published bundles are immutable; submit a new reviewed candidate');
|
|
150
|
+
const root = prev?.root || this.storeDir;
|
|
151
|
+
const [directory, suffix] = TYPES[type];
|
|
152
|
+
const file = prev?.file || path.join(root, directory, `${filename(id)}${suffix}`);
|
|
153
|
+
atomicWrite(file, prepared.raw, Boolean(prev?.private));
|
|
154
|
+
this._install(type, prepared, { root, private: Boolean(prev?.private), file });
|
|
155
|
+
return { id, type, sha256: prepared.sha256, private: Boolean(prev?.private) };
|
|
156
|
+
}
|
|
157
|
+
_candidateFile(id) {
|
|
158
|
+
if (typeof id !== 'string' || !/^candidate_[a-f0-9]{64}$/.test(id)) throw new Error('Invalid candidate ID');
|
|
159
|
+
return path.join(this.storeDir, '.candidates', `${id}.json`);
|
|
160
|
+
}
|
|
161
|
+
_candidatePayload(input) {
|
|
162
|
+
const value = JSON.parse(JSON.stringify(input));
|
|
163
|
+
tenantScope(value.scope);
|
|
164
|
+
if (!value.evidence || typeof value.evidence !== 'object' || !Object.keys(value.evidence).length) throw new Error('Candidate requires origin evidence');
|
|
165
|
+
if (typeof value.regime !== 'string' || !value.regime.trim()) throw new Error('Candidate requires regime');
|
|
166
|
+
const a = value.artifacts;
|
|
167
|
+
if (!a || !a.bytecode_artifact) throw new Error('Candidate requires bytecode_artifact');
|
|
168
|
+
const bytecode = this._prepare('bytecode', typeof a.bytecode_artifact === 'string' ? YAML.parse(a.bytecode_artifact)?.id : a.bytecode_artifact.id, a.bytecode_artifact);
|
|
169
|
+
if (bytecode.data.scope.tenant !== value.scope.tenant || bytecode.data.scope.level !== 'tenant') throw new Error('Candidate bytecode must match origin tenant scope');
|
|
170
|
+
let skill = a.skill_artifact ?? null, map = a.map_artifact ?? null;
|
|
171
|
+
// Compiler pioneer output uses structured Markdown wrappers.
|
|
172
|
+
if (skill && typeof skill === 'object') {
|
|
173
|
+
if (typeof skill.content !== 'string' || !skill.content.trim() || !skill.frontmatter || typeof skill.frontmatter !== 'object' || Array.isArray(skill.frontmatter)) throw new Error('Invalid compiler skill artifact');
|
|
174
|
+
skill = `---\n${YAML.stringify(skill.frontmatter)}---\n\n${skill.content}`;
|
|
175
|
+
}
|
|
176
|
+
if (map && typeof map === 'object') {
|
|
177
|
+
if (map.tenant !== value.scope.tenant || typeof map.content !== 'string' || !map.content.trim()) throw new Error('Invalid compiler map artifact');
|
|
178
|
+
map = `tenant: ${map.tenant}\n\n${map.content}`;
|
|
179
|
+
}
|
|
180
|
+
value.artifacts = { bytecode_artifact: bytecode.data, skill_artifact: skill, map_artifact: map };
|
|
181
|
+
if (skill != null) this._prepare('skill', bytecode.id, skill);
|
|
182
|
+
if (map != null) this._prepare('map', value.scope.tenant, map);
|
|
183
|
+
return { artifacts: value.artifacts, scope: value.scope, evidence: value.evidence, regime: value.regime };
|
|
184
|
+
}
|
|
185
|
+
_candidateId(payload) { return `candidate_${calculateSha256(JSON.stringify(stable(payload)))}`; }
|
|
186
|
+
_withCandidateLock(operation) {
|
|
187
|
+
const directory = path.join(this.storeDir, '.candidates');
|
|
188
|
+
safePath(directory);
|
|
189
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
190
|
+
const lock = path.join(directory, '.write-lock');
|
|
191
|
+
const fd = fs.openSync(lock, 'wx', 0o600);
|
|
192
|
+
try { return operation(); }
|
|
193
|
+
finally { fs.closeSync(fd); fs.unlinkSync(lock); }
|
|
194
|
+
}
|
|
195
|
+
saveCandidate(input) {
|
|
196
|
+
const payload = this._candidatePayload(input);
|
|
197
|
+
const id = this._candidateId(payload), file = this._candidateFile(id);
|
|
198
|
+
return this._withCandidateLock(() => {
|
|
199
|
+
safePath(file);
|
|
200
|
+
if (fs.existsSync(file)) return this.getCandidate(id);
|
|
201
|
+
const record = { schema: 'cognitivebc/candidate-v1', id, status: 'candidate', ...payload, createdAt: new Date().toISOString(), version: 1, history: [] };
|
|
202
|
+
atomicWrite(file, JSON.stringify(record, null, 2), true);
|
|
203
|
+
return record;
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
_readCandidate(file) {
|
|
207
|
+
safePath(file);
|
|
208
|
+
const record = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
209
|
+
if (path.basename(file) !== `${record.id}.json`) throw new Error('Invalid candidate record');
|
|
210
|
+
return this._checkCandidate(record);
|
|
211
|
+
}
|
|
212
|
+
/** Storage-independent integrity check shared by file and database backends. */
|
|
213
|
+
_checkCandidate(record) {
|
|
214
|
+
const payload = this._candidatePayload(record);
|
|
215
|
+
if (record.schema !== 'cognitivebc/candidate-v1' || this._candidateId(payload) !== record.id || !['candidate', 'promoted'].includes(record.status)) throw new Error('Invalid candidate record');
|
|
216
|
+
if (record.status === 'promoted') {
|
|
217
|
+
if (record.version !== 2 || !Array.isArray(record.history) || record.history.length !== 1) throw new Error('Invalid promotion history');
|
|
218
|
+
const approval = record.history[0]; tenantScope(approval.scope);
|
|
219
|
+
if (approval.scope.tenant !== record.scope.tenant || typeof approval.approvedBy !== 'string' || !approval.approvedBy.trim() || !approval.at) throw new Error('Invalid promotion approval');
|
|
220
|
+
} else if (record.version !== 1 || !Array.isArray(record.history) || record.history.length) throw new Error('Invalid candidate history');
|
|
221
|
+
return record;
|
|
222
|
+
}
|
|
223
|
+
getCandidate(id) { return this._readCandidate(this._candidateFile(id)); }
|
|
224
|
+
listCandidates({ tenants = null, status = 'candidate', limit = 100 } = {}) {
|
|
225
|
+
const directory = path.join(this.storeDir, '.candidates');
|
|
226
|
+
const out = [];
|
|
227
|
+
this._scan(directory, /^candidate_[a-f0-9]{64}\.json$/, file => {
|
|
228
|
+
const record = this._readCandidate(file);
|
|
229
|
+
if (record.status === status && (!tenants || tenants.includes(record.scope.tenant))) out.push(record);
|
|
230
|
+
});
|
|
231
|
+
return out.sort((a, b) => String(b.createdAt).localeCompare(String(a.createdAt))).slice(0, Math.min(Math.max(limit, 1), 500));
|
|
232
|
+
}
|
|
233
|
+
_bundle(record) {
|
|
234
|
+
const a = record.artifacts, id = a.bytecode_artifact.id;
|
|
235
|
+
return [['bytecode', id, a.bytecode_artifact], ...(a.skill_artifact == null ? [] : [['skill', id, a.skill_artifact]]), ...(a.map_artifact == null ? [] : [['map', record.scope.tenant, a.map_artifact]])].map(([type, key, content]) => [type, this._prepare(type, key, content)]);
|
|
236
|
+
}
|
|
237
|
+
_checkBundle(record) {
|
|
238
|
+
const bundle = this._bundle(record);
|
|
239
|
+
for (const [type, prepared] of bundle) {
|
|
240
|
+
const prev = this._collection(type).get(prepared.id);
|
|
241
|
+
if (prev && prev.candidateId !== record.id) throw new Error(`Publication conflicts with existing ${type}: ${prepared.id}`);
|
|
242
|
+
}
|
|
243
|
+
return bundle;
|
|
244
|
+
}
|
|
245
|
+
_publish(record, root, file) {
|
|
246
|
+
const bundle = this._checkBundle(record);
|
|
247
|
+
for (const [type, prepared] of bundle) this._install(type, prepared, { root, file, private: true, candidateId: record.id, scope: { ...record.scope } });
|
|
248
|
+
}
|
|
249
|
+
promoteCandidate(id, options = {}) {
|
|
250
|
+
return this._withCandidateLock(() => this._promoteCandidate(id, options));
|
|
251
|
+
}
|
|
252
|
+
_promoteCandidate(id, options) {
|
|
253
|
+
const record = this.getCandidate(id);
|
|
254
|
+
if (typeof options.approvedBy !== 'string' || !options.approvedBy.trim()) throw new Error('Explicit approvedBy required');
|
|
255
|
+
tenantScope(options.scope);
|
|
256
|
+
if (options.scope.tenant !== record.scope.tenant) throw new Error('Origin tenant is immutable');
|
|
257
|
+
if (record.status === 'promoted') return record;
|
|
258
|
+
this._checkBundle(record);
|
|
259
|
+
const disk = new CognitiveStore(this.storeDir, { privateDirs: this.privateDirs });
|
|
260
|
+
if (!disk.loadAll().integrity) throw new Error('Store integrity failure blocks promotion');
|
|
261
|
+
disk._checkBundle(record);
|
|
262
|
+
const promoted = { ...record, status: 'promoted', version: 2, history: [{ approvedBy: options.approvedBy, scope: options.scope, at: new Date().toISOString() }] };
|
|
263
|
+
const file = this._candidateFile(id);
|
|
264
|
+
atomicWrite(file, JSON.stringify(promoted, null, 2), true);
|
|
265
|
+
this._publish(promoted, this.storeDir, file);
|
|
266
|
+
return promoted;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
module.exports = { CognitiveStore, DEFAULT_PRIVATE_STORE, resolvePrivateDirs, tenantScope, identifier, TYPES, decode };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Telemetria de uso do CognitiveBC.
|
|
5
|
+
*
|
|
6
|
+
* Grava, em JSON Lines fora do repositório (padrão ~/.cognitivebc/telemetry):
|
|
7
|
+
* - usage.jsonl : cada /api/v1/resolve (quem pediu, papel, tenant e quais regras foram aplicadas)
|
|
8
|
+
* - activity.jsonl : cada evento recebido em /api/v1/streams/ingest (sessão, papel, tipo, tokens)
|
|
9
|
+
* e agrega por regra, papel e sessão (job) para o grafo.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const os = require('os');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
|
|
16
|
+
const DAY = 86400000;
|
|
17
|
+
|
|
18
|
+
function dir(options = {}) {
|
|
19
|
+
return options.telemetryDir || process.env.COGNITIVEBC_TELEMETRY_DIR || path.join(os.homedir(), '.cognitivebc', 'telemetry');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function append(file, rec, options) {
|
|
23
|
+
if (options?.telemetry === false) return;
|
|
24
|
+
if (options?.telemetryDb) {
|
|
25
|
+
// Fire-and-forget: telemetry never fails the API request.
|
|
26
|
+
Promise.resolve().then(() => options.telemetryDb.query('INSERT INTO cbc_telemetry (ts, kind, tenant, record) VALUES ($1,$2,$3,$4)', [rec.ts, rec.kind, rec.tenant ?? null, JSON.stringify(rec)])).catch(() => {});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const d = dir(options);
|
|
31
|
+
fs.mkdirSync(d, { recursive: true });
|
|
32
|
+
fs.appendFileSync(path.join(d, file), JSON.stringify(rec) + '\n');
|
|
33
|
+
} catch (_) { /* telemetria nunca derruba a API */ }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const short = (v, n = 80) => (typeof v === 'string' && v.trim() ? v.trim().slice(0, n) : null);
|
|
37
|
+
|
|
38
|
+
function recordResolve(body, resolved, options) {
|
|
39
|
+
append('usage.jsonl', {
|
|
40
|
+
ts: new Date().toISOString(), kind: 'resolve',
|
|
41
|
+
tenant: short(body.tenant), squad: short(body.squad), area: short(body.area), role: short(body.role),
|
|
42
|
+
stacks: Array.isArray(body.stacks) ? body.stacks.slice(0, 20).map(s => String(s).slice(0, 80)) : [],
|
|
43
|
+
agent: short(body.agent_id || body.agent), team: short(body.team), client: short(body.client),
|
|
44
|
+
ids: Array.isArray(resolved.applied_ids) ? resolved.applied_ids.slice(0, 200) : [],
|
|
45
|
+
tokens: safeTokens(resolved.token_count),
|
|
46
|
+
}, options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const safeTokens = v => Number.isSafeInteger(v) && v >= 0 ? v : 0;
|
|
50
|
+
const tokenSum = (a, b) => Math.min(Number.MAX_SAFE_INTEGER, a + safeTokens(b));
|
|
51
|
+
|
|
52
|
+
function tokensOf(payload) {
|
|
53
|
+
const t = payload && payload.tokens;
|
|
54
|
+
if (!t || typeof t !== 'object') return 0;
|
|
55
|
+
let s = 0;
|
|
56
|
+
for (const m of Object.values(t)) if (m && typeof m === 'object') {
|
|
57
|
+
for (const key of ['input', 'output', 'cacheWrite']) s = tokenSum(s, m[key]);
|
|
58
|
+
}
|
|
59
|
+
return s;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function recordIngest(body, events, options) {
|
|
63
|
+
const sessionId = short(body.session_id || body.sessionId, 160);
|
|
64
|
+
for (const e of events || []) {
|
|
65
|
+
append('activity.jsonl', {
|
|
66
|
+
ts: e.timestamp || new Date().toISOString(), kind: 'event',
|
|
67
|
+
session: sessionId || short(e.session_id, 160), tenant: short(body.tenant || e.tenant), squad: short(body.squad || e.squad),
|
|
68
|
+
role: short(e.role || e.agente || body.role),
|
|
69
|
+
agent: short(e.agent_id || e.agent || body.agent_id || body.agent, 160),
|
|
70
|
+
agent_id: short(e.agent_id || e.agent || body.agent_id || body.agent, 160),
|
|
71
|
+
project_id: short(e.project_id || body.project_id, 160),
|
|
72
|
+
type: short(e.event_type, 60), tokens: tokensOf(e.payload),
|
|
73
|
+
}, options);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function readLines(file, options) {
|
|
78
|
+
try {
|
|
79
|
+
return fs.readFileSync(path.join(dir(options), file), 'utf8').split('\n').filter(Boolean)
|
|
80
|
+
.map(l => { try { return JSON.parse(l); } catch { return null; } }).filter(Boolean);
|
|
81
|
+
} catch { return []; }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Todos os registros, opcionalmente filtrados por tenant permitido e janela. */
|
|
85
|
+
function load({ allowTenant = () => true, since = null } = {}, options) {
|
|
86
|
+
if (options?.telemetry === false) return [];
|
|
87
|
+
const s = since ? Date.parse(since) : -Infinity;
|
|
88
|
+
const select = records => records
|
|
89
|
+
.filter(r => r && r.ts && Number.isFinite(Date.parse(r.ts)) && Date.parse(r.ts) > s && allowTenant(r.tenant ?? null))
|
|
90
|
+
.sort((a, b) => Date.parse(a.ts) - Date.parse(b.ts));
|
|
91
|
+
if (options?.telemetryDb) {
|
|
92
|
+
const floor = Number.isFinite(s) ? new Date(s).toISOString() : new Date(Date.now() - 366 * DAY).toISOString();
|
|
93
|
+
return options.telemetryDb.query('SELECT record FROM cbc_telemetry WHERE ts > $1 ORDER BY ts LIMIT 50000', [floor])
|
|
94
|
+
.then(({ rows }) => select(rows.map(r => r.record)), () => []);
|
|
95
|
+
}
|
|
96
|
+
return select(readLines('usage.jsonl', options).concat(readLines('activity.jsonl', options)));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function aggregate(records, { days = 30, now = Date.now() } = {}) {
|
|
100
|
+
days = Number.isInteger(days) && days > 0 && days <= 3660 ? days : 30;
|
|
101
|
+
const start = now - days * DAY;
|
|
102
|
+
const dayIdx = (ts) => Math.floor((Date.parse(ts) - start) / DAY);
|
|
103
|
+
const bump = (map, key, r, extra) => {
|
|
104
|
+
if (!key) return;
|
|
105
|
+
const m = map[key] || (map[key] = { count: 0, last: null, first: null, roles: Object.create(null), agents: Object.create(null), daily: new Array(days).fill(0), tokens: 0 });
|
|
106
|
+
m.count++;
|
|
107
|
+
if (!m.last || Date.parse(r.ts) > Date.parse(m.last)) m.last = r.ts;
|
|
108
|
+
if (!m.first || Date.parse(r.ts) < Date.parse(m.first)) m.first = r.ts;
|
|
109
|
+
if (r.role) m.roles[r.role] = (m.roles[r.role] || 0) + 1;
|
|
110
|
+
if (r.agent) m.agents[r.agent] = (m.agents[r.agent] || 0) + 1;
|
|
111
|
+
const i = Math.min(days - 1, dayIdx(r.ts)); if (i >= 0 && i < days) m.daily[i]++;
|
|
112
|
+
m.tokens = tokenSum(m.tokens, extra);
|
|
113
|
+
};
|
|
114
|
+
const rules = Object.create(null), roles = Object.create(null), jobs = Object.create(null), tenants = Object.create(null);
|
|
115
|
+
records = records.filter(r => r && ['resolve', 'event'].includes(r.kind) && Number.isFinite(Date.parse(r.ts)) && Date.parse(r.ts) >= start && Date.parse(r.ts) <= now)
|
|
116
|
+
.sort((a, b) => Date.parse(a.ts) - Date.parse(b.ts));
|
|
117
|
+
let resolves = 0, events = 0;
|
|
118
|
+
for (const r of records) {
|
|
119
|
+
if (r.kind === 'resolve') {
|
|
120
|
+
resolves++;
|
|
121
|
+
for (const id of Array.isArray(r.ids) ? r.ids : []) bump(rules, id, r);
|
|
122
|
+
bump(roles, r.role, r);
|
|
123
|
+
} else if (r.kind === 'event') {
|
|
124
|
+
events++;
|
|
125
|
+
bump(roles, r.role, r, r.tokens);
|
|
126
|
+
bump(jobs, r.session, r, r.tokens);
|
|
127
|
+
}
|
|
128
|
+
bump(tenants, r.tenant, r);
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
generated_at: new Date(now).toISOString(),
|
|
132
|
+
window: { start: new Date(start).toISOString(), end: new Date(now).toISOString(), days },
|
|
133
|
+
totals: { resolves, events, rules_used: Object.keys(rules).length },
|
|
134
|
+
rules, roles, jobs, tenants,
|
|
135
|
+
first_ts: records.length ? records[0].ts : null,
|
|
136
|
+
last_ts: records.length ? records[records.length - 1].ts : null,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function recent(records, limit = 60) {
|
|
141
|
+
return records.slice(-limit).reverse().map(r => ({
|
|
142
|
+
ts: r.ts, kind: r.kind, role: r.role, tenant: r.tenant, squad: r.squad, agent: r.agent || null,
|
|
143
|
+
agent_id: r.agent_id || null, project_id: r.project_id || null,
|
|
144
|
+
session: r.session || null, type: r.type || null, ids: r.ids || [], tokens: safeTokens(r.tokens),
|
|
145
|
+
}));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports = { dir, recordResolve, recordIngest, load, aggregate, recent, tokensOf };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const { normalizePrincipal, authorize, allowsTenant, fail } = require('./access-policy');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Personal access tokens stored as SHA-256 digests in Postgres.
|
|
8
|
+
* Secrets are shown exactly once at creation and never persisted.
|
|
9
|
+
*/
|
|
10
|
+
function createTokenService(db) {
|
|
11
|
+
const cache = new Map();
|
|
12
|
+
const TTL = 30000;
|
|
13
|
+
async function lookup(digestHex) {
|
|
14
|
+
const hit = cache.get(digestHex);
|
|
15
|
+
if (hit && hit.until > Date.now()) return hit.principal;
|
|
16
|
+
const { rows } = await db.query('SELECT principal FROM cbc_tokens WHERE token_sha256=$1 AND revoked_at IS NULL', [digestHex]);
|
|
17
|
+
const principal = rows[0]?.principal || null;
|
|
18
|
+
cache.set(digestHex, { principal, until: Date.now() + TTL });
|
|
19
|
+
if (principal) db.query('UPDATE cbc_tokens SET last_used_at=now() WHERE token_sha256=$1', [digestHex]).catch(() => {});
|
|
20
|
+
return principal;
|
|
21
|
+
}
|
|
22
|
+
async function issue({ label, principal }, issuer) {
|
|
23
|
+
authorize(issuer, 'approve');
|
|
24
|
+
if (typeof label !== 'string' || !label.trim() || label.length > 120) fail(400, 'Invalid token label');
|
|
25
|
+
let normalized;
|
|
26
|
+
try { normalized = normalizePrincipal(principal); } catch (err) { fail(400, err.message); }
|
|
27
|
+
// Issuers may only delegate tenants and permissions they hold.
|
|
28
|
+
for (const tenant of normalized.tenants) if (!allowsTenant(issuer, tenant === '*' ? '*' : tenant)) fail(403, `Cannot delegate tenant ${tenant}`);
|
|
29
|
+
for (const permission of normalized.permissions) if (!issuer.permissions.includes('*') && !issuer.permissions.includes(permission)) fail(403, `Cannot delegate permission ${permission}`);
|
|
30
|
+
const secret = `cbc_${crypto.randomBytes(32).toString('base64url')}`;
|
|
31
|
+
const digest = crypto.createHash('sha256').update(secret).digest('hex');
|
|
32
|
+
await db.query('INSERT INTO cbc_tokens (token_sha256, label, principal, created_by) VALUES ($1,$2,$3,$4)', [digest, label, JSON.stringify({ id: normalized.id, tenants: normalized.tenants, permissions: normalized.permissions }), issuer.id]);
|
|
33
|
+
return { token: secret, token_id: digest.slice(0, 16), label, principal: normalized };
|
|
34
|
+
}
|
|
35
|
+
async function list(issuer) {
|
|
36
|
+
authorize(issuer, 'approve');
|
|
37
|
+
const { rows } = await db.query('SELECT token_sha256, label, principal, created_by, created_at, last_used_at, revoked_at FROM cbc_tokens ORDER BY created_at DESC');
|
|
38
|
+
return rows.filter(r => r.principal.tenants.every(t => allowsTenant(issuer, t)))
|
|
39
|
+
.map(r => ({ token_id: r.token_sha256.slice(0, 16), label: r.label, principal: r.principal, created_by: r.created_by, created_at: r.created_at, last_used_at: r.last_used_at, revoked_at: r.revoked_at }));
|
|
40
|
+
}
|
|
41
|
+
async function revoke(tokenId, issuer) {
|
|
42
|
+
authorize(issuer, 'approve');
|
|
43
|
+
if (typeof tokenId !== 'string' || !/^[a-f0-9]{16}$/.test(tokenId)) fail(400, 'Invalid token id');
|
|
44
|
+
const { rows } = await db.query('SELECT token_sha256, principal FROM cbc_tokens WHERE token_sha256 LIKE $1', [`${tokenId}%`]);
|
|
45
|
+
if (rows.length !== 1) fail(404, 'Token not found');
|
|
46
|
+
if (!rows[0].principal.tenants.every(t => allowsTenant(issuer, t))) fail(403, 'Token outside your tenants');
|
|
47
|
+
await db.query('UPDATE cbc_tokens SET revoked_at=now() WHERE token_sha256=$1', [rows[0].token_sha256]);
|
|
48
|
+
cache.delete(rows[0].token_sha256);
|
|
49
|
+
return { token_id: tokenId, revoked: true };
|
|
50
|
+
}
|
|
51
|
+
async function route({ method, pathname, principal, parseBody, sendJson }) {
|
|
52
|
+
if (pathname === '/api/v1/tokens' && method === 'GET') { sendJson(200, { tokens: await list(principal) }); return true; }
|
|
53
|
+
if (pathname === '/api/v1/tokens' && method === 'POST') { sendJson(201, await issue(await parseBody(), principal)); return true; }
|
|
54
|
+
const m = /^\/api\/v1\/tokens\/([a-f0-9]{16})\/revoke$/.exec(pathname);
|
|
55
|
+
if (m && method === 'POST') { sendJson(200, await revoke(m[1], principal)); return true; }
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return { lookup, issue, list, revoke, route };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { createTokenService };
|