hammerkit 2.1.0-next.13 → 2.1.0-next.15

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.
@@ -10,6 +10,7 @@ const path_1 = require("path");
10
10
  const readline_1 = require("readline");
11
11
  const resolve_git_source_1 = require("../includes/resolve-git-source");
12
12
  const git_checkout_1 = require("./git-checkout");
13
+ const runs_1 = require("./runs");
13
14
  const run_plan_1 = require("./run-plan");
14
15
  const work_network_1 = require("../planner/work-network");
15
16
  const execute_docker_1 = require("../executer/execute-docker");
@@ -22,8 +23,12 @@ function isGitDirectory(segment) {
22
23
  return name === '.git' || name === 'git~1';
23
24
  }
24
25
  // A cancelled run gets this long to stop its tasks and remove its containers, after that
25
- // its process is killed
26
+ // its process is killed. A server without a store waits for it on close (CLOSE_TIMEOUT in
27
+ // runs.ts bounds only a server that detaches its runs)
26
28
  const STOP_GRACE_MS = 15000;
29
+ // A run the server leaves to the next one gets this long to stop its tasks on this machine: the
30
+ // rest of what CLOSE_TIMEOUT gives it is for its execution to end (its directory, its volumes)
31
+ const DETACH_GRACE_MS = runs_1.CLOSE_TIMEOUT - 5000;
27
32
  // what is kept of the error output of a process that died without saying why
28
33
  const MAX_STDERR = 2 * 1024;
