Skip to content

refactor(api): typed import operation contract — Operations pattern foundation (#1247) - #1316

Merged
Sinity merged 1 commit into
masterfrom
feature/refactor/operations-pattern-1247
May 18, 2026
Merged

Sinity merged 1 commit into
masterfrom
feature/refactor/operations-pattern-1247

Conversation

@Sinity

@Sinity Sinity commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Lands the typed Operation contract called out in #1247 as the strategic
foundation for the broader Polylogue Operations pattern, plus the first
concrete instance for import operations. The contract is additive — no
existing surface is rewired in this slice; #1248/#1249/#1250 will wire
adapters to accept ImportRequest and emit ImportAck.

Problem

The audit issue notes that there is no named import operation contract.
CLI ingest, daemon HTTP /api/ingest, live watcher, convergence
stages, and MCP IndexService each carry their own scheduling/result
semantics, and the same gap applies to maintenance (#996),
convergence (#845), and health (#999). Without a shared typed contract,
every future Operation invents its own request/ack envelope.

Solution

Two new modules under polylogue/operations/ and one test module.

operation_contract.py — the reusable foundation:

  • OperationStatus: closed string enum (accepted, rejected,
    pending, running, completed, failed).
  • OperationFollowUp: status_endpoint / status_token /
    poll_after_ms — the polling triple every client needs after an
    ack.
  • OperationRequest: frozen Pydantic base with extra=forbid and
    an operation_kind ClassVar contract. _require_operation_kind
    rejects subclasses that forget to declare their kind.
  • OperationAck: operation_id, kind, status, message,
    error, follow_up — the single shape every client parses.
    Factory helpers accepted / rejected / pending.

import_operations.py — the first instance:

Both modules carry design-notes docstrings that describe how the same
contract maps onto maintenance (#996), convergence (#845), and schema
upgrades — so the next slice has the prior-art reference inline.

The existing dataclass ImportOperation in import_contracts.py
(terminal result envelope, consumed by polylogue/api/contracts/*,
polylogue/cli/commands/ingest.py, polylogue/daemon/http.py) is
unchanged. The two layers compose: ImportRequest admits work,
ImportAck returns the scheduling response, ImportOperation
re-emits the terminal result envelope once the work completes.

Verification

devtools verify --quick — exit_code 0 (pre-push hook).

Focused pytest:

pytest tests/unit/operations/ tests/unit/api/test_write_surface_contracts.py -q
67 passed in 9.35s

The new test_operation_contract.py adds 25 unit tests covering:

  • OperationFollowUp construction, JSON shape, extra=forbid,
    frozen-after-construction.
  • ImportRequest minimal/full construction, missing-field rejection,
    extra=forbid, frozen, JSON roundtrip.
  • ImportAck factories (accept/reject/pending) — status, kind,
    follow_up, error wiring.
  • OperationAck to_dict for accepted and rejected, model_validate
    roundtrip, byte-stable enum serialization.
  • _require_operation_kind contract: ImportRequest declares,
    subclass without operation_kind raises TypeError, custom
    subclass with maintenance kind works.
  • OperationStatus enum value set is closed.

Topology projection regenerated via devtools build-topology-projection;
docs/topology-status.md re-rendered.

Closes #1247.

…oundation

Introduces the reusable typed Operation contract called out in #1247, plus
the first concrete instance for import operations. The contract is the
strategic foundation for the Operations pattern that #845 (convergence),
#996 (maintenance), and #999 (health) will consume so each slice does not
invent its own scheduling envelope.

New modules:
- polylogue/operations/operation_contract.py: OperationRequest,
  OperationAck, OperationFollowUp, OperationStatus — frozen Pydantic
  models with extra=forbid, byte-stable JSON serialization, and an
  operation_kind class variable contract for subclasses.
- polylogue/operations/import_operations.py: ImportRequest declares
  source_path / source_name / staged_path / idempotency_key; ImportAck
  pins kind=IMPORT via accept_import / reject_import / pending_import
  factories.

The existing dataclass ImportOperation in import_contracts.py (terminal
result envelope) is unchanged and continues to flow through CLI ingest,
daemon HTTP, and the write-surface contracts; subsequent slices wire
surfaces to accept ImportRequest at admission.

Tests: tests/unit/operations/test_operation_contract.py covers
serialization, validation (extra=forbid, frozen, required fields),
factory helpers, JSON roundtrip, kind/status enum stability, and the
_require_operation_kind contract for subclass declarations.

Closes #1247.
@Sinity
Sinity merged commit 9cfd585 into master May 18, 2026
@Sinity
Sinity deleted the feature/refactor/operations-pattern-1247 branch May 18, 2026 15:03
@coderabbitai

coderabbitai Bot commented May 18, 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 44 minutes and 56 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: 9f7a8008-fa6b-4253-85e6-74643d014860

📥 Commits

Reviewing files that changed from the base of the PR and between 602a922 and 0847081.

📒 Files selected for processing (6)
  • docs/plans/topology-target.yaml
  • docs/topology-status.md
  • polylogue/operations/__init__.py
  • polylogue/operations/import_operations.py
  • polylogue/operations/operation_contract.py
  • tests/unit/operations/test_operation_contract.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/refactor/operations-pattern-1247

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.

@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: 08470817a7

ℹ️ 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 added a commit that referenced this pull request May 18, 2026
Closes #1322, #1248.

Per audit #1322, address all seven findings in one coherent PR.

F1 (Operations contract — #1247/#1316): wire it. daemon /api/ingest
now validates request bodies through ImportRequest and emits ImportAck
with the shared OperationFollowUp envelope. Legacy wire keys (path,
status, ok) are preserved so the CLI ingest adapter and existing
HTTP clients keep working. polylogue/daemon/http.py:1574-1630.

F2 (api/cli/mcp read-surface adapters): delete. The Python API, CLI,
and MCP do not go through dedicated adapter envelopes — they
hand-roll richer envelopes inline (daemon/http.py:_do_list with
target_ref/anchor/actions/repo/cwd/flags and mcp/server_tools.py).
Wiring inline-handrolled envelopes through the bare adapter envelope
would lose information. TUIReadSurface stays — it is used by
polylogue/ui/tui/screens/{browser,search}.py.

F4 (ContractEvidenceRecorder): delete. No non-test consumer existed.
Removed tests/infra/contract_evidence.py + ~17 test call sites +
the matching devtools dashboards/loaders that aggregated artifacts
from .cache/verification/evidence/.

F6 (verify-cross-cuts): delete. Verifier and generator share the
same naming convention; the lint was a tautology. Removed from
devtools/command_catalog.py, devtools/verify.py chain, and
devtools/evidence_dashboard.py static-gate list. cross_cut_tags()
in build_topology_projection.py is kept because verify-topology
still consumes the generated tags.

F7 (insights/provenance helpers): wire. daemon insights browser
profile panel (#1018/#1120) now passes the SessionProfileRecord
through is_stale() against the conversation updated_at and exposes
the typed verdict as a `staleness` field on the panel.

F3, F5: out of scope per audit guidance.

Verification:
- devtools verify --quick → exit 0 (34s)
- pytest -q --ignore=tests/integration → 8022 passed, 2 xfailed (117s)
- Two pre-existing local-env failures from a live daemon were
  deselected (tests/unit/cli/test_init.py::test_status_first_run_hint_suggests_init,
  tests/unit/cli/test_status.py::TestNoArchiveStatus::test_status_subprocess_no_archive);
  both failed identically against origin/master.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sinity added a commit that referenced this pull request May 18, 2026
## Summary

Address all seven findings from the verifiability quality audit in #1322
in one
coherent PR. Wire what has a real consumer, delete what is inert, and
land #1248
(daemon `/api/ingest` as the first Operations consumer) in the same
slice so
the contract introduced in #1316 is no longer a
foundation-without-consumer.

## Problem

#1322 audited the open + closed issue corpus, codebase, and tests for
hollow
abstractions, inert tests, puppeteered scenarios, and naming theatre.
Seven
high-confidence findings (F1–F7). F1 specifically called out that PR
#1316 had
landed the Operations contract substrate without any production
consumer; #1248
(daemon `/api/ingest`) was the obvious wiring target but still open.

This PR resolves the audit by routing each finding through one of three
disposals: wire, delete, or leave-as-out-of-scope per the audit's own
guidance.

## Solution

Per-finding actions:

- **F1 Operations contract (PR #1316) → WIRE.**
`polylogue/daemon/http.py`'s
`/api/ingest` now validates request bodies through `ImportRequest` and
emits
`ImportAck` with the shared `OperationFollowUp` envelope. Legacy wire
keys
(`path`, `status`, `ok`) are preserved so the CLI ingest adapter and
existing
  HTTP clients keep working. Closes #1248.
- **F2 read-surface adapters → DELETE.**
`polylogue/api/contracts/{api,cli,mcp}_surface.py`
removed. The real Python API, CLI, and MCP code paths do not go through
these
adapter envelopes — they hand-roll richer envelopes inline
(`daemon/http.py:_do_list`
carries `target_ref/anchor/actions/repo/cwd/flags`;
`mcp/server_tools.py` has
  its own envelope). Wiring inline-handrolled envelopes through the bare
adapter envelope would lose information. `TUIReadSurface` stays — used
by
  `polylogue/ui/tui/screens/{browser,search}.py`.
- **F4 `ContractEvidenceRecorder` → DELETE.** No non-test consumer.
Removed
`tests/infra/contract_evidence.py` + call sites + the matching devtools
dashboards/loaders that aggregated artifacts from
`.cache/verification/evidence/`.
- **F6 `verify-cross-cuts` → DELETE.** Verifier and generator share the
same
  naming convention; the lint was a tautology. Removed from
  `devtools/command_catalog.py`, the `devtools verify` chain, and
  `devtools/evidence_dashboard.py`. `cross_cut_tags()` in
  `build_topology_projection.py` is kept because `verify-topology` still
  consumes the generated tags.
- **F7 insights provenance helpers → WIRE.** Daemon insights browser
profile
panel (#1018/#1120) now passes the `SessionProfileRecord` through
`is_stale()`
against the conversation `updated_at` and exposes the typed verdict as a
  `staleness` field on the panel.
- **F3, F5 → out of scope** per audit guidance.

## Verification

- `python -m devtools verify --quick` → exit 0 (39s)
- `pytest -q --ignore=tests/integration` → **8062 passed, 2 xfailed**
(105.75s)

## Notes

Closes #1322, #1248.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@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.

refactor(api): typed import operation contract — request/result models (#861 slice A)

1 participant