|
| 1 | +// The daemon runtime's dependency construction, extracted from `src/daemon/server/daemon-runtime.ts` |
| 2 | +// so the operational server owns sequencing and this module owns naming runtimes (ADR 0019). |
| 3 | +// Everything here that names a platform runtime, a provider runtime, an interactor, host |
| 4 | +// diagnostics, credential fingerprinting, resource cleanup, the platform lifecycle owners, the |
| 5 | +// platform-services composition, or the claim-recovery gateway lives in the process root; |
| 6 | +// `src/daemon.ts` calls `createDaemonRuntimeDeps` and hands the result to `startDaemonRuntime`. |
| 7 | +// |
| 8 | +// The static import set is exactly the family `daemon-runtime.ts` already evaluated eagerly, so |
| 9 | +// the daemon entry's eager closure does not grow; the platform-services and claim-recovery |
| 10 | +// compositions stay behind function-scoped `await import`, and the command surface loads on first |
| 11 | +// request use. Nothing here value-imports `src/daemon/`: the daemon's session data enters through |
| 12 | +// plain callbacks, so the platform-runtime zone keeps its value-free direction toward the daemon. |
| 13 | + |
| 14 | +import { |
| 15 | + createProviderDeviceRuntimeRequestProviders, |
| 16 | + isActiveProviderDevice, |
| 17 | +} from './provider-device-runtime.ts'; |
| 18 | +import { |
| 19 | + createDaemonProviderRuntimeComposition, |
| 20 | + DEFAULT_PROVIDER_RUNTIME_REQUIRED_IDS, |
| 21 | +} from './provider-device-runtimes.ts'; |
| 22 | +import { readDaemonProviderCredentials } from './provider-credential-fingerprint.ts'; |
| 23 | +import { getInteractor } from './core/interactors.ts'; |
| 24 | +import { |
| 25 | + androidObservation, |
| 26 | + createPlatformRuntimeGateway, |
| 27 | + createPlatformDeviceInventoryGateways, |
| 28 | + createRequestPlatformProviders, |
| 29 | +} from './platform-runtime.ts'; |
| 30 | +import { createHostDiagnostics } from './platform-runtime-host-diagnostics.ts'; |
| 31 | +import { platformResourceCleanup } from './platform-runtime-resource-cleanup.ts'; |
| 32 | +import { platformDaemonLifecycleOwners } from './platform-runtime-daemon-lifecycle.ts'; |
| 33 | +import { |
| 34 | + createOwnedProcessRecordStore, |
| 35 | + type OwnedProcessRecordStore, |
| 36 | +} from '@agent-device/host-kit/process'; |
| 37 | +import { emitDiagnostic } from '@agent-device/host-kit/diagnostics'; |
| 38 | +import { withDaemonDiagnosticsScope } from '@agent-device/daemon-contracts/daemon-diagnostics-scope'; |
| 39 | +import type { AppLogSessionArtifacts } from '@agent-device/contracts/app-log-runtime'; |
| 40 | +import type { AgentDeviceRuntimeConfig } from '@agent-device/contracts/runtime-contract'; |
| 41 | +import type { DeviceInfo } from '@agent-device/kernel/device'; |
| 42 | +import type { DaemonPolicy } from '@agent-device/daemon-contracts/daemon-policy-file'; |
| 43 | +import type { DaemonRuntimeDeps } from './daemon/server/daemon-runtime.ts'; |
| 44 | + |
| 45 | +/** |
| 46 | + * Best-effort startup cleanup of managed-agent-browser orphans left by a previous daemon. It runs |
| 47 | + * only after this process holds the daemon lock, and it never fails startup: a cleanup the host |
| 48 | + * refuses is a diagnostic, not a reason to keep the next daemon from serving. |
| 49 | + */ |
| 50 | +export async function cleanupWebBrowserOrphansForDaemonStartup(params: { |
| 51 | + stateDir: string; |
| 52 | + openWebSessionNames: readonly string[]; |
| 53 | + ownedProcessRecords?: OwnedProcessRecordStore; |
| 54 | +}): Promise<void> { |
| 55 | + try { |
| 56 | + await platformDaemonLifecycleOwners.cleanupManagedWebOrphans(params); |
| 57 | + } catch (error) { |
| 58 | + emitDiagnostic({ |
| 59 | + level: 'warn', |
| 60 | + phase: 'web_agent_browser_orphan_cleanup_failed', |
| 61 | + data: { error: error instanceof Error ? error.message : String(error) }, |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Builds every capability the daemon runtime cannot construct for itself, in the order the |
| 68 | + * runtime used to build them. All members are required: a daemon process either receives this |
| 69 | + * composition or cannot start, and a test passes the same shared fixture (ADR 0019). The |
| 70 | + * session-scoped callbacks are the daemon's own data seen as questions — recording artifact |
| 71 | + * paths, session directories, and which web sessions are open — so no daemon value crosses here. |
| 72 | + */ |
| 73 | +export async function createDaemonRuntimeDeps( |
| 74 | + input: Readonly<{ |
| 75 | + env: NodeJS.ProcessEnv; |
| 76 | + /** Resolved daemon paths' state-dir root; the daemon owns path resolution and passes it down. */ |
| 77 | + baseDir: string; |
| 78 | + sessionsDir: string; |
| 79 | + /** ADR 0029: the operator's policy, loaded by the executable so a bad policy stops startup. */ |
| 80 | + daemonPolicy: DaemonPolicy | undefined; |
| 81 | + /** Refuses the shutdown capability the operator denied; undefined when no policy names it. */ |
| 82 | + assertShutdownAllowed: (() => void) | undefined; |
| 83 | + resolveSessionDir(sessionId: string): string; |
| 84 | + resolveSessionArtifacts(sessionId: string): AppLogSessionArtifacts; |
| 85 | + openWebSessionNames(): readonly string[]; |
| 86 | + }>, |
| 87 | +): Promise<DaemonRuntimeDeps> { |
| 88 | + const { |
| 89 | + env, |
| 90 | + baseDir, |
| 91 | + sessionsDir, |
| 92 | + daemonPolicy, |
| 93 | + assertShutdownAllowed, |
| 94 | + resolveSessionDir, |
| 95 | + resolveSessionArtifacts, |
| 96 | + openWebSessionNames, |
| 97 | + } = input; |
| 98 | + const ownedProcessRecords = createOwnedProcessRecordStore({ |
| 99 | + stateDir: baseDir, |
| 100 | + sessionsDir, |
| 101 | + resolveSessionDir, |
| 102 | + }); |
| 103 | + const hostDiagnostics = createHostDiagnostics(); |
| 104 | + const providerComposition = await createDaemonProviderRuntimeComposition(env); |
| 105 | + const providerDeviceRuntimes = [...providerComposition.runtimes]; |
| 106 | + const deviceRuntimeGateway = createPlatformRuntimeGateway({ |
| 107 | + assertShutdownAllowed, |
| 108 | + providerRuntimes: providerDeviceRuntimes, |
| 109 | + providerModules: providerComposition.platformModules, |
| 110 | + sessionsDir, |
| 111 | + ownedProcesses: ownedProcessRecords, |
| 112 | + resolveSessionArtifacts, |
| 113 | + }); |
| 114 | + const providerRuntimeProviders = createProviderDeviceRuntimeRequestProviders( |
| 115 | + providerDeviceRuntimes, |
| 116 | + { providerRuntimeRequiredIds: DEFAULT_PROVIDER_RUNTIME_REQUIRED_IDS }, |
| 117 | + ); |
| 118 | + const requestPlatformProviders = createRequestPlatformProviders({ |
| 119 | + providers: { |
| 120 | + appleRunnerProvider: providerRuntimeProviders.appleRunnerProvider, |
| 121 | + appleRunnerScreenRecordingTransport: |
| 122 | + providerRuntimeProviders.appleRunnerScreenRecordingTransport, |
| 123 | + }, |
| 124 | + defaultWebProvider: { |
| 125 | + stateDir: baseDir, |
| 126 | + openWebSessionNames, |
| 127 | + ownedProcessRecords, |
| 128 | + }, |
| 129 | + }); |
| 130 | + const deviceInventoryGateways = createPlatformDeviceInventoryGateways( |
| 131 | + providerRuntimeProviders.deviceInventorySource, |
| 132 | + ); |
| 133 | + // Root composition of the platform-services port and the owner-scoped claim-recovery gateway. |
| 134 | + // platform-runtime.ts is already static in this module, so the deferred imports keep these thin |
| 135 | + // composition modules (and the readiness/open-target adapters only the services module pulls) |
| 136 | + // off `src/daemon.ts`'s eager closure; what each adapter itself loads stays as lazy as it was. |
| 137 | + const { createDaemonPlatformServices } = await import('./platform-runtime-daemon-services.ts'); |
| 138 | + const { createClaimRecoveryGateway } = await import('./platform-runtime-claim-recovery.ts'); |
| 139 | + |
| 140 | + return Object.freeze({ |
| 141 | + daemonPolicy, |
| 142 | + ownedProcessRecords, |
| 143 | + hostDiagnostics, |
| 144 | + ownerLifecycle: platformDaemonLifecycleOwners, |
| 145 | + providerDeviceRuntimes, |
| 146 | + interactorResolution: Object.freeze({ resolve: getInteractor }), |
| 147 | + providerDeviceAdmission: Object.freeze({ |
| 148 | + isActive: (device: DeviceInfo) => isActiveProviderDevice(device), |
| 149 | + }), |
| 150 | + cleanupManagedWebOrphans: cleanupWebBrowserOrphansForDaemonStartup, |
| 151 | + notifySkippedProviderRuntimes: async (logPath: string) => { |
| 152 | + // Outside any request, so debug is forced on for the same reason the #2681 handoff forces |
| 153 | + // it — the event is the point of the record and must not be dropped by a level that was |
| 154 | + // never set for it. |
| 155 | + for (const { provider, error } of providerComposition.skipped ?? []) { |
| 156 | + await withDaemonDiagnosticsScope({ logPath }, () => { |
| 157 | + emitDiagnostic({ |
| 158 | + level: 'warn', |
| 159 | + phase: 'provider_runtime_skipped', |
| 160 | + data: { |
| 161 | + provider, |
| 162 | + code: error.code, |
| 163 | + message: error.message, |
| 164 | + hint: error.details?.hint, |
| 165 | + }, |
| 166 | + }); |
| 167 | + }); |
| 168 | + } |
| 169 | + }, |
| 170 | + providerRuntimeProviders: Object.freeze({ |
| 171 | + leaseLifecycleProvider: providerRuntimeProviders.leaseLifecycleProvider, |
| 172 | + providerRuntimeIds: providerRuntimeProviders.providerRuntimeIds, |
| 173 | + recoverExpiredLease: providerRuntimeProviders.recoverExpiredLease, |
| 174 | + recoverableProviderIds: providerRuntimeProviders.recoverableProviderIds, |
| 175 | + }), |
| 176 | + requestRouter: Object.freeze({ |
| 177 | + requestPlatformProviders, |
| 178 | + deviceInventoryGateways, |
| 179 | + deviceRuntimeGateway, |
| 180 | + platformServices: createDaemonPlatformServices(), |
| 181 | + claimRecoveryGateway: createClaimRecoveryGateway, |
| 182 | + androidObservation, |
| 183 | + platformResourceCleanup, |
| 184 | + providerCredentials: readDaemonProviderCredentials(env, baseDir), |
| 185 | + createCommandSurface: async (config: AgentDeviceRuntimeConfig) => |
| 186 | + ( |
| 187 | + await import('./command-runtime/runtime-command-surface.ts') |
| 188 | + ).createCommandSurfaceAgentDevice(config), |
| 189 | + cloudArtifactProvider: providerRuntimeProviders.cloudArtifactProvider, |
| 190 | + providerAppCatalog: providerRuntimeProviders.providerAppCatalog, |
| 191 | + leaseLifecycleProvider: providerRuntimeProviders.leaseLifecycleProvider, |
| 192 | + providerRuntimeIds: providerRuntimeProviders.providerRuntimeIds, |
| 193 | + providerRuntimeRequiredIds: providerRuntimeProviders.providerRuntimeRequiredIds, |
| 194 | + providerDeviceRuntimeScope: providerRuntimeProviders.providerDeviceRuntimeScope, |
| 195 | + }), |
| 196 | + }); |
| 197 | +} |
0 commit comments