Skip to content

Phase 5c-2a: receiver-side submit_job accept path - #528

Merged
J. Nick Koston (bdraco) merged 10 commits into
mainfrom
phase-5c-2-receiver-accept
May 10, 2026
Merged

J. Nick Koston (bdraco) merged 10 commits into
mainfrom
phase-5c-2-receiver-accept

Conversation

@bdraco

Copy link
Copy Markdown
Member

What does this implement/fix?

Phase 5c-2a of #106 — receiver-side bundle reception + queue + ack, the half of 5c-2 that drives a submitted bundle from the wire into a queued :class:FirmwareJob. Splits the table row's 5c-2 into:

  • 5c-2a (this PR): bundle reception + ack contract + queue. The "did the job get accepted?" half.
  • 5c-2b (next): firmware-event fan-out — subscribe to JOB_* events filtered to remote_peer == session.dashboard_id and push job_state_changed / job_output frames over the submitting session.

Splitting was a sizing call — even just the receive path is ~560 lines of production + ~17 unit tests; landing it cleanly before the fan-out keeps each PR focused.

What this PR adds:

  • controllers/remote_build_submit_job.py — SubmitJobReceiver class plus _PendingSubmit per-session dataclass. Owns the receive-loop dispatch for AppMessageType.SUBMIT_JOB / SUBMIT_JOB_CHUNK, drives BundleAssembler from 5c-1, writes the assembled tarball + extracts via esphome.bundle.prepare_bundle_for_compile() (preserves .esphome/ / .pioenvs/ for incremental compiles — load-bearing for the per-peer per-device subtree), and queues a FirmwareJob with remote_peer=session.dashboard_id.

  • Per-peer per-device subtree at <config>/.esphome/.remote_builds/<dashboard_id>/<device_name>/. Two-segment key dedupes correctly across multi-offloader fleets (two HA Greens both shipping a "kitchen" device land in distinct subtrees) without colliding within one offloader's pool. PlatformIO's .pio/build cache then sees stable source paths and skips the cold-rebuild hit.

  • FirmwareJob.remote_peer — new optional string field (default "") carrying the offloader's dashboard_id when the job came in via peer-link. Empty for locally-submitted jobs. Threads through FirmwareController._create_job(... remote_peer=...). Surfaced as a "from <peer>" badge in the Firmware tasks UI later.

  • Receive-loop wiring in controllers/remote_build_peer_link._receive_loop — two new branches dispatch the new frame types to controller.get_submit_job_receiver(). controller is now threaded into the loop; existing tests pass MagicMock() for the controller arg since their inputs don't include the new types.

  • Lifecycle integration — RemoteBuildController.start() constructs the SubmitJobReceiver (gated on self._db.firmware is not None); unregister_peer_link_session() calls discard_session(dashboard_id) so a bundle reception that was mid-stream when the session ended doesn't outlive its session.

Reject reason taxonomy. Eight distinct submit_job_ack.reason codes — six from the 5c-1 BundleAssemblerErrorCode (mapped through 1:1) plus two receiver-side codes for paths after assembly (extract_failed / queue_rejected) and four for header / chunk dispatch (duplicate_submit / invalid_header / no_inflight_submit / job_id_mismatch / chunk_decode_failed). Wire-level misbehaviour codes (out-of-order chunks, decode failures, post-completion frames) trigger terminate{malformed_frame} after the ack; recoverable errors (oversized / undersized / hash mismatch / extract / queue) ack-and-stay so the offloader can retry on a fresh submit.

17 unit tests in tests/test_remote_build_submit_job.py covering header validation (duplicate / invalid target / oversized), chunk dispatch (no-inflight, job_id mismatch, decode failure, out-of-order, hash mismatch), the happy path through a stubbed prepare_bundle_for_compile, extraction failure, and the session-discard contract. End-to-end against the real FirmwareController + a real bundle goes in 5c-2b once the firmware-event fan-out lands.

Related issue or feature (if applicable):

Types of changes

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — docs
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Frontend coordination

  • No frontend change needed
  • Companion frontend PR: esphome/device-builder-dashboard-frontend#

(FirmwareJob.remote_peer is a new field but it defaults to "", so existing frontend renderers ignoring unknown fields keep working unchanged. The "from <peer>" badge UI lands in a later phase alongside the offloader Settings UI.)

