From 65ea9016267ce4fc56cdfff2eaeb36da4c7190c5 Mon Sep 17 00:00:00 2001 From: talksik Date: Mon, 30 Mar 2026 13:52:40 -0700 Subject: [PATCH] fix: use actual particle child particle creation time This was causing issues in which the last_child_created_at field for stream particles was slightly after the actual particle's creation time, and so comparing whether it's unseen merely via the playback markers would be inaccurate. --- go/cmd/particleprocessorworker/main.go | 43 ++++++++++++++++---------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/go/cmd/particleprocessorworker/main.go b/go/cmd/particleprocessorworker/main.go index 03cc79e..72528b5 100644 --- a/go/cmd/particleprocessorworker/main.go +++ b/go/cmd/particleprocessorworker/main.go @@ -102,7 +102,7 @@ func main() { // --- Perform side effects --- - updateParentLastChildCreatedAt(ctx, change.Doc.Ref) + updateParentLastChildCreatedAt(ctx, change.Doc) transcribeMediaParticle(ctx, depotSvc, speechSvc, change.Doc) if err := processingRepo.MarkProcessed(ctx, particleID); err != nil { @@ -192,16 +192,18 @@ func toFirestoreTranscript(result *speech.TranscriptResult) particle.FirestoreTr } } -// updateParentLastChildCreatedAt updates the parent particle's field only if it's a particle of type stream -func updateParentLastChildCreatedAt(ctx context.Context, docRef *firestore.DocumentRef) { - parentChildrenCollectionRef := docRef.Parent +// updateParentLastChildCreatedAt updates the parent stream's last_child_created_at +// to the child's actual created_at timestamp, so it stays directly comparable with +// playback markers (which also store child created_at values). +func updateParentLastChildCreatedAt(ctx context.Context, doc *firestore.DocumentSnapshot) { + parentChildrenCollectionRef := doc.Ref.Parent if parentChildrenCollectionRef == nil { return } parentParticleDocRef := parentChildrenCollectionRef.Parent if parentParticleDocRef == nil { - slog.Error("particle has no parent document", "particleID", docRef.ID) + slog.Error("particle has no parent document", "particleID", doc.Ref.ID) return } @@ -225,16 +227,25 @@ func updateParentLastChildCreatedAt(ctx context.Context, docRef *firestore.Docum return } - if particleType == particle.TypeStream { - slog.Info("going to update the last_child_created_at for parent particle") - _, err = parentParticleDocRef.Update(ctx, []firestore.Update{ - { - Path: "last_child_created_at", - Value: firestore.ServerTimestamp, - }, - }) - if err != nil { - slog.Error("unable to update parent particle `last_child_created_at`") - } + if particleType != particle.TypeStream { + return + } + + // Read the child's created_at — this is the same value that playback markers store + childCreatedAt, err := doc.DataAt("created_at") + if err != nil { + slog.Error("failed to read child created_at", "error", err, "particleID", doc.Ref.ID) + return + } + + slog.Info("going to update the last_child_created_at for parent particle") + _, err = parentParticleDocRef.Update(ctx, []firestore.Update{ + { + Path: "last_child_created_at", + Value: childCreatedAt, + }, + }) + if err != nil { + slog.Error("unable to update parent particle `last_child_created_at`") } }