Skip to content

fix: bind upload ownership and shared staging capacity - #5281

Closed
thymikee wants to merge 5 commits into
fix/lease-artifact-owner-admissionfrom
fix/daybreak-upload-review
Closed

thymikee wants to merge 5 commits into
fix/lease-artifact-owner-admissionfrom
fix/daybreak-upload-review

Conversation

@thymikee

@thymikee thymikee commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Closed as superseded by #5344 on the 0.22 remediation line. Continue review and implementation there. Historical validation below remains attributed to its original commits; closure does not claim the replacements are merge-ready.

Summary

Use one staging budget for legacy uploads, resumable uploads, and retained artifacts. Keep each reservation charged until its staging directory is removed. The first ticket close owns removal and the daemon logs the first deferred cleanup. Stacked on #5278.

Before / after

  • Failed removal left capacity charged after the upload entry expired. The shared cleanup path retries until removal succeeds or the directory is absent.
  • Legacy file uploads stream with chunked framing, and the daemon charges bytes as they arrive. Prepared bundle archives retain fixed-length framing.
  • Preflight, finalize, and legacy capacity refusals preserve UPLOAD_CAPACITY_EXCEEDED and retriable classification.

Validation

At 0cd48fbd30, all five upload patches match e3ba3f57bc by range-diff after rebasing onto #5278 fe6452bc3f. Hosted checks are running. At e3ba3f57bc, all 19 hosted checks passed.

At ad64964df7, 19 hosted checks completed without failures. All 23 tests in packages/daemon/src/server/__tests__/resumable-upload.test.ts passed. The mocked rollback failure exercises an initial removal and a scheduled retry. At 024f263a10, 51 wire and mutation tests passed after the upload header types and control-request mutation were updated. The emitted headers and protocol 4 are unchanged by that update.

At c125cd3375, an Android emulator installed an 87 MB APK through a remote proxy lease. This receipt predates the current stack.

20313d1e5a passed merge-tree. CI pending.

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.79 MB 6.79 MB +1.8 kB
Package (unpacked) 6.79 MB 6.79 MB +1.8 kB
Package (download) 2.07 MB 2.07 MB +679 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 23.3 ms 22.5 ms -0.8 ms
CLI --help 56.6 ms 46.0 ms -10.6 ms

Affected plugins

Plugin Installed (including dependencies) Unpacked Download
@agent-device/revyl 181.2 kB → 181.2 kB (0 B) 180.8 kB → 180.8 kB (0 B) 58.6 kB → 58.6 kB (-3 B)
@agent-device/testmu 26.5 kB → 26.5 kB (0 B) 26.2 kB → 26.2 kB (0 B) 9.6 kB → 9.6 kB (+1 B)
@agent-device/tizen 99.5 kB → 99.5 kB (0 B) 99.1 kB → 99.1 kB (0 B) 34.9 kB → 34.9 kB (0 B)
@agent-device/webos 98.5 kB → 98.5 kB (0 B) 98.1 kB → 98.1 kB (0 B) 34.6 kB → 34.6 kB (0 B)

@blacksmith-sh

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 3 new findings on 905de55. The verdict and the full list are in the summary comment.

Comment thread packages/remote/src/upload-client.ts Resolved
Comment thread packages/remote/src/upload-client.ts Resolved
@thymikee

Copy link
Copy Markdown
Member Author

At 905de55, a large but valid app bundle that installs on the base branch can no longer be uploaded, and there are further problems in upload cleanup, admission and error handling.

During extraction, a reservation holds the payload bytes plus the expanded bytes (resumable path, legacy path). The per-tenant budget is 4 GiB, but the per-artifact limits allow 2 GiB compressed and 4 GiB expanded. For example, a 1.2 GB tgz that expands to 3.5 GB finalizes on the base branch. Here it fails at finalize, even on an empty tenant, with a retriable 503 that can never succeed. The legacy route can also re-upload up to 2 GiB before it fails again. Any single upload that the per-artifact limits accept must fit in an empty tenant budget. Please derive the per-tenant budget from the compressed and expanded limits in host-kit, keep the daemon-wide budget at least as large, and add a test that charges both maximums on an empty tenant.

A second independent pass found that resumable cleanup returns capacity while files are still staged. Ticket expiry, terminal finalize failure and rollback invalidation release the reservation before the writer stops or the directory is removed. New uploads can spend those bytes too early, and a failed removal leaves files uncharged. A ticket can be invalidated at once, but every cleanup path must keep the reservation until the writer has finished and removal has succeeded, and must retry a failed removal.

A second independent pass found that a failed directory removal loses ownership of the reservation. Retained-upload expiry and install cleanup delete the only entry that holds the release callback, so the capacity stays charged with no retry. Resumable initialization and refused publication have the same problem, while a failed legacy body or publication frees the charge even if files remain. For every provisional, unpublished or retained upload, one record must own both the directory and the reservation until removal succeeds.

A second independent pass found that a materialization cleanup error skips uploaded-source cleanup. If removal of the IPA extraction directory rejects, the original upload is never cleaned up. Its expiry timer is already cleared, so its files and reservation remain until shutdown. Uploaded-source cleanup must run even when materialized cleanup rejects, for example from an inner finally.

A second independent pass found that legacy admission refuses known-length uploads that fit. It demands 2 GiB before it reads the request. A 1 MiB upload gets a 503 when less than 2 GiB is left in the tenant or daemon budget, although the base branch accepts it. A body with a valid Content-Length should reserve that size and be capped at it before any write. A body without a length should be charged ahead of writes or keep a conservative reservation.

A second independent pass found that the client replaces a tenant header supplied by a direct-upload ticket. A proxy that issues a canonical tenant in uploadHeaders and authenticates the PUT with it can reject the caller's alias, including on the legacy fallback. On a same-origin ticket, the caller's tenant header should apply only when the ticket has none, with header names compared case-insensitively.

A second independent pass found that the client drops typed capacity refusals on finalize and on legacy upload. A finalize 503 with UPLOAD_CAPACITY_EXCEEDED becomes a status-only error, so the client can resend the whole artifact and then report an untyped failure. Every upload response with that reason must become the same typed error, and the direct flow must propagate it without a legacy retry. A client test should return that 503 from finalize and show that only preflight, PUT and finalize are requested.

Not blocking: these can be taken or left: compare the reason through UPLOAD_CAPACITY_REASONS.exceeded.reason instead of string literals; let extraction take the reservation and charge and settle it itself, and return releaseReservation in one way only, to remove the duplicated sequence and extra callbacks; extend the capacity test, which covers only the count limit, to the byte budget, the expansion charge, release after a finalize failure and admission of a second tenant; and add to the error reason row that an admitted archive's expansion can also exceed the byte budget.

Coverage and Repo Guards fail in the wire compatibility tests, and the failures are likely related to this change: it alters the declared upload client requests with the tenant header and the 503 body parsing. The artifact consumer mutation failure comes from #5278 on the base branch, which fails the same gate. One other check was still in progress.

Not verified here: no tests were run, CI failures were read only from the failure summary, it was not traced whether a proxy forwards or overwrites the tenant header on the upload routes, and it is unknown how many real app bundles exceed 4 GiB combined.

Before merge, a single valid upload accepted on the base branch must fit the byte budgets again, and the other problems above need fixes. The pending ADR 0006 wire compatibility ledger decision for the new tenant headers also needs to be resolved.

@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch from 905de55 to 15b8b10 Compare November 30, 2026 18:29
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on 15b8b10. The verdict and the full list are in the summary comment.

@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch 2 times, most recently from 2868af0 to 28b27fa Compare November 30, 2026 18:47
@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch from 28b27fa to 49ec620 Compare November 30, 2026 19:00
@apex-sentinel-staging

This comment has been minimized.

Comment thread packages/remote/src/upload-client.ts Resolved
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At f97e504, the direct client now sends Content-Length and the daemon reserves that size, legacy capacity refusals are mapped, and both retry guards compare the capacity reason. Valid large uploads can still be refused, and the size fix does not reach the built-in proxy. This follows the earlier review.

