diff --git a/libs/research/src/notebook-snapshot.ts b/libs/research/src/notebook-snapshot.ts index f7ed610d..f6af2521 100644 --- a/libs/research/src/notebook-snapshot.ts +++ b/libs/research/src/notebook-snapshot.ts @@ -255,14 +255,9 @@ function assertWithinRoot( } } -function splitDirAndBase(normalizedFilePath: string): { - readonly dir: string; - readonly base: string; -} { +function dirnamePosix(normalizedFilePath: string): string { const lastSlash = normalizedFilePath.lastIndexOf('/'); - const dir = lastSlash <= 0 ? '/' : normalizedFilePath.slice(0, lastSlash); - const base = normalizedFilePath.slice(lastSlash + 1); - return { dir, base }; + return lastSlash <= 0 ? '/' : normalizedFilePath.slice(0, lastSlash); } function validatePortsAndOptions(options: ExportNotebookSnapshotOptions): { @@ -530,11 +525,11 @@ function generateTempToken(): string { } async function reserveSiblingTempHtml(fs: NotebookSnapshotFs, outputPath: string): Promise { - const { dir, base } = splitDirAndBase(outputPath); + const dir = dirnamePosix(outputPath); const dirPrefix = dir === '/' ? '' : dir; for (let attempt = 0; attempt < MAX_TEMP_RESERVATION_ATTEMPTS; attempt += 1) { - const candidate = `${dirPrefix}/.${base}.${generateTempToken()}.tmp.html`; + const candidate = `${dirPrefix}/.marimo-snapshot-${generateTempToken()}.tmp.html`; try { await fs.createExclusive(candidate); return candidate; @@ -636,12 +631,12 @@ export async function exportNotebookSnapshot( 'check' ); - const trimmedCheckStdout = checkResult.stdout.trim(); + const hasCheckStdout = checkResult.stdout.length > 0; let parsedCheckJson: unknown = undefined; let checkJsonParseError: unknown = undefined; - if (trimmedCheckStdout.length > 0) { + if (hasCheckStdout) { try { - parsedCheckJson = JSON.parse(trimmedCheckStdout); + parsedCheckJson = JSON.parse(checkResult.stdout); } catch (err) { checkJsonParseError = err; } @@ -665,7 +660,7 @@ export async function exportNotebookSnapshot( } let checkDiagnostics: MarimoCheckReport | null = null; - if (trimmedCheckStdout.length > 0) { + if (hasCheckStdout) { if (checkJsonParseError !== undefined) { throw new NotebookSnapshotError( 'CHECK_OUTPUT_INVALID', diff --git a/libs/research/test/notebook-snapshot.test.ts b/libs/research/test/notebook-snapshot.test.ts index 04531912..3e5e31b0 100644 --- a/libs/research/test/notebook-snapshot.test.ts +++ b/libs/research/test/notebook-snapshot.test.ts @@ -335,6 +335,33 @@ describe('exportNotebookSnapshot', () => { expect(calls).toHaveLength(1); }); + test('never invokes export when check exits 0 with whitespace-only stdout', async () => { + for (const stdout of ['\n', ' \t\r\n']) { + const fs = new InMemoryNotebookFs(); + fs.putFile('/work/nb.py', 'import marimo\n'); + fs.putFile('/work/out.html', 'existing-snapshot'); + const { runner, calls } = createRecordingRunner(async () => ({ + exitCode: 0, + stdout, + stderr: '', + })); + + await expectSnapshotError( + exportNotebookSnapshot({ + notebookPath: '/work/nb.py', + outputPath: '/work/out.html', + runner, + fs, + }), + 'CHECK_OUTPUT_INVALID' + ); + expect(calls).toHaveLength(1); + expect(calls[0]?.args[0]).toBe('check'); + expect(fs.createdTempPaths).toHaveLength(0); + expect(fs.getText('/work/out.html')).toBe('existing-snapshot'); + } + }); + test('never invokes export when check exits 0 with malformed JSON or invalid report shape', async () => { const badOutputs = [ '{not-valid-json', @@ -538,7 +565,39 @@ describe('exportNotebookSnapshot', () => { expect(fs.getText('/work/out.html')).toBe('keep-original'); }); - test('fail-closed export: rename failure throws EXPORT_FAILED, removes temp, preserves destination, and preserves primary error if cleanup fails', async () => { + test('fail-closed export: rename failure throws EXPORT_FAILED, removes temp, and preserves destination', async () => { + const fs = new InMemoryNotebookFs(); + fs.putFile('/work/nb.py', 'import marimo\n'); + fs.putFile('/work/out.html', 'prior-snapshot'); + fs.failRenameError = new Error('EXDEV: cross-device link not permitted'); + + const { runner } = createRecordingRunner(async (_cmd, args) => { + if (args[0] === 'check') { + return { exitCode: 0, stdout: '', stderr: '' }; + } + const tempPath = args[args.indexOf('-o') + 1] as string; + fs.putFile(tempPath, 'new-snapshot'); + return { exitCode: 0, stdout: '', stderr: '' }; + }); + + const err = await expectSnapshotError( + exportNotebookSnapshot({ + notebookPath: '/work/nb.py', + outputPath: '/work/out.html', + runner, + fs, + }), + 'EXPORT_FAILED' + ); + + const tempPath = fs.createdTempPaths[0] as string; + expect(err.cause).toBe(fs.failRenameError); + expect(fs.removedPaths).toContain(tempPath); + expect(fs.getBytes(tempPath)).toBeUndefined(); + expect(fs.getText('/work/out.html')).toBe('prior-snapshot'); + }); + + test('fail-closed export: rename failure preserves primary error if temp cleanup also fails', async () => { const fs = new InMemoryNotebookFs(); fs.putFile('/work/nb.py', 'import marimo\n'); fs.putFile('/work/out.html', 'prior-snapshot'); @@ -569,6 +628,33 @@ describe('exportNotebookSnapshot', () => { expect(fs.getText('/work/out.html')).toBe('prior-snapshot'); }); + test('uses a short sibling temp filename even when output basename is near POSIX NAME_MAX', async () => { + const fs = new InMemoryNotebookFs(); + const notebookPath = '/work/nb.py'; + const longBasename = `${'a'.repeat(250)}.html`; + const outputPath = `/work/${longBasename}`; + fs.putFile(notebookPath, 'import marimo\n'); + + const { runner } = createRecordingRunner(async (_cmd, args) => { + if (args[0] === 'check') { + return { exitCode: 0, stdout: '', stderr: '' }; + } + const tempPath = args[args.indexOf('-o') + 1] as string; + fs.putFile(tempPath, 'long-name-ok'); + return { exitCode: 0, stdout: '', stderr: '' }; + }); + + await exportNotebookSnapshot({ notebookPath, outputPath, runner, fs }); + + expect(fs.createdTempPaths).toHaveLength(1); + const tempPath = fs.createdTempPaths[0] as string; + const tempBasename = tempPath.slice(tempPath.lastIndexOf('/') + 1); + expect(tempPath.startsWith('/work/')).toBe(true); + expect(tempBasename.endsWith('.html')).toBe(true); + expect(tempBasename.length).toBeLessThan(128); + expect(fs.getText(outputPath)).toBe('long-name-ok'); + }); + test('overlapping exports to the same outputPath reserve separate temp files without partial publication', async () => { const fs = new InMemoryNotebookFs(); fs.putFile('/work/nb.py', 'import marimo\n');