{"id":"cdf80746-4434-422c-8ae1-b8050339e91a","shortId":"37WVgB","kind":"skill","title":"lifecycle-scoped-resource-cleanup","tagline":"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.","description":"# SKILL: Lifecycle-Scoped Resource Cleanup\n**Domain:** lifecycle-management\n**Trigger:** Each agent run, regardless of outcome, must unwind resources reserved for its lifetime without assuming the parent session will later re-claim them.\n**Source Pattern:** Distilled from reviewed subagent orchestration, isolation, and lifecycle implementations.\n\n## Core Method\nTreat 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.\n\n## Key Rules\n- Run lifecycle cleanup from one unconditional `finally`/defer path so success, failure, and cancellation all converge on the same teardown logic.\n- Guard optional cleanup steps with the same feature flags that created those resources in the first place.\n- Remove agent-owned records entirely rather than leaving empty placeholders in shared state maps.\n- Stop background jobs, hooks, and watches before the parent session can reuse the same identifiers or workspaces.\n\n## Example Application\nWhen 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.\n\n## Anti-Patterns\n- Do not perform cleanup only on the happy path or only in `catch`; missing the unconditional cleanup path leaves clients, hooks, or jobs running forever.\n- Do not leave temporary caches or agent-owned state records behind after completion; stale state can leak memory and corrupt later runs.","tags":["lifecycle","scoped","resource","cleanup","cskill","agents","ychampion","agent-skills","ai-agents","cli","coding-agents","context-engineering"],"capabilities":["skill","source-ychampion","skill-lifecycle-scoped-resource-cleanup","topic-agent-skills","topic-ai-agents","topic-cli","topic-coding-agents","topic-context-engineering","topic-developer-tools","topic-mcp","topic-multi-agent","topic-terminal-ui"],"categories":["cskill-agents"],"synonyms":[],"warnings":[],"endpointUrl":"https://skills.sh/ychampion/cskill-agents/lifecycle-scoped-resource-cleanup","protocol":"skill","transport":"skills-sh","auth":{"type":"none","details":{"cli":"npx skills add ychampion/cskill-agents","source_repo":"https://github.com/ychampion/cskill-agents","install_from":"skills.sh"}},"qualityScore":"0.467","qualityRationale":"deterministic score 0.47 from registry signals: · indexed on github topic:agent-skills · 34 github stars · SKILL.md body (1,892 chars)","verified":false,"liveness":"unknown","lastLivenessCheck":null,"agentReviews":{"count":0,"score_avg":null,"cost_usd_avg":null,"success_rate":null,"latency_p50_ms":null,"narrative_summary":null,"summary_updated_at":null},"enrichmentModel":"deterministic:skill-github:v1","enrichmentVersion":1,"enrichedAt":"2026-04-22T06:56:50.189Z","embedding":null,"createdAt":"2026-04-18T22:20:35.680Z","updatedAt":"2026-04-22T06:56:50.189Z","lastSeenAt":"2026-04-22T06:56:50.189Z","tsv":"'/defer':165 'abort':18,100,235 'agent':9,22,44,83,110,124,136,148,198,234,298 'agent-own':123,197,297 'agent-scop':8 'anti':264 'anti-pattern':263 'applic':229 'assum':57 'background':30,131,212,232 'behalf':138 'behind':302 'block':15 'bookkeep':126 'cach':28,118,295 'cancel':171 'catch':278 'child':82 'claim':65 'cleanup':5,37,92,140,160,181,240,269,282 'clear':116 'client':26,285 'close':242 'complet':304 'converg':173 'core':78 'corrupt':311 'creat':189 'disconnect':107 'distil':69 'domain':38 'due':236 'empti':205 'entir':201 'everi':7 'exampl':228 'execut':101 'fail':98 'failur':169 'featur':186 'final':14,164 'finish':20,97 'fire':249 'first':194 'flag':187 'forev':290 'guard':179 'happi':273 'hook':27,113,214,246,286 'identifi':225 'implement':77 'integr':111 'isol':74 'job':132,213,288 'keep':139 'key':156 'kill':254 'later':62,312 'leak':308 'leav':23,204,284,293 'lifecycl':2,34,40,76,87,159 'lifecycle-manag':39 'lifecycle-scop':33 'lifecycle-scoped-resource-cleanup':1 'lifetim':55 'linger':25 'logic':178 'loop':257 'manag':41 'map':210 'mcp':243 'memori':309 'method':79 'miss':279 'must':49 'one':86,147,162 'option':180 'orchestr':73 'orphan':255 'outcom':48 'own':125,144,199,299 'parent':59,150,219 'path':93,166,274,283 'pattern':68,265 'per':109 'per-ag':108 'perform':268 'place':195 'placehold':206 'predict':103 'rather':202 're':64 're-claim':63 'record':127,200,301 'regardless':46 'releas':6 'remain':154,261 'remov':122,196,245 'reserv':52 'resourc':4,11,36,51,143,191 'respons':262 'reus':222 'review':71 'routin':241 'rule':157 'run':45,84,96,158,289,313 'scope':3,10,35,141 'sequenc':105 'session':60,153,220 'share':208 'shell':256 'sibl':152 'singl':90 'skill':32 'skill-lifecycle-scoped-resource-cleanup' 'socket':244 'sourc':67 'source-ychampion' 'spawn':17 'stage':120 'stale':305 'start':133 'state':121,209,300,306 'step':182 'stop':129,211 'subag':72,252 'subsequ':251 'success':168 'task':31 'teardown':104,177 'temporari':117,294 'termin':260 'timeout':238 'topic-agent-skills' 'topic-ai-agents' 'topic-cli' 'topic-coding-agents' 'topic-context-engineering' 'topic-developer-tools' 'topic-mcp' 'topic-multi-agent' 'topic-terminal-ui' 'treat':80 'trigger':42 'uncondit':91,163,281 'unregist':112 'untouch':155 'unwind':50 'watch':216 'watcher':115 'without':56 'workflow':233 'workspac':227 'would':248","prices":[{"id":"51e6bd49-e30f-46d8-8d7d-21a4c183b500","listingId":"cdf80746-4434-422c-8ae1-b8050339e91a","amountUsd":"0","unit":"free","nativeCurrency":null,"nativeAmount":null,"chain":null,"payTo":null,"paymentMethod":"skill-free","isPrimary":true,"details":{"org":"ychampion","category":"cskill-agents","install_from":"skills.sh"},"createdAt":"2026-04-18T22:20:35.680Z"}],"sources":[{"listingId":"cdf80746-4434-422c-8ae1-b8050339e91a","source":"github","sourceId":"ychampion/cskill-agents/lifecycle-scoped-resource-cleanup","sourceUrl":"https://github.com/ychampion/cskill-agents/tree/main/skills/lifecycle-scoped-resource-cleanup","isPrimary":false,"firstSeenAt":"2026-04-18T22:20:35.680Z","lastSeenAt":"2026-04-22T06:56:50.189Z"}],"details":{"listingId":"cdf80746-4434-422c-8ae1-b8050339e91a","quickStartSnippet":null,"exampleRequest":null,"exampleResponse":null,"schema":null,"openapiUrl":null,"agentsTxtUrl":null,"citations":[],"useCases":[],"bestFor":[],"notFor":[],"kindDetails":{"org":"ychampion","slug":"lifecycle-scoped-resource-cleanup","github":{"repo":"ychampion/cskill-agents","stars":34,"topics":["agent-skills","ai-agents","cli","coding-agents","context-engineering","developer-tools","mcp","multi-agent","terminal-ui"],"license":"mit","html_url":"https://github.com/ychampion/cskill-agents","pushed_at":"2026-04-04T14:13:23Z","description":"Agent skills for coding CLIs, multi-agent runtimes, context engines, MCP extensions, and terminal tooling. Instead of using claude code's source code, give your agent skills to create your own!","skill_md_sha":"243c6f35416093c9d4b5515c4037c59d8e24d4f3","skill_md_path":"skills/lifecycle-scoped-resource-cleanup/SKILL.md","default_branch":"main","skill_tree_url":"https://github.com/ychampion/cskill-agents/tree/main/skills/lifecycle-scoped-resource-cleanup"},"layout":"multi","source":"github","category":"cskill-agents","frontmatter":{"name":"lifecycle-scoped-resource-cleanup","description":"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."},"skills_sh_url":"https://skills.sh/ychampion/cskill-agents/lifecycle-scoped-resource-cleanup"},"updatedAt":"2026-04-22T06:56:50.189Z"}}