pico-engine 1.6.4 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +16 -9
  2. package/dist/RulesetRegistryLoaderFs.js +60 -6
  3. package/dist/auth/cookies.js +32 -0
  4. package/dist/auth/index.js +6 -1
  5. package/dist/channelIdentityPolicy.js +22 -0
  6. package/dist/cli.js +106 -69
  7. package/dist/engineConfig.js +291 -0
  8. package/dist/identity/IdentityService.js +90 -0
  9. package/dist/identity/crossPico.js +24 -8
  10. package/dist/identity/didoModule.js +80 -18
  11. package/dist/identity/establishSubscription.js +2 -2
  12. package/dist/identity/identityPortable.js +92 -0
  13. package/dist/identity/index.js +10 -1
  14. package/dist/identity/localPeerReconcile.js +65 -0
  15. package/dist/identity/peerDid4.js +175 -0
  16. package/dist/identity/peerDid4Create.js +90 -0
  17. package/dist/identity/portableExportPrepare.js +196 -0
  18. package/dist/identity/portableMove.js +87 -0
  19. package/dist/identity/skyIntro.js +6 -3
  20. package/dist/identity/skyProtocol.js +26 -1
  21. package/dist/identity/skyRotation.js +121 -0
  22. package/dist/identity/store.js +5 -1
  23. package/dist/identity/subscriptionEntSync.js +72 -0
  24. package/dist/identity/veramoDidComm.js +41 -2
  25. package/dist/identity/webvh.js +4 -2
  26. package/dist/identity/webvhRelocation.js +115 -0
  27. package/dist/index.js +84 -27
  28. package/dist/logging.js +156 -16
  29. package/dist/oauth/OAuthService.js +6 -2
  30. package/dist/policyConfigFromJson.js +112 -0
  31. package/dist/runMcp.js +27 -0
  32. package/dist/server.js +261 -24
  33. package/dist/stageKrl.js +26 -0
  34. package/dist/subtreeWrangler.js +180 -0
  35. package/dist/uiChannelPolicies.js +15 -0
  36. package/dist/xmluiRuntime.js +68 -0
  37. package/krl/io.picolabs.oauth.krl +41 -2
  38. package/krl/io.picolabs.pds.krl +131 -10
  39. package/krl/io.picolabs.pico-engine-ui.krl +217 -94
  40. package/krl/io.picolabs.portable_subtree.krl +97 -0
  41. package/krl/io.picolabs.subscription.krl +180 -72
  42. package/krl/io.picolabs.wrangler.krl +313 -57
  43. package/krl/io.picolabs.xmlui.krl +295 -0
  44. package/mcp/devResources.js +118 -0
  45. package/mcp/devTools.js +351 -0
  46. package/mcp/engineClient.js +133 -0
  47. package/mcp/meshHttp.js +137 -0
  48. package/mcp/meshTools.js +189 -0
  49. package/mcp/runStdio.js +34 -0
  50. package/mcp/schemaToZod.js +56 -0
  51. package/package.json +24 -9
  52. package/public/.DS_Store +0 -0
  53. package/public/docs/_sidebar.md +3 -1
  54. package/public/docs/llms-full.txt +630 -0
  55. package/public/docs/llms.txt +38 -0
  56. package/public/docs/openapi.yaml +78 -0
  57. package/public/docs/pico-engine.md +239 -4
  58. package/public/docs/pico-overview.md +117 -0
  59. package/public/docs/pico-views-xmlui.md +5 -0
  60. package/public/docs/pico-views.md +140 -0
  61. package/public/docs/pico.md +3 -1
  62. package/public/docs/rulesets/README.md +26 -0
  63. package/public/docs/rulesets/io.picolabs.oauth.md +71 -0
  64. package/public/docs/rulesets/io.picolabs.pds.md +327 -0
  65. package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +65 -0
  66. package/public/docs/rulesets/io.picolabs.portable_subtree.md +94 -0
  67. package/public/docs/rulesets/io.picolabs.subscription.md +398 -0
  68. package/public/docs/rulesets/io.picolabs.wrangler.md +471 -0
  69. package/public/pico-engine-ui.css +1 -1
  70. package/public/pico-engine-ui.js +27 -23
  71. package/public/xmlui/0.12.7.js +419 -0
  72. package/public/xmlui/pico-view-boot.js +153 -0
  73. package/src/RulesetRegistryLoaderFs.ts +66 -7
  74. package/src/auth/cookies.ts +34 -0
  75. package/src/auth/index.ts +7 -0
  76. package/src/channelIdentityPolicy.ts +29 -0
  77. package/src/cli.ts +70 -37
  78. package/src/engineConfig.ts +363 -0
  79. package/src/identity/IdentityService.ts +189 -1
  80. package/src/identity/crossPico.ts +57 -8
  81. package/src/identity/didoModule.ts +140 -23
  82. package/src/identity/establishSubscription.ts +2 -2
  83. package/src/identity/identityPortable.ts +153 -0
  84. package/src/identity/index.ts +14 -0
  85. package/src/identity/localPeerReconcile.ts +97 -0
  86. package/src/identity/peerDid4.ts +197 -0
  87. package/src/identity/peerDid4Create.ts +121 -0
  88. package/src/identity/portableExportPrepare.ts +302 -0
  89. package/src/identity/portableMove.ts +193 -0
  90. package/src/identity/skyIntro.ts +11 -11
  91. package/src/identity/skyProtocol.ts +46 -0
  92. package/src/identity/skyRotation.ts +211 -0
  93. package/src/identity/store.ts +5 -1
  94. package/src/identity/subscriptionEntSync.ts +97 -0
  95. package/src/identity/veramoDidComm.ts +52 -2
  96. package/src/identity/webvh.ts +4 -4
  97. package/src/identity/webvhRelocation.ts +167 -0
  98. package/src/index.ts +123 -31
  99. package/src/logging.ts +173 -18
  100. package/src/oauth/OAuthService.ts +11 -3
  101. package/src/policyConfigFromJson.ts +131 -0
  102. package/src/runMcp.ts +30 -0
  103. package/src/server.ts +328 -27
  104. package/src/stageKrl.ts +26 -0
  105. package/src/subtreeWrangler.ts +278 -0
  106. package/src/uiChannelPolicies.ts +15 -0
  107. package/src/xmluiRuntime.ts +66 -0
  108. package/krl/io.picolabs.did-o.krl +0 -349
@@ -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
@@ -4,24 +4,41 @@ import * as makeDir from "make-dir";
4
4
  import * as path from "path";
5
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
23
  import {
13
- getPicoLogs,
24
+ picoLogEntries,
14
25
  makeEngineLogWriter,
15
26
  resolveEngineLogFilePath,
27
+ resolvePicoLogsReadPath,
16
28
  } from "./logging";
17
29
  import { openClassicLevelWithRecovery } from "./levelDb";
18
30
  import { provisionRoot, uiECIForRoot } from "./provisionRoot";
19
31
  import { createRulesetRegistryLoaderFs } from "./RulesetRegistryLoaderFs";
32
+ import { resolveBundledKrlUrl } from "./bundledKrl";
33
+ import { wranglerSubtreeImportServices } from "./subtreeWrangler";
20
34
  import { server } from "./server";
35
+ import {
36
+ installEngineRuntimeSettings,
37
+ resolveEngineSettings,
38
+ } from "./engineConfig";
21
39
  const charwise = require("charwise");
22
40
  const safeJsonCodec = require("level-json-coerce-null");
23
41
 
24
- const homeDir = require("home-dir");
25
42
  const version = require("../package.json").version;
26
43
 
27
44
  /**
@@ -99,6 +116,15 @@ export interface PicoEngineConfiguration {
99
116
  * Tests may set this to true.
100
117
  */
101
118
  autoCreateRootPico?: boolean;
119
+
120
+ /** Default max channel policy for root picos (1.8 policy ceiling). */
121
+ defaultRootPolicyCeiling?: PolicyCeiling;
122
+
123
+ /** Engine-wide event denies on every channel ECI (1.8). */
124
+ globalEventDeny?: EventPolicyRule[];
125
+
126
+ /** Engine-wide query denies on every channel ECI (1.8). */
127
+ globalQueryDeny?: QueryPolicyRule[];
102
128
  }
103
129
 
104
130
  export interface PicoEngine {
@@ -109,6 +135,7 @@ export interface PicoEngine {
109
135
  base_url: string;
110
136
 
111
137
  pf: PicoFramework;
138
+ core: PicoEngineCore;
112
139
  /** UI channel for the session root; null when no roots exist yet at boot. */
113
140
  uiECI: string | null;
114
141
  rsRegistry: RulesetRegistry;
@@ -121,31 +148,53 @@ export interface PicoEngine {
121
148
  shutdown(): Promise<void>;
122
149
  }
123
150
 
151
+ /** Integration harnesses (e.g. manifold-api Docker tests): boot with one root, no passkey. */
152
+ function autoCreateRootFromEnv(): boolean | undefined {
153
+ const v = process.env.PICO_ENGINE_AUTO_CREATE_ROOT;
154
+ if (typeof v !== "string" || v.trim().length === 0) {
155
+ return undefined;
156
+ }
157
+ const s = v.trim().toLowerCase();
158
+ if (s === "1" || s === "true" || s === "yes") {
159
+ return true;
160
+ }
161
+ if (s === "0" || s === "false" || s === "no") {
162
+ return false;
163
+ }
164
+ return undefined;
165
+ }
166
+
124
167
  export async function startEngine(
125
168
  configuration: PicoEngineConfiguration = {}
126
169
  ): Promise<PicoEngine> {
127
- let home = configuration.home;
128
- let port = configuration.port;
129
- let base_url = configuration.base_url;
130
-
131
- if (typeof home !== "string") {
132
- const envHome = process.env.PICO_ENGINE_HOME;
133
- if (
134
- typeof envHome === "string" &&
135
- envHome.length > 0 &&
136
- process.env.NODE_ENV === "test"
137
- ) {
138
- home = envHome;
139
- } else {
140
- home = homeDir(".pico-engine") as string;
141
- }
170
+ const settings = resolveEngineSettings({
171
+ home: configuration.home,
172
+ port: configuration.port,
173
+ base_url: configuration.base_url,
174
+ rpID: configuration.rpID,
175
+ rpName: configuration.rpName,
176
+ origin: configuration.origin,
177
+ allowSelfSignup: configuration.allowSelfSignup,
178
+ defaultRootPolicyCeiling: configuration.defaultRootPolicyCeiling,
179
+ globalEventDeny: configuration.globalEventDeny,
180
+ globalQueryDeny: configuration.globalQueryDeny,
181
+ });
182
+ installEngineRuntimeSettings(settings);
183
+
184
+ const home = settings.home;
185
+ let port = configuration.port ?? settings.port;
186
+ const baseUrlFromCaller = configuration.base_url;
187
+ /** Filled after listen when using port 0 or when config baseUrl port does not match. */
188
+ let base_url: string | undefined = baseUrlFromCaller;
189
+ if (base_url == null && port > 0) {
190
+ base_url = settings.baseUrl;
142
191
  }
143
192
  await makeDir(home);
144
193
 
145
194
  const logFilePath = resolveEngineLogFilePath(home);
146
195
  const log = configuration.log
147
196
  ? configuration.log
148
- : makeKrlLogger(makeEngineLogWriter(logFilePath));
197
+ : makeKrlLogger(makeEngineLogWriter(logFilePath, home));
149
198
 
150
199
  const db = await openClassicLevelWithRecovery<PicoDbKey, any>(
151
200
  path.resolve(home, "db"),
@@ -163,27 +212,63 @@ export async function startEngine(
163
212
  log,
164
213
  modules: configuration.modules,
165
214
  useEventInputTime: configuration.useEventInputTime,
166
- autoCreateRootPico: configuration.autoCreateRootPico ?? false,
215
+ autoCreateRootPico:
216
+ configuration.autoCreateRootPico ??
217
+ autoCreateRootFromEnv() ??
218
+ false,
219
+ defaultRootPolicyCeiling:
220
+ configuration.defaultRootPolicyCeiling ??
221
+ settings.defaultRootPolicyCeiling,
222
+ globalEventDeny: configuration.globalEventDeny ?? settings.globalEventDeny,
223
+ globalQueryDeny: configuration.globalQueryDeny ?? settings.globalQueryDeny,
167
224
  getPicoLogs(picoId) {
168
- if (!logFilePath) {
169
- return Promise.resolve([]);
170
- }
171
- return getPicoLogs(logFilePath, picoId);
225
+ return Promise.resolve(picoLogEntries(picoId));
172
226
  },
173
227
  });
174
228
  const pf = core.picoFramework;
229
+ core.resolveBundledRulesetUrl = resolveBundledKrlUrl;
230
+ core.subtreeImportServices = wranglerSubtreeImportServices(core.rsRegistry);
175
231
  const identity = new IdentityStore({ db: pf.db });
176
232
  let identityService!: IdentityService;
233
+ const onPicoCreated = async (
234
+ picoId: string,
235
+ ctx: { isRoot: boolean; parentPicoId: string }
236
+ ) => {
237
+ if (core.base_url) {
238
+ await identityService.ensureWebvhDid(picoId, { isRoot: ctx.isRoot });
239
+ }
240
+ };
177
241
  identityService = new IdentityService({
178
242
  store: identity,
179
243
  getBaseUrl: () => core.base_url || base_url || "",
180
244
  pf,
245
+ rsRegistry: core.rsRegistry,
246
+ resolveBundledRulesetUrl: resolveBundledKrlUrl,
247
+ reloadImportedSchedules: (picoIds) => core.reloadImportedSchedules(picoIds),
248
+ firePicoImported: async (result) => {
249
+ const importedRoot = pf.getPico(result.newRootPicoId);
250
+ if (!importedRoot) {
251
+ return;
252
+ }
253
+ await importedRoot.event({
254
+ eci: result.newRootEci,
255
+ domain: "wrangler",
256
+ name: "pico_imported",
257
+ data: {
258
+ attrs: {
259
+ oldRootPicoId: result.oldRootPicoId,
260
+ newRootPicoId: result.newRootPicoId,
261
+ eciMap: result.eciMap,
262
+ picoIdMap: result.picoIdMap,
263
+ portableIdentity: true,
264
+ },
265
+ },
266
+ time: Date.now(),
267
+ });
268
+ },
181
269
  });
182
- core.onPicoCreated = async (picoId, ctx) => {
183
- if (core.base_url) {
184
- await identityService.ensureWebvhDid(picoId, { isRoot: ctx.isRoot });
185
- }
186
- };
270
+ core.onPicoCreated = onPicoCreated;
271
+ core.subtreePortableHooks = createSubtreePortableHooks(identity, pf);
187
272
  core.modules["dido"] = initDidoModule(identityService);
188
273
 
189
274
  try {
@@ -212,7 +297,8 @@ export async function startEngine(
212
297
  rpID: configuration.rpID,
213
298
  rpName: configuration.rpName,
214
299
  origin: configuration.origin,
215
- allowSelfSignup: configuration.allowSelfSignup ?? false,
300
+ allowSelfSignup:
301
+ configuration.allowSelfSignup ?? settings.allowSelfSignup,
216
302
  flushBootstrapUrl: async (url) => {
217
303
  const flushed = await rsRegistry.flush(url);
218
304
  return { rid: flushed.rid };
@@ -273,6 +359,11 @@ export async function startEngine(
273
359
  });
274
360
  if (typeof base_url !== "string") {
275
361
  base_url = `http://localhost:${port}`;
362
+ } else if (
363
+ baseUrlFromCaller == null &&
364
+ (configuration.port === 0 || port !== settings.port)
365
+ ) {
366
+ base_url = `http://localhost:${port}`;
276
367
  }
277
368
  core.base_url = base_url;
278
369
 
@@ -319,6 +410,7 @@ export async function startEngine(
319
410
  base_url,
320
411
 
321
412
  pf,
413
+ core,
322
414
  uiECI,
323
415
  rsRegistry,
324
416
  auth,
package/src/logging.ts CHANGED
@@ -2,11 +2,132 @@ import * as fs from "fs";
2
2
  import * as readline from "readline";
3
3
  import { krlLogLevelCodeToHuman, PicoLogEntry } from "krl-stdlib";
4
4
  import * as path from "path";
5
+ import { getEngineRuntimeSettings } from "./engineConfig";
5
6
 
6
7
  const rfs = require("rotating-file-stream");
7
8
 
8
9
  const logStreams: { [filePath: string]: NodeJS.WritableStream } = {};
9
10
 
11
+ /** Last N transactions per pico kept for the Logging tab. A restart clears this. */
12
+ const DEFAULT_LOG_RING = 200;
13
+
14
+ interface PicoLogRing {
15
+ order: string[];
16
+ txns: Map<string, PicoLogEntry[]>;
17
+ }
18
+
19
+ const picoLogRings = new Map<string, PicoLogRing>();
20
+
21
+ export type PicoLogListener = (picoId: string, txnId: string) => void;
22
+
23
+ const picoLogListeners = new Set<PicoLogListener>();
24
+
25
+ /** Fires when a transaction on a pico finishes (`txnDone` or `txnError`). */
26
+ export function subscribePicoLogRing(listener: PicoLogListener): () => void {
27
+ picoLogListeners.add(listener);
28
+ return () => {
29
+ picoLogListeners.delete(listener);
30
+ };
31
+ }
32
+
33
+ function notifyPicoLog(picoId: string, txnId: string): void {
34
+ for (const listener of [...picoLogListeners]) {
35
+ try {
36
+ listener(picoId, txnId);
37
+ } catch {
38
+ // One client must not stop the log writer or the other clients.
39
+ }
40
+ }
41
+ }
42
+
43
+ export function picoLogRingLimit(): number {
44
+ const raw = process.env.PICO_ENGINE_LOG_RING;
45
+ if (raw == null || raw.trim() === "") return DEFAULT_LOG_RING;
46
+ const n = Number(raw);
47
+ if (!Number.isFinite(n) || n < 1) return DEFAULT_LOG_RING;
48
+ return Math.min(Math.floor(n), 10000);
49
+ }
50
+
51
+ export function clearPicoLogRing(): void {
52
+ picoLogRings.clear();
53
+ picoLogListeners.clear();
54
+ }
55
+
56
+ /**
57
+ * Keep one structured log line for the Logging tab. Lines without a pico and
58
+ * a transaction are ignored. The oldest transaction for that pico is dropped
59
+ * once the ring is full.
60
+ */
61
+ export function rememberPicoLogLine(
62
+ line: string,
63
+ limit = picoLogRingLimit()
64
+ ): void {
65
+ const trimmed = line.trim();
66
+ if (!trimmed || trimmed[0] !== "{") return;
67
+ let entry: any;
68
+ try {
69
+ entry = JSON.parse(trimmed);
70
+ } catch {
71
+ return;
72
+ }
73
+ if (
74
+ !entry ||
75
+ typeof entry.picoId !== "string" ||
76
+ entry.picoId === "" ||
77
+ typeof entry.txnId !== "string" ||
78
+ entry.txnId === ""
79
+ ) {
80
+ return;
81
+ }
82
+ const picoId = entry.picoId as string;
83
+ const txnId = entry.txnId as string;
84
+ let ring = picoLogRings.get(picoId);
85
+ if (!ring) {
86
+ ring = { order: [], txns: new Map() };
87
+ picoLogRings.set(picoId, ring);
88
+ }
89
+ let lines = ring.txns.get(txnId);
90
+ if (!lines) {
91
+ lines = [];
92
+ ring.txns.set(txnId, lines);
93
+ ring.order.push(txnId);
94
+ while (ring.order.length > limit) {
95
+ const drop = ring.order.shift();
96
+ if (drop) ring.txns.delete(drop);
97
+ }
98
+ }
99
+ const stored: PicoLogEntry = {
100
+ ...entry,
101
+ level: krlLogLevelCodeToHuman[String(entry.level)] || `${entry.level}`,
102
+ time: entry.time,
103
+ txnId,
104
+ };
105
+ delete (stored as { picoId?: string }).picoId;
106
+ lines.push(stored);
107
+ if (entry.msg === "txnDone" || entry.msg === "txnError") {
108
+ notifyPicoLog(picoId, txnId);
109
+ }
110
+ }
111
+
112
+ /** Recent log lines for one pico, oldest transaction first. */
113
+ export function picoLogEntries(picoId: string): PicoLogEntry[] {
114
+ const ring = picoLogRings.get(picoId);
115
+ if (!ring) return [];
116
+ const output: PicoLogEntry[] = [];
117
+ for (const txnId of ring.order) {
118
+ const lines = ring.txns.get(txnId);
119
+ if (lines) output.push(...lines);
120
+ }
121
+ return output;
122
+ }
123
+
124
+ function tapLogRing(write: (line: string) => void): (line: string) => void {
125
+ return (line: string) => {
126
+ rememberPicoLogLine(line);
127
+ write(line);
128
+ };
129
+ }
130
+
10
131
  function stdoutOnlyLogWriter(): (line: string) => void {
11
132
  const isTest = process.env.NODE_ENV === "test";
12
133
  return (line: string) => {
@@ -49,29 +170,65 @@ function getRotatingFileStream(filePath: string): NodeJS.WritableStream {
49
170
  return logStreams[filePath];
50
171
  }
51
172
 
173
+ /**
174
+ * Primary log sink: file path when logging to a file, or null when operator chose
175
+ * stdout-only (`logToStdout` / `PICO_ENGINE_LOG_FILE=stdout`).
176
+ */
52
177
  export function resolveEngineLogFilePath(home: string): string | null {
53
- const configured = process.env.PICO_ENGINE_LOG_FILE;
54
- if (
55
- configured === "0" ||
56
- configured === "-" ||
57
- configured === "stdout" ||
58
- configured === ""
59
- ) {
178
+ const settings = getEngineRuntimeSettings();
179
+ if (settings.logToStdout) {
60
180
  return null;
61
181
  }
62
- if (typeof configured === "string" && configured.length > 0) {
63
- return path.resolve(configured);
182
+ if (settings.logFile) {
183
+ return settings.logFile;
184
+ }
185
+ return path.resolve(home, "pico-engine.log");
186
+ }
187
+
188
+ /**
189
+ * Operator log file. The Logging tab reads the in-memory ring, not this file.
190
+ * When stdout-only, mirrors to `$PICO_ENGINE_HOME/pico-engine.log` unless
191
+ * `logMirror` is false (config `logMirror`, or `PICO_ENGINE_LOG_MIRROR=0`).
192
+ */
193
+ export function resolvePicoLogsReadPath(home: string): string | null {
194
+ const settings = getEngineRuntimeSettings();
195
+ if (settings.logToStdout) {
196
+ if (settings.logMirror === false) {
197
+ return null;
198
+ }
199
+ return path.resolve(home, "pico-engine.log");
200
+ }
201
+ if (settings.logFile) {
202
+ return settings.logFile;
64
203
  }
65
204
  return path.resolve(home, "pico-engine.log");
66
205
  }
67
206
 
68
207
  export function makeEngineLogWriter(
69
- filePath: string | null
208
+ filePath: string | null,
209
+ home: string
70
210
  ): (line: string) => void {
71
- if (!filePath) {
72
- return stdoutOnlyLogWriter();
211
+ if (filePath) {
212
+ return tapLogRing(makeRotatingFileLogWriter(filePath));
73
213
  }
74
- return makeRotatingFileLogWriter(filePath);
214
+ const mirrorPath = resolvePicoLogsReadPath(home);
215
+ if (!mirrorPath) {
216
+ return tapLogRing(stdoutOnlyLogWriter());
217
+ }
218
+ const fileStream = getRotatingFileStream(mirrorPath);
219
+ const isTest = process.env.NODE_ENV === "test";
220
+ return tapLogRing((line: string) => {
221
+ if (!isTest) {
222
+ process.stdout.write(line);
223
+ }
224
+ fileStream.write(line, (err?: Error | null) => {
225
+ if (err) {
226
+ process.stderr.write(
227
+ `pico-engine log file write failed (${mirrorPath}): ${err.message}\n`
228
+ );
229
+ }
230
+ });
231
+ });
75
232
  }
76
233
 
77
234
  export function makeRotatingFileLogWriter(
@@ -111,21 +268,19 @@ export async function getPicoLogs(
111
268
  });
112
269
 
113
270
  for await (const line of rl) {
114
- // Each line in the readline input will be successively available here as
115
- // `line`.
116
271
  if (line.indexOf(picoId) < 0) {
117
- continue; // not my pico
272
+ continue;
118
273
  }
119
274
  let entry;
120
275
  try {
121
276
  entry = JSON.parse(line);
122
277
  } catch (err) {}
123
278
  if (!entry || entry.picoId !== picoId) {
124
- continue; // not my pico
279
+ continue;
125
280
  }
126
281
  const time = new Date(entry.time);
127
282
  if (Date.now() - time.getTime() > 1000 * 60 * 60 * 12) {
128
- continue; // too old
283
+ continue;
129
284
  }
130
285
 
131
286
  const out: PicoLogEntry = {
@@ -7,6 +7,11 @@ import {
7
7
  } from "./channelEligibility";
8
8
  import { OAuthError } from "./errors";
9
9
  import { isChannelUnderRoot, meshRequiresOAuth as meshRequiresOAuthForEci } from "./meshOAuth";
10
+ import {
11
+ channelHasUnrestrictedAllow,
12
+ isDidcommIngressChannel,
13
+ wildcardSkyRequireBearerEnabled,
14
+ } from "../channelIdentityPolicy";
10
15
 
11
16
  const OAUTH_CHANNEL_PREFIX = "oauth-channel";
12
17
  const OAUTH_TOKEN_PREFIX = "oauth-token";
@@ -237,11 +242,14 @@ export class OAuthService {
237
242
  async skyRequiresBearer(eci: string): Promise<boolean> {
238
243
  try {
239
244
  const channel = this.lookupChannelReadOnly(eci);
245
+ if (isDidcommIngressChannel(channel)) {
246
+ return false;
247
+ }
240
248
  if (
241
- channel.tags.includes("didcomm") &&
242
- channel.tags.includes("ingress")
249
+ wildcardSkyRequireBearerEnabled() &&
250
+ channelHasUnrestrictedAllow(channel)
243
251
  ) {
244
- return false;
252
+ return true;
245
253
  }
246
254
  if (isOAuthEligibleChannel(channel)) {
247
255
  return true;