Skip to content

refactor(mutations): centralize validation through ArchiveMutationsMixin (#862) - #1112

Merged
Sinity merged 1 commit into
masterfrom
feature/refactor/centralize-mutation-validation
May 17, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/refactor/centralize-mutation-validation

Conversation

@Sinity

@Sinity Sinity commented May 17, 2026

Copy link
Copy Markdown
Owner

Summary

Routes the seven remaining surface-side mutation paths through the shared
ArchiveMutationsMixin contract introduced in #1109, completing the
cross-surface centralization tracked by #862. Adds typed result models
(MetadataMutationResult, DeleteConversationResult,
BulkTagMutationResult) and a single source of truth for metadata key
validation.

Problem

Audit of master left seven divergences where CLI, MCP, and daemon
surfaces still bypassed the operation mixin:

  1. polylogue/cli/query_actions.py::apply_modifiers set metadata via
    repo.update_metadata directly, skipping the conversation-existence
    check.
  2. polylogue/mcp/server_mutation_tools.py::bulk_tag_conversations
    called tag_store.bulk_add_tags directly with inline 100/20 batch
    caps duplicated outside the operation layer.
  3. polylogue/mcp/server_mutation_tools.py::delete_metadata called
    tag_store.delete_metadata directly with no resolution through
    the facade.
  4. polylogue/mcp/server_mutation_tools.py::delete_conversation
    mapped bool -> "deleted"/"not_found" inside the surface; nothing
    guaranteed every adapter would do that the same way.
  5. polylogue/cli/query_actions.py::delete_conversations called
    repo.delete_conversation directly without facade resolution or
    idempotency contract.
  6. Metadata key validation (empty/whitespace, 200-char limit) was
    inline in MCP set_metadata only — every other surface relied on
    storage-layer behavior.
  7. polylogue/daemon/http.py::_handle_reset had a latent async bug:
    the synchronous _do_reset callback called the async
    poly.delete_conversation and returned the coroutine into a JSON
    encode path, never deleting anything.

Solution

  • polylogue/operations/mutations.py: extended ArchiveMutationsMixin
    with resolve-aware typed-result entrypoints — set_metadata_validated,
    delete_metadata_validated, delete_conversation_safe,
    bulk_tag_conversations — plus the centralized
    _validate_metadata_key helper and a MetadataKeyValidationError
    exception. The previous bool-returning methods stay as low-level
    delegates.
  • polylogue/surfaces/payloads.py: added typed
    MetadataMutationResult, DeleteConversationResult,
    BulkTagMutationResult, and validate_metadata_key /
    METADATA_KEY_MAX_LENGTH constants.
  • polylogue/api/archive.py: Polylogue.delete_conversation,
    update_metadata, the new set_metadata, delete_metadata,
    delete_conversation_safe, and bulk_tag_conversations all route
    through the mixin.
  • polylogue/cli/query_actions.py: apply_modifiers metadata writes
    go through env.polylogue.set_metadata; delete_conversations calls
    env.polylogue.delete_conversation_safe. Custom-repo escape hatches
    preserved for test/batch tools.
  • polylogue/mcp/server_mutation_tools.py: bulk tagging, set/delete
    metadata, and delete-conversation tools route through the facade and
    the typed results; key validation runs at the surface boundary before
    opening the facade so unit tests can exercise the rejection path
    without standing up an archive.
  • polylogue/daemon/http.py: _handle_reset now awaits
    delete_conversation_safe through the existing _sync_run async
    bridge and removes the obsolete _get_or_create_polylogue helper.
  • tests/unit/operations/test_mutations.py (new): 17 tests covering
    every (mutation, exists/missing, validation) pair on top of a real
    SQLite archive built via ConversationBuilder.
  • tests/unit/mcp/test_tool_contracts.py, tests/infra/mcp.py:
    refreshed mocks to track the new facade-typed return values.
  • tests/property/test_filter_chain_laws.py: fix for an inherited
    property-test failure (provider filter replaces prior provider rather
    than intersecting, so the monotonicity law required tightening to
    an impossible provider on disagreement). Surfaced by devtools verify
    before this PR could be merged.

Verification

$ devtools verify --quick
total_duration_s: 20.22, exit_code: 0

$ devtools verify --all
"name": "pytest full", "exit": 0, "count": 7268, "passed": 7266,
"xfailed": 2
total_duration_s: 101.93, exit_code: 0

$ pytest tests/unit/operations/test_mutations.py
17 passed in 8.13s

Closes #862

Routes the seven remaining surface-side mutation paths through the
shared ArchiveMutationsMixin contract (#862):

1. CLI apply_modifiers metadata path goes through env.polylogue.set_metadata
   so the conversation-existence check fires.
2. MCP bulk_tag_conversations routes through Polylogue.bulk_tag_conversations
   instead of reaching into the tag store with inline batch caps.
3. MCP delete_metadata routes through Polylogue.delete_metadata instead of
   the tag store.
4. MCP delete_conversation routes through delete_conversation_safe which
   returns a typed not_found result rather than relying on bool->string
   mapping inside the surface.
5. CLI delete_conversations routes through env.polylogue.delete_conversation_safe
   when no custom repo is provided.
6. Metadata key validation is centralized in validate_metadata_key and
   ArchiveMutationsMixin._validate_metadata_key.
7. Daemon _handle_reset is fixed: the previous sync callback dropped the
   delete_conversation coroutine into the JSON encoder. The handler now
   awaits delete_conversation_safe through the async run path.

Adds typed result models (MetadataMutationResult, DeleteConversationResult,
BulkTagMutationResult) and an inherited property-test fix for filter
monotonicity that surfaced during full verify.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@Sinity has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 21 minutes and 33 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d077898d-5a24-4205-a467-289690b82ce9

📥 Commits

Reviewing files that changed from the base of the PR and between 1acf3a0 and 56a4db1.

📒 Files selected for processing (10)
  • polylogue/api/archive.py
  • polylogue/cli/query_actions.py
  • polylogue/daemon/http.py
  • polylogue/mcp/server_mutation_tools.py
  • polylogue/operations/mutations.py
  • polylogue/surfaces/payloads.py
  • tests/infra/mcp.py
  • tests/property/test_filter_chain_laws.py
  • tests/unit/mcp/test_tool_contracts.py
  • tests/unit/operations/test_mutations.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/refactor/centralize-mutation-validation

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 e6b7122 into master May 17, 2026
3 checks passed
@Sinity
Sinity deleted the feature/refactor/centralize-mutation-validation branch May 17, 2026 00:11
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.

refactor(mutations): centralize tag metadata and delete contracts

1 participant