- Switch Git trace2 monitoring from polling to filesystem watch - Parse trace chunks with schema decoding and keep hook progress flowing - Update test expectations for hook output ordering
Unified · Split
| @@ -1,7 +1,5 @@ | |||
| 1 | import { randomUUID } from "node:crypto"; | 1 | import { randomUUID } from "node:crypto"; |
| 2 | import { readFile, unlink } from "node:fs/promises"; | ||
| 3 | import { tmpdir } from "node:os"; | 2 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; | ||
| 5 | 3 | ||
| 6 | import { | 4 | import { |
| 7 | Cache, | 5 | Cache, |
| @@ -32,7 +30,6 @@ import { ServerConfig } from "../../config.ts"; · 24 unmodified lines | |||
| 32 | 30 | ||
| 33 | const DEFAULT_TIMEOUT_MS = 30_000; | 31 | const DEFAULT_TIMEOUT_MS = 30_000; |
| 34 | const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; | 32 | const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; |
| 35 | const TRACE2_POLL_INTERVAL_MS = 100; | ||
| 36 | const STATUS_UPSTREAM_REFRESH_INTERVAL = Duration.seconds(15); | 33 | const STATUS_UPSTREAM_REFRESH_INTERVAL = Duration.seconds(15); |
| 37 | const STATUS_UPSTREAM_REFRESH_TIMEOUT = Duration.seconds(5); | 34 | const STATUS_UPSTREAM_REFRESH_TIMEOUT = Duration.seconds(5); |
| 38 | const STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY = 2_048; | 35 | const STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY = 2_048; |
| @@ -300,6 +297,8 @@ function parseTraceRecord( · 261 unmodified lines | |||
| 300 | 297 | ||
| 301 | const createTrace2Monitor = Effect.fn(function* ( | 298 | const createTrace2Monitor = Effect.fn(function* ( |
| 302 | input: Pick<ExecuteGitInput, "operation" | "cwd" | "args">, | 299 | input: Pick<ExecuteGitInput, "operation" | "cwd" | "args">, |
| 300 | fileSystem: FileSystem.FileSystem, | ||
| 301 | path: Path.Path, | ||
| 303 | progress: ExecuteGitProgress | undefined, | 302 | progress: ExecuteGitProgress | undefined, |
| 304 | ): Effect.fn.Return<Trace2Monitor, never, Scope.Scope> { | 303 | ): Effect.fn.Return<Trace2Monitor, never, Scope.Scope> { |
| 305 | if (!progress?.onHookStarted && !progress?.onHookFinished) { | 304 | if (!progress?.onHookStarted && !progress?.onHookFinished) { |
| @@ -309,11 +308,12 @@ const createTrace2Monitor = Effect.fn(function* ( · 3 unmodified lines | |||
| 309 | }; | 308 | }; |
| 310 | } | 309 | } |
| 311 | 310 | ||
| 312 | const traceFilePath = join(tmpdir(), `t3code-git-trace2-${process.pid}-${randomUUID()}.json`); | 311 | const traceDir = tmpdir(); |
| 312 | const traceFileName = `t3code-git-trace2-${process.pid}-${randomUUID()}.json`; | ||
| 313 | const traceFilePath = path.join(traceDir, traceFileName); | ||
| 313 | const hookStartByChildKey = new Map<string, { hookName: string; startedAtMs: number }>(); | 314 | const hookStartByChildKey = new Map<string, { hookName: string; startedAtMs: number }>(); |
| 314 | let processedChars = 0; | 315 | let processedChars = 0; |
| 315 | let lineBuffer = ""; | 316 | let lineBuffer = ""; |
| 316 | let polling = false; | ||
| 317 | 317 | ||
| 318 | const handleTraceLine = (line: string) => | 318 | const handleTraceLine = (line: string) => |
| 319 | Effect.gen(function* () { | 319 | Effect.gen(function* () { |
| @@ -370,40 +370,16 @@ const createTrace2Monitor = Effect.fn(function* ( · 50 unmodified lines | |||
| 370 | } | 370 | } |
| 371 | }); | 371 | }); |
| 372 | 372 | ||
| 373 | const pollTraceFile = Effect.tryPromise({ | 373 | const readTraceDelta = fileSystem.readFileString(traceFilePath).pipe( |
| 374 | try: async () => { | ||
| 375 | if (polling) { | ||
| 376 | return ""; | ||
| 377 | } | ||
| 378 | polling = true; | ||
| 379 | try { | ||
| 380 | const contents = await readFile(traceFilePath, "utf8").catch((error: unknown) => { | ||
| 381 | const code = | ||
| 382 | typeof error === "object" && error !== null ? (error as { code?: unknown }).code : null; | ||
| 383 | if (code === "ENOENT") { | ||
| 384 | return ""; | ||
| 385 | } | ||
| 386 | throw error; | ||
| 387 | }); | ||
| 388 | if (contents.length <= processedChars) { | ||
| 389 | return ""; | ||
| 390 | } | ||
| 391 | const delta = contents.slice(processedChars); | ||
| 392 | processedChars = contents.length; | ||
| 393 | return delta; | ||
| 394 | } finally { | ||
| 395 | polling = false; | ||
| 396 | } | ||
| 397 | }, | ||
| 398 | catch: () => undefined, | ||
| 399 | }).pipe( | ||
| 400 | Effect.catch(() => Effect.succeed("")), | 374 | Effect.catch(() => Effect.succeed("")), |
| 401 | Effect.flatMap((delta) => | 375 | Effect.flatMap((delta) => |
| 402 | Effect.gen(function* () { | 376 | Effect.gen(function* () { |
| 403 | if (delta.length === 0) { | 377 | if (delta.length <= processedChars) { |
| 404 | return; | 378 | return; |
| 405 | } | 379 | } |
| 406 | lineBuffer += delta; | 380 | const appended = delta.slice(processedChars); |
| 381 | processedChars = delta.length; | ||
| 382 | lineBuffer += appended; | ||
| 407 | let newlineIndex = lineBuffer.indexOf("\n"); | 383 | let newlineIndex = lineBuffer.indexOf("\n"); |
| 408 | while (newlineIndex >= 0) { | 384 | while (newlineIndex >= 0) { |
| 409 | const line = lineBuffer.slice(0, newlineIndex); | 385 | const line = lineBuffer.slice(0, newlineIndex); |
| @@ -414,23 +390,28 @@ const createTrace2Monitor = Effect.fn(function* ( · 4 unmodified lines | |||
| 414 | }), | 390 | }), |
| 415 | ), | 391 | ), |
| 416 | ); | 392 | ); |
| 393 | const watchTraceFile = Stream.runForEach(fileSystem.watch(traceDir), (event) => { | ||
| 394 | const eventPath = event.path; | ||
| 395 | const isTargetTraceEvent = | ||
| 396 | eventPath === traceFileName || | ||
| 397 | eventPath === traceFilePath || | ||
| 398 | path.resolve(traceDir, eventPath) === traceFilePath; | ||
| 399 | if (!isTargetTraceEvent) { | ||
| 400 | return Effect.void; | ||
| 401 | } | ||
| 402 | return readTraceDelta; | ||
| 403 | }).pipe(Effect.ignoreCause({ log: true })); | ||
| 417 | 404 | ||
| 418 | const interval = setInterval(() => { | 405 | yield* Effect.forkScoped(watchTraceFile); |
| 419 | void Effect.runPromise(pollTraceFile); | ||
| 420 | }, TRACE2_POLL_INTERVAL_MS); | ||
| 421 | 406 | ||
| 422 | yield* Effect.addFinalizer(() => | 407 | yield* Effect.addFinalizer(() => |
| 423 | Effect.gen(function* () { | 408 | Effect.gen(function* () { |
| 424 | clearInterval(interval); | 409 | yield* readTraceDelta; |
| 425 | yield* pollTraceFile; | ||
| 426 | const finalLine = lineBuffer.trim(); | 410 | const finalLine = lineBuffer.trim(); |
| 427 | if (finalLine.length > 0) { | 411 | if (finalLine.length > 0) { |
| 428 | yield* handleTraceLine(finalLine); | 412 | yield* handleTraceLine(finalLine); |
| 429 | } | 413 | } |
| 430 | yield* Effect.tryPromise({ | 414 | yield* fileSystem.remove(traceFilePath).pipe(Effect.catch(() => Effect.void)); |
| 431 | try: () => unlink(traceFilePath), | ||
| 432 | catch: () => undefined, | ||
| 433 | }).pipe(Effect.ignore); | ||
| 434 | }), | 415 | }), |
| 435 | ); | 416 | ); |
| 436 | 417 | ||
| @@ -438,7 +419,7 @@ const createTrace2Monitor = Effect.fn(function* ( · 1 unmodified line | |||
| 438 | env: { | 419 | env: { |
| 439 | GIT_TRACE2_EVENT: traceFilePath, | 420 | GIT_TRACE2_EVENT: traceFilePath, |
| 440 | }, | 421 | }, |
| 441 | flush: pollTraceFile, | 422 | flush: readTraceDelta, |
| 442 | }; | 423 | }; |
| 443 | }); | 424 | }); |
| 444 | 425 | ||
| @@ -520,7 +501,12 @@ export const makeGitCore = (options?: { executeOverride?: GitCoreShape["execute" · 75 unmodified lines | |||
| 520 | const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; | 501 | const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; |
| 521 | 502 | ||
| 522 | const commandEffect = Effect.gen(function* () { | 503 | const commandEffect = Effect.gen(function* () { |
| 523 | const trace2Monitor = yield* createTrace2Monitor(commandInput, input.progress); | 504 | const trace2Monitor = yield* createTrace2Monitor( |
| 505 | commandInput, | ||
| 506 | fileSystem, | ||
| 507 | path, | ||
| 508 | input.progress, | ||
| 509 | ); | ||
| 524 | const child = yield* commandSpawner | 510 | const child = yield* commandSpawner |
| 525 | .spawn( | 511 | .spawn( |
| 526 | ChildProcess.make("git", commandInput.args, { | 512 | ChildProcess.make("git", commandInput.args, { |
| @@ -1996,9 +1996,12 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => { · 1995 unmodified lines | |||
| 1996 | }), | 1996 | }), |
| 1997 | expect.objectContaining({ | 1997 | expect.objectContaining({ |
| 1998 | kind: "hook_output", | 1998 | kind: "hook_output", |
| 1999 | hookName: "pre-commit", | ||
| 2000 | text: "hook: start", | 1999 | text: "hook: start", |
| 2001 | }), | 2000 | }), |
| 2001 | expect.objectContaining({ | ||
| 2002 | kind: "hook_output", | ||
| 2003 | text: "hook: end", | ||
| 2004 | }), | ||
| 2002 | expect.objectContaining({ | 2005 | expect.objectContaining({ |
| 2003 | kind: "hook_finished", | 2006 | kind: "hook_finished", |
| 2004 | hookName: "pre-commit", | 2007 | hookName: "pre-commit", |