diff --git a/README.md b/README.md index 727dc82..276cc4f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,8 @@ delete an entity that tombstones point to, or one that is the only `applies-to` silently become "everywhere"); the offending preferences/tombstones are listed. `sirmem lint --clean` applies structural fixes only and prints each change: removes unreferenced redirect -tombstones, `relates-to` lines whose pair is also linked by a typed verb, and `part-of` lines implied by a +tombstones, `relates-to` lines whose pair is also linked by a typed verb (a non-empty note is appended to the first typed +relation's note, separated by ` | `), and `part-of` lines implied by a longer `part-of` chain. `sirmem lint --suggest` is read-only and prints numbered candidates (superseded, contradicted, duplicate by normalised label/alias, orphan) with a suggested action for an LLM to review with the user. Both flags can be combined (clean first, then lint, then suggest). diff --git a/src/sirmem/core.nim b/src/sirmem/core.nim index c3da52f..f429dd6 100644 --- a/src/sirmem/core.nim +++ b/src/sirmem/core.nim @@ -6,7 +6,7 @@ import std/[os, strutils, sequtils, algorithm, tables, times, osproc, nativesockets, deques] const - Version* = "0.1.3" + Version* = "0.1.4" CycleVerbs* = ["part-of", "supersedes"] DefaultVerb* = "relates-to" @@ -755,7 +755,7 @@ proc cmdLint*(st: Store, errorCount: var int): string = used.add r.s used.add r.o for id, e in st.entities: - if e.redirect == "" and id notin used: warns.add "orphan entity: " & id + if e.redirect == "" and e.typ != "preference" and id notin used: warns.add "orphan entity: " & id if e.fileId != id: warns.add "entity id " & id & " differs from filename " & e.fileId & ".md" if not st.schema.hasType(e.typ) and e.redirect == "": warns.add "entity " & id & " has type not in schema: " & e.typ @@ -772,15 +772,30 @@ proc cmdLintClean*(st: var Store): string = var lines: seq[string] var rels = st.rels # b. relates-to implied by any other verb on the same pair (either direction) - var keep: seq[Rel] + var keep: seq[bool] for r in rels: - if r.v == DefaultVerb and rels.anyIt(it.v != DefaultVerb and - ((it.s == r.s and it.o == r.o) or (it.s == r.o and it.o == r.s))): - lines.add "removed relation " & r.s & " " & r.v & " " & r.o & suffix(r.note) & - " (pair already linked by a typed verb)" - else: - keep.add r - rels = keep + keep.add not (r.v == DefaultVerb and rels.anyIt(it.v != DefaultVerb and + ((it.s == r.s and it.o == r.o) or (it.s == r.o and it.o == r.s)))) + for idx in 0 ..< rels.len: + if keep[idx]: continue + let r = rels[idx] + var target = -1 + for j in 0 ..< rels.len: + let t = rels[j] + if t.v != DefaultVerb and ((t.s == r.s and t.o == r.o) or (t.s == r.o and t.o == r.s)): + if target < 0 or (t.s, t.v, t.o) < (rels[target].s, rels[target].v, rels[target].o): + target = j + var msg = "removed relation " & r.s & " " & r.v & " " & r.o & suffix(r.note) & + " (pair already linked by a typed verb)" + if r.note != "" and target >= 0: + let t = rels[target] + rels[target].note = if t.note == "": r.note else: t.note & " | " & r.note + msg.add "; note appended to " & t.s & " " & t.v & " " & t.o + lines.add msg + var kept: seq[Rel] + for idx in 0 ..< rels.len: + if keep[idx]: kept.add rels[idx] + rels = kept # c. transitively redundant part-of (sequential, so reachability is always preserved) var i = 0 while i < rels.len: @@ -794,7 +809,7 @@ proc cmdLintClean*(st: var Store): string = rels = others continue inc i - if rels.len != st.rels.len: + if rels != st.rels: st.rels = rels st.saveRels # a. unreferenced redirect tombstones, repeated until stable diff --git a/tests/test_all.nim b/tests/test_all.nim index 28d22e3..9f317c4 100644 --- a/tests/test_all.nim +++ b/tests/test_all.nim @@ -349,6 +349,8 @@ suite "lint": check "warning: domain/range" in out1 check "warning: orphan entity: d" in out1 check run(d, "lint") == out1 and lastCode == 0 + discard run(d, "new", "pref9", "preference") + check "orphan entity: pref9" notin run(d, "lint") # break things by hand writeFile(d / "relations.tsv", "a\tbogus\tb\n" & @@ -405,6 +407,21 @@ suite "rm guards": check fileExists(d / "entities" / "b.md") suite "lint --clean / --suggest": + test "clean appends removed relates-to note to the first typed relation": + let d = freshDir() + seed(d) + discard run(d, "link", "a", "uses", "b", "first") + discard run(d, "link", "b", "part-of", "a", "second") + discard run(d, "link", "a", "relates-to", "b", "extra") + let o = run(d, "lint", "--clean") + check "note appended to a uses b" in o + check "a\tuses\tb\tfirst | extra" in relsText(d) + check "relates-to" notin relsText(d) + discard run(d, "link", "c", "uses", "d") + discard run(d, "link", "c", "relates-to", "d", "n2") + discard run(d, "lint", "--clean") + check "c\tuses\td\tn2" in relsText(d) + test "clean removes tombstones, redundant relates-to and part-of; prints changes": let d = freshDir() seed(d)