{
 "item": "core-yield-harvest",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge Version: 1.9.0 (--use solar, Solar 0.8.38)",
 "verdict": "findings",
 "findings": [
  {
   "id": "F1",
   "severity": "low",
   "title": "Base wstETH yield leg: a wrong Chainlink rate answer (negative read as uint256, or any spike) is read as the capped ceiling, and a stranger's harvest takes principal up to all the headroom accrued since the last poke",
   "property": "P1 (a harvest takes only shares that are yield since the mark); C3 (every leg takes a fall in full)",
   "claim": "C3, C2",
   "files": [
    "src/feeds/RateFeed.sol:63",
    "src/feeds/RateFeed.sol:73",
    "src/feeds/RateFeed.sol:75",
    "src/feeds/RateFeed.sol:89",
    "script/DeployConfig.sol:793",
    "script/DeployConfig.sol:802",
    "src/LedgerBase.sol:386",
    "src/LedgerBase.sol:394",
    "src/Ledger.sol:157"
   ],
   "sequence": "Base config: WSTETH yieldRate = spot = RateFeed(CL_WSTETH_RATE, latestAnswer(), cap 0.1%/day) x ETH/USD, median = market leg (DeployConfig.sol:793,802). (1) Alice opens 10 wstETH / 10,000 debt; mark = R0 = 1.25. (2) Nobody pokes for 30 days; the real rate rises 0.3% (about 3.65%/yr). (3) The rate proxy answers -1 (liveRate decodes int256 as uint256 = 2^256-1, RateFeed.sol:63) or any value above the real rate. rate() = min(live, ceiling) = the ceiling R0 x 1.03 (RateFeed.sol:73-76), not 0 and not a fall. (4) The median (market leg) is fresh, so the type stays priced; anyone calls Ledger.harvest(WSTETH,[id]) in that block. _harvest uses rateNow = ceiling, takes C x (ceiling - mark)/ceiling shares, credits 0.9 x their market value, sets the mark to the ceiling. A poke in the same window also ratchets highRate to the ceiling (RateFeed.sol:89), after which any later wrong answer reaches it with no cap at all (the 'return to highRate is uncapped' rule).",
   "expected": "taken <= C x (real - mark)/real = 0.0299 wstETH (only yield since the mark; a negative answer reads as dead/unpriced like a reverting source)",
   "actual": "taken = 0.2913 wstETH (9.7x the real yield): 0.2614 wstETH of principal moved to the Treasury, credited at (1 - yieldFee) of its market value. The borrower loses yieldFee x that principal (0.026 wstETH per 10 wstETH at 30 days unpoked; up to the 5% skew-agree range or more past it, since the market leg keeps it priced) if they exit before the real rate reaches the inflated mark; if they stay, their real yield is withheld until it does. The fuzz over 1-40 unpoked days and any negative or spiking answer fails on every input. Not covered by README:1135 as written ('`RateFeed`'s growth cap is measured from the last poke, so headroom accrues while nobody pokes'): that line states the headroom, not that a negative answer is not a fall nor that the harvest turns a wrong answer into a principal take. BaseWsteth.t.sol W1-W5 bound borrowing and liquidation only, not the harvest.",
   "reachableAtDeploy": true,
   "fix": "For a Chainlink rate source decode int256 and return ok=false on answer <= 0 (and read latestRoundData with a max age); for the harvest on Base use min(RateFeed.rate(), market-implied rate) or skip the harvest unless q.agree; do not let poke() raise highRate from a reading the market leg does not confirm.",
   "test": {
    "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {Modules} from \"test/utils/Modules.sol\";\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 {ImdUSD} from \"src/ImdUSD.sol\";\nimport {Parameters} from \"src/Parameters.sol\";\nimport {ParametersBase} from \"src/ParametersBase.sol\";\nimport {Pricer} from \"src/Pricer.sol\";\nimport {Treasury, ICoverable} from \"src/Treasury.sol\";\nimport {PositionNFT} from \"src/PositionNFT.sol\";\nimport {Ledger} from \"src/Ledger.sol\";\nimport {IPulse} from \"src/LedgerBase.sol\";\nimport {Redeemer} from \"src/Redeemer.sol\";\nimport {Liquidator} from \"src/Liquidator.sol\";\nimport {Join} from \"src/Join.sol\";\nimport {IFeed} from \"src/interfaces/IFeed.sol\";\nimport {RateFeed} from \"src/feeds/RateFeed.sol\";\nimport {Adjust} from \"src/interfaces/IPositions.sol\";\n\ncontract YTok is ERC20 {\n    constructor(string memory s) ERC20(s, s) {}\n    function mint(address to, uint256 a) external { _mint(to, a); }\n}\ncontract YFeed is IFeed {\n    uint256 public value; bool public stale;\n    constructor(uint256 v) { value = v; }\n    function set(uint256 v) external { value = v; }\n    function setStale(bool s) external { stale = s; }\n    function latestValue() external view returns (uint256, uint64) { return (value, uint64(block.timestamp)); }\n    function isStale() external view returns (bool) { return stale || value == 0; }\n    function maxAge() external pure returns (uint256) { return 1 hours; }\n}\ncontract YRate { uint256 public rate = 1e18; function set(uint256 r) external { rate = r; } }\n/// ERC-4626-style rate source (convertToAssets); a donation lifts `r`.\ncontract YVault { uint256 public r = 1e18; function set(uint256 x) external { r = x; }\n    function convertToAssets(uint256 s) external view returns (uint256) { return s * r / 1e18; } }\n/// Chainlink rate proxy as on Base: latestAnswer() returns int256.\ncontract YAgg { int256 public latestAnswer; function set(int256 a) external { latestAnswer = a; } }\n\ncontract YieldHarvestTest is Test {\n    bytes32 constant W = \"WSTETH-BASE\"; // median = market leg, spot = RateFeed(latestAnswer) x ETH, yieldRate = that RateFeed\n    bytes32 constant S = \"S-VAULT\";     // median = spot = RateFeed(convertToAssets) x USD, yieldRate = that RateFeed\n    bytes32 constant R = \"S-RAW\";       // plain rate source, for the redemption ordering test\n    uint256 constant CAP = uint256(0.001e18) / 1 days; // DeployConfig CAP\n    uint256 constant R0 = 1.25e18;\n    address governor = makeAddr(\"governor\");\n    address alice = makeAddr(\"alice\");\n    address bob = makeAddr(\"bob\");\n    address keeper = makeAddr(\"keeper\");\n\n    ImdUSD usd; Parameters params; Treasury treasury; Ledger ledger; Redeemer redeemer;\n    YTok wTok; YTok sTok; YTok rTok;\n    YFeed eth; YFeed mkt; YFeed usdF; YFeed rFeed;\n    YAgg agg; YVault vault; YRate rRate;\n    RateFeed rfW; RateFeed rfS;\n\n    function setUp() public {\n        vm.warp(1_800_000_000);\n        usd = new ImdUSD(address(this), 10_000_000e18, address(0));\n        params = Modules.newParameters(governor, address(this));\n        Pricer pricer = new Pricer(params);\n        treasury = new Treasury(governor, IERC20(address(usd)), address(this));\n        PositionNFT nft = new PositionNFT(address(this));\n        ledger = Modules.newLedger(usd, params, pricer, address(treasury), address(this), IPulse(address(0)), nft);\n        nft.bind(address(ledger));\n        redeemer = new Redeemer(ledger);\n        Liquidator liquidator = new Liquidator(ledger, pricer);\n        ledger.wire(address(redeemer), address(liquidator));\n        usd.wire(address(ledger), address(Modules.newHub(usd, governor, address(treasury))), address(params));\n        params.wire(address(ledger), address(usd));\n        treasury.wire(ICoverable(address(ledger)));\n\n        eth = new YFeed(2000e18); usdF = new YFeed(1e18); rFeed = new YFeed(1e18);\n        agg = new YAgg(); agg.set(int256(R0));\n        mkt = new YFeed(R0 * 2000);\n        rfW = new RateFeed(address(agg), bytes4(keccak256(\"latestAnswer()\")), false, eth, CAP);\n        vault = new YVault();\n        rfS = new RateFeed(address(vault), bytes4(keccak256(\"convertToAssets(uint256)\")), true, usdF, CAP);\n        rRate = new YRate();\n        wTok = _add(W, mkt, rfW, address(rfW), 1.45e18, 0.01e18);\n        sTok = _add(S, rfS, rfS, address(rfS), 1.08e18, 0.005e18);\n        rTok = _add(R, rFeed, rFeed, address(rRate), 1.08e18, 0.005e18);\n        params.seal();\n        address[2] memory us = [alice, bob];\n        for (uint256 i; i < 2; ++i) {\n            wTok.mint(us[i], 100e18); sTok.mint(us[i], 100_000e18); rTok.mint(us[i], 100_000e18);\n            vm.startPrank(us[i]);\n            wTok.approve(address(ledger), type(uint256).max); sTok.approve(address(ledger), type(uint256).max);\n            rTok.approve(address(ledger), type(uint256).max); usd.approve(address(ledger), type(uint256).max);\n            vm.stopPrank();\n            deal(address(usd), us[i], 100_000e18);\n        }\n        deal(address(usd), keeper, 100_000e18);\n        vm.prank(keeper); usd.approve(address(ledger), type(uint256).max);\n    }\n\n    function _add(bytes32 ilk, IFeed median, IFeed spot, address yr, uint64 mat, uint64 floor) private returns (YTok tok) {\n        tok = new YTok(string(abi.encodePacked(ilk)));\n        Join join = new Join(address(tok), address(this));\n        join.wire(address(ledger));\n        params.addGenesisIlk(ilk,\n            ParametersBase.Config({token: address(tok), join: address(join), median: median, spot: spot, yieldRate: yr,\n                pegAggregator: address(0), redeemable: true}),\n            ParametersBase.Risk({mat: mat, chop: 0.05e18, cut: 0.5e18, chip: 0.1e18, rateFloor: floor, yieldFee: 0.1e18,\n                skew: 0.05e18, pegFloor: 0, line: 10_000_000e18, dust: 100e18, redeemFloor: 0.005e18, redeemDivisor: 4}));\n    }\n    function _ids(uint256 id) private pure returns (uint256[] memory a) { a = new uint256[](1); a[0] = id; }\n    function _adj(int256 dc, int256 dd, uint64 rate) private pure returns (Adjust memory) {\n        return Adjust(dc, dd, rate, 0, 0, false, type(uint256).max, address(0));\n    }\n\n    // ---------------------------------------------------------------- C3 / C2 / P1: the Base leg\n    /// P1: a harvest takes only shares that are yield since the mark. Base wstETH's rate leg is Chainlink latestAnswer()\n    /// (int256) decoded as uint256 with no sanity of its own: a wrong answer (negative = 2^256-x, or any spike) is read as\n    /// the capped ceiling, so the harvest takes principal up to the headroom accrued since the last poke.\n    function testFuzz_C3_baseLegWrongAnswerHarvestsPrincipal(uint256 daysUnpoked, int256 wrong) public {\n        daysUnpoked = bound(daysUnpoked, 1, 40);\n        vm.prank(alice);\n        uint256 id = ledger.open(W, 0, _adj(10e18, 10_000e18, 0.02e18));\n        assertEq(ledger.position(id).yieldMark, R0);\n        vm.warp(block.timestamp + daysUnpoked * 1 days);\n        uint256 real = R0 + R0 * 1e14 * daysUnpoked / 1e18; // ~3.65%/yr real growth, under the 0.1%/day cap\n        agg.set(int256(real));\n        mkt.set(real * 2000); // market agrees with the real rate\n        uint256 realYield = uint256(10e18) * (real - R0) / real;\n        // the wrong answer: negative, or a spike above the real rate\n        if (wrong >= 0) wrong = int256(real) + int256(bound(uint256(wrong), 1e15, 1e18));\n        else wrong = -int256(bound(uint256(-(wrong + 1)), 0, 1e30)) - 1;\n        agg.set(wrong);\n        assertGt(rfW.rate(), real, \"a wrong answer reads above the real rate (negative: not a fall, not unpriced)\");\n        uint256 t0 = wTok.balanceOf(address(treasury));\n        vm.prank(bob);\n        ledger.harvest(W, _ids(id)); // a stranger, at the block the answer is wrong\n        uint256 taken = wTok.balanceOf(address(treasury)) - t0;\n        assertLe(taken, realYield + 1, \"P1: harvest took only yield since the mark\");\n    }\n\n    /// The same with the exact negative answer -1: rate() is the ceiling, not 0 (no fall taken, still priced).\n    function test_C3_negativeAnswerReadsAsCeiling() public {\n        vm.prank(alice);\n        uint256 id = ledger.open(W, 0, _adj(10e18, 10_000e18, 0.02e18));\n        vm.warp(vm.getBlockTimestamp() + 30 days);\n        uint256 real = R0 + R0 * 3e15 / 1e18; // +0.3% in 30 days\n        agg.set(int256(real)); mkt.set(real * 2000);\n        uint256 realYield = uint256(10e18) * (real - R0) / real;\n        agg.set(-1);\n        assertEq(rfW.rate(), R0 + R0 * CAP * 30 days / 1e18, \"negative answer = the capped ceiling\");\n        uint256 t0 = wTok.balanceOf(address(treasury));\n        vm.prank(bob);\n        ledger.harvest(W, _ids(id));\n        uint256 taken = wTok.balanceOf(address(treasury)) - t0;\n        emit log_named_uint(\"taken\", taken);\n        emit log_named_uint(\"realYield\", realYield);\n        assertLe(taken, realYield + 1, \"P1/C3: a negative answer harvests principal\");\n    }\n\n    // ---------------------------------------------------------------- C4: a harvest at a dipped spot leg\n    function testFuzz_C4_dippedLowBounded(uint256 dip, uint256 dt) public {\n        dip = bound(dip, 0, 0.05e18);\n        dt = bound(dt, 1 days, 30 days);\n        vm.prank(alice);\n        uint256 id = ledger.open(W, 0, _adj(10e18, 10_000e18, 0.02e18));\n        vm.warp(block.timestamp + dt);\n        uint256 real = R0 + R0 * 1e14 * (dt / 1 days) / 1e18;\n        agg.set(int256(real)); mkt.set(real * 2000);\n        uint256 snap = vm.snapshotState();\n        uint256 rb = ledger.reserveBacked();\n        ledger.harvest(W, _ids(id));\n        uint256 creditMedian = ledger.reserveBacked() - rb;\n        uint256 coll0 = ledger.position(id).collateral;\n        vm.revertToState(snap);\n        eth.set(2000e18 * (1e18 - dip) / 1e18); // spot leg dips within skew; median holds\n        ledger.harvest(W, _ids(id));\n        uint256 creditDip = ledger.reserveBacked() - rb;\n        assertEq(ledger.position(id).collateral, coll0, \"same shares taken\");\n        assertGe(creditDip + 2, creditMedian * (1e18 - 0.05e18) / 1e18, \"loss <= skew x credit\");\n        assertLe(creditDip, creditMedian);\n    }\n\n    // ---------------------------------------------------------------- C2 / P1: donations, pokes, timings\n    /// Random steps (warp, donation, poke, harvest by anyone) on an ERC-4626 leg: every harvest takes at most the yield\n    /// since the mark at the capped rate, its credit raises reserveBacked by exactly the debt it cancels, the shares reach\n    /// the Treasury, and the total taken equals C0 x (1 - mark0 / final mark) (no yield taken twice).\n    function testFuzz_C2_P1_donationsAndTimings(uint256[12] memory steps) public {\n        vm.prank(alice);\n        uint256 id = ledger.open(S, 0, _adj(10_000e18, 8_000e18, 0.02e18));\n        uint256 m0 = ledger.position(id).yieldMark;\n        uint256 live = 1e18;\n        uint256 start = vm.getBlockTimestamp();\n        for (uint256 i; i < 12; ++i) {\n            uint256 s = steps[i];\n            vm.warp(vm.getBlockTimestamp() + (s % 3 == 0 ? 0 : bound(s >> 8, 0, 5 days)));\n            if ((s >> 40) % 2 == 0) { live += live * bound(s >> 48, 0, 0.2e18) / 1e18; vault.set(live); } // donation\n            if ((s >> 120) % 3 == 0) rfS.poke();\n            uint256 rNow = rfS.rate();\n            assertLe(rNow, live, \"never above live\");\n            assertLe(rNow, 1e18 + 2 * CAP * (vm.getBlockTimestamp() - start), \"within the cap\");\n            if ((s >> 130) % 2 == 0) {\n                Ledger.Position memory p = ledger.position(id);\n                uint256 debt = ledger.debtOf(id);\n                uint256 rb = ledger.reserveBacked();\n                uint256 t0 = sTok.balanceOf(address(treasury));\n                vm.prank(bob);\n                ledger.harvest(S, _ids(id));\n                Ledger.Position memory q = ledger.position(id);\n                uint256 taken = p.collateral - q.collateral;\n                if (rNow > p.yieldMark) assertLe(taken, uint256(p.collateral) * (rNow - p.yieldMark) / rNow, \"yield only\");\n                else assertEq(taken, 0);\n                assertEq(sTok.balanceOf(address(treasury)) - t0, taken, \"shares to the Treasury\");\n                assertEq(ledger.reserveBacked() - rb, debt - ledger.debtOf(id), \"reserveBacked += credit\");\n                assertGe(q.yieldMark, p.yieldMark, \"mark never falls\");\n            }\n        }\n        uint256 mf = ledger.position(id).yieldMark;\n        uint256 total = 10_000e18 - ledger.position(id).collateral;\n        assertLe(total, 10_000e18 * (mf - m0) / mf + 12, \"no yield harvested twice\");\n    }\n\n    // ---------------------------------------------------------------- P2 / C5: unpriced skip, then a debt-free period\n    function testFuzz_C5_debtFreePeriodNotHarvested(uint256 d1, uint256 d2, uint256 d3) public {\n        d1 = bound(d1, 1 hours, 20 days); d2 = bound(d2, 1 hours, 20 days); d3 = bound(d3, 1 hours, 20 days);\n        vm.prank(alice);\n        uint256 id = ledger.open(S, 0, _adj(10_000e18, 5_000e18, 0.02e18));\n        vm.warp(block.timestamp + d1); vault.set(1e18 + 1e18 * d1 / 1000 days); // under the cap\n        usdF.setStale(true); // unpriced: the repay's harvest is skipped, the mark lags\n        vm.prank(alice);\n        ledger.adjust(id, _adj(0, -int256(10_000e18), 0)); // repay all\n        assertEq(ledger.debtOf(id), 0);\n        vm.warp(block.timestamp + d2); vault.set(1e18 + 1e18 * (d1 + d2) / 1000 days);\n        usdF.setStale(false);\n        vm.prank(bob); ledger.harvest(S, _ids(id)); // anyone, in front\n        uint256 c0 = ledger.position(id).collateral;\n        assertEq(c0, 10_000e18, \"debt-free period: nothing taken\");\n        vm.prank(alice);\n        ledger.adjust(id, _adj(0, 5_000e18, 0.02e18)); // reborrow\n        uint256 mark = ledger.position(id).yieldMark;\n        assertEq(mark, rfS.rate(), \"mark followed the rate while debt-free\");\n        vm.warp(block.timestamp + d3); vault.set(1e18 + 1e18 * (d1 + d2 + d3) / 1000 days);\n        uint256 rNow = rfS.rate();\n        vm.prank(bob); ledger.harvest(S, _ids(id));\n        assertLe(c0 - ledger.position(id).collateral, c0 * (rNow - mark) / rNow, \"only yield since the reborrow\");\n    }\n\n    // ---------------------------------------------------------------- P3: does a harvest in front change redemption?\n    /// A's yield pays its whole debt; B keeps debt. A redemption visiting 1 position: with A harvested in front it fills\n    /// from B; without, A's in-call harvest leaves it debt-free and the redemption reverts NothingRedeemed.\n    function test_P3_harvestInFrontChangesRedemption() public {\n        vm.prank(alice); uint256 a = ledger.open(R, 0, _adj(4_000e18, 1_000e18, 0.01e18));\n        vm.prank(bob); uint256 b = ledger.open(R, 0, _adj(4_000e18, 3_000e18, 0.05e18));\n        rRate.set(1.5e18);\n        uint256 snap = vm.snapshotState();\n        ledger.harvest(R, _ids(a));\n        vm.prank(keeper);\n        (uint256 redeemedHarvested,) = redeemer.redeem(R, 500e18, 1e18, 1, 0);\n        assertEq(redeemedHarvested, 500e18, \"harvested in front: fills from B\");\n        assertLt(ledger.debtOf(b), 3_000e18);\n        vm.revertToState(snap);\n        uint256 redeemedPlain;\n        vm.prank(keeper);\n        try redeemer.redeem(R, 500e18, 1e18, 1, 0) returns (uint256 x, uint256) { redeemedPlain = x; } catch {}\n        assertEq(redeemedPlain, redeemedHarvested, \"P3: whether anyone harvests first changes no outcome\");\n    }\n\n    // ---------------------------------------------------------------- P4\n    function testFuzz_P4_reserveBackedFallsOnlyByOwnBurn(uint256 amt, address who) public {\n        vm.assume(who != address(0) && who != address(treasury));\n        vm.prank(alice); uint256 a = ledger.open(R, 0, _adj(4_000e18, 3_000e18, 0.01e18));\n        rRate.set(1.1e18);\n        ledger.harvest(R, _ids(a));\n        uint256 rb = ledger.reserveBacked();\n        assertGt(rb, 0);\n        vm.prank(who);\n        vm.expectRevert();\n        treasury.retireReserve(1); // governor only\n        amt = bound(amt, 0, 2 * rb);\n        deal(address(usd), who, amt);\n        uint256 supply = usd.totalSupply();\n        vm.prank(who);\n        ledger.retire(amt);\n        uint256 burned = amt < rb ? amt : rb;\n        assertEq(ledger.reserveBacked(), rb - burned);\n        assertEq(usd.totalSupply(), supply - burned, \"burned the caller's own imdUSD\");\n        assertEq(usd.balanceOf(who), amt - burned);\n    }\n}\n",
    "ran": "forge test --use solar --match-path test/scratch/YieldHarvest.t.sol (forge 1.9.0, Solar 0.8.38; file placed at test/scratch/ of the audit repo at ff1c192) --fuzz-runs 1000 -vv",
    "result": "[FAIL: P1/C3: a negative answer harvests principal: 291262135920520312 > 29910269192422732] test_C3_negativeAnswerReadsAsCeiling() (logs: taken 291262135920520312, realYield 29910269192422731)\n[FAIL: P1: harvest took only yield since the mark: 224828934504947306 > 22947221390801158; counterexample: args=[93097179437764047388103, 243815997144329243107354132717725]] testFuzz_C3_baseLegWrongAnswerHarvestsPrincipal(uint256,int256)\nSame file: [PASS] testFuzz_C2_P1_donationsAndTimings (1000 runs), testFuzz_C4_dippedLowBounded (1000), testFuzz_C5_debtFreePeriodNotHarvested (1000), testFuzz_P4_reserveBackedFallsOnlyByOwnBurn (256)"
   }
  },
  {
   "id": "F2",
   "severity": "info",
   "title": "Whether anyone harvests first changes a redemption's outcome: a position whose pending yield pays all its debt stays first in the redemption list and uses up one of maxPositions, so the redemption fills less or reverts NothingRedeemed",
   "property": "P3 (redemption acts on the harvested state, so whether anyone harvests first changes no outcome)",
   "claim": "P3",
   "files": [
    "src/Redeemer.sol:121",
    "src/Redeemer.sol:123",
    "src/LedgerSettlement.sol:74",
    "src/LedgerSettlement.sol:76",
    "src/Ledger.sol:171"
   ],
   "sequence": "A opens 4,000 / 1,000 debt at 1% (first in the list), B 4,000 / 3,000 at 5%. The share rate rises to 1.5: A's pending yield (1,200 credit) pays all of A's debt. Branch 1: anyone harvests A first (Ledger.harvest relists A out), then redeem(R, 500, maxFee, maxPositions=1) fills 500 from B. Branch 2 (same state, no harvest in front): the Redeemer visits A, redeemPosition's own harvest pays A to zero, skip, taken 0, loop ends at maxPositions; NothingRedeemed.",
   "expected": "same redemption in both branches",
   "actual": "branch 1 redeems 500e18, branch 2 reverts (0 redeemed). No funds lost: the redeemer's transaction reverts or fills less; with a larger maxPositions the fill is the same. Severity info.",
   "reachableAtDeploy": true,
   "fix": "In Redeemer.redeem, do not count positions that redeemPosition skipped because its harvest left no debt against maxPositions (or keep visiting while taken == 0 and debt == 0), or document P3 as holding for the amounts only.",
   "test": {
    "source": "// harness: setUp/_add/_ids/_adj of F1's file (ilk R: plain rate source YRate, median = spot = YFeed 1e18, yieldFee 10%, dust 100)\n    /// A's yield pays its whole debt; B keeps debt. A redemption visiting 1 position: with A harvested in front it fills\n    /// from B; without, A's in-call harvest leaves it debt-free and the redemption reverts NothingRedeemed.\n    function test_P3_harvestInFrontChangesRedemption() public {\n        vm.prank(alice); uint256 a = ledger.open(R, 0, _adj(4_000e18, 1_000e18, 0.01e18));\n        vm.prank(bob); uint256 b = ledger.open(R, 0, _adj(4_000e18, 3_000e18, 0.05e18));\n        rRate.set(1.5e18);\n        uint256 snap = vm.snapshotState();\n        ledger.harvest(R, _ids(a));\n        vm.prank(keeper);\n        (uint256 redeemedHarvested,) = redeemer.redeem(R, 500e18, 1e18, 1, 0);\n        assertEq(redeemedHarvested, 500e18, \"harvested in front: fills from B\");\n        assertLt(ledger.debtOf(b), 3_000e18);\n        vm.revertToState(snap);\n        uint256 redeemedPlain;\n        vm.prank(keeper);\n        try redeemer.redeem(R, 500e18, 1e18, 1, 0) returns (uint256 x, uint256) { redeemedPlain = x; } catch {}\n        assertEq(redeemedPlain, redeemedHarvested, \"P3: whether anyone harvests first changes no outcome\");\n    }\n",
    "ran": "forge test --use solar --match-path test/scratch/YieldHarvest.t.sol (forge 1.9.0, Solar 0.8.38; file placed at test/scratch/ of the audit repo at ff1c192)",
    "result": "[FAIL: P3: whether anyone harvests first changes no outcome: 0 != 500000000000000000000] test_P3_harvestInFrontChangesRedemption() (gas: 1753292)"
   }
  }
 ],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "grep of src/ and script/ at ff1c192: core yieldMark written only at LedgerBase.sol:390,395 (_harvest) and :459 (_blendMark); core reserveBacked only at LedgerBase.sol:400 (+= credit) and Ledger.sol:366 (retire, -=). No script writes. The other hits are the IMD house's own Ledger (src/house/LedgerBase.sol:428,433,438,485; src/house/LedgerSettlement.sol:156 `reserveBacked += repaid`), a separate contract outside this item."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "For ERC-4626/stEthPerToken legs: testFuzz_C2_P1_donationsAndTimings (1000 runs, 12 random steps of warp 0-5 days incl. same-block steps, donations up to +20% per step, pokes, stranger harvests) asserts per harvest taken <= coll x (rate - mark)/rate, rate <= live and within the cap, shares to the Treasury, reserveBacked += exactly the debt cancelled, mark never falls, and total taken <= C0 x (1 - mark0/markFinal) + 12 wei: PASS. A donation is real value in those vaults and is capped. Exception: a leg whose source can report a value above the real rate (Base latestAnswer), see F1."
  },
  {
   "id": "C3",
   "result": "refuted",
   "evidence": "F1: on the Base leg a negative answer is read as 2^256-1 and rate() returns the ceiling (no fall, still priced via the market median); a harvest at that block takes 9.7x the real yield after 30 unpoked days (test_C3_negativeAnswerReadsAsCeiling, testFuzz_C3_baseLegWrongAnswerHarvestsPrincipal FAIL). Dip-and-return does not harvest twice: the mark holds on a dip (LedgerBase.sol:388) and _blendMark only raises it; covered by the C2 fuzz (final total equals C0 x (1 - mark0/markFinal)) and repo test AuditW1RateFeedDip (PASS)."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "Pricer.sol:66-71: low = min(median, spot) only while they agree within skew (5% in DeployConfig), else low = median. testFuzz_C4_dippedLowBounded (1000 runs, spot dip 0-5%, 1-30 days) shows the same shares are taken and creditDip >= 0.95 x creditMedian: the borrower loses at most skew x the harvest's credit versus a harvest at the median."
  },
  {
   "id": "C5",
   "result": "holds",
   "evidence": "_harvest sets the mark when debt == 0 before the priced check (LedgerBase.sol:389-391), and every borrow runs it first with debt 0 (Ledger.sol:204). testFuzz_C5_debtFreePeriodNotHarvested (1000 runs): harvest skipped unpriced, repay to zero, rate rises while debt-free, a stranger harvests in front, reborrow, later harvest takes only yield since the reborrow: PASS. Only a rate at or below the mark keeps an old mark (LedgerBase.sol:388), which favours the borrower."
  },
  {
   "id": "C6",
   "result": "refuted",
   "evidence": "test/invariant/Invariants.t.sol harvest handler (lines 259-275) only counts ok_harvest/ok_harvestCredit; the only invariant touching the harvest is invariant_supplyEquation (reserveBacked in the sum). It asserts no part of P1 (taken <= coll x (rate - mark)/rate, credit = (1 - fee) x value at low, shares to Treasury/owed) nor P2 (mark follows while debt-free, mark never falls). Its one yield type uses a MockVault RateFeed with a 1%/day cap (line 448), not the deployed 0.1%/day, and no Chainlink latestAnswer leg, so F1's sequence (wrong rate answer, stranger harvest) is outside it. Campaign counts quoted in the sheet were not re-run (NOT RUN)."
  }
 ],
 "holds": [
  {
   "property": "P1",
   "tried": "Holds for ERC-4626 and stEthPerToken legs: C2 fuzz (per-harvest yield-only bound, credit == reserveBacked rise == debt cancelled, shares to Treasury; owed path covered by repo FRHarvestOwed.t.sol PASS). Fails for the Base latestAnswer leg (F1)."
  },
  {
   "property": "P2",
   "tried": "C5 fuzz (unpriced skip, repay to zero, debt-free rise, front-run harvest, reborrow); mark never falls (C2 fuzz); deposit while the mark lags: repo AuditW1Harvest test_depositWhileUnpricedEarnsNoPastYield PASS; _blendMark only raises the mark (rate - old x (rate - mark)/(old + added) >= mark)."
  },
  {
   "property": "P3",
   "tried": "Liquidation and health view: _liqView/_afterHarvest mirror _harvest's skips (LedgerBase.sol:435-449) and liquidatePosition harvests first; repo AuditW1Harvest third-party-harvest and liquidation tests PASS. setRate harvests first (LedgerSettlement.sol:229). Redemption: refuted for the fill when a debt-paying harvest is pending (F2)."
  },
  {
   "property": "P4",
   "tried": "testFuzz_P4_reserveBackedFallsOnlyByOwnBurn (256 runs, any caller/amount): retire burns only the caller's imdUSD, reserveBacked falls by min(amount, reserveBacked), retireReserve reverts for a non-governor. Treasury.retireReserve reverts while totalBadDebt != 0 and keeps the notes' outstanding (Treasury.sol:119-125); repo AuditW1RetireKeepsNotes PASS. No other writer (C1)."
  }
 ],
 "notRun": [
  "[fork]/[data]: no fork claims in this sheet; no RPC read. NOT RUN: the Invariants.t.sol campaign counts (343-408 harvests per 8 seeds) quoted in C6; RateManager (reads Position without yieldMark) not exercised."
 ]
}