Zero-Coupling ServiceAdapter Specification & Architecture
Overview
The pkg/service package defines a pure, zero-coupling contract for managing OS-level service daemons (such as launchd, systemd, and local process supervisors) with zero internal dependencies on pkg/kernel or internal ZQK runtime facilities.
Architecture
flowchart TD
subgraph Client["Consumers"]
CLI["zqk system service / daemon CLI"]
Daemon["Background Supervisor Daemon"]
end
subgraph FacadeLayer["Service Management Layer"]
Mgr["service.Manager (Auto-detecting Facade)"]
end
subgraph Adapters["Host Platform Adapters"]
Darwin["LaunchdAdapter (macOS)"]
Linux["SystemdAdapter (Linux)"]
Fallback["SupervisorAdapter (Process Fallback)"]
Mock["MockAdapter (In-Memory Testing)"]
end
Client --> Mgr
Mgr --> Darwin
Mgr --> Linux
Mgr --> Fallback
Mgr --> Mock
Core Contracts & Invariants
-
Zero Internal Coupling: -
pkg/serviceimports only standard library packages and path utilities. - It contains zero imports ofpkg/kernel,pkg/storage, or internal runtime layers, enabling standalone library extraction. -
Pluggable Host Adapters: -
LaunchdAdapter: Generates valid XML plists and manages launchctl services on Darwin. -SystemdAdapter: Generates compliant systemd unit files on Linux. -SupervisorAdapter: Provides portable cross-platform process management when host supervisors are unavailable. -MockAdapter: In-memory test double supporting full lifecycle simulation and legacy cleanup. -
Autonomous Host Detection: -
service.NewManager(nil)detects the host operating system (runtime.GOOS) and instantiates the appropriate adapter automatically.
Host Platform Adapter Implementation Details
1. macOS Launchd Adapter
- Encapsulates
.plistgeneration under~/Library/LaunchAgentsor/Library/LaunchDaemons. - Configures key attributes:
Label,ProgramArguments,RunAtLoad, andKeepAlive. - Interacts with
launchctlusing deterministic process management.
2. Linux Systemd Adapter
- Generates systemd unit files under
~/.config/systemd/user/or/etc/systemd/system/. - Configures
[Unit],[Service], and[Install]sections withRestart=always|on-failure|no. - Interacts with
systemctlfor daemon-reload and unit lifecycle commands.
3. Local Process Supervisor Fallback
- Operates when native system init is unavailable or in containerized sandboxes.
- Tracks process states (running, stopped, failed) with PID files and signal handling.
ServiceManager Facade API
The service.Manager struct provides high-level unified lifecycle orchestration:
Install(ctx, spec): Validates spec invariants and delegates to the active platform adapter.Uninstall(ctx, id): Unregisters and removes service descriptor files.Start(ctx, id)/Stop(ctx, id)/Restart(ctx, id): Controls process lifecycle states.Status(ctx, id): Returns normalizedServiceStatus(PID, State, Uptime).CleanupLegacy(ctx, legacyIDs): Bulk sweeps orphaned and superseded service identifiers.