pico-engine 1.8.0 → 1.9.1

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 (57) hide show
  1. package/README.md +5 -3
  2. package/dist/RulesetRegistryLoaderFs.js +46 -4
  3. package/dist/cli.js +3 -1
  4. package/dist/engineConfig.js +11 -0
  5. package/dist/index.js +2 -5
  6. package/dist/logging.js +148 -4
  7. package/dist/server.js +206 -1
  8. package/dist/stageKrl.js +26 -0
  9. package/dist/xmluiRuntime.js +68 -0
  10. package/krl/io.picolabs.pico-engine-ui.krl +14 -77
  11. package/krl/io.picolabs.wrangler.krl +28 -5
  12. package/krl/io.picolabs.xmlui.krl +305 -0
  13. package/mcp/devResources.js +17 -7
  14. package/mcp/docs/CLAUDE.md +85 -0
  15. package/{public → mcp}/docs/pico-engine.md +15 -11
  16. package/package.json +11 -7
  17. package/public/pico-engine-ui.css +1 -1
  18. package/public/pico-engine-ui.js +27 -25
  19. package/public/xmlui/0.12.7.js +419 -0
  20. package/public/xmlui/pico-view-boot.js +153 -0
  21. package/src/RulesetRegistryLoaderFs.ts +48 -4
  22. package/src/cli.ts +3 -1
  23. package/src/engineConfig.ts +18 -0
  24. package/src/index.ts +4 -6
  25. package/src/logging.ts +165 -4
  26. package/src/server.ts +250 -2
  27. package/src/stageKrl.ts +26 -0
  28. package/src/xmluiRuntime.ts +66 -0
  29. package/public/docs/README.md +0 -9
  30. package/public/docs/_media/logo.png +0 -0
  31. package/public/docs/_sidebar.md +0 -6
  32. package/public/docs/contributing.md +0 -64
  33. package/public/docs/favicon.ico +0 -0
  34. package/public/docs/index.html +0 -29
  35. package/public/docs/krl/README.md +0 -3
  36. package/public/docs/krl/_sidebar.md +0 -10
  37. package/public/docs/krl/discovery.md +0 -144
  38. package/public/docs/krl/modules/ctx.md +0 -151
  39. package/public/docs/krl/modules/event.md +0 -70
  40. package/public/docs/krl/modules/http.md +0 -42
  41. package/public/docs/krl/modules/math.md +0 -5
  42. package/public/docs/krl/modules/schedule.md +0 -5
  43. package/public/docs/krl/modules.md +0 -11
  44. package/public/docs/krl/rulesets.md +0 -81
  45. package/public/docs/krl/stdlib.md +0 -3
  46. package/public/docs/llms-full.txt +0 -479
  47. package/public/docs/llms.txt +0 -37
  48. package/public/docs/openapi.yaml +0 -78
  49. package/public/docs/pico.md +0 -5
  50. package/public/docs/rulesets/README.md +0 -25
  51. package/public/docs/rulesets/io.picolabs.oauth.md +0 -71
  52. package/public/docs/rulesets/io.picolabs.pds.md +0 -327
  53. package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +0 -65
  54. package/public/docs/rulesets/io.picolabs.portable_subtree.md +0 -94
  55. package/public/docs/rulesets/io.picolabs.subscription.md +0 -398
  56. package/public/docs/rulesets/io.picolabs.wrangler.md +0 -471
  57. /package/{public → mcp}/docs/pico-overview.md +0 -0
package/README.md CHANGED
@@ -11,9 +11,9 @@ npm install -g pico-engine
11
11
  pico-engine
12
12
  ```
13
13
 
14
- Open `http://localhost:3000` — a root pico and UI channel are created on first run. Operator settings (port, data dir, policy ceiling) live in **`~/.config/pico-engine/config.json`**; copy [`config.example.json`](config.example.json). See [`public/docs/pico-engine.md`](public/docs/pico-engine.md).
14
+ Open `http://localhost:3000` — a root pico and UI channel are created on first run. Operator settings (port, data dir, policy ceiling) live in **`~/.config/pico-engine/config.json`**; copy [`config.example.json`](config.example.json). See [`docs/pico-engine.md`](../../docs/pico-engine.md) (published at https://docs.picolabs.io/).
15
15
 
16
- **Agents (local dev):** [`CLAUDE.md`](../../CLAUDE.md) at repo root + `pico-engine mcp --stdio` (stdio MCP to the same engine). Machine-readable docs: [`public/docs/llms.txt`](public/docs/llms.txt).
16
+ **Agents (local dev):** [`CLAUDE.md`](../../CLAUDE.md) at repo root + `pico-engine mcp --stdio` (stdio MCP to the same engine). Machine-readable docs: [`docs/llms.txt`](../../docs/llms.txt).
17
17
 
18
18
  ## Getting Started
19
19
 
@@ -134,6 +134,7 @@ The server is configured via some environment variables.
134
134
  - `PICO_ENGINE_ALLOW_SELF_SIGNUP` - Set to `"true"` or `"1"` to allow open registration after bootstrap (default: off; bootstrap and invite still work)
135
135
  - `PICO_ENGINE_ALLOW_LOCALHOST_C` - Set to `"0"` to require passkey session on `/c/*` even from localhost (default: allow localhost without session for in-engine HTTP loops). Full registry: repo `MEMORY.md` § Engine environment variables.
136
136
  - `PICO_ENGINE_LOG_FILE` - Log file path. Set to `stdout`, `-`, or `0` for **stdout only** (recommended on Fargate + EFS; use CloudWatch for retention). Default: `$PICO_ENGINE_HOME/pico-engine.log`.
137
+ - `PICO_ENGINE_LOG_MIRROR` - When stdout-only: **`true`/`1`** (default) mirrors structured JSON to **`$PICO_ENGINE_HOME/pico-engine.log`** so **`ctx:logs`** and the Developer UI **Logging** tab work; set **`false`/`0`** to skip the mirror file (e.g. avoid EFS log churn). **Config:** `logMirror` in `config.json` (see `config.example.json`).
137
138
 
138
139
  The `PORT` is the only value used in setting up the engine’s [nodejs http server](https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback). We only specify the `port` so it listens listens to all traffic on that port, it will not filter by host.
139
140
 
@@ -189,7 +190,8 @@ EFS + LevelDB is a common deployment pattern. LevelDB expects a **single writer*
189
190
  | Container port | `3000` |
190
191
  | `PICO_ENGINE_HOME` | `/var/pico-image` |
191
192
  | `PICO_ENGINE_BASE_URL` | Public **HTTPS** origin users hit (e.g. `https://picos.example.com`) — required for passkeys and did:webvh |
192
- | `PICO_ENGINE_LOG_FILE` | `stdout` — log to CloudWatch only; avoid writing `pico-engine.log` on EFS (NFS stale-handle errors can crash the process) |
193
+ | `PICO_ENGINE_LOG_FILE` | `stdout` — log to CloudWatch |
194
+ | `PICO_ENGINE_LOG_MIRROR` | `0` — do **not** mirror to `pico-engine.log` on EFS when using stdout-only; or set **`logMirror: false`** in config |
193
195
  | EFS mount | `containerPath`: `/var/pico-image` |
194
196
  | Container `stopTimeout` | `120` (seconds) |
195
197
 
@@ -46,10 +46,23 @@ async function createRulesetRegistryLoaderFs(engineHomeDir, log) {
46
46
  async function compileAndLoad(url, krl, hash) {
47
47
  const jsFile = path.resolve(rulesetDir, compileGenerationDir(), hash.substr(0, 2), hash.substr(2, 2), hash + ".js");
48
48
  let compileWarnings = [];
49
- if (await fsExist(jsFile)) {
50
- // already compiled
49
+ let needCompile = !(await fsExist(jsFile));
50
+ if (!needCompile) {
51
+ const cached = requireRulesetModule(jsFile);
52
+ if (cached) {
53
+ const compileGeneration = rulesetCompileGeneration();
54
+ return {
55
+ ruleset: cached,
56
+ compiler: {
57
+ version: krlCompilerVersion,
58
+ warnings: compileWarnings,
59
+ compileGeneration,
60
+ },
61
+ };
62
+ }
63
+ needCompile = true;
51
64
  }
52
- else {
65
+ if (needCompile) {
53
66
  const out = krlCompiler(krl, {
54
67
  parser_options: {
55
68
  filename: getUrlFilename(url),
@@ -65,7 +78,10 @@ async function createRulesetRegistryLoaderFs(engineHomeDir, log) {
65
78
  fs.writeFile(jsFile, out.code, { encoding: "utf8" }, (err) => err ? reject(err) : resolve(undefined));
66
79
  });
67
80
  }
68
- const ruleset = require(jsFile);
81
+ const ruleset = requireRulesetModule(jsFile);
82
+ if (!ruleset) {
83
+ throw new Error(`Compiled ruleset is missing init (corrupt cache file). Delete ${jsFile} and flush again: ${url}`);
84
+ }
69
85
  const compileGeneration = rulesetCompileGeneration();
70
86
  return {
71
87
  ruleset,
@@ -128,6 +144,32 @@ async function fetchKrl(url) {
128
144
  const krl = await res.text();
129
145
  return krl;
130
146
  }
147
+ function requireRulesetModule(jsFile) {
148
+ try {
149
+ delete require.cache[require.resolve(jsFile)];
150
+ }
151
+ catch (_a) {
152
+ return null;
153
+ }
154
+ let ruleset;
155
+ try {
156
+ ruleset = require(jsFile);
157
+ }
158
+ catch (_b) {
159
+ return null;
160
+ }
161
+ if (typeof (ruleset === null || ruleset === void 0 ? void 0 : ruleset.init) !== "function") {
162
+ try {
163
+ delete require.cache[require.resolve(jsFile)];
164
+ fs.unlinkSync(jsFile);
165
+ }
166
+ catch (_c) {
167
+ /* best effort */
168
+ }
169
+ return null;
170
+ }
171
+ return ruleset;
172
+ }
131
173
  function fsExist(filePath) {
132
174
  return new Promise((resolve, reject) => {
133
175
  fs.stat(filePath, function (err, stats) {
package/dist/cli.js CHANGED
@@ -72,7 +72,9 @@ Environment (optional; Docker/CI when not set in config file):
72
72
  PICO_ENGINE_ALLOW_SELF_SIGNUP - "true"/"1" or "false"/"0"
73
73
  PICO_ENGINE_ALLOW_LOCALHOST_C - "0" disables localhost /c/* bypass (see config allowLocalhostChannelBypass)
74
74
  PICO_ENGINE_WILDCARD_SKY_REQUIRE_BEARER - "1" enables wildcard /sky/* bearer requirement
75
- PICO_ENGINE_LOG_FILE - Log path, or stdout/- / 0 for console only
75
+ PICO_ENGINE_LOG_FILE - Log path, or stdout/- / 0 for console-only
76
+ PICO_ENGINE_LOG_MIRROR - "true"/"1" or "false"/"0" (mirror JSON lines to \$PICO_ENGINE_HOME/pico-engine.log when stdout-only; default on)
77
+ PICO_ENGINE_LOG_RING - how many recent transactions per pico the Logging tab keeps in memory (default 200; cleared on restart)
76
78
  `);
77
79
  process.exit(0);
78
80
  }
@@ -33,6 +33,7 @@ function defaultEngineSettings() {
33
33
  allowLocalhostChannelBypass: true,
34
34
  wildcardSkyRequireBearer: false,
35
35
  logToStdout: false,
36
+ logMirror: true,
36
37
  };
37
38
  }
38
39
  function defaultEngineConfigFilePath() {
@@ -134,6 +135,9 @@ function parseConfigFile(raw) {
134
135
  if (typeof o.logToStdout === "boolean") {
135
136
  out.logToStdout = o.logToStdout;
136
137
  }
138
+ if (typeof o.logMirror === "boolean") {
139
+ out.logMirror = o.logMirror;
140
+ }
137
141
  if (typeof o.logFile === "string" && o.logFile.length > 0) {
138
142
  if (o.logFile === "stdout" || o.logFile === "-") {
139
143
  out.logToStdout = true;
@@ -219,6 +223,10 @@ function settingsFromEnv() {
219
223
  else if (typeof logFile === "string" && logFile.length > 0) {
220
224
  out.logFile = path.resolve(expandHome(logFile));
221
225
  }
226
+ const logMirror = parseBoolEnv(process.env.PICO_ENGINE_LOG_MIRROR);
227
+ if (logMirror !== undefined) {
228
+ out.logMirror = logMirror;
229
+ }
222
230
  return out;
223
231
  }
224
232
  function mergeSettings(base, ...layers) {
@@ -266,6 +274,9 @@ function resolveEngineSettings(programmatic = {}) {
266
274
  if (programmatic.logFile !== undefined) {
267
275
  fromProgrammatic.logFile = programmatic.logFile;
268
276
  }
277
+ if (programmatic.logMirror !== undefined) {
278
+ fromProgrammatic.logMirror = programmatic.logMirror;
279
+ }
269
280
  if (programmatic.defaultRootPolicyCeiling !== undefined) {
270
281
  fromProgrammatic.defaultRootPolicyCeiling =
271
282
  programmatic.defaultRootPolicyCeiling;
package/dist/index.js CHANGED
@@ -62,7 +62,7 @@ async function startEngine(configuration = {}) {
62
62
  const logFilePath = (0, logging_1.resolveEngineLogFilePath)(home);
63
63
  const log = configuration.log
64
64
  ? configuration.log
65
- : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeEngineLogWriter)(logFilePath));
65
+ : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeEngineLogWriter)(logFilePath, home));
66
66
  const db = await (0, levelDb_1.openClassicLevelWithRecovery)(path.resolve(home, "db"), {
67
67
  keyEncoding: charwise,
68
68
  valueEncoding: safeJsonCodec,
@@ -79,10 +79,7 @@ async function startEngine(configuration = {}) {
79
79
  globalEventDeny: (_e = configuration.globalEventDeny) !== null && _e !== void 0 ? _e : settings.globalEventDeny,
80
80
  globalQueryDeny: (_f = configuration.globalQueryDeny) !== null && _f !== void 0 ? _f : settings.globalQueryDeny,
81
81
  getPicoLogs(picoId) {
82
- if (!logFilePath) {
83
- return Promise.resolve([]);
84
- }
85
- return (0, logging_1.getPicoLogs)(logFilePath, picoId);
82
+ return Promise.resolve((0, logging_1.picoLogEntries)(picoId));
86
83
  },
87
84
  });
88
85
  const pf = core.picoFramework;
package/dist/logging.js CHANGED
@@ -7,7 +7,13 @@ 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.subscribePicoLogRing = subscribePicoLogRing;
11
+ exports.picoLogRingLimit = picoLogRingLimit;
12
+ exports.clearPicoLogRing = clearPicoLogRing;
13
+ exports.rememberPicoLogLine = rememberPicoLogLine;
14
+ exports.picoLogEntries = picoLogEntries;
10
15
  exports.resolveEngineLogFilePath = resolveEngineLogFilePath;
16
+ exports.resolvePicoLogsReadPath = resolvePicoLogsReadPath;
11
17
  exports.makeEngineLogWriter = makeEngineLogWriter;
12
18
  exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
13
19
  exports.getPicoLogs = getPicoLogs;
@@ -18,6 +24,107 @@ const path = require("path");
18
24
  const engineConfig_1 = require("./engineConfig");
19
25
  const rfs = require("rotating-file-stream");
20
26
  const logStreams = {};
27
+ /** Last N transactions per pico kept for the Logging tab. A restart clears this. */
28
+ const DEFAULT_LOG_RING = 200;
29
+ const picoLogRings = new Map();
30
+ const picoLogListeners = new Set();
31
+ /** Fires when a transaction on a pico finishes (`txnDone` or `txnError`). */
32
+ function subscribePicoLogRing(listener) {
33
+ picoLogListeners.add(listener);
34
+ return () => {
35
+ picoLogListeners.delete(listener);
36
+ };
37
+ }
38
+ function notifyPicoLog(picoId, txnId) {
39
+ for (const listener of [...picoLogListeners]) {
40
+ try {
41
+ listener(picoId, txnId);
42
+ }
43
+ catch (_a) {
44
+ // One client must not stop the log writer or the other clients.
45
+ }
46
+ }
47
+ }
48
+ function picoLogRingLimit() {
49
+ const raw = process.env.PICO_ENGINE_LOG_RING;
50
+ if (raw == null || raw.trim() === "")
51
+ return DEFAULT_LOG_RING;
52
+ const n = Number(raw);
53
+ if (!Number.isFinite(n) || n < 1)
54
+ return DEFAULT_LOG_RING;
55
+ return Math.min(Math.floor(n), 10000);
56
+ }
57
+ function clearPicoLogRing() {
58
+ picoLogRings.clear();
59
+ picoLogListeners.clear();
60
+ }
61
+ /**
62
+ * Keep one structured log line for the Logging tab. Lines without a pico and
63
+ * a transaction are ignored. The oldest transaction for that pico is dropped
64
+ * once the ring is full.
65
+ */
66
+ function rememberPicoLogLine(line, limit = picoLogRingLimit()) {
67
+ const trimmed = line.trim();
68
+ if (!trimmed || trimmed[0] !== "{")
69
+ return;
70
+ let entry;
71
+ try {
72
+ entry = JSON.parse(trimmed);
73
+ }
74
+ catch (_a) {
75
+ return;
76
+ }
77
+ if (!entry ||
78
+ typeof entry.picoId !== "string" ||
79
+ entry.picoId === "" ||
80
+ typeof entry.txnId !== "string" ||
81
+ entry.txnId === "") {
82
+ return;
83
+ }
84
+ const picoId = entry.picoId;
85
+ const txnId = entry.txnId;
86
+ let ring = picoLogRings.get(picoId);
87
+ if (!ring) {
88
+ ring = { order: [], txns: new Map() };
89
+ picoLogRings.set(picoId, ring);
90
+ }
91
+ let lines = ring.txns.get(txnId);
92
+ if (!lines) {
93
+ lines = [];
94
+ ring.txns.set(txnId, lines);
95
+ ring.order.push(txnId);
96
+ while (ring.order.length > limit) {
97
+ const drop = ring.order.shift();
98
+ if (drop)
99
+ ring.txns.delete(drop);
100
+ }
101
+ }
102
+ const stored = Object.assign(Object.assign({}, entry), { level: krl_stdlib_1.krlLogLevelCodeToHuman[String(entry.level)] || `${entry.level}`, time: entry.time, txnId });
103
+ delete stored.picoId;
104
+ lines.push(stored);
105
+ if (entry.msg === "txnDone" || entry.msg === "txnError") {
106
+ notifyPicoLog(picoId, txnId);
107
+ }
108
+ }
109
+ /** Recent log lines for one pico, oldest transaction first. */
110
+ function picoLogEntries(picoId) {
111
+ const ring = picoLogRings.get(picoId);
112
+ if (!ring)
113
+ return [];
114
+ const output = [];
115
+ for (const txnId of ring.order) {
116
+ const lines = ring.txns.get(txnId);
117
+ if (lines)
118
+ output.push(...lines);
119
+ }
120
+ return output;
121
+ }
122
+ function tapLogRing(write) {
123
+ return (line) => {
124
+ rememberPicoLogLine(line);
125
+ write(line);
126
+ };
127
+ }
21
128
  function stdoutOnlyLogWriter() {
22
129
  const isTest = process.env.NODE_ENV === "test";
23
130
  return (line) => {
@@ -48,6 +155,10 @@ function getRotatingFileStream(filePath) {
48
155
  }
49
156
  return logStreams[filePath];
50
157
  }
158
+ /**
159
+ * Primary log sink: file path when logging to a file, or null when operator chose
160
+ * stdout-only (`logToStdout` / `PICO_ENGINE_LOG_FILE=stdout`).
161
+ */
51
162
  function resolveEngineLogFilePath(home) {
52
163
  const settings = (0, engineConfig_1.getEngineRuntimeSettings)();
53
164
  if (settings.logToStdout) {
@@ -58,11 +169,44 @@ function resolveEngineLogFilePath(home) {
58
169
  }
59
170
  return path.resolve(home, "pico-engine.log");
60
171
  }
61
- function makeEngineLogWriter(filePath) {
62
- if (!filePath) {
63
- return stdoutOnlyLogWriter();
172
+ /**
173
+ * Operator log file. The Logging tab reads the in-memory ring, not this file.
174
+ * When stdout-only, mirrors to `$PICO_ENGINE_HOME/pico-engine.log` unless
175
+ * `logMirror` is false (config `logMirror`, or `PICO_ENGINE_LOG_MIRROR=0`).
176
+ */
177
+ function resolvePicoLogsReadPath(home) {
178
+ const settings = (0, engineConfig_1.getEngineRuntimeSettings)();
179
+ if (settings.logToStdout) {
180
+ if (settings.logMirror === false) {
181
+ return null;
182
+ }
183
+ return path.resolve(home, "pico-engine.log");
184
+ }
185
+ if (settings.logFile) {
186
+ return settings.logFile;
187
+ }
188
+ return path.resolve(home, "pico-engine.log");
189
+ }
190
+ function makeEngineLogWriter(filePath, home) {
191
+ if (filePath) {
192
+ return tapLogRing(makeRotatingFileLogWriter(filePath));
193
+ }
194
+ const mirrorPath = resolvePicoLogsReadPath(home);
195
+ if (!mirrorPath) {
196
+ return tapLogRing(stdoutOnlyLogWriter());
64
197
  }
65
- return makeRotatingFileLogWriter(filePath);
198
+ const fileStream = getRotatingFileStream(mirrorPath);
199
+ const isTest = process.env.NODE_ENV === "test";
200
+ return tapLogRing((line) => {
201
+ if (!isTest) {
202
+ process.stdout.write(line);
203
+ }
204
+ fileStream.write(line, (err) => {
205
+ if (err) {
206
+ process.stderr.write(`pico-engine log file write failed (${mirrorPath}): ${err.message}\n`);
207
+ }
208
+ });
209
+ });
66
210
  }
67
211
  function makeRotatingFileLogWriter(filePath) {
68
212
  const fileStream = getRotatingFileStream(filePath);
package/dist/server.js CHANGED
@@ -8,12 +8,17 @@ const helmet_1 = require("helmet");
8
8
  const _ = require("lodash");
9
9
  const path = require("path");
10
10
  const bundledKrl_1 = require("./bundledKrl");
11
+ const pico_engine_core_1 = require("pico-engine-core");
11
12
  const auth_1 = require("./auth");
12
13
  const oauth_1 = require("./oauth");
13
14
  const provisionRoot_1 = require("./provisionRoot");
15
+ const pico_engine_core_2 = require("pico-engine-core");
16
+ const xmluiRuntime_1 = require("./xmluiRuntime");
14
17
  const webvhRoutes_1 = require("./identity/webvhRoutes");
15
18
  const didcommIngressRoutes_1 = require("./identity/didcommIngressRoutes");
16
19
  const engineConfig_1 = require("./engineConfig");
20
+ const logging_1 = require("./logging");
21
+ const stageKrl_1 = require("./stageKrl");
17
22
  const engineVersion = require("../package.json").version;
18
23
  function localhostChannelBypassEnabled() {
19
24
  return (0, engineConfig_1.getEngineRuntimeSettings)().allowLocalhostChannelBypass;
@@ -103,6 +108,10 @@ function server(core, uiECI, auth, oauth, identity) {
103
108
  if (req.path === "/oauth/authorize" || req.path === "/oauth/approve") {
104
109
  return (0, helmet_1.default)({ contentSecurityPolicy: false })(req, res, next);
105
110
  }
111
+ // XMLUI view shells use small inline scripts (__PUBLIC_PATH, startApp).
112
+ if (req.path === "/ui" || req.path.startsWith("/ui/")) {
113
+ return (0, helmet_1.default)({ contentSecurityPolicy: false })(req, res, next);
114
+ }
106
115
  return defaultHelmet(req, res, next);
107
116
  });
108
117
  app.use(express.static(path.resolve(__dirname, "..", "public")));
@@ -218,6 +227,23 @@ function server(core, uiECI, auth, oauth, identity) {
218
227
  if (oauth) {
219
228
  registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies, cookies);
220
229
  }
230
+ app.get("/api/ui-sky-context", function (req, res, next) {
231
+ const eci = typeof req.query.eci === "string" ? req.query.eci.trim() : "";
232
+ if (!eci) {
233
+ res.status(400).json({ error: "eci query parameter required" });
234
+ return;
235
+ }
236
+ if (!oauth) {
237
+ res.json({ bearerRequired: false });
238
+ return;
239
+ }
240
+ oauth
241
+ .skyRequiresBearer(eci)
242
+ .then((bearerRequired) => {
243
+ res.json({ bearerRequired });
244
+ })
245
+ .catch(next);
246
+ });
221
247
  app.get("/api/oauth/mesh-enabled", requireAuthSession, function (_req, res) {
222
248
  const roots = pf.rootPicos();
223
249
  const meshOAuthEnabled = roots.length > 0 &&
@@ -276,6 +302,23 @@ function server(core, uiECI, auth, oauth, identity) {
276
302
  next(err);
277
303
  }
278
304
  });
305
+ app.post("/api/stage-krl", function (req, res, next) {
306
+ var _a, _b;
307
+ try {
308
+ const content = (_a = req.body) === null || _a === void 0 ? void 0 : _a.content;
309
+ const filename = typeof ((_b = req.body) === null || _b === void 0 ? void 0 : _b.filename) === "string" ? req.body.filename : "staged.krl";
310
+ if (typeof content !== "string" || content.length > 2000000) {
311
+ res.status(400).json({ error: "content required (max 2MB)" });
312
+ return;
313
+ }
314
+ const url = (0, stageKrl_1.stageKrlContent)(content, filename);
315
+ res.json({ url });
316
+ }
317
+ catch (err) {
318
+ const message = err instanceof Error ? err.message : String(err);
319
+ res.status(400).json({ error: message });
320
+ }
321
+ });
279
322
  app.get(/^\/krl\/(.+\.krl)$/, function (req, res, next) {
280
323
  const suffix = req.params[0];
281
324
  if (typeof suffix !== "string" || !suffix.endsWith(".krl")) {
@@ -323,8 +366,56 @@ function server(core, uiECI, auth, oauth, identity) {
323
366
  })
324
367
  .catch(next);
325
368
  });
369
+ /**
370
+ * Pushes each finished transaction for this pico. Owner session only,
371
+ * same gate as the rest of the developer UI.
372
+ */
373
+ app.get("/api/picos/:eci/logs/live", requireAuthSession, function (req, res) {
374
+ let picoId;
375
+ try {
376
+ picoId = pf.getPico(req.params.eci).id;
377
+ }
378
+ catch (_e) {
379
+ res.status(404).json({ error: "Unknown pico" });
380
+ return;
381
+ }
382
+ res.writeHead(200, {
383
+ "Content-Type": "text/event-stream",
384
+ "Cache-Control": "no-cache, no-transform",
385
+ Connection: "keep-alive",
386
+ "X-Accel-Buffering": "no",
387
+ });
388
+ res.write(": ok\n\n");
389
+ let closed = false;
390
+ const unsubscribe = (0, logging_1.subscribePicoLogRing)((id, txnId) => {
391
+ if (closed || id !== picoId)
392
+ return;
393
+ const entries = (0, logging_1.picoLogEntries)(picoId).filter((entry) => entry.txnId === txnId);
394
+ const [txn] = (0, pico_engine_core_1.assembleLogTransactions)(entries);
395
+ if (!txn)
396
+ return;
397
+ try {
398
+ res.write(`data: ${JSON.stringify(txn)}\n\n`);
399
+ }
400
+ catch (_err) {
401
+ closed = true;
402
+ unsubscribe();
403
+ }
404
+ });
405
+ const beat = setInterval(() => {
406
+ if (closed)
407
+ return;
408
+ res.write(": ping\n\n");
409
+ }, 25000);
410
+ req.on("close", () => {
411
+ closed = true;
412
+ clearInterval(beat);
413
+ unsubscribe();
414
+ });
415
+ });
326
416
  /** Localhost-only context for dev MCP (stdio) and other local tooling. */
327
417
  app.get("/api/dev/local", function (req, res) {
418
+ var _a;
328
419
  if (!localhostChannelBypassEnabled() || !isLocalhostRequest(req)) {
329
420
  res.status(403).json({ error: "localhost only" });
330
421
  return;
@@ -336,11 +427,28 @@ function server(core, uiECI, auth, oauth, identity) {
336
427
  if (!baseUrl) {
337
428
  baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
338
429
  }
430
+ const settings = (0, engineConfig_1.getEngineRuntimeSettings)();
431
+ const primaryLogFile = (0, logging_1.resolveEngineLogFilePath)(settings.home);
432
+ const picoLogsReadPath = (0, logging_1.resolvePicoLogsReadPath)(settings.home);
433
+ const mirrorDisabled = settings.logToStdout && settings.logMirror === false;
339
434
  res.json({
340
435
  version: engineVersion,
341
436
  hasRoots: roots.length > 0,
342
437
  rootUiEci,
343
438
  baseUrl: baseUrl.replace(/\/$/, ""),
439
+ logging: {
440
+ home: settings.home,
441
+ logToStdout: settings.logToStdout,
442
+ logMirror: settings.logMirror,
443
+ logFileSetting: (_a = settings.logFile) !== null && _a !== void 0 ? _a : null,
444
+ primaryLogFile,
445
+ /** File `getPicoLogs` reads (mirrors under stdout-only unless PICO_ENGINE_LOG_MIRROR=0). */
446
+ picoLogsReadPath,
447
+ picoLogsForDeveloperUi: true,
448
+ logMirrorDisabled: mirrorDisabled,
449
+ logRing: true,
450
+ hint: "Logging tab reads the in-memory ring (last N transactions per pico, PICO_ENGINE_LOG_RING, default 200). A restart clears it. The log file remains the operator record.",
451
+ },
344
452
  });
345
453
  });
346
454
  app.all("/api/flush", requireAuthSession, function (req, res, next) {
@@ -496,6 +604,102 @@ function server(core, uiECI, auth, oauth, identity) {
496
604
  })
497
605
  .catch(next);
498
606
  });
607
+ const XMLUI_FILE_RID = "io.picolabs.xmlui";
608
+ function respondXmluiQueryFailure(err, res, next) {
609
+ if (!(0, pico_engine_core_1.isRoutineQueryError)(err)) {
610
+ return false;
611
+ }
612
+ const message = err + "";
613
+ if (!res.headersSent) {
614
+ if (/channel policy/i.test(message)) {
615
+ res.status(403).type("text/plain").end(message);
616
+ }
617
+ else {
618
+ res.status(404).type("text/plain").end("Not found");
619
+ }
620
+ }
621
+ return true;
622
+ }
623
+ async function serveXmluiFile(req, res, next, eci, appRid, filePath) {
624
+ const rel = (filePath || "index.html").replace(/^\/+/, "") || "index.html";
625
+ try {
626
+ if ((0, xmluiRuntime_1.isXmluiCodeBehindPath)(rel)) {
627
+ res
628
+ .status(200)
629
+ .setHeader("Content-Type", (0, xmluiRuntime_1.contentTypeForUiPath)(rel))
630
+ .end(xmluiRuntime_1.XMLUI_EMPTY_CODE_BEHIND);
631
+ return;
632
+ }
633
+ let data;
634
+ const componentName = appRid ? (0, xmluiRuntime_1.parseComponentXmluiPath)(rel) : null;
635
+ if (componentName) {
636
+ const { pico } = pf.lookupChannel(eci);
637
+ const channelSchema = (0, pico_engine_core_2.channelSchemaForEci)(pico, core.rsRegistry, (channelEci) => pf.lookupChannel(channelEci), eci);
638
+ const viewFn = (0, xmluiRuntime_1.viewQueryNameForComponent)(channelSchema.queries, appRid, componentName);
639
+ if (!viewFn) {
640
+ res.status(404).type("text/plain").end("Not found");
641
+ return;
642
+ }
643
+ data = await core.query({
644
+ eci,
645
+ rid: appRid,
646
+ name: viewFn,
647
+ args: {},
648
+ });
649
+ }
650
+ else {
651
+ data = await core.query({
652
+ eci,
653
+ rid: XMLUI_FILE_RID,
654
+ name: "file",
655
+ args: { rid: appRid, path: rel },
656
+ });
657
+ }
658
+ if (data == null && appRid && (0, xmluiRuntime_1.isXmluiSpaClientRoute)(rel)) {
659
+ data = await core.query({
660
+ eci,
661
+ rid: XMLUI_FILE_RID,
662
+ name: "file",
663
+ args: { rid: appRid, path: "index.html" },
664
+ });
665
+ }
666
+ if (data == null) {
667
+ res.status(404).type("text/plain").end("Not found");
668
+ return;
669
+ }
670
+ if (typeof data === "object" &&
671
+ data !== null &&
672
+ "error" in data) {
673
+ const message = String(data.error);
674
+ res.status(404).type("text/plain").end(message);
675
+ return;
676
+ }
677
+ const servedPath = rel === "index.html" || !(0, xmluiRuntime_1.isXmluiSpaClientRoute)(rel) ? rel : "index.html";
678
+ const contentType = (0, xmluiRuntime_1.contentTypeForUiPath)(servedPath);
679
+ res.setHeader("Content-Type", contentType);
680
+ if (contentType.startsWith("application/json") && typeof data === "object") {
681
+ res.json(data);
682
+ return;
683
+ }
684
+ res.end(typeof data === "string" ? data : JSON.stringify(data));
685
+ }
686
+ catch (err) {
687
+ if (respondXmluiQueryFailure(err, res, next)) {
688
+ return;
689
+ }
690
+ next(err);
691
+ }
692
+ }
693
+ app.get("/ui/:eci", function (req, res, next) {
694
+ void serveXmluiFile(req, res, next, req.params.eci, "", "index.html");
695
+ });
696
+ app.get("/ui/:eci/:rid", function (req, res, next) {
697
+ void serveXmluiFile(req, res, next, req.params.eci, req.params.rid, "index.html");
698
+ });
699
+ app.get("/ui/:eci/:rid/*", function (req, res, next) {
700
+ const rest = req.params[0] || "index.html";
701
+ void serveXmluiFile(req, res, next, req.params.eci, req.params.rid, rest);
702
+ });
499
703
  app.all("/sky/query/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
500
704
  // Legacy alias; prefer /sky/query/ per Sky Query API docs.
501
705
  app.all("/sky/cloud/:eci/:rid/:function", requireSkyBearerIfOAuth, handleSkyQuery);
@@ -515,7 +719,8 @@ function server(core, uiECI, auth, oauth, identity) {
515
719
  }
516
720
  let message = err + "";
517
721
  const policyNoise = process.env.NODE_ENV === "test" &&
518
- /not allowed by channel policy/i.test(message);
722
+ ((0, pico_engine_core_1.isRoutineQueryError)(err) ||
723
+ /not allowed by channel policy/i.test(message));
519
724
  if (!policyNoise) {
520
725
  console.error(err);
521
726
  }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stageKrlContent = stageKrlContent;
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const os = require("os");
7
+ const toFileUrl_1 = require("./utils/toFileUrl");
8
+ const STAGE_DIR = path.join(os.tmpdir(), "pico-engine-staged-krl");
9
+ /** Write KRL source to a temp file and return a file:// URL the registry can load. */
10
+ function stageKrlContent(content, filename) {
11
+ if (typeof content !== "string" || !/^ruleset\s+/m.test(content)) {
12
+ throw new Error("File must contain a KRL ruleset (starting with ruleset …)");
13
+ }
14
+ let base = path.basename(filename || "staged.krl");
15
+ if (!base.toLowerCase().endsWith(".krl")) {
16
+ base = base + ".krl";
17
+ }
18
+ base = base.replace(/[^\w.-]+/g, "_");
19
+ if (base === ".krl" || base === "") {
20
+ base = "staged.krl";
21
+ }
22
+ fs.mkdirSync(STAGE_DIR, { recursive: true });
23
+ const dest = path.join(STAGE_DIR, `${Date.now()}-${base}`);
24
+ fs.writeFileSync(dest, content, { encoding: "utf8", flag: "wx" });
25
+ return (0, toFileUrl_1.toFileUrl)(dest);
26
+ }