Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions docs/ai/design/2026-06-01-feature-agent-console-kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
phase: design
title: Agent Console Kill Design
description: Technical design for confirmed kill support in agent console
---

# System Design & Architecture

## Architecture Overview

```mermaid
graph TD
User["User presses K"] --> ConsoleApp["ConsoleAppShell"]
ConsoleApp --> Confirm["Kill confirmation overlay"]
Confirm -->|Esc/n| Cancel["Close overlay"]
Confirm -->|Enter/y| RunAction["runAction({ type: kill })"]
RunAction --> KillCmd["agent kill <name>"]
KillCmd --> AgentManager["AgentManager.listAgents/resolveAgent"]
KillCmd --> ProcessKill["process.kill(pid, SIGTERM)"]
KillCmd --> Registry["AgentRegistry.lookup(name)"]
Registry --> TmuxSession{"tmuxSession?"}
TmuxSession -->|yes| TmuxManager["TmuxManager.killSession"]
TmuxSession -->|no| Done["Success"]
```

Kill support follows the existing console action pattern: the TUI handles key input and presentation, while the destructive operation runs through a CLI subprocess. The CLI receives the agent name, resolves it against the current live agent list, sends `SIGTERM` to the PID, and cleans up the registry-backed tmux session when available.

## Data Models

- `ConsoleAction`
- Add `{ type: 'kill'; agentName: string }`.
- `AgentInfo`
- Existing selected agent data supplies `name`, `pid`, and optionally `tmuxSession`.
- `RegistryEntry`
- `AgentRegistry.lookup(name)` supplies a preserved `tmuxSession` when the live adapter did not populate it.

## API Design

- Add CLI command: `ai-devkit agent kill <name>`
- Resolves `<name>` like `open`/`send`.
- Rejects no-match and ambiguous matches with existing agent-list messaging patterns.
- Calls a reusable service function for process/tmux cleanup.
- Add service function: `killAgent(agent, deps)`
- Inputs: selected `AgentInfo`, `AgentRegistry`, `TmuxManager`, optional signal.
- Behavior: `process.kill(agent.pid, 'SIGTERM')`, then `tmux.killSession(session)` when a session name exists.

## Component Breakdown

| Component | Change |
|-----------|--------|
| `ConsoleAppShell` | Track pending kill confirmation and handle `K`, `y`, `Enter`, `n`, `Esc` |
| New `KillConfirmDialog` | Render centered absolute-positioned confirmation overlay |
| `StatusFooter` | Include `K kill` in key hints |
| `runAction` | Add `kill` action that spawns `agent kill <name>` |
| `agent.service.ts` | Add kill service with tmux cleanup |
| `commands/agent.ts` | Add `agent kill` command |

## Design Decisions

- Keep lowercase `k` as up navigation and use uppercase `K` for kill.
- Require confirmation before invoking the subprocess.
- Render the confirmation as an absolute-positioned overlay so the list, preview, input, and footer keep their existing dimensions.
- Use subprocess actions consistently with existing console `open` and `send`.
- Resolve tmux session from the registry as a fallback because `AgentInfo` may be adapter-derived and not always contain registry metadata.
- Treat missing/already-ended process as non-fatal enough to continue tmux cleanup; the user action intent is to ensure the selected agent is stopped.

## Non-Functional Requirements

- The confirmation overlay must be keyboard-only, absolute-positioned, and not resize the main console panes.
- TUI errors should appear as transient messages.
- Kill must not take over stdin/stdout from Ink.
- Tests should cover both process-only and tmux-backed agents.
93 changes: 93 additions & 0 deletions docs/ai/implementation/2026-06-01-feature-agent-console-kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
phase: implementation
title: Agent Console Kill Implementation
description: Implementation notes for confirmed kill support in agent console
---

# Implementation Guide

## Development Setup

- Feature worktree: `.worktrees/feature-agent-console-kill`
- Branch: `feature-agent-console-kill`
- Dependency bootstrap: `npm ci` completed. Husky prepare could not lock the main repo git config from the sandbox, but dependency install exited successfully.

## Code Structure

