hammerkit 2.0.0-next.2 → 2.0.0-next.21

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 (112) hide show
  1. package/dist/cache/backends/registry/registry-client.js +8 -2
  2. package/dist/cache/explain.js +7 -5
  3. package/dist/cache/last-resolved.js +11 -0
  4. package/dist/cli.js +4 -1
  5. package/dist/dashboard/assets/graph-canvas-DLioOiRN.css +1 -0
  6. package/dist/dashboard/assets/graph-canvas-DtJSpova.js +8 -0
  7. package/dist/dashboard/assets/index-BndChMep.css +2 -0
  8. package/dist/dashboard/assets/index-DRSwIRXd.js +17 -0
  9. package/dist/dashboard/index.html +3 -3
  10. package/dist/dashboard/logo.png +0 -0
  11. package/dist/docker/item-network.js +51 -18
  12. package/dist/docker/package.js +85 -76
  13. package/dist/docker/server-tools.js +96 -0
  14. package/dist/docker/stream.js +15 -7
  15. package/dist/environment/replace-env-variables.js +1 -1
  16. package/dist/environment/run-source-envs.js +18 -0
  17. package/dist/executer/check-container-binds.js +158 -0
  18. package/dist/executer/docker-service.js +42 -3
  19. package/dist/executer/docker-task.js +21 -5
  20. package/dist/executer/execute-command.js +9 -15
  21. package/dist/executer/execute-work-task.js +14 -2
  22. package/dist/executer/execute-work-tree.js +2 -1
  23. package/dist/executer/execution-steps.js +3 -2
  24. package/dist/executer/local-task.js +3 -0
  25. package/dist/executer/print-container-options.js +10 -10
  26. package/dist/executer/process-manager.js +84 -5
  27. package/dist/executer/scheduler/enqueue-next.js +6 -1
  28. package/dist/executer/scheduler/state-key.js +71 -34
  29. package/dist/executer/task-priority.js +131 -0
  30. package/dist/executer/watch-loop.js +18 -5
  31. package/dist/file/get-file-context.js +5 -1
  32. package/dist/file/stat-beneath.js +66 -0
  33. package/dist/file/within-root.js +13 -4
  34. package/dist/harness/connect.js +1 -6
  35. package/dist/harness/mcp-command.js +1 -1
  36. package/dist/harness/sandbox-client.js +39 -4
  37. package/dist/harness/service-tools.js +2 -2
  38. package/dist/kubernetes/await-running-state.js +98 -18
  39. package/dist/kubernetes/claim-layout.js +10 -1
  40. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +10 -1
  41. package/dist/kubernetes/ensure-persistent-data.js +129 -26
  42. package/dist/kubernetes/follow-pod-logs.js +128 -29
  43. package/dist/kubernetes/kept-claims.js +16 -1
  44. package/dist/kubernetes/network-gate.js +74 -11
  45. package/dist/kubernetes/network-policy.js +35 -13
  46. package/dist/kubernetes/quota-wait.js +97 -0
  47. package/dist/kubernetes/remove-stale-items.js +15 -2
  48. package/dist/kubernetes/restore-kubernetes-data.js +28 -4
  49. package/dist/kubernetes/sandbox-runner.js +3 -4
  50. package/dist/kubernetes/store-kubernetes-data.js +14 -2
  51. package/dist/kubernetes/transfer-tools.js +66 -0
  52. package/dist/kubernetes/volumes.js +78 -14
  53. package/dist/optimizer/calculate-checksum.js +46 -3
  54. package/dist/optimizer/check-key.js +7 -0
  55. package/dist/optimizer/check-keys.js +19 -5
  56. package/dist/optimizer/get-work-cache-stats.js +55 -31
  57. package/dist/optimizer/work-task-cache-description.js +36 -7
  58. package/dist/parser/read-build-file.js +8 -3
  59. package/dist/planner/utils/append-work-service.js +1 -1
  60. package/dist/planner/utils/append-work-task.js +1 -1
  61. package/dist/planner/utils/assert-item-paths.js +6 -7
  62. package/dist/planner/utils/template-value.js +20 -4
  63. package/dist/planner/work-item-executions.js +27 -0
  64. package/dist/planner/work-runtime-docker.js +31 -50
  65. package/dist/planner/work-runtime-kubernetes.js +70 -41
  66. package/dist/planner/work-runtime-local.js +3 -7
  67. package/dist/planner/work-secret.js +135 -13
  68. package/dist/planner/work-tree.js +10 -0
  69. package/dist/remote/collect-bundle.js +9 -1
  70. package/dist/remote/host-config.js +49 -3
  71. package/dist/remote/location.js +5 -5
  72. package/dist/remote/remote-commands.js +12 -14
  73. package/dist/remote/remote-work-commands.js +31 -10
  74. package/dist/schema/schema-parser.js +46 -2
  75. package/dist/schema/work-scope-parser.js +16 -1
  76. package/dist/secrets/secret-catalog.js +8 -1
  77. package/dist/server/capacity.js +71 -41
  78. package/dist/server/dashboard.js +35 -14
  79. package/dist/server/endpoints.js +10 -1
  80. package/dist/server/executor.js +68 -35
  81. package/dist/server/kubernetes-backend.js +29 -4
  82. package/dist/server/kubernetes-sandbox-storage.js +14 -4
  83. package/dist/server/oauth.js +16 -1
  84. package/dist/server/overview.js +100 -13
  85. package/dist/server/plan.js +41 -6
  86. package/dist/server/policy.js +22 -9
  87. package/dist/server/repo-access.js +4 -3
  88. package/dist/server/run-environment.js +5 -2
  89. package/dist/server/run-plan.js +18 -6
  90. package/dist/server/run-retry.js +57 -0
  91. package/dist/server/run-source-environment.js +39 -0
  92. package/dist/server/run-worker.js +71 -9
  93. package/dist/server/runs.js +133 -7
  94. package/dist/server/sandbox-files.js +339 -55
  95. package/dist/server/sandbox-patch.js +43 -17
  96. package/dist/server/sandboxes.js +57 -23
  97. package/dist/server/server-config.js +28 -5
  98. package/dist/server/server.js +150 -20
  99. package/dist/server/service-runs.js +50 -5
  100. package/dist/server/sources.js +68 -11
  101. package/dist/server/timings.js +23 -3
  102. package/dist/utils/buffer-context.js +30 -20
  103. package/dist/utils/limit.js +30 -0
  104. package/dist/utils/line-splitter.js +52 -0
  105. package/dist/utils/read-lines.js +65 -0
  106. package/dist/utils/tar.js +51 -1
  107. package/dist/utils/units.js +36 -1
  108. package/dist/utils/web-links.js +34 -0
  109. package/package.json +1 -1
  110. package/dist/dashboard/assets/index-CjnKm5-3.css +0 -2
  111. package/dist/dashboard/assets/index-zPXa4klZ.js +0 -13
  112. package/dist/dashboard/favicon.svg +0 -1
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unavailableExecutor = exports.RunRegistry = exports.MAX_TASK_ERROR = exports.MAX_CAUSES = exports.SANDBOX_ID_PATTERN = exports.RUN_ID_PATTERN = exports.QuotaError = exports.RunError = exports.isFinal = void 0;
3
+ exports.unavailableExecutor = exports.RunRegistry = exports.MAX_COMMANDS = exports.MAX_TASK_ERROR = exports.MAX_CAUSES = exports.SANDBOX_ID_PATTERN = exports.RUN_ID_PATTERN = exports.QuotaError = exports.RunError = exports.RUN_PHASES = exports.isFinal = void 0;
4
4
  const crypto_1 = require("crypto");
