Skip to content

Commit 252856d

Browse files
committed
feat(executor): add drift executor (cron/CI layer)
The executor wraps the verifier's POSIX sh fallback (run-verifier.sh) and adds: 1-hour idempotency window (skip-if-recent), concurrent-run safety (flock + mktemp lock file), exit code propagation (0=clean, 1=drift, 2=missing prereq), and timestamped logging (reports/session-drift.log). Default invocation is --no-llm (deterministic detection + POSIX sh template renderer, no LLM cost). Set ENFORCEMENT_USE_LLM=1 to opt into the LLM-formatted report path. The shipped GitHub Actions workflow (.github/workflows/enforcement-executor.yml) has 3 triggers: - workflow_run: chained after the eval (conformance check) workflow completes on push to main - schedule: weekly on Monday at 09:00 UTC - workflow_dispatch: manual Workflow uses Node.js 24 LTS (v20 reached EOL 2026-03-24), pinned to actions/checkout@v6.0.3, actions/setup-node@v6.4.0, actions/upload-artifact@v7.0.1. permissions: contents: read, actions: read. concurrency and timeout-minutes: 5 configured for safety. All env vars use the ENFORCEMENT_* prefix. The verifier's no-LLM path is the default for cron/CI. Conformance: 10/10 PASS verified (test-executor.sh). Includes basic run, env-var override, idempotency window, lock file cleanup, missing prereq, empty run, workflow YAML validation, 3-trigger check, --no-llm default, idempotency state file. Task: T-007
1 parent 1ab4c6e commit 252856d

5 files changed

Lines changed: 693 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: enforcement-executor
2+
3+
# The executor runs the drift verifier on a schedule and
4+
# after each push to main (chained after the conformance
5+
# check completes). Default invocation is --no-llm
6+
# (deterministic, no LLM cost). Set ENFORCEMENT_USE_LLM=1
7+
# to opt into the LLM-formatted report path.
8+
9+
on:
10+
# Chained trigger: run after the eval workflow completes
11+
# on push to main. The eval workflow is the conformance
12+
# check (per .github/workflows/eval.yml).
13+
workflow_run:
14+
workflows: ["eval"]
15+
types: [completed]
16+
branches: [main]
17+
# Scheduled trigger: weekly on Monday at 09:00 UTC, as a
18+
# safety net for the workflow_run trigger (covers the case
19+
# where the eval workflow is disabled or skipped).
20+
schedule:
21+
- cron: "0 9 * * 1"
22+
# Manual trigger: for ad-hoc runs.
23+
workflow_dispatch:
24+
25+
# Cancel any in-progress run of this workflow on the same
26+
# ref. Prevents runaway executor runs if multiple triggers
27+
# fire close together.
28+
concurrency:
29+
group: enforcement-executor-${{ github.ref }}
30+
cancel-in-progress: true
31+
32+
# 5-minute timeout: a verifier run should take 5-10
33+
# seconds. 5 minutes is generous (allows for slow runners
34+
# or a few retries). Default 360 minutes is too long.
35+
timeout-minutes: 5
36+
37+
# Least-privilege permissions. The executor only needs
38+
# to checkout the repo and write the artifact. No
39+
# contents:write (the report is committed to the repo
40+
# by the consumer's workflow, not by this action).
41+
permissions:
42+
contents: read
43+
actions: read
44+
45+
jobs:
46+
executor:
47+
runs-on: ubuntu-latest
48+
# Skip the executor if the upstream eval workflow was
49+
# cancelled (e.g., a fast-follow commit) or failed. We
50+
# still want the artifact upload to fire (if: always()
51+
# below), so the upload happens regardless of the
52+
# executor's exit code.
53+
if: >
54+
github.event_name != 'workflow_run' ||
55+
github.event.workflow_run.conclusion == 'success'
56+
steps:
57+
- name: Checkout
58+
uses: actions/checkout@v6.0.3
59+
60+
- name: Setup Node.js
61+
uses: actions/setup-node@v6.4.0
62+
with:
63+
# Node.js 24 LTS (v20 is EOL as of 2026-03-24).
64+
# The verifier's no-LLM path uses `tsx` (Node-only)
65+
# to run the deterministic algorithm.
66+
node-version: '24'
67+
68+
- name: Install tsx
69+
# The verifier's no-LLM path invokes `tsx
70+
# verifier-core.ts`. Install tsx globally so the
71+
# verifier can find it on PATH.
72+
run: npm install -g tsx
73+
74+
- name: Run executor
75+
# The executor script is universal — it works for
76+
# any agent. It calls the verifier's POSIX sh
77+
# fallback (scripts/run-verifier.sh, which the
78+
# verifier's bootstrap installs to that path).
79+
# Default mode is --no-llm (no LLM cost).
80+
# Set ENFORCEMENT_USE_LLM=1 to opt into the LLM path.
81+
env:
82+
ENFORCEMENT_MODE: ci
83+
ENFORCEMENT_VERIFIER_SCRIPT: scripts/run-verifier.sh
84+
run: |
85+
chmod +x enforcement/executor/run-executor.sh
86+
./enforcement/executor/run-executor.sh
87+
88+
- name: Upload drift report as artifact
89+
# `if: always()` so the artifact is uploaded even
90+
# if the executor failed (the report is still
91+
# useful for debugging). `if-no-files-found: ignore`
92+
# so an empty/missing report doesn't fail the run.
93+
if: always()
94+
uses: actions/upload-artifact@v7.0.1
95+
with:
96+
name: enforcement-drift-report
97+
path: reports/session-drift.md
98+
if-no-files-found: ignore
99+
# 30-day retention: nightly reports accumulate
100+
# quickly; 30 days is enough for trend analysis.
101+
retention-days: 30

‎enforcement/eval/test-executor.sh‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
#!/bin/sh
2+
# enforcement/eval/test-executor.sh — conformance test for
3+
# the local enforcement executor. 10 cases; 10/10 PASS
4+
# expected.
5+
#
6+
# Tests the run-executor.sh script in isolation (the
7+
# GitHub Actions workflow itself is tested by the workflow
8+
# YAML schema check, not by this script).
9+
#
10+
# Usage: sh enforcement/eval/test-executor.sh
11+
# (run from the playbook repo root, not the consumer repo)
12+
13+
set -e
14+
15+
# Resolve paths relative to the script location.
16+
TEST_DIR="$(cd "$(dirname "$0")" && pwd)"
17+
PLAYBOOK_ROOT="$(cd "$TEST_DIR/../.." && pwd)"
18+
EXECUTOR="$PLAYBOOK_ROOT/enforcement/executor/run-executor.sh"
19+
WORKFLOW="$PLAYBOOK_ROOT/.github/workflows/enforcement-executor.yml"
20+
21+
command -v git >/dev/null 2>&1 || \
22+
{ printf 'test-executor.sh: git not in PATH.\n' >&2; exit 1; }
23+
[ -x "$EXECUTOR" ] || \
24+
{ printf 'test-executor.sh: executor not found at %s\n' "$EXECUTOR" >&2; exit 1; }
25+
26+
PASS=0
27+
FAIL=0
28+
REPO=""
29+
30+
# Helper: create a clean temp repo with a fake verifier
31+
# script. Each test case gets a fresh repo to avoid
32+
# state leakage between cases.
33+
setup_repo() {
34+
REPO=$(mktemp -d -t enforcement-executor-test-XXXXXX)
35+
cd "$REPO"
36+
git init -q
37+
git config user.email "t@t"
38+
git config user.name "T"
39+
mkdir -p scripts
40+
cat > scripts/run-verifier.sh <<'EOF'
41+
#!/bin/sh
42+
# Fake verifier that records a report and exits 0.
43+
mkdir -p reports
44+
printf "# Session drift report\n\n## Header\n\n- **Timestamp:** %s\n- **Range:** root..HEAD\n- **Total commits analyzed:** 0\n\n## Summary\n\n- **Drift count:** 0\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > reports/session-drift.md
45+
exit 0
46+
EOF
47+
chmod +x scripts/run-verifier.sh
48+
}
49+
50+
# Helper: cleanup the temp repo.
51+
cleanup_repo() {
52+
if [ -n "$REPO" ] && [ -d "$REPO" ]; then
53+
rm -rf "$REPO"
54+
fi
55+
REPO=""
56+
}
57+
58+
# Track all cleanup needs
59+
ALL_CLEANUPS=""
60+
add_cleanup() { ALL_CLEANUPS="$ALL_CLEANUPS $1"; }
61+
run_cleanups() {
62+
for d in $ALL_CLEANUPS; do
63+
[ -n "$d" ] && [ -d "$d" ] && rm -rf "$d"
64+
done
65+
ALL_CLEANUPS=""
66+
}
67+
trap run_cleanups EXIT
68+
69+
# check <name> <expected: pass|fail> <command>
70+
check() {
71+
if eval "$3" 2>/dev/null; then o=pass; else o=fail; fi
72+
if [ "$o" = "$2" ]; then
73+
PASS=$((PASS+1))
74+
printf ' PASS: %s\n' "$1"
75+
else
76+
FAIL=$((FAIL+1))
77+
printf ' FAIL: %s\n' "$1"
78+
fi
79+
}
80+
81+
# Case 1: basic run — verifier runs, report produced
82+
setup_repo
83+
check "basic run" pass \
84+
"ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' && [ -f reports/session-drift.md ]"
85+
cleanup_repo
86+
87+
# Case 2: env-var override — ENFORCEMENT_VERIFIER_SCRIPT
88+
# points to a non-existent script. The executor should
89+
# exit non-zero with a "verifier script not found" error.
90+
setup_repo
91+
check "env-var override" pass \
92+
"! ENFORCEMENT_VERIFIER_SCRIPT=scripts/does-not-exist.sh ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' 2>/dev/null"
93+
cleanup_repo
94+
95+
# Case 3: idempotency window — second run within 1h is skipped
96+
setup_repo
97+
ENFORCEMENT_SKIP_IF_RECENT=0 "$EXECUTOR" >/dev/null 2>&1
98+
if ENFORCEMENT_SKIP_IF_RECENT=1 "$EXECUTOR" 2>&1 | grep -q 'skipping'; then
99+
PASS=$((PASS+1))
100+
printf ' PASS: %s\n' "idempotency window"
101+
else
102+
FAIL=$((FAIL+1))
103+
printf ' FAIL: %s\n' "idempotency window"
104+
fi
105+
cleanup_repo
106+
107+
# Case 4: lock file cleanup — the script creates a lock
108+
# file via mktemp and cleans it up via the EXIT trap.
109+
# We verify the trap fires by checking that the lock
110+
# file is gone after the run.
111+
setup_repo
112+
ENFORCEMENT_SKIP_IF_RECENT=0 "$EXECUTOR" >/dev/null 2>&1
113+
# The lock file is in /tmp with prefix enforcement-executor.
114+
LOCK_FILES=$(ls /tmp/enforcement-executor.* 2>/dev/null | wc -l)
115+
if [ "$LOCK_FILES" -eq 0 ]; then
116+
PASS=$((PASS+1))
117+
printf ' PASS: %s\n' "lock file cleanup"
118+
else
119+
FAIL=$((FAIL+1))
120+
printf ' FAIL: %s\n' "lock file cleanup ($LOCK_FILES leftover)"
121+
fi
122+
cleanup_repo
123+
124+
# Case 5: missing verifier script — executor exits non-zero
125+
setup_repo
126+
rm scripts/run-verifier.sh
127+
check "missing verifier script" pass \
128+
"! ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' 2>/dev/null"
129+
cleanup_repo
130+
131+
# Case 6: empty run (0 drift) — produces a report, exits 0
132+
setup_repo
133+
check "empty run produces report" pass \
134+
"ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' && [ -s reports/session-drift.md ]"
135+
cleanup_repo
136+
137+
# Case 7: workflow file present and valid YAML
138+
if [ -f "$WORKFLOW" ]; then
139+
if /data/data/com.termux/files/usr/bin/python3 -c "
140+
import yaml
141+
class L(yaml.SafeLoader): pass
142+
def on_constructor(loader, node):
143+
return loader.construct_scalar(node)
144+
L.add_constructor('tag:yaml.org,2002:bool', on_constructor)
145+
d = yaml.load(open('$WORKFLOW'), Loader=L)
146+
assert 'jobs' in d and 'executor' in d['jobs'], 'jobs.executor missing'
147+
assert 'timeout-minutes' in d, 'top-level timeout-minutes missing'
148+
assert d['timeout-minutes'] <= 5, 'timeout too long'
149+
print('workflow OK')
150+
" 2>/dev/null; then
151+
PASS=$((PASS+1))
152+
printf ' PASS: %s\n' "workflow file valid YAML"
153+
else
154+
FAIL=$((FAIL+1))
155+
printf ' FAIL: %s\n' "workflow file valid YAML"
156+
fi
157+
else
158+
FAIL=$((FAIL+1))
159+
printf ' FAIL: %s\n' "workflow file not found at $WORKFLOW"
160+
fi
161+
162+
# Case 8: workflow has the 3 required triggers
163+
if [ -f "$WORKFLOW" ]; then
164+
if /data/data/com.termux/files/usr/bin/python3 -c "
165+
import yaml
166+
class L(yaml.SafeLoader): pass
167+
def on_constructor(loader, node):
168+
return loader.construct_scalar(node)
169+
L.add_constructor('tag:yaml.org,2002:bool', on_constructor)
170+
d = yaml.load(open('$WORKFLOW'), Loader=L)
171+
triggers = list(d['on'].keys())
172+
required = ['workflow_run', 'schedule', 'workflow_dispatch']
173+
missing = [t for t in required if t not in triggers]
174+
assert not missing, f'missing triggers: {missing}'
175+
print('all 3 triggers present')
176+
" 2>/dev/null; then
177+
PASS=$((PASS+1))
178+
printf ' PASS: %s\n' "workflow has 3 required triggers"
179+
else
180+
FAIL=$((FAIL+1))
181+
printf ' FAIL: %s\n' "workflow has 3 required triggers"
182+
fi
183+
else
184+
FAIL=$((FAIL+1))
185+
printf ' FAIL: %s\n' "workflow file not found at $WORKFLOW"
186+
fi
187+
188+
# Case 9: --no-llm is the default — the executor runs
189+
# successfully without ENFORCEMENT_USE_LLM=1
190+
setup_repo
191+
check "--no-llm is the default" pass \
192+
"ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' 2>&1 | grep -q 'use_llm=0'"
193+
cleanup_repo
194+
195+
# Case 10: idempotency state file is created/updated
196+
setup_repo
197+
rm -f reports/session-drift.log
198+
ENFORCEMENT_SKIP_IF_RECENT=0 "$EXECUTOR" >/dev/null 2>&1
199+
check "idempotency state file created" pass \
200+
"[ -f reports/session-drift.log ] && grep -q 'executor starting' reports/session-drift.log"
201+
cleanup_repo
202+
203+
printf '\nResult: %d PASS, %d FAIL\n' "$PASS" "$FAIL"
204+
[ "$FAIL" -eq 0 ]

0 commit comments

Comments
 (0)