Skip to content

Detailed non-convergence diagnostics for introspections - #7364

Merged
laurmaedje merged 4 commits into
mainfrom
feature/introspector-warnings
Nov 14, 2025
Merged

laurmaedje merged 4 commits into
mainfrom
feature/introspector-warnings

Conversation

@laurmaedje

Copy link
Copy Markdown
Member

Description

This PR adds rich diagnostics for non-convergence of the document. Previously, only a single global warning would be emitted. Now, there are individual warnings for each specific introspection that did not converge.

This is implemented by tracing all introspections that occur during compilation (in a manner similar to how warnings are accumulated), re-running them against the documents for all iterations, and checking whether they converged.

Each introspection is now represented by a type that knows how to resolve itself against a document and how to diagnose its own non-convergence. This makes it easy to introduce new introspections with high-quality diagnostics.

Closes #4528.

Design

Previously, when considering the problem of introspection diagnostics, I struggled to find a way to retain spans (pointing to source code locations) for diagnostics without them interfering with memoization and query deduplication (important for efficiency). The crucial insight of this PR is that we can decouple the high-level introspections (which are recorded with spans) from the low-level methods on the introspector (spanless and deduplicated).

Aside from the ability to record spans, this decoupling also lets us produce more high-level diagnostics. For example, while the QueryIntrospection maps directly to introspector.query, the StateAtIntrospection internally uses introspector.query to resolve the state's value, but its convergence behavior and its diagnostics are at the level of abstractions of state.

Examples

Many of these examples use a switch function, which invokes a callback with the current layout iteration number. It's definition is as follows:

The switch function
#let switch(f) = context {
  // The `here()` trick is just to produce a counter unique to this context
  // block.
  let c = counter(metadata.where(value: here()))
  let i = c.final().first()
  let n = i + 1
  c.update(if n < 5 { i + 1 } else { i })
  context f(n)
}
Position
#switch(n => v(n * 10pt) + [= Heading])
#context locate(heading).position().y
warning: document did not converge within five attempts
 = hint: see 1 additional warning for more details
 = hint: see https://typst.app/help/convergence for help

warning: heading position did not stabilize
  ┌─ <stdin>:3:9
  │
2 │ #switch(n => v(n * 10pt) + [= Heading])
  │                             --------- heading was created here
3 │ #context locate(heading).position().y
  │          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  │
  = hint: the following positions were observed:
    - run 1: page 1 at (0pt, 0pt)
    - run 2: page 1 at (71pt, 81pt)
    - run 3: page 1 at (71pt, 91pt)
    - run 4: page 1 at (71pt, 101pt)
    - run 5: page 1 at (71pt, 111pt)
    - final: page 1 at (71pt, 121pt)
Query 1
#context {
  let elems = query(heading)
  let count = 1 + elems.len()
  count * [= Fake <fake>]
}
warning: document did not converge within five attempts
 = hint: see 1 additional warning for more details
 = hint: see https://typst.app/help/convergence for help

warning: number of heading elements did not stabilize
  ┌─ <stdin>:3:14
  │
3 │   let elems = query(heading)
  │               ^^^^^^^^^^^^^^
  │
  = hint: the following numbers of elements were observed:
    - run 1: 0
    - run 2: 1
    - run 3: 2
    - run 4: 3
    - run 5: 4
    - final: 5
Query 2
#switch(n => if n == 4 [*A*] else [*B*])
#context query(strong)
warning: document did not converge within five attempts
 = hint: see 1 additional warning for more details
 = hint: see https://typst.app/help/convergence for help

warning: query for strong elements did not stabilize
  ┌─ <stdin>:3:9
  │
3 │ #context query(strong)
  │          ^^^^^^^^^^^^^
Heading & Reference
#set heading(numbering: "1.")
= A
#switch(n => if n == 5 [= B <b>])
@b
warning: document did not converge within five attempts
 = hint: see 2 additional warnings for more details
 = hint: see https://typst.app/help/convergence for help

warning: value of `counter(heading)` did not converge
  ┌─ <stdin>:4:24
  │
4 │ #switch(n => if n == 5 [= B <b>])
  │                         ^^^
  │
  = hint: the following values were observed:
    - run 1: 0
    - run 2: 1
    - run 3: 1
    - run 4: 1
    - run 5: 1
    - final: 2

warning: query for a unique element labelled `<b>` did not stabilize
  ┌─ <stdin>:5:0
  │
5 │ @b
  │ ^^
  │
  = hint: the following numbers of elements were observed:
    - run 1: 0
    - run 2: 0
    - run 3: 0
    - run 4: 0
    - run 5: 0
    - final: 1

error: label `<b>` does not exist in the document
  ┌─ <stdin>:5:0
  │
5 │ @b
  │ ^^
State
#let hi = state("hi", 0)
#context hi.update(hi.get() + 1)
#context hi.update(hi.get() + 2)
#context hi.update(hi.get() + 3)
#context hi.update(hi.get() + 4)
#context hi.update(hi.get() + 5)
#context hi.update(hi.get() + 6)

#let s = state("s", 1)
#context s.update(s.final() + 1)
warning: document did not converge within five attempts
 = hint: see 2 additional warnings for more details
 = hint: see https://typst.app/help/convergence for help

warning: value of `state("hi")` did not converge
  ┌─ <stdin>:7:19
  │
7 │ #context hi.update(hi.get() + 6)
  │                    ^^^^^^^^
  │
  = hint: the following values were observed:
    - run 1: `0`
    - run 2: `5`
    - run 3: `9`
    - run 4: `12`
    - run 5: `14`
    - final: `15`
  = hint: see https://typst.app/help/state-convergence for help

warning: value of `state("s")` did not converge
   ┌─ <stdin>:10:18
   │
10 │ #context s.update(s.final() + 1)
   │                   ^^^^^^^^^
   │
   = hint: the following values were observed:
     - run 1: `1`
     - run 2: `2`
     - run 3: `3`
     - run 4: `4`
     - run 5: `5`
     - final: `6`
   = hint: see https://typst.app/help/state-convergence for help
State (errored)
#let s = state("s")
#switch(n => s.update(if n == 5 { _ => panic() } else { "ok" }))
#context s.get()
warning: document did not converge within five attempts
 = hint: see 1 additional warning for more details
 = hint: see https://typst.app/help/convergence for help

warning: value of `state("s")` did not converge
  ┌─ <stdin>:4:9
  │
4 │ #context s.get()
  │          ^^^^^^^
  │
  = hint: the following values were observed:
    - run 1: `none`
    - run 2: `"ok"`
    - run 3: `"ok"`
    - run 4: `"ok"`
    - run 5: `"ok"`
    - final: (errored)
  = hint: see https://typst.app/help/state-convergence for help
Counter
#let c = counter("hi")

#context c.at(<end>)
#context c.update({
  let v = c.final()
  v + (1 + v.last() * 2,)
})

#metadata(none) <end>
warning: document did not converge within five attempts
 = hint: see 2 additional warnings for more details
 = hint: see https://typst.app/help/convergence for help

warning: value of `counter("hi")` did not converge
  ┌─ <stdin>:3:9
  │
3 │ #context c.at(<end>)
  │          ^^^^^^^^^^^
  │
  = hint: the following values were observed:
    - run 1: 0
    - run 2: 0, 1
    - run 3: 0, 1, 3
    - run 4: 0, 1, 3, 7
    - run 5: 0, 1, 3, 7, 15
    - final: 0, 1, 3, 7, 15, 31

warning: value of `counter("hi")` did not converge
  ┌─ <stdin>:5:10
  │
5 │   let v = c.final()
  │           ^^^^^^^^^
  │
  = hint: the following values were observed:
    - run 1: 0
    - run 2: 0, 1
    - run 3: 0, 1, 3
    - run 4: 0, 1, 3, 7
    - run 5: 0, 1, 3, 7, 15
    - final: 0, 1, 3, 7, 15, 31
Bibliography 1
#switch(n => if n >= 5 { bibliography("works.bib") })
@netwok
warning: document did not converge within five attempts
 = hint: see 1 additional warning for more details
 = hint: see https://typst.app/help/convergence for help

warning: number of bibliography elements did not stabilize
  ┌─ <stdin>:3:0
  │
3 │ @netwok
  │ ^^^^^^^
  │
  = hint: the following numbers of elements were observed:
    - run 1: 0
    - run 2: 0
    - run 3: 0
    - run 4: 0
    - run 5: 0
    - final: 1

error: label `<netwok>` does not exist in the document
  ┌─ <stdin>:3:0
  │
3 │ @netwok
  │ ^^^^^^^
Bibliography 2
#switch(n => if n >= 4 { bibliography("works.bib") })
@netwok
warning: document did not converge within five attempts
 = hint: see 2 additional warnings for more details
 = hint: see https://typst.app/help/convergence for help

warning: citation grouping did not stabilize
  ┌─ <stdin>:2:25
  │
2 │ #switch(n => if n >= 4 { bibliography("works.bib") })
  │                          ^^^^^^^^^^^^^^^^^^^^^^^^^
  │
  = hint: this can happen if the citations and bibliographies in the document did not stabilize by the end of the third layout iteration

warning: citation grouping did not stabilize
  ┌─ <stdin>:3:0
  │
3 │ @netwok
  │ ^^^^^^^
  │
  = hint: this can happen if the citations and bibliographies in the document did not stabilize by the end of the third layout iteration

error: cannot format citation in isolation
  ┌─ <stdin>:3:0
  │
3 │ @netwok
  │ ^^^^^^^
  │
  = hint: check whether this citation is measured without being inserted into the document
Page
#switch(n => if n == 4 { pagebreak() + [= Heading] })
#context locate(heading).page()
warning: document did not converge within five attempts
 = hint: see 2 additional warnings for more details
 = hint: see https://typst.app/help/convergence for help

warning: query for a unique heading element did not stabilize
  ┌─ <stdin>:3:9
  │
3 │ #context locate(heading).page()
  │          ^^^^^^^^^^^^^^^
  │
  = hint: the following numbers of elements were observed:
    - run 1: 0
    - run 2: 0
    - run 3: 0
    - run 4: 0
    - run 5: 1
    - final: 0

warning: page number of the element did not stabilize
  ┌─ <stdin>:3:9
  │
3 │ #context locate(heading).page()
  │          ^^^^^^^^^^^^^^^^^^^^^^
  │
  = hint: the following page numbers were observed:
    - run 1: page 1
    - run 2: page 1
    - run 3: page 1
    - run 4: page 1
    - run 5: page 2
    - final: page 1
Supplement & Numbering
#set page(numbering: "1", margin: (bottom: 20pt))
#show: doc => switch(n => {
  set page(supplement: "Pagus", numbering: "I") if n == 4
  doc
})

= Hello <hello>
#ref(<hello>, form: "page")
warning: document did not converge within five attempts
 = hint: see 2 additional warnings for more details
 = hint: see https://typst.app/help/convergence for help

warning: numbering of the page on which the heading is located did not stabilize
  ┌─ <stdin>:9:1
  │
8 │ = Hello <hello>
  │ ------- heading was created here
9 │ #ref(<hello>, form: "page")
  │  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  │
  = hint: the following numberings were observed:
    - run 1: `none`
    - run 2: `"1"`
    - run 3: `"1"`
    - run 4: `"1"`
    - run 5: `"I"`
    - final: `"1"`

warning: supplement of the page on which the heading is located did not stabilize
  ┌─ <stdin>:9:1
  │
8 │ = Hello <hello>
  │ ------- heading was created here
9 │ #ref(<hello>, form: "page")
  │  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  │
  = hint: the following supplements were observed:
    - run 1: `[]`
    - run 2: `[page]`
    - run 3: `[page]`
    - run 4: `[page]`
    - run 5: `[Pagus]`
    - final: `[page]`
Measurement
#switch(n => {
  let body = [= Hello]
  _ = measure(body)
  if n == 4 {
    body
  }
})
warning: document did not converge within five attempts
 = hint: see 1 additional warning for more details
 = hint: see https://typst.app/help/convergence for help

warning: a measured element did not stabilize
  ┌─ <stdin>:4:6
  │
3 │   let body = [= Hello]
  │               ------- the closest match for this element did not stabilize
4 │   _ = measure(body)
  │       ^^^^^^^^^^^^^
  │
  = hint: measurement tries to resolve introspections by finding the closest matching elements in the real document

Help links (Feedback wanted!)

Some diagnostics contains links.

The links are currently still 404. My idea with these was to provide stable, readable permalinks whose concrete destination we can adjust as the documentation and teaching materials evolve. I'd imagine adding more of those help links over time.

For example, such a help link could start out as pointing to a Forum post and later migrate to the documentation as its expanded. Having a layer of decoupling seems more robust to me that trying to link directly into the docs (that link is bound to get broken at some point and we can't update old compilers to point elsewhere).

For now, there aren't great materials for these two links. If someone knows good Forum posts for these two, that might be helpful. Otherwise, I'd probably link to https://typst.app/docs/reference/context/#compiler-iterations and https://typst.app/docs/reference/introspection/state/#caution for now.

Overall, I'm also interested in feedback on the help link idea! An alternative to the link idea would be stable error codes, but this is a much more complex endeavour and not really likely to happen particularly soon. So I figured, these links might be a pragmatic solution to help users now.

@oliverpool

Copy link
Copy Markdown
Contributor

I did a quick test with https://typst.app/project/r9GASqmDOYasvWLDv5x4cI and the warning looks good 👍

warning: document did not converge within five attempts
 = hint: see 1 additional warning for more details
 = hint: see https://typst.app/help/convergence for help

warning: page number of the layout did not stabilize
   ┌─ ../typst-converge-bug/autobreak.typ:15:25
   │
 7 │ ╭   layout(size => {
 8 │ │     let neededHeight = measure(block(width: size.width,content)).height
 9 │ │     if neededHeight <= size.height {
10 │ │       // needs less than one page: make it non-breakable
   · │
15 │ │     let isOdd = calc.odd(here().page())
   │ │                          ^^^^^^^^^^^^^
   · │
27 │ │     }
28 │ │   })
   │ ╰────' layout was created here
   │
   = hint: the following page numbers were observed:
     - run 1: page 1
     - run 2: page 16
     - run 3: page 12
     - run 4: page 14
     - run 5: page 14
     - final: page 15

What would be even nicer, would be to have some kind of stacktrace. In the example above autobreak.typ defines a function that is called 10 times from main.typ. With the current message, I can't know which instantiation is problematic.

Nevertheless this PR is a great step forward and would be enough to close my initial issue!

@laurmaedje

Copy link
Copy Markdown
Member Author

What would be even nicer, would be to have some kind of stacktrace. In the example above autobreak.typ defines a function that is called 10 times from main.typ. With the current message, I can't know which instantiation is problematic.

I have some plans for better traces for introspections (and context expressions, too) for the future. They won't work exactly like the normal stack traces as there are some rather fundamental implementation limitations, but it should also help with tracking down the causes. Essentially, it'll be a trace of the content tree rather than a trace of the call stack.

@oliverpool

Copy link
Copy Markdown
Contributor

This diagnostics helped me tweak my autobreak feature 🎉

The actual bug is not resolved, but at least I can understand a little bit better how Typst works!

@laurmaedje
laurmaedje force-pushed the feature/introspector-warnings branch from 981caa8 to 387c394 Compare November 14, 2025 16:42
@laurmaedje
laurmaedje merged commit 387c394 into main Nov 14, 2025
@laurmaedje
laurmaedje deleted the feature/introspector-warnings branch November 14, 2025 18:21
@Andrew15-5

Copy link
Copy Markdown
Contributor

I'm not a fan of the hacky \n in the warning comment. Why not let the multi-line warning be a multi-line comment? Is it simply because of compactness? For me, readability and ease of creation of the comments is more important that compact comments.

@laurmaedje

Copy link
Copy Markdown
Member Author

I'm not a fan of the hacky \n in the warning comment. Why not let the multi-line warning be a multi-line comment?

Because that would have been a more complex change to the test parsing and I did not consider it worth the time and (potentially, depends on how complex it would) maintenance effort. I also don't think it's that hacky tbh.

@Andrew15-5

Copy link
Copy Markdown
Contributor

Fair enough.

@oliverpool oliverpool mentioned this pull request Dec 3, 2025
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jan 16, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jan 16, 2026
ParaN3xus added a commit to ParaN3xus/typst.ts that referenced this pull request Jan 16, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jan 16, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jan 16, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jan 16, 2026
ParaN3xus added a commit to ParaN3xus/tinymist that referenced this pull request Jan 16, 2026
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.

Ease debugging of "layout did not converge within 5 attempts" issues

3 participants