Lifecycle scoped resource cleanup

From ychampion/cskill-agents · View interactive version

Lifecycle scoped resource cleanup is an AI agent skill from the GitHub repository ychampion/cskill-agents. Release every agent-scoped resource in the `finally` block so spawning, aborting, or finishing an agent leaves no lingering clients, hooks, caches, or background tasks.

How to use this skill

Skill file

SKILL: Lifecycle-Scoped Resource Cleanup

Domain: lifecycle-management

Trigger: Each agent run, regardless of outcome, must unwind resources reserved for its lifetime without assuming the parent session will later re-claim them.

Source Pattern: Distilled from reviewed subagent orchestration, isolation, and lifecycle implementations.

Core Method

Treat each child agent run as one lifecycle with a single unconditional cleanup path. After the run finishes, fails, or aborts, execute a predictable teardown sequence that disconnects per-agent integrations, unregisters hooks and watchers, clears temporary caches and staging state, removes agent-owned bookkeeping records, and stops any background jobs started on that agent’s behalf. Keep cleanup scoped to resources owned by that one agent so parent and sibling sessions remain untouched.

Key Rules

Example Application

When a background workflow agent aborts due to timeout, these cleanup routines close MCP sockets, remove hooks that would fire on subsequent subagents, and kill orphaned shell loops so the terminal remains responsive.

Anti-Patterns

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.