From f13ad7eae6270a7c80db6d27c891a2dbe76bcfe3 Mon Sep 17 00:00:00 2001 From: Waozi Date: Sat, 19 Sep 2026 12:18:05 -0300 Subject: [PATCH] Prove sync retry policy with Bend and enforce build contracts --- .github/workflows/ci.yml | 30 +-- .github/workflows/release.yml | 72 ++++++- AGENTS.md | 122 ++++------- CHANGELOG.md | 1 + Makefile | 28 ++- README.md | 5 + docs/build-laws-plan.md | 193 +++++++++--------- droid/app/build.gradle | 18 ++ droid/app/src/main/cpp/CMakeLists.txt | 16 +- .../metadata/android/en-US/changelogs/124.txt | 1 + laws/sync_retry/LAWS.bend | 86 ++++++++ laws/sync_retry/PROOF.bend | 76 +++++++ laws/sync_retry/main.bend | 89 ++++++++ packaging/flatpak/xyz.waozi.inbe.yml | 16 ++ packaging/snap/snap/snapcraft.yaml | 5 + scripts/generate-sync-retry.mjs | 90 ++++++++ scripts/install-build-node.sh | 34 +++ src/app/app_sync.kry | 53 ++--- src/app/sync_retry.kry | 33 +-- tests/sync_recovery_test.c | 39 ++++ tests/sync_retry_proof_test.mjs | 106 ++++++++++ tests/version_test.py | 20 ++ vendor/kryon | 2 +- 23 files changed, 880 insertions(+), 255 deletions(-) create mode 100644 laws/sync_retry/LAWS.bend create mode 100644 laws/sync_retry/PROOF.bend create mode 100644 laws/sync_retry/main.bend create mode 100644 scripts/generate-sync-retry.mjs create mode 100644 scripts/install-build-node.sh create mode 100644 tests/sync_retry_proof_test.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 882cc20cc..c587c1ac2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -76,6 +76,11 @@ jobs: fetch-depth: 1 lfs: true + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Test apt packages uses: actions/cache@v4 with: @@ -105,11 +110,6 @@ jobs: go-version-file: .test-server/go.mod cache-dependency-path: .test-server/go.sum - - name: Set up Node.js for sync tests - uses: actions/setup-node@v4 - with: - node-version: 22 - - name: Verify sync and recovery run: | make -C .test-server build @@ -131,6 +131,11 @@ jobs: fetch-depth: 1 lfs: true + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Web apt packages uses: actions/cache@v4 with: @@ -143,11 +148,6 @@ jobs: timeout-minutes: 15 run: .github/scripts/install-apt-packages.sh .github/apt/web-packages.txt - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: 22 - - name: Set up Emscripten run: | set -euo pipefail @@ -193,6 +193,11 @@ jobs: fetch-depth: 1 lfs: true + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Web apt packages uses: actions/cache@v4 with: @@ -205,11 +210,6 @@ jobs: timeout-minutes: 15 run: .github/scripts/install-apt-packages.sh .github/apt/web-packages.txt - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: 22 - - name: Set up Emscripten run: | set -euo pipefail diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 26723c4ef..17101cc83 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -50,6 +50,11 @@ jobs: fetch-depth: 1 lfs: true + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Test apt packages uses: actions/cache@v4 with: @@ -81,6 +86,11 @@ jobs: fetch-depth: 1 lfs: true + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Web apt packages uses: actions/cache@v4 with: @@ -93,11 +103,6 @@ jobs: timeout-minutes: 15 run: .github/scripts/install-apt-packages.sh .github/apt/web-packages.txt - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: 22 - - name: Set up Emscripten run: | set -euo pipefail @@ -140,6 +145,11 @@ jobs: fetch-depth: 1 lfs: true + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Web apt packages uses: actions/cache@v4 with: @@ -194,6 +204,11 @@ jobs: fetch-depth: 1 lfs: true + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Web apt packages uses: actions/cache@v4 with: @@ -206,11 +221,6 @@ jobs: timeout-minutes: 15 run: .github/scripts/install-apt-packages.sh .github/apt/web-packages.txt - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: 22 - - name: Set up Emscripten run: | set -euo pipefail @@ -263,6 +273,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Linux apt packages uses: actions/cache@v4 with: @@ -364,6 +379,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Linux apt packages uses: actions/cache@v4 with: @@ -417,6 +437,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Linux apt packages uses: actions/cache@v4 with: @@ -470,6 +495,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Linux apt packages uses: actions/cache@v4 with: @@ -520,6 +550,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Linux apt packages uses: actions/cache@v4 with: @@ -560,6 +595,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Build FreeBSD .pkg uses: vmactions/freebsd-vm@v1 with: @@ -567,7 +607,7 @@ jobs: usesh: true prepare: | for attempt in 1 2 3; do - pkg install -y gmake cmake pkgconf sdl2 gtk3 curl sqlite3 mesa-libs libdrm hicolor-icon-theme desktop-file-utils && break + pkg install -y node22 python3 gmake cmake pkgconf sdl2 gtk3 curl sqlite3 mesa-libs libdrm hicolor-icon-theme desktop-file-utils && break if [ "$attempt" -eq 3 ]; then exit 1 fi @@ -603,6 +643,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Cache Windows apt packages uses: actions/cache@v4 with: @@ -661,6 +706,11 @@ jobs: submodules: recursive fetch-depth: 1 + - name: Set up Node.js for Bend proofs + uses: actions/setup-node@v4 + with: + node-version: 22 + - name: Set up JDK 21 uses: actions/setup-java@v4 with: diff --git a/AGENTS.md b/AGENTS.md index b14226d4d..ec427cbdd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,87 +1,51 @@ -# Inbe Agent Instructions +# Inbe repository rules -- Never edit files under `vendor/` from this repository. -- Kryon changes must be made in the core Kryon repository, committed there, pushed there, and then brought into Inbe by updating the Kryon submodule pointer. -- Do not add tests, policy checks, wrappers, compatibility aliases, or generated changes inside `vendor/` from Inbe. -- If a task appears to require a vendor change, stop and switch to the upstream project workflow first. -- When adding or changing localized strings, translate the content for every locale file being touched. Do not copy English placeholder text into non-English locales. -- Run `make no-vendor-edits` or `make test` before handing work back when a task touches Kryon or other vendored code. +## Ownership and releases -## Version Consistency Rule +- Never edit `vendor/`. Change Kryon in its upstream repository on `master`, + commit and push there, then update only Inbe's clean submodule pointer. + Run `make no-vendor-edits` after dependency changes. +- Add the numeric release to `CHANGELOG.md`, then run `./update_version.sh`. + The canonical macros are `APP_VERSION_STRING`, `APP_VERSION_MAJOR`, + `APP_VERSION_MINOR`, and `APP_VERSION_PATCH`; do not create aliases. + Only the GitHub Actions release workflow creates or pushes release tags. +- Translate changed strings in every affected locale; no English placeholders. -- `CHANGELOG.md` is the sole release-version source of truth. Add or update its - numeric release entry, then run `./update_version.sh`; never edit generated - version values individually or use `Unreleased` as an application version. -- `APP_VERSION_STRING`, `APP_VERSION_MAJOR`, `APP_VERSION_MINOR`, and - `APP_VERSION_PATCH` in `src/core/version.h` are the permanent canonical macro - names. Do not rename them or add alternate version macros or aliases. -- Application, packaging, website, and release readers must use that canonical - version. Shell release readers use `scripts/check-version.py --print-version`, - which rejects missing, malformed, or inconsistent metadata. -- Keep `make version-check` and `make version-test` passing. Never bypass these - checks to package or publish a release. Only the GitHub Actions release - workflow may create or push release tags. +## Machine-checked contracts -## Clean Sync API Rule +- `make build-laws` is mandatory for native, web, and Windows artifacts; + Gradle and CMake enforce the same version and proof checks independently. + Never bypass a failed gate or hand-edit generated policy tables. +- Retry behavior lives in `laws/sync_retry/main.bend`. Implement changes there + and prove `LAWS.bend` in `PROOF.bend`; the app consumes the checked table. + Do not weaken laws to make an implementation pass. Contract changes require + explicit product intent and separate review from implementation changes. +- Run `make proof-test sync-recovery-test version-test` for these contracts. + Coverage, trust boundaries, and the next proof migrations are described in + [docs/build-laws-plan.md](docs/build-laws-plan.md). -- New Inbe releases use sync protocol v6 and the clean Kryon sync API. Do not - add `Ksync`-prefixed types, functions, files, build variables, or compatibility - wrappers. Legacy wire strings may remain only where migration code must read - data produced by an already shipped release. -- Migration must be automatic, resumable, and non-destructive. Do not purge - synced tombstones or legacy projections while a supported older installation - can still return. +## Public UI and sync boundaries -## Canonical Text Rule +- Use `Text(TextProps)`, `Image(ImageProps)`, and `Button(ButtonProps)` directly. + Semantic images use the same `Image` surface. No positional Text, legacy + drawing calls, alternate widgets, or thin local forwarding wrappers. + Keep meaningful compositions and gestures; use standard Checkbox and Toggle. + Run `make clean-text-api-check button-api-check` for UI changes. +- If a required widget capability is missing, implement the reusable primitive + upstream first. Do not work around it in Inbe or generated code. +- Use sync protocol v6 and the clean Kryon API; no new `Ksync` names or wrappers. + Keep legacy wire strings only for migration. Migrations must be automatic, + resumable, and non-destructive, preserving tombstones and legacy projections + while supported older installations can return. -- `Text` has exactly one supported form: `Text((TextProps){...})`. Put bounds, - wrapping, clipping, color, alignment, and disabled state in `TextProps`. -- Do not add positional `Text` calls, parallel helpers such as `TextWrapped` or - `TextColored`, or local wrappers that conceal the old signature. Change the - maintained `.kry` or C source, never generated output or vendored Kryon code. -- Keep `make clean-text-api-check` passing. A UI migration is incomplete until - the maintained source and freshly generated output build with upstream Kryon. +## Product behavior and readability -## Canonical Image Rule - -- App `.kry` UI uses Kryon's `Image(ImageProps)` for images, or - `PageImage(ImageProps, alt_text)` for semantic page images. -- Do not replace legacy `Texture(...)` calls with `DrawTexture*`, - `DrawTexturePro`, `DrawTextureRec`, or other raylib draw calls in maintained - UI. Those are low-level backend primitives, not the clean Kryon public widget - surface. -- If a screen currently only has a `Texture2D` handle, prefer moving the UI to - asset-backed `ImageProps`. If Kryon cannot express the needed case yet, make - the reusable primitive in upstream `~/Projects/kryon`, commit it there, and - then update Inbe's `vendor/kryon` pointer. Do not solve it with app-local - wrappers or edits under `vendor/`. - -## Session Animation and Controls - -- Desktop focus loss must never pause a practice, freeze its circle, suppress - normal session updates, or switch it into the mobile background-timer path. - Explicit user pause is separate from window focus. Preserve Android/web - lifecycle handling without applying it to an unfocused desktop window. -- Keep Kryon's default hover, press, and focus transitions enabled. Do not - disable transition cues globally in the app frame loop. -- Session audio controls use 44-unit targets (24-unit icons plus 10-unit - padding on each side), scaled through Kryon, with space between controls. - -## Readability Rule - -- Buttons use Kryon's `Button(ButtonProps)` directly, including icon buttons - and clickable cards. Do not restore app-local button wrappers or separately - painted hover/click surfaces. Keep custom card content and domain gestures, - but let the shared button own its surface and activation. Checkboxes and - toggles use their own standard widgets. Run `make button-api-check`. -- Do not add thin forwarding helpers around `Button`, even with screen-specific - names. Put `ButtonProps` directly at the call site. Keep composed widgets only - when they own meaningful layout, content, or behavior beyond forwarding props. -- The Lists screen starts with list tabs; do not restore a separate Lists - title bar or replace the tabs with a dropdown. - -- Write conventional, fully readable code. Never compress multiple statements, - branches, declarations, or error checks onto one line. Use descriptive names, - explicit control flow, and focused helpers. -- Format changed source, run `git diff --check`, and inspect the final diff before - considering a change complete. +- Desktop focus loss must not pause practices, freeze animation, suppress + updates, or enter the mobile background-timer path. Preserve mobile/web + lifecycle handling and explicit user pause as separate behavior. +- Preserve hover, press, and focus transitions. Session audio controls have + 44-unit targets (24-unit icons plus 10-unit padding), scaled and spaced. +- Lists starts with list tabs, without a separate title bar or dropdown. +- Use descriptive names, explicit control flow, and focused helpers. Never + compress multiple statements, branches, declarations, or checks onto one line. + Format changed source, inspect the final diff, and run `git diff --check`. diff --git a/CHANGELOG.md b/CHANGELOG.md index 3657a5f92..f36e531aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Improved text selection, keyboard focus, and composition handling when editing text. - Connecting to sync runs in the background so network requests do not block the interface. - Sync retries temporary connection failures automatically without losing queued changes. +- Automatic friend-list retries stop when account details need attention, and retry delays recover safely from invalid saved counts. - Profile shows actionable sync status instead of internal repair messages. - Restoring an account key now starts synchronization and retrieves the account alias and existing friendships. Friend lists remain available when leaderboard loading fails. - Sync no longer asks you to upgrade just because the server supports a newer sync protocol. diff --git a/Makefile b/Makefile index 7fd90c922..3ac6aa2d6 100644 --- a/Makefile +++ b/Makefile @@ -413,6 +413,10 @@ APP_INCLUDE := -Isrc -Isrc/app -Isrc/core -Isrc/screens -Isrc/screens/settings - APP_INCLUDE += $(KRYON_INCLUDE) APP_INCLUDE += -I$(KRY_GEN_DIR) APP_INCLUDE += -I$(KRY_GEN_DIR)/src +PROOF_DIR := $(BUILD_DIR)/proofs +SYNC_RETRY_HEADER := $(PROOF_DIR)/sync_retry_table.h +APP_INCLUDE += -I$(PROOF_DIR) +KRYON_INCLUDE += -I$(PROOF_DIR) RAY_PKGS ?= sdl2 libdrm gbm egl glesv2 RAY_SDL_CFLAGS ?= $(shell pkg-config --cflags sdl2 2>/dev/null) RAY_SDL_LDLIBS ?= $(shell pkg-config --libs sdl2 2>/dev/null) @@ -580,7 +584,7 @@ $(KRYON_RUNTIME_STAMP): Makefile $(K2C) $(KRYON_RUNTIME_KRY) $(KRYON_RUNTIME_C) $(KRYON_RUNTIME_H): $(KRYON_RUNTIME_STAMP) @test -f $@ -$(KRY_GEN_STAMP): Makefile $(K2C) $(KRY_SRCS) +$(KRY_GEN_STAMP): Makefile $(K2C) $(KRY_SRCS) $(SYNC_RETRY_HEADER) | build-laws rm -rf $(KRY_GEN_DIR) mkdir -p $(KRY_GEN_DIR) $(K2C) --root $(abspath .) -o $(KRY_GEN_DIR) $(abspath $(KRY_SRCS)) @@ -603,6 +607,7 @@ kry-c-plan9: $(KRY_GEN_STAMP) --include-dir vendor/kryon/include --include-dir src \ --include-dir $(KRY_GEN_DIR) --include-dir vendor-builds/sqlite \ -o $(PLAN9_GENERATED) $(KRY_SRCS) + cp $(SYNC_RETRY_HEADER) $(PLAN9_GENERATED)/sync_retry_table.h find $(PLAN9_GENERATED) -type f -name '*.c' | LC_ALL=C sort > $(PLAN9_FILE_LIST) sh vendor/kryon/scripts/embed-assets.sh $(PLAN9_EMBEDDED_ASSETS_C) \ $(STYLE_FILES) $(LOCALE_FILES) $(IMAGE_FILES) $(FONT_FILES) @@ -762,6 +767,22 @@ version-test: test: version-check version-test +# Proofs run on every invocation, even with an existing generated header. +# The generator replaces the header only when its contents actually change. +.PHONY: proofs proof-test build-laws +proofs: + node scripts/generate-sync-retry.mjs $(SYNC_RETRY_HEADER) + +proof-test: + node --test tests/sync_retry_proof_test.mjs + +build-laws: version-check proofs + +$(SYNC_RETRY_HEADER): | proofs + @test -f $@ + +test: proof-test + secret-check: python3 ./scripts/check-secrets.py --working-tree @@ -1478,7 +1499,7 @@ $(FLATPAK_TARGET): Makefile $(FLATPAK_MANIFEST) | $(FLATPAK_BUILD_DIR) $(FLATPAK -v "$(abspath .):/work" \ -w /work \ $(FLATPAK_IMAGE) \ - sh -lc 'set -eu; rm -rf .flatpak-builder $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir; flatpak-builder --disable-rofiles-fuse --force-clean --repo=$(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir $(FLATPAK_MANIFEST) || { rm -rf $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir vendor-builds/linux build/bin/linux; make vendor-prebuilds-native; make native; flatpak build-init $(FLATPAK_BUILD_DIR)/build-dir $(APP_ID) org.gnome.Sdk org.gnome.Platform 46; install -D -m755 "$$(find build/bin/linux -maxdepth 1 -type f -name '\''inbe-linux-*'\'' | head -n 1)" $(FLATPAK_BUILD_DIR)/build-dir/files/bin/inbe; install -D -m644 packaging/linux/appimage/inbe.desktop $(FLATPAK_BUILD_DIR)/build-dir/files/share/applications/$(APP_ID).desktop; sed -i '\''s/^Icon=.*/Icon=$(APP_ID)/'\'' $(FLATPAK_BUILD_DIR)/build-dir/files/share/applications/$(APP_ID).desktop; install -D -m644 packaging/linux/appimage/inbe.png $(FLATPAK_BUILD_DIR)/build-dir/files/share/icons/hicolor/512x512/apps/$(APP_ID).png; install -D -m644 packaging/linux/appimage/inbe.appdata.xml $(FLATPAK_BUILD_DIR)/build-dir/files/share/metainfo/$(APP_ID).metainfo.xml; flatpak build-finish --share=ipc --share=network --socket=fallback-x11 --socket=wayland --socket=pulseaudio --device=dri --filesystem=home $(FLATPAK_BUILD_DIR)/build-dir; flatpak build-export $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir; }; flatpak build-bundle $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_TARGET) $(APP_ID)' + sh -lc 'set -eu; sh scripts/install-build-node.sh /tmp/inbe-build-node; export PATH=/tmp/inbe-build-node/bin:$$PATH; rm -rf .flatpak-builder $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir; flatpak-builder --disable-rofiles-fuse --force-clean --repo=$(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir $(FLATPAK_MANIFEST) || { rm -rf $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir vendor-builds/linux build/bin/linux; make vendor-prebuilds-native; make native; flatpak build-init $(FLATPAK_BUILD_DIR)/build-dir $(APP_ID) org.gnome.Sdk org.gnome.Platform 46; install -D -m755 "$$(find build/bin/linux -maxdepth 1 -type f -name '\''inbe-linux-*'\'' | head -n 1)" $(FLATPAK_BUILD_DIR)/build-dir/files/bin/inbe; install -D -m644 packaging/linux/appimage/inbe.desktop $(FLATPAK_BUILD_DIR)/build-dir/files/share/applications/$(APP_ID).desktop; sed -i '\''s/^Icon=.*/Icon=$(APP_ID)/'\'' $(FLATPAK_BUILD_DIR)/build-dir/files/share/applications/$(APP_ID).desktop; install -D -m644 packaging/linux/appimage/inbe.png $(FLATPAK_BUILD_DIR)/build-dir/files/share/icons/hicolor/512x512/apps/$(APP_ID).png; install -D -m644 packaging/linux/appimage/inbe.appdata.xml $(FLATPAK_BUILD_DIR)/build-dir/files/share/metainfo/$(APP_ID).metainfo.xml; flatpak build-finish --share=ipc --share=network --socket=fallback-x11 --socket=wayland --socket=pulseaudio --device=dri --filesystem=home $(FLATPAK_BUILD_DIR)/build-dir; flatpak build-export $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_BUILD_DIR)/build-dir; }; flatpak build-bundle $(FLATPAK_BUILD_DIR)/repo $(FLATPAK_TARGET) $(APP_ID)' test -f $@ $(WEB_JS_TARGET): Makefile $(WEB_SRC) $(KRYON_WEB_SRCS) $(SQLITE_SRC) $(SQLITE_AMALGAMATION_H) $(FONT_FILES) $(EMBEDDED_ASSETS_C) $(KRY_GEN_STAMP) $(WEB_LIBOQS_A) web-tools-check | $(WEB_DIST_DIR) @@ -1966,3 +1987,6 @@ $(TEST_BIN_DIR)/sync_recovery_test: tests/sync_recovery_test.c $(KRY_GEN_STAMP) # Validate before producing release packages, including direct artifact targets. $(APPIMAGE_TARGET) $(DEB_TARGET) $(RPM_TARGET) $(SNAP_TARGET) $(FLATPAK_TARGET) $(CLICK_TARGET) $(CHROME_WEB_STORE_ZIP) $(FIREFOX_ADDONS_ZIP): | version-check android-release android-bundle android-copy-release-apks android-copy-bundle windows-setup site: version-check + +# Actual artifacts are gated too, including direct and incremental builds. +$(TARGET) $(KRYON_HOST_TARGET) $(WIN64_TARGET) $(WIN32_TARGET) $(WEB_JS_TARGET) $(WEB_CANVAS_TARGET): $(SYNC_RETRY_HEADER) | build-laws diff --git a/README.md b/README.md index 939201f66..cbcd48d16 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ optionally sync user-owned data through a Daochi-compatible sync server. ## Build +The build host needs Node.js 22.18 or newer for mandatory Bend proof checking, +as well as Python 3 and the platform's native build dependencies. Node and Bend +are build tools; they are not installed with the app. See +[build contracts and proofs](docs/build-laws-plan.md) for the checked behavior. + Initialize the submodules once: ```bash diff --git a/docs/build-laws-plan.md b/docs/build-laws-plan.md index 1bc9e5db6..91d7baa8c 100644 --- a/docs/build-laws-plan.md +++ b/docs/build-laws-plan.md @@ -1,119 +1,108 @@ -# Enforced build laws for Inbe 2.0.0 +# Build contracts and Bend proofs -## Goal and current evidence +## Implemented -Turn objective rules into failing compiler or build checks, and keep -`AGENTS.md` short. A green build guarantees only its specified invariants; -it does not guarantee that every possible product behavior is correct. +Inbe's release metadata uses the numeric changelog release and the permanent +`APP_VERSION_*` macros. `update_version.sh` synchronizes the release fields; +`check-version.py` rejects inconsistencies. The version check now runs in the +native, web, and Windows artifact graphs and in both Gradle and CMake, including +incremental builds. Version semantics remain a structural build check, not a +formal theorem. -Inbe remains the pending 2.0.0 release, Android versionCode 124. Version -metadata is synchronized by `update_version.sh`; this work does not create -a second release number or a release tag. +The retry implementation is now **Bend 2 code that ships as evaluated data**: -- Kryon commit `e89003b1` makes existing compiler laws mandatory in all six - frontends, including ordinary compilation and `--no-strict`. Its regression - tests reject 105 forbidden calls across compiler modes without emitting - output. Compiler specification and runtime parity checks pass. -- Inbe now references that upstream commit. Its complete maintained `.kry` - source transpiles with the mandatory laws enabled. -- A full strict-checking trial produced 25,409 diagnostics, predominantly - unresolved names and unsupported C expressions. Enabling `--strict` - everywhere is not an implementation of this plan. -- Version checks currently gate tests, packaging, and release CI. Several - checks still do not gate native compilation or direct Gradle invocation. +1. `laws/sync_retry/main.bend` defines the actual pure retry policy. +2. `LAWS.bend` states 11 universally quantified contracts; `PROOF.bend` proves + them for the complete finite input types. +3. Kryon's `tools/bend-laws.mjs` uses pinned upstream Bend 2.0.16, commit + `15ae0c86f3193b8f645b4bedbc438655b648d0da`. It verifies the checker and Base + hashes before loading, checks termination and types, and rejects holes, + unproved declarations, `@unsafe`, foreign implementations, remote imports, + and imports outside the proof package. +4. The checked function is evaluated directly in Bend's kernel for all 45 + combinations of result and retry state. `scripts/generate-sync-retry.mjs` + writes the C table consumed by `src/app/sync_retry.kry`. There is no separate + handwritten retry implementation or optimizing Bend code generator in this + path. No Bend runtime is installed on the phone. +5. Account-data and social retries both use that table. The small adapter + clamps persisted integers before indexing, avoiding increment overflow. + Unknown results stop automatic retries. -## 1. Close build-entrypoint gaps +The proofs establish success resetting the retry state; temporary challenge +and request failures following 5/15/30/60 seconds with a saturated fourth +attempt; actionable failures stopping automatic retry; and the mapping of +saved retry states and delays. Each error classification has its own law. +They do not prove server availability, delivery, alias/friend restoration, +or arbitrary `.kry` behavior. Existing integration tests cover those effects. -Add `scripts/check-build-laws.py` as the single non-mutating build preflight. -It invokes existing version and canonical-widget checks, checks submodule -cleanliness and public HTTPS URLs, and validates locale keys and nonempty -translations. Do not duplicate existing validators in the orchestrator. -Every diagnostic names the violated invariant and the offending file. +## Build behavior and verification -Run it before source generation and final artifacts in the native, web, -Windows, and Android build graphs. In Make, use order-only prerequisites on -the actual generation and artifact targets, not only convenience targets. -In Gradle, attach it to `preBuild` for debug, release, and gplay. Direct CMake -builds must also depend on the check before app generation and compilation. -The Plan 9 export runs the same checks on the host before emitting sources; -the native Plan 9 build consumes that checked export. +Install Node.js **22.18 or newer** on the build host and initialize recursive +submodules. The checker runs offline once those dependencies are present. +CI and package builders provide Node explicitly. The container setup script +uses pinned, checksummed official Node archives; ordinary compilation never +downloads a proof tool. Flatpak removes its Node build tool from the app. -Run the preflight on incremental builds too. Do not accept a previously -written success marker after source, metadata, or vendor state changes. -Git-less source archives may validate content and version rules, but must -explicitly report that repository ownership checks cannot be evaluated; -release builds require a Git checkout for those checks. +- `make build-laws`: release consistency and proofs. +- `make proof-test`: deterministic generation, contract-breaking mutations, + enum mapping drift, and rejection with an old header already present. +- `make sync-recovery-test`: the actual generated C and coordinator, including + all known result codes, extreme persisted integers, and social retry state. +- `make version-test`: release synchronization and rejecting mismatches. +- Upstream `make bend-laws-test`: checker/evaluator acceptance and rejection, + including missing proofs, holes, unsafe definitions, and escaped imports. -## 2. Move reusable API rules into Kryon +Make's actual generated-header and app artifact prerequisites run the checks +on every invocation. Failed checking prevents compilation even if an old +header exists. Successful generation preserves the header's timestamp when +its contents are unchanged. Gradle `preBuild` always checks; direct CMake +builds independently regenerate their checked table. `make kry-c-plan9` +exports the checked table with the generated sources for native Plan 9, +which consumes the host-verified export. -Keep one declared, typed public signature for each widget. Extend the shared -compiler checks to enforce the Text, Image, and Button contracts before -lowering, independently of the full host-language type checker. Validate -arguments against the canonical declarations; do not create a second list -of signatures that can drift from them. +Kryon's syntax law `image.surface.no_low_level_calls` remains mandatory in +all six compiler frontends, including default and `--no-strict` builds. Its +105 rejection cases are compiler regression tests, not behavioral proofs. +The text/button scanners, migration tests, and sync integration suite remain +separate checks with their own scope. -Add rejecting fixtures for positional Text, obsolete image calls, removed -widget aliases, and wrong props. Include accepting fixtures with named props, -comments, strings, and legitimate composition. Exercise C, C++, Go, JS, KIR, -and cartridge frontends. Backend implementation calls remain confined to -their owning runtime sources and are not public app widget APIs. +## Trust and use with smaller models -Leave Inbe-specific rules such as its version format, Lists layout, and -account recovery in Inbe. Make reusable Kryon changes on upstream `master`, -commit and push them, then move only Inbe's clean submodule pointer. +A model can change the implementation and provide proofs while the contracts +stay fixed. Its output is accepted only when the checker verifies those +contracts. This reduces dependence on model quality for **specified behavior**; +it does not establish that a smaller model is equally good at designing the +contracts or diagnosing missing requirements. -## 3. Enforce behavior at the appropriate level +The trusted boundary is the pinned checker/Base, the finite-table generator, +the enum/integer adapter, build graph, and target compiler. Regression tests +exercise the integration boundary. An agent able to rewrite the laws or gates +can weaken the guarantee. Protected branches, required checks, and independent +review ownership should protect those files on the hosting service; repository +files alone cannot make themselves immutable. Hosting settings have not been +changed by this implementation. -| Invariant | Enforcement | -|---|---| -| Version fields agree; versionCode changes once | Build preflight and updater regression tests | -| Canonical widget signatures and forbidden app API calls | Compiler diagnostics before lowering | -| All locale keys have values | Build preflight; translation meaning still requires review | -| Temporary sync failure retains queued data | Sync state and integration tests in required CI | -| Same-key restoration recovers alias, friends, and data | Disposable-server integration test in release prerequisites | -| Leaderboard failure preserves friends | Social recovery regression test | -| Desktop focus loss does not pause a practice | Lifecycle regression test | -| No downstream vendor edits; submodules use HTTPS | Repository preflight | +`AGENTS.md` now states ownership, contract-change boundaries, and the remaining +product/review obligations instead of treating prose as a proof system. -Reuse existing tests where they already establish the invariant. Add tests -only for missing coverage. Network-dependent integration checks gate release -CI, while ordinary local compilation runs deterministic, offline checks. +## Next migrations, in order -## 4. Reduce instructions after enforcement exists +1. Move practice lifecycle decisions into a pure finite policy: desktop focus + cannot pause a session, user pause remains explicit, and mobile background + transitions preserve the required timer behavior. Compile its proved table + through the same Kryon checker; retain platform integration tests. +2. Specify sync recovery transitions: restoring an account cannot discard + queued local changes, partial social refresh cannot erase known friends, + and an account switch cannot apply responses belonging to the old account. + Model effects explicitly; proofs of scheduling do not prove network delivery. +3. For unbounded algorithms such as collection merges, define a checked direct + compilation path and its semantics before expanding beyond finite tables. + Do not claim a proof of a model also proves a separately implemented C path. +4. Add typed widget contracts in Kryon's parsed compiler representation and + negative fixtures before removing the remaining API instructions. Turning on + today's whole-project strict mode produces thousands of unsupported-host + diagnostics and is not a substitute for those contracts. -Replace long implemented API and version instructions in `AGENTS.md` with -short references to their canonical declarations, law IDs, and check command. -Keep repository ownership, upstream workflow, release-tag ownership, product -boundaries, and human review responsibilities. Do not delete an instruction -until its replacement check and a negative regression test are present. - -Keep objective contracts and their tests separate from ordinary feature -changes. Configure required CI and review ownership for changes to laws and -release gates if repository administration is included in the rollout. A -modifiable local check alone is not an immutable rule. - -## Acceptance and rollout - -In disposable checkouts, introduce one violation at a time: a mismatched -version, duplicate version macro, missing locale value, forbidden widget -call, dirty vendor file, or SSH submodule URL. Every applicable build entry -point must fail before emitting a new app artifact, including an incremental -build that had previously succeeded. Restore the file and verify the build -can proceed. Leave unrelated changes and real account data untouched. - -Then rebuild native, web, and Android targets, run the sync release checks, -and verify startup after an in-place Android update. Publish only through -the normal release workflow; never create a tag manually. - -## Bend-style proof boundary - -[Bend 2's guide](https://github.com/bendlang/bend/blob/main/guide/GUIDE.md) -describes laws discharged by proof terms. Kryon's current syntax checks and -runtime tests are not that proof system. This rollout does not add dependent -types, a new `law` syntax, or an external prover dependency. - -A later proof project must first define a pure, fully typed `.kry` subset, -termination and integer semantics, and explicit foreign-function assumptions. -Proofs must refer to the implementation actually emitted, not a separately -maintained model. Until that design is complete, label behavior checks as -tests and do not claim mathematical proof of arbitrary app or network code. +[Bend's upstream guide](https://github.com/bendlang/bend/blob/main/guide/GUIDE.md) +describes its law/proof convention. Inbe uses the actual checker rather than +naming ordinary assertions or tests mathematical proofs. diff --git a/droid/app/build.gradle b/droid/app/build.gradle index 92d9599af..770882dd2 100644 --- a/droid/app/build.gradle +++ b/droid/app/build.gradle @@ -136,6 +136,24 @@ androidComponents { } } +// Always run, including Java-only incremental builds. CMake independently +// checks its own generated header so direct native builds have the same gate. +tasks.register('checkBuildLaws') { + doLast { + exec { + workingDir rootProject.file('..') + commandLine 'python3', 'scripts/check-version.py' + } + exec { + workingDir rootProject.file('..') + commandLine 'node', 'scripts/generate-sync-retry.mjs' + } + } +} +tasks.named('preBuild').configure { + dependsOn 'checkBuildLaws' +} + gradle.taskGraph.whenReady { taskGraph -> def signedTasks = [":app:assembleRelease", ":app:bundleRelease", ":app:assembleGplay", ":app:bundleGplay"] if (signedTasks.any { taskGraph.hasTask(it) } diff --git a/droid/app/src/main/cpp/CMakeLists.txt b/droid/app/src/main/cpp/CMakeLists.txt index a292f70c4..3ca669f1d 100644 --- a/droid/app/src/main/cpp/CMakeLists.txt +++ b/droid/app/src/main/cpp/CMakeLists.txt @@ -83,6 +83,17 @@ list(APPEND KRYON_K2C_SOURCES ${KRYON_KIR_SOURCES} list(SORT KRYON_K2C_SOURCES) find_program(HOST_CC NAMES cc clang gcc REQUIRED) find_program(HOST_PYTHON NAMES python3 REQUIRED) +find_program(HOST_NODE NAMES node REQUIRED) +set(APP_PROOF_DIR "${CMAKE_BINARY_DIR}/proofs") +add_custom_target(app_build_laws + COMMAND "${HOST_PYTHON}" "${REPO_ROOT}/scripts/check-version.py" + COMMAND "${HOST_NODE}" "${REPO_ROOT}/scripts/generate-sync-retry.mjs" + "${APP_PROOF_DIR}/sync_retry_table.h" + BYPRODUCTS "${APP_PROOF_DIR}/sync_retry_table.h" + WORKING_DIRECTORY "${REPO_ROOT}" + COMMENT "Checking release consistency and proving sync retry laws" + VERBATIM +) set(KRYON_K2C "${CMAKE_BINARY_DIR}/host-tools/k2c") # needs the kir headers on the include path set(APP_KRY_GEN_DIR "${CMAKE_BINARY_DIR}/kryon/generated") @@ -165,7 +176,7 @@ add_custom_target(app_clean_runtime_api COMMENT "Rejecting stale or duplicate Kryon runtime API usage" VERBATIM ) -add_dependencies(app_kry_generated app_clean_runtime_api) +add_dependencies(app_kry_generated app_clean_runtime_api app_build_laws) add_custom_command( OUTPUT @@ -293,7 +304,7 @@ add_library(main SHARED "${NATIVE_APP_GLUE_DIR}/android_native_app_glue.c" ) add_dependencies(main app_kry_generated kryon_raylib_generated - kryon_runtime_generated) + kryon_runtime_generated app_build_laws) file(GLOB_RECURSE KRYON_ICON_FILES CONFIGURE_DEPENDS "${KRYON_DIR}/icons/*.png" "${KRYON_DIR}/icons/*.json") add_custom_command( @@ -310,6 +321,7 @@ add_custom_target(kryon_icon_assets_generated DEPENDS "${KRYON_ICON_ASSETS_C}" " add_dependencies(main kryon_icon_assets_generated) target_include_directories(main PRIVATE + "${APP_PROOF_DIR}" "${KRYON_ICON_GENERATED_INCLUDE_DIR}" "${KRYON_DIR}/include" "${KRYON_DIR}/src" diff --git a/fastlane/metadata/android/en-US/changelogs/124.txt b/fastlane/metadata/android/en-US/changelogs/124.txt index f52a81f56..d4ea52212 100644 --- a/fastlane/metadata/android/en-US/changelogs/124.txt +++ b/fastlane/metadata/android/en-US/changelogs/124.txt @@ -13,6 +13,7 @@ Fixed - Improved text selection, keyboard focus, and composition handling when editing text. - Connecting to sync runs in the background so network requests do not block the interface. - Sync retries temporary connection failures automatically without losing queued changes. +- Automatic friend-list retries stop when account details need attention, and retry delays recover safely from invalid saved counts. - Profile shows actionable sync status instead of internal repair messages. - Restoring an account key now starts synchronization and retrieves the account alias and existing friendships. Friend lists remain available when leaderboard loading fails. - Sync no longer asks you to upgrade just because the server supports a newer sync protocol. diff --git a/laws/sync_retry/LAWS.bend b/laws/sync_retry/LAWS.bend new file mode 100644 index 000000000..2f95375c3 --- /dev/null +++ b/laws/sync_retry/LAWS.bend @@ -0,0 +1,86 @@ +import Base +import ./main.bend as Policy + +# The contract owns the schedule; the implementation must prove it agrees. +def schedule(attempt: Policy.Attempt) -> Policy.Decision: + match attempt: + case Policy.Initial{}: + Policy.Decision{1, 5, 0} + case Policy.First{}: + Policy.Decision{2, 15, 0} + case Policy.Second{}: + Policy.Decision{3, 30, 0} + case Policy.Third{}: + Policy.Decision{4, 60, 0} + case Policy.Capped{}: + Policy.Decision{4, 60, 0} + +def saved_delay(attempt: Policy.Attempt) -> U32: + match attempt: + case Policy.Initial{}: + 5 + case Policy.First{}: + 5 + case Policy.Second{}: + 15 + case Policy.Third{}: + 30 + case Policy.Capped{}: + 60 + +def saved_attempt(attempt: Policy.Attempt) -> U32: + match attempt: + case Policy.Initial{}: + 0 + case Policy.First{}: + 1 + case Policy.Second{}: + 2 + case Policy.Third{}: + 3 + case Policy.Capped{}: + 4 + +law success_resets: + for attempt: Policy.Attempt + {Policy.decide(Policy.Success{}, attempt) == Policy.Decision{0, 0, 0} : Policy.Decision} + +law challenge_retries: + for +attempt: Policy.Attempt + {Policy.decide(Policy.ChallengeFailed{}, attempt) == schedule(attempt) : Policy.Decision} + +law request_retries: + for +attempt: Policy.Attempt + {Policy.decide(Policy.RequestFailed{}, attempt) == schedule(attempt) : Policy.Decision} + +law invalid_url_stops: + for +attempt: Policy.Attempt + {Policy.decide(Policy.InvalidUrl{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision} + +law no_account_stops: + for +attempt: Policy.Attempt + {Policy.decide(Policy.NoAccount{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision} + +law payload_failure_stops: + for +attempt: Policy.Attempt + {Policy.decide(Policy.PayloadFailed{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision} + +law signing_failure_stops: + for +attempt: Policy.Attempt + {Policy.decide(Policy.SignFailed{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision} + +law authentication_failure_stops: + for +attempt: Policy.Attempt + {Policy.decide(Policy.AuthFailed{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision} + +law unknown_result_stops: + for +attempt: Policy.Attempt + {Policy.decide(Policy.Unknown{}, attempt) == Policy.Decision{Policy.number(attempt), 0, 1} : Policy.Decision} + +law saved_delay_matches: + for +attempt: Policy.Attempt + {Policy.delay(attempt) == saved_delay(attempt) : U32} + +law attempt_number_matches: + for +attempt: Policy.Attempt + {Policy.number(attempt) == saved_attempt(attempt) : U32} diff --git a/laws/sync_retry/PROOF.bend b/laws/sync_retry/PROOF.bend new file mode 100644 index 000000000..ee1b40a7e --- /dev/null +++ b/laws/sync_retry/PROOF.bend @@ -0,0 +1,76 @@ +import Base +import ./main.bend as Policy +import ./LAWS.bend as Laws + +def Laws.success_resets(attempt): + {==} + +def Laws.challenge_retries(attempt): + match attempt: + case Policy.Initial{}: + {==} + case Policy.First{}: + {==} + case Policy.Second{}: + {==} + case Policy.Third{}: + {==} + case Policy.Capped{}: + {==} + +def Laws.request_retries(attempt): + match attempt: + case Policy.Initial{}: + {==} + case Policy.First{}: + {==} + case Policy.Second{}: + {==} + case Policy.Third{}: + {==} + case Policy.Capped{}: + {==} + +def Laws.invalid_url_stops(attempt): + {==} + +def Laws.no_account_stops(attempt): + {==} + +def Laws.payload_failure_stops(attempt): + {==} + +def Laws.signing_failure_stops(attempt): + {==} + +def Laws.authentication_failure_stops(attempt): + {==} + +def Laws.unknown_result_stops(attempt): + {==} + +def Laws.saved_delay_matches(attempt): + match attempt: + case Policy.Initial{}: + {==} + case Policy.First{}: + {==} + case Policy.Second{}: + {==} + case Policy.Third{}: + {==} + case Policy.Capped{}: + {==} + +def Laws.attempt_number_matches(attempt): + match attempt: + case Policy.Initial{}: + {==} + case Policy.First{}: + {==} + case Policy.Second{}: + {==} + case Policy.Third{}: + {==} + case Policy.Capped{}: + {==} diff --git a/laws/sync_retry/main.bend b/laws/sync_retry/main.bend new file mode 100644 index 000000000..11690a050 --- /dev/null +++ b/laws/sync_retry/main.bend @@ -0,0 +1,89 @@ +import Base + +type Result is Data: + Success{} + InvalidUrl{} + NoAccount{} + PayloadFailed{} + ChallengeFailed{} + SignFailed{} + RequestFailed{} + AuthFailed{} + Unknown{} + +type Attempt is Data: + Initial{} + First{} + Second{} + Third{} + Capped{} + +type Decision is Data: + Decision{attempt: U32, delay: U32, needs_action: U32} + +def number(attempt: Attempt) -> U32: + match attempt: + case Initial{}: + 0 + case First{}: + 1 + case Second{}: + 2 + case Third{}: + 3 + case Capped{}: + 4 + +def advance(attempt: Attempt) -> Attempt: + match attempt: + case Initial{}: + First{} + case First{}: + Second{} + case Second{}: + Third{} + case Third{}: + Capped{} + case Capped{}: + Capped{} + +def delay(attempt: Attempt) -> U32: + match attempt: + case Initial{}: + 5 + case First{}: + 5 + case Second{}: + 15 + case Third{}: + 30 + case Capped{}: + 60 + +def retry(attempt: Attempt) -> Decision: + +next = advance(attempt) + Decision{number(next), delay(next), 0} + +def stop(attempt: Attempt) -> Decision: + Decision{number(attempt), 0, 1} + +def decide(result: Result, attempt: Attempt) -> Decision: + match result: + case Success{}: + Decision{0, 0, 0} + case InvalidUrl{}: + stop(attempt) + case NoAccount{}: + stop(attempt) + case PayloadFailed{}: + stop(attempt) + case ChallengeFailed{}: + retry(attempt) + case SignFailed{}: + stop(attempt) + case RequestFailed{}: + retry(attempt) + case AuthFailed{}: + stop(attempt) + case Unknown{}: + stop(attempt) diff --git a/packaging/flatpak/xyz.waozi.inbe.yml b/packaging/flatpak/xyz.waozi.inbe.yml index 4b1613492..e20947b91 100644 --- a/packaging/flatpak/xyz.waozi.inbe.yml +++ b/packaging/flatpak/xyz.waozi.inbe.yml @@ -12,6 +12,22 @@ finish-args: - --device=dri - --filesystem=home modules: + # Host build tool only; removed from the finished application. + - name: node-for-proofs + buildsystem: simple + build-commands: + - install -D -m755 bin/node /app/bin/node + cleanup: + - '*' + sources: + - type: archive + url: https://nodejs.org/dist/v22.23.2/node-v22.23.2-linux-x64.tar.xz + sha256: d60acfe00a2932254bb0ad20e01b0d74397a0875595de719654b214f4b03f307 + only-arches: [x86_64] + - type: archive + url: https://nodejs.org/dist/v22.23.2/node-v22.23.2-linux-arm64.tar.xz + sha256: fff4078c5def658577f92c88db7db3bc0072924bfb93fe52c1e744a54e94abb8 + only-arches: [aarch64] - name: inbe buildsystem: simple build-commands: diff --git a/packaging/snap/snap/snapcraft.yaml b/packaging/snap/snap/snapcraft.yaml index 3698b6de7..3217cd55c 100644 --- a/packaging/snap/snap/snapcraft.yaml +++ b/packaging/snap/snap/snapcraft.yaml @@ -81,7 +81,12 @@ parts: - libegl1-mesa-dev - libgles2-mesa-dev - pkg-config + - curl + - xz-utils + - python3 override-build: | + sh scripts/install-build-node.sh /tmp/inbe-build-node + export PATH="/tmp/inbe-build-node/bin:$PATH" make vendor-prebuilds-native make native binary="$(find build/bin/linux -maxdepth 1 -type f -name 'inbe-linux-*' | head -n 1)" diff --git a/scripts/generate-sync-retry.mjs b/scripts/generate-sync-retry.mjs new file mode 100644 index 000000000..63eb3294b --- /dev/null +++ b/scripts/generate-sync-retry.mjs @@ -0,0 +1,90 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; +import { checkLaws } from '../vendor/kryon/tools/bend-laws.mjs'; + +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); +const results = ['Success', 'InvalidUrl', 'NoAccount', 'PayloadFailed', 'ChallengeFailed', + 'SignFailed', 'RequestFailed', 'AuthFailed', 'Unknown']; +const codes = ['SYNC_OK', 'SYNC_INVALID_URL', 'SYNC_NO_ACCOUNT', 'SYNC_PAYLOAD_FAILED', + 'SYNC_CHALLENGE_FAILED', 'SYNC_SIGN_FAILED', 'SYNC_REQUEST_FAILED', 'SYNC_AUTH_FAILED']; +const attempts = ['Initial', 'First', 'Second', 'Third', 'Capped']; +const requiredLaws = ['success_resets', 'challenge_retries', 'request_retries', + 'invalid_url_stops', 'no_account_stops', 'payload_failure_stops', + 'signing_failure_stops', 'authentication_failure_stops', 'unknown_result_stops', + 'saved_delay_matches', 'attempt_number_matches']; + +export async function generateSyncRetry(packageDir, output, syncHeader) { + const proof = await checkLaws(path.join(packageDir, 'PROOF.bend')); + assert.deepEqual(proof.laws.toSorted(), requiredLaws.toSorted(), 'sync.retry.required_laws'); + const decisions = proof.table('main.decide'); + const delays = proof.table('main.delay'); + const domain = names => names.map(name => `main.${name}`); + assert.deepEqual(decisions.domains, [domain(results), domain(attempts)], 'sync.retry.domains'); + assert.deepEqual(delays.domains, [domain(attempts)], 'sync.retry.delay_domain'); + const enumBody = fs.readFileSync(syncHeader, 'utf8').match(/typedef enum SyncResult\s*\{([^}]+)\}/)?.[1]; + assert.ok(enumBody, 'sync.retry.result_enum_missing'); + assert.deepEqual(enumBody.match(/\bSYNC_[A-Z_]+\b/g), codes, 'sync.retry.result_enum_changed'); + const rows = decisions.rows.map(row => { + assert.equal(row.value.constructor, 'main.Decision'); + assert.equal(row.value.fields.length, 3); + const [attempt, delay, action] = row.value.fields; + assert.ok(Number.isInteger(attempt) && attempt >= 0 && attempt <= 4); + assert.ok(Number.isInteger(delay) && delay >= 0 && delay <= 60); + assert.ok(action === 0 || action === 1); + return `{${attempt}, ${delay}, ${action}}`; + }); + const body = []; + for (let index = 0; index < results.length; index++) { + body.push(` {${rows.slice(index * 5, index * 5 + 5).join(', ')}}, /* ${results[index]} */`); + } + const seconds = delays.rows.map(row => { + assert.ok(Number.isInteger(row.value) && row.value > 0 && row.value <= 60); + return row.value; + }); + const text = `/* Generated by scripts/generate-sync-retry.mjs from proved Bend ${proof.version} policy. */ +#ifndef SYNC_RETRY_TABLE_H +#define SYNC_RETRY_TABLE_H +#include "sync.h" + +typedef struct RetryDecision { + int attempt; + int delay; + int needs_action; +} RetryDecision; + +/* Reject changed wire-code ordering at the C boundary, including Plan 9. */ +typedef char RetryResultCodesMatch[(${codes.map((code, index) => `${code} == ${index}`).join(' && ')}) ? 1 : -1]; + +static const RetryDecision retry_decisions[9][5] = { +${body.join('\n')} +}; +static const int retry_delays[5] = {${seconds.join(', ')}}; +#endif +`; + fs.mkdirSync(path.dirname(output), { recursive: true }); + if (!fs.existsSync(output) || fs.readFileSync(output, 'utf8') !== text) { + const temporary = `${output}.${process.pid}.tmp`; + try { + fs.writeFileSync(temporary, text); + fs.renameSync(temporary, output); + } finally { + fs.rmSync(temporary, { force: true }); + } + } + return proof; +} + +if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url) { + try { + assert.ok(process.argv.length <= 3, 'usage: node scripts/generate-sync-retry.mjs [output.h]'); + const output = path.resolve(process.argv[2] ?? path.join(root, 'build/proofs/sync_retry_table.h')); + const proof = await generateSyncRetry(path.join(root, 'laws/sync_retry'), output, + path.join(root, 'vendor/kryon/include/sync.h')); + console.log(`sync.retry: ${proof.laws.length} Bend laws proved; 45 decisions generated`); + } catch (error) { + console.error(error.message); + process.exitCode = 1; + } +} diff --git a/scripts/install-build-node.sh b/scripts/install-build-node.sh new file mode 100644 index 000000000..f3a891a4e --- /dev/null +++ b/scripts/install-build-node.sh @@ -0,0 +1,34 @@ +#!/bin/sh +# Explicit container build dependency setup; never invoked by the proof checker. +set -eu + +if command -v node >/dev/null 2>&1 && node -e ' + const [major, minor] = process.versions.node.split(".").map(Number); + process.exit(major > 22 || (major === 22 && minor >= 18) ? 0 : 1); +'; then + exit 0 +fi + +destination=${1:?Usage: install-build-node.sh destination} +case "$(uname -sm)" in + "Linux x86_64") + archive=node-v22.23.2-linux-x64.tar.xz + checksum=d60acfe00a2932254bb0ad20e01b0d74397a0875595de719654b214f4b03f307 + ;; + "Linux aarch64") + archive=node-v22.23.2-linux-arm64.tar.xz + checksum=fff4078c5def658577f92c88db7db3bc0072924bfb93fe52c1e744a54e94abb8 + ;; + *) + echo 'Install Node.js 22.18 or newer for this build host.' >&2 + exit 1 + ;; +esac + +temporary=$(mktemp -d) +trap 'rm -rf "$temporary"' EXIT HUP INT TERM +curl --fail --location --retry 3 "https://nodejs.org/dist/v22.23.2/$archive" -o "$temporary/$archive" +printf '%s %s\n' "$checksum" "$temporary/$archive" | sha256sum -c - +tar -xJf "$temporary/$archive" -C "$temporary" +mkdir -p "$destination/bin" +install -m755 "$temporary/${archive%.tar.xz}/bin/node" "$destination/bin/node" diff --git a/src/app/app_sync.kry b/src/app/app_sync.kry index e2d4b2c89..8b9a7185c 100644 --- a/src/app/app_sync.kry +++ b/src/app/app_sync.kry @@ -6,6 +6,7 @@ #import "storage.h" #private #import "sync_account.h" #private #import "sync_client.h" #private +#import "app/sync_retry.h" #private #import #import #import @@ -94,21 +95,30 @@ sync_unlock :: () #private { app_sync_record_result :: (result: int) { storage_set_setting_int("sync_last_result", result) - if result == SYNC_OK { - storage_set_setting_int("sync_retry_attempt", 0) - g_sync.retry_at = 0.0 + decision := sync_retry_decision(result, + storage_get_setting_int("sync_retry_attempt", 0)) + g_sync.retry_at = 0.0 + if decision.needs_action { return } - if app_sync_failure_needs_action(result) { - g_sync.retry_at = 0.0 + storage_set_setting_int("sync_retry_attempt", decision.attempt) + if decision.delay > 0 { + g_sync.retry_at = GetTime() + decision.delay + } +} + +app_social_record_result :: (result: int) #private { + decision := sync_retry_decision(result, g_sync.social_retry_attempt) + g_sync.social_retry_at = 0.0 + if decision.needs_action { + g_sync.social_refresh_pending = 0 return } - attempt := storage_get_setting_int("sync_retry_attempt", 0) + 1 - if attempt > 4 { - attempt = 4 + g_sync.social_retry_attempt = decision.attempt + if decision.delay > 0 { + g_sync.social_retry_at = GetTime() + decision.delay + g_sync.social_refresh_pending = 1 } - storage_set_setting_int("sync_retry_attempt", attempt) - g_sync.retry_at = GetTime() + app_sync_retry_delay(attempt) } app_sync_status_key :: () -> const char* { @@ -898,17 +908,7 @@ app_apply_pending_social_refresh :: (app: InnerBreeze*) #private { sizeof(action_value)) } g_sync.social_refresh_pending = g_sync.social_action_count > 0 - if result != SYNC_OK && !app_sync_failure_needs_action(result) { - g_sync.social_retry_attempt++ - if g_sync.social_retry_attempt > 4 { - g_sync.social_retry_attempt = 4 - } - g_sync.social_retry_at = GetTime() + app_sync_retry_delay(g_sync.social_retry_attempt) - g_sync.social_refresh_pending = 1 - } else if result == SYNC_OK { - g_sync.social_retry_attempt = 0 - g_sync.social_retry_at = 0.0 - } + app_social_record_result(result) } } @@ -946,21 +946,12 @@ app_collect_finished_social_refresh :: (app: InnerBreeze*) #private { if !finished { return } + app_social_record_result(result) if result != SYNC_OK { - if !app_sync_failure_needs_action(result) { - g_sync.social_retry_attempt++ - if g_sync.social_retry_attempt > 4 { - g_sync.social_retry_attempt = 4 - } - g_sync.social_retry_at = GetTime() + app_sync_retry_delay(g_sync.social_retry_attempt) - g_sync.social_refresh_pending = 1 - } TraceLog(LOG_WARNING, "SYNC: social refresh failed result=%d name=%s", result, GetSyncResultName(result)) return } - g_sync.social_retry_attempt = 0 - g_sync.social_retry_at = 0.0 app_social_apply_result(app, requests_json, friends_json, leaderboard_json, leaderboard_key, leaderboard_avg_json, leaderboard_avg_key) diff --git a/src/app/sync_retry.kry b/src/app/sync_retry.kry index 263b91178..542c3e8d6 100644 --- a/src/app/sync_retry.kry +++ b/src/app/sync_retry.kry @@ -1,20 +1,29 @@ #module "app.sync_retry" #import "sync.h" +#import "sync_retry_table.h" + +retry_attempt_index :: (attempt: int) -> int #private { + if attempt < 0 { + return 0 + } + if attempt > 4 { + return 4 + } + return attempt +} + +sync_retry_decision :: (result: int, attempt: int) -> RetryDecision #export { + if result < SYNC_OK || result > SYNC_AUTH_FAILED { + result = 8 + } + return retry_decisions[result][retry_attempt_index(attempt)] +} app_sync_failure_needs_action :: (result: int) -> int #export { - return result != SYNC_OK && result != SYNC_CHALLENGE_FAILED && - result != SYNC_REQUEST_FAILED + decision := sync_retry_decision(result, 0) + return decision.needs_action } app_sync_retry_delay :: (attempt: int) -> double #export { - if attempt <= 1 { - return 5.0 - } - if attempt == 2 { - return 15.0 - } - if attempt == 3 { - return 30.0 - } - return 60.0 + return retry_delays[retry_attempt_index(attempt)] } diff --git a/tests/sync_recovery_test.c b/tests/sync_recovery_test.c index e3d86f5af..c59a4ea14 100644 --- a/tests/sync_recovery_test.c +++ b/tests/sync_recovery_test.c @@ -1,5 +1,6 @@ #include "src/app/app_sync.c" #include +#include void PushInspectSource(const char *path, int line) { @@ -104,6 +105,44 @@ SyncResult sync_client_get_friend_stats(const char *url, const char *app, } int main(void) { + /* Exercise the C boundary, including corrupted persisted attempt values. */ + const int attempts[] = {INT_MIN, -1, 0, 1, 2, 3, 4, 5, INT_MAX}; + const int expected_attempts[] = {1, 1, 1, 2, 3, 4, 4, 4, 4}; + const int expected_delays[] = {5, 5, 5, 15, 30, 60, 60, 60, 60}; + for(unsigned index = 0; index < sizeof(attempts) / sizeof(attempts[0]); index++) { + for(int result = SYNC_OK; result <= SYNC_AUTH_FAILED; result++) { + retry_attempt = attempts[index]; + app_sync_record_result(result); + if(result == SYNC_OK) { + assert(retry_attempt == 0 && g_sync.retry_at == 0); + } else if(result == SYNC_CHALLENGE_FAILED || result == SYNC_REQUEST_FAILED) { + assert(retry_attempt == expected_attempts[index]); + assert(g_sync.retry_at == test_time + expected_delays[index]); + } else { + assert(retry_attempt == attempts[index]); + assert(g_sync.retry_at == 0); + } + } + } + app_sync_record_result(INT_MIN); + assert(g_sync.retry_at == 0 && app_sync_failure_needs_action(INT_MIN)); + app_sync_record_result(INT_MAX); + assert(g_sync.retry_at == 0 && app_sync_failure_needs_action(INT_MAX)); + assert(app_sync_retry_delay(INT_MIN) == 5); + assert(app_sync_retry_delay(INT_MAX) == 60); + g_sync.social_retry_attempt = INT_MAX; + app_social_record_result(SYNC_REQUEST_FAILED); + assert(g_sync.social_retry_attempt == 4); + assert(g_sync.social_retry_at == test_time + 60); + g_sync.social_action_count = 1; + app_social_record_result(SYNC_AUTH_FAILED); + assert(g_sync.social_retry_at == 0); + assert(g_sync.social_refresh_pending == 0); + assert(g_sync.social_action_count == 1); + app_social_record_result(SYNC_OK); + assert(g_sync.social_retry_attempt == 0); + g_sync.social_action_count = 0; + retry_attempt = 0; const double delays[] = {5, 15, 30, 60, 60}; for(unsigned i = 0; i < sizeof(delays) / sizeof(delays[0]); i++) { app_sync_record_result(SYNC_REQUEST_FAILED); diff --git a/tests/sync_retry_proof_test.mjs b/tests/sync_retry_proof_test.mjs new file mode 100644 index 000000000..7ab580706 --- /dev/null +++ b/tests/sync_retry_proof_test.mjs @@ -0,0 +1,106 @@ +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { spawnSync } from 'node:child_process'; +import { test } from 'node:test'; +import { generateSyncRetry } from '../scripts/generate-sync-retry.mjs'; + +const root = path.resolve(import.meta.dirname, '..'); +const header = path.join(root, 'vendor/kryon/include/sync.h'); + +async function fixture(run) { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'sync-retry-proof-')); + try { + fs.cpSync(path.join(root, 'laws/sync_retry'), path.join(directory, 'laws/sync_retry'), { recursive: true }); + const policy = path.join(directory, 'laws/sync_retry'); + const output = path.join(directory, 'build/proofs/sync_retry_table.h'); + await run({ directory, policy, output }); + } finally { + fs.rmSync(directory, { recursive: true, force: true }); + } +} + +test('proved table generation is deterministic and repairs stale generated content', async () => { + await fixture(async ({ policy, output }) => { + const proof = await generateSyncRetry(policy, output, header); + assert.equal(proof.laws.length, 11); + const first = fs.readFileSync(output, 'utf8'); + const mtime = fs.statSync(output).mtimeMs; + await generateSyncRetry(policy, output, header); + assert.equal(fs.statSync(output).mtimeMs, mtime); + fs.writeFileSync(output, 'stale or hand-edited output'); + await generateSyncRetry(policy, output, header); + assert.equal(fs.readFileSync(output, 'utf8'), first); + }); +}); + +test('policy changes that break each class of behavior fail their proofs', async () => { + const mutations = [ + [' 60', ' 61'], + [' Decision{0, 0, 0}', ' Decision{1, 0, 0}'], + [' case AuthFailed{}:\n stop(attempt)', ' case AuthFailed{}:\n retry(attempt)'], + [' case Capped{}:\n Capped{}', ' case Capped{}:\n Initial{}'], + ]; + for (const [before, after] of mutations) { + await fixture(async ({ policy, output }) => { + await generateSyncRetry(policy, output, header); + const old = fs.readFileSync(output, 'utf8'); + const source = path.join(policy, 'main.bend'); + const text = fs.readFileSync(source, 'utf8'); + assert.ok(text.includes(before)); + fs.writeFileSync(source, text.replace(before, after)); + await assert.rejects(generateSyncRetry(policy, output, header), /LAWS\./); + assert.equal(fs.readFileSync(output, 'utf8'), old); + }); + } +}); + +test('changed runtime enum cannot silently acquire an unproved mapping', async () => { + await fixture(async ({ directory, policy, output }) => { + const changedHeader = path.join(directory, 'sync.h'); + fs.writeFileSync(changedHeader, fs.readFileSync(header, 'utf8').replace('SYNC_AUTH_FAILED', 'SYNC_NEW_RESULT')); + await assert.rejects(generateSyncRetry(policy, output, changedHeader), /result_enum_changed/); + }); +}); + +test('C compilation rejects renumbered result codes', async () => { + await fixture(async ({ directory, policy, output }) => { + const enumDeclaration = fs.readFileSync(header, 'utf8').match(/typedef enum SyncResult\s*\{[^}]+\}\s*SyncResult;/)[0]; + const changedHeader = path.join(directory, 'sync.h'); + fs.writeFileSync(changedHeader, enumDeclaration.replace('SYNC_OK = 0', 'SYNC_OK = 1')); + await generateSyncRetry(policy, output, changedHeader); + const result = spawnSync(process.env.CC ?? 'cc', ['-x', 'c', '-fsyntax-only', '-I', directory, output], { + encoding: 'utf8', timeout: 30000, + }); + assert.ifError(result.error); + assert.notEqual(result.status, 0); + assert.match(result.stderr, /RetryResultCodesMatch/); + }); +}); + +test('actual Make proof prerequisite rejects broken policy despite an existing header', async () => { + await fixture(async ({ directory, policy, output }) => { + fs.mkdirSync(path.join(directory, 'scripts')); + fs.mkdirSync(path.join(directory, 'vendor')); + fs.mkdirSync(path.join(directory, 'src/core'), { recursive: true }); + for (const file of ['scripts/generate-sync-retry.mjs', 'src/core/version.h', 'Makefile']) { + fs.copyFileSync(path.join(root, file), path.join(directory, file)); + } + fs.symlinkSync(path.join(root, 'vendor/kryon'), path.join(directory, 'vendor/kryon'), 'dir'); + const make = () => spawnSync('make', ['build/proofs/sync_retry_table.h'], { + cwd: directory, encoding: 'utf8', timeout: 30000, + }); + const good = make(); + assert.ifError(good.error); + assert.equal(good.status, 0, good.stdout + good.stderr); + const old = fs.readFileSync(output, 'utf8'); + const source = path.join(policy, 'main.bend'); + fs.writeFileSync(source, fs.readFileSync(source, 'utf8').replace(' 60', ' 61')); + const bad = make(); + assert.ifError(bad.error); + assert.notEqual(bad.status, 0); + assert.match(bad.stdout + bad.stderr, /LAWS\./); + assert.equal(fs.readFileSync(output, 'utf8'), old); + }); +}); diff --git a/tests/version_test.py b/tests/version_test.py index c72e144d9..ebe24cacd 100644 --- a/tests/version_test.py +++ b/tests/version_test.py @@ -62,6 +62,26 @@ class VersionTest(unittest.TestCase): self.assertEqual(self.check("--print-version").stdout, self.version + "\n") self.assertEqual(before, self.snapshot()) + def test_build_gate_rejects_mismatch_after_success(self): + (self.root / "vendor").mkdir() + (self.root / "vendor/kryon").symlink_to(REPO / "vendor/kryon", target_is_directory=True) + shutil.copytree(REPO / "laws", self.root / "laws") + shutil.copy2(REPO / "scripts/generate-sync-retry.mjs", self.root / "scripts") + + def build(): + return subprocess.run(["make", "build-laws"], cwd=self.root, + capture_output=True, text=True, check=False) + + valid = build() + self.assertEqual(valid.returncode, 0, valid.stdout + valid.stderr) + generated = self.root / "build/proofs/sync_retry_table.h" + self.assertTrue(generated.exists()) + name = "droid/app/build.gradle" + self.write(name, self.read(name).replace(self.version, "9.8.7")) + invalid = build() + self.assertNotEqual(invalid.returncode, 0) + self.assertIn("expected", invalid.stdout + invalid.stderr) + def test_each_package_mismatch_is_rejected(self): for name in ("droid/app/build.gradle", "packaging/click/manifest.json", "packaging/click/control", "packaging/firefox-addons/manifest.json", diff --git a/vendor/kryon b/vendor/kryon index e89003b14..1bc6c970f 160000 --- a/vendor/kryon +++ b/vendor/kryon @@ -1 +1 @@ -Subproject commit e89003b14e2e75c3b4db6249e3ce72d46965a8e2 +Subproject commit 1bc6c970fa8c19c2b6abc9d4327dc7d15660a8c7