@rine-kit/agent-memory-postgres 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,101 @@
1
+ -- Automatic learning (Light / REM / Deep) from observed utterances. Adapted from Gate migration 0028:
2
+ -- the KOOK DM identity (actor_id) becomes the host-authorised scope (scope_kind, scope_key), and the
3
+ -- host's transcript tables are replaced by an observation corpus owned by this package, so Light/REM
4
+ -- never read anything the host did not explicitly hand over through observe().
5
+ CREATE TABLE agent_memory_consolidation_consents (
6
+ namespace TEXT NOT NULL CHECK (length(namespace) BETWEEN 1 AND 128),
7
+ scope_kind TEXT NOT NULL CHECK (scope_kind ~ '^[a-z][a-z0-9_]{0,31}$'),
8
+ scope_key TEXT NOT NULL CHECK (length(scope_key) BETWEEN 1 AND 256),
9
+ workflow TEXT NOT NULL CHECK (workflow IN ('light','rem')),
10
+ enabled BOOLEAN NOT NULL,
11
+ consent_version BIGINT NOT NULL CHECK (consent_version >= 1),
12
+ changed_by_event_id TEXT NOT NULL CHECK (length(changed_by_event_id) BETWEEN 1 AND 256),
13
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
14
+ PRIMARY KEY (namespace,scope_kind,scope_key,workflow)
15
+ );
16
+
17
+ -- The utterances handed over by the host, bounded and expiring. Content is read only by the worker
18
+ -- after consent, expiry and lease checks pass.
19
+ CREATE TABLE agent_memory_observations (
20
+ namespace TEXT NOT NULL CHECK (length(namespace) BETWEEN 1 AND 128),
21
+ scope_kind TEXT NOT NULL CHECK (scope_kind ~ '^[a-z][a-z0-9_]{0,31}$'),
22
+ scope_key TEXT NOT NULL CHECK (length(scope_key) BETWEEN 1 AND 256),
23
+ source_event_id TEXT NOT NULL CHECK (length(source_event_id) BETWEEN 1 AND 256),
24
+ content TEXT NOT NULL CHECK (length(content) BETWEEN 1 AND 1200),
25
+ observed_at TIMESTAMPTZ NOT NULL,
26
+ expires_at TIMESTAMPTZ NOT NULL DEFAULT (now()+interval '7 days'),
27
+ PRIMARY KEY (namespace,scope_kind,scope_key,source_event_id)
28
+ );
29
+ CREATE INDEX idx_agent_memory_observations_expiry ON agent_memory_observations(expires_at);
30
+
31
+ CREATE TABLE agent_memory_consolidation_admissions (
32
+ admission_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
33
+ namespace TEXT NOT NULL CHECK (length(namespace) BETWEEN 1 AND 128),
34
+ scope_kind TEXT NOT NULL CHECK (scope_kind ~ '^[a-z][a-z0-9_]{0,31}$'),
35
+ scope_key TEXT NOT NULL CHECK (length(scope_key) BETWEEN 1 AND 256),
36
+ workflow TEXT NOT NULL CHECK (workflow IN ('light','rem')),
37
+ source_event_id TEXT NOT NULL CHECK (length(source_event_id) BETWEEN 1 AND 256),
38
+ consent_version BIGINT NOT NULL CHECK (consent_version >= 1),
39
+ status TEXT NOT NULL DEFAULT 'pending'
40
+ CHECK (status IN ('pending','processing','retry','done','skipped','dead')),
41
+ attempt_count INTEGER NOT NULL DEFAULT 0 CHECK (attempt_count >= 0),
42
+ next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
43
+ lease_token UUID,
44
+ lease_until TIMESTAMPTZ,
45
+ skip_reason TEXT CHECK (skip_reason IS NULL OR skip_reason IN (
46
+ 'consent_revoked','source_provenance_missing','source_expired','source_scope_invalid',
47
+ 'input_over_budget','system_work_unavailable'
48
+ )),
49
+ admitted_at TIMESTAMPTZ NOT NULL DEFAULT now(),
50
+ expires_at TIMESTAMPTZ NOT NULL DEFAULT (now()+interval '7 days'),
51
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
52
+ UNIQUE (namespace,scope_kind,scope_key,workflow,source_event_id),
53
+ CHECK ((status='processing') = (lease_token IS NOT NULL AND lease_until IS NOT NULL))
54
+ );
55
+ CREATE INDEX idx_agent_memory_consolidation_admissions_ready
56
+ ON agent_memory_consolidation_admissions(namespace,workflow,status,next_attempt_at,admitted_at)
57
+ WHERE status IN ('pending','retry','processing');
58
+
59
+ CREATE TABLE agent_memory_consolidation_candidates (
60
+ candidate_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
61
+ idempotency_key CHAR(64) NOT NULL UNIQUE CHECK (idempotency_key ~ '^[a-f0-9]{64}$'),
62
+ namespace TEXT NOT NULL CHECK (length(namespace) BETWEEN 1 AND 128),
63
+ scope_kind TEXT NOT NULL CHECK (scope_kind ~ '^[a-z][a-z0-9_]{0,31}$'),
64
+ scope_key TEXT NOT NULL CHECK (length(scope_key) BETWEEN 1 AND 256),
65
+ workflow TEXT NOT NULL CHECK (workflow IN ('light','rem')),
66
+ consent_version BIGINT NOT NULL CHECK (consent_version >= 1),
67
+ memory_key TEXT NOT NULL CHECK (length(memory_key) BETWEEN 1 AND 96),
68
+ content TEXT NOT NULL CHECK (length(content) BETWEEN 1 AND 800),
69
+ candidate_kind TEXT NOT NULL CHECK (candidate_kind IN ('stable_preference','verifiable_fact','episodic_checkpoint')),
70
+ valid_until TIMESTAMPTZ,
71
+ source_event_ids TEXT[] NOT NULL CHECK (cardinality(source_event_ids) BETWEEN 0 AND 8),
72
+ expected_revision BIGINT CHECK (expected_revision IS NULL OR expected_revision >= 1),
73
+ status TEXT NOT NULL DEFAULT 'pending_confirmation'
74
+ CHECK (status IN ('pending_confirmation','conflict','duplicate','promoted','rejected','expired')),
75
+ result_reason TEXT CHECK (result_reason IS NULL OR result_reason IN (
76
+ 'existing_memory_conflict','already_present','consent_revoked','source_expired','cas_conflict'
77
+ )),
78
+ confirmed_by_event_id TEXT,
79
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
80
+ expires_at TIMESTAMPTZ NOT NULL DEFAULT (now()+interval '30 days'),
81
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
82
+ CHECK ((candidate_kind='stable_preference' AND valid_until IS NULL)
83
+ OR (candidate_kind IN ('verifiable_fact','episodic_checkpoint') AND valid_until IS NOT NULL))
84
+ );
85
+ CREATE INDEX idx_agent_memory_consolidation_candidates_owner_recent
86
+ ON agent_memory_consolidation_candidates(namespace,scope_kind,scope_key,status,created_at DESC,candidate_id);
87
+ CREATE UNIQUE INDEX idx_agent_memory_consolidation_candidates_confirm_event
88
+ ON agent_memory_consolidation_candidates(namespace,confirmed_by_event_id)
89
+ WHERE confirmed_by_event_id IS NOT NULL;
90
+
91
+ CREATE TABLE agent_memory_consolidation_checkpoints (
92
+ namespace TEXT NOT NULL,
93
+ scope_kind TEXT NOT NULL,
94
+ scope_key TEXT NOT NULL,
95
+ workflow TEXT NOT NULL CHECK (workflow IN ('light','rem')),
96
+ consent_version BIGINT NOT NULL CHECK (consent_version >= 1),
97
+ last_admitted_at TIMESTAMPTZ NOT NULL,
98
+ last_event_id TEXT NOT NULL,
99
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
100
+ PRIMARY KEY (namespace,scope_kind,scope_key,workflow,consent_version)
101
+ );
@@ -0,0 +1,18 @@
1
+ -- Rebuildable, bounded extractive checkpoints for older working context. Adapted from Gate migration
2
+ -- 0023: the KOOK channel / binding columns become an opaque conversation key chosen by the host.
3
+ -- The host's transcript stays the source of truth; this is never semantic memory.
4
+ CREATE TABLE agent_memory_conversation_checkpoints (
5
+ namespace TEXT NOT NULL CHECK (length(namespace) BETWEEN 1 AND 128),
6
+ conversation_key TEXT NOT NULL CHECK (length(conversation_key) BETWEEN 1 AND 256),
7
+ generation BIGINT NOT NULL CHECK (generation > 0),
8
+ coverage_from TIMESTAMPTZ NOT NULL,
9
+ coverage_through TIMESTAMPTZ NOT NULL,
10
+ covered_message_count INTEGER NOT NULL CHECK (covered_message_count BETWEEN 1 AND 100),
11
+ source_event_ids TEXT[] NOT NULL CHECK (cardinality(source_event_ids) <= 48),
12
+ checkpoint_text TEXT NOT NULL CHECK (length(checkpoint_text) <= 16000),
13
+ source_digest TEXT NOT NULL CHECK (source_digest ~ '^[a-f0-9]{64}$'),
14
+ rebuildable BOOLEAN NOT NULL DEFAULT TRUE,
15
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
16
+ PRIMARY KEY (namespace,conversation_key),
17
+ CHECK (coverage_through >= coverage_from)
18
+ );
@@ -0,0 +1,34 @@
1
+ -- Optional vector schema (Gate migration 0025, generalised). Applied by enableMemoryVectorIndex() only
2
+ -- when the vector extension is already installed; without it every recall degrades to exact + lexical.
3
+ DO $agent_vector_check$
4
+ BEGIN
5
+ IF NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname='vector') THEN
6
+ RAISE EXCEPTION 'agent_memory_vector_extension_required';
7
+ END IF;
8
+ END
9
+ $agent_vector_check$;
10
+
11
+ CREATE TABLE agent_memory_embeddings (
12
+ memory_id UUID NOT NULL REFERENCES agent_memory_entries(memory_id) ON DELETE CASCADE,
13
+ namespace TEXT NOT NULL CHECK (length(namespace) BETWEEN 1 AND 128),
14
+ scope_kind TEXT NOT NULL CHECK (scope_kind ~ '^[a-z][a-z0-9_]{0,31}$'),
15
+ scope_key TEXT NOT NULL CHECK (length(scope_key) BETWEEN 1 AND 256),
16
+ source_revision BIGINT NOT NULL CHECK (source_revision > 0),
17
+ provider_id TEXT NOT NULL CHECK (length(provider_id) BETWEEN 1 AND 96),
18
+ model_id TEXT NOT NULL CHECK (length(model_id) BETWEEN 1 AND 200),
19
+ dimensions INTEGER NOT NULL CHECK (dimensions BETWEEN 1 AND 16000),
20
+ normalization TEXT NOT NULL CHECK (normalization='l2_unit'),
21
+ index_version INTEGER NOT NULL CHECK (index_version > 0),
22
+ content_hash TEXT NOT NULL CHECK (content_hash ~ '^[a-f0-9]{64}$'),
23
+ embedding vector NOT NULL,
24
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
25
+ PRIMARY KEY (memory_id,provider_id,model_id,index_version),
26
+ CHECK (vector_dims(embedding)=dimensions)
27
+ );
28
+
29
+ -- Scope-first B-tree for exact filtered cosine scans. No global HNSW/ANN index is created: an
30
+ -- approximate index would need a separate filter-recall study.
31
+ CREATE INDEX idx_agent_memory_embeddings_scope
32
+ ON agent_memory_embeddings(
33
+ namespace,scope_kind,scope_key,provider_id,model_id,dimensions,index_version,memory_id
34
+ );
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
1
  {
2
2
  "name": "@rine-kit/agent-memory-postgres",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "PostgreSQL (+ optional pgvector) storage for @rine-kit/agent-memory: scoped entries, exact/lexical/semantic search, embedding index with budget, usage evidence, dreaming runs. Extracted from Gate; no KOOK dependency.",
6
+ "license": "MIT",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "migrations"
20
+ ],
21
+ "dependencies": {
22
+ "pg": "^8.13.0",
23
+ "zod": "^4.6.7",
24
+ "@rine-kit/agent-memory": "0.1.0"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "^26.6.5",
28
+ "@types/pg": "^8.11.10",
29
+ "tsx": "^4.23.15",
30
+ "typescript": "^5.9.2"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+ssh://git@github.com/ChristmasFox/rine-kit.git",
35
+ "directory": "packages/agent-memory-postgres"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc -p tsconfig.build.json",
39
+ "typecheck": "tsc -p tsconfig.json --noEmit",
40
+ "test": "tsx --test tests/**/*.test.ts",
41
+ "test:pg": "bash ../../scripts/with-pgvector.sh npx tsx --test \"tests/*.test.ts\""
42
+ }
6
43
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.