Skip to content

fix(path): keep asynchronous canonicalization off the Node event loop - #1053

Merged
steipete merged 2 commits into
mainfrom
fix/async-path-prefix
Oct 31, 2026
Merged

steipete merged 2 commits into
mainfrom
fix/async-path-prefix

Conversation

@steipete

@steipete steipete commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes event-loop stalls when asynchronous path preflight waits on slow filesystem metadata: resolveAbsolutePathForRead currently calls synchronous realpath, and prefix resolution offers only a synchronous component walk.

User Impact

Consumers can await resolvePathPrefix through the advanced API, and existing resolveAbsolutePathForRead callers use asynchronous Node canonicalization. Sync prefix resolution remains available. Physical symlink traversal, raw missing suffixes, permission errors, and non-directory rejection retain their contracts.

Bun POSIX canonicalization and Windows AppContainer fallback retain the existing synchronous native-realpath owner for platform compatibility; those fallback paths remain potentially blocking. No dependency or version change is included.

Why This Change Was Made

One walker owns traversal for both APIs. Synchronous calls execute their IO directly; the async fallback yields awaited IO operations. Node POSIX existing paths use kernel lstat and canonicalization without separate ancestor inspections, with a target-search-permission check for final symlinks. A proven missing leaf with clean normalized spelling probes only to the nearest existing non-symlink directory. Dangling links, raw dot/separator forms, and uncertain failures retain the component algorithm.

There is no cache or new authority policy: these are observations, and guarded Root operations still own later effects. Production source grows by 79 lines for the async API/driver, bounded fast paths, and preserved runtime-specific canonicalization behavior.

Evidence

The absolute-read regression fails against the original owner with blocking realpath and passes with the fix. Nine focused path/security files passed: 123 tests, 5 platform skips, 1.12 seconds in the final source run. Coverage includes pending asynchronous metadata, physical symlink parents, raw missing suffixes, replaced aliases, direct dangling links outside the starting directory, permission-denied traversal, loops, non-directory errors, and absolute-read symlink policy.

pnpm check passed file-size and filesystem-boundary lints and TypeScript compilation, then stopped at the missing Cargo/WASM toolchain; the local machine also lacks the pinned .NET SDK for the Windows helper. Full local check is incomplete. Final targeted TypeScript compilation, documentation examples, and git diff --check passed. The public API manifest was regenerated by its owner. Independent Codex review found no actionable P0–P2 findings. Initial-head CI found the new export missing from benchmark coverage; the final head registers it, and the filtered runner passed all four prefix cases. Exact source head 32a19880de089cef5a5a620fd49d46808eee7efc has 51 successful checks and 5 skipped, with no pending or failed checks. CI provides the full platform/build/package validation.

Measurements

The final consumer scope moves only prefix canonicalization and sandbox raw-path resolution to async IO, reuses prepared prefixes within one owner invocation, and removes duplicate caller-side mutation checks while retaining the effect owner's guard. Git metadata lstat and ordinary-realpath handling retain their existing synchronous contract. There is no whole-Gateway zero-sync-filesystem claim.

Acceptance: five interleaved A1/B/A2 batches, 200 selections/batch; count prefix traversal and timer delay under injected 100 ms prefix latency, with allocation sampling and single delayed selections measured separately. Require nonblocking prefix IO, preserved security, and no material CPU/allocation/elapsed regression. The narrowed scope meets this rule; broader metadata conversion was rejected.

The combined source and prepared OpenClaw owner experiment uses 62,000 synthetic workspace files totaling 1.116 GB on APFS, Node 26.12.1/Darwin arm64. These are owner measurements, not a live Gateway or btrfs result. Source and consumer inputs were pinned for the final campaign.

Per workspace-root selection A1 A2 control Candidate
Exported sync lstat calls, existing root 13 13 1
Prefix sync lstat calls, existing root 12 12 0
Prefix async lstat calls, existing root 0 0 1
Prefix sync lstat calls, missing parent 13 13 0
Prefix async lstat calls, missing parent 0 0 2
Ordinary sync metadata realpath calls 2 2 2
Native sync / async prefix realpath calls 2 / 0 2 / 0 0 / 1
100 ms prefix delay: timer ms, existing root 103.016 100.630 1.409
100 ms prefix delay: timer ms, missing parent 105.957 106.759 1.297
Main CPU µs, existing root 223.010 288.905 178.590
Total CPU µs, existing root 325.195 385.715 323.325
Elapsed ms, existing root 0.285 0.350 0.273
Main CPU µs, nested existing parent 282.380 252.475 174.100
Total CPU µs, nested existing parent 374.940 337.860 317.710
Elapsed ms, nested existing parent 0.339 0.303 0.275
Main CPU µs, missing parent 275.280 259.235 211.290
Total CPU µs, missing parent 396.995 373.940 376.355
Elapsed ms, missing parent 0.358 0.338 0.359
Sampled allocation bytes, existing root 59,359 59,921 46,176
Sampled allocation bytes, missing parent 63,947 63,506 53,438

