@lchase/kiln 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,10 @@ import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readdirSync
4
4
  import { dirname, join, resolve, sep } from "node:path";
5
5
  import { z } from "zod";
6
6
  import { authenticationFailureLine } from "./auth.js";
7
- import { appendEvent } from "./events.js";
7
+ import { attemptsFor } from "./config.js";
8
+ import { appendEvent, readEvents } from "./events.js";
9
+ import { decideFailure, failureFingerprint, isSandboxStartFlake } from "./failure.js";
10
+ import { fingerprintCount } from "./rails.js";
8
11
  import { jobBasename } from "./paths.js";
9
12
  import { FAILURE_REASONS } from "./reconcile.js";
10
13
  /**
@@ -88,25 +91,23 @@ export function resetAttemptLedger(kilnDir, jobId, runId) {
88
91
  */
89
92
  export function recoverRuns(config, jobs, options) {
90
93
  const notes = [];
94
+ const events = readEvents(config.kilnDir, { warn: () => { } });
91
95
  for (const job of jobs) {
92
96
  if (job.error)
93
97
  continue;
94
98
  const existing = readAttemptLedger(config.kilnDir, job.jobId);
95
99
  if (existing === "corrupt")
96
100
  continue;
97
- if (job.run?.sandboxError && job.run.status !== "alive") {
98
- suppressAutomaticRetry(config.kilnDir, job.jobId);
99
- continue;
100
- }
101
101
  const pending = pendingFailure(job, config, options);
102
102
  if (!pending)
103
103
  continue;
104
104
  if (pending.killPid !== undefined && !killTree(pending.killPid))
105
105
  continue;
106
- const recorded = recordFailure(config, job.jobId, existing, pending, options.now);
106
+ const recorded = recordFailure(config, job, existing, pending, options.now, events);
107
107
  if (recorded.created) {
108
108
  appendLog(job.logPath, recorded.ledger.attempts.at(-1));
109
109
  const runId = pending.runId ?? job.run?.runId;
110
+ const cost = job.run?.cost ?? { tokens: null, usd: null };
110
111
  appendEvent(config.kilnDir, {
111
112
  job_id: job.jobId,
112
113
  run_id: runId ?? null,
@@ -114,15 +115,31 @@ export function recoverRuns(config, jobs, options) {
114
115
  properties: {
115
116
  reason: pending.reason,
116
117
  ...(pending.detail ? { detail: pending.detail } : {}),
118
+ class: recorded.decision.class,
119
+ fingerprint: recorded.decision.fingerprint,
120
+ tokens: cost.tokens,
121
+ usd: cost.usd,
117
122
  once: runId ? `run.failed:${runId}` : `run.failed:launch:${recorded.ledger.attempts.length}`,
118
123
  },
119
124
  });
125
+ if (recorded.decision.needsHuman) {
126
+ appendEvent(config.kilnDir, {
127
+ job_id: job.jobId,
128
+ run_id: runId ?? null,
129
+ event: "needs-human",
130
+ properties: {
131
+ reason: recorded.decision.needsHuman,
132
+ once: `needs-human:${recorded.decision.needsHuman}:${runId ?? recorded.ledger.attempts.length}`,
133
+ },
134
+ });
135
+ }
120
136
  notes.push({
121
137
  jobId: job.jobId,
122
138
  kind: kindFor(pending.reason),
123
139
  reason: pending.reason,
124
140
  attempt: recorded.ledger.attempts.length,
125
141
  ...(recorded.ledger.nextRetryAt ? { nextRetryAt: recorded.ledger.nextRetryAt } : {}),
142
+ ...(recorded.decision.needsHuman ? { needsHuman: true } : {}),
126
143
  });
127
144
  }
128
145
  const checkout = job.run?.checkout ?? config.repos[job.repo]?.path;
@@ -141,18 +158,6 @@ export function recoverRuns(config, jobs, options) {
141
158
  }
142
159
  return notes;
143
160
  }
144
- /** Drop a scheduled retry so a sandbox failure stays in Needs human. */
145
- function suppressAutomaticRetry(kilnDir, jobId) {
146
- const existing = readAttemptLedger(kilnDir, jobId);
147
- if (!existing || existing === "corrupt" || !existing.nextRetryAt)
148
- return;
149
- const written = {
150
- jobId: existing.jobId,
151
- attempts: existing.attempts,
152
- ...(existing.ignoredRunIds && existing.ignoredRunIds.length > 0 ? { ignoredRunIds: existing.ignoredRunIds } : {}),
153
- };
154
- writeLedger(kilnDir, written);
155
- }
156
161
  function pendingFailure(job, config, options) {
157
162
  if (job.run?.gateDecision === "reject")
158
163
  return undefined;
@@ -261,10 +266,24 @@ function readAuthLog(logPath) {
261
266
  closeSync(fd);
262
267
  }
263
268
  }
264
- function recordFailure(config, jobId, existing, pending, now) {
265
- const ledger = existing ?? { jobId, attempts: [] };
269
+ function recordFailure(config, job, existing, pending, now, events) {
270
+ const ledger = existing ?? { jobId: job.jobId, attempts: [] };
271
+ const text = failureText(job, pending);
272
+ const decision = decideFailure({
273
+ text,
274
+ operational: pending.reason === "stalled" ||
275
+ pending.reason === "orphaned" ||
276
+ pending.reason === "crashed" ||
277
+ pending.reason === "failed",
278
+ blocked: pending.reason === "auth-failed" ||
279
+ (Boolean(job.run?.sandboxError) && !isSandboxStartFlake(text)),
280
+ attempt: ledger.attempts.length + 1,
281
+ maxAttempts: attemptsFor(config, job.repo),
282
+ sameFingerprint: fingerprintCount(events, job.jobId, failureFingerprint(text)) + 1,
283
+ maxSameFingerprint: config.maxSameFingerprint,
284
+ });
266
285
  if (alreadyRecorded(ledger, pending))
267
- return { ledger, created: false };
286
+ return { ledger, created: false, decision };
268
287
  const attempt = {
269
288
  n: ledger.attempts.length + 1,
270
289
  at: now.toISOString(),
@@ -274,16 +293,25 @@ function recordFailure(config, jobId, existing, pending, now) {
274
293
  detail: pending.detail,
275
294
  };
276
295
  const attempts = [...ledger.attempts, attempt];
277
- const exhausted = attempts.length >= config.maxAttempts || pending.reason === "auth-failed";
278
- const next = exhausted ? undefined : new Date(now.getTime() + retryDelayMs(attempts.length)).toISOString();
296
+ const next = decision.retry ? new Date(now.getTime() + retryDelayMs(attempts.length)).toISOString() : undefined;
279
297
  const written = {
280
- jobId,
298
+ jobId: job.jobId,
281
299
  attempts,
282
300
  ...(next ? { nextRetryAt: next } : {}),
283
301
  ...(ledger.ignoredRunIds && ledger.ignoredRunIds.length > 0 ? { ignoredRunIds: ledger.ignoredRunIds } : {}),
284
302
  };
285
303
  writeLedger(config.kilnDir, written);
286
- return { ledger: written, created: true };
304
+ return { ledger: written, created: true, decision };
305
+ }
306
+ function failureText(job, pending) {
307
+ const parts = [pending.detail];
308
+ if (job.run?.sandboxError)
309
+ parts.push(job.run.sandboxError);
310
+ for (const event of job.run?.stepEvents ?? []) {
311
+ if (event.detail)
312
+ parts.push(event.detail);
313
+ }
314
+ return parts.filter((part) => part.length > 0).join("\n");
287
315
  }
288
316
  function alreadyRecorded(ledger, pending) {
289
317
  if (pending.runId)
package/dist/report.js CHANGED
@@ -159,6 +159,10 @@ export function statusDetails(job) {
159
159
  lines.push(`feedback ${job.feedbackRounds?.length}`);
160
160
  if (job.state === "needs-human" && job.feedbackReason)
161
161
  lines.push(`reason ${job.feedbackReason}`);
162
+ if (job.stopReason)
163
+ lines.push(`reason ${job.stopReason}`);
164
+ if (job.waitReason)
165
+ lines.push(job.waitReason);
162
166
  if (job.attemptCount > 0)
163
167
  lines.push(`attempts ${job.attemptCount}`);
164
168
  if (job.lastFailureReason)
@@ -167,8 +171,9 @@ export function statusDetails(job) {
167
171
  lines.push(AUTH_FAILED_LINE);
168
172
  if (job.nextRetryAt)
169
173
  lines.push(`next retry ${job.nextRetryAt}`);
170
- if (job.state === "dead" && job.attemptCount > 0)
174
+ if ((job.state === "dead" || job.stopReason) && job.attemptCount > 0 && job.gates.length === 0) {
171
175
  lines.push(`retry: kiln retry ${job.jobId}`);
176
+ }
172
177
  if (job.noReadableApproval)
173
178
  lines.push("no readable approval decision");
174
179
  if (job.cancelCommand)
@@ -186,6 +191,9 @@ export function statusDetails(job) {
186
191
  lines.push(`approve: kiln approve ${job.jobId}`);
187
192
  if (gate.choices.includes("reject"))
188
193
  lines.push(`reject: kiln reject ${job.jobId}`);
194
+ if (gate.choices.includes("approve") || gate.choices.includes("revise")) {
195
+ lines.push(`revise: kiln revise ${job.jobId} --note <text>`);
196
+ }
189
197
  }
190
198
  }
191
199
  return lines;
@@ -216,7 +224,7 @@ function toStatusJob(job, observation) {
216
224
  ...(job.findingsPath ? { findingsPath: job.findingsPath } : {}),
217
225
  launchFailed: job.launchFailed,
218
226
  attemptCount: job.attemptCount ?? 0,
219
- maxAttempts: observation.maxAttempts,
227
+ maxAttempts: job.attemptCap ?? observation.maxAttempts,
220
228
  ...(job.lastFailureReason ? { lastFailureReason: job.lastFailureReason } : {}),
221
229
  ...(job.nextRetryAt ? { nextRetryAt: job.nextRetryAt } : {}),
222
230
  invalidReason: job.error,
@@ -226,6 +234,7 @@ function toStatusJob(job, observation) {
226
234
  gates: [],
227
235
  canApprove: false,
228
236
  canReject: false,
237
+ canRevise: false,
229
238
  steps: progressFor(job),
230
239
  ...(job.deliver ? { deliver: job.deliver } : {}),
231
240
  ...(job.createdAt ? { createdAt: job.createdAt } : {}),
@@ -239,7 +248,7 @@ function toStatusJob(job, observation) {
239
248
  : job.feedbackAmbiguous
240
249
  ? "feedback-ambiguous"
241
250
  : undefined;
242
- const gates = state === "needs-human" && job.run && !feedbackReason ? job.run.gates.map(toGateView) : [];
251
+ const gates = state === "needs-human" && job.run && !feedbackReason && !job.stopReason ? job.run.gates.map(toGateView) : [];
243
252
  const prNumber = job.delivery?.prNumber;
244
253
  const runId = job.run?.runId;
245
254
  const showBranch = runId !== undefined && (state === "done" || state === "in-review" || feedbackReason !== undefined);
@@ -263,8 +272,12 @@ function toStatusJob(job, observation) {
263
272
  ...(job.findingsPath ? { findingsPath: job.findingsPath } : {}),
264
273
  launchFailed: job.launchFailed,
265
274
  attemptCount: job.attemptCount ?? 0,
266
- maxAttempts: observation.maxAttempts,
275
+ maxAttempts: job.attemptCap ?? observation.maxAttempts,
267
276
  ...(job.lastFailureReason ? { lastFailureReason: job.lastFailureReason } : {}),
277
+ ...(job.stopReason ? { stopReason: job.stopReason } : {}),
278
+ ...(observation.holdDetail && (state === "queued" || state === "blocked" || state === "failed")
279
+ ? { waitReason: observation.holdDetail }
280
+ : {}),
268
281
  ...(job.nextRetryAt && !job.retriesExhausted ? { nextRetryAt: job.nextRetryAt } : {}),
269
282
  ...(job.ledgerError ? { ledgerError: job.ledgerError } : {}),
270
283
  ...(job.feedbackLedgerError ? { feedbackLedgerError: job.feedbackLedgerError } : {}),
@@ -281,6 +294,7 @@ function toStatusJob(job, observation) {
281
294
  gates,
282
295
  canApprove: gates.some((gate) => gate.choices.includes("approve")),
283
296
  canReject: gates.some((gate) => gate.choices.includes("reject")),
297
+ canRevise: gates.some((gate) => gate.choices.includes("approve") || gate.choices.includes("revise")),
284
298
  steps: progressFor(job),
285
299
  ...(job.createdAt ? { createdAt: job.createdAt } : {}),
286
300
  ...(job.run?.startedAt ? { startedAt: job.run.startedAt } : {}),
@@ -402,7 +416,7 @@ export function renderTick(observation, actions, launches, delivers = [], findin
402
416
  : `findings ${recorded.jobId} missing`);
403
417
  }
404
418
  for (const note of notes) {
405
- const next = note.nextRetryAt ? ` next ${note.nextRetryAt}` : note.reason === "auth-failed" ? "" : " dead";
419
+ const next = note.nextRetryAt ? ` next ${note.nextRetryAt}` : note.needsHuman ? " needs human" : "";
406
420
  lines.push(`${note.kind} ${note.jobId} ${note.reason} attempt ${note.attempt}${next}`);
407
421
  }
408
422
  for (const note of feedbackNotes) {
package/dist/schemas.js CHANGED
@@ -36,6 +36,18 @@ export const RepoEntrySchema = z.object({
36
36
  * The local branch stays `yak/<run-id>`.
37
37
  */
38
38
  branch: z.string().min(1, "Branch template is empty.").optional(),
39
+ /** Overrides the global attempt cap for jobs in this repo. */
40
+ maxAttempts: z
41
+ .number()
42
+ .int("Max attempts must be a whole number.")
43
+ .min(1, "Max attempts must be at least 1.")
44
+ .optional(),
45
+ /** Overrides the global fix-round cap for jobs in this repo. */
46
+ maxFixRounds: z
47
+ .number()
48
+ .int("Max fix rounds must be a whole number.")
49
+ .min(1, "Max fix rounds must be at least 1.")
50
+ .optional(),
39
51
  });
40
52
  /** Repo map shared by the config file and the settings API. */
41
53
  export const KilnReposSchema = z.record(z.string().regex(REPO_NAME, "must start with a letter or digit and contain only letters, digits, dots, underscores, and hyphens"), RepoEntrySchema);
@@ -75,21 +87,34 @@ export const EditableSandboxSchema = SandboxSettingsSchema.pick({
75
87
  const WORKFLOW_KEY = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
76
88
  /** No yak event and no log output for this long stalls a running job. `<= 0` disables it. */
77
89
  export const DEFAULT_STALL_TIMEOUT_MS = 15 * 60 * 1000;
78
- /** Transient failures retry until this many attempts, then the job is dead. */
90
+ /** Transient failures retry until this many attempts, then the job needs a person. */
79
91
  export const DEFAULT_MAX_ATTEMPTS = 3;
80
92
  /** Pull request fix rounds before the job is needs-human (`feedback-exhausted`). */
81
93
  export const DEFAULT_MAX_FIX_ROUNDS = 2;
94
+ /** Identical failure fingerprints before the job needs a person. */
95
+ export const DEFAULT_MAX_SAME_FINGERPRINT = 3;
82
96
  /**
83
97
  * Milliseconds without a yak event or log output before a running job is
84
98
  * killed and retried. Default 15 minutes. `0` or negative disables stall detection.
85
99
  */
86
100
  const stallTimeoutMs = z.number().int("Stall timeout must be a whole number of milliseconds.").optional();
87
- /** Transient failures before the job is dead. Default 3. */
101
+ /** Transient failures before the job needs a person. Default 3. Hitting the cap is a budget failure. */
88
102
  const maxAttempts = z
89
103
  .number()
90
104
  .int("Max attempts must be a whole number.")
91
105
  .min(1, "Max attempts must be at least 1.")
92
106
  .optional();
107
+ /** Same failure fingerprint before Needs human. Default 3. */
108
+ const maxSameFingerprint = z
109
+ .number()
110
+ .int("Fingerprint limit must be a whole number.")
111
+ .min(1, "Fingerprint limit must be at least 1.")
112
+ .optional();
113
+ /**
114
+ * Dollars of recorded run cost allowed on the local calendar day.
115
+ * Omitted means no cap. `0` blocks new runs immediately.
116
+ */
117
+ const dailyDollarCap = z.number().nonnegative("Daily dollar cap cannot be negative.").optional();
93
118
  /**
94
119
  * Fix rounds for an open pull request before the job is needs-human with
95
120
  * reason `feedback-exhausted`. Default 2. Kiln does not merge.
@@ -109,6 +134,8 @@ export const EditableKilnSettingsSchema = z.object({
109
134
  stallTimeoutMs,
110
135
  maxAttempts,
111
136
  maxFixRounds,
137
+ maxSameFingerprint,
138
+ dailyDollarCap,
112
139
  sandbox: EditableSandboxSchema.optional(),
113
140
  });
114
141
  /** PUT /api/settings body. `revision` is the sha256 of the file the client loaded. */
@@ -121,6 +148,8 @@ export const KilnConfigFileSchema = z.object({
121
148
  stallTimeoutMs,
122
149
  maxAttempts,
123
150
  maxFixRounds,
151
+ maxSameFingerprint,
152
+ dailyDollarCap,
124
153
  adapter: z.enum(["mock", "claude-code"]).optional(),
125
154
  sandbox: SandboxSettingsSchema.optional(),
126
155
  /** Per-workflow sandbox override. A set field replaces the global one; an omitted field inherits. */
@@ -176,9 +205,10 @@ export const JobSchema = z.object({
176
205
  * `deliver=pr`. It does not enter `in-review`. A reject, a failure, a dead
177
206
  * process, and an ok run with no readable approval do not unblock an
178
207
  * after-merged dependent. A stacked job unblocks when its one upstream has
179
- * landed, which includes `in-review`. `dead` is also a job whose transient
180
- * failures have used `maxAttempts`. Those attempts live under the kiln
181
- * directory, not in this file.
208
+ * landed, which includes `in-review`. A job that has used its attempt cap,
209
+ * fix-round cap, or fingerprint breaker is `needs-human`. `dead` is a run
210
+ * whose process died and has not been recorded yet. Attempts live under
211
+ * the kiln directory, not in this file.
182
212
  */
183
213
  export const JOB_STATES = [
184
214
  "queued",