hammerkit 1.27.0-next.3 → 1.27.0-next.30

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 (119) hide show
  1. package/build.schema.json +188 -0
  2. package/dist/cache/backends/local-cache-backend.js +6 -0
  3. package/dist/cache/backends/registry-cache-backend.js +2 -2
  4. package/dist/cache/explain.js +17 -0
  5. package/dist/cli.js +2 -0
  6. package/dist/docker/item-network.js +13 -5
  7. package/dist/executer/deadline.js +17 -3
  8. package/dist/executer/deferred-restore.js +36 -0
  9. package/dist/executer/docker-service.js +30 -8
  10. package/dist/executer/docker-task.js +21 -1
  11. package/dist/executer/event-cache.js +20 -2
  12. package/dist/executer/execute-work-service.js +2 -0
  13. package/dist/executer/execute-work-task.js +10 -3
  14. package/dist/executer/execution-steps.js +25 -4
  15. package/dist/executer/reset-work-tree.js +4 -0
  16. package/dist/executer/scheduler/enqueue-next.js +57 -6
  17. package/dist/executer/scheduler/state-key.js +42 -5
  18. package/dist/executer/watch-loop.js +4 -1
  19. package/dist/harness/connect.js +20 -6
  20. package/dist/harness/mcp-command.js +22 -6
  21. package/dist/harness/mcp.js +63 -114
  22. package/dist/harness/opencode.js +158 -50
  23. package/dist/harness/pi.js +29 -0
  24. package/dist/harness/sandbox-client.js +168 -17
  25. package/dist/harness/service-tools.js +141 -0
  26. package/dist/harness/task-tools.js +221 -0
  27. package/dist/kubernetes/apply.js +27 -16
  28. package/dist/kubernetes/await-running-state.js +59 -13
  29. package/dist/kubernetes/claim-layout.js +71 -0
  30. package/dist/kubernetes/cluster-state.js +245 -0
  31. package/dist/kubernetes/ensure-http-route.js +1 -1
  32. package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +35 -12
  33. package/dist/kubernetes/ensure-kubernetes-persistent-volume-claim-exists.js +1 -1
  34. package/dist/kubernetes/ensure-kubernetes-service-exists.js +6 -9
  35. package/dist/kubernetes/ensure-persistent-data.js +30 -20
  36. package/dist/kubernetes/follow-pod-logs.js +182 -0
  37. package/dist/kubernetes/get-service-ip.js +2 -2
  38. package/dist/kubernetes/job-placement.js +36 -12
  39. package/dist/kubernetes/job-secrets.js +33 -19
  40. package/dist/kubernetes/kept-claims.js +22 -10
  41. package/dist/kubernetes/network-policy.js +85 -27
  42. package/dist/kubernetes/remove-persistent-data.js +3 -1
  43. package/dist/kubernetes/resources.js +25 -3
  44. package/dist/kubernetes/restore-kubernetes-data.js +9 -3
  45. package/dist/kubernetes/sandbox-runner.js +628 -0
  46. package/dist/kubernetes/secrets.js +7 -11
  47. package/dist/kubernetes/volumes.js +6 -3
  48. package/dist/main.js +2 -0
  49. package/dist/network/egress-proxy.js +45 -6
  50. package/dist/optimizer/calculate-checksum.js +3 -2
  51. package/dist/optimizer/check-key.js +94 -0
  52. package/dist/optimizer/check-keys.js +64 -0
  53. package/dist/optimizer/get-work-cache-stats.js +17 -3
  54. package/dist/optimizer/work-task-cache-description.js +51 -1
  55. package/dist/planner/utils/append-work-dependencies.js +10 -5
  56. package/dist/planner/utils/append-work-service.js +19 -1
  57. package/dist/planner/utils/append-work-task.js +18 -3
  58. package/dist/planner/utils/get-work-runtime.js +6 -1
  59. package/dist/planner/utils/normalize-path.js +19 -1
  60. package/dist/planner/utils/parse-work-mount.js +7 -1
  61. package/dist/planner/utils/parse-work-port.js +4 -1
  62. package/dist/planner/utils/task-arguments.js +3 -0
  63. package/dist/planner/validate.js +22 -0
  64. package/dist/planner/work-network.js +3 -1
  65. package/dist/planner/work-runtime-aggregate.js +32 -0
  66. package/dist/planner/work-runtime-docker.js +2 -1
  67. package/dist/planner/work-runtime-kubernetes.js +124 -48
  68. package/dist/planner/work-runtime-local.js +2 -1
  69. package/dist/planner/work-task.js +6 -1
  70. package/dist/program.js +31 -10
  71. package/dist/remote/device-login.js +7 -2
  72. package/dist/remote/host-config.js +69 -3
  73. package/dist/remote/remote-commands.js +30 -7
  74. package/dist/remote/remote-work-commands.js +426 -45
  75. package/dist/remote/server-client.js +15 -2
  76. package/dist/remote/token-source.js +64 -26
  77. package/dist/run-program.js +9 -0
  78. package/dist/schema/build-file-base-task-schema.js +8 -1
  79. package/dist/schema/build-file-container-service-schema.js +12 -0
  80. package/dist/schema/build-file-container-task-schema.js +16 -1
  81. package/dist/schema/build-file-include-schema.js +18 -4
  82. package/dist/schema/build-file-kubernetes-service-schema.js +2 -0
  83. package/dist/schema/build-file-task-schema.js +5 -1
  84. package/dist/schema/capabilities-schema.js +56 -0
  85. package/dist/schema/reference-parser.js +120 -11
  86. package/dist/schema/schema-parser.js +92 -41
  87. package/dist/schema/work-scope-parser.js +33 -10
  88. package/dist/secrets/command-secret-provider.js +3 -1
  89. package/dist/secrets/github-app-token-command.js +3 -1
  90. package/dist/secrets/secret-catalog.js +43 -19
  91. package/dist/server/accounts-api.js +3 -1
  92. package/dist/server/approvals-api.js +1 -1
  93. package/dist/server/checks-api.js +83 -0
  94. package/dist/server/checks.js +116 -0
  95. package/dist/server/definitions-api.js +66 -3
  96. package/dist/server/endpoints.js +739 -0
  97. package/dist/server/executor.js +57 -5
  98. package/dist/server/kubernetes-backend.js +367 -65
  99. package/dist/server/kubernetes-sandbox-storage.js +48 -3
  100. package/dist/server/oauth.js +12 -1
  101. package/dist/server/plan.js +188 -103
  102. package/dist/server/policy.js +32 -2
  103. package/dist/server/repo-access.js +5 -0
  104. package/dist/server/run-plan.js +50 -19
  105. package/dist/server/run-request.js +58 -3
  106. package/dist/server/run-worker.js +245 -12
  107. package/dist/server/runs.js +410 -27
  108. package/dist/server/sandbox-pod.js +28 -0
  109. package/dist/server/sandbox-storage.js +23 -0
  110. package/dist/server/sandboxes-api.js +10 -4
  111. package/dist/server/sandboxes.js +8 -0
  112. package/dist/server/server-command.js +39 -20
  113. package/dist/server/server-config.js +255 -14
  114. package/dist/server/server.js +269 -15
  115. package/dist/server/service-runs.js +82 -0
  116. package/dist/utils/abort-on-signals.js +46 -6
  117. package/dist/utils/tar.js +36 -0
  118. package/dist/utils/terminal-prompt.js +53 -0
  119. package/package.json +1 -1
