Skip to content

feat: add target-aware reader user state - #1053

Merged
Sinity merged 5 commits into
masterfrom
feature/feat/user-state-annotations-targets
May 15, 2026
Merged

Sinity merged 5 commits into
masterfrom
feature/feat/user-state-annotations-targets

Conversation

@Sinity

@Sinity Sinity commented May 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

Add the next #867 durable reader-state slice: target-aware marks, first-class annotations, CLI parity, daemon HTTP/MCP exposure, and the schema upgrade needed to preserve existing conversation marks while supporting message targets.

Problem

The reader/user-state work merged so far exposed conversation-level marks and saved views through daemon, reader, and MCP surfaces, but #867 still required message targets, annotations, CLI parity, and proof that user metadata remains outside archive content hashes. The old user_marks primary key could not represent message-level marks, and annotations were still advertised as unavailable in reader action payloads.

Solution

  • Bump archive schema to v13 and migrate legacy user_marks into target-aware (target_type, target_id, mark_type) rows.
  • Add durable user_annotations storage for conversation/message targets.
  • Extend SQL helpers, repository writes, and the Polylogue facade for target-aware marks and annotation CRUD.
  • Add daemon /api/user/annotations routes and widen /api/user/marks to conversation/message targets.
  • Add MCP annotation tools and target-aware mark parameters, with regenerated tool-schema witness.
  • Add lazy polylogue user-state CLI commands for marks, annotations, and saved views.
  • Flip reader action availability for annotation actions now that the substrate route exists.
  • Add storage tests for migration, target validation, and content-hash exclusion; update daemon/MCP/CLI/visual evidence.

Remaining #867 scope after this PR: recall-pack export/read model that cites marks/annotations/messages with explicit degraded-target states, plus fuller reader UI workflows for editing annotations inline.

Ref #867

Verification

  • pytest -q tests/unit/storage/test_user_state_contracts.py tests/unit/storage/test_schema_upgrades.py::test_ensure_schema_upgrades_v12_marks_to_target_aware_user_state tests/unit/storage/test_schema_upgrades.py::test_v12_to_v13_upgrade_plan_migrates_legacy_mark_table tests/unit/daemon/test_web_reader.py tests/unit/mcp/test_user_state_tools.py tests/unit/mcp/test_envelope_contracts.py tests/unit/mcp/test_tool_schema_witness.py tests/visual/test_reader_dom_smoke.py -> 89 passed
  • pytest -q tests/unit/cli/test_user_state_command.py tests/unit/cli/test_click_app.py::TestCliMetadata::test_all_subcommands_registered -> 5 passed
  • pytest -q tests/unit/storage/test_backend.py::test_ensure_schema_upgrades_v2_message_types_without_reimport tests/unit/storage/test_backend.py::test_ensure_schema_upgrades_v7_by_dropping_run_ledger tests/unit/storage/test_backend.py::test_ensure_schema_upgrades_v2_already_extended_without_overwriting tests/unit/storage/test_backend.py::test_async_backend_upgrades_v2_message_types_without_reimport tests/unit/storage/test_provider_events_schema.py::test_ensure_schema_upgrades_v6_provider_meta_compactions_to_provider_events tests/unit/storage/test_schema_safety.py::TestMigrationRollbackSafety::test_v2_to_v3_upgrade_rolls_back_on_failure tests/unit/storage/test_schema_upgrades.py::test_ensure_schema_upgrades_v8_by_indexing_message_foreign_keys tests/unit/storage/test_action_fts_schema_upgrade.py::test_ensure_schema_upgrades_v3_action_fts_rowids_without_reimport -> 8 passed
  • pytest -q tests/unit/cli/test_terminal_snapshots.py --snapshot-update -> 9 passed
  • devtools verify --quick -> exit_code 0
  • devtools verify --affected --skip-slow -> structural gates passed; pytest ran 6,384 tests with one daemon timeout in test_search_with_bad_date_returns_query_error; exact rerun passed: pytest -q tests/unit/daemon/test_web_reader.py::TestQueryNoResultsDiagnosticPath::test_search_with_bad_date_returns_query_error -> 1 passed

Summary by CodeRabbit

Release Notes

  • New Features
    • Added user-state CLI command for managing marks, annotations, and saved views across conversations and individual messages.
    • Introduced annotations feature: save and organize text notes on conversations or messages.
    • Expanded marks to support message-level targeting alongside conversation-level marks.
    • Enabled the "annotate" reader action in conversation and message interfaces.

Review Change Stack

Sinity added 5 commits May 15, 2026 17:02
Problem: #867 still had durable reader state gaps after marks and saved views reached daemon, reader, and MCP surfaces. Marks were conversation-only, annotations were explicitly unavailable, and the schema could not represent message-target user state.

What changed: bump the archive schema to v13, migrate legacy conversation marks into target-aware rows, add durable annotation storage, expose conversation/message targets through Polylogue, daemon HTTP, and MCP, and update focused storage/daemon/MCP/visual evidence.

Verification: pytest -q tests/unit/storage/test_user_state_contracts.py tests/unit/storage/test_schema_upgrades.py::test_ensure_schema_upgrades_v12_marks_to_target_aware_user_state tests/unit/storage/test_schema_upgrades.py::test_v12_to_v13_upgrade_plan_migrates_legacy_mark_table tests/unit/daemon/test_web_reader.py tests/unit/mcp/test_user_state_tools.py tests/unit/mcp/test_envelope_contracts.py tests/unit/mcp/test_tool_schema_witness.py tests/visual/test_reader_dom_smoke.py -> 89 passed. ruff format --check/ruff check/python -m mypy on changed files passed.

Ref #867
Problem: #867 durable reader state had daemon, reader, and MCP access, but operators still lacked CLI parity for marks, annotations, and saved views.

What changed: add a lazy user-state root command with mark, annotation, and saved-view subcommands; validate saved-view query JSON through ConversationQuerySpec; render the new CLI reference section; and add focused CLI registration/behavior tests.

Verification: pytest -q tests/unit/cli/test_user_state_command.py tests/unit/cli/test_click_app.py::TestCliMetadata::test_all_subcommands_registered -> 5 passed. ruff check and python -m mypy on changed CLI files passed.

Ref #867
Problem: adding the user-state CLI command changes generated command/proof/topology projections, so render-all --check required the committed surfaces to be refreshed.

What changed: update generated quality workflow, topology status, and verification catalog counts for the new user-state command.

Verification: devtools render-all --check -> sync OK.

Ref #867
Problem: the user-state branch added routes and a CLI module that made verify-topology and verify-file-budgets fail.

What changed: register the CLI module in the topology projection, refresh topology status, move user-state HTTP route dispatch into the user-state handler module to keep daemon/http.py under budget, and update the explicit schema-bootstrap file budget for the v13 migration branch.

Verification: devtools verify-topology, devtools verify-file-budgets, python -m mypy polylogue/daemon/http.py polylogue/daemon/user_state_http.py, pytest -q tests/unit/daemon/test_web_reader.py::TestReaderUserState, devtools render-all --check.

Ref #867
Problem: affected pytest showed that extending older schema upgrade paths to current made v11->v12 cost-column SQL run against minimal historical fixtures without messages/session_profiles, and the new user-state root command changed terminal help snapshots.

What changed: make v11->v12 cost-column statements conditional on the target tables, avoid attachment index promotion when the referenced attachments table is absent, and refresh terminal snapshots for the user-state command.

Verification: focused failed migration tests -> 8 passed; pytest -q tests/unit/cli/test_terminal_snapshots.py --snapshot-update -> 9 passed; ruff check and mypy for schema_bootstrap passed.

Ref #867
@coderabbitai

coderabbitai Bot commented May 15, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 26d0a269-afaa-4945-b70e-b338343134c4

📥 Commits

Reviewing files that changed from the base of the PR and between 2face55 and 4de162d.

📒 Files selected for processing (34)
  • devtools/render_cli_reference.py
  • docs/cli-reference.md
  • docs/plans/file-size-budgets.yaml
  • docs/plans/mk3-execution-log.md
  • docs/plans/topology-target.yaml
  • docs/test-quality-workflows.md
  • docs/topology-status.md
  • docs/verification-catalog.md
  • polylogue/api/archive.py
  • polylogue/cli/click_command_registration.py
  • polylogue/cli/commands/user_state.py
  • polylogue/daemon/http.py
  • polylogue/daemon/user_state_http.py
  • polylogue/mcp/payloads.py
  • polylogue/mcp/server_mutation_tools.py
  • polylogue/storage/repository/archive/repository_writes.py
  • polylogue/storage/sqlite/queries/conversations.py
  • polylogue/storage/sqlite/queries/conversations_identity.py
  • polylogue/storage/sqlite/schema.py
  • polylogue/storage/sqlite/schema_bootstrap.py
  • polylogue/storage/sqlite/schema_ddl.py
  • polylogue/storage/sqlite/schema_ddl_archive.py
  • polylogue/surfaces/payloads.py
  • tests/data/witnesses/mcp-tool-schemas.json
  • tests/infra/mcp.py
  • tests/unit/cli/__snapshots__/test_terminal_snapshots.ambr
  • tests/unit/cli/test_click_app.py
  • tests/unit/cli/test_user_state_command.py
  • tests/unit/daemon/test_web_reader.py
  • tests/unit/mcp/test_envelope_contracts.py
  • tests/unit/mcp/test_user_state_tools.py
  • tests/unit/storage/test_schema_upgrades.py
  • tests/unit/storage/test_user_state_contracts.py
  • tests/visual/conftest.py

📝 Walkthrough

Walkthrough

This PR implements comprehensive durable user-state management (marks, annotations, saved-views) across the polylogue system. It introduces a new user-state CLI command, extends the API/HTTP/MCP layers, upgrades the SQLite schema from v12 to v13 to support target-aware (conversation and message) user operations, and provides extensive test coverage.

Changes

User-State Feature Implementation

