Skip to content

Refactor: separate package-format detection from classification and normalization strategy #782

Closed

Description

Follow-up from #781 (which fixed the immediate ordering bug behind #780).

Problem

detect_package_type() in src/apm_cli/models/validation.py is a positional-priority if/elif cascade that conflates three concerns:

  1. Evidence gathering -- did we see apm.yml, SKILL.md, hooks/*.json, plugin.json, agents/skills/commands/?
  2. Classification -- which PackageType enum value to assign.
  3. Normalization-strategy selection -- which downstream synthesizer to run (e.g. normalize_plugin_directory for MARKETPLACE_PLUGIN).

The cascade's first-match-wins semantics means every new bundled-format introduces a potential ordering bug. #780 was the latest one; the next one is just a matter of time.

Proposed shape

Move to a composition model:

  • FormatDetector interface, one impl per format: ApmYmlDetector, SkillMdDetector, HookJsonDetector, ClaudePluginDetector. Each returns Optional[FormatEvidence] independent of the others.
  • PackageFormatRegistry runs all detectors and produces a DetectionReport (set of evidences -- not a single PackageType).
  • NormalizationPlanner reads the report and decides which normalizers to run. Mixed packages become first-class (e.g. a Claude plugin that legitimately ships hooks runs both the plugin synthesizer AND the hooks pass-through, instead of being shoehorned into one classification).

The DetectionEvidence dataclass + gather_detection_evidence() helper landed in #781 are intentionally a step in this direction -- they decouple the evidence scan from classification so observability code can already use the richer payload.

Benefits

  • New formats are additive -- no ordering bugs possible.
  • Mixed packages are first-class (today's implicit "MARKETPLACE_PLUGIN happens to also handle hooks" becomes explicit).
  • The DetectionReport doubles as the observability payload (verbose detection traces, deploy-summary labels, near-miss warnings).
  • The classification logic stops being a positional puzzle.

Cost

Medium -- touches models/validation.py, install/sources.py, plus all consumers of PackageType (lockfile recording, install summary, drift detection). Should land behind a feature flag for one release if downstream test churn is high.

Out of scope for this issue

  • Tightening _has_hook_json to only count files matching the actual Claude hooks schema (today: any *.json in hooks/).
  • Introducing an explicit MIXED PackageType -- the DetectionReport model makes the enum redundant for mixed cases.

Related

Activity

  1. github-actions commented on Apr 26, 2026

    @github-actions

    Triage decision

    accept

    Proposed labels

    area/cli
    area/testing
    type/refactor
    status/accepted
    priority/low
    

    Milestone

    0.9.4

    Suggested next action

    Implement the three-concern separation in detect_package_type() -- evidence gathering, classification, and normalization-strategy selection -- with full unit test coverage for each concern boundary.

    Suggested issue comment

    Good follow-up framing. PR #781 fixed the immediate ordering bug, but the underlying if/elif cascade still conflates three concerns, making it fragile to extend.
    
    The proposed separation is sound: gather evidence first (which files/dirs are present), then classify to a `PackageType`, then select a normalization strategy. This makes each concern independently testable and makes adding a new package type explicit rather than positional.
    
    A few things to keep in mind during implementation:
    
    - The refactor touches `detect_package_type()` which is called at every package load. Include regression tests for the existing package types (apm-native, skill-only, plugin, etc.) to confirm the refactor does not change behavior.
    - Document the new precedence rules explicitly (as code comments or a docstring) so the next contributor does not have to reverse-engineer them.
    - If the Strategy pattern introduces an abstract base class or a registry, keep it in the same module for now -- premature cross-module extraction is out of scope for this ticket.
    
    Happy to review a PR when ready.

    Per-lens notes (collapsed)

    DevX UX Expert -- User-Need Reviewer

    This is an internal refactor with indirect user benefit: a more explicit and testable detection path reduces the risk of regressions like bug #780. The user task is "have APM correctly identify package types"; this refactor serves that by making the detection logic maintainable and auditable. Well-aligned with APM's goal of reliable package type support across diverse package structures.

    Supply Chain Security Expert -- Risk-Surface Reviewer

    No risk-surface implication. detect_package_type() is a classification utility; the refactor does not change the security enforcement boundary. Package type misclassification is a correctness bug, not a supply-chain attack surface in this context.

    OSS Growth Hacker -- Contributor-Tone Reviewer

    Not activated -- danielmeppiel is a core maintainer with extensive project ownership; warmth tuning for a new contributor is not needed.

    Python Architect -- Architecture Reviewer

    The three-concern separation is the right design. The key implementation risk is that the existing if/elif has implicit evaluation-order dependencies that may not be obvious until the refactor surfaces them. Recommend explicit documentation of precedence rules in the new design. A Strategy pattern (or a simple StrategySelector function) keeps the abstraction at the right level for the current codebase size. status/accepted is appropriate -- the design is sufficiently specified by the issue body.

    Doc Writer -- Documentation Reviewer

    Not activated -- this is a pure internal refactor with no user-visible API or behavior change; no new documentation pages are needed.

    APM CEO -- Triage Arbiter

    Straightforward internal refactor following a completed bug fix. Accept at priority/low for 0.9.4: the fix is not urgent but improves long-term maintainability and reduces regression risk as new package types are added. No theme label needed -- pure infra (area/cli + area/testing) with no product surface implication.

    {
      "decision": "accept",
      "decision_detail": "",
      "theme": null,
      "areas": ["area/cli", "area/testing"],
      "type": "type/refactor",
      "status": "status/accepted",
      "priority": "priority/low",
      "preserved_labels": [],
      "milestone": "0.9.4",
      "next_action": "Implement the three-concern separation in detect_package_type() with full unit test coverage for each concern boundary.",
      "comment_markdown": "Good follow-up framing. PR #781 fixed the immediate ordering bug, but the underlying if/elif cascade still conflates three concerns, making it fragile to extend.\n\nThe proposed separation is sound: gather evidence first (which files/dirs are present), then classify to a `PackageType`, then select a normalization strategy. This makes each concern independently testable and makes adding a new package type explicit rather than positional.\n\nA few things to keep in mind during implementation:\n\n- The refactor touches `detect_package_type()` which is called at every package load. Include regression tests for the existing package types (apm-native, skill-only, plugin, etc.) to confirm the refactor does not change behavior.\n- Document the new precedence rules explicitly (as code comments or a docstring) so the next contributor does not have to reverse-engineer them.\n- If the Strategy pattern introduces an abstract base class or a registry, keep it in the same module for now -- premature cross-module extraction is out of scope for this ticket.\n\nHappy to review a PR when ready."
    }

    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 · ● 2.3M · ◷

  2. added
    area/cli
    area/testing
    priority/low
    status/accepted
    status/triaged
    type/refactor
    on Apr 26, 2026
  3. added this to the 0.9.4 milestone on Apr 26, 2026
  4. added
    status/shepherding
    on Jun 2, 2026
  5. moved this from Todo to In Progress in APM Roadmapon Jun 2, 2026
  6. removed
    status/shepherding
    on Jun 2, 2026
  7. moved this from In Progress to Done in APM Roadmapon Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/cliarea/testingpriority/lowstatus/acceptedstatus/triagedtype/refactor

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions