Skip to content

fix(git): keep task PR state in sync when the primary PR changes#3263

Merged
frankh merged 2 commits into
mainfrom
posthog-code/sync-task-pr-state
Jul 8, 2026
Merged

fix(git): keep task PR state in sync when the primary PR changes#3263
frankh merged 2 commits into
mainfrom
posthog-code/sync-task-pr-state

Conversation

@frankh

@frankh frankh commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Problem

The #3227 feature let a task carry multiple PRs, with the "primary" PR being whichever URL sits at index 0 of an ordered list. A task's displayed state (merged / open / draft / closed) is a denormalized cache derived from that primary PR.

When a user promotes a different PR to primary (via the "Other PRs" submenu), TaskPrStatusService.setPrimaryPrUrl reordered the URL list but broadcast the old primary's cached prState alongside the new primary's URL. If the old primary was merged and the new primary was still open, every consumer of the taskPrInfoChanged event (sidebar badge, inbox, home, code-review) showed the task as "merged" while its primary PR was actually open. The drift only self-healed up to ~60s later when the next background revalidation poll overwrote the cache.

Root cause: promotePrUrl only rewrites the prUrls JSON column; the prUrl/prState columns are maintained by a separate updatePrCache path driven by revalidation, so the two disagreed until the next poll.

Fix

setPrimaryPrUrl now fetches the promoted PR's live state via gitService.getPrDetailsByUrl + mapPrState (the same primitives computeTaskPrStatus already uses), persists it through updatePrCache, then emits the fresh state. The cache columns and prUrls[0] can no longer disagree, and every taskPrInfoChanged consumer updates immediately rather than on the next poll.

The port signature changed from void to Promise<void>; the tRPC router forward already awaits it transparently.

Non-goals

The reverse scenario (a non-primary PR merging) intentionally leaves task state unchanged, since state is derived only from the primary PR.

Testing

  • Replaced the setPrimaryPrUrl test that codified the buggy stale-emit with two tests: one asserting the promoted PR's live state is recomputed and emitted, one covering getPrDetailsByUrl → null yielding prState: null.
  • pnpm --filter @posthog/workspace-server test task-pr-status — 7 tests pass.
  • @posthog/workspace-server and @posthog/host-router typecheck clean.

Created with PostHog Code

Promoting a different PR to primary reordered the URL list but broadcast
the old primary's cached prState alongside the new primary's URL, so a
task could show "merged" while its primary PR was actually open until the
next background poll (~60s later) overwrote the cache.

setPrimaryPrUrl now fetches the promoted PR's live state via
getPrDetailsByUrl + mapPrState, persists it through updatePrCache, and
emits the fresh state, so the cache columns and prUrls[0] can no longer
disagree and every taskPrInfoChanged consumer updates immediately.

Generated-By: PostHog Code
Task-Id: 2772f1aa-cba4-46df-a8b6-3a14a0f15299
@trunk-io

trunk-io Bot commented Jul 8, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 0ed3219.

@frankh frankh marked this pull request as ready for review July 8, 2026 11:28
@frankh frankh added the Stamphog This will request an autostamp by stamphog on small changes label Jul 8, 2026
@frankh frankh enabled auto-merge (squash) July 8, 2026 11:29
github-actions[bot]
github-actions Bot previously approved these changes Jul 8, 2026

@github-actions github-actions 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.

Small correctness fix: promotes PR URL then fetches live state instead of emitting stale cached state. The interface change from void to Promise<void> is consistent across the interface, implementation, and the single router call site (tRPC mutation handles async natively). No showstoppers.

@greptile-apps

greptile-apps Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "fix(git): keep task PR state in sync whe..." | Re-trigger Greptile

Comment thread packages/workspace-server/src/services/git/task-pr-status.ts
Comment thread packages/workspace-server/src/services/git/task-pr-status.test.ts Outdated
Guard getPrDetailsByUrl with a catch so a network/API failure after the
prUrls reorder still runs updatePrCache and emit with prState null,
instead of leaving prUrls[0] and the prUrl/prState columns disagreeing.
Collapse the setPrimaryPrUrl tests into an it.each table and add the
rejection case.

Generated-By: PostHog Code
Task-Id: 2772f1aa-cba4-46df-a8b6-3a14a0f15299
@github-actions github-actions Bot dismissed their stale review July 8, 2026 11:34

New commits pushed (delta classified non_trivial_delta) — stamphog approval dismissed; re-review running automatically.

@github-actions github-actions 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.

Small correctness fix that fetches live PR state after promoting a URL instead of emitting stale cached data. The .catch(() => null) guards against network failures, and the Promise<void> interface change is handled natively by the tRPC mutation call site. Both reviewer concerns were addressed in the current head.

@frankh frankh merged commit 6444db5 into main Jul 8, 2026
26 checks passed
@frankh frankh deleted the posthog-code/sync-task-pr-state branch July 8, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Stamphog This will request an autostamp by stamphog on small changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant