@ai-ecoverse/slicc-kernel 1.77.2 → 1.78.0

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.
package/dist/index.js CHANGED
@@ -326,6 +326,9 @@ function clipboardAnswerer(port, hook) {
326
326
  };
327
327
  }
328
328
 
329
+ // src/fs/types.ts
330
+ var DEFAULT_USERS = "enforce";
331
+
329
332
  // src/fs/protect.ts
330
333
  function protectInit(protection) {
331
334
  const writers = [...writerTags(protection)];
@@ -546,6 +549,62 @@ function transferOf(reply) {
546
549
  return [reply.bytes.buffer];
547
550
  }
548
551
 
552
+ // src/kernel/signals.ts
553
+ var SIG = {
554
+ HUP: 1,
555
+ INT: 2,
556
+ QUIT: 3,
557
+ ILL: 4,
558
+ TRAP: 5,
559
+ ABRT: 6,
560
+ BUS: 7,
561
+ FPE: 8,
562
+ KILL: 9,
563
+ USR1: 10,
564
+ SEGV: 11,
565
+ USR2: 12,
566
+ PIPE: 13,
567
+ ALRM: 14,
568
+ TERM: 15,
569
+ STKFLT: 16,
570
+ CHLD: 17,
571
+ CONT: 18,
572
+ STOP: 19,
573
+ TSTP: 20,
574
+ TTIN: 21,
575
+ TTOU: 22,
576
+ URG: 23,
577
+ XCPU: 24,
578
+ XFSZ: 25,
579
+ VTALRM: 26,
580
+ PROF: 27,
581
+ WINCH: 28,
582
+ POLL: 29,
583
+ PWR: 30,
584
+ SYS: 31
585
+ };
586
+ var IGNORED_BY_DEFAULT = /* @__PURE__ */ new Set([SIG.CHLD, SIG.URG, SIG.WINCH]);
587
+ var STOPPING = /* @__PURE__ */ new Set([SIG.STOP, SIG.TSTP, SIG.TTIN, SIG.TTOU]);
588
+ function signalName(sig) {
589
+ const name = Object.entries(SIG).find(([, n]) => n === sig)?.[0];
590
+ return name === void 0 ? void 0 : `SIG${name}`;
591
+ }
592
+ function quiet(p) {
593
+ p.catch(() => void 0);
594
+ return p;
595
+ }
596
+ function signalField(sig) {
597
+ const name = signalName(sig);
598
+ return name ? { signal: name } : {};
599
+ }
600
+ var SIGNAL_BY_NAME = {
601
+ SIGINT: SIG.INT,
602
+ SIGTERM: SIG.TERM,
603
+ SIGKILL: SIG.KILL,
604
+ SIGSTOP: SIG.STOP,
605
+ SIGCONT: SIG.CONT
606
+ };
607
+
549
608
  // src/sealed-relay.ts
550
609
  function sealedAnswerer(port, hook) {
551
610
  const asked = /* @__PURE__ */ new Map();
@@ -1328,6 +1387,7 @@ var Channel = class {
1328
1387
  return;
1329
1388
  }
1330
1389
  this.pending.delete(reply.id);
1390
+ call.ended?.(reply.signal);
1331
1391
  if (reply.error !== void 0) call.reject(new KernelCallError(reply.error, reply.code));
1332
1392
  else call.resolve(reply.result);
1333
1393
  }
@@ -1376,12 +1436,14 @@ async function spawnOn(channel, argv, opts) {
1376
1436
  if (opts.user !== void 0) await since(channel, 8, "spawn as a user", async () => {
1377
1437
  });
1378
1438
  const started = Promise.withResolvers();
1439
+ let endedBy;
1379
1440
  const options = { ...rest, ...stdin !== void 0 ? { stdin: bytesOf(stdin) } : {} };
1380
1441
  const { done } = channel.request(
1381
1442
  { op: "spawn", argv, options },
1382
1443
  {
1383
1444
  output: (fd, bytes) => (fd === 1 ? onStdout : onStderr)?.(bytes),
1384
- started: started.resolve
1445
+ started: started.resolve,
1446
+ ended: (signal) => endedBy = signal
1385
1447
  }
1386
1448
  );
1387
1449
  done.catch(started.reject);
@@ -1391,9 +1453,18 @@ async function spawnOn(channel, argv, opts) {
1391
1453
  pid,
1392
1454
  pgid,
1393
1455
  exited: done,
1456
+ ended: endedOf(done, () => endedBy),
1394
1457
  signal: async (name = "SIGTERM") => void await channel.call({ op: "kill", pid: -pgid, signal: name })
1395
1458
  };
1396
1459
  }
1460
+ function endedOf(done, signal) {
1461
+ return quiet(
1462
+ done.then((status) => {
1463
+ const by = signal();
1464
+ return { status, ...by ? { signal: by } : {} };
1465
+ })
1466
+ );
1467
+ }
1397
1468
  async function runOn(channel, argv, opts) {
1398
1469
  const { onStdout, onStderr, ...rest } = opts;
1399
1470
  const chunks3 = { 1: [], 2: [] };
@@ -1407,9 +1478,15 @@ async function runOn(channel, argv, opts) {
1407
1478
  onStdout: tee(1, onStdout),
1408
1479
  onStderr: tee(2, onStderr)
1409
1480
  });
1410
- const status = await child.exited;
1481
+ const { status, signal } = await child.ended;
1411
1482
  const text = (fd) => new TextDecoder().decode(concat(chunks3[fd]));
1412
- return { pid: child.pid, status, stdout: text(1), stderr: text(2) };
1483
+ return {
1484
+ pid: child.pid,
1485
+ status,
1486
+ ...signal ? { signal } : {},
1487
+ stdout: text(1),
1488
+ stderr: text(2)
1489
+ };
1413
1490
  }