- `packages/cli/src/services/agent/agent.service.ts`
- Added `killAgent()` service.
- `packages/cli/src/commands/agent.ts`
- Added `agent kill <name>`.
- `packages/cli/src/tui/console/actions/types.ts`
- Added `kill` console action.
- `packages/cli/src/tui/console/actions/runAction.ts`
- Dispatches kill through a CLI subprocess.
- `packages/cli/src/tui/console/ConsoleApp.tsx`
- Handles uppercase `K`, confirmation state, and kill result messages.
- `packages/cli/src/tui/console/KillConfirmDialog.tsx`
- New Ink confirmation dialog.
- `packages/cli/src/tui/console/StatusFooter.tsx`
- Documents `K kill`.

## Implementation Notes

### Core Features

- `killAgent()` sends `SIGTERM` to the selected agent PID.
- `killAgent()` looks up the registry entry by agent name and kills the stored `tmuxSession` when present.
- `ESRCH` process-kill errors are treated as already stopped so tmux cleanup still runs.
- `agent kill <name>` uses existing list/resolve behavior and reuses the no-match/ambiguous-match messaging style from existing commands.
- `agent console` keeps lowercase `k` navigation and uses uppercase `K` to open a confirmation dialog.
- Confirming with `Enter` or `y` dispatches `agent kill <name>` through `runAction`; cancelling with `Esc` or `n` closes the dialog without side effects.
- `KillConfirmDialog` is rendered inside an absolute-positioned `Box` centered from terminal dimensions, so opening it does not shrink the agent list, preview pane, input box, or footer.

### Patterns & Best Practices

- Console actions remain subprocess-based so the Ink TUI keeps terminal control.
- Tmux cleanup is centralized in the service layer, not the TUI.
- Process killing is dependency-injected in tests via `killProcess`.

## Error Handling

- CLI command resolution failures return visible `ui.error`/`ui.info` messages and do not call `killAgent`.
- Non-`ESRCH` process kill errors propagate through the existing `withErrorHandler` path.
- TUI action failures are shown as transient footer errors.

## Verification Evidence

- `npx ai-devkit@latest lint --feature agent-console-kill` exited 0.
- `npm run build` in `packages/agent-manager` exited 0.
- `npm run build` in `packages/cli` exited 0.
- `npm run lint` in `packages/cli` exited 0.
- `npx vitest run src/__tests__/services/agent/agent.service.test.ts src/__tests__/commands/agent.test.ts src/__tests__/tui/console/actions/runAction.test.ts src/__tests__/tui/console/computeLayout.test.ts` exited 0 with 90 passing tests.

## Phase 6 Implementation Check

### Alignment Status

- Requirements alignment: pass.
- Uppercase `K` opens kill confirmation.
- Lowercase `k` navigation remains unchanged.
- Confirmed kill dispatches through `agent kill <name>`.
- The kill service sends `SIGTERM` and kills registry-backed `tmuxSession` when present.
- Console errors are surfaced as transient messages.
- Design alignment: pass.
- `ConsoleAction` includes `kill`.
- `runAction` spawns `agent kill <name>` with piped stdio.
- `agent kill` resolves agents through `AgentManager.listAgents()` and `resolveAgent()`.
- `killAgent()` centralizes process and tmux cleanup.
- `KillConfirmDialog` is absolute-positioned so it does not resize the main UI panes.

### File-by-File Notes

- `packages/cli/src/services/agent/agent.service.ts`: matches design. `ESRCH` is handled as already stopped, while unexpected process errors still propagate.
- `packages/cli/src/commands/agent.ts`: matches existing command resolution patterns for no agents, no match, ambiguous match, and success.
- `packages/cli/src/tui/console/ConsoleApp.tsx`: matches the keyboard flow. Confirmation handling has priority over normal shortcuts while pending.
- `packages/cli/src/tui/console/KillConfirmDialog.tsx`: matches the overlay design and does not own kill behavior.
- `packages/cli/src/tui/console/actions/*`: matches existing subprocess action pattern.
- `packages/cli/src/tui/console/StatusFooter.tsx`: documents `K kill`.

### Deviations and Follow-Ups

- No blocking deviations found.
- Follow-up for Phase 7: add or manually execute coverage for live Ink key handling (`k`, `K`, `n`/`Esc`, `Enter`/`y`) and the managed tmux smoke test.
57 changes: 57 additions & 0 deletions docs/ai/planning/2026-06-01-feature-agent-console-kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
phase: planning
title: Agent Console Kill Plan
description: Task breakdown for confirmed kill support in agent console
---

