{
 "lead": "midnight-assumptions: the adapter's assumptions about Midnight (continuous/settlement fees and fee changes, pendingFee vs stored net credit, permissionless updatePosition, repayment, liquidation/lossFactor, liquidation lock, withdrawable liquidity, toMarket/ids, credit changes outside onBuy/onSell)",
 "commit": "bbeda7b27ae80abcf070e0158cb8a31e87bb2286 (lib/midnight submodule 4e2570e51171d09228645b2d5bdf84ef24b1720b)",
 "hasCandidates": false,
 "candidates": [],
 "summary": "No candidate survives. Every way an untrusted account can change the adapter's Midnight position outside onBuy/onSell was either shown to have no effect on accounting, or is already handled by realAssets reading live credit when lossFactor != 0, or is already in the 2026-10-12 drafts or Midnight's own NatSpec. The PoC is not attached to a finding, but the full test file and its real output are in the 'poc' field below.",
 "poc": {
  "path": "test/poc/MidnightAssumptionsPoC.t.sol",
  "source": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.0;\n\nimport \"../integration/MidnightAdapterIntegrationTest.sol\";\nimport {console} from \"forge-std/console.sol\";\n\n/// @dev Probes the adapter's assumptions about Midnight state that can change without onBuy/onSell.\ncontract MidnightAssumptionsPoC is MidnightAdapterIntegrationTest {\n    address internal attacker = makeAddr(\"attacker\");\n\n    function _fees(uint256 continuousFee, uint256 settlementFee7d) internal {\n        vm.prank(owner);\n        midnight.setFeeSetter(owner);\n        vm.startPrank(owner);\n        midnight.setDefaultContinuousFee(address(underlyingToken), continuousFee);\n        if (settlementFee7d > 0) {\n            midnight.setDefaultSettlementFee(address(underlyingToken), 1, 0.000014e18);\n            midnight.setDefaultSettlementFee(address(underlyingToken), 2, settlementFee7d);\n        }\n        vm.stopPrank();\n    }\n\n    function _net(bytes32 id) internal view returns (uint256) {\n        (uint256 c, uint256 p,) = midnight.updatePositionView(_market(0), id, address(adapter));\n        return c - p;\n    }\n\n    function _stored(bytes32 id) internal view returns (uint256 n) {\n        n = adapter.marketData(id).netCredit;\n    }\n\n    /// Lead: permissionless updatePosition and continuous fee changes do not move net credit or realAssets.\n    function testPermissionlessUpdatePositionAndFeeChange() public {\n        _fees(uint256(0.01e18) / 365 days, 0);\n        Market memory market = _market(7 days);\n        bytes32 id = IdLib.toId(market);\n        testMakerBuy();\n        vm.warp(block.timestamp + 3 days);\n        uint256 ra0 = adapter.realAssets();\n        uint256 cur0 = _net(id);\n        vm.prank(attacker);\n        midnight.updatePosition(market, address(adapter));\n        vm.prank(owner);\n        midnight.setMarketContinuousFee(id, 0);\n        console.log(\"stored net credit       \", _stored(id));\n        console.log(\"current net before/after\", cur0, _net(id));\n        console.log(\"realAssets before/after \", ra0, adapter.realAssets());\n        assertEq(cur0, _net(id));\n        assertEq(ra0, adapter.realAssets());\n        assertEq(_stored(id), _net(id));\n    }\n\n    /// Lead: repaid liquidity withdrawn before maturity carries its pending fee away (Midnight forgives it).\n    function testEarlyWithdrawForgivesPendingFee() public {\n        _fees(uint256(0.01e18) / 365 days, 0);\n        Market memory market = _market(7 days);\n        bytes32 id = IdLib.toId(market);\n        testMakerBuy();\n        uint256 repayUnits = 50_000 * oneToken;\n        deal(address(underlyingToken), borrower, repayUnits);\n        vm.startPrank(borrower);\n        SafeERC20Lib.safeApprove(address(underlyingToken), address(midnight), repayUnits);\n        midnight.repay(market, repayUnits, borrower, address(0), \"\");\n        vm.stopPrank();\n        uint256 ta0 = adapter.realAssets() + underlyingToken.balanceOf(address(vault));\n        uint256 n0 = _stored(id);\n        vm.prank(allocator);\n        vault.deallocate(address(adapter), abi.encode(market), repayUnits);\n        uint256 ta1 = adapter.realAssets() + underlyingToken.balanceOf(address(vault));\n        console.log(\"withdrawn assets        \", repayUnits);\n        console.log(\"net credit decrease     \", n0 - _stored(id));\n        console.log(\"realAssets+idle bef/aft \", ta0, ta1);\n        assertGt(ta1, ta0);\n    }\n\n    /// Lead: bad debt via permissionless liquidation; realAssets follows Midnight, stored net credit and allocations do not, until a sync.\n    function testBadDebtStoredVsCurrentAndShareholderSync() public {\n        Market memory market = _market(7 days);\n        bytes32 id = IdLib.toId(market);\n        testMakerBuy();\n        uint256 ra0 = adapter.realAssets();\n        uint256 alloc0 = vault.allocation(keccak256(abi.encode(\"this\", address(adapter))));\n        oracle.setPrice(ORACLE_PRICE_SCALE * oneToken / 1e18 / 4);\n        vm.prank(attacker);\n        midnight.liquidate(market, 0, 0, 0, borrower, false, attacker, address(0), \"\");\n        console.log(\"lossFactor              \", midnight.lossFactor(id));\n        console.log(\"realAssets before/after \", ra0, adapter.realAssets());\n        console.log(\"stored / current net    \", _stored(id), _net(id));\n        console.log(\"adapter allocation      \", alloc0, vault.allocation(keccak256(abi.encode(\"this\", address(adapter)))));\n        // any shareholder can sync with a 0-asset forceDeallocate\n        vm.startPrank(depositor);\n        midnight.setIsAuthorized(address(adapter), true, depositor);\n        vault.forceDeallocate(address(adapter), abi.encode(market, depositor), 0, depositor);\n        vm.stopPrank();\n        console.log(\"allocation after 0-sync \", vault.allocation(keccak256(abi.encode(\"this\", address(adapter)))));\n        assertEq(_stored(id), _net(id));\n    }\n\n    /// Lead: settlement fee rounding in forceDeallocate (adapter's quote vs Midnight's charge).\n    function testForceDeallocateSettlementFeeRounding() public {\n        _fees(0, 0.000098e18);\n        Market memory market = _market(7 days);\n        testMakerBuy();\n        vm.warp(block.timestamp + 2 days);\n        deal(address(underlyingToken), depositor, 10_000 * oneToken);\n        vm.startPrank(depositor);\n        SafeERC20Lib.safeApprove(address(underlyingToken), address(adapter), type(uint256).max);\n        midnight.setIsAuthorized(address(adapter), true, depositor);\n        uint256 reverts;\n        for (uint256 a = 1; a <= 200; a++) {\n            uint256 snap = vm.snapshotState();\n            (bool ok,) = address(vault).call(\n                abi.encodeCall(IVaultV2.forceDeallocate, (address(adapter), abi.encode(market, depositor), a * 7919, depositor))\n            );\n            if (!ok) reverts++;\n            vm.revertToState(snap);\n        }\n        uint256 bal0 = underlyingToken.balanceOf(address(adapter));\n        vault.forceDeallocate(address(adapter), abi.encode(market, depositor), 1_234_567, depositor);\n        vm.stopPrank();\n        console.log(\"reverting amounts of 200\", reverts);\n        console.log(\"adapter dust after one  \", underlyingToken.balanceOf(address(adapter)) - bal0);\n        assertEq(reverts, 0);\n    }\n\n    /// Lead: a full bad-debt wipe maxes lossFactor; Midnight.take then reverts, so forceDeallocate (in-kind redemption) and its 0-asset sync revert, while an allocator withdraw(0) still syncs.\n    function testFullWipeBlocksForceDeallocateSync() public {\n        Market memory market = _market(7 days);\n        bytes32 id = IdLib.toId(market);\n        testMakerBuy();\n        oracle.setPrice(0);\n        vm.prank(attacker);\n        midnight.liquidate(market, 0, 0, 0, borrower, false, attacker, address(0), \"\");\n        bytes32 aid = keccak256(abi.encode(\"this\", address(adapter)));\n        console.log(\"realAssets after wipe   \", adapter.realAssets());\n        console.log(\"allocation after wipe   \", vault.allocation(aid));\n        vm.startPrank(depositor);\n        midnight.setIsAuthorized(address(adapter), true, depositor);\n        vm.expectRevert(IMidnight.MarketLossFactorMaxedOut.selector);\n        vault.forceDeallocate(address(adapter), abi.encode(market, depositor), 0, depositor);\n        vm.stopPrank();\n        vm.prank(allocator);\n        vault.deallocate(address(adapter), abi.encode(market), 0);\n        console.log(\"allocation after alloc 0\", vault.allocation(aid));\n        console.log(\"marketIdsLength         \", adapter.marketIdsLength());\n        assertEq(vault.allocation(aid), 0);\n        assertEq(_stored(id), 0);\n    }\n}\n",
  "command": "FOUNDRY_TEST=test/poc forge test --match-path test/poc/MidnightAssumptionsPoC.t.sol -vv  (after: forge build src lib/midnight/src/Midnight.sol; cp test/imports/MidnightImport.sol test/poc/MidnightImportCopy.sol; FOUNDRY_TEST=test/poc forge build test/poc/MidnightImportCopy.sol. FOUNDRY_TEST only narrows compilation, because a full test compile was SIGKILLed for memory on this machine)",
  "output": "Compiler run successful!\n\nRan 13 tests for test/poc/MidnightAssumptionsPoC.t.sol:MidnightAssumptionsPoC\n[PASS] testBadDebtStoredVsCurrentAndShareholderSync() (gas: 1779377)\nLogs:\n  lossFactor               170141183460469231731685612341188336919\n  realAssets before/after  100000000000030600145311 50000000000015300072655\n  stored / current net     100593430826474621736007 50296715413237310868003\n  adapter allocation       100593430826474621736007 100593430826474621736007\n  allocation after 0-sync  50296715413237310868003\n\n[PASS] testEarlyWithdrawForgivesPendingFee() (gas: 1847339)\nLogs:\n  withdrawn assets         50000000000000000000000\n  net credit decrease      49990410958929440000000\n  realAssets+idle bef/aft  100000000000042545435847 100294965000021398212068\n\n[PASS] testForceDeallocate() (gas: 1750424)\n[PASS] testForceDeallocateSettlementFeeRounding() (gas: 70554097)\nLogs:\n  reverting amounts of 200 0\n  adapter dust after one   0\n\n[PASS] testForceSell() (gas: 1652382)\n[PASS] testFullWipeBlocksForceDeallocateSync() (gas: 1641378)\nLogs:\n  realAssets after wipe    0\n  allocation after wipe    100593430826474621736007\n  allocation after alloc 0 0\n  marketIdsLength          0\n\n[PASS] testMakerBuy() (gas: 1129769)\n[PASS] testMakerSell() (gas: 1920305)\n[PASS] testPermissionlessUpdatePositionAndFeeChange() (gas: 1552236)\nLogs:\n  stored net credit        100574138935682149705759\n  current net before/after 100574138935682149705759 100574138935682149705759\n  realAssets before/after  100245254913061114095371 100245254913061114095371\n\n[PASS] testRepayWithdrawRedeem() (gas: 1779943)\n[PASS] testTakerBuy() (gas: 1166469)\n[PASS] testTakerSell() (gas: 1958000)\n[PASS] testWrapperGateForceDeallocateAfterRevocation() (gas: 17206533)\nSuite result: ok. 13 passed; 0 failed; 0 skipped; finished in 185.39ms (177.62ms CPU time)\n\nRan 1 test suite in 187.87ms (185.39ms CPU time): 13 tests passed, 0 failed, 0 skipped (13 total tests)"
 },
 "lowInfo": [
  {
   "title": "A full bad-debt wipe maxes Midnight's lossFactor, after which forceDeallocate (in-kind redemption) in that market always reverts and shareholders cannot resync the stale vault allocation; only an allocator or sentinel withdraw(0) can",
   "class": "Informational",
   "verified": true,
   "refs": [
    "src/adapters/midnight/MidnightAdapter.sol:469",
    "src/adapters/midnight/MidnightAdapter.sol:471-477",
    "src/adapters/midnight/MidnightAdapter.sol:717-737",
    "lib/midnight/src/Midnight.sol:332",
    "lib/midnight/src/Midnight.sol:101"
   ],
   "detail": "Test testFullWipeBlocksForceDeallocateSync: once the borrower's collateral price is 0, a permissionless liquidate gives lossFactor = type(uint128).max. realAssets correctly drops to 0, but the adapter's allocation in the vault stays at 100593430826474621736007 (the stored net credit). vault.forceDeallocate(adapter, (market, depositor), 0, depositor) reverts with MarketLossFactorMaxedOut, because Midnight.take requires lossFactor < max (Midnight.sol:332). So a shareholder cannot use a 0-asset forceDeallocate to clean up the stale cap usage, as they can after a partial loss (testBadDebtStoredVsCurrentAndShareholderSync: allocation 100593430826474621736007 -> 50296715413237310868003). An allocator's vault.deallocate(adapter, abi.encode(market), 0) still syncs it: allocation goes to 0 and marketIdsLength to 0. There is no value at stake (net credit is 0) and Midnight documents that take reverts at a maxed loss factor, so this is Informational: the adapter's 'Can be called with withdrawnAssets == 0 to synchronize marketData after a loss' path is the only cleanup path, and it is permissioned. Stale stored net credit after bad debt in general is already Cantina draft 3.2.5."
  },
  {
   "title": "Early withdrawal of repaid liquidity forgives the withdrawn credit's pending continuous fee and realizes the remaining discount immediately",
   "class": "Informational",
   "verified": true,
   "refs": [
    "src/adapters/midnight/MidnightAdapter.sol:717-737",
    "lib/midnight/src/Midnight.sol:481-486"
   ],
   "detail": "Test testEarlyWithdrawForgivesPendingFee (continuous fee 1%/yr, 7-day market, 50000e18 repaid by the borrower before maturity, then withdrawn by an allocator): stored net credit falls by only 49990410958929440000000 for 50000000000000000000000 assets received, because Midnight removes pendingFee pro rata on withdraw, which forgives about 9.59e18 of fee. adapter.realAssets() plus vault idle goes from 100000000000042545435847 to 100294965000021398212068 (+294.96e18) in one step. Already reported as Cantina draft 3.6.5 ('Depositors can time entry ahead of early Midnight withdrawals'). The vault's maxRate caps the effect. The forgiven pending fee is Midnight's own design. No new finding."
  }
 ],
 "negativeResults": [
  {
   "check": "Permissionless Midnight.updatePosition on the adapter, plus a later setMarketContinuousFee change",
   "result": "No effect. Stored net credit, live net credit (credit - pendingFee) and realAssets are all unchanged (100574138935682149705759 / 100574138935682149705759 / 100245254913061114095371 before and after). currentNetCredit uses a dummy market with maturity 0, so the fee accrual term is 0. This is harmless, because accrual lowers credit and pendingFee by the same amount (Midnight.sol:808-814).",
   "test": "testPermissionlessUpdatePositionAndFeeChange"
  },
  {
   "check": "forceDeallocate settlement-fee quote (ceil(assets*fee)) vs Midnight's charge (ceil(u*(p+f)) - ceil(u*p)) at a tick-0 price",
   "result": "0 reverts over 200 amounts (k*7919, k=1..200) at a 0.0098% 7-day fee, 2 days into the market. A 1234567-wei redemption left 0 dust in the adapter.",
   "test": "testForceDeallocateSettlementFeeRounding"
  },
  {
   "check": "Adapter credit changing without onBuy/onSell (transfer, a take on its behalf, an authorization)",
   "result": "By code review: Midnight has no credit transfer. take/withdraw/repay/setConsumed on behalf of the adapter need isAuthorized[adapter][x], and the adapter only authorizes itself (constructor). Buys into the adapter need ratifier = adapter, whose isRatified requires callback == adapter, so onBuy always runs. The inForceDeallocate ratifier bypass is open only between isRatified and onBuy, and the only external calls in that window are view (IEnterGate.canIncreaseCredit/canIncreaseDebt are view in lib/midnight/src/interfaces/IGate.sol:6-7, plus oracle prices). So it cannot be reentered to take another adapter offer. The only out-of-callback changes are bad-debt slashing (handled by realAssets reading live credit when lossFactor != 0, MidnightAdapter.sol:848-855) and continuous fee accrual (net-neutral). Code review only, not a test."
  }
 ],
 "priorAuditCheck": "Read both 2026-10-12 drafts in audits/. Cantina: 3.2.5 covers stale stored net credit after bad debt, 3.6.5 covers timing around early withdrawals. TrustSec: L-4 covers fee changes as slippage on take, R-3 covers forceDeallocate face-value payment. Neither covers the lossFactor-maxed revert of forceDeallocate, which is reported here only as Informational.",
 "covered": {
  "testsRun": "test/poc/MidnightAssumptionsPoC.t.sol, which inherits test/integration/MidnightAdapterIntegrationTest.sol: 13 tests, 13 passed, 0 failed. That includes the 8 original integration tests (testMakerBuy, testRepayWithdrawRedeem, testTakerBuy, testTakerSell, testMakerSell, testForceSell, testForceDeallocate, testWrapperGateForceDeallocateAfterRevocation) and 5 new tests.",
  "areas": [
   "continuous fee and pendingFee vs stored net credit",
   "continuous fee change after a buy",
   "permissionless updatePosition",
   "early repayment and withdrawable liquidity",
   "liquidation bad debt with a partial and a total lossFactor",
   "liquidation lock and the ratifier bypass window during forceDeallocate (code review)",
   "settlement fee rounding in forceDeallocate",
   "credit changes outside the callbacks (code review)"
  ]
 },
 "notCovered": [
  "The full repository test suite was not run: compiling every test was SIGKILLed for memory on this machine, so only the Midnight integration tests, which the PoC inherits, were run",
  "No fuzz or invariant campaign; the settlement-fee check uses a fixed set of 200 amounts",
  "Settlement-fee and continuous-fee changes landing between an adapter maker offer's ratification and its take in a later block (TrustSec L-4 territory)",
  "Multi-market shortfall interaction with lossFactor != 0 markets in onSell",
  "Gated markets (IKRCompatibleEnterGate) combined with bad debt"
 ]
}