1414
1491
  async function terminalOn(channel, argv, opts) {
1415
1492
  const { onData, ...options } = opts;
@@ -1418,11 +1495,13 @@ async function terminalOn(channel, argv, opts) {
1418
1495
  let listener = onData ?? null;
1419
1496
  const backlog = [];
1420
1497
  const started = Promise.withResolvers();
1498
+ let endedBy;
1421
1499
  const { id, done } = channel.request(
1422
1500
  { op: "open-terminal", argv, options },
1423
1501
  {
1424
1502
  output: (_fd, bytes) => listener ? listener(bytes) : backlog.push(bytes),
1425
- started: started.resolve
1503
+ started: started.resolve,
1504
+ ended: (signal) => endedBy = signal
1426
1505
  }
1427
1506
  );
1428
1507
  done.catch(started.reject);
@@ -1431,6 +1510,7 @@ async function terminalOn(channel, argv, opts) {
1431
1510
  return {
1432
1511
  pid,
1433
1512
  exited: done,
1513
+ ended: endedOf(done, () => endedBy),
1434
1514
  get onData() {
1435
1515
  return listener;
1436
1516
  },
@@ -1890,7 +1970,7 @@ function initCall(options, transport) {
1890
1970
  ...options.hostfs ? { hostfs: true } : {},
1891
1971
  ...options.cdp ? { cdp: true } : {},
1892
1972
  ...options.hostname !== void 0 ? { hostname: options.hostname } : {},
1893
- ...options.users !== void 0 ? { users: options.users } : {},
1973
+ users: options.users === void 0 ? DEFAULT_USERS : options.users,
1894
1974
  ...options.onDenial ? { denials: true } : {},
1895
1975
  ...options.sealed ? { sealed: true } : {},
1896
1976
  ...options.clipboard ? { clipboard: true } : {},
@@ -1933,7 +2013,7 @@ function kernelGroups(call) {
1933
2013
  function accounts(call, mode) {
1934
2014
  return { users: kernelUsers(call, mode), groups: kernelGroups(call) };
1935
2015
  }
1936
- function kernelUsers(call, mode = "off") {
2016
+ function kernelUsers(call, mode = DEFAULT_USERS) {
1937
2017
  return {
1938
2018
  mode,
1939
2019
  add: async (user) => await call({ op: "users-add", user }),
@@ -2018,6 +2098,20 @@ function hookRelays(worker, options) {
2018
2098
  close: () => sealed.close()
2019
2099
  };
2020
2100
  }
2101
+ function decodedRun(result) {
2102
+ const decoder2 = new TextDecoder();
2103
+ return {
2104
+ status: result.status,
2105
+ ...signalField(result.signal),
2106
+ stdout: decoder2.decode(result.stdout),
2107
+ stderr: decoder2.decode(result.stderr)
2108
+ };
2109
+ }
2110
+ function endedOf2(r) {
2111
+ if (typeof r === "number") return { status: r };
2112
+ const { status, signal } = r;
2113
+ return { status, ...signalField(signal) };
2114
+ }
2021
2115
  async function createKernel(options = {}) {
2022
2116
  checkOptions(options);
2023
2117
  writerTags(options.protect);
@@ -2127,7 +2221,8 @@ async function createKernel(options = {}) {
2127
2221
  const send2 = (action) => void call({ op: "terminal", terminal: id, ...action }).catch(() => void 0);
2128
2222
  return {
2129
2223
  pid,
2130
- exited: done,
2224
+ exited: quiet(done.then((r) => endedOf2(r).status)),
2225
+ ended: quiet(done.then(endedOf2)),
2131
2226
  get onData() {
2132
2227
  return listener;
2133
2228
  },
@@ -2155,16 +2250,11 @@ async function createKernel(options = {}) {
2155
2250
  { op: "run", argv, options: { ...rest, ...input ? { stdin: input } : {} } },
2156
2251
  { output: (fd, bytes) => streams[fd](bytes) }
2157
2252
  );
2158
- const decoder2 = new TextDecoder();
2159
- return {
2160
- status: result.status,
2161
- stdout: decoder2.decode(result.stdout),
2162
- stderr: decoder2.decode(result.stderr)
2163
- };
2253
+ return decodedRun(result);
2164
2254
  },
2165
2255
  openTerminal,
2166
2256
  connect: async (connectOptions = {}) => await call({ op: "connect", ...connectOptions }),
2167
- ...accounts(call, options.users),
2257
+ ...accounts(call, options.users === void 0 ? DEFAULT_USERS : options.users),
2168
2258
  setRoutes: async (routes) => void await call({ op: "routes", routes }),
2169
2259
  dial,
2170
2260
  loopbackFetch: (input, fetchOptions) => loopbackFetch(dial, input, fetchOptions),