1---2name: architecture3description: Use when you need to understand the Pyre or Pysa codebase architecture, find where specific functionality is implemented, or navigate the source directory structure. Use when working on OCaml code, debugging type checking or taint analysis issues, or exploring the codebase.4---56# Pyre Architecture78## Type Checking Pipeline910- **Python CLI** (`client/`): Reads `.pyre_configuration`, determines flags, shells out to `pyre.bin`11- **Command dispatch** (`source/main.ml`): Routes to `Check`, `Analyze` (Pysa), `Infer`, `Server`, `CodeNavigation`, or `NoDaemonQuery`12- **Parsing** (`source/menhir_parser/`, `source/cpython_parser/`): Python source -> AST (`source/ast/`)13- **Environment building** (`source/analysis/`): Populates global type environment in parallel. All sources added at once (not recursively following imports). Builds a layered environment chain: `UnannotatedGlobalEnvironment` -> `ClassHierarchyEnvironment` -> `TypeAliasEnvironment` -> `AnnotatedGlobalEnvironment` -> `FunctionDefinitionEnvironment` -> `ErrorsEnvironment` -> `TypeEnvironment`14- **Type checking** (`source/analysis/typeCheck.ml`): Each function checked in parallel via control flow graph. Propagates parameter types through function body, checks compatibility at each operation. Never goes beyond function call boundaries.15- **Error reporting**: Results collected and returned to CLI1617## Pysa (Taint Analysis) Pipeline1819- **Analysis orchestrator** (`source/interprocedural_analyses/taint/taintAnalysis.ml`): Main entrypoint of the analysis, runs the different analysis steps20- **PyrePysaApi** (`source/interprocedural/pyrePysaApi.ml`): Provides an API to query a type checker (pyre or pyrefly)21- **Class Hierarchy Graph** (`source/interprocedural/classHierarchyGraph.ml`): Builds a class hierarchy graph22- **Callables Shared Memory** (`source/interprocedural/callablesSharedMemory.ml`): Builds a mapping from callables (functions and methods) to their AST and signature23- **Call Graph** (`source/interprocedural/callGraph.ml`): Defines call graph data structures24- **Call Graph Builder** (`source/interprocedural/callGraphBuilder.ml`): Builds call graphs for all callables25- **Model parsing** (`source/interprocedural_analyses/taint/modelParser.ml`): Parses `.pysa` model files26- **Forward analysis** (`source/interprocedural_analyses/taint/forwardAnalysis.ml`): Tracks taint from sources27- **Backward analysis** (`source/interprocedural_analyses/taint/backwardAnalysis.ml`): Tracks taint to sinks28- **Configuration** (`source/interprocedural_analyses/taint/taintConfiguration.ml`): Defines rules connecting sources to sinks29- **Global Fixpoint** (`source/interprocedural/fixpointAnalysis.ml`): Implements a global fixpoint over callables30- **Model** (`source/interprocedural_analyses/taint/model.ml`): Defines a summary of the taint behavior of a function, inferred during analysis3132## Key Source Directories3334- `source/ast/`: Python AST representation (Expression, Statement, Source, Location, Reference)35- `source/analysis/`: Core type checker: type representation (`type.ml`), CFG, fixpoint, preprocessing, environments, type order, class hierarchy36- `source/interprocedural/`: Interprocedural framework: call graph, dependency graph, override graph, fixpoint37- `source/interprocedural_analyses/taint/`: Pysa taint analysis: sources, sinks, domains, models, rules, reporting38- `source/interprocedural_analyses/type_inference/`: Interprocedural type inference39- `source/server/`: Pyre daemon for incremental analysis40- `source/code_navigation_server/`: IDE code navigation server41- `source/command/`: OCaml CLI command implementations42- `source/buck_command/`: Buck-specific commands43- `source/buck_integration/`: Buck build system integration44- `source/service/`: Shared memory management, scheduling45- `source/domains/`: Abstract domain library (lattices for interprocedural analyses)46- `source/hack_parallel/`: Shared memory and multi-worker infrastructure (forked from Hack)47- `source/saved_state/`: Serialization for incremental analysis48- `client/`: Python CLI (`pyre` command)49- `client/commands/`: All user-facing commands (check, analyze, start, stop, infer, query, etc.)50- `client/configuration/`: `.pyre_configuration` file parsing51- `client/language_server/`: LSP protocol implementation52- `api/`: Programmatic Python API for Pyre server53- `pyre_extensions/`: Runtime Python helpers (`none_throws`, `safe_cast`, etc.)54- `tools/generate_taint_models/`: Auto-generation of Pysa taint models55- `tools/typeshed_patcher/`: Applies patches to bundled typeshed stubs56- `stubs/`: Bundled typeshed with patching mechanism57- `facebook/`: Meta-internal code (not open-sourced)5859## OCaml Conventions6061- Most types derive `show`, `eq`, `compare`, `to_yojson` via PPX derivers. For a type `Module.t`, `Module.show` gives a debug string.62- Debug with `Log.dump "format %s" value` and run with `pyre --noninteractive check`63- In Python test files, use `pyre_dump()`, `pyre_dump_cfg()`, `pyre_dump_locations()`, and `reveal_type(x)` to inspect Pyre's state