cortex-kg 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. cortex_kg-0.1.0/LICENSE +21 -0
  2. cortex_kg-0.1.0/MANIFEST.in +9 -0
  3. cortex_kg-0.1.0/PKG-INFO +242 -0
  4. cortex_kg-0.1.0/README.md +177 -0
  5. cortex_kg-0.1.0/docs/README.md +23 -0
  6. cortex_kg-0.1.0/docs/automation.md +164 -0
  7. cortex_kg-0.1.0/docs/cursor.md +201 -0
  8. cortex_kg-0.1.0/docs/installation.md +87 -0
  9. cortex_kg-0.1.0/docs/other-platforms.md +181 -0
  10. cortex_kg-0.1.0/docs/project-workflow.md +147 -0
  11. cortex_kg-0.1.0/docs/publishing.md +67 -0
  12. cortex_kg-0.1.0/docs/templates/cortex-agent-rule.mdc +33 -0
  13. cortex_kg-0.1.0/docs/templates/cortex-mcp.json +11 -0
  14. cortex_kg-0.1.0/examples/sample_repo/api/openapi.yaml +21 -0
  15. cortex_kg-0.1.0/examples/sample_repo/docs/adr-001-session-tokens.md +14 -0
  16. cortex_kg-0.1.0/examples/sample_repo/docs/architecture.md +12 -0
  17. cortex_kg-0.1.0/examples/sample_repo/notes/meeting-2026-07-10-billing-sync.md +13 -0
  18. cortex_kg-0.1.0/examples/sample_repo/schema/schema.sql +12 -0
  19. cortex_kg-0.1.0/examples/sample_repo/src/auth_service.py +27 -0
  20. cortex_kg-0.1.0/examples/sample_repo/src/billing_service.py +14 -0
  21. cortex_kg-0.1.0/pyproject.toml +86 -0
  22. cortex_kg-0.1.0/sdk/python/cortex_sdk/__init__.py +117 -0
  23. cortex_kg-0.1.0/sdk/python/pyproject.toml +13 -0
  24. cortex_kg-0.1.0/sdk/typescript/package.json +13 -0
  25. cortex_kg-0.1.0/sdk/typescript/src/index.ts +158 -0
  26. cortex_kg-0.1.0/sdk/typescript/tsconfig.json +12 -0
  27. cortex_kg-0.1.0/setup.cfg +4 -0
  28. cortex_kg-0.1.0/src/cortex/__init__.py +8 -0
  29. cortex_kg-0.1.0/src/cortex/agents/__init__.py +0 -0
  30. cortex_kg-0.1.0/src/cortex/agents/hooks.py +55 -0
  31. cortex_kg-0.1.0/src/cortex/agents/writeback.py +58 -0
  32. cortex_kg-0.1.0/src/cortex/api/__init__.py +0 -0
  33. cortex_kg-0.1.0/src/cortex/api/rest.py +239 -0
  34. cortex_kg-0.1.0/src/cortex/cli.py +238 -0
  35. cortex_kg-0.1.0/src/cortex/config.py +80 -0
  36. cortex_kg-0.1.0/src/cortex/context/__init__.py +0 -0
  37. cortex_kg-0.1.0/src/cortex/context/generator.py +146 -0
  38. cortex_kg-0.1.0/src/cortex/daemon.py +102 -0
  39. cortex_kg-0.1.0/src/cortex/extractors/__init__.py +25 -0
  40. cortex_kg-0.1.0/src/cortex/extractors/api_spec_extractor.py +76 -0
  41. cortex_kg-0.1.0/src/cortex/extractors/base.py +63 -0
  42. cortex_kg-0.1.0/src/cortex/extractors/code_extractor.py +255 -0
  43. cortex_kg-0.1.0/src/cortex/extractors/db_schema_extractor.py +78 -0
  44. cortex_kg-0.1.0/src/cortex/extractors/git_extractor.py +111 -0
  45. cortex_kg-0.1.0/src/cortex/extractors/image_extractor.py +62 -0
  46. cortex_kg-0.1.0/src/cortex/extractors/llm_relation_extractor.py +99 -0
  47. cortex_kg-0.1.0/src/cortex/extractors/markdown_extractor.py +86 -0
  48. cortex_kg-0.1.0/src/cortex/extractors/notes_extractor.py +101 -0
  49. cortex_kg-0.1.0/src/cortex/extractors/pdf_extractor.py +59 -0
  50. cortex_kg-0.1.0/src/cortex/ingestion/__init__.py +0 -0
  51. cortex_kg-0.1.0/src/cortex/ingestion/content_hash.py +56 -0
  52. cortex_kg-0.1.0/src/cortex/ingestion/pipeline.py +152 -0
  53. cortex_kg-0.1.0/src/cortex/ingestion/resolver.py +131 -0
  54. cortex_kg-0.1.0/src/cortex/mcp_server/__init__.py +0 -0
  55. cortex_kg-0.1.0/src/cortex/mcp_server/server.py +187 -0
  56. cortex_kg-0.1.0/src/cortex/models.py +169 -0
  57. cortex_kg-0.1.0/src/cortex/offline/__init__.py +0 -0
  58. cortex_kg-0.1.0/src/cortex/offline/local_llm.py +71 -0
  59. cortex_kg-0.1.0/src/cortex/ontology/__init__.py +3 -0
  60. cortex_kg-0.1.0/src/cortex/ontology/registry.py +171 -0
  61. cortex_kg-0.1.0/src/cortex/plugins/__init__.py +0 -0
  62. cortex_kg-0.1.0/src/cortex/plugins/examples/jira_like_plugin/plugin.py +51 -0
  63. cortex_kg-0.1.0/src/cortex/plugins/interface.py +48 -0
  64. cortex_kg-0.1.0/src/cortex/plugins/loader.py +106 -0
  65. cortex_kg-0.1.0/src/cortex/replication/__init__.py +0 -0
  66. cortex_kg-0.1.0/src/cortex/replication/crdt.py +95 -0
  67. cortex_kg-0.1.0/src/cortex/retrieval/__init__.py +0 -0
  68. cortex_kg-0.1.0/src/cortex/retrieval/embeddings.py +79 -0
  69. cortex_kg-0.1.0/src/cortex/retrieval/fusion.py +47 -0
  70. cortex_kg-0.1.0/src/cortex/retrieval/planner.py +49 -0
  71. cortex_kg-0.1.0/src/cortex/retrieval/query_engine.py +158 -0
  72. cortex_kg-0.1.0/src/cortex/retrieval/traversal.py +47 -0
  73. cortex_kg-0.1.0/src/cortex/security/__init__.py +0 -0
  74. cortex_kg-0.1.0/src/cortex/security/acl.py +90 -0
  75. cortex_kg-0.1.0/src/cortex/security/audit.py +26 -0
  76. cortex_kg-0.1.0/src/cortex/security/encryption.py +56 -0
  77. cortex_kg-0.1.0/src/cortex/security/secrets_scanner.py +34 -0
  78. cortex_kg-0.1.0/src/cortex/server_profile/__init__.py +0 -0
  79. cortex_kg-0.1.0/src/cortex/server_profile/neo4j_store.py +200 -0
  80. cortex_kg-0.1.0/src/cortex/setup/__init__.py +15 -0
  81. cortex_kg-0.1.0/src/cortex/setup/generators.py +108 -0
  82. cortex_kg-0.1.0/src/cortex/storage/__init__.py +12 -0
  83. cortex_kg-0.1.0/src/cortex/storage/graph_store.py +322 -0
  84. cortex_kg-0.1.0/src/cortex/storage/keyword_store.py +81 -0
  85. cortex_kg-0.1.0/src/cortex/storage/provenance.py +233 -0
  86. cortex_kg-0.1.0/src/cortex/storage/vector_store.py +77 -0
  87. cortex_kg-0.1.0/src/cortex/sync/__init__.py +0 -0
  88. cortex_kg-0.1.0/src/cortex/sync/change_feed.py +71 -0
  89. cortex_kg-0.1.0/src/cortex/sync/job_queue.py +65 -0
  90. cortex_kg-0.1.0/src/cortex/sync/watcher.py +92 -0
  91. cortex_kg-0.1.0/src/cortex/versioning/__init__.py +0 -0
  92. cortex_kg-0.1.0/src/cortex/versioning/bitemporal.py +82 -0
  93. cortex_kg-0.1.0/src/cortex/workspace/__init__.py +0 -0
  94. cortex_kg-0.1.0/src/cortex/workspace/federation.py +40 -0
  95. cortex_kg-0.1.0/src/cortex/workspace/registry.py +98 -0
  96. cortex_kg-0.1.0/src/cortex_kg.egg-info/PKG-INFO +242 -0
  97. cortex_kg-0.1.0/src/cortex_kg.egg-info/SOURCES.txt +99 -0
  98. cortex_kg-0.1.0/src/cortex_kg.egg-info/dependency_links.txt +1 -0
  99. cortex_kg-0.1.0/src/cortex_kg.egg-info/entry_points.txt +2 -0
  100. cortex_kg-0.1.0/src/cortex_kg.egg-info/requires.txt +39 -0
  101. cortex_kg-0.1.0/src/cortex_kg.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Roshan Gamage
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,9 @@
1
+ include LICENSE
2
+ include README.md
3
+ include pyproject.toml
4
+ recursive-include docs *.md *.mdc *.json
5
+ recursive-include examples *
6
+ recursive-include sdk *
7
+ global-exclude __pycache__ *.py[cod] .DS_Store
8
+ prune .venv
9
+ prune tests
@@ -0,0 +1,242 @@
1
+ Metadata-Version: 2.4
2
+ Name: cortex-kg
3
+ Version: 0.1.0
4
+ Summary: Cortex: a persistent, workspace-level Engineering Knowledge Graph.
5
+ Author-email: Roshan Gamage <roshangamage01@users.noreply.github.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/RoshanGamage01/Cortex
8
+ Project-URL: Repository, https://github.com/RoshanGamage01/Cortex
9
+ Project-URL: Documentation, https://github.com/RoshanGamage01/Cortex/tree/main/docs
10
+ Project-URL: Issues, https://github.com/RoshanGamage01/Cortex/issues
11
+ Project-URL: Changelog, https://github.com/RoshanGamage01/Cortex/releases
12
+ Keywords: knowledge-graph,mcp,engineering,ai,local-first,rag,cursor
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Environment :: Console
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: Software Development :: Libraries
24
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
25
+ Requires-Python: >=3.10
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: kuzu>=0.11
29
+ Requires-Dist: lancedb>=0.15
30
+ Requires-Dist: pyarrow>=15
31
+ Requires-Dist: fastapi>=0.110
32
+ Requires-Dist: uvicorn[standard]>=0.29
33
+ Requires-Dist: watchdog>=4.0
34
+ Requires-Dist: tree-sitter>=0.21
35
+ Requires-Dist: tree-sitter-python>=0.23
36
+ Requires-Dist: tree-sitter-javascript>=0.23
37
+ Requires-Dist: tree-sitter-typescript>=0.23
38
+ Requires-Dist: tree-sitter-go>=0.23
39
+ Requires-Dist: tree-sitter-java>=0.23
40
+ Requires-Dist: pypdf>=4.0
41
+ Requires-Dist: pillow>=10.0
42
+ Requires-Dist: cryptography>=42.0
43
+ Requires-Dist: networkx>=3.2
44
+ Requires-Dist: pydantic>=2.6
45
+ Requires-Dist: mcp>=1.0
46
+ Requires-Dist: httpx>=0.27
47
+ Requires-Dist: gitpython>=3.1
48
+ Requires-Dist: pyyaml>=6.0
49
+ Requires-Dist: neo4j>=5.19
50
+ Requires-Dist: jsonschema>=4.21
51
+ Requires-Dist: python-ulid>=2.2
52
+ Requires-Dist: sse-starlette>=2.0
53
+ Requires-Dist: python-multipart>=0.0.9
54
+ Provides-Extra: dev
55
+ Requires-Dist: pytest>=8.0; extra == "dev"
56
+ Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
57
+ Requires-Dist: httpx2>=2.0; extra == "dev"
58
+ Requires-Dist: build>=1.2; extra == "dev"
59
+ Requires-Dist: twine>=5.0; extra == "dev"
60
+ Provides-Extra: ocr
61
+ Requires-Dist: pytesseract>=0.3; extra == "ocr"
62
+ Provides-Extra: server
63
+ Requires-Dist: neo4j>=5.19; extra == "server"
64
+ Dynamic: license-file
65
+
66
+ # Cortex — Engineering Knowledge Graph
67
+
68
+ Cortex is a persistent, **workspace-level** knowledge graph for software
69
+ engineering. It models *relationships* (Services, APIs, Decisions, People,
70
+ Documents, ...) rather than files, survives project deletion, and exposes
71
+ that knowledge to any AI model or dev tool over MCP and a REST API.
72
+
73
+ This repo is a working implementation of the [technical roadmap](.cursor/plans)
74
+ for Cortex, built in Python (Kuzu/LanceDB/SQLite embedded stores, in place
75
+ of the roadmap's suggested Rust core — see "Design notes" below) and
76
+ organized into the roadmap's 7 delivery phases.
77
+
78
+ ## Install
79
+
80
+ ```bash
81
+ # Recommended (isolated global CLI):
82
+ pipx install cortex-kg
83
+
84
+ # Or with pip:
85
+ pip install cortex-kg
86
+ ```
87
+
88
+ From source (development):
89
+
90
+ ```bash
91
+ git clone https://github.com/RoshanGamage01/Cortex.git
92
+ cd Cortex
93
+ python3 -m venv .venv && source .venv/bin/activate
94
+ pip install -e ".[dev]"
95
+ ```
96
+
97
+ ## Quickstart
98
+
99
+ ```bash
100
+ # Ingest a project (or the bundled sample repo from a clone):
101
+ cortex --workspace demo ingest examples/sample_repo --project SampleApp
102
+ # Or any local repo:
103
+ # cortex ingest /path/to/your/repo --project MyApp
104
+
105
+ # Ask questions:
106
+ cortex --workspace demo search "what depends on AuthService"
107
+ cortex --workspace demo context "AuthService" --format markdown
108
+
109
+ # Explore the graph directly:
110
+ cortex --workspace demo info
111
+ cortex --workspace demo node traverse <node-id> --depth 2
112
+
113
+ # Serve it to tools:
114
+ cortex --workspace demo serve-api # REST API on :8420 (see /docs)
115
+ cortex --workspace demo serve-mcp # MCP server over stdio
116
+
117
+ # Wire Cursor into a project:
118
+ cortex setup project --write --project MyApp
119
+ ```
120
+
121
+ Data lives at `~/.cortex/<workspace>/` (override with `CORTEX_HOME`) —
122
+ portable, backup-able, and independent of any project's lifecycle.
123
+
124
+ ## User guides
125
+
126
+ Full setup for Cursor, other platforms, and automation:
127
+
128
+ | Guide | Description |
129
+ |---|---|
130
+ | [docs/README.md](docs/README.md) | Guide index |
131
+ | [docs/installation.md](docs/installation.md) | pip/pipx install |
132
+ | [docs/cursor.md](docs/cursor.md) | **Cursor MCP + agent instructions** |
133
+ | [docs/project-workflow.md](docs/project-workflow.md) | Ingest and maintain a project |
134
+ | [docs/other-platforms.md](docs/other-platforms.md) | Claude, VS Code, REST, SDKs, CI |
135
+ | [docs/automation.md](docs/automation.md) | Watch mode, hooks, automated rules |
136
+ | [docs/publishing.md](docs/publishing.md) | Release to PyPI |
137
+
138
+ **Connect Cursor to a project in two commands:**
139
+
140
+ ```bash
141
+ cortex ingest . --project MyApp
142
+ cortex setup project --write --project MyApp # writes .cursor/mcp.json + agent rule
143
+ ```
144
+
145
+ Reload Cursor after setup.
146
+
147
+ ## Architecture
148
+
149
+ ```
150
+ Sources (code, git, docs, PDFs, images, DB schemas, APIs, notes, plugins)
151
+ -> Extractors (deterministic first, LLM-assisted where structure is ambiguous)
152
+ -> Entity Resolver (Candidate -> canonical Node/Edge, with provenance)
153
+ -> Storage: Kuzu (graph) + LanceDB (vectors) + SQLite FTS5 (keyword) + SQLite (provenance/jobs/ACLs/ontology)
154
+ -> Hybrid Query Engine (keyword + vector seeds -> graph expansion -> RRF fusion -> re-rank)
155
+ -> Context Generator (token-budgeted, provenance-carrying context)
156
+ -> Access layer: MCP server + REST API + TS/Python SDKs
157
+ ```
158
+
159
+ Every node/edge type is **data**, not code: the ontology lives in a schema
160
+ registry (`cortex.ontology.registry`) seeded with the roadmap's default
161
+ taxonomy but extensible at runtime by extractors, plugins, or API/MCP
162
+ clients — no migration required.
163
+
164
+ ## Project layout
165
+
166
+ ```
167
+ src/cortex/
168
+ config.py workspace data-dir layout (~/.cortex/<id>/...)
169
+ models.py Node / Edge / Candidate / Provenance
170
+ daemon.py CortexDaemon: owns every store for one workspace
171
+ cli.py `cortex` command-line entrypoint
172
+ ontology/ schema registry (ontology-as-data)
173
+ storage/ GraphStore (Kuzu), VectorStore (LanceDB), KeywordStore (FTS5), ProvenanceStore
174
+ ingestion/ content-hashing, entity resolver, the Detect->...->Emit pipeline
175
+ extractors/ code (tree-sitter), git, db schema, OpenAPI, markdown, PDF, image, notes, LLM-assisted relations
176
+ sync/ file watcher, job queue, change feed (pub/sub)
177
+ retrieval/ embeddings, RRF fusion, graph traversal/impact analysis, query planner, hybrid query engine
178
+ context/ token-budgeted context generation for LLMs
179
+ mcp_server/ MCP server (tools + resources)
180
+ api/ REST API (FastAPI)
181
+ plugins/ out-of-process plugin interface/loader + example plugin
182
+ security/ ACLs, secrets scanning, encryption-at-rest, audit log
183
+ workspace/ multi-workspace registry + cross-workspace links + federation
184
+ versioning/ bitemporal nodes/edges, supersede/tombstone, history chains
185
+ server_profile/ Neo4j/Memgraph GraphStore adapter (team-scale server profile)
186
+ replication/ CRDT/oplog multi-device sync
187
+ offline/ connectivity check + durable cloud-augmentation queue
188
+ agents/ agent write-back API + change-feed hooks
189
+ sdk/
190
+ python/cortex_sdk/ Python REST client SDK
191
+ typescript/src/index.ts TypeScript REST client SDK
192
+ examples/sample_repo/ tiny multi-service demo repo used by the tests + quickstart
193
+ scripts/benchmark.py ingestion/search/context performance benchmark
194
+ tests/ 60+ tests covering every phase below
195
+ ```
196
+
197
+ ## Phases (see the plan for full detail)
198
+
199
+ | Phase | What it delivers | Where |
200
+ |---|---|---|
201
+ | 1. Foundation | Daemon, Kuzu graph store, ontology-as-data, SQLite provenance, minimal API | `daemon.py`, `ontology/`, `storage/` |
202
+ | 2. Extraction & sync | Code/git/schema/API extractors, content-hash incremental sync, resolver v1 | `extractors/`, `ingestion/`, `sync/` |
203
+ | 3. Hybrid retrieval | Vectors, keyword index, traversal, RRF fusion, query planner | `retrieval/` |
204
+ | 4. Access layer | MCP server, REST API, TS+Python SDKs, context generation | `mcp_server/`, `api/`, `sdk/`, `context/` |
205
+ | 5. Unstructured + LLM-assisted | Markdown/PDF/image/notes extractors, schema-constrained LLM relation extraction, feedback loop | `extractors/markdown_extractor.py`, `pdf_extractor.py`, `image_extractor.py`, `notes_extractor.py`, `llm_relation_extractor.py` |
206
+ | 6. Platform | Plugin SDK, ACLs/encryption/secrets-scanning, multi-workspace, bitemporal versioning | `plugins/`, `security/`, `workspace/`, `versioning/` |
207
+ | 7. Scale & agents | Neo4j/Memgraph adapter, CRDT sync, offline fallback, caching, agent write-back | `server_profile/`, `replication/`, `offline/`, `agents/` |
208
+
209
+ ## Design notes / deviations from the roadmap
210
+
211
+ - **Core engine is Python, not Rust.** The roadmap's storage picks (Kuzu,
212
+ LanceDB, SQLite) and every architectural boundary (`GraphStore` /
213
+ `VectorStore` / `Extractor` / plugin protocol) are implemented exactly as
214
+ specified; only the host language differs, for iteration speed and
215
+ because Kuzu/LanceDB both ship first-class embedded Python bindings. A
216
+ Rust rewrite of the core would implement the same `GraphStore` /
217
+ extraction / retrieval contracts.
218
+ - **Keyword index is SQLite FTS5, not Tantivy.** Zero extra dependency,
219
+ same "exact-term/symbol search" role in the hybrid retrieval recipe;
220
+ swappable behind `KeywordStore` if Tantivy bindings are preferred later.
221
+ - **Default embedding model is a deterministic offline hashing embedding**
222
+ (`cortex.retrieval.embeddings.HashingEmbeddingProvider`), not a trained
223
+ semantic model — by design, so the whole system (including tests/CI) runs
224
+ fully offline with no model download. Swap in a real local/cloud model by
225
+ implementing `EmbeddingProvider`.
226
+ - **LLM-assisted extraction/planning ship transparent local-heuristic
227
+ defaults** (`cortex.extractors.llm_relation_extractor`,
228
+ `cortex.retrieval.planner`) rather than calling a cloud model, again for
229
+ offline-by-default operation; both are written to the exact request/response
230
+ contract a real LLM backend would fill in via `CORTEX_LLM_PROVIDER`.
231
+
232
+ ## Testing
233
+
234
+ ```bash
235
+ python3 -m pytest tests/ -q
236
+ ```
237
+
238
+ 60 tests cover the full pipeline end-to-end (ingest the sample repo through
239
+ every extractor, resolve into the graph, hybrid-search it, generate
240
+ context, serve it over MCP/REST) plus targeted tests per phase (ACLs,
241
+ secrets scanning, encryption, CRDT merge, bitemporal versioning, plugin
242
+ sandboxing, multi-workspace federation, and the performance cache).
@@ -0,0 +1,177 @@
1
+ # Cortex — Engineering Knowledge Graph
2
+
3
+ Cortex is a persistent, **workspace-level** knowledge graph for software
4
+ engineering. It models *relationships* (Services, APIs, Decisions, People,
5
+ Documents, ...) rather than files, survives project deletion, and exposes
6
+ that knowledge to any AI model or dev tool over MCP and a REST API.
7
+
8
+ This repo is a working implementation of the [technical roadmap](.cursor/plans)
9
+ for Cortex, built in Python (Kuzu/LanceDB/SQLite embedded stores, in place
10
+ of the roadmap's suggested Rust core — see "Design notes" below) and
11
+ organized into the roadmap's 7 delivery phases.
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ # Recommended (isolated global CLI):
17
+ pipx install cortex-kg
18
+
19
+ # Or with pip:
20
+ pip install cortex-kg
21
+ ```
22
+
23
+ From source (development):
24
+
25
+ ```bash
26
+ git clone https://github.com/RoshanGamage01/Cortex.git
27
+ cd Cortex
28
+ python3 -m venv .venv && source .venv/bin/activate
29
+ pip install -e ".[dev]"
30
+ ```
31
+
32
+ ## Quickstart
33
+
34
+ ```bash
35
+ # Ingest a project (or the bundled sample repo from a clone):
36
+ cortex --workspace demo ingest examples/sample_repo --project SampleApp
37
+ # Or any local repo:
38
+ # cortex ingest /path/to/your/repo --project MyApp
39
+
40
+ # Ask questions:
41
+ cortex --workspace demo search "what depends on AuthService"
42
+ cortex --workspace demo context "AuthService" --format markdown
43
+
44
+ # Explore the graph directly:
45
+ cortex --workspace demo info
46
+ cortex --workspace demo node traverse <node-id> --depth 2
47
+
48
+ # Serve it to tools:
49
+ cortex --workspace demo serve-api # REST API on :8420 (see /docs)
50
+ cortex --workspace demo serve-mcp # MCP server over stdio
51
+
52
+ # Wire Cursor into a project:
53
+ cortex setup project --write --project MyApp
54
+ ```
55
+
56
+ Data lives at `~/.cortex/<workspace>/` (override with `CORTEX_HOME`) —
57
+ portable, backup-able, and independent of any project's lifecycle.
58
+
59
+ ## User guides
60
+
61
+ Full setup for Cursor, other platforms, and automation:
62
+
63
+ | Guide | Description |
64
+ |---|---|
65
+ | [docs/README.md](docs/README.md) | Guide index |
66
+ | [docs/installation.md](docs/installation.md) | pip/pipx install |
67
+ | [docs/cursor.md](docs/cursor.md) | **Cursor MCP + agent instructions** |
68
+ | [docs/project-workflow.md](docs/project-workflow.md) | Ingest and maintain a project |
69
+ | [docs/other-platforms.md](docs/other-platforms.md) | Claude, VS Code, REST, SDKs, CI |
70
+ | [docs/automation.md](docs/automation.md) | Watch mode, hooks, automated rules |
71
+ | [docs/publishing.md](docs/publishing.md) | Release to PyPI |
72
+
73
+ **Connect Cursor to a project in two commands:**
74
+
75
+ ```bash
76
+ cortex ingest . --project MyApp
77
+ cortex setup project --write --project MyApp # writes .cursor/mcp.json + agent rule
78
+ ```
79
+
80
+ Reload Cursor after setup.
81
+
82
+ ## Architecture
83
+
84
+ ```
85
+ Sources (code, git, docs, PDFs, images, DB schemas, APIs, notes, plugins)
86
+ -> Extractors (deterministic first, LLM-assisted where structure is ambiguous)
87
+ -> Entity Resolver (Candidate -> canonical Node/Edge, with provenance)
88
+ -> Storage: Kuzu (graph) + LanceDB (vectors) + SQLite FTS5 (keyword) + SQLite (provenance/jobs/ACLs/ontology)
89
+ -> Hybrid Query Engine (keyword + vector seeds -> graph expansion -> RRF fusion -> re-rank)
90
+ -> Context Generator (token-budgeted, provenance-carrying context)
91
+ -> Access layer: MCP server + REST API + TS/Python SDKs
92
+ ```
93
+
94
+ Every node/edge type is **data**, not code: the ontology lives in a schema
95
+ registry (`cortex.ontology.registry`) seeded with the roadmap's default
96
+ taxonomy but extensible at runtime by extractors, plugins, or API/MCP
97
+ clients — no migration required.
98
+
99
+ ## Project layout
100
+
101
+ ```
102
+ src/cortex/
103
+ config.py workspace data-dir layout (~/.cortex/<id>/...)
104
+ models.py Node / Edge / Candidate / Provenance
105
+ daemon.py CortexDaemon: owns every store for one workspace
106
+ cli.py `cortex` command-line entrypoint
107
+ ontology/ schema registry (ontology-as-data)
108
+ storage/ GraphStore (Kuzu), VectorStore (LanceDB), KeywordStore (FTS5), ProvenanceStore
109
+ ingestion/ content-hashing, entity resolver, the Detect->...->Emit pipeline
110
+ extractors/ code (tree-sitter), git, db schema, OpenAPI, markdown, PDF, image, notes, LLM-assisted relations
111
+ sync/ file watcher, job queue, change feed (pub/sub)
112
+ retrieval/ embeddings, RRF fusion, graph traversal/impact analysis, query planner, hybrid query engine
113
+ context/ token-budgeted context generation for LLMs
114
+ mcp_server/ MCP server (tools + resources)
115
+ api/ REST API (FastAPI)
116
+ plugins/ out-of-process plugin interface/loader + example plugin
117
+ security/ ACLs, secrets scanning, encryption-at-rest, audit log
118
+ workspace/ multi-workspace registry + cross-workspace links + federation
119
+ versioning/ bitemporal nodes/edges, supersede/tombstone, history chains
120
+ server_profile/ Neo4j/Memgraph GraphStore adapter (team-scale server profile)
121
+ replication/ CRDT/oplog multi-device sync
122
+ offline/ connectivity check + durable cloud-augmentation queue
123
+ agents/ agent write-back API + change-feed hooks
124
+ sdk/
125
+ python/cortex_sdk/ Python REST client SDK
126
+ typescript/src/index.ts TypeScript REST client SDK
127
+ examples/sample_repo/ tiny multi-service demo repo used by the tests + quickstart
128
+ scripts/benchmark.py ingestion/search/context performance benchmark
129
+ tests/ 60+ tests covering every phase below
130
+ ```
131
+
132
+ ## Phases (see the plan for full detail)
133
+
134
+ | Phase | What it delivers | Where |
135
+ |---|---|---|
136
+ | 1. Foundation | Daemon, Kuzu graph store, ontology-as-data, SQLite provenance, minimal API | `daemon.py`, `ontology/`, `storage/` |
137
+ | 2. Extraction & sync | Code/git/schema/API extractors, content-hash incremental sync, resolver v1 | `extractors/`, `ingestion/`, `sync/` |
138
+ | 3. Hybrid retrieval | Vectors, keyword index, traversal, RRF fusion, query planner | `retrieval/` |
139
+ | 4. Access layer | MCP server, REST API, TS+Python SDKs, context generation | `mcp_server/`, `api/`, `sdk/`, `context/` |
140
+ | 5. Unstructured + LLM-assisted | Markdown/PDF/image/notes extractors, schema-constrained LLM relation extraction, feedback loop | `extractors/markdown_extractor.py`, `pdf_extractor.py`, `image_extractor.py`, `notes_extractor.py`, `llm_relation_extractor.py` |
141
+ | 6. Platform | Plugin SDK, ACLs/encryption/secrets-scanning, multi-workspace, bitemporal versioning | `plugins/`, `security/`, `workspace/`, `versioning/` |
142
+ | 7. Scale & agents | Neo4j/Memgraph adapter, CRDT sync, offline fallback, caching, agent write-back | `server_profile/`, `replication/`, `offline/`, `agents/` |
143
+
144
+ ## Design notes / deviations from the roadmap
145
+
146
+ - **Core engine is Python, not Rust.** The roadmap's storage picks (Kuzu,
147
+ LanceDB, SQLite) and every architectural boundary (`GraphStore` /
148
+ `VectorStore` / `Extractor` / plugin protocol) are implemented exactly as
149
+ specified; only the host language differs, for iteration speed and
150
+ because Kuzu/LanceDB both ship first-class embedded Python bindings. A
151
+ Rust rewrite of the core would implement the same `GraphStore` /
152
+ extraction / retrieval contracts.
153
+ - **Keyword index is SQLite FTS5, not Tantivy.** Zero extra dependency,
154
+ same "exact-term/symbol search" role in the hybrid retrieval recipe;
155
+ swappable behind `KeywordStore` if Tantivy bindings are preferred later.
156
+ - **Default embedding model is a deterministic offline hashing embedding**
157
+ (`cortex.retrieval.embeddings.HashingEmbeddingProvider`), not a trained
158
+ semantic model — by design, so the whole system (including tests/CI) runs
159
+ fully offline with no model download. Swap in a real local/cloud model by
160
+ implementing `EmbeddingProvider`.
161
+ - **LLM-assisted extraction/planning ship transparent local-heuristic
162
+ defaults** (`cortex.extractors.llm_relation_extractor`,
163
+ `cortex.retrieval.planner`) rather than calling a cloud model, again for
164
+ offline-by-default operation; both are written to the exact request/response
165
+ contract a real LLM backend would fill in via `CORTEX_LLM_PROVIDER`.
166
+
167
+ ## Testing
168
+
169
+ ```bash
170
+ python3 -m pytest tests/ -q
171
+ ```
172
+
173
+ 60 tests cover the full pipeline end-to-end (ingest the sample repo through
174
+ every extractor, resolve into the graph, hybrid-search it, generate
175
+ context, serve it over MCP/REST) plus targeted tests per phase (ACLs,
176
+ secrets scanning, encryption, CRDT merge, bitemporal versioning, plugin
177
+ sandboxing, multi-workspace federation, and the performance cache).
@@ -0,0 +1,23 @@
1
+ # Cortex user guides
2
+
3
+ Step-by-step guides for installing Cortex and connecting it to your tools.
4
+
5
+ | Guide | What it covers |
6
+ |---|---|
7
+ | [Installation](installation.md) | pip/pipx install, first run, workspaces |
8
+ | [Cursor integration](cursor.md) | MCP setup, per-project config, agent instructions |
9
+ | [Project workflow](project-workflow.md) | Ingest a repo, keep knowledge fresh, multi-project |
10
+ | [Other platforms](other-platforms.md) | Claude Desktop, VS Code, REST API, SDKs, CI |
11
+ | [Automation](automation.md) | Watch mode, cron, Git hooks, agent write-back |
12
+ | [Publishing](publishing.md) | Release `cortex-kg` to PyPI |
13
+
14
+ **Quick one-liner for a new project:**
15
+
16
+ ```bash
17
+ pipx install cortex-kg # or: pip install -e . from this repo
18
+ cortex init
19
+ cortex ingest . --project MyApp
20
+ cortex setup project --write --project MyApp
21
+ ```
22
+
23
+ Then reload Cursor — the AI can call Cortex MCP tools in that project.
@@ -0,0 +1,164 @@
1
+ # Automation
2
+
3
+ Ways to keep Cortex updated and instruct AI agents automatically.
4
+
5
+ ---
6
+
7
+ ## 1. Watch mode (local dev)
8
+
9
+ Re-ingest files as they change:
10
+
11
+ ```bash
12
+ cortex watch /path/to/repo --project MyApp
13
+ ```
14
+
15
+ Runs until Ctrl+C. Uses filesystem events with ~750ms debounce.
16
+
17
+ **Tip:** Run in a dedicated terminal tab or tmux pane while coding.
18
+
19
+ ---
20
+
21
+ ## 2. Project bootstrap script
22
+
23
+ Add `scripts/cortex-sync.sh` to your repo:
24
+
25
+ ```bash
26
+ #!/usr/bin/env bash
27
+ set -euo pipefail
28
+ PROJECT_NAME="${CORTEX_PROJECT:-$(basename "$PWD")}"
29
+ WORKSPACE="${CORTEX_WORKSPACE:-default}"
30
+ cortex --workspace "$WORKSPACE" ingest . --project "$PROJECT_NAME"
31
+ ```
32
+
33
+ ```bash
34
+ chmod +x scripts/cortex-sync.sh
35
+ ./scripts/cortex-sync.sh
36
+ ```
37
+
38
+ ---
39
+
40
+ ## 3. Git post-merge hook
41
+
42
+ Re-index after pulling main:
43
+
44
+ `.git/hooks/post-merge`:
45
+
46
+ ```bash
47
+ #!/bin/sh
48
+ cortex --workspace default ingest . --project MyApp 2>/dev/null || true
49
+ ```
50
+
51
+ ---
52
+
53
+ ## 4. Cron (nightly refresh)
54
+
55
+ ```cron
56
+ 0 2 * * * cd /home/you/projects/my-app && /usr/local/bin/cortex --workspace default ingest . --project MyApp >> ~/.cortex/ingest.log 2>&1
57
+ ```
58
+
59
+ ---
60
+
61
+ ## 5. Cursor agent rules (automated instructions)
62
+
63
+ `cortex setup project --write` installs `.cursor/rules/cortex.mdc`.
64
+
65
+ This tells the AI to:
66
+
67
+ | Trigger | Automated behavior |
68
+ |---|---|
69
+ | Refactor / delete / rename service | Call `explain_impact` first |
70
+ | Architecture or “how does X work?” questions | Call `search_knowledge` + `get_context` |
71
+ | User states a decision | Call `add_note` + `link` |
72
+ | Low-confidence graph edge shown | Ask user; use `confirm_edge` or `reject_edge` |
73
+ | No graph data for topic | Suggest `cortex ingest . --project …` |
74
+
75
+ Customize the rule for your team — template at `docs/templates/cortex-agent-rule.mdc`.
76
+
77
+ ---
78
+
79
+ ## 6. MCP server instructions (built-in)
80
+
81
+ The MCP server ships with system instructions (see `mcp_server/server.py`):
82
+
83
+ > Cortex models relationships between engineering concepts, not files.
84
+ > Use search_knowledge, get_context, and explain_impact before changes.
85
+
86
+ Cursor merges these with your project rules.
87
+
88
+ ---
89
+
90
+ ## 7. Change feed (event-driven)
91
+
92
+ When the REST API is running:
93
+
94
+ ```bash
95
+ curl -N "http://localhost:8420/change-feed/stream?workspace=default"
96
+ ```
97
+
98
+ Events include `node_added`, `edge_added`, ingest completions, and agent writebacks.
99
+
100
+ Use for:
101
+
102
+ - Slack notifications when ADRs are added
103
+ - Triggering downstream doc generation
104
+ - Agent hooks (`cortex.agents.hooks`)
105
+
106
+ ---
107
+
108
+ ## 8. Background job queue
109
+
110
+ Ingest enqueues embedding jobs automatically. Process them:
111
+
112
+ ```python
113
+ from cortex.daemon import get_daemon
114
+ from cortex.sync.job_queue import JobWorker
115
+
116
+ daemon = get_daemon("default")
117
+ worker = JobWorker(daemon)
118
+ worker.run_once() # or run in a loop / thread
119
+ ```
120
+
121
+ ---
122
+
123
+ ## 9. Environment variables
124
+
125
+ | Variable | Purpose |
126
+ |---|---|
127
+ | `CORTEX_HOME` | Override `~/.cortex` data root |
128
+ | `CORTEX_LLM_PROVIDER` | Plug in cloud/local LLM for relation extraction (future) |
129
+ | `CORTEX_PROJECT_ROOT` | Set by project MCP config; optional hint for tools |
130
+
131
+ ---
132
+
133
+ ## 10. Recommended automation stack
134
+
135
+ **Solo developer:**
136
+
137
+ 1. `cortex ingest .` once
138
+ 2. `cortex setup project --write`
139
+ 3. `cortex watch .` in background while coding
140
+
141
+ **Team:**
142
+
143
+ 1. Commit `.cursor/mcp.json` + `.cursor/rules/cortex.mdc`
144
+ 2. Document `cortex ingest` in README
145
+ 3. Optional CI ingest on `main`
146
+ 4. Optional shared REST server for dashboards
147
+
148
+ ---
149
+
150
+ ## Setup command reference
151
+
152
+ ```bash
153
+ # Print MCP JSON for Cursor
154
+ cortex setup cursor --workspace default
155
+
156
+ # Write .cursor/mcp.json + agent rule
157
+ cortex setup project --write --project MyApp
158
+
159
+ # Claude Desktop config
160
+ cortex setup claude --write
161
+
162
+ # VS Code (same JSON as Cursor)
163
+ cortex setup vscode --write --path .
164
+ ```