Skip to content

Commit 3d3bf09

Browse files
committed
fix(development-system): enforce checkpoint state predicates
Validate state-dependent tests, gates, delivery identities, CI receipts, bootstrap exceptions, and one-record framing before replacing authoritative local checkpoint state. Closes: 20260901-xige
1 parent 4e707de commit 3d3bf09

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

plugins/development-system/scripts/write-local-checkpoint.sh

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ record_file=$4
1515
[[ $checkpoint_id =~ ^[A-Za-z0-9._-]+$ ]] || { echo "invalid checkpoint id" >&2; exit 2; }
1616
[[ $expected_generation =~ ^(0|[1-9][0-9]*)$ ]] || usage
1717
[[ -f $record_file ]] || usage
18-
[[ $(wc -l < "$record_file") -eq 1 ]] || { echo "record must contain exactly one newline-terminated line" >&2; exit 2; }
18+
[[ $(wc -l < "$record_file") -eq 1 && $(tail -c 1 "$record_file" | od -An -t u1 | tr -d ' ') == 10 ]] || { echo "record must contain exactly one newline-terminated line" >&2; exit 2; }
1919

2020
record=$(<"$record_file")
2121
[[ $record == checkpoint-v1\ * ]] || { echo "record must start with checkpoint-v1" >&2; exit 2; }
2222
json=${record#checkpoint-v1 }
2323
jq -e --argjson generation "$expected_generation" --arg predecessor "$expected_predecessor" '
2424
def exact_keys($expected): (keys | sort) == ($expected | sort);
2525
def string_or_null: type == "string" or . == null;
26+
. as $record |
2627
exact_keys(["generation", "predecessor_sha256", "baseline_oid", "snapshot", "state", "test", "gates", "delivery", "ci", "next_action"]) and
2728
.generation == $generation and
2829
(if $generation == 0 then .predecessor_sha256 == null else .predecessor_sha256 == $predecessor end) and
@@ -33,7 +34,33 @@ jq -e --argjson generation "$expected_generation" --arg predecessor "$expected_p
3334
(.gates | exact_keys(["lightweight_review_receipt", "fast_gate_receipt", "exact_identity_verification_receipt"]) and all(.[]; string_or_null)) and
3435
(.delivery == null or (.delivery | exact_keys(["mode", "commit_oid", "pushed_oid", "local_snapshot"]) and (.mode | IN("local-only", "direct-to-trunk", "pull-request")) and (.commit_oid | string_or_null) and (.pushed_oid | string_or_null) and (.local_snapshot | string_or_null))) and
3536
(.ci | exact_keys(["runs", "terminal_success_run_id"]) and (.runs | type == "array") and all(.runs[]; exact_keys(["provider", "run_id", "commit_oid", "status"]) and (.provider | type == "string") and (.run_id | type == "string") and (.commit_oid | type == "string") and (.status | IN("queued", "running", "success", "failure"))) and (.terminal_success_run_id | string_or_null)) and
36-
(.next_action | type == "string")
37+
(.next_action | type == "string") and
38+
(.ci.terminal_success_run_id == null or any(.ci.runs[]; .run_id == $record.ci.terminal_success_run_id and .status == "success" and .commit_oid == $record.delivery.pushed_oid)) and
39+
(if .state == "failing" then
40+
.test != null and .test.outcome == "fail" and .delivery == null and all(.gates[]; . == null)
41+
elif .state == "passing-awaiting-gates-or-review" then
42+
.test != null and .test.outcome == "pass" and .delivery == null and .gates.exact_identity_verification_receipt == null
43+
elif .state == "committed" then
44+
.delivery != null and .delivery.commit_oid == .snapshot.head_oid and
45+
(.gates.lightweight_review_receipt | type == "string") and
46+
(.gates.fast_gate_receipt | type == "string") and
47+
((.gates.exact_identity_verification_receipt == null and .next_action == "verify-exact-commit") or (.gates.exact_identity_verification_receipt | type == "string"))
48+
elif .state == "pushed-or-delivery-mode-equivalent" and .generation == 0 then
49+
.baseline_oid == .snapshot.head_oid and .test == null and all(.gates[]; . == null) and .delivery != null and
50+
(if .delivery.mode == "local-only" then (.delivery.local_snapshot | type == "string") else .delivery.pushed_oid == .snapshot.head_oid end)
51+
else
52+
.delivery != null and
53+
(.gates.lightweight_review_receipt | type == "string") and
54+
(.gates.fast_gate_receipt | type == "string") and
55+
(.gates.exact_identity_verification_receipt | type == "string") and
56+
(if .delivery.mode == "local-only" then
57+
(.delivery.local_snapshot | type == "string")
58+
else
59+
.delivery.pushed_oid == .snapshot.head_oid and
60+
((.ci.runs | length) > 0 or .next_action == "register-exact-sha-ci-monitor") and
61+
all(.ci.runs[]; .commit_oid == .delivery.pushed_oid)
62+
end)
63+
end)
3764
' <<<"$json" >/dev/null
3865

3966
git_common_dir=$(git rev-parse --path-format=absolute --git-common-dir)

scripts/tests/development-discipline-plugin.bats

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,24 @@ setup() {
9696
second="$repo/second.record"
9797
stale="$repo/stale.record"
9898
invalid="$repo/invalid.record"
99+
invalid_state="$repo/invalid-state.record"
100+
invalid_framing="$repo/invalid-framing.record"
99101
printf '%s\n' 'checkpoint-v1 {"generation":0,"predecessor_sha256":null}' >"$invalid"
100102

101103
run bash -c 'cd "$1" && "$2" work-item 0 null "$3"' _ "$repo" "$writer" "$invalid"
102104
[ "$status" -ne 0 ]
103105
[ ! -e "$repo/.git/development-system/checkpoints/work-item.latest" ]
104106

105-
first_json=$(jq -cn '{generation:0,predecessor_sha256:null,baseline_oid:"baseline",snapshot:{head_oid:"head",tracked_sha256:"tracked",untracked_sha256:"untracked"},state:"pushed-or-delivery-mode-equivalent",test:null,gates:{lightweight_review_receipt:null,fast_gate_receipt:null,exact_identity_verification_receipt:null},delivery:{mode:"local-only",commit_oid:null,pushed_oid:null,local_snapshot:"snapshot"},ci:{runs:[],terminal_success_run_id:null},next_action:"edit"}')
107+
invalid_state_json=$(jq -cn '{generation:0,predecessor_sha256:null,baseline_oid:"baseline",snapshot:{head_oid:"head",tracked_sha256:"tracked",untracked_sha256:"untracked"},state:"pushed-or-delivery-mode-equivalent",test:null,gates:{lightweight_review_receipt:null,fast_gate_receipt:null,exact_identity_verification_receipt:null},delivery:null,ci:{runs:[],terminal_success_run_id:null},next_action:"edit"}')
108+
printf 'checkpoint-v1 %s\n' "$invalid_state_json" >"$invalid_state"
109+
run bash -c 'cd "$1" && "$2" work-item 0 null "$3"' _ "$repo" "$writer" "$invalid_state"
110+
[ "$status" -ne 0 ]
111+
112+
printf 'checkpoint-v1 %s\n%s' "$invalid_state_json" "$invalid_state_json" >"$invalid_framing"
113+
run bash -c 'cd "$1" && "$2" work-item 0 null "$3"' _ "$repo" "$writer" "$invalid_framing"
114+
[ "$status" -ne 0 ]
115+
116+
first_json=$(jq -cn '{generation:0,predecessor_sha256:null,baseline_oid:"baseline",snapshot:{head_oid:"baseline",tracked_sha256:"tracked",untracked_sha256:"untracked"},state:"pushed-or-delivery-mode-equivalent",test:null,gates:{lightweight_review_receipt:null,fast_gate_receipt:null,exact_identity_verification_receipt:null},delivery:{mode:"local-only",commit_oid:null,pushed_oid:null,local_snapshot:"snapshot"},ci:{runs:[],terminal_success_run_id:null},next_action:"edit"}')
106117
printf 'checkpoint-v1 %s\n' "$first_json" >"$first"
107118

108119
run bash -c 'cd "$1" && "$2" work-item 0 null "$3"' _ "$repo" "$writer" "$first"

0 commit comments

Comments
 (0)