Skip to content

improve(telegram): speed up formatting long replies - #150213

Merged
steipete merged 1 commit into
mainfrom
codex/perf-telegram-visible-projection-r13
Sep 16, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf-telegram-visible-projection-r13

Conversation

@steipete

@steipete steipete commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #150211

What Problem This Solves

Formatting long Telegram replies spends CPU rebuilding visible text one character and entity at a time during repeated chunk-size probes.

User Impact

Long replies format faster with identical HTML and chunk boundaries. No configuration or migration is required.

Why This Change Was Made

Decode each text segment once with the existing entity decoder, then append the segment as a whole. Code exclusions and transcript-header protection keep the same visible text and UTF-16 offsets. This removes the per-character loop without adding a cache.

Evidence

The actual markdownToTelegramChunks caller was measured in 12 fresh processes: three alternating baseline/candidate pairs on each of Node 24.19.0 and 26.8.2. All 13,056 timed calls and every adverse result were retained. Median paired wall-time changes:

Input Node 24 Node 26
Long bold prose -50.6% -53.2%
Escaping-heavy reply -40.0% -41.2%
Link-heavy reply -6.7% -13.0%
Long code block -82.9% -81.9%
Short formatted reply +2.2% -0.9%

The short case is noisy (two of three Node 24 pairs were slower); no short-message improvement or network-latency claim. Windows CPU counters were too coarse for short cases. Inputs were synthetic, approximately 4–9.5K visible characters for the long cases, with a 4,000-character HTML cap. Parsing and formatting were timed together; imports, output comparisons, and warmups were outside the timed region.

  • Full output equality across 126 projection cases and five chunking fixtures in every process, including malformed/nested entities, astral characters, lone surrogates, and code exclusions.
  • 112 existing tests passed in format.test.ts and format.wrap-md.test.ts.
  • Independent review passed with no actionable P0–P2 findings.
  • Formatting checked with the repository formatter. Local type-aware lint reached the declaration builder and was blocked by its rejection of borrowed dependencies; Exact-head hosted CI passed, including lint, types, build, and selected tests.

@clawsweeper

clawsweeper Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review September 16, 2026 18:01
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: XS maintainer Maintainer-authored PR labels Sep 16, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 16, 2026
@clawsweeper

clawsweeper Bot commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 16, 2026, 2:07 PM ET / 18:07 UTC.

ClawSweeper review

What this changes

The PR reduces CPU work when formatting long Telegram replies by decoding and appending whole text segments while preserving HTML output and chunk boundaries.

Merge readiness

✅ Ready for maintainer review

Keep open: this is a focused, useful optimization that remains absent from current main and the latest release. Source review found no actionable correctness defect; the protected maintainer submission should proceed through normal review.

Priority: P2
Reviewed head: 289c254a5d97a3b88db338bb531f4b2a558551b2

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused simplification with credible reported performance measurements, relevant existing coverage, and no actionable source-review findings.
Proof confidence 🌊 off-meta tidepool Not applicable: The ordinary external-contributor proof gate does not apply to this maintainer submission. Its body reports after-change measurements through the actual Telegram chunking caller on Node 24 and 26 with identical outputs; those offline measurements were not independently rerun.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The ordinary external-contributor proof gate does not apply to this maintainer submission. Its body reports after-change measurements through the actual Telegram chunking caller on Node 24 and 26 with identical outputs; those offline measurements were not independently rerun.
Evidence reviewed 8 items Verified introduced change: The pinned merge-base-to-head diff only replaces per-character projection with one existing-decoder call per segment. Tag processing, exclusion boundaries, and header protection remain unchanged.
Equivalent decoding and offsets: The shared decoder performs one nonrecursive replacement pass, preserves unknown or invalid entities, and validates numeric code points. Appending the decoded segment preserves the same UTF-16 text length and merged exclusion ranges as appending its individual decoded values.
Production caller: Telegram text delivery calls markdownToTelegramChunks, whose rendered-size probes invoke the changed projection through HTML rendering. This matches the entrypoint named in the submitted performance measurements.
Findings None None.
Security None None.

How this fits together

Telegram’s formatter converts assistant Markdown into size-limited HTML messages. Its visible-text projection detects transcript-like role headers while excluding code, so outgoing chunks retain appropriate assistant identification.

flowchart LR
  A[Assistant Markdown] --> B[Telegram HTML rendering]
  B --> C[Decode visible text segments]
  C --> D[Exclude code from header detection]
  D --> E[Protect transcript headers]
  E --> F[Measure and split chunks]
  F --> G[Telegram delivery]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source -22. Total -22 across 1 file.

View PR surface stats
Area Files Added Removed Net
Source 1 2 24 -22
Tests 0 0 0 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 1 2 24 -22

Review metrics

Metric Value Why it matters
Reported formatter performance 40–83% lower median wall time for escaping-heavy, bold, and code-block fixtures Measurements exercise the production chunking caller on two Node versions; short replies remain mixed and network latency was not measured.

Technical review

Best possible solution:

Retain one shared entity decoder and the existing transcript-protection logic while removing repeated per-character projection work.

Do we have a high-confidence way to reproduce the issue?

Not applicable as a functional bug reproduction: this is a performance optimization. Current source confirms the repeated projection work, and the body reports comparative measurements; this review did not rerun them.

Is this the best way to solve the issue?

Yes. Reusing the existing decoder for each segment is a narrow simplification that preserves text and offsets without introducing caches, settings, or another formatting owner.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against ca38979d2128.

Labels

Label changes:

  • add P2: This is a bounded performance improvement to long Telegram reply formatting, with no demonstrated urgent workflow failure.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The ordinary external-contributor proof gate does not apply to this maintainer submission. Its body reports after-change measurements through the actual Telegram chunking caller on Node 24 and 26 with identical outputs; those offline measurements were not independently rerun.

Label justifications:

  • P2: This is a bounded performance improvement to long Telegram reply formatting, with no demonstrated urgent workflow failure.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The ordinary external-contributor proof gate does not apply to this maintainer submission. Its body reports after-change measurements through the actual Telegram chunking caller on Node 24 and 26 with identical outputs; those offline measurements were not independently rerun.

Evidence

What I checked:

Likely related people:

  • unknown: The claimed source-line change could not be verified from bounded local history. (role: source history unknown; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@steipete
steipete merged commit d3d5c29 into main Sep 16, 2026
180 checks passed
@steipete
steipete deleted the codex/perf-telegram-visible-projection-r13 branch September 16, 2026 18:19
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: telegram Channel integration: telegram maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce CPU used to format long Telegram replies

1 participant