@miracle352/statute 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +319 -2
- package/action.yml +356 -0
- package/bin/hook.js +81 -0
- package/bin/init.js +142 -0
- package/bin/mcp.js +166 -0
- package/bin/scope.js +78 -0
- package/bin/shots.js +52 -0
- package/bin/statute.js +362 -0
- package/examples/expect/newsletter_test.md +22 -0
- package/examples/expect/output-newsletter/.internal/execution.json +2053 -0
- package/examples/expect/output-newsletter/.internal/meta.json +39 -0
- package/examples/expect/output-newsletter/.internal/steps/1-d2368411/flows/0/actions.ndjson +2 -0
- package/examples/expect/output-newsletter/.internal/steps/2-a9e761f8/flows/0/actions.ndjson +6 -0
- package/examples/expect/output-newsletter/.internal/steps/3-63305a01/flows/0/actions.ndjson +2 -0
- package/examples/expect/output-newsletter/Result.md +21 -0
- package/examples/expect/search_test.md +20 -0
- package/laws/README.md +24 -0
- package/laws/accept_still_works_test.md +12 -0
- package/laws/cancel_as_easy_as_signup_test.md +15 -0
- package/laws/output-reject_means_reject/.internal/execution.json +1279 -0
- package/laws/output-reject_means_reject/.internal/meta.json +18 -0
- package/laws/output-reject_means_reject/.internal/steps/1-567cc234/flows/0/actions.ndjson +6 -0
- package/laws/output-reject_means_reject/.internal/steps/2-e3c06462/flows/0/actions.ndjson +1 -0
- package/laws/output-reject_means_reject/.internal/steps/3-3040a29b/flows/0/actions.ndjson +2 -0
- package/laws/output-reject_means_reject/Result.md +21 -0
- package/laws/price_shown_is_price_paid_test.md +12 -0
- package/laws/reject_means_reject_test.md +15 -0
- package/package.json +65 -4
- package/replays/cancel-demo-shop/evidence/cancel-1794754943192.evidence +0 -0
- package/replays/cancel-demo-shop/evidence/cancel-1794755110410.evidence +0 -0
- package/replays/cancel-demo-shop/loop.json +32 -0
- package/replays/demo-shop/evidence/consent-1794751413210.evidence +0 -0
- package/replays/demo-shop/evidence/consent-1794751523524.evidence +0 -0
- package/replays/demo-shop/evidence/consent-accept-1794751588395.evidence +0 -0
- package/replays/demo-shop/loop.json +40 -0
- package/replays/haxcms/evidence/consent-1794831086682.evidence +0 -0
- package/replays/haxcms/evidence/consent-1794831262757.evidence +0 -0
- package/replays/haxcms/evidence/consent-accept-1794831347132.evidence +0 -0
- package/replays/haxcms/loop.json +56 -0
- package/replays/in-the-wake-check/consent.json +67 -0
- package/replays/in-the-wake-check/consent.md +32 -0
- package/replays/kane-testmd-demo-shop/evidence/consent-1794935266000.evidence +0 -0
- package/replays/kane-testmd-demo-shop/loop.json +18 -0
- package/replays/lumen-pr-2/evidence/consent-1794929771799.evidence +0 -0
- package/replays/lumen-pr-2/evidence/consent-1794929845913.evidence +0 -0
- package/replays/lumen-pr-2/evidence/consent-accept-1794929886905.evidence +0 -0
- package/replays/lumen-pr-2/loop.json +57 -0
- package/replays/newsletter-expect/evidence/newsletter_test-round1-1795518837738.evidence +0 -0
- package/replays/newsletter-expect/evidence/newsletter_test-round2-1795519079029.evidence +0 -0
- package/replays/newsletter-expect/loop.json +99 -0
- package/replays/price-demo-shop/evidence/price-1794837093949.evidence +0 -0
- package/replays/price-demo-shop/evidence/price-1794837311639.evidence +0 -0
- package/replays/price-demo-shop/loop.json +43 -0
- package/rules/trackers.json +61 -0
- package/site/assets/fonts/OFL-archivo.txt +93 -0
- package/site/assets/fonts/OFL-geist.txt +93 -0
- package/site/assets/fonts/OFL-instrument-serif.txt +93 -0
- package/site/assets/fonts/archivo.woff2 +0 -0
- package/site/assets/fonts/geist.woff2 +0 -0
- package/site/assets/fonts/instrument-serif-italic.woff2 +0 -0
- package/site/assets/fonts/instrument-serif.woff2 +0 -0
- package/site/assets/fonts/inter.woff2 +0 -0
- package/site/assets/fonts/jetbrains-mono.woff2 +0 -0
- package/skill/statute/SKILL.md +57 -0
- package/src/agent.js +70 -0
- package/src/app.js +41 -0
- package/src/evidence.js +80 -0
- package/src/expect-cli.js +296 -0
- package/src/expect-view.js +79 -0
- package/src/expect.js +392 -0
- package/src/infer.js +32 -0
- package/src/kane.js +124 -0
- package/src/pack.js +128 -0
- package/src/packs/cancel.js +83 -0
- package/src/packs/consent-accept.js +57 -0
- package/src/packs/consent.js +237 -0
- package/src/packs/price.js +104 -0
- package/src/page.js +360 -0
- package/src/report.js +59 -0
- package/src/zip.js +38 -0
package/action.yml
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
name: Statute
|
|
2
|
+
description: Kane CLI checks a site against consumer law in a real browser; code judges the evidence. Fails on a violation.
|
|
3
|
+
author: miracle352
|
|
4
|
+
branding:
|
|
5
|
+
icon: check-circle
|
|
6
|
+
color: green
|
|
7
|
+
|
|
8
|
+
inputs:
|
|
9
|
+
url:
|
|
10
|
+
description: Address Kane should visit (a PR preview URL, your production site, or a local URL used with `start`).
|
|
11
|
+
required: true
|
|
12
|
+
check:
|
|
13
|
+
description: >
|
|
14
|
+
Which checks to run: consent, price, cancel, several separated by spaces, or auto.
|
|
15
|
+
auto reads the pull request diff and runs only the checks the change could affect
|
|
16
|
+
(needs actions/checkout with fetch-depth 0). A change that touches none of them runs nothing and costs no Kane credits.
|
|
17
|
+
default: consent
|
|
18
|
+
tests:
|
|
19
|
+
description: >
|
|
20
|
+
Kane CLI test files with expectations to run as well, for example `tests/kane/*_test.md`
|
|
21
|
+
(separate several patterns with spaces). Each test's expectations (in a comment Kane ignores)
|
|
22
|
+
are checked against what Kane recorded: requests, data leaving the site, cookies, errors, numbers.
|
|
23
|
+
With check auto, they run only when the pull request changes code (not only docs).
|
|
24
|
+
default: ""
|
|
25
|
+
start:
|
|
26
|
+
description: Optional command that starts your app on the runner before Kane visits (for example `npm start`).
|
|
27
|
+
default: ""
|
|
28
|
+
lt-username:
|
|
29
|
+
description: TestMu AI username, for Kane CLI. Pass it from a repository secret.
|
|
30
|
+
required: true
|
|
31
|
+
lt-access-key:
|
|
32
|
+
description: TestMu AI access key, for Kane CLI. Pass it from a repository secret.
|
|
33
|
+
required: true
|
|
34
|
+
fail-on:
|
|
35
|
+
description: "violation (default) fails the job on a violation; never only reports."
|
|
36
|
+
default: violation
|
|
37
|
+
comment:
|
|
38
|
+
description: "Post the verdict as a comment on the pull request (needs pull-requests: write)."
|
|
39
|
+
default: "true"
|
|
40
|
+
fix:
|
|
41
|
+
description: >
|
|
42
|
+
On a pull request with a violation, hand Statute's report to Claude Code, let it fix the app,
|
|
43
|
+
and send Kane back. The fix is committed to the pull request only if Kane passes.
|
|
44
|
+
Needs `start` (the app must run on the runner), `claude-token`, contents: write,
|
|
45
|
+
and actions/checkout set to the pull request branch (ref: github.head_ref). Pull requests from forks are only checked.
|
|
46
|
+
default: "false"
|
|
47
|
+
claude-token:
|
|
48
|
+
description: Claude Code token for `fix` (run `claude setup-token`). Pass it from a repository secret.
|
|
49
|
+
default: ""
|
|
50
|
+
app:
|
|
51
|
+
description: Folder Claude Code may edit when fixing. Defaults to the whole repository.
|
|
52
|
+
default: "."
|
|
53
|
+
rounds:
|
|
54
|
+
description: Most fix attempts before giving up.
|
|
55
|
+
default: "3"
|
|
56
|
+
screenshots:
|
|
57
|
+
description: >
|
|
58
|
+
Show Kane's screenshots in the pull request comment or watch issue. They are stored on a
|
|
59
|
+
`statute-shots` branch of this repository (needs contents: write and actions/checkout).
|
|
60
|
+
default: "true"
|
|
61
|
+
issue:
|
|
62
|
+
description: >
|
|
63
|
+
Outside pull requests (a schedule, a manual run), open an issue when a check fails and close it
|
|
64
|
+
once the checks pass again (needs issues: write). Use it to watch your live site.
|
|
65
|
+
default: "false"
|
|
66
|
+
github-token:
|
|
67
|
+
description: Token used to comment and to open or close issues.
|
|
68
|
+
default: ${{ github.token }}
|
|
69
|
+
|
|
70
|
+
outputs:
|
|
71
|
+
verdict:
|
|
72
|
+
description: pass, violation, inconclusive or skipped
|
|
73
|
+
value: ${{ steps.run.outputs.verdict }}
|
|
74
|
+
checks:
|
|
75
|
+
description: The checks that ran, separated by spaces
|
|
76
|
+
value: ${{ steps.plan.outputs.checks }}
|
|
77
|
+
|
|
78
|
+
runs:
|
|
79
|
+
using: composite
|
|
80
|
+
steps:
|
|
81
|
+
- uses: actions/setup-node@v4
|
|
82
|
+
with:
|
|
83
|
+
node-version: 22
|
|
84
|
+
|
|
85
|
+
- name: Pick the checks
|
|
86
|
+
id: plan
|
|
87
|
+
shell: bash
|
|
88
|
+
env:
|
|
89
|
+
STATUTE_CHECK: ${{ inputs.check }}
|
|
90
|
+
STATUTE_TESTS: ${{ inputs.tests }}
|
|
91
|
+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
|
|
92
|
+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
|
|
93
|
+
LT_USERNAME: ${{ inputs.lt-username }}
|
|
94
|
+
LT_ACCESS_KEY: ${{ inputs.lt-access-key }}
|
|
95
|
+
run: |
|
|
96
|
+
checks="${STATUTE_CHECK//,/ }"
|
|
97
|
+
# A commit Statute pushed itself was already proven by Kane in the run that made it.
|
|
98
|
+
if [ -n "$HEAD_SHA" ] && [ "$(git log -1 --format=%ae "$HEAD_SHA" 2>/dev/null)" = "statute-bot@users.noreply.github.com" ]; then
|
|
99
|
+
echo "The latest commit is Statute's own fix, already proven by Kane. Nothing to run."
|
|
100
|
+
echo "self=true" >> "$GITHUB_OUTPUT"
|
|
101
|
+
echo "checks=" >> "$GITHUB_OUTPUT"
|
|
102
|
+
exit 0
|
|
103
|
+
fi
|
|
104
|
+
if [ -z "$LT_USERNAME" ] || [ -z "$LT_ACCESS_KEY" ]; then
|
|
105
|
+
echo "::warning title=Statute::No Kane CLI credentials in this run, so Kane was not started. Pull requests from forks don't receive repository secrets."
|
|
106
|
+
echo "nocreds=true" >> "$GITHUB_OUTPUT"
|
|
107
|
+
echo "checks=" >> "$GITHUB_OUTPUT"
|
|
108
|
+
exit 0
|
|
109
|
+
fi
|
|
110
|
+
if [ "$checks" = "auto" ]; then
|
|
111
|
+
if [ -n "$BASE_SHA" ] && git cat-file -e "$BASE_SHA" 2>/dev/null; then
|
|
112
|
+
git diff "$BASE_SHA"...HEAD > "$RUNNER_TEMP/statute.diff"
|
|
113
|
+
node "$GITHUB_ACTION_PATH/bin/scope.js" --json < "$RUNNER_TEMP/statute.diff" > "$RUNNER_TEMP/statute-scope.json"
|
|
114
|
+
checks=$(node -e 'console.log(require(process.argv[1]).checks.join(" "))' "$RUNNER_TEMP/statute-scope.json")
|
|
115
|
+
else
|
|
116
|
+
echo "::warning title=Statute::check is auto but the pull request diff is not available (use actions/checkout with fetch-depth: 0). Running every check."
|
|
117
|
+
checks="consent price cancel"
|
|
118
|
+
fi
|
|
119
|
+
fi
|
|
120
|
+
tests=""
|
|
121
|
+
if [ -n "$STATUTE_TESTS" ]; then
|
|
122
|
+
shopt -s nullglob globstar
|
|
123
|
+
for pat in $STATUTE_TESTS; do for f in $pat; do
|
|
124
|
+
grep -qi '<!--[[:space:]]*expect' "$f" && tests="$tests $f" || echo "::warning title=Statute::$f has no expectations (<!-- expect ... -->), skipped."
|
|
125
|
+
done; done
|
|
126
|
+
if [ "$STATUTE_CHECK" = "auto" ] && [ -f "$RUNNER_TEMP/statute-scope.json" ]; then
|
|
127
|
+
code_changed=$(node -e 'console.log(require(process.argv[1]).changed.length)' "$RUNNER_TEMP/statute-scope.json")
|
|
128
|
+
if [ "$code_changed" = 0 ]; then tests=""; echo "Only docs changed, so the Kane tests were not run."; fi
|
|
129
|
+
fi
|
|
130
|
+
fi
|
|
131
|
+
tests="${tests# }"
|
|
132
|
+
echo "tests=$tests" >> "$GITHUB_OUTPUT"
|
|
133
|
+
[ -n "$tests" ] && [ -z "$checks" ] && checks="none"
|
|
134
|
+
echo "checks=$checks" >> "$GITHUB_OUTPUT"
|
|
135
|
+
echo "Checks to run: ${checks:-none}${tests:+; Kane tests: $tests}"
|
|
136
|
+
|
|
137
|
+
- name: Install Kane CLI
|
|
138
|
+
if: steps.plan.outputs.checks != ''
|
|
139
|
+
shell: bash
|
|
140
|
+
run: npm install -g @testmuai/kane-cli
|
|
141
|
+
|
|
142
|
+
- name: Install Claude Code
|
|
143
|
+
if: steps.plan.outputs.checks != '' && inputs.fix == 'true' && github.event_name == 'pull_request' && inputs.claude-token != '' && inputs.start != ''
|
|
144
|
+
shell: bash
|
|
145
|
+
run: npm install -g @anthropic-ai/claude-code && echo "STATUTE_FIX=1" >> "$GITHUB_ENV"
|
|
146
|
+
|
|
147
|
+
- name: Run the checks
|
|
148
|
+
id: run
|
|
149
|
+
if: steps.plan.outputs.self != 'true'
|
|
150
|
+
shell: bash
|
|
151
|
+
env:
|
|
152
|
+
LT_USERNAME: ${{ inputs.lt-username }}
|
|
153
|
+
LT_ACCESS_KEY: ${{ inputs.lt-access-key }}
|
|
154
|
+
STATUTE_CHECKS: ${{ steps.plan.outputs.checks }}
|
|
155
|
+
STATUTE_TEST_FILES: ${{ steps.plan.outputs.tests }}
|
|
156
|
+
STATUTE_NOCREDS: ${{ steps.plan.outputs.nocreds }}
|
|
157
|
+
STATUTE_URL: ${{ inputs.url }}
|
|
158
|
+
STATUTE_START: ${{ inputs.start }}
|
|
159
|
+
STATUTE_APP: ${{ inputs.app }}
|
|
160
|
+
STATUTE_ROUNDS: ${{ inputs.rounds }}
|
|
161
|
+
CLAUDE_CODE_OAUTH_TOKEN: ${{ inputs.claude-token }}
|
|
162
|
+
run: |
|
|
163
|
+
set +e
|
|
164
|
+
out="$RUNNER_TEMP/statute-report"; mkdir -p "$out"
|
|
165
|
+
summary="$out/summary.md"; : > "$summary"
|
|
166
|
+
if [ "$STATUTE_NOCREDS" = true ]; then
|
|
167
|
+
echo "verdict=skipped" >> "$GITHUB_OUTPUT"
|
|
168
|
+
printf '## Statute: not run\n\nThis run has no Kane CLI credentials, so Kane could not visit the app. Pull requests from forks do not receive repository secrets; a maintainer can run the check from a branch in this repository.\n' > "$summary"
|
|
169
|
+
cat "$summary" >> "$GITHUB_STEP_SUMMARY"
|
|
170
|
+
exit 0
|
|
171
|
+
fi
|
|
172
|
+
if [ -z "$STATUTE_CHECKS" ]; then
|
|
173
|
+
echo "verdict=skipped" >> "$GITHUB_OUTPUT"
|
|
174
|
+
{
|
|
175
|
+
echo "## Statute: nothing to check"
|
|
176
|
+
echo
|
|
177
|
+
echo "Nothing in this change touches tracking, prices or subscriptions, so Kane was not run. 0 credits used."
|
|
178
|
+
} > "$summary"
|
|
179
|
+
if [ -f "$RUNNER_TEMP/statute-scope.json" ]; then
|
|
180
|
+
echo >> "$summary"
|
|
181
|
+
node -e 'const r=require(process.argv[1]);const l=r.changed.slice(0,8).map(f=>"`"+f+"`").join(", ");console.log("Changed: "+(l||"no files")+(r.changed.length>8?", and "+(r.changed.length-8)+" more":"")+".")' "$RUNNER_TEMP/statute-scope.json" >> "$summary"
|
|
182
|
+
fi
|
|
183
|
+
cat "$summary" >> "$GITHUB_STEP_SUMMARY"
|
|
184
|
+
exit 0
|
|
185
|
+
fi
|
|
186
|
+
worst=pass
|
|
187
|
+
fixed=""
|
|
188
|
+
[ "$STATUTE_CHECKS" = none ] && STATUTE_CHECKS=""
|
|
189
|
+
for c in $STATUTE_CHECKS; do
|
|
190
|
+
dir="$out"
|
|
191
|
+
if [ "$STATUTE_FIX" = 1 ]; then
|
|
192
|
+
dir="$out/$c"; mkdir -p "$dir"
|
|
193
|
+
args=(fix "$c" --url "$STATUTE_URL" --start "$STATUTE_START" --app "$STATUTE_APP" --rounds "$STATUTE_ROUNDS" --out "$dir")
|
|
194
|
+
else
|
|
195
|
+
args=(check "$c" --url "$STATUTE_URL" --out "$out")
|
|
196
|
+
if [ -n "$STATUTE_START" ]; then args+=(--start "$STATUTE_START"); fi
|
|
197
|
+
fi
|
|
198
|
+
node "$GITHUB_ACTION_PATH/bin/statute.js" "${args[@]}" 2>&1 | tee -a "$RUNNER_TEMP/statute.log"
|
|
199
|
+
code=${PIPESTATUS[0]}
|
|
200
|
+
case $code in 0) v=pass ;; 1) v=violation ;; *) v=inconclusive ;; esac
|
|
201
|
+
if [ "$STATUTE_FIX" = 1 ] && grep -q "Claude Code fixed" "$dir/loop.md" 2>/dev/null && [ "$code" = 0 ]; then fixed="$fixed $c"; fi
|
|
202
|
+
if [ "$v" = violation ]; then worst=violation; elif [ "$v" = inconclusive ] && [ "$worst" = pass ]; then worst=inconclusive; fi
|
|
203
|
+
if [ "$STATUTE_FIX" = 1 ] && [ -f "$dir/loop.md" ]; then
|
|
204
|
+
sed '1s/^\xEF\xBB\xBF//' "$dir/loop.md" >> "$summary"
|
|
205
|
+
elif [ -f "$dir/$c.md" ]; then sed '1s/^\xEF\xBB\xBF//' "$dir/$c.md" >> "$summary"
|
|
206
|
+
else echo "Statute could not finish the **$c** check. See the job log." >> "$summary"; fi
|
|
207
|
+
if [ -f "$RUNNER_TEMP/statute-scope.json" ]; then
|
|
208
|
+
why=$(node -e 'const r=require(process.argv[1]).reasons[process.argv[2]]||[];console.log(r.slice(0,3).map(x=>"`"+x.file+"` ("+x.why+": `"+x.line.replace(/`/g,"").slice(0,60)+"`)").join(", "))' "$RUNNER_TEMP/statute-scope.json" "$c")
|
|
209
|
+
[ -n "$why" ] && printf '\n_Statute ran this check because the change touches %s._\n' "$why" >> "$summary"
|
|
210
|
+
fi
|
|
211
|
+
printf '\n\n' >> "$summary"
|
|
212
|
+
done
|
|
213
|
+
for t in $STATUTE_TEST_FILES; do
|
|
214
|
+
name=$(basename "$t" .md); dir="$out/expect-$name"; mkdir -p "$dir"
|
|
215
|
+
args=(expect "$t" --url "$STATUTE_URL" --out "$dir")
|
|
216
|
+
if [ -n "$STATUTE_START" ]; then args+=(--start "$STATUTE_START"); fi
|
|
217
|
+
if [ "$STATUTE_FIX" = 1 ]; then args+=(--fix --app "$STATUTE_APP" --rounds "$STATUTE_ROUNDS"); fi
|
|
218
|
+
node "$GITHUB_ACTION_PATH/bin/statute.js" "${args[@]}" 2>&1 | tee -a "$RUNNER_TEMP/statute.log"
|
|
219
|
+
code=${PIPESTATUS[0]}
|
|
220
|
+
case $code in 0) v=pass ;; 1) v=violation ;; *) v=inconclusive ;; esac
|
|
221
|
+
if [ "$STATUTE_FIX" = 1 ] && [ "$code" = 0 ] && grep -q '"agent"' "$dir/expect-loop.json" 2>/dev/null; then fixed="$fixed $name"; fi
|
|
222
|
+
if [ "$v" = violation ]; then worst=violation; elif [ "$v" = inconclusive ] && [ "$worst" = pass ]; then worst=inconclusive; fi
|
|
223
|
+
if [ -f "$dir/expect.md" ]; then sed '1s/^\xEF\xBB\xBF//' "$dir/expect.md" >> "$summary"
|
|
224
|
+
else echo "Statute could not finish the Kane test **$t**. See the job log." >> "$summary"; fi
|
|
225
|
+
printf '\n\n' >> "$summary"
|
|
226
|
+
done
|
|
227
|
+
echo "verdict=$worst" >> "$GITHUB_OUTPUT"
|
|
228
|
+
echo "fixed=${fixed# }" >> "$GITHUB_OUTPUT"
|
|
229
|
+
cat "$summary" >> "$GITHUB_STEP_SUMMARY"
|
|
230
|
+
|
|
231
|
+
- name: Push the fix Kane proved
|
|
232
|
+
id: push
|
|
233
|
+
if: steps.run.outputs.fixed != ''
|
|
234
|
+
shell: bash
|
|
235
|
+
env:
|
|
236
|
+
FIXED: ${{ steps.run.outputs.fixed }}
|
|
237
|
+
STATUTE_APP: ${{ inputs.app }}
|
|
238
|
+
run: |
|
|
239
|
+
git config user.name "statute[bot]"
|
|
240
|
+
git config user.email "statute-bot@users.noreply.github.com"
|
|
241
|
+
git add -A -- "$STATUTE_APP" ':(exclude,glob)**/node_modules/**' ':(exclude,glob)**/statute-report/**'
|
|
242
|
+
if git diff --cached --quiet; then echo "Nothing to commit."; exit 0; fi
|
|
243
|
+
git commit -m "Fix $FIXED: Statute found the violation, Claude Code fixed it, Kane proved the fix"
|
|
244
|
+
git push origin "HEAD:${GITHUB_HEAD_REF}"
|
|
245
|
+
echo "sha=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
|
|
246
|
+
|
|
247
|
+
- name: Publish Kane's screenshots
|
|
248
|
+
id: shots
|
|
249
|
+
if: always() && inputs.screenshots == 'true' && steps.plan.outputs.checks != ''
|
|
250
|
+
continue-on-error: true
|
|
251
|
+
shell: bash
|
|
252
|
+
env:
|
|
253
|
+
REPO: ${{ github.repository }}
|
|
254
|
+
SERVER: ${{ github.server_url }}
|
|
255
|
+
run: |
|
|
256
|
+
set -e
|
|
257
|
+
git rev-parse --git-dir >/dev/null 2>&1 || { echo "No checkout, so no screenshots."; exit 0; }
|
|
258
|
+
shots="$RUNNER_TEMP/statute-shots"
|
|
259
|
+
node "$GITHUB_ACTION_PATH/bin/shots.js" "$RUNNER_TEMP/statute-report" "$shots"
|
|
260
|
+
n=$(node -e 'console.log(require(process.argv[1]).length)' "$shots/shots.json")
|
|
261
|
+
[ "$n" -gt 0 ] || exit 0
|
|
262
|
+
dest="runs/$GITHUB_RUN_ID-$GITHUB_RUN_ATTEMPT"
|
|
263
|
+
wt="$RUNNER_TEMP/statute-shots-wt"
|
|
264
|
+
git worktree add --detach "$wt" >/dev/null 2>&1
|
|
265
|
+
cd "$wt"
|
|
266
|
+
git config user.name "statute[bot]"
|
|
267
|
+
git config user.email "statute-bot@users.noreply.github.com"
|
|
268
|
+
if git fetch -q --depth 1 origin statute-shots 2>/dev/null; then git checkout -q -B statute-shots FETCH_HEAD
|
|
269
|
+
else git checkout -q --orphan statute-shots && git rm -rfq . >/dev/null 2>&1 || true; fi
|
|
270
|
+
mkdir -p "$dest" && cp "$shots"/* "$dest/"
|
|
271
|
+
git add "$dest" && git commit -qm "Kane screenshots for run $GITHUB_RUN_ID"
|
|
272
|
+
for i in 1 2 3; do
|
|
273
|
+
git push -q origin statute-shots && break
|
|
274
|
+
git fetch -q --depth 1 origin statute-shots && git rebase -q FETCH_HEAD
|
|
275
|
+
done
|
|
276
|
+
node -e '
|
|
277
|
+
const list = require(process.argv[1]);
|
|
278
|
+
const base = `${process.argv[2]}/${process.argv[3]}/blob/statute-shots/${process.argv[4]}/`;
|
|
279
|
+
const cells = list.slice(0, 4).map((s) => `<td valign="top" width="50%"><img src="${base}${s.file}?raw=true" alt="${s.caption.replace(/"/g, "")}"><br><sub>${s.pack}, step ${s.step}: ${s.caption}</sub></td>`);
|
|
280
|
+
const rows = [];
|
|
281
|
+
for (let i = 0; i < cells.length; i += 2) rows.push("<tr>" + cells.slice(i, i + 2).join("") + "</tr>");
|
|
282
|
+
console.log("#### What Kane saw\n\n<table>" + rows.join("") + "</table>\n");
|
|
283
|
+
' "$shots/shots.json" "$SERVER" "$REPO" "$dest" > "$RUNNER_TEMP/statute-report/shots.md"
|
|
284
|
+
echo "md=$RUNNER_TEMP/statute-report/shots.md" >> "$GITHUB_OUTPUT"
|
|
285
|
+
|
|
286
|
+
- name: Keep the case file
|
|
287
|
+
if: always() && steps.plan.outputs.checks != ''
|
|
288
|
+
uses: actions/upload-artifact@v4
|
|
289
|
+
with:
|
|
290
|
+
name: statute-case-file
|
|
291
|
+
path: ${{ runner.temp }}/statute-report
|
|
292
|
+
if-no-files-found: ignore
|
|
293
|
+
|
|
294
|
+
- name: Comment on the pull request
|
|
295
|
+
if: always() && inputs.comment == 'true' && github.event_name == 'pull_request' && steps.plan.outputs.self != 'true'
|
|
296
|
+
uses: actions/github-script@v7
|
|
297
|
+
env:
|
|
298
|
+
SUMMARY: ${{ runner.temp }}/statute-report/summary.md
|
|
299
|
+
HAS_RUN: ${{ steps.plan.outputs.checks != '' }}
|
|
300
|
+
PUSHED: ${{ steps.push.outputs.sha }}
|
|
301
|
+
SHOTS: ${{ steps.shots.outputs.md }}
|
|
302
|
+
FIXED: ${{ steps.run.outputs.fixed }}
|
|
303
|
+
with:
|
|
304
|
+
github-token: ${{ inputs.github-token }}
|
|
305
|
+
script: |
|
|
306
|
+
const fs = require("fs");
|
|
307
|
+
let body = "";
|
|
308
|
+
try { body = fs.readFileSync(process.env.SUMMARY, "utf8"); } catch { body = "Statute could not finish. See the job log."; }
|
|
309
|
+
const run = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
|
|
310
|
+
if (process.env.PUSHED) body += `**Fixed and proven.** Claude Code fixed ${process.env.FIXED}, Kane checked the page again and it passed, so Statute pushed the fix to this pull request as ${process.env.PUSHED}.\n\n`;
|
|
311
|
+
try { if (process.env.SHOTS) body += fs.readFileSync(process.env.SHOTS, "utf8") + "\n"; } catch {}
|
|
312
|
+
if (process.env.HAS_RUN === "true") body += `---\nCase file with Kane's screenshots: download **statute-case-file** from [this run](${run}).`;
|
|
313
|
+
const marker = "<!-- statute:check -->";
|
|
314
|
+
const { data: comments } = await github.rest.issues.listComments({ ...context.repo, issue_number: context.issue.number, per_page: 100 });
|
|
315
|
+
const mine = comments.find((c) => c.body && c.body.includes(marker));
|
|
316
|
+
if (mine) await github.rest.issues.updateComment({ ...context.repo, comment_id: mine.id, body: marker + "\n" + body });
|
|
317
|
+
else await github.rest.issues.createComment({ ...context.repo, issue_number: context.issue.number, body: marker + "\n" + body });
|
|
318
|
+
|
|
319
|
+
- name: Open or close the watch issue
|
|
320
|
+
if: always() && inputs.issue == 'true' && github.event_name != 'pull_request' && steps.plan.outputs.checks != ''
|
|
321
|
+
uses: actions/github-script@v7
|
|
322
|
+
env:
|
|
323
|
+
SUMMARY: ${{ runner.temp }}/statute-report/summary.md
|
|
324
|
+
VERDICT: ${{ steps.run.outputs.verdict }}
|
|
325
|
+
URL: ${{ inputs.url }}
|
|
326
|
+
SHOTS: ${{ steps.shots.outputs.md }}
|
|
327
|
+
with:
|
|
328
|
+
github-token: ${{ inputs.github-token }}
|
|
329
|
+
script: |
|
|
330
|
+
const fs = require("fs");
|
|
331
|
+
const label = "statute";
|
|
332
|
+
const url = process.env.URL, verdict = process.env.VERDICT;
|
|
333
|
+
const run = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
|
|
334
|
+
let report = "";
|
|
335
|
+
try { report = fs.readFileSync(process.env.SUMMARY, "utf8"); } catch {}
|
|
336
|
+
try { if (process.env.SHOTS) report += "\n" + fs.readFileSync(process.env.SHOTS, "utf8"); } catch {}
|
|
337
|
+
const { data: open } = await github.rest.issues.listForRepo({ ...context.repo, state: "open", labels: label, per_page: 50 });
|
|
338
|
+
const mine = open.find((i) => i.title.includes(url));
|
|
339
|
+
if (verdict === "violation") {
|
|
340
|
+
const body = `Statute's scheduled check of ${url} found a violation on the live site.\n\n${report}---\nCase file with Kane's screenshots: download **statute-case-file** from [this run](${run}).\n\nThis issue closes itself once the check passes again.`;
|
|
341
|
+
if (mine) await github.rest.issues.createComment({ ...context.repo, issue_number: mine.number, body: `Still failing. [Run](${run}).\n\n${report}` });
|
|
342
|
+
else {
|
|
343
|
+
try { await github.rest.issues.createLabel({ ...context.repo, name: label, color: "b60205", description: "Opened by Statute" }); } catch {}
|
|
344
|
+
await github.rest.issues.create({ ...context.repo, title: `Statute: live site violation on ${url}`, body, labels: [label] });
|
|
345
|
+
}
|
|
346
|
+
} else if (verdict === "pass" && mine) {
|
|
347
|
+
await github.rest.issues.createComment({ ...context.repo, issue_number: mine.number, body: `Kane checked ${url} again and it passes now. Closing. [Run](${run}).` });
|
|
348
|
+
await github.rest.issues.update({ ...context.repo, issue_number: mine.number, state: "closed", state_reason: "completed" });
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
- name: Fail on a violation
|
|
352
|
+
if: inputs.fail-on == 'violation' && steps.run.outputs.verdict == 'violation'
|
|
353
|
+
shell: bash
|
|
354
|
+
run: |
|
|
355
|
+
echo "::error title=Statute::Kane found a violation. See the job summary and the statute-case-file artifact."
|
|
356
|
+
exit 1
|
package/bin/hook.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Statute as a Claude Code Stop hook. When Claude Code is about to finish, Kane
|
|
3
|
+
// CLI checks the app; on a violation the hook blocks the stop and hands Claude
|
|
4
|
+
// the evidence, so it keeps fixing until Kane passes (or the round limit).
|
|
5
|
+
//
|
|
6
|
+
// .claude/settings.json in your project:
|
|
7
|
+
// { "hooks": { "Stop": [ { "hooks": [ { "type": "command", "timeout": 600,
|
|
8
|
+
// "command": "npx -y @miracle352/statute hook" } ] } ] } }
|
|
9
|
+
//
|
|
10
|
+
// statute.json in your project says what to check:
|
|
11
|
+
// { "check": "consent", "url": "http://localhost:3000/", "start": "npm run dev", "rounds": 3 }
|
|
12
|
+
// and, optionally, Kane test files whose expectations must hold:
|
|
13
|
+
// "tests": ["tests/kane/newsletter_test.md"]
|
|
14
|
+
const { spawnSync } = require("child_process");
|
|
15
|
+
const fs = require("fs");
|
|
16
|
+
const os = require("os");
|
|
17
|
+
const path = require("path");
|
|
18
|
+
|
|
19
|
+
const ROOT = path.join(__dirname, "..");
|
|
20
|
+
|
|
21
|
+
function readStdin() {
|
|
22
|
+
try { return JSON.parse(fs.readFileSync(0, "utf8") || "{}"); } catch { return {}; }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function main() {
|
|
26
|
+
const input = readStdin();
|
|
27
|
+
const cwd = input.cwd || process.cwd();
|
|
28
|
+
let cfg;
|
|
29
|
+
try { cfg = JSON.parse(fs.readFileSync(path.join(cwd, "statute.json"), "utf8")); } catch { process.exit(0); } // not set up here: stay out of the way
|
|
30
|
+
const tests = [].concat(cfg.tests || []);
|
|
31
|
+
const checks = [].concat(cfg.check || (tests.length ? [] : "consent"));
|
|
32
|
+
const rounds = Number(cfg.rounds || 3);
|
|
33
|
+
|
|
34
|
+
// Count rounds per session so a fix that never passes cannot loop forever.
|
|
35
|
+
const state = path.join(os.tmpdir(), `statute-hook-${String(input.session_id || "local").replace(/[^\w-]/g, "")}.json`);
|
|
36
|
+
let n = 0;
|
|
37
|
+
try { n = JSON.parse(fs.readFileSync(state, "utf8")).n || 0; } catch {}
|
|
38
|
+
if (!input.stop_hook_active) n = 0;
|
|
39
|
+
if (n >= rounds) {
|
|
40
|
+
fs.rmSync(state, { force: true });
|
|
41
|
+
process.stderr.write(`Statute: still failing after ${rounds} rounds; letting Claude stop. See the last report.\n`);
|
|
42
|
+
process.exit(0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const failures = [];
|
|
46
|
+
for (const check of checks) {
|
|
47
|
+
const out = fs.mkdtempSync(path.join(os.tmpdir(), "statute-hook-"));
|
|
48
|
+
const args = [path.join(ROOT, "bin", "statute.js"), "check", check, "--url", cfg.url, "--out", out];
|
|
49
|
+
if (cfg.start) args.push("--start", cfg.start);
|
|
50
|
+
const r = spawnSync(process.execPath, args, { cwd, env: process.env, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
51
|
+
process.stderr.write(r.stdout || "");
|
|
52
|
+
if (r.status === 1) {
|
|
53
|
+
let report = "";
|
|
54
|
+
try { report = fs.readFileSync(path.join(out, `${check}.md`), "utf8").replace(/^/, ""); } catch {}
|
|
55
|
+
failures.push(report || `The ${check} check found a violation.`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const t of tests) {
|
|
60
|
+
const out = fs.mkdtempSync(path.join(os.tmpdir(), "statute-hook-"));
|
|
61
|
+
const args = [path.join(ROOT, "bin", "statute.js"), "expect", t, "--url", cfg.url, "--out", out];
|
|
62
|
+
if (cfg.start) args.push("--start", cfg.start);
|
|
63
|
+
const r = spawnSync(process.execPath, args, { cwd, env: process.env, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
|
|
64
|
+
process.stderr.write(r.stdout || "");
|
|
65
|
+
if (r.status === 1) {
|
|
66
|
+
let report = "";
|
|
67
|
+
try { report = fs.readFileSync(path.join(out, "expect.md"), "utf8").replace(/^\uFEFF/, ""); } catch {}
|
|
68
|
+
failures.push(report || `The Kane test ${t} broke its expectations.`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!failures.length) { fs.rmSync(state, { force: true }); process.exit(0); }
|
|
73
|
+
fs.writeFileSync(state, JSON.stringify({ n: n + 1 }));
|
|
74
|
+
const reason =
|
|
75
|
+
`Statute (Kane CLI) checked ${cfg.url} in a real browser and found a violation or a broken expectation (round ${n + 1} of ${rounds}). ` +
|
|
76
|
+
`Fix it in the code, then finish again; Kane will re-check.\n\n` + failures.join("\n\n---\n\n");
|
|
77
|
+
process.stdout.write(JSON.stringify({ decision: "block", reason }));
|
|
78
|
+
process.exit(0);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
main();
|
package/bin/init.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// statute init: sets up a repository in one step.
|
|
2
|
+
// Writes the pull request workflow, the nightly watch, and statute.json for the
|
|
3
|
+
// Claude Code hook, then says which two secrets to add.
|
|
4
|
+
//
|
|
5
|
+
// npx @miracle352/statute init --start "npm start" --url http://localhost:3000 [--live https://your-site.com] [--fix]
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const { execSync } = require("child_process");
|
|
9
|
+
|
|
10
|
+
function arg(name, def) {
|
|
11
|
+
const i = process.argv.indexOf("--" + name);
|
|
12
|
+
if (i < 0) return def;
|
|
13
|
+
const v = process.argv[i + 1];
|
|
14
|
+
return v && !v.startsWith("--") ? v : true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function guessStart(cwd) {
|
|
18
|
+
try {
|
|
19
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(cwd, "package.json"), "utf8"));
|
|
20
|
+
const s = pkg.scripts || {};
|
|
21
|
+
if (s.start) return "npm ci && npm start";
|
|
22
|
+
if (s.preview) return "npm ci && npm run build && npm run preview";
|
|
23
|
+
if (s.dev) return "npm ci && npm run dev";
|
|
24
|
+
} catch {}
|
|
25
|
+
if (fs.existsSync(path.join(cwd, "index.html"))) return "npx -y serve -l 3000 .";
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function repoSlug(cwd) {
|
|
30
|
+
try {
|
|
31
|
+
const u = execSync("git remote get-url origin", { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
32
|
+
const m = u.match(/github\.com[:/](.+?)(\.git)?$/);
|
|
33
|
+
return m ? m[1] : null;
|
|
34
|
+
} catch { return null; }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const q = (s) => JSON.stringify(String(s));
|
|
38
|
+
|
|
39
|
+
function prWorkflow({ url, start, fix }) {
|
|
40
|
+
return `# Statute: Kane CLI checks each pull request against consumer law in a real browser.
|
|
41
|
+
# It reads the diff first and only runs the checks the change could affect.
|
|
42
|
+
name: Statute
|
|
43
|
+
on: pull_request
|
|
44
|
+
|
|
45
|
+
permissions:
|
|
46
|
+
contents: write # screenshots go to the statute-shots branch${fix ? "; fixes are pushed to the PR" : ""}
|
|
47
|
+
pull-requests: write
|
|
48
|
+
|
|
49
|
+
jobs:
|
|
50
|
+
statute:
|
|
51
|
+
runs-on: ubuntu-latest
|
|
52
|
+
steps:
|
|
53
|
+
- uses: actions/checkout@v4
|
|
54
|
+
with:
|
|
55
|
+
fetch-depth: 0${fix ? "\n ref: ${{ github.head_ref }}" : ""}
|
|
56
|
+
- uses: miracle352/statute@main
|
|
57
|
+
with:
|
|
58
|
+
check: auto
|
|
59
|
+
url: ${q(url)}
|
|
60
|
+
start: ${q(start)}
|
|
61
|
+
lt-username: \${{ secrets.LT_USERNAME }}
|
|
62
|
+
lt-access-key: \${{ secrets.LT_ACCESS_KEY }}${fix ? `
|
|
63
|
+
fix: "true"
|
|
64
|
+
claude-token: \${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}` : ""}
|
|
65
|
+
`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function watchWorkflow({ live }) {
|
|
69
|
+
return `# Statute watch: Kane checks the live site every night. Opens an issue on a
|
|
70
|
+
# violation and closes it once the site passes again.
|
|
71
|
+
name: Statute watch
|
|
72
|
+
on:
|
|
73
|
+
schedule:
|
|
74
|
+
- cron: "17 2 * * *"
|
|
75
|
+
workflow_dispatch:
|
|
76
|
+
|
|
77
|
+
permissions:
|
|
78
|
+
contents: write
|
|
79
|
+
issues: write
|
|
80
|
+
|
|
81
|
+
jobs:
|
|
82
|
+
watch:
|
|
83
|
+
runs-on: ubuntu-latest
|
|
84
|
+
steps:
|
|
85
|
+
- uses: actions/checkout@v4
|
|
86
|
+
- uses: miracle352/statute@main
|
|
87
|
+
with:
|
|
88
|
+
url: ${q(live)}
|
|
89
|
+
check: consent price
|
|
90
|
+
issue: "true"
|
|
91
|
+
fail-on: never
|
|
92
|
+
lt-username: \${{ secrets.LT_USERNAME }}
|
|
93
|
+
lt-access-key: \${{ secrets.LT_ACCESS_KEY }}
|
|
94
|
+
`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function write(file, text, force) {
|
|
98
|
+
if (fs.existsSync(file) && !force) { console.log(` kept ${path.relative(process.cwd(), file)} (exists; --force to replace)`); return false; }
|
|
99
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
100
|
+
fs.writeFileSync(file, text);
|
|
101
|
+
console.log(` wrote ${path.relative(process.cwd(), file)}`);
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = function init() {
|
|
106
|
+
const cwd = process.cwd();
|
|
107
|
+
const force = !!arg("force", false);
|
|
108
|
+
const start = arg("start") && arg("start") !== true ? arg("start") : guessStart(cwd);
|
|
109
|
+
const url = arg("url") && arg("url") !== true ? arg("url") : "http://localhost:3000";
|
|
110
|
+
const live = arg("live") && arg("live") !== true ? arg("live") : null;
|
|
111
|
+
const fix = !!arg("fix", false);
|
|
112
|
+
|
|
113
|
+
if (!start) {
|
|
114
|
+
console.error("Statute could not tell how to start your app. Run it again with --start \"<command>\" and --url <where it listens>.");
|
|
115
|
+
process.exit(2);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
console.log("\nSetting up Statute in " + cwd + "\n");
|
|
119
|
+
write(path.join(cwd, ".github", "workflows", "statute.yml"), prWorkflow({ url, start, fix }), force);
|
|
120
|
+
if (live) write(path.join(cwd, ".github", "workflows", "statute-watch.yml"), watchWorkflow({ live }), force);
|
|
121
|
+
write(path.join(cwd, "statute.json"), JSON.stringify({ check: ["consent"], url, start, rounds: 3 }, null, 2) + "\n", force);
|
|
122
|
+
|
|
123
|
+
const slug = repoSlug(cwd);
|
|
124
|
+
const secrets = slug ? `https://github.com/${slug}/settings/secrets/actions` : "your repository's Settings → Secrets and variables → Actions";
|
|
125
|
+
console.log(`
|
|
126
|
+
Next:
|
|
127
|
+
1. Add these repository secrets at ${secrets}
|
|
128
|
+
LT_USERNAME your TestMu AI username (free account: https://www.testmuai.com/kane-cli/)
|
|
129
|
+
LT_ACCESS_KEY your TestMu AI access key${fix ? `
|
|
130
|
+
CLAUDE_CODE_OAUTH_TOKEN run \`claude setup-token\` and paste what it prints` : ""}
|
|
131
|
+
2. Commit and push. Every pull request that touches tracking, prices or
|
|
132
|
+
subscriptions now gets Kane in a real browser and a verdict on the PR.${fix ? `
|
|
133
|
+
On a violation, Claude Code fixes it and the fix is pushed only after Kane passes.` : ""}${live ? `
|
|
134
|
+
3. The watch checks ${live} every night and opens an issue if it breaks the law.` : `
|
|
135
|
+
3. Optional: run again with --live https://your-site.com to watch production every night.`}
|
|
136
|
+
|
|
137
|
+
To have Claude Code check its own work before it says "done", add this to .claude/settings.json:
|
|
138
|
+
{ "hooks": { "Stop": [ { "hooks": [ { "type": "command", "timeout": 600, "command": "npx -y @miracle352/statute hook" } ] } ] } }
|
|
139
|
+
|
|
140
|
+
Try a check right now: npx @miracle352/statute check consent --url ${url} --start ${q(start)}
|
|
141
|
+
`);
|
|
142
|
+
};
|