Skip to content

refactor(#101): extract systemd watchdog group to sd_watchdog.py - #198

Merged
jphein merged 1 commit into
mainfrom
refactor/101-main-split-next
May 30, 2026
Merged

refactor(#101): extract systemd watchdog group to sd_watchdog.py#198
jphein merged 1 commit into
mainfrom
refactor/101-main-split-next

Conversation

@jphein

@jphein jphein commented May 30, 2026

Copy link
Copy Markdown
Collaborator

Incremental progress on the main.py decomposition (#101 / status doc #135). One slice, behavior-preserving — not a full split.

What moved

The three systemd-integration helpers from the top of main.py → a dedicated sd_watchdog.py:

Helper Role
_sd_notify NOTIFY_SOCKET datagram (READY=1, WATCHDOG=1, …)
_watchdog_interval WATCHDOG_USEC → seconds budget
_watchdog_loop health-gated keepalive coroutine (skips the probe + feeds the watchdog unconditionally during a mode=rebuild repair)

Logic is byte-identical — only the names _watchdog_loop reaches back for (_repair_state, _mp, _sd_notify, _log) became main.-qualified.

How tests stay green without edits

main.py re-exports all three under their original _-prefixed names. watchdog_loop resolves main's mutable module state (_repair_state, _mp) and the notifier (_sd_notify) at call time via a function-local import main, so tests/test_watchdog_rebuild.py — which does patch.object(main, "_sd_notify"), patch.object(main._mp, "_get_collection"), and mutates main._repair_state — keeps passing untouched. Same lazy-import pattern documented for fast_intercept (#133) and daemon_tools (#131).

Naming note

The module is sd_watchdog, not watchdog, on purpose: a top-level watchdog.py on the daemon's sys.path shadows the pip watchdog package that watcher.py imports for its filesystem observers (verified the shadow occurs, then renamed to avoid it).

Verification

  • main.py: 3161 → 3116 lines
  • python -c "import main" + import watcher (pip watchdog resolves to InotifyObserver, not the local module) — both clean
  • tests/test_watchdog_rebuild.py — 4 passed
  • Full suite — 582 passed, 1 skipped, 20 subtests passed
  • Daemon not restarted/deployed; import + test-suite check only.

🤖 Generated with Claude Code

Behavior-preserving extraction of the three systemd-integration helpers
from main.py into a dedicated sd_watchdog.py module (#101 / #135):

- `_sd_notify`        — NOTIFY_SOCKET datagram (READY=1, WATCHDOG=1, …)
- `_watchdog_interval`— WATCHDOG_USEC → seconds budget
- `_watchdog_loop`    — health-gated keepalive coroutine

main.py re-exports all three under their original `_`-prefixed names, so
the lifespan startup and existing callers are untouched. `watchdog_loop`
reaches back into main's namespace (`_repair_state`, `_mp`, `_sd_notify`,
`_log`) via a function-local `import main`, so tests/test_watchdog_rebuild.py
— which patches `main._sd_notify` / `main._mp._get_collection` and mutates
`main._repair_state` — keeps passing with no edits. Same lazy-import pattern
as fast_intercept (#133) and daemon_tools (#131).

Module is named `sd_watchdog` (not `watchdog`) on purpose: a top-level
`watchdog.py` on the daemon's sys.path would shadow the pip `watchdog`
package that watcher.py imports for its filesystem observers.

main.py: 3161 → 3116 lines. Full suite green (582 passed, 1 skipped).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 30, 2026 03:40
@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@jphein
jphein merged commit d9f9065 into main May 30, 2026
1 check failed
@jphein
jphein deleted the refactor/101-main-split-next branch May 30, 2026 03:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants