baphuongna/pi-crew/live-agent-lifecycle
Claude Code and agent skill by baphuongna/pi-crew
- community
Checking that the SKILL.md file is still in the repository.
Live agent registration, workspace isolation, termination, and eviction workflow. When NOT to use: static extension registration patterns (use pi-extension-lifecycle); ownership disputes (use ownership-session-security).
Install
npx vskill@latest install baphuongna/pi-crew/live-agent-lifecycleSKILL.md
live-agent-lifecycle
Live agents are real-time, in-memory worker sessions managed by LiveAgentManager (src/runtime/live-agent-manager.ts). They are distinct from CrewAgentRecord files on disk — live agents provide real-time activity (tool names, response text, turn count) while agent records are durable snapshots.
Architecture
LiveAgentHandle is the core data structure:
The in-memory liveAgents Map stores all active handles. It is never persisted — on Pi restart, the Map is empty and agents are re-created from agent records.
Registration
registerLiveAgent(input, eventLogFn?, eventsPath?) is called when a live session worker starts. It:
- Creates or reuses the handle in
liveAgentsMap - Preserves pending steers/followups from previous sessions
- Emits
live_agent.registeredevent to events.jsonl - Flushes any pending steers/followups immediately if the session already has the methods
Key caller sites:
live-session-runtime.ts— when a live session agent startslive-executor.ts— when spawning a live task- (workspaceId is passed through the entire call chain)
Workspace Isolation
`workspaceId: string` field is the workspace boundary. Set to manifest.cwd at registration time.
Why it matters: When Pi has multiple workspace folders open, agents from workspace A must not be visible or controllable from workspace B. Every handle carries its origin workspace.
Enforcement in api.ts:
listActiveLiveAgentsByWorkspace(workspaceId)— filters by workspace- Steering/follow-up operations check
live.workspaceId !== manifest.cwd→ reject with error - Widget queries use
listLiveAgentsByWorkspace(manifest.cwd)so each workspace only sees its own agents
Enforcement in live-session-runtime.ts:
- Config carries
workspaceIdfromTeamContext.workspaceId - Session creation passes workspaceId through
Activity Tracking
LiveAgentActivity provides real-time data without reading disk:
Tracking functions (called from live-executor):
trackLiveAgentToolStart(agentId, toolName)— adds tool to activeToolstrackLiveAgentToolEnd(agentId, toolName)— removes tool from activeToolstrackLiveAgentTurnEnd(agentId, compaction?)— increments turn, clears toolstrackLiveAgentResponseText(agentId, text)— stores last 200 charsmarkLiveAgentCompleted(agentId)— sets completedAtMs
Termination
terminateLiveAgent(agentIdOrTaskId, status?, eventLogFn?, eventsPath?) is the canonical termination path:
- Sets handle status (default: "stopped")
- Emits
live_agent.terminatedevent to events.jsonl - Calls
session.abort()to stop the child - Calls
session.dispose()to clean up - Removes from
liveAgentsMap
Termination call sites (4 total):
| Location | When | |
|---|---|---|
team-runner.ts (run complete) | All agents terminated when run succeeds or fails | |
team-runner.ts (task complete) | Per-task termination when terminateOnTaskComplete=true | |
background-runner.ts (catch) | Termination in finally block after background run | |
cancel.ts | Termination when user cancels a run | |
respond.ts | Termination when responding to waiting tasks | |
crash-recovery.ts (purgeStale) | Termination when cleaning up orphaned runs | |
crash-recovery.ts (reconcile) | Termination when reconciling stale runs |
terminateLiveAgentsForRun(runId, status?, eventLogFn?, eventsPath?) terminates all agents for a run in parallel.
Eviction
Stale handles are handles whose status is terminal (not running/queued/waiting) and older than 10 minutes. evictStaleLiveAgentHandles(now?) removes them:
Triggered on every widget refresh in crew-widget.ts:
This prevents the Map from growing indefinitely with completed agents.
Live Agent → Agent Record Sync
On task completion, upsertCrewAgent(manifest, recordFromTask(manifest, task, "live-session")) is called to persist the final status to disk (agents.json, agents/<id>/status.json). This ensures the widget sees the correct status even after the live agent handle is evicted.
The sync chain:
Enforcement — Live Agent Lifecycle Gate
Before terminating or evicting live agents, verify:
- [ ] Agent handle status is terminal (not running/queued/waiting) for eviction
- [ ] Handle age exceeds STALE_HANDLE_MS (10 minutes) for eviction
- [ ] workspaceId matches current workspace for cross-workspace prevention
- [ ] Agent record sync completed before handle eviction (upsertCrewAgent called)
- [ ] Termination called in all exit paths (finally blocks, crash-recovery)
If ANY answer is NO → Stop. Verify lifecycle state before mutation.
Anti-patterns
- Missing termination on error path: If a live agent crashes and
terminateLiveAgentis not called, the handle stays in the Map forever with status "running". Usefinallyblocks or crash-recovery to ensure termination. - Stale handle accumulation: Without
evictStaleLiveAgentHandles, completed agents accumulate in the Map. This is mitigated by calling eviction on every widget refresh. - Cross-workspace access: Never call steer/follow-up/stop/resume on a handle whose
workspaceIddiffers from the current workspace. Always checklive.workspaceId === manifest.cwd. - Losing activity on session switch: Live agents are in-memory only. On Pi session switch/restart, all activity tracking is lost. Agent records on disk persist.
Source patterns
src/runtime/live-agent-manager.ts— register, terminate, evict, workspaceIdsrc/runtime/live-session-runtime.ts— 4 lifecycle gaps fixedsrc/runtime/team-runner.ts— terminate on success/failsrc/runtime/background-runner.ts— terminate on catchsrc/runtime/crash-recovery.ts— terminate in purge+reconcilesrc/extension/team-tool/api.ts— workspaceId filtersrc/extension/team-tool/cancel.ts— terminate on cancelsrc/extension/team-tool/respond.ts— terminate on respondsrc/ui/crew-widget.ts— evictStaleLiveAgentHandles on refresh
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/live-agent-lifecycle/SKILL.md
Popularity
- 7-day trend score
- 0
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/live-agent-lifecycle)