Skip to content

Add persistent background webhook uploads - #5703

Open
Pixelobserver wants to merge 13 commits into
home-assistant:mainfrom
Pixelobserver:prep/split-5629-persistent-webhook
Open

Add persistent background webhook uploads#5703
Pixelobserver wants to merge 13 commits into
home-assistant:mainfrom
Pixelobserver:prep/split-5629-persistent-webhook

Conversation

@Pixelobserver

Copy link
Copy Markdown

AI Policy

Select exactly one option that describes AI usage in this contribution:

  • I have not used AI for this contribution.
  • AI assistance was used for this contribution.
  • AI fully generated the code for this contribution, but I've reviewed and understood it before submitting and will respond without AI during review.

Summary

This PR adds a persistent background upload path for webhook events.

The changes:

  • attach stable event identifiers and metadata to persisted background requests;
  • start persistent webhook uploads synchronously before the app can be suspended;
  • reconcile existing background upload tasks after an app relaunch;
  • select a stable webhook URL for persistent delivery;
  • prevent duplicate uploads when an existing task is adopted;
  • add focused tests for task creation, persistence, adoption, and reconciliation.

This is the transport foundation split from #5629 at the maintainer's request. It does not change iBeacon detection or zone transition behavior by itself.

Screenshots

N/A — no user-interface changes.

Link to pull request in Documentation repository

Documentation: N/A — this adds internal delivery infrastructure and no new user-facing configuration.

Any other notes

This is the first independent foundation PR split from #5629.

Testing:

  • git diff --check
  • bundle exec fastlane lint
  • bundle exec fastlane test
  • App-Debug simulator build

The remaining testing boxes will be checked after the corresponding Mac/Xcode commands complete successfully.

Copilot AI lite review requested due to automatic review settings September 9, 2026 06:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The reconciliation path cancels duplicate/background tasks without resolving any attached resolvers, which can leave callers waiting on promises/tasks that will never complete.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds an explicit “persisted background upload” path for webhook event delivery so background wakes can synchronously hand work off to URLSession, persist stable identifiers/metadata, and re-adopt/reconcile in-flight tasks after relaunch.

Changes:

  • Introduces startPersistedBackground + reconcilePersistedBackground in WebhookManager with request identifier tracking/adoption and stable background webhook URL selection.
  • Extends persisted task metadata (WebhookPersisted) to include a stable requestIdentifier, and adds a small public state enum for reconciliation results.
  • Adds targeted unit tests covering synchronous task creation, main-thread enforcement, URL selection, adoption, and HAAPI integration.
File summaries
File Description
Tests/Shared/Webhook/WebhookManager.test.swift Adds tests for synchronous persisted background start, main-thread failure, URL preference, and reconciliation/adoption behavior.
Tests/Shared/Webhook/FakeWebhookManager.swift Extends the fake to record calls and to support the new persisted-background API for tests.
Tests/Shared/HAAPIPersistentEvent.test.swift Adds coverage ensuring HomeAssistantAPI.startPersistentEvent uses the persisted background path (not send).
Sources/Shared/API/Webhook/Networking/WebhookManager.swift Implements persisted background start + reconciliation/adoption logic and tracks active/restored/completed persisted requests.
Sources/Shared/API/Webhook/Networking/URLSessionTask+WebhookPersisted.swift Persists a stable requestIdentifier onto background tasks for adoption/reconciliation.
Sources/Shared/API/Webhook/Networking/PersistedBackgroundRequestState.swift Adds a small public enum to represent reconciliation outcomes.
Sources/Shared/API/HAAPI.swift Exposes startPersistentEvent / reconcilePersistentEvent wrappers over the new webhook manager APIs.
Sources/HANetworking/Sources/Server.swift Adds a synchronous “preferred background webhook URL” accessor.
Sources/HANetworking/Sources/ConnectionInfo.swift Implements URL selection that prefers remote endpoints for time-critical background delivery.
Review details

Suppressed comments (1)

Sources/Shared/API/Webhook/Networking/WebhookManager.swift:532

  • When cancelling duplicate persisted tasks, any existing resolverForTask entries should be resolved/rejected before calling cancel(). Otherwise the cancellation is ignored in didCompleteWithError and leaves unresolved promises hanging.
                        for duplicate in matchingTasks where duplicate != task {
                            duplicate.cancel()
                        }
  • Files reviewed: 9/9 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread Sources/Shared/API/Webhook/Networking/WebhookManager.swift

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Background execution protection currently starts after the asynchronous response-handling chain is already running.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 9/9 changed files
  • Comments generated: 4
  • Review effort level: Balanced

Comment thread Sources/Shared/API/Webhook/Networking/WebhookManager.swift
Comment thread Sources/Shared/API/Webhook/Networking/WebhookManager.swift Outdated
Comment thread Sources/Shared/API/Webhook/Networking/WebhookManager.swift
Comment thread Tests/Shared/Webhook/FakeWebhookManager.swift
@codecov

codecov Bot commented Sep 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.30612% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 42.89%. Comparing base (a9db138) to head (438de48).

Files with missing lines Patch % Lines
...Shared/API/Webhook/Networking/WebhookManager.swift 89.94% 18 Missing ⚠️
Sources/Shared/API/HAAPI.swift 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5703      +/-   ##
==========================================
+ Coverage   42.72%   42.89%   +0.17%     
==========================================
  Files        1124     1124              
  Lines       77886    78346     +460     
==========================================
+ Hits        33273    33609     +336     
- Misses      44613    44737     +124     
Files with missing lines Coverage Δ
...k/Networking/URLSessionTask+WebhookPersisted.swift 100.00% <100.00%> (ø)
Sources/Shared/API/HAAPI.swift 41.56% <92.30%> (+0.43%) ⬆️
...Shared/API/Webhook/Networking/WebhookManager.swift 89.69% <89.94%> (+0.05%) ⬆️

... and 53 files with indirect coverage changes

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants