feat(cells): Phase 2 — exit-offer signaling between connected cells

Adds the ability for a cell to signal to a peer that it's willing to
route internet traffic on their behalf.  This is the signaling layer
for Phase 3 (per-peer routing via exit cell).

Changes:
- cell_links.json: exit_offered (bool) + remote_exit_offered (bool)
  fields with lazy migration (default false for existing records)
- _push_permissions_to_remote: includes exit_offered in the push body
- apply_remote_permissions: accepts exit_offered kwarg; stores it as
  remote_exit_offered on the matching cell link
- peer-sync receiver: passes exit_offered from body to apply_remote_permissions
- CellLinkManager.set_exit_offered(cell_name, offered): persists +
  triggers push so the remote learns of our offer immediately
- PUT /api/cells/<name>/exit-offer: REST endpoint to toggle the flag
- 12 new tests covering all new paths

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-01 15:49:21 -04:00
parent 7da0cbb714
commit dcee03dd3f
4 changed files with 230 additions and 3 deletions
+23 -1
View File
@@ -162,6 +162,27 @@ def update_cell_permissions(cell_name):
return jsonify({'error': str(e)}), 500
@bp.route('/api/cells/<cell_name>/exit-offer', methods=['PUT'])
def set_exit_offer(cell_name):
"""Toggle whether this cell offers to route internet for a connected cell.
Body: {"exit_offered": true|false}
The new value is persisted and pushed to the remote cell.
"""
try:
from app import cell_link_manager
data = request.get_json(silent=True) or {}
if 'exit_offered' not in data:
return jsonify({'error': 'exit_offered field required'}), 400
link = cell_link_manager.set_exit_offered(cell_name, bool(data['exit_offered']))
return jsonify({'message': f"Exit offer for '{cell_name}' updated", 'link': link})
except ValueError as e:
return jsonify({'error': str(e)}), 404
except Exception as e:
logger.error(f"Error setting exit offer: {e}")
return jsonify({'error': str(e)}), 500
@bp.route('/api/cells/peer-sync/permissions', methods=['POST'])
def peer_sync_permissions():
"""Machine-to-machine endpoint: a connected cell pushes its mirrored permission state.
@@ -197,8 +218,9 @@ def peer_sync_permissions():
if svc not in VALID_SERVICES:
return jsonify({'ok': False, 'error': f'unknown service: {svc!r}'}), 400
exit_offered = bool(data.get('exit_offered', False))
from app import cell_link_manager
cell_link_manager.apply_remote_permissions(sender_pubkey, perms)
cell_link_manager.apply_remote_permissions(sender_pubkey, perms, exit_offered=exit_offered)
return jsonify({'ok': True, 'applied_at': datetime.utcnow().isoformat()})
except ValueError as e:
return jsonify({'ok': False, 'error': str(e)}), 404