Values are medians of five batch means. One synchronous Git lstat and two ordinary metadata realpaths remain per selection. These are JavaScript-call counters, not a complete kernel syscall census. Existing-root elapsed ranges were 0.254–0.375 / 0.257–0.553 / 0.243–0.338 ms for A1/A2/B. Nested-parent elapsed ranges were 0.272–0.397 / 0.237–0.695 / 0.240–0.532 ms for A1/A2/B. Missing-parent elapsed ranges were 0.269–1.212 / 0.248–8.088 / 0.259–0.886 ms for A1/A2/B. Nearest-rank batch p95 equals each range maximum with five samples. Allocation falls about 22–23% for the root shape and 16% for missing parents. Missing-parent elapsed median changes by 0–6%; main CPU improves and total CPU is flat or lower. No release, dependency change, or deployment is included in this source PR.

The retained synchronous API has its own committed control:

node benchmarks/path-prefix-comparison.mjs /absolute/baseline/dist /absolute/candidate/dist /absolute/temporary-parent 1000

Five A1/B/A2 batches on Node 24.22.1/Darwin arm64, 1,000 resolutions each. Existing paths retain 7 lstat + 1 realpath; missing paths retain 8 + 1. Existing-path main CPU is 45.72/49.06 → 43.07 µs and elapsed 46.27/49.44 → 43.28 µs. Missing-path main CPU is 74.29/74.14 → 79.30 µs and elapsed 80.04/78.63 → 82.65 µs. Process CPU changes remain below 8% in these final controls. This isolates the synchronous API from the consumer's asynchronous scheduling tradeoff.

  • Tests added or updated when behavior changed
  • Security and compatibility impact considered
  • CHANGELOG.md updated when release-relevant
  • No credentials, private paths, private hosts, or sensitive contents included

@steipete
steipete requested a review from a team as a code owner October 31, 2026 19:25
@clawsweeper

clawsweeper Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-31T20:09:35.715193Z 32a1988 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ed2de0c62c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/path-prefix.ts
// Missing prefixes, long link chains and errors retain the walker's contract.
}
}
const reads = resolvePrefix(input);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the anchored path when falling back to component traversal

On Node POSIX, if process.chdir() runs while the initial metadata probe is pending, a failed probe falls back to resolvePrefix(input), which anchors the relative input again using the new working directory. This can return a completely different file rather than the missing suffix in the originally requested directory. Reproduced by calling resolvePathPrefix('missing') from directory A and immediately changing to directory B containing that filename: the result reports B's existing file. Capture the absolute path once and pass that same path to the fallback walker.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal priority bug or improvement with limited blast radius. merge-risk: 🚨 automation 🚨 Merging this PR could break CI, automerge, proof capture, label sync, or automation. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 31, 2026
@clawsweeper

clawsweeper Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

This PR adds asynchronous prefix resolution and moves absolute-read canonicalization onto asynchronous Node filesystem APIs.

Example: Resolve /srv/data/future/file.json while filesystem metadata is slow.

  • Before: Prefix resolution offers only a synchronous API that blocks the JavaScript thread.
  • After: Callers can await resolvePathPrefix and receive /srv/data plus ["future", "file.json"] while Node metadata requests run asynchronously.

Review scores

Measure Result What it means
Overall readiness 🦪 silver shellfish (2/6) Useful, owner-directed work with focused unit evidence, but two concrete defects block merge.
Proof confidence 🦐 gold shrimp (3/6) The host-supplied maintainer-authored designation exempts this PR from the ordinary contributor proof gate; supplied evidence exercises source-level tests, while consumer timings remain planned.
Patch quality 🦪 silver shellfish (2/6) 2 actionable review findings remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Awaiting path preflight can stall unrelated JavaScript work while synchronous filesystem metadata waits on slow storage.
Reason: The host identifies this as maintainer-authored, approving the direction and additive async API; the change preserves the synchronous API and documented platform fallbacks.

Merge readiness

⛔ Needs changes before merge - 2 items remain

Keep this maintainer-authored PR open: asynchronous path resolution has clear value, but the fallback can resolve against the wrong directory and the new export breaks benchmark coverage.

Priority: P2
Reviewed head: ed2de0c62cef32ce3979be44481f63d2613bcaf5

