Skip to content

Fix #147: truncate task prompt by byte length for Telegram's 4096-byte limit#172

Merged
grinev merged 2 commits into
grinev:mainfrom
hbx12:hbx12-cautious-potato
Jul 2, 2026
Merged

Fix #147: truncate task prompt by byte length for Telegram's 4096-byte limit#172
grinev merged 2 commits into
grinev:mainfrom
hbx12:hbx12-cautious-potato

Conversation

@hbx12

@hbx12 hbx12 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Changes

🐛 #147 — Task detail view silently fails when prompt exceeds byte limit

  • Problem: Tapping a scheduled task with a long prompt does nothing — editMessageText exceeds Telegram's 4096-byte limit. The previous fix used character count, which still crashed for non-ASCII text (Arabic/Cyrillic: ~7600 bytes for 3800 chars)
  • Fix: Reworked truncatePromptForDetails() to use Buffer.byteLength() with binary search, ensuring the prompt stays within byte budget regardless of encoding

Files changed

  • scheduled-task-callback-handler.ts — byte-length truncation with 3800-byte budget
  • Added 3 tests: ASCII long prompt (truncated), short prompt (intact), Arabic multibyte prompt (truncated)

✅ TypeScript — zero errors
✅ 1054/1054 tests passing (3 new tests for byte truncation)
⚠️ Note: #170 (svelte file support) moved to separate PR #173

- Issue grinev#147: truncate task prompt in detail view to avoid Telegram's
  4096-char limit on editMessageText (add truncatePromptForDetails)
- Issue grinev#170: add filename-based text type detection fallback to handle
  .svelte and other code files that lack a proper MIME type
- Update isTextMimeType to accept an optional filename parameter and
  check against TEXT_FILE_EXTENSIONS when MIME type is unrecognized

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@grinev

grinev commented Jul 1, 2026

Copy link
Copy Markdown
Owner

@hbx12 thanks for the fixes! A couple of comments before we merge.

#170 (.svelte / unknown MIME types) — code looks good, the fallback path works as intended. Could we move it into a separate PR? The two fixes are independent and splitting keeps things easier to review and revert. Only thing missing is tests — the new extension fallback in isTextMimeType isn't covered, would be good to add a couple of cases (e.g. application/octet-stream + .svelte filename → true, and an unknown extension → false).

#147 (long prompt truncation) — this one needs a small change. It truncates by character count, but Telegram's limit is 4096 bytes in UTF-8. For non-ASCII text that's a different number: a 3800-char Cyrillic prompt is ~7600 bytes and still crashes (real threshold is ~1800 chars; CJK is even lower). Suggest truncating by byte length (e.g. Buffer.byteLength) with a byte budget that leaves room for the rest of the template, instead of a fixed char limit.

Once #147 is reworked and tests are added for both, I'm happy to merge.

@hbx12

hbx12 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@grinev Thanks for the detailed review!

Good point about splitting #170 into a separate PR. I’ll move the MIME type fallback into its own PR and add the missing test cases for both the .svelte fallback and the unknown extension case.

For #147, you’re absolutely right. I overlooked Telegram’s UTF-8 byte limit and only considered character count. I’ll rework it to truncate based on byte length while leaving enough room for the rest of the template, and I’ll add tests covering non-ASCII input as well.

I’ll update both PRs and let you know once they’re ready. Thanks again!

…am's 4096-byte limit

Rework truncation to use Buffer.byteLength() instead of character count.
Telegram's editMessageText limit is 4096 bytes (not characters), so
non-ASCII prompts (Arabic, Cyrillic, CJK) still crashed with the
previous character-based fix.

Binary search approach finds the correct character offset efficiently.

Also removes the svelte MIME type fallback changes from this PR —
they now live in a separate PR (grinev#173).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@hbx12

hbx12 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@grinev PR is updated! Here's what changed:

#147 rework (this PR)

  • Switched from character-based to byte-based truncation using Buffer.byteLength() — non-ASCII prompts (Arabic, Cyrillic, CJK) now work correctly
  • Budget: 3800 bytes for the prompt, leaving room for ~296 bytes of template overhead → total ≤ 4096
  • Binary search for efficient exact truncation point
  • Added 3 tests: ASCII long prompt, short prompt intact, Arabic multibyte prompt

#170 moved to separate PR

The svelte/file extension fallback was moved to #173 with its own tests as requested.

@hbx12 hbx12 changed the title Fix #147: tasklist long prompt crash & Fix #170: svelte file support Fix #147: truncate task prompt by byte length for Telegram's 4096-byte limit Jul 1, 2026
@grinev

grinev commented Jul 2, 2026

Copy link
Copy Markdown
Owner

@hbx12 thanks for contribution!

@grinev grinev merged commit 5e8da31 into grinev:main Jul 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants