Skip to content

refactor(preflight): simplify swap handling#1122

Merged
ericksoa merged 2 commits intoNVIDIA:mainfrom
cv:refactor/preflight-swap-lint-fix
Mar 30, 2026
Merged

refactor(preflight): simplify swap handling#1122
ericksoa merged 2 commits intoNVIDIA:mainfrom
cv:refactor/preflight-swap-lint-fix

Conversation

@cv
Copy link
Copy Markdown
Contributor

@cv cv commented Mar 30, 2026

Summary

Refactor bin/lib/preflight.js swap handling to satisfy the current ESLint rules. This keeps the existing behavior but reduces ensureSwap() complexity and replaces the empty catch block with an explicit best-effort path.

Changes

  • extract swapfile existence, existing swap activation, disk-space checks, marker writing, cleanup, and creation into small helpers
  • simplify ensureSwap() control flow so it stays under the configured complexity limit
  • replace the empty catch around the managed swap marker write with a documented best-effort catch

Type of Change

  • Code change for a new feature, bug fix, or refactor.
  • Code change with doc updates.
  • Doc only. Prose changes without code sample modifications.
  • Doc only. Includes code sample changes.

Testing

  • npx prek run --all-files passes (or equivalently make check).
  • npm test passes.
  • make docs builds without warnings. (for doc-only changes)

Ran locally:

  • npm run lint
  • npm test -- test/preflight.test.js
  • npm test (currently fails locally in existing test/onboard.test.js cases unrelated to this change)

Checklist

General

Code Changes

  • Formatters applied — npx prek run --all-files auto-fixes formatting (or make format for targeted runs).
  • Tests added or updated for new or changed behavior.
  • No secrets, API keys, or credentials committed.
  • Doc pages updated for any user-facing behavior changes (new commands, changed defaults, new features, bug fixes that contradict existing docs).

Summary by CodeRabbit

  • Refactor
    • Reorganized internal swap provisioning to improve maintainability and reliability with no change to user-facing behavior.
  • Tests
    • Extended sandbox health-check mocks so provisioning/startup flows are more reliably validated in automated tests.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 059be4b4-76cf-48c2-8f5b-cebc6d432f73

📥 Commits

Reviewing files that changed from the base of the PR and between 81423f8 and 5d08a35.

📒 Files selected for processing (1)
  • test/onboard.test.js

📝 Walkthrough

Walkthrough

Refactored swap provisioning in bin/lib/preflight.js by extracting inline logic from ensureSwap() into helper functions (getExistingSwapResult, checkSwapDiskSpace, createSwapfile, cleanupPartialSwap, writeManagedSwapMarker) and adjusting control flow (including dry-run early return).

Changes

Cohort / File(s) Summary
Swap Provisioning Refactor
bin/lib/preflight.js
Extracted swap-related helpers for existing-swap detection, disk-space guard, swapfile creation, cleanup, and managed-marker persistence. Replaced inline blocks with helper calls, removed redundant platform local, and tightened memory-info retrieval.
Test: sandbox health probe
test/onboard.test.js
Added mocks for an in-sandbox health-check command (sandbox exec ... curl -sf http://localhost:18789/) in two runner.runCapture stubs so the probe returns "ok" for affected flows.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I hopped through heaps of code tonight,
Extracted bits and set them right,
Helpers snug in tidy rows,
Swapfile springs where memory grows,
🍃 A small hop, and tests say “ok” — delight!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and concisely describes the main change: refactoring swap handling in the preflight module to simplify code organization.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
bin/lib/preflight.js (1)

228-239: Prefer fs.mkdirSync over shell mkdir -p.

Using a shell command for directory creation is unnecessary when Node.js provides fs.mkdirSync with recursive: true. This also avoids a potential command injection risk if os.homedir() contains shell metacharacters (unlikely but possible).

♻️ Suggested refactor
 function writeManagedSwapMarker() {
   const nemoclawDir = path.join(os.homedir(), ".nemoclaw");
-  if (!fs.existsSync(nemoclawDir)) {
-    runCapture(`mkdir -p ${nemoclawDir}`, { ignoreError: true });
-  }
+  fs.mkdirSync(nemoclawDir, { recursive: true });
 
   try {
     fs.writeFileSync(path.join(nemoclawDir, "managed_swap"), "/swapfile");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@bin/lib/preflight.js` around lines 228 - 239, The writeManagedSwapMarker
function currently uses runCapture("mkdir -p ...") to create the .nemoclaw
directory; replace that shell call with Node's fs.mkdirSync(nemoclawDir, {
recursive: true }) to avoid spawning a shell and potential injection risks and
to use native filesystem behavior; keep the existing existence check or simply
call mkdirSync in a try/catch and preserve the current best-effort semantics
(the try/catch around fs.writeFileSync for the managed_swap marker should remain
unchanged).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@bin/lib/preflight.js`:
- Around line 228-239: The writeManagedSwapMarker function currently uses
runCapture("mkdir -p ...") to create the .nemoclaw directory; replace that shell
call with Node's fs.mkdirSync(nemoclawDir, { recursive: true }) to avoid
spawning a shell and potential injection risks and to use native filesystem
behavior; keep the existing existence check or simply call mkdirSync in a
try/catch and preserve the current best-effort semantics (the try/catch around
fs.writeFileSync for the managed_swap marker should remain unchanged).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b732a5d4-367d-4f97-a595-c13710db1c1b

📥 Commits

Reviewing files that changed from the base of the PR and between 711b98e and 81423f8.

📒 Files selected for processing (1)
  • bin/lib/preflight.js

@ericksoa ericksoa self-requested a review March 30, 2026 21:36
Copy link
Copy Markdown
Contributor

@ericksoa ericksoa left a comment

Choose a reason for hiding this comment

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

Clean refactoring — the extracted helpers make ensureSwap() much easier to follow. Behavior-preserving with good early-return structure for the dry-run path. LGTM.

@ericksoa ericksoa merged commit 0a97e89 into NVIDIA:main Mar 30, 2026
9 checks passed
@cv cv deleted the refactor/preflight-swap-lint-fix branch March 30, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants