UNPKG

@bonginkan/maria

Version:

MARIA OS v5.9.5 – Self-Evolving Organizational Intelligence OS | Speed Improvement Phase 3: LLM Optimization + Command Refactoring | Performance Measurement + Run Evidence System | Zero ESLint/TypeScript Errors | 人とAIが役割を持ち、学び、進化し続けるための仕事のOS | GraphRAG ×

1,082 lines (1,071 loc) 3.41 MB
#!/usr/bin/env node 'use strict'; var util$1 = require('util'); var chalk = require('chalk'); var os7 = require('os'); var path42 = require('path'); var module$1 = require('module'); var fs = require('fs'); var crypto7 = require('crypto'); var zod = require('zod'); var fsp5 = require('fs/promises'); var secretManager = require('@google-cloud/secret-manager'); var yaml2 = require('js-yaml'); var child_process = require('child_process'); var http = require('http'); var Url = require('url'); var async_hooks = require('async_hooks'); var promises = require('timers/promises'); var Stream3 = require('stream'); var buffer = require('buffer'); var net = require('net'); var https = require('https'); var zlib2 = require('zlib'); var querystring = require('querystring'); var googleAuthLibrary = require('google-auth-library'); var events = require('events'); var pLimit2 = require('p-limit'); var express3 = require('express'); var dotenv = require('dotenv'); var winston = require('winston'); var auth = require('firebase-admin/auth'); var Ajv = require('ajv'); var addFormats = require('ajv-formats'); var toml = require('toml'); var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null; function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var chalk__default = /*#__PURE__*/_interopDefault(chalk); var os7__namespace = /*#__PURE__*/_interopNamespace(os7); var path42__namespace = /*#__PURE__*/_interopNamespace(path42); var fs__namespace = /*#__PURE__*/_interopNamespace(fs); var crypto7__namespace = /*#__PURE__*/_interopNamespace(crypto7); var fsp5__namespace = /*#__PURE__*/_interopNamespace(fsp5); var yaml2__namespace = /*#__PURE__*/_interopNamespace(yaml2); var http__default = /*#__PURE__*/_interopDefault(http); var Url__namespace = /*#__PURE__*/_interopNamespace(Url); var Stream3__default = /*#__PURE__*/_interopDefault(Stream3); var https__default = /*#__PURE__*/_interopDefault(https); var zlib2__namespace = /*#__PURE__*/_interopNamespace(zlib2); var querystring__namespace = /*#__PURE__*/_interopNamespace(querystring); var pLimit2__default = /*#__PURE__*/_interopDefault(pLimit2); var express3__default = /*#__PURE__*/_interopDefault(express3); var dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv); var winston__default = /*#__PURE__*/_interopDefault(winston); var Ajv__default = /*#__PURE__*/_interopDefault(Ajv); var addFormats__default = /*#__PURE__*/_interopDefault(addFormats); var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __require = /* @__PURE__ */ ((x2) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x2, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x2)(function(x2) { if (typeof require !== "undefined") return require.apply(this, arguments); throw Error('Dynamic require of "' + x2 + '" is not supported'); }); var __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __commonJS = (cb, mod) => function __require2() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. __defProp(target, "default", { value: mod, enumerable: true }) , mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); function isTestEnv() { return process.env.NODE_ENV === "test" || process.env.VITEST === "true" || process.env.VITEST_WORKER_ID != null || process.env.JEST_WORKER_ID != null; } function isMockFn(fn) { if (!fn || typeof fn !== "function") return false; const meta = fn; return Array.isArray(meta.mock?.calls); } function stdoutLine(...args) { const text = format(...args); if (isTestEnv()) { process.stdout.write(`${text} `); if (isMockFn(console.log)) console.log(text); return; } process.stdout.write(`${text} `); } function stderrLine(...args) { const text = format(...args); if (isTestEnv()) { process.stderr.write(`${text} `); if (isMockFn(console.error)) console.error(text); return; } process.stderr.write(`${text} `); } var format; var init_tty = __esm({ "src/shared/utils/tty.ts"() { format = util$1.format; } }); var LogLevel, Logger, ChildCapableConsoleLogger, logger, envLogLevel, envLogFormat; var init_logger = __esm({ "src/utils/logger.ts"() { init_tty(); LogLevel = /* @__PURE__ */ ((LogLevel2) => { LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG"; LogLevel2[LogLevel2["INFO"] = 1] = "INFO"; LogLevel2[LogLevel2["WARN"] = 2] = "WARN"; LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR"; LogLevel2[LogLevel2["NONE"] = 4] = "NONE"; return LogLevel2; })(LogLevel || {}); Logger = class _Logger { level = 2 /* WARN */; // Default to WARN to reduce noise prefix; format = "pretty"; constructor(context) { this.prefix = context ? `[MARIA CODE][${context}]` : "[MARIA CODE]"; } setLevel(level) { this.level = level; } setFormat(format2) { this.format = format2; } out(level, tag, args) { const consoleFn = console[level]; if (this.format === "json") { const payload = { ts: (/* @__PURE__ */ new Date()).toISOString(), level: tag.replace(/\[|\]/g, "").toLowerCase(), msg: args.map(String).join(" ") }; consoleFn(JSON.stringify(payload)); return; } const debugMode = process.env.MARIA_DEBUG === "1"; const isError = tag.includes("[ERROR]"); let outArgs = args; if (isError && !debugMode) { const filtered = []; for (const a of args) { const t2 = typeof a; if (a == null || t2 === "string" || t2 === "number" || t2 === "boolean") { filtered.push(a); } } outArgs = filtered; } if (tag) { consoleFn(tag, ...outArgs); } else { consoleFn(...outArgs); } } debug(message, fields) { if (this.level <= 0 /* DEBUG */) { const args = fields ? [message, fields] : [message]; this.out("log", chalk__default.default.magenta(`${this.prefix} [DEBUG]`), args); } } info(message, fields) { if (this.level <= 1 /* INFO */) { const args = fields ? [message, fields] : [message]; this.out("log", chalk__default.default.bold.magenta(`${this.prefix} [INFO]`), args); } } warn(message, fields) { if (this.level <= 2 /* WARN */) { const args = fields ? [message, fields] : [message]; this.out("warn", chalk__default.default.bold.magenta(`${this.prefix} [WARN]`), args); } } error(message, fields) { if (this.level <= 3 /* ERROR */) { const args = fields ? [message, fields] : [message]; this.out("error", chalk__default.default.bold.magenta(`${this.prefix} [ERROR]`), args); } } success(...args) { if (this.level <= 1 /* INFO */) { this.out("log", chalk__default.default.bold.magenta(`${this.prefix} [SUCCESS]`), args); } } task(taskName, status, message) { if (process.env.MARIA_BENCH_MODE === "1") return; if (this.level > 1 /* INFO */) { return; } const statusIcons = { start: "\u{1F680}", progress: "\u23F3", complete: "\u2705", error: "\u274C" }; const statusColors = { start: chalk__default.default.bold.magenta, progress: chalk__default.default.magenta, complete: chalk__default.default.bold.magenta, error: chalk__default.default.bold.magenta }; const icon = statusIcons[status]; const color = statusColors[status]; const formattedMessage = message ? `: ${message}` : ""; stdoutLine(color(`${this.prefix} ${icon} ${taskName}${formattedMessage}`)); } table(data) { if (this.level > 1 /* INFO */) { return; } stdoutLine(data); } json(obj, pretty = true) { if (this.level > 0 /* DEBUG */) { return; } if (this.format === "json") { this.out("log", "", [JSON.stringify(obj)]); } else { stdoutLine(chalk__default.default.magenta(`${this.prefix} [JSON]`)); stdoutLine(pretty ? JSON.stringify(obj, null, 2) : JSON.stringify(obj)); } } divider() { if (this.level > 1 /* INFO */) { return; } stdoutLine(chalk__default.default.magenta("\u2500".repeat(60))); } /** * Create a child logger with additional context. * * For compatibility with existing code, supports a pino-like signature: * `logger.child({ module: "name" })`. * - If context is a string, use it directly as the prefix context. * - If context is an object, prefer the `module` field; otherwise join `key=value` pairs. */ child(context) { let ctx; if (typeof context === "string") { ctx = context; } else if (context && typeof context === "object") { const obj = context; if (typeof obj.module === "string" && obj.module.length > 0) { ctx = obj.module; } else { const entries = Object.entries(obj).filter(([_, v]) => v != null).map(([k, v]) => `${k}=${String(v)}`); ctx = entries.join(","); } } const childLogger = new _Logger(ctx); childLogger.setLevel(this.level); childLogger.setFormat(this.format); return childLogger; } clear() { console.clear(); } /** * Render a progress bar. */ progress(current, total, label) { if (process.env.MARIA_BENCH_MODE === "1") return; if (this.level > 1 /* INFO */) { return; } const percentage = Math.round(current / total * 100); const barLength = 30; const filled = Math.round(percentage / 100 * barLength); const empty = barLength - filled; const bar = "\u2588".repeat(filled) + "\u2591".repeat(empty); const progressText = `${current}/${total}`; const labelText = label ? ` ${label}` : ""; process.stdout.write( `\r${chalk__default.default.bold.magenta(bar)} ${percentage}% ${progressText}${labelText}` ); if (current === total) { process.stdout.write("\n"); } } }; ChildCapableConsoleLogger = class _ChildCapableConsoleLogger extends Logger { constructor(context) { super(context); } child(fields) { const moduleName = fields && typeof fields.module === "string" && fields.module || void 0; const ctx = moduleName ? moduleName : void 0; return new _ChildCapableConsoleLogger(ctx); } }; logger = new ChildCapableConsoleLogger(); envLogLevel = process.env["MARIA_LOG_LEVEL"]?.toUpperCase(); if (envLogLevel && LogLevel[envLogLevel] !== void 0) { logger.setLevel(LogLevel[envLogLevel]); } envLogFormat = process.env["MARIA_LOG_FORMAT"]?.toLowerCase(); if (envLogFormat === "json" || envLogFormat === "pretty") { logger.setFormat(envLogFormat); } } }); // src/adapters/logging/default.ts var defaultLogger; var init_default = __esm({ "src/adapters/logging/default.ts"() { init_logger(); defaultLogger = logger; } }); function getMariaHomeDir() { const explicit = process.env.MARIA_HOME_DIR?.trim(); if (explicit) return explicit; const baseHome = process.env.HOME?.trim() || os7__namespace.homedir() || process.cwd(); return path42__namespace.join(baseHome, ".maria"); } function resolveEnvelopeLedgerDbPath() { const override = String(process.env.ENVELOPE_LEDGER_DB_PATH || "").trim(); if (override) return override; return path42__namespace.join(getMariaHomeDir(), "envelope-ledger", "envelope-ledger.db"); } var init_EnvelopeLedgerPaths = __esm({ "src/services/envelope-ledger/EnvelopeLedgerPaths.ts"() { } }); function tryGetBetterSqlite3DatabaseCtor() { try { const req = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('server-express.cjs', document.baseURI).href))); const mod = req("better-sqlite3"); const maybeObj = mod; const Database2 = maybeObj && typeof maybeObj === "object" ? maybeObj.default ?? mod : mod; if (typeof Database2 !== "function") return null; return { Database: Database2 }; } catch { return null; } } var init_better_sqlite3_loader = __esm({ "src/services/envelope-ledger/better-sqlite3-loader.ts"() { } }); function ensureDirExistsSync(dir) { try { if (!fs__namespace.existsSync(dir)) fs__namespace.mkdirSync(dir, { recursive: true }); } catch { } } function safeJson(_obj) { try { return JSON.stringify(_obj ?? null); } catch { return "null"; } } function parseJsonObject(_text) { try { const v = JSON.parse(String(_text || "null")); if (!v || typeof v !== "object" || Array.isArray(v)) return null; return v; } catch { return null; } } function nodeIdQueue(_queueId) { return `a2a.queue:${_queueId}`; } function nodeIdEnvelope(_envelopeId) { return `a2a.envelope:${_envelopeId}`; } function nodeIdCorrelation(_correlationId) { return `a2a.correlation:${_correlationId}`; } function nodeIdAttempt(_queueId, _attemptNo) { return `a2a.attempt:${_queueId}:${_attemptNo}`; } function nodeIdDecision(_decisionId) { return `a2a.decision:${_decisionId}`; } function edgeId(_kind, _src, _dst) { return `${_kind}:${_src}->${_dst}`; } var EnvelopeOpsKgService; var init_EnvelopeOpsKgService = __esm({ "src/services/envelope-ledger/EnvelopeOpsKgService.ts"() { init_EnvelopeLedgerPaths(); init_better_sqlite3_loader(); EnvelopeOpsKgService = class { db; constructor(dbPath) { const p = dbPath || resolveEnvelopeLedgerDbPath(); ensureDirExistsSync(path42__namespace.dirname(p)); const loaded = tryGetBetterSqlite3DatabaseCtor(); if (!loaded) { throw new Error("better-sqlite3 native bindings are missing. Run `pnpm rebuild better-sqlite3`."); } this.db = new loaded.Database(p); this.db.pragma("journal_mode = WAL"); this.db.pragma("synchronous = NORMAL"); this.db.pragma("busy_timeout = 5000"); this.initializeSchema(); } initializeSchema() { this.db.exec(` CREATE TABLE IF NOT EXISTS ops_kg_nodes ( node_id TEXT NOT NULL, kind TEXT NOT NULL, label TEXT NOT NULL, attrs_json TEXT NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (node_id) ); CREATE TABLE IF NOT EXISTS ops_kg_edges ( edge_id TEXT NOT NULL, kind TEXT NOT NULL, src_id TEXT NOT NULL, dst_id TEXT NOT NULL, ts TEXT NOT NULL, attrs_json TEXT NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (edge_id) ); CREATE INDEX IF NOT EXISTS idx_ops_kg_edges_src ON ops_kg_edges (src_id); CREATE INDEX IF NOT EXISTS idx_ops_kg_edges_dst ON ops_kg_edges (dst_id); CREATE INDEX IF NOT EXISTS idx_ops_kg_edges_kind ON ops_kg_edges (kind, ts); CREATE TABLE IF NOT EXISTS ops_kg_meta ( key TEXT NOT NULL, value TEXT NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (key) ); `); } getWatermark(key) { try { const rowRaw = this.db.prepare("SELECT value FROM ops_kg_meta WHERE key = ?").get(key); const row = rowRaw && typeof rowRaw === "object" && rowRaw !== null ? rowRaw : null; const parsed = row && typeof row.value === "string" ? parseJsonObject(row.value) : null; const ts = typeof parsed?.ts === "string" ? String(parsed.ts) : "1970-01-01T00:00:00.000Z"; const event_id = typeof parsed?.event_id === "string" ? String(parsed.event_id) : ""; return { ts, event_id }; } catch { return { ts: "1970-01-01T00:00:00.000Z", event_id: "" }; } } setWatermark(key, w) { const now3 = Date.now(); this.db.prepare("INSERT INTO ops_kg_meta(key, value, updated_at) VALUES (?, ?, ?) ON CONFLICT(key) DO UPDATE SET value=excluded.value, updated_at=excluded.updated_at").run(key, safeJson({ ts: w.ts, event_id: w.event_id }), now3); } syncA2AFromLedger(params) { const limit = typeof params?.limit === "number" ? Math.max(1, Math.floor(params.limit)) : 2e3; const wm = this.getWatermark("ops_kg_watermark:a2a"); const events = this.db.prepare(` SELECT system, event_id, envelope_id, correlation_id, queue_id, message_id, transport, attempt_no, outcome, type, ts, payload_json FROM events WHERE system = 'a2a' AND (ts > ? OR (ts = ? AND event_id > ?)) ORDER BY ts ASC, event_id ASC LIMIT ? `).all(wm.ts, wm.ts, wm.event_id, limit); const now3 = Date.now(); const upsertNode = this.db.prepare("INSERT INTO ops_kg_nodes(node_id, kind, label, attrs_json, updated_at) VALUES (?, ?, ?, ?, ?) ON CONFLICT(node_id) DO UPDATE SET kind=excluded.kind, label=excluded.label, attrs_json=excluded.attrs_json, updated_at=excluded.updated_at"); const upsertEdge = this.db.prepare("INSERT INTO ops_kg_edges(edge_id, kind, src_id, dst_id, ts, attrs_json, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(edge_id) DO UPDATE SET kind=excluded.kind, src_id=excluded.src_id, dst_id=excluded.dst_id, ts=excluded.ts, attrs_json=excluded.attrs_json, updated_at=excluded.updated_at"); let last = wm; const tx = this.db.transaction(() => { for (const e2 of events) { last = { ts: e2.ts, event_id: e2.event_id }; const payload = parseJsonObject(e2.payload_json) ?? {}; const queueId = e2.queue_id ? String(e2.queue_id) : ""; const envelopeId = e2.envelope_id ? String(e2.envelope_id) : ""; const correlationId = e2.correlation_id ? String(e2.correlation_id) : ""; const attemptNo = typeof e2.attempt_no === "number" ? e2.attempt_no : null; const decisionIdRaw = payload["decision_id"]; const decisionId = typeof decisionIdRaw === "string" && decisionIdRaw.trim() ? decisionIdRaw.trim() : ""; if (queueId) { upsertNode.run(nodeIdQueue(queueId), "queue", queueId, safeJson({ queue_id: queueId }), now3); } if (envelopeId) { upsertNode.run(nodeIdEnvelope(envelopeId), "envelope", envelopeId, safeJson({ envelope_id: envelopeId }), now3); } if (correlationId) { upsertNode.run(nodeIdCorrelation(correlationId), "correlation", correlationId, safeJson({ correlation_id: correlationId }), now3); } if (decisionId) { upsertNode.run(nodeIdDecision(decisionId), "decision", decisionId, safeJson({ decision_id: decisionId }), now3); } if (queueId && envelopeId) { upsertEdge.run(edgeId("HAS_ENVELOPE", nodeIdQueue(queueId), nodeIdEnvelope(envelopeId)), "HAS_ENVELOPE", nodeIdQueue(queueId), nodeIdEnvelope(envelopeId), e2.ts, safeJson({ source_event: e2.event_id }), now3); } if (correlationId && queueId) { upsertEdge.run(edgeId("HAS_QUEUE", nodeIdCorrelation(correlationId), nodeIdQueue(queueId)), "HAS_QUEUE", nodeIdCorrelation(correlationId), nodeIdQueue(queueId), e2.ts, safeJson({ source_event: e2.event_id }), now3); } if (decisionId && queueId) { upsertEdge.run(edgeId("DECISION_ON_QUEUE", nodeIdDecision(decisionId), nodeIdQueue(queueId)), "DECISION_ON_QUEUE", nodeIdDecision(decisionId), nodeIdQueue(queueId), e2.ts, safeJson({ source_event: e2.event_id, type: e2.type }), now3); } if (queueId && attemptNo != null) { upsertNode.run(nodeIdAttempt(queueId, attemptNo), "attempt", `${queueId}#${attemptNo}`, safeJson({ queue_id: queueId, attempt_no: attemptNo, transport: e2.transport ?? null }), now3); upsertEdge.run(edgeId("HAS_ATTEMPT", nodeIdQueue(queueId), nodeIdAttempt(queueId, attemptNo)), "HAS_ATTEMPT", nodeIdQueue(queueId), nodeIdAttempt(queueId, attemptNo), e2.ts, safeJson({ source_event: e2.event_id }), now3); } if (queueId && attemptNo != null && (e2.type === "a2a_delivery_attempt_result" || e2.type === "a2a_delivery_ack")) { upsertEdge.run(edgeId("ATTEMPT_EVENT", nodeIdAttempt(queueId, attemptNo), `a2a.event:${e2.event_id}`), "ATTEMPT_EVENT", nodeIdAttempt(queueId, attemptNo), `a2a.event:${e2.event_id}`, e2.ts, safeJson({ type: e2.type, outcome: e2.outcome ?? null, transport: e2.transport ?? null, message_id: e2.message_id ?? null }), now3); upsertNode.run(`a2a.event:${e2.event_id}`, "event", e2.type, safeJson({ event_id: e2.event_id, type: e2.type, ts: e2.ts, outcome: e2.outcome ?? null }), now3); } } }); tx(); if (events.length) this.setWatermark("ops_kg_watermark:a2a", last); return { processed: events.length, watermark: events.length ? last : wm }; } syncDecisionOsFromLedger(params) { const limit = typeof params?.limit === "number" ? Math.max(1, Math.floor(params.limit)) : 2e3; const wm = this.getWatermark("ops_kg_watermark:decision-os"); const events = this.db.prepare(` SELECT system, event_id, envelope_id, correlation_id, queue_id, message_id, transport, attempt_no, outcome, type, ts, payload_json FROM events WHERE system = 'decision-os' AND (ts > ? OR (ts = ? AND event_id > ?)) ORDER BY ts ASC, event_id ASC LIMIT ? `).all(wm.ts, wm.ts, wm.event_id, limit); const now3 = Date.now(); const upsertNode = this.db.prepare("INSERT INTO ops_kg_nodes(node_id, kind, label, attrs_json, updated_at) VALUES (?, ?, ?, ?, ?) ON CONFLICT(node_id) DO UPDATE SET kind=excluded.kind, label=excluded.label, attrs_json=excluded.attrs_json, updated_at=excluded.updated_at"); const upsertEdge = this.db.prepare("INSERT INTO ops_kg_edges(edge_id, kind, src_id, dst_id, ts, attrs_json, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(edge_id) DO UPDATE SET kind=excluded.kind, src_id=excluded.src_id, dst_id=excluded.dst_id, ts=excluded.ts, attrs_json=excluded.attrs_json, updated_at=excluded.updated_at"); const nodeDecision = (decisionId) => `decision-os.decision:${decisionId}`; const nodeGateEnv = (envelopeId) => `decision-os.gate_env:${envelopeId}`; const nodeCommand = (cmd) => `decision-os.command:${cmd}`; let last = wm; const tx = this.db.transaction(() => { for (const e2 of events) { last = { ts: e2.ts, event_id: e2.event_id }; if (e2.type !== "decision_gate") continue; const payload = parseJsonObject(e2.payload_json) ?? {}; const decisionId = typeof payload["decision_id"] === "string" ? String(payload["decision_id"]) : ""; const gateStatus = typeof payload["gate_status"] === "string" ? String(payload["gate_status"]) : ""; const approved = typeof payload["approved"] === "boolean" ? payload["approved"] : null; const command = typeof payload["command"] === "string" ? String(payload["command"]) : ""; if (decisionId) { upsertNode.run(nodeDecision(decisionId), "decision", decisionId, safeJson({ decision_id: decisionId }), now3); } if (command) { upsertNode.run(nodeCommand(command), "command", command, safeJson({ command }), now3); } if (e2.envelope_id) { upsertNode.run(nodeGateEnv(e2.envelope_id), "envelope", e2.envelope_id, safeJson({ envelope_id: e2.envelope_id }), now3); } if (decisionId && e2.envelope_id) { upsertEdge.run(edgeId("DECISION_GATE", nodeDecision(decisionId), nodeGateEnv(e2.envelope_id)), "DECISION_GATE", nodeDecision(decisionId), nodeGateEnv(e2.envelope_id), e2.ts, safeJson({ gate_status: gateStatus || null, approved, source_event: e2.event_id }), now3); } if (e2.envelope_id && command) { upsertEdge.run(edgeId("GATE_COMMAND", nodeGateEnv(e2.envelope_id), nodeCommand(command)), "GATE_COMMAND", nodeGateEnv(e2.envelope_id), nodeCommand(command), e2.ts, safeJson({ gate_status: gateStatus || null, approved, source_event: e2.event_id }), now3); } } }); tx(); if (events.length) this.setWatermark("ops_kg_watermark:decision-os", last); return { processed: events.filter((x2) => x2.type === "decision_gate").length, watermark: events.length ? last : wm }; } syncGovernanceFromLedger(params) { const limit = typeof params?.limit === "number" ? Math.max(1, Math.floor(params.limit)) : 2e3; const wm = this.getWatermark("ops_kg_watermark:governance"); const events = this.db.prepare(` SELECT system, event_id, envelope_id, correlation_id, queue_id, message_id, transport, attempt_no, outcome, type, ts, payload_json FROM events WHERE system = 'governance' AND (ts > ? OR (ts = ? AND event_id > ?)) ORDER BY ts ASC, event_id ASC LIMIT ? `).all(wm.ts, wm.ts, wm.event_id, limit); const now3 = Date.now(); const upsertNode = this.db.prepare("INSERT INTO ops_kg_nodes(node_id, kind, label, attrs_json, updated_at) VALUES (?, ?, ?, ?, ?) ON CONFLICT(node_id) DO UPDATE SET kind=excluded.kind, label=excluded.label, attrs_json=excluded.attrs_json, updated_at=excluded.updated_at"); const upsertEdge = this.db.prepare("INSERT INTO ops_kg_edges(edge_id, kind, src_id, dst_id, ts, attrs_json, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(edge_id) DO UPDATE SET kind=excluded.kind, src_id=excluded.src_id, dst_id=excluded.dst_id, ts=excluded.ts, attrs_json=excluded.attrs_json, updated_at=excluded.updated_at"); const nodeReq = (requestId) => `governance.approval_request:${requestId}`; const nodeResp = (requestId) => `governance.approval_response:${requestId}`; const nodeDecision = (decisionId) => `decision-os.decision:${decisionId}`; const nodeCorrelation = (correlationId) => `ops.correlation:${correlationId}`; let last = wm; let processed = 0; const tx = this.db.transaction(() => { for (const e2 of events) { last = { ts: e2.ts, event_id: e2.event_id }; const payload = parseJsonObject(e2.payload_json) ?? {}; const requestId = typeof payload["request_id"] === "string" ? String(payload["request_id"]) : typeof e2.correlation_id === "string" ? String(e2.correlation_id) : ""; if (!requestId) continue; processed += 1; const correlationId = typeof payload["correlation_id"] === "string" && String(payload["correlation_id"]).trim() ? String(payload["correlation_id"]).trim() : typeof e2.correlation_id === "string" && String(e2.correlation_id).trim() ? String(e2.correlation_id).trim() : ""; const decisionId = typeof payload["decision_id"] === "string" && String(payload["decision_id"]).trim() ? String(payload["decision_id"]).trim() : ""; upsertNode.run(nodeReq(requestId), "approval_request", requestId, safeJson({ request_id: requestId }), now3); upsertNode.run(`governance.event:${e2.event_id}`, "event", e2.type, safeJson({ event_id: e2.event_id, type: e2.type }), now3); upsertEdge.run(edgeId("REQUEST_EVENT", nodeReq(requestId), `governance.event:${e2.event_id}`), "REQUEST_EVENT", nodeReq(requestId), `governance.event:${e2.event_id}`, e2.ts, safeJson({ source_event: e2.event_id, type: e2.type }), now3); if (correlationId) { upsertNode.run(nodeCorrelation(correlationId), "correlation", correlationId, safeJson({ correlation_id: correlationId }), now3); upsertEdge.run(edgeId("CORRELATION_HAS_APPROVAL", nodeCorrelation(correlationId), nodeReq(requestId)), "CORRELATION_HAS_APPROVAL", nodeCorrelation(correlationId), nodeReq(requestId), e2.ts, safeJson({ source_event: e2.event_id }), now3); } if (decisionId) { upsertNode.run(nodeDecision(decisionId), "decision", decisionId, safeJson({ decision_id: decisionId }), now3); upsertEdge.run(edgeId("DECISION_REQUIRES_APPROVAL", nodeDecision(decisionId), nodeReq(requestId)), "DECISION_REQUIRES_APPROVAL", nodeDecision(decisionId), nodeReq(requestId), e2.ts, safeJson({ source_event: e2.event_id }), now3); } if (e2.type === "approval_responded") { upsertNode.run(nodeResp(requestId), "approval_response", requestId, safeJson(payload), now3); upsertEdge.run(edgeId("HAS_RESPONSE", nodeReq(requestId), nodeResp(requestId)), "HAS_RESPONSE", nodeReq(requestId), nodeResp(requestId), e2.ts, safeJson({ source_event: e2.event_id, approved: payload["approved"] ?? null, action: payload["action"] ?? null }), now3); } } }); tx(); if (events.length) this.setWatermark("ops_kg_watermark:governance", last); return { processed, watermark: events.length ? last : wm }; } getSubgraphForQueue(params) { const queueId = String(params.queueId || "").trim(); if (!queueId) return { nodes: [], edges: [] }; const limit = typeof params.limit === "number" ? Math.max(1, Math.floor(params.limit)) : 500; const q = nodeIdQueue(queueId); const edges = this.db.prepare(` SELECT edge_id, kind, src_id, dst_id, ts, attrs_json, updated_at FROM ops_kg_edges WHERE src_id = ? OR dst_id = ? ORDER BY ts ASC LIMIT ? `).all(q, q, limit); const nodeIds = /* @__PURE__ */ new Set([q]); for (const e2 of edges) { nodeIds.add(e2.src_id); nodeIds.add(e2.dst_id); } const nodes = []; const stmt = this.db.prepare("SELECT node_id, kind, label, attrs_json, updated_at FROM ops_kg_nodes WHERE node_id = ?"); for (const id of Array.from(nodeIds)) { const row = stmt.get(id); if (row) nodes.push(row); } return { nodes, edges }; } getSubgraphForDecision(params) { const decisionId = String(params.decisionId || "").trim(); if (!decisionId) return { nodes: [], edges: [] }; const limit = typeof params.limit === "number" ? Math.max(1, Math.floor(params.limit)) : 500; const d = `decision-os.decision:${decisionId}`; const edges = this.db.prepare(` SELECT edge_id, kind, src_id, dst_id, ts, attrs_json, updated_at FROM ops_kg_edges WHERE src_id = ? OR dst_id = ? ORDER BY ts ASC LIMIT ? `).all(d, d, limit); const nodeIds = /* @__PURE__ */ new Set([d]); for (const e2 of edges) { nodeIds.add(e2.src_id); nodeIds.add(e2.dst_id); } const nodes = []; const stmt = this.db.prepare("SELECT node_id, kind, label, attrs_json, updated_at FROM ops_kg_nodes WHERE node_id = ?"); for (const id of Array.from(nodeIds)) { const row = stmt.get(id); if (row) nodes.push(row); } return { nodes, edges }; } getSubgraphForApprovalRequest(params) { const requestId = String(params.requestId || "").trim(); if (!requestId) return { nodes: [], edges: [] }; const limit = typeof params.limit === "number" ? Math.max(1, Math.floor(params.limit)) : 500; const r2 = `governance.approval_request:${requestId}`; const edges = this.db.prepare(` SELECT edge_id, kind, src_id, dst_id, ts, attrs_json, updated_at FROM ops_kg_edges WHERE src_id = ? OR dst_id = ? ORDER BY ts ASC LIMIT ? `).all(r2, r2, limit); const nodeIds = /* @__PURE__ */ new Set([r2]); for (const e2 of edges) { nodeIds.add(e2.src_id); nodeIds.add(e2.dst_id); } const nodes = []; const stmt = this.db.prepare("SELECT node_id, kind, label, attrs_json, updated_at FROM ops_kg_nodes WHERE node_id = ?"); for (const id of Array.from(nodeIds)) { const row = stmt.get(id); if (row) nodes.push(row); } return { nodes, edges }; } toMermaid(params) { const g = this.getSubgraphForQueue({ queueId: params.queueId, limit: params.limit }); const parseAttrs = (s2) => { try { const v = JSON.parse(String(s2 || "null")); return v && typeof v === "object" ? v : null; } catch { return null; } }; const edgeAttrs = new Map(g.edges.map((e2) => [e2.edge_id, parseAttrs(e2.attrs_json)])); new Map(g.nodes.map((n) => [n.node_id, parseAttrs(n.attrs_json)])); const edgesByNode = /* @__PURE__ */ new Map(); for (const e2 of g.edges) { edgesByNode.set(e2.src_id, [...edgesByNode.get(e2.src_id) || [], e2]); edgesByNode.set(e2.dst_id, [...edgesByNode.get(e2.dst_id) || [], e2]); } const label = (n) => { const safeBase = String(n.label || n.node_id).replace(/\"/g, "'"); const nodeId = n.node_id; const edges = edgesByNode.get(nodeId) || []; if (n.kind === "approval_request") { const resp = edges.find((e2) => e2.kind === "HAS_RESPONSE" && e2.src_id === nodeId); if (resp) { const aRaw = edgeAttrs.get(resp.edge_id); const a = aRaw && typeof aRaw === "object" && aRaw !== null ? aRaw : null; const approved = a && typeof a.approved === "boolean" ? a.approved : null; const badge = approved === true ? "APPROVED" : approved === false ? "REJECTED" : "RESOLVED"; return `"[${badge}] ${safeBase}"`; } return `"[APPROVAL_PENDING] ${safeBase}"`; } if (n.kind === "queue") { const attemptEvents = edges.filter((e2) => e2.kind === "ATTEMPT_EVENT"); const last = attemptEvents.map((e2) => { const aRaw = edgeAttrs.get(e2.edge_id); const a = aRaw && typeof aRaw === "object" && aRaw !== null ? aRaw : null; return { e: e2, a }; }).sort((x2, y) => String(x2.e.ts).localeCompare(String(y.e.ts))).slice(-1)[0]; const outcome = last?.a && typeof last.a.outcome === "string" ? String(last.a.outcome) : ""; const badge = outcome === "DELIVERED" ? "DELIVERED" : outcome === "FAILED" ? "FAILED" : "PENDING"; return `"[${badge}] ${safeBase}"`; } if (n.kind === "decision") { const gate = edges.find((e2) => e2.kind === "DECISION_GATE" && e2.src_id === nodeId); const aRaw = gate ? edgeAttrs.get(gate.edge_id) : null; const a = aRaw && typeof aRaw === "object" && aRaw !== null ? aRaw : null; const status = a && typeof a.gate_status === "string" ? String(a.gate_status) : ""; const approved = a && typeof a.approved === "boolean" ? a.approved : null; const badge = status === "PASS" ? "PASS" : approved === true ? "APPROVED" : status ? "APPROVAL_PENDING" : "DECISION"; return `"[${badge}] ${safeBase}"`; } return `"${safeBase}"`; }; const nodeById = new Map(g.nodes.map((n) => [n.node_id, n])); const lines = []; lines.push("graph TD"); for (const e2 of g.edges) { const a = nodeById.get(e2.src_id); const b = nodeById.get(e2.dst_id); if (!a || !b) continue; const src = e2.src_id.replace(/[^a-zA-Z0-9_]/g, "_"); const dst = e2.dst_id.replace(/[^a-zA-Z0-9_]/g, "_"); const eaRaw = edgeAttrs.get(e2.edge_id); const ea = eaRaw && typeof eaRaw === "object" && eaRaw !== null ? eaRaw : null; const note = e2.kind === "HAS_RESPONSE" ? ea && typeof ea.action === "string" ? `:${String(ea.action)}` : "" : e2.kind === "ATTEMPT_EVENT" ? ea && (typeof ea.type === "string" || typeof ea.outcome === "string") ? `:${String(ea.type || "")}${typeof ea.outcome === "string" ? `:${String(ea.outcome)}` : ""}` : "" : e2.kind === "DECISION_GATE" ? ea && (typeof ea.gate_status === "string" || typeof ea.approved === "boolean") ? `:${String(ea.gate_status || "")}${typeof ea.approved === "boolean" ? `:${ea.approved ? "approved" : "blocked"}` : ""}` : "" : ""; lines.push(` ${src}[${label(a)}] -->|${e2.kind}${note}| ${dst}[${label(b)}]`); } if (lines.length === 1) { lines.push(` empty["(no edges yet)"]`); } return lines.join("\n"); } toMermaidDecision(params) { const g = this.getSubgraphForDecision({ decisionId: params.decisionId, limit: params.limit }); const parseAttrs = (s2) => { try { const v = JSON.parse(String(s2 || "null")); return v && typeof v === "object" ? v : null; } catch { return null; } }; const edgeAttrs = new Map(g.edges.map((e2) => [e2.edge_id, parseAttrs(e2.attrs_json)])); const edgesByNode = /* @__PURE__ */ new Map(); for (const e2 of g.edges) { edgesByNode.set(e2.src_id, [...edgesByNode.get(e2.src_id) || [], e2]); edgesByNode.set(e2.dst_id, [...edgesByNode.get(e2.dst_id) || [], e2]); } const label = (n) => { const safeBase = String(n.label || n.node_id).replace(/\"/g, "'"); const nodeId = n.node_id; const edges = edgesByNode.get(nodeId) || []; if (n.kind === "decision") { const gate = edges.find((e2) => e2.kind === "DECISION_GATE" && e2.src_id === nodeId); const aRaw = gate ? edgeAttrs.get(gate.edge_id) : null; const a = aRaw && typeof aRaw === "object" && aRaw !== null ? aRaw : null; const status = a && typeof a.gate_status === "string" ? String(a.gate_status) : ""; const approved = a && typeof a.approved === "boolean" ? a.approved : null; const badge = status === "PASS" ? "PASS" : approved === true ? "APPROVED" : status ? "APPROVAL_PENDING" : "DECISION"; return `"[${badge}] ${safeBase}"`; } if (n.kind === "approval_request") { const resp = edges.find((e2) => e2.kind === "HAS_RESPONSE" && e2.src_id === nodeId); if (resp) { const aRaw = edgeAttrs.get(resp.edge_id); const a = aRaw && typeof aRaw === "object" && aRaw !== null ? aRaw : null; const approved = a && typeof a.approved === "boolean" ? a.approved : null; const badge = approved === true ? "APPROVED" : approved === false ? "REJECTED" : "RESOLVED"; return `"[${badge}] ${safeBase}"`; } return `"[APPROVAL_PENDING] ${safeBase}"`; } return `"${safeBase}"`; }; const nodeById = new Map(g.nodes.map((n) => [n.node_id, n])); const lines = []; lines.push("graph TD"); for (const e2 of g.edges) { const a = nodeById.get(e2.src_id); const b = nodeById.get(e2.dst_id); if (!a || !b) continue; const src = e2.src_id.replace(/[^a-zA-Z0-9_]/g, "_"); const dst = e2.dst_id.replace(/[^a-zA-Z0-9_]/g, "_"); const eaRaw = edgeAttrs.get(e2.edge_id); const ea = eaRaw && typeof eaRaw === "object" && eaRaw !== null ? eaRaw : null; const note = e2.kind === "HAS_RESPONSE" ? ea && typeof ea.action === "string" ? `:${String(ea.action)}` : "" : e2.kind === "DECISION_GATE" ? ea && (typeof ea.gate_status === "string" || typeof ea.approved === "boolean") ? `:${String(ea.gate_status || "")}${typeof ea.approved === "boolean" ? `:${ea.approved ? "approved" : "blocked"}` : ""}` : "" : ""; lines.push(` ${src}[${label(a)}] -->|${e2.kind}${note}| ${dst}[${label(b)}]`); } if (lines.length === 1) lines.push(` empty["(no edges yet)"]`); return lines.join("\n"); } toMermaidApprovalRequest(params) { const g = this.getSubgraphForApprovalRequest({ requestId: params.requestId, limit: params.limit }); const parseAttrs = (s2) => { try { const v = JSON.parse(String(s2 || "null")); return v && typeof v === "object" ? v : null; } catch { return null; } }; const edgeAttrs = new Map(g.edges.map((e2) => [e2.edge_id, parseAttrs(e2.attrs_json)])); const edgesByNode = /* @__PURE__ */ new Map(); for (const e2 of g.edges) { edgesByNode.set(e2.src_id, [...edgesByNode.get(e2.src_id) || [], e2]); edgesByNode.set(e2.dst_id, [...edgesByNode.get(e2.dst_id) || [], e2]); } const label = (n) => { const safeBase = String(n.label || n.node_id).replace(/\"/g, "'"); const nodeId = n.node_id; const edges = edgesByNode.get(nodeId) || []; if (n.kind === "approval_request") { const resp = edges.find((e2) => e2.kind === "HAS_RESPONSE" && e2.src_id === nodeId); if (resp) { const aRaw = edgeAttrs.get(resp.edge_id); const a = aRaw && typeof aRaw === "object" && aRaw !== null ? aRaw : null; const approved = a && typeof a.approved === "boolean" ? a.approved : null; const badge = approved === true ? "APPROVED" : approved === false ? "REJECTED" : "RESOLVED"; return `"[${badge}] ${safeBase}"`; } return `"[APPROVAL_PENDING] ${safeBase}"`; } if (n.kind === "decision") { const gate = edges.find((e2) => e2.kind === "DECISION_GATE" && e2.src_id === nodeId); const aRaw = gate ? edgeAttrs.get(gate.edge_id) : null; const a = aRaw && typeof aRaw === "object" && aRaw !== null ? aRaw : null; const status = a && typeof a.gate_status === "string" ? String(a.gate_status) : ""; const approved = a && typeof a.approved === "boolean" ? a.approved : null; const badge = status === "PASS" ? "PASS" : approved === true ? "APPROVED" : status ? "APPROVAL_PENDING" : "DECISION"; return `"[${badge}] ${safeBase}"`; } return `"${safeBase}"`; }; const nodeById = new Map(g.nodes.map((n) => [n.node_id, n])); const lines = []; lines.push("graph TD"); for (const e2 of g.edges) { const a = nodeById.get(e2.src_id); const b = nodeById.get(e2.dst_id); if (!a || !b) continue; const src = e2.src_id.replace(/[^a-zA-Z0-9_]/g, "_"); const dst = e2.dst_id.replace(/[^a-zA-Z0-9_]/g, "_"); const eaRaw = edgeAttrs.get(e2.edge_id); const ea = eaRaw && typeof eaRaw === "object" && eaRaw !== null ? eaRaw : null; const note = e2.kind === "HAS_RESPONSE" ? ea && typeof ea.action === "string" ? `:${String(ea.action)}` : "" : e2.kind === "DECISION_GATE" ? ea && (typeof ea.gate_status === "string" || typeof ea.approved === "boolean") ? `:${String(ea.gate_status || "")}${typeof ea.approved === "boolean" ? `:${ea.approved ? "approved" : "blocked"}` : ""}` : "" : e2.kind === "REQUEST_EVENT" ? ea && typeof ea.type === "string" ? `:${String(ea.type)}` : "" : ""; lines.push(` ${src}[${label(a)}] -->|${e2.kind}${note}| ${dst}[${label(b)}]`); } if (lines.length === 1) lines.push(` empty["(no edges yet)"]`); return lines.join("\n"); } }; } }); // src/services/envelope-ledger/EnvelopeOpsKgRealtimeSync.ts function clamp(n, min, max) { return Math.max(min, Math.min(max, n)); } var EnvelopeOpsKgRealtimeSync; var init_EnvelopeOpsKgRealtimeSync = __esm({ "src/services/envelope-ledger/EnvelopeOpsKgRealtimeSync.ts"() { init_EnvelopeOpsKgService(); EnvelopeOpsKgRealtimeSync = class { kg; pending = /* @__PURE__ */ new Set(); timer = null; backoffMs = 0; constructor(dbPath) { this.kg = new EnvelopeOpsKgService(dbPath); } enqueue(system) { this.pending.add(system); this.schedule(); } schedule() { if (this.timer) return; const delay2 = this.backoffMs > 0 ? this.backoffMs : 10; this.timer = setTimeout(() => { this.timer = null; this.flush(); }, delay2); } flush() { const systems = Array.from(this.pending); this.pending.clear(); if (!systems.length) return; try { const limit = 200; for (const s2 of systems) { if (s2 === "a2a") this.kg.syncA2AFromLedger({ limit }); if (s2 === "decision-os") this.kg.syncDecisionOsFromLedger({ limit }); if (s2 === "governance") this.kg.syncGovernanceFromLedger({ limit }); } this.backoffMs = 0; } catch { this.backoffMs = this.backoffMs ? clamp(this.backoffMs * 2, 50, 2e3) : 50; for (const s2 of systems) this.pending.add(s2); this.schedule(); } } }; } }); function stableSortKeys(v) { if (Array.isArray(v)) return v.map(stableSortKeys); if (!v || typeof v !== "object") return v; const out = {}; for (const k of Object.keys(v).sort()) out[k] = stableSortKeys(v[k]); return out; } function canonicalJsonSorted(obj) { return JSON.stringify(stableSortKeys(obj)); } function sha256Hex(input) { return crypto7__namespace.createHash("sha256").update(input, "utf8").digest("hex"); } function buildChecksum(params) { return { algo: "sha256", canonicalization: "sorted-json", input_hash: `sha256:${sha256Hex(canonicalJsonSorted(params.input))}`, output_hash: `sha256:${sha256Hex(canonicalJsonSorted(params.output))}` }; } var init_ssot_envelope = __esm({ "src/shared/envelope-ssot/ssot-envelope.ts"() { } }); var SSOT_STATUS, SsotStatusSchema, AGENTS_TEAM_STATUS, AGENTS_WORKLOAD_CACHE_STATUS, AUTO_DEV_JOB_STATUS, UNIVERSE_TRACE_STATUS, UNIVERSE_ENVELOPE_STATE, DECISION_STATUS, FINAL_DECISION, HITL_ITEM_STATUS, JOB_REGISTRY_STATUS; var init_status = __esm({ "src/shared/status/status.ts"() { SSOT_STATUS = ["success", "failed", "hold", "running", "unknown"]; SsotStatusSchema = zod.z.enum(SSOT_STATUS); AGENTS_TEAM_STATUS = [ "draft", "planned", "running", "paused", "interrupted", "completed", "failed" ]; zod.z.enum(AGENTS_TEAM_STATUS); AGENTS_WORKLOAD_CACHE_STATUS = ["pending", "done", "failed"]; zod.z.enum(AGENTS_WORKLOAD_CACHE_STATUS); AUTO_DEV_JOB_STATUS = ["success", "partial", "failed", "suggest_only", "aborted"]; zod.z.enum(AUTO_DEV_JOB_STATUS); UNIVERSE_TRACE_STATUS = [ "RECEIVED", "PLANNING", "GOVERNING", "IMPLEMENTING", "TESTING", "DEPLOYING", "DELIVERING", "BLOCKED", "SUCCEEDED", "FAILED", "CANCELLED" ]; zod.z.enum(UNIVERSE_TRACE_STATUS); UNIVERSE_ENVELOPE_STATE = [ "DRAFT", "SIGNED", "DISPATCHED", "RUNNING", "BLOCKED", "SUCCEEDED", "FAILED", "CANCELLED"