@@ -324,8 +324,10 @@ def finalize_setup(
324324 all_extension = list (extension_files or [])
325325
326326 # Filter agent_files: only keep files under the agent's directory
327- # tree. setup() may return shared project files (e.g. .specify/)
328- # which must not be tracked per-agent.
327+ # tree. setup() returns *all* scaffolded files (including shared
328+ # project infrastructure in .specify/) but only agent-owned files
329+ # should be tracked per-agent — shared files are not removed
330+ # during teardown/switch.
329331 agent_root = self .agent_dir (project_path )
330332 agent_root_resolved = agent_root .resolve ()
331333 all_agent : List [Path ] = []
@@ -334,25 +336,22 @@ def finalize_setup(
334336 p .resolve ().relative_to (agent_root_resolved )
335337 all_agent .append (p )
336338 except ValueError :
337- pass # shared file — not tracked per-agent
338-
339- # Scan the agent's directory tree for files created by the init
340- # pipeline that setup() did not report directly. We scan the
341- # entire agent directory (the parent of commands_dir) because
342- # skills-migrated agents replace the commands directory with a
343- # sibling skills directory during init.
339+ pass
340+
341+ # Scan the agent's directory tree for files created by later
342+ # init pipeline steps (skills, presets, extensions) that
343+ # setup() did not report. We scan the agent root directory
344+ # (e.g. .claude/) so we catch both commands and skills
345+ # directories (skills-migrated agents replace the commands
346+ # directory with a sibling skills directory during init).
344347 if self .manifest .commands_dir :
345- commands_dir = project_path / self .manifest .commands_dir
346- # Scan the agent root (e.g. .claude/) so we catch both
347- # commands and skills directories.
348- agent_root = commands_dir .parent
349- agent_set = {p .resolve () for p in all_agent }
350- for scan_dir in (commands_dir , agent_root ):
351- if scan_dir .is_dir ():
352- for p in scan_dir .rglob ("*" ):
353- if p .is_file () and p .resolve () not in agent_set :
354- all_agent .append (p )
355- agent_set .add (p .resolve ())
348+ agent_root = self .agent_dir (project_path )
349+ if agent_root .is_dir ():
350+ agent_set = {p .resolve () for p in all_agent }
351+ for p in agent_root .rglob ("*" ):
352+ if p .is_file () and p .resolve () not in agent_set :
353+ all_agent .append (p )
354+ agent_set .add (p .resolve ())
356355
357356 record_installed_files (
358357 project_path ,
0 commit comments