-
Notifications
You must be signed in to change notification settings - Fork 54
feat: Add Split-Solve-Reconcile workflow for cloud-based SMT solving #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
olivier-aws
wants to merge
9
commits into
main2
Choose a base branch
from
SSR_workflow
base: main2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e54605c
feat: Add Split-Solve-Reconcile workflow for cloud-based SMT solving
olivier-aws 6e2f508
test: Add SSR workflow integration test for Python verification
olivier-aws 390c07e
Remove manifest and have reconcile only with SMT2 files
olivier-aws 59661dd
Update in script
olivier-aws ebdc08a
docs: Update design doc to reflect manifest-free reconcile architecture
keyboardDrummer-bot 84d3bdf
fix: Address PR review feedback for SSR workflow
atomb 0a5ee75
Stop using deprecated function
atomb bbf7035
Remove references to term "SSR"
atomb b6399ab
Rename SplitSolveReconcile -> SplitSolveAggregate
atomb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| #!/usr/bin/env bash | ||
| # ------------------------------------------------------------------------------ | ||
| # ssr_py.sh — Split-Solve-Reconcile workflow for Python analysis. | ||
| # | ||
| # Runs the three phases of cloud-compatible SMT solving for a Python file: | ||
| # | ||
| # 1. Generate — convert .py to Strata Ion, run `strata pyAnalyzeLaurel | ||
| # --no-solve` to produce .smt2 files. | ||
| # 2. Solve — run an SMT solver on every .smt2 file (in parallel). | ||
| # 3. Reconcile — run `strata reconcile` against the .smt2 + .result | ||
| # files and produce the final report. | ||
| # | ||
| # By default, everything runs locally (the Solve phase fans out over | ||
| # `xargs -P`). To wire in a real cloud solver, override the SOLVER_CMD | ||
| # environment variable or edit the `solve_phase` function. | ||
| # | ||
| # Prerequisites: | ||
| # - strata executable available (default: ./.lake/build/bin/strata) | ||
| # - Python 3.13+ with the strata package installed | ||
| # (`cd Tools/Python && pip install .`) | ||
| # - An SMT solver on PATH (cvc5 by default; Z3 works too) | ||
| # - The Python dialect file at ./dialects/Python.dialect.st.ion | ||
| # (regenerate with `python -m strata.gen dialect dialects`) | ||
| # | ||
| # Usage: | ||
| # ./ssr_py.sh [options] <input.py> | ||
| # | ||
| # Options: | ||
| # -o, --output-dir <dir> Where to place the .smt2, .result files | ||
| # (default: ./ssr_out/<basename>) | ||
| # -s, --solver <cmd> SMT solver command. Receives the .smt2 path as | ||
| # the first arg; its stdout becomes the .result. | ||
| # (default: "cvc5 --produce-models") | ||
| # -j, --jobs <N> Number of parallel solver processes (default: 4) | ||
| # -c, --check-mode <mode> Strata check mode: deductive, bugFinding, | ||
| # bugFindingAssumingCompleteSpec (default: deductive) | ||
| # -l, --check-level <lev> Strata check level: minimal, minimalVerbose, full | ||
| # (default: minimal) | ||
| # --spec-dir <dir> Directory with compiled PySpec Ion files | ||
| # (default: ".") | ||
| # --sarif Also emit reconcile.sarif in <output-dir> | ||
| # --strict Fail if any .result file is missing | ||
| # --keep Keep intermediate files even on failure | ||
| # --skip-generate Skip phase 1 (reuse existing <output-dir>) | ||
| # --skip-solve Skip phase 2 (reuse existing .result files) | ||
| # --skip-reconcile Skip phase 3 (only generate + solve) | ||
| # --strata <path> Path to the strata binary | ||
| # (default: ./.lake/build/bin/strata) | ||
| # --dialect <path> Path to Python dialect Ion file | ||
| # (default: ./dialects/Python.dialect.st.ion) | ||
| # --dispatch <module> Dispatch module name (may be repeated) | ||
| # --pyspec <module> PySpec module name (may be repeated) | ||
| # -h, --help Show this help | ||
| # | ||
| # Exit codes: | ||
| # 0 — all goals passed | ||
| # 1 — user error (missing prerequisites, bad args) | ||
| # 2 — failures found during reconcile | ||
| # 3 — internal error (generate/solve/reconcile crashed) | ||
| # ------------------------------------------------------------------------------ | ||
|
|
||
| set -u # Treat unset variables as errors. We handle non-zero ourselves. | ||
|
|
||
| # -------- defaults -------- | ||
| OUTPUT_DIR="" | ||
| SOLVER_CMD="${SOLVER_CMD:-cvc5 --produce-models}" | ||
| JOBS=4 | ||
| CHECK_MODE="deductive" | ||
| CHECK_LEVEL="minimal" | ||
| SPEC_DIR="." | ||
| EMIT_SARIF=0 | ||
| STRICT=0 | ||
| KEEP=0 | ||
| SKIP_GENERATE=0 | ||
| SKIP_SOLVE=0 | ||
| SKIP_RECONCILE=0 | ||
| STRATA_BIN="./.lake/build/bin/strata" | ||
| DIALECT_FILE="./dialects/Python.dialect.st.ion" | ||
| DISPATCH_MODULES=() | ||
| PYSPEC_MODULES=() | ||
| INPUT_PY="" | ||
|
|
||
| # -------- helpers -------- | ||
| err() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; } | ||
| warn() { printf '\033[1;33mwarn:\033[0m %s\n' "$*" >&2; } | ||
| info() { printf '\033[1;34m==>\033[0m %s\n' "$*" >&2; } | ||
| step() { printf '\033[1;32m-->\033[0m %s\n' "$*" >&2; } | ||
|
|
||
| usage() { | ||
| sed -n '3,62p' "$0" | sed 's/^# \{0,1\}//' | ||
| } | ||
|
|
||
| die_user() { | ||
| err "$*" | ||
| exit 1 | ||
| } | ||
|
|
||
| die_internal() { | ||
| err "$*" | ||
| exit 3 | ||
| } | ||
|
|
||
| require_cmd() { | ||
| command -v "$1" >/dev/null 2>&1 || die_user "'$1' not found on PATH" | ||
| } | ||
|
|
||
| # -------- arg parsing -------- | ||
| while [ $# -gt 0 ]; do | ||
| case "$1" in | ||
| -o|--output-dir) OUTPUT_DIR="$2"; shift 2 ;; | ||
| -s|--solver) SOLVER_CMD="$2"; shift 2 ;; | ||
| -j|--jobs) JOBS="$2"; shift 2 ;; | ||
| -c|--check-mode) CHECK_MODE="$2"; shift 2 ;; | ||
| -l|--check-level) CHECK_LEVEL="$2"; shift 2 ;; | ||
| --spec-dir) SPEC_DIR="$2"; shift 2 ;; | ||
| --sarif) EMIT_SARIF=1; shift ;; | ||
| --strict) STRICT=1; shift ;; | ||
| --keep) KEEP=1; shift ;; | ||
| --skip-generate) SKIP_GENERATE=1; shift ;; | ||
| --skip-solve) SKIP_SOLVE=1; shift ;; | ||
| --skip-reconcile) SKIP_RECONCILE=1; shift ;; | ||
| --strata) STRATA_BIN="$2"; shift 2 ;; | ||
| --dialect) DIALECT_FILE="$2"; shift 2 ;; | ||
| --dispatch) DISPATCH_MODULES+=("$2"); shift 2 ;; | ||
| --pyspec) PYSPEC_MODULES+=("$2"); shift 2 ;; | ||
| -h|--help) usage; exit 0 ;; | ||
| --) shift; INPUT_PY="${1:-}"; break ;; | ||
| -*) die_user "unknown option: $1" ;; | ||
| *) INPUT_PY="$1"; shift ;; | ||
| esac | ||
| done | ||
|
|
||
| # -------- validate args -------- | ||
| [ -z "$INPUT_PY" ] && { usage; exit 1; } | ||
| [ -f "$INPUT_PY" ] || die_user "input file '$INPUT_PY' does not exist" | ||
|
|
||
| case "$INPUT_PY" in | ||
| *.py|*.python.st.ion) ;; | ||
| *) warn "input file '$INPUT_PY' is not a .py or .python.st.ion file; proceeding anyway" ;; | ||
| esac | ||
|
|
||
| # Derive output directory if not provided. | ||
| if [ -z "$OUTPUT_DIR" ]; then | ||
| base="$(basename "$INPUT_PY")" | ||
| base="${base%.py}" | ||
| base="${base%.python.st.ion}" | ||
| OUTPUT_DIR="./ssr_out/${base}" | ||
| fi | ||
|
|
||
| [ -x "$STRATA_BIN" ] || die_user "strata binary not found at '$STRATA_BIN'. Run 'lake build strata:exe' first, or pass --strata <path>." | ||
|
|
||
| mkdir -p "$OUTPUT_DIR" | ||
|
|
||
| # -------- phase 1: generate -------- | ||
| # Compiles the Python source to an Ion file (if needed) and runs | ||
| # `strata pyAnalyzeLaurel --no-solve` to emit *.smt2 files. | ||
|
|
||
| ION_FILE="$OUTPUT_DIR/input.python.st.ion" | ||
|
|
||
| generate_phase() { | ||
| step "Phase 1: generate" | ||
|
|
||
| # If the input is already an Ion file, just symlink it into place. | ||
| if [ "${INPUT_PY##*.}" = "ion" ] || [ "${INPUT_PY: -14}" = ".python.st.ion" ]; then | ||
| info "Input is already a Python Ion file; copying into place" | ||
| cp -f "$INPUT_PY" "$ION_FILE" | ||
| else | ||
| [ -f "$DIALECT_FILE" ] || die_user "dialect file '$DIALECT_FILE' not found. Regenerate with 'python -m strata.gen dialect dialects'." | ||
| require_cmd python3 | ||
| info "Converting $INPUT_PY -> $ION_FILE" | ||
| if ! python3 -m strata.gen py_to_strata --dialect "$DIALECT_FILE" "$INPUT_PY" "$ION_FILE"; then | ||
| die_internal "py_to_strata failed on '$INPUT_PY'. Is the strata Python package installed? Try 'cd Tools/Python && pip install .'" | ||
| fi | ||
| fi | ||
|
|
||
| info "Running strata pyAnalyzeLaurel --no-solve" | ||
| local py_args=( | ||
| pyAnalyzeLaurel "$ION_FILE" | ||
| --no-solve | ||
| --vc-directory "$OUTPUT_DIR" | ||
| --spec-dir "$SPEC_DIR" | ||
| --check-mode "$CHECK_MODE" | ||
| --check-level "$CHECK_LEVEL" | ||
| ) | ||
| for m in "${DISPATCH_MODULES[@]}"; do py_args+=( --dispatch "$m" ); done | ||
| for m in "${PYSPEC_MODULES[@]}"; do py_args+=( --pyspec "$m" ); done | ||
|
|
||
| if ! "$STRATA_BIN" "${py_args[@]}" > "$OUTPUT_DIR/generate.log" 2>&1; then | ||
| err "strata pyAnalyzeLaurel --no-solve failed (see $OUTPUT_DIR/generate.log)" | ||
| tail -n 30 "$OUTPUT_DIR/generate.log" >&2 || true | ||
| exit 3 | ||
| fi | ||
| info "Generate phase complete. Log: $OUTPUT_DIR/generate.log" | ||
| } | ||
|
|
||
| # -------- phase 2: solve -------- | ||
| # Runs the SMT solver on every .smt2 file, in parallel, capturing stdout | ||
| # (and stderr) into matching .result files. | ||
|
|
||
| solve_one() { | ||
| local smt="$1" | ||
| local base="${smt%.smt2}" | ||
| local result="${base}.result" | ||
| # shellcheck disable=SC2086 # we *want* word splitting on $SOLVER_CMD | ||
| $SOLVER_CMD "$smt" > "$result" 2>&1 | ||
| } | ||
| export -f solve_one | ||
|
|
||
| solve_phase() { | ||
| step "Phase 2: solve" | ||
| local solver_bin="${SOLVER_CMD%% *}" | ||
| require_cmd "$solver_bin" | ||
|
|
||
| local smt2_count | ||
| smt2_count=$(find "$OUTPUT_DIR" -maxdepth 1 -name '*.smt2' -type f | wc -l) | ||
| if [ "$smt2_count" -eq 0 ]; then | ||
| info "No .smt2 files to solve (all obligations resolved by evaluator)." | ||
| return | ||
| fi | ||
| info "Solving $smt2_count .smt2 files with $JOBS parallel workers" | ||
| info "Solver: $SOLVER_CMD" | ||
|
|
||
| export SOLVER_CMD | ||
| # Use xargs for portable parallelism. `-P $JOBS` runs up to $JOBS in | ||
| # parallel; `-I {}` implies one argument per invocation. | ||
| find "$OUTPUT_DIR" -maxdepth 1 -name '*.smt2' -type f -print0 \ | ||
| | xargs -0 -P "$JOBS" -I {} bash -c 'solve_one "$@"' _ {} | ||
| info "Solve phase complete." | ||
| } | ||
|
|
||
| # -------- phase 3: reconcile -------- | ||
| # Runs `strata reconcile` to classify results and produce the final report. | ||
|
|
||
| reconcile_phase() { | ||
| step "Phase 3: reconcile" | ||
| local rec_args=( | ||
| reconcile | ||
| --vc-directory "$OUTPUT_DIR" | ||
| --check-mode "$CHECK_MODE" | ||
| --check-level "$CHECK_LEVEL" | ||
| ) | ||
| [ "$EMIT_SARIF" -eq 1 ] && rec_args+=( --sarif ) | ||
| [ "$STRICT" -eq 1 ] && rec_args+=( --strict ) | ||
|
|
||
| "$STRATA_BIN" "${rec_args[@]}" | tee "$OUTPUT_DIR/reconcile.log" | ||
| local rc="${PIPESTATUS[0]}" | ||
| info "Reconcile phase complete. Log: $OUTPUT_DIR/reconcile.log" | ||
| return "$rc" | ||
| } | ||
|
|
||
| # -------- drive the workflow -------- | ||
| [ "$SKIP_GENERATE" -eq 0 ] && generate_phase | ||
| [ "$SKIP_SOLVE" -eq 0 ] && solve_phase | ||
| if [ "$SKIP_RECONCILE" -eq 0 ]; then | ||
| reconcile_phase | ||
| rc=$? | ||
| if [ "$KEEP" -eq 0 ] && [ "$rc" -eq 0 ]; then | ||
| info "Success. Artifacts kept in $OUTPUT_DIR (pass --keep or rerun with --skip-* to reuse)." | ||
| fi | ||
| exit "$rc" | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below about bash scripts in general - this could easily be a
StrataMain.leancommand as well, even if it would need to shell-out for each phase.