Skip to content

feat: canonical bottom-up stack frame order for error tracking#200

Draft
cat-ph wants to merge 1 commit into
mainfrom
cat/canonical-frame-order
Draft

feat: canonical bottom-up stack frame order for error tracking#200
cat-ph wants to merge 1 commit into
mainfrom
cat/canonical-frame-order

Conversation

@cat-ph

@cat-ph cat-ph commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Problem

PHP's Throwable::getTrace() is innermost-first: the crash site sits at index 0 and the entry point is last. The PHP SDK currently serializes $exception_list[].stacktrace.frames in that raw order, so frames[0] is the crash site. The cross-SDK stack frame ordering standard (PostHog/sdk-specs#11) defines the canonical wire order as bottom-up: frames[0] is the outermost/entry-point call and the last frame is the crash site.

Spec: PostHog/sdk-specs#11

Change

ExceptionPayloadBuilder::buildStacktrace() now reverses the built frames after truncating to max_frames. Slicing before reversing keeps the most-relevant crash-site frames (the ones PHP puts first) and then orders them bottom-up, so the crash site — including the synthetic throw-site frame prepended for exceptions that omit their throw location from getTrace() — ends up last.

Both frame-producing paths flow through buildStacktrace(), so this covers thrown exceptions (getTrace()) and the PHP error/warning handler path (debug_backtrace()). The $exception_list chain order is unchanged (still outermost-first via the getPrevious() walk). Source context fields (context_line/pre_context/post_context) stay attached to their frames and remain in canonical file order.

Tests

  • Updated the existing frame-order assertions in ExceptionPayloadBuilderTest and ExceptionCaptureTest to expect the crash site at the last frame instead of the first.
  • Added testStacktraceFramesUseCanonicalBottomUpOrder as an explicit regression guard: a nested throw must serialize entry point first and crash site (the synthetic throw-site frame) last.
  • Scoped suite green: vendor/bin/phpunit test/ExceptionPayloadBuilderTest.php test/ExceptionCaptureTest.php — 42 tests, 208 assertions. Full suite also green.

Coordination

This is a breaking wire-order change for the stack frame ordering. Merge and release only after the pipeline normalization gate (cymbal) is live, so the backend can normalize old vs new ordering. The changeset is intentionally a minor bump (not major) so the pipeline can gate on $lib_version to distinguish SDK versions emitting the old order from those emitting canonical order.

PHP's getTrace() is innermost-first (crash site at index 0). PostHog's
canonical wire order for $exception_list[].stacktrace.frames is bottom-up:
frames[0] is the outermost/entry-point call and the last frame is the crash
site. Reverse the built frames in buildStacktrace after slicing to maxFrames
so the crash-site (most-relevant) frames are retained and ordered last.

Aligns the PHP SDK with the cross-SDK stack frame ordering standard.
@greptile-apps

greptile-apps Bot commented Jul 3, 2026

Copy link
Copy Markdown

Reviews (1): Last reviewed commit: "feat: emit error tracking stack frames i..." | Re-trigger Greptile

Comment on lines +274 to +286
// The three helpers unwind inward: leaf (crash) is called by middle, called by the entry
// point. Canonical order therefore reads entry -> middle -> leaf from first to last.
$entryIndex = array_search(__CLASS__ . '->orderedNestedThrowHelper', $functions, true);
$middleIndex = array_search(__CLASS__ . '->orderedThrowMiddle', $functions, true);
$leafIndex = array_search(__CLASS__ . '->orderedThrowLeaf', $functions, true);

$this->assertNotFalse($entryIndex);
$this->assertNotFalse($middleIndex);
$this->assertNotFalse($leafIndex);
$this->assertTrue(
$entryIndex < $middleIndex && $middleIndex < $leafIndex,
'frames must read bottom-up: entry point first, crash site last'
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Throwing Frame Is Missing

When orderedThrowLeaf() throws, PHP omits that throwing function from Throwable::getTrace(). The synthetic crash-site frame preserves the throw file and line, but it does not add orderedThrowLeaf as a function name, so $leafIndex stays false and this test fails in CI.

Suggested change
// The three helpers unwind inward: leaf (crash) is called by middle, called by the entry
// point. Canonical order therefore reads entry -> middle -> leaf from first to last.
$entryIndex = array_search(__CLASS__ . '->orderedNestedThrowHelper', $functions, true);
$middleIndex = array_search(__CLASS__ . '->orderedThrowMiddle', $functions, true);
$leafIndex = array_search(__CLASS__ . '->orderedThrowLeaf', $functions, true);
$this->assertNotFalse($entryIndex);
$this->assertNotFalse($middleIndex);
$this->assertNotFalse($leafIndex);
$this->assertTrue(
$entryIndex < $middleIndex && $middleIndex < $leafIndex,
'frames must read bottom-up: entry point first, crash site last'
);
// The reachable PHP trace records caller frames; the throwing function itself is
// represented by the synthetic crash-site frame's file and line below.
$entryIndex = array_search(__CLASS__ . '->orderedNestedThrowHelper', $functions, true);
$middleIndex = array_search(__CLASS__ . '->orderedThrowMiddle', $functions, true);
$this->assertNotFalse($entryIndex);
$this->assertNotFalse($middleIndex);
$this->assertTrue(
$entryIndex < $middleIndex,
'frames must read bottom-up: entry point first, crash-site caller last'
);

@@ -0,0 +1,5 @@
---
"posthog-php": minor

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Breaking Change Bumps Minor

This changes the wire meaning of stacktrace.frames[0] from crash site to entry point. Consumers that follow normal version ranges can upgrade from 4.8.x to 4.9.0 and silently read the wrong crash location, so the changeset should match the repo policy for breaking changes unless the release policy is also being changed.

Suggested change
"posthog-php": minor
"posthog-php": major

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

posthog-php Compliance Report

Date: 2026-07-03 00:08:06 UTC
Duration: 95316ms

✅ All Tests Passed!

46/46 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 12ms
Format Validation.Event Has Uuid 6ms
Format Validation.Event Has Lib Properties 6ms
Format Validation.Distinct Id Is String 6ms
Format Validation.Token Is Present 6ms
Format Validation.Custom Properties Preserved 5ms
Format Validation.Event Has Timestamp 6ms
Retry Behavior.Retries On 503 5315ms
Retry Behavior.Does Not Retry On 400 2010ms
Retry Behavior.Does Not Retry On 401 2009ms
Retry Behavior.Respects Retry After Header 8015ms
Retry Behavior.Implements Backoff 15729ms
Retry Behavior.Retries On 500 5115ms
Retry Behavior.Retries On 502 5114ms
Retry Behavior.Retries On 504 5115ms
Retry Behavior.Max Retries Respected 16528ms
Deduplication.Generates Unique Uuids 12ms
Deduplication.Preserves Uuid On Retry 5113ms
Deduplication.Preserves Uuid And Timestamp On Retry 10321ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 5115ms
Deduplication.No Duplicate Events In Batch 12ms
Deduplication.Different Events Have Different Uuids 7ms
Compression.Sends Gzip When Enabled 6ms
Batch Format.Uses Proper Batch Structure 6ms
Batch Format.Flush With No Events Sends Nothing 3ms
Batch Format.Multiple Events Batched Together 11ms
Error Handling.Does Not Retry On 403 2007ms
Error Handling.Does Not Retry On 413 2010ms
Error Handling.Retries On 408 5115ms

Feature_Flags Tests

17/17 tests passed

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

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