This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Semiconductor Industry Knowledge Graph (半导体产业链图谱系统) — a dual-frontend acceptance platform with a FastAPI backend, React admin portal, and React user portal. The system provides deep retrieval, evidence-gated Q&A, graph visualization, governance operations, and industry search over a Neo4j + Graphiti knowledge graph.
# Install dependencies
cd apps/api-server
python -m pip install -e .
# Run all tests
python -m pytest -q
# Run a single test file
python -m pytest tests/test_qa_pipeline.py -q
# Run a single test by name
python -m pytest tests/test_retrieval.py::test_merge_candidates_deduplication -q
# Start the API server (from repo root)
$env:APP_ENV = "development"
$env:ADMIN_PASSWORD = "admin"
$env:SESSION_SECRET = "<random-secret>"
$env:COOKIE_SECURE = "false"
python -m uvicorn app.main:app --app-dir apps/api-server --host 127.0.0.1 --port 8001 --workers 1cd apps/admin-portal
pnpm install
pnpm dev # dev server
pnpm test # vitest run
pnpm build # tsc + vite buildcd apps/user-portal
pnpm install
pnpm dev # dev server
pnpm test # vitest run
pnpm build # tsc + vite build.\start-acceptance-platform.bat # starts API + both frontends
.\stop-acceptance-platform.bat # stops them (does not stop Neo4j)apps/api-server/— Python 3.11+ FastAPI backend. Entry point:app/main.py→create_app(). Uses SQLite for observability (conversations, query runs, retrieval traces, LLM call logs, background jobs) and Neo4j + Graphiti for the knowledge graph.apps/admin-portal/— React + Vite + TypeScript admin UI (Cytoscape graph canvas, governance panel, trace monitor, decision signals, industry search). Port 7871.apps/user-portal/— React + Vite + TypeScript user-facing Q&A UI. Port 7870.
The backend follows a layered architecture: Routers → Services → Storage/External.
Key services and their roles:
qa_pipeline.py—QuestionAnsweringPipeline: orchestrates the full Q&A flow: question rewrite → deep retrieval → evidence gate → answer generation. Each stage is traced and persisted to SQLite.retrieval.py—DeepSearchService: multi-slice retrieval combining 4 algorithms in parallel (vector cosine, BM25, BFS from entity origins, Neo4j lexical). Results are deduplicated by UUID/content-hash, merged via RRF, then reranked by CrossEncoder (SiliconFlow reranker) with RRF fallback. Configurable limits:slice_limit=20,candidate_pool_limit=60,final_result_limit=12.trust.py—EvidenceGate+TrustBuilder: evidence gate refuses answers lacking public evidence; trust builder outputs 4 independent dimensions (evidence quality, confidence, freshness, verification) — never a composite score.potential_fitandinferredclaims never upgrade to confirmed.evidence.py—EvidenceIndex: loads JSONL evidence files fromdata/evidence/, indexes byevidence_id, resolves citations for retrieval candidates.governance.py—GovernanceService: saga summarization (paginated LLM calls), community rebuild (Louvain on NetworkX + LLM naming), all write-locked to groupsemiconductor_dc_kg.unified_graph.py—UnifiedGraphService: hierarchical graph expansion with 8 branches (hierarchy, business, facts, timeline, saga, community, evidence, digital_china). Translates Neo4j Cypher results intoGraphPayloadwith positioned nodes.graph.py—GraphService: lower-level graph queries with label/relation mappings (NODE_LABELS,RELATION_LABELS,RELATION_NAME_LABELS,BUSINESS_RELATION_TYPES).decision_signals.py— Import decision signals (policy/tech/industry/business) from links, PDFs, or text into Graphiti episodes under a dedicated saga.web_search.py—WebSearchService: multi-provider web search (Tavily, Bocha) for industry search.group_context.py— Multi-tenancy: resolvesgroup_idfrom query param,X-Graph-Groupheader, or default setting. Validates group IDs against pattern^[A-Za-z0-9][A-Za-z0-9_-]{1,63}$.runtime.py—OpenAICompatibleChatClient: wraps OpenAI SDK for question rewrite and answer generation via SiliconFlow.tracing.py— Optional OpenTelemetry integration (disabled by default).start_span()is a no-op when OTEL is not configured.events.py—RequestEventBroker: in-process SSE event streaming for real-time Q&A progress.
The Q&A pipeline uses Python Protocol classes for testability:
ChatClientprotocol —rewrite()andanswer()methods, implemented byOpenAICompatibleChatClientSearchServiceprotocol —search()method, implemented byDeepSearchServiceCrossEncoderprotocol —rank()method, wrapped byGraphitiCrossEncoderAdapter
Tests inject mock implementations via these protocols. When adding new service dependencies, define a Protocol and accept it in the constructor.
Services are lazily initialized per group_id and cached in app.state dictionaries:
qa_pipeline_by_group: dict[str, QuestionAnsweringPipeline]withpipeline_lockgraph_service_by_group: dict[str, GraphService]withgraph_service_lockunified_graph_service_by_group: dict[str, UnifiedGraphService]withunified_graph_service_lockgovernance_service_by_group: dict[str, GovernanceService]withgovernance_service_lock
Each router checks the cache first, then creates the service under the corresponding asyncio.Lock if missing. The default service (no group) is stored separately (e.g., app.state.qa_pipeline). On shutdown, all cached services are closed.
- User sends question →
POST /api/v1/user/conversations/{id}/messages QuestionAnsweringPipeline.run():- Rewrite: if conversation history exists, LLM rewrites the question (falls back to original on failure)
- Deep Retrieval: 4 parallel slices (vector, BM25, BFS, lexical) → merge → dedup → RRF → CrossEncoder rerank → top-12 selected
- Evidence Gate: only candidates with resolvable
evidence_idspass;no_public_evidenceclaims are rejected - Answer Generation: LLM generates answer from supporting candidates; falls back to fact summary on LLM failure
- Result includes answer, citations, trust profile, and full retrieval trace
- SSE events stream progress to the user portal in real-time (
stage,token,completedevents)
Governance operations (saga summarize, community rebuild) run as background jobs with a lease/heartbeat pattern:
- Jobs record
worker_id,heartbeat_at, andlease_expires_atin SQLite - On startup,
recover_interrupted_jobs()only interrupts jobs with expired or missing leases - Lease duration: 300s; heartbeat interval: 60s
- SQLite (
data/runtime/qa_observability.db): conversations, messages, query_runs, retrieval_slices, retrieval_candidates, llm_calls, background_jobs. Uses WAL mode.Repositoryclass instorage.pymanages all access. Path is resolved relative toPROJECT_ROOT(computed fromconfig.pylocation, 3 levels up), not CWD. - Neo4j: knowledge graph (entities, relationships, episodes, sagas, communities, evidence). Accessed via Graphiti SDK or direct Cypher.
- JSONL (
data/evidence/*.jsonl): evidence records loaded at startup byEvidenceIndex. Evidence IDs follow pattern:E\d+-(?:[A-Z]+)?\d+,DC(?:-[A-Z]+)?-\d+, orDS-[A-Z0-9][A-Z0-9-]*.
- Cookie-based:
CookieSignerinauth.pysigns/verifies user and admin cookies - Admin access requires
ADMIN_PASSWORDlogin → admin cookie - User identity is anonymous session-based (auto-generated UUID)
- Non-development environments require
SESSION_SECRETor the server refuses to start
| Variable | Default | Notes |
|---|---|---|
APP_ENV |
development |
Non-dev requires SESSION_SECRET |
NEO4J_URI |
bolt://localhost:7687 |
|
NEO4J_USER |
neo4j |
|
NEO4J_PASSWORD |
password |
Override in production |
GROUP_ID |
semiconductor_dc_kg |
Default graph group |
API_PORT |
8001 |
|
SQLITE_PATH |
data/runtime/qa_observability.db |
Resolved relative to project root |
ADMIN_PASSWORD |
(empty) | Must be set |
SESSION_SECRET |
(auto-generated in dev) | Required in non-dev environments |
COOKIE_SECURE |
true |
Set false for local HTTP |
SILICONFLOW_API_KEY |
(empty) | Required for LLM/embedding/reranker |
GRAPHITI_LLM_BASE_URL |
https://api.siliconflow.cn/v1 |
|
GRAPHITI_LLM_MODEL |
deepseek-ai/DeepSeek-V3.2 |
|
GRAPHITI_EMBEDDING_BASE_URL |
https://api.siliconflow.cn/v1 |
|
GRAPHITI_EMBEDDING_MODEL |
BAAI/bge-m3 |
|
GRAPHITI_RERANKER_MODEL |
BAAI/bge-reranker-v2-m3 |
|
RETRIEVAL_SLICE_LIMIT |
20 |
Per-slice retrieval cap |
RETRIEVAL_CANDIDATE_POOL_LIMIT |
60 |
Post-dedup candidate pool cap |
RETRIEVAL_FINAL_RESULT_LIMIT |
12 |
Final selected results cap |
RETRIEVAL_SEARCH_TIMEOUT_SECONDS |
20.0 |
Per-search timeout |
TAVILY_API_KEY |
(empty) | For web search |
BOCHA_API_KEY |
(empty) | For web search |
BOCHA_SEARCH_URL |
(empty) | Bocha search endpoint |
OTEL_ENABLED |
false |
Enable OpenTelemetry tracing |
OTEL_SERVICE_NAME |
dual-frontend-api-server |
|
OTEL_EXPORTER_OTLP_ENDPOINT |
(empty) |
Data processing and maintenance scripts for building, importing, enriching, and verifying the knowledge graph. These are standalone scripts (not part of the API server) that operate directly on Neo4j/Graphiti. Key shared utilities in scripts/kg_common.py.
Notable scripts:
import_semiconductor_graph_to_neo4j.py— initial graph importimport_graphiti_episodes.py/export_graphiti_episodes.py— Graphiti data migrationbuild_industry_graph.py— industry graph constructionbusiness_graph_enrichment.py— enrich business relationshipsstage2_company_leaders.py/stage3_segment_leaders.py/stage4_company_relationships.py— staged data enrichmentverify_graph.py/audit_graph_quality.py— graph quality verificationevaluate_graphiti_recall.py— retrieval recall evaluationmigrate_graphiti_saga_layers.py— saga layer migrationtemporal_enrichment.py— temporal data enrichmentrepair_stage3_authoritative_evidence.py(and rounds 4-10) — iterative evidence repair
business_graph/— CSV files for L1-L4 segments, products, company relationships, competition, digital China relations, business opportunitiescompany_pool/— Company candidates, leaders, and relationship dataevidence/— JSONL evidence records (stages 1-3, decision signals)graphiti/— Graphiti episode data and migration notesdecision_signals/— Seed decision signal definitionsruntime/— SQLite DB and runtime artifacts (gitignored)
/api/v1/user/*— User-facing: conversations, messages (SSE streaming), graph expand/api/v1/admin/*— Admin: governance (saga summarize, community rebuild), graph visualization, trace monitor, decision signal import, industry search, background jobs/api/v1/groups/*— Group management/health— Health check (verifies SQLite connectivity)
Both frontends call the API at http://127.0.0.1:8001. CORS only allows http://127.0.0.1:7870 and http://127.0.0.1:7871 with credentials. The admin portal uses Cytoscape for graph visualization; the user portal is a simpler Q&A interface.
Admin Portal (apps/admin-portal/src/): Single-page app with sidebar navigation. Pages:
GraphCenter— Cytoscape graph canvas with expand/collapse by branch, governance panel sidebarIndustrySearchPanel— Industry search with multi-provider supportDecisionSignalsPage— Decision signal import (link/document/text)TraceMonitor— Query run trace viewer
User Portal (apps/user-portal/src/): Chat-style Q&A interface. SSE flow:
submitQuestion()→ POST message → getrequest_idrequestEventSource(request_id)→ SSE connection forstage,token,completedevents- On
completed→getQuestionResult()→ replace streaming message with final result
Both portals share the same api.ts pattern: fetch with credentials, group_id in query params or X-Graph-Group header.
- Backend tests use
pytestwithhttpxAsyncClient(no live server needed). Tests mock Neo4j/Graphiti/LLM dependencies via Protocol-based injection.conftest.pyaddsAPI_ROOTtosys.path. - Frontend tests use
vitest+@testing-library/react. - All test files follow
test_*.py/*.test.ts(x)convention colocated intests/directories.