diff --git a/.changeset/20980-book-tree-orphans-scoped.md b/.changeset/20980-book-tree-orphans-scoped.md new file mode 100644 index 00000000000..25640ad065d --- /dev/null +++ b/.changeset/20980-book-tree-orphans-scoped.md @@ -0,0 +1,12 @@ +--- +'@objectstack/spec': patch +--- + +fix(spec): a book tree's synthetic *Uncategorized* group holds only the unplaced docs of the book's own packages, per ADR-0046 §6.4 + +Clause-②: no + +- `resolveBookTree` used to put every unclaimed doc it was handed into the book's *Uncategorized* group. `GET /api/v1/meta/book/:name/tree` resolves over every doc in the environment, so a book's tree listed every other package's ungrouped docs there. The docs portal never showed those docs in the book. +- The group now holds a doc only when it belongs to one of the book's packages: the package that ships the book, or a package a group names with `package`. A doc with no stamped package still counts as the book's. A doc of another package stays reachable through its own package's book. +- The implicit per-package book that the tree route serves for a package id catches no other package's docs either. +- Unchanged: a doc whose `group` names one of the book's groups joins that group from any package. A book that declares no package keeps every unclaimed doc in *Uncategorized*. The docs a book claims, and so every doc's audience, do not change. diff --git a/content/docs/ui/doc-pages.mdx b/content/docs/ui/doc-pages.mdx index 9824a2db94c..601777e831a 100644 --- a/content/docs/ui/doc-pages.mdx +++ b/content/docs/ui/doc-pages.mdx @@ -180,8 +180,11 @@ Precisely what derives it, in the order it runs: First claim wins, so a doc never appears twice. 3. **Within a group, docs sort by `doc.order`, then by label** (falling back to the doc name). -4. **Anything claimed by nobody is appended last** in a synthetic *Uncategorized* group. - Nothing is ever dropped. +4. **Any doc of the book's own packages that nobody claims is appended last** in a + synthetic *Uncategorized* group, so none of the book's docs is ever dropped. The + book's packages are the one that ships it plus any package a group names with + `package`. A doc of another package is not this book's to catch: it appears in its own + package's book. A book that names no package at all collects every unclaimed doc here. So the AI-authoring property the design was built for holds: create a doc whose name matches a rule and it files itself. There is no central array to read, modify and write diff --git a/packages/rest/src/meta-app-nav-doc-audience.test.ts b/packages/rest/src/meta-app-nav-doc-audience.test.ts index 290594d7c60..f5cd8511c2f 100644 --- a/packages/rest/src/meta-app-nav-doc-audience.test.ts +++ b/packages/rest/src/meta-app-nav-doc-audience.test.ts @@ -237,10 +237,11 @@ describe('[#19790] a `book` entry is judged on its readable PAGES', () => { // The implicit `ops` book is `org` — the tree read opens (200)… const tree = await bookTree(rest, 'ops'); expect(tree.statusCode).toBe(200); - // …and serves `crm_intro` in the synthetic Uncategorized group only. The - // spec calls those orphans "not an authored membership claim", so they - // are not the book's pages, and the entry is still dropped. - expect(tree.body.groups.map((g: any) => g.key)).toEqual(['uncategorized']); + // …and serves no group at all: both its pages are gated, and + // `crm_intro` is the `crm` package's doc, so it is not this book's + // orphan either (#20980, ADR-0046 §6.4 — the synthetic Uncategorized + // group holds only the book's own packages' unplaced docs). + expect(tree.body.groups.map((g: any) => g.key)).toEqual([]); }); }); diff --git a/packages/spec/src/system/book.test.ts b/packages/spec/src/system/book.test.ts index f78d9227d0d..746cc6bacba 100644 --- a/packages/spec/src/system/book.test.ts +++ b/packages/spec/src/system/book.test.ts @@ -182,7 +182,83 @@ describe('resolveBookTree — derived membership (the AI-safety core)', () => { { name: 'b', packageId: 'other' }, ]); expect(tree.groups[0].entries.map((e) => e.doc)).toEqual(['a']); - expect(tree.groups.at(-1)!.key).toBe('uncategorized'); // 'b' falls through + // 'b' is another package's doc: not this book's orphan either (#20980, + // ADR-0046 §6.4), so no Uncategorized group is appended for it. + expect(tree.groups.map((g) => g.key)).toEqual(['g']); + }); +}); + +// [#20980] ADR-0046 §6.4: "any doc left unplaced rolls up under a synthetic +// *Uncategorized* group" — the unplaced docs are the PACKAGE's. The orphan +// pass used to sweep every doc handed in, so `GET /meta/book/:name/tree` +// (which resolves over an env-wide corpus) filed every other package's +// ungrouped docs under this book's Uncategorized group. +describe('resolveBookTree — the Uncategorized group holds only the book\'s own packages\' docs (§6.4)', () => { + const book: Book = { name: 'crm_guide', groups: [{ key: 'start', label: 'Start', include: 'crm_intro' }] }; + const keysAndDocs = (tree: ResolvedBook) => + Object.fromEntries(tree.groups.map((g) => [g.key, g.entries.map((e) => e.doc)])); + + it('another package\'s ungrouped doc is NOT in the book\'s Uncategorized group; the book\'s own is', () => { + const tree = resolveBookTree(book, [ + { name: 'crm_intro', packageId: 'crm' }, + { name: 'crm_stray', packageId: 'crm' }, + { name: 'ops_keys', packageId: 'ops' }, + ], 'crm'); + expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro'], uncategorized: ['crm_stray'] }); + }); + + it('control: a cross-package doc whose `group` names the book\'s group still joins it (placement is unscoped)', () => { + const tree = resolveBookTree(book, [ + { name: 'crm_intro', packageId: 'crm' }, + { name: 'ops_placed', packageId: 'ops', group: 'start' }, + { name: 'ops_keys', packageId: 'ops' }, + ], 'crm'); + expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro', 'ops_placed'] }); + }); + + it('a group\'s `package` is one of the book\'s packages: that package\'s unmatched doc is an orphan here', () => { + const crossBook: Book = { + name: 'crm_guide', + groups: [{ key: 'ops', label: 'Operations', include: 'ops_runbook_*', package: 'ops' }], + }; + const tree = resolveBookTree(crossBook, [ + { name: 'crm_stray', packageId: 'crm' }, + { name: 'ops_runbook_keys', packageId: 'ops' }, + { name: 'ops_other', packageId: 'ops' }, + { name: 'hr_policy', packageId: 'hr' }, + ], 'crm'); + expect(keysAndDocs(tree)).toEqual({ ops: ['ops_runbook_keys'], uncategorized: ['crm_stray', 'ops_other'] }); + }); + + it('the implicit package book (no `bookPackage`, the tree route\'s fallback) catches no other package\'s doc', () => { + const tree = resolveBookTree(deriveImplicitPackageBook('crm', 'CRM'), [ + { name: 'crm_intro', packageId: 'crm' }, + { name: 'ops_keys', packageId: 'ops' }, + ]); + expect(keysAndDocs(tree)).toEqual({ all: ['crm_intro'] }); + }); + + it('no package declared anywhere: every unclaimed doc is an orphan, as before', () => { + const tree = resolveBookTree(book, [ + { name: 'crm_intro', packageId: 'crm' }, + { name: 'crm_stray', packageId: 'crm' }, + { name: 'ops_keys', packageId: 'ops' }, + ]); + expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro'], uncategorized: ['crm_stray', 'ops_keys'] }); + }); + + it('a doc with no stamped `packageId` is in every scope — the same reading `include` scoping uses', () => { + const tree = resolveBookTree(book, [{ name: 'crm_intro', packageId: 'crm' }, { name: 'loose_note' }], 'crm'); + expect(keysAndDocs(tree)).toEqual({ start: ['crm_intro'], uncategorized: ['loose_note'] }); + }); + + it('the claim set is unchanged: neither the own nor the foreign orphan is claimed', () => { + const corpus: ResolverDoc[] = [ + { name: 'crm_intro', packageId: 'crm' }, + { name: 'crm_stray', packageId: 'crm' }, + { name: 'ops_keys', packageId: 'ops' }, + ]; + expect([...resolveBookClaimedDocs(book, corpus, 'crm')]).toEqual(['crm_intro']); }); }); diff --git a/packages/spec/src/system/book.zod.ts b/packages/spec/src/system/book.zod.ts index 3b6949fcc13..a3e0522bd71 100644 --- a/packages/spec/src/system/book.zod.ts +++ b/packages/spec/src/system/book.zod.ts @@ -295,8 +295,16 @@ function entryFromDoc(doc: ResolverDoc): ResolvedEntry { * - Otherwise membership is derived: a doc joins the first group (in group * order) whose `include` matches it OR whose `key` equals the doc's explicit * `group`. Within a group, docs sort by `doc.order` then label. - * - Any doc claimed by no group falls into a synthetic *Uncategorized* group - * appended last — nothing is ever dropped. + * - Any doc OF THE BOOK'S PACKAGES claimed by no group falls into a + * synthetic *Uncategorized* group appended last — no doc of the book is + * ever dropped (ADR-0046 §6.4: "any doc left unplaced rolls up under a + * synthetic *Uncategorized* group", the unplaced docs being the + * package's). The book's packages are `bookPackage` plus every group's + * `package`; a doc of any OTHER package is not this book's to catch — it + * stays reachable through its own package's book. When the book declares + * no package at all, every unclaimed doc is an orphan, as before. + * Explicit placement is NOT scoped: a doc of another package whose + * `group` names one of this book's groups still joins that group. */ export function resolveBookTree(book: Book, docs: ResolverDoc[], bookPackage?: string): ResolvedBook { const groupsSorted = [...book.groups] @@ -373,8 +381,20 @@ export function resolveBookTree(book: Book, docs: ResolverDoc[], bookPackage?: s resolvedGroups.push({ key: group.key, label: group.label, entries }); } - // Orphans: docs claimed by no group. - const orphans = docs.filter((d) => !claimed.has(d.name)).sort(byOrderThenLabel); + // Orphans: docs of the book's own packages claimed by no group (ADR-0046 + // §6.4). "Of the book's packages" is asked through `matchesInclude` with + // the catch-all rule `'*'` — the implicit package book's own rule + // (`deriveImplicitPackageBook`) — so the orphan pass and `include` share + // ONE reading of a doc's package: its stamped `packageId`, and a doc with + // none is in every scope. No package declared ⇒ no scope, every + // unclaimed doc is an orphan. + const ownPackages = [bookPackage, ...book.groups.map((g) => g.package)].filter( + (p): p is string => typeof p === 'string' && p.length > 0, + ); + const orphans = docs + .filter((d) => !claimed.has(d.name)) + .filter((d) => ownPackages.length === 0 || ownPackages.some((p) => matchesInclude(d, '*', p))) + .sort(byOrderThenLabel); if (orphans.length) { resolvedGroups.push({ key: UNCATEGORIZED_KEY,