Goddess Commits / blog

Fixing swap thrashing and context limits on scheduled local agents

4 min readTechlocal aiollamaresource contentioncontext limitsdiscord automation

TL;DR

  • Co-locating two large Ollama models on a Mac caused swap thrashing that crashed Python apps and stalled agent passes.
  • A 1200-character context window made the snippet writer miss post history and fabricate claims in drafts.
  • Routing approvals through three dedicated MCP tools and enforcing one model per host restored reliable weekly scheduling.

I scheduled local AI agents to review pull requests and run SEO reports across a Mac and a Raspberry Pi. Memory thrashing, context limits, and inference contention broke the workflow until I moved to one model per host, expanded the context window, and routed approvals through dedicated tools instead of generic prompts.

Why does co-locating Ollama models crash Python apps?

I built a local PR review agent that runs on my Mac, queries open pull requests via gh, sends diffs to qwen3-coder:30b for bug and edge-case analysis, and posts results via Hermes. The idea was straightforward, but running two large coding models at the same time broke everything. Ollama logs showed significant swap usage as the models evicted each other from RAM.

The Mac is causing memory thrashing due to two large models (qwen3.6 and qwen3-coder) evicting each other; Ollama logs show significant swap usage (10.6 GB of 12 GB).

This was not a theoretical problem. A leftover Ollama server loaded a 22 GB model while a background vault-sync job ran at the same time, and the Mac killed my Python app outright. The fix required strict host separation. Each machine now runs exactly one model during its scheduled window, and I added an OBS-aware LaunchAgent to control the server lifecycle so unused models unload before sync jobs start.

Why did my agent drafts start containing fabricated claims?

The review agent writes through an outbox directory with a read-only _agent/ folder on the Pi to prevent deletion by sync pushes. This routing setup worked, but the drafting step failed silently. The snippet writer only had access to the top of each draft before passing it to the model for revision.

The root cause of fabricated claims in previous drafts was the snippet writer only seeing the first 1200 characters instead of the whole post.

With that limited window, the agent saw isolated sentences without the surrounding context or historical feedback. It filled the gaps with plausible but wrong details about dependencies and formatting rules. I expanded the context window so the writer reads the full post before generating revisions. The hallucinations stopped immediately because the model could now reference actual comments and rejection reasons from earlier passes.

Why do scheduled agent passes stall on a Raspberry Pi?

The Pi handles weekly SEO analysis with search-report and search-review, running every Monday morning. Fast agent passes suddenly turned into 14-minute waits. The logs pointed to inference bottlenecks rather than network latency. A conflicting process on the Pi competed for the Ollama inference slot while the scheduled job started.

A conflicting process (agents.watch) on the Pi competed for the Ollama inference slot, slowing down the pass to 14 minutes.

The watchdog script and the agent were both calling into the same local endpoint. I separated their execution paths by scheduling them with non-overlapping launch times and added a lightweight lock around the inference call. The Pi now completes its review pass in under four minutes without blocking other processes.

How do I route agent approvals without retry loops?

The blog review loop moved away from open-ended prompts to a strict routing system. Hermes calls three narrow MCP tools (publish, reject, revise) in the Discord thread, and built-in handlers translate simple replies like yes or no into pull request state changes. This matches the approach some teams have taken with multi-agent reviewers inside Discord, as noted in [PR Pilot](https://dev.to/jaysid97/pr-pilot-putting-multi-agent-ai-code-reviewers-inside-discord-17gl), though my setup relies on local inference rather than remote APIs.

Revision priority follows a strict fallback chain. The Mac's Ollama post-editor runs first for one attempt. If it fails or returns low confidence, the system tries an OpenRouter free model exactly once. No retry loops run under any condition. This prevents queued requests from piling up when inference slots fill or context windows expire.

The workflow now survives on its own schedule. Agents fetch diffs, analyze them locally, route findings through Discord, and sync results back without manual intervention. Memory separation keeps the Mac stable, expanded context windows keep the drafts accurate, and strict routing stops the Pi from locking up. The system runs weekly without leaving crashed processes or fake claims behind.

Written from a live stream transcript with AI help, reviewed by the author.