Repository navigation
fix(telegram): properly nest link tags inside bold/italic formatting - #4249
pradeeppeddineni wants to merge 1 commit into
Conversation
| // Close links before styles to maintain proper nesting order | ||
| // (links are typically nested inside styles like bold/italic) | ||
| const closingLinks = linkEnds.get(pos); | ||
| if (closingLinks && closingLinks.length > 0) { |
There was a problem hiding this comment.
[P2] Consider keeping renderMarkdownWithMarkers free of hard-coded assumptions about nesting direction.
With the current ordering, links always close before styles at the same pos. That fixes Telegram HTML, but for other renderers (src/slack/format.ts, src/markdown/tables.ts) the correct nesting order may differ (e.g., if a renderer chooses to wrap style markers inside link markup). If you expect links to always be “inner”, it may be worth documenting that invariant at the RenderLink/IR level rather than encoding it here.
Also appears in behavior for: src/slack/format.ts:102, src/markdown/tables.ts:23 (they share this renderer).
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/markdown/render.ts
Line: 96:99
Comment:
[P2] Consider keeping `renderMarkdownWithMarkers` free of hard-coded assumptions about nesting direction.
With the current ordering, links always close before styles at the same `pos`. That fixes Telegram HTML, but for other renderers (`src/slack/format.ts`, `src/markdown/tables.ts`) the correct nesting order may differ (e.g., if a renderer chooses to wrap style markers inside link markup). If you expect links to always be “inner”, it may be worth documenting that invariant at the `RenderLink`/IR level rather than encoding it here.
Also appears in behavior for: `src/slack/format.ts:102`, `src/markdown/tables.ts:23` (they share this renderer).
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.| it("renders emoji before bold correctly", () => { | ||
| const res = markdownToTelegramHtml("🧪 **Header text**"); | ||
| expect(res).toBe("🧪 <b>Header text</b>"); |
There was a problem hiding this comment.
[P3] This test name doesn’t match the behavior under test.
renders emoji before bold correctly doesn’t exercise link nesting (the PR’s stated bug), and the same case could be covered by a more general “non-ASCII prefix” test elsewhere. If this is meant as a regression for a specific prior bug, consider renaming it to reference the issue it guards against (or removing it if it’s unrelated).
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/telegram/format.test.ts
Line: 61:63
Comment:
[P3] This test name doesn’t match the behavior under test.
`renders emoji before bold correctly` doesn’t exercise link nesting (the PR’s stated bug), and the same case could be covered by a more general “non-ASCII prefix” test elsewhere. If this is meant as a regression for a specific prior bug, consider renaming it to reference the issue it guards against (or removing it if it’s unrelated).
How can I resolve this? If you propose a fix, please make it concise.bfc1ccb to
f92900f
Compare
… a Gateway crash (#199499) ## What Problem This Solves Fixes: a conversation that ended its turn waiting for a background command to exit never hears about it again when the Gateway crashes or is killed mid-wait. The exit wake lived only in memory, and the restart resume marker is written only by a graceful shutdown, so after a crash the conversation hung silently. ## User Impact User impact: after any Gateway restart, crashes included, each conversation that was waiting on an ordinary background command gets one notice for that command. The notice names the process handle and start time and says what happened to the command, so the agent can check its output and run it again. Durable commands (`durable: true`) keep their existing adopted exit wake and get no notice. Behavior changes: - The generic "Background exec processes were stopped; rerun them if still needed" sentence in the graceful-restart resume input is gone. The per-command notice replaces it. - A background command whose exit would not wake the conversation, such as a service with `notifyOnExit` off, no longer causes a restart resume turn. ## Why This Change Was Made The process registry already owns the `durable_background_processes` table, its startup pass and the lost-process wake. It now also keeps a best-effort row for each running ordinary command whose exit would wake its conversation, on every platform: - The row reuses the existing table with `kind: "ordinary"` and the generic command summary. It needs no new table, column or schema version. - The row is written when the command is backgrounded and deleted when the command exits. - Incognito sessions are never recorded, because their state stays in memory. - Sandbox commands are recorded with a `sandboxed` flag. The Gateway cannot see inside the container, so their notice says the command may still be running. - Windows runs the startup pass too (durable launches stay Linux and macOS only), so Windows waiters keep a restart notice. On startup, the existing durable pass: - reports every remaining row once through the existing completion path, under the session's current authority, then deletes it; - rechecks eligibility as for durable outcomes (reset, archived and hidden subagent sessions, or `notifyOnExit` off, get nothing); - waits for agent database readiness, which finishes after the listener binds after a crash. The graceful-restart marker no longer tracks background processes (`hadBackgroundExec` is removed), so one owner reports lost commands. The notice text follows a measured fact. In each case below, the Gateway process alone was killed and a real agent's background commands were watched: | Host | Gateway killed (SIGKILL) | Graceful stop | | --- | --- | --- | | macOS, plain and under a launchd LaunchAgent | A silent command keeps running as an orphan; one that writes output dies at its next write | All stopped | | Linux (systemd 255 user service with the installed policy, and plain) | All stopped within 2 s | All stopped | The startup pass reads the Gateway boot record to tell a crash from a shutdown. After a shutdown, or a crash on Linux, the notice says the command was stopped. After a crash on macOS it says the command may still be running, but its result will not wake this conversation. No orphan tracking is added; `durable: true` remains the way to keep a command alive. ## Evidence Real-agent E2E on an isolated source Gateway (`openai/gpt-6-astra`, 6 concurrent conversations per arm, automations tool denied). Each conversation waits on a fake CI that turns green about 300 s after its first check. The Gateway is stopped about 90 s after every conversation has started its wait and ended its turn, then a new Gateway starts on the same state. | Arm | Waiting, turn ended | Conversations resumed | Re-armed a waiter | Finished the task | | --- | --- | --- | --- | --- | | base `669576890a1f`, crash (SIGKILL) | 6/6 | **0/6** | 0/6 | 0/6 | | fix, crash (SIGKILL) | 6/6 | **6/6** | 6/6 | 6/6 | | fix, graceful (SIGTERM) | 6/6 | 6/6, one notice each | 6/6 | 5/6 (one model-written loop's pattern never matched "all 15 checks passed") | | fix, `durable: true` waiter, graceful | 6/6 | 0 restart notices, 0 polls; 6/6 woken by the adopted exit | n/a | 6/6 | <details><summary>Public trace, crash on macOS, base vs fix (trial 1, paths redacted)</summary> Base: the conversation waits forever. ``` + 28s exec {"yieldMs": 1000, "timeoutSeconds": 3660} "python3 -u - <<'PY' …" + 29s -> Command still running (session quiet-ocean:…). Its exit wakes this conversation … + 32s sessions_yield (Gateway SIGKILLed at +137 s, new Gateway ready; nothing else ever arrives) ``` Fix: one notice, the agent checks and re-arms, then finishes. ``` + 32s exec {"background": true, "timeoutSeconds": 1830} "python3 -u - <<'PY' …" + 32s -> Command still running (session briny-falcon:…). Its exit wakes this conversation … + 36s sessions_yield +166s USER: Exec failed (briny-fa, unknown exit code, 2m9s): The Gateway restarted unexpectedly while this background command (started 2026-11-15T05:37:58.662Z) was running. It may still be running, but its result will not wake this conversation. Check its output and side effects before running it again. +176s exec "ps -p 13156 …; ./ci-status" +176s -> PR #4241: 4 checks pending, 11 passed +190s exec {"background": true, "timeoutSeconds": 1830} "python3 -u - <<'PY' …" +326s USER: Exec completed (neat-bre, code 0, 2m15s): … all 15 checks passed … +328s reply: PR #4241 is ready—all 15 checks passed. Saved and verified the summary in `pr-1/RESULT.md` … ``` Graceful stop with the fix: `Exec failed (vivid-du, unknown exit code, 2m12s): This background command (started …) was stopped when the Gateway shut down, so its result will not arrive. If you still need it, check its output and side effects, then run it again.` </details> Upgrade from latest stable (v2026.11.3, `847611dcaa3b`), real agent, isolated state: 1. A v2026.11.3 Gateway ran 3 waiting conversations, then got a graceful stop. 2. This branch ran `openclaw doctor --fix --yes`, which migrated state schema 22 to 40 (exit 0), then started on the same state. Sessions and transcripts loaded (transcript backfill 3/3 sessions, 122 rows). The startup pass found no `durable_background_processes` table, because the table is created on first use, and logged nothing. 3. A new conversation on the upgraded state started a waiter and ended its turn. The table was created and held one row: `ordinary:cool-ridge:…`. 4. The Gateway got SIGKILL. The next start sent `Exec failed (cool-rid, unknown exit code, 1m5s): The Gateway restarted unexpectedly while this background command (started 2026-11-15T07:22:46.300Z) was running. It may still be running, …`. The agent checked for survivors, re-armed, and finished (`PR #4249 is ready—all 15 checks passed`). 0 rows remained. v2026.11.3 writes neither restart markers nor process rows. Commands running when it stops therefore get no notice after the upgrade. This PR does not change the legacy upgrade bridge. Tests: - `src/agents/bash-process-durable.test.ts` adds "reports each wake-armed ordinary command a restart lost, once, with what happened to it". It fails on base and passes with the fix. It covers: a command that exits leaves no row; a crashed command and a stopped command are each reported once with the right text; rows are gone afterwards. - Restart marker tests now mark sessions through a live run instead of a background process, because background processes no longer create a marker. - `test/gateway-restart-resume.e2e.test.ts` now expects the stopped background case to receive the per-command notice instead of the resume sentence. It passes 5/5 on 4 consecutive runs. Under heavy host load, both base and this branch hit an intermittent mock-provider `JSON Parse error: Unexpected EOF`. - The durable registry test also records a sandbox command (cautious notice) and an incognito command (no row); removing the incognito check turns it red (4 rows instead of 3). - Focused suites pass: restart resume, session event context, lifecycle state, startup resume, process registry, durable notify, post-attach startup, and the restart recovery integrity suite. Known gaps: - Worker and `host=node` commands are not recorded, as before; their processes are owned elsewhere. - A best-effort row write can fail; the command still runs normally and only the restart notice is missing. - If the boot record is unavailable, a macOS crash would be reported as a shutdown. Co-authored-by: Ayaan Zaidi <[email protected]>
… a Gateway crash (openclaw#199499) ## What Problem This Solves Fixes: a conversation that ended its turn waiting for a background command to exit never hears about it again when the Gateway crashes or is killed mid-wait. The exit wake lived only in memory, and the restart resume marker is written only by a graceful shutdown, so after a crash the conversation hung silently. ## User Impact User impact: after any Gateway restart, crashes included, each conversation that was waiting on an ordinary background command gets one notice for that command. The notice names the process handle and start time and says what happened to the command, so the agent can check its output and run it again. Durable commands (`durable: true`) keep their existing adopted exit wake and get no notice. Behavior changes: - The generic "Background exec processes were stopped; rerun them if still needed" sentence in the graceful-restart resume input is gone. The per-command notice replaces it. - A background command whose exit would not wake the conversation, such as a service with `notifyOnExit` off, no longer causes a restart resume turn. ## Why This Change Was Made The process registry already owns the `durable_background_processes` table, its startup pass and the lost-process wake. It now also keeps a best-effort row for each running ordinary command whose exit would wake its conversation, on every platform: - The row reuses the existing table with `kind: "ordinary"` and the generic command summary. It needs no new table, column or schema version. - The row is written when the command is backgrounded and deleted when the command exits. - Incognito sessions are never recorded, because their state stays in memory. - Sandbox commands are recorded with a `sandboxed` flag. The Gateway cannot see inside the container, so their notice says the command may still be running. - Windows runs the startup pass too (durable launches stay Linux and macOS only), so Windows waiters keep a restart notice. On startup, the existing durable pass: - reports every remaining row once through the existing completion path, under the session's current authority, then deletes it; - rechecks eligibility as for durable outcomes (reset, archived and hidden subagent sessions, or `notifyOnExit` off, get nothing); - waits for agent database readiness, which finishes after the listener binds after a crash. The graceful-restart marker no longer tracks background processes (`hadBackgroundExec` is removed), so one owner reports lost commands. The notice text follows a measured fact. In each case below, the Gateway process alone was killed and a real agent's background commands were watched: | Host | Gateway killed (SIGKILL) | Graceful stop | | --- | --- | --- | | macOS, plain and under a launchd LaunchAgent | A silent command keeps running as an orphan; one that writes output dies at its next write | All stopped | | Linux (systemd 255 user service with the installed policy, and plain) | All stopped within 2 s | All stopped | The startup pass reads the Gateway boot record to tell a crash from a shutdown. After a shutdown, or a crash on Linux, the notice says the command was stopped. After a crash on macOS it says the command may still be running, but its result will not wake this conversation. No orphan tracking is added; `durable: true` remains the way to keep a command alive. ## Evidence Real-agent E2E on an isolated source Gateway (`openai/gpt-6-astra`, 6 concurrent conversations per arm, automations tool denied). Each conversation waits on a fake CI that turns green about 300 s after its first check. The Gateway is stopped about 90 s after every conversation has started its wait and ended its turn, then a new Gateway starts on the same state. | Arm | Waiting, turn ended | Conversations resumed | Re-armed a waiter | Finished the task | | --- | --- | --- | --- | --- | | base `0a8e0b7d9cd0`, crash (SIGKILL) | 6/6 | **0/6** | 0/6 | 0/6 | | fix, crash (SIGKILL) | 6/6 | **6/6** | 6/6 | 6/6 | | fix, graceful (SIGTERM) | 6/6 | 6/6, one notice each | 6/6 | 5/6 (one model-written loop's pattern never matched "all 15 checks passed") | | fix, `durable: true` waiter, graceful | 6/6 | 0 restart notices, 0 polls; 6/6 woken by the adopted exit | n/a | 6/6 | <details><summary>Public trace, crash on macOS, base vs fix (trial 1, paths redacted)</summary> Base: the conversation waits forever. ``` + 28s exec {"yieldMs": 1000, "timeoutSeconds": 3660} "python3 -u - <<'PY' …" + 29s -> Command still running (session quiet-ocean:…). Its exit wakes this conversation … + 32s sessions_yield (Gateway SIGKILLed at +137 s, new Gateway ready; nothing else ever arrives) ``` Fix: one notice, the agent checks and re-arms, then finishes. ``` + 32s exec {"background": true, "timeoutSeconds": 1830} "python3 -u - <<'PY' …" + 32s -> Command still running (session briny-falcon:…). Its exit wakes this conversation … + 36s sessions_yield +166s USER: Exec failed (briny-fa, unknown exit code, 2m9s): The Gateway restarted unexpectedly while this background command (started 2026-11-15T05:37:58.662Z) was running. It may still be running, but its result will not wake this conversation. Check its output and side effects before running it again. +176s exec "ps -p 13156 …; ./ci-status" +176s -> PR openclaw#4241: 4 checks pending, 11 passed +190s exec {"background": true, "timeoutSeconds": 1830} "python3 -u - <<'PY' …" +326s USER: Exec completed (neat-bre, code 0, 2m15s): … all 15 checks passed … +328s reply: PR openclaw#4241 is ready—all 15 checks passed. Saved and verified the summary in `pr-1/RESULT.md` … ``` Graceful stop with the fix: `Exec failed (vivid-du, unknown exit code, 2m12s): This background command (started …) was stopped when the Gateway shut down, so its result will not arrive. If you still need it, check its output and side effects, then run it again.` </details> Upgrade from latest stable (v2026.11.3, `847611dcaa3b`), real agent, isolated state: 1. A v2026.11.3 Gateway ran 3 waiting conversations, then got a graceful stop. 2. This branch ran `openclaw doctor --fix --yes`, which migrated state schema 22 to 40 (exit 0), then started on the same state. Sessions and transcripts loaded (transcript backfill 3/3 sessions, 122 rows). The startup pass found no `durable_background_processes` table, because the table is created on first use, and logged nothing. 3. A new conversation on the upgraded state started a waiter and ended its turn. The table was created and held one row: `ordinary:cool-ridge:…`. 4. The Gateway got SIGKILL. The next start sent `Exec failed (cool-rid, unknown exit code, 1m5s): The Gateway restarted unexpectedly while this background command (started 2026-11-15T07:22:46.300Z) was running. It may still be running, …`. The agent checked for survivors, re-armed, and finished (`PR openclaw#4249 is ready—all 15 checks passed`). 0 rows remained. v2026.11.3 writes neither restart markers nor process rows. Commands running when it stops therefore get no notice after the upgrade. This PR does not change the legacy upgrade bridge. Tests: - `src/agents/bash-process-durable.test.ts` adds "reports each wake-armed ordinary command a restart lost, once, with what happened to it". It fails on base and passes with the fix. It covers: a command that exits leaves no row; a crashed command and a stopped command are each reported once with the right text; rows are gone afterwards. - Restart marker tests now mark sessions through a live run instead of a background process, because background processes no longer create a marker. - `test/gateway-restart-resume.e2e.test.ts` now expects the stopped background case to receive the per-command notice instead of the resume sentence. It passes 5/5 on 4 consecutive runs. Under heavy host load, both base and this branch hit an intermittent mock-provider `JSON Parse error: Unexpected EOF`. - The durable registry test also records a sandbox command (cautious notice) and an incognito command (no row); removing the incognito check turns it red (4 rows instead of 3). - Focused suites pass: restart resume, session event context, lifecycle state, startup resume, process registry, durable notify, post-attach startup, and the restart recovery integrity suite. Known gaps: - Worker and `host=node` commands are not recorded, as before; their processes are owned elsewhere. - A best-effort row write can fail; the command still runs normally and only the restart notice is missing. - If the boot record is unavailable, a macOS crash would be reported as a shutdown. Co-authored-by: Ayaan Zaidi <[email protected]>
Summary
Fixes a bug where bold/italic formatting breaks when containing auto-linked URLs in Telegram messages.
Problem
When a URL appears inside bold/italic text (e.g.,
**Check https://example.com**), the HTML tags were being closed in the wrong order:The misnested tags caused Telegram to fail to render the bold formatting when URLs were present inside styled spans.
Root Cause
In
src/markdown/render.ts, therenderMarkdownWithMarkersfunction was closing style tags (bold/italic) before link tags at the same position. Since links are nested inside styles, the inner element (link) should close first.Solution
Reordered the closing logic to close links before styles, maintaining proper HTML nesting order.
Changes
src/markdown/render.ts: Swapped order of closing links and stylessrc/telegram/format.test.ts: Added 3 test cases for the fixTest Plan
properly nests link tags inside bold when URL is at end of bold spanproperly nests link tags inside italicrenders emoji before bold correctlyTesting
Fixes #4174
Greptile Overview
Greptile Summary
This PR updates the shared markdown renderer (
renderMarkdownWithMarkers) to close link tags before closing style markers at the same boundary, fixing mis-nested HTML when Telegram auto-linking occurs inside bold/italic spans. It also adds Telegram formatting regression tests covering bold/italic spans that include linkified URLs.Because
renderMarkdownWithMarkersis used by other formatters (e.g., Slack mrkdwn and markdown table conversion), the behavioral change applies across those outputs too; if the “links are always nested inside styles” invariant isn’t guaranteed by the IR, it may need to be documented or encoded more explicitly.Confidence Score: 4/5
renderMarkdownWithMarkersis a shared renderer, so the changed close ordering could subtly affect other outputs if they rely on different nesting assumptions at identical boundaries.(2/5) Greptile learns from your feedback when you react with thumbs up/down!