Summary
Implement the queryable API in Gradient. This is the key enabler for agentic workflows.
Background
With a working type checker (Phase 3), we can now provide structured, programmatic access to compiler internals. This moves the queryable API from Rust to Gradient.
Current State
Rust has working query API (codebase/compiler/src/query.rs - 5,400 lines). We need equivalent in Gradient.
Target State
NEW FILE: compiler/query.gr (~2,000 lines)
Provides:
Session type for holding compiled state
session_from_source() - compile from string
session_check() - get errors
session_symbols() - get all symbols
session_type_at() - type at position
session_rename() - safe refactoring
session_effects() - effect analysis
session_documentation() - doc extraction
session_call_graph() - call relationships
Implementation Requirements
Types
type Session:
source: String
tokens: TokenList
ast: AstModule
typed_module: TypedModule
type_cache: TypeCache
symbol_table: SymbolTable
Core Functions
-
session_from_source(source: String) -> Session
- Run full pipeline: lex → parse → check
- Store all intermediate results
- Return session handle
-
session_check(sess: Session) -> CheckResult
- Return all diagnostics
- Structured errors (not text)
- Include positions, severity, phase
-
session_symbols(sess: Session) -> SymbolList
- Return all top-level symbols
- Name, kind, type, effects, location
-
session_type_at(sess: Session, line: Int, col: Int) -> TypeAtResult
- Get type of expression at position
- Used for hover information
-
session_rename(sess: Session, old: String, new: String) -> RenameResult
- Find all references
- Verify new name is valid
- Return all changes needed
-
session_effects(sess: Session) -> EffectSummary
- Return effect analysis for all functions
- Purity tracking
JSON Output
All results must be JSON-serializable for tooling:
fn check_result_to_json(result: CheckResult) -> String
fn symbol_list_to_json(symbols: SymbolList) -> String
Acceptance Criteria
Why This Matters
This enables agents to develop Gradient in Gradient:
- Agents can query the compiler programmatically
- Structured data instead of text parsing
- Real-time feedback (<50ms)
- Safe, automated refactoring
Part Of
- Epic: Full Self-Hosting with Rust Kernel
- Phase: 4 of 7
- Blocks: Phase 5 (LSP in Gradient)
Effort
~5 days, ~1,500 lines of Gradient
Dependencies
Summary
Implement the queryable API in Gradient. This is the key enabler for agentic workflows.
Background
With a working type checker (Phase 3), we can now provide structured, programmatic access to compiler internals. This moves the queryable API from Rust to Gradient.
Current State
Rust has working query API (
codebase/compiler/src/query.rs- 5,400 lines). We need equivalent in Gradient.Target State
NEW FILE:
compiler/query.gr(~2,000 lines)Provides:
Sessiontype for holding compiled statesession_from_source()- compile from stringsession_check()- get errorssession_symbols()- get all symbolssession_type_at()- type at positionsession_rename()- safe refactoringsession_effects()- effect analysissession_documentation()- doc extractionsession_call_graph()- call relationshipsImplementation Requirements
Types
Core Functions
session_from_source(source: String) -> Session
session_check(sess: Session) -> CheckResult
session_symbols(sess: Session) -> SymbolList
session_type_at(sess: Session, line: Int, col: Int) -> TypeAtResult
session_rename(sess: Session, old: String, new: String) -> RenameResult
session_effects(sess: Session) -> EffectSummary
JSON Output
All results must be JSON-serializable for tooling:
Acceptance Criteria
Why This Matters
This enables agents to develop Gradient in Gradient:
Part Of
Effort
~5 days, ~1,500 lines of Gradient
Dependencies