rememberstack 0.1.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.
- rememberstack/__init__.py +9 -0
- rememberstack/adapters/__init__.py +42 -0
- rememberstack/adapters/codex_writer.py +221 -0
- rememberstack/adapters/markitdown_converter.py +42 -0
- rememberstack/adapters/openrouter.py +136 -0
- rememberstack/adapters/selfhost/__init__.py +54 -0
- rememberstack/adapters/selfhost/forget.py +66 -0
- rememberstack/adapters/selfhost/git.py +374 -0
- rememberstack/adapters/selfhost/lance.py +328 -0
- rememberstack/adapters/selfhost/minio.py +279 -0
- rememberstack/adapters/selfhost/mounts.py +249 -0
- rememberstack/adapters/selfhost/object_store.py +130 -0
- rememberstack/adapters/selfhost/projection.py +80 -0
- rememberstack/adapters/selfhost/queue.py +137 -0
- rememberstack/adapters/selfhost/telemetry.py +45 -0
- rememberstack/adapters/selfhost/watcher.py +70 -0
- rememberstack/adapters/testing/__init__.py +15 -0
- rememberstack/adapters/testing/cost_meter.py +13 -0
- rememberstack/adapters/testing/model_provider.py +83 -0
- rememberstack/adapters/testing/queue.py +43 -0
- rememberstack/adapters/testing/telemetry.py +22 -0
- rememberstack/client.py +19 -0
- rememberstack/core/__init__.py +127 -0
- rememberstack/core/blockizer.py +189 -0
- rememberstack/core/chunker.py +216 -0
- rememberstack/core/consumption_skill.py +275 -0
- rememberstack/core/conversion.py +76 -0
- rememberstack/core/core_manifest.py +598 -0
- rememberstack/core/extension_packs.py +124 -0
- rememberstack/core/forget.py +17 -0
- rememberstack/core/knowledge_authored.py +276 -0
- rememberstack/core/knowledge_compile.py +215 -0
- rememberstack/core/knowledge_fact_sheet.py +210 -0
- rememberstack/core/knowledge_hashing.py +68 -0
- rememberstack/core/knowledge_planner.py +64 -0
- rememberstack/core/knowledge_writer.py +175 -0
- rememberstack/core/ranking.py +200 -0
- rememberstack/core/recipe_linter.py +149 -0
- rememberstack/core/section_snap.py +209 -0
- rememberstack/core/storage_routing.py +27 -0
- rememberstack/eval/__init__.py +53 -0
- rememberstack/eval/consumption.py +141 -0
- rememberstack/eval/contradiction.py +184 -0
- rememberstack/eval/harness.py +136 -0
- rememberstack/eval/lifecycle.py +400 -0
- rememberstack/eval/operational_scale.py +49 -0
- rememberstack/eval/resolution.py +255 -0
- rememberstack/eval/retrieval_spikes.py +50 -0
- rememberstack/eval/skeleton.py +231 -0
- rememberstack/llm/__init__.py +1 -0
- rememberstack/model/__init__.py +589 -0
- rememberstack/model/adjudication.py +100 -0
- rememberstack/model/auth.py +27 -0
- rememberstack/model/blocks.py +30 -0
- rememberstack/model/chunks.py +190 -0
- rememberstack/model/claims.py +162 -0
- rememberstack/model/client.py +98 -0
- rememberstack/model/clustering.py +54 -0
- rememberstack/model/component_version.py +124 -0
- rememberstack/model/consumption.py +88 -0
- rememberstack/model/conversion.py +31 -0
- rememberstack/model/deployment.py +53 -0
- rememberstack/model/documents.py +168 -0
- rememberstack/model/envelope.py +513 -0
- rememberstack/model/evaluation.py +72 -0
- rememberstack/model/forget.py +143 -0
- rememberstack/model/git.py +13 -0
- rememberstack/model/knowledge.py +840 -0
- rememberstack/model/knowledge_authored.py +325 -0
- rememberstack/model/knowledge_planner.py +431 -0
- rememberstack/model/lifecycle.py +42 -0
- rememberstack/model/model_provider.py +78 -0
- rememberstack/model/mounts.py +24 -0
- rememberstack/model/object_store.py +21 -0
- rememberstack/model/operational_scale.py +59 -0
- rememberstack/model/operations.py +153 -0
- rememberstack/model/processing.py +228 -0
- rememberstack/model/queue.py +73 -0
- rememberstack/model/recipes.py +83 -0
- rememberstack/model/relations.py +79 -0
- rememberstack/model/resolution.py +83 -0
- rememberstack/model/retrieval_spikes.py +62 -0
- rememberstack/model/sections.py +120 -0
- rememberstack/model/telemetry.py +30 -0
- rememberstack/ports/__init__.py +29 -0
- rememberstack/ports/auth.py +16 -0
- rememberstack/ports/connector.py +23 -0
- rememberstack/ports/cost_meter.py +17 -0
- rememberstack/ports/forget.py +20 -0
- rememberstack/ports/git.py +20 -0
- rememberstack/ports/model_provider.py +28 -0
- rememberstack/ports/mounts.py +16 -0
- rememberstack/ports/object_store.py +27 -0
- rememberstack/ports/p1_index.py +92 -0
- rememberstack/ports/purge.py +93 -0
- rememberstack/ports/queue.py +23 -0
- rememberstack/ports/telemetry.py +21 -0
- rememberstack/profiles/__init__.py +22 -0
- rememberstack/profiles/selfhost.py +324 -0
- rememberstack/profiles/selfhost_forget.py +158 -0
- rememberstack/profiles/selfhost_operations.py +95 -0
- rememberstack/py.typed +1 -0
- rememberstack/spine/__init__.py +93 -0
- rememberstack/spine/admission.py +26 -0
- rememberstack/spine/backfill.py +168 -0
- rememberstack/spine/catalog_contract.py +742 -0
- rememberstack/spine/chunk_catalog.py +237 -0
- rememberstack/spine/claim_catalog.py +298 -0
- rememberstack/spine/clustering.py +740 -0
- rememberstack/spine/component_versions.py +208 -0
- rememberstack/spine/consumption.py +81 -0
- rememberstack/spine/deployment_bootstrap.py +445 -0
- rememberstack/spine/document_catalog.py +621 -0
- rememberstack/spine/entity_registry.py +205 -0
- rememberstack/spine/extension_packs.py +220 -0
- rememberstack/spine/fact_catalog.py +571 -0
- rememberstack/spine/forget.py +1753 -0
- rememberstack/spine/knowledge.py +5467 -0
- rememberstack/spine/lifecycle.py +1071 -0
- rememberstack/spine/migrations/__init__.py +1 -0
- rememberstack/spine/migrations/_helpers.py +153 -0
- rememberstack/spine/migrations/env.py +58 -0
- rememberstack/spine/migrations/script.py.mako +27 -0
- rememberstack/spine/migrations/versions/__init__.py +1 -0
- rememberstack/spine/migrations/versions/p0_02_0001_extensions_enums.py +189 -0
- rememberstack/spine/migrations/versions/p0_02_0002_infrastructure_registries.py +321 -0
- rememberstack/spine/migrations/versions/p0_02_0003_entities_evaluation_e0_e1.py +631 -0
- rememberstack/spine/migrations/versions/p0_02_0004_claims_facts_evidence.py +411 -0
- rememberstack/spine/migrations/versions/p0_02_0005_projection_knowledge_retrieval.py +391 -0
- rememberstack/spine/migrations/versions/p0_02_0006_partitions_views.py +158 -0
- rememberstack/spine/migrations/versions/p2_06_0007_invalidated_outcome.py +26 -0
- rememberstack/spine/migrations/versions/p3_01_0008_document_version_target.py +58 -0
- rememberstack/spine/migrations/versions/p3_05_0009_reconcile_stage.py +27 -0
- rememberstack/spine/migrations/versions/p3_07_0010_lifecycle_eval_suite.py +25 -0
- rememberstack/spine/migrations/versions/p4_01_0011_survivor_view_rewrite.py +57 -0
- rememberstack/spine/migrations/versions/p6_02_0012_knowledge_compile_recovery.py +58 -0
- rememberstack/spine/migrations/versions/p6_04_0013_knowledge_writer_ledger.py +46 -0
- rememberstack/spine/migrations/versions/p6_05_0014_knowledge_planner_runtime.py +217 -0
- rememberstack/spine/migrations/versions/p6_06_0015_authored_dispatch_runtime.py +38 -0
- rememberstack/spine/migrations/versions/p7_02_0016_operational_eval_suite.py +19 -0
- rememberstack/spine/migrations/versions/p7_05_0017_hard_forget.py +55 -0
- rememberstack/spine/observation_adjudication.py +778 -0
- rememberstack/spine/operations.py +298 -0
- rememberstack/spine/projection.py +662 -0
- rememberstack/spine/recipes.py +276 -0
- rememberstack/spine/resolver.py +763 -0
- rememberstack/spine/review.py +650 -0
- rememberstack/spine/settings.py +22 -0
- rememberstack/spine/supersession.py +510 -0
- rememberstack/spine/sync.py +128 -0
- rememberstack/spine/work_ledger.py +816 -0
- rememberstack/surfaces/__init__.py +110 -0
- rememberstack/surfaces/cli.py +447 -0
- rememberstack/surfaces/consumption_skill.py +87 -0
- rememberstack/surfaces/graph_queries.py +698 -0
- rememberstack/surfaces/http_api.py +377 -0
- rememberstack/surfaces/mcp.py +67 -0
- rememberstack/surfaces/query_engine.py +1591 -0
- rememberstack/surfaces/recipe_executor.py +185 -0
- rememberstack/surfaces/recipe_surface.py +219 -0
- rememberstack/surfaces/remote_mcp.py +133 -0
- rememberstack/surfaces/sdk.py +324 -0
- rememberstack/workers/__init__.py +155 -0
- rememberstack/workers/base.py +312 -0
- rememberstack/workers/e0.py +577 -0
- rememberstack/workers/e1.py +425 -0
- rememberstack/workers/e2.py +525 -0
- rememberstack/workers/e3.py +434 -0
- rememberstack/workers/forget.py +299 -0
- rememberstack/workers/knowledge_authored.py +146 -0
- rememberstack/workers/knowledge_driver.py +735 -0
- rememberstack/workers/knowledge_fact_sheet.py +123 -0
- rememberstack/workers/knowledge_planner.py +325 -0
- rememberstack/workers/knowledge_writer.py +393 -0
- rememberstack/workers/operations.py +42 -0
- rememberstack/workers/p1.py +234 -0
- rememberstack/workers/p2.py +513 -0
- rememberstack/workers/p2_analytics.py +276 -0
- rememberstack/workers/p3.py +673 -0
- rememberstack/workers/reconcile.py +485 -0
- rememberstack/workers/sync.py +168 -0
- rememberstack-0.1.0.dist-info/METADATA +213 -0
- rememberstack-0.1.0.dist-info/RECORD +186 -0
- rememberstack-0.1.0.dist-info/WHEEL +4 -0
- rememberstack-0.1.0.dist-info/entry_points.txt +2 -0
- rememberstack-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"""Create tenancy, pipeline, and registry structures."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
|
|
5
|
+
from alembic import op
|
|
6
|
+
|
|
7
|
+
from rememberstack.spine.migrations._helpers import apply_ddl
|
|
8
|
+
from rememberstack.spine.migrations._helpers import drop_tables
|
|
9
|
+
from rememberstack.spine.migrations._helpers import drop_types
|
|
10
|
+
|
|
11
|
+
revision: str = "p0_02_0002"
|
|
12
|
+
down_revision: str | None = "p0_02_0001"
|
|
13
|
+
branch_labels: str | Sequence[str] | None = None
|
|
14
|
+
depends_on: str | Sequence[str] | None = None
|
|
15
|
+
|
|
16
|
+
_DDL = r"""-- ─────────────────────────────────────────────────────────────────────────
|
|
17
|
+
-- deployments — the tenancy root. One identity row for the independent deployment served by
|
|
18
|
+
-- this Postgres instance/schema (D16/D68/registries §1).
|
|
19
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
20
|
+
CREATE TABLE deployments (
|
|
21
|
+
deployment_id uuid PRIMARY KEY, -- stable instance identity; appears in every scoped FK
|
|
22
|
+
slug text NOT NULL UNIQUE, -- short handle used in GCS bucket names: rememberstack-<slug>-raw etc.
|
|
23
|
+
name text NOT NULL, -- human label ("Personal assistant", "Acme migration")
|
|
24
|
+
description text, -- what this deployment is for
|
|
25
|
+
default_language text NOT NULL DEFAULT 'en', -- primary corpus language; gates the multilingual matching path (registries §5)
|
|
26
|
+
raw_bucket text NOT NULL, -- gs:// raw bucket (immutable originals; mounted read-only OFF the navigation path, audit-logged — D37/D51)
|
|
27
|
+
artifacts_bucket text NOT NULL, -- gs:// artifacts bucket (markdown/pageindex, mount-readable) — D37
|
|
28
|
+
corpusfs_bucket text NOT NULL, -- gs:// P3 corpus-filesystem bucket (snapshots + latest) — D40
|
|
29
|
+
knowledge_repo_uri text, -- plane-K git remote (git is truth for K; PG holds provenance only) — D1
|
|
30
|
+
status deployment_status NOT NULL DEFAULT 'active',
|
|
31
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
32
|
+
updated_at timestamptz NOT NULL DEFAULT now()
|
|
33
|
+
);
|
|
34
|
+
COMMENT ON TABLE deployments IS
|
|
35
|
+
'Identity root for the independent deployment served by this Postgres instance/schema (D16/D68). Entity spaces, registries, graphs and buckets are never shared across deployments; deployment_id is constant here and participates in every scoped FK as defense in depth.';
|
|
36
|
+
|
|
37
|
+
-- DATA BOUNDARY (D69): Alembic creates this table but inserts no deployment row. After schema
|
|
38
|
+
-- head, the library-owned bootstrap described below creates or verifies the one real row from
|
|
39
|
+
-- typed profile inputs before any deployment-scoped registry FK row is inserted.
|
|
40
|
+
|
|
41
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
42
|
+
-- pipeline_component_versions — what every *_version string means (D1/D12).
|
|
43
|
+
-- Pin model+prompt+params so replay-on-rebuild (D7) is auditable and re-process batches can be
|
|
44
|
+
-- scoped by a version filter (e.g. re-embed where embedding_version < 'emb-2026-02').
|
|
45
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
46
|
+
CREATE TABLE pipeline_component_versions (
|
|
47
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
48
|
+
component pipeline_component NOT NULL, -- which producer (see the pipeline_component enum, §1 — every value listed there can appear)
|
|
49
|
+
version text NOT NULL, -- the string stamped on artifacts (e.g. 'extractor-2026-03a')
|
|
50
|
+
model_name text, -- LLM/embedding model id, if this component calls a model (e.g. 'claude-opus-4-8')
|
|
51
|
+
prompt_hash text, -- sha256 of the rendered prompt template (registry-derived prompts change ⇒ new version) — D15
|
|
52
|
+
embedding_dim int, -- embedder only: vector dimension (re-embedding is the hardest migration — questions.md Q3)
|
|
53
|
+
params jsonb NOT NULL DEFAULT '{}', -- decoding params, routing table, thresholds snapshot, etc.
|
|
54
|
+
notes text,
|
|
55
|
+
configured_at timestamptz NOT NULL DEFAULT now(),
|
|
56
|
+
PRIMARY KEY (deployment_id, component, version)
|
|
57
|
+
);
|
|
58
|
+
COMMENT ON TABLE pipeline_component_versions IS
|
|
59
|
+
'Catalog resolving every *_version string to its model/prompt/params, so replay-on-rebuild (D7) and version-scoped reprocessing are auditable (D1, D12). Every pipeline_component enum value can be catalogued here.';
|
|
60
|
+
|
|
61
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
62
|
+
-- processing_state — idempotency + status + dead-letter, the D12 spine.
|
|
63
|
+
-- One row per (target, stage, component_version). A worker INSERTs (ON CONFLICT DO NOTHING) this
|
|
64
|
+
-- row before work; it is a no-op if a row for the SAME (deployment_id, target_kind, target_id,
|
|
65
|
+
-- stage, component_version) already has status='succeeded'. Because target ids are content-derived
|
|
66
|
+
-- and stable (a document re-upload resolves to the same doc_id via documents' UNIQUE(deployment,
|
|
67
|
+
-- content_hash); sub-document targets are deterministic), the (target,stage,version) tuple IS the
|
|
68
|
+
-- content+version idempotency key D12 calls for. content_hash is carried for diagnostics/replay.
|
|
69
|
+
-- Lane is intentionally not part of that key: it routes one logical unit of work. A duplicate
|
|
70
|
+
-- steady enqueue may promote pending/failed backfill work (never the reverse); an explicit
|
|
71
|
+
-- dead-letter replay may also reroute it. Promotion clears only defer_reason='budget' and resets
|
|
72
|
+
-- that row's not_before to now(); scheduled and retry_backoff waits are preserved. The dead-letter
|
|
73
|
+
-- queue is status='dead_letter' rows.
|
|
74
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
75
|
+
CREATE TABLE processing_state (
|
|
76
|
+
processing_id uuid PRIMARY KEY,
|
|
77
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
78
|
+
target_kind processing_target NOT NULL, -- document | document_section | chunk | claim | relation | observation | entity | snapshot | knowledge_artifact
|
|
79
|
+
target_id uuid NOT NULL, -- LOGICAL FK → the target table's PK (kind tells you which)
|
|
80
|
+
stage pipeline_stage NOT NULL, -- the processing stage (see pipeline_stage enum, §1)
|
|
81
|
+
component_version text NOT NULL, -- LOGICAL FK → pipeline_component_versions(version); the version this attempt ran
|
|
82
|
+
content_hash text NOT NULL, -- sha256 carried for diagnostics/replay; = doc raw-bytes hash, or parent-hash+salt for sub-document targets
|
|
83
|
+
lane processing_lane, -- steady | backfill for plane E; NULL for K/P scheduled jobs (D67); stage/lane pairing enforced at the spine enqueue path (catalog UNLANED_STAGES), not by a stage-enumerating CHECK
|
|
84
|
+
status processing_status NOT NULL DEFAULT 'pending',
|
|
85
|
+
not_before timestamptz NOT NULL DEFAULT now(), -- canonical earliest claim time; never hidden in payload (D67)
|
|
86
|
+
defer_reason processing_defer_reason, -- scheduled | retry_backoff | budget; constrained to the corresponding status below
|
|
87
|
+
attempts smallint NOT NULL DEFAULT 0, -- handler executions actually begun; delivery attempts and budget parking do not increment it
|
|
88
|
+
max_attempts smallint NOT NULL DEFAULT 3, -- total handler-attempt limit; starting point = initial + D12's two retries
|
|
89
|
+
last_error text, -- full traceback + cause chain for the most recent handler failure; failures never disappear
|
|
90
|
+
payload jsonb, -- open-ended handler input for DLQ inspection/replay; never route, due, retry, budget, or DLQ state
|
|
91
|
+
enqueued_at timestamptz NOT NULL DEFAULT now(),
|
|
92
|
+
started_at timestamptz,
|
|
93
|
+
finished_at timestamptz,
|
|
94
|
+
UNIQUE (deployment_id, target_kind, target_id, stage, component_version),
|
|
95
|
+
UNIQUE (deployment_id, processing_id), -- supports tenancy-safe cost_ledger FK
|
|
96
|
+
CHECK (attempts >= 0 AND max_attempts >= 1 AND attempts <= max_attempts),
|
|
97
|
+
CHECK (status <> 'failed' OR attempts < max_attempts),
|
|
98
|
+
CHECK (
|
|
99
|
+
(status = 'failed' AND defer_reason = 'retry_backoff') OR
|
|
100
|
+
(status = 'pending' AND (defer_reason IS NULL OR defer_reason IN ('scheduled','budget'))) OR
|
|
101
|
+
(status NOT IN ('pending','failed') AND defer_reason IS NULL)
|
|
102
|
+
)
|
|
103
|
+
);
|
|
104
|
+
COMMENT ON TABLE processing_state IS
|
|
105
|
+
'Per-(target,stage,version) idempotency and work-truth ledger (D12/D67). Route is deployment+stage+lane; not_before/defer_reason govern scheduling, retry backoff, and no-attempt budget parking. The DLQ is status=dead_letter rows; delivery-provider metadata is never authoritative.';
|
|
106
|
+
CREATE INDEX ix_procstate_dlq ON processing_state (deployment_id, stage) WHERE status = 'dead_letter';
|
|
107
|
+
CREATE INDEX ix_procstate_due ON processing_state (deployment_id, stage, lane, not_before, enqueued_at, processing_id) WHERE status IN ('pending','failed');
|
|
108
|
+
CREATE INDEX ix_procstate_target ON processing_state (target_kind, target_id);
|
|
109
|
+
|
|
110
|
+
-- Transactional initial wake for the self-host shell (D67). PostgreSQL delivers NOTIFY only if
|
|
111
|
+
-- the INSERT commits; future-scheduled rows are discovered by the due-work fallback poll. The
|
|
112
|
+
-- delivery port never INSERTs processing_state. Explicit retry/replay/janitor announcements call
|
|
113
|
+
-- the same pg_notify operation through spine after their state transition commits.
|
|
114
|
+
CREATE FUNCTION notify_due_processing_insert() RETURNS trigger
|
|
115
|
+
LANGUAGE plpgsql AS $$
|
|
116
|
+
BEGIN
|
|
117
|
+
IF NEW.status IN ('pending','failed') AND NEW.not_before <= now() THEN
|
|
118
|
+
PERFORM pg_notify('queue_wake', NEW.processing_id::text);
|
|
119
|
+
END IF;
|
|
120
|
+
RETURN NEW;
|
|
121
|
+
END;
|
|
122
|
+
$$;
|
|
123
|
+
|
|
124
|
+
CREATE TRIGGER tr_processing_state_initial_wake
|
|
125
|
+
AFTER INSERT ON processing_state
|
|
126
|
+
FOR EACH ROW EXECUTE FUNCTION notify_due_processing_insert();
|
|
127
|
+
|
|
128
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
129
|
+
-- cost_ledger — per-invocation cost/latency metering for enforced per-layer budgets (§8 overall).
|
|
130
|
+
-- A succeeded-but-ack-lost call must not be re-billed on retry, while D31 and other handlers may
|
|
131
|
+
-- make several calls in one attempt. A ledger row is therefore anchored to one processing row,
|
|
132
|
+
-- handler attempt, and deterministic stage-local call_key. A batched call (a D58 window) is
|
|
133
|
+
-- billed as one row on the claiming processing row — a batch never crosses a document or a lane,
|
|
134
|
+
-- so lane budgets and document-level accounting stay exact without splitting. Enforcement reads
|
|
135
|
+
-- the deduplicated total. The spine cost-write method accepts processing_id + call_key + measured cost;
|
|
136
|
+
-- while the processing row is locked/running it copies stage, lane, and attempts from that row.
|
|
137
|
+
-- Callers and delivery envelopes cannot supply or override those three attribution fields.
|
|
138
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
139
|
+
CREATE TABLE cost_ledger (
|
|
140
|
+
cost_id uuid PRIMARY KEY,
|
|
141
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
142
|
+
processing_id uuid NOT NULL, -- owning processing_state row; composite FK below
|
|
143
|
+
stage pipeline_stage NOT NULL, -- which layer/stage incurred the spend
|
|
144
|
+
lane processing_lane, -- copied from processing_state when the billed call begins; NULL for unlaned K/P work (D67); pairing enforced upstream (spine copies from the validated row)
|
|
145
|
+
target_kind processing_target, -- optional: what was being processed
|
|
146
|
+
target_id uuid, -- LOGICAL FK → target
|
|
147
|
+
component_version text, -- LOGICAL FK → pipeline_component_versions(version)
|
|
148
|
+
attempt smallint NOT NULL, -- processing_state.attempts value when this call began; starts at 1
|
|
149
|
+
call_key text NOT NULL, -- deterministic key within the attempt, e.g. selection | decontextualize | adjudicate:<candidate_id>
|
|
150
|
+
model_name text, -- model billed
|
|
151
|
+
tier text, -- cascade rung that fired (e.g. 'T4-small','T4-frontier','selection','decontext') — cost scales with ambiguity (D4/D17)
|
|
152
|
+
tokens_in bigint, -- prompt tokens (incl. cached-prefix accounting where applicable)
|
|
153
|
+
tokens_out bigint, -- completion tokens
|
|
154
|
+
cost_usd numeric(12,6), -- billed cost in USD
|
|
155
|
+
latency_ms integer, -- wall-clock of the call
|
|
156
|
+
occurred_at timestamptz NOT NULL DEFAULT now(),
|
|
157
|
+
FOREIGN KEY (deployment_id, processing_id) REFERENCES processing_state (deployment_id, processing_id),
|
|
158
|
+
UNIQUE (deployment_id, processing_id, attempt, call_key), -- multiple calls per attempt; one row per logical call
|
|
159
|
+
CHECK (attempt >= 1)
|
|
160
|
+
);
|
|
161
|
+
COMMENT ON TABLE cost_ledger IS
|
|
162
|
+
'Append-only LLM/embedding call attribution for D67 budgets. Idempotent per (processing_id,attempt,call_key), so multi-call handlers are complete and acknowledged-late retries cannot double-count. A batched call (D58) bills one row on the claiming processing row; batches never cross a document or lane, so lane and document accounting stay exact. Nullable diagnostic target fields do not weaken deduplication.';
|
|
163
|
+
CREATE INDEX ix_cost_budget_window ON cost_ledger (deployment_id, stage, lane, occurred_at);
|
|
164
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
165
|
+
-- extension_packs — system-shipped sets of types+predicates a deployment enables as a unit.
|
|
166
|
+
-- "Extensions are not second-class" (registries §4): full entity status, governance tier only.
|
|
167
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
168
|
+
CREATE TABLE extension_packs (
|
|
169
|
+
pack_id text PRIMARY KEY, -- stable pack handle: 'work' | 'legal' | 'systems' | ...
|
|
170
|
+
name text NOT NULL,
|
|
171
|
+
description text NOT NULL, -- what the pack is for and which deployments want it
|
|
172
|
+
is_system boolean NOT NULL DEFAULT true -- shipped with the system vs deployment-defined
|
|
173
|
+
);
|
|
174
|
+
COMMENT ON TABLE extension_packs IS
|
|
175
|
+
'Predefined bundles of extension types/predicates (registries §4) — e.g. the Work pack (Task/Decision/Goal). Enabling a pack inserts its registry rows for a deployment.';
|
|
176
|
+
|
|
177
|
+
CREATE TABLE deployment_extension_packs (
|
|
178
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
179
|
+
pack_id text NOT NULL REFERENCES extension_packs,
|
|
180
|
+
enabled_at timestamptz NOT NULL DEFAULT now(),
|
|
181
|
+
PRIMARY KEY (deployment_id, pack_id)
|
|
182
|
+
);
|
|
183
|
+
COMMENT ON TABLE deployment_extension_packs IS 'Which packs each deployment has enabled (registries §1: core + chosen packs + own scopes).';
|
|
184
|
+
|
|
185
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
186
|
+
-- scopes — K2 special-purpose lenses over the one shared entity space (D16).
|
|
187
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
188
|
+
CREATE TABLE scopes (
|
|
189
|
+
scope_id uuid NOT NULL,
|
|
190
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
191
|
+
slug text NOT NULL, -- 'people' | 'product-atlas' | ...
|
|
192
|
+
name text NOT NULL,
|
|
193
|
+
description text,
|
|
194
|
+
git_path text, -- directory in the K-plane repo this scope compiles into (K2)
|
|
195
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
196
|
+
PRIMARY KEY (scope_id),
|
|
197
|
+
UNIQUE (deployment_id, scope_id), -- composite-FK target (tenancy isolation, §0)
|
|
198
|
+
UNIQUE (deployment_id, slug)
|
|
199
|
+
);
|
|
200
|
+
COMMENT ON TABLE scopes IS
|
|
201
|
+
'K2 scopes (D16): perspectives over shared evidence. "Scopes multiply; truth doesn''t" — a scope is registry rows + a view definition + a git directory, never a new database.';
|
|
202
|
+
|
|
203
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
204
|
+
-- entity_types — the typed-entity vocabulary (8-type core + pack/scope extensions, D18).
|
|
205
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
206
|
+
CREATE TABLE entity_types (
|
|
207
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
208
|
+
type text NOT NULL, -- 'Person','Organization',... ; extension subtypes like 'ResearchPaper'
|
|
209
|
+
parent_type text, -- extend-never-fork: every extension declares a core parent (D15); NULL only for the 8 core roots
|
|
210
|
+
description text NOT NULL, -- plain-language meaning, rendered into extraction prompts (D15)
|
|
211
|
+
examples text[] NOT NULL DEFAULT '{}',-- few-shot examples rendered into prompts
|
|
212
|
+
schema_org_ref text, -- schema.org anchor (D18); spot-checked before freezing
|
|
213
|
+
tier ontology_tier NOT NULL, -- core | extension | other | deprecated (the three speeds, D15)
|
|
214
|
+
pack_id text REFERENCES extension_packs, -- non-null if from an extension pack
|
|
215
|
+
scope_id uuid, -- non-null if defined by a single K2 scope
|
|
216
|
+
status ontology_status NOT NULL DEFAULT 'active',
|
|
217
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
218
|
+
PRIMARY KEY (deployment_id, type),
|
|
219
|
+
FOREIGN KEY (deployment_id, parent_type) REFERENCES entity_types (deployment_id, type),
|
|
220
|
+
FOREIGN KEY (deployment_id, scope_id) REFERENCES scopes (deployment_id, scope_id) ON DELETE SET NULL (scope_id)
|
|
221
|
+
);
|
|
222
|
+
COMMENT ON TABLE entity_types IS
|
|
223
|
+
'Governed entity-type registry (D15/D18). 8 schema.org-aligned core roots + pack/scope extensions, each anchored to a core parent (extend-never-fork) so blocking and cross-scope queries always fall back to the core level.';
|
|
224
|
+
CREATE INDEX ix_entity_types_parent ON entity_types (deployment_id, parent_type);
|
|
225
|
+
|
|
226
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
227
|
+
-- predicates — the governed relation vocabulary (D5/D18). related_to is the core parent.
|
|
228
|
+
-- The other:<freetext> escape (D5) is materialized here too: when the normalizer encounters an
|
|
229
|
+
-- other:<value>, it UPSERTs a row with tier='other' (so relations.predicate's FK holds AND the
|
|
230
|
+
-- promotion funnel is countable via usage_count). Domain/range is NOT enforced for tier='other'
|
|
231
|
+
-- rows until a periodic job promotes them (registries §4/§7).
|
|
232
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
233
|
+
CREATE TABLE predicates (
|
|
234
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
235
|
+
predicate text NOT NULL, -- 'works_for',... ; 'other:<freetext>' rows live here as tier='other'
|
|
236
|
+
parent_predicate text, -- optional predicate-side extend-never-fork anchor (default parent 'related_to', D18)
|
|
237
|
+
description text NOT NULL, -- meaning rendered into normalization prompts
|
|
238
|
+
examples text[] NOT NULL DEFAULT '{}',
|
|
239
|
+
synonyms text[] NOT NULL DEFAULT '{}',-- surface variants the normalizer maps onto this predicate (works_at/employed_by → works_for) — D5
|
|
240
|
+
schema_org_ref text,
|
|
241
|
+
tier ontology_tier NOT NULL, -- core | extension | other | deprecated
|
|
242
|
+
pack_id text REFERENCES extension_packs,
|
|
243
|
+
scope_id uuid,
|
|
244
|
+
usage_count bigint NOT NULL DEFAULT 0, -- cached count of relations using this predicate; ranks tier='other' values for promotion (D5 funnel)
|
|
245
|
+
is_change_prone boolean NOT NULL DEFAULT false, -- employment/affiliation/location change over time ⇒ supersession-relevant (D18)
|
|
246
|
+
exclude_from_graph_distance boolean NOT NULL DEFAULT false, -- causal/promiscuous predicates excluded from graph-distance reranking (registries §4)
|
|
247
|
+
status ontology_status NOT NULL DEFAULT 'active',
|
|
248
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
249
|
+
PRIMARY KEY (deployment_id, predicate),
|
|
250
|
+
FOREIGN KEY (deployment_id, parent_predicate) REFERENCES predicates (deployment_id, predicate),
|
|
251
|
+
FOREIGN KEY (deployment_id, scope_id) REFERENCES scopes (deployment_id, scope_id) ON DELETE SET NULL (scope_id)
|
|
252
|
+
);
|
|
253
|
+
COMMENT ON TABLE predicates IS
|
|
254
|
+
'Governed predicate vocabulary (D5/D18). Extraction is constrained to these names with an other:<freetext> escape, which the normalizer upserts as tier=other rows (FK holds; usage_count makes the promotion funnel queryable). related_to is the permissive core parent.';
|
|
255
|
+
CREATE INDEX ix_predicates_other ON predicates (deployment_id, usage_count DESC) WHERE tier = 'other'; -- promotion-candidate ranking
|
|
256
|
+
|
|
257
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
258
|
+
-- predicate_signatures — the domain/range gate (Graphiti edge_type_map shape, D18).
|
|
259
|
+
-- A child table because a predicate may allow several (subject_type,object_type) pairs. ENFORCEMENT
|
|
260
|
+
-- is by the NORMALIZER at E3 write time (application-enforced, not a per-insert DB trigger — at 10⁸
|
|
261
|
+
-- relations a trigger walking the type hierarchy on every write is too costly): the normalizer
|
|
262
|
+
-- resolves the subject/object entity types, walks each up its parent chain (extend-never-fork), and
|
|
263
|
+
-- accepts the relation only if some signature matches at any ancestor level. A relation that fails
|
|
264
|
+
-- is DROPPED — and is re-derivable from its immutable claim if the entity is later retyped, so no
|
|
265
|
+
-- quarantine table is needed (registries §4). (An optional BEFORE INSERT trigger may be added as a
|
|
266
|
+
-- belt-and-braces backstop in low-throughput deployments.)
|
|
267
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
268
|
+
CREATE TABLE predicate_signatures (
|
|
269
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
270
|
+
predicate text NOT NULL, -- the predicate this signature constrains
|
|
271
|
+
subject_type text NOT NULL, -- allowed subject entity_type (matched at this level OR any descendant via the normalizer's parent walk)
|
|
272
|
+
object_type text NOT NULL, -- allowed object entity_type
|
|
273
|
+
PRIMARY KEY (deployment_id, predicate, subject_type, object_type),
|
|
274
|
+
FOREIGN KEY (deployment_id, predicate) REFERENCES predicates (deployment_id, predicate) ON DELETE CASCADE,
|
|
275
|
+
FOREIGN KEY (deployment_id, subject_type) REFERENCES entity_types (deployment_id, type),
|
|
276
|
+
FOREIGN KEY (deployment_id, object_type) REFERENCES entity_types (deployment_id, type)
|
|
277
|
+
);
|
|
278
|
+
COMMENT ON TABLE predicate_signatures IS
|
|
279
|
+
'Allowed (subject_type → object_type) pairs per predicate — the one structural ontology gate (D18), enforced by the normalizer (parent-chain walk) at E3 write time. Subtypes inherit a parent''s signatures; a relation matching none is dropped (re-derivable from its claim).';
|
|
280
|
+
|
|
281
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
282
|
+
-- scope_interests — registry-declared scope views + extraction interests (D16).
|
|
283
|
+
-- ─────────────────────────────────────────────────────────────────────────
|
|
284
|
+
CREATE TABLE scope_interests (
|
|
285
|
+
interest_id uuid PRIMARY KEY,
|
|
286
|
+
deployment_id uuid NOT NULL REFERENCES deployments,
|
|
287
|
+
scope_id uuid NOT NULL,
|
|
288
|
+
interest_type scope_interest_kind NOT NULL, -- entity_type | predicate | metadata | keyword
|
|
289
|
+
value text NOT NULL, -- the type name / predicate / metadata key / keyword
|
|
290
|
+
UNIQUE (scope_id, interest_type, value),
|
|
291
|
+
FOREIGN KEY (deployment_id, scope_id) REFERENCES scopes (deployment_id, scope_id) ON DELETE CASCADE
|
|
292
|
+
);
|
|
293
|
+
COMMENT ON TABLE scope_interests IS
|
|
294
|
+
'Per-scope interest list (D16): the predicate/type footprint that defines the scope''s PROJECT_GRAPH_CYPHER view and what its K2 compilation selects. A query/compile-time selection over fully-extracted facts — never a promotion trigger (D28 withdrawn).';
|
|
295
|
+
"""
|
|
296
|
+
_TABLES = (
|
|
297
|
+
"deployments",
|
|
298
|
+
"pipeline_component_versions",
|
|
299
|
+
"processing_state",
|
|
300
|
+
"cost_ledger",
|
|
301
|
+
"extension_packs",
|
|
302
|
+
"deployment_extension_packs",
|
|
303
|
+
"scopes",
|
|
304
|
+
"entity_types",
|
|
305
|
+
"predicates",
|
|
306
|
+
"predicate_signatures",
|
|
307
|
+
"scope_interests",
|
|
308
|
+
)
|
|
309
|
+
_TYPES = ()
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
def upgrade() -> None:
|
|
313
|
+
"""Apply create tenancy, pipeline, and registry structures."""
|
|
314
|
+
apply_ddl(sql=_DDL)
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def downgrade() -> None:
|
|
318
|
+
"""Revert create tenancy, pipeline, and registry structures."""
|
|
319
|
+
drop_tables(table_names=reversed(_TABLES))
|
|
320
|
+
op.execute("DROP FUNCTION IF EXISTS notify_due_processing_insert()")
|
|
321
|
+
drop_types(type_names=reversed(_TYPES))
|