@lchase/kiln 0.10.0 → 0.12.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.
package/dist/board.js CHANGED
@@ -1,102 +1,5 @@
1
1
  import { existsSync, readFileSync, readdirSync } from "node:fs";
2
2
  import { join } from "node:path";
3
- import { scheduledRetry } from "./attention.js";
4
- const FEATURE_STAGES = ["spec", "build", "review", "approve", "land", "pr-review"];
5
- const CATALOG = [
6
- { id: "queued", label: "Queued", who: "waiting · deps", tone: "queued" },
7
- { id: "spec", label: "Spec", who: "spec-writer · opus" },
8
- { id: "build", label: "Build", who: "builder · opus" },
9
- { id: "review", label: "Review", who: "sec · ux · ui · code" },
10
- { id: "approve", label: "Approve", who: "approver · opus" },
11
- { id: "explore", label: "Explore", who: "investigator · opus" },
12
- { id: "land", label: "Land", who: "git commit" },
13
- { id: "pr-review", label: "PR review", who: "pr-sync" },
14
- { id: "done", label: "Done", who: "local", tone: "merged" },
15
- { id: "merged", label: "Merged", who: "pr", tone: "merged" },
16
- { id: "needs-human", label: "Needs human", who: "approve · retry", tone: "human" },
17
- ];
18
- const STEP_STAGE = {
19
- spec: "spec",
20
- build: "build",
21
- verify: "build",
22
- security: "review",
23
- ux: "review",
24
- ui: "review",
25
- code: "review",
26
- approver: "approve",
27
- approve: "needs-human",
28
- land: "land",
29
- explore: "explore",
30
- };
31
- const HUMAN_STATES = new Set(["needs-human", "failed", "dead", "invalid", "rejected"]);
32
- /**
33
- * Column for one job. Human gates and broken runs sit in Needs human.
34
- * A failed job with an automatic retry still scheduled stays on the step
35
- * its last run reached. `scheduledRetry` is that decision.
36
- */
37
- export function stageOf(job) {
38
- if (scheduledRetry(job))
39
- return runStage(job);
40
- if (HUMAN_STATES.has(job.state))
41
- return "needs-human";
42
- if (job.state === "in-review")
43
- return "pr-review";
44
- if (job.state === "done")
45
- return job.prUrl || job.prNumber ? "merged" : "done";
46
- const current = job.steps.find((step) => step.phase === "current" || step.phase === "failed");
47
- if (job.state === "queued" || job.state === "blocked")
48
- return "queued";
49
- if (job.state === "approved") {
50
- if (current && current.id !== "land" && current.id !== "approve" && STEP_STAGE[current.id])
51
- return STEP_STAGE[current.id];
52
- return "land";
53
- }
54
- if (current && STEP_STAGE[current.id])
55
- return STEP_STAGE[current.id];
56
- return runStage(job);
57
- }
58
- /**
59
- * Stage of the last step the run actually reached.
60
- * The approve gate's column is Needs human; a scheduled retry is not a gate,
61
- * so that step stays on Approve. Spec or Explore when the run never started a step.
62
- */
63
- function runStage(job) {
64
- const last = [...job.steps].reverse().find((step) => step.phase === "current" || step.phase === "failed");
65
- const stage = last ? STEP_STAGE[last.id] : undefined;
66
- if (stage && stage !== "needs-human")
67
- return stage;
68
- if (stage === "needs-human") {
69
- const earlier = [...job.steps].reverse().find((step) => {
70
- const mapped = STEP_STAGE[step.id];
71
- return step.phase === "done" && mapped !== undefined && mapped !== "needs-human";
72
- });
73
- if (earlier)
74
- return STEP_STAGE[earlier.id] ?? "approve";
75
- return "approve";
76
- }
77
- return job.workflow === "investigation" ? "explore" : "spec";
78
- }
79
- /** Stages to render. An empty kiln still shows the feature pipeline, collapsed. */
80
- export function stagesFor(jobs) {
81
- const workflows = new Set(jobs.map((job) => job.workflow));
82
- const wanted = new Set();
83
- if (jobs.length === 0 || workflows.has("feature") || [...workflows].some((name) => name !== "investigation")) {
84
- for (const id of FEATURE_STAGES)
85
- wanted.add(id);
86
- }
87
- if (workflows.has("investigation"))
88
- wanted.add("explore");
89
- wanted.add("queued");
90
- wanted.add("needs-human");
91
- const reached = new Set(jobs.map((job) => stageOf(job)));
92
- if (reached.has("done") || !reached.has("merged"))
93
- wanted.add("done");
94
- if (reached.has("merged"))
95
- wanted.add("merged");
96
- for (const id of reached)
97
- wanted.add(id);
98
- return CATALOG.filter((stage) => wanted.has(stage.id));
99
- }
100
3
  const REVIEW_FILE = /^(security|ux|ui|code)(?:\.(\d+))?\.json$/;
