@sanctuary-framework/mcp-server 1.2.10 → 1.2.11

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/dist/index.js CHANGED
@@ -16558,6 +16558,306 @@ async function handleSentinelRoute(deps, req, res) {
16558
16558
  return true;
16559
16559
  }
16560
16560
  }
16561
+ var HANDOFF_LOG_OBSERVED_OPS = {
16562
+ /** Tau-3 in-process coordination handoff. */
16563
+ LOCAL_HANDOFF: "v1.1_local_handoff",
16564
+ /** Upsilon-1 cross-harness approval (wrapped-agent -> operator). */
16565
+ CROSS_HARNESS_APPROVAL: "cross_harness_approval_aggregated"
16566
+ };
16567
+ var OBSERVED_OP_LIST = [
16568
+ HANDOFF_LOG_OBSERVED_OPS.LOCAL_HANDOFF,
16569
+ HANDOFF_LOG_OBSERVED_OPS.CROSS_HARNESS_APPROVAL
16570
+ ];
16571
+ var OPERATOR_PSEUDO_AGENT = "operator";
16572
+ function makeEntryId(auditEventId) {
16573
+ return createHash("sha256").update(auditEventId).digest("hex").slice(0, 32);
16574
+ }
16575
+ var DEFAULT_LIMIT = 50;
16576
+ var MAX_LIMIT = 500;
16577
+ var AUDIT_QUERY_LIMIT = 1e4;
16578
+ var HandoffLog = class {
16579
+ auditLog;
16580
+ fortressId;
16581
+ constructor(opts) {
16582
+ this.auditLog = opts.auditLog;
16583
+ this.fortressId = opts.fortressId;
16584
+ }
16585
+ /** Stable fortress id this HandoffLog reads. */
16586
+ getFortressId() {
16587
+ return this.fortressId;
16588
+ }
16589
+ /**
16590
+ * Query handoffs in chronological-newest-first order. Filters
16591
+ * applied after normalization so the per-event-class shape
16592
+ * differences (sender field name, recipient inference) are handled
16593
+ * once.
16594
+ */
16595
+ async query(opts) {
16596
+ const limit = Math.min(opts.limit ?? DEFAULT_LIMIT, MAX_LIMIT);
16597
+ const queryResult = await this.auditLog.query({
16598
+ ...opts.since !== void 0 ? { since: opts.since } : {},
16599
+ layer: "l2",
16600
+ limit: AUDIT_QUERY_LIMIT
16601
+ });
16602
+ const normalized = [];
16603
+ for (const entry of queryResult.entries) {
16604
+ if (!OBSERVED_OP_LIST.includes(entry.operation)) continue;
16605
+ const handoff = this.normalize(entry);
16606
+ if (!handoff) continue;
16607
+ if (opts.until && handoff.observed_at > opts.until) continue;
16608
+ if (opts.since && handoff.observed_at < opts.since) continue;
16609
+ if (opts.agent_id) {
16610
+ if (handoff.source_agent_id !== opts.agent_id && handoff.target_agent_id !== opts.agent_id) {
16611
+ continue;
16612
+ }
16613
+ }
16614
+ normalized.push(handoff);
16615
+ }
16616
+ normalized.sort((a, b) => a.observed_at < b.observed_at ? 1 : -1);
16617
+ return normalized.slice(0, limit);
16618
+ }
16619
+ /**
16620
+ * Look up a single entry by id. Returns the normalized entry +
16621
+ * the source audit payload for operator-facing detail rendering.
16622
+ * Returns null when no audit entry maps to the given id.
16623
+ */
16624
+ async getEntry(entryId) {
16625
+ const queryResult = await this.auditLog.query({
16626
+ layer: "l2",
16627
+ limit: AUDIT_QUERY_LIMIT
16628
+ });
16629
+ for (const audit of queryResult.entries) {
16630
+ if (!OBSERVED_OP_LIST.includes(audit.operation)) continue;
16631
+ const handoff = this.normalize(audit);
16632
+ if (!handoff) continue;
16633
+ if (handoff.entry_id === entryId) {
16634
+ return { entry: handoff, source_audit_entry: audit };
16635
+ }
16636
+ }
16637
+ return null;
16638
+ }
16639
+ normalize(audit) {
16640
+ const details = audit.details;
16641
+ if (audit.operation === HANDOFF_LOG_OBSERVED_OPS.LOCAL_HANDOFF) {
16642
+ const sender = optString(details, "sender_agent_id");
16643
+ const recipient = optString(details, "recipient_agent_id");
16644
+ if (!sender || !recipient || sender === recipient) return null;
16645
+ const auditEventId = optString(details, "event_id") ?? auditEventIdFallback(audit);
16646
+ return {
16647
+ entry_id: makeEntryId(auditEventId),
16648
+ audit_event_id: auditEventId,
16649
+ source_agent_id: sender,
16650
+ target_agent_id: recipient,
16651
+ observed_at: audit.timestamp,
16652
+ event_class: audit.operation,
16653
+ context_transfer_summary: localHandoffSummary(details, sender, recipient),
16654
+ workflow_link: null
16655
+ };
16656
+ }
16657
+ if (audit.operation === HANDOFF_LOG_OBSERVED_OPS.CROSS_HARNESS_APPROVAL) {
16658
+ const sender = optString(details, "source_harness") ?? optString(details, "source_agent_id");
16659
+ if (!sender) return null;
16660
+ const auditEventId = optString(details, "aggregator_id") ?? auditEventIdFallback(audit);
16661
+ return {
16662
+ entry_id: makeEntryId(auditEventId),
16663
+ audit_event_id: auditEventId,
16664
+ source_agent_id: sender,
16665
+ target_agent_id: OPERATOR_PSEUDO_AGENT,
16666
+ observed_at: audit.timestamp,
16667
+ event_class: audit.operation,
16668
+ context_transfer_summary: crossHarnessSummary(details, sender),
16669
+ workflow_link: null
16670
+ };
16671
+ }
16672
+ return null;
16673
+ }
16674
+ };
16675
+ function optString(details, key) {
16676
+ if (!details) return null;
16677
+ const value = details[key];
16678
+ if (typeof value !== "string" || value.length === 0) return null;
16679
+ return value;
16680
+ }
16681
+ function auditEventIdFallback(audit) {
16682
+ return `${audit.timestamp}:${audit.operation}`;
16683
+ }
16684
+ function localHandoffSummary(details, sender, recipient) {
16685
+ const taskScope = optString(details, "task_scope");
16686
+ const reasonClass = optString(details, "reason_class");
16687
+ if (taskScope) {
16688
+ return `${sender} -> ${recipient} handoff: ${taskScope}`;
16689
+ }
16690
+ if (reasonClass) {
16691
+ return `${sender} -> ${recipient} handoff (${reasonClass})`;
16692
+ }
16693
+ return `${sender} -> ${recipient} handoff`;
16694
+ }
16695
+ function crossHarnessSummary(details, sender) {
16696
+ const policyRule = optString(details, "policy_rule_id");
16697
+ if (policyRule) {
16698
+ return `${sender} -> operator approval (${policyRule})`;
16699
+ }
16700
+ return `${sender} -> operator approval`;
16701
+ }
16702
+ var COORDINATION_VIEW_AUDIT_OPS = {
16703
+ VIEW_OPENED: "operator_coordination_view_opened",
16704
+ ENTRY_DRILLED: "operator_handoff_entry_drilled"
16705
+ };
16706
+
16707
+ // src/coordination/handoff-routes.ts
16708
+ var COORDINATION_API_PREFIX = "/api/coordination";
16709
+ var COORDINATION_HANDOFFS_PREFIX = "/api/coordination/handoffs";
16710
+ var COORDINATION_LIST_DEFAULT_LIMIT = 50;
16711
+ var COORDINATION_LIST_MAX_LIMIT = 500;
16712
+ var HandoffEventBridge = class {
16713
+ listeners = /* @__PURE__ */ new Set();
16714
+ subscribe(listener) {
16715
+ this.listeners.add(listener);
16716
+ return () => this.listeners.delete(listener);
16717
+ }
16718
+ emit(entry) {
16719
+ for (const listener of this.listeners) {
16720
+ try {
16721
+ listener(entry);
16722
+ } catch {
16723
+ }
16724
+ }
16725
+ }
16726
+ };
16727
+ function writeJSON6(res, status, payload) {
16728
+ res.writeHead(status, {
16729
+ "Content-Type": "application/json",
16730
+ "Cache-Control": "no-store"
16731
+ });
16732
+ res.end(JSON.stringify(payload));
16733
+ }
16734
+ function parseLimit4(raw, defaultValue, max) {
16735
+ if (raw === null || raw === "") return defaultValue;
16736
+ const parsed = Number.parseInt(raw, 10);
16737
+ if (Number.isNaN(parsed) || parsed < 0) return defaultValue;
16738
+ return Math.min(parsed, max);
16739
+ }
16740
+ function matchEntryRoute2(path) {
16741
+ const prefix = `${COORDINATION_HANDOFFS_PREFIX}/`;
16742
+ if (!path.startsWith(prefix)) return null;
16743
+ const rest = path.slice(prefix.length);
16744
+ if (rest.length === 0 || rest === "stream") return null;
16745
+ if (rest.includes("/")) return null;
16746
+ return { entryId: decodeURIComponent(rest) };
16747
+ }
16748
+ async function handleStream3(deps, res) {
16749
+ res.writeHead(200, {
16750
+ "Content-Type": "text/event-stream",
16751
+ "Cache-Control": "no-cache, no-transform",
16752
+ Connection: "keep-alive",
16753
+ "X-Accel-Buffering": "no"
16754
+ });
16755
+ const snapshot = await deps.handoffLog.query({ limit: 50 });
16756
+ res.write(
16757
+ `event: handoff_snapshot
16758
+ data: ${JSON.stringify({ entries: snapshot })}
16759
+
16760
+ `
16761
+ );
16762
+ const unsubscribe = deps.events.subscribe((entry) => {
16763
+ try {
16764
+ res.write(
16765
+ `event: handoff_added
16766
+ data: ${JSON.stringify(entry)}
16767
+
16768
+ `
16769
+ );
16770
+ } catch {
16771
+ }
16772
+ });
16773
+ const keepAlive = setInterval(() => {
16774
+ try {
16775
+ res.write(": keepalive\n\n");
16776
+ } catch {
16777
+ }
16778
+ }, 25e3);
16779
+ const cleanup = () => {
16780
+ clearInterval(keepAlive);
16781
+ unsubscribe();
16782
+ };
16783
+ res.on("close", cleanup);
16784
+ res.on("error", cleanup);
16785
+ }
16786
+ async function handleCoordinationRoute(deps, req, res) {
16787
+ const host = req.headers.host || "localhost";
16788
+ const url = new URL(req.url ?? "/", `http://${host}`);
16789
+ const method = (req.method ?? "GET").toUpperCase();
16790
+ const path = url.pathname;
16791
+ if (path !== COORDINATION_API_PREFIX && !path.startsWith(`${COORDINATION_API_PREFIX}/`)) {
16792
+ return false;
16793
+ }
16794
+ const checkAuth = authMiddleware(deps.authConfig);
16795
+ if (!checkAuth(req, res, url)) return true;
16796
+ try {
16797
+ if (method === "GET" && path === `${COORDINATION_HANDOFFS_PREFIX}/stream`) {
16798
+ await handleStream3(deps, res);
16799
+ return true;
16800
+ }
16801
+ if (method === "GET" && path === COORDINATION_HANDOFFS_PREFIX) {
16802
+ const limit = parseLimit4(
16803
+ url.searchParams.get("limit"),
16804
+ COORDINATION_LIST_DEFAULT_LIMIT,
16805
+ COORDINATION_LIST_MAX_LIMIT
16806
+ );
16807
+ const since = url.searchParams.get("since") ?? void 0;
16808
+ const until = url.searchParams.get("until") ?? void 0;
16809
+ const agentId = url.searchParams.get("agent_id") ?? void 0;
16810
+ const entries = await deps.handoffLog.query({
16811
+ limit,
16812
+ ...since !== void 0 ? { since } : {},
16813
+ ...until !== void 0 ? { until } : {},
16814
+ ...agentId !== void 0 ? { agent_id: agentId } : {}
16815
+ });
16816
+ deps.auditLog.append(
16817
+ "l2",
16818
+ COORDINATION_VIEW_AUDIT_OPS.VIEW_OPENED,
16819
+ deps.operatorId,
16820
+ {
16821
+ fortress_id: deps.handoffLog.getFortressId(),
16822
+ result_count: entries.length,
16823
+ ...since !== void 0 ? { since } : {},
16824
+ ...until !== void 0 ? { until } : {},
16825
+ ...agentId !== void 0 ? { agent_id: agentId } : {}
16826
+ }
16827
+ );
16828
+ writeJSON6(res, 200, { ok: true, data: { entries } });
16829
+ return true;
16830
+ }
16831
+ const entryMatch = matchEntryRoute2(path);
16832
+ if (method === "GET" && entryMatch) {
16833
+ const detail = await deps.handoffLog.getEntry(entryMatch.entryId);
16834
+ if (!detail) {
16835
+ writeJSON6(res, 404, { ok: false, error: "not_found" });
16836
+ return true;
16837
+ }
16838
+ deps.auditLog.append(
16839
+ "l2",
16840
+ COORDINATION_VIEW_AUDIT_OPS.ENTRY_DRILLED,
16841
+ deps.operatorId,
16842
+ {
16843
+ fortress_id: deps.handoffLog.getFortressId(),
16844
+ entry_id: detail.entry.entry_id,
16845
+ event_class: detail.entry.event_class,
16846
+ source_agent_id: detail.entry.source_agent_id,
16847
+ target_agent_id: detail.entry.target_agent_id
16848
+ }
16849
+ );
16850
+ writeJSON6(res, 200, { ok: true, data: detail });
16851
+ return true;
16852
+ }
16853
+ writeJSON6(res, 404, { ok: false, error: "not_found", path });
16854
+ return true;
16855
+ } catch (err) {
16856
+ const msg = err instanceof Error ? err.message : String(err);
16857
+ writeJSON6(res, 500, { ok: false, error: "internal", detail: msg });
16858
+ return true;
16859
+ }
16860
+ }
16561
16861
 
