@ship.zone/runner 0.1.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/ts/errors.ts ADDED
@@ -0,0 +1,68 @@
1
+ export class RunnerError extends Error {
2
+ constructor(message: string, options?: ErrorOptions) {
3
+ super(message, options);
4
+ this.name = 'RunnerError';
5
+ }
6
+ }
7
+
8
+ export class ResourceCleanupError extends RunnerError {
9
+ public readonly errors: readonly unknown[];
10
+
11
+ constructor(message: string, errors: readonly unknown[]) {
12
+ const preservedErrors: unknown[] = [];
13
+ const preserve = (error: unknown): void => {
14
+ if (error instanceof ResourceCleanupError) {
15
+ for (const nested of error.errors) preserve(nested);
16
+ } else {
17
+ preservedErrors.push(error);
18
+ }
19
+ };
20
+ for (const error of errors) preserve(error);
21
+ super(message, {
22
+ cause: preservedErrors.length === 1
23
+ ? preservedErrors[0]
24
+ : new AggregateError(preservedErrors, message),
25
+ });
26
+ this.name = 'ResourceCleanupError';
27
+ this.errors = Object.freeze(preservedErrors);
28
+ }
29
+ }
30
+
31
+ export class ConfigurationError extends RunnerError {
32
+ constructor(message: string) {
33
+ super(message);
34
+ this.name = 'ConfigurationError';
35
+ }
36
+ }
37
+
38
+ export class CoordinatorError extends RunnerError {
39
+ constructor(
40
+ message: string,
41
+ public readonly status?: number,
42
+ options?: ErrorOptions,
43
+ ) {
44
+ super(message, options);
45
+ this.name = 'CoordinatorError';
46
+ }
47
+ }
48
+
49
+ export class AuthenticationError extends CoordinatorError {
50
+ constructor(message = 'Runner authentication was rejected') {
51
+ super(message, 401);
52
+ this.name = 'AuthenticationError';
53
+ }
54
+ }
55
+
56
+ export class LeaseLostError extends CoordinatorError {
57
+ constructor(message = 'The job lease is no longer valid') {
58
+ super(message, 409);
59
+ this.name = 'LeaseLostError';
60
+ }
61
+ }
62
+
63
+ export class ProtocolError extends CoordinatorError {
64
+ constructor(message: string, options?: ErrorOptions) {
65
+ super(message, undefined, options);
66
+ this.name = 'ProtocolError';
67
+ }
68
+ }
package/ts/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ export * from './api-client.ts';
2
+ export * from './artifact-manager.ts';
3
+ export * from './cache-manager.ts';
4
+ export * from './config.ts';
5
+ export * from './errors.ts';
6
+ export * from './interfaces.ts';
7
+ export * from './log-sender.ts';
8
+ export * from './oci-executor.ts';
9
+ export * from './protocol-validator.ts';
10
+ export * from './redactor.ts';
11
+ export * from './runner.ts';
12
+ export * from './source-workspace.ts';
13
+ export * from './staging-scavenger.ts';
@@ -0,0 +1,547 @@
1
+ import * as plugins from './plugins.ts';
2
+ import denoConfig from '../deno.json' with { type: 'json' };
3
+
4
+ export const runnerVersion: string = denoConfig.version;
5
+ export const runnerProtocolIdentifier: 'foss-ci-runner/2.0-draft.1' =
6
+ plugins.runnerProtocolIdentifier;
7
+
8
+ export type TRunnerProtocolIdentifier = typeof runnerProtocolIdentifier;
9
+ export type TExecutionProfile = 'oci.v1';
10
+ export type TRunnerFeature = 'source.tar-gz' | 'artifacts.v1' | 'caches.v1';
11
+ export type TJobNetwork = 'none' | 'bridge';
12
+ export type TSourceFormat = 'tar.gz';
13
+ export type TOciRuntime = 'docker' | 'podman';
14
+ export type TArtifactPermission = 'none' | 'write';
15
+ export type TCachePermission = 'none' | 'read' | 'read-write';
16
+ export type TCacheAccess = 'read' | 'read-write';
17
+ export type TArtifactWhen = 'always' | 'success' | 'failure';
18
+ export type TLogStream = 'stdout' | 'stderr' | 'runner';
19
+ export type TJobCompletionStatus =
20
+ | 'succeeded'
21
+ | 'failed'
22
+ | 'canceled'
23
+ | 'timed_out'
24
+ | 'system_error';
25
+ export type TCancellationReason =
26
+ | 'user_requested'
27
+ | 'superseded'
28
+ | 'policy'
29
+ | 'coordinator_shutdown';
30
+ export type TAbandonReason = 'runner_shutdown' | 'runner_error';
31
+ export type TJsonPrimitive = null | boolean | number | string;
32
+ export type TJsonValue = TJsonPrimitive | TJsonValue[] | { [key: string]: TJsonValue };
33
+ export type TJsonObject = { [key: string]: TJsonValue };
34
+
35
+ export interface IProtocolLimits {
36
+ maximumJsonBytes: number;
37
+ maximumLogChunkBytes: number;
38
+ maximumTotalLogBytes: number;
39
+ minimumLeaseSeconds: number;
40
+ maximumLeaseSeconds: number;
41
+ }
42
+
43
+ export interface IDiscoveryResponse {
44
+ protocol: TRunnerProtocolIdentifier;
45
+ basePath: '/api/runner/v2';
46
+ status: 'draft';
47
+ serverTime: string;
48
+ limits: IProtocolLimits;
49
+ }
50
+
51
+ export interface IRunnerCredential {
52
+ credentialId: string;
53
+ runnerId: string;
54
+ token: string;
55
+ expiresAt: string;
56
+ previousCredentialValidUntil?: string;
57
+ }
58
+
59
+ export interface ITransferLimits {
60
+ maximumSourceArchiveBytes: number;
61
+ maximumSourceExtractedBytes: number;
62
+ maximumSourceEntries: number;
63
+ maximumArtifactArchiveBytes: number;
64
+ maximumArtifactExtractedBytes: number;
65
+ maximumArtifactEntries: number;
66
+ maximumArtifacts: number;
67
+ maximumCacheArchiveBytes: number;
68
+ maximumCacheExtractedBytes: number;
69
+ maximumCacheEntries: number;
70
+ maximumCaches: number;
71
+ maximumLogChunkBytes: number;
72
+ maximumTotalLogBytes: number;
73
+ maximumWorkspaceBytes: number;
74
+ maximumWorkspaceEntries: number;
75
+ }
76
+
77
+ export interface IRunnerCapabilities {
78
+ executionProfiles: TExecutionProfile[];
79
+ labels: string[];
80
+ features: TRunnerFeature[];
81
+ networks: TJobNetwork[];
82
+ sourceFormats: TSourceFormat[];
83
+ limits: ITransferLimits;
84
+ }
85
+
86
+ export interface IRegisterRunnerRequest {
87
+ registrationRequestId: string;
88
+ protocol: TRunnerProtocolIdentifier;
89
+ runnerVersion: string;
90
+ name: string;
91
+ capabilities: IRunnerCapabilities;
92
+ }
93
+
94
+ export interface IRegisterRunnerResponse {
95
+ protocol: TRunnerProtocolIdentifier;
96
+ serverTime: string;
97
+ registrationReplayUntil: string;
98
+ credential: IRunnerCredential;
99
+ }
100
+
101
+ export interface IStartSessionRequest {
102
+ sessionRequestId: string;
103
+ runnerId: string;
104
+ protocol: TRunnerProtocolIdentifier;
105
+ runnerVersion: string;
106
+ capabilities: IRunnerCapabilities;
107
+ }
108
+
109
+ export interface IStartSessionResponse {
110
+ sessionId: string;
111
+ protocol: TRunnerProtocolIdentifier;
112
+ serverTime: string;
113
+ credential?: IRunnerCredential;
114
+ }
115
+
116
+ export interface IRunnerHeartbeatRequest {
117
+ runnerId: string;
118
+ sessionId: string;
119
+ }
120
+
121
+ export interface IRunnerHeartbeatResponse {
122
+ serverTime: string;
123
+ drainRequested: boolean;
124
+ credential?: IRunnerCredential;
125
+ }
126
+
127
+ export interface IClaimJobRequest {
128
+ claimRequestId: string;
129
+ runnerId: string;
130
+ sessionId: string;
131
+ capabilities: IRunnerCapabilities;
132
+ }
133
+
134
+ export interface IClaimJobResponse {
135
+ serverTime: string;
136
+ retryAfterMs?: number;
137
+ lease: unknown | null;
138
+ }
139
+
140
+ export interface IJobLease {
141
+ jobId: string;
142
+ attemptId: string;
143
+ leaseId: string;
144
+ fencingToken: string;
145
+ leaseExpiresAt: string;
146
+ job: ICompiledRunnerJob;
147
+ }
148
+
149
+ export interface IJobIdentity {
150
+ runnerId: string;
151
+ sessionId: string;
152
+ jobId: string;
153
+ attemptId: string;
154
+ leaseId: string;
155
+ fencingToken: string;
156
+ }
157
+
158
+ export interface IAcceptJobRequest {
159
+ acceptRequestId: string;
160
+ identity: IJobIdentity;
161
+ }
162
+
163
+ export interface IJobHeartbeatRequest {
164
+ identity: IJobIdentity;
165
+ observedCancellationRequestId?: string;
166
+ }
167
+
168
+ export interface ICancellationDirective {
169
+ requestId: string;
170
+ requestedAt: string;
171
+ reason: TCancellationReason;
172
+ terminateBy: string;
173
+ }
174
+
175
+ export interface IJobHeartbeatResponse {
176
+ serverTime: string;
177
+ leaseExpiresAt: string;
178
+ cancellation?: ICancellationDirective;
179
+ }
180
+
181
+ export interface ILogChunk {
182
+ identity: IJobIdentity;
183
+ sequence: number;
184
+ stream: TLogStream;
185
+ data: string;
186
+ timestamp: string;
187
+ }
188
+
189
+ export interface ILogAcknowledgement {
190
+ serverTime: string;
191
+ nextSequence: number;
192
+ }
193
+
194
+ export interface ICompleteJobRequest {
195
+ completionRequestId: string;
196
+ identity: IJobIdentity;
197
+ status: TJobCompletionStatus;
198
+ exitCode: number | null;
199
+ startedAt: string;
200
+ completedAt: string;
201
+ cancellationRequestId?: string;
202
+ error?: string;
203
+ }
204
+
205
+ export interface ICompleteJobResponse {
206
+ serverTime: string;
207
+ accepted: true;
208
+ }
209
+
210
+ export interface IAbandonJobRequest {
211
+ abandonRequestId: string;
212
+ identity: IJobIdentity;
213
+ reason: TAbandonReason;
214
+ }
215
+
216
+ export interface IRecoverAttemptRequest {
217
+ recoveryRequestId: string;
218
+ runnerId: string;
219
+ currentSessionId: string;
220
+ previousSessionId: string;
221
+ jobId: string;
222
+ attemptId: string;
223
+ reason: 'runner_restarted';
224
+ }
225
+
226
+ export interface ITransferRecord {
227
+ name: string;
228
+ format: 'tar.gz';
229
+ sha256: string;
230
+ sizeBytes: number;
231
+ extractedSizeBytes: number;
232
+ entryCount: number;
233
+ createdAt: string;
234
+ }
235
+
236
+ export interface IErrorResponse {
237
+ error: {
238
+ code: string;
239
+ message: string;
240
+ };
241
+ }
242
+
243
+ export interface IJobRequirements {
244
+ executionProfile: TExecutionProfile;
245
+ labels: string[];
246
+ features: TRunnerFeature[];
247
+ limits: ITransferLimits;
248
+ }
249
+
250
+ export interface IJobPermissions {
251
+ source: 'read';
252
+ artifacts: TArtifactPermission;
253
+ caches: TCachePermission;
254
+ packages: 'none';
255
+ }
256
+
257
+ export interface IOciExecution {
258
+ profile: 'oci.v1';
259
+ image: string;
260
+ network: TJobNetwork;
261
+ workingDirectory?: string;
262
+ }
263
+
264
+ export interface ICompiledStep {
265
+ name?: string;
266
+ command: string[];
267
+ environment: Record<string, string>;
268
+ }
269
+
270
+ export interface IJobSource {
271
+ format: 'tar.gz';
272
+ sha256: string;
273
+ sizeBytes: number;
274
+ extractedSizeBytes: number;
275
+ entryCount: number;
276
+ }
277
+
278
+ export interface IJobResources {
279
+ memoryBytes?: number;
280
+ cpus?: number;
281
+ pids?: number;
282
+ }
283
+
284
+ export interface IArtifactDeclaration {
285
+ name: string;
286
+ path: string;
287
+ format: 'tar.gz';
288
+ when: TArtifactWhen;
289
+ required: boolean;
290
+ maximumBytes: number;
291
+ maximumExtractedBytes: number;
292
+ maximumEntries: number;
293
+ retentionDays?: number;
294
+ }
295
+
296
+ export interface ICacheDeclaration {
297
+ name: string;
298
+ key: string;
299
+ paths: string[];
300
+ access: TCacheAccess;
301
+ maximumBytes: number;
302
+ maximumExtractedBytes: number;
303
+ maximumEntries: number;
304
+ retentionDays?: number;
305
+ }
306
+
307
+ export interface ICompiledRunnerJob {
308
+ specVersion: '2.0-draft.1';
309
+ sourceObjectId: string;
310
+ workflowDigest: string;
311
+ compiledPlanDigest: string;
312
+ requirements: IJobRequirements;
313
+ permissions: IJobPermissions;
314
+ execution: IOciExecution;
315
+ steps: ICompiledStep[];
316
+ source: IJobSource;
317
+ secrets: Record<string, string>;
318
+ timeoutMs: number;
319
+ resources?: IJobResources;
320
+ artifacts: IArtifactDeclaration[];
321
+ caches: ICacheDeclaration[];
322
+ }
323
+
324
+ export interface ITransferMetadata {
325
+ sha256: string;
326
+ sizeBytes: number;
327
+ extractedSizeBytes: number;
328
+ entryCount: number;
329
+ }
330
+
331
+ export interface ITransferDownload extends ITransferMetadata {
332
+ body: ReadableStream<Uint8Array>;
333
+ }
334
+
335
+ export interface ICacheDownloadMiss {
336
+ hit: false;
337
+ }
338
+
339
+ export interface ICacheDownloadHit extends ITransferDownload {
340
+ hit: true;
341
+ }
342
+
343
+ export type TCacheDownload = ICacheDownloadMiss | ICacheDownloadHit;
344
+ export type TOpenTransferBody = () =>
345
+ | ReadableStream<Uint8Array>
346
+ | Promise<ReadableStream<Uint8Array>>;
347
+
348
+ export interface ITransferManagerConfig {
349
+ stateDirectory: string;
350
+ transferStagingReserveBytes: number;
351
+ transferStagingReserveInodes: number;
352
+ maximumSourceArchiveBytes: number;
353
+ maximumSourceExtractedBytes: number;
354
+ maximumSourceEntries: number;
355
+ maximumArtifactBytes: number;
356
+ maximumArtifactExtractedBytes: number;
357
+ maximumArtifactEntries: number;
358
+ maximumArtifacts: number;
359
+ maximumCacheArchiveBytes: number;
360
+ maximumCacheExtractedBytes: number;
361
+ maximumCacheEntries: number;
362
+ maximumCaches: number;
363
+ maximumWorkspaceEntries: number;
364
+ oci: Pick<IOciExecutorConfig, 'workspaceSizeBytes'>;
365
+ }
366
+
367
+ export type TExecutionStopReason =
368
+ | 'job_canceled'
369
+ | 'lease_lost'
370
+ | 'lease_expired'
371
+ | 'runner_shutdown'
372
+ | 'job_timeout'
373
+ | 'log_delivery_failed'
374
+ | 'resource_limit';
375
+
376
+ export type TOciArchitecture = 'amd64' | 'arm64';
377
+ export type TOciHelperTarget = 'linux_amd64' | 'linux_arm64';
378
+
379
+ export interface IOciExecutorConfig {
380
+ runtime: TOciRuntime;
381
+ runtimePath: string;
382
+ workspaceHelperPath?: string;
383
+ requireRootless: boolean;
384
+ allowedNetworks: TJobNetwork[];
385
+ containerUser: string;
386
+ defaultTimeoutMs: number;
387
+ maximumTimeoutMs: number;
388
+ defaultMemoryBytes: number;
389
+ maximumMemoryBytes: number;
390
+ defaultCpus: number;
391
+ maximumCpus: number;
392
+ defaultPids: number;
393
+ maximumPids: number;
394
+ workspaceSizeBytes: number;
395
+ temporarySizeBytes: number;
396
+ stopTimeoutSeconds: number;
397
+ }
398
+
399
+ export interface IRunnerConfig extends ITransferManagerConfig {
400
+ controlPlaneUrl: string;
401
+ allowInsecureLoopback: boolean;
402
+ name: string;
403
+ labels: string[];
404
+ bootstrapToken?: string;
405
+ runnerId?: string;
406
+ runnerCredentialId?: string;
407
+ runnerToken?: string;
408
+ runnerTokenExpiresAt?: string;
409
+ pollIntervalMs: number;
410
+ runnerHeartbeatIntervalMs: number;
411
+ jobHeartbeatIntervalMs: number;
412
+ leaseSafetyMarginMs: number;
413
+ requestTimeoutMs: number;
414
+ maximumResponseBytes: number;
415
+ sourceTransferTimeoutMs: number;
416
+ maximumLogBytes: number;
417
+ maximumLogChunkBytes: number;
418
+ artifactUploadTimeoutMs: number;
419
+ cacheTransferTimeoutMs: number;
420
+ maximumEnvironmentVariables: number;
421
+ maximumEnvironmentValueBytes: number;
422
+ maximumSecretBytes: number;
423
+ oci: IOciExecutorConfig;
424
+ }
425
+
426
+ export interface IExecutionContext {
427
+ runnerId: string;
428
+ sessionId: string;
429
+ lease: IJobLease;
430
+ preparedLease: IPreparedOciLease;
431
+ workspacePath: string;
432
+ signal: AbortSignal;
433
+ terminationDeadline?: () => number | undefined;
434
+ onLog: (stream: 'stdout' | 'stderr', data: string) => Promise<void>;
435
+ }
436
+
437
+ export interface IOciRuntimeFacts {
438
+ readonly runtime: TOciRuntime;
439
+ readonly endpoint: string;
440
+ readonly rootless: boolean;
441
+ readonly os: 'linux';
442
+ readonly architecture: TOciArchitecture;
443
+ readonly helperTarget: TOciHelperTarget;
444
+ readonly helperPath: string;
445
+ readonly helperSha256: string;
446
+ readonly workspaceBlockSize: number;
447
+ }
448
+
449
+ export interface IPrepareOciLeaseContext {
450
+ runnerId: string;
451
+ sessionId: string;
452
+ lease: IJobLease;
453
+ signal: AbortSignal;
454
+ }
455
+
456
+ export interface IPreparedOciLease {
457
+ readonly preparationId: string;
458
+ readonly attemptId: string;
459
+ readonly image: string;
460
+ readonly imageArchitecture: TOciArchitecture;
461
+ cleanup(): Promise<void>;
462
+ }
463
+
464
+ export interface IWorkspaceTreeMetadata {
465
+ logicalBytes: number;
466
+ entryCount: number;
467
+ }
468
+
469
+ export interface IWorkspaceExportRequest {
470
+ selections: string[];
471
+ maximumBytes: number;
472
+ maximumEntries: number;
473
+ signal?: AbortSignal;
474
+ }
475
+
476
+ export interface IWorkspaceExport {
477
+ path: string;
478
+ metadata: IWorkspaceTreeMetadata;
479
+ cleanup(): Promise<void>;
480
+ }
481
+
482
+ export interface IRuntimeWorkspaceExporter {
483
+ exportSelected(request: IWorkspaceExportRequest): Promise<IWorkspaceExport>;
484
+ cleanup(): Promise<void>;
485
+ }
486
+
487
+ export interface IExecutionResult {
488
+ exitCode: number | null;
489
+ stopReason?: TExecutionStopReason;
490
+ workspace?: IRuntimeWorkspaceExporter;
491
+ }
492
+
493
+ export interface IJobExecutor {
494
+ verifyRuntime(): Promise<IOciRuntimeFacts>;
495
+ prepareLease(context: IPrepareOciLeaseContext): Promise<IPreparedOciLease>;
496
+ reconcileOrphans(runnerId: string, currentSessionId: string): Promise<void>;
497
+ execute(context: IExecutionContext): Promise<IExecutionResult>;
498
+ }
499
+
500
+ export interface IPreparedWorkspace {
501
+ path: string;
502
+ metadata: IWorkspaceTreeMetadata;
503
+ cleanup(): Promise<void>;
504
+ }
505
+
506
+ export interface ISourcePrepareContext {
507
+ identity: IJobIdentity;
508
+ job: Pick<ICompiledRunnerJob, 'source' | 'permissions' | 'requirements'>;
509
+ signal: AbortSignal;
510
+ }
511
+
512
+ export interface IRunnerRunResult {
513
+ claimed: boolean;
514
+ status?: TJobCompletionStatus;
515
+ }
516
+
517
+ export interface IArtifactUploadContext {
518
+ workspace: IRuntimeWorkspaceExporter;
519
+ job: Pick<ICompiledRunnerJob, 'artifacts' | 'permissions' | 'requirements'>;
520
+ status: TJobCompletionStatus;
521
+ identity: IJobIdentity;
522
+ signal: AbortSignal;
523
+ }
524
+
525
+ export interface IArtifactManager {
526
+ upload(context: IArtifactUploadContext): Promise<void>;
527
+ }
528
+
529
+ export interface ICacheRestoreContext {
530
+ workspacePath: string;
531
+ job: Pick<ICompiledRunnerJob, 'caches' | 'permissions' | 'requirements' | 'source'>;
532
+ identity: IJobIdentity;
533
+ signal: AbortSignal;
534
+ }
535
+
536
+ export interface ICacheSaveContext {
537
+ workspace: IRuntimeWorkspaceExporter;
538
+ job: Pick<ICompiledRunnerJob, 'caches' | 'permissions' | 'requirements'>;
539
+ status: TJobCompletionStatus;
540
+ identity: IJobIdentity;
541
+ signal: AbortSignal;
542
+ }
543
+
544
+ export interface ICacheManager {
545
+ restore(context: ICacheRestoreContext): Promise<void>;
546
+ save(context: ICacheSaveContext): Promise<void>;
547
+ }