101
4
  const TEXT_LIMIT = 8_000;
102
5
  export function readReviewRounds(runDir) {
package/dist/cli.js CHANGED
@@ -2,6 +2,7 @@
2
2
  import { addRepo, renderRepoList, resolveKilnConfig } from "./config.js";
3
3
  import { renderSandboxStatus, runSandboxBuild } from "./sandbox.js";
4
4
  import { answerJob, retryJob, reviseJob, runStatus, runTick } from "./driver.js";
5
+ import { productionHost } from "./host.js";
5
6
  import { pauseFactory, resumeFactory } from "./rails.js";
6
7
  import { parseSince, renderLedgerFile } from "./ledger.js";
7
8
  import { startServe } from "./serve.js";
@@ -88,6 +89,11 @@ function usage() {
88
89
  "that requests changes starts a fix on that same branch, up to maxFixRounds",
89
90
  "(default 2). Kiln does not merge, approve, or comment. Past that bound the",
90
91
  "job is needs-human with reason feedback-exhausted.",
92
+ "Triggers in the kiln config start work without kiln add. A cron fires",
93
+ "on the serve tick in local time, once per slot, and once on startup",
94
+ "when a slot was missed. Jira polls are per repo. Kiln-owned pull",
95
+ "request activity uses the same read as the fix loop. A failed fire",
96
+ "waits for the next slot. Pause and the daily cap skip the fire.",
91
97
  "",
92
98
  "serve listens on 127.0.0.1. The page is a board of workflow stages:",
93
99
  "queued, spec, build, review, approve, land, PR review, Done (landed locally),",
@@ -401,7 +407,7 @@ function tickCommand(argv) {
401
407
  usage();
402
408
  }
403
409
  const config = resolveKilnConfig({ configPath: flags.configPath });
404
- process.stdout.write(runTick(config));
410
+ process.stdout.write(runTick(config, productionHost()));
405
411
  }
406
412
  function statusCommand(argv) {
407
413
  const flags = parseFlags(argv);
package/dist/config.js CHANGED
@@ -54,6 +54,11 @@ export function loadKilnConfig(configPath, options = {}) {
54
54
  sandbox: parsed.data.sandbox ?? {},
55
55
  workflowSandbox: workflowSandboxFrom(parsed.data.workflows),
56
56
  ...(jira ? { jira } : {}),
57
+ triggers: parsed.data.triggers ?? [],
58
+ routes: parsed.data.routes ?? [],
59
+ ...(parsed.data.reviewPrompt
60
+ ? { reviewPrompt: resolveConfiguredPath(configDir, parsed.data.reviewPrompt, home) }
61
+ : {}),
57
62
  };
58
63
  }
59
64
  /**
package/dist/driver.js CHANGED
@@ -21,7 +21,8 @@ import { projectRunJournal, promptsHash } from "./project.js";
21
21
  import { gateAnswerCommand, renderStatus, renderTick, statusView, yakCliPath, sandboxFailureDetail, } from "./report.js";
22
22
  import { AGENT_SANDBOX_IMAGE, injectAgentSandbox, jobIsSandboxed, probeSandboxEnvironment, PROXY_SANDBOX_IMAGE, requiredSandboxImages, resolveSandbox, sandboxIsEnabled, sandboxPreflight, sandboxPreflightFor, } from "./sandbox.js";
23
23
  import { JobSchema } from "./schemas.js";
24
- import { leafStepIds, materializeWorkflow, workflowInputs, workflowNeedsTestCommand, workflowStepPlan } from "./workflow.js";
24
+ import { ensureReviewHead, evaluateTriggers, recordOwnedPullRequest, reviewHeadOf, } from "./triggers.js";
25
+ import { leafStepIds, materializeWorkflow, REVIEW_WORKFLOW, workflowInputs, workflowNeedsTestCommand, workflowStepPlan, } from "./workflow.js";
25
26
  const TAG_PREFIX = "kiln:";
26
27
  export function jobTag(jobId) {
27
28
  return `${TAG_PREFIX}${jobId}`;
@@ -53,8 +54,14 @@ export function runTick(config, host = defaultHost(), options = {}) {
53
54
  const midway = observe(config, host, now);
54
55
  bindOpenFixRuns(config, midway);
55
56
  const published = publishReadyFixes(config, host, midway);
57
+ evaluateTriggers({
58
+ config,
59
+ now,
60
+ startup: options.triggerStartup ?? startup,
61
+ ...(host.reviews ? { reviews: host.reviews } : {}),
62
+ });
56
63
  const world = observe(config, host, now);
57
- const feedbackNotes = watchPullRequests(config, world, host);
64
+ const feedbackNotes = watchPullRequests(config, world, host, now);
58
65
  const actions = decide(world.observation, now);
59
66
  if (world.observation.spendBlocked) {
60
67
  recordDailyCap(config.kilnDir, spendSnapshot(readEvents(config.kilnDir, { warn: () => { } }), config.dailyDollarCap, now), now);
@@ -749,7 +756,7 @@ function gateEdit(checkout, runId, stepId) {
749
756
  * Read each open pull request once per tick and attach a plan.
750
757
  * `readStatus` does not call this. A host with no feedback reader does not watch.
751
758
  */
752
- function watchPullRequests(config, world, host) {
759
+ function watchPullRequests(config, world, host, now) {
753
760
  const notes = [];
754
761
  if (!host.feedback)
755
762
  return notes;
@@ -775,11 +782,28 @@ function watchPullRequests(config, world, host) {
775
782
  ledger,
776
783
  snapshot,
777
784
  });
785
+ recordOwnedPullRequest(config, {
786
+ jobId: job.jobId,
787
+ repo: job.repo,
788
+ prNumber: job.delivery?.prNumber ?? 0,
789
+ headSha: snapshot.headSha,
790
+ comments: snapshot.comments.map((comment) => ({
791
+ id: comment.id,
792
+ author: comment.author,
793
+ body: comment.body,
794
+ resolved: comment.resolved,
795
+ })),
796
+ reviews: snapshot.reviews.map((review) => ({
797
+ id: review.id,
798
+ author: review.author,
799
+ body: review.body,
800
+ })),
801
+ }, now);
778
802
  }
779
803
  return notes;
780
804
  }
