taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

replay.py (5664B)


      1 # This file is part of GNU Taler
      2 # (C) 2026 Taler Systems S.A.
      3 #
      4 # GNU Taler is free software; you can redistribute it and/or modify it under the
      5 # terms of the GNU General Public License as published by the Free Software
      6 # Foundation; either version 3, or (at your option) any later version.
      7 #
      8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
      9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     10 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     11 #
     12 # You should have received a copy of the GNU General Public License along with
     13 # GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     14 """Replay a recorded episode, and shrink it to the steps that matter.
     15 
     16 An 80-step random walk that ends in a crash is not a bug report.  Delta
     17 debugging over the action list turns it into one: each candidate subsequence is
     18 replayed from the same starting state, and the shortest one that still produces
     19 the same finding fingerprint is what gets written out.
     20 
     21 Replay is not perfectly deterministic — wallet-core retries on its own clock —
     22 so a candidate is only discarded after it fails to reproduce `attempts` times.
     23 """
     24 
     25 from __future__ import annotations
     26 
     27 import argparse
     28 import json
     29 import sys
     30 import time
     31 from pathlib import Path
     32 from typing import Callable, Optional, Sequence
     33 
     34 import scenarios
     35 from device import Device
     36 from episode import Action, Episode
     37 from stack import Env
     38 
     39 SETUPS: dict[str, Callable] = {
     40     "fresh": lambda app, env: app.fresh_start(dev_mode=True),
     41     "fund_wallet": scenarios.fund_wallet,
     42     "funded_and_paid": scenarios.funded_and_paid,
     43 }
     44 
     45 
     46 def load_actions(path: Path) -> list[Action]:
     47     data = json.loads((path / "actions.json").read_text()
     48                       if path.is_dir() else path.read_text())
     49     return [Action(op=a["op"], arg=a.get("arg")) for a in data]
     50 
     51 
     52 def replay(
     53     dev: Device,
     54     actions: Sequence[Action],
     55     out_dir: Path,
     56     setup: str = "funded_and_paid",
     57     name: str = "replay",
     58 ) -> Episode:
     59     env = Env.load()
     60     ep = Episode(dev, out_dir, name=name)
     61     SETUPS[setup](ep.app, env)
     62     # The setup itself must not count as a finding of the sequence.
     63     ep.session = type(ep.session)()
     64     dev.logcat(clear=True)
     65     for action in actions:
     66         ep.perform(action)
     67         ep.observe(with_log=False)
     68     ep.observe(with_log=True)
     69     ep.finish({"setup": setup, "replayed": len(actions)})
     70     return ep
     71 
     72 
     73 def reproduces(ep: Episode, fingerprint: Optional[str]) -> bool:
     74     if fingerprint:
     75         return fingerprint in ep.session.findings
     76     return bool(ep.session.serious())
     77 
     78 
     79 def minimize(
     80     dev: Device,
     81     actions: Sequence[Action],
     82     out_dir: Path,
     83     fingerprint: Optional[str],
     84     setup: str = "funded_and_paid",
     85     attempts: int = 2,
     86 ) -> list[Action]:
     87     """Classic ddmin, with repeats to survive a flaky reproduction."""
     88 
     89     def still_fails(candidate: Sequence[Action], tag: str) -> bool:
     90         for attempt in range(attempts):
     91             ep = replay(dev, candidate, out_dir, setup=setup,
     92                         name=f"min-{tag}-{attempt}")
     93             if reproduces(ep, fingerprint):
     94                 return True
     95         return False
     96 
     97     current = list(actions)
     98     granularity = 2
     99     while len(current) >= 2:
    100         chunk = max(1, len(current) // granularity)
    101         reduced = False
    102         for i in range(0, len(current), chunk):
    103             candidate = current[:i] + current[i + chunk:]
    104             if not candidate:
    105                 continue
    106             if still_fails(candidate, f"{len(current)}-{i}"):
    107                 current = candidate
    108                 granularity = max(granularity - 1, 2)
    109                 reduced = True
    110                 break
    111         if not reduced:
    112             if granularity >= len(current):
    113                 break
    114             granularity = min(len(current), granularity * 2)
    115     return current
    116 
    117 
    118 def main() -> int:
    119     ap = argparse.ArgumentParser(description=__doc__)
    120     ap.add_argument("episode", type=Path, help="episode directory or actions.json")
    121     ap.add_argument("--fingerprint", help="the finding to preserve (default: any serious one)")
    122     ap.add_argument("--setup", default="funded_and_paid", choices=sorted(SETUPS))
    123     ap.add_argument("--minimize", action="store_true")
    124     ap.add_argument("--serial", default="emulator-5554")
    125     ap.add_argument("--out", type=Path, default=Path("/opt/taler-sim/run/out/replays"))
    126     args = ap.parse_args()
    127 
    128     dev = Device(args.serial)
    129     actions = load_actions(args.episode)
    130     print(f"{len(actions)} actions from {args.episode}")
    131 
    132     ep = replay(dev, actions, args.out, setup=args.setup,
    133                 name=f"replay-{int(time.time())}")
    134     ok = reproduces(ep, args.fingerprint)
    135     print(f"reproduced: {ok}")
    136     for f in ep.session.findings.values():
    137         print(f"  [{f.kind}] {f.fingerprint} {f.detail[:140]}")
    138     if not ok:
    139         print("nothing to minimize")
    140         return 1
    141 
    142     if args.minimize:
    143         target = args.fingerprint or next(iter(
    144             f.fingerprint for f in ep.session.serious()))
    145         shortest = minimize(dev, actions, args.out, target, setup=args.setup)
    146         print(f"\nminimized {len(actions)} -> {len(shortest)} actions:")
    147         for a in shortest:
    148             print("   ", a.describe())
    149         target_file = args.out / f"minimal-{target}.json"
    150         target_file.write_text(json.dumps(
    151             {"setup": args.setup, "fingerprint": target,
    152              "actions": [{"op": a.op, "arg": a.arg} for a in shortest]}, indent=2))
    153         print(f"written: {target_file}")
    154     return 0
    155 
    156 
    157 if __name__ == "__main__":
    158     sys.exit(main())