{
 "item": "fixed-affirm-config",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge Version: 1.9.0 (run with --use solar)",
 "verdict": "holds",
 "findings": [],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "Writers of affirmedAt: src/spokes/FixedLoans.sol:572 (_affirm, = block.timestamp) and :633 (_apply, = 0); never written in the constructor (:207-225, deploys stale, comment :72); no assembly/sstore/delegatecall in FixedLoans.sol or GovernedByKind.sol. Readers: _live :656 (open only) and openRoom :474; script/launch/SafeBatch.s.sol:249 (simulation). configHash :596-598 = keccak256(abi.encode(lendPrice, _terms, _limits)): Terms (8 static fields) and Limits (4) abi-encode word by word, no packing collision. testFuzz_C1_hashBindsEveryField (5000 runs): changing any one of the 13 fields (lendPrice, term, grace, feeBps, maxExtensions, startMultipleBps, halfLife, saleLength, floorBps, dailyLimit, surgeLimit, minDebt, affirmEvery) changes the hash; PASS. Writers of the hashed numbers: constructor :219, _lower :525, lowerDailyLimit :534, lowerSurgeLimit :543 (instant, tightening only), _apply :636/:639/:643 (with affirmedAt = 0 at :633)."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "FlGate.blocking script/launch/FlGate.sol:18-20 masks bits 2 and 3 (SET_TERMS 0x02, SET_LIMITS 0x03 equal FixedLoans :78-79); SafeBatch.fl refuses at script/launch/SafeBatch.s.sol:237. testFuzz_C2_gate (5000 runs, every 5-bit mask): non-zero iff bit 2 or 3; PASS. The other kinds cannot move the hash after the lower: SET_LEND_PRICE applied in front raises to q > lendPrice at build >= p (SafeBatch :235), and _lower (:522-528) brings it back to p, which the hash binds (:246); a pending one is dropped by _clear (:523); UNPAUSE is not hashed. testFuzz_P3_flFile (5000 runs; pause + UNPAUSE and a raise pending, any subset applied in front): lendPrice == p, no raise pending, affirm lands unless a SET_TERMS proposed after the build is applied in front; PASS. Invariant check 'C2: FL built with the gate clear skipped its affirm' never fired over 831 landed FL files. Note (docs only, no property impact): the first paragraph of FlGate.sol:4-9 and the SafeBatch header :67 still describe the file as a MultiSend of lowerLendPrice + affirm that reverts whole; the code at :246 is one lowerAndAffirm call."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "Not worse than README:713-715 / :701-702. A stranger's only lever is applyPending (src/GovernedByKind.sol applyPending: _take deletes the slot), so each pending proposal moves the hash or zeroes affirmedAt once; new proposals need the Safe, which consumes the Safe nonce and voids any signed affirm / FL file. A Safe tx whose affirm reverts ConfigChanged does not consume its nonce (safeTxGas 0), so the affirm built for the numbers after every pending change stays executable and lands once all are applied (testFuzz_P2, 5000 runs). The invariant handler models one signed Safe tx at the next nonce, voided by every Safe act (propose, lowers, pause), executed by anyone at any time: 51,200 calls x 256 runs, no route made a stale signed file affirm numbers other than those it hashed; a stranger can only choose WHEN a still-valid signed file runs, which README:701-702 states as written ('A signed FL file stays executable by anyone holding the signatures while its Safe nonce is unused: void it by a transaction at its nonce')."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "Known as written (README:703-705: 'the two-argument forms take whatever is in force'); no loss beyond minOut / maxPay found. test_C4_twoArgOpen (PASS): a stranger applies a matured SET_TERMS(1 day, 12 h grace, same fee) and executes the Safe's signed affirm in front: open(s, quotedOut, quotedHash) reverts ConfigChanged; open(s, quotedOut) runs at the quoted price and fee (minOut met) on a 1-day term instead of 14. What the 2-argument open loses vs the 3-argument one is only term/grace/sale curve (time to repay before forfeit), not money at the call: debt depends on lendPrice and vault rate, the fee on feeBps, both bounded by minOut. extend: paid = over + fee (:330-339) depends only on lend, t.feeBps (both in extendHash :452-455) and is bounded by maxPay; the term/grace/curve it copies are extendHash's t; liveUntil and the dA/dC minimum (:326-328) can only refuse. Source:\n    /// C4: a stranger applies a matured SET_TERMS and executes the Safe's signed affirm of it in front of two opens.\n    /// The 3-argument open reverts; the 2-argument open gets a 1-day term at the quoted price and fee (minOut met).\n    function test_C4_twoArgOpen() public {\n        _gov(abi.encode(uint8(2), FixedLoans.Terms(1 days, 12 hours, 100, 0, 10_000, 1 hours, 1 days, 2_500)));\n        _pass(48 hours);\n        _affirm(); // keep new loans open before the apply\n        bytes32 quoted = fl.configHash();\n        (, , uint256 quotedOut) = fl.quote(_sharesFor(100e18));\n        uint256 s = _sharesFor(100e18);\n        // front: apply + the Safe's signed affirm of the numbers after it\n        vm.prank(stranger);\n        fl.applyPending(2);\n        _affirm();\n        vm.prank(alice);\n        vm.expectRevert(FixedLoans.ConfigChanged.selector);\n        fl.open(s, quotedOut, quoted);\n        vm.prank(alice);\n        uint256 id = fl.open(s, quotedOut);\n        assertEq(fl.loan(id).expiry, block.timestamp + 1 days, \"1-day term vs 14 quoted\");\n        assertEq(fl.loan(id).grace, 12 hours);\n    }"
  }
 ],
 "holds": [
  {
   "property": "P1 open lends only at affirmed numbers (or tightened by an instant lower); every apply closes it; no apply gets numbers affirmed unread",
   "tried": "forge 1.9.0 --use solar, test/scratch/FixedAffirm.t.sol on FixedBase (real ImdUSD+MinterHub, FixedLoans at the spec numbers). Invariant FixedAffirmInvariant (256 runs x depth 200 = 51,200 calls, 0 handler reverts): Safe proposes any kind with random valid payloads (raise <=25%, 3 term sets incl. 1-day, limit raises with minDebt 1/50/100e18, affirmEvery 1/7/14 d, UNPAUSE), warps 0-3 days (same block when 0), a stranger applies any kind, the Safe signs affirm(configHash) or an affirm of the numbers after every pending payload or an FL file (gate read at build), anyone executes it, instant lowers and pause, 2- and 3-argument opens, stranger repays. Checks after every open: lendPrice <= last affirmed, terms/minDebt/affirmEvery == last affirmed, daily/surge <= last affirmed; after every apply: affirmedAt == 0, openRoom == 0 and an immediate open reverts. Totals: 958 opens, 1291 affirms, 435 applies, 831 FL landed, 11 FL skipped: no violation. UNPAUSE is not in the hash: a matured UNPAUSE applied in front of a signed affirm/FL file lets it land and loans open (the Safe's own proposal; no number changes) - tested in testFuzz_P3_flFile. Deployer: FixedLoans' governor is immutable cfg.governor = GOVERNANCE_SAFE (script/deploy/StageCore.sol:123-126, script/DeployConfig.sol:210), no deployer or wirer role. Mutation check NOT RUN (editing src in the clone was not permitted). Handler source:\n    function open(uint256 debt, bool bound3) external {\n        if (fl.lendPrice() == 0) return;\n        debt = _bound(debt, 50e18, 300e18);\n        uint256 s = _shares(debt);\n        uint256 id;\n        bool ok;\n        bytes32 h = fl.configHash();\n        vm.prank(alice);\n        if (bound3) {\n            try fl.open(s, 0, h) returns (uint256 i) {\n                (ok, id) = (true, i);\n            } catch {}\n        } else {\n            try fl.open(s, 0) returns (uint256 i) {\n                (ok, id) = (true, i);\n            } catch {}\n        }\n        if (!ok) return;\n        ++opens;\n        ids.push(id);\n        // P1: the numbers in force are the last affirmed ones, or tightened by an instant lower\n        FixedLoans.Limits memory x = fl.limits();\n        if (!aSet) _bad(\"P1: open before any affirm\");\n        if (fl.lendPrice() > aPrice) _bad(\"P1: open above the affirmed price\");\n        if (keccak256(abi.encode(fl.terms())) != aTerms) _bad(\"P1: open on unaffirmed terms\");\n        if (x.minDebt != aMinDebt || x.affirmEvery != aEvery) _bad(\"P1: open on unaffirmed minDebt/affirmEvery\");\n        if (x.dailyLimit > aDaily || x.surgeLimit > aSurge) _bad(\"P1: open above the affirmed limits\");\n    }\n    function applyAny(uint256 kind) external {\n        kind = _bound(kind, 1, 4);\n        vm.prank(stranger);\n        try fl.applyPending(uint8(kind)) {\n            ++applies;\n            if (fl.affirmedAt() != 0 || fl.openRoom() != 0) _bad(\"P1: an apply left new loans open\");\n            uint256 s = _shares(60e18);\n            vm.prank(alice);\n            try fl.open(s, 0) {\n                _bad(\"P1: open right after an apply\");\n            } catch {}\n        } catch {}\n    }"
  },
  {
   "property": "P2 an affirm built for the numbers after every pending change lands whoever applies; exits read neither",
   "tried": "forge 1.9.0 --use solar, test/scratch/FixedAffirm.t.sol on FixedBase (real ImdUSD+MinterHub, FixedLoans at the spec numbers). testFuzz_P2_affirmAfterPending (5000 runs): the Safe pauses, proposes SET_LEND_PRICE, SET_TERMS, SET_LIMITS and UNPAUSE, signs affirm(h*) of the numbers after all four; strangers (distinct accounts) apply a fuzzed subset in a fuzzed permutation, all in one block or 12 s apart, 48 h to 14 d after propose: the affirm lands iff every hashed kind was applied (UNPAUSE optional), reverts ConfigChanged otherwise; PASS. Each kind writes only its own field and _validate per kind reads only that field (:603-620), so the post-apply hash is order-independent. Exits: repay/forfeit/take/close (:276-300, :364-427) contain no read of affirmedAt, _affirmed*, _terms, _limits or lendPrice (grep); the invariant's stranger full repay never reverted across applies/stale periods. Source:\n    /// P2: the Safe pauses, proposes all four kinds, signs affirm(h*) of the numbers after all of them; a stranger applies\n    /// a fuzzed subset in a fuzzed order (same block or consecutive blocks). The affirm lands iff every hash kind was applied.\n    function testFuzz_P2_affirmAfterPending(uint256 order, uint8 subset, bool sameBlock, uint32 extra) public {\n        vm.startPrank(governor);\n        fl.pause();\n        FixedLoans.Limits memory x = _limits();\n        x.dailyLimit += 1e18;\n        x.minDebt = 60e18;\n        x.affirmEvery = 3 days;\n        fl.propose(abi.encode(uint8(1), uint128(0.8e18)));\n        fl.propose(abi.encode(uint8(2), _t1()));\n        fl.propose(abi.encode(uint8(3), x));\n        fl.propose(abi.encode(uint8(4)));\n        vm.stopPrank();\n        bytes32 hStar = keccak256(abi.encode(uint128(0.8e18), _t1(), x));\n        _pass(48 hours + uint256(extra) % (14 days - 60));\n        uint8[4] memory k = [uint8(1), 2, 3, 4];\n        for (uint256 i = 3; i > 0; --i) {\n            uint256 j = (order >> (i * 8)) % (i + 1);\n            (k[i], k[j]) = (k[j], k[i]);\n        }\n        bool all = true;\n        for (uint256 i; i < 4; ++i) {\n            if ((subset >> i) & 1 == 1) {\n                vm.prank(address(uint160(0xA000 + i)));\n                fl.applyPending(k[i]);\n                if (!sameBlock) _pass(12);\n            } else if (k[i] != 4) {\n                all = false;\n            }\n        }\n        vm.prank(governor);\n        if (!all) {\n            vm.expectRevert(FixedLoans.ConfigChanged.selector);\n            fl.affirm(hStar);\n            return;\n        }\n        fl.affirm(hStar);\n        assertEq(fl.affirmedAt(), block.timestamp);\n        assertEq(fl.configHash(), hStar);\n    }"
  },
  {
   "property": "P3 the Safe's FL file (lowerAndAffirm) lands whatever matured change a stranger applies in front",
   "tried": "forge 1.9.0 --use solar, test/scratch/FixedAffirm.t.sol on FixedBase (real ImdUSD+MinterHub, FixedLoans at the spec numbers). lowerAndAffirm :584-593 cannot revert past onlyGovernor (_clear never reverts, configHash is a view). testFuzz_P3_flFile (5000 runs, p in [0.1, 0.7]e18, any subset of {raise, UNPAUSE, SET_TERMS proposed while signing} applied in front, 48 h-5 d later): lendPrice == p, no SET_LEND_PRICE pending, affirm iff the hash still matches; PASS. Invariant: 'P3: the FL file reverted' / 'the lower did not land' / 'a pending raise survived' never fired (842 executed FL files). Test source:\n    /// P3 / C2: the FL file built with the gate clear (only SET_LEND_PRICE and UNPAUSE may be pending) lands its lower AND\n    /// its affirm whatever a stranger applies in front, in any order; built with SET_TERMS/SET_LIMITS proposed after the\n    /// build and applied in front, the lower lands and the affirm is skipped.\n    function testFuzz_P3_flFile(uint128 p, uint8 front, bool late, uint256 v) public {\n        p = uint128(_bound(p, 0.1e18, PRICE));\n        vm.startPrank(governor);\n        if (front & 4 != 0) fl.pause();\n        fl.propose(abi.encode(uint8(1), uint128(PRICE + 1 + v % (PRICE / 4))));\n        if (front & 4 != 0) fl.propose(abi.encode(uint8(4)));\n        vm.stopPrank();\n        assertEq(FlGate.blocking(fl.pendingKinds()), 0);\n        bytes32 hFL = keccak256(abi.encode(p, fl.terms(), fl.limits()));\n        if (late) {\n            // proposed while the file is being signed (the Safe's own act at an earlier nonce)\n            vm.prank(governor);\n            fl.propose(abi.encode(uint8(2), _t1()));\n        }\n        _pass(48 hours + v % 3 days);\n        if (front & 1 != 0) fl.applyPending(1);\n        if (front & 4 != 0 && front & 8 != 0) fl.applyPending(4);\n        if (late && front & 2 != 0) fl.applyPending(2);\n        vm.prank(governor);\n        bool ok = fl.lowerAndAffirm(p, hFL);\n        assertEq(fl.lendPrice(), p, \"the lower always lands\");\n        assertEq(fl.pendingKinds() & 2, 0, \"a pending raise is dropped\");\n        assertEq(ok, !(late && front & 2 != 0), \"skipped only for a hash kind applied in front\");\n        assertEq(fl.affirmedAt() != 0, ok);\n    }"
  }
 ],
 "notRun": [
  "No [fork] or [data] claims in the sheet: nothing run against an RPC (NOT RUN).",
  "Mutation sanity run (deleting affirmedAt = 0 in _apply) NOT RUN: editing src/ in the clone required approval."
 ]
}