@ship.zone/runner 5.0.1 → 5.2.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/changelog.md +17 -0
- package/dist_rust/runnerworkspace_linux_amd64_musl.tsrust-build.json +3 -3
- package/dist_rust/runnerworkspace_linux_arm64_musl.tsrust-build.json +3 -3
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/attempt-state.d.ts +6 -0
- package/dist_ts/attempt-state.js +85 -8
- package/dist_ts/index.d.ts +1 -0
- package/dist_ts/index.js +2 -1
- package/dist_ts/interfaces.d.ts +17 -0
- package/dist_ts/microvm-executor.js +19 -2
- package/dist_ts/resource-budget.d.ts +51 -0
- package/dist_ts/resource-budget.js +194 -0
- package/dist_ts/runner.d.ts +5 -2
- package/dist_ts/runner.js +159 -63
- package/package.json +2 -2
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/attempt-state.ts +88 -7
- package/ts/index.ts +1 -0
- package/ts/interfaces.ts +19 -0
- package/ts/microvm-executor.ts +21 -1
- package/ts/resource-budget.ts +258 -0
- package/ts/runner.ts +175 -65
package/ts/runner.ts
CHANGED
|
@@ -49,6 +49,7 @@ import { runnerVersion } from './interfaces.js';
|
|
|
49
49
|
import { LogSender } from './log-sender.js';
|
|
50
50
|
import { createRunnerCapabilitySnapshot, RunnerProtocolValidator } from './protocol-validator.js';
|
|
51
51
|
import { SecretRedactionSet } from './redactor.js';
|
|
52
|
+
import { type IResourceBudgetHostProbe, RunnerResourceBudget } from './resource-budget.js';
|
|
52
53
|
import { runnerSpecVersion, specMajorVersionsMatch } from './spec-version.js';
|
|
53
54
|
import { scavengeRunnerStaging } from './staging-scavenger.js';
|
|
54
55
|
import type { RunnerStateOwner } from './state-owner.js';
|
|
@@ -92,6 +93,8 @@ export interface IRunnerDependencies {
|
|
|
92
93
|
/** Monotonic milliseconds that measure the attempt deadline; `performance.now` by default. */
|
|
93
94
|
monotonicNow?: () => number;
|
|
94
95
|
sleep?: (milliseconds: number, signal?: AbortSignal) => Promise<void>;
|
|
96
|
+
/** Optional host resource probe for deterministic concurrency testing. */
|
|
97
|
+
hostProbe?: IResourceBudgetHostProbe;
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
interface IHeartbeatState {
|
|
@@ -346,6 +349,7 @@ export class ShipRunner {
|
|
|
346
349
|
if (session.credential) await this.applyCredential(session.credential);
|
|
347
350
|
|
|
348
351
|
const journal = new AttemptStateStore(stateOwner);
|
|
352
|
+
const budget = new RunnerResourceBudget(this.config, runtime, this.dependencies.hostProbe);
|
|
349
353
|
const sessionLifecycle = new AbortController();
|
|
350
354
|
const externalAbort = (): void => sessionLifecycle.abort('runner_shutdown');
|
|
351
355
|
options.signal?.addEventListener('abort', externalAbort, { once: true });
|
|
@@ -356,6 +360,7 @@ export class ShipRunner {
|
|
|
356
360
|
const runnerHeartbeat = this.runnerHeartbeatLoop(
|
|
357
361
|
runnerId,
|
|
358
362
|
session.sessionId,
|
|
363
|
+
budget,
|
|
359
364
|
sessionLifecycle,
|
|
360
365
|
).catch((error) => {
|
|
361
366
|
heartbeatError = error;
|
|
@@ -363,9 +368,39 @@ export class ShipRunner {
|
|
|
363
368
|
});
|
|
364
369
|
let operationError: unknown;
|
|
365
370
|
let lastResult: IRunnerRunResult = { claimed: false };
|
|
371
|
+
const inFlight = new Map<string, Promise<void>>();
|
|
372
|
+
let wakeWaiters: Array<() => void> = [];
|
|
373
|
+
const notifyCapacityChange = (): void => {
|
|
374
|
+
const current = wakeWaiters;
|
|
375
|
+
wakeWaiters = [];
|
|
376
|
+
for (const resolveWake of current) resolveWake();
|
|
377
|
+
};
|
|
378
|
+
const waitUntilCapacityOrDelay = async (
|
|
379
|
+
delayMs: number,
|
|
380
|
+
signal: AbortSignal,
|
|
381
|
+
): Promise<void> => {
|
|
382
|
+
if (signal.aborted) return;
|
|
383
|
+
const wakeController = new AbortController();
|
|
384
|
+
const onParentAbort = (): void => wakeController.abort(signal.reason);
|
|
385
|
+
signal.addEventListener('abort', onParentAbort, { once: true });
|
|
386
|
+
const onWake = (): void => {
|
|
387
|
+
if (!wakeController.signal.aborted) wakeController.abort('capacity_changed');
|
|
388
|
+
};
|
|
389
|
+
wakeWaiters.push(onWake);
|
|
390
|
+
try {
|
|
391
|
+
await this.sleep(delayMs, wakeController.signal);
|
|
392
|
+
} catch (error) {
|
|
393
|
+
if (wakeController.signal.reason === 'capacity_changed' && !signal.aborted) return;
|
|
394
|
+
throw error;
|
|
395
|
+
} finally {
|
|
396
|
+
signal.removeEventListener('abort', onParentAbort);
|
|
397
|
+
wakeWaiters = wakeWaiters.filter((fn) => fn !== onWake);
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
|
|
366
401
|
try {
|
|
367
402
|
await this.dependencies.executor.reconcileOrphans(runnerId, session.sessionId);
|
|
368
|
-
await this.
|
|
403
|
+
await this.recoverInterruptedAttempts(
|
|
369
404
|
journal,
|
|
370
405
|
runnerId,
|
|
371
406
|
session.sessionId,
|
|
@@ -374,22 +409,107 @@ export class ShipRunner {
|
|
|
374
409
|
while (
|
|
375
410
|
!this.drainRequested && !this.sessionInvalid && !sessionLifecycle.signal.aborted
|
|
376
411
|
) {
|
|
377
|
-
|
|
412
|
+
if (!budget.canClaimAnother()) {
|
|
413
|
+
if (options.once) break;
|
|
414
|
+
await waitUntilCapacityOrDelay(this.config.pollIntervalMs, sessionLifecycle.signal);
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const claimStartedAt = this.now();
|
|
418
|
+
const claim = await this.dependencies.api.claimJob({
|
|
419
|
+
claimRequestId: plugins.crypto.randomUUID(),
|
|
420
|
+
runnerId,
|
|
421
|
+
sessionId: session.sessionId,
|
|
422
|
+
availableResources: budget.available(),
|
|
423
|
+
}, sessionLifecycle.signal);
|
|
424
|
+
const claimElapsedMs = Math.max(0, this.now() - claimStartedAt);
|
|
425
|
+
if (claim.lease === null) {
|
|
426
|
+
lastResult = { claimed: false };
|
|
427
|
+
if (options.once) break;
|
|
428
|
+
const waitMs = Math.max(this.config.pollIntervalMs, claim.retryAfterMs ?? 0);
|
|
429
|
+
await waitUntilCapacityOrDelay(waitMs, sessionLifecycle.signal);
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const unvalidatedIdentity = safeIdentityFromUnknown(
|
|
378
434
|
runnerId,
|
|
379
435
|
session.sessionId,
|
|
380
|
-
|
|
381
|
-
discovery.limits,
|
|
382
|
-
journal,
|
|
383
|
-
sessionLifecycle.signal,
|
|
436
|
+
claim.lease,
|
|
384
437
|
);
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
await
|
|
438
|
+
let lease: IOciJobLease;
|
|
439
|
+
try {
|
|
440
|
+
lease = await validator.validateExecutableLease(claim.lease);
|
|
441
|
+
} catch (error) {
|
|
442
|
+
if (!unvalidatedIdentity) throw error;
|
|
443
|
+
this.dependencies.onMessage?.(
|
|
444
|
+
`Refused lease ${unvalidatedIdentity.jobId}/${unvalidatedIdentity.attemptId}: ${
|
|
445
|
+
error instanceof Error ? error.message : 'invalid lease'
|
|
446
|
+
}`,
|
|
447
|
+
);
|
|
448
|
+
await this.abandon(unvalidatedIdentity, 'runner_error');
|
|
449
|
+
lastResult = { claimed: true, status: 'system_error' };
|
|
450
|
+
if (options.once) break;
|
|
451
|
+
continue;
|
|
388
452
|
}
|
|
453
|
+
|
|
454
|
+
if (options.once) {
|
|
455
|
+
budget.reserve(lease.attemptId, lease);
|
|
456
|
+
try {
|
|
457
|
+
lastResult = await this.executeValidatedLease(
|
|
458
|
+
runnerId,
|
|
459
|
+
session.sessionId,
|
|
460
|
+
claim,
|
|
461
|
+
claimElapsedMs,
|
|
462
|
+
lease,
|
|
463
|
+
discovery.limits,
|
|
464
|
+
journal,
|
|
465
|
+
sessionLifecycle.signal,
|
|
466
|
+
);
|
|
467
|
+
} finally {
|
|
468
|
+
budget.release(lease.attemptId);
|
|
469
|
+
}
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
budget.reserve(lease.attemptId, lease);
|
|
474
|
+
const attemptTask = (async (): Promise<void> => {
|
|
475
|
+
try {
|
|
476
|
+
lastResult = await this.executeValidatedLease(
|
|
477
|
+
runnerId,
|
|
478
|
+
session.sessionId,
|
|
479
|
+
claim,
|
|
480
|
+
claimElapsedMs,
|
|
481
|
+
lease,
|
|
482
|
+
discovery.limits,
|
|
483
|
+
journal,
|
|
484
|
+
sessionLifecycle.signal,
|
|
485
|
+
);
|
|
486
|
+
} catch (error) {
|
|
487
|
+
if (error instanceof ResourceCleanupError) {
|
|
488
|
+
this.sessionInvalid = true;
|
|
489
|
+
operationError ??= error;
|
|
490
|
+
if (!sessionLifecycle.signal.aborted) sessionLifecycle.abort('runner_shutdown');
|
|
491
|
+
} else if (!sessionLifecycle.signal.aborted) {
|
|
492
|
+
operationError ??= error;
|
|
493
|
+
sessionLifecycle.abort('runner_shutdown');
|
|
494
|
+
}
|
|
495
|
+
} finally {
|
|
496
|
+
budget.release(lease.attemptId);
|
|
497
|
+
inFlight.delete(lease.attemptId);
|
|
498
|
+
notifyCapacityChange();
|
|
499
|
+
}
|
|
500
|
+
})();
|
|
501
|
+
inFlight.set(lease.attemptId, attemptTask);
|
|
502
|
+
}
|
|
503
|
+
if (inFlight.size > 0) {
|
|
504
|
+
await Promise.allSettled(inFlight.values());
|
|
389
505
|
}
|
|
390
506
|
} catch (error) {
|
|
391
507
|
if (error instanceof ResourceCleanupError) this.sessionInvalid = true;
|
|
392
|
-
operationError
|
|
508
|
+
operationError ??= error;
|
|
509
|
+
if (!sessionLifecycle.signal.aborted) sessionLifecycle.abort('runner_shutdown');
|
|
510
|
+
if (inFlight.size > 0) {
|
|
511
|
+
await Promise.allSettled(inFlight.values());
|
|
512
|
+
}
|
|
393
513
|
} finally {
|
|
394
514
|
if (!sessionLifecycle.signal.aborted) sessionLifecycle.abort('runner_shutdown');
|
|
395
515
|
options.signal?.removeEventListener('abort', externalAbort);
|
|
@@ -409,66 +529,41 @@ export class ShipRunner {
|
|
|
409
529
|
return lastResult;
|
|
410
530
|
}
|
|
411
531
|
|
|
412
|
-
private async
|
|
532
|
+
private async recoverInterruptedAttempts(
|
|
413
533
|
journal: AttemptStateStore,
|
|
414
534
|
runnerId: string,
|
|
415
535
|
sessionId: string,
|
|
416
536
|
signal: AbortSignal,
|
|
417
537
|
): Promise<void> {
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
538
|
+
const interruptedEntries = await journal.loadAll();
|
|
539
|
+
for (const interrupted of interruptedEntries) {
|
|
540
|
+
try {
|
|
541
|
+
await this.dependencies.api.recoverAttempt({
|
|
542
|
+
recoveryRequestId: plugins.crypto.randomUUID(),
|
|
543
|
+
runnerId,
|
|
544
|
+
currentSessionId: sessionId,
|
|
545
|
+
previousSessionId: interrupted.previousSessionId,
|
|
546
|
+
jobId: interrupted.jobId,
|
|
547
|
+
attemptId: interrupted.attemptId,
|
|
548
|
+
reason: 'runner_restarted',
|
|
549
|
+
}, signal);
|
|
550
|
+
} catch (error) {
|
|
551
|
+
if (!isDefinitiveConflict(error)) throw error;
|
|
552
|
+
}
|
|
553
|
+
await journal.remove(interrupted.attemptId);
|
|
432
554
|
}
|
|
433
|
-
await journal.clear();
|
|
434
555
|
}
|
|
435
556
|
|
|
436
|
-
private async
|
|
557
|
+
private async executeValidatedLease(
|
|
437
558
|
runnerId: string,
|
|
438
559
|
sessionId: string,
|
|
439
|
-
|
|
560
|
+
claim: IClaimJobResponse,
|
|
561
|
+
claimElapsedMs: number,
|
|
562
|
+
lease: IOciJobLease,
|
|
440
563
|
protocolLimits: IProtocolLimits,
|
|
441
564
|
journal: AttemptStateStore,
|
|
442
565
|
runnerSignal: AbortSignal,
|
|
443
566
|
): Promise<IRunnerRunResult> {
|
|
444
|
-
const claimStartedAt = this.now();
|
|
445
|
-
const claim = await this.dependencies.api.claimJob({
|
|
446
|
-
claimRequestId: plugins.crypto.randomUUID(),
|
|
447
|
-
runnerId,
|
|
448
|
-
sessionId,
|
|
449
|
-
}, runnerSignal);
|
|
450
|
-
const claimElapsedMs = Math.max(0, this.now() - claimStartedAt);
|
|
451
|
-
if (claim.lease === null) {
|
|
452
|
-
if (claim.retryAfterMs && claim.retryAfterMs > this.config.pollIntervalMs) {
|
|
453
|
-
await this.sleep(claim.retryAfterMs - this.config.pollIntervalMs, runnerSignal);
|
|
454
|
-
}
|
|
455
|
-
return { claimed: false };
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
const unvalidatedIdentity = safeIdentityFromUnknown(runnerId, sessionId, claim.lease);
|
|
459
|
-
let lease: IOciJobLease;
|
|
460
|
-
try {
|
|
461
|
-
lease = await validator.validateExecutableLease(claim.lease);
|
|
462
|
-
} catch (error) {
|
|
463
|
-
if (!unvalidatedIdentity) throw error;
|
|
464
|
-
this.dependencies.onMessage?.(
|
|
465
|
-
`Refused lease ${unvalidatedIdentity.jobId}/${unvalidatedIdentity.attemptId}: ${
|
|
466
|
-
error instanceof Error ? error.message : 'invalid lease'
|
|
467
|
-
}`,
|
|
468
|
-
);
|
|
469
|
-
await this.abandon(unvalidatedIdentity, 'runner_error');
|
|
470
|
-
return { claimed: true, status: 'system_error' };
|
|
471
|
-
}
|
|
472
567
|
|
|
473
568
|
const identity = identityFor(runnerId, sessionId, lease);
|
|
474
569
|
const guard = new LeaseGuard(protocolLimits, this.config.leaseSafetyMarginMs);
|
|
@@ -549,7 +644,7 @@ export class ShipRunner {
|
|
|
549
644
|
}
|
|
550
645
|
|
|
551
646
|
try {
|
|
552
|
-
await journal.
|
|
647
|
+
await journal.add({
|
|
553
648
|
previousSessionId: sessionId,
|
|
554
649
|
jobId: lease.jobId,
|
|
555
650
|
attemptId: lease.attemptId,
|
|
@@ -591,7 +686,7 @@ export class ShipRunner {
|
|
|
591
686
|
);
|
|
592
687
|
}
|
|
593
688
|
if (isDefinitiveConflict(error)) {
|
|
594
|
-
await journal.
|
|
689
|
+
await journal.remove(identity.attemptId);
|
|
595
690
|
return { claimed: true, status: 'system_error' };
|
|
596
691
|
}
|
|
597
692
|
if (guard.controller.signal.reason === 'runner_shutdown') {
|
|
@@ -656,7 +751,8 @@ export class ShipRunner {
|
|
|
656
751
|
heartbeatState,
|
|
657
752
|
heartbeatLifecycle.signal,
|
|
658
753
|
);
|
|
659
|
-
const
|
|
754
|
+
const startedAtMs = this.now();
|
|
755
|
+
const startedAt = new Date(startedAtMs).toISOString();
|
|
660
756
|
const redactionSet = SecretRedactionSet.forLease(lease);
|
|
661
757
|
const logs = new LogSender(
|
|
662
758
|
this.dependencies.api,
|
|
@@ -668,6 +764,7 @@ export class ShipRunner {
|
|
|
668
764
|
);
|
|
669
765
|
let stagingWorkspace: IPreparedWorkspace | undefined;
|
|
670
766
|
let runtimeWorkspace: Awaited<ReturnType<IJobExecutor['execute']>>['workspace'];
|
|
767
|
+
let executionResourceUsage: ICompleteJobRequest['resourceUsage'] | undefined;
|
|
671
768
|
let status: TJobCompletionStatus = 'system_error';
|
|
672
769
|
let exitCode: number | null = null;
|
|
673
770
|
let systemFailure: unknown;
|
|
@@ -753,6 +850,9 @@ export class ShipRunner {
|
|
|
753
850
|
onLog: (stream, data) => logs.write(stream, data),
|
|
754
851
|
});
|
|
755
852
|
runtimeWorkspace = result.workspace;
|
|
853
|
+
if (result.resourceUsage) {
|
|
854
|
+
executionResourceUsage = result.resourceUsage;
|
|
855
|
+
}
|
|
756
856
|
if (isImageBuild) {
|
|
757
857
|
exitCode = null;
|
|
758
858
|
if (result.stopReason) {
|
|
@@ -914,7 +1014,7 @@ export class ShipRunner {
|
|
|
914
1014
|
return { claimed: true, status: 'system_error' };
|
|
915
1015
|
}
|
|
916
1016
|
if (fenceReason === 'lease_lost' || fenceReason === 'lease_expired') {
|
|
917
|
-
if (guard.definitive) await journal.
|
|
1017
|
+
if (guard.definitive) await journal.remove(identity.attemptId);
|
|
918
1018
|
else this.sessionInvalid = true;
|
|
919
1019
|
if (heartbeatState.terminalError !== undefined) throw heartbeatState.terminalError;
|
|
920
1020
|
return { claimed: true, status };
|
|
@@ -935,26 +1035,35 @@ export class ShipRunner {
|
|
|
935
1035
|
exitCode = null;
|
|
936
1036
|
}
|
|
937
1037
|
|
|
1038
|
+
const completedAtMs = this.now();
|
|
938
1039
|
const completion: ICompleteJobRequest = {
|
|
939
1040
|
completionRequestId: plugins.crypto.randomUUID(),
|
|
940
1041
|
identity,
|
|
941
1042
|
status,
|
|
942
1043
|
exitCode,
|
|
943
1044
|
startedAt,
|
|
944
|
-
completedAt: new Date(
|
|
1045
|
+
completedAt: new Date(completedAtMs).toISOString(),
|
|
945
1046
|
...(status === 'canceled'
|
|
946
1047
|
? { cancellationRequestId: heartbeatState.cancellation!.requestId }
|
|
947
1048
|
: {}),
|
|
948
1049
|
...(status === 'system_error' || systemFailure !== undefined
|
|
949
1050
|
? { error: genericSystemError }
|
|
950
1051
|
: {}),
|
|
1052
|
+
...(executionResourceUsage
|
|
1053
|
+
? {
|
|
1054
|
+
resourceUsage: {
|
|
1055
|
+
...executionResourceUsage,
|
|
1056
|
+
durationMs: executionResourceUsage.durationMs ?? Math.max(0, Math.floor(completedAtMs - startedAtMs)),
|
|
1057
|
+
},
|
|
1058
|
+
}
|
|
1059
|
+
: {}),
|
|
951
1060
|
};
|
|
952
1061
|
try {
|
|
953
1062
|
await this.dependencies.api.completeJob(completion);
|
|
954
|
-
await journal.
|
|
1063
|
+
await journal.remove(identity.attemptId);
|
|
955
1064
|
} catch (error) {
|
|
956
1065
|
if (isDefinitiveConflict(error)) {
|
|
957
|
-
await journal.
|
|
1066
|
+
await journal.remove(identity.attemptId);
|
|
958
1067
|
} else {
|
|
959
1068
|
throw error;
|
|
960
1069
|
}
|
|
@@ -1070,12 +1179,13 @@ export class ShipRunner {
|
|
|
1070
1179
|
private async runnerHeartbeatLoop(
|
|
1071
1180
|
runnerId: string,
|
|
1072
1181
|
sessionId: string,
|
|
1182
|
+
budget: RunnerResourceBudget,
|
|
1073
1183
|
lifecycle: AbortController,
|
|
1074
1184
|
): Promise<void> {
|
|
1075
1185
|
while (!lifecycle.signal.aborted) {
|
|
1076
1186
|
try {
|
|
1077
1187
|
const heartbeat = await this.dependencies.api.heartbeatRunner(
|
|
1078
|
-
{ runnerId, sessionId },
|
|
1188
|
+
{ runnerId, sessionId, availableResources: budget.available() },
|
|
1079
1189
|
lifecycle.signal,
|
|
1080
1190
|
);
|
|
1081
1191
|
this.drainRequested ||= heartbeat.drainRequested;
|
|
@@ -1115,7 +1225,7 @@ export class ShipRunner {
|
|
|
1115
1225
|
} catch (error) {
|
|
1116
1226
|
if (!isDefinitiveConflict(error)) throw error;
|
|
1117
1227
|
}
|
|
1118
|
-
await journal?.
|
|
1228
|
+
await journal?.remove(identity.attemptId);
|
|
1119
1229
|
}
|
|
1120
1230
|
|
|
1121
1231
|
private async applyCredential(credential: IRunnerCredential): Promise<void> {
|