5
+ const oauth_1 = require("./oauth");
5
6
  const run_logs_1 = require("./run-logs");
6
7
  const units_1 = require("../utils/units");
7
8
  const FINAL_STATES = ['succeeded', 'failed', 'cancelled', 'rejected'];
@@ -9,6 +10,26 @@ function isFinal(state) {
9
10
  return FINAL_STATES.includes(state);
10
11
  }
11
12
  exports.isFinal = isFinal;
13
+ // What an executor reports while it works. `seq` and `time` are added by the registry.
14
+ // The steps of a run before its tasks: `source` gets its files (a checkout of the commit, or
15
+ // those of the sandbox), `namespace` prepares its place on the cluster, `plan` starts the
16
+ // process of the run and reads its build files.
17
+ exports.RUN_PHASES = ['source', 'namespace', 'plan'];
18
+ // how many past runs of a task its expected duration is the median of
19
+ const DURATION_SAMPLES = 9;
20
+ // How long a task that succeeded ran: from its first command to the end of its last when
21
+ // they are known, as its duration also counts its wait for its dependencies and a place.
22
+ function ranFor(result) {
23
+ const commands = result.commands ?? [];
24
+ if (commands.length > 0) {
25
+ const from = Date.parse(commands[0].startedAt);
26
+ const to = Date.parse(commands[commands.length - 1].finishedAt);
27
+ if (Number.isFinite(from) && Number.isFinite(to) && to >= from) {
28
+ return to - from;
29
+ }
30
+ }
31
+ return result.durationMs;
32
+ }
12
33
  // The server can not take the run now; `status` is what the caller is told
