Decompose spawnTuiAgent() into cohesive helpers (#2833)#2860
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
spawnTuiAgent()inserver/services/agentTuiSpawning.jshad grown to ~1000 lines, handling PTY spawn, output buffering/spooling, sentinel polling, idle/runtime timeouts, paste-marker handshaking, and failure analysis in one function body. This extracts the two most cohesive, self-contained sub-concerns into sibling modules sospawnTuiAgentreads as an orchestrator:agentTuiSpawning/outputSpooler.js—createOutputSpooler({ agentId, outputFile, rawFile })owns both batched write pipelines: parsed status lines →output.txt+ agent state stream, and raw PTY bytes → theraw.txtdisk spool (with the once-per-run overflow warn + metadata flag, the disk-safety-valve truncation, and the 250ms debounce). ExposesappendLine,pushRaw,flushRaw,drainLines,drainRaw,getOutputBuffer.agentTuiSpawning/finalizeHelpers.js— the failure-analysis + worktree-inspection helpers that were module-level functions:readFileTail,worktreeHasChanges,captureWorktreeDiff, plus a newresolveErrorAnalysisthat folds the finalize-path "read the raw-spool tail, else fall back to the output buffer, else honor an immediate-fallback signal" logic into one call.RAW_TAIL_ANALYSIS_BYTESmoves here.The main module drops from ~1385 to ~1166 lines. No behavior change to spawn/timeout/completion semantics — the extracted code is byte-for-byte the same logic, only relocated, and every call site in
spawnTuiAgent(finish()drain + error-analysis +outputBufferreads,handleDataraw push,finishStartupFailureraw flush) is rewired to the new surface with identical ordering.Test plan
npx vitest run services/agentTuiSpawning.test.js— all 54 existing agent-spawn tests pass unchanged (idle-complete/idle-no-activity/idle-no-changes, max-runtime, paste handshake, MCP-boot budget, raw-spool truncation, tail-read, sentinel ingestion, etc.).services/agentTuiSpawning/outputSpooler.test.js(7 cases) covers appendLine flush + dedup + blank-drop, output-buffer overflow warn/metadata (once), raw append vs. truncation dispatch, and no-op drains.services/agentTuiSpawning/finalizeHelpers.test.js(15 cases) coversreadFileTail(missing → null, empty → '', whole-file, tail-slice),worktreeHasChanges(dirty/clean/reject/empty-path),captureWorktreeDiff(combined write + return, no-diff, invalid args), andresolveErrorAnalysis(success skips read, immediate-fallback short-circuit, tail analyze, missing-spool fallback).npx vitest run services/agentManagement.test.js— 34 pass (downstream consumer unaffected).Closes #2833