Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(ci): validate service-axis metadata without losing coverage

Alfredo Lopez52c37a02ab1e
diff --git a/scripts/dev/check-test-services.cjs b/scripts/dev/check-test-services.cjs
index 1d8bf7d04..f7169df01 100644
--- a/scripts/dev/check-test-services.cjs
+++ b/scripts/dev/check-test-services.cjs
@@ -5,19 +5,97 @@ const fs = require('node:fs')
const path = require('node:path')
// The CI contract is a literal service list. Refuse expressions or a changed
-// matrix shape instead of silently checking a partial list. The parser relies
-// on the current 6/8/10-space indentation and on `service:` being the matrix's
-// only key; changing either makes the check fail rather than pass.
+// matrix shape instead of silently checking a partial list. The parser reads
+// only the consecutive literal entries under `matrix.service`; sibling matrix
+// metadata such as `include` ends that collection.
function checkServices(workflow, localServices) {
const matrices = [...(workflow + '\n').matchAll(/^ matrix:\n((?: {8}.*\n|\n)*)/gm)]
- const lists = matrices.map((match) => match[1].split('\n').filter((line) => line.trim() && !line.trim().startsWith('#')))
- .filter((lines) => lines.some((line) => /^ service:/.test(line)))
- if (lists.length !== 1 || lists[0][0] !== ' service:' || lists[0].length < 2) throw new Error('Expected one literal CI service matrix')
- const services = lists[0].slice(1).map((line) => {
- const match = line.match(/^ - ([a-z0-9][a-z0-9/-]*)\s*$/)
- if (!match || match[1].split('/').some((part) => !part)) throw new Error('Unsupported CI service entry')
- return match[1]
- })
+ const lists = []
+ for (const match of matrices) {
+ const lines = match[1].split('\n').filter((line) => line.trim() && !line.trim().startsWith('#'))
+ const serviceKeys = lines.reduce((keys, line, index) => {
+ if (/^ {8}service:(?:\s|$)/.test(line)) keys.push(index)
+ return keys
+ }, [])
+ if (serviceKeys.length > 1) throw new Error('Expected one literal CI service matrix')
+ const serviceKey = serviceKeys[0] ?? -1
+ if (serviceKey === -1) continue
+ if (serviceKey !== 0 || lines[0] !== ' service:') {
+ throw new Error('Expected one literal CI service matrix')
+ }
+
+ const services = []
+ let cursor = 1
+ while (cursor < lines.length) {
+ const line = lines[cursor]
+ if (/^ - /.test(line)) {
+ const entry = line.match(/^ - ([a-z0-9][a-z0-9/-]*)\s*$/)
+ if (!entry || entry[1].split('/').some((part) => !part)) {
+ throw new Error('Unsupported CI service entry')
+ }
+ services.push(entry[1])
+ cursor++
+ continue
+ }
+ // A key at the matrix level or dedent ends the service list. Deeper or
+ // differently indented content inside the list is a malformed entry.
+ if (/^ {8}\S/.test(line) || /^ {0,7}\S/.test(line)) break
+ throw new Error('Unsupported CI service entry')
+ }
+ if (services.length < 1) throw new Error('Expected one literal CI service matrix')
+ const metadata = lines.slice(cursor)
+ if (metadata.some((line) => /^ {8}exclude:/.test(line))) {
+ throw new Error('Unsupported CI matrix metadata: exclude removes required service coverage')
+ }
+ const includeKeys = metadata.filter((line) => /^ {8}include:/.test(line))
+ if (includeKeys.length > 1 || (includeKeys.length === 1 && includeKeys[0] !== ' include:')) {
+ throw new Error('Unsupported CI matrix metadata')
+ }
+ if (includeKeys.length === 1) {
+ const includeIndex = metadata.indexOf(includeKeys[0])
+ const includeEntries = []
+ let serviceValues = null
+ const finishEntry = () => {
+ if (serviceValues === null) return
+ if (serviceValues.length !== 1) throw new Error('Unsupported CI include entry: expected one service property')
+ includeEntries.push(serviceValues[0])
+ serviceValues = null
+ }
+ const readProperty = (text) => {
+ const property = text.match(/^([A-Za-z_][A-Za-z0-9_-]*):(?:\s+(.*))?$/)
+ if (!property) throw new Error(`Unsupported CI include entry: ${text}`)
+ if (property[1] !== 'service') return
+ const service = property[2].match(/^[a-z0-9][a-z0-9/-]*$/)
+ if (!service || service[0].split('/').some((part) => !part)) {
+ throw new Error(`Unsupported CI include service: ${property[2]}`)
+ }
+ serviceValues.push(service[0])
+ }
+ for (const line of metadata.slice(includeIndex + 1)) {
+ if (/^ {8}\S/.test(line)) break
+ if (/^ - /.test(line)) {
+ finishEntry()
+ serviceValues = []
+ readProperty(line.slice(12))
+ continue
+ }
+ if (serviceValues === null) throw new Error('Unsupported CI include entry')
+ if (/^ {14}/.test(line)) continue
+ if (!/^ {12}\S/.test(line)) throw new Error('Unsupported CI include entry')
+ readProperty(line.slice(12))
+ }
+ finishEntry()
+ if (includeEntries.length === 0) throw new Error('Unsupported CI include entry: no entries')
+ for (const service of includeEntries) {
+ if (!services.includes(service)) {
+ throw new Error(`Include-only CI service: ${service}`)
+ }
+ }
+ }
+ lists.push(services)
+ }
+ if (lists.length !== 1) throw new Error('Expected one literal CI service matrix')
+ const services = lists[0]
for (const [label, entries] of [['CI', services], ['Makefile', localServices]]) {
if (new Set(entries).size !== entries.length) throw new Error(`${label} contains duplicate services`)
}
diff --git a/scripts/tests/test-test-services.cjs b/scripts/tests/test-test-services.cjs
index 89d246b43..54cfb8e13 100644
--- a/scripts/tests/test-test-services.cjs
+++ b/scripts/tests/test-test-services.cjs
@@ -4,6 +4,47 @@ const { test } = require('node:test')
const assert = require('node:assert/strict')
const { checkServices } = require('../dev/check-test-services.cjs')
const workflow = ' matrix:\n service:\n - one\n - packages/two\n\n steps:\n'
+const singleServiceWorkflow = ' matrix:\n service:\n - one\n\n steps:\n'
+const workflowWithMetadata = workflow.replace(
+ '\n steps:',
+ '\n include:\n - pinned_suites: one.test.ts\n service: one\n - service: one\n pinned_suites: one.test.ts\n max-parallel: 2\n steps:'
+)
+const workflowWithDuplicateServiceAxis = workflow.replace(
+ '\n steps:',
+ '\n include:\n - service: one\n service:\n - two\n steps:'
+)
+const workflowWithUnknownLaterIncludeService = workflowWithMetadata.replace(
+ ' service: one',
+ ' service: fixture-only'
+)
+const workflowWithDynamicLaterIncludeService = workflowWithMetadata.replace(
+ ' service: one',
+ ' service: ${{ inputs.service }}'
+)
+const workflowWithFlowFirstInclude = workflowWithMetadata.replace(
+ ' - pinned_suites: one.test.ts\n service: one',
+ ' - { service: fixture-only }'
+)
+const workflowWithFlowLaterInclude = workflowWithMetadata.replace(
+ ' - pinned_suites: one.test.ts\n service: one',
+ ' - { pinned_suites: one.test.ts, service: fixture-only }'
+)
+const workflowWithAliasInclude = workflowWithMetadata.replace(
+ ' - pinned_suites: one.test.ts\n service: one',
+ ' - &include_defaults\n service: fixture-only'
+)
+const workflowWithDuplicateIncludeService = workflowWithMetadata.replace(
+ ' - pinned_suites: one.test.ts\n service: one',
+ ' - pinned_suites: one.test.ts\n service: one\n service: fixture-only'
+)
+const workflowWithDynamicIncludeHeader = workflowWithMetadata.replace(
+ ' include:',
+ ' include: ${{ inputs.includes }}'
+)
+const workflowWithExclude = workflow.replace(
+ '\n steps:',
+ '\n exclude:\n - service: packages/two\n steps:'
+)
test('matrix parity is independent of order', () => {
assert.equal(checkServices(workflow, ['packages/two', 'one']), 2)
@@ -19,7 +60,61 @@ test('dynamic, missing and ambiguous matrices fail closed', () => {
assert.throws(() => checkServices(workflow.replace('packages/two', '${{ inputs.service }}'), ['one']), /Unsupported/)
assert.throws(() => checkServices('', []), /Expected one/)
assert.throws(() => checkServices(workflow + workflow, []), /Expected one/)
- assert.throws(() => checkServices(workflow.replace('\n steps:', ' exclude:\n - service: one\n steps:'), ['one', 'packages/two']), /Unsupported/)
+})
+test('matrix service collection ends at sibling metadata', () => {
+ assert.equal(checkServices(workflowWithMetadata, ['one', 'packages/two']), 2)
+})
+test('a literal one-service axis is valid', () => {
+ assert.equal(checkServices(singleServiceWorkflow, ['one']), 1)
+})
+test('include services must be static members of the service axis', () => {
+ assert.throws(
+ () => checkServices(workflowWithUnknownLaterIncludeService, ['one', 'packages/two']),
+ /Include-only CI service: fixture-only/
+ )
+ assert.throws(
+ () => checkServices(workflowWithDynamicLaterIncludeService, ['one', 'packages/two']),
+ /Unsupported CI include service/
+ )
+ for (const unsupported of [workflowWithFlowFirstInclude, workflowWithFlowLaterInclude, workflowWithAliasInclude]) {
+ assert.throws(() => checkServices(unsupported, ['one', 'packages/two']), /Unsupported CI include entry/)
+ }
+ assert.throws(
+ () => checkServices(workflowWithDuplicateIncludeService, ['one', 'packages/two']),
+ /Unsupported CI include entry/
+ )
+ assert.throws(
+ () => checkServices(workflowWithDynamicIncludeHeader, ['one', 'packages/two']),
+ /Unsupported CI matrix metadata/
+ )
+})
+test('matrix exclude that removes required coverage fails closed', () => {
+ assert.throws(() => checkServices(workflowWithExclude, ['one', 'packages/two']), /Unsupported CI matrix metadata/)
+})
+test('duplicate service axes in one matrix fail closed', () => {
+ assert.throws(
+ () => checkServices(workflowWithDuplicateServiceAxis, ['one', 'packages/two']),
+ /Expected one/
+ )
+})
+test('malformed entries and indentation still fail closed', () => {
+ assert.throws(() => checkServices(workflow.replace('packages/two', 'packages/two?'), ['one']), /Unsupported/)
+ assert.throws(
+ () =>
+ checkServices(
+ workflow.replace('\n - packages/two', '\n - packages/two'),
+ ['one']
+ ),
+ /Unsupported/
+ )
+ assert.throws(
+ () =>
+ checkServices(
+ workflow.replace('\n service:', '\n include:\n service:'),
+ ['one', 'packages/two']
+ ),
+ /Expected one/
+ )
})
test('empty, inline and re-indented matrices fail closed', () => {
assert.throws(() => checkServices(' matrix:\n service:\n\n steps:\n', []), /Expected one/)