Skip to content

fix(capture): retry capture v1 requests that get 404 or 429 - #1178

Open
andehen wants to merge 2 commits into
mainfrom
fix/capture-v1-retry-on-404-429
Open

andehen wants to merge 2 commits into
mainfrom
fix/capture-v1-retry-on-404-429

Conversation

@andehen

@andehen andehen commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

With capture_mode="v1", the SDK fails a batch at once when capture returns 404 or 429. The v1 release line has retried both since #1132. The capture v1 suites in the SDK test harness check this too: retry_behavior.retries_on_404 and retries_on_429 were added in PostHog/posthog-sdk-test-harness#92 and released in 1.24.0. Main's v1 mode fails both tests, so this repo's compliance workflow cannot move its harness pin past 1.23.0.

This ports the status change from #1132 to posthog/capture_v1.py on main:

  • _RETRYABLE_STATUSES adds 404 and 429, and they leave _TERMINAL_STATUSES. They use the same backoff, Retry-After handling and retry budget as 408 and the 5xx statuses. After the last attempt, CaptureV1Error is raised with their status.
  • The retry and exhaustion tests now run for 404, 429 and 503. The terminal-status test uses 402 in place of 429.
  • AGENTS.md no longer lists "terminal v1 429" as a rule to keep.
  • A patch changeset describes the change.

v0 capture does not change. It already retries 429, and it does not retry 404.

Spec: PostHog/sdk-specs#146 (open) specifies capture v1 delivery for every v1 SDK. It says a v1 SDK SHALL retry 404, 408, 429, 500, 502, 503 and 504, and it records the 2026-10-27 decision to retry 404 and 429. The retry-queue spec on sdk-specs main already lists 429 as retryable.

This PR matches #1132 and PostHog/sdk-specs#146 on which statuses are retried, but not on timing. Main keeps its 1, 2, 4 second backoff (capped at 30 seconds, with Retry-After as a floor). #1132 and the spec wait 100, 200, 400 ms with ±25% jitter. This PR does not change timing.

💚 How did you test it?

  • pytest posthog/test/test_capture_v1.py: 102 passed. The four new 404 and 429 cases fail without the fix. The exhaustion cases also check attempts == 2 and exactly one 1 second sleep, with no sleep after the final attempt.
  • pytest --timeout=30: everything passes except 3 fork tests in test_metrics_autocapture.py. They pass when that file runs alone, both with and without this change.
  • ruff format --check ., ruff check ., mypy ... | mypy-baseline filter and python -W error -c "import posthog" pass.
  • Harness 1.27.0 without Docker, with this branch installed in the compliance adapter:
Capture mode Result
v0 56/56 passed
v1 121/121 passed, including retries_on_404 and retries_on_429

Without this change, v1 fails exactly those two tests.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Authored with Claude Code, directed by @andehen. The change copies the status sets and tests from #1132 on the v1 branch, so the two lines agree. This PR does not move the harness pin. That can follow once this merges.

🤖 Generated with Claude Code

With capture_mode="v1", 404 and 429 now retry like 408 and the 5xx
statuses, with the same backoff, Retry-After handling and retry budget.
They raise CaptureV1Error with their status after the last attempt.
This matches the v1 branch (#1132) and the harness capture v1
retry_behavior suite.
@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-11-06T09:07:44.502363+00:00
Duration: 260301ms

✅ All Tests Passed!

121/121 tests passed


Capture_V1 Tests

✅ 95/95 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 517ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 513ms
Required Headers.Has Authorization Bearer Header ✅ 511ms
Required Headers.Has Content Type Json ✅ 512ms
Required Headers.Has Posthog Sdk Info Format ✅ 512ms
Required Headers.Has Posthog Attempt Header ✅ 512ms
Required Headers.Has Posthog Request Id ✅ 513ms
Required Headers.Has Posthog Request Timestamp ✅ 512ms
Required Headers.Has User Agent ✅ 513ms
Body Format.Body Has Created At And Batch ✅ 512ms
Body Format.No Api Key In Body ✅ 513ms
Body Format.No Sent At In Body ✅ 511ms
Event Format.Event Has Required Root Fields ✅ 513ms
Event Format.Event Uuid Is Valid ✅ 512ms
Event Format.Event Timestamp Is Rfc3339 ✅ 512ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 529ms
Event Format.Distinct Id Is String ✅ 512ms
Event Format.Distinct Id At Root Not Properties ✅ 511ms
Event Format.Custom Properties Preserved ✅ 513ms
Event Format.Set Properties Preserved ✅ 511ms
Event Format.Set Once Properties Preserved ✅ 513ms
Event Format.Groups Properties Preserved ✅ 513ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 512ms
Event Format.Event Has Required Root Fields Batch ✅ 516ms
Event Format.Event Uuid Is Valid Batch ✅ 517ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 516ms
Event Format.Distinct Id Is String Batch ✅ 517ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 516ms
Event Format.Custom Properties Preserved Batch ✅ 517ms
Event Format.Set Properties Preserved Batch ✅ 517ms
Event Format.Set Once Properties Preserved Batch ✅ 515ms
Event Format.Groups Properties Preserved Batch ✅ 515ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 516ms
Batch Behavior.Multiple Events In Single Batch ✅ 522ms
Batch Behavior.Batch Envelope Smoke ✅ 519ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 509ms
Batch Behavior.Flush At Triggers Batch ✅ 1012ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 513ms
Deduplication.Generates Unique Uuids ✅ 522ms
Deduplication.Different Events Same Content Different Uuids ✅ 515ms
Deduplication.Preserves Uuid On Retry ✅ 6521ms
Deduplication.Preserves Timestamp On Retry ✅ 6522ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 6521ms
Deduplication.No Duplicate Events In Batch ✅ 520ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 512ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 13530ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 6521ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 3022ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 6521ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 513ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 515ms
Response Format Validation.Success No Retry After When All Ok ✅ 514ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 1517ms
Response Format Validation.Success No Retry After When Drop Only ✅ 515ms
Response Format Validation.Response Echoes Request Id ✅ 511ms
Retry Behavior.Retries On 408 ✅ 6521ms
Retry Behavior.Retries On 500 ✅ 6522ms
Retry Behavior.Retries On 503 ✅ 8525ms
Retry Behavior.Retries On 504 ✅ 6522ms
Retry Behavior.Retryable Errors Have Retry After ✅ 3519ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 11524ms
Retry Behavior.Does Not Retry On 400 ✅ 2514ms
Retry Behavior.Does Not Retry On 401 ✅ 2515ms
Retry Behavior.Does Not Retry On 402 ✅ 2514ms
Retry Behavior.Does Not Retry On 413 ✅ 2515ms
Retry Behavior.Does Not Retry On 415 ✅ 2515ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2514ms
Retry Behavior.Implements Backoff ✅ 22538ms
Retry Behavior.Max Retries Respected ✅ 22536ms
Partial Batch Handling.Handles 200 Full Success ✅ 2514ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3517ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3518ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3518ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 6518ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 6521ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 6521ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 6524ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 6524ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 6524ms
Partial Batch Handling.Respects Retry After On Partial ✅ 8524ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3518ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3523ms
Compression.Sends Gzip Content Encoding ✅ 513ms
Compression.No Content Encoding When Disabled ✅ 514ms
Compression.Compressed Body Is Decompressible ✅ 520ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2515ms
Event Options.Cookieless Mode Override ✅ 513ms
Event Options.Disable Skew Correction Override ✅ 512ms
Event Options.Process Person Profile Override ✅ 513ms
Event Options.Product Tour Id Override ✅ 512ms
Event Options.Unset Options Omitted ✅ 512ms
Event Options.Options Override In Batch ✅ 518ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 513ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 513ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 513ms

Capture_Ai Tests

✅ 5/5 tests passed

View Details
Test Status Duration
Routing.Capture Ai Posts To Ai Endpoint ✅ 513ms
Routing.Capture Does Not Reroute Ai Named Events ✅ 513ms
Identity.Capture Ai Event Has Uuid ✅ 512ms
Identity.Capture Ai Keeps Supplied Uuid ✅ 513ms
Timestamp Format.Non Utc Event Timestamp Is Converted To Utc ✅ 513ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 15ms
Request Payload.Flags Request Uses V4 Query Param ✅ 12ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 12ms
Request Payload.Flags Request Omits Authorization Header ✅ 13ms
Request Payload.Token In Flags Body Matches Init ✅ 12ms
Request Payload.Groups Round Trip ✅ 12ms
Request Payload.Groups Default To Empty Object ✅ 12ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 12ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 12ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 12ms
Request Lifecycle.No Flags Request On Init Alone ✅ 5ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 511ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 19ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 12ms
Retry Behavior.Retries Flags On 502 ✅ 316ms
Retry Behavior.Retries Flags On 504 ✅ 317ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 515ms

Feature_Flags_Local_Evaluation Tests

✅ 4/4 tests passed

View Details
Test Status Duration
Versioned Boolean Matching.Matching Version Missing ✅ 71ms
Versioned Boolean Matching.Matching Version 1 ✅ 71ms
Versioned Boolean Matching.Matching Version 2 ✅ 74ms
Versioned Boolean Matching.Version Only Reload 1 2 1 2 Missing ✅ 45ms

…leep

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andehen
andehen marked this pull request as ready for review November 6, 2026 09:05
@andehen
andehen requested a review from a team as a code owner November 6, 2026 09:05
@andehen andehen added the stamphog Request stamphog review label Nov 6, 2026
@stamphog stamphog Bot removed the stamphog Request stamphog review label Nov 6, 2026

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approved — escalated to a human reviewer.

Re-add the stamphog label to request another review once you have addressed this.

The change enters data-ingestion territory without independent assurance; no current-head reviews, owning-team membership, or STRONG familiarity are reported. Request review from a teammate or agent reviewer before re-requesting, with Eli Reisman or Manoel Aranda Neto suggested based on familiarity.

  • Author wrote 0% of the modified lines and has 5 merged PRs in these paths (familiarity MODERATE).
  • Risky territory without independent assurance: Data ingestion or write paths — posthog/capture_v1.py changes event-delivery behavior so HTTP 404 and 429 responses retry the pending batch using the existing backoff and retry budget instead of failing immediately.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
size ✓ 9L, 1F substantive, 37L/4F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1b-small (37L, 4F, two-areas, fix)
stamphog 2.7.0 .stamphog/policy.yml @ 6e7aa13 · reviewed head 6e7aa13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant