Skip to content

How Zhi works

Three trust boundaries separate the parts: the LLM can only propose a rule tree, the harness can only propose an intent, and only the Guardrail Vault can place an order. Each layer distrusts the one above it.

1. Compile once, sign the hash

The compiler sends your text to Claude as quoted data with a JSON-schema-constrained output. Deterministic code then decides what is accepted: values not grounded in the text become operator defaults (yellow), values above your presets are clamped (red), unknown keys fail. The canonical JSON (sorted keys, minimal numbers) is hashed with keccak256; you sign that hash with EIP-712 and the vault stores it. TypeScript, Rust and an independent Python implementation produce the same hash for the shared fixture.

2. The deterministic harness

A Rust service (zhi run) samples the Injective oracle precompile, builds candles locally, evaluates the frozen tree in 18-decimal fixed point, asks Synth before entries, and submits intents. Every tick is logged with its inputs and the raw Synth body; zhi replay re-runs the log and must reproduce every intent hash bit-for-bit.

# hash + validate a rule tree
zhi hash ruletree.json
zhi validate ruletree.json --vault 0xVAULT

# run the harness (testnet)
HARNESS_PRIVATE_KEY=0x… SYNTH_API_KEY=… zhi run --tree ruletree.json --vault 0xVAULT

# prove determinism
zhi replay logs/0xvault.jsonl

# with vs without the Synth gate, on Injective candle history
zhi backtest --tree ruletree.json --days 30

# daily evidence -> Zhi Score -> ERC-8004 feedback
zhi attest --vault 0xVAULT --attestor 0xATTESTOR --post

# anyone: enforce the drawdown freeze
zhi poke --vault 0xVAULT

3. Synth SN50 gate

On the last time step of the horizon the harness interpolates the forecast CDF F at the current oracle price P₀. A long needs P(up) = 1 − F(P₀) ≥ the tree's threshold (shorts mirror it), and the 5–95% band (p95 − p05) / P₀ × 10⁴ must be under the tree's ceiling. Failing either check, or Synth being unreachable, skips the trade and records TradeSkipped(SYNTH_*) on-chain with keccak256 of the raw response. The gate is a filter that reduces bad entries; the backtester reports the same tree with and without it.

4. Guardrail Vault checks

Each failure emits TradeSkipped(reason) and returns false. Nothing reverts, so the evidence and the consumed nonce persist.

CodeReasonMeaning
0FROZENFrozen (or credit reduce-only) vaults accept reduce-only exits only
1BAD_RULETREEIntent cites a rule tree hash the owner never signed (or revoked)
2EXPIREDblock.timestamp > intent.deadline
3MARKET_NOT_ALLOWEDMarket not on the vault allowlist
4COOLDOWNToo soon after the previous entry
5NOTIONALquantity × worstPrice > maxNotional
6LEVERAGEmargin = 0 or notional ÷ margin > maxLeverage
7ORACLE_STALEInjective oracle reference missing or older than maxOracleAgeSec (entries only)
8PRICE_DEVIATIONworstPrice further than maxDeviationBps from the oracle reference
9DRAWDOWNThis intent found the daily drawdown breached and froze the vault
10OFF_TICKQuantity or price not on the market's tick sizes
11EQUITY_UNREADABLEFail closed: equity could not be read for an entry
12ORDER_FAILEDExchange precompile rejected the order (no revert, evidence kept)
13–15SYNTH_*Off-chain gate verdict recorded on-chain with the forecast hash

Contracts (Injective EVM testnet, chain 1439)

PRD open questions closed during the build

  • Oracle precompile address

    0x…0067, interface IOracleModule (InjectiveLabs/solidity-contracts src/Oracle.sol). Verified live: oraclePricePairState(13, BTC feed, USDC feed) returns a 2-second-old price on testnet.

  • ERC-8004 registries on Injective

    Identity 0x8004A818BFB912233c491871b3d84c89A494BD9e, Reputation 0x8004B663056A597Dffe9eCcC1965A193B7388713 on testnet; reference giveFeedback signature and self-feedback rule confirmed in source and exercised in a fork test.

  • ExchangeTypes fields

    DerivativeOrder {marketID, subaccountID, feeRecipient, price, quantity, cid, orderType, margin, triggerPrice}; reduce-only = margin 0.

  • Synth API

    GET /insights/prediction-percentiles?asset&horizon(&start_time) with Authorization: Apikey; 9 levels 0.005–0.995 per step. Equities use SP500/NVDA/TSLA/AAPL/GOOGL symbols.