|
| 1 | +# Task State Machine Runbook |
| 2 | + |
| 3 | +Operational guide for the SQLite-backed task state machine used by `memory-access`. |
| 4 | + |
| 5 | +## Scope |
| 6 | + |
| 7 | +This runbook covers: |
| 8 | +- Task lifecycle transitions |
| 9 | +- Lock and dependency troubleshooting |
| 10 | +- Validation queries |
| 11 | +- Migration verification |
| 12 | + |
| 13 | +## Lifecycle |
| 14 | + |
| 15 | +Allowed states: |
| 16 | +- `todo` |
| 17 | +- `in_progress` |
| 18 | +- `blocked` |
| 19 | +- `done` |
| 20 | +- `failed` |
| 21 | +- `canceled` |
| 22 | + |
| 23 | +Allowed transitions are DB-enforced by trigger: |
| 24 | +- `todo -> in_progress|blocked|failed|canceled` |
| 25 | +- `in_progress -> done|blocked|failed|canceled` |
| 26 | +- `blocked -> in_progress|failed|canceled` |
| 27 | +- `done|failed|canceled` are terminal |
| 28 | + |
| 29 | +## Locking Rules |
| 30 | + |
| 31 | +Active locks are stored in `task_locks`. |
| 32 | +Conflicts are DB-enforced for both: |
| 33 | +- exact same resource |
| 34 | +- path-prefix overlap (for example, `src` conflicts with `src/a.py`) |
| 35 | + |
| 36 | +## Troubleshooting |
| 37 | + |
| 38 | +### `LockConflict` |
| 39 | + |
| 40 | +Meaning: lock acquisition violated exact or path-prefix overlap rules. |
| 41 | + |
| 42 | +Actions: |
| 43 | +1. List active locks for the resource prefix. |
| 44 | +2. Release stale locks for completed/canceled tasks. |
| 45 | +3. Retry lock assignment. |
| 46 | + |
| 47 | +### `DependencyNotMet` |
| 48 | + |
| 49 | +Meaning: attempted to transition to `in_progress` while at least one dependency is not `done`. |
| 50 | + |
| 51 | +Actions: |
| 52 | +1. Query dependency states. |
| 53 | +2. Complete or fail dependency tasks. |
| 54 | +3. Retry transition. |
| 55 | + |
| 56 | +### `InvalidTransition` |
| 57 | + |
| 58 | +Meaning: transition is not in allowed state graph. |
| 59 | + |
| 60 | +Actions: |
| 61 | +1. Fetch task state/version. |
| 62 | +2. Use a legal transition path. |
| 63 | + |
| 64 | +### `ConcurrencyConflict` |
| 65 | + |
| 66 | +Meaning: optimistic version check failed (`expected_version` stale). |
| 67 | + |
| 68 | +Actions: |
| 69 | +1. Re-read task row. |
| 70 | +2. Retry transition with latest `version`. |
| 71 | + |
| 72 | +## Validation SQL |
| 73 | + |
| 74 | +All examples assume SQLite shell connected to the target DB. |
| 75 | + |
| 76 | +### Show migration version |
| 77 | + |
| 78 | +```sql |
| 79 | +SELECT MAX(version) AS latest_version FROM schema_versions; |
| 80 | +``` |
| 81 | + |
| 82 | +### Inspect tasks |
| 83 | + |
| 84 | +```sql |
| 85 | +SELECT task_id, status, owner, retry_count, version, updated_at |
| 86 | +FROM tasks |
| 87 | +ORDER BY updated_at DESC |
| 88 | +LIMIT 50; |
| 89 | +``` |
| 90 | + |
| 91 | +### Inspect active locks |
| 92 | + |
| 93 | +```sql |
| 94 | +SELECT id, task_id, resource, active, created_at |
| 95 | +FROM task_locks |
| 96 | +WHERE active = 1 |
| 97 | +ORDER BY resource; |
| 98 | +``` |
| 99 | + |
| 100 | +### Inspect task dependencies + current dependency state |
| 101 | + |
| 102 | +```sql |
| 103 | +SELECT td.task_id, |
| 104 | + td.depends_on_task_id, |
| 105 | + t.status AS depends_on_status |
| 106 | +FROM task_dependencies td |
| 107 | +JOIN tasks t ON t.task_id = td.depends_on_task_id |
| 108 | +ORDER BY td.task_id, td.depends_on_task_id; |
| 109 | +``` |
| 110 | + |
| 111 | +### Inspect recent task events |
| 112 | + |
| 113 | +```sql |
| 114 | +SELECT task_id, event_type, actor, payload, created_at |
| 115 | +FROM task_events |
| 116 | +ORDER BY created_at DESC |
| 117 | +LIMIT 100; |
| 118 | +``` |
| 119 | + |
| 120 | +## Migration Verification Procedure |
| 121 | + |
| 122 | +1. Back up the DB file. |
| 123 | +2. Start the app once to trigger migrations. |
| 124 | +3. Validate: |
| 125 | + - `schema_versions` latest includes the task migrations. |
| 126 | + - Task tables/triggers exist. |
| 127 | +4. Run one smoke transition flow: |
| 128 | + - create task |
| 129 | + - `todo -> in_progress` |
| 130 | + - `in_progress -> done` |
| 131 | + |
| 132 | +## Type Stub Decision |
| 133 | + |
| 134 | +`src/memory_access/orm_models.pyi` is intentionally kept. |
| 135 | +Reason: Peewee field accessors are dynamic; the stub improves static checking and editor hints for `TaskStore` and callers. |
0 commit comments