Skip to content

Decompose spawnTuiAgent() into cohesive helpers (#2833)#2860

Merged
atomantic merged 1 commit into
mainfrom
claim/issue-2833
Jul 22, 2026
Merged

Decompose spawnTuiAgent() into cohesive helpers (#2833)#2860
atomantic merged 1 commit into
mainfrom
claim/issue-2833

Conversation

@atomantic

Copy link
Copy Markdown
Owner

Summary

spawnTuiAgent() in server/services/agentTuiSpawning.js had 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 so spawnTuiAgent reads as an orchestrator:

  • agentTuiSpawning/outputSpooler.jscreateOutputSpooler({ agentId, outputFile, rawFile }) owns both batched write pipelines: parsed status lines → output.txt + agent state stream, and raw PTY bytes → the raw.txt disk spool (with the once-per-run overflow warn + metadata flag, the disk-safety-valve truncation, and the 250ms debounce). Exposes appendLine, pushRaw, flushRaw, drainLines, drainRaw, getOutputBuffer.
  • agentTuiSpawning/finalizeHelpers.js — the failure-analysis + worktree-inspection helpers that were module-level functions: readFileTail, worktreeHasChanges, captureWorktreeDiff, plus a new resolveErrorAnalysis that 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_BYTES moves 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 + outputBuffer reads, handleData raw push, finishStartupFailure raw 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.).
  • New 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.
  • New services/agentTuiSpawning/finalizeHelpers.test.js (15 cases) covers readFileTail (missing → null, empty → '', whole-file, tail-slice), worktreeHasChanges (dirty/clean/reject/empty-path), captureWorktreeDiff (combined write + return, no-diff, invalid args), and resolveErrorAnalysis (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

@atomantic
atomantic merged commit d0828ef into main Jul 22, 2026
2 checks passed
@atomantic
atomantic deleted the claim/issue-2833 branch July 22, 2026 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decompose spawnTuiAgent() in server/services/agentTuiSpawning.js (~1093-line function)

1 participant