pico-engine 1.6.4 → 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.
- package/README.md +13 -8
- package/dist/RulesetRegistryLoaderFs.js +14 -2
- package/dist/auth/cookies.js +32 -0
- package/dist/auth/index.js +6 -1
- package/dist/channelIdentityPolicy.js +22 -0
- package/dist/cli.js +104 -69
- package/dist/engineConfig.js +280 -0
- package/dist/identity/IdentityService.js +90 -0
- package/dist/identity/crossPico.js +24 -8
- package/dist/identity/didoModule.js +80 -18
- package/dist/identity/establishSubscription.js +2 -2
- package/dist/identity/identityPortable.js +92 -0
- package/dist/identity/index.js +10 -1
- package/dist/identity/localPeerReconcile.js +65 -0
- package/dist/identity/peerDid4.js +175 -0
- package/dist/identity/peerDid4Create.js +90 -0
- package/dist/identity/portableExportPrepare.js +196 -0
- package/dist/identity/portableMove.js +87 -0
- package/dist/identity/skyIntro.js +6 -3
- package/dist/identity/skyProtocol.js +26 -1
- package/dist/identity/skyRotation.js +121 -0
- package/dist/identity/store.js +5 -1
- package/dist/identity/subscriptionEntSync.js +72 -0
- package/dist/identity/veramoDidComm.js +41 -2
- package/dist/identity/webvh.js +4 -2
- package/dist/identity/webvhRelocation.js +115 -0
- package/dist/index.js +82 -22
- package/dist/logging.js +8 -12
- package/dist/oauth/OAuthService.js +6 -2
- package/dist/policyConfigFromJson.js +112 -0
- package/dist/runMcp.js +27 -0
- package/dist/server.js +56 -24
- package/dist/subtreeWrangler.js +180 -0
- package/dist/uiChannelPolicies.js +15 -0
- package/krl/io.picolabs.oauth.krl +41 -2
- package/krl/io.picolabs.pds.krl +131 -10
- package/krl/io.picolabs.pico-engine-ui.krl +209 -21
- package/krl/io.picolabs.portable_subtree.krl +97 -0
- package/krl/io.picolabs.subscription.krl +180 -72
- package/krl/io.picolabs.wrangler.krl +296 -57
- package/mcp/devResources.js +118 -0
- package/mcp/devTools.js +351 -0
- package/mcp/engineClient.js +133 -0
- package/mcp/meshHttp.js +137 -0
- package/mcp/meshTools.js +189 -0
- package/mcp/runStdio.js +34 -0
- package/mcp/schemaToZod.js +56 -0
- package/package.json +20 -8
- package/public/docs/_sidebar.md +2 -1
- package/public/docs/llms-full.txt +479 -0
- package/public/docs/llms.txt +37 -0
- package/public/docs/openapi.yaml +78 -0
- package/public/docs/pico-engine.md +235 -4
- package/public/docs/pico-overview.md +117 -0
- package/public/docs/pico.md +3 -1
- package/public/docs/rulesets/README.md +25 -0
- package/public/docs/rulesets/io.picolabs.oauth.md +71 -0
- package/public/docs/rulesets/io.picolabs.pds.md +327 -0
- package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +65 -0
- package/public/docs/rulesets/io.picolabs.portable_subtree.md +94 -0
- package/public/docs/rulesets/io.picolabs.subscription.md +398 -0
- package/public/docs/rulesets/io.picolabs.wrangler.md +471 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +28 -26
- package/src/RulesetRegistryLoaderFs.ts +18 -3
- package/src/auth/cookies.ts +34 -0
- package/src/auth/index.ts +7 -0
- package/src/channelIdentityPolicy.ts +29 -0
- package/src/cli.ts +68 -37
- package/src/engineConfig.ts +345 -0
- package/src/identity/IdentityService.ts +189 -1
- package/src/identity/crossPico.ts +57 -8
- package/src/identity/didoModule.ts +140 -23
- package/src/identity/establishSubscription.ts +2 -2
- package/src/identity/identityPortable.ts +153 -0
- package/src/identity/index.ts +14 -0
- package/src/identity/localPeerReconcile.ts +97 -0
- package/src/identity/peerDid4.ts +197 -0
- package/src/identity/peerDid4Create.ts +121 -0
- package/src/identity/portableExportPrepare.ts +302 -0
- package/src/identity/portableMove.ts +193 -0
- package/src/identity/skyIntro.ts +11 -11
- package/src/identity/skyProtocol.ts +46 -0
- package/src/identity/skyRotation.ts +211 -0
- package/src/identity/store.ts +5 -1
- package/src/identity/subscriptionEntSync.ts +97 -0
- package/src/identity/veramoDidComm.ts +52 -2
- package/src/identity/webvh.ts +4 -4
- package/src/identity/webvhRelocation.ts +167 -0
- package/src/index.ts +119 -25
- package/src/logging.ts +8 -14
- package/src/oauth/OAuthService.ts +11 -3
- package/src/policyConfigFromJson.ts +131 -0
- package/src/runMcp.ts +30 -0
- package/src/server.ts +79 -26
- package/src/subtreeWrangler.ts +278 -0
- package/src/uiChannelPolicies.ts +15 -0
- package/krl/io.picolabs.did-o.krl +0 -349
package/src/server.ts
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
} from "./bundledKrl";
|
|
13
13
|
import { PicoEngineCore } from "pico-engine-core";
|
|
14
14
|
import { PicoFramework } from "pico-framework";
|
|
15
|
-
import { AuthError, AuthService } from "./auth";
|
|
15
|
+
import { AuthCookieNames, AuthError, AuthService, authCookieNames } from "./auth";
|
|
16
16
|
import {
|
|
17
17
|
OAuthError,
|
|
18
18
|
OAuthService,
|
|
@@ -26,13 +26,12 @@ import { IdentityService } from "./identity/IdentityService";
|
|
|
26
26
|
import { registerWebvhRoutes } from "./identity/webvhRoutes";
|
|
27
27
|
import { registerDidcommIngressRoutes } from "./identity/didcommIngressRoutes";
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
import { getEngineRuntimeSettings } from "./engineConfig";
|
|
30
30
|
|
|
31
|
-
const
|
|
32
|
-
const CEREMONY_COOKIE = "pico-ceremony";
|
|
31
|
+
const engineVersion = require("../package.json").version;
|
|
33
32
|
|
|
34
33
|
function localhostChannelBypassEnabled(): boolean {
|
|
35
|
-
return
|
|
34
|
+
return getEngineRuntimeSettings().allowLocalhostChannelBypass;
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
function mergeGetPost(req: Request) {
|
|
@@ -88,6 +87,11 @@ export function server(
|
|
|
88
87
|
): Express {
|
|
89
88
|
const pf = core.picoFramework;
|
|
90
89
|
const app = express();
|
|
90
|
+
function cookies(): AuthCookieNames {
|
|
91
|
+
return auth
|
|
92
|
+
? authCookieNames(auth.rp().origin)
|
|
93
|
+
: authCookieNames("http://localhost:3000");
|
|
94
|
+
}
|
|
91
95
|
|
|
92
96
|
app.use(function(req, res, next) {
|
|
93
97
|
// /auth routes need a specific credentialed CORS origin (below), not "*".
|
|
@@ -158,7 +162,7 @@ export function server(
|
|
|
158
162
|
}
|
|
159
163
|
|
|
160
164
|
if (auth) {
|
|
161
|
-
registerAuthRoutes(app, auth);
|
|
165
|
+
registerAuthRoutes(app, auth, cookies);
|
|
162
166
|
}
|
|
163
167
|
|
|
164
168
|
function isEciInRootTree(rootPicoId: string, eci: string): boolean {
|
|
@@ -188,7 +192,7 @@ export function server(
|
|
|
188
192
|
next();
|
|
189
193
|
return;
|
|
190
194
|
}
|
|
191
|
-
const token = parseCookies(req)[
|
|
195
|
+
const token = parseCookies(req)[cookies().session] || "";
|
|
192
196
|
auth
|
|
193
197
|
.getSession(token)
|
|
194
198
|
.then((session) => {
|
|
@@ -264,7 +268,14 @@ export function server(
|
|
|
264
268
|
}
|
|
265
269
|
|
|
266
270
|
if (oauth) {
|
|
267
|
-
registerOAuthRoutes(
|
|
271
|
+
registerOAuthRoutes(
|
|
272
|
+
app,
|
|
273
|
+
oauth,
|
|
274
|
+
auth,
|
|
275
|
+
requireAuthSession,
|
|
276
|
+
parseCookies,
|
|
277
|
+
cookies
|
|
278
|
+
);
|
|
268
279
|
}
|
|
269
280
|
|
|
270
281
|
app.get("/api/oauth/mesh-enabled", requireAuthSession, function (_req, res) {
|
|
@@ -360,7 +371,7 @@ export function server(
|
|
|
360
371
|
res.json(base);
|
|
361
372
|
return;
|
|
362
373
|
}
|
|
363
|
-
const token = parseCookies(req)[
|
|
374
|
+
const token = parseCookies(req)[cookies().session] || "";
|
|
364
375
|
auth
|
|
365
376
|
.needsAuthMigration()
|
|
366
377
|
.then((needsAuthMigration) => {
|
|
@@ -377,6 +388,27 @@ export function server(
|
|
|
377
388
|
.catch(next);
|
|
378
389
|
});
|
|
379
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
|
+
|
|
380
412
|
app.all("/api/flush", requireAuthSession, function (req, res, next) {
|
|
381
413
|
const attrs = mergeGetPost(req);
|
|
382
414
|
const url = attrs["url"];
|
|
@@ -543,6 +575,17 @@ export function server(
|
|
|
543
575
|
// Legacy alias; prefer /sky/query/ per Sky Query API docs.
|
|
544
576
|
app.all("/sky/cloud/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
|
|
545
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
|
+
|
|
546
589
|
app.use(function (
|
|
547
590
|
err: any,
|
|
548
591
|
req: express.Request,
|
|
@@ -558,7 +601,12 @@ export function server(
|
|
|
558
601
|
return;
|
|
559
602
|
}
|
|
560
603
|
let message = err + "";
|
|
561
|
-
|
|
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
|
+
}
|
|
562
610
|
if (err?.krl_compiler?.loc?.start) {
|
|
563
611
|
message +=
|
|
564
612
|
" [at line " +
|
|
@@ -585,9 +633,13 @@ export function server(
|
|
|
585
633
|
return app;
|
|
586
634
|
}
|
|
587
635
|
|
|
588
|
-
function registerAuthRoutes(
|
|
636
|
+
function registerAuthRoutes(
|
|
637
|
+
app: Express,
|
|
638
|
+
auth: AuthService,
|
|
639
|
+
cookies: () => AuthCookieNames
|
|
640
|
+
) {
|
|
589
641
|
function setSessionCookie(res: Response, token: string, expiry: number) {
|
|
590
|
-
res.cookie(
|
|
642
|
+
res.cookie(cookies().session, token, {
|
|
591
643
|
httpOnly: true,
|
|
592
644
|
sameSite: "lax",
|
|
593
645
|
secure: auth.isSecure(),
|
|
@@ -597,7 +649,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
597
649
|
}
|
|
598
650
|
|
|
599
651
|
function setCeremonyCookie(res: Response, ceremonyId: string) {
|
|
600
|
-
res.cookie(
|
|
652
|
+
res.cookie(cookies().ceremony, ceremonyId, {
|
|
601
653
|
httpOnly: true,
|
|
602
654
|
sameSite: "lax",
|
|
603
655
|
secure: auth.isSecure(),
|
|
@@ -607,7 +659,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
607
659
|
}
|
|
608
660
|
|
|
609
661
|
function clearCeremonyCookie(res: Response) {
|
|
610
|
-
res.clearCookie(
|
|
662
|
+
res.clearCookie(cookies().ceremony, { path: "/" });
|
|
611
663
|
}
|
|
612
664
|
|
|
613
665
|
// WebAuthn requires a specific, credentialed origin — never "*".
|
|
@@ -631,7 +683,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
631
683
|
});
|
|
632
684
|
|
|
633
685
|
function requireSession(req: Request, res: Response, next: express.NextFunction) {
|
|
634
|
-
const token = parseCookies(req)[
|
|
686
|
+
const token = parseCookies(req)[cookies().session] || "";
|
|
635
687
|
auth
|
|
636
688
|
.getSession(token)
|
|
637
689
|
.then((session) => {
|
|
@@ -659,7 +711,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
659
711
|
});
|
|
660
712
|
|
|
661
713
|
app.post("/auth/register/verify", function (req, res, next) {
|
|
662
|
-
const ceremonyId = parseCookies(req)[
|
|
714
|
+
const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
|
|
663
715
|
auth
|
|
664
716
|
.registerNewAccountVerify({ ceremonyId, response: req.body })
|
|
665
717
|
.then(({ account, session, uiECI }) => {
|
|
@@ -686,7 +738,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
686
738
|
});
|
|
687
739
|
|
|
688
740
|
app.post("/auth/claim/verify", function (req, res, next) {
|
|
689
|
-
const ceremonyId = parseCookies(req)[
|
|
741
|
+
const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
|
|
690
742
|
auth
|
|
691
743
|
.claimPrimaryRootVerify({ ceremonyId, response: req.body })
|
|
692
744
|
.then(({ account, session, uiECI }) => {
|
|
@@ -713,7 +765,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
713
765
|
});
|
|
714
766
|
|
|
715
767
|
app.post("/auth/login/verify", function (req, res, next) {
|
|
716
|
-
const ceremonyId = parseCookies(req)[
|
|
768
|
+
const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
|
|
717
769
|
auth
|
|
718
770
|
.loginVerify({ ceremonyId, response: req.body })
|
|
719
771
|
.then(({ account, session }) => {
|
|
@@ -729,18 +781,18 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
729
781
|
});
|
|
730
782
|
|
|
731
783
|
app.post("/auth/logout", function (req, res, next) {
|
|
732
|
-
const token = parseCookies(req)[
|
|
784
|
+
const token = parseCookies(req)[cookies().session] || "";
|
|
733
785
|
auth
|
|
734
786
|
.deleteSession(token)
|
|
735
787
|
.then(() => {
|
|
736
|
-
res.clearCookie(
|
|
788
|
+
res.clearCookie(cookies().session, { path: "/" });
|
|
737
789
|
res.json({ ok: true });
|
|
738
790
|
})
|
|
739
791
|
.catch(next);
|
|
740
792
|
});
|
|
741
793
|
|
|
742
794
|
app.get("/auth/session", function (req, res, next) {
|
|
743
|
-
const token = parseCookies(req)[
|
|
795
|
+
const token = parseCookies(req)[cookies().session] || "";
|
|
744
796
|
auth
|
|
745
797
|
.whoami(token)
|
|
746
798
|
.then((who) => res.json(who))
|
|
@@ -760,7 +812,7 @@ function registerAuthRoutes(app: Express, auth: AuthService) {
|
|
|
760
812
|
|
|
761
813
|
app.post("/auth/credentials/verify", requireSession, function (req, res, next) {
|
|
762
814
|
const session = (req as any).picoSession;
|
|
763
|
-
const ceremonyId = parseCookies(req)[
|
|
815
|
+
const ceremonyId = parseCookies(req)[cookies().ceremony] || "";
|
|
764
816
|
auth
|
|
765
817
|
.addCredentialVerify({
|
|
766
818
|
ceremonyId,
|
|
@@ -812,7 +864,8 @@ function registerOAuthRoutes(
|
|
|
812
864
|
res: Response,
|
|
813
865
|
next: express.NextFunction
|
|
814
866
|
) => void,
|
|
815
|
-
parseCookiesFn: (req: Request) => { [name: string]: string }
|
|
867
|
+
parseCookiesFn: (req: Request) => { [name: string]: string },
|
|
868
|
+
cookies: () => AuthCookieNames
|
|
816
869
|
) {
|
|
817
870
|
function requireOAuthSession(
|
|
818
871
|
req: Request,
|
|
@@ -823,7 +876,7 @@ function registerOAuthRoutes(
|
|
|
823
876
|
res.status(503).json({ error: "Authentication is not configured" });
|
|
824
877
|
return;
|
|
825
878
|
}
|
|
826
|
-
const token = parseCookiesFn(req)[
|
|
879
|
+
const token = parseCookiesFn(req)[cookies().session] || "";
|
|
827
880
|
auth
|
|
828
881
|
.getSession(token)
|
|
829
882
|
.then((session) => {
|
|
@@ -869,7 +922,7 @@ function registerOAuthRoutes(
|
|
|
869
922
|
res.status(503).send("Authentication is not configured");
|
|
870
923
|
return;
|
|
871
924
|
}
|
|
872
|
-
const token = parseCookiesFn(req)[
|
|
925
|
+
const token = parseCookiesFn(req)[cookies().session] || "";
|
|
873
926
|
const session = await auth.getSession(token);
|
|
874
927
|
if (!session) {
|
|
875
928
|
const returnPath = req.originalUrl || "/oauth/authorize";
|
|
@@ -892,7 +945,7 @@ function registerOAuthRoutes(
|
|
|
892
945
|
res.status(503).send("Authentication is not configured");
|
|
893
946
|
return;
|
|
894
947
|
}
|
|
895
|
-
const token = parseCookiesFn(req)[
|
|
948
|
+
const token = parseCookiesFn(req)[cookies().session] || "";
|
|
896
949
|
auth
|
|
897
950
|
.getSession(token)
|
|
898
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
|
+
}
|
package/src/uiChannelPolicies.ts
CHANGED
|
@@ -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" },
|
|
@@ -28,6 +33,11 @@ export const UI_CHANNEL_EVENT_POLICY: EventPolicy = {
|
|
|
28
33
|
{ domain: "wrangler", name: "set_public_intro" },
|
|
29
34
|
{ domain: "wrangler", name: "send_event_on_subs" },
|
|
30
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" },
|
|
31
41
|
],
|
|
32
42
|
deny: [],
|
|
33
43
|
};
|
|
@@ -36,11 +46,16 @@ export const UI_CHANNEL_EVENT_POLICY: EventPolicy = {
|
|
|
36
46
|
export const UI_CHANNEL_QUERY_POLICY: QueryPolicy = {
|
|
37
47
|
allow: [
|
|
38
48
|
{ rid: "*", name: "__testing" },
|
|
49
|
+
{ rid: "*", name: "__consoleEval" },
|
|
50
|
+
{ rid: "io.picolabs.wrangler", name: "rulesetSchema" },
|
|
51
|
+
{ rid: "io.picolabs.wrangler", name: "picoSchema" },
|
|
39
52
|
{ rid: "io.picolabs.pico-engine-ui", name: "uiECI" },
|
|
40
53
|
{ rid: "io.picolabs.pico-engine-ui", name: "box" },
|
|
41
54
|
{ rid: "io.picolabs.pico-engine-ui", name: "pico" },
|
|
42
55
|
{ rid: "io.picolabs.pico-engine-ui", name: "logs" },
|
|
43
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" },
|
|
44
59
|
{ rid: "io.picolabs.pico-engine-ui", name: "name" },
|
|
45
60
|
{ rid: "io.picolabs.subscription", name: "established" },
|
|
46
61
|
{ rid: "io.picolabs.subscription", name: "inbound" },
|