@foldrun/core 15.1.0 → 16.0.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.
Files changed (45) hide show
  1. package/dist/src/a2a-publish.d.ts +7 -0
  2. package/dist/src/a2a-publish.js +15 -1
  3. package/dist/src/a2a-reach.d.ts +156 -0
  4. package/dist/src/a2a-reach.js +446 -0
  5. package/dist/src/browse-egress.d.ts +1 -1
  6. package/dist/src/browse-egress.js +7 -6
  7. package/dist/src/env-names.d.ts +12 -0
  8. package/dist/src/env-names.js +49 -0
  9. package/dist/src/failure.d.ts +92 -0
  10. package/dist/src/failure.js +294 -0
  11. package/dist/src/frontmatter-fields.js +1 -1
  12. package/dist/src/install-helper.js +10 -5
  13. package/dist/src/network.js +1 -1
  14. package/dist/src/ops-log.js +4 -0
  15. package/dist/src/otel.d.ts +16 -1
  16. package/dist/src/otel.js +121 -5
  17. package/dist/src/run-container.d.ts +55 -3
  18. package/dist/src/run-container.js +109 -17
  19. package/dist/src/run-event.d.ts +179 -0
  20. package/dist/src/run-event.js +473 -0
  21. package/dist/src/runner.js +8 -1
  22. package/dist/src/runtime.d.ts +113 -16
  23. package/dist/src/runtime.js +528 -44
  24. package/dist/src/scan.js +80 -18
  25. package/dist/src/starter.js +1 -1
  26. package/dist/src/store.d.ts +25 -3
  27. package/dist/src/store.js +40 -10
  28. package/package.json +2 -1
  29. package/src/a2a-publish.ts +19 -1
  30. package/src/a2a-reach.ts +503 -0
  31. package/src/browse-egress.ts +8 -7
  32. package/src/env-names.ts +53 -0
  33. package/src/failure.ts +368 -0
  34. package/src/frontmatter-fields.ts +1 -1
  35. package/src/install-helper.ts +10 -5
  36. package/src/network.ts +1 -1
  37. package/src/ops-log.ts +4 -0
  38. package/src/otel.ts +122 -7
  39. package/src/run-container.ts +112 -21
  40. package/src/run-event.ts +563 -0
  41. package/src/runner.ts +8 -1
  42. package/src/runtime.ts +543 -45
  43. package/src/scan.ts +91 -20
  44. package/src/starter.ts +1 -1
  45. package/src/store.ts +59 -11
@@ -33,8 +33,8 @@ import { divertedSummary, isDivertedPath, overlayTestWrites } from "./test-mode.
33
33
  import { refusedBack, refusedLine, keptLine, type RefusedPath } from "./copy-back.ts";
34
34
  import type { CallIo, EventExtra, ExecUsage } from "./step-exec.ts";
35
35
  import type { TokenPrice } from "./catalog.ts";
36
- import { isFileValue, fileContent } from "./secrets.ts";
37
- import { safeTenantSegment, systemInstallSh, SYSTEM_FROM_INPUT_SH, INSTALL_SOCKET, type RuntimeSpec } from "./runtime.ts";
36
+ import { isFileValue, fileContent, installKeyMaterial } from "./secrets.ts";
37
+ import { safeTenantSegment, systemInstallSh, SYSTEM_FROM_INPUT_SH, INSTALL_SOCKET, ROOT_ENV_RESET_SH, ROOT_SHELL_GUARD_SH, ROOT_ENV_FILE, STEP_ENV_FILE, stepEnvShell, rootEnvShell, SNAPSHOT_KEY_FILE, type RuntimeSpec } from "./runtime.ts";
38
38
  import { egressCaSh, type NetworkLevel } from "./network.ts";
39
39
  import { anyMemoryMi, quantityMi, type ResourceOverride } from "./resources.ts";
40
40
  import type { ScriptSpec } from "./script-tools.ts";
@@ -152,6 +152,10 @@ export interface StepTiming {
152
152
  * from an executor that marks none, and from a resumed driver (the
153
153
  * first driver's phases are on the record already). */
154
154
  phases?: StartupPhases;
155
+ /** When the phases count from: the epoch ms the executor began the step
156
+ * (FOL-122) — what places them on the clock (StepRecord.startupAt). Set
157
+ * beside `phases` only. */
158
+ origin?: number;
155
159
  }
156
160
 
157
161
  /** The start-up phases and what each marks — startup.ts, where every
@@ -609,10 +613,88 @@ export const RUNTIME_CACHE = "/home/agent/.foldrun/runner/.runtimes";
609
613
  /** Starts the install helper as root, in the background, before the drop to
610
614
  * the agent user: the root half of the `install` tool (install-helper.ts).
611
615
  * The agent reaches it only through the socket; it installs only validated
612
- * Debian package names, and dies with the container. */
616
+ * Debian package names, and dies with the container.
617
+ * @deprecated the start-ups use rootStartSh, which starts the helper in a
618
+ * clean environment with the snapshot key's path (FOL-129). */
613
619
  export const INSTALL_HELPER_SH = `FOLDRUN_INSTALL_SOCKET=${INSTALL_SOCKET}; export FOLDRUN_INSTALL_SOCKET
614
620
  node /opt/runner/node_modules/@foldrun/core/dist/src/install-helper.js "$FOLDRUN_INSTALL_SOCKET" ${RUNTIME_CACHE} >/dev/null 2>&1 &`;
615
621
 
622
+ /**
623
+ * The account's key for sealing its `runtime: system:` snapshots (FOL-129):
624
+ * derived from the install key, so it needs no store and no rotation of its
625
+ * own, and per account, so one account's key seals nothing for another. The
626
+ * worker stages it as the step's SNAPSHOT_KEY_FILE; null (no tenant, or a
627
+ * process without the install key) means the step gets no snapshots.
628
+ */
629
+ export function aptSnapshotKey(tenant: string | undefined | null): string | null {
630
+ if (!tenant) return null;
631
+ try {
632
+ return crypto.createHmac("sha256", installKeyMaterial()).update(`foldrun apt snapshot v1\0${tenant}`).digest("hex");
633
+ } catch {
634
+ return null;
635
+ }
636
+ }
637
+
638
+ /**
639
+ * Root's part of a run container's start-up, before the chown and the drop
640
+ * to the agent user (k8s SHIM, docker ENTRY): the snapshot key moved where
641
+ * only root can read it, then — in a subshell whose environment is the root
642
+ * shell's own plus the proxy's variables (ROOT_ENV_FILE), reset to those
643
+ * alone (ROOT_ENV_RESET_SH) — the declared system packages and the install
644
+ * helper. The key never enters an environment: root's commands and the
645
+ * helper get only its path, and the copy in the job folder (which is chowned
646
+ * to the agent next) is gone before the subshell ends, or the start-up stops.
647
+ *
648
+ * The step's own environment (STEP_ENV_FILE) is never read here (FOL-130):
649
+ * its names are the account's to choose, and an LD_PRELOAD or PATH there
650
+ * pointing into the agent-writable cache would run the agent's code as root.
651
+ * ROOT_ENV_FILE is the worker's cut of it to ROOT_ENV_NAMES, sourced before
652
+ * the reset so even a name that should not be there is unset again; the
653
+ * socket and the locale are set after it, so nothing read can move them.
654
+ */
655
+ export function rootStartSh(cacheDir: string = RUNTIME_CACHE): string {
656
+ return `FOLDRUN_INSTALL_SOCKET=${INSTALL_SOCKET}; export FOLDRUN_INSTALL_SOCKET
657
+ (
658
+ if [ -f ${ROOT_ENV_FILE} ] && [ ! -L ${ROOT_ENV_FILE} ]; then . ${ROOT_ENV_FILE}; fi
659
+ ${ROOT_ENV_RESET_SH}
660
+ FOLDRUN_INSTALL_SOCKET=${INSTALL_SOCKET} LANG=C.UTF-8 LC_ALL=C.UTF-8; export FOLDRUN_INSTALL_SOCKET LANG LC_ALL
661
+ unset FOLDRUN_SNAP_KEY_FILE; FOLDRUN_SNAP_KEY_FILE=""
662
+ if [ -e ${SNAPSHOT_KEY_FILE} ] || [ -L ${SNAPSHOT_KEY_FILE} ]; then
663
+ _fk=$(mktemp -d /tmp/.foldrun-key.XXXXXX 2>/dev/null) && [ ! -L ${SNAPSHOT_KEY_FILE} ] && cat ${SNAPSHOT_KEY_FILE} > "$_fk/key" 2>/dev/null && FOLDRUN_SNAP_KEY_FILE=$_fk/key
664
+ rm -f ${SNAPSHOT_KEY_FILE}
665
+ fi
666
+ ${SYSTEM_FROM_INPUT_SH}
667
+ ${systemInstallSh(cacheDir)}
668
+ FOLDRUN_SNAP_KEY_FILE="$FOLDRUN_SNAP_KEY_FILE" node /opt/runner/node_modules/@foldrun/core/dist/src/install-helper.js "$FOLDRUN_INSTALL_SOCKET" ${cacheDir} >/dev/null 2>&1 &
669
+ )
670
+ if [ -e ${SNAPSHOT_KEY_FILE} ] || [ -L ${SNAPSHOT_KEY_FILE} ]; then echo "the snapshot key could not be removed from the job folder — not starting the step" >&2; exit 1; fi`;
671
+ }
672
+
673
+ /**
674
+ * The drop to the agent user, and the only place the step's environment is
675
+ * read (FOL-130): runuser starts with the root shell's own environment — the
676
+ * image's and the pod spec's, which ROOT_SHELL_GUARD_SH has already cleaned —
677
+ * and the agent's shell then sources STEP_ENV_FILE, sets up the egress
678
+ * proxy's CA (which root never needs: apt goes through the proxy unopened),
679
+ * and execs `command`, so its exit status is the step's.
680
+ *
681
+ * What the agent ends with is what it had when root sourced the file before
682
+ * the drop: the step's names over the root shell's, the CA's variables over
683
+ * those, runuser's HOME/USER/LOGNAME over all (kept in positional
684
+ * parameters, which no sourced name can reach), and the install socket at
685
+ * its fixed path. No `set -e`: an egress CA that cannot be written leaves
686
+ * the step without it, as before, rather than stopping it.
687
+ */
688
+ export function agentStartSh(command = "node /opt/runner/driver.mjs"): string {
689
+ const inner = `set -- "$HOME" "$USER" "$LOGNAME"
690
+ set -a; if [ -f ${STEP_ENV_FILE} ]; then . ${STEP_ENV_FILE}; fi; set +a
691
+ HOME=$1 USER=$2 LOGNAME=$3; export HOME USER LOGNAME; shift 3
692
+ FOLDRUN_INSTALL_SOCKET=${INSTALL_SOCKET}; export FOLDRUN_INSTALL_SOCKET
693
+ ${egressCaSh("/opt/runner/job")}
694
+ exec ${command}`;
695
+ return `runuser -u agent -- /bin/sh -c '${inner.replaceAll("'", `'\\''`)}'`;
696
+ }
697
+
616
698
  /**
617
699
  * Where the platform's shared runtime layer is mounted, read-only, in a run
618
700
  * pod — and read-write, one entry at a time, in the pod that builds an entry
@@ -1219,19 +1301,19 @@ ${CORE_INSTALL}`;
1219
1301
  // runtime: system: — apt as root, before the drop (runtime.ts systemInstallSh).
1220
1302
  // set -e is off around it: a failed install is reported by the driver, which
1221
1303
  // reads the status file and fails the step with apt's own words.
1222
- const ENTRY = `#!/bin/sh
1304
+ /** The docker run container's entrypoint (entry.sh), exported for its tests. */
1305
+ export const ENTRY = `#!/bin/sh
1223
1306
  set +e
1224
- ${egressCaSh("/opt/runner/job")}
1225
- ${SYSTEM_FROM_INPUT_SH}
1226
- ${systemInstallSh(RUNTIME_CACHE)}
1227
- ${INSTALL_HELPER_SH}
1228
- set -e
1229
- chown -R agent:agent /workspace /library /opt/runner/job
1307
+ ${ROOT_SHELL_GUARD_SH}
1230
1308
  # The runtime cache, when one is mounted. NOT recursive: a warm cache is tens
1231
1309
  # of thousands of files and its contents are already agent-owned from the step
1232
- # that built them — only the mount point itself arrives owned by root.
1310
+ # that built them — only the mount point itself arrives owned by root. Before
1311
+ # the system install: root works in the cache as the agent user (FOL-129).
1233
1312
  chown agent:agent ${RUNTIME_CACHE} 2>/dev/null || true
1234
- exec runuser -u agent -- node /opt/runner/driver.mjs
1313
+ ${rootStartSh(RUNTIME_CACHE)}
1314
+ set -e
1315
+ chown -R agent:agent /workspace /library /opt/runner/job
1316
+ exec ${agentStartSh()}
1235
1317
  `;
1236
1318
 
1237
1319
  function corePackageDir(): string {
@@ -1659,8 +1741,10 @@ export interface WriteDivert {
1659
1741
  /**
1660
1742
  * What crosses into the container as its environment, and how.
1661
1743
  *
1662
- * A docker --env-file is one `KEY=value` per line and cannot carry a
1663
- * newline. @file secrets are multi-line by nature (a PEM key, a cert), so
1744
+ * It crosses as a file the agent's shell sources after the drop (env.sh,
1745
+ * FOL-130); until then it was a docker --env-file, one `KEY=value` per
1746
+ * line, which cannot carry a newline — and what a step sees stays the shape
1747
+ * that file set. @file secrets are multi-line by nature (a PEM key, a cert), so
1664
1748
  * they are staged as files NOW, into the copied-in workspace, and the env
1665
1749
  * carries the container path — the one place a path is stable across the
1666
1750
  * boundary. A plain secret whose value happens to hold a newline gets the
@@ -1868,15 +1952,19 @@ export async function runStepInContainer(args: RunInContainerArgs): Promise<Cont
1868
1952
  const jobIn = path.join(staging, "job");
1869
1953
  fs.mkdirSync(jobIn);
1870
1954
  fs.writeFileSync(path.join(jobIn, "input.json"), JSON.stringify(args.input, null, 2));
1955
+ // The account's snapshot key, for root's start-up alone (rootStartSh
1956
+ // moves it out of the agent's reach before the drop) — FOL-129.
1957
+ const snapKey = aptSnapshotKey(args.tenant);
1958
+ if (snapKey) fs.writeFileSync(path.join(jobIn, "snapshot.key"), snapKey, { mode: 0o600 });
1871
1959
 
1872
1960
  const containerEnv = stageContainerEnv(args.env, wsIn, args.input.agentRel, args.emit);
1873
1961
 
1874
- const envFile = path.join(staging, "env");
1875
- fs.writeFileSync(
1876
- envFile,
1877
- Object.entries(containerEnv).map(([k, v]) => `${k}=${v}`).join("\n"),
1878
- { mode: 0o600 },
1879
- );
1962
+ // Files in the job folder, not the container's environment (FOL-130): a
1963
+ // docker --env-file is PID 1's environment, so a secret named LD_PRELOAD
1964
+ // reached the root shell before its first line. The agent's shell sources
1965
+ // env.sh after the drop; root's start-up reads only the proxy's lines.
1966
+ fs.writeFileSync(path.join(jobIn, "env.sh"), stepEnvShell(containerEnv), { mode: 0o600 });
1967
+ fs.writeFileSync(path.join(jobIn, "root-env.sh"), rootEnvShell(containerEnv), { mode: 0o600 });
1880
1968
 
1881
1969
  const lim = args.limits ?? resolveResources(args.size);
1882
1970
  const cpus = dockerCpus(lim.cpus);
@@ -1894,7 +1982,6 @@ export async function runStepInContainer(args: RunInContainerArgs): Promise<Cont
1894
1982
  // the whole machine from the steps beside it.
1895
1983
  "--memory", dockerMemory(lim.memory),
1896
1984
  ...(cpus ? ["--cpus", cpus] : []),
1897
- "--env-file", envFile,
1898
1985
  ];
1899
1986
  // gVisor (or kata) where the host has it: one env var, because the flag
1900
1987
  // is the same docker invocation everywhere else.
@@ -1947,6 +2034,9 @@ export async function runStepInContainer(args: RunInContainerArgs): Promise<Cont
1947
2034
  cp(`${wsIn}/.`, `${containerId}:/workspace`);
1948
2035
  cp(`${libIn}/.`, `${containerId}:/library`);
1949
2036
  cp(`${jobIn}/input.json`, `${containerId}:/opt/runner/job/input.json`);
2037
+ cp(`${jobIn}/env.sh`, `${containerId}:${STEP_ENV_FILE}`);
2038
+ cp(`${jobIn}/root-env.sh`, `${containerId}:${ROOT_ENV_FILE}`);
2039
+ if (snapKey) cp(`${jobIn}/snapshot.key`, `${containerId}:${SNAPSHOT_KEY_FILE}`);
1950
2040
  phases.files = Date.now() - t0;
1951
2041
 
1952
2042
  // Start attached and read the driver's JSONL. The step timeout gets a
@@ -2032,6 +2122,7 @@ export async function runStepInContainer(args: RunInContainerArgs): Promise<Cont
2032
2122
  firstOutputMs: firstOutputAt === null ? null : firstOutputAt - t0 - sandboxMs,
2033
2123
  totalMs: Date.now() - t0,
2034
2124
  phases,
2125
+ origin: t0,
2035
2126
  };
2036
2127
  args.emit("info", timingLine(timing));
2037
2128
  return { ...outcome, timing, applied, ...(refused.length ? { refused } : {}) };