pico-engine 1.6.4 → 1.9.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 (108) hide show
  1. package/README.md +16 -9
  2. package/dist/RulesetRegistryLoaderFs.js +60 -6
  3. package/dist/auth/cookies.js +32 -0
  4. package/dist/auth/index.js +6 -1
  5. package/dist/channelIdentityPolicy.js +22 -0
  6. package/dist/cli.js +106 -69
  7. package/dist/engineConfig.js +291 -0
  8. package/dist/identity/IdentityService.js +90 -0
  9. package/dist/identity/crossPico.js +24 -8
  10. package/dist/identity/didoModule.js +80 -18
  11. package/dist/identity/establishSubscription.js +2 -2
  12. package/dist/identity/identityPortable.js +92 -0
  13. package/dist/identity/index.js +10 -1
  14. package/dist/identity/localPeerReconcile.js +65 -0
  15. package/dist/identity/peerDid4.js +175 -0
  16. package/dist/identity/peerDid4Create.js +90 -0
  17. package/dist/identity/portableExportPrepare.js +196 -0
  18. package/dist/identity/portableMove.js +87 -0
  19. package/dist/identity/skyIntro.js +6 -3
  20. package/dist/identity/skyProtocol.js +26 -1
  21. package/dist/identity/skyRotation.js +121 -0
  22. package/dist/identity/store.js +5 -1
  23. package/dist/identity/subscriptionEntSync.js +72 -0
  24. package/dist/identity/veramoDidComm.js +41 -2
  25. package/dist/identity/webvh.js +4 -2
  26. package/dist/identity/webvhRelocation.js +115 -0
  27. package/dist/index.js +84 -27
  28. package/dist/logging.js +156 -16
  29. package/dist/oauth/OAuthService.js +6 -2
  30. package/dist/policyConfigFromJson.js +112 -0
  31. package/dist/runMcp.js +27 -0
  32. package/dist/server.js +261 -24
  33. package/dist/stageKrl.js +26 -0
  34. package/dist/subtreeWrangler.js +180 -0
  35. package/dist/uiChannelPolicies.js +15 -0
  36. package/dist/xmluiRuntime.js +68 -0
  37. package/krl/io.picolabs.oauth.krl +41 -2
  38. package/krl/io.picolabs.pds.krl +131 -10
  39. package/krl/io.picolabs.pico-engine-ui.krl +217 -94
  40. package/krl/io.picolabs.portable_subtree.krl +97 -0
  41. package/krl/io.picolabs.subscription.krl +180 -72
  42. package/krl/io.picolabs.wrangler.krl +313 -57
  43. package/krl/io.picolabs.xmlui.krl +295 -0
  44. package/mcp/devResources.js +118 -0
  45. package/mcp/devTools.js +351 -0
  46. package/mcp/engineClient.js +133 -0
  47. package/mcp/meshHttp.js +137 -0
  48. package/mcp/meshTools.js +189 -0
  49. package/mcp/runStdio.js +34 -0
  50. package/mcp/schemaToZod.js +56 -0
  51. package/package.json +24 -9
  52. package/public/.DS_Store +0 -0
  53. package/public/docs/_sidebar.md +3 -1
  54. package/public/docs/llms-full.txt +630 -0
  55. package/public/docs/llms.txt +38 -0
  56. package/public/docs/openapi.yaml +78 -0
  57. package/public/docs/pico-engine.md +239 -4
  58. package/public/docs/pico-overview.md +117 -0
  59. package/public/docs/pico-views-xmlui.md +5 -0
  60. package/public/docs/pico-views.md +140 -0
  61. package/public/docs/pico.md +3 -1
  62. package/public/docs/rulesets/README.md +26 -0
  63. package/public/docs/rulesets/io.picolabs.oauth.md +71 -0
  64. package/public/docs/rulesets/io.picolabs.pds.md +327 -0
  65. package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +65 -0
  66. package/public/docs/rulesets/io.picolabs.portable_subtree.md +94 -0
  67. package/public/docs/rulesets/io.picolabs.subscription.md +398 -0
  68. package/public/docs/rulesets/io.picolabs.wrangler.md +471 -0
  69. package/public/pico-engine-ui.css +1 -1
  70. package/public/pico-engine-ui.js +27 -23
  71. package/public/xmlui/0.12.7.js +419 -0
  72. package/public/xmlui/pico-view-boot.js +153 -0
  73. package/src/RulesetRegistryLoaderFs.ts +66 -7
  74. package/src/auth/cookies.ts +34 -0
  75. package/src/auth/index.ts +7 -0
  76. package/src/channelIdentityPolicy.ts +29 -0
  77. package/src/cli.ts +70 -37
  78. package/src/engineConfig.ts +363 -0
  79. package/src/identity/IdentityService.ts +189 -1
  80. package/src/identity/crossPico.ts +57 -8
  81. package/src/identity/didoModule.ts +140 -23
  82. package/src/identity/establishSubscription.ts +2 -2
  83. package/src/identity/identityPortable.ts +153 -0
  84. package/src/identity/index.ts +14 -0
  85. package/src/identity/localPeerReconcile.ts +97 -0
  86. package/src/identity/peerDid4.ts +197 -0
  87. package/src/identity/peerDid4Create.ts +121 -0
  88. package/src/identity/portableExportPrepare.ts +302 -0
  89. package/src/identity/portableMove.ts +193 -0
  90. package/src/identity/skyIntro.ts +11 -11
  91. package/src/identity/skyProtocol.ts +46 -0
  92. package/src/identity/skyRotation.ts +211 -0
  93. package/src/identity/store.ts +5 -1
  94. package/src/identity/subscriptionEntSync.ts +97 -0
  95. package/src/identity/veramoDidComm.ts +52 -2
  96. package/src/identity/webvh.ts +4 -4
  97. package/src/identity/webvhRelocation.ts +167 -0
  98. package/src/index.ts +123 -31
  99. package/src/logging.ts +173 -18
  100. package/src/oauth/OAuthService.ts +11 -3
  101. package/src/policyConfigFromJson.ts +131 -0
  102. package/src/runMcp.ts +30 -0
  103. package/src/server.ts +328 -27
  104. package/src/stageKrl.ts +26 -0
  105. package/src/subtreeWrangler.ts +278 -0
  106. package/src/uiChannelPolicies.ts +15 -0
  107. package/src/xmluiRuntime.ts +66 -0
  108. package/krl/io.picolabs.did-o.krl +0 -349
@@ -0,0 +1,131 @@
1
+ import type {
2
+ EventPolicy,
3
+ EventPolicyRule,
4
+ PolicyCeiling,
5
+ QueryPolicy,
6
+ QueryPolicyRule,
7
+ } from "pico-framework";
8
+
9
+ function isRecord(v: unknown): v is Record<string, unknown> {
10
+ return v != null && typeof v === "object" && !Array.isArray(v);
11
+ }
12
+
13
+ function cleanEventPolicyFromJson(orig: unknown): EventPolicy {
14
+ if (
15
+ !isRecord(orig) ||
16
+ !Array.isArray(orig.allow) ||
17
+ !Array.isArray(orig.deny)
18
+ ) {
19
+ throw new TypeError(
20
+ "eventPolicy expects { allow: EventPolicyRule[], deny: EventPolicyRule[] }",
21
+ );
22
+ }
23
+ const allow: EventPolicyRule[] = [];
24
+ for (const item of orig.allow) {
25
+ if (!isRecord(item) || typeof item.domain !== "string" || typeof item.name !== "string") {
26
+ throw new TypeError("eventPolicy allow entries need domain and name strings");
27
+ }
28
+ allow.push({ domain: item.domain.trim(), name: item.name.trim() });
29
+ }
30
+ const deny: EventPolicyRule[] = [];
31
+ for (const item of orig.deny) {
32
+ if (!isRecord(item) || typeof item.domain !== "string" || typeof item.name !== "string") {
33
+ throw new TypeError("eventPolicy deny entries need domain and name strings");
34
+ }
35
+ deny.push({ domain: item.domain.trim(), name: item.name.trim() });
36
+ }
37
+ return { allow, deny };
38
+ }
39
+
40
+ function cleanQueryPolicyFromJson(orig: unknown): QueryPolicy {
41
+ if (
42
+ !isRecord(orig) ||
43
+ !Array.isArray(orig.allow) ||
44
+ !Array.isArray(orig.deny)
45
+ ) {
46
+ throw new TypeError(
47
+ "queryPolicy expects { allow: QueryPolicyRule[], deny: QueryPolicyRule[] }",
48
+ );
49
+ }
50
+ const allow: QueryPolicyRule[] = [];
51
+ for (const item of orig.allow) {
52
+ if (!isRecord(item) || typeof item.rid !== "string" || typeof item.name !== "string") {
53
+ throw new TypeError("queryPolicy allow entries need rid and name strings");
54
+ }
55
+ allow.push({ rid: item.rid.trim(), name: item.name.trim() });
56
+ }
57
+ const deny: QueryPolicyRule[] = [];
58
+ for (const item of orig.deny) {
59
+ if (!isRecord(item) || typeof item.rid !== "string" || typeof item.name !== "string") {
60
+ throw new TypeError("queryPolicy deny entries need rid and name strings");
61
+ }
62
+ deny.push({ rid: item.rid.trim(), name: item.name.trim() });
63
+ }
64
+ return { allow, deny };
65
+ }
66
+
67
+ function parseEventRule(v: unknown): EventPolicyRule | null {
68
+ if (!isRecord(v)) return null;
69
+ if (typeof v.domain !== "string" || typeof v.name !== "string") return null;
70
+ return { domain: v.domain, name: v.name };
71
+ }
72
+
73
+ function parseQueryRule(v: unknown): QueryPolicyRule | null {
74
+ if (!isRecord(v)) return null;
75
+ if (typeof v.rid !== "string" || typeof v.name !== "string") return null;
76
+ return { rid: v.rid, name: v.name };
77
+ }
78
+
79
+ export function parsePolicyCeilingFromJson(
80
+ val: unknown,
81
+ fieldName: string,
82
+ ): PolicyCeiling | undefined {
83
+ if (val === undefined || val === null) {
84
+ return undefined;
85
+ }
86
+ if (!isRecord(val) || !val.eventPolicy || !val.queryPolicy) {
87
+ throw new Error(
88
+ `${fieldName} must be { eventPolicy: { allow, deny }, queryPolicy: { allow, deny } }`,
89
+ );
90
+ }
91
+ return {
92
+ eventPolicy: cleanEventPolicyFromJson(val.eventPolicy),
93
+ queryPolicy: cleanQueryPolicyFromJson(val.queryPolicy),
94
+ };
95
+ }
96
+
97
+ export function parseGlobalEventDenyFromJson(val: unknown): EventPolicyRule[] | undefined {
98
+ if (val === undefined || val === null) {
99
+ return undefined;
100
+ }
101
+ if (!Array.isArray(val)) {
102
+ throw new Error("globalEventDeny must be an array of { domain, name } rules");
103
+ }
104
+ const rules: EventPolicyRule[] = [];
105
+ for (const item of val) {
106
+ const rule = parseEventRule(item);
107
+ if (!rule) {
108
+ throw new Error("globalEventDeny: each entry needs domain and name strings");
109
+ }
110
+ rules.push(rule);
111
+ }
112
+ return rules;
113
+ }
114
+
115
+ export function parseGlobalQueryDenyFromJson(val: unknown): QueryPolicyRule[] | undefined {
116
+ if (val === undefined || val === null) {
117
+ return undefined;
118
+ }
119
+ if (!Array.isArray(val)) {
120
+ throw new Error("globalQueryDeny must be an array of { rid, name } rules");
121
+ }
122
+ const rules: QueryPolicyRule[] = [];
123
+ for (const item of val) {
124
+ const rule = parseQueryRule(item);
125
+ if (!rule) {
126
+ throw new Error("globalQueryDeny: each entry needs rid and name strings");
127
+ }
128
+ rules.push(rule);
129
+ }
130
+ return rules;
131
+ }
package/src/runMcp.ts ADDED
@@ -0,0 +1,30 @@
1
+ import {
2
+ installEngineRuntimeSettings,
3
+ resolveEngineSettings,
4
+ } from "./engineConfig";
5
+
6
+ /** Dev MCP stdio entry (pico-engine mcp --stdio). */
7
+ export async function runMcpCli(argv: string[]): Promise<void> {
8
+ const settings = resolveEngineSettings({});
9
+ installEngineRuntimeSettings(settings);
10
+ if (!process.env.PICO_ENGINE_BASE_URL) {
11
+ process.env.PICO_ENGINE_BASE_URL = settings.baseUrl;
12
+ }
13
+ if (!process.env.PICO_ENGINE_HOME) {
14
+ process.env.PICO_ENGINE_HOME = settings.home;
15
+ }
16
+ const args = argv.slice(1);
17
+ const stdio = args.includes("--stdio") || args.length === 0;
18
+ if (!stdio) {
19
+ console.error("Usage: pico-engine mcp --stdio");
20
+ console.error("");
21
+ console.error("Connects to a running local engine (PICO_ENGINE_BASE_URL).");
22
+ console.error("Requires localhost /c access or PICO_ENGINE_UI_ECI.");
23
+ process.exit(1);
24
+ }
25
+ // JS (not tsc): avoids pulling MCP SDK types into the main compile.
26
+ const { runDevMcpStdio } = require("../mcp/runStdio.js") as {
27
+ runDevMcpStdio: () => Promise<void>;
28
+ };
29
+ await runDevMcpStdio();
30
+ }
package/src/server.ts CHANGED
@@ -10,9 +10,14 @@ import {
10
10
  listEngineKrlSources,
11
11
  resolveEngineKrlFile,
12
12
  } from "./bundledKrl";
