Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,3 @@
# Copilot code review instructions for jModGuard
# Copilot review notes

Copilot is a backup reviewer here, requested manually only when Sourcery can't review. It is advisory: PR Reviewer and Pullfrog form the merge gate. See `AGENTS.md` for the full contracts.

## Layout
- `src/`: the shared `jmodguard` data and config crate. `src/db.rs` is the local/remote libSQL pool and data-layer facade, and it runs its own database runtimes. `src/db/` holds bootstrap/migrations, configuration, settings, moderation and tests. `src/db_compat.rs` has remote deadlines and rollback, and `src/tenant.rs` holds tenant stores.
- `crates/bot`: the Discord gateway bot (Serenity). It handles automod, commands, action and nickname workers, and is the only role that migrates.
- `crates/api`: the axum WebUI/API. It never migrates and dispatches through the queue, not the bot token.
- `crates/updater`: the in-app updater. `src/launcher_child.rs` is the launcher child side.
- `crates/runtime`: process primitives (health, cgroup). It must not import root data, Serenity, or bot/API state.
- `crates/integration-tests`: cross-service tests. `migrations/`, `frontend/` and `docs/` round it out.

## Hard rules (flag any break as P1)
- The bot must always be able to moderate. No change may block or disable bans, kicks, timeouts, deletes or automod enforcement.
- The in-app updater never runs schema migrations. Schema changes use the durable bot/API paired protocol, and recovery after the migration fence is forward-only.
- Never display or estimate host-wide memory; reading MemTotal solely to detect an unbounded cgroup limit is allowed. Use the process's or container's own limits only.
- Keep the service boundaries. Bot and API dependency graphs stay independent, the API never migrates, and API work never takes unscoped queue or moderation access.
- Never run core migrations or defaults on a guild store. Credentials stay in process config, never in catalog rows.

## Rust conventions
- Code must pass Clippy with `-D warnings`. No `unwrap()`/`expect()` in runtime paths (tests are fine).
- Every behavior change needs a test at the smallest reliable boundary that proves it.

## How to review
- Stay within the PR's stated scope. Don't ask for unrelated refactors.
- Report only real bugs, contract breaks, security issues, data loss, and missing tests for changed behavior. Skip style nits and formatting.
- Start each finding with a priority: **P1** for a bug, data loss, security issue, or hard-rule break; **P2** for a likely bug or a missing test; **P3** for a minor but real issue.
- Never comment on Release Please PRs (`chore(main): release …`) or on `CHANGELOG.md` / `.release-please-manifest.json` changes.
Report behavioral bugs, data loss, and security issues in the diff. Formatting and service boundaries are enforced in CI.
577 changes: 3 additions & 574 deletions AGENTS.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion development.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cargo test --locked -p jmodguard-bot dry_run_known_spam_does_not_displace_a_live
cargo clippy --locked -p jmodguard-bot --all-targets -- -D warnings
cargo fmt --check
# The API has two feature modes; lint both when you change it. The default
# mode embeds the WebUI, so build it first (see AGENTS.md Commands):
# mode embeds the WebUI, so build it first (see docs/SETUP.md):
cargo clippy --locked -p jmodguard-api --all-targets -- -D warnings
cargo clippy --locked -p jmodguard-api --no-default-features --all-targets -- -D warnings
# Documentation or sample configuration:
Expand Down
2 changes: 1 addition & 1 deletion docs/CI.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ commit's checks. New commits and reopened PRs still run affected checks.

| Touched surface | Checks |
|---|---|
| Documentation or sample configuration | Contracts, plus `runner-tests` for `AGENTS.md` and `docs/CI.md` because the audit-expiry self-check reads them |
| Documentation or sample configuration | Contracts. `docs/CI.md` also runs `runner-tests` because the audit-expiry self-check reads that file for the enforced date |
| Frontend | Frontend and the default-feature API checks that embed the generated UI (`api-embed-clippy`, `api-test`, `api-build`); the API built without the UI cannot observe these files |
| API or bot | That service, integration, boundaries, formatting, scoped Dylint and supply chain, plus the API, updater and remote database groups for bot changes |
| Shared root source or migrations | Core plus bot/API/updater/integration consumers and remote databases |
Expand Down
189 changes: 189 additions & 0 deletions scripts/check-pinned-facts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#!/usr/bin/env bash
# Executable stand-in for agent-doc claims that are facts about this tree:
# the Rust pin, the runtime image, and production dependency edges.
# Which process migrates is asserted by process_options_fix_migration_role_and_keep_shared_settings.
# Operator manuals are not the source of these facts.
set -euo pipefail