781
805
  function shouldWatch(job) {
782
- if (job.deliver !== "pr" || job.workflow === INVESTIGATION_WORKFLOW)
806
+ if (job.deliver !== "pr" || job.workflow === INVESTIGATION_WORKFLOW || job.workflow === REVIEW_WORKFLOW)
783
807
  return false;
784
808
  if (job.error || job.feedbackLedgerError || job.feedbackExhausted || job.feedbackAmbiguous)
785
809
  return false;
@@ -1041,7 +1065,9 @@ function launchJob(config, world, job, host, sandboxFacts) {
1041
1065
  if (workflowNeedsTestCommand(sourceWorkflow) && !testCommand?.trim()) {
1042
1066
  throw new Error(`Repo ${job.repo} has no test command. Set repos.${job.repo}.test. The feature workflow refuses to run an empty KILN_TEST.`);
1043
1067
  }
1044
- const workflow = materializeWorkflow(config.kilnDir, job.id, job.workflow);
1068
+ const workflow = materializeWorkflow(config.kilnDir, job.id, job.workflow, {
1069
+ ...(config.reviewPrompt ? { reviewPrompt: config.reviewPrompt } : {}),
1070
+ });
1045
1071
  const logPath = join(config.kilnDir, "logs", `${jobBasename(job.id)}.log`);
1046
1072
  mkdirSync(dirname(logPath), { recursive: true });
1047
1073
  ensureCheckoutExcludes(repo.path);
@@ -1053,14 +1079,23 @@ function launchJob(config, world, job, host, sandboxFacts) {
1053
1079
  if (blocked)
1054
1080
  return { jobId: job.id, repo: job.repo, output: blocked };
1055
1081
  assertStackTarget(world, job);
1056
- baseSha = resolveLaunchBase({
1057
- checkout: repo.path,
1058
- deliver: repo.deliver,
1059
- stack: job.stack === true,
1060
- ...(repo.base ? { configuredBase: repo.base } : {}),
1061
- ...(job.stack ? { upstreamBranch: upstreamLocalBranch(world, job) } : {}),
1062
- git: host.git,
1063
- });
1082
+ const pinned = reviewHeadOf(job);
1083
+ if (pinned) {
1084
+ const number = Number(job.source?.ref.split(":")[2]);
1085
+ if (Number.isInteger(number) && number > 0)
1086
+ ensureReviewHead(repo.path, number, pinned);
1087
+ baseSha = resolveCommit(host, repo.path, pinned);
1088
+ }
1089
+ else {
1090
+ baseSha = resolveLaunchBase({
1091
+ checkout: repo.path,
1092
+ deliver: repo.deliver,
1093
+ stack: job.stack === true,
1094
+ ...(repo.base ? { configuredBase: repo.base } : {}),
1095
+ ...(job.stack ? { upstreamBranch: upstreamLocalBranch(world, job) } : {}),
1096
+ git: host.git,
1097
+ });
1098
+ }
1064
1099
  specText = specHandedToYak(config, world, job);
1065
1100
  }
1066
1101
  catch (error) {
package/dist/events.js CHANGED
@@ -9,8 +9,8 @@ import { redactDeep } from "./redact.js";
9
9
  * parses it. A write failure is reported and does not escape to the caller.
10
10
  *
11
11
  * `run_id` and `step` are null when the moment has none. `job_id` is null
12
- * for `config.reloaded`, `kiln.paused`, `kiln.resumed`, and `budget.blocked`,
13
- * which are not a job.
12
+ * for `config.reloaded`, `kiln.paused`, `kiln.resumed`, `budget.blocked`,
13
+ * and trigger fires that do not belong to a job yet.
14
14
  */
15
15
  export const EVENT_NAMES = [
16
16
  "job.added",
@@ -31,6 +31,10 @@ export const EVENT_NAMES = [
31
31
  "kiln.paused",
32
32
  "kiln.resumed",
33
33
  "budget.blocked",
34
+ "trigger.fired",
35
+ "trigger.routed",
36
+ "trigger.failed",
37
+ "trigger.skipped",
34
38
  ];
35
39
  const ISO_UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
36
40
  const once = z.string().min(1).optional();
@@ -131,6 +135,35 @@ const BudgetBlocked = z.object({
131
135
  cap: z.number().nonnegative(),
132
136
  once,
133
137
  });
138
+ const TriggerFired = z.object({
139
+ trigger_id: z.string().min(1),
140
+ source: z.enum(["schedule", "jira", "pr", "review"]),
141
+ /** Stable dedupe id. Not named `key`: the redactor masks any property with that name. */
142
+ dedupe: z.string().min(1),
143
+ external_id: z.string().min(1),
144
+ revision: z.string().min(1),
145
+ repo: z.string().min(1).optional(),
146
+ title: z.string().min(1).optional(),
147
+ once,
148
+ });
149
+ const TriggerRouted = z.object({
150
+ trigger_id: z.string().min(1),
151
+ dedupe: z.string().min(1),
152
+ action: z.enum(["create", "attach"]),
153
+ workflow: z.string().min(1).optional(),
154
+ repo: z.string().min(1).optional(),
155
+ once,
156
+ });
157
+ const TriggerFailed = z.object({
158
+ trigger_id: z.string().min(1),
159
+ error: z.string().min(1),
160
+ once,
161
+ });
162
+ const TriggerSkipped = z.object({
163
+ trigger_id: z.string().min(1),
164
+ reason: z.enum(["paused", "capped"]),
165
+ once,
166
+ });
134
167
  const envelope = {
135
168
  id: z.string().min(1),
136
169
  ts: z.string().regex(ISO_UTC, "ts must be ISO-8601 UTC"),
@@ -176,6 +209,30 @@ export const EventSchema = z.discriminatedUnion("event", [
176
209
  job_id: z.string().min(1).nullable(),
177
210
  properties: BudgetBlocked,
178
211
  }),
212
+ z.object({
213
+ ...envelope,
214
+ event: z.literal("trigger.fired"),
215
+ job_id: z.string().min(1).nullable(),
216
+ properties: TriggerFired,
217
+ }),
218
+ z.object({
219
+ ...envelope,
220
+ event: z.literal("trigger.routed"),
221
+ job_id: z.string().min(1).nullable(),
222
+ properties: TriggerRouted,
223
+ }),
224
+ z.object({
225
+ ...envelope,
226
+ event: z.literal("trigger.failed"),
227
+ job_id: z.string().min(1).nullable(),
228
+ properties: TriggerFailed,
229
+ }),
230
+ z.object({
231
+ ...envelope,
232
+ event: z.literal("trigger.skipped"),
233
+ job_id: z.string().min(1).nullable(),
234
+ properties: TriggerSkipped,
235
+ }),
179
236
  ]);
180
237
  const LOCK_WAIT_MS = 5_000;
181
238
  const pauseBuf = new Int32Array(new SharedArrayBuffer(4));
package/dist/host.js CHANGED
@@ -18,6 +18,59 @@ export class HostError extends Error {
18
18
  export function defaultHost() {
19
19
  return { github: realGitHub, git: realGitRemote, feedback: realPullRequestFeedback };
20
20
  }
21
+ /** `kiln tick` and `kiln serve`. Review requests use the operator's gh auth. */
22
+ export function productionHost() {
23
+ return { ...defaultHost(), reviews: { list: listReviewRequests } };
24
+ }
25
+ /**
26
+ * Open pull requests awaiting the authenticated user's review, per registered repo.
27
+ * A remote that is not GitHub is skipped, so a local test origin does not call gh.
28
+ */
29
+ export function listReviewRequests(repos) {
30
+ const found = [];
31
+ for (const repo of repos) {
32
+ if (!githubRemote(repo.path))
33
+ continue;
34
+ const result = spawnSync(ghBin(), ["pr", "list", "--search", "review-requested:@me", "--state", "open", "--limit", "50", "--json", "number,title,url,headRefOid"], {
35
+ cwd: repo.path,
36
+ encoding: "utf8",
37
+ timeout: 20_000,
38
+ env: { ...process.env, GH_PROMPT_DISABLED: "1", GIT_TERMINAL_PROMPT: "0" },
39
+ });
40
+ if (result.error || result.status !== 0) {
41
+ throw new HostError(`Could not list review requests for ${repo.name}.`);
42
+ }
43
+ let parsed;
44
+ try {
45
+ parsed = JSON.parse(result.stdout || "[]");
46
+ }
47
+ catch {
48
+ throw new HostError("gh pr list returned data that is not JSON.");
49
+ }
50
+ if (!Array.isArray(parsed))
51
+ throw new HostError("gh pr list returned data that is not a list.");
52
+ for (const item of parsed) {
53
+ if (typeof item !== "object" || item === null)
54
+ continue;
55
+ const record = item;
56
+ const number = typeof record.number === "number" ? record.number : Number.NaN;
57
+ const sha = typeof record.headRefOid === "string" ? record.headRefOid.trim().toLowerCase() : "";
58
+ if (!Number.isInteger(number) || number <= 0 || !/^[0-9a-f]{7,64}$/.test(sha))
59
+ continue;
60
+ const title = typeof record.title === "string" && record.title.trim() ? record.title.trim() : `PR ${number}`;
61
+ const url = typeof record.url === "string" ? record.url : "";
62
+ found.push({ repo: repo.name, number, title, url, headSha: sha });
63
+ }
64
+ }
65
+ return found;
66
+ }
67
+ function githubRemote(checkout) {
68
+ const result = gitResult(checkout, ["remote", "get-url", "origin"]);
69
+ if (result.status !== 0)
70
+ return false;
71
+ const url = result.stdout.trim();
72
+ return /github\.com[:/]/i.test(url);
73
+ }
21
74
  const YAK_BRANCH = /^yak\/[A-Za-z0-9._-]+$/;
22
75
  function assertYakBranch(branch, baseBranch) {
23
76
  if (branch === baseBranch || branch === `refs/heads/${baseBranch}`) {
@@ -1,3 +1,4 @@
1
+ import { spawnSync } from "node:child_process";
1
2
  import { assertCanStack, listValidJobs, renderAddedJob, writeJob } from "../jobs.js";
2
3
  import { ISSUE_KEY } from "../schemas.js";
3
4
  import { assertWorkflowName } from "../workflow.js";
@@ -89,6 +90,92 @@ export async function addFromJira(config, input) {
89
90
  warnings,
90
91
  };
91
92
  }
93
+ /**
94
+ * Search Jira with the operator's token. The token stays in the child
95
+ * environment and is never written to stdout, a job, or an event.
96
+ * `POST /rest/api/3/search/jql` is the read. There is no webhook here.
97
+ */
98
+ export function searchJiraIssues(config, jql, env = process.env) {
99
+ const query = jql.trim();
100
+ if (!query || query.length > 2_000)
101
+ throw new Error("Jira poll JQL is empty or too long.");
102
+ const credentials = resolveJiraCredentials(config, env);
103
+ const script = [
104
+ "const base = process.env.KILN_JIRA_BASE;",
105
+ "const email = process.env.KILN_JIRA_EMAIL;",
106
+ "const token = process.env.KILN_JIRA_TOKEN;",
107
+ "const jql = process.env.KILN_JIRA_JQL;",
108
+ "if (!base || !email || !token || !jql) { console.error('jira search is missing credentials'); process.exit(2); }",
109
+ "const auth = Buffer.from(email + ':' + token, 'utf8').toString('base64');",
110
+ "let url;",
111
+ "try { url = new URL('/rest/api/3/search/jql', base.endsWith('/') ? base : base + '/'); }",
112
+ "catch { console.error('jira base url is invalid'); process.exit(2); }",
113
+ "const response = await fetch(url, {",
114
+ " method: 'POST',",
115
+ " headers: { Authorization: 'Basic ' + auth, Accept: 'application/json', 'Content-Type': 'application/json' },",
116
+ " body: JSON.stringify({ jql, maxResults: 50, fields: ['summary', 'updated', 'description'] }),",
117
+ "});",
118
+ "const text = await response.text();",
119
+ "if (!response.ok) { console.error('jira search failed HTTP ' + response.status); process.exit(1); }",
120
+ "process.stdout.write(text);",
121
+ ].join("\n");
122
+ const result = spawnSync(process.execPath, ["--input-type=module", "-e", script], {
123
+ encoding: "utf8",
124
+ timeout: 20_000,
125
+ env: {
126
+ PATH: env.PATH,
127
+ KILN_JIRA_BASE: credentials.baseUrl,
128
+ KILN_JIRA_EMAIL: credentials.email,
129
+ KILN_JIRA_TOKEN: credentials.apiToken,
130
+ KILN_JIRA_JQL: query,
131
+ },
132
+ });
133
+ if (result.error || result.status !== 0) {
134
+ const detail = (result.stderr || result.error?.message || "jira search failed").trim().split("\n")[0] ?? "jira search failed";
135
+ throw new Error(redactSecrets(detail, credentials).slice(0, 300));
136
+ }
137
+ let parsed;
138
+ try {
139
+ parsed = JSON.parse(result.stdout || "{}");
140
+ }
141
+ catch {
142
+ throw new Error("Jira search returned data that is not JSON.");
143
+ }
144
+ return parseSearchIssues(parsed, credentials);
145
+ }
146
+ function parseSearchIssues(payload, credentials) {
147
+ const issues = typeof payload === "object" && payload !== null && "issues" in payload ? payload.issues : undefined;
148
+ if (!Array.isArray(issues))
149
+ throw new Error("Jira search returned data that is not a list of issues.");
150
+ const out = [];
151
+ for (const item of issues) {
152
+ if (typeof item !== "object" || item === null)
153
+ continue;
154
+ const record = item;
155
+ if (typeof record.key !== "string")
156
+ continue;
157
+ let key;
158
+ try {
159
+ key = normalizeIssueKey(record.key);
160
+ }
161
+ catch {
162
+ continue;
163
+ }
164
+ const fields = typeof record.fields === "object" && record.fields !== null ? record.fields : {};
165
+ const summary = typeof fields.summary === "string" ? fields.summary.trim().replace(/\s+/g, " ") : "";
166
+ if (!summary)
167
+ continue;
168
+ const updated = typeof fields.updated === "string" && fields.updated.trim() ? fields.updated.trim() : key;
169
+ const description = adfToMarkdown(fields.description).trim();
170
+ out.push({
171
+ key,
172
+ summary: redactSecrets(summary, credentials),
173
+ updated: redactSecrets(updated, credentials),
174
+ ...(description ? { description: redactSecrets(description, credentials) } : {}),
175
+ });
176
+ }
177
+ return out;
178
+ }
92
179
  export function normalizeIssueKey(raw) {
93
180
  const key = raw.trim().toUpperCase();
94
181
  if (!ISSUE_KEY.test(key)) {
package/dist/jobs.js CHANGED
@@ -5,7 +5,7 @@ import { appendEvent } from "./events.js";
5
5
  import { INVESTIGATION_WORKFLOW } from "./findings.js";
6
6
  import { jobBasename } from "./paths.js";
7
7
  import { JobSchema, parseIssueKey } from "./schemas.js";
8
- import { assertWorkflowName } from "./workflow.js";
8
+ import { assertWorkflowName, REVIEW_WORKFLOW } from "./workflow.js";
9
9
  export function newJobId(now = new Date()) {
10
10
  const stamp = now.toISOString().replace(/[-:TZ.]/g, "").slice(0, 14);
11
11
  return `j${stamp}${randomBytes(2).toString("hex")}`;
@@ -149,8 +149,9 @@ export function assertCanStack(repo, dependsOn, stack, known = []) {
149
149
  }
150
150
  const upstreamId = dependsOn[0];
151
151
  const upstream = known.find((job) => job.id === upstreamId);
152
- if (upstream?.workflow === INVESTIGATION_WORKFLOW) {
153
- throw new Error(`stack cannot target investigation job ${upstreamId}. An investigation does not land a branch.`);
152
+ if (upstream?.workflow === INVESTIGATION_WORKFLOW || upstream?.workflow === REVIEW_WORKFLOW) {
153
+ const kind = upstream.workflow === INVESTIGATION_WORKFLOW ? "investigation" : "review";
154
+ throw new Error(`stack cannot target ${kind} job ${upstreamId}. ${kind === "investigation" ? "An investigation" : "A review"} does not land a branch.`);
154
155
  }
155
156
  if (upstream && upstream.repo !== repo) {
156
157
  throw new Error(`stack needs upstream ${upstreamId} to be in repo ${repo}. That job is in repo ${upstream.repo}.`);
package/dist/reconcile.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { INVESTIGATION_WORKFLOW } from "./findings.js";
2
+ import { REVIEW_WORKFLOW } from "./workflow.js";
2
3
  /**
3
4
  * Why an attempt failed. Written under `<kilnDir>/attempts`, not on the job.
4
5
  * `auth-failed` is not an automatic retry: the job stays in Needs human.
@@ -118,17 +119,20 @@ export function isLanded(job) {
118
119
  * `deliver=local`: landed. `deliver=pr`: landed and the pull request has merged.
119
120
  * An investigation is done once the gate says approve. There is no pull request.
120
121
  */
122
+ function noPullRequest(workflow) {
123
+ return workflow === INVESTIGATION_WORKFLOW || workflow === REVIEW_WORKFLOW;
124
+ }
121
125
  export function isDone(job) {
122
126
  if (!isLanded(job))
123
127
  return false;
124
- if (job.workflow === INVESTIGATION_WORKFLOW)
128
+ if (noPullRequest(job.workflow))
125
129
  return true;
126
130
  if ((job.deliver ?? "local") !== "pr")
127
131
  return true;
128
132
  return job.delivery?.merged === true || job.delivery?.prState === "MERGED";
129
133
  }
130
134
  function landedState(job) {
131
- if (job.workflow === INVESTIGATION_WORKFLOW)
135
+ if (noPullRequest(job.workflow))
132
136
  return "done";
133
137
  if ((job.deliver ?? "local") !== "pr")
134
138
  return "done";
@@ -146,7 +150,7 @@ function landedState(job) {
146
150
  function needsDelivery(job) {
147
151
  if (job.error)
148
152
  return false;
149
- if (job.workflow === INVESTIGATION_WORKFLOW)
153
+ if (noPullRequest(job.workflow))
150
154
  return false;
151
155
  if ((job.deliver ?? "local") !== "pr")
152
156
  return false;
package/dist/schemas.js CHANGED
@@ -127,8 +127,45 @@ const maxFixRounds = z
127
127
  /**
128
128
  * The subset `kiln serve` settings can edit. The file schema below uses these
129
129
  * same field validators. Settings does not accept jira, tokens, adapter,
130
- * workflows, or sandbox image.
130
+ * workflows, or sandbox image. Trigger edits are enable/disable only.
131
131
  */
132
+ const TRIGGER_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
133
+ /**
134
+ * One trigger in `kiln.config.json`. `enabled` defaults to true.
135
+ * `schedule` uses `cron` (5 fields, local time). `jira` uses `jql` or
136
+ * `label` / `status` (compiled to JQL) and `every` (default `5m`).
137
+ * `pr` is the built-in Kiln-owned pull request source.
138
+ */
139
+ export const TriggerConfigSchema = z
140
+ .object({
141
+ id: z.string().regex(TRIGGER_ID, "Trigger id must start with a letter or digit."),
142
+ enabled: z.boolean().optional(),
143
+ kind: z.enum(["schedule", "jira", "pr"]),
144
+ cron: z.string().min(1).optional(),
145
+ every: z.string().min(1).optional(),
146
+ repo: z.string().regex(REPO_NAME).optional(),
147
+ jql: z.string().min(1).optional(),
148
+ label: z.string().min(1).optional(),
149
+ status: z.string().min(1).optional(),
150
+ })
151
+ .strict();
152
+ /** Maps a trigger id to create a job or attach to one. Hot-reloaded with the config. */
153
+ export const RouteConfigSchema = z
154
+ .object({
155
+ id: z.string().regex(TRIGGER_ID, "Route id must start with a letter or digit."),
156
+ trigger: z.string().regex(TRIGGER_ID),
157
+ action: z.enum(["create", "attach"]),
158
+ workflow: z.string().min(1).optional(),
159
+ repo: z.string().regex(REPO_NAME).optional(),
160
+ })
161
+ .strict();
162
+ /** Settings API body for enable/disable. Other trigger fields stay in the file. */
163
+ export const TriggerToggleSchema = z
164
+ .object({
165
+ id: z.string().regex(TRIGGER_ID),
166
+ enabled: z.boolean(),
167
+ })
168
+ .strict();
132
169
  export const EditableKilnSettingsSchema = z.object({
133
170
  repos: KilnReposSchema,
134
171
  stallTimeoutMs,
@@ -137,6 +174,7 @@ export const EditableKilnSettingsSchema = z.object({
137
174
  maxSameFingerprint,
138
175
  dailyDollarCap,
139
176
  sandbox: EditableSandboxSchema.optional(),
177
+ triggers: z.array(TriggerToggleSchema).optional(),
140
178
  });
141
179
  /** PUT /api/settings body. `revision` is the sha256 of the file the client loaded. */
142
180
  export const SettingsWriteSchema = EditableKilnSettingsSchema.extend({
@@ -156,14 +194,24 @@ export const KilnConfigFileSchema = z.object({
156
194
  workflows: z.record(z.string().regex(WORKFLOW_KEY), z.object({ sandbox: SandboxSettingsSchema.optional() })).optional(),
157
195
  repos: KilnReposSchema,
158
196
  jira: JiraConfigSchema.optional(),
197
+ /** Clock, Jira poll, and pull-request sources. Omitted entries use the built-ins. */
198
+ triggers: z.array(TriggerConfigSchema).optional(),
199
+ /** Trigger id → create a job or attach to an existing one. */
200
+ routes: z.array(RouteConfigSchema).optional(),
201
+ /**
202
+ * Reviewer prompt file. Relative paths resolve from the config directory.
203
+ * Omitted uses the packaged `prompts/kiln/review.md`.
204
+ */
205
+ reviewPrompt: z.string().min(1).optional(),
159
206
  });
160
207
  /**
161
208
  * Where a job came from. Adapters set this; tick and reconcile do not branch on it.
162
209
  * `ref` is an absolute spec path (`file`), `inline` or `stdin` (`prompt`), the
163
- * absolute ticket path (`tickets`), or the issue key (`jira`, such as `PROJ-123`).
210
+ * absolute ticket path (`tickets`), the issue key (`jira`, such as `SAMPLE-1`),
211
+ * or the trigger dedupe key (`trigger`).
164
212
  */
165
213
  export const JobSourceSchema = z.object({
166
- kind: z.enum(["file", "prompt", "tickets", "jira"]),
214
+ kind: z.enum(["file", "prompt", "tickets", "jira", "trigger"]),
167
215
  ref: z.string().min(1),
168
216
  });
169
217
  /**