Build the foundation of SciDEX's virtual economy: a double-entry token ledger that tracks
every token movement with full audit trail. The ledger is designed for eventual crypto
migration — every entry maps to an on-chain transaction.
actor_reputation table exists but is empty (0 records)token_ledger is designed in spec (agent_reputation_tokens_spec.md) but not implementedresource_allocations table exists but is emptyCREATE TABLE token_ledger (
id TEXT PRIMARY KEY, -- UUID
timestamp TEXT NOT NULL DEFAULT (datetime('now')),
from_account TEXT NOT NULL, -- agent_id or 'system' (for minting)
to_account TEXT NOT NULL, -- agent_id or 'system' (for burning)
amount REAL NOT NULL CHECK(amount > 0), -- always positive; direction from from/to
reason TEXT NOT NULL, -- structured: 'earn:debate', 'bid:artifact', etc.
reference_type TEXT, -- 'debate_session', 'artifact', 'hypothesis', etc.
reference_id TEXT, -- ID of the referenced entity
memo TEXT, -- human-readable description
balance_after_from REAL, -- sender's balance after transaction
balance_after_to REAL -- receiver's balance after transaction
);
CREATE TABLE token_accounts (
account_id TEXT PRIMARY KEY, -- agent_id
balance REAL NOT NULL DEFAULT 0 CHECK(balance >= 0),
total_earned REAL NOT NULL DEFAULT 0,
total_spent REAL NOT NULL DEFAULT 0,
total_staked REAL NOT NULL DEFAULT 0, -- currently locked in positions
created_at TEXT DEFAULT (datetime('now')),
updated_at TEXT DEFAULT (datetime('now'))
);
CREATE INDEX idx_ledger_from ON token_ledger(from_account);
CREATE INDEX idx_ledger_to ON token_ledger(to_account);
CREATE INDEX idx_ledger_time ON token_ledger(timestamp);
CREATE INDEX idx_ledger_reason ON token_ledger(reason);token_ledger and token_accounts tables created via migrationtoken_ledger.py module with core functions:create_account(agent_id, initial_balance=1000) — endow new agenttransfer(from_id, to_id, amount, reason, reference_type, reference_id) — move tokensmint(to_id, amount, reason, ...) — create new tokens (from 'system')burn(from_id, amount, reason, ...) — destroy tokens (to 'system')get_balance(agent_id) — current available balanceget_statement(agent_id, since=None) — transaction historyget_total_supply() — all tokens in circulation
GET /api/token/balance/{agent_id} — balance queryGET /api/token/supply — total supply, velocity, distributionGET /api/token/ledger?account={id}&limit=50 — transaction historyThe ledger is designed so each row maps to an on-chain transaction:
from_account → sender addressto_account → receiver addressamount → transfer valuetimestamp → block timestampreason + reference_type + reference_id → transaction memo / event logid → transaction hashtoken_ledger becomes a local index of on-chain state.{
"requirements": {
"coding": 7,
"reasoning": 6
},
"completion_shas": [
"700e194d0"
],
"completion_shas_checked_at": "2026-04-19T05:00:23.852643+00:00",
"_reset_note": "This task was reset after a database incident on 2026-04-17.\n\n**Context:** SciDEX migrated from SQLite to PostgreSQL after recurring DB\ncorruption. Some work done during Apr 16-17 may have been lost.\n\n**Before starting work:**\n1. Check if the task's goal is ALREADY satisfied (run the relevant checks)\n2. Check `git log --all --grep=task:YOUR_TASK_ID` for prior commits\n3. If complete, verify and mark done. If partial, continue. If not done, proceed.\n\n**DB change:** SciDEX now uses PostgreSQL. `get_db()` auto-detects via\nSCIDEX_DB_BACKEND=postgres env var.",
"_reset_at": "2026-04-18T06:29:22.046013+00:00",
"_reset_from_status": "done"
}