{
 "item": "fixed-extensions",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge Version: 1.9.0, run with --use solar (solc 0.8.38 via solar), only test/scratch/FxExt.t.sol compiled (--match-path)",
 "verdict": "holds",
 "findings": [],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "Loan.expiry is written only at src/spokes/FixedLoans.sol:262 (open: now + term) and src/spokes/FixedLoans.sol:343 (extend: += affirmed term); Loan.grace only through _copyTerms src/spokes/FixedLoans.sol:670, called from open src/spokes/FixedLoans.sol:263 and extend src/spokes/FixedLoans.sol:344, plus the clamp src/spokes/FixedLoans.sol:347. forfeit/take/close/repay never write either (grep `expiry`/`grace` in src/ and script/: no other writer; scripts only construct FixedLoans, script/deploy/StageCore.sol:123). The clamp sets grace = was - newExpiry = oldGrace - term (+ any older kept part) < old grace, and only when the new grace would bring the forfeit date earlier, so a kept grace is never above the old one. Tested: testFuzz_P1_graceShrinkNeverEarlier (10000 runs) and the invariant ghost vGraceAboveOld (0)."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "Writers of Loan.debt/shares: open src/spokes/FixedLoans.sol:261 (creation), repay src/spokes/FixedLoans.sol:293 (full: anyone, rest==0 branch src/spokes/FixedLoans.sol:283-284; part: NotBorrower src/spokes/FixedLoans.sol:289), extend src/spokes/FixedLoans.sol:333 (debt only, paydown `over`, borrower only src/spokes/FixedLoans.sol:313), take src/spokes/FixedLoans.sol:399, close src/spokes/FixedLoans.sol:420. forfeit src/spokes/FixedLoans.sol:369-372 copies them to lotShares/lotDebt and moves the aggregate openDebt->saleDebt without changing l.debt/l.shares. No other writer in src/ or script/. invariant_debtSum (openDebt+saleDebt == hub outstanding with an OverBurn-checking hub) held over 102400 calls."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "Bounded exactly as README:720 says. After an extension, time left = lead + t0 where lead <= window(t0) (src/spokes/FixedLoans.sol:318); a second extension under a new affirmed term t1 needs lead + t0 <= window(t1) <= 3 days, so t0 < 3 days, and leaves lead + t0 + t1 <= t1 + window(t1). testFuzz_C3_doubleExtendBound (10000 runs, t0,t1 in [1d,90d], lead in [1s,window], the widened SET_TERMS matured and applied by a stranger, the Safe's signed affirm executed in the same block between her two extends) never left more than t1 + window(t1) and never doubled from t0 >= 3 days; test_C3_doubleHappensBounded shows the double does happen (1-day term, 90-day term affirmed: 2 extensions, left = 1h + 1d + 90d). Each extension pays its fee and counts against maxExtensions (src/spokes/FixedLoans.sol:319,341)."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "extend src/spokes/FixedLoans.sol:307-352 reads: paused (:308), extendTerms() (:309 = _affirmedLast, _affirmedEvery, lendPrice, _affirmedPrice, _affirmedTerms, _terms.feeBps/maxExtensions, _limits.minDebt, _affirmedMinDebt), the loan, collateral.convertToAssets (:321), and directly the same two sets' price/minimum for the whole-set minimum check (:326-328: _affirmedPrice, lendPrice, _affirmedMinDebt, _limits.minDebt; README:706 describes this), then hub.reserveShare/payReserve and hub.burn via _payFee/:350. It never reads affirmedAt (only _live src/spokes/FixedLoans.sol:656, openRoom :474, _affirm, _apply). _apply src/spokes/FixedLoans.sol:632-649 never writes any _affirmed* field. invariant_C4_applyNeverLoosens: after every stranger-applied SET_LEND_PRICE/SET_TERMS/SET_LIMITS (fuzzed, any moment), extendTerms gives lend <= both prices, fee >= both, maxExtensions <= both, minDebt >= both, liveUntil unchanged, term/grace/sale curve = the affirmed ones: 0 violations. An apply can only raise cost or refuse (fee, maxExtensions, minDebt), as README:706 states."
  },
  {
   "id": "C5",
   "result": "holds",
   "evidence": "One invariant covering open and extend (the only writers of expiry): for every OPEN loan L, at every block, (a) L.expiry - now <= T + window(T) where T = the affirmed term in force at L's last open/extend, (b) expiry + grace never decreases, and (c) L.extensions counts at most one write of expiry per pair (window, affirm epoch). Smallest design that removes the C3 residual: store in each loan the affirm epoch (a uint32 counter bumped in _affirm) of its last extension and require `block.timestamp >= oldExpiry - window` AND that the previous extension was not inside the same window (e.g. keep `lastExtendedAt` and require `lastExtendedAt < l.expiry - t.term`, i.e. no extension yet in the current window); then one extension per window whatever is affirmed in between. Tested as written: invariant_P3_onePerAffirmedSet ((a) and one extension per window per affirm epoch) and invariant_P1_forfeitNeverEarlier ((b)) held over 512 runs x 200 calls; the C3 double across an affirm is the only two-per-window route and is bounded by (a)."
  }
 ],
 "holds": [
  {
   "property": "P1 No extension brings a loan's forfeit date (expiry + grace) earlier, across any SET_TERMS anyone applies at any moment.",
   "tried": "Code: src/spokes/FixedLoans.sol:342-347 (was = old expiry + grace; expiry += term >= 1 day; grace clamped up to was - newExpiry). testFuzz_P1_graceShrinkNeverEarlier (10000 runs: opening grace in [12h,14d], then any SET_TERMS term/grace applied by a stranger and affirmed, extension 1s before expiry). Invariant handler (512 runs x 200 calls): open, warp (0-5 days), warpToWindow (inside window, and at the forfeit edge expiry+grace+0..2s), extend, extendAffirmExtend (two extends around the Safe's signed affirm in one block), repay full by a stranger, repay part, forfeit, take/close, proposeApply (SET_TERMS/SET_LIMITS/SET_LEND_PRICE matured and applied by a stranger), affirm, lowerLendPrice, vault rate rise: ghost vForfeitEarlier = 0, vGraceAboveOld = 0."
  },
  {
   "property": "P2 Anyone repays a loan in full until forfeit; only its borrower repays in part; no stranger's payment takes her extension, no apply closes it, none reverts her repayment.",
   "tried": "repay src/spokes/FixedLoans.sol:276-299 reads only the loan; amount clamped to debt (:280) so a dust payment in front cannot revert a full one; part only by the borrower (:289) and the rest >= l.minDebt, which only her own extend writes; hub.burn reads only outstanding (src/hub/MinterHub.sol:185-193) and hub rows are never removed (indexOf written only at MinterHub.sol:526). testFuzz_P2_fullRepayAnyState (10000 runs: after SET_TERMS + SET_LIMITS(minDebt 10000) applied, optional pause, warp 0-400 days, i.e. past expiry+grace before forfeit): a stranger's full repay always ran, state DONE, all 100e18 shares to the borrower. Invariant: every stranger full repay of an OPEN loan succeeded (vFullRepayReverted 0), every stranger partial repay reverted and her partial repay never moved expiry/grace (0). Applies never touch loans (_apply writes no Loan) and extend is the borrower's own (:313). A stranger's FULL repayment ending the loan (shares to her) is the README:724 design ('anyone still repays in full')."
  },
  {
   "property": "P3 At most one extension per window per set of affirmed numbers, only in the last min(3 days, term / 4) before expiry; across an affirm of a widened term, see C3.",
   "tried": "src/spokes/FixedLoans.sol:318 window = _window(affirmed term) and :343 adds that same term, so after one extension time left > term >= 4 x window: no second extension until the next window under the same affirmed term. Invariant invariant_P3_onePerAffirmedSet (512 x 200, with warpToWindow and extendAffirmExtend in one block, consecutive blocks, stranger applies of any SET_TERMS at any moment): no two extensions in one window within the same affirm epoch, and time left after any extension <= T + window(T) (0 violations); coverage probe confirmed >= 3 extensions and full repays were reached. Across an affirm: C3 (README:720)."
  }
 ],
 "notRun": [
  "No [fork] or [data] claims in this sheet: nothing run against a chain (NOT RUN). Earlier submissions (157d860f-..., 4d56c477-...) were not fetched; the wave-7 judge fixes (L2-L4) are in the code and test/unit/AuditW7JudgeFixed.t.sol at this commit.",
  "The real MinterHub/sIMD were replaced by a stand-in hub with the same outstanding/OverBurn accounting and a rising-rate share token; hub and vault internals are other items' scope."
 ],
 "tests": {
  "path": "test/scratch/FxExt.t.sol (in a clone at the commit)",
  "command": "FOUNDRY_INVARIANT_RUNS=512 FOUNDRY_INVARIANT_DEPTH=200 FOUNDRY_FUZZ_RUNS=10000 forge test --use solar --match-path test/scratch/FxExt.t.sol",
  "ran": true,
  "result": "[PASS] testFuzz_C3_doubleExtendBound(uint32,uint32,uint32) (runs: 10000, \u03bc: 1698857, ~: 1685426)\n[PASS] testFuzz_P1_graceShrinkNeverEarlier(uint32,uint32,uint32) (runs: 10000, \u03bc: 1405599, ~: 1406583)\n[PASS] testFuzz_P2_fullRepayAnyState(uint256,bool) (runs: 10000, \u03bc: 1109663, ~: 1087772)\n[PASS] test_C3_doubleHappensBounded() (gas: 1815218)\nSuite result: ok. 4 passed; 0 failed; 0 skipped; finished in 10.68s (21.72s CPU time)\n[PASS]\n[PASS] invariant_C4_applyNeverLoosens\n[PASS] invariant_P1_forfeitNeverEarlier\n[PASS] invariant_P2_repay\n[PASS] invariant_P3_onePerAffirmedSet\n[PASS] invariant_debtSum\n FxExtInvariant invariants (runs: 512, calls: 102400, reverts: 0)\nSuite result: ok. 1 passed; 0 failed; 0 skipped; finished in 20.36s (20.35s CPU time)",
  "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {Test} from \"forge-std/Test.sol\";\nimport {ERC20} from \"@openzeppelin/contracts/token/ERC20/ERC20.sol\";\nimport {IERC20} from \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\nimport {FixedLoans} from \"../../src/spokes/FixedLoans.sol\";\nimport {IMinterHub} from \"../../src/hub/IMinterHub.sol\";\n\ncontract XTok is ERC20 {\n    uint256 public rate = 1e18; // assets per 1e18 shares, only rises\n    constructor() ERC20(\"T\", \"T\") {}\n    function mint(address to, uint256 a) external { _mint(to, a); }\n    function burn(address from, uint256 a) external { _burn(from, a); }\n    function bump(uint256 bps) external { rate += rate * bps / 10_000; }\n    function convertToAssets(uint256 s) external view returns (uint256) { return s * rate / 1e18; }\n}\n\n/// Hub stand-in that keeps the real hub's `outstanding` accounting: burn reverts OverBurn above it.\ncontract XHub {\n    XTok public tok = new XTok();\n    uint256 public outstanding;\n    function token() external view returns (address) { return address(tok); }\n    function mint(address to, uint256 a) external returns (bool) { outstanding += a; tok.mint(to, a); return true; }\n    function burn(uint256 a) external returns (bool) { require(a <= outstanding, \"OverBurn\"); outstanding -= a; tok.burn(msg.sender, a); return true; }\n    function reportLoss(uint256 a) external returns (uint256 m) { m = a > outstanding ? outstanding : a; outstanding -= m; }\n    function payReserve(uint256 a) external { tok.transferFrom(msg.sender, address(this), a); }\n    function room(address) external pure returns (uint256) { return type(uint128).max; }\n    function reserveShare(address) external pure returns (uint256) { return 3_000; }\n}\n\n/// The handler is the Safe (governor) AND drives every stranger / borrower route.\ncontract H is Test {\n    FixedLoans public f;\n    XHub public hub;\n    XTok public usd;\n    XTok public coll;\n    address constant B = address(0xB0B);\n    address constant S = address(0x5712);\n    uint256[] public ids;\n    uint256 public epoch; // affirm count\n    mapping(uint256 => uint256) public lastExtEpoch;\n    mapping(uint256 => uint256) public lastExtLeftBound;\n    // ghosts\n    uint256 public vForfeitEarlier;\n    uint256 public vTwoPerEpoch;\n    uint256 public vLeftBound;\n    uint256 public vGraceAboveOld;\n    uint256 public vExpiryWrittenElsewhere;\n    uint256 public vFullRepayReverted;\n    uint256 public vLoosened;\n    uint256 public nExt;\n    uint256 public nDoubleAcrossAffirm;\n    uint256 public nFullRepay;\n    uint256 public nApply;\n\n    function T(uint32 term, uint32 grace, uint16 fee, uint8 maxExt) public pure returns (FixedLoans.Terms memory) {\n        return FixedLoans.Terms(term, grace, fee, maxExt, 15_000, 6 hours, 1 days, 5_000);\n    }\n\n    constructor() {\n        hub = new XHub();\n        usd = hub.tok();\n        coll = new XTok();\n        f = new FixedLoans(IMinterHub(address(hub)), IERC20(address(coll)), address(0x7EA5), address(this), 1e18,\n            T(14 days, 1 days, 100, 3), FixedLoans.Limits(1e30, 1e30, 50e18, 7 days));\n        coll.mint(B, 1e30);\n        usd.mint(B, 1e30);\n        usd.mint(S, 1e30);\n        vm.prank(B); coll.approve(address(f), type(uint256).max);\n        vm.prank(B); usd.approve(address(f), type(uint256).max);\n        vm.prank(S); usd.approve(address(f), type(uint256).max);\n        affirm();\n    }\n\n    function _id(uint256 s) internal view returns (uint256) { return ids.length == 0 ? 0 : ids[s % ids.length]; }\n\n    function open(uint256 amt) external {\n        amt = bound(amt, 50e18, 10_000e18);\n        vm.prank(B);\n        try f.open(amt, 0) returns (uint256 id) { ids.push(id); } catch {}\n    }\n\n    function warp(uint256 dt) external { vm.warp(block.timestamp + bound(dt, 0, 5 days)); }\n    /// warp into loan s's extension window (or its grace), then maybe extend in the same block\n    function warpToWindow(uint256 s, uint256 x, bool ext) external {\n        uint256 id = _id(s);\n        if (id == 0) return;\n        FixedLoans.Loan memory l = f.loan(id);\n        uint256 w = f.extendWindow();\n        uint256 target = uint256(l.expiry) - bound(x, 0, w + 1 hours);\n        if (x % 7 == 0) target = uint256(l.expiry) + l.grace + bound(x, 0, 2); // the forfeit edge\n        if (target > block.timestamp) vm.warp(target);\n        if (ext) extend(s);\n    }\n\n    function bumpRate(uint256 bps) external { coll.bump(bound(bps, 0, 500)); }\n\n    function _snap(uint256 id) internal view returns (FixedLoans.Loan memory) { return f.loan(id); }\n\n    function extend(uint256 s) public {\n        uint256 id = _id(s);\n        if (id == 0) return;\n        FixedLoans.Loan memory a = _snap(id);\n        (, , FixedLoans.Terms memory t,) = f.extendTerms();\n        vm.prank(B);\n        try f.extend(id, type(uint256).max) {\n            FixedLoans.Loan memory b = _snap(id);\n            nExt++;\n            if (uint256(b.expiry) + b.grace < uint256(a.expiry) + a.grace) vForfeitEarlier++;\n            if (b.grace > a.grace && b.grace != t.grace) vGraceAboveOld++;\n            if (uint256(b.expiry) - block.timestamp > uint256(t.term) + _w(t.term)) vLeftBound++;\n            // two extensions in ONE window (before the old expiry of the previous one)\n            if (lastExtEpoch[id] != 0 && lastExtLeftBound[id] > block.timestamp) {\n                if (lastExtEpoch[id] == epoch) vTwoPerEpoch++; // same affirmed set: P3 broken\n                else nDoubleAcrossAffirm++; // across an affirm: C3 (known), bounded by vLeftBound\n            }\n            lastExtEpoch[id] = epoch;\n            lastExtLeftBound[id] = a.expiry; // the old expiry: an extension before it was in the previous window\n        } catch {}\n    }\n\n    function _w(uint256 term) internal pure returns (uint256 w) { w = term / 4; if (w > 3 days) w = 3 days; }\n\n    /// the borrower extends, the Safe's signed affirm runs, she extends again: all in one block.\n    function extendAffirmExtend(uint256 s) external { extend(s); affirm(); extend(s); }\n\n    function repayFull(uint256 s) external {\n        uint256 id = _id(s);\n        if (id == 0) return;\n        FixedLoans.Loan memory a = _snap(id);\n        if (a.state != 1) return;\n        vm.prank(S);\n        try f.repay(id, type(uint256).max) { nFullRepay++; } catch { vFullRepayReverted++; }\n    }\n\n    function repayPart(uint256 s, uint256 amt) external {\n        uint256 id = _id(s);\n        if (id == 0) return;\n        FixedLoans.Loan memory a = _snap(id);\n        if (a.state != 1 || a.debt <= a.minDebt) return;\n        amt = bound(amt, 1, a.debt - a.minDebt);\n        vm.prank(S);\n        try f.repay(id, amt) { vExpiryWrittenElsewhere++; } catch {} // a stranger's part must revert\n        FixedLoans.Loan memory b = _snap(id);\n        vm.prank(B);\n        try f.repay(id, amt) {\n            FixedLoans.Loan memory c = _snap(id);\n            if (c.expiry != b.expiry || c.grace != b.grace) vExpiryWrittenElsewhere++;\n        } catch {}\n    }\n\n    function forfeit(uint256 s) external {\n        uint256 id = _id(s);\n        if (id == 0) return;\n        try f.forfeit(id) {} catch {}\n    }\n\n    function take(uint256 s) external {\n        uint256 id = _id(s);\n        if (id == 0) return;\n        vm.prank(S);\n        try f.take(id, type(uint256).max, type(uint256).max) {} catch {}\n        try f.close(id) {} catch {}\n    }\n\n    /// propose any valid SET_TERMS / SET_LIMITS / SET_LEND_PRICE, mature it, a stranger applies it in front of anything.\n    function proposeApply(uint8 kind, uint32 term, uint32 grace, uint16 fee, uint8 maxExt, uint96 minDebt) external {\n        kind = uint8(bound(kind, 1, 3));\n        bytes memory p;\n        if (kind == 2) {\n            p = abi.encode(uint8(2), T(uint32(bound(term, 1 days, 90 days)), uint32(bound(grace, 12 hours, 14 days)),\n                uint16(bound(fee, 1, 1000)), uint8(bound(maxExt, 0, 12))));\n        } else if (kind == 3) {\n            FixedLoans.Limits memory l = f.limits();\n            p = abi.encode(uint8(3), FixedLoans.Limits(l.dailyLimit, l.surgeLimit, uint96(bound(minDebt, 1e18, 10_000e18)), l.affirmEvery));\n        } else {\n            p = abi.encode(uint8(1), uint128(f.lendPrice() + f.lendPrice() / 5));\n        }\n        try this.gov(p) {} catch { return; }\n        (, uint128 lend0, FixedLoans.Terms memory t0, uint96 m0) = f.extendTerms();\n        (uint256 lu0,,,) = f.extendTerms();\n        vm.warp(block.timestamp + 48 hours);\n        vm.prank(S);\n        try f.applyPending(kind) {\n            nApply++;\n            (uint256 lu1, uint128 lend1, FixedLoans.Terms memory t1, uint96 m1) = f.extendTerms();\n            FixedLoans.Terms memory c = f.terms();\n            uint96 cm = f.limits().minDebt;\n            // past BOTH sets: the last affirmed (affT, affP, affM) and the ones in force now\n            if (lend1 > f.lendPrice() || lend1 > affP || t1.feeBps < c.feeBps || t1.feeBps < affT.feeBps\n                || t1.maxExtensions > c.maxExtensions || t1.maxExtensions > affT.maxExtensions || m1 < cm || m1 < affM\n                || lu1 != lu0 || t1.term != affT.term || t1.grace != affT.grace || t1.saleLength != affT.saleLength\n                || t1.halfLife != affT.halfLife || t1.startMultipleBps != affT.startMultipleBps || t1.floorBps != affT.floorBps) vLoosened++;\n            lend0; t0; m0;\n        } catch {}\n    }\n\n    function prop(uint32 term, uint32 grace) external {\n        this.gov(abi.encode(uint8(2), T(uint32(bound(term, 1 days, 90 days)), uint32(bound(grace, 12 hours, 14 days)), 100, 12)));\n    }\n\n    function app(uint8 kind) external { vm.prank(S); f.applyPending(kind); }\n\n    function gov(bytes memory p) external { require(msg.sender == address(this)); f.cancel(abi.decode(p, (uint8))); f.propose(p); }\n\n    FixedLoans.Terms affT;\n    uint128 affP;\n    uint96 affM;\n\n    function affirm() public {\n        f.affirm(f.configHash());\n        epoch++;\n        (affT, affP, affM) = (f.terms(), f.lendPrice(), f.limits().minDebt);\n    }\n\n    function lower(uint256 bps) external { f.lowerLendPrice(uint128(f.lendPrice() * bound(bps, 5_000, 10_000) / 10_000)); }\n\n    function idsLength() external view returns (uint256) { return ids.length; }\n}\n\ncontract FxExtInvariant is Test {\n    H h;\n\n    function setUp() public {\n        vm.warp(1_800_000_000);\n        h = new H();\n        targetContract(address(h));\n        bytes4[] memory sel = new bytes4[](14);\n        (sel[12], sel[13]) = (H.warpToWindow.selector, H.warpToWindow.selector);\n        (sel[0], sel[1], sel[2], sel[3]) = (H.open.selector, H.warp.selector, H.extend.selector, H.extendAffirmExtend.selector);\n        (sel[4], sel[5], sel[6], sel[7]) = (H.repayFull.selector, H.repayPart.selector, H.forfeit.selector, H.take.selector);\n        (sel[8], sel[9], sel[10], sel[11]) = (H.proposeApply.selector, H.affirm.selector, H.lower.selector, H.bumpRate.selector);\n        targetSelector(FuzzSelector(address(h), sel));\n    }\n\n    function invariant_P1_forfeitNeverEarlier() public view { assertEq(h.vForfeitEarlier(), 0); assertEq(h.vGraceAboveOld(), 0); }\n    function invariant_P2_repay() public view { assertEq(h.vFullRepayReverted(), 0, \"full repay reverted\"); assertEq(h.vExpiryWrittenElsewhere(), 0); }\n    function invariant_P3_onePerAffirmedSet() public view { assertEq(h.vTwoPerEpoch(), 0); assertEq(h.vLeftBound(), 0); }\n    function invariant_C4_applyNeverLoosens() public view { assertEq(h.vLoosened(), 0); }\n    function invariant_debtSum() public view {\n        FixedLoans f = h.f();\n        assertEq(uint256(f.openDebt()) + f.saleDebt(), h.hub().outstanding());\n    }\n    // coverage probes, run once with --match-test probe and EXPECTED to fail (they did: >=3 extensions, a full repay reached):\n    // invariant_probe_ext: assertLt(h.nExt(), 3); invariant_probe_repay: assertEq(h.nFullRepay(), 0)\n\n    function afterInvariant() public {\n        emit log_named_uint(\"extensions\", h.nExt());\n        emit log_named_uint(\"doubleAcrossAffirm\", h.nDoubleAcrossAffirm());\n        emit log_named_uint(\"fullRepays\", h.nFullRepay());\n        emit log_named_uint(\"applies\", h.nApply());\n    }\n}\n\n/// Targeted: coverage of the routes the invariant needs (an extension, a C3 double, a full repay after any apply).\ncontract FxExtUnit is Test {\n    H h;\n    FixedLoans f;\n    address constant B = address(0xB0B);\n\n    function setUp() public { vm.warp(1_800_000_000); h = new H(); f = h.f(); vm.warp(block.timestamp + 1 days); }\n\n    /// C3 bound: from a 1-day term, a widened term affirmed between two extends in ONE tx; time left after both\n    /// is at most the new term + its window, and impossible from a term >= 3 days (fuzzed widened term).\n    function testFuzz_C3_doubleExtendBound(uint32 t0, uint32 t1, uint32 lead) public {\n        t0 = uint32(bound(t0, 1 days, 90 days));\n        t1 = uint32(bound(t1, 1 days, 90 days));\n        h.proposeApply(2, t0, 1 days, 100, 12, 0);\n        h.affirm();\n        uint256 A = block.timestamp;\n        uint256 w0 = t0 / 4 > 3 days ? 3 days : t0 / 4;\n        lead = uint32(bound(lead, 1, w0));\n        if (A + 48 hours + lead > A + t0) vm.warp(A + 48 hours + lead - t0);\n        h.open(100e18);\n        uint256 id = h.ids(0);\n        vm.warp(f.loan(id).expiry - lead - 48 hours);\n        h.prop(t1, 1 days); // the widened term: matures exactly when she extends\n        vm.warp(f.loan(id).expiry - lead);\n        h.affirm(); // re-affirm the same numbers (keeps extensions live for a long t0)\n        h.extend(0);\n        assertEq(f.loan(id).extensions, 1);\n        h.app(2); // matured, a stranger applies it\n        h.affirm(); // the Safe's signed affirm, executed by her in the same block\n        h.extend(0);\n        FixedLoans.Loan memory l = f.loan(id);\n        uint256 w1 = t1 / 4 > 3 days ? 3 days : t1 / 4;\n        if (l.extensions == 2) assertLe(uint256(l.expiry) - block.timestamp, uint256(t1) + w1, \"left above the new term + window\");\n        if (l.extensions == 2) assertLt(t0, 3 days, \"double extension from a term >= 3 days\");\n        assertEq(h.vForfeitEarlier(), 0);\n    }\n\n    /// The known C3 double does happen (1-day term, 90-day term affirmed between her two extends): bounded.\n    function test_C3_doubleHappensBounded() public {\n        testFuzz_C3_doubleExtendBound(1 days, 90 days, 1 hours);\n        FixedLoans.Loan memory l = f.loan(h.ids(0));\n        assertEq(l.extensions, 2);\n        assertEq(uint256(l.expiry) - block.timestamp, 1 hours + 1 days + 90 days);\n    }\n\n    function _eta() internal view returns (uint256 e) { (, e) = f.pendingOf(2); }\n\n    /// P1 directly: any SET_TERMS applied and affirmed between extensions never brings expiry + grace earlier.\n    function testFuzz_P1_graceShrinkNeverEarlier(uint32 g0, uint32 t1, uint32 g1) public {\n        g0 = uint32(bound(g0, 12 hours, 14 days));\n        h.proposeApply(2, 7 days, g0, 100, 12, 0);\n        h.affirm();\n        h.open(100e18);\n        uint256 id = h.ids(0);\n        FixedLoans.Loan memory a = f.loan(id);\n        h.proposeApply(2, t1, g1, 100, 12, 0);\n        h.affirm();\n        vm.warp(a.expiry - 1);\n        a = f.loan(id);\n        h.extend(0);\n        FixedLoans.Loan memory b = f.loan(id);\n        assertEq(b.extensions, 1, \"extension did not run\");\n        assertGe(uint256(b.expiry) + b.grace, uint256(a.expiry) + a.grace);\n        assertLe(b.grace, a.grace > uint32(bound(g1, 12 hours, 14 days)) ? a.grace : uint32(bound(g1, 12 hours, 14 days)));\n    }\n\n    /// P2: after expiry + grace and any apply / pause, a stranger's full repay runs until forfeit; her shares go to her.\n    function testFuzz_P2_fullRepayAnyState(uint256 dt, bool pauseIt) public {\n        h.open(100e18);\n        uint256 id = h.ids(0);\n        h.proposeApply(2, 1 days, 12 hours, 1000, 0, 0);\n        h.proposeApply(3, 0, 0, 0, 0, 10_000e18);\n        if (pauseIt) { vm.prank(address(h)); f.pause(); }\n        vm.warp(block.timestamp + bound(dt, 0, 400 days));\n        uint256 before = h.coll().balanceOf(B);\n        h.repayFull(0);\n        assertEq(h.vFullRepayReverted(), 0);\n        assertEq(f.loan(id).state, 3);\n        assertEq(h.coll().balanceOf(B) - before, 100e18);\n    }\n}\n"
 }
}