The new cli uses :db/id as its primary id while the existing mcp uses :block/uuid. These changes on list tools cause slow and sometimes breaking interactions for updates. It also causes buggy/inconsistent mcp behavior b/n mcp cli and server as there are different implentations being called. The new cli should use its own thread-api/* fns and replace MCP when it can meet existing functionality - working updates and provide a CLI interface. Also fix mcp.tools dissoc bug which had been fixed in the newer list fns
10 KiB
Logseq CLI List Default Updated-at Sort Implementation Plan
Goal: Make list page, list tag, and list property behave as if --sort updated-at is provided when users do not pass --sort.
Architecture: Keep the change in the CLI command layer so the existing db-worker-node API surface remains stable.
Architecture: Reuse current CLI-side sorting flow in /Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/command/list.cljs where sorting already runs before offset and limit.
Architecture: Keep db-worker-node list providers in /Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/common/mcp/tools.cljs unchanged for this scope, and document that decision explicitly.
Tech Stack: ClojureScript, babashka.cli option specs, Promesa, Datascript-backed db-worker-node thread-api, existing CLI integration test harness.
Related: Builds on /Users/rcmerci/gh-repos/logseq/docs/agent-guide/004-logseq-cli-verb-subcommands.md and /Users/rcmerci/gh-repos/logseq/docs/agent-guide/043-logseq-cli-tag-property-management.md.
Problem statement
Current list commands support --sort and --order, but default behavior is unsorted because no sort field is applied unless users pass --sort.
In current implementation, list execution fetches items from db-worker-node and runs apply-sort only when :sort is present in options.
This means default output order depends on entity scan order from db-worker-node list functions, which is not aligned with the desired product behavior.
The requested behavior is a consistent default sort key of updated-at for page, tag, and property list subcommands.
Current behavior snapshot
| Layer | File | Current behavior |
|---|---|---|
| CLI command parsing and execution | /Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/command/list.cljs |
:sort is optional and defaults to nil, so apply-sort is skipped when --sort is absent. |
| db-worker-node list thread-api bridge | /Users/rcmerci/gh-repos/logseq/src/main/frontend/worker/db_core.cljs |
:thread-api/cli-list-pages, :thread-api/cli-list-tags, and :thread-api/cli-list-properties return unsorted collections from shared list helpers. |
| Shared list helpers | /Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/common/mcp/tools.cljs |
List helpers filter and shape items but do not apply sort by updated-at. |
| User docs | /Users/rcmerci/gh-repos/logseq/docs/cli/logseq-cli.md |
List command syntax documents --sort as optional but does not state default sort behavior. |
Scope and non-goals
This plan changes default sort behavior for CLI commands list page, list tag, and list property.
This plan does not add new db-worker-node thread-api methods.
This plan does not push pagination or sorting into db-worker-node.
This plan does not change field names or output schemas.
Proposed behavior
When users run logseq list page, logseq list tag, or logseq list property without --sort, CLI should sort by updated-at using the existing list sorting pipeline.
When users pass --sort, the explicit value must override the default.
--order should continue to default to asc unless explicitly set to desc.
When multiple entities have the same primary sort value, CLI should apply :db/id as the deterministic secondary sort key.
offset and limit must still be applied after sorting.
The behavior should be equivalent to explicitly passing --sort updated-at today, with deterministic tie-breaking by :db/id.
Integration overview
logseq list page
-> /Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/command/list.cljs
- determine effective sort field (default updated-at)
- apply CLI-side sort/order
- apply offset/limit/fields
-> /Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/transport.cljs
-> /Users/rcmerci/gh-repos/logseq/src/main/frontend/worker/db_core.cljs
-> /Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/common/mcp/tools.cljs
Testing Plan
I will follow @test-driven-development and add tests before implementation changes.
I will add unit tests for CLI list execution paths that verify default sorting is applied when :sort is missing.
I will add unit tests that verify explicit --sort still overrides the new default.
I will add integration tests that verify default output order matches explicit --sort updated-at output for page, tag, and property list commands.
I will update docs assertions or command help expectations if any existing tests encode old behavior.
NOTE: I will write all tests before I add any implementation behavior.
Detailed implementation plan
- Add a failing unit test in
/Users/rcmerci/gh-repos/logseq/src/test/logseq/cli/commands_test.cljsfor list-page execution where input items are intentionally out of updated-at order and no:sortis provided. - Assert in that test that returned item ids are ordered exactly as if
:sortwere"updated-at"with default:order. - Add a failing unit test for list-tag execution with no
:sortand confirm default updated-at ordering is applied after tag item preparation. - Add a failing unit test for list-property execution with no
:sortand confirm default updated-at ordering is applied after property item preparation. - Add a failing unit test that passes explicit
:sort "title"and confirms explicit sort overrides default updated-at behavior. - Add a failing unit test that passes explicit
:order "desc"without:sortand confirms order is applied to default updated-at sort key. - Add a failing integration test in
/Users/rcmerci/gh-repos/logseq/src/test/logseq/cli/integration_test.cljsthat compareslist pageresults againstlist page --sort updated-atfor the same graph and asserts identical id sequences. - Add a failing integration test that compares
list tagagainstlist tag --sort updated-atand asserts identical id sequences. - Add a failing integration test that compares
list propertyagainstlist property --sort updated-atand asserts identical id sequences. - Run focused tests to verify they fail for the new default-sort behavior and not for unrelated setup issues.
- Implement an
effective-sortdecision in/Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/command/list.cljsso list commands use"updated-at"when:sortis absent. - Reuse the effective sort key for all three executors:
execute-list-page,execute-list-tag, andexecute-list-property. - Keep existing explicit sort validation and allowed sort fields unchanged.
- Add deterministic tie-breaking by
:db/idin CLI list sorting when primary sort values are equal. - Keep existing order default (
asc) unchanged. - Update option descriptions in
/Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/command/list.cljsto clarify default sort behavior for users. - Update docs in
/Users/rcmerci/gh-repos/logseq/docs/cli/logseq-cli.mdto state that list subcommands default to sorting byupdated-at. - Run focused unit and integration tests again and confirm green.
- Run
bb dev:test -v logseq.cli.commands-testto confirm command-level regressions are not introduced. - Run
bb dev:test -v logseq.cli.integration-testfor list command scenarios impacted by ordering. - Run
bb dev:lint-and-testfor final confidence.
Edge cases to cover
Entities with missing :block/updated-at should still be sortable without runtime errors.
Multiple entities with equal updated-at values should be secondarily sorted by :db/id for deterministic output across repeated runs.
--fields filtering should not remove updated-at before sorting is executed.
--offset and --limit should continue to apply after sorting, not before sorting.
--sort with any allowed non-time field should keep existing behavior and take precedence over the new default.
--order desc without explicit --sort should now reverse default updated-at order.
Verification commands
| Command | Expected result |
|---|---|
bb dev:test -v logseq.cli.commands-test/test-list-subcommand-parse |
Existing parse behavior remains valid and list options remain accepted. |
bb dev:test -v logseq.cli.commands-test |
New default-sort unit tests pass and no command-level regressions are introduced. |
bb dev:test -v logseq.cli.integration-test |
Integration checks for list default ordering pass against a real db-worker-node flow. |
bb dev:lint-and-test |
Full lint and unit suite pass with zero errors. |
Rollout and compatibility
This is a behavior change in default ordering for three CLI list commands.
Scripts that depended on previous implicit scan order may observe changed item order.
Scripts that already pass explicit --sort remain unaffected.
No db-worker-node API contract change is introduced in this scope.
Testing Details
Tests verify user-visible command behavior by comparing result ordering between default list calls and explicit --sort updated-at calls.
Tests validate override behavior so explicit sort fields still control final ordering.
Tests validate order plus pagination interaction to ensure behavior consistency.
Implementation Details
- Update default sort selection in
/Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/command/list.cljsfor page, tag, and property list executors. - Keep existing
apply-sort,apply-offset-limit, andapply-fieldssequencing unchanged. - Extend CLI sort implementation to apply
:db/idas secondary key when primary sort values are equal. - Reuse existing
list-*-field-mapentries forupdated-atso no new field mapping is introduced. - Keep db-worker-node list handlers in
/Users/rcmerci/gh-repos/logseq/src/main/frontend/worker/db_core.cljsunchanged in this scope. - Keep shared list helper behavior in
/Users/rcmerci/gh-repos/logseq/src/main/logseq/cli/common/mcp/tools.cljsunchanged in this scope. - Add command-level unit coverage in
/Users/rcmerci/gh-repos/logseq/src/test/logseq/cli/commands_test.cljs. - Add end-to-end list ordering coverage in
/Users/rcmerci/gh-repos/logseq/src/test/logseq/cli/integration_test.cljs. - Update user-facing list docs in
/Users/rcmerci/gh-repos/logseq/docs/cli/logseq-cli.md.
Question
No open questions.