Speculative Execution - Implementation Guide for Agents
This guide helps AI coding agents implement the speculative execution system.
Speculative Execution - Implementation Guide for Agents
This guide helps AI coding agents implement the speculative execution system.
Quick Links
DESIGN-DOCUMENT.mdAPI-SPECIFICATION.mdON-CHAIN-SPECIFICATION.mdImplementation Order
CRITICAL: Follow this order. Each phase depends on the previous.
Phase 0: On-Chain Prerequisites (START HERE)
text
#260 β #262 β #263- #260 - Add
depends_onfield to Task struct inprograms/agenc-coordination/src/state.rs - #262 - Add SDK query helpers in
packages/sdk/src/ - #263 - Add
create_dependent_taskinstruction
Phase 1: Runtime Foundation
text
#265 β #267 β #268- #265 - Create
DependencyGraphclass inpackages/runtime/src/task/ - #267 - Create
ProofPipelinefor async proof generation - #268 - Basic single-level speculation (MVP milestone)
Phase 2: Full Speculation Core
text
#270 β #272 β #274 β #276- #270 -
CommitmentLedger - #272 -
ProofDeferralManager - #274 -
RollbackController - #276 -
SpeculativeTaskScheduler(ties everything together)
Phase 3: Safety & Bounds
text
#277, #279, #280 (can be parallel)Phase 4: On-Chain State (Optional)
text
#281 β #283 β #284Phase 5: Observability & Testing
text
#286, #287, #288Key Files to Modify
On-Chain (Anchor/Rust)
text
programs/agenc-coordination/src/
βββ state.rs # Task struct (add depends_on)
βββ instructions/
β βββ mod.rs
β βββ create_dependent_task.rs # NEW
βββ events.rs # DependentTaskCreated event
βββ errors.rs # New error codesRuntime (TypeScript)
text
packages/runtime/src/task/
βββ dependency-graph.ts # NEW (#265)
βββ proof-pipeline.ts # NEW (#267)
βββ speculative-executor.ts # NEW (#268)
βββ commitment-ledger.ts # NEW (#270)
βββ proof-deferral.ts # NEW (#272)
βββ rollback-controller.ts # NEW (#274)
βββ speculative-scheduler.ts # NEW (#276)SDK (TypeScript)
text
packages/sdk/src/
βββ queries.ts # Add getTasksByDependency (#262)
βββ instructions/
βββ createDependentTask.ts # NEW (#263)Critical Invariant
NEVER submit a proof until all ancestor proofs are confirmed on-chain.
This must be enforced in:
ProofDeferralManager.trySubmit()SpeculativeTaskScheduler.shouldSpeculate()See DESIGN-DOCUMENT.md Section 5.1 for the formal correctness argument.
Testing Requirements
Each PR must include:
- Unit tests for new code (see
test-plans/unit-test-plan.md) - Integration tests if touching multiple components
- Update to existing tests if modifying shared code
Run tests:
bash
# Unit tests
yarn test
# Integration tests (requires localnet)
yarn test:integration
# Specific package
yarn workspace @agenc/runtime testCode Patterns
TypeScript Interfaces
Follow the patterns in API-SPECIFICATION.md. Example:
typescript
// Good: matches spec
export interface SpeculativeCommitment {
id: string;
sourceTaskPda: PublicKey;
resultHash: Uint8Array;
proofStatus: ProofStatus;
// ... all fields from spec
}
// Bad: deviating from spec
export interface Commitment {
taskId: string; // Wrong: should be sourceTaskPda
hash: string; // Wrong: should be Uint8Array
}Rust Accounts
Follow existing patterns in state.rs:
rust
#[account]
pub struct Task {
// ... existing fields ...
/// NEW: Optional parent task dependency
pub depends_on: Option<Pubkey>,
pub dependency_type: DependencyType,
}Events
All state changes must emit events:
rust
emit!(DependentTaskCreated {
task_id,
parent_task: ctx.accounts.parent_task.key(),
dependency_type,
creator: ctx.accounts.creator.key(),
timestamp: Clock::get()?.unix_timestamp,
});Common Mistakes to Avoid
- Don't skip phases - Phase 1 code depends on Phase 0 being complete
- Don't forget events - Every instruction needs events for indexing
- Don't ignore the invariant - Proof ordering is critical for correctness
- Don't hardcode limits - Use config for depth/stake limits
- Don't skip tests - Every PR needs tests
Reference Documents
| Need | Document |
|---|---|
| TypeScript interfaces | API-SPECIFICATION.md |
| Rust structs/instructions | ON-CHAIN-SPECIFICATION.md |
| Component relationships | diagrams/class-diagram.md |
| Execution flow | diagrams/sequence-happy-path.md |
| Rollback flow | diagrams/sequence-rollback.md |
| State transitions | diagrams/state-machine-*.md |
| Test cases | test-plans/unit-test-plan.md |
| Failure modes | RISK-ASSESSMENT.md |
Debugging Tips
- Check dependency graph - Use
graph.toJSON()to inspect state - Check commitment ledger - Use
ledger.getStats()for counts - Enable debug logging - Set
LOG_LEVEL=debug - Trace proof lifecycle - Check
proofStatustransitions
Getting Help
DESIGN-DOCUMENT.md for detailed design