links: bind a [[link]] to a note, not to a string
CI & Build / Build now, or wait for Android? (push) Successful in 3s
CI & Build / Python lint (push) Successful in 4s
CI & Build / TypeScript typecheck (push) Successful in 7s
CI & Build / Python tests (push) Successful in 11s
CI & Build / Build & push image (push) Successful in 34s
Desktop (Tauri) / Windows installer (cross-compiled) (push) Successful in 2m21s
Desktop (Tauri) / Tauri desktop (Linux) (push) Successful in 4m18s
Desktop (Tauri) / Update manifest (push) Successful in 5s

A wiki-link was stored only as normalized TEXT, so a note's NAME was the edge.
Renaming it broke every inbound link — and the fix that shipped for that
(task 1848, option b) was `_rename_inbound_links`: rewrite the `[[Old Name]]`
text inside the body of every note that linked to the renamed one.

That works while an explicit title exists to hold still. It stops being
defensible the moment a note's name is just its first body line, which is where
M13 is going: fixing a typo in your opening sentence would silently edit other
notes' words, with nothing to opt out to. So this lands first, before the title
comes out, and that window never ships.

`note_links` gains `target_id`, bound when the link is written. `target_norm`
stays and is what an UNRESOLVED link carries — linking to a note that doesn't
exist yet is a supported way to create one, so a link has to be able to name a
target that isn't there. Resolution reads the id, falling back to the name only
where nothing was bound, which is what lets a forward link connect the moment
its target appears. `_claim_unresolved_links` then binds it, so the fallback is
a transitional state rather than a permanent one.

`_rename_inbound_links` and `rewrite_link_title` are gone. What replaced them
touches link rows only: a note's text is never modified by something happening
to a different note.

The client can no longer resolve links for itself, and that is the point. It
used to look `[[text]]` up in a client-side name index, which only held together
BECAUSE renaming rewrote the text everywhere. Now the written text can name
something the target is no longer called, and only the server holds the binding
— so each note serializes its resolved links (`norm`, `id`, and the target's
name as it stands NOW). A renamed note reads correctly everywhere it is linked
from, without a single body having been edited. Unresolved links are simply
absent and fall through to the create-on-click affordance that already existed;
so does the offline desktop store, which derives links at query time and has no
binding to send.

The name-fallback join is owner-scoped everywhere it appears. Bound ids were
resolved owner-scoped when written, but matching on display_title alone would
have let two users who each have a note called "Groceries" see the other's id
and name through an unresolved link (rule 47).

The new behaviour is all SQL and this suite runs without a database, so the
dead helpers' tests are removed rather than replaced. This repo has no
integration lane to hold that ground — noted, not papered over.
This commit is contained in:
2026-08-22 11:02:39 -04:00
parent bacedea8a3
commit 982d24c83b
13 changed files with 352 additions and 96 deletions
+14 -8
View File
@@ -22,7 +22,6 @@ from thoughtsync.notes import (
parse_link_titles,
parse_list_items,
parse_tags,
rewrite_link_title,
)
@@ -128,15 +127,22 @@ def test_parse_link_titles_empty():
assert parse_link_titles("no links here") == []
def test_rewrite_link_title():
body = "see [[Alpha]] and [[ alpha ]] and [[Beta]]"
assert rewrite_link_title(body, "alpha", "Gamma") == "see [[Gamma]] and [[Gamma]] and [[Beta]]"
# `rewrite_link_title` and `_rename_inbound_links` are gone (M13 step 1). They kept
# backlinks alive across a rename by editing the [[text]] inside every note that
# linked to the renamed one — which meant one note's edit silently rewrote another's
# words. Links are bound to a note id now, so a rename needs no repair at all.
#
# What replaced them (`_resolve_target`, `_claim_unresolved_links`, and the id-first
# resolution in backlinks / the graph / serialization) is all SQL, and this suite runs
# without a database, so it is deliberately not asserted here. See the task log: that
# behaviour was checked by hand, and this repo has no integration lane to hold it.
def test_rewrite_link_title_noop():
assert rewrite_link_title("", "alpha", "Gamma") == ""
assert rewrite_link_title(None, "alpha", "Gamma") == ""
assert rewrite_link_title("no links here", "alpha", "Gamma") == "no links here"
def test_parse_link_titles_ignores_nesting():
# The link regex refuses [ and ] inside a token, so a malformed nest yields the
# inner name rather than something spanning both — worth pinning, since this is
# the string that becomes a link's stored target.
assert parse_link_titles("[[outer [[inner]] ]]") == ["inner"]
def test_derive_display_title_explicit_wins():