Repository navigation
refactor: renew transient run-state and storage view contracts - #236
Conversation
📝 WalkthroughWalkthroughA large typing refactor introduces strongly-typed Pydantic models ( Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related issues
Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
polylogue/lib/run_activity.py (1)
7-26:⚠️ Potential issue | 🟠 MajorHandle
RunDriftbuckets instead of dropping them.
RunDrift.get()returns aDriftBucket, not aMapping, so Line 23 makes typed drift always read as zero. This breaksconversation_activity_counts(..., drift=RunDrift(...)).🐛 Proposed fix
-from polylogue.storage.state_views import RunCounts, RunDrift +from polylogue.storage.state_views import DriftBucket, RunCounts, RunDrift @@ if drift is None: return 0 bucket_mapping = drift.get(bucket) - if not isinstance(bucket_mapping, Mapping): + if not isinstance(bucket_mapping, (Mapping, DriftBucket)): return 0 value = bucket_mapping.get(key) return value if isinstance(value, int) else 0🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@polylogue/lib/run_activity.py` around lines 7 - 26, The _drift_value helper currently treats drift.get(bucket) as a generic Mapping and returns 0 when it's a RunDrift/DriftBucket, so typed drift values are always zero; update _drift_value to handle both Mapping and RunDrift types by checking if drift is a RunDrift (or if bucket_mapping is a DriftBucket) and then retrieving the int value from that bucket (e.g., call drift.get(bucket) and accept either a Mapping or a DriftBucket, then use .get(key) and isinstance(..., int) to return the int or 0). Ensure you reference the existing function _drift_value and the RunDrift/DriftBucket return type when making the conditional branch so RunDrift-backed buckets are not dropped.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@polylogue/pipeline/watch.py`:
- Around line 58-67: Change the _conversation_count signature to accept a
RunResult (not object) and remove the unreachable Mapping and getattr fallbacks:
inside _conversation_count(result: RunResult) call
result.counts.int_value("conversations") directly (using RunCounts.int_value)
and return the integer if isinstance(..., int) else 0; update any callers (e.g.,
WatchRunner._sync_fn) if necessary to keep types consistent. This eliminates
defensive type checks and relies on RunResult.counts being a RunCounts instance.
In `@tests/unit/sources/test_decoders.py`:
- Around line 298-310: The test uses cursor_state.get("failed_files", []) which
can produce an untyped default literal; change the access to use the shared
helper _failed_files(cursor_state) (the same helper used in
tests/integration/test_security.py) so the result is correctly typed as
list[CursorFailurePayload]; update the assertion variables in
test_cursor_state_records_failures to call _failed_files(cursor_state) instead
of cursor_state.get(...) and keep the rest of the assertions the same (the test
still seeds cursor_state via _seeded_cursor_state and exercises
_ZipEntryValidator).
---
Outside diff comments:
In `@polylogue/lib/run_activity.py`:
- Around line 7-26: The _drift_value helper currently treats drift.get(bucket)
as a generic Mapping and returns 0 when it's a RunDrift/DriftBucket, so typed
drift values are always zero; update _drift_value to handle both Mapping and
RunDrift types by checking if drift is a RunDrift (or if bucket_mapping is a
DriftBucket) and then retrieving the int value from that bucket (e.g., call
drift.get(bucket) and accept either a Mapping or a DriftBucket, then use
.get(key) and isinstance(..., int) to return the int or 0). Ensure you reference
the existing function _drift_value and the RunDrift/DriftBucket return type when
making the conditional branch so RunDrift-backed buckets are not dropped.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 5891ef69-8431-4333-99d3-895a61e4c632
📒 Files selected for processing (30)
polylogue/cli/formatting.pypolylogue/cli/run_display_workflow.pypolylogue/cli/run_workflow.pypolylogue/lib/run_activity.pypolylogue/pipeline/run_finalization.pypolylogue/pipeline/run_stages.pypolylogue/pipeline/run_state.pypolylogue/pipeline/services/acquisition.pypolylogue/pipeline/services/acquisition_records.pypolylogue/pipeline/services/acquisition_streams.pypolylogue/pipeline/services/planning_runtime.pypolylogue/pipeline/services/rendering.pypolylogue/pipeline/watch.pypolylogue/sources/cursor.pypolylogue/sources/decoder_zip.pypolylogue/sources/drive.pypolylogue/sources/source_acquisition.pypolylogue/sources/source_parsing.pypolylogue/sources/source_walk.pypolylogue/storage/state_views.pytests/integration/test_security.pytests/integration/test_workflows.pytests/unit/cli/test_run.pytests/unit/cli/test_run_int.pytests/unit/cli/test_run_laws.pytests/unit/pipeline/test_run_sources.pytests/unit/sources/test_acquisition_encoding.pytests/unit/sources/test_decoders.pytests/unit/sources/test_drive_ops.pytests/unit/sources/test_source_laws.py
| def _conversation_count(result: object) -> int: | ||
| counts = getattr(result, "counts", None) | ||
| int_value = getattr(counts, "int_value", None) | ||
| if callable(int_value): | ||
| value = int_value("conversations") | ||
| return value if isinstance(value, int) else 0 | ||
| if isinstance(counts, Mapping): | ||
| value = counts.get("conversations", 0) | ||
| return value if isinstance(value, int) else 0 | ||
| return 0 |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
rg -nP -C3 'counts\s*=' --type=py -g '!tests/**' | rg -n 'RunResult|counts='
rg -nP -C2 '\bRunResult\s*\(' --type=pyRepository: Sinity/polylogue
Length of output: 10982
🏁 Script executed:
# Get the full RunResult and RunCounts definitions
rg -A 10 'class RunResult' polylogue/storage/state_views.py
rg -A 15 'class RunCounts' --type=pyRepository: Sinity/polylogue
Length of output: 2541
🏁 Script executed:
# Check the watch.py file to see the full context of _conversation_count
cat -n polylogue/pipeline/watch.py | head -100Repository: Sinity/polylogue
Length of output: 2566
🏁 Script executed:
# Find all usages of _conversation_count
rg '_conversation_count' --type=py -B 2 -A 2Repository: Sinity/polylogue
Length of output: 29489
🏁 Script executed:
# Get the full _SparseIntMap definition
rg -B 5 -A 20 'class _SparseIntMap' polylogue/storage/state_views.pyRepository: Sinity/polylogue
Length of output: 888
🏁 Script executed:
# Check if there are any other places where counts is assigned a plain dict/Mapping to RunResult
rg 'RunResult\s*\(' --type=py -A 3 | grep -A 3 'counts\s*='Repository: Sinity/polylogue
Length of output: 2804
Type _conversation_count parameter to RunResult instead of object to eliminate unnecessary defensive code.
WatchRunner._sync_fn() returns RunResult, which always has counts: RunCounts. Since RunCounts provides a callable int_value(field_name: str) method, narrowing the parameter type from object to RunResult allows direct access without getattr fallbacks. The Mapping branch is unreachable—every instantiation of RunResult in the codebase uses RunCounts, never a plain dict.
♻️ Proposed simplification
-from collections.abc import Callable, Mapping
+from collections.abc import Callable
@@
-def _conversation_count(result: object) -> int:
- counts = getattr(result, "counts", None)
- int_value = getattr(counts, "int_value", None)
- if callable(int_value):
- value = int_value("conversations")
- return value if isinstance(value, int) else 0
- if isinstance(counts, Mapping):
- value = counts.get("conversations", 0)
- return value if isinstance(value, int) else 0
- return 0
+def _conversation_count(result: RunResult) -> int:
+ return result.counts.int_value("conversations")🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@polylogue/pipeline/watch.py` around lines 58 - 67, Change the
_conversation_count signature to accept a RunResult (not object) and remove the
unreachable Mapping and getattr fallbacks: inside _conversation_count(result:
RunResult) call result.counts.int_value("conversations") directly (using
RunCounts.int_value) and return the integer if isinstance(..., int) else 0;
update any callers (e.g., WatchRunner._sync_fn) if necessary to keep types
consistent. This eliminates defensive type checks and relies on RunResult.counts
being a RunCounts instance.
| def test_cursor_state_records_failures(self) -> None: | ||
| """Rejected entries record failures in cursor_state.""" | ||
| cursor_state: dict[str, Any] = {"failed_files": [], "failed_count": 0} | ||
| cursor_state = _seeded_cursor_state() | ||
| validator = _ZipEntryValidator( | ||
| "chatgpt", | ||
| cursor_state=cursor_state, | ||
| zip_path=Path("archive.zip"), | ||
| ) | ||
| bomb_entry = self._make_zip_info("bomb.json", file_size=500000, compress_size=1) | ||
| list(validator.filter_entries([bomb_entry])) | ||
| assert int(cursor_state["failed_count"]) >= 1 | ||
| assert len(cast(list[object], cursor_state["failed_files"])) >= 1 | ||
| failed_files: list[CursorFailurePayload] = cursor_state.get("failed_files", []) | ||
| assert cursor_state["failed_count"] >= 1 | ||
| assert len(failed_files) >= 1 |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
Minor: explicit typing of .get default.
cursor_state.get("failed_files", []) returns list[CursorFailurePayload] | list[Never] under strict TypedDict semantics. The inline annotation is fine at runtime, but mypy may complain about the default literal. Consider routing through the same helper used in tests/integration/test_security.py (_failed_files) to keep the access pattern uniform across tests.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@tests/unit/sources/test_decoders.py` around lines 298 - 310, The test uses
cursor_state.get("failed_files", []) which can produce an untyped default
literal; change the access to use the shared helper _failed_files(cursor_state)
(the same helper used in tests/integration/test_security.py) so the result is
correctly typed as list[CursorFailurePayload]; update the assertion variables in
test_cursor_state_records_failures to call _failed_files(cursor_state) instead
of cursor_state.get(...) and keep the rest of the assertions the same (the test
still seeds cursor_state via _seeded_cursor_state and exercises
_ZipEntryValidator).
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6c328b96b6
ℹ️ 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".
Ref #235 Ref #222 ## Summary - renew transient run-state and plan-state contracts around `PlanResult`, `RunResult`, cursor payloads, and run drift/count snapshots - propagate those typed contracts through planning, acquisition, render/watch helpers, and the affected test surfaces - keep persisted `RunRecord` JSON boundaries broad while making the transient pipeline/runtime layer explicit and typed ## Problem The run-state seam was still path-dependent and loosely modeled. Planning counts/details, cursor snapshots, run counts, drift, and render failures moved through broad dicts, which made the pipeline runtime ambiguous and forced repeated coercion at the CLI/test boundaries. ## Solution - introduced explicit transient state models and payload aliases in `polylogue/storage/state_views.py` for plan counts/details, run counts, drift buckets, cursor state, and render failures - rewired planning and execution state in `polylogue/pipeline/services/planning_runtime.py`, `polylogue/pipeline/run_state.py`, and `polylogue/pipeline/run_finalization.py` to use those models directly - propagated the tighter cursor and render-failure contracts through source acquisition/decoding helpers plus `watch.py`, `run_stages.py`, and `pipeline/services/rendering.py` - updated the affected CLI, pipeline, source, and integration tests to construct typed run/plan fixtures explicitly instead of raw dict payloads ## Verification - `pytest -q tests/unit/cli/test_run.py tests/unit/cli/test_run_laws.py tests/unit/cli/test_run_int.py tests/unit/cli/test_deterministic_output.py tests/unit/pipeline/test_run_sources.py tests/unit/pipeline/test_parsing_service.py tests/unit/pipeline/test_observers.py tests/unit/pipeline/test_watch.py` - `mypy polylogue/pipeline/watch.py polylogue/pipeline/run_stages.py polylogue/pipeline/services/rendering.py tests/unit/cli/test_run.py tests/unit/cli/test_run_int.py tests/unit/cli/test_run_laws.py tests/unit/sources/test_drive_ops.py tests/unit/sources/test_decoders.py tests/unit/sources/test_acquisition_encoding.py tests/unit/sources/test_source_laws.py tests/integration/test_security.py tests/unit/pipeline/test_run_sources.py tests/integration/test_workflows.py` - `devtools verify`
The run-state seam was still path-dependent and loosely modeled. Planning counts/details, cursor snapshots, run counts, drift, and render failures moved through broad dicts, making the pipeline runtime ambiguous and forcing repeated coercion at the CLI/test boundaries. Persisted `RunRecord` JSON boundaries stay broad by design; the transient pipeline/runtime layer is what tightens here. Introduces explicit transient state models and payload aliases in `storage/state_views.py` for plan counts/details, run counts, drift buckets, cursor state, and render failures. Rewires planning and execution state in `pipeline/services/planning_runtime.py`, `pipeline/run_state.py`, and `pipeline/run_finalization.py` to use those models directly. Propagates the tighter cursor and render- failure contracts through source acquisition/decoding helpers plus `watch.py`, `run_stages.py`, and `pipeline/services/rendering.py`. Affected CLI, pipeline, source, and integration tests construct typed run/plan fixtures explicitly instead of raw dict payloads. Verification: focused pytest and mypy over the touched run/pipeline/ source tests; devtools verify green. Ref #235 Ref #222
The run-state seam was still path-dependent and loosely modeled. Planning counts/details, cursor snapshots, run counts, drift, and render failures moved through broad dicts, making the pipeline runtime ambiguous and forcing repeated coercion at the CLI/test boundaries. Persisted `RunRecord` JSON boundaries stay broad by design; the transient pipeline/runtime layer is what tightens here. Introduces explicit transient state models and payload aliases in `storage/state_views.py` for plan counts/details, run counts, drift buckets, cursor state, and render failures. Rewires planning and execution state in `pipeline/services/planning_runtime.py`, `pipeline/run_state.py`, and `pipeline/run_finalization.py` to use those models directly. Propagates the tighter cursor and render- failure contracts through source acquisition/decoding helpers plus `watch.py`, `run_stages.py`, and `pipeline/services/rendering.py`. Affected CLI, pipeline, source, and integration tests construct typed run/plan fixtures explicitly instead of raw dict payloads. Verification: focused pytest and mypy over the touched run/pipeline/ source tests; devtools verify green. Ref #235 Ref #222
The run-state seam was still path-dependent and loosely modeled. Planning counts/details, cursor snapshots, run counts, drift, and render failures moved through broad dicts, making the pipeline runtime ambiguous and forcing repeated coercion at the CLI/test boundaries. Persisted `RunRecord` JSON boundaries stay broad by design; the transient pipeline/runtime layer is what tightens here. Introduces explicit transient state models and payload aliases in `storage/state_views.py` for plan counts/details, run counts, drift buckets, cursor state, and render failures. Rewires planning and execution state in `pipeline/services/planning_runtime.py`, `pipeline/run_state.py`, and `pipeline/run_finalization.py` to use those models directly. Propagates the tighter cursor and render- failure contracts through source acquisition/decoding helpers plus `watch.py`, `run_stages.py`, and `pipeline/services/rendering.py`. Affected CLI, pipeline, source, and integration tests construct typed run/plan fixtures explicitly instead of raw dict payloads. Verification: focused pytest and mypy over the touched run/pipeline/ source tests; devtools verify green. Ref #235 Ref #222
|
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. |
Ref #235
Ref #222
Summary
PlanResult,RunResult, cursor payloads, and run drift/count snapshotsRunRecordJSON boundaries broad while making the transient pipeline/runtime layer explicit and typedProblem
The run-state seam was still path-dependent and loosely modeled. Planning counts/details, cursor snapshots, run counts, drift, and render failures moved through broad dicts, which made the pipeline runtime ambiguous and forced repeated coercion at the CLI/test boundaries.
Solution
polylogue/storage/state_views.pyfor plan counts/details, run counts, drift buckets, cursor state, and render failurespolylogue/pipeline/services/planning_runtime.py,polylogue/pipeline/run_state.py, andpolylogue/pipeline/run_finalization.pyto use those models directlywatch.py,run_stages.py, andpipeline/services/rendering.pyVerification
pytest -q tests/unit/cli/test_run.py tests/unit/cli/test_run_laws.py tests/unit/cli/test_run_int.py tests/unit/cli/test_deterministic_output.py tests/unit/pipeline/test_run_sources.py tests/unit/pipeline/test_parsing_service.py tests/unit/pipeline/test_observers.py tests/unit/pipeline/test_watch.pymypy polylogue/pipeline/watch.py polylogue/pipeline/run_stages.py polylogue/pipeline/services/rendering.py tests/unit/cli/test_run.py tests/unit/cli/test_run_int.py tests/unit/cli/test_run_laws.py tests/unit/sources/test_drive_ops.py tests/unit/sources/test_decoders.py tests/unit/sources/test_acquisition_encoding.py tests/unit/sources/test_source_laws.py tests/integration/test_security.py tests/unit/pipeline/test_run_sources.py tests/integration/test_workflows.pydevtools verify