Skip to content

scripts/ai-context.py check fails on every Windows contributor: orphan detection compares /-form keys against \-form paths #329

Closed

Description

@star-nebula

What happens

python scripts/ai-context.py check — the command AGENTS.md tells every contributor to run after documentation changes ("Run python3 scripts/ai-context.py check after documentation changes") — reports 13 orphaned docs on a clean, current tree when run on Windows:

FAIL website\AGENTS.md: scoped instructions no topic loads
FAIL wiki\ai-context.md: wiki page no topic loads (map it, or list it under "unrouted" with a reason)
FAIL wiki\architecture.md: wiki page no topic loads (...)
FAIL wiki\audit.md: wiki page no topic loads (...)
FAIL wiki\cli-development.md: wiki page no topic loads (...)
FAIL wiki\documentation.md: wiki page no topic loads (...)
FAIL wiki\frontend.md: wiki page no topic loads (...)
FAIL wiki\README.md: wiki page no topic loads (...)
FAIL wiki\release.md: wiki page no topic loads (...)
FAIL wiki\testing.md: wiki page no topic loads (...)
FAIL wiki\history\git-hooks-management.md: wiki page no topic loads (...)
FAIL wiki\history\hooks-management.md: wiki page no topic loads (...)
FAIL wiki\history\mcp-pi-builtin-tool-policy.md: wiki page no topic loads (...)
13 problem(s)          # exit code 1

Every one of those files is routed. The same working tree, same Python, run under Linux reports ok / exit 0. Two details make the cause obvious: wiki/README.md is the router itself and can never be an orphan, and the three wiki/history/* files are reported with the wiki message instead of the "history file not indexed" message — they never reach the history branch at all.

Root cause

orphans() builds its comparison keys in forward-slash form (they come from docs/ai-context.json and the defaults at scripts/ai-context.py:73-78), but os.walk + os.path.relpath produce native separators, so on Windows rel is wiki\README.md while routed / allowed / router_text / unrouted all hold wiki/README.md. Four comparisons in scripts/ai-context.py:85-92 therefore never match:

line expression on Windows
:85 rel = os.path.relpath(...) yields wiki\history\x.md
:86 rel in allowed never true — unrouted keys are /-form
:88 rel.startswith(history + os.sep) wiki\history\x.md.startswith(wiki/history\) → False, so the history branch is dead code and its files fall through to the wiki branch
:89 os.path.relpath(rel, wiki) not in router_text compares history\x.md against a /-form index
:91 rel.startswith(wiki + os.sep) True by accident (wiki\ matches), which is why every wiki file lands in the wrong-message branch

Nothing else in the script is affected: section() and check() open files through os.path.join(ROOT, ...), and the byte-budget output is separator-free — which is why the run gets as far as printing the budget table before the orphan list.

Why it matters

CI is not the victim here — scripts/ai-context.py check runs only in unit-test on ubuntu-latest (.github/workflows/test.yaml:36,44) and in the website workflow, so the gate stays green. The contributor is: on Windows the check produces 13 false failures, so its signal is unusable exactly where AGENTS.md asks you to use it. The realistic outcomes are worse than noise — a contributor either learns to ignore the check, or "fixes" routing that was already correct and then proposes a docs PR that adds no value.

Reproduction

Measured on a detached clean checkout of current main (4e3b2b26), no config changes, no local docs, no unrouted entries:

run script result
Windows as published 13 problem(s), exit 1
Linux as published ok, exit 0
Windows with the 4-line fix below ok, exit 0
Linux with the 4-line fix ok, exit 0 (unchanged)

The broken check is not merely noisy — it is blind. Deleting a real router entry (wiki/README.md line indexing history/git-hooks-management.md) and re-running on Windows leaves the output byte-identical to the clean run: same 13 problems, same 13 lines. So the check cannot distinguish "docs are consistent" from "a history file has lost its index entry", because that file already appears in the list for the wrong reason. After the fix the same two states are ok / exit 0 versus exactly:

FAIL wiki/history/git-hooks-management.md: history file not indexed in wiki/README.md
1 problem(s)

which is the branch the unpatched script can never reach on Windows.

Proposed fix

Normalize the walked path once, and keep the prefix tests in the same / vocabulary the rest of the function uses (4 lines):

-            rel = os.path.relpath(os.path.join(d, f), ROOT)
+            rel = os.path.relpath(os.path.join(d, f), ROOT).replace(os.sep, '/')
             if rel in allowed or not f.endswith('.md'):
                 continue
-            if rel.startswith(history + os.sep):
-                if os.path.relpath(rel, wiki) not in router_text and rel not in router_text:
+            if rel.startswith(history + '/'):
+                if os.path.relpath(rel, wiki).replace(os.sep, '/') not in router_text and rel not in router_text:
                     bad.append(f'{rel}: history file not indexed in {router}')
-            elif rel.startswith(wiki + os.sep):
+            elif rel.startswith(wiki + '/'):

Verified: after the change, the same tree gives ok / exit 0 on both Windows and Linux, so the false positives go away without hiding a single real one — the wiki and history branches become distinguishable on Windows for the first time (wiki/history/*.md now correctly reports "history file not indexed" when it actually is).

I kept ROOT + os.sep at :48 alone: that one compares native realpaths against each other, so it is already consistent.

Notes on scope

  • Only orphans() is affected. check()'s heading/path validation and the byte budgets pass on both platforms.
  • The script's docstring advertises "No dependencies beyond the Python 3.8+ standard library" — the fix stays inside that promise (str.replace, os.sep).
  • Alternative considered and rejected: normalize by replacing / with os.sep in the config keys instead. That makes the failure messages print backslash paths in the docs output, which is what the router index and the unrouted list quote back to users, so /-form is the better canonical vocabulary.
  • Happy to open a PR for the 4 lines plus a regression note; filing the issue first per CONTRIBUTING.
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions