|
1 | 1 | import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; |
2 | 2 | import { tmpdir } from "node:os"; |
3 | 3 | import path from "node:path"; |
4 | | -import type { ProviderDefinition, Supervisor } from "@coder-studio/core"; |
| 4 | +import { |
| 5 | + DEFAULT_SUPERVISOR_EVALUATION_TIMEOUT_SEC, |
| 6 | + type ProviderDefinition, |
| 7 | + type Supervisor, |
| 8 | +} from "@coder-studio/core"; |
5 | 9 | import type { FastifyBaseLogger } from "fastify"; |
6 | 10 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 11 | +import { closeDatabase, openDatabase } from "../storage/db.js"; |
7 | 12 | import type { ProviderConfigRepo } from "../storage/repositories/provider-config-repo.js"; |
| 13 | +import { SettingsRepo } from "../storage/repositories/settings-repo.js"; |
8 | 14 | import type { SupervisorEvaluationContext } from "./context-builder.js"; |
9 | 15 | import { SupervisorEvaluator } from "./evaluator.js"; |
| 16 | +import { getSupervisorEvaluationTimeoutMs } from "./settings.js"; |
10 | 17 |
|
11 | 18 | function nodeEchoCommand(stdout: string) { |
12 | 19 | return { |
@@ -190,6 +197,90 @@ describe("SupervisorEvaluator", () => { |
190 | 197 | expect(result.message).toBe("proceed with review"); |
191 | 198 | }); |
192 | 199 |
|
| 200 | + it("uses the shared 600-second default timeout when the setting is missing", () => { |
| 201 | + expect(getSupervisorEvaluationTimeoutMs()).toBe( |
| 202 | + DEFAULT_SUPERVISOR_EVALUATION_TIMEOUT_SEC * 1000 |
| 203 | + ); |
| 204 | + expect( |
| 205 | + getSupervisorEvaluationTimeoutMs({ |
| 206 | + get: vi.fn(() => undefined), |
| 207 | + } as never) |
| 208 | + ).toBe(DEFAULT_SUPERVISOR_EVALUATION_TIMEOUT_SEC * 1000); |
| 209 | + }); |
| 210 | + |
| 211 | + it("uses the stored supervisor timeout setting when available", () => { |
| 212 | + expect( |
| 213 | + getSupervisorEvaluationTimeoutMs({ |
| 214 | + get: vi.fn(() => 900), |
| 215 | + } as never) |
| 216 | + ).toBe(900_000); |
| 217 | + }); |
| 218 | + |
| 219 | + it("falls back to the default timeout when the stored setting exceeds the supported maximum", () => { |
| 220 | + expect( |
| 221 | + getSupervisorEvaluationTimeoutMs({ |
| 222 | + get: vi.fn(() => 999_999_999), |
| 223 | + } as never) |
| 224 | + ).toBe(DEFAULT_SUPERVISOR_EVALUATION_TIMEOUT_SEC * 1000); |
| 225 | + }); |
| 226 | + |
| 227 | + it("falls back to the default timeout when the stored setting is fractional", () => { |
| 228 | + expect( |
| 229 | + getSupervisorEvaluationTimeoutMs({ |
| 230 | + get: vi.fn(() => 1.9), |
| 231 | + } as never) |
| 232 | + ).toBe(DEFAULT_SUPERVISOR_EVALUATION_TIMEOUT_SEC * 1000); |
| 233 | + }); |
| 234 | + |
| 235 | + it("falls back to the default timeout when reading the stored setting throws", () => { |
| 236 | + expect( |
| 237 | + getSupervisorEvaluationTimeoutMs({ |
| 238 | + get: vi.fn(() => { |
| 239 | + throw new SyntaxError("Unexpected token"); |
| 240 | + }), |
| 241 | + } as never) |
| 242 | + ).toBe(DEFAULT_SUPERVISOR_EVALUATION_TIMEOUT_SEC * 1000); |
| 243 | + }); |
| 244 | + |
| 245 | + it("reads the evaluator timeout from settingsRepo when timeoutMs is not provided", async () => { |
| 246 | + const settingsRepo = { |
| 247 | + get: vi.fn(() => 900), |
| 248 | + }; |
| 249 | + const evaluator = new SupervisorEvaluator({ |
| 250 | + providerRegistry: [createProvider("codex", "next step: run tests")], |
| 251 | + providerConfigRepo: createProviderConfigRepo(), |
| 252 | + settingsRepo: settingsRepo as never, |
| 253 | + }); |
| 254 | + |
| 255 | + const result = await evaluator.evaluate(makeSupervisor("codex"), makeContext()); |
| 256 | + |
| 257 | + expect(result.message).toBe("next step: run tests"); |
| 258 | + expect(settingsRepo.get).toHaveBeenCalledWith("supervisor.evaluationTimeoutSec"); |
| 259 | + }); |
| 260 | + |
| 261 | + it("falls back to the default timeout when the stored row is malformed JSON", async () => { |
| 262 | + const db = openDatabase(":memory:"); |
| 263 | + |
| 264 | + try { |
| 265 | + db.prepare("INSERT INTO user_settings (key, value) VALUES (?, ?)").run( |
| 266 | + "supervisor.evaluationTimeoutSec", |
| 267 | + "not-json" |
| 268 | + ); |
| 269 | + |
| 270 | + const evaluator = new SupervisorEvaluator({ |
| 271 | + providerRegistry: [createProvider("codex", "next step: run tests")], |
| 272 | + providerConfigRepo: createProviderConfigRepo(), |
| 273 | + settingsRepo: new SettingsRepo(db), |
| 274 | + }); |
| 275 | + |
| 276 | + const result = await evaluator.evaluate(makeSupervisor("codex"), makeContext()); |
| 277 | + |
| 278 | + expect(result.message).toBe("next step: run tests"); |
| 279 | + } finally { |
| 280 | + closeDatabase(db); |
| 281 | + } |
| 282 | + }); |
| 283 | + |
193 | 284 | it("builds a natural language prompt matching the develop supervisor pattern", async () => { |
194 | 285 | const logger = createLogger(); |
195 | 286 | const evaluator = new SupervisorEvaluator({ |
|
0 commit comments