hindsight-api 0.2.1__py3-none-any.whl → 0.4.0__py3-none-any.whl

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 (88) hide show
  1. hindsight_api/admin/__init__.py +1 -0
  2. hindsight_api/admin/cli.py +311 -0
  3. hindsight_api/alembic/versions/f1a2b3c4d5e6_add_memory_links_composite_index.py +44 -0
  4. hindsight_api/alembic/versions/g2a3b4c5d6e7_add_tags_column.py +48 -0
  5. hindsight_api/alembic/versions/h3c4d5e6f7g8_mental_models_v4.py +112 -0
  6. hindsight_api/alembic/versions/i4d5e6f7g8h9_delete_opinions.py +41 -0
  7. hindsight_api/alembic/versions/j5e6f7g8h9i0_mental_model_versions.py +95 -0
  8. hindsight_api/alembic/versions/k6f7g8h9i0j1_add_directive_subtype.py +58 -0
  9. hindsight_api/alembic/versions/l7g8h9i0j1k2_add_worker_columns.py +109 -0
  10. hindsight_api/alembic/versions/m8h9i0j1k2l3_mental_model_id_to_text.py +41 -0
  11. hindsight_api/alembic/versions/n9i0j1k2l3m4_learnings_and_pinned_reflections.py +134 -0
  12. hindsight_api/alembic/versions/o0j1k2l3m4n5_migrate_mental_models_data.py +113 -0
  13. hindsight_api/alembic/versions/p1k2l3m4n5o6_new_knowledge_architecture.py +194 -0
  14. hindsight_api/alembic/versions/q2l3m4n5o6p7_fix_mental_model_fact_type.py +50 -0
  15. hindsight_api/alembic/versions/r3m4n5o6p7q8_add_reflect_response_to_reflections.py +47 -0
  16. hindsight_api/alembic/versions/s4n5o6p7q8r9_add_consolidated_at_to_memory_units.py +53 -0
  17. hindsight_api/alembic/versions/t5o6p7q8r9s0_rename_mental_models_to_observations.py +134 -0
  18. hindsight_api/alembic/versions/u6p7q8r9s0t1_mental_models_text_id.py +41 -0
  19. hindsight_api/alembic/versions/v7q8r9s0t1u2_add_max_tokens_to_mental_models.py +50 -0
  20. hindsight_api/api/http.py +1406 -118
  21. hindsight_api/api/mcp.py +11 -196
  22. hindsight_api/config.py +359 -27
  23. hindsight_api/engine/consolidation/__init__.py +5 -0
  24. hindsight_api/engine/consolidation/consolidator.py +859 -0
  25. hindsight_api/engine/consolidation/prompts.py +69 -0
  26. hindsight_api/engine/cross_encoder.py +706 -88
  27. hindsight_api/engine/db_budget.py +284 -0
  28. hindsight_api/engine/db_utils.py +11 -0
  29. hindsight_api/engine/directives/__init__.py +5 -0
  30. hindsight_api/engine/directives/models.py +37 -0
  31. hindsight_api/engine/embeddings.py +553 -29
  32. hindsight_api/engine/entity_resolver.py +8 -5
  33. hindsight_api/engine/interface.py +40 -17
  34. hindsight_api/engine/llm_wrapper.py +744 -68
  35. hindsight_api/engine/memory_engine.py +2505 -1017
  36. hindsight_api/engine/mental_models/__init__.py +14 -0
  37. hindsight_api/engine/mental_models/models.py +53 -0
  38. hindsight_api/engine/query_analyzer.py +4 -3
  39. hindsight_api/engine/reflect/__init__.py +18 -0
  40. hindsight_api/engine/reflect/agent.py +933 -0
  41. hindsight_api/engine/reflect/models.py +109 -0
  42. hindsight_api/engine/reflect/observations.py +186 -0
  43. hindsight_api/engine/reflect/prompts.py +483 -0
  44. hindsight_api/engine/reflect/tools.py +437 -0
  45. hindsight_api/engine/reflect/tools_schema.py +250 -0
  46. hindsight_api/engine/response_models.py +168 -4
  47. hindsight_api/engine/retain/bank_utils.py +79 -201
  48. hindsight_api/engine/retain/fact_extraction.py +424 -195
  49. hindsight_api/engine/retain/fact_storage.py +35 -12
  50. hindsight_api/engine/retain/link_utils.py +29 -24
  51. hindsight_api/engine/retain/orchestrator.py +24 -43
  52. hindsight_api/engine/retain/types.py +11 -2
  53. hindsight_api/engine/search/graph_retrieval.py +43 -14
  54. hindsight_api/engine/search/link_expansion_retrieval.py +391 -0
  55. hindsight_api/engine/search/mpfp_retrieval.py +362 -117
  56. hindsight_api/engine/search/reranking.py +2 -2
  57. hindsight_api/engine/search/retrieval.py +848 -201
  58. hindsight_api/engine/search/tags.py +172 -0
  59. hindsight_api/engine/search/think_utils.py +42 -141
  60. hindsight_api/engine/search/trace.py +12 -1
  61. hindsight_api/engine/search/tracer.py +26 -6
  62. hindsight_api/engine/search/types.py +21 -3
  63. hindsight_api/engine/task_backend.py +113 -106
  64. hindsight_api/engine/utils.py +1 -152
  65. hindsight_api/extensions/__init__.py +10 -1
  66. hindsight_api/extensions/builtin/tenant.py +5 -1
  67. hindsight_api/extensions/context.py +10 -1
  68. hindsight_api/extensions/operation_validator.py +81 -4
  69. hindsight_api/extensions/tenant.py +26 -0
  70. hindsight_api/main.py +69 -6
  71. hindsight_api/mcp_local.py +12 -53
  72. hindsight_api/mcp_tools.py +494 -0
  73. hindsight_api/metrics.py +433 -48
  74. hindsight_api/migrations.py +141 -1
  75. hindsight_api/models.py +3 -3
  76. hindsight_api/pg0.py +53 -0
  77. hindsight_api/server.py +39 -2
  78. hindsight_api/worker/__init__.py +11 -0
  79. hindsight_api/worker/main.py +296 -0
  80. hindsight_api/worker/poller.py +486 -0
  81. {hindsight_api-0.2.1.dist-info → hindsight_api-0.4.0.dist-info}/METADATA +16 -6
  82. hindsight_api-0.4.0.dist-info/RECORD +112 -0
  83. {hindsight_api-0.2.1.dist-info → hindsight_api-0.4.0.dist-info}/entry_points.txt +2 -0
  84. hindsight_api/engine/retain/observation_regeneration.py +0 -254
  85. hindsight_api/engine/search/observation_utils.py +0 -125
  86. hindsight_api/engine/search/scoring.py +0 -159
  87. hindsight_api-0.2.1.dist-info/RECORD +0 -75
  88. {hindsight_api-0.2.1.dist-info → hindsight_api-0.4.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1 @@
1
+ # Admin CLI for Hindsight
@@ -0,0 +1,311 @@
1
+ """
2
+ Hindsight Admin CLI - backup and restore operations.
3
+ """
4
+
5
+ import asyncio
6
+ import io
7
+ import json
8
+ import logging
9
+ import zipfile
10
+ from datetime import datetime, timezone
11
+ from pathlib import Path
12
+ from typing import Any
13
+
14
+ import asyncpg
15
+ import typer
16
+
17
+ from ..config import HindsightConfig
18
+ from ..pg0 import parse_pg0_url, resolve_database_url
19
+
20
+
21
+ def _fq_table(table: str, schema: str) -> str:
22
+ """Get fully-qualified table name with schema prefix."""
23
+ return f"{schema}.{table}"
24
+
25
+
26
+ # Setup logging
27
+ logging.basicConfig(
28
+ level=logging.INFO,
29
+ format="%(message)s",
30
+ )
31
+ logger = logging.getLogger(__name__)
32
+
33
+ app = typer.Typer(name="hindsight-admin", help="Hindsight administrative commands")
34
+
35
+ # Tables to backup/restore in dependency order
36
+ # Import must happen in this order due to foreign key constraints
37
+ BACKUP_TABLES = [
38
+ "banks",
39
+ "documents",
40
+ "entities",
41
+ "chunks",
42
+ "memory_units",
43
+ "unit_entities",
44
+ "entity_cooccurrences",
45
+ "memory_links",
46
+ ]
47
+
48
+ MANIFEST_VERSION = "1"
49
+
50
+
51
+ async def _backup(database_url: str, output_path: Path, schema: str = "public") -> dict[str, Any]:
52
+ """Backup all tables to a zip file using binary COPY protocol."""
53
+ conn = await asyncpg.connect(database_url)
54
+ try:
55
+ tables: dict[str, Any] = {}
56
+ manifest: dict[str, Any] = {
57
+ "version": MANIFEST_VERSION,
58
+ "created_at": datetime.now(timezone.utc).isoformat(),
59
+ "schema": schema,
60
+ "tables": tables,
61
+ }
62
+
63
+ # Use a transaction with REPEATABLE READ isolation to get a consistent
64
+ # snapshot across all tables. This prevents race conditions where
65
+ # entity_cooccurrences could reference entities created after the
66
+ # entities table was backed up.
67
+ async with conn.transaction(isolation="repeatable_read"):
68
+ with zipfile.ZipFile(output_path, "w", zipfile.ZIP_DEFLATED) as zf:
69
+ for i, table in enumerate(BACKUP_TABLES, 1):
70
+ typer.echo(f" [{i}/{len(BACKUP_TABLES)}] Backing up {table}...", nl=False)
71
+
72
+ buffer = io.BytesIO()
73
+
74
+ # Use binary COPY for exact type preservation
75
+ # asyncpg requires schema_name as separate parameter
76
+ await conn.copy_from_table(table, schema_name=schema, output=buffer, format="binary")
77
+
78
+ data = buffer.getvalue()
79
+ zf.writestr(f"{table}.bin", data)
80
+
81
+ # Get row count for manifest
82
+ qualified_table = _fq_table(table, schema)
83
+ row_count = await conn.fetchval(f"SELECT COUNT(*) FROM {qualified_table}")
84
+ tables[table] = {
85
+ "rows": row_count,
86
+ "size_bytes": len(data),
87
+ }
88
+
89
+ typer.echo(f" {row_count} rows")
90
+
91
+ zf.writestr("manifest.json", json.dumps(manifest, indent=2))
92
+
93
+ return manifest
94
+ finally:
95
+ await conn.close()
96
+
97
+
98
+ async def _restore(database_url: str, input_path: Path, schema: str = "public") -> dict[str, Any]:
99
+ """Restore all tables from a zip file using binary COPY protocol."""
100
+ conn = await asyncpg.connect(database_url)
101
+ try:
102
+ with zipfile.ZipFile(input_path, "r") as zf:
103
+ # Read and validate manifest
104
+ manifest: dict[str, Any] = json.loads(zf.read("manifest.json"))
105
+ if manifest.get("version") != MANIFEST_VERSION:
106
+ raise ValueError(f"Unsupported backup version: {manifest.get('version')}")
107
+
108
+ # Use a transaction for atomic restore - either all tables are
109
+ # restored or none are, preventing partial/inconsistent state.
110
+ async with conn.transaction():
111
+ typer.echo(" Clearing existing data...")
112
+ # Truncate tables in reverse order (respects FK constraints)
113
+ for table in reversed(BACKUP_TABLES):
114
+ qualified_table = _fq_table(table, schema)
115
+ await conn.execute(f"TRUNCATE TABLE {qualified_table} CASCADE")
116
+
117
+ # Restore tables in forward order
118
+ for i, table in enumerate(BACKUP_TABLES, 1):
119
+ filename = f"{table}.bin"
120
+ if filename not in zf.namelist():
121
+ typer.echo(f" [{i}/{len(BACKUP_TABLES)}] {table}: skipped (not in backup)")
122
+ continue
123
+
124
+ expected_rows = manifest["tables"].get(table, {}).get("rows", "?")
125
+ typer.echo(f" [{i}/{len(BACKUP_TABLES)}] Restoring {table}... {expected_rows} rows")
126
+
127
+ data = zf.read(filename)
128
+ buffer = io.BytesIO(data)
129
+ # asyncpg requires schema_name as separate parameter
130
+ await conn.copy_to_table(table, schema_name=schema, source=buffer, format="binary")
131
+
132
+ # Refresh materialized view
133
+ typer.echo(" Refreshing materialized views...")
134
+ await conn.execute(f"REFRESH MATERIALIZED VIEW {_fq_table('memory_units_bm25', schema)}")
135
+
136
+ return manifest
137
+ finally:
138
+ await conn.close()
139
+
140
+
141
+ async def _run_backup(db_url: str, output: Path, schema: str = "public") -> dict[str, Any]:
142
+ """Resolve database URL and run backup."""
143
+ is_pg0, instance_name, _ = parse_pg0_url(db_url)
144
+ if is_pg0:
145
+ typer.echo(f"Starting embedded PostgreSQL (instance: {instance_name})...")
146
+ resolved_url = await resolve_database_url(db_url)
147
+ return await _backup(resolved_url, output, schema)
148
+
149
+
150
+ async def _run_restore(db_url: str, input_file: Path, schema: str = "public") -> dict[str, Any]:
151
+ """Resolve database URL and run restore."""
152
+ is_pg0, instance_name, _ = parse_pg0_url(db_url)
153
+ if is_pg0:
154
+ typer.echo(f"Starting embedded PostgreSQL (instance: {instance_name})...")
155
+ resolved_url = await resolve_database_url(db_url)
156
+ return await _restore(resolved_url, input_file, schema)
157
+
158
+
159
+ @app.command()
160
+ def backup(
161
+ output: Path = typer.Argument(..., help="Output file path (.zip)"),
162
+ schema: str = typer.Option("public", "--schema", "-s", help="Database schema to backup"),
163
+ ):
164
+ """Backup the Hindsight database to a zip file."""
165
+ config = HindsightConfig.from_env()
166
+
167
+ if not config.database_url:
168
+ typer.echo("Error: Database URL not configured.", err=True)
169
+ typer.echo("Set HINDSIGHT_API_DATABASE_URL environment variable.", err=True)
170
+ raise typer.Exit(1)
171
+
172
+ if output.suffix != ".zip":
173
+ output = output.with_suffix(".zip")
174
+
175
+ typer.echo(f"Backing up database (schema: {schema}) to {output}...")
176
+
177
+ manifest = asyncio.run(_run_backup(config.database_url, output, schema))
178
+
179
+ total_rows = sum(t["rows"] for t in manifest["tables"].values())
180
+ typer.echo(f"Backed up {total_rows} rows across {len(BACKUP_TABLES)} tables")
181
+ typer.echo(f"Backup saved to {output}")
182
+
183
+
184
+ @app.command()
185
+ def restore(
186
+ input_file: Path = typer.Argument(..., help="Input backup file (.zip)"),
187
+ schema: str = typer.Option("public", "--schema", "-s", help="Database schema to restore to"),
188
+ yes: bool = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt"),
189
+ ):
190
+ """Restore the database from a backup file. WARNING: This deletes all existing data."""
191
+ config = HindsightConfig.from_env()
192
+
193
+ if not config.database_url:
194
+ typer.echo("Error: Database URL not configured.", err=True)
195
+ typer.echo("Set HINDSIGHT_API_DATABASE_URL environment variable.", err=True)
196
+ raise typer.Exit(1)
197
+
198
+ if not input_file.exists():
199
+ typer.echo(f"Error: File not found: {input_file}", err=True)
200
+ raise typer.Exit(1)
201
+
202
+ if not yes:
203
+ typer.confirm(
204
+ "This will DELETE all existing data and replace it with the backup. Continue?",
205
+ abort=True,
206
+ )
207
+
208
+ typer.echo(f"Restoring database (schema: {schema}) from {input_file}...")
209
+
210
+ manifest = asyncio.run(_run_restore(config.database_url, input_file, schema))
211
+
212
+ total_rows = sum(t["rows"] for t in manifest["tables"].values())
213
+ typer.echo(f"Restored {total_rows} rows across {len(BACKUP_TABLES)} tables")
214
+ typer.echo("Restore complete")
215
+
216
+
217
+ async def _run_migration(db_url: str, schema: str = "public") -> None:
218
+ """Resolve database URL and run migrations."""
219
+ from ..migrations import run_migrations
220
+
221
+ is_pg0, instance_name, _ = parse_pg0_url(db_url)
222
+ if is_pg0:
223
+ typer.echo(f"Starting embedded PostgreSQL (instance: {instance_name})...")
224
+ resolved_url = await resolve_database_url(db_url)
225
+ run_migrations(resolved_url, schema=schema)
226
+
227
+
228
+ @app.command(name="run-db-migration")
229
+ def run_db_migration(
230
+ schema: str = typer.Option("public", "--schema", "-s", help="Database schema to run migrations on"),
231
+ ):
232
+ """Run database migrations to the latest version."""
233
+ config = HindsightConfig.from_env()
234
+
235
+ if not config.database_url:
236
+ typer.echo("Error: Database URL not configured.", err=True)
237
+ typer.echo("Set HINDSIGHT_API_DATABASE_URL environment variable.", err=True)
238
+ raise typer.Exit(1)
239
+
240
+ typer.echo(f"Running database migrations (schema: {schema})...")
241
+
242
+ asyncio.run(_run_migration(config.database_url, schema))
243
+
244
+ typer.echo("Database migrations completed successfully")
245
+
246
+
247
+ async def _decommission_worker(db_url: str, worker_id: str, schema: str = "public") -> int:
248
+ """Release all tasks owned by a worker, setting them back to pending status."""
249
+ is_pg0, instance_name, _ = parse_pg0_url(db_url)
250
+ if is_pg0:
251
+ typer.echo(f"Starting embedded PostgreSQL (instance: {instance_name})...")
252
+ resolved_url = await resolve_database_url(db_url)
253
+
254
+ conn = await asyncpg.connect(resolved_url)
255
+ try:
256
+ table = _fq_table("async_operations", schema)
257
+ result = await conn.fetch(
258
+ f"""
259
+ UPDATE {table}
260
+ SET status = 'pending', worker_id = NULL, claimed_at = NULL, updated_at = now()
261
+ WHERE worker_id = $1 AND status = 'processing'
262
+ RETURNING operation_id
263
+ """,
264
+ worker_id,
265
+ )
266
+ return len(result)
267
+ finally:
268
+ await conn.close()
269
+
270
+
271
+ @app.command(name="decommission-worker")
272
+ def decommission_worker(
273
+ worker_id: str = typer.Argument(..., help="Worker ID to decommission"),
274
+ schema: str = typer.Option("public", "--schema", "-s", help="Database schema"),
275
+ yes: bool = typer.Option(False, "--yes", "-y", help="Skip confirmation prompt"),
276
+ ):
277
+ """Release all tasks owned by a worker (sets status back to pending).
278
+
279
+ Use this command when a worker has crashed or been removed without graceful shutdown.
280
+ All tasks that were being processed by the worker will be released back to the queue
281
+ so other workers can pick them up.
282
+ """
283
+ config = HindsightConfig.from_env()
284
+
285
+ if not config.database_url:
286
+ typer.echo("Error: Database URL not configured.", err=True)
287
+ typer.echo("Set HINDSIGHT_API_DATABASE_URL environment variable.", err=True)
288
+ raise typer.Exit(1)
289
+
290
+ if not yes:
291
+ typer.confirm(
292
+ f"This will release all tasks owned by worker '{worker_id}' back to pending. Continue?",
293
+ abort=True,
294
+ )
295
+
296
+ typer.echo(f"Decommissioning worker '{worker_id}' (schema: {schema})...")
297
+
298
+ count = asyncio.run(_decommission_worker(config.database_url, worker_id, schema))
299
+
300
+ if count > 0:
301
+ typer.echo(f"Released {count} task(s) from worker '{worker_id}'")
302
+ else:
303
+ typer.echo(f"No tasks found for worker '{worker_id}'")
304
+
305
+
306
+ def main():
307
+ app()
308
+
309
+
310
+ if __name__ == "__main__":
311
+ main()
@@ -0,0 +1,44 @@
1
+ """add_memory_links_from_type_weight_index
2
+
3
+ Revision ID: f1a2b3c4d5e6
4
+ Revises: e0a1b2c3d4e5
5
+ Create Date: 2025-01-12
6
+
7
+ Add composite index on memory_links (from_unit_id, link_type, weight DESC)
8
+ to optimize MPFP graph traversal queries that need top-k edges per type.
9
+ """
10
+
11
+ from collections.abc import Sequence
12
+
13
+ from alembic import context, op
14
+
15
+ # revision identifiers, used by Alembic.
16
+ revision: str = "f1a2b3c4d5e6"
17
+ down_revision: str | Sequence[str] | None = "e0a1b2c3d4e5"
18
+ branch_labels: str | Sequence[str] | None = None
19
+ depends_on: str | Sequence[str] | None = None
20
+
21
+
22
+ def _get_schema_prefix() -> str:
23
+ """Get schema prefix for table names (e.g., 'tenant_x.' or '' for public)."""
24
+ schema = context.config.get_main_option("target_schema")
25
+ return f'"{schema}".' if schema else ""
26
+
27
+
28
+ def upgrade() -> None:
29
+ """Add composite index for efficient MPFP edge loading."""
30
+ schema = _get_schema_prefix()
31
+ # Create composite index for efficient top-k per (from_node, link_type) queries
32
+ # This enables LATERAL joins to use index-only scans with early termination
33
+ # Note: Not using CONCURRENTLY here as it requires running outside a transaction
34
+ # For production with large tables, consider running this manually with CONCURRENTLY
35
+ op.execute(
36
+ f"CREATE INDEX IF NOT EXISTS idx_memory_links_from_type_weight "
37
+ f"ON {schema}memory_links(from_unit_id, link_type, weight DESC)"
38
+ )
39
+
40
+
41
+ def downgrade() -> None:
42
+ """Remove the composite index."""
43
+ schema = _get_schema_prefix()
44
+ op.execute(f"DROP INDEX IF EXISTS {schema}idx_memory_links_from_type_weight")
@@ -0,0 +1,48 @@
1
+ """add_tags_column
2
+
3
+ Revision ID: g2a3b4c5d6e7
4
+ Revises: f1a2b3c4d5e6
5
+ Create Date: 2025-01-13
6
+
7
+ Add tags column to memory_units and documents tables for visibility scoping.
8
+ Tags enable filtering memories by scope (e.g., user IDs, session IDs) during recall/reflect.
9
+ """
10
+
11
+ from collections.abc import Sequence
12
+
13
+ from alembic import context, op
14
+
15
+ # revision identifiers, used by Alembic.
16
+ revision: str = "g2a3b4c5d6e7"
17
+ down_revision: str | Sequence[str] | None = "f1a2b3c4d5e6"
18
+ branch_labels: str | Sequence[str] | None = None
19
+ depends_on: str | Sequence[str] | None = None
20
+
21
+
22
+ def _get_schema_prefix() -> str:
23
+ """Get schema prefix for table names (e.g., 'tenant_x.' or '' for public)."""
24
+ schema = context.config.get_main_option("target_schema")
25
+ return f'"{schema}".' if schema else ""
26
+
27
+
28
+ def upgrade() -> None:
29
+ """Add tags column to memory_units and documents tables."""
30
+ schema = _get_schema_prefix()
31
+
32
+ # Add tags column to memory_units table
33
+ op.execute(f"ALTER TABLE {schema}memory_units ADD COLUMN IF NOT EXISTS tags VARCHAR[] NOT NULL DEFAULT '{{}}'")
34
+
35
+ # Create GIN index for efficient array containment queries (tags && ARRAY['x'])
36
+ op.execute(f"CREATE INDEX IF NOT EXISTS idx_memory_units_tags ON {schema}memory_units USING GIN (tags)")
37
+
38
+ # Add tags column to documents table for document-level tags
39
+ op.execute(f"ALTER TABLE {schema}documents ADD COLUMN IF NOT EXISTS tags VARCHAR[] NOT NULL DEFAULT '{{}}'")
40
+
41
+
42
+ def downgrade() -> None:
43
+ """Remove tags columns and index."""
44
+ schema = _get_schema_prefix()
45
+
46
+ op.execute(f"DROP INDEX IF EXISTS {schema}idx_memory_units_tags")
47
+ op.execute(f"ALTER TABLE {schema}memory_units DROP COLUMN IF EXISTS tags")
48
+ op.execute(f"ALTER TABLE {schema}documents DROP COLUMN IF EXISTS tags")
@@ -0,0 +1,112 @@
1
+ """mental_models_v4
2
+
3
+ Revision ID: h3c4d5e6f7g8
4
+ Revises: g2a3b4c5d6e7
5
+ Create Date: 2026-01-08 00:00:00.000000
6
+
7
+ This migration implements the v4 mental models system:
8
+ 1. Deletes existing observation memory_units (observations now in mental models)
9
+ 2. Adds mission column to banks (replacing background)
10
+ 3. Creates mental_models table with final schema
11
+
12
+ Mental models can reference entities when an entity is "promoted" to a mental model.
13
+ Summary content is stored as JSONB observations with per-observation fact attribution.
14
+ """
15
+
16
+ from collections.abc import Sequence
17
+
18
+ from alembic import context, op
19
+
20
+ # revision identifiers, used by Alembic.
21
+ revision: str = "h3c4d5e6f7g8"
22
+ down_revision: str | Sequence[str] | None = "g2a3b4c5d6e7"
23
+ branch_labels: str | Sequence[str] | None = None
24
+ depends_on: str | Sequence[str] | None = None
25
+
26
+
27
+ def _get_schema_prefix() -> str:
28
+ """Get schema prefix for table names (required for multi-tenant support)."""
29
+ schema = context.config.get_main_option("target_schema")
30
+ return f'"{schema}".' if schema else ""
31
+
32
+
33
+ def upgrade() -> None:
34
+ """Apply mental models v4 changes."""
35
+ schema = _get_schema_prefix()
36
+
37
+ # Step 1: Delete observation memory_units (cascades to unit_entities links)
38
+ # Observations are now handled through mental models, not memory_units
39
+ op.execute(f"DELETE FROM {schema}memory_units WHERE fact_type = 'observation'")
40
+
41
+ # Step 2: Drop observation-specific index (if it exists)
42
+ op.execute(f"DROP INDEX IF EXISTS {schema}idx_memory_units_observation_date")
43
+
44
+ # Step 3: Add mission column to banks (replacing background)
45
+ op.execute(f"ALTER TABLE {schema}banks ADD COLUMN IF NOT EXISTS mission TEXT")
46
+
47
+ # Migrate: copy background to mission if background column exists
48
+ # Use DO block to check column existence first (idempotent for re-runs)
49
+ schema_name = context.config.get_main_option("target_schema") or "public"
50
+ op.execute(f"""
51
+ DO $$
52
+ BEGIN
53
+ IF EXISTS (
54
+ SELECT 1 FROM information_schema.columns
55
+ WHERE table_schema = '{schema_name}' AND table_name = 'banks' AND column_name = 'background'
56
+ ) THEN
57
+ UPDATE {schema}banks
58
+ SET mission = background
59
+ WHERE mission IS NULL;
60
+ END IF;
61
+ END $$;
62
+ """)
63
+
64
+ # Remove background column (replaced by mission)
65
+ op.execute(f"ALTER TABLE {schema}banks DROP COLUMN IF EXISTS background")
66
+
67
+ # Step 4: Create mental_models table with final v4 schema (if not exists)
68
+ op.execute(f"""
69
+ CREATE TABLE IF NOT EXISTS {schema}mental_models (
70
+ id VARCHAR(64) NOT NULL,
71
+ bank_id VARCHAR(64) NOT NULL,
72
+ subtype VARCHAR(32) NOT NULL,
73
+ name VARCHAR(256) NOT NULL,
74
+ description TEXT NOT NULL,
75
+ entity_id UUID,
76
+ observations JSONB DEFAULT '{{"observations": []}}'::jsonb,
77
+ links VARCHAR[],
78
+ tags VARCHAR[] DEFAULT '{{}}',
79
+ last_updated TIMESTAMP WITH TIME ZONE,
80
+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
81
+ PRIMARY KEY (id, bank_id),
82
+ FOREIGN KEY (bank_id) REFERENCES {schema}banks(bank_id) ON DELETE CASCADE,
83
+ FOREIGN KEY (entity_id) REFERENCES {schema}entities(id) ON DELETE SET NULL,
84
+ CONSTRAINT ck_mental_models_subtype CHECK (subtype IN ('structural', 'emergent', 'pinned', 'learned'))
85
+ )
86
+ """)
87
+
88
+ # Step 5: Create indexes for efficient queries (if not exist)
89
+ op.execute(f"CREATE INDEX IF NOT EXISTS idx_mental_models_bank_id ON {schema}mental_models(bank_id)")
90
+ op.execute(f"CREATE INDEX IF NOT EXISTS idx_mental_models_subtype ON {schema}mental_models(bank_id, subtype)")
91
+ op.execute(f"CREATE INDEX IF NOT EXISTS idx_mental_models_entity_id ON {schema}mental_models(entity_id)")
92
+ # GIN index for efficient tags array filtering
93
+ op.execute(f"CREATE INDEX IF NOT EXISTS idx_mental_models_tags ON {schema}mental_models USING GIN(tags)")
94
+
95
+
96
+ def downgrade() -> None:
97
+ """Revert mental models v4 changes."""
98
+ schema = _get_schema_prefix()
99
+
100
+ # Drop mental_models table (cascades to indexes)
101
+ op.execute(f"DROP TABLE IF EXISTS {schema}mental_models CASCADE")
102
+
103
+ # Add back background column to banks
104
+ op.execute(f"ALTER TABLE {schema}banks ADD COLUMN IF NOT EXISTS background TEXT")
105
+
106
+ # Migrate mission back to background
107
+ op.execute(f"UPDATE {schema}banks SET background = mission WHERE background IS NULL")
108
+
109
+ # Remove mission column
110
+ op.execute(f"ALTER TABLE {schema}banks DROP COLUMN IF EXISTS mission")
111
+
112
+ # Note: Cannot restore deleted observations - they are lost on downgrade
@@ -0,0 +1,41 @@
1
+ """delete_opinions
2
+
3
+ Revision ID: i4d5e6f7g8h9
4
+ Revises: h3c4d5e6f7g8
5
+ Create Date: 2026-01-15 00:00:00.000000
6
+
7
+ This migration removes opinion facts from memory_units.
8
+ Opinions are no longer a separate fact type - they are now represented
9
+ through mental model observations with confidence scores.
10
+ """
11
+
12
+ from collections.abc import Sequence
13
+
14
+ from alembic import context, op
15
+
16
+ # revision identifiers, used by Alembic.
17
+ revision: str = "i4d5e6f7g8h9"
18
+ down_revision: str | Sequence[str] | None = "h3c4d5e6f7g8"
19
+ branch_labels: str | Sequence[str] | None = None
20
+ depends_on: str | Sequence[str] | None = None
21
+
22
+
23
+ def _get_schema_prefix() -> str:
24
+ """Get schema prefix for table names (required for multi-tenant support)."""
25
+ schema = context.config.get_main_option("target_schema")
26
+ return f'"{schema}".' if schema else ""
27
+
28
+
29
+ def upgrade() -> None:
30
+ """Delete opinion memory_units."""
31
+ schema = _get_schema_prefix()
32
+
33
+ # Delete opinion memory_units (cascades to unit_entities links)
34
+ # Opinions are now handled through mental model observations
35
+ op.execute(f"DELETE FROM {schema}memory_units WHERE fact_type = 'opinion'")
36
+
37
+
38
+ def downgrade() -> None:
39
+ """Cannot restore deleted opinions."""
40
+ # Note: Cannot restore deleted opinions - they are lost on downgrade
41
+ pass
@@ -0,0 +1,95 @@
1
+ """mental_model_versions
2
+
3
+ Revision ID: j5e6f7g8h9i0
4
+ Revises: i4d5e6f7g8h9
5
+ Create Date: 2026-01-16 00:00:00.000000
6
+
7
+ This migration adds versioning support for mental models:
8
+ 1. Creates mental_model_versions table to store observation snapshots
9
+ 2. Adds version column to mental_models for tracking current version
10
+
11
+ This enables changelog/diff functionality for mental model observations.
12
+ """
13
+
14
+ from collections.abc import Sequence
15
+
16
+ from alembic import context, op
17
+
18
+ # revision identifiers, used by Alembic.
19
+ revision: str = "j5e6f7g8h9i0"
20
+ down_revision: str | Sequence[str] | None = "i4d5e6f7g8h9"
21
+ branch_labels: str | Sequence[str] | None = None
22
+ depends_on: str | Sequence[str] | None = None
23
+
24
+
25
+ def _get_schema_prefix() -> str:
26
+ """Get schema prefix for table names (required for multi-tenant support)."""
27
+ schema = context.config.get_main_option("target_schema")
28
+ return f'"{schema}".' if schema else ""
29
+
30
+
31
+ def upgrade() -> None:
32
+ """Create mental_model_versions table and add version tracking."""
33
+ schema = _get_schema_prefix()
34
+
35
+ # Create mental_model_versions table for storing observation snapshots
36
+ op.execute(f"""
37
+ CREATE TABLE {schema}mental_model_versions (
38
+ id SERIAL PRIMARY KEY,
39
+ mental_model_id VARCHAR(64) NOT NULL,
40
+ bank_id VARCHAR(64) NOT NULL,
41
+ version INT NOT NULL,
42
+ observations JSONB NOT NULL DEFAULT '{{"observations": []}}'::jsonb,
43
+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
44
+ FOREIGN KEY (mental_model_id, bank_id)
45
+ REFERENCES {schema}mental_models(id, bank_id) ON DELETE CASCADE,
46
+ UNIQUE (mental_model_id, bank_id, version)
47
+ )
48
+ """)
49
+
50
+ # Index for efficient version queries (get latest, list versions)
51
+ op.execute(f"""
52
+ CREATE INDEX idx_mental_model_versions_lookup
53
+ ON {schema}mental_model_versions(mental_model_id, bank_id, version DESC)
54
+ """)
55
+
56
+ # Add version column to mental_models to track current version
57
+ op.execute(f"""
58
+ ALTER TABLE {schema}mental_models
59
+ ADD COLUMN IF NOT EXISTS version INT NOT NULL DEFAULT 0
60
+ """)
61
+
62
+ # Migrate existing mental models: create version 1 for any that have observations
63
+ op.execute(f"""
64
+ INSERT INTO {schema}mental_model_versions (mental_model_id, bank_id, version, observations, created_at)
65
+ SELECT id, bank_id, 1, observations, COALESCE(last_updated, created_at)
66
+ FROM {schema}mental_models
67
+ WHERE observations IS NOT NULL
68
+ AND observations != '{{"observations": []}}'::jsonb
69
+ AND (observations->'observations') IS NOT NULL
70
+ AND jsonb_array_length(observations->'observations') > 0
71
+ """)
72
+
73
+ # Update version to 1 for migrated mental models
74
+ op.execute(f"""
75
+ UPDATE {schema}mental_models
76
+ SET version = 1
77
+ WHERE observations IS NOT NULL
78
+ AND observations != '{{"observations": []}}'::jsonb
79
+ AND (observations->'observations') IS NOT NULL
80
+ AND jsonb_array_length(observations->'observations') > 0
81
+ """)
82
+
83
+
84
+ def downgrade() -> None:
85
+ """Remove mental_model_versions table and version column."""
86
+ schema = _get_schema_prefix()
87
+
88
+ # Drop index
89
+ op.execute(f"DROP INDEX IF EXISTS {schema}idx_mental_model_versions_lookup")
90
+
91
+ # Drop versions table
92
+ op.execute(f"DROP TABLE IF EXISTS {schema}mental_model_versions")
93
+
94
+ # Remove version column from mental_models
95
+ op.execute(f"ALTER TABLE {schema}mental_models DROP COLUMN IF EXISTS version")