root="$(cd "$(dirname "$0")/.." && pwd)"

fail() {
echo "check-pinned-facts: $*" >&2
exit 1
}

section_body() {
local file="$1"
local section="$2"
awk -v section="$section" '
$0 == section { grab = 1; next }
grab && /^\[/ { exit }
grab { print }
' "$file"
}

# grep -q in a pipefail pipeline returns success before awk finishes, so a
# large section makes awk die with SIGPIPE and the match is reported missing.
section_matches() {
local file="$1"
local section="$2"
local pattern="$3"
local body
body="$(section_body "$file" "$section")" || return 1
printf '%s\n' "$body" | grep -E -- "$pattern" >/dev/null
}
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved

require_section() {
local file="$1"
local section="$2"
local pattern="$3"
local message="$4"
section_matches "$file" "$section" "$pattern" || fail "$message"
}

forbid_section() {
local file="$1"
local section="$2"
local pattern="$3"
local message="$4"
if section_matches "$file" "$section" "$pattern"; then
fail "$message"
fi
}

final_stage() {
awk '
/^FROM / { stage = ""; has = 1 }
has { stage = stage $0 ORS }
END { printf "%s", stage }
' "$1"
}

# The last assignment in the runtime stage is the one the container keeps.
final_stage_sets_remote_db() {
local stage last
stage="$(final_stage "$1")"
last="$(printf '%s\n' "$stage" | awk '
$0 ~ /^ENV JMODGUARD_REQUIRE_REMOTE_DB=/ { value = $0 }
END { print value }
')"
[[ "$last" == "ENV JMODGUARD_REQUIRE_REMOTE_DB=1" ]]
}

self_test() {
local tmp impl_file
tmp="$(mktemp)"
cat >"$tmp" <<'EOF'
[dependencies]
anyhow = "1"
[dev-dependencies]
serenity = "1"
jmodguard-runtime = { path = "runtime" }
EOF
if section_matches "$tmp" "[dependencies]" '^serenity[[:space:]]*='; then
fail "self-test: dev-dependencies leaked into [dependencies]"
fi
section_matches "$tmp" "[dev-dependencies]" '^serenity[[:space:]]*=' \
|| fail "self-test: missed serenity in [dev-dependencies]"
if section_matches "$tmp" "[dev-dependencies]" '^jmodguard[[:space:]]*='; then
fail "self-test: jmodguard-runtime matched as jmodguard"
fi
{
echo '[dependencies]'
echo 'serenity = "1"'
i=0
while [ "$i" -lt 20000 ]; do
echo "crate${i} = \"1\""
i=$((i + 1))
done
echo '[dev-dependencies]'
} >"$tmp"
section_matches "$tmp" "[dependencies]" '^serenity[[:space:]]*=' \
|| fail "self-test: a large section hid a real dependency match"

impl_file="$(mktemp)"
cat >"$impl_file" <<'EOF'
FROM rust:1.100.0-trixie@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa AS builder
ENV JMODGUARD_REQUIRE_REMOTE_DB=1
FROM debian:trixie-slim
ENV OTHER=1
EOF
if final_stage_sets_remote_db "$impl_file"; then
fail "self-test: builder-only remote DB setting counted for the runtime stage"
fi
cat >"$impl_file" <<'EOF'
FROM rust:1.100.0-trixie AS builder
ENV JMODGUARD_REQUIRE_REMOTE_DB=1
FROM debian:trixie-slim
ENV JMODGUARD_REQUIRE_REMOTE_DB=0
ENV JMODGUARD_REQUIRE_REMOTE_DB=1
EOF
final_stage_sets_remote_db "$impl_file" \
|| fail "self-test: the last runtime assignment must win"
rm -f "$tmp" "$impl_file"
}

check_tree() {
local channel channel_re msrv versions version dockerfile tag
channel="$(awk -F'"' '/^channel = / { print $2; exit }' "$root/rust-toolchain.toml")"
[[ "$channel" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || fail "rust-toolchain channel must be X.Y.Z, found: ${channel:-empty}"
msrv="${channel%.*}"

versions="$(grep -E '^rust-version = "' "$root/Cargo.toml" || true)"
[[ -n "$versions" ]] || fail "Cargo.toml has no rust-version pin"
while IFS= read -r line; do
version="$(printf '%s\n' "$line" | sed -E 's/.*"([^"]+)".*/\1/')"
[[ "$version" == "$msrv" ]] || fail "Cargo.toml rust-version ${version} must be ${msrv} (from channel ${channel})"
done <<<"$versions"

local bot_from api_from
bot_from="$(awk '/^FROM rust:/ { print; exit }' "$root/Dockerfile.bot")"
api_from="$(awk '/^FROM rust:/ { print; exit }' "$root/Dockerfile.api")"
[[ "$bot_from" == "FROM rust:${channel}-"*@sha256:* ]] \
|| fail "Dockerfile.bot builder must pin rust:${channel} by digest, found: ${bot_from:-missing}"
channel_re="${channel//./\\.}"
[[ "$bot_from" =~ ^FROM\ rust:${channel_re}-[^@[:space:]]+@sha256:[0-9a-f]{64}([[:space:]]+AS[[:space:]].+)?$ ]] \
|| fail "Dockerfile.bot builder digest is not a full sha256 pin: $bot_from"
[[ "$api_from" == "$bot_from" ]] \
|| fail "Dockerfile.api builder must use the same pinned image as Dockerfile.bot"
for dockerfile in Dockerfile.bot Dockerfile.api; do
final_stage_sets_remote_db "$root/$dockerfile" \
|| fail "$dockerfile runtime stage must set JMODGUARD_REQUIRE_REMOTE_DB=1"
done

forbid_section "$root/Cargo.toml" "[dependencies]" '^serenity[[:space:]]*=' \
"root production dependencies must not include serenity"
forbid_section "$root/Cargo.toml" "[dependencies]" '^jmodguard-bot[[:space:]]*=' \
"root production dependencies must not include jmodguard-bot"
forbid_section "$root/Cargo.toml" "[dependencies]" '^jmodguard-api[[:space:]]*=' \
"root production dependencies must not include jmodguard-api"

require_section "$root/crates/bot/Cargo.toml" "[dependencies]" '^serenity[[:space:]]*=' \
"bot production dependencies must include serenity"
forbid_section "$root/crates/bot/Cargo.toml" "[dependencies]" '^jmodguard-api[[:space:]]*=' \
"bot production dependencies must not include jmodguard-api"

forbid_section "$root/crates/api/Cargo.toml" "[dependencies]" '^serenity[[:space:]]*=' \
"API production dependencies must not include serenity"
forbid_section "$root/crates/api/Cargo.toml" "[dependencies]" '^jmodguard-bot[[:space:]]*=' \
"API production dependencies must not include jmodguard-bot"
require_section "$root/crates/api/Cargo.toml" "[dependencies]" 'memory-store' \
"API sessions must keep the tower-sessions memory-store feature"

forbid_section "$root/crates/runtime/Cargo.toml" "[dependencies]" '^jmodguard[[:space:]]*=' \
"runtime must not depend on the data crate"
forbid_section "$root/crates/runtime/Cargo.toml" "[dependencies]" '^jmodguard-bot[[:space:]]*=' \
"runtime must not depend on the bot"
forbid_section "$root/crates/runtime/Cargo.toml" "[dependencies]" '^jmodguard-api[[:space:]]*=' \
"runtime must not depend on the API"
forbid_section "$root/crates/runtime/Cargo.toml" "[dependencies]" '^serenity[[:space:]]*=' \
"runtime must not depend on serenity"

tag="$(awk -F'"' '/^tag = / { print $2; exit }' "$root/dylint.toml")"
[[ "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] \
|| fail "dylint.toml must pin a vX.Y.Z library tag, found: ${tag:-empty}"
}

self_test
check_tree
echo "check-pinned-facts: ok"
2 changes: 1 addition & 1 deletion scripts/ci-preflight-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ git -C "$frontend_root" -c user.name=Fixture -c user.email=fixture@example.inval
fixture_base="$(git -C "$frontend_root" rev-parse HEAD)"
cp "$root/scripts/check-test-flakes.mjs" "$frontend_root/scripts/"
: >"$frontend_root/tools/test-flake-allowlist.tsv"
for gate in select-checks-test.sh public-install-acceptance-test.sh dependabot-config-test.sh; do
for gate in select-checks-test.sh check-pinned-facts.sh public-install-acceptance-test.sh dependabot-config-test.sh; do
printf 'exit 0\n' >"$frontend_root/scripts/$gate"
done
printf '{"type":"module"}\n' >"$frontend_root/tools/eslint/package.json"
Expand Down
4 changes: 3 additions & 1 deletion scripts/ci-preflight.sh
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ if [ "${1:-}" = "--audit-expiry-self-check" ]; then
[ -n "$rustsec_expiry" ] || fail 'real audit-expiries must list RUSTSEC-2026-0258'
data_lines="$(grep -cE '^RUSTSEC-' "$real_expiries" || true)"
[ "$data_lines" = 1 ] || fail 'audit-expiries must contain exactly one advisory line'
grep -F "$rustsec_expiry" "$self_root/AGENTS.md" >/dev/null || fail 'AGENTS.md must document the enforced expiry'
grep -F "$rustsec_expiry" "$self_root/docs/CI.md" >/dev/null || fail 'docs/CI.md must document the enforced expiry'
grep -F "$rustsec_expiry" "$self_root/.cargo/audit.toml" >/dev/null || fail 'audit.toml must document the enforced expiry'
day_before="$(TZ=UTC date -u -d "${rustsec_expiry}T00:00:00Z - 1 day" +%Y-%m-%d)"
Expand Down Expand Up @@ -1019,6 +1018,9 @@ if should_run boundaries; then
fi

if should_run contracts; then
echo 'ci-preflight: bash scripts/check-pinned-facts.sh'
bash scripts/check-pinned-facts.sh
Comment thread
jatmn marked this conversation as resolved.
Resolved

echo 'ci-preflight: bash scripts/public-install-acceptance-test.sh'
bash scripts/public-install-acceptance-test.sh

Expand Down
2 changes: 1 addition & 1 deletion scripts/ci-workflow-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ try {
fs.copyFileSync(path.join(root, "scripts", file), path.join(repo, "scripts", file));
}
const record = name => `printf '%s\\n' ${name} >>"$AFFECTED_CALLS"\n`;
const contractGates = ["select-checks-test.sh", "public-install-acceptance-test.sh", "dependabot-config-test.sh"];
const contractGates = ["select-checks-test.sh", "check-pinned-facts.sh", "public-install-acceptance-test.sh", "dependabot-config-test.sh"];
for (const gate of contractGates) fs.writeFileSync(path.join(repo, "scripts", gate), record(gate));
const nodeContractGates = ["ci-workflow-test.mjs", "check-test-flakes.test.mjs", "check-test-flakes.mjs"];
for (const gate of nodeContractGates) {
Expand Down
10 changes: 2 additions & 8 deletions scripts/dependabot-config-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ yaml=".github/dependabot.yml"
development="development.md"
ci="docs/CI.md"
deploy="docs/DEPLOY-BUNNY.md"
agents="AGENTS.md"
preflight="scripts/ci-preflight.sh"

for needle in \
Expand Down Expand Up @@ -119,7 +118,7 @@ require_contains "$socket_workflow" 'group: socket-scan-${{ github.ref }}'
require_contains "$socket_workflow" "cancel-in-progress: true"
# The exact ref-based group above cancels superseded scans. github.sha is valid
# in the diff's head binding and must not be banned throughout the workflow.
for doc in AGENTS.md development.md docs/CI.md docs/DEPLOY-BUNNY.md docs/PULLFROG.md; do
for doc in development.md docs/CI.md docs/DEPLOY-BUNNY.md docs/PULLFROG.md; do
require_contains "$doc" "SOCKET_SECURITY_API_KEY"
require_contains "$doc" "not stored in git."
require_contains "$doc" "uploads discovered Cargo and npm manifests outside ignored paths to Socket"
Expand All @@ -138,7 +137,7 @@ do
require_contains "$yaml" "$needle"
done

for path in "$development" "$ci" "$deploy" "$agents"; do
for path in "$development" "$ci" "$deploy"; do
require_contains "$path" "Dependabot"
done

Expand All @@ -156,15 +155,10 @@ require_contains "$deploy" "standard GitHub-hosted runners"
require_contains "$deploy" "Dependabot on self-hosted runners"
require_absent "$deploy" "still use GitHub-hosted"
require_absent "$deploy" "This repository has no GitHub Actions jobs"
require_contains "$agents" "bash scripts/dependabot-config-test.sh"
require_contains "$agents" "Dependabot on self-hosted runners"
require_contains "$agents" "24 hours"
require_contains "$agents" "no GitHub Actions self-hosted"
require_contains "$preflight" "bash scripts/dependabot-config-test.sh"
require_contains "$preflight" "Shared selected GitHub Actions check groups"
require_contains "$preflight" "Dependabot Updates dynamic workflows are not CI"
require_contains "$preflight" "npm audit --prefix tools/eslint --audit-level=high"
require_contains "$agents" "npm audit --prefix tools/eslint --audit-level=high"
require_contains "$ci" "npm audit --prefix tools/eslint --audit-level=high"
require_contains "$development" "five open version-update PRs per ecosystem"

Expand Down
2 changes: 1 addition & 1 deletion scripts/public-install-acceptance-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,6 @@ require_contains .github/workflows/publish.yml "googleapis/release-please-action
require_contains .github/workflows/ci.yml "bash scripts/ci-preflight.sh"
require_contains docs/RELEASES.md "Release Please action"
require_contains docs/CI.md "bash scripts/publish-image-test.sh"
require_contains AGENTS.md "bash scripts/check-service-boundaries.sh"
require_contains scripts/ci-preflight.sh "bash scripts/check-pinned-facts.sh"

echo 'public-install-acceptance-test: ok'
5 changes: 3 additions & 2 deletions scripts/select-checks-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ all="contracts frontend fmt dylint $api_groups $bot_groups runtime updater core
expect "contracts" docs/INSTALL.md
expect "contracts" README.md
expect "contracts" config.example.toml
expect_each "contracts runner-tests" AGENTS.md docs/CI.md
expect "contracts" AGENTS.md
expect "contracts runner-tests" docs/CI.md
# Browser files reach Rust only through the default embed-ui feature, so they
# skip the API built without it. A real API change still runs both halves once,
# and integration subsumes the standalone default-feature API/bot builds.
Expand Down Expand Up @@ -83,7 +84,7 @@ expect "contracts remote-database supply-chain" scripts/ci-remote-database.sh
expect "contracts boundaries supply-chain" scripts/check-service-boundaries.sh
expect_each "contracts pullfrog" \
scripts/pullfrog-command-check.mjs scripts/pullfrog-command-check.test.mjs .github/workflows/pullfrog.yml .github/dependabot.yml
expect_each "contracts" scripts/public-install-acceptance-test.sh scripts/dependabot-config-test.sh .githooks/pre-push scripts/install-git-hooks.sh
expect_each "contracts" scripts/public-install-acceptance-test.sh scripts/dependabot-config-test.sh scripts/check-pinned-facts.sh .githooks/pre-push scripts/install-git-hooks.sh
# The runner regression suites follow their own inputs, not application code.
expect "contracts runner-tests supply-chain" scripts/ci-ensure-cargo-tool.sh
expect "contracts frontend runner-tests" scripts/ci-frontend-node-timeout.sh
Expand Down
6 changes: 3 additions & 3 deletions scripts/select-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ classify() {
add runner-tests
return
;;
AGENTS.md|docs/CI.md)
# The runner's audit-expiry self-check verifies both documents.
docs/CI.md)
# The audit-expiry self-check reads this file for the enforced date.
add runner-tests
return
;;
Expand Down Expand Up @@ -293,7 +293,7 @@ classify() {
add boundaries
return
;;
scripts/public-install-acceptance-test.sh|scripts/dependabot-config-test.sh|.githooks/*|scripts/install-git-hooks.sh)
scripts/public-install-acceptance-test.sh|scripts/dependabot-config-test.sh|scripts/check-pinned-facts.sh|.githooks/*|scripts/install-git-hooks.sh)
add contracts
return
;;
Expand Down
Loading