mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* chore(ratchets): count sync fs calls on Electron main and setInterval calls
Sync fs on main and fixed-cadence intervals that never settle were two of the
most common perf fix classes since September, and no gate counted them. Both
ratchets grandfather today's uses per file and fail when a file's count grows.
* chore(ratchets): count every sync fs export, src/shared and node:timers intervals
Review: the allow-list missed descriptor/copy APIs, src/shared helpers run on
main too, and setInterval imported from node:timers escaped the timer count.
* chore(ratchets): keep hosted setInterval calls beside a named timer import
Review: a named node:timers import zeroed window/globalThis calls too, a
destructured require was counted twice, and collector edits didn't select
the timer ratchet in check:changed.
* chore(ratchets): charge calls through an alias, not the alias binding
Decider ruling B on review finding C6: binding a plain alias of a gated call
counted as a use, so an alias refactor failed every collector-based ratchet.
Conditional/fallback aliases and exported bindings stay charged.
* chore(ratchets): count bare setInterval only when nothing local shadows it
Review: the timer ratchet kept its own narrower binding resolver, so alias
and namespace-destructure forms of an imported setInterval counted twice.
Module bindings are the shared collector's; a bare call is the global only
when the file declares no setInterval binding.
* chore(ratchets): resolve bare setInterval calls per scope
Decider ruling A: the file-wide shadow rule let an unrelated parameter or type
declaration named setInterval hide every global poll in the file. Each bare
call now resolves through a single-file TypeScript checker (noLib, noResolve)
and counts when it binds to nothing declared in the file.
* chore(ratchets): write the timer baseline exactly as --prune does
* chore(ratchets): charge global timer aliases and bracket access
CodeRabbit: `const schedule = setInterval; schedule(poll)` and
`window['setInterval'](poll)` counted zero. Count value references to the
global timer (per scope) instead of only direct calls.
* chore(ratchets): charge shorthand timer escapes; skip writes, typeof and JSX tags
Review: `{ setInterval }` of the global counted 0, while assignments to it,
parenthesized typeof probes and a <setInterval/> tag counted 1.
* chore(ratchets): charge compound assignments to the global timer
Review: `||=` and `??=` read setInterval before writing it, so only a
plain `=` target is write-only.
* chore(ratchets): regenerate baselines for files main moved
main's SSH reorganisation moved counted files under src/main/ssh and
src/main/managed-server; counts are unchanged except one sync fs call main
removed.
116 lines
3.4 KiB
JavaScript
116 lines
3.4 KiB
JavaScript
import process from 'node:process'
|
|
import { pathToFileURL } from 'node:url'
|
|
import { createGatedCallCollector } from '../../process-boundaries/gated-call-use-collector.mjs'
|
|
import { readTrackedSources, runCountRatchet } from '../../ratchet-baseline/ratchet-baseline.mjs'
|
|
import { isTestOnlySourcePath } from '../../process-boundaries/test-only-source-path.mjs'
|
|
|
|
// Sync fs on Electron main stalls every window (and in the daemon/orcad, every terminal); per-spawn/RPC/focus stat and read sweeps were a recurring lag source (#39805, #35967, #38481).
|
|
|
|
const BASELINE_PATH = 'config/main-sync-fs-baseline.txt'
|
|
// src/shared runs on main too; leaving it out would let a helper move bypass the gate.
|
|
const SCAN_ROOTS = ['src/main', 'src/shared']
|
|
const ADVICE =
|
|
'use node:fs/promises, or cache the result on a stat fingerprint outside the hot path'
|
|
// Every *Sync export of node:fs (descriptor and copy APIs included), listed so the baseline doesn't depend on the runtime.
|
|
const SYNC_FS = [
|
|
'accessSync',
|
|
'appendFileSync',
|
|
'chmodSync',
|
|
'chownSync',
|
|
'closeSync',
|
|
'copyFileSync',
|
|
'cpSync',
|
|
'existsSync',
|
|
'fchmodSync',
|
|
'fchownSync',
|
|
'fdatasyncSync',
|
|
'fstatSync',
|
|
'fsyncSync',
|
|
'ftruncateSync',
|
|
'futimesSync',
|
|
'globSync',
|
|
'lchmodSync',
|
|
'lchownSync',
|
|
'linkSync',
|
|
'lstatSync',
|
|
'lutimesSync',
|
|
'mkdirSync',
|
|
'mkdtempDisposableSync',
|
|
'mkdtempSync',
|
|
'openSync',
|
|
'opendirSync',
|
|
'readFileSync',
|
|
'readSync',
|
|
'readdirSync',
|
|
'readlinkSync',
|
|
'readvSync',
|
|
'realpathSync',
|
|
'renameSync',
|
|
'rmSync',
|
|
'rmdirSync',
|
|
'statSync',
|
|
'statfsSync',
|
|
'symlinkSync',
|
|
'truncateSync',
|
|
'unlinkSync',
|
|
'utimesSync',
|
|
'writeFileSync',
|
|
'writeSync',
|
|
'writevSync'
|
|
]
|
|
const collector = createGatedCallCollector(
|
|
[
|
|
{
|
|
modules: ['fs', 'node:fs', 'original-fs', 'graceful-fs'],
|
|
advice: Object.fromEntries(SYNC_FS.map((name) => [name, ADVICE]))
|
|
}
|
|
],
|
|
'main sync-fs'
|
|
)
|
|
|
|
function isScannedPath(rel) {
|
|
return /\.[cm]?[jt]sx?$/.test(rel) && !rel.endsWith('.d.ts') && !isTestOnlySourcePath(rel)
|
|
}
|
|
|
|
export function countSyncFsCalls(rel, source) {
|
|
return collector.collectUses(rel, source).count
|
|
}
|
|
|
|
export function collectMainSyncFsCounts(root = process.cwd()) {
|
|
const counts = new Map()
|
|
for (const [rel, source] of readTrackedSources(root, SCAN_ROOTS, isScannedPath)) {
|
|
const count = countSyncFsCalls(rel, source)
|
|
if (count > 0) {
|
|
counts.set(rel, count)
|
|
}
|
|
}
|
|
return counts
|
|
}
|
|
|
|
export function main(root = process.cwd(), arg = undefined) {
|
|
return runCountRatchet(
|
|
{
|
|
name: 'main-sync-fs',
|
|
baselinePath: BASELINE_PATH,
|
|
header: [
|
|
'# Synchronous node:fs calls (every *Sync export) in production src/main and src/shared, per file.',
|
|
'# Startup-only or one-shot uses are fine to keep; per-RPC, per-spawn, per-focus or per-poll uses are the target.',
|
|
'# This is a RATCHET: counts may only go DOWN.',
|
|
'# Prune after removing calls: pnpm check:main-sync-fs-ratchet --prune'
|
|
],
|
|
collect: collectMainSyncFsCounts,
|
|
unit: 'sync fs call(s)',
|
|
fix: [
|
|
`Sync fs blocks the thread it runs on: Electron main (every window) or the daemon/orcad loop (every terminal). ${ADVICE}.`,
|
|
`A one-shot startup or shutdown use may raise the file's count in ${BASELINE_PATH} with reviewer sign-off.`
|
|
]
|
|
},
|
|
root,
|
|
arg
|
|
)
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
process.exit(main(process.cwd(), process.argv[2]))
|
|
}
|