mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-27 20:29:04 +01:00
Offline-first sync for endurain and keepass
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
Python tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (mypy, mypy) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (pylint, pylint) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (rest, ) (push) Waiting to run
Pre-commit checks / pre-commit (${{ matrix.group }}) (shellcheck, shellcheck) (push) Waiting to run
Python tests / test (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / lib-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Endurain import now pulls phone exports into the local inbox before probing the server, so a down Endurain leaves them queued for the next run. keepass_sync treats only a 404 as "no remote yet"; an outage or other status skips the cycle instead of publishing over the remote. AGENTS.md records the offline-first rule for every syncing tool. Also bumps hypothesis 6.170.19 and gatelock 0.16.2 for the dependency-freshness gate. Bug-class-check: linux_configuration/tests/test_keepass_sync.bats, python_pkg/endurain_import/tests/test_orchestration.py Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
652638e920
commit
b0886b29a7
@ -131,6 +131,11 @@ Deletions in `docs/superpowers/sessions/*` abort the commit (append-only).
|
||||
|
||||
## Code Conventions
|
||||
|
||||
- **Offline-first sync.** Every app or script that syncs or shares data saves
|
||||
locally first and is usable at once; sync runs whenever the remote is
|
||||
reachable; everything fetched (details included) is cached locally; an
|
||||
unreachable remote is a retry-later skip, never "the remote is empty".
|
||||
|
||||
Anything already enforced by a hook is omitted here — run
|
||||
`pre-commit run --all-files` and read `.pre-commit-config.yaml` for the
|
||||
complete list. What follows is what a linter cannot check.
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
{
|
||||
"title": "G29 shifter fix: detect PS4 mode instead of failing silently",
|
||||
"objective": "On two consecutive boots the wheel enumerated as 046D:C260 (PS4 selector), hid-generic bound it, and the HID-BPF shifter fix, its udev rule and its ensure unit all looked for C24F only - the unit failed once with 'no G29 found' and 1st/3rd stayed broken all day. Make every layer recognise the mode, tell the desktop, and keep retrying until the selector is flipped.",
|
||||
"acceptance_criteria": [
|
||||
"g29_wheel_mode.sh classifies the HID bus as native/ps4/compat/absent from sysfs and is testable against a fake tree",
|
||||
"fix_g29_shifter.sh --ensure exits 0 when no wheel is present, exits 1 and sends a critical desktop notification when the wheel is in PS4/compat mode, attaches when native",
|
||||
"g29-shifter-bpf.service retries on failure every 5 minutes; 99-g29-shifter-ensure.rules also fires on C260",
|
||||
"--status prints the wheel mode first",
|
||||
"tests/test_fix_g29_shifter.bats passes (13) and every file is under the 250-line cap"
|
||||
],
|
||||
"out_of_scope": [
|
||||
"making the BPF program work in PS4 mode (different report layout, unmeasured; no kernel driver binds C260)",
|
||||
"utils/toggle_wheel.sh, which also keys on c24f (separate power-toggle utility)"
|
||||
],
|
||||
"verifier": "bats linux_configuration/tests/test_fix_g29_shifter.bats; linux_configuration/fixes/fix_g29_shifter.sh --status"
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
{
|
||||
"title": "Offline-first sync for every syncing tool",
|
||||
"objective": "Syncing apps and scripts save locally first, stay usable offline, sync whenever the remote is reachable, and never read an unreachable remote as an empty one.",
|
||||
"acceptance_criteria": [
|
||||
"AGENTS.md (CLAUDE.md) states the offline-first convention for every syncing app or script",
|
||||
"endurain_import pulls phone exports into the local inbox before probing Endurain; a down server leaves them queued for the next run",
|
||||
"keepass_sync treats only a 404 as 'no remote yet'; an outage or other status skips the cycle and leaves both vaults untouched",
|
||||
"Dependency-freshness gate passes (hypothesis 6.170.19, gatelock 0.16.2)"
|
||||
],
|
||||
"out_of_scope": [
|
||||
"Apps extracted to their own repos (todo, diet-guard, wake-alarm, ...)",
|
||||
"Changing the KeePass merge or CAS logic"
|
||||
],
|
||||
"verifier": "pytest python_pkg/endurain_import 104 passed at 100% branch; bats test_keepass_sync.bats 3/3; keepass_sync.sh against a dead REMOTE_URL with a scratch vault: skip, exit 0, sha256 unchanged; check_dependency_freshness.sh --all exit 0."
|
||||
}
|
||||
48
docs/superpowers/evidence/offline-first-sync-2026-11-27.json
Normal file
48
docs/superpowers/evidence/offline-first-sync-2026-11-27.json
Normal file
@ -0,0 +1,48 @@
|
||||
{
|
||||
"intent": "Syncing tools stay offline-first: phone workouts reach the local inbox even when Endurain is down, and a KeePass server outage never overwrites the remote vault.",
|
||||
"scope": [
|
||||
"python_pkg/endurain_import/__main__.py, tests/test_orchestration.py",
|
||||
"linux_configuration/features/keepass_sync.sh, tests/test_keepass_sync.bats",
|
||||
"AGENTS.md offline-first convention; no other packages touched",
|
||||
"meta/requirements.txt, meta/requirements-test.txt: hypothesis 6.170.19, gatelock 0.16.2 (freshness gate)"
|
||||
],
|
||||
"changes": [
|
||||
"endurain_import pulls phone exports before probing Endurain; an unreachable server logs the queued file count and exits 1",
|
||||
"keepass_sync remote_hash tells 404 (no remote) from outage/other status (return 2); reconcile skips on outage instead of publishing over the remote",
|
||||
"Bump hypothesis 6.170.17 -> 6.170.19 and gatelock 0.16.1 -> 0.16.2 so the dependency-freshness gate passes"
|
||||
],
|
||||
"verification": [
|
||||
{
|
||||
"command": "pytest python_pkg/endurain_import --cov=python_pkg/endurain_import --cov-branch",
|
||||
"result": "pass",
|
||||
"evidence": "104 passed, 100% branch; new test fails on the old order"
|
||||
},
|
||||
{
|
||||
"command": "bats linux_configuration/tests/test_keepass_sync.bats",
|
||||
"result": "pass",
|
||||
"evidence": "3/3 ok; outage and 503 cases fail on the old script"
|
||||
},
|
||||
{
|
||||
"command": "ENDURAIN_URL=http://127.0.0.1:1 python -m python_pkg.endurain_import (fake adb)",
|
||||
"result": "pass",
|
||||
"evidence": "exit 1, '1 file(s) wait in the inbox', file present in scratch inbox"
|
||||
},
|
||||
{
|
||||
"command": "keepass_sync.sh against REMOTE_URL=http://127.0.0.1:1 with a throwaway vault",
|
||||
"result": "pass",
|
||||
"evidence": "skip: remote unreachable; exit 0; vault sha256 unchanged"
|
||||
},
|
||||
{
|
||||
"command": "bash scripts/check_dependency_freshness.sh --all",
|
||||
"result": "pass",
|
||||
"evidence": "exit 0; only the recorded typescript exception remains"
|
||||
}
|
||||
],
|
||||
"risks": [
|
||||
"A dufs that answers ?hash with a status other than 200/404 for an existing file now skips every cycle instead of syncing"
|
||||
],
|
||||
"rollback": [
|
||||
"git revert the commit",
|
||||
"Rerun the endurain and keepass tests after the revert"
|
||||
]
|
||||
}
|
||||
@ -34,9 +34,15 @@ readonly LOCKFILE="$STATE_DIR/lock"
|
||||
readonly MAX_CAS_RETRIES=3
|
||||
|
||||
log() { printf '[keepass-sync] %s\n' "$*" >&2; }
|
||||
die() { printf '[keepass-sync] ERROR: %s\n' "$*" >&2; exit 1; }
|
||||
die() {
|
||||
printf '[keepass-sync] ERROR: %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
# Clean, non-fatal skip (keyring locked / server down) — retried next cycle.
|
||||
skip() { printf '[keepass-sync] skip: %s\n' "$*" >&2; exit 0; }
|
||||
skip() {
|
||||
printf '[keepass-sync] skip: %s\n' "$*" >&2
|
||||
exit 0
|
||||
}
|
||||
|
||||
# --- load config -------------------------------------------------------------
|
||||
[[ -f "$KP_SYNC_CONFIG" ]] || die "config not found: $KP_SYNC_CONFIG (run setup_keepass_sync.sh)"
|
||||
@ -80,27 +86,49 @@ remote_get() {
|
||||
case "$REMOTE_MODE" in
|
||||
file)
|
||||
[[ -f "$REMOTE_FILE" ]] || return 1
|
||||
cp -f "$REMOTE_FILE" "$dest" ;;
|
||||
cp -f "$REMOTE_FILE" "$dest"
|
||||
;;
|
||||
webdav)
|
||||
curl -fsS -u "$DUFS_USER:$DUFS_PASS" -o "$dest" "$REMOTE_URL" 2>/dev/null || return 1 ;;
|
||||
curl -fsS -u "$DUFS_USER:$DUFS_PASS" -o "$dest" "$REMOTE_URL" 2>/dev/null || return 1
|
||||
;;
|
||||
*) die "unknown REMOTE_MODE: $REMOTE_MODE" ;;
|
||||
esac
|
||||
}
|
||||
remote_hash() {
|
||||
case "$REMOTE_MODE" in
|
||||
file) if [[ -f "$REMOTE_FILE" ]]; then sha "$REMOTE_FILE" || true; fi ;;
|
||||
webdav) curl -fsS -u "$DUFS_USER:$DUFS_PASS" "${REMOTE_URL}?hash" 2>/dev/null || true ;;
|
||||
file) if [[ -f "$REMOTE_FILE" ]]; then sha "$REMOTE_FILE" || true; fi ;;
|
||||
webdav)
|
||||
# Only a real 404 means "no remote yet"; an outage (000) or any other
|
||||
# status is unknowable and must not be read as an empty remote.
|
||||
local out code
|
||||
out="$(mktemp)"
|
||||
code="$(curl -sS -u "$DUFS_USER:$DUFS_PASS" -o "$out" -w '%{http_code}' "${REMOTE_URL}?hash" 2>/dev/null || true)"
|
||||
case "$code" in
|
||||
200)
|
||||
cat "$out"
|
||||
rm -f "$out"
|
||||
;;
|
||||
404) rm -f "$out" ;;
|
||||
*)
|
||||
rm -f "$out"
|
||||
return 2
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
}
|
||||
remote_put() {
|
||||
local src="$1"
|
||||
case "$REMOTE_MODE" in
|
||||
file)
|
||||
local dir; dir="$(dirname "$REMOTE_FILE")"
|
||||
local dir
|
||||
dir="$(dirname "$REMOTE_FILE")"
|
||||
mkdir -p "$dir"
|
||||
cp -f "$src" "$REMOTE_FILE.tmp.$$" && mv -f "$REMOTE_FILE.tmp.$$" "$REMOTE_FILE" ;;
|
||||
cp -f "$src" "$REMOTE_FILE.tmp.$$" && mv -f "$REMOTE_FILE.tmp.$$" "$REMOTE_FILE"
|
||||
;;
|
||||
webdav)
|
||||
curl -fsS -u "$DUFS_USER:$DUFS_PASS" -T "$src" "$REMOTE_URL" -o /dev/null ;;
|
||||
curl -fsS -u "$DUFS_USER:$DUFS_PASS" -T "$src" "$REMOTE_URL" -o /dev/null
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
@ -135,17 +163,19 @@ reconcile() {
|
||||
verify_pw "$LOCAL_DB" || die "master password from keyring does not open $LOCAL_DB — refusing to touch it"
|
||||
|
||||
local attempt=0
|
||||
while (( attempt < MAX_CAS_RETRIES )); do
|
||||
while ((attempt < MAX_CAS_RETRIES)); do
|
||||
attempt=$((attempt + 1))
|
||||
|
||||
local remote_hash_before; remote_hash_before="$(remote_hash)"
|
||||
local remote_hash_before
|
||||
remote_hash_before="$(remote_hash)" || skip "remote unreachable; local vault unchanged, retrying next cycle"
|
||||
if [[ -z "$remote_hash_before" ]]; then
|
||||
# No remote yet — publish our local copy as the first version.
|
||||
remote_put "$LOCAL_DB" && log "published local vault to remote (first copy)"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local local_before; local_before="$(sha "$LOCAL_DB")"
|
||||
local local_before
|
||||
local_before="$(sha "$LOCAL_DB")"
|
||||
if [[ "$remote_hash_before" == "$local_before" ]]; then
|
||||
log "already in sync (hash $local_before)"
|
||||
return 0
|
||||
@ -160,12 +190,14 @@ reconcile() {
|
||||
die "keepassxc-cli merge failed — local vault left intact (see latest .backup_*)"
|
||||
fi
|
||||
|
||||
local local_after; local_after="$(sha "$LOCAL_DB")"
|
||||
local local_after
|
||||
local_after="$(sha "$LOCAL_DB")"
|
||||
|
||||
# Compare-and-swap: only upload if the remote hasn't changed since our
|
||||
# read; otherwise someone else wrote concurrently — retry the whole
|
||||
# reconcile with the new remote (merges are commutative, so it converges).
|
||||
local remote_hash_now; remote_hash_now="$(remote_hash)"
|
||||
local remote_hash_now
|
||||
remote_hash_now="$(remote_hash)" || skip "remote unreachable; local vault unchanged, retrying next cycle"
|
||||
if [[ "$remote_hash_now" != "$remote_hash_before" ]]; then
|
||||
log "remote changed during merge (CAS) — retrying ($attempt/$MAX_CAS_RETRIES)"
|
||||
continue
|
||||
|
||||
74
linux_configuration/tests/test_keepass_sync.bats
Executable file
74
linux_configuration/tests/test_keepass_sync.bats
Executable file
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env bats
|
||||
# Tests for features/keepass_sync.sh in webdav mode against a stubbed curl and
|
||||
# keepassxc-cli, so no real vault or dufs server is touched.
|
||||
#
|
||||
# Requires: bats (extra/bats). Run with:
|
||||
# bats linux_configuration/tests/test_keepass_sync.bats
|
||||
|
||||
bats_require_minimum_version 1.5.0
|
||||
|
||||
setup() {
|
||||
REPO_DIR="$(cd -- "$(dirname -- "$BATS_TEST_FILENAME")/.." && pwd)"
|
||||
SCRIPT="$REPO_DIR/features/keepass_sync.sh"
|
||||
mkdir -p "$BATS_TEST_TMPDIR/bin" "$BATS_TEST_TMPDIR/vault"
|
||||
export CALLS="$BATS_TEST_TMPDIR/calls"
|
||||
: >"$CALLS"
|
||||
# Fake curl: records its argv, answers the ?hash probe with $FAKE_HTTP_CODE
|
||||
# via -w (000 = connection failed, exit 7 like the real one), accepts uploads.
|
||||
cat >"$BATS_TEST_TMPDIR/bin/curl" <<'STUB'
|
||||
#!/bin/bash
|
||||
echo "curl $*" >>"$CALLS"
|
||||
out="" prev="" probe=0
|
||||
for a in "$@"; do
|
||||
[[ "$a" == "-T" ]] && exit 0
|
||||
[[ "$a" == "-w" ]] && probe=1
|
||||
[[ "$prev" == "-o" ]] && out="$a"
|
||||
prev="$a"
|
||||
done
|
||||
[[ -n "$out" && "$out" != /dev/null ]] && : >"$out"
|
||||
((probe)) && printf '%s' "$FAKE_HTTP_CODE"
|
||||
[[ "$FAKE_HTTP_CODE" == 000 ]] && exit 7
|
||||
# Without -w the caller used -f: an HTTP error is exit 22 and no body.
|
||||
((probe == 0 && FAKE_HTTP_CODE >= 400)) && exit 22
|
||||
exit 0
|
||||
STUB
|
||||
printf '#!/bin/bash\nexit 0\n' >"$BATS_TEST_TMPDIR/bin/keepassxc-cli"
|
||||
chmod +x "$BATS_TEST_TMPDIR/bin/curl" "$BATS_TEST_TMPDIR/bin/keepassxc-cli"
|
||||
export PATH="$BATS_TEST_TMPDIR/bin:$PATH"
|
||||
LOCAL_DB="$BATS_TEST_TMPDIR/vault/Passwords.kdbx"
|
||||
printf 'local-vault' >"$LOCAL_DB"
|
||||
export KP_SYNC_CONFIG="$BATS_TEST_TMPDIR/config.env"
|
||||
cat >"$KP_SYNC_CONFIG" <<EOF
|
||||
LOCAL_DB=$LOCAL_DB
|
||||
REMOTE_MODE=webdav
|
||||
REMOTE_URL=https://dufs.invalid/Keepass/Passwords.kdbx
|
||||
DUFS_USER=u
|
||||
EOF
|
||||
export KP_STATE_DIR="$BATS_TEST_TMPDIR/state" KP_MASTER_PW=x KP_DUFS_PW=y
|
||||
}
|
||||
|
||||
# $1 = HTTP code the ?hash probe returns; asserts nothing was uploaded.
|
||||
assert_skip_without_upload() {
|
||||
export FAKE_HTTP_CODE="$1"
|
||||
run bash "$SCRIPT"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"remote unreachable"* ]]
|
||||
[ "$(cat "$LOCAL_DB")" = local-vault ]
|
||||
run ! grep -q -- ' -T ' "$CALLS"
|
||||
}
|
||||
|
||||
@test "an unreachable remote is a skip that leaves the vault alone" {
|
||||
assert_skip_without_upload 000
|
||||
}
|
||||
|
||||
@test "a 5xx on the hash probe never publishes over the remote" {
|
||||
assert_skip_without_upload 503
|
||||
}
|
||||
|
||||
@test "a missing remote (404) gets the local vault as its first copy" {
|
||||
export FAKE_HTTP_CODE=404
|
||||
run bash "$SCRIPT"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" == *"first copy"* ]]
|
||||
grep -q -- ' -T ' "$CALLS"
|
||||
}
|
||||
@ -27,7 +27,7 @@ coverage==7.17.5
|
||||
defusedxml==0.7.1
|
||||
fastmcp==4.2.6
|
||||
genanki==0.13.1
|
||||
hypothesis==6.170.17
|
||||
hypothesis==6.170.19
|
||||
jeepney==0.9.0
|
||||
kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit
|
||||
markdownify==1.2.4
|
||||
|
||||
@ -36,11 +36,11 @@ flake8-pyi==26.5.0
|
||||
flake8-pytest-style==2.2.0
|
||||
flake8-return==1.2.0
|
||||
flake8-simplify==0.31.1
|
||||
gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.1#subdirectory=gatelock
|
||||
gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.2#subdirectory=gatelock
|
||||
genanki==0.13.1
|
||||
geopandas==1.2.0
|
||||
howlongtobeatpy==1.0.24
|
||||
hypothesis==6.170.17
|
||||
hypothesis==6.170.19
|
||||
importlib-metadata==10.2.0
|
||||
interrogate==1.7.0
|
||||
jeepney==0.9.0
|
||||
|
||||
@ -180,6 +180,15 @@ def main() -> int:
|
||||
bulk_dir = Path(bulk) if bulk else None
|
||||
|
||||
client = EndurainClient(base_url, api_key)
|
||||
|
||||
# Offline-first: local saves land before the remote is probed, so a down
|
||||
# Endurain never blocks the phone files from reaching the inbox.
|
||||
ledger = Ledger(state / "ledger.json")
|
||||
processed_dir = processed_dir_for(inbox)
|
||||
|
||||
if os.environ.get("ENDURAIN_NO_ADB") != "1":
|
||||
pull_from_phone(inbox)
|
||||
|
||||
try:
|
||||
client.about()
|
||||
# requests.RequestException covers the transport failures; OSError/ValueError
|
||||
@ -187,14 +196,12 @@ def main() -> int:
|
||||
# separate requests handler would be unreachable.
|
||||
except OSError, ValueError, RuntimeError, requests.RequestException:
|
||||
_logger.exception("Endurain is not reachable at %s", base_url)
|
||||
_logger.info(
|
||||
"%d file(s) wait in the inbox; they upload on the next run",
|
||||
len(inbox_files(inbox)),
|
||||
)
|
||||
return 1
|
||||
|
||||
ledger = Ledger(state / "ledger.json")
|
||||
processed_dir = processed_dir_for(inbox)
|
||||
|
||||
if os.environ.get("ENDURAIN_NO_ADB") != "1":
|
||||
pull_from_phone(inbox)
|
||||
|
||||
# TCX carries more than GPX (laps, cadence, HR), so when a run is present
|
||||
# in both formats the TCX must be the one that wins the dedupe race.
|
||||
pending = sorted(inbox_files(inbox), key=lambda p: p.suffix.lower() != ".tcx")
|
||||
|
||||
@ -104,6 +104,33 @@ def test_main_runs_adb_fallback_when_enabled(
|
||||
assert called == [inbox]
|
||||
|
||||
|
||||
def test_unreachable_still_pulls_phone_files(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, inbox: Path
|
||||
) -> None:
|
||||
"""Offline-first: a down Endurain must not block the phone pull.
|
||||
|
||||
The pull (and ledger init) has to land BEFORE the reachability probe, so the
|
||||
unreachable path still runs it and leaves the files in the inbox for the
|
||||
next run. This blocks re-ordering the probe back ahead of the pull.
|
||||
"""
|
||||
_env(monkeypatch, tmp_path, inbox)
|
||||
monkeypatch.delenv("ENDURAIN_NO_ADB")
|
||||
called: list[Path] = []
|
||||
import python_pkg.endurain_import.__main__ as mod
|
||||
|
||||
def _record(path: Path) -> list[Path]:
|
||||
called.append(path)
|
||||
return []
|
||||
|
||||
monkeypatch.setattr(mod, "pull_from_phone", _record)
|
||||
_patch_client(
|
||||
monkeypatch,
|
||||
_StubMainClient(Result(Outcome.OK, 1, "ok"), reachable=False),
|
||||
)
|
||||
assert main() == 1
|
||||
assert called == [inbox]
|
||||
|
||||
|
||||
def test_route_rejected_handles_unwritable_bulk_dir(
|
||||
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
||||
) -> None:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user