Skip to content

javascript: second occurrence of a structurally-identical top-level function silently dropped from function_data #1222

Closed

Description

@squid-protocol

What

When a JavaScript file has two (or more) top-level named functions with different names but near-identical body shape, the extraction pipeline records the first one and silently drops the second, even though func_start's raw regex correctly matches both. Found via a tree-sitter-based ground-truth accuracy pass for JavaScript (mirroring ast_accuracy_audit.py's Python methodology, #1200) against expressjs/express (v5.2.1).

Repro (real file, minimal isolated scan)

examples/view-locals/index.js in expressjs/express at tag v5.2.1 defines four top-level functions:

function count(req, res, next) {          // line 48
  User.count(function(err, count){
    if (err) return next(err);
    res.locals.count = count;
    next();
  })
}

function users(req, res, next) {          // line 56
  User.all(function(err, users){
    if (err) return next(err);
    res.locals.users = users.filter(ferrets);
    next();
  })
}

function count2(req, res, next) {         // line 86 -- same shape as count()
  User.count(function(err, count){
    if (err) return next(err);
    res.locals.count = count;
    next();
  })
}

function users2(req, res, next) {         // line 94 -- same shape as users()
  User.all(function(err, users){
    if (err) return next(err);
    res.locals.users = users.filter(ferrets);
    next();
  })
}

Scanning just that directory (galaxyscope examples/view-locals --db-only), function_data for index.js only contains:

count
users
next     <- phantom, see #1221, unrelated to this issue
ferrets

count2 and users2 never appear, despite both being real, differently-named, top-level function declarations.

Confirmed NOT a regex-matching problem

Running func_start directly against the raw file content finds all four correctly, at their real positions:

from gitgalaxy.standards.language_standards import LANGUAGE_DEFINITIONS
fs = LANGUAGE_DEFINITIONS["javascript"]["rules"]["func_start"]
src = open("examples/view-locals/index.js").read()
[(m.group(0), m.span()) for m in fs.finditer(src)]
# 'function count' (48), 'function users' (56), 'function count2' (86), 'function users2' (94)
# -- all four found, at the right offsets.

So the raw signature-matching regex is fine; the loss happens somewhere downstream of it -- in whatever stage turns a func_start match into a function_data row (the Mode B brace-slicer / block-boundary dispatcher in detector.py, per this repo's own phase terminology). Root cause not yet diagnosed beyond that -- filing with a confirmed, minimal, isolated (single-file, no cross-file interaction) reproduction rather than guessing further, per this repo's own convention for this class of finding (see docs/language_status/python.md §9's #1184 entry for the same "confirmed mechanism, not yet root-caused to the exact line" shape).

Why this matters

count/count2 and users/users2 are structurally near-identical (same param list shape, same body shape, different only in name and the inner callback's use of that name) -- exactly the shape of copy-pasted example/boilerplate/generated code, which is common in real repos. If the loss is keyed on body/shape similarity rather than name, this could silently undercount function totals on any codebase with repeated boilerplate patterns (e.g. multiple near-identical CRUD handlers, generated route handlers, etc.), not just this specific tutorial file.

Scope note

Distinct from #1221 (bare call statements false-positive as definitions) -- that's a precision bug (phantom extra functions); this is a recall bug (real functions silently missing) with an unrelated mechanism. Also distinct from #1193 (comment-delimiter corruption, line_exclusive languages only -- javascript is standard_block, not affected by that one).

Activity

  1. squid-protocol commented on Aug 10, 2026

    @squid-protocol
    OwnerAuthor

    Additional corroborating evidence from a second, unrelated corpus (GitGalaxy's own site/js/ -- the WebGPU visualizer, real hand-written code, not generated/boilerplate like the count/count2 example that opened this issue) -- this looks like the same underlying mechanism (func_start finds the match, function_data doesn't have the row), but manifesting inside ES6 class bodies with multiple methods, and at a much higher severity than the original repro suggested:

    File Real methods (tree-sitter) Recorded in function_data Missing
    tools/search.js (SearchController class) 10 1 (constructor only) 9 of 10
    tools/ally.js 5 5 present but 6 other real methods never appear at all -- focusNext, focusPrev, generateDescription, getVisibleNodes, handleNavigation, renderTree 6
    tools/poster.js -- -- 8 missing (_verifyRenderIntegrity, cleanup, drawLegend, drawTypography, get, handleAccept, handleDecline, initiateCheckout)
    js/core/materials.js -- resolveBasalColor, refresh, constructor present get, resolveMaterialProperties missing
    js/core/galaxy-engine.js -- ~21 methods present (most of the class) 5 missing (clearDependencyLines, drawDependencyLines, setAttr, toggleDependencyLines, toggleGlobalWeb)

    Confirmed via the same method as the original repro: LANGUAGE_DEFINITIONS["javascript"]["rules"]["func_start"].finditer(src) finds all 10 of search.js's method names (including the 9 missing ones) at their correct positions in the raw text -- so this is not a regex-matching gap, same as the count2/users2 case.

    Notably this ISN'T a simple "only the first method of a class survives" pattern (galaxy-engine.js keeps ~21 of ~26 methods; search.js keeps only 1 of 10) -- whatever the real mechanism is, it's more selective than a single early-exit. Might be worth checking whether it correlates with specific syntax inside the previous sibling method's body (arrow-function callbacks, template literals, nested braces/parens depth, etc.) tripping up the Mode B brace-slicer's boundary detection for that method, causing it to either over-consume into the next method(s) or lose sync with the class's true structure. Leaving the deeper diagnosis to whoever picks this up -- flagging so the fix isn't scoped too narrowly to just the top-level-duplicate-function shape the issue was originally filed against.

  2. squid-protocol commented on Aug 10, 2026

    @squid-protocol
    OwnerAuthor

    Relevant prior art found after filing: #789 (csharp) diagnosed the same general failure layer -- `detector.py`'s `_slice_by_braces` (Mode B) does a bounded post-hoc search for the next literal `{` after each regex match to determine a function's body extent, with a `if start_idx < last_end_idx: continue` guard that silently drops any match falling inside territory an earlier match already claimed. That guard is a strong candidate mechanism for what's happening here: if an earlier sibling (`count`'s body, or `search.js`'s `constructor` body) gets a body-boundary search that over-extends past its real closing brace, everything textually between there and the next correctly-detected boundary -- including a same-shaped sibling like `count2`, or eight of `SearchController`'s nine methods -- would fall inside `last_end_idx` and get silently skipped, exactly matching what's observed here (regex finds it, function_data doesn't have it).

    #789 was closed without the downstream _slice_by_braces fix landing (scoped out as its own follow-up issue, which I can't find filed anywhere) -- this may effectively be that follow-up, now with two independent real-corpus confirmations (expressjs/express for the top-level-duplicate shape, GitGalaxy's own site/js/ for the class-method shape) rather than a synthetic csharp snippet. Worth reading #789's full diagnosis before investigating further here, since it already narrows down the exact function (_slice_by_braces) and the exact guard (last_end_idx) most likely responsible, even though its own repro was csharp-specific.

  3. added 4 commits that reference this issue on Aug 11, 2026
    89d86da
    a094f45
    afd4d16
    63269ec
  4. added this to the v2.4.8 milestone on Aug 16, 2026
  5. added
    scope:frontier
    on Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope:frontier

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions