Skip to content
Pocket Grove
Developer blog

Agent Cleanup Needs Proof of Ownership

Published · 7 min read

By · Pocket Grove

Original research brief: . Sources reviewed: .

Quick answer: Delete a resource only when the cleanup system can establish that it created or explicitly owns that exact resource, the resource is no longer in use, and retention rules permit removal. A familiar path, old timestamp, or clean Git status is insufficient. Preserve ambiguous resources and report why they were skipped.

Cleanup often starts as a convenient final step: remove the temporary directory, stop the preview server, discard the worker checkout. As an agent workflow grows, that step becomes responsible for deciding which user resources may disappear.

The decision deserves a concrete contract. The same directory name may be reused by a later task. A process may outlive the session that started it. A shared application may have been open before the agent arrived.

This article develops an illustrative cleanup design for a task that creates a scratch directory and a preview process. No deletion or process-control experiment was executed for the example.

Separate ownership from location

A directory under a runtime's usual scratch root is a candidate for inspection. Its location alone does not establish who created it, whether someone adopted it for longer use, or whether another task now depends on it.

The same applies to descriptive labels. Kubernetes provides a useful concrete example: its documentation distinguishes labels used to group resources from owner references that identify ownership relationships. An owner reference includes identity information such as the owner's UID. Kubernetes owners and dependents documents that mechanism.

For a local agent, use a trusted creation record. Record resources when they are created, and distinguish those the task owns from those it merely uses. Opening an existing browser or connecting to an existing server should produce a borrowed-resource entry, with no implied right to terminate it.

The registry needs protection appropriate to the environment. A marker that any unrelated process can rewrite is a weak authority for a privileged deletion service.

Identify the resource generation

Imagine a directory named preview-42. Task A creates it, finishes, and removes it. Later, task B creates a new directory at the same path. A delayed cleanup message from A must not authorize removing B's directory.

Attach a creation identity to the resource and compare it at cleanup. The identity must belong to that generation, independently of the reusable name. Kubernetes similarly distinguishes object names from UIDs, which identify particular object instances across deletion and recreation. Its object identity documentation explains the distinction.

Here is a proposed record, using illustrative values:

{
  "task_id": "task-A",
  "resource_id": "scratch-generation-7",
  "kind": "directory",
  "path": "/managed-scratch/preview-42",
  "relationship": "owned",
  "lifecycle": "active",
  "retention": "until-artifact-accepted",
  "cleanup_allowed": false
}

This JSON is bookkeeping, not an enforcement mechanism. The creator must bind the record to the actual resource, and the cleanup operation must verify that binding. If the binding cannot be established after a restart, mark the resource unresolved.

Make eligibility a separate decision

Ownership answers who may manage a resource. Eligibility answers whether removal is appropriate now.

For the scratch directory, require that the task has stopped writing, its accepted output is preserved elsewhere, and its retention condition has been satisfied. For the preview process, require that the task no longer needs the preview and that no authorized consumer has retained it.

A useful decision table is:

Observation Cleanup decision in this design
Exact owned generation, no active users, retention satisfied Eligible for removal
Owned resource still serving an active task Keep it
Existing resource borrowed by the task Leave lifecycle control with its owner
Recorded path now identifies a different generation Reject the stale request
Ownership or activity cannot be established Preserve and record the unresolved reason

An age threshold can nominate resources for review. It should not replace ownership and activity evidence. A directory timestamp does not describe the full lifecycle of every file, process, or task associated with that directory.

Avoid an unbounded accumulation of unresolved resources by limiting new allocations and surfacing the backlog. Disk pressure is a reason to resolve ownership or reduce work, rather than silently expanding deletion authority.

Keep the check attached to the operation

There is a race if the system checks a path and later deletes whatever happens to occupy it. Another actor can replace or redirect the target between those steps.

Use platform facilities that constrain resolution and retain useful identity across the operation. On Linux, openat2 offers controls such as RESOLVE_BENEATH and RESOLVE_NO_SYMLINKS. They can restrict how paths are resolved relative to a directory handle. The Linux manual documents those controls and their errors.

That API does not itself implement safe recursive deletion. A removal routine still needs to handle descendants, concurrent changes, mount boundaries, and its permission model. Treat path canonicalization as one input to the design, rather than a complete answer to races.

Coordinate the lifecycle transition too. For example, atomically move an eligible registry entry from idle to deleting while preventing another worker from acquiring it. Recheck the generation at removal, then record the observed result. A worker that can resume writing after losing ownership needs a separate mechanism that prevents those stale writes.

Stop the process you actually created

A saved process number can become stale. Prefer a process handle retained by the creating supervisor, with lifecycle tracking that identifies the intended process.

Linux PID file descriptors are one platform-specific mechanism: they refer to a process and can be used with supported monitoring and signalling operations. Obtaining a descriptor later from an old PID still needs care about identity. The pidfd_open manual describes the API and the conditions around process exit and PID reuse.

Define whether ownership includes a whole process group or only one process. If descendants can escape the supervision boundary, killing the original process does not establish that cleanup finished. Likewise, terminating every process with the same executable name can reach unrelated work.

For a shared GUI application, release the task's document or session through a supported interface where possible. Closing the entire application needs its own ownership decision.

Treat Git cleanup as a resource operation

A clean worktree can still contain a branch someone intends to retain. Git status describes changes relative to the checkout; it does not express that person's retention intent.

Git worktree locks can protect against ordinary worktree pruning, moving, and removal. Git also documents force behavior, and those locks cannot stop arbitrary filesystem deletion. Use the Git worktree manual to understand that protection's scope.

Before a task-owned checkout is removed, identify its branch, preserve the required result, account for relevant untracked or ignored files, and check for explicit retention. If cleanup is interrupted, read back the actual state before assuming the removal succeeded or repeating it.

Review the cleanup contract

For each resource type, write down:

  • Who created it and who may authorize removal.
  • Whether the task owns or borrows it.
  • How the exact generation is identified.
  • Which activity and retention conditions prevent removal.
  • How races and stale workers are handled.
  • What evidence confirms removal, and where failures remain visible.

These rules belong in the same tool contract as the operation itself. A cleanup tool should accept an owned resource identity, enforce its scope, and explain a refusal without widening the target.

Begin with the cleanup command in your workflow that accepts the broadest path or process selector. Replace the implicit ownership assumption with an explicit resource record and a narrow removal decision. That makes routine cleanup reviewable before it ever encounters an ambiguous resource.

Apps from the studio

All apps

These practices come from shipping Pocket Grove's active apps. If you came here looking for something to install, start with one of these.

Related guides