Skip to content

Add 256MB swap to VM rootfs#9

Open
joshuaisaact wants to merge 1 commit intomainfrom
feat/vm-swap
Open

Add 256MB swap to VM rootfs#9
joshuaisaact wants to merge 1 commit intomainfrom
feat/vm-swap

Conversation

@joshuaisaact
Copy link
Copy Markdown
Owner

@joshuaisaact joshuaisaact commented Mar 21, 2026

Summary

  • Creates a 256MB swap file in the rootfs during hearth setup
  • Enables it via swapon in the init script before the agent starts
  • Swap state persists through snapshot restore (kernel state is in memory snapshot)

Fixes OOM kills when running memory-hungry tools like pnpm install in a 512MB VM. The swap file is sparse-friendly and compresses well in snapshots.

To test: hearth setup (rebuilds rootfs), then try pnpm install inside a sandbox.

Summary by CodeRabbit

  • New Features
    • System now automatically creates and enables a 256MB swap file during setup to improve memory resource management.

Memory-hungry tools like pnpm OOM-kill at 512MB RAM. Create a swap
file during rootfs build and enable it in the init script so VMs
can handle temporary memory spikes without increasing base RAM.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 21, 2026

📝 Walkthrough

Walkthrough

During rootfs setup, a 256MB swap file is created inside the extracted root filesystem with permissions 0600 and initialized with mkswap. The generated init script activates the swap file at boot before launching the hearth-agent service.

Changes

Cohort / File(s) Summary
Swap file initialization
src/cli/setup.ts
Added creation of a 256MB swap file during rootfs setup with proper permissions, initialization, and boot-time activation before starting hearth-agent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • Add dm-thin instant snapshots #4 — Both PRs modify the generated rootfs init script in src/cli/setup.ts to add new initialization steps (hosts entry and docker build in that PR, swap file activation in this one).

Poem

🐰 A swap file hops into the rootfs with glee,
256 megabytes of memory so free,
Initialized with care, permissions set just right,
Boot-time activation—hearth-agent takes flight! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 clearly and accurately summarizes the main change: adding a 256MB swap file to the VM rootfs during setup, which matches the file-level changes in src/cli/setup.ts and aligns with the PR objectives.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/vm-swap

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Copy Markdown

@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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/cli/setup.ts`:
- Line 283: Replace the silent swap activation command that redirects errors
away ("swapon /swapfile 2>/dev/null") with a command that preserves error output
so failures are visible during init; update the initialization line (the entry
containing "swapon /swapfile 2>/dev/null") to either remove the stderr
redirection or redirect stderr to the init log (e.g., allow errors to propagate
or append "2>&1 | tee -a /var/log/boot-swap.log") and ensure the init process
will surface or log any failure so operators can diagnose swap activation
issues.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e871428c-80c8-4d55-8f5c-9aafb20f0fdc

📥 Commits

Reviewing files that changed from the base of the PR and between a009de7 and c8f1a0c.

📒 Files selected for processing (1)
  • src/cli/setup.ts

"hostname hearth",
"echo '127.0.0.1 localhost hearth' > /etc/hosts",
"ip link set lo up 2>/dev/null",
"swapon /swapfile 2>/dev/null",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Avoid silent swap activation failures in init.

swapon /swapfile 2>/dev/null hides why swap didn’t come up. If this fails, the VM can still OOM with no clue in boot logs.

Suggested change
-        "swapon /swapfile 2>/dev/null",
+        "swapon /swapfile >/dev/null 2>&1 || echo 'WARN: failed to enable /swapfile' >&2",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"swapon /swapfile 2>/dev/null",
"swapon /swapfile >/dev/null 2>&1 || echo 'WARN: failed to enable /swapfile' >&2",
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/cli/setup.ts` at line 283, Replace the silent swap activation command
that redirects errors away ("swapon /swapfile 2>/dev/null") with a command that
preserves error output so failures are visible during init; update the
initialization line (the entry containing "swapon /swapfile 2>/dev/null") to
either remove the stderr redirection or redirect stderr to the init log (e.g.,
allow errors to propagate or append "2>&1 | tee -a /var/log/boot-swap.log") and
ensure the init process will surface or log any failure so operators can
diagnose swap activation issues.

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.

1 participant