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 @@ exports.webvhPathsForPico = webvhPathsForPico;
6
6
  exports.webvhHttpPath = webvhHttpPath;
7
7
  exports.webvhDidJsonPath = webvhDidJsonPath;
8
8
  exports.meshRootIdFor = meshRootIdFor;
9
+ exports.ensureDidcommIngress = ensureDidcommIngress;
10
+ exports.didcommServiceEndpoint = didcommServiceEndpoint;
9
11
  exports.ensureWebvhDid = ensureWebvhDid;
10
12
  exports.ensureWebvhForLoadedPicos = ensureWebvhForLoadedPicos;
11
13
  exports.formatDidLogAsJsonl = formatDidLogAsJsonl;
@@ -117,7 +119,7 @@ const INGRESS_CHANNEL = {
117
119
  deny: [{ rid: "*", name: "*" }],
118
120
  },
119
121
  };
120
- async function ensureDidcommIngress(pf, store, picoId, baseUrl) {
122
+ async function ensureDidcommIngress(store, pf, picoId, baseUrl) {
121
123
  const existing = await store.getDidcommIngressEci(picoId);
122
124
  if (existing) {
123
125
  return existing;
@@ -147,7 +149,7 @@ async function ensureWebvhDid(store, pf, getBaseUrl, picoId, options = {}) {
147
149
  const webvh = await (0, webvhCrypto_1.loadDidWebvhTs)();
148
150
  const keyMaterial = await (0, webvhCrypto_1.generateWebvhKeyMaterial)();
149
151
  const crypto = await (0, webvhCrypto_1.createWebvhCrypto)(keyMaterial);
150
- const ingressEci = await ensureDidcommIngress(pf, store, picoId, baseUrl);
152
+ const ingressEci = await ensureDidcommIngress(store, pf, picoId, baseUrl);
151
153
  const result = await webvh.createDID({
152
154
  address: baseUrlToDidAddress(baseUrl),
153
155
  paths: webvhPathsForPico(picoId),
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ /**
3
+ * did:webvh log update for portable move (Phase D, pico-move).
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.relocateWebvhDid = relocateWebvhDid;
7
+ exports.webvhScidFromStore = webvhScidFromStore;
8
+ exports.assertWebvhScidUnchanged = assertWebvhScidUnchanged;
9
+ const errors_1 = require("./errors");
10
+ const webvh_1 = require("./webvh");
11
+ const webvhCrypto_1 = require("./webvhCrypto");
12
+ function scidFromLog(log) {
13
+ var _a;
14
+ const params = (_a = log[0]) === null || _a === void 0 ? void 0 : _a.parameters;
15
+ const scid = params === null || params === void 0 ? void 0 : params.scid;
16
+ if (typeof scid !== "string" || scid.length === 0) {
17
+ throw new errors_1.IdentityError("webvh log missing SCID", 500);
18
+ }
19
+ return scid;
20
+ }
21
+ /** Append a portable log entry for a new host/path or DIDComm endpoint. */
22
+ async function relocateWebvhDid(store, pf, getBaseUrl, picoId, options = {}) {
23
+ const priorDid = await store.getWebvhDid(picoId);
24
+ const log = await store.getWebvhLog(picoId);
25
+ const rawKeys = await store.getWebvhKeys(picoId);
26
+ if (!priorDid || !(log === null || log === void 0 ? void 0 : log.length) || !rawKeys) {
27
+ throw new errors_1.IdentityError(`Pico ${picoId} has no did:webvh to relocate`, 404);
28
+ }
29
+ const keyMaterial = (0, webvh_1.keysFromStore)(rawKeys);
30
+ if (!keyMaterial) {
31
+ throw new errors_1.IdentityError(`Pico ${picoId} webvh keys are invalid`, 500);
32
+ }
33
+ const baseUrl = options.baseUrl || getBaseUrl();
34
+ if (!baseUrl) {
35
+ throw new errors_1.IdentityError("Engine base_url is required to relocate did:webvh", 500);
36
+ }
37
+ const priorScid = scidFromLog(log);
38
+ const crypto = await (0, webvhCrypto_1.createWebvhCrypto)(keyMaterial);
39
+ const verifier = await (0, webvhCrypto_1.createPassthroughVerifier)();
40
+ let ingressEci = await store.getDidcommIngressEci(picoId);
41
+ if (options.renewIngress || !ingressEci) {
42
+ if (options.renewIngress && ingressEci) {
43
+ await store.putDidcommIngressEci(picoId, "");
44
+ }
45
+ ingressEci = await (0, webvh_1.ensureDidcommIngress)(store, pf, picoId, baseUrl);
46
+ }
47
+ const webvh = await (0, webvhCrypto_1.loadDidWebvhTs)();
48
+ const updated = await webvh.updateDID({
49
+ log: log,
50
+ signer: crypto,
51
+ verifier,
52
+ updateKeys: [keyMaterial.updateKeyMultibase],
53
+ verificationMethods: [keyMaterial.verificationMethod],
54
+ address: (0, webvh_1.baseUrlToDidAddress)(baseUrl),
55
+ paths: options.paths || (0, webvh_1.webvhPathsForPico)(picoId),
56
+ services: [
57
+ {
58
+ id: "#didcomm",
59
+ type: "DIDCommMessaging",
60
+ serviceEndpoint: (0, webvh_1.didcommServiceEndpoint)(baseUrl, ingressEci),
61
+ accept: ["didcomm/v2"],
62
+ },
63
+ ],
64
+ });
65
+ const newDid = updated.did;
66
+ const newScid = updated.meta.scid;
67
+ if (newScid !== priorScid) {
68
+ throw new errors_1.IdentityError(`webvh SCID changed during relocation (${priorScid} → ${newScid})`, 500);
69
+ }
70
+ await store.putWebvhDid(picoId, newDid);
71
+ await store.putWebvhLog(picoId, updated.log);
72
+ if (updated.webDoc) {
73
+ await store.putWebvhWebDoc(picoId, updated.webDoc);
74
+ }
75
+ await store.putDidDoc(picoId, {
76
+ did: newDid,
77
+ doc: updated.doc,
78
+ cachedAt: new Date().toISOString(),
79
+ });
80
+ const endpoint = (0, webvh_1.didcommServiceEndpoint)(baseUrl, ingressEci);
81
+ return {
82
+ priorDid,
83
+ newDid,
84
+ scid: newScid,
85
+ didcommEndpoint: endpoint,
86
+ };
87
+ }
88
+ /** Resolve SCID from a stored log (for tests and verification). */
89
+ async function webvhScidFromStore(store, picoId) {
90
+ const log = await store.getWebvhLog(picoId);
91
+ if (!(log === null || log === void 0 ? void 0 : log.length)) {
92
+ return null;
93
+ }
94
+ try {
95
+ return scidFromLog(log);
96
+ }
97
+ catch (_a) {
98
+ return null;
99
+ }
100
+ }
101
+ /** Verify local log resolves and SCID matches expectation. */
102
+ async function assertWebvhScidUnchanged(store, picoId, expectedScid) {
103
+ const log = await store.getWebvhLog(picoId);
104
+ if (!(log === null || log === void 0 ? void 0 : log.length)) {
105
+ throw new errors_1.IdentityError("Missing webvh log", 404);
106
+ }
107
+ const resolved = await (0, webvh_1.resolveLocalWebvhLog)(log);
108
+ const scid = scidFromLog(log);
109
+ if (scid !== expectedScid) {
110
+ throw new errors_1.IdentityError(`SCID mismatch: ${scid} !== ${expectedScid}`, 500);
111
+ }
112
+ if (resolved.did !== (await store.getWebvhDid(picoId))) {
113
+ throw new errors_1.IdentityError("Resolved DID does not match stored DID", 500);
114
+ }
115
+ }
package/dist/index.js CHANGED
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.startEngine = startEngine;
4
4
  const krl_stdlib_1 = require("krl-stdlib");
5
- const classic_level_1 = require("classic-level");
6
5
  const _ = require("lodash");
7
6
  const makeDir = require("make-dir");
8
7
  const path = require("path");
@@ -11,63 +10,132 @@ const auth_1 = require("./auth");
11
10
  const identity_1 = require("./identity");
12
11
  const oauth_1 = require("./oauth");
13
12
  const logging_1 = require("./logging");
13
+ const levelDb_1 = require("./levelDb");
14
14
  const provisionRoot_1 = require("./provisionRoot");
15
15
  const RulesetRegistryLoaderFs_1 = require("./RulesetRegistryLoaderFs");
16
+ const bundledKrl_1 = require("./bundledKrl");
17
+ const subtreeWrangler_1 = require("./subtreeWrangler");
16
18
  const server_1 = require("./server");
19
+ const engineConfig_1 = require("./engineConfig");
17
20
  const charwise = require("charwise");
18
21
  const safeJsonCodec = require("level-json-coerce-null");
19
- const homeDir = require("home-dir");
20
22
  const version = require("../package.json").version;
23
+ /** Integration harnesses (e.g. manifold-api Docker tests): boot with one root, no passkey. */
24
+ function autoCreateRootFromEnv() {
25
+ const v = process.env.PICO_ENGINE_AUTO_CREATE_ROOT;
26
+ if (typeof v !== "string" || v.trim().length === 0) {
27
+ return undefined;
28
+ }
29
+ const s = v.trim().toLowerCase();
30
+ if (s === "1" || s === "true" || s === "yes") {
31
+ return true;
32
+ }
33
+ if (s === "0" || s === "false" || s === "no") {
34
+ return false;
35
+ }
36
+ return undefined;
37
+ }
21
38
  async function startEngine(configuration = {}) {
22
- var _a, _b;
23
- let home = configuration.home;
24
- let port = configuration.port;
25
- let base_url = configuration.base_url;
26
- if (typeof home !== "string") {
27
- const envHome = process.env.PICO_ENGINE_HOME;
28
- if (typeof envHome === "string" &&
29
- envHome.length > 0 &&
30
- process.env.NODE_ENV === "test") {
31
- home = envHome;
32
- }
33
- else {
34
- home = homeDir(".pico-engine");
35
- }
39
+ var _a, _b, _c, _d, _e, _f, _g;
40
+ const settings = (0, engineConfig_1.resolveEngineSettings)({
41
+ home: configuration.home,
42
+ port: configuration.port,
43
+ base_url: configuration.base_url,
44
+ rpID: configuration.rpID,
45
+ rpName: configuration.rpName,
46
+ origin: configuration.origin,
47
+ allowSelfSignup: configuration.allowSelfSignup,
48
+ defaultRootPolicyCeiling: configuration.defaultRootPolicyCeiling,
49
+ globalEventDeny: configuration.globalEventDeny,
50
+ globalQueryDeny: configuration.globalQueryDeny,
51
+ });
52
+ (0, engineConfig_1.installEngineRuntimeSettings)(settings);
53
+ const home = settings.home;
54
+ let port = (_a = configuration.port) !== null && _a !== void 0 ? _a : settings.port;
55
+ const baseUrlFromCaller = configuration.base_url;
56
+ /** Filled after listen when using port 0 or when config baseUrl port does not match. */
57
+ let base_url = baseUrlFromCaller;
58
+ if (base_url == null && port > 0) {
59
+ base_url = settings.baseUrl;
36
60
  }
37
61
  await makeDir(home);
38
- const logFilePath = path.resolve(home, "pico-engine.log");
62
+ const logFilePath = (0, logging_1.resolveEngineLogFilePath)(home);
39
63
  const log = configuration.log
40
64
  ? configuration.log
41
- : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeRotatingFileLogWriter)(logFilePath));
65
+ : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeEngineLogWriter)(logFilePath));
66
+ const db = await (0, levelDb_1.openClassicLevelWithRecovery)(path.resolve(home, "db"), {
67
+ keyEncoding: charwise,
68
+ valueEncoding: safeJsonCodec,
69
+ }, log);
70
+ const rulesetLoaderHandle = await (0, RulesetRegistryLoaderFs_1.createRulesetRegistryLoaderFs)(home, log);
42
71
  const core = new pico_engine_core_1.PicoEngineCore({
43
- db: new classic_level_1.ClassicLevel(path.resolve(home, "db"), {
44
- keyEncoding: charwise,
45
- valueEncoding: safeJsonCodec,
46
- }),
47
- rsRegLoader: (0, RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs)(home),
72
+ db,
73
+ rsRegLoader: rulesetLoaderHandle.loader,
48
74
  log,
49
75
  modules: configuration.modules,
50
76
  useEventInputTime: configuration.useEventInputTime,
51
- autoCreateRootPico: (_a = configuration.autoCreateRootPico) !== null && _a !== void 0 ? _a : false,
77
+ autoCreateRootPico: (_c = (_b = configuration.autoCreateRootPico) !== null && _b !== void 0 ? _b : autoCreateRootFromEnv()) !== null && _c !== void 0 ? _c : false,
78
+ defaultRootPolicyCeiling: (_d = configuration.defaultRootPolicyCeiling) !== null && _d !== void 0 ? _d : settings.defaultRootPolicyCeiling,
79
+ globalEventDeny: (_e = configuration.globalEventDeny) !== null && _e !== void 0 ? _e : settings.globalEventDeny,
80
+ globalQueryDeny: (_f = configuration.globalQueryDeny) !== null && _f !== void 0 ? _f : settings.globalQueryDeny,
52
81
  getPicoLogs(picoId) {
82
+ if (!logFilePath) {
83
+ return Promise.resolve([]);
84
+ }
53
85
  return (0, logging_1.getPicoLogs)(logFilePath, picoId);
54
86
  },
55
87
  });
56
88
  const pf = core.picoFramework;
89
+ core.resolveBundledRulesetUrl = bundledKrl_1.resolveBundledKrlUrl;
90
+ core.subtreeImportServices = (0, subtreeWrangler_1.wranglerSubtreeImportServices)(core.rsRegistry);
57
91
  const identity = new identity_1.IdentityStore({ db: pf.db });
58
92
  let identityService;
93
+ const onPicoCreated = async (picoId, ctx) => {
94
+ if (core.base_url) {
95
+ await identityService.ensureWebvhDid(picoId, { isRoot: ctx.isRoot });
96
+ }
97
+ };
59
98
  identityService = new identity_1.IdentityService({
60
99
  store: identity,
61
100
  getBaseUrl: () => core.base_url || base_url || "",
62
101
  pf,
102
+ rsRegistry: core.rsRegistry,
103
+ resolveBundledRulesetUrl: bundledKrl_1.resolveBundledKrlUrl,
104
+ reloadImportedSchedules: (picoIds) => core.reloadImportedSchedules(picoIds),
105
+ firePicoImported: async (result) => {
106
+ const importedRoot = pf.getPico(result.newRootPicoId);
107
+ if (!importedRoot) {
108
+ return;
109
+ }
110
+ await importedRoot.event({
111
+ eci: result.newRootEci,
112
+ domain: "wrangler",
113
+ name: "pico_imported",
114
+ data: {
115
+ attrs: {
116
+ oldRootPicoId: result.oldRootPicoId,
117
+ newRootPicoId: result.newRootPicoId,
118
+ eciMap: result.eciMap,
119
+ picoIdMap: result.picoIdMap,
120
+ portableIdentity: true,
121
+ },
122
+ },
123
+ time: Date.now(),
124
+ });
125
+ },
63
126
  });
64
- core.onPicoCreated = async (picoId, ctx) => {
65
- if (core.base_url) {
66
- await identityService.ensureWebvhDid(picoId, { isRoot: ctx.isRoot });
67
- }
68
- };
127
+ core.onPicoCreated = onPicoCreated;
128
+ core.subtreePortableHooks = (0, identity_1.createSubtreePortableHooks)(identity, pf);
69
129
  core.modules["dido"] = (0, identity_1.initDidoModule)(identityService);
70
- await core.start();
130
+ try {
131
+ await core.start();
132
+ }
133
+ catch (err) {
134
+ log.error("Engine startup failed", { error: err });
135
+ await rulesetLoaderHandle.close().catch(() => undefined);
136
+ await db.close().catch(() => undefined);
137
+ throw err;
138
+ }
71
139
  const rsRegistry = core.rsRegistry;
72
140
  // Fresh engines boot with zero roots. Adopt + (re)provision only when roots
73
141
  // already exist (test harness with autoCreateRootPico, or a migrated DB).
@@ -84,7 +152,7 @@ async function startEngine(configuration = {}) {
84
152
  rpID: configuration.rpID,
85
153
  rpName: configuration.rpName,
86
154
  origin: configuration.origin,
87
- allowSelfSignup: (_b = configuration.allowSelfSignup) !== null && _b !== void 0 ? _b : false,
155
+ allowSelfSignup: (_g = configuration.allowSelfSignup) !== null && _g !== void 0 ? _g : settings.allowSelfSignup,
88
156
  flushBootstrapUrl: async (url) => {
89
157
  const flushed = await rsRegistry.flush(url);
90
158
  return { rid: flushed.rid };
@@ -128,10 +196,11 @@ async function startEngine(configuration = {}) {
128
196
  if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
129
197
  port = process.env.NODE_ENV === "test" ? 0 : 3000;
130
198
  }
199
+ let httpServer;
131
200
  await new Promise((resolve) => {
132
- const listener = app.listen(port, () => {
133
- if (listener) {
134
- const addr = listener.address();
201
+ httpServer = app.listen(port, () => {
202
+ if (httpServer) {
203
+ const addr = httpServer.address();
135
204
  if (addr && typeof addr !== "string" && _.isInteger(addr.port)) {
136
205
  // Get the actual port i.e. if they set port to 0 nodejs will assign you an available port
137
206
  port = addr.port;
@@ -143,6 +212,10 @@ async function startEngine(configuration = {}) {
143
212
  if (typeof base_url !== "string") {
144
213
  base_url = `http://localhost:${port}`;
145
214
  }
215
+ else if (baseUrlFromCaller == null &&
216
+ (configuration.port === 0 || port !== settings.port)) {
217
+ base_url = `http://localhost:${port}`;
218
+ }
146
219
  core.base_url = base_url;
147
220
  await identityService.ensureWebvhForAllLoadedPicos();
148
221
  log.info(`Listening at ${base_url}`);
@@ -157,17 +230,37 @@ async function startEngine(configuration = {}) {
157
230
  log.error("Error signaling engine:started event", { error });
158
231
  });
159
232
  }
233
+ let shuttingDown = false;
234
+ async function shutdown() {
235
+ if (shuttingDown) {
236
+ return;
237
+ }
238
+ shuttingDown = true;
239
+ log.info("Shutting down pico-engine");
240
+ await new Promise((resolve, reject) => {
241
+ if (!httpServer) {
242
+ resolve(undefined);
243
+ return;
244
+ }
245
+ httpServer.close((err) => (err ? reject(err) : resolve(undefined)));
246
+ });
247
+ await rulesetLoaderHandle.close();
248
+ await db.close();
249
+ log.info("Shutdown complete");
250
+ }
160
251
  return {
161
252
  version,
162
253
  home,
163
254
  port,
164
255
  base_url,
165
256
  pf,
257
+ core,
166
258
  uiECI,
167
259
  rsRegistry,
168
260
  auth,
169
261
  oauth,
170
262
  identity,
171
263
  identityService,
264
+ shutdown,
172
265
  };
173
266
  }
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isLevelDbOpenError = isLevelDbOpenError;
4
+ exports.openClassicLevelWithRecovery = openClassicLevelWithRecovery;
5
+ const classic_level_1 = require("classic-level");
6
+ const fs = require("fs/promises");
7
+ const makeDir = require("make-dir");
8
+ const path = require("path");
9
+ function errCode(err) {
10
+ if (err && typeof err === "object" && "code" in err) {
11
+ const code = err.code;
12
+ return typeof code === "string" ? code : undefined;
13
+ }
14
+ return undefined;
15
+ }
16
+ function errMessage(err) {
17
+ if (err instanceof Error) {
18
+ return err.message;
19
+ }
20
+ return String(err);
21
+ }
22
+ function isLevelDbOpenError(err) {
23
+ const code = errCode(err);
24
+ if (code === "LEVEL_LOCKED") {
25
+ return true;
26
+ }
27
+ const message = errMessage(err);
28
+ return /LOCK|corrupt|IO error|database/i.test(message);
29
+ }
30
+ async function removeStaleLockFile(location, log) {
31
+ const lockPath = path.join(location, "LOCK");
32
+ try {
33
+ await fs.unlink(lockPath);
34
+ log === null || log === void 0 ? void 0 : log.info("Removed stale LevelDB LOCK file", { location });
35
+ }
36
+ catch (err) {
37
+ if ((err === null || err === void 0 ? void 0 : err.code) !== "ENOENT") {
38
+ throw err;
39
+ }
40
+ }
41
+ }
42
+ async function closeClassicLevelQuietly(db) {
43
+ try {
44
+ await db.close();
45
+ }
46
+ catch (_a) {
47
+ // May never have opened.
48
+ }
49
+ }
50
+ async function openOnce(location, options) {
51
+ const db = new classic_level_1.ClassicLevel(location, options);
52
+ try {
53
+ await db.open();
54
+ return db;
55
+ }
56
+ catch (err) {
57
+ // A failed open can still leave a handle; if it is GC'd later it closes
58
+ // the underlying store and breaks a subsequent successful open on the same path.
59
+ await closeClassicLevelQuietly(db);
60
+ throw err;
61
+ }
62
+ }
63
+ /**
64
+ * Open a ClassicLevel database, recovering from stale locks or minor corruption
65
+ * left by an unclean shutdown (e.g. container SIGKILL).
66
+ *
67
+ * Recovery order (non-destructive first):
68
+ * 1. Normal open
69
+ * 2. Remove stale LOCK and retry
70
+ * 3. LevelDB repair and retry
71
+ */
72
+ async function openClassicLevelWithRecovery(location, options, log) {
73
+ await makeDir(location);
74
+ try {
75
+ return await openOnce(location, options);
76
+ }
77
+ catch (firstErr) {
78
+ if (!isLevelDbOpenError(firstErr)) {
79
+ throw firstErr;
80
+ }
81
+ log === null || log === void 0 ? void 0 : log.warn("LevelDB open failed; attempting recovery", {
82
+ location,
83
+ error: firstErr,
84
+ });
85
+ }
86
+ await removeStaleLockFile(location, log);
87
+ try {
88
+ return await openOnce(location, options);
89
+ }
90
+ catch (secondErr) {
91
+ if (!isLevelDbOpenError(secondErr)) {
92
+ throw secondErr;
93
+ }
94
+ log === null || log === void 0 ? void 0 : log.warn("LevelDB open failed after lock cleanup; running repair", {
95
+ location,
96
+ error: secondErr,
97
+ });
98
+ }
99
+ await classic_level_1.ClassicLevel.repair(location);
100
+ log === null || log === void 0 ? void 0 : log.info("LevelDB repair finished", { location });
101
+ return openOnce(location, options);
102
+ }
package/dist/logging.js CHANGED
@@ -7,29 +7,63 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
7
7
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
8
  };
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.resolveEngineLogFilePath = resolveEngineLogFilePath;
11
+ exports.makeEngineLogWriter = makeEngineLogWriter;
10
12
  exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
11
13
  exports.getPicoLogs = getPicoLogs;
12
14
  const fs = require("fs");
13
15
  const readline = require("readline");
14
16
  const krl_stdlib_1 = require("krl-stdlib");
15
17
  const path = require("path");
18
+ const engineConfig_1 = require("./engineConfig");
16
19
  const rfs = require("rotating-file-stream");
17
20
  const logStreams = {};
21
+ function stdoutOnlyLogWriter() {
22
+ const isTest = process.env.NODE_ENV === "test";
23
+ return (line) => {
24
+ if (!isTest) {
25
+ process.stdout.write(line);
26
+ }
27
+ };
28
+ }
29
+ function attachSafeLogStreamHandlers(fileStream, filePath) {
30
+ fileStream.on("error", (err) => {
31
+ process.stderr.write(`pico-engine log file write failed (${filePath}): ${err.message}\n`);
32
+ });
33
+ }
18
34
  function getRotatingFileStream(filePath) {
19
35
  if (!logStreams[filePath]) {
20
36
  const filename = path.basename(filePath);
21
- logStreams[filePath] = rfs((time, index) => {
37
+ const fileStream = rfs((time, index) => {
22
38
  if (!time)
23
39
  return filename;
24
40
  return filename + "." + index;
25
41
  }, {
26
42
  path: path.dirname(filePath),
27
- size: "100M", // rotate every 10 MegaBytes written
43
+ size: "100M", // rotate every 100 MegaBytes written
28
44
  maxFiles: 12,
29
45
  });
46
+ attachSafeLogStreamHandlers(fileStream, filePath);
47
+ logStreams[filePath] = fileStream;
30
48
  }
31
49
  return logStreams[filePath];
32
50
  }
51
+ function resolveEngineLogFilePath(home) {
52
+ const settings = (0, engineConfig_1.getEngineRuntimeSettings)();
53
+ if (settings.logToStdout) {
54
+ return null;
55
+ }
56
+ if (settings.logFile) {
57
+ return settings.logFile;
58
+ }
59
+ return path.resolve(home, "pico-engine.log");
60
+ }
61
+ function makeEngineLogWriter(filePath) {
62
+ if (!filePath) {
63
+ return stdoutOnlyLogWriter();
64
+ }
65
+ return makeRotatingFileLogWriter(filePath);
66
+ }
33
67
  function makeRotatingFileLogWriter(filePath) {
34
68
  const fileStream = getRotatingFileStream(filePath);
35
69
  const isTest = process.env.NODE_ENV === "test";
@@ -37,13 +71,20 @@ function makeRotatingFileLogWriter(filePath) {
37
71
  if (!isTest) {
38
72
  process.stdout.write(line);
39
73
  }
40
- fileStream.write(line);
74
+ fileStream.write(line, (err) => {
75
+ if (err) {
76
+ process.stderr.write(`pico-engine log file write failed (${filePath}): ${err.message}\n`);
77
+ }
78
+ });
41
79
  }
42
80
  return write;
43
81
  }
44
82
  async function getPicoLogs(filePath, picoId) {
45
83
  var _a, e_1, _b, _c;
46
84
  const output = [];
85
+ if (!(await fs.promises.stat(filePath).catch(() => null))) {
86
+ return output;
87
+ }
47
88
  const rl = readline.createInterface({
48
89
  input: fs.createReadStream(filePath),
49
90
  });
@@ -52,10 +93,8 @@ async function getPicoLogs(filePath, picoId) {
52
93
  _c = rl_1_1.value;
53
94
  _d = false;
54
95
  const line = _c;
55
- // Each line in the readline input will be successively available here as
56
- // `line`.
57
96
  if (line.indexOf(picoId) < 0) {
58
- continue; // not my pico
97
+ continue;
59
98
  }
60
99
  let entry;
61
100
  try {
@@ -63,11 +102,11 @@ async function getPicoLogs(filePath, picoId) {
63
102
  }
64
103
  catch (err) { }
65
104
  if (!entry || entry.picoId !== picoId) {
66
- continue; // not my pico
105
+ continue;
67
106
  }
68
107
  const time = new Date(entry.time);
69
108
  if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
70
- continue; // too old
109
+ continue;
71
110
  }
72
111
  const out = Object.assign(Object.assign({}, entry), { level: krl_stdlib_1.krlLogLevelCodeToHuman[entry.level] || `${entry.level}`, time: entry.time, txnId: entry.txnId });
73
112
  delete out.picoId;
@@ -13,6 +13,7 @@ const acg = require("./acg");
13
13
  const channelEligibility_1 = require("./channelEligibility");
14
14
  const errors_1 = require("./errors");
15
15
  const meshOAuth_1 = require("./meshOAuth");
16
+ const channelIdentityPolicy_1 = require("../channelIdentityPolicy");
16
17
  const OAUTH_CHANNEL_PREFIX = "oauth-channel";
17
18
  const OAUTH_TOKEN_PREFIX = "oauth-token";
18
19
  const DEFAULT_TOKEN_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours
@@ -170,17 +171,23 @@ class OAuthService {
170
171
  async skyRequiresBearer(eci) {
171
172
  try {
172
173
  const channel = this.lookupChannelReadOnly(eci);
173
- if (channel.tags.includes("didcomm") &&
174
- channel.tags.includes("ingress")) {
174
+ if ((0, channelIdentityPolicy_1.isDidcommIngressChannel)(channel)) {
175
+ return false;
176
+ }
177
+ if ((0, channelIdentityPolicy_1.wildcardSkyRequireBearerEnabled)() &&
178
+ (0, channelIdentityPolicy_1.channelHasUnrestrictedAllow)(channel)) {
179
+ return true;
180
+ }
181
+ if ((0, channelEligibility_1.isOAuthEligibleChannel)(channel)) {
182
+ return true;
183
+ }
184
+ if ((0, channelEligibility_1.isMeshOAuthExemptChannel)(channel)) {
175
185
  return false;
176
186
  }
177
187
  }
178
188
  catch (_e) {
179
189
  // channel not found — fall through
180
190
  }
181
- if (await this.channelRequiresBearer(eci)) {
182
- return true;
183
- }
184
191
  return this.meshRequiresOAuth(eci);
185
192
  }
186
193
  async validateBearerToken(token, channelEci) {
@@ -1,9 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OAUTH_WEBHOOK_TAG = void 0;
3
+ exports.MESH_OAUTH_EXEMPT_TAG = exports.OAUTH_WEBHOOK_TAG = void 0;
4
4
  exports.isSubscriptionChannel = isSubscriptionChannel;
5
5
  exports.isOAuthEligibleChannel = isOAuthEligibleChannel;
6
+ exports.isMeshOAuthExemptChannel = isMeshOAuthExemptChannel;
6
7
  exports.OAUTH_WEBHOOK_TAG = "oauth-webhook";
8
+ /** Opt out of mesh-wide /sky/* OAuth when the root has io.picolabs.oauth installed. */
9
+ exports.MESH_OAUTH_EXEMPT_TAG = "mesh-oauth-exempt";
7
10
  const SUBSCRIPTION_CHANNEL_TAGS = new Set([
8
11
  "wellknown_rx",
9
12
  "tx_rx",
@@ -24,3 +27,6 @@ function isOAuthEligibleChannel(channel) {
24
27
  }
25
28
  return channel.tags.includes(exports.OAUTH_WEBHOOK_TAG);
26
29
  }
30
+ function isMeshOAuthExemptChannel(channel) {
31
+ return channel.tags.some((tag) => tag.toLowerCase() === exports.MESH_OAUTH_EXEMPT_TAG);
32
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initOAuthModule = exports.isChannelUnderRoot = exports.rootPicoIdForChannelEci = exports.rootHasOAuthMeshRuleset = exports.meshRequiresOAuth = exports.OAUTH_MESH_RULESET_RID = exports.isSubscriptionChannel = exports.isOAuthEligibleChannel = exports.OAUTH_WEBHOOK_TAG = exports.renderConsentHtml = exports.parseAuthorizeQuery = exports.parseApproveBody = exports.OAuthError = exports.OAuthService = void 0;
3
+ exports.initOAuthModule = exports.isChannelUnderRoot = exports.rootPicoIdForChannelEci = exports.rootHasOAuthMeshRuleset = exports.meshRequiresOAuth = exports.OAUTH_MESH_RULESET_RID = exports.isSubscriptionChannel = exports.isMeshOAuthExemptChannel = exports.isOAuthEligibleChannel = exports.MESH_OAUTH_EXEMPT_TAG = exports.OAUTH_WEBHOOK_TAG = exports.renderConsentHtml = exports.parseAuthorizeQuery = exports.parseApproveBody = exports.OAuthError = exports.OAuthService = void 0;
4
4
  var OAuthService_1 = require("./OAuthService");
5
5
  Object.defineProperty(exports, "OAuthService", { enumerable: true, get: function () { return OAuthService_1.OAuthService; } });
6
6
  Object.defineProperty(exports, "OAuthError", { enumerable: true, get: function () { return OAuthService_1.OAuthError; } });
@@ -9,7 +9,9 @@ Object.defineProperty(exports, "parseAuthorizeQuery", { enumerable: true, get: f
9
9
  Object.defineProperty(exports, "renderConsentHtml", { enumerable: true, get: function () { return OAuthService_1.renderConsentHtml; } });
10
10
  var channelEligibility_1 = require("./channelEligibility");
11
11
  Object.defineProperty(exports, "OAUTH_WEBHOOK_TAG", { enumerable: true, get: function () { return channelEligibility_1.OAUTH_WEBHOOK_TAG; } });
12
+ Object.defineProperty(exports, "MESH_OAUTH_EXEMPT_TAG", { enumerable: true, get: function () { return channelEligibility_1.MESH_OAUTH_EXEMPT_TAG; } });
12
13
  Object.defineProperty(exports, "isOAuthEligibleChannel", { enumerable: true, get: function () { return channelEligibility_1.isOAuthEligibleChannel; } });
14
+ Object.defineProperty(exports, "isMeshOAuthExemptChannel", { enumerable: true, get: function () { return channelEligibility_1.isMeshOAuthExemptChannel; } });
13
15
  Object.defineProperty(exports, "isSubscriptionChannel", { enumerable: true, get: function () { return channelEligibility_1.isSubscriptionChannel; } });
14
16
  var meshOAuth_1 = require("./meshOAuth");
15
17
  Object.defineProperty(exports, "OAUTH_MESH_RULESET_RID", { enumerable: true, get: function () { return meshOAuth_1.OAUTH_MESH_RULESET_RID; } });