Repository navigation
perf: make metadata observations synchronous inside async hot paths - #238
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: blocked before merge. Reviewed September 6, 2026, 9:59 PM ET / September 7, 2026, 01:59 UTC. ClawSweeper reviewWhat this changesThe PR makes metadata checks synchronous within asynchronous filesystem operations, adds a size-based read optimization, and updates race tests, performance budgets, and documentation. Regression provenancePossible regression — suspected (reviewed change). No predecessor PR is attributed. Merge readiness⛔ Blocked before merge - 4 items remain This remains useful, distinct performance work, but the previously reported allocation defect is unchanged at the reviewed head. Priority: P2 Review scores
Verification
How this fits togetherfs-safe checks untrusted paths and filesystem identities before applications read or modify files. These shared checks feed root-scoped operations, atomic writes, secret files, and file stores. flowchart TD
A[Application paths and file handles] --> B[Root and path checks]
B --> C[Synchronous metadata observations]
C --> D{Identity and containment valid?}
D -->|No| E[Reject operation]
D -->|Yes| F[Asynchronous data reads or writes]
F --> G[Bounded results and publication checks]
Decision needed
Why: Local throughput gains cannot determine the acceptable event-loop blocking tradeoff for existing consumers. Before merge
Findings
Agent review detailsSecurityNone. Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep read allocations independently bounded and preserve responsive slow-mount operation unless maintainers explicitly accept the scheduling change with representative latency evidence. Do we have a high-confidence way to reproduce the issue? Yes, from source for the PR defect: read an already-consumed large file with maxBytes set to Infinity; the shortcut allocates the full file size before discovering EOF. This was not executed, and current main uses the bounded streaming implementation. Is this the best way to solve the issue? No, not as submitted: the read shortcut needs an independent allocation cap, and default synchronous metadata needs an explicit slow-mount scheduling decision. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 274292202089. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
History |
e25437b to
8b7a99a
Compare
8b7a99a to
32754e7
Compare
Use realpathSync.native for observations converted from fs.promises.realpath so Windows short paths retain their expanded canonical spelling. Keep identity and durability checks unchanged, repoint fault injection and separate-process secret probes, and count both native and legacy realpath observations. Validation: TypeScript, file-size and filesystem-boundary lint, docs, package checks, 226 security tests, 245 focused tests, and independent P2 review passed. Full single-worker suite: 7,984 passed and 81 skipped, with two launcher-specific failures subsequently passing with the exact pnpm 11.25.0 JavaScript CLI. Root-only npm and pnpm package smoke passed.
…ove paths (#248) Complete the synchronous-observation rule from #238 across archive extraction, copy and publication, move, sibling staging, secure and secret reads, hashing, locks, queues, and directory-mode paths: lstat, stat, realpath (native semantics) and fstat via retained descriptors run synchronously; open, read, write, fsync, rename, mkdir, unlink and close stay asynchronous. Injected custom filesystem adapters keep their async metadata contract. Every containment, identity, hardlink, symlink, permission, ownership, deadline and publication check keeps its order; error codes are unchanged. Race and fault tests are re-pointed at the synchronous observation, including one mechanism spy in the security suite whose assertions are unchanged; the Windows hash-identity proof observes the synchronous checks. Async metadata calls in a 500-file extraction drop to zero (total async calls down 48-62 percent) and extraction wall time improves 24-43 percent in paired runs.
Async filesystem hot paths spent 50–70% of profiled wall time waiting for libuv metadata round-trips. On the profiling machine, lstat cost about 14 µs asynchronously versus 1.4 µs synchronously. This change performs metadata observations synchronously inside async operations, while open, data reads/writes, fsync, rename, directory creation, unlink, and close remain asynchronous.
Every existing containment, identity, symlink, hardlink, mode, retry, and publication check stays in its original order. Awaited production test hooks stay in place. The path resolution result does not carry a final stat, so the independent pre-open observation remains. Existing-ancestor resolution retains its lstat-based missing/error behavior rather than adopting the sync API's existsSync semantics.
Changes by file
root-path.ts,root-path-existing.ts,root-path-symlink.ts,symlink-parents.ts: synchronous lexical traversal and symlink/ancestor observations, with existing async signatures and error mapping.root-context.ts,path-policy.ts,root-paths.ts,absolute-path.ts: synchronous root, canonical alias, and directory observations.root-impl.ts,root-write-mode.ts,root-write-verification.ts: synchronous pre-open/path/canonical observations and fstat via retained descriptors, including mode inheritance and post-publication checks.opened-realpath.ts,strict-file-identity.ts: synchronous observers in the existing async-shaped identity retry and descriptor/path resolution flows; parent enumeration remains async.directory-guard.ts,guarded-mkdir.ts,directory-durability.ts: synchronous guard and directory receipt observations, used by the existing guarded mutation helpers; mkdir/open/fsync/close remain async.pinned-write.ts,native-pinned-write.ts,replace-file-descriptor.ts,replace-file-temp-owner.ts,secret-file.ts: synchronous staging, parent admission, retained-fd, temp/path, and published-file observations.regular-file.ts,file-store.ts,file-store-boundary.ts,file-store-prune.ts: synchronous read/exists and related store observations; JSON readers inherit the change through the guarded regular-file reader.bounded-read.ts: read min(maxBytes, observed regular-file size) + 1 bytes in one initial async call. A short read is accepted only after a fresh same-fd size observation confirms that the bytes consumed cover the current file size; otherwise reading continues through the existing bounded streaming loop. This refines the proposed short-read shortcut because short regular-file reads are not necessarily EOF. Read-only adapters and non-regular/unknown-size inputs retain streaming behavior.test/fs-call-budget.test.ts: separate async-hop budgets, updated total-call budgets, and counting of FileHandle.close own properties as well as prototype methods.test/bounded-read.test.ts: one-read coverage for empty, small and multi-chunk-sized files, growth below/above the cap, partial reads before EOF, and custom/unknown-size adapters.docs/reading.md,CHANGELOG.md: document brief synchronous metadata observations and asynchronous data I/O.The explicit compatibility exception is custom
replaceFileAtomicfilesystem adapters: their public interface exposes only async metadata functions and FileHandles, so those observations remain async. The default Node backend uses synchronous observations. No mutation-metadata call was converted.Async-hop and total-call budgets
Measured on macOS with native mode off, including FileHandle.close. Async counts include only fs/promises functions and FileHandle methods; synchronous observations are excluded. Total-call limits retain two calls of platform headroom; async limits are measured + 1.
Remaining read hops are open/read/close. Atomic replacement retains mkdir, two opens, two closes, writeFile, chmod, and rename. Root.write retains three opens/closes, writeFile, chmod, rename, and two durability syncs.
Interleaved A/B
Baseline: origin/main at 9ad2d5c, separately compiled with the same dependencies and prepared WASM artifact. Node 24.20.0 on macOS. The supplied harness alternates baseline/PR for five rounds and reports the best ms/op; 150 iterations per small case and 20 per 1 MiB case. The repository benchmark script is unchanged.
Native auto
Native off
The 40% readBytes target is met in both modes. tryReadJson reaches 42% in auto and 39% in off, just below the 40% off-mode target. replaceFileAtomic improves 13–14%, below the 30% target; its eight remaining async calls are listed above. Durability costs remain unchanged. The tables report the fixed five-round run after the earlier full-suite run finished.
Validation
pnpm exec tsc --pretty false: passed.pnpm lint:file-size,pnpm lint:fs-boundary,pnpm docs:check,node scripts/check-pack.mjs,git diff --check: passed.pnpm test:security: 226 passed, 5 files.pnpm test --maxWorkers=4: 7,809 passed, 80 skipped, 2 known local failures; 231 files passed, 2 skipped, 1 failed. Both failures are inconsumer-pnpm-lifecycle.test.ts, where this local launcher does not expose the expected pnpm lifecycle JavaScript CLI.--input-type=commonjsonly to no-argument Node invocations; file-based scripts and the library tests use the same Node runtime unchanged. The dispatch fixture then passed, including in the final full run.--maxWorkers=1and in the final full run. An earlier store-overlap run reported the existing fail-closedpath-mismatchduring concurrent replacement; isolated reruns and the final full run passed without changing that test or relaxing identity checks.pnpm checkreached the known local WASM build failure (missingwasm32-unknown-unknownRust target). The prepared WASM artifact was restored; TypeScript and all remaining component checks were run explicitly as requested.Re-pointed race/fault tests
The following test files and shared fixture were updated to intercept the new synchronous observation. All scenarios and parameterized cases remain. Synthetic filesystem swaps inside synchronous spies now use synchronous fixture mutations. Async owner-release scenarios retain their barriers at the existing pre-resolution hook or async resolver wrapper; directory-preparation authority expiration is injected before the synchronous observation returns.
test/absolute-directory.test.tstest/archive.test.tstest/deepsec-regression.test.tstest/deny-mutations.test.tstest/directory-guard-exact-identity.test.tstest/helpers/read-admission-identity.tstest/json-durable-queue-batch-durability.test.tstest/json.test.tstest/move-path-authority.test.tstest/native-write-mode-ownership.test.tstest/new-primitives.test.tstest/opened-realpath-observation.test.tstest/opened-realpath.test.tstest/regular-file-failure.test.tstest/root-copy-read-identity.test.tstest/root-directory-errors.test.tstest/root-error-coverage.test.tstest/root-path-store-coverage.test.tstest/root-read-identity.test.tstest/root-write-exact-identity.test.tstest/root-write-inheritance.test.tstest/root-write-lifetime.test.tstest/root-write-reopen.test.tstest/root-write-verification.test.tstest/secret-directory-receipt.test.tstest/secret-write-publication.test.tstest/sibling-temp-directory-mode.test.tstest/sidecar-lock-root-ancestry.test.tstest/sidecar-lock-root-create-denial.test.tstest/sidecar-lock-root-preopen-boundary.test.tstest/sidecar-lock-root-resolver.test.tstest/sidecar-lock-root-unlink.test.ts