Repository navigation
fix(skills): install and update remove bytecode caches from deployed skills - #5837
Open
Daniel Meppiel (danielmeppiel) wants to merge 5 commits into
Open
Daniel Meppiel (danielmeppiel) wants to merge 5 commits into
Daniel Meppiel (danielmeppiel) wants to merge 5 commits into
Conversation
…skills (#5829) A skill script that runs leaves __pycache__/*.pyc in the deployed skill folder; install and update now prune those caches from every APM-owned skill folder they write or refresh, on the full copy and the unchanged fast path. One owner, integration/skill_bytecode.py, removes only files is_python_bytecode_cache_path matches; uninstall delegates to it. A new closed-world rule keeps cache deletion in that owner. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
apm-spec-waiver: skill deploy cleanup of interpreter caches; no OpenAPM requirement changes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Cache pruning can leave stale bytecode while installation succeeds, and the reinstall tests do not exercise the unchanged fast path.
2 open issues
What changed in this PR
This PR addresses #5829 by sharing bytecode-cache cleanup across skill installation, updates, and uninstall.
Changes:
- Adds a shared cache-pruning helper and connects deployment and cleanup paths.
- Adds lifecycle tests and architecture guards.
- Records the user-visible fix in a changelog fragment.
| File | Description |
|---|---|
tests/unit/scripts/test_architecture_runner.py |
Registers the new guard in the expected inventory. |
tests/integration/test_skill_bytecode_cache_lifecycle.py |
Tests cache cleanup and preservation boundaries. |
tests/integration/test_architecture_owner_rule_mutations.py |
Adds a mutation for missing deployment cleanup. |
src/apm_cli/integration/skill_integrator.py |
Calls cache pruning after skill deployment. |
src/apm_cli/integration/skill_bytecode.py |
Introduces shared cache-removal helpers. |
src/apm_cli/integration/cleanup.py |
Delegates uninstall cache cleanup. |
scripts/architecture_linter/checks/install_skill_bytecode_cache.py |
Guards centralized cache deletion and required callers. |
scripts/architecture_linter/checks/install_deployment_analyzers.py |
Registers the new architecture rule. |
scripts/architecture_linter/checks/install_cleanup_target_roots.py |
Updates the permitted directory-removal location. |
changelog.d/5837.fixed.md |
Documents install and update cache cleanup. |
.apm/architecture/owners/install-deployment.json |
Registers the helper and its guard. |
Review effort: Balanced
Install never fails on a bytecode cache it cannot remove: the cache is kept and named once as a warning. A cache holding a link or a file that is not bytecode is kept whole and named in verbose output. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A bytecode-named file link and a directory link inside __pycache__ keep the cache whole through install and uninstall, with their targets untouched; the probe for a skill folder is one lstat. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add two lifecycle cells: - a cache-only sub-skill reinstall that takes the unchanged path (the deployed SKILL.md keeps its inode) and still prunes the caches; - a cache holding a nested non-bytecode file, kept whole and named in verbose output. Test-only; src is unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Daniel Meppiel (danielmeppiel)
added a commit
that referenced
this pull request
Dec 3, 2026
Re-measured every fs.scandir_calls and fs.open_reads row on the composed train-5 tip ace1dbb (Linux PR CI Lifecycle Smoke), budget = measured + 5%. The earlier rows were measured on this PR's own base, which lacked the train links underneath. Bisected per link: - #5837 (train 3 B3) lists each deployed skill root once more: install-50-skills 654 -> 704 (+50), cold/warm installs +40, install-g-50-skills +50, plugin journeys +78 / +132; - #5842 (train 5 B5) removes listings: install-g-50-skills -160, install-g-14-plugins -543, repass -1031; plugin reads -27 / -44; - no other link moves these counters; other open_reads moves are run-to-run variation of up to 3. Refs #5838 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Daniel Meppiel (danielmeppiel)
added a commit
that referenced
this pull request
Dec 4, 2026
Slice 5 of #5838, step 2. - scan_package_tree (utils/content_hash.py) walks a tree once without following links and reads each payload file once, yielding the payload set, each file's SHA-256, the package digests (legacy and framed, from the same bytes) and, for a strict scan, the link/special/hardlink/ junction refusals. - verify_unchanged_local_plugin scans the source and the image once each (leaf listings [4, 3, 3] -> [1, 1, 1]); the image's raw part is checked file by file against the source's digests and the .apm/skills copies against theirs, replacing the image package-digest pass and the member re-hash. Tamper reasons are unchanged. - detect_package_type: the verifier reuses its eligibility detection, and the resolver hands its detection to validate_apm_package (consumed before any normalization; nothing is written in between). On the 23-plugin re-pass 161 -> 115 calls; a run-wide path memo was rejected because normalization rewrites images in place. - The #5837 bytecode prune reuses the copy's pre-walk (tree_file_hashes collects the outermost __pycache__ folders) instead of walking each deployed skill again; uninstall keeps its own walk. The format-precedence rule pins the resolver's classification as `detected = detect_package_type(install_path)`. cost: verify reads each file once per tree; no extra syscall anywhere. Refs #5838 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
6 of 10 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
A skill script that runs leaves
__pycache__/*.pycinside the deployed skill folder. Until now,apm installandapm updateleft those caches in place, on both paths:SkillIntegrator._copy_skill_bundlewrites only the package's members and preserves destination siblings, so an existing<skill>/__pycache__/was never touched.unchangedfast path:is_skill_dir_identical_to_sourceusesfilecmp.dircmp, whose default ignores include__pycache__. A cached skill therefore still counted as identical, and nothing ran.apm uninstallalready removed these caches. Now one owner handles all three:integration/skill_bytecode.py(new)prune_python_bytecode_caches(root)finds__pycache__directories and walks only those. It deletes only files thatsecurity.gate.is_python_bytecode_cache_pathmatches and removes a cache directory once it is empty. It never follows or removes a symlink, and a cache holding anything else stays.skill_integrator._copy_skill_bundlecopy_skill_to_target, package skills, promoted sub-skills).cleanup._remove_emptied_tree_remove_empty_directorymoves with it. Behaviour is unchanged (uninstall cells green).install-deployment-skill-bytecode-cache. The owner must define the function, both callers must call it, and no other function underintegration/,install/orcommands/uninstall/may match bytecode caches and delete files. One documented exception,_safe_remove_skill_directory, removes a whole verified skill folder. The rule ID is in_EXPECTED_RULE_ID_TEXT, with a sortedMutationCase.install-deployment-cleanup-target-rootsnow names the movedrmdirsite.Bounds: only APM-owned skill folders, meaning the destination
_copy_skill_bundlewrites after its ownership and collision gate. Loose*.pycfiles outside__pycache__keep today's behaviour, and a user's own skill folder is never touched.Cost: each deployed skill gets one directory listing per directory level to find
__pycache__. Only a found cache directory is walked. There are no file reads, and nothing is written unless a cache exists. The unchanged path already walks the same tree indircmp.Issue and approved scope
Issue: Fixes #5829
Human scope-approval comment: #5829 (comment)
Does this PR complete the issue, or what remains? It completes it.
Type of change
Testing
tests/integration/test_skill_bytecode_cache_lifecycle.py(real CLI, local packages):test_install_removes_bytecode_caches_from_a_deployed_skill[unchanged-native]...[unchanged-subskills]...[changed-native]...[changed-subskills]test_a_package_shipped_bytecode_cache_is_never_deployed(guard)test_a_user_skill_folder_keeps_its_bytecode_cacheEach red cell also asserts that the loose
loose.pyc, the user'snotes.txt,SKILL.mdandscripts/run.pystay.Changelog
changelog.d/<pr>.<section>.md, holdingone line: user-visible effect first, 25 words or fewer, ending with
(#PR); breaking changes only under upgrade notes; no Highlights; nonew lines under
## [Unreleased]. Seechangelog.d/README.mdand.github/instructions/changelog.instructions.md.Spec conformance (OpenAPM v0.1)
CONFORMANCE.{md,json}regeneratedAgent assessment
Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com