diff --git a/src/grimoire/services/family_adoption.py b/src/grimoire/services/family_adoption.py index 8ae8a197..6c077aff 100644 --- a/src/grimoire/services/family_adoption.py +++ b/src/grimoire/services/family_adoption.py @@ -1061,10 +1061,12 @@ async def answer_reference_holders(user_id: int, idea_id: int) -> list[dict]: """A project whose own snippet is one of a canon idea's reference implementations has built the idea, so its unassessed row is answered `adopted` (decided_via system, the snippet as evidence). Only an - unassessed row is answered — an answer somebody gave stands — and only - where the project's shapes say nothing about the idea, because the shape - ledger's answer and this one must never disagree. Returns the answers - given: {project_id, idea_id, snippet_id, decision_id}.""" + unassessed row is answered — an answer somebody gave stands. Where the + project's shapes already speak for the idea (the reference's own + `canonical` stamp, an instance, a variant), the shape ledger gives the + answer now, as the coverage refresh would, because the two ledgers never + disagree. Returns the answers given: {project_id, idea_id, snippet_id, + decision_id}.""" async with async_session() as session: idea = await session.get(FamilyIdea, idea_id) if idea is None or idea.status != "canon": @@ -1088,9 +1090,18 @@ async def answer_reference_holders(user_id: int, idea_id: int) -> list[dict]: continue if row is not None and row.status != "unassessed": continue - if shapes_say(await _shape_evidence(session, project_id, idea_id)): - continue - row = row or await _row_for_write(session, project_id, idea_id) + spoken = shapes_say(await _shape_evidence(session, project_id, idea_id)) is not None + if spoken: + # The reference row that made the shapes evidence may be new, so + # nothing has synced them onto this idea yet. + for moved in await sync_from_shapes(user_id, project_id, [snippet_id]): + if moved["idea_id"] == idea_id: + given.append({"project_id": project_id, "idea_id": idea_id, + "snippet_id": snippet_id, "decision_id": moved["decision_id"]}) + continue + async with async_session() as session: + row = await _row(session, project_id, idea_id) \ + or await _row_for_write(session, project_id, idea_id) reason = (f"{project_title} holds a reference implementation of this idea: " f"#{snippet_id} \u201c{snippet_title}\u201d.") before = _row_state(row) diff --git a/tests/test_integration_family_promotion.py b/tests/test_integration_family_promotion.py index 42a12b42..36589b9b 100644 --- a/tests/test_integration_family_promotion.py +++ b/tests/test_integration_family_promotion.py @@ -238,6 +238,37 @@ async def test_a_snippet_stays_in_its_project_as_a_reference_and_its_project_is_ assert answer.decided_via == "system" and answer.evidence["source"] == "reference" +async def test_a_reference_the_shape_ledger_already_holds_is_answered_by_the_shapes_at_promotion(family): + """The project's own snippet is already stamped `canonical` in its shape + ledger, so the shapes answer the idea the moment promotion makes that + snippet a reference — not at the project's next coverage refresh.""" + from grimoire.models.code_shape import CodeShape + from grimoire.services import family_adoption + async with async_session() as s: + snippet = Note(user_id=family["owner"], project_id=family["a"], note_type="snippet", + title="ReleaseSigner", body="fun sign(apk: File) = ...", + data={"language": "kotlin"}) + s.add(snippet) + await s.flush() + s.add(CodeShape(project_id=family["a"], repo_key="git.example.com/x/y", + path="app/src/ReleaseSigner.kt", symbol="sign", kind="sym", + status="canonical", snippet_id=snippet.id, classified_by="mechanical")) + await s.commit() + await s.refresh(snippet) + out = await family_svc.promote( + family["owner"], snippet.id, applies_when="any Android app that ships its own APK", + platforms=["android-app"], criteria=CRITERIA, evidence=["CI green"], reason="proven", + ) + lifted = out["idea"]["note_id"] + assert (await _ledger(lifted))[family["a"]] == "adopted" + assert [a["project_id"] for a in out["answered"]] == [family["a"]] + async with async_session() as s: + answer = (await s.execute(select(FamilyDecision).where( + FamilyDecision.idea_id == lifted, FamilyDecision.project_id == family["a"]))).scalars().one() + assert answer.decided_via == "system" + assert answer.evidence["source"] == family_adoption.SHAPE_SOURCE + + async def test_an_answer_somebody_gave_is_not_overwritten_by_the_reference(family): from grimoire.services import family_adoption