Two sync fixes and the bugs they brought with them
Comment Command is a .NET and React app for handling YouTube and Instagram comments as a team. Background jobs sync the comments into it. On 19 October I fixed two bugs in those jobs with Claude Code. A week later, the read-only scan I run over my repos every week found that both fixes had a bug of their own.
A first sync that came up empty
I ran a real signup on production. The channel I connected had one video, about a month old, with four comments posted minutes earlier. The first sync looked back 30 days, so it found nothing and the workspace stayed empty.
The fix was a fallback: when a sync finds no uploads and the channel has no stored videos, read the newest uploads with no date filter. But a channel with no uploads at all matches that condition on every sync, so it would spend twice the YouTube quota on listing videos every time. The fallback now runs only on the first sync:
if (videosResult.Items.Count == 0 && since.HasValue && channel.LastSyncAt == null &&
!await _db.YouTubeVideos.AnyAsync(v => v.YouTubeChannelId == channel.Id))
Posts that looked up to date
The Instagram job read the newest 12 posts and never asked for more, so comments on older posts were never picked up. It now reads up to 50. The newest 12 are read on every sync, and an older post only when it is new to us or its comment count has changed.
That made the stored count a record of what had been read, and the record could be wrong. If a later page of comments failed, the post still saved the full count. On the next sync the count looked unchanged, so the missing comments would never be read. A partial read now keeps the old count, so the next sync tries that post again:
if (commentsResult.IsPartial || (commentItems.Count == 0 && (item.CommentsCount ?? 0) > 0))
{
media.CommentsCount = previousCommentCounts[item.ExternalMediaId];
}
What I took from this
Saved sync state is a claim about what has been read, so it should only be written when the read really finished. And a fallback needs a condition that names the one case it is for, not just what that case looks like.
The follow-up fixes came with tests that fail on the old code. One gap is still open: I have not seen the Instagram paging run live, because I don't have an account with more than 12 posts. The repository is private, so the snippets above are copied from it.