Multi-Agent Bitcoin Spending: Atomic Reservation Coordination
Multi-Agent Bitcoin Spending: Atomic Reservation Coordination
Research question
Use one durable reservation ledger per configured budget scope. Duplicate request IDs must return the existing reservation only when the amount matches. Concurrent agents must not independently spend the same remaining budget.
Architecture and trust boundary
Treat model output and messages from other agents as untrusted proposals. Authenticate principals outside the model, keep BTC KEY in an isolated local executor, and separate authorization from execution. A request identifier is not authorization. Preserve the wallet's original signed fee law; distinguish sender total from recipient net amount.
Source implementation
1# SPDX-License-Identifier: 0BSD
2import sqlite3
3import tempfile
4from pathlib import Path
5
6def reserve(db, request_id, amount):
7 if type(amount) is not int or amount <= 0 or not request_id:
8 raise ValueError("invalid reservation")
9 with __import__("contextlib").closing(sqlite3.connect(db, isolation_level=None)) as c:
10 c.execute("BEGIN IMMEDIATE")
11 try:
12 old = c.execute("SELECT amount FROM reservations WHERE id=?", (request_id,)).fetchone()
13 if old is not None:
14 if old[0] != amount:
15 raise ValueError("conflicting request")
16 return True
17 used = c.execute("SELECT COALESCE(SUM(amount),0) FROM reservations").fetchone()[0]
18 if amount > 1000 - used:
19 return False
20 c.execute("INSERT INTO reservations VALUES (?,?)", (request_id, amount))
21 c.commit()
22 return True
23 finally:
24 if c.in_transaction:
25 c.rollback()
26
27with tempfile.TemporaryDirectory() as d:
28 db = str(Path(d) / "budget.db")
29 with sqlite3.connect(db) as c:
30 c.execute("CREATE TABLE reservations(id TEXT PRIMARY KEY, amount INTEGER NOT NULL)")
31 assert reserve(db, "a", 700)
32 assert reserve(db, "a", 700)
33 assert not reserve(db, "b", 400)Integration with WAB ENGINE
A trusted executor can query POST /api/v5/btc/wallet/balance using X-BTC-KEY and JSON body {"net":"test","type":"segwit"}. The examples do not call this endpoint or implement SEND. A balance does not prove payment settlement. Any future write integration must persist the intent, approval, idempotency identity and execution evidence before allowing recovery or retries.
Limitations and failure handling
The example uses SQLite transactions with a fixed synthetic cap. Production needs trusted scope, exact intent binding, durable settlement and reconciliation. A reservation is not an on-chain payment.
The included assertions use synthetic data only. No live engine, model or blockchain connection was tested.
References
- https://thamer.org/api
- https://thamer.org/engine
- https://developer.bitcoin.org/devguide/transactions.html
Reference pointers only; external documentation was not reverified for this stage.
