Make runtime diagnostics non-blocking and release 2.1.69
CI / verify (push) Canceled after 0s

This commit is contained in:
Sucukdeluxe committed 2026-10-18 02:51:48 +02:00
1 parent 9d96f67261
commit 1e289ee49b
9 files changed
+129 -78

No files matched your search

+2 -2
View File
@@ -14,11 +14,11 @@ function createAgent(collectors) {
get_health: () => collectors.getHealth()
};
function handle(op, args) {
async function handle(op, args) {
const fn = (typeof op === 'string' && Object.prototype.hasOwnProperty.call(OPS, op)) ? OPS[op] : null;
if (typeof fn !== 'function') return { ok: false, error: `unknown or non-readonly op: ${op}` };
try {
const data = fn(args || {});
const data = await fn(args || {});
if (data && data.ok === false) return data;
return { ok: true, data };
} catch (e) {
+20 -20
View File
@@ -44,8 +44,8 @@ function createCollectors(deps) {
return p;
}
function getSystemInfo() {
return { app: appInfo(), system: systemInfo(), agent: agentInfo() };
async function getSystemInfo() {
return { app: appInfo(), system: await systemInfo(), agent: agentInfo() };
}
function getConfigRedacted(args) {
@@ -65,7 +65,7 @@ function createCollectors(deps) {
return { section, note, config: _deepRedact(pick, secrets) };
}
function listLogs() {
async function listLogs() {
const paths = getAllLogPaths();
const dir = paths.logDir;
const files = [];
@@ -78,7 +78,7 @@ function createCollectors(deps) {
const fp = backup === 0 ? base : getRotatedLogPath(base, backup);
readableNames.add(path.basename(fp));
try {
const st = fs.statSync(fp);
const st = await fs.promises.stat(fp);
variants.push({ backup, sizeBytes: st.size, mtime: st.mtime.toISOString() });
} catch {}
}
@@ -86,26 +86,28 @@ function createCollectors(deps) {
}
let siblings = [];
try {
siblings = fs.readdirSync(dir)
siblings = (await fs.promises.readdir(dir))
.filter(f => /\.log(\.\d+)?$/i.test(f))
.filter(f => !readableNames.has(f));
siblings = siblings.map(f => {
const otherLogs = [];
for (const f of siblings) {
let size = 0, mtime = null;
try { const st = fs.statSync(path.join(dir, f)); size = st.size; mtime = st.mtime.toISOString(); } catch {}
return { name: f, readable: false, sizeBytes: size, mtime };
});
try { const st = await fs.promises.stat(path.join(dir, f)); size = st.size; mtime = st.mtime.toISOString(); } catch {}
otherLogs.push({ name: f, readable: false, sizeBytes: size, mtime });
}
siblings = otherLogs;
} catch {}
return { dir, files, otherLogs: siblings };
}
function readLog(args) {
async function readLog(args, context = {}) {
const a = args || {};
const name = a.name;
const p = _resolveLogPath(name, a.backup);
if (!p) return { ok: false, error: `unknown or non-readable log: ${name}` };
const tailKb = Math.min(Math.max(Number(a.tailKb) || 256, 1), 1024);
const raw = support.collectFile(p, name, tailKb * 1024);
let content = support.redactLogText(raw, _secrets());
const snapshot = await support.collectFileSnapshot(p, name, tailKb * 1024, { signal: context.signal });
let content = support.redactLogText(snapshot.text, _secrets());
let matchedLines;
if (a.grep && typeof a.grep === 'string' && a.grep.length <= 200) {
const terms = a.grep.split('|').map(s => s.trim().toLowerCase()).filter(Boolean);
@@ -118,19 +120,17 @@ function createCollectors(deps) {
content = lines.join('\n');
}
}
let sizeBytes = null;
try { sizeBytes = fs.statSync(p).size; } catch {}
return { name, path: p, sizeBytes, returnedBytes: Buffer.byteLength(content), tailKb, matchedLines, content };
return { name, path: p, sizeBytes: snapshot.sizeBytes, returnedBytes: Buffer.byteLength(content), tailKb, matchedLines, content };
}
function getAppEvents(args) {
async function getAppEvents(args, context = {}) {
const limit = Math.min(Math.max(Number(args && args.limit) || 50, 1), 500);
const out = [];
const secrets = _secrets();
for (const name of ['crash', 'debug']) {
const p = _resolveLogPath(name);
if (!p) continue;
const raw = support.redactLogText(support.collectFile(p, name, 256 * 1024), secrets);
const raw = support.redactLogText(await support.collectFile(p, name, 256 * 1024, { signal: context.signal }), secrets);
const lines = raw.split('\n').filter(l => l.trim() && !l.startsWith('==='));
for (const line of lines.slice(-limit)) out.push({ source: name, text: line });
}
@@ -250,7 +250,7 @@ function createCollectors(deps) {
};
}
function serverHealth(args) {
async function serverHealth(args) {
const a = args || {};
const errorLimit = Math.min(Math.max(Number(a.errorLimit) || 20, 1), 200);
const errArgs = Number.isFinite(a.errorSinceMs) ? { sinceMs: a.errorSinceMs, limit: errorLimit } : { limit: errorLimit };
@@ -261,13 +261,13 @@ function createCollectors(deps) {
if (queue.source === 'persisted' && queue.stale) warnings.push('queue state is from the persisted snapshot (may lag live state; UploadManager not introspected in this build).');
if (errors.total > 0) warnings.push(`${errors.total} non-success result(s) in the error window.`);
return {
server: getSystemInfo(),
server: await getSystemInfo(),
queue,
recentBatches: history.batches,
perHoster: history.perHoster,
errors,
hosters: getHealth(),
logs: listLogs(),
logs: await listLogs(),
warnings
};
}
+6 -3
View File
@@ -143,11 +143,14 @@ function createMcpApp({ loadConfig, invoke, invokeGuarded = (_context, channel,
mhu_history_prune: args => invoke('prune-history', { retention: args.retention, dryRun: false }),
mhu_history_summary: args => collectors.getHistory({ limit: args.limit || 20 }),
mhu_history_clear: () => invoke('clear-history'),
mhu_diagnostics_summary: () => ({ system: collectors.getSystemInfo(), health: collectors.getHealth(), rotation: collectors.getRotationState(), server: collectors.serverHealth({ errorLimit: 20 }), runtime: getRuntimeDiagnostics() }),
mhu_app_events: args => collectors.getAppEvents(args),
mhu_diagnostics_summary: async () => {
const server = await collectors.serverHealth({ errorLimit: 20 });
return { system: server.server, health: collectors.getHealth(), rotation: collectors.getRotationState(), server, runtime: getRuntimeDiagnostics() };
},
mhu_app_events: (args, context) => collectors.getAppEvents(args, context),
mhu_errors: args => collectors.listErrors(args),
mhu_logs_list: () => collectors.listLogs(),
mhu_logs_read: args => collectors.readLog(args),
mhu_logs_read: (args, context) => collectors.readLog(args, context),
mhu_cloud_status: () => invoke('cloud-log:status'),
mhu_cloud_configure: args => invoke('cloud-log:configure', args),
mhu_cloud_backfill: () => invoke('cloud-log:backfill'),
+3 -3
View File
@@ -7,7 +7,7 @@ const { McpAccessError } = require('./mcp-access');
function createMcpDiagnostics({ getSources = () => [], redact }) {
let reading = 0;
const guard = context => { if (context.signal?.aborted || !Number.isFinite(context.deadline) || Date.now() >= context.deadline) throw new McpAccessError('REQUEST_EXPIRED'); };
const sources = () => getSources().slice(0, 64).map(source => ({ ...source, id: createHash('sha256').update(source.path).digest('hex').slice(0, 24) }));
const sources = async () => (await getSources()).slice(0, 64).map(source => ({ ...source, id: createHash('sha256').update(source.path).digest('hex').slice(0, 24) }));
const resolve = async source => {
const actual = await fs.realpath(source.path);
const parent = await fs.realpath(source.directory);
@@ -21,7 +21,7 @@ function createMcpDiagnostics({ getSources = () => [], redact }) {
return {
mhu_diagnostics_files: async (args, context) => {
const files = [];
for (const source of sources()) {
for (const source of await sources()) {
guard(context);
try { const { stat } = await resolve(source); files.push({ id: source.id, name: source.label, sizeBytes: stat.size, modifiedAt: stat.mtime.toISOString() }); }
catch (error) { if (!['ENOENT', 'ENOTDIR'].includes(error.code)) files.push({ id: source.id, name: source.label, unavailable: true }); }
@@ -31,7 +31,7 @@ function createMcpDiagnostics({ getSources = () => [], redact }) {
},
mhu_diagnostics_read: async (args, context) => {
guard(context);
const source = sources().find(item => item.id === args.artifactId);
const source = (await sources()).find(item => item.id === args.artifactId);
if (!source) throw new McpAccessError('DIAGNOSTIC_NOT_FOUND');
if (reading >= 4) throw new McpAccessError('TOO_MANY_REQUESTS');
reading++;
+30 -1
View File
@@ -180,8 +180,37 @@ function createNativeStartupLog({ fs, path, directory, commandLine, now = Date.n
return target;
} catch { return null; }
};
const listAsync = async pattern => {
const found = [];
try {
for (const entry of await fs.promises.readdir(logDirectory, { withFileTypes: true })) {
if (!entry.isFile() || !pattern.test(entry.name)) continue;
try {
const target = path.join(logDirectory, entry.name);
found.push({ name: entry.name, path: target, modified: (await fs.promises.stat(target)).mtimeMs });
} catch {}
}
} catch {}
return found.sort((a, b) => b.modified - a.modified || b.name.localeCompare(a.name));
};
const filesAsync = async () => (await listAsync(nativePattern)).sort((a, b) => Number(b.path === nativePath) - Number(a.path === nativePath))
.map(file => ({ label: file.name, path: file.path, maxBytes }));
const reportsAsync = async () => (await listAsync(reportPattern)).sort((a, b) => Number(b.path === latestReport) - Number(a.path === latestReport))
.map(file => ({ label: file.name, path: file.path, maxBytes: 5 * 1024 * 1024 }));
const saveReportAsync = async text => {
if (typeof text !== 'string' || Buffer.byteLength(text, 'utf8') > 5 * 1024 * 1024) return null;
try {
await fs.promises.mkdir(logDirectory, { recursive: true });
const target = path.join(logDirectory, `report-${now()}-${pid}-${reportSequence++}.txt`);
await fs.promises.writeFile(target, text, { flag: 'wx', mode: 0o600 });
latestReport = target;
const entries = await listAsync(reportPattern);
for (const file of entries.filter(file => file.path !== target).slice(9)) await fs.promises.unlink(file.path).catch(() => {});
return target;
} catch { return null; }
};
prune(reportPattern, 10);
return { enabled, nativePath, logDirectory, files, reports, saveReport, maintain, maintainAsync };
return { enabled, nativePath, logDirectory, files, filesAsync, reports, reportsAsync, saveReport, saveReportAsync, maintain, maintainAsync };
}
module.exports = { createStartupWindow, resolveStartupLanguage, createStartupQuery, createStartupDiagnostics, summarizeStartupPixels, createNativeStartupLog };
+45 -26
View File
@@ -69,37 +69,56 @@ function valueScrub(value, secrets) {
return JSON.parse(scrubbed);
}
function collectFile(filePath, label, maxBytes, options) {
async function collectFileSnapshot(filePath, label, maxBytes, options) {
const includePath = !options || options.includePath !== false;
if (!filePath) return `=== ${label} ===\n<no path configured>\n\n`;
const signal = options?.signal;
if (!filePath) return { text: `=== ${label} ===\n<no path configured>\n\n`, sizeBytes: null };
let stat;
try { stat = fs.statSync(filePath); }
catch (err) {
const context = includePath ? ` (${filePath})` : '';
if (err && err.code === 'ENOENT') return `=== ${label}${context} ===\n<file does not exist yet>\n\n`;
return `=== ${label}${context} ===\n<stat error: ${err.message}>\n\n`;
}
const cap = Number.isFinite(maxBytes) && maxBytes > 0 ? maxBytes : 5 * 1024 * 1024;
let content;
let file;
try {
if (stat.size > cap) {
const fd = fs.openSync(filePath, 'r');
const buf = Buffer.alloc(cap);
fs.readSync(fd, buf, 0, cap, stat.size - cap);
fs.closeSync(fd);
const skipped = stat.size - cap;
content = `<truncated: skipped first ${skipped} bytes; showing last ${cap} bytes of ${stat.size}>\n` + buf.toString('utf-8');
} else {
content = fs.readFileSync(filePath, 'utf-8');
signal?.throwIfAborted();
try {
file = await fs.promises.open(filePath, 'r');
stat = await file.stat();
} catch (err) {
signal?.throwIfAborted();
const context = includePath ? ` (${filePath})` : '';
const message = err?.code === 'ENOENT' ? '<file does not exist yet>' : `<stat error: ${err.message}>`;
return { text: `=== ${label}${context} ===\n${message}\n\n`, sizeBytes: null };
}
} catch (err) {
content = `<read error: ${err.message}>`;
const cap = Number.isFinite(maxBytes) && maxBytes > 0 ? Math.max(1, Math.floor(maxBytes)) : 5 * 1024 * 1024;
let content;
try {
signal?.throwIfAborted();
if (!stat.isFile()) throw new Error('Not a regular file');
const size = Math.min(stat.size, cap);
const start = Math.max(0, stat.size - size);
const buffer = Buffer.alloc(size);
let offset = 0;
while (offset < size) {
signal?.throwIfAborted();
const { bytesRead } = await file.read(buffer, offset, Math.min(size - offset, 256 * 1024), start + offset);
if (!bytesRead) break;
offset += bytesRead;
}
signal?.throwIfAborted();
content = (start > 0 ? `<truncated: skipped first ${start} bytes; showing last ${size} bytes of ${stat.size}>\n` : '') + buffer.subarray(0, offset).toString('utf-8');
} catch (err) {
signal?.throwIfAborted();
content = `<read error: ${err.message}>`;
}
const metadata = includePath ? `${filePath}, size=${stat.size} bytes` : `size=${stat.size} bytes`;
return { text: `=== ${label} (${metadata}) ===\n${content}\n\n`, sizeBytes: stat.size };
} finally {
await file?.close().catch(() => {});
}
const metadata = includePath ? `${filePath}, size=${stat.size} bytes` : `size=${stat.size} bytes`;
return `=== ${label} (${metadata}) ===\n${content}\n\n`;
}
function buildSupportBundleText({ header, sanitizedConfig, files, secrets }) {
async function collectFile(filePath, label, maxBytes, options) {
return (await collectFileSnapshot(filePath, label, maxBytes, options)).text;
}
async function buildSupportBundleText({ header, sanitizedConfig, files, secrets }) {
const parts = [];
parts.push('=== Multi-Hoster-Upload Support Bundle ===\n');
if (header && typeof header === 'object') {
@@ -110,9 +129,9 @@ function buildSupportBundleText({ header, sanitizedConfig, files, secrets }) {
parts.push(redactLogText(JSON.stringify(sanitizedConfig, null, 2), secrets));
parts.push('\n\n');
for (const f of (files || [])) {
parts.push(redactLogText(collectFile(f.path, f.label || f.path, f.maxBytes, { includePath: false }), secrets));
parts.push(redactLogText(await collectFile(f.path, f.label || f.path, f.maxBytes, { includePath: false }), secrets));
}
return parts.join('');
}
module.exports = { sanitizeConfig, collectSecretValues, redactLogText, valueScrub, collectFile, buildSupportBundleText, CRED_KEYS, REDACTED };
module.exports = { sanitizeConfig, collectSecretValues, redactLogText, valueScrub, collectFile, collectFileSnapshot, buildSupportBundleText, CRED_KEYS, REDACTED };