Repository navigation
fix(messages): reject reply fallback data loss - #1116
Conversation
lilyshen0722
left a comment
There was a problem hiding this comment.
Gated at head a2ac3f75. This is right and I'd merge it. Two forward-looking notes, neither blocking.
Verified, including the ordering that makes it correct
:239 try {
:240 PGMessage.create(…) ← the write
:254 let populated = null;
:255 try { populated = await PGMessage.findById(…) }
:257 catch (readErr) { console.warn(…) } ← read failure isolated
:260 message = (populated || created)
:261 } catch (pgErr) { ← PG unavailable
:269 if (replyToMessageId) → 503 ← before the fallback
:275 MongoMessage.create(…) ← non-replies only
The 503 sits in the outer catch, so a successful INSERT whose re-read fails returns 200 with the raw row and still dispatches. A failed INSERT with a reply is rejected; a failed INSERT without one keeps the availability fallback. That is exactly the ordering the ruling needed and it is easy to get wrong by one nesting level.
The test set is complete for the change, which is rare enough to say plainly:
- rejects a reply when PG is unavailable
- does not reject or duplicate a reply when the INSERT succeeded but the post-write read fails — the branch I was going to flag as unpinned, named for both failure modes
- keeps the Mongo fallback for a non-reply — the discriminator proving the guard is scoped rather than blanket
The third is what makes the first two mean something; without it, a guard that rejected everything would pass the first test.
1. The guard's correctness depends on a property a future PR could change
The rejection is right because nothing reconciles Mongo rows into PG — you say so in the body, and TASK-040 says it too. But reconciliation is one of the two options that task names as the eventual fix. If it is ever built, replies could safely take the Mongo path and this 503 becomes a rejection with no remaining reason.
Neither diff would show that: this PR's correctness lives in an assumption about a component that does not exist yet. Worth one line in the comment at :262 tying the guard to it explicitly — "correct only while no Mongo→PG reconciliation exists; if one is built, revisit" — so whoever builds reconciliation finds the dependency instead of inheriting a stale rejection.
2. "try again shortly" is wrong for one real population
error: 'Replies are temporarily unavailable. Please try again shortly.'
True for a transient PG outage. Not true for an FK-orphaned pod — :224 documents that a pod created through the Mongo-only POST /api/pods fails the messages.pod_id foreign key, so PGMessage.create throws for every message in that pod, permanently, until someone backfills it. A user there retries "shortly" forever and every reply fails.
That population is exactly the one this guard now surfaces rather than silently corrupting, which is an improvement — the previous behaviour was a 200 and a lost edge. But the copy tells them to wait for something that will not happen on its own.
Not blocking, and I do not think it needs branching in this PR. It does mean the FK-orphan case now has a visible symptom for the first time, which makes the backfill worth prioritising — a permanently reply-less pod is a much louder bug than a quietly unthreaded one.
Not verified
- I did not run the 89 tests; the mutation control is your measurement.
- Whether the v2 client surfaces
REPLY_REQUIRES_POSTGRESdistinctly or shows a generic failure. A 503 the UI renders as "something went wrong" loses the retry affordance the copy is written for.
|
Closing the gap I left open in my review — I said I had not checked whether the v2 client surfaces
setError(e.response?.data?.error || e.response?.data?.msg || e.message || 'Failed to send message');Your 503 body is ( This makes note 2 sharper rather than softer. Because the copy is shown verbatim, an FK-orphaned pod's users read "please try again shortly" on every reply, forever — Still not blocking, and still not something I would branch on in this PR. It does mean the wording is doing more work than it looks like, and that the pod backfill has a user-visible cost now. One process note against myself: my first read of this landed on the wrong |
|
Qualifying my own verification above, prompted by #1118 opening for exactly this. I said the v2 client surfaces this PR's 503 copy verbatim rather than a generic failure, and that the retry affordance "does reach them. Nothing to change." The string part was right. The placement was not, and I did not check it.
That is what #1118 separates — a distinct The mechanism of my mistake is worth more than the correction. I traced the value from the server body to Nothing here changes my gate on this PR: the 503, the ordering, and the isolated post-write read are all still right, and #1118 is a frontend fix that does not touch them. But if anyone was relying on my "nothing to change" to judge how this lands for a user, it needed the second hop. |
…to PG" (#1129) @sprint-review 56881: the guard #1116 added is correct BECAUSE nothing reconciles Mongo into PG — but that is a current fact, not an invariant, and the dependency lives in neither diff. Reconciliation was the other option considered on TASK-040; #1116 chose fail-loudly instead, so nothing reconciles today and the 503 is the resolution rather than a stopgap. If reconciliation is ever built the guard stops being a data-loss protection and becomes a rejection with no remaining reason — a user told "Replies are temporarily unavailable" about a path that would then preserve their reply fine. Written at both ends, since a one-way pointer is the half that rots: this comment names the dependency, and TASK-040 carries a note pointing back here for anyone who reads the task before proposing reconciliation. Comment only. No behaviour change. Co-authored-by: Claude Opus 5 <[email protected]>
Summary
503 REPLY_REQUIRES_POSTGRESwhen its PostgreSQL INSERT fails, rather than write an unthreaded Mongo fallback row that cannot be reconciled laterVerification
PATH=/opt/homebrew/opt/node@20/bin:$PATH npm test -- --runInBand __tests__/unit/controllers/messageController.test.js __tests__/service/pgMessages.test.js __tests__/unit/services/agentMentionService.test.js(89 tests)PATH=/opt/homebrew/opt/node@20/bin:$PATH npm run tsc:check200instead of503. Restored the guard and reran green.No Mongo schema widening: the fallback store has no reply-edge field, and these records are not reconciled into PostgreSQL.