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
@@ -6,6 +6,8 @@ import {
6
6
  type StoredPeerDidBundle,
7
7
  type StoredVeramoKey,
8
8
  } from "./peerDid";
9
+ import { isPeerDid4, resolvePeerDid4 } from "./peerDid4";
10
+ import { createPeerDid4WithKeys } from "./peerDid4Create";
9
11
  import {
10
12
  storedKeysFromWebvhKeys,
11
13
  webvhAgreementKeysForVeramo,
@@ -105,6 +107,40 @@ async function createAgentBundle(): Promise<AgentBundle> {
105
107
  const { PeerDIDProvider, getResolver: getPeerResolver } = require("@veramo/did-provider-peer");
106
108
  const { Resolver } = require("did-resolver");
107
109
 
110
+ const basePeerResolver = getPeerResolver();
111
+ const peerResolver = {
112
+ ...basePeerResolver,
113
+ peer: async (
114
+ did: string,
115
+ parsed: unknown,
116
+ resolver: unknown,
117
+ options: unknown
118
+ ) => {
119
+ if (isPeerDid4(did)) {
120
+ try {
121
+ const didDocument = resolvePeerDid4(did);
122
+ return {
123
+ didDocument,
124
+ didDocumentMetadata: {},
125
+ didResolutionMetadata: {
126
+ contentType: "application/did+ld+json",
127
+ },
128
+ };
129
+ } catch (err) {
130
+ return {
131
+ didDocument: null,
132
+ didDocumentMetadata: {},
133
+ didResolutionMetadata: {
134
+ error: "invalidDid",
135
+ message: (err as Error).message,
136
+ },
137
+ };
138
+ }
139
+ }
140
+ return basePeerResolver.peer(did, parsed, resolver, options);
141
+ },
142
+ };
143
+
108
144
  const privateKeyStore = new MemoryPrivateKeyStore();
109
145
  const agent = createAgent({
110
146
  plugins: [
@@ -120,7 +156,7 @@ async function createAgentBundle(): Promise<AgentBundle> {
120
156
  },
121
157
  }),
122
158
  new DIDResolverPlugin({
123
- resolver: new Resolver({ ...getPeerResolver(), ...webvhResolverMethods() }),
159
+ resolver: new Resolver({ ...peerResolver, ...webvhResolverMethods() }),
124
160
  }),
125
161
  new DIDComm(),
126
162
  ],
@@ -149,7 +185,7 @@ export async function exportVeramoKeysForDid(
149
185
  }
150
186
 
151
187
  /** Create a did:peer (num_algo 2) and export key material for persistence. */
152
- export async function createPeerDidWithKeys(): Promise<{
188
+ export async function createPeerDid2WithKeys(): Promise<{
153
189
  did: string;
154
190
  keys: StoredPeerDidBundle;
155
191
  }> {
@@ -165,6 +201,20 @@ export async function createPeerDidWithKeys(): Promise<{
165
201
  };
166
202
  }
167
203
 
204
+ /** Create a did:peer:4 (long form) and export key material for persistence. */
205
+ export async function createPeerDidWithKeys(): Promise<{
206
+ did: string;
207
+ keys: StoredPeerDidBundle;
208
+ }> {
209
+ const created = createPeerDid4WithKeys();
210
+ await getAgentBundle();
211
+ await importPeerDidOnAgent(created.did, created.keys.veramoKeys);
212
+ return {
213
+ did: created.did,
214
+ keys: created.keys,
215
+ };
216
+ }
217
+
168
218
  /** @deprecated Prefer createPeerDidWithKeys — returns DID only. */
169
219
  export async function createPeerDid(): Promise<string> {
170
220
  const { did } = await createPeerDidWithKeys();
@@ -131,9 +131,9 @@ const INGRESS_CHANNEL: ChannelConfig = {
131
131
  },
132
132
  };
133
133
 
134
- async function ensureDidcommIngress(
135
- pf: PicoFramework,
134
+ export async function ensureDidcommIngress(
136
135
  store: IdentityStore,
136
+ pf: PicoFramework,
137
137
  picoId: string,
138
138
  baseUrl: string
139
139
  ): Promise<string> {
@@ -151,7 +151,7 @@ async function ensureDidcommIngress(
151
151
  return eci;
152
152
  }
153
153
 
154
- function didcommServiceEndpoint(baseUrl: string, ingressEci: string): string {
154
+ export function didcommServiceEndpoint(baseUrl: string, ingressEci: string): string {
155
155
  const base = baseUrl.replace(/\/$/, "");
156
156
  return `${base}/sky/event/${ingressEci}/none/dido/didcomm_message`;
157
157
  }
@@ -176,7 +176,7 @@ export async function ensureWebvhDid(
176
176
  const webvh = await loadDidWebvhTs();
177
177
  const keyMaterial = await generateWebvhKeyMaterial();
178
178
  const crypto = await createWebvhCrypto(keyMaterial);
179
- const ingressEci = await ensureDidcommIngress(pf, store, picoId, baseUrl);
179
+ const ingressEci = await ensureDidcommIngress(store, pf, picoId, baseUrl);
180
180
 
181
181
  const result = await webvh.createDID({
182
182
  address: baseUrlToDidAddress(baseUrl),
@@ -0,0 +1,167 @@
1
+ /**
2
+ * did:webvh log update for portable move (Phase D, pico-move).
3
+ */
4
+
5
+ import { PicoFramework } from "pico-framework";
6
+ import { IdentityStore } from "./store";
7
+ import type { WebvhLogEntry } from "./types";
8
+ import { IdentityError } from "./errors";
9
+ import {
10
+ baseUrlToDidAddress,
11
+ didcommServiceEndpoint,
12
+ ensureDidcommIngress,
13
+ keysFromStore,
14
+ resolveLocalWebvhLog,
15
+ webvhPathsForPico,
16
+ } from "./webvh";
17
+ import {
18
+ createPassthroughVerifier,
19
+ createWebvhCrypto,
20
+ loadDidWebvhTs,
21
+ } from "./webvhCrypto";
22
+
23
+ export interface RelocateWebvhOptions {
24
+ /** Override engine base URL (default: running engine base_url). */
25
+ baseUrl?: string;
26
+ /** Override log path segments (default: picos/{picoId}). */
27
+ paths?: string[];
28
+ /** Mint a fresh DIDComm ingress channel (simulates post-import ECIs). */
29
+ renewIngress?: boolean;
30
+ }
31
+
32
+ export interface RelocateWebvhResult {
33
+ priorDid: string;
34
+ newDid: string;
35
+ scid: string;
36
+ didcommEndpoint: string;
37
+ }
38
+
39
+ function scidFromLog(log: WebvhLogEntry[]): string {
40
+ const params = log[0]?.parameters as { scid?: string } | undefined;
41
+ const scid = params?.scid;
42
+ if (typeof scid !== "string" || scid.length === 0) {
43
+ throw new IdentityError("webvh log missing SCID", 500);
44
+ }
45
+ return scid;
46
+ }
47
+
48
+ /** Append a portable log entry for a new host/path or DIDComm endpoint. */
49
+ export async function relocateWebvhDid(
50
+ store: IdentityStore,
51
+ pf: PicoFramework,
52
+ getBaseUrl: () => string,
53
+ picoId: string,
54
+ options: RelocateWebvhOptions = {}
55
+ ): Promise<RelocateWebvhResult> {
56
+ const priorDid = await store.getWebvhDid(picoId);
57
+ const log = await store.getWebvhLog(picoId);
58
+ const rawKeys = await store.getWebvhKeys(picoId);
59
+ if (!priorDid || !log?.length || !rawKeys) {
60
+ throw new IdentityError(`Pico ${picoId} has no did:webvh to relocate`, 404);
61
+ }
62
+
63
+ const keyMaterial = keysFromStore(rawKeys);
64
+ if (!keyMaterial) {
65
+ throw new IdentityError(`Pico ${picoId} webvh keys are invalid`, 500);
66
+ }
67
+
68
+ const baseUrl = options.baseUrl || getBaseUrl();
69
+ if (!baseUrl) {
70
+ throw new IdentityError("Engine base_url is required to relocate did:webvh", 500);
71
+ }
72
+
73
+ const priorScid = scidFromLog(log);
74
+ const crypto = await createWebvhCrypto(keyMaterial);
75
+ const verifier = await createPassthroughVerifier();
76
+
77
+ let ingressEci = await store.getDidcommIngressEci(picoId);
78
+ if (options.renewIngress || !ingressEci) {
79
+ if (options.renewIngress && ingressEci) {
80
+ await store.putDidcommIngressEci(picoId, "");
81
+ }
82
+ ingressEci = await ensureDidcommIngress(store, pf, picoId, baseUrl);
83
+ }
84
+
85
+ const webvh = await loadDidWebvhTs();
86
+ const updated = await webvh.updateDID({
87
+ log: log as any,
88
+ signer: crypto,
89
+ verifier,
90
+ updateKeys: [keyMaterial.updateKeyMultibase],
91
+ verificationMethods: [keyMaterial.verificationMethod],
92
+ address: baseUrlToDidAddress(baseUrl),
93
+ paths: options.paths || webvhPathsForPico(picoId),
94
+ services: [
95
+ {
96
+ id: "#didcomm",
97
+ type: "DIDCommMessaging",
98
+ serviceEndpoint: didcommServiceEndpoint(baseUrl, ingressEci),
99
+ accept: ["didcomm/v2"],
100
+ },
101
+ ],
102
+ });
103
+
104
+ const newDid = updated.did;
105
+ const newScid = updated.meta.scid;
106
+ if (newScid !== priorScid) {
107
+ throw new IdentityError(
108
+ `webvh SCID changed during relocation (${priorScid} → ${newScid})`,
109
+ 500
110
+ );
111
+ }
112
+
113
+ await store.putWebvhDid(picoId, newDid);
114
+ await store.putWebvhLog(picoId, updated.log as unknown as WebvhLogEntry[]);
115
+ if (updated.webDoc) {
116
+ await store.putWebvhWebDoc(picoId, updated.webDoc as Record<string, unknown>);
117
+ }
118
+ await store.putDidDoc(picoId, {
119
+ did: newDid,
120
+ doc: updated.doc as Record<string, unknown>,
121
+ cachedAt: new Date().toISOString(),
122
+ });
123
+
124
+ const endpoint = didcommServiceEndpoint(baseUrl, ingressEci);
125
+ return {
126
+ priorDid,
127
+ newDid,
128
+ scid: newScid,
129
+ didcommEndpoint: endpoint,
130
+ };
131
+ }
132
+
133
+ /** Resolve SCID from a stored log (for tests and verification). */
134
+ export async function webvhScidFromStore(
135
+ store: IdentityStore,
136
+ picoId: string
137
+ ): Promise<string | null> {
138
+ const log = await store.getWebvhLog(picoId);
139
+ if (!log?.length) {
140
+ return null;
141
+ }
142
+ try {
143
+ return scidFromLog(log);
144
+ } catch {
145
+ return null;
146
+ }
147
+ }
148
+
149
+ /** Verify local log resolves and SCID matches expectation. */
150
+ export async function assertWebvhScidUnchanged(
151
+ store: IdentityStore,
152
+ picoId: string,
153
+ expectedScid: string
154
+ ): Promise<void> {
155
+ const log = await store.getWebvhLog(picoId);
156
+ if (!log?.length) {
157
+ throw new IdentityError("Missing webvh log", 404);
158
+ }
159
+ const resolved = await resolveLocalWebvhLog(log);
160
+ const scid = scidFromLog(log);
161
+ if (scid !== expectedScid) {
162
+ throw new IdentityError(`SCID mismatch: ${scid} !== ${expectedScid}`, 500);
163
+ }
164
+ if (resolved.did !== (await store.getWebvhDid(picoId))) {
165
+ throw new IdentityError("Resolved DID does not match stored DID", 500);
166
+ }
167
+ }
package/src/index.ts CHANGED
@@ -1,22 +1,43 @@
1
1
  import { krl, KrlLogger, makeKrlLogger } from "krl-stdlib";
2
- import { ClassicLevel } from "classic-level";
3
2
  import * as _ from "lodash";
4
3
  import * as makeDir from "make-dir";
5
4
  import * as path from "path";
5
+ import type { Server } from "http";
6
6
  import { PicoEngineCore, RulesetRegistry } from "pico-engine-core";
7
- import { PicoDbKey, PicoFramework } from "pico-framework";
7
+ import {
8
+ EventPolicyRule,
9
+ PicoDbKey,
10
+ PicoFramework,
11
+ PolicyCeiling,
12
+ QueryPolicyRule,
13
+ } from "pico-framework";
8
14
  import { AuthService, WebAuthnAdapter, makeWebAuthnAdapter } from "./auth";
9
- import { IdentityStore, IdentityService, initDidoModule } from "./identity";
15
+ import {
16
+ IdentityStore,
17
+ IdentityService,
18
+ initDidoModule,
19
+ createSubtreePortableHooks,
20
+ } from "./identity";
10
21
  import { registerWebvhRoutes } from "./identity/webvhRoutes";
11
22
  import { OAuthService, initOAuthModule } from "./oauth";
12
- import { getPicoLogs, makeRotatingFileLogWriter } from "./logging";
23
+ import {
24
+ getPicoLogs,
25
+ makeEngineLogWriter,
26
+ resolveEngineLogFilePath,
27
+ } from "./logging";
28
+ import { openClassicLevelWithRecovery } from "./levelDb";
13
29
  import { provisionRoot, uiECIForRoot } from "./provisionRoot";
14
- import { RulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs";
30
+ import { createRulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs";
31
+ import { resolveBundledKrlUrl } from "./bundledKrl";
32
+ import { wranglerSubtreeImportServices } from "./subtreeWrangler";
15
33
  import { server } from "./server";
34
+ import {
35
+ installEngineRuntimeSettings,
36
+ resolveEngineSettings,
37
+ } from "./engineConfig";
16
38
  const charwise = require("charwise");
17
39
  const safeJsonCodec = require("level-json-coerce-null");
18
40
 
19
- const homeDir = require("home-dir");
20
41
  const version = require("../package.json").version;
21
42
 
22
43
  /**
@@ -94,6 +115,15 @@ export interface PicoEngineConfiguration {
94
115
  * Tests may set this to true.
95
116
  */
96
117
  autoCreateRootPico?: boolean;
118
+
119
+ /** Default max channel policy for root picos (1.8 policy ceiling). */
120
+ defaultRootPolicyCeiling?: PolicyCeiling;
121
+
122
+ /** Engine-wide event denies on every channel ECI (1.8). */
123
+ globalEventDeny?: EventPolicyRule[];
124
+
125
+ /** Engine-wide query denies on every channel ECI (1.8). */
126
+ globalQueryDeny?: QueryPolicyRule[];
97
127
  }
98
128
 
99
129
  export interface PicoEngine {
@@ -104,6 +134,7 @@ export interface PicoEngine {
104
134
  base_url: string;
105
135
 
106
136
  pf: PicoFramework;
137
+ core: PicoEngineCore;
107
138
  /** UI channel for the session root; null when no roots exist yet at boot. */
108
139
  uiECI: string | null;
109
140
  rsRegistry: RulesetRegistry;
@@ -111,64 +142,145 @@ export interface PicoEngine {
111
142
  oauth: OAuthService;
112
143
  identity: IdentityStore;
113
144
  identityService: IdentityService;
145
+
146
+ /** Stop HTTP and close LevelDB stores (for container SIGTERM). */
147
+ shutdown(): Promise<void>;
148
+ }
149
+
150
+ /** Integration harnesses (e.g. manifold-api Docker tests): boot with one root, no passkey. */
151
+ function autoCreateRootFromEnv(): boolean | undefined {
152
+ const v = process.env.PICO_ENGINE_AUTO_CREATE_ROOT;
153
+ if (typeof v !== "string" || v.trim().length === 0) {
154
+ return undefined;
155
+ }
156
+ const s = v.trim().toLowerCase();
157
+ if (s === "1" || s === "true" || s === "yes") {
158
+ return true;
159
+ }
160
+ if (s === "0" || s === "false" || s === "no") {
161
+ return false;
162
+ }
163
+ return undefined;
114
164
  }
115
165
 
116
166
  export async function startEngine(
117
167
  configuration: PicoEngineConfiguration = {}
118
168
  ): Promise<PicoEngine> {
119
- let home = configuration.home;
120
- let port = configuration.port;
121
- let base_url = configuration.base_url;
122
-
123
- if (typeof home !== "string") {
124
- const envHome = process.env.PICO_ENGINE_HOME;
125
- if (
126
- typeof envHome === "string" &&
127
- envHome.length > 0 &&
128
- process.env.NODE_ENV === "test"
129
- ) {
130
- home = envHome;
131
- } else {
132
- home = homeDir(".pico-engine") as string;
133
- }
169
+ const settings = resolveEngineSettings({
170
+ home: configuration.home,
171
+ port: configuration.port,
172
+ base_url: configuration.base_url,
173
+ rpID: configuration.rpID,
174
+ rpName: configuration.rpName,
175
+ origin: configuration.origin,
176
+ allowSelfSignup: configuration.allowSelfSignup,
177
+ defaultRootPolicyCeiling: configuration.defaultRootPolicyCeiling,
178
+ globalEventDeny: configuration.globalEventDeny,
179
+ globalQueryDeny: configuration.globalQueryDeny,
180
+ });
181
+ installEngineRuntimeSettings(settings);
182
+
183
+ const home = settings.home;
184
+ let port = configuration.port ?? settings.port;
185
+ const baseUrlFromCaller = configuration.base_url;
186
+ /** Filled after listen when using port 0 or when config baseUrl port does not match. */
187
+ let base_url: string | undefined = baseUrlFromCaller;
188
+ if (base_url == null && port > 0) {
189
+ base_url = settings.baseUrl;
134
190
  }
135
191
  await makeDir(home);
136
192
 
137
- const logFilePath = path.resolve(home, "pico-engine.log");
193
+ const logFilePath = resolveEngineLogFilePath(home);
138
194
  const log = configuration.log
139
195
  ? configuration.log
140
- : makeKrlLogger(makeRotatingFileLogWriter(logFilePath));
196
+ : makeKrlLogger(makeEngineLogWriter(logFilePath));
141
197
 
142
- const core = new PicoEngineCore({
143
- db: new ClassicLevel<PicoDbKey, any>(path.resolve(home, "db"), {
198
+ const db = await openClassicLevelWithRecovery<PicoDbKey, any>(
199
+ path.resolve(home, "db"),
200
+ {
144
201
  keyEncoding: charwise,
145
202
  valueEncoding: safeJsonCodec,
146
- }),
147
- rsRegLoader: RulesetRegistryLoaderFs(home),
203
+ },
204
+ log
205
+ );
206
+ const rulesetLoaderHandle = await createRulesetRegistryLoaderFs(home, log);
207
+
208
+ const core = new PicoEngineCore({
209
+ db,
210
+ rsRegLoader: rulesetLoaderHandle.loader,
148
211
  log,
149
212
  modules: configuration.modules,
150
213
  useEventInputTime: configuration.useEventInputTime,
151
- autoCreateRootPico: configuration.autoCreateRootPico ?? false,
214
+ autoCreateRootPico:
215
+ configuration.autoCreateRootPico ??
216
+ autoCreateRootFromEnv() ??
217
+ false,
218
+ defaultRootPolicyCeiling:
219
+ configuration.defaultRootPolicyCeiling ??
220
+ settings.defaultRootPolicyCeiling,
221
+ globalEventDeny: configuration.globalEventDeny ?? settings.globalEventDeny,
222
+ globalQueryDeny: configuration.globalQueryDeny ?? settings.globalQueryDeny,
152
223
  getPicoLogs(picoId) {
224
+ if (!logFilePath) {
225
+ return Promise.resolve([]);
226
+ }
153
227
  return getPicoLogs(logFilePath, picoId);
154
228
  },
155
229
  });
156
230
  const pf = core.picoFramework;
231
+ core.resolveBundledRulesetUrl = resolveBundledKrlUrl;
232
+ core.subtreeImportServices = wranglerSubtreeImportServices(core.rsRegistry);
157
233
  const identity = new IdentityStore({ db: pf.db });
158
234
  let identityService!: IdentityService;
235
+ const onPicoCreated = async (
236
+ picoId: string,
237
+ ctx: { isRoot: boolean; parentPicoId: string }
238
+ ) => {
239
+ if (core.base_url) {
240
+ await identityService.ensureWebvhDid(picoId, { isRoot: ctx.isRoot });
241
+ }
242
+ };
159
243
  identityService = new IdentityService({
160
244
  store: identity,
161
245
  getBaseUrl: () => core.base_url || base_url || "",
162
246
  pf,
247
+ rsRegistry: core.rsRegistry,
248
+ resolveBundledRulesetUrl: resolveBundledKrlUrl,
249
+ reloadImportedSchedules: (picoIds) => core.reloadImportedSchedules(picoIds),
250
+ firePicoImported: async (result) => {
251
+ const importedRoot = pf.getPico(result.newRootPicoId);
252
+ if (!importedRoot) {
253
+ return;
254
+ }
255
+ await importedRoot.event({
256
+ eci: result.newRootEci,
257
+ domain: "wrangler",
258
+ name: "pico_imported",
259
+ data: {
260
+ attrs: {
261
+ oldRootPicoId: result.oldRootPicoId,
262
+ newRootPicoId: result.newRootPicoId,
263
+ eciMap: result.eciMap,
264
+ picoIdMap: result.picoIdMap,
265
+ portableIdentity: true,
266
+ },
267
+ },
268
+ time: Date.now(),
269
+ });
270
+ },
163
271
  });
164
- core.onPicoCreated = async (picoId, ctx) => {
165
- if (core.base_url) {
166
- await identityService.ensureWebvhDid(picoId, { isRoot: ctx.isRoot });
167
- }
168
- };
272
+ core.onPicoCreated = onPicoCreated;
273
+ core.subtreePortableHooks = createSubtreePortableHooks(identity, pf);
169
274
  core.modules["dido"] = initDidoModule(identityService);
170
275
 
171
- await core.start();
276
+ try {
277
+ await core.start();
278
+ } catch (err) {
279
+ log.error("Engine startup failed", { error: err });
280
+ await rulesetLoaderHandle.close().catch(() => undefined);
281
+ await db.close().catch(() => undefined);
282
+ throw err;
283
+ }
172
284
  const rsRegistry = core.rsRegistry;
173
285
 
174
286
  // Fresh engines boot with zero roots. Adopt + (re)provision only when roots
@@ -187,7 +299,8 @@ export async function startEngine(
187
299
  rpID: configuration.rpID,
188
300
  rpName: configuration.rpName,
189
301
  origin: configuration.origin,
190
- allowSelfSignup: configuration.allowSelfSignup ?? false,
302
+ allowSelfSignup:
303
+ configuration.allowSelfSignup ?? settings.allowSelfSignup,
191
304
  flushBootstrapUrl: async (url) => {
192
305
  const flushed = await rsRegistry.flush(url);
193
306
  return { rid: flushed.rid };
@@ -233,10 +346,11 @@ export async function startEngine(
233
346
  if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
234
347
  port = process.env.NODE_ENV === "test" ? 0 : 3000;
235
348
  }
236
- await new Promise((resolve) => {
237
- const listener = app.listen(port, () => {
238
- if (listener) {
239
- const addr = listener.address();
349
+ let httpServer: Server | undefined;
350
+ await new Promise<void>((resolve) => {
351
+ httpServer = app.listen(port, () => {
352
+ if (httpServer) {
353
+ const addr = httpServer.address();
240
354
  if (addr && typeof addr !== "string" && _.isInteger(addr.port)) {
241
355
  // Get the actual port i.e. if they set port to 0 nodejs will assign you an available port
242
356
  port = addr.port;
@@ -247,6 +361,11 @@ export async function startEngine(
247
361
  });
248
362
  if (typeof base_url !== "string") {
249
363
  base_url = `http://localhost:${port}`;
364
+ } else if (
365
+ baseUrlFromCaller == null &&
366
+ (configuration.port === 0 || port !== settings.port)
367
+ ) {
368
+ base_url = `http://localhost:${port}`;
250
369
  }
251
370
  core.base_url = base_url;
252
371
 
@@ -266,6 +385,25 @@ export async function startEngine(
266
385
  });
267
386
  }
268
387
 
388
+ let shuttingDown = false;
389
+ async function shutdown() {
390
+ if (shuttingDown) {
391
+ return;
392
+ }
393
+ shuttingDown = true;
394
+ log.info("Shutting down pico-engine");
395
+ await new Promise<void>((resolve, reject) => {
396
+ if (!httpServer) {
397
+ resolve(undefined);
398
+ return;
399
+ }
400
+ httpServer.close((err) => (err ? reject(err) : resolve(undefined)));
401
+ });
402
+ await rulesetLoaderHandle.close();
403
+ await db.close();
404
+ log.info("Shutdown complete");
405
+ }
406
+
269
407
  return {
270
408
  version,
271
409
 
@@ -274,11 +412,13 @@ export async function startEngine(
274
412
  base_url,
275
413
 
276
414
  pf,
415
+ core,
277
416
  uiECI,
278
417
  rsRegistry,
279
418
  auth,
280
419
  oauth,
281
420
  identity,
282
421
  identityService,
422
+ shutdown,
283
423
  };
284
424
  }