Skip to content

fix(skills): install and update remove bytecode caches from deployed skills - #5837

Open
Daniel Meppiel (danielmeppiel) wants to merge 5 commits into
mainfrom
skill-pycache-prune
Open

Daniel Meppiel (danielmeppiel) wants to merge 5 commits into
mainfrom
skill-pycache-prune

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

A skill script that runs leaves __pycache__/*.pyc inside the deployed skill folder. Until now, apm install and apm update left those caches in place, on both paths:

  • Full copy: SkillIntegrator._copy_skill_bundle writes only the package's members and preserves destination siblings, so an existing <skill>/__pycache__/ was never touched.
  • unchanged fast path: is_skill_dir_identical_to_source uses filecmp.dircmp, whose default ignores include __pycache__. A cached skill therefore still counted as identical, and nothing ran.

apm uninstall already removed these caches. Now one owner handles all three:

Where Change
integration/skill_bytecode.py (new) prune_python_bytecode_caches(root) finds __pycache__ directories and walks only those. It deletes only files that security.gate.is_python_bytecode_cache_path matches 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_bundle One call after the copy, for changed and unchanged skills alike. All three skill deploy paths go through it (copy_skill_to_target, package skills, promoted sub-skills).
cleanup._remove_emptied_tree Uninstall delegates its bytecode step to the owner; _remove_empty_directory moves with it. Behaviour is unchanged (uninstall cells green).
Architecture linter New closed-world rule install-deployment-skill-bytecode-cache. The owner must define the function, both callers must call it, and no other function under integration/, install/ or commands/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 sorted MutationCase. install-deployment-cleanup-target-roots now names the moved rmdir site.

Bounds: only APM-owned skill folders, meaning the destination _copy_skill_bundle writes after its ownership and collision gate. Loose *.pyc files 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 in dircmp.

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

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

tests/integration/test_skill_bytecode_cache_lifecycle.py (real CLI, local packages):

Cell main 641743f After
test_install_removes_bytecode_caches_from_a_deployed_skill[unchanged-native] failed pass
...[unchanged-subskills] failed pass
...[changed-native] failed pass
...[changed-subskills] failed pass
test_a_package_shipped_bytecode_cache_is_never_deployed (guard) pass pass
test_a_user_skill_folder_keeps_its_bytecode_cache pass pass

Each red cell also asserts that the loose loose.pyc, the user's notes.txt, SKILL.md and scripts/run.py stay.

  • The architecture runner inventory and the full owner-rule mutation suite pass, including the new case, as do the uninstall and cleanup cells: 2069 passed.
  • Skill integrator units plus the uninstall lifecycle modules: 725 passed.
  • CI-mirror lint: ruff 0.15.12, R0801, auth signals, complexity ratchet, tm006 and architecture boundaries all pass.

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour.

Agent assessment

  • Change confidence: high. It is one owner with a closed-world guard and mutation, and red-first lifecycle cells cover both paths and both skill layouts.
  • Diagnosis confidence: high. Both survival paths are confirmed at file:line and reproduced on main with the real CLI.
  • Evidence fit: strong. The four red cells fail on main for the stated reason; the guard and user-folder cells pin the boundaries.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

…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>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 08:45
apm-spec-waiver: skill deploy cleanup of interpreter caches; no OpenAPM requirement changes.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
@danielmeppiel Daniel Meppiel (danielmeppiel) added the full-integration Run the full integration suite (ci-integration.yml) on this PR before merge label Dec 3, 2026
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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-integration Run the full integration suite (ci-integration.yml) on this PR before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Install and update should clear Python bytecode caches in skill folders, as uninstall does

2 participants