superbee 0.4.3-pre.1 → 0.4.3-pre.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4040,6 +4040,12 @@ async function probeSegment(port, candidate, rel, segment) {
4040
4040
  function hasExact(listing, segment) {
4041
4041
  return listing.some((entry) => entry.name === segment);
4042
4042
  }
4043
+ function freshListings(port) {
4044
+ return async (dir) => {
4045
+ const listing = await port.entries(dir);
4046
+ return listing === null ? null : { has: (name) => hasExact(listing, name) };
4047
+ };
4048
+ }
4043
4049
  async function walkExact(port, rootResolved, segments, rel) {
4044
4050
  let parent = rootResolved;
4045
4051
  for (let index = 0; index < segments.length; index++) {
@@ -4071,14 +4077,14 @@ async function recordWalk(port, rootResolved, segments, rel) {
4071
4077
  }
4072
4078
  return recorded;
4073
4079
  }
4074
- async function confirmedWalk(port, rootResolved, segments, rel, recorded, handle) {
4080
+ async function confirmedWalk(port, rootResolved, segments, rel, recorded, handle, listings = freshListings(port)) {
4075
4081
  let parent = rootResolved;
4076
4082
  for (let index = 0; index < segments.length; index++) {
4077
4083
  const segment = segments[index];
4078
4084
  const candidate = path2.join(parent, segment);
4079
- const listing = await port.entries(parent);
4085
+ const listing = await listings(parent);
4080
4086
  if (listing === null) return "absent";
4081
- const listed = hasExact(listing, segment);
4087
+ const listed = listing.has(segment);
4082
4088
  if (handle !== null && index === segments.length - 1) {
4083
4089
  const verdict = classifyLeaf({ listed, probe: await probeSegment(port, candidate, rel, segment), handle });
4084
4090
  if (verdict === "aliased") throw new FilesystemIdentityAliasError(rel, segment);
@@ -4098,13 +4104,15 @@ function countRestart(restarts, rel) {
4098
4104
  return restarts + 1;
4099
4105
  }
4100
4106
  async function observeExact(port, root, rel, read) {
4107
+ return observeWith(port, path2.resolve(root), rel, read, freshListings(port));
4108
+ }
4109
+ async function observeWith(port, rootResolved, rel, read, listings, onExact) {
4101
4110
  const segments = relSegments(rel);
4102
- const rootResolved = path2.resolve(root);
4103
4111
  const target = path2.join(rootResolved, ...segments);
4104
4112
  for (let restarts = 0; ; ) {
4105
4113
  const recorded = await recordWalk(port, rootResolved, segments, rel);
4106
4114
  if (recorded === null) return { state: "absent" };
4107
- const verified = await confirmedWalk(port, rootResolved, segments, rel, recorded, null);
4115
+ const verified = await confirmedWalk(port, rootResolved, segments, rel, recorded, null, listings);
4108
4116
  if (verified === "absent") return { state: "absent" };
4109
4117
  if (verified === "restart") {
4110
4118
  restarts = countRestart(restarts, rel);
@@ -4126,11 +4134,14 @@ async function observeExact(port, root, rel, read) {
4126
4134
  try {
4127
4135
  value = await read(opened.handle, target);
4128
4136
  if (value === null) return { state: "absent" };
4129
- verdict = await confirmedWalk(port, rootResolved, segments, rel, recorded, opened);
4137
+ verdict = await confirmedWalk(port, rootResolved, segments, rel, recorded, opened, listings);
4130
4138
  } finally {
4131
4139
  await port.close(opened.handle);
4132
4140
  }
4133
- if (verdict === "exact") return { state: "exact", value };
4141
+ if (verdict === "exact") {
4142
+ onExact?.(recorded, { dev: opened.dev, ino: opened.ino });
4143
+ return { state: "exact", value };
4144
+ }
4134
4145
  if (verdict === "absent") return { state: "absent" };
4135
4146
  restarts = countRestart(restarts, rel);
4136
4147
  }
@@ -4149,6 +4160,100 @@ async function probeExact(port, root, rel) {
4149
4160
  restarts = countRestart(restarts, rel);
4150
4161
  }
4151
4162
  }
4163
+ var SETTLED_AFTER_NS = 3000000000n;
4164
+ function witnessKey(witness) {
4165
+ return `${witness.dev}:${witness.ino}:${witness.mtimeNs}:${witness.ctimeNs}`;
4166
+ }
4167
+ function openObservationPass(port, root, now = Date.now) {
4168
+ const rootResolved = path2.resolve(root);
4169
+ const kept = /* @__PURE__ */ new Map();
4170
+ const reusedVerdicts = [];
4171
+ const fresh = freshListings(port);
4172
+ const witnessOf = port.directoryWitness?.bind(port);
4173
+ const settled = (witness) => {
4174
+ const nowNs = BigInt(Math.floor(now())) * 1000000n;
4175
+ const latest = witness.mtimeNs > witness.ctimeNs ? witness.mtimeNs : witness.ctimeNs;
4176
+ return nowNs - latest >= SETTLED_AFTER_NS;
4177
+ };
4178
+ const listingsFor = () => {
4179
+ let reused = false;
4180
+ if (witnessOf === void 0) return { source: fresh, reused: () => false };
4181
+ const source = async (dir) => {
4182
+ const keep = kept.get(dir);
4183
+ if (keep !== void 0) {
4184
+ const current = await witnessOf(dir);
4185
+ if (current !== null && witnessKey(current) === keep.witness) {
4186
+ reused = true;
4187
+ return keep.names;
4188
+ }
4189
+ kept.delete(dir);
4190
+ }
4191
+ const before = await witnessOf(dir);
4192
+ const listing = await port.entries(dir);
4193
+ if (listing === null) return null;
4194
+ const names = new Set(listing.map((entry) => entry.name));
4195
+ const after = await witnessOf(dir);
4196
+ if (before !== null && after !== null && witnessKey(before) === witnessKey(after) && settled(after)) {
4197
+ kept.set(dir, { witness: witnessKey(after), names });
4198
+ }
4199
+ return names;
4200
+ };
4201
+ return { source, reused: () => reused };
4202
+ };
4203
+ return {
4204
+ async observe(rel, read) {
4205
+ const listings = listingsFor();
4206
+ let exact;
4207
+ let observed;
4208
+ try {
4209
+ observed = await observeWith(port, rootResolved, rel, read, listings.source, (recorded, handle) => {
4210
+ exact = { rel, segments: relSegments(rel), recorded, handle };
4211
+ });
4212
+ } catch (error) {
4213
+ if (!listings.reused()) throw error;
4214
+ return observeExact(port, rootResolved, rel, read);
4215
+ }
4216
+ if (!listings.reused()) return observed;
4217
+ if (observed.state === "absent") return observeExact(port, rootResolved, rel, read);
4218
+ if (exact !== void 0) reusedVerdicts.push(exact);
4219
+ return observed;
4220
+ },
4221
+ async finish() {
4222
+ const retry = /* @__PURE__ */ new Set();
4223
+ const endListings = /* @__PURE__ */ new Map();
4224
+ const listedAtEnd = async (dir) => {
4225
+ if (!endListings.has(dir)) {
4226
+ const listing = await port.entries(dir);
4227
+ endListings.set(dir, listing === null ? null : new Set(listing.map((entry) => entry.name)));
4228
+ }
4229
+ return endListings.get(dir);
4230
+ };
4231
+ try {
4232
+ for (const verdict of reusedVerdicts) {
4233
+ if (!await holdsAtEnd(port, rootResolved, verdict, listedAtEnd)) retry.add(verdict.rel);
4234
+ }
4235
+ } finally {
4236
+ kept.clear();
4237
+ reusedVerdicts.length = 0;
4238
+ }
4239
+ return [...retry];
4240
+ }
4241
+ };
4242
+ }
4243
+ async function holdsAtEnd(port, rootResolved, verdict, listedAtEnd) {
4244
+ let parent = rootResolved;
4245
+ for (let index = 0; index < verdict.segments.length; index++) {
4246
+ const segment = verdict.segments[index];
4247
+ const listing = await listedAtEnd(parent);
4248
+ if (listing === null || !listing.has(segment)) return false;
4249
+ const candidate = path2.join(parent, segment);
4250
+ const probed = await port.probe(candidate);
4251
+ const expected = index === verdict.segments.length - 1 ? verdict.handle : verdict.recorded[index];
4252
+ if (probed === null || probed.kind === "symlink" || !sameWitness(probed, expected)) return false;
4253
+ parent = candidate;
4254
+ }
4255
+ return true;
4256
+ }
4152
4257
  var linkUnsupportedRoots = /* @__PURE__ */ new WeakMap();
4153
4258
  function unsupportedLinkRoots(port) {
4154
4259
  let roots = linkUnsupportedRoots.get(port);
@@ -4302,8 +4407,56 @@ function kindOf(stats) {
4302
4407
  if (stats.isFile()) return "file";
4303
4408
  return "other";
4304
4409
  }
4410
+ var LOCAL_LINUX_FILESYSTEMS = /* @__PURE__ */ new Set([
4411
+ 61267,
4412
+ // ext2/3/4
4413
+ 1481003842,
4414
+ // xfs
4415
+ 2435016766,
4416
+ // btrfs
4417
+ 16914836,
4418
+ // tmpfs
4419
+ 2240043254,
4420
+ // ramfs
4421
+ 2035054128,
4422
+ // overlayfs
4423
+ 801189825,
4424
+ // zfs
4425
+ 4076150800,
4426
+ // f2fs
4427
+ 3393526350,
4428
+ // bcachefs
4429
+ 19780,
4430
+ // vfat
4431
+ 538032816,
4432
+ // exfat
4433
+ 1936094318,
4434
+ // ntfs3
4435
+ 18475
4436
+ // hfsplus
4437
+ ]);
4438
+ function directoryWitnessTrusted(platform, filesystemType) {
4439
+ if (platform !== "linux") return true;
4440
+ return filesystemType !== null && LOCAL_LINUX_FILESYSTEMS.has(filesystemType);
4441
+ }
4442
+ function witnessTrust() {
4443
+ const devices = /* @__PURE__ */ new Map();
4444
+ return (dev, dir) => {
4445
+ if (process.platform !== "linux") return Promise.resolve(true);
4446
+ let trusted = devices.get(dev);
4447
+ if (trusted === void 0) {
4448
+ trusted = fs2.statfs(dir).then(
4449
+ (info) => directoryWitnessTrusted("linux", Number(info.type)),
4450
+ () => false
4451
+ );
4452
+ devices.set(dev, trusted);
4453
+ }
4454
+ return trusted;
4455
+ };
4456
+ }
4305
4457
  function createNodeFilesystemIdentityPort(hostPolicy) {
4306
4458
  const policy = captureFilesystemHostPolicy(hostPolicy);
4459
+ const witnessTrustedOn = witnessTrust();
4307
4460
  return Object.freeze({
4308
4461
  isTransientOpenError: policy.isTransientOpenError,
4309
4462
  isReplacementConflict: policy.isReplacementConflict,
@@ -4326,6 +4479,17 @@ function createNodeFilesystemIdentityPort(hostPolicy) {
4326
4479
  throw err;
4327
4480
  }
4328
4481
  },
4482
+ async directoryWitness(dir) {
4483
+ let stats;
4484
+ try {
4485
+ stats = await fs2.stat(dir, { bigint: true });
4486
+ } catch (err) {
4487
+ if (isAbsentPathError(err)) return null;
4488
+ throw err;
4489
+ }
4490
+ if (!await witnessTrustedOn(stats.dev, dir)) return null;
4491
+ return { dev: stats.dev, ino: stats.ino, mtimeNs: stats.mtimeNs, ctimeNs: stats.ctimeNs };
4492
+ },
4329
4493
  async open(target) {
4330
4494
  const handle = await fs2.open(target, "r");
4331
4495
  try {
@@ -4636,10 +4800,11 @@ var FilesystemBackend = class _FilesystemBackend {
4636
4800
  return void 0;
4637
4801
  }
4638
4802
  }
4639
- async #readWithEdition(id, edition) {
4803
+ async #readWithEdition(id, edition, pass) {
4640
4804
  assertSafeConceptId(id);
4641
4805
  const rel = pathFromConceptId(id);
4642
- const observed = await observeExact(this.#port, this.#root, rel, (handle) => readBytes(this.#port, handle));
4806
+ const read = (handle) => readBytes(this.#port, handle);
4807
+ const observed = pass ? await pass.observe(rel, read) : await observeExact(this.#port, this.#root, rel, read);
4643
4808
  if (observed.state === "absent") throw notFound(this.#root, rel);
4644
4809
  const raw = observed.value.toString("utf8");
4645
4810
  const { frontmatter: frontmatter2, body } = parseMarkdown(raw, rel, { okfVersion: await edition() });
@@ -4654,7 +4819,12 @@ var FilesystemBackend = class _FilesystemBackend {
4654
4819
  }
4655
4820
  let editionPromise;
4656
4821
  const edition = () => editionPromise ??= this.#readEdition();
4657
- for (const id of ids) out.push(await this.#readWithEdition(id, edition));
4822
+ const pass = openObservationPass(this.#port, this.#root);
4823
+ for (const id of ids) out.push(await this.#readWithEdition(id, edition, pass));
4824
+ const retry = new Set(await pass.finish());
4825
+ for (let index = 0; index < ids.length; index++) {
4826
+ if (retry.has(pathFromConceptId(ids[index]))) out[index] = await this.#readWithEdition(ids[index], edition);
4827
+ }
4658
4828
  return out;
4659
4829
  }
4660
4830
  async write(id, doc, options = {}) {
@@ -31994,6 +31994,12 @@ async function probeSegment(port, candidate, rel, segment) {
31994
31994
  function hasExact(listing, segment) {
31995
31995
  return listing.some((entry) => entry.name === segment);
31996
31996
  }
31997
+ function freshListings(port) {
31998
+ return async (dir) => {
31999
+ const listing = await port.entries(dir);
32000
+ return listing === null ? null : { has: (name) => hasExact(listing, name) };
32001
+ };
32002
+ }
31997
32003
  async function walkExact(port, rootResolved, segments, rel) {
31998
32004
  let parent = rootResolved;
31999
32005
  for (let index2 = 0; index2 < segments.length; index2++) {
@@ -32025,14 +32031,14 @@ async function recordWalk(port, rootResolved, segments, rel) {
32025
32031
  }
32026
32032
  return recorded;
32027
32033
  }
32028
- async function confirmedWalk(port, rootResolved, segments, rel, recorded, handle) {
32034
+ async function confirmedWalk(port, rootResolved, segments, rel, recorded, handle, listings = freshListings(port)) {
32029
32035
  let parent = rootResolved;
32030
32036
  for (let index2 = 0; index2 < segments.length; index2++) {
32031
32037
  const segment = segments[index2];
32032
32038
  const candidate = path2.join(parent, segment);
32033
- const listing = await port.entries(parent);
32039
+ const listing = await listings(parent);
32034
32040
  if (listing === null) return "absent";
32035
- const listed = hasExact(listing, segment);
32041
+ const listed = listing.has(segment);
32036
32042
  if (handle !== null && index2 === segments.length - 1) {
32037
32043
  const verdict = classifyLeaf({ listed, probe: await probeSegment(port, candidate, rel, segment), handle });
32038
32044
  if (verdict === "aliased") throw new FilesystemIdentityAliasError(rel, segment);
@@ -32052,13 +32058,15 @@ function countRestart(restarts, rel) {
32052
32058
  return restarts + 1;
32053
32059
  }
32054
32060
  async function observeExact(port, root, rel, read) {
32061
+ return observeWith(port, path2.resolve(root), rel, read, freshListings(port));
32062
+ }
32063
+ async function observeWith(port, rootResolved, rel, read, listings, onExact) {
32055
32064
  const segments = relSegments(rel);
32056
- const rootResolved = path2.resolve(root);
32057
32065
  const target = path2.join(rootResolved, ...segments);
32058
32066
  for (let restarts = 0; ; ) {
32059
32067
  const recorded = await recordWalk(port, rootResolved, segments, rel);
32060
32068
  if (recorded === null) return { state: "absent" };
32061
- const verified = await confirmedWalk(port, rootResolved, segments, rel, recorded, null);
32069
+ const verified = await confirmedWalk(port, rootResolved, segments, rel, recorded, null, listings);
32062
32070
  if (verified === "absent") return { state: "absent" };
32063
32071
  if (verified === "restart") {
32064
32072
  restarts = countRestart(restarts, rel);
@@ -32080,11 +32088,14 @@ async function observeExact(port, root, rel, read) {
32080
32088
  try {
32081
32089
  value = await read(opened.handle, target);
32082
32090
  if (value === null) return { state: "absent" };
32083
- verdict = await confirmedWalk(port, rootResolved, segments, rel, recorded, opened);
32091
+ verdict = await confirmedWalk(port, rootResolved, segments, rel, recorded, opened, listings);
32084
32092
  } finally {
32085
32093
  await port.close(opened.handle);
32086
32094
  }
32087
- if (verdict === "exact") return { state: "exact", value };
32095
+ if (verdict === "exact") {
32096
+ onExact?.(recorded, { dev: opened.dev, ino: opened.ino });
32097
+ return { state: "exact", value };
32098
+ }
32088
32099
  if (verdict === "absent") return { state: "absent" };
32089
32100
  restarts = countRestart(restarts, rel);
32090
32101
  }
@@ -32103,6 +32114,100 @@ async function probeExact(port, root, rel) {
32103
32114
  restarts = countRestart(restarts, rel);
32104
32115
  }
32105
32116
  }
32117
+ var SETTLED_AFTER_NS = 3000000000n;
32118
+ function witnessKey(witness) {
32119
+ return `${witness.dev}:${witness.ino}:${witness.mtimeNs}:${witness.ctimeNs}`;
32120
+ }
32121
+ function openObservationPass(port, root, now = Date.now) {
32122
+ const rootResolved = path2.resolve(root);
32123
+ const kept = /* @__PURE__ */ new Map();
32124
+ const reusedVerdicts = [];
32125
+ const fresh = freshListings(port);
32126
+ const witnessOf = port.directoryWitness?.bind(port);
32127
+ const settled = (witness) => {
32128
+ const nowNs = BigInt(Math.floor(now())) * 1000000n;
32129
+ const latest = witness.mtimeNs > witness.ctimeNs ? witness.mtimeNs : witness.ctimeNs;
32130
+ return nowNs - latest >= SETTLED_AFTER_NS;
32131
+ };
32132
+ const listingsFor = () => {
32133
+ let reused = false;
32134
+ if (witnessOf === void 0) return { source: fresh, reused: () => false };
32135
+ const source = async (dir) => {
32136
+ const keep = kept.get(dir);
32137
+ if (keep !== void 0) {
32138
+ const current = await witnessOf(dir);
32139
+ if (current !== null && witnessKey(current) === keep.witness) {
32140
+ reused = true;
32141
+ return keep.names;
32142
+ }
32143
+ kept.delete(dir);
32144
+ }
32145
+ const before = await witnessOf(dir);
32146
+ const listing = await port.entries(dir);
32147
+ if (listing === null) return null;
32148
+ const names = new Set(listing.map((entry) => entry.name));
32149
+ const after = await witnessOf(dir);
32150
+ if (before !== null && after !== null && witnessKey(before) === witnessKey(after) && settled(after)) {
32151
+ kept.set(dir, { witness: witnessKey(after), names });
32152
+ }
32153
+ return names;
32154
+ };
32155
+ return { source, reused: () => reused };
32156
+ };
32157
+ return {
32158
+ async observe(rel, read) {
32159
+ const listings = listingsFor();
32160
+ let exact;
32161
+ let observed;
32162
+ try {
32163
+ observed = await observeWith(port, rootResolved, rel, read, listings.source, (recorded, handle) => {
32164
+ exact = { rel, segments: relSegments(rel), recorded, handle };
32165
+ });
32166
+ } catch (error) {
32167
+ if (!listings.reused()) throw error;
32168
+ return observeExact(port, rootResolved, rel, read);
32169
+ }
32170
+ if (!listings.reused()) return observed;
32171
+ if (observed.state === "absent") return observeExact(port, rootResolved, rel, read);
32172
+ if (exact !== void 0) reusedVerdicts.push(exact);
32173
+ return observed;
32174
+ },
32175
+ async finish() {
32176
+ const retry = /* @__PURE__ */ new Set();
32177
+ const endListings = /* @__PURE__ */ new Map();
32178
+ const listedAtEnd = async (dir) => {
32179
+ if (!endListings.has(dir)) {
32180
+ const listing = await port.entries(dir);
32181
+ endListings.set(dir, listing === null ? null : new Set(listing.map((entry) => entry.name)));
32182
+ }
32183
+ return endListings.get(dir);
32184
+ };
32185
+ try {
32186
+ for (const verdict of reusedVerdicts) {
32187
+ if (!await holdsAtEnd(port, rootResolved, verdict, listedAtEnd)) retry.add(verdict.rel);
32188
+ }
32189
+ } finally {
32190
+ kept.clear();
32191
+ reusedVerdicts.length = 0;
32192
+ }
32193
+ return [...retry];
32194
+ }
32195
+ };
32196
+ }
32197
+ async function holdsAtEnd(port, rootResolved, verdict, listedAtEnd) {
32198
+ let parent = rootResolved;
32199
+ for (let index2 = 0; index2 < verdict.segments.length; index2++) {
32200
+ const segment = verdict.segments[index2];
32201
+ const listing = await listedAtEnd(parent);
32202
+ if (listing === null || !listing.has(segment)) return false;
32203
+ const candidate = path2.join(parent, segment);
32204
+ const probed = await port.probe(candidate);
32205
+ const expected = index2 === verdict.segments.length - 1 ? verdict.handle : verdict.recorded[index2];
32206
+ if (probed === null || probed.kind === "symlink" || !sameWitness(probed, expected)) return false;
32207
+ parent = candidate;
32208
+ }
32209
+ return true;
32210
+ }
32106
32211
  var linkUnsupportedRoots = /* @__PURE__ */ new WeakMap();
32107
32212
  function unsupportedLinkRoots(port) {
32108
32213
  let roots = linkUnsupportedRoots.get(port);
@@ -32256,8 +32361,56 @@ function kindOf(stats) {
32256
32361
  if (stats.isFile()) return "file";
32257
32362
  return "other";
32258
32363
  }
32364
+ var LOCAL_LINUX_FILESYSTEMS = /* @__PURE__ */ new Set([
32365
+ 61267,
32366
+ // ext2/3/4
32367
+ 1481003842,
32368
+ // xfs
32369
+ 2435016766,
32370
+ // btrfs
32371
+ 16914836,
32372
+ // tmpfs
32373
+ 2240043254,
32374
+ // ramfs
32375
+ 2035054128,
32376
+ // overlayfs
32377
+ 801189825,
32378
+ // zfs
32379
+ 4076150800,
32380
+ // f2fs
32381
+ 3393526350,
32382
+ // bcachefs
32383
+ 19780,
32384
+ // vfat
32385
+ 538032816,
32386
+ // exfat
32387
+ 1936094318,
32388
+ // ntfs3
32389
+ 18475
32390
+ // hfsplus
32391
+ ]);
32392
+ function directoryWitnessTrusted(platform, filesystemType) {
32393
+ if (platform !== "linux") return true;
32394
+ return filesystemType !== null && LOCAL_LINUX_FILESYSTEMS.has(filesystemType);
32395
+ }
32396
+ function witnessTrust() {
32397
+ const devices = /* @__PURE__ */ new Map();
32398
+ return (dev, dir) => {
32399
+ if (process.platform !== "linux") return Promise.resolve(true);
32400
+ let trusted = devices.get(dev);
32401
+ if (trusted === void 0) {
32402
+ trusted = fs2.statfs(dir).then(
32403
+ (info) => directoryWitnessTrusted("linux", Number(info.type)),
32404
+ () => false
32405
+ );
32406
+ devices.set(dev, trusted);
32407
+ }
32408
+ return trusted;
32409
+ };
32410
+ }
32259
32411
  function createNodeFilesystemIdentityPort(hostPolicy) {
32260
32412
  const policy = captureFilesystemHostPolicy(hostPolicy);
32413
+ const witnessTrustedOn = witnessTrust();
32261
32414
  return Object.freeze({
32262
32415
  isTransientOpenError: policy.isTransientOpenError,
32263
32416
  isReplacementConflict: policy.isReplacementConflict,
@@ -32280,6 +32433,17 @@ function createNodeFilesystemIdentityPort(hostPolicy) {
32280
32433
  throw err;
32281
32434
  }
32282
32435
  },
32436
+ async directoryWitness(dir) {
32437
+ let stats;
32438
+ try {
32439
+ stats = await fs2.stat(dir, { bigint: true });
32440
+ } catch (err) {
32441
+ if (isAbsentPathError(err)) return null;
32442
+ throw err;
32443
+ }
32444
+ if (!await witnessTrustedOn(stats.dev, dir)) return null;
32445
+ return { dev: stats.dev, ino: stats.ino, mtimeNs: stats.mtimeNs, ctimeNs: stats.ctimeNs };
32446
+ },
32283
32447
  async open(target) {
32284
32448
  const handle = await fs2.open(target, "r");
32285
32449
  try {
@@ -32590,10 +32754,11 @@ var FilesystemBackend = class _FilesystemBackend {
32590
32754
  return void 0;
32591
32755
  }
32592
32756
  }
32593
- async #readWithEdition(id, edition) {
32757
+ async #readWithEdition(id, edition, pass) {
32594
32758
  assertSafeConceptId(id);
32595
32759
  const rel = pathFromConceptId(id);
32596
- const observed = await observeExact(this.#port, this.#root, rel, (handle) => readBytes(this.#port, handle));
32760
+ const read = (handle) => readBytes(this.#port, handle);
32761
+ const observed = pass ? await pass.observe(rel, read) : await observeExact(this.#port, this.#root, rel, read);
32597
32762
  if (observed.state === "absent") throw notFound(this.#root, rel);
32598
32763
  const raw = observed.value.toString("utf8");
32599
32764
  const { frontmatter: frontmatter2, body } = parseMarkdown(raw, rel, { okfVersion: await edition() });
@@ -32608,7 +32773,12 @@ var FilesystemBackend = class _FilesystemBackend {
32608
32773
  }
32609
32774
  let editionPromise;
32610
32775
  const edition = () => editionPromise ??= this.#readEdition();
32611
- for (const id of ids) out.push(await this.#readWithEdition(id, edition));
32776
+ const pass = openObservationPass(this.#port, this.#root);
32777
+ for (const id of ids) out.push(await this.#readWithEdition(id, edition, pass));
32778
+ const retry = new Set(await pass.finish());
32779
+ for (let index2 = 0; index2 < ids.length; index2++) {
32780
+ if (retry.has(pathFromConceptId(ids[index2]))) out[index2] = await this.#readWithEdition(ids[index2], edition);
32781
+ }
32612
32782
  return out;
32613
32783
  }
32614
32784
  async write(id, doc, options = {}) {
@@ -32768,14 +32938,36 @@ function publicationContext(backend) {
32768
32938
  return context;
32769
32939
  }
32770
32940
  async function readRawFilesystemDocument(backend, id) {
32941
+ return readRawDocumentIn(backend, id);
32942
+ }
32943
+ async function readRawDocumentIn(backend, id, pass) {
32771
32944
  assertSafeConceptId(id);
32772
32945
  const { root, port } = publicationContext(backend);
32773
32946
  const rel = pathFromConceptId(id);
32774
- const observed = await observeExact(port, root, rel, (handle) => readBytes(port, handle));
32947
+ const read = (handle) => readBytes(port, handle);
32948
+ const observed = pass ? await pass.observe(rel, read) : await observeExact(port, root, rel, read);
32775
32949
  if (observed.state === "absent") throw notFound(root, rel);
32776
32950
  const raw = new TextDecoder("utf-8", { fatal: true }).decode(observed.value);
32777
32951
  return { bytes: observed.value.slice(), version: versionOfBytes(raw) };
32778
32952
  }
32953
+ function openRawFilesystemDocumentPass(backend) {
32954
+ const { root, port } = publicationContext(backend);
32955
+ const pass = openObservationPass(port, root);
32956
+ const ids = /* @__PURE__ */ new Map();
32957
+ return {
32958
+ read(id) {
32959
+ assertSafeConceptId(id);
32960
+ ids.set(pathFromConceptId(id), id);
32961
+ return readRawDocumentIn(backend, id, pass);
32962
+ },
32963
+ async finish() {
32964
+ return (await pass.finish()).flatMap((rel) => {
32965
+ const id = ids.get(rel);
32966
+ return id === void 0 ? [] : [id];
32967
+ });
32968
+ }
32969
+ };
32970
+ }
32779
32971
  async function readRawFilesystemReserved(backend, dir, name) {
32780
32972
  assertSafeReservedDir(dir);
32781
32973
  assertSafeReservedFilename(name);
@@ -43605,10 +43797,20 @@ async function readInventory(backend, limits) {
43605
43797
  return bytes;
43606
43798
  };
43607
43799
  const documents = [];
43800
+ const documentPass = openRawFilesystemDocumentPass(backend);
43608
43801
  for (const id of documentIds) {
43609
- const row = await readRawFilesystemDocument(backend, id);
43802
+ const row = await documentPass.read(id);
43610
43803
  documents.push({ id, version: row.version, bytes: admitBytes(row.bytes, id) });
43611
43804
  }
43805
+ for (const id of await documentPass.finish()) {
43806
+ const row = await readRawFilesystemDocument(backend, id).catch((error) => {
43807
+ if (error?.code !== "ENOENT") throw error;
43808
+ return null;
43809
+ });
43810
+ if (row === null || documents.some((document3) => document3.id === id && document3.version !== row.version)) {
43811
+ throw new PublicationError("SOURCE_CHANGED", "a publication object changed during capture", { retryable: true });
43812
+ }
43813
+ }
43612
43814
  const reserved = [];
43613
43815
  for (const identity of reservedIds) {
43614
43816
  const row = await readRawFilesystemReserved(backend, identity.dir, identity.name);