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.
Files changed (66) hide show
  1. package/LICENSE +70 -0
  2. package/README.md +98 -4
  3. package/SECURITY.md +29 -0
  4. package/TERMS_OF_SERVICE.md +55 -0
  5. package/bin/cbc-cloud-bootstrap.js +108 -0
  6. package/bin/cbc-seed-projects.js +29 -0
  7. package/bin/cbc-server.js +45 -0
  8. package/bin/cognitivebc.js +251 -0
  9. package/docs/ACCESS_CONTROL.md +111 -0
  10. package/docs/ARTIFACT_AUDIT.md +36 -0
  11. package/docs/CONNECT_CLIENTS.md +116 -0
  12. package/docs/DEPLOY_AZURE.md +136 -0
  13. package/docs/INGESTION.md +29 -0
  14. package/docs/OPERATIONS.md +85 -0
  15. package/docs/PROJECT_IDENTITY.md +35 -0
  16. package/docs/RESOLVER.md +86 -0
  17. package/docs/SDK.md +96 -0
  18. package/docs/STORAGE.md +63 -0
  19. package/docs/TECHNICAL_SPECIFICATION.md +365 -0
  20. package/docs/TELEMETRY.md +119 -0
  21. package/index.js +27 -2
  22. package/package.json +45 -5
  23. package/packages/cbc-compiler/index.js +357 -0
  24. package/packages/cbc-compiler/sanitizer.js +43 -0
  25. package/packages/cbc-identity/index.js +92 -0
  26. package/packages/cbc-mcp/http-backend.js +138 -0
  27. package/packages/cbc-mcp/http-route.js +64 -0
  28. package/packages/cbc-mcp/index.js +50 -0
  29. package/packages/cbc-mcp/tools.js +122 -0
  30. package/packages/cbc-resolver/catalog-linker.js +215 -0
  31. package/packages/cbc-resolver/graph-builder.js +533 -0
  32. package/packages/cbc-resolver/index.js +548 -0
  33. package/packages/cbc-resolver/obsidian-bridge.js +564 -0
  34. package/packages/cbc-schema/index.js +194 -0
  35. package/packages/cbc-sdk-js/client.js +162 -0
  36. package/services/cbc-api/access-policy.js +130 -0
  37. package/services/cbc-api/artifact-audit.js +167 -0
  38. package/services/cbc-api/cloud.js +56 -0
  39. package/services/cbc-api/context-service.js +48 -0
  40. package/services/cbc-api/ingestion-service.js +234 -0
  41. package/services/cbc-api/pg-store.js +172 -0
  42. package/services/cbc-api/project-registry.js +322 -0
  43. package/services/cbc-api/public/app.html +123 -0
  44. package/services/cbc-api/public/graph.html +1168 -0
  45. package/services/cbc-api/public/graph2.html +455 -0
  46. package/services/cbc-api/public/graph3.html +503 -0
  47. package/services/cbc-api/public/index.html +78 -0
  48. package/services/cbc-api/public/vendor/cbc-app.js +209 -0
  49. package/services/cbc-api/public/vendor/cbc-auth.js +49 -0
  50. package/services/cbc-api/public/vendor/cbc-site.css +101 -0
  51. package/services/cbc-api/public/vendor/cose-base.js +3214 -0
  52. package/services/cbc-api/public/vendor/cytoscape-dagre.js +397 -0
  53. package/services/cbc-api/public/vendor/cytoscape-fcose.js +1549 -0
  54. package/services/cbc-api/public/vendor/cytoscape.min.js +32 -0
  55. package/services/cbc-api/public/vendor/dagre.min.js +3809 -0
  56. package/services/cbc-api/public/vendor/graphology-library.min.js +2 -0
  57. package/services/cbc-api/public/vendor/graphology.umd.min.js +2 -0
  58. package/services/cbc-api/public/vendor/layout-base.js +5230 -0
  59. package/services/cbc-api/public/vendor/pf-graph.css +86 -0
  60. package/services/cbc-api/public/vendor/pf-telemetry.js +176 -0
  61. package/services/cbc-api/public/vendor/sigma.min.js +1351 -0
  62. package/services/cbc-api/runtime-info.js +39 -0
  63. package/services/cbc-api/server.js +642 -0
  64. package/services/cbc-api/store-loader.js +269 -0
  65. package/services/cbc-api/telemetry.js +148 -0
  66. package/services/cbc-api/token-service.js +61 -0
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ const { Pool } = require('pg');
4
+ const { createServer } = require('./server');
5
+ const { PgCognitiveStore } = require('./pg-store');
6
+ const { createPgIngestionService } = require('./ingestion-service');
7
+ const { createTokenService } = require('./token-service');
8
+
9
+ function poolFrom(url, env = process.env) {
10
+ const parsed = new URL(url);
11
+ const local = ['localhost', '127.0.0.1', '::1', ''].includes(parsed.hostname) || parsed.hostname.startsWith('/');
12
+ const ssl = env.COGNITIVEBC_DATABASE_SSL === 'off' || local ? false : { rejectUnauthorized: env.COGNITIVEBC_DATABASE_SSL !== 'insecure' };
13
+ return new Pool({ connectionString: url, ssl, max: Number(env.COGNITIVEBC_DATABASE_POOL || 10), idleTimeoutMillis: 30000 });
14
+ }
15
+
16
+ /**
17
+ * Multi-instance server: Postgres for knowledge, receipts, telemetry, tokens and
18
+ * projects. Optional modules (project registry, remote MCP) are attached when present.
19
+ */
20
+ async function createCloudServer({ databaseUrl, db, env = process.env, refreshMs = 30000, ...options } = {}) {
21
+ const pool = db || poolFrom(databaseUrl || env.COGNITIVEBC_DATABASE_URL, env);
22
+ const store = await new PgCognitiveStore(pool).init();
23
+ const tokens = createTokenService(pool);
24
+ const extraRoutes = [tokens.route];
25
+ let registry = null;
26
+ try {
27
+ const { createProjectRegistry, handleProjectRoutes } = require('./project-registry');
28
+ registry = createProjectRegistry({ db: pool });
29
+ await registry.migrate();
30
+ extraRoutes.push(async ({ method, pathname, principal, parseBody }) => {
31
+ if (!pathname.startsWith('/api/v1/projects')) return false;
32
+ const body = method === 'POST' ? await parseBody() : {};
33
+ const out = await handleProjectRoutes({ method, pathname, body, principal, registry });
34
+ return out;
35
+ });
36
+ } catch (err) { if (err.code !== 'MODULE_NOT_FOUND') throw err; }
37
+ const ingestionService = createPgIngestionService({ store, db: pool });
38
+ const routes = extraRoutes.map(route => route === tokens.route ? route : async ctx => {
39
+ const out = await route(ctx);
40
+ if (!out) return false;
41
+ ctx.sendJson(out.status, out.json);
42
+ return true;
43
+ });
44
+ const context = { store, registry, ingestionService, db: pool };
45
+ try {
46
+ const { createMcpRoute } = require('../../packages/cbc-mcp/http-route');
47
+ routes.push(createMcpRoute(context));
48
+ } catch (err) { if (err.code !== 'MODULE_NOT_FOUND') throw err; }
49
+ const { server } = createServer({ ...options, store, ingestionService, tokenLookup: tokens.lookup, telemetryDb: pool, extraRoutes: routes, allowedHosts: options.allowedHosts });
50
+ const timer = refreshMs > 0 ? setInterval(() => store.refresh().catch(() => {}), refreshMs) : null;
51
+ timer?.unref();
52
+ server.on('close', () => { if (timer) clearInterval(timer); if (!db) pool.end().catch(() => {}); });
53
+ return { server, store, pool, tokens, registry, ingestionService };
54
+ }
55
+
56
+ module.exports = { createCloudServer, poolFrom };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+ const { resolveContextForRole } = require('../../packages/cbc-resolver');
3
+ const { createStoreView, authorize, fail } = require('./access-policy');
4
+ function object(value, label) {
5
+ if (!value || typeof value !== 'object' || Array.isArray(value)) fail(400, `Invalid ${label}`);
6
+ return value;
7
+ }
8
+ function identifier(value, label, required = false) {
9
+ if (value == null && !required) return;
10
+ if (typeof value !== 'string' || !value.trim() || value.length > 256 || /[\x00-\x1f\x7f]/.test(value)) fail(400, `Invalid ${label}`);
11
+ }
12
+ function budget(value) {
13
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < 100 || value > 2000) fail(400, 'Token budget must be finite in range 100..2000');
14
+ }
15
+ /** One authorized context contract for HTTP, MCP and trusted runtime adapters. */
16
+ function resolveAuthorizedContext({ store, principal, params = {} }) {
17
+ authorize(principal, 'read'); object(params, 'context request');
18
+ const scope = params.scope === undefined ? {} : object(params.scope, 'scope');
19
+ if (params.tenant && scope.tenant && params.tenant !== scope.tenant) fail(400, 'Conflicting tenant scope');
20
+ const tenant = params.tenant ?? scope.tenant ?? null;
21
+ const role = params.role ?? scope.role;
22
+ identifier(role, 'role', true);
23
+ for (const key of ['tenant', 'squad', 'area', 'stack', 'project_id']) identifier(scope[key], `scope.${key}`);
24
+ identifier(tenant, 'tenant');
25
+ for (const key of ['squad', 'area']) identifier(params[key], key);
26
+ for (const key of ['query', 'task']) if (params[key] !== undefined && (typeof params[key] !== 'string' || params[key].length > 20000)) fail(400, `Invalid ${key}`);
27
+ for (const key of ['stacks', 'subscopes']) if (params[key] !== undefined) {
28
+ if (!Array.isArray(params[key]) || params[key].length > 200) fail(400, `Invalid ${key}`);
29
+ params[key].forEach(value => identifier(value, key, true));
30
+ }
31
+ if (params.maxTokens !== undefined && params.max_tokens !== undefined && params.maxTokens !== params.max_tokens) fail(400, 'Conflicting token budgets');
32
+ const explicit = params.maxTokens ?? params.max_tokens;
33
+ if (explicit !== undefined && explicit !== null) budget(explicit);
34
+ else if (Object.hasOwn(params, 'maxTokens') || Object.hasOwn(params, 'max_tokens')) fail(400, 'Invalid token budget');
35
+ const hp = params.hyperparameters === undefined ? {} : object(params.hyperparameters, 'hyperparameters');
36
+ if (hp.defaultMaxTokens !== undefined) budget(hp.defaultMaxTokens);
37
+ if (hp.roleMaxTokens !== undefined) Object.values(object(hp.roleMaxTokens, 'role budgets')).forEach(budget);
38
+ if (hp.minEvidenceCount !== undefined && (!Number.isFinite(hp.minEvidenceCount) || hp.minEvidenceCount < 0)) fail(400, 'Invalid evidence threshold');
39
+ if (hp.packingStrategy !== undefined && !['section_block', 'item_granular'].includes(hp.packingStrategy)) fail(400, 'Invalid packing strategy');
40
+ if (hp.compressionLevel !== undefined && !['standard', 'compact'].includes(hp.compressionLevel)) fail(400, 'Invalid compression level');
41
+ if (hp.priorityOrder !== undefined && (!Array.isArray(hp.priorityOrder) || new Set(hp.priorityOrder).size !== hp.priorityOrder.length || hp.priorityOrder.some(v => !['constraints', 'principles', 'tenant', 'heuristics', 'experience'].includes(v)))) fail(400, 'Invalid priority order');
42
+ if (params.profile !== undefined) identifier(params.profile, 'profile', true);
43
+ if (hp.profile !== undefined) identifier(hp.profile, 'hyperparameters.profile', true);
44
+ const { role: _roleSelection, ...artifactScope } = scope;
45
+ const view = createStoreView(store, principal, { ...artifactScope, tenant });
46
+ return resolveContextForRole({ tenant, role, squad: params.squad ?? scope.squad ?? null, area: params.area ?? scope.area ?? null, stacks: params.stacks ?? (scope.stack ? [scope.stack] : []), subscopes: params.subscopes ?? [], query: params.query ?? params.task ?? null, maxTokens: explicit, hyperparameters: { ...hp, ...(params.profile ? { profile: params.profile } : {}) }, bytecodes: view.getBytecodes(), maps: view.getMapsDict() });
47
+ }
48
+ module.exports = { resolveAuthorizedContext };
@@ -0,0 +1,234 @@
1
+ 'use strict';
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const crypto = require('crypto');
6
+ const { compileExperience } = require('../../packages/cbc-compiler');
7
+
8
+ function stable(value) {
9
+ if (Array.isArray(value)) return value.map(stable);
10
+ if (value && typeof value === 'object') return Object.fromEntries(Object.keys(value).sort().map(k => [k, stable(value[k])]));
11
+ return value;
12
+ }
13
+ function digest(value) { return crypto.createHash('sha256').update(JSON.stringify(stable(value))).digest('hex'); }
14
+ function failure(message, statusCode) { return Object.assign(new Error(message), { statusCode, status: statusCode }); }
15
+ /**
16
+ * Content hashes must be identical on every machine. A date-time without an
17
+ * offset is interpreted as UTC, never as the host's local time zone.
18
+ */
19
+ function isoTimestamp(value) {
20
+ if (typeof value !== 'string' || !Number.isFinite(Date.parse(value))) return null;
21
+ const floating = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?$/.test(value.trim());
22
+ return new Date(floating ? `${value.trim()}Z` : value).toISOString();
23
+ }
24
+ function text(value, label) {
25
+ if (typeof value !== 'string' || !value.trim() || value.length > 256 || /[\x00-\x1f\x7f]/.test(value)) throw failure(`Invalid ${label}`, 400);
26
+ return value;
27
+ }
28
+ function safeDirectory(directory) {
29
+ const absolute = path.resolve(directory);
30
+ let current = path.parse(absolute).root;
31
+ for (const part of absolute.slice(current.length).split(path.sep)) {
32
+ current = path.join(current, part);
33
+ try { if (fs.lstatSync(current).isSymbolicLink()) throw failure('Ingestion path must not contain symlinks', 500); }
34
+ catch (err) { if (err.code !== 'ENOENT') throw err; }
35
+ }
36
+ fs.mkdirSync(absolute, { recursive: true, mode: 0o700 });
37
+ return absolute;
38
+ }
39
+ function atomicReceipt(file, receipt) {
40
+ const tmp = `${file}.${crypto.randomBytes(12).toString('hex')}.tmp`;
41
+ let fd;
42
+ try {
43
+ fd = fs.openSync(tmp, 'wx', 0o600);
44
+ fs.writeFileSync(fd, JSON.stringify(receipt) + '\n');
45
+ fs.fsyncSync(fd);
46
+ fs.closeSync(fd); fd = undefined;
47
+ fs.renameSync(tmp, file);
48
+ } finally {
49
+ if (fd !== undefined) fs.closeSync(fd);
50
+ try { fs.unlinkSync(tmp); } catch (err) { if (err.code !== 'ENOENT') throw err; }
51
+ }
52
+ }
53
+ function qualify(event) {
54
+ const p = event.payload || {};
55
+ if (event.event_type === 'ExternalCorrectiveFeedback' && typeof p.issue === 'string' && p.issue.trim() && typeof p.resolution === 'string' && p.resolution.trim()) {
56
+ return { regime: 'external_corrective', inputPayload: p };
57
+ }
58
+ if (event.event_type === 'SilentSelfCorrectionCycle' && Array.isArray(p.versions) && p.versions.length >= 2 && p.versions.every(v => v && typeof v === 'object' && !Array.isArray(v)) && Array.isArray(p.reasoning_summaries) && p.reasoning_summaries.some(s => typeof s === 'string' && s.trim()) && p.reasoning_summaries.every(s => typeof s === 'string')) {
59
+ return { regime: 'silent_multimodal', inputPayload: p };
60
+ }
61
+ return null;
62
+ }
63
+
64
+ function validateBatch(body, principal) {
65
+ if (!body || typeof body !== 'object' || Array.isArray(body)) throw failure('Invalid ingestion body', 400);
66
+ const tenant = text(body.tenant, 'tenant');
67
+ if (Buffer.byteLength(tenant) > 96) throw failure('Invalid tenant', 400);
68
+ const session = text(body.session_id || body.sessionId, 'session_id');
69
+ if (!Array.isArray(body.events) || body.events.length > 1000) throw failure('events must be an array of at most 1000 entries', 400);
70
+ if (principal && Array.isArray(principal.tenants) && !principal.tenants.includes('*') && !principal.tenants.includes(tenant)) throw failure('Tenant not authorized', 403);
71
+ for (const field of ['project_id', 'agent_id', 'agent', 'role']) if (body[field] != null) text(body[field], field);
72
+ const batch = new Map();
73
+ for (const event of body.events) {
74
+ if (!event || typeof event !== 'object' || Array.isArray(event)) throw failure('Invalid event', 400);
75
+ const eventId = text(event.event_id || event.id, 'event_id');
76
+ text(event.event_type, 'event_type');
77
+ if (event.tenant && event.tenant !== tenant) throw failure('Event tenant conflicts with request scope', 403);
78
+ if (event.session_id && event.session_id !== session) throw failure('Event session conflicts with request scope', 400);
79
+ if (event.payload !== undefined && (!event.payload || typeof event.payload !== 'object' || Array.isArray(event.payload))) throw failure('Invalid event payload', 400);
80
+ if (event.payload?.tenant && event.payload.tenant !== tenant) throw failure('Payload tenant conflicts with request scope', 403);
81
+ const effectiveScope = { squad: body.squad || event.squad || 'geral', stack: event.payload?.stack || body.stack || 'general_stack', role: event.role || body.role || 'specialist' };
82
+ for (const [key, value] of Object.entries(effectiveScope)) text(value, key);
83
+ for (const field of ['project_id', 'agent_id', 'agent', 'role']) if (event[field] != null) text(event[field], field);
84
+ const activity = { agent_id: event.agent_id || event.agent || body.agent_id || body.agent || null, role: event.role || body.role || null, project_id: event.project_id || body.project_id || null, squad: effectiveScope.squad, event_type: event.event_type || null, timestamp: isoTimestamp(event.timestamp) };
85
+ const hash = digest({ event, scope: effectiveScope, activity });
86
+ if (batch.has(eventId) && batch.get(eventId).hash !== hash) throw failure('Event ID reused with different content', 409);
87
+ batch.set(eventId, { event, eventId, hash, effectiveScope, activity });
88
+ }
89
+ return { tenant, session, batch };
90
+ }
91
+ /** Durable delivery receipts, not an operational session-memory replacement.
92
+ * The caller authorizes the explicit tenant. No promotion is performed here.
93
+ * Each stable event ID is bound to its content and the tenant/session scope.
94
+ */
95
+ function createIngestionService({ store, receiptDir, compiler = compileExperience, writeReceipt = atomicReceipt } = {}) {
96
+ if (!store || typeof store.saveCandidate !== 'function') throw new Error('Ingestion requires a candidate-capable store');
97
+ const directory = receiptDir || path.join(store.storeDir, '.ingestion');
98
+ function ingest(body, { principal } = {}) {
99
+ const { tenant, session, batch } = validateBatch(body, principal);
100
+ const root = safeDirectory(directory);
101
+ const lock = path.join(root, digest([tenant, session]) + '.lock');
102
+ let lockFd;
103
+ try { lockFd = fs.openSync(lock, 'wx', 0o600); }
104
+ catch (err) { if (err.code === 'EEXIST') throw failure('Session ingestion is busy or has an orphaned lock; retry or inspect the lock', 503); throw err; }
105
+ try { fs.writeFileSync(lockFd, JSON.stringify({ pid: process.pid, created_at: new Date().toISOString() })); fs.fsyncSync(lockFd); }
106
+ catch (err) { fs.closeSync(lockFd); fs.unlinkSync(lock); throw err; }
107
+ const receipts = [], triggered = [], cycles = [], newEventIds = [];
108
+ let newlyAccepted = 0, duplicateCount = body.events.length - batch.size;
109
+ try {
110
+ // Reject all persisted conflicts before accepting any new item from this batch.
111
+ for (const entry of batch.values()) {
112
+ entry.file = path.join(root, digest([tenant, session, entry.eventId]) + '.json');
113
+ try {
114
+ if (fs.lstatSync(entry.file).isSymbolicLink()) throw failure('Invalid receipt file', 500);
115
+ entry.previous = JSON.parse(fs.readFileSync(entry.file, 'utf8'));
116
+ if (entry.previous.content_sha256 !== entry.hash) throw failure('Event ID reused with different content', 409);
117
+ } catch (err) { if (err.code !== 'ENOENT') throw err; }
118
+ }
119
+ for (const entry of batch.values()) {
120
+ let receipt = entry.previous;
121
+ if (receipt) duplicateCount++;
122
+ else {
123
+ const qualified = qualify(entry.event);
124
+ let candidate = null;
125
+ if (qualified) {
126
+ const scope = { level: 'tenant', tenant, ...entry.effectiveScope };
127
+ const artifacts = compiler({ regime: qualified.regime, scope, inputPayload: qualified.inputPayload });
128
+ candidate = store.saveCandidate({ artifacts, scope, regime: qualified.regime, evidence: { producer: principal?.id || 'local-internal', session_id: session, event_id: entry.eventId, content_sha256: entry.hash, validation_status: 'unverified' } });
129
+ }
130
+ receipt = { schema: 'cognitivebc/receipt/v1', origin_principal_id: principal?.id || null, tenant, session_id: session, event_id: entry.eventId, content_sha256: entry.hash, activity: entry.activity, status: candidate ? 'candidate_saved' : 'received', candidate_id: candidate?.id || null, regime: qualified?.regime || null, qualification: candidate ? 'evidence_requires_review' : 'insufficient_or_unclassified_evidence', received_at: new Date().toISOString() };
131
+ writeReceipt(entry.file, receipt);
132
+ newlyAccepted++;
133
+ newEventIds.push(entry.eventId);
134
+ }
135
+ receipts.push(receipt);
136
+ if (receipt.candidate_id) {
137
+ triggered.push({ job_id: receipt.candidate_id, candidate_id: receipt.candidate_id, regime: receipt.regime, status: 'candidate_saved' });
138
+ if (receipt.regime === 'silent_multimodal') cycles.push({ id: receipt.candidate_id, session_id: session, event_id: receipt.event_id });
139
+ }
140
+ }
141
+ } finally { fs.closeSync(lockFd); fs.unlinkSync(lock); }
142
+ return { accepted: body.events.length, newly_accepted: newlyAccepted, new_event_ids: newEventIds, duplicates: duplicateCount, cycles_detected: cycles, triggered_compilations: triggered, receipts };
143
+ }
144
+ function listJobs({ allowTenant = () => false, tenant, project_id } = {}) {
145
+ if (!fs.existsSync(directory)) return [];
146
+ const root = safeDirectory(directory), jobs = new Map();
147
+ for (const name of fs.readdirSync(root).sort()) {
148
+ if (!/^[a-f0-9]{64}\.json$/.test(name)) continue;
149
+ const fd = fs.openSync(path.join(root, name), fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
150
+ let receipt;
151
+ try {
152
+ if (!fs.fstatSync(fd).isFile()) throw failure('Invalid activity receipt', 503);
153
+ receipt = JSON.parse(fs.readFileSync(fd, 'utf8'));
154
+ } finally { fs.closeSync(fd); }
155
+ if (receipt?.schema !== 'cognitivebc/receipt/v1' || typeof receipt.tenant !== 'string' || typeof receipt.session_id !== 'string' || !/^[a-f0-9]{64}$/.test(receipt.content_sha256)) throw failure('Invalid activity receipt', 503);
156
+ if (!allowTenant(receipt.tenant) || (tenant && tenant !== receipt.tenant)) continue;
157
+ if (project_id && (receipt.activity || {}).project_id !== project_id) continue;
158
+ project(jobs, receipt);
159
+ }
160
+ return [...jobs.values()];
161
+ }
162
+ return { ingest, listJobs };
163
+ }
164
+ function project(jobs, receipt) {
165
+ const activity = receipt.activity || {};
166
+ const key = JSON.stringify([receipt.tenant, receipt.session_id]);
167
+ let job = jobs.get(key);
168
+ if (!job) { job = { id: receipt.session_id, tenant: receipt.tenant, project_id: activity.project_id || null, agentes: [], observations: [], eventos_count: 0, created_at: null, last_seen: null, source: 'durable_receipts' }; jobs.set(key, job); }
169
+ const ts = activity.timestamp || receipt.received_at;
170
+ job.eventos_count++;
171
+ if (!job.created_at || ts < job.created_at) job.created_at = ts;
172
+ if (!job.last_seen || ts > job.last_seen) job.last_seen = ts;
173
+ if (activity.role && !job.agentes.includes(activity.role)) job.agentes.push(activity.role);
174
+ job.observations.push({ ...activity, timestamp: ts, event_id: receipt.event_id });
175
+ }
176
+
177
+ /**
178
+ * Database-backed ingestion with the same contract as the file service.
179
+ * The (tenant, session, event) primary key replaces the file lock: a
180
+ * concurrent duplicate insert is resolved by ON CONFLICT and re-checked.
181
+ */
182
+ function createPgIngestionService({ store, db, compiler = compileExperience } = {}) {
183
+ if (!store || typeof store.saveCandidate !== 'function' || !db) throw new Error('Ingestion requires a candidate-capable store and database');
184
+ async function ingest(body, { principal } = {}) {
185
+ const { tenant, session, batch } = validateBatch(body, principal);
186
+ const ids = [...batch.keys()];
187
+ const { rows } = await db.query('SELECT event_id, receipt FROM cbc_receipts WHERE tenant=$1 AND session_id=$2 AND event_id = ANY($3)', [tenant, session, ids]);
188
+ for (const row of rows) {
189
+ const entry = batch.get(row.event_id);
190
+ if (row.receipt.content_sha256 !== entry.hash) throw failure('Event ID reused with different content', 409);
191
+ entry.previous = row.receipt;
192
+ }
193
+ const receipts = [], triggered = [], cycles = [], newEventIds = [];
194
+ let newlyAccepted = 0, duplicateCount = body.events.length - batch.size;
195
+ for (const entry of batch.values()) {
196
+ let receipt = entry.previous;
197
+ if (receipt) duplicateCount++;
198
+ else {
199
+ const qualified = qualify(entry.event);
200
+ let candidate = null;
201
+ if (qualified) {
202
+ const scope = { level: 'tenant', tenant, ...entry.effectiveScope };
203
+ const artifacts = compiler({ regime: qualified.regime, scope, inputPayload: qualified.inputPayload });
204
+ candidate = await store.saveCandidate({ artifacts, scope, regime: qualified.regime, evidence: { producer: principal?.id || 'local-internal', session_id: session, event_id: entry.eventId, content_sha256: entry.hash, validation_status: 'unverified' } });
205
+ }
206
+ receipt = { schema: 'cognitivebc/receipt/v1', origin_principal_id: principal?.id || null, tenant, session_id: session, event_id: entry.eventId, content_sha256: entry.hash, activity: entry.activity, status: candidate ? 'candidate_saved' : 'received', candidate_id: candidate?.id || null, regime: qualified?.regime || null, qualification: candidate ? 'evidence_requires_review' : 'insufficient_or_unclassified_evidence', received_at: new Date().toISOString() };
207
+ const inserted = await db.query('INSERT INTO cbc_receipts (tenant, session_id, event_id, content_sha256, project_id, receipt) VALUES ($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING RETURNING event_id', [tenant, session, entry.eventId, entry.hash, entry.activity.project_id, JSON.stringify(receipt)]);
208
+ if (inserted.rows.length) { newlyAccepted++; newEventIds.push(entry.eventId); }
209
+ else {
210
+ const raced = await db.query('SELECT receipt FROM cbc_receipts WHERE tenant=$1 AND session_id=$2 AND event_id=$3', [tenant, session, entry.eventId]);
211
+ if (raced.rows[0]?.receipt.content_sha256 !== entry.hash) throw failure('Event ID reused with different content', 409);
212
+ receipt = raced.rows[0].receipt; duplicateCount++;
213
+ }
214
+ }
215
+ receipts.push(receipt);
216
+ if (receipt.candidate_id) {
217
+ triggered.push({ job_id: receipt.candidate_id, candidate_id: receipt.candidate_id, regime: receipt.regime, status: 'candidate_saved' });
218
+ if (receipt.regime === 'silent_multimodal') cycles.push({ id: receipt.candidate_id, session_id: session, event_id: receipt.event_id });
219
+ }
220
+ }
221
+ return { accepted: body.events.length, newly_accepted: newlyAccepted, new_event_ids: newEventIds, duplicates: duplicateCount, cycles_detected: cycles, triggered_compilations: triggered, receipts };
222
+ }
223
+ async function listJobs({ allowTenant = () => false, tenant, project_id } = {}) {
224
+ const params = [], where = [];
225
+ if (tenant) { params.push(tenant); where.push(`tenant=$${params.length}`); }
226
+ if (project_id) { params.push(project_id); where.push(`project_id=$${params.length}`); }
227
+ const { rows } = await db.query(`SELECT receipt FROM cbc_receipts ${where.length ? 'WHERE ' + where.join(' AND ') : ''} ORDER BY received_at LIMIT 20000`, params);
228
+ const jobs = new Map();
229
+ for (const { receipt } of rows) if (allowTenant(receipt.tenant)) project(jobs, receipt);
230
+ return [...jobs.values()];
231
+ }
232
+ return { ingest, listJobs };
233
+ }
234
+ module.exports = { createIngestionService, createPgIngestionService, qualify, validateBatch };
@@ -0,0 +1,172 @@
1
+ 'use strict';
2
+
3
+ const os = require('os');
4
+ const path = require('path');
5
+ const fs = require('fs');
6
+ const { CognitiveStore, tenantScope } = require('./store-loader');
7
+
8
+ /**
9
+ * PostgreSQL-backed store for multi-instance deployments.
10
+ *
11
+ * Reads stay synchronous and in memory (the resolver and graph builders are
12
+ * synchronous); every write goes to the database first and only then is
13
+ * installed in memory. `refresh()` reloads from the database so replicas
14
+ * converge without a shared filesystem.
15
+ */
16
+ const SCHEMA = `
17
+ CREATE TABLE IF NOT EXISTS cbc_artifacts (
18
+ type text NOT NULL CHECK (type IN ('bytecode','skill','map')),
19
+ id text NOT NULL,
20
+ raw text NOT NULL,
21
+ sha256 text NOT NULL,
22
+ private boolean NOT NULL DEFAULT false,
23
+ candidate_id text,
24
+ scope jsonb,
25
+ updated_at timestamptz NOT NULL DEFAULT now(),
26
+ PRIMARY KEY (type, id)
27
+ );
28
+ CREATE TABLE IF NOT EXISTS cbc_candidates (
29
+ id text PRIMARY KEY,
30
+ tenant text NOT NULL,
31
+ status text NOT NULL CHECK (status IN ('candidate','promoted')),
32
+ record jsonb NOT NULL,
33
+ created_at timestamptz NOT NULL DEFAULT now(),
34
+ updated_at timestamptz NOT NULL DEFAULT now()
35
+ );
36
+ CREATE INDEX IF NOT EXISTS cbc_candidates_tenant ON cbc_candidates (tenant, status);
37
+ CREATE TABLE IF NOT EXISTS cbc_receipts (
38
+ tenant text NOT NULL,
39
+ session_id text NOT NULL,
40
+ event_id text NOT NULL,
41
+ content_sha256 text NOT NULL,
42
+ project_id text,
43
+ receipt jsonb NOT NULL,
44
+ received_at timestamptz NOT NULL DEFAULT now(),
45
+ PRIMARY KEY (tenant, session_id, event_id)
46
+ );
47
+ CREATE INDEX IF NOT EXISTS cbc_receipts_project ON cbc_receipts (tenant, project_id);
48
+ CREATE TABLE IF NOT EXISTS cbc_telemetry (
49
+ id bigserial PRIMARY KEY,
50
+ ts timestamptz NOT NULL,
51
+ kind text NOT NULL,
52
+ tenant text,
53
+ record jsonb NOT NULL
54
+ );
55
+ CREATE INDEX IF NOT EXISTS cbc_telemetry_ts ON cbc_telemetry (ts);
56
+ CREATE TABLE IF NOT EXISTS cbc_tokens (
57
+ token_sha256 text PRIMARY KEY,
58
+ label text NOT NULL,
59
+ principal jsonb NOT NULL,
60
+ created_by text NOT NULL,
61
+ created_at timestamptz NOT NULL DEFAULT now(),
62
+ last_used_at timestamptz,
63
+ revoked_at timestamptz
64
+ );
65
+ `;
66
+
67
+ async function migrate(db) {
68
+ await db.query(SCHEMA);
69
+ }
70
+
71
+ class PgCognitiveStore extends CognitiveStore {
72
+ constructor(db, options = {}) {
73
+ // Scratch directory only for transient exports; never authoritative state.
74
+ super(options.scratchDir || fs.mkdtempSync(path.join(os.tmpdir(), 'cbc-pg-')), { privateDirs: false });
75
+ if (!db || typeof db.query !== 'function') throw new Error('PgCognitiveStore requires a query-capable database');
76
+ this.db = db;
77
+ this.backend = 'postgres';
78
+ }
79
+ async init() {
80
+ await migrate(this.db);
81
+ await this.refresh();
82
+ return this;
83
+ }
84
+ /** Synchronous compatibility: data is already resident after init/refresh. */
85
+ loadAll() {
86
+ return { bytecodesCount: this.bytecodes.size, skillsCount: this.skills.size, mapsCount: this.maps.size, privateRoots: [], integrity: this.loadErrors.length === 0, errors: this.loadErrors.slice() };
87
+ }
88
+ async refresh() {
89
+ const { rows } = await this.db.query('SELECT type, id, raw, private, candidate_id, scope, updated_at FROM cbc_artifacts ORDER BY type, id');
90
+ const next = { bytecode: new Map(), skill: new Map(), map: new Map() };
91
+ const errors = [];
92
+ this.bytecodes = next.bytecode; this.skills = next.skill; this.maps = next.map;
93
+ for (const row of rows) {
94
+ try {
95
+ const prepared = this._prepare(row.type, row.id, row.raw);
96
+ this._install(row.type, prepared, { root: 'postgres', private: row.private, file: `postgres:${row.type}/${row.id}`, mtime: new Date(row.updated_at).toISOString(), ...(row.candidate_id ? { candidateId: row.candidate_id, scope: row.scope } : {}) });
97
+ } catch (err) { errors.push({ file: `postgres:${row.type}/${row.id}`, message: err.message, code: 'INVALID_ARTIFACT' }); }
98
+ }
99
+ this.loadErrors = errors;
100
+ return this.loadAll();
101
+ }
102
+ async _upsertArtifact(client, type, prepared, { isPrivate, candidateId = null, scope = null }) {
103
+ await client.query(
104
+ `INSERT INTO cbc_artifacts (type, id, raw, sha256, private, candidate_id, scope, updated_at)
105
+ VALUES ($1,$2,$3,$4,$5,$6,$7,now())
106
+ ON CONFLICT (type, id) DO UPDATE SET raw=EXCLUDED.raw, sha256=EXCLUDED.sha256, private=EXCLUDED.private, candidate_id=EXCLUDED.candidate_id, scope=EXCLUDED.scope, updated_at=now()`,
107
+ [type, prepared.id, prepared.raw, prepared.sha256, isPrivate, candidateId, scope ? JSON.stringify(scope) : null]);
108
+ }
109
+ async saveArtifact(type, id, content, { private: isPrivate } = {}) {
110
+ const prepared = this._prepare(type, id, content);
111
+ const { rows } = await this.db.query('SELECT private, candidate_id FROM cbc_artifacts WHERE type=$1 AND id=$2', [type, id]);
112
+ if (rows[0]?.candidate_id) throw new Error('Published bundles are immutable; submit a new reviewed candidate');
113
+ const priv = rows[0] ? rows[0].private : Boolean(isPrivate);
114
+ await this._upsertArtifact(this.db, type, prepared, { isPrivate: priv });
115
+ this._install(type, prepared, { root: 'postgres', private: priv, file: `postgres:${type}/${id}` });
116
+ return { id, type, sha256: prepared.sha256, private: priv };
117
+ }
118
+ async saveCandidate(input) {
119
+ const payload = this._candidatePayload(input);
120
+ const id = this._candidateId(payload);
121
+ const record = { schema: 'cognitivebc/candidate-v1', id, status: 'candidate', ...payload, createdAt: new Date().toISOString(), version: 1, history: [] };
122
+ const inserted = await this.db.query(
123
+ 'INSERT INTO cbc_candidates (id, tenant, status, record) VALUES ($1,$2,$3,$4) ON CONFLICT (id) DO NOTHING RETURNING record',
124
+ [id, payload.scope.tenant, 'candidate', JSON.stringify(record)]);
125
+ if (inserted.rows.length) return record;
126
+ return this.getCandidate(id);
127
+ }
128
+ async getCandidate(id) {
129
+ if (typeof id !== 'string' || !/^candidate_[a-f0-9]{64}$/.test(id)) throw new Error('Invalid candidate ID');
130
+ const { rows } = await this.db.query('SELECT record FROM cbc_candidates WHERE id=$1', [id]);
131
+ if (!rows.length) throw Object.assign(new Error('Candidate not found'), { code: 'ENOENT' });
132
+ return this._checkCandidate(rows[0].record);
133
+ }
134
+ async listCandidates({ tenants = null, status = 'candidate', limit = 100 } = {}) {
135
+ const params = [status, Math.min(Math.max(limit, 1), 500)];
136
+ let where = 'status=$1';
137
+ if (tenants) { params.push(tenants); where += ` AND tenant = ANY($${params.length})`; }
138
+ const { rows } = await this.db.query(`SELECT record FROM cbc_candidates WHERE ${where} ORDER BY created_at DESC LIMIT $2`, params);
139
+ return rows.map(r => r.record);
140
+ }
141
+ async promoteCandidate(id, options = {}) {
142
+ if (typeof options.approvedBy !== 'string' || !options.approvedBy.trim()) throw new Error('Explicit approvedBy required');
143
+ tenantScope(options.scope);
144
+ const client = typeof this.db.connect === 'function' ? await this.db.connect() : this.db;
145
+ const release = () => { if (client !== this.db && typeof client.release === 'function') client.release(); };
146
+ try {
147
+ await client.query('BEGIN');
148
+ const { rows } = await client.query('SELECT record FROM cbc_candidates WHERE id=$1 FOR UPDATE', [id]);
149
+ if (!rows.length) throw Object.assign(new Error('Candidate not found'), { code: 'ENOENT', status: 404 });
150
+ const record = this._checkCandidate(rows[0].record);
151
+ if (options.scope.tenant !== record.scope.tenant) throw new Error('Origin tenant is immutable');
152
+ if (record.status === 'promoted') { await client.query('ROLLBACK'); return record; }
153
+ const bundle = this._bundle(record);
154
+ for (const [type, prepared] of bundle) {
155
+ const existing = await client.query('SELECT candidate_id FROM cbc_artifacts WHERE type=$1 AND id=$2', [type, prepared.id]);
156
+ if (existing.rows.length && existing.rows[0].candidate_id !== record.id) throw new Error(`Publication conflicts with existing ${type}: ${prepared.id}`);
157
+ }
158
+ const promoted = { ...record, status: 'promoted', version: 2, history: [{ approvedBy: options.approvedBy, scope: options.scope, at: new Date().toISOString() }] };
159
+ this._checkCandidate(promoted);
160
+ await client.query('UPDATE cbc_candidates SET status=$2, record=$3, updated_at=now() WHERE id=$1', [id, 'promoted', JSON.stringify(promoted)]);
161
+ for (const [type, prepared] of bundle) await this._upsertArtifact(client, type, prepared, { isPrivate: true, candidateId: record.id, scope: record.scope });
162
+ await client.query('COMMIT');
163
+ for (const [type, prepared] of bundle) this._install(type, prepared, { root: 'postgres', file: `postgres:${type}/${prepared.id}`, private: true, candidateId: record.id, scope: { ...record.scope } });
164
+ return promoted;
165
+ } catch (err) {
166
+ try { await client.query('ROLLBACK'); } catch { /* connection already failed */ }
167
+ throw err;
168
+ } finally { release(); }
169
+ }
170
+ }
171
+
172
+ module.exports = { PgCognitiveStore, migrate, SCHEMA };