Fix Telegram streamed duplicate reply bubbles#40883
Conversation
🔒 Aisle Security AnalysisWe found 3 potential security issue(s) in this PR:
1. 🟡 Potential DoS via unbounded answer segment accumulation and full recomposition on each partial stream update
DescriptionThe new answer-segment buffering system can consume excessive CPU and memory during streaming, because it repeatedly recomputes the full concatenated answer text across all accumulated segments on every partial update. Key behaviors:
Vulnerable code path:
Vulnerable code: const composeAnswerSegmentsText = () =>
answerSegments.reduce((acc, segment) => appendAnswerSegment(acc, segment.text), "");
const updateAnswerSegmentFromPartial = (text: string) => {
...
segment.text = text;
updateDraftFromPartial(answerLane, composeAnswerSegmentsText());
};Notes on bounds:
This means a single crafted request that induces extensive tool-calling / many assistant message boundaries plus high-frequency partial updates can degrade the bot process (DoS). RecommendationMitigate uncontrolled CPU/memory usage by avoiding full recomposition and by bounding segment growth. Suggested changes (pick a combination):
Example: let answerPrefixText = ""; // finalized segments only
function finalizeSegmentText(segText: string) {
answerPrefixText = appendAnswerSegment(answerPrefixText, segText);
}
function currentComposedText(currentSegText: string) {
return appendAnswerSegment(answerPrefixText, currentSegText);
}
These changes reduce the risk of CPU spikes from repeated reductions and large transient string allocations during streaming. 2. 🔵 Stale Telegram preview retained when final edit fails (treated as delivered), preventing cleanup
DescriptionIn
Security impact (privacy/integrity):
Vulnerable flow:
Vulnerable code (final edit failure treated as delivered): if (args.treatEditFailureAsDelivered) {
if (args.context === "final") {
args.lane.lastPartialText = args.text;
}
params.log(`... edit failed; keeping existing preview ...`);
params.markDelivered();
return true;
}Cleanup then skips deleting the preview message: const shouldClear = !finalizedPreviewByLane[laneState.laneName];
...
if (cleanupState.shouldClear) {
await stream.clear();
}RecommendationDo not mark a final preview as delivered/finalized when the edit operation fails. Options (choose one):
Example adjustment (option 1): // in tryEditPreviewMessage
if (args.treatEditFailureAsDelivered) {
if (isMessageNotModifiedError(err)) return true;
if (isMissingPreviewMessageError(err)) return false;
// for final: do NOT treat as delivered
return false;
}And ensure callers only set 3. 🔵 Stale Telegram inline buttons can be preserved across assistant-message boundaries
DescriptionIn
While callback queries are still subject to sender authorization ( Vulnerable logic: const bufferedPayload =
bufferedAnswerFinal &&
hasBufferedAnswerPayloadMetadata(bufferedAnswerFinal.payload) &&
!hasBufferedAnswerPayloadMetadata(payload)
? bufferedAnswerFinal.payload
: payload;
bufferedAnswerFinal = { payload: bufferedPayload, text: composeAnswerSegmentsText() };RecommendationGate metadata preservation so inline buttons/media cannot cross assistant-message boundaries unless explicitly re-specified. Options:
onAssistantMessageStart: () => enqueueDraftLaneEvent(async () => {
// ...existing...
bufferedAnswerFinal = undefined; // or clear just telegram.buttons/media fields
answerBoundaryPending = true;
})
Additionally, consider binding Analyzed PR: #40883 at commit Last updated on: 2026-03-09T11:57:23Z |
Greptile SummaryThis PR fixes duplicate reply bubbles in Telegram streaming by replacing the old
Additionally, the Key changes:
Confidence Score: 4/5
Last reviewed commit: 70eea02 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 70eea0235f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eb81960037
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Summary
Testing
pnpm test -- src/telegram/lane-delivery.test.tspnpm test -- src/telegram/bot-message-dispatch.test.tsTGFILEPROBE-0309103331,TGREPRO5-0309103355