fix(pi-subagents): terminate children recursively when parent process dies - #40
Merged
Merged
Conversation
… dies Add an extension-local anonymous lifeline pipe (stdin/fd 0) that lets managed subagent children detect parent process death. When the parent dies (even SIGKILL), the kernel closes the write end of the pipe; the child's prompt runtime watches the read end for EOF and self-terminates via SIGTERM. Because every recursively spawned child uses the same mechanism, the cascade is automatic: parent death → child EOF/termination → grandchild EOF/termination. Key design decisions: - stdin fd 0 reused as lifeline (zero extra FDs, zero new OS objects) - Lifeline closed on session_shutdown (session replacement) but NOT on agent_end (background children survive turns) - Normal child completion also releases the lifeline - Timeout semantics unchanged: timeout notifies and does not kill Tests added (6 new, all passing): - lifeline env constant export - prompt runtime sets up EOF watcher on lifeline fd - session_shutdown terminates children via lifeline - agent_end does NOT kill children - abrupt parent SIGKILL cascades to real child termination - recursive cascade: grandparent death → parent subagent death
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
Child Pi subagents now self-terminate when their owning parent Pi session/process dies. This uses an extension-local anonymous lifeline pipe on stdin (fd 0) — zero extra FDs, zero new OS objects, zero external dependencies.
Architecture
stdio: [pipe, pipe, pipe]— stdin becomes the lifelinechild.stdinopen without writing; the child runtime watches fd 0 for EOFsession_shutdown(session replacement) but NOT onagent_end(background children survive turns)Changes
src/runs/shared/subagent-prompt-runtime.tsPI_SUBAGENT_LIFELINE_FDenv constant; add module-level lifeline watcher that listens for EOF on the lifeline fd and self-SIGTERMssrc/extension/index.ts[pipe, pipe, pipe]; manage lifelines map; close on completion and session_shutdowntest/unit/minimal-subagents.test.tsValidation
lifeline env constant export— the runtime exposes the env varprompt runtime sets up EOF watcher— the runtime registers the watcher when env is presentsession_shutdown terminates children via lifeline— closing lifeline kills real child processagent_end does NOT kill child process— background children survive turnsabrupt parent SIGKILL cascades to child termination— killing a real parent process causes its real child to exit promptlyrecursive cascade — grandparent death kills parent subagent— killing the grandparent causes the parent subagent to die (and by extension any grandchildren)stdiopattern and still passes