Skip to content

refactor: renew rendering and source boundary contracts - #279

Merged
Sinity merged 4 commits into
masterfrom
feature/refactor/rendering-source-boundary-renewal-r1
Apr 21, 2026
Merged

Sinity merged 4 commits into
masterfrom
feature/refactor/rendering-source-boundary-renewal-r1

Conversation

@Sinity

@Sinity Sinity commented Apr 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • renew rendering, source, and decoder boundary contracts around block payloads and dispatch
  • tighten follow-through across provider helpers, drive/source support, and plain-console rendering tests
  • eliminate the remaining hotspot seams in this slice without reopening the verification-renewal diff

Problem

The next high-leverage source-side weak typing and payload-plumbing debt was concentrated in the rendering/source boundary layer:

  • polylogue/rendering/block_models.py
  • polylogue/rendering/core_messages.py
  • polylogue/sources/dispatch.py
  • polylogue/sources/token_store.py
  • polylogue/lib/json.py

Those files still carried cast-heavy extraction, loose object/sequence handling, and partially modeled provider/decoder seams. Adjacent helpers like drive support, ChatGPT/Claude/Gemini provider adapters, and the plain-console shim were then compensating for that imprecision.

Closes #278

Solution

  • rebuilt the JSON/document helpers in polylogue/lib/json.py and polylogue/sources/decoder_json.py around typed validation rather than cast-driven loads
  • tightened renderable block/message handling in polylogue/rendering/block_models.py, polylogue/rendering/blocks.py, polylogue/rendering/core_messages.py, and polylogue/rendering/renderers/html_messages.py
  • removed source/provider hotspot casts across polylogue/sources/dispatch.py, token_store.py, emitter.py, drive_source_client.py, drive_source_support.py, parsers/claude_index.py, parsers/claude_code_parser.py, parsers/drive_support_{attachments,blocks}.py, and provider adapters under polylogue/sources/providers/
  • tightened the remaining boundary seams in drive_auth.py, drive_gateway.py, blob_store.py, and ui/facade_console.py
  • extended rendering/source law coverage with targeted tests for media blocks, HTML message rendering, output snapshots, and source detection/dispatch laws

Verification

  • ruff check tests/unit/rendering/test_rendering.py tests/unit/ui/test_ui.py polylogue/ui/facade_console.py
  • mypy tests/unit/rendering/test_rendering.py tests/unit/ui/test_ui.py polylogue/ui/facade_console.py
  • pytest -q tests/unit/rendering/test_rendering.py tests/unit/ui/test_ui.py
  • devtools verify

@coderabbitai

coderabbitai Bot commented Apr 21, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR systematically removes unsafe typing casts and tightens JSON/value contracts by introducing explicit runtime validation helpers (_is_json_value, _json_object, KeyringModule protocol) and replacing loose Any types with stricter type aliases. It updates binary I/O annotations from BinaryIO to IO[bytes] and adds media block rendering support with structured block handling.

Changes

Cohort / File(s) Summary
JSON Value Contract & Validation
polylogue/lib/json.py, polylogue/sources/decoder_json.py
Tightened is_json_document to validate dict values, introduced _loaded_json_value and _is_json_value validators with explicit failure paths for non-conforming JSON payloads, and updated JSONL/stream decoding to filter/validate parsed results.
Rendering & Content Blocks
polylogue/rendering/block_models.py, polylogue/rendering/blocks.py, polylogue/rendering/renderers/html_messages.py
Replaced Mapping[str, object] with JSONDocument for tool_input, added explicit string-extraction helpers (_optional_string, _first_string), implemented media block rendering (_render_media_html, _render_media_plaintext), and optimized content block precomputation in HTML message building.
Message Processing & Mainline Logic
polylogue/rendering/core_messages.py
Updated timestamp normalization to handle OverflowError, changed mainline selection to filter by branch_index == 0 or parent_message_id is None, and removed type casts via list comprehensions.
Cast Removal & Protocol Definitions
polylogue/sources/dispatch.py, polylogue/sources/drive_auth.py, polylogue/sources/drive_gateway.py, polylogue/sources/drive_source_client.py
Replaced cast() usage with direct variable annotations and a new _WritableBinaryHandle protocol; simplified type-checking expectations for stream handles.
Protocol & Token Management
polylogue/sources/token_store.py
Introduced KeyringModule runtime-checkable protocol to validate dynamically-imported keyring APIs, replacing type-ignore comments with explicit protocol conformance.
Drive Support & Type Aliases
polylogue/sources/drive_source_support.py, polylogue/sources/parsers/drive_support_attachments.py, polylogue/sources/parsers/drive_support_blocks.py
Introduced DriveJSONPayloadSequence and DriveJSONRecordSequence type aliases, added _as_document_metadata helper for JSON document conversion, and replaced dict filtering predicates with stricter type validation.
Provider Models & Metadata
polylogue/sources/providers/chatgpt_message_models.py, polylogue/sources/providers/claude_ai.py, polylogue/sources/providers/gemini_message.py
Replaced loose dict[str, Any] with explicit type aliases (ChatGPTMetadata, ChatGPTPartValue, ClaudeAIObject, GeminiDictValue), added _model_slug and _json_object helpers for type-safe metadata extraction, and optimized repeated payload conversions.
Parser Updates
polylogue/sources/parsers/claude_code_parser.py, polylogue/sources/parsers/codex.py, polylogue/sources/parsers/claude_index.py
Widened parse/parse_code parameters from list[object] to Sequence[object], introduced SessionIndexMapping alias and dedicated field-extraction helpers (_extract_entries, _extract_string, _extract_int, _extract_bool) for type-safe dictionary parsing.
Stream Type Standardization
polylogue/sources/emitter.py, polylogue/sources/source_acquisition.py, polylogue/sources/decoder_zip.py, polylogue/storage/blob_store.py
Standardized binary stream type annotations from BinaryIO to IO[bytes] across stream handling, ZIP decoding, and blob storage; removed associated cast() calls.
UI Console & Markup Handling
polylogue/ui/facade_console.py
Updated ConsoleLike.print protocol to accept **kwargs: Any, removed _plain_text_from_markup helper, and explicit rich.errors.MarkupError handling for safer markup parsing fallback.
Test Coverage
tests/unit/rendering/test_html_messages.py, tests/unit/rendering/test_output_snapshots.py, tests/unit/rendering/test_rendering.py, tests/unit/sources/test_source_laws.py
Added regression test for structured-only message rendering, media block rendering assertions, _make_media_block helper and TestMediaBlockRendering/TestPlainConsoleLiteralOutput test classes, and updated stream type annotations in test fixtures to use IO[bytes].

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related issues

  • Issue #278: Main changes directly implement the renewal of typed boundaries by tightening JSON contracts, replacing unsafe casts in rendering and source modules, and introducing validation protocols (e.g., KeyringModule).

Possibly related PRs

  • PR #276: Both PRs modify and tighten JSON utility contracts (json_document, loads, _is_json_value) and propagate stricter JSONDocument/JSONValue types across storage and source modules.
  • PR #253: Both PRs make overlapping changes to polylogue/lib/json.py by tightening JSON contracts and adjusting json_document/json_document_list behavior, with cascading type updates across parsers and sources.
  • PR #228: Both PRs modify JSON/payload validation and rendering boundaries (tightening contracts in decoder_json.py and block_models.py) with related schema validation and type-safe conversion logic.

Poem

🐰 Hops through the types with glee,
No more casts to set us free!
Validators dance, contracts tight—
JSON values pass the knight!
Schemas guard what flows through streams,
Type-safe dreams and protocol schemes. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 21.95% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the primary change: refactoring boundary contracts between rendering and source layers, which is the main focus of the 25-file changeset.
Description check ✅ Passed The description follows the template structure with all required sections: Summary (what changed and why), Problem (what was needed), Solution (what was done with key modules listed), and Verification (exact commands run). The description is comprehensive and directly addresses the template requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/refactor/rendering-source-boundary-renewal-r1

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: 399d94b6f6

ℹ️ 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 on lines +54 to 55
if not text and not content_blocks:
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve text-only block content when retaining empty-text messages

This new guard keeps messages whenever content_blocks is present, but non-structured blocks (like type: "text") still render HTML from msg.text inside build_rendered_message. For inputs such as text="" with content_blocks=[{"type":"text","text":"hello"}], the message is now included but its HTML body is empty, creating blank message shells in both conversation/projection views. This regression appears whenever upstream data stores text in blocks but leaves the legacy text field empty.

Useful? React with 👍 / 👎.

Comment thread polylogue/rendering/blocks.py Resolved
@Sinity
Sinity merged commit 5e54f62 into master Apr 21, 2026
14 of 15 checks passed
@Sinity
Sinity deleted the feature/refactor/rendering-source-boundary-renewal-r1 branch April 21, 2026 01:59
Sinity added a commit that referenced this pull request Apr 22, 2026
## Summary

- renew rendering, source, and decoder boundary contracts around block payloads and dispatch
- tighten follow-through across provider helpers, drive/source support, and plain-console rendering tests
- eliminate the remaining hotspot seams in this slice without reopening the verification-renewal diff

## Problem

The next high-leverage source-side weak typing and payload-plumbing debt was concentrated in the rendering/source boundary layer:

- `polylogue/rendering/block_models.py`
- `polylogue/rendering/core_messages.py`
- `polylogue/sources/dispatch.py`
- `polylogue/sources/token_store.py`
- `polylogue/lib/json.py`

Those files still carried cast-heavy extraction, loose `object`/sequence handling, and partially modeled provider/decoder seams. Adjacent helpers like drive support, ChatGPT/Claude/Gemini provider adapters, and the plain-console shim were then compensating for that imprecision.

Closes #278

## Solution

- rebuilt the JSON/document helpers in `polylogue/lib/json.py` and `polylogue/sources/decoder_json.py` around typed validation rather than cast-driven loads
- tightened renderable block/message handling in `polylogue/rendering/block_models.py`, `polylogue/rendering/blocks.py`, `polylogue/rendering/core_messages.py`, and `polylogue/rendering/renderers/html_messages.py`
- removed source/provider hotspot casts across `polylogue/sources/dispatch.py`, `token_store.py`, `emitter.py`, `drive_source_client.py`, `drive_source_support.py`, `parsers/claude_index.py`, `parsers/claude_code_parser.py`, `parsers/drive_support_{attachments,blocks}.py`, and provider adapters under `polylogue/sources/providers/`
- tightened the remaining boundary seams in `drive_auth.py`, `drive_gateway.py`, `blob_store.py`, and `ui/facade_console.py`
- extended rendering/source law coverage with targeted tests for media blocks, HTML message rendering, output snapshots, and source detection/dispatch laws

## Verification

- `ruff check tests/unit/rendering/test_rendering.py tests/unit/ui/test_ui.py polylogue/ui/facade_console.py`
- `mypy tests/unit/rendering/test_rendering.py tests/unit/ui/test_ui.py polylogue/ui/facade_console.py`
- `pytest -q tests/unit/rendering/test_rendering.py tests/unit/ui/test_ui.py`
- `devtools verify`
@Sinity Sinity added area:parser Parser area:qa QA tool:claude Claude type:refactor Code restructuring labels Apr 22, 2026
Sinity added a commit that referenced this pull request Apr 22, 2026
The next high-leverage source-side debt was concentrated in the
rendering/source boundary: `rendering/block_models.py`,
`rendering/core_messages.py`, `sources/dispatch.py`,
`sources/token_store.py`, and `lib/json.py` still carried cast-heavy
extraction, loose `object` / sequence handling, and partially
modeled provider/decoder seams. Adjacent helpers (Drive support,
ChatGPT/Claude/Gemini provider adapters, the plain-console shim)
were compensating for that imprecision.

Rebuilds the JSON/document helpers in `lib/json.py` and
`sources/decoder_json.py` around typed validation rather than
cast-driven loads. Tightens renderable block/message handling in
`rendering/block_models.py`, `blocks.py`, `core_messages.py`, and
`renderers/html_messages.py`. Removes source/provider hotspot casts
across `sources/dispatch.py`, `token_store.py`, `emitter.py`,
`drive_source_client.py`, `drive_source_support.py`,
`parsers/claude_index.py`, `parsers/claude_code_parser.py`,
`parsers/drive_support_{attachments,blocks}.py`, and provider
adapters under `sources/providers/`. Tightens remaining boundary
seams in `drive_auth.py`, `drive_gateway.py`, `blob_store.py`, and
`ui/facade_console.py`.

Extends rendering/source law coverage with targeted tests for media
blocks, HTML message rendering, output snapshots, and source
detection/dispatch laws.

Verification: ruff + mypy + pytest over rendering/UI suites;
devtools verify green.

Closes #278
Sinity added a commit that referenced this pull request Apr 22, 2026
The next high-leverage source-side debt was concentrated in the
rendering/source boundary: `rendering/block_models.py`,
`rendering/core_messages.py`, `sources/dispatch.py`,
`sources/token_store.py`, and `lib/json.py` still carried cast-heavy
extraction, loose `object` / sequence handling, and partially
modeled provider/decoder seams. Adjacent helpers (Drive support,
ChatGPT/Claude/Gemini provider adapters, the plain-console shim)
were compensating for that imprecision.

Rebuilds the JSON/document helpers in `lib/json.py` and
`sources/decoder_json.py` around typed validation rather than
cast-driven loads. Tightens renderable block/message handling in
`rendering/block_models.py`, `blocks.py`, `core_messages.py`, and
`renderers/html_messages.py`. Removes source/provider hotspot casts
across `sources/dispatch.py`, `token_store.py`, `emitter.py`,
`drive_source_client.py`, `drive_source_support.py`,
`parsers/claude_index.py`, `parsers/claude_code_parser.py`,
`parsers/drive_support_{attachments,blocks}.py`, and provider
adapters under `sources/providers/`. Tightens remaining boundary
seams in `drive_auth.py`, `drive_gateway.py`, `blob_store.py`, and
`ui/facade_console.py`.

Extends rendering/source law coverage with targeted tests for media
blocks, HTML message rendering, output snapshots, and source
detection/dispatch laws.

Verification: ruff + mypy + pytest over rendering/UI suites;
devtools verify green.

Closes #278
Sinity added a commit that referenced this pull request Apr 22, 2026
The next high-leverage source-side debt was concentrated in the
rendering/source boundary: `rendering/block_models.py`,
`rendering/core_messages.py`, `sources/dispatch.py`,
`sources/token_store.py`, and `lib/json.py` still carried cast-heavy
extraction, loose `object` / sequence handling, and partially
modeled provider/decoder seams. Adjacent helpers (Drive support,
ChatGPT/Claude/Gemini provider adapters, the plain-console shim)
were compensating for that imprecision.

Rebuilds the JSON/document helpers in `lib/json.py` and
`sources/decoder_json.py` around typed validation rather than
cast-driven loads. Tightens renderable block/message handling in
`rendering/block_models.py`, `blocks.py`, `core_messages.py`, and
`renderers/html_messages.py`. Removes source/provider hotspot casts
across `sources/dispatch.py`, `token_store.py`, `emitter.py`,
`drive_source_client.py`, `drive_source_support.py`,
`parsers/claude_index.py`, `parsers/claude_code_parser.py`,
`parsers/drive_support_{attachments,blocks}.py`, and provider
adapters under `sources/providers/`. Tightens remaining boundary
seams in `drive_auth.py`, `drive_gateway.py`, `blob_store.py`, and
`ui/facade_console.py`.

Extends rendering/source law coverage with targeted tests for media
blocks, HTML message rendering, output snapshots, and source
detection/dispatch laws.

Verification: ruff + mypy + pytest over rendering/UI suites;
devtools verify green.

Closes #278
@Sinity

Sinity commented Apr 28, 2026

Copy link
Copy Markdown
Owner Author

🤖 Post-hoc audit note: this PR received bot review feedback verified against current HEAD as part of #544. 2 finding(s) confirmed still present → tracked in #550.

@Sinity
Sinity requested a review from Copilot May 1, 2026 00:43
@Sinity
Sinity requested review from Copilot and removed request for Copilot May 1, 2026 01:05
@Sinity
Sinity removed the request for review from Copilot May 1, 2026 02:15
@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.

  • 70.F003 / P1: fixed-already at the checked revision: The report applies sanitize_html to each rendered message at polylogue/rendering/templates/session.html:328. The sanitizer passes an explicit http/https/mailto scheme allowlist to nh3 at polylogue/rendering/renderers/html_sanitizer.py:116, so javascript: and data: media hrefs do not survive into the published HTML.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Renew rendering and source boundary contracts around block payloads and dispatch

1 participant