Repository navigation
fix(telegram): split the webhook ack on which side of the pod write failed - #1286
Conversation
Telegram redelivers any update the webhook does not 2xx, and relayTelegramMessageToPod is not idempotent: it writes the pod row (PGMessage.create) before it wakes agents (deliverMessageToAgents), and nothing dedupes on message_id. A throw in delivery therefore reached the route's outer catch, returned 500, and the retry duplicated both the pod message and every agent wake it triggered. Wrap the relay call and sendStatus(200) regardless. A dropped relay is recoverable; a doubled one is not. Two route tests: the live-relay branch bypasses the buffer provider, and a throwing relay still acks 200. Reverting the route change turns the second red and leaves the other four green. Co-Authored-By: Claude Opus 5 <[email protected]>
The previous shape acked 200 unconditionally, which is right for only half
the cases. relayTelegramMessageToPod writes the pod row (PGMessage.create)
before it wakes agents (deliverMessageToAgents), so a redelivery means two
different things:
before the write - nothing persisted; Telegram's retry is the only thing
that saves the update. A blanket 200 drops it silently: no pod message,
no wake, one console line, and the sender has to retype.
after the write - the row exists and nothing dedupes on message_id, so the
retry duplicates both the message and every wake it triggers.
So guard only the post-write step, inside the bridge, and let anything
earlier propagate to the route's outer handler and 500. The route now
carries an explicit comment that its bare await is deliberate.
Three service tests pin the split (propagates before, swallows after,
succeeds normally) and the route test inverts to assert the non-ack.
Mutation controls, one red each and unique: removing the post-write guard
fails only 'swallows a failure AFTER the write'; re-adding a blanket catch
in the route fails only 'does not ack when the relay throws'.
Correction credit: sprint-review, who caught that the original PR body had
it exactly backwards - the dropped relay is the unrecoverable one.
Co-Authored-By: Claude Opus 5 <[email protected]>
|
Approving at The reasoning in the comment is the right one. A duplicate wake is unrecoverable and a dropped relay is not, so unconditional ack is the correct trade even before idempotency exists. One residual the comment overstates"A dropped relay is recoverable" holds only when the throw lands before the pod write. In and Not an argument against the ack — retrying would still duplicate. It is an argument that the catch is swallowing two different failures under one log line. Cheapest split, no new machinery: } catch (relayErr) {
console.error('[tg-bridge] inbound relay failed:', (relayErr as Error).message);
}→ have Second, smallerThe Telegram sender gets no signal either way. The bridge already holds Not verifiedI did not exercise the retry itself — no test here proves Telegram's redelivery behaviour, and I am taking "non-2xx ⇒ redeliver" from the Bot API contract, not from an observed retry against this endpoint. The mutation proves the ack, not the consequence of failing to ack. Also unverified: that |
Why one ack policy can't be right
relayTelegramMessageToPodwrites the pod row before it wakes agents:telegramBridgeService.ts—PGMessage.create(...)deliverMessageToAgents(...)Telegram redelivers any update the webhook does not 2xx, and nothing dedupes on
telegramMessage.message_id— the field is declared in the payload type and never read. So the redelivery means two opposite things:A blanket
try/catch+sendStatus(200)on the route fixes the second and breaks the first, and the broken one is the quieter failure: no pod message, no wake, oneconsole.error, and the sender has to retype with no signal that anything went wrong.The shape
Guard only the post-write step, and put the guard where that step is:
deliverMessageToAgentsis wrapped inside the bridge — the row already exists, so swallowing keeps the record and costs only the wake.awaitplus a comment stating the bareness is deliberate, because re-adding a blanket catch there is precisely the next edit someone makes.The split now falls out of where the code sits rather than a runtime decision about which side failed.
Proof
__tests__/unit/services/telegramBridgeService.inboundRetry.test.js— three cases: relays normally; propagates whenPGMessage.createrejects (and never reaches delivery); resolves whendeliverMessageToAgentsrejects (with the create still counted once). Route test inverted to assert the non-ack.Two mutations, each producing exactly one red, and a different one:
Not done
Dedupe on
message_idis the better fix — it gives retry safety without depending on where the throw landed — and it wants somewhere to keep seen ids. Deliberately not added hours before a live demo. This change is free and removes the duplication path either way.Correction credit: sprint-review.