forked from angular/web-codegen-scorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-serve-loop.ts
More file actions
213 lines (199 loc) · 6.17 KB
/
build-serve-loop.ts
File metadata and controls
213 lines (199 loc) · 6.17 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import PQueue from 'p-queue';
import { LlmGenerateFilesResponse } from '../codegen/llm-runner.js';
import { BuildResultStatus } from '../workers/builder/builder-types.js';
import { Environment } from '../configuration/environment.js';
import {
AttemptDetails,
LlmContextFile,
RootPromptDefinition,
} from '../shared-interfaces.js';
import { DEFAULT_MAX_REPAIR_ATTEMPTS } from '../configuration/constants.js';
import { ProgressLogger } from '../progress/progress-logger.js';
import { runBuild } from './build-worker.js';
import { repairAndBuild } from './build-repair.js';
import { EvalID, Gateway } from './gateway.js';
import { serveAndTestApp } from './serve-testing-worker.js';
import { BrowserAgentTaskInput } from '../testing/browser-agent/models.js';
/**
* Attempts to build the code that an LLM generated. If the build fails, attempts
* to fix the breakage and build again.
*
* @param evalID ID of the eval being attempted for build.
* @param gateway Gateway.
* @param model Model to be used for repair generation requests.
* @param env Environment that is currently being run.
* @param rootPromptDef Definition of the root prompt.
* @param directory Directory on disk to which to write.
* @param contextFiles Files that should be passed as context to the LLM.
* @param initialOutputFiles Initial files generated by the LLM.
* @param usage Usage data from the initial LLM run.
* @param attemptDetails Array tracking information about the current build attempt.
* @param skipScreenshots Whether to skip taking screenshots of the app.
* @param skipAxeTesting Whether or not to skip Axe testing of the app.
* @param abortSignal Signal to fire when the build should be aborted.
* @param workerConcurrencyQueue Concurrency queue for controlling parallelism of worker invocations (as they are more expensive than LLM calls).
*/
export async function attemptBuild(
evalID: EvalID,
gateway: Gateway<Environment>,
model: string,
env: Environment,
rootPromptDef: RootPromptDefinition,
directory: string,
contextFiles: LlmContextFile[],
initialResponse: LlmGenerateFilesResponse,
attemptDetails: AttemptDetails[],
abortSignal: AbortSignal,
workerConcurrencyQueue: PQueue,
progress: ProgressLogger,
skipScreenshots: boolean,
skipAxeTesting: boolean,
enableAutoCsp: boolean,
userJourneyAgentTaskInput?: BrowserAgentTaskInput
) {
// Clone the original files, because we're going to mutate them between repair
// attempts and we don't want the different runs to influence each other.
const finalOutputFiles = initialResponse.files.map((file) => ({
...file,
}));
const initialBuildResult = await runBuild(
evalID,
gateway,
directory,
env,
rootPromptDef,
abortSignal,
workerConcurrencyQueue,
progress
);
let repairAttempts = 0;
const maxRepairAttempts = gateway.shouldRetryFailedBuilds(evalID)
? DEFAULT_MAX_REPAIR_ATTEMPTS
: 0;
const initialAttempt = {
outputFiles: initialResponse.files,
usage: {
...{ inputTokens: 0, outputTokens: 0, totalTokens: 0 },
...initialResponse.usage,
},
reasoning: initialResponse.reasoning,
buildResult: initialBuildResult,
serveTestingResult: null,
attempt: 0,
};
attemptDetails.push(initialAttempt);
let lastAttempt: AttemptDetails = initialAttempt;
while (
lastAttempt.buildResult.status !== BuildResultStatus.SUCCESS &&
repairAttempts < maxRepairAttempts
) {
repairAttempts++;
progress.log(
rootPromptDef,
'build',
`Trying to repair app build (attempt #${repairAttempts + 1})`
);
const attempt = await repairAndBuild(
evalID,
gateway,
model,
env,
rootPromptDef,
directory,
finalOutputFiles,
lastAttempt.buildResult.message,
'There are the following build errors:',
contextFiles,
abortSignal,
workerConcurrencyQueue,
repairAttempts,
progress
);
attemptDetails.push(attempt);
lastAttempt = attempt;
}
// Now that we got a working app, try to serve it and collect
// findings from the running app.
lastAttempt.serveTestingResult = await serveAndTestApp(
evalID,
gateway,
directory,
env,
rootPromptDef,
workerConcurrencyQueue,
abortSignal,
progress,
skipScreenshots,
skipAxeTesting,
enableAutoCsp,
userJourneyAgentTaskInput
);
// Attempt to repair axe testing.
let axeRepairAttempts = 0;
while (
lastAttempt.serveTestingResult &&
(lastAttempt.serveTestingResult.axeViolations?.length ?? 0) > 0 &&
axeRepairAttempts < maxRepairAttempts
) {
axeRepairAttempts++;
progress.log(
rootPromptDef,
'build',
`Trying to repair axe accessibility violations (attempt #${axeRepairAttempts + 1})...`
);
const axeViolationsError = JSON.stringify(
lastAttempt.serveTestingResult.axeViolations,
null,
2
);
progress.log(rootPromptDef, 'error', 'Found Axe accessibility violations');
const attempt = await repairAndBuild(
evalID,
gateway,
model,
env,
rootPromptDef,
directory,
finalOutputFiles,
axeViolationsError,
'There are the following accessibility errors from axe accessibility violations:',
contextFiles,
abortSignal,
workerConcurrencyQueue,
axeRepairAttempts + repairAttempts,
progress
);
// Re-run serving & tests after Axe repair.
// This allows us to check if we fixed the violations.
attempt.serveTestingResult = await serveAndTestApp(
evalID,
gateway,
directory,
env,
rootPromptDef,
workerConcurrencyQueue,
abortSignal,
progress,
skipScreenshots,
skipAxeTesting,
enableAutoCsp,
userJourneyAgentTaskInput
);
attemptDetails.push(attempt);
lastAttempt = attempt;
if (attempt.serveTestingResult.axeViolations?.length === 0) {
progress.log(
rootPromptDef,
'success',
`Successfully fixed all Axe accessibility violations`
);
}
}
return {
buildResult: lastAttempt.buildResult,
serveTestingResult: lastAttempt.serveTestingResult,
outputFiles: finalOutputFiles,
repairAttempts,
axeRepairAttempts,
};
}