diff --git a/changelog.d/1394-watchdog-launch-recovery.md b/changelog.d/1394-watchdog-launch-recovery.md new file mode 100644 index 0000000000..4b0e6cea28 --- /dev/null +++ b/changelog.d/1394-watchdog-launch-recovery.md @@ -0,0 +1,4 @@ +### Interrupted watchdog launch recovery + +- Recover a candidate's launch receipt when the watchdog exits after starting it but before recording success. A running candidate that never produces a heartbeat then reaches the existing startup timeout; missing launch evidence does not count as a failed start. +- Preserve unproven launch intent across health observations before restart. Require comparable saved and current launch evidence so an old serving process cannot acquire a false failed-start timeout when baseline evidence is missing or partial. diff --git a/docs/developer/test.md b/docs/developer/test.md index 7ac340c224..1901d10e04 100644 --- a/docs/developer/test.md +++ b/docs/developer/test.md @@ -315,6 +315,25 @@ This is not real Apple service validation. Physical smoke is optional for normal publication; official-build registration and runtime Apple authorization remain distinct. See [release](../operations/provider-release.md). +## Watchdog Launch Evidence + +`WatchdogRecoveryIntegrationTests` exercises +`WatchdogRecoveryService.observeHealthyProvider`, the real updater and the +disk-backed recovery store with synthetic bundles, temporary install roots and +loopback release HTTP. Launch snapshots, process liveness and restart callbacks +are injected; no model or MLX kernel runs. + +```bash +swift test --skip-build --no-parallel \ + --filter 'unprovenLaunchEvidencePreservesIntent|prelaunchHealthObservationRetainsIntent|partialLaunchEvidenceRequiresComparison|interruptedLaunchReceiptTimesOut|unprovenLaunchIntentDoesNotTimeOut|interruptedLaunchReceiptPromotes' +``` + +It covers missing and partial baseline evidence, intent retention before +restart, later positive run-count or process-identity evidence, the startup +timeout, one-time failure accounting and heartbeat promotion. It does not run +native launchd, the controller cancellation interleaving or a signed release +installation. + ## Public Fixtures And Private Qualification Public protocol JSON and prompt golden vectors contain fixed inputs and expected diff --git a/docs/provider/installation.md b/docs/provider/installation.md index b5abdf2e07..168f401bf3 100644 --- a/docs/provider/installation.md +++ b/docs/provider/installation.md @@ -1,6 +1,6 @@ # Install, update, and uninstall the provider -> Last updated: 2026-10-09 +> Last updated: 2026-10-20 How to put the `darkbloom` CLI on an Apple Silicon Mac with `scripts/install.sh`, what the script verifies before it touches an existing install, how the binary @@ -211,6 +211,20 @@ that exact version is blocked; a newer release installs normally. `darkbloom update --override-quarantine` reinstalls it anyway, and `darkbloom doctor` reports the quarantine under `up to date`. +If the watchdog exits after starting a candidate but before recording the +launch receipt, its next health observations reconcile the saved intent with +launchd's run count or process identity for the same job. For each comparison, +both saved and current values must be known; either a higher run count or a +changed process identity proves a launch. A proven launch receives the existing +startup timeout from reconciliation; +repeated observations do not extend it. Missing or unchanged evidence preserves +the intent for a later observation without arming a failed-start timeout. This +includes a health observation before restart and a previously unreadable process +identity becoming available without other evidence of a launch. This also covers +a candidate that remains running without a heartbeat +(`provider-swift/Sources/ProviderCore/Service/WatchdogRecoveryService.swift`, +`observeHealthyProvider`). + The unprivileged updater never touches the root fan helper; after an update run `sudo darkbloom fan enable` again if you use [fan control](./fan-control.md). diff --git a/provider-swift/Sources/ProviderCore/Service/WatchdogRecoveryService.swift b/provider-swift/Sources/ProviderCore/Service/WatchdogRecoveryService.swift index 29a016784f..53e4b1bfaa 100644 --- a/provider-swift/Sources/ProviderCore/Service/WatchdogRecoveryService.swift +++ b/provider-swift/Sources/ProviderCore/Service/WatchdogRecoveryService.swift @@ -618,6 +618,29 @@ public struct WatchdogRecoveryService: Sendable { do { var state = try session.readState() + let before = state + // The previous watchdog may have exited after kickstart but + // before persisting its launch receipt. Require comparable + // evidence of a new launch; a health tick can precede kickstart. + // Keep unproven intent available for a later observation. + if let baseline = state.candidate?.launchIntent?.baseline, + let snapshot = deps.launchSnapshot(), + snapshot.label == baseline.label + { + let runsAdvanced = if let previous = baseline.runs, let current = snapshot.runs { + current > previous + } else { + false + } + let processChanged = if let previous = baseline.process, let current = snapshot.process { + current != previous + } else { + false + } + if runsAdvanced || processChanged { + _ = state.reconcileLaunchIntent(snapshot: snapshot, now: now) + } + } guard let candidate = state.candidate else { return .noCandidate } let freshMatchingHeartbeat: Bool if let daemonState { @@ -629,7 +652,6 @@ public struct WatchdogRecoveryService: Sendable { freshMatchingHeartbeat = false } - let before = state let promoted = state.observeCandidateHealth( healthySignal: freshMatchingHeartbeat, processStartedAt: daemonState?.startedAt, diff --git a/provider-swift/Tests/ProviderCoreTests/Service/WatchdogRecoveryIntegrationTests.swift b/provider-swift/Tests/ProviderCoreTests/Service/WatchdogRecoveryIntegrationTests.swift index a10efd8be1..21d9faca83 100644 --- a/provider-swift/Tests/ProviderCoreTests/Service/WatchdogRecoveryIntegrationTests.swift +++ b/provider-swift/Tests/ProviderCoreTests/Service/WatchdogRecoveryIntegrationTests.swift @@ -548,6 +548,292 @@ struct WatchdogRecoveryIntegrationTests { #expect(try context.fixture.liveBinaryContents() == "2.0.0-darkbloom") } + @Test("unproven launch evidence cannot arm an attempt for a serving predecessor", + arguments: ["no-baseline-both", "no-baseline-runs", "no-baseline-process", "identity-became-readable"]) + func unprovenLaunchEvidencePreservesIntent(scenario: String) async throws { + let context = try await installedCandidate() + defer { context.fixture.cleanup() } + defer { Task { await context.mock.shutdown() } } + let predecessor = ProcessIdentity(pid: 4242, startTimeMicros: 50_000_000) + let baseline: ProviderLaunchSnapshot? = scenario == "identity-became-readable" + ? ProviderLaunchSnapshot(label: "io.darkbloom.provider", runs: 10, process: nil) + : nil + let unchanged = ProviderLaunchSnapshot( + label: "io.darkbloom.provider", + runs: scenario == "no-baseline-process" ? nil : 10, + process: scenario == "no-baseline-runs" ? nil : predecessor) + let store = recoveryStore(context.fixture) + var state = try store.loadState() + state.cancelPendingAttempt() + try store.writeState(state) + // Use the real updater's lease and durable writer. Cancellation of a + // subsequent restart is represented by making no launch callback. + try context.updater.prepareCandidateLaunch( + operation: "test-unproven-launch-evidence", baseline: baseline) + let intentID = try #require(try store.loadState().candidate?.launchIntent?.id) + let restartsBefore = context.restarts.value + let watchdog = makeService( + updater: context.updater, restarts: context.restarts, + launchSnapshot: { unchanged }) + var lastHealth: WatchdogRecoveryService.HealthOutcome = .noCandidate + for now in [200.0, 501.0, 1_000.0] { + lastHealth = watchdog.observeHealthyProvider( + providerRunning: true, + daemonState: DaemonState( + pid: predecessor.pid, version: "1.0.0", writtenAt: now, startedAt: 50), + now: now) + state = try store.loadState() + #expect(lastHealth == .stabilizing(since: nil)) + } + #expect(state.candidate?.launchIntent?.id == intentID) + #expect(state.candidate?.pendingAttemptID == nil) + #expect(state.candidate?.attemptStartedAt == nil) + #expect(state.candidate?.failureCount == 0) + state = try store.loadState() + #expect(state.candidate?.failureCount == 0) + #expect(context.restarts.value == restartsBefore) + #expect(state.quarantine == nil) + #expect(try context.fixture.persistentStateIsIntact()) + } + + @Test("prelaunch health observation retains evidence for a later silent launch", + arguments: [false, true]) + func prelaunchHealthObservationRetainsIntent(processChanged: Bool) async throws { + let context = try await installedCandidate() + defer { context.fixture.cleanup() } + defer { Task { await context.mock.shutdown() } } + let baseline = ProviderLaunchSnapshot( + label: "io.darkbloom.provider", runs: 10, + process: ProcessIdentity(pid: 4242, startTimeMicros: 50_000_000)) + let launched = ProviderLaunchSnapshot( + label: baseline.label, runs: processChanged ? 10 : 11, + process: processChanged + ? ProcessIdentity(pid: 4242, startTimeMicros: 175_000_000) : baseline.process) + let store = recoveryStore(context.fixture) + var state = try store.loadState() + state.cancelPendingAttempt() + try store.writeState(state) + try context.updater.prepareCandidateLaunch( + operation: "test-prelaunch-health-observation", baseline: baseline) + let intentID = try #require(try store.loadState().candidate?.launchIntent?.id) + let prelaunchWatchdog = makeService( + updater: context.updater, restarts: context.restarts, + launchSnapshot: { baseline }) + #expect(prelaunchWatchdog.observeHealthyProvider( + providerRunning: true, + daemonState: DaemonState(pid: 4242, version: "1.0.0", writtenAt: 150, startedAt: 50), + now: 150) == .stabilizing(since: nil)) + state = try store.loadState() + #expect(state.candidate?.launchIntent?.id == intentID) + #expect(state.candidate?.pendingAttemptID == nil) + // Recreate the service, forcing recovery to rely on durable evidence. + let watchdog = makeService( + updater: context.updater, restarts: context.restarts, + launchSnapshot: { launched }) + #expect(watchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 200) == .stabilizing(since: nil)) + state = try store.loadState() + #expect(state.candidate?.pendingAttemptID == intentID) + #expect(state.candidate?.attemptStartedAt == 200) + #expect(watchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 499) == .stabilizing(since: nil)) + var lastHealth: WatchdogRecoveryService.HealthOutcome = .noCandidate + for now in [501.0, 1_000.0] { + lastHealth = watchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: now) + #expect(lastHealth == .inactiveCandidate(attemptStartedAt: 200)) + } + #expect(try store.loadState().candidate?.failureCount == 0) + let restartsBefore = context.restarts.value + if case .inactiveCandidate = lastHealth { + let result = await watchdog.recoverDownProvider( + autoUpdateEnabled: false, providerProcessAlive: true, now: 1_000) + #expect(result == .restartIssued(updatedTo: nil, rolledBackTo: nil)) + } + state = try store.loadState() + #expect(state.candidate?.failureCount == 1) + #expect(context.restarts.value == restartsBefore + 1) + #expect(watchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 1_001) == .stabilizing(since: nil)) + #expect(try store.loadState().candidate?.failureCount == 1) + #expect(try context.fixture.persistentStateIsIntact()) + } + + @Test("healthy launch reconciliation requires comparable evidence from the same job", + arguments: ["runs-only", "identity-only", "decreasing-runs", "missing-current-runs", "missing-saved-runs", "different-job"]) + func partialLaunchEvidenceRequiresComparison(scenario: String) async throws { + let context = try await installedCandidate() + defer { context.fixture.cleanup() } + defer { Task { await context.mock.shutdown() } } + let predecessor = ProcessIdentity(pid: 4242, startTimeMicros: 50_000_000) + let baseline = ProviderLaunchSnapshot( + label: "io.darkbloom.provider", + runs: ["identity-only", "missing-saved-runs"].contains(scenario) ? nil : 10, + process: scenario == "runs-only" ? nil : predecessor) + let current = ProviderLaunchSnapshot( + label: scenario == "different-job" ? "io.darkbloom.other" : baseline.label, + runs: ["identity-only", "missing-current-runs"].contains(scenario) + ? nil : (scenario == "decreasing-runs" ? 9 : 11), + process: scenario == "runs-only" ? nil + : (scenario == "identity-only" + ? ProcessIdentity(pid: 4242, startTimeMicros: 175_000_000) : predecessor)) + let launchProven = ["runs-only", "identity-only"].contains(scenario) + let store = recoveryStore(context.fixture) + var state = try store.loadState() + state.cancelPendingAttempt() + try store.writeState(state) + try context.updater.prepareCandidateLaunch( + operation: "test-partial-launch-evidence", baseline: baseline) + let intentID = try #require(try store.loadState().candidate?.launchIntent?.id) + let watchdog = makeService( + updater: context.updater, restarts: context.restarts, + launchSnapshot: { current }) + #expect(watchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 200) == .stabilizing(since: nil)) + state = try store.loadState() + #expect(state.candidate?.launchIntent?.id == (launchProven ? nil : intentID)) + #expect(state.candidate?.pendingAttemptID == (launchProven ? intentID : nil)) + #expect(state.candidate?.attemptStartedAt == (launchProven ? 200 : nil)) + #expect(watchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 501) + == (launchProven ? .inactiveCandidate(attemptStartedAt: 200) : .stabilizing(since: nil))) + #expect(try store.loadState().candidate?.failureCount == 0) + #expect(context.restarts.value == 1) + #expect(try context.fixture.persistentStateIsIntact()) + } + + @Test("interrupted launch receipt is recovered before a silent candidate times out", arguments: [false, true]) + func interruptedLaunchReceiptTimesOut(processChanged: Bool) async throws { + let context = try await installedCandidate() + defer { context.fixture.cleanup() } + defer { Task { await context.mock.shutdown() } } + + let baseline = ProviderLaunchSnapshot( + label: "io.darkbloom.provider", runs: 10, + process: ProcessIdentity(pid: 4242, startTimeMicros: 1)) + let launched = ProviderLaunchSnapshot( + label: baseline.label, runs: processChanged ? 10 : 11, + process: processChanged + ? ProcessIdentity(pid: 4242, startTimeMicros: 2) : baseline.process) + let store = recoveryStore(context.fixture) + var state = try store.loadState() + state.cancelPendingAttempt() + let prepared = state.prepareLaunchIntent(now: 100, baseline: baseline) + #expect(prepared) + let intentID = try #require(state.candidate?.launchIntent?.id) + try store.writeState(state) + + // A transient launchctl snapshot failure cannot erase the durable + // evidence needed by a later watchdog tick. + #expect(context.service.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 150 + ) == .stabilizing(since: nil)) + #expect(try store.loadState().candidate?.launchIntent?.id == intentID) + + // Recreate the service from the durable pre-kickstart intent: the + // old watchdog died after launchd started the process, before it + // could persist markLaunchIssued. No heartbeat has arrived yet. + let restartedWatchdog = makeService( + updater: context.updater, restarts: context.restarts, + launchSnapshot: { launched }) + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 200 + ) == .stabilizing(since: nil)) + state = try store.loadState() + #expect(state.candidate?.pendingAttemptID == intentID) + #expect(state.candidate?.launchIntent == nil) + #expect(state.candidate?.attemptStartedAt == 200) + #expect(state.candidate?.failureCount == 0) + + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 499 + ) == .stabilizing(since: nil)) + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 501 + ) == .inactiveCandidate(attemptStartedAt: 200)) + #expect(try store.loadState().candidate?.attemptStartedAt == 200) + + let restartsBefore = context.restarts.value + let recovery = await restartedWatchdog.recoverDownProvider( + autoUpdateEnabled: false, providerProcessAlive: true, now: 501) + #expect(recovery == .restartIssued(updatedTo: nil, rolledBackTo: nil)) + state = try store.loadState() + #expect(state.candidate?.failureCount == 1) + #expect(state.candidate?.pendingAttemptID != intentID) + #expect(state.candidate?.pendingAttemptID != nil) + #expect(context.restarts.value == restartsBefore + 1) + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 502 + ) == .stabilizing(since: nil)) + #expect(try store.loadState().candidate?.failureCount == 1) + #expect(try context.fixture.persistentStateIsIntact()) + } + + @Test("an unproven launch intent does not time out a serving process", arguments: [false, true]) + func unprovenLaunchIntentDoesNotTimeOut(snapshotUnavailable: Bool) async throws { + let context = try await installedCandidate() + defer { context.fixture.cleanup() } + defer { Task { await context.mock.shutdown() } } + let baseline = ProviderLaunchSnapshot( + label: "io.darkbloom.provider", runs: 10, process: nil) + let store = recoveryStore(context.fixture) + var state = try store.loadState() + state.cancelPendingAttempt() + let prepared = state.prepareLaunchIntent(now: 100, baseline: baseline) + #expect(prepared) + try store.writeState(state) + let restartedWatchdog = makeService( + updater: context.updater, restarts: context.restarts, + launchSnapshot: { snapshotUnavailable ? nil : baseline }) + + for now in [200.0, 501.0, 1_000.0] { + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: now + ) == .stabilizing(since: nil)) + } + state = try store.loadState() + #expect(state.candidate?.pendingAttemptID == nil) + #expect(state.candidate?.attemptStartedAt == nil) + #expect(state.candidate?.failureCount == 0) + #expect(state.quarantine == nil) + #expect(context.restarts.value == 1) + } + + @Test("a reconciled launch still promotes on continuous matching heartbeats") + func interruptedLaunchReceiptPromotes() async throws { + let context = try await installedCandidate() + defer { context.fixture.cleanup() } + defer { Task { await context.mock.shutdown() } } + let baseline = ProviderLaunchSnapshot( + label: "io.darkbloom.provider", runs: 10, process: nil) + let launched = ProviderLaunchSnapshot( + label: baseline.label, runs: 11, process: nil) + let store = recoveryStore(context.fixture) + var state = try store.loadState() + state.cancelPendingAttempt() + let prepared = state.prepareLaunchIntent(now: 100, baseline: baseline) + #expect(prepared) + try store.writeState(state) + let restartedWatchdog = makeService( + updater: context.updater, restarts: context.restarts, + stabilizationSeconds: 60, launchSnapshot: { launched }) + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, daemonState: nil, now: 200 + ) == .stabilizing(since: nil)) + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, + daemonState: DaemonState(pid: 4242, version: "2.0.0", writtenAt: 210, startedAt: 150), + now: 210 + ) == .stabilizing(since: 210)) + #expect(restartedWatchdog.observeHealthyProvider( + providerRunning: true, + daemonState: DaemonState(pid: 4242, version: "2.0.0", writtenAt: 271, startedAt: 150), + now: 271 + ) == .promoted(version: "2.0.0")) + #expect(try store.loadState().candidate == nil) + #expect(context.restarts.value == 1) + } + @Test("pending fresh candidate without a blocked rollback is not bridged from the healthy path") func freshPendingCandidateIsNotBridged() async throws { let context = try await installedCandidate() @@ -1316,7 +1602,8 @@ struct WatchdogRecoveryIntegrationTests { restarts: RecoveryRestartCounter, stabilizationSeconds: Double = 180, candidateStartupTimeoutSeconds: Double = 300, - isPastTickDeadline: @escaping @Sendable () -> Bool = { false } + isPastTickDeadline: @escaping @Sendable () -> Bool = { false }, + launchSnapshot: @escaping @Sendable () -> ProviderLaunchSnapshot? = { nil } ) -> WatchdogRecoveryService { WatchdogRecoveryService( updater: updater, @@ -1327,7 +1614,7 @@ struct WatchdogRecoveryIntegrationTests { }, // Injected: tests must never shell out to the real // `launchctl print` for the host's provider job. - launchSnapshot: { nil }, + launchSnapshot: launchSnapshot, processAlive: { _ in true }, isPastTickDeadline: isPastTickDeadline, log: { _ in }