lab5: завершение второго ревью

PNG: validate color-type/bit-depth pairs, reject duplicate IHDR/PLTE, non-consecutive IDAT, PLTE after IDAT, and trailing bytes after the zlib stream; keep accepting valid palette, split-IDAT, 16-bit, and Adam7 uploads. Replay: a newer failed unpublished version returns the attachment to queued, while a published ready version stays serving.
This commit is contained in:
Vladislav Kunetskiy
2026-10-31 01:22:22 +03:00
parent 8c0a455aa5
commit 624f76b7d0
15 changed files with 406 additions and 61 deletions
@@ -1474,15 +1474,25 @@ func (r *PipelineRepository) ReplayJob(
return fmt.Errorf("replay job: %w", mapError(err))
}
if job.PipelineVersion == attachment.PipelineVersion {
// Mirror failJobTx: a replay of a newer unpublished version returns
// the attachment to queued so the API does not keep showing a stale
// terminal state while the replayed job waits. A published ready
// version is never disturbed by a newer version's replay.
switch {
case job.PipelineVersion == attachment.PipelineVersion:
_, err = tx.Exec(ctx, `
UPDATE attachments
SET status = 'queued', error_code = NULL, failed_at = NULL, updated_at = now()
WHERE id = $1 AND pipeline_version = $2`,
attachment.ID, job.PipelineVersion)
if err != nil {
return fmt.Errorf("replay attachment: %w", mapError(err))
}
case job.PipelineVersion > attachment.PipelineVersion && attachment.Status != domain.AttachmentReady:
_, err = tx.Exec(ctx, `
UPDATE attachments
SET status = 'queued', error_code = NULL, failed_at = NULL, updated_at = now()
WHERE id = $1 AND status <> 'ready'`, attachment.ID)
}
if err != nil {
return fmt.Errorf("replay attachment: %w", mapError(err))
}
if err := insertEventTx(ctx, tx, job.ID, domain.EventReplayed, nextAttempt, ""); err != nil {
@@ -301,6 +301,118 @@ func (s *PipelineSuite) TestReprocessFailureKeepsReadyServingVersion() {
s.Nil(current.ErrorCode)
}
// Replaying a failed newer version must clear the stale terminal projection
// for an attachment that never published any version.
func (s *PipelineSuite) TestReplayNewerFailedVersionRequeuesUnpublishedAttachment() {
alice := s.createUser("alice")
chat := s.createChat(alice)
repo := s.store.Pipeline()
draft, job := s.attachmentDraft(chat, alice, 1)
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
reprocessID, err := uuid.NewV7()
s.Require().NoError(err)
reprocess, err := repo.Reprocess(s.T().Context(), draft.ID, 2, 5, reprocessID)
s.Require().NoError(err)
claim, err := repo.ClaimJob(s.T().Context(), domain.NewJobMessage(reprocess), "lease-2", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
failed, err := repo.FailJob(s.T().Context(), reprocess.ID, 1, "lease-2", "invalid_image")
s.Require().NoError(err)
s.True(failed)
current, err := repo.GetForOwner(s.T().Context(), alice.ID, draft.ID)
s.Require().NoError(err)
s.Equal(domain.AttachmentFailed, current.Status)
replayed, err := repo.ReplayJob(s.T().Context(), reprocess.ID, 2)
s.Require().NoError(err)
s.Equal(2, replayed.Attempt)
current, err = repo.GetForOwner(s.T().Context(), alice.ID, draft.ID)
s.Require().NoError(err)
s.Equal(domain.AttachmentQueued, current.Status)
s.Nil(current.ErrorCode)
claim, err = repo.ClaimJob(s.T().Context(), domain.NewJobMessage(replayed), "lease-3", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
variants := []domain.Variant{
{
Name: domain.VariantThumbnail, ContentType: "image/jpeg", SizeBytes: 11,
Width: 256, Height: 160, SHA256: strings.Repeat("c", 64),
ObjectKey: domain.VariantObjectKey(chat.ID, draft.ID, 2, domain.VariantThumbnail),
},
}
ok, err := repo.CompleteJob(s.T().Context(), replayed.ID, replayed.Attempt, "lease-3", variants)
s.Require().NoError(err)
s.True(ok)
current, err = repo.GetForOwner(s.T().Context(), alice.ID, draft.ID)
s.Require().NoError(err)
s.Equal(domain.AttachmentReady, current.Status)
s.Equal(2, current.PipelineVersion)
}
// Replaying a failed newer version must not disturb a published ready
// serving version; only the new job returns to queued.
func (s *PipelineSuite) TestReplayNewerFailedVersionKeepsReadyServingVersion() {
alice := s.createUser("alice")
chat := s.createChat(alice)
repo := s.store.Pipeline()
draft, job := s.attachmentDraft(chat, alice, 1)
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
s.Require().NoError(err)
claim, err := repo.ClaimJob(s.T().Context(), domain.NewJobMessage(job), "lease-1", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
variants := []domain.Variant{
{
Name: domain.VariantThumbnail, ContentType: "image/jpeg", SizeBytes: 11,
Width: 256, Height: 160, SHA256: strings.Repeat("b", 64),
ObjectKey: domain.VariantObjectKey(chat.ID, draft.ID, 1, domain.VariantThumbnail),
},
}
ok, err := repo.CompleteJob(s.T().Context(), job.ID, 1, "lease-1", variants)
s.Require().NoError(err)
s.True(ok)
reprocessID, err := uuid.NewV7()
s.Require().NoError(err)
reprocess, err := repo.Reprocess(s.T().Context(), draft.ID, 2, 5, reprocessID)
s.Require().NoError(err)
claim, err = repo.ClaimJob(s.T().Context(), domain.NewJobMessage(reprocess), "lease-2", time.Minute)
s.Require().NoError(err)
s.Equal(domain.ClaimAcquired, claim.Outcome)
failed, err := repo.FailJob(s.T().Context(), reprocess.ID, 1, "lease-2", "invalid_image")
s.Require().NoError(err)
s.True(failed)
replayed, err := repo.ReplayJob(s.T().Context(), reprocess.ID, 1)
s.Require().NoError(err)
s.Equal(2, replayed.Attempt)
current, err := repo.GetForOwner(s.T().Context(), alice.ID, draft.ID)
s.Require().NoError(err)
s.Equal(domain.AttachmentReady, current.Status)
s.Equal(1, current.PipelineVersion)
s.Nil(current.ErrorCode)
}
func (s *PipelineSuite) TestReconcileQueuedJobsRepublishesLostIntent() {
alice := s.createUser("alice")
chat := s.createChat(alice)