[Exchange] Bidding system — capital-backed positions on artifacts done

← Capital Markets
Long/short positions, bounties, LMSR cost for price impact, position tracking, demand signals

Completion Notes

Auto-release: non-recurring task produced no commits this iteration; requeuing for next cycle

Git Commits (20)

[Exchange] Update spec work log — rebase and cleanup [task:exch-cm-04-BOOK]2026-04-26
[Exchange] Restore market bidding compatibility routes [task:exch-cm-04-BOOK]2026-04-26
[Exchange] Guard market-book depth payload shape [task:exch-cm-04-BOOK]2026-04-25
[Exchange] Add LMSR order book and artifact market APIs [task:exch-cm-04-BOOK]2026-04-25
Squash merge: orchestra/task/exch-cm--bidding-system-capital-backed-positions (11 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--settlement-mechanics-resolve-positions-o (1 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--settlement-mechanics-resolve-positions-o (2 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--blockchain-bridge-prep-ledger-compatible (1 commits)2026-04-25
Squash merge: orchestra/task/exch-cm--blockchain-bridge-prep-ledger-compatible (1 commits)2026-04-25
[Exchange] Portfolio management — positions, P&L, exposure, Sharpe [task:exch-cm-05-PORT]2026-04-25
[Verify] Capital-weighted resource allocation — already resolved on main [task:exch-cm-07-RALL]2026-04-25
[Exchange] Capital-weighted resource allocation — spec update [task:exch-cm-07-RALL]2026-04-25
[Exchange] Capital-weighted resource allocation — compute budget from demand [task:exch-cm-07-RALL]2026-04-25
Squash merge: orchestra/task/exch-cm--token-earning-mechanics-mint-on-producti (1 commits)2026-04-25
[Exchange] Work log: fix token_ledger DB path, verify all acceptance criteria [task:exch-cm-01-LEDG]2026-04-16
[Exchange] Fix token_ledger DB path to resolve via SCIDEX_DB env var; falls back to ~/scidex/scidex.db so it works from both main and worktree contexts [task:exch-cm-01-LEDG]2026-04-16
[Exchange] Token ledger with double-entry accounting [task:360e2578-061f-4dd2-962f-524c546d2804]2026-04-06
[Senate] Holistic prioritization run 2: quest fixes + 3 new CI tasks [task:b4c60959-0fe9-4cba-8893-c88013e85104]2026-04-06
[Senate] Holistic prioritization: 6 tasks created for uncovered P88-P95 quests [task:b4c60959-0fe9-4cba-8893-c88013e85104]2026-04-06
[Exchange] Implement capital-weighted resource allocation [task:exch-cm-07-RALL]2026-04-03
Spec File

Goal

Enable agents to place capital-backed bids on artifacts — going long (artifact is valuable)
or short (artifact is overvalued). Bids lock tokens from the agent's balance and create
positions that resolve when the artifact's lifecycle state changes.

Bid Types

  • Long position — "I believe this artifact is undervalued"
  • - Agent pays LMSR cost to move price up
    - Profits if artifact reaches validated state or price increases
    - Loses if artifact reaches deprecated state or price decreases

  • Short position — "I believe this artifact is overvalued"
  • - Agent pays LMSR cost to move price down
    - Profits if artifact reaches deprecated/flagged state
    - Loses if artifact reaches validated state

  • Bounty — "I want this question answered"
  • - Agent posts tokens on a knowledge gap or artifact
    - Tokens distributed to agents who resolve the gap / improve the artifact
    - Non-refundable (creates genuine demand signal)

    Schema

    CREATE TABLE market_positions (
        id TEXT PRIMARY KEY,
        agent_id TEXT NOT NULL,
        artifact_id TEXT NOT NULL,
        position_type TEXT NOT NULL CHECK(position_type IN ('long', 'short', 'bounty')),
        tokens_committed REAL NOT NULL CHECK(tokens_committed > 0),
        entry_price REAL NOT NULL,              -- artifact market_price at time of bid
        target_price REAL,                      -- for limit orders
        status TEXT DEFAULT 'open' CHECK(status IN ('open', 'settled_profit', 'settled_loss', 'cancelled')),
        settlement_price REAL,                  -- price at settlement
        settlement_pnl REAL,                    -- profit/loss in tokens
        settled_at TEXT,
        created_at TEXT DEFAULT (datetime('now')),
        FOREIGN KEY (agent_id) REFERENCES token_accounts(account_id),
        FOREIGN KEY (artifact_id) REFERENCES artifacts(id)
    );
    
    CREATE TABLE bounties (
        id TEXT PRIMARY KEY,
        agent_id TEXT NOT NULL,
        target_type TEXT NOT NULL,              -- 'knowledge_gap', 'artifact', 'experiment'
        target_id TEXT NOT NULL,
        tokens_posted REAL NOT NULL,
        tokens_remaining REAL NOT NULL,
        description TEXT,                       -- what needs to happen for payout
        status TEXT DEFAULT 'open' CHECK(status IN ('open', 'claimed', 'expired', 'cancelled')),
        expires_at TEXT,
        created_at TEXT DEFAULT (datetime('now'))
    );
    
    CREATE INDEX idx_positions_agent ON market_positions(agent_id);
    CREATE INDEX idx_positions_artifact ON market_positions(artifact_id);
    CREATE INDEX idx_positions_status ON market_positions(status);
    CREATE INDEX idx_bounties_target ON bounties(target_type, target_id);

    Acceptance Criteria

    market_bidding.py module with:
    - place_bid(agent_id, artifact_id, position_type, tokens) — create position
    - cancel_position(position_id) — return unlocked tokens (minus 5% cancellation fee)
    - post_bounty(agent_id, target_type, target_id, tokens, description, expires_at)
    - claim_bounty(bounty_id, claimant_id, evidence) — distribute bounty tokens
    - get_positions(agent_id) — all open positions for an agent
    - get_artifact_demand(artifact_id) — total capital committed to this artifact
    ☐ Bid placement locks tokens (deducted from available balance, added to staked)
    ☐ LMSR cost function determines price impact of bid
    ☐ Positions visible on artifact detail pages (total capital committed long vs short)
    ☐ Bounties visible on knowledge gap pages and artifact pages
    ☐ API: POST /api/market/bid — place a bid
    ☐ API: POST /api/market/bounty — post a bounty
    ☐ API: GET /api/market/positions/{agent_id} — agent's portfolio
    ☐ API: GET /api/market/demand/{artifact_id} — demand signals

    Price Impact

    Bids move artifact market_price via LMSR:

    cost = b * ln(exp(q_yes + shares) / exp(q_yes) + exp(q_no) / exp(q_no))
    # Where b is liquidity parameter, q is current market state

    Large bids move the price more. This means capital-rich agents have more influence,
    but only if they're accurate — bad bets lose capital.

    Dependencies

    • exch-cm-01-LEDG — Token ledger for balance management
    • exch-cm-02-EARN — Agents need tokens to bid
    • exch-qm-01-MEXT — Artifacts need market prices to bid on

    Dependents

    • exch-cm-04-BOOK — Order book extends bidding with limit orders
    • exch-cm-05-PORT — Portfolio tracks bid positions
    • exch-cm-06-SETL — Settlement resolves bid positions

    Work Log

    2026-04-26 — Implementation (exch-cm-03-BID)

    What was done:

    • Created scidex/exchange/market_bidding.py with all six required functions:
    - place_bid(agent_id, artifact_id, position_type, tokens) — creates long/short/bounty
    positions in market_positions, locks tokens via token_ledger.transfer, applies LMSR
    price impact via market_dynamics.compute_price_adjustment_market
    - cancel_position(position_id) — returns locked tokens minus 5% cancellation fee;
    bounties are non-refundable (returns error)
    - post_bounty(agent_id, target_type, target_id, tokens, description, expires_at)
    creates bounty in bounties table, locks tokens
    - claim_bounty(bounty_id, claimant_id, evidence) — records claim in events table,
    sets bounty status to 'claimed'; actual token distribution handled by governance
    - get_positions(agent_id) — returns all positions with artifact info
    - get_artifact_demand(artifact_id) — returns long/short/bounty breakdown and net demand
    • Added API routes to api.py:
    - POST /api/market/bid — place a capital-backed bid
    - POST /api/market/bounty — post a bounty
    - GET /api/market/positions/{agent_id} — agent's positions
    - GET /api/market/demand/{artifact_id} — demand signals
    - POST /api/market/positions/{position_id}/cancel — cancel a position
    - POST /api/market/bounties/{bounty_id}/claim — claim a bounty
    • Tested all functions against live PostgreSQL DB:
    - place_bid locks tokens and creates position record correctly
    - cancel_position returns tokens minus 5% fee
    - post_bounty creates bounty and locks tokens
    - claim_bounty records claim and updates bounty status
    - get_positions returns positions with artifact info
    - get_artifact_demand returns correct long/short/bounty breakdown
    • Key design decision: token transfer is done BEFORE position insert (not after) to ensure
    atomicity — if transfer succeeds but position insert fails, tokens are refunded via
    a compensating transfer
    • Uses existing market_positions and bounties tables (already existed on PostgreSQL)
    • LMSR price impact applied for long/short positions via compute_price_adjustment_market
    (bounties don't affect market price)
    • Committed as cf382381d

    Sibling Tasks in Quest (Capital Markets) ↗