// SPDX-License-Identifier: MIT pragma solidity 0.8.30; /// @notice Colony lifecycle ledger. Compute credits are internal units and never transferable assets. /// @dev The colony owner/operator attests observations. The contract enforces all lifecycle transitions. contract AutomatonCompute { uint16 public constant MAX_EPOCHS = 120; uint16 public constant MAX_LIVING = 16; uint16 public constant BIRTH_RESERVE = 22; uint16 public constant CHILD_CREDITS = 8; uint16 public constant BIRTH_COST = 10; uint16 public constant REWARD = 5; uint16 public constant PENALTY = 6; uint64 public constant STALE_SECONDS = 90; uint64 public constant MAX_OBSERVATION_AGE = 600; uint8 private constant NONE = 0; uint8 private constant FRESH = 1; uint8 private constant STALE = 2; struct Observation { uint64 sourceBlock; uint64 blockTimestamp; uint64 observedAt; uint16 pressureBps; bytes32 sourceHash; bytes32 evidenceHash; } struct Colony { address owner; address operator; uint64 lastSourceBlock; uint64 lastObservedAt; uint32 agents; uint32 computeReserve; uint16 epoch; uint16 living; uint8 outcome; bytes32 lastSourceHash; } struct Life { uint32 parent; uint16 born; uint16 died; uint16 reserve; uint16 wins; uint16 losses; uint16 generation; uint16 socialBps; uint16 memoryBps; int16 biasBps; int16 lastDelta; uint8 family; uint8 forecast; uint8 streak; } struct Epoch { uint32 computeReserve; uint16 living; uint16 births; uint16 deaths; uint16 pressureBps; uint8 outcome; uint64 sourceBlock; uint64 observedAt; } error MissingColony(); error OwnerOnly(); error OperatorOnly(); error EpochChanged(uint16 currentEpoch); error ColonyComplete(); error InvalidObservation(); error ObservationExpired(); error RepeatedObservation(); error PageTooLarge(); event ColonyCreated(uint256 indexed colonyId, address indexed owner); event OperatorSet(uint256 indexed colonyId, address indexed operator); event AgentBorn(uint256 indexed colonyId, uint32 indexed agentId, uint32 indexed parentId, uint16 epoch, uint16 reserve); event AgentTerminated(uint256 indexed colonyId, uint32 indexed agentId, uint16 epoch); event EpochSettled( uint256 indexed colonyId, uint16 indexed epoch, uint64 indexed sourceBlock, uint8 outcome, uint16 pressureBps, uint16 living, uint16 births, uint16 deaths, uint32 computeReserve, bytes32 sourceHash, bytes32 evidenceHash ); uint256 public colonyCount; mapping(uint256 => Colony) private _colonies; mapping(uint256 => mapping(uint32 => Life)) private _lives; mapping(uint256 => uint32[]) private _active; mapping(uint256 => mapping(uint16 => Epoch)) private _epochs; function protocolVersion() external pure returns (string memory) { return "1.0.0"; } function createColony() external returns (uint256 colonyId) { colonyId = ++colonyCount; Colony storage colony = _colonies[colonyId]; colony.owner = msg.sender; colony.agents = 3; colony.living = 3; colony.computeReserve = 45; emit ColonyCreated(colonyId, msg.sender); _founder(colonyId, 1, 0, 18, 3500, 2500, 400); _founder(colonyId, 2, 1, 15, 7600, 4200, -1500); _founder(colonyId, 3, 2, 12, 1900, 6300, 1200); _epochs[colonyId][0] = Epoch(45, 3, 0, 0, 0, NONE, 0, 0); } function setOperator(uint256 colonyId, address operator) external { Colony storage colony = _colony(colonyId); if (msg.sender != colony.owner) revert OwnerOnly(); colony.operator = operator; emit OperatorSet(colonyId, operator); } function getColony(uint256 colonyId) external view returns (Colony memory) { return _colony(colonyId); } /// @notice Pages are zero-indexed. Agent ID is offset + index + 1, including terminated lives. function getAgents(uint256 colonyId, uint32 offset, uint32 limit) external view returns (Life[] memory page) { uint32 count = _colony(colonyId).agents; if (limit > 64) revert PageTooLarge(); if (offset >= count) return new Life[](0); uint32 remaining = count - offset; uint32 length = limit < remaining ? limit : remaining; page = new Life[](length); for (uint32 index; index < length; ++index) { page[index] = _lives[colonyId][offset + index + 1]; } } function getEpoch(uint256 colonyId, uint16 epoch) external view returns (Epoch memory) { Colony storage colony = _colony(colonyId); if (epoch > colony.epoch) revert EpochChanged(colony.epoch); return _epochs[colonyId][epoch]; } function getEpochs(uint256 colonyId, uint16 start, uint16 limit) external view returns (Epoch[] memory page) { uint16 count = _colony(colonyId).epoch + 1; if (limit > 64) revert PageTooLarge(); if (start >= count) return new Epoch[](0); uint16 remaining = count - start; uint16 length = limit < remaining ? limit : remaining; page = new Epoch[](length); for (uint16 index; index < length; ++index) { page[index] = _epochs[colonyId][start + index]; } } /// @notice Accepts one operator-attested observation and advances every living agent. /// @param expectedEpoch The current epoch seen by the caller, to prevent concurrent settlements. function advance(uint256 colonyId, uint16 expectedEpoch, Observation calldata observation) external { Colony storage colony = _colony(colonyId); if (msg.sender != colony.owner && msg.sender != colony.operator) revert OperatorOnly(); if (colony.epoch != expectedEpoch) revert EpochChanged(colony.epoch); if (colony.living == 0 || colony.epoch == MAX_EPOCHS) revert ColonyComplete(); uint8 outcome = _validateObservation(colony, observation); uint16 epoch = expectedEpoch + 1; uint32[16] memory working; uint16 deaths; uint16 births; uint16 living; uint32 reserve; for (uint256 i; i < _active[colonyId].length; ++i) { uint32 id = _active[colonyId][i]; Life storage life = _lives[colonyId][id]; _score(life, outcome); if (life.reserve == 0) { life.died = epoch; life.forecast = NONE; ++deaths; emit AgentTerminated(colonyId, id, epoch); continue; } life.forecast = _predict(life, outcome, observation.pressureBps); working[living++] = id; reserve += life.reserve; } uint16 eligible = living; for (uint16 i; i < eligible && living < MAX_LIVING; ++i) { uint32 parentId = working[i]; Life storage parent = _lives[colonyId][parentId]; if (parent.reserve < BIRTH_RESERVE || parent.streak < 2 || epoch - parent.born < 3) continue; parent.reserve -= BIRTH_COST; parent.streak = 0; uint32 childId = ++colony.agents; _reproduce(colonyId, parent, parentId, childId, epoch, outcome, observation.pressureBps); working[living++] = childId; reserve -= BIRTH_COST - CHILD_CREDITS; ++births; } delete _active[colonyId]; for (uint16 i; i < living; ++i) _active[colonyId].push(working[i]); colony.epoch = epoch; colony.living = living; colony.computeReserve = reserve; colony.lastSourceBlock = observation.sourceBlock; colony.lastSourceHash = observation.sourceHash; colony.lastObservedAt = observation.observedAt; colony.outcome = outcome; _epochs[colonyId][epoch] = Epoch(reserve, living, births, deaths, observation.pressureBps, outcome, observation.sourceBlock, observation.observedAt); emit EpochSettled( colonyId, epoch, observation.sourceBlock, outcome, observation.pressureBps, living, births, deaths, reserve, observation.sourceHash, observation.evidenceHash ); } function _colony(uint256 id) private view returns (Colony storage colony) { colony = _colonies[id]; if (colony.owner == address(0)) revert MissingColony(); } function _founder(uint256 colonyId, uint32 id, uint8 family, uint16 reserve, uint16 social, uint16 memoryWeight, int16 bias) private { Life storage life = _lives[colonyId][id]; life.family = family; life.reserve = reserve; life.socialBps = social; life.memoryBps = memoryWeight; life.biasBps = bias; _active[colonyId].push(id); emit AgentBorn(colonyId, id, 0, 0, reserve); } function _validateObservation(Colony storage colony, Observation calldata input) private view returns (uint8 outcome) { if ( input.sourceBlock == 0 || input.blockTimestamp == 0 || input.blockTimestamp > input.observedAt || input.pressureBps > 10_000 || input.sourceHash == bytes32(0) || input.evidenceHash == bytes32(0) ) revert InvalidObservation(); if (input.observedAt > block.timestamp + 30 || uint256(input.observedAt) + MAX_OBSERVATION_AGE < block.timestamp) { revert ObservationExpired(); } outcome = input.observedAt - input.blockTimestamp > STALE_SECONDS ? STALE : FRESH; if (colony.epoch == 0) return outcome; if (input.sourceBlock < colony.lastSourceBlock || input.observedAt < colony.lastObservedAt) revert RepeatedObservation(); if (input.sourceBlock == colony.lastSourceBlock && ( input.sourceHash != colony.lastSourceHash || colony.outcome != FRESH || outcome != STALE || input.observedAt <= colony.lastObservedAt )) revert RepeatedObservation(); } function _score(Life storage life, uint8 outcome) private { uint16 before = life.reserve; int256 next = int256(uint256(before)) - (before <= 6 ? int256(1) : int256(2)); if (life.forecast == outcome) { next += int256(uint256(REWARD)); ++life.wins; ++life.streak; } else if (life.forecast != NONE) { next -= int256(uint256(PENALTY)); ++life.losses; life.streak = 0; } life.reserve = next > 0 ? uint16(uint256(next)) : 0; life.lastDelta = int16(int256(uint256(life.reserve)) - int256(uint256(before))); } function _predict(Life storage life, uint8 outcome, uint16 pressureBps) private view returns (uint8) { int256 bias = life.biasBps; int256 history = outcome == STALE ? int256(uint256(life.memoryBps)) : int256(0); int256 score = int256(uint256(life.socialBps) * pressureBps) + (history + bias) * 10_000; return score >= 47_000_000 ? STALE : FRESH; } function _reproduce( uint256 colonyId, Life storage parent, uint32 parentId, uint32 childId, uint16 epoch, uint8 outcome, uint16 pressureBps ) private { int256 variant = int256((uint256(childId) * 41 + uint256(epoch) * 53) % 23) - 11; Life storage child = _lives[colonyId][childId]; child.parent = parentId; child.born = epoch; child.reserve = CHILD_CREDITS; child.family = parent.family; child.generation = parent.generation + 1; child.socialBps = uint16(uint256(_boundedRound(int256(uint256(parent.socialBps)) * 60 + variant * 10_000, 60, 500, 9500))); child.memoryBps = uint16(uint256(_boundedRound(int256(uint256(parent.memoryBps)) * 600 - variant * 70_000, 600, 500, 9500))); child.biasBps = int16(_boundedRound(int256(parent.biasBps) * 1200 + variant * 110_000, 1200, -3000, 3000)); child.forecast = _predict(child, outcome, pressureBps); emit AgentBorn(colonyId, childId, parentId, epoch, CHILD_CREDITS); } function _boundedRound(int256 numerator, int256 denominator, int256 min, int256 max) private pure returns (int256) { if (numerator <= min * denominator) return min; if (numerator >= max * denominator) return max; int256 step = denominator * 100; int256 shifted = numerator + step / 2; int256 rounded = shifted / step; if (shifted < 0 && shifted % step != 0) --rounded; return rounded * 100; } }