@@ -1,15 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.refuseArguments = exports.planRun = exports.getRunScopes = void 0;
3
+ exports.describeArguments = exports.refuseArguments = exports.planRun = exports.getRunScopes = void 0;
4
4
  const parse_label_arguments_1 = require("../parser/parse-label-arguments");
5
5
  const plan_1 = require("./plan");
6
6
  function labels(values) {
7
7
  return values && values.length > 0 ? (0, parse_label_arguments_1.parseLabelArguments)(values) : undefined;
8
8
  }
9
- // The tasks in the order they were named, one scope each; by labels when none is.
9
+ // The tasks in the order they were named, one scope each; by labels when none is. A
10
+ // service run is one scope of its services.
10
11
  // The arguments, if any, go to the one task named (the request allows no more). That
11
12
  // task runs uncached unless the request names a `cache` method, as `run --cache` does.
12
- function getRunScopes(tasks, options) {
13
+ function getRunScopes(tasks, options, services = []) {
14
+ if (services.length > 0) {
15
+ return [{ name: services.join(', '), scope: { serviceNames: services, environmentName: options.env ?? null } }];
16
+ }
13
17
  const args = options.args && options.args.length > 0
14
18
  ? { values: options.args, services: false, cache: options.cache !== undefined }
15
19
  : undefined;
@@ -37,7 +41,7 @@ exports.getRunScopes = getRunScopes;
37
41
  */
38
42
  async function planRun(bundle, run, environment, policy) {
39
43
  const plan = {};
40
- for (const { scope } of getRunScopes(run.tasks, run.options)) {
44
+ for (const { scope } of getRunScopes(run.tasks, run.options, run.up?.services ?? run.services)) {
41
45
  Object.assign(plan, (await (0, plan_1.planBundle)(bundle, environment, { scope, policy })).plan);
42
46
  }
43
47
  return {
@@ -46,26 +50,16 @@ async function planRun(bundle, run, environment, policy) {
46
50
  };
47
51
  }
48
52
  exports.planRun = planRun;
49
- // Variables of a server that hold no credential (FR-008): a task may read these and
50
- // still take arguments.
51
- const PLAIN_SERVER_ENVS = ['PATH', 'HOME', 'CI'];
52
- function readsOf(entry) {
53
- return [
54
- ...entry.secrets.map((secret) => `the secret ${secret.target}`),
55
- ...(entry.serverEnvs ?? [])
56
- .filter((env) => !PLAIN_SERVER_ENVS.includes(env.toUpperCase()))
57
- .map((env) => `$${env} of the server`),
58
- ];
59
- }
60
53
  /**
61
54
  * Why a run may not pass its arguments, or null when it may. They go to a task, never a
62
55
  * service, and never to a run in which an item reads a secret or takes a variable of the
63
56
  * server into its `envs`: an argument can make the command print what the task holds,
64
57
  * and what its dependencies write into their outputs or its services serve, past the
65
58
  * masking of secrets. So a shell on a server runs with no account and no credential of
66
- * the server.
59
+ * the server. Under `runs.args: declared` they go only to a task that says `args: true`,
60
+ * so a task nobody meant to take them never does.
67
61
  */
68
- function refuseArguments(run, plan) {
62
+ function refuseArguments(run, plan, policy = {}) {
69
63
  if (!run.options.args || run.options.args.length === 0) {
70
64
  return null;
71
65
  }
@@ -75,14 +69,21 @@ function refuseArguments(run, plan) {
75
69
  if (!entry || entry.kind !== 'task') {
76
70
  return `${name} is not a task of this version`;
77
71
  }
78
- const reads = readsOf(entry);
72
+ // planning the run refuses it before, this keeps the check with the others
73
+ if (entry.args === false) {
74
+ return `${name} takes no arguments (args: false)`;
75
+ }
76
+ if (policy.args === 'declared' && entry.args !== true) {
77
+ return `${name} takes no arguments on this server: it takes them only for tasks that say args: true (runs.args: declared)`;
78
+ }
79
+ const reads = (0, plan_1.getCredentialReads)(entry);
79
80
  if (reads.length > 0) {
80
81
  return `${name} reads ${reads.join(', ')}, it takes no arguments on a server: an argument could make it print them`;
81
82
  }
82
83
  // what a dependency or a service of the run holds reaches the task through its outputs or
83
84
  // its answers, where an argument can print it in a form no mask knows (`rev token.txt`)
84
85
  for (const [other, item] of Object.entries(plan).sort(([a], [b]) => a.localeCompare(b))) {
85
- const held = (other === name ? [] : readsOf(item)).join(', ');
86
+ const held = (other === name ? [] : (0, plan_1.getCredentialReads)(item)).join(', ');
86
87
  if (held.length > 0) {
87
88
  return `${name} takes no arguments on a server: ${other} in its run reads ${held}, and an argument could make ${name} print what ${other} hands on`;
88
89
  }
@@ -90,4 +91,34 @@ function refuseArguments(run, plan) {
90
91
  return null;
91
92
  }
92
93
  exports.refuseArguments = refuseArguments;
94
+ /**
95
+ * For each task of a version, whether a run of it alone may pass arguments: null when it may,
96
+ * else why not, as {@link refuseArguments} answers a run. The plan is the one of the whole
97
+ * version: a task's run is the task with its dependencies and services.
98
+ */
99
+ function describeArguments(plan, policy = {}) {
100
+ const described = {};
101
+ for (const [name, entry] of Object.entries(plan)) {
102
+ if (entry.kind !== 'task') {
103
+ continue;
104
+ }
105
+ const run = {};
106
+ const visit = (item) => {
107
+ if (run[item] || !plan[item]) {
108
+ return;
109
+ }
110
+ run[item] = plan[item];
111
+ for (const next of [...plan[item].deps, ...plan[item].needs]) {
112
+ visit(next);
113
+ }
114
+ };
115
+ visit(name);
116
+ described[name] =
117
+ entry.command === null
118
+ ? `${name} has no command to take arguments`
119
+ : refuseArguments({ tasks: [name], options: { args: ['-'] } }, run, policy);
120
+ }
121
+ return described;
122
+ }
123
+ exports.describeArguments = describeArguments;
93
124
  //# sourceMappingURL=run-plan.js.map
@@ -1,12 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runRequestSchema = exports.runOptionsSchema = exports.MAX_ARGS_BYTES = exports.MAX_ARGS = void 0;
3
+ exports.runRequestSchema = exports.runOptionsSchema = exports.MAX_ARGS_BYTES = exports.MAX_ARGS = exports.MAX_ENDPOINTS = exports.SERVICE_RUN_NAME_PATTERN = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const units_1 = require("../utils/units");
6
6
  const bundle_1 = require("../remote/bundle");
7
7
  const repo_1 = require("./repo");
8
8
  const runs_1 = require("./runs");
9
9
  const name = zod_1.z.string().min(1).max(200);
10
+ // The name of a service run, one DNS label: the hosts of its endpoints carry it, and a run of
11
+ // the same caller with the same name replaces it
12
+ exports.SERVICE_RUN_NAME_PATTERN = /^[a-z0-9]([a-z0-9-]{0,38}[a-z0-9])?$/;
13
+ // endpoints one service run may publish
14
+ exports.MAX_ENDPOINTS = 5;
10
15
  // what one run may pass after `--`
11
16
  exports.MAX_ARGS = 100;
12
17
  exports.MAX_ARGS_BYTES = 16 * 1024;
@@ -38,6 +43,8 @@ exports.runOptionsSchema = zod_1.z
38
43
  cacheReadOnly: zod_1.z.boolean().optional(),
39
44
  // as --timeout
40
45
  timeout: duration.optional(),
46
+ // as --max-timeout: caps every task, also one with a longer timeout of its own
47
+ maxTimeout: duration.optional(),
41
48
  // as --no-skip-deps when false
42
49
  skipDeps: zod_1.z.boolean().optional(),
43
50
  // as the text after `--`: appended to the last command of the one task the run
@@ -87,18 +94,66 @@ exports.runRequestSchema = zod_1.z
87
94
  zod_1.z.object({ type: zod_1.z.literal('upload'), snapshot: zod_1.z.string().regex(bundle_1.HASH_PATTERN) }).strict(),
88
95
  // a sandbox of the caller's, see POST /v1/sandboxes
89
96
  zod_1.z
90
- .object({ type: zod_1.z.literal('sandbox'), id: zod_1.z.string().regex(runs_1.SANDBOX_ID_PATTERN, 'expected a sandbox id') })
97
+ .object({
98
+ type: zod_1.z.literal('sandbox'),
99
+ id: zod_1.z.string().regex(runs_1.SANDBOX_ID_PATTERN, 'expected a sandbox id'),
100
+ // a check: run on a copy of the files a push of the sandbox would commit, so what
101
+ // passes counts for that commit (remote verify)
102
+ check: zod_1.z.literal(true).optional(),
103
+ })
91
104
  .strict(),
92
105
  ]),
93
106
  options: exports.runOptionsSchema.default({}),
107
+ // a service run: these services of the build file, started as `hammerkit up` starts
108
+ // them and kept up until `ttl` ends or the run is cancelled, instead of tasks
109
+ services: zod_1.z.array(name).max(20).default([]),
110
+ // how long the services stay once they are ready, whether they sleep in between or not;
111
+ // `runs.up.ttl` when left out. At its end they are removed.
112
+ ttl: duration.optional(),
113
+ // how long the services stay up without a request to one of the endpoints, then they
114
+ // sleep until the next request wakes them; `runs.up.sleepTtl` when left out
115
+ sleepTtl: duration.optional(),
116
+ // a second service run of the caller with the same name replaces the first; the branch
117
+ // of the source when left out
118
+ name: zod_1.z
119
+ .string()
120
+ .regex(exports.SERVICE_RUN_NAME_PATTERN, 'expected lower case letters, digits and -, at most 40, as a DNS label')
121
+ .optional(),
122
+ // ports of the run's services published at hosts of their own, behind the server's sign-in
123
+ endpoints: zod_1.z
124
+ .array(zod_1.z.object({ service: name, port: zod_1.z.number().int().min(1).max(65535) }).strict())
125
+ .max(exports.MAX_ENDPOINTS)
126
+ .default([]),
94
127
  })
95
128
  .strict()
96
- .refine((request) => request.tasks.length > 0 || (request.options.filter ?? []).length > 0, {
129
+ .refine((request) => request.services.length > 0 || request.tasks.length > 0 || (request.options.filter ?? []).length > 0, {
97
130
  message: 'name at least one task, or filter by labels',
98
131
  path: ['tasks'],
99
132
  })
133
+ .refine((request) => request.services.length === 0 || request.tasks.length === 0, {
134
+ message: 'a run names tasks or services, not both',
135
+ path: ['services'],
136
+ })
137
+ .refine((request) => request.services.length === 0 ||
138
+ ((request.options.filter ?? []).length === 0 && (request.options.exclude ?? []).length === 0), { message: 'a service run names its services, labels do not select them', path: ['options', 'filter'] })
139
+ .refine((request) => request.services.length === 0 || (request.options.args ?? []).length === 0, {
140
+ message: 'a service run takes no arguments',
141
+ path: ['options', 'args'],
142
+ })
143
+ .refine((request) => request.services.length > 0 ||
144
+ (request.ttl === undefined &&
145
+ request.sleepTtl === undefined &&
146
+ request.name === undefined &&
147
+ request.endpoints.length === 0), { message: 'ttl, sleepTtl, name and endpoints are for a run of services', path: ['services'] })
148
+ .refine((request) => new Set(request.endpoints.map((endpoint) => `${endpoint.service}:${endpoint.port}`)).size ===
149
+ request.endpoints.length, { message: 'an endpoint is named twice', path: ['endpoints'] })
100
150
  .refine((request) => (request.options.args ?? []).length === 0 || request.tasks.length === 1, {
101
151
  message: 'arguments go to one task, name exactly one in tasks',
102
152
  path: ['options', 'args'],
153
+ })
154
+ .refine((request) => request.source.type !== 'sandbox' || !request.source.check || (request.options.args ?? []).length === 0, { message: 'a check runs the task as the build file says, it takes no arguments', path: ['options', 'args'] })
155
+ .refine((request) => request.source.type !== 'sandbox' || !request.source.check || request.services.length === 0, {
156
+ message: 'a check runs tasks, a run of services keeps them up and passes nothing',
157
+ path: ['services'],
103
158
  });
104
159
  //# sourceMappingURL=run-request.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.workerMain = exports.taskCauses = exports.runTasks = exports.getRunNetwork = exports.workerInputSchema = void 0;
3
+ exports.workerMain = exports.readInput = exports.taskCauses = exports.describeFailures = exports.runTasks = 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");
@@ -21,6 +21,10 @@ const network_schema_1 = require("../schema/network-schema");
21
21
  const work_network_1 = require("../planner/work-network");
22
22
  const run_plan_1 = require("./run-plan");
23
23
  const runs_1 = require("./runs");
24
+ const resources_1 = require("../kubernetes/resources");
25
+ const cluster_state_1 = require("../kubernetes/cluster-state");
26
+ const kubernetes_instance_1 = require("../kubernetes/kubernetes-instance");
27
+ const check_keys_1 = require("../optimizer/check-keys");
24
28
  // one log line of a task is cut here, the registry caps how many events a run keeps
25
29
  const MAX_LOG_TEXT = 8 * 1024;
26
30
  /**
@@ -49,6 +53,9 @@ exports.workerInputSchema = zod_1.z
49
53
  kubernetes: zod_1.z
50
54
  .custom((value) => typeof value === 'object' && value !== null && value.type === 'kubernetes')
51
55
  .optional(),
56
+ // on the server's docker host, the runtime a container that adds capabilities runs in
57
+ // (`backend.capabilities.runtime`)
58
+ capabilityRuntime: zod_1.z.string().min(1).optional(),
52
59
  // the most any task or service reaches besides its needs, as `--network`: `runs.network`
53
60
  network: network_schema_1.networkSchema.optional(),
54
61
  // for a run on a sandbox, `sandboxes.network`: the run gets the lower of the two
@@ -59,6 +66,17 @@ exports.workerInputSchema = zod_1.z
59
66
  .string()
60
67
  .regex(/^[a-z]+-[0-9a-f]{24}$/)
61
68
  .optional(),
69
+ // a service run: the services it keeps up instead of tasks, and the ports the server
70
+ // reaches as endpoints
71
+ up: zod_1.z
72
+ .object({
73
+ services: zod_1.z.array(zod_1.z.string()).min(1),
74
+ endpoints: zod_1.z.array(zod_1.z.object({ service: zod_1.z.string(), port: zod_1.z.number().int() }).strict()),
75
+ })
76
+ .strict()
77
+ .optional(),
78
+ // the run's results count as checks: report each task's check key before it runs
79
+ check: zod_1.z.boolean().optional(),
62
80
  })
63
81
  .strict();
64
82
  // The ceiling of the run, combined here and not in the server: the lower of two allow lists
@@ -68,6 +86,60 @@ function getRunNetwork(input) {
68
86
  return input.sandboxNetwork ? (0, work_network_1.limitNetwork)((0, work_network_1.parseWorkNetwork)(input.sandboxNetwork), runs) : runs;
69
87
  }
70
88
  exports.getRunNetwork = getRunNetwork;
89
+ /**
90
+ * The ports of the services of a service run as the server publishes them: on a host the
91
+ * ports of endpoints go to a free port of the loopback interface, where only the server
92
+ * reaches them, and no other port goes to the host, whatever the build file says (`up`
93
+ * would publish them on every interface, where two runs collide). On a cluster nothing is
94
+ * published, the server reaches the Service of each.
95
+ */
96
+ function publishEndpoints(workTree, endpoints, cluster) {
97
+ for (const service of Object.values(workTree.services)) {
98
+ if (!('ports' in service.data)) {
99
+ continue;
100
+ }
101
+ service.data.ports = service.data.ports.map((port) => !cluster &&
102
+ endpoints.some((endpoint) => endpoint.service === service.name && endpoint.port === port.containerPort)
103
+ ? { containerPort: port.containerPort, hostPort: 0, hostIp: '127.0.0.1' }
104
+ : { containerPort: port.containerPort, hostPort: null });
105
+ }
106
+ }
107
+ exports.publishEndpoints = publishEndpoints;
108
+ // Where the server reaches each endpoint, once every service the run keeps up is ready;
109
+ // null until then.
110
+ function getEndpointTargets(workTree, up, kubernetes) {
111
+ const services = Object.values(workTree.services).filter((service) => up.services.includes(service.name));
112
+ if (services.length < up.services.length || services.some((service) => service.state.current.type !== 'running')) {
113
+ return null;
114
+ }
115
+ const targets = [];
116
+ for (const endpoint of up.endpoints) {
117
+ const service = services.find((candidate) => candidate.name === endpoint.service);
118
+ const state = service?.state.current;
119
+ if (!service || state?.type !== 'running') {
120
+ return null;
121
+ }
122
+ if (kubernetes) {
123
+ targets.push({
124
+ service: endpoint.service,
125
+ port: endpoint.port,
126
+ address: { host: `${(0, resources_1.getResourceName)(service, kubernetes)}.${kubernetes.namespace}.svc`, port: endpoint.port },
127
+ });
128
+ continue;
129
+ }
130
+ const published = state.published?.find((candidate) => candidate.containerPort === endpoint.port);
131
+ if (!published) {
132
+ throw new Error(`${endpoint.service} is up, but its port ${endpoint.port} was not published`);
133
+ }
134
+ targets.push({
135
+ service: endpoint.service,
136
+ port: endpoint.port,
137
+ address: { host: published.host, port: published.port },
138
+ });
139
+ }
140
+ return targets;
141
+ }
142
+ exports.getEndpointTargets = getEndpointTargets;
71
143
  /**
72
144
  * Executes the tasks of a run with the engine of `hammerkit run`. The caller says which
73
145
  * process this is in: it reports what happens through `emit` and stops when `abortCtrl`
@@ -75,7 +147,9 @@ exports.getRunNetwork = getRunNetwork;
75
147
  */
76
148
  async function runTasks(input, cwd, processEnvs, abortCtrl,
77
149
  // whether the run was stopped from outside; the engine aborts too when a task fails
78
- cancelled, emit) {
150
+ cancelled, emit,
151
+ // a service run: its services are ready, at these addresses
152
+ up) {
79
153
  const secrets = (0, redact_1.createSecretRegistry)();
80
154
  const status = (0, work_item_status_1.statusConsole)((0, empty_writable_1.emptyWritable)(), secrets);
81
155
  // the process starts in a directory of the server, the tasks work on the run's files
@@ -84,7 +158,12 @@ cancelled, emit) {
84
158
  // is what is streamed: it is redacted with the secrets of this run
85
159
  const environment = {
86
160
  processEnvs,
87
- hosted: { catalog: input.catalog, accounts: input.accounts, kubernetes: input.kubernetes },
161
+ hosted: {
162
+ catalog: input.catalog,
163
+ accounts: input.accounts,
164
+ kubernetes: input.kubernetes,
165
+ capabilityRuntime: input.capabilityRuntime,
166
+ },
88
167
  abortCtrl,
89
168
  cwd: work,
90
169
  file: (0, get_file_context_1.getFileContext)(work),
@@ -117,15 +196,27 @@ cancelled, emit) {
117
196
  const options = input.options;
118
197
  const execOptions = {
119
198
  logMode: 'live',
120
- cacheDefault: options.cache ?? 'checksum',
199
+ // a check takes a cached result only for the same files, never for the same dates
200
+ cacheDefault: input.check && options.cache === 'modify-date' ? 'checksum' : options.cache ?? 'checksum',
121
201
  cacheReadOnly: options.cacheReadOnly ?? false,
122
202
  timeout: options.timeout ? (0, units_1.parseDuration)(options.timeout) : null,
203
+ maxTimeout: options.maxTimeout ? (0, units_1.parseDuration)(options.maxTimeout) : null,
123
204
  skipDeps: options.skipDeps !== false,
124
205
  network: getRunNetwork(input),
125
206
  // why a task was not taken from the cache goes into its result
126
207
  explain: true,
127
208
  };
128
- for (const { name, scope } of (0, run_plan_1.getRunScopes)(input.tasks, options)) {
209
+ const scopes = (0, run_plan_1.getRunScopes)(input.tasks, options, input.up?.services);
210
+ if (input.check) {
211
+ // the files as they are before any task wrote to them, a second task's too: a first
212
+ // one that formats or generates into src would key it by files no commit holds
213
+ for (const { scope } of scopes) {
214
+ for (const [task, { key }] of Object.entries(await (0, check_keys_1.getCheckKeys)(input.entry, environment, scope))) {
215
+ emit({ type: 'key', task, key });
216
+ }
217
+ }
218
+ }
219
+ for (const { name, scope } of scopes) {
129
220
  const { ctx, scope: parsed } = await (0, schema_parser_1.createParseContext)(input.entry, environment, {
130
221
  directory: input.directory,
131
222
  });
@@ -134,19 +225,57 @@ cancelled, emit) {
134
225
  if (input.volumeScope && !input.kubernetes) {
135
226
  (0, hosted_volumes_1.scopeServiceVolumes)(workTree, input.volumeScope);
136
227
  }
228
+ if (input.up) {
229
+ publishEndpoints(workTree, input.up.endpoints, !!input.kubernetes);
230
+ }
137
231
  const cli = (0, cli_1.getCli)(workTree, environment);
138
- if (cli.tasks().length === 0) {
232
+ if (cli.tasks().length === 0 && (!input.up || cli.services().length === 0)) {
139
233
  throw new Error(`no tasks found for ${name}`);
140
234
  }
141
- const execution = cli.exec({ ...execOptions, workers: cli.defaultWorkers() });
235
+ const execution = input.up
236
+ ? cli.up({ ...execOptions, workers: cli.defaultWorkers(), daemon: false, wait: 'ready' })
237
+ : cli.exec({ ...execOptions, workers: cli.defaultWorkers() });
142
238
  const seen = new Map();
143
- const report = () => reportTasks(emit, execution.state.current, seen, (text) => secrets.redact(text));
239
+ const failedServices = new Set();
240
+ let ready = false;
241
+ const report = () => {
242
+ reportTasks(emit, execution.state.current, seen, (text) => secrets.redact(text));
243
+ reportServiceFailures(emit, execution.state.current, failedServices, (text) => secrets.redact(text));
244
+ if (input.up && up && !ready) {
245
+ const targets = getEndpointTargets(execution.state.current, input.up, input.kubernetes);
246
+ if (targets) {
247
+ ready = true;
248
+ up(targets);
249
+ }
250
+ }
251
+ };
144
252
  const watch = execution.state.on('run-events', report);
145
253
  try {
146
254
  const result = await execution.start();
147
255
  report();
256
+ // on a cluster the services stay in their Deployments once they are ready, until
257
+ // the namespace of the run goes
258
+ if (input.up && input.kubernetes && ready && result.success && !abortCtrl.signal.aborted) {
259
+ await new Promise((resolve) => abortCtrl.signal.addEventListener('abort', () => resolve(), { once: true }));
260
+ return;
261
+ }
262
+ if (input.up && ready) {
263
+ if (!abortCtrl.signal.aborted) {
264
+ throw new Error('the services stopped on their own');
265
+ }
266
+ return;
267
+ }
148
268
  if (!result.success) {
149
- throw new Error(cancelled() ? 'the run was cancelled' : `${name} failed`);
269
+ if (cancelled()) {
270
+ throw new Error('the run was cancelled');
271
+ }
272
+ if (input.kubernetes) {
273
+ // what the cluster said, before the server removes what the run made
274
+ await reportClusterState(input.kubernetes, execution.state.current, !!input.up, emit, (text) => secrets.redact(text));
275
+ }
276
+ // a service run names what failed and why: its services have no results of their own
277
+ const failures = input.up ? describeFailures(execution.state.current, (text) => secrets.redact(text)) : [];
278
+ throw new Error(failures.length > 0 ? failures.join('; ').slice(0, runs_1.MAX_TASK_ERROR) : `${name} failed`);
150
279
  }
151
280
  }
152
281
  finally {
@@ -159,6 +288,82 @@ cancelled, emit) {
159
288
  }
160
289
  }
161
290
  exports.runTasks = runTasks;
291
+ // What the cluster said of a run that failed, into its log: the server removes its pods
292
+ // once it ended.
293
+ async function reportClusterState(kubernetes, workTree, ownNamespace, emit, redact) {
294
+ let lines;
295
+ try {
296
+ lines = await (0, cluster_state_1.readClusterState)((0, kubernetes_instance_1.createKubernetesInstances)(kubernetes), kubernetes, workTree, ownNamespace);
297
+ }
298
+ catch (e) {
299
+ lines = [`it could not be read: ${(0, log_1.getErrorMessage)(e)}`];
300
+ }
301
+ if (lines.length === 0) {
302
+ return;
303
+ }
304
+ for (const text of [`what the cluster said in ${kubernetes.namespace}:`, ...lines.map((line) => ` ${line}`)]) {
305
+ emit({ type: 'log', task: 'hammerkit', stream: 'stderr', text: redact(text).slice(0, MAX_LOG_TEXT) });
306
+ }
307
+ }
308
+ // A service has no result of its own in the run: why it failed goes into the log, under
309
+ // its name, once.
310
+ function reportServiceFailures(emit, workTree, said, redact) {
311
+ for (const service of (0, plan_work_tasks_1.iterateWorkServices)(workTree)) {
312
+ const state = service.state.current;
313
+ if (state.type === 'error' && !said.has(service.name)) {
314
+ said.add(service.name);
315
+ if (lastError(service) === state.errorMessage) {
316
+ // the runtime said it already
317
+ continue;
318
+ }
319
+ emit({
320
+ type: 'log',
321
+ task: service.name,
322
+ stream: 'stderr',
323
+ text: redact(state.errorMessage).slice(0, MAX_LOG_TEXT),
324
+ });
325
+ }
326
+ }
327
+ }
328
+ // The last error the runtime of an item wrote to its status.
329
+ function lastError(item) {
330
+ let last;
331
+ for (const message of item.status.read()) {
332
+ if (message.level === 'error') {
333
+ last = message.message;
334
+ }
335
+ }
336
+ return last;
337
+ }
338
+ /**
339
+ * What failed in a run, one line each, for its error: the services and tasks that failed,
340
+ * with their reason. Those that only stopped because another failed are left out.
341
+ */
342
+ function describeFailures(workTree, redact) {
343
+ const failures = [];
344
+ for (const service of (0, plan_work_tasks_1.iterateWorkServices)(workTree)) {
345
+ const state = service.state.current;
346
+ if (state.type === 'error') {
347
+ failures.push(`${service.name}: ${redact(state.errorMessage)}`);
348
+ }
349
+ else if (state.type === 'end' && state.reason === 'crash') {
350
+ // on Docker a service that does not get ready ends, and its runtime says why
351
+ const why = lastError(service);
352
+ failures.push(`${service.name}: ${why ? redact(why) : 'it stopped on its own'}`);
353
+ }
354
+ }
355
+ for (const task of (0, plan_work_tasks_1.iterateWorkTasks)(workTree)) {
356
+ const state = task.state.current;
357
+ if (state.type === 'error') {
358
+ failures.push(`${task.name}: ${redact(state.errorMessage)}`);
359
+ }
360
+ else if (state.type === 'crash') {
361
+ failures.push(`${task.name}: exit code ${state.exitCode}`);
362
+ }
363
+ }
364
+ return failures;
365
+ }
366
+ exports.describeFailures = describeFailures;
162
367
  // Why a task ran, as a task event carries it: the first causes, masked and cut short.
163
368
  function taskCauses(causes, redact) {
164
369
  return causes?.length
@@ -217,9 +422,13 @@ redact) {
217
422
  // The run is the first line of the standard input. The server keeps it open and writes
218
423
  // `cancel` on it to stop the run: that works the same on every platform, where a signal
219
424
  // on Windows ends the process without letting it stop its tasks.
425
+ // `onEnd` is told when the input ends before the run does: the server is gone.
220
426
  function readInput(stdin, cancel) {
221
427
  const lines = (0, readline_1.createInterface)({ input: stdin, crlfDelay: Infinity });
222
428
  let first;
429
+ let closing = false;
430
+ let ended = false;
431
+ let onEnd;
223
432
  const input = new Promise((resolve) => {
224
433
  lines.on('line', (line) => {
225
434
  if (first === undefined) {
@@ -230,10 +439,29 @@ function readInput(stdin, cancel) {
230
439
  cancel();
231
440
  }
232
441
  });
233
- lines.on('close', () => resolve(first ?? ''));
442
+ lines.on('close', () => {
443
+ resolve(first ?? '');
444
+ if (!closing) {
445
+ ended = true;
446
+ onEnd?.();
447
+ }
448
+ });
234
449
  });
235
- return { input, close: () => lines.close() };
450
+ return {
451
+ input,
452
+ close: () => {
453
+ closing = true;
454
+ lines.close();
455
+ },
456
+ onEnd: (listener) => {
457
+ onEnd = listener;
458
+ if (ended) {
459
+ listener();
460
+ }
461
+ },
462
+ };
236
463
  }
464
+ exports.readInput = readInput;
237
465
  /**
238
466
  * The process of one run: reads its input, executes the tasks in the current directory
239
467
  * and reports as lines of JSON. It exits 0 when it could say how the run ended, and the
@@ -260,8 +488,13 @@ async function workerMain(stdin, stdout, processEnvs, cwd, abortCtrl) {
260
488
  say({ kind: 'result', ok: false, error: `the run could not be read: ${e instanceof Error ? e.message : e}` });
261
489
  return;
262
490
  }
491
+ if (input.up) {
492
+ // services stay until they are stopped: without the server that would stop them, at
493
+ // their ttl or on a cancel, nothing would
494
+ reader.onEnd(stop);
495
+ }
263
496
  try {
264
- await runTasks(input, cwd, processEnvs, abortCtrl, () => cancelled, (event) => say({ kind: 'event', event }));
497
+ await runTasks(input, cwd, processEnvs, abortCtrl, () => cancelled, (event) => say({ kind: 'event', event }), (targets) => say({ kind: 'up', targets }));
265
498
  say({ kind: 'result', ok: true });
266
499
  }
267
500
  catch (e) {