---
name: default-config
description: Auto-create and maintain config.toml with defaults and comments
status: done
parts:
- src/types/task.rs
- src/storage.rs
- src/servers/task.rs
- src/servers/unified.rs
estimate:
  optimistic: 2 hours
  normal: 3 hours
  pessimistic: 5 hours
---

# Implementation Plan

## Task Summary

| ID | Task | Files |
|----|------|-------|
| T1 | Add `to_toml_with_comments()` method to ProjectConfig | `src/types/task.rs` |
| T2 | Add `ensure_config_initialized()` method to StateManager | `src/storage.rs` |
| T3 | Add config migration logic to merge missing settings | `src/storage.rs` |
| T4 | Call config initialization from task operations and return notification | `src/servers/task.rs`, `src/servers/unified.rs` |
| T5 | Add tests for config initialization and migration | `src/storage.rs`, `src/types/task.rs` |

## Tasks

- [ ] **T1**: Add `to_toml_with_comments()` method to `ProjectConfig` that generates a TOML string with inline comments explaining each setting. Include all available settings with their defaults.

- [ ] **T2**: Add `ensure_config_initialized()` method to `StateManager` that:
  - Creates config.toml with defaults if it doesn't exist
  - Returns `(ProjectConfig, bool)` where bool indicates if file was created/modified

- [ ] **T3**: Add config migration logic in `ensure_config_initialized()`:
  - If config exists, parse it and check for missing settings
  - Merge missing settings with defaults (preserve existing values)
  - Use TOML parsing to preserve comments where possible
  - Re-write file only if changes were needed

- [ ] **T4**: Integrate config initialization into task operations:
  - Call `ensure_config_initialized()` at start of operations
  - Include notification in response when config was created/updated
  - Operations: `list_project_tasks`, `create_task`, `analyze_task`, `get_task_status`, etc.

- [ ] **T5**: Add comprehensive tests:
  - Test config creation from scratch
  - Test config migration with missing settings
  - Test that existing settings are preserved
  - Test comment generation

## Testing Considerations

- Test with no config file → should create with all defaults and comments
- Test with partial config → should add missing settings, preserve existing
- Test with complete config → should not modify file
- Test that user-set values like `feature_approval_mode = "auto"` are never overwritten
- Verify comments are present and helpful

## Technical Notes

### Comment Format
```toml
# K4000 Project Configuration
# This file controls K4000 behavior for all tasks in this project.

# Feature approval mode controls whether the agent waits for human approval
# between features during implementation.
#
# Values:
#   - "manual" (default): Agent pauses after each feature and asks user
#                         for approval before starting the next feature
#   - "auto": Agent proceeds to the next feature automatically
#
feature_approval_mode = "manual"
```

### Migration Safety
- Parse existing TOML to preserve structure
- Only add missing keys, never modify existing
- Use `toml_edit` crate if needed for preserving formatting (or manual string manipulation)

---

## Estimate

**Senior Developer Profile:** 5+ years experience, familiar with Rust and TOML handling.

| Scenario | Time |
|----------|------|
| Optimistic | 2 hours |
| Normal | 3 hours |
| Pessimistic | 5 hours |

### Assumptions
- The `toml` crate can handle basic serialization with comments (may need `toml_edit` for advanced preservation)
- No breaking changes to existing config behavior
- Limited number of settings to document (currently just `feature_approval_mode`)
