mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 04:11:35 +00:00
`pumpStreamToFile` opened the user-chosen destination with `fs.createWriteStream`, which truncates the target the instant it opens, and its error path then unlinked that same path. When a user picked an existing file in the Save dialog (and confirmed the overwrite) and the gateway dropped mid-stream, the original was gone: truncated first, deleted second, with nothing written in its place. The data-URL compatibility fallback (`saveGatewayFileViaDataUrl`) had the same class of bug via `fs.promises.writeFile`, which truncates before the write completes. Both paths now go through one failure-atomic primitive. Bytes land in a short, randomly named sibling temp file (`.hermes-download-<hex>.part`, same directory so the final step is a same-volume rename), created with `flags: 'wx'`, and are renamed onto the destination only after the whole body has been written and the descriptor released. The destination is never opened before that point, so a failed download leaves whatever was there untouched. - Ownership-gated cleanup: the temp file is unlinked only after the stream's 'open' event proved THIS operation created it. An exclusive create that fails before open (EEXIST collision, EACCES, missing parent) never removes a file that belongs to someone else. - `WriteStream.close(cb)` rather than `end(cb)` before renaming: `end`'s callback fires on 'finish' while the fd may still be open, and Windows refuses to rename a file with an open handle. Falls back to `end` for stream shapes without `close`. - The failure path waits for 'close' (bounded by a 2s grace period) before unlinking, for the same reason: `destroy()` releases the fd asynchronously and an unlink racing the open handle would leak the `.part` file on Windows. - A rename failure (destination locked, permissions) removes the owned temp file and rejects; nothing is left behind. - Fixed-length temp name so a long user-chosen filename cannot push it past the filesystem's name limit. - `fsPumpDeps()` is the single production deps factory (`'wx'` create, `fs.promises.rename`, `fs.promises.unlink`); `writeBufferToFile()` routes the data-URL fallback through the same pump. `PumpDeps` gains `rename` and a `tempPathFor` test seam. Tests. Fakes: temp-then-rename on success, close-before-rename ordering, the regression itself (destination neither opened nor unlinked when the response fails mid-stream), write-error cleanup, close-before-unlink ordering, rename-failure cleanup, pre-open EEXIST leaves the colliding file alone, `writeBufferToFile` success and post-open write failure, the temp-name length bound, and the `main.ts` wiring. Real filesystem (`gateway-file-download.fs.test.ts`, exact production deps in a scratch dir): completed download replaces the destination with no temp left; mid-stream failure leaves the pre-existing destination byte-for-byte with no `.part`; failure into a fresh name leaves nothing; seeded temp path survives a pre-open EEXIST with no rename; rename failure (directory at the destination) cleans the owned temp; data-URL fallback success and missing-directory failure. Adds the contributor email mapping required by the attribution check. Fixes #96597 Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_015u8q2pHVPZmxpSrgkt94jC
152 lines
5.4 KiB
TypeScript
152 lines
5.4 KiB
TypeScript
// Real-filesystem witnesses for the failure-atomic save contract (#96597).
|
|
//
|
|
// The unit tests in gateway-file-download.test.ts prove the pump's control flow
|
|
// against fakes. These run the exact production deps (`fsPumpDeps()`) against
|
|
// node:fs in a scratch directory and assert the user-visible invariants
|
|
// byte-for-byte: a pre-existing destination survives every failure mode this
|
|
// harness can force, a pre-existing file at the temp name survives a pre-open
|
|
// collision, and no owned `.part` file is ever left behind.
|
|
|
|
import assert from 'node:assert/strict'
|
|
import fs from 'node:fs'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import { Readable } from 'node:stream'
|
|
|
|
import { afterEach, beforeEach, test } from 'vitest'
|
|
|
|
import { fsPumpDeps, pumpStreamToFile, writeBufferToFile } from './gateway-file-download'
|
|
|
|
let dir = ''
|
|
|
|
beforeEach(async () => {
|
|
dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'hermes-download-fs-'))
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await fs.promises.rm(dir, { force: true, recursive: true })
|
|
})
|
|
|
|
// A body that delivers `chunks` then fails with `error` (or ends cleanly when
|
|
// `error` is omitted). Readable satisfies the pump's ReadableLike shape.
|
|
function body(chunks: string[], error?: Error): Readable {
|
|
let i = 0
|
|
|
|
return new Readable({
|
|
read() {
|
|
if (i < chunks.length) {
|
|
this.push(Buffer.from(chunks[i++]))
|
|
|
|
return
|
|
}
|
|
|
|
if (error) {
|
|
this.destroy(error)
|
|
|
|
return
|
|
}
|
|
|
|
this.push(null)
|
|
}
|
|
})
|
|
}
|
|
|
|
async function listing(): Promise<string[]> {
|
|
return (await fs.promises.readdir(dir)).sort()
|
|
}
|
|
|
|
test('a completed download replaces the destination and leaves no temp file', async () => {
|
|
const dest = path.join(dir, 'report.bin')
|
|
|
|
await fs.promises.writeFile(dest, 'OLD CONTENT')
|
|
|
|
await pumpStreamToFile(body(['new ', 'content']), dest, fsPumpDeps())
|
|
|
|
assert.equal(await fs.promises.readFile(dest, 'utf8'), 'new content')
|
|
assert.deepEqual(await listing(), ['report.bin'])
|
|
})
|
|
|
|
test('a download that fails mid-stream leaves the pre-existing destination byte-for-byte and no temp file', async () => {
|
|
const dest = path.join(dir, 'report.bin')
|
|
const original = Buffer.from('OLD CONTENT THAT MUST SURVIVE')
|
|
|
|
await fs.promises.writeFile(dest, original)
|
|
|
|
await assert.rejects(
|
|
pumpStreamToFile(body(['partial'], new Error('socket hang up')), dest, fsPumpDeps()),
|
|
/socket hang up/
|
|
)
|
|
|
|
assert.ok(original.equals(await fs.promises.readFile(dest)), 'destination bytes must be unchanged')
|
|
assert.deepEqual(await listing(), ['report.bin'], 'no .part file may remain')
|
|
})
|
|
|
|
test('a download into a name with no existing file that fails leaves nothing behind', async () => {
|
|
const dest = path.join(dir, 'fresh.bin')
|
|
|
|
await assert.rejects(pumpStreamToFile(body(['partial'], new Error('reset')), dest, fsPumpDeps()), /reset/)
|
|
|
|
assert.deepEqual(await listing(), [])
|
|
})
|
|
|
|
// The reviewer-requested regression: seed the candidate temp path with known
|
|
// bytes, force the exclusive open to fail with EEXIST, and prove those bytes
|
|
// remain untouched and no rename occurred.
|
|
test('a pre-open EEXIST collision leaves the seeded temp file and the destination untouched', async () => {
|
|
const dest = path.join(dir, 'report.bin')
|
|
const pinnedTemp = path.join(dir, '.hermes-download-pinned.part')
|
|
const seeded = Buffer.from('SOMEONE ELSES BYTES')
|
|
const original = Buffer.from('OLD CONTENT')
|
|
|
|
await fs.promises.writeFile(dest, original)
|
|
await fs.promises.writeFile(pinnedTemp, seeded)
|
|
|
|
const deps = { ...fsPumpDeps(), tempPathFor: () => pinnedTemp }
|
|
|
|
await assert.rejects(pumpStreamToFile(body(['new content']), dest, deps), (err: NodeJS.ErrnoException) => {
|
|
assert.equal(err.code, 'EEXIST')
|
|
|
|
return true
|
|
})
|
|
|
|
assert.ok(seeded.equals(await fs.promises.readFile(pinnedTemp)), 'the colliding file must not be unlinked')
|
|
assert.ok(original.equals(await fs.promises.readFile(dest)), 'destination must not be renamed over')
|
|
assert.deepEqual(await listing(), ['.hermes-download-pinned.part', 'report.bin'])
|
|
})
|
|
|
|
test('a failed final rename removes the owned temp file and leaves the destination as it was', async () => {
|
|
// A directory at the destination makes rename(2) fail on every platform.
|
|
const dest = path.join(dir, 'report.bin')
|
|
|
|
await fs.promises.mkdir(dest)
|
|
await fs.promises.writeFile(path.join(dest, 'keep.txt'), 'inside')
|
|
|
|
await assert.rejects(pumpStreamToFile(body(['new content']), dest, fsPumpDeps()))
|
|
|
|
assert.ok((await fs.promises.stat(dest)).isDirectory(), 'destination directory must survive')
|
|
assert.equal(await fs.promises.readFile(path.join(dest, 'keep.txt'), 'utf8'), 'inside')
|
|
assert.deepEqual(await listing(), ['report.bin'], 'the owned temp file must be cleaned up')
|
|
})
|
|
|
|
test('writeBufferToFile replaces the destination atomically and leaves no temp file', async () => {
|
|
const dest = path.join(dir, 'fallback.bin')
|
|
|
|
await fs.promises.writeFile(dest, 'OLD CONTENT')
|
|
|
|
await writeBufferToFile(Buffer.from('data-url payload'), dest, fsPumpDeps())
|
|
|
|
assert.equal(await fs.promises.readFile(dest, 'utf8'), 'data-url payload')
|
|
assert.deepEqual(await listing(), ['fallback.bin'])
|
|
})
|
|
|
|
test('writeBufferToFile into a missing directory fails without creating anything', async () => {
|
|
const dest = path.join(dir, 'missing-subdir', 'fallback.bin')
|
|
|
|
await assert.rejects(writeBufferToFile(Buffer.from('payload'), dest, fsPumpDeps()), (err: NodeJS.ErrnoException) => {
|
|
assert.equal(err.code, 'ENOENT')
|
|
|
|
return true
|
|
})
|
|
|
|
assert.deepEqual(await listing(), [])
|
|
})
|