Skip to content

Commit 4c74a43

Browse files
committed
fix(remote): load the lease beat only for the request that can use it
The extraction that moved the beat out of daemon-client.ts put it in cli.ts's eager closure, which every command pays for, and took it from 295 modules to 296 (#2990's coverage failure). The beat is only ever useful to one kind of request: one heading to a remote daemon that names a lease. Both guards are cheap and local — the lease scope comes off the request the caller already built — so the module now loads behind them and the closure is back to 295. Also folds the beat's thin wrappers away from the entry surface, which is what the four test-only exports in daemon-client.ts had become: createLeaseRenewalBeat and leaseScopeForHeartbeat existed only for tests, the scope precedence they pinned is already covered where it lives in contracts, and the one invariant that wasn't covered anywhere — a beat per request id, so a timed-out beat cannot cancel its successor — is now pinned over the real transport, where a shared id is actually observable.
1 parent b391a4a commit 4c74a43

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

‎src/daemon-client/daemon-client.ts‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import {
1313
import { INTERNAL_COMMANDS, PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog';
1414
import { resolveCommandTimeoutPolicy } from '@agent-device/command-registry/registry';
1515
import { resolveCommandRequestTimeoutMs } from '@agent-device/command-registry/timeout-policy';
16-
import { prepareRemoteRequestArtifacts } from '../remote/daemon-artifacts.ts';
16+
import {
17+
prepareRemoteRequestArtifacts,
18+
type PreparedRemoteRequest,
19+
} from '../remote/daemon-artifacts.ts';
1720
import {
1821
attachActiveSessionAddressHint,
1922
attachRepairSessionAddressHint,
@@ -26,7 +29,8 @@ import {
2629
type EnsuredDaemon,
2730
} from './daemon-client-lifecycle.ts';
2831
import { sendRequest } from './daemon-client-transport.ts';
29-
import { buildUploadLeaseHeartbeat, runProtectedLeaseWork } from './daemon-client-lease-beat.ts';
32+
import { isRemoteDaemon, type DaemonInfo } from './daemon-client-metadata.ts';
33+
import { leaseScopeFromRequest } from '@agent-device/contracts/lease-scope';
3034

3135
export type DaemonRequest = SharedDaemonRequest;
3236
export type DaemonResponse = SharedDaemonResponse;
@@ -62,10 +66,11 @@ export async function sendToDaemon(
6266
{ requestId, session: req.session },
6367
);
6468
const info = daemon.info;
65-
const preparedRemoteRequest = await runProtectedLeaseWork({
66-
heartbeat: buildUploadLeaseHeartbeat(info, settings, requestWithoutAuthFlag),
67-
task: (signal) => prepareRemoteRequestArtifacts(requestWithoutAuthFlag, info, signal),
68-
});
69+
const preparedRemoteRequest = await protectArtifactUploadWithLeaseBeats(
70+
info,
71+
settings,
72+
requestWithoutAuthFlag,
73+
);
6974
writeInstallInProgressNotice(requestWithoutAuthFlag.command);
7075

7176
const request = buildTransportRequest(
@@ -285,3 +290,29 @@ function isInstallLikeCommand(command: string | undefined): boolean {
285290
command === INTERNAL_COMMANDS.installSource
286291
);
287292
}
293+
294+
/**
295+
* Uploads a remote request's artifact under a lease beat, so a large artifact cannot outlive the
296+
* lease paying for the device it is going to (#2946).
297+
*
298+
* Only a remote daemon uploads, and a request that names no lease has nothing to renew, so those two
299+
* guards answer for the overwhelming majority of requests — and they are what let the beat module
300+
* stay out of `cli.ts`'s eager closure, which every command pays for. Both are cheap and local: the
301+
* lease scope is read from the request the caller already built.
302+
*/
303+
async function protectArtifactUploadWithLeaseBeats(
304+
info: DaemonInfo,
305+
settings: DaemonClientSettings,
306+
request: Omit<DaemonRequest, 'token'>,
307+
): Promise<PreparedRemoteRequest> {
308+
const leaseScope = leaseScopeFromRequest(request);
309+
if (!isRemoteDaemon(info) || !leaseScope.leaseId) {
310+
return await prepareRemoteRequestArtifacts(request, info, new AbortController().signal);
311+
}
312+
const { buildUploadLeaseHeartbeat, runProtectedLeaseWork } =
313+
await import('./daemon-client-lease-beat.ts');
314+
return await runProtectedLeaseWork({
315+
heartbeat: buildUploadLeaseHeartbeat(info, settings, request),
316+
task: (signal) => prepareRemoteRequestArtifacts(request, info, signal),
317+
});
318+
}

‎src/remote/daemon-artifacts.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type DaemonArtifactEndpoint = {
2020
token: string;
2121
};
2222

23-
type PreparedRemoteRequest = {
23+
export type PreparedRemoteRequest = {
2424
positionals: string[];
2525
flags?: DaemonRequest['flags'];
2626
installSource?: NonNullable<DaemonRequest['meta']>['installSource'];

0 commit comments

Comments
 (0)