Skip to content

Refactor: wake_lifecycle hot-spot files exceed 600-line maintainability threshold #807

@michael-wojcik

Description

@michael-wojcik

Context

Surfaced during CODE phase of PR for #781/#786/#760/#738 bundle (capture-shape HARD GATE), session pact-7642b0c9, 2026-05-19. Out of scope for that bundle — these files were already oversized before the bundle work began; that bundle's edits are minimal string-pin syncs, not the cause.

Affected files (current line counts at issue-file time)

File Lines Threshold
pact-plugin/hooks/wake_lifecycle_emitter.py 1041 600
pact-plugin/tests/test_wake_inbox_drain.py 1783 600
pact-plugin/tests/test_inbox_wake_lifecycle_emitter.py 1230 600
pact-plugin/tests/test_wake_lifecycle_emitter.py 869 600
pact-plugin/tests/test_wake_lifecycle_arm_edge_cases.py 683 600

The production hook (wake_lifecycle_emitter.py) is the most actionable — it consolidates Arm/Teardown directive emission, transition detection, lead-session guarding, session_init delegation, and journal-event paired-write logic in one file. Per the maintainability-warning hook's own recommendation:

  • Single Responsibility: does wake_lifecycle_emitter.py do one thing? Currently combines directive emission + transition detection + lead-session guard + session_init delegation + journal-event coordination.
  • Modular design: candidates for extraction:
    • shared/wake_directives.py_ARM_DIRECTIVE / _TEARDOWN_DIRECTIVE constants + anti-softening guards
    • shared/wake_transitions.py_decide_directive / _arm_or_none / count_active_tasks logic
    • shared/wake_journal.pyscan_armed / scan_disarmed paired-write coordination
  • Test files: split by concern (per-helper test modules, fixture-driven shape pinning module, etc.).

Scope discipline

Suggested approach

  1. /PACT:plan-mode consultation with pact-architect (the maintainability-warning hook's explicit recommendation) once TaskCompleted actor-discriminator empirical capture + teardown_request_emitter.py migration (follow-up to #611/#778) #781 bundle merges.
  2. SEC-AC-2 disk-verify discipline applies: re-grep usage of every extracted symbol against pact-plugin/ to enumerate import sites before module reshuffle.
  3. Counter-test-by-revert cardinality must be re-measured post-refactor — the test corpus codifying legacy contracts (TestStdinShapePin, TestGate0LeadSessionGuard, named-invariant tests landed by TaskCompleted actor-discriminator empirical capture + teardown_request_emitter.py migration (follow-up to #611/#778) #781) will need their import paths updated atomically with the source-module relocation.

Falsifier

Refactor lands; production hooks still behave byte-identically (same scan_armed / scan_disarmed events written, same _ARM_DIRECTIVE / _TEARDOWN_DIRECTIVE strings emitted, same lead-session guard semantics); test cardinality unchanged.

Provenance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions