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

Aperta Adatta ai principianti
#4,806 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
79/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
bun, typescript

Direzione di ricerca

Inizia da packages/ui-server/src/files/walker.ts, in corrispondenza di buildWikilinkMap (righe 449–515), e leggi la gestione delle voci intorno alle righe 461–493. Aggiungi o individua il test per i file normali e i symlink in packages/ui-server/tests/files-walker.test.ts, poi esegui questo test e packages/ui-server/tests/wikilink-reliability.test.ts. Il lavoro è completato quando vengono sottoposti a stat solo i symlink, le note symlink interne al brain restano mappate, le destinazioni esterne restano escluse e i risultati esistenti della map rimangono invariati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
TypeScript
Stelle
5
Fork
0
Merge medio
8h 3m
PR unite (30g)
606

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di schlessera/brain-kit

Tutte le issue di schlessera/brain-kit

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.