16562
16862
  // src/principal-policy/dashboard.ts
16563
16863
  var SESSION_TTL_REMOTE_MS = 5 * 60 * 1e3;
@@ -16638,6 +16938,17 @@ var DashboardApprovalChannel = class {
16638
16938
  * dispatcher's audited paths.
16639
16939
  */
16640
16940
  sentinelDispatcher = null;
16941
+ /**
16942
+ * v1.3 WP-V1.3-3 Omega-1 Coordination Handoff Visualization.
16943
+ * Mounted additively at `/api/coordination/*` when set. Read-only
16944
+ * against the audit log; the only writes are operator-action audit
16945
+ * events (operator_coordination_view_opened,
16946
+ * operator_handoff_entry_drilled).
16947
+ */
16948
+ handoffLog = null;
16949
+ handoffEventBridge = null;
16950
+ handoffAuditLog = null;
16951
+ handoffOperatorId = null;
16641
16952
  constructor(config) {
16642
16953
  this.config = config;
16643
16954
  this.authToken = config.auth_token;
@@ -16705,6 +17016,18 @@ var DashboardApprovalChannel = class {
16705
17016
  setSentinelDispatcher(dispatcher) {
16706
17017
  this.sentinelDispatcher = dispatcher;
16707
17018
  }
17019
+ /**
17020
+ * v1.3 WP-V1.3-3 Omega-1: bind the Coordination handoff log +
17021
+ * event bridge + audit log + operator id. Once set, requests to
17022
+ * `/api/coordination/*` route through `handleCoordinationRoute`.
17023
+ * Pass `null` for any field to detach.
17024
+ */
17025
+ setHandoffLog(opts) {
17026
+ this.handoffLog = opts.handoffLog;
17027
+ this.handoffEventBridge = opts.eventBridge ?? null;
17028
+ this.handoffAuditLog = opts.auditLog ?? null;
17029
+ this.handoffOperatorId = opts.operatorId ?? null;
17030
+ }
16708
17031
  /**
16709
17032
  * v1.3 WP-V1.3-10 dispatch entry point. Called from `handleRequest`
16710
17033
  * before the legacy approval route table. Returns true when served.
@@ -16743,6 +17066,30 @@ var DashboardApprovalChannel = class {
16743
17066
  res
16744
17067
  );
16745
17068
  }
17069
+ /**
17070
+ * v1.3 WP-V1.3-3 Omega-1 dispatch entry point. Routes
17071
+ * `/api/coordination/*` requests through the coordination router
17072
+ * when a HandoffLog has been bound. Returns true when served.
17073
+ */
17074
+ async dispatchCoordination(req, res) {
17075
+ if (!this.handoffLog || !this.handoffEventBridge || !this.handoffAuditLog) {
17076
+ return false;
17077
+ }
17078
+ return handleCoordinationRoute(
17079
+ {
17080
+ authConfig: {
17081
+ loopbackAutoAuth: this._autoAuthLocalhost,
17082
+ ...this.authToken !== void 0 ? { authToken: this.authToken } : {}
17083
+ },
17084
+ handoffLog: this.handoffLog,
17085
+ auditLog: this.handoffAuditLog,
17086
+ operatorId: this.handoffOperatorId ?? this.identityManager?.getPrimaryIdentityId() ?? "operator_dashboard",
17087
+ events: this.handoffEventBridge
17088
+ },
17089
+ req,
17090
+ res
17091
+ );
17092
+ }
16746
17093
  /**
16747
17094
  * v1.1 dispatch entry point. Called from `handleRequest` before the
16748
17095
  * legacy route table. Returns true when the request was served by v1.1
@@ -17142,6 +17489,18 @@ var DashboardApprovalChannel = class {
17142
17489
  });
17143
17490
  return;
17144
17491
  }
17492
+ if (this.handoffLog && url.pathname.startsWith(COORDINATION_API_PREFIX)) {
17493
+ this.dispatchCoordination(req, res).then((handled) => {
17494
+ if (handled) return;
17495
+ this.handleLegacyRequest(req, res, url, method);
17496
+ }).catch(() => {
17497
+ if (!res.headersSent) {
17498
+ res.writeHead(500, { "Content-Type": "application/json" });
17499
+ res.end(JSON.stringify({ error: "Internal server error" }));
17500
+ }
17501
+ });
17502
+ return;
17503
+ }
17145
17504
  if (this.v11Bindings) {
17146
17505
  this.dispatchV11(req, res, url, method).then((handled) => {
17147
17506
  if (handled) return;
@@ -21337,7 +21696,7 @@ var ALERT_SIGMA2 = 6;
21337
21696
  var BASELINE_WINDOWS2 = 7;
21338
21697
  var QUERY_LIMIT2 = 1e4;
21339
21698
  var MULTI_NEW_PARTNER_ALERT_THRESHOLD = 3;
21340
- var OPERATOR_PSEUDO_AGENT = "operator";
21699
+ var OPERATOR_PSEUDO_AGENT2 = "operator";
21341
21700
  var HANDOFF_OP = "v1.1_local_handoff";
21342
21701
  var CROSS_HARNESS_OPS = /* @__PURE__ */ new Set([
21343
21702
  "cross_harness_approval_aggregated",
@@ -21570,7 +21929,7 @@ function extractInterAgentEvents(entries) {
21570
21929
  if (!sender) continue;
21571
21930
  out.push({
21572
21931
  sender,
21573
- recipient: OPERATOR_PSEUDO_AGENT,
21932
+ recipient: OPERATOR_PSEUDO_AGENT2,
21574
21933
  timestampMs: Date.parse(entry.timestamp),
21575
21934
  auditId: `${entry.timestamp}:${entry.operation}`
21576
21935
  });
@@ -41081,6 +41440,19 @@ ${err.message}
41081
41440
  identityId: aggregatorIdentityId
41082
41441
  });
41083
41442
  anomalyDispatcher.start();
41443
+ const handoffLog = new HandoffLog({
41444
+ auditLog,
41445
+ fortressId: fortressIdForAggregator
41446
+ });
41447
+ const handoffEventBridge = new HandoffEventBridge();
41448
+ if (dashboard) {
41449
+ dashboard.setHandoffLog({
41450
+ handoffLog,
41451
+ eventBridge: handoffEventBridge,
41452
+ auditLog,
41453
+ operatorId: aggregatorIdentityId
41454
+ });
41455
+ }
41084
41456
  const policyTools = createPrincipalPolicyTools(policy, baseline, auditLog);
41085
41457
  const { tools: sanctuaryMetaTools } = createSanctuaryTools({
41086
41458
  config,