- 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 | 1 | import { randomUUID } from "node:crypto"; |
| 2 | −import { readFile, unlink } from "node:fs/promises"; | |
| 3 | 2 | import { tmpdir } from "node:os"; |
| 4 | −import { join } from "node:path"; | |
| 5 | 3 | |
| 6 | 4 | import { |
| 7 | 5 | Cache, |
| @@ -32,7 +30,6 @@ import { ServerConfig } from "../../config.ts"; · 24 unmodified lines | ||
| 32 | 30 | |
| 33 | 31 | const DEFAULT_TIMEOUT_MS = 30_000; |
| 34 | 32 | const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000; |
| 35 | −const TRACE2_POLL_INTERVAL_MS = 100; | |
| 36 | 33 | const STATUS_UPSTREAM_REFRESH_INTERVAL = Duration.seconds(15); |
| 37 | 34 | const STATUS_UPSTREAM_REFRESH_TIMEOUT = Duration.seconds(5); |
| 38 | 35 | const STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY = 2_048; |
| @@ -300,6 +297,8 @@ function parseTraceRecord( · 261 unmodified lines | ||
| 300 | 297 | |
| 301 | 298 | const createTrace2Monitor = Effect.fn(function* ( |
| 302 | 299 | input: Pick<ExecuteGitInput, "operation" | "cwd" | "args">, |
| 300 | + fileSystem: FileSystem.FileSystem, | |
| 301 | + path: Path.Path, | |
| 303 | 302 | progress: ExecuteGitProgress | undefined, |
| 304 | 303 | ): Effect.fn.Return<Trace2Monitor, never, Scope.Scope> { |
| 305 | 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 | 314 | const hookStartByChildKey = new Map<string, { hookName: string; startedAtMs: number }>(); |
| 314 | 315 | let processedChars = 0; |
| 315 | 316 | let lineBuffer = ""; |
| 316 | − let polling = false; | |
| 317 | 317 | |
| 318 | 318 | const handleTraceLine = (line: string) => |
| 319 | 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({ | |
| 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( | |
| 373 | + const readTraceDelta = fileSystem.readFileString(traceFilePath).pipe( | |
| 400 | 374 | Effect.catch(() => Effect.succeed("")), |
| 401 | 375 | Effect.flatMap((delta) => |
| 402 | 376 | Effect.gen(function* () { |
| 403 | − if (delta.length === 0) { | |
| 377 | + if (delta.length <= processedChars) { | |
| 404 | 378 | return; |
| 405 | 379 | } |
| 406 | − lineBuffer += delta; | |
| 380 | + const appended = delta.slice(processedChars); | |
| 381 | + processedChars = delta.length; | |
| 382 | + lineBuffer += appended; | |
| 407 | 383 | let newlineIndex = lineBuffer.indexOf("\n"); |
| 408 | 384 | while (newlineIndex >= 0) { |
| 409 | 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(() => { | |
| 419 | − void Effect.runPromise(pollTraceFile); | |
| 420 | − }, TRACE2_POLL_INTERVAL_MS); | |
| 405 | + yield* Effect.forkScoped(watchTraceFile); | |
| 421 | 406 | |
| 422 | 407 | yield* Effect.addFinalizer(() => |
| 423 | 408 | Effect.gen(function* () { |
| 424 | − clearInterval(interval); | |
| 425 | − yield* pollTraceFile; | |
| 409 | + yield* readTraceDelta; | |
| 426 | 410 | const finalLine = lineBuffer.trim(); |
| 427 | 411 | if (finalLine.length > 0) { |
| 428 | 412 | yield* handleTraceLine(finalLine); |
| 429 | 413 | } |
| 430 | − yield* Effect.tryPromise({ | |
| 431 | − try: () => unlink(traceFilePath), | |
| 432 | − catch: () => undefined, | |
| 433 | − }).pipe(Effect.ignore); | |
| 414 | + yield* fileSystem.remove(traceFilePath).pipe(Effect.catch(() => Effect.void)); | |
| 434 | 415 | }), |
| 435 | 416 | ); |
| 436 | 417 | |
| @@ -438,7 +419,7 @@ const createTrace2Monitor = Effect.fn(function* ( · 1 unmodified line | ||
| 438 | 419 | env: { |
| 439 | 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 | 501 | const maxOutputBytes = input.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; |
| 521 | 502 | |
| 522 | 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 | 510 | const child = yield* commandSpawner |
| 525 | 511 | .spawn( |
| 526 | 512 | ChildProcess.make("git", commandInput.args, { |
| @@ -1996,9 +1996,12 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => { · 1995 unmodified lines | ||
| 1996 | 1996 | }), |
| 1997 | 1997 | expect.objectContaining({ |
| 1998 | 1998 | kind: "hook_output", |
| 1999 | − hookName: "pre-commit", | |
| 2000 | 1999 | text: "hook: start", |
| 2001 | 2000 | }), |
| 2001 | + expect.objectContaining({ | |
| 2002 | + kind: "hook_output", | |
| 2003 | + text: "hook: end", | |
| 2004 | + }), | |
| 2002 | 2005 | expect.objectContaining({ |
| 2003 | 2006 | kind: "hook_finished", |
| 2004 | 2007 | hookName: "pre-commit", |