Skip to content

feat: locate every finding and print it as json, sarif or github - #47

Merged
webern merged 3 commits into
mainfrom
m/deslag-finding
Sep 27, 2026
Merged

webern merged 3 commits into
mainfrom
m/deslag-finding

Conversation

@webern

@webern webern commented Sep 27, 2026

Copy link
Copy Markdown
Owner

Summary

Until now deslag could only print its text report on standard error. That report is written for the agent that has to fix the file, and a machine cannot read it: CI cannot annotate a line with it, and GitHub code scanning cannot take it. This PR adds deslag check --format text|json|sarif|github. The text report, the exit codes and what fails do not change.

To get there, every finding now knows the places in the file it points at. The PR has three commits:

  1. chore: list the lints once, in a Lint enum: a registry of the lints.
  2. feat: give every finding the places it points at: locations and marks.
  3. feat: print findings as json, sarif or github: the formats, tests and docs.

Every finding knows its places

Each lint already builds a detail struct for a failing file (banned_chars::Over, density::Over and so on), and its text report is rendered from that struct. The places now live in that same struct: a banned character keeps the Location of each time it occurs, a dense paragraph keeps the Location of its block, and so on. The line N: in the text report is read from those locations too.

Violation::marks() turns a finding's detail into a list of Marks, in the order the report lists them. Every format reads its places from marks() and its text from Finding::render(), so no format can point at a place, or say a thing, that the text report does not. That is why locations derive from each lint's detail rather than being computed a second time for output: there is one source of truth, and the text report is the proof of it.

A Mark has a kind:

  • occurrence: something wrong in its own right, which you fix at that place. A banned character or phrase, a dense paragraph, a line of the repo layout that is too wide, out of format, or names a path that does not exist.
  • evidence: part of what a verdict on the whole file rests on. The emphasized spans that together put a file over its share, the heading of a layout section with no code block, the code block of a layout with too many or too few entries.

Some verdicts have no place at all: a file over its byte budget, or a file with no layout section. Those findings have no marks.

Locations

Document::locate(range) is now the only place that works out lines and columns. A Location holds:

  • start, end: bytes, counted from 0, half-open.
  • line, column: counted from 1. Lines end at each LF, so the CR of a CRLF is the last character of its line. Columns count Unicode scalar values (code points), not bytes and not UTF-16 units.
  • end_line, end_column: end_line is the line of the last byte, and end_column is one past the column of the last character (exclusive). An empty range ends where it starts.

A byte order mark at the start of the file takes no column, which is how SARIF 2.1.0 counts (section 3.30.2).

Files that are not valid UTF-8 are read lossily, as they already were. On such a file, the bytes of a location are bytes of the decoded text, which can differ from the file's own bytes after the first invalid sequence.

The four formats

The standard error stream always carries the text report, in every format. Standard output carries the format's document:

  • text (the default): nothing.
  • json: one JSON document.
  • sarif: one SARIF 2.1.0 log.
  • github: one GitHub Actions workflow command per finding.

Exit codes are unchanged: 0 when nothing fails, 1 when a file fails a lint, 2 when deslag cannot run. On exit 2 nothing is printed on standard output. A clean run prints JSON with no findings, SARIF with no results, and nothing for github.

The examples below come from a small repo with one file named a, b: 100%.md (a name that needs escaping in every format).

json

Each finding has its path, the lint's id, the full text report and its marks. deslag instructions output-schema prints the JSON schema (draft 7), and format_version goes up only when a change could break a reader.

{
  "format_version": 1,
  "deslag_version": "0.1.0",
  "files_scanned": 2,
  "findings": [
    {
      "path": "a, b: 100%.md",
      "lint": "banned_chars",
      "report": "a, b: 100%.md has 1 banned character.\n\n...",
      "marks": [
        {
          "kind": "occurrence",
          "start": 43,
          "end": 46,
          "line": 3,
          "column": 37,
          "end_line": 3,
          "end_column": 38,
          "note": "U+2014 em dash \"—\"; write `-`"
        }
      ]
    }
  ]
}

sarif

Each lint is a rule, taken from the registry, with id, shortDescription, fullDescription, help and a default level of error. The run declares "columnKind": "unicodeCodePoints", and every region also carries byteOffset and byteLength, so a reader that counts columns another way can still find the exact place.

  • Each occurrence is a result of its own, at its place. Its message is the mark's note, a blank line, then the report without its heading.
  • A verdict on the whole file is one result. GitHub needs a region on every result, so it sits at line 1, column 1. Its evidence marks become relatedLocations, each with the mark's note as its message.
  • Paths are percent-encoded URIs relative to %SRCROOT%.

An occurrence (message cut short here):

{
  "ruleId": "banned_chars",
  "ruleIndex": 3,
  "level": "error",
  "message": { "text": "U+2014 em dash \"—\"; write `-`\n\na, b: 100%.md has 1 banned character. ..." },
  "locations": [{
    "physicalLocation": {
      "artifactLocation": { "uri": "a%2C%20b%3A%20100%25.md", "uriBaseId": "%SRCROOT%" },
      "region": { "startLine": 3, "startColumn": 37, "endLine": 3, "endColumn": 38, "byteOffset": 43, "byteLength": 3 }
    }
  }]
}

A verdict with its evidence (one related location shown):

{
  "ruleId": "max_emphasis",
  "ruleIndex": 1,
  "level": "error",
  "message": { "text": "a, b: 100%.md has 2 emphasized spans covering 6.81% of its prose. ..." },
  "locations": [{ "physicalLocation": { "artifactLocation": { "uri": "a%2C%20b%3A%20100%25.md", "uriBaseId": "%SRCROOT%" },
                  "region": { "startLine": 1, "startColumn": 1, "endLine": 1, "endColumn": 1 } } }],
  "relatedLocations": [{
    "id": 0,
    "message": { "text": "**must**" },
    "physicalLocation": { "artifactLocation": { "uri": "a%2C%20b%3A%20100%25.md", "uriBaseId": "%SRCROOT%" },
                          "region": { "startLine": 3, "startColumn": 4, "endLine": 3, "endColumn": 12, "byteOffset": 10, "byteLength": 8 } }
  }]
}

github

One ::error command per finding, not one per place, because GitHub shows at most 10 error annotations per step (actions/toolkit problem-matchers.md). The annotation sits at the finding's first mark, or on the whole file when it has none. col and endColumn are only given when the mark is on one line, since GitHub annotations support columns only then (check run annotations). The title is the lint's id and the message is the report without its heading. Messages escape %, CR and LF; properties also escape : and ,, as @actions/core does.

::error file=a%2C b%3A 100%25.md,line=3,endLine=3,col=37,endColumn=38,title=banned_chars::a, b: 100%25.md has 1 banned character.%0A%0AThis repo writes ...
::error file=docs/big.md,title=max_size_bytes::docs/big.md is larger than 400 bytes.%0A%0AThe file must be made more compact ...

README and the setup guide (deslag instructions) now describe --format, and what CI does with each: annotate with github, upload sarif to code scanning. Paths are relative to where deslag runs, so run it at the root of the repository.

The lint registry

Lint is a new enum with one variant per lint, and Lint::ALL lists them in the order they run. It gives each lint's id (the key of its table in the config, and its id in every format), its one-sentence summary (used by SARIF rules), and the words of the closing tally. The order lints run in, the tally, the SARIF rules and the lint field of the JSON all read from it, so a lint is listed once.

A drift test (every_lint_is_a_table_of_the_config in tests/instructions.rs) checks that the ids in Lint::ALL are exactly the lint tables of the config's schema. A lint added to one and not the other fails it.

Left out, on purpose

  • Fixes and replacements. SARIF has fixes and a mark could carry a suggested edit, but no lint can yet say which edits are safe to apply without a human, so none are offered. A later change will define safe fixes and can add them here.
  • Snippets and contextRegion: the byte range already lets a reader cut the text it wants.
  • partialFingerprints: GitHub's upload-sarif action computes them when a log has none.
  • A rule name and helpUri. The SARIF validator warns when a rule's name is the same as its id, even in another case, and there is no page per lint to link yet.

Choices worth a look

  • The method is Violation::marks(), not locations(), because a mark is a location plus its kind and note.
  • A byte order mark takes no column, following SARIF. Counting it as column 1 was the other option.
  • A layout with too many or too few entries now points at its code block, as evidence, so a reader can see what was counted. Its text report does not change.
  • end_column is exclusive, in every format. SARIF defines it that way. GitHub's docs do not say whether endColumn of a workflow command is inclusive; if GitHub reads it as inclusive, an annotation reaches one character past the place.
  • Each SARIF occurrence repeats the full message of its finding after its own note. That makes each result readable alone in the code scanning view, at the cost of some repetition.
  • SARIF columns are declared as unicodeCodePoints. GitHub's own SARIF example uses utf16CodeUnits, and its docs do not list columnKind among the properties it reads. No log was uploaded to GitHub, so whether it honors unicodeCodePoints is not verified. The two differ only after a character outside the Basic Multilingual Plane (an emoji, say) on the same line, and the byte offsets are exact either way.

Testing

  • make ci: exit 0

  • Not one existing tests/cases/**/*.stderr file changed, and the golden set (tests/golden/) did not move.

  • Every finding's text report over the corpus was dumped before and after the change and diffed: identical.

  • Each case in tests/cases gains a .json file beside its .stderr. The harness runs the case again with --format json, checks that the exit code and stderr are the same, and compares the JSON (with the version masked). A case can also have an .args file of extra arguments; a new case, max_size_bytes/config_path, uses it to read its config from --config-path. make fix-test-output rewrites the .json files too.

  • tests/output.rs runs all four formats on one repo where every lint fails at least once. It checks that every format prints the same report and exit code, that a run that cannot run prints nothing on stdout, what a clean run prints, that the JSON fits its schema, that the SARIF has a rule per lint and exactly the results the JSON implies, and that github prints one correctly escaped command per finding.

  • tests/document.rs pins locate on the edges: columns after multibyte characters, CRLF, a byte order mark, an empty range, and the end of the file.

  • tests/corpus.rs gains the_corpus_locations_hold_what_they_point_at: for every finding over the corpus, each location lies in the file, starts and ends on character boundaries, starts on the line it names, and holds what its lint says (the banned character, the phrase, the emphasized span, the fence of the code block, and so on). It was also run by hand over the full corpus, 2430 files: all pass.

  • SARIF validated by hand with the SARIF Multitool 5.7.0 from npm (@microsoft/sarif-multitool), on the log for the every-lint repo above and on a clean run's log. npx splits a ; argument through the shell, so the downloaded binary was run directly:

    Sarif.Multitool validate allfail.sarif --rule-kind "Sarif;Gh" --level "Error;Warning;Note" -o allfail.validation.sarif

    Gh adds GitHub's own ingestion rules. The result has no errors and no warnings, only notes: SARIF2002 (message arguments), SARIF2003 (version control provenance), SARIF2009 (conventional rule ids), SARIF2010 (code snippets), SARIF2011 (context regions) and SARIF2012 (help URI and friendly name). A first run failed GH2012, which requires fullDescription and help on every rule; both were added. The clean run's log has no results at all.

Research notes

  • The idea of deriving every output format, SARIF and GitHub annotations included, from one finding type, and of a test that keeps the rule ids in step with the rules, comes from unicode-safety-check (MIT), studied at commit de490427. No code was ported. ACKNOWLEDGEMENTS.md records it.
  • Ideas from Vale that deslag is not taking #34 recorded the rule to never work out a position from rendered or rewritten text. Every location here comes from a byte range the Document already had.
  • GitHub's SARIF requirements come from SARIF support for code scanning: id, shortDescription, fullDescription and help are required on each rule, and start and end line and column on each region.

References

The lints were listed by hand in check_file, in the tally that closes
a run, in the golden test and in the cases test. Lint now holds them
once: its id is the key of the lint's table in the config, and
Lint::ALL is the order they run in. Violation::lint says which lint
found a violation, so the tally and the golden files key off it.

A new test checks that the ids are exactly the tables that the
config's sections allow under lints, so an id always names a table.
A finding held lines where it held any position, and a lint that
needed one computed it itself. Now each lint keeps a Location where
it kept a line: the byte range in the file, and the line and column
of its start and end. Document::locate is the one place that
computes them, and the reports read the line from the same value, so
their text does not change.

Byte ranges are 0-based and exclude their end. Lines count from 1
and end at each LF. Columns count characters from 1; a byte order
mark that opens the file takes no column. The end line is the line
of the last byte, and the end column is one past the last character.

Violation::marks lists the places a finding points at, each with a
note in the lint's words and a kind: an occurrence, wrong in its own
right, or evidence for a verdict on the whole file, such as an
emphasized span. A file over its byte budget has none.

max_emphasis mapped only the start of a run of capitals back to the
file. It now maps both ends through the pieces it read, with the
layout's lines, which repo_layout reads the same way. A layout that
lists too few or too many entries now points at its code block.

A corpus test checks that each location holds what it points at:
the banned character, the phrase, the emphasis markers, the layout
line with its path.
`deslag check --format` picks what goes to standard output: nothing
(text, the default), one JSON document, one SARIF 2.1.0 log, or one
GitHub Actions error command per finding. The text report still goes
to standard error in every format, and the exit codes do not change.
A run that cannot run prints nothing on standard output.

Every format is built from the report alone: the text of each finding
and its marks. So no format can say what the text report does not.

- json: each finding with its lint, its text report and its marks.
  `deslag instructions output-schema` prints the schema, and the
  document carries a format version.
- sarif: a rule per lint, taken from `Lint::ALL`. Each occurrence is a
  result. A verdict on the whole file is one result at the first
  character, with its evidence as related locations. Columns count
  code points, and each region also gives its bytes.
- github: one annotation per finding, at its first mark, since GitHub
  shows only 10 error annotations per step.

Each lint gains a one-sentence summary for its SARIF rule.

The cases gain a .json file beside each .stderr, which the harness
checks with `--format json`, and an .args file for extra arguments. A
new case reads its config from `--config-path`. tests/output.rs runs
every format over a repo where every lint fails a file. The schema
checker moves to tests/common so both schemas share it.

README, the guide and the as-built doc describe the formats.
@webern webern added enhancement New feature or request ai Primarily authored by AI (whether PR, Issue, Whatever) area/cli Deals with the command line interface. area/lint Deals with the linting algorithms themselves, how they work. labels Sep 27, 2026
@webern
webern marked this pull request as ready for review September 27, 2026 04:52
@webern
webern merged commit 1dcafe2 into main Sep 27, 2026
2 checks passed
@webern
webern deleted the m/deslag-finding branch September 27, 2026 04:52
webern added a commit that referenced this pull request Sep 27, 2026
## Summary

`docs/design/deslag.asbuilt.md` describes the code as it is. At a2d8ea1
it is 16378 bytes of its 16384 `max_size_bytes`, and it has been within
180 bytes of that budget since #23 (16352). Each of #44, #45 and #47
made room for its new paragraphs by cutting others, and #45 moved the
corpus text out to `corpus.asbuilt.md`. The `deslag-design-docs` skill
says a budget is not raised without the owner, so every change to the
code has had to squeeze the doc.

The budget was also kept by hand: `.agents/deslag.toml` did not select
the doc. Selected at a2d8ea1, it fails `max_emphasis` (20 bold spans,
1.24% of its prose) and `density` (8 paragraphs over 500 characters).

This PR splits the doc the way `corpus.asbuilt.md` already is: one
as-built doc per subsystem, each with its own budget, and every one of
them linted.

- `deslag.asbuilt.md` is now the overview: what deslag is, a run, the
modules at directory level and what calls what, an index of the
subsystem docs, the command line and exit codes, other files, the tests
and the build. Its budget drops from 16384 to 8192, so it cannot grow
back into the place every detail goes.
- `config.asbuilt.md`: the config, glob patterns, the walk and `deslag
explain`.
- `document.asbuilt.md`: the `Document` and frontmatter. It is named for
`src/document/`, the module it holds.
- `lints.asbuilt.md`: checking, marks, one section per lint in
`Lint::ALL` order, reports and `--format`. A new lint is one new
section.

Each subsystem doc opens with its entry points and callers, then its
`src/` tree, then one section per concern. `subsystems:` in each doc's
frontmatter holds `src/` module names, each in exactly one doc: the one
with its detail.

| Doc | Bytes | Budget | `subsystems:` |
|---|---|---|---|
| `deslag.asbuilt.md` | 6786 | 8192 (was 16384) | `cli`, `instructions`
|
| `config.asbuilt.md` | 3889 | 5000 | `config`, `glob`, `explain` |
| `document.asbuilt.md` | 2194 | 3000 | `document`, `parse` |
| `lints.asbuilt.md` | 5884 | 7500 | `lint`, `output` |
| `corpus.asbuilt.md` | 5998 | 8192 | `corpus` (unchanged) |

### Where each old section went

| Section at a2d8ea1 | Now |
|---|---|
| intro, A run | `deslag.asbuilt.md`, as they were |
| Modules | `deslag.asbuilt.md` keeps the tree at directory level and
the paragraph on what calls what; each subsystem doc takes its
file-level lines |
| The document | `document.asbuilt.md`: Blocks; Tokens, sentences and
locations |
| Where a file's budget comes from | `lints.asbuilt.md`: max_size_bytes
|
| The config | `config.asbuilt.md`: The config, and Glob patterns for
the `glob::Pattern` paragraph |
| Frontmatter | `document.asbuilt.md`: Frontmatter |
| Explaining a file | `config.asbuilt.md`: Explaining a file |
| Walking the repo | `config.asbuilt.md`: Walking the repo |
| Checking and reporting | `lints.asbuilt.md`: Checking, Marks, a
section per lint, Reports, --format |
| The command line, Other files, Tests, Build | `deslag.asbuilt.md`, as
they were |

### Commits

1. `docs: split the as-built doc by subsystem`: the text moves as it
was. New text is limited to each subsystem doc's opening sentences, the
overview's index, the section headings, and short descriptions for the
directories in the overview's tree, worded after the layout in
`AGENTS.md`. `README.md` says the overview indexes the subsystem docs.
2. `docs: fix the lint findings in the as-built docs`: 8 paragraphs
split at a sentence end, and 13 of 20 bold spans un-bolded. Each doc
keeps its first two; the sentence that defines each of the others stays.
No sentence is reworded.
3. `docs: correct the as-built docs against the code`: an ellipsis,
U+2026, also ends a sentence (`src/document/sentences.rs:113`); `make
clean` also runs `cargo clean` (`Makefile:98-99`); the trees now name
`parse/mod.rs`, `ACKNOWLEDGEMENTS.md` and `.agents/skills/`; `explain`
is in `subsystems:`, where no doc named it (`src/lib.rs:27`).
4. `test: lint every as-built doc and its subsystems`:
- `.agents/deslag.toml` selects `/docs/design/*.asbuilt.md` in place of
`corpus.asbuilt.md` alone. The glob does not select `deslag.desired.md`,
which is written by hand and stays unlinted, and topic docs stay listed
one by one.
- A selected file with no budget is counted but never size-checked
(`src/lint/max_size_bytes.rs:35-38`). An override on the same glob with
`value = 4096` closes that: a frontmatter budget wins over it, and a doc
that declares none fails at 4 KB.
- `tests/asbuilt.rs` fails when a module under `src/` is named in no
as-built doc's `subsystems:`, or in more than one, so a new module gets
its doc on purpose. It finds the frontmatter with `Document` and reads
it with `serde-saphyr`; no new dependency.

### Nothing lost in the move

A script outside the repo cuts the a2d8ea1 doc into 177 units (each
sentence, code line and heading) and 166 backticked names, and looks for
each in the four new docs, with whitespace and bold markers ignored.
After each commit:

| After commit | Units missing | Names missing |
|---|---|---|
| 1 | 4 | 0 |
| 2 | 4 | 0 |
| 3 | 6 | 0 |
| 4 | 6 | 0 |

The six, each on purpose:

- Three headings, `The document`, `Where a file's budget comes from` and
`Checking and reporting`, replaced by the sections in the table above.
- "Most specific first: the `max_size_bytes` key ..." now opens "A
file's budget comes from, most specific first: ...". Its heading used to
say what it was about; the section is now named for the lint.
- The sentence rule now names the ellipsis (commit 3).
- "`make clean` removes both directories" now adds "and runs `cargo
clean`" (commit 3).

The script proves each unit is somewhere, not that it is in the right
place, so the trees were read by eye.

### Left as it was

- The overview's paragraph on what calls what does not say that
`explain` calls `config`, `glob` and `parse` too. It is not wrong, so it
was not rewritten.
- The tests stay in the overview; no doc owns them yet.
- `corpus.asbuilt.md`, `corpus.md` and their lines in
`.agents/deslag.toml` are untouched; the glob now selects
`corpus.asbuilt.md` in place of its own line.

## Testing

- [x] `make ci`: exit 0
- [x] `deslag check` on the repo: exit 0 with all five as-built docs
selected; `deslag explain docs/design/deslag.desired.md` says it is not
selected.
- [x] A 5149-byte `docs/design/nofm.asbuilt.md` with no frontmatter
fails at 4096 bytes; `deslag explain` shows the frontmatter budget
winning over the override for `lints.asbuilt.md`. File removed after.
- [x] `tests/asbuilt.rs` fails with `parse: in no doc` when `parse` is
dropped from `document.asbuilt.md`, and with `lint: in
corpus.asbuilt.md, lints.asbuilt.md` when `lint` is added to
`corpus.asbuilt.md`, which also shows it reads a YAML block list. Both
reverted.
- [x] The residue script after each commit, as above.

## References

- No issue. Related to #23, #44, #45 and #47, each of which squeezed the
old doc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Primarily authored by AI (whether PR, Issue, Whatever) area/cli Deals with the command line interface. area/lint Deals with the linting algorithms themselves, how they work. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant