This article is for authors who keep a weft project in git. It
follows one small report from its first commit to a release check. After
it you will know what the commit gate refuses and how to answer it, what
the ledger records and how changes() reads it, and how
verify() checks a project before you hand it on.
The examples need git, Quarto and a project set up with
use_weft(), so they are not run when this article is built.
Each command was run in a scratch project, and the output shown is what
it printed. Paths have been shortened.
Three checks, at three moments
weft checks a project at three moments:
| When | What runs | What it reads |
|---|---|---|
| each commit | the commit gate, gate()
|
the static lints of the changed files, compared with
HEAD
|
| each render | the checks in the document | the values, the spans and the claims as they knit |
| before release | verify() |
everything: lints, a full render, the ledgers |
The commit gate is a commit-msg git
hook that runs gate(). use_weft() installs it,
or use_hooks() on its own. It compares what the commit will
hold with HEAD, and refuses the commit for one of three
reasons:
- a construct error in a changed document, whatever
HEADheld; - a tier-1 finding that
HEADdid not hold, such as a newly typed number; - weft constructs removed, when the commit message does not say so.
The gate runs no R code from the project and renders nothing. It reads git only, so it takes a fraction of a second.
A report before weft
The report starts as an ordinary Quarto document, committed to git:
---
title: "Creek report"
---
## Sites {#sec-sites}
```{r}
#| label: data
turbidity <- c(Upper = 4.2, Middle = 7.9, Lower = 12.6)
n_high <- sum(turbidity > 10)
```
We sampled three sites in 2024. One site read above the trigger of 10 NTU.Then the author sets weft up:
weft::use_weft()
#> weft will set up creek-report:
#> use_setup .Rprofile (new), _weft.yml (new), report.qmd (edit)
#> use_quarto _extensions/weft-check/_extension.yml (new), _extensions/weft-check/weft.lua (new), _extensions/weft-check/weftlib/ (new), _extensions/weft-check/weft-check.lua (new), _extensions/weft-check/weft-output.lua (new), _weft/pre-render.R (new), _weft/post-render.R (new), _quarto.yml (edit)
#> use_dictionary dict.yml (new), _weft/dict.schema.json (new)
#> use_hooks .git/hooks/commit-msg (new)
#> use_ci skipped: origin is not on GitHub
#> use_agent_docs AGENTS.md (new), .agents/skills/weft/SKILL.md (new), .agents/skills/weft/references/reports.md (new), .agents/skills/weft/references/syntax.md (new)
#> Wrote: .Rprofile, _weft.yml, report.qmd, ...vignette("setup", package = "weft") describes each of
these files. The edit to report.qmd adds
engine: knitr to its front matter.
git add .Rprofile _weft.yml report.qmd _extensions _weft _quarto.yml \
dict.yml AGENTS.md .agents
git commit -m "Set up weft"commit-msg: weft passed (1 changed file, 0.3 s)
[main 780cac8] Set up weft
12 files changed, 1041 insertions(+)
The report still holds two typed numbers, “2024” and “10”. The gate
lets them through, because it refuses only findings that
HEAD did not hold. A project can adopt weft without fixing
every old number first.
A new typed number
The author adds a sentence:
commit-msg: 1 finding added that HEAD did not hold
report.qmd
15 numeral "12.6" is a number typed in prose (rule 5.2.1).
Write it as a value, put it in the dictionary, or mark it [12.6]{.weft-literal why="..."}.
commit-msg: 0.3 s
The commit is refused. The hint lists the three fixes. Here the
number is computed from data, so the author computes it. The trigger is
a choice, so it goes in dict.yml:
and the report becomes:
---
title: "Creek report"
engine: knitr
---
## Sites {#sec-sites}
```{r}
#| label: data
turbidity <- u(c(Upper = 4.2, Middle = 7.9, Lower = 12.6), NTU)
n_high <- sum(turbidity > dict$trigger)
```
We sampled three sites in 2024.
[No site | One site | # sites]{n="n_high"} read above the trigger of `r dict$trigger`{.num}.
The highest reading was `r claim(max(turbidity), . < u(50, NTU))`{.num}.$ git add report.qmd dict.yml
$ git commit -m "Compute the highest reading"
commit-msg: weft passed (1 changed file, 0.3 s)
[main db9c980] Compute the highest reading
“2024” is still there and still tolerated. lints() lists
every finding, old and new, if you want to see what remains (see
vignette("lints", package = "weft")).
The ledger
Each render of a set-up project writes a ledger for
each document: a record of every value and every chosen arm the render
printed, with the sentence it sat in. It lives in
_weft/ledger/, one folder per source:
_weft/ledger/report.qmd/report.jsonl
_weft/ledger/report.qmd/report.conditions.jsonl
Commit it with the source. Then git holds what each version of the
report said, and changes() can compare them.
The ledger is JSON Lines. The first line records what the render read: the dictionary entries, the source files and any targets, each with a hash. Each later line is one entry:
{"weft":"ledger","version":1,"source":"report.qmd","output":"report","dict":{"trigger":"8e71a4f3e615896d"},"files":{"report.qmd":"7f971c44b8bcdfd7"},"targets":{}}
{"file":"report.qmd","section":"sec-sites","kind":"n","expr":"n_high","occ":1,"id":null,"context":"prose","text":"No site","arm":"zero","rel":"=","raw":0,"unit":null,"failed":null,"dict":[],"sentence":"No site read above the trigger of 10 NTU."}
{"file":"report.qmd","section":"sec-sites","kind":"value","expr":"dict$trigger","occ":1,"id":null,"context":"prose","text":"10 NTU","arm":null,"rel":"=","raw":10,"unit":"NTU","failed":null,"dict":["dict$trigger"],"sentence":"No site read above the trigger of 10 NTU."}
An entry is keyed by its file, its section anchor and its expression.
That is why the lints ask for a {#id} on each heading: the
key stays the same when the heading’s words change.
A table gets one entry too: a table that an R chunk
prints, such as knitr::kable(), gt, flextable or tinytable,
and text that a chunk with results: asis prints. The entry
holds no cells. It holds a digest of the table’s visible text, with the
markup taken out, so a change of style, class name or element id is not
a change, and a change of any number or word is. It is keyed by the
chunk’s label, and by its place in the chunk when a chunk prints
two:
{"file":"report.qmd","section":"sec-sites","kind":"table","expr":"tbl-sites","occ":1,"id":"tbl-sites","context":"chunk","text":"5c1d0e2f9a7b3c48","arm":null,"rel":null,"raw":null,"unit":null,"failed":null,"dict":[],"sentence":"Turbidity at each site."}
The sentence is the table’s caption, or the start of its
text. A table printed as plain code output (print(df)
without results: asis), a figure, and a table that a Python
or Julia chunk prints in a knitr document are not recorded. A document
on the Jupyter or Julia engine gets the same entries from the filter,
keyed by the label of the cell (see the languages article); its digest
is of the cell text, and does not compare with an R document’s. Label
the chunks that print tables, so that each keeps its name when a chunk
is added above it.
The condition log,
report.conditions.jsonl, records each warning and error
that a chunk or an inline expression raised during the render, and, for
a document that reads targets, the ones recorded in the targets store. A
new warning then shows in the diff, even when the output looks the
same.
What changed: changes()
New data arrive. The Lower reading was 9.6, not 12.6, and the author adds a nitrate column whose values came from a laboratory as text:
turbidity <- u(c(Upper = 4.2, Middle = 7.9, Lower = 9.6), NTU)
n_high <- sum(turbidity > dict$trigger)
nitrate <- as.numeric(c("0.8", "<0.5", "1.1"))After a render, changes() compares the ledgers in the
working tree with those in HEAD:
weft::changes()
#> weft changes since HEAD (9e34dd7): 1 ledger compared; 2 changed, 0 added, 0 removed; 1 new condition
#>
#> Structural changes
#> ~ report.qmd#sec-sites n n_high [arm one -> zero, crosses 0]
#> before: One site read above the trigger of 10 NTU.
#> after: No site read above the trigger of 10 NTU.
#>
#> Other changes, ranked
#> ~ report.qmd#sec-sites value claim(max(turbidity), . < u(50, NTU)) [claim word "highest"]
#> before: The highest reading was 12.6 NTU.
#> after: The highest reading was 9.6 NTU.
#>
#> New conditions
#> ! report.qmd chunk data warning: NAs introduced by coercionRead it from the top:
- Structural changes come first. An arm changed, a value changed sign, crossed 0, 1 or a dictionary value, or moved by an order of magnitude. These are the changes most likely to make a sentence elsewhere wrong.
- Other changes, ranked follow. A change beside a claim word, such as “highest” or “all”, ranks above a plain change; then larger relative changes rank higher.
-
New conditions lists warnings and errors the last
committed render did not raise. Here
as.numeric()turned “<0.5” intoNA, which the output alone would not show. - Not re-rendered, when present, lists ledgers whose inputs (a dictionary entry, a source file, a target) changed after the render. Their values are stale.
A different value with the same printed text is not a change.
changes() also lists tier-2 lint findings on lines added
since the commit.
Tables come last, under Tables: a changed table by its label, with its caption under it, and added and removed tables one line per ledger:
Tables
~ table `tbl-sites` changed
Turbidity at each site.
report.qmd: 1 table added: tbl-flow
changes() says that a table changed, not which cells;
render the two versions to compare them. The first render after you
upgrade to a weft that records tables lists every table of an existing
ledger as added, once. Commit that ledger, and the next comparison is
quiet.
changes(since = ) compares with any commit, and
print(x, brief = TRUE) prints only the header, the
structural changes, the stale ledgers and the new conditions:
print(weft::changes(since = "9e34dd7"), brief = TRUE)
#> weft changes since 9e34dd7 (9e34dd7): 1 ledger compared; 1 changed, 0 added, 1 removed; 1 new condition
#>
#> Structural changes
#> ~ report.qmd#sec-sites n n_high [arm one -> zero, crosses 0]
#> before: One site read above the trigger of 10 NTU.
#> after: No site read above the trigger of 10 NTU.
#>
#> New conditions
#> ! report.qmd chunk data warning: NAs introduced by coercionchanges() renders nothing and writes nothing.
Removing constructs
A computed value, a span or a claim is a check. Deleting one is sometimes right, but it should never happen by accident. The author drops the sentence on the highest reading:
$ git commit -m "Drop the sentence on the highest reading"
commit-msg: 2 weft constructs removed, and the message does not say so
report.qmd, as in HEAD
17 claim(max(turbidity), . < u(50, NTU))
17 `r claim(max(turbidity), . < u(50, NTU))`{.num}
If the removal is intended, add this line at the end of the commit
message, after a blank line:
Weft-Removed: 2
or commit with: git commit --trailer "Weft-Removed: 2"
commit-msg: 1 check gone since HEAD (information: what still checks each; rule 6.3.7)
report.qmd, as in HEAD
17 max(turbidity) < u(50, NTU)
looser: no check of max(turbidity) against a quantity in NTU remains in report.qmd
commit-msg: 0.2 s
The value and its claim count as two constructs. The removal is intended, so the author says so:
$ git commit -m "Drop the sentence on the highest reading" --trailer "Weft-Removed: 2"
commit-msg: note: report.qmd:17 in HEAD, `max(turbidity) < u(50, NTU)`, is gone and nothing that remains covers it (rule 6.3.7)
commit-msg: weft passed (1 changed file, 0.2 s)
[main c2ba829] Drop the sentence on the highest reading
The note says that nothing else in the document checks the highest reading now (see “What still checks a removed check” below). It decides nothing.
The trailer stays in the history, so a reviewer can find every commit
that removed checks with git log --grep "Weft-Removed". The
number must match: a trailer that says 1 when 2 were removed is
refused.
What still checks a removed check
The gate also says, for each check that a commit removes, whether a
check that remains still covers it. A document guards
turb_max >= trigger in nine sentences and
turb_max > trigger in one, at line 64. Deleting the
sentence at line 64 leaves the document looser than it was, and the
gate’s report says so after the constructs removed:
commit-msg: 1 check gone since HEAD (information: what still checks each; rule 6.3.7)
report.qmd, as in HEAD
64 turb_max > trigger
looser: nothing that remains implies it; remaining on turb_max against trigger: `turb_max >= trigger` (9 places)
These lines are information. They decide nothing: the commit passes or fails on the constructs removed and the trailer, exactly as above. Each check that is gone gets one verdict:
| Verdict | Means |
|---|---|
| covered | the same check remains, or a remaining check implies it:
x > 5 implies x >= 5,
x > 3 and x != 5
|
| looser | no remaining check implies it: checks of the same value against the same thing remain but are weaker, or only a warned check implies it (a warned check fails nothing), or nothing checks that value against that thing any more |
| not judged | a name the check reads is assigned in more than one place in the document, so no other check is taken to cover it |
| not read | the check is not a comparison weft reads (see checks()
in vignette("wording", package = "weft")) |
| the document is gone | the file was deleted or renamed, and its checks with it |
One remaining check must cover the removed one alone:
x >= 5 and x != 5 together do not cover
x > 5. A check that does not always run, in an arm of a
span or in a claim under an R if or loop, covers
nothing.
A changed bound removes no construct, so it needs no trailer. When it loosens the document, a passing commit prints a note before its line:
commit-msg: note: report.qmd:44 in HEAD, `n_up >= 10`, is gone and nothing that remains covers it (rule 6.3.7)
gate() returns the rows in checks. The way
to keep these lines short is the advice of the wording article: check a
value once, where it is made, and use its name after.
Claims in scripts
A claim() in an R script counts as one in a document.
The gate reads every call of claim(),
weft::claim() or weft:::claim() in the
script’s code, wherever it sits: in a function body, a target’s command,
a lambda, a pipe or a loop. A call in a comment or a string is not one,
nor is claim from another package, nor a claim inside a
weft() literal, which is read as a string is. The script
claims are added to the claims of the project’s documents in one total.
So moving a claim between a chunk and a script, or between two scripts,
removes nothing, while deleting one, deleting the script that holds it,
or staging a script that no longer parses needs
Weft-Removed: N like any other removal. A script that does
not parse is named in a note, once.
A claim in the body of an if whose condition is the
constant FALSE is not a script claim: the gate does not
count it, checks() does not list it and
verify() does not collect its decision. The
else arm is read. F, 0 and
FALSE && x are not the constant. The rule holds in
R chunk code too, and it makes wrapping a claim in
if (FALSE) a removal, as turning eval off
is.
A script claim does nothing different when it runs: outside a knit it
stops (or warns at level warn), and it writes nothing to
the ledger. checks() lists it as a check with the script as
its file, inert-check reads it, and verify()
checks a literal decision pointer, as it does for a
document’s. A claim inside a function definition is listed as not read,
because the check cannot be judged from the source. Python and Julia
files are not read, nor are scripts left out by
scripts_skip. See “Scripts” in
vignette("lints", package = "weft") for how a script is
opened.
Construct errors
A construct error is refused in any changed file, even one that held it before:
$ git commit -m "Say which way turbidity moved"
commit-msg: 1 construct error
report.qmd
17 construct A formatter on an `if` span is an error (rule 1.5.7): .num.
commit-msg: 0.2 s
Running the gate by hand
gate() is the function the hook runs. Call it to judge
what is staged without committing:
weft::gate()
#> commit-msg: 1 finding added that HEAD did not hold
#> report.qmd
#> 17 numeral "3.4" is a number typed in prose (rule 5.2.1).
#> Write it as a value, put it in the dictionary, or mark it [3.4]{.weft-literal why="..."}.
#>
#> commit-msg: 0.3 sIt returns, invisibly, a list with ok and the findings,
so a script can test weft::gate()$ok.
gate(message = ) takes the path of a file holding a commit
message, to check a Weft-Removed trailer. It judges the
whole repository, including every subfolder that holds a
_weft.yml.
The hook
use_hooks() writes .git/hooks/commit-msg.
The script is POSIX sh and starts:
#!/bin/sh
# weft-file: use_hooks 0.1.0 063f9359c3c3c3c91daa6def625f32d8
# The weft commit gate (rules 6.1 to 6.3), written by weft::use_hooks().
# Remove it with weft::use_hooks(remove = TRUE). Skip it for one commit
# with: git commit --no-verifyIt records the Rscript and the library paths of the R
session that ran use_hooks(), and runs R without the
project’s .Rprofile or .Renviron. If you
upgrade R or move your library, run use_hooks() again;
status() reports a hook whose Rscript has
gone.
When the repository already has a commit-msg hook from
another tool, or uses a hook manager (core.hooksPath),
use_hooks() leaves it alone, writes its script to
.git/weft/commit-msg, and prints the line to add to the
hook you already run.
git commit --no-verify skips every hook for one commit.
It is there for emergencies, such as R being broken. It is not a way
past a finding: answer the finding instead.
use_hooks(remove = TRUE) removes the hook.
What the reader sees: the rendered-output check
Some mistakes show only after Quarto has done its work. A
cross-reference to a label that does not exist prints “?@sec-missing”. A value that was missing prints
“NA”. A fence Pandoc did not read prints “:::” in the text. A third
filter, weft-output.lua, reads the finished document after
Quarto’s own filters, in every output format, and looks for three
things:
| Kind | Finds | Setting | Default |
|---|---|---|---|
| unresolved reference | under Quarto, “?@sec-missing” for a cross-reference, and “?meta:key” or “?var:key” for a shortcode, where Quarto found nothing | output_refs |
fail |
| missing value in output |
NA, NaN, Inf or
-Inf (also nan, inf,
-inf) as a word in prose, headings, lists, footnotes and
captions |
output_missing |
warn |
| leftover markup | text that starts with :::, an attribute block stuck to
a word, such as m{.num}, and under Quarto bookdown’s
\@ref(
|
output_markup |
fail |
Each setting is fail, warn or
off, in _weft.yml or under a document’s
weft: key. A Quarto project collects the findings with the
other failures and reports them once, after the render, naming each
format in which a finding occurred:
weft: 1 failure
ch2.qmd #sec-methods (html) [unresolved reference] ?@sec-missing
As shown in ?@sec-missing, the flow was low.
Quarto could not resolve the reference `@sec-missing`, and printed "?@sec-missing" (rule 2.5.5).
weft: 1 warned check(s)
ch2.qmd #sec-results (html) [missing value in output] NA
The mean was NA across the sites.
"NA" in the rendered prose: a value printed as missing. Mark a deliberate one [NA]{.weft-literal why="..."} (rule 2.5.5).
An “NA” that is meant, such as an abbreviation, is marked
[NA]{.weft-literal why="not applicable"}. Table cells, code
and raw content are not searched for missing values, so “NA” in a table
is never reported. “N/A”, “NULL” and “None” are not matched.
What it cannot check:
- citations: a filter sees a citation before the citation processor
runs, so an unknown key cannot be told from a known one. The
cite-keylint checks keys against the bibliography; - what a writer or a later program adds: bookdown’s
\@ref()under R Markdown, LaTeX’s “??”, Typst’s own references.
Two filters can name the same text. bare_missing warns
at knit time about a bare value that prints NA, naming its
expression, and the output check warns about the same “NA” in the
output, naming its sentence. The weft-check filter fails on
a weft attribute block it could not read, before Quarto’s filters, and
also on a raw <<weft: marker anywhere in the
document, raw HTML included (written <<weft: or
<<weft:, so one in a gt or reactable cell is
found too). The output check may list the same text again as leftover
markup. Neither pair is merged.
The filter must run after Quarto’s, which an extension cannot
arrange, so use_quarto() adds an entry of its own to
_quarto.yml (see
vignette("setup", package = "weft")). A project set
up before this check gets it only when you run use_quarto()
again (or use_setup() again for an R Markdown
document outside Quarto). Until then status() says what is
missing:
quarto missing missing: _extensions/weft-check/weft-output.lua; _quarto.yml lacks the weft entries (the weft-output.lua filter entry, `at: post-render`)
verify() collects the findings in its render part: each
fail finding fails it, and warn findings are
listed. Its result holds them in output.
Before release: verify()
verify() is the full check. It has four parts:
| Part | What it does |
|---|---|
lints |
runs the tier-1 lints over the whole project and compares them with a commit |
decisions |
checks the decision pointers in dict.yml
and in the documents: on .weft-literal and
cite spans, and in claim() calls that give a
literal decision, except in a chunk with
eval: false
|
render |
renders every document with Quarto (or knits it, under R Markdown),
backing up and restoring _freeze, and reads the record each
render leaves to see that every check ran and passed; it also collects
the findings of the rendered-output check, below |
ledger |
checks that no ledger is stale |
weft::verify()
#> weft verify: passed
#> lints 1 tolerated finding
#> decisions 0 pointers
#> render 1 document rendered
#> ledger 1 ledger checked
#>
#> Tolerated findings (rule 8.1.2); make a rule strict in _weft.yml when its count is 0:
#> numeral 1 remain
#> weft verify: 4.7 sThe tolerated finding is the “2024” committed before weft. The count
is a measure of how far adoption has come. When a rule’s count reaches
0, add it to strict in _weft.yml, and
verify() then fails on any instance at all.
A render re-writes the ledgers. Commit them after
verify() if they changed.
When something is wrong, verify() says what and fails.
Here the Lower reading was mistyped as 79:
weft::verify()
#> weft verify: failed
#> lints 1 tolerated finding
#> decisions 0 pointers
#> render quarto exited with status 1; 1 failure, 0 not executed
#> ledger 1 ledger checked
#>
#> Knit-time failures
#> report.qmd
#> 17 failed claim . < u(50, NTU) was FALSE (value 79 [NTU])
#> `r claim(max(turbidity), . < u(50, NTU))`
#>
#> Tier-2 findings on added lines
#> report.qmd
#> 17 claim-word Claim word "highest" in a sentence with a value; check that the value still bears it out, or guard the word with an `if` span (rule 5.5.1).
#>
#> Tolerated findings (rule 8.1.2); make a rule strict in _weft.yml when its count is 0:
#> numeral 1 remain
#> weft verify: 4.9 s
#> Error: verify() failed: 2 reason(s); the report is above (rule 8.1.1).A failing verify() signals an error of class
weft_verify_error, so
Rscript -e "weft::verify()" exits with status 1. Tier-2
findings are listed for a reader and never fail it.
Arguments narrow it:
-
verify(static = TRUE)runs the lints and the decision check only, with no render. It takes a second, and needs no data. -
verify(files = "report.qmd")renders and judges some documents only. -
verify(since = "v1.0")compares the lints with another commit.
weft::verify(static = TRUE)
#> weft verify: passed
#> lints 1 tolerated finding
#> decisions 0 pointers
#> render skipped: static = TRUE
#> ledger skipped: static = TRUE
#>
#> Tolerated findings (rule 8.1.2); make a rule strict in _weft.yml when its count is 0:
#> numeral 1 remain
#> weft verify: 0.3 sverify_skip in _weft.yml names parts to
skip every time, for example verify_skip: [render] for a
project whose data are not on every machine. status()
reports a skipped part as off.
On pull requests: use_ci()
use_ci() writes .github/workflows/weft.yml,
a GitHub Actions workflow that runs
weft::verify(static = TRUE) on each pull request, compared
with the branch it will merge into. It needs no data and no Quarto, so
it runs for any project. use_weft() skips it unless
origin is on GitHub.
The workflow catches a commit made with --no-verify, or
on a machine without the hook.
Draft mode
While a chapter is half written, a failing claim or an unfinished
span can get in the way. draft: true in
_weft.yml (or the environment variable
WEFT_DRAFT=1) makes a render report its failures and
finish, with a marker such as
<<weft: failed claim, line 20>> in the output
in place of the value. See
vignette("wording", package = "weft").
Draft mode does not change the commit gate, which still runs.
verify() refuses to run in draft mode, because a draft
render fails nothing:
weft::verify()
#> Error: verify() does not run in draft mode (rule 8.1.5): draft is on in _weft.yml. A draft render fails nothing, so it can verify nothing.Turn draft mode off before release. status() shows it as
off while it is on.
Seeing the set-up: status()
status() reports what weft has installed, and what is
missing or out of date:
weft::status()
#> weft status: creek-report
#> setup ok _weft.yml records weft 0.1.0 (installed: 0.1.0)
#> activation ok route found (the .Rprofile)
#> quarto ok extension, scripts and _quarto.yml entries are in place
#> dictionary ok dict.yml and its schema are in place
#> hooks ok the commit-msg hook is weft 0.1.0, Rscript /usr/local/bin/Rscript
#> ci n/a origin is not on GitHub
#> agent docs ok the AGENTS.md block and the skill are current
#> draft ok draft mode is off
#> verify_skip ok verify() skips nothing
#> strict ok no rule is strict
#> Nothing to install. Run weft::verify() to check the project.It writes nothing and starts no R process.
A routine
- Write. Render as often as you like.
- Before each commit, read
changes(). Look at every structural change and every new condition. - Commit the source and the ledger together. Answer the gate if it refuses: compute the number, fix the construct, or add the trailer.
- Before you hand the report on, turn draft mode off and run
verify().