# Project Planning & Task Breakdown

## Milestones

- [x] Milestone 1: CLI kill service and command implemented.
- [x] Milestone 2: Console confirmation UI and `K` keybinding implemented.
- [x] Milestone 3: Tests, docs, and verification complete.

## Task Breakdown

### Phase 1: CLI Kill Path
- [x] Task 1.1: Add `killAgent` service that sends `SIGTERM` and kills tmux session when available.
- [x] Task 1.2: Add `agent kill <name>` command with no-match and ambiguous-match handling.
- [x] Task 1.3: Add command/service tests for process kill and tmux cleanup.

### Phase 2: Console Integration
- [x] Task 2.1: Extend console action types and `runAction` with `kill`.
- [x] Task 2.2: Add confirmation overlay component.
- [x] Task 2.3: Wire uppercase `K` to open confirmation while preserving lowercase `k` navigation.
- [x] Task 2.4: Show kill result/error through transient footer messages.

### Phase 3: Verification
- [x] Task 3.1: Update footer/docs hints.
- [x] Task 3.2: Run focused tests.
- [x] Task 3.3: Run build/typecheck for touched packages.

## Dependencies

- Existing `AgentManager.resolveAgent` behavior for name resolution.
- Existing `TmuxManager.killSession` behavior for tmux cleanup.
- Existing console subprocess action pattern.

## Timeline & Estimates

- CLI kill path: 30-45 minutes.
- Console integration: 45-60 minutes.
- Tests and verification: 30-45 minutes.

## Risks & Mitigation

- Risk: agent process exits before kill is invoked.
- Mitigation: tolerate `ESRCH`/already-gone process errors and still attempt tmux cleanup.
- Risk: adapter-derived agent lacks `tmuxSession`.
- Mitigation: fallback to registry lookup by agent name.
- Risk: confirmation input conflicts with chat input.
- Mitigation: ignore `K` while input focus is active and give dialog input handling priority.

## Resources Needed

- Existing CLI and Ink test patterns.
- `TmuxManager` and `AgentRegistry` utilities from `@ai-devkit/agent-manager`.
56 changes: 56 additions & 0 deletions docs/ai/requirements/2026-06-01-feature-agent-console-kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
phase: requirements
title: Agent Console Kill
description: Add confirmed kill support to the interactive agent console
---

# Requirements & Problem Understanding

## Problem Statement

Developers can monitor, open, and message agents from `ai-devkit agent console`, but cannot stop a selected agent from the same interface. Stopping a managed agent currently requires leaving the console and using external process or tmux commands. This is slower and risks leaving the managed tmux session behind.

## Goals & Objectives

- Add a keyboard-driven kill action to `agent console`.
- Use uppercase `K` for kill so lowercase `k` remains upward navigation.
- Show a confirmation pop-up before killing the selected agent.
- Stop the selected agent process.
- If the selected agent has a non-empty `tmuxSession`, kill that tmux session as part of the same action.
- Refresh console state after a successful kill so the stopped agent disappears once discovery/registry pruning observes it.

## Non-Goals

- Do not change lowercase `j/k` navigation.
- Do not add bulk kill support.
- Do not add force/timeout tuning in this feature.
- Do not expose kill for historical sessions.

## User Stories & Use Cases

- As a developer using `agent console`, I want to press `K` on the selected agent and confirm the action so I can stop the agent without leaving the console.
- As a developer using managed tmux agents, I want the tmux session cleaned up when the agent is killed so that no detached tmux session remains.
- As a developer, I want accidental `K` presses to be reversible at the confirmation prompt.

## Success Criteria

- Pressing lowercase `k` still moves the selection up.
- Pressing uppercase `K` with a selected agent opens a confirmation pop-up.
- Confirming the pop-up invokes a kill action for the selected agent.
- Cancelling the pop-up leaves the agent and tmux session untouched.
- The kill action sends a termination signal to the agent PID.
- The kill action kills `tmuxSession` when the selected agent has one.
- The console footer documents `K kill`.
- Errors are shown as transient console errors rather than crashing the TUI.

## Constraints & Assumptions

- The console is an Ink TUI; the confirmation should be implemented with existing Ink components, not an external terminal prompt.
- Existing console actions are subprocess-based through `runAction`; kill should follow the same pattern so the TUI keeps control of the terminal.
- `tmuxSession` is available from `AgentInfo` for registry-backed managed agents.
- A regular `SIGTERM` is the default process stop signal for this feature.
- Memory search was unavailable due to a local `better-sqlite3` Node ABI mismatch, so this scope is based on existing repo docs/code and user clarification.

## Questions & Open Items

- Resolved: shortcut is uppercase `K`, not lowercase `k`.
69 changes: 69 additions & 0 deletions docs/ai/testing/2026-06-01-feature-agent-console-kill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
phase: testing
title: Agent Console Kill Testing
description: Test plan for confirmed kill support in agent console
---

# Testing Strategy

## Test Coverage Goals

- Unit coverage for new kill service branches.
- Command coverage for `agent kill` resolution behavior.
- Console/action coverage for kill action dispatch and confirmation keyboard behavior where practical.
- Manual TUI smoke test for the confirmation overlay.

## Unit Tests

### Agent Kill Service
- [x] Kills the selected agent PID with `SIGTERM`.
- [x] Kills the registry `tmuxSession` when present.
- [x] Falls back to `AgentRegistry.lookup(name).tmuxSession`.
- [x] Continues tmux cleanup when the process is already gone.
- [x] Does not call tmux when no tmux session exists.

### Console Actions
- [x] `runAction({ type: 'kill' })` spawns `agent kill <name>`.
- [x] Existing open/send action behavior is unchanged.

### Console UI
- [ ] Pressing lowercase `k` navigates up.
- [ ] Pressing uppercase `K` opens confirmation for the selected agent.
- [x] Confirmation renders as an overlay without changing computed pane dimensions.
- [x] `Esc`/`n` maps to cancel while kill confirmation is pending.
- [x] `Enter`/`y` maps to confirm while kill confirmation is pending.
- [ ] Full live Ink key handling (`k`, `K`, cancel, confirm) is manually smoke-tested in a TTY.

## Integration Tests

- [x] `agent kill <name>` resolves an exact or unique partial agent and calls the kill service.
- [x] `agent kill <name>` reports no-match and ambiguous-match cases without killing anything.
- [x] `agent console` footer includes `K kill`.

## Manual Testing

- [ ] Start a managed tmux agent with `ai-devkit agent start`.
- [ ] Open `ai-devkit agent console`.
- [ ] Select the agent and press lowercase `k`; verify selection moves up.
- [ ] Press uppercase `K`; verify confirmation appears.
- [ ] Press `n`; verify the agent remains.
- [ ] Press uppercase `K` again, then `Enter`; verify the agent process exits and the tmux session is gone.

## Test Reporting & Coverage

- Run focused package tests for CLI and agent-manager changes.
- Run build or typecheck for touched packages before completion.

## Verification Results

- `npx vitest run src/__tests__/tui/console/hooks/useKillAgentAction.test.ts src/__tests__/tui/console/actions/runAction.test.ts src/__tests__/tui/console/computeLayout.test.ts src/__tests__/commands/agent.test.ts src/__tests__/services/agent/agent.service.test.ts`: exit 0, 97 tests passed.
- `npx vitest run src/__tests__/tui/console/hooks/useKillAgentAction.test.ts src/__tests__/tui/console/actions/runAction.test.ts src/__tests__/services/agent/agent.service.test.ts --coverage --coverage.include=src/tui/console/hooks/useKillAgentAction.ts --coverage.include=src/tui/console/actions/runAction.ts --coverage.include=src/services/agent/agent.service.ts`: exit 0, 36 tests passed, touched-module coverage 84.58% statements / 94.73% branches / 92.85% functions / 84.58% lines.
- `npm run build` in `packages/agent-manager`: exit 0.
- `npm run build` in `packages/cli`: exit 0.
- `npm run lint` in `packages/cli`: exit 0.

## Coverage Notes

- Full CLI coverage with a focused test subset exits non-zero because Vitest includes the entire CLI source tree and enforces global 60% thresholds against unrelated unexecuted modules.
- Scoped coverage for touched kill/action/service modules exits zero.
- The package does not currently include an Ink or React hook test renderer; full keypress behavior remains covered by manual TTY smoke testing.
Loading
Loading