Skip to content

Commit 73f6cf1

Browse files
committed
refactor(daemon): separate runtime composition types
1 parent 7bf1525 commit 73f6cf1

5 files changed

Lines changed: 113 additions & 117 deletions

File tree

‎packages/daemon/src/__tests__/support/daemon-runtime-deps-fixture.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
ComposeDaemonRuntimeDeps,
88
DaemonRuntimeDeps,
99
DaemonRuntimeDepsInput,
10-
} from '../../server/http/daemon-runtime.ts';
10+
} from '../../server/http/daemon-runtime-deps.ts';
1111
import type { PlatformOwnerLifecycle } from '../../core/platform-owner-lifecycle.ts';
1212
import { clearAndroidObservationFixture } from './android-observation-fixture.ts';
1313
import { fakeCreateCommandSurface } from './command-surface-fake.ts';

‎packages/daemon/src/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export { daemonStartupExitCode, startDaemonRuntime } from './server/http/daemon-runtime.ts';
22
export { reportDaemonClose } from './server/http/server-shutdown.ts';
3+
export type { DaemonRuntimeController } from './server/http/daemon-runtime.ts';
34
export type {
45
ComposeDaemonRuntimeDeps,
5-
DaemonRuntimeController,
66
DaemonRuntimeDeps,
7-
} from './server/http/daemon-runtime.ts';
7+
} from './server/http/daemon-runtime-deps.ts';
88
export type { ShutdownReport } from './server/http/server-shutdown.ts';
99
export type { DaemonInvokeFn, DaemonRequest } from './core/daemon-request.ts';
1010
export type { DaemonPlatformServices } from './core/platform-services.ts';

‎packages/daemon/src/server/http/daemon-runtime-composition.test.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DaemonRuntimeDepsInput } from './daemon-runtime-deps.ts';
12
import assert from 'node:assert/strict';
23
import fs from 'node:fs';
34
import path from 'node:path';
@@ -15,11 +16,7 @@ import {
1516
} from '@agent-device/test-kit';
1617
import { interactorResolution } from '../../core/interactor-resolution.ts';
1718
import { daemonRuntimeDepsFixture } from '../../__tests__/support/daemon-runtime-deps-fixture.ts';
18-
import {
19-
daemonStartupExitCode,
20-
startDaemonRuntime,
21-
type DaemonRuntimeDepsInput,
22-
} from './daemon-runtime.ts';
19+
import { daemonStartupExitCode, startDaemonRuntime } from './daemon-runtime.ts';
2320

2421
// The server builds its own store, so capture that instance: the proof is about the store the
2522
// server hands to the composition callbacks, not one the test fabricates.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import type { AppLogSessionArtifacts } from '@agent-device/contracts/app-log-runtime';
2+
import type {
3+
LeaseLifecycleProvider,
4+
ProviderExpiredLeaseRecovery,
5+
} from '@agent-device/contracts/device';
6+
import type { HostDiagnostics } from '@agent-device/contracts/host-diagnostics';
7+
import type {
8+
PluginCleanupOutcome,
9+
PluginInventoryEntry,
10+
} from '@agent-device/contracts/plugin-inventory';
11+
import type { ClaimRecoveryGatewayFactory } from '@agent-device/daemon-contracts/device-claim-recovery';
12+
import type { OwnedProcessRecordStore } from '@agent-device/host-kit/process';
13+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
14+
import type { InteractorResolution } from '../../core/interactor-resolution.ts';
15+
import type { PlatformOwnerLifecycle } from '../../core/platform-owner-lifecycle.ts';
16+
import type { ProviderDeviceAdmission } from '../../core/provider-device-admission.ts';
17+
import type { RequestRouterDeps } from '../request-router.ts';
18+
19+
/**
20+
* Every capability the daemon runtime cannot construct for itself: the platform and provider
21+
* runtimes, host diagnostics, credential fingerprinting, resource cleanup, the platform lifecycle
22+
* owners, and the request-router inputs that name them. The server owns the paths, the policy, and
23+
* the session store, and asks the root for the rest through a `composeDeps` factory — the process
24+
* root's implementation is `src/daemon-composition.ts` (ADR 0019). All members are required: a
25+
* daemon process either receives the root's composition or cannot start, and a test passes the
26+
* same factory, so the production assembly is the one every runtime test boots.
27+
*/
28+
export type DaemonRuntimeDeps = Readonly<{
29+
ownedProcessRecords: OwnedProcessRecordStore;
30+
hostDiagnostics: HostDiagnostics;
31+
ownerLifecycle: PlatformOwnerLifecycle;
32+
/**
33+
* Shuts every provider down, each runtime and then its plugin's own cleanup. The composition owns
34+
* what a provider is made of, so the server calls this once per shutdown path and never reaches
35+
* into a runtime.
36+
*/
37+
shutdownProviderRuntimes: () => Promise<readonly PluginCleanupOutcome[]>;
38+
/**
39+
* Every provider plugin the composition saw, active or failed. Data only: this server publishes
40+
* it in the daemon registration and records each failed entry as a `provider_plugin_failed`
41+
* diagnostic, so log emission stays in this module.
42+
*/
43+
pluginInventory: readonly PluginInventoryEntry[];
44+
interactorResolution: InteractorResolution;
45+
providerDeviceAdmission: ProviderDeviceAdmission;
46+
cleanupManagedWebOrphans: (
47+
params: Readonly<{
48+
stateDir: string;
49+
openWebSessionNames: readonly string[];
50+
ownedProcessRecords?: OwnedProcessRecordStore;
51+
}>,
52+
) => Promise<void>;
53+
leaseLifecycleProvider?: LeaseLifecycleProvider;
54+
providerRuntimeIds: readonly string[];
55+
recoverExpiredLease?: ProviderExpiredLeaseRecovery;
56+
recoverableProviderIds: readonly string[];
57+
/** The runtime gateway every lifecycle and recovery step in this server runs through. */
58+
deviceRuntimeGateway: RequestRouterDeps['deviceRuntimeGateway'];
59+
/** The resource-cleanup surface the session teardowns run through. */
60+
platformResourceCleanup: RequestRouterDeps['platformResourceCleanup'];
61+
/** The owner-scoped gateway factory the startup claim reconciliation composes. */
62+
claimRecoveryGateway: ClaimRecoveryGatewayFactory;
63+
/**
64+
* The root-composed half of `createRequestHandler`'s inputs; the server adds the rest.
65+
* Optionality mirrors `RequestRouterDeps`. Facts the server also reads outside any request —
66+
* the gateway, the cleanup surface, the claim-recovery factory, and the lease-provider facts —
67+
* are declared once at the top level of the deps, not here.
68+
*/
69+
requestRouter: Pick<
70+
RequestRouterDeps,
71+
| 'requestPlatformProviders'
72+
| 'deviceInventoryGateways'
73+
| 'platformServices'
74+
| 'androidObservation'
75+
| 'providerCredentials'
76+
| 'createCommandSurface'
77+
| 'cloudArtifactProvider'
78+
| 'providerAppCatalog'
79+
| 'providerDeviceRuntimeScope'
80+
>;
81+
}>;
82+
83+
/** Everything the composition factory needs that the server owns: paths, policy, and the store. */
84+
export type DaemonRuntimeDepsInput = Readonly<{
85+
/** The environment and working directory this daemon runs with; nothing below reads the process's. */
86+
host: HostEnvironment;
87+
/** The resolved state-dir root; the server resolves daemon paths and passes it down. */
88+
baseDir: string;
89+
sessionsDir: string;
90+
/** Refuses the shutdown capability the operator denied; undefined when no policy names it. */
91+
assertShutdownAllowed: (() => void) | undefined;
92+
resolveSessionDir(sessionId: string): string;
93+
resolveSessionArtifacts(sessionId: string): AppLogSessionArtifacts;
94+
openWebSessionNames(): readonly string[];
95+
}>;
96+
97+
/**
98+
* The factory that names this daemon's runtimes. The server calls it once, after resolving its
99+
* paths and loading the operator's policy, and before it acquires the registration; the process
100+
* root's implementation is `createDaemonRuntimeDeps`.
101+
*/
102+
export type ComposeDaemonRuntimeDeps = (
103+
input: DaemonRuntimeDepsInput,
104+
) => Promise<DaemonRuntimeDeps>;

‎packages/daemon/src/server/http/daemon-runtime.ts‎

Lines changed: 4 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import type { ComposeDaemonRuntimeDeps } from './daemon-runtime-deps.ts';
12
import { recoverAppLogResourcesAfterDaemonLock } from '@agent-device/capture-kit/app-log-resource-recovery';
23
import { resolveSessionRequestLogPath } from '@agent-device/daemon-contracts/session-artifact-paths';
34
import {
45
createOwnerScopedDeviceClaimReconciler,
56
createRecoveryPlatformScope,
6-
type ClaimRecoveryGatewayFactory,
77
} from '@agent-device/daemon-contracts/device-claim-recovery';
88
import crypto from 'node:crypto';
99
import { asAppError, AppError, normalizeError } from '@agent-device/kernel/errors';
@@ -15,25 +15,19 @@ import {
1515
type DaemonRuntimeOwnership,
1616
} from '@agent-device/daemon-contracts/daemon-resolution';
1717
import { trackDownloadableArtifact } from '../../resources/artifact-tracking.ts';
18-
import {
19-
installProviderDeviceAdmission,
20-
type ProviderDeviceAdmission,
21-
} from '../../core/provider-device-admission.ts';
18+
import { installProviderDeviceAdmission } from '../../core/provider-device-admission.ts';
2219
import {
2320
loadDaemonPolicy,
2421
type DaemonPolicy,
2522
} from '@agent-device/daemon-contracts/daemon-policy-file';
2623
import { assertDaemonPolicyAllowsCapability } from '../../core/daemon-policy.ts';
27-
import {
28-
installInteractorResolution,
29-
type InteractorResolution,
30-
} from '../../core/interactor-resolution.ts';
24+
import { installInteractorResolution } from '../../core/interactor-resolution.ts';
3125
import { LeaseRegistry } from '../../resources/lease-registry.ts';
3226
import {
3327
createExpiredProviderLeaseReleaser,
3428
leaseReleaseDiagnosticEvent,
3529
} from '../provider-lease-expiry.ts';
36-
import { createRequestHandler, type RequestRouterDeps } from '../request-router.ts';
30+
import { createRequestHandler } from '../request-router.ts';
3731
import {
3832
createLeaseSessionEnd,
3933
getLeaseRegistryExecutionLocks,
@@ -81,7 +75,6 @@ import type { HostEnvironment } from '@agent-device/kernel/source-value';
8175
import {
8276
readCurrentOwnerIdentity,
8377
reapOwnedProcessRecordsAtStartup,
84-
type OwnedProcessRecordStore,
8578
type OwnerIdentity,
8679
} from '@agent-device/host-kit/process';
8780
import { isEnvTruthy, sleep } from '@agent-device/host-kit/retry';
@@ -104,17 +97,6 @@ import {
10497
import { retainPngWorker } from '@agent-device/capture-kit/png-worker-client';
10598
import { openWebSessionNames } from '../../sessions/web-session-names.ts';
10699
import { createAppLogAdmissionLedger } from '../../resources/app-log-admission-ledger.ts';
107-
import type { AppLogSessionArtifacts } from '@agent-device/contracts/app-log-runtime';
108-
import type { PlatformOwnerLifecycle } from '../../core/platform-owner-lifecycle.ts';
109-
import type {
110-
LeaseLifecycleProvider,
111-
ProviderExpiredLeaseRecovery,
112-
} from '@agent-device/contracts/device';
113-
import type { HostDiagnostics } from '@agent-device/contracts/host-diagnostics';
114-
import type {
115-
PluginCleanupOutcome,
116-
PluginInventoryEntry,
117-
} from '@agent-device/contracts/plugin-inventory';
118100

119101
const DAEMON_LEASE_RELEASE_TIMEOUT_MS = 1_000;
120102
const LEASE_EXPIRY_SWEEP_INTERVAL_MS = 1_000;
@@ -129,93 +111,6 @@ const DAEMON_REQUEST_DRAIN_TIMEOUT_MS = 5_000;
129111
// (#2170). Bound the grace to the recorder purpose, so daemon startup stays under the client budget.
130112
const DAEMON_RECORDING_REAP_TERM_TIMEOUT_MS = 5_000;
131113

132-
/**
133-
* Every capability the daemon runtime cannot construct for itself: the platform and provider
134-
* runtimes, host diagnostics, credential fingerprinting, resource cleanup, the platform lifecycle
135-
* owners, and the request-router inputs that name them. The server owns the paths, the policy, and
136-
* the session store, and asks the root for the rest through a `composeDeps` factory — the process
137-
* root's implementation is `src/daemon-composition.ts` (ADR 0019). All members are required: a
138-
* daemon process either receives the root's composition or cannot start, and a test passes the
139-
* same factory, so the production assembly is the one every runtime test boots.
140-
*/
141-
export type DaemonRuntimeDeps = Readonly<{
142-
ownedProcessRecords: OwnedProcessRecordStore;
143-
hostDiagnostics: HostDiagnostics;
144-
ownerLifecycle: PlatformOwnerLifecycle;
145-
/**
146-
* Shuts every provider down, each runtime and then its plugin's own cleanup. The composition owns
147-
* what a provider is made of, so the server calls this once per shutdown path and never reaches
148-
* into a runtime.
149-
*/
150-
shutdownProviderRuntimes: () => Promise<readonly PluginCleanupOutcome[]>;
151-
/**
152-
* Every provider plugin the composition saw, active or failed. Data only: this server publishes
153-
* it in the daemon registration and records each failed entry as a `provider_plugin_failed`
154-
* diagnostic, so log emission stays in this module.
155-
*/
156-
pluginInventory: readonly PluginInventoryEntry[];
157-
interactorResolution: InteractorResolution;
158-
providerDeviceAdmission: ProviderDeviceAdmission;
159-
cleanupManagedWebOrphans: (
160-
params: Readonly<{
161-
stateDir: string;
162-
openWebSessionNames: readonly string[];
163-
ownedProcessRecords?: OwnedProcessRecordStore;
164-
}>,
165-
) => Promise<void>;
166-
leaseLifecycleProvider?: LeaseLifecycleProvider;
167-
providerRuntimeIds: readonly string[];
168-
recoverExpiredLease?: ProviderExpiredLeaseRecovery;
169-
recoverableProviderIds: readonly string[];
170-
/** The runtime gateway every lifecycle and recovery step in this server runs through. */
171-
deviceRuntimeGateway: RequestRouterDeps['deviceRuntimeGateway'];
172-
/** The resource-cleanup surface the session teardowns run through. */
173-
platformResourceCleanup: RequestRouterDeps['platformResourceCleanup'];
174-
/** The owner-scoped gateway factory the startup claim reconciliation composes. */
175-
claimRecoveryGateway: ClaimRecoveryGatewayFactory;
176-
/**
177-
* The root-composed half of `createRequestHandler`'s inputs; the server adds the rest.
178-
* Optionality mirrors `RequestRouterDeps`. Facts the server also reads outside any request —
179-
* the gateway, the cleanup surface, the claim-recovery factory, and the lease-provider facts —
180-
* are declared once at the top level of the deps, not here.
181-
*/
182-
requestRouter: Pick<
183-
RequestRouterDeps,
184-
| 'requestPlatformProviders'
185-
| 'deviceInventoryGateways'
186-
| 'platformServices'
187-
| 'androidObservation'
188-
| 'providerCredentials'
189-
| 'createCommandSurface'
190-
| 'cloudArtifactProvider'
191-
| 'providerAppCatalog'
192-
| 'providerDeviceRuntimeScope'
193-
>;
194-
}>;
195-
196-
/** Everything the composition factory needs that the server owns: paths, policy, and the store. */
197-
export type DaemonRuntimeDepsInput = Readonly<{
198-
/** The environment and working directory this daemon runs with; nothing below reads the process's. */
199-
host: HostEnvironment;
200-
/** The resolved state-dir root; the server resolves daemon paths and passes it down. */
201-
baseDir: string;
202-
sessionsDir: string;
203-
/** Refuses the shutdown capability the operator denied; undefined when no policy names it. */
204-
assertShutdownAllowed: (() => void) | undefined;
205-
resolveSessionDir(sessionId: string): string;
206-
resolveSessionArtifacts(sessionId: string): AppLogSessionArtifacts;
207-
openWebSessionNames(): readonly string[];
208-
}>;
209-
210-
/**
211-
* The factory that names this daemon's runtimes. The server calls it once, after resolving its
212-
* paths and loading the operator's policy, and before it acquires the registration; the process
213-
* root's implementation is `createDaemonRuntimeDeps`.
214-
*/
215-
export type ComposeDaemonRuntimeDeps = (
216-
input: DaemonRuntimeDepsInput,
217-
) => Promise<DaemonRuntimeDeps>;
218-
219114
export type DaemonRuntimeOptions = {
220115
host: HostEnvironment;
221116
stdout?: WritableOutput;

0 commit comments

Comments
 (0)