Skip to content

refactor(api): formalize shared write-surface contracts (#861) - #1107

Merged
Sinity merged 1 commit into
masterfrom
feature/refactor/shared-write-surface-contracts
May 16, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/refactor/shared-write-surface-contracts

Conversation

@Sinity

@Sinity Sinity commented May 16, 2026

Copy link
Copy Markdown
Owner

Summary

Mirror of #859/#1099 for the write/import/maintenance side. Defines a typed
Protocol family in polylogue/api/contracts/write_surface.py and three
adapter modules (api/cli/mcp_write_surface.py) with import-time
assert_implements pins so any signature drift fails mypy --strict. Adds
runtime envelope-parity contract tests.

Problem

#859 landed read-surface protocol contracts (ConversationListSurface,
ReadSurface, etc.) so CLI, MCP, daemon HTTP, and Python API adapters cannot
drift apart on read semantics. The write/import/maintenance side had no
equivalent contract: CLI ingest stages into archive_root/inbox and POSTs to
the daemon, MCP maintenance instantiates IndexService-adjacent helpers while
CLI maintenance routes through execute_backfill, and Polylogue.parse_file
/ parse_sources bypass the operation boundary entirely. There was no named
operation contract telling agents which behavior is canonical or which
adapters are allowed to do direct pipeline/service work — exactly the gap
called out in #861.

Solution

polylogue/api/contracts/write_surface.py (new) — five
@runtime_checkable Protocols plus a composite:

  • IngestSurface.ingest_path(path) -> ImportOperation — typed import
    envelope with operation_id / status / bounded raw_failure_samples.
    Adapters that cannot reach the daemon must surface status="failed" with
    a populated error field rather than silently downgrading to "scheduled".
  • MaintenanceSurface.run_maintenance(targets, *, dry_run) -> BackfillOperation
    — delegates to the shared execute_backfill planner so adapters share the
    same target vocabulary (MAINTENANCE_TARGET_NAMES).
  • IndexMaintenanceSurface.rebuild_index() / update_index(ids) — routes
    through ArchiveOperations and the shared indexing free functions.
  • TagMutationSurface.add_tag / remove_tag -> TagMutationResult — write-side
    counterpart to the read-side ConversationTagsSurface.
  • ConversationDeleteSurface.delete_conversation(id) -> bool.
  • Composite WriteSurface for callers that want the full union.

Three adapter modules — api_write_surface.py, cli_write_surface.py,
mcp_write_surface.py — each carry assert_implements(Adapter, Protocol)
calls at module scope (the same mechanism used by the read-surface family).
The CLI/MCP adapters take an optional polylogue facade for tag/delete
mutations so they do not own a parallel runtime.

Tests in tests/unit/api/test_write_surface_contracts.py cover:

  1. Static conformance — every (adapter × protocol) pair via
    issubclass, plus the composite WriteSurface.
  2. Runtime envelope parity — TagMutationResult outcome vocabulary
    (added → no_op → removed → not_present across API/CLI/MCP),
    BackfillOperation envelope shape from dry-run maintenance, and
    ImportOperation truthfulness for a missing path (status="failed" with
    populated error). Each test records bounded contract_evidence.

Non-goals (deferred):

  • CLI/MCP/daemon code is not rewired in this PR to delegate through the new
    adapters — that is the broader surgery refactor(ingest): define shared import and maintenance operation contracts #861 calls for. This PR formalizes
    the contract surface that the rewiring will land against, and proves
    envelope shapes match across the existing implementations.
  • Polylogue.parse_file / parse_sources are kept as low-level pipeline
    helpers; this is documented in the protocol docstrings and in
    import_contracts.py. The APIWriteSurface.ingest_path adapter wraps
    them with a typed ImportOperation envelope so callers see one shape.

Verification

nix develop -c mypy polylogue/api/contracts/ tests/unit/api/test_write_surface_contracts.py
  Success: no issues found in 5 source files
nix develop -c pytest tests/unit/api/ -q
  36 passed in 19.98s   (17 read-surface + 19 write-surface contract tests)
nix develop -c devtools verify --quick
  exit_code: 0   (total 22.45s)
nix develop -c devtools verify-topology
  realized=742 declared=742 blocking=False

Ref #861

@coderabbitai

coderabbitai Bot commented May 16, 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 43 minutes and 3 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: 111e7356-3a4a-4df1-bfd4-3e4e36449c5d

📥 Commits

Reviewing files that changed from the base of the PR and between d67f442 and ea38980.

📒 Files selected for processing (8)
  • docs/plans/topology-target.yaml
  • docs/topology-status.md
  • polylogue/api/contracts/__init__.py
  • polylogue/api/contracts/api_write_surface.py
  • polylogue/api/contracts/cli_write_surface.py
  • polylogue/api/contracts/mcp_write_surface.py
  • polylogue/api/contracts/write_surface.py
  • tests/unit/api/test_write_surface_contracts.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/refactor/shared-write-surface-contracts

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.

Mirror of #859/#1099 for the write/import/maintenance side of polylogue.
Introduces a typed Protocol family in `polylogue/api/contracts/write_surface.py`
covering ingest scheduling, maintenance backfills, FTS index
maintenance, tag mutations, and conversation delete, plus three adapter
modules (api/cli/mcp_write_surface.py) with import-time
`assert_implements` pins so any signature drift fails `mypy --strict`.

Adds `tests/unit/api/test_write_surface_contracts.py` covering static
conformance (every adapter implements every protocol) and runtime
envelope parity (TagMutationResult, BackfillOperation, ImportOperation
shapes match across API/CLI/MCP for tag mutation, dry-run maintenance,
and missing-path ingest).

Refs #861
@Sinity
Sinity force-pushed the feature/refactor/shared-write-surface-contracts branch from 576aed4 to ea38980 Compare May 16, 2026 23:50
@Sinity
Sinity merged commit 513432a into master May 16, 2026
3 checks passed
@Sinity
Sinity deleted the feature/refactor/shared-write-surface-contracts branch May 16, 2026 23:50

@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: 576aed4ce2

ℹ️ 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".

@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.

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