@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/.smartconfig.json +48 -0
- package/changelog.md +12 -0
- package/deno.json +41 -0
- package/deno.lock +1843 -0
- package/dist_npm/mod.d.ts +689 -0
- package/dist_npm/mod.js +8980 -0
- package/dist_rust/runnerworkspace_linux_amd64_musl +0 -0
- package/dist_rust/runnerworkspace_linux_arm64_musl +0 -0
- package/license.md +18 -0
- package/mod.ts +8 -0
- package/musl-copyright.md +81 -0
- package/package.json +58 -0
- package/readme.md +274 -0
- package/rust/.cargo/config.toml +5 -0
- package/rust/Cargo.lock +144 -0
- package/rust/Cargo.toml +17 -0
- package/rust/src/lib.rs +1784 -0
- package/rust/src/main.rs +6 -0
- package/third-party-licenses.md +95 -0
- package/ts/00_commitinfo_data.ts +8 -0
- package/ts/api-client.ts +1059 -0
- package/ts/artifact-manager.ts +218 -0
- package/ts/attempt-state.ts +190 -0
- package/ts/cache-manager.ts +431 -0
- package/ts/cli.ts +239 -0
- package/ts/config.ts +724 -0
- package/ts/errors.ts +68 -0
- package/ts/index.ts +13 -0
- package/ts/interfaces.ts +547 -0
- package/ts/log-sender.ts +134 -0
- package/ts/oci-executor.ts +2472 -0
- package/ts/plugins.ts +11 -0
- package/ts/protocol-validator.ts +535 -0
- package/ts/redactor.ts +41 -0
- package/ts/registration-state.ts +253 -0
- package/ts/runner.ts +1036 -0
- package/ts/source-workspace.ts +161 -0
- package/ts/staging-scavenger.ts +62 -0
- package/ts/transfer-archive.ts +1605 -0
- package/ts/validation.ts +77 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { join } from '@std/path';
|
|
2
|
+
import {
|
|
3
|
+
AuthenticationError,
|
|
4
|
+
LeaseLostError,
|
|
5
|
+
ProtocolError,
|
|
6
|
+
ResourceCleanupError,
|
|
7
|
+
} from './errors.ts';
|
|
8
|
+
import type {
|
|
9
|
+
IArtifactDeclaration,
|
|
10
|
+
IArtifactManager,
|
|
11
|
+
IArtifactUploadContext,
|
|
12
|
+
IJobIdentity,
|
|
13
|
+
ITransferManagerConfig,
|
|
14
|
+
ITransferMetadata,
|
|
15
|
+
ITransferRecord,
|
|
16
|
+
TOpenTransferBody,
|
|
17
|
+
} from './interfaces.ts';
|
|
18
|
+
import {
|
|
19
|
+
createTarGzipArchive,
|
|
20
|
+
ensureStagingCapacity,
|
|
21
|
+
validateRelativeSelections,
|
|
22
|
+
validateTransferRecord,
|
|
23
|
+
} from './transfer-archive.ts';
|
|
24
|
+
|
|
25
|
+
export interface IArtifactApiClient {
|
|
26
|
+
uploadArtifact(
|
|
27
|
+
identity: IJobIdentity,
|
|
28
|
+
name: string,
|
|
29
|
+
transferRequestId: string,
|
|
30
|
+
metadata: ITransferMetadata,
|
|
31
|
+
openBody: TOpenTransferBody,
|
|
32
|
+
signal?: AbortSignal,
|
|
33
|
+
): Promise<ITransferRecord>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const safePositive = (value: number, field: string): number => {
|
|
37
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
38
|
+
throw new ProtocolError(`${field} must be a positive integer`);
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const shouldUpload = (
|
|
44
|
+
artifact: IArtifactDeclaration,
|
|
45
|
+
status: IArtifactUploadContext['status'],
|
|
46
|
+
): boolean => {
|
|
47
|
+
if (artifact.when === 'always') return true;
|
|
48
|
+
if (artifact.when === 'success') return status === 'succeeded';
|
|
49
|
+
if (artifact.when === 'failure') return status !== 'succeeded';
|
|
50
|
+
throw new ProtocolError(`Artifact ${artifact.name} has an invalid when condition`);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const validateArtifacts = (
|
|
54
|
+
config: ITransferManagerConfig,
|
|
55
|
+
context: IArtifactUploadContext,
|
|
56
|
+
): void => {
|
|
57
|
+
const artifacts = context.job.artifacts;
|
|
58
|
+
if (artifacts.length === 0) return;
|
|
59
|
+
if (context.job.permissions.artifacts !== 'write') {
|
|
60
|
+
throw new ProtocolError('Job does not permit artifact uploads');
|
|
61
|
+
}
|
|
62
|
+
if (!context.job.requirements.features.includes('artifacts.v1')) {
|
|
63
|
+
throw new ProtocolError('Job does not declare the artifacts.v1 feature');
|
|
64
|
+
}
|
|
65
|
+
const maximumArtifacts = Math.min(
|
|
66
|
+
config.maximumArtifacts,
|
|
67
|
+
context.job.requirements.limits.maximumArtifacts,
|
|
68
|
+
);
|
|
69
|
+
if (artifacts.length > maximumArtifacts) {
|
|
70
|
+
throw new ProtocolError('Job declares too many artifacts');
|
|
71
|
+
}
|
|
72
|
+
const maximumArchiveBytes = Math.min(
|
|
73
|
+
config.maximumArtifactBytes,
|
|
74
|
+
context.job.requirements.limits.maximumArtifactArchiveBytes,
|
|
75
|
+
);
|
|
76
|
+
const maximumExtractedBytes = Math.min(
|
|
77
|
+
config.maximumArtifactExtractedBytes,
|
|
78
|
+
config.oci.workspaceSizeBytes,
|
|
79
|
+
context.job.requirements.limits.maximumArtifactExtractedBytes,
|
|
80
|
+
context.job.requirements.limits.maximumWorkspaceBytes,
|
|
81
|
+
);
|
|
82
|
+
const maximumEntries = Math.min(
|
|
83
|
+
config.maximumArtifactEntries,
|
|
84
|
+
config.maximumWorkspaceEntries,
|
|
85
|
+
context.job.requirements.limits.maximumArtifactEntries,
|
|
86
|
+
context.job.requirements.limits.maximumWorkspaceEntries,
|
|
87
|
+
);
|
|
88
|
+
for (const artifact of artifacts) {
|
|
89
|
+
if (artifact.format !== 'tar.gz') {
|
|
90
|
+
throw new ProtocolError(`Artifact ${artifact.name} has an unsupported format`);
|
|
91
|
+
}
|
|
92
|
+
if (typeof artifact.required !== 'boolean') {
|
|
93
|
+
throw new ProtocolError(`Artifact ${artifact.name} has an invalid required flag`);
|
|
94
|
+
}
|
|
95
|
+
validateRelativeSelections([artifact.path], `Artifact ${artifact.name}`, 1);
|
|
96
|
+
safePositive(artifact.maximumBytes, `Artifact ${artifact.name} maximumBytes`);
|
|
97
|
+
safePositive(
|
|
98
|
+
artifact.maximumExtractedBytes,
|
|
99
|
+
`Artifact ${artifact.name} maximumExtractedBytes`,
|
|
100
|
+
);
|
|
101
|
+
safePositive(artifact.maximumEntries, `Artifact ${artifact.name} maximumEntries`);
|
|
102
|
+
if (
|
|
103
|
+
artifact.maximumBytes > maximumArchiveBytes ||
|
|
104
|
+
artifact.maximumExtractedBytes > maximumExtractedBytes ||
|
|
105
|
+
artifact.maximumEntries > maximumEntries
|
|
106
|
+
) {
|
|
107
|
+
throw new ProtocolError(`Artifact ${artifact.name} exceeds its effective runner limits`);
|
|
108
|
+
}
|
|
109
|
+
shouldUpload(artifact, context.status);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const optionalFailureMustPropagate = (
|
|
114
|
+
error: unknown,
|
|
115
|
+
artifact: IArtifactDeclaration,
|
|
116
|
+
signal: AbortSignal,
|
|
117
|
+
): boolean =>
|
|
118
|
+
artifact.required ||
|
|
119
|
+
signal.aborted ||
|
|
120
|
+
error instanceof AuthenticationError ||
|
|
121
|
+
error instanceof LeaseLostError ||
|
|
122
|
+
error instanceof ResourceCleanupError;
|
|
123
|
+
|
|
124
|
+
export class ArtifactManager implements IArtifactManager {
|
|
125
|
+
constructor(
|
|
126
|
+
private readonly config: ITransferManagerConfig,
|
|
127
|
+
private readonly api: IArtifactApiClient,
|
|
128
|
+
private readonly onMessage?: (message: string) => void,
|
|
129
|
+
) {}
|
|
130
|
+
|
|
131
|
+
public async upload(context: IArtifactUploadContext): Promise<void> {
|
|
132
|
+
validateArtifacts(this.config, context);
|
|
133
|
+
for (const artifact of context.job.artifacts) {
|
|
134
|
+
if (!shouldUpload(artifact, context.status)) continue;
|
|
135
|
+
if (context.signal.aborted) throw context.signal.reason;
|
|
136
|
+
let exported: Awaited<ReturnType<typeof context.workspace.exportSelected>> | undefined;
|
|
137
|
+
let archive: Awaited<ReturnType<typeof createTarGzipArchive>> | undefined;
|
|
138
|
+
let operationError: unknown;
|
|
139
|
+
try {
|
|
140
|
+
await Deno.mkdir(this.config.stateDirectory, { recursive: true, mode: 0o700 });
|
|
141
|
+
await ensureStagingCapacity(
|
|
142
|
+
this.config.stateDirectory,
|
|
143
|
+
artifact.maximumExtractedBytes,
|
|
144
|
+
artifact.maximumEntries + 1,
|
|
145
|
+
this.config.transferStagingReserveBytes,
|
|
146
|
+
this.config.transferStagingReserveInodes,
|
|
147
|
+
);
|
|
148
|
+
exported = await context.workspace.exportSelected({
|
|
149
|
+
selections: [artifact.path],
|
|
150
|
+
maximumBytes: artifact.maximumExtractedBytes,
|
|
151
|
+
maximumEntries: artifact.maximumEntries,
|
|
152
|
+
signal: context.signal,
|
|
153
|
+
});
|
|
154
|
+
const archiveRoot = join(this.config.stateDirectory, 'artifact-uploads');
|
|
155
|
+
await Deno.mkdir(archiveRoot, { recursive: true, mode: 0o700 });
|
|
156
|
+
await ensureStagingCapacity(
|
|
157
|
+
archiveRoot,
|
|
158
|
+
artifact.maximumBytes,
|
|
159
|
+
1,
|
|
160
|
+
this.config.transferStagingReserveBytes,
|
|
161
|
+
this.config.transferStagingReserveInodes,
|
|
162
|
+
);
|
|
163
|
+
archive = await createTarGzipArchive({
|
|
164
|
+
sourcePath: exported.path,
|
|
165
|
+
selections: [artifact.path],
|
|
166
|
+
expectedMetadata: exported.metadata,
|
|
167
|
+
archiveDirectory: archiveRoot,
|
|
168
|
+
archivePrefix: 'artifact-',
|
|
169
|
+
maximumArchiveBytes: artifact.maximumBytes,
|
|
170
|
+
maximumExtractedBytes: artifact.maximumExtractedBytes,
|
|
171
|
+
maximumEntries: artifact.maximumEntries,
|
|
172
|
+
allowSymlinks: true,
|
|
173
|
+
signal: context.signal,
|
|
174
|
+
});
|
|
175
|
+
const transferRequestId = crypto.randomUUID();
|
|
176
|
+
const record = await this.api.uploadArtifact(
|
|
177
|
+
context.identity,
|
|
178
|
+
artifact.name,
|
|
179
|
+
transferRequestId,
|
|
180
|
+
archive.metadata,
|
|
181
|
+
archive.openBody,
|
|
182
|
+
context.signal,
|
|
183
|
+
);
|
|
184
|
+
validateTransferRecord(record, artifact.name, archive.metadata);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
operationError = error;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const cleanupErrors: unknown[] = [];
|
|
190
|
+
for (
|
|
191
|
+
const cleanup of [
|
|
192
|
+
...(archive ? [() => archive!.cleanup()] : []),
|
|
193
|
+
...(exported ? [() => exported!.cleanup()] : []),
|
|
194
|
+
]
|
|
195
|
+
) {
|
|
196
|
+
try {
|
|
197
|
+
await cleanup();
|
|
198
|
+
} catch (error) {
|
|
199
|
+
cleanupErrors.push(error);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (cleanupErrors.length) {
|
|
203
|
+
throw new ResourceCleanupError(
|
|
204
|
+
`Artifact ${artifact.name} publication and cleanup failed`,
|
|
205
|
+
operationError === undefined ? cleanupErrors : [operationError, ...cleanupErrors],
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
if (operationError !== undefined) {
|
|
209
|
+
if (optionalFailureMustPropagate(operationError, artifact, context.signal)) {
|
|
210
|
+
throw operationError;
|
|
211
|
+
}
|
|
212
|
+
this.onMessage?.(`Optional artifact ${artifact.name} was omitted`);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
this.onMessage?.(`Artifact ${artifact.name} uploaded`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { join } from '@std/path';
|
|
2
|
+
import { RunnerError } from './errors.ts';
|
|
3
|
+
|
|
4
|
+
export interface IAttemptJournal {
|
|
5
|
+
previousSessionId: string;
|
|
6
|
+
jobId: string;
|
|
7
|
+
attemptId: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const identifierPattern = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
11
|
+
const journalName = 'active-attempt.json';
|
|
12
|
+
const maximumJournalBytes = 1024;
|
|
13
|
+
|
|
14
|
+
const validateJournal = (value: unknown): IAttemptJournal => {
|
|
15
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
16
|
+
throw new RunnerError('Attempt journal must contain a JSON object');
|
|
17
|
+
}
|
|
18
|
+
const record = value as Record<string, unknown>;
|
|
19
|
+
const keys = Object.keys(record);
|
|
20
|
+
if (
|
|
21
|
+
keys.length !== 3 ||
|
|
22
|
+
!Object.hasOwn(record, 'previousSessionId') ||
|
|
23
|
+
!Object.hasOwn(record, 'jobId') ||
|
|
24
|
+
!Object.hasOwn(record, 'attemptId') ||
|
|
25
|
+
keys.some((key) => !['previousSessionId', 'jobId', 'attemptId'].includes(key))
|
|
26
|
+
) {
|
|
27
|
+
throw new RunnerError('Attempt journal has an invalid shape');
|
|
28
|
+
}
|
|
29
|
+
for (const field of ['previousSessionId', 'jobId', 'attemptId'] as const) {
|
|
30
|
+
if (typeof record[field] !== 'string' || !identifierPattern.test(record[field])) {
|
|
31
|
+
throw new RunnerError(`Attempt journal contains an invalid ${field}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
previousSessionId: record.previousSessionId as string,
|
|
36
|
+
jobId: record.jobId as string,
|
|
37
|
+
attemptId: record.attemptId as string,
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
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
|
+
export class AttemptStateStore {
|
|
53
|
+
readonly path: string;
|
|
54
|
+
|
|
55
|
+
constructor(private readonly stateDirectory: string) {
|
|
56
|
+
this.path = join(stateDirectory, journalName);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async load(): Promise<IAttemptJournal | undefined> {
|
|
60
|
+
await this.ensureDirectory();
|
|
61
|
+
let pathStat: Deno.FileInfo;
|
|
62
|
+
try {
|
|
63
|
+
pathStat = await Deno.lstat(this.path);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (isNotFound(error)) return undefined;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
this.assertSafeJournalFile(pathStat);
|
|
69
|
+
if (pathStat.size > maximumJournalBytes) {
|
|
70
|
+
throw new RunnerError('Attempt journal exceeds its size limit');
|
|
71
|
+
}
|
|
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
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async save(journal: IAttemptJournal): Promise<void> {
|
|
105
|
+
const validated = validateJournal(journal);
|
|
106
|
+
await this.ensureDirectory();
|
|
107
|
+
try {
|
|
108
|
+
this.assertSafeJournalFile(await Deno.lstat(this.path));
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (!isNotFound(error)) throw error;
|
|
111
|
+
}
|
|
112
|
+
const temporaryPath = join(
|
|
113
|
+
this.stateDirectory,
|
|
114
|
+
`.${crypto.randomUUID()}.attempt-journal.tmp`,
|
|
115
|
+
);
|
|
116
|
+
const bytes = new TextEncoder().encode(`${JSON.stringify(validated)}\n`);
|
|
117
|
+
if (bytes.byteLength > maximumJournalBytes) {
|
|
118
|
+
throw new RunnerError('Attempt journal exceeds its size limit');
|
|
119
|
+
}
|
|
120
|
+
let file: Deno.FsFile | undefined;
|
|
121
|
+
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);
|
|
134
|
+
await this.syncDirectory();
|
|
135
|
+
} catch (error) {
|
|
136
|
+
file?.close();
|
|
137
|
+
try {
|
|
138
|
+
await Deno.remove(temporaryPath);
|
|
139
|
+
} catch (cleanupError) {
|
|
140
|
+
if (!isNotFound(cleanupError)) {
|
|
141
|
+
throw new AggregateError([error, cleanupError], 'Attempt journal write cleanup failed');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async clear(): Promise<void> {
|
|
149
|
+
await this.ensureDirectory();
|
|
150
|
+
let stat: Deno.FileInfo;
|
|
151
|
+
try {
|
|
152
|
+
stat = await Deno.lstat(this.path);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (isNotFound(error)) return;
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
this.assertSafeJournalFile(stat);
|
|
158
|
+
await Deno.remove(this.path);
|
|
159
|
+
await this.syncDirectory();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
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
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private assertSafeJournalFile(stat: Deno.FileInfo): void {
|
|
174
|
+
if (!stat.isFile || stat.isSymlink) {
|
|
175
|
+
throw new RunnerError('Attempt journal must be a regular file');
|
|
176
|
+
}
|
|
177
|
+
if (Deno.build.os !== 'windows' && stat.mode !== null && (stat.mode & 0o777) !== 0o600) {
|
|
178
|
+
throw new RunnerError('Attempt journal permissions must be 0600');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
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
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|