Skip to content

feat(app): bake git SHA and build number into analytics and crash reports - #14336

Merged
Git-on-my-level merged 2 commits into
mainfrom
task/msd-ci-v10-build-provenance
Sep 17, 2026
Merged

Git-on-my-level merged 2 commits into
mainfrom
task/msd-ci-v10-build-provenance

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

What changed and why

Today nothing in app/lib knows its git commit. pubspec.yaml is 1.0.543+992; Codemagic computes a different BUILD_NUMBER from TestFlight / App Store / Play. Production PostHog events and Crashlytics issues cannot be resolved to a commit with one query.

This bakes OMI_GIT_SHA + OMI_BUILD_NUMBER (+ OMI_GIT_DIRTY=true only when a tracked file differs from HEAD) via --dart-define on every Codemagic app build (ios-internal-auto, android-internal-auto, ios-prod-testflight, android-prod-internal, ios-prod-patch, android-prod-patch) and on local/dev harness setup.sh builds. A single Dart accessor (BuildProvenance) defaults to 'unknown' when a define is omitted.

Those properties are registered as PostHog super-properties through the existing analytics adapter (PostHog stays a no-op without a key) and as Crashlytics custom keys inside CrashlyticsManager.init(), which already runs after Firebase.initializeApp. app/lib/main.dart is not touched.

Dirty flag: git status --porcelain would have marked Codemagic releases -dirty because those workflows write untracked files into the checkout (.env, firebase_key.json, FlutterFire output). Ignored paths cover env files, lib/firebase_options_*.dart, google-services.json, GoogleService-Info.plist, and key.properties; untracked-but-not-ignored files (e.g. firebase_key.json) still show in porcelain. Dirty now means git diff --quiet HEAD -- failed: a tracked file differs in the worktree or the index. Untracked and gitignored files do not count. When CM_BUILD_ID is set and the tree is dirty, the script prints build provenance: tracked files differ from HEAD: plus git diff --name-only HEAD -- so the Codemagic log names the files. SHA and build number are validated (^(unknown|[0-9a-f]{40})$ / ^(local|[0-9]+)$) before print, so unquoted $(scripts/build_provenance_dart_defines.sh) cannot word-split.

Codemagic cwd: all six workflows set working_directory: app. pod install cd ios is a prior step; the Flutter / Shorebird build step starts again in app/, so scripts/build_provenance_dart_defines.sh resolves.

Shorebird --dart-define: docs.shorebird.dev/code-push/patch/ — shorebird patch wraps flutter build; args after -- are flutter build args; example shorebird patch android -- --dart-define="foo=bar". FAQ: "Dart defines are compiled into your app's Dart code, they can be updated via patches." A patch that changes only OMI_GIT_SHA (a Dart String.fromEnvironment constant) is a valid Dart-snapshot patch; native / flavor / obfuscate must still match the store release. Both patch workflows pass the defines after --.

Shorebird patch number (cut, follow-up): shorebird_code_push was added to read ShorebirdUpdater.readCurrentPatch() for one string. That package is Dart + FFI into the updater already present in Shorebird-built engines; on flutter run / the dev flavor (non-Shorebird engine) it is unavailable and prints a banner. It would register a plugin in every binary, including store builds, and the previous init awaited the read before first events. Patch number is a nice-to-have; this package's value is lead time through store review. SHA + build number ship now. Follow-up if wanted: async read, never throw into startup, registerSuperProperties late so the first events are not delayed.

This is useless for production until a build carrying it has passed store review.

Product invariants affected

  • INV-AUTH-1
  • INV-DATA-1
  • INV-BETA-1

Path-based: codemagic.yaml is on these globs. This PR only appends --dart-define provenance flags to existing mobile app build/patch steps. It does not change OAuth, Firebase project, API routing, or desktop Beta identity.

How it was verified

  • bash app/scripts/build_provenance_dart_defines_test.sh (clean / tracked-dirty / untracked-only / non-git / BUILD_NUMBER / OMI_BUILD_NUMBER / CM_TAG ±+N / none / whitespace fail / Codemagic dirty file list)
  • bash app/scripts/mobile_build_wrapper_test.sh
  • flutter test test/unit/build_provenance_test.dart test/unit/build_provenance_registration_test.dart
  • TZ=UTC bash app/test.sh
  • uv run --python 3.11.15 --with pytest pytest --noconftest backend/tests/unit/test_workflow_contracts.py
  • make preflight
  • Codemagic whole-document digest fixture updated: .github/scripts/fixtures/codemagic_workflow_contract/v1.json

Could not verify: a store-signed binary in PostHog/Crashlytics (needs store review). Host tests have no Firebase app; Crashlytics registration is asserted as a no-op before Firebase.initializeApp plus matching key names.

Query: event → commit

PostHog (super properties git_sha and build_number on every event once the adapter has inited):

  1. PostHog → Activity → Event explorer (or SQL).
  2. Filter: property git_sha equals the full 40-char git SHA (dirty local builds end in -dirty; strip that suffix before git show).
  3. HogQL:
SELECT timestamp, event, distinct_id,
       properties.git_sha,
       properties.build_number
FROM events
WHERE properties.git_sha = '<full-git-sha>'
ORDER BY timestamp DESC
LIMIT 100

Then git show <full-git-sha>. build_number is the store / Codemagic integer (BUILD_NUMBER), not pubspec.yaml's +992.

Crashlytics: Firebase console → Crashlytics → Issues → filter by custom key git_sha equals <full-git-sha> → open the issue → Keys tab shows git_sha and build_number. Same mapping as PostHog.

Tests

  • Accessor defaults and dirty-SHA marking: app/test/unit/build_provenance_test.dart
  • Super-property registration + Crashlytics key names + no-op before Firebase: app/test/unit/build_provenance_registration_test.dart
  • Local wrapper injects the three dart-defines: app/scripts/mobile_build_wrapper_test.sh
  • Dirty semantics + build-number sources + validation: app/scripts/build_provenance_dart_defines_test.sh (run from app/test.sh and app/scripts/test.sh)
  • All six Codemagic mobile app workflows call scripts/build_provenance_dart_defines.sh; script uses tracked-only dirty detection

Failure class (fixes)

Failure-Class: none


Part of the mobile self-driving program (CI and release lane, package V10). Implemented by Cursor cursor-grok-4.6-high. This goes early because it is useless until a build carrying it has passed store review. Coordinator verification on the head commit: the script's shell tests pass (clean, tracked-dirty, untracked-only, non-git, every build-number source); the two Dart unit test files pass (7); on a clean tree the script emits a 40-hex SHA with OMI_GIT_DIRTY=false. The first review round rejected a dirty flag that counted untracked files (it would have labelled Codemagic release builds -dirty) and a new shorebird_code_push runtime dependency added for one string; both are gone. Not verifiable before merge: that each of the six Codemagic workflows resolves the script and passes the defines — the first internal build after merge is that check, and the query in this body is how to confirm it.

🤖 Generated with Claude Code

Review in cubic

Git-on-my-level and others added 2 commits September 17, 2026 15:39
Codemagic and the local harness pass --dart-define OMI_GIT_SHA /
OMI_BUILD_NUMBER (plus a dirty-tree marker). A single accessor defaults
to unknown, then PostHog super-properties and Crashlytics keys carry
them so a production signal resolves to a commit. Shorebird patches
also report the runtime patch number when the updater is present.

Co-authored-by: Cursor <[email protected]>
Porcelain counted Codemagic's untracked env and Firebase files as dirty,
so a release SHA would not resolve. Validate dart-define values so
unquoted command substitution cannot word-split.

Failure-Class: none
Co-authored-by: Cursor <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit fc38608 into main Sep 17, 2026
60 checks passed
@Git-on-my-level
Git-on-my-level deleted the task/msd-ci-v10-build-provenance branch September 17, 2026 11:30
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