hammerkit 2.0.0-next.2 → 2.0.0-next.20

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 (111) 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-HmkW_-XJ.js +8 -0
  7. package/dist/dashboard/assets/index-gIBDC_m8.css +2 -0
  8. package/dist/dashboard/assets/index-vE22S5E_.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 +19 -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 +27 -9
  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 +80 -11
  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-source-environment.js +39 -0
  91. package/dist/server/run-worker.js +71 -9
  92. package/dist/server/runs.js +133 -7
  93. package/dist/server/sandbox-files.js +339 -55
  94. package/dist/server/sandbox-patch.js +43 -17
  95. package/dist/server/sandboxes.js +57 -23
  96. package/dist/server/server-config.js +28 -5
  97. package/dist/server/server.js +94 -16
  98. package/dist/server/service-runs.js +50 -5
  99. package/dist/server/sources.js +68 -11
  100. package/dist/server/timings.js +23 -3
  101. package/dist/utils/buffer-context.js +30 -20
  102. package/dist/utils/limit.js +30 -0
  103. package/dist/utils/line-splitter.js +52 -0
  104. package/dist/utils/read-lines.js +65 -0
  105. package/dist/utils/tar.js +51 -1
  106. package/dist/utils/units.js +36 -1
  107. package/dist/utils/web-links.js +34 -0
  108. package/package.json +1 -1
  109. package/dist/dashboard/assets/index-CjnKm5-3.css +0 -2
  110. package/dist/dashboard/assets/index-zPXa4klZ.js +0 -13
  111. package/dist/dashboard/favicon.svg +0 -1
@@ -14,6 +14,8 @@ const remove_container_1 = require("../docker/remove-container");
14
14
  const check_readiness_1 = require("./check-readiness");
15
15
  const main_process_user_1 = require("./main-process-user");
16
16
  const execution_steps_1 = require("./execution-steps");
17
+ const check_container_binds_1 = require("./check-container-binds");
18
+ const server_tools_1 = require("../docker/server-tools");
17
19
  const item_network_1 = require("../docker/item-network");
18
20
  const container_secrets_1 = require("./container-secrets");
19
21
  const replace_env_variables_1 = require("../environment/replace-env-variables");
@@ -44,9 +46,12 @@ async function readPublishedPorts(container, ports) {
44
46
  });
45
47
  }
46
48
  exports.readPublishedPorts = readPublishedPorts;
47
- function buildServiceCreateOptions(item, options, network, secrets, resources = {}) {
49
+ function buildServiceCreateOptions(item, options, network, secrets, resources = {},
50
+ // on a server: the server's tools, and the command the service runs once the server
51
+ // checked its binds (gatedCommand)
52
+ gate) {
48
53
  const envs = (0, replace_env_variables_1.getEnvironmentVariables)(item.data.envs);
49
- const command = (0, work_service_1.getServiceCommand)(item.data);
54
+ const command = gate ? { entrypoint: (0, server_tools_1.gatedCommand)(gate.tools, gate.command), cmd: [] } : (0, work_service_1.getServiceCommand)(item.data);
50
55
  return {
51
56
  Image: item.data.image,
52
57
  Hostname: (0, service_hostname_1.getServiceHostname)(item.name),
@@ -74,7 +79,9 @@ function buildServiceCreateOptions(item, options, network, secrets, resources =
74
79
  ...item.data.mounts.map((v) => `${v.localPath}:${(0, execute_docker_1.convertToPosixPath)(v.containerPath)}${v.readOnly ? ':ro' : ''}`),
75
80
  ...item.data.volumes.map((v) => `${v.name}:${(0, execute_docker_1.convertToPosixPath)(v.containerPath)}${v.readOnly ? ':ro' : ''}`),
76
81
  ...secrets.binds,
82
+ ...(gate ? (0, server_tools_1.serverToolsBinds)(gate.tools) : []),
77
83
  ],
84
+ ...(gate ? { Tmpfs: (0, server_tools_1.serviceGateTmpfs)(gate.tools) } : {}),
78
85
  // host ports are for `hammerkit up` and local tasks; container tasks
79
86
  // reach the service over its link, and a run publishing them would
80
87
  // collide with another run on the same host. A limited service's are
@@ -89,6 +96,15 @@ function buildServiceCreateOptions(item, options, network, secrets, resources =
89
96
  };
90
97
  }
91
98
  exports.buildServiceCreateOptions = buildServiceCreateOptions;
99
+ // What docker would run in the service's container: its own command, or the image's.
100
+ async function getServiceProcess(docker, service) {
101
+ const { entrypoint, cmd } = (0, work_service_1.getServiceCommand)(service);
102
+ if (entrypoint) {
103
+ return [...entrypoint, ...(cmd ?? [])];
104
+ }
105
+ const config = (await docker.getImage(service.image).inspect()).Config;
106
+ return [...(config.Entrypoint ?? []), ...(cmd ?? config.Cmd ?? [])];
107
+ }
92
108
  async function dockerService(docker, item, options, environment) {
93
109
  let container = null;
94
110
  let network = null;
@@ -107,12 +123,35 @@ async function dockerService(docker, item, options, environment) {
107
123
  item.status.write('debug', `create container with image ${item.data.image}`);
108
124
  const secrets = await (0, container_secrets_1.prepareContainerSecrets)(item.data.secrets, (0, work_instance_id_1.getWorkInstanceId)(item), environment);
109
125
  const resources = await (0, docker_resources_1.resolveDockerResources)(docker, item.data.resources, item.status);
126
+ (0, check_container_binds_1.refuseServerToolsPaths)(item, environment);
127
+ const gate = (0, execution_steps_1.hostedRoot)(environment)
128
+ ? {
129
+ tools: (0, server_tools_1.createServerTools)(await (0, server_tools_1.ensureServerBusybox)(docker, item.status, environment)),
130
+ command: await getServiceProcess(docker, item.data),
131
+ }
132
+ : undefined;
110
133
  await (0, execution_steps_1.refuseHostLinks)(item, environment);
111
- container = await docker.createContainer(buildServiceCreateOptions(item, options, network.options, secrets, resources));
134
+ container = await docker.createContainer(buildServiceCreateOptions(item, options, network.options, secrets, resources, gate));
112
135
  const relay = await network.connect(container);
113
136
  const stream = await container.attach({ stream: true, stdout: true, stderr: true });
114
137
  (0, stream_1.logStream)(item.status, stream);
138
+ // the engine resolves the paths it binds when the container starts: on a server the
139
+ // service's own command waits for the server to check them (gatedCommand)
140
+ await (0, execution_steps_1.refuseHostLinks)(item, environment);
115
141
  await container.start();
142
+ const started = container;
143
+ const binds = [
144
+ ...item.data.src.map((s) => ({ localPath: s.absolutePath, containerPath: s.absolutePath })),
145
+ ...item.data.mounts,
146
+ ];
147
+ await (0, check_container_binds_1.assertContainerBinds)(item, environment, started, binds, gate?.tools)
148
+ .then(() => (gate ? (0, check_container_binds_1.openServiceGate)(started, gate.tools) : undefined))
149
+ .catch(async (e) => {
150
+ // not kept, not even for a daemon
151
+ container = null;
152
+ await (0, remove_container_1.removeContainer)(started);
153
+ throw e;
154
+ });
116
155
  const published = options.publishPorts ? await readPublishedPorts(relay ?? container, item.data.ports) : [];
117
156
  if (!item.data.healthcheck || !options.waitForReady) {
118
157
  options.state.set({
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dockerTask = exports.getCapabilityHostConfig = exports.buildCreateOptions = void 0;
4
+ const work_item_executions_1 = require("../planner/work-item-executions");
4
5
  const run_labels_1 = require("../docker/run-labels");
5
6
  const execute_docker_1 = require("./execute-docker");
6
7
  const abort_1 = require("./abort");
7
8
  const log_1 = require("../log");
8
9
  const execution_steps_1 = require("./execution-steps");
9
10
  const using_container_1 = require("../docker/using-container");
11
+ const check_container_binds_1 = require("./check-container-binds");
12
+ const server_tools_1 = require("../docker/server-tools");
10
13
  const print_container_options_1 = require("./print-container-options");
11
14
  const tar_1 = require("tar");
12
15
  const replace_env_variables_1 = require("../environment/replace-env-variables");
@@ -17,7 +20,9 @@ const get_outputs_to_reset_1 = require("../planner/utils/get-outputs-to-reset");
17
20
  const container_secrets_1 = require("./container-secrets");
18
21
  const container_task_state_1 = require("./container-task-state");
19
22
  const docker_resources_1 = require("./docker-resources");
20
- function buildCreateOptions(item, stateKey, network, environment, secrets, resources = {}) {
23
+ function buildCreateOptions(item, stateKey, network, environment, secrets, resources = {},
24
+ // on a server: the server's tools, whose busybox is the container's first process
25
+ tools) {
21
26
  const binds = (0, get_container_binds_1.getContainerBinds)(item);
22
27
  const envs = (0, replace_env_variables_1.getEnvironmentVariables)(item.data.envs);
23
28
  // Running as the host's uid (Linux), the user has no home in the image and
@@ -28,8 +33,9 @@ function buildCreateOptions(item, stateKey, network, environment, secrets, resou
28
33
  abortSignal: environment.abortCtrl.signal,
29
34
  Image: item.data.image,
30
35
  Tty: true,
31
- Entrypoint: [item.data.shell],
32
- Cmd: ['-c', 'sleep 3600'],
36
+ // on a server nothing of the image runs before its binds are checked (check-container-binds.ts)
37
+ Entrypoint: tools ? [(0, server_tools_1.serverBusybox)(tools)] : [item.data.shell],
38
+ Cmd: tools ? ['sleep', '3600'] : ['-c', 'sleep 3600'],
33
39
  Env: Object.entries({ ...envs, ...home, ...network.env, ...secrets.env }).map(([key, value]) => `${key}=${value}`),
34
40
  WorkingDir: (0, execute_docker_1.convertToPosixPath)(item.data.cwd),
35
41
  Labels: {
@@ -43,6 +49,7 @@ function buildCreateOptions(item, stateKey, network, environment, secrets, resou
43
49
  Binds: [
44
50
  ...binds.map((b) => `${b.localPath}:${(0, execute_docker_1.convertToPosixPath)(b.containerPath)}${b.readOnly ? ':ro' : ''}`),
45
51
  ...secrets.binds,
52
+ ...(tools ? (0, server_tools_1.serverToolsBinds)(tools) : []),
46
53
  ],
47
54
  ...(0, item_network_1.getNetworkHostConfig)(network),
48
55
  AutoRemove: true,
@@ -93,11 +100,18 @@ async function dockerTask(docker, item, environment, options) {
93
100
  (0, abort_1.checkForAbort)(options.abort);
94
101
  const secrets = await (0, container_secrets_1.prepareContainerSecrets)(item.data.secrets, (0, work_instance_id_1.getWorkInstanceId)(item), environment);
95
102
  const resources = await (0, docker_resources_1.resolveDockerResources)(docker, item.data.resources, item.status);
96
- const containerOptions = buildCreateOptions(item, options.stateKey, network.options, environment, secrets, resources);
97
- (0, print_container_options_1.printContainerOptions)(item.status, containerOptions);
103
+ (0, check_container_binds_1.refuseServerToolsPaths)(item, environment);
104
+ (0, check_container_binds_1.refuseUncheckableBinds)(item, environment);
105
+ const tools = (0, execution_steps_1.hostedRoot)(environment)
106
+ ? (0, server_tools_1.createServerTools)(await (0, server_tools_1.ensureServerBusybox)(docker, item.status, environment))
107
+ : undefined;
108
+ const containerOptions = buildCreateOptions(item, options.stateKey, network.options, environment, secrets, resources, tools);
109
+ (0, print_container_options_1.printContainerOptions)(item.status, containerOptions, tools);
98
110
  await (0, execution_steps_1.refuseHostLinks)(item, environment);
99
111
  const itemNetwork = network;
100
112
  const succeeded = await (0, using_container_1.usingContainer)(docker, item, containerOptions, async (container) => {
113
+ // the engine resolved the paths it binds as the container started: before anything runs in it
114
+ await (0, check_container_binds_1.assertContainerBinds)(item, environment, container, (0, get_container_binds_1.getContainerBinds)(item), tools);
101
115
  await itemNetwork.connect(container);
102
116
  await (0, execution_steps_1.setUserPermissions)(item, container, environment);
103
117
  for (const generate of outputsToReset.filter((g) => !g.isFile)) {
@@ -106,7 +120,9 @@ async function dockerTask(docker, item, environment, options) {
106
120
  for (const cmd of item.data.cmds) {
107
121
  (0, abort_1.checkForAbort)(options.abort);
108
122
  item.status.write('info', `execute cmd "${cmd.cmd}" in container`);
123
+ const commandStarted = new Date();
109
124
  const result = await (0, execute_docker_1.execCommand)(item.status, environment, container, (0, execute_docker_1.convertToPosixPath)(cmd.cwd), [item.data.shell, '-c', cmd.cmd], item.data.user, undefined, options.abort);
125
+ (0, work_item_executions_1.recordExecution)(item.data, commandStarted, new Date());
110
126
  if (result.type === 'timeout') {
111
127
  throw new Error(`command ${cmd.cmd} timed out`);
112
128
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.executeCommand = exports.processEnv = void 0;
4
4
  const child_process_1 = require("child_process");
5
5
  const os_1 = require("os");
6
- const log_1 = require("../log");
6
+ const line_splitter_1 = require("../utils/line-splitter");
7
7
  const abort_1 = require("./abort");
8
8
  const abort_event_1 = require("../utils/abort-event");
9
9
  const environment_config_1 = require("../utils/environment-config");
@@ -43,25 +43,19 @@ async function executeCommand(status, abortSignal, cwd, command, envs, environme
43
43
  // they would keep running and holding the output open
44
44
  detached: !windows,
45
45
  });
46
- // decode across chunk boundaries, so a multi-byte character split between
47
- // two chunks stays intact
48
- ps.stdout?.setEncoding('utf8');
49
- ps.stderr?.setEncoding('utf8');
50
- ps.stdout?.on('data', async (data) => {
51
- for (const log of (0, log_1.getLogs)(data)) {
52
- status.console('stdout', log);
53
- }
54
- });
55
- ps.stderr?.on('data', async (data) => {
56
- for (const log of (0, log_1.getLogs)(data)) {
57
- status.console('stderr', log);
58
- }
59
- });
46
+ // a line that two chunks split is logged once it ends, whole: a multi-byte character
47
+ // stays intact, and so does a secret value, which is masked per line
48
+ const stdout = (0, line_splitter_1.lineSplitter)((line) => status.console('stdout', line));
49
+ const stderr = (0, line_splitter_1.lineSplitter)((line) => status.console('stderr', line));
50
+ ps.stdout?.on('data', (data) => stdout.write(data));
51
+ ps.stderr?.on('data', (data) => stderr.write(data));
60
52
  ps.on('error', (err) => {
61
53
  reject(err);
62
54
  });
63
55
  ps.on('close', (code) => {
64
56
  abortListener.close();
57
+ stdout.end();
58
+ stderr.end();
65
59
  if (abortSignal.aborted) {
66
60
  reject(new abort_1.AbortError());
67
61
  return;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.executeWorkTask = void 0;
4
+ const work_item_executions_1 = require("../planner/work-item-executions");
4
5
  const work_task_1 = require("../planner/work-task");
5
6
  const states_1 = require("./states");
6
7
  const abort_1 = require("./abort");
@@ -12,6 +13,7 @@ const explain_1 = require("../cache/explain");
12
13
  const archive_task_entry_1 = require("./archive-task-entry");
13
14
  const units_1 = require("../utils/units");
14
15
  const deferred_restore_1 = require("./deferred-restore");
16
+ const task_priority_1 = require("./task-priority");
15
17
  async function pushToBackend(work, environment, cacheState, options) {
16
18
  const { resolved, stateKey, provable } = cacheState;
17
19
  // an aggregate has nothing to store, see checkCacheState
@@ -83,7 +85,7 @@ dependents = null) {
83
85
  // build summary can show the cause column. Reporting only.
84
86
  let missCauses;
85
87
  if (options.explain) {
86
- const explanation = await (0, explain_1.explainTask)(work, options.cacheDefault, environment);
88
+ const explanation = await (0, explain_1.explainTask)(work, options.cacheDefault, environment, undefined, undefined, (0, watch_loop_1.getSharedStateKeys)(work, options));
87
89
  missCauses = explanation.causes.map(explain_1.describeCause);
88
90
  for (const cause of missCauses) {
89
91
  work.status.write('info', `cache miss: ${cause}`);
@@ -96,6 +98,8 @@ dependents = null) {
96
98
  }
97
99
  await (0, await_completed_dependencies_1.awaitRunningNeeds)(work, work.needs.map((n) => n.service), abort);
98
100
  await options.processManager.task(work, async () => {
101
+ // from here it runs; `started` also counts its wait for dependencies and a place
102
+ const ranFrom = new Date();
99
103
  work.state.set({
100
104
  type: 'running',
101
105
  stateKey: cacheState.stateKey,
@@ -103,6 +107,7 @@ dependents = null) {
103
107
  });
104
108
  const timeout = (0, deadline_1.getTaskTimeout)(work.data.timeout, options);
105
109
  const deadline = (0, deadline_1.withDeadline)(abort, timeout);
110
+ (0, work_item_executions_1.clearExecutions)(work.data);
106
111
  try {
107
112
  await work.runtime.execute(environment, {
108
113
  cache: cacheState,
@@ -112,6 +117,7 @@ dependents = null) {
112
117
  daemon: options.daemon,
113
118
  publishPorts: false,
114
119
  waitForReady: true,
120
+ onQuotaRefused: (refused) => options.processManager.quotaRefused(work, refused),
115
121
  });
116
122
  }
117
123
  finally {
@@ -135,13 +141,19 @@ dependents = null) {
135
141
  work.status.write('debug', 'completed for state key ' + cacheState.stateKey);
136
142
  await pushToBackend(work, environment, cacheState, options);
137
143
  const cached = onlyGroupsCacheHits(work);
144
+ const duration = (0, states_1.getDuration)(started);
138
145
  work.state.set({
139
146
  stateKey: cacheState.stateKey,
140
147
  type: 'completed',
141
148
  cached,
142
- duration: (0, states_1.getDuration)(started),
149
+ duration,
143
150
  missCauses: cached ? undefined : missCauses,
144
151
  });
152
+ // what it took orders the next runs, see getTaskPriority
153
+ // on a server the runs are the record, see RunRegistry
154
+ if (!(0, work_task_1.isAggregateTask)(work.data) && !environment.hosted) {
155
+ await (0, task_priority_1.recordTaskDuration)(environment, work, (0, states_1.getDuration)(ranFrom));
156
+ }
145
157
  }
146
158
  else {
147
159
  if (!options.watch) {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.executeWorkTree = void 0;
4
+ const work_tree_1 = require("../planner/work-tree");
4
5
  const plan_work_tasks_1 = require("../planner/utils/plan-work-tasks");
5
6
  const execute_work_service_1 = require("./execute-work-service");
6
7
  const execute_work_task_1 = require("./execute-work-task");
@@ -18,7 +19,7 @@ async function executeWorkTree(work, environment, options) {
18
19
  if (initOf) {
19
20
  const service = work.services[initOf.serviceName] ?? null;
20
21
  // asked for alone (by name), it runs against a service already running too
21
- const requested = work.requested?.length === 1 && work.requested[0] === task.name;
22
+ const requested = (0, work_tree_1.isAskedByName)(work, task.name);
22
23
  itemPromises.push((0, execute_init_task_1.executeInitTask)(task, initOf.init, service, requested, environment, options));
23
24
  continue;
24
25
  }
@@ -20,8 +20,9 @@ exports.hostedRoot = hostedRoot;
20
20
  * reads or writes for it, has to stay inside the run's files with no link on the way:
21
21
  * tasks of this or an earlier run on a sandbox, or the commit of a ref, could have left
22
22
  * one pointing anywhere on the server. The runtime checks before an item runs; this is
23
- * checked again right before a container is made and before outputs are copied, after
24
- * other tasks of the run had their turn.
23
+ * checked again right before a container is made, after it started (the engine resolves
24
+ * the paths it binds as it starts) and before outputs are copied, after other tasks of the
25
+ * run had their turn.
25
26
  */
26
27
  async function refuseHostLinks(item, environment) {
27
28
  const root = hostedRoot(environment);
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.localTask = void 0;
4
+ const work_item_executions_1 = require("../planner/work-item-executions");
4
5
  const abort_1 = require("./abort");
5
6
  const execute_command_1 = require("./execute-command");
6
7
  const log_1 = require("../log");
@@ -18,7 +19,9 @@ async function localTask(item, environment, options) {
18
19
  for (const cmd of item.data.cmds) {
19
20
  (0, abort_1.checkForAbort)(options.abort);
20
21
  item.status.write('info', `execute cmd "${cmd.cmd}" locally`);
22
+ const commandStarted = new Date();
21
23
  const exitCode = await (0, execute_command_1.executeCommand)(item.status, options.abort, cmd.cwd, cmd.cmd, envs, environment);
24
+ (0, work_item_executions_1.recordExecution)(item.data, commandStarted, new Date());
22
25
  if (exitCode !== 0) {
23
26
  options.state.set({
24
27
  type: 'crash',
@@ -1,25 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.printContainerOptions = void 0;
4
- function printContainerOptions(status, containerOptions) {
5
- status.write('debug', `create container with image ${containerOptions.Image} with ${containerOptions.Entrypoint}`);
4
+ const server_tools_1 = require("../docker/server-tools");
5
+ // `tools`: the server's tools in the container, whose folder is not printed
6
+ function printContainerOptions(status, containerOptions, tools) {
7
+ const write = (text) => status.write('debug', (0, server_tools_1.hideServerTools)(text, tools));
8
+ write(`create container with image ${containerOptions.Image} with ${containerOptions.Entrypoint}`);
6
9
  for (const mount of containerOptions.HostConfig?.Binds || []) {
7
- status.write('debug', `bind ${mount}`);
8
- }
9
- for (const mount of containerOptions.HostConfig?.Binds || []) {
10
- status.write('debug', `bind ${mount}`);
10
+ write(`bind ${mount}`);
11
11
  }
12
12
  if (containerOptions.HostConfig?.NanoCpus) {
13
- status.write('debug', `limit cpus to ${containerOptions.HostConfig.NanoCpus / 1e9}`);
13
+ write(`limit cpus to ${containerOptions.HostConfig.NanoCpus / 1e9}`);
14
14
  }
15
15
  if (containerOptions.HostConfig?.Memory) {
16
- status.write('debug', `limit memory to ${containerOptions.HostConfig.Memory} bytes`);
16
+ write(`limit memory to ${containerOptions.HostConfig.Memory} bytes`);
17
17
  }
18
18
  if (containerOptions.HostConfig?.MemoryReservation) {
19
- status.write('debug', `reserve memory ${containerOptions.HostConfig.MemoryReservation} bytes`);
19
+ write(`reserve memory ${containerOptions.HostConfig.MemoryReservation} bytes`);
20
20
  }
21
21
  for (const link of containerOptions.HostConfig?.Links || []) {
22
- status.write('debug', `link ${link}`);
22
+ write(`link ${link}`);
23
23
  }
24
24
  }
25
25
  exports.printContainerOptions = printContainerOptions;
@@ -7,14 +7,27 @@ class ProcessManager {
7
7
  // runs already; one starts regardless when nothing else runs, so a run
8
8
  // whose services alone fill the budget, or a task bigger than it, still
9
9
  // makes progress.
10
- constructor(workerLimit, budget = null) {
10
+ // With a priority, the waiting task that is most urgent takes a free place,
11
+ // else the one that waits longest. Places are then given out a moment after a
12
+ // task arrives or one ends, so the tasks that the same event made ready (the
13
+ // dependents of a task that completed) compete for them together.
14
+ constructor(workerLimit, budget = null, priority = null) {
11
15
  this.workerLimit = workerLimit;
12
16
  this.budget = budget;
17
+ this.priority = priority;
13
18
  this.processes = [];
14
19
  this.pendingProcesses = [];
20
+ this.pickScheduled = false;
21
+ // the running tasks whose pod the quota of the namespace refuses now
22
+ this.refused = new Set();
15
23
  }
16
24
  task(item, factory) {
17
25
  return new Promise((resolve, reject) => {
26
+ if (this.priority) {
27
+ this.pendingProcesses.push({ item, factory, resolve, reject });
28
+ this.schedulePick();
29
+ return;
30
+ }
18
31
  if (this.budget) {
19
32
  this.pendingProcesses.push({ item, factory, resolve, reject });
20
33
  this.startFitting();
@@ -43,11 +56,76 @@ class ProcessManager {
43
56
  if (!released) {
44
57
  released = true;
45
58
  budget.release(requests);
46
- this.startFitting();
59
+ if (this.priority) {
60
+ this.schedulePick();
61
+ }
62
+ else {
63
+ this.startFitting();
64
+ }
47
65
  }
48
66
  };
49
67
  }
68
+ // A running task tells whether the quota refuses its pod now. While it is refused, the
69
+ // tasks less urgent than it wait, so the room that frees goes to it and not to a pod of
70
+ // theirs; without a priority nothing changes.
71
+ quotaRefused(item, refused) {
72
+ if (refused) {
73
+ this.refused.add(item);
74
+ }
75
+ else if (this.refused.delete(item) && this.priority) {
76
+ this.schedulePick();
77
+ }
78
+ }
79
+ schedulePick() {
80
+ if (this.pickScheduled) {
81
+ return;
82
+ }
83
+ this.pickScheduled = true;
84
+ setImmediate(() => {
85
+ this.pickScheduled = false;
86
+ if (this.budget) {
87
+ this.startFitting();
88
+ }
89
+ else {
90
+ this.startByPriority();
91
+ }
92
+ });
93
+ }
94
+ // The waiting tasks that may start now, most urgent first; of equal ones, the one
95
+ // waiting longest. While the quota refuses a running task's pod, only those more urgent
96
+ // than it.
97
+ startable() {
98
+ const priority = this.priority;
99
+ if (!priority) {
100
+ return [...this.pendingProcesses];
101
+ }
102
+ const memo = new Map();
103
+ const floor = this.refused.size > 0 ? Math.max(...[...this.refused].map((item) => priority(item, memo))) : null;
104
+ // A task that needs a service is never held back: its service is up already and holds
105
+ // its room until the task ran, so holding the task back would keep that room taken.
106
+ return this.pendingProcesses
107
+ .map((pending, index) => ({ pending, index, rank: priority(pending.item, memo) }))
108
+ .filter(({ pending, rank }) => floor === null || rank > floor || pending.item.needs.length > 0)
109
+ .sort((a, b) => b.rank - a.rank || a.index - b.index)
110
+ .map(({ pending }) => pending);
111
+ }
112
+ startByPriority() {
113
+ for (const pending of this.startable()) {
114
+ if (this.workerLimit !== 0 && this.processes.length >= this.workerLimit) {
115
+ return;
116
+ }
117
+ if (this.processes.some((p) => p.id === pending.item.name)) {
118
+ continue;
119
+ }
120
+ this.pendingProcesses.splice(this.pendingProcesses.indexOf(pending), 1);
121
+ this.startProcess(pending);
122
+ }
123
+ }
50
124
  enqueuePendingProcesses() {
125
+ if (this.priority) {
126
+ this.schedulePick();
127
+ return;
128
+ }
51
129
  if (this.budget) {
52
130
  this.startFitting();
53
131
  return;
@@ -64,10 +142,10 @@ class ProcessManager {
64
142
  this.startProcess(pendingProcess);
65
143
  }
66
144
  }
67
- // In order, every pending task that fits; a later, smaller one may start
68
- // while an earlier one waits for room.
145
+ // In order (by priority when there is one), every pending task that fits; a
146
+ // later, smaller one may start while an earlier one waits for room.
69
147
  startFitting() {
70
- for (const pending of [...this.pendingProcesses]) {
148
+ for (const pending of this.startable()) {
71
149
  if (this.workerLimit !== 0 && this.processes.length >= this.workerLimit) {
72
150
  return;
73
151
  }
@@ -98,6 +176,7 @@ class ProcessManager {
98
176
  pendingProcess.reject(err);
99
177
  })
100
178
  .finally(() => {
179
+ this.refused.delete(pendingProcess.item);
101
180
  const index = this.processes.indexOf(item);
102
181
  this.processes.splice(index, 1);
103
182
  if (requests) {
@@ -18,6 +18,7 @@ async function checkCacheState(item, defaultCacheMethod, environment,
18
18
  // the cache is off
19
19
  // restore: false only looks a backend hit up, its outputs are downloaded once
20
20
  // something that runs depends on them (`run --no-restore`, see deferred-restore)
21
+ // memo: the keys of the execution so far, see getExecutionStateKeys
21
22
  options = {}) {
22
23
  (0, deferred_restore_1.clearDeferredRestore)(item);
23
24
  // A task whose cache is off is never looked up, so hashing its sources buys
@@ -28,7 +29,11 @@ options = {}) {
28
29
  item.status.write('debug', `${item.name} is skipping cache check, because caching is disabled`);
29
30
  return { cached: false, stateKey: exports.UNCACHED_STATE_KEY, resolved: declared, provable: false };
30
31
  }
31
- const { stateKey, stats: currentStats, resolved, provable, unmatched, } = await (0, state_key_1.computeStateKey)(item, defaultCacheMethod, environment);
32
+ const { stateKey, stats: currentStats, resolved, provable, unmatched, } = await (0, state_key_1.computeStateKey)(item, defaultCacheMethod, environment, options.memo);
33
+ // what depends on it takes its key from here
34
+ if (options.memo && !options.memo.has(item.id())) {
35
+ options.memo.set(item.id(), Promise.resolve({ stateKey, provable }));
36
+ }
32
37
  for (const src of unmatched) {
33
38
  item.status.write('warn', `src "${src.source}" matches no files`);
34
39
  }