astrocyte-postgres 0.12.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 (28) hide show
  1. astrocyte_postgres-0.12.0/.gitignore +54 -0
  2. astrocyte_postgres-0.12.0/PKG-INFO +133 -0
  3. astrocyte_postgres-0.12.0/README.md +117 -0
  4. astrocyte_postgres-0.12.0/astrocyte_postgres/__init__.py +11 -0
  5. astrocyte_postgres-0.12.0/astrocyte_postgres/store.py +919 -0
  6. astrocyte_postgres-0.12.0/astrocyte_postgres/wiki_store.py +458 -0
  7. astrocyte_postgres-0.12.0/migrations/001_extension.sql +2 -0
  8. astrocyte_postgres-0.12.0/migrations/002_astrocytes_vectors.sql +20 -0
  9. astrocyte_postgres-0.12.0/migrations/003_indexes.sql +11 -0
  10. astrocyte_postgres-0.12.0/migrations/004_memory_layer.sql +2 -0
  11. astrocyte_postgres-0.12.0/migrations/005_banks_access.sql +31 -0
  12. astrocyte_postgres-0.12.0/migrations/006_lifecycle_indexes.sql +36 -0
  13. astrocyte_postgres-0.12.0/migrations/007_wiki_tables.sql +103 -0
  14. astrocyte_postgres-0.12.0/migrations/008_entities_temporal.sql +97 -0
  15. astrocyte_postgres-0.12.0/migrations/009_entities_trigram_embedding.sql +40 -0
  16. astrocyte_postgres-0.12.0/migrations/010_hybrid_recall_indexes.sql +4 -0
  17. astrocyte_postgres-0.12.0/migrations/011_text_fts.sql +44 -0
  18. astrocyte_postgres-0.12.0/pyproject.toml +55 -0
  19. astrocyte_postgres-0.12.0/scripts/migrate-all-tenants.sh +219 -0
  20. astrocyte_postgres-0.12.0/scripts/migrate.sh +86 -0
  21. astrocyte_postgres-0.12.0/scripts/test-integration.sh +70 -0
  22. astrocyte_postgres-0.12.0/tests/__init__.py +0 -0
  23. astrocyte_postgres-0.12.0/tests/conftest.py +68 -0
  24. astrocyte_postgres-0.12.0/tests/test_postgres_fts.py +219 -0
  25. astrocyte_postgres-0.12.0/tests/test_postgres_store.py +321 -0
  26. astrocyte_postgres-0.12.0/tests/test_postgres_tenant_isolation.py +170 -0
  27. astrocyte_postgres-0.12.0/tests/test_postgres_wiki_store.py +79 -0
  28. astrocyte_postgres-0.12.0/uv.lock +524 -0
