The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(build): wait out Windows scanner holds when publishing products
Brooklyn Nicholsonf0b9649032e9
diff --git a/apps/desktop/scripts/build-hud-modifier-monitor.mjs b/apps/desktop/scripts/build-hud-modifier-monitor.mjsindex 2b5eff582eb..f9e684268a1 100644--- a/apps/desktop/scripts/build-hud-modifier-monitor.mjs+++ b/apps/desktop/scripts/build-hud-modifier-monitor.mjs@@ -4,6 +4,7 @@ import { execFileSync } from 'node:child_process'import { chmodSync, existsSync, mkdirSync, renameSync, rmdirSync, rmSync } from 'node:fs'import { dirname, resolve } from 'node:path'import { fileURLToPath } from 'node:url'+import { retryHeld } from '../../../scripts/build/frontend-common.mjs'import { macosSysroot, xcrunClangArgv } from './macos-sysroot.mjs'const script = fileURLToPath(import.meta.url)@@ -90,7 +91,8 @@ export function buildHudModifierMonitor({)}chmodSync(staging, 0o755)- renameSync(staging, output)+ // A scanner opens the exe the compiler just wrote; wait it out like publication does.+ retryHeld(() => renameSync(staging, output))console.log(`built ${output}`)return output} catch (error) {diff --git a/scripts/build/frontend-common.mjs b/scripts/build/frontend-common.mjsindex d6d5010d2ac..213223c4b45 100644--- a/scripts/build/frontend-common.mjs+++ b/scripts/build/frontend-common.mjs@@ -74,22 +74,53 @@ export function productOutput(source, out, inputs) {return { source: src, out: dest }}+// Windows refuses to move a tree while anything holds a handle inside it, and+// an antivirus scanner opens every freshly written exe. Node reports that as+// EPERM/EACCES/EBUSY; it clears in moments, so ride it out (~3s) rather than+// throw away a build that took minutes. Other codes never clear by waiting.+const heldCodes = new Set(['EPERM', 'EACCES', 'EBUSY'])+const heldRetryDelays = [100, 200, 400, 800, 1600]++export function retryHeld(operation) {+ for (const delay of [...heldRetryDelays, undefined]) {+ try {+ return operation()+ } catch (error) {+ if (delay === undefined || !heldCodes.has(error?.code)) throw error+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay)+ }+ }+}+// Never delete the last successful product before a compiler succeeds. The-// staging and backup directories are siblings so publication stays on one FS.+// backup lives beside out, outside withProduct's scratch tree, so a failed+// rollback leaves the previous product somewhere a person can recover it.export function publishDirectory(staged, out, { source } = {}) {// The destination may have been occupied while the compiler was running.requireOwnedOutput(out, source)writeFileSync(path.join(staged, productMarker), productOwner)- const backup = `${staged}.previous`- const previous = existsSync(out)- if (previous) renameSync(out, backup)+ if (!existsSync(out)) return retryHeld(() => renameSync(staged, out))+ const backupRoot = mkdtempSync(path.join(path.dirname(out), `.${path.basename(out)}-previous-`))+ const backup = path.join(backupRoot, 'product')try {- renameSync(staged, out)+ retryHeld(() => renameSync(out, backup))} catch (error) {- if (previous) renameSync(backup, out)+ rmSync(backupRoot, { recursive: true, force: true })+ throw error+ }+ try {+ retryHeld(() => renameSync(staged, out))+ } catch (error) {+ try {+ retryHeld(() => renameSync(backup, out))+ } catch (rollbackError) {+ error.message += `; rollback failed (${rollbackError.message}); previous product kept at ${backup}`+ throw error+ }+ rmSync(backupRoot, { recursive: true, force: true })throw error}- if (previous) rmSync(backup, { recursive: true, force: true })+ rmSync(backupRoot, { recursive: true, force: true })}export async function withProduct(out, compile, { source } = {}) {diff --git a/tests-js/frontend-common-publish.test.mjs b/tests-js/frontend-common-publish.test.mjsnew file mode 100644index 00000000000..7c00e2cdc93--- /dev/null+++ b/tests-js/frontend-common-publish.test.mjs@@ -0,0 +1,91 @@+import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { afterEach, beforeEach, expect, test, vi } from 'vitest'++// Windows antivirus holds a freshly written tree (the HUD helper exe in+// native-deps, web_dist after a long build) and publication's renames fail+// EPERM until the scan ends (#126914). Fault only the renames a test names;+// every other rename reaches the real filesystem.+const faults = vi.hoisted(() => [])++vi.mock('node:fs', async importOriginal => {+ const actual = await importOriginal()+ return {+ ...actual,+ renameSync(from, to) {+ const fault = faults.find(entry => entry.match(from))+ if (fault) fault.calls+++ if (fault && fault.calls <= fault.failures) {+ throw Object.assign(new Error(`${fault.code}: rename '${from}' -> '${to}'`), { code: fault.code })+ }+ return actual.renameSync(from, to)+ },+ }+})++const { withProduct } = await import('../scripts/build/frontend-common.mjs')++let root++beforeEach(() => {+ root = mkdtempSync(join(tmpdir(), 'publish-held-'))+ vi.spyOn(Atomics, 'wait').mockReturnValue('timed-out') // the backoff is real; tests don't spend it+})++afterEach(() => {+ faults.length = 0+ vi.restoreAllMocks()+ rmSync(root, { recursive: true, force: true })+})++function fault(match, code, failures = Infinity) {+ const entry = { match: typeof match === 'string' ? from => from === match : match, code, failures, calls: 0 }+ faults.push(entry)+ return entry+}++function previousProduct() {+ const out = join(root, 'native-deps')+ mkdirSync(out)+ writeFileSync(join(out, '.hermes-product'), 'hermes-frontend-product-v1\n')+ writeFileSync(join(out, 'old.txt'), 'old')+ return out+}++test('a held staging tree publishes once the scanner lets go', async () => {+ const out = previousProduct()+ let staged+ await withProduct(out, product => {+ staged = fault(product, 'EPERM', 2)+ writeFileSync(join(product, 'hud-modifier-monitor.exe'), 'new')+ })+ expect(staged.calls).toBe(3)+ expect(readdirSync(out).sort()).toEqual(['.hermes-product', 'hud-modifier-monitor.exe'])+ expect(readdirSync(root)).toEqual(['native-deps'])+})++test('a code waiting cannot fix fails at once and keeps the previous product', async () => {+ const out = previousProduct()+ let staged+ await expect(withProduct(out, product => { staged = fault(product, 'EINVAL') })).rejects.toThrow(/EINVAL/)+ expect(staged.calls).toBe(1)+ expect(readFileSync(join(out, 'old.txt'), 'utf8')).toBe('old')+ expect(readdirSync(root)).toEqual(['native-deps'])+})++test('a hold that outlasts the budget rolls back to the previous product', async () => {+ const out = previousProduct()+ await expect(withProduct(out, product => { fault(product, 'EACCES') })).rejects.toThrow(/EACCES/)+ expect(readFileSync(join(out, 'old.txt'), 'utf8')).toBe('old')+ expect(readdirSync(root)).toEqual(['native-deps'])+})++test('a failed rollback survives scratch cleanup and names where the previous product is', async () => {+ const out = previousProduct()+ fault(from => from.includes('.native-deps-previous-'), 'EBUSY')+ const error = await withProduct(out, product => { fault(product, 'EPERM') }).catch(caught => caught)+ const backup = error.message.match(/previous product kept at (.+)$/)?.[1]+ expect(error.code).toBe('EPERM')+ expect(readFileSync(join(backup, 'old.txt'), 'utf8')).toBe('old')+})