Repository navigation
fix(enrichment): PLTF-3689 save PR stats once, count attempts up front - #586
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||
bf86e6e to
dfa97bc
Compare
…claim update_pr_openhands_stats guarded concurrent writes with updated_at != original_updated_at, but updated_at has no onupdate, so a successful write never advanced it and the guard could not see a concurrent commit. Refuse the write under the existing row lock when the PR is already processed. increment_process_attempts ran after processing, so a crash loop never exhausted MAX_RETRIES. Replace it with claim_pr_for_processing, which consumes the attempt atomically before any work. OHE-3417 (Track 1, T1.4) Co-authored-by: openhands <openhands@all-hands.dev>
dfa97bc to
f61d0bb
Compare
Mutation review: test strengthI broke the changed code by hand, one mutant at a time, and ran the two test files this PR adds against a real Postgres each time. Baseline: 10 passed. Controls (revert the fix; these must fail)
Also caught: the claim ignoring Survivors
M6: the row lock is not pinned"First successful completion wins" between runs that truly overlap depends on async def test_write_waits_for_a_finisher_holding_the_row(self, store):
"""A run that already holds the row and commits first must win."""
await _insert_pr(store)
original = (await _load()).updated_at
async with a_session_maker() as holder:
row = (
await holder.execute(select(OpenhandsPR).with_for_update())
).scalars().one()
late = asyncio.create_task(_record(store, original, commits=2))
await asyncio.sleep(0.2)
row.processed = True
row.num_openhands_commits = 1
await holder.commit()
assert await late is False
assert (await _load()).num_openhands_commits == 1With the lock in place, M3 / M4: the claim's scope is not pinnedEvery claim test inserts exactly one PR, so the async def test_claim_touches_only_the_named_pr(self, store):
await _insert_pr(store)
await _insert_pr(store, repo_id='other-repo')
await _insert_pr(store, pr_number=PR_NUMBER + 1)
claimed = await store.claim_pr_for_processing(REPO_ID, PR_NUMBER, MAX_RETRIES)
assert claimed is not None
assert (claimed.repo_id, claimed.pr_number) == (REPO_ID, PR_NUMBER)
async with a_session_maker() as session:
rows = (
await session.execute(
select(
OpenhandsPR.repo_id,
OpenhandsPR.pr_number,
OpenhandsPR.process_attempts,
)
)
).all()
assert sorted(rows) == sorted(
[
(REPO_ID, PR_NUMBER, 1),
('other-repo', PR_NUMBER, 0),
(REPO_ID, PR_NUMBER + 1, 0),
]
)M7:
|
Kill surviving mutants from the mutation review: the stats write must wait on a row lock held by a finisher, the claim must touch only the named PR, and process_pr must hand the claimed row (not the stale scan) to save_full_pr. Also add the missing f-prefix on the 'Did not find PR' warning. Co-authored-by: openhands <openhands@all-hands.dev>
|
Addressed the mutation review in fdc840d.
Each mutant was applied on top of the new commit and failed exactly the test named next to it. No source behaviour changed apart from the log line. Verified: This comment was generated by an AI assistant on behalf of the user. |
Code review: #586, head
|
Keep the winner's saved PR file when the stats write is refused, and report the outcome of process_pr so a skipped PR is not logged as processed. Co-authored-by: openhands <openhands@all-hands.dev>
|
Both nits addressed in 18cce17.
This comment was generated by an AI agent (OpenHands) on behalf of the user. |
The docstring claimed True meant the PR was enriched, but it means the claim succeeded; an overlapping run that loses the stats write still returns True. Co-authored-by: openhands <openhands@all-hands.dev>
Say claiming reserves an attempt rather than implying exclusive ownership, and state that the first stats write to commit is the one kept. Co-authored-by: openhands <openhands@all-hands.dev>
The test asserted a successful claim, not completed enrichment; the old wording was removed from process_pr in c23b2ed. Co-authored-by: openhands <openhands@all-hands.dev>
Code review: #586, head
|
|
Replicated no-regression test — ✅ PASSTested this PR's enterprise-server image ( Setup
Results
Scope noteThe closed-PR enrichment job that this PR changes is disabled by default in the self-hosted product (the cronjob is gated off) and depends on a configured GitHub App, which a GitLab-based install does not have. So this test validates that the PR's image does not regress install, auth, or the conversation flow — it does not exercise the enrichment concurrency-guard fix itself, which is covered by the PR's DB-backed unit/integration tests. Conclusion: No regression observed. ✅ This comment was posted by an AI agent (OpenHands) on behalf of @aivong. |
Impact assessmentVerdict: Only SaaS prod and feature previews behave differently, plus any OHE install that turned the PR-enrichment job on by hand. OHE / Replicated defaults, staging and dev are unaffected. The only opt-out is
Validation: The diff and call sites were read statically at Customer installs:
Rollout: SaaS prod and feature previews get this with the next enterprise-server image bump. PRs that currently fail on every run will use up their 3 attempts within about 30 minutes (three 10-minute runs). After that they stay unprocessed until someone resets Changed symbols and call sites
This analysis was produced by an AI agent (OpenHands) on behalf of @aivong-openhands. |
|
🚀 Released in 1.69.0. |
HUMAN:
AGENT:
Why
Two runs of the PR enrichment job can overlap, for example when a stalled run is reclaimed while its process is still alive. Two defects make that unsafe. The duplicate-write guard compares
updated_at, but nothing advances that column after the row is inserted, so a run that finishes second overwrites the first. And an attempt is counted only after processing, so a PR whose processing crashes every time is retried forever instead of stopping atMAX_RETRIES.Enrichment records each closed PR once, so the first run to finish should win. Under the row lock the stats write already takes, it is now refused if the PR is already processed, and the later run writes nothing, neither the database row nor the saved PR file. The attempt is now consumed when the PR is claimed, before any work, so a crash still uses it up. The
updated_atcheck stays, because it still catches a row thatinsert_prreplaced mid-run.Summary
update_pr_openhands_statsrefuses the write when the PR is alreadyprocessed.claim_pr_for_processingreplacesincrement_process_attempts. It consumes an attempt in a singleUPDATE ... RETURNINGand returns nothing for a PR that is already processed or out of attempts.save_full_prand skips it when the claim returns nothing.save_full_prreturns before saving the PR file when the stats write is refused, so a losing run does not replace the winner's file.process_prreturns whether it claimed the PR, and the job only logs success when it did.Issue Number
OHE-3417 (the tracked issue; the title's PLTF-3689 is the task-queue umbrella, as on #587).
Fixes #554. The
install-gitlab-webhookshalf is now tracked separately in #623.How to Test
uv run pytest tests/unit/storage/test_openhands_pr_store.py tests/unit/sync/test_enrich_user_interaction_data.py tests/unit/integrations/github/test_data_collector_save_full_pr.py(thePython Tests on LinuxCI job also runs them). Against a real database, the tests check that:insert_prhas since replaced writes nothing;MAX_RETRIES;Every writer of
OpenhandsPR.updated_atwas also checked. Onlyinsert_prchanges it, by deleting and re-inserting the row. The column has noonupdate, and no migration adds a trigger toopenhands_prs.Video/Screenshots
N/A. No UI change.
Type
Notes
No migration or config change.
This PR was drafted by an AI agent on behalf of the user.
@aivong-openhands can click here to continue refining the PR
Enterprise server image for this PR: