feat(8565,8566,8567,8568): promoting a family idea lifts it onto a family record with no project
CI & Build / TypeScript typecheck (push) Successful in 14s
CI & Build / Plugin hooks (push) Successful in 15s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Python tests (push) Failing after 1m55s
CI & Build / integration (push) Failing after 2m33s
CI & Build / Build & push image (push) Skipped

Canon now belongs to the family. Promotion moves an idea off the project
record it was written on and onto a note with no project (owned by the same
person, arose_from_id naming the project record), carrying its state,
platforms, references, ledger and decision log. The project record stays its
own; a snippet becomes a reference implementation. Revising the idea or
folding a conflict into it now edits the family record, never a project's.
The project record's id still names the idea in every family call
(family.idea_key).

- A project that owns a reference snippet and has not answered the idea is
  answered adopted by the system, naming the snippet, unless its shapes
  already speak for it (on promotion and on set_family_references).
- The family record takes the project record's shares and its project's
  shares, so whoever could read the idea still can.
- An owed task still finds its Systems through the project record the idea
  was lifted from.
- Migration 0141 lifts every idea promoted before this, the same way.
- The Family page shows where each idea lives and its reference
  implementations; the skill, tool descriptions, model docstring and
  features doc describe the family record. Plugin minted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-04 14:44:56 -05:00
co-authored by Claude Opus 5.5
parent 0d9fe5d3c5
commit 6c0481ef9c
13 changed files with 788 additions and 19 deletions
@@ -112,6 +112,8 @@ async def family():
evidence=["CI green on the release lane"], reason="proven, stated for the platform",
)
assert out["promoted"] is True
# Promotion lifted the idea onto a family record; the ledger is keyed on it.
ids["origin"], ids["note"] = ids["note"], out["idea"]["note_id"]
return ids
+120 -2
View File
@@ -112,6 +112,9 @@ async def _promote(f, **overrides):
async def _ledger(note_id: int) -> dict[int, str]:
# Promotion lifts the idea onto a family record; the project's record id
# still names it.
note_id = await family_svc.idea_key(note_id)
async with async_session() as s:
rows = (await s.execute(
select(FamilyAdoption.project_id, FamilyAdoption.status)
@@ -121,6 +124,7 @@ async def _ledger(note_id: int) -> dict[int, str]:
async def _idea(note_id: int) -> FamilyIdea:
note_id = await family_svc.idea_key(note_id)
async with async_session() as s:
return await s.get(FamilyIdea, note_id)
@@ -167,13 +171,127 @@ async def test_someone_who_cannot_write_the_note_cannot_promote_it(family):
)
# --- the family record ----------------------------------------------------------------
async def test_promotion_lifts_the_idea_onto_a_family_record_and_leaves_the_projects_record_alone(family):
async with async_session() as s:
s.add(FamilyIdea(note_id=family["note"], status="candidate"))
await s.flush()
s.add(FamilyDecision(idea_id=family["note"], action="propose", reason="repeated",
decided_via="system", user_id=family["owner"]))
await s.commit()
out = await _promote(family)
lifted = out["idea"]["note_id"]
assert lifted != family["note"]
assert out["lifted"] == {"family_record": lifted, "from": family["note"],
"note": out["lifted"]["note"]}
async with async_session() as s:
record = await s.get(Note, lifted)
origin = await s.get(Note, family["note"])
assert await s.get(FamilyIdea, family["note"]) is None
moved = (await s.execute(select(FamilyDecision.action).where(
FamilyDecision.idea_id == lifted).order_by(FamilyDecision.id))).scalars().all()
# The family record has no project, belongs to the same person, and names
# where it came from; the project's record is unchanged.
assert record.project_id is None and record.user_id == family["owner"]
assert record.arose_from_id == family["note"] and record.title == origin.title
assert f"#{family['note']}" in record.body
assert origin.project_id == family["a"]
assert origin.body == "One keystore, two channels, in-place update."
# Its whole history moved with it, the proposal included.
assert moved == ["propose", "promote"]
assert out["decision"]["evidence"]["lifted_from"] == family["note"]
# The project's record still names the idea everywhere an id is taken.
assert await family_svc.idea_key(family["note"]) == lifted
assert (await family_svc.get_idea(family["owner"], family["note"]))["note_id"] == lifted
with pytest.raises(ValueError, match="already canon"):
await _promote(family)
async def test_a_snippet_stays_in_its_project_as_a_reference_and_its_project_is_answered_adopted(family):
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.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"]
async with async_session() as s:
record = await s.get(Note, lifted)
# The code stays in the project's snippet; the family record carries the idea.
assert record.note_type == "note"
refs = await family_svc._reference_ids(lifted)
assert refs == {snippet.id}
# The project that wrote the reference has built the idea; the other
# member project still owes an answer.
assert await _ledger(lifted) == {family["a"]: "adopted", family["b"]: "unassessed"}
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" and answer.evidence["source"] == "reference"
async def test_an_answer_somebody_gave_is_not_overwritten_by_the_reference(family):
from grimoire.services import family_adoption
async with async_session() as s:
snippet = Note(user_id=family["owner"], project_id=family["b"], note_type="snippet",
title="Signer", body="...", data={"language": "kotlin"})
s.add(snippet)
await s.commit()
await s.refresh(snippet)
out = await _promote(family)
lifted = out["idea"]["note_id"]
async with async_session() as s:
row = (await s.execute(select(FamilyAdoption).where(
FamilyAdoption.idea_id == lifted, FamilyAdoption.project_id == family["b"]))).scalars().one()
row.status, row.reason, row.canon_version, row.decided_via = "exempt", "store-installed", 1, "agent"
await s.commit()
await family_adoption.set_references(family["owner"], lifted, [snippet.id])
assert (await _ledger(lifted))[family["b"]] == "exempt"
async def test_whoever_could_read_the_idea_through_its_project_can_read_the_family_record(family):
from grimoire.models.share import ProjectShare
from grimoire.services import access
async with async_session() as s:
s.add(ProjectShare(project_id=family["a"], shared_with_user_id=family["outsider"],
permission="viewer", invited_by=family["owner"]))
await s.commit()
out = await _promote(family)
lifted = out["idea"]["note_id"]
assert await access.get_note_permission(family["outsider"], lifted) == "viewer"
async def test_a_repeat_of_a_lifted_record_says_it_is_canon_and_opens_nothing_new(family):
out = await _promote(family)
repeat = await _note_in(family["b"], family["owner"], "One keystore, two channels.")
async with async_session() as s:
original = await s.get(Note, family["note"])
with patch("grimoire.services.embeddings.semantic_search_notes",
AsyncMock(return_value=[(0.86, original)])):
hint = await family_svc.repeat_trigger(family["owner"], repeat)
assert hint and "already" in hint and "family canon" in hint
async with async_session() as s:
ideas = (await s.execute(select(FamilyIdea.note_id))).scalars().all()
assert family["note"] not in ideas and out["idea"]["note_id"] in ideas
# --- undo and retirement ------------------------------------------------------------
async def test_undoing_a_promotion_restores_the_prior_state_and_keeps_judged_rows(family):
out = await _promote(family)
async with async_session() as s:
row = (await s.execute(select(FamilyAdoption).where(
FamilyAdoption.idea_id == family["note"],
FamilyAdoption.idea_id == out["idea"]["note_id"],
FamilyAdoption.project_id == family["a"]))).scalars().one()
row.status, row.canon_version, row.decided_via = "adopted", 1, "agent"
await s.commit()
@@ -218,7 +336,7 @@ async def test_an_undo_names_what_it_reversed_as_its_precedent(family):
assert undo["decision"]["precedent_ids"] == [out["decision"]["id"]]
async with async_session() as s:
actions = (await s.execute(select(FamilyDecision.action).where(
FamilyDecision.idea_id == family["note"]).order_by(FamilyDecision.id))).scalars().all()
FamilyDecision.idea_id == out["idea"]["note_id"]).order_by(FamilyDecision.id))).scalars().all()
assert actions == ["promote", "undo"]
@@ -0,0 +1,161 @@
"""Migration 0141 against real Postgres: every promoted idea still on a
project's record moves onto a family record with no project.
The lane migrates an empty database, so the migration's work is driven here
directly: rows are seeded the way a promotion made them before promotion
lifted, then the migration's own function runs on the test connection.
"""
import importlib.util
from pathlib import Path
import pytest
import pytest_asyncio
from sqlalchemy import select
from grimoire.models import async_session
from grimoire.models.family import (
FamilyAdoption, FamilyDecision, FamilyIdea, FamilyIdeaPlatform, FamilyIdeaReference,
Platform, ProjectPlatform,
)
from grimoire.models.note import Note
from grimoire.models.project import Project
from grimoire.models.share import NoteShare, ProjectShare
from grimoire.models.user import User
from grimoire.services import access
from grimoire.services import family as family_svc
from tests.helpers import ensure_user
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
OWNER = "family_records_owner"
READER = "family_records_reader"
_MIGRATION = Path(__file__).resolve().parents[1] / "alembic" / "versions" / "0141_family_records.py"
def _migration():
spec = importlib.util.spec_from_file_location("migration_0141", _MIGRATION)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
async def _purge(username: str) -> None:
"""SETUP ONLY: a database call after a `yield` in a fixture orphans a
pooled connection. Notes and projects cascade to every family row."""
async with async_session() as s:
for user in (await s.execute(select(User).where(User.username == username))).scalars():
for note in (await s.execute(select(Note).where(Note.user_id == user.id))).scalars():
await s.delete(note)
for project in (await s.execute(select(Project).where(Project.user_id == user.id))).scalars():
await s.delete(project)
await s.commit()
@pytest_asyncio.fixture
async def promoted():
"""A Kotlin snippet in the first of two Android apps, promoted to canon
the way promotion stored it before it lifted: the idea on the snippet,
an unassessed row for each app, the first app shared with a reader."""
await _purge(OWNER)
await _purge(READER)
async with async_session() as s:
android = await s.scalar(select(Platform.id).where(
Platform.slug == "android-app", Platform.deleted_at.is_(None)))
owner = await ensure_user(s, OWNER)
reader = await ensure_user(s, READER)
a = Project(user_id=owner.id, title="android one")
b = Project(user_id=owner.id, title="android two")
s.add_all([a, b])
await s.flush()
s.add_all([ProjectPlatform(project_id=a.id, platform_id=android, state="declared"),
ProjectPlatform(project_id=b.id, platform_id=android, state="declared"),
ProjectShare(project_id=a.id, shared_with_user_id=reader.id,
permission="viewer", invited_by=owner.id)])
snippet = Note(user_id=owner.id, project_id=a.id, note_type="snippet",
title="ReleaseSigner", body="fun sign(apk: File) = ...",
tags=["android"], data={"language": "kotlin"})
candidate = Note(user_id=owner.id, project_id=b.id, note_type="snippet",
title="Unproven", body="...")
s.add_all([snippet, candidate])
await s.flush()
s.add_all([
FamilyIdea(note_id=snippet.id, status="canon", canon_version=1,
applies_when="any Android app that ships its own APK"),
FamilyIdea(note_id=candidate.id, status="candidate"),
])
await s.flush()
s.add_all([
FamilyIdeaPlatform(note_id=snippet.id, platform_id=android),
FamilyAdoption(project_id=a.id, idea_id=snippet.id, status="unassessed"),
FamilyAdoption(project_id=b.id, idea_id=snippet.id, status="unassessed"),
FamilyDecision(idea_id=snippet.id, action="propose", reason="repeated",
decided_via="system", user_id=owner.id),
FamilyDecision(idea_id=snippet.id, action="promote", reason="proven",
decided_via="agent", user_id=owner.id),
FamilyDecision(idea_id=candidate.id, action="propose", reason="repeated",
decided_via="system", user_id=owner.id),
])
await s.commit()
return {"owner": owner.id, "reader": reader.id, "a": a.id, "b": b.id,
"snippet": snippet.id, "candidate": candidate.id}
async def _lift() -> list[tuple[int, int]]:
module = _migration()
async with async_session() as s:
conn = await s.connection()
moved = await conn.run_sync(module.lift_promoted_ideas)
await s.commit()
return moved
async def test_a_promoted_idea_moves_onto_a_family_record_and_a_candidate_stays(promoted):
moved = await _lift()
[(origin, family_id)] = [m for m in moved if m[0] == promoted["snippet"]]
async with async_session() as s:
record = await s.get(Note, family_id)
snippet = await s.get(Note, origin)
assert await s.get(FamilyIdea, origin) is None
assert (await s.get(FamilyIdea, family_id)).status == "canon"
assert (await s.get(FamilyIdea, promoted["candidate"])).status == "candidate"
actions = (await s.execute(select(FamilyDecision.action).where(
FamilyDecision.idea_id == family_id, FamilyDecision.project_id.is_(None))
.order_by(FamilyDecision.id))).scalars().all()
platforms = (await s.execute(select(FamilyIdeaPlatform.note_id))).scalars().all()
refs = (await s.execute(select(FamilyIdeaReference.snippet_id).where(
FamilyIdeaReference.idea_id == family_id))).scalars().all()
assert record.project_id is None and record.user_id == promoted["owner"]
assert record.note_type == "note" and record.arose_from_id == origin
assert record.tags == ["android"] and f"#{origin}" in record.body
assert snippet.project_id == promoted["a"] and snippet.body == "fun sign(apk: File) = ..."
assert actions == ["propose", "promote"]
assert family_id in platforms and origin not in platforms
assert refs == [origin]
assert await family_svc.idea_key(origin) == family_id
async def test_the_project_holding_the_reference_is_answered_and_the_other_still_owes(promoted):
[(_, family_id)] = [m for m in await _lift() if m[0] == promoted["snippet"]]
async with async_session() as s:
rows = dict((await s.execute(select(FamilyAdoption.project_id, FamilyAdoption.status)
.where(FamilyAdoption.idea_id == family_id))).all())
answer = (await s.execute(select(FamilyDecision).where(
FamilyDecision.idea_id == family_id,
FamilyDecision.project_id == promoted["a"]))).scalars().one()
assert rows == {promoted["a"]: "adopted", promoted["b"]: "unassessed"}
assert answer.decided_via == "system" and answer.evidence["source"] == "reference"
assert answer.after["status"] == "adopted" and answer.before["status"] == "unassessed"
async def test_whoever_read_it_through_its_project_reads_the_family_record(promoted):
[(_, family_id)] = [m for m in await _lift() if m[0] == promoted["snippet"]]
assert await access.get_note_permission(promoted["reader"], family_id) == "viewer"
async with async_session() as s:
shares = (await s.execute(select(NoteShare).where(NoteShare.note_id == family_id))).scalars().all()
assert [(sh.shared_with_user_id, sh.permission) for sh in shares] == [(promoted["reader"], "viewer")]
async def test_running_it_again_lifts_nothing(promoted):
first = await _lift()
assert promoted["snippet"] in [o for o, _ in first]
assert promoted["snippet"] not in [o for o, _ in await _lift()]