pico-engine 1.6.2 → 1.8.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 (107) hide show
  1. package/README.md +117 -14
  2. package/dist/RulesetRegistryLoaderFs.js +24 -8
  3. package/dist/auth/cookies.js +32 -0
  4. package/dist/auth/index.js +6 -1
  5. package/dist/bundledKrl.js +129 -11
  6. package/dist/channelIdentityPolicy.js +22 -0
  7. package/dist/cli.js +105 -46
  8. package/dist/engineConfig.js +280 -0
  9. package/dist/identity/IdentityService.js +90 -0
  10. package/dist/identity/crossPico.js +106 -30
  11. package/dist/identity/didoModule.js +80 -18
  12. package/dist/identity/establishSubscription.js +2 -2
  13. package/dist/identity/identityPortable.js +92 -0
  14. package/dist/identity/index.js +10 -1
  15. package/dist/identity/localPeerReconcile.js +65 -0
  16. package/dist/identity/peerDid4.js +175 -0
  17. package/dist/identity/peerDid4Create.js +90 -0
  18. package/dist/identity/portableExportPrepare.js +196 -0
  19. package/dist/identity/portableMove.js +87 -0
  20. package/dist/identity/skyIntro.js +10 -4
  21. package/dist/identity/skyProtocol.js +26 -1
  22. package/dist/identity/skyRotation.js +121 -0
  23. package/dist/identity/store.js +5 -1
  24. package/dist/identity/subscriptionEntSync.js +72 -0
  25. package/dist/identity/veramoDidComm.js +41 -2
  26. package/dist/identity/webvh.js +4 -2
  27. package/dist/identity/webvhRelocation.js +115 -0
  28. package/dist/index.js +127 -34
  29. package/dist/levelDb.js +102 -0
  30. package/dist/logging.js +47 -8
  31. package/dist/oauth/OAuthService.js +12 -5
  32. package/dist/oauth/channelEligibility.js +7 -1
  33. package/dist/oauth/index.js +3 -1
  34. package/dist/policyConfigFromJson.js +112 -0
  35. package/dist/runMcp.js +27 -0
  36. package/dist/server.js +95 -24
  37. package/dist/subtreeWrangler.js +180 -0
  38. package/dist/uiChannelPolicies.js +16 -0
  39. package/krl/io.picolabs.oauth.krl +51 -5
  40. package/krl/io.picolabs.pds.krl +131 -10
  41. package/krl/io.picolabs.pico-engine-ui.krl +219 -22
  42. package/krl/io.picolabs.portable_subtree.krl +97 -0
  43. package/krl/io.picolabs.subscription.krl +226 -72
  44. package/krl/io.picolabs.wrangler.krl +300 -58
  45. package/mcp/devResources.js +118 -0
  46. package/mcp/devTools.js +351 -0
  47. package/mcp/engineClient.js +133 -0
  48. package/mcp/meshHttp.js +137 -0
  49. package/mcp/meshTools.js +189 -0
  50. package/mcp/runStdio.js +34 -0
  51. package/mcp/schemaToZod.js +56 -0
  52. package/package.json +22 -8
  53. package/public/docs/_sidebar.md +2 -1
  54. package/public/docs/llms-full.txt +479 -0
  55. package/public/docs/llms.txt +37 -0
  56. package/public/docs/openapi.yaml +78 -0
  57. package/public/docs/pico-engine.md +235 -4
  58. package/public/docs/pico-overview.md +117 -0
  59. package/public/docs/pico.md +3 -1
  60. package/public/docs/rulesets/README.md +25 -0
  61. package/public/docs/rulesets/io.picolabs.oauth.md +71 -0
  62. package/public/docs/rulesets/io.picolabs.pds.md +327 -0
  63. package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +65 -0
  64. package/public/docs/rulesets/io.picolabs.portable_subtree.md +94 -0
  65. package/public/docs/rulesets/io.picolabs.subscription.md +398 -0
  66. package/public/docs/rulesets/io.picolabs.wrangler.md +471 -0
  67. package/public/pico-engine-ui.css +1 -1
  68. package/public/pico-engine-ui.js +28 -26
  69. package/src/RulesetRegistryLoaderFs.ts +43 -11
  70. package/src/auth/cookies.ts +34 -0
  71. package/src/auth/index.ts +7 -0
  72. package/src/bundledKrl.ts +150 -12
  73. package/src/channelIdentityPolicy.ts +29 -0
  74. package/src/cli.ts +98 -40
  75. package/src/engineConfig.ts +345 -0
  76. package/src/identity/IdentityService.ts +189 -1
  77. package/src/identity/crossPico.ts +161 -41
  78. package/src/identity/didoModule.ts +140 -23
  79. package/src/identity/establishSubscription.ts +2 -2
  80. package/src/identity/identityPortable.ts +153 -0
  81. package/src/identity/index.ts +14 -0
  82. package/src/identity/localPeerReconcile.ts +97 -0
  83. package/src/identity/peerDid4.ts +197 -0
  84. package/src/identity/peerDid4Create.ts +121 -0
  85. package/src/identity/portableExportPrepare.ts +302 -0
  86. package/src/identity/portableMove.ts +193 -0
  87. package/src/identity/skyIntro.ts +16 -12
  88. package/src/identity/skyProtocol.ts +46 -0
  89. package/src/identity/skyRotation.ts +211 -0
  90. package/src/identity/store.ts +5 -1
  91. package/src/identity/subscriptionEntSync.ts +97 -0
  92. package/src/identity/veramoDidComm.ts +52 -2
  93. package/src/identity/webvh.ts +4 -4
  94. package/src/identity/webvhRelocation.ts +167 -0
  95. package/src/index.ts +179 -39
  96. package/src/levelDb.ts +122 -0
  97. package/src/logging.ts +63 -11
  98. package/src/oauth/OAuthService.ts +20 -6
  99. package/src/oauth/channelEligibility.ts +9 -0
  100. package/src/oauth/index.ts +7 -1
  101. package/src/policyConfigFromJson.ts +131 -0
  102. package/src/runMcp.ts +30 -0
  103. package/src/server.ts +133 -27
  104. package/src/subtreeWrangler.ts +278 -0
  105. package/src/uiChannelPolicies.ts +16 -0
  106. package/krl/io.picolabs.did-o.krl +0 -349
  107. package/krl/test/didtestdid.krl +0 -78
package/src/server.ts CHANGED
@@ -1,25 +1,37 @@
1
1
  import * as bodyParser from "body-parser";
2
2
  import * as express from "express";
3
3
  import { Express, Request, Response } from "express";
4
+ import * as fs from "fs";
4
5
  import helmet from "helmet";
5
6
  import * as _ from "lodash";
6
7
  import * as path from "path";
8
+ import {
9
+ bundledKrlDir,
10
+ listEngineKrlSources,
11
+ resolveEngineKrlFile,
12
+ } from "./bundledKrl";
7
13
  import { PicoEngineCore } from "pico-engine-core";
8
14
  import { PicoFramework } from "pico-framework";
9
- import { AuthError, AuthService } from "./auth";
10
- import { OAuthError, OAuthService, parseApproveBody, parseAuthorizeQuery, renderConsentHtml } from "./oauth";
15
+ import { AuthCookieNames, AuthError, AuthService, authCookieNames } from "./auth";
16
+ import {
17
+ OAuthError,
18
+ OAuthService,
19
+ parseApproveBody,
20
+ parseAuthorizeQuery,
21
+ renderConsentHtml,
22
+ rootHasOAuthMeshRuleset,
23
+ } from "./oauth";
11
24
  import { uiECIForRoot } from "./provisionRoot";
12
25
  import { IdentityService } from "./identity/IdentityService";
13
26
  import { registerWebvhRoutes } from "./identity/webvhRoutes";
14
27
  import { registerDidcommIngressRoutes } from "./identity/didcommIngressRoutes";
15
28
 
16
- const engineVersion = require("../package.json").version;
29
+ import { getEngineRuntimeSettings } from "./engineConfig";
17
30
 
18
- const SESSION_COOKIE = "pico-session";
19
- const CEREMONY_COOKIE = "pico-ceremony";
31
+ const engineVersion = require("../package.json").version;
20
32
 
21
33
  function localhostChannelBypassEnabled(): boolean {
22
- return process.env.PICO_ENGINE_ALLOW_LOCALHOST_C !== "0";
34
+ return getEngineRuntimeSettings().allowLocalhostChannelBypass;
23
35
  }
24
36
 
25
37
  function mergeGetPost(req: Request) {
@@ -75,6 +87,11 @@ export function server(
75
87
  ): Express {
76
88
  const pf = core.picoFramework;
77
89
  const app = express();
90
+ function cookies(): AuthCookieNames {
91
+ return auth
92
+ ? authCookieNames(auth.rp().origin)
93
+ : authCookieNames("http://localhost:3000");
94
+ }
78
95
 
79
96
  app.use(function(req, res, next) {
80
97
  // /auth routes need a specific credentialed CORS origin (below), not "*".
@@ -145,7 +162,7 @@ export function server(
145
162
  }
146
163
 
147
164
  if (auth) {
148
- registerAuthRoutes(app, auth);
165
+ registerAuthRoutes(app, auth, cookies);
149
166
  }
150
167
 
151
168
  function isEciInRootTree(rootPicoId: string, eci: string): boolean {
@@ -175,7 +192,7 @@ export function server(
175
192
  next();
176
193
  return;
177
194
  }
178
- const token = parseCookies(req)[SESSION_COOKIE] || "";
195
+ const token = parseCookies(req)[cookies().session] || "";
179
196
  auth
180
197
  .getSession(token)
181
198
  .then((session) => {
@@ -251,9 +268,24 @@ export function server(
251
268
  }
252
269
 
253
270
  if (oauth) {
254
- registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
271
+ registerOAuthRoutes(
272
+ app,
273
+ oauth,
274
+ auth,
275
+ requireAuthSession,
276
+ parseCookies,
277
+ cookies
278
+ );
255
279
  }
256
280
 
281
+ app.get("/api/oauth/mesh-enabled", requireAuthSession, function (_req, res) {
282
+ const roots = pf.rootPicos();
283
+ const meshOAuthEnabled =
284
+ roots.length > 0 &&
285
+ roots.some((root) => rootHasOAuthMeshRuleset(pf, root.id));
286
+ res.json({ meshOAuthEnabled });
287
+ });
288
+
257
289
  app.get("/api/mesh-context", function (req, res, next) {
258
290
  if (!oauth) {
259
291
  res.status(503).json({ error: "OAuth not enabled" });
@@ -295,6 +327,38 @@ export function server(
295
327
  .catch(next);
296
328
  });
297
329
 
330
+ app.get("/api/krl-sources", function (req, res, next) {
331
+ try {
332
+ let baseUrl = core.base_url;
333
+ if (!baseUrl) {
334
+ baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
335
+ }
336
+ res.json({ sources: listEngineKrlSources(baseUrl.replace(/\/$/, "")) });
337
+ } catch (err) {
338
+ next(err);
339
+ }
340
+ });
341
+
342
+ app.get(/^\/krl\/(.+\.krl)$/, function (req, res, next) {
343
+ const suffix = req.params[0];
344
+ if (typeof suffix !== "string" || !suffix.endsWith(".krl")) {
345
+ res.status(404).end();
346
+ return;
347
+ }
348
+ const filePath = resolveEngineKrlFile(suffix);
349
+ if (!filePath) {
350
+ res.status(404).end();
351
+ return;
352
+ }
353
+ fs.readFile(filePath, { encoding: "utf8" }, function (err, data) {
354
+ if (err) {
355
+ next(err);
356
+ return;
357
+ }
358
+ res.type("text/plain").send(data);
359
+ });
360
+ });
361
+
298
362
  app.get("/api/ui-context", function (req, res, next) {
299
363
  const hasRoots = pf.rootPicos().length > 0;
300
364
  const base: any = {
@@ -307,7 +371,7 @@ export function server(
307
371
  res.json(base);
308
372
  return;
309
373
  }
310
- const token = parseCookies(req)[SESSION_COOKIE] || "";
374
+ const token = parseCookies(req)[cookies().session] || "";
311
375
  auth
312
376
  .needsAuthMigration()
313
377
  .then((needsAuthMigration) => {
@@ -324,6 +388,27 @@ export function server(
324
388
  .catch(next);
325
389
  });
326
390
 
391
+ /** Localhost-only context for dev MCP (stdio) and other local tooling. */
392
+ app.get("/api/dev/local", function (req, res) {
393
+ if (!localhostChannelBypassEnabled() || !isLocalhostRequest(req)) {
394
+ res.status(403).json({ error: "localhost only" });
395
+ return;
396
+ }
397
+ const roots = pf.rootPicos();
398
+ const rootPico = roots[0];
399
+ const rootUiEci = rootPico ? uiECIForRoot(pf, rootPico.id) : null;
400
+ let baseUrl = core.base_url;
401
+ if (!baseUrl) {
402
+ baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
403
+ }
404
+ res.json({
405
+ version: engineVersion,
406
+ hasRoots: roots.length > 0,
407
+ rootUiEci,
408
+ baseUrl: baseUrl.replace(/\/$/, ""),
409
+ });
410
+ });
411
+
327
412
  app.all("/api/flush", requireAuthSession, function (req, res, next) {
328
413
  const attrs = mergeGetPost(req);
329
414
  const url = attrs["url"];
@@ -490,6 +575,17 @@ export function server(
490
575
  // Legacy alias; prefer /sky/query/ per Sky Query API docs.
491
576
  app.all("/sky/cloud/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
492
577
 
578
+ // Mesh MCP (Streamable HTTP, OAuth bearer scoped to :eci)
579
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
580
+ const { registerMeshMcpRoutes } = require("../mcp/meshHttp.js") as {
581
+ registerMeshMcpRoutes: (
582
+ app: Express,
583
+ core: PicoEngineCore,
584
+ oauth?: OAuthService
585
+ ) => void;
586
+ };
587
+ registerMeshMcpRoutes(app, core, oauth);
588
+
493
589
  app.use(function (
494
590
  err: any,
495
591
  req: express.Request,
@@ -505,7 +601,12 @@ export function server(
505
601
  return;
506
602
  }
507
603
  let message = err + "";
508
- console.error(err);
604
+ const policyNoise =
605
+ process.env.NODE_ENV === "test" &&
606
+ /not allowed by channel policy/i.test(message);
607
+ if (!policyNoise) {
608
+ console.error(err);
609
+ }
509
610
  if (err?.krl_compiler?.loc?.start) {
510
611
  message +=
511
612
  " [at line " +
@@ -532,9 +633,13 @@ export function server(
532
633
  return app;
533
634
  }
534
635
 
535
- function registerAuthRoutes(app: Express, auth: AuthService) {
636
+ function registerAuthRoutes(
637
+ app: Express,
638
+ auth: AuthService,
639
+ cookies: () => AuthCookieNames
640
+ ) {
536
641
  function setSessionCookie(res: Response, token: string, expiry: number) {
537
- res.cookie(SESSION_COOKIE, token, {
642
+ res.cookie(cookies().session, token, {
538
643
  httpOnly: true,
539
644
  sameSite: "lax",
540
645
  secure: auth.isSecure(),
@@ -544,7 +649,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
544
649
  }
545
650
 
546
651
  function setCeremonyCookie(res: Response, ceremonyId: string) {
547
- res.cookie(CEREMONY_COOKIE, ceremonyId, {
652
+ res.cookie(cookies().ceremony, ceremonyId, {
548
653
  httpOnly: true,
549
654
  sameSite: "lax",
550
655
  secure: auth.isSecure(),
@@ -554,7 +659,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
554
659
  }
555
660
 
556
661
  function clearCeremonyCookie(res: Response) {
557
- res.clearCookie(CEREMONY_COOKIE, { path: "/" });
662
+ res.clearCookie(cookies().ceremony, { path: "/" });
558
663
  }
559
664
 
560
665
  // WebAuthn requires a specific, credentialed origin — never "*".
@@ -578,7 +683,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
578
683
  });
579
684
 
580
685
  function requireSession(req: Request, res: Response, next: express.NextFunction) {
581
- const token = parseCookies(req)[SESSION_COOKIE] || "";
686
+ const token = parseCookies(req)[cookies().session] || "";
582
687
  auth
583
688
  .getSession(token)
584
689
  .then((session) => {
@@ -606,7 +711,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
606
711
  });
607
712
 
608
713
  app.post("/auth/register/verify", function (req, res, next) {
609
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
714
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
610
715
  auth
611
716
  .registerNewAccountVerify({ ceremonyId, response: req.body })
612
717
  .then(({ account, session, uiECI }) => {
@@ -633,7 +738,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
633
738
  });
634
739
 
635
740
  app.post("/auth/claim/verify", function (req, res, next) {
636
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
741
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
637
742
  auth
638
743
  .claimPrimaryRootVerify({ ceremonyId, response: req.body })
639
744
  .then(({ account, session, uiECI }) => {
@@ -660,7 +765,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
660
765
  });
661
766
 
662
767
  app.post("/auth/login/verify", function (req, res, next) {
663
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
768
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
664
769
  auth
665
770
  .loginVerify({ ceremonyId, response: req.body })
666
771
  .then(({ account, session }) => {
@@ -676,18 +781,18 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
676
781
  });
677
782
 
678
783
  app.post("/auth/logout", function (req, res, next) {
679
- const token = parseCookies(req)[SESSION_COOKIE] || "";
784
+ const token = parseCookies(req)[cookies().session] || "";
680
785
  auth
681
786
  .deleteSession(token)
682
787
  .then(() => {
683
- res.clearCookie(SESSION_COOKIE, { path: "/" });
788
+ res.clearCookie(cookies().session, { path: "/" });
684
789
  res.json({ ok: true });
685
790
  })
686
791
  .catch(next);
687
792
  });
688
793
 
689
794
  app.get("/auth/session", function (req, res, next) {
690
- const token = parseCookies(req)[SESSION_COOKIE] || "";
795
+ const token = parseCookies(req)[cookies().session] || "";
691
796
  auth
692
797
  .whoami(token)
693
798
  .then((who) => res.json(who))
@@ -707,7 +812,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
707
812
 
708
813
  app.post("/auth/credentials/verify", requireSession, function (req, res, next) {
709
814
  const session = (req as any).picoSession;
710
- const ceremonyId = parseCookies(req)[CEREMONY_COOKIE] || "";
815
+ const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
711
816
  auth
712
817
  .addCredentialVerify({
713
818
  ceremonyId,
@@ -759,7 +864,8 @@ function registerOAuthRoutes(
759
864
  res: Response,
760
865
  next: express.NextFunction
761
866
  ) => void,
762
- parseCookiesFn: (req: Request) => { [name: string]: string }
867
+ parseCookiesFn: (req: Request) => { [name: string]: string },
868
+ cookies: () => AuthCookieNames
763
869
  ) {
764
870
  function requireOAuthSession(
765
871
  req: Request,
@@ -770,7 +876,7 @@ function registerOAuthRoutes(
770
876
  res.status(503).json({ error: "Authentication is not configured" });
771
877
  return;
772
878
  }
773
- const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
879
+ const token = parseCookiesFn(req)[cookies().session] || "";
774
880
  auth
775
881
  .getSession(token)
776
882
  .then((session) => {
@@ -816,7 +922,7 @@ function registerOAuthRoutes(
816
922
  res.status(503).send("Authentication is not configured");
817
923
  return;
818
924
  }
819
- const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
925
+ const token = parseCookiesFn(req)[cookies().session] || "";
820
926
  const session = await auth.getSession(token);
821
927
  if (!session) {
822
928
  const returnPath = req.originalUrl || "/oauth/authorize";
@@ -839,7 +945,7 @@ function registerOAuthRoutes(
839
945
  res.status(503).send("Authentication is not configured");
840
946
  return;
841
947
  }
842
- const token = parseCookiesFn(req)[SESSION_COOKIE] || "";
948
+ const token = parseCookiesFn(req)[cookies().session] || "";
843
949
  auth
844
950
  .getSession(token)
845
951
  .then((session) => {
@@ -0,0 +1,278 @@
1
+ /**
2
+ * Subtree import child creation via wrangler (engine layer — above ctx).
3
+ */
4
+
5
+ import {
6
+ allowsEventPolicy,
7
+ ChannelReadOnly,
8
+ Pico,
9
+ PicoEvent,
10
+ PicoFramework,
11
+ } from "pico-framework";
12
+ import type {
13
+ CreateImportChildOptions,
14
+ RulesetRegistry,
15
+ RulesetStateServices,
16
+ } from "pico-engine-core";
17
+
18
+ const WRANGLER_RID = "io.picolabs.wrangler";
19
+ const ENGINE_UI_RID = "io.picolabs.pico-engine-ui";
20
+ const SUBSCRIPTION_RID = "io.picolabs.subscription";
21
+ const PDS_RID = "io.picolabs.pds";
22
+
23
+ function isUiChannel(c: ChannelReadOnly): boolean {
24
+ return [...c.tags].sort().join(",") === "engine,ui";
25
+ }
26
+
27
+ function nonFamilyChannels(pico: Pico): ChannelReadOnly[] {
28
+ return pico.toReadOnly().channels.filter((c) => !c.familyChannelPicoID);
29
+ }
30
+
31
+ function channelAllowsEvent(
32
+ c: ChannelReadOnly,
33
+ event: Pick<PicoEvent, "domain" | "name">
34
+ ): boolean {
35
+ return allowsEventPolicy(c.eventPolicy, event);
36
+ }
37
+
38
+ /** Pick a channel that may deliver a specific wrangler bootstrap event. */
39
+ function pickEventEci(
40
+ pico: Pico,
41
+ event: Pick<PicoEvent, "domain" | "name">
42
+ ): string {
43
+ const channels = nonFamilyChannels(pico);
44
+ if (channels.length === 0) {
45
+ throw new Error(
46
+ `Pico ${pico.id} has no non-family channels for wrangler events`
47
+ );
48
+ }
49
+ const permitted = channels.filter((c) => channelAllowsEvent(c, event));
50
+ if (permitted.length > 0) {
51
+ const ui = permitted.find((c) => isUiChannel(c));
52
+ return (ui ?? permitted[0]).id;
53
+ }
54
+ throw new Error(
55
+ `Pico ${pico.id} has no channel that allows ${event.domain}:${event.name}`
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Prefer caller's channel when it allows the event; otherwise pick one that does.
61
+ * HTTP/UI import passes the UI ECI — skip it when policy blocks wrangler bootstrap.
62
+ */
63
+ function resolveWranglerChannelEci(
64
+ pico: Pico,
65
+ event: Pick<PicoEvent, "domain" | "name">,
66
+ preferred?: string
67
+ ): string {
68
+ if (preferred) {
69
+ const ch = nonFamilyChannels(pico).find((c) => c.id === preferred);
70
+ if (ch && channelAllowsEvent(ch, event)) {
71
+ return preferred;
72
+ }
73
+ }
74
+ return pickEventEci(pico, event);
75
+ }
76
+
77
+ async function rulesetFromParent(
78
+ parent: Pico,
79
+ rid: string,
80
+ rsRegistry: RulesetRegistry
81
+ ) {
82
+ const config = parent.rulesets[rid]?.config;
83
+ if (!config?.url) {
84
+ throw new Error(
85
+ `Parent pico ${parent.id} does not have ${rid} installed (required for wrangler import bootstrap)`
86
+ );
87
+ }
88
+ const loaded = await rsRegistry.load(config.url);
89
+ return { rs: loaded.ruleset, config };
90
+ }
91
+
92
+ /** Same bootstrap as wrangler bootstrapNewChild, without blocking the parent's event queue. */
93
+ async function bootstrapImportChildSync(
94
+ pf: PicoFramework,
95
+ parent: Pico,
96
+ name: string,
97
+ rsRegistry: RulesetRegistry,
98
+ options?: CreateImportChildOptions
99
+ ): Promise<{ familyEci: string; child: Pico }> {
100
+ const ui = await rulesetFromParent(parent, ENGINE_UI_RID, rsRegistry);
101
+ const wrangler = await rulesetFromParent(parent, WRANGLER_RID, rsRegistry);
102
+ const familyEci = await parent.newPico({
103
+ rulesets: [ui, wrangler],
104
+ });
105
+ const child = pf.getPico(familyEci);
106
+ if (!child) {
107
+ throw new Error(`Failed to resolve child for family ECI ${familyEci}`);
108
+ }
109
+
110
+ const parentUi = parent.toReadOnly().channels.find(
111
+ (c) => [...c.tags].sort().join(",") === "engine,ui"
112
+ );
113
+
114
+ await pf.eventWait(
115
+ {
116
+ eci: familyEci,
117
+ domain: "wrangler",
118
+ name: "pico_created",
119
+ data: { attrs: { name: name || "imported" } },
120
+ time: Date.now(),
121
+ },
122
+ parent.id
123
+ );
124
+
125
+ await waitForWranglerBootstrapRulesets(
126
+ pf,
127
+ parent.id,
128
+ familyEci,
129
+ child
130
+ );
131
+
132
+ const uiEci = await pf.eventQuery(
133
+ {
134
+ eci: familyEci,
135
+ domain: "engine_ui",
136
+ name: "setup",
137
+ data: { attrs: {} },
138
+ time: Date.now(),
139
+ },
140
+ {
141
+ eci: familyEci,
142
+ rid: ENGINE_UI_RID,
143
+ name: "uiECI",
144
+ args: {},
145
+ },
146
+ parent.id
147
+ );
148
+
149
+ await pf.event({
150
+ eci: uiEci,
151
+ domain: "engine_ui",
152
+ name: "box",
153
+ data: {
154
+ attrs: {
155
+ name: name || "imported",
156
+ parentUiEci: parentUi?.id,
157
+ },
158
+ },
159
+ time: Date.now(),
160
+ });
161
+
162
+ const newChildCreated = {
163
+ domain: "wrangler" as const,
164
+ name: "new_child_created" as const,
165
+ };
166
+ const parentEci = resolveWranglerChannelEci(
167
+ parent,
168
+ newChildCreated,
169
+ options?.channelEci
170
+ );
171
+ await pf.event({
172
+ eci: parentEci,
173
+ domain: newChildCreated.domain,
174
+ name: newChildCreated.name,
175
+ data: {
176
+ attrs: {
177
+ name: name || "imported",
178
+ eci: familyEci,
179
+ uiEci,
180
+ },
181
+ },
182
+ time: Date.now(),
183
+ });
184
+
185
+ return { familyEci, child };
186
+ }
187
+
188
+ function parentIsHandlingEvent(parent: Pico): boolean {
189
+ return parent.getCurrentTxn()?.kind === "event";
190
+ }
191
+
192
+ /** Wait for pico_created's queued install_ruleset_request (pds + subscription). */
193
+ async function waitForWranglerBootstrapRulesets(
194
+ pf: PicoFramework,
195
+ parentPicoId: string,
196
+ familyEci: string,
197
+ child: Pico,
198
+ timeoutMs = 20000
199
+ ): Promise<void> {
200
+ const need = [SUBSCRIPTION_RID, PDS_RID];
201
+ const deadline = Date.now() + timeoutMs;
202
+ while (Date.now() < deadline) {
203
+ if (need.every((rid) => child.rulesets[rid])) {
204
+ return;
205
+ }
206
+ await pf
207
+ .query(
208
+ {
209
+ eci: familyEci,
210
+ rid: WRANGLER_RID,
211
+ name: "id",
212
+ args: {},
213
+ },
214
+ parentPicoId
215
+ )
216
+ .catch(() => undefined);
217
+ await new Promise((resolve) => setTimeout(resolve, 15));
218
+ }
219
+ const missing = need.filter((rid) => !child.rulesets[rid]);
220
+ throw new Error(
221
+ `Import child wrangler bootstrap missing rulesets: ${missing.join(", ")}`
222
+ );
223
+ }
224
+
225
+ /** Create an import child through wrangler (sync bootstrap when parent queue is busy). */
226
+ export async function createImportChildViaWrangler(
227
+ pf: PicoFramework,
228
+ parent: Pico,
229
+ name: string,
230
+ rsRegistry: RulesetRegistry,
231
+ options?: CreateImportChildOptions
232
+ ): Promise<{ familyEci: string; child: Pico }> {
233
+ if (parentIsHandlingEvent(parent)) {
234
+ return bootstrapImportChildSync(pf, parent, name, rsRegistry, options);
235
+ }
236
+
237
+ const newChildRequest = {
238
+ domain: "wrangler" as const,
239
+ name: "new_child_request" as const,
240
+ };
241
+ const parentEci = resolveWranglerChannelEci(
242
+ parent,
243
+ newChildRequest,
244
+ options?.channelEci
245
+ );
246
+ const childCountBefore = parent.toReadOnly().children.length;
247
+
248
+ await pf.eventWait({
249
+ eci: parentEci,
250
+ domain: newChildRequest.domain,
251
+ name: newChildRequest.name,
252
+ data: { attrs: { name: name || "imported" } },
253
+ time: Date.now(),
254
+ });
255
+
256
+ const children = parent.toReadOnly().children;
257
+ if (children.length <= childCountBefore) {
258
+ throw new Error(
259
+ "wrangler new_child_request did not create a child (is io.picolabs.wrangler installed on the parent?)"
260
+ );
261
+ }
262
+ const familyEci = children[children.length - 1];
263
+ const child = pf.getPico(familyEci);
264
+ if (!child) {
265
+ throw new Error(`Failed to resolve child for family ECI ${familyEci}`);
266
+ }
267
+ return { familyEci, child };
268
+ }
269
+
270
+ /** Wrangler-backed child creation wired into ctx subtree import. */
271
+ export function wranglerSubtreeImportServices(
272
+ rsRegistry: RulesetRegistry
273
+ ): Pick<RulesetStateServices, "createImportChild"> {
274
+ return {
275
+ createImportChild: (pf, parent, name, opts) =>
276
+ createImportChildViaWrangler(pf, parent, name, rsRegistry, opts),
277
+ };
278
+ }
@@ -14,7 +14,12 @@ export const UI_CHANNEL_EVENT_POLICY: EventPolicy = {
14
14
  { domain: "engine_ui", name: "del_channel" },
15
15
  { domain: "engine_ui", name: "update_channel" },
16
16
  { domain: "engine_ui", name: "testing_eci" },
17
+ { domain: "engine_ui", name: "testing_history_record" },
18
+ { domain: "engine_ui", name: "testing_history_clear" },
19
+ { domain: "engine_ui", name: "testing_saved_upsert" },
20
+ { domain: "engine_ui", name: "testing_saved_delete" },
17
21
  { domain: "engine", name: "started" },
22
+ { domain: "engine", name: "ruleset_set_changed" },
18
23
  { domain: "wrangler", name: "subscription" },
19
24
  { domain: "wrangler", name: "relationship" },
20
25
  { domain: "wrangler", name: "pending_subscription_approval" },
@@ -27,6 +32,12 @@ export const UI_CHANNEL_EVENT_POLICY: EventPolicy = {
27
32
  { domain: "wrangler", name: "relationship_cancellation" },
28
33
  { domain: "wrangler", name: "set_public_intro" },
29
34
  { domain: "wrangler", name: "send_event_on_subs" },
35
+ { domain: "wrangler", name: "send_event_on_relationships" },
36
+ { domain: "wrangler", name: "child_deleted" },
37
+ { domain: "wrangler", name: "new_child_request" },
38
+ { domain: "wrangler", name: "new_child_created" },
39
+ { domain: "wrangler", name: "install_ruleset_request" },
40
+ { domain: "wrangler", name: "new_channel_request" },
30
41
  ],
31
42
  deny: [],
32
43
  };
@@ -35,11 +46,16 @@ export const UI_CHANNEL_EVENT_POLICY: EventPolicy = {
35
46
  export const UI_CHANNEL_QUERY_POLICY: QueryPolicy = {
36
47
  allow: [
37
48
  { rid: "*", name: "__testing" },
49
+ { rid: "*", name: "__consoleEval" },
50
+ { rid: "io.picolabs.wrangler", name: "rulesetSchema" },
51
+ { rid: "io.picolabs.wrangler", name: "picoSchema" },
38
52
  { rid: "io.picolabs.pico-engine-ui", name: "uiECI" },
39
53
  { rid: "io.picolabs.pico-engine-ui", name: "box" },
40
54
  { rid: "io.picolabs.pico-engine-ui", name: "pico" },
41
55
  { rid: "io.picolabs.pico-engine-ui", name: "logs" },
42
56
  { rid: "io.picolabs.pico-engine-ui", name: "testingECI" },
57
+ { rid: "io.picolabs.pico-engine-ui", name: "testingHistory" },
58
+ { rid: "io.picolabs.pico-engine-ui", name: "testingSaved" },
43
59
  { rid: "io.picolabs.pico-engine-ui", name: "name" },
44
60
  { rid: "io.picolabs.subscription", name: "established" },
45
61
  { rid: "io.picolabs.subscription", name: "inbound" },