fix(anatomy): B/C changes leave every touched finding and annotation at to_confirm (applier rule, reviewed refused), typed test reads, docs numbers
This commit is contained in:
1 parent
37550c29a5
commit
6034909674
10 files changed
+776
-327
No files matched your search
@@ -6,8 +6,14 @@ import {
|
||||
type SheetIssue,
|
||||
} from '@faunosteo/domain'
|
||||
import { applyOp, assertDeclared } from './ops'
|
||||
import { changeSummary, type Change, type Changeset } from './schema'
|
||||
import type { Item } from './targets'
|
||||
import {
|
||||
changeSummary,
|
||||
needsConfirmation,
|
||||
type Change,
|
||||
type ChangeSummary,
|
||||
type Changeset,
|
||||
} from './schema'
|
||||
import { allFindings, topCollection, type Item } from './targets'
|
||||
import type { ChangeRecord, CurationResult } from './types'
|
||||
|
||||
/**
|
||||
@@ -16,8 +22,56 @@ import type { ChangeRecord, CurationResult } from './types'
|
||||
* registry first (it is the source of truth). Any op that cannot apply
|
||||
* exactly as written, and any integrity error left at the end, throws:
|
||||
* a curated document is never produced from a partially applied changeset.
|
||||
*
|
||||
* Review rule: a level B or C change never leaves what it touched
|
||||
* (findings, finding categories, annotations: every item a set, add, cite,
|
||||
* rename or move resolved to, whole added subtrees and subtree scopes
|
||||
* included) as inherited or reviewed. After its ops, the applier sets
|
||||
* `review_status = 'to_confirm'` on each one and records that edit under the
|
||||
* same change id. An explicit 'reviewed' in such a change throws. Level A
|
||||
* is untouched by the rule.
|
||||
*/
|
||||
|
||||
/** Findings and annotations of the document, by item, with their target. */
|
||||
function reviewable(doc: Item): Map<Item, string> {
|
||||
const map = new Map<Item, string>()
|
||||
for (const { item, target } of allFindings(doc)) map.set(item, target)
|
||||
for (const item of topCollection(doc, 'annotation')) {
|
||||
map.set(item, `annotation:${String(item.id)}`)
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
/** Marks every touched item still in the document 'to_confirm'. */
|
||||
function markToConfirm(
|
||||
doc: Item,
|
||||
change: ChangeSummary,
|
||||
touched: readonly Item[],
|
||||
records: ChangeRecord[],
|
||||
) {
|
||||
const targets = reviewable(doc)
|
||||
for (const item of new Set(touched)) {
|
||||
const target = targets.get(item)
|
||||
if (target === undefined || item.review_status === 'to_confirm') continue
|
||||
const before = item.review_status ?? 'inherited'
|
||||
item.review_status = 'to_confirm'
|
||||
const op = {
|
||||
op: 'set',
|
||||
target,
|
||||
field: 'review_status',
|
||||
value: 'to_confirm',
|
||||
} as const
|
||||
records.push({
|
||||
change,
|
||||
op,
|
||||
target,
|
||||
field: 'review_status',
|
||||
before,
|
||||
after: 'to_confirm',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function applyChange(doc: Item, change: Change, records: ChangeRecord[]) {
|
||||
const summarised = changeSummary(change)
|
||||
try {
|
||||
@@ -25,13 +79,17 @@ function applyChange(doc: Item, change: Change, records: ChangeRecord[]) {
|
||||
} catch (error) {
|
||||
throw new Error(`${change.id}: ${(error as Error).message}`)
|
||||
}
|
||||
const touched: Item[] = []
|
||||
change.ops.forEach((op, index) => {
|
||||
try {
|
||||
applyOp({ doc, change: summarised, op, records })
|
||||
applyOp({ doc, change: summarised, op, records, touched })
|
||||
} catch (error) {
|
||||
throw new Error(`${change.id} op ${index}: ${(error as Error).message}`)
|
||||
}
|
||||
})
|
||||
if (needsConfirmation(change.level)) {
|
||||
markToConfirm(doc, summarised, touched, records)
|
||||
}
|
||||
}
|
||||
|
||||
function describe(issues: readonly { path: string; message: string }[]) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { isDeepStrictEqual } from 'node:util'
|
||||
import { fieldKind, isSettable, readField, writeField } from './fields'
|
||||
import { propagateRename } from './rename'
|
||||
import type { Op, Position } from './schema'
|
||||
import { needsConfirmation, type Op, type Position } from './schema'
|
||||
import {
|
||||
childList,
|
||||
childPrefix,
|
||||
@@ -57,8 +57,20 @@ function unknownField(op: OpOf<'set'>): Error {
|
||||
return new Error(`unknown field "${op.field}" on "${op.target}"`)
|
||||
}
|
||||
|
||||
/** A B or C change leaves findings to the professional: no 'reviewed'. */
|
||||
function assertNotReviewed(ctx: OpContext, op: OpOf<'set'>) {
|
||||
if (op.field !== 'review_status' || op.value !== 'reviewed') return
|
||||
if (needsConfirmation(ctx.change.level)) {
|
||||
throw new Error(
|
||||
`level ${ctx.change.level} cannot set ` +
|
||||
`"${op.target}" review_status to "reviewed"`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function setSelf(ctx: OpContext, located: Located, op: OpOf<'set'>) {
|
||||
if (!isSettable(fieldKind(located.target), op.field)) throw unknownField(op)
|
||||
ctx.touched.push(located.item)
|
||||
const before = readField(located.item, op.field)
|
||||
writeField(located.item, op.field, op.value)
|
||||
record(ctx, op.target, op.field, { before, after: op.value })
|
||||
@@ -73,6 +85,7 @@ function setSubtree(ctx: OpContext, located: Located, op: OpOf<'set'>) {
|
||||
isSettable(fieldKind(located.target, entry.isCategory), op.field),
|
||||
)
|
||||
if (entries.length === 0) throw unknownField(op)
|
||||
ctx.touched.push(...entries.map((entry) => entry.item))
|
||||
for (const { target, item } of entries) {
|
||||
const before = readField(item, op.field)
|
||||
if (isDeepStrictEqual(before, op.value)) continue
|
||||
@@ -82,6 +95,7 @@ function setSubtree(ctx: OpContext, located: Located, op: OpOf<'set'>) {
|
||||
}
|
||||
|
||||
function applySet(ctx: OpContext, op: OpOf<'set'>): void {
|
||||
assertNotReviewed(ctx, op)
|
||||
const located = locate(ctx.doc, op.target)
|
||||
if ((op.scope ?? 'self') === 'self') setSelf(ctx, located, op)
|
||||
else setSubtree(ctx, located, op)
|
||||
@@ -95,6 +109,7 @@ function applyRename(ctx: OpContext, op: OpOf<'rename'>): void {
|
||||
const before = structuredClone(item)
|
||||
const from = String(item[key])
|
||||
item[key] = op.id
|
||||
ctx.touched.push(item)
|
||||
record(ctx, op.target, null, { before, after: item })
|
||||
propagateRename(ctx, { kind: target.kind, from, to: op.id })
|
||||
}
|
||||
@@ -118,6 +133,11 @@ function addDestination(ctx: OpContext, op: OpOf<'add'>): Destination {
|
||||
return { list, key: keyField(kind), prefix: `${kind}:` }
|
||||
}
|
||||
|
||||
function descendants(item: Item): Item[] {
|
||||
const children = Array.isArray(item.children) ? (item.children as Item[]) : []
|
||||
return children.flatMap((child) => [child, ...descendants(child)])
|
||||
}
|
||||
|
||||
function applyAdd(ctx: OpContext, op: OpOf<'add'>): void {
|
||||
if (!isItem(op.value)) throw new Error('add needs an object value')
|
||||
const dest = addDestination(ctx, op)
|
||||
@@ -127,7 +147,9 @@ function applyAdd(ctx: OpContext, op: OpOf<'add'>): void {
|
||||
}
|
||||
assertFree(dest.list, dest.key, id)
|
||||
const index = insertionIndex(dest.list, dest.key, op.position)
|
||||
dest.list.splice(index, 0, structuredClone(op.value))
|
||||
const added = structuredClone(op.value)
|
||||
dest.list.splice(index, 0, added)
|
||||
ctx.touched.push(added, ...descendants(added))
|
||||
record(ctx, dest.prefix + id, null, { before: null, after: op.value })
|
||||
}
|
||||
|
||||
@@ -153,6 +175,7 @@ function applyMove(ctx: OpContext, op: OpOf<'move'>): void {
|
||||
assertFree(list, 'id', id, source.item)
|
||||
source.siblings!.splice(source.siblings!.indexOf(source.item), 1)
|
||||
list.splice(insertionIndex(list, 'id', op.position), 0, source.item)
|
||||
ctx.touched.push(source.item)
|
||||
const after = childPrefix(parent) + id
|
||||
record(ctx, op.target, null, { before: source.item, after })
|
||||
}
|
||||
@@ -170,7 +193,9 @@ export function assertDeclared(doc: Item, ids: readonly string[]): void {
|
||||
function applyCite(ctx: OpContext, op: OpOf<'cite'>): void {
|
||||
assertDeclared(ctx.doc, op.reference_ids)
|
||||
const located = locate(ctx.doc, op.target)
|
||||
for (const { target, item } of findingScope(located, op.scope ?? 'self')) {
|
||||
const scope = findingScope(located, op.scope ?? 'self')
|
||||
ctx.touched.push(...scope.map((entry) => entry.item))
|
||||
for (const { target, item } of scope) {
|
||||
const before = Array.isArray(item.reference_ids) ? item.reference_ids : []
|
||||
const added = [...new Set(op.reference_ids)].filter(
|
||||
(id) => !before.includes(id),
|
||||
|
||||
@@ -27,6 +27,8 @@ export interface OpContext {
|
||||
change: ChangeSummary
|
||||
op: Op
|
||||
records: ChangeRecord[]
|
||||
/** Items the change edited, in order; the applier marks them at the end. */
|
||||
touched: Item[]
|
||||
}
|
||||
|
||||
/** Absent values are recorded as null so records stay plain JSON. */
|
||||
|
||||
Reference in new issue
Block a user