Skip to content

[BUG] Subdirectory/skill-subset package content hashes differ across OSes due to synthetic apm.yml line endings #2619

Closed

Description

@McNultyyy

Environment

Component Value
APM CLI version 0.28.0 (also reproduces on 0.26.0)
Windows Windows 11, Python 3.12
Linux python:3.12-slim Docker image (Debian)
git core.autocrlf Irrelevant -- disproven as cause (see below)

Description

When resolving a git dependency with a skills: subset (subdirectory package), APM writes a synthetic apm.yml into apm_modules/<owner>/<repo>/. This file is written with the platform's native line endings (CRLF on Windows, LF on Linux). compute_package_hash() in src/apm_cli/utils/content_hash.py hashes raw file bytes with no CRLF normalisation, producing a different content_hash on each OS for byte-identical upstream content.

This makes apm.lock.yaml non-portable: a lockfile generated on Windows fails apm install --frozen on Linux CI and vice versa.

Relation to #2187 / PR #2223

This is the same bug class as #2187 ("[BUG] Virtual single-file package content hashes differ across OSes due to synthetic apm.yml line endings"), which was fixed by PR #2223 and shipped in 0.26.0.

However, that fix only covered download_virtual_file_package(). The code path exercised here -- download_subdirectory_package() -- was never patched. That is why the bug still reproduces on 0.26.0 and 0.28.0.

Minimal Reproduction

apm.yml

name: apm-hash-repro
version: 1.0.0
dependencies:
  apm:
    - git: anthropics/skills#2c7ec5e78b8e5d43ea02e90bb8826f6b9f147b0c
      skills:
        - pdf

Windows

# Clear cache
Remove-Item "$env:LOCALAPPDATA\apm\cache" -Recurse -Force -ErrorAction SilentlyContinue

mkdir C:\Temp\apm-mvr-win
Set-Content C:\Temp\apm-mvr-win\apm.yml -NoNewline -Value @"
name: apm-hash-repro
version: 1.0.0
dependencies:
  apm:
    - git: anthropics/skills#2c7ec5e78b8e5d43ea02e90bb8826f6b9f147b0c
      skills:
        - pdf
"@

cd C:\Temp\apm-mvr-win
apm lock

# Inspect synthetic file
$b = [IO.File]::ReadAllBytes("$PWD\apm_modules\anthropics\skills\apm.yml")
$crlf = 0; for ($i=0; $i -lt $b.Length-1; $i++) { if ($b[$i] -eq 13 -and $b[$i+1] -eq 10) { $crlf++ } }
"size=$($b.Length) crlf=$crlf"
Get-Content apm.lock.yaml

Linux (Docker)

mkdir -p /tmp/apm-mvr && cd /tmp/apm-mvr
cat > apm.yml <<'EOF'
name: apm-hash-repro
version: 1.0.0
dependencies:
  apm:
    - git: anthropics/skills#2c7ec5e78b8e5d43ea02e90bb8826f6b9f147b0c
      skills:
        - pdf
EOF

docker run --rm -v "$PWD:/work" -w /work \
  -e GH_TOKEN -e GITHUB_TOKEN \
  python:3.12-slim bash -c '
    apt-get update -qq && apt-get install -y -qq git >/dev/null 2>&1
    pip install -q apm-cli==0.28.0
    apm lock
    echo "--- synthetic manifest ---"
    wc -c apm_modules/anthropics/skills/apm.yml
    od -c apm_modules/anthropics/skills/apm.yml | grep -c "\\\\r" || echo "no CR bytes"
    grep content_hash apm.lock.yaml
  '

(Run natively on Linux without Docker if preferred -- the container is only used here to
contrast against a Windows host.)

Observed Results

Synthetic apm_modules/anthropics/skills/apm.yml content (identical text, different encoding)

name: skills
version: 2c7ec5e
description: ''
type: hybrid

Measurements

OS Synthetic file size CRLF pairs content_hash
Windows 63 bytes 4 sha256:e93ee1488ddae2ec5f09839f9aa258c432888edf7ae767ca86b0c4baac6ce7c4
Linux 59 bytes 0 sha256:606a7fcbcd7d293a932db6e97644f7e60e0a868607bc9ac5b82802d334f7d518

The 4-byte delta (63 - 59 = 4) corresponds exactly to the 4 CRLF pairs (\r\n -> \n saves 1 byte each). No other files differ.

Note that content_hash covers the whole downloaded package tree while skill_subset is
recorded separately in the lockfile, so the divergence is independent of how many skills are
selected. Verified: selecting a single skill (pdf) and selecting three (pdf, pptx,
xlsx) both produce e93ee148... on Windows and 606a7fcb... on Linux. A one-skill subset
is therefore the smallest form that reproduces.

Expected Behaviour

content_hash must be identical on all platforms for the same dependency at the same commit. The lockfile must be portable.

Root Cause

In src/apm_cli/utils/content_hash.py:

  • compute_package_hash() reads file bytes as-is and feeds them to SHA-256. Its docstring assumes content is already platform-canonical from git checkout, but the synthetic apm.yml is written by APM itself, not checked out from git.
  • The sibling function compute_file_hash() already normalises CRLF via _canonical_hash_bytes() -- so the inconsistency is within APM's own codebase.
  • The synthetic file is written in download_subdirectory_package() using Python's default text-mode open(), which emits platform-native line endings.

