create-agent-rig 0.5.0 → 0.6.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.
Files changed (60) hide show
  1. package/CHANGELOG.md +140 -34
  2. package/README.md +12 -6
  3. package/package.json +1 -1
  4. package/packages/cli/dist/commands/init.js +6 -3
  5. package/packages/cli/dist/commands/upgrade.js +2 -2
  6. package/packages/cli/dist/index.js +46 -8
  7. package/packages/cli/dist/lib/manifest.js +10 -0
  8. package/scripts/prepare.mjs +1 -1
  9. package/templates/agent-os/init/AGENTS.md +11 -3
  10. package/templates/agent-os/init/CLAUDE.md +11 -3
  11. package/templates/agent-os/stack/aws-cdk/.agents/skills/post-deploy-verify/SKILL.md +8 -1
  12. package/templates/agent-os/stack/aws-cdk/.claude/agents/cdk-diff-reviewer.md +8 -1
  13. package/templates/agent-os/stack/aws-cdk/.claude/skills/post-deploy-verify/SKILL.md +8 -1
  14. package/templates/agent-os/stack/aws-cdk/.codex/agents/cdk-diff-reviewer.toml +1 -1
  15. package/templates/agent-os/stack/node-ts/.claude/rules/node-ts.md +29 -0
  16. package/templates/agent-os/universal/.agents/skills/check-premises/SKILL.md +4 -1
  17. package/templates/agent-os/universal/.agents/skills/loop/SKILL.md +298 -16
  18. package/templates/agent-os/universal/.agents/skills/pr-ship/SKILL.md +64 -6
  19. package/templates/agent-os/universal/.claude/agents/code-reviewer.md +8 -1
  20. package/templates/agent-os/universal/.claude/agents/prose-reviewer.md +8 -1
  21. package/templates/agent-os/universal/.claude/agents/security-scanner.md +8 -1
  22. package/templates/agent-os/universal/.claude/hooks/gate-stop-dod.mjs +28 -3
  23. package/templates/agent-os/universal/.claude/hooks/guard-rulebook.mjs +127 -0
  24. package/templates/agent-os/universal/.claude/hooks/lib/edit-input.mjs +23 -0
  25. package/templates/agent-os/universal/.claude/rules/autonomy.md +8 -0
  26. package/templates/agent-os/universal/.claude/rules/invariants.md +33 -3
  27. package/templates/agent-os/universal/.claude/scripts/decision-router.mjs +19 -1
  28. package/templates/agent-os/universal/.claude/scripts/doctor.mjs +351 -0
  29. package/templates/agent-os/universal/.claude/scripts/lib/gate-coverage.mjs +306 -0
  30. package/templates/agent-os/universal/.claude/scripts/lib/revalidation-points.mjs +28 -0
  31. package/templates/agent-os/universal/.claude/scripts/lib/verdict.mjs +37 -8
  32. package/templates/agent-os/universal/.claude/scripts/preflight.mjs +27 -1
  33. package/templates/agent-os/universal/.claude/scripts/queue/as-of.mjs +51 -0
  34. package/templates/agent-os/universal/.claude/scripts/queue/checkout.mjs +62 -2
  35. package/templates/agent-os/universal/.claude/scripts/queue/core.mjs +479 -9
  36. package/templates/agent-os/universal/.claude/scripts/queue/github-issues.mjs +89 -15
  37. package/templates/agent-os/universal/.claude/scripts/queue/index.mjs +138 -15
  38. package/templates/agent-os/universal/.claude/scripts/queue/jira.mjs +394 -46
  39. package/templates/agent-os/universal/.claude/scripts/queue/plan-md.mjs +68 -5
  40. package/templates/agent-os/universal/.claude/scripts/revalidate.mjs +316 -0
  41. package/templates/agent-os/universal/.claude/scripts/revalidation-report.mjs +180 -0
  42. package/templates/agent-os/universal/.claude/scripts/run-state.mjs +101 -3
  43. package/templates/agent-os/universal/.claude/scripts/stop-flag.mjs +15 -8
  44. package/templates/agent-os/universal/.claude/scripts/unattended-flag.mjs +239 -0
  45. package/templates/agent-os/universal/.claude/scripts/verdict.mjs +101 -4
  46. package/templates/agent-os/universal/.claude/settings.json +5 -1
  47. package/templates/agent-os/universal/.claude/skills/check-premises/SKILL.md +4 -1
  48. package/templates/agent-os/universal/.claude/skills/loop/SKILL.md +298 -16
  49. package/templates/agent-os/universal/.claude/skills/pr-ship/SKILL.md +64 -6
  50. package/templates/agent-os/universal/.codex/agents/code-reviewer.toml +1 -1
  51. package/templates/agent-os/universal/.codex/agents/prose-reviewer.toml +1 -1
  52. package/templates/agent-os/universal/.codex/agents/security-scanner.toml +1 -1
  53. package/templates/agent-os/universal/.codex/hooks.json +6 -1
  54. package/templates/agent-os/universal/AGENTS.md +3 -1
  55. package/templates/agent-os/universal/CLAUDE.md +3 -1
  56. package/templates/agent-os/universal/docs/decisions/gate-coverage.md +83 -0
  57. package/templates/agent-os/universal/docs/decisions/two-empty-endings.md +18 -6
  58. package/templates/agent-os/universal/layers.json +9 -0
  59. package/templates/hash-history.json +309 -49
  60. package/templates/release-ledger.json +9 -0
@@ -17,8 +17,14 @@
17
17
  //
18
18
  // { "adapter": "jira", "options": { "project": "ABC" } }
19
19
  // { "adapter": "jira", "options": { "jql": "project = ABC AND ..." } }
20
- import { duplicateOf, fingerprintOf, validateProposal } from './core.mjs';
21
- import { recordEscalation } from '../run-state.mjs';
20
+ // { "adapter": "jira", "options": { "project": "ABC", "owner": "my-repo" } }
21
+ //
22
+ // `owner` names this checkout for the `owner-<name>` label (AR-132): an item
23
+ // marked for another repository is held, and a checkout that declares no
24
+ // owner holds every marked item, since it cannot confirm a match.
25
+ import { duplicateOf, fingerprintOf, validateProposal, ownerOfLabels, lifecycleOf } from './core.mjs';
26
+ import { withAsOf } from './as-of.mjs';
27
+ import { recordEscalation, recordTakeUp } from '../run-state.mjs';
22
28
 
23
29
  export const name = 'jira';
24
30
 
@@ -101,8 +107,21 @@ export const toTicket = (issue) => {
101
107
  tier: labels.includes('elevated') ? 'elevated' : 'normal',
102
108
  blockedBy,
103
109
  blocks,
104
- priority: PRIORITY[String(fields.priority?.name ?? '').toLowerCase()] ?? 999,
110
+ // English names first; a localised board ("Höchste", "Mittel") falls back
111
+ // to the numeric priority id. Neither → 999.
112
+ priority:
113
+ PRIORITY[String(fields.priority?.name ?? '').toLowerCase()] ??
114
+ (Number.isFinite(Number(fields.priority?.id)) && Number(fields.priority?.id) > 0
115
+ ? Number(fields.priority?.id)
116
+ : 999),
105
117
  createdAt: toIso(fields.created),
118
+ // The take-up marker for revalidation at SELECT (`core.mjs` › revalidationOf):
119
+ // the tracker's own last-modified field. That it moves on every status
120
+ // change, edit and comment is Jira's contract, assumed and not checked
121
+ // here. `null` when the search did not carry it — never `''`, which would
122
+ // compare equal to itself and read as "unchanged" where the truth is "not
123
+ // looked".
124
+ updatedAt: toIso(fields.updated),
106
125
  // Flattened from the document description — the same text this adapter
107
126
  // already reads internally, now visible to the shared hygiene checks.
108
127
  body: descriptionTextOf(issue) || null,
@@ -112,6 +131,11 @@ export const toTicket = (issue) => {
112
131
  : labels.includes('trigger-human')
113
132
  ? 'human'
114
133
  : null,
134
+ // The repository this item belongs to (AR-132): `owner-<name>`, or null.
135
+ owner: ownerOfLabels(labels),
136
+ // The lifecycle and the scheduling flag (AR-144): `lifecycleOf` above the seam
137
+ // owns the semantics; this adapter only hands it the labels.
138
+ ...lifecycleOf(labels),
115
139
  };
116
140
  };
117
141
 
@@ -148,8 +172,55 @@ export const EXCLUDED_LABELS = ['triage', 'operator-queue'];
148
172
  * innermost groups. `AND` binds tighter than `OR` in JQL, so the flat form means
149
173
  * `(a AND b) OR empty`, which is the intent.
150
174
  */