The tenant budget problem is still open. upload-capacity.ts:7 keeps MAX_BYTES_PER_TENANT at 4 GiB, while resumable-upload.ts:402 and upload-http.ts:210 add expanded bytes on top of the still-charged compressed payload. The artifact limits allow 2 GiB compressed plus 4 GiB expanded. So a valid 1.2 GB tgz that expands to 3.5 GB passes on the base, but fails here on an empty tenant. The 503 says it is retriable, so clients retry a request that can never pass. Any single upload accepted by the artifact limits must fit an empty tenant budget. Please derive the tenant budget from MAX_ARTIFACT_COMPRESSED_BYTES + MAX_ARCHIVE_EXPANDED_BYTES in host-kit, keep MAX_BYTES_TOTAL at least as large, and add a test that charges both maxima on an empty tenant.

The size fix likely does not work through the remote proxy. daemon-proxy.ts:141 does not forward content-length, and daemon-proxy.ts:779 passes the body as a stream. The upstream request should then be chunked, so upload-http.ts:194 reserves MAX_ARTIFACT_COMPRESSED_BYTES. A legacy upload can get a 503 that the base does not return, when the tenant has more than 2 GiB reserved or the daemon more than 14 GiB. The current capacity test covers only a direct client. Legacy admission must charge at most the bytes the request will write, however each hop frames the body. Either forward content-length on POST /upload, or charge accepted body bytes as they are written (onAcceptedBodyBytes to addBytes, capped by the byte limit).

On the same path, a second independent pass found a concrete case: a tenant with 3 GiB retained sends a valid 1 MiB legacy upload with Content-Length through createDaemonProxyServer. The proxy drops the header (daemon-proxy.ts:744, transfer-transport.ts:148, transfer-transport.ts:269), so upload-http.ts:197 tries to reserve 2 GiB and returns 503 with 1 GiB still free. A known-length legacy body must keep its validated length through the built-in proxy, and only bodies of truly unknown size should reserve the ceiling. A test through that proxy should show this 1 MiB upload being accepted.

For a follow-up issue, not for this PR:

  • Reservations are still released before the writer stops and removal succeeds (resumable-upload.ts:441); release only after both, and retry failed removals.
  • A failed removal can still lose the only record of a reservation (artifact-tracking.ts:575); keep one record for the directory and reservation until removal succeeds.
  • A rejected materialized cleanup still skips uploaded-source cleanup (session-app-source-deployment.ts:143); run the latter in an inner finally.

These threads no longer apply and can be resolved:

Repo Guards fails the ADR 0006 daemon RPC wire-compatibility check, likely because of this PR. The changed declarations include handleUpload, uploadLegacyArtifact, UploadResponse and finalizeDirectUpload, alongside download declarations from the base (#5278). Coverage (unit-ci) also failed. Body rules was cancelled, and one other check was still in progress.

Not verified here: no tests were run. The cause of the Coverage failure is unknown, because the captured log ends before any failing test. Chunked forwarding by undici for a stream without content-length was not observed live. Hosted proxies other than @agent-device/proxy were not checked for forwarding that header.

Before merge, the tenant budget and proxy upload cases need fixes with the tests above, and Repo Guards needs to pass.

@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch from f97e504 to 8f48d74 Compare December 1, 2026 09:19
@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch from 8f48d74 to 48b785f Compare December 1, 2026 09:45
@thymikee

thymikee commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

At 48b785f, valid uploads can still be refused for capacity, and the wire compatibility check fails. Since the earlier review, both retry guards now compare the typed capacity reason. The tenant byte cap, legacy upload admission, the typed legacy refusal and the wire ledger still need work.

The tenant cap is too small (upload-capacity.ts:7, unchanged since f97e504). MAX_BYTES_PER_TENANT is 4 GiB, but one artifact may use 2 GiB compressed plus 4 GiB expanded (artifact-limits.ts:1). On an empty tenant, a valid 1.2 GB tgz that expands to 3.5 GB is charged 1.2 GB (resumable-upload.ts:276, upload-http.ts:118), then adds 3.5 GB (resumable-upload.ts:402, upload-http.ts:210). It is refused as UPLOAD_CAPACITY_EXCEEDED with a retriable 503 (upload-capacity.ts:57, http-errors.ts:27, upload-client.ts:546). The base installs this archive. Here it can never succeed, so clients that honor retriable loop. Any single upload accepted by the per-artifact limits must fit an empty tenant budget. Please derive the cap from MAX_ARTIFACT_COMPRESSED_BYTES + MAX_ARCHIVE_EXPANDED_BYTES, keep MAX_BYTES_TOTAL at least that large, and add a test that admits both maxima on an empty tenant.

Legacy upload reserves 2 GiB for small files again. The rebase dropped f97e504, so uploadLegacyArtifact sends chunked with no Content-Length (upload-client.ts:282), and the daemon reserves MAX_ARTIFACT_COMPRESSED_BYTES (upload-http.ts:194). The built-in proxy also drops the length (daemon-proxy.ts:141). A tenant with more than 2 GiB reserved, or a daemon with more than 14 GiB reserved, gets 503 for a valid 1 MiB upload that the base accepts. This affects the fallback when preflight is unsupported or a direct ticket fails twice. Legacy admission must charge at most the bytes the request will write, however each hop frames the body. Preferably the daemon owns this: reserve a validated Content-Length when present, otherwise add accepted body bytes as they are written (onAcceptedBodyBytes to reservation.addBytes, within the byte limit). Restoring the client header alone fixes only the direct route. Please test that a tenant with 3 GiB reserved can send 1 MiB through createDaemonProxyServer.

A second independent pass found the same cap problem on resumable finalize (resumable-upload.ts:402). A 1.2 GiB app-bundle archive expanding to 3.5 GiB passes preflight and PUT on an empty daemon, then gets the retriable 503 (upload-capacity.ts:58, upload-client.ts:410). A fresh ticket cannot help. The per-tenant cap must cover the shared compressed and expanded maxima, and the daemon cap must still admit a second tenant's maximum upload beside the first.

A second independent pass found that POST /upload hits this too (upload-http.ts:210). Even with a correct Content-Length, the full 1.2 GiB body is staged before the 4.7 GiB total is refused with the same temporary 503. The same shared cap must apply to legacy staging.

A second independent pass found the direct legacy case (upload-client.ts:282): with 1 GiB free, a 1 MiB fallback upload is sent chunked, so the daemon reserves 2 GiB and refuses it before reading the file. A known-size body should be charged its actual size, for example by sending Content-Length as f97e504 did and removing the explicit chunked header.

A second independent pass found the proxy case (daemon-proxy.ts:744): the forwarded headers omit a valid Content-Length (daemon-proxy.ts:141, transfer-transport.ts:148, transfer-transport.ts:269), so the daemon sees a chunked body and refuses 1 MiB with 1 GiB free. Every proxy route for POST /upload must preserve a validated length.

A second independent pass found that the typed legacy refusal is lost (upload-client.ts:308). A temporary capacity 503 becomes COMMAND_FAILED with the JSON inside the message, without reason or retriable. The legacy parser must map this status and reason to the same typed error as preflight and finalize, as f97e504 did.

A second independent pass found that the wire ledger is stale (ledger.json:183). Upload producers and consumers listed in surface.ts:168 and surface.ts:466 changed, but the digests and old acknowledgments (ledger.json:491, ledger.json:561, ledger.json:606) did not. Please record current digests, update the manifest where closure requires it, and give each compatible change a specific ADR 0006 rationale. If an existing peer cannot interoperate safely, the ADR's protocol version decision and matching ledger update apply.

Not blocking: since throwIfUploadCapacityRefused and the UploadResponse reason field are gone from the legacy path (upload-client.ts:307), the description's typed retriable claim could instead be dropped if that behavior is not meant to return, so this note can be taken or left.

Four lower-priority threads still apply. Two other threads do not and can be resolved: retry reason guards is fixed at this commit, and truncated tar charge is benign because manifest validation fails a truncated archive before the charge (release-before-removal remains a follow-up).

Repo Guards fails on pnpm check:daemon-wire-compat against v0.21.45, and this is likely caused by the change. Twelve listed declarations are upload changes from this PR, and three download declarations come from the base (#5278). Coverage also failed, but its log ends in passing output with no failing test. One other check is queued.

Not verified here: no tests were run, and the findings come from reading the code and the changes since f97e504. It is also not known whether dropping f97e504 was intentional (the description still describes its behavior), what caused the Coverage failure, or how the built-in proxy forwards chunked bodies in a live run.

Before merge, the tenant cap must fit one maximum upload, legacy admission must charge only written bytes on every hop, the typed legacy refusal must return, and the ADR 0006 ledger must be updated so Repo Guards passes.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch 2 times, most recently from a4c3cf8 to 78e1a50 Compare December 1, 2026 11:09
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

At ad64964, the first cleanup failure is observable and each closed ticket has one retry chain; surviving bytes remain charged while removal fails. The earlier review at 79b1c86 found no issues, and this follow-up finds no new code issues.

The cleanup retry thread no longer applies because the failure is observable and retries share one chain: #5281 (comment).

Both cancelled jobs are Body rules, which evaluate the pull request title and body; the code delta does not change the workflow or its rules. Not verified here: the cancellation cause and a passing gate result were not established because no cancellation log was provided. No conflicts are known. Before merge, confirm a completed Body rules result at the reviewed commit if the gate is required.

Not verified here: tests, mutation checks, and a live daemon run were not performed; the test failure before the fix and the log event runtime wiring were traced statically. The previously reported Android proxy-lease install remains applicable because this delta does not change device dispatch or native execution.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 2, 2026
@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch 2 times, most recently from 1c403c8 to e3ba3f5 Compare December 2, 2026 09:47
@apex-sentinel-staging

This comment has been minimized.

@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 2, 2026
@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

The code review of e3ba3f5 found no remaining code issues. Since the earlier review, cleanup retries retain capacity until removal succeeds, and expiry and rollback share one removal owner. No conflicts are known.

The cleanup retry thread no longer applies: the retry preserves capacity until the directory is removed.

All five checks were cancelled; the cancellation cause and affected Smoke Tests platform and phase are not identified. Prose covers the changed documentation, Bundle Size compares the changed packages, Smoke Tests shares daemon startup and dispatch with this change, and both Body rules entries check the updated title and body. Confirm completed required Prose, Smoke Tests, both Body rules entries if required, and Bundle Size checks for e3ba3f5 before merge.

Not verified here: local tests, CI reruns, or device actions were performed. Historical test counts and the Android run lack raw receipts. The earlier Android proxy-lease install receipt is attributed to c125cd3; the upload commits add no changed device-facing route since ad64964. The pre-start recording guard and durable owner normalization are already in #5278, whose live validation remains pending.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 2, 2026
@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch from e3ba3f5 to 0cd48fb Compare December 2, 2026 10:19
@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot 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.

Apex Sentinel: 1 new finding on 0cd48fb. The verdict and the full list are in the summary comment.

Comment thread packages/daemon/src/server/upload.ts Resolved
@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 2, 2026
@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

The follow-up at 0cd48fb removes expired owner records after the same deadline enforced by historical reads; the earlier cleanup disposition was rechecked in source. The code review found no outstanding issues, and no conflicts are known.

The request reservation thread no longer applies: the standard Node listener supplies a fixed-length parsed request, so its body is covered by the declared-length reservation.

The initial Apple Smoke Tests aggregate failed after a prerequisite was cancelled. The latest GitHub check snapshot for 0cd48fb reports 19 passed, with no pending or failed checks.

Not verified here: tests, gates, CI reruns, or device actions were not performed. The previously accepted Android proxy-lease install evidence is attributed to c125cd3; no raw transcript or current-head device run was supplied. The separate #5278 live-evidence gaps are recorded in its issue artifact.

The code review is complete for 0cd48fb.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 2, 2026
@thymikee
thymikee force-pushed the fix/daybreak-upload-review branch from 0cd48fb to 20313d1 Compare December 2, 2026 16:54
@thymikee thymikee removed the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Dec 2, 2026
@apex-sentinel-staging

This comment has been minimized.

@thymikee

thymikee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

@apex-sentinel-staging review

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 20313d1

Re-run on the same commit.

No open findings.

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (7c6ef4a2ed46).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@thymikee thymikee closed this Dec 2, 2026
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.

1 participant