fix(ops): three targeted fixes — MarkdownV2 escaping, CPU tracer args… - #119
Conversation
…, jq fallback
rw_cpu_guard.sh:
- Add escape_md_v2() helper: escapes all 20 MarkdownV2 special characters
(backslash processed first to prevent double-escaping)
- tg_alert(): escape the full composed text (prefix + caller message) via
escape_md_v2() before sending; switch parse_mode Markdown → MarkdownV2;
raw ${short_cmd} can no longer break message formatting or leak syntax
- CPU tracer: switch ps format 'cmd' → 'args' so the full executable path
and every argument are captured; combined with the existing $5..$NF loop
this gives a complete, delimiter-safe command string in every CSV row
runewager_redeploy.sh:
- _health_check pretty-print cascade: python3 -m json.tool → jq . → raw;
diagnostics never fail when neither pretty-printer is installed
https://claude.ai/code/session_01WpEqAiDbpqnBywMjYXJHf4
|
CodeAnt AI is reviewing your PR. Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
Reviewer's GuideUpdates the CPU guard and redeploy scripts to make Telegram alerts safe under MarkdownV2, improve CPU forensics command capture, and make health-check payload pretty-printing degrade gracefully across available tools. Sequence diagram for updated Telegram MarkdownV2 alert flowsequenceDiagram
actor Admin
participant rw_cpu_guard
participant TelegramAPI
Admin->>rw_cpu_guard: triggers tg_alert(msg)
rw_cpu_guard->>rw_cpu_guard: escape_md_v2("🛡 [rw_cpu_guard] " + msg)
rw_cpu_guard->>rw_cpu_guard: set safe_text
rw_cpu_guard->>rw_cpu_guard: check TG_BOT_TOKEN and TG_ADMIN_IDS
rw_cpu_guard->>rw_cpu_guard: enforce _ALERT_COOLDOWN
loop for each id in TG_ADMIN_IDS
rw_cpu_guard->>TelegramAPI: POST sendMessage
TelegramAPI-->>rw_cpu_guard: 200 OK or error
end
rw_cpu_guard-->>Admin: alert sent using MarkdownV2
Flow diagram for CPU tracer command capture with argsflowchart TD
A[start _run_forensics CPU tracer] --> B[Create trace CSV with header]
B --> C[Start 1 second sampling loop]
C --> D[ps -eo pid,ppid,pcpu,pmem,args --sort=-pcpu]
D --> E[Filter processes with pcpu > 15]
E --> F[Join fields 5..NF into cmd string]
F --> G[Replace commas in cmd with semicolons]
G --> H[Write timestamp,pid,ppid,cpu%,mem%,cmd to trace CSV]
H --> I{More samples?}
I -->|Yes| C
I -->|No| J[End CPU tracer]
Flow diagram for health_check pretty-print cascadeflowchart TD
A[start _health_check] --> B[curl HEAD to HEALTH_URL]
B --> C{http_code == 200?}
C -->|No| D[warn Bot not healthy]
D --> E[end]
C -->|Yes| F[ok Bot healthy]
F --> G[curl GET HEALTH_URL into _payload]
G --> H{python3 available?}
H -->|Yes| I[Pretty-print via python3 -m json.tool]
I --> J{python3 pretty-print succeeded?}
J -->|Yes| K[Indent and show first 20 lines]
J -->|No| L[Indent and show raw first 20 lines]
K --> E
L --> E
H -->|No| M{jq available?}
M -->|Yes| N[Pretty-print via jq .]
N --> O{jq pretty-print succeeded?}
O -->|Yes| P[Indent and show first 20 lines]
O -->|No| Q[Indent and show raw first 20 lines]
P --> E
Q --> E
M -->|No| R[Indent and show raw first 20 lines]
R --> E[end]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Caution Review failedThe pull request is closed. ℹ️ Recent review infoConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PR enhances two shell scripts: adding JSON pretty-printing with cascading format tool fallbacks to health check output in runewager_redeploy.sh, and improving Telegram message safety through MarkdownV2 escaping while capturing full command arguments in forensics data collection within rw_cpu_guard.sh. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
✨ Finishing Touches
🧪 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.
Hey - I've found 1 issue, and left some high level feedback:
- In the CPU tracer, the CSV header still says
cmdwhile the data now comes fromargs; consider updating the header label to match the new field for clarity and downstream parsing. - The
_health_checkpretty-printing logic repeats the sameecho ... | head -20 | sed 's/^/ /'fallback in multiple branches; consider extracting that into a small helper to reduce duplication and make the cascade easier to follow.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the CPU tracer, the CSV header still says `cmd` while the data now comes from `args`; consider updating the header label to match the new field for clarity and downstream parsing.
- The `_health_check` pretty-printing logic repeats the same `echo ... | head -20 | sed 's/^/ /'` fallback in multiple branches; consider extracting that into a small helper to reduce duplication and make the cascade easier to follow.
## Individual Comments
### Comment 1
<location path="scripts/runewager_redeploy.sh" line_range="257-258" />
<code_context>
local _payload; _payload=$(curl -sS --max-time 3 "$HEALTH_URL" 2>/dev/null)
if command -v python3 >/dev/null 2>&1; then
- echo "$_payload" | python3 -m json.tool 2>/dev/null | head -20 | sed 's/^/ /' || echo "$_payload" | head -20 | sed 's/^/ /'
+ echo "$_payload" | python3 -m json.tool 2>/dev/null | head -20 | sed 's/^/ /' \
+ || echo "$_payload" | head -20 | sed 's/^/ /'
+ elif command -v jq >/dev/null 2>&1; then
+ echo "$_payload" | jq . 2>/dev/null | head -20 | sed 's/^/ /' \
</code_context>
<issue_to_address>
**issue (bug_risk):** The pretty-print fallback on JSON-tool failure probably never triggers because the pipeline exit status is dominated by `sed`.
Because this is in a pipeline, the exit code comes from the last command (`sed`), so the `|| echo "$_payload" ...` fallback will almost never run even if `python3 -m json.tool` fails or outputs nothing. The same applies to the `jq` branch.
To make the fallback work on parse failure, either capture the `json.tool` output and branch explicitly:
```bash
if ! _formatted=$(printf '%s' "$_payload" | python3 -m json.tool 2>/dev/null); then
printf '%s
' "$_payload" | head -20 | sed 's/^/ /'
else
printf '%s
' "_formatted" | head -20 | sed 's/^/ /'
fi
```
or enable `set -o pipefail` so the pipeline fails when `python3`/`jq` fail, and keep the `||` form.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| echo "$_payload" | python3 -m json.tool 2>/dev/null | head -20 | sed 's/^/ /' \ | ||
| || echo "$_payload" | head -20 | sed 's/^/ /' |
There was a problem hiding this comment.
issue (bug_risk): The pretty-print fallback on JSON-tool failure probably never triggers because the pipeline exit status is dominated by sed.
Because this is in a pipeline, the exit code comes from the last command (sed), so the || echo "$_payload" ... fallback will almost never run even if python3 -m json.tool fails or outputs nothing. The same applies to the jq branch.
To make the fallback work on parse failure, either capture the json.tool output and branch explicitly:
if ! _formatted=$(printf '%s' "$_payload" | python3 -m json.tool 2>/dev/null); then
printf '%s
' "$_payload" | head -20 | sed 's/^/ /'
else
printf '%s
' "_formatted" | head -20 | sed 's/^/ /'
fior enable set -o pipefail so the pipeline fails when python3/jq fail, and keep the || form.
Nitpicks 🔍
|
|
CodeAnt AI finished reviewing your PR. |
User description
…, jq fallback
rw_cpu_guard.sh:
runewager_redeploy.sh:
https://claude.ai/code/session_01WpEqAiDbpqnBywMjYXJHf4
Summary by Sourcery
Improve robustness of operational scripts for CPU alerting and deployment health checks.
Bug Fixes:
CodeAnt-AI Description
Fix Telegram alert formatting, capture full process commands in CPU traces, and reliably pretty-print health check output
What Changed
Impact
✅ Safer Telegram alerts (no broken or malformed messages)✅ Complete CPU command traces for accurate forensics✅ Clearer health diagnostics even without formatting tools💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.
Summary by CodeRabbit