Repository navigation
Expand file tree
/
Copy pathconfig-assembly.ts
More file actions
160 lines (155 loc) · 6.42 KB
/
Copy pathconfig-assembly.ts
File metadata and controls
160 lines (155 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { WrapperConfig } from '../../types';
import { applyAgentTimeout } from '../../option-parsers';
import { logger } from '../../logger';
import { buildConfig } from '../build-config';
import { LogAndLimitsResult } from './log-and-limits';
import { NetworkOptionsResult } from './network-options';
import { AgentOptionsResult } from './agent-options';
import { validateInfrastructureOptions, applyRateLimitConfig, validateFeatureFlagCompatibility } from './infrastructure-validator';
import { applySecurityMode } from './security-mode';
import { validateHostAccessConfig } from './network-access-validator';
import { validateApiProxyOptions, validateCopilotModelOption } from './api-proxy-validator';
import {
assertCloudHypervisorPreSecurityCompatibility,
assertCloudHypervisorRuntimeCompatibility,
assertCloudHypervisorSelection,
isPrimaryCloudHypervisorRuntime,
requiresCloudHypervisorInfrastructure,
usesCloudHypervisorEnclaveRuntime,
} from '../../cloud-hypervisor/runtime-validation';
import {
assertNvxRuntimeCompatibility,
assertNvxSelection,
isPrimaryNvxRuntime,
} from '../../nvx/runtime-validation';
import { assertProtectedCommandOutputCaptureSupported } from '../../protected-command-output-capture';
import { assertFilesystemWritePolicyCompatibility } from '../../filesystem-policy';
import {
formatCloudHypervisorDockerFallbackWarning,
isCloudHypervisorUnsupportedHostError,
} from '../../cloud-hypervisor/errors';
function fallBackCloudHypervisorToDocker(config: WrapperConfig, error: unknown): void {
logger.warn(formatCloudHypervisorDockerFallbackWarning(error));
config.containerRuntime = undefined;
config.cloudHypervisor = undefined;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
* Assembles the {@link WrapperConfig} from pre-validated partial results and
* runs all post-assembly validation guards.
*
* This is the final stage of the validation pipeline. Every input must
* already be validated by the earlier stages; this function only:
* 1. Calls {@link buildConfig} to merge everything into a single object.
* 2. Runs post-config guards that require the fully-assembled config (docker
* host URI format, rate limits, feature-flag compatibility, port rules,
* API-proxy configuration warnings).
*
* Calls `process.exit(1)` on any validation failure so the caller always
* receives a fully-validated, ready-to-use config object.
*/
export function assembleAndValidateConfig(
options: Record<string, unknown>,
agentCommand: string,
logAndLimits: LogAndLimitsResult,
networkOptions: NetworkOptionsResult,
agentOptions: AgentOptionsResult,
): WrapperConfig {
const config = buildConfig({
options,
agentCommand,
logLevel: logAndLimits.logLevel,
allowedDomains: networkOptions.allowedDomains,
sensitiveAllowedDomains: networkOptions.sensitiveAllowedDomains,
blockedDomains: networkOptions.blockedDomains,
localhostDetected: networkOptions.localhostResult.localhostDetected,
additionalEnv: agentOptions.additionalEnv,
volumeMounts: agentOptions.volumeMounts,
upstreamProxy: networkOptions.upstreamProxy,
dnsServers: networkOptions.dnsServers,
dnsServersExplicit: networkOptions.dnsServersExplicit,
dnsOverHttps: networkOptions.dnsOverHttps,
networkSubnet: networkOptions.networkSubnet,
allowedUrls: agentOptions.allowedUrls,
memoryLimit: logAndLimits.memoryLimit,
pidsLimit: logAndLimits.pidsLimit,
agentImage: logAndLimits.agentImage,
modelAliases: logAndLimits.modelAliases,
allowedModels: logAndLimits.allowedModels,
disallowedModels: logAndLimits.disallowedModels,
maxAiCredits: logAndLimits.maxAiCredits,
modelMultipliers: logAndLimits.modelMultipliers,
defaultModelMultiplier: logAndLimits.defaultModelMultiplier,
maxModelMultiplierCap: logAndLimits.maxModelMultiplierCap,
maxRuns: logAndLimits.maxRuns,
maxPermissionDenied: logAndLimits.maxPermissionDenied,
maxCacheMisses: logAndLimits.maxCacheMisses,
resolvedCopilotApiTarget: networkOptions.resolvedCopilotApiTarget,
resolvedCopilotApiBasePath: networkOptions.resolvedCopilotApiBasePath,
dockerHostPathPrefix: networkOptions.dockerHostPathPrefixResolution.dockerHostPathPrefix,
});
try {
assertProtectedCommandOutputCaptureSupported(config);
} catch (error) {
logger.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
validateInfrastructureOptions(config);
try {
assertCloudHypervisorSelection(config);
assertNvxSelection(config);
} catch (error) {
logger.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
if (requiresCloudHypervisorInfrastructure(config)) {
try {
assertCloudHypervisorPreSecurityCompatibility(config);
} catch (error) {
logger.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
}
applySecurityMode(config);
try {
assertFilesystemWritePolicyCompatibility(config);
} catch (error) {
logger.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
if (requiresCloudHypervisorInfrastructure(config)) {
try {
assertCloudHypervisorRuntimeCompatibility(config);
} catch (error) {
if (
isCloudHypervisorUnsupportedHostError(error)
&& isPrimaryCloudHypervisorRuntime(config)
&& !usesCloudHypervisorEnclaveRuntime(config)
) {
fallBackCloudHypervisorToDocker(config, error);
} else {
logger.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
}
}
if (isPrimaryNvxRuntime(config)) {
// NVX never falls back to another runtime: any validation failure here
// (missing preview flag, unsupported host, invalid artifacts) is fatal.
try {
assertNvxRuntimeCompatibility(config);
} catch (error) {
logger.error(`❌ ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
}
}
applyAgentTimeout(options.agentTimeout as string | undefined, config, logger);
applyRateLimitConfig(config, options);
validateFeatureFlagCompatibility(config);
validateHostAccessConfig(config, networkOptions);
validateApiProxyOptions(config, networkOptions);
validateCopilotModelOption(config, agentOptions);
return config;
}