@kitman-sh/software-engineering 0.10.69 → 0.10.70

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.
package/dist/bundle.js CHANGED
@@ -13787,6 +13787,12 @@ var RunWaiting = Union2([
13787
13787
  name: String5.check(isMinLength(1)),
13788
13788
  verificationId: Id
13789
13789
  }),
13790
+ Struct({
13791
+ kind: Literal2("claim"),
13792
+ name: String5.check(isMinLength(1)),
13793
+ resource: String5.check(isMinLength(1)),
13794
+ holder: optionalKey2(Struct({ runId: Id, loopId: String5 }))
13795
+ }),
13790
13796
  Struct({
13791
13797
  kind: Literal2("queue"),
13792
13798
  name: String5.check(isMinLength(1)),
@@ -19249,7 +19255,10 @@ var PipelineStatus = Struct({
19249
19255
  source: Literals(["forge", "reported"]),
19250
19256
  jobs: ArraySchema(PipelineJob),
19251
19257
  readAt: optionalKey2(String5),
19252
- runnerChecks: optionalKey2(ArraySchema(String5))
19258
+ runnerChecks: optionalKey2(ArraySchema(String5)),
19259
+ approved: optionalKey2(String5),
19260
+ repository: optionalKey2(String5),
19261
+ target: optionalKey2(Struct({ branch: String5, head: String5, jobs: ArraySchema(PipelineJob) }))
19253
19262
  });
19254
19263
  var ReviewThread = Struct({
19255
19264
  id: String5,
@@ -19681,6 +19690,11 @@ var claim = (name, request) => gen2(function* () {
19681
19690
  return taken;
19682
19691
  return yield* new StepFailed({ step: name, reason: `${request.resource} is kept by ${holderName(taken.holder)}` });
19683
19692
  });
19693
+ var letGo = (name, held) => takeStep(name, "let_go", "claim", gen2(function* () {
19694
+ const deferred = letGoDeferred(name);
19695
+ yield* launch(deferred, (host, token) => host.claimResource({ step: name, resource: held.resource, action: "let_go", token }));
19696
+ yield* await_2(deferred);
19697
+ }));
19684
19698
 
19685
19699
  // ../../packages/core/src/repo-binding.ts
19686
19700
  var ForgeKinds = ["github", "gitlab", "forgejo", "gitea", "gitee", "bitbucket", "azure-devops"];
@@ -20277,7 +20291,7 @@ var scriptedHost = (script, engine, agentLaunches, childLaunches, waits, verifyL
20277
20291
  });
20278
20292
  return resolve(eventDeferred(launch.step), launch.token, event === undefined ? fail5(new WaitTimedOut({ step: launch.step, event: launch.event })) : succeed4(event));
20279
20293
  },
20280
- claimResource: (launch) => launch.action === "let_go" ? resolve(letGoDeferred(launch.step), launch.token, succeed4(null)) : resolve(claimDeferred(launch.step), launch.token, succeed4(script.claims?.[launch.step] ?? { status: "granted", resource: launch.resource })),
20294
+ claimResource: (launch) => flatMap3(script.claimed?.({ step: launch.step, resource: launch.resource, action: launch.action }) ?? undefined_2, (settled) => launch.action === "let_go" ? resolve(letGoDeferred(launch.step), launch.token, succeed4(null)) : resolve(claimDeferred(launch.step), launch.token, succeed4(settled ?? script.claims?.[launch.step] ?? { status: "granted", resource: launch.resource }))),
20281
20295
  startVerification: (launch) => {
20282
20296
  const scripted = script.verifications?.[launch.step];
20283
20297
  if (launch.measure === undefined)
@@ -20635,6 +20649,7 @@ var Input = Struct({
20635
20649
  head: optional2(String5),
20636
20650
  files: optional2(ArraySchema(String5)),
20637
20651
  since: optional2(String5),
20652
+ approved: optional2(String5),
20638
20653
  rebuttals: optional2(ArraySchema(Rebuttal)),
20639
20654
  outcome: optional2(LoopOutcome),
20640
20655
  claims: optional2(ArraySchema(String5)),
@@ -20644,9 +20659,13 @@ var Input = Struct({
20644
20659
  });
20645
20660
  var FollowUpStatus = Literals(["fixed", "changed", "still_there"]);
20646
20661
  var FollowUp = Struct({ id: String5, status: FollowUpStatus });
20662
+ var SeatFinding = Struct({
20663
+ ...Finding.fields,
20664
+ approvedCode: optional2(Boolean3)
20665
+ });
20647
20666
  var SeatOutput = Struct({
20648
20667
  summary: String5,
20649
- findings: ArraySchema(Finding),
20668
+ findings: ArraySchema(SeatFinding),
20650
20669
  verdicts: optional2(ArraySchema(RebuttalVerdict)),
20651
20670
  scope: optional2(ScopeJudgement),
20652
20671
  followUps: optional2(ArraySchema(FollowUp))
@@ -20654,6 +20673,7 @@ var SeatOutput = Struct({
20654
20673
  var Output = Struct({
20655
20674
  summary: String5,
20656
20675
  findings: ArraySchema(MergedFinding),
20676
+ approvedFindings: optional2(ArraySchema(MergedFinding)),
20657
20677
  followUps: ArraySchema(FollowUp),
20658
20678
  verdicts: ArraySchema(RebuttalVerdict),
20659
20679
  blocking: Boolean3,
@@ -20670,6 +20690,13 @@ class ReviewTargetMissing extends TaggedError3()("ReviewTargetMissing", {
20670
20690
  var targetOf = (input, head) => {
20671
20691
  if (input.changeRequestId !== undefined)
20672
20692
  return `ChangeRequest ${input.changeRequestId} at revision ${head}. Read it with \`kitman.changeRequests.get("${input.changeRequestId}")\` through Kitman's MCP, fetch its source branch, and review the diff of revision ${head} against its target branch, even if the branch has moved since. If the Runner cannot fetch that revision (a fork, say), report exactly that as your only finding, a blocker, and stop.`;
20693
+ if (input.base !== undefined && input.head !== undefined && input.files === undefined && input.approved !== undefined)
20694
+ return [
20695
+ `only what the change gained or lost since it was approved at ${input.approved}: compare \`git diff ${input.base}...${input.approved}\`, the change as approved, with \`git diff ${input.base}...${input.head}\`, the change now, in your checkout. The rest was reviewed and approved before.`,
20696
+ "Report a problem in code the change already had at the approved head only if it matters, with `approvedCode` set to true: such findings are kept apart and do not block."
20697
+ ].join(`
20698
+
20699
+ `);
20673
20700
  if (input.base !== undefined && input.head !== undefined)
20674
20701
  return input.files === undefined ? `the change \`git diff ${input.base}...${input.head}\` in your checkout.` : [
20675
20702
  `only these files of the change \`git diff ${input.base}...${input.head}\` in your checkout; the rest of it was reviewed and approved before:`,
@@ -20679,6 +20706,7 @@ var targetOf = (input, head) => {
20679
20706
  `);
20680
20707
  return;
20681
20708
  };
20709
+ var wholeChange = (input) => input.files === undefined && input.approved === undefined;
20682
20710
  var scopePrompt = (input) => [
20683
20711
  "If your brief has a Task, judge its scope and give it as `scope`: does the change do what the Task's description and each of its Acceptance Criteria ask? Set `met` to true only if it does all of it, and list in `unmet` one line for each criterion or part of the description that is not done. If your brief has no Task, leave `scope` out.",
20684
20712
  ...input.outcome === undefined ? [] : [`The Outcome to prove is ${input.outcome}.`],
@@ -20700,7 +20728,7 @@ var promptFor = (seat, target, input) => [
20700
20728
  "Give `summary`: one sentence on whether it can merge and what matters most.",
20701
20729
  "Report each problem as a finding: severity blocker (must not ship), major (fix before merge) or minor (optional); the file path from the repository root; the line; and the detail. When it is one instance of a class of bug, name every other instance in the diff in its detail. Report nothing you have not checked in the code.",
20702
20730
  "If the diff is too big to review in one go, review it directory by directory and say so in a finding on the first file you skipped.",
20703
- ...seat === scopeSeat && input.files === undefined ? [scopePrompt(input)] : [],
20731
+ ...seat === scopeSeat && wholeChange(input) ? [scopePrompt(input)] : [],
20704
20732
  ...input.earlier === undefined || input.earlier.length === 0 ? [] : [earlierPrompt(input.earlier)],
20705
20733
  ...input.failing === undefined || input.failing.length === 0 ? [] : [
20706
20734
  `These checks fail on this head and pass on the base: ${input.failing.join(", ")}. The implementer has their output, so do not report them as findings, and do not say it can merge while they fail.`
@@ -20720,12 +20748,13 @@ var followUpsOf = (earlier, answers) => earlier.map((finding) => {
20720
20748
  status: statuses.length === 0 ? "still_there" : statuses.reduce((worst, status) => rank2[status] < rank2[worst] ? status : worst)
20721
20749
  };
20722
20750
  });
20723
- var reviewText = (summary, findings, followUps) => [
20751
+ var findingLines = (findings) => findings.map((finding) => `- **${finding.severity}** \`${finding.id}\` ${finding.detail} _(${finding.seats.join(", ")})_`).join(`
20752
+ `);
20753
+ var reviewText = (summary, findings, followUps, approvedFindings) => [
20724
20754
  `**Kitman review.** ${summary}`,
20725
- ...findings.length === 0 ? [] : [
20726
- findings.map((finding) => `- **${finding.severity}** \`${finding.id}\` ${finding.detail} _(${finding.seats.join(", ")})_`).join(`
20727
- `)
20728
- ],
20755
+ ...findings.length === 0 ? [] : [findingLines(findings)],
20756
+ ...approvedFindings.length === 0 ? [] : [`In code approved before, not blocking:
20757
+ ${findingLines(approvedFindings)}`],
20729
20758
  ...followUps.length === 0 ? [] : [
20730
20759
  `Earlier findings:
20731
20760
  ${followUps.map((entry) => `- \`${entry.id}\` ${entry.status.replace("_", " ")}`).join(`
@@ -20741,7 +20770,7 @@ var fixBrief = (changeRequestId, findings) => [
20741
20770
  `);
20742
20771
  var review_default = defineLoop({
20743
20772
  id: "review",
20744
- version: "3.3.0",
20773
+ version: "3.4.0",
20745
20774
  title: "Review",
20746
20775
  input: Input,
20747
20776
  output: Output,
@@ -20786,22 +20815,25 @@ var review_default = defineLoop({
20786
20815
  };
20787
20816
  const carried = earlier.filter((finding) => standing.has(finding.id) && !withdrawn(finding.id));
20788
20817
  const followUps = answered.filter((entry) => !withdrawn(entry.id));
20789
- const fresh = mergeFindings(Object.fromEntries(outputs.map(([seat, output]) => [
20818
+ const sinceApproval = input.approved !== undefined && input.files === undefined;
20819
+ const merged = (approvedCode) => mergeFindings(Object.fromEntries(outputs.map(([seat, output]) => [
20790
20820
  seat,
20791
- output.findings.filter((finding) => !earlierIds.has(`${finding.file}:${finding.line}`))
20821
+ output.findings.filter((finding) => !earlierIds.has(`${finding.file}:${finding.line}`) && (sinceApproval && finding.approvedCode === true) === approvedCode)
20792
20822
  ])));
20823
+ const fresh = merged(false);
20824
+ const approvedFindings = sinceApproval ? merged(true) : [];
20793
20825
  const findings = [...carried, ...fresh];
20794
20826
  const blockers = findings.filter(isBlocking);
20795
- const scope = input.files === undefined ? outputs.find(([seat]) => seat === scopeSeat)?.[1].scope : undefined;
20827
+ const scope = wholeChange(input) ? outputs.find(([seat]) => seat === scopeSeat)?.[1].scope : undefined;
20796
20828
  const failing = input.failing ?? [];
20797
- const summary = blockers.length > 0 ? `Blocking: fix ${blockers.map((finding) => finding.id).join(", ")} before merging.` : scope?.met === false ? "Not ready: the change does not yet do what was asked." : failing.length > 0 ? `Not ready: ${failing.join(", ")} fails on this head.` : findings.length === 0 ? "No findings: nothing stands in the way of merging." : ["Can merge: nothing blocks it.", ...outputs.map(([, output]) => output.summary)].join(" ");
20829
+ const summary = blockers.length > 0 ? `Blocking: fix ${blockers.map((finding) => finding.id).join(", ")} before merging.` : scope?.met === false ? "Not ready: the change does not yet do what was asked." : failing.length > 0 ? `Not ready: ${failing.join(", ")} fails on this head.` : findings.length === 0 ? sinceApproval ? `No findings in what changed since ${input.approved}: nothing stands in the way of merging.` : "No findings: nothing stands in the way of merging." : ["Can merge: nothing blocks it.", ...outputs.map(([, output]) => output.summary)].join(" ");
20798
20830
  const freshIds = new Set(fresh.map((finding) => finding.id));
20799
- const text = reviewText(summary, findings, followUps);
20831
+ const text = reviewText(summary, findings, followUps, approvedFindings);
20800
20832
  const stored = yield* artifact("findings", {
20801
20833
  kind: "review-findings",
20802
20834
  title: "Review findings",
20803
20835
  text,
20804
- data: { findings, followUps }
20836
+ data: { findings, followUps, ...approvedFindings.length === 0 ? undefined : { approvedFindings } }
20805
20837
  });
20806
20838
  const reviewed = {
20807
20839
  summary,
@@ -20810,6 +20842,7 @@ var review_default = defineLoop({
20810
20842
  verdicts,
20811
20843
  blocking: findings.some(isBlocking),
20812
20844
  artifactId: stored.artifactId,
20845
+ ...approvedFindings.length === 0 ? undefined : { approvedFindings },
20813
20846
  ...scope === undefined ? undefined : { scope }
20814
20847
  };
20815
20848
  const changeRequestId = input.changeRequestId;
@@ -21209,23 +21242,30 @@ ${drafted.notes}`,
21209
21242
 
21210
21243
  // src/shepherd-output.ts
21211
21244
  var ShepherdOutput = Struct({
21212
- status: Literals(["merged", "ready", "needs_you"]),
21245
+ status: Literals(["merged", "ready", "needs_you", "handed_back"]),
21213
21246
  reason: String5,
21214
21247
  fixRounds: Int,
21215
21248
  replies: Int,
21216
- release: optional2(Struct({ runStatus: String5, reason: String5, tag: optional2(String5) }))
21249
+ release: optional2(Struct({ runStatus: String5, reason: String5, tag: optional2(String5) })),
21250
+ handBack: optional2(Struct({
21251
+ changeRequestId: String5,
21252
+ words: String5,
21253
+ taskId: optional2(String5),
21254
+ branch: optional2(String5)
21255
+ }))
21217
21256
  });
21218
21257
 
21219
21258
  // src/shepherd.ts
21220
21259
  var reviewLoop = () => ({ id: "review", input: Input, output: Output });
21221
21260
  var stepBudget = "45 minutes";
21261
+ var roundLimits = { "ci-fix": 1, "head-fix": 2 };
21262
+ var handBackAnswer = "Hand back to Build";
21222
21263
  var Input3 = Struct({
21223
21264
  changeRequestId: String5,
21224
21265
  taskId: optional2(String5),
21225
21266
  merge: optional2(Boolean3),
21226
21267
  branch: optional2(String5),
21227
21268
  continuing: optional2(Continuing),
21228
- maxFixRounds: optional2(Int),
21229
21269
  maxCycles: optional2(Int)
21230
21270
  });
21231
21271
  var Output3 = ShepherdOutput;
@@ -21243,24 +21283,39 @@ var Replies = Struct({
21243
21283
  var failedJobs = (pipeline) => pipeline.jobs.flatMap((job) => job.status === "failed" ? [`### ${job.name}${job.url === undefined ? "" : ` (${job.url})`}
21244
21284
 
21245
21285
  ${job.logTail ?? "No log was served."}`] : []);
21246
- var ciFixPrompt = (pipeline, target) => [
21247
- `The pipeline at ${pipeline.head} failed. These jobs failed, with the end of each log:`,
21248
- ...failedJobs(pipeline),
21249
- "Run the failing job once more here to rule out a flake; if it passes, change nothing and answer flaky.",
21250
- `Then run it on the target branch (a worktree of \`origin/${target}\`, which Kitman fetched just before this Step; do not fetch). If it fails there too, the branch did not cause it: change nothing and answer pre_existing.`,
21251
- "Otherwise it is this branch's own failure: write or fix a test that shows it first, then fix it, and answer fixed. Kitman pushes what you leave."
21252
- ].join(`
21286
+ var failingJobs = (pipeline) => pipeline.jobs.flatMap((job) => job.status === "failed" ? [job.name] : []);
21287
+ var ciFixPrompt = (pipeline, target, onTarget) => {
21288
+ const unknown = failingJobs(pipeline).filter((name) => !onTarget.failed.includes(name) && !onTarget.passed.includes(name));
21289
+ return [
21290
+ `The pipeline at ${pipeline.head} failed. These jobs failed, with the end of each log:`,
21291
+ ...failedJobs(pipeline),
21292
+ "Run the failing job once more here to rule out a flake; if it passes, change nothing and answer flaky.",
21293
+ ...onTarget.passed.length === 0 ? [] : [
21294
+ `Kitman ran ${onTarget.passed.join(", ")} at the head of \`origin/${target}\` and they passed there, so do not run them on the target branch.`
21295
+ ],
21296
+ ...onTarget.failed.length === 0 ? [] : [
21297
+ `${onTarget.failed.join(", ")} fail at the head of \`origin/${target}\` too: they are the target's to fix, so leave them as they are.`
21298
+ ],
21299
+ ...unknown.length === 0 ? [] : [
21300
+ `Run ${unknown.join(", ")} on the target branch (a worktree of \`origin/${target}\`, which Kitman fetched just before this Step; do not fetch). If every failing job fails there too, the branch did not cause it: change nothing and answer pre_existing.`
21301
+ ],
21302
+ "Otherwise it is this branch's own failure: write or fix a test that shows it first, then fix it, and answer fixed. Kitman pushes what you leave."
21303
+ ].join(`
21253
21304
 
21254
21305
  `);
21306
+ };
21307
+ var turnResource = (target, repository) => `merge:${repository === undefined ? "" : `${repository}:`}${target}`.toLowerCase().replace(/[^a-z0-9._:-]+/g, "-").slice(0, 128);
21255
21308
  var threadLine = (thread) => [
21256
21309
  `### Thread ${thread.id}${thread.path === undefined ? "" : ` on ${thread.path}${thread.line === undefined ? "" : `:${thread.line}`}`}`,
21257
21310
  ...thread.comments.map((entry) => `${entry.author ?? "someone"}: ${entry.body}`)
21258
21311
  ].join(`
21259
21312
  `);
21260
21313
  var ReviewFix = Struct({ summary: String5 });
21314
+ var findingLines2 = (findings) => findings.map((finding) => `- ${finding.id} (${finding.severity}): ${finding.detail}`).join(`
21315
+ `);
21261
21316
  var reviewFixPrompt = (reviewed, findings) => [
21262
21317
  `A review of ${reviewed} found these. Fix each blocker and major finding, with a test that shows it where one can. Kitman pushes what you leave.`,
21263
- ...findings.map((finding) => `- ${finding.id} (${finding.severity}): ${finding.detail}`)
21318
+ findingLines2(findings)
21264
21319
  ].join(`
21265
21320
  `);
21266
21321
  var catchUpVerdict = (catchUp) => {
@@ -21282,7 +21337,7 @@ var repliesPrompt = (threads) => [
21282
21337
  var threadKey = (thread) => `${thread.id}:${thread.comments.length}`;
21283
21338
  var shepherd_default = defineLoop({
21284
21339
  id: "shepherd",
21285
- version: "1.1.0",
21340
+ version: "1.2.0",
21286
21341
  title: "Shepherd",
21287
21342
  input: Input3,
21288
21343
  output: Output3,
@@ -21297,9 +21352,11 @@ var shepherd_default = defineLoop({
21297
21352
  "call",
21298
21353
  "write_back",
21299
21354
  "ask",
21300
- "verify"
21355
+ "verify",
21356
+ "claim"
21301
21357
  ],
21302
21358
  calls: () => [review_default, release_default],
21359
+ offers: [{ id: "build", title: "Hand back to Build", loop: "build", from: "handBack" }],
21303
21360
  run: (input) => {
21304
21361
  const changeRequestId = input.changeRequestId;
21305
21362
  const target = input.continuing?.targetBranch ?? input.branch ?? "HEAD";
@@ -21308,7 +21365,8 @@ var shepherd_default = defineLoop({
21308
21365
  const ranOnRunner = new Set;
21309
21366
  const leftToPerson = new Set;
21310
21367
  let headFindings = [];
21311
- let fixRounds = 0;
21368
+ const rounds = { "ci-fix": 0, "head-fix": 0 };
21369
+ const allowed = { "ci-fix": 0, "head-fix": 0 };
21312
21370
  let replies = 0;
21313
21371
  let stalledAt;
21314
21372
  let stalls = 0;
@@ -21316,7 +21374,65 @@ var shepherd_default = defineLoop({
21316
21374
  let notCarried;
21317
21375
  let behind = false;
21318
21376
  let pushed;
21319
- const done = (status, reason) => ({ status, reason, fixRounds, replies });
21377
+ let turn;
21378
+ let turns = 0;
21379
+ let passes = 0;
21380
+ let repository;
21381
+ let redTarget;
21382
+ const fixRounds = () => rounds["ci-fix"] + rounds["head-fix"];
21383
+ const done = (status, reason) => ({
21384
+ status,
21385
+ reason,
21386
+ fixRounds: fixRounds(),
21387
+ replies
21388
+ });
21389
+ const handBack = (reason, open) => ({
21390
+ ...done("handed_back", reason),
21391
+ handBack: {
21392
+ changeRequestId,
21393
+ words: `Shepherd handed ChangeRequest ${changeRequestId} back: ${reason}. Fix it forward on its branch:
21394
+ ${open}`,
21395
+ ...input.taskId === undefined ? undefined : { taskId: input.taskId },
21396
+ ...target === "HEAD" ? undefined : { branch: target }
21397
+ }
21398
+ });
21399
+ const takeTurn = () => gen2(function* () {
21400
+ if (turn !== undefined)
21401
+ return;
21402
+ return yield* claim(`turn:${++turns}`, { resource: turnResource(target, repository) }).pipe(map8((granted) => {
21403
+ turn = granted;
21404
+ return;
21405
+ }), catchTag2("StepFailed", (error) => succeed7(error.reason)));
21406
+ });
21407
+ const passTurn = () => gen2(function* () {
21408
+ if (turn === undefined)
21409
+ return;
21410
+ const held = turn;
21411
+ turn = undefined;
21412
+ yield* letGo(`pass-turn:${++passes}`, held);
21413
+ });
21414
+ const mayFix = (kind, cycle, open) => gen2(function* () {
21415
+ if (rounds[kind] < roundLimits[kind] + allowed[kind])
21416
+ return true;
21417
+ const answer = yield* ask(`rounds:${cycle}`, {
21418
+ prompt: `Shepherd has run ${rounds[kind]} ${kind} rounds, its limit, and ${open} still stands. Hand the ChangeRequest back to Build?`,
21419
+ options: [
21420
+ {
21421
+ value: handBackAnswer,
21422
+ description: "Stops here; the ChangeRequest stays open, with a Build offer that carries what is open."
21423
+ },
21424
+ { value: "One more round", description: `Runs one more ${kind} round here.` }
21425
+ ],
21426
+ recommended: handBackAnswer,
21427
+ recommendationReason: "More rounds here have not settled it, and Build fixes and reviews the change as a whole.",
21428
+ default: handBackAnswer,
21429
+ auto: handBackAnswer,
21430
+ timeout: "1 hour"
21431
+ });
21432
+ if (answer !== handBackAnswer)
21433
+ allowed[kind]++;
21434
+ return answer !== handBackAnswer;
21435
+ });
21320
21436
  const runOnRunner = (step, cycle, head, checks) => gen2(function* () {
21321
21437
  const verification = `pipeline-${step}:${cycle}`;
21322
21438
  const failed = yield* verify(verification, { revision: head, checks }).pipe(as2(undefined), catchTag2("StepFailed", (error) => succeed7(error.reason)));
@@ -21327,6 +21443,86 @@ var shepherd_default = defineLoop({
21327
21443
  });
21328
21444
  return failed;
21329
21445
  });
21446
+ const checkTarget = (cycle, pipeline) => gen2(function* () {
21447
+ const failing = failingJobs(pipeline);
21448
+ const onForge = (pipeline.target?.jobs ?? []).filter((job) => failing.includes(job.name));
21449
+ const failed = onForge.flatMap((job) => job.status === "failed" ? [job.name] : []);
21450
+ const passed = onForge.flatMap((job) => job.status === "success" ? [job.name] : []);
21451
+ const onRunner = failing.filter((name) => !failed.includes(name) && !passed.includes(name) && (pipeline.runnerChecks ?? []).includes(name));
21452
+ if (onRunner.length === 0)
21453
+ return { failed, passed };
21454
+ const atTarget = yield* verify(`target-check:${cycle}`, {
21455
+ revision: `origin/${target}`,
21456
+ checks: onRunner
21457
+ }).pipe(catchTag2("StepFailed", () => undefined_2));
21458
+ const ran = atTarget?.checks.filter((check) => onRunner.includes(check.name)) ?? [];
21459
+ return {
21460
+ failed: [...failed, ...ran.flatMap((check) => check.status === "failed" ? [check.name] : [])],
21461
+ passed: [...passed, ...ran.flatMap((check) => check.status === "passed" ? [check.name] : [])]
21462
+ };
21463
+ });
21464
+ const waitOutTarget = (cycle, pipeline, why) => gen2(function* () {
21465
+ yield* passTurn();
21466
+ yield* note(`target-red:${cycle}`, {
21467
+ text: `Shepherd is waiting for ${target} to change instead of fixing ${pipeline.head}: ${why}`
21468
+ }).pipe(catchTag2("StepFailed", () => void_3));
21469
+ const woke = yield* waitFor(`target-moved:${cycle}`, {
21470
+ event: "change_request.closed",
21471
+ filter: {
21472
+ merged: true,
21473
+ ...target === "HEAD" ? undefined : { targetBranch: target },
21474
+ ...repository === undefined ? undefined : { repository }
21475
+ },
21476
+ or: [{ event: "change_request.closed", filter: { changeRequestId } }],
21477
+ timeout: "1 hour",
21478
+ ...pipeline.readAt === undefined ? undefined : { since: pipeline.readAt }
21479
+ }).pipe(catchTag2("WaitTimedOut", () => undefined_2));
21480
+ if (woke?.payload.changeRequestId === changeRequestId)
21481
+ return woke.payload.merged === true ? done("merged", "Someone else merged the ChangeRequest") : done("needs_you", "The ChangeRequest was closed");
21482
+ redTarget = { head: pipeline.head, targetHead: pipeline.target?.head, moved: woke !== undefined };
21483
+ return;
21484
+ });
21485
+ const catchUp = (cycle) => gen2(function* () {
21486
+ const refused = yield* takeTurn();
21487
+ if (refused !== undefined)
21488
+ return done("needs_you", `The turn to merge into ${target} was refused: ${refused}`);
21489
+ const synced = yield* syncBase(`sync:${cycle}`, { agent: "implementer", target });
21490
+ if (synced.status === "conflict")
21491
+ return done("needs_you", `Merging ${target} in conflicts on ${synced.files.join(", ")}: ${synced.reason}`);
21492
+ if (synced.status !== "merged")
21493
+ return;
21494
+ const caught = synced.catchUp;
21495
+ const verdict = caught === undefined ? { kind: "changed", why: "its Runner did not report what it did to the branch" } : catchUpVerdict(caught);
21496
+ if (verdict.kind === "changed") {
21497
+ unreviewed = undefined;
21498
+ notCarried = `merging ${target} in changed it: ${verdict.why}`;
21499
+ } else if (caught !== undefined && unreviewed !== undefined) {
21500
+ const files = [...new Set([...unreviewed.files, ...verdict.kind === "review" ? verdict.files : []])];
21501
+ unreviewed = caught.from === unreviewed.synced ? { ...unreviewed, files, synced: caught.head } : { ...unreviewed, files };
21502
+ } else if (caught !== undefined && verdict.kind === "review") {
21503
+ notCarried = undefined;
21504
+ unreviewed = { files: verdict.files, synced: caught.head, sync: `sync:${cycle}`, earlier: [] };
21505
+ } else if (caught !== undefined) {
21506
+ notCarried = undefined;
21507
+ const carried = yield* approveChangeRequest(`carry:${cycle}`, {
21508
+ changeRequestId,
21509
+ head: caught.head,
21510
+ summary: `Merging ${target} in left the branch's own diff as it was approved.`,
21511
+ carries: `sync:${cycle}`
21512
+ }).pipe(catchTag2("StepFailed", (error) => succeed7({ approved: false, reason: error.reason })));
21513
+ if (!carried.approved)
21514
+ notCarried = `the approval was not carried: ${carried.reason}`;
21515
+ }
21516
+ if (caught !== undefined) {
21517
+ pushed = { head: caught.head, stale: 0 };
21518
+ yield* waitFor(`pushed:${cycle}`, {
21519
+ event: "change_request.mergeable",
21520
+ filter: { changeRequestId, head: caught.head },
21521
+ timeout: "5 minutes"
21522
+ }).pipe(catchTag2("WaitTimedOut", () => void_3));
21523
+ }
21524
+ return;
21525
+ });
21330
21526
  return gen2(function* () {
21331
21527
  for (let cycle = 1;; cycle++) {
21332
21528
  if (cycle > (input.maxCycles ?? 20)) {
@@ -21342,6 +21538,7 @@ var shepherd_default = defineLoop({
21342
21538
  return done("needs_you", `Stopped after ${cycle - 1} looks without landing it`);
21343
21539
  }
21344
21540
  const pipeline = yield* pipelineStatus(`pipeline:${cycle}`, { changeRequestId });
21541
+ repository = pipeline.repository ?? repository;
21345
21542
  if (pushed !== undefined && pipeline.head !== pushed.head) {
21346
21543
  if (++pushed.stale > 5)
21347
21544
  return done("needs_you", `The forge still shows ${pipeline.head}, not ${pushed.head} that the catch-up pushed`);
@@ -21368,24 +21565,61 @@ var shepherd_default = defineLoop({
21368
21565
  continue;
21369
21566
  }
21370
21567
  if (pipeline.status === "failed" || pipeline.status === "canceled") {
21568
+ const targetMoved = redTarget?.head === pipeline.head && (redTarget.targetHead !== undefined && pipeline.target !== undefined ? pipeline.target.head !== redTarget.targetHead : redTarget.moved);
21569
+ if (targetMoved) {
21570
+ redTarget = undefined;
21571
+ const stopped = yield* catchUp(cycle);
21572
+ if (stopped !== undefined)
21573
+ return stopped;
21574
+ continue;
21575
+ }
21576
+ const onTarget = yield* checkTarget(cycle, pipeline);
21577
+ const failing = failingJobs(pipeline);
21578
+ if (failing.length > 0 && failing.every((name) => onTarget.failed.includes(name))) {
21579
+ const stopped = yield* waitOutTarget(cycle, pipeline, `${target} fails ${failing.join(", ")} at its own head too, so no fix here can mend it`);
21580
+ if (stopped !== undefined)
21581
+ return stopped;
21582
+ continue;
21583
+ }
21584
+ if (onTarget.passed.length > 0 && redTarget?.head === pipeline.head) {
21585
+ redTarget = undefined;
21586
+ const stopped = yield* catchUp(cycle);
21587
+ if (stopped !== undefined)
21588
+ return stopped;
21589
+ continue;
21590
+ }
21591
+ if (redTarget?.head === pipeline.head) {
21592
+ const stopped = yield* waitOutTarget(cycle, pipeline, `${target} still fails as ${pipeline.head} does`);
21593
+ if (stopped !== undefined)
21594
+ return stopped;
21595
+ continue;
21596
+ }
21371
21597
  if (stalledAt === pipeline.head)
21372
21598
  stalls++;
21373
21599
  else
21374
21600
  stalls = 0;
21375
21601
  if (stalls >= 2)
21376
21602
  return done("needs_you", `Two fixes pushed nothing new; the pipeline at ${pipeline.head} still fails`);
21377
- if (fixRounds >= (input.maxFixRounds ?? 3))
21378
- return done("needs_you", `The pipeline still fails after ${fixRounds} fix rounds`);
21379
- fixRounds++;
21603
+ if (!(yield* mayFix("ci-fix", cycle, `the failing pipeline at ${pipeline.head}`)))
21604
+ return handBack(`The pipeline at ${pipeline.head} still fails after ${rounds["ci-fix"]} ci-fix rounds`, failedJobs(pipeline).join(`
21605
+
21606
+ `));
21380
21607
  stalledAt = pipeline.head;
21608
+ yield* passTurn();
21381
21609
  const fixed = yield* agentWork(`ci-fix:${cycle}`, {
21382
21610
  agent: "implementer",
21383
- prompt: ciFixPrompt(pipeline, target),
21611
+ prompt: ciFixPrompt(pipeline, target, onTarget),
21384
21612
  output: CiFix,
21385
21613
  timeout: stepBudget
21386
21614
  });
21387
- if (fixed.verdict === "pre_existing")
21388
- return done("needs_you", `The failure is pre-existing on the target branch: ${fixed.summary}`);
21615
+ if (fixed.verdict === "fixed")
21616
+ rounds["ci-fix"]++;
21617
+ if (fixed.verdict === "pre_existing") {
21618
+ const stopped = yield* waitOutTarget(cycle, pipeline, `the failure is pre-existing on the target branch: ${fixed.summary}`);
21619
+ if (stopped !== undefined)
21620
+ return stopped;
21621
+ continue;
21622
+ }
21389
21623
  if (fixed.verdict === "flaky") {
21390
21624
  if (pipeline.source === "reported") {
21391
21625
  if (pipeline.runnerChecks === undefined)
@@ -21406,8 +21640,10 @@ var shepherd_default = defineLoop({
21406
21640
  }
21407
21641
  continue;
21408
21642
  }
21643
+ redTarget = undefined;
21409
21644
  const threads = (yield* reviewThreads(`threads:${cycle}`, { changeRequestId })).threads.filter((thread) => !answered.has(threadKey(thread)));
21410
21645
  if (threads.length > 0) {
21646
+ yield* passTurn();
21411
21647
  const answers = yield* agentWork(`comments:${cycle}`, {
21412
21648
  agent: "implementer",
21413
21649
  prompt: repliesPrompt(threads),
@@ -21442,47 +21678,16 @@ var shepherd_default = defineLoop({
21442
21678
  continue;
21443
21679
  if (mergeable.payload.mergeable === false || behind) {
21444
21680
  behind = false;
21445
- const synced = yield* syncBase(`sync:${cycle}`, { agent: "implementer", target });
21446
- if (synced.status === "conflict")
21447
- return done("needs_you", `Merging ${target} in conflicts on ${synced.files.join(", ")}: ${synced.reason}`);
21448
- if (synced.status === "merged") {
21449
- const caught = synced.catchUp;
21450
- const verdict = caught === undefined ? { kind: "changed", why: "its Runner did not report what it did to the branch" } : catchUpVerdict(caught);
21451
- if (verdict.kind === "changed") {
21452
- unreviewed = undefined;
21453
- notCarried = `merging ${target} in changed it: ${verdict.why}`;
21454
- } else if (caught !== undefined && unreviewed !== undefined) {
21455
- const files = [...new Set([...unreviewed.files, ...verdict.kind === "review" ? verdict.files : []])];
21456
- unreviewed = caught.from === unreviewed.synced ? { ...unreviewed, files, synced: caught.head } : { ...unreviewed, files };
21457
- } else if (caught !== undefined && verdict.kind === "review") {
21458
- notCarried = undefined;
21459
- unreviewed = { files: verdict.files, synced: caught.head, sync: `sync:${cycle}`, earlier: [] };
21460
- } else if (caught !== undefined) {
21461
- notCarried = undefined;
21462
- const carried = yield* approveChangeRequest(`carry:${cycle}`, {
21463
- changeRequestId,
21464
- head: caught.head,
21465
- summary: `Merging ${target} in left the branch's own diff as it was approved.`,
21466
- carries: `sync:${cycle}`
21467
- }).pipe(catchTag2("StepFailed", (error) => succeed7({ approved: false, reason: error.reason })));
21468
- if (!carried.approved)
21469
- notCarried = `the approval was not carried: ${carried.reason}`;
21470
- }
21471
- if (caught !== undefined) {
21472
- pushed = { head: caught.head, stale: 0 };
21473
- yield* waitFor(`pushed:${cycle}`, {
21474
- event: "change_request.mergeable",
21475
- filter: { changeRequestId, head: caught.head },
21476
- timeout: "5 minutes"
21477
- }).pipe(catchTag2("WaitTimedOut", () => void_3));
21478
- }
21479
- }
21681
+ const stopped = yield* catchUp(cycle);
21682
+ if (stopped !== undefined)
21683
+ return stopped;
21480
21684
  continue;
21481
21685
  }
21482
21686
  let reviewedHere = false;
21483
21687
  if (unreviewed !== undefined) {
21484
21688
  const files = unreviewed;
21485
21689
  const head = pipeline.head;
21690
+ yield* passTurn();
21486
21691
  const reviewed = yield* call(`review:${cycle}`, reviewLoop(), {
21487
21692
  base: `origin/${target}`,
21488
21693
  head,
@@ -21492,9 +21697,9 @@ var shepherd_default = defineLoop({
21492
21697
  });
21493
21698
  if (reviewed.blocking) {
21494
21699
  const blocking = reviewed.findings.filter(isBlocking);
21495
- if (fixRounds >= (input.maxFixRounds ?? 3))
21496
- return done("needs_you", `The review of the catch-up merge's files still finds ${blocking.map((finding) => finding.id).join(", ")} after ${fixRounds} fix rounds`);
21497
- fixRounds++;
21700
+ if (!(yield* mayFix("head-fix", cycle, blocking.map((finding) => finding.id).join(", "))))
21701
+ return handBack(`The review of the catch-up merge's files still finds ${blocking.map((finding) => finding.id).join(", ")} after ${rounds["head-fix"]} head-fix rounds`, findingLines2(blocking));
21702
+ rounds["head-fix"]++;
21498
21703
  yield* agentWork(`review-fix:${cycle}`, {
21499
21704
  agent: "implementer",
21500
21705
  prompt: reviewFixPrompt("the files the catch-up merge changed", blocking),
@@ -21540,21 +21745,23 @@ var shepherd_default = defineLoop({
21540
21745
  }).pipe(map8((woke) => woke.type === "change_request.approval" && woke.payload.source !== "kitman" ? undefined : woke), catchTag2("WaitTimedOut", () => undefined_2));
21541
21746
  if (!reviewedHere && approved === undefined) {
21542
21747
  const head = pipeline.head;
21748
+ const since = pipeline.approved === undefined || pipeline.approved === head ? undefined : pipeline.approved;
21543
21749
  const reviewIt = leftToPerson.has(head) ? "Wait" : yield* ask(`unapproved:${cycle}`, {
21544
21750
  prompt: `Nobody has approved ${head}, the head whose pipeline passed. Have Kitman review it?`,
21545
21751
  options: [
21546
21752
  {
21547
21753
  value: "Review",
21548
- description: "Kitman's Review reads the whole change at this head and approves it if nothing blocks."
21754
+ description: since === undefined ? "Kitman's Review reads the whole change at this head and approves it if nothing blocks." : `Kitman's Review reads what changed since ${since}, the last approved head, and approves it if nothing there blocks.`
21549
21755
  },
21550
21756
  { value: "Wait", description: "Waits for a person to approve this head." }
21551
21757
  ],
21552
21758
  recommended: "Review",
21553
- recommendationReason: "A passing Review of the whole change is what approved the head Build shipped.",
21759
+ recommendationReason: "A passing Review of the change is what approved the head Build shipped.",
21554
21760
  default: "Wait",
21555
21761
  auto: "Review",
21556
21762
  timeout: "1 hour"
21557
21763
  });
21764
+ yield* passTurn();
21558
21765
  if (reviewIt === "Wait") {
21559
21766
  leftToPerson.add(head);
21560
21767
  approved = yield* waitFor(`approval:${cycle}`, { ...approval, timeout: "7 days" });
@@ -21562,16 +21769,17 @@ var shepherd_default = defineLoop({
21562
21769
  const reviewed = yield* call(`head-review:${cycle}`, reviewLoop(), {
21563
21770
  base: `origin/${target}`,
21564
21771
  head,
21772
+ ...since === undefined ? undefined : { approved: since },
21565
21773
  ...headFindings.length === 0 ? undefined : { earlier: headFindings }
21566
21774
  });
21567
21775
  if (reviewed.blocking) {
21568
21776
  const blocking = reviewed.findings.filter(isBlocking);
21569
- if (fixRounds >= (input.maxFixRounds ?? 3))
21570
- return done("needs_you", `The review of ${head} still finds ${blocking.map((finding) => finding.id).join(", ")} after ${fixRounds} fix rounds`);
21571
- fixRounds++;
21777
+ if (!(yield* mayFix("head-fix", cycle, blocking.map((finding) => finding.id).join(", "))))
21778
+ return handBack(`The review of ${head} still finds ${blocking.map((finding) => finding.id).join(", ")} after ${rounds["head-fix"]} head-fix rounds`, findingLines2(blocking));
21779
+ rounds["head-fix"]++;
21572
21780
  yield* agentWork(`head-fix:${cycle}`, {
21573
21781
  agent: "implementer",
21574
- prompt: reviewFixPrompt(`the change at ${head}`, blocking),
21782
+ prompt: reviewFixPrompt(since === undefined ? `the change at ${head}` : `what changed between ${since} and ${head}`, blocking),
21575
21783
  output: ReviewFix,
21576
21784
  timeout: stepBudget
21577
21785
  });
@@ -21581,7 +21789,7 @@ var shepherd_default = defineLoop({
21581
21789
  const recorded = yield* approveChangeRequest(`head-approve:${cycle}`, {
21582
21790
  changeRequestId,
21583
21791
  head,
21584
- summary: `A review of the whole change at ${head} passed with nothing blocking.`,
21792
+ summary: since === undefined ? `A review of the whole change at ${head} passed with nothing blocking.` : `A review of what changed between ${since}, the last approved head, and ${head} passed with nothing blocking.`,
21585
21793
  review: `head-review:${cycle}`
21586
21794
  }).pipe(catchTag2("StepFailed", (error) => succeed7({ approved: false, reason: error.reason })));
21587
21795
  if (!recorded.approved)
@@ -21607,6 +21815,9 @@ var shepherd_default = defineLoop({
21607
21815
  });
21608
21816
  if (merge === "Wait")
21609
21817
  return done("ready", "Approved, green and mergeable; left for a person to merge");
21818
+ const refused = yield* takeTurn();
21819
+ if (refused !== undefined)
21820
+ return done("needs_you", `The turn to merge into ${target} was refused: ${refused}`);
21610
21821
  const merged = yield* mergeChangeRequest(`merge-change-request:${cycle}`, {
21611
21822
  changeRequestId,
21612
21823
  head: pipeline.head
@@ -21615,6 +21826,7 @@ var shepherd_default = defineLoop({
21615
21826
  behind = true;
21616
21827
  if (merged.status !== "merged")
21617
21828
  continue;
21829
+ yield* passTurn();
21618
21830
  yield* writeBack("write-back", { milestone: "merged" });
21619
21831
  const ship = yield* ask("ship", {
21620
21832
  prompt: `Merged at ${merged.revision}. Release it now?`,
@@ -26671,7 +26883,19 @@ class RunnerCapabilityRpcs extends make30(make29("PutQuestion", {
26671
26883
  logTail: optionalKey2(String5)
26672
26884
  })),
26673
26885
  readAt: optionalKey2(String5),
26674
- runnerChecks: optionalKey2(ArraySchema(String5))
26886
+ runnerChecks: optionalKey2(ArraySchema(String5)),
26887
+ approved: optionalKey2(String5),
26888
+ repository: optionalKey2(String5),
26889
+ target: optionalKey2(Struct({
26890
+ branch: String5,
26891
+ head: String5,
26892
+ jobs: ArraySchema(Struct({
26893
+ name: String5,
26894
+ status: Literals(["running", "success", "failed", "skipped"]),
26895
+ url: optionalKey2(String5),
26896
+ logTail: optionalKey2(String5)
26897
+ }))
26898
+ }))
26675
26899
  }),
26676
26900
  error: RunnerRpcError
26677
26901
  }), make29("ReportPipeline", {
@@ -29538,4 +29762,4 @@ var runKit = (config, identity, packages) => {
29538
29762
  runMain2(program.pipe(flatMap3((code) => sync2(() => process.exit(code)))));
29539
29763
  };
29540
29764
  // .kitman-build-entry.ts
29541
- runKit(kitman_config_default, { name: "@kitman-sh/software-engineering", version: "0.10.69" }, []);
29765
+ runKit(kitman_config_default, { name: "@kitman-sh/software-engineering", version: "0.10.70" }, []);