29
34
  /**
@@ -370,11 +375,14 @@ class HostExecutor {
370
375
  : {}),
371
376
  ...(placed ? { kubernetes: placed.kubernetes } : {}),
372
377
  ...(!placed && this.options.capabilityRuntime ? { capabilityRuntime: this.options.capabilityRuntime } : {}),
373
- }, published, { ...(0, run_source_environment_1.withRunSource)(this.processEnvironment(home, cache), run.source), ...placed?.env }, context, abortCtrl.signal);
378
+ }, published, { ...(0, run_source_environment_1.withRunSource)(this.processEnvironment(home, cache), run.source), ...placed?.env }, context, abortCtrl.signal, !!placed);
374
379
  }
375
380
  finally {
376
381
  if (placement && placed) {
377
- await placement.release(run, context);
382
+ // a run that resumes with the next server finds its Jobs and services where they are
383
+ if (!(0, runs_1.isDetached)(context)) {
384
+ await placement.release(run, context);
385
+ }
378
386
  }
379
387
  else if (run.up && !context.keep?.()) {
380
388
  // the services are gone with the process, their volumes stay only while the run sleeps
@@ -445,7 +453,10 @@ class HostExecutor {
445
453
  // engine stops its tasks and removes its containers; a signal on Windows would end the
446
454
  // process at once and leave its tasks running in the directory of the run. The process is
447
455
  // killed if it has not stopped after the grace period.
448
- runInProcess(command, input, cwd, env, context, signal) {
456
+ // A run that the server leaves to the next one (RunContext.detached) and whose tasks are on
457
+ // a cluster: the process is killed at once, so its Jobs and services stay. One whose tasks
458
+ // are on this machine stops them as a cancel does, in less time.
459
+ runInProcess(command, input, cwd, env, context, signal, onCluster) {
449
460
  return new Promise((resolvePromise, reject) => {
450
461
  const child = (0, child_process_1.spawn)(command[0], command.slice(1), {
451
462
  cwd,
@@ -460,12 +471,18 @@ class HostExecutor {
460
471
  let stderr = '';
461
472
  let killTimer;
462
473
  const stop = () => {
474
+ const detaching = (0, runs_1.isDetached)(context);
475
+ if (detaching && onCluster) {
476
+ child.kill('SIGKILL');
477
+ return;
478
+ }
463
479
  child.stdin.write('cancel\n', (e) => {
464
480
  if (e) {
465
481
  child.kill();
466
482
  }
467
483
  });
468
- killTimer = setTimeout(() => child.kill('SIGKILL'), this.options.stopGraceMs ?? STOP_GRACE_MS);
484
+ const grace = this.options.stopGraceMs ?? STOP_GRACE_MS;
485
+ killTimer = setTimeout(() => child.kill('SIGKILL'), detaching ? Math.min(grace, DETACH_GRACE_MS) : grace);
469
486
  killTimer.unref();
470
487
  };
471
488
  if (signal.aborted) {
@@ -19,6 +19,7 @@ const version_1 = require("../version");
19
19
  const executor_1 = require("./executor");
20
20
  const kept_claims_1 = require("../kubernetes/kept-claims");
21
21
  const units_1 = require("../utils/units");
22
+ const runs_1 = require("./runs");
22
23
  const server_config_1 = require("./server-config");
23
24
  // the name of the quota and limit range the server keeps in a caller's namespace
24
25
  const POLICY_NAME = 'hammerkit';
@@ -270,8 +271,11 @@ class KubernetesExecutor {
270
271
  // per caller namespace, what changes objects of the whole namespace: its setup, the
271
272
  // removal of what a stopped server left, the sweeps of kept claims. The last one waiting.
272
273
  this.queues = new Map();
273
- // per caller namespace, the runs that execute in it and mount kept claims: by run label,
274
- // their volume scope
274
+ // per caller namespace, the runs that execute in it: by run label, the volume scope of the
275
+ // kept claims they mount, if any. Every run that executes is here, not only those that mount
276
+ // kept claims: a tidy of the namespace (after a release that failed, or the first after a
277
+ // restart) leaves the objects of all of them, a resumed run's too once it no longer counts
278
+ // as resuming
275
279
  this.active = new Map();
276
280
  // per caller namespace, when its kept claims are next looked at
277
281
  this.sweeps = new Map();
@@ -285,6 +289,9 @@ class KubernetesExecutor {
285
289
  this.serviceRuns = new Map();
286
290
  this.serviceRunsSwept = false;
287
291
  this.asleep = new Set();
292
+ // the runs whose execution got its place on the cluster, until it ended: their release
293
+ // removes what they made
294
+ this.placed = new Set();
288
295
  this.access = getClusterAccess(config.backend, processEnvs);
289
296
  this.instance = (0, kubernetes_instance_1.createKubernetesInstances)({
290
297
  type: 'kubernetes',
@@ -316,6 +323,7 @@ class KubernetesExecutor {
316
323
  log: options.log,
317
324
  });
318
325
  }
326
+ this.resuming = new ResumingRuns(config.backend.namespacePrefix, (run) => this.namespaceOf(run));
319
327
  this.host = new executor_1.HostExecutor({ ...options, placement: this.placement() });
320
328
  }
321
329
  // what the namespaces of the server's callers and service runs request and use
@@ -345,6 +353,38 @@ class KubernetesExecutor {
345
353
  });
346
354
  }
347
355
  async execute(run, context) {
356
+ // what a run that resumes left is this execution's from now: the end of a run that got its
357
+ // place on the cluster (release) removes it, the end of one that did not removes it here
358
+ const left = this.resuming.has(run);
359
+ try {
360
+ await this.executeOn(run, context);
361
+ }
362
+ finally {
363
+ const placed = this.placed.delete(run.id);
364
+ if (left && !placed && !(0, runs_1.isDetached)(context)) {
365
+ await this.removeLeft(run);
366
+ }
367
+ }
368
+ }
369
+ // What a run left on the cluster, that no release removes
370
+ async removeLeft(run) {
371
+ const say = (text) => this.log?.(`run=${run.id} ${text}`);
372
+ this.resuming.forget(run);
373
+ try {
374
+ if (!run.up) {
375
+ await this.removeRun(run, getCallerNamespace(this.config.backend.namespacePrefix, run.owner), say);
376
+ return;
377
+ }
378
+ // a namespace is kept only for a run that has one from start to end
379
+ const namespace = this.namespaceOf(run);
380
+ this.asleep.delete(namespace);
381
+ await this.enqueue(namespace, () => this.removeServiceRun(namespace, say));
382
+ }
383
+ catch (e) {
384
+ say(`what the run left could not be removed: ${(0, log_1.getErrorMessage)(e)}`);
385
+ }
386
+ }
387
+ async executeOn(run, context) {
348
388
  if (run.up) {
349
389
  if (run.source.type === 'sandbox') {
350
390
  throw new Error('on a cluster a service run starts from a branch, push the sandbox and start it from there');
@@ -352,6 +392,8 @@ class KubernetesExecutor {
352
392
  // in a namespace of its own, apart from the caller's runs and their claims
353
393
  return this.host.execute(run, context);
354
394
  }
395
+ // it executes: its objects are kept from a tidy as those of any run that executes
396
+ this.resuming.forget(run);
355
397
  const namespace = getCallerNamespace(this.config.backend.namespacePrefix, run.owner);
356
398
  const label = (0, job_placement_1.getRunLabelValue)(run.id);
357
399
  let runs = this.active.get(namespace);
@@ -359,9 +401,7 @@ class KubernetesExecutor {
359
401
  runs = new Map();
360
402
  this.active.set(namespace, runs);
361
403
  }
362
- if (this.holds(run)) {
363
- runs.set(label, getVolumeScope(run));
364
- }
404
+ runs.set(label, this.holds(run) ? getVolumeScope(run) : undefined);
365
405
  // the caller is promised its share while the run executes
366
406
  this.quotas?.enter(namespace, run.id);
367
407
  try {
@@ -372,9 +412,12 @@ class KubernetesExecutor {
372
412
  if (runs.size === 0 && this.active.get(namespace) === runs) {
373
413
  this.active.delete(namespace);
374
414
  }
375
- await this.quotas
376
- ?.leave(namespace, run.id)
377
- .catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
415
+ // a run the next server resumes still has its Jobs, under the quota it had
416
+ if (!(0, runs_1.isDetached)(context)) {
417
+ await this.quotas
418
+ ?.leave(namespace, run.id)
419
+ .catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
420
+ }
378
421
  }
379
422
  }
380
423
  // A service run moves to another commit in its namespace: unchanged services stay, the
@@ -390,25 +433,31 @@ class KubernetesExecutor {
390
433
  }
391
434
  /**
392
435
  * A service run that slept with its volumes ended without waking: its namespace goes, with
393
- * the claims in it.
436
+ * the claims in it. A run that resumed after a restart ended: what an execution before the
437
+ * restart left goes, as no execution may have removed it (one cancelled in the queue).
394
438
  */
