@ai-ecoverse/slicc-kernel 1.189.0 → 1.189.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.
@@ -684,7 +684,10 @@ function remoteWorker(open, options = {}) {
684
684
  opened2 = await open(wire);
685
685
  } catch (err) {
686
686
  kernel.end();
687
- queueMicrotask(() => fail4(err instanceof Error ? err.message : String(err)));
687
+ const status = err?.exitStatus;
688
+ const message3 = err instanceof Error ? err.message : String(err);
689
+ if (typeof status === "number") refuse2(message3, status);
690
+ else queueMicrotask(() => fail4(message3));
688
691
  return;
689
692
  }
690
693
  if (ended) {
@@ -4818,7 +4821,7 @@ function satisfies(version, range2) {
4818
4821
  }
4819
4822
 
4820
4823
  // src/version.ts
4821
- var KERNEL_VERSION = true ? "1.189.0" : "0.0.0-dev";
4824
+ var KERNEL_VERSION = true ? "1.189.2" : "0.0.0-dev";
4822
4825
  var OS_RELEASE = "6.0.0-slicc";
4823
4826
  function procVersion() {
4824
4827
  return `Linux version ${OS_RELEASE} (slicc-kernel ${KERNEL_VERSION}) #1 SMP PREEMPT_DYNAMIC
@@ -12017,6 +12020,35 @@ function statOf2(attr2, path, mount) {
12017
12020
  } : {}
12018
12021
  };
12019
12022
  }
12023
+ var Gathered = class {
12024
+ bytes = new Uint8Array(0);
12025
+ owned = false;
12026
+ size;
12027
+ length = 0;
12028
+ constructor(size) {
12029
+ this.size = size;
12030
+ }
12031
+ add(chunk2) {
12032
+ const need2 = this.length + chunk2.length;
12033
+ if (this.length === 0) {
12034
+ this.bytes = chunk2;
12035
+ } else {
12036
+ if (!this.owned || this.bytes.length < need2) {
12037
+ const grown = new Uint8Array(
12038
+ Math.max(this.size, need2, this.owned ? this.bytes.length * 2 : 0)
12039
+ );
12040
+ grown.set(this.bytes.subarray(0, this.length));
12041
+ this.bytes = grown;
12042
+ this.owned = true;
12043
+ }
12044
+ this.bytes.set(chunk2, this.length);
12045
+ }
12046
+ this.length = need2;
12047
+ }
12048
+ get whole() {
12049
+ return this.length === this.bytes.length ? this.bytes : this.bytes.subarray(0, this.length);
12050
+ }
12051
+ };
12020
12052
  function concat4(chunks2, total) {
12021
12053
  if (chunks2.length === 1 && chunks2[0]?.length === total) return chunks2[0];
12022
12054
  const out = new Uint8Array(total);
@@ -12093,6 +12125,10 @@ var MountTable = class {
12093
12125
  for (const mount of this.mounts.values()) if (within5(mount.target, path)) return true;
12094
12126
  return false;
12095
12127
  }
12128
+ readOnlyAt(path) {
12129
+ const found = this.locate(path);
12130
+ return found !== void 0 && readonly(found.mount);
12131
+ }
12096
12132
  locate(path) {
12097
12133
  let best;
12098
12134
  for (const mount of this.mounts.values()) {
@@ -12509,25 +12545,23 @@ var MountFs = class _MountFs {
12509
12545
  }
12510
12546
  const fh = await this.open(mount, rel2, false);
12511
12547
  const chunk2 = mount.caps.maxIo ?? DEFAULT_IO;
12512
- const chunks2 = [];
12513
- let total = 0;
12548
+ const out = new Gathered(attr2.size);
12514
12549
  try {
12515
12550
  for (; ; ) {
12516
12551
  const bytes = await mount.conn.call({
12517
12552
  op: "read",
12518
12553
  fh,
12519
- offset: total,
12554
+ offset: out.length,
12520
12555
  size: chunk2
12521
12556
  });
12522
12557
  if (bytes.length === 0) break;
12523
- chunks2.push(bytes);
12524
- total += bytes.length;
12525
- if (bytes.length < chunk2 && total >= attr2.size) break;
12558
+ out.add(bytes);
12559
+ if (bytes.length < chunk2 && out.length >= attr2.size) break;
12526
12560
  }
12527
12561
  } finally {
12528
12562
  await mount.conn.call({ op: "release", fh });
12529
12563
  }
12530
- return concat4(chunks2, total);
12564
+ return out.whole;
12531
12565
  }
12532
12566
  async writeFile(path, content, owner, redirects = 0) {
12533
12567
  const real2 = await this.follow(path, 0, true);
@@ -34888,6 +34922,8 @@ var Launcher = class {
34888
34922
  tagBases = /* @__PURE__ */ new Map();
34889
34923
  links = /* @__PURE__ */ new Map();
34890
34924
  freshModules = new FreshModules();
34925
+ imagePaths = /* @__PURE__ */ new WeakMap();
34926
+ imageHolds = /* @__PURE__ */ new Map();
34891
34927
  granted = /* @__PURE__ */ new Map();
34892
34928
  users;
34893
34929
  sudoer;
@@ -35877,20 +35913,23 @@ var Launcher = class {
35877
35913
  const pin = await this.pinOf(path, caller);
35878
35914
  const sum = Object.values(pin)[0];
35879
35915
  const key = sum === void 0 ? await this.cacheKey(path, asked) : `${path}:sha256:${sum}`;
35880
- return this.cached(this.compiled, key, async () => {
35916
+ const loaded = this.cached(this.compiled, key, async () => {
35881
35917
  const { bytes: read, key: readKey } = await this.keyed(path, caller, asked);
35882
35918
  const bytes = sum === void 0 ? read : await this.pinnedBytes(pin, read);
35883
35919
  const memory = importedMemory(bytes);
35884
35920
  const foreign = foreignImports(bytes);
35885
35921
  const libc = libcVersionOf(bytes);
35886
35922
  const named = customSectionsOf(bytes, "name").length > 0;
35887
- const compiled = this.compile ? await this.compile(bytes, key) : await WebAssembly.compile(bytes);
35923
+ const compiled = this.compile ? await this.compile(bytes, key, this.sourceOf(path)) : await WebAssembly.compile(bytes);
35888
35924
  if (libc !== void 0) this.libcs.set(compiled, libc);
35889
35925
  return {
35890
35926
  value: { module: compiled, ...memory ? { memory } : {}, foreign, named },
35891
35927
  key: sum === void 0 ? readKey : key
35892
35928
  };
35893
35929
  });
35930
+ const value2 = await loaded;
35931
+ this.imagePaths.set(value2.module, path);
35932
+ return value2;
35894
35933
  }
35895
35934
  async checkedSources(planned, caller) {
35896
35935
  const { target } = planned;
@@ -35996,10 +36035,52 @@ var Launcher = class {
35996
36035
  );
35997
36036
  }
35998
36037
  reread(target, read) {
36038
+ this.forgetCompiled(target.wasm);
36039
+ return read();
36040
+ }
36041
+ forgetCompiled(path) {
35999
36042
  for (const key of this.compiled.keys()) {
36000
- if (key.startsWith(`${target.wasm}:`)) this.compiled.delete(key);
36043
+ if (key.startsWith(`${path}:`) || key.startsWith(`${path}\0`)) this.compiled.delete(key);
36001
36044
  }
36002
- return read();
36045
+ }
36046
+ sourceOf(path) {
36047
+ return {
36048
+ path,
36049
+ read: () => this.base.readFileBuffer(path),
36050
+ stale: () => this.forgetCompiled(path),
36051
+ kept: () => this.keptCopies(path).map((at) => ({ path: at, read: () => this.base.readFileBuffer(at) }))
36052
+ };
36053
+ }
36054
+ keptCopies(path) {
36055
+ const out = /* @__PURE__ */ new Set();
36056
+ for (const hold of this.imageHolds.get(path) ?? []) {
36057
+ for (const key of hold.holds.held()) out.add(hold.holds.keptAt(key)?.hidden ?? key);
36058
+ }
36059
+ out.delete(path);
36060
+ return [...out];
36061
+ }
36062
+ holdImage(program) {
36063
+ const module = program.abi === "js" ? void 0 : program.module;
36064
+ const path = this.compile && module ? this.imagePaths.get(module) : void 0;
36065
+ if (!module || path === void 0 || this.mounts.readOnlyAt(path)) return (status) => status;
36066
+ const holds3 = this.imageHolds.get(path) ?? /* @__PURE__ */ new Set();
36067
+ this.imageHolds.set(path, holds3);
36068
+ let hold = [...holds3].find((known) => known.module === module);
36069
+ if (!hold) {
36070
+ hold = { module, holds: new HeldPaths(), users: 0 };
36071
+ hold.holds.hold(path, true, true);
36072
+ holds3.add(hold);
36073
+ this.held.add(hold.holds);
36074
+ }
36075
+ const mine = hold;
36076
+ mine.users++;
36077
+ return (status) => {
36078
+ if (--mine.users > 0) return status;
36079
+ holds3.delete(mine);
36080
+ if (holds3.size === 0 && this.imageHolds.get(path) === holds3) this.imageHolds.delete(path);
36081
+ this.held.delete(mine.holds);
36082
+ return status;
36083
+ };
36003
36084
  }
36004
36085
  exeId(target) {
36005
36086
  if (target.abi !== "wasi" || (target.copy ?? target.glue) !== target.wasm) {
@@ -36410,7 +36491,7 @@ var Launcher = class {
36410
36491
  handle.onState(
36411
36492
  (state) => state === "stopped" ? this.stopped.add(pid) : this.stopped.delete(pid)
36412
36493
  );
36413
- void handle.exited.then((status) => {
36494
+ void handle.exited.then(this.holdImage(req.program)).then((status) => {
36414
36495
  this.traceEnded(pid, handle, status);
36415
36496
  this.cwds.release(this.cwdSlots.get(pid));
36416
36497
  this.cwdSlots.delete(pid);
@@ -39037,38 +39118,99 @@ function initialMemory(bytes) {
39037
39118
  }
39038
39119
  return 0;
39039
39120
  }
39121
+ var viewOf = (source) => source instanceof ArrayBuffer ? new Uint8Array(source) : new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
39122
+ var bufferOf2 = (bytes) => bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength && bytes.buffer instanceof ArrayBuffer ? bytes.buffer : bytes.slice().buffer;
39123
+ var ImageError = class extends Error {
39124
+ code;
39125
+ exitStatus;
39126
+ constructor(code, message3) {
39127
+ super(`${code}: ${message3}`);
39128
+ this.code = code;
39129
+ this.exitStatus = code === "ENOENT" ? 127 : 126;
39130
+ }
39131
+ };
39132
+ function unread(path, err) {
39133
+ const code = err?.code;
39134
+ if (code === "ENOENT" || code === "ENOTDIR") {
39135
+ return new ImageError("ENOENT", `${path} is gone since the kernel compiled it`);
39136
+ }
39137
+ const why = err instanceof Error ? err.message : String(err);
39138
+ return new ImageError(typeof code === "string" ? code : "EIO", `${path} cannot be read: ${why}`);
39139
+ }
39040
39140
  var Images = class {
39041
- initial = /* @__PURE__ */ new Map();
39042
- bytes = /* @__PURE__ */ new Map();
39043
- handles = /* @__PURE__ */ new Map();
39044
- async register(source) {
39045
- const view2 = source instanceof ArrayBuffer ? new Uint8Array(source) : new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
39141
+ images = /* @__PURE__ */ new Map();
39142
+ reading = /* @__PURE__ */ new Map();
39143
+ async register(source, from) {
39144
+ const view2 = viewOf(source);
39046
39145
  const image = await digest(view2);
39047
- const known = this.handles.get(image);
39048
- if (known) return known;
39146
+ const known = this.images.get(image);
39147
+ if (known) {
39148
+ if (from) known.sources.set(from.path, from);
39149
+ else known.bytes ??= view2.slice().buffer;
39150
+ return known.handle;
39151
+ }
39049
39152
  const handle = Object.freeze({ image });
39050
- this.bytes.set(image, view2.slice().buffer);
39051
- this.initial.set(image, initialMemory(view2));
39052
- this.handles.set(image, handle);
39153
+ this.images.set(image, {
39154
+ handle,
39155
+ initial: initialMemory(view2),
39156
+ module: view2.byteLength,
39157
+ sources: new Map(from ? [[from.path, from]] : []),
39158
+ ...from ? {} : { bytes: view2.slice().buffer }
39159
+ });
39053
39160
  return handle;
39054
39161
  }
39055
- compile = async (bytes) => await this.register(bytes);
39162
+ compile = async (bytes, _key, from) => await this.register(bytes, from);
39163
+ load(image) {
39164
+ const entry = this.images.get(image);
39165
+ if (!entry) return Promise.reject(new ImageError("ENOENT", `no program image ${image}`));
39166
+ if (entry.bytes) return Promise.resolve(entry.bytes);
39167
+ const known = this.reading.get(image);
39168
+ if (known !== void 0) return known;
39169
+ const read = this.read(image, entry).finally(() => this.reading.delete(image));
39170
+ this.reading.set(image, read);
39171
+ return read;
39172
+ }
39173
+ async read(image, entry) {
39174
+ let failed4;
39175
+ for (const source of [...entry.sources.values()]) {
39176
+ for (const kept of source.kept?.() ?? []) {
39177
+ const bytes = await kept.read().catch(() => void 0);
39178
+ if (bytes && await this.matches(bytes, entry, image)) return bufferOf2(bytes);
39179
+ }
39180
+ try {
39181
+ const bytes = await source.read();
39182
+ if (await this.matches(bytes, entry, image)) return bufferOf2(bytes);
39183
+ failed4 ??= new ImageError("ESTALE", `${source.path} changed since the kernel compiled it`);
39184
+ } catch (err) {
39185
+ failed4 ??= unread(source.path, err);
39186
+ }
39187
+ entry.sources.delete(source.path);
39188
+ source.stale?.();
39189
+ }
39190
+ throw failed4 ?? new ImageError("ENOENT", `no file holds program image ${image}`);
39191
+ }
39192
+ async matches(bytes, entry, image) {
39193
+ return bytes.byteLength === entry.module && await digest(bytes) === image;
39194
+ }
39056
39195
  get(image) {
39057
- const bytes = this.bytes.get(image);
39058
- if (!bytes) throw new Error(`no program image ${image}`);
39196
+ const bytes = this.images.get(image)?.bytes;
39197
+ if (!bytes) throw new Error(`no program image ${image} kept in memory: load() reads it`);
39059
39198
  return bytes;
39060
39199
  }
39061
39200
  sizeOf(image) {
39062
- return {
39063
- initial: this.initial.get(image) ?? 0,
39064
- module: this.bytes.get(image)?.byteLength ?? 0
39065
- };
39201
+ const entry = this.images.get(image);
39202
+ return { initial: entry?.initial ?? 0, module: entry?.module ?? 0 };
39066
39203
  }
39067
39204
  has(image) {
39068
- return this.bytes.has(image);
39205
+ return this.images.has(image);
39069
39206
  }
39070
39207
  get size() {
39071
- return this.bytes.size;
39208
+ return this.images.size;
39209
+ }
39210
+ get held() {
39211
+ let total = 0;
39212
+ for (const entry of this.images.values()) total += entry.bytes?.byteLength ?? 0;
39213
+ return total;
39072
39214
  }
39073
39215
  };
39074
39216
  function imageOf(module) {
@@ -39829,12 +39971,16 @@ function parentOf11(init) {
39829
39971
  function cloudflareProcesses(loader, images, gate, code, memory = {}) {
39830
39972
  let hostDigest;
39831
39973
  let loads = 0;
39974
+ const failedLoads = /* @__PURE__ */ new Map();
39832
39975
  const idOf3 = async (ref) => {
39833
39976
  hostDigest ??= digest(`${code.host}\0${code.process}\0${code.localForks !== false}`);
39834
39977
  const imports = ref.imports ? `-${ref.imports.digest.slice(0, 16)}` : "";
39835
- return `slicc-${(await hostDigest).slice(0, 16)}-${ref.image.slice(0, 40)}${imports}`;
39978
+ const failed4 = failedLoads.get(keyOf3(ref));
39979
+ const again = failed4 === void 0 ? "" : `-r${failed4}`;
39980
+ return `slicc-${(await hostDigest).slice(0, 16)}-${ref.image.slice(0, 40)}${imports}${again}`;
39836
39981
  };
39837
- const workerCode = (ref) => {
39982
+ const workerCode = async (ref) => {
39983
+ const wasm = await images.load(ref.image);
39838
39984
  loads++;
39839
39985
  const env = {
39840
39986
  ...code.localForks === false ? { SLICC_LOCAL_FORKS: "off" } : {},
@@ -39847,7 +39993,7 @@ function cloudflareProcesses(loader, images, gate, code, memory = {}) {
39847
39993
  modules: {
39848
39994
  "host.js": code.host,
39849
39995
  "process.js": code.process,
39850
- "program.wasm": { wasm: images.get(ref.image) },
39996
+ "program.wasm": { wasm },
39851
39997
  "imports.js": ref.imports?.source ?? NO_IMPORTS_MODULE
39852
39998
  },
39853
39999
  globalOutbound: null,
@@ -39932,8 +40078,13 @@ function cloudflareProcesses(loader, images, gate, code, memory = {}) {
39932
40078
  if (pid !== void 0 && seated.get(pid) === seat) seated.delete(pid);
39933
40079
  seat.release();
39934
40080
  };
40081
+ const cold = {};
40082
+ const code2 = () => workerCode(ref).catch((err) => {
40083
+ cold.failed = err;
40084
+ throw err;
40085
+ });
39935
40086
  try {
39936
- const host = loader.get(seat.id, () => workerCode(ref)).getEntrypoint();
40087
+ const host = loader.get(seat.id, code2).getEntrypoint();
39937
40088
  return await openLink(host, release, {
39938
40089
  forks,
39939
40090
  sized: (bytes) => seat.memory(bytes),
@@ -39942,7 +40093,11 @@ function cloudflareProcesses(loader, images, gate, code, memory = {}) {
39942
40093
  });
39943
40094
  } catch (err) {
39944
40095
  release();
39945
- throw err;
40096
+ const failed4 = cold.failed ?? err;
40097
+ if (failed4 instanceof ImageError) {
40098
+ failedLoads.set(keyOf3(ref), (failedLoads.get(keyOf3(ref)) ?? 0) + 1);
40099
+ }
40100
+ throw failed4;
39946
40101
  }
39947
40102
  }
39948
40103
  };
@@ -40093,6 +40248,10 @@ var SqlStore = class {
40093
40248
  rows(query, ...bindings) {
40094
40249
  return this.sql.exec(query, ...bindings).toArray();
40095
40250
  }
40251
+ each(query, ...bindings) {
40252
+ const cursor = this.sql.exec(query, ...bindings);
40253
+ return cursor[Symbol.iterator] ? cursor : cursor.toArray();
40254
+ }
40096
40255
  row(query, ...bindings) {
40097
40256
  return this.rows(query, ...bindings)[0];
40098
40257
  }
@@ -40524,7 +40683,7 @@ var Files = class {
40524
40683
  const first = Math.floor(start2 / CHUNK_BYTES);
40525
40684
  const last = Math.floor((end - 1) / CHUNK_BYTES);
40526
40685
  const state = this.chunks.get(node);
40527
- const rows = this.store.rows(
40686
+ const rows = this.store.each(
40528
40687
  `SELECT idx, substr(data, max(? - idx * ${CHUNK_BYTES}, 0) + 1, min(? - idx * ${CHUNK_BYTES}, ${CHUNK_BYTES}) - max(? - idx * ${CHUNK_BYTES}, 0)) AS part FROM slicc_chunks WHERE node = ? AND idx BETWEEN ? AND ?`,
40529
40688
  start2,
40530
40689
  end,