runTask.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import * as fs from "fs"
  2. import * as path from "path"
  3. import * as os from "node:os"
  4. import pWaitFor from "p-wait-for"
  5. import { execa } from "execa"
  6. import {
  7. type TaskEvent,
  8. TaskCommandName,
  9. RooCodeEventName,
  10. IpcMessageType,
  11. EVALS_SETTINGS,
  12. EVALS_TIMEOUT,
  13. } from "@roo-code/types"
  14. import { IpcClient } from "@roo-code/ipc"
  15. import { type Run, type Task, updateTask, createTaskMetrics, updateTaskMetrics, createToolError } from "../db/index.js"
  16. import { exercisesPath } from "../exercises/index.js"
  17. import { isDockerContainer } from "./utils.js"
  18. import { FileLogger } from "./FileLogger.js"
  19. class SubprocessTimeoutError extends Error {
  20. constructor(timeout: number) {
  21. super(`Subprocess timeout after ${timeout}ms`)
  22. this.name = "SubprocessTimeoutError"
  23. }
  24. }
  25. type RunTaskOptions = {
  26. run: Run
  27. task: Task
  28. publish: (taskEvent: TaskEvent) => Promise<void>
  29. logger: FileLogger
  30. }
  31. export const runTask = async ({ run, task, publish, logger }: RunTaskOptions) => {
  32. const { language, exercise } = task
  33. const prompt = fs.readFileSync(path.resolve(exercisesPath, `prompts/${language}.md`), "utf-8")
  34. const workspacePath = path.resolve(exercisesPath, language, exercise)
  35. const ipcSocketPath = path.resolve(os.tmpdir(), `evals-${run.id}-${task.id}.sock`)
  36. const env = { ROO_CODE_IPC_SOCKET_PATH: ipcSocketPath }
  37. const controller = new AbortController()
  38. const cancelSignal = controller.signal
  39. const containerized = isDockerContainer()
  40. const codeCommand = containerized
  41. ? `xvfb-run --auto-servernum --server-num=1 code --wait --log trace --disable-workspace-trust --disable-gpu --disable-lcd-text --no-sandbox --user-data-dir /roo/.vscode --password-store="basic" -n ${workspacePath}`
  42. : `code --disable-workspace-trust -n ${workspacePath}`
  43. logger.info(codeCommand)
  44. // Sleep for a random amount of time between 5 and 10 seconds, unless we're
  45. // running in a container, in which case there are no issues with flooding
  46. // VSCode with new windows.
  47. if (!containerized) {
  48. await new Promise((resolve) => setTimeout(resolve, Math.random() * 5_000 + 5_000))
  49. }
  50. const subprocess = execa({ env, shell: "/bin/bash", cancelSignal })`${codeCommand}`
  51. // If debugging, add `--verbose` to `command` and uncomment the following line.
  52. // subprocess.stdout.pipe(process.stdout)
  53. // Give VSCode some time to spawn before connecting to its unix socket.
  54. await new Promise((resolve) => setTimeout(resolve, 3_000))
  55. let client: IpcClient | undefined = undefined
  56. let attempts = 5
  57. while (true) {
  58. try {
  59. client = new IpcClient(ipcSocketPath)
  60. await pWaitFor(() => client!.isReady, { interval: 250, timeout: 1_000 })
  61. break
  62. } catch (_error) {
  63. client?.disconnect()
  64. attempts--
  65. if (attempts <= 0) {
  66. logger.error(`unable to connect to IPC socket -> ${ipcSocketPath}`)
  67. throw new Error("Unable to connect.")
  68. }
  69. }
  70. }
  71. let taskStartedAt = Date.now()
  72. let taskFinishedAt: number | undefined
  73. let taskMetricsId: number | undefined
  74. let rooTaskId: string | undefined
  75. let isClientDisconnected = false
  76. const ignoreEvents: Record<"broadcast" | "log", RooCodeEventName[]> = {
  77. broadcast: [RooCodeEventName.Message],
  78. log: [RooCodeEventName.TaskTokenUsageUpdated, RooCodeEventName.TaskAskResponded],
  79. }
  80. client.on(IpcMessageType.TaskEvent, async (taskEvent) => {
  81. const { eventName, payload } = taskEvent
  82. // Publish all events except for these to Redis.
  83. if (!ignoreEvents.broadcast.includes(eventName)) {
  84. await publish({ ...taskEvent, taskId: task.id })
  85. }
  86. // Log all events except for these.
  87. // For message events we only log non-partial messages.
  88. if (
  89. !ignoreEvents.log.includes(eventName) &&
  90. (eventName !== RooCodeEventName.Message || payload[0].message.partial !== true)
  91. ) {
  92. logger.info(`${eventName} ->`, payload)
  93. }
  94. if (eventName === RooCodeEventName.TaskStarted) {
  95. taskStartedAt = Date.now()
  96. const taskMetrics = await createTaskMetrics({
  97. cost: 0,
  98. tokensIn: 0,
  99. tokensOut: 0,
  100. tokensContext: 0,
  101. duration: 0,
  102. cacheWrites: 0,
  103. cacheReads: 0,
  104. })
  105. await updateTask(task.id, { taskMetricsId: taskMetrics.id, startedAt: new Date() })
  106. taskStartedAt = Date.now()
  107. taskMetricsId = taskMetrics.id
  108. rooTaskId = payload[0]
  109. }
  110. if (eventName === RooCodeEventName.TaskToolFailed) {
  111. const [_taskId, toolName, error] = payload
  112. await createToolError({ taskId: task.id, toolName, error })
  113. }
  114. if (
  115. (eventName === RooCodeEventName.TaskTokenUsageUpdated || eventName === RooCodeEventName.TaskCompleted) &&
  116. taskMetricsId
  117. ) {
  118. const duration = Date.now() - taskStartedAt
  119. const { totalCost, totalTokensIn, totalTokensOut, contextTokens, totalCacheWrites, totalCacheReads } =
  120. payload[1]
  121. await updateTaskMetrics(taskMetricsId, {
  122. cost: totalCost,
  123. tokensIn: totalTokensIn,
  124. tokensOut: totalTokensOut,
  125. tokensContext: contextTokens,
  126. duration,
  127. cacheWrites: totalCacheWrites ?? 0,
  128. cacheReads: totalCacheReads ?? 0,
  129. })
  130. }
  131. if (eventName === RooCodeEventName.TaskCompleted && taskMetricsId) {
  132. const toolUsage = payload[2]
  133. await updateTaskMetrics(taskMetricsId, { toolUsage })
  134. }
  135. if (eventName === RooCodeEventName.TaskAborted || eventName === RooCodeEventName.TaskCompleted) {
  136. taskFinishedAt = Date.now()
  137. await updateTask(task.id, { finishedAt: new Date() })
  138. }
  139. })
  140. client.on(IpcMessageType.Disconnect, async () => {
  141. logger.info(`disconnected from IPC socket -> ${ipcSocketPath}`)
  142. isClientDisconnected = true
  143. })
  144. client.sendCommand({
  145. commandName: TaskCommandName.StartNewTask,
  146. data: {
  147. configuration: {
  148. ...EVALS_SETTINGS,
  149. ...run.settings,
  150. openRouterApiKey: process.env.OPENROUTER_API_KEY,
  151. },
  152. text: prompt,
  153. newTab: true,
  154. },
  155. })
  156. try {
  157. await pWaitFor(() => !!taskFinishedAt || isClientDisconnected, { interval: 1_000, timeout: EVALS_TIMEOUT })
  158. } catch (_error) {
  159. logger.error("time limit reached")
  160. if (rooTaskId && !isClientDisconnected) {
  161. logger.info("cancelling task")
  162. client.sendCommand({ commandName: TaskCommandName.CancelTask, data: rooTaskId })
  163. await new Promise((resolve) => setTimeout(resolve, 5_000)) // Allow some time for the task to cancel.
  164. }
  165. await updateTask(task.id, { finishedAt: new Date() })
  166. }
  167. if (isClientDisconnected) {
  168. logger.error("client disconnected before task finished")
  169. } else {
  170. if (rooTaskId) {
  171. logger.info("closing task")
  172. client.sendCommand({ commandName: TaskCommandName.CloseTask, data: rooTaskId })
  173. await new Promise((resolve) => setTimeout(resolve, 2_000)) // Allow some time for the window to close.
  174. }
  175. client.disconnect()
  176. }
  177. logger.info("waiting for subprocess to finish")
  178. controller.abort()
  179. // Wait for subprocess to finish gracefully, with a timeout.
  180. const SUBPROCESS_TIMEOUT = 10_000
  181. try {
  182. await Promise.race([
  183. subprocess,
  184. new Promise((_, reject) =>
  185. setTimeout(() => reject(new SubprocessTimeoutError(SUBPROCESS_TIMEOUT)), SUBPROCESS_TIMEOUT),
  186. ),
  187. ])
  188. logger.info("subprocess finished gracefully")
  189. } catch (error) {
  190. if (error instanceof SubprocessTimeoutError) {
  191. logger.error("subprocess did not finish within timeout, force killing")
  192. try {
  193. if (subprocess.kill("SIGKILL")) {
  194. logger.info("SIGKILL sent to subprocess")
  195. } else {
  196. logger.error("failed to send SIGKILL to subprocess")
  197. }
  198. } catch (killError) {
  199. logger.error("subprocess.kill(SIGKILL) failed:", killError)
  200. }
  201. } else {
  202. throw error
  203. }
  204. }
  205. logger.close()
  206. }