13
34
  class RunError extends Error {
14
35
  constructor() {
@@ -60,6 +81,11 @@ const TAIL_LINE_LENGTH = 1000;
60
81
  // does not can still not make a result of any size.
61
82
  exports.MAX_CAUSES = 20;
62
83
  exports.MAX_TASK_ERROR = 2 * 1024;
84
+ // the command times a task result keeps
85
+ exports.MAX_COMMANDS = 50;
86
+ function isTimestamp(value) {
87
+ return typeof value === 'string' && value.length <= 40 && !Number.isNaN(Date.parse(value));
88
+ }
63
89
  function bounded(event) {
64
90
  const causes = event.causes;
65
91
  return {
@@ -73,12 +99,20 @@ function bounded(event) {
73
99
  }
74
100
  : {}),
75
101
  ...(event.error !== undefined ? { error: event.error.slice(0, exports.MAX_TASK_ERROR) } : {}),
102
+ ...(event.commands
103
+ ? {
104
+ commands: event.commands
105
+ .filter((c) => isTimestamp(c?.startedAt) && isTimestamp(c?.finishedAt))
106
+ .slice(0, exports.MAX_COMMANDS)
107
+ .map(({ startedAt, finishedAt }) => ({ startedAt, finishedAt })),
108
+ }
109
+ : {}),
76
110
  };
77
111
  }
78
112
  // what a task event weighs against the log limits of a run
79
113
  function taskEventSize(event) {
80
114
  const causes = (event.causes ?? []).reduce((sum, cause) => sum + Buffer.byteLength(cause) + 4, 0);
81
- return 64 + causes + Buffer.byteLength(event.error ?? '');
115
+ return 64 + causes + Buffer.byteLength(event.error ?? '') + (event.commands?.length ?? 0) * 64;
82
116
  }
83
117
  // Where runs are kept while the server is up. Everything is scoped to the
84
118
  // caller that submitted the run: the same id asked for by someone else is
@@ -94,6 +128,8 @@ class RunRegistry {
94
128
  this.waiters = new Set();
95
129
  // the moves of service runs that execute beside their services
96
130
  this.moving = new Set();
131
+ // the records of evicted runs still being removed from the store, waited for by close
132
+ this.removing = new Set();
97
133
  this.maxConcurrent = options.maxConcurrent ?? 4;
98
134
  this.keep = options.keep ?? 200;
99
135
  this.maxEvents = options.maxEvents ?? 5000;
@@ -192,12 +228,14 @@ class RunRegistry {
192
228
  this.checkUpLimits(owner, spec.up.name);
193
229
  this.checkHosts(owner, spec.up);
194
230
  }
231
+ const name = (0, oauth_1.displayName)(owner);
195
232
  const run = {
196
233
  ...spec,
197
234
  id: `run_${(0, crypto_1.randomBytes)(12).toString('base64url')}`,
198
235
  owner: {
199
236
  iss: owner.iss,
200
237
  sub: owner.sub,
238
+ ...(name ? { name } : {}),
201
239
  ...(spec.up?.sleepTtl ? { groups: [...(owner.groups ?? [])], claims: { ...(owner.claims ?? {}) } } : {}),
202
240
  },
203
241
  state: waiting ? 'awaiting-approval' : 'queued',
@@ -255,6 +293,44 @@ class RunRegistry {
255
293
  const entry = this.entries.get(id);
256
294
  return entry ? this.copy(entry.run) : undefined;
257
295
  }
296
+ // What the tasks of a run took when they last ran in its repository, in ms by task name:
297
+ // the median of their last DURATION_SAMPLES runs that succeeded. Only times, of runs of any
298
+ // caller, and only for the scheduling of this run.
299
+ durationsOf(run) {
300
+ const repo = run.source.type === 'upload' ? undefined : run.source.repo;
301
+ if (!repo) {
302
+ return {};
303
+ }
304
+ const samples = new Map();
305
+ for (const entry of [...this.entries.values()].reverse()) {
306
+ const other = entry.run;
307
+ if (other.id === run.id || other.source.type === 'upload' || other.source.repo !== repo) {
308
+ continue;
309
+ }
310
+ for (const [task, result] of Object.entries(other.results ?? {})) {
311
+ const took = result.status === 'succeeded' ? ranFor(result) : undefined;
312
+ if (took === undefined) {
313
+ continue;
314
+ }
315
+ const times = samples.get(task) ?? [];
316
+ if (times.length < DURATION_SAMPLES) {
317
+ times.push(took);
318
+ samples.set(task, times);
319
+ }
320
+ }
321
+ }
322
+ const durations = {};
323
+ // only the tasks of this run, when its plan says which
324
+ const planned = run.plan ? new Set(Object.keys(run.plan)) : null;
325
+ for (const [task, times] of samples) {
326
+ if (planned && !planned.has(task)) {
327
+ continue;
328
+ }
329
+ times.sort((a, b) => a - b);
330
+ durations[task] = times[Math.floor((times.length - 1) / 2)];
331
+ }
332
+ return durations;
333
+ }
258
334
  // every run it keeps, of every caller, newest first: who may see which is for the caller
259
335
  all() {
260
336
  return [...this.entries.values()].map((entry) => this.copy(entry.run)).reverse();
@@ -306,8 +382,10 @@ class RunRegistry {
306
382
  }
307
383
  // A queued run is cancelled at once; a running one when its executor stops. A service run
308
384
  // that is up stops its services, and says who stopped it.
309
- cancel(owner, id, canUse) {
310
- const entry = this.find(owner, id, canUse);
385
+ // Stops a run for its owner, who decides about it, and, a service run, whoever `mayStop` says.
386
+ cancel(owner, id, canUse, mayStop) {
387
+ const other = this.entries.get(id);
388
+ const entry = this.find(owner, id, canUse) ?? (other?.run.up && mayStop?.(this.copy(other.run)) ? other : undefined);
311
389
  if (!entry) {
312
390
  return undefined;
313
391
  }
@@ -336,6 +414,24 @@ class RunRegistry {
336
414
  this.expireLater(entry);
337
415
  return { status: 'ok', run: this.copy(entry.run) };
338
416
  }
417
+ /**
418
+ * Wakes a service run that sleeps, for its owner or who `mayOpen` it, as a request to its
419
+ * endpoints would (visit): with the same check of its owner (`mayWake`) and the same places.
420
+ * Answers why it did not wake when it could not.
421
+ */
422
+ wake(caller, id, mayOpen, mayWake) {
423
+ const entry = this.entries.get(id);
424
+ if (!entry?.run.up || !(this.owns(caller, entry) || mayOpen?.(this.copy(entry.run)))) {
425
+ return { status: 'not-found' };
426
+ }
427
+ if (entry.run.state !== 'asleep') {
428
+ return { status: 'not-asleep', state: entry.run.state };
429
+ }
430
+ const refused = this.visit(id, mayWake);
431
+ return refused
432
+ ? { status: 'refused', reason: refused, run: this.copy(entry.run) }
433
+ : { status: 'ok', run: this.copy(entry.run) };
434
+ }
339
435
  /**
340
436
  * The service run an endpoint host belongs to: the newest that is up, so one that replaces
341
437
  * a run of the same name takes its hosts over as soon as it is up; else the newest that
@@ -360,7 +456,10 @@ class RunRegistry {
360
456
  }
361
457
  // The run that holds a sandbox: the oldest on it that is not over. It holds it from
362
458
  // when it is submitted, so the files are not moved under what an approver saw. A service
363
- // run that is up works on a copy of the files, and holds it no longer.
459
+ // run that is up holds it no longer, so the agent can go on working while its services
460
+ // run: an approved one works on a copy of the files, any other (on a host) in the sandbox
461
+ // itself, which edits then write in through the directories they checked (see `write` of
462
+ // HostSandboxFiles).
364
463
  holder(sandbox) {
365
464
  return [...this.entries.values()].find((entry) => onSandbox(entry.run, sandbox) &&
366
465
  !isFinal(entry.run.state) &&
@@ -575,8 +674,10 @@ class RunRegistry {
575
674
  [...this.entries.values()].some((entry) => entry.run.state === 'running' || entry.run.state === 'up')) {
576
675
  await new Promise((resolve) => setTimeout(resolve, 10));
577
676
  }
578
- // the runs that were stopped are written down before the server is gone
677
+ // the runs that were stopped are written down before the server is gone, and the records
678
+ // of evicted runs are gone from the store
579
679
  await Promise.all([...this.entries.values()].map((entry) => entry.saving));
680
+ await Promise.all([...this.removing]);
580
681
  }
581
682
  find(owner, id, canUse) {
582
683
  const entry = this.entries.get(id);
@@ -612,6 +713,7 @@ class RunRegistry {
612
713
  accounts: [...run.accounts],
613
714
  ...(run.approvals ? { approvals: JSON.parse(JSON.stringify(run.approvals)) } : {}),
614
715
  ...(run.results ? { results: JSON.parse(JSON.stringify(run.results)) } : {}),
716
+ ...(run.phases ? { phases: run.phases.map((phase) => ({ ...phase })) } : {}),
615
717
  ...(run.moves ? { moves: run.moves.map((move) => ({ ...move })) } : {}),
616
718
  ...(run.moving ? { moving: { ...run.moving } } : {}),
617
719
  };
@@ -796,9 +898,11 @@ class RunRegistry {
796
898
  ? { ...this.copy(entry.run), source: { ...landing.source }, ...(landing.plan ? { plan: landing.plan } : {}) }
797
899
  : this.copy(entry.run);
798
900
  try {
901
+ const durations = this.durationsOf(run);
799
902
  await this.options.executor.execute(run, {
800
903
  emit: (event) => this.record(entry, event),
801
904
  signal: entry.abort.signal,
905
+ ...(Object.keys(durations).length > 0 ? { durations } : {}),
802
906
  ...(entry.run.up
803
907
  ? {
804
908
  up: (targets) => this.markUp(entry, targets),
@@ -1424,6 +1528,10 @@ class RunRegistry {
1424
1528
  }
1425
1529
  return;
1426
1530
  }
1531
+ if (recorded.type === 'phase') {
1532
+ this.keepPhase(entry, recorded);
1533
+ return;
1534
+ }
1427
1535
  const event = recorded.type === 'task' ? bounded(recorded) : recorded;
1428
1536
  if (event.type === 'log') {
1429
1537
  this.keepTail(entry, event.task, event.text);
@@ -1446,6 +1554,19 @@ class RunRegistry {
1446
1554
  }
1447
1555
  this.push(entry, event);
1448
1556
  }
1557
+ // once each, from the executor: a phase it reports again (a service run that wakes) is the
1558
+ // newest
1559
+ keepPhase(entry, event) {
1560
+ if (!exports.RUN_PHASES.includes(event.phase) ||
1561
+ !isTimestamp(event.startedAt) ||
1562
+ !isTimestamp(event.finishedAt) ||
1563
+ Date.parse(event.finishedAt) < Date.parse(event.startedAt)) {
1564
+ return;
1565
+ }
1566
+ const phases = (entry.run.phases ?? []).filter((phase) => phase.phase !== event.phase);
1567
+ phases.push({ phase: event.phase, startedAt: event.startedAt, finishedAt: event.finishedAt });
1568
+ entry.run.phases = phases.sort((a, b) => exports.RUN_PHASES.indexOf(a.phase) - exports.RUN_PHASES.indexOf(b.phase));
1569
+ }
1449
1570
  keepTail(entry, task, text) {
1450
1571
  let tail = entry.tails.get(task);
1451
1572
  if (!tail) {
@@ -1487,6 +1608,7 @@ class RunRegistry {
1487
1608
  ...(event.durationMs !== undefined ? { durationMs: event.durationMs } : {}),
1488
1609
  ...(event.exitCode !== undefined ? { exitCode: event.exitCode } : {}),
1489
1610
  ...(event.error ? { error: event.error } : {}),
1611
+ ...(event.commands?.length ? { commands: event.commands } : {}),
1490
1612
  ...(event.state === 'cached'
1491
1613
  ? { cache: { hit: true } }
1492
1614
  : event.state === 'succeeded' || event.state === 'failed'
@@ -1524,7 +1646,11 @@ class RunRegistry {
1524
1646
  const finished = [...this.entries.values()].filter((entry) => FINAL_STATES.includes(entry.run.state));
1525
1647
  for (const entry of finished.slice(0, Math.max(0, finished.length - this.keep))) {
1526
1648
  this.entries.delete(entry.run.id);
1527
- void entry.saving.then(() => this.options.store?.remove(entry.run.id)).catch(() => undefined);
1649
+ const removal = entry.saving
1650
+ .then(() => this.options.store?.remove(entry.run.id))
1651
+ .catch(() => undefined)
1652
+ .finally(() => this.removing.delete(removal));
1653
+ this.removing.add(removal);
1528
1654
  }
1529
1655
  }
1530
1656
  }