Commit 5bdf5a5
authored
fix(telegram): preserve code block when editing approval messages (#21)
* fix(telegram): preserve code block when editing approval messages
Edits on approval prompts (resolved by button, timed out, or
cancelled) were appending a status suffix to a stored rendered HTML
string. Under some conditions the appended suffix caused Telegram to
drop the <pre><code class="language-json"> formatting on the args
block, so approved/denied prompts lost the pretty-printed JSON code
block.
Switch msgMap to store the full ApprovalRequest instead of a
pre-rendered text string. Every edit path (CancelApproval, inflight
timeout, callback-resolved, callback-timed-out) now re-renders the
body by calling FormatApprovalMessage(am.req) fresh and appending the
status suffix to the result. This guarantees the <pre><code> block is
always intact in the final body we send to editMessageText.
* fix(telegram): pre-load msgMap to avoid edit race with cancelOnChannels
broker.Resolve synchronously calls cancelOnChannels -> tc.CancelApproval
on the same Telegram channel that just resolved the request. That cleanup
path did LoadAndDelete on msgMap and issued its own "(resolved via another
channel)" edit. Control then returned to handleCallback, which could no
longer find the entry in msgMap and fell back to editing with
cq.Message.Text - Telegram's plain-text extraction, where <pre>/<code>
tags are already stripped. The second edit clobbered the first, leaving
the user with plain-text JSON after tapping Allow/Deny.
Fix: handleCallback now LoadAndDelete's the msgMap entry into a local
'am' variable before calling broker.Resolve. This takes ownership of the
entry so CancelApproval becomes a no-op, and handleCallback's edit
becomes the single last-write-wins render with the <pre><code> block
intact and the "Allowed (once)/(request timed out)" label appended.
Add regression test TestHandleCallbackAllowOncePreservesCodeBlockOnEdit
that simulates an MCP approval with ToolArgs, taps Allow, and asserts
the final editMessageText payload still contains
<pre><code class="language-json"> and the status label.1 parent aff2467 commit 5bdf5a5
2 files changed
Lines changed: 139 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
105 | | - | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
106 | 110 | | |
107 | 111 | | |
108 | | - | |
| 112 | + | |
109 | 113 | | |
110 | 114 | | |
111 | 115 | | |
| |||
155 | 159 | | |
156 | 160 | | |
157 | 161 | | |
158 | | - | |
159 | | - | |
| 162 | + | |
160 | 163 | | |
161 | 164 | | |
162 | 165 | | |
163 | 166 | | |
164 | 167 | | |
165 | 168 | | |
166 | | - | |
| 169 | + | |
167 | 170 | | |
168 | 171 | | |
169 | 172 | | |
| |||
186 | 189 | | |
187 | 190 | | |
188 | 191 | | |
189 | | - | |
| 192 | + | |
| 193 | + | |
190 | 194 | | |
191 | 195 | | |
192 | 196 | | |
| |||
300 | 304 | | |
301 | 305 | | |
302 | 306 | | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
303 | 323 | | |
304 | 324 | | |
305 | 325 | | |
| |||
309 | 329 | | |
310 | 330 | | |
311 | 331 | | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
318 | 345 | | |
319 | | - | |
320 | | - | |
| 346 | + | |
321 | 347 | | |
322 | 348 | | |
323 | | - | |
324 | 349 | | |
325 | 350 | | |
326 | 351 | | |
327 | 352 | | |
328 | 353 | | |
329 | | - | |
330 | | - | |
331 | | - | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
332 | 359 | | |
333 | | - | |
334 | | - | |
| 360 | + | |
335 | 361 | | |
336 | 362 | | |
337 | | - | |
338 | 363 | | |
339 | 364 | | |
340 | 365 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
459 | 459 | | |
460 | 460 | | |
461 | 461 | | |
462 | | - | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
463 | 466 | | |
464 | 467 | | |
465 | 468 | | |
| |||
508 | 511 | | |
509 | 512 | | |
510 | 513 | | |
511 | | - | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
512 | 518 | | |
513 | 519 | | |
514 | 520 | | |
| |||
808 | 814 | | |
809 | 815 | | |
810 | 816 | | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
811 | 901 | | |
812 | 902 | | |
813 | 903 | | |
| |||
0 commit comments