ui-server: the wikilink map stats every file it walks although the directory entry already has the type

Open Beginner friendly
#4,806 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
79/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
bun, typescript

Research direction

Start in packages/ui-server/src/files/walker.ts at buildWikilinkMap (lines 449–515) and read the entry handling around lines 461–493. Add or locate the test in packages/ui-server/tests/files-walker.test.ts for regular files and symlinks, then run it and packages/ui-server/tests/wikilink-reliability.test.ts. Done means only symlinks are stat-ed, in-brain symlink notes remain mapped, outside targets stay excluded, and existing map results remain unchanged.

Written by the indexing model from the issue text.

Description

agent-ready area: ui-server priority: p2 type: refactor

Context

buildWikilinkMap (packages/ui-server/src/files/walker.ts:449-515) walks the whole brain to map note slugs
to paths. It lists each directory with readdir(abs, { withFileTypes: true }) (:461), and then awaits
stat() on every entry (:477-482). The only thing it reads from the result is whether the entry is a
directory (:493); size and modification time are never used. For everything except a symbolic link the
directory entry from readdir already answers that question.

GET /api/files/wikilinks rebuilds the map at most every 30 seconds, or on ?refresh=1
(WIKILINK_TTL_MS, packages/ui-server/src/routes/files.ts:24; the route at :284-292). The client asks for
it when a markdown surface first needs a wikilink (packages/ui-react/src/stores/file-state.ts:159).

Measured cost (auditor micro-benchmark, synthetic tree of 10,000 files in 200 directories, warm file cache,
one server-class machine; not a device or production figure): the serial walk with a stat per entry took
103 to 121 ms; the same walk trusting the directory entry took 10 to 12 ms. The walk grows with the brain,
and every non-markdown attachment is stat-ed too.

Related: #1799 (file browsing and .gitignore when anchored snapshots are unavailable) and #1800 (symlinked
brain roots) change the same file.

Scope

  • In buildWikilinkMap, decide "is a directory" from the directory entry, and call stat only when the
    entry is a symbolic link. The symlink branch keeps its realpath containment check
    (packages/ui-server/src/files/walker.ts:483-491).
  • The resulting map, including which path wins when two files share a slug, is unchanged.

Out of scope

  • listDirectory (packages/ui-server/src/files/walker.ts:217-262). It returns size and modification time,
    so it needs its stat.
  • Extracting the entry-filter block that listDirectory and buildWikilinkMap both carry into one helper.
    It is a real duplicate, but merging it widens this diff into the listing path that #1799 and #1800 are
    changing.
  • Caching the parsed .gitignore matcher (loadIgnore, packages/ui-server/src/files/walker.ts:180-191).
    That read goes through the anchored snapshot path #1799 owns, and it was not measured.
  • The per-directory safeResolve and realpath calls (:455, :458); they guard against cycles and
    escapes.
  • The 30-second cache and the route.

Acceptance criteria

  • A test builds a brain with regular files, a nested directory, a symlink to a directory inside the
    brain and a symlink to a directory outside it, replaces fs/promises stat with a counting double
    for the walk (restoring the complete original in teardown), and asserts stat is called only for the
    two symlinks. It fails on the tree before the change, where every entry is stat-ed. Mutation: call
    stat for every entry again and the count assertion fails.
  • The same test asserts notes under the in-brain symlinked directory are in the map and notes under the
    outside one are not. Mutation: drop the symlink branch and the in-brain symlinked notes disappear.
  • For the fixtures of packages/ui-server/tests/files-walker.test.ts and
    packages/ui-server/tests/wikilink-reliability.test.ts, the map is unchanged: both files pass with no
    expected value changed, including the first-wins precedence for a shared slug.

Verification

bun run test packages/ui-server/tests/files-walker.test.ts
bun run test packages/ui-server/tests/wikilink-reliability.test.ts
NODE_OPTIONS=--max-old-space-size=6144 bun run typecheck
bun run lint
bun run check:pr --base origin/main

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

The wikilink map returned by GET /api/files/wikilinks is identical; only the number of filesystem calls
changes.
It edits files under a published package's src, so scripts/check-changeset.ts requires a (patch) changeset
even though outputs are unchanged.

Found by a repository audit on 2026-11-26 (main 87b96b10c); independently reviewed before filing.

Dominant language
TypeScript
Stars
5
Forks
0
Avg merge
8h 3m
Merged PRs (30d)
606

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from schlessera/brain-kit

All issues in schlessera/brain-kit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.