Skip to content

Image and link-target markup in a decision document is read in linear time - #370

Merged
zlareb1 merged 7 commits into
mainfrom
fix/decision-extract-markup-linear
Nov 7, 2026
Merged

zlareb1 merged 7 commits into
mainfrom
fix/decision-extract-markup-linear

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

A synced knowledge document full of unclosed image or link markup no longer holds the server for seconds while decisions are extracted from it.

Why

image_only_sections (server/memory/domain/decision_extract.py, called by extract_document in server/memory/app/decision_extract.py) runs _IMAGE_MARKUP and _LINK_TARGET over each synced knowledge document's content, synchronously on the event loop. Each negated class excluded only its closer ([^\]]*, [^)]*, [^>]*), so with no closer the scan ran from every opener to the end of the text: quadratic in the number of openers. CPU seconds on main, measured on a shared, loaded development machine:

Body main
"<img " * 30_000 5.5 s
"![" * 30_000 4.6 s
"[image:" * 6_000 2.7 s (20 000: 29.6 s)
"![x](y)" + "a](" * 40_000 3.5 s

Alt text and the [image: …] placeholder's text now stop at the next [ ([^\[\]]*+), a tag at the next < (<img\b[^<>]*+>), and an image or link target (_PAREN_TARGET) at its first ), as on main, but never past the next ](; it may hold up to three line ends anywhere, the most CommonMark allows (after (, between the destination and the title, before )), and a quoted title may span lines. Reference labels are unchanged: a later opener holds a ] that ends their scan. The module's other patterns were checked and have no such scan.

Scope and risk

Two patterns in the decision extractor. On ordinary markup the matches are the same: inline and reference images, linked badges, titles (on one line or several), upper-case <IMG>, the placeholders, multi-line tags and alt text, a destination or title after a line end, a title holding ], and targets holding an unbalanced or escaped ( (![x](diagrams/flow(v2.png), ![x](a\(b.png), ![x](f.png "see (a"), ![x](a(b.png) and 1) done). These malformed inputs read differently, each run on both versions:

Input main here
[image: a [b] c] [image: a [b] no match
<img alt="a<b"> the whole tag no match
![x](a](b) the whole image no image; the link-target pattern matches only ](b), so the caption keeps ![x](a]
![x](a + four line ends + e) done the whole image no match
![open ![x](a.png) from the outer ![ only ![x](a.png), as CommonMark reads it
![a [<img ](x) > ![a [<img ](x) the later span <img ](x) >

All but the last match less than main, so they keep text rather than remove it; the last removes a trailing > that main kept.

Compatibility, rollout and rollback

  • Wire: none. Operator steps: none. Rollback: revert.

Tests

  • tests/unit/test_decision_extract.py: test_unclosed_image_markup_is_read_in_linear_time (the four bodies above and "![x](a" * 30_000, an unclosed image target, each under 0.5 s CPU), test_image_markup_matches (ordinary and edge cases, and every row of the table above, with main's matches where they agree) and test_link_targets_are_removed_from_a_caption. The timing cases fail on main; the file passes here (439).
  • test_knowledge_source_sync.py, test_notion_source_sync.py, test_bench_design_rationale.py: pass.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass; the integration suite passes if server behaviour changed.
  • Generated files are regenerated in this PR, not by hand.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships bumps both plugin manifests' version.
  • The frontend builds and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers.
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

zlareb1 and others added 7 commits November 7, 2026 13:00
… time

image_only_sections runs _IMAGE_MARKUP and _LINK_TARGET over every synced
knowledge document's content, on the event loop. Each pattern scanned from
every opener to the end of the text when no closer followed, so a body of
unclosed openers cost quadratic time:

  "<img " * 30_000       5.5 s
  "![" * 30_000          4.6 s
  "[image:" * 6_000      2.7 s
  "![x](y)" + "a](" * 40_000   3.5 s

Pattern: a negated class that excludes only the closer (`[^\]]*`, `[^)]*`,
`[^>]*`) lets the scan run past every later opener.

Fix: each class now also excludes its opener, so a scan stops at the next
`[`, `(` or `<`: alt text and the placeholder's text are `[^\[\]]*`, a tag
is `<img\b[^<>]*>`, and a link or image target is `(...)` with at most one
nested pair of parentheses, so a wiki-style target such as `Run_(sort)` is
read whole instead of cut at its first `)`. Multi-line tags and alt text
still match. The same inputs now take 0.04-0.1 s at 200_000 openers, and
the four cases above stay under 0.5 s in the new test, which fails on main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
The one-nested-pair target read `![x](diagrams/flow(v2.png)`,
`![x](a\(b.png)` and `![x](f.png "see (a")` as no image, and read
`![x](a(b.png) and 1) done` up to the stray `)`, across lines too, which
deletes text from no_words and caption_text and can make a section read as
image-only.

A target now ends at its first `)`, as on main, and stays linear by never
running past the next `](` or past a line end outside a quoted title: a
CommonMark destination holds no line ending, while a title may span lines.
The comment above the patterns now says what each part stops at.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
66a798a read no image in `![x](\nf.png)` or `![x](f.png\n  "title")`,
and caption_text kept such a link's target: a target could not cross a
line end outside a quoted title, though CommonMark allows one before the
destination and one between the destination and the title.

A target is now its pieces with at most one line end between them, still
bounded by the next `](`. The pieces are a verbose pattern, one branch per
line with what each one reads. The tests add both images, `![x](a](b)`
with its caption, and `"![x](a" * 30_000` to the timing cases (about 3 s
on main, 0.02 s now).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…allows

0d3fe3b let a target hold one line end in all, so `![x](\nf.png\n"t")`
and `![x](f.png\n"t"\n)` read as no image, though main read both and
CommonMark allows a line end after `(`, between the destination and the
title, and before `)`. A target now holds up to three, still bounded by
the next `](`, so the scan stays linear.

The quoted-title comment now says a title holding `]` is read piece by
piece, on one line. The image tests add both inputs and the cases that
still read differently from main: `[image: a [b] c]` and
`<img alt="a<b">` are no image, and `![a [<img ](x) >` matches the later
span `<img ](x) >`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ree line ends

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…le where it applies

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 4198874 into main Nov 7, 2026
38 checks passed
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
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.

1 participant