Skip to content

cortheon: keep search ads, quotes, hrefs and registry hosts when only the spelling differs - #2750

Merged
ashhart merged 19 commits into
ashhart:mainfrom
ppardus:cortheon-ad-wrapper-class
Nov 12, 2026
Merged

ashhart merged 19 commits into
ashhart:mainfrom
ppardus:cortheon-ad-wrapper-class

Conversation

@ppardus

@ppardus ppardus commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Cortheon drops or misreads DuckDuckGo results and package registry URLs when only the spelling differs.

  • A search ad whose wrapper uses CLASS="result …" is still skipped.

  • A search ad whose wrapper uses class='result …' is still skipped.

  • An HTML result whose class is class='result__a' is kept.

  • An HTML snippet whose class is class='result__snippet' is kept.

  • A Lite result whose class is class="result-link" is kept.

  • An unquoted href=… on a result is kept, and it ends at a space, tab, CR or LF.

  • A result whose attribute is written href = "…" is kept.

  • A result whose href follows a newline inside the tag is kept.

  • An earlier attribute that only contains the text href is skipped.

  • A quoted href that never closes drops the result.

  • A duplicate whose scheme or host differs only in letters is dropped. The path and query keep their case.

  • PyPI registry JSON is allowed when the host's letters differ. The path stays exact, so /PYPI/ and JSON are refused.

  • npm registry JSON is allowed when the host's letters differ. The latest path stays exact.

  • crates.io registry JSON is allowed when the host's letters differ. The path stays exact.

  • Issue: n/a. Host-testable spelling bugs, with no open issue.

  • Stacked on: main.

  • atc overlap --json: n/a. atc is not installed here.

Receipt

n/a. This does not reach inference: no lane, sampler, precision or prompt-processing change. Metal and CUDA were not run.

  • Environment: Zig 0.17.0 host tests. No checkpoint and no GPU.
  • Paths: Cortheon search parsing and registry URL allow-list. Run as host tests. No GPU.
  • Exactness: n/a. Does not reach inference.
  • Decode speed: n/a. Does not reach inference.
  • Prompt speed: n/a. Does not reach prompt processing.
  • Licence: n/a.

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks @ppardus. Each spelling case has a small test of its own that fails on main, and the test pages are short hand-written snippets, which is what we want. A few things before this can land:

  1. Duplicates (search.zig:189): comparing whole URLs without case drops distinct pages, because paths and queries are case-sensitive (?v=abcD against ?v=abcd, /wiki/Apple against /wiki/APPLE). Please compare only the scheme and host without case, as seen.normal does, and add a test that keeps two results differing only in path case.
  2. Scan cost (findMark): on a page written only in the other quote style, every call rescans to the end for the primary mark, so the cost grows with marks × page size. Since from only moves forward, a primary miss stays a miss. Remembering it, or scanning for both styles in one pass, keeps it linear for both styles.
  3. JSON and Latest (seen.zig:84,86,98): hosts are case-insensitive, but paths aren't. Do PyPI and npm actually serve /JSON and a Latest tag? If not, please keep those two exact, since it's the fetch allow-list.
  4. Href nits: if an earlier attribute contains the text "href", try the next one instead of dropping the result. Keep skipping a quoted value that has no closing quote. End an unquoted value at tab, CR and LF as well as a space.

We'll write the CHANGELOG line at landing.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks @ppardus, that was quick. Duplicates now compare only the scheme and host without case
(zig/src/server/cortheon/search.zig:100-110), with the path-case test at :419. findMark (:88) finds either
quote style in one walk, so a page written in one style stays linear. json and latest stay exact (seen.zig:84,
:98). An earlier data-href, an unquoted value ending at a tab, CR or LF, and href = "..." all have tests, and CI
is green.

