Repository navigation
Image and link-target markup in a decision document is read in linear time - #370
Merged
Merged
Conversation
… 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
"" + "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 ``, `` and `` as no image, and read ` 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 `` or ``, 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, `` with its caption, and `". 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 `` and `` 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 ` >` 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]>
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changelog category (leave one):
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 byextract_documentinserver/memory/app/decision_extract.py) runs_IMAGE_MARKUPand_LINK_TARGETover 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 onmain, measured on a shared, loaded development machine:main"<img " * 30_000"" + "a](" * 40_000Alt 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 onmain, 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((,,, and 1) done). These malformed inputs read differently, each run on both versions:main[image: a [b] c][image: a [b]<img alt="a<b">](b), so the caption keeps done, as CommonMark reads it ><img ](x) >All but the last match less than
main, so they keep text rather than remove it; the last removes a trailing>thatmainkept.Compatibility, rollout and rollback
Tests
tests/unit/test_decision_extract.py:test_unclosed_image_markup_is_read_in_linear_time(the four bodies above and",test_image_markup_matches(ordinary and edge cases, and every row of the table above, withmain's matches where they agree) andtest_link_targets_are_removed_from_a_caption. The timing cases fail onmain; 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 typecheckandmake test-unitpass; the integration suite passes if server behaviour changed.main; no merged migration is edited.frontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.git commit -s), as CONTRIBUTING.md asks.🤖 Generated with Claude Code