Disproven Alternative: core.autocrlf

Setting core.autocrlf=false and core.eol=lf globally on Windows does not change the hash. The synthetic file is written by APM's Python code (not checked out by git), so git's line-ending settings are irrelevant to this code path.

Real-World Impact

  • Teams using mixed OS (Windows dev machines + Linux CI) cannot share lockfiles
  • apm install --frozen fails in CI (ubuntu-24.04) with "content hash mismatch" when the lockfile was generated on Windows
  • Workaround: regenerate lockfile in CI (defeats the purpose of a lockfile)

Suggested Fix

Either (or both):

  1. Write the synthetic apm.yml with explicit LF line endings in download_subdirectory_package() (use open(..., newline='\n') or write in binary mode). This mirrors the fix in PR fix(deps): canonicalize virtual package manifest bytes #2223 for the virtual-file path.

  2. Route compute_package_hash() through _canonical_hash_bytes() to normalise CRLF before hashing, making the hash immune to platform line endings regardless of how files were written.

Option 1 is the more surgical fix; option 2 is defence-in-depth.

Versions Affected

Activity

  1. moved this from Todo to In Progress in APM Roadmapon Aug 18, 2026
  2. McNultyyy commented on Aug 18, 2026

    @McNultyyy
    ContributorAuthor

    Reproduction confirmed (on current main, not just 0.28.0)

    Ran the minimal repro from the description on Windows 11 (Python 3.13, isolated APM_CACHE_DIR, APM installed from main source) and on Linux (python:3.12-slim, Docker, same source):

    OS Synthetic file size CRLF pairs content_hash
    Windows (main) 63 bytes 4 sha256:e93ee148…
    Linux (main) 59 bytes 0 sha256:606a7fcb…

    Both hashes match this issue's table exactly. Additionally verified that converting only the synthetic apm.yml to LF in the Windows tree reproduces the Linux hash bit-for-bit — confirming the 4 CRLF pairs are the sole divergence.

    Two corrections to the root-cause analysis

    1. Code path. This exact repro does not go through download_subdirectory_package(). anthropics/skills has a .claude-plugin/ directory, so it is detected as a marketplace_plugin and the whole repo is downloaded via download_package() (the skills: subset only filters deployment). download_subdirectory_package() is equally affected, though, because both paths share the writes below.

    2. Write sites. The synthetic apm.yml is not written directly in the downloader. It is written twice by shared helpers invoked from both download paths:

      • plugin_parser.synthesize_apm_yml_from_plugin() — initial synthesis during validate_apm_package(), via plain text-mode open() (CRLF on Windows);
      • package_validator.stamp_plugin_version() — rewrites it with the short commit SHA via yaml_io.dump_yaml(), whose open() lacked newline="". This second write produces the final on-disk bytes (hence version: 2c7ec5e in the observed content).

      An exhaustive audit of every APM-written file landing in hashed package trees found one more affected site: inline plugin.json hooks objects serialized to .apm/hooks/hooks.json via Path.write_text() (platform-native newlines and locale encoding).

    Fix

    Draft PR #2620 takes suggested option 1 (LF-deterministic writes at every affected site, mirroring #2223). Option 2 (normalizing CRLF inside compute_package_hash()) was deliberately avoided: it would silently change recorded hashes for packages whose git-canonical content legitimately contains CRLF, invalidating existing correct lockfiles.

    With the fix, Windows and Linux both produce sha256:606a7fcb… for this repro, and a Windows-generated lockfile passes apm install --frozen on Linux (verified end-to-end in Docker).

  3. github-actions commented on Aug 19, 2026

    @github-actions

    Triage decision

    accept

    Proposed labels

    theme/portability
    area/lockfile
    type/bug
    status/accepted
    priority/high
    

    Milestone

    null

    Suggested next action

    Apply the same LF-deterministic write fix used in PR #2223 (for download_virtual_file_package) to download_subdirectory_package(), using open(..., newline='\n') or binary mode when writing the synthetic apm.yml.

    Suggested issue comment

    Thank you for this excellent, thorough bug report -- the reproduction
    steps, OS measurements, and root cause analysis are exactly what makes
    a fix straightforward to land.
    
    This is confirmed as a regression gap from PR #2223: the CRLF fix
    applied to `download_virtual_file_package()` was never ported to the
    sibling `download_subdirectory_package()` path. Because the synthetic
    `apm.yml` is written by APM's Python code (not by git checkout), the
    platform's default text-mode line endings produce a different SHA-256
    on Windows vs Linux, making the lockfile non-portable.
    
    The fix is surgical: in `download_subdirectory_package()`, write the
    synthetic file with `open(..., newline='\n')` or in binary mode --
    mirroring the PR #2223 approach. Defence-in-depth (routing
    `compute_package_hash` through `_canonical_hash_bytes`) is also valid
    but not required for this fix.
    
    This is marked `priority/high` because it silently breaks
    `apm install --frozen` in Linux CI for teams developing on Windows.
    
    If you would like to send a PR, the relevant code is in
    `src/apm_cli/utils/content_hash.py` and the write site is in the
    downloader for subdirectory packages. A regression test that writes a
    synthetic file on the current platform and asserts a stable hash would
    seal the fix. Happy to review!

    Per-lens notes (collapsed)

    DevX UX Expert -- User-Need Reviewer

    Cross-platform lockfile portability is a first-class promise of any
    package manager. The user need is concrete: mixed-OS teams (Windows
    dev + Linux CI) cannot share lockfiles because apm install --frozen
    fails in CI with a content hash mismatch. The issue maps directly to
    the APM lockfile contract and the reproducible-install guarantee. The
    reproduction steps are complete and the delta measurement (4 CRLF
    pairs = 4 extra bytes) is convincing. Full alignment with the npm/pip
    mental model where a committed lockfile should be portable.

    Supply Chain Security Expert -- Risk-Surface Reviewer

    This touches area/lockfile directly. The content_hash in
    apm.lock.yaml diverges by platform, undermining the integrity
    guarantee of the lockfile: a verifier consuming the Windows-generated
    lockfile will reject the Linux-installed tree and vice versa. Not a
    security vulnerability, but a correctness failure in the supply chain
    integrity surface. Theme theme/portability is correct; no
    theme/security elevation warranted since the root cause is line-
    ending encoding rather than a trust or authenticity gap.

    OSS Growth Hacker -- Contributor-Tone Reviewer

    McNultyyy has author_association: NONE -- likely a first-time
    contributor to microsoft/apm. The report quality is exceptional
    (measurements, root cause, suggested fix, disproven alternative). The
    reply acknowledges that quality explicitly and invites a PR, naming
    the exact files and suggesting a regression test. Tone is warm,
    specific, and actionable. The contributor can go from reading the
    reply to opening a PR without any follow-up questions.

    Python Architect -- Architecture Reviewer

    Not activated -- the fix is surgical (one write-mode change at a
    named call site), following a well-established pattern from PR #2223;
    no cross-cutting design decision is required before code can land.

    Doc Writer -- Documentation Reviewer

    Not activated -- this is a bug fix to an internal write path; no
    new user-facing documentation is required, and the existing lockfile
    portability contract in the README does not need revision.

    APM CEO -- Triage Arbiter

    Clear accept. The bug class is confirmed (same as #2187/PR #2223),
    the affected code path is named, and the fix pattern is established.
    priority/high is warranted because silent CI failures in frozen
    installs are a high-friction blocker for mixed-OS teams. No open
    milestone exists at this time, so milestone is null; the fix should
    ride the next patch release whenever that milestone opens.

    {
      "decision": "accept",
      "decision_detail": "",
      "theme": "theme/portability",
      "areas": ["area/lockfile"],
      "type": "type/bug",
      "status": "status/accepted",
      "priority": "priority/high",
      "preserved_labels": [],
      "milestone": null,
      "next_action": "Apply the LF-deterministic write fix to download_subdirectory_package(), mirroring PR #2223, and add a regression test that asserts a stable content_hash across platform line-ending settings.",
      "comment_markdown": "Thank you for this excellent, thorough bug report -- the reproduction steps, OS measurements, and root cause analysis are exactly what makes a fix straightforward to land.\n\nThis is confirmed as a regression gap from PR #2223: the CRLF fix applied to download_virtual_file_package() was never ported to the sibling download_subdirectory_package() path. Because the synthetic apm.yml is written by APM's Python code (not by git checkout), the platform's default text-mode line endings produce a different SHA-256 on Windows vs Linux, making the lockfile non-portable.\n\nThe fix is surgical: in download_subdirectory_package(), write the synthetic file with open(..., newline='\\n') or in binary mode -- mirroring the PR #2223 approach.\n\nThis is marked priority/high because it silently breaks apm install --frozen in Linux CI for teams developing on Windows. If you would like to send a PR, the relevant code is in src/apm_cli/utils/content_hash.py and the write site is in the downloader for subdirectory packages. Happy to review!"
    }

    Triage status: agentic proposal pending human ratification.
    Silence is approval. Maintainers can:

    • Override any label or milestone above by editing it directly --
      human edits are authoritative and will not be reverted on
      subsequent runs.
    • Re-trigger triage by applying the status/needs-triage label, or
      by removing status/triaged to enroll the issue in the next
      daily sweep.

    Posted by the Triage Panel workflow. See .apm/skills/apm-triage-panel for the panel skill.

    Generated by Triage Panel · 135.3 AIC · ⌖ 11.2 AIC · ⊞ 9.2K · ◷

  4. added
    area/lockfile
    priority/high
    status/accepted
    status/triaged
    type/bug
    on Aug 19, 2026
  5. added a commit that references this issue on Aug 19, 2026
    2e5a7f5
  6. added a commit that references this issue on Aug 23, 2026
    a803628
  7. moved this from In Progress to Done in APM Roadmapon Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/lockfilepriority/highstatus/acceptedstatus/triagedtype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions