Skip to content

[formal-spec] compiler-threat-detection-compliance/README.md — Formal model & test suite — 2026-10-15 #69062

Closed as not planned

Description

@github-actions

Summary

The compliance map in specs/compiler-threat-detection-compliance/README.md is a crosswalk from rule IDs (CTR-*) to test IDs (T-CTR-*). Test IDs share one sequence with the Section 6.4 and 6.6 norm tests, so rule and test numbers drift apart after CTR-023. Earlier runs formalized the bijection, suppression lifecycle and deprecation stubs. This run formalizes the shared-ID-space layer that was left open: the allocation gap, the disjointness of rule tests and norm tests, monotonicity of the crosswalk, and consistency with the norm tables. The tests parse the README directly.

Specification

  • File: specs/compiler-threat-detection-compliance/README.md
  • Focus area: Rule-to-test crosswalk and shared T-CTR ID-space allocation
  • Formal notation used: Z3/SMT-style set constraints + Lean 4-style lemmas (illustrative)

Formal Model

Predicates and invariants (illustrative notation)
-- Sets parsed from the README
R   := { r | row (r, t) in crosswalk table }      -- rule IDs
T   := { t | row (r, t) in crosswalk table }      -- rule test IDs
N   := { t | row t in 6.4 table or 6.6 table }    -- norm test IDs
f   : R -> T                                      -- crosswalk

-- P1 CrosswalkFunctional ("one required T-CTR-* test ID per rule")
forall r. |{ t | (r,t) in crosswalk }| = 1
-- P2 CrosswalkInjective (no two rules share a test ID)
forall r1 r2. f(r1) = f(r2) -> r1 = r2
-- P3 IDFormat
forall r in R. r ~ ^CTR-[0-9]{3}$ ; forall t in T u N. t ~ ^T-CTR-[0-9]{3}$
-- P4 RuleNormDisjoint ("Test IDs are allocated from a single sequence")
T /\ N = {}
-- P5 NormRangeReserved
N = {T-CTR-024..038} u {T-CTR-040}
-- P6 IdentityPrefix
forall r with num(r) <= 23. f(r) = T-CTR-num(r)
-- P7 MonotoneCrosswalk
forall r1 r2. num(r1) < num(r2) -> num(f(r1)) < num(f(r2))
-- P8 ShiftedRange (CTR-025..033 -> 039, 041..048)
f(CTR-025)=T-CTR-039 /\ forall n in 026..033. f(CTR-n)=T-CTR-(n+15)
-- P9 UnusedRuleSlot (CTR-024 absent from crosswalk; its test slot is a norm)
CTR-024 not in R /\ T-CTR-024 in N
-- P10 NoGapInAllocation (T u N is gap-free from 001 to max)
T u N = { T-CTR-001 .. T-CTR-max(T u N) }
-- P11 NormTableNoDuplicates
forall t in N. appears exactly once

Behavioral Coverage Map

Predicate / Invariant Test Function Description
CrosswalkFunctional TestFormal_CrosswalkEachRuleHasOneTestID Every rule appears once in the crosswalk
CrosswalkInjective TestFormal_CrosswalkTestIDsUnique No test ID is used by two rules
IDFormat TestFormal_CrosswalkIDFormat Rule/test IDs match the strict regexes
RuleNormDisjoint TestFormal_RuleTestsDisjointFromNormTests Rule tests never reuse norm IDs
NormRangeReserved TestFormal_NormTestIDRange Norm IDs are exactly 024..038 and 040
IdentityPrefix / ShiftedRange TestFormal_CrosswalkKnownMappings CTR-001..023 are identity; CTR-025..033 are shifted
MonotoneCrosswalk TestFormal_CrosswalkMonotone Order of rules is preserved by the map
UnusedRuleSlot TestFormal_CTR024AbsentFromCrosswalk CTR-024 has no rule row
NoGapInAllocation TestFormal_SharedIDSpaceGapFree Rule + norm IDs cover 001..max

Generated Test Suite

📄 `pkg/workflow/compiler_threat_detection_compliance_crosswalk_formal_test.go`
(go/redacted):build !integration

// Formal tests for specs/compiler-threat-detection-compliance/README.md
// Encodes: CrosswalkFunctional, CrosswalkInjective, IDFormat, RuleNormDisjoint,
// NormRangeReserved, IdentityPrefix, ShiftedRange, MonotoneCrosswalk,
// UnusedRuleSlot, NoGapInAllocation.

package workflow

import (
	"os"
	"path/filepath"
	"regexp"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var (
	crosswalkRowRe = regexp.MustCompile(`^\| (CTR-\d{3}) \| (T-CTR-\d{3}) \|$`)
	normRowRe      = regexp.MustCompile(`^\| (T-CTR-\d{3}) \| .+ \|$`)
	ruleRe         = regexp.MustCompile(`^CTR-\d{3}$`)
	testRe         = regexp.MustCompile(`^T-CTR-\d{3}$`)
)

type crosswalkRow struct{ rule, test string }

func crosswalkNum(t *testing.T, id string) int {
	t.Helper()
	n, err := strconv.Atoi(id[strings.LastIndex(id, "-")+1:])
	require.NoError(t, err, "ID %q should end in a number", id)
	return n
}

func loadCrosswalk(t *testing.T) ([]crosswalkRow, []string) {
	t.Helper()
	_, thisFile, _, ok := runtime.Caller(0)
	require.True(t, ok, "runtime.Caller should succeed")
	data, err := os.ReadFile(filepath.Join(filepath.Dir(thisFile), "..", "..", "specs", "compiler-threat-detection-compliance", "README.md"))
	require.NoError(t, err, "compliance README should be readable")

	var rows []crosswalkRow
	var norms []string
	for _, line := range strings.Split(string(data), "\n") {
		line = strings.TrimSpace(line)
		if m := crosswalkRowRe.FindStringSubmatch(line); m != nil {
			rows = append(rows, crosswalkRow{m[1], m[2]})
		} else if m := normRowRe.FindStringSubmatch(line); m != nil {
			norms = append(norms, m[1])
		}
	}
	require.NotEmpty(t, rows, "crosswalk table should have rows")
	require.NotEmpty(t, norms, "norm tables should have rows")
	return rows, norms
}

func TestFormal_CrosswalkEachRuleHasOneTestID(t *testing.T) {
	rows, _ := loadCrosswalk(t)
	seen := map[string]int{}
	for _, r := range rows {
		seen[r.rule]++
	}
	for rule, n := range seen {
		assert.Equal(t, 1, n, "rule %s should appear exactly once in the crosswalk", rule)
	}
}

func TestFormal_CrosswalkTestIDsUnique(t *testing.T) {
	rows, _ := loadCrosswalk(t)
	seen := map[string]string{}
	for _, r := range rows {
		prev, dup := seen[r.test]
		assert.False(t, dup, "test ID %s is used by both %s and %s", r.test, prev, r.rule)
		seen[r.test] = r.rule
	}
}

func TestFormal_CrosswalkIDFormat(t *testing.T) {
	rows, norms := loadCrosswalk(t)
	for _, r := range rows {
		assert.Regexp(t, ruleRe, r.rule, "rule ID %q should be well formed", r.rule)
		assert.Regexp(t, testRe, r.test, "test ID %q should be well formed", r.test)
	}
	for _, n := range norms {
		assert.Regexp(t, testRe, n, "norm test ID %q should be well formed", n)
	}
}

func TestFormal_RuleTestsDisjointFromNormTests(t *testing.T) {
	rows, norms := loadCrosswalk(t)
	normSet := map[string]bool{}
	for _, n := range norms {
		normSet[n] = true
	}
	for _, r := range rows {
		assert.False(t, normSet[r.test], "rule test %s must not reuse a Section 6 norm test ID", r.test)
	}
}

func TestFormal_NormTestIDRange(t *testing.T) {
	_, norms := loadCrosswalk(t)
	expected := map[int]bool{40: true}
	for i := 24; i <= 38; i++ {
		expected[i] = true
	}
	got := map[int]int{}
	for _, n := range norms {
		got[crosswalkNum(t, n)]++
	}
	for num, count := range got {
		assert.True(t, expected[num], "T-CTR-%03d is not an allowed norm test ID", num)
		assert.Equal(t, 1, count, "T-CTR-%03d should appear once in norm tables", num)
	}
	for num := range expected {
		assert.Contains(t, got, num, "norm test T-CTR-%03d should be listed", num)
	}
}

func TestFormal_CrosswalkKnownMappings(t *testing.T) {
	rows, _ := loadCrosswalk(t)
	m := map[string]string{}
	for _, r := range rows {
		m[r.rule] = r.test
	}
	for i := 1; i <= 23; i++ {
		assert.Equal(t, "T-CTR-"+leftPad3(i), m["CTR-"+leftPad3(i)], "CTR-%03d should map to the identity test ID", i)
	}
	assert.Equal(t, "T-CTR-039", m["CTR-025"], "CTR-025 should map to T-CTR-039")
	for i := 26; i <= 33; i++ {
		assert.Equal(t, "T-CTR-"+leftPad3(i+15), m["CTR-"+leftPad3(i)], "CTR-%03d should map to T-CTR-%03d", i, i+15)
	}
}

func leftPad3(n int) string {
	s := strconv.Itoa(n)
	for len(s) < 3 {
		s = "0" + s
	}
	return s
}

func TestFormal_CrosswalkMonotone(t *testing.T) {
	rows, _ := loadCrosswalk(t)
	sorted := append([]crosswalkRow(nil), rows...)
	sort.Slice(sorted, func(i, j int) bool { return crosswalkNum(t, sorted[i].rule) < crosswalkNum(t, sorted[j].rule) })
	for i := 1; i < len(sorted); i++ {
		assert.Less(t, crosswalkNum(t, sorted[i-1].test), crosswalkNum(t, sorted[i].test),
			"test IDs should increase with rule IDs (%s -> %s)", sorted[i-1].rule, sorted[i].rule)
	}
}

func TestFormal_CTR024AbsentFromCrosswalk(t *testing.T) {
	rows, norms := loadCrosswalk(t)
	for _, r := range rows {
		assert.NotEqual(t, "CTR-024", r.rule, "CTR-024 has no active rule row")
	}
	assert.Contains(t, norms, "T-CTR-024", "T-CTR-024 slot should be held by a norm test")
}

func TestFormal_SharedIDSpaceGapFree(t *testing.T) {
	rows, norms := loadCrosswalk(t)
	used := map[int]bool{}
	max := 0
	for _, r := range rows {
		n := crosswalkNum(t, r.test)
		used[n] = true
		if n > max {
			max = n
		}
	}
	for _, n := range norms {
		num := crosswalkNum(t, n)
		used[num] = true
		if num > max {
			max = num
		}
	}
	for i := 1; i <= max; i++ {
		assert.True(t, used[i], "T-CTR-%03d should be allocated (shared sequence must be gap-free)", i)
	}
}

Usage

  1. Copy the test file to pkg/workflow/.
  2. The README is parsed directly, so no stubs are needed.
  3. Run: go test ./pkg/workflow/ -run Formal

Note: the gap-free predicate assumes the README tables remain contiguous. If an ID is intentionally retired, relax P10 accordingly. The tests were reviewed visually only and were not compiled.

Context

Generated by 🔬 Daily Formal Spec Verifier · copilot · auto · 24.3 AIC · ⌖ 0.742 AIC · ⊞ 10.5K · ◷

  • expires on Oct 22, 2026, 7:38 AM UTC-08:00

Activity

  1. github-actions commented on Oct 22, 2026

    @github-actions
    ContributorAuthor

    This issue was automatically closed because it expired on 2026-10-22T15:38:27.727Z.

    Closed by Workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions