@fro.bot/systematic 3.32.3 → 3.33.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HARNESSES.md +16 -13
- package/dist/ce-connect-helper.d.ts +4 -2
- package/dist/cli.js +1 -1
- package/dist/{index-nrbdgp4m.js → index-1cj1mrpb.js} +34 -2
- package/dist/index.js +1 -1
- package/dist/lib/config-schema.d.ts +2 -0
- package/dist/lib/config.d.ts +3 -2
- package/dist/lib/connect-ci.d.ts +189 -0
- package/dist/lib/connect-config.d.ts +28 -8
- package/dist/lib/connect-helper-core.d.ts +110 -1
- package/dist/lib/connect-import.d.ts +169 -0
- package/dist/lib/connect-repo-identity.d.ts +33 -0
- package/dist/lib/connect-state.d.ts +138 -3
- package/dist/pi.js +34 -2
- package/package.json +8 -8
- package/skills/ce-connect/SKILL.md +28 -5
- package/skills/ce-connect/references/fetch-and-import.md +136 -0
- package/skills/ce-connect/references/headless-and-outcomes.md +42 -4
- package/skills/ce-connect/scripts/connect.mjs +3136 -144
|
@@ -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:
|
|
5
|
+
// source-digest: 1dff040830b364bc15183c7ac02704b38be2eeff00e226e50e74817dea36bf40
|
|
6
6
|
// src/ce-connect-helper.ts
|
|
7
7
|
import { spawnSync as spawnSync3 } from 'node:child_process'
|
|
8
|
-
import
|
|
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
|
|
1126
|
-
if (!
|
|
1127
|
-
|
|
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
|
|
1137
|
-
for (const entry of
|
|
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
|
|
1153
|
+
return
|
|
1144
1154
|
}
|
|
1145
|
-
|
|
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
|
-
|
|
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 (
|
|
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
|
-
? {
|
|
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
|
|
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 }),
|
|
@@ -1410,13 +1473,13 @@ var HOST_SUPPORT_TABLE = [
|
|
|
1410
1473
|
},
|
|
1411
1474
|
{
|
|
1412
1475
|
host: 'opencode-v2',
|
|
1413
|
-
version: '2.2.
|
|
1476
|
+
version: '2.2.7',
|
|
1414
1477
|
observedBy:
|
|
1415
1478
|
'tests/integration/opencode-v2.test.ts > a session-level allow does not lift the guarded researcher when a parent with that rule dispatches it',
|
|
1416
1479
|
},
|
|
1417
1480
|
{
|
|
1418
1481
|
host: 'pi',
|
|
1419
|
-
version: '1.5.
|
|
1482
|
+
version: '1.5.2',
|
|
1420
1483
|
observedBy:
|
|
1421
1484
|
'tests/unit/pi-delegate-session.test.ts > the shipped researcher reaches createAgentSession with read, grep and find and none of bash, write, edit or systematic_delegate',
|
|
1422
1485
|
},
|
|
@@ -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(
|
|
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
|
|
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' &&
|
|
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(
|
|
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)
|
|
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:
|
|
7847
|
+
if (coverage === undefined) return { version: LEDGER_VERSION, entries }
|
|
7602
7848
|
if (!isCoverage(coverage)) return
|
|
7603
7849
|
return coverage.length === 0
|
|
7604
|
-
? { version:
|
|
7605
|
-
: { version:
|
|
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:
|
|
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(
|
|
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:
|
|
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(
|
|
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 = [
|
|
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) => !
|
|
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(
|
|
8110
|
+
const gone = new Set(stale.map(coverageKey))
|
|
7846
8111
|
return records
|
|
7847
|
-
.filter((record) => !gone.has(record
|
|
7848
|
-
.sort(
|
|
8112
|
+
.filter((record) => !gone.has(coverageKey(record)))
|
|
8113
|
+
.sort(byRule)
|
|
7849
8114
|
}
|
|
7850
|
-
return records.sort(
|
|
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:
|
|
7867
|
-
: { version:
|
|
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,1741 @@ function clearLedger(root, options = {}) {
|
|
|
8069
8554
|
}
|
|
8070
8555
|
}
|
|
8071
8556
|
|
|
8072
|
-
// src/lib/
|
|
8073
|
-
|
|
8074
|
-
|
|
8075
|
-
|
|
8076
|
-
|
|
8077
|
-
|
|
8078
|
-
|
|
8079
|
-
|
|
8080
|
-
|
|
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
|
|
8084
|
-
|
|
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
|
|
8091
|
-
|
|
8092
|
-
|
|
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
|
-
|
|
8095
|
-
modulePath !== undefined &&
|
|
8096
|
-
entryPath === modulePath
|
|
8599
|
+
!Number.isNaN(parsed.getTime()) && parsed.toISOString().startsWith(text)
|
|
8097
8600
|
)
|
|
8098
8601
|
}
|
|
8099
|
-
|
|
8100
|
-
|
|
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
|
|
8116
|
-
|
|
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 fetchWord(payload) {
|
|
9044
|
+
return `${payload.runId}.${payload.attempt}`
|
|
9045
|
+
}
|
|
9046
|
+
function blockOf(position, id, payload, isNew) {
|
|
9047
|
+
const lines = [`### Candidate ${position}${isNew ? ' (new)' : ''}`, '']
|
|
9048
|
+
if (payload === undefined) {
|
|
9049
|
+
lines.push(`Details unavailable for \`${id}\`.`)
|
|
9050
|
+
} else {
|
|
9051
|
+
lines.push(
|
|
9052
|
+
`- Fetch: \`/ce:connect fetch:${fetchWord(payload)}\` (run ${payload.runId}, attempt ${payload.attempt})`,
|
|
9053
|
+
'',
|
|
9054
|
+
fenceText(factsOf(id, payload)),
|
|
9055
|
+
)
|
|
9056
|
+
}
|
|
9057
|
+
lines.push(
|
|
9058
|
+
'',
|
|
9059
|
+
decisionLine(id, 'promoted', false),
|
|
9060
|
+
decisionLine(id, 'rejected', false),
|
|
9061
|
+
'',
|
|
9062
|
+
)
|
|
9063
|
+
return lines.join(`
|
|
9064
|
+
`)
|
|
9065
|
+
}
|
|
9066
|
+
function ranges(positions) {
|
|
9067
|
+
const spans = []
|
|
9068
|
+
for (const position of positions) {
|
|
9069
|
+
const last = spans.at(-1)
|
|
9070
|
+
if (last !== undefined && position === last[1] + 1) last[1] = position
|
|
9071
|
+
else spans.push([position, position])
|
|
9072
|
+
}
|
|
9073
|
+
return spans
|
|
9074
|
+
.map(([from, to]) => (from === to ? `${from}` : `${from}-${to}`))
|
|
9075
|
+
.join(', ')
|
|
9076
|
+
}
|
|
9077
|
+
function fetchLines(rows) {
|
|
9078
|
+
const byFetch = new Map()
|
|
9079
|
+
rows.forEach((row, index) => {
|
|
9080
|
+
if (row.payload === undefined) return
|
|
9081
|
+
const word = fetchWord(row.payload)
|
|
9082
|
+
const list = byFetch.get(word) ?? []
|
|
9083
|
+
list.push(index + 1)
|
|
9084
|
+
byFetch.set(word, list)
|
|
9085
|
+
})
|
|
9086
|
+
const lines = [...byFetch].map(
|
|
9087
|
+
([word, positions]) =>
|
|
9088
|
+
`- \`/ce:connect fetch:${word}\` for candidates ${ranges(positions)}`,
|
|
9089
|
+
)
|
|
9090
|
+
return { fetches: [...byFetch.keys()], lines }
|
|
9091
|
+
}
|
|
9092
|
+
var RESET_NOTICE =
|
|
9093
|
+
'> **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.'
|
|
9094
|
+
var STATE_NOT_UPDATED_NOTICE =
|
|
9095
|
+
'> **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.'
|
|
9096
|
+
var HOW_TO_TICK =
|
|
9097
|
+
'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.'
|
|
9098
|
+
function headOf(input) {
|
|
9099
|
+
const lines = ['## ce:connect report', '']
|
|
9100
|
+
lines.push(`**Outcome:** ${OUTCOME_SENTENCE[input.outcome]}`, '')
|
|
9101
|
+
if (input.facts.length > 0)
|
|
9102
|
+
lines.push(
|
|
9103
|
+
fenceText(
|
|
9104
|
+
input.facts.join(`
|
|
9105
|
+
`),
|
|
9106
|
+
),
|
|
9107
|
+
'',
|
|
9108
|
+
)
|
|
9109
|
+
if (!input.cacheRestored) lines.push(RESET_NOTICE, '')
|
|
9110
|
+
if (!input.stateUpdated) lines.push(STATE_NOT_UPDATED_NOTICE, '')
|
|
9111
|
+
return lines
|
|
9112
|
+
}
|
|
9113
|
+
function hiddenLine(hidden) {
|
|
9114
|
+
const noun = hidden === 1 ? 'candidate is' : 'candidates are'
|
|
9115
|
+
return `**${hidden} open ${noun} not shown.** They stay open and appear here, oldest first, as the ones above are settled.`
|
|
9116
|
+
}
|
|
9117
|
+
function assemble(input, shown) {
|
|
9118
|
+
const rows = input.rows.slice(0, shown)
|
|
9119
|
+
const ids = rows.map((row) => row.id)
|
|
9120
|
+
const hash = manifestHash(ids)
|
|
9121
|
+
const fetch = fetchLines(rows)
|
|
9122
|
+
const fresh = rows.filter((row) => row.isNew).length
|
|
9123
|
+
const open = input.rows.length
|
|
9124
|
+
const out = [manifestMarker(hash), '', ...headOf(input)]
|
|
9125
|
+
if (fetch.lines.length > 0) {
|
|
9126
|
+
out.push(
|
|
9127
|
+
'## Fetch',
|
|
9128
|
+
'',
|
|
9129
|
+
'Run one of these in a local checkout to bring the evidence in before you promote:',
|
|
9130
|
+
...fetch.lines,
|
|
9131
|
+
'',
|
|
9132
|
+
)
|
|
9133
|
+
}
|
|
9134
|
+
out.push('## Open candidates', '')
|
|
9135
|
+
out.push(
|
|
9136
|
+
`${open} open, ${shown} listed${fresh > 0 ? `, ${fresh} new` : ''}.`,
|
|
9137
|
+
'',
|
|
9138
|
+
HOW_TO_TICK,
|
|
9139
|
+
'',
|
|
9140
|
+
DECISIONS_BEGIN,
|
|
9141
|
+
'',
|
|
9142
|
+
)
|
|
9143
|
+
for (const row of rows) out.push(row.block)
|
|
9144
|
+
out.push(DECISIONS_END, '')
|
|
9145
|
+
if (shown < open) out.push(hiddenLine(open - shown), '')
|
|
9146
|
+
return {
|
|
9147
|
+
body: out.join(`
|
|
9148
|
+
`),
|
|
9149
|
+
ids,
|
|
9150
|
+
hash,
|
|
9151
|
+
fetches: fetch.fetches,
|
|
9152
|
+
fresh,
|
|
9153
|
+
}
|
|
9154
|
+
}
|
|
9155
|
+
function fit(input) {
|
|
9156
|
+
const total = input.rows.length
|
|
9157
|
+
if (assemble(input, total).body.length <= REPORT_MAX_CHARS) return total
|
|
9158
|
+
let low = 0
|
|
9159
|
+
let high = total - 1
|
|
9160
|
+
while (low < high) {
|
|
9161
|
+
const middle = Math.ceil((low + high) / 2)
|
|
9162
|
+
if (assemble(input, middle).body.length <= REPORT_MAX_CHARS) low = middle
|
|
9163
|
+
else high = middle - 1
|
|
9164
|
+
}
|
|
9165
|
+
return low
|
|
9166
|
+
}
|
|
9167
|
+
function openOldestFirst(entries) {
|
|
9168
|
+
return entries
|
|
9169
|
+
.filter((entry) => entry.status === 'candidate')
|
|
9170
|
+
.sort((a, b) => {
|
|
9171
|
+
if (a.recordedAt !== b.recordedAt)
|
|
9172
|
+
return a.recordedAt < b.recordedAt ? -1 : 1
|
|
9173
|
+
return a.id < b.id ? -1 : 1
|
|
9174
|
+
})
|
|
9175
|
+
}
|
|
9176
|
+
function proposed(run, open, request) {
|
|
9177
|
+
const out = new Map()
|
|
9178
|
+
for (const raw of run?.candidates ?? []) {
|
|
9179
|
+
if (!isRecord4(raw) || !isCandidateId(raw.id) || !open.has(raw.id)) continue
|
|
9180
|
+
const text = payloadText(raw)
|
|
9181
|
+
if (text === undefined) continue
|
|
9182
|
+
const target = typeof raw.target === 'string' ? raw.target : undefined
|
|
9183
|
+
const repo = target === undefined ? undefined : run?.repoByRoot.get(target)
|
|
9184
|
+
out.set(raw.id, {
|
|
9185
|
+
id: raw.id,
|
|
9186
|
+
...text,
|
|
9187
|
+
...(repo === undefined ? {} : { repo }),
|
|
9188
|
+
runId: request.runId,
|
|
9189
|
+
attempt: request.attempt,
|
|
9190
|
+
})
|
|
9191
|
+
}
|
|
9192
|
+
return out
|
|
9193
|
+
}
|
|
9194
|
+
function mergedPayloads(open, stored, fresh) {
|
|
9195
|
+
const out = new Map()
|
|
9196
|
+
for (const entry of open) {
|
|
9197
|
+
const earlier = stored.get(entry.id)
|
|
9198
|
+
const current = fresh.get(entry.id)
|
|
9199
|
+
const merged =
|
|
9200
|
+
current === undefined
|
|
9201
|
+
? earlier
|
|
9202
|
+
: {
|
|
9203
|
+
...current,
|
|
9204
|
+
...(earlier?.firstShown === undefined
|
|
9205
|
+
? {}
|
|
9206
|
+
: { firstShown: earlier.firstShown }),
|
|
9207
|
+
}
|
|
9208
|
+
if (merged !== undefined) out.set(entry.id, merged)
|
|
9209
|
+
}
|
|
9210
|
+
return out
|
|
9211
|
+
}
|
|
9212
|
+
function factLines(invocation, run) {
|
|
9213
|
+
const lines = []
|
|
9214
|
+
if (run?.selection !== undefined) lines.push(`Selection: ${run.selection}`)
|
|
9215
|
+
if (run?.sweep !== undefined) lines.push(run.sweep)
|
|
9216
|
+
const reason = invocation.phase === 'terminal' ? invocation.reason : undefined
|
|
9217
|
+
if (reason !== undefined) lines.push(`Reason: ${reason}`)
|
|
9218
|
+
if (run !== undefined && run.degradeReasons.length > 0) {
|
|
9219
|
+
lines.push('Degrade reasons:', ...run.degradeReasons.map((r) => `- ${r}`))
|
|
9220
|
+
}
|
|
9221
|
+
return lines
|
|
9222
|
+
}
|
|
9223
|
+
function rowsOf(open, payloads, date) {
|
|
9224
|
+
return open.map((entry, index) => {
|
|
9225
|
+
const payload = payloads.get(entry.id)
|
|
9226
|
+
const isNew = payload !== undefined && (payload.firstShown ?? date) === date
|
|
9227
|
+
return {
|
|
9228
|
+
id: entry.id,
|
|
9229
|
+
payload,
|
|
9230
|
+
isNew,
|
|
9231
|
+
block: blockOf(index + 1, entry.id, payload, isNew),
|
|
9232
|
+
}
|
|
9233
|
+
})
|
|
9234
|
+
}
|
|
9235
|
+
function loadStores(root, recordState) {
|
|
9236
|
+
const payloads = readPayloadStore(root)
|
|
9237
|
+
if (payloads.status !== 'ok' && recordState) {
|
|
9238
|
+
return { status: 'failed', reason: 'payload-store-unreadable' }
|
|
9239
|
+
}
|
|
9240
|
+
const manifests = recordState ? readManifestStore(root) : undefined
|
|
9241
|
+
if (manifests !== undefined && manifests.status !== 'ok') {
|
|
9242
|
+
return { status: 'failed', reason: 'manifest-store-unreadable' }
|
|
9243
|
+
}
|
|
9244
|
+
return {
|
|
9245
|
+
status: 'ok',
|
|
9246
|
+
stores: {
|
|
9247
|
+
payloads: payloads.status === 'ok' ? payloads.items : new Map(),
|
|
9248
|
+
manifests: manifests?.status === 'ok' ? manifests.items : undefined,
|
|
9249
|
+
},
|
|
9250
|
+
}
|
|
9251
|
+
}
|
|
9252
|
+
function renderState(root, request, recordState) {
|
|
9253
|
+
const read = readLedger(root)
|
|
9254
|
+
if (read.status !== 'ok')
|
|
9255
|
+
return { status: 'failed', reason: 'ledger-unreadable' }
|
|
9256
|
+
const invocation = readInvocationRecord(root)
|
|
9257
|
+
if (invocation?.marker !== `${request.runId}-${request.attempt}`) {
|
|
9258
|
+
return { status: 'failed', reason: 'no-invocation' }
|
|
9259
|
+
}
|
|
9260
|
+
const run =
|
|
9261
|
+
invocation.runId === undefined
|
|
9262
|
+
? undefined
|
|
9263
|
+
: readRunSummary(root, invocation.runId)
|
|
9264
|
+
const loaded = loadStores(root, recordState)
|
|
9265
|
+
if (loaded.status === 'failed') return loaded
|
|
9266
|
+
const open = openOldestFirst(read.ledger.entries)
|
|
9267
|
+
const payloads = mergedPayloads(
|
|
9268
|
+
open,
|
|
9269
|
+
loaded.stores.payloads,
|
|
9270
|
+
proposed(run, new Set(open.map((entry) => entry.id)), request),
|
|
9271
|
+
)
|
|
9272
|
+
const outcome = outcomeOf(invocation)
|
|
9273
|
+
const input = {
|
|
9274
|
+
outcome,
|
|
9275
|
+
facts: factLines(invocation, run),
|
|
9276
|
+
cacheRestored: request.cacheRestored,
|
|
9277
|
+
stateUpdated: recordState,
|
|
9278
|
+
rows: rowsOf(open, payloads, request.date),
|
|
9279
|
+
}
|
|
9280
|
+
const shown = fit(input)
|
|
9281
|
+
const result = assemble(input, shown)
|
|
9282
|
+
for (const id of result.ids) {
|
|
9283
|
+
const payload = payloads.get(id)
|
|
9284
|
+
if (payload !== undefined && payload.firstShown === undefined) {
|
|
9285
|
+
payloads.set(id, { ...payload, firstShown: request.date })
|
|
9286
|
+
}
|
|
9287
|
+
}
|
|
9288
|
+
const storeText = payloadStoreText([...payloads.values()])
|
|
9289
|
+
if (recordState && Buffer.byteLength(storeText) > MAX_PAYLOAD_BYTES) {
|
|
9290
|
+
return { status: 'failed', reason: 'payload-store-too-large' }
|
|
9291
|
+
}
|
|
9292
|
+
const bodyFile = path6.join(root, REPORT_FILE)
|
|
9293
|
+
try {
|
|
9294
|
+
if (loaded.stores.manifests !== undefined) {
|
|
9295
|
+
writeFileAtomic2(path6.join(root, PAYLOAD_FILE), storeText)
|
|
9296
|
+
writeManifestStore(
|
|
9297
|
+
root,
|
|
9298
|
+
withManifest(loaded.stores.manifests, {
|
|
9299
|
+
hash: result.hash,
|
|
9300
|
+
ids: result.ids,
|
|
9301
|
+
}),
|
|
9302
|
+
)
|
|
9303
|
+
}
|
|
9304
|
+
writeFileAtomic2(bodyFile, result.body)
|
|
9305
|
+
} catch {
|
|
9306
|
+
return { status: 'failed', reason: 'write-failed' }
|
|
9307
|
+
}
|
|
9308
|
+
return {
|
|
9309
|
+
status: 'rendered',
|
|
9310
|
+
body: result.body,
|
|
9311
|
+
bodyFile,
|
|
9312
|
+
manifest: result.hash,
|
|
9313
|
+
open: open.length,
|
|
9314
|
+
shown,
|
|
9315
|
+
hidden: open.length - shown,
|
|
9316
|
+
fresh: result.fresh,
|
|
9317
|
+
fetches: result.fetches,
|
|
9318
|
+
outcome,
|
|
9319
|
+
red: isRed(outcome),
|
|
9320
|
+
stateUpdated: recordState,
|
|
9321
|
+
}
|
|
9322
|
+
}
|
|
9323
|
+
function renderBusyRun(root, request) {
|
|
9324
|
+
const invocation = readInvocationRecord(root)
|
|
9325
|
+
if (
|
|
9326
|
+
invocation?.marker !== `${request.runId}-${request.attempt}` ||
|
|
9327
|
+
outcomeOf(invocation) !== 'busy'
|
|
9328
|
+
) {
|
|
9329
|
+
return
|
|
9330
|
+
}
|
|
9331
|
+
try {
|
|
9332
|
+
return renderState(root, request, false)
|
|
9333
|
+
} catch {
|
|
9334
|
+
return { status: 'failed', reason: 'write-failed' }
|
|
9335
|
+
}
|
|
9336
|
+
}
|
|
9337
|
+
function renderReport(root, request, options = {}) {
|
|
9338
|
+
if (!validRequest(request))
|
|
9339
|
+
return { status: 'failed', reason: 'invalid-request' }
|
|
9340
|
+
const acquired = acquireLock(root, lockOptions(options))
|
|
9341
|
+
if (acquired.status === 'busy') {
|
|
9342
|
+
return (
|
|
9343
|
+
renderBusyRun(root, request) ?? {
|
|
9344
|
+
status: 'busy',
|
|
9345
|
+
reason: acquired.reason,
|
|
9346
|
+
}
|
|
9347
|
+
)
|
|
9348
|
+
}
|
|
9349
|
+
if (acquired.status === 'failed') {
|
|
9350
|
+
return { status: 'failed', reason: 'lock-failed' }
|
|
9351
|
+
}
|
|
9352
|
+
try {
|
|
9353
|
+
return renderState(root, request, true)
|
|
9354
|
+
} catch {
|
|
9355
|
+
return { status: 'failed', reason: 'write-failed' }
|
|
9356
|
+
} finally {
|
|
9357
|
+
releaseLock(acquired.lock)
|
|
9358
|
+
}
|
|
9359
|
+
}
|
|
9360
|
+
function tickedBoxes(boxes) {
|
|
9361
|
+
const seen = new Set()
|
|
9362
|
+
const out = []
|
|
9363
|
+
for (const box of boxes) {
|
|
9364
|
+
const key = `${box.id}:${box.status}`
|
|
9365
|
+
if (!box.ticked || seen.has(key)) continue
|
|
9366
|
+
seen.add(key)
|
|
9367
|
+
out.push({ id: box.id, status: box.status })
|
|
9368
|
+
}
|
|
9369
|
+
return out
|
|
9370
|
+
}
|
|
9371
|
+
function emptyApplied(section) {
|
|
9372
|
+
return {
|
|
9373
|
+
status: 'applied',
|
|
9374
|
+
section,
|
|
9375
|
+
accepted: [],
|
|
9376
|
+
skipped: [],
|
|
9377
|
+
unresolved: [],
|
|
9378
|
+
}
|
|
9379
|
+
}
|
|
9380
|
+
function groupById(ticks) {
|
|
9381
|
+
const byId = new Map()
|
|
9382
|
+
for (const tick of ticks) {
|
|
9383
|
+
byId.set(tick.id, [...(byId.get(tick.id) ?? []), tick.status])
|
|
9384
|
+
}
|
|
9385
|
+
return byId
|
|
9386
|
+
}
|
|
9387
|
+
function judge(id, statuses, manifest, entry, reason) {
|
|
9388
|
+
const [status] = statuses
|
|
9389
|
+
if (status === undefined) return
|
|
9390
|
+
const skip = (why) => ({
|
|
9391
|
+
kind: 'skip',
|
|
9392
|
+
skipped: { id, status, reason: why },
|
|
9393
|
+
})
|
|
9394
|
+
if (statuses.length > 1) {
|
|
9395
|
+
return { kind: 'skip', skipped: { id, reason: 'conflict' } }
|
|
9396
|
+
}
|
|
9397
|
+
if (!manifest.ids.includes(id)) return skip('not-in-manifest')
|
|
9398
|
+
if (entry === undefined) {
|
|
9399
|
+
return {
|
|
9400
|
+
kind: 'unresolved',
|
|
9401
|
+
unresolved: { id, status, reason: 'unknown-candidate' },
|
|
9402
|
+
}
|
|
9403
|
+
}
|
|
9404
|
+
if (entry.status === status && entry.reason === reason) {
|
|
9405
|
+
return skip('already-recorded')
|
|
9406
|
+
}
|
|
9407
|
+
if (entry.status !== 'candidate') return skip('not-open')
|
|
9408
|
+
return { kind: 'record', tick: { id, status } }
|
|
9409
|
+
}
|
|
9410
|
+
function triage(ticks, manifest, entries, reason) {
|
|
9411
|
+
const out = { record: [], skipped: [], unresolved: [] }
|
|
9412
|
+
if (manifest === undefined) {
|
|
9413
|
+
for (const tick of ticks) {
|
|
9414
|
+
out.unresolved.push({ ...tick, reason: 'manifest-missing' })
|
|
9415
|
+
}
|
|
9416
|
+
return out
|
|
9417
|
+
}
|
|
9418
|
+
for (const [id, statuses] of groupById(ticks)) {
|
|
9419
|
+
const entry = entries.find((item) => item.id === id)
|
|
9420
|
+
const verdict = judge(id, statuses, manifest, entry, reason)
|
|
9421
|
+
if (verdict?.kind === 'record') out.record.push(verdict.tick)
|
|
9422
|
+
else if (verdict?.kind === 'skip') out.skipped.push(verdict.skipped)
|
|
9423
|
+
else if (verdict?.kind === 'unresolved') {
|
|
9424
|
+
out.unresolved.push(verdict.unresolved)
|
|
9425
|
+
}
|
|
9426
|
+
}
|
|
9427
|
+
return out
|
|
9428
|
+
}
|
|
9429
|
+
function failureOf(reason) {
|
|
9430
|
+
return reason === 'ledger-unreadable' || reason === 'lock-failed'
|
|
9431
|
+
? reason
|
|
9432
|
+
: 'write-failed'
|
|
9433
|
+
}
|
|
9434
|
+
function applyTicks(root, request, options = {}) {
|
|
9435
|
+
if (!Number.isSafeInteger(request.issue) || request.issue < 1) {
|
|
9436
|
+
return { status: 'failed', reason: 'invalid-request', accepted: [] }
|
|
9437
|
+
}
|
|
9438
|
+
const parsed = parseReportBody(request.body)
|
|
9439
|
+
const ticks = tickedBoxes(parsed.boxes)
|
|
9440
|
+
if (parsed.section !== 'ok' || ticks.length === 0) {
|
|
9441
|
+
return emptyApplied(parsed.section)
|
|
9442
|
+
}
|
|
9443
|
+
const stored = readManifestStore(root)
|
|
9444
|
+
const manifest =
|
|
9445
|
+
stored.status === 'ok'
|
|
9446
|
+
? stored.items.find((item) => item.hash === parsed.manifest)
|
|
9447
|
+
: undefined
|
|
9448
|
+
const read = readLedger(root)
|
|
9449
|
+
if (read.status !== 'ok') {
|
|
9450
|
+
return { status: 'failed', reason: 'ledger-unreadable', accepted: [] }
|
|
9451
|
+
}
|
|
9452
|
+
const reason = `settled in report #${request.issue}`
|
|
9453
|
+
const sorted = triage(ticks, manifest, read.ledger.entries, reason)
|
|
9454
|
+
options.hooks?.afterTriage?.()
|
|
9455
|
+
const accepted = []
|
|
9456
|
+
const skipped = [...sorted.skipped]
|
|
9457
|
+
for (const tick of sorted.record) {
|
|
9458
|
+
const decided = decideLedgerEntry(
|
|
9459
|
+
root,
|
|
9460
|
+
{ id: tick.id, status: tick.status, reason, onlyIfOpen: true },
|
|
9461
|
+
lockOptions(options),
|
|
9462
|
+
)
|
|
9463
|
+
if (decided.status === 'busy') {
|
|
9464
|
+
return { status: 'busy', reason: decided.reason, accepted }
|
|
9465
|
+
}
|
|
9466
|
+
if (decided.status === 'failed' && decided.reason === 'not-open') {
|
|
9467
|
+
skipped.push({ id: tick.id, status: tick.status, reason: 'not-open' })
|
|
9468
|
+
continue
|
|
9469
|
+
}
|
|
9470
|
+
if (decided.status === 'failed') {
|
|
9471
|
+
return { status: 'failed', reason: failureOf(decided.reason), accepted }
|
|
9472
|
+
}
|
|
9473
|
+
accepted.push(tick)
|
|
9474
|
+
}
|
|
9475
|
+
return {
|
|
9476
|
+
status: 'applied',
|
|
9477
|
+
section: parsed.section,
|
|
9478
|
+
accepted,
|
|
9479
|
+
skipped,
|
|
9480
|
+
unresolved: sorted.unresolved,
|
|
9481
|
+
}
|
|
9482
|
+
}
|
|
9483
|
+
|
|
9484
|
+
// src/lib/connect-import.ts
|
|
9485
|
+
import fs8 from 'node:fs'
|
|
9486
|
+
import path7 from 'node:path'
|
|
9487
|
+
var IMPORT_RECORD_FILE = 'run.json'
|
|
9488
|
+
var MAX_IMPORT_RECORD_BYTES = 2 * 1024 * 1024
|
|
9489
|
+
var MAX_IMPORT_PROJECTS = 64
|
|
9490
|
+
var MAX_IMPORT_CANDIDATES = 200
|
|
9491
|
+
var MAX_ROOT_LENGTH = 4096
|
|
9492
|
+
var RUN_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
|
|
9493
|
+
var PROJECT_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/
|
|
9494
|
+
var REPO_SLUG2 = /^[a-z0-9][a-z0-9-]{0,38}\/[a-z0-9._-]{1,100}$/
|
|
9495
|
+
var OBJECT_ID2 = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/
|
|
9496
|
+
var CANDIDATE_ID2 = /^[0-9a-f]{64}$/
|
|
9497
|
+
var CONTROL2 = /[\p{Cc}\p{Cf}]/u
|
|
9498
|
+
var CORPUS_PATH = /^docs\/(?:solutions|plans|brainstorms)\//
|
|
9499
|
+
function isRecord5(value) {
|
|
9500
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
9501
|
+
}
|
|
9502
|
+
function plainString(value, max) {
|
|
9503
|
+
return (
|
|
9504
|
+
typeof value === 'string' &&
|
|
9505
|
+
value !== '' &&
|
|
9506
|
+
value.length <= max &&
|
|
9507
|
+
!CONTROL2.test(value)
|
|
9508
|
+
)
|
|
9509
|
+
}
|
|
9510
|
+
function parseProject(value) {
|
|
9511
|
+
if (!isRecord5(value)) return
|
|
9512
|
+
const { root, key, name, repo, commit } = value
|
|
9513
|
+
if (!plainString(root, MAX_ROOT_LENGTH) || !path7.posix.isAbsolute(root)) {
|
|
9514
|
+
return
|
|
9515
|
+
}
|
|
9516
|
+
if (key !== projectKey(root)) return
|
|
9517
|
+
if (typeof name !== 'string' || !PROJECT_NAME.test(name)) return
|
|
9518
|
+
if (
|
|
9519
|
+
repo !== undefined &&
|
|
9520
|
+
!(typeof repo === 'string' && REPO_SLUG2.test(repo))
|
|
9521
|
+
) {
|
|
9522
|
+
return
|
|
9523
|
+
}
|
|
9524
|
+
if (
|
|
9525
|
+
commit !== undefined &&
|
|
9526
|
+
!(typeof commit === 'string' && OBJECT_ID2.test(commit))
|
|
9527
|
+
) {
|
|
9528
|
+
return
|
|
9529
|
+
}
|
|
9530
|
+
return {
|
|
9531
|
+
root,
|
|
9532
|
+
key,
|
|
9533
|
+
name,
|
|
9534
|
+
...(repo === undefined ? {} : { repo }),
|
|
9535
|
+
...(commit === undefined ? {} : { commit }),
|
|
9536
|
+
}
|
|
9537
|
+
}
|
|
9538
|
+
function parseImportRecord(value) {
|
|
9539
|
+
const malformed = { status: 'failed', reason: 'record-malformed' }
|
|
9540
|
+
if (!isRecord5(value)) return malformed
|
|
9541
|
+
if (value.schemaVersion !== 1) {
|
|
9542
|
+
return { status: 'failed', reason: 'record-schema-version' }
|
|
9543
|
+
}
|
|
9544
|
+
const { runId, projects, candidateBlock } = value
|
|
9545
|
+
if (typeof runId !== 'string' || !RUN_ID.test(runId)) return malformed
|
|
9546
|
+
if (
|
|
9547
|
+
!Array.isArray(projects) ||
|
|
9548
|
+
projects.length < 1 ||
|
|
9549
|
+
projects.length > MAX_IMPORT_PROJECTS
|
|
9550
|
+
) {
|
|
9551
|
+
return malformed
|
|
9552
|
+
}
|
|
9553
|
+
const parsed = []
|
|
9554
|
+
for (const item of projects) {
|
|
9555
|
+
const project = parseProject(item)
|
|
9556
|
+
if (project === undefined) return malformed
|
|
9557
|
+
if (parsed.some((known) => known.root === project.root)) return malformed
|
|
9558
|
+
parsed.push(project)
|
|
9559
|
+
}
|
|
9560
|
+
if (!isRecord5(candidateBlock)) return malformed
|
|
9561
|
+
if (candidateBlock.schemaVersion !== 1) {
|
|
9562
|
+
return { status: 'failed', reason: 'record-schema-version' }
|
|
9563
|
+
}
|
|
9564
|
+
const { candidates } = candidateBlock
|
|
9565
|
+
if (!Array.isArray(candidates) || candidates.length > MAX_IMPORT_CANDIDATES) {
|
|
9566
|
+
return malformed
|
|
9567
|
+
}
|
|
9568
|
+
return {
|
|
9569
|
+
status: 'ok',
|
|
9570
|
+
record: { runId, projects: parsed, candidates },
|
|
9571
|
+
}
|
|
9572
|
+
}
|
|
9573
|
+
function readImportRecord(directory) {
|
|
9574
|
+
let real
|
|
9575
|
+
try {
|
|
9576
|
+
real = fs8.realpathSync.native(directory)
|
|
9577
|
+
if (!fs8.statSync(real).isDirectory()) {
|
|
9578
|
+
return { status: 'failed', reason: 'directory-unreadable' }
|
|
9579
|
+
}
|
|
9580
|
+
} catch {
|
|
9581
|
+
return { status: 'failed', reason: 'directory-unreadable' }
|
|
9582
|
+
}
|
|
9583
|
+
const read = readContainedFile(real, IMPORT_RECORD_FILE, {
|
|
9584
|
+
maxBytes: MAX_IMPORT_RECORD_BYTES,
|
|
9585
|
+
})
|
|
9586
|
+
if (read.kind === 'refused') {
|
|
9587
|
+
switch (read.reason) {
|
|
9588
|
+
case 'missing':
|
|
9589
|
+
return { status: 'failed', reason: 'record-missing' }
|
|
9590
|
+
case 'symlink':
|
|
9591
|
+
return { status: 'failed', reason: 'record-symlink' }
|
|
9592
|
+
case 'not-regular':
|
|
9593
|
+
return { status: 'failed', reason: 'record-not-regular' }
|
|
9594
|
+
case 'too-large':
|
|
9595
|
+
return { status: 'failed', reason: 'record-too-large' }
|
|
9596
|
+
default:
|
|
9597
|
+
return { status: 'failed', reason: 'record-unreadable' }
|
|
9598
|
+
}
|
|
9599
|
+
}
|
|
9600
|
+
let value
|
|
9601
|
+
try {
|
|
9602
|
+
value = JSON.parse(read.bytes.toString('utf8'))
|
|
9603
|
+
} catch {
|
|
9604
|
+
return { status: 'failed', reason: 'record-malformed' }
|
|
9605
|
+
}
|
|
9606
|
+
return parseImportRecord(value)
|
|
9607
|
+
}
|
|
9608
|
+
function mapProject(project, locals) {
|
|
9609
|
+
if (project.repo === undefined) {
|
|
9610
|
+
return { status: 'unmapped', reason: 'project-without-repo' }
|
|
9611
|
+
}
|
|
9612
|
+
const matches = locals.filter((local) => local.repo === project.repo)
|
|
9613
|
+
const [only] = matches
|
|
9614
|
+
if (only === undefined) {
|
|
9615
|
+
return { status: 'unmapped', reason: 'repo-not-allowed' }
|
|
9616
|
+
}
|
|
9617
|
+
if (matches.length > 1) {
|
|
9618
|
+
return {
|
|
9619
|
+
status: 'unmapped',
|
|
9620
|
+
reason: 'repo-ambiguous',
|
|
9621
|
+
keys: matches.map((local) => local.key).sort(),
|
|
9622
|
+
}
|
|
9623
|
+
}
|
|
9624
|
+
return { status: 'mapped', local: only }
|
|
9625
|
+
}
|
|
9626
|
+
var REFUSAL_TEXT = Object.freeze({
|
|
9627
|
+
'candidate-malformed': 'the candidate is malformed',
|
|
9628
|
+
'duplicate-candidate': 'the record lists this candidate more than once',
|
|
9629
|
+
'candidate-id-mismatch':
|
|
9630
|
+
'the recorded id does not match the candidate, so the record was changed',
|
|
9631
|
+
'project-not-in-record':
|
|
9632
|
+
'the candidate names a project the record does not list',
|
|
9633
|
+
'project-without-repo':
|
|
9634
|
+
'a project of the candidate has no GitHub remote in the record, so it cannot be mapped to a local checkout',
|
|
9635
|
+
'repo-not-allowed':
|
|
9636
|
+
'no allowed local checkout has the recorded repository as its origin',
|
|
9637
|
+
'repo-ambiguous':
|
|
9638
|
+
'more than one allowed local checkout has the recorded repository as its origin',
|
|
9639
|
+
'project-unreadable': 'a mapped local checkout could not be read',
|
|
9640
|
+
'path-rejected': 'a cited path is not a safe path for this kind of file',
|
|
9641
|
+
'rule-id-mismatch':
|
|
9642
|
+
'the recorded rule id does not belong to any source of the candidate',
|
|
9643
|
+
'file-not-tracked': 'a cited file is not tracked in the local checkout',
|
|
9644
|
+
'file-symlink': 'a cited file is a symlink in the local checkout',
|
|
9645
|
+
'file-changed': 'a cited file differs from the file the run read',
|
|
9646
|
+
'file-vanished': 'a cited file is gone from the local checkout',
|
|
9647
|
+
'check-failed': 'the candidate failed a check against the local files',
|
|
9648
|
+
})
|
|
9649
|
+
function fileRefs(value, max) {
|
|
9650
|
+
if (value === undefined) return []
|
|
9651
|
+
if (!Array.isArray(value) || value.length > max) return
|
|
9652
|
+
const out = []
|
|
9653
|
+
for (const item of value) {
|
|
9654
|
+
if (!isRecord5(item)) return
|
|
9655
|
+
const { project, path: file, fingerprint, excerpt } = item
|
|
9656
|
+
if (
|
|
9657
|
+
typeof project !== 'string' ||
|
|
9658
|
+
typeof file !== 'string' ||
|
|
9659
|
+
typeof fingerprint !== 'string' ||
|
|
9660
|
+
typeof excerpt !== 'string'
|
|
9661
|
+
) {
|
|
9662
|
+
return
|
|
9663
|
+
}
|
|
9664
|
+
out.push({ project, path: file, fingerprint, excerpt, rest: item })
|
|
9665
|
+
}
|
|
9666
|
+
return out
|
|
9667
|
+
}
|
|
9668
|
+
function evidenceRefs(value, max) {
|
|
9669
|
+
if (value === undefined) return []
|
|
9670
|
+
if (!Array.isArray(value) || value.length > max) return
|
|
9671
|
+
const out = []
|
|
9672
|
+
for (const item of value) {
|
|
9673
|
+
if (!isRecord5(item)) return
|
|
9674
|
+
if (typeof item.path !== 'string' || typeof item.fingerprint !== 'string') {
|
|
9675
|
+
return
|
|
9676
|
+
}
|
|
9677
|
+
out.push(item)
|
|
9678
|
+
}
|
|
9679
|
+
return out
|
|
9680
|
+
}
|
|
9681
|
+
function shapeOf(raw) {
|
|
9682
|
+
if (!isRecord5(raw)) return
|
|
9683
|
+
const { id, kind, ruleId, target } = raw
|
|
9684
|
+
if (typeof id !== 'string' || !CANDIDATE_ID2.test(id)) return
|
|
9685
|
+
if (
|
|
9686
|
+
typeof kind !== 'string' ||
|
|
9687
|
+
typeof ruleId !== 'string' ||
|
|
9688
|
+
typeof target !== 'string'
|
|
9689
|
+
) {
|
|
9690
|
+
return
|
|
9691
|
+
}
|
|
9692
|
+
const sources = fileRefs(raw.sources, CANDIDATE_LIMITS.sources)
|
|
9693
|
+
const counterevidence = fileRefs(
|
|
9694
|
+
raw.counterevidence,
|
|
9695
|
+
CANDIDATE_LIMITS.counterevidence,
|
|
9696
|
+
)
|
|
9697
|
+
const targetEvidence = evidenceRefs(
|
|
9698
|
+
raw.targetEvidence,
|
|
9699
|
+
CANDIDATE_LIMITS.targetEvidence,
|
|
9700
|
+
)
|
|
9701
|
+
if (
|
|
9702
|
+
sources === undefined ||
|
|
9703
|
+
counterevidence === undefined ||
|
|
9704
|
+
targetEvidence === undefined
|
|
9705
|
+
) {
|
|
9706
|
+
return
|
|
9707
|
+
}
|
|
9708
|
+
return {
|
|
9709
|
+
id,
|
|
9710
|
+
kind,
|
|
9711
|
+
ruleId,
|
|
9712
|
+
target,
|
|
9713
|
+
title: raw.title,
|
|
9714
|
+
summary: raw.summary,
|
|
9715
|
+
nextStep: raw.nextStep,
|
|
9716
|
+
overlapCheck: raw.overlapCheck,
|
|
9717
|
+
sources,
|
|
9718
|
+
counterevidence,
|
|
9719
|
+
targetEvidence,
|
|
9720
|
+
}
|
|
9721
|
+
}
|
|
9722
|
+
function recordedIdentity(shape) {
|
|
9723
|
+
return candidateIdentity({
|
|
9724
|
+
kind: shape.kind,
|
|
9725
|
+
target: shape.target,
|
|
9726
|
+
ruleId: shape.ruleId,
|
|
9727
|
+
sources: shape.sources.map((source) => ({
|
|
9728
|
+
role: `source@${projectKey(source.project)}`,
|
|
9729
|
+
path: source.path,
|
|
9730
|
+
fingerprint: source.fingerprint,
|
|
9731
|
+
})),
|
|
9732
|
+
targetEvidence: shape.targetEvidence.map((item) => ({
|
|
9733
|
+
path: String(item.path),
|
|
9734
|
+
fingerprint: String(item.fingerprint),
|
|
9735
|
+
})),
|
|
9736
|
+
})
|
|
9737
|
+
}
|
|
9738
|
+
function isRefusal(value) {
|
|
9739
|
+
return typeof value === 'object' && value !== null && 'refusal' in value
|
|
9740
|
+
}
|
|
9741
|
+
function refuse(reason, extra = {}) {
|
|
9742
|
+
return { refusal: { reason, ...extra } }
|
|
9743
|
+
}
|
|
9744
|
+
function pathRefusal(file, corpusOnly) {
|
|
9745
|
+
const problem = pathProblem(file)
|
|
9746
|
+
if (problem !== undefined) return refuse('path-rejected', { detail: problem })
|
|
9747
|
+
if (corpusOnly && !CORPUS_PATH.test(file)) {
|
|
9748
|
+
return refuse('path-rejected', { detail: 'outside-corpus' })
|
|
9749
|
+
}
|
|
9750
|
+
return
|
|
9751
|
+
}
|
|
9752
|
+
function createLandings(projects, locals, factsFor) {
|
|
9753
|
+
const byRecorded = new Map()
|
|
9754
|
+
for (const project of projects) {
|
|
9755
|
+
byRecorded.set(project.root, mapProject(project, locals))
|
|
9756
|
+
}
|
|
9757
|
+
const cache = new Map()
|
|
9758
|
+
return {
|
|
9759
|
+
projects,
|
|
9760
|
+
mappingOf: (root) => byRecorded.get(root),
|
|
9761
|
+
projectOf: (root) => projects.find((project) => project.root === root),
|
|
9762
|
+
factsOf(local) {
|
|
9763
|
+
if (!cache.has(local.root)) cache.set(local.root, factsFor(local.root))
|
|
9764
|
+
return cache.get(local.root)
|
|
9765
|
+
},
|
|
9766
|
+
}
|
|
9767
|
+
}
|
|
9768
|
+
function land(shape, landings) {
|
|
9769
|
+
const roots = new Set([
|
|
9770
|
+
shape.target,
|
|
9771
|
+
...shape.sources.map((source) => source.project),
|
|
9772
|
+
...shape.counterevidence.map((source) => source.project),
|
|
9773
|
+
])
|
|
9774
|
+
const out = new Map()
|
|
9775
|
+
for (const root of roots) {
|
|
9776
|
+
const project = landings.projectOf(root)
|
|
9777
|
+
const mapping = landings.mappingOf(root)
|
|
9778
|
+
if (project === undefined || mapping === undefined) {
|
|
9779
|
+
return refuse('project-not-in-record')
|
|
9780
|
+
}
|
|
9781
|
+
if (mapping.status === 'unmapped') {
|
|
9782
|
+
return refuse(mapping.reason, {
|
|
9783
|
+
...(mapping.keys === undefined ? {} : { keys: mapping.keys }),
|
|
9784
|
+
...(project.repo === undefined ? {} : { repo: project.repo }),
|
|
9785
|
+
})
|
|
9786
|
+
}
|
|
9787
|
+
const facts = landings.factsOf(mapping.local)
|
|
9788
|
+
if (facts === undefined) return refuse('project-unreadable')
|
|
9789
|
+
out.set(root, { local: mapping.local, facts })
|
|
9790
|
+
}
|
|
9791
|
+
return out
|
|
9792
|
+
}
|
|
9793
|
+
function checkPaths(shape) {
|
|
9794
|
+
const groups = [
|
|
9795
|
+
[shape.sources, true],
|
|
9796
|
+
[shape.counterevidence, false],
|
|
9797
|
+
[shape.targetEvidence.map((item) => ({ path: String(item.path) })), false],
|
|
9798
|
+
]
|
|
9799
|
+
for (const [items, corpusOnly] of groups) {
|
|
9800
|
+
for (const item of items) {
|
|
9801
|
+
const refusal = pathRefusal(item.path, corpusOnly)
|
|
9802
|
+
if (refusal !== undefined) return refusal
|
|
9803
|
+
}
|
|
9804
|
+
}
|
|
9805
|
+
return
|
|
9806
|
+
}
|
|
9807
|
+
function localRuleId(shape, landed) {
|
|
9808
|
+
const bound = shape.sources.find(
|
|
9809
|
+
(source) => ruleIdFor(source.project, source.path) === shape.ruleId,
|
|
9810
|
+
)
|
|
9811
|
+
const landing = bound === undefined ? undefined : landed.get(bound.project)
|
|
9812
|
+
if (bound === undefined || landing === undefined) {
|
|
9813
|
+
return refuse('rule-id-mismatch')
|
|
9814
|
+
}
|
|
9815
|
+
return ruleIdFor(landing.local.root, bound.path)
|
|
9816
|
+
}
|
|
9817
|
+
function localFile(landing, rel, fingerprint) {
|
|
9818
|
+
const file = `${landing.local.name}:${rel}`
|
|
9819
|
+
const known = landing.facts.files.get(rel)
|
|
9820
|
+
if (known === undefined) {
|
|
9821
|
+
const probe = readContainedFile(landing.local.root, rel, { maxBytes: 1 })
|
|
9822
|
+
const symlink = probe.kind === 'refused' && probe.reason === 'symlink'
|
|
9823
|
+
return refuse(symlink ? 'file-symlink' : 'file-not-tracked', { file })
|
|
9824
|
+
}
|
|
9825
|
+
return known.sha256 === fingerprint
|
|
9826
|
+
? undefined
|
|
9827
|
+
: refuse('file-changed', { file })
|
|
9828
|
+
}
|
|
9829
|
+
function checkFiles(shape, landed) {
|
|
9830
|
+
const refs = [
|
|
9831
|
+
...shape.sources.map((s) => [s.project, s.path, s.fingerprint]),
|
|
9832
|
+
...shape.counterevidence.map((s) => [s.project, s.path, s.fingerprint]),
|
|
9833
|
+
...shape.targetEvidence.map((e) => [
|
|
9834
|
+
shape.target,
|
|
9835
|
+
String(e.path),
|
|
9836
|
+
String(e.fingerprint),
|
|
9837
|
+
]),
|
|
9838
|
+
]
|
|
9839
|
+
for (const [root, rel, fingerprint] of refs) {
|
|
9840
|
+
const landing = landed.get(root)
|
|
9841
|
+
if (landing === undefined) return refuse('project-not-in-record')
|
|
9842
|
+
const refusal = localFile(landing, rel, fingerprint)
|
|
9843
|
+
if (refusal !== undefined) return refusal
|
|
9844
|
+
}
|
|
9845
|
+
return
|
|
9846
|
+
}
|
|
9847
|
+
function localRaw(shape, ruleId, landed) {
|
|
9848
|
+
const rootOf = (recorded) => landed.get(recorded)?.local.root ?? recorded
|
|
9849
|
+
const source = (ref) => ({
|
|
9850
|
+
project: rootOf(ref.project),
|
|
9851
|
+
path: ref.path,
|
|
9852
|
+
fingerprint: ref.fingerprint,
|
|
9853
|
+
excerpt: ref.excerpt,
|
|
9854
|
+
})
|
|
9855
|
+
return {
|
|
9856
|
+
kind: shape.kind,
|
|
9857
|
+
ruleId,
|
|
9858
|
+
target: rootOf(shape.target),
|
|
9859
|
+
title: shape.title,
|
|
9860
|
+
summary: shape.summary,
|
|
9861
|
+
nextStep: shape.nextStep,
|
|
9862
|
+
overlapCheck: shape.overlapCheck,
|
|
9863
|
+
sources: shape.sources.map(source),
|
|
9864
|
+
targetEvidence: shape.targetEvidence.map((item) => ({
|
|
9865
|
+
path: item.path,
|
|
9866
|
+
start: item.start,
|
|
9867
|
+
end: item.end,
|
|
9868
|
+
symbol: item.symbol,
|
|
9869
|
+
fingerprint: item.fingerprint,
|
|
9870
|
+
excerpt: item.excerpt,
|
|
9871
|
+
})),
|
|
9872
|
+
counterevidence: shape.counterevidence.map(source),
|
|
9873
|
+
}
|
|
9874
|
+
}
|
|
9875
|
+
function contextOf(landed) {
|
|
9876
|
+
const projects = new Map()
|
|
9877
|
+
const ruleIds = new Set()
|
|
9878
|
+
for (const { local, facts } of landed.values()) {
|
|
9879
|
+
projects.set(local.root, facts)
|
|
9880
|
+
for (const [rel, info] of facts.files) {
|
|
9881
|
+
if (info.role === 'learning') ruleIds.add(ruleIdFor(local.root, rel))
|
|
9882
|
+
}
|
|
9883
|
+
}
|
|
9884
|
+
return { projects, ruleIds }
|
|
9885
|
+
}
|
|
9886
|
+
function verifyOne(raw, landings, seen) {
|
|
9887
|
+
const shape = shapeOf(raw)
|
|
9888
|
+
if (shape === undefined) return refuse('candidate-malformed')
|
|
9889
|
+
if (seen.has(shape.id)) return refuse('duplicate-candidate')
|
|
9890
|
+
seen.add(shape.id)
|
|
9891
|
+
if (recordedIdentity(shape) !== shape.id) {
|
|
9892
|
+
return refuse('candidate-id-mismatch')
|
|
9893
|
+
}
|
|
9894
|
+
const landed = land(shape, landings)
|
|
9895
|
+
if (isRefusal(landed)) return landed
|
|
9896
|
+
const pathIssue = checkPaths(shape)
|
|
9897
|
+
if (pathIssue !== undefined) return pathIssue
|
|
9898
|
+
const ruleId = localRuleId(shape, landed)
|
|
9899
|
+
if (isRefusal(ruleId)) return ruleId
|
|
9900
|
+
const fileIssue = checkFiles(shape, landed)
|
|
9901
|
+
if (fileIssue !== undefined) return fileIssue
|
|
9902
|
+
const checked = checkCandidate(
|
|
9903
|
+
localRaw(shape, ruleId, landed),
|
|
9904
|
+
contextOf(landed),
|
|
9905
|
+
)
|
|
9906
|
+
if (checked.status === 'dropped') {
|
|
9907
|
+
return refuse('check-failed', { detail: checked.reason })
|
|
9908
|
+
}
|
|
9909
|
+
if (checked.status === 'unresolved') {
|
|
9910
|
+
const [missing] = checked.missing
|
|
9911
|
+
return refuse('file-vanished', {
|
|
9912
|
+
...(missing === undefined ? {} : { file: missing }),
|
|
9913
|
+
})
|
|
9914
|
+
}
|
|
9915
|
+
const record = toCandidateRecord(checked.candidate)
|
|
9916
|
+
return {
|
|
9917
|
+
recordedId: shape.id,
|
|
9918
|
+
candidate: checked.candidate,
|
|
9919
|
+
record,
|
|
9920
|
+
id: candidateIdentity(record),
|
|
9921
|
+
}
|
|
9922
|
+
}
|
|
9923
|
+
function projectReports(landings) {
|
|
9924
|
+
return landings.projects.map((project) => {
|
|
9925
|
+
const mapping = landings.mappingOf(project.root)
|
|
9926
|
+
const base = {
|
|
9927
|
+
name: project.name,
|
|
9928
|
+
...(project.repo === undefined ? {} : { repo: project.repo }),
|
|
9929
|
+
...(project.commit === undefined ? {} : { ciCommit: project.commit }),
|
|
9930
|
+
}
|
|
9931
|
+
if (mapping === undefined || mapping.status === 'unmapped') {
|
|
9932
|
+
return { ...base, status: mapping?.reason ?? 'repo-not-allowed' }
|
|
9933
|
+
}
|
|
9934
|
+
const { local } = mapping
|
|
9935
|
+
return {
|
|
9936
|
+
...base,
|
|
9937
|
+
status: 'mapped',
|
|
9938
|
+
localRoot: local.root,
|
|
9939
|
+
localKey: local.key,
|
|
9940
|
+
localName: local.name,
|
|
9941
|
+
...(local.commit === undefined ? {} : { localCommit: local.commit }),
|
|
9942
|
+
}
|
|
9943
|
+
})
|
|
9944
|
+
}
|
|
9945
|
+
function named(raw, refusal) {
|
|
9946
|
+
const id =
|
|
9947
|
+
isRecord5(raw) && typeof raw.id === 'string' && CANDIDATE_ID2.test(raw.id)
|
|
9948
|
+
? raw.id
|
|
9949
|
+
: null
|
|
9950
|
+
return {
|
|
9951
|
+
...refusal,
|
|
9952
|
+
id,
|
|
9953
|
+
title: nameDropped('import', raw, refusal.reason).title,
|
|
9954
|
+
reasonText: REFUSAL_TEXT[refusal.reason],
|
|
9955
|
+
...(refusal.file === undefined
|
|
9956
|
+
? {}
|
|
9957
|
+
: {
|
|
9958
|
+
file: sanitizeText(refusal.file).replaceAll(
|
|
9959
|
+
`
|
|
9960
|
+
`,
|
|
9961
|
+
' ',
|
|
9962
|
+
),
|
|
9963
|
+
}),
|
|
9964
|
+
}
|
|
9965
|
+
}
|
|
9966
|
+
function verifyRecord(input) {
|
|
9967
|
+
const landings = createLandings(
|
|
9968
|
+
input.record.projects,
|
|
9969
|
+
input.locals,
|
|
9970
|
+
input.factsFor,
|
|
9971
|
+
)
|
|
9972
|
+
const accepted = []
|
|
9973
|
+
const refused = []
|
|
9974
|
+
const seen = new Set()
|
|
9975
|
+
const local = new Set()
|
|
9976
|
+
for (const raw of input.record.candidates) {
|
|
9977
|
+
const outcome = verifyOne(raw, landings, seen)
|
|
9978
|
+
if ('refusal' in outcome) {
|
|
9979
|
+
refused.push(named(raw, outcome.refusal))
|
|
9980
|
+
continue
|
|
9981
|
+
}
|
|
9982
|
+
if (local.has(outcome.id)) {
|
|
9983
|
+
refused.push(named(raw, refuse('duplicate-candidate').refusal))
|
|
9984
|
+
continue
|
|
9985
|
+
}
|
|
9986
|
+
local.add(outcome.id)
|
|
9987
|
+
accepted.push(outcome)
|
|
9988
|
+
}
|
|
9989
|
+
return { projects: projectReports(landings), accepted, refused }
|
|
9990
|
+
}
|
|
9991
|
+
function provenanceLine(provenance) {
|
|
9992
|
+
const commit =
|
|
9993
|
+
provenance.commit === undefined
|
|
9994
|
+
? ''
|
|
9995
|
+
: ` at commit ${inline(provenance.commit)}`
|
|
9996
|
+
return `GitHub run ${inline(provenance.runId)} of ${inline(provenance.repo)}${commit}`
|
|
9997
|
+
}
|
|
9998
|
+
function projectLine(project) {
|
|
9999
|
+
const repo = project.repo === undefined ? 'no GitHub remote' : project.repo
|
|
10000
|
+
const head = `- ${inline(project.name)} (${inline(repo)})`
|
|
10001
|
+
if (project.status !== 'mapped') {
|
|
10002
|
+
return `${head}: not mapped (${REFUSAL_TEXT[project.status]})`
|
|
10003
|
+
}
|
|
10004
|
+
const ci =
|
|
10005
|
+
project.ciCommit === undefined ? 'unknown' : inline(project.ciCommit)
|
|
10006
|
+
const local =
|
|
10007
|
+
project.localCommit === undefined ? 'unknown' : inline(project.localCommit)
|
|
10008
|
+
return `${head}: CI commit ${ci}; local commit ${local} in ${inline(project.localRoot ?? '')}`
|
|
10009
|
+
}
|
|
10010
|
+
function refusalLines(refused) {
|
|
10011
|
+
if (refused.length === 0) return []
|
|
10012
|
+
const out = ['Refused (nothing was imported from these):']
|
|
10013
|
+
for (const item of refused) {
|
|
10014
|
+
const where = item.file === undefined ? '' : ` File: ${inline(item.file)}.`
|
|
10015
|
+
const detail = item.detail === undefined ? '' : ` (${item.detail})`
|
|
10016
|
+
const keys =
|
|
10017
|
+
item.keys === undefined
|
|
10018
|
+
? ''
|
|
10019
|
+
: ` Local project keys: ${item.keys.map(inline).join(', ')}.`
|
|
10020
|
+
out.push(
|
|
10021
|
+
quote(item.title),
|
|
10022
|
+
` ${item.reason}${detail}: ${item.reasonText}.${where}${keys}`,
|
|
10023
|
+
)
|
|
10024
|
+
}
|
|
10025
|
+
return out
|
|
10026
|
+
}
|
|
10027
|
+
function skippedLines(skipped) {
|
|
10028
|
+
if (skipped.length === 0) return []
|
|
10029
|
+
const out = ['Skipped (already decided locally):']
|
|
10030
|
+
for (const item of skipped) {
|
|
10031
|
+
const pointer =
|
|
10032
|
+
item.refreshPointer === undefined
|
|
10033
|
+
? ''
|
|
10034
|
+
: ` Refresh pointer: ${inline(item.refreshPointer)}.`
|
|
10035
|
+
out.push(quote(item.title), ` ${item.reason}.${pointer}`)
|
|
10036
|
+
}
|
|
10037
|
+
return out
|
|
10038
|
+
}
|
|
10039
|
+
var MAX_LINES_LISTED = 50
|
|
10040
|
+
function promotionLines(candidates) {
|
|
10041
|
+
if (candidates.length === 0) return []
|
|
10042
|
+
const out = ['Candidates (untrusted text, quoted):']
|
|
10043
|
+
for (const { item, runRecordPath } of candidates.slice(0, MAX_LINES_LISTED)) {
|
|
10044
|
+
out.push(quote(`${item.candidate.kind}: ${item.candidate.title}`))
|
|
10045
|
+
out.push(handoffLine(item, runRecordPath, isPasteablePath(runRecordPath)))
|
|
10046
|
+
}
|
|
10047
|
+
const left = candidates.length - MAX_LINES_LISTED
|
|
10048
|
+
if (left > 0) out.push(`${left} more candidates are in the run record.`)
|
|
10049
|
+
return out
|
|
10050
|
+
}
|
|
10051
|
+
function renderImportSummary(model, digestPath) {
|
|
10052
|
+
const out = [
|
|
10053
|
+
`Connect import ${model.runId === undefined ? '(no new run)' : sanitizeText(model.runId)}: ${model.outcome}.`,
|
|
10054
|
+
`Imported: ${model.imported}. Already imported: ${model.candidates.length - model.imported}. Skipped: ${model.skipped.length}. Refused: ${model.refused.length}.`,
|
|
10055
|
+
`Source: ${provenanceLine(model.provenance)}.`,
|
|
10056
|
+
'Projects:',
|
|
10057
|
+
...model.projects.map(projectLine),
|
|
10058
|
+
]
|
|
10059
|
+
if (model.outcome === 'degraded') {
|
|
10060
|
+
out.push(
|
|
10061
|
+
'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.',
|
|
10062
|
+
)
|
|
10063
|
+
}
|
|
10064
|
+
out.push(...refusalLines(model.refused), ...skippedLines(model.skipped))
|
|
10065
|
+
if (digestPath !== undefined) {
|
|
10066
|
+
out.push(
|
|
10067
|
+
`Digest: ${sanitizeText(digestPath).replaceAll(
|
|
10068
|
+
`
|
|
10069
|
+
`,
|
|
10070
|
+
' ',
|
|
10071
|
+
)}`,
|
|
10072
|
+
)
|
|
10073
|
+
}
|
|
10074
|
+
out.push(...promotionLines(model.candidates))
|
|
10075
|
+
return out.join(`
|
|
10076
|
+
`)
|
|
10077
|
+
}
|
|
10078
|
+
function renderImportDigest(model, written, generatedAt) {
|
|
10079
|
+
const out = [
|
|
10080
|
+
`# Connect import digest ${inline(model.runId ?? 'none')}`,
|
|
10081
|
+
'',
|
|
10082
|
+
`Outcome: ${model.outcome}`,
|
|
10083
|
+
...(model.degradeReasons.length > 0
|
|
10084
|
+
? [`Degraded because: ${model.degradeReasons.map(inline).join(', ')}`]
|
|
10085
|
+
: []),
|
|
10086
|
+
`Generated: ${generatedAt}`,
|
|
10087
|
+
`Source: ${provenanceLine(model.provenance)}`,
|
|
10088
|
+
`Run record: ${inline(model.sourceRunId)}`,
|
|
10089
|
+
'',
|
|
10090
|
+
'Projects:',
|
|
10091
|
+
...model.projects.map(projectLine),
|
|
10092
|
+
'',
|
|
10093
|
+
...refusalLines(model.refused),
|
|
10094
|
+
...(model.refused.length > 0 ? [''] : []),
|
|
10095
|
+
...skippedLines(model.skipped),
|
|
10096
|
+
...(model.skipped.length > 0 ? [''] : []),
|
|
10097
|
+
'## Candidates',
|
|
10098
|
+
'',
|
|
10099
|
+
]
|
|
10100
|
+
if (written.length === 0) out.push('None.', '')
|
|
10101
|
+
written.forEach((item, index) => {
|
|
10102
|
+
out.push(...renderCandidate(item, index))
|
|
10103
|
+
})
|
|
10104
|
+
return `${out
|
|
10105
|
+
.join(`
|
|
10106
|
+
`)
|
|
10107
|
+
.trimEnd()}
|
|
10108
|
+
`
|
|
10109
|
+
}
|
|
10110
|
+
|
|
10111
|
+
// src/lib/connect-repo-identity.ts
|
|
10112
|
+
var MAX_CONFIG_BYTES = 1024 * 1024
|
|
10113
|
+
var MAX_HEAD_BYTES = 4096
|
|
10114
|
+
var MAX_REF_BYTES = 4096
|
|
10115
|
+
var MAX_PACKED_REFS_BYTES = 8 * 1024 * 1024
|
|
10116
|
+
var OWNER = /^[A-Za-z0-9][A-Za-z0-9-]{0,38}$/
|
|
10117
|
+
var REPO2 = /^[A-Za-z0-9._-]{1,100}$/
|
|
10118
|
+
var HTTPS_FORM = /^https:\/\/(?:[^/@\s]*@)?github\.com\/(\S+)$/i
|
|
10119
|
+
var SSH_URL_FORM = /^ssh:\/\/(?:[A-Za-z0-9._-]+@)?github\.com\/(\S+)$/i
|
|
10120
|
+
var SCP_FORM = /^(?:[A-Za-z0-9._-]+@)?github\.com:(\S+)$/i
|
|
10121
|
+
function ownerAndRepo(pathPart) {
|
|
10122
|
+
const segments = pathPart.replace(/\/+$/, '').split('/')
|
|
10123
|
+
if (segments.length !== 2) return
|
|
10124
|
+
const [owner = '', named = ''] = segments
|
|
10125
|
+
const repo = named.endsWith('.git') ? named.slice(0, -'.git'.length) : named
|
|
10126
|
+
if (!OWNER.test(owner) || !REPO2.test(repo)) return
|
|
10127
|
+
if (repo === '.' || repo === '..') return
|
|
10128
|
+
return `${owner}/${repo}`.toLowerCase()
|
|
10129
|
+
}
|
|
10130
|
+
function normalizeGithubRemote(url) {
|
|
10131
|
+
const match =
|
|
10132
|
+
HTTPS_FORM.exec(url) ?? SSH_URL_FORM.exec(url) ?? SCP_FORM.exec(url)
|
|
10133
|
+
const rest = match?.[1]
|
|
10134
|
+
return rest === undefined ? undefined : ownerAndRepo(rest)
|
|
10135
|
+
}
|
|
10136
|
+
function configValue(raw) {
|
|
10137
|
+
let out = ''
|
|
10138
|
+
let quoted = false
|
|
10139
|
+
for (const ch of raw.trim()) {
|
|
10140
|
+
if (ch === '"') {
|
|
10141
|
+
quoted = !quoted
|
|
10142
|
+
continue
|
|
10143
|
+
}
|
|
10144
|
+
if (!quoted && (ch === '#' || ch === ';')) break
|
|
10145
|
+
out += ch
|
|
10146
|
+
}
|
|
10147
|
+
return out.trim()
|
|
10148
|
+
}
|
|
10149
|
+
function isOriginSection(header) {
|
|
10150
|
+
const match = /^([A-Za-z]+)[ \t]+"([^"]*)"$/.exec(header.trim())
|
|
10151
|
+
return match?.[1]?.toLowerCase() === 'remote' && match[2] === 'origin'
|
|
10152
|
+
}
|
|
10153
|
+
function originUrl(config) {
|
|
10154
|
+
let inOrigin = false
|
|
10155
|
+
for (const raw of config.split(`
|
|
10156
|
+
`)) {
|
|
10157
|
+
let line = raw.trim()
|
|
10158
|
+
if (line.startsWith('[')) {
|
|
10159
|
+
const end = line.indexOf(']')
|
|
10160
|
+
if (end === -1) {
|
|
10161
|
+
inOrigin = false
|
|
10162
|
+
continue
|
|
10163
|
+
}
|
|
10164
|
+
inOrigin = isOriginSection(line.slice(1, end))
|
|
10165
|
+
line = line.slice(end + 1).trim()
|
|
10166
|
+
}
|
|
10167
|
+
if (!inOrigin) continue
|
|
10168
|
+
const key = /^url[ \t]*=(.*)$/i.exec(line)
|
|
10169
|
+
if (key?.[1] !== undefined) return configValue(key[1])
|
|
10170
|
+
}
|
|
10171
|
+
return
|
|
10172
|
+
}
|
|
10173
|
+
var OBJECT_ID3 = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/
|
|
10174
|
+
var REF_NAME = /^refs\/[A-Za-z0-9._/-]+$/
|
|
10175
|
+
function readGitText(mainRoot, rel, maxBytes) {
|
|
10176
|
+
const read = readContainedFile(mainRoot, rel, { maxBytes })
|
|
10177
|
+
return read.kind === 'bytes' ? read.bytes.toString('utf8') : undefined
|
|
10178
|
+
}
|
|
10179
|
+
function firstLine2(text) {
|
|
10180
|
+
return (
|
|
10181
|
+
text.split(
|
|
10182
|
+
`
|
|
10183
|
+
`,
|
|
10184
|
+
1,
|
|
10185
|
+
)[0] ?? ''
|
|
10186
|
+
)
|
|
10187
|
+
.replace(/\r$/, '')
|
|
10188
|
+
.trim()
|
|
10189
|
+
}
|
|
10190
|
+
function safeRefName(ref) {
|
|
10191
|
+
return (
|
|
10192
|
+
REF_NAME.test(ref) &&
|
|
10193
|
+
ref
|
|
10194
|
+
.split('/')
|
|
10195
|
+
.every(
|
|
10196
|
+
(part) =>
|
|
10197
|
+
part !== '' && !part.startsWith('.') && !part.endsWith('.lock'),
|
|
10198
|
+
)
|
|
10199
|
+
)
|
|
10200
|
+
}
|
|
10201
|
+
function packedRef(packed, ref) {
|
|
10202
|
+
for (const raw of packed.split(`
|
|
10203
|
+
`)) {
|
|
10204
|
+
const line = raw.trim()
|
|
10205
|
+
if (line === '' || line.startsWith('#') || line.startsWith('^')) continue
|
|
10206
|
+
const [id, name, ...rest] = line.split(/\s+/)
|
|
10207
|
+
if (rest.length === 0 && name === ref && OBJECT_ID3.test(id ?? ''))
|
|
10208
|
+
return id
|
|
10209
|
+
}
|
|
10210
|
+
return
|
|
10211
|
+
}
|
|
10212
|
+
function headCommit(mainRoot) {
|
|
10213
|
+
const head = readGitText(mainRoot, '.git/HEAD', MAX_HEAD_BYTES)
|
|
10214
|
+
if (head === undefined) return
|
|
10215
|
+
const line = firstLine2(head)
|
|
10216
|
+
if (OBJECT_ID3.test(line)) return line
|
|
10217
|
+
const symbolic = /^ref:[ \t]*(\S+)$/.exec(line)?.[1]
|
|
10218
|
+
if (symbolic === undefined || !safeRefName(symbolic)) return
|
|
10219
|
+
const loose = readGitText(mainRoot, `.git/${symbolic}`, MAX_REF_BYTES)
|
|
10220
|
+
const looseId = loose === undefined ? '' : firstLine2(loose)
|
|
10221
|
+
if (OBJECT_ID3.test(looseId)) return looseId
|
|
10222
|
+
const packed = readGitText(
|
|
10223
|
+
mainRoot,
|
|
10224
|
+
'.git/packed-refs',
|
|
10225
|
+
MAX_PACKED_REFS_BYTES,
|
|
10226
|
+
)
|
|
10227
|
+
return packed === undefined ? undefined : packedRef(packed, symbolic)
|
|
10228
|
+
}
|
|
10229
|
+
function readRepoIdentity(mainRoot) {
|
|
10230
|
+
try {
|
|
10231
|
+
const config = readGitText(mainRoot, '.git/config', MAX_CONFIG_BYTES)
|
|
10232
|
+
const url = config === undefined ? undefined : originUrl(config)
|
|
10233
|
+
const repo = url === undefined ? undefined : normalizeGithubRemote(url)
|
|
10234
|
+
const commit = headCommit(mainRoot)
|
|
10235
|
+
return {
|
|
10236
|
+
...(repo === undefined ? {} : { repo }),
|
|
10237
|
+
...(commit === undefined ? {} : { commit }),
|
|
10238
|
+
}
|
|
10239
|
+
} catch {
|
|
10240
|
+
return {}
|
|
10241
|
+
}
|
|
10242
|
+
}
|
|
10243
|
+
|
|
10244
|
+
// src/lib/main-module.ts
|
|
10245
|
+
import fs9 from 'node:fs'
|
|
10246
|
+
import { fileURLToPath } from 'node:url'
|
|
10247
|
+
function resolveRealPath(value) {
|
|
10248
|
+
if (value === undefined || value === '') return
|
|
10249
|
+
try {
|
|
10250
|
+
return fs9.realpathSync(value)
|
|
10251
|
+
} catch {
|
|
10252
|
+
return
|
|
10253
|
+
}
|
|
10254
|
+
}
|
|
10255
|
+
function moduleFilePath(moduleUrl) {
|
|
10256
|
+
try {
|
|
10257
|
+
return fileURLToPath(moduleUrl)
|
|
10258
|
+
} catch {
|
|
10259
|
+
return
|
|
10260
|
+
}
|
|
10261
|
+
}
|
|
10262
|
+
function isMainModule(moduleUrl, argv = process.argv) {
|
|
10263
|
+
const entryPath = resolveRealPath(argv[1])
|
|
10264
|
+
const modulePath = resolveRealPath(moduleFilePath(moduleUrl))
|
|
10265
|
+
return (
|
|
10266
|
+
entryPath !== undefined &&
|
|
10267
|
+
modulePath !== undefined &&
|
|
10268
|
+
entryPath === modulePath
|
|
10269
|
+
)
|
|
10270
|
+
}
|
|
10271
|
+
|
|
10272
|
+
// src/ce-connect-helper.ts
|
|
10273
|
+
var CE_CONNECT_USAGE =
|
|
10274
|
+
'Usage: node connect.mjs <begin|validate|publish|decide|import|apply-ticks|render-report|ledger show [--offset N]|ledger clear|ledger unlock>'
|
|
10275
|
+
var OPS = [
|
|
10276
|
+
'begin',
|
|
10277
|
+
'validate',
|
|
10278
|
+
'publish',
|
|
10279
|
+
'decide',
|
|
10280
|
+
'import',
|
|
10281
|
+
'apply-ticks',
|
|
10282
|
+
'render-report',
|
|
10283
|
+
'ledger-show',
|
|
10284
|
+
'ledger-clear',
|
|
10285
|
+
'ledger-unlock',
|
|
10286
|
+
]
|
|
10287
|
+
function isOp(value) {
|
|
10288
|
+
return OPS.includes(value)
|
|
10289
|
+
}
|
|
10290
|
+
function isRecord6(value) {
|
|
10291
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
8117
10292
|
}
|
|
8118
10293
|
function failure2(op, reason, extra = {}) {
|
|
8119
10294
|
return {
|
|
@@ -8158,7 +10333,7 @@ function psField(pid, field) {
|
|
|
8158
10333
|
}
|
|
8159
10334
|
function procStat(pid) {
|
|
8160
10335
|
try {
|
|
8161
|
-
const stat =
|
|
10336
|
+
const stat = fs10.readFileSync(`/proc/${pid}/stat`, 'utf8')
|
|
8162
10337
|
const close = stat.lastIndexOf(')')
|
|
8163
10338
|
return close < 0
|
|
8164
10339
|
? undefined
|
|
@@ -8180,7 +10355,9 @@ var systemProcessInspector = {
|
|
|
8180
10355
|
commandOf(pid) {
|
|
8181
10356
|
if (process.platform === 'linux') {
|
|
8182
10357
|
try {
|
|
8183
|
-
return
|
|
10358
|
+
return (
|
|
10359
|
+
fs10.readFileSync(`/proc/${pid}/comm`, 'utf8').trim() || undefined
|
|
10360
|
+
)
|
|
8184
10361
|
} catch {
|
|
8185
10362
|
return
|
|
8186
10363
|
}
|
|
@@ -8189,7 +10366,7 @@ var systemProcessInspector = {
|
|
|
8189
10366
|
},
|
|
8190
10367
|
}
|
|
8191
10368
|
function isShell(command) {
|
|
8192
|
-
return SHELLS.has(
|
|
10369
|
+
return SHELLS.has(path8.basename(command).replace(/^-/, ''))
|
|
8193
10370
|
}
|
|
8194
10371
|
function findHolderPid(inspector, from = process.pid) {
|
|
8195
10372
|
let pid = inspector.parentOf(from)
|
|
@@ -8221,6 +10398,7 @@ function describeEnvironment(deps) {
|
|
|
8221
10398
|
stateRoot: resolveStateRoot(deps.env ?? process.env, homeDir),
|
|
8222
10399
|
allowedRoots: resolved.resolved.map((item) => item.mainRoot),
|
|
8223
10400
|
resolved,
|
|
10401
|
+
groups: load.groups,
|
|
8224
10402
|
warnings: load.warnings,
|
|
8225
10403
|
configUnreadable: load.unreadable,
|
|
8226
10404
|
}
|
|
@@ -8238,7 +10416,7 @@ function prepare(op, deps) {
|
|
|
8238
10416
|
}
|
|
8239
10417
|
return { ok: true, root: state.root, environment }
|
|
8240
10418
|
}
|
|
8241
|
-
var
|
|
10419
|
+
var RUN_ID2 = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
|
|
8242
10420
|
var MAX_TEXT = 200
|
|
8243
10421
|
var MAX_BATCH_PATHS = 64
|
|
8244
10422
|
var MAX_BATCH_BYTES = 1024 * 1024
|
|
@@ -8295,7 +10473,7 @@ function fingerprintIndex(projects) {
|
|
|
8295
10473
|
`
|
|
8296
10474
|
}
|
|
8297
10475
|
function parseLock(value) {
|
|
8298
|
-
if (!
|
|
10476
|
+
if (!isRecord6(value)) return
|
|
8299
10477
|
const { nonce, pid } = value
|
|
8300
10478
|
if (typeof nonce !== 'string' || !/^[0-9A-Za-z-]{8,64}$/.test(nonce)) {
|
|
8301
10479
|
return
|
|
@@ -8306,24 +10484,24 @@ function parseLock(value) {
|
|
|
8306
10484
|
return { nonce, pid }
|
|
8307
10485
|
}
|
|
8308
10486
|
function parseRunRequest(request) {
|
|
8309
|
-
if (!
|
|
10487
|
+
if (!isRecord6(request)) return
|
|
8310
10488
|
const { runId } = request
|
|
8311
|
-
if (typeof runId !== 'string' || !
|
|
10489
|
+
if (typeof runId !== 'string' || !RUN_ID2.test(runId)) return
|
|
8312
10490
|
if (runId.endsWith('.tmp')) return
|
|
8313
10491
|
const lock = parseLock(request.lock)
|
|
8314
10492
|
return lock === undefined ? undefined : { runId, lock, record: request }
|
|
8315
10493
|
}
|
|
8316
10494
|
function isSnapshot(value) {
|
|
8317
10495
|
return (
|
|
8318
|
-
|
|
10496
|
+
isRecord6(value) &&
|
|
8319
10497
|
value.version === 1 &&
|
|
8320
10498
|
typeof value.runId === 'string' &&
|
|
8321
|
-
|
|
10499
|
+
isRecord6(value.lock) &&
|
|
8322
10500
|
typeof value.lock.nonce === 'string' &&
|
|
8323
10501
|
Array.isArray(value.projects) &&
|
|
8324
10502
|
Array.isArray(value.ruleIds) &&
|
|
8325
10503
|
Array.isArray(value.ruleVersions) &&
|
|
8326
|
-
|
|
10504
|
+
isRecord6(value.offer) &&
|
|
8327
10505
|
Array.isArray(value.offer.offered) &&
|
|
8328
10506
|
Array.isArray(value.offer.fresh) &&
|
|
8329
10507
|
Array.isArray(value.provenance) &&
|
|
@@ -8333,18 +10511,18 @@ function isSnapshot(value) {
|
|
|
8333
10511
|
}
|
|
8334
10512
|
function isValidated(value) {
|
|
8335
10513
|
return (
|
|
8336
|
-
|
|
10514
|
+
isRecord6(value) &&
|
|
8337
10515
|
value.version === 1 &&
|
|
8338
10516
|
typeof value.runId === 'string' &&
|
|
8339
10517
|
Array.isArray(value.accepted) &&
|
|
8340
10518
|
Array.isArray(value.recorded) &&
|
|
8341
10519
|
Array.isArray(value.droppedCandidates) &&
|
|
8342
10520
|
Array.isArray(value.coveredRuleIds) &&
|
|
8343
|
-
|
|
10521
|
+
isRecord6(value.sweep) &&
|
|
8344
10522
|
Array.isArray(value.degradeReasons)
|
|
8345
10523
|
)
|
|
8346
10524
|
}
|
|
8347
|
-
function
|
|
10525
|
+
function readJsonFile2(dir, name, maxBytes, guard) {
|
|
8348
10526
|
const read = readWorkFile(dir, name, maxBytes)
|
|
8349
10527
|
if (read.kind !== 'bytes') return
|
|
8350
10528
|
try {
|
|
@@ -8357,9 +10535,85 @@ function readJsonFile(dir, name, maxBytes, guard) {
|
|
|
8357
10535
|
function addUnique(list, value) {
|
|
8358
10536
|
if (!list.includes(value)) list.push(value)
|
|
8359
10537
|
}
|
|
10538
|
+
function ciRunMarker(deps) {
|
|
10539
|
+
return readCiRunMarker(
|
|
10540
|
+
resolveStateRoot(deps.env ?? process.env, deps.homeDir ?? os2.homedir()),
|
|
10541
|
+
)
|
|
10542
|
+
}
|
|
10543
|
+
function invocationProjects(projects) {
|
|
10544
|
+
return projects.map((project) => ({
|
|
10545
|
+
key: project.key,
|
|
10546
|
+
name: project.name,
|
|
10547
|
+
...(project.repo === undefined ? {} : { repo: project.repo }),
|
|
10548
|
+
...(project.commit === undefined ? {} : { commit: project.commit }),
|
|
10549
|
+
}))
|
|
10550
|
+
}
|
|
10551
|
+
function beganWith(root, marker, runId) {
|
|
10552
|
+
if (runId === undefined) return
|
|
10553
|
+
const began = readInvocationRecord(root)
|
|
10554
|
+
return began?.marker === marker && began.runId === runId
|
|
10555
|
+
? began.projects
|
|
10556
|
+
: undefined
|
|
10557
|
+
}
|
|
10558
|
+
function writeTerminal(root, marker, end, deps) {
|
|
10559
|
+
const projects = end.projects ?? beganWith(root, marker, end.runId)
|
|
10560
|
+
const result = writeInvocationRecord(
|
|
10561
|
+
root,
|
|
10562
|
+
{
|
|
10563
|
+
version: 1,
|
|
10564
|
+
marker,
|
|
10565
|
+
phase: 'terminal',
|
|
10566
|
+
outcome: end.outcome,
|
|
10567
|
+
finalLine: `Connect ${end.outcome}`,
|
|
10568
|
+
...(end.reason === undefined ? {} : { reason: end.reason }),
|
|
10569
|
+
...(end.runId === undefined ? {} : { runId: end.runId }),
|
|
10570
|
+
...(projects === undefined ? {} : { projects }),
|
|
10571
|
+
},
|
|
10572
|
+
deps.invocationHooks,
|
|
10573
|
+
)
|
|
10574
|
+
return result === 'written' ? 'written' : 'unwritten'
|
|
10575
|
+
}
|
|
10576
|
+
function endInvocation(marker, result, end, deps) {
|
|
10577
|
+
const prepared = prepare('invocation', deps)
|
|
10578
|
+
const written = prepared.ok
|
|
10579
|
+
? writeTerminal(prepared.root, marker, end, deps)
|
|
10580
|
+
: 'unwritten'
|
|
10581
|
+
return written === 'written'
|
|
10582
|
+
? result
|
|
10583
|
+
: { ok: result.ok, body: { ...result.body, invocationRecord: 'unwritten' } }
|
|
10584
|
+
}
|
|
10585
|
+
function endOfFailure(result, runId) {
|
|
10586
|
+
const { outcome, reason } = result.body
|
|
10587
|
+
return {
|
|
10588
|
+
outcome: outcome === 'busy' ? 'busy' : 'failed',
|
|
10589
|
+
...(typeof reason === 'string' ? { reason } : {}),
|
|
10590
|
+
...(runId === undefined ? {} : { runId }),
|
|
10591
|
+
}
|
|
10592
|
+
}
|
|
10593
|
+
function requestRunId(request) {
|
|
10594
|
+
if (!isRecord6(request)) return
|
|
10595
|
+
const { runId } = request
|
|
10596
|
+
return typeof runId === 'string' &&
|
|
10597
|
+
RUN_ID2.test(runId) &&
|
|
10598
|
+
!runId.endsWith('.tmp')
|
|
10599
|
+
? runId
|
|
10600
|
+
: undefined
|
|
10601
|
+
}
|
|
10602
|
+
function parseSelectionWords(value) {
|
|
10603
|
+
if (value === undefined) return []
|
|
10604
|
+
if (!Array.isArray(value)) return
|
|
10605
|
+
const words = []
|
|
10606
|
+
for (const word of value) {
|
|
10607
|
+
if (typeof word !== 'string') return
|
|
10608
|
+
words.push(word)
|
|
10609
|
+
}
|
|
10610
|
+
return words
|
|
10611
|
+
}
|
|
8360
10612
|
function parseBeginRequest(request) {
|
|
8361
|
-
if (!
|
|
10613
|
+
if (!isRecord6(request)) return
|
|
8362
10614
|
const { host, version, mode, consent } = request
|
|
10615
|
+
const selection = parseSelectionWords(request.selection)
|
|
10616
|
+
if (selection === undefined) return
|
|
8363
10617
|
if (typeof host !== 'string' || host === '' || host.length > MAX_TEXT) {
|
|
8364
10618
|
return
|
|
8365
10619
|
}
|
|
@@ -8371,7 +10625,7 @@ function parseBeginRequest(request) {
|
|
|
8371
10625
|
}
|
|
8372
10626
|
if (mode !== 'interactive' && mode !== 'headless') return
|
|
8373
10627
|
if (consent !== undefined && typeof consent !== 'boolean') return
|
|
8374
|
-
return { host, version, mode, consent: consent === true }
|
|
10628
|
+
return { host, version, mode, consent: consent === true, selection }
|
|
8375
10629
|
}
|
|
8376
10630
|
var CORPUS_PREFIXES = ['docs/solutions/', 'docs/plans/', 'docs/brainstorms/']
|
|
8377
10631
|
var MAX_STALE_RECORDED = 200
|
|
@@ -8515,6 +10769,7 @@ function scanProject(entry, root, deps, limits) {
|
|
|
8515
10769
|
root: inventory.mainRoot,
|
|
8516
10770
|
key: projectKey(inventory.mainRoot),
|
|
8517
10771
|
entry,
|
|
10772
|
+
...readRepoIdentity(inventory.mainRoot),
|
|
8518
10773
|
files: inventory.files.length,
|
|
8519
10774
|
citationsUnsupported: report.unsupported,
|
|
8520
10775
|
citationsChecked: report.citations.length,
|
|
@@ -8601,7 +10856,7 @@ function gateHost(parsed, deps) {
|
|
|
8601
10856
|
function isResult(value) {
|
|
8602
10857
|
return 'body' in value
|
|
8603
10858
|
}
|
|
8604
|
-
function scanAll(environment, deps, limits, unmatched) {
|
|
10859
|
+
function scanAll(environment, selection, projects, deps, limits, unmatched) {
|
|
8605
10860
|
const degradeReasons = []
|
|
8606
10861
|
const unread = []
|
|
8607
10862
|
if (unmatched.length > 0) addUnique(degradeReasons, 'unmatched-entry')
|
|
@@ -8611,7 +10866,7 @@ function scanAll(environment, deps, limits, unmatched) {
|
|
|
8611
10866
|
}
|
|
8612
10867
|
const scans = []
|
|
8613
10868
|
let allRead = unmatched.length === 0 && !environment.configUnreadable
|
|
8614
|
-
for (const item of
|
|
10869
|
+
for (const item of projects) {
|
|
8615
10870
|
const scanned = scanProject(item.entry, item.mainRoot, deps, limits)
|
|
8616
10871
|
if ('failed' in scanned) {
|
|
8617
10872
|
allRead = false
|
|
@@ -8628,10 +10883,17 @@ function scanAll(environment, deps, limits, unmatched) {
|
|
|
8628
10883
|
.filter((scan) => scan.prunable)
|
|
8629
10884
|
.map((scan) => scan.digest.key),
|
|
8630
10885
|
retain: scans.flatMap((scan) => scan.retain),
|
|
8631
|
-
pruneUnattributed:
|
|
10886
|
+
pruneUnattributed:
|
|
10887
|
+
selection.kind === 'all' &&
|
|
10888
|
+
allRead &&
|
|
10889
|
+
scans.every((scan) => scan.prunable),
|
|
8632
10890
|
}
|
|
8633
10891
|
return { scans, degradeReasons, unread, coverageScope }
|
|
8634
10892
|
}
|
|
10893
|
+
function selectionOf(selection) {
|
|
10894
|
+
const id = selectionNamespace(selection)
|
|
10895
|
+
return id === undefined ? {} : { selection: id }
|
|
10896
|
+
}
|
|
8635
10897
|
function prunedCounts(pruned) {
|
|
8636
10898
|
return pruned.status === 'pruned'
|
|
8637
10899
|
? {
|
|
@@ -8647,9 +10909,9 @@ function beginResponse(setup, snapshot, scans, workDir, limits) {
|
|
|
8647
10909
|
runId: snapshot.runId,
|
|
8648
10910
|
stateRoot: setup.root,
|
|
8649
10911
|
workDir,
|
|
8650
|
-
rulesFile:
|
|
8651
|
-
filesIndex:
|
|
8652
|
-
rulesOverview:
|
|
10912
|
+
rulesFile: path8.join(workDir, 'rules.json'),
|
|
10913
|
+
filesIndex: path8.join(workDir, 'files.tsv'),
|
|
10914
|
+
rulesOverview: path8.join(workDir, 'rules.tsv'),
|
|
8653
10915
|
offeredRuleIds: snapshot.offer.offered,
|
|
8654
10916
|
offeredCount: snapshot.offer.offered.length,
|
|
8655
10917
|
sweep: {
|
|
@@ -8663,6 +10925,7 @@ function beginResponse(setup, snapshot, scans, workDir, limits) {
|
|
|
8663
10925
|
...(setup.reclaimed === undefined ? {} : { reclaimed: setup.reclaimed }),
|
|
8664
10926
|
host: snapshot.host,
|
|
8665
10927
|
mode: setup.parsed.mode,
|
|
10928
|
+
selection: setup.selection,
|
|
8666
10929
|
projects: scans.map((scan) => ({
|
|
8667
10930
|
root: scan.digest.root,
|
|
8668
10931
|
name: scan.digest.name,
|
|
@@ -8700,6 +10963,8 @@ function indexRun(setup, deps) {
|
|
|
8700
10963
|
const limits = bounds(deps)
|
|
8701
10964
|
const { scans, degradeReasons, unread, coverageScope } = scanAll(
|
|
8702
10965
|
setup.environment,
|
|
10966
|
+
setup.selection,
|
|
10967
|
+
setup.projects,
|
|
8703
10968
|
deps,
|
|
8704
10969
|
limits,
|
|
8705
10970
|
setup.unmatched,
|
|
@@ -8719,6 +10984,7 @@ function indexRun(setup, deps) {
|
|
|
8719
10984
|
rules,
|
|
8720
10985
|
coverage: ledger.ledger.coverage ?? [],
|
|
8721
10986
|
size: limits.maxFamiliesPerRun * limits.maxRulesPerFamily,
|
|
10987
|
+
...selectionOf(setup.selection),
|
|
8722
10988
|
})
|
|
8723
10989
|
const { parsed, verdict } = setup
|
|
8724
10990
|
const snapshot = {
|
|
@@ -8740,6 +11006,7 @@ function indexRun(setup, deps) {
|
|
|
8740
11006
|
unmatched: setup.unmatched,
|
|
8741
11007
|
degradeReasons,
|
|
8742
11008
|
unread,
|
|
11009
|
+
selection: setup.selection,
|
|
8743
11010
|
ruleIds: rules.map((rule) => rule.id),
|
|
8744
11011
|
ruleVersions: rules.map((rule) => ({
|
|
8745
11012
|
id: rule.id,
|
|
@@ -8766,22 +11033,83 @@ function indexRun(setup, deps) {
|
|
|
8766
11033
|
)
|
|
8767
11034
|
writeWorkFile(workDir, 'files.tsv', fingerprintIndex(snapshot.projects))
|
|
8768
11035
|
writeWorkFile(workDir, 'rules.tsv', rulesOverview(rules, offer))
|
|
11036
|
+
if (setup.ciMarker !== undefined) {
|
|
11037
|
+
const recorded = writeInvocationRecord(
|
|
11038
|
+
root,
|
|
11039
|
+
{
|
|
11040
|
+
version: 1,
|
|
11041
|
+
marker: setup.ciMarker,
|
|
11042
|
+
phase: 'begun',
|
|
11043
|
+
runId,
|
|
11044
|
+
projects: invocationProjects(snapshot.projects),
|
|
11045
|
+
},
|
|
11046
|
+
deps.invocationHooks,
|
|
11047
|
+
)
|
|
11048
|
+
if (recorded === 'failed') {
|
|
11049
|
+
return releaseAndFail(
|
|
11050
|
+
'begin',
|
|
11051
|
+
lock,
|
|
11052
|
+
root,
|
|
11053
|
+
runId,
|
|
11054
|
+
'invocation-write-failed',
|
|
11055
|
+
)
|
|
11056
|
+
}
|
|
11057
|
+
}
|
|
8769
11058
|
return beginResponse(setup, snapshot, scans, workDir, limits)
|
|
8770
11059
|
} catch {
|
|
8771
11060
|
return releaseAndFail('begin', lock, root, runId, 'internal-error')
|
|
8772
11061
|
}
|
|
8773
11062
|
}
|
|
11063
|
+
function selectProjects(words, environment) {
|
|
11064
|
+
const resolution = resolveSelection({
|
|
11065
|
+
words,
|
|
11066
|
+
groups: environment.groups,
|
|
11067
|
+
allowed: environment.resolved.resolved,
|
|
11068
|
+
resolveMember: (entry) =>
|
|
11069
|
+
resolveAllowEntry(entry, { homeDir: environment.homeDir }),
|
|
11070
|
+
})
|
|
11071
|
+
if (resolution.status === 'invalid') {
|
|
11072
|
+
return { ok: false, result: failure2('begin', 'invalid-request') }
|
|
11073
|
+
}
|
|
11074
|
+
if (resolution.status === 'refused') {
|
|
11075
|
+
const { reason, ...detail } = resolution.refusal
|
|
11076
|
+
return { ok: false, result: failure2('begin', reason, detail) }
|
|
11077
|
+
}
|
|
11078
|
+
return {
|
|
11079
|
+
ok: true,
|
|
11080
|
+
selection: resolution.selection,
|
|
11081
|
+
projects: resolution.projects,
|
|
11082
|
+
}
|
|
11083
|
+
}
|
|
8774
11084
|
function begin(request, deps) {
|
|
11085
|
+
const ciMarker = ciRunMarker(deps)
|
|
11086
|
+
const result = beginRun(request, deps, ciMarker)
|
|
11087
|
+
if (
|
|
11088
|
+
ciMarker === undefined ||
|
|
11089
|
+
result.ok ||
|
|
11090
|
+
result.body.outcome === 'consent-required'
|
|
11091
|
+
) {
|
|
11092
|
+
return result
|
|
11093
|
+
}
|
|
11094
|
+
return endInvocation(ciMarker, result, endOfFailure(result), deps)
|
|
11095
|
+
}
|
|
11096
|
+
function beginRun(request, deps, ciMarker) {
|
|
8775
11097
|
const parsed = parseBeginRequest(request)
|
|
8776
11098
|
if (parsed === undefined) return failure2('begin', 'invalid-request')
|
|
11099
|
+
const environment = describeEnvironment(deps)
|
|
11100
|
+
const chosen = selectProjects(parsed.selection, environment)
|
|
11101
|
+
if (!chosen.ok) return chosen.result
|
|
8777
11102
|
const verdict = gateHost(parsed, deps)
|
|
8778
11103
|
if (isResult(verdict)) return verdict
|
|
8779
|
-
const
|
|
8780
|
-
const unmatched =
|
|
8781
|
-
|
|
8782
|
-
|
|
8783
|
-
|
|
8784
|
-
|
|
11104
|
+
const { selection, projects } = chosen
|
|
11105
|
+
const unmatched =
|
|
11106
|
+
selection.kind === 'all'
|
|
11107
|
+
? environment.resolved.unmatched.map((item) => ({
|
|
11108
|
+
entry: item.entry,
|
|
11109
|
+
reason: item.reason,
|
|
11110
|
+
}))
|
|
11111
|
+
: []
|
|
11112
|
+
if (projects.length === 0) {
|
|
8785
11113
|
return failure2('begin', 'no-allowed-projects', {
|
|
8786
11114
|
unmatched,
|
|
8787
11115
|
configWarnings: environment.warnings,
|
|
@@ -8819,12 +11147,15 @@ function begin(request, deps) {
|
|
|
8819
11147
|
parsed,
|
|
8820
11148
|
verdict,
|
|
8821
11149
|
environment,
|
|
11150
|
+
selection,
|
|
11151
|
+
projects,
|
|
8822
11152
|
unmatched,
|
|
8823
11153
|
root: state.root,
|
|
8824
11154
|
now,
|
|
8825
11155
|
holder,
|
|
8826
11156
|
lock: acquired.lock,
|
|
8827
11157
|
reclaimed: acquired.reclaimed,
|
|
11158
|
+
ciMarker,
|
|
8828
11159
|
}
|
|
8829
11160
|
try {
|
|
8830
11161
|
return indexRun(setup, deps)
|
|
@@ -8845,15 +11176,15 @@ function readBatches(workDir, runId, requested) {
|
|
|
8845
11176
|
const batches = []
|
|
8846
11177
|
for (const given of requested) {
|
|
8847
11178
|
let name
|
|
8848
|
-
if (
|
|
8849
|
-
if (
|
|
11179
|
+
if (path8.isAbsolute(given)) {
|
|
11180
|
+
if (path8.dirname(path8.resolve(given)) !== workDir) {
|
|
8850
11181
|
return { ok: false, reason: 'batch-outside-work-dir' }
|
|
8851
11182
|
}
|
|
8852
|
-
name =
|
|
11183
|
+
name = path8.basename(given)
|
|
8853
11184
|
} else {
|
|
8854
11185
|
name = given
|
|
8855
11186
|
}
|
|
8856
|
-
if (!isWorkName(name) || name !==
|
|
11187
|
+
if (!isWorkName(name) || name !== path8.basename(name)) {
|
|
8857
11188
|
return { ok: false, reason: 'batch-outside-work-dir' }
|
|
8858
11189
|
}
|
|
8859
11190
|
if (RESERVED_WORK_FILES.has(name)) {
|
|
@@ -8903,11 +11234,11 @@ function contextFrom(snapshot) {
|
|
|
8903
11234
|
}
|
|
8904
11235
|
return { projects, ruleIds: new Set(snapshot.ruleIds) }
|
|
8905
11236
|
}
|
|
8906
|
-
function
|
|
11237
|
+
function count2(table, key, by = 1) {
|
|
8907
11238
|
table[key] = (table[key] ?? 0) + by
|
|
8908
11239
|
}
|
|
8909
11240
|
function recordDrop(result, family, raw, reason) {
|
|
8910
|
-
|
|
11241
|
+
count2(result.dropped, reason)
|
|
8911
11242
|
noteDropped(result, family, raw, reason)
|
|
8912
11243
|
}
|
|
8913
11244
|
function noteDropped(result, family, raw, reason) {
|
|
@@ -8961,7 +11292,7 @@ function reopensForChangedSources(record, input) {
|
|
|
8961
11292
|
return outcome.standing === 'reopened' && outcome.reason === 'sources changed'
|
|
8962
11293
|
}
|
|
8963
11294
|
function holdBack(raw, input, result) {
|
|
8964
|
-
const ruleId =
|
|
11295
|
+
const ruleId = isRecord6(raw) ? raw.ruleId : undefined
|
|
8965
11296
|
if (typeof ruleId === 'string' && input.ctx.ruleIds.has(ruleId)) {
|
|
8966
11297
|
result.heldBackRuleIds.add(ruleId)
|
|
8967
11298
|
}
|
|
@@ -9024,7 +11355,7 @@ function classifyBatches(batches, input, limits) {
|
|
|
9024
11355
|
const considered = batch.candidates.slice(0, limits.maxCandidatesPerBatch)
|
|
9025
11356
|
const over = batch.candidates.length - considered.length
|
|
9026
11357
|
if (over > 0) {
|
|
9027
|
-
|
|
11358
|
+
count2(result.dropped, 'over-batch-cap', over)
|
|
9028
11359
|
addUnique(result.degrade, 'candidate-bound')
|
|
9029
11360
|
}
|
|
9030
11361
|
for (const raw of considered) {
|
|
@@ -9113,7 +11444,7 @@ function validateResponse(runId, state, classified) {
|
|
|
9113
11444
|
})
|
|
9114
11445
|
}
|
|
9115
11446
|
function loadRun(op, workDir, runId, nonce) {
|
|
9116
|
-
const snapshot =
|
|
11447
|
+
const snapshot = readJsonFile2(
|
|
9117
11448
|
workDir,
|
|
9118
11449
|
'begin.json',
|
|
9119
11450
|
MAX_SNAPSHOT_BYTES,
|
|
@@ -9195,7 +11526,7 @@ function runValidation(snapshot, workDir, batchesRead, ledger, deps) {
|
|
|
9195
11526
|
return validateResponse(snapshot.runId, state, classified)
|
|
9196
11527
|
}
|
|
9197
11528
|
function refuseValidate(request, deps) {
|
|
9198
|
-
const lock =
|
|
11529
|
+
const lock = isRecord6(request) ? parseLock(request.lock) : undefined
|
|
9199
11530
|
if (lock === undefined) {
|
|
9200
11531
|
return failure2('validate', 'invalid-request', { lockReleased: false })
|
|
9201
11532
|
}
|
|
@@ -9204,9 +11535,9 @@ function refuseValidate(request, deps) {
|
|
|
9204
11535
|
deps.homeDir ?? os2.homedir(),
|
|
9205
11536
|
)
|
|
9206
11537
|
const runId =
|
|
9207
|
-
|
|
11538
|
+
isRecord6(request) &&
|
|
9208
11539
|
typeof request.runId === 'string' &&
|
|
9209
|
-
|
|
11540
|
+
RUN_ID2.test(request.runId) &&
|
|
9210
11541
|
!request.runId.endsWith('.tmp')
|
|
9211
11542
|
? request.runId
|
|
9212
11543
|
: undefined
|
|
@@ -9219,6 +11550,23 @@ function refuseValidate(request, deps) {
|
|
|
9219
11550
|
)
|
|
9220
11551
|
}
|
|
9221
11552
|
function validate(request, deps) {
|
|
11553
|
+
const ciMarker = ciRunMarker(deps)
|
|
11554
|
+
const result = validateRun(request, deps)
|
|
11555
|
+
if (
|
|
11556
|
+
ciMarker === undefined ||
|
|
11557
|
+
result.ok ||
|
|
11558
|
+
result.body.lockReleased !== true
|
|
11559
|
+
) {
|
|
11560
|
+
return result
|
|
11561
|
+
}
|
|
11562
|
+
return endInvocation(
|
|
11563
|
+
ciMarker,
|
|
11564
|
+
result,
|
|
11565
|
+
endOfFailure(result, requestRunId(request)),
|
|
11566
|
+
deps,
|
|
11567
|
+
)
|
|
11568
|
+
}
|
|
11569
|
+
function validateRun(request, deps) {
|
|
9222
11570
|
const parsed = parseValidateRequest(request)
|
|
9223
11571
|
if (parsed === undefined) return refuseValidate(request, deps)
|
|
9224
11572
|
const root = resolveStateRoot(
|
|
@@ -9296,6 +11644,9 @@ function digestModel(snapshot, state, outcome, degradeReasons) {
|
|
|
9296
11644
|
mode: snapshot.mode,
|
|
9297
11645
|
hostSupport: snapshot.host,
|
|
9298
11646
|
degradeReasons,
|
|
11647
|
+
...(snapshot.selection === undefined
|
|
11648
|
+
? {}
|
|
11649
|
+
: { selection: snapshot.selection }),
|
|
9299
11650
|
sweep: state.sweep,
|
|
9300
11651
|
projects,
|
|
9301
11652
|
unmatched: snapshot.unmatched,
|
|
@@ -9324,10 +11675,13 @@ function runRecord(snapshot, state, outcome, degradeReasons) {
|
|
|
9324
11675
|
? {}
|
|
9325
11676
|
: { lockReclaim: snapshot.reclaimed }),
|
|
9326
11677
|
pruned: snapshot.pruned,
|
|
11678
|
+
selection: snapshot.selection ?? { kind: 'all' },
|
|
9327
11679
|
projects: snapshot.projects.map((project) => ({
|
|
9328
11680
|
name: project.name,
|
|
9329
11681
|
root: project.root,
|
|
9330
11682
|
key: project.key,
|
|
11683
|
+
...(project.repo === undefined ? {} : { repo: project.repo }),
|
|
11684
|
+
...(project.commit === undefined ? {} : { commit: project.commit }),
|
|
9331
11685
|
files: project.files,
|
|
9332
11686
|
citationsUnsupported: project.citationsUnsupported,
|
|
9333
11687
|
citationsChecked: project.citationsChecked,
|
|
@@ -9397,7 +11751,7 @@ function runFiles(snapshot, state, outcome, degradeReasons) {
|
|
|
9397
11751
|
}
|
|
9398
11752
|
var LEDGER_NOT_WRITTEN =
|
|
9399
11753
|
'the ledger could not be written; the candidates of this run may be proposed again'
|
|
9400
|
-
function commitRun(snapshot, state, handle, root, deps) {
|
|
11754
|
+
function commitRun(snapshot, state, handle, root, deps, ciMarker) {
|
|
9401
11755
|
const degradeReasons = [...state.degradeReasons]
|
|
9402
11756
|
const outcome = degradeReasons.length > 0 ? 'degraded' : 'complete'
|
|
9403
11757
|
const first = runFiles(snapshot, state, outcome, degradeReasons)
|
|
@@ -9419,6 +11773,9 @@ function commitRun(snapshot, state, handle, root, deps) {
|
|
|
9419
11773
|
...(rule.project === undefined ? {} : { project: rule.project }),
|
|
9420
11774
|
})),
|
|
9421
11775
|
covered: state.coveredRuleIds,
|
|
11776
|
+
...(snapshot.selection === undefined
|
|
11777
|
+
? {}
|
|
11778
|
+
: selectionOf(snapshot.selection)),
|
|
9422
11779
|
...(scope === undefined
|
|
9423
11780
|
? {}
|
|
9424
11781
|
: {
|
|
@@ -9459,16 +11816,30 @@ function commitRun(snapshot, state, handle, root, deps) {
|
|
|
9459
11816
|
const released = releaseLock(handle)
|
|
9460
11817
|
removeWorkDir(root, snapshot.runId)
|
|
9461
11818
|
const finalOutcome = ledgerWritten ? outcome : 'degraded'
|
|
9462
|
-
const digestPath =
|
|
11819
|
+
const digestPath = path8.join(published.runDir, 'digest.md')
|
|
11820
|
+
const recorded =
|
|
11821
|
+
ciMarker === undefined
|
|
11822
|
+
? 'written'
|
|
11823
|
+
: writeTerminal(
|
|
11824
|
+
root,
|
|
11825
|
+
ciMarker,
|
|
11826
|
+
{
|
|
11827
|
+
outcome: finalOutcome,
|
|
11828
|
+
runId: snapshot.runId,
|
|
11829
|
+
projects: invocationProjects(snapshot.projects),
|
|
11830
|
+
},
|
|
11831
|
+
deps,
|
|
11832
|
+
)
|
|
9463
11833
|
return success('publish', {
|
|
9464
11834
|
outcome: finalOutcome,
|
|
9465
11835
|
finalLine: `Connect ${finalOutcome}`,
|
|
9466
11836
|
runId: snapshot.runId,
|
|
9467
11837
|
runDir: published.runDir,
|
|
9468
11838
|
digestPath,
|
|
9469
|
-
runRecordPath:
|
|
11839
|
+
runRecordPath: path8.join(published.runDir, 'run.json'),
|
|
9470
11840
|
ledger: published.ledger,
|
|
9471
11841
|
...(recordsCurrent ? {} : { runRecordCurrent: false }),
|
|
11842
|
+
...(recorded === 'written' ? {} : { invocationRecord: 'unwritten' }),
|
|
9472
11843
|
lockReleased: released === 'released',
|
|
9473
11844
|
degradeReasons,
|
|
9474
11845
|
candidates: state.accepted.length,
|
|
@@ -9476,11 +11847,30 @@ function commitRun(snapshot, state, handle, root, deps) {
|
|
|
9476
11847
|
summary: renderSummary(
|
|
9477
11848
|
final.model,
|
|
9478
11849
|
digestPath,
|
|
9479
|
-
|
|
11850
|
+
path8.join(published.runDir, 'run.json'),
|
|
9480
11851
|
),
|
|
9481
11852
|
})
|
|
9482
11853
|
}
|
|
9483
11854
|
function publish(request, deps) {
|
|
11855
|
+
const ciMarker = ciRunMarker(deps)
|
|
11856
|
+
const result = publishRunOp(request, deps, ciMarker)
|
|
11857
|
+
if (ciMarker === undefined) return result
|
|
11858
|
+
const runId = requestRunId(request)
|
|
11859
|
+
if (result.ok) {
|
|
11860
|
+
return result.body.outcome === 'aborted'
|
|
11861
|
+
? endInvocation(
|
|
11862
|
+
ciMarker,
|
|
11863
|
+
result,
|
|
11864
|
+
{ outcome: 'failed', reason: 'aborted', runId },
|
|
11865
|
+
deps,
|
|
11866
|
+
)
|
|
11867
|
+
: result
|
|
11868
|
+
}
|
|
11869
|
+
return result.body.lockReleased === true
|
|
11870
|
+
? endInvocation(ciMarker, result, endOfFailure(result, runId), deps)
|
|
11871
|
+
: result
|
|
11872
|
+
}
|
|
11873
|
+
function publishRunOp(request, deps, ciMarker) {
|
|
9484
11874
|
const parsed = parseRunRequest(request)
|
|
9485
11875
|
if (parsed === undefined) return failure2('publish', 'invalid-request')
|
|
9486
11876
|
const root = resolveStateRoot(
|
|
@@ -9497,7 +11887,7 @@ function publish(request, deps) {
|
|
|
9497
11887
|
const snapshot = loadRun('publish', workDir, parsed.runId, parsed.lock.nonce)
|
|
9498
11888
|
if (!isSnapshotValue(snapshot)) return snapshot
|
|
9499
11889
|
try {
|
|
9500
|
-
const state =
|
|
11890
|
+
const state = readJsonFile2(
|
|
9501
11891
|
workDir,
|
|
9502
11892
|
'validated.json',
|
|
9503
11893
|
MAX_STATE_BYTES,
|
|
@@ -9512,7 +11902,7 @@ function publish(request, deps) {
|
|
|
9512
11902
|
'not-validated',
|
|
9513
11903
|
)
|
|
9514
11904
|
}
|
|
9515
|
-
return commitRun(snapshot, state, handle, root, deps)
|
|
11905
|
+
return commitRun(snapshot, state, handle, root, deps, ciMarker)
|
|
9516
11906
|
} catch {
|
|
9517
11907
|
return releaseAndFail(
|
|
9518
11908
|
'publish',
|
|
@@ -9524,7 +11914,7 @@ function publish(request, deps) {
|
|
|
9524
11914
|
}
|
|
9525
11915
|
}
|
|
9526
11916
|
function parseDecision(request) {
|
|
9527
|
-
if (!
|
|
11917
|
+
if (!isRecord6(request)) return
|
|
9528
11918
|
const { id, status, reason, destination } = request
|
|
9529
11919
|
if (typeof id !== 'string' || typeof reason !== 'string') return
|
|
9530
11920
|
if (status !== 'promoted' && status !== 'rejected') return
|
|
@@ -9575,7 +11965,7 @@ function decide(request, deps) {
|
|
|
9575
11965
|
}
|
|
9576
11966
|
var MAX_LEDGER_ENTRIES_SHOWN = 500
|
|
9577
11967
|
function parseShowRequest(request) {
|
|
9578
|
-
if (!
|
|
11968
|
+
if (!isRecord6(request)) return
|
|
9579
11969
|
const { offset } = request
|
|
9580
11970
|
if (offset === undefined) return { offset: 0 }
|
|
9581
11971
|
if (
|
|
@@ -9587,6 +11977,67 @@ function parseShowRequest(request) {
|
|
|
9587
11977
|
}
|
|
9588
11978
|
return { offset }
|
|
9589
11979
|
}
|
|
11980
|
+
var MAX_COVERAGE_NAMESPACES_SHOWN = 50
|
|
11981
|
+
var MAX_LABEL_PICKS = 32
|
|
11982
|
+
var MAX_LABEL_TEXT = 100
|
|
11983
|
+
var LABEL_FORBIDDEN = /[\p{Cc}\p{Cf}]/u
|
|
11984
|
+
function labelText(value) {
|
|
11985
|
+
return typeof value === 'string' &&
|
|
11986
|
+
value.length > 0 &&
|
|
11987
|
+
value.length <= MAX_LABEL_TEXT &&
|
|
11988
|
+
!LABEL_FORBIDDEN.test(value)
|
|
11989
|
+
? value
|
|
11990
|
+
: undefined
|
|
11991
|
+
}
|
|
11992
|
+
function selectionLabel2(record) {
|
|
11993
|
+
if (!isRecord6(record) || !isRecord6(record.selection)) return
|
|
11994
|
+
const { kind, name, picks } = record.selection
|
|
11995
|
+
if (kind === 'group') {
|
|
11996
|
+
const text = labelText(name)
|
|
11997
|
+
return text === undefined ? undefined : `group:${text}`
|
|
11998
|
+
}
|
|
11999
|
+
if (kind !== 'projects' || !Array.isArray(picks)) return
|
|
12000
|
+
if (picks.length === 0 || picks.length > MAX_LABEL_PICKS) return
|
|
12001
|
+
const texts = picks.map(labelText)
|
|
12002
|
+
return texts.every((text) => text !== undefined)
|
|
12003
|
+
? `projects:${texts.join(',')}`
|
|
12004
|
+
: undefined
|
|
12005
|
+
}
|
|
12006
|
+
function coverageNamespaces(root, coverage) {
|
|
12007
|
+
const tallies = new Map()
|
|
12008
|
+
for (const record of coverage) {
|
|
12009
|
+
const key = record.selection ?? 'all'
|
|
12010
|
+
const tally = tallies.get(key)
|
|
12011
|
+
if (tally === undefined) {
|
|
12012
|
+
tallies.set(key, { selection: key, rules: 1, newest: record })
|
|
12013
|
+
continue
|
|
12014
|
+
}
|
|
12015
|
+
tally.rules++
|
|
12016
|
+
const newer =
|
|
12017
|
+
record.at > tally.newest.at ||
|
|
12018
|
+
(record.at === tally.newest.at && record.runId > tally.newest.runId)
|
|
12019
|
+
if (newer) tally.newest = record
|
|
12020
|
+
}
|
|
12021
|
+
const ordered = [...tallies.values()].sort((a, b) => {
|
|
12022
|
+
if (a.selection === 'all' || b.selection === 'all') {
|
|
12023
|
+
return a.selection === 'all' ? -1 : 1
|
|
12024
|
+
}
|
|
12025
|
+
if (a.newest.at !== b.newest.at) return a.newest.at < b.newest.at ? 1 : -1
|
|
12026
|
+
return a.selection < b.selection ? -1 : 1
|
|
12027
|
+
})
|
|
12028
|
+
const shown = ordered.slice(0, MAX_COVERAGE_NAMESPACES_SHOWN)
|
|
12029
|
+
const listed = shown.map((tally) => {
|
|
12030
|
+
if (tally.selection === 'all')
|
|
12031
|
+
return { selection: 'all', rules: tally.rules }
|
|
12032
|
+
const label = selectionLabel2(readRunRecord(root, tally.newest.runId))
|
|
12033
|
+
return {
|
|
12034
|
+
selection: tally.selection,
|
|
12035
|
+
rules: tally.rules,
|
|
12036
|
+
...(label === undefined ? {} : { label }),
|
|
12037
|
+
}
|
|
12038
|
+
})
|
|
12039
|
+
return { listed, omitted: ordered.length - shown.length }
|
|
12040
|
+
}
|
|
9590
12041
|
function ledgerShow(request, deps) {
|
|
9591
12042
|
const page = parseShowRequest(request)
|
|
9592
12043
|
if (page === undefined) return failure2('ledger-show', 'invalid-request')
|
|
@@ -9599,11 +12050,19 @@ function ledgerShow(request, deps) {
|
|
|
9599
12050
|
const total = read.ledger.entries.length
|
|
9600
12051
|
const end = page.offset + MAX_LEDGER_ENTRIES_SHOWN
|
|
9601
12052
|
const entries = read.ledger.entries.slice(page.offset, end)
|
|
12053
|
+
const records = read.ledger.coverage ?? []
|
|
12054
|
+
const namespaces = coverageNamespaces(prepared.root, records)
|
|
9602
12055
|
return success('ledger-show', {
|
|
9603
12056
|
total,
|
|
9604
12057
|
offset: page.offset,
|
|
9605
12058
|
nextOffset: end < total ? end : null,
|
|
9606
|
-
coverage: {
|
|
12059
|
+
coverage: {
|
|
12060
|
+
rules: records.length,
|
|
12061
|
+
namespaces: namespaces.listed,
|
|
12062
|
+
...(namespaces.omitted > 0
|
|
12063
|
+
? { namespacesOmitted: namespaces.omitted }
|
|
12064
|
+
: {}),
|
|
12065
|
+
},
|
|
9607
12066
|
entries: entries.map((entry) => ({
|
|
9608
12067
|
id: entry.id,
|
|
9609
12068
|
kind: entry.kind,
|
|
@@ -9661,6 +12120,514 @@ function ledgerUnlock(deps) {
|
|
|
9661
12120
|
: {}),
|
|
9662
12121
|
})
|
|
9663
12122
|
}
|
|
12123
|
+
var GITHUB_REPO = /^[A-Za-z0-9][A-Za-z0-9-]{0,38}\/[A-Za-z0-9._-]{1,100}$/
|
|
12124
|
+
var GITHUB_RUN_ID = /^[0-9]{1,20}$/
|
|
12125
|
+
var COMMIT_ID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/
|
|
12126
|
+
var MAX_DIRECTORY_LENGTH = 4096
|
|
12127
|
+
function parseImportRequest(request) {
|
|
12128
|
+
if (!isRecord6(request)) return
|
|
12129
|
+
const { directory, provenance } = request
|
|
12130
|
+
if (
|
|
12131
|
+
typeof directory !== 'string' ||
|
|
12132
|
+
directory === '' ||
|
|
12133
|
+
directory.length > MAX_DIRECTORY_LENGTH ||
|
|
12134
|
+
CONTROL_IN_PATH.test(directory) ||
|
|
12135
|
+
!path8.isAbsolute(directory)
|
|
12136
|
+
) {
|
|
12137
|
+
return
|
|
12138
|
+
}
|
|
12139
|
+
if (!isRecord6(provenance)) return
|
|
12140
|
+
const { repo, runId, commit } = provenance
|
|
12141
|
+
if (typeof repo !== 'string' || !GITHUB_REPO.test(repo)) return
|
|
12142
|
+
if (typeof runId !== 'string' || !GITHUB_RUN_ID.test(runId)) return
|
|
12143
|
+
if (
|
|
12144
|
+
commit !== undefined &&
|
|
12145
|
+
!(typeof commit === 'string' && COMMIT_ID.test(commit))
|
|
12146
|
+
) {
|
|
12147
|
+
return
|
|
12148
|
+
}
|
|
12149
|
+
return {
|
|
12150
|
+
directory,
|
|
12151
|
+
provenance: {
|
|
12152
|
+
repo,
|
|
12153
|
+
runId,
|
|
12154
|
+
...(commit === undefined ? {} : { commit }),
|
|
12155
|
+
},
|
|
12156
|
+
}
|
|
12157
|
+
}
|
|
12158
|
+
function importFailure(reason, extra = {}) {
|
|
12159
|
+
return failure2('import', reason, { finalLine: 'Connect failed', ...extra })
|
|
12160
|
+
}
|
|
12161
|
+
function importLocals(environment) {
|
|
12162
|
+
return environment.resolved.resolved.map(({ mainRoot }) => ({
|
|
12163
|
+
root: mainRoot,
|
|
12164
|
+
key: projectKey(mainRoot),
|
|
12165
|
+
name: projectName(mainRoot),
|
|
12166
|
+
...readRepoIdentity(mainRoot),
|
|
12167
|
+
}))
|
|
12168
|
+
}
|
|
12169
|
+
function importFacts(deps) {
|
|
12170
|
+
const limits = bounds(deps)
|
|
12171
|
+
return (root) => {
|
|
12172
|
+
if (UNSAFE_PATH_CHARACTER.test(root)) return
|
|
12173
|
+
const inventory = inventoryProject(root, {
|
|
12174
|
+
bounds: limits,
|
|
12175
|
+
...(deps.gitBinary === undefined ? {} : { gitBinary: deps.gitBinary }),
|
|
12176
|
+
})
|
|
12177
|
+
return inventory.status === 'ok'
|
|
12178
|
+
? toProjectFacts(inventory.mainRoot, inventory.files)
|
|
12179
|
+
: undefined
|
|
12180
|
+
}
|
|
12181
|
+
}
|
|
12182
|
+
var NO_PLACEMENT = {
|
|
12183
|
+
fresh: [],
|
|
12184
|
+
open: [],
|
|
12185
|
+
skipped: [],
|
|
12186
|
+
order: [],
|
|
12187
|
+
}
|
|
12188
|
+
function evidenceOf(candidate) {
|
|
12189
|
+
return JSON.stringify([
|
|
12190
|
+
candidate.target,
|
|
12191
|
+
candidate.sources,
|
|
12192
|
+
candidate.targetEvidence,
|
|
12193
|
+
candidate.counterevidence,
|
|
12194
|
+
])
|
|
12195
|
+
}
|
|
12196
|
+
function keptImportRuns(root, runs) {
|
|
12197
|
+
const out = []
|
|
12198
|
+
for (const runId of runs) {
|
|
12199
|
+
const record = readRunRecord(root, runId)
|
|
12200
|
+
if (!isRecord6(record) || record.kind !== 'import') continue
|
|
12201
|
+
const block = record.candidateBlock
|
|
12202
|
+
if (!isRecord6(block) || !Array.isArray(block.candidates)) continue
|
|
12203
|
+
const evidence = new Map()
|
|
12204
|
+
for (const item of block.candidates) {
|
|
12205
|
+
if (isRecord6(item) && typeof item.id === 'string') {
|
|
12206
|
+
evidence.set(item.id, evidenceOf(item))
|
|
12207
|
+
}
|
|
12208
|
+
}
|
|
12209
|
+
out.push({ runId, evidence })
|
|
12210
|
+
}
|
|
12211
|
+
return out
|
|
12212
|
+
}
|
|
12213
|
+
function freshOf(item, standing, extra = {}) {
|
|
12214
|
+
return {
|
|
12215
|
+
kind: 'fresh',
|
|
12216
|
+
id: item.id,
|
|
12217
|
+
standing,
|
|
12218
|
+
...(extra.refreshPointer === undefined
|
|
12219
|
+
? {}
|
|
12220
|
+
: { refreshPointer: extra.refreshPointer }),
|
|
12221
|
+
possibleMatches: extra.possibleMatches ?? [],
|
|
12222
|
+
candidate: item.candidate,
|
|
12223
|
+
record: item.record,
|
|
12224
|
+
}
|
|
12225
|
+
}
|
|
12226
|
+
function skippedOf(item, entry) {
|
|
12227
|
+
const title = item.candidate.title
|
|
12228
|
+
if (entry?.status === 'rejected') {
|
|
12229
|
+
return { id: item.id, title, reason: 'rejected-locally' }
|
|
12230
|
+
}
|
|
12231
|
+
if (entry?.status !== 'promoted') return
|
|
12232
|
+
return {
|
|
12233
|
+
id: item.id,
|
|
12234
|
+
title,
|
|
12235
|
+
reason: 'promoted-locally',
|
|
12236
|
+
...(entry.destination === undefined
|
|
12237
|
+
? {}
|
|
12238
|
+
: { refreshPointer: entry.destination }),
|
|
12239
|
+
}
|
|
12240
|
+
}
|
|
12241
|
+
function placeOne(item, ledger, heldIn) {
|
|
12242
|
+
const outcome = classifyCandidate(ledger, item.record)
|
|
12243
|
+
if (outcome.standing === 'new') {
|
|
12244
|
+
return {
|
|
12245
|
+
placed: freshOf(item, 'new', {
|
|
12246
|
+
possibleMatches: [
|
|
12247
|
+
...new Set([
|
|
12248
|
+
...outcome.possibleMatches,
|
|
12249
|
+
...findRenamedEntries(ledger, item.record, item.id),
|
|
12250
|
+
]),
|
|
12251
|
+
],
|
|
12252
|
+
}),
|
|
12253
|
+
}
|
|
12254
|
+
}
|
|
12255
|
+
if (outcome.standing === 'reopened') {
|
|
12256
|
+
return {
|
|
12257
|
+
placed: freshOf(
|
|
12258
|
+
item,
|
|
12259
|
+
outcome.reason,
|
|
12260
|
+
outcome.refreshPointer === undefined
|
|
12261
|
+
? {}
|
|
12262
|
+
: { refreshPointer: outcome.refreshPointer },
|
|
12263
|
+
),
|
|
12264
|
+
}
|
|
12265
|
+
}
|
|
12266
|
+
const skipped = skippedOf(
|
|
12267
|
+
item,
|
|
12268
|
+
ledger.entries.find((known) => known.id === item.id),
|
|
12269
|
+
)
|
|
12270
|
+
if (skipped !== undefined) return { skipped }
|
|
12271
|
+
const held = heldIn(item)
|
|
12272
|
+
return {
|
|
12273
|
+
placed:
|
|
12274
|
+
held === undefined
|
|
12275
|
+
? freshOf(item, 'new')
|
|
12276
|
+
: { kind: 'open', accepted: item, runRecordPath: held },
|
|
12277
|
+
}
|
|
12278
|
+
}
|
|
12279
|
+
function placeCandidates(accepted, ledger, root, runs) {
|
|
12280
|
+
let kept
|
|
12281
|
+
const heldIn = (item) => {
|
|
12282
|
+
kept ??= keptImportRuns(root, runs)
|
|
12283
|
+
const current = evidenceOf(item.candidate)
|
|
12284
|
+
const holder = kept.find((run) => run.evidence.get(item.id) === current)
|
|
12285
|
+
return holder === undefined
|
|
12286
|
+
? undefined
|
|
12287
|
+
: path8.join(root, 'runs', holder.runId, 'run.json')
|
|
12288
|
+
}
|
|
12289
|
+
const fresh = []
|
|
12290
|
+
const open = []
|
|
12291
|
+
const skipped = []
|
|
12292
|
+
const order = []
|
|
12293
|
+
for (const item of accepted) {
|
|
12294
|
+
const result = placeOne(item, ledger, heldIn)
|
|
12295
|
+
if ('skipped' in result) {
|
|
12296
|
+
skipped.push(result.skipped)
|
|
12297
|
+
continue
|
|
12298
|
+
}
|
|
12299
|
+
order.push(result.placed)
|
|
12300
|
+
if (result.placed.kind === 'fresh') fresh.push(result.placed)
|
|
12301
|
+
else open.push(result.placed)
|
|
12302
|
+
}
|
|
12303
|
+
return { fresh, open, skipped, order }
|
|
12304
|
+
}
|
|
12305
|
+
function countByReason(refused) {
|
|
12306
|
+
const counts = {}
|
|
12307
|
+
for (const item of refused) count2(counts, item.reason)
|
|
12308
|
+
return counts
|
|
12309
|
+
}
|
|
12310
|
+
function importRunRecord(model, createdAt, verified, placement) {
|
|
12311
|
+
return {
|
|
12312
|
+
schemaVersion: 1,
|
|
12313
|
+
runId: model.runId,
|
|
12314
|
+
createdAt,
|
|
12315
|
+
kind: 'import',
|
|
12316
|
+
outcome: model.outcome,
|
|
12317
|
+
degradeReasons: model.degradeReasons,
|
|
12318
|
+
provenance: model.provenance,
|
|
12319
|
+
source: { runId: model.sourceRunId },
|
|
12320
|
+
projects: verified.projects,
|
|
12321
|
+
counts: {
|
|
12322
|
+
candidates: placement.fresh.length,
|
|
12323
|
+
alreadyImported: placement.open.length,
|
|
12324
|
+
skipped: placement.skipped.length,
|
|
12325
|
+
refused: model.refused.length,
|
|
12326
|
+
},
|
|
12327
|
+
refused: model.refused,
|
|
12328
|
+
skipped: placement.skipped,
|
|
12329
|
+
candidateBlock: buildCandidateBlock(placement.fresh),
|
|
12330
|
+
}
|
|
12331
|
+
}
|
|
12332
|
+
function importModel(
|
|
12333
|
+
parsed,
|
|
12334
|
+
sourceRunId,
|
|
12335
|
+
verified,
|
|
12336
|
+
placement,
|
|
12337
|
+
runId,
|
|
12338
|
+
newRecordPath,
|
|
12339
|
+
) {
|
|
12340
|
+
const candidates = placement.order.map((entry) =>
|
|
12341
|
+
entry.kind === 'fresh'
|
|
12342
|
+
? { item: entry, runRecordPath: newRecordPath }
|
|
12343
|
+
: {
|
|
12344
|
+
item: {
|
|
12345
|
+
id: entry.accepted.id,
|
|
12346
|
+
standing: 'new',
|
|
12347
|
+
possibleMatches: [],
|
|
12348
|
+
candidate: entry.accepted.candidate,
|
|
12349
|
+
},
|
|
12350
|
+
runRecordPath: entry.runRecordPath,
|
|
12351
|
+
},
|
|
12352
|
+
)
|
|
12353
|
+
return {
|
|
12354
|
+
runId,
|
|
12355
|
+
outcome: verified.refused.length > 0 ? 'degraded' : 'complete',
|
|
12356
|
+
degradeReasons: verified.refused.length > 0 ? ['candidates-refused'] : [],
|
|
12357
|
+
provenance: parsed.provenance,
|
|
12358
|
+
sourceRunId,
|
|
12359
|
+
projects: verified.projects,
|
|
12360
|
+
imported: placement.fresh.length,
|
|
12361
|
+
candidates,
|
|
12362
|
+
skipped: placement.skipped,
|
|
12363
|
+
refused: verified.refused,
|
|
12364
|
+
}
|
|
12365
|
+
}
|
|
12366
|
+
function importAnswer(model, verified, written) {
|
|
12367
|
+
const runRecordPath =
|
|
12368
|
+
written === undefined ? undefined : path8.join(written.runDir, 'run.json')
|
|
12369
|
+
const digestPath =
|
|
12370
|
+
written === undefined ? undefined : path8.join(written.runDir, 'digest.md')
|
|
12371
|
+
return success('import', {
|
|
12372
|
+
outcome: model.outcome,
|
|
12373
|
+
finalLine: `Connect ${model.outcome}`,
|
|
12374
|
+
...(written === undefined || model.runId === undefined
|
|
12375
|
+
? {}
|
|
12376
|
+
: {
|
|
12377
|
+
runId: model.runId,
|
|
12378
|
+
runDir: written.runDir,
|
|
12379
|
+
runRecordPath,
|
|
12380
|
+
digestPath,
|
|
12381
|
+
}),
|
|
12382
|
+
imported: model.imported,
|
|
12383
|
+
alreadyImported: model.candidates.length - model.imported,
|
|
12384
|
+
candidates: model.candidates.map(({ item, runRecordPath: held }) => ({
|
|
12385
|
+
id: item.id,
|
|
12386
|
+
kind: item.candidate.kind,
|
|
12387
|
+
title: item.candidate.title,
|
|
12388
|
+
standing: item.standing,
|
|
12389
|
+
runRecordPath: held,
|
|
12390
|
+
})),
|
|
12391
|
+
skipped: model.skipped,
|
|
12392
|
+
refused: model.refused,
|
|
12393
|
+
refusedBy: countByReason(model.refused),
|
|
12394
|
+
projects: verified.projects,
|
|
12395
|
+
provenance: model.provenance,
|
|
12396
|
+
degradeReasons: model.degradeReasons,
|
|
12397
|
+
summary: renderImportSummary(model, digestPath),
|
|
12398
|
+
})
|
|
12399
|
+
}
|
|
12400
|
+
function insertFailure(inserted) {
|
|
12401
|
+
if (inserted.status === 'busy') {
|
|
12402
|
+
return {
|
|
12403
|
+
ok: false,
|
|
12404
|
+
body: {
|
|
12405
|
+
ok: false,
|
|
12406
|
+
op: 'import',
|
|
12407
|
+
outcome: 'busy',
|
|
12408
|
+
reason: inserted.reason,
|
|
12409
|
+
finalLine: 'Connect busy',
|
|
12410
|
+
hint: 'Another run holds the lock. If no run is active, `ledger unlock` clears it.',
|
|
12411
|
+
},
|
|
12412
|
+
}
|
|
12413
|
+
}
|
|
12414
|
+
if (inserted.status === 'failed') {
|
|
12415
|
+
return importFailure(
|
|
12416
|
+
inserted.reason === 'lock-failed' ||
|
|
12417
|
+
inserted.reason === 'ledger-unreadable'
|
|
12418
|
+
? inserted.reason
|
|
12419
|
+
: `publish-${inserted.reason}`,
|
|
12420
|
+
)
|
|
12421
|
+
}
|
|
12422
|
+
if (inserted.status === 'published' && !inserted.ledger.written) {
|
|
12423
|
+
return importFailure('ledger-write-failed', {
|
|
12424
|
+
runDir: inserted.runDir,
|
|
12425
|
+
...(inserted.runRecordCurrent === false
|
|
12426
|
+
? { runRecordCurrent: false }
|
|
12427
|
+
: {}),
|
|
12428
|
+
})
|
|
12429
|
+
}
|
|
12430
|
+
return
|
|
12431
|
+
}
|
|
12432
|
+
function importWrite(parsed, sourceRunId, verified, root, deps) {
|
|
12433
|
+
const now = deps.now ?? Date.now
|
|
12434
|
+
let placement = NO_PLACEMENT
|
|
12435
|
+
let localRunId
|
|
12436
|
+
const inserted = insertImportRun(
|
|
12437
|
+
root,
|
|
12438
|
+
(view) => {
|
|
12439
|
+
placement = placeCandidates(
|
|
12440
|
+
verified.accepted,
|
|
12441
|
+
view.ledger,
|
|
12442
|
+
root,
|
|
12443
|
+
view.importRuns,
|
|
12444
|
+
)
|
|
12445
|
+
if (placement.fresh.length === 0) return { publish: false }
|
|
12446
|
+
const runId = newImportRunId(now())
|
|
12447
|
+
localRunId = runId
|
|
12448
|
+
const createdAt = new Date(now()).toISOString()
|
|
12449
|
+
const model = importModel(
|
|
12450
|
+
parsed,
|
|
12451
|
+
sourceRunId,
|
|
12452
|
+
verified,
|
|
12453
|
+
placement,
|
|
12454
|
+
runId,
|
|
12455
|
+
path8.join(root, 'runs', runId, 'run.json'),
|
|
12456
|
+
)
|
|
12457
|
+
const filesOf = (shown) => ({
|
|
12458
|
+
'digest.md': renderImportDigest(shown, placement.fresh, createdAt),
|
|
12459
|
+
'run.json': `${JSON.stringify(importRunRecord(shown, createdAt, verified, placement), null, 2)}
|
|
12460
|
+
`,
|
|
12461
|
+
})
|
|
12462
|
+
return {
|
|
12463
|
+
publish: true,
|
|
12464
|
+
runId,
|
|
12465
|
+
files: filesOf(model),
|
|
12466
|
+
candidates: placement.fresh.map((item) => item.record),
|
|
12467
|
+
onLedgerWriteFailed: () =>
|
|
12468
|
+
filesOf({
|
|
12469
|
+
...model,
|
|
12470
|
+
outcome: 'degraded',
|
|
12471
|
+
degradeReasons: [...model.degradeReasons, 'ledger-write-failed'],
|
|
12472
|
+
}),
|
|
12473
|
+
}
|
|
12474
|
+
},
|
|
12475
|
+
{
|
|
12476
|
+
now,
|
|
12477
|
+
...(deps.processes === undefined ? {} : { processes: deps.processes }),
|
|
12478
|
+
...(deps.publishHooks === undefined ? {} : { hooks: deps.publishHooks }),
|
|
12479
|
+
},
|
|
12480
|
+
)
|
|
12481
|
+
const failed = insertFailure(inserted)
|
|
12482
|
+
if (failed !== undefined) return failed
|
|
12483
|
+
const written =
|
|
12484
|
+
inserted.status === 'published' ? { runDir: inserted.runDir } : undefined
|
|
12485
|
+
return importAnswer(
|
|
12486
|
+
importModel(
|
|
12487
|
+
parsed,
|
|
12488
|
+
sourceRunId,
|
|
12489
|
+
verified,
|
|
12490
|
+
placement,
|
|
12491
|
+
localRunId,
|
|
12492
|
+
written === undefined ? '' : path8.join(written.runDir, 'run.json'),
|
|
12493
|
+
),
|
|
12494
|
+
verified,
|
|
12495
|
+
written,
|
|
12496
|
+
)
|
|
12497
|
+
}
|
|
12498
|
+
function importRun(request, deps) {
|
|
12499
|
+
const parsed = parseImportRequest(request)
|
|
12500
|
+
if (parsed === undefined) return importFailure('invalid-request')
|
|
12501
|
+
const read = readImportRecord(parsed.directory)
|
|
12502
|
+
if (read.status === 'failed') return importFailure(read.reason)
|
|
12503
|
+
const environment = describeEnvironment(deps)
|
|
12504
|
+
if (environment.resolved.resolved.length === 0) {
|
|
12505
|
+
return importFailure('no-allowed-projects', {
|
|
12506
|
+
unmatched: environment.resolved.unmatched,
|
|
12507
|
+
configWarnings: environment.warnings,
|
|
12508
|
+
})
|
|
12509
|
+
}
|
|
12510
|
+
const verified = verifyRecord({
|
|
12511
|
+
record: read.record,
|
|
12512
|
+
locals: importLocals(environment),
|
|
12513
|
+
factsFor: importFacts(deps),
|
|
12514
|
+
})
|
|
12515
|
+
const sourceRunId = read.record.runId
|
|
12516
|
+
if (verified.accepted.length === 0) {
|
|
12517
|
+
return importAnswer(
|
|
12518
|
+
importModel(parsed, sourceRunId, verified, NO_PLACEMENT, undefined, ''),
|
|
12519
|
+
verified,
|
|
12520
|
+
undefined,
|
|
12521
|
+
)
|
|
12522
|
+
}
|
|
12523
|
+
const state = prepareStateRoot(environment.stateRoot, {
|
|
12524
|
+
allowedRoots: environment.allowedRoots,
|
|
12525
|
+
})
|
|
12526
|
+
if (state.status === 'refused') {
|
|
12527
|
+
return importFailure('state-root-refused', { detail: state.reason })
|
|
12528
|
+
}
|
|
12529
|
+
return importWrite(parsed, sourceRunId, verified, state.root, deps)
|
|
12530
|
+
}
|
|
12531
|
+
var ISSUE_DIGITS = /^[0-9]{1,10}$/
|
|
12532
|
+
function parseTicksRequest(request) {
|
|
12533
|
+
if (!isRecord6(request)) return
|
|
12534
|
+
const { bodyFile, issue } = request
|
|
12535
|
+
if (
|
|
12536
|
+
typeof bodyFile !== 'string' ||
|
|
12537
|
+
bodyFile === '' ||
|
|
12538
|
+
bodyFile.length > MAX_DIRECTORY_LENGTH ||
|
|
12539
|
+
CONTROL_IN_PATH.test(bodyFile) ||
|
|
12540
|
+
!path8.isAbsolute(bodyFile)
|
|
12541
|
+
) {
|
|
12542
|
+
return
|
|
12543
|
+
}
|
|
12544
|
+
const number =
|
|
12545
|
+
typeof issue === 'string' && ISSUE_DIGITS.test(issue)
|
|
12546
|
+
? Number(issue)
|
|
12547
|
+
: issue
|
|
12548
|
+
if (typeof number !== 'number' || !Number.isSafeInteger(number)) {
|
|
12549
|
+
return
|
|
12550
|
+
}
|
|
12551
|
+
return number < 1 ? undefined : { bodyFile, issue: number }
|
|
12552
|
+
}
|
|
12553
|
+
function parseRenderRequest(request) {
|
|
12554
|
+
if (!isRecord6(request)) return
|
|
12555
|
+
const { date, runId, attempt, cacheRestored } = request
|
|
12556
|
+
if (
|
|
12557
|
+
typeof date !== 'string' ||
|
|
12558
|
+
typeof runId !== 'string' ||
|
|
12559
|
+
typeof attempt !== 'string' ||
|
|
12560
|
+
typeof cacheRestored !== 'boolean'
|
|
12561
|
+
) {
|
|
12562
|
+
return
|
|
12563
|
+
}
|
|
12564
|
+
return { date, runId, attempt, cacheRestored }
|
|
12565
|
+
}
|
|
12566
|
+
function ciOptions(deps) {
|
|
12567
|
+
return {
|
|
12568
|
+
...(deps.processes === undefined ? {} : { processes: deps.processes }),
|
|
12569
|
+
...(deps.now === undefined ? {} : { now: deps.now }),
|
|
12570
|
+
}
|
|
12571
|
+
}
|
|
12572
|
+
function busyAnswer(op, reason, extra = {}) {
|
|
12573
|
+
return {
|
|
12574
|
+
ok: false,
|
|
12575
|
+
body: { ok: false, op, outcome: 'busy', reason, ...extra },
|
|
12576
|
+
}
|
|
12577
|
+
}
|
|
12578
|
+
function applyTicksOp(request, deps) {
|
|
12579
|
+
const parsed = parseTicksRequest(request)
|
|
12580
|
+
if (parsed === undefined) return failure2('apply-ticks', 'invalid-request')
|
|
12581
|
+
const read = readReportBody(parsed.bodyFile)
|
|
12582
|
+
if (read.status === 'failed') return failure2('apply-ticks', read.reason)
|
|
12583
|
+
const prepared = prepare('apply-ticks', deps)
|
|
12584
|
+
if (!prepared.ok) return prepared.result
|
|
12585
|
+
const result = applyTicks(
|
|
12586
|
+
prepared.root,
|
|
12587
|
+
{ body: read.body, issue: parsed.issue },
|
|
12588
|
+
ciOptions(deps),
|
|
12589
|
+
)
|
|
12590
|
+
if (result.status === 'busy') {
|
|
12591
|
+
return busyAnswer('apply-ticks', result.reason, {
|
|
12592
|
+
accepted: result.accepted,
|
|
12593
|
+
})
|
|
12594
|
+
}
|
|
12595
|
+
if (result.status === 'failed') {
|
|
12596
|
+
return failure2('apply-ticks', result.reason, { accepted: result.accepted })
|
|
12597
|
+
}
|
|
12598
|
+
return success('apply-ticks', {
|
|
12599
|
+
report: parsed.issue,
|
|
12600
|
+
section: result.section,
|
|
12601
|
+
accepted: result.accepted,
|
|
12602
|
+
skipped: result.skipped,
|
|
12603
|
+
unresolved: result.unresolved,
|
|
12604
|
+
})
|
|
12605
|
+
}
|
|
12606
|
+
function renderReportOp(request, deps) {
|
|
12607
|
+
const parsed = parseRenderRequest(request)
|
|
12608
|
+
if (parsed === undefined) return failure2('render-report', 'invalid-request')
|
|
12609
|
+
const prepared = prepare('render-report', deps)
|
|
12610
|
+
if (!prepared.ok) return prepared.result
|
|
12611
|
+
const result = renderReport(prepared.root, parsed, ciOptions(deps))
|
|
12612
|
+
if (result.status === 'busy')
|
|
12613
|
+
return busyAnswer('render-report', result.reason)
|
|
12614
|
+
if (result.status === 'failed') {
|
|
12615
|
+
return failure2('render-report', result.reason)
|
|
12616
|
+
}
|
|
12617
|
+
return success('render-report', {
|
|
12618
|
+
bodyFile: result.bodyFile,
|
|
12619
|
+
bodyChars: result.body.length,
|
|
12620
|
+
manifest: result.manifest,
|
|
12621
|
+
open: result.open,
|
|
12622
|
+
shown: result.shown,
|
|
12623
|
+
hidden: result.hidden,
|
|
12624
|
+
fresh: result.fresh,
|
|
12625
|
+
fetches: result.fetches,
|
|
12626
|
+
outcome: result.outcome,
|
|
12627
|
+
red: result.red,
|
|
12628
|
+
stateUpdated: result.stateUpdated,
|
|
12629
|
+
})
|
|
12630
|
+
}
|
|
9664
12631
|
function runHelperOp(op, request, deps = {}) {
|
|
9665
12632
|
if (!isOp(op)) return failure2(op, 'unknown-op')
|
|
9666
12633
|
try {
|
|
@@ -9673,6 +12640,12 @@ function runHelperOp(op, request, deps = {}) {
|
|
|
9673
12640
|
return publish(request, deps)
|
|
9674
12641
|
case 'decide':
|
|
9675
12642
|
return decide(request, deps)
|
|
12643
|
+
case 'import':
|
|
12644
|
+
return importRun(request, deps)
|
|
12645
|
+
case 'apply-ticks':
|
|
12646
|
+
return applyTicksOp(request, deps)
|
|
12647
|
+
case 'render-report':
|
|
12648
|
+
return renderReportOp(request, deps)
|
|
9676
12649
|
case 'ledger-show':
|
|
9677
12650
|
return ledgerShow(request, deps)
|
|
9678
12651
|
case 'ledger-clear':
|
|
@@ -9693,7 +12666,7 @@ function errorCodeOf(error) {
|
|
|
9693
12666
|
}
|
|
9694
12667
|
function readStdinChunk(chunk) {
|
|
9695
12668
|
try {
|
|
9696
|
-
return
|
|
12669
|
+
return fs10.readSync(0, chunk, 0, chunk.length, null)
|
|
9697
12670
|
} catch (error) {
|
|
9698
12671
|
const code = errorCodeOf(error)
|
|
9699
12672
|
if (code === 'EOF') return 0
|
|
@@ -9717,7 +12690,15 @@ function readStdinBounded() {
|
|
|
9717
12690
|
}
|
|
9718
12691
|
return Buffer.concat(chunks).toString('utf8')
|
|
9719
12692
|
}
|
|
9720
|
-
var REQUEST_OPS = new Set([
|
|
12693
|
+
var REQUEST_OPS = new Set([
|
|
12694
|
+
'begin',
|
|
12695
|
+
'validate',
|
|
12696
|
+
'publish',
|
|
12697
|
+
'decide',
|
|
12698
|
+
'import',
|
|
12699
|
+
'apply-ticks',
|
|
12700
|
+
'render-report',
|
|
12701
|
+
])
|
|
9721
12702
|
function resolveCliOp(argv) {
|
|
9722
12703
|
const [first, second] = argv
|
|
9723
12704
|
if (first === 'ledger') {
|
|
@@ -9737,24 +12718,35 @@ function showRequestFromArgv(argv) {
|
|
|
9737
12718
|
? { offset: Number(value) }
|
|
9738
12719
|
: { offset: value }
|
|
9739
12720
|
}
|
|
12721
|
+
function refusedBegin(result, options) {
|
|
12722
|
+
const deps = options.deps ?? {}
|
|
12723
|
+
const marker = ciRunMarker(deps)
|
|
12724
|
+
return marker === undefined
|
|
12725
|
+
? result
|
|
12726
|
+
: endInvocation(marker, result, endOfFailure(result), deps)
|
|
12727
|
+
}
|
|
9740
12728
|
function readCliRequest(op, options, out, err) {
|
|
9741
12729
|
if (!REQUEST_OPS.has(op)) return { ok: true, request: {} }
|
|
9742
12730
|
if ((options.isTTY ?? process.stdin.isTTY) === true) {
|
|
9743
12731
|
err(
|
|
9744
12732
|
`${op} reads one JSON request from stdin; interactive input is not supported`,
|
|
9745
12733
|
)
|
|
12734
|
+
if (op === 'begin') {
|
|
12735
|
+
refusedBegin(failure2(op, 'request-interactive'), options)
|
|
12736
|
+
}
|
|
9746
12737
|
return { ok: false, exitCode: 2 }
|
|
9747
12738
|
}
|
|
9748
12739
|
const text = (options.readRequest ?? readStdinBounded)()
|
|
9749
12740
|
const refuse = (reason) => {
|
|
12741
|
+
const refused = failure2(
|
|
12742
|
+
op,
|
|
12743
|
+
reason,
|
|
12744
|
+
op === 'validate' ? { lockReleased: false } : {},
|
|
12745
|
+
)
|
|
9750
12746
|
out(
|
|
9751
|
-
JSON.stringify(
|
|
9752
|
-
|
|
9753
|
-
|
|
9754
|
-
outcome: 'failed',
|
|
9755
|
-
reason,
|
|
9756
|
-
...(op === 'validate' ? { lockReleased: false } : {}),
|
|
9757
|
-
}),
|
|
12747
|
+
JSON.stringify(
|
|
12748
|
+
(op === 'begin' ? refusedBegin(refused, options) : refused).body,
|
|
12749
|
+
),
|
|
9758
12750
|
)
|
|
9759
12751
|
return { ok: false, exitCode: 2 }
|
|
9760
12752
|
}
|