diff --git a/scripts/lib/control-pane/action-output.js b/scripts/lib/control-pane/action-output.js new file mode 100644 index 000000000..420bfb737 --- /dev/null +++ b/scripts/lib/control-pane/action-output.js @@ -0,0 +1,26 @@ +'use strict'; + +const OUTPUT_LIMIT = 20000; + +/** Drain an owned child pipe while retaining only the response's output prefix. */ +function captureActionOutput(stream) { + let prefix = ''; + let totalLength = 0; + + // Readable's decoder preserves UTF-8 split across chunks and flushes an + // incomplete final sequence at EOF. Counts retain the existing UTF-16 units. + stream.setEncoding('utf8'); + stream.on('data', text => { + totalLength += text.length; + if (prefix.length < OUTPUT_LIMIT) { + prefix += text.slice(0, OUTPUT_LIMIT - prefix.length); + } + // Keep consuming after the limit so a full pipe cannot stall the child. + }); + + return () => totalLength > OUTPUT_LIMIT + ? `${prefix}\n[truncated ${totalLength - OUTPUT_LIMIT} chars]` + : prefix; +} + +module.exports = { captureActionOutput }; diff --git a/scripts/lib/control-pane/server.js b/scripts/lib/control-pane/server.js index cf4d73168..3e5a1d827 100644 --- a/scripts/lib/control-pane/server.js +++ b/scripts/lib/control-pane/server.js @@ -6,6 +6,7 @@ const path = require('path'); const { spawn } = require('child_process'); const { buildControlPaneAction } = require('./actions'); +const { captureActionOutput } = require('./action-output'); const { buildControlPaneSnapshot, resolveControlPaneConfig } = require('./state'); const { renderControlPaneHtml } = require('./ui'); const { renderProximityVizHtml } = require('./proximity-viz'); @@ -98,12 +99,6 @@ async function readRequestJson(req) { return JSON.parse(raw); } -function boundedOutput(value, limit = 20000) { - const text = String(value || ''); - if (text.length <= limit) return text; - return `${text.slice(0, limit)}\n[truncated ${text.length - limit} chars]`; -} - function runAction(action, options = {}) { const timeoutMs = options.timeoutMs || 120000; return new Promise(resolve => { @@ -113,8 +108,8 @@ function runAction(action, options = {}) { env: process.env, stdio: ['ignore', 'pipe', 'pipe'] }); - let stdout = ''; - let stderr = ''; + const readStdout = captureActionOutput(child.stdout); + const readStderr = captureActionOutput(child.stderr); let settled = false; const timeout = setTimeout(() => { if (!settled) { @@ -122,12 +117,6 @@ function runAction(action, options = {}) { } }, timeoutMs); - child.stdout.on('data', chunk => { - stdout += chunk.toString('utf8'); - }); - child.stderr.on('data', chunk => { - stderr += chunk.toString('utf8'); - }); child.on('error', error => { settled = true; clearTimeout(timeout); @@ -138,8 +127,8 @@ function runAction(action, options = {}) { finishedAt: new Date().toISOString(), code: null, error: error.message, - stdout: boundedOutput(stdout), - stderr: boundedOutput(stderr) + stdout: readStdout(), + stderr: readStderr() }); }); child.on('close', (code, signal) => { @@ -152,8 +141,8 @@ function runAction(action, options = {}) { finishedAt: new Date().toISOString(), code, signal, - stdout: boundedOutput(stdout), - stderr: boundedOutput(stderr) + stdout: readStdout(), + stderr: readStderr() }); }); }); diff --git a/tests/lib/control-pane-output.test.js b/tests/lib/control-pane-output.test.js new file mode 100644 index 000000000..4de1a4b07 --- /dev/null +++ b/tests/lib/control-pane-output.test.js @@ -0,0 +1,120 @@ +/** Tests for bounded, streaming control-pane action output. */ + +const assert = require('assert'); +const { once } = require('events'); +const path = require('path'); +const { PassThrough } = require('stream'); +const { captureActionOutput } = require('../../scripts/lib/control-pane/action-output'); +const { runAction } = require('../../scripts/lib/control-pane/server'); + +const REPO_ROOT = path.join(__dirname, '..', '..'); + +async function capture(chunks) { + const stream = new PassThrough(); + const readOutput = captureActionOutput(stream); + const ended = once(stream, 'end'); + for (const chunk of chunks) stream.write(chunk); + stream.end(); + await ended; + return readOutput(); +} + +async function main() { + const tests = [ + ['preserves empty, short, and exact-limit output', async () => { + assert.strictEqual(await capture([]), ''); + assert.strictEqual(await capture([Buffer.from('hello')]), 'hello'); + assert.strictEqual(await capture([Buffer.from('x'.repeat(20000))]), 'x'.repeat(20000)); + }], + ['keeps the original prefix and counts output discarded across chunks', async () => { + const stream = new PassThrough(); + const readOutput = captureActionOutput(stream); + const ended = once(stream, 'end'); + stream.write(Buffer.from('a'.repeat(19990))); + stream.write(Buffer.from('b'.repeat(30))); + assert.strictEqual(readOutput(), `${'a'.repeat(19990)}${'b'.repeat(10)}\n[truncated 20 chars]`); + const chunk = Buffer.from('c'.repeat(65536)); + for (let index = 0; index < 256; index += 1) stream.write(chunk); + stream.end(); + await ended; + assert.strictEqual(readOutput(), `${'a'.repeat(19990)}${'b'.repeat(10)}\n[truncated ${20 + chunk.length * 256} chars]`); + }], + ['decodes UTF-8 characters split at every byte boundary', async () => { + const text = '\ud55c\ud83d\ude42\u00e9'; + const bytes = Buffer.from(text); + for (let split = 1; split < bytes.length; split += 1) { + assert.strictEqual(await capture([bytes.subarray(0, split), bytes.subarray(split)]), text); + } + assert.strictEqual(await capture([...bytes].map(byte => Buffer.from([byte]))), text); + }], + ['flushes an incomplete final UTF-8 sequence and counts UTF-16 characters', async () => { + assert.strictEqual(await capture([Buffer.from([0xed])]), '\ufffd'); + assert.strictEqual( + await capture([Buffer.from('x'.repeat(19999)), Buffer.from('\ud83d\ude42\u00e9')]), + `${'x'.repeat(19999)}\ud83d\n[truncated 2 chars]` + ); + }], + ['tracks independent streams without stopping either stream at its limit', async () => { + const stdout = new PassThrough(); + const stderr = new PassThrough(); + const readStdout = captureActionOutput(stdout); + const readStderr = captureActionOutput(stderr); + const ended = Promise.all([once(stdout, 'end'), once(stderr, 'end')]); + stdout.write(Buffer.from('out'.repeat(10000))); + stderr.write(Buffer.from('err')); + stdout.end(Buffer.from('tail')); + stderr.end(Buffer.from('or')); + await ended; + assert.strictEqual(readStdout(), `${'out'.repeat(10000).slice(0, 20000)}\n[truncated 10004 chars]`); + assert.strictEqual(readStderr(), 'error'); + }], + ['runAction drains large stdout and stderr while retaining their prefixes', async () => { + const result = await runAction({ + id: 'node-large-streams', + command: process.execPath, + args: ['-e', [ + 'process.stdout.write("x".repeat(4 * 1024 * 1024));', + 'process.stderr.write("y".repeat(4 * 1024 * 1024));', + ].join('\n')], + cwd: REPO_ROOT + }); + const suffix = `\n[truncated ${4 * 1024 * 1024 - 20000} chars]`; + assert.strictEqual(result.ok, true); + assert.strictEqual(result.code, 0); + assert.strictEqual(result.stdout, `${'x'.repeat(20000)}${suffix}`); + assert.strictEqual(result.stderr, `${'y'.repeat(20000)}${suffix}`); + }], + ['runAction preserves UTF-8 split across stdout and stderr chunks', async () => { + const result = await runAction({ + id: 'node-streamed-utf8', + command: process.execPath, + args: ['-e', [ + 'process.stdout.write(Buffer.from([0xed]));', + 'process.stderr.write(Buffer.from([0xf0, 0x9f]));', + 'setTimeout(() => {', + ' process.stdout.write(Buffer.from([0x95, 0x9c]));', + ' process.stderr.write(Buffer.from([0x99, 0x82]));', + '}, 80);', + ].join('\n')], + cwd: REPO_ROOT + }); + assert.strictEqual(result.ok, true); + assert.strictEqual(result.stdout, '\ud55c'); + assert.strictEqual(result.stderr, '\ud83d\ude42'); + }], + ]; + let failed = 0; + for (const [name, run] of tests) { + try { + await run(); + console.log(` PASS ${name}`); + } catch (error) { + failed += 1; + console.log(` FAIL ${name}\n ${error.stack}`); + } + } + console.log(`\nResults: Passed: ${tests.length - failed}, Failed: ${failed}`); + process.exitCode = failed > 0 ? 1 : 0; +} + +main().catch(error => { console.error(error); process.exitCode = 1; }); diff --git a/tests/scripts/npm-publish-surface.test.js b/tests/scripts/npm-publish-surface.test.js index 84afcb203..c0551ab19 100644 --- a/tests/scripts/npm-publish-surface.test.js +++ b/tests/scripts/npm-publish-surface.test.js @@ -261,6 +261,7 @@ function main() { "docs/design/context-carriers.md", "docs/design/context-profile-delivery.md", "scripts/control-pane.js", + "scripts/lib/control-pane/action-output.js", "scripts/lib/control-pane/work-item-runner.js", "scripts/lib/control-pane/work-item-worker.js", "scripts/lib/state-store/file-lock.js",