Before merge

  • Reuse the initial absolute path in the fallback walker (P2) - On Node POSIX, call resolvePathPrefix('missing') from /srv/a, where that entry is absent, then change cwd to /srv/b while the initial stat is pending. If /srv/b/missing exists, the failed probe reaches resolvePrefix(input), which reads cwd again and returns /srv/b/missing instead of the unresolved suffix under /srv/a. Capture the raw absolute path once and pass it to both paths. This confirms the existing discussion at fix(path): keep asynchronous canonicalization off the Node event loop #1053 (comment).
  • Register the new export with the method benchmark owner (P2) - Exporting resolvePathPrefix makes benchmarks/runner.mjs discover another callable API, but benchmarks/paths.mjs registers only resolvePathPrefixSync and no exclusion covers the new name. The assertion at runner.mjs:177 therefore fails with 'Unmeasured methods: resolvePathPrefix' before measurement, breaking both native and JavaScript benchmark smoke commands used by CI. Add async cases using the existing fixtures and result checks.

Findings

  • [P2] Reuse the initial absolute path in the fallback walker — src/path-prefix.ts:149
  • [P2] Register the new export with the method benchmark owner — src/advanced.ts:14

Tests

  • Missing end-to-end proof: Supplied evidence contains tests rather than a consumer run showing responsive timers during delayed metadata; no regression coverage demonstrates preservation of the cwd anchor.
Agent review details

How this fits together

Advanced path helpers turn caller-supplied paths into canonical path observations; guarded Root operations separately own authorization and filesystem effects.

flowchart TD
  A[Caller path] --> B[Advanced path API]
  B --> C[Async existing path probe]
  C --> D[Shared component walker]
  C --> E[Runtime canonicalization]
  D --> E
  E --> F[Canonical path observation]
  F --> G[Caller or guarded Root operation]
Loading

Technical review

Best possible solution:

Retain the shared traversal owner, anchor input once before awaiting, and register the new async API with the existing benchmark owner.

Do we have a high-confidence way to reproduce the issue?

Source shows both synchronous canonicalization on main and the introduced fallback race; the existing review supplies the same A-to-B working-directory scenario, but this read-only review ran no target code.

Is this the best way to solve the issue?

Sharing the existing walker and preserving runtime-specific canonicalization is appropriate, provided the fallback retains its initial path anchor and the export is integrated with benchmark coverage.

Full review comments:

  • [P2] Reuse the initial absolute path in the fallback walker — src/path-prefix.ts:149
    On Node POSIX, call resolvePathPrefix('missing') from /srv/a, where that entry is absent, then change cwd to /srv/b while the initial stat is pending. If /srv/b/missing exists, the failed probe reaches resolvePrefix(input), which reads cwd again and returns /srv/b/missing instead of the unresolved suffix under /srv/a. Capture the raw absolute path once and pass it to both paths. This confirms the existing discussion at fix(path): keep asynchronous canonicalization off the Node event loop #1053 (comment).
    Confidence: 0.99
  • [P2] Register the new export with the method benchmark owner — src/advanced.ts:14
    Exporting resolvePathPrefix makes benchmarks/runner.mjs discover another callable API, but benchmarks/paths.mjs registers only resolvePathPrefixSync and no exclusion covers the new name. The assertion at runner.mjs:177 therefore fails with 'Unmeasured methods: resolvePathPrefix' before measurement, breaking both native and JavaScript benchmark smoke commands used by CI. Add async cases using the existing fixtures and result checks.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.97

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 92f296e85810.

Provenance checked

Testing

Proof path: unit tests only.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test test/path-prefix-async.test.ts test/path-prefix.test.ts test/path-prefix-windows.test.ts test/root-path-store-coverage.test.ts
  • pnpm benchmark:methods --mode off --iterations 1 --samples 1 --warmup 0
  • pnpm benchmark:methods --mode require --iterations 1 --samples 1 --warmup 0
  • pnpm check
  • git diff --check

What I checked:

Review metrics

Metric Value Why it matters
Production source growth +73 / -10 lines; net +63 Four production files add the async entry point and adapters while retaining one traversal algorithm.

Labels

Label changes:

  • add P2: This is a focused responsiveness improvement with a reproducible path-selection defect and a narrow benchmark integration failure.
  • add merge-risk: 🚨 automation: The new callable export makes the benchmark coverage assertion fail in existing CI smoke jobs.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Label justifications:

  • P2: This is a focused responsiveness improvement with a reproducible path-selection defect and a narrow benchmark integration failure.
  • merge-risk: 🚨 automation: The new callable export makes the benchmark coverage assertion fail in existing CI smoke jobs.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦐 gold shrimp and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 31, 2026, 3:38 PM ET / 19:38 UTC.

@steipete
steipete marked this pull request as draft October 31, 2026 19:55
@steipete
steipete marked this pull request as ready for review October 31, 2026 20:08
@steipete
steipete merged commit 64ba766 into main Oct 31, 2026
59 of 60 checks passed
@steipete
steipete deleted the fix/async-path-prefix branch October 31, 2026 20:11
steipete added a commit that referenced this pull request Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 automation 🚨 Merging this PR could break CI, automerge, proof capture, label sync, or automation. P2 Normal priority bug or improvement with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant