Repository navigation
feat(pm-stack): derive item genealogy read model #12070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import type { ProjectPoolItem, RefKey, RoadmapRow } from "./model.ts"; | ||
|
|
||
| export type GenealogyRelation = "parent" | "primary-parent" | "spawned-by" | "roadmap" | "priority"; | ||
|
|
||
| export interface GenealogyAncestor { | ||
| ref: RefKey; | ||
| title: string; | ||
| kind: string | null; | ||
| relation: GenealogyRelation; | ||
| } | ||
|
|
||
| export interface GenealogyRelease { | ||
| title: string; | ||
| dueOn: string | null; | ||
| ref: RefKey; | ||
| } | ||
|
|
||
| export interface ItemGenealogy { | ||
| ancestors: GenealogyAncestor[]; | ||
| release: GenealogyRelease | null; | ||
| afterRefs: RefKey[]; | ||
| sentence: string; | ||
| } | ||
|
|
||
| const labelTarget = (labels: string[], prefix: string): string | null => | ||
| labels.find((label) => label.startsWith(prefix))?.slice(prefix.length) ?? null; | ||
|
|
||
| /** Derive the item's known genealogy using only verified pool edges and labels. */ | ||
| export function deriveGenealogy(item: ProjectPoolItem, pool: readonly ProjectPoolItem[]): ItemGenealogy { | ||
| const byRef = new Map(pool.map((entry) => [entry.ref, entry])); | ||
| const chain: ProjectPoolItem[] = []; | ||
| const seen = new Set<RefKey>([item.ref]); | ||
| let cursor = item.parentRef ? byRef.get(item.parentRef) : undefined; | ||
| while (cursor && !seen.has(cursor.ref)) { | ||
| chain.push(cursor); | ||
| seen.add(cursor.ref); | ||
| cursor = cursor.parentRef ? byRef.get(cursor.parentRef) : undefined; | ||
| } | ||
|
|
||
| const ancestors: GenealogyAncestor[] = []; | ||
| const add = (entry: ProjectPoolItem | undefined, relation: GenealogyRelation) => { | ||
| if (entry && !ancestors.some((ancestor) => ancestor.ref === entry.ref && ancestor.relation === relation)) { | ||
| ancestors.push({ ref: entry.ref, title: entry.title, kind: entry.kind, relation }); | ||
| } | ||
| }; | ||
| for (const entry of chain) add(entry, "parent"); | ||
| const historicalParents = (item.historicalParentRefs ?? []).flatMap((ref) => { | ||
| const former = byRef.get(ref); | ||
| if (!former) return []; | ||
| add(former, "parent"); | ||
| const formerChain: ProjectPoolItem[] = [former]; | ||
| const formerSeen = new Set<RefKey>([item.ref, former.ref]); | ||
| let oldCursor = former.parentRef ? byRef.get(former.parentRef) : undefined; | ||
| while (oldCursor && !formerSeen.has(oldCursor.ref)) { | ||
| formerChain.push(oldCursor); | ||
| formerSeen.add(oldCursor.ref); | ||
| add(oldCursor, "parent"); | ||
| oldCursor = oldCursor.parentRef ? byRef.get(oldCursor.parentRef) : undefined; | ||
| } | ||
| return formerChain; | ||
| }); | ||
|
|
||
| const nearestEpic = chain.find((entry) => entry.kind === "Epic"); | ||
| const primarySlug = labelTarget(item.labels ?? [], "primary-parent/"); | ||
| const primaryEpic = primarySlug | ||
| ? pool.find((entry) => (entry.labels ?? []).includes(`epic/${primarySlug}`) && entry.kind === "Epic") | ||
| : undefined; | ||
| add(primaryEpic ?? nearestEpic, "primary-parent"); | ||
|
|
||
| const design = chain.find((entry) => (entry.labels ?? []).includes("kind/design") || entry.kind === "Design") ?? | ||
| (() => { | ||
| const designLabel = labelTarget(item.labels ?? [], "design/"); | ||
| return designLabel | ||
| ? pool.find((entry) => | ||
| (entry.labels ?? []).includes(`design/${designLabel}`) && | ||
| ((entry.labels ?? []).includes("kind/design") || entry.kind === "Design"), | ||
| ) | ||
| : undefined; | ||
| })(); | ||
| add(design, "spawned-by"); | ||
|
|
||
| const roadmapItem = chain.find((entry) => (entry.labels ?? []).includes("kind/roadmap-item") || entry.kind === "Roadmap item"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a roadmap row's Useful? React with 👍 / 👎. |
||
| add(roadmapItem, "roadmap"); | ||
| const priority = roadmapItem | ||
| ? chain.find((entry) => entry.ref !== roadmapItem.ref && ((entry.labels ?? []).includes("kind/roadmap-item") || entry.kind === "Roadmap item")) | ||
| : undefined; | ||
| add(priority, "priority"); | ||
|
|
||
| const releaseItem = priority ?? roadmapItem; | ||
| const release = releaseItem?.milestone | ||
| ? { title: releaseItem.milestone.title, dueOn: releaseItem.milestone.dueOn, ref: releaseItem.ref } | ||
| : null; | ||
|
Comment on lines
+89
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the selected roadmap or priority issue carries a rolling program milestone, this condition exposes it as a release in Useful? React with 👍 / 👎. |
||
| const afterRefs = [...new Set((roadmapItem ?? priority ?? item).blockedBy ?? [])]; | ||
| const epicPhrase = nearestEpic ? `as part of epic ${nearestEpic.title} (${nearestEpic.ref})` : "with no epic yet"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an issue has a valid Useful? React with 👍 / 👎. |
||
| const designPhrase = design ? `, spawned by design ${design.title} (${design.ref})` : ", with no design yet"; | ||
| const roadmapPhrase = roadmapItem ? `; on roadmap item ${roadmapItem.title} (${roadmapItem.ref})` : "; with no roadmap item yet"; | ||
| const priorityPhrase = priority ? `, overall priority ${priority.title} (${priority.ref})` : ""; | ||
| const releasePhrase = release ? `; release ${release.title}${release.dueOn ? ` due ${release.dueOn}` : ""}` : ""; | ||
| const historicalPhrase = historicalParents.length | ||
| ? ` Formerly part of ${historicalParents.map((former) => `${former.title} (${former.ref})`).join(" → ")}.` | ||
| : ""; | ||
| return { | ||
| ancestors, | ||
| release, | ||
| afterRefs, | ||
| sentence: `Working on ${item.title} ${epicPhrase}${designPhrase}${roadmapPhrase}${priorityPhrase}${releasePhrase}.${historicalPhrase}`, | ||
| }; | ||
| } | ||
|
|
||
| /** Attach genealogy overlay fields to legacy Grist/repo roadmap rows when their links resolve. */ | ||
| export function enrichRoadmapRows(rows: readonly RoadmapRow[], pool: readonly ProjectPoolItem[]) { | ||
| const byRef = new Map(pool.map((item) => [item.ref, item])); | ||
| return rows.map((row) => { | ||
| const linked = [...row.links].sort((a, b) => Number(b.relation === "tracks") - Number(a.relation === "tracks")) | ||
| .map((link) => byRef.get(link.ref)).find((item): item is ProjectPoolItem => Boolean(item)); | ||
| const genealogy = linked ? deriveGenealogy(linked, pool) : null; | ||
| const roadmapRef = genealogy?.ancestors.find((ancestor) => ancestor.relation === "roadmap")?.ref; | ||
| const priorityRef = genealogy?.ancestors.find((ancestor) => ancestor.relation === "priority")?.ref ?? roadmapRef; | ||
| return { | ||
| ...row, | ||
| priority: priorityRef ?? null, | ||
| afterRefs: genealogy?.afterRefs ?? [], | ||
| releaseRef: genealogy?.release?.ref ?? null, | ||
| }; | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { deriveGenealogy, enrichRoadmapRows } from "../src/genealogy.ts"; | ||
| import { historicalParentsFromMetadata } from "../src/projects.ts"; | ||
| import type { ProjectPoolItem, RefKey, RoadmapRow } from "../src/model.ts"; | ||
|
|
||
| const ref = (n: number) => `developing-today/code#${n}` as RefKey; | ||
| function item(n: number, partial: Partial<ProjectPoolItem> = {}): ProjectPoolItem { | ||
| return { | ||
| id: `PVTI_${n}`, url: "https://example.test/board", projectUrl: "https://example.test/board", | ||
| ref: ref(n), repo: "developing-today/code", number: n, | ||
| issueUrl: `https://github.com/developing-today/code/issues/${n}`, title: `Item ${n}`, | ||
| state: "open", contentKind: "issue", status: null, area: null, horizon: null, kind: null, | ||
| ...partial, | ||
| }; | ||
| } | ||
|
|
||
| describe("genealogy read model", () => { | ||
| test("derives the verified parent, epic, roadmap, priority, release and blocker chain", () => { | ||
| const pool = [ | ||
| item(1, { title: "Overall priority", kind: "Roadmap item", labels: ["kind/roadmap-item"], milestone: { title: "Release 1", dueOn: "2026-12-01", closedAt: null } }), | ||
| item(2, { title: "Project roadmap", kind: "Roadmap item", labels: ["kind/roadmap-item"], parentRef: ref(1), blockedBy: [ref(9)] }), | ||
| item(3, { title: "Design", kind: "Design", labels: ["kind/design", "design/crate-split"], parentRef: ref(2) }), | ||
| item(4, { title: "Epic", kind: "Epic", labels: ["epic/crate-split"], parentRef: ref(3) }), | ||
| item(5, { title: "Task", parentRef: ref(4), labels: ["design/crate-split"] }), | ||
| ]; | ||
| const result = deriveGenealogy(pool[4]!, pool); | ||
| expect(result.ancestors.map(({ ref: r, relation }) => [r, relation])).toEqual([ | ||
| [ref(4), "parent"], [ref(3), "parent"], [ref(2), "parent"], [ref(1), "parent"], | ||
| [ref(4), "primary-parent"], [ref(3), "spawned-by"], [ref(2), "roadmap"], [ref(1), "priority"], | ||
| ]); | ||
| expect(result.release).toEqual({ title: "Release 1", dueOn: "2026-12-01", ref: ref(1) }); | ||
| expect(result.afterRefs).toEqual([ref(9)]); | ||
| expect(result.sentence).toContain("Working on Task as part of epic Epic"); | ||
| expect(result.sentence).toContain("release Release 1 due 2026-12-01"); | ||
| }); | ||
|
|
||
| test("renders chain breaks honestly and enriches roadmap rows with known overlay refs", () => { | ||
| const lone = item(7, { title: "Orphan" }); | ||
| const result = deriveGenealogy(lone, [lone]); | ||
| expect(result.sentence).toContain("no epic yet"); | ||
| expect(result.sentence).toContain("no roadmap item yet"); | ||
| const row = { id: 1, subject: "grist:x/y/1", title: "Roadmap", area: "ID", status: "Now", horizon: "Now", source: "test", notes: "", links: [{ ref: ref(7), relation: "tracks" }] } satisfies RoadmapRow; | ||
| expect(enrichRoadmapRows([row], [lone])[0]).toMatchObject({ priority: null, afterRefs: [], releaseRef: null }); | ||
| }); | ||
|
|
||
| test("reconstructs the design's crate-split worked chain from its real refs", () => { | ||
| const pool = [ | ||
| item(2056, { title: "ID program", kind: "Epic", labels: ["epic/id-program"] }), | ||
| item(3195, { title: "Crate split design", kind: "Design", labels: ["kind/design", "design/id-crate-split"], parentRef: ref(2056) }), | ||
| item(3447, { title: "Crate split epic", kind: "Epic", labels: ["epic/id-crate-split", "design/id-crate-split"], parentRef: ref(3195) }), | ||
| item(4403, { title: "First crate-split step", parentRef: ref(3447), labels: ["design/id-crate-split"] }), | ||
| ]; | ||
| const result = deriveGenealogy(pool[3]!, pool); | ||
| expect(result.ancestors.map((ancestor) => ancestor.ref)).toContain(ref(3195)); | ||
| expect(result.ancestors.map((ancestor) => ancestor.ref)).toContain(ref(3447)); | ||
| expect(result.ancestors.map((ancestor) => ancestor.ref)).toContain(ref(2056)); | ||
| expect(result.sentence).toContain("Crate split epic"); | ||
| expect(result.sentence).not.toContain("Formerly part of"); | ||
| }); | ||
|
|
||
| test("renders a historical split parent only when canonical metadata marks it historical", () => { | ||
| const body = `<!-- pm-metadata:start -->\n\`\`\`json\n{"pm_meta":1,"links":[{"target":{"repo":"developing-today/code","number":99},"relation":"parent","status":"historical","since":"2026-11-02","until":"2026-11-12","reason":"split"},{"target":{"repo":"developing-today/code","number":98},"relation":"parent","status":"removed"}]}\n\`\`\`\n<!-- pm-metadata:end -->`; | ||
| const refs = historicalParentsFromMetadata(body); | ||
| expect(refs).toEqual([ref(99)]); | ||
| const former = item(99, { title: "Former epic" }); | ||
| const current = item(100, { title: "Split task", historicalParentRefs: refs }); | ||
| expect(deriveGenealogy(current, [current, former]).sentence).toContain("Formerly part of Former epic (developing-today/code#99)"); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a native ancestor is a design tracking issue marked with the existing
designlabel rather thankind/designor Project KindDesign, this predicate skips it and the label-based fallback applies the same exclusion. The hierarchy design's §2 defines tracking issues usingdesignplusdesign/<name>, andpm-stack/design-register-2026-11-11.mdrecords tracking issues #9539, #9521, #9399 and #8884 on Project #4 with the plaindesignlabel. Their descendants can therefore receive “with no design yet” and nospawned-byancestor despite a known design in the native chain. Recognize the existing tracking marker in both lookups without treating every issue carrying a design-family membership label as a tracking issue.Useful? React with 👍 / 👎.