395
439
  async forget(run) {
396
- if (!run.up) {
397
- return;
398
- }
399
- // a namespace is kept only for a run that has one from start to end
400
- const namespace = getServiceRunNamespace(this.config.backend.namespacePrefix, run.id);
401
- this.asleep.delete(namespace);
402
- await this.enqueue(namespace, () => this.removeServiceRun(namespace, (text) => this.log?.(`run=${run.id} ${text}`)));
440
+ await this.removeLeft(run);
403
441
  }
404
442
  /**
405
- * The service runs of a previous start that sleep with their volumes: their namespaces stay,
406
- * without what runs in them (a server that stopped without putting the run to sleep left
407
- * it). The namespaces of any other service run go.
443
+ * The runs of a previous start that are not over: the service runs that sleep with their
444
+ * volumes, whose namespaces stay without what runs in them (a server that stopped without
445
+ * putting the run to sleep left it), and the runs that resume (queued), whose Jobs and
446
+ * services stay where they are until they execute again or end. The namespaces of any other
447
+ * service run go.
408
448
  */
409
449
  restored(runs) {
450
+ for (const run of runs.filter((candidate) => candidate.state === 'queued')) {
451
+ try {
452
+ this.resuming.add(run, this.holds(run) ? getVolumeScope(run) : undefined);
453
+ }
454
+ catch (e) {
455
+ // its objects are not kept from a sweep, the run does not start for nothing
456
+ this.log?.(`what run ${run.id} left on the cluster is not kept for it: ${(0, log_1.getErrorMessage)(e)}`);
457
+ }
458
+ }
410
459
  if (this.keepServiceRuns) {
411
- for (const run of runs.filter((candidate) => candidate.up)) {
460
+ for (const run of runs.filter((candidate) => candidate.up && candidate.state !== 'queued')) {
412
461
  const namespace = getServiceRunNamespace(this.config.backend.namespacePrefix, run.id);
413
462
  this.asleep.add(namespace);
414
463
  this.enqueue(namespace, () => removeRunObjects(this.instance, namespace, job_placement_1.RUN_LABEL, RELEASE_TIMEOUT, { claims: false })).catch((e) => this.log?.(`what runs in the namespace ${namespace} of run ${run.id} could not be removed: ${(0, log_1.getErrorMessage)(e)}`));
@@ -479,8 +528,12 @@ class KubernetesExecutor {
479
528
  if (this.keep === 0) {
480
529
  return new Set();
481
530
  }
482
- return new Set([...(this.active.get(namespace) ?? new Map()).entries()]
483
- .filter(([label]) => label !== except)
531
+ return new Set([
532
+ ...(this.active.get(namespace) ?? new Map()).entries(),
533
+ // a run that resumes mounts them again
534
+ ...this.resuming.scopes(namespace),
535
+ ]
536
+ .filter(([label, scope]) => label !== except && scope !== undefined)
484
537
  .map(([, scope]) => scope));
485
538
  }
486
539
  // What changes objects of a whole namespace happens one at a time, not what its runs do
@@ -506,6 +559,9 @@ class KubernetesExecutor {
506
559
  }
507
560
  this.serviceRuns.set(namespace, (this.serviceRuns.get(namespace) ?? 0) + 1);
508
561
  this.asleep.delete(namespace);
562
+ // its execution holds the namespace now, and its release removes it
563
+ this.resuming.forget(run);
564
+ this.placed.add(run.id);
509
565
  this.sweepServiceRuns();
510
566
  const up = run.up;
511
567
  // after what a stop of its sleep left in it went (see restored)
@@ -524,7 +580,7 @@ class KubernetesExecutor {
524
580
  // its pods take room the callers can't borrow
525
581
  await this.quotas?.serviceRun(namespace, this.config.runs.up.quota ?? this.config.backend.quota);
526
582
  context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
527
- return { kubernetes: this.environmentOf(run, namespace), env: this.access.env };
583
+ return { kubernetes: this.environmentOf(run, namespace, context), env: this.access.env };
528
584
  }
529
585
  const label = (0, job_placement_1.getRunLabelValue)(run.id);
530
586
  let capabilitiesProblem;
@@ -537,7 +593,9 @@ class KubernetesExecutor {
537
593
  // release failed: the objects of a run that does not execute. One that starts
538
594
  // after this waits for it here before it makes any.
539
595
  if (!this.tidy.has(namespace)) {
540
- const running = [...new Set([label, ...(this.active.get(namespace)?.keys() ?? [])])];
596
+ const running = [
597
+ ...new Set([label, ...(this.active.get(namespace)?.keys() ?? []), ...this.resuming.labels(namespace)]),
598
+ ];
541
599
  await removeRunObjects(this.instance, namespace, `${job_placement_1.RUN_LABEL},${job_placement_1.RUN_LABEL} notin (${running.join(',')})`, RELEASE_TIMEOUT);
542
600
  this.tidy.add(namespace);
543
601
  }
@@ -557,8 +615,9 @@ class KubernetesExecutor {
557
615
  // tasks run
558
616
  await this.quotas?.apply(namespace);
559
617
  context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
618
+ this.placed.add(run.id);
560
619
  return {
561
- kubernetes: this.environmentOf(run, namespace, work, capabilitiesProblem),
620
+ kubernetes: this.environmentOf(run, namespace, context, work, capabilitiesProblem),
562
621
  env: this.access.env,
563
622
  };
564
623
  },
@@ -599,40 +658,35 @@ class KubernetesExecutor {
599
658
  }
600
659
  return;
601
660
  }
602
- try {
603
- const selector = `${job_placement_1.RUN_LABEL}=${(0, job_placement_1.getRunLabelValue)(run.id)}`;
604
- await removeRunObjects(this.instance, namespace, selector, RELEASE_TIMEOUT);
605
- }
606
- catch (e) {
607
- // the next run removes what is left
608
- this.tidy.delete(namespace);
609
- context.emit({
610
- type: 'log',
611
- task: 'hammerkit',
612
- stream: 'stderr',
613
- text: `what the run made in ${namespace} could not be removed: ${(0, log_1.getErrorMessage)(e)}`,
614
- });
615
- }
616
- if (this.keep > 0) {
617
- try {
618
- await (0, kept_claims_1.expireKeptClaims)(this.instance, namespace, (0, job_placement_1.getRunLabelValue)(run.id), new Date(Date.now() + this.keep));
619
- }
620
- catch (e) {
621
- // without an expiry, the next look at the namespace removes them
622
- context.emit({
623
- type: 'log',
624
- task: 'hammerkit',
625
- stream: 'stderr',
626
- text: `the claims the run kept in ${namespace} could not be given their expiry: ${(0, log_1.getErrorMessage)(e)}`,
627
- });
628
- }
629
- this.sweepLater(namespace, this.keep);
630
- }
661
+ await this.removeRun(run, namespace, (text) => context.emit({ type: 'log', task: 'hammerkit', stream: 'stderr', text }));
631
662
  },
632
663
  };
633
664
  }
665
+ // The end of a run of tasks: what it made in the caller's namespace goes, but for the claims
666
+ // it keeps, which expire from now
667
+ async removeRun(run, namespace, say) {
668
+ try {
669
+ const selector = `${job_placement_1.RUN_LABEL}=${(0, job_placement_1.getRunLabelValue)(run.id)}`;
670
+ await removeRunObjects(this.instance, namespace, selector, RELEASE_TIMEOUT);
671
+ }
672
+ catch (e) {
673
+ // the next run removes what is left
674
+ this.tidy.delete(namespace);
675
+ say(`what the run made in ${namespace} could not be removed: ${(0, log_1.getErrorMessage)(e)}`);
676
+ }
677
+ if (this.keep > 0) {
678
+ try {
679
+ await (0, kept_claims_1.expireKeptClaims)(this.instance, namespace, (0, job_placement_1.getRunLabelValue)(run.id), new Date(Date.now() + this.keep));
680
+ }
681
+ catch (e) {
682
+ // without an expiry, the next look at the namespace removes them
683
+ say(`the claims the run kept in ${namespace} could not be given their expiry: ${(0, log_1.getErrorMessage)(e)}`);
684
+ }
685
+ this.sweepLater(namespace, this.keep);
686
+ }
687
+ }
634
688
  // The cluster as the process of a run sees it: the namespace its tasks and services go in.
635
- environmentOf(run, namespace, work, capabilitiesProblem) {
689
+ environmentOf(run, namespace, context, work, capabilitiesProblem) {
636
690
  const backend = this.config.backend;
637
691
  const openGate = (0, network_gate_1.getOpenNetworkGate)(backend);
638
692
  return {
@@ -656,6 +710,8 @@ class KubernetesExecutor {
656
710
  ...(this.keep > 0 && !run.up ? { volumeScope: getVolumeScope(run) } : {}),
657
711
  ...(run.up ? { serviceRun: true } : {}),
658
712
  ...(openGate ? { openGate } : {}),
713
+ // what the execution before a restart left, its tasks and services take over
714
+ ...(context.resumed ? { resumed: context.resumed } : {}),
659
715
  ...(backend.capabilities
660
716
  ? {
661
717
  capabilities: {
@@ -686,6 +742,7 @@ class KubernetesExecutor {
686
742
  isCallerNamespace(this.config.backend.namespacePrefix, name) &&
687
743
  !this.serviceRuns.has(name) &&
688
744
  !this.asleep.has(name) &&
745
+ !this.resuming.keeps(name) &&
689
746
  !namespace.metadata?.deletionTimestamp) {
690
747
  this.log?.(`removing the namespace ${name} of a service run from before the server started`);
691
748
  await removeServiceRunNamespace(this.instance, name, 0);
@@ -747,6 +804,54 @@ class KubernetesExecutor {
747
804
  }
748
805
  }
749
806
  exports.KubernetesExecutor = KubernetesExecutor;
807
+ // What the runs that resume after a restart left on the cluster, which the sweeps leave: the
808
+ // namespaces of service runs, and per caller namespace the runs by run label, with the volume
809
+ // scope of the kept claims they mount
810
+ class ResumingRuns {
811
+ constructor(prefix, namespaceOf) {
812
+ this.prefix = prefix;
813
+ this.namespaceOf = namespaceOf;
814
+ this.serviceRuns = new Set();
815
+ this.runs = new Map();
816
+ }
817
+ add(run, scope) {
818
+ if (run.up) {
819
+ this.serviceRuns.add(this.namespaceOf(run));
820
+ return;
821
+ }
822
+ const namespace = getCallerNamespace(this.prefix, run.owner);
823
+ const runs = this.runs.get(namespace) ?? new Map();
824
+ runs.set((0, job_placement_1.getRunLabelValue)(run.id), scope);
825
+ this.runs.set(namespace, runs);
826
+ }
827
+ has(run) {
828
+ return run.up
829
+ ? this.serviceRuns.has(this.namespaceOf(run))
830
+ : !!this.runs.get(getCallerNamespace(this.prefix, run.owner))?.has((0, job_placement_1.getRunLabelValue)(run.id));
831
+ }
832
+ forget(run) {
833
+ if (run.up) {
834
+ this.serviceRuns.delete(this.namespaceOf(run));
835
+ return;
836
+ }
837
+ const namespace = getCallerNamespace(this.prefix, run.owner);
838
+ const runs = this.runs.get(namespace);
839
+ runs?.delete((0, job_placement_1.getRunLabelValue)(run.id));
840
+ if (runs?.size === 0) {
841
+ this.runs.delete(namespace);
842
+ }
843
+ }
844
+ // whether the namespace of a service run is kept for it
845
+ keeps(namespace) {
846
+ return this.serviceRuns.has(namespace);
847
+ }
848
+ labels(namespace) {
849
+ return [...(this.runs.get(namespace)?.keys() ?? [])];
850
+ }
851
+ scopes(namespace) {
852
+ return [...(this.runs.get(namespace)?.entries() ?? [])];
853
+ }
854
+ }
750
855
  const ENFORCE_LABEL = 'pod-security.kubernetes.io/enforce';
751
856
  const AUDIT_LABEL = 'pod-security.kubernetes.io/audit';
752
857
  /**
@@ -113,6 +113,23 @@ class LogIndex {
113
113
  lines(task) {
114
114
  return this.taskLines.get(task) ?? [];
115
115
  }
116
+ // How many lines of the log, or of one task, the first `count` events hold
117
+ linesBefore(count, task) {
118
+ const lines = task !== undefined ? this.lines(task) : undefined;
119
+ let low = 0;
120
+ let high = lines ? lines.length : this.size;
121
+ // the lines are in the order of their events
122
+ while (low < high) {
123
+ const middle = (low + high) >> 1;
124
+ if (this.lineEvent[lines ? lines[middle] : middle] < count) {
125
+ low = middle + 1;
126
+ }
127
+ else {
128
+ high = middle;
129
+ }
130
+ }
131
+ return low;
132
+ }
116
133
  text(events, line) {
117
134
  const event = events[this.lineEvent[line]];
118
135
  return { task: event.task, stream: event.stream, text: event.text.slice(this.lineStart[line], this.lineEnd[line]) };
@@ -145,9 +162,15 @@ class LogIndex {
145
162
  }
146
163
  exports.LogIndex = LogIndex;
147
164
  // The lines of the log a query selects, see LogQuery.
148
- function readLogs(events, index, query, truncated) {
165
+ // `lostAfter`: Run.lostAfter
166
+ function readLogs(events, index, query, truncated, lostAfter) {
149
167
  const selected = query.task !== undefined ? index.lines(query.task) : undefined;
150
168
  const total = selected ? selected.length : index.size;
169
+ const lost = lostAfter !== undefined
170
+ ? index.linesBefore(lostAfter, query.task) + 1
171
+ : query.from !== undefined && query.from > total + 1
172
+ ? total + 1
173
+ : undefined;
151
174
  const at = (position) => (selected ? selected[position] : position);
152
175
  const hits = query.grep !== undefined ? index.matches(events, query.grep, query.ignoreCase) : undefined;
153
176
  const toLine = (position) => ({
@@ -215,6 +238,7 @@ function readLogs(events, index, query, truncated) {
215
238
  total,
216
239
  ...(next !== undefined ? { next } : {}),
217
240
  truncated,
241
+ ...(lost !== undefined ? { lost } : {}),
218
242
  };
219
243
  }
220
244
  exports.readLogs = readLogs;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.workerMain = exports.readInput = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = void 0;
3
+ exports.workerMain = exports.readInput = exports.taskCommands = exports.taskCauses = exports.describeFailures = exports.failedTasks = exports.runTasks = exports.getLogEvent = exports.getEndpointTargets = exports.publishEndpoints = exports.getRunNetwork = exports.workerInputSchema = void 0;
4
4
  const readline_1 = require("readline");
5
5
  const zod_1 = require("zod");
6
6
  const cli_1 = require("../cli");
@@ -153,6 +153,24 @@ function getEndpointTargets(workTree, up, kubernetes) {
153
153
  return targets;
154
154
  }
155
155
  exports.getEndpointTargets = getEndpointTargets;
156
+ // What the run says of a message of the engine: the output of its tasks and services, and
157
+ // what went wrong; where the log of a pod got to with a line of it (RunEventData)
158
+ function getLogEvent(message) {
159
+ if (message.type === 'console') {
160
+ return {
161
+ type: 'log',
162
+ task: message.context.name,
163
+ stream: message.console,
164
+ text: message.message.slice(0, MAX_LOG_TEXT),
165
+ ...(message.cursor ? { cursor: message.cursor } : {}),
166
+ };
167
+ }
168
+ if (message.level === 'error' || message.level === 'warn') {
169
+ return { type: 'log', task: message.context.name, stream: 'stderr', text: message.message.slice(0, MAX_LOG_TEXT) };
170
+ }
171
+ return undefined;
172
+ }
173
+ exports.getLogEvent = getLogEvent;
156
174
  /**
157
175
  * Executes the tasks of a run with the engine of `hammerkit run`. The caller says which
158
176
  * process this is in: it reports what happens through `emit` and stops when `abortCtrl`
@@ -188,21 +206,9 @@ up) {
188
206
  stdoutColumns: 120,
189
207
  };
190
208
  const listener = status.on((message) => {
191
- if (message.type === 'console') {
192
- emit({
193
- type: 'log',
194
- task: message.context.name,
195
- stream: message.console,
196
- text: message.message.slice(0, MAX_LOG_TEXT),
197
- });
198
- }
199
- else if (message.level === 'error' || message.level === 'warn') {
200
- emit({
201
- type: 'log',
202
- task: message.context.name,
203
- stream: 'stderr',
204
- text: message.message.slice(0, MAX_LOG_TEXT),
205
- });
209
+ const event = getLogEvent(message);
210
+ if (event) {
211
+ emit(event);
206
212
  }
207
213
  });
208
214
  try {