# How drift checks work

> Link pages to product code and find pages that went out of date.

Docs drift when the product changes and the page does not. Driftless finds drift by linking each page to the product files it describes.

<Diagram name="docs-loop" alt="A product pull request goes to the docs-update skill, which opens a docs pull request. The docs pull request builds the docs site. Each week a drift check compares written pages with the product code and opens an issue for pages to review." />

## The source map

`docs/source-map.json` lists the product files behind each page:

```json
{
  "pages": {
    "get-started/quickstart": {
      "type": "how-to",
      "sources": ["README.md", "src/cli/init.ts"],
      "visuals": ["screenshot: home"]
    }
  }
}
```

`npm run check:sources` checks that every page has an entry and that every source file exists at `product.ref`.

## The drift check

`npm run check:drift` lists pages whose sources changed after the page was last checked, up to `product.ref`. How it finds the last check depends on the layout. It finds the layout from git: if the site folder and `product.path` are in the same repo, the site is in-app.

### Separate sites

Each written page has a `verified.commit`, the product commit that someone checked the page against. The check lists pages whose sources changed between that commit and `product.ref`. It also lists pages with no `verified` commit. `/docs-update finish` sets it after each product merge.

### In-app sites

The docs and the code share one history, so the last commit that changed a page counts as its check. A page is stale when its sources changed in a later commit. If a page has a `verified.commit` that is later, the check uses that commit instead.

| Merge style | Result |
|---|---|
| Squash merge | Code and docs are in one commit, so the page is current. |
| Rebase or merge commit, docs committed last | The page is current. |
| Rebase or merge commit, docs committed first | The page shows as stale. `/docs-update` commits the docs last to prevent this. |

### Options

Add `-- --issue` to keep one "Docs drift" GitHub issue up to date. The workflow templates run this each Monday: `docs-check.yml` in-app, `drift.yml` in a separate docs repo. If a separate product repo is private, add a `PRODUCT_REPO_TOKEN` secret with read access to it.

| Option | What it does |
|---|---|
| `--json` | Prints the result as JSON, for other tools and CI steps. It includes the `layout`. |
| `--fail` | Exits with code 1 if a page needs review or a `verified` commit is not in the product repo. |
| `--no-fetch` | Uses the product checkout as it is, with no `git fetch`. |
| `--issue` | Keeps the "Docs drift" issue up to date. This works only in drift mode. |

## Check one change

Set `PRODUCT_BASE` to check one change instead of all pages. The script then lists the written pages whose sources changed between the merge base of `PRODUCT_BASE` and `PRODUCT_REF`. This is the same diff that a pull request shows.

```bash
PRODUCT_BASE=origin/main PRODUCT_REF=HEAD npm run check:drift
```

The result also counts the changed files that no page maps. Some of them can need a new page. Both workflows in [Linked pull requests](/linked-prs) use this mode.

In-app, the same change can update the pages too. The result then puts each page in one of two lists:

- **Stale:** its sources changed and the page did not. With `--fail`, these pages make the check fail.
- **Updated:** its sources changed and the change also edits the page.

## What the check can and cannot tell you

The check compares file paths. A changed source file means that a page can be out of date. It does not prove that the page is wrong, and it cannot find a wrong page whose sources did not change. A person or an agent still reads the page and the change.

## Fix drift

- For each product change, run `/docs-update`. In-app, it adds the docs change to the feature pull request. With a separate repo, it drafts a linked docs pull request. Refer to [Linked pull requests](/linked-prs).
- With a separate repo, run `/docs-update finish <number>` after the merge. It updates `verified.commit`.
- Each week, run `/docs-update sweep` to fix the pages in the drift issue.

These steps are advisory. A missing docs change blocks a merge only if you turn that on in the workflow.
