Skip to content

Stamp refs with the text of their target, and add iref stamp - #2

Open
PaoloRondot wants to merge 3 commits into
mainfrom
stamped-refs
Open

PaoloRondot wants to merge 3 commits into
mainfrom
stamped-refs

Conversation

@PaoloRondot

@PaoloRondot PaoloRondot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

A ref can end in a stamp: @ and the first 8 hex digits of the SHA-256
of its target file's text, as in sync:/docs/auth.md#token-lifetime@f24671a0.
The stamp always covers the whole file, also when the ref has a heading:
a change anywhere in the file makes it stale. A byte order mark, line
ends, trailing spaces, blank lines, the refs in comments, the comments
that hold only refs and the stamps of refs do not count, so adding or
stamping a ref does not make other stamps stale. A comment that holds
only refs can have markers with letters, such as REM, Ruby's
=begin/=end or Perl's =begin comment. A line in the middle of a
comment that has no letters or digits, such as * in a doc comment,
does not count either, so a ref on its own line in a doc comment does
not change the stamp. The other text of comments counts, docstrings too.
A ref without a stamp works as before.

  • check fails on a stale stamp (new rule stale-stamp), and on a
    stamp on a directory or a binary file (invalid-stamp). Under the
    first stale stamp of a file, it lists every ref in the repo to that
    file or to one of its headings, also the refs without a stamp. In
    JSON, each record has its stamp, and a stale one also the list of refs.
  • stamp <path>... writes the current stamp into the stamped refs in
    the paths, once someone has checked them, and prints @old -> @new.
    --to <file> keeps the refs to one file and its headings (a
    #heading is an error, because the stamp covers the whole file), and
    --add P also stamps the P: refs that have none. A broken ref is
    not stamped, and it makes the command exit 1. stamp does not write
    through a symlink, writes a file that two paths reach once (also
    through hard links), and does
    not rewrite a file that is not valid UTF-8.
  • A suggested fix keeps the stamp, so that check compares it with the
    new target. A path that the repo has as written, @ and all, is a
    path, and iref:@3f9a1c2e is a bare path as before.
  • Repo.stamp caches the stamp of a file until invalidate, which now
    tells whether it forgot any. The extension invalidates a file on any
    change, because a stamp can cover any file, and rechecks the open
    documents only when it forgot some.
  • The README, the guide (with a new "Fix a stale stamp" procedure) and
    the extension's README describe stamps.

Built on the pull request that reads refs in YAML values, so a ref in a YAML value can be stamped too.

Both change the syntax or the config, so a CLI or extension older than this release reports a stamped ref as broken and stops on the new config key: a minor release, with a note.

Tests: 83 core and 28 CLI tests pass; lint is clean.

🤖 Generated with Claude Code

mondaychen
mondaychen previously approved these changes Dec 1, 2026

@mondaychen mondaychen left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review found 9 issues: 3 important, 1 pre-existing, and 5 nits. See the inline comments

Comment thread packages/core/src/stamp.ts Outdated
): string[] {
const removed: Span[] = [];
for (const comment of commentSpans(path, text) ?? []) {
if (scanRefs(text.slice(comment.start, comment.end), protocols).length === 0) continue;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: Once a comment or docstring holds a ref, its whole text is left out of the target's stamp. Later edits to that text never make any stamp stale. stampLines drops every comment span that has at least one ref in it, and not only the ref. Also, adding a ref to an existing comment drops that comment's text, which changes the stamp. This breaks the README promise that adding a ref does not make other stamps stale.

Example: src/model.py starts with the docstring """Tokens last 15 minutes. sync:/docs/spec.md#tokens""", and another file holds sync:/src/model.py@<stamp>. Someone edits the docstring to say 30 minutes. The stamp of model.py does not change, so interref check passes.

In another case, docs/spec.md#tokens has <!-- TTL is configurable -->. A user adds sync:/src/x.py inside that comment. Every stamped ref to #tokens now fails as stale-stamp, though no text changed.

Possible fix: Remove only the ref span (and the spaces next to it), not the whole comment. Or remove the whole comment only when the ref is all it holds.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. stampLines now drops a whole comment only when it holds nothing but refs (no letter or
digit besides them). In any other comment, only the refs are dropped, with the spaces after them,
or the spaces before them when none follow. So your docstring edit (15 to 30 minutes) makes the
stamp stale. Adding sync:/src/x.py inside <!-- TTL is configurable --> does not change the
stamp. Both examples are tests now, and the README and the guide say it. One limit is left: a
comment whose marker is a word (REM in batch files, \begin{comment} in TeX) keeps that word
when it holds only refs.

Comment thread packages/core/src/check.ts Outdated
if (match.stamp === undefined || match.fragment !== undefined) return match;
const path = `${match.path}@${match.stamp}`;
const resolved = resolveTarget(source, path, match.protocol);
return resolved.ok && repo.kind(resolved.value.path) !== undefined ? { ...match, path, stamp: undefined } : match;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: A ref to an untracked or git-ignored file whose name ends in @ and 8 hex digits used to give a target-ignored warning. Now it is read as a stamp and gives an error. literalPath checks only repo.kind (the git file list), but checkRef also accepts files on disk through repo.kindOnDisk.

Example: build/v@1a2b3c4d exists on disk but git ignores it, and a comment holds iref:/build/v@1a2b3c4d. Before this PR: a warning and exit 0. Now: the stamp is split off, the path /build/v is checked, and the result is missing-file (or stale-stamp if build/v exists), with exit 1.

Possible fix: In literalPath, also accept the literal path when repo.kindOnDisk(resolved.value.path) !== undefined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. literalPath moved to stamp.ts and also accepts repo.kindOnDisk. So
iref:/build/v@1a2b3c4d to an ignored file gives target-ignored again. This is a test now.

for (const repo of this.loaded) {
const path = repo.relative(uri.fsPath);
if (path !== undefined) repo.invalidate(path);
if (path !== undefined && repo.invalidate(path)) forgot = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important: A stamped ref whose target is reached through a symlink keeps a stale diagnostic after the real file is edited. Repos.invalidate turns the URI into a repo path with repo.relative, which follows symlinks. The cache key is the git path of the symlink, so nothing is forgotten. With this PR, onDidChange no longer fires at all.

Example: docs/current.md is a symlink to docs/v2.md, and an open file holds sync:/docs/current.md#tokens@abcd1234. The user edits docs/v2.md. invalidate("docs/v2.md") returns false and no recheck runs. The Problems panel shows the old status until the extension reloads.

Possible fix: Also forget cache entries whose real path matches, or fire onDidChange when the cache is keyed by a symlink.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in Repo. When headings or stamp caches a path that is a symlink, Repo notes its real
path. invalidate(real path) then also forgets the symlink's entries and returns true, so the
extension rechecks. This covers the old heading cache too. This is a test now.

if (event.contentChanges.length === 0) return;
diagnostics.schedule(event.document);
if (isMarkdownPath(event.document.uri.fsPath)) repos.invalidate(event.document.uri);
repos.invalidate(event.document.uri);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing: If a user edits a target document and then closes it without saving, the cached stamps (and headings) stay as they were computed from the unsaved editor text. Nothing invalidates the cache on close, and the disk file did not change, so the watcher does not fire. This already happened for headings, and the new stamp cache has the same gap.

Example: The user opens docs/spec.md and types into #tokens. A stamped ref in another open file goes stale, and the stamp is cached from the unsaved text. The user closes spec.md with Don't Save. The other file still shows stale-stamp, though the disk text matches the stamp.

Possible fix: Call repos.invalidate(document.uri) in onDidCloseTextDocument.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. onDidCloseTextDocument now also calls repos.invalidate(document.uri).

Comment thread packages/cli/src/main.ts
let refused = 0;
for (const [path, list] of edits) {
const file = repo.absolute(path);
if (lstatSync(file).isSymbolicLink()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: interref stamp can exit 0 while some of the refs it was asked to stamp were not written. A file that is a symlink is skipped with a message, but refused is not increased. Agents and scripts that trust the exit code will think every ref was stamped.

Example: src/link.py is a symlink to ../lib/model.py, which is outside the given paths. interref stamp src --add sync prints src/link.py not stamped: a symlink, stamps nothing in it, and exits 0.

Possible fix: Count skipped symlinks (and 'same file' skips whose real file is not in scope) in the exit status, or state the behavior in the docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. A symlink is a refused file (exit 1, N files not stamped in the summary), unless the
file that it points to is in the paths too. In that case its refs are stamped there. A skip for
"the same file" happens only when two paths in the paths reach one file that is written through
the first path, so it is not a refusal. The README describes the exit code.

Comment thread packages/cli/src/main.ts Outdated
}

/** The target of `stamp --to`: a file, and the slug of one of its headings after `#`. */
function stampTarget(repo: Repo, argument: string): { path: string; slug?: string } {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: If the heading in stamp --to <file>#<heading> does not exist (for example a typo or the wrong case), the command stamps nothing and exits 0. stampTarget parses the fragment but does not check it against repo.headings(path).

Example: interref stamp src --to docs/spec.md#Tokens (the slug is tokens) prints 0 refs stamped (...) and exits 0. The user thinks the stale refs are now stamped.

Possible fix: Give a UsageError when no heading of the file has the slug, and suggest the nearest slug as check does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. It is now a usage error (exit 2) with the nearest slug, for example
no heading #Tokens in docs/spec.md; did you mean #tokens?. check and stamp share the
suggestion code (similarSlugs).

Comment thread packages/cli/src/main.ts Outdated
const changed = (record: RefRecord): string | undefined =>
record.rule === "stale-stamp" && record.target !== undefined ? sectionOf(record.target) : undefined;
const all = roots !== undefined && problems.some((record) => changed(record) !== undefined) ? scanRepo(repo).records : scan.records;
const refsTo = (section: string): RefRecord[] =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Under a stale stamp, check does not list every ref to the changed text, as the README and the skill say it does. refsTo matches sectionOf(target) exactly, so it leaves out refs to subsections, to the parent section, and to the whole file, though their text changed too.

Example: A change in ### Renewal makes sync:/docs/spec.md#tokens@x stale. The list shows only refs to #tokens. An unstamped iref:/docs/spec.md#renewal and an unstamped sync:/docs/spec.md are not shown, though they are the refs that the change affects most.

Possible fix: Also list refs whose section contains the changed section or is contained in it (a whole-file ref, an enclosing heading, or a subheading).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The list now has every ref to the same file whose section overlaps the changed one: the
whole file, an enclosing heading, or a subheading. It shares the section bounds with the stamp
code (sectionLines). The README, the guide and a CLI test cover it.

removed.push({ start: comment.start, end: comment.end + (SPACES.exec(text)?.[0].length ?? 0) });
}
if (values) {
for (const ref of findRefs(path, text, protocols, true)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: In a YAML-values file, the stamp leaves out @ and 8 hex digits at the end of any value ref, even when that suffix is part of a literal file name. stampLines uses the findRefs stamps directly, without the literalPath step that checkRef applies.

Example: model/x.yaml holds path: iref:/lib/a@3f9a1c2e, and lib/a@3f9a1c2e exists. Someone changes the value to iref:/lib/a@00000000. The stamp of model/x.yaml stays the same, so stamped refs to it do not go stale.

Possible fix: Run the same literal-path check before you remove the stamp span, or remove only the stamps that checkRef would treat as stamps.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Repo.stamp passes literalPath to stampLines, so iref:/lib/a@3f9a1c2e keeps its
suffix in the stamp when that path exists. This is a test now.

Comment thread packages/core/src/check.ts Outdated
if (stamp === undefined) {
return problem("invalid-stamp", `${kind === "directory" ? "a directory" : "a binary file"} has no stamp`, [format(path, kind)], target);
}
if (stamp !== match.stamp) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The invalid-stamp message can call a text file 'a binary file'. repo.stamp returns undefined whenever repo.text returns undefined. That includes a file that cannot be read (permissions) and a git-listed symlink to a directory (EISDIR), not only binary files.

Example: docs/secret.md exists but is mode 000, and a ref holds sync:/docs/secret.md@abcd1234. Check reports a binary file has no stamp [invalid-stamp] and suggests removing the stamp.

Possible fix: Make the message more general (for example 'cannot read the text of the target'), or tell binary files apart from files that cannot be read.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The message is now "a file that is binary or cannot be read has no stamp". The SARIF text,
the README and the guide say the same.

@mondaychen
mondaychen dismissed their stale review December 1, 2026 19:29

Approval removed: code review found 3 important issues. See the inline comments.

A ref can end in a stamp: `@` and the first 8 hex digits of the SHA-256
of its target's text, as in `sync:/docs/auth.md#token-lifetime@f24671a0`.
With a heading, the stamp covers the section and its subsections;
without one, the whole file. A byte order mark, line ends, trailing
spaces, blank lines, stamps, refs in comments and the comments that
hold only refs do not count, so adding or stamping a ref does not make
other stamps stale. The other text of comments counts, docstrings too,
also when it holds a ref. A ref without a stamp works as before.

- `check` fails on a stale stamp (new rule `stale-stamp`), and on a
  stamp on a directory, or on a file that is binary or cannot be read
  (`invalid-stamp`). Under the first stale stamp of a section, it lists
  every ref in the repo to that section, to a section in it or around
  it, or to its file, also the refs without a stamp. In JSON, each
  record has its stamp, and a stale one also the list of refs.
- `stamp <path>...` writes the current stamp into the stamped refs in
  the paths, once someone has checked them, and prints `@old -> @new`.
  `--to <file>[#heading]` keeps the refs to one file or heading (a
  heading that the file does not have is an error), and `--add P` also
  stamps the `P:` refs that have none. A broken ref is not stamped, and
  it makes the command exit 1, as does a file that it does not write: a
  symlink to a file outside the paths, or a file that is not valid
  UTF-8. A file that two paths reach is written once.
- A suggested fix keeps the stamp, so that `check` compares it with the
  new target. A path that the repo has as written, `@` and all, also a
  file that git ignores, is a path, in a check and in a stamp, and
  `iref:@3f9a1c2e` is a bare path as before.
- `Repo.stamp` caches the stamps of a file until `invalidate`, which now
  also forgets the symlinks to the file, and tells whether it forgot
  any. The extension invalidates a file on any change and when its
  editor closes, because a stamp can cover any file, and rechecks the
  open documents only when it forgot some.
- The README, the guide (with a new "Fix a stale stamp" procedure) and
  the extension's README describe stamps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Base automatically changed from yaml-values to main December 4, 2026 15:17
mondaychen and others added 2 commits December 4, 2026 10:26
A heading in a stamped ref no longer limits the stamp to its section. This
removes the section line ranges from stamps, check and stamp --to, which now
takes only a file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rd-linked file once

- A line in the middle of a comment that has no letters or digits
  besides its refs, such as ` *` in a doc comment, does not count. Thus
  adding the line ` * ref`, or writing a ref on a line ` *`, does not
  change the stamp.
- A comment that holds only refs and markers that have letters does not
  count: `REM` in Batch and VB, `=begin`/`=end` in Ruby, `=pod`,
  `=begin comment` and `=for comment` in Perl, `{% comment %}` in Jinja,
  `#+begin_comment` in Org, `\begin{comment}` in LaTeX, and the prefix of
  a Python docstring such as `r"""`.
- stampLines removes a byte order mark before it finds the comments, so
  a BOM does not hide a docstring on the first line.
- stamp finds a file that two paths reach by its device and inode, so a
  file that two hard links reach is written once. Before, the second
  write put a second stamp on the ref.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants