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