Skip to content

improve(telegram): avoid redundant parsing of formatted replies - #148285

Merged
steipete merged 1 commit into
mainfrom
codex/perf-telegram-role-parse-guard-r12
Sep 14, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/perf-telegram-role-parse-guard-r12

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Closes #148284

What Problem This Solves

Long formatted Telegram replies spend avoidable time reparsing visible text while finding message chunk boundaries.

User Impact

Formatting the measured long reply takes 23% less time on Node 26 and 28% less on Node 24, with identical output and chunk boundaries. Some annotation and raw-HTML cases instead add about 20–32 microseconds on average. Whole-child RSS rises slightly; this is not a universal speedup or memory-saving claim.

Why This Change Was Made

After the existing HTML projection, skip the additional Markdown parse only when its text contains no literal or potentially entity-encoded header delimiter. Retain the original parser and code-exclusion masking whenever [, <, or & occurs. The change stays in the existing private predicate and preserves role-header protection, entity decoding, formatting, and limits.

Evidence

  • All 131 focused formatter, Markdown wrapping, and transcript annotation tests passed against both baseline and candidate. The existing final-HTML-chunk test now also covers projected angle brackets, deferred numeric brackets, and deferred delimiters inside code. Targeted lint, extension production/test types, formatting, and diff checks passed.
  • All 131 tests passed again after refreshing onto main. An initial explicit-config invocation selected no tests; the corrected repository routing ran the intended unit and Telegram shards. No test assertion or timeout was changed.
  • Fresh independent source review found no actionable P0–P2 issues. A separate reviewer audited both measurement cohorts and accepted the long-message benefit with the adverse results below.
  • Each fixed cohort ran 24 serial children in repeated baseline/candidate/candidate/baseline order on Windows, using Node 26.8.2 and 24.19.0: six pairs per runtime, three warmup and three measured batches per cell. Each independently audited cohort preserved 360 reference outputs, 1,080 controls, 192 renderer callback traces, and 39,024 warmup plus 39,024 measured calls. No retries, exclusions, forced GC, or pooling between cohorts.
  • Calls exercised actual markdownToTelegramChunks and splitTelegramHtmlChunks, plus six unchanged Slack controls. Quiet confirmation followed coordinated settlement of other task jobs; ordinary OS/user activity remained. Screening overlapped unrelated work. These are complete local conversions/splits, not Telegram API or Gateway delivery measurements.

Quiet mean milliseconds per complete conversion/split (baseline → candidate):

Telegram fixture Node 26 Node 24
Plain short 0.01457 → 0.01221 0.01531 → 0.01043
Styled links 0.31271 → 0.27819 0.32012 → 0.30205
Styled multichunk 1872.28 → 1444.35 2121.58 → 1519.79
Dense links 0.53769 → 0.46881 0.59712 → 0.52866
Fenced code 0.20562 → 0.12061 0.22990 → 0.11016
Annotations 0.24185 → 0.26232 0.23173 → 0.25505
Raw HTML entities 0.55906 → 0.59076 0.61597 → 0.64176
Raw HTML code/Unicode 0.15046 → 0.17253 0.16549 → 0.16122
Markdown Unicode 0.74873 → 0.72533 0.89529 → 0.83941

The long styled fixture saves 428/602 ms on Node 26/24, with all paired means and medians improving. The separate contended cohort saves 516/526 ms and has the same pair consistency. Long-workload CPU means also fall 22.85%/28.18% in quiet confirmation; small-cell CPU readings are heavily quantized and support no general CPU claim.

Counterevidence, separate screening results, and reproducibility scope

Quiet annotation means add 20.47/23.32 µs; five of six Node 26 and four of six Node 24 paired means regress. Their pooled medians change less: +3.05 µs on Node 26 and effectively unchanged on Node 24. Raw HTML code/Unicode on Node 26 adds 22.07 µs mean and 32.20 µs median, with five of six pairs slower by both measures. Node 24's aggregate reverses its screening regression but retains a +96.76% adverse paired mean (+98.97 µs). Raw HTML entity means add 31.70/25.80 µs. Node 26 plain-short retains a final pair with +65.34% mean and +68.60% median, approximately 9.3 µs extra. Dense-link and Markdown-Unicode aggregate improvements retain opposing pairs; they are not headline acceptance evidence.

Separate contended mean milliseconds per conversion/split:

Telegram fixture Node 26 Node 24
Plain short 0.01811 → 0.01064 0.02432 → 0.01186
Styled links 0.38182 → 0.30899 0.37189 → 0.33402
Styled multichunk 2103.15 → 1586.83 2172.26 → 1645.95
Dense links 0.59533 → 0.55056 0.58719 → 0.47084
Fenced code 0.23249 → 0.13027 0.24540 → 0.10668
Annotations 0.27036 → 0.27924 0.27576 → 0.21830
Raw HTML entities 0.67199 → 0.64090 0.66402 → 0.72305
Raw HTML code/Unicode 0.17394 → 0.18346 0.18269 → 0.19779
Markdown Unicode 0.87704 → 0.80176 0.97072 → 0.89109

The six unchanged Slack controls also vary. Quiet mean changes for plain, styled links, multichunk, dense links, fenced code, and annotations are respectively −3.78%, −3.96%, +0.035%, −0.11%, −1.89%, −1.16% on Node 26 and −3.76%, −1.64%, +0.67%, +0.21%, +11.35%, −1.89% on Node 24. Screening changes are +5.17%, +3.45%, −0.18%, −4.44%, −0.78%, +6.78% and +1.46%, −11.55%, −1.43%, −9.06%, +4.16%, −8.92%. They demonstrate residual variability; they do not provide a correction factor or excuse recurring regressions.

Quiet mean peak RSS rises 218.32 → 219.64 MiB on Node 26 and 247.12 → 248.18 MiB on Node 24. Final RSS adds 2.00/0.16 MiB. Screening peak means are 222.42 → 221.89 MiB and 246.64 → 247.60 MiB. These whole-child readings include imports, controls, and all workloads, and establish neither allocation savings nor retained-memory improvement. Quiet baseline/candidate zero-CPU batches are 141/146 of 270 on Node 26 and 131/150 of 270 on Node 24.

The benchmark retains all timing batches, references, controls, runtime identities, graph captures, and resource observations. The independent saved-data auditor verifies fixtures, outputs, callback traces, pinned source/runtime identities, and all cells. It does not independently implement the whole Markdown parser or replay discarded per-invocation outputs and warmups. Setup and reference checking stay outside timed calls.

Measured baseline: e3b9b3810976cadf1cf0a02e342cea93d30b9b0b. Refreshed PR base: 8638c54e15f8323f1c4926ade7cffa86226ed1c9, preserving exact owner/test bytes. Candidate production SHA-256: 35998826328f8307542f470f03c749e69179fb1c9bbc1a8c0074030eb0f82378. Frozen benchmark manifest: 30eb0325aed30058c8af82690e3aa86169fd7c4e3698f2c1efda86433186f1f2; quiet audit: 00a80b61935c5cb3b7f216d32b1bafed70b071d665f372ceaa6f8069674dc5e9; contended audit: fc7f83719530e810873f69c73c51f95c855b17077a95613fa77fae1e7a361c7b. These identify retained local evidence, not downloadable artifacts. No live Telegram transport or cross-platform benchmark was run.

@clawsweeper

clawsweeper Bot commented Sep 14, 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 14, 2026 14:06
@openclaw-barnacle openclaw-barnacle Bot added channel: telegram Channel integration: telegram size: XS maintainer Maintainer-authored PR labels Sep 14, 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 14, 2026
@clawsweeper

clawsweeper Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 14, 2026, 10:23 AM ET / 14:23 UTC.

ClawSweeper review

What this changes

The Telegram formatter skips unnecessary Markdown parsing when checking formatted replies for transcript-role headers and expands coverage for encoded delimiters and code examples.

Merge readiness

✅ Ready for maintainer review

The optimization remains useful: current main still performs the redundant parse. No actionable correctness or security defect was found, and the supplied measurements support the narrowly stated performance benefit.

Priority: P2
Reviewed head: 95890f78f6dd297bad15036c4a2c44503c4631f5

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, maintainable optimization with useful production-entrypoint measurements, candid counterevidence, and no blocking findings.
Proof confidence 🐚 platinum hermit (4/6) Not applicable: The external-contributor proof gate does not apply to this maintainer-authored internal optimization; supplied Windows measurements exercise the actual formatter and splitter and report identical output, but establish no transport-latency claim.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The external-contributor proof gate does not apply to this maintainer-authored internal optimization; supplied Windows measurements exercise the actual formatter and splitter and report identical output, but establish no transport-latency claim.
Evidence reviewed 7 items Introduced change: The verified merge-base-to-head patch adds only the delimiter guard and expands the existing final-chunk test; base-branch drift is not attributed to this PR.
Delimiter invariant and implementation boundary: All three recognized header forms require '[' or '<'. The guard also retains parsing for '&', covering deferred entity decoding. The SDK resolves Markdown parsing to this repository’s markdown-core package; no Codex dependency contract is involved.
Current main still needs the optimization: The fetched main implementation calls markdownToIR unconditionally after projection. HTML projection, entity decoding, excluded code ranges, and prefix insertion remain unchanged in the candidate.
Findings None None.
Security None None.

How this fits together

Telegram’s formatter converts assistant replies into HTML and splits them into bounded messages. Its transcript-header check prevents assistant-generated text from appearing to introduce another speaker.

flowchart TD
  A[Assistant reply] --> B[Telegram HTML formatting]
  B --> C[Project visible text]
  C --> D{Possible header delimiter?}
  D -->|No| F[Keep formatted content]
  D -->|Yes| E[Parse with code exclusions]
  E --> F
  F --> G[Bounded Telegram messages]
Loading

Before merge

None.

Agent review details

Security

None.

PR surface

Source +8, Tests +12. Total +20 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 8 0 +8
Tests 1 22 10 +12
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 30 10 +20

Review metrics

Metric Value Why it matters
Reported long-reply formatting benefit 23% faster on Node 26; 28% on Node 24 The measured benefit concerns complete local conversions; disclosed small-fixture regressions prevent a universal speedup claim.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #148284
Summary: This PR implements the paired issue’s requested formatter optimization; the issue remains open pending merge.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Technical review

Best possible solution:

Keep the optimization inside the existing private predicate while preserving the original parser for every potentially encoded header.

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

Not applicable as a functional bug reproduction: this is an optimization, supported by source-confirmed redundant work and supplied before/after formatter measurements.

Is this the best way to solve the issue?

Yes. The conservative delimiter check avoids unnecessary work without replacing the parser, changing public APIs, or duplicating header recognition.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: This is a bounded performance improvement for long formatted Telegram replies, without evidence of an urgent delivery failure.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit 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 external-contributor proof gate does not apply to this maintainer-authored internal optimization; supplied Windows measurements exercise the actual formatter and splitter and report identical output, but establish no transport-latency claim.

Label justifications:

  • P2: This is a bounded performance improvement for long formatted Telegram replies, without evidence of an urgent delivery failure.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit 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 external-contributor proof gate does not apply to this maintainer-authored internal optimization; supplied Windows measurements exercise the actual formatter and splitter and report identical output, but establish no transport-latency claim.

Evidence

What I checked:

  • Introduced change: The verified merge-base-to-head patch adds only the delimiter guard and expands the existing final-chunk test; base-branch drift is not attributed to this PR. (extensions/telegram/src/format-assistant-transcript.ts:35, 95890f78f6dd)
  • Delimiter invariant and implementation boundary: All three recognized header forms require '[' or '<'. The guard also retains parsing for '&', covering deferred entity decoding. The SDK resolves Markdown parsing to this repository’s markdown-core package; no Codex dependency contract is involved. (packages/markdown-core/src/assistant-transcript-headers.ts:93, 95890f78f6dd)
  • Current main still needs the optimization: The fetched main implementation calls markdownToIR unconditionally after projection. HTML projection, entity decoding, excluded code ranges, and prefix insertion remain unchanged in the candidate. (extensions/telegram/src/format-assistant-transcript.ts:35, 10d67c803047)
  • Latest release comparison: The v2026.9.4 tree records blob 8d7d349029ded5e1af93e9eb5b262e4dcc16a2a9 for the predicate owner, matching the pre-change blob in the introduced diff; this optimization is absent from that release. (extensions/telegram/src/format-assistant-transcript.ts, 3a9d69db306c)
  • Behavioral coverage: The expanded table exercises the real HTML splitter with literal brackets, projected angle brackets, deferred numeric entities, and excluded code. Assertions preserve chunk limits, suffix content, and expected prefix behavior without adding production test seams. (extensions/telegram/src/format.test.ts:373, 95890f78f6dd)
  • Supplied performance evidence: The captured PR body reports actual markdownToTelegramChunks and splitTelegramHtmlChunks conversions on Windows with Node 26.8.2 and 24.19.0, unchanged outputs, 23%/28% improvement for the long styled fixture, and adverse small-fixture/RSS results. The candidate file SHA-256 matches the supplied 35998826328f8307542f470f03c749e69179fb1c9bbc1a8c0074030eb0f82378. Raw benchmark artifacts are described as retained locally, not downloadable; measurements were not independently rerun. (extensions/telegram/src/format-assistant-transcript.ts:35, 95890f78f6dd)

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • Vincent Koc: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; 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 5523bc6 into main Sep 14, 2026
177 checks passed
@steipete
steipete deleted the codex/perf-telegram-role-parse-guard-r12 branch September 14, 2026 15:30
@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.

Long formatted Telegram replies repeatedly parse text without role headers

1 participant