175
+ /**
176
+ * A Jira project key: one uppercase letter, then up to nine of [A-Z0-9_].
177
+ * Exported so the refusal below can name the rule it applied (AR-51).
178
+ */
179
+ export const PROJECT_KEY = /^[A-Z][A-Z0-9_]{1,9}$/;
180
+
181
+ /**
182
+ * The one place `options.project` and `options.jql` from `.claude/queue.json`
183
+ * reach the query. Both used to be interpolated raw (AR-51): a committed
184
+ * `queue.json` — a file a pull request can edit — could make this adapter read
185
+ * another board, or anything the JQL grammar allows. A project key is now
186
+ * validated against PROJECT_KEY, and an explicit `jql` must begin with
187
+ * `project = <KEY>` — the same key when `options.project` is also given — so
188
+ * an override has to NAME this board. ⚠ Naming is not confinement: a query
189
+ * that leads with `project = AR` may still say `OR project = X` after it, and
190
+ * the credential's own scope is what bounds that. The residual is accepted
191
+ * because `.claude/queue.json` is part of the rulebook (`guard-rulebook`) and
192
+ * a declared elevated path, so a change widening it reaches the model lane.
193
+ */
194
+ /** The project key a config names — options.project, or the key options.jql leads with. */
195
+ export const projectKeyOf = ({ project = null, jql = null } = {}) => {
196
+ buildJql({ project, jql }); // the same refusals, once
197
+ if (project) return String(project);
198
+ return /^\s*project\s*=\s*([A-Z][A-Z0-9_]{1,9})\b/.exec(String(jql))[1];
199
+ };
200
+
151
201
  export const buildJql = ({ project = null, jql = null } = {}) => {
152
- if (jql) return jql;
202
+ if (project !== null && project !== undefined && !PROJECT_KEY.test(String(project))) {
203
+ throw new Error(
204
+ `options.project ${JSON.stringify(project)} is not a Jira project key — it must match ` +
205
+ `${PROJECT_KEY.source}. It is interpolated into JQL, so anything else is refused, not quoted.`,
206
+ );
207
+ }
208
+ if (jql) {
209
+ const lead = /^\s*project\s*=\s*([A-Z][A-Z0-9_]{1,9})\b/.exec(String(jql));
210
+ if (!lead) {
211
+ throw new Error(
212
+ 'options.jql must begin with `project = <KEY>` (a key matching ' +
213
+ `${PROJECT_KEY.source}) — a query that does not name its project can read any board.`,
214
+ );
215
+ }
216
+ if (project && lead[1] !== project) {
217
+ throw new Error(
218
+ `options.jql names project ${lead[1]} while options.project is ${project} — an override ` +
219
+ 'may narrow the query, never point it at another board.',
220
+ );
221
+ }
222
+ return jql;
223
+ }
153
224
  if (!project) {
154
225
  throw new Error(
155
226
  'the jira adapter needs either options.project or options.jql in ' +
@@ -186,23 +257,91 @@ export const requireCredentials = (env = process.env) => {
186
257
  return { baseUrl, email: env.JIRA_EMAIL, token: env.JIRA_API_TOKEN };
187
258
  };
188
259
 
189
- const request = async (route, { method = 'GET', body = null, env = process.env } = {}) => {
260
+ /** Statuses worth one more try: rate-limited, or a gateway that will be back. */
261
+ const TRANSIENT = new Set([429, 502, 503, 504]);
262
+ const MAX_ATTEMPTS = 4;
263
+ const DEFAULT_TIMEOUT_MS = 20_000;
264
+ const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
265
+
266
+ /**
267
+ * The wait before the next attempt: `Retry-After` in seconds when the server
268
+ * names one, otherwise 500 ms doubling per attempt. Bounded by the attempt cap.
269
+ */
270
+ const MAX_RETRY_AFTER_MS = 60_000;
271
+ const retryDelayMs = (response, attempt) => {
272
+ // Seconds form only; the HTTP-date form is not parsed and falls to backoff
273
+ // (› "falls back to the backoff when Retry-After is an HTTP-date, which it
274
+ // does not parse"). Capped, because a header is input like any other:
275
+ // `Retry-After: 86400` must not sleep the loop for a day (› "caps Retry-After
276
+ // so a hostile header cannot sleep the loop for a day").
277
+ const header = Number(response?.headers?.get?.('Retry-After'));
278
+ if (Number.isFinite(header) && header > 0) return Math.min(header * 1000, MAX_RETRY_AFTER_MS);
279
+ return 500 * 2 ** (attempt - 1);
280
+ };
281
+
282
+ /**
283
+ * One HTTP call to Jira, with the three things AR-54 added to it:
284
+ *
285
+ * - a timeout (`timeoutMs`, default 20 s) through an AbortController — a stalled
286
+ * connection used to block selection forever, and a loop that cannot read its
287
+ * queue must stop, not hang;
288
+ * - a retry on 429/502/503/504, at most `MAX_ATTEMPTS`, honouring `Retry-After`;
289
+ * 401/403/404 and every other status fail at once — a bad credential is not
290
+ * transient, and retrying it only delays the diagnosis;
291
+ * - `retry.sleep` injectable, so a test measures the delay it would have waited
292
+ * instead of waiting it.
293
+ *
294
+ * Pinned in the generator's `test/template/queue-jira.test.ts` (absent in a
295
+ * generated rig) › "hands fetch an AbortSignal", › "rejects naming the timeout
296
+ * and the route when fetch never resolves", › "a 429 followed by a 200 yields
297
+ * the 200 body", › "sleeps for the Retry-After the 429 carried, in
298
+ * milliseconds", › "gives up after four consecutive 503s, naming the status and
299
+ * the attempts" and › "does not retry a 401 — a bad credential is not transient".
300
+ */
301
+ const request = async (
302
+ route,
303
+ { method = 'GET', body = null, env = process.env, timeoutMs = DEFAULT_TIMEOUT_MS, retry = {} } = {},
304
+ ) => {
190
305
  const { baseUrl, email, token } = requireCredentials(env);
191
- const response = await fetch(`${baseUrl}${route}`, {
192
- method,
193
- headers: {
194
- Authorization: `Basic ${Buffer.from(`${email}:${token}`).toString('base64')}`,
195
- Accept: 'application/json',
196
- ...(body ? { 'Content-Type': 'application/json' } : {}),
197
- },
198
- ...(body ? { body: JSON.stringify(body) } : {}),
199
- });
200
- if (!response.ok) {
306
+ const sleep = retry.sleep ?? defaultSleep;
307
+ for (let attempt = 1; ; attempt += 1) {
308
+ const controller = new AbortController();
309
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
310
+ let response;
311
+ let payload = null;
312
+ try {
313
+ response = await fetch(`${baseUrl}${route}`, {
314
+ method,
315
+ headers: {
316
+ Authorization: `Basic ${Buffer.from(`${email}:${token}`).toString('base64')}`,
317
+ Accept: 'application/json',
318
+ ...(body ? { 'Content-Type': 'application/json' } : {}),
319
+ },
320
+ ...(body ? { body: JSON.stringify(body) } : {}),
321
+ signal: controller.signal,
322
+ });
323
+ // The body is read INSIDE the timed region: headers can arrive and the
324
+ // body then stall, which is the same hung connection with a 200 on it
325
+ // (› "keeps the timeout armed while the body is read").
326
+ if (response.ok && response.status !== 204) payload = await response.json();
327
+ } catch (error) {
328
+ if (error?.name === 'AbortError' || controller.signal.aborted) {
329
+ throw new Error(`jira ${method} ${route} timed out after ${timeoutMs} ms`, { cause: error });
330
+ }
331
+ throw error;
332
+ } finally {
333
+ clearTimeout(timer);
334
+ }
335
+ if (response.ok) return payload;
336
+ if (TRANSIENT.has(response.status) && attempt < MAX_ATTEMPTS) {
337
+ await sleep(retryDelayMs(response, attempt));
338
+ continue;
339
+ }
201
340
  // The status alone; never echo the response body, which can carry the token
202
341
  // back in an error envelope.
203
- throw new Error(`jira ${method} ${route} failed: ${response.status} ${response.statusText}`);
342
+ const attempts = TRANSIENT.has(response.status) ? ` after ${attempt} attempts` : '';
343
+ throw new Error(`jira ${method} ${route} failed: ${response.status} ${response.statusText}${attempts}`);
204
344
  }
205
- return response.status === 204 ? null : response.json();
206
345
  };
207
346
 
208
347
  // `description` is requested because the triage dedupe matches the fingerprint
@@ -214,14 +353,26 @@ const request = async (route, { method = 'GET', body = null, env = process.env }
214
353
  // decides how hard this is to notice: sending the joined form the retired query
215
354
  // parameter wanted answers `400 Invalid request payload`, not a 200 with empty
216
355
  // fields. It fails loudly, so a wrong value here cannot sit undetected.
217
- const FIELDS = ['summary', 'status', 'labels', 'priority', 'created', 'issuelinks', 'description'];
356
+ const FIELDS = [
357
+ 'summary',
358
+ 'status',
359
+ 'labels',
360
+ 'priority',
361
+ 'created',
362
+ 'updated',
363
+ 'issuelinks',
364
+ 'description',
365
+ ];
218
366
 
219
367
  // --- the adapter contract ------------------------------------------------------
220
368
 
221
369
  /**
222
370
  * Query fresh every time — the queue changes as the loop closes items and
223
371
  * unblocks their dependents, so a list read at the start of a run is wrong by the
224
- * second task. `issues` is the offline seam the tests use.
372
+ * second task. `issues` is the offline seam the tests use. Since AR-54 `limit`
373
+ * is the PAGE size, not a result cap: `search` walks every page up to its own
374
+ * `hardCap`, which this function leaves at the default (› "returns both pages
375
+ * as one list").
225
376
  */
226
377
  export const listEligible = async ({
227
378
  issues = null,
@@ -254,18 +405,106 @@ export const listEligible = async ({
254
405
  * path and not the auth. Both searching call sites (`listEligible` and the
255
406
  * `proposeTriage` dedupe) come through here, which is why one fix covers both.
256
407
  *
257
- * Not handled here on purpose: the response also carries `nextPageToken` for
258
- * cursor pagination, so a board with more open issues than `limit` still loses
259
- * its tail — as does a retry policy and a request timeout. Those belong together
260
- * in one change; half a pagination interface with nothing testing it is worse
261
- * than none.
408
+ * Pages through `nextPageToken` until the server sends none, or `hardCap`
409
+ * issues (default 1000) are in hand, or `maxPages` requests (default 100) have
410
+ * been made — each cap is announced on stderr, never
411
+ * silent, because a board whose tail is dropped is exactly the board the loop
412
+ * would otherwise believe it had read. `timeoutMs` and `retry` travel down to
413
+ * every page. Pinned in the generator's `test/template/queue-jira.test.ts`
414
+ * (absent in a generated rig) › "returns both pages as one list", › "sends the
415
+ * token from page 1 in the body of the request for page 2" and › "stops at
416
+ * hardCap and says on stderr that the list was capped".
262
417
  */
263
- export const search = async ({ project = null, jql = null, limit = 100, env = process.env } = {}) =>
264
- request('/rest/api/3/search/jql', {
265
- method: 'POST',
266
- body: { jql: buildJql({ project, jql }), maxResults: limit, fields: FIELDS },
267
- env,
268
- });
418
+ export const search = async ({
419
+ project = null,
420
+ jql = null,
421
+ limit = 100,
422
+ env = process.env,
423
+ timeoutMs = DEFAULT_TIMEOUT_MS,
424
+ retry = {},
425
+ hardCap = 1000,
426
+ maxPages = 100,
427
+ } = {}) => {
428
+ const query = buildJql({ project, jql });
429
+ const issues = [];
430
+ // Bounds beside hardCap, because a cap on issues alone is no bound at all
431
+ // against a server that repeats a token: a token equal to the one just sent
432
+ // ends the walk, and so does `maxPages` (default 100 requests), each with a
433
+ // stderr line. An EMPTY page with a fresh token is NOT a stop — the enhanced
434
+ // search endpoint may return short or empty pages while later pages exist,
435
+ // and stopping there drops a real tail (› "keeps walking past an empty page
436
+ // that carries a fresh token", › "stops paging when a page brings no issues,
437
+ // even if the token repeats", › "caps the number of requests outright, and
438
+ // says so on stderr").
439
+ let nextPageToken = null;
440
+ let pages = 0;
441
+ do {
442
+ pages += 1;
443
+ const page = await request('/rest/api/3/search/jql', {
444
+ method: 'POST',
445
+ body: {
446
+ jql: query,
447
+ maxResults: limit,
448
+ fields: FIELDS,
449
+ ...(nextPageToken ? { nextPageToken } : {}),
450
+ },
451
+ env,
452
+ timeoutMs,
453
+ retry,
454
+ });
455
+ const received = page?.issues ?? [];
456
+ issues.push(...received.slice(0, Math.max(0, hardCap - issues.length)));
457
+ const sent = nextPageToken;
458
+ nextPageToken = page?.isLast === true ? null : (page?.nextPageToken ?? null);
459
+ if (nextPageToken && nextPageToken === sent) {
460
+ process.stderr.write(
461
+ `jira search: the server repeated page token ${JSON.stringify(sent)} — ` +
462
+ 'stopping the walk; the tail of this board may not have been read\n',
463
+ );
464
+ break;
465
+ }
466
+ if (nextPageToken && pages >= maxPages) {
467
+ process.stderr.write(
468
+ `jira search: capped at ${maxPages} requests with more pages available — ` +
469
+ 'the tail of this board was not read; raise maxPages or narrow the JQL\n',
470
+ );
471
+ break;
472
+ }
473
+ if (issues.length >= hardCap && nextPageToken) {
474
+ process.stderr.write(
475
+ `jira search: capped at ${hardCap} issues with more pages available — ` +
476
+ 'the tail of this board was not read; raise hardCap or narrow the JQL\n',
477
+ );
478
+ break;
479
+ }
480
+ } while (nextPageToken);
481
+ return { issues };
482
+ };
483
+
484
+ /**
485
+ * One item by key, mapped raw — closed included. `listEligible` drops closed
486
+ * items because selection must never take one; the close point needs to see
487
+ * exactly that one. Honours the same offline `issues` seam.
488
+ */
489
+ export const find = async (id, { issues = null, env = process.env } = {}) => {
490
+ if (issues) {
491
+ return issues.map(toTicket).find((ticket) => String(ticket.id) === String(id)) ?? null;
492
+ }
493
+ // 🔴 By key, never through `search`: `buildJql` carries `statusCategory != Done`
494
+ // for selection's sake, so a search can never return the closed item this
495
+ // point exists to see. A 404 is "the tracker has no such item" — `null`;
496
+ // every other failure is raised as it is.
497
+ try {
498
+ const issue = await request(
499
+ `/rest/api/3/issue/${encodeURIComponent(String(id))}?fields=${FIELDS.join(',')}`,
500
+ { env },
501
+ );
502
+ return issue ? toTicket(issue) : null;
503
+ } catch (error) {
504
+ if (/ 404 /.test(String(error?.message))) return null;
505
+ throw error;
506
+ }
507
+ };
269
508
 
270
509
  export const resolveBlockers = (ticket) => (ticket.blockedBy ?? []).filter((b) => !b.resolved);
271
510
 
@@ -274,6 +513,52 @@ export const resolveBlockers = (ticket) => (ticket.blockedBy ?? []).filter((b) =
274
513
  * worked while it still reads as available is invisible to the human and
275
514
  * re-selectable by the very next query.
276
515
  */
516
+ /**
517
+ * Re-record the item's marker after a write of this adapter's own (AR-140).
518
+ *
519
+ * Every write here — a claim, a comment, a close, an escalation — moves the
520
+ * tracker's `updated`, and the next revalidation compared against the take-up
521
+ * from before it — the generator's journal records one run whose every
522
+ * BEFORE_PR catch was a hold on its own comment (`revalidation-report.mjs`
523
+ * over that run). So the marker is read back after the write
524
+ * and recorded as the take-up in the declared run; a hold that still fires is
525
+ * a move by something other than this adapter.
526
+ *
527
+ * ⚠ Limit: only writes made THROUGH this adapter re-baseline. A comment the
528
+ * session posts by another route — a REST call by hand, a connector — moves
529
+ * the marker like anyone else's, and the next check holds on it.
530
+ *
531
+ * Best-effort, like `proposeTriage`'s baseline: the write has landed by now,
532
+ * and a read-back the tracker refused or a stale run directory is announced on
533
+ * stderr, never thrown — a thrown write is retried and lands twice.
534
+ */
535
+ const recordMarker = (ticket, updatedAt, env) => {
536
+ try {
537
+ recordTakeUp(env.RIG_RUN_DIR, { id: ticket.id, updatedAt });
538
+ } catch (error) {
539
+ process.stderr.write(
540
+ `${ticket.id}: the write landed, but its marker was NOT re-recorded in ` +
541
+ `${env.RIG_RUN_DIR} — ${error.message}\n`,
542
+ );
543
+ }
544
+ };
545
+
546
+ const rebaseline = async (ticket, env) => {
547
+ if (!env?.RIG_RUN_DIR) return;
548
+ let updatedAt;
549
+ try {
550
+ const after = await request(`/rest/api/3/issue/${ticket.id}?fields=updated`, { env });
551
+ updatedAt = toIso(after?.fields?.updated);
552
+ } catch (error) {
553
+ process.stderr.write(
554
+ `${ticket.id}: the write landed, but its marker was NOT re-recorded in ` +
555
+ `${env.RIG_RUN_DIR} — ${error.message}\n`,
556
+ );
557
+ return;
558
+ }
559
+ recordMarker(ticket, updatedAt, env);
560
+ };
561
+
277
562
  export const claim = async (ticket, { transitionId = null, env = process.env } = {}) => {
278
563
  if (!transitionId) {
279
564
  const available = await request(`/rest/api/3/issue/${ticket.id}/transitions`, { env });
@@ -293,6 +578,7 @@ export const claim = async (ticket, { transitionId = null, env = process.env } =
293
578
  body: { transition: { id: transitionId } },
294
579
  env,
295
580
  });
581
+ await rebaseline(ticket, env);
296
582
  return { ok: true };
297
583
  };
298
584
 
@@ -304,9 +590,17 @@ export const comment = async (ticket, body, { env = process.env } = {}) => {
304
590
  },
305
591
  env,
306
592
  });
593
+ await rebaseline(ticket, env);
307
594
  return { ok: true };
308
595
  };
309
596
 
597
+ /**
598
+ * 🔴 `transitioned` is read back from the tracker, never inferred from the
599
+ * argument. The first version returned `Boolean(transitionId)` — a fact about
600
+ * the call, reported as a fact about the issue — so a transition the workflow
601
+ * rejected, or one that landed in a status outside the `done` category, was
602
+ * published as a close (AR-135).
603
+ */
310
604
  export const close = async (ticket, { prUrl = null, transitionId = null, env = process.env } = {}) => {
311
605
  await comment(ticket, prUrl ? `Landed in ${prUrl}.` : 'Closed by the run.', { env });
312
606
  if (transitionId) {
@@ -316,7 +610,20 @@ export const close = async (ticket, { prUrl = null, transitionId = null, env = p
316
610
  env,
317
611
  });
318
612
  }
319
- return { ok: true, transitioned: Boolean(transitionId) };
613
+ // One read-back for both facts the close needs: the status that proves the
614
+ // transition, and the marker the write produced (AR-140) — `comment` above
615
+ // already re-baselined once; this is the read after the transition.
616
+ const after = await request(`/rest/api/3/issue/${ticket.id}?fields=status,updated`, { env });
617
+ const fields = after?.fields ?? {};
618
+ // Through the same announce-never-throw path as `rebaseline`: the round
619
+ // that inlined `recordTakeUp` here reintroduced a throw on a stale run
620
+ // directory after the transition had landed.
621
+ if (env?.RIG_RUN_DIR) recordMarker(ticket, toIso(fields.updated), env);
622
+ return {
623
+ ok: true,
624
+ transitioned: statusCategory(fields) === 'done',
625
+ status: fields.status?.name ?? null,
626
+ };
320
627
  };
321
628
 
322
629
  /**
@@ -331,6 +638,7 @@ export const escalate = async (ticket, diagnosis, { env = process.env } = {}) =>
331
638
  body: { update: { labels: [{ add: 'escalated' }] } },
332
639
  env,
333
640
  });
641
+ await rebaseline(ticket, env);
334
642
  // Counted through the one recorder, never a counter of this adapter's own —
335
643
  // "twice in a row" has to mean the same thing on every tracker.
336
644
  recordEscalation(env.RIG_RUN_DIR);
@@ -352,8 +660,10 @@ export const triageItemFor = (proposal) => {
352
660
  `- part to change — ${proposal.part}`,
353
661
  `- proposed change — ${proposal.change}`,
354
662
  `- how the next run proves it — ${proposal.proof}`,
663
+ ...(proposal.measured ? [`- measured — ${proposal.measured}`, `- inferred — ${proposal.inferred}`] : []),
355
664
  '',
356
665
  `fingerprint: ${fingerprint}`,
666
+ ...(proposal.asOf ? [`asOf: ${proposal.asOf}`] : []),
357
667
  '',
358
668
  'The loop proposes; the owner patches. Self-applying a change to its own',
359
669
  'rulebook is how an unattended run drifts irreversibly.',
@@ -365,22 +675,36 @@ export const triageItemFor = (proposal) => {
365
675
  };
366
676
 
367
677
  /** File the proposal, or increment the one already carrying this fingerprint. */
678
+ /**
679
+ * The proposals on file, as `{ id, body }` — every `triage`-labelled issue, the
680
+ * body being its DESCRIPTION, which is where the fingerprint and `asOf` were
681
+ * written. An earlier dedupe mapped candidates through `toTicket` — which emits
682
+ * no body at all — so the predicate was always false and twenty identical stops
683
+ * filed twenty issues against the tracker.
684
+ */
685
+ export const listProposals = async ({
686
+ existing = null,
687
+ project = null,
688
+ jql = null,
689
+ env = process.env,
690
+ retry = {},
691
+ } = {}) => {
692
+ if (existing) return existing;
693
+ // Project-qualified, like every query this adapter sends (AR-51): the key is
694
+ // options.project, or the one options.jql leads with — `buildJql` refuses
695
+ // both when they disagree, so the triage query can only read this board.
696
+ const key = projectKeyOf({ project, jql });
697
+ const response = await search({ jql: `project = ${key} AND labels = triage ORDER BY created DESC`, env, retry });
698
+ return response.issues.map((issue) => ({ id: issue.key, body: descriptionTextOf(issue) }));
699
+ };
700
+
368
701
  export const proposeTriage = async (
369
- proposal,
370
- { project = null, existing = null, env = process.env } = {},
702
+ rawProposal,
703
+ { project = null, jql = null, existing = null, env = process.env, retry = {} } = {},
371
704
  ) => {
705
+ const proposal = withAsOf(rawProposal);
372
706
  const item = triageItemFor(proposal);
373
- // Compared against the issue's DESCRIPTION, which is where the fingerprint was
374
- // written. The previous version mapped candidates through `toTicket` — which
375
- // emits no body at all — so the predicate was always false and twenty identical
376
- // stops filed twenty issues against the tracker.
377
- const found =
378
- existing ??
379
- (await search({ jql: 'labels = triage ORDER BY created DESC', env })).issues.map((issue) => ({
380
- id: issue.key,
381
- body: descriptionTextOf(issue),
382
- }));
383
- const duplicate = duplicateOf(item, found);
707
+ const duplicate = duplicateOf(item, await listProposals({ existing, project, jql, env, retry }));
384
708
 
385
709
  if (duplicate) {
386
710
  await comment(duplicate, `Seen again (fingerprint ${item.fingerprint}). Incrementing.`, { env });
@@ -389,7 +713,7 @@ export const proposeTriage = async (
389
713
  if (!project) {
390
714
  throw new Error('filing a triage proposal needs options.project');
391
715
  }
392
- await request('/rest/api/3/issue', {
716
+ const created = await request('/rest/api/3/issue', {
393
717
  method: 'POST',
394
718
  body: {
395
719
  fields: {
@@ -406,5 +730,29 @@ export const proposeTriage = async (
406
730
  },
407
731
  env,
408
732
  });
409
- return { ok: true, filed: item.title, item };
733
+ // The proposal's own baseline (AR-138): its marker as filed, recorded as a
734
+ // take-up in the run that filed it, so the next run that is offered it
735
+ // compares against something. Read back rather than assumed — the marker is
736
+ // the tracker's, and `created` carries only the key. No run directory →
737
+ // nothing recorded, and the proposal is still filed.
738
+ //
739
+ // 🔴 Best-effort, and it says so: the proposal is FILED by now, and a throw
740
+ // here — a read-back the tracker refused, a stale or unwritable run
741
+ // directory in `updateState` — would tell the caller the filing failed when
742
+ // it succeeded, and the natural response (file again) double-files. The same
743
+ // defect `recordEscalation` closes for escalations; announced on stderr,
744
+ // because a baseline silently missing reads as a first sight later.
745
+ const id = created?.key ?? null;
746
+ if (id && env.RIG_RUN_DIR) {
747
+ try {
748
+ const after = await request(`/rest/api/3/issue/${id}?fields=updated`, { env });
749
+ recordTakeUp(env.RIG_RUN_DIR, { id, updatedAt: toIso(after?.fields?.updated) });
750
+ } catch (error) {
751
+ process.stderr.write(
752
+ `proposeTriage: ${id} is filed, but its baseline was NOT recorded in ` +
753
+ `${env.RIG_RUN_DIR} — ${error.message}\n`,
754
+ );
755
+ }
756
+ }
757
+ return { ok: true, filed: item.title, id, item };
410
758
  };