@fro.bot/systematic 3.32.3 → 3.33.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.
@@ -2,13 +2,27 @@
2
2
  // biome-ignore-all assist/source/organizeImports: generated bundle
3
3
  // Generated by scripts/generate-ce-connect-helper.ts; do not edit by hand.
4
4
  // Run `bun run ce-connect-helper:build` to regenerate.
5
- // source-digest: 9b652bc13739937297f63e9284e288f7e230df862d2c573fa399e9f69e3cae21
5
+ // source-digest: b8bad3da7ee1510ef1d5e677e8fc1d02a4f06997375ada8fe59ca7a81134751c
6
6
  // src/ce-connect-helper.ts
7
7
  import { spawnSync as spawnSync3 } from 'node:child_process'
8
- import fs8 from 'node:fs'
8
+ import fs10 from 'node:fs'
9
9
  import os2 from 'node:os'
10
+ import path8 from 'node:path'
11
+
12
+ // src/lib/connect-ci.ts
13
+ import {
14
+ createHash as createHash4,
15
+ randomUUID as randomUUID3,
16
+ } from 'node:crypto'
17
+ import fs7 from 'node:fs'
10
18
  import path6 from 'node:path'
11
19
 
20
+ // src/lib/connect-evidence.ts
21
+ import { spawnSync } from 'node:child_process'
22
+ import { createHash } from 'node:crypto'
23
+ import fs4 from 'node:fs'
24
+ import path3 from 'node:path'
25
+
12
26
  // src/lib/connect-config.ts
13
27
  import fs3 from 'node:fs'
14
28
  import os from 'node:os'
@@ -1118,33 +1132,63 @@ var MAX_ALLOW_PROJECTS = 64
1118
1132
  var MAX_ALLOW_PROJECT_LENGTH = 4096
1119
1133
  var MAX_CONNECT_CONFIG_BYTES = 1024 * 1024
1120
1134
  var MAX_DIAGNOSTIC_LENGTH = 200
1135
+ var MAX_CONNECT_GROUPS = 32
1136
+ var CONNECT_GROUP_NAME_PATTERN = /^[a-z0-9][a-z0-9-]{0,63}$/
1121
1137
  var ALLOW_PROJECTS_KEY = 'allow_projects'
1138
+ var GROUPS_KEY = 'groups'
1122
1139
  function isRecord(value) {
1123
1140
  return typeof value === 'object' && value !== null && !Array.isArray(value)
1124
1141
  }
1125
- function validateConnectBlock(value) {
1126
- if (!isRecord(value)) return { ok: false }
1127
- if (Object.keys(value).some((key) => key !== ALLOW_PROJECTS_KEY)) {
1128
- return { ok: false }
1129
- }
1130
- const list = Object.hasOwn(value, ALLOW_PROJECTS_KEY)
1131
- ? value[ALLOW_PROJECTS_KEY]
1132
- : []
1133
- if (!Array.isArray(list) || list.length > MAX_ALLOW_PROJECTS) {
1134
- return { ok: false }
1142
+ function readEntryList(value, min, max) {
1143
+ if (!Array.isArray(value) || value.length < min || value.length > max) {
1144
+ return
1135
1145
  }
1136
- const allowProjects = []
1137
- for (const entry of list) {
1146
+ const entries = []
1147
+ for (const entry of value) {
1138
1148
  if (
1139
1149
  typeof entry !== 'string' ||
1140
1150
  entry.length === 0 ||
1141
1151
  entry.length > MAX_ALLOW_PROJECT_LENGTH
1142
1152
  ) {
1143
- return { ok: false }
1153
+ return
1144
1154
  }
1145
- allowProjects.push(entry)
1155
+ entries.push(entry)
1156
+ }
1157
+ return entries
1158
+ }
1159
+ function readGroups(value) {
1160
+ if (!isRecord(value)) return
1161
+ const names = Object.keys(value)
1162
+ if (names.length > MAX_CONNECT_GROUPS) return
1163
+ const groups = {}
1164
+ for (const name of names) {
1165
+ if (!CONNECT_GROUP_NAME_PATTERN.test(name)) return
1166
+ const members = readEntryList(value[name], 1, MAX_ALLOW_PROJECTS)
1167
+ if (members === undefined) return
1168
+ groups[name] = members
1169
+ }
1170
+ return groups
1171
+ }
1172
+ function validateConnectBlock(value) {
1173
+ if (!isRecord(value)) return { ok: false }
1174
+ if (
1175
+ Object.keys(value).some(
1176
+ (key) => key !== ALLOW_PROJECTS_KEY && key !== GROUPS_KEY,
1177
+ )
1178
+ ) {
1179
+ return { ok: false }
1146
1180
  }
1147
- return { ok: true, allowProjects }
1181
+ const allowProjects = readEntryList(
1182
+ Object.hasOwn(value, ALLOW_PROJECTS_KEY) ? value[ALLOW_PROJECTS_KEY] : [],
1183
+ 0,
1184
+ MAX_ALLOW_PROJECTS,
1185
+ )
1186
+ if (allowProjects === undefined) return { ok: false }
1187
+ const groups = readGroups(
1188
+ Object.hasOwn(value, GROUPS_KEY) ? value[GROUPS_KEY] : {},
1189
+ )
1190
+ if (groups === undefined) return { ok: false }
1191
+ return { ok: true, allowProjects, groups }
1148
1192
  }
1149
1193
  function errorCode2(error) {
1150
1194
  return error instanceof Error && 'code' in error ? error.code : undefined
@@ -1216,10 +1260,20 @@ function isInside(child, parent) {
1216
1260
  !path2.isAbsolute(relative))
1217
1261
  )
1218
1262
  }
1263
+ function isGitEntry(target) {
1264
+ let stat
1265
+ try {
1266
+ stat = fs3.lstatSync(target)
1267
+ } catch (error) {
1268
+ const code = errorCode2(error)
1269
+ return code !== 'ENOENT' && code !== 'ENOTDIR'
1270
+ }
1271
+ return stat.isDirectory() ? entryExists(path2.join(target, 'HEAD')) : true
1272
+ }
1219
1273
  function insideCheckout(dir) {
1220
1274
  let current = dir
1221
1275
  for (;;) {
1222
- if (entryExists(path2.join(current, '.git'))) return true
1276
+ if (isGitEntry(path2.join(current, '.git'))) return true
1223
1277
  const parent = path2.dirname(current)
1224
1278
  if (parent === current) return false
1225
1279
  current = parent
@@ -1276,11 +1330,15 @@ function readConnectBlock(filePath) {
1276
1330
  return { kind: 'failed', reason: 'root must be an object' }
1277
1331
  }
1278
1332
  if (!Object.hasOwn(parsed, 'connect')) {
1279
- return { kind: 'read', allowProjects: [] }
1333
+ return { kind: 'read', allowProjects: [], groups: {} }
1280
1334
  }
1281
1335
  const validation = validateConnectBlock(parsed.connect)
1282
1336
  return validation.ok
1283
- ? { kind: 'read', allowProjects: validation.allowProjects }
1337
+ ? {
1338
+ kind: 'read',
1339
+ allowProjects: validation.allowProjects,
1340
+ groups: validation.groups,
1341
+ }
1284
1342
  : { kind: 'failed', reason: 'the connect block is not valid' }
1285
1343
  }
1286
1344
  function expandHome(entry, homeDir) {
@@ -1306,6 +1364,7 @@ function loadConnectAllowList(options) {
1306
1364
  const selection = selectConnectConfigFiles(dirs)
1307
1365
  const warnings = []
1308
1366
  const entries = new Set()
1367
+ const groups = {}
1309
1368
  let unreadable = false
1310
1369
  const readLayer = (file) => {
1311
1370
  const read = readConnectBlock(file.path)
@@ -1313,11 +1372,14 @@ function loadConnectAllowList(options) {
1313
1372
  if (read.kind === 'failed') {
1314
1373
  unreadable = true
1315
1374
  warnings.push(
1316
- `[systematic] connect settings in ${displaySafe(file.path)} could not be read (${read.reason}); the allow list from that file was ignored.`,
1375
+ `[systematic] connect settings in ${displaySafe(file.path)} could not be read (${read.reason}); the allow list and groups from that file were ignored.`,
1317
1376
  )
1318
1377
  return
1319
1378
  }
1320
1379
  for (const entry of read.allowProjects) entries.add(entry)
1380
+ for (const [name, members] of Object.entries(read.groups)) {
1381
+ groups[name] = [...members]
1382
+ }
1321
1383
  }
1322
1384
  const isUserLayer = (file) =>
1323
1385
  file.layer === 'user' || file.layer === 'user.local'
@@ -1339,6 +1401,7 @@ function loadConnectAllowList(options) {
1339
1401
  }
1340
1402
  return {
1341
1403
  allowProjects: [...entries],
1404
+ groups,
1342
1405
  unreadable,
1343
1406
  warnings,
1344
1407
  ...(customDirRefused !== undefined && { customDirRefused }),
@@ -1449,10 +1512,6 @@ function checkHostSupport(host, version, table = HOST_SUPPORT_TABLE) {
1449
1512
  }
1450
1513
 
1451
1514
  // src/lib/connect-evidence.ts
1452
- import { spawnSync } from 'node:child_process'
1453
- import { createHash } from 'node:crypto'
1454
- import fs4 from 'node:fs'
1455
- import path3 from 'node:path'
1456
1515
  var CONNECT_BOUNDS = Object.freeze({
1457
1516
  maxFilesPerProject: 4000,
1458
1517
  maxFileBytes: 256 * 1024,
@@ -6058,7 +6117,11 @@ function roundRobin(rules) {
6058
6117
  }
6059
6118
  function planOffer(input) {
6060
6119
  const size = Math.max(1, Math.floor(input.size))
6061
- const covered = new Map(input.coverage.map((item) => [item.ruleId, item]))
6120
+ const covered = new Map(
6121
+ input.coverage
6122
+ .filter((item) => item.selection === input.selection)
6123
+ .map((item) => [item.ruleId, item]),
6124
+ )
6062
6125
  const groups = new Map()
6063
6126
  let coveredBefore = 0
6064
6127
  for (const rule of input.rules) {
@@ -6733,6 +6796,7 @@ function renderHeader(model) {
6733
6796
  `Mode: ${model.mode}`,
6734
6797
  `Generated: ${model.generatedAt}`,
6735
6798
  hostLine(model),
6799
+ ...(model.selection === undefined ? [] : [selectionLine(model.selection)]),
6736
6800
  ]
6737
6801
  const sweep = model.sweep === undefined ? '' : renderSweepLine(model.sweep)
6738
6802
  if (sweep !== '') out.push(sweep)
@@ -7030,6 +7094,177 @@ var BATCH_BOUNDS = Object.freeze({
7030
7094
  maxRulesPerFamily: CONNECT_BOUNDS.maxRulesPerFamily,
7031
7095
  maxFamiliesPerRun: CONNECT_BOUNDS.maxFamiliesPerRun,
7032
7096
  })
7097
+ var MAX_SELECTION_WORDS = 32
7098
+ var MAX_SELECTION_WORD_LENGTH = 2048
7099
+ var GROUP_PREFIX = 'group:'
7100
+ var PROJECTS_PREFIX = 'projects:'
7101
+ var PROJECT_KEY = /^[0-9a-f]{12}$/
7102
+ var WORD_FORBIDDEN = /[\s\p{Cc}\p{Cf}]/u
7103
+ var MAX_SHOWN_SELECTION_TEXT = 100
7104
+ function selectionNamespace(selection) {
7105
+ return selection.kind === 'all' ? undefined : selection.id
7106
+ }
7107
+ function selectionIdFor(keys) {
7108
+ return createHash2('sha256')
7109
+ .update(`connect-selection-v1\x00${[...keys].sort().join(',')}`)
7110
+ .digest('hex')
7111
+ .slice(0, 16)
7112
+ }
7113
+ function shownSelectionText(text) {
7114
+ const flat = text.replace(/[\p{Cc}\p{Cf}]+/gu, ' ').trim()
7115
+ return flat.length > MAX_SHOWN_SELECTION_TEXT
7116
+ ? flat.slice(0, MAX_SHOWN_SELECTION_TEXT)
7117
+ : flat
7118
+ }
7119
+ function isWellFormedWord(word) {
7120
+ if (word.length > MAX_SELECTION_WORD_LENGTH) return false
7121
+ if (WORD_FORBIDDEN.test(word)) return false
7122
+ const value = word.startsWith(GROUP_PREFIX)
7123
+ ? word.slice(GROUP_PREFIX.length)
7124
+ : word.startsWith(PROJECTS_PREFIX)
7125
+ ? word.slice(PROJECTS_PREFIX.length)
7126
+ : undefined
7127
+ if (value === undefined || value === '') return false
7128
+ return word.startsWith(GROUP_PREFIX) || !value.split(',').includes('')
7129
+ }
7130
+ function conflict(detail) {
7131
+ return {
7132
+ status: 'refused',
7133
+ refusal: { reason: 'conflicting-selection', detail },
7134
+ }
7135
+ }
7136
+ function selectAllowed(allowed, roots) {
7137
+ return allowed.filter((item) => roots.has(item.mainRoot))
7138
+ }
7139
+ function resolveGroup(name, request) {
7140
+ if (!Object.hasOwn(request.groups, name)) {
7141
+ return {
7142
+ status: 'refused',
7143
+ refusal: { reason: 'unknown-group', group: shownSelectionText(name) },
7144
+ }
7145
+ }
7146
+ const members = request.groups[name] ?? []
7147
+ const allowedRoots = new Set(request.allowed.map((item) => item.mainRoot))
7148
+ const roots = new Set()
7149
+ for (const member of members) {
7150
+ const resolution = request.resolveMember(member)
7151
+ if (resolution.status === 'unmatched') {
7152
+ return {
7153
+ status: 'refused',
7154
+ refusal: {
7155
+ reason: 'group-member-not-allowed',
7156
+ group: shownSelectionText(name),
7157
+ member: shownSelectionText(member),
7158
+ memberReason: resolution.reason,
7159
+ },
7160
+ }
7161
+ }
7162
+ if (!allowedRoots.has(resolution.mainRoot)) {
7163
+ return {
7164
+ status: 'refused',
7165
+ refusal: {
7166
+ reason: 'group-member-not-allowed',
7167
+ group: shownSelectionText(name),
7168
+ member: shownSelectionText(member),
7169
+ },
7170
+ }
7171
+ }
7172
+ roots.add(resolution.mainRoot)
7173
+ }
7174
+ const projects = selectAllowed(request.allowed, roots)
7175
+ return {
7176
+ status: 'ok',
7177
+ projects,
7178
+ selection: {
7179
+ kind: 'group',
7180
+ name,
7181
+ id: selectionIdFor(projects.map((item) => projectKey(item.mainRoot))),
7182
+ },
7183
+ }
7184
+ }
7185
+ function matchPick(pick, allowed) {
7186
+ if (PROJECT_KEY.test(pick)) {
7187
+ const byKey = allowed.filter((item) => projectKey(item.mainRoot) === pick)
7188
+ if (byKey.length > 0) return byKey
7189
+ }
7190
+ return allowed.filter((item) => projectName(item.mainRoot) === pick)
7191
+ }
7192
+ function resolvePicks(picks, request) {
7193
+ const roots = new Set()
7194
+ for (const pick of picks) {
7195
+ const matches = matchPick(pick, request.allowed)
7196
+ const [only, ...rest] = matches
7197
+ if (only === undefined) {
7198
+ return {
7199
+ status: 'refused',
7200
+ refusal: {
7201
+ reason: 'unknown-project',
7202
+ project: shownSelectionText(pick),
7203
+ },
7204
+ }
7205
+ }
7206
+ if (rest.length > 0) {
7207
+ return {
7208
+ status: 'refused',
7209
+ refusal: {
7210
+ reason: 'ambiguous-project',
7211
+ project: shownSelectionText(pick),
7212
+ keys: matches.map((item) => projectKey(item.mainRoot)),
7213
+ },
7214
+ }
7215
+ }
7216
+ roots.add(only.mainRoot)
7217
+ }
7218
+ const projects = selectAllowed(request.allowed, roots)
7219
+ return {
7220
+ status: 'ok',
7221
+ projects,
7222
+ selection: {
7223
+ kind: 'projects',
7224
+ picks: projects.map((item) => projectName(item.mainRoot)),
7225
+ id: selectionIdFor(projects.map((item) => projectKey(item.mainRoot))),
7226
+ },
7227
+ }
7228
+ }
7229
+ function resolveSelection(request) {
7230
+ const { words } = request
7231
+ if (words.length > MAX_SELECTION_WORDS) return { status: 'invalid' }
7232
+ if (!words.every(isWellFormedWord)) return { status: 'invalid' }
7233
+ const distinct = [...new Set(words)]
7234
+ const groupWords = distinct.filter((word) => word.startsWith(GROUP_PREFIX))
7235
+ const pickWords = distinct.filter((word) => word.startsWith(PROJECTS_PREFIX))
7236
+ if (groupWords.length > 0 && pickWords.length > 0) {
7237
+ return conflict('group: and projects: words together')
7238
+ }
7239
+ if (groupWords.length > 1) return conflict('two different group: words')
7240
+ if (pickWords.length > 1) return conflict('two different projects: words')
7241
+ const [groupWord] = groupWords
7242
+ if (groupWord !== undefined) {
7243
+ return resolveGroup(groupWord.slice(GROUP_PREFIX.length), request)
7244
+ }
7245
+ const [pickWord] = pickWords
7246
+ if (pickWord !== undefined) {
7247
+ return resolvePicks(
7248
+ pickWord.slice(PROJECTS_PREFIX.length).split(','),
7249
+ request,
7250
+ )
7251
+ }
7252
+ return {
7253
+ status: 'ok',
7254
+ selection: { kind: 'all' },
7255
+ projects: request.allowed,
7256
+ }
7257
+ }
7258
+ function selectionLine(selection) {
7259
+ switch (selection.kind) {
7260
+ case 'all':
7261
+ return 'Selection: all allowed projects'
7262
+ case 'group':
7263
+ return `Selection: group ${inline(selection.name)} (id ${selection.id})`
7264
+ case 'projects':
7265
+ return `Selection: projects ${selection.picks.map(inline).join(', ')} (id ${selection.id})`
7266
+ }
7267
+ }
7033
7268
 
7034
7269
  // src/lib/connect-state.ts
7035
7270
  import { spawnSync as spawnSync2 } from 'node:child_process'
@@ -7499,6 +7734,7 @@ function candidateIdentity(candidate) {
7499
7734
  )
7500
7735
  .digest('hex')
7501
7736
  }
7737
+ var LEDGER_VERSION = 2
7502
7738
  var LEDGER_STATUSES = ['candidate', 'promoted', 'rejected']
7503
7739
  function isLedgerStatus(value) {
7504
7740
  return LEDGER_STATUSES.some((status) => status === value)
@@ -7563,9 +7799,13 @@ function isRuleId(value) {
7563
7799
  return typeof value === 'string' && RULE_ID2.test(value)
7564
7800
  }
7565
7801
  var SHA256 = /^[0-9a-f]{64}$/
7566
- var PROJECT_KEY = /^[0-9a-f]{12}$/
7802
+ var PROJECT_KEY2 = /^[0-9a-f]{12}$/
7803
+ var SELECTION_ID = /^[0-9a-f]{16}$/
7804
+ function isSelectionId(value) {
7805
+ return typeof value === 'string' && SELECTION_ID.test(value)
7806
+ }
7567
7807
  function isProjectKey(value) {
7568
- return typeof value === 'string' && PROJECT_KEY.test(value)
7808
+ return typeof value === 'string' && PROJECT_KEY2.test(value)
7569
7809
  }
7570
7810
  function isSha256(value) {
7571
7811
  return typeof value === 'string' && SHA256.test(value)
@@ -7577,15 +7817,19 @@ function isCoverageRecord(value) {
7577
7817
  isSha256(value.hash) &&
7578
7818
  plainText(value.runId, MAX_STRING_LENGTH) &&
7579
7819
  plainText(value.at, MAX_STRING_LENGTH) &&
7580
- (value.project === undefined || isProjectKey(value.project))
7820
+ (value.project === undefined || isProjectKey(value.project)) &&
7821
+ (value.selection === undefined || isSelectionId(value.selection))
7581
7822
  )
7582
7823
  }
7824
+ function coverageKey(record) {
7825
+ return `${record.selection ?? ''}\x00${record.ruleId}`
7826
+ }
7583
7827
  function isCoverage(value) {
7584
7828
  return (
7585
7829
  Array.isArray(value) &&
7586
7830
  value.length <= MAX_COVERAGE &&
7587
7831
  value.every(isCoverageRecord) &&
7588
- new Set(value.map((item) => item.ruleId)).size === value.length
7832
+ new Set(value.map(coverageKey)).size === value.length
7589
7833
  )
7590
7834
  }
7591
7835
  function parseLedger(text) {
@@ -7595,14 +7839,16 @@ function parseLedger(text) {
7595
7839
  } catch {
7596
7840
  return
7597
7841
  }
7598
- if (!isRecord3(value) || value.version !== 1) return
7842
+ if (!isRecord3(value) || (value.version !== 1 && value.version !== 2)) {
7843
+ return
7844
+ }
7599
7845
  const { entries, coverage } = value
7600
7846
  if (!Array.isArray(entries) || !entries.every(isLedgerEntry)) return
7601
- if (coverage === undefined) return { version: 1, entries }
7847
+ if (coverage === undefined) return { version: LEDGER_VERSION, entries }
7602
7848
  if (!isCoverage(coverage)) return
7603
7849
  return coverage.length === 0
7604
- ? { version: 1, entries }
7605
- : { version: 1, entries, coverage }
7850
+ ? { version: LEDGER_VERSION, entries }
7851
+ : { version: LEDGER_VERSION, entries, coverage }
7606
7852
  }
7607
7853
  function readLedger(root) {
7608
7854
  const read = readContainedFile(root, LEDGER_FILE, {
@@ -7610,7 +7856,7 @@ function readLedger(root) {
7610
7856
  })
7611
7857
  if (read.kind === 'refused') {
7612
7858
  if (read.reason === 'missing') {
7613
- return { status: 'ok', ledger: { version: 1, entries: [] } }
7859
+ return { status: 'ok', ledger: { version: LEDGER_VERSION, entries: [] } }
7614
7860
  }
7615
7861
  return {
7616
7862
  status: 'unreadable',
@@ -7739,7 +7985,10 @@ function applyCandidates(ledger, candidates, recordedAt) {
7739
7985
  let entries = [...ledger.entries]
7740
7986
  const outcomes = []
7741
7987
  for (const candidate of candidates) {
7742
- const outcome = classifyCandidate({ version: 1, entries }, candidate)
7988
+ const outcome = classifyCandidate(
7989
+ { version: LEDGER_VERSION, entries },
7990
+ candidate,
7991
+ )
7743
7992
  outcomes.push(outcome)
7744
7993
  if (outcome.standing === 'new') {
7745
7994
  entries.push({
@@ -7772,7 +8021,7 @@ function applyCandidates(ledger, candidates, recordedAt) {
7772
8021
  )
7773
8022
  }
7774
8023
  }
7775
- return { ledger: { version: 1, entries }, outcomes }
8024
+ return { ledger: { version: LEDGER_VERSION, entries }, outcomes }
7776
8025
  }
7777
8026
  function validPublishInput(input) {
7778
8027
  if (!isRunId(input.runId)) return 'invalid-run-id'
@@ -7790,6 +8039,9 @@ function validCoverageUpdate(update) {
7790
8039
  if (!isRunId(update.runId) || update.indexed.length > MAX_COVERAGE) {
7791
8040
  return false
7792
8041
  }
8042
+ if (update.selection !== undefined && !isSelectionId(update.selection)) {
8043
+ return false
8044
+ }
7793
8045
  const hashes = new Map()
7794
8046
  for (const item of update.indexed) {
7795
8047
  if (!isRuleId(item.ruleId) || !isSha256(item.hash)) return false
@@ -7800,7 +8052,9 @@ function validCoverageUpdate(update) {
7800
8052
  if (!(update.retain ?? []).every(isRuleId)) return false
7801
8053
  return update.covered.every((id) => hashes.has(id))
7802
8054
  }
7803
- function applyCoverage(prior, update, at) {
8055
+ function applyCoverage(allPrior, update, at) {
8056
+ const own = (record) => record.selection === update.selection
8057
+ const prior = allPrior.filter(own)
7804
8058
  const hashes = new Map(update.indexed.map((item) => [item.ruleId, item.hash]))
7805
8059
  const projects = new Map(
7806
8060
  update.indexed.map((item) => [item.ruleId, item.project]),
@@ -7822,9 +8076,11 @@ function applyCoverage(prior, update, at) {
7822
8076
  )
7823
8077
  .map((record) => [record.ruleId, record]),
7824
8078
  )
8079
+ const recorded = new Set()
7825
8080
  for (const ruleId of update.covered) {
7826
8081
  const hash = hashes.get(ruleId)
7827
8082
  if (hash !== undefined) {
8083
+ recorded.add(ruleId)
7828
8084
  const project = projects.get(ruleId)
7829
8085
  kept.set(ruleId, {
7830
8086
  ruleId,
@@ -7832,22 +8088,31 @@ function applyCoverage(prior, update, at) {
7832
8088
  runId: update.runId,
7833
8089
  at,
7834
8090
  ...(project === undefined ? {} : { project }),
8091
+ ...(update.selection === undefined
8092
+ ? {}
8093
+ : { selection: update.selection }),
7835
8094
  })
7836
8095
  }
7837
8096
  }
7838
- const records = [...kept.values()]
8097
+ const records = [
8098
+ ...allPrior.filter((record) => !own(record)),
8099
+ ...kept.values(),
8100
+ ]
8101
+ const byRule = (a, b) =>
8102
+ codepointOrder2(a.ruleId, b.ruleId) ||
8103
+ codepointOrder2(a.selection ?? '', b.selection ?? '')
7839
8104
  const overflow = records.length - MAX_COVERAGE
7840
8105
  if (overflow > 0) {
7841
8106
  const stale = records
7842
- .filter((record) => !hashes.has(record.ruleId))
8107
+ .filter((record) => !(own(record) && recorded.has(record.ruleId)))
7843
8108
  .sort((a, b) => codepointOrder2(a.at, b.at))
7844
8109
  .slice(0, overflow)
7845
- const gone = new Set(stale.map((record) => record.ruleId))
8110
+ const gone = new Set(stale.map(coverageKey))
7846
8111
  return records
7847
- .filter((record) => !gone.has(record.ruleId))
7848
- .sort((a, b) => codepointOrder2(a.ruleId, b.ruleId))
8112
+ .filter((record) => !gone.has(coverageKey(record)))
8113
+ .sort(byRule)
7849
8114
  }
7850
- return records.sort((a, b) => codepointOrder2(a.ruleId, b.ruleId))
8115
+ return records.sort(byRule)
7851
8116
  }
7852
8117
  function foldRun(prior, input, recordedAt) {
7853
8118
  const applied = applyCandidates(prior, input.candidates, recordedAt)
@@ -7863,8 +8128,8 @@ function foldRun(prior, input, recordedAt) {
7863
8128
  }
7864
8129
  function withEntries(ledger, entries) {
7865
8130
  return ledger.coverage === undefined
7866
- ? { version: 1, entries }
7867
- : { version: 1, entries, coverage: ledger.coverage }
8131
+ ? { version: LEDGER_VERSION, entries }
8132
+ : { version: LEDGER_VERSION, entries, coverage: ledger.coverage }
7868
8133
  }
7869
8134
  function stageRun(staging, files) {
7870
8135
  try {
@@ -7960,6 +8225,223 @@ function rewriteRunFiles(root, lock, runId, files) {
7960
8225
  return 'failed'
7961
8226
  }
7962
8227
  }
8228
+ var MAX_RUN_RECORD_BYTES = 2 * 1024 * 1024
8229
+ function readRunRecord(root, runId) {
8230
+ if (!isRunId(runId)) return
8231
+ const read = readContainedFile(root, `${RUNS_DIR}/${runId}/run.json`, {
8232
+ maxBytes: MAX_RUN_RECORD_BYTES,
8233
+ })
8234
+ if (read.kind === 'refused') return
8235
+ try {
8236
+ return JSON.parse(read.bytes.toString('utf8'))
8237
+ } catch {
8238
+ return
8239
+ }
8240
+ }
8241
+ var IMPORT_RUN_PREFIX = 'import-'
8242
+ var MAX_IMPORT_RUNS_LISTED = 50
8243
+ function newImportRunId(now = Date.now()) {
8244
+ return `${IMPORT_RUN_PREFIX}${newRunId(now)}`
8245
+ }
8246
+ function listImportRuns(root) {
8247
+ let names
8248
+ try {
8249
+ names = fs6.readdirSync(path5.join(root, RUNS_DIR))
8250
+ } catch {
8251
+ return []
8252
+ }
8253
+ return names
8254
+ .filter((name) => name.startsWith(IMPORT_RUN_PREFIX) && isRunId(name))
8255
+ .filter((name) =>
8256
+ lstatMaybe(path5.join(root, RUNS_DIR, name))?.isDirectory(),
8257
+ )
8258
+ .sort((a, b) => codepointOrder2(b, a))
8259
+ .slice(0, MAX_IMPORT_RUNS_LISTED)
8260
+ }
8261
+ function replaced(root, lock, runId, build) {
8262
+ try {
8263
+ return rewriteRunFiles(root, lock, runId, build()) === 'rewritten'
8264
+ } catch {
8265
+ return false
8266
+ }
8267
+ }
8268
+ function insertImportRun(root, plan, options = {}) {
8269
+ const acquired = acquireLock(root, options)
8270
+ if (acquired.status === 'busy') return acquired
8271
+ if (acquired.status === 'failed') {
8272
+ return { status: 'failed', reason: 'lock-failed' }
8273
+ }
8274
+ try {
8275
+ const read = readLedger(root)
8276
+ if (read.status !== 'ok') {
8277
+ return { status: 'failed', reason: 'ledger-unreadable' }
8278
+ }
8279
+ const decision = plan({
8280
+ ledger: read.ledger,
8281
+ importRuns: listImportRuns(root),
8282
+ })
8283
+ if (!decision.publish) return { status: 'unchanged' }
8284
+ if (!decision.runId.startsWith(IMPORT_RUN_PREFIX)) {
8285
+ return { status: 'failed', reason: 'invalid-run-id' }
8286
+ }
8287
+ const published = publishRun(
8288
+ root,
8289
+ acquired.lock,
8290
+ {
8291
+ runId: decision.runId,
8292
+ files: decision.files,
8293
+ candidates: decision.candidates,
8294
+ },
8295
+ options,
8296
+ )
8297
+ if (published.status === 'failed') {
8298
+ return { status: 'failed', reason: published.reason }
8299
+ }
8300
+ const current =
8301
+ published.ledger.written ||
8302
+ decision.onLedgerWriteFailed === undefined ||
8303
+ replaced(
8304
+ root,
8305
+ acquired.lock,
8306
+ decision.runId,
8307
+ decision.onLedgerWriteFailed,
8308
+ )
8309
+ return {
8310
+ status: 'published',
8311
+ runDir: published.runDir,
8312
+ ledger: published.ledger,
8313
+ ...(current ? {} : { runRecordCurrent: false }),
8314
+ }
8315
+ } finally {
8316
+ releaseLock(acquired.lock)
8317
+ }
8318
+ }
8319
+ var CI_RUN_FILE = 'ci-run.json'
8320
+ var INVOCATION_FILE = 'invocation.json'
8321
+ var MAX_CI_RUN_BYTES = 1024
8322
+ var MAX_INVOCATION_BYTES = 1024 * 1024
8323
+ var MAX_INVOCATION_NAME_LENGTH = 200
8324
+ var OBJECT_ID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/
8325
+ var REPO_SLUG = /^[a-z0-9][a-z0-9-]{0,38}\/[a-z0-9._-]{1,100}$/
8326
+ var INVOCATION_OUTCOMES = ['complete', 'degraded', 'busy', 'failed']
8327
+ function isInvocationOutcome(value) {
8328
+ return INVOCATION_OUTCOMES.includes(value)
8329
+ }
8330
+ function readCiRunMarker(root) {
8331
+ try {
8332
+ const stat = lstatMaybe(root)
8333
+ if (stat === undefined || !stat.isDirectory()) return
8334
+ const read = readContainedFile(root, CI_RUN_FILE, {
8335
+ maxBytes: MAX_CI_RUN_BYTES,
8336
+ })
8337
+ if (read.kind === 'refused') return
8338
+ const value = JSON.parse(read.bytes.toString('utf8'))
8339
+ if (!isRecord3(value) || Object.keys(value).length !== 2) return
8340
+ const { version, marker } = value
8341
+ return version === 1 && typeof marker === 'string' && SAFE_NAME.test(marker)
8342
+ ? marker
8343
+ : undefined
8344
+ } catch {
8345
+ return
8346
+ }
8347
+ }
8348
+ function optionalMatch(value, pattern) {
8349
+ return (
8350
+ value === undefined || (typeof value === 'string' && pattern.test(value))
8351
+ )
8352
+ }
8353
+ function parseInvocationProject(item) {
8354
+ if (!isRecord3(item)) return
8355
+ const { key, name, repo, commit } = item
8356
+ if (!isProjectKey(key) || !plainText(name, MAX_INVOCATION_NAME_LENGTH)) {
8357
+ return
8358
+ }
8359
+ if (!optionalMatch(repo, REPO_SLUG) || !optionalMatch(commit, OBJECT_ID)) {
8360
+ return
8361
+ }
8362
+ return {
8363
+ key,
8364
+ name,
8365
+ ...(typeof repo === 'string' ? { repo } : {}),
8366
+ ...(typeof commit === 'string' ? { commit } : {}),
8367
+ }
8368
+ }
8369
+ function parseInvocationProjects(value) {
8370
+ if (!Array.isArray(value) || value.length > MAX_ALLOW_PROJECTS) {
8371
+ return
8372
+ }
8373
+ const projects = []
8374
+ for (const item of value) {
8375
+ const project = parseInvocationProject(item)
8376
+ if (project === undefined) return
8377
+ projects.push(project)
8378
+ }
8379
+ return projects
8380
+ }
8381
+ function parseTerminal(value, marker, runId, projects) {
8382
+ const { outcome, finalLine, reason } = value
8383
+ if (!isInvocationOutcome(outcome) || finalLine !== `Connect ${outcome}`) {
8384
+ return
8385
+ }
8386
+ if (reason !== undefined && !plainText(reason, MAX_REASON_LENGTH)) {
8387
+ return
8388
+ }
8389
+ return {
8390
+ version: 1,
8391
+ marker,
8392
+ phase: 'terminal',
8393
+ outcome,
8394
+ finalLine,
8395
+ ...(reason === undefined ? {} : { reason }),
8396
+ ...(runId === undefined ? {} : { runId }),
8397
+ ...(projects === undefined ? {} : { projects }),
8398
+ }
8399
+ }
8400
+ function parseInvocation(value) {
8401
+ if (!isRecord3(value) || value.version !== 1) return
8402
+ const { marker, phase, runId } = value
8403
+ if (typeof marker !== 'string' || !SAFE_NAME.test(marker)) return
8404
+ if (runId !== undefined && !(typeof runId === 'string' && isRunId(runId))) {
8405
+ return
8406
+ }
8407
+ const projects =
8408
+ value.projects === undefined
8409
+ ? undefined
8410
+ : parseInvocationProjects(value.projects)
8411
+ if (value.projects !== undefined && projects === undefined) return
8412
+ if (phase === 'terminal') return parseTerminal(value, marker, runId, projects)
8413
+ if (phase !== 'begun') return
8414
+ if (runId === undefined || projects === undefined) return
8415
+ return { version: 1, marker, phase, runId, projects }
8416
+ }
8417
+ function writeInvocationRecord(root, record, options = {}) {
8418
+ try {
8419
+ const clean = parseInvocation(record)
8420
+ if (clean === undefined) return 'failed'
8421
+ options.beforeWrite?.(clean)
8422
+ const stat = lstatMaybe(root)
8423
+ if (stat === undefined || !stat.isDirectory()) return 'failed'
8424
+ writeFileAtomic(
8425
+ path5.join(root, INVOCATION_FILE),
8426
+ `${JSON.stringify(clean, null, 2)}
8427
+ `,
8428
+ )
8429
+ return 'written'
8430
+ } catch {
8431
+ return 'failed'
8432
+ }
8433
+ }
8434
+ function readInvocationRecord(root) {
8435
+ try {
8436
+ const read = readContainedFile(root, INVOCATION_FILE, {
8437
+ maxBytes: MAX_INVOCATION_BYTES,
8438
+ })
8439
+ if (read.kind === 'refused') return
8440
+ return parseInvocation(JSON.parse(read.bytes.toString('utf8')))
8441
+ } catch {
8442
+ return
8443
+ }
8444
+ }
7963
8445
  function pruneRuns(root, lock, options = {}) {
7964
8446
  if (!holdsLock(lock)) return { status: 'failed', reason: 'lock-lost' }
7965
8447
  const runs = path5.join(root, RUNS_DIR)
@@ -8018,6 +8500,9 @@ function decideLedgerEntry(root, input, options = {}) {
8018
8500
  if (prior === undefined) {
8019
8501
  return { status: 'failed', reason: 'unknown-candidate' }
8020
8502
  }
8503
+ if (input.onlyIfOpen === true && prior.status !== 'candidate') {
8504
+ return { status: 'failed', reason: 'not-open' }
8505
+ }
8021
8506
  const decided = {
8022
8507
  id: prior.id,
8023
8508
  kind: prior.kind,
@@ -8069,51 +8554,1737 @@ function clearLedger(root, options = {}) {
8069
8554
  }
8070
8555
  }
8071
8556
 
8072
- // src/lib/main-module.ts
8073
- import fs7 from 'node:fs'
8074
- import { fileURLToPath } from 'node:url'
8075
- function resolveRealPath(value) {
8076
- if (value === undefined || value === '') return
8077
- try {
8078
- return fs7.realpathSync(value)
8079
- } catch {
8080
- return
8081
- }
8557
+ // src/lib/connect-ci.ts
8558
+ var REPORT_MAX_CHARS = 60000
8559
+ var MANIFESTS_KEPT = 14
8560
+ var REPORT_FILE = 'report.md'
8561
+ var PAYLOAD_FILE = 'ci-payloads.json'
8562
+ var MANIFEST_FILE = 'ci-manifests.json'
8563
+ var DECISIONS_BEGIN = '<!-- connect-decisions:begin -->'
8564
+ var DECISIONS_END = '<!-- connect-decisions:end -->'
8565
+ var STATUS_LABEL = {
8566
+ promoted: 'Promoted',
8567
+ rejected: 'Rejected',
8568
+ }
8569
+ var MANIFEST_HASH_LENGTH = 16
8570
+ var CANDIDATE_ID = /^[0-9a-f]{64}$/
8571
+ var MANIFEST_HASH = /^[0-9a-f]{16}$/
8572
+ var DIGITS = /^[0-9]{1,20}$/
8573
+ var DATE = /^\d{4}-\d{2}-\d{2}$/
8574
+ var REPO = /^[a-z0-9][a-z0-9-]{0,38}\/[a-z0-9._-]{1,100}$/
8575
+ var TOKEN = /^[a-z][a-z0-9-]{0,39}$/
8576
+ function manifestMarker(hash) {
8577
+ return `<!-- connect-manifest:${hash} -->`
8578
+ }
8579
+ function decisionLine(id, status, ticked) {
8580
+ return `- [${ticked ? 'x' : ' '}] ${STATUS_LABEL[status]} <!-- connect-decision:${status}:${id} -->`
8581
+ }
8582
+ var MANIFEST_LINE = /^<!-- connect-manifest:([0-9a-f]{16}) -->[ \t]*$/
8583
+ var DECISION_LINE =
8584
+ /^- \[([ xX])\] (Promoted|Rejected) <!-- connect-decision:(promoted|rejected):([0-9a-f]{64}) -->[ \t]*$/
8585
+ var FENCE_LINE2 = /^ {0,3}(`{3,}|~{3,})(.*)$/
8586
+ function isRecord4(value) {
8587
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
8082
8588
  }
8083
- function moduleFilePath(moduleUrl) {
8084
- try {
8085
- return fileURLToPath(moduleUrl)
8086
- } catch {
8087
- return
8088
- }
8589
+ function isCandidateId(value) {
8590
+ return typeof value === 'string' && CANDIDATE_ID.test(value)
8089
8591
  }
8090
- function isMainModule(moduleUrl, argv = process.argv) {
8091
- const entryPath = resolveRealPath(argv[1])
8092
- const modulePath = resolveRealPath(moduleFilePath(moduleUrl))
8592
+ function isDigits(value) {
8593
+ return typeof value === 'string' && DIGITS.test(value)
8594
+ }
8595
+ function validDate(text) {
8596
+ if (!DATE.test(text)) return false
8597
+ const parsed = new Date(`${text}T00:00:00Z`)
8093
8598
  return (
8094
- entryPath !== undefined &&
8095
- modulePath !== undefined &&
8096
- entryPath === modulePath
8599
+ !Number.isNaN(parsed.getTime()) && parsed.toISOString().startsWith(text)
8097
8600
  )
8098
8601
  }
8099
-
8100
- // src/ce-connect-helper.ts
8101
- var CE_CONNECT_USAGE =
8102
- 'Usage: node connect.mjs <begin|validate|publish|decide|ledger show [--offset N]|ledger clear|ledger unlock>'
8103
- var OPS = [
8104
- 'begin',
8105
- 'validate',
8106
- 'publish',
8107
- 'decide',
8108
- 'ledger-show',
8109
- 'ledger-clear',
8110
- 'ledger-unlock',
8111
- ]
8112
- function isOp(value) {
8113
- return OPS.includes(value)
8602
+ function clip(text, max) {
8603
+ return Array.from(text).slice(0, max).join('').trimEnd()
8114
8604
  }
8115
- function isRecord4(value) {
8116
- return typeof value === 'object' && value !== null && !Array.isArray(value)
8605
+ function lineOf(value, max) {
8606
+ if (typeof value !== 'string') return
8607
+ const text = clip(
8608
+ sanitizeText(value)
8609
+ .replaceAll(
8610
+ `
8611
+ `,
8612
+ ' ',
8613
+ )
8614
+ .trim(),
8615
+ max,
8616
+ )
8617
+ return text === '' ? undefined : text
8618
+ }
8619
+ function paragraphOf(value, max) {
8620
+ if (typeof value !== 'string') return
8621
+ const text = clip(sanitizeText(value).trim(), max)
8622
+ return text === '' ? undefined : text
8623
+ }
8624
+ function tokenOf(value) {
8625
+ return typeof value === 'string' && TOKEN.test(value) ? value : 'unknown'
8626
+ }
8627
+ function lockOptions(options) {
8628
+ return {
8629
+ ...(options.processes === undefined
8630
+ ? {}
8631
+ : { processes: options.processes }),
8632
+ ...(options.now === undefined ? {} : { now: options.now }),
8633
+ }
8634
+ }
8635
+ function writeFileAtomic2(target, data) {
8636
+ const temporary = `${target}.tmp-${randomUUID3()}`
8637
+ try {
8638
+ const fd = fs7.openSync(temporary, 'wx', 384)
8639
+ try {
8640
+ fs7.writeSync(fd, data)
8641
+ fs7.fsyncSync(fd)
8642
+ } finally {
8643
+ fs7.closeSync(fd)
8644
+ }
8645
+ fs7.chmodSync(temporary, 384)
8646
+ fs7.renameSync(temporary, target)
8647
+ } catch (error) {
8648
+ fs7.rmSync(temporary, { force: true })
8649
+ throw error
8650
+ }
8651
+ }
8652
+ function readJsonFile(root, name, maxBytes) {
8653
+ const read = readContainedFile(root, name, { maxBytes })
8654
+ if (read.kind === 'refused') {
8655
+ return read.reason === 'missing'
8656
+ ? { status: 'missing' }
8657
+ : { status: 'unreadable' }
8658
+ }
8659
+ try {
8660
+ return { status: 'ok', value: JSON.parse(read.bytes.toString('utf8')) }
8661
+ } catch {
8662
+ return { status: 'unreadable' }
8663
+ }
8664
+ }
8665
+ var ZERO_WIDTH = '​'
8666
+ function neutraliseText(text) {
8667
+ return sanitizeText(text)
8668
+ .replaceAll('@', `@${ZERO_WIDTH}`)
8669
+ .replace(/#(?=\d)/g, `#${ZERO_WIDTH}`)
8670
+ .replace(/(gh-)(?=\d)/gi, `$1${ZERO_WIDTH}`)
8671
+ .replaceAll('<!--', `<!${ZERO_WIDTH}--`)
8672
+ .replaceAll('-->', `--${ZERO_WIDTH}>`)
8673
+ }
8674
+ function longestBacktickRun(text) {
8675
+ let longest = 0
8676
+ let current = 0
8677
+ for (const char of text) {
8678
+ current = char === '`' ? current + 1 : 0
8679
+ if (current > longest) longest = current
8680
+ }
8681
+ return longest
8682
+ }
8683
+ function fenceText(text) {
8684
+ const clean = neutraliseText(text)
8685
+ const fence = '`'.repeat(Math.max(3, longestBacktickRun(clean) + 1))
8686
+ return `${fence}
8687
+ ${clean}
8688
+ ${fence}`
8689
+ }
8690
+ function inFence(scan, line) {
8691
+ const match = FENCE_LINE2.exec(line)
8692
+ const run = match?.[1]
8693
+ const info = match?.[2] ?? ''
8694
+ const char = run?.startsWith('~') ? '~' : '`'
8695
+ if (scan.fence !== undefined) {
8696
+ if (
8697
+ run !== undefined &&
8698
+ char === scan.fence.char &&
8699
+ run.length >= scan.fence.length &&
8700
+ info.trim() === ''
8701
+ ) {
8702
+ scan.fence = undefined
8703
+ }
8704
+ return true
8705
+ }
8706
+ if (run !== undefined && !(char === '`' && info.includes('`'))) {
8707
+ scan.fence = { char, length: run.length }
8708
+ return true
8709
+ }
8710
+ return false
8711
+ }
8712
+ function boxOf(line) {
8713
+ const match = DECISION_LINE.exec(line)
8714
+ if (match === null) return
8715
+ const [, mark, label, status, id] = match
8716
+ if (id === undefined || label === undefined || mark === undefined) {
8717
+ return
8718
+ }
8719
+ if (status !== 'promoted' && status !== 'rejected') return
8720
+ if (STATUS_LABEL[status] !== label) return
8721
+ return { id, status, ticked: mark !== ' ' }
8722
+ }
8723
+ function scanLine(scan, line) {
8724
+ if (inFence(scan, line)) return
8725
+ const text = line.trimEnd()
8726
+ if (text === DECISIONS_BEGIN) {
8727
+ scan.begins++
8728
+ if (scan.ends > 0) scan.ordered = false
8729
+ scan.inside = true
8730
+ return
8731
+ }
8732
+ if (text === DECISIONS_END) {
8733
+ scan.ends++
8734
+ if (scan.begins === 0) scan.ordered = false
8735
+ scan.inside = false
8736
+ return
8737
+ }
8738
+ const manifest = MANIFEST_LINE.exec(line)?.[1]
8739
+ if (manifest !== undefined) scan.manifests.push(manifest)
8740
+ if (!scan.inside) return
8741
+ const box = boxOf(line)
8742
+ if (box !== undefined) scan.boxes.push(box)
8743
+ }
8744
+ function parseReportBody(body) {
8745
+ const scan = {
8746
+ fence: undefined,
8747
+ inside: false,
8748
+ begins: 0,
8749
+ ends: 0,
8750
+ ordered: true,
8751
+ manifests: [],
8752
+ boxes: [],
8753
+ }
8754
+ for (const line of body.split(/\r?\n/)) scanLine(scan, line)
8755
+ const manifest = scan.manifests.length === 1 ? scan.manifests[0] : undefined
8756
+ if (scan.begins === 0 && scan.ends === 0) {
8757
+ return { manifest, section: 'missing', boxes: [] }
8758
+ }
8759
+ const whole = scan.begins === 1 && scan.ends === 1 && scan.ordered
8760
+ return whole
8761
+ ? { manifest, section: 'ok', boxes: scan.boxes }
8762
+ : { manifest, section: 'malformed', boxes: [] }
8763
+ }
8764
+ var MAX_BODY_BYTES = 512 * 1024
8765
+ function readReportBody(file) {
8766
+ let directory
8767
+ try {
8768
+ directory = fs7.realpathSync.native(path6.dirname(file))
8769
+ } catch {
8770
+ return { status: 'failed', reason: 'body-unreadable' }
8771
+ }
8772
+ const read = readContainedFile(directory, path6.basename(file), {
8773
+ maxBytes: MAX_BODY_BYTES,
8774
+ })
8775
+ if (read.kind === 'bytes') {
8776
+ return { status: 'ok', body: read.bytes.toString('utf8') }
8777
+ }
8778
+ switch (read.reason) {
8779
+ case 'missing':
8780
+ return { status: 'failed', reason: 'body-missing' }
8781
+ case 'symlink':
8782
+ return { status: 'failed', reason: 'body-symlink' }
8783
+ case 'not-regular':
8784
+ return { status: 'failed', reason: 'body-not-regular' }
8785
+ case 'too-large':
8786
+ return { status: 'failed', reason: 'body-too-large' }
8787
+ default:
8788
+ return { status: 'failed', reason: 'body-unreadable' }
8789
+ }
8790
+ }
8791
+ var LIMITS = Object.freeze({
8792
+ title: 160,
8793
+ summary: 800,
8794
+ name: 100,
8795
+ path: 300,
8796
+ excerpt: 240,
8797
+ sources: 8,
8798
+ reason: 200,
8799
+ reasons: 20,
8800
+ })
8801
+ var MAX_PAYLOAD_BYTES = 8 * 1024 * 1024
8802
+ var MAX_MANIFEST_BYTES = 1024 * 1024
8803
+ var MAX_MANIFEST_IDS = 1000
8804
+ var MAX_PROJECTS = 64
8805
+ var MAX_CANDIDATES = 200
8806
+ function sourceOf(item) {
8807
+ if (!isRecord4(item)) return
8808
+ const sourcePath = lineOf(item.path, LIMITS.path)
8809
+ if (sourcePath === undefined) return
8810
+ const excerpt = lineOf(item.excerpt, LIMITS.excerpt)
8811
+ return {
8812
+ projectName: lineOf(item.projectName, LIMITS.name) ?? 'unknown',
8813
+ path: sourcePath,
8814
+ ...(excerpt === undefined ? {} : { excerpt }),
8815
+ }
8816
+ }
8817
+ function payloadText(raw) {
8818
+ if (!isRecord4(raw) || !Array.isArray(raw.sources)) return
8819
+ const title = lineOf(raw.title, LIMITS.title)
8820
+ const summary = paragraphOf(raw.summary, LIMITS.summary)
8821
+ if (title === undefined || summary === undefined) return
8822
+ const sources = raw.sources
8823
+ .slice(0, LIMITS.sources)
8824
+ .flatMap((item) => sourceOf(item) ?? [])
8825
+ return {
8826
+ title,
8827
+ summary,
8828
+ kind: tokenOf(raw.kind),
8829
+ nextStep: tokenOf(raw.nextStep),
8830
+ targetName: lineOf(raw.targetName, LIMITS.name) ?? 'unknown',
8831
+ sources,
8832
+ }
8833
+ }
8834
+ function payloadFromStore(raw) {
8835
+ if (!isRecord4(raw) || !isCandidateId(raw.id)) return
8836
+ const text = payloadText(raw)
8837
+ if (text === undefined) return
8838
+ if (!isDigits(raw.runId) || !isDigits(raw.attempt)) return
8839
+ const { repo, firstShown } = raw
8840
+ return {
8841
+ id: raw.id,
8842
+ ...text,
8843
+ ...(typeof repo === 'string' && REPO.test(repo) ? { repo } : {}),
8844
+ ...(typeof firstShown === 'string' && validDate(firstShown)
8845
+ ? { firstShown }
8846
+ : {}),
8847
+ runId: raw.runId,
8848
+ attempt: raw.attempt,
8849
+ }
8850
+ }
8851
+ function readPayloadStore(root) {
8852
+ const out = new Map()
8853
+ const read = readJsonFile(root, PAYLOAD_FILE, MAX_PAYLOAD_BYTES)
8854
+ if (read.status === 'missing') return { status: 'ok', items: out }
8855
+ const value = read.status === 'ok' ? read.value : undefined
8856
+ if (!isRecord4(value) || value.version !== 1) return { status: 'unreadable' }
8857
+ if (!Array.isArray(value.payloads)) return { status: 'unreadable' }
8858
+ for (const item of value.payloads) {
8859
+ const payload = payloadFromStore(item)
8860
+ if (payload !== undefined && !out.has(payload.id)) {
8861
+ out.set(payload.id, payload)
8862
+ }
8863
+ }
8864
+ return { status: 'ok', items: out }
8865
+ }
8866
+ function payloadStoreText(payloads) {
8867
+ const stored = payloads.map((payload) => ({
8868
+ id: payload.id,
8869
+ title: payload.title,
8870
+ summary: payload.summary,
8871
+ kind: payload.kind,
8872
+ nextStep: payload.nextStep,
8873
+ targetName: payload.targetName,
8874
+ ...(payload.repo === undefined ? {} : { repo: payload.repo }),
8875
+ sources: payload.sources,
8876
+ ...(payload.firstShown === undefined
8877
+ ? {}
8878
+ : { firstShown: payload.firstShown }),
8879
+ runId: payload.runId,
8880
+ attempt: payload.attempt,
8881
+ }))
8882
+ return `${JSON.stringify({ version: 1, payloads: stored }, null, 2)}
8883
+ `
8884
+ }
8885
+ function manifestHash(ids) {
8886
+ return createHash4('sha256')
8887
+ .update(JSON.stringify(['connect-manifest-v1', ids]))
8888
+ .digest('hex')
8889
+ .slice(0, MANIFEST_HASH_LENGTH)
8890
+ }
8891
+ function manifestFrom(raw) {
8892
+ if (!isRecord4(raw)) return
8893
+ if (Object.keys(raw).some((key) => key !== 'hash' && key !== 'ids')) {
8894
+ return
8895
+ }
8896
+ const { hash, ids } = raw
8897
+ if (typeof hash !== 'string' || !MANIFEST_HASH.test(hash)) return
8898
+ if (!Array.isArray(ids) || ids.length > MAX_MANIFEST_IDS) return
8899
+ if (!ids.every(isCandidateId)) return
8900
+ const list = ids
8901
+ return manifestHash(list) === hash ? { hash, ids: list } : undefined
8902
+ }
8903
+ function readManifestStore(root) {
8904
+ const read = readJsonFile(root, MANIFEST_FILE, MAX_MANIFEST_BYTES)
8905
+ if (read.status === 'missing') return { status: 'ok', items: [] }
8906
+ const value = read.status === 'ok' ? read.value : undefined
8907
+ if (!isRecord4(value) || value.version !== 1) return { status: 'unreadable' }
8908
+ if (!Array.isArray(value.manifests)) return { status: 'unreadable' }
8909
+ const items = value.manifests
8910
+ .slice(-MANIFESTS_KEPT)
8911
+ .flatMap((item) => manifestFrom(item) ?? [])
8912
+ return { status: 'ok', items }
8913
+ }
8914
+ function withManifest(stored, added) {
8915
+ return [...stored.filter((item) => item.hash !== added.hash), added].slice(
8916
+ -MANIFESTS_KEPT,
8917
+ )
8918
+ }
8919
+ function writeManifestStore(root, manifests) {
8920
+ writeFileAtomic2(
8921
+ path6.join(root, MANIFEST_FILE),
8922
+ `${JSON.stringify({ version: 1, manifests }, null, 2)}
8923
+ `,
8924
+ )
8925
+ }
8926
+ function selectionLabel(value) {
8927
+ if (!isRecord4(value)) return
8928
+ if (value.kind === 'all') return 'all allowed projects'
8929
+ if (value.kind === 'group') {
8930
+ const name = lineOf(value.name, LIMITS.name)
8931
+ return name === undefined ? undefined : `group ${name}`
8932
+ }
8933
+ if (value.kind !== 'projects' || !Array.isArray(value.picks)) {
8934
+ return
8935
+ }
8936
+ const picks = value.picks
8937
+ .slice(0, 32)
8938
+ .flatMap((pick) => lineOf(pick, LIMITS.name) ?? [])
8939
+ return picks.length === 0 ? undefined : `projects ${picks.join(', ')}`
8940
+ }
8941
+ function count(value) {
8942
+ return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0
8943
+ ? value
8944
+ : undefined
8945
+ }
8946
+ function sweepLabel(value) {
8947
+ if (!isRecord4(value)) return
8948
+ const total = count(value.total)
8949
+ const covered = count(value.covered)
8950
+ const coveredThisRun = count(value.coveredThisRun)
8951
+ const runsLeft = count(value.runsLeft)
8952
+ if (
8953
+ total === undefined ||
8954
+ covered === undefined ||
8955
+ coveredThisRun === undefined ||
8956
+ runsLeft === undefined
8957
+ ) {
8958
+ return
8959
+ }
8960
+ const line = renderSweepLine({ total, covered, coveredThisRun, runsLeft })
8961
+ return line === '' ? undefined : line
8962
+ }
8963
+ function repoMap(value) {
8964
+ const out = new Map()
8965
+ if (!Array.isArray(value)) return out
8966
+ for (const item of value.slice(0, MAX_PROJECTS)) {
8967
+ if (!isRecord4(item)) continue
8968
+ const { root, repo } = item
8969
+ if (
8970
+ typeof root === 'string' &&
8971
+ typeof repo === 'string' &&
8972
+ REPO.test(repo)
8973
+ ) {
8974
+ out.set(root, repo)
8975
+ }
8976
+ }
8977
+ return out
8978
+ }
8979
+ function candidatesOf(value) {
8980
+ if (!isRecord4(value) || !Array.isArray(value.candidates)) return []
8981
+ return value.candidates.slice(0, MAX_CANDIDATES)
8982
+ }
8983
+ function readRunSummary(root, runId) {
8984
+ const record = readRunRecord(root, runId)
8985
+ if (!isRecord4(record)) return
8986
+ const reasons = Array.isArray(record.degradeReasons)
8987
+ ? record.degradeReasons
8988
+ .slice(0, LIMITS.reasons)
8989
+ .flatMap((reason) => lineOf(reason, LIMITS.reason) ?? [])
8990
+ : []
8991
+ const selection = selectionLabel(record.selection)
8992
+ const sweep = sweepLabel(record.sweep)
8993
+ return {
8994
+ degradeReasons: reasons,
8995
+ ...(selection === undefined ? {} : { selection }),
8996
+ ...(sweep === undefined ? {} : { sweep }),
8997
+ repoByRoot: repoMap(record.projects),
8998
+ candidates: candidatesOf(record.candidateBlock),
8999
+ }
9000
+ }
9001
+ function outcomeOf(invocation) {
9002
+ return invocation.phase === 'begun' ? 'no-final-line' : invocation.outcome
9003
+ }
9004
+ var OUTCOME_SENTENCE = {
9005
+ complete: 'Connect complete.',
9006
+ degraded: 'Connect degraded.',
9007
+ busy: 'Connect busy: another run held the lock, so nothing was read.',
9008
+ failed: 'Connect failed.',
9009
+ 'no-final-line':
9010
+ 'The run ended with no final line: it began and never reported an outcome.',
9011
+ }
9012
+ function isRed(outcome) {
9013
+ return outcome === 'failed' || outcome === 'no-final-line'
9014
+ }
9015
+ function validRequest(request) {
9016
+ return (
9017
+ validDate(request.date) &&
9018
+ isDigits(request.runId) &&
9019
+ isDigits(request.attempt)
9020
+ )
9021
+ }
9022
+ function factsOf(id, payload) {
9023
+ const target =
9024
+ payload.repo === undefined
9025
+ ? payload.targetName
9026
+ : `${payload.targetName} (${payload.repo})`
9027
+ const lines = [
9028
+ `Id: ${id}`,
9029
+ `Kind: ${payload.kind}`,
9030
+ `Target: ${target}`,
9031
+ `Title: ${payload.title}`,
9032
+ `Summary: ${payload.summary}`,
9033
+ `Next step: ${payload.nextStep}`,
9034
+ ]
9035
+ if (payload.sources.length > 0) lines.push('Sources:')
9036
+ for (const source of payload.sources) {
9037
+ lines.push(`- ${source.projectName} ${source.path}`)
9038
+ if (source.excerpt !== undefined) lines.push(` Excerpt: ${source.excerpt}`)
9039
+ }
9040
+ return lines.join(`
9041
+ `)
9042
+ }
9043
+ function blockOf(position, id, payload, isNew) {
9044
+ const lines = [`### Candidate ${position}${isNew ? ' (new)' : ''}`, '']
9045
+ if (payload === undefined) {
9046
+ lines.push(`Details unavailable for \`${id}\`.`)
9047
+ } else {
9048
+ lines.push(
9049
+ `- Fetch: \`/ce:connect fetch:${payload.runId}\` (run ${payload.runId}, attempt ${payload.attempt})`,
9050
+ '',
9051
+ fenceText(factsOf(id, payload)),
9052
+ )
9053
+ }
9054
+ lines.push(
9055
+ '',
9056
+ decisionLine(id, 'promoted', false),
9057
+ decisionLine(id, 'rejected', false),
9058
+ '',
9059
+ )
9060
+ return lines.join(`
9061
+ `)
9062
+ }
9063
+ function ranges(positions) {
9064
+ const spans = []
9065
+ for (const position of positions) {
9066
+ const last = spans.at(-1)
9067
+ if (last !== undefined && position === last[1] + 1) last[1] = position
9068
+ else spans.push([position, position])
9069
+ }
9070
+ return spans
9071
+ .map(([from, to]) => (from === to ? `${from}` : `${from}-${to}`))
9072
+ .join(', ')
9073
+ }
9074
+ function fetchLines(rows) {
9075
+ const byRun = new Map()
9076
+ rows.forEach((row, index) => {
9077
+ if (row.payload === undefined) return
9078
+ const list = byRun.get(row.payload.runId) ?? []
9079
+ list.push(index + 1)
9080
+ byRun.set(row.payload.runId, list)
9081
+ })
9082
+ const lines = [...byRun].map(
9083
+ ([run, positions]) =>
9084
+ `- \`/ce:connect fetch:${run}\` for candidates ${ranges(positions)}`,
9085
+ )
9086
+ return { runs: [...byRun.keys()], lines }
9087
+ }
9088
+ var RESET_NOTICE =
9089
+ '> **Ledger reset.** No cache was restored for this run, so the ledger and the sweep started fresh. Earlier decisions are not remembered, and a tick for a candidate the ledger no longer knows is reported as unresolved.'
9090
+ var STATE_NOT_UPDATED_NOTICE =
9091
+ '> **State not updated.** Another run held the lock, so this report was rendered from the ledger as it stands and recorded nothing. A tick on it is read only when an earlier report listed the same candidates.'
9092
+ var HOW_TO_TICK =
9093
+ 'Tick **Promoted** after you have promoted a candidate, or **Rejected** to dismiss it, one box per candidate. Ticking both boxes for one candidate is ignored. The next run reads the ticks, and a settled candidate leaves this list.'
9094
+ function headOf(input) {
9095
+ const lines = ['## ce:connect report', '']
9096
+ lines.push(`**Outcome:** ${OUTCOME_SENTENCE[input.outcome]}`, '')
9097
+ if (input.facts.length > 0)
9098
+ lines.push(
9099
+ fenceText(
9100
+ input.facts.join(`
9101
+ `),
9102
+ ),
9103
+ '',
9104
+ )
9105
+ if (!input.cacheRestored) lines.push(RESET_NOTICE, '')
9106
+ if (!input.stateUpdated) lines.push(STATE_NOT_UPDATED_NOTICE, '')
9107
+ return lines
9108
+ }
9109
+ function hiddenLine(hidden) {
9110
+ const noun = hidden === 1 ? 'candidate is' : 'candidates are'
9111
+ return `**${hidden} open ${noun} not shown.** They stay open and appear here, oldest first, as the ones above are settled.`
9112
+ }
9113
+ function assemble(input, shown) {
9114
+ const rows = input.rows.slice(0, shown)
9115
+ const ids = rows.map((row) => row.id)
9116
+ const hash = manifestHash(ids)
9117
+ const fetch = fetchLines(rows)
9118
+ const fresh = rows.filter((row) => row.isNew).length
9119
+ const open = input.rows.length
9120
+ const out = [manifestMarker(hash), '', ...headOf(input)]
9121
+ if (fetch.lines.length > 0) {
9122
+ out.push(
9123
+ '## Fetch',
9124
+ '',
9125
+ 'Run one of these in a local checkout to bring the evidence in before you promote:',
9126
+ ...fetch.lines,
9127
+ '',
9128
+ )
9129
+ }
9130
+ out.push('## Open candidates', '')
9131
+ out.push(
9132
+ `${open} open, ${shown} listed${fresh > 0 ? `, ${fresh} new` : ''}.`,
9133
+ '',
9134
+ HOW_TO_TICK,
9135
+ '',
9136
+ DECISIONS_BEGIN,
9137
+ '',
9138
+ )
9139
+ for (const row of rows) out.push(row.block)
9140
+ out.push(DECISIONS_END, '')
9141
+ if (shown < open) out.push(hiddenLine(open - shown), '')
9142
+ return {
9143
+ body: out.join(`
9144
+ `),
9145
+ ids,
9146
+ hash,
9147
+ runs: fetch.runs,
9148
+ fresh,
9149
+ }
9150
+ }
9151
+ function fit(input) {
9152
+ const total = input.rows.length
9153
+ if (assemble(input, total).body.length <= REPORT_MAX_CHARS) return total
9154
+ let low = 0
9155
+ let high = total - 1
9156
+ while (low < high) {
9157
+ const middle = Math.ceil((low + high) / 2)
9158
+ if (assemble(input, middle).body.length <= REPORT_MAX_CHARS) low = middle
9159
+ else high = middle - 1
9160
+ }
9161
+ return low
9162
+ }
9163
+ function openOldestFirst(entries) {
9164
+ return entries
9165
+ .filter((entry) => entry.status === 'candidate')
9166
+ .sort((a, b) => {
9167
+ if (a.recordedAt !== b.recordedAt)
9168
+ return a.recordedAt < b.recordedAt ? -1 : 1
9169
+ return a.id < b.id ? -1 : 1
9170
+ })
9171
+ }
9172
+ function proposed(run, open, request) {
9173
+ const out = new Map()
9174
+ for (const raw of run?.candidates ?? []) {
9175
+ if (!isRecord4(raw) || !isCandidateId(raw.id) || !open.has(raw.id)) continue
9176
+ const text = payloadText(raw)
9177
+ if (text === undefined) continue
9178
+ const target = typeof raw.target === 'string' ? raw.target : undefined
9179
+ const repo = target === undefined ? undefined : run?.repoByRoot.get(target)
9180
+ out.set(raw.id, {
9181
+ id: raw.id,
9182
+ ...text,
9183
+ ...(repo === undefined ? {} : { repo }),
9184
+ runId: request.runId,
9185
+ attempt: request.attempt,
9186
+ })
9187
+ }
9188
+ return out
9189
+ }
9190
+ function mergedPayloads(open, stored, fresh) {
9191
+ const out = new Map()
9192
+ for (const entry of open) {
9193
+ const earlier = stored.get(entry.id)
9194
+ const current = fresh.get(entry.id)
9195
+ const merged =
9196
+ current === undefined
9197
+ ? earlier
9198
+ : {
9199
+ ...current,
9200
+ ...(earlier?.firstShown === undefined
9201
+ ? {}
9202
+ : { firstShown: earlier.firstShown }),
9203
+ }
9204
+ if (merged !== undefined) out.set(entry.id, merged)
9205
+ }
9206
+ return out
9207
+ }
9208
+ function factLines(invocation, run) {
9209
+ const lines = []
9210
+ if (run?.selection !== undefined) lines.push(`Selection: ${run.selection}`)
9211
+ if (run?.sweep !== undefined) lines.push(run.sweep)
9212
+ const reason = invocation.phase === 'terminal' ? invocation.reason : undefined
9213
+ if (reason !== undefined) lines.push(`Reason: ${reason}`)
9214
+ if (run !== undefined && run.degradeReasons.length > 0) {
9215
+ lines.push('Degrade reasons:', ...run.degradeReasons.map((r) => `- ${r}`))
9216
+ }
9217
+ return lines
9218
+ }
9219
+ function rowsOf(open, payloads, date) {
9220
+ return open.map((entry, index) => {
9221
+ const payload = payloads.get(entry.id)
9222
+ const isNew = payload !== undefined && (payload.firstShown ?? date) === date
9223
+ return {
9224
+ id: entry.id,
9225
+ payload,
9226
+ isNew,
9227
+ block: blockOf(index + 1, entry.id, payload, isNew),
9228
+ }
9229
+ })
9230
+ }
9231
+ function loadStores(root, recordState) {
9232
+ const payloads = readPayloadStore(root)
9233
+ if (payloads.status !== 'ok' && recordState) {
9234
+ return { status: 'failed', reason: 'payload-store-unreadable' }
9235
+ }
9236
+ const manifests = recordState ? readManifestStore(root) : undefined
9237
+ if (manifests !== undefined && manifests.status !== 'ok') {
9238
+ return { status: 'failed', reason: 'manifest-store-unreadable' }
9239
+ }
9240
+ return {
9241
+ status: 'ok',
9242
+ stores: {
9243
+ payloads: payloads.status === 'ok' ? payloads.items : new Map(),
9244
+ manifests: manifests?.status === 'ok' ? manifests.items : undefined,
9245
+ },
9246
+ }
9247
+ }
9248
+ function renderState(root, request, recordState) {
9249
+ const read = readLedger(root)
9250
+ if (read.status !== 'ok')
9251
+ return { status: 'failed', reason: 'ledger-unreadable' }
9252
+ const invocation = readInvocationRecord(root)
9253
+ if (invocation?.marker !== `${request.runId}-${request.attempt}`) {
9254
+ return { status: 'failed', reason: 'no-invocation' }
9255
+ }
9256
+ const run =
9257
+ invocation.runId === undefined
9258
+ ? undefined
9259
+ : readRunSummary(root, invocation.runId)
9260
+ const loaded = loadStores(root, recordState)
9261
+ if (loaded.status === 'failed') return loaded
9262
+ const open = openOldestFirst(read.ledger.entries)
9263
+ const payloads = mergedPayloads(
9264
+ open,
9265
+ loaded.stores.payloads,
9266
+ proposed(run, new Set(open.map((entry) => entry.id)), request),
9267
+ )
9268
+ const outcome = outcomeOf(invocation)
9269
+ const input = {
9270
+ outcome,
9271
+ facts: factLines(invocation, run),
9272
+ cacheRestored: request.cacheRestored,
9273
+ stateUpdated: recordState,
9274
+ rows: rowsOf(open, payloads, request.date),
9275
+ }
9276
+ const shown = fit(input)
9277
+ const result = assemble(input, shown)
9278
+ for (const id of result.ids) {
9279
+ const payload = payloads.get(id)
9280
+ if (payload !== undefined && payload.firstShown === undefined) {
9281
+ payloads.set(id, { ...payload, firstShown: request.date })
9282
+ }
9283
+ }
9284
+ const storeText = payloadStoreText([...payloads.values()])
9285
+ if (recordState && Buffer.byteLength(storeText) > MAX_PAYLOAD_BYTES) {
9286
+ return { status: 'failed', reason: 'payload-store-too-large' }
9287
+ }
9288
+ const bodyFile = path6.join(root, REPORT_FILE)
9289
+ try {
9290
+ if (loaded.stores.manifests !== undefined) {
9291
+ writeFileAtomic2(path6.join(root, PAYLOAD_FILE), storeText)
9292
+ writeManifestStore(
9293
+ root,
9294
+ withManifest(loaded.stores.manifests, {
9295
+ hash: result.hash,
9296
+ ids: result.ids,
9297
+ }),
9298
+ )
9299
+ }
9300
+ writeFileAtomic2(bodyFile, result.body)
9301
+ } catch {
9302
+ return { status: 'failed', reason: 'write-failed' }
9303
+ }
9304
+ return {
9305
+ status: 'rendered',
9306
+ body: result.body,
9307
+ bodyFile,
9308
+ manifest: result.hash,
9309
+ open: open.length,
9310
+ shown,
9311
+ hidden: open.length - shown,
9312
+ fresh: result.fresh,
9313
+ runs: result.runs,
9314
+ outcome,
9315
+ red: isRed(outcome),
9316
+ stateUpdated: recordState,
9317
+ }
9318
+ }
9319
+ function renderBusyRun(root, request) {
9320
+ const invocation = readInvocationRecord(root)
9321
+ if (
9322
+ invocation?.marker !== `${request.runId}-${request.attempt}` ||
9323
+ outcomeOf(invocation) !== 'busy'
9324
+ ) {
9325
+ return
9326
+ }
9327
+ try {
9328
+ return renderState(root, request, false)
9329
+ } catch {
9330
+ return { status: 'failed', reason: 'write-failed' }
9331
+ }
9332
+ }
9333
+ function renderReport(root, request, options = {}) {
9334
+ if (!validRequest(request))
9335
+ return { status: 'failed', reason: 'invalid-request' }
9336
+ const acquired = acquireLock(root, lockOptions(options))
9337
+ if (acquired.status === 'busy') {
9338
+ return (
9339
+ renderBusyRun(root, request) ?? {
9340
+ status: 'busy',
9341
+ reason: acquired.reason,
9342
+ }
9343
+ )
9344
+ }
9345
+ if (acquired.status === 'failed') {
9346
+ return { status: 'failed', reason: 'lock-failed' }
9347
+ }
9348
+ try {
9349
+ return renderState(root, request, true)
9350
+ } catch {
9351
+ return { status: 'failed', reason: 'write-failed' }
9352
+ } finally {
9353
+ releaseLock(acquired.lock)
9354
+ }
9355
+ }
9356
+ function tickedBoxes(boxes) {
9357
+ const seen = new Set()
9358
+ const out = []
9359
+ for (const box of boxes) {
9360
+ const key = `${box.id}:${box.status}`
9361
+ if (!box.ticked || seen.has(key)) continue
9362
+ seen.add(key)
9363
+ out.push({ id: box.id, status: box.status })
9364
+ }
9365
+ return out
9366
+ }
9367
+ function emptyApplied(section) {
9368
+ return {
9369
+ status: 'applied',
9370
+ section,
9371
+ accepted: [],
9372
+ skipped: [],
9373
+ unresolved: [],
9374
+ }
9375
+ }
9376
+ function groupById(ticks) {
9377
+ const byId = new Map()
9378
+ for (const tick of ticks) {
9379
+ byId.set(tick.id, [...(byId.get(tick.id) ?? []), tick.status])
9380
+ }
9381
+ return byId
9382
+ }
9383
+ function judge(id, statuses, manifest, entry, reason) {
9384
+ const [status] = statuses
9385
+ if (status === undefined) return
9386
+ const skip = (why) => ({
9387
+ kind: 'skip',
9388
+ skipped: { id, status, reason: why },
9389
+ })
9390
+ if (statuses.length > 1) {
9391
+ return { kind: 'skip', skipped: { id, reason: 'conflict' } }
9392
+ }
9393
+ if (!manifest.ids.includes(id)) return skip('not-in-manifest')
9394
+ if (entry === undefined) {
9395
+ return {
9396
+ kind: 'unresolved',
9397
+ unresolved: { id, status, reason: 'unknown-candidate' },
9398
+ }
9399
+ }
9400
+ if (entry.status === status && entry.reason === reason) {
9401
+ return skip('already-recorded')
9402
+ }
9403
+ if (entry.status !== 'candidate') return skip('not-open')
9404
+ return { kind: 'record', tick: { id, status } }
9405
+ }
9406
+ function triage(ticks, manifest, entries, reason) {
9407
+ const out = { record: [], skipped: [], unresolved: [] }
9408
+ if (manifest === undefined) {
9409
+ for (const tick of ticks) {
9410
+ out.unresolved.push({ ...tick, reason: 'manifest-missing' })
9411
+ }
9412
+ return out
9413
+ }
9414
+ for (const [id, statuses] of groupById(ticks)) {
9415
+ const entry = entries.find((item) => item.id === id)
9416
+ const verdict = judge(id, statuses, manifest, entry, reason)
9417
+ if (verdict?.kind === 'record') out.record.push(verdict.tick)
9418
+ else if (verdict?.kind === 'skip') out.skipped.push(verdict.skipped)
9419
+ else if (verdict?.kind === 'unresolved') {
9420
+ out.unresolved.push(verdict.unresolved)
9421
+ }
9422
+ }
9423
+ return out
9424
+ }
9425
+ function failureOf(reason) {
9426
+ return reason === 'ledger-unreadable' || reason === 'lock-failed'
9427
+ ? reason
9428
+ : 'write-failed'
9429
+ }
9430
+ function applyTicks(root, request, options = {}) {
9431
+ if (!Number.isSafeInteger(request.issue) || request.issue < 1) {
9432
+ return { status: 'failed', reason: 'invalid-request', accepted: [] }
9433
+ }
9434
+ const parsed = parseReportBody(request.body)
9435
+ const ticks = tickedBoxes(parsed.boxes)
9436
+ if (parsed.section !== 'ok' || ticks.length === 0) {
9437
+ return emptyApplied(parsed.section)
9438
+ }
9439
+ const stored = readManifestStore(root)
9440
+ const manifest =
9441
+ stored.status === 'ok'
9442
+ ? stored.items.find((item) => item.hash === parsed.manifest)
9443
+ : undefined
9444
+ const read = readLedger(root)
9445
+ if (read.status !== 'ok') {
9446
+ return { status: 'failed', reason: 'ledger-unreadable', accepted: [] }
9447
+ }
9448
+ const reason = `settled in report #${request.issue}`
9449
+ const sorted = triage(ticks, manifest, read.ledger.entries, reason)
9450
+ options.hooks?.afterTriage?.()
9451
+ const accepted = []
9452
+ const skipped = [...sorted.skipped]
9453
+ for (const tick of sorted.record) {
9454
+ const decided = decideLedgerEntry(
9455
+ root,
9456
+ { id: tick.id, status: tick.status, reason, onlyIfOpen: true },
9457
+ lockOptions(options),
9458
+ )
9459
+ if (decided.status === 'busy') {
9460
+ return { status: 'busy', reason: decided.reason, accepted }
9461
+ }
9462
+ if (decided.status === 'failed' && decided.reason === 'not-open') {
9463
+ skipped.push({ id: tick.id, status: tick.status, reason: 'not-open' })
9464
+ continue
9465
+ }
9466
+ if (decided.status === 'failed') {
9467
+ return { status: 'failed', reason: failureOf(decided.reason), accepted }
9468
+ }
9469
+ accepted.push(tick)
9470
+ }
9471
+ return {
9472
+ status: 'applied',
9473
+ section: parsed.section,
9474
+ accepted,
9475
+ skipped,
9476
+ unresolved: sorted.unresolved,
9477
+ }
9478
+ }
9479
+
9480
+ // src/lib/connect-import.ts
9481
+ import fs8 from 'node:fs'
9482
+ import path7 from 'node:path'
9483
+ var IMPORT_RECORD_FILE = 'run.json'
9484
+ var MAX_IMPORT_RECORD_BYTES = 2 * 1024 * 1024
9485
+ var MAX_IMPORT_PROJECTS = 64
9486
+ var MAX_IMPORT_CANDIDATES = 200
9487
+ var MAX_ROOT_LENGTH = 4096
9488
+ var RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
9489
+ var PROJECT_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/
9490
+ var REPO_SLUG2 = /^[a-z0-9][a-z0-9-]{0,38}\/[a-z0-9._-]{1,100}$/
9491
+ var OBJECT_ID2 = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/
9492
+ var CANDIDATE_ID2 = /^[0-9a-f]{64}$/
9493
+ var CONTROL2 = /[\p{Cc}\p{Cf}]/u
9494
+ var CORPUS_PATH = /^docs\/(?:solutions|plans|brainstorms)\//
9495
+ function isRecord5(value) {
9496
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
9497
+ }
9498
+ function plainString(value, max) {
9499
+ return (
9500
+ typeof value === 'string' &&
9501
+ value !== '' &&
9502
+ value.length <= max &&
9503
+ !CONTROL2.test(value)
9504
+ )
9505
+ }
9506
+ function parseProject(value) {
9507
+ if (!isRecord5(value)) return
9508
+ const { root, key, name, repo, commit } = value
9509
+ if (!plainString(root, MAX_ROOT_LENGTH) || !path7.posix.isAbsolute(root)) {
9510
+ return
9511
+ }
9512
+ if (key !== projectKey(root)) return
9513
+ if (typeof name !== 'string' || !PROJECT_NAME.test(name)) return
9514
+ if (
9515
+ repo !== undefined &&
9516
+ !(typeof repo === 'string' && REPO_SLUG2.test(repo))
9517
+ ) {
9518
+ return
9519
+ }
9520
+ if (
9521
+ commit !== undefined &&
9522
+ !(typeof commit === 'string' && OBJECT_ID2.test(commit))
9523
+ ) {
9524
+ return
9525
+ }
9526
+ return {
9527
+ root,
9528
+ key,
9529
+ name,
9530
+ ...(repo === undefined ? {} : { repo }),
9531
+ ...(commit === undefined ? {} : { commit }),
9532
+ }
9533
+ }
9534
+ function parseImportRecord(value) {
9535
+ const malformed = { status: 'failed', reason: 'record-malformed' }
9536
+ if (!isRecord5(value)) return malformed
9537
+ if (value.schemaVersion !== 1) {
9538
+ return { status: 'failed', reason: 'record-schema-version' }
9539
+ }
9540
+ const { runId, projects, candidateBlock } = value
9541
+ if (typeof runId !== 'string' || !RUN_ID.test(runId)) return malformed
9542
+ if (
9543
+ !Array.isArray(projects) ||
9544
+ projects.length < 1 ||
9545
+ projects.length > MAX_IMPORT_PROJECTS
9546
+ ) {
9547
+ return malformed
9548
+ }
9549
+ const parsed = []
9550
+ for (const item of projects) {
9551
+ const project = parseProject(item)
9552
+ if (project === undefined) return malformed
9553
+ if (parsed.some((known) => known.root === project.root)) return malformed
9554
+ parsed.push(project)
9555
+ }
9556
+ if (!isRecord5(candidateBlock)) return malformed
9557
+ if (candidateBlock.schemaVersion !== 1) {
9558
+ return { status: 'failed', reason: 'record-schema-version' }
9559
+ }
9560
+ const { candidates } = candidateBlock
9561
+ if (!Array.isArray(candidates) || candidates.length > MAX_IMPORT_CANDIDATES) {
9562
+ return malformed
9563
+ }
9564
+ return {
9565
+ status: 'ok',
9566
+ record: { runId, projects: parsed, candidates },
9567
+ }
9568
+ }
9569
+ function readImportRecord(directory) {
9570
+ let real
9571
+ try {
9572
+ real = fs8.realpathSync.native(directory)
9573
+ if (!fs8.statSync(real).isDirectory()) {
9574
+ return { status: 'failed', reason: 'directory-unreadable' }
9575
+ }
9576
+ } catch {
9577
+ return { status: 'failed', reason: 'directory-unreadable' }
9578
+ }
9579
+ const read = readContainedFile(real, IMPORT_RECORD_FILE, {
9580
+ maxBytes: MAX_IMPORT_RECORD_BYTES,
9581
+ })
9582
+ if (read.kind === 'refused') {
9583
+ switch (read.reason) {
9584
+ case 'missing':
9585
+ return { status: 'failed', reason: 'record-missing' }
9586
+ case 'symlink':
9587
+ return { status: 'failed', reason: 'record-symlink' }
9588
+ case 'not-regular':
9589
+ return { status: 'failed', reason: 'record-not-regular' }
9590
+ case 'too-large':
9591
+ return { status: 'failed', reason: 'record-too-large' }
9592
+ default:
9593
+ return { status: 'failed', reason: 'record-unreadable' }
9594
+ }
9595
+ }
9596
+ let value
9597
+ try {
9598
+ value = JSON.parse(read.bytes.toString('utf8'))
9599
+ } catch {
9600
+ return { status: 'failed', reason: 'record-malformed' }
9601
+ }
9602
+ return parseImportRecord(value)
9603
+ }
9604
+ function mapProject(project, locals) {
9605
+ if (project.repo === undefined) {
9606
+ return { status: 'unmapped', reason: 'project-without-repo' }
9607
+ }
9608
+ const matches = locals.filter((local) => local.repo === project.repo)
9609
+ const [only] = matches
9610
+ if (only === undefined) {
9611
+ return { status: 'unmapped', reason: 'repo-not-allowed' }
9612
+ }
9613
+ if (matches.length > 1) {
9614
+ return {
9615
+ status: 'unmapped',
9616
+ reason: 'repo-ambiguous',
9617
+ keys: matches.map((local) => local.key).sort(),
9618
+ }
9619
+ }
9620
+ return { status: 'mapped', local: only }
9621
+ }
9622
+ var REFUSAL_TEXT = Object.freeze({
9623
+ 'candidate-malformed': 'the candidate is malformed',
9624
+ 'duplicate-candidate': 'the record lists this candidate more than once',
9625
+ 'candidate-id-mismatch':
9626
+ 'the recorded id does not match the candidate, so the record was changed',
9627
+ 'project-not-in-record':
9628
+ 'the candidate names a project the record does not list',
9629
+ 'project-without-repo':
9630
+ 'a project of the candidate has no GitHub remote in the record, so it cannot be mapped to a local checkout',
9631
+ 'repo-not-allowed':
9632
+ 'no allowed local checkout has the recorded repository as its origin',
9633
+ 'repo-ambiguous':
9634
+ 'more than one allowed local checkout has the recorded repository as its origin',
9635
+ 'project-unreadable': 'a mapped local checkout could not be read',
9636
+ 'path-rejected': 'a cited path is not a safe path for this kind of file',
9637
+ 'rule-id-mismatch':
9638
+ 'the recorded rule id does not belong to any source of the candidate',
9639
+ 'file-not-tracked': 'a cited file is not tracked in the local checkout',
9640
+ 'file-symlink': 'a cited file is a symlink in the local checkout',
9641
+ 'file-changed': 'a cited file differs from the file the run read',
9642
+ 'file-vanished': 'a cited file is gone from the local checkout',
9643
+ 'check-failed': 'the candidate failed a check against the local files',
9644
+ })
9645
+ function fileRefs(value, max) {
9646
+ if (value === undefined) return []
9647
+ if (!Array.isArray(value) || value.length > max) return
9648
+ const out = []
9649
+ for (const item of value) {
9650
+ if (!isRecord5(item)) return
9651
+ const { project, path: file, fingerprint, excerpt } = item
9652
+ if (
9653
+ typeof project !== 'string' ||
9654
+ typeof file !== 'string' ||
9655
+ typeof fingerprint !== 'string' ||
9656
+ typeof excerpt !== 'string'
9657
+ ) {
9658
+ return
9659
+ }
9660
+ out.push({ project, path: file, fingerprint, excerpt, rest: item })
9661
+ }
9662
+ return out
9663
+ }
9664
+ function evidenceRefs(value, max) {
9665
+ if (value === undefined) return []
9666
+ if (!Array.isArray(value) || value.length > max) return
9667
+ const out = []
9668
+ for (const item of value) {
9669
+ if (!isRecord5(item)) return
9670
+ if (typeof item.path !== 'string' || typeof item.fingerprint !== 'string') {
9671
+ return
9672
+ }
9673
+ out.push(item)
9674
+ }
9675
+ return out
9676
+ }
9677
+ function shapeOf(raw) {
9678
+ if (!isRecord5(raw)) return
9679
+ const { id, kind, ruleId, target } = raw
9680
+ if (typeof id !== 'string' || !CANDIDATE_ID2.test(id)) return
9681
+ if (
9682
+ typeof kind !== 'string' ||
9683
+ typeof ruleId !== 'string' ||
9684
+ typeof target !== 'string'
9685
+ ) {
9686
+ return
9687
+ }
9688
+ const sources = fileRefs(raw.sources, CANDIDATE_LIMITS.sources)
9689
+ const counterevidence = fileRefs(
9690
+ raw.counterevidence,
9691
+ CANDIDATE_LIMITS.counterevidence,
9692
+ )
9693
+ const targetEvidence = evidenceRefs(
9694
+ raw.targetEvidence,
9695
+ CANDIDATE_LIMITS.targetEvidence,
9696
+ )
9697
+ if (
9698
+ sources === undefined ||
9699
+ counterevidence === undefined ||
9700
+ targetEvidence === undefined
9701
+ ) {
9702
+ return
9703
+ }
9704
+ return {
9705
+ id,
9706
+ kind,
9707
+ ruleId,
9708
+ target,
9709
+ title: raw.title,
9710
+ summary: raw.summary,
9711
+ nextStep: raw.nextStep,
9712
+ overlapCheck: raw.overlapCheck,
9713
+ sources,
9714
+ counterevidence,
9715
+ targetEvidence,
9716
+ }
9717
+ }
9718
+ function recordedIdentity(shape) {
9719
+ return candidateIdentity({
9720
+ kind: shape.kind,
9721
+ target: shape.target,
9722
+ ruleId: shape.ruleId,
9723
+ sources: shape.sources.map((source) => ({
9724
+ role: `source@${projectKey(source.project)}`,
9725
+ path: source.path,
9726
+ fingerprint: source.fingerprint,
9727
+ })),
9728
+ targetEvidence: shape.targetEvidence.map((item) => ({
9729
+ path: String(item.path),
9730
+ fingerprint: String(item.fingerprint),
9731
+ })),
9732
+ })
9733
+ }
9734
+ function isRefusal(value) {
9735
+ return typeof value === 'object' && value !== null && 'refusal' in value
9736
+ }
9737
+ function refuse(reason, extra = {}) {
9738
+ return { refusal: { reason, ...extra } }
9739
+ }
9740
+ function pathRefusal(file, corpusOnly) {
9741
+ const problem = pathProblem(file)
9742
+ if (problem !== undefined) return refuse('path-rejected', { detail: problem })
9743
+ if (corpusOnly && !CORPUS_PATH.test(file)) {
9744
+ return refuse('path-rejected', { detail: 'outside-corpus' })
9745
+ }
9746
+ return
9747
+ }
9748
+ function createLandings(projects, locals, factsFor) {
9749
+ const byRecorded = new Map()
9750
+ for (const project of projects) {
9751
+ byRecorded.set(project.root, mapProject(project, locals))
9752
+ }
9753
+ const cache = new Map()
9754
+ return {
9755
+ projects,
9756
+ mappingOf: (root) => byRecorded.get(root),
9757
+ projectOf: (root) => projects.find((project) => project.root === root),
9758
+ factsOf(local) {
9759
+ if (!cache.has(local.root)) cache.set(local.root, factsFor(local.root))
9760
+ return cache.get(local.root)
9761
+ },
9762
+ }
9763
+ }
9764
+ function land(shape, landings) {
9765
+ const roots = new Set([
9766
+ shape.target,
9767
+ ...shape.sources.map((source) => source.project),
9768
+ ...shape.counterevidence.map((source) => source.project),
9769
+ ])
9770
+ const out = new Map()
9771
+ for (const root of roots) {
9772
+ const project = landings.projectOf(root)
9773
+ const mapping = landings.mappingOf(root)
9774
+ if (project === undefined || mapping === undefined) {
9775
+ return refuse('project-not-in-record')
9776
+ }
9777
+ if (mapping.status === 'unmapped') {
9778
+ return refuse(mapping.reason, {
9779
+ ...(mapping.keys === undefined ? {} : { keys: mapping.keys }),
9780
+ ...(project.repo === undefined ? {} : { repo: project.repo }),
9781
+ })
9782
+ }
9783
+ const facts = landings.factsOf(mapping.local)
9784
+ if (facts === undefined) return refuse('project-unreadable')
9785
+ out.set(root, { local: mapping.local, facts })
9786
+ }
9787
+ return out
9788
+ }
9789
+ function checkPaths(shape) {
9790
+ const groups = [
9791
+ [shape.sources, true],
9792
+ [shape.counterevidence, false],
9793
+ [shape.targetEvidence.map((item) => ({ path: String(item.path) })), false],
9794
+ ]
9795
+ for (const [items, corpusOnly] of groups) {
9796
+ for (const item of items) {
9797
+ const refusal = pathRefusal(item.path, corpusOnly)
9798
+ if (refusal !== undefined) return refusal
9799
+ }
9800
+ }
9801
+ return
9802
+ }
9803
+ function localRuleId(shape, landed) {
9804
+ const bound = shape.sources.find(
9805
+ (source) => ruleIdFor(source.project, source.path) === shape.ruleId,
9806
+ )
9807
+ const landing = bound === undefined ? undefined : landed.get(bound.project)
9808
+ if (bound === undefined || landing === undefined) {
9809
+ return refuse('rule-id-mismatch')
9810
+ }
9811
+ return ruleIdFor(landing.local.root, bound.path)
9812
+ }
9813
+ function localFile(landing, rel, fingerprint) {
9814
+ const file = `${landing.local.name}:${rel}`
9815
+ const known = landing.facts.files.get(rel)
9816
+ if (known === undefined) {
9817
+ const probe = readContainedFile(landing.local.root, rel, { maxBytes: 1 })
9818
+ const symlink = probe.kind === 'refused' && probe.reason === 'symlink'
9819
+ return refuse(symlink ? 'file-symlink' : 'file-not-tracked', { file })
9820
+ }
9821
+ return known.sha256 === fingerprint
9822
+ ? undefined
9823
+ : refuse('file-changed', { file })
9824
+ }
9825
+ function checkFiles(shape, landed) {
9826
+ const refs = [
9827
+ ...shape.sources.map((s) => [s.project, s.path, s.fingerprint]),
9828
+ ...shape.counterevidence.map((s) => [s.project, s.path, s.fingerprint]),
9829
+ ...shape.targetEvidence.map((e) => [
9830
+ shape.target,
9831
+ String(e.path),
9832
+ String(e.fingerprint),
9833
+ ]),
9834
+ ]
9835
+ for (const [root, rel, fingerprint] of refs) {
9836
+ const landing = landed.get(root)
9837
+ if (landing === undefined) return refuse('project-not-in-record')
9838
+ const refusal = localFile(landing, rel, fingerprint)
9839
+ if (refusal !== undefined) return refusal
9840
+ }
9841
+ return
9842
+ }
9843
+ function localRaw(shape, ruleId, landed) {
9844
+ const rootOf = (recorded) => landed.get(recorded)?.local.root ?? recorded
9845
+ const source = (ref) => ({
9846
+ project: rootOf(ref.project),
9847
+ path: ref.path,
9848
+ fingerprint: ref.fingerprint,
9849
+ excerpt: ref.excerpt,
9850
+ })
9851
+ return {
9852
+ kind: shape.kind,
9853
+ ruleId,
9854
+ target: rootOf(shape.target),
9855
+ title: shape.title,
9856
+ summary: shape.summary,
9857
+ nextStep: shape.nextStep,
9858
+ overlapCheck: shape.overlapCheck,
9859
+ sources: shape.sources.map(source),
9860
+ targetEvidence: shape.targetEvidence.map((item) => ({
9861
+ path: item.path,
9862
+ start: item.start,
9863
+ end: item.end,
9864
+ symbol: item.symbol,
9865
+ fingerprint: item.fingerprint,
9866
+ excerpt: item.excerpt,
9867
+ })),
9868
+ counterevidence: shape.counterevidence.map(source),
9869
+ }
9870
+ }
9871
+ function contextOf(landed) {
9872
+ const projects = new Map()
9873
+ const ruleIds = new Set()
9874
+ for (const { local, facts } of landed.values()) {
9875
+ projects.set(local.root, facts)
9876
+ for (const [rel, info] of facts.files) {
9877
+ if (info.role === 'learning') ruleIds.add(ruleIdFor(local.root, rel))
9878
+ }
9879
+ }
9880
+ return { projects, ruleIds }
9881
+ }
9882
+ function verifyOne(raw, landings, seen) {
9883
+ const shape = shapeOf(raw)
9884
+ if (shape === undefined) return refuse('candidate-malformed')
9885
+ if (seen.has(shape.id)) return refuse('duplicate-candidate')
9886
+ seen.add(shape.id)
9887
+ if (recordedIdentity(shape) !== shape.id) {
9888
+ return refuse('candidate-id-mismatch')
9889
+ }
9890
+ const landed = land(shape, landings)
9891
+ if (isRefusal(landed)) return landed
9892
+ const pathIssue = checkPaths(shape)
9893
+ if (pathIssue !== undefined) return pathIssue
9894
+ const ruleId = localRuleId(shape, landed)
9895
+ if (isRefusal(ruleId)) return ruleId
9896
+ const fileIssue = checkFiles(shape, landed)
9897
+ if (fileIssue !== undefined) return fileIssue
9898
+ const checked = checkCandidate(
9899
+ localRaw(shape, ruleId, landed),
9900
+ contextOf(landed),
9901
+ )
9902
+ if (checked.status === 'dropped') {
9903
+ return refuse('check-failed', { detail: checked.reason })
9904
+ }
9905
+ if (checked.status === 'unresolved') {
9906
+ const [missing] = checked.missing
9907
+ return refuse('file-vanished', {
9908
+ ...(missing === undefined ? {} : { file: missing }),
9909
+ })
9910
+ }
9911
+ const record = toCandidateRecord(checked.candidate)
9912
+ return {
9913
+ recordedId: shape.id,
9914
+ candidate: checked.candidate,
9915
+ record,
9916
+ id: candidateIdentity(record),
9917
+ }
9918
+ }
9919
+ function projectReports(landings) {
9920
+ return landings.projects.map((project) => {
9921
+ const mapping = landings.mappingOf(project.root)
9922
+ const base = {
9923
+ name: project.name,
9924
+ ...(project.repo === undefined ? {} : { repo: project.repo }),
9925
+ ...(project.commit === undefined ? {} : { ciCommit: project.commit }),
9926
+ }
9927
+ if (mapping === undefined || mapping.status === 'unmapped') {
9928
+ return { ...base, status: mapping?.reason ?? 'repo-not-allowed' }
9929
+ }
9930
+ const { local } = mapping
9931
+ return {
9932
+ ...base,
9933
+ status: 'mapped',
9934
+ localRoot: local.root,
9935
+ localKey: local.key,
9936
+ localName: local.name,
9937
+ ...(local.commit === undefined ? {} : { localCommit: local.commit }),
9938
+ }
9939
+ })
9940
+ }
9941
+ function named(raw, refusal) {
9942
+ const id =
9943
+ isRecord5(raw) && typeof raw.id === 'string' && CANDIDATE_ID2.test(raw.id)
9944
+ ? raw.id
9945
+ : null
9946
+ return {
9947
+ ...refusal,
9948
+ id,
9949
+ title: nameDropped('import', raw, refusal.reason).title,
9950
+ reasonText: REFUSAL_TEXT[refusal.reason],
9951
+ ...(refusal.file === undefined
9952
+ ? {}
9953
+ : {
9954
+ file: sanitizeText(refusal.file).replaceAll(
9955
+ `
9956
+ `,
9957
+ ' ',
9958
+ ),
9959
+ }),
9960
+ }
9961
+ }
9962
+ function verifyRecord(input) {
9963
+ const landings = createLandings(
9964
+ input.record.projects,
9965
+ input.locals,
9966
+ input.factsFor,
9967
+ )
9968
+ const accepted = []
9969
+ const refused = []
9970
+ const seen = new Set()
9971
+ const local = new Set()
9972
+ for (const raw of input.record.candidates) {
9973
+ const outcome = verifyOne(raw, landings, seen)
9974
+ if ('refusal' in outcome) {
9975
+ refused.push(named(raw, outcome.refusal))
9976
+ continue
9977
+ }
9978
+ if (local.has(outcome.id)) {
9979
+ refused.push(named(raw, refuse('duplicate-candidate').refusal))
9980
+ continue
9981
+ }
9982
+ local.add(outcome.id)
9983
+ accepted.push(outcome)
9984
+ }
9985
+ return { projects: projectReports(landings), accepted, refused }
9986
+ }
9987
+ function provenanceLine(provenance) {
9988
+ const commit =
9989
+ provenance.commit === undefined
9990
+ ? ''
9991
+ : ` at commit ${inline(provenance.commit)}`
9992
+ return `GitHub run ${inline(provenance.runId)} of ${inline(provenance.repo)}${commit}`
9993
+ }
9994
+ function projectLine(project) {
9995
+ const repo = project.repo === undefined ? 'no GitHub remote' : project.repo
9996
+ const head = `- ${inline(project.name)} (${inline(repo)})`
9997
+ if (project.status !== 'mapped') {
9998
+ return `${head}: not mapped (${REFUSAL_TEXT[project.status]})`
9999
+ }
10000
+ const ci =
10001
+ project.ciCommit === undefined ? 'unknown' : inline(project.ciCommit)
10002
+ const local =
10003
+ project.localCommit === undefined ? 'unknown' : inline(project.localCommit)
10004
+ return `${head}: CI commit ${ci}; local commit ${local} in ${inline(project.localRoot ?? '')}`
10005
+ }
10006
+ function refusalLines(refused) {
10007
+ if (refused.length === 0) return []
10008
+ const out = ['Refused (nothing was imported from these):']
10009
+ for (const item of refused) {
10010
+ const where = item.file === undefined ? '' : ` File: ${inline(item.file)}.`
10011
+ const detail = item.detail === undefined ? '' : ` (${item.detail})`
10012
+ const keys =
10013
+ item.keys === undefined
10014
+ ? ''
10015
+ : ` Local project keys: ${item.keys.map(inline).join(', ')}.`
10016
+ out.push(
10017
+ quote(item.title),
10018
+ ` ${item.reason}${detail}: ${item.reasonText}.${where}${keys}`,
10019
+ )
10020
+ }
10021
+ return out
10022
+ }
10023
+ function skippedLines(skipped) {
10024
+ if (skipped.length === 0) return []
10025
+ const out = ['Skipped (already decided locally):']
10026
+ for (const item of skipped) {
10027
+ const pointer =
10028
+ item.refreshPointer === undefined
10029
+ ? ''
10030
+ : ` Refresh pointer: ${inline(item.refreshPointer)}.`
10031
+ out.push(quote(item.title), ` ${item.reason}.${pointer}`)
10032
+ }
10033
+ return out
10034
+ }
10035
+ var MAX_LINES_LISTED = 50
10036
+ function promotionLines(candidates) {
10037
+ if (candidates.length === 0) return []
10038
+ const out = ['Candidates (untrusted text, quoted):']
10039
+ for (const { item, runRecordPath } of candidates.slice(0, MAX_LINES_LISTED)) {
10040
+ out.push(quote(`${item.candidate.kind}: ${item.candidate.title}`))
10041
+ out.push(handoffLine(item, runRecordPath, isPasteablePath(runRecordPath)))
10042
+ }
10043
+ const left = candidates.length - MAX_LINES_LISTED
10044
+ if (left > 0) out.push(`${left} more candidates are in the run record.`)
10045
+ return out
10046
+ }
10047
+ function renderImportSummary(model, digestPath) {
10048
+ const out = [
10049
+ `Connect import ${model.runId === undefined ? '(no new run)' : sanitizeText(model.runId)}: ${model.outcome}.`,
10050
+ `Imported: ${model.imported}. Already imported: ${model.candidates.length - model.imported}. Skipped: ${model.skipped.length}. Refused: ${model.refused.length}.`,
10051
+ `Source: ${provenanceLine(model.provenance)}.`,
10052
+ 'Projects:',
10053
+ ...model.projects.map(projectLine),
10054
+ ]
10055
+ if (model.outcome === 'degraded') {
10056
+ out.push(
10057
+ 'Degraded because: some candidates were refused. A refused candidate is not proof the lesson is wrong; run the scheduled run again to see it afresh.',
10058
+ )
10059
+ }
10060
+ out.push(...refusalLines(model.refused), ...skippedLines(model.skipped))
10061
+ if (digestPath !== undefined) {
10062
+ out.push(
10063
+ `Digest: ${sanitizeText(digestPath).replaceAll(
10064
+ `
10065
+ `,
10066
+ ' ',
10067
+ )}`,
10068
+ )
10069
+ }
10070
+ out.push(...promotionLines(model.candidates))
10071
+ return out.join(`
10072
+ `)
10073
+ }
10074
+ function renderImportDigest(model, written, generatedAt) {
10075
+ const out = [
10076
+ `# Connect import digest ${inline(model.runId ?? 'none')}`,
10077
+ '',
10078
+ `Outcome: ${model.outcome}`,
10079
+ ...(model.degradeReasons.length > 0
10080
+ ? [`Degraded because: ${model.degradeReasons.map(inline).join(', ')}`]
10081
+ : []),
10082
+ `Generated: ${generatedAt}`,
10083
+ `Source: ${provenanceLine(model.provenance)}`,
10084
+ `Run record: ${inline(model.sourceRunId)}`,
10085
+ '',
10086
+ 'Projects:',
10087
+ ...model.projects.map(projectLine),
10088
+ '',
10089
+ ...refusalLines(model.refused),
10090
+ ...(model.refused.length > 0 ? [''] : []),
10091
+ ...skippedLines(model.skipped),
10092
+ ...(model.skipped.length > 0 ? [''] : []),
10093
+ '## Candidates',
10094
+ '',
10095
+ ]
10096
+ if (written.length === 0) out.push('None.', '')
10097
+ written.forEach((item, index) => {
10098
+ out.push(...renderCandidate(item, index))
10099
+ })
10100
+ return `${out
10101
+ .join(`
10102
+ `)
10103
+ .trimEnd()}
10104
+ `
10105
+ }
10106
+
10107
+ // src/lib/connect-repo-identity.ts
10108
+ var MAX_CONFIG_BYTES = 1024 * 1024
10109
+ var MAX_HEAD_BYTES = 4096
10110
+ var MAX_REF_BYTES = 4096
10111
+ var MAX_PACKED_REFS_BYTES = 8 * 1024 * 1024
10112
+ var OWNER = /^[A-Za-z0-9][A-Za-z0-9-]{0,38}$/
10113
+ var REPO2 = /^[A-Za-z0-9._-]{1,100}$/
10114
+ var HTTPS_FORM = /^https:\/\/(?:[^/@\s]*@)?github\.com\/(\S+)$/i
10115
+ var SSH_URL_FORM = /^ssh:\/\/(?:[A-Za-z0-9._-]+@)?github\.com\/(\S+)$/i
10116
+ var SCP_FORM = /^(?:[A-Za-z0-9._-]+@)?github\.com:(\S+)$/i
10117
+ function ownerAndRepo(pathPart) {
10118
+ const segments = pathPart.replace(/\/+$/, '').split('/')
10119
+ if (segments.length !== 2) return
10120
+ const [owner = '', named = ''] = segments
10121
+ const repo = named.endsWith('.git') ? named.slice(0, -'.git'.length) : named
10122
+ if (!OWNER.test(owner) || !REPO2.test(repo)) return
10123
+ if (repo === '.' || repo === '..') return
10124
+ return `${owner}/${repo}`.toLowerCase()
10125
+ }
10126
+ function normalizeGithubRemote(url) {
10127
+ const match =
10128
+ HTTPS_FORM.exec(url) ?? SSH_URL_FORM.exec(url) ?? SCP_FORM.exec(url)
10129
+ const rest = match?.[1]
10130
+ return rest === undefined ? undefined : ownerAndRepo(rest)
10131
+ }
10132
+ function configValue(raw) {
10133
+ let out = ''
10134
+ let quoted = false
10135
+ for (const ch of raw.trim()) {
10136
+ if (ch === '"') {
10137
+ quoted = !quoted
10138
+ continue
10139
+ }
10140
+ if (!quoted && (ch === '#' || ch === ';')) break
10141
+ out += ch
10142
+ }
10143
+ return out.trim()
10144
+ }
10145
+ function isOriginSection(header) {
10146
+ const match = /^([A-Za-z]+)[ \t]+"([^"]*)"$/.exec(header.trim())
10147
+ return match?.[1]?.toLowerCase() === 'remote' && match[2] === 'origin'
10148
+ }
10149
+ function originUrl(config) {
10150
+ let inOrigin = false
10151
+ for (const raw of config.split(`
10152
+ `)) {
10153
+ let line = raw.trim()
10154
+ if (line.startsWith('[')) {
10155
+ const end = line.indexOf(']')
10156
+ if (end === -1) {
10157
+ inOrigin = false
10158
+ continue
10159
+ }
10160
+ inOrigin = isOriginSection(line.slice(1, end))
10161
+ line = line.slice(end + 1).trim()
10162
+ }
10163
+ if (!inOrigin) continue
10164
+ const key = /^url[ \t]*=(.*)$/i.exec(line)
10165
+ if (key?.[1] !== undefined) return configValue(key[1])
10166
+ }
10167
+ return
10168
+ }
10169
+ var OBJECT_ID3 = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/
10170
+ var REF_NAME = /^refs\/[A-Za-z0-9._/-]+$/
10171
+ function readGitText(mainRoot, rel, maxBytes) {
10172
+ const read = readContainedFile(mainRoot, rel, { maxBytes })
10173
+ return read.kind === 'bytes' ? read.bytes.toString('utf8') : undefined
10174
+ }
10175
+ function firstLine2(text) {
10176
+ return (
10177
+ text.split(
10178
+ `
10179
+ `,
10180
+ 1,
10181
+ )[0] ?? ''
10182
+ )
10183
+ .replace(/\r$/, '')
10184
+ .trim()
10185
+ }
10186
+ function safeRefName(ref) {
10187
+ return (
10188
+ REF_NAME.test(ref) &&
10189
+ ref
10190
+ .split('/')
10191
+ .every(
10192
+ (part) =>
10193
+ part !== '' && !part.startsWith('.') && !part.endsWith('.lock'),
10194
+ )
10195
+ )
10196
+ }
10197
+ function packedRef(packed, ref) {
10198
+ for (const raw of packed.split(`
10199
+ `)) {
10200
+ const line = raw.trim()
10201
+ if (line === '' || line.startsWith('#') || line.startsWith('^')) continue
10202
+ const [id, name, ...rest] = line.split(/\s+/)
10203
+ if (rest.length === 0 && name === ref && OBJECT_ID3.test(id ?? ''))
10204
+ return id
10205
+ }
10206
+ return
10207
+ }
10208
+ function headCommit(mainRoot) {
10209
+ const head = readGitText(mainRoot, '.git/HEAD', MAX_HEAD_BYTES)
10210
+ if (head === undefined) return
10211
+ const line = firstLine2(head)
10212
+ if (OBJECT_ID3.test(line)) return line
10213
+ const symbolic = /^ref:[ \t]*(\S+)$/.exec(line)?.[1]
10214
+ if (symbolic === undefined || !safeRefName(symbolic)) return
10215
+ const loose = readGitText(mainRoot, `.git/${symbolic}`, MAX_REF_BYTES)
10216
+ const looseId = loose === undefined ? '' : firstLine2(loose)
10217
+ if (OBJECT_ID3.test(looseId)) return looseId
10218
+ const packed = readGitText(
10219
+ mainRoot,
10220
+ '.git/packed-refs',
10221
+ MAX_PACKED_REFS_BYTES,
10222
+ )
10223
+ return packed === undefined ? undefined : packedRef(packed, symbolic)
10224
+ }
10225
+ function readRepoIdentity(mainRoot) {
10226
+ try {
10227
+ const config = readGitText(mainRoot, '.git/config', MAX_CONFIG_BYTES)
10228
+ const url = config === undefined ? undefined : originUrl(config)
10229
+ const repo = url === undefined ? undefined : normalizeGithubRemote(url)
10230
+ const commit = headCommit(mainRoot)
10231
+ return {
10232
+ ...(repo === undefined ? {} : { repo }),
10233
+ ...(commit === undefined ? {} : { commit }),
10234
+ }
10235
+ } catch {
10236
+ return {}
10237
+ }
10238
+ }
10239
+
10240
+ // src/lib/main-module.ts
10241
+ import fs9 from 'node:fs'
10242
+ import { fileURLToPath } from 'node:url'
10243
+ function resolveRealPath(value) {
10244
+ if (value === undefined || value === '') return
10245
+ try {
10246
+ return fs9.realpathSync(value)
10247
+ } catch {
10248
+ return
10249
+ }
10250
+ }
10251
+ function moduleFilePath(moduleUrl) {
10252
+ try {
10253
+ return fileURLToPath(moduleUrl)
10254
+ } catch {
10255
+ return
10256
+ }
10257
+ }
10258
+ function isMainModule(moduleUrl, argv = process.argv) {
10259
+ const entryPath = resolveRealPath(argv[1])
10260
+ const modulePath = resolveRealPath(moduleFilePath(moduleUrl))
10261
+ return (
10262
+ entryPath !== undefined &&
10263
+ modulePath !== undefined &&
10264
+ entryPath === modulePath
10265
+ )
10266
+ }
10267
+
10268
+ // src/ce-connect-helper.ts
10269
+ var CE_CONNECT_USAGE =
10270
+ 'Usage: node connect.mjs <begin|validate|publish|decide|import|apply-ticks|render-report|ledger show [--offset N]|ledger clear|ledger unlock>'
10271
+ var OPS = [
10272
+ 'begin',
10273
+ 'validate',
10274
+ 'publish',
10275
+ 'decide',
10276
+ 'import',
10277
+ 'apply-ticks',
10278
+ 'render-report',
10279
+ 'ledger-show',
10280
+ 'ledger-clear',
10281
+ 'ledger-unlock',
10282
+ ]
10283
+ function isOp(value) {
10284
+ return OPS.includes(value)
10285
+ }
10286
+ function isRecord6(value) {
10287
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
8117
10288
  }
8118
10289
  function failure2(op, reason, extra = {}) {
8119
10290
  return {
@@ -8158,7 +10329,7 @@ function psField(pid, field) {
8158
10329
  }
8159
10330
  function procStat(pid) {
8160
10331
  try {
8161
- const stat = fs8.readFileSync(`/proc/${pid}/stat`, 'utf8')
10332
+ const stat = fs10.readFileSync(`/proc/${pid}/stat`, 'utf8')
8162
10333
  const close = stat.lastIndexOf(')')
8163
10334
  return close < 0
8164
10335
  ? undefined
@@ -8180,7 +10351,9 @@ var systemProcessInspector = {
8180
10351
  commandOf(pid) {
8181
10352
  if (process.platform === 'linux') {
8182
10353
  try {
8183
- return fs8.readFileSync(`/proc/${pid}/comm`, 'utf8').trim() || undefined
10354
+ return (
10355
+ fs10.readFileSync(`/proc/${pid}/comm`, 'utf8').trim() || undefined
10356
+ )
8184
10357
  } catch {
8185
10358
  return
8186
10359
  }
@@ -8189,7 +10362,7 @@ var systemProcessInspector = {
8189
10362
  },
8190
10363
  }
8191
10364
  function isShell(command) {
8192
- return SHELLS.has(path6.basename(command).replace(/^-/, ''))
10365
+ return SHELLS.has(path8.basename(command).replace(/^-/, ''))
8193
10366
  }
8194
10367
  function findHolderPid(inspector, from = process.pid) {
8195
10368
  let pid = inspector.parentOf(from)
@@ -8221,6 +10394,7 @@ function describeEnvironment(deps) {
8221
10394
  stateRoot: resolveStateRoot(deps.env ?? process.env, homeDir),
8222
10395
  allowedRoots: resolved.resolved.map((item) => item.mainRoot),
8223
10396
  resolved,
10397
+ groups: load.groups,
8224
10398
  warnings: load.warnings,
8225
10399
  configUnreadable: load.unreadable,
8226
10400
  }
@@ -8238,7 +10412,7 @@ function prepare(op, deps) {
8238
10412
  }
8239
10413
  return { ok: true, root: state.root, environment }
8240
10414
  }
8241
- var RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
10415
+ var RUN_ID2 = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
8242
10416
  var MAX_TEXT = 200
8243
10417
  var MAX_BATCH_PATHS = 64
8244
10418
  var MAX_BATCH_BYTES = 1024 * 1024
@@ -8295,7 +10469,7 @@ function fingerprintIndex(projects) {
8295
10469
  `
8296
10470
  }
8297
10471
  function parseLock(value) {
8298
- if (!isRecord4(value)) return
10472
+ if (!isRecord6(value)) return
8299
10473
  const { nonce, pid } = value
8300
10474
  if (typeof nonce !== 'string' || !/^[0-9A-Za-z-]{8,64}$/.test(nonce)) {
8301
10475
  return
@@ -8306,24 +10480,24 @@ function parseLock(value) {
8306
10480
  return { nonce, pid }
8307
10481
  }
8308
10482
  function parseRunRequest(request) {
8309
- if (!isRecord4(request)) return
10483
+ if (!isRecord6(request)) return
8310
10484
  const { runId } = request
8311
- if (typeof runId !== 'string' || !RUN_ID.test(runId)) return
10485
+ if (typeof runId !== 'string' || !RUN_ID2.test(runId)) return
8312
10486
  if (runId.endsWith('.tmp')) return
8313
10487
  const lock = parseLock(request.lock)
8314
10488
  return lock === undefined ? undefined : { runId, lock, record: request }
8315
10489
  }
8316
10490
  function isSnapshot(value) {
8317
10491
  return (
8318
- isRecord4(value) &&
10492
+ isRecord6(value) &&
8319
10493
  value.version === 1 &&
8320
10494
  typeof value.runId === 'string' &&
8321
- isRecord4(value.lock) &&
10495
+ isRecord6(value.lock) &&
8322
10496
  typeof value.lock.nonce === 'string' &&
8323
10497
  Array.isArray(value.projects) &&
8324
10498
  Array.isArray(value.ruleIds) &&
8325
10499
  Array.isArray(value.ruleVersions) &&
8326
- isRecord4(value.offer) &&
10500
+ isRecord6(value.offer) &&
8327
10501
  Array.isArray(value.offer.offered) &&
8328
10502
  Array.isArray(value.offer.fresh) &&
8329
10503
  Array.isArray(value.provenance) &&
@@ -8333,18 +10507,18 @@ function isSnapshot(value) {
8333
10507
  }
8334
10508
  function isValidated(value) {
8335
10509
  return (
8336
- isRecord4(value) &&
10510
+ isRecord6(value) &&
8337
10511
  value.version === 1 &&
8338
10512
  typeof value.runId === 'string' &&
8339
10513
  Array.isArray(value.accepted) &&
8340
10514
  Array.isArray(value.recorded) &&
8341
10515
  Array.isArray(value.droppedCandidates) &&
8342
10516
  Array.isArray(value.coveredRuleIds) &&
8343
- isRecord4(value.sweep) &&
10517
+ isRecord6(value.sweep) &&
8344
10518
  Array.isArray(value.degradeReasons)
8345
10519
  )
8346
10520
  }
8347
- function readJsonFile(dir, name, maxBytes, guard) {
10521
+ function readJsonFile2(dir, name, maxBytes, guard) {
8348
10522
  const read = readWorkFile(dir, name, maxBytes)
8349
10523
  if (read.kind !== 'bytes') return
8350
10524
  try {
@@ -8357,9 +10531,85 @@ function readJsonFile(dir, name, maxBytes, guard) {
8357
10531
  function addUnique(list, value) {
8358
10532
  if (!list.includes(value)) list.push(value)
8359
10533
  }
10534
+ function ciRunMarker(deps) {
10535
+ return readCiRunMarker(
10536
+ resolveStateRoot(deps.env ?? process.env, deps.homeDir ?? os2.homedir()),
10537
+ )
10538
+ }
10539
+ function invocationProjects(projects) {
10540
+ return projects.map((project) => ({
10541
+ key: project.key,
10542
+ name: project.name,
10543
+ ...(project.repo === undefined ? {} : { repo: project.repo }),
10544
+ ...(project.commit === undefined ? {} : { commit: project.commit }),
10545
+ }))
10546
+ }
10547
+ function beganWith(root, marker, runId) {
10548
+ if (runId === undefined) return
10549
+ const began = readInvocationRecord(root)
10550
+ return began?.marker === marker && began.runId === runId
10551
+ ? began.projects
10552
+ : undefined
10553
+ }
10554
+ function writeTerminal(root, marker, end, deps) {
10555
+ const projects = end.projects ?? beganWith(root, marker, end.runId)
10556
+ const result = writeInvocationRecord(
10557
+ root,
10558
+ {
10559
+ version: 1,
10560
+ marker,
10561
+ phase: 'terminal',
10562
+ outcome: end.outcome,
10563
+ finalLine: `Connect ${end.outcome}`,
10564
+ ...(end.reason === undefined ? {} : { reason: end.reason }),
10565
+ ...(end.runId === undefined ? {} : { runId: end.runId }),
10566
+ ...(projects === undefined ? {} : { projects }),
10567
+ },
10568
+ deps.invocationHooks,
10569
+ )
10570
+ return result === 'written' ? 'written' : 'unwritten'
10571
+ }
10572
+ function endInvocation(marker, result, end, deps) {
10573
+ const prepared = prepare('invocation', deps)
10574
+ const written = prepared.ok
10575
+ ? writeTerminal(prepared.root, marker, end, deps)
10576
+ : 'unwritten'
10577
+ return written === 'written'
10578
+ ? result
10579
+ : { ok: result.ok, body: { ...result.body, invocationRecord: 'unwritten' } }
10580
+ }
10581
+ function endOfFailure(result, runId) {
10582
+ const { outcome, reason } = result.body
10583
+ return {
10584
+ outcome: outcome === 'busy' ? 'busy' : 'failed',
10585
+ ...(typeof reason === 'string' ? { reason } : {}),
10586
+ ...(runId === undefined ? {} : { runId }),
10587
+ }
10588
+ }
10589
+ function requestRunId(request) {
10590
+ if (!isRecord6(request)) return
10591
+ const { runId } = request
10592
+ return typeof runId === 'string' &&
10593
+ RUN_ID2.test(runId) &&
10594
+ !runId.endsWith('.tmp')
10595
+ ? runId
10596
+ : undefined
10597
+ }
10598
+ function parseSelectionWords(value) {
10599
+ if (value === undefined) return []
10600
+ if (!Array.isArray(value)) return
10601
+ const words = []
10602
+ for (const word of value) {
10603
+ if (typeof word !== 'string') return
10604
+ words.push(word)
10605
+ }
10606
+ return words
10607
+ }
8360
10608
  function parseBeginRequest(request) {
8361
- if (!isRecord4(request)) return
10609
+ if (!isRecord6(request)) return
8362
10610
  const { host, version, mode, consent } = request
10611
+ const selection = parseSelectionWords(request.selection)
10612
+ if (selection === undefined) return
8363
10613
  if (typeof host !== 'string' || host === '' || host.length > MAX_TEXT) {
8364
10614
  return
8365
10615
  }
@@ -8371,7 +10621,7 @@ function parseBeginRequest(request) {
8371
10621
  }
8372
10622
  if (mode !== 'interactive' && mode !== 'headless') return
8373
10623
  if (consent !== undefined && typeof consent !== 'boolean') return
8374
- return { host, version, mode, consent: consent === true }
10624
+ return { host, version, mode, consent: consent === true, selection }
8375
10625
  }
8376
10626
  var CORPUS_PREFIXES = ['docs/solutions/', 'docs/plans/', 'docs/brainstorms/']
8377
10627
  var MAX_STALE_RECORDED = 200
@@ -8515,6 +10765,7 @@ function scanProject(entry, root, deps, limits) {
8515
10765
  root: inventory.mainRoot,
8516
10766
  key: projectKey(inventory.mainRoot),
8517
10767
  entry,
10768
+ ...readRepoIdentity(inventory.mainRoot),
8518
10769
  files: inventory.files.length,
8519
10770
  citationsUnsupported: report.unsupported,
8520
10771
  citationsChecked: report.citations.length,
@@ -8601,7 +10852,7 @@ function gateHost(parsed, deps) {
8601
10852
  function isResult(value) {
8602
10853
  return 'body' in value
8603
10854
  }
8604
- function scanAll(environment, deps, limits, unmatched) {
10855
+ function scanAll(environment, selection, projects, deps, limits, unmatched) {
8605
10856
  const degradeReasons = []
8606
10857
  const unread = []
8607
10858
  if (unmatched.length > 0) addUnique(degradeReasons, 'unmatched-entry')
@@ -8611,7 +10862,7 @@ function scanAll(environment, deps, limits, unmatched) {
8611
10862
  }
8612
10863
  const scans = []
8613
10864
  let allRead = unmatched.length === 0 && !environment.configUnreadable
8614
- for (const item of environment.resolved.resolved) {
10865
+ for (const item of projects) {
8615
10866
  const scanned = scanProject(item.entry, item.mainRoot, deps, limits)
8616
10867
  if ('failed' in scanned) {
8617
10868
  allRead = false
@@ -8628,10 +10879,17 @@ function scanAll(environment, deps, limits, unmatched) {
8628
10879
  .filter((scan) => scan.prunable)
8629
10880
  .map((scan) => scan.digest.key),
8630
10881
  retain: scans.flatMap((scan) => scan.retain),
8631
- pruneUnattributed: allRead && scans.every((scan) => scan.prunable),
10882
+ pruneUnattributed:
10883
+ selection.kind === 'all' &&
10884
+ allRead &&
10885
+ scans.every((scan) => scan.prunable),
8632
10886
  }
8633
10887
  return { scans, degradeReasons, unread, coverageScope }
8634
10888
  }
10889
+ function selectionOf(selection) {
10890
+ const id = selectionNamespace(selection)
10891
+ return id === undefined ? {} : { selection: id }
10892
+ }
8635
10893
  function prunedCounts(pruned) {
8636
10894
  return pruned.status === 'pruned'
8637
10895
  ? {
@@ -8647,9 +10905,9 @@ function beginResponse(setup, snapshot, scans, workDir, limits) {
8647
10905
  runId: snapshot.runId,
8648
10906
  stateRoot: setup.root,
8649
10907
  workDir,
8650
- rulesFile: path6.join(workDir, 'rules.json'),
8651
- filesIndex: path6.join(workDir, 'files.tsv'),
8652
- rulesOverview: path6.join(workDir, 'rules.tsv'),
10908
+ rulesFile: path8.join(workDir, 'rules.json'),
10909
+ filesIndex: path8.join(workDir, 'files.tsv'),
10910
+ rulesOverview: path8.join(workDir, 'rules.tsv'),
8653
10911
  offeredRuleIds: snapshot.offer.offered,
8654
10912
  offeredCount: snapshot.offer.offered.length,
8655
10913
  sweep: {
@@ -8663,6 +10921,7 @@ function beginResponse(setup, snapshot, scans, workDir, limits) {
8663
10921
  ...(setup.reclaimed === undefined ? {} : { reclaimed: setup.reclaimed }),
8664
10922
  host: snapshot.host,
8665
10923
  mode: setup.parsed.mode,
10924
+ selection: setup.selection,
8666
10925
  projects: scans.map((scan) => ({
8667
10926
  root: scan.digest.root,
8668
10927
  name: scan.digest.name,
@@ -8700,6 +10959,8 @@ function indexRun(setup, deps) {
8700
10959
  const limits = bounds(deps)
8701
10960
  const { scans, degradeReasons, unread, coverageScope } = scanAll(
8702
10961
  setup.environment,
10962
+ setup.selection,
10963
+ setup.projects,
8703
10964
  deps,
8704
10965
  limits,
8705
10966
  setup.unmatched,
@@ -8719,6 +10980,7 @@ function indexRun(setup, deps) {
8719
10980
  rules,
8720
10981
  coverage: ledger.ledger.coverage ?? [],
8721
10982
  size: limits.maxFamiliesPerRun * limits.maxRulesPerFamily,
10983
+ ...selectionOf(setup.selection),
8722
10984
  })
8723
10985
  const { parsed, verdict } = setup
8724
10986
  const snapshot = {
@@ -8740,6 +11002,7 @@ function indexRun(setup, deps) {
8740
11002
  unmatched: setup.unmatched,
8741
11003
  degradeReasons,
8742
11004
  unread,
11005
+ selection: setup.selection,
8743
11006
  ruleIds: rules.map((rule) => rule.id),
8744
11007
  ruleVersions: rules.map((rule) => ({
8745
11008
  id: rule.id,
@@ -8766,22 +11029,83 @@ function indexRun(setup, deps) {
8766
11029
  )
8767
11030
  writeWorkFile(workDir, 'files.tsv', fingerprintIndex(snapshot.projects))
8768
11031
  writeWorkFile(workDir, 'rules.tsv', rulesOverview(rules, offer))
11032
+ if (setup.ciMarker !== undefined) {
11033
+ const recorded = writeInvocationRecord(
11034
+ root,
11035
+ {
11036
+ version: 1,
11037
+ marker: setup.ciMarker,
11038
+ phase: 'begun',
11039
+ runId,
11040
+ projects: invocationProjects(snapshot.projects),
11041
+ },
11042
+ deps.invocationHooks,
11043
+ )
11044
+ if (recorded === 'failed') {
11045
+ return releaseAndFail(
11046
+ 'begin',
11047
+ lock,
11048
+ root,
11049
+ runId,
11050
+ 'invocation-write-failed',
11051
+ )
11052
+ }
11053
+ }
8769
11054
  return beginResponse(setup, snapshot, scans, workDir, limits)
8770
11055
  } catch {
8771
11056
  return releaseAndFail('begin', lock, root, runId, 'internal-error')
8772
11057
  }
8773
11058
  }
11059
+ function selectProjects(words, environment) {
11060
+ const resolution = resolveSelection({
11061
+ words,
11062
+ groups: environment.groups,
11063
+ allowed: environment.resolved.resolved,
11064
+ resolveMember: (entry) =>
11065
+ resolveAllowEntry(entry, { homeDir: environment.homeDir }),
11066
+ })
11067
+ if (resolution.status === 'invalid') {
11068
+ return { ok: false, result: failure2('begin', 'invalid-request') }
11069
+ }
11070
+ if (resolution.status === 'refused') {
11071
+ const { reason, ...detail } = resolution.refusal
11072
+ return { ok: false, result: failure2('begin', reason, detail) }
11073
+ }
11074
+ return {
11075
+ ok: true,
11076
+ selection: resolution.selection,
11077
+ projects: resolution.projects,
11078
+ }
11079
+ }
8774
11080
  function begin(request, deps) {
11081
+ const ciMarker = ciRunMarker(deps)
11082
+ const result = beginRun(request, deps, ciMarker)
11083
+ if (
11084
+ ciMarker === undefined ||
11085
+ result.ok ||
11086
+ result.body.outcome === 'consent-required'
11087
+ ) {
11088
+ return result
11089
+ }
11090
+ return endInvocation(ciMarker, result, endOfFailure(result), deps)
11091
+ }
11092
+ function beginRun(request, deps, ciMarker) {
8775
11093
  const parsed = parseBeginRequest(request)
8776
11094
  if (parsed === undefined) return failure2('begin', 'invalid-request')
11095
+ const environment = describeEnvironment(deps)
11096
+ const chosen = selectProjects(parsed.selection, environment)
11097
+ if (!chosen.ok) return chosen.result
8777
11098
  const verdict = gateHost(parsed, deps)
8778
11099
  if (isResult(verdict)) return verdict
8779
- const environment = describeEnvironment(deps)
8780
- const unmatched = environment.resolved.unmatched.map((item) => ({
8781
- entry: item.entry,
8782
- reason: item.reason,
8783
- }))
8784
- if (environment.resolved.resolved.length === 0) {
11100
+ const { selection, projects } = chosen
11101
+ const unmatched =
11102
+ selection.kind === 'all'
11103
+ ? environment.resolved.unmatched.map((item) => ({
11104
+ entry: item.entry,
11105
+ reason: item.reason,
11106
+ }))
11107
+ : []
11108
+ if (projects.length === 0) {
8785
11109
  return failure2('begin', 'no-allowed-projects', {
8786
11110
  unmatched,
8787
11111
  configWarnings: environment.warnings,
@@ -8819,12 +11143,15 @@ function begin(request, deps) {
8819
11143
  parsed,
8820
11144
  verdict,
8821
11145
  environment,
11146
+ selection,
11147
+ projects,
8822
11148
  unmatched,
8823
11149
  root: state.root,
8824
11150
  now,
8825
11151
  holder,
8826
11152
  lock: acquired.lock,
8827
11153
  reclaimed: acquired.reclaimed,
11154
+ ciMarker,
8828
11155
  }
8829
11156
  try {
8830
11157
  return indexRun(setup, deps)
@@ -8845,15 +11172,15 @@ function readBatches(workDir, runId, requested) {
8845
11172
  const batches = []
8846
11173
  for (const given of requested) {
8847
11174
  let name
8848
- if (path6.isAbsolute(given)) {
8849
- if (path6.dirname(path6.resolve(given)) !== workDir) {
11175
+ if (path8.isAbsolute(given)) {
11176
+ if (path8.dirname(path8.resolve(given)) !== workDir) {
8850
11177
  return { ok: false, reason: 'batch-outside-work-dir' }
8851
11178
  }
8852
- name = path6.basename(given)
11179
+ name = path8.basename(given)
8853
11180
  } else {
8854
11181
  name = given
8855
11182
  }
8856
- if (!isWorkName(name) || name !== path6.basename(name)) {
11183
+ if (!isWorkName(name) || name !== path8.basename(name)) {
8857
11184
  return { ok: false, reason: 'batch-outside-work-dir' }
8858
11185
  }
8859
11186
  if (RESERVED_WORK_FILES.has(name)) {
@@ -8903,11 +11230,11 @@ function contextFrom(snapshot) {
8903
11230
  }
8904
11231
  return { projects, ruleIds: new Set(snapshot.ruleIds) }
8905
11232
  }
8906
- function count(table, key, by = 1) {
11233
+ function count2(table, key, by = 1) {
8907
11234
  table[key] = (table[key] ?? 0) + by
8908
11235
  }
8909
11236
  function recordDrop(result, family, raw, reason) {
8910
- count(result.dropped, reason)
11237
+ count2(result.dropped, reason)
8911
11238
  noteDropped(result, family, raw, reason)
8912
11239
  }
8913
11240
  function noteDropped(result, family, raw, reason) {
@@ -8961,7 +11288,7 @@ function reopensForChangedSources(record, input) {
8961
11288
  return outcome.standing === 'reopened' && outcome.reason === 'sources changed'
8962
11289
  }
8963
11290
  function holdBack(raw, input, result) {
8964
- const ruleId = isRecord4(raw) ? raw.ruleId : undefined
11291
+ const ruleId = isRecord6(raw) ? raw.ruleId : undefined
8965
11292
  if (typeof ruleId === 'string' && input.ctx.ruleIds.has(ruleId)) {
8966
11293
  result.heldBackRuleIds.add(ruleId)
8967
11294
  }
@@ -9024,7 +11351,7 @@ function classifyBatches(batches, input, limits) {
9024
11351
  const considered = batch.candidates.slice(0, limits.maxCandidatesPerBatch)
9025
11352
  const over = batch.candidates.length - considered.length
9026
11353
  if (over > 0) {
9027
- count(result.dropped, 'over-batch-cap', over)
11354
+ count2(result.dropped, 'over-batch-cap', over)
9028
11355
  addUnique(result.degrade, 'candidate-bound')
9029
11356
  }
9030
11357
  for (const raw of considered) {
@@ -9113,7 +11440,7 @@ function validateResponse(runId, state, classified) {
9113
11440
  })
9114
11441
  }
9115
11442
  function loadRun(op, workDir, runId, nonce) {
9116
- const snapshot = readJsonFile(
11443
+ const snapshot = readJsonFile2(
9117
11444
  workDir,
9118
11445
  'begin.json',
9119
11446
  MAX_SNAPSHOT_BYTES,
@@ -9195,7 +11522,7 @@ function runValidation(snapshot, workDir, batchesRead, ledger, deps) {
9195
11522
  return validateResponse(snapshot.runId, state, classified)
9196
11523
  }
9197
11524
  function refuseValidate(request, deps) {
9198
- const lock = isRecord4(request) ? parseLock(request.lock) : undefined
11525
+ const lock = isRecord6(request) ? parseLock(request.lock) : undefined
9199
11526
  if (lock === undefined) {
9200
11527
  return failure2('validate', 'invalid-request', { lockReleased: false })
9201
11528
  }
@@ -9204,9 +11531,9 @@ function refuseValidate(request, deps) {
9204
11531
  deps.homeDir ?? os2.homedir(),
9205
11532
  )
9206
11533
  const runId =
9207
- isRecord4(request) &&
11534
+ isRecord6(request) &&
9208
11535
  typeof request.runId === 'string' &&
9209
- RUN_ID.test(request.runId) &&
11536
+ RUN_ID2.test(request.runId) &&
9210
11537
  !request.runId.endsWith('.tmp')
9211
11538
  ? request.runId
9212
11539
  : undefined
@@ -9219,6 +11546,23 @@ function refuseValidate(request, deps) {
9219
11546
  )
9220
11547
  }
9221
11548
  function validate(request, deps) {
11549
+ const ciMarker = ciRunMarker(deps)
11550
+ const result = validateRun(request, deps)
11551
+ if (
11552
+ ciMarker === undefined ||
11553
+ result.ok ||
11554
+ result.body.lockReleased !== true
11555
+ ) {
11556
+ return result
11557
+ }
11558
+ return endInvocation(
11559
+ ciMarker,
11560
+ result,
11561
+ endOfFailure(result, requestRunId(request)),
11562
+ deps,
11563
+ )
11564
+ }
11565
+ function validateRun(request, deps) {
9222
11566
  const parsed = parseValidateRequest(request)
9223
11567
  if (parsed === undefined) return refuseValidate(request, deps)
9224
11568
  const root = resolveStateRoot(
@@ -9296,6 +11640,9 @@ function digestModel(snapshot, state, outcome, degradeReasons) {
9296
11640
  mode: snapshot.mode,
9297
11641
  hostSupport: snapshot.host,
9298
11642
  degradeReasons,
11643
+ ...(snapshot.selection === undefined
11644
+ ? {}
11645
+ : { selection: snapshot.selection }),
9299
11646
  sweep: state.sweep,
9300
11647
  projects,
9301
11648
  unmatched: snapshot.unmatched,
@@ -9324,10 +11671,13 @@ function runRecord(snapshot, state, outcome, degradeReasons) {
9324
11671
  ? {}
9325
11672
  : { lockReclaim: snapshot.reclaimed }),
9326
11673
  pruned: snapshot.pruned,
11674
+ selection: snapshot.selection ?? { kind: 'all' },
9327
11675
  projects: snapshot.projects.map((project) => ({
9328
11676
  name: project.name,
9329
11677
  root: project.root,
9330
11678
  key: project.key,
11679
+ ...(project.repo === undefined ? {} : { repo: project.repo }),
11680
+ ...(project.commit === undefined ? {} : { commit: project.commit }),
9331
11681
  files: project.files,
9332
11682
  citationsUnsupported: project.citationsUnsupported,
9333
11683
  citationsChecked: project.citationsChecked,
@@ -9397,7 +11747,7 @@ function runFiles(snapshot, state, outcome, degradeReasons) {
9397
11747
  }
9398
11748
  var LEDGER_NOT_WRITTEN =
9399
11749
  'the ledger could not be written; the candidates of this run may be proposed again'
9400
- function commitRun(snapshot, state, handle, root, deps) {
11750
+ function commitRun(snapshot, state, handle, root, deps, ciMarker) {
9401
11751
  const degradeReasons = [...state.degradeReasons]
9402
11752
  const outcome = degradeReasons.length > 0 ? 'degraded' : 'complete'
9403
11753
  const first = runFiles(snapshot, state, outcome, degradeReasons)
@@ -9419,6 +11769,9 @@ function commitRun(snapshot, state, handle, root, deps) {
9419
11769
  ...(rule.project === undefined ? {} : { project: rule.project }),
9420
11770
  })),
9421
11771
  covered: state.coveredRuleIds,
11772
+ ...(snapshot.selection === undefined
11773
+ ? {}
11774
+ : selectionOf(snapshot.selection)),
9422
11775
  ...(scope === undefined
9423
11776
  ? {}
9424
11777
  : {
@@ -9459,16 +11812,30 @@ function commitRun(snapshot, state, handle, root, deps) {
9459
11812
  const released = releaseLock(handle)
9460
11813
  removeWorkDir(root, snapshot.runId)
9461
11814
  const finalOutcome = ledgerWritten ? outcome : 'degraded'
9462
- const digestPath = path6.join(published.runDir, 'digest.md')
11815
+ const digestPath = path8.join(published.runDir, 'digest.md')
11816
+ const recorded =
11817
+ ciMarker === undefined
11818
+ ? 'written'
11819
+ : writeTerminal(
11820
+ root,
11821
+ ciMarker,
11822
+ {
11823
+ outcome: finalOutcome,
11824
+ runId: snapshot.runId,
11825
+ projects: invocationProjects(snapshot.projects),
11826
+ },
11827
+ deps,
11828
+ )
9463
11829
  return success('publish', {
9464
11830
  outcome: finalOutcome,
9465
11831
  finalLine: `Connect ${finalOutcome}`,
9466
11832
  runId: snapshot.runId,
9467
11833
  runDir: published.runDir,
9468
11834
  digestPath,
9469
- runRecordPath: path6.join(published.runDir, 'run.json'),
11835
+ runRecordPath: path8.join(published.runDir, 'run.json'),
9470
11836
  ledger: published.ledger,
9471
11837
  ...(recordsCurrent ? {} : { runRecordCurrent: false }),
11838
+ ...(recorded === 'written' ? {} : { invocationRecord: 'unwritten' }),
9472
11839
  lockReleased: released === 'released',
9473
11840
  degradeReasons,
9474
11841
  candidates: state.accepted.length,
@@ -9476,11 +11843,30 @@ function commitRun(snapshot, state, handle, root, deps) {
9476
11843
  summary: renderSummary(
9477
11844
  final.model,
9478
11845
  digestPath,
9479
- path6.join(published.runDir, 'run.json'),
11846
+ path8.join(published.runDir, 'run.json'),
9480
11847
  ),
9481
11848
  })
9482
11849
  }
9483
11850
  function publish(request, deps) {
11851
+ const ciMarker = ciRunMarker(deps)
11852
+ const result = publishRunOp(request, deps, ciMarker)
11853
+ if (ciMarker === undefined) return result
11854
+ const runId = requestRunId(request)
11855
+ if (result.ok) {
11856
+ return result.body.outcome === 'aborted'
11857
+ ? endInvocation(
11858
+ ciMarker,
11859
+ result,
11860
+ { outcome: 'failed', reason: 'aborted', runId },
11861
+ deps,
11862
+ )
11863
+ : result
11864
+ }
11865
+ return result.body.lockReleased === true
11866
+ ? endInvocation(ciMarker, result, endOfFailure(result, runId), deps)
11867
+ : result
11868
+ }
11869
+ function publishRunOp(request, deps, ciMarker) {
9484
11870
  const parsed = parseRunRequest(request)
9485
11871
  if (parsed === undefined) return failure2('publish', 'invalid-request')
9486
11872
  const root = resolveStateRoot(
@@ -9497,7 +11883,7 @@ function publish(request, deps) {
9497
11883
  const snapshot = loadRun('publish', workDir, parsed.runId, parsed.lock.nonce)
9498
11884
  if (!isSnapshotValue(snapshot)) return snapshot
9499
11885
  try {
9500
- const state = readJsonFile(
11886
+ const state = readJsonFile2(
9501
11887
  workDir,
9502
11888
  'validated.json',
9503
11889
  MAX_STATE_BYTES,
@@ -9512,7 +11898,7 @@ function publish(request, deps) {
9512
11898
  'not-validated',
9513
11899
  )
9514
11900
  }
9515
- return commitRun(snapshot, state, handle, root, deps)
11901
+ return commitRun(snapshot, state, handle, root, deps, ciMarker)
9516
11902
  } catch {
9517
11903
  return releaseAndFail(
9518
11904
  'publish',
@@ -9524,7 +11910,7 @@ function publish(request, deps) {
9524
11910
  }
9525
11911
  }
9526
11912
  function parseDecision(request) {
9527
- if (!isRecord4(request)) return
11913
+ if (!isRecord6(request)) return
9528
11914
  const { id, status, reason, destination } = request
9529
11915
  if (typeof id !== 'string' || typeof reason !== 'string') return
9530
11916
  if (status !== 'promoted' && status !== 'rejected') return
@@ -9575,7 +11961,7 @@ function decide(request, deps) {
9575
11961
  }
9576
11962
  var MAX_LEDGER_ENTRIES_SHOWN = 500
9577
11963
  function parseShowRequest(request) {
9578
- if (!isRecord4(request)) return
11964
+ if (!isRecord6(request)) return
9579
11965
  const { offset } = request
9580
11966
  if (offset === undefined) return { offset: 0 }
9581
11967
  if (
@@ -9587,6 +11973,67 @@ function parseShowRequest(request) {
9587
11973
  }
9588
11974
  return { offset }
9589
11975
  }
11976
+ var MAX_COVERAGE_NAMESPACES_SHOWN = 50
11977
+ var MAX_LABEL_PICKS = 32
11978
+ var MAX_LABEL_TEXT = 100
11979
+ var LABEL_FORBIDDEN = /[\p{Cc}\p{Cf}]/u
11980
+ function labelText(value) {
11981
+ return typeof value === 'string' &&
11982
+ value.length > 0 &&
11983
+ value.length <= MAX_LABEL_TEXT &&
11984
+ !LABEL_FORBIDDEN.test(value)
11985
+ ? value
11986
+ : undefined
11987
+ }
11988
+ function selectionLabel2(record) {
11989
+ if (!isRecord6(record) || !isRecord6(record.selection)) return
11990
+ const { kind, name, picks } = record.selection
11991
+ if (kind === 'group') {
11992
+ const text = labelText(name)
11993
+ return text === undefined ? undefined : `group:${text}`
11994
+ }
11995
+ if (kind !== 'projects' || !Array.isArray(picks)) return
11996
+ if (picks.length === 0 || picks.length > MAX_LABEL_PICKS) return
11997
+ const texts = picks.map(labelText)
11998
+ return texts.every((text) => text !== undefined)
11999
+ ? `projects:${texts.join(',')}`
12000
+ : undefined
12001
+ }
12002
+ function coverageNamespaces(root, coverage) {
12003
+ const tallies = new Map()
12004
+ for (const record of coverage) {
12005
+ const key = record.selection ?? 'all'
12006
+ const tally = tallies.get(key)
12007
+ if (tally === undefined) {
12008
+ tallies.set(key, { selection: key, rules: 1, newest: record })
12009
+ continue
12010
+ }
12011
+ tally.rules++
12012
+ const newer =
12013
+ record.at > tally.newest.at ||
12014
+ (record.at === tally.newest.at && record.runId > tally.newest.runId)
12015
+ if (newer) tally.newest = record
12016
+ }
12017
+ const ordered = [...tallies.values()].sort((a, b) => {
12018
+ if (a.selection === 'all' || b.selection === 'all') {
12019
+ return a.selection === 'all' ? -1 : 1
12020
+ }
12021
+ if (a.newest.at !== b.newest.at) return a.newest.at < b.newest.at ? 1 : -1
12022
+ return a.selection < b.selection ? -1 : 1
12023
+ })
12024
+ const shown = ordered.slice(0, MAX_COVERAGE_NAMESPACES_SHOWN)
12025
+ const listed = shown.map((tally) => {
12026
+ if (tally.selection === 'all')
12027
+ return { selection: 'all', rules: tally.rules }
12028
+ const label = selectionLabel2(readRunRecord(root, tally.newest.runId))
12029
+ return {
12030
+ selection: tally.selection,
12031
+ rules: tally.rules,
12032
+ ...(label === undefined ? {} : { label }),
12033
+ }
12034
+ })
12035
+ return { listed, omitted: ordered.length - shown.length }
12036
+ }
9590
12037
  function ledgerShow(request, deps) {
9591
12038
  const page = parseShowRequest(request)
9592
12039
  if (page === undefined) return failure2('ledger-show', 'invalid-request')
@@ -9599,11 +12046,19 @@ function ledgerShow(request, deps) {
9599
12046
  const total = read.ledger.entries.length
9600
12047
  const end = page.offset + MAX_LEDGER_ENTRIES_SHOWN
9601
12048
  const entries = read.ledger.entries.slice(page.offset, end)
12049
+ const records = read.ledger.coverage ?? []
12050
+ const namespaces = coverageNamespaces(prepared.root, records)
9602
12051
  return success('ledger-show', {
9603
12052
  total,
9604
12053
  offset: page.offset,
9605
12054
  nextOffset: end < total ? end : null,
9606
- coverage: { rules: read.ledger.coverage?.length ?? 0 },
12055
+ coverage: {
12056
+ rules: records.length,
12057
+ namespaces: namespaces.listed,
12058
+ ...(namespaces.omitted > 0
12059
+ ? { namespacesOmitted: namespaces.omitted }
12060
+ : {}),
12061
+ },
9607
12062
  entries: entries.map((entry) => ({
9608
12063
  id: entry.id,
9609
12064
  kind: entry.kind,
@@ -9661,6 +12116,514 @@ function ledgerUnlock(deps) {
9661
12116
  : {}),
9662
12117
  })
9663
12118
  }
12119
+ var GITHUB_REPO = /^[A-Za-z0-9][A-Za-z0-9-]{0,38}\/[A-Za-z0-9._-]{1,100}$/
12120
+ var GITHUB_RUN_ID = /^[0-9]{1,20}$/
12121
+ var COMMIT_ID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/
12122
+ var MAX_DIRECTORY_LENGTH = 4096
12123
+ function parseImportRequest(request) {
12124
+ if (!isRecord6(request)) return
12125
+ const { directory, provenance } = request
12126
+ if (
12127
+ typeof directory !== 'string' ||
12128
+ directory === '' ||
12129
+ directory.length > MAX_DIRECTORY_LENGTH ||
12130
+ CONTROL_IN_PATH.test(directory) ||
12131
+ !path8.isAbsolute(directory)
12132
+ ) {
12133
+ return
12134
+ }
12135
+ if (!isRecord6(provenance)) return
12136
+ const { repo, runId, commit } = provenance
12137
+ if (typeof repo !== 'string' || !GITHUB_REPO.test(repo)) return
12138
+ if (typeof runId !== 'string' || !GITHUB_RUN_ID.test(runId)) return
12139
+ if (
12140
+ commit !== undefined &&
12141
+ !(typeof commit === 'string' && COMMIT_ID.test(commit))
12142
+ ) {
12143
+ return
12144
+ }
12145
+ return {
12146
+ directory,
12147
+ provenance: {
12148
+ repo,
12149
+ runId,
12150
+ ...(commit === undefined ? {} : { commit }),
12151
+ },
12152
+ }
12153
+ }
12154
+ function importFailure(reason, extra = {}) {
12155
+ return failure2('import', reason, { finalLine: 'Connect failed', ...extra })
12156
+ }
12157
+ function importLocals(environment) {
12158
+ return environment.resolved.resolved.map(({ mainRoot }) => ({
12159
+ root: mainRoot,
12160
+ key: projectKey(mainRoot),
12161
+ name: projectName(mainRoot),
12162
+ ...readRepoIdentity(mainRoot),
12163
+ }))
12164
+ }
12165
+ function importFacts(deps) {
12166
+ const limits = bounds(deps)
12167
+ return (root) => {
12168
+ if (UNSAFE_PATH_CHARACTER.test(root)) return
12169
+ const inventory = inventoryProject(root, {
12170
+ bounds: limits,
12171
+ ...(deps.gitBinary === undefined ? {} : { gitBinary: deps.gitBinary }),
12172
+ })
12173
+ return inventory.status === 'ok'
12174
+ ? toProjectFacts(inventory.mainRoot, inventory.files)
12175
+ : undefined
12176
+ }
12177
+ }
12178
+ var NO_PLACEMENT = {
12179
+ fresh: [],
12180
+ open: [],
12181
+ skipped: [],
12182
+ order: [],
12183
+ }
12184
+ function evidenceOf(candidate) {
12185
+ return JSON.stringify([
12186
+ candidate.target,
12187
+ candidate.sources,
12188
+ candidate.targetEvidence,
12189
+ candidate.counterevidence,
12190
+ ])
12191
+ }
12192
+ function keptImportRuns(root, runs) {
12193
+ const out = []
12194
+ for (const runId of runs) {
12195
+ const record = readRunRecord(root, runId)
12196
+ if (!isRecord6(record) || record.kind !== 'import') continue
12197
+ const block = record.candidateBlock
12198
+ if (!isRecord6(block) || !Array.isArray(block.candidates)) continue
12199
+ const evidence = new Map()
12200
+ for (const item of block.candidates) {
12201
+ if (isRecord6(item) && typeof item.id === 'string') {
12202
+ evidence.set(item.id, evidenceOf(item))
12203
+ }
12204
+ }
12205
+ out.push({ runId, evidence })
12206
+ }
12207
+ return out
12208
+ }
12209
+ function freshOf(item, standing, extra = {}) {
12210
+ return {
12211
+ kind: 'fresh',
12212
+ id: item.id,
12213
+ standing,
12214
+ ...(extra.refreshPointer === undefined
12215
+ ? {}
12216
+ : { refreshPointer: extra.refreshPointer }),
12217
+ possibleMatches: extra.possibleMatches ?? [],
12218
+ candidate: item.candidate,
12219
+ record: item.record,
12220
+ }
12221
+ }
12222
+ function skippedOf(item, entry) {
12223
+ const title = item.candidate.title
12224
+ if (entry?.status === 'rejected') {
12225
+ return { id: item.id, title, reason: 'rejected-locally' }
12226
+ }
12227
+ if (entry?.status !== 'promoted') return
12228
+ return {
12229
+ id: item.id,
12230
+ title,
12231
+ reason: 'promoted-locally',
12232
+ ...(entry.destination === undefined
12233
+ ? {}
12234
+ : { refreshPointer: entry.destination }),
12235
+ }
12236
+ }
12237
+ function placeOne(item, ledger, heldIn) {
12238
+ const outcome = classifyCandidate(ledger, item.record)
12239
+ if (outcome.standing === 'new') {
12240
+ return {
12241
+ placed: freshOf(item, 'new', {
12242
+ possibleMatches: [
12243
+ ...new Set([
12244
+ ...outcome.possibleMatches,
12245
+ ...findRenamedEntries(ledger, item.record, item.id),
12246
+ ]),
12247
+ ],
12248
+ }),
12249
+ }
12250
+ }
12251
+ if (outcome.standing === 'reopened') {
12252
+ return {
12253
+ placed: freshOf(
12254
+ item,
12255
+ outcome.reason,
12256
+ outcome.refreshPointer === undefined
12257
+ ? {}
12258
+ : { refreshPointer: outcome.refreshPointer },
12259
+ ),
12260
+ }
12261
+ }
12262
+ const skipped = skippedOf(
12263
+ item,
12264
+ ledger.entries.find((known) => known.id === item.id),
12265
+ )
12266
+ if (skipped !== undefined) return { skipped }
12267
+ const held = heldIn(item)
12268
+ return {
12269
+ placed:
12270
+ held === undefined
12271
+ ? freshOf(item, 'new')
12272
+ : { kind: 'open', accepted: item, runRecordPath: held },
12273
+ }
12274
+ }
12275
+ function placeCandidates(accepted, ledger, root, runs) {
12276
+ let kept
12277
+ const heldIn = (item) => {
12278
+ kept ??= keptImportRuns(root, runs)
12279
+ const current = evidenceOf(item.candidate)
12280
+ const holder = kept.find((run) => run.evidence.get(item.id) === current)
12281
+ return holder === undefined
12282
+ ? undefined
12283
+ : path8.join(root, 'runs', holder.runId, 'run.json')
12284
+ }
12285
+ const fresh = []
12286
+ const open = []
12287
+ const skipped = []
12288
+ const order = []
12289
+ for (const item of accepted) {
12290
+ const result = placeOne(item, ledger, heldIn)
12291
+ if ('skipped' in result) {
12292
+ skipped.push(result.skipped)
12293
+ continue
12294
+ }
12295
+ order.push(result.placed)
12296
+ if (result.placed.kind === 'fresh') fresh.push(result.placed)
12297
+ else open.push(result.placed)
12298
+ }
12299
+ return { fresh, open, skipped, order }
12300
+ }
12301
+ function countByReason(refused) {
12302
+ const counts = {}
12303
+ for (const item of refused) count2(counts, item.reason)
12304
+ return counts
12305
+ }
12306
+ function importRunRecord(model, createdAt, verified, placement) {
12307
+ return {
12308
+ schemaVersion: 1,
12309
+ runId: model.runId,
12310
+ createdAt,
12311
+ kind: 'import',
12312
+ outcome: model.outcome,
12313
+ degradeReasons: model.degradeReasons,
12314
+ provenance: model.provenance,
12315
+ source: { runId: model.sourceRunId },
12316
+ projects: verified.projects,
12317
+ counts: {
12318
+ candidates: placement.fresh.length,
12319
+ alreadyImported: placement.open.length,
12320
+ skipped: placement.skipped.length,
12321
+ refused: model.refused.length,
12322
+ },
12323
+ refused: model.refused,
12324
+ skipped: placement.skipped,
12325
+ candidateBlock: buildCandidateBlock(placement.fresh),
12326
+ }
12327
+ }
12328
+ function importModel(
12329
+ parsed,
12330
+ sourceRunId,
12331
+ verified,
12332
+ placement,
12333
+ runId,
12334
+ newRecordPath,
12335
+ ) {
12336
+ const candidates = placement.order.map((entry) =>
12337
+ entry.kind === 'fresh'
12338
+ ? { item: entry, runRecordPath: newRecordPath }
12339
+ : {
12340
+ item: {
12341
+ id: entry.accepted.id,
12342
+ standing: 'new',
12343
+ possibleMatches: [],
12344
+ candidate: entry.accepted.candidate,
12345
+ },
12346
+ runRecordPath: entry.runRecordPath,
12347
+ },
12348
+ )
12349
+ return {
12350
+ runId,
12351
+ outcome: verified.refused.length > 0 ? 'degraded' : 'complete',
12352
+ degradeReasons: verified.refused.length > 0 ? ['candidates-refused'] : [],
12353
+ provenance: parsed.provenance,
12354
+ sourceRunId,
12355
+ projects: verified.projects,
12356
+ imported: placement.fresh.length,
12357
+ candidates,
12358
+ skipped: placement.skipped,
12359
+ refused: verified.refused,
12360
+ }
12361
+ }
12362
+ function importAnswer(model, verified, written) {
12363
+ const runRecordPath =
12364
+ written === undefined ? undefined : path8.join(written.runDir, 'run.json')
12365
+ const digestPath =
12366
+ written === undefined ? undefined : path8.join(written.runDir, 'digest.md')
12367
+ return success('import', {
12368
+ outcome: model.outcome,
12369
+ finalLine: `Connect ${model.outcome}`,
12370
+ ...(written === undefined || model.runId === undefined
12371
+ ? {}
12372
+ : {
12373
+ runId: model.runId,
12374
+ runDir: written.runDir,
12375
+ runRecordPath,
12376
+ digestPath,
12377
+ }),
12378
+ imported: model.imported,
12379
+ alreadyImported: model.candidates.length - model.imported,
12380
+ candidates: model.candidates.map(({ item, runRecordPath: held }) => ({
12381
+ id: item.id,
12382
+ kind: item.candidate.kind,
12383
+ title: item.candidate.title,
12384
+ standing: item.standing,
12385
+ runRecordPath: held,
12386
+ })),
12387
+ skipped: model.skipped,
12388
+ refused: model.refused,
12389
+ refusedBy: countByReason(model.refused),
12390
+ projects: verified.projects,
12391
+ provenance: model.provenance,
12392
+ degradeReasons: model.degradeReasons,
12393
+ summary: renderImportSummary(model, digestPath),
12394
+ })
12395
+ }
12396
+ function insertFailure(inserted) {
12397
+ if (inserted.status === 'busy') {
12398
+ return {
12399
+ ok: false,
12400
+ body: {
12401
+ ok: false,
12402
+ op: 'import',
12403
+ outcome: 'busy',
12404
+ reason: inserted.reason,
12405
+ finalLine: 'Connect busy',
12406
+ hint: 'Another run holds the lock. If no run is active, `ledger unlock` clears it.',
12407
+ },
12408
+ }
12409
+ }
12410
+ if (inserted.status === 'failed') {
12411
+ return importFailure(
12412
+ inserted.reason === 'lock-failed' ||
12413
+ inserted.reason === 'ledger-unreadable'
12414
+ ? inserted.reason
12415
+ : `publish-${inserted.reason}`,
12416
+ )
12417
+ }
12418
+ if (inserted.status === 'published' && !inserted.ledger.written) {
12419
+ return importFailure('ledger-write-failed', {
12420
+ runDir: inserted.runDir,
12421
+ ...(inserted.runRecordCurrent === false
12422
+ ? { runRecordCurrent: false }
12423
+ : {}),
12424
+ })
12425
+ }
12426
+ return
12427
+ }
12428
+ function importWrite(parsed, sourceRunId, verified, root, deps) {
12429
+ const now = deps.now ?? Date.now
12430
+ let placement = NO_PLACEMENT
12431
+ let localRunId
12432
+ const inserted = insertImportRun(
12433
+ root,
12434
+ (view) => {
12435
+ placement = placeCandidates(
12436
+ verified.accepted,
12437
+ view.ledger,
12438
+ root,
12439
+ view.importRuns,
12440
+ )
12441
+ if (placement.fresh.length === 0) return { publish: false }
12442
+ const runId = newImportRunId(now())
12443
+ localRunId = runId
12444
+ const createdAt = new Date(now()).toISOString()
12445
+ const model = importModel(
12446
+ parsed,
12447
+ sourceRunId,
12448
+ verified,
12449
+ placement,
12450
+ runId,
12451
+ path8.join(root, 'runs', runId, 'run.json'),
12452
+ )
12453
+ const filesOf = (shown) => ({
12454
+ 'digest.md': renderImportDigest(shown, placement.fresh, createdAt),
12455
+ 'run.json': `${JSON.stringify(importRunRecord(shown, createdAt, verified, placement), null, 2)}
12456
+ `,
12457
+ })
12458
+ return {
12459
+ publish: true,
12460
+ runId,
12461
+ files: filesOf(model),
12462
+ candidates: placement.fresh.map((item) => item.record),
12463
+ onLedgerWriteFailed: () =>
12464
+ filesOf({
12465
+ ...model,
12466
+ outcome: 'degraded',
12467
+ degradeReasons: [...model.degradeReasons, 'ledger-write-failed'],
12468
+ }),
12469
+ }
12470
+ },
12471
+ {
12472
+ now,
12473
+ ...(deps.processes === undefined ? {} : { processes: deps.processes }),
12474
+ ...(deps.publishHooks === undefined ? {} : { hooks: deps.publishHooks }),
12475
+ },
12476
+ )
12477
+ const failed = insertFailure(inserted)
12478
+ if (failed !== undefined) return failed
12479
+ const written =
12480
+ inserted.status === 'published' ? { runDir: inserted.runDir } : undefined
12481
+ return importAnswer(
12482
+ importModel(
12483
+ parsed,
12484
+ sourceRunId,
12485
+ verified,
12486
+ placement,
12487
+ localRunId,
12488
+ written === undefined ? '' : path8.join(written.runDir, 'run.json'),
12489
+ ),
12490
+ verified,
12491
+ written,
12492
+ )
12493
+ }
12494
+ function importRun(request, deps) {
12495
+ const parsed = parseImportRequest(request)
12496
+ if (parsed === undefined) return importFailure('invalid-request')
12497
+ const read = readImportRecord(parsed.directory)
12498
+ if (read.status === 'failed') return importFailure(read.reason)
12499
+ const environment = describeEnvironment(deps)
12500
+ if (environment.resolved.resolved.length === 0) {
12501
+ return importFailure('no-allowed-projects', {
12502
+ unmatched: environment.resolved.unmatched,
12503
+ configWarnings: environment.warnings,
12504
+ })
12505
+ }
12506
+ const verified = verifyRecord({
12507
+ record: read.record,
12508
+ locals: importLocals(environment),
12509
+ factsFor: importFacts(deps),
12510
+ })
12511
+ const sourceRunId = read.record.runId
12512
+ if (verified.accepted.length === 0) {
12513
+ return importAnswer(
12514
+ importModel(parsed, sourceRunId, verified, NO_PLACEMENT, undefined, ''),
12515
+ verified,
12516
+ undefined,
12517
+ )
12518
+ }
12519
+ const state = prepareStateRoot(environment.stateRoot, {
12520
+ allowedRoots: environment.allowedRoots,
12521
+ })
12522
+ if (state.status === 'refused') {
12523
+ return importFailure('state-root-refused', { detail: state.reason })
12524
+ }
12525
+ return importWrite(parsed, sourceRunId, verified, state.root, deps)
12526
+ }
12527
+ var ISSUE_DIGITS = /^[0-9]{1,10}$/
12528
+ function parseTicksRequest(request) {
12529
+ if (!isRecord6(request)) return
12530
+ const { bodyFile, issue } = request
12531
+ if (
12532
+ typeof bodyFile !== 'string' ||
12533
+ bodyFile === '' ||
12534
+ bodyFile.length > MAX_DIRECTORY_LENGTH ||
12535
+ CONTROL_IN_PATH.test(bodyFile) ||
12536
+ !path8.isAbsolute(bodyFile)
12537
+ ) {
12538
+ return
12539
+ }
12540
+ const number =
12541
+ typeof issue === 'string' && ISSUE_DIGITS.test(issue)
12542
+ ? Number(issue)
12543
+ : issue
12544
+ if (typeof number !== 'number' || !Number.isSafeInteger(number)) {
12545
+ return
12546
+ }
12547
+ return number < 1 ? undefined : { bodyFile, issue: number }
12548
+ }
12549
+ function parseRenderRequest(request) {
12550
+ if (!isRecord6(request)) return
12551
+ const { date, runId, attempt, cacheRestored } = request
12552
+ if (
12553
+ typeof date !== 'string' ||
12554
+ typeof runId !== 'string' ||
12555
+ typeof attempt !== 'string' ||
12556
+ typeof cacheRestored !== 'boolean'
12557
+ ) {
12558
+ return
12559
+ }
12560
+ return { date, runId, attempt, cacheRestored }
12561
+ }
12562
+ function ciOptions(deps) {
12563
+ return {
12564
+ ...(deps.processes === undefined ? {} : { processes: deps.processes }),
12565
+ ...(deps.now === undefined ? {} : { now: deps.now }),
12566
+ }
12567
+ }
12568
+ function busyAnswer(op, reason, extra = {}) {
12569
+ return {
12570
+ ok: false,
12571
+ body: { ok: false, op, outcome: 'busy', reason, ...extra },
12572
+ }
12573
+ }
12574
+ function applyTicksOp(request, deps) {
12575
+ const parsed = parseTicksRequest(request)
12576
+ if (parsed === undefined) return failure2('apply-ticks', 'invalid-request')
12577
+ const read = readReportBody(parsed.bodyFile)
12578
+ if (read.status === 'failed') return failure2('apply-ticks', read.reason)
12579
+ const prepared = prepare('apply-ticks', deps)
12580
+ if (!prepared.ok) return prepared.result
12581
+ const result = applyTicks(
12582
+ prepared.root,
12583
+ { body: read.body, issue: parsed.issue },
12584
+ ciOptions(deps),
12585
+ )
12586
+ if (result.status === 'busy') {
12587
+ return busyAnswer('apply-ticks', result.reason, {
12588
+ accepted: result.accepted,
12589
+ })
12590
+ }
12591
+ if (result.status === 'failed') {
12592
+ return failure2('apply-ticks', result.reason, { accepted: result.accepted })
12593
+ }
12594
+ return success('apply-ticks', {
12595
+ report: parsed.issue,
12596
+ section: result.section,
12597
+ accepted: result.accepted,
12598
+ skipped: result.skipped,
12599
+ unresolved: result.unresolved,
12600
+ })
12601
+ }
12602
+ function renderReportOp(request, deps) {
12603
+ const parsed = parseRenderRequest(request)
12604
+ if (parsed === undefined) return failure2('render-report', 'invalid-request')
12605
+ const prepared = prepare('render-report', deps)
12606
+ if (!prepared.ok) return prepared.result
12607
+ const result = renderReport(prepared.root, parsed, ciOptions(deps))
12608
+ if (result.status === 'busy')
12609
+ return busyAnswer('render-report', result.reason)
12610
+ if (result.status === 'failed') {
12611
+ return failure2('render-report', result.reason)
12612
+ }
12613
+ return success('render-report', {
12614
+ bodyFile: result.bodyFile,
12615
+ bodyChars: result.body.length,
12616
+ manifest: result.manifest,
12617
+ open: result.open,
12618
+ shown: result.shown,
12619
+ hidden: result.hidden,
12620
+ fresh: result.fresh,
12621
+ runs: result.runs,
12622
+ outcome: result.outcome,
12623
+ red: result.red,
12624
+ stateUpdated: result.stateUpdated,
12625
+ })
12626
+ }
9664
12627
  function runHelperOp(op, request, deps = {}) {
9665
12628
  if (!isOp(op)) return failure2(op, 'unknown-op')
9666
12629
  try {
@@ -9673,6 +12636,12 @@ function runHelperOp(op, request, deps = {}) {
9673
12636
  return publish(request, deps)
9674
12637
  case 'decide':
9675
12638
  return decide(request, deps)
12639
+ case 'import':
12640
+ return importRun(request, deps)
12641
+ case 'apply-ticks':
12642
+ return applyTicksOp(request, deps)
12643
+ case 'render-report':
12644
+ return renderReportOp(request, deps)
9676
12645
  case 'ledger-show':
9677
12646
  return ledgerShow(request, deps)
9678
12647
  case 'ledger-clear':
@@ -9693,7 +12662,7 @@ function errorCodeOf(error) {
9693
12662
  }
9694
12663
  function readStdinChunk(chunk) {
9695
12664
  try {
9696
- return fs8.readSync(0, chunk, 0, chunk.length, null)
12665
+ return fs10.readSync(0, chunk, 0, chunk.length, null)
9697
12666
  } catch (error) {
9698
12667
  const code = errorCodeOf(error)
9699
12668
  if (code === 'EOF') return 0
@@ -9717,7 +12686,15 @@ function readStdinBounded() {
9717
12686
  }
9718
12687
  return Buffer.concat(chunks).toString('utf8')
9719
12688
  }
9720
- var REQUEST_OPS = new Set(['begin', 'validate', 'publish', 'decide'])
12689
+ var REQUEST_OPS = new Set([
12690
+ 'begin',
12691
+ 'validate',
12692
+ 'publish',
12693
+ 'decide',
12694
+ 'import',
12695
+ 'apply-ticks',
12696
+ 'render-report',
12697
+ ])
9721
12698
  function resolveCliOp(argv) {
9722
12699
  const [first, second] = argv
9723
12700
  if (first === 'ledger') {
@@ -9737,24 +12714,35 @@ function showRequestFromArgv(argv) {
9737
12714
  ? { offset: Number(value) }
9738
12715
  : { offset: value }
9739
12716
  }
12717
+ function refusedBegin(result, options) {
12718
+ const deps = options.deps ?? {}
12719
+ const marker = ciRunMarker(deps)
12720
+ return marker === undefined
12721
+ ? result
12722
+ : endInvocation(marker, result, endOfFailure(result), deps)
12723
+ }
9740
12724
  function readCliRequest(op, options, out, err) {
9741
12725
  if (!REQUEST_OPS.has(op)) return { ok: true, request: {} }
9742
12726
  if ((options.isTTY ?? process.stdin.isTTY) === true) {
9743
12727
  err(
9744
12728
  `${op} reads one JSON request from stdin; interactive input is not supported`,
9745
12729
  )
12730
+ if (op === 'begin') {
12731
+ refusedBegin(failure2(op, 'request-interactive'), options)
12732
+ }
9746
12733
  return { ok: false, exitCode: 2 }
9747
12734
  }
9748
12735
  const text = (options.readRequest ?? readStdinBounded)()
9749
12736
  const refuse = (reason) => {
12737
+ const refused = failure2(
12738
+ op,
12739
+ reason,
12740
+ op === 'validate' ? { lockReleased: false } : {},
12741
+ )
9750
12742
  out(
9751
- JSON.stringify({
9752
- ok: false,
9753
- op,
9754
- outcome: 'failed',
9755
- reason,
9756
- ...(op === 'validate' ? { lockReleased: false } : {}),
9757
- }),
12743
+ JSON.stringify(
12744
+ (op === 'begin' ? refusedBegin(refused, options) : refused).body,
12745
+ ),
9758
12746
  )
9759
12747
  return { ok: false, exitCode: 2 }
9760
12748
  }