Three intertwined fixes for the torn-write that corrupted the prod
data/chat-cache.json mid-session:
- ChatCache.save / persistCooldown now write via temp file + rename.
rename(2) is atomic on the same filesystem, so a crashed or
concurrent writer can never leave NULs in the middle of the file.
A small serialising write queue stops two concurrent saves from
racing to the same temp name.
- ChatCache throws if constructed without a filePath under NODE_ENV=test.
The old default (./data/chat-cache.json) silently routed test
writes into the production cache — exactly how the corruption
happened. Future test that forgets to pass a tmp path fails loudly
instead of stomping data.
- Cooldown / delta-media / username-persist tests now pass an
explicit tmp ChatCache so the new guard is satisfied.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
For chats where we sent an opening message but the recipient never
accepted, /sync drops the room with no heroes and the recipient is
in state as m.room.member with membership:'invite' — not 'join'.
chat.reddit.com's web UI labels these rooms with the invitee anyway,
because it reads the full room state.
- roomToActiveChat now accepts 'invite' membership in addition to
'join' when scanning member-join state events.
- enrichUsernames pass 2 hits /_matrix/client/v3/rooms/{id}/state
first (cheap, returns all member events including invites) and
only falls back to /messages history if /state yields nothing.
- Added MatrixClient.getRoomFullState to back that lookup.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
api.reddit.com/api/user_data_by_account_ids returns 404 without an
OAuth token — which is why every chat in the dashboard sat on
t2_xxx or unknown. The same path on www.reddit.com (with a .json
suffix) is served unauthenticated and returns the expected user
map. Switch the resolver to that host so enrichUsernames actually
succeeds.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Reddit's homeserver does not expose /_matrix/client/v3/rooms/{id}/joined_members
(it returns 'page not found'), so the previous fallback was always a 404.
Two replacements:
- roomToActiveChat: include timeline event senders as candidate MXIDs
alongside heroes / member-join state. /sync omits heroes for idle
rooms but always carries full sender MXIDs in any timeline events
it does return, so we recover a t2 ID whenever the delta sees a
message.
- enrichUsernames: split into three passes — apply known mappings
first (so re-created delta chats get upgraded without a restart),
then page /messages 'b' once per still-unknown room to mine a t2
from the recipient's history, then resolve any remaining raw t2s
via the bulk user_data endpoint. Failed history lookups are cached
in unresolvableRooms so we don't retry every sync.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Reddit's homeserver returns 404 from /_matrix/client/v3/rooms/{id}/joined_members
for rooms we've left, been kicked from, or never fully joined — which is
roughly every chat that wasn't accepted by the recipient. The previous
enrichUsernames pass retried these on every delta sync, flooding the
dashboard log with debug-level 404s. Track failures in an in-memory
unresolvable set so each room is tried at most once per process.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>