Goal
Add a persistent, deterministic, and efficient custom ordering mechanism for work items using an integer sort_index and supporting CLI and TUI reordering operations.
Motivation Current ordering (priority + creation time) does not capture execution order or producer intent. Contributors and producers need a way to persist and share custom ordering across the team and views.
Scope (in)
- Add integer
sort_indexcolumn to the work items table - DB migration to populate initial
sort_indexvalues using existingnextlogic - CLI commands:
wl move <id> --before <id>,wl move <id> --after <id>, andwl move autofor redistribution wl listandwl nextdefault ordering updated to usesort_indexunless--sortis provided- TUI: interactive reordering with keyboard shortcuts (move up/down, move to before/after selection)
- Reindexing strategy and
synchooks to resolve conflicts
Out of scope
- Deep UI redesign beyond minimal TUI keyboard handlers
- Remote collaborative real-time editing (beyond git-based sync resolution)
Success criteria (testable)
wl listandwl nextreturn items ordered bysort_indexby defaultwl movecommands persist newsort_indexvalues in DB and maintain hierarchical grouping (parent+children)- Adding new items inserts them at the correct hierarchy level using the
nextlogic - Reindexing preserves relative ordering and is triggered only when gaps exhausted
- Migration preserves current importance/next semantics; restore via backup if needed
- Performance: ordering operations and queries remain acceptable for up to 1000 items per level
Constraints
- Backwards compatible CLI behavior;
--sortoverridessort_index - Use large interval gaps (e.g., 100) between adjacent
sort_indexvalues to limit reindexing frequency - Migration must support backups; rollback helper is out of scope for this phase
Design
-
Data model
- Add
sort_index INTEGER NOT NULL DEFAULT 0towork_itemstable - Add an index on
sort_indexand(parent_id, sort_index)for hierarchical queries
- Add
-
Initial sort_index calculation
- Use existing
nextselection logic applied across the tree: level-0 items ordered first, then each parent's children - Assign starting values in increments of 100 (configurable constant SORT_GAP = 100)
- Use existing
-
Move operations
wl move <id> --before <id2>: assign newsort_indexbetween predecessor and target; if gap < MIN_GAP, trigger redistribution for that levelwl move auto: evenly redistributesort_indexvalues across the affected level using SORT_GAP- Parent moves bring child subtree along: moving a parent moves whole group maintaining relative gaps
-
Sync and conflict resolution
- On
wl sync, detect conflictingsort_indexvalues (same values or reversed) and run a deterministic merge: prefer largerupdated_at, then owner, then fallback to redistributing that level
- On
Migration plan
- Add migration: create
sort_indexcolumn and index - Populate values in a single transaction where possible; if SQLite, use a migration script that runs within a transaction
- Include a reversible path via backup export before migration (no rollback helper in this phase)
Testing and validation
- Unit tests for helpers that compute insertion index and detect gap exhaustion
- Integration tests for
wl movecommands (before/after/auto) asserting DB state andwl listoutput - Migration test: fixture DB with representative items -> run migration -> assert preserved ordering
- Performance test: generate 1000 items per level and measure list/query latency
Milestones & child work items
- PRD and planning (this document) — WL-0MKXFC2600PRVAOO (current)
- DB migration & model changes — child work item: "Add sort_index column and migration" (high)
- Core ordering logic &
wl list/wl nextchanges — child work item: "Apply sort_index ordering to list/next" (high) - CLI
wl movecommands — child work item: "Implement wl move CLI" (high) - Reindexing &
wl move auto— child work item: "Implement reindex and auto-redistribute" (medium) - TUI reordering support — child work item: "TUI interactive reorder" (medium)
- Tests & benchmarks — child work item: "Sort order tests and perf benchmarks" (high)
- Docs & rollout guide — child work item: "Docs: sort order and migration guide" (medium)
Risks & mitigations
- Migration corruption: mitigate by backup and dry-run mode
- Concurrent edits: mitigate by deterministic conflict resolution and reindex on sync
- Gap exhaustion in hotspots: mitigate by
wl move autoand periodic reindexing
Open questions
- Default SORT_GAP value — recommended 100 (configurable). If you prefer a different default say so.
- Should
wl moveaccept a fractional-based approach (e.g., using decimals) instead of integer gaps? Recommendation: use integers to avoid float precision and make conflicts explicit.
Acceptance test checklist (for reviewers)
- PRD merged into repo
- Child work items created and linked
- Example migration script included or referenced
- Tests added for ordering and migration (placeholder test files created)
Rollout
- Stage: implement migration and core logic; run on a staging DB; validate ordering; then release with docs and migration helper.
Appendix: references
- src/database.ts
- src/commands/list.ts
- src/commands/next.ts
- src/commands/helpers.ts