Checklist

  • The code change is tested and works locally.
  • Pre-commit hooks pass (ruff, codespell, yaml/json/python checks).
  • Tests have been added or updated under tests/ where applicable.
  • components.json has not been hand-edited (regenerate via script/sync_components.py if a sync is needed).
  • Architecture-level changes are reflected in docs/ARCHITECTURE.md and/or docs/API.md.

(Architecture mention deferred to 5c-2b — the receive-then-fan-out shape is what docs/ARCHITECTURE.md's "Long-lived peer-link sessions" section will want to describe end-to-end, not the half landed here.)

Copilot AI review requested due to automatic review settings May 10, 2026 05:42
@github-actions github-actions Bot added the new-feature New feature label May 10, 2026
@codecov-commenter

Codecov Comments Bot (codecov-commenter) commented May 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.19%. Comparing base (8edae2c) to head (1bafe4c).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #528      +/-   ##
==========================================
+ Coverage   99.18%   99.19%   +0.01%     
==========================================
  Files          69       70       +1     
  Lines        8806     8997     +191     
==========================================
+ Hits         8734     8925     +191     
  Misses         72       72              
Flag Coverage Δ
py3.12 99.16% <100.00%> (+0.01%) ⬆️
py3.14 99.19% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
..._device_builder/controllers/firmware/controller.py 100.00% <ø> (ø)
esphome_device_builder/controllers/remote_build.py 100.00% <100.00%> (ø)
...vice_builder/controllers/remote_build_peer_link.py 100.00% <100.00%> (ø)
...ice_builder/controllers/remote_build_submit_job.py 100.00% <100.00%> (ø)
esphome_device_builder/models/firmware.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented May 10, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 12 untouched benchmarks


Comparing phase-5c-2-receiver-accept (1bafe4c) with main (8edae2c)

Open in CodSpeed

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.

Pull request overview

Implements issue #106 phase 5c-2a on the receiver side: accepting submit_job / submit_job_chunk frames over peer-link, assembling + extracting the submitted bundle into a stable per-peer/per-device subtree, enqueueing a FirmwareJob tagged with the offloader identity, and replying with a structured submit_job_ack.

Changes:

  • Added SubmitJobReceiver to manage per-session in-flight bundle assembly, extraction via prepare_bundle_for_compile(), and firmware job queueing + submit_job_ack.
  • Wired peer-link receive-loop dispatch for submit_job + submit_job_chunk and integrated receiver lifecycle cleanup on session unregister.
  • Extended FirmwareJob/FirmwareController._create_job() with remote_peer to tag remotely-submitted jobs; added unit tests for receiver-side submit/ack behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
esphome_device_builder/controllers/remote_build_submit_job.py New receiver-side submit/ack state machine: assemble → write → extract → queue → ack.
esphome_device_builder/controllers/remote_build_peer_link.py Dispatch submit_job and submit_job_chunk frames from the peer-link receive loop.
esphome_device_builder/controllers/remote_build.py Instantiate SubmitJobReceiver on start; discard in-flight state when a session unregisters; expose accessor for receive-loop dispatch.
esphome_device_builder/controllers/firmware/controller.py Thread remote_peer into job creation for remote submissions.
esphome_device_builder/models/firmware.py Add FirmwareJob.remote_peer field to carry the offloader’s dashboard_id.
tests/test_remote_build_submit_job.py New unit tests covering header validation, chunk handling, rejection reasons, happy path, and session discard behavior.
tests/test_remote_build_peer_link.py Update unit tests for new _receive_loop(session, controller) signature.

Comment thread tests/test_remote_build_submit_job.py Resolved
@bdraco
J. Nick Koston (bdraco) marked this pull request as draft May 10, 2026 05:52

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.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Comment thread tests/test_remote_build_peer_link.py Resolved

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

@bdraco
J. Nick Koston (bdraco) marked this pull request as ready for review May 10, 2026 14:28
@bdraco
J. Nick Koston (bdraco) merged commit 74163de into main May 10, 2026
13 checks passed
@bdraco
J. Nick Koston (bdraco) deleted the phase-5c-2-receiver-accept branch May 10, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new-feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants