jonny@neuromatch.social ("jonny (nonvenomous)") wrote:
I fucking can't with this shit sometimes. So there is a skill for rendering local HTML "artifacts", little local websites. However the LLM frequently outputs invalid HTML/JS, and it wants to enforce some other rules like not loading external js. There is already a check for this when artifacts are built into a runnable state, but that guess and check is apparently expensive, so rather than "fixing that and just failing fast in a single codepath" this PR adds a duplicate, slightly diverged layer of checks that can be run without building.
One small part is so wonderfully broken and cursed, emblematic of LLM code: the check for whether all
scripttag openings have a corresponding close.So there already IS a proper HTML/js AST parser, which is run, but fuck that too easy. First we call
findScriptElements, which searches for the start of a script start tag with regex, and then uses a handrolled character-level parser to find where it's closed. Amazing! Why? Fuck off that's why! That finds the<script>tag, and then we look for the corresponding</script>tag with a similar but slightly different regex+custom parser combination. Whats that you say? Thats invalid? Not how HTML works? Fuck off! If no end tag is found the start tag is ignored andfindScriptElementsbreaks. So at this point we already know if there are unbalanced script tags, even if we are incorrectly parsing HTML with regex, we just don't use that information.But wait! There so much more! You didn't ask for more? Fuck you! Since the scripts get transformed, and scripts can have strings that look like script tags, we then get a version of the document with all the BODIES of its script tags stripped out. To do that it also uses
findScriptElements, but wait, wasn't that based on regex and would incorrectly match against the very thing we're trying to strip out? Fuck you!What were we doing? Finding unclosed script tags! So we have a list of closed script element locations, a string with all the bodies of its script elements stripped out, so now of course we need a new function
countScriptTagStarts, which is of course just a plain regex search for the opening script tags! With everything we need in hand, which is... Whatever... We then compare if the number of script start tags in the script element stripped html matches the count of the script elements we extracted at the beginning. If that doesn't match we say that... Something... Is wrong!Still not done! Still not released from this torment! You know how validators "scan the whole thing and collect all the errors" and report them, because then you can fix them all at once? Not here! Emit only one error and then bash the LLM at it in a loop! Notice how it says "errors cascade and presenting multiple is just noise" - thats strictly a feature of how shitty their validator is! Usually parse errors are just a different kind of error that shortcircuits further validation that assumes syntactically valid input, so then validation errors don't always propagate! So to compensate for the shitty random walk code churn, we ensure all code that get written in the future has even more preposterously expensive shitty random walk code churn!
Coding!!!! Is !!!! Solved!!!!!!!!!
![[js code as described in post: call findScriptElements, compare script tag count in body without script tags against its length. the real gem here is the comment and the error text] const elements = findScriptElements(html); // Ask the count question the way the build asks it. assertOnlyApprovedScripts // runs over the REWRITTEN document, whose script data is gone, so a <script // the author merely wrote inside a string is not a second element there and // must not be one here either -- counting it over the raw page refuses a // document every browser renders and the build accepts. // // This projection is marginally quieter than the build in one case: the build // re-renders a non-executable script WITH its body, so a text/template // holding <script text still trips its count. That is the safe direction -- // the build refuses the page and the check simply had nothing to say. const withoutScriptBodies = documentWithoutScriptBodies(html); if (countScriptTagStarts(withoutScriptBodies) !== elements.length) { return { ok: false, defect: { kind: "unreadable_script", message: "index.html contains a <!-- raw HTML omitted --> element private rendering cannot read to its " + "end -- usually an unterminated tag, or a literal <!-- raw HTML omitted --> inside a string. " + "A browser would not close it either, so the page is broken as written.", }, }; }](https://files.mastodon.social/cache/media_attachments/files/117/352/037/378/701/372/original/e07b8f174f5b316f.jpg)
![// Every <script> element in document order. Scanning resumes past each // element, so <script text inside a script body never opens a second one; an // unterminated element ends the scan and is caught by the count check in // assertOnlyApprovedScripts. export function findScriptElements(html: string): ScriptElement[] { const elements: ScriptElement[] = []; const starts = new RegExp(SCRIPT_TAG_START_SOURCE, "gi"); let match: RegExpExecArray | null; while ((match = starts.exec(html)) !== null) { const index = match.index; const openTagClose = findTagClose(html, index + match[0].length); if (openTagClose < 0) break; const bodyStart = openTagClose + 1; const end = findScriptEndTag(html, bodyStart); if (end === null) break;](https://files.mastodon.social/cache/media_attachments/files/117/352/037/478/124/002/original/84748a01e31ed2f5.jpg)
