<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dmitry</title>
    <description>The latest articles on DEV Community by Dmitry (@dimitreee).</description>
    <link>https://dev.to/dimitreee</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F82477%2F226b9483-9925-45eb-a21d-4eb56bfb6fd2.png</url>
      <title>DEV Community: Dmitry</title>
      <link>https://dev.to/dimitreee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dimitreee"/>
    <language>en</language>
    <item>
      <title>I wrote a garbage collector for what my coding agents leave behind</title>
      <dc:creator>Dmitry</dc:creator>
      <pubDate>Tue, 01 Dec 2026 14:09:31 +0000</pubDate>
      <link>https://dev.to/dimitreee/i-wrote-a-garbage-collector-for-what-my-coding-agents-leave-behind-45ak</link>
      <guid>https://dev.to/dimitreee/i-wrote-a-garbage-collector-for-what-my-coding-agents-leave-behind-45ak</guid>
      <description>&lt;p&gt;I use Claude Code every day. After about a year of trying plugins, skills and MCP servers, I noticed two things.&lt;/p&gt;

&lt;p&gt;First, my sessions were &lt;strong&gt;expensive before I typed anything&lt;/strong&gt;. The first reply of a fresh session cost about &lt;strong&gt;55k tokens&lt;/strong&gt;. All of it was context loaded at start-up: plugin and skill descriptions, MCP tool schemas, hook output, &lt;code&gt;CLAUDE.md&lt;/code&gt; files. A lot of it was stuff I had forgotten I'd installed: duplicates, MCP servers that never connected, and tools I hadn't called in a month.&lt;/p&gt;