Layer / File(s) Summary
CLI user-state command
polylogue/cli/commands/user_state.py
New Click command group implementing marks, annotations, and saved-views subcommands with list/add/remove/save/delete operations, JSON/plain output, input validation, and deterministic saved-view ID derivation via SHA-256 hashing.
CLI registration and help
polylogue/cli/click_command_registration.py, tests/unit/cli/*
Registers user-state with root Click app, updates help snapshots across terminal widths, adds subcommand assertions in CLI tests.
Archive API layer
polylogue/api/archive.py
Extends PolylogueArchiveMixin with target-aware mark operations (add/remove/list with target_type/target_id/message_id), annotation CRUD (save/get/list/delete), and shared _resolve_user_state_target resolver validating message membership.
Repository wrappers
polylogue/storage/repository/archive/repository_writes.py
RepositoryWriteMixin wrapper methods for marks and annotations, delegating to SQLite queries with UTC timestamp computation for saves.
SQLite mark and annotation queries
polylogue/storage/sqlite/queries/conversations_identity.py
Target-aware add_mark/remove_mark/list_marks with filtering, and complete annotation CRUD (save_annotation upsert, get_annotation, list_annotations with filters, delete_annotation).
Queries module exports
polylogue/storage/sqlite/queries/conversations.py
Re-exports annotation functions from conversations_identity to surface public API.
SQLite schema v12→v13 upgrade
polylogue/storage/sqlite/schema_bootstrap.py
build_v12_to_v13_upgrade_plan migrates user_marks to target-aware layout (rename legacy, recreate, map conversation targets), creates user_annotations table with conditional checks.
SQLite schema DDL
polylogue/storage/sqlite/schema_ddl_archive.py, schema_ddl.py
Defines target-aware USER_MARKS_DDL and new USER_ANNOTATIONS_DDL with target_type/target_id/message_id columns, CHECK constraints, and indexes; integrates into composed SCHEMA_DDL and bumps version to 13.
MCP payloads
polylogue/mcp/payloads.py
Pydantic models: MCPUserMarkPayload (extended with target_type, target_id, message_id), MCPUserAnnotationPayload (annotation_id, target reference, note_text, timestamps), and MCPUserAnnotationListPayload.
MCP mutation tools
polylogue/mcp/server_mutation_tools.py
Extended list_marks/add_mark/remove_mark with target fields; added list_annotations/save_annotation/delete_annotation tools with payload helpers (_annotation_payload, _none_if_empty).
HTTP daemon routing
polylogue/daemon/http.py, user_state_http.py
Consolidated /api/user/* routing delegates to dispatch_get/dispatch_post/dispatch_delete; user_state_http implements handlers for marks and annotations, _default_annotation_id generation.
Reader action contracts
polylogue/surfaces/payloads.py
Enables annotate action for reader_conversation_actions and reader_message_actions.
MCP tool schemas and test fixtures
tests/data/witnesses/mcp-tool-schemas.json, tests/infra/mcp.py
Adds argument schemas for extended mark tools and new annotation tools; extends EXPECTED_TOOL_NAMES and make_polylogue_mock() with stubs.
Unit tests: CLI
tests/unit/cli/test_user_state_command.py
Tests CLI contracts for marks add with message targeting, annotations list, saved-views save with canonicalization and validation.
Unit tests: MCP tools
tests/unit/mcp/test_user_state_tools.py, test_envelope_contracts.py
Validates typed target fields in mark/annotation payloads, message targeting, annotation roundtrips, and tool schema classification.
Integration tests: HTTP daemon
tests/unit/daemon/test_web_reader.py
Seeds USER_ANNOTATIONS_DDL, enables annotate action in contracts, tests user-state endpoint roundtrips with target-aware assertions.
Storage contract tests
tests/unit/storage/test_schema_upgrades.py, test_user_state_contracts.py
Validates v12→v13 schema migration, verifies marks/annotations do not change conversation content hash, rejects unknown message targets.
Visual test fixtures
tests/visual/conftest.py
Seeds USER_ANNOTATIONS_DDL and updates user_marks seed data with target-aware columns.
Documentation
docs/cli-reference.md, docs/plans/mk3-execution-log.md, docs/plans/topology-target.yaml, docs/test-quality-workflows.md, docs/topology-status.md, docs/verification-catalog.md, docs/plans/file-size-budgets.yaml
CLI reference documents new user-state command, execution log records two issue #867 launches, topology/catalog/status updated with new target and file location changes.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

  • Sinity/polylogue#867: This PR fully implements the reader "user-state" feature scope including target-aware marks and annotations, CLI/MCP/HTTP/storage layers, and v12→v13 schema upgrade.

Possibly related PRs

  • Sinity/polylogue#1052: Preceding PR introduced MCP marks and saved-views tools; this PR extends them with annotation support and target-aware functionality.

Suggested labels

type:feature, area:cli, area:storage, area:mcp, area:schema

Poem

🐰 Marks and notes now dance with targets bright,
Conversations and messages shine in light,
From SQLite schemas to CLI commands clean,
User state features: the finest we've seen!
A v12-to-v13 journey, complete end-to-end,
Your durable reader state, forever a friend. 🌟

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/feat/user-state-annotations-targets

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Sinity
Sinity merged commit 3633308 into master May 15, 2026
3 of 4 checks passed
@Sinity
Sinity deleted the feature/feat/user-state-annotations-targets branch May 15, 2026 15:27

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4de162d0d0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread polylogue/daemon/user_state_http.py Resolved
@Sinity

Sinity commented Sep 30, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

Only the rows listed above are dispositioned. Held findings remain open.

@Sinity

Sinity commented Oct 1, 2026

Copy link
Copy Markdown
Owner Author

Historical Codex triage: evidence checked at 5ebcfe5. This records that source revision; it does not claim a fresh review of current master.

  • 77.F050 / P2: refuted at the checked revision: Repairing index creation in the predecessor v12-to-v13 user_marks migration would reinstate an unsupported prior-state upgrade route. The fresh-format contract excludes migration or readback of earlier Polylogue state, while allowing new external source ingestion. See AGENTS.md:75.

Only the rows listed above are dispositioned. Held findings remain open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant