Repository navigation
fix: bind upload ownership and shared staging capacity #5281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
thymikee
wants to merge
5
commits into
fix/lease-artifact-owner-admission
from
fix/daybreak-upload-review
+820
−181
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8989718
fix(upload): bind staging ownership and share capacity
thymikee 2f56918
fix(upload): reuse staging cleanup and request paths
thymikee 8877199
chore(gates): cover shared upload headers in the wire ledger
thymikee a9e275e
chore(gates): isolate upload wire header types
thymikee 20313d1
fix(upload): give staged cleanup one owner
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/daemon/src/server/__tests__/upload-http-capacity.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { createHash, randomUUID } from 'node:crypto'; | ||
| import { test } from 'vitest'; | ||
| import { | ||
| MAX_ARCHIVE_EXPANDED_BYTES, | ||
| MAX_ARTIFACT_COMPRESSED_BYTES, | ||
| } from '@agent-device/host-kit/archive'; | ||
| import { createRuntimeStateHandle } from '@agent-device/host-kit/runtime-slots'; | ||
| import { createDaemonProxyServer } from '@agent-device/proxy'; | ||
| import { | ||
| closeLoopbackServer, | ||
| listenOnLoopback, | ||
| skipWhenLoopbackUnavailable, | ||
| } from '@agent-device/test-kit'; | ||
| import { cleanupUploadedArtifact } from '../../resources/artifact-tracking.ts'; | ||
| import { createDaemonHttpServer } from '../http/http-server.ts'; | ||
| import { reserveUploadCapacity } from '../upload-capacity.ts'; | ||
|
|
||
| test('the staging budget admits one maximum-size archive and its expanded contents', () => { | ||
| const reservation = reserveUploadCapacity('archive-tenant', MAX_ARTIFACT_COMPRESSED_BYTES); | ||
| try { | ||
| assert.doesNotThrow(() => reservation.addBytes(MAX_ARCHIVE_EXPANDED_BYTES)); | ||
| } finally { | ||
| reservation.release(); | ||
| } | ||
| }); | ||
|
|
||
| test('legacy uploads use the same staged capacity over direct and proxied HTTP', async (t) => { | ||
| if (await skipWhenLoopbackUnavailable(t, 'upload capacity')) return; | ||
| const env = { ...process.env }; | ||
| delete env.AGENT_DEVICE_HTTP_AUTH_HOOK; | ||
| const runtime = createRuntimeStateHandle(); | ||
| const server = await runtime.run(() => | ||
| createDaemonHttpServer({ | ||
| token: 'test-token', | ||
| env, | ||
| cwd: process.cwd(), | ||
| handleRequest: async () => ({ ok: true, data: {} }), | ||
| }), | ||
| ); | ||
| try { | ||
| const port = await listenOnLoopback(server); | ||
| const base = `http://127.0.0.1:${port}`; | ||
| const auth = { authorization: 'Bearer test-token', 'x-agent-device-tenant': 'tenant-a' }; | ||
| const emptyHash = createHash('sha256').update('').digest('hex'); | ||
| const declaredBytes = Buffer.byteLength('new-app'); | ||
| for (const [index, sizeBytes] of [ | ||
|
thymikee marked this conversation as resolved.
Resolved
|
||
| MAX_ARTIFACT_COMPRESSED_BYTES, | ||
| MAX_ARTIFACT_COMPRESSED_BYTES, | ||
| MAX_ARTIFACT_COMPRESSED_BYTES - declaredBytes, | ||
| ].entries()) { | ||
| const preflight = await fetch(`${base}/upload/preflight`, { | ||
| method: 'POST', | ||
| headers: { ...auth, 'content-type': 'application/json' }, | ||
| body: JSON.stringify({ | ||
| sha256: emptyHash, | ||
| uploadAttemptId: randomUUID(), | ||
| fileName: `empty-${index}.apk`, | ||
| sizeBytes, | ||
| artifactType: 'file', | ||
| }), | ||
| }); | ||
| assert.equal(preflight.status, 200); | ||
| await preflight.json(); | ||
| } | ||
|
|
||
| const uploadLegacy = (origin: string, token: string) => | ||
| fetch(`${origin}/upload`, { | ||
| method: 'POST', | ||
| headers: { | ||
| ...auth, | ||
| authorization: `Bearer ${token}`, | ||
| 'x-artifact-type': 'file', | ||
| 'x-artifact-filename': 'new.apk', | ||
| 'content-length': String(declaredBytes), | ||
| }, | ||
| body: 'new-app', | ||
| }); | ||
| const admittedWithDeclaredLength = await uploadLegacy(base, 'test-token'); | ||
| assert.equal(admittedWithDeclaredLength.status, 200); | ||
| const retainedId = ((await admittedWithDeclaredLength.json()) as { uploadId: string }).uploadId; | ||
|
|
||
| const full = await uploadLegacy(base, 'test-token'); | ||
| assert.equal(full.status, 503); | ||
| const refusal = (await full.json()) as { reason?: string }; | ||
| assert.equal(refusal.reason, 'UPLOAD_CAPACITY_EXCEEDED'); | ||
|
|
||
| runtime.run(() => cleanupUploadedArtifact(retainedId)); | ||
| const proxy = createDaemonProxyServer({ | ||
| upstreamBaseUrl: base, | ||
| upstreamToken: 'test-token', | ||
| clientToken: 'proxy-token', | ||
| }); | ||
| try { | ||
| const proxyPort = await listenOnLoopback(proxy); | ||
| const admittedThroughProxy = await uploadLegacy( | ||
| `http://127.0.0.1:${proxyPort}`, | ||
| 'proxy-token', | ||
| ); | ||
| assert.equal(admittedThroughProxy.status, 200); | ||
| const uploaded = (await admittedThroughProxy.json()) as { uploadId: string }; | ||
| assert.ok(uploaded.uploadId); | ||
| } finally { | ||
| await closeLoopbackServer(proxy); | ||
| } | ||
| } finally { | ||
| try { | ||
| await closeLoopbackServer(server); | ||
| } finally { | ||
| runtime.dispose(); | ||
| } | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.