&lt;p&gt;Second, my machine was &lt;strong&gt;filling up with leftovers&lt;/strong&gt;. Run &lt;code&gt;ps aux | grep mcp&lt;/code&gt; after a day of work and you may be surprised. When an agent exits, its stdio MCP servers get re-parented to &lt;code&gt;launchd&lt;/code&gt; or &lt;code&gt;init&lt;/code&gt; and keep running. This is a known issue (&lt;a href="https://github.com/anthropics/claude-code/issues/1935" rel="noopener noreferrer"&gt;#1935&lt;/a&gt;, &lt;a href="https://github.com/anthropics/claude-code/issues/33947" rel="noopener noreferrer"&gt;#33947&lt;/a&gt;); one report counts 96 orphaned processes after a single day. On top of that there were temp directories, interrupted plugin installs, old plugin versions, transcripts, and git worktrees that agents created for parallel work and never removed.&lt;/p&gt;

&lt;p&gt;So I built &lt;a href="https://github.com/Dimitreee/agent_gc" rel="noopener noreferrer"&gt;Agent GC&lt;/a&gt;. This post is about how it decides what is garbage, because that turned out to be the interesting part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two problems, two treatments
&lt;/h2&gt;

&lt;p&gt;The context problem and the leftovers problem look similar, but they need opposite answers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Context sources are the user's choices.&lt;/strong&gt; A plugin that costs 8k tokens may be worth it to you. So Agent GC measures and explains, and gives you a reversible switch, but it &lt;strong&gt;never turns anything off by itself&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leftovers can be collected automatically&lt;/strong&gt;, but only if the collector is provably conservative. A worktree with uncommitted changes is not garbage, however old it looks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Measuring the start cost (instead of guessing)
&lt;/h2&gt;

&lt;p&gt;You can't estimate the start cost reliably by adding up file sizes. Luckily you don't have to: the agent already records it. The first assistant message of every session in the transcripts contains the token usage the model received:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input_tokens + cache_creation_input_tokens + cache_read_input_tokens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Agent GC takes the median over the last 7 days of interactive sessions (headless runs started by other tools are excluded) and shows a weekly trend. Then it breaks the total down by source: each plugin, skill, MCP server (measured by actually calling &lt;code&gt;initialize&lt;/code&gt; and &lt;code&gt;tools/list&lt;/code&gt;), hook and &lt;code&gt;CLAUDE.md&lt;/code&gt; import, heaviest first. It also flags them: &lt;em&gt;duplicate&lt;/em&gt;, &lt;em&gt;not connecting&lt;/em&gt;, &lt;em&gt;unused for 30 days&lt;/em&gt;, &lt;em&gt;loaded while disabled&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Every switch can be undone. Plugins and skills are turned off through the agent's own settings. MCP servers and hooks are moved to a stash, and the config file is edited atomically with a backup, preserving symlinks and key order.&lt;/p&gt;

&lt;h2&gt;
  
  
  The leftovers: borrowing from runtime garbage collectors
&lt;/h2&gt;

&lt;p&gt;For leftovers I deliberately copied the structure of a tracing garbage collector, the kind that lives inside the JVM or Go. The analogy fits surprisingly well:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Runtime GC&lt;/th&gt;
&lt;th&gt;Agent GC&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Roots&lt;/td&gt;
&lt;td&gt;Running processes and their working directories, running agents' start times, paths referenced by config files, git repositories&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Heap objects&lt;/td&gt;
&lt;td&gt;Temp dirs, transcripts, plugin versions, worktrees, orphaned MCP helpers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mark&lt;/td&gt;
&lt;td&gt;One function that decides "is this dead"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Write barrier&lt;/td&gt;
&lt;td&gt;Re-check every fact right before acting&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generations&lt;/td&gt;
&lt;td&gt;Young garbage every 15 min, old garbage daily&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Quarantine.&lt;/strong&gt; Nothing is deleted immediately&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That last row is the main departure. In a language runtime, a wrong free corrupts a few bytes of heap. Here, a wrong free deletes someone's work.&lt;/p&gt;

&lt;h3&gt;
  
  
  One decision point
&lt;/h3&gt;

&lt;p&gt;Each kind of garbage is a &lt;em&gt;heap&lt;/em&gt;. A heap only lists its objects and reports facts about them: when it was last touched, what it's anchored to (a path, a config key, a process identity), and whether it looks like garbage on its own. &lt;strong&gt;A heap never decides to remove anything.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The decision lives in exactly one function, &lt;code&gt;mark&lt;/code&gt;. An object is dead only if every rule holds:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgovbtf3mocacxq1floef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgovbtf3mocacxq1floef.png" alt="Mark decision flow" width="800" height="1402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;It looks like garbage on its own.&lt;/strong&gt; "Unknown" counts as alive. If &lt;code&gt;git&lt;/code&gt; fails or a config file can't be read, nothing is touched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing reachable points to it.&lt;/strong&gt; A live process working inside the path, or a config file referencing it, keeps it alive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It has been idle long enough.&lt;/strong&gt; Each heap has its own threshold: 3 days for temp dirs, the agent's own &lt;code&gt;cleanupPeriodDays&lt;/code&gt; for transcripts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No running agent could still be using it.&lt;/strong&gt; More on this below.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It has looked dead for long enough.&lt;/strong&gt; Some heaps require an object to stay dead for a while before it counts.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Putting all of this in one place means a safety rule can't be forgotten in one of ten collectors.&lt;/p&gt;

&lt;h3&gt;
  
  
  The rule that does the most work: the agent epoch
&lt;/h3&gt;

&lt;p&gt;Rule 4 is borrowed from &lt;em&gt;snapshot-at-the-beginning&lt;/em&gt; marking. If any running agent &lt;strong&gt;started before&lt;/strong&gt; an object went quiet, the object stays alive, because that agent may have started using it and just hasn't touched it lately.&lt;/p&gt;

&lt;p&gt;This single rule covers three cases that would otherwise need special code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the temp directory of a session that's still running;&lt;/li&gt;
&lt;li&gt;a transcript that a running session may still append to;&lt;/li&gt;
&lt;li&gt;a plugin version that an agent loaded before you updated the plugin.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It already paid off during testing. On my machine, a real Claude Code session had been running for five days. Its five-day-old temp directory was correctly kept alive. To see it collected in the test, I had to age the directory to 60 days.&lt;/p&gt;

&lt;h3&gt;
  
  
  The barrier: agents don't stop while you collect
&lt;/h3&gt;

&lt;p&gt;Between deciding and acting, a worktree can get a new commit, a temp directory can get a cloned repo, and a pid can be reused by a different process. So right before each action, the heap re-reads the object's facts, the process table is refreshed, and &lt;code&gt;mark&lt;/code&gt; runs again. If anything changed, the object is skipped. Ending up alive is not an error.&lt;/p&gt;

&lt;p&gt;Processes are identified by &lt;strong&gt;pid plus start time&lt;/strong&gt;, never pid alone, which protects against pid reuse.&lt;/p&gt;

&lt;h3&gt;
  
  
  Orphans: identity, not names
&lt;/h3&gt;

&lt;p&gt;Most existing MCP cleanup scripts match by name: anything with &lt;code&gt;mcp&lt;/code&gt; in its command line, re-parented to &lt;code&gt;init&lt;/code&gt;. That also catches your own detached servers.&lt;/p&gt;

&lt;p&gt;Agent GC is deliberately narrower. A process counts as an orphaned helper only if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it was re-parented to &lt;code&gt;init&lt;/code&gt;/&lt;code&gt;launchd&lt;/code&gt;/&lt;code&gt;systemd&lt;/code&gt;, &lt;strong&gt;and&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Agent GC saw it as a direct child of an agent, &lt;strong&gt;or&lt;/strong&gt; its command line exactly matches an MCP server in your config.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every signal goes through a single guard that refuses pid 0, pid 1, process groups, and Agent GC's own process tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quarantine instead of delete
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjzigcvb0d9fni6z13omc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjzigcvb0d9fni6z13omc.png" alt="Object lifecycle" width="438" height="1100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Files and directories&lt;/strong&gt; are renamed into a graveyard with a manifest. A rename on the same file system is instant and never leaves a half-moved tree. Temp files stay for a day, everything else for a week. Under disk pressure the graveyard shrinks, and it never takes more than 10% of free space.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Worktrees&lt;/strong&gt; are locked with &lt;code&gt;git worktree lock&lt;/code&gt; before they move, so git's own &lt;code&gt;prune&lt;/code&gt; doesn't destroy the record. A restored worktree comes back whole.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Processes&lt;/strong&gt; are paused with &lt;code&gt;SIGSTOP&lt;/code&gt;, not killed. For an hour you can resume one, and then it's marked &lt;em&gt;spared&lt;/em&gt; and never taken again. Only after that hour, and only if it's still the same process and still stopped, does it get &lt;code&gt;SIGTERM&lt;/code&gt; and eventually &lt;code&gt;SIGKILL&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The trade-off is that disk space is freed later. That seemed like the right price.&lt;/p&gt;

&lt;h2&gt;
  
  
  Live mode: event-driven, not cron
&lt;/h2&gt;

&lt;p&gt;A 15-minute timer means orphaned MCP servers keep running for up to 15 minutes. Live mode reacts to agent exits instead. It uses kqueue &lt;code&gt;NOTE_EXIT&lt;/code&gt; on macOS and &lt;code&gt;pidfd&lt;/code&gt; on Linux. When an agent exits, the loop waits 2 seconds for its helpers to be re-parented, then runs a young collection. The orphans are suspended seconds after their agent is gone.&lt;/p&gt;

&lt;p&gt;Between events the loop doesn't poll. It asks &lt;code&gt;mark&lt;/code&gt; &lt;em&gt;when&lt;/em&gt; each object stops being alive purely because of time (for example &lt;code&gt;last_touch + min_idle&lt;/code&gt;), and sleeps until the earliest such deadline. File system events (FSEvents/inotify) trigger a recalculation. When idle, the daemon uses about 0% CPU and 25 MB of RAM.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it deliberately doesn't do
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No agent memory.&lt;/strong&gt; Deleting a memory can't be undone, and its value can't be judged by a rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No automatic context changes.&lt;/strong&gt; It measures and suggests; you decide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No &lt;code&gt;node_modules&lt;/code&gt; or build caches.&lt;/strong&gt; Other tools do that well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing leaves your machine.&lt;/strong&gt; No telemetry, no network calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;It's open source (MIT), written in Rust, with a small tray app built on Tauri. It works with Claude Code and Codex CLI on macOS and Linux.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew install --cask dimitreee/tap/agent-gc-app   &lt;span class="c"&gt;# menu bar app, macOS&lt;/span&gt;
brew install dimitreee/tap/agent-gc              &lt;span class="c"&gt;# CLI&lt;/span&gt;
npx agent-gc-cli                                 &lt;span class="c"&gt;# or via npm&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/Dimitreee/agent_gc" rel="noopener noreferrer"&gt;github.com/Dimitreee/agent_gc&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Full design write-up, with the safety argument, test setup and comparison to similar tools: &lt;a href="https://github.com/Dimitreee/agent_gc/blob/main/docs/how-it-works.md" rel="noopener noreferrer"&gt;how-it-works.md&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'd love to hear what your session start costs, and which leftovers it misses on your machine. Cursor and Gemini CLI support is next.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rust</category>
      <category>opensource</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
