Repository navigation
Commit 23dae89
fix(metadata): FilesystemLoader remembers what findFile resolved, so a repeated read makes no fs.access probe (#24621)
Fixes #24537
Clause-②: no
## What changed
`FilesystemLoader`
(`packages/metadata/src/loaders/filesystem-loader.ts`) now remembers
what `findFile` resolved for each `(type, name)`. The answer is either
the path or "no file". The memo belongs to one loader instance, so each
kernel has its own. A repeated read of the same name makes no
`fs.access` probe, whether a file holds the name or not.
- The caller is `plugin-security`'s `catalogSetLoader`
(`security-plugin.ts`). It does not change.
- The fix lands where the claim expected. The loader is the producer of
the cost, and no other package changes.
- Nothing public is added: no member, export or option. In the emitted
`dist/node.d.ts`, `FilesystemLoader` gains only `private` members, and
its public members are byte-identical.
## Premise, measured before any change (`origin/main` 2667e24)
The probe counted calls to `node:fs/promises.access` along the real
chain:
`createSecurityCatalogReader().resolve('permission', name)` →
`NodeMetadataManager.getDiagnosed` → `FilesystemLoader.load` →
`findFile`
It used the plugin's format set (yaml, json, typescript, javascript),
which makes five resolvable extensions.
| name | `fs.access` per resolution, over 4 resolutions |
|---|---|
| no file holds it | 5, 5, 5, 5 |
| `permission/hit_set.json` holds it | 2, 2, 2, 2 (`load` and `stat`
each resolved it) |
One narrowing of the card's reach: only names that the engine registry
does not serve fall through to the metadata service. A set the registry
holds never reaches this loader. That covers platform sets, the sets in
package manifests, and environment-authored sets hydrated from
`sys_metadata`.
The same probe after this change reads `5, 0, 0, 0, 0` for the miss and
`1, 0, 0, 0, 0` for the hit.
## What can change the answer, and what retires it
I enumerated every in-process path that touches `ROOT/TYPE/`:
| path | exists? | how the memo is retired | pinned by |
|---|---|---|---|
| `FilesystemLoader.save()`, the only in-process writer. It is reached
through `MetadataManager.save()`, which no production code in this repo
calls. | yes | Every entry is retired, in a `finally`, because a direct
write can fail after creating the file. | "a save through the loader
retires a remembered miss" |
| `FilesystemLoader.delete()` | does not exist | n/a | n/a |
| The file watcher on `added` / `changed`. This is
`NodeMetadataManager.handleFileEvent` (chokidar), which runs only with
`watch: true`. | yes | The handler already re-reads with `useCache:
false`. That read now resolves the name again from disk and keeps the
answer. | "an `added` file event ..." |
| The file watcher on `unlink` | yes | The handler reads nothing. The
next read finds the remembered path gone: `stat` reports ENOENT/ENOTDIR,
or the probe that `exists()` makes to confirm the path fails. That read
resolves the name once more from disk and logs no stat failure. | "an
`unlink` event ..." |
| The loader's own walk (`list`, `loadMany`, `loadManyKeyed`, all
through `resolvableNames`) | yes | that type's entries | the RECORD case
|
| Artifact reload (HMR POST or the artifact watcher), and `register()` |
yes | Not needed. Both write the manager's registry, which `get` /
`getDiagnosed` ask before any loader. | the two "writing a permission
set" cases |
| The ADR-0008 `FileSystemRepository` | It writes under
`ROOT/.objectstack/metadata`, never under `ROOT/TYPE/`. | n/a | n/a |
Two more guards:
- **Race.** A probe can still be on disk when a retirement happens. An
epoch counter stops it from writing its older answer back. Pinned by "a
probe that was on disk ...".
- **Bound.** Callers supply the names, so the memo is capped at 10,000
entries and starts over past that. Pinned by "is bounded ...".
## The gap no event reaches
The gap is named here and deliberately not closed with a clock. The
scenario:
1. A name has already been answered "no file".
2. Another process then creates `ROOT/TYPE/NAME.ext`.
3. No watcher is running.
No watcher running is the normal case: every shipped composition passes
`watch: false`. That covers `createStandaloneStack`, both of `os
serve`'s doors (the dev artifact door and the host-config door), and the
verify harness.
- **On main today.** `get()` sees the file on the next read, because
`findFile` probes every time. `list()` sees it once the manager's 30 s
list cache expires and the next `list()` walks.
- **After this PR, `list()`.** Unchanged.
- **After this PR, `get()`.** It keeps the remembered miss until one of
three things happens: the next walk of that type (the next `list()`
after the TTL is one), a `save()` through the loader, or a restart. A
walk retires the type, so `get()` is never staler than `list()`. A name
that `list()` reports is never answered `null`.
- **Edits to a file that already resolves.** These are still read on the
next request, because the content cache revalidates by etag. Pinned by
the CONTROL case.
- **Deletions.** These are covered by the `unlink` row above.
The gap is pinned as a `RECORD:` case, so any change to it shows up in a
diff.
## "Activating or writing a permission set is seen on the next request"
Neither of these goes through this loader:
- **Activation.** `POST /security/_activation/:type/:name`
(`packages/runtime/src/domains/catalog-activation.ts`) writes a
`sys_metadata_activation` row. The resolver reads that ledger
(`readDisabledCatalogNames`), not the catalog body, so the memo never
sees it.
- **Writing a definition.** `saveMetaItem` (the clone door, for example)
puts the set in the engine registry through `registerItem`, and
`catalog.resolve` asks that registry first. `MetadataManager.register()`
puts it in the manager's registry, which `getDiagnosed` asks before any
loader.
Two cases pin that a remembered miss hides neither write.
## Tests
`packages/metadata/src/loaders/filesystem-loader-resolution-memo.test.ts`
has 13 cases. They run real files in a tmpdir through the real
`NodeMetadataManager` and the real `createSecurityCatalogReader`, and
count probes at `node:fs/promises.access`.
- probe count, miss: `[5, 0, 0, 0, 0]`
- probe count, hit: `[1, 0, 0, 0, 0]`, and the body is still served
- CONTROL: an edit to a resolved file is read on the next resolution
- two managers do not share what they remember
- a set written to the engine registry is seen on the next resolution
- a set registered with the metadata service is seen on the next
resolution
- a save through the loader retires a remembered miss
- an `added` file event announces the body (not `null`), and the next
resolution sees the file
- an `unlink` event: `load()` and `exists()`, each asked first, answer
the miss, and no stat failure is logged
- a deletion no event reports is re-resolved from disk (json removed,
yaml added → the yaml body)
- RECORD: the gap above, closed by the next walk
- a probe still on disk during a save does not write its miss back
- the memo is bounded
Results on the final head (HEAD dff1e3f):
- `pnpm --filter @objectstack/metadata test`: 64 files, 905 tests, all
passed.
- `pnpm --filter @objectstack/metadata typecheck`: exit 0. `tsc
--listFiles` includes the new test file.
## Ablation
There is one leg per mechanism. Each leg ran through
`scripts/ablation-replace.mjs` in WRAP mode: the anchor hit once, the
mutation was proven on disk by blob hash, and the restore was proven
(blob equals HEAD, `git diff HEAD` empty). The test imports
`./filesystem-loader.js` relatively, so it reads source and no `dist/`
step applies.
| leg | mutation | went red |
|---|---|---|
| memo | `if (remembered?.has(name))` → `if (false && …)` | both probe
counts, the RECORD case, the bound (4) |
| save retire | `this.retireResolutions();` → `void 0;` | the save case,
the late-probe case (2) |
| walk retire | `this.retireResolutions(type);` → `void type;` | the
RECORD case (1) |
| fresh read | `resolveStats(type, name, !useCache)` → `false` | the
`added` event case (1) |
| self-heal | the ENOENT/ENOTDIR `continue` → `if (false)` | the
`unlink` case, the unreported-deletion case (2) |
| `exists()` confirmation | the fresh fallback → `return true` | the
`unlink` case (1) |
| epoch | `if (epoch === this.resolutionEpoch)` → `if (true)` | the
late-probe case (1) |
| bound | the cap test → `if (false)` | the bound case (1) |
## Gates
The local subset was derived with `node scripts/pm/dispatch-gates.mjs
--commands --repo objectstack-ai/objectstack` from the real diff, on the
final head dff1e3f (merge base 5a871e4). It produced 64 commands,
and I ran all of them.
- Each exit code was captured before any pipe. **All 64 exited 0.**
- `--ran` reconciliation: `64 derived, 64 run, 0 NOT-MEASURED, 0 UNRUN`
("a DERIVED zero — all 64 recorded an exit code and none of them is 3").
- An earlier run, before merging main (6fb0a2d), hit three
prerequisite refusals (exit 3), all since cleared:
- the plugin-teardown self-test could not reach its pinned fixture
commit from the shallow clone;
- `check:lean-entry-closure` needed `@objectstack/objectql`'s built
dist;
- `check:dual-build-cjs-loads` needed built dist for every package.
All three exit 0 on the rerun at dff1e3f. Its last line reads: 111
require entry points across 69 packages load.
- After the merge I rebuilt `@objectstack/spec`. `check:generated`
reported all 14 generated artifacts up to date.
- Left to CI and read from this PR's check-runs, not re-run locally: the
required contexts (`Lint & Repo Gates`, `TypeScript Type Check`, `Test
Core`, `Build Core`, `Governed Surface Queue Guard`). `test:repo` was
not run locally either: the diff touches no file in that project.
## Acceptance notes
- **Not filed, no owner yet.** Artifact-mode runtimes still register
this `FilesystemLoader` at `rootDir`. `NodeMetadataManager` installs it
whenever no loaders are passed, and `MetadataPlugin` never passes any.
Two pieces of documentation disagree with that:
`MetadataPluginOptions.watch` says the source-file scan is redundant in
artifact mode, and the `bootstrap: 'artifact-only'` branch says "Never
touch the filesystem". The per-request probe this PR memoises exists
only because that loader is registered. Removing it from artifact modes
would be a composition change with its own blast radius. I took no
public-door reading, so it is noted here rather than filed.
- **Observation.** A name that a file does hold still costs one
`fs.stat` and one `readFile` per read. That is the content cache's etag
revalidation, and it is what keeps edits visible without a watcher. It
only applies when a file exists under the root, so it is not the miss
path this card measured.
- **Observation.** `FilesystemLoader.contract` declares `supportsWatch:
true`, but the loader has no watch of its own: `NodeMetadataManager`
owns the chokidar watcher. That is why the watch retirement rides on the
handler's existing `useCache: false` re-read and the read-time repair,
and not on a new loader member.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01AHeUBbe6S3JRawcpbLejXD)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 3566c08 commit 23dae89
3 files changed
Lines changed: 615 additions & 51 deletions
File tree
- .changeset
- packages/metadata/src/loaders
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
0 commit comments