# T3-2.1: CLI Task Commands - Core

## Priority
High

## Status
Completed

## Description
Implement core task management commands for the CLI: add, list, show, edit, done, and rm. These are the fundamental CRUD operations needed for basic task management.

## Objectives
- Create `src/cli/commands/task.py` module
- Implement `anyt task add` to create new tasks
- Implement `anyt task list` with filtering
- Implement `anyt task show` for task details
- Implement `anyt task edit` for inline updates
- Implement `anyt task done` to mark tasks complete
- Implement `anyt task rm` to delete tasks
- Rich formatted output with colors and tables
- Workspace context integration

## Commands

### anyt task add
```bash
$ anyt task add "Implement OAuth callback"
Created: DEV-42 (Implement OAuth callback)

$ anyt task add "Add tests" -d "Unit and integration tests" -p 1 --labels test,backend
Created: DEV-43 (Add tests)

# Options:
# -d, --description TEXT  - Task description
# -p, --priority INT      - Priority (-2 to 2, default: 0)
# --labels TEXT           - Comma-separated labels
# --status TEXT           - Status (default: backlog)
# --owner TEXT            - Assign to user or agent
# --estimate INT          - Time estimate in hours
```

### anyt task list (alias: anyt ls)
```bash
# List all tasks
$ anyt task list
ID      Title                        Status      Priority  Updated
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
DEV-42  Implement OAuth callback     inprogress  ●●○       2h ago
DEV-43  Add tests                    backlog     ●○○       1h ago
DEV-44  Fix login bug                backlog     ○○○       30m ago
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
3 tasks

# Filter by status
$ anyt task list --status inprogress,todo

# Filter by owner
$ anyt task list --mine

# Filter by labels
$ anyt task list --labels auth,backend

# Sort and limit
$ anyt task list --sort updated_at --order desc --limit 10
```

### anyt task show
```bash
$ anyt task show DEV-42

DEV-42: Implement OAuth callback
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Status: inprogress    Priority: ●●○ (1)
Owner: you            Labels: auth, backend
Project: Backend      Estimate: 5h

Description:
Add OAuth 2.0 callback handler for Google, GitHub, and Microsoft.

Created: 2 days ago
Updated: 2 hours ago
Version: 5
```

### anyt task edit
```bash
# Edit specific fields
$ anyt task edit DEV-42 --status done
✓ Updated DEV-42

$ anyt task edit DEV-42 --title "Refactor OAuth callback" --priority 2
✓ Updated DEV-42

$ anyt task edit DEV-42 --labels auth,backend,oauth
✓ Updated DEV-42
```

### anyt task done
```bash
$ anyt task done DEV-42
✓ Marked DEV-42 as done

# Alias
$ anyt done DEV-42
✓ Marked DEV-42 as done
```

### anyt task rm
```bash
$ anyt task rm DEV-42
? Delete task DEV-42 (Implement OAuth callback)? (y/N) y
✓ Deleted DEV-42

# Skip confirmation
$ anyt task rm DEV-42 --force
✓ Deleted DEV-42
```

## Acceptance Criteria
- [x] Module created at `src/cli/commands/task.py`
- [x] `anyt task add` creates tasks with all options (title, description, priority, labels, status, owner, estimate)
- [x] `anyt task list` displays tasks in formatted table
- [x] `anyt task list` supports filtering by --status, --mine, --labels
- [x] `anyt task list` supports sorting (--sort, --order) and pagination (--limit, --offset)
- [x] `anyt task show` displays full task details with rich formatting
- [x] `anyt task edit` updates tasks with inline field editing
- [x] `anyt task done` marks tasks as complete (status=done)
- [x] `anyt task rm` soft-deletes tasks with confirmation prompt
- [x] `anyt task rm --force` skips confirmation
- [x] All commands respect workspace context from .anyt/workspace.json
- [x] Commands require workspace initialization (error if not in .anyt/ directory)
- [x] Rich formatted output with colors, tables, and symbols
- [x] Error messages are clear and actionable
- [x] Commands registered in main CLI (`src/cli/main.py`)
- [x] Basic error handling for API failures (network, 404, 401, 403)

## Dependencies
- T3-2: CLI Task Commands - Foundation (API client methods) ✅
- T3-1: CLI Foundation & Setup ✅
- T2-1: Task CRUD API ✅

## Estimated Effort
6-8 hours

## Technical Notes

### Module Structure
```python
# src/cli/commands/task.py
import typer
from rich.console import Console
from rich.table import Table

from cli.client import APIClient
from cli.config import GlobalConfig, WorkspaceConfig

app = typer.Typer(help="Manage tasks")
console = Console()

@app.command("add")
def add_task(...):
    """Create a new task."""
    pass

@app.command("list")
def list_tasks(...):
    """List tasks with filtering."""
    pass

# ... more commands
```

### Workspace Context
- Check for `.anyt/workspace.json` at startup
- Load workspace_id from config
- Pass workspace_id to API client calls
- Show helpful error if not initialized

### Rich Formatting
- Use `rich.table.Table` for task lists
- Use `rich.console.Console` for colored output
- Status symbols: ✓ (done), ● (priority dots), etc.
- Truncate long descriptions in list view
- Relative timestamps (2h ago, 1d ago)

### Error Handling
```python
try:
    task = await client.get_task(identifier)
except httpx.HTTPStatusError as e:
    if e.response.status_code == 404:
        console.print(f"[red]Error:[/red] Task '{identifier}' not found")
        raise typer.Exit(1)
    elif e.response.status_code == 401:
        console.print("[red]Error:[/red] Not authenticated")
        console.print("Run [cyan]anyt auth login[/cyan] first")
        raise typer.Exit(1)
    # ... handle other errors
```

### Registration
Add to `src/cli/main.py`:
```python
from cli.commands import task as task_commands

app.add_typer(task_commands.app, name="task")
```

## Testing
- Manual testing with local dev server
- Test workspace context detection
- Test error cases (not initialized, not authenticated, task not found)
- Test all filters and options
- Verify rich formatting displays correctly

## Out of Scope
- Dependency management commands (→ T3-2.2)
- Task picker / active task (→ T3-2.3)
- JSON output mode (→ T3-2.4)
- Optimistic concurrency control (→ T3-2.4)
- Fuzzy task ID matching (→ T3-2.4)
- Bulk operations (→ T3-2.4)
- Interactive editing with $EDITOR (future enhancement)

## Events

### 2025-10-15 20:20 - Started work
- Moved task from backlog to active
- Beginning implementation of core task commands
- Will implement commands: add, list, show, edit, done, rm

### 2025-10-15 21:00 - Implementation completed
- Created `src/cli/commands/task.py` with all core commands
- Implemented all 6 commands: add, list, show, edit, done, rm
- Added rich formatting with tables, colors, and symbols
- Implemented workspace context checking and auth validation
- Added filtering support for list command (status, mine, labels)
- Added sorting and pagination support
- Implemented confirmation prompt for rm command with --force flag
- Registered commands in main.py
- All code passes linting, formatting, and type checking
- Ready for manual testing

### 2025-10-15 21:05 - PR created
- Committed changes with descriptive message
- Pushed to branch T3-2-cli-task-commands
- Created PR #21: https://github.com/supercarl87/AnyTaskBackend/pull/21
- Task moved to done/
