pico-engine 1.8.0 → 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.
- package/README.md +3 -1
- package/dist/RulesetRegistryLoaderFs.js +46 -4
- package/dist/cli.js +3 -1
- package/dist/engineConfig.js +11 -0
- package/dist/index.js +2 -5
- package/dist/logging.js +148 -4
- package/dist/server.js +206 -1
- package/dist/stageKrl.js +26 -0
- package/dist/xmluiRuntime.js +68 -0
- package/krl/io.picolabs.pico-engine-ui.krl +12 -77
- package/krl/io.picolabs.wrangler.krl +21 -4
- package/krl/io.picolabs.xmlui.krl +295 -0
- package/package.json +9 -6
- package/public/.DS_Store +0 -0
- package/public/docs/_sidebar.md +1 -0
- package/public/docs/llms-full.txt +154 -3
- package/public/docs/llms.txt +1 -0
- package/public/docs/pico-engine.md +7 -3
- package/public/docs/pico-views-xmlui.md +5 -0
- package/public/docs/pico-views.md +140 -0
- package/public/docs/rulesets/README.md +1 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +27 -25
- package/public/xmlui/0.12.7.js +419 -0
- package/public/xmlui/pico-view-boot.js +153 -0
- package/src/RulesetRegistryLoaderFs.ts +48 -4
- package/src/cli.ts +3 -1
- package/src/engineConfig.ts +18 -0
- package/src/index.ts +4 -6
- package/src/logging.ts +165 -4
- package/src/server.ts +250 -2
- package/src/stageKrl.ts +26 -0
- package/src/xmluiRuntime.ts +66 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pico view shell (S1): optional Sky bearer for DataSource/APICall when the mesh
|
|
3
|
+
* requires OAuth. Loads the vendored XMLUI runtime after installing fetch hooks.
|
|
4
|
+
*/
|
|
5
|
+
(function picoViewBoot() {
|
|
6
|
+
var eci = window.__PICO_VIEW_ECI;
|
|
7
|
+
var xmluiScript = window.__PICO_XMLUI_SCRIPT || "/xmlui/0.12.7.js";
|
|
8
|
+
var authHeaders = {};
|
|
9
|
+
|
|
10
|
+
function normPath(p) {
|
|
11
|
+
if (!p || p === "/") return "/";
|
|
12
|
+
return p.replace(/\/+$/, "") || "/";
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function appRootPath() {
|
|
16
|
+
return (window.__PUBLIC_PATH || "").replace(/\/+$/, "");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function useHashRouting() {
|
|
20
|
+
if (window.__PICO_VIEW_USE_HASH_ROUTING === "false") return false;
|
|
21
|
+
if (window.__PICO_VIEW_USE_HASH_ROUTING === "true") return true;
|
|
22
|
+
return !!window.__PICO_VIEW_ECI;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** XMLUI standalone defaults to hash routing; pathnames stay at the app root. */
|
|
26
|
+
function syncRoutesBeforeXmlui() {
|
|
27
|
+
var root = appRootPath();
|
|
28
|
+
if (!root) return;
|
|
29
|
+
|
|
30
|
+
if (useHashRouting()) {
|
|
31
|
+
var path = window.location.pathname;
|
|
32
|
+
if (path.indexOf(root) === 0) {
|
|
33
|
+
var rest = path.slice(root.length).replace(/^\/+/, "").replace(/\/+$/, "");
|
|
34
|
+
if (rest) {
|
|
35
|
+
var hashRoute = rest.charAt(0) === "/" ? rest : "/" + rest;
|
|
36
|
+
window.history.replaceState(
|
|
37
|
+
null,
|
|
38
|
+
"",
|
|
39
|
+
root + "/" + window.location.search + "#" + hashRoute
|
|
40
|
+
);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
var fb = window.__XMLUI_FALLBACK_PATH || "/";
|
|
45
|
+
if (!fb.startsWith("/")) fb = "/" + fb;
|
|
46
|
+
var hash = window.location.hash;
|
|
47
|
+
if (!hash || hash === "#") {
|
|
48
|
+
window.history.replaceState(
|
|
49
|
+
null,
|
|
50
|
+
"",
|
|
51
|
+
root + "/" + window.location.search + "#" + fb
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (window.location.pathname === root) {
|
|
58
|
+
window.history.replaceState(
|
|
59
|
+
null,
|
|
60
|
+
"",
|
|
61
|
+
root + "/" + window.location.search + window.location.hash
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
var fbPath = window.__XMLUI_FALLBACK_PATH;
|
|
66
|
+
if (!fbPath || fbPath === "/") return;
|
|
67
|
+
var here = normPath(window.location.pathname);
|
|
68
|
+
if (here !== normPath(root)) return;
|
|
69
|
+
var segment = fbPath.replace(/^\//, "");
|
|
70
|
+
window.history.replaceState(
|
|
71
|
+
null,
|
|
72
|
+
"",
|
|
73
|
+
root + "/" + segment + window.location.search + window.location.hash
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
syncRoutesBeforeXmlui();
|
|
78
|
+
|
|
79
|
+
function patchFetch() {
|
|
80
|
+
var origFetch = window.fetch;
|
|
81
|
+
window.fetch = function (input, init) {
|
|
82
|
+
var url =
|
|
83
|
+
typeof input === "string"
|
|
84
|
+
? input
|
|
85
|
+
: input && input.url
|
|
86
|
+
? input.url
|
|
87
|
+
: "";
|
|
88
|
+
if (
|
|
89
|
+
url.indexOf("/sky/query/") !== -1 ||
|
|
90
|
+
url.indexOf("/sky/event/") !== -1 ||
|
|
91
|
+
url.indexOf("/sky/cloud/") !== -1
|
|
92
|
+
) {
|
|
93
|
+
init = init || {};
|
|
94
|
+
var h = new Headers(init.headers || {});
|
|
95
|
+
if (authHeaders.Authorization) {
|
|
96
|
+
h.set("Authorization", authHeaders.Authorization);
|
|
97
|
+
}
|
|
98
|
+
init.headers = h;
|
|
99
|
+
}
|
|
100
|
+
if (typeof url === "string" && url.indexOf("config.json") !== -1) {
|
|
101
|
+
return origFetch(input, init).then(function (res) {
|
|
102
|
+
if (!res.ok) return res;
|
|
103
|
+
return res.clone().json().then(function (cfg) {
|
|
104
|
+
cfg.appGlobals = cfg.appGlobals || {};
|
|
105
|
+
cfg.appGlobals.auth = authHeaders;
|
|
106
|
+
if (useHashRouting()) {
|
|
107
|
+
cfg.appGlobals.useHashBasedRouting = true;
|
|
108
|
+
cfg.xmluiConfig = cfg.xmluiConfig || {};
|
|
109
|
+
cfg.xmluiConfig.useHashBasedRouting = true;
|
|
110
|
+
}
|
|
111
|
+
return new Response(JSON.stringify(cfg), {
|
|
112
|
+
status: 200,
|
|
113
|
+
headers: { "Content-Type": "application/json" },
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return origFetch(input, init);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function loadXmlui() {
|
|
123
|
+
patchFetch();
|
|
124
|
+
var s = document.createElement("script");
|
|
125
|
+
s.src = xmluiScript;
|
|
126
|
+
s.onload = function () {
|
|
127
|
+
if (window.xmlui && typeof window.xmlui.startApp === "function") {
|
|
128
|
+
window.xmlui.startApp();
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
document.head.appendChild(s);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!eci) {
|
|
135
|
+
loadXmlui();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
fetch("/api/ui-sky-context?eci=" + encodeURIComponent(eci))
|
|
140
|
+
.then(function (r) {
|
|
141
|
+
return r.ok ? r.json() : { bearerRequired: false };
|
|
142
|
+
})
|
|
143
|
+
.then(function (ctx) {
|
|
144
|
+
if (ctx && ctx.bearerRequired) {
|
|
145
|
+
var tok = sessionStorage.getItem("picoViewBearer");
|
|
146
|
+
if (tok) authHeaders = { Authorization: "Bearer " + tok };
|
|
147
|
+
}
|
|
148
|
+
loadXmlui();
|
|
149
|
+
})
|
|
150
|
+
.catch(function () {
|
|
151
|
+
loadXmlui();
|
|
152
|
+
});
|
|
153
|
+
})();
|
|
@@ -80,9 +80,24 @@ export async function createRulesetRegistryLoaderFs(
|
|
|
80
80
|
|
|
81
81
|
let compileWarnings: any[] = [];
|
|
82
82
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
let needCompile = !(await fsExist(jsFile));
|
|
84
|
+
if (!needCompile) {
|
|
85
|
+
const cached = requireRulesetModule(jsFile);
|
|
86
|
+
if (cached) {
|
|
87
|
+
const compileGeneration = rulesetCompileGeneration();
|
|
88
|
+
return {
|
|
89
|
+
ruleset: cached,
|
|
90
|
+
compiler: {
|
|
91
|
+
version: krlCompilerVersion,
|
|
92
|
+
warnings: compileWarnings,
|
|
93
|
+
compileGeneration,
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
needCompile = true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (needCompile) {
|
|
86
101
|
const out = krlCompiler(krl, {
|
|
87
102
|
parser_options: {
|
|
88
103
|
filename: getUrlFilename(url),
|
|
@@ -101,7 +116,12 @@ export async function createRulesetRegistryLoaderFs(
|
|
|
101
116
|
});
|
|
102
117
|
}
|
|
103
118
|
|
|
104
|
-
const ruleset
|
|
119
|
+
const ruleset = requireRulesetModule(jsFile);
|
|
120
|
+
if (!ruleset) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`Compiled ruleset is missing init (corrupt cache file). Delete ${jsFile} and flush again: ${url}`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
105
125
|
|
|
106
126
|
const compileGeneration = rulesetCompileGeneration();
|
|
107
127
|
return {
|
|
@@ -183,6 +203,30 @@ async function fetchKrl(url: string): Promise<string> {
|
|
|
183
203
|
return krl;
|
|
184
204
|
}
|
|
185
205
|
|
|
206
|
+
function requireRulesetModule(jsFile: string): Ruleset | null {
|
|
207
|
+
try {
|
|
208
|
+
delete require.cache[require.resolve(jsFile)];
|
|
209
|
+
} catch {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
let ruleset: Ruleset;
|
|
213
|
+
try {
|
|
214
|
+
ruleset = require(jsFile) as Ruleset;
|
|
215
|
+
} catch {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
if (typeof ruleset?.init !== "function") {
|
|
219
|
+
try {
|
|
220
|
+
delete require.cache[require.resolve(jsFile)];
|
|
221
|
+
fs.unlinkSync(jsFile);
|
|
222
|
+
} catch {
|
|
223
|
+
/* best effort */
|
|
224
|
+
}
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
return ruleset;
|
|
228
|
+
}
|
|
229
|
+
|
|
186
230
|
function fsExist(filePath: string): Promise<boolean> {
|
|
187
231
|
return new Promise((resolve, reject) => {
|
|
188
232
|
fs.stat(filePath, function (err, stats) {
|
package/src/cli.ts
CHANGED
|
@@ -78,7 +78,9 @@ Environment (optional; Docker/CI when not set in config file):
|
|
|
78
78
|
PICO_ENGINE_ALLOW_SELF_SIGNUP - "true"/"1" or "false"/"0"
|
|
79
79
|
PICO_ENGINE_ALLOW_LOCALHOST_C - "0" disables localhost /c/* bypass (see config allowLocalhostChannelBypass)
|
|
80
80
|
PICO_ENGINE_WILDCARD_SKY_REQUIRE_BEARER - "1" enables wildcard /sky/* bearer requirement
|
|
81
|
-
PICO_ENGINE_LOG_FILE - Log path, or stdout/- / 0 for console
|
|
81
|
+
PICO_ENGINE_LOG_FILE - Log path, or stdout/- / 0 for console-only
|
|
82
|
+
PICO_ENGINE_LOG_MIRROR - "true"/"1" or "false"/"0" (mirror JSON lines to \$PICO_ENGINE_HOME/pico-engine.log when stdout-only; default on)
|
|
83
|
+
PICO_ENGINE_LOG_RING - how many recent transactions per pico the Logging tab keeps in memory (default 200; cleared on restart)
|
|
82
84
|
`);
|
|
83
85
|
process.exit(0);
|
|
84
86
|
}
|
package/src/engineConfig.ts
CHANGED
|
@@ -32,6 +32,12 @@ export interface EngineSettings {
|
|
|
32
32
|
logToStdout: boolean;
|
|
33
33
|
/** Absolute log file path; ignored when logToStdout is true. */
|
|
34
34
|
logFile?: string;
|
|
35
|
+
/**
|
|
36
|
+
* When `logToStdout` is true: also write structured JSON lines to
|
|
37
|
+
* `$PICO_ENGINE_HOME/pico-engine.log` so `ctx:logs` / Developer UI Logging work.
|
|
38
|
+
* Set false on EFS/Fargate to avoid mirror file churn (`PICO_ENGINE_LOG_MIRROR=0`).
|
|
39
|
+
*/
|
|
40
|
+
logMirror: boolean;
|
|
35
41
|
/** Max channel policy for root picos when none stored on the pico (1.8). */
|
|
36
42
|
defaultRootPolicyCeiling?: PolicyCeiling;
|
|
37
43
|
/** Deny rules applied on every ECI before per-channel policy (1.8). */
|
|
@@ -52,6 +58,7 @@ export interface EngineSettingsOverrides {
|
|
|
52
58
|
wildcardSkyRequireBearer?: boolean;
|
|
53
59
|
logToStdout?: boolean;
|
|
54
60
|
logFile?: string;
|
|
61
|
+
logMirror?: boolean;
|
|
55
62
|
defaultRootPolicyCeiling?: PolicyCeiling;
|
|
56
63
|
globalEventDeny?: EventPolicyRule[];
|
|
57
64
|
globalQueryDeny?: QueryPolicyRule[];
|
|
@@ -79,6 +86,7 @@ export function defaultEngineSettings(): EngineSettings {
|
|
|
79
86
|
allowLocalhostChannelBypass: true,
|
|
80
87
|
wildcardSkyRequireBearer: false,
|
|
81
88
|
logToStdout: false,
|
|
89
|
+
logMirror: true,
|
|
82
90
|
};
|
|
83
91
|
}
|
|
84
92
|
|
|
@@ -183,6 +191,9 @@ function parseConfigFile(raw: string): Partial<EngineSettings> {
|
|
|
183
191
|
if (typeof o.logToStdout === "boolean") {
|
|
184
192
|
out.logToStdout = o.logToStdout;
|
|
185
193
|
}
|
|
194
|
+
if (typeof o.logMirror === "boolean") {
|
|
195
|
+
out.logMirror = o.logMirror;
|
|
196
|
+
}
|
|
186
197
|
if (typeof o.logFile === "string" && o.logFile.length > 0) {
|
|
187
198
|
if (o.logFile === "stdout" || o.logFile === "-") {
|
|
188
199
|
out.logToStdout = true;
|
|
@@ -277,6 +288,10 @@ export function settingsFromEnv(): Partial<EngineSettings> {
|
|
|
277
288
|
} else if (typeof logFile === "string" && logFile.length > 0) {
|
|
278
289
|
out.logFile = path.resolve(expandHome(logFile));
|
|
279
290
|
}
|
|
291
|
+
const logMirror = parseBoolEnv(process.env.PICO_ENGINE_LOG_MIRROR);
|
|
292
|
+
if (logMirror !== undefined) {
|
|
293
|
+
out.logMirror = logMirror;
|
|
294
|
+
}
|
|
280
295
|
|
|
281
296
|
return out;
|
|
282
297
|
}
|
|
@@ -325,6 +340,9 @@ export function resolveEngineSettings(
|
|
|
325
340
|
if (programmatic.logFile !== undefined) {
|
|
326
341
|
fromProgrammatic.logFile = programmatic.logFile;
|
|
327
342
|
}
|
|
343
|
+
if (programmatic.logMirror !== undefined) {
|
|
344
|
+
fromProgrammatic.logMirror = programmatic.logMirror;
|
|
345
|
+
}
|
|
328
346
|
if (programmatic.defaultRootPolicyCeiling !== undefined) {
|
|
329
347
|
fromProgrammatic.defaultRootPolicyCeiling =
|
|
330
348
|
programmatic.defaultRootPolicyCeiling;
|
package/src/index.ts
CHANGED
|
@@ -21,9 +21,10 @@ import {
|
|
|
21
21
|
import { registerWebvhRoutes } from "./identity/webvhRoutes";
|
|
22
22
|
import { OAuthService, initOAuthModule } from "./oauth";
|
|
23
23
|
import {
|
|
24
|
-
|
|
24
|
+
picoLogEntries,
|
|
25
25
|
makeEngineLogWriter,
|
|
26
26
|
resolveEngineLogFilePath,
|
|
27
|
+
resolvePicoLogsReadPath,
|
|
27
28
|
} from "./logging";
|
|
28
29
|
import { openClassicLevelWithRecovery } from "./levelDb";
|
|
29
30
|
import { provisionRoot, uiECIForRoot } from "./provisionRoot";
|
|
@@ -193,7 +194,7 @@ export async function startEngine(
|
|
|
193
194
|
const logFilePath = resolveEngineLogFilePath(home);
|
|
194
195
|
const log = configuration.log
|
|
195
196
|
? configuration.log
|
|
196
|
-
: makeKrlLogger(makeEngineLogWriter(logFilePath));
|
|
197
|
+
: makeKrlLogger(makeEngineLogWriter(logFilePath, home));
|
|
197
198
|
|
|
198
199
|
const db = await openClassicLevelWithRecovery<PicoDbKey, any>(
|
|
199
200
|
path.resolve(home, "db"),
|
|
@@ -221,10 +222,7 @@ export async function startEngine(
|
|
|
221
222
|
globalEventDeny: configuration.globalEventDeny ?? settings.globalEventDeny,
|
|
222
223
|
globalQueryDeny: configuration.globalQueryDeny ?? settings.globalQueryDeny,
|
|
223
224
|
getPicoLogs(picoId) {
|
|
224
|
-
|
|
225
|
-
return Promise.resolve([]);
|
|
226
|
-
}
|
|
227
|
-
return getPicoLogs(logFilePath, picoId);
|
|
225
|
+
return Promise.resolve(picoLogEntries(picoId));
|
|
228
226
|
},
|
|
229
227
|
});
|
|
230
228
|
const pf = core.picoFramework;
|
package/src/logging.ts
CHANGED
|
@@ -8,6 +8,126 @@ const rfs = require("rotating-file-stream");
|
|
|
8
8
|
|
|
9
9
|
const logStreams: { [filePath: string]: NodeJS.WritableStream } = {};
|
|
10
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
|
+
|
|
11
131
|
function stdoutOnlyLogWriter(): (line: string) => void {
|
|
12
132
|
const isTest = process.env.NODE_ENV === "test";
|
|
13
133
|
return (line: string) => {
|
|
@@ -50,6 +170,10 @@ function getRotatingFileStream(filePath: string): NodeJS.WritableStream {
|
|
|
50
170
|
return logStreams[filePath];
|
|
51
171
|
}
|
|
52
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
|
+
*/
|
|
53
177
|
export function resolveEngineLogFilePath(home: string): string | null {
|
|
54
178
|
const settings = getEngineRuntimeSettings();
|
|
55
179
|
if (settings.logToStdout) {
|
|
@@ -61,13 +185,50 @@ export function resolveEngineLogFilePath(home: string): string | null {
|
|
|
61
185
|
return path.resolve(home, "pico-engine.log");
|
|
62
186
|
}
|
|
63
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;
|
|
203
|
+
}
|
|
204
|
+
return path.resolve(home, "pico-engine.log");
|
|
205
|
+
}
|
|
206
|
+
|
|
64
207
|
export function makeEngineLogWriter(
|
|
65
|
-
filePath: string | null
|
|
208
|
+
filePath: string | null,
|
|
209
|
+
home: string
|
|
66
210
|
): (line: string) => void {
|
|
67
|
-
if (
|
|
68
|
-
return
|
|
211
|
+
if (filePath) {
|
|
212
|
+
return tapLogRing(makeRotatingFileLogWriter(filePath));
|
|
69
213
|
}
|
|
70
|
-
|
|
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
|
+
});
|
|
71
232
|
}
|
|
72
233
|
|
|
73
234
|
export function makeRotatingFileLogWriter(
|