Commit 287144f
authored
feat(session list): per-key sort direction for --sort / ?order_by (#777)
Follow-up to #739. On that PR, @cpcloud asked whether sort direction could be specified per key rather than forcing one direction for all sort keys, given the `SessionFilter` abstraction. This implements that.
## What changed
`order_by` (and `--sort`) now accept a comma-separated list of sort keys, each optionally suffixed `:asc` or `:desc`:
```
GET /api/v1/sessions?order_by=messages:desc,started:asc
agentsview session list --sort messages:desc,started:asc
```
Different keys can sort in different directions. A key with no suffix takes its direction from the `descending` param (HTTP) or `--reverse` (CLI), then from its own natural default; an explicit `:asc`/`:desc` always wins. The single-key forms (`order_by=messages&descending=true`, `--sort messages -r`) behave exactly as before, and existing pagination cursors keep working.
## How it works
`SessionFilter` gains a structured `Sort []SortKey` (each key plus an optional direction); `OrderBy`/`Descending` remain as the single-key shorthand and parse fallback. `OrderByClause` and `CursorPredicate` now render N columns from one definition shared by SQLite, PostgreSQL, and DuckDB.
The keyset pagination predicate becomes the lexicographic OR-expansion -- `(c1 OPdir1 v1) OR (c1 = v1 AND c2 OPdir2 v2) OR ...` -- because a single row-value tuple comparison is only valid when every column shares one direction; mixed per-key directions require the expansion. The `id` tie-breaker follows the last sort term's direction (appended unless `id` is already a term). Cursors carry a per-key `Keys` list; legacy and single-key cursors still decode, and a cursor reused under a different sort list (different keys, order, or any direction) is rejected as an invalid cursor rather than silently paging wrong rows.
The `order_by` enum was replaced by free-form parsing with server-side validation (unknown key, bad direction token, duplicate key, empty term -> 400), shared by the list and sidebar-index routes; the valid keys are enumerated in the param doc.
## Where to look
- `internal/db/sort.go` -- sort registry, `ParseSortSpec`/`ResolveSort`, cursor helpers
- `internal/db/query_dialect.go` -- `OrderByClause` / `CursorPredicate` OR-expansion
- the three `ListSessions` implementations (`internal/db`, `internal/postgres`, `internal/duckdb`)
- `internal/server/huma_routes_sessions.go`, `internal/service/direct.go`, `cmd/agentsview/session_list.go`
## Limitations / possible follow-ups
- No new indexes (same scoping as #739); at local-archive scale a scan+sort is sub-millisecond.
- The CLI has no `--descending` flag mirroring HTTP's absolute `descending` fallback -- `--reverse` flips each bare key's own natural direction instead. Easy to add if transport symmetry is wanted.
Co-authored-by: Matthew Jacobs <mjacobs@users.noreply.github.com>1 parent 1ba339b commit 287144f
15 files changed
Lines changed: 1141 additions & 111 deletions
File tree
- cmd/agentsview
- internal
- db
- duckdb
- postgres
- server
- service
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
65 | 64 | | |
66 | 65 | | |
67 | 66 | | |
68 | 67 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
74 | 88 | | |
| 89 | + | |
75 | 90 | | |
76 | 91 | | |
77 | 92 | | |
| |||
129 | 144 | | |
130 | 145 | | |
131 | 146 | | |
132 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
133 | 150 | | |
134 | | - | |
| 151 | + | |
| 152 | + | |
135 | 153 | | |
136 | 154 | | |
137 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
56 | 112 | | |
57 | 113 | | |
58 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
253 | 253 | | |
254 | 254 | | |
255 | 255 | | |
256 | | - | |
257 | | - | |
258 | | - | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
259 | 260 | | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
268 | 266 | | |
269 | | - | |
| 267 | + | |
270 | 268 | | |
271 | 269 | | |
272 | 270 | | |
273 | | - | |
274 | | - | |
275 | | - | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
276 | 282 | | |
277 | | - | |
| 283 | + | |
278 | 284 | | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
283 | 306 | | |
284 | | - | |
285 | | - | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
286 | 313 | | |
287 | | - | |
288 | | - | |
289 | | - | |
| 314 | + | |
290 | 315 | | |
291 | 316 | | |
292 | 317 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
341 | 341 | | |
342 | 342 | | |
343 | 343 | | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
344 | 371 | | |
345 | 372 | | |
346 | 373 | | |
| |||
443 | 470 | | |
444 | 471 | | |
445 | 472 | | |
446 | | - | |
447 | | - | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
448 | 481 | | |
449 | | - | |
| 482 | + | |
| 483 | + | |
450 | 484 | | |
451 | 485 | | |
452 | 486 | | |
| |||
560 | 594 | | |
561 | 595 | | |
562 | 596 | | |
563 | | - | |
564 | | - | |
| 597 | + | |
565 | 598 | | |
566 | 599 | | |
567 | 600 | | |
| |||
591 | 624 | | |
592 | 625 | | |
593 | 626 | | |
594 | | - | |
| 627 | + | |
595 | 628 | | |
596 | 629 | | |
597 | 630 | | |
598 | 631 | | |
599 | | - | |
| 632 | + | |
600 | 633 | | |
601 | 634 | | |
602 | 635 | | |
603 | 636 | | |
604 | 637 | | |
605 | | - | |
| 638 | + | |
606 | 639 | | |
607 | 640 | | |
608 | 641 | | |
| |||
623 | 656 | | |
624 | 657 | | |
625 | 658 | | |
626 | | - | |
| 659 | + | |
627 | 660 | | |
628 | 661 | | |
629 | 662 | | |
| |||
0 commit comments