# aditya-siraskar/stance architecture

> Architecture diagram of the GitHub repository aditya-siraskar/stance, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aditya-siraskar/stance
- Repository: https://github.com/aditya-siraskar/stance

## Overview

STANCE is a local SEC-filings research copilot. Its main workflow takes an analyst’s question through entity/period scoping, plan generation and routing, then retrieves filing text for narrative answers or resolves XBRL facts for deterministic numeric answers; verification checks cited figures before the UI presents the result. A separate ingestion capability builds the filing corpus and structured fact store. The graph and README document the major stages; some ingestion and retrieval internals were not excerpted, so those links are cited to the README rather than implying inspected calls.

Main entry points: [`stance/agents/graph.py`](https://github.com/aditya-siraskar/stance/blob/main/stance/agents/graph.py) (LangGraph workflow), [`stance/numeric/resolver.py`](https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/resolver.py) (Fact resolution), [`stance/contracts.py`](https://github.com/aditya-siraskar/stance/blob/main/stance/contracts.py) (Typed contracts), [`stance/numeric/interpret.py`](https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/interpret.py) (Numeric interpretation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Corpus ingestion"]
  node_acquire["Filing acquisition<br/>[acquire.py]"]
  node_parse["Filing parsing<br/>[parse.py]"]
  node_segment["Section segmentation<br/>[segment.py]"]
  node_chunk["Parent-child chunks<br/>[chunk.py]"]
  node_embed["Text embeddings<br/>[embed.py]"]
  node_facts_ingest["XBRL fact extraction<br/>[facts.py]"]
  node_database[("Postgres + pgvector<br/>[db.py]")]
end

subgraph group_agents["Question workflow"]
  node_graph["LangGraph workflow<br/>[graph.py]"]
  node_scope["Scope resolution<br/>[scope.py]"]
  node_planner["Question planner<br/>[planner.py]"]
  node_router["Deterministic router<br/>[router.py]"]
  node_contracts["Typed contracts<br/>[contracts.py]"]
  node_verifier["Citation verifier<br/>[verifier.py]"]
  node_llm["Model routing<br/>[llm.py]"]
end

subgraph group_retrieval["Narrative retrieval"]
  node_hybrid["Hybrid search<br/>[hybrid.py]"]
  node_rerank["Result reranking<br/>[rerank.py]"]
  node_expand["Section expansion<br/>[expand.py]"]
  node_writer["Narrative drafting<br/>[writer.py]"]
end

subgraph group_numeric["Numeric verification"]
  node_interpret["Numeric interpretation<br/>[interpret.py]"]
  node_resolver["Fact resolution<br/>[resolver.py]"]
  node_calculator["Computation specs<br/>[calculator.py]"]
  node_executor["Spec execution<br/>[executor.py]"]
  node_sandbox["Arithmetic sandbox<br/>[sandbox.py]"]
end

subgraph group_experience["User experience"]
  node_ui["Streamlit UI<br/>[app.py]"]
  node_pipeline_viz["Pipeline display<br/>[pipeline_viz.py]"]
  node_evaluation["Evaluation driver<br/>[run_eval.py]"]
end

node_analyst(("Analyst"))
node_edgar["SEC EDGAR"]

node_analyst -->|"asks question"| node_ui
node_ui -->|"invokes / streams"| node_graph
node_graph -->|"resolves scope"| node_scope
node_graph -->|"builds plan"| node_planner
node_graph -->|"routes plan"| node_router
node_graph -->|"searches text"| node_hybrid
node_graph -->|"reranks candidates"| node_rerank
node_graph -->|"expands results"| node_expand
node_graph -->|"drafts narrative"| node_writer
node_graph -->|"answers numeric steps"| node_interpret
node_graph -->|"verifies draft"| node_verifier
node_graph -->|"uses typed state"| node_contracts
node_planner -->|"requests completion"| node_llm
node_writer -->|"requests completion"| node_llm
node_interpret -->|"resolves facts"| node_resolver
node_interpret -->|"builds computation"| node_calculator
node_interpret -->|"executes spec"| node_executor
node_calculator -->|"emits spec"| node_contracts
node_executor -->|"loads operands"| node_resolver
node_executor -->|"runs operation"| node_sandbox
node_verifier -->|"rechecks citations"| node_resolver
node_scope -->|"produces scope"| node_contracts
node_planner -->|"produces plan"| node_contracts
node_router -->|"reads plan"| node_contracts
node_ui -->|"renders trace"| node_pipeline_viz
node_ui -->|"runs evaluation"| node_evaluation
node_evaluation -->|"evaluates questions"| node_graph
node_evaluation -->|"runs injection check"| node_verifier
node_evaluation -->|"looks up test fact"| node_resolver
node_edgar -->|"supplies filings"| node_acquire
node_acquire -->|"ingestion sequence"| node_parse
node_parse -->|"ingestion sequence"| node_segment
node_segment -->|"ingestion sequence"| node_chunk
node_chunk -->|"ingestion sequence"| node_embed
node_embed -->|"stores vectors"| node_database
node_acquire -->|"extracts XBRL facts"| node_facts_ingest
node_facts_ingest -->|"stores facts"| node_database
node_hybrid -->|"searches chunks"| node_database
node_resolver -->|"queries facts"| node_database

click node_ui "https://github.com/aditya-siraskar/stance/blob/main/apps/ui/app.py"
click node_pipeline_viz "https://github.com/aditya-siraskar/stance/blob/main/apps/ui/pipeline_viz.py"
click node_acquire "https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/acquire.py"
click node_parse "https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/parse.py"
click node_segment "https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/segment.py"
click node_chunk "https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/chunk.py"
click node_embed "https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/embed.py"
click node_facts_ingest "https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/facts.py"
click node_database "https://github.com/aditya-siraskar/stance/blob/main/stance/db.py"
click node_graph "https://github.com/aditya-siraskar/stance/blob/main/stance/agents/graph.py"
click node_scope "https://github.com/aditya-siraskar/stance/blob/main/stance/agents/scope.py"
click node_planner "https://github.com/aditya-siraskar/stance/blob/main/stance/agents/planner.py"
click node_router "https://github.com/aditya-siraskar/stance/blob/main/stance/agents/router.py"
click node_contracts "https://github.com/aditya-siraskar/stance/blob/main/stance/contracts.py"
click node_hybrid "https://github.com/aditya-siraskar/stance/blob/main/stance/retrieval/hybrid.py"
click node_rerank "https://github.com/aditya-siraskar/stance/blob/main/stance/retrieval/rerank.py"
click node_expand "https://github.com/aditya-siraskar/stance/blob/main/stance/retrieval/expand.py"
click node_writer "https://github.com/aditya-siraskar/stance/blob/main/stance/agents/writer.py"
click node_interpret "https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/interpret.py"
click node_resolver "https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/resolver.py"
click node_calculator "https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/calculator.py"
click node_executor "https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/executor.py"
click node_sandbox "https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/sandbox.py"
click node_verifier "https://github.com/aditya-siraskar/stance/blob/main/stance/agents/verifier.py"
click node_llm "https://github.com/aditya-siraskar/stance/blob/main/stance/llm.py"
click node_evaluation "https://github.com/aditya-siraskar/stance/blob/main/eval/run_eval.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_acquire,node_parse,node_segment,node_chunk,node_embed,node_facts_ingest,node_database toneBlue
class node_graph,node_scope,node_planner,node_router,node_contracts,node_verifier,node_llm toneAmber
class node_hybrid,node_rerank,node_expand,node_writer toneMint
class node_interpret,node_resolver,node_calculator,node_executor,node_sandbox toneRose
class node_ui,node_pipeline_viz,node_evaluation,node_analyst,node_edgar toneIndigo
```

## Components

### Corpus ingestion

- [Filing acquisition](https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/acquire.py): `stance/ingest/acquire.py`
- [Filing parsing](https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/parse.py): `stance/ingest/parse.py`
- [Section segmentation](https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/segment.py): `stance/ingest/segment.py`
- [Parent-child chunks](https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/chunk.py): `stance/ingest/chunk.py`
- [Text embeddings](https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/embed.py): `stance/ingest/embed.py`
- [XBRL fact extraction](https://github.com/aditya-siraskar/stance/blob/main/stance/ingest/facts.py): `stance/ingest/facts.py`
- [Postgres + pgvector](https://github.com/aditya-siraskar/stance/blob/main/stance/db.py): `stance/db.py`

### Question workflow

- [LangGraph workflow](https://github.com/aditya-siraskar/stance/blob/main/stance/agents/graph.py): `stance/agents/graph.py`
- [Scope resolution](https://github.com/aditya-siraskar/stance/blob/main/stance/agents/scope.py): `stance/agents/scope.py`
- [Question planner](https://github.com/aditya-siraskar/stance/blob/main/stance/agents/planner.py): `stance/agents/planner.py`
- [Deterministic router](https://github.com/aditya-siraskar/stance/blob/main/stance/agents/router.py): `stance/agents/router.py`
- [Typed contracts](https://github.com/aditya-siraskar/stance/blob/main/stance/contracts.py): `stance/contracts.py`
- [Citation verifier](https://github.com/aditya-siraskar/stance/blob/main/stance/agents/verifier.py): `stance/agents/verifier.py`
- [Model routing](https://github.com/aditya-siraskar/stance/blob/main/stance/llm.py): `stance/llm.py`

### Narrative retrieval

- [Hybrid search](https://github.com/aditya-siraskar/stance/blob/main/stance/retrieval/hybrid.py): `stance/retrieval/hybrid.py`
- [Result reranking](https://github.com/aditya-siraskar/stance/blob/main/stance/retrieval/rerank.py): `stance/retrieval/rerank.py`
- [Section expansion](https://github.com/aditya-siraskar/stance/blob/main/stance/retrieval/expand.py): `stance/retrieval/expand.py`
- [Narrative drafting](https://github.com/aditya-siraskar/stance/blob/main/stance/agents/writer.py): `stance/agents/writer.py`

### Numeric verification

- [Numeric interpretation](https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/interpret.py): `stance/numeric/interpret.py`
- [Fact resolution](https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/resolver.py): `stance/numeric/resolver.py`
- [Computation specs](https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/calculator.py): `stance/numeric/calculator.py`
- [Spec execution](https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/executor.py): `stance/numeric/executor.py`
- [Arithmetic sandbox](https://github.com/aditya-siraskar/stance/blob/main/stance/numeric/sandbox.py): `stance/numeric/sandbox.py`

### User experience

- [Streamlit UI](https://github.com/aditya-siraskar/stance/blob/main/apps/ui/app.py): `apps/ui/app.py`
- [Pipeline display](https://github.com/aditya-siraskar/stance/blob/main/apps/ui/pipeline_viz.py): `apps/ui/pipeline_viz.py`
- [Evaluation driver](https://github.com/aditya-siraskar/stance/blob/main/eval/run_eval.py): `eval/run_eval.py`

### Other components

- **Analyst**
- **SEC EDGAR**

## Connections

- Analyst → Streamlit UI: asks question (evidence: `README.md`)
- Streamlit UI → LangGraph workflow: invokes / streams (evidence: `apps/ui/app.py`)
- LangGraph workflow → Scope resolution: resolves scope (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Question planner: builds plan (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Deterministic router: routes plan (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Hybrid search: searches text (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Result reranking: reranks candidates (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Section expansion: expands results (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Narrative drafting: drafts narrative (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Numeric interpretation: answers numeric steps (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Citation verifier: verifies draft (evidence: `stance/agents/graph.py`)
- LangGraph workflow → Typed contracts: uses typed state (evidence: `stance/agents/graph.py`)
- Question planner → Model routing: requests completion (evidence: `stance/agents/planner.py`)
- Narrative drafting → Model routing: requests completion (evidence: `stance/agents/writer.py`)
- Numeric interpretation → Fact resolution: resolves facts (evidence: `stance/numeric/interpret.py`)
- Numeric interpretation → Computation specs: builds computation (evidence: `stance/numeric/interpret.py`)
- Numeric interpretation → Spec execution: executes spec (evidence: `stance/numeric/interpret.py`)
- Computation specs → Typed contracts: emits spec (evidence: `stance/numeric/calculator.py`)
- Spec execution → Fact resolution: loads operands (evidence: `stance/numeric/executor.py`)
- Spec execution → Arithmetic sandbox: runs operation (evidence: `stance/numeric/executor.py`)
- Citation verifier → Fact resolution: rechecks citations (evidence: `stance/agents/verifier.py`)
- Scope resolution → Typed contracts: produces scope (evidence: `stance/agents/scope.py`)
- Question planner → Typed contracts: produces plan (evidence: `stance/agents/planner.py`)
- Deterministic router → Typed contracts: reads plan (evidence: `stance/agents/router.py`)
- Streamlit UI → Pipeline display: renders trace (evidence: `apps/ui/app.py`)
- Streamlit UI → Evaluation driver: runs evaluation (evidence: `apps/ui/app.py`)
- Evaluation driver → LangGraph workflow: evaluates questions (evidence: `eval/run_eval.py`)
- Evaluation driver → Citation verifier: runs injection check (evidence: `eval/run_eval.py`)
- Evaluation driver → Fact resolution: looks up test fact (evidence: `eval/run_eval.py`)
- SEC EDGAR → Filing acquisition: supplies filings (evidence: `README.md`)
- Filing acquisition → Filing parsing: ingestion sequence (evidence: `README.md`)
- Filing parsing → Section segmentation: ingestion sequence (evidence: `README.md`)
- Section segmentation → Parent-child chunks: ingestion sequence (evidence: `README.md`)
- Parent-child chunks → Text embeddings: ingestion sequence (evidence: `README.md`)
- Text embeddings → Postgres + pgvector: stores vectors (evidence: `README.md`)
- Filing acquisition → XBRL fact extraction: extracts XBRL facts (evidence: `README.md`)
- XBRL fact extraction → Postgres + pgvector: stores facts (evidence: `README.md`)
- Hybrid search → Postgres + pgvector: searches chunks (evidence: `README.md`)
- Fact resolution → Postgres + pgvector: queries facts (evidence: `stance/numeric/resolver.py`)