13
- import { PicoEngineCore } from "pico-engine-core";
13
+ import {
14
+ assembleLogTransactions,
15
+ isRoutineQueryError,
16
+ PicoEngineCore,
17
+ RawLogEntry,
18
+ } from "pico-engine-core";
14
19
  import { PicoFramework } from "pico-framework";
15
- import { AuthError, AuthService } from "./auth";
20
+ import { AuthCookieNames, AuthError, AuthService, authCookieNames } from "./auth";
16
21
  import {
17
22
  OAuthError,
18
23
  OAuthService,
@@ -22,17 +27,32 @@ import {
22
27
  rootHasOAuthMeshRuleset,
23
28
  } from "./oauth";
24
29
  import { uiECIForRoot } from "./provisionRoot";
30
+ import { channelSchemaForEci } from "pico-engine-core";
31
+ import {
32
+ contentTypeForUiPath,
33
+ isXmluiCodeBehindPath,
34
+ isXmluiSpaClientRoute,
35
+ parseComponentXmluiPath,
36
+ viewQueryNameForComponent,
37
+ XMLUI_EMPTY_CODE_BEHIND,
38
+ } from "./xmluiRuntime";
25
39
  import { IdentityService } from "./identity/IdentityService";
26
40
  import { registerWebvhRoutes } from "./identity/webvhRoutes";
27
41
  import { registerDidcommIngressRoutes } from "./identity/didcommIngressRoutes";
28
42
 
29
- const engineVersion = require("../package.json").version;
43
+ import { getEngineRuntimeSettings } from "./engineConfig";
44
+ import {
45
+ picoLogEntries,
46
+ resolveEngineLogFilePath,
47
+ resolvePicoLogsReadPath,
48
+ subscribePicoLogRing,
49
+ } from "./logging";
50
+ import { stageKrlContent } from "./stageKrl";
30
51
 
31
- const SESSION_COOKIE = "pico-session";
32
- const CEREMONY_COOKIE = "pico-ceremony";
52
+ const engineVersion = require("../package.json").version;
33
53
 
34
54
  function localhostChannelBypassEnabled(): boolean {
35
- return process.env.PICO_ENGINE_ALLOW_LOCALHOST_C !== "0";
55
+ return getEngineRuntimeSettings().allowLocalhostChannelBypass;
36
56
  }
37
57
 
38
58
  function mergeGetPost(req: Request) {
@@ -88,6 +108,11 @@ export function server(
88
108
  ): Express {
89
109
  const pf = core.picoFramework;
90
110
  const app = express();
111
+ function cookies(): AuthCookieNames {
112
+ return auth
113
+ ? authCookieNames(auth.rp().origin)
114
+ : authCookieNames("http://localhost:3000");
115
+ }
91
116
 
92
117
  app.use(function(req, res, next) {
93
118
  // /auth routes need a specific credentialed CORS origin (below), not "*".
@@ -127,6 +152,10 @@ export function server(
127
152
  if (req.path === "/oauth/authorize" || req.path === "/oauth/approve") {
128
153
  return helmet({ contentSecurityPolicy: false })(req, res, next);
129
154
  }
155
+ // XMLUI view shells use small inline scripts (__PUBLIC_PATH, startApp).
156
+ if (req.path === "/ui" || req.path.startsWith("/ui/")) {
157
+ return helmet({ contentSecurityPolicy: false })(req, res, next);
158
+ }
130
159
  return defaultHelmet(req, res, next);
131
160
  });
132
161
  app.use(express.static(path.resolve(__dirname, "..", "public")));
@@ -158,7 +187,7 @@ export function server(
158
187
  }
159
188
 
160
189
  if (auth) {
161
- registerAuthRoutes(app, auth);
190
+ registerAuthRoutes(app, auth, cookies);
162
191
  }
163
192
 
164
193
  function isEciInRootTree(rootPicoId: string, eci: string): boolean {
@@ -188,7 +217,7 @@ export function server(
188
217
  next();
189
218
  return;
190
219
  }
191
- const token = parseCookies(req)[SESSION_COOKIE] || "";
220
+ const token = parseCookies(req)[cookies().session] || "";
192
221
  auth
193
222
  .getSession(token)
194
223
  .then((session) => {
@@ -264,9 +293,35 @@ export function server(
264
293
  }
265
294
 
266
295
  if (oauth) {
267
- registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
296
+ registerOAuthRoutes(
297
+ app,
298
+ oauth,
299
+ auth,
300
+ requireAuthSession,
301
+ parseCookies,
302
+ cookies
303
+ );
268
304
  }
269
305
 
306
+ app.get("/api/ui-sky-context", function (req, res, next) {
307
+ const eci =
308
+ typeof req.query.eci === "string" ? req.query.eci.trim() : "";
309
+ if (!eci) {
310
+ res.status(400).json({ error: "eci query parameter required" });
311
+ return;
312
+ }
313
+ if (!oauth) {
314
+ res.json({ bearerRequired: false });
315
+ return;
316
+ }
317
+ oauth
318
+ .skyRequiresBearer(eci)
319
+ .then((bearerRequired) => {
320
+ res.json({ bearerRequired });
321
+ })
322
+ .catch(next);
323
+ });
324
+
270
325
  app.get("/api/oauth/mesh-enabled", requireAuthSession, function (_req, res) {
271
326
  const roots = pf.rootPicos();
272
327
  const meshOAuthEnabled =
@@ -328,6 +383,23 @@ export function server(
328
383
  }
329
384
  });
330
385
 
386
+ app.post("/api/stage-krl", function (req, res, next) {
387
+ try {
388
+ const content = req.body?.content;
389
+ const filename =
390
+ typeof req.body?.filename === "string" ? req.body.filename : "staged.krl";
391
+ if (typeof content !== "string" || content.length > 2_000_000) {
392
+ res.status(400).json({ error: "content required (max 2MB)" });
393
+ return;
394
+ }
395
+ const url = stageKrlContent(content, filename);
396
+ res.json({ url });
397
+ } catch (err) {
398
+ const message = err instanceof Error ? err.message : String(err);
399
+ res.status(400).json({ error: message });
400
+ }
401
+ });
402
+
331
403
  app.get(/^\/krl\/(.+\.krl)$/, function (req, res, next) {
332
404
  const suffix = req.params[0];
333
405
  if (typeof suffix !== "string" || !suffix.endsWith(".krl")) {
@@ -360,7 +432,7 @@ export function server(
360
432
  res.json(base);
361
433
  return;
362
434
  }
363
- const token = parseCookies(req)[SESSION_COOKIE] || "";
435
+ const token = parseCookies(req)[cookies().session] || "";
364
436
  auth
365
437
  .needsAuthMigration()
366
438
  .then((needsAuthMigration) => {
@@ -377,6 +449,90 @@ export function server(
377
449
  .catch(next);
378
450
  });
379
451
 
452
+ /**
453
+ * Pushes each finished transaction for this pico. Owner session only,
454
+ * same gate as the rest of the developer UI.
455
+ */
456
+ app.get("/api/picos/:eci/logs/live", requireAuthSession, function (req, res) {
457
+ let picoId: string;
458
+ try {
459
+ picoId = pf.getPico(req.params.eci).id;
460
+ } catch (_e) {
461
+ res.status(404).json({ error: "Unknown pico" });
462
+ return;
463
+ }
464
+ res.writeHead(200, {
465
+ "Content-Type": "text/event-stream",
466
+ "Cache-Control": "no-cache, no-transform",
467
+ Connection: "keep-alive",
468
+ "X-Accel-Buffering": "no",
469
+ });
470
+ res.write(": ok\n\n");
471
+ let closed = false;
472
+ const unsubscribe = subscribePicoLogRing((id, txnId) => {
473
+ if (closed || id !== picoId) return;
474
+ const entries = picoLogEntries(picoId).filter(
475
+ (entry) => entry.txnId === txnId
476
+ );
477
+ const [txn] = assembleLogTransactions(entries as RawLogEntry[]);
478
+ if (!txn) return;
479
+ try {
480
+ res.write(`data: ${JSON.stringify(txn)}\n\n`);
481
+ } catch (_err) {
482
+ closed = true;
483
+ unsubscribe();
484
+ }
485
+ });
486
+ const beat = setInterval(() => {
487
+ if (closed) return;
488
+ res.write(": ping\n\n");
489
+ }, 25000);
490
+ req.on("close", () => {
491
+ closed = true;
492
+ clearInterval(beat);
493
+ unsubscribe();
494
+ });
495
+ });
496
+
497
+ /** Localhost-only context for dev MCP (stdio) and other local tooling. */
498
+ app.get("/api/dev/local", function (req, res) {
499
+ if (!localhostChannelBypassEnabled() || !isLocalhostRequest(req)) {
500
+ res.status(403).json({ error: "localhost only" });
501
+ return;
502
+ }
503
+ const roots = pf.rootPicos();
504
+ const rootPico = roots[0];
505
+ const rootUiEci = rootPico ? uiECIForRoot(pf, rootPico.id) : null;
506
+ let baseUrl = core.base_url;
507
+ if (!baseUrl) {
508
+ baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
509
+ }
510
+ const settings = getEngineRuntimeSettings();
511
+ const primaryLogFile = resolveEngineLogFilePath(settings.home);
512
+ const picoLogsReadPath = resolvePicoLogsReadPath(settings.home);
513
+ const mirrorDisabled = settings.logToStdout && settings.logMirror === false;
514
+ res.json({
515
+ version: engineVersion,
516
+ hasRoots: roots.length > 0,
517
+ rootUiEci,
518
+ baseUrl: baseUrl.replace(/\/$/, ""),
519
+ logging: {
520
+ home: settings.home,
521
+ logToStdout: settings.logToStdout,
522
+ logMirror: settings.logMirror,
523
+ logFileSetting: settings.logFile ?? null,
524
+ primaryLogFile,
525
+ /** File `getPicoLogs` reads (mirrors under stdout-only unless PICO_ENGINE_LOG_MIRROR=0). */
526
+ picoLogsReadPath,
527
+ picoLogsForDeveloperUi: true,
528
+ logMirrorDisabled: mirrorDisabled,
529
+ logRing: true,
530
+ hint:
531
+ "Logging tab reads the in-memory ring (last N transactions per pico, PICO_ENGINE_LOG_RING, default 200). A restart clears it. The log file remains the operator record.",
532
+ },
533
+ });
534
+ });
535
+
380
536
  app.all("/api/flush", requireAuthSession, function (req, res, next) {
381
537
  const attrs = mergeGetPost(req);
382
538
  const url = attrs["url"];
@@ -539,10 +695,144 @@ export function server(
539
695
  .catch(next);
540
696
  });
541
697
 
698
+ const XMLUI_FILE_RID = "io.picolabs.xmlui";
699
+
700
+ function respondXmluiQueryFailure(
701
+ err: unknown,
702
+ res: Response,
703
+ next: express.NextFunction
704
+ ): boolean {
705
+ if (!isRoutineQueryError(err)) {
706
+ return false;
707
+ }
708
+ const message = err + "";
709
+ if (!res.headersSent) {
710
+ if (/channel policy/i.test(message)) {
711
+ res.status(403).type("text/plain").end(message);
712
+ } else {
713
+ res.status(404).type("text/plain").end("Not found");
714
+ }
715
+ }
716
+ return true;
717
+ }
718
+
719
+ async function serveXmluiFile(
720
+ req: Request,
721
+ res: Response,
722
+ next: express.NextFunction,
723
+ eci: string,
724
+ appRid: string,
725
+ filePath: string
726
+ ) {
727
+ const rel = (filePath || "index.html").replace(/^\/+/, "") || "index.html";
728
+ try {
729
+ if (isXmluiCodeBehindPath(rel)) {
730
+ res
731
+ .status(200)
732
+ .setHeader("Content-Type", contentTypeForUiPath(rel))
733
+ .end(XMLUI_EMPTY_CODE_BEHIND);
734
+ return;
735
+ }
736
+
737
+ let data: unknown;
738
+ const componentName = appRid ? parseComponentXmluiPath(rel) : null;
739
+ if (componentName) {
740
+ const { pico } = pf.lookupChannel(eci);
741
+ const channelSchema = channelSchemaForEci(
742
+ pico,
743
+ core.rsRegistry,
744
+ (channelEci) => pf.lookupChannel(channelEci),
745
+ eci
746
+ );
747
+ const viewFn = viewQueryNameForComponent(
748
+ channelSchema.queries,
749
+ appRid,
750
+ componentName
751
+ );
752
+ if (!viewFn) {
753
+ res.status(404).type("text/plain").end("Not found");
754
+ return;
755
+ }
756
+ data = await core.query({
757
+ eci,
758
+ rid: appRid,
759
+ name: viewFn,
760
+ args: {},
761
+ });
762
+ } else {
763
+ data = await core.query({
764
+ eci,
765
+ rid: XMLUI_FILE_RID,
766
+ name: "file",
767
+ args: { rid: appRid, path: rel },
768
+ });
769
+ }
770
+ if (data == null && appRid && isXmluiSpaClientRoute(rel)) {
771
+ data = await core.query({
772
+ eci,
773
+ rid: XMLUI_FILE_RID,
774
+ name: "file",
775
+ args: { rid: appRid, path: "index.html" },
776
+ });
777
+ }
778
+ if (data == null) {
779
+ res.status(404).type("text/plain").end("Not found");
780
+ return;
781
+ }
782
+ if (
783
+ typeof data === "object" &&
784
+ data !== null &&
785
+ "error" in (data as Record<string, unknown>)
786
+ ) {
787
+ const message = String((data as Record<string, unknown>).error);
788
+ res.status(404).type("text/plain").end(message);
789
+ return;
790
+ }
791
+ const servedPath =
792
+ rel === "index.html" || !isXmluiSpaClientRoute(rel) ? rel : "index.html";
793
+ const contentType = contentTypeForUiPath(servedPath);
794
+ res.setHeader("Content-Type", contentType);
795
+ if (contentType.startsWith("application/json") && typeof data === "object") {
796
+ res.json(data);
797
+ return;
798
+ }
799
+ res.end(typeof data === "string" ? data : JSON.stringify(data));
800
+ } catch (err) {
801
+ if (respondXmluiQueryFailure(err, res, next)) {
802
+ return;
803
+ }
804
+ next(err);
805
+ }
806
+ }
807
+
808
+ app.get("/ui/:eci", function (req, res, next) {
809
+ void serveXmluiFile(req, res, next, req.params.eci, "", "index.html");
810
+ });
811
+
812
+ app.get("/ui/:eci/:rid", function (req, res, next) {
813
+ void serveXmluiFile(req, res, next, req.params.eci, req.params.rid, "index.html");
814
+ });
815
+
816
+ app.get("/ui/:eci/:rid/*", function (req, res, next) {
817
+ const rest = (req.params as { 0?: string })[0] || "index.html";
818
+ void serveXmluiFile(req, res, next, req.params.eci, req.params.rid, rest);
819
+ });
820
+
542
821
  app.all("/sky/query/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
543
822
  // Legacy alias; prefer /sky/query/ per Sky Query API docs.
544
823
  app.all("/sky/cloud/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
545
824
 
825
+ // Mesh MCP (Streamable HTTP, OAuth bearer scoped to :eci)
826
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
827
+ const { registerMeshMcpRoutes } = require("../mcp/meshHttp.js") as {
828
+ registerMeshMcpRoutes: (
829
+ app: Express,
830
+ core: PicoEngineCore,
831
+ oauth?: OAuthService
832
+ ) => void;
833
+ };
834
+ registerMeshMcpRoutes(app, core, oauth);
835
+
546
836
  app.use(function (
547
837
  err: any,
548
838
  req: express.Request,
@@ -558,7 +848,13 @@ export function server(
558
848
  return;
559
849
  }
560
850
  let message = err + "";
561
- console.error(err);
851
+ const policyNoise =
852
+ process.env.NODE_ENV === "test" &&
853
+ (isRoutineQueryError(err) ||
854
+ /not allowed by channel policy/i.test(message));
855
+ if (!policyNoise) {
856
+ console.error(err);
857
+ }
562
858
  if (err?.krl_compiler?.loc?.start) {
563
859
  message +=
564
860
  " [at line " +
@@ -585,9 +881,13 @@ export function server(
585
881
  return app;
586
882
  }
587
883
 
588
- function registerAuthRoutes(app: Express, auth: AuthService) {
884
+ function registerAuthRoutes(
885
+ app: Express,
886
+ auth: AuthService,
887
+ cookies: () => AuthCookieNames
888
+ ) {
589
889
  function setSessionCookie(res: Response, token: string, expiry: number) {
590
- res.cookie(SESSION_COOKIE, token, {
890
+ res.cookie(cookies().session, token, {
591
891
  httpOnly: true,
592
892
  sameSite: "lax",
593
893
  secure: auth.isSecure(),
@@ -597,7 +897,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
597
897
  }
598
898
 
599
899
  function setCeremonyCookie(res: Response, ceremonyId: string) {
600
- res.cookie(CEREMONY_COOKIE, ceremonyId, {
900
+ res.cookie(cookies().ceremony, ceremonyId, {
601
901
  httpOnly: true,
602
902
  sameSite: "lax",
603
903
  secure: auth.isSecure(),
@@ -607,7 +907,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
607
907
  }
608
908
 
609
909
  function clearCeremonyCookie(res: Response) {
610
- res.clearCookie(CEREMONY_COOKIE, { path: "/" });
910
+ res.clearCookie(cookies().ceremony, { path: "/" });
611
911
  }
612
912
 
613
913
  // WebAuthn requires a specific, credentialed origin — never "*".
@@ -631,7 +931,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
631
931
  });
632
932
 
633
933
  function requireSession(req: Request, res: Response, next: express.NextFunction) {
634
- const token = parseCookies(req)[SESSION_COOKIE] || "";
934
+ const token = parseCookies(req)[cookies().session] || "";
635
935
  auth
636
936
  .getSession(token)
637
937
  .then((session) => {
@@ -659,7 +959,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
659
959
  });
660
960
 
661
961
  app.post("/auth/register/verify", function (req, res, next) {
662
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
962
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
663
963
  auth
664
964
  .registerNewAccountVerify({ ceremonyId, response: req.body })
665
965
  .then(({ account, session, uiECI }) => {
@@ -686,7 +986,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
686
986
  });
687
987
 
688
988
  app.post("/auth/claim/verify", function (req, res, next) {
689
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
989
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
690
990
  auth
691
991
  .claimPrimaryRootVerify({ ceremonyId, response: req.body })
692
992
  .then(({ account, session, uiECI }) => {
@@ -713,7 +1013,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
713
1013
  });
714
1014
 
715
1015
  app.post("/auth/login/verify", function (req, res, next) {
716
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
1016
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
717
1017
  auth
718
1018
  .loginVerify({ ceremonyId, response: req.body })
719
1019
  .then(({ account, session }) => {
@@ -729,18 +1029,18 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
729
1029
  });
730
1030
 
731
1031
  app.post("/auth/logout", function (req, res, next) {
732
- const token = parseCookies(req)[SESSION_COOKIE] || "";
1032
+ const token = parseCookies(req)[cookies().session] || "";
733
1033
  auth
734
1034
  .deleteSession(token)
735
1035
  .then(() => {
736
- res.clearCookie(SESSION_COOKIE, { path: "/" });
1036
+ res.clearCookie(cookies().session, { path: "/" });
737
1037
  res.json({ ok: true });
738
1038
  })
739
1039
  .catch(next);
740
1040
  });
741
1041
 
742
1042
  app.get("/auth/session", function (req, res, next) {
743
- const token = parseCookies(req)[SESSION_COOKIE] || "";
1043
+ const token = parseCookies(req)[cookies().session] || "";
744
1044
  auth
745
1045
  .whoami(token)
746
1046
  .then((who) => res.json(who))
@@ -760,7 +1060,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
760
1060
 
761
1061
  app.post("/auth/credentials/verify", requireSession, function (req, res, next) {
762
1062
  const session = (req as any).picoSession;
763
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
1063
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
764
1064
  auth
765
1065
  .addCredentialVerify({
766
1066
  ceremonyId,
@@ -812,7 +1112,8 @@ function registerOAuthRoutes(
812
1112
  res: Response,
813
1113
  next: express.NextFunction
814
1114
  ) => void,
815
- parseCookiesFn: (req: Request) => { [name: string]: string }
1115
+ parseCookiesFn: (req: Request) => { [name: string]: string },
1116
+ cookies: () => AuthCookieNames
816
1117
  ) {
817
1118
  function requireOAuthSession(
818
1119
  req: Request,
@@ -823,7 +1124,7 @@ function registerOAuthRoutes(
823
1124
  res.status(503).json({ error: "Authentication is not configured" });
824
1125
  return;
825
1126
  }
826
- const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
1127
+ const token = parseCookiesFn(req)[cookies().session] || "";
827
1128
  auth
828
1129
  .getSession(token)
829
1130
  .then((session) => {
@@ -869,7 +1170,7 @@ function registerOAuthRoutes(
869
1170
  res.status(503).send("Authentication is not configured");
870
1171
  return;
871
1172
  }
872
- const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
1173
+ const token = parseCookiesFn(req)[cookies().session] || "";
873
1174
  const session = await auth.getSession(token);
874
1175
  if (!session) {
875
1176
  const returnPath = req.originalUrl || "/oauth/authorize";
@@ -892,7 +1193,7 @@ function registerOAuthRoutes(
892
1193
  res.status(503).send("Authentication is not configured");
893
1194
  return;
894
1195
  }
895
- const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
1196
+ const token = parseCookiesFn(req)[cookies().session] || "";
896
1197
  auth
897
1198
  .getSession(token)
898
1199
  .then((session) => {