baphuongna/pi-crew/child-pi-spawning
Claude Code and agent skill by baphuongna/pi-crew
- community
Checking that the SKILL.md file is still in the repository.
Child Pi worker spawning, lifecycle callbacks, and failure modes. When NOT to use: non-Pi subprocess execution (use safe-bash); for general async reliability (use async-worker-recovery).
Install
npx vskill@latest install baphuongna/pi-crew/child-pi-spawningSKILL.md
child-pi-spawning
Child Pi workers are subprocesses spawned by task-runner.ts via runChildPi() in child-pi.ts. Understanding the spawn flow, lifecycle events, and failure modes is essential for debugging worker crashes and "worker blinks" issues.
Spawn Flow
Key components
- ChildPiLineObserver: Parses JSON events and stdout lines from child Pi's output stream
- Response timeout: 5-minute timer resets on every stdout/stderr chunk; on timeout → SIGTERM
- Final drain: After last assistant event, waits
finalDrainMs(default 2s) then SIGTERM - Hard kill: After
hardKillMs(default 2s) from SIGTERM, SIGKILL - Active process tracking:
activeChildProcessesMap for global cleanup
Lifecycle Events
ChildPiLifecycleEvent interface — emitted via onLifecycleEvent callback:
Event sequence for normal completion:
Event sequence for crash:
Event sequence for timeout:
onLifecycleEvent Callback Pattern
The callback bridges child-pi events → events.jsonl:
Why a callback instead of direct logging: child-pi.ts has no access to manifest/eventsPath. The callback lets the caller (task-runner) decide how to log.
Scaffold Mode
When: executeWorkers = false or runtime.kind === 'scaffold'
Behavior: No child process spawned. runChildPi is never called. The task:
- Writes the prompt to disk as an artifact
- Immediately completes with a scaffold result artifact
- No
worker.spawnedevent — the agent appears and completes instantly
Display implication: In widget, scaffold agents appear and complete within 1 frame. This is normal behavior, not a bug.
Detection: runtimeKind === "child-process" triggers child spawning; "scaffold" or "live-session" skip it.
Child Args and Environment
Args built by buildPiWorkerArgs() (pi-args.ts)
Environment variables
GetPiSpawnCommand
Resolves the pi binary path and builds the final command/args. On Windows, uses pi.cmd or pi.exe.
Common Spawn Failures
| Symptom | Root cause | Fix | |
|---|---|---|---|
spawn_error: spawn returned no pid | child.pid is undefined — spawn call failed silently | Check binary path via getPiSpawnCommand() | |
spawn_error: not a valid Win32 application | Wrong binary (32-bit vs 64-bit) | Reinstall pi binary | |
spawn_error: Access is denied | Binary not executable, or antivirus blocking | Check file permissions, run as admin | |
spawn_error: ENOENT: no such file or directory | pi not in PATH | Add pi to PATH, or use full path | |
| Worker crashes with exitCode=1, no output | API key missing or wrong | Check PI_API_KEY / ANTHROPIC_API_KEY | |
| Worker crashes with exitCode=1, "Model not available" | Wrong model name | Check model name in config | |
| Worker spawns, logs in, then crashes | Model rate limit / quota exceeded | Check provider limits | |
response_timeout: No output for 300000ms | Child process hung (network issue, model timeout) | Increase responseTimeoutMs, check network | |
| Worker completes but output not captured | stdout/stderr stream issue | Check ChildPiLineObserver parsing |
Exit Code Mapping
| Exit code | Meaning | |
|---|---|---|
0 | Success — worker produced output and completed | |
1 | Error — worker encountered a non-fatal error (API error, validation failure) | |
null | Killed — worker was SIGTERM'd or SIGKILL'd (timeout, cancel, drain) | |
130 | SIGINT — interrupted by user cancel |
Note: final_drain followed by exitCode=0 means the worker completed its output before being killed. The 0 exit code preserves the result.
PID Tracking
- PID recorded in
manifest.async.pidat spawn (viacheckpointTask) - PID checked by
hasStaleAsyncProcess()(process-status.ts) to detect dead processes - PID used by
killProcessPid()(child-pi.ts) for termination - PID in
childHardKillTimersMap for timer cleanup on exit
Enforcement — Child Pi Spawning Gate
Before debugging worker spawn issues, verify:
- [ ] Spawn error type identified (binary not found, permission denied, API key missing)
- [ ] PID tracking status confirmed (recorded in manifest, process alive/dead)
- [ ] Lifecycle event sequence matches expected pattern (spawned → output → exit)
- [ ] Timeout source determined (5-min response timeout, hard kill, final drain)
- [ ] Scaffold mode correctly identified (no worker.spawned event expected)
If ANY answer is NO → Stop. Re-examine events.jsonl and manifest before proceeding.
Anti-patterns
- Blocking on spawn:
spawn()is async — never await it synchronously. Use the Promise-based API. - Not handling exit: Always handle
child.on("exit")andchild.on("close"). Without handlers, zombie processes accumulate. - Ignoring lifecycle events: Without
onLifecycleEventhandling, worker crashes leave no traceable evidence. - Not cleaning up timers: Hard-kill timers, response-timeout timers, and final-drain timers must be cleared on all exit paths.
- Passing secrets in args: Child args are visible in process list. Use env vars (with redaction) instead.
- Not handling `spawn_error`: Errors on spawn (binary not found, permission denied) must be caught and logged.
Source patterns
src/runtime/child-pi.ts— runChildPi, ChildPiLifecycleEvent, activeChildProcesses, killProcessPidsrc/runtime/task-runner.ts— executeTask loop, onLifecycleEvent callback, runtimeKindsrc/runtime/pi-args.ts— buildPiWorkerArgs, applyThinkingSuffixsrc/runtime/runtime-resolver.ts— resolveCrewRuntime, isLiveSessionRuntimeAvailable, scaffold detectionsrc/runtime/model-resolver.ts— model fallback chainsrc/utils/env-filter.ts— sanitizeEnvSecretssrc/config/defaults.ts— responseTimeoutMs, finalDrainMs, hardKillMs
Verification
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/child-pi-spawning/SKILL.md
Popularity
- Repository stars
- 58
- Repository forks
- 22
- 7-day trend score
- 0
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/baphuongna/pi-crew/child-pi-spawning)