@@ -0,0 +1,54 @@
1
+ # Node (Starlight in docs/)
2
+ node_modules/
3
+ .pnpm-store/
4
+ docs/.astro/
5
+
6
+ # Generated by docs/scripts/fetch-pypi-pins.mjs (recreated on every pnpm dev/build)
7
+ docs/src/data/pypi-install-pins.json
8
+
9
+ # Generated by docs/scripts/sync-docs.mjs (recreated on every pnpm dev/build)
10
+ docs/src/content/docs/introduction.md
11
+ docs/src/content/docs/design/
12
+ docs/src/content/docs/plugins/
13
+ docs/src/content/docs/end-user/
14
+ docs/src/content/docs/tutorials/
15
+
16
+ # Claude Code
17
+ .claude/
18
+
19
+ # OS
20
+ .DS_Store
21
+
22
+ # Local reference material (not tracked)
23
+ references/
24
+
25
+ # nWave feature artifacts (per-feature discover/deliver, roadmaps, execution logs).
26
+ # Intended to be a separate private worktree or nested git repo; not part of public Astrocyte.
27
+ docs/feature/
28
+
29
+ # Python
30
+ __pycache__/
31
+ *.py[cod]
32
+ *$py.class
33
+ .venv/
34
+ venv/
35
+ .env
36
+ .env.*
37
+ !.env.example
38
+ *.egg-info/
39
+ .eggs/
40
+ dist/
41
+ build/
42
+ .pytest_cache/
43
+ .mypy_cache/
44
+ .ruff_cache/
45
+ .coverage
46
+ htmlcov/
47
+ benchmark-results/
48
+ datasets/
49
+
50
+ # Rust
51
+ target/
52
+
53
+ # IDE (optional)
54
+ .idea/
@@ -0,0 +1,133 @@
1
+ Metadata-Version: 2.4
2
+ Name: astrocyte-postgres
3
+ Version: 0.12.0
4
+ Summary: PostgreSQL adapter for Astrocyte (vector + document + wiki stores backed by pgvector and tsvector)
5
+ License-Expression: Apache-2.0
6
+ Requires-Python: >=3.11
7
+ Requires-Dist: astrocyte<2,>=0.7.0
8
+ Requires-Dist: pgvector>=0.4
9
+ Requires-Dist: psycopg-pool>=3.2
10
+ Requires-Dist: psycopg[binary]>=3.1
11
+ Provides-Extra: dev
12
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
13
+ Requires-Dist: pytest-timeout>=2.2; extra == 'dev'
14
+ Requires-Dist: pytest>=8.0; extra == 'dev'
15
+ Description-Content-Type: text/markdown
16
+
17
+ # astrocyte-postgres
18
+
19
+ **PostgreSQL + [pgvector](https://github.com/pgvector/pgvector)** implementation of the Astrocyte **`VectorStore`** and **`WikiStore`** SPIs ([`provider-spi.md`](../../docs/_plugins/provider-spi.md)).
20
+
21
+ ## Install
22
+
23
+ From the monorepo (with `astrocyte` available):
24
+
25
+ ```bash
26
+ cd adapters-storage-py/astrocyte-postgres
27
+ uv sync
28
+ # or: pip install -e ../../astrocyte-py && pip install -e .
29
+ ```
30
+
31
+ Entry point names:
32
+
33
+ - **`postgres`** (group `astrocyte.vector_stores`) for raw/compiled memory vectors.
34
+ - **`postgres`** (group `astrocyte.document_stores`) for BM25 keyword retrieval over the same table.
35
+ - **`postgres`** (group `astrocyte.wiki_stores`) for durable wiki pages/revisions/provenance.
36
+
37
+ ## PostgreSQL with Docker
38
+
39
+ Use the **combined** Compose stack in **[`../../astrocyte-services-py/docker-compose.yml`](../../astrocyte-services-py/docker-compose.yml)** to run **Postgres (pgvector) + the reference REST service** together:
40
+
41
+ ```bash
42
+ cd astrocyte-services-py
43
+ docker compose up -d
44
+ ```
45
+
46
+ For **Postgres only** (no HTTP), start only `postgres`:
47
+
48
+ ```bash
49
+ cd astrocyte-services-py
50
+ docker compose up -d postgres
51
+ ```
52
+
53
+ Default DSN from your host (port **5433** maps to Postgres in the compose file):
54
+
55
+ ```text
56
+ postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
57
+ ```
58
+
59
+ ## Schema migrations (production)
60
+
61
+ DDL is shipped as **plain SQL** under [`migrations/`](migrations/) and applied with **`psql`** via [`scripts/migrate.sh`](scripts/migrate.sh) (no Python migration framework).
62
+
63
+ ```bash
64
+ export DATABASE_URL='postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte'
65
+ cd adapters-storage-py/astrocyte-postgres
66
+ ./scripts/migrate.sh
67
+ ```
68
+
69
+ Requirements: **PostgreSQL 15+** (for `CREATE INDEX CONCURRENTLY IF NOT EXISTS`), **psql** on `PATH`.
70
+
71
+ After migrations are applied, set **`bootstrap_schema: false`** in `vector_store_config` so the app does not run `CREATE TABLE` / indexes at runtime (see configuration table below). For a **single command** that starts Postgres, runs migrations, then starts the stack with runbook config, use **[`runbook-up.sh`](../../astrocyte-services-py/scripts/runbook-up.sh)** (see **[Runbook](../../astrocyte-services-py/README.md#runbook)**).
72
+
73
+ **Embedding width:** [`migrations/002_astrocyte_vectors.sql`](migrations/002_astrocyte_vectors.sql) creates `vector(${ASTROCYTE_EMBEDDING_DIMENSIONS:-128})`. That must match **`embedding_dimensions`** in config. For OpenAI `text-embedding-3-small`, run migrations with `ASTROCYTE_EMBEDDING_DIMENSIONS=1536`.
74
+
75
+ **Custom `table_name`:** The shipped SQL targets **`astrocyte_vectors`**. If you use another table name, copy and adjust the migration files accordingly.
76
+
77
+ The later migrations add the Hindsight-comparable Postgres substrate around vectors: bank metadata and access grants, lifecycle columns (`retained_at`, `forgotten_at`), durable wiki pages/revisions/provenance, canonical entity/link tables, and normalized temporal facts.
78
+
79
+ ## Configuration
80
+
81
+ | Constructor / YAML `vector_store_config` | Meaning |
82
+ |--------------------------------------------|---------|
83
+ | `dsn` | PostgreSQL connection URI (or set `DATABASE_URL` / `ASTROCYTE_PG_DSN`) |
84
+ | `table_name` | Table name (default `astrocyte_vectors`; alphanumeric + underscore only) |
85
+ | `embedding_dimensions` | Fixed `vector(N)` width; must match your embedding model and the **`vector(N)`** in SQL migrations (default **128**) |
86
+ | `bootstrap_schema` | If **`true`** (default), create extension / table / btree index on first use (dev-friendly; no HNSW). If **`false`**, assume **`migrate.sh`** already applied [`migrations/`](migrations/) (production). |
87
+
88
+ ## How this fits `astrocyte_gateway`
89
+
90
+ 1. **`astrocyte-py`** defines the **`VectorStore`** protocol and discovers adapters by **entry point** (`astrocyte.vector_stores`).
91
+ 2. **`astrocyte-postgres`** registers **`postgres` → `PostgresStore`**. Installing this package makes the name **`postgres`** available to **`resolve_provider()`**.
92
+ 3. **`astrocyte_gateway/wiring.py`** calls **`resolve_vector_store(config)`**, which loads the class from the entry point and passes **`vector_store_config`** from YAML (or env-only defaults).
93
+ 4. **`astrocyte_gateway/brain.py`** builds **`Astrocyte`** + **`PipelineOrchestrator`** with that store and your chosen **`llm_provider`** (still **`mock`** unless you configure a real LLM).
94
+
95
+ Example **`ASTROCYTE_CONFIG_PATH`** snippet:
96
+
97
+ ```yaml
98
+ provider_tier: storage
99
+ vector_store: postgres
100
+ llm_provider: mock
101
+ vector_store_config:
102
+ dsn: postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
103
+ embedding_dimensions: 128
104
+ bootstrap_schema: false
105
+ wiki_store: postgres
106
+ wiki_store_config:
107
+ dsn: postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
108
+ bootstrap_schema: false
109
+ ```
110
+
111
+ Then run the REST service (from repo layout):
112
+
113
+ ```bash
114
+ export ASTROCYTE_CONFIG_PATH=/path/to/that.yaml
115
+ cd astrocyte-services-py/astrocyte-gateway-py && uv run astrocyte-gateway-py
116
+ ```
117
+
118
+ Or set only env (no YAML file):
119
+
120
+ ```bash
121
+ export ASTROCYTE_VECTOR_STORE=postgres
122
+ export DATABASE_URL=postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
123
+ # embedding_dimensions default 128 — override via YAML if you add a file
124
+ cd astrocyte-services-py/astrocyte-gateway-py && uv sync --extra postgres
125
+ ```
126
+
127
+ **Note:** `vector_store_config` for dimensions is only merged from YAML today; for env-only mode, add a small YAML or extend `brain.py` to pass `ASTROCYTE_EMBEDDING_DIMENSIONS` (future improvement).
128
+
129
+ ## Production notes
130
+
131
+ - **HNSW** parameters (`m`, `ef_construction`) live in [`migrations/003_indexes.sql`](migrations/003_indexes.sql); tune with DBA guidance as load grows.
132
+ - **Embedding dimension** must match the **`LLMProvider.embed()`** output used by the pipeline.
133
+ - Use **secrets** for `dsn`, not committed YAML.
@@ -0,0 +1,117 @@
1
+ # astrocyte-postgres
2
+
3
+ **PostgreSQL + [pgvector](https://github.com/pgvector/pgvector)** implementation of the Astrocyte **`VectorStore`** and **`WikiStore`** SPIs ([`provider-spi.md`](../../docs/_plugins/provider-spi.md)).
4
+
5
+ ## Install
6
+
7
+ From the monorepo (with `astrocyte` available):
8
+
9
+ ```bash
10
+ cd adapters-storage-py/astrocyte-postgres
11
+ uv sync
12
+ # or: pip install -e ../../astrocyte-py && pip install -e .
13
+ ```
14
+
15
+ Entry point names:
16
+
17
+ - **`postgres`** (group `astrocyte.vector_stores`) for raw/compiled memory vectors.
18
+ - **`postgres`** (group `astrocyte.document_stores`) for BM25 keyword retrieval over the same table.
19
+ - **`postgres`** (group `astrocyte.wiki_stores`) for durable wiki pages/revisions/provenance.
20
+
21
+ ## PostgreSQL with Docker
22
+
23
+ Use the **combined** Compose stack in **[`../../astrocyte-services-py/docker-compose.yml`](../../astrocyte-services-py/docker-compose.yml)** to run **Postgres (pgvector) + the reference REST service** together:
24
+
25
+ ```bash
26
+ cd astrocyte-services-py
27
+ docker compose up -d
28
+ ```
29
+
30
+ For **Postgres only** (no HTTP), start only `postgres`:
31
+
32
+ ```bash
33
+ cd astrocyte-services-py
34
+ docker compose up -d postgres
35
+ ```
36
+
37
+ Default DSN from your host (port **5433** maps to Postgres in the compose file):
38
+
39
+ ```text
40
+ postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
41
+ ```
42
+
43
+ ## Schema migrations (production)
44
+
45
+ DDL is shipped as **plain SQL** under [`migrations/`](migrations/) and applied with **`psql`** via [`scripts/migrate.sh`](scripts/migrate.sh) (no Python migration framework).
46
+
47
+ ```bash
48
+ export DATABASE_URL='postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte'
49
+ cd adapters-storage-py/astrocyte-postgres
50
+ ./scripts/migrate.sh
51
+ ```
52
+
53
+ Requirements: **PostgreSQL 15+** (for `CREATE INDEX CONCURRENTLY IF NOT EXISTS`), **psql** on `PATH`.
54
+
55
+ After migrations are applied, set **`bootstrap_schema: false`** in `vector_store_config` so the app does not run `CREATE TABLE` / indexes at runtime (see configuration table below). For a **single command** that starts Postgres, runs migrations, then starts the stack with runbook config, use **[`runbook-up.sh`](../../astrocyte-services-py/scripts/runbook-up.sh)** (see **[Runbook](../../astrocyte-services-py/README.md#runbook)**).
56
+
57
+ **Embedding width:** [`migrations/002_astrocyte_vectors.sql`](migrations/002_astrocyte_vectors.sql) creates `vector(${ASTROCYTE_EMBEDDING_DIMENSIONS:-128})`. That must match **`embedding_dimensions`** in config. For OpenAI `text-embedding-3-small`, run migrations with `ASTROCYTE_EMBEDDING_DIMENSIONS=1536`.
58
+
59
+ **Custom `table_name`:** The shipped SQL targets **`astrocyte_vectors`**. If you use another table name, copy and adjust the migration files accordingly.
60
+
61
+ The later migrations add the Hindsight-comparable Postgres substrate around vectors: bank metadata and access grants, lifecycle columns (`retained_at`, `forgotten_at`), durable wiki pages/revisions/provenance, canonical entity/link tables, and normalized temporal facts.
62
+
63
+ ## Configuration
64
+
65
+ | Constructor / YAML `vector_store_config` | Meaning |
66
+ |--------------------------------------------|---------|
67
+ | `dsn` | PostgreSQL connection URI (or set `DATABASE_URL` / `ASTROCYTE_PG_DSN`) |
68
+ | `table_name` | Table name (default `astrocyte_vectors`; alphanumeric + underscore only) |
69
+ | `embedding_dimensions` | Fixed `vector(N)` width; must match your embedding model and the **`vector(N)`** in SQL migrations (default **128**) |
70
+ | `bootstrap_schema` | If **`true`** (default), create extension / table / btree index on first use (dev-friendly; no HNSW). If **`false`**, assume **`migrate.sh`** already applied [`migrations/`](migrations/) (production). |
71
+
72
+ ## How this fits `astrocyte_gateway`
73
+
74
+ 1. **`astrocyte-py`** defines the **`VectorStore`** protocol and discovers adapters by **entry point** (`astrocyte.vector_stores`).
75
+ 2. **`astrocyte-postgres`** registers **`postgres` → `PostgresStore`**. Installing this package makes the name **`postgres`** available to **`resolve_provider()`**.
76
+ 3. **`astrocyte_gateway/wiring.py`** calls **`resolve_vector_store(config)`**, which loads the class from the entry point and passes **`vector_store_config`** from YAML (or env-only defaults).
77
+ 4. **`astrocyte_gateway/brain.py`** builds **`Astrocyte`** + **`PipelineOrchestrator`** with that store and your chosen **`llm_provider`** (still **`mock`** unless you configure a real LLM).
78
+
79
+ Example **`ASTROCYTE_CONFIG_PATH`** snippet:
80
+
81
+ ```yaml
82
+ provider_tier: storage
83
+ vector_store: postgres
84
+ llm_provider: mock
85
+ vector_store_config:
86
+ dsn: postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
87
+ embedding_dimensions: 128
88
+ bootstrap_schema: false
89
+ wiki_store: postgres
90
+ wiki_store_config:
91
+ dsn: postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
92
+ bootstrap_schema: false
93
+ ```
94
+
95
+ Then run the REST service (from repo layout):
96
+
97
+ ```bash
98
+ export ASTROCYTE_CONFIG_PATH=/path/to/that.yaml
99
+ cd astrocyte-services-py/astrocyte-gateway-py && uv run astrocyte-gateway-py
100
+ ```
101
+
102
+ Or set only env (no YAML file):
103
+
104
+ ```bash
105
+ export ASTROCYTE_VECTOR_STORE=postgres
106
+ export DATABASE_URL=postgresql://astrocyte:astrocyte@127.0.0.1:5433/astrocyte
107
+ # embedding_dimensions default 128 — override via YAML if you add a file
108
+ cd astrocyte-services-py/astrocyte-gateway-py && uv sync --extra postgres
109
+ ```
110
+
111
+ **Note:** `vector_store_config` for dimensions is only merged from YAML today; for env-only mode, add a small YAML or extend `brain.py` to pass `ASTROCYTE_EMBEDDING_DIMENSIONS` (future improvement).
112
+
113
+ ## Production notes
114
+
115
+ - **HNSW** parameters (`m`, `ef_construction`) live in [`migrations/003_indexes.sql`](migrations/003_indexes.sql); tune with DBA guidance as load grows.
116
+ - **Embedding dimension** must match the **`LLMProvider.embed()`** output used by the pipeline.
117
+ - Use **secrets** for `dsn`, not committed YAML.
@@ -0,0 +1,11 @@
1
+ """PostgreSQL adapter for Astrocyte Tier 1.
2
+
3
+ Backed by pgvector (HNSW vector index) and tsvector (BM25 keyword search).
4
+ Provides VectorStore, DocumentStore, and WikiStore implementations against
5
+ a single Postgres database.
6
+ """
7
+
8
+ from astrocyte_postgres.store import PostgresStore
9
+ from astrocyte_postgres.wiki_store import PostgresWikiStore
10
+
11
+ __all__ = ["PostgresStore", "PostgresWikiStore"]