今日已更新 244 条资讯 | 累计 27419 条内容
关于我们

My AI Agent's Temp Files Were Leaking Across Runs. Here's the Guard Pattern That Stopped It.

Chen Yuan 2026年08月01日 20:15 0 次阅读 来源:Dev.to

When an AI agent runs a multi-step pipeline, every step creates temporary files. Article drafts, image uploads, JSON payloads, log files. Over fifty runs, these files accumulate. Some get cleaned up, some don't. And the ones that don't cause the next run to fail in confusing ways. I hit this exact problem with my publishing pipeline. A failed cleanup from run #12 left a stale devto_article.json in the working directory. Run #13 picked it up, parsed it, and published a draft with last week's title. The logs showed "JSON loaded successfully" — which was technically true. The file was valid JSON. It just belonged to the wrong run. The fix was a Guard class that sits between the pipeline and the filesystem. Every file the pipeline creates must be registered before the pipeline starts. Any file that appears without registration halts the pipeline immediately. Run identity gets embedded into every file, so even if a cleanup fails, the next run can tell the file doesn't belong. The Problem With Temp Files Temp files are invisible by design. You create them, use them, delete them. But when deletion fails — file lock, process crash, permission error — the file becomes a ghost. It exists on disk but nobody remembers it's there. The next run scans the directory, finds the ghost, and treats it as intentional. This is especially dangerous for JSON files because they're always valid. A stale manifest.json looks identical to a fresh one. The only difference is the content, and the loader doesn't check content provenance. Here's a concrete example from my pipeline: # The naive approach — just check if the file exists def load_manifest ( path ): if not path . exists (): return None return json . loads ( path . read_text ()) This code returns valid data from any run, any day, any context. It answers "can I read this file?" but not "should I read this file?" That distinction is the entire bug. The Guard Pattern The Guard class solves this by requiring every temp file to be registered

本文内容来源于互联网,版权归原作者所有
查看原文