Skip to content

Commit b493535

Browse files
committed
More progress
1 parent 029f97f commit b493535

File tree

1 file changed

+65
-63
lines changed

1 file changed

+65
-63
lines changed

benchmark/apps/cli/src/index.ts

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { IpcServer, IpcClient } from "@benchmark/ipc"
3333
import { __dirname, extensionDevelopmentPath, exercisesPath } from "./paths.js"
3434
import { getExercises } from "./exercises.js"
3535

36+
const maxConcurrency = 2
3637
const taskTimeLimit = 2 * 60 * 1_000
3738

3839
const testCommands: Record<ExerciseLanguage, { commands: string[]; timeout?: number; cwd?: string }> = {
@@ -44,8 +45,6 @@ const testCommands: Record<ExerciseLanguage, { commands: string[]; timeout?: num
4445
rust: { commands: ["cargo test"] }, // timeout 15s bash -c "cd '$dir' && cargo test > /dev/null 2>&1"
4546
}
4647

47-
let parentPid: number | undefined = undefined
48-
4948
const run = async (toolbox: GluegunToolbox) => {
5049
const { config, prompt } = toolbox
5150

@@ -117,7 +116,6 @@ const run = async (toolbox: GluegunToolbox) => {
117116
// })
118117
// })
119118

120-
const maxConcurrency = 3
121119
const runningPromises: Promise<void>[] = []
122120

123121
const processTask = async (task: Task) => {
@@ -158,15 +156,6 @@ const run = async (toolbox: GluegunToolbox) => {
158156
// console.error(error)
159157
}
160158

161-
if (parentPid) {
162-
try {
163-
console.log(await execa`kill -INT ${parentPid}`)
164-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
165-
} catch (error) {
166-
// console.error(error)
167-
}
168-
}
169-
170159
console.log(await execa({ cwd: exercisesPath })`git add .`)
171160
console.log(await execa({ cwd: exercisesPath })`git commit -m ${`Run #${run.id}`} --no-verify`)
172161
}
@@ -177,37 +166,37 @@ const runExercise = async ({ run, task, server }: { run: Run; task: Task; server
177166
const dirname = path.dirname(run.socketPath)
178167
const taskSocketPath = path.resolve(dirname, `${dirname}/task-${task.id}.sock`)
179168

169+
const controller = new AbortController()
170+
const cancelSignal = controller.signal
171+
180172
// If debugging:
181-
// --log trace
182-
// --verbose
173+
// Use --log trace or --verbose.
183174
let codeCommand = `code --wait --log trace --disable-workspace-trust`
184175

185-
const isDocker = fs.existsSync("/.dockerenv")
186-
187-
if (isDocker) {
188-
codeCommand = `xvfb-run --auto-servernum --server-num=1 ${codeCommand} --password-store="basic"`
176+
if (fs.existsSync("/.dockerenv")) {
177+
codeCommand = `xvfb-run --auto-servernum --server-num=1 ${codeCommand} --disable-gpu --password-store="basic"`
189178
}
190179

191180
const subprocess = execa({
192181
env: {
193182
ROO_CODE_IPC_SOCKET_PATH: taskSocketPath,
194183
},
195184
shell: "/bin/bash",
185+
cancelSignal,
196186
})`${codeCommand} -n ${path.resolve(exercisesPath, language, exercise)}`
197187

198188
// If debugging:
199-
subprocess.stdout.pipe(process.stdout)
189+
// subprocess.stdout.pipe(process.stdout)
200190

201191
// Give VSCode some time to spawn before connectint to its unix socket.
202-
await new Promise((resolve) => setTimeout(resolve, 1_000))
192+
await new Promise((resolve) => setTimeout(resolve, 2_000))
203193
console.log(`Connecting to ${taskSocketPath} (pid: ${subprocess.pid})`)
204194

205195
const createClient = (taskSocketPath: string) => {
206196
const ipcClient = new IpcClient(taskSocketPath)
207197

208198
ipcClient.on(IpcMessageType.Ack, (ack) => {
209199
console.log(`[cli#runExercise | ${language} / ${exercise}] ack`, ack)
210-
parentPid = ack.ppid
211200
})
212201

213202
return ipcClient
@@ -227,24 +216,19 @@ const runExercise = async ({ run, task, server }: { run: Run; task: Task; server
227216
}
228217
}
229218

230-
if (!client.isReady) {
231-
client.disconnect()
232-
console.log(`[cli#runExercise | ${language} / ${exercise}] unable to connect`)
233-
return
234-
}
235-
236219
let isTaskFinished = false
220+
let isClientDisconnected = false
237221

238222
client.on(IpcMessageType.Disconnect, async () => {
239223
console.log(`[cli#runExercise | ${language} / ${exercise}] disconnect`)
240-
// await updateTask(task.id, { finishedAt: new Date() })
241224
isTaskFinished = true
225+
isClientDisconnected = true
242226
})
243227

244228
const ignoreEvents: RooCodeEventName[] = [
245-
// RooCodeEventName.Message,
246-
// RooCodeEventName.TaskTokenUsageUpdated,
247-
// RooCodeEventName.TaskAskResponded,
229+
RooCodeEventName.Message,
230+
RooCodeEventName.TaskTokenUsageUpdated,
231+
RooCodeEventName.TaskAskResponded,
248232
]
249233

250234
let taskStartedAt = Date.now()
@@ -263,7 +247,7 @@ const runExercise = async ({ run, task, server }: { run: Run; task: Task; server
263247

264248
if (!ignoreEvents.includes(eventName)) {
265249
console.log(`[cli#runExercise | ${language} / ${exercise}] taskEvent -> ${eventName}`)
266-
console.log(payload)
250+
// console.log(payload)
267251
}
268252

269253
if (eventName === RooCodeEventName.TaskStarted) {
@@ -312,33 +296,40 @@ const runExercise = async ({ run, task, server }: { run: Run; task: Task; server
312296
}
313297
})
314298

315-
client.sendMessage({
316-
type: IpcMessageType.TaskCommand,
317-
origin: IpcOrigin.Client,
318-
clientId: client.clientId!,
319-
data: {
320-
commandName: TaskCommandName.StartNewTask,
299+
if (client.isReady) {
300+
client.sendMessage({
301+
type: IpcMessageType.TaskCommand,
302+
origin: IpcOrigin.Client,
303+
clientId: client.clientId!,
321304
data: {
322-
configuration: {
323-
...rooCodeDefaults,
324-
openRouterApiKey: process.env.OPENROUTER_API_KEY!,
325-
...run.settings,
305+
commandName: TaskCommandName.StartNewTask,
306+
data: {
307+
configuration: {
308+
...rooCodeDefaults,
309+
openRouterApiKey: process.env.OPENROUTER_API_KEY!,
310+
...run.settings,
311+
},
312+
text: prompt,
313+
newTab: true,
326314
},
327-
text: prompt,
328-
newTab: true,
329315
},
330-
},
331-
})
316+
})
332317

333-
console.log(`[cli#runExercise | ${language} / ${exercise}] starting task`)
318+
console.log(`[cli#runExercise | ${language} / ${exercise}] starting task`)
319+
} else {
320+
console.log(`[cli#runExercise | ${language} / ${exercise}] unable to connect`)
321+
client.disconnect()
322+
isTaskFinished = true
323+
isClientDisconnected = true
324+
}
334325

335326
try {
336327
await pWaitFor(() => isTaskFinished, { interval: 1_000, timeout: taskTimeLimit })
337328
// eslint-disable-next-line @typescript-eslint/no-unused-vars
338329
} catch (error) {
339330
console.log(`[cli#runExercise | ${language} / ${exercise}] time limit reached`)
340331

341-
if (rooTaskId) {
332+
if (rooTaskId && !isClientDisconnected) {
342333
client.sendMessage({
343334
type: IpcMessageType.TaskCommand,
344335
origin: IpcOrigin.Client,
@@ -352,24 +343,35 @@ const runExercise = async ({ run, task, server }: { run: Run; task: Task; server
352343
await updateTask(task.id, { finishedAt: new Date() })
353344
}
354345

355-
try {
356-
client.sendMessage({
357-
type: IpcMessageType.VSCodeCommand,
358-
origin: IpcOrigin.Client,
359-
clientId: client.clientId!,
360-
data: "workbench.action.files.saveFiles",
361-
})
346+
if (!isClientDisconnected) {
347+
try {
348+
client.sendMessage({
349+
type: IpcMessageType.VSCodeCommand,
350+
origin: IpcOrigin.Client,
351+
clientId: client.clientId!,
352+
data: "workbench.action.files.saveFiles",
353+
})
362354

363-
client.sendMessage({
364-
type: IpcMessageType.VSCodeCommand,
365-
origin: IpcOrigin.Client,
366-
clientId: client.clientId!,
367-
data: "workbench.action.closeWindow",
368-
})
355+
client.sendMessage({
356+
type: IpcMessageType.VSCodeCommand,
357+
origin: IpcOrigin.Client,
358+
clientId: client.clientId!,
359+
data: "workbench.action.closeWindow",
360+
})
369361

370-
client.disconnect()
362+
client.disconnect()
363+
} catch (error) {
364+
console.error(error)
365+
}
366+
}
367+
368+
try {
369+
console.log(`[cli#runExercise | ${language} / ${exercise}] aborting subprocess`)
370+
controller.abort()
371+
await subprocess
372+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
371373
} catch (error) {
372-
console.error(error)
374+
// console.error(error)
373375
}
374376
}
375377

0 commit comments

Comments
 (0)