Fix 11 memory leaks and add 35 tests#8
Open
vsumner wants to merge 2 commits into
Open
Conversation
6 tasks
Resource leaks fixed: - Session shutdown now disposes groupJoin, widget, agentActivity, batch timer - outputCleanup called in dispose() and removeRecord() before session disposal - AbortController nulled after completion/error to prevent retention - DefaultResourceLoader disposed in agent-runner finally block - execFileSync maxBuffer (10MB) added to all git calls in worktree.ts - finishedTurnAge entries pruned when exceeding ERROR_LINGER_TURNS - Stale typeListText cache replaced with inline buildTypeListText() - session_switch now updates currentCtx when provided - waitForAll gets timeout parameter (default 5min) with Promise.race - output-file rewritten to use async writes with batching - resetDefaults() added to agent-runner for global state cleanup Test coverage added: - agent-runner: resetDefaults, get/set for maxTurns and graceTurns - group-join: registration, completion flow, timeout, stragglers, dispose - output-file: JSONL format, async/sync flush, no-duplication, toolResult - agent-manager: outputCleanup, abortController lifecycle, waitForAll
P1 fixes:
- Extract releaseRecordResources() helper to deduplicate 4x cleanup pattern
- Replace dynamic require("node:fs") with static appendFileSync import
- Clear timeout timer in waitForAll when Promise.allSettled wins the race
P2 fixes:
- Fix asyncFlush/syncFlush race: advance writtenCount only after successful write
- Narrow (loader as any) cast to { dispose?(): void }
- Replace flaky 50ms sleeps in tests with deterministic file-size polling
- Expand inline batchFinalizeTimer cleanup to multi-line
P3 fixes:
- Remove redundant _INITIAL suffix from constants (DEFAULT_MAX_TURNS, GRACE_TURNS)
- Shorten over-verbose widget prune comment to inline
- Use queueMicrotask for asyncFlush re-entry instead of direct recursion
vsumner
force-pushed
the
fix/memory-leaks-and-test-coverage
branch
from
March 18, 2026 19:07
8754b53 to
ea19241
Compare
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
~/.pi/agent/agents/)Fixes
P0 — Session shutdown cleanup (
index.ts)session_shutdownnow disposesgroupJoin,widget, clearsagentActivity,batchFinalizeTimer,currentBatchAgents, and callsresetDefaults()P0 — outputCleanup in dispose (
agent-manager.ts)dispose()andremoveRecord()callrecord.outputCleanup?.()beforesession.dispose()P0 — AbortController nulling (
agent-manager.ts).then()and.catch()handlers nullrecord.abortControllerafter completionP1 — ResourceLoader disposal (
agent-runner.ts)finallyblock calls(loader as any).dispose?.()to release file watchersP1 — maxBuffer on execFileSync (
worktree.ts)maxBufferto all git status/add/commit/worktree-add callsP2 — finishedTurnAge pruning (
agent-widget.ts)onTurnStart()prunes entries exceedingERROR_LINGER_TURNSP2 — Stale tool descriptions (
index.ts)const typeListText, callsbuildTypeListText()inlineP2 — currentCtx on session_switch (
index.ts)session_switchhandler accepts and updatesctxif providedP3 — waitForAll timeout (
agent-manager.ts)timeoutMsparameter (default 5 minutes) withPromise.raceP3 — Async output writes (
output-file.ts)appendFilewith batching; final cleanup uses sync for safetyP3 — Global state reset (
agent-runner.ts)resetDefaults()export, called from shutdown handlerNew tests (35 total)
test/agent-runner.test.tsresetDefaults(), get/set maxTurns, get/set graceTurns, min-clampingtest/group-join.test.tstest/output-file.test.tstest/agent-manager.test.tsTest plan
npx tsc --noEmit— clean typechecknpx vitest run— 226 passed, 4 failed (pre-existing, unrelated)