Three small things, then it's ready:

  1. A line break before href. hrefValue only accepts a space or a tab before the name (search.zig:117), so a
    link whose href follows a line break inside the tag is now dropped, where main kept it. Please treat any
    whitespace as the boundary (std.ascii.isWhitespace), around the = too (:122, :128), and add a test with a
    newline before href.
  2. An unclosed quote. When a quoted value never closes, the rest of the tag is inside that value, so a later
    href there isn't an attribute. The test at :453 reads https://example.org/page out of the first href's value.
    By keeping it skipped we meant dropping the result, as main did. Please return null at :132-135 and have that test
    expect no result.
  3. The registry prefixes. startsWithIgnoreCase on the whole prefix (seen.zig:80, :88, :101) also matches
    /pypi/ and /api/v1/crates/ without case, so https://pypi.org/PYPI/httpx/json passes, while the comments at
    :84 and :98 say paths stay exact. Please compare only https:// and the host without case and the path
    exactly, add that URL as a refusal, and bring those two comments within 120 columns (they're 151 and 149).

Please also update the description. It still says json's and latest's letters may differ, and that duplicates
differing only in letters are dropped.

Once that's pushed and CI is green, it goes on the landing queue. Main freezes tomorrow morning for 1.2.7, so it lands
tonight if the queue reaches it, otherwise right after the release.

  • Synthena (Ash's Agent)

@ashhart

ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thanks @ppardus, that covers all three. Any whitespace now bounds href and both sides of the = (zig/src/server/cortheon/search.zig:116, :121, :127), with the newline test at :458; an unclosed quote drops the result as main did (:132, test at :449); and the registry check compares only https:// and the host without case and each path exactly (seen.zig:78-110), with https://pypi.org/PYPI/httpx/json refused at :151. The description reads right now too. Once CI is green this is ready to land from our side, and we'll write the CHANGELOG line.

  • Synthena (Ash's Agent)

@Maxrio9 Maxrio9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced an additional href parsing defect at head eaaf6393897bfcb83ddea18672aade2261c10c57, using Zig 0.17.0 and the actual parseDuckDuckGo function.

<a class="result__a" title="see href='https://wrong.example/x'" href="https://right.example/page">Example</a>

The result URL is https://wrong.example/x, not the real href. Removing the real href still creates a result pointing to the URL inside title. hrefValue finds a substring within another attribute's quoted value and treats it as an attribute name. This is a remaining parsing case, not a claim that this PR introduced the defect.

A local candidate walks attribute names and complete values, then selects the actual href. Evidence:

  • Original head: both new regressions fail; ordinary-link and data-href controls pass (2 pass, 2 fail, exit 1).
  • Candidate: both new regressions and all 30 existing search tests pass. The combined run is 37 pass, 0 fail (30 existing, 2 new, 4 external review cases, 1 registration stub), exit 0.
  • An independent 22-case matrix passes, covering quote styles, boolean attributes, whitespace, data-href values, missing/empty href and malformed quotes.
  • zig fmt --check passes; changed-file lean checks add zero findings; search.zig stays at 600 lines. git apply --check passes against this head.

This is a focused local review experiment. Full server/build suites were not run, and it adds no inference or GPU qualification. The existing quoted-> tag-boundary limitation is outside this candidate.

The author-adoption patch below keeps this change on the existing PR:

diff --git a/zig/src/server/cortheon/search.zig b/zig/src/server/cortheon/search.zig
--- a/zig/src/server/cortheon/search.zig
+++ b/zig/src/server/cortheon/search.zig
@@ -109,31 +109,30 @@
     return std.mem.eql(u8, a_rest[a_host..], b_rest[b_host..]);
 }
 
-/// The href in a tag. Whitespace bounds the name, and a quote with no closer drops it.
+/// The href in a tag, skipping complete values of other attributes; an unclosed quote drops the result.
 fn hrefValue(tag: []const u8) ?[]const u8 {
-    var from: usize = 0;
-    while (std.ascii.findIgnoreCasePos(tag, from, "href")) |href_name| { // HREF is the same attribute
-        if (href_name > 0 and !std.ascii.isWhitespace(tag[href_name - 1])) { // data-href is not this attribute
-            from = href_name + 1;
-            continue;
+    var at: usize = 0;
+    while (at < tag.len) {
+        while (at < tag.len and std.ascii.isWhitespace(tag[at])) at += 1;
+        const name_start = at;
+        while (at < tag.len and !std.ascii.isWhitespace(tag[at]) and tag[at] != '=') at += 1;
+        const name = tag[name_start..at];
+        while (at < tag.len and std.ascii.isWhitespace(tag[at])) at += 1;
+        if (at == tag.len or tag[at] != '=') continue;
+        at += 1;
+        while (at < tag.len and std.ascii.isWhitespace(tag[at])) at += 1;
+        var start = at;
+        var end = at;
+        if (at < tag.len and (tag[at] == '"' or tag[at] == '\'')) {
+            const quote = tag[at];
+            start += 1;
+            end = std.mem.indexOfScalarPos(u8, tag, start, quote) orelse return null;
+            at = end + 1;
+        } else {
+            while (at < tag.len and !std.ascii.isWhitespace(tag[at]) and tag[at] != '>') at += 1;
+            end = at;
         }
-        var after = href_name + 4;
-        while (after < tag.len and std.ascii.isWhitespace(tag[after])) after += 1; // href = is the same attribute
-        if (after >= tag.len or tag[after] != '=') {
-            from = href_name + 1;
-            continue;
-        }
-        var v = tag[after + 1 ..];
-        while (v.len > 0 and std.ascii.isWhitespace(v[0])) v = v[1..];
-        if (v.len > 0 and (v[0] == '"' or v[0] == '\'')) {
-            const q = v[0];
-            v = v[1..];
-            // the rest of the tag is inside this value, so a later href there is not an attribute
-            const end = std.mem.indexOfScalar(u8, v, q) orelse return null;
-            return v[0..end];
-        }
-        const end = std.mem.indexOfAny(u8, v, " \t\r\n>") orelse v.len; // unquoted ends at whitespace or >
-        return std.mem.trim(u8, v[0..end], " \t\r\n");
+        if (std.ascii.eqlIgnoreCase(name, "href")) return tag[start..end];
     }
     return null;
 }
@@ -595,3 +594,7 @@
     try std.testing.expectEqualStrings("q=zig+0.17+%22std.Io%22", (try duckduckgoUrl(a, "zig 0.17 \"std.Io\""))[std.mem.indexOf(u8, try duckduckgoUrl(a, "zig 0.17 \"std.Io\""), "q=").?..]);
     try std.testing.expectEqualStrings("a b/c", try decode(a, "a+b%2Fc"));
 }
+
+test {
+    _ = @import("search_href_test.zig");
+}
diff --git a/zig/src/server/cortheon/search_href_test.zig b/zig/src/server/cortheon/search_href_test.zig
new file mode 100644
--- /dev/null
+++ b/zig/src/server/cortheon/search_href_test.zig
@@ -0,0 +1,26 @@
+const std = @import("std");
+const search = @import("search.zig");
+
+test "a search result uses its actual href instead of href text in a quoted attribute" {
+    var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+    defer arena.deinit();
+    const page =
+        \\<div class="result results_links web-result"><h2>
+        \\<a class="result__a" title="see href='https://wrong.example/x'" href="https://right.example/page">Example</a>
+        \\</h2></div>
+    ;
+    const results = try search.parseDuckDuckGo(arena.allocator(), page, 5);
+    try std.testing.expectEqual(@as(usize, 1), results.len);
+    try std.testing.expectEqualStrings("https://right.example/page", results[0].url);
+}
+
+test "href text in a quoted attribute does not create a search result without an href" {
+    var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+    defer arena.deinit();
+    const page =
+        \\<div class="result results_links web-result"><h2>
+        \\<a class="result__a" title="see href='https://wrong.example/x'">Example</a>
+        \\</h2></div>
+    ;
+    try std.testing.expectEqual(@as(usize, 0), (try search.parseDuckDuckGo(arena.allocator(), page, 5)).len);
+}

ashhart and others added 19 commits November 12, 2026 02:11
@ashhart
ashhart force-pushed the cortheon-ad-wrapper-class branch from eaaf639 to aeeca55 Compare November 12, 2026 02:11
@ashhart
ashhart merged commit 000b534 into ashhart:main Nov 12, 2026
2 of 10 checks passed
@ashhart

ashhart commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thank you @ppardus. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

@ashhart

ashhart commented Nov 13, 2026

Copy link
Copy Markdown
Owner

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.

3 participants