Magic Chess — Master Audit Report
Date: 2026-08-07 | Agents: 7 (chess logic, new features, frontend, SDK, tests, docs, consolidation)
1. Chess Engine Correctness (vs FIDE)
Verdict: Needs work — 1 critical bug, 1 high issue, 3 medium
Critical
- Castling rights not revoked when rook captured on starting square (
chess_logic.rs:439-455). If rook on a1/h1/a8/h8 is captured without moving, castling right stays true. If another rook later occupies that corner, illegal castle passes. Fix: check captured piece position invalidate_and_apply_moveand revoke corresponding right.
High
- Fifty-move draw returns
MoveResult::Stalemate(chess_logic.rs:203). Semantic confusion with actual stalemate.GameEndReasonenum hasFiftyMoveRulevariant but mapping relies onhalfmove_clockcheck to disambiguate. Add dedicatedMoveResult::FiftyMoveRule.
Medium
- FNV-1a Zobrist hash not cryptographically seeded — deterministic, collision-constructable
- K+B+B (same color) vs lone King not recognized as insufficient material
- Initial position hash never recorded in
position_history— first occurrence not counted for threefold
Low
- Ring buffer evicts at 200 entries — threefold detection fails in games >100 full moves
- No 75-move auto-draw rule (FIDE Article 9.6.2)
are_no_legal_movesis O(64^4) worst case — acceptable for ER, note for L1
✅ Correct
- All 6 piece movement types, en passant (both colors, expiry, temp board simulation), castling (path clear, square attack checks, rook existence verification), check/checkmate/stalemate via simulation, promotion (4 pieces + default Queen), halfmove/fullmove clocks, FEN generation, board initialization
Production readiness: needs-work — fix critical castling bug before mainnet.
2. New Features Audit
2 critical, 5 high, 6 medium, 6 low
Critical
-
Prediction market platform fees never transferred (
claim_prediction_winnings.rs:104-114). Fee is CALCULATED (subtracted from winner_share_pool) but no token transfer sends it to a platform ATA. TheClaimPredictionWinningscontext has noplatform_fee_ataaccount. Fees permanently trapped in vault. -
Shared session key = turn hijacking (
make_move.rs:56-63). Singlesession_signerfield on ChessMatch. Either player can set it. Once set, the session key can move for whichever color hascurrent_turn. Malicious player sets session key → moves for opponent on their turn. Fix: per-player session keys or require session key to be bound to specific player.
High
- Escrow token account not closable after settlement — dust trapped.
abort_matchcloses escrow butprocess_match_settlementdoes not. - Prediction vault dust permanently trapped after all claims (integer division remainder).
delegate_match/commit_state/undelegate_matchhave NO authorization — any signer can call.claim_timeout_winschedules undelegation even when match was never delegated.- Invalid
platform_fee_wallet(zero pubkey) makes match permanently un-settleable — funds locked.
Medium
- Negative
move_timeout_durationnot validated at init. - No max session key expiry — can set to
i64::MAXfor permanent key. cancel_prediction_betpost-settlement + empty pool: vault could be drained below refund amount.PayoutEvent/DrawPayoutEventdefined but never emitted anywhere.- State machine gaps: no Active→Aborted, no draw-by-agreement.
✅ Correct
- PDA derivations (5 types), double-spend/claim prevention, payout math (fee calc correct), timeout enforcement in
make_move, session key expiry check, prediction bet cancel refunds correctly, 41 error variants comprehensive
3. Test Coverage
281 tests | ~72% coverage | 0 frontend/SDK tests | No CI
| Harness | Count |
|---|---|
| Unit (Rust) | 182 |
| LiteSVM | 43 |
| Mollusk CU | 8 |
| Anchor TS | 12 |
| MagicBlock TS (integration/crank/session) | 18 |
| Frontend | 0 |
| SDK | 0 |
Critical Gaps (must fix before mainnet)
- No frontend tests — no Jest, Vitest, or Playwright setup
- No SDK tests — client methods, PDA derivation, React hooks all untested
- No CI pipeline — no GitHub Actions for test on push/PR
- SPL token edge cases: wrong owner, wrong mint, insufficient balance, overflow
- Re-entrancy: double-initialize, double-join, resign on concluded game
High Gaps
claim_timeout_winintegration — 2 LiteSVM tests#[ignore], need MagicBlock scheduler- Draw settlement via actual stalemate — tested via account mutation, not full game
- Expired session key rejection —
#[ignore](LiteSVM clock returns 0) - Prediction pool: claim before settle, cancel while WaitingForOpponent
✅ Covered
- All piece movements, castling, en passant, promotion, check/checkmate/stalemate
- 50-move, threefold, insufficient material (unit)
- All 22 instructions have at least unit coverage; 20/22 have integration
- Payout: 15 settlement scenarios
- CU benchmarks: 8 under 200k CU
- Full ER lifecycle: init→delegate→verify→moves→commit→undelegate
- Token math edge cases: min bet, max fee, fee rounding, unequal bets
4. Frontend vs Contract Alignment
4 critical, 5 high, 5 medium, 5 low
Critical
-
Clock mismatch: Frontend uses standard chess clock (total time per player with increment via
useChessClock). On-chain uses per-move timeout (last_move_timestamp + move_timeout_duration). Fundamentally different time models. Frontend clock is misleading. -
Dummy pubkeys in match creation (
CreateMatchForm.tsx):platformFeeWallet,bettingTokenMint,playerTokenAccountall set to11111111111111111111111111111111. Transaction guaranteed to fail on-chain. -
Resign is local-only (
BoardControls.tsx): Sets localresignedstate but never callsclient.resign(). On-chain match unaffected. -
SDK
abortMatch()throws claiming "not yet implemented" — instruction exists and works on-chain (abort_match.rs, 131 lines).
High
- Hardcoded incomplete IDL in
SolanaProgramProvider.tsx—initializeMatchmissingprediction_enabledarg,makeMoveuses flat u8 instead of struct, wrong account names. - Optimistic move with no rollback — if on-chain tx fails, board stays in optimistic state.
useMatchEventsonly subscribes tomoveMadeEvent.gameEndedEvent,playerJoinedEvent, all prediction/payout events unhandled.- Session keys non-functional —
useMagicBlock.connect()sets mock sessionId, never callsset_session_key. - Timeout claim never called — clock hits 0 and does nothing.
Medium
- GameStatus component has
timeoutvariant but no code path sets it. Aborted status missing. - Prediction market buttons inert — no
placePredictionBetcall, no claim UI. - 40 on-chain errors mapped to generic "Failed to submit move" toast.
- Board state sync: chess.js FEN vs event.boardFen — no single source of truth.
- Arena: only
WaitingForOpponentfilter works. "Live" and "Completed" = mock data only.
✅ Aligned
- Match creation params, draw detection (all 4 types via chess.js), prediction pool data reads, SDK types mirror on-chain enums, FEN generation, promotion dialog, castling/en passant via chess.js
5. SDK Completeness
67% implemented | 3 critical missing | Package unpublishable | All files @ts-nocheck
Critical Missing Methods
| Method | Instruction | Status |
|---|---|---|
abortMatch | abort_match.rs | Throws "not implemented" — instruction EXISTS |
closeMatch | close_match.rs | Missing entirely |
setSessionKey | set_session_key.rs | Missing — critical for ER gasless UX |
High Missing
revokeSessionKey, React mutation hooks (useCreateMatch,useJoinMatch,useMakeMove, etc.), React prediction hooks, React delegation hook
Type Issues (8 found)
ChessMatchmissing 6 MagicBlock fields (prediction_enabled,delegation_uid,is_delegated,session_signer,session_expires_at,active_task_id)MoveResultmissingInsufficientMaterialvariant- Enum deserialization mismatch: TS string enums vs Anchor's object-based Borsh — confirmed runtime bug in
determineMoveResult PredictionPool/PredictionBetcamelCase vs snake_case mismatch- IDL type covers only 6 of 21 instructions (rest suppressed with
@ts-nocheck)
Quality
- Unpublishable: package.json exports raw
.ts, no build step, no tsconfig.json, no scripts - No error type system — throws generic
Errorfor all 40+ on-chain error variants - MagicBlock helpers: devnet-only endpoints, no mainnet
- PDA helpers duplicated inline 4 times for prediction pool
useMatchEventsregisters only 1 of 6+ event types
6. Documentation Health
6 files to delete | 7 files to update | 4 keep | 1 planning doc
Delete (speculative/unimplemented/outdated)
backend-design.md— 2701 lines, zero implementationfee-split.md— treasury vault, not startedtoken-strategy.md— CHESS token, not startedhackathon.md— past event, historical artifactfrontend-research.md— library research from planning phasefolder-structure.md— planning doc for now-implemented structurecurrent-state.md— agent handoff, extract intofrontend.md
Update (inaccurate claims)
index.md— rewrite as proper landing pagearchitecture.md— merge with spec.md, update MagicBlock status, instruction count, test countchess-engine.md— fix test counts (179→205), row indexing, feature claimsmagicblock.md— Task Scheduler disabled, custom session keys, actual delegation flowsdk.md— verify all method signatures, add MagicBlock helpersdeployment.md— update Anchor version, MagicBlock deploy notessecurity-audit.md— current bug status, accurate test counts
Keep
spec.md— merge into architecture.md or keep as is after updatesplanning/prediction-market.md— accurate planning doc for stubbed feature
7. Priority Actions
P0 (critical — fix before mainnet)
| # | Action | File | Effort |
|---|---|---|---|
| 1 | Fix castling rights not revoked on rook capture | chess_logic.rs | Small |
| 2 | Add platform_fee_ata transfer in claim_prediction_winnings | claim_prediction_winnings.rs | Small |
| 3 | Add per-player session keys (or bind key to player) | chess_match.rs, set_session_key.rs, make_move.rs | Medium |
| 4 | Fix abortMatch in SDK — implement, don't throw | sdk/src/client.ts | Small |
| 5 | Wire resign to on-chain client.resign() | BoardControls.tsx | Small |
| 6 | Fix match creation dummy pubkeys | CreateMatchForm.tsx | Small |
| 7 | Replace hardcoded IDL with generated magic_chess.json | SolanaProgramProvider.tsx | Small |
P1 (high — fix for production)
| # | Action | File | Effort |
|---|---|---|---|
| 8 | Add authorization to delegate/commit/undelegate | delegate_match.rs etc. | Small |
| 9 | Close escrow after settlement (or separate close_escrow) | process_match_settlement.rs | Medium |
| 10 | Validate platform_fee_wallet != default at init | initialize_match.rs | Small |
| 11 | Add 50-move draw MoveResult::FiftyMoveRule variant | enums.rs, chess_logic.rs, make_move.rs | Small |
| 12 | Wire prediction bet buttons to placePredictionBet | page.tsx | Medium |
| 13 | Add timeout claim call when clock hits 0 | useChessClock.ts, page.tsx | Small |
| 14 | Add rollback on failed optimistic move | page.tsx | Small |
| 15 | Wire all 6+ event types in useMatchEvents | sdk/src/react/index.tsx | Medium |
P2 (medium)
| # | Action | File | Effort |
|---|---|---|---|
| 16 | Add prediction vault clawback/sweep | claim_prediction_winnings.rs | Medium |
| 17 | Fix FNV-1a Zobrist → keyed hash | chess_logic.rs | Medium |
| 18 | Fix K+B+B (same color) insufficient material | chess_logic.rs | Small |
| 19 | Align frontend clock to per-move countdown | useChessClock.ts | Medium |
| 20 | Add session key management UI | New component | Medium |
| 21 | Build SDK — tsconfig, build scripts, regenerated IDL | sdk/ | Medium |
| 22 | Map 40 on-chain errors to user-friendly messages | Frontend | Small |
| 23 | Add GameStatus.Aborted to SDK types | sdk/src/types.ts | Small |
P3 (low — nice to have)
| # | Action | File | Effort |
|---|---|---|---|
| 24 | Add draw-by-agreement instruction | New agree_to_draw.rs | Medium |
| 25 | Add 75-move auto-draw | chess_logic.rs | Small |
| 26 | Increase position history capacity or use hashmap | chess_logic.rs | Small |
| 27 | Emit PayoutEvent/DrawPayoutEvent | process_match_settlement.rs | Small |
| 28 | Add session key change cooldown | set_session_key.rs | Small |
| 29 | Deduplicate PDA helpers in SDK | sdk/src/pda.ts | Small |
8. GitHub Issues
Docs
- Consolidate architecture.md and spec.md — 60% overlap, merge into single architecture doc
- Create frontend architecture guide — extract from
current-state.md - Fix chess-engine.md — test counts, feature claims, board indexing
- Update magicblock.md — disabled crank, custom session keys, actual delegation flow
- Delete speculative planning docs — 6 files to remove
- Update security-audit.md — current bug status, accurate counts
- Set up Docusaurus deployment — docs subdomain, Vercel, nav link
Bugs
- Castling rights not revoked on rook capture (P0)
- Prediction platform fees trapped in vault (P0)
- Shared session key enables turn hijacking (P0)
- Escrow not closable after settlement (P1)
- No authorization on delegate/undelegate (P1)
- Invalid platform_fee_wallet locks match funds (P1)
Frontend
- Clock model mismatch — chess clock vs per-move timeout (P1)
- Match creation sends dummy pubkeys (P0)
- Resign doesn't call on-chain (P0)
- Hardcoded IDL — replace with generated (P0)
- Prediction bet UI not wired (P1)
- Optimistic move no rollback (P1)
SDK
- Build pipeline — tsconfig, scripts, regenerated IDL (P2)
- Missing methods — abortMatch, closeMatch, setSessionKey (P0-P1)
- Type fixes — ChessMatch fields, enum deserialization, camelCase (P1)
- React mutation hooks (P2)
Tests
- Set up CI pipeline (P1)
- Add frontend test harness (P1)
- Add SDK integration tests (P1)
- Fuzz test escrow safety and prediction math (P2)
9. Apple Developer Grade Standards Assessment
Current Score: C+
| Dimension | Grade | Notes |
|---|---|---|
| Code correctness | B | 1 critical engine bug, 2 critical feature bugs |
| Test coverage | C | 72% on-chain, 0% frontend/SDK, no CI |
| Type safety | D | All SDK files @ts-nocheck, unpublishable |
| Documentation | C | 25+ outdated claims, 6 files should be deleted |
| Error handling | C | 40 on-chain errors → 1 generic frontend toast |
| Auth/security | C | Session key hijacking, no delegation auth |
| Contributor UX | D | No CONTRIBUTING.md, no CI, no PR template |
| Branch strategy | D | Everything on main, no PR process |
What "A" Looks Like
- Zero known critical/high bugs in engine
- CI runs all 4 test harnesses + frontend + SDK on every push
- SDK has build pipeline, generated IDL, 100% method coverage, typed errors
- Every on-chain error has user-friendly frontend message
- Docs accurate, navigable, hosted, with changelog
- Branch protection + PR template + review checklist
- Pre-commit hooks: lint, format, typecheck, test
Recommended Contribution Patterns
main ← feature/XXX branches
│
├── PR required for main
├── CI must pass (unit + litesvm + mollusk + anchor-ts + frontend + sdk)
├── PR template: what, why, how tested, breaking changes
└── Review checklist: security, auth, math, state machine, events, tests
Commit format: Conventional Commits
feat(engine): add draw-by-agreement instruction
fix(sdk): abortMatch now calls on-chain instruction
docs(chess-engine): fix test counts and board indexing
Top 3 Standards Fixes (immediate)
- Add CI: GitHub Actions running
cargo test,anchor test, frontend lint - Fix SDK types: Regenerate IDL, add tsconfig, enable type checking
- PR template + CONTRIBUTING.md: How to set up, test, submit