Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b2f7d14
fix(remote): bound artifact preparation and propagate cancellation
thymikee Nov 12, 2026
bec05d5
fix(remote): budget each artifact-transfer phase inside the preparati…
thymikee Nov 13, 2026
f1e7532
fix(remote): end each upload attempt's owned work and time its real p…
thymikee Nov 13, 2026
6089d90
fix(remote): retire completed upload hop budgets before resuming
thymikee Nov 14, 2026
9544701
test(remote): preserve retryable failures across resumed upload hops
thymikee Nov 14, 2026
7e03b3e
test(remote): clarify resumed upload progress windows
thymikee Nov 14, 2026
5f818b3
refactor(remote): keep the transfer phase owner and preparation aggre…
thymikee Nov 14, 2026
8a62f24
chore(gates): acknowledge the retired hop watchdog in the upload atte…
thymikee Nov 14, 2026
09e1e76
refactor(remote): keep the phase-expiry reason and error constructor …
thymikee Nov 14, 2026
fae463f
fix(remote): reuse shared response reader and tighten review guards
thymikee Nov 14, 2026
c9f91d0
fix(remote): count completed body writes as upload progress
thymikee Nov 14, 2026
0eb01da
fix(transport): own completed request writes in host-kit and time fix…
thymikee Nov 14, 2026
f295243
chore(gates): acknowledge the host-kit write owner in the upload wire…
thymikee Nov 14, 2026
1777e1d
test(remote): tie loopback fixture cleanup to the test lifecycle
thymikee Nov 14, 2026
ad4603b
test(remote): end fixture teardown waits on settlement, not loop turns
thymikee Nov 14, 2026
22cf1d4
fix(transport): make remaining progress tests independent of scheduling
thymikee Nov 14, 2026
cffddd7
fix(remote): stop control request expiry before upload fallback
thymikee Nov 14, 2026
dc5e024
refactor(test): simplify shared upload fixture expectations
thymikee Nov 14, 2026
049d116
fix(test): preserve declared upload budgets and caller cancellation p…
thymikee Nov 14, 2026
e486339
chore(gates): track the shared upload control request owner
thymikee Nov 14, 2026
4de08a9
refactor(remote): constrain control requests at their sending owner
thymikee Nov 14, 2026
d33183d
chore(gates): acknowledge fixed upload control request construction
thymikee Nov 14, 2026
77a5e16
test(remote): simplify control request count expectations
thymikee Nov 14, 2026
16a12af
fix(remote): start the header deadline at request finish
thymikee Nov 14, 2026
ee7735b
chore(gates): acknowledge request-finish header timing
thymikee Nov 14, 2026
0d5790e
test(remote): pin the upload source close before advancing time
thymikee Nov 14, 2026
13a4c1d
test(remote): narrow the held-close callback and descriptor
thymikee Nov 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions packages/host-kit/src/internal/transport-writes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import assert from 'node:assert/strict';
import { EventEmitter } from 'node:events';
import { runInNewContext } from 'node:vm';
import type { ClientRequest } from 'node:http';
import { test } from 'vitest';
import { observeCompletedRequestWrites } from '../transport.ts';

class FakeRequest extends EventEmitter {
destroyed = false;
accepts = true;
readonly writes: { self: unknown; args: unknown[] }[] = [];
write(this: FakeRequest, ...args: unknown[]): boolean {
this.writes.push({ self: this, args });
return this.accepts;
}
complete(index: number, error: Error | null = null): void {
const callback = this.writes[index]?.args.find((arg) => typeof arg === 'function');
assert.ok(typeof callback === 'function', `write ${index} has no callback`);
callback(error);
}
}

type Observed = ReturnType<typeof observe>;

function observe(prepare?: (fake: FakeRequest) => void) {
const fake = new FakeRequest();
prepare?.(fake);
const caller = new AbortController();
const completed = { count: 0 };
const req = fake as unknown as ClientRequest;
const dispose = observeCompletedRequestWrites(req, caller.signal, () => (completed.count += 1));
return { fake, req, caller, completed, dispose };
}

test('only a non-empty write that completes without an error is reported', () => {
const { fake, req, completed, dispose } = observe();
req.write(Buffer.from('queued'));
req.write('', () => {});
fake.complete(1);
req.write(new Uint8Array(0), () => {});
fake.complete(2);
req.write('failed', () => {});
fake.complete(3, new Error('EPIPE'));
for (const [text, encoding] of [
['a', 'hex'],
['zz', 'hex'],
['zz', 'HEX'],
['=', 'base64'],
['=', 'base64url'],
] as const) {
req.write(text, encoding as BufferEncoding, () => {});
fake.complete(fake.writes.length - 1);
}
assert.equal(
completed.count,
0,
'a queued, empty, failed or encoded-empty write is not progress',
);
fake.complete(0);
assert.equal(completed.count, 1);
dispose();
});

test.each([
['utf8', 'é'],
['hex', 'ff'],
['base64', 'YQ=='],
['base64url', 'YQ'],
] as const)('a successful %s string write is reported', (encoding, text) => {
const { fake, req, completed, dispose } = observe();
req.write(text, encoding);
fake.complete(0);
assert.equal(completed.count, 1);
dispose();
});

test('a Uint8Array from another realm is reported', () => {
const { fake, req, completed, dispose } = observe();
const chunk = runInNewContext('new Uint8Array([1])') as Uint8Array;
assert.equal(chunk instanceof Uint8Array, false);
req.write(chunk);
fake.complete(0);
assert.equal(completed.count, 1);
dispose();
});

test('the observed write keeps every argument, this, the result and one callback', () => {
const { fake, req, dispose } = observe();
const calls: unknown[] = [];
const error = new Error('EPIPE');
fake.accepts = false;
assert.equal(
req.write('a', 'latin1', (cause) => calls.push(cause)),
false,
);
fake.accepts = true;
assert.equal(
req.write('b', (cause) => calls.push(cause)),
true,
);
assert.equal(req.write('c'), true);
assert.deepEqual(
fake.writes.map(({ self, args }) => [self === fake, ...args.map((arg) => typeof arg)]),
[
[true, 'string', 'string', 'function'],
[true, 'string', 'function'],
[true, 'string', 'undefined', 'function'],
],
);
assert.equal(fake.writes[0]?.args[1], 'latin1');
fake.complete(0, error);
fake.complete(1);
fake.complete(2);
assert.deepEqual(calls, [error, null]);
dispose();
});

const endings: [string, (observed: Observed) => unknown][] = [
['the response started', ({ fake }) => fake.emit('response')],
['the signal aborted', ({ caller }) => caller.abort()],
['the request was destroyed', ({ fake }) => (fake.destroyed = true)],
['the observer was disposed', ({ dispose }) => dispose()],
];

test.each(endings)('a completion after %s is not reported', (_, end) => {
const observed = observe();
const calls: unknown[] = [];
observed.req.write(Buffer.from('late'), (cause) => calls.push(cause));
end(observed);
observed.fake.complete(0);
assert.equal(observed.completed.count, 0);
assert.deepEqual(calls, [null], 'the caller still hears its own completion');
observed.dispose();
});

test('disposing removes the listener and restores only the write it wrapped', () => {
const inherited = observe();
inherited.dispose();
assert.equal(inherited.fake.listenerCount('response'), 0);
assert.equal(Object.hasOwn(inherited.fake, 'write'), false, 'the inherited write is back');
const ownWrite = () => true;
const owned = observe((fake) => Object.assign(fake, { write: ownWrite }));
owned.dispose();
assert.equal(owned.fake.write, ownWrite, 'an own write is put back');
const wrapped = observe();
const foreign = () => true;
Object.assign(wrapped.fake, { write: foreign });
wrapped.dispose();
assert.equal(wrapped.fake.write, foreign, 'a later wrapper stays installed');
});
7 changes: 7 additions & 0 deletions packages/host-kit/src/internal/transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ test('loadNodeHttpRequester selects the protocol module and keeps request mutabl
test('readNodeHttpResponseBody decodes the complete response stream', async () => {
const response = Readable.from(['hello', Buffer.from(' world')]) as unknown as IncomingMessage;
assert.equal(await readNodeHttpResponseBody(response), 'hello world');
const observed: string[] = [];
const progressing = Readable.from(['hello', Buffer.from(' world')]) as unknown as IncomingMessage;
assert.equal(
await readNodeHttpResponseBody(progressing, (chunk) => observed.push(chunk)),
'hello world',
);
assert.deepEqual(observed, ['hello', ' world']);
});

test('readNodeHttpRequestBody returns bytes and preserves the caller error message at the limit', async () => {
Expand Down
61 changes: 58 additions & 3 deletions packages/host-kit/src/internal/transport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from 'node:crypto';
import type { IncomingMessage } from 'node:http';
import { types } from 'node:util';
import type { ClientRequest, IncomingMessage } from 'node:http';
import { AppError } from '@agent-device/kernel/errors';

export function consumeTextLines(
Expand Down Expand Up @@ -42,11 +43,15 @@ export async function loadNodeHttpRequester(protocol: string): Promise<NodeHttpR
: (await import('node:http')).default;
}

export function readNodeHttpResponseBody(res: IncomingMessage): Promise<string> {
export function readNodeHttpResponseBody(
res: IncomingMessage,
onChunk?: (chunk: string) => void,
): Promise<string> {
return new Promise((resolve, reject) => {
let body = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
res.on('data', (chunk: string) => {
onChunk?.(chunk);
body += chunk;
});
res.on('end', () => resolve(body));
Expand Down Expand Up @@ -82,3 +87,53 @@ export function timingSafeStringEqual(a: string, b: string): boolean {
const hashB = crypto.createHash('sha256').update(b).digest();
return crypto.timingSafeEqual(hashA, hashB);
}

/**
* Reports each non-empty `req.write` chunk that Node completes without an error: its bytes reached
* the local kernel, not the peer. A queued write, `drain`, and a completion after the response
* started, after `signal` aborted, after a destroy or after disposal are not reported.
*
* Attach it in the tick that creates the request, before anything writes to it. The wrapper keeps
* the arguments, `this`, the backpressure result and one call of the caller's callback with its
* error. Disposing removes its listener and restores `write` only while this wrapper is installed.
*/
export function observeCompletedRequestWrites(
req: ClientRequest,
signal: AbortSignal,
onCompleted: () => void,
): () => void {
const ownWrite = Object.getOwnPropertyDescriptor(req, 'write');
const write = req.write;
let observing = true;
const onResponse = () => {
observing = false;
};
const observedWrite = function (this: ClientRequest, ...args: unknown[]): boolean {
const [chunk, encoding] = args;
const callbackIndex = typeof encoding === 'function' ? 1 : 2;
const callback = args[callbackIndex];
const encodingName = typeof encoding === 'string' ? encoding.toLowerCase() : undefined;
const hasBytes =
typeof chunk === 'string'
? encodingName === 'hex' || encodingName === 'base64' || encodingName === 'base64url'
? Buffer.from(chunk, encodingName).byteLength > 0
: chunk.length > 0
: types.isUint8Array(chunk) && chunk.byteLength > 0;
if (observing && hasBytes) {
args[callbackIndex] = (error?: Error | null) => {
if (!error && observing && !req.destroyed && !signal.aborted) onCompleted();
if (typeof callback === 'function') callback(error);
};
}
return Reflect.apply(write, this, args);
};
req.write = observedWrite;
req.prependListener('response', onResponse);
return () => {
observing = false;
req.off('response', onResponse);
if (req.write !== observedWrite) return;
if (ownWrite) Object.defineProperty(req, 'write', ownWrite);
else Reflect.deleteProperty(req, 'write');
};
}
1 change: 1 addition & 0 deletions packages/host-kit/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {
consumeTextLines,
loadNodeHttpRequester,
observeCompletedRequestWrites,
readNodeHttpRequestBody,
readNodeHttpResponseBody,
timingSafeStringEqual,
Expand Down
Loading
Loading