@rasensio/aidlc 2.2.0 → 2.4.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 (38) hide show
  1. package/dist/autopilot/decisions.d.ts +72 -0
  2. package/dist/autopilot/decisions.d.ts.map +1 -0
  3. package/dist/autopilot/decisions.js +176 -0
  4. package/dist/autopilot/decisions.js.map +1 -0
  5. package/dist/autopilot/driver.d.ts +10 -4
  6. package/dist/autopilot/driver.d.ts.map +1 -1
  7. package/dist/autopilot/driver.js +181 -62
  8. package/dist/autopilot/driver.js.map +1 -1
  9. package/dist/autopilot/gitlab.d.ts +71 -0
  10. package/dist/autopilot/gitlab.d.ts.map +1 -0
  11. package/dist/autopilot/gitlab.js +159 -0
  12. package/dist/autopilot/gitlab.js.map +1 -0
  13. package/dist/autopilot/review.d.ts +31 -0
  14. package/dist/autopilot/review.d.ts.map +1 -0
  15. package/dist/autopilot/review.js +109 -0
  16. package/dist/autopilot/review.js.map +1 -0
  17. package/dist/commands/autopilot.d.ts +6 -1
  18. package/dist/commands/autopilot.d.ts.map +1 -1
  19. package/dist/commands/autopilot.js +29 -5
  20. package/dist/commands/autopilot.js.map +1 -1
  21. package/dist/core/host.d.ts +4 -2
  22. package/dist/core/host.d.ts.map +1 -1
  23. package/dist/core/host.js +4 -2
  24. package/dist/core/host.js.map +1 -1
  25. package/dist/doctor/migrations/index.d.ts.map +1 -1
  26. package/dist/doctor/migrations/index.js +8 -0
  27. package/dist/doctor/migrations/index.js.map +1 -1
  28. package/dist/doctor/migrations/roadmap-misfiled-items.d.ts +26 -0
  29. package/dist/doctor/migrations/roadmap-misfiled-items.d.ts.map +1 -0
  30. package/dist/doctor/migrations/roadmap-misfiled-items.js +80 -0
  31. package/dist/doctor/migrations/roadmap-misfiled-items.js.map +1 -0
  32. package/dist/roadmap/misfiled.d.ts +82 -0
  33. package/dist/roadmap/misfiled.d.ts.map +1 -0
  34. package/dist/roadmap/misfiled.js +163 -0
  35. package/dist/roadmap/misfiled.js.map +1 -0
  36. package/dist/setup/host.js +1 -1
  37. package/dist/setup/host.js.map +1 -1
  38. package/package.json +2 -2
@@ -20,10 +20,12 @@ import { microToUsd, priceUnits } from '../cost/rates.js';
20
20
  import { MODEL_ID_RE } from '../cost/types.js';
21
21
  import { hostFromConfigText } from '../core/host.js';
22
22
  import { classify, jobRef, parseRunJobs } from './ci-jobs.js';
23
+ import { GLAB_READ_RE, TAG_PIPELINES_ARGS, classifyJobs, jobsArgs, mergeArgs, mergedAnswer, mrCreateArgs, mrNumber, mrViewArgs, parseJobs, parseMr, releaseInFlight, retryArgs, } from './gitlab.js';
23
24
  import { REASON_MAX, RESET_WAIT_MAX_MS, codexEnd, emptyCodexProgress, firstLine, readClaudeResult, readCodexEvents, refusalSuffix, resetAt, stoppedBy, summaryLines, } from './result.js';
24
25
  import { capInForce, tokensLeft } from './cap.js';
25
26
  import { fmtTokens } from '../commands/cost.js';
26
27
  import { LEGACY_STOPS, pauseEnv, pendingReview, stopsInForce } from './stops.js';
28
+ import { stopsLogPath, unreviewedOpenQuestions } from './decisions.js';
27
29
  import { OUTCOME_SCHEMA, UUID_RE, budgetCents, resumePrompt, typedArgv, typedHarness, withOutcomeContract, } from './typed.js';
28
30
  /**
29
31
  * A value `process.kill` reads as one process. It reads 0 as this process's group and
@@ -49,6 +51,9 @@ function isReadOnly(cmd, args) {
49
51
  // (ci-infra-failure-reads-as-code-failure/AC-11); `run rerun` stays off the list.
50
52
  if (cmd === 'gh')
51
53
  return /^(pr checks|run list|pr view)\b/.test(a) || JOBS_READ_RE.test(a);
54
+ // The retry and the merge carry `--method`, so a dry run prints them (autopilot-gitlab-merge-requests/AC-16).
55
+ if (cmd === 'glab')
56
+ return a === '--version' || GLAB_READ_RE.test(a);
52
57
  if (cmd === 'git')
53
58
  return /^(rev-parse|status|merge-base)\b/.test(a);
54
59
  return /\broadmap next\b/.test(a) || (/\bcost\b/.test(a) && /--json/.test(a));
@@ -102,6 +107,18 @@ function transitionFailure(r) {
102
107
  }
103
108
  return r;
104
109
  }
110
+ /**
111
+ * How the item's change request is named: `pull request #7`, or `merge request !7` on
112
+ * GitLab, in messages, notices and prompts alike (autopilot-gitlab-merge-requests/AC-17).
113
+ */
114
+ export function ref(host, n) {
115
+ return host === 'gitlab' ? `merge request !${n}` : `pull request #${n}`;
116
+ }
117
+ /** `ref` at the start of a sentence. */
118
+ const Ref = (host, n) => {
119
+ const r = ref(host, n);
120
+ return r[0].toUpperCase() + r.slice(1);
121
+ };
105
122
  /**
106
123
  * @param pauses - `AIDLC_PAUSE_AFTER` as the agent will see it. Empty adds nothing,
107
124
  * so an item with no pauses gets the prompt it always did.
@@ -116,14 +133,14 @@ export function agentPrompt(item, pauses = '') {
116
133
  `the rule's review boundary: commit, release your claim, and stop.`
117
134
  : ''));
118
135
  }
119
- export function fixPrompt(item, failing) {
136
+ export function fixPrompt(item, failing, host = 'github') {
120
137
  return (`You are running unattended (AIDLC_UNATTENDED=1). Follow .aidlc/resources/rules/unattended.md. ` +
121
- `CI failed on pull request #${item.pr} for the AIDLC instance "${item.name}": ${failing.join(', ')}. ` +
138
+ `CI failed on ${ref(host, item.pr)} for the AIDLC instance "${item.name}": ${failing.join(', ')}. ` +
122
139
  `Find the cause, fix it on this branch, run the tests, commit, and stop. Do not push, merge or release.`);
123
140
  }
124
- export function conflictPrompt(item) {
141
+ export function conflictPrompt(item, host = 'github') {
125
142
  return (`You are running unattended (AIDLC_UNATTENDED=1). Follow .aidlc/resources/rules/unattended.md. ` +
126
- `Pull request #${item.pr} for the AIDLC instance "${item.name}" conflicts with main. Run ` +
143
+ `${Ref(host, item.pr)} for the AIDLC instance "${item.name}" conflicts with main. Run ` +
127
144
  `\`git fetch origin main\` and \`git merge origin/main\` on this branch, resolve every conflict keeping ` +
128
145
  `the intent of both sides, run the tests, commit the merge, and stop. Do not push, merge or release.`);
129
146
  }
@@ -938,16 +955,36 @@ function openPullRequest(ctx, io, state, item, budget) {
938
955
  failItem(ctx, state, item, `push failed: ${fail(push)}`, 'building');
939
956
  return;
940
957
  }
941
- // Off GitHub the pushed branch is the end: every later step is a `gh` call, and the work
942
- // itself succeeded, so the item is dropped rather than parked (gitlab-and-plain-git/AC-15, design D3).
958
+ // On plain git, and on GitLab without `glab`, the pushed branch is the end: the work itself
959
+ // succeeded, so the item is dropped rather than parked (gitlab-and-plain-git/AC-15, design D3).
960
+ // Upgrading without `glab` must not fail worse than that (autopilot-gitlab-merge-requests/AC-3, design D5).
943
961
  const host = hostOf(ctx);
944
- if (host !== 'github') {
945
- const said = `pushed ${item.branch} — host is ${host}, so autopilot opens no pull request; open and merge it yourself`;
946
- tell(ctx, item, said);
947
- notice(ctx, 'branch pushed', `${item.name}: ${said}`);
962
+ const stop = host === 'git'
963
+ ? `pushed ${item.branch} — host is git, so autopilot opens no pull request; open and merge it yourself`
964
+ : host === 'gitlab' && io.call('glab', ['--version'], { cwd: ctx.repo }).status !== 0
965
+ ? `pushed ${item.branch} — glab is not available, so autopilot opens no merge request; open and merge it yourself`
966
+ : null;
967
+ if (stop !== null) {
968
+ tell(ctx, item, stop);
969
+ notice(ctx, 'branch pushed', `${item.name}: ${stop}`);
948
970
  drop(state, item, budget);
949
971
  return;
950
972
  }
973
+ if (host === 'gitlab') {
974
+ if (!item.pr) {
975
+ // In the worktree, like `gh pr create`: glab reads host and project from its remote (AC-18).
976
+ const mr = io.call('glab', mrCreateArgs(item.branch), { cwd: item.worktree });
977
+ const n = mrNumber(mr.stdout);
978
+ if (mr.status !== 0 || (n === null && !mr.dry)) {
979
+ failItem(ctx, state, item, `mr create failed: ${fail(mr)}`, 'building');
980
+ return;
981
+ }
982
+ item.pr = n;
983
+ }
984
+ item.step = 'pr-open';
985
+ tell(ctx, item, `${ref(host, item.pr)} open`);
986
+ return;
987
+ }
951
988
  if (!item.pr) {
952
989
  const pr = io.call('gh', ['pr', 'create', '--base', 'main', '--head', item.branch, '--fill'], { cwd: item.worktree });
953
990
  const n = /\/pull\/(\d+)/.exec(pr.stdout)?.[1];
@@ -960,8 +997,15 @@ function openPullRequest(ctx, io, state, item, budget) {
960
997
  item.step = 'pr-open';
961
998
  tell(ctx, item, `pull request #${item.pr} open`);
962
999
  }
963
- /** `gh pr view`, or null when it cannot be read. Read-only, so a dry run makes it too. */
1000
+ /**
1001
+ * `gh pr view`, or on GitLab the merge request read in GitHub's words; null when it
1002
+ * cannot be read. Read-only, so a dry run makes it too (autopilot-gitlab-merge-requests/AC-4, AC-5).
1003
+ */
964
1004
  function prView(ctx, io, item) {
1005
+ if (hostOf(ctx) === 'gitlab') {
1006
+ const mr = io.call('glab', mrViewArgs(item.pr ?? 0), { cwd: ctx.repo });
1007
+ return mr.status === 0 ? parseMr(mr.stdout) : null;
1008
+ }
965
1009
  const r = io.call('gh', ['pr', 'view', String(item.pr), '--json', 'state,mergeable'], { cwd: ctx.repo });
966
1010
  if (r.status !== 0)
967
1011
  return null;
@@ -980,7 +1024,7 @@ function prView(ctx, io, item) {
980
1024
  function droppedIfMerged(ctx, state, item, view, budget) {
981
1025
  if (view?.state !== 'MERGED')
982
1026
  return false;
983
- tell(ctx, item, `pull request #${item.pr} was merged outside autopilot — no longer tracked`);
1027
+ tell(ctx, item, `${ref(hostOf(ctx), item.pr)} was merged outside autopilot — no longer tracked`);
984
1028
  drop(state, item, budget);
985
1029
  return true;
986
1030
  }
@@ -995,7 +1039,7 @@ function answeredByPerson(ctx, io, state, item, view, budget) {
995
1039
  return true;
996
1040
  }
997
1041
  if (view?.state === 'CLOSED') {
998
- tell(ctx, item, `pull request #${item.pr} was closed without merging — no longer tracked`);
1042
+ tell(ctx, item, `${ref(hostOf(ctx), item.pr)} was closed without merging — no longer tracked`);
999
1043
  drop(state, item, budget);
1000
1044
  return true;
1001
1045
  }
@@ -1023,8 +1067,8 @@ function closeOut(ctx, io, state, item, budget) {
1023
1067
  // Closed by hand already: nothing to transition, and a second close-out commit
1024
1068
  // would fail on an empty index (autopilot-stop-controls/AC-22).
1025
1069
  if (COMPLETE_RE.test(ctx.fs.read(`${ctx.repo}/.aidlc/state/${item.name}/phase-deployment.yaml`) ?? '')) {
1026
- tell(ctx, item, `pull request #${item.pr} merged, and already closed on main — no longer tracked`);
1027
- notice(ctx, 'item closed', `${item.name}: pull request #${item.pr} merged, and already closed on main`);
1070
+ tell(ctx, item, `${ref(hostOf(ctx), item.pr)} merged, and already closed on main — no longer tracked`);
1071
+ notice(ctx, 'item closed', `${item.name}: ${ref(hostOf(ctx), item.pr)} merged, and already closed on main`);
1028
1072
  drop(state, item, budget);
1029
1073
  return;
1030
1074
  }
@@ -1037,8 +1081,8 @@ function closeOut(ctx, io, state, item, budget) {
1037
1081
  return;
1038
1082
  }
1039
1083
  }
1040
- tell(ctx, item, `pull request #${item.pr} merged by you, and closed`);
1041
- notice(ctx, 'item closed', `${item.name}: pull request #${item.pr} merged by you, and closed`);
1084
+ tell(ctx, item, `${ref(hostOf(ctx), item.pr)} merged by you, and closed`);
1085
+ notice(ctx, 'item closed', `${item.name}: ${ref(hostOf(ctx), item.pr)} merged by you, and closed`);
1042
1086
  drop(state, item, budget);
1043
1087
  }
1044
1088
  /** Close the instance on main. Shared by both close-outs so they cannot drift. */
@@ -1085,7 +1129,7 @@ function handleConflict(ctx, io, state, item, budget) {
1085
1129
  dequeue(state, item);
1086
1130
  item.step = 'building';
1087
1131
  tell(ctx, item, `conflicts with main — relaunching the agent to resolve it (attempt ${tries + 1} of ${CONFLICT_FIX_LIMIT})`);
1088
- launchAgent(ctx, io, state, item, conflictPrompt(item), budget);
1132
+ launchAgent(ctx, io, state, item, conflictPrompt(item, hostOf(ctx)), budget);
1089
1133
  }
1090
1134
  /**
1091
1135
  * The two `merge=union` cost ledgers a worktree collects between commits. `name` is
@@ -1187,30 +1231,38 @@ function upToDateWithMain(ctx, io, state, item, resume, budget) {
1187
1231
  tell(ctx, item, 'brought up to date with main — waiting for CI on the new head');
1188
1232
  return false;
1189
1233
  }
1234
+ /**
1235
+ * Open questions in the item's worktree log that no person has reviewed. Read every pass, so a
1236
+ * CI-fix or conflict relaunch that logs one is seen (unattended-decision-diary/AC-10).
1237
+ */
1238
+ function openQuestions(ctx, item) {
1239
+ return unreviewedOpenQuestions(ctx.fs.read(stopsLogPath(item.worktree, item.name)));
1240
+ }
1190
1241
  function advancePr(ctx, io, state, item, budget) {
1191
1242
  const view = prView(ctx, io, item);
1192
- const atPr = itemStops(item).stop_at === 'pr';
1243
+ // An open question makes this pass a stop at the pull request: opened and watched, closed
1244
+ // out on a person's merge, never merged or released by autopilot
1245
+ // (unattended-decision-diary/AC-10, AC-11, design D2).
1246
+ const open = itemStops(item).stop_at === 'pr' ? 0 : openQuestions(ctx, item);
1247
+ const atPr = itemStops(item).stop_at === 'pr' || open > 0;
1193
1248
  // A person merging before CI finishes is still the merge the stop asked for.
1194
1249
  if (atPr ? answeredByPerson(ctx, io, state, item, view, budget) : droppedIfMerged(ctx, state, item, view, budget))
1195
1250
  return;
1196
- const r = io.call('gh', ['pr', 'checks', String(item.pr), '--json', 'name,bucket,link'], { cwd: ctx.repo });
1197
- let checks = [];
1198
- try {
1199
- checks = JSON.parse(r.stdout || '[]');
1200
- }
1201
- catch {
1251
+ const host = hostOf(ctx);
1252
+ const read = readChecks(ctx, io, item, host, view);
1253
+ if (read === null)
1202
1254
  return;
1203
- }
1255
+ const { checks } = read;
1204
1256
  if (checks.length === 0) {
1205
1257
  item.ci = 'waiting';
1206
1258
  // GitHub runs no pull_request checks on a PR that conflicts, so "no checks
1207
1259
  // yet" would otherwise be waited on forever (AC-12).
1208
1260
  if (view?.mergeable === 'CONFLICTING') {
1209
- tell(ctx, item, `pull request #${item.pr} conflicts with main and has no checks`);
1261
+ tell(ctx, item, `${ref(hostOf(ctx), item.pr)} conflicts with main and has no checks`);
1210
1262
  upToDateWithMain(ctx, io, state, item, 'pr-open', budget);
1211
1263
  return;
1212
1264
  }
1213
- tell(ctx, item, `pull request #${item.pr} — waiting for checks to start`);
1265
+ tell(ctx, item, `${ref(hostOf(ctx), item.pr)} — waiting for checks to start`);
1214
1266
  return;
1215
1267
  }
1216
1268
  if (checks.some((c) => c.bucket === 'pending')) {
@@ -1221,10 +1273,17 @@ function advancePr(ctx, io, state, item, budget) {
1221
1273
  const failing = failed.map((c) => c.name);
1222
1274
  if (failing.length === 0) {
1223
1275
  item.ci = 'passed';
1276
+ if (open > 0) {
1277
+ item.step = 'awaiting-merge';
1278
+ const questions = `${open} open question${open === 1 ? '' : 's'}`;
1279
+ tell(ctx, item, `CI green — ${questions} wait${open === 1 ? 's' : ''} for you: run aidlc autopilot review ${item.name}, then merge pull request #${item.pr}`);
1280
+ notice(ctx, 'open questions', `${item.name}: ${questions} — pull request #${item.pr} waits for you to merge it`);
1281
+ return;
1282
+ }
1224
1283
  if (atPr) {
1225
1284
  item.step = 'awaiting-merge';
1226
- tell(ctx, item, `CI green — pull request #${item.pr} waits for you to merge it`);
1227
- notice(ctx, 'merge needed', `${item.name}: pull request #${item.pr} waits for you to merge it`);
1285
+ tell(ctx, item, `CI green — ${ref(hostOf(ctx), item.pr)} waits for you to merge it`);
1286
+ notice(ctx, 'merge needed', `${item.name}: ${ref(hostOf(ctx), item.pr)} waits for you to merge it`);
1228
1287
  return;
1229
1288
  }
1230
1289
  item.step = 'queued';
@@ -1233,7 +1292,7 @@ function advancePr(ctx, io, state, item, budget) {
1233
1292
  tell(ctx, item, 'CI green, queued for merge');
1234
1293
  return;
1235
1294
  }
1236
- if (waitedOnRunner(ctx, io, item, failed))
1295
+ if (waitedOnRunner(ctx, io, item, failed, read.gitlab))
1237
1296
  return;
1238
1297
  item.ci = 'failed';
1239
1298
  const failures = (item.ci_failures ?? 0) + 1;
@@ -1252,7 +1311,33 @@ function advancePr(ctx, io, state, item, budget) {
1252
1311
  item.ci_failures = failures;
1253
1312
  tell(ctx, item, `CI failed (${failing.join(', ')}), relaunching the agent once`);
1254
1313
  item.step = 'building';
1255
- launchAgent(ctx, io, state, item, fixPrompt(item, failing), budget);
1314
+ launchAgent(ctx, io, state, item, fixPrompt(item, failing, hostOf(ctx)), budget);
1315
+ }
1316
+ /**
1317
+ * The item's checks, or null to do nothing with it this pass. On GitLab, the jobs of the
1318
+ * merge request's head pipeline, read as `gh pr checks` reads checks; no current head
1319
+ * pipeline is no checks yet (autopilot-gitlab-merge-requests/AC-6..AC-8).
1320
+ */
1321
+ function readChecks(ctx, io, item, host, view) {
1322
+ if (host === 'gitlab') {
1323
+ // No merge request read, no pipeline to read: wait for the next pass (AC-5).
1324
+ if (view === null)
1325
+ return null;
1326
+ if (view.pipeline === null || view.pipeline === undefined)
1327
+ return { checks: [] };
1328
+ const r = io.call('glab', jobsArgs(view.pipeline), { cwd: ctx.repo });
1329
+ const jobs = r.status === 0 ? parseJobs(r.stdout) : null;
1330
+ if (jobs === null)
1331
+ return null;
1332
+ return { checks: jobs.jobs, gitlab: { id: view.pipeline, ...jobs } };
1333
+ }
1334
+ const r = io.call('gh', ['pr', 'checks', String(item.pr), '--json', 'name,bucket,link'], { cwd: ctx.repo });
1335
+ try {
1336
+ return { checks: JSON.parse(r.stdout || '[]') };
1337
+ }
1338
+ catch {
1339
+ return null;
1340
+ }
1256
1341
  }
1257
1342
  /**
1258
1343
  * When no failing check tested the code — no runner took the job, or the runner went away
@@ -1261,24 +1346,17 @@ function advancePr(ctx, io, state, item, budget) {
1261
1346
  * (ci-infra-failure-reads-as-code-failure/AC-3, AC-4, AC-10). Returns false when the
1262
1347
  * failure is to be counted as before (AC-8, AC-9).
1263
1348
  */
1264
- function waitedOnRunner(ctx, io, item, failed) {
1265
- const runs = new Map();
1266
- for (const check of failed) {
1267
- const ref = jobRef(check.link);
1268
- if (ref === null || runs.has(ref.run))
1269
- continue;
1270
- // One read per run, not per check (AC-1).
1271
- const read = io.call('gh', ['api', `repos/{owner}/{repo}/actions/runs/${ref.run}/jobs`], { cwd: ctx.repo });
1272
- runs.set(ref.run, read.status === 0 ? parseRunJobs(read.stdout) : null);
1273
- }
1274
- const verdict = classify(failed, runs);
1349
+ function waitedOnRunner(ctx, io, item, failed, gitlab) {
1350
+ const verdict = gitlab === undefined ? githubVerdict(ctx, io, failed) : gitlabVerdict(gitlab);
1275
1351
  if (verdict.kind === 'tested')
1276
1352
  return false;
1277
1353
  item.ci = 'untested';
1278
1354
  const names = verdict.checks.join(', ');
1279
1355
  let refused = null;
1280
1356
  for (const run of verdict.fresh) {
1281
- const rerun = io.call('gh', ['run', 'rerun', run, '--failed'], { cwd: ctx.repo });
1357
+ const rerun = gitlab === undefined
1358
+ ? io.call('gh', ['run', 'rerun', run, '--failed'], { cwd: ctx.repo })
1359
+ : io.call('glab', retryArgs(run), { cwd: ctx.repo });
1282
1360
  if (rerun.status !== 0) {
1283
1361
  refused = fail(rerun);
1284
1362
  break;
@@ -1294,11 +1372,28 @@ function waitedOnRunner(ctx, io, item, failed) {
1294
1372
  // it — including a re-run that failed again between two passes, where item.ci already
1295
1373
  // read untested (AC-5, design D4).
1296
1374
  if (item.last_event?.text !== text) {
1297
- notice(ctx, 'runner needed', `${item.name}: pull request #${item.pr} — CI did not run its tests (${names})`);
1375
+ notice(ctx, 'runner needed', `${item.name}: ${ref(hostOf(ctx), item.pr)} — CI did not run its tests (${names})`);
1298
1376
  }
1299
1377
  tell(ctx, item, text);
1300
1378
  return true;
1301
1379
  }
1380
+ /** Whether the failing checks' Actions jobs ran a step that failed (ci-infra-failure-reads-as-code-failure). */
1381
+ function githubVerdict(ctx, io, failed) {
1382
+ const runs = new Map();
1383
+ for (const check of failed) {
1384
+ const ref = jobRef(check.link);
1385
+ if (ref === null || runs.has(ref.run))
1386
+ continue;
1387
+ // One read per run, not per check (AC-1).
1388
+ const read = io.call('gh', ['api', `repos/{owner}/{repo}/actions/runs/${ref.run}/jobs`], { cwd: ctx.repo });
1389
+ runs.set(ref.run, read.status === 0 ? parseRunJobs(read.stdout) : null);
1390
+ }
1391
+ return classify(failed, runs);
1392
+ }
1393
+ /** The same question from jobs already read: GitLab names the reason (autopilot-gitlab-merge-requests/AC-9..AC-11). */
1394
+ function gitlabVerdict(p) {
1395
+ return classifyJobs(p.jobs.filter((j) => j.bucket === 'fail' || j.bucket === 'cancel'), p.id, p.retried);
1396
+ }
1302
1397
  /**
1303
1398
  * Mark the deployment artifact complete so `aidlc transition` can close the
1304
1399
  * instance — whatever status the agent left it in; replacing only
@@ -1318,6 +1413,34 @@ function markDeploymentComplete(ctx, name) {
1318
1413
  ctx.fs.write(file, next);
1319
1414
  return true;
1320
1415
  }
1416
+ /** The last `release.yml` run is queued or running; unreadable is nothing in flight. */
1417
+ function githubReleaseInFlight(ctx, io) {
1418
+ const runs = io.call('gh', ['run', 'list', '--workflow=release.yml', '-L', '1', '--json', 'status'], { cwd: ctx.repo });
1419
+ try {
1420
+ const status = JSON.parse(runs.stdout || '[]')[0]?.status;
1421
+ return status === 'queued' || status === 'in_progress';
1422
+ }
1423
+ catch {
1424
+ // unreadable — treat as nothing in flight; the release command checks the tree itself
1425
+ return false;
1426
+ }
1427
+ }
1428
+ /** The latest tag pipeline is still going (autopilot-gitlab-merge-requests/AC-12, design D4). */
1429
+ function gitlabReleaseInFlight(ctx, io) {
1430
+ const r = io.call('glab', [...TAG_PIPELINES_ARGS], { cwd: ctx.repo });
1431
+ return r.status === 0 && releaseInFlight(r.stdout);
1432
+ }
1433
+ /**
1434
+ * Merge exactly `sha`, and only call it merged when GitLab says so: a merge answered
1435
+ * before it finished must not pull a `main` without it and release that
1436
+ * (autopilot-gitlab-merge-requests/AC-13, AC-14).
1437
+ */
1438
+ function gitlabMerge(ctx, io, item, sha) {
1439
+ const r = io.call('glab', mergeArgs(item.pr ?? 0, sha || 'HEAD'), { cwd: ctx.repo });
1440
+ if (r.status !== 0 || r.dry || mergedAnswer(r.stdout))
1441
+ return r;
1442
+ return { ...r, status: 1, stderr: `GitLab did not report ${ref('gitlab', item.pr)} merged` };
1443
+ }
1321
1444
  function serveQueue(ctx, io, state, budget) {
1322
1445
  const name = state.queue[0];
1323
1446
  if (!name)
@@ -1334,16 +1457,10 @@ function serveQueue(ctx, io, state, budget) {
1334
1457
  }
1335
1458
  if (droppedIfMerged(ctx, state, item, prView(ctx, io, item), budget))
1336
1459
  return;
1337
- const runs = io.call('gh', ['run', 'list', '--workflow=release.yml', '-L', '1', '--json', 'status'], { cwd: ctx.repo });
1338
- try {
1339
- const status = JSON.parse(runs.stdout || '[]')[0]?.status;
1340
- if (status === 'queued' || status === 'in_progress') {
1341
- tell(ctx, item, 'waiting — a release is already in flight');
1342
- return;
1343
- }
1344
- }
1345
- catch {
1346
- // unreadable — treat as nothing in flight; the release command checks the tree itself
1460
+ const host = hostOf(ctx);
1461
+ if (host === 'gitlab' ? gitlabReleaseInFlight(ctx, io) : githubReleaseInFlight(ctx, io)) {
1462
+ tell(ctx, item, 'waiting — a release is already in flight');
1463
+ return;
1347
1464
  }
1348
1465
  const branch = io.call('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: ctx.repo });
1349
1466
  if (branch.stdout.trim() !== 'main' && !ctx.dryRun) {
@@ -1363,7 +1480,9 @@ function serveQueue(ctx, io, state, budget) {
1363
1480
  const steps = [
1364
1481
  [
1365
1482
  'merge',
1366
- () => io.call('gh', ['pr', 'merge', String(item.pr), '--merge', '--match-head-commit', sha || 'HEAD'], { cwd: ctx.repo }),
1483
+ () => host === 'gitlab'
1484
+ ? gitlabMerge(ctx, io, item, sha)
1485
+ : io.call('gh', ['pr', 'merge', String(item.pr), '--merge', '--match-head-commit', sha || 'HEAD'], { cwd: ctx.repo }),
1367
1486
  ],
1368
1487
  ['pull', () => io.call('git', ['pull', '--rebase', '--autostash'], { cwd: ctx.repo })],
1369
1488
  ];
@@ -1542,13 +1661,13 @@ export function forgetItem(state, name) {
1542
1661
  * Approve the phase an item is waiting on. Touches only autopilot's own state; the
1543
1662
  * next pass carries the item on (autopilot-stop-controls/AC-16, AC-17).
1544
1663
  */
1545
- export function approveItem(state, name) {
1664
+ export function approveItem(state, name, host = 'github') {
1546
1665
  const item = state.items.find((i) => i.name === name);
1547
1666
  if (item === undefined) {
1548
1667
  return { ok: false, reason: `autopilot is not tracking "${name}" — \`aidlc autopilot status\` lists what it tracks` };
1549
1668
  }
1550
1669
  if (item.step === 'awaiting-merge') {
1551
- return { ok: false, reason: `${name} is waiting for you to merge pull request #${item.pr} — merging it is the approval` };
1670
+ return { ok: false, reason: `${name} is waiting for you to merge ${ref(host, item.pr)} — merging it is the approval` };
1552
1671
  }
1553
1672
  if (item.step !== 'awaiting-review' || item.review === undefined) {
1554
1673
  return { ok: false, reason: `${name} is not waiting for a review (it is ${item.step})` };
@@ -1615,14 +1734,14 @@ export function markStepChanges(state, before, now) {
1615
1734
  }
1616
1735
  }
1617
1736
  /** What a person is being waited on for, in words (autopilot-stop-controls/AC-14, AC-20). */
1618
- function waitingFor(i) {
1737
+ function waitingFor(i, host) {
1619
1738
  if (i.step === 'awaiting-review') {
1620
1739
  return ` — waiting for your review after ${i.review ?? 'a phase'} in ${i.worktree}; approve with \`aidlc autopilot approve ${i.name}\``;
1621
1740
  }
1622
1741
  if (i.step === 'approved')
1623
1742
  return ` — ${i.review ?? 'phase'} approved; carries on at the next pass with a free build slot`;
1624
1743
  if (i.step === 'awaiting-merge')
1625
- return ` — waiting for you to merge pull request #${i.pr}`;
1744
+ return ` — waiting for you to merge ${ref(host, i.pr)}`;
1626
1745
  return '';
1627
1746
  }
1628
1747
  /**
@@ -1641,13 +1760,13 @@ export function driverLine(stopped, state, now, lastPass = (iso) => iso) {
1641
1760
  export function queueLine(state) {
1642
1761
  return state.queue.length > 0 ? `merge queue: ${state.queue.join(' → ')}` : null;
1643
1762
  }
1644
- export function statusLines(ctx, state) {
1763
+ export function statusLines(ctx, state, host = 'github') {
1645
1764
  const lines = [driverLine(ctx.fs.exists(ctx.paths.stop), state, ctx.now())];
1646
1765
  if (state.items.length === 0)
1647
1766
  lines.push('no items in flight');
1648
1767
  for (const i of state.items) {
1649
1768
  lines.push(`${i.name}: ${i.step}${i.pr ? ` PR #${i.pr}` : ''} — CI failures ${i.ci_failures ?? 0}` +
1650
- `${waitingFor(i)}${i.error ? ` — last error: ${i.error}` : ''}`);
1769
+ `${waitingFor(i, host)}${i.error ? ` — last error: ${i.error}` : ''}`);
1651
1770
  }
1652
1771
  const queue = queueLine(state);
1653
1772
  if (queue !== null)