Skip to content

fix(worker): use json.dumps for redis.publish payloads#136

Closed
JohnRDOrazio wants to merge 1 commit intodevfrom
fix/issue-111-worker-json-dumps
Closed

fix(worker): use json.dumps for redis.publish payloads#136
JohnRDOrazio wants to merge 1 commit intodevfrom
fix/issue-111-worker-json-dumps

Conversation

@JohnRDOrazio
Copy link
Copy Markdown
Member

@JohnRDOrazio JohnRDOrazio commented Apr 28, 2026

Summary

ontokit/worker.py previously built Redis pubsub payloads via f-string-formatted JSON. This was fragile, and outright buggy in three places — lint_failed, normalization_failed, and remote_check_failed interpolated str(e) directly into the JSON string, so any exception message containing a ", \, or newline produced invalid JSON for subscribers calling json.loads(payload).

This PR replaces every f-string JSON payload with json.dumps({...}). json was already imported at the top of the file.

Sites changed (11 total)

  • lint_started, lint_complete, lint_failed
  • normalization_status (status check task), normalization_started, normalization_complete, normalization_failed, normalization_status (per-project loop in check_all_projects_normalization cron)
  • remote_check_started, remote_check_complete, remote_check_failed

Schema unchanged — subscribers unaffected

  • Same type values, same field names, same ordering.
  • UUIDs are stringified the same way as before (str(run.id) etc.).
  • Booleans are now passed as actual Python bools — json.dumps emits true/false, replacing the prior str(x).lower() workaround. The wire format is identical.
  • Integer fields (e.g. issues_found) remain ints.

Test plan

  • ruff check ontokit/worker.py — passes
  • ruff format ontokit/worker.py — no changes
  • mypy ontokit/worker.py (strict) — passes
  • pytest tests/unit/test_worker.py -v — all 51 worker tests pass (worker.py coverage 99%)
  • Pre-commit hooks (ruff, ruff format, mypy) all pass on commit
  • Manually round-trip-verified all 11 payload schemas through json.dumpsjson.loads with adversarial error strings (containing ", \n, \\)

Closes #111

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Enhanced internal message handling and data serialization to ensure consistent formatting across system event publications.

The ARQ worker built Redis pubsub payloads via f-string-formatted JSON.
This was fragile, and outright buggy in three places — `lint_failed`,
`normalization_failed`, and `remote_check_failed` interpolated `str(e)`
directly, so any exception message containing a `"`, `\`, or newline
produced invalid JSON for subscribers calling `json.loads(payload)`.

Replace every f-string JSON payload with `json.dumps({...})` (json was
already imported). Schema is preserved exactly: same `type` values, same
field names and order, UUIDs as strings, booleans as real booleans
(json.dumps emits `true`/`false`, replacing the prior
`str(x).lower()` workaround), ints as ints. Subscribers are unaffected.

Sites converted (11 total):
- lint_started, lint_complete, lint_failed
- normalization_status (status check task), normalization_started,
  normalization_complete, normalization_failed,
  normalization_status (per-project loop in cron)
- remote_check_started, remote_check_complete, remote_check_failed

Closes #111

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d369e095-68b0-430e-bc78-98f387be4923

📥 Commits

Reviewing files that changed from the base of the PR and between a2f22f1 and ef5f1eb.

📒 Files selected for processing (1)
  • ontokit/worker.py

📝 Walkthrough

Walkthrough

Updated ontokit/worker.py to replace manually constructed f-string JSON payloads with json.dumps() calls for Redis pubsub messages. This fixes a bug where exception messages containing special characters (quotes, backslashes, newlines) could produce invalid JSON and break subscribers.

Changes

Cohort / File(s) Summary
Redis Pubsub Payload Refactoring
ontokit/worker.py
Replaced ~10 f-string-formatted JSON payloads with json.dumps() calls across lint (lint_started, lint_complete, lint_failed), normalization (normalization_started, normalization_complete, normalization_failed, normalization_status), and remote check (remote_check_started, remote_check_complete, remote_check_failed) event publications. Ensures proper escaping of exception strings and type correctness for fields like project_id, run_id, error, counts, and booleans.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 F-strings were fractured with sneaky " marks,
But json.dumps now brings harmony—see how it sparks!
Exception strings safe, no more broken JSON,
The rabbit's refactor has set things in motion! 🎯✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: switching from f-string JSON construction to json.dumps for redis.publish payloads in worker.py.
Linked Issues check ✅ Passed The PR comprehensively addresses all coding requirements from #111: converts all 11 redis.publish calls from f-strings to json.dumps with proper type handling (stringifying UUIDs, preserving booleans/ints) and fixes the critical bug of unescaped exception strings.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the objective: converting redis.publish payloads from f-strings to json.dumps. No schema changes, field additions, channel name modifications, or subscriber changes are present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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 fix/issue-111-worker-json-dumps

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 28, 2026

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
ontokit/worker.py 93.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@JohnRDOrazio
Copy link
Copy Markdown
Member Author

Closing as duplicate of #118 (opened 2026-04-27, includes a dedicated regression test for the lint_failed payload with quoted/newline-containing errors). The agent that opened this PR worked from a stale worktree base (9c35db4) and didn't check open PRs first.

@JohnRDOrazio JohnRDOrazio deleted the fix/issue-111-worker-json-dumps branch April 28, 2026 06: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.

Use json.dumps for redis.publish payloads in worker.py

1 participant