Skip to content

Keep repeated backlinks in source order#688

Closed
srid wants to merge 4 commits intomasterfrom
fix/backlink-context-order
Closed

Keep repeated backlinks in source order#688
srid wants to merge 4 commits intomasterfrom
fix/backlink-context-order

Conversation

@srid
Copy link
Copy Markdown
Owner

@srid srid commented Apr 30, 2026

Repeated backlinks now render in the order they appear in Markdown. A note that links to the same target several times now carries source occurrence order through relation extraction and backlink grouping, so the rendered context list matches the original note. Closes #186.

The fix makes the ordering contract explicit instead of relying on IxSet iteration or derived record ordering:

  • extract link occurrences in document order using the existing Pandoc inline-ref parser
  • store each relation occurrence with a RelOrder
  • sort backlink contexts by a named source-order key before grouping them for rendering
  • cover both noteRels and modelLookupBacklinks with focused regressions

Validation

nix develop -c cabal test test --test-options='--match /Emanote.Model.Graph/ --match /Emanote.Model.Link.Rel/noteRels/'

Try it locally

nix run github:srid/emanote/fix/backlink-context-order

Generated by /do on Codex (model gpt-5).

srid added 4 commits April 30, 2026 07:47
Keep backlink rels ordered by their source occurrence so repeated links from one note render in Markdown order. Closes #186.
Address review finding: backlink source order should be named at the backlink boundary instead of riding Rel field order and IxSet iteration.
Address review finding: replace the raw link occurrence tuple with a named representation and reuse the shared Pandoc inline-ref parser.
Address code-police readability rule by documenting the new exported relation ordering helpers and link occurrence type.
@srid
Copy link
Copy Markdown
Owner Author

srid commented Apr 30, 2026

Hickey/Lowy Analysis

# Lens Finding Disposition
1 Hickey relOrder hides sort contract Fixed in this PR
2 Hickey Link occurrence lacks representation Fixed in this PR
3 Lowy Backlink order rides Ord Fixed in this PR

Hickey rationale

The first implementation preserved source order by putting _relOrder before _relTo in Rel, which made the behavior depend on derived Ord and record field position. That was split from the ordering contract reviewers need to understand. The follow-up commit introduced RelOrder, an explicit Ord Rel, and a named relSourceOrder key.

The extraction path also carried a raw (url, attrs, context) tuple through multiple helpers. That tuple represented one domain concept, so the follow-up commit replaced it with LinkOccurrence and reused Emanote.Pandoc.Link.parseInlineRef for Pandoc link parsing.

Lowy rationale

Backlink ordering is user-visible behavior, so the boundary that assembles backlinks should own the ordering decision. The original patch made this behavior ride on low-level Rel representation and IxSet iteration. The follow-up change sorts repeated backlink rels by Rel.relSourceOrder before grouping, and adds a regression at modelLookupBacklinks so the tested boundary matches the rendered behavior.

@srid
Copy link
Copy Markdown
Owner Author

srid commented Apr 30, 2026

Evidence

Foo backlink contexts in source order

Captured from http://127.0.0.1:21720/foo using a scratch notebook where source.md links to foo in this order: b, a, c.

Verified the Links to this page section renders backlink contexts in the same source order: b, a, c.

@srid
Copy link
Copy Markdown
Owner Author

srid commented Apr 30, 2026

/do results

Step Status Duration Verification
sync 1s git fetch ok; forge=github; noGit=false
research 5m 39s Issue #186 maps to Emanote.Model.Link.Rel.noteRels and Emanote.Model.Graph.modelLookupBacklinks; rel extraction/storage lacks source occurrence order, causing repeated backlink contexts to render out of Markdown order.
branch 0s Created feature branch fix/backlink-context-order from origin/master.
implement 4m 3s Implemented ordered rel extraction with per-note occurrence indices, added regression for repeated [[Foo]] backlink contexts, removed unused pandoc-link-context dependency, and updated CHANGELOG.
check 19s nix develop -c cabal build all passed (bare cabal not on PATH, same configured command run inside project dev shell).
docs 1s CHANGELOG.md updated under Unreleased bug fixes; README.md and docs/ need no changes for this internal backlink-order fix.
fmt 18s just fmt passed after fourmolu formatted the changed Haskell files.
commit 8s Committed 2e77aff and pushed origin/fix/backlink-context-order.
hickey+lowy 9m 55s Hickey and Lowy reviews completed. Fixed Lowy/Hickey ordering-contract finding in 61fb989 and Hickey link-occurrence representation finding in ed0412d; no deferred findings.
police 1m 24s code-police clean after fixing readability doc comments in 8309c54; rules, fact-check, and elegance passes found no remaining issues.
test 8s Focused Haskell tests passed: Emanote.Model.Graph modelLookupBacklinks and Emanote.Model.Link.Rel noteRels regressions for issue #186.
create-pr 50s Draft PR #688 created and Hickey/Lowy analysis comment posted; no deferred findings.
ci 4m 4s vira ci passed for HEAD 8309c54; signoffs vira/aarch64-darwin and vira/x86_64-linux succeeded.
evidence 1m 48s Posted ## Evidence comment with screenshot asset: #688 (comment)
done 0s All /do steps passed; final status report prepared.
Total 31m 40s

Slowest step: hickey+lowy (9m 55s)

Optimization suggestions

  • hickey+lowy dominated the run at 9m 55s. For tiny, low-risk edits, $do --minimal would skip those reviews; for model/rendering behavior like this one, keeping the default was worthwhile because it produced the explicit ordering contract commits.
  • research took 5m 39s. A prompt that names suspected modules such as Emanote.Model.Link.Rel and Emanote.Model.Graph, or includes a minimal note pair, would trim the initial mapping time.
  • ci took 4m 4s and passed on the first run. For future follow-up-only validation, resuming from the CI step after the branch is already pushed avoids repeating earlier workflow work.

Workflow completed at 2026-04-30T12:08:29Z.

@srid
Copy link
Copy Markdown
Owner Author

srid commented Apr 30, 2026

#692

@srid srid closed this Apr 30, 2026
@srid srid deleted the fix/backlink-context-order branch April 30, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Individual backlinks not in right order

1 participant