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 };
+20 -20
View File
@@ -37,7 +37,7 @@ const { hosterLogToFileEnabled } = require('./lib/log-policy');
const { formatUploadLogLine, iterateUploadLogEntries, summarizeBatchPlan, formatUploadPlanLogLine } = require('./lib/upload-log');
const { createInternalLogPathResolver, createInternalLogWriter, createUploadAuditWriter, createBufferedInternalLogFlusher, getLogOpenDirectory } = require('./lib/upload-audit');
const { selectOrphanTmps } = require('./lib/orphan-tmp');
const { sanitizeConfig, buildSupportBundleText, collectSecretValues, redactLogText, valueScrub, collectFile, REDACTED } = require('./lib/support-bundle');
const { sanitizeConfig, buildSupportBundleText, collectSecretValues, redactLogText, valueScrub, collectFile, collectFileSnapshot, REDACTED } = require('./lib/support-bundle');
const { buildWebhookRequest, isAllAborted } = require('./lib/webhook-notify');
const stats = require('./lib/stats');
const { createCollectors } = require('./lib/diagnostics-collectors');
@@ -448,14 +448,14 @@ if (!_hasSingleInstanceLock) {
});
}
function recordStartupIncident(reason) {
async function recordStartupIncident(reason) {
const key = `${mainWindow?.id || 0}:${folderMonitorRendererGeneration}:${reason}`;
if (!nativeStartupLog || startupIncidentKeys.has(key)) return;
startupIncidentKeys.add(key);
try {
let secrets = [];
try { secrets = collectSecretValues(configStore.load()); } catch {}
const text = buildSupportBundleText({
const text = await buildSupportBundleText({
header: { Version: app.getVersion(), Electron: process.versions.electron, CreatedAt: new Date().toISOString(), Reason: reason, OS: require('os').release() },
sanitizedConfig: { included: false },
secrets,
@@ -466,7 +466,7 @@ function recordStartupIncident(reason) {
{ label: 'native Chromium errors', path: nativeStartupLog.nativePath, maxBytes: 1024 * 1024 }
]
});
const saved = nativeStartupLog.saveReport(text);
const saved = await nativeStartupLog.saveReportAsync(text);
startupDiagnostics.write(saved ? 'startup-report-saved' : 'startup-report-unavailable', { reason });
} catch {
startupDiagnostics.write('startup-report-unavailable', { reason });
@@ -716,14 +716,14 @@ function _flushRotLog() {
function getAllLogPaths() {
const configuredUpload = getLogFilePath();
const uploadTarget = _resolveUploadLogTarget();
const upload = uploadTarget ? uploadTarget.path : configuredUpload;
const upload = _cachedUploadLogKey === configuredUpload && _cachedUploadLogTarget ? _cachedUploadLogTarget.path : configuredUpload;
const debugPath = getDebugLogPath();
const rot = getRotLogPath();
const internalDirectory = path.join(app.getPath('userData'), 'logs');
const rot = _rotLogWriter.getActivePath() || path.join(internalDirectory, 'account-rotation.log');
const dir = path.dirname(debugPath);
return {
fileuploader: upload,
uploadAudit: _uploadAuditWriter.getPath(),
uploadAudit: _uploadAuditWriter.getActivePath() || path.join(internalDirectory, 'upload-audit.log'),
debug: debugPath,
accountRotation: rot,
doodstreamDebug: path.join(dir, 'doodstream-debug.log'),
@@ -3142,9 +3142,7 @@ ipcMain.handle('set-log-verbose', (_event, enabled) => {
async function createSupportBundle() {
const { dialog } = require('electron');
try {
if (_debugLogBuffer.length) {
try { fs.appendFileSync(getDebugLogPath(), _debugLogBuffer.join(''), 'utf-8'); _debugLogBuffer.length = 0; } catch {}
}
await _flushDebugLog();
const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
const defaultName = `multi-hoster-support-${stamp}.txt`;
const desktop = (() => { try { return app.getPath('desktop'); } catch { return app.getPath('userData'); } })();
@@ -3158,7 +3156,9 @@ async function createSupportBundle() {
try { cfg = configStore.load(); } catch { cfg = null; }
let paths = null;
try { if (cfg) paths = getAllLogPaths(); } catch {}
const text = buildSupportBundleText({
const nativeReports = nativeStartupLog ? await nativeStartupLog.reportsAsync() : [];
const nativeFiles = nativeStartupLog ? await nativeStartupLog.filesAsync() : [];
const text = await buildSupportBundleText({
header: {
App: app.getName(),
Version: app.getVersion(),
@@ -3177,8 +3177,8 @@ async function createSupportBundle() {
sanitizedConfig: sanitizeConfig(cfg),
secrets: collectSecretValues(cfg),
files: [
...(nativeStartupLog ? nativeStartupLog.reports().slice(0, 3).map(file => ({ ...file, maxBytes: 5 * 1024 * 1024 })) : []),
...(nativeStartupLog ? nativeStartupLog.files().slice(0, 4).map(file => ({ ...file, maxBytes: 1024 * 1024 })) : []),
...nativeReports.slice(0, 3).map(file => ({ ...file, maxBytes: 5 * 1024 * 1024 })),
...nativeFiles.slice(0, 4).map(file => ({ ...file, maxBytes: 1024 * 1024 })),
{ label: 'startup-diagnostics.jsonl (previous sessions)', path: startupDiagnostics.filePath + '.1', maxBytes: 1024 * 1024 },
{ label: 'startup-diagnostics.jsonl', path: startupDiagnostics.filePath, maxBytes: 1024 * 1024 },
...(cfg && paths ? [
@@ -3191,7 +3191,7 @@ async function createSupportBundle() {
] : [])
]
});
fs.writeFileSync(res.filePath, text, 'utf-8');
await fs.promises.writeFile(res.filePath, text, 'utf-8');
logMarker('SUPPORT BUNDLE', { path: res.filePath, bytes: text.length });
return { ok: true, path: res.filePath, bytes: text.length };
} catch (err) {
@@ -4079,11 +4079,11 @@ function _diagAppInfo() {
};
}
function _diagSystemInfo() {
async function _diagSystemInfo() {
const os = require('os');
let disk = null;
try {
const sf = fs.statfsSync(app.getPath('userData'));
const sf = await fs.promises.statfs(app.getPath('userData'));
disk = { freeBytes: sf.bavail * sf.bsize, totalBytes: sf.blocks * sf.bsize };
} catch {}
return {
@@ -4108,7 +4108,7 @@ function _diagAgentInfo() {
function createAppCollectors() {
return createCollectors({
loadConfig: () => configStore.load(), loadHistory: () => configStore.loadHistory(), getAllLogPaths,
support: { sanitizeConfig, collectSecretValues, redactLogText, valueScrub, collectFile, REDACTED },
support: { sanitizeConfig, collectSecretValues, redactLogText, valueScrub, collectFile, collectFileSnapshot, REDACTED },
stats, appInfo: _diagAppInfo, systemInfo: _diagSystemInfo, agentInfo: _diagAgentInfo
});
}
@@ -4164,10 +4164,10 @@ async function startMcpService() {
renderer: { available: !!mainWindow && !mainWindow.isDestroyed(), initialized: closeHandshakeReady, generation: folderMonitorRendererGeneration },
memory: process.memoryUsage()
}),
getDiagnosticSources: () => [
getDiagnosticSources: async () => [
{ label: 'startup-diagnostics.jsonl', path: startupDiagnostics.filePath, directory: path.dirname(startupDiagnostics.filePath) },
{ label: 'startup-diagnostics.jsonl.1', path: startupDiagnostics.filePath + '.1', directory: path.dirname(startupDiagnostics.filePath) },
...(nativeStartupLog ? [...nativeStartupLog.files(), ...nativeStartupLog.reports()].map(file => ({ ...file, directory: nativeStartupLog.logDirectory })) : [])
...(nativeStartupLog ? [...await nativeStartupLog.filesAsync(), ...await nativeStartupLog.reportsAsync()].map(file => ({ ...file, directory: nativeStartupLog.logDirectory })) : [])
] });
mcpService = new McpService({ directory: app.getPath('userData'), encrypt: secretStore.encryptField, decrypt: secretStore.decryptField,
handlers, version: app.getVersion(), loadConfig: () => configStore.load(), dialog, getWindow: () => mainWindow,
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "multi-hoster-uploader",
"version": "2.1.68",
"version": "2.1.69",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "multi-hoster-uploader",
"version": "2.1.68",
"version": "2.1.69",
"dependencies": {
"chokidar": "^4.0.3",
"undici": "7.29.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "multi-hoster-uploader",
"version": "2.1.68",
"version": "2.1.69",
"description": "Upload files to doodstream, voe, vidmoly, byse simultaneously",
"main": "main.js",
"scripts": {