Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ design is in the "Proposal: Cross-File Reference System" doc (2026-10-19).

| Folder | Package | Contents |
| ------------------ | ---------------------------- | ------------------------------------------------------------------------ |
| `packages/core/` | `@interref/core` | Comment lexer, scanner, path resolution, Markdown headings, checks. Used by both tools. |
| `packages/core/` | `@interref/core` | Comment lexer, scanner, path resolution, Markdown headings, stamps, checks. Used by both tools. |
| `packages/cli/` | `@normal-computing/interref` | The command-line tool. Its commands are `interref` and `iref`. |
| `packages/vscode/` | `vscode-interref` | The VS Code extension. Attached to each GitHub release as a `.vsix`. |

Expand Down
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ iref:./relative/to/this/file
iref:../also/relative
iref:/some/doc.md#heading-slug
iref:#heading-in-this-file
sync:/some/doc.md#heading-slug@3f9a1c2e
```

- A path starts with `/` (from the repo root), `./` or `../` (from the directory of the file that
Expand All @@ -67,6 +68,7 @@ iref:#heading-in-this-file
- Percent-encode spaces, `#` and `%` in paths (`%20`, `%23`, `%25`).
- The ref ends at whitespace, a quote, a backtick or a bracket. A trailing `-->`, `*/` or
`. , ; : ! ?` is not part of it.
- A ref can end in a [stamp](#stamps): `@` and 8 hex digits.
- Write `iref\:` to show the syntax in a comment without making a ref.

### Where a ref counts
Expand Down Expand Up @@ -143,6 +145,78 @@ Note: some site generators, for example MkDocs, make heading ids differently fro
merges repeated hyphens. Use the GitHub form. When a slug is wrong, `interref check` and the editor
suggest the nearest correct slug.

### Stamps

A stamp ends a ref: `@` and the first 8 hex digits of the SHA-256 of the target file's text. It
records the text that someone checked the ref against. When that text changes, `interref check`
fails on the ref (`stale-stamp`), so that someone reads the change and updates the file that holds
the ref.

```typescript
// TTL must match the docs: sync:/docs/auth.md#token-lifetime@f24671a0
```

- The stamp always covers the whole file. A heading fragment does not change this: a change
anywhere in the file makes the stamp stale. A directory, or a file that is binary or cannot be
read, has no stamp (`invalid-stamp`).
- A byte order mark, line ends, trailing spaces and blank lines do not count. Neither do stamps,
refs in comments, comments that hold only refs, and the spaces next to them. Thus adding a ref to
a file, or stamping one, does not make the stamps of other refs to it stale.
- The other text of comments counts, doc comments and docstrings too: a changed docstring makes the
stamps of the refs to its file stale, also when the docstring holds a ref.
- A ref without a stamp works as before: a change to its target does not fail the check.
- A path that the repo has as written, such as `/lib/v@1a2b3c4d`, is a path, not a stamp.

Do not write a stamp by hand. `interref stamp <path>...` writes the current stamp into each
stamped ref in the files, once you have checked that they agree with their targets.
`--add sync` also stamps the `sync:` refs that have no stamp yet:

```console
$ interref stamp --add sync src config
config/auth.yaml:3:9 sync:/docs/auth.md#token-lifetime stamped @f24671a0
src/auth/session.ts:1:29 sync:/docs/auth.md#token-lifetime stamped @f24671a0
2 refs stamped (3 refs in 3 files)
```

After a change to the file, `interref check` shows each stale stamp. Under the first one of a file,
it lists every ref in the repo to that file or to one of its headings, also those without a stamp:

```console
$ interref check
config/auth.yaml:3:9 error sync:/docs/auth.md#token-lifetime@f24671a0 docs/auth.md changed since this ref was stamped [stale-stamp]
refs to docs/auth.md:
config/auth.yaml:3:9 sync:/docs/auth.md#token-lifetime@f24671a0
src/auth/notes.py:1:7 iref:/docs/auth.md#token-lifetime
src/auth/session.ts:1:29 sync:/docs/auth.md#token-lifetime@f24671a0
src/auth/session.ts:1:29 error sync:/docs/auth.md#token-lifetime@f24671a0 docs/auth.md changed since this ref was stamped [stale-stamp]
2 broken refs (3 refs in 5 files)
```

With `--format json`, each record has its `stamp`, and a stale one also has the list of `refs` to
the file.

To see what changed, find the commit that wrote the stamp, then compare the target with it:

```sh
git log -1 --format=%H -S'@f24671a0' -- src/auth/session.ts
git diff <that commit> -- docs/auth.md
```

Update each file in the list to agree with the new text. Then stamp only the refs that you
checked: `--to` keeps the refs to one file and its headings. `--to` with a `#heading` is an error,
because the stamp covers the whole file.

```console
$ interref stamp src/auth/session.ts config/auth.yaml --to docs/auth.md
config/auth.yaml:3:9 sync:/docs/auth.md#token-lifetime@f24671a0 @f24671a0 -> @9c41e7b2
src/auth/session.ts:1:29 sync:/docs/auth.md#token-lifetime@f24671a0 @f24671a0 -> @9c41e7b2
2 refs stamped (2 refs in 2 files)
```

A fix that `check` suggests for a moved file or heading keeps the stamp, so `check` still compares
it with the new target. `stamp` does not write through a symlink (the file that it points to is
stamped when it is in the paths too), and does not rewrite a file that is not valid UTF-8.

## CLI

Run it from anywhere in the repo that you want to check:
Expand All @@ -156,6 +230,7 @@ src/auth/session.ts:3:5 error iref:/docs/auth.md#tokn no heading #tokn in doc
| Command | Output |
| ------------------------------------- | ------------------------------------------------------------------------ |
| `interref check [<path>...]` | Broken refs in the repo, or under the paths. Exit 1 if any. |
| `interref stamp <path>... [--to <file>]` | Write the current [stamp](#stamps) into the stamped refs in the paths, or only into those to `<file>` and its headings. `--add P` also stamps the `P:` refs that have none. |
| `interref links <file>... [--to <file>]` | Refs that the files hold and where each one goes. |
| `interref backlinks <path>...` | Refs in the repo that point at the files or directories. |
| `interref graph [<file>]` | The link graph: `--format dot` (default), `mermaid`, `json` or `drawio`. |
Expand All @@ -172,7 +247,9 @@ src/auth/session.ts:3:5 error iref:/docs/auth.md#tokn no heading #tokn in doc
| `-v`, `--version` | Show the version. |

Exit codes: 0 clean, 1 broken refs, 2 usage, git, config file, network or editor error. `links` and
`backlinks` also exit 1 when a ref that they list is broken.
`backlinks` also exit 1 when a ref that they list is broken, and `stamp` when a ref that it would
stamp is broken, or when it does not write a file: a symlink to a file outside the paths, or a file
that is not valid UTF-8.

### Paths from stdin

Expand Down Expand Up @@ -265,6 +342,8 @@ source of the guide is [skills/iref/SKILL.md](skills/iref/SKILL.md).
| `reserved-authority` | error | The ref uses `iref://` (or `sync://`, …), kept for cross-repo refs. |
| `case-mismatch` | error | The path differs from a repo path only by case. |
| `invalid-encoding` | error | The percent-encoding is bad. |
| `stale-stamp` | error | The target's text changed since the ref was stamped. |
| `invalid-stamp` | error | The stamp is on a directory, or on a file that is binary or cannot be read. |
| `target-ignored` | warn | The target is untracked or ignored by git. |

### Config file
Expand Down
17 changes: 15 additions & 2 deletions packages/cli/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export function location(record: RefRecord): string {
return `${record.source.path}:${record.source.line}:${record.source.col}`;
}

/** The line of `check` for a ref that has a problem, with its fixes. */
export function problemLine(record: RefRecord): string {
const hint = record.suggestions.length > 0 ? ` (fix: ${record.suggestions.join(" or ")})` : "";
return `${location(record)} ${record.status} ${record.raw} ${record.message ?? ""} [${record.rule ?? ""}]${hint}`;
}

export function describeTarget(record: RefRecord): string {
const target = record.target;
if (target === undefined) return "?";
Expand All @@ -19,8 +25,8 @@ export function describeTarget(record: RefRecord): string {
/** The record as the proposal's JSON shape, without editor-only offsets. */
export function recordJson(record: RefRecord): { [key: string]: JsonValue } {
const json: { [key: string]: JsonValue } = {
source: { path: record.source.path, line: record.source.line, col: record.source.col },
raw: record.raw,
...refJson(record),
...(record.stamp === undefined ? {} : { stamp: record.stamp }),
target: null,
status: record.status,
};
Expand All @@ -37,6 +43,11 @@ export function recordJson(record: RefRecord): { [key: string]: JsonValue } {
return json;
}

/** Where a ref is written, and the ref. */
export function refJson(record: RefRecord): { [key: string]: JsonValue } {
return { source: { path: record.source.path, line: record.source.line, col: record.source.col }, raw: record.raw };
}

/** Prints `values` as one JSON array, or one value per line for ndjson. */
export function printJson(values: readonly JsonValue[], format: "json" | "ndjson"): void {
if (format === "json") console.log(JSON.stringify(values, null, 2));
Expand Down Expand Up @@ -93,6 +104,8 @@ const RULE_TEXT: Readonly<Record<RuleId, string>> = {
"case-mismatch": "Path differs from a repo path only by case",
"unsupported-fragment": "Only heading fragments are supported",
"invalid-encoding": "Bad percent-encoding",
"stale-stamp": "Target text changed since the ref was stamped",
"invalid-stamp": "A directory, or a file that is binary or cannot be read, has no stamp",
"target-ignored": "Target is untracked or ignored by git",
};

Expand Down
Loading
Loading