superbee 0.4.3-pre.1 → 0.4.3-pre.2

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);
package/dist/superbee.mjs CHANGED
@@ -44,7 +44,7 @@ var __toESM = (mod, isNodeMode, target3) => (target3 = mod != null ? __create(__
44
44
  var define_SUPERBEE_BUILD_IDENTITY_default;
45
45
  var init_define_SUPERBEE_BUILD_IDENTITY = __esm({
46
46
  "<define:__SUPERBEE_BUILD_IDENTITY__>"() {
47
- define_SUPERBEE_BUILD_IDENTITY_default = { schema: "superbee.build-identity.v1", package: { name: "superbee", version: "0.4.3-pre.1" }, source: { commit: "0c5c7f2b2437d3e33a4aa707f13704e9896482a6", dirty: false }, artifact: { channel: "npm-package" }, compatibility_contracts: { skill: 1, hook: 1, mcp: 1 } };
47
+ define_SUPERBEE_BUILD_IDENTITY_default = { schema: "superbee.build-identity.v1", package: { name: "superbee", version: "0.4.3-pre.2" }, source: { commit: "9a0a564d4096ec7fe51a7f337e7e001542e8e6d5", dirty: false }, artifact: { channel: "npm-package" }, compatibility_contracts: { skill: 1, hook: 1, mcp: 1 } };
48
48
  }
49
49
  });
50
50
 
@@ -4943,6 +4943,12 @@ async function probeSegment(port, candidate, rel, segment) {
4943
4943
  function hasExact(listing, segment) {
4944
4944
  return listing.some((entry) => entry.name === segment);
4945
4945
  }
4946
+ function freshListings(port) {
4947
+ return async (dir) => {
4948
+ const listing = await port.entries(dir);
4949
+ return listing === null ? null : { has: (name) => hasExact(listing, name) };
4950
+ };
4951
+ }
4946
4952
  async function walkExact(port, rootResolved, segments, rel) {
4947
4953
  let parent = rootResolved;
4948
4954
  for (let index2 = 0; index2 < segments.length; index2++) {
@@ -4973,14 +4979,14 @@ async function recordWalk(port, rootResolved, segments, rel) {
4973
4979
  }
4974
4980
  return recorded;
4975
4981
  }
4976
- async function confirmedWalk(port, rootResolved, segments, rel, recorded, handle) {
4982
+ async function confirmedWalk(port, rootResolved, segments, rel, recorded, handle, listings = freshListings(port)) {
4977
4983
  let parent = rootResolved;
4978
4984
  for (let index2 = 0; index2 < segments.length; index2++) {
4979
4985
  const segment = segments[index2];
4980
4986
  const candidate = path2.join(parent, segment);
4981
- const listing = await port.entries(parent);
4987
+ const listing = await listings(parent);
4982
4988
  if (listing === null) return "absent";
4983
- const listed2 = hasExact(listing, segment);
4989
+ const listed2 = listing.has(segment);
4984
4990
  if (handle !== null && index2 === segments.length - 1) {
4985
4991
  const verdict = classifyLeaf({ listed: listed2, probe: await probeSegment(port, candidate, rel, segment), handle });
4986
4992
  if (verdict === "aliased") throw new FilesystemIdentityAliasError(rel, segment);
@@ -5000,13 +5006,15 @@ function countRestart(restarts, rel) {
5000
5006
  return restarts + 1;
5001
5007
  }
5002
5008
  async function observeExact(port, root, rel, read) {
5009
+ return observeWith(port, path2.resolve(root), rel, read, freshListings(port));
5010
+ }
5011
+ async function observeWith(port, rootResolved, rel, read, listings, onExact) {
5003
5012
  const segments = relSegments(rel);
5004
- const rootResolved = path2.resolve(root);
5005
5013
  const target3 = path2.join(rootResolved, ...segments);
5006
5014
  for (let restarts = 0; ; ) {
5007
5015
  const recorded = await recordWalk(port, rootResolved, segments, rel);
5008
5016
  if (recorded === null) return { state: "absent" };
5009
- const verified2 = await confirmedWalk(port, rootResolved, segments, rel, recorded, null);
5017
+ const verified2 = await confirmedWalk(port, rootResolved, segments, rel, recorded, null, listings);
5010
5018
  if (verified2 === "absent") return { state: "absent" };
5011
5019
  if (verified2 === "restart") {
5012
5020
  restarts = countRestart(restarts, rel);
@@ -5028,11 +5036,14 @@ async function observeExact(port, root, rel, read) {
5028
5036
  try {
5029
5037
  value = await read(opened.handle, target3);
5030
5038
  if (value === null) return { state: "absent" };
5031
- verdict = await confirmedWalk(port, rootResolved, segments, rel, recorded, opened);
5039
+ verdict = await confirmedWalk(port, rootResolved, segments, rel, recorded, opened, listings);
5032
5040
  } finally {
5033
5041
  await port.close(opened.handle);
5034
5042
  }
5035
- if (verdict === "exact") return { state: "exact", value };
5043
+ if (verdict === "exact") {
5044
+ onExact?.(recorded, { dev: opened.dev, ino: opened.ino });
5045
+ return { state: "exact", value };
5046
+ }
5036
5047
  if (verdict === "absent") return { state: "absent" };
5037
5048
  restarts = countRestart(restarts, rel);
5038
5049
  }
@@ -5051,6 +5062,99 @@ async function probeExact(port, root, rel) {
5051
5062
  restarts = countRestart(restarts, rel);
5052
5063
  }
5053
5064
  }
5065
+ function witnessKey(witness) {
5066
+ return `${witness.dev}:${witness.ino}:${witness.mtimeNs}:${witness.ctimeNs}`;
5067
+ }
5068
+ function openObservationPass(port, root, now = Date.now) {
5069
+ const rootResolved = path2.resolve(root);
5070
+ const kept = /* @__PURE__ */ new Map();
5071
+ const reusedVerdicts = [];
5072
+ const fresh = freshListings(port);
5073
+ const witnessOf = port.directoryWitness?.bind(port);
5074
+ const settled = (witness) => {
5075
+ const nowNs = BigInt(Math.floor(now())) * 1000000n;
5076
+ const latest = witness.mtimeNs > witness.ctimeNs ? witness.mtimeNs : witness.ctimeNs;
5077
+ return nowNs - latest >= SETTLED_AFTER_NS;
5078
+ };
5079
+ const listingsFor = () => {
5080
+ let reused = false;
5081
+ if (witnessOf === void 0) return { source: fresh, reused: () => false };
5082
+ const source = async (dir) => {
5083
+ const keep = kept.get(dir);
5084
+ if (keep !== void 0) {
5085
+ const current = await witnessOf(dir);
5086
+ if (current !== null && witnessKey(current) === keep.witness) {
5087
+ reused = true;
5088
+ return keep.names;
5089
+ }
5090
+ kept.delete(dir);
5091
+ }
5092
+ const before = await witnessOf(dir);
5093
+ const listing = await port.entries(dir);
5094
+ if (listing === null) return null;
5095
+ const names = new Set(listing.map((entry) => entry.name));
5096
+ const after = await witnessOf(dir);
5097
+ if (before !== null && after !== null && witnessKey(before) === witnessKey(after) && settled(after)) {
5098
+ kept.set(dir, { witness: witnessKey(after), names });
5099
+ }
5100
+ return names;
5101
+ };
5102
+ return { source, reused: () => reused };
5103
+ };
5104
+ return {
5105
+ async observe(rel, read) {
5106
+ const listings = listingsFor();
5107
+ let exact;
5108
+ let observed;
5109
+ try {
5110
+ observed = await observeWith(port, rootResolved, rel, read, listings.source, (recorded, handle) => {
5111
+ exact = { rel, segments: relSegments(rel), recorded, handle };
5112
+ });
5113
+ } catch (error51) {
5114
+ if (!listings.reused()) throw error51;
5115
+ return observeExact(port, rootResolved, rel, read);
5116
+ }
5117
+ if (!listings.reused()) return observed;
5118
+ if (observed.state === "absent") return observeExact(port, rootResolved, rel, read);
5119
+ if (exact !== void 0) reusedVerdicts.push(exact);
5120
+ return observed;
5121
+ },
5122
+ async finish() {
5123
+ const retry = /* @__PURE__ */ new Set();
5124
+ const endListings = /* @__PURE__ */ new Map();
5125
+ const listedAtEnd = async (dir) => {
5126
+ if (!endListings.has(dir)) {
5127
+ const listing = await port.entries(dir);
5128
+ endListings.set(dir, listing === null ? null : new Set(listing.map((entry) => entry.name)));
5129
+ }
5130
+ return endListings.get(dir);
5131
+ };
5132
+ try {
5133
+ for (const verdict of reusedVerdicts) {
5134
+ if (!await holdsAtEnd(port, rootResolved, verdict, listedAtEnd)) retry.add(verdict.rel);
5135
+ }
5136
+ } finally {
5137
+ kept.clear();
5138
+ reusedVerdicts.length = 0;
5139
+ }
5140
+ return [...retry];
5141
+ }
5142
+ };
5143
+ }
5144
+ async function holdsAtEnd(port, rootResolved, verdict, listedAtEnd) {
5145
+ let parent = rootResolved;
5146
+ for (let index2 = 0; index2 < verdict.segments.length; index2++) {
5147
+ const segment = verdict.segments[index2];
5148
+ const listing = await listedAtEnd(parent);
5149
+ if (listing === null || !listing.has(segment)) return false;
5150
+ const candidate = path2.join(parent, segment);
5151
+ const probed = await port.probe(candidate);
5152
+ const expected = index2 === verdict.segments.length - 1 ? verdict.handle : verdict.recorded[index2];
5153
+ if (probed === null || probed.kind === "symlink" || !sameWitness(probed, expected)) return false;
5154
+ parent = candidate;
5155
+ }
5156
+ return true;
5157
+ }
5054
5158
  function unsupportedLinkRoots(port) {
5055
5159
  let roots = linkUnsupportedRoots.get(port);
5056
5160
  if (!roots) {
@@ -5201,8 +5305,28 @@ function kindOf(stats) {
5201
5305
  if (stats.isFile()) return "file";
5202
5306
  return "other";
5203
5307
  }
5308
+ function directoryWitnessTrusted(platform, filesystemType) {
5309
+ if (platform !== "linux") return true;
5310
+ return filesystemType !== null && LOCAL_LINUX_FILESYSTEMS.has(filesystemType);
5311
+ }
5312
+ function witnessTrust() {
5313
+ const devices = /* @__PURE__ */ new Map();
5314
+ return (dev, dir) => {
5315
+ if (process.platform !== "linux") return Promise.resolve(true);
5316
+ let trusted = devices.get(dev);
5317
+ if (trusted === void 0) {
5318
+ trusted = fs2.statfs(dir).then(
5319
+ (info) => directoryWitnessTrusted("linux", Number(info.type)),
5320
+ () => false
5321
+ );
5322
+ devices.set(dev, trusted);
5323
+ }
5324
+ return trusted;
5325
+ };
5326
+ }
5204
5327
  function createNodeFilesystemIdentityPort(hostPolicy) {
5205
5328
  const policy = captureFilesystemHostPolicy(hostPolicy);
5329
+ const witnessTrustedOn = witnessTrust();
5206
5330
  return Object.freeze({
5207
5331
  isTransientOpenError: policy.isTransientOpenError,
5208
5332
  isReplacementConflict: policy.isReplacementConflict,
@@ -5225,6 +5349,17 @@ function createNodeFilesystemIdentityPort(hostPolicy) {
5225
5349
  throw err;
5226
5350
  }
5227
5351
  },
5352
+ async directoryWitness(dir) {
5353
+ let stats;
5354
+ try {
5355
+ stats = await fs2.stat(dir, { bigint: true });
5356
+ } catch (err) {
5357
+ if (isAbsentPathError(err)) return null;
5358
+ throw err;
5359
+ }
5360
+ if (!await witnessTrustedOn(stats.dev, dir)) return null;
5361
+ return { dev: stats.dev, ino: stats.ino, mtimeNs: stats.mtimeNs, ctimeNs: stats.ctimeNs };
5362
+ },
5228
5363
  async open(target3) {
5229
5364
  const handle = await fs2.open(target3, "r");
5230
5365
  try {
@@ -5279,7 +5414,7 @@ function createNodeFilesystemIdentityPort(hostPolicy) {
5279
5414
  }
5280
5415
  });
5281
5416
  }
5282
- var FilesystemSymlinkEntryError, FilesystemShapeMismatchError, ABSENT_PATH_CODES, MAX_RESTARTS, linkUnsupportedRoots, queues, ABSENT_FILE_CODES;
5417
+ var FilesystemSymlinkEntryError, FilesystemShapeMismatchError, ABSENT_PATH_CODES, MAX_RESTARTS, SETTLED_AFTER_NS, linkUnsupportedRoots, queues, ABSENT_FILE_CODES, LOCAL_LINUX_FILESYSTEMS;
5283
5418
  var init_filesystem_identity = __esm({
5284
5419
  "../core/src/filesystem-identity.ts"() {
5285
5420
  "use strict";
@@ -5310,9 +5445,38 @@ var init_filesystem_identity = __esm({
5310
5445
  };
5311
5446
  ABSENT_PATH_CODES = /* @__PURE__ */ new Set(["ENOENT", "ENOTDIR"]);
5312
5447
  MAX_RESTARTS = 3;
5448
+ SETTLED_AFTER_NS = 3000000000n;
5313
5449
  linkUnsupportedRoots = /* @__PURE__ */ new WeakMap();
5314
5450
  queues = /* @__PURE__ */ new Map();
5315
5451
  ABSENT_FILE_CODES = /* @__PURE__ */ new Set(["ENOENT", "ENOTDIR", "EISDIR"]);
5452
+ LOCAL_LINUX_FILESYSTEMS = /* @__PURE__ */ new Set([
5453
+ 61267,
5454
+ // ext2/3/4
5455
+ 1481003842,
5456
+ // xfs
5457
+ 2435016766,
5458
+ // btrfs
5459
+ 16914836,
5460
+ // tmpfs
5461
+ 2240043254,
5462
+ // ramfs
5463
+ 2035054128,
5464
+ // overlayfs
5465
+ 801189825,
5466
+ // zfs
5467
+ 4076150800,
5468
+ // f2fs
5469
+ 3393526350,
5470
+ // bcachefs
5471
+ 19780,
5472
+ // vfat
5473
+ 538032816,
5474
+ // exfat
5475
+ 1936094318,
5476
+ // ntfs3
5477
+ 18475
5478
+ // hfsplus
5479
+ ]);
5316
5480
  }
5317
5481
  });
5318
5482
 
@@ -5627,10 +5791,11 @@ var init_backend = __esm({
5627
5791
  return void 0;
5628
5792
  }
5629
5793
  }
5630
- async #readWithEdition(id, edition) {
5794
+ async #readWithEdition(id, edition, pass) {
5631
5795
  assertSafeConceptId(id);
5632
5796
  const rel = pathFromConceptId(id);
5633
- const observed = await observeExact(this.#port, this.#root, rel, (handle) => readBytes(this.#port, handle));
5797
+ const read = (handle) => readBytes(this.#port, handle);
5798
+ const observed = pass ? await pass.observe(rel, read) : await observeExact(this.#port, this.#root, rel, read);
5634
5799
  if (observed.state === "absent") throw notFound(this.#root, rel);
5635
5800
  const raw = observed.value.toString("utf8");
5636
5801
  const { frontmatter, body } = parseMarkdown(raw, rel, { okfVersion: await edition() });
@@ -5645,7 +5810,12 @@ var init_backend = __esm({
5645
5810
  }
5646
5811
  let editionPromise;
5647
5812
  const edition = () => editionPromise ??= this.#readEdition();
5648
- for (const id of ids) out.push(await this.#readWithEdition(id, edition));
5813
+ const pass = openObservationPass(this.#port, this.#root);
5814
+ for (const id of ids) out.push(await this.#readWithEdition(id, edition, pass));
5815
+ const retry = new Set(await pass.finish());
5816
+ for (let index2 = 0; index2 < ids.length; index2++) {
5817
+ if (retry.has(pathFromConceptId(ids[index2]))) out[index2] = await this.#readWithEdition(ids[index2], edition);
5818
+ }
5649
5819
  return out;
5650
5820
  }
5651
5821
  async write(id, doc2, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "superbee",
3
- "version": "0.4.3-pre.1",
3
+ "version": "0.4.3-pre.2",
4
4
  "type": "module",
5
5
  "description": "Agent-facing Superbee CLI for reading and writing local OKF knowledge bundles: context notes, docs, cross-links, and live bundle Views.",
6
6
  "keywords": [