@ship.zone/runner 2.1.0 → 4.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 (200) hide show
  1. package/.smartconfig.json +118 -3
  2. package/changelog.md +101 -0
  3. package/dist_rust/runnerworkspace_linux_amd64_musl +0 -0
  4. package/dist_rust/runnerworkspace_linux_amd64_musl.third-party-notices.json +679 -0
  5. package/dist_rust/runnerworkspace_linux_amd64_musl.third-party-notices.txt +15719 -0
  6. package/dist_rust/runnerworkspace_linux_amd64_musl.tsrust-build.json +5 -5
  7. package/dist_rust/runnerworkspace_linux_arm64_musl +0 -0
  8. package/dist_rust/runnerworkspace_linux_arm64_musl.third-party-notices.json +679 -0
  9. package/dist_rust/runnerworkspace_linux_arm64_musl.third-party-notices.txt +15719 -0
  10. package/dist_rust/runnerworkspace_linux_arm64_musl.tsrust-build.json +5 -5
  11. package/dist_ts/00_commitinfo_data.d.ts +8 -0
  12. package/dist_ts/00_commitinfo_data.js +9 -0
  13. package/dist_ts/api-client.d.ts +47 -0
  14. package/dist_ts/api-client.js +600 -0
  15. package/dist_ts/artifact-manager.d.ts +11 -0
  16. package/dist_ts/artifact-manager.js +140 -0
  17. package/dist_ts/attempt-state.d.ts +18 -0
  18. package/dist_ts/attempt-state.js +151 -0
  19. package/dist_ts/cache-manager.d.ts +13 -0
  20. package/dist_ts/cache-manager.js +300 -0
  21. package/dist_ts/cli-entry.d.ts +2 -0
  22. package/dist_ts/cli-entry.js +18 -0
  23. package/dist_ts/cli.d.ts +14 -0
  24. package/dist_ts/cli.js +243 -0
  25. package/dist_ts/config.d.ts +8 -0
  26. package/dist_ts/config.js +495 -0
  27. package/dist_ts/egress-proxy.d.ts +99 -0
  28. package/dist_ts/egress-proxy.js +607 -0
  29. package/dist_ts/errors.d.ts +41 -0
  30. package/dist_ts/errors.js +90 -0
  31. package/dist_ts/filesystem.d.ts +73 -0
  32. package/dist_ts/filesystem.js +207 -0
  33. package/dist_ts/index.d.ts +20 -0
  34. package/dist_ts/index.js +21 -0
  35. package/dist_ts/interfaces.d.ts +685 -0
  36. package/dist_ts/interfaces.js +3 -0
  37. package/dist_ts/log-sender.d.ts +26 -0
  38. package/dist_ts/log-sender.js +124 -0
  39. package/dist_ts/microvm-executor.d.ts +90 -0
  40. package/dist_ts/microvm-executor.js +1045 -0
  41. package/dist_ts/node-version.d.ts +11 -0
  42. package/dist_ts/node-version.js +43 -0
  43. package/dist_ts/npm-grants.d.ts +9 -0
  44. package/dist_ts/npm-grants.js +44 -0
  45. package/dist_ts/oci-registry.d.ts +73 -0
  46. package/dist_ts/oci-registry.js +508 -0
  47. package/dist_ts/plugins.d.ts +23 -0
  48. package/dist_ts/plugins.js +27 -0
  49. package/dist_ts/protocol-validator.d.ts +39 -0
  50. package/dist_ts/protocol-validator.js +488 -0
  51. package/dist_ts/redactor.d.ts +78 -0
  52. package/dist_ts/redactor.js +235 -0
  53. package/dist_ts/registration-state.d.ts +16 -0
  54. package/dist_ts/registration-state.js +171 -0
  55. package/dist_ts/runner.d.ts +65 -0
  56. package/dist_ts/runner.js +912 -0
  57. package/dist_ts/source-workspace.d.ts +10 -0
  58. package/dist_ts/source-workspace.js +125 -0
  59. package/dist_ts/spec-version.d.ts +11 -0
  60. package/dist_ts/spec-version.js +41 -0
  61. package/dist_ts/staging-scavenger.d.ts +7 -0
  62. package/dist_ts/staging-scavenger.js +63 -0
  63. package/dist_ts/state-owner.d.ts +74 -0
  64. package/dist_ts/state-owner.js +291 -0
  65. package/dist_ts/transfer-archive.d.ts +78 -0
  66. package/dist_ts/transfer-archive.js +1619 -0
  67. package/dist_ts/validation.d.ts +11 -0
  68. package/dist_ts/validation.js +79 -0
  69. package/dist_ts/vsock-protocol.d.ts +130 -0
  70. package/dist_ts/vsock-protocol.js +336 -0
  71. package/dist_ts/wire-schema.d.ts +10 -0
  72. package/dist_ts/wire-schema.js +114 -0
  73. package/native-notices/crates/bitflags-2.13.1/license-apache +201 -0
  74. package/native-notices/crates/bitflags-2.13.1/license-mit +25 -0
  75. package/native-notices/crates/cc-1.8.0/license-apache +201 -0
  76. package/native-notices/crates/cc-1.8.0/license-mit +25 -0
  77. package/native-notices/crates/cfg-if-1.0.4/license-apache +201 -0
  78. package/native-notices/crates/cfg-if-1.0.4/license-mit +25 -0
  79. package/native-notices/crates/cfg_aliases-0.2.2/license +9 -0
  80. package/native-notices/crates/cfg_aliases-0.2.2/notices.md +26 -0
  81. package/native-notices/crates/find-msvc-tools-0.1.14/license-apache +201 -0
  82. package/native-notices/crates/find-msvc-tools-0.1.14/license-mit +25 -0
  83. package/native-notices/crates/itoa-1.0.18/license-apache +176 -0
  84. package/native-notices/crates/itoa-1.0.18/license-mit +23 -0
  85. package/native-notices/crates/libc-0.2.189/license-apache +176 -0
  86. package/native-notices/crates/libc-0.2.189/license-mit +25 -0
  87. package/native-notices/crates/memchr-2.8.3/copying +3 -0
  88. package/native-notices/crates/memchr-2.8.3/license-mit +21 -0
  89. package/native-notices/crates/memchr-2.8.3/unlicense +24 -0
  90. package/native-notices/crates/nix-0.30.1/license +21 -0
  91. package/native-notices/crates/proc-macro2-1.0.107/license-apache +176 -0
  92. package/native-notices/crates/proc-macro2-1.0.107/license-mit +23 -0
  93. package/native-notices/crates/quote-1.0.47/license-apache +176 -0
  94. package/native-notices/crates/quote-1.0.47/license-mit +23 -0
  95. package/native-notices/crates/serde-1.0.229/license-apache +176 -0
  96. package/native-notices/crates/serde-1.0.229/license-mit +23 -0
  97. package/native-notices/crates/serde_core-1.0.229/license-apache +176 -0
  98. package/native-notices/crates/serde_core-1.0.229/license-mit +23 -0
  99. package/native-notices/crates/serde_derive-1.0.229/license-apache +176 -0
  100. package/native-notices/crates/serde_derive-1.0.229/license-mit +23 -0
  101. package/native-notices/crates/serde_json-1.0.151/license-apache +176 -0
  102. package/native-notices/crates/serde_json-1.0.151/license-mit +23 -0
  103. package/native-notices/crates/shlex-2.0.1/license-apache +13 -0
  104. package/native-notices/crates/shlex-2.0.1/license-mit +21 -0
  105. package/native-notices/crates/syn-3.0.3/license-apache +176 -0
  106. package/native-notices/crates/syn-3.0.3/license-mit +23 -0
  107. package/native-notices/crates/tikv-jemalloc-sys-0.7.1+5.3.1-0-g81034ce1f1373e37dc865038e1bc8eeecf559ce8/license-apache +201 -0
  108. package/native-notices/crates/tikv-jemalloc-sys-0.7.1+5.3.1-0-g81034ce1f1373e37dc865038e1bc8eeecf559ce8/license-mit +25 -0
  109. package/native-notices/crates/tikv-jemallocator-0.7.0/license-apache +201 -0
  110. package/native-notices/crates/tikv-jemallocator-0.7.0/license-mit +25 -0
  111. package/native-notices/crates/unicode-ident-1.0.24/license-apache +176 -0
  112. package/native-notices/crates/unicode-ident-1.0.24/license-mit +23 -0
  113. package/native-notices/crates/unicode-ident-1.0.24/license-unicode +39 -0
  114. package/native-notices/crates/zmij-1.0.23/license-mit +23 -0
  115. package/native-notices/manifest.json +763 -0
  116. package/native-notices/native/tikv-jemalloc-sys-0.7.1+5.3.1-0-g81034ce1f1373e37dc865038e1bc8eeecf559ce8/jemalloc-5.3.1/jemalloc-copying +27 -0
  117. package/native-notices/readme.md +46 -0
  118. package/native-notices/runtime/gcc/gpl-3.0.txt +674 -0
  119. package/native-notices/runtime/gcc/runtime-library-exception-3.1.txt +73 -0
  120. package/native-notices/runtime/libunwind-22.1.2/license.txt +311 -0
  121. package/native-notices/runtime/musl-1.2.5/copyright +193 -0
  122. package/native-notices/rust/compiler-builtins-license.txt +275 -0
  123. package/native-notices/rust/copyright-library.html +8266 -0
  124. package/native-notices/rust/crates/addr2line-0.25.1/license-apache +201 -0
  125. package/native-notices/rust/crates/addr2line-0.25.1/license-mit +25 -0
  126. package/native-notices/rust/crates/adler2-2.0.1/license-0bsd +12 -0
  127. package/native-notices/rust/crates/adler2-2.0.1/license-apache +201 -0
  128. package/native-notices/rust/crates/adler2-2.0.1/license-mit +23 -0
  129. package/native-notices/rust/crates/memchr-2.7.6/copying +3 -0
  130. package/native-notices/rust/crates/memchr-2.7.6/license-mit +21 -0
  131. package/native-notices/rust/crates/memchr-2.7.6/unlicense +24 -0
  132. package/native-notices/rust/crates/miniz_oxide-0.8.9/license +25 -0
  133. package/native-notices/rust/crates/miniz_oxide-0.8.9/license-apache.md +177 -0
  134. package/native-notices/rust/crates/miniz_oxide-0.8.9/license-mit.md +24 -0
  135. package/native-notices/rust/crates/miniz_oxide-0.8.9/license-zlib.md +14 -0
  136. package/native-notices/rust/crates/object-0.37.3/license-apache +201 -0
  137. package/native-notices/rust/crates/object-0.37.3/license-mit +25 -0
  138. package/native-notices/rust/licenses/apache-2.0.txt +73 -0
  139. package/native-notices/rust/licenses/bsd-2-clause.txt +9 -0
  140. package/native-notices/rust/licenses/cc-by-sa-4.0.txt +427 -0
  141. package/native-notices/rust/licenses/gcc-exception-3.1.txt +30 -0
  142. package/native-notices/rust/licenses/gpl-2.0-only.txt +133 -0
  143. package/native-notices/rust/licenses/gpl-3.0-or-later.txt +202 -0
  144. package/native-notices/rust/licenses/isc.txt +7 -0
  145. package/native-notices/rust/licenses/llvm-exception.txt +15 -0
  146. package/native-notices/rust/licenses/mit.txt +9 -0
  147. package/native-notices/rust/licenses/ncsa.txt +29 -0
  148. package/native-notices/rust/licenses/ofl-1.1.txt +43 -0
  149. package/native-notices/rust/licenses/unicode-3.0.txt +39 -0
  150. package/package.json +31 -26
  151. package/readme.md +157 -95
  152. package/rust/.cargo/config.toml +1 -1
  153. package/rust/Cargo.lock +1 -0
  154. package/rust/Cargo.toml +3 -2
  155. package/rust/src/cpio.rs +147 -0
  156. package/rust/src/extfs.rs +747 -0
  157. package/rust/src/guest.rs +2411 -0
  158. package/rust/src/lib.rs +197 -1
  159. package/rust/src/main.rs +7 -2
  160. package/rust/src/oci_tar.rs +842 -0
  161. package/rust-toolchain.toml +4 -0
  162. package/ts/00_commitinfo_data.ts +2 -2
  163. package/ts/api-client.ts +29 -8
  164. package/ts/artifact-manager.ts +9 -7
  165. package/ts/attempt-state.ts +75 -84
  166. package/ts/cache-manager.ts +22 -28
  167. package/ts/cli-entry.ts +17 -0
  168. package/ts/cli.ts +86 -53
  169. package/ts/config.ts +146 -62
  170. package/ts/egress-proxy.ts +649 -0
  171. package/ts/errors.ts +19 -0
  172. package/ts/filesystem.ts +316 -0
  173. package/ts/index.ts +20 -15
  174. package/ts/interfaces.ts +105 -23
  175. package/ts/log-sender.ts +4 -4
  176. package/ts/microvm-executor.ts +1265 -0
  177. package/ts/node-version.ts +49 -0
  178. package/ts/npm-grants.ts +49 -0
  179. package/ts/oci-registry.ts +649 -0
  180. package/ts/plugins.ts +25 -3
  181. package/ts/protocol-validator.ts +34 -30
  182. package/ts/redactor.ts +2 -2
  183. package/ts/registration-state.ts +82 -90
  184. package/ts/runner.ts +81 -34
  185. package/ts/source-workspace.ts +11 -8
  186. package/ts/spec-version.ts +2 -2
  187. package/ts/staging-scavenger.ts +37 -26
  188. package/ts/state-owner.ts +353 -0
  189. package/ts/transfer-archive.ts +214 -243
  190. package/ts/validation.ts +1 -1
  191. package/ts/vsock-protocol.ts +437 -0
  192. package/ts/wire-schema.ts +3 -3
  193. package/deno.json +0 -56
  194. package/deno.lock +0 -1495
  195. package/dist_npm/mod.d.ts +0 -967
  196. package/dist_npm/mod.js +0 -10058
  197. package/mod.ts +0 -8
  198. package/musl-copyright.md +0 -81
  199. package/third-party-licenses.md +0 -187
  200. package/ts/oci-executor.ts +0 -3137
@@ -0,0 +1,4 @@
1
+ [toolchain]
2
+ channel = "1.95.0"
3
+ components = ["rust-src"]
4
+ targets = ["x86_64-unknown-linux-musl", "aarch64-unknown-linux-musl"]
@@ -3,6 +3,6 @@
3
3
  */
4
4
  export const commitinfo = {
5
5
  name: '@ship.zone/runner',
6
- version: '2.1.0',
7
- description: 'The isolated OCI job runner for ship.zone CI/CD'
6
+ version: '4.0.0',
7
+ description: 'The Firecracker microVM OCI job runner for ship.zone CI/CD'
8
8
  }
package/ts/api-client.ts CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  LeaseLostError,
5
5
  ProtocolError,
6
6
  ResourceCleanupError,
7
- } from './errors.ts';
7
+ } from './errors.js';
8
8
  import type {
9
9
  IAbandonJobRequest,
10
10
  IAcceptJobRequest,
@@ -15,12 +15,14 @@ import type {
15
15
  ICompleteJobResponse,
16
16
  IDiscoveryResponse,
17
17
  IErrorResponse,
18
+ IImageRecord,
18
19
  IJobHeartbeatRequest,
19
20
  IJobHeartbeatResponse,
20
21
  IJobIdentity,
21
22
  IJobSource,
22
23
  ILogAcknowledgement,
23
24
  ILogChunk,
25
+ IRecordImageRequest,
24
26
  IRecoverAttemptRequest,
25
27
  IRegisterRunnerRequest,
26
28
  IRegisterRunnerResponse,
@@ -33,14 +35,14 @@ import type {
33
35
  ITransferRecord,
34
36
  TCacheDownload,
35
37
  TOpenTransferBody,
36
- } from './interfaces.ts';
37
- import { runnerProtocolBasePath } from './plugins.ts';
38
- import { runnerSpecVersion, specMajorVersionsMatch } from './spec-version.ts';
39
- import { isProtocolTimestamp, isRecord, validateHttpUrl } from './validation.ts';
40
- import { isValidWireMessage, type TWireSchemaName, validateWireMessage } from './wire-schema.ts';
38
+ } from './interfaces.js';
39
+ import { runnerProtocolBasePath } from './plugins.js';
40
+ import { runnerSpecVersion, specMajorVersionsMatch } from './spec-version.js';
41
+ import { isProtocolTimestamp, isRecord, validateHttpUrl } from './validation.js';
42
+ import { isValidWireMessage, type TWireSchemaName, validateWireMessage } from './wire-schema.js';
41
43
 
42
44
  export type TRunnerFetch = (
43
- input: RequestInfo | URL,
45
+ input: string | URL | Request,
44
46
  init?: RequestInit,
45
47
  ) => Promise<Response>;
46
48
 
@@ -59,6 +61,7 @@ export interface IRunnerApiClientOptions {
59
61
 
60
62
  interface IJsonRequestOptions {
61
63
  requestSchema: TWireSchemaName;
64
+ method?: 'POST' | 'PUT';
62
65
  token?: string;
63
66
  signal?: AbortSignal;
64
67
  fenced?: boolean;
@@ -437,6 +440,21 @@ export class RunnerApiClient {
437
440
  );
438
441
  }
439
442
 
443
+ public async recordImage(
444
+ request: IRecordImageRequest,
445
+ signal?: AbortSignal,
446
+ ): Promise<IImageRecord> {
447
+ return validateWireMessage<IImageRecord>(
448
+ 'ImageRecord',
449
+ await this.requestJson(this.jobPath(request.identity, 'image'), request, {
450
+ method: 'PUT',
451
+ requestSchema: 'RecordImageRequest',
452
+ signal,
453
+ fenced: true,
454
+ }),
455
+ );
456
+ }
457
+
440
458
  public async completeJob(
441
459
  request: ICompleteJobRequest,
442
460
  signal?: AbortSignal,
@@ -632,7 +650,7 @@ export class RunnerApiClient {
632
650
  let response: Response;
633
651
  try {
634
652
  response = await this.fetchImplementation(new URL(path, this.baseUrl), {
635
- method: 'POST',
653
+ method: options.method ?? 'POST',
636
654
  headers: {
637
655
  authorization: `Bearer ${token}`,
638
656
  'content-type': 'application/json',
@@ -741,6 +759,9 @@ export class RunnerApiClient {
741
759
  method: 'PUT',
742
760
  headers,
743
761
  body,
762
+ // A streamed request body must be sent half-duplex: the request body is written before
763
+ // the response is read.
764
+ duplex: 'half',
744
765
  redirect: 'error',
745
766
  signal: requestSignal,
746
767
  });
@@ -1,11 +1,11 @@
1
- import { join } from '@std/path';
1
+ import * as plugins from './plugins.js';
2
2
  import {
3
3
  AuthenticationError,
4
4
  LeaseLostError,
5
5
  ProtocolError,
6
6
  ResourceCleanupError,
7
7
  SecretDisclosureError,
8
- } from './errors.ts';
8
+ } from './errors.js';
9
9
  import type {
10
10
  IArtifactDeclaration,
11
11
  IArtifactManager,
@@ -15,13 +15,15 @@ import type {
15
15
  ITransferMetadata,
16
16
  ITransferRecord,
17
17
  TOpenTransferBody,
18
- } from './interfaces.ts';
18
+ } from './interfaces.js';
19
19
  import {
20
20
  createTarGzipArchive,
21
21
  ensureStagingCapacity,
22
22
  validateRelativeSelections,
23
23
  validateTransferRecord,
24
- } from './transfer-archive.ts';
24
+ } from './transfer-archive.js';
25
+
26
+ const { join } = plugins.path;
25
27
 
26
28
  export interface IArtifactApiClient {
27
29
  uploadArtifact(
@@ -138,7 +140,7 @@ export class ArtifactManager implements IArtifactManager {
138
140
  let archive: Awaited<ReturnType<typeof createTarGzipArchive>> | undefined;
139
141
  let operationError: unknown;
140
142
  try {
141
- await Deno.mkdir(this.config.stateDirectory, { recursive: true, mode: 0o700 });
143
+ await plugins.fs.mkdir(this.config.stateDirectory, { recursive: true, mode: 0o700 });
142
144
  await ensureStagingCapacity(
143
145
  this.config.stateDirectory,
144
146
  artifact.maximumExtractedBytes,
@@ -153,7 +155,7 @@ export class ArtifactManager implements IArtifactManager {
153
155
  signal: context.signal,
154
156
  });
155
157
  const archiveRoot = join(this.config.stateDirectory, 'artifact-uploads');
156
- await Deno.mkdir(archiveRoot, { recursive: true, mode: 0o700 });
158
+ await plugins.fs.mkdir(archiveRoot, { recursive: true, mode: 0o700 });
157
159
  await ensureStagingCapacity(
158
160
  archiveRoot,
159
161
  artifact.maximumBytes,
@@ -174,7 +176,7 @@ export class ArtifactManager implements IArtifactManager {
174
176
  redactionSet: context.redactionSet,
175
177
  signal: context.signal,
176
178
  });
177
- const transferRequestId = crypto.randomUUID();
179
+ const transferRequestId = plugins.crypto.randomUUID();
178
180
  const record = await this.api.uploadArtifact(
179
181
  context.identity,
180
182
  artifact.name,
@@ -1,5 +1,18 @@
1
- import { join } from '@std/path';
2
- import { RunnerError } from './errors.ts';
1
+ import * as plugins from './plugins.js';
2
+ import { RunnerError } from './errors.js';
3
+ import {
4
+ createNewWriteFlags,
5
+ isNotFound,
6
+ readInto,
7
+ removePath,
8
+ sameFileIdentity,
9
+ syncDirectory,
10
+ withOpenFile,
11
+ writeAll,
12
+ } from './filesystem.js';
13
+ import { ensurePrivateStateDirectory, type RunnerStateOwner } from './state-owner.js';
14
+
15
+ const { join } = plugins.path;
3
16
 
4
17
  export interface IAttemptJournal {
5
18
  previousSessionId: string;
@@ -38,29 +51,20 @@ const validateJournal = (value: unknown): IAttemptJournal => {
38
51
  };
39
52
  };
40
53
 
41
- const writeAll = async (file: Deno.FsFile, bytes: Uint8Array): Promise<void> => {
42
- let offset = 0;
43
- while (offset < bytes.byteLength) {
44
- const written = await file.write(bytes.subarray(offset));
45
- if (written <= 0) throw new RunnerError('Attempt journal write made no progress');
46
- offset += written;
47
- }
48
- };
49
-
50
- const isNotFound = (error: unknown): boolean => error instanceof Deno.errors.NotFound;
51
-
52
54
  export class AttemptStateStore {
53
55
  readonly path: string;
56
+ private readonly stateDirectory: string;
54
57
 
55
- constructor(private readonly stateDirectory: string) {
56
- this.path = join(stateDirectory, journalName);
58
+ constructor(private readonly owner: RunnerStateOwner) {
59
+ this.stateDirectory = owner.stateDirectory;
60
+ this.path = join(this.stateDirectory, journalName);
57
61
  }
58
62
 
59
63
  async load(): Promise<IAttemptJournal | undefined> {
60
64
  await this.ensureDirectory();
61
- let pathStat: Deno.FileInfo;
65
+ let pathStat: plugins.Stats;
62
66
  try {
63
- pathStat = await Deno.lstat(this.path);
67
+ pathStat = await plugins.fs.lstat(this.path);
64
68
  } catch (error) {
65
69
  if (isNotFound(error)) return undefined;
66
70
  throw error;
@@ -69,73 +73,71 @@ export class AttemptStateStore {
69
73
  if (pathStat.size > maximumJournalBytes) {
70
74
  throw new RunnerError('Attempt journal exceeds its size limit');
71
75
  }
72
- const file = await Deno.open(this.path, { read: true });
73
- try {
74
- const openStat = await file.stat();
75
- if (
76
- !openStat.isFile ||
77
- (pathStat.dev !== null && openStat.dev !== pathStat.dev) ||
78
- (pathStat.ino !== null && openStat.ino !== pathStat.ino)
79
- ) {
80
- throw new RunnerError('Attempt journal changed while it was opened');
81
- }
82
- const bytes = new Uint8Array(pathStat.size + 1);
83
- let offset = 0;
84
- while (offset < bytes.byteLength) {
85
- const read = await file.read(bytes.subarray(offset));
86
- if (read === null) break;
87
- offset += read;
88
- }
89
- if (offset !== pathStat.size) {
90
- throw new RunnerError('Attempt journal changed while it was read');
91
- }
92
- let parsed: unknown;
93
- try {
94
- parsed = JSON.parse(new TextDecoder().decode(bytes.subarray(0, offset)));
95
- } catch (error) {
96
- throw new RunnerError('Attempt journal is not valid JSON', { cause: error });
97
- }
98
- return validateJournal(parsed);
99
- } finally {
100
- file.close();
101
- }
76
+ return await withOpenFile(
77
+ this.path,
78
+ 'r',
79
+ undefined,
80
+ async (file) => {
81
+ const openStat = await file.stat();
82
+ if (!openStat.isFile() || !sameFileIdentity(pathStat, openStat)) {
83
+ throw new RunnerError('Attempt journal changed while it was opened');
84
+ }
85
+ const bytes = new Uint8Array(pathStat.size + 1);
86
+ let offset = 0;
87
+ while (offset < bytes.byteLength) {
88
+ const read = await readInto(file, bytes.subarray(offset));
89
+ if (read === null) break;
90
+ offset += read;
91
+ }
92
+ if (offset !== pathStat.size) {
93
+ throw new RunnerError('Attempt journal changed while it was read');
94
+ }
95
+ let parsed: unknown;
96
+ try {
97
+ parsed = JSON.parse(new TextDecoder().decode(bytes.subarray(0, offset)));
98
+ } catch (error) {
99
+ throw new RunnerError('Attempt journal is not valid JSON', { cause: error });
100
+ }
101
+ return validateJournal(parsed);
102
+ },
103
+ 'Attempt journal read and close failed',
104
+ );
102
105
  }
103
106
 
104
107
  async save(journal: IAttemptJournal): Promise<void> {
105
108
  const validated = validateJournal(journal);
106
109
  await this.ensureDirectory();
107
110
  try {
108
- this.assertSafeJournalFile(await Deno.lstat(this.path));
111
+ this.assertSafeJournalFile(await plugins.fs.lstat(this.path));
109
112
  } catch (error) {
110
113
  if (!isNotFound(error)) throw error;
111
114
  }
112
115
  const temporaryPath = join(
113
116
  this.stateDirectory,
114
- `.${crypto.randomUUID()}.attempt-journal.tmp`,
117
+ `.${plugins.crypto.randomUUID()}.attempt-journal.tmp`,
115
118
  );
116
119
  const bytes = new TextEncoder().encode(`${JSON.stringify(validated)}\n`);
117
120
  if (bytes.byteLength > maximumJournalBytes) {
118
121
  throw new RunnerError('Attempt journal exceeds its size limit');
119
122
  }
120
- let file: Deno.FsFile | undefined;
121
123
  try {
122
- file = await Deno.open(temporaryPath, {
123
- write: true,
124
- createNew: true,
125
- mode: 0o600,
126
- });
127
- await writeAll(file, bytes);
128
- await file.sync();
129
- file.close();
130
- file = undefined;
131
- await Deno.chmod(temporaryPath, 0o600);
132
- await Deno.rename(temporaryPath, this.path);
133
- await Deno.chmod(this.path, 0o600);
124
+ await withOpenFile(
125
+ temporaryPath,
126
+ createNewWriteFlags,
127
+ 0o600,
128
+ async (file) => {
129
+ await writeAll(file, bytes, 'Attempt journal write made no progress');
130
+ await file.sync();
131
+ },
132
+ 'Attempt journal write and close failed',
133
+ );
134
+ await plugins.fs.chmod(temporaryPath, 0o600);
135
+ await plugins.fs.rename(temporaryPath, this.path);
136
+ await plugins.fs.chmod(this.path, 0o600);
134
137
  await this.syncDirectory();
135
138
  } catch (error) {
136
- file?.close();
137
139
  try {
138
- await Deno.remove(temporaryPath);
140
+ await removePath(temporaryPath);
139
141
  } catch (cleanupError) {
140
142
  if (!isNotFound(cleanupError)) {
141
143
  throw new AggregateError([error, cleanupError], 'Attempt journal write cleanup failed');
@@ -147,44 +149,33 @@ export class AttemptStateStore {
147
149
 
148
150
  async clear(): Promise<void> {
149
151
  await this.ensureDirectory();
150
- let stat: Deno.FileInfo;
152
+ let stat: plugins.Stats;
151
153
  try {
152
- stat = await Deno.lstat(this.path);
154
+ stat = await plugins.fs.lstat(this.path);
153
155
  } catch (error) {
154
156
  if (isNotFound(error)) return;
155
157
  throw error;
156
158
  }
157
159
  this.assertSafeJournalFile(stat);
158
- await Deno.remove(this.path);
160
+ await removePath(this.path);
159
161
  await this.syncDirectory();
160
162
  }
161
163
 
162
164
  private async ensureDirectory(): Promise<void> {
163
- await Deno.mkdir(this.stateDirectory, { recursive: true, mode: 0o700 });
164
- const stat = await Deno.lstat(this.stateDirectory);
165
- if (!stat.isDirectory || stat.isSymlink) {
166
- throw new RunnerError('Runner state directory must be a real directory');
167
- }
168
- if (Deno.build.os !== 'windows' && stat.mode !== null && (stat.mode & 0o077) !== 0) {
169
- throw new RunnerError('Runner state directory permissions are not private');
170
- }
165
+ this.owner.assertHeld();
166
+ await ensurePrivateStateDirectory(this.stateDirectory);
171
167
  }
172
168
 
173
- private assertSafeJournalFile(stat: Deno.FileInfo): void {
174
- if (!stat.isFile || stat.isSymlink) {
169
+ private assertSafeJournalFile(stat: plugins.Stats): void {
170
+ if (!stat.isFile() || stat.isSymbolicLink()) {
175
171
  throw new RunnerError('Attempt journal must be a regular file');
176
172
  }
177
- if (Deno.build.os !== 'windows' && stat.mode !== null && (stat.mode & 0o777) !== 0o600) {
173
+ if ((stat.mode & 0o777) !== 0o600) {
178
174
  throw new RunnerError('Attempt journal permissions must be 0600');
179
175
  }
180
176
  }
181
177
 
182
178
  private async syncDirectory(): Promise<void> {
183
- const directory = await Deno.open(this.stateDirectory, { read: true });
184
- try {
185
- await directory.sync();
186
- } finally {
187
- directory.close();
188
- }
179
+ await syncDirectory(this.stateDirectory, 'Runner state directory sync and close failed');
189
180
  }
190
181
  }
@@ -1,5 +1,6 @@
1
- import { dirname, join, resolve } from '@std/path';
2
- import { ProtocolError, ResourceCleanupError, RunnerError } from './errors.ts';
1
+ import * as plugins from './plugins.js';
2
+ import { lstatIfPresent, makeTempDir, removePath } from './filesystem.js';
3
+ import { ProtocolError, ResourceCleanupError, RunnerError } from './errors.js';
3
4
  import type {
4
5
  ICacheDeclaration,
5
6
  ICacheManager,
@@ -11,7 +12,7 @@ import type {
11
12
  ITransferRecord,
12
13
  TCacheDownload,
13
14
  TOpenTransferBody,
14
- } from './interfaces.ts';
15
+ } from './interfaces.js';
15
16
  import {
16
17
  createTarGzipArchive,
17
18
  ensureStagingCapacity,
@@ -20,7 +21,9 @@ import {
20
21
  stageTransferDownload,
21
22
  validateRelativeSelections,
22
23
  validateTransferRecord,
23
- } from './transfer-archive.ts';
24
+ } from './transfer-archive.js';
25
+
26
+ const { dirname, join, resolve } = plugins.path;
24
27
 
25
28
  export interface ICacheApiClient {
26
29
  downloadCache(
@@ -143,15 +146,6 @@ const validateCaches = (
143
146
  return limits;
144
147
  };
145
148
 
146
- const lstatIfPresent = async (path: string): Promise<Deno.FileInfo | undefined> => {
147
- try {
148
- return await Deno.lstat(path);
149
- } catch (error) {
150
- if (error instanceof Deno.errors.NotFound) return undefined;
151
- throw error;
152
- }
153
- };
154
-
155
149
  const assertSafeDestinationParents = async (workspacePath: string, path: string): Promise<void> => {
156
150
  let current = resolve(workspacePath);
157
151
  const segments = path.split('/');
@@ -159,7 +153,7 @@ const assertSafeDestinationParents = async (workspacePath: string, path: string)
159
153
  current = join(current, segments[index]);
160
154
  const stat = await lstatIfPresent(current);
161
155
  if (!stat) return;
162
- if (!stat.isDirectory || stat.isSymlink) {
156
+ if (!stat.isDirectory() || stat.isSymbolicLink()) {
163
157
  throw new RunnerError('Cache destination has an unsafe ancestor');
164
158
  }
165
159
  }
@@ -182,14 +176,14 @@ const restoreSelectedPaths = async (
182
176
  await assertSafeDestinationParents(workspacePath, path);
183
177
  const destination = join(resolve(workspacePath), ...path.split('/'));
184
178
  const destinationStat = await lstatIfPresent(destination);
185
- if (destinationStat?.isSymlink) {
179
+ if (destinationStat?.isSymbolicLink()) {
186
180
  throw new RunnerError('Cache destination is a symbolic link');
187
181
  }
188
182
  candidates.push({ source, destination, existed: destinationStat !== undefined });
189
183
  }
190
184
 
191
185
  const workspaceRoot = resolve(workspacePath);
192
- const backupRoot = await Deno.makeTempDir({
186
+ const backupRoot = await makeTempDir({
193
187
  dir: dirname(workspaceRoot),
194
188
  prefix: '.cache-restore-',
195
189
  });
@@ -200,15 +194,15 @@ const restoreSelectedPaths = async (
200
194
  const candidate = candidates[index];
201
195
  if (!candidate.existed) continue;
202
196
  const backup = join(backupRoot, String(index));
203
- await Deno.rename(candidate.destination, backup);
197
+ await plugins.fs.rename(candidate.destination, backup);
204
198
  backedUp.push({ backup, destination: candidate.destination });
205
199
  }
206
200
  for (const candidate of candidates) {
207
- await Deno.mkdir(dirname(candidate.destination), { recursive: true, mode: 0o700 });
208
- await Deno.rename(candidate.source, candidate.destination);
201
+ await plugins.fs.mkdir(dirname(candidate.destination), { recursive: true, mode: 0o700 });
202
+ await plugins.fs.rename(candidate.source, candidate.destination);
209
203
  installed.push(candidate.destination);
210
204
  }
211
- await Deno.remove(backupRoot, { recursive: true });
205
+ await removePath(backupRoot, { recursive: true });
212
206
  } catch (error) {
213
207
  const errors = [error];
214
208
  for (const destination of installed.reverse()) {
@@ -220,8 +214,8 @@ const restoreSelectedPaths = async (
220
214
  }
221
215
  for (const entry of backedUp.reverse()) {
222
216
  try {
223
- await Deno.mkdir(dirname(entry.destination), { recursive: true, mode: 0o700 });
224
- await Deno.rename(entry.backup, entry.destination);
217
+ await plugins.fs.mkdir(dirname(entry.destination), { recursive: true, mode: 0o700 });
218
+ await plugins.fs.rename(entry.backup, entry.destination);
225
219
  } catch (rollbackError) {
226
220
  errors.push(rollbackError);
227
221
  }
@@ -316,12 +310,12 @@ export class CacheManager implements ICacheManager {
316
310
  let restorePath: string | undefined;
317
311
  let primaryError: unknown;
318
312
  try {
319
- await Deno.mkdir(restoreRoot, { recursive: true, mode: 0o700 });
320
- restorePath = await Deno.makeTempDir({
313
+ await plugins.fs.mkdir(restoreRoot, { recursive: true, mode: 0o700 });
314
+ restorePath = await makeTempDir({
321
315
  dir: restoreRoot,
322
316
  prefix: 'cache-',
323
317
  });
324
- await Deno.chmod(restorePath, 0o700);
318
+ await plugins.fs.chmod(restorePath, 0o700);
325
319
  const extracted = await extractTarGzipArchive({
326
320
  archivePath: staged.path,
327
321
  destinationPath: restorePath,
@@ -368,7 +362,7 @@ export class CacheManager implements ICacheManager {
368
362
  for (const cache of context.job.caches) {
369
363
  if (cache.access !== 'read-write') continue;
370
364
  if (context.signal.aborted) throw context.signal.reason;
371
- await Deno.mkdir(this.config.stateDirectory, { recursive: true, mode: 0o700 });
365
+ await plugins.fs.mkdir(this.config.stateDirectory, { recursive: true, mode: 0o700 });
372
366
  await ensureStagingCapacity(
373
367
  this.config.stateDirectory,
374
368
  cache.maximumExtractedBytes,
@@ -387,7 +381,7 @@ export class CacheManager implements ICacheManager {
387
381
  signal: context.signal,
388
382
  });
389
383
  const archiveRoot = join(this.config.stateDirectory, 'cache-uploads');
390
- await Deno.mkdir(archiveRoot, { recursive: true, mode: 0o700 });
384
+ await plugins.fs.mkdir(archiveRoot, { recursive: true, mode: 0o700 });
391
385
  await ensureStagingCapacity(
392
386
  archiveRoot,
393
387
  cache.maximumBytes,
@@ -408,7 +402,7 @@ export class CacheManager implements ICacheManager {
408
402
  redactionSet: context.redactionSet,
409
403
  signal: context.signal,
410
404
  });
411
- const transferRequestId = crypto.randomUUID();
405
+ const transferRequestId = plugins.crypto.randomUUID();
412
406
  const record = await this.api.uploadCache(
413
407
  context.identity,
414
408
  cache.name,
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+ import { assertSupportedNodeVersion } from './node-version.js';
3
+
4
+ // The CLI's modules load only after the Node.js version check, so an unsupported release gets a
5
+ // clear refusal instead of a failure deep inside.
6
+ let supported = false;
7
+ try {
8
+ assertSupportedNodeVersion(process.versions.node);
9
+ supported = true;
10
+ } catch (error) {
11
+ console.error(error instanceof Error ? error.message : 'Runner failed');
12
+ process.exitCode = 1;
13
+ }
14
+ if (supported) {
15
+ const { runCli } = await import('./cli.js');
16
+ await runCli(process.argv.slice(2));
17
+ }