Repository navigation
refactor(transport): share outgoing transfer phase ownership #4551
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
Merged
thymikee
merged 14 commits into
t3/audit4441/remote-budgets
from
t3/audit4441/phase-owner
Nov 15, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
459b321
refactor(transport): one owner for transfer phase budgets and the out…
thymikee 0e493d5
refactor(transport): keep transfer phase budgets in host-kit's transp…
thymikee b9b226f
chore(gates): acknowledge the host-kit phase observer in the upload a…
thymikee 731c3d6
docs(agents): drop the transfer-phase ownership line that overflows t…
thymikee a889020
refactor(transport): keep phase surface limited to current consumers
thymikee d7d1be7
fix(transport): compose the host-kit write owner beside the phase owner
thymikee 717fceb
chore(gates): acknowledge the composed write and phase owners in the …
thymikee b8a74f6
test(transport): dispose phase-suite watchdogs with their test
thymikee e3a4793
refactor(transport): keep control deadlines in the shared phase owner
thymikee 0230de4
docs(transport): retain the response phase ordering constraint
thymikee 3c3674a
refactor(remote): consolidate upload failure and join ownership
thymikee 48b8a98
chore(gates): acknowledge consolidated upload outcome ownership
thymikee e5fa7a2
refactor(remote): omit the redundant successful join callback
thymikee 024cb04
chore(gates): pin the final upload join digest
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
218 changes: 218 additions & 0 deletions
218
packages/host-kit/src/internal/transport-phases.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,218 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { EventEmitter } from 'node:events'; | ||
| import type { ClientRequest } from 'node:http'; | ||
| import { afterEach, beforeEach, onTestFinished, test, vi } from 'vitest'; | ||
| import { AppError } from '@agent-device/kernel/errors'; | ||
| import { | ||
| createTransferPhaseWatchdog, | ||
| isTransferPhaseTimeoutError, | ||
| observeClientRequestPhases, | ||
| observeCompletedRequestWrites, | ||
| TRANSFER_BODY_INACTIVITY_TIMEOUT_MS, | ||
| TRANSFER_CONNECT_TIMEOUT_MS, | ||
| TRANSFER_RESPONSE_HEADER_TIMEOUT_MS, | ||
| } from './transport.ts'; | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| function watchPhases(callerSignal?: AbortSignal) { | ||
| const watchdog = createTransferPhaseWatchdog(callerSignal); | ||
| onTestFinished(() => watchdog.dispose()); | ||
| return watchdog; | ||
| } | ||
|
|
||
| test('the three transfer ceilings are the adopted policy values', () => { | ||
| assert.equal(TRANSFER_CONNECT_TIMEOUT_MS, 30_000); | ||
| assert.equal(TRANSFER_RESPONSE_HEADER_TIMEOUT_MS, 30_000); | ||
| assert.equal(TRANSFER_BODY_INACTIVITY_TIMEOUT_MS, 120_000); | ||
| }); | ||
|
|
||
| test('a body phase restarts its ceiling on every byte that moves', async () => { | ||
| const watchdog = watchPhases(); | ||
| watchdog.arm('response-body', TRANSFER_BODY_INACTIVITY_TIMEOUT_MS); | ||
| for (const _ of [0, 1, 2]) { | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_BODY_INACTIVITY_TIMEOUT_MS - 20_000); | ||
| watchdog.touchBody(); | ||
| } | ||
| assert.equal(watchdog.signal.aborted, false); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_BODY_INACTIVITY_TIMEOUT_MS + 1); | ||
| assert.equal(watchdog.signal.aborted, true); | ||
| assertPhaseDetails(watchdog.signal.reason, 'response-body', 120_000); | ||
| }); | ||
|
|
||
| test('a phase that carries no bytes is not extended by progress reported in another phase', async () => { | ||
| const watchdog = watchPhases(); | ||
| watchdog.arm('response-headers', TRANSFER_RESPONSE_HEADER_TIMEOUT_MS); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_RESPONSE_HEADER_TIMEOUT_MS - 10_000); | ||
| watchdog.touchBody(); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_RESPONSE_HEADER_TIMEOUT_MS); | ||
| assertPhaseDetails(watchdog.signal.reason, 'response-headers', 30_000); | ||
| }); | ||
|
|
||
| test('arming the next phase stops counting the previous one', async () => { | ||
| const watchdog = watchPhases(); | ||
| watchdog.arm('connect', TRANSFER_CONNECT_TIMEOUT_MS); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_CONNECT_TIMEOUT_MS - 1_000); | ||
| watchdog.arm('response-headers', TRANSFER_RESPONSE_HEADER_TIMEOUT_MS); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_RESPONSE_HEADER_TIMEOUT_MS - 1_000); | ||
| assert.equal(watchdog.signal.aborted, false); | ||
| await vi.advanceTimersByTimeAsync(1_000); | ||
| assertPhaseDetails(watchdog.signal.reason, 'response-headers', 30_000); | ||
| }); | ||
|
|
||
| test('an enclosing deadline wins and is never extended by a phase timer', async () => { | ||
| const caller = new AbortController(); | ||
| const reason = new Error('preparation aggregate expired'); | ||
| setTimeout(() => caller.abort(reason), 5_000); | ||
| const watchdog = watchPhases(caller.signal); | ||
| watchdog.arm('response-body', TRANSFER_BODY_INACTIVITY_TIMEOUT_MS); | ||
| await vi.advanceTimersByTimeAsync(5_001); | ||
| assert.equal(watchdog.signal.aborted, true); | ||
| assert.equal(watchdog.signal.reason, reason); | ||
| assert.equal(isTransferPhaseTimeoutError(watchdog.signal.reason), false); | ||
| }); | ||
|
|
||
| test('a phase expiry is its own typed evidence and never caller cancellation', async () => { | ||
| const caller = new AbortController(); | ||
| const watchdog = watchPhases(caller.signal); | ||
| watchdog.arm('connect', TRANSFER_CONNECT_TIMEOUT_MS); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_CONNECT_TIMEOUT_MS + 1); | ||
| assertPhaseDetails(watchdog.signal.reason, 'connect', 30_000); | ||
| assert.equal(caller.signal.aborted, false); | ||
| }); | ||
|
|
||
| test('a disposed watchdog stops watching', async () => { | ||
| const watchdog = watchPhases(); | ||
| watchdog.arm('connect', TRANSFER_CONNECT_TIMEOUT_MS); | ||
| watchdog.dispose(); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_CONNECT_TIMEOUT_MS * 3); | ||
| assert.equal(watchdog.signal.aborted, false); | ||
| }); | ||
|
|
||
| test('a phase is never armed after the attempt is already over', async () => { | ||
| const watchdog = watchPhases(); | ||
| watchdog.signal.throwIfAborted(); | ||
| const caller = new AbortController(); | ||
| caller.abort(new Error('gone')); | ||
| const late = watchPhases(caller.signal); | ||
| late.arm('response-headers', TRANSFER_RESPONSE_HEADER_TIMEOUT_MS); | ||
| assert.equal(vi.getTimerCount(), 0); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_RESPONSE_HEADER_TIMEOUT_MS * 2); | ||
| assert.equal(late.signal.reason.message, 'gone'); | ||
| }); | ||
|
|
||
| class FakeRequest extends EventEmitter { | ||
| destroyed = false; | ||
| readonly callbacks: unknown[] = []; | ||
| write(...args: unknown[]): boolean { | ||
| this.callbacks.push(args.find((arg) => typeof arg === 'function')); | ||
| return true; | ||
| } | ||
| complete(index: number): void { | ||
| const callback = this.callbacks[index]; | ||
| assert.ok(typeof callback === 'function', `write ${index} has no callback`); | ||
| callback(null); | ||
| } | ||
| } | ||
|
|
||
| function createObservedRequest() { | ||
| const fake = new FakeRequest(); | ||
| const watchdog = watchPhases(); | ||
| watchdog.arm('connect', TRANSFER_CONNECT_TIMEOUT_MS); | ||
| const req = fake as unknown as ClientRequest; | ||
| const phases = observeClientRequestPhases(req, watchdog); | ||
| const stopObservingWrites = observeCompletedRequestWrites(req, watchdog.signal, () => | ||
| watchdog.touchBody(), | ||
| ); | ||
| const emit = (event: string, ...args: unknown[]) => fake.emit(event, ...args); | ||
| const socket = (state: { connecting: boolean; encrypted?: boolean }) => | ||
| Object.assign(new EventEmitter(), state); | ||
| return { fake, req, watchdog, phases, stopObservingWrites, emit, socket }; | ||
| } | ||
|
|
||
| test('a fresh socket keeps the connect ceiling until it connects, even after the body flushed', async () => { | ||
| const { watchdog, emit, socket } = createObservedRequest(); | ||
| const fresh = socket({ connecting: true }); | ||
| emit('socket', fresh); | ||
| emit('finish'); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_CONNECT_TIMEOUT_MS + 1); | ||
| assertPhaseDetails(watchdog.signal.reason, 'connect', 30_000); | ||
| }); | ||
|
|
||
| test('the header wait starts only after both the flush and the connection', async () => { | ||
| const { watchdog, emit, socket } = createObservedRequest(); | ||
| const fresh = socket({ connecting: true }); | ||
| emit('socket', fresh); | ||
| emit('finish'); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_CONNECT_TIMEOUT_MS - 1_000); | ||
| fresh.emit('connect'); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_RESPONSE_HEADER_TIMEOUT_MS - 1); | ||
| assert.equal(watchdog.signal.aborted, false); | ||
| await vi.advanceTimersByTimeAsync(1); | ||
| assertPhaseDetails(watchdog.signal.reason, 'response-headers', 30_000); | ||
| }); | ||
|
|
||
| test('a TLS socket is established by secureConnect, not connect', async () => { | ||
| const { watchdog, emit, socket } = createObservedRequest(); | ||
| const tls = socket({ connecting: true, encrypted: true }); | ||
| emit('socket', tls); | ||
| tls.emit('connect'); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_CONNECT_TIMEOUT_MS + 1); | ||
| assertPhaseDetails(watchdog.signal.reason, 'connect', 30_000); | ||
| }); | ||
|
|
||
| test('a pooled socket is already connected and times the body write as inactivity', async () => { | ||
| const { fake, req, watchdog, emit, socket } = createObservedRequest(); | ||
| emit('socket', socket({ connecting: false })); | ||
| req.write(Buffer.from('body')); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_BODY_INACTIVITY_TIMEOUT_MS - 1_000); | ||
| fake.complete(0); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_BODY_INACTIVITY_TIMEOUT_MS - 1); | ||
| assert.equal(watchdog.signal.aborted, false); | ||
| await vi.advanceTimersByTimeAsync(1); | ||
| assertPhaseDetails(watchdog.signal.reason, 'request-body', 120_000); | ||
| }); | ||
|
|
||
| test('once the response started, later request events do not overwrite its phase', async () => { | ||
| const { fake, req, watchdog, phases, emit, socket } = createObservedRequest(); | ||
| const fresh = socket({ connecting: true }); | ||
| emit('socket', fresh); | ||
| req.write(Buffer.from('body')); | ||
| emit('response'); | ||
| assert.equal(phases.responseStarted, true); | ||
| fresh.emit('connect'); | ||
| emit('finish'); | ||
| await vi.advanceTimersByTimeAsync(TRANSFER_BODY_INACTIVITY_TIMEOUT_MS - 1_000); | ||
| fake.complete(0); | ||
| await vi.advanceTimersByTimeAsync(1_001); | ||
| assertPhaseDetails(watchdog.signal.reason, 'response-body', 120_000); | ||
| }); | ||
|
|
||
| test('disposing the phase observer removes only its own listeners', () => { | ||
| const { fake, phases, stopObservingWrites, emit, socket } = createObservedRequest(); | ||
| const fresh = socket({ connecting: true }); | ||
| emit('socket', fresh); | ||
| phases.dispose(); | ||
| assert.equal(fake.listenerCount('socket'), 0); | ||
| assert.equal(fake.listenerCount('finish'), 0); | ||
| assert.equal(fresh.listenerCount('connect'), 0); | ||
| assert.equal(fake.listenerCount('response'), 1, "the write owner's listener is its own"); | ||
| stopObservingWrites(); | ||
| assert.equal(fake.listenerCount('response'), 0); | ||
| }); | ||
|
|
||
| function assertPhaseDetails(reason: unknown, phase: string, timeoutMs: number): void { | ||
| assert.ok(reason instanceof AppError, `phase expiry must be an AppError, got ${String(reason)}`); | ||
| assert.equal(isTransferPhaseTimeoutError(reason), true); | ||
| assert.deepEqual(reason.details, { | ||
| reason: 'transfer_phase_timeout', | ||
| transferPhase: phase, | ||
| timeoutMs, | ||
| }); | ||
| } |
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.