forked from NateBJones-Projects/OB1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
328 lines (292 loc) · 12 KB
/
Copy pathschema.sql
File metadata and controls
328 lines (292 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
-- OB1 Agent Memory
-- Runtime-neutral sidecar schema for governed agent recall/write-back.
--
-- This migration intentionally keeps public.thoughts as the durable content
-- table. Agent memory metadata, provenance, review, trace, and audit state
-- live in sidecar tables so existing OB1 capture/search behavior keeps working.
BEGIN;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'thoughts'
) THEN
RAISE EXCEPTION
'agent-memory requires public.thoughts. Run docs/01-getting-started.md first.';
END IF;
END $$;
CREATE TABLE IF NOT EXISTS public.agent_memories (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
thought_id UUID REFERENCES public.thoughts(id) ON DELETE SET NULL,
workspace_id TEXT NOT NULL,
project_id TEXT,
channel_kind TEXT,
channel_id TEXT,
channel_thread_id TEXT,
visibility TEXT NOT NULL DEFAULT 'project' CHECK (
visibility IN ('personal', 'channel', 'project', 'workspace', 'organization')
),
memory_type TEXT NOT NULL CHECK (
memory_type IN (
'decision',
'output',
'lesson',
'constraint',
'open_question',
'failure',
'artifact_reference',
'work_log'
)
),
summary TEXT NOT NULL,
content TEXT NOT NULL,
lifecycle_status TEXT NOT NULL DEFAULT 'active' CHECK (
lifecycle_status IN ('active', 'stale', 'superseded', 'disputed', 'rejected')
),
provenance_status TEXT NOT NULL DEFAULT 'generated' CHECK (
provenance_status IN (
'observed',
'inferred',
'user_confirmed',
'imported',
'generated',
'superseded',
'disputed'
)
),
confidence NUMERIC(3,2) NOT NULL DEFAULT 0.50 CHECK (confidence >= 0 AND confidence <= 1),
created_by TEXT NOT NULL DEFAULT 'agent' CHECK (created_by IN ('user', 'agent', 'system', 'import')),
runtime_name TEXT,
runtime_version TEXT,
provider TEXT,
model TEXT,
task_id TEXT,
flow_id TEXT,
can_use_as_instruction BOOLEAN NOT NULL DEFAULT false,
can_use_as_evidence BOOLEAN NOT NULL DEFAULT true,
requires_user_confirmation BOOLEAN NOT NULL DEFAULT true,
review_status TEXT NOT NULL DEFAULT 'pending' CHECK (
review_status IN (
'pending',
'confirmed',
'evidence_only',
'restricted',
'rejected',
'stale',
'merged'
)
),
last_confirmed_at TIMESTAMPTZ,
stale_after TIMESTAMPTZ,
idempotency_key TEXT,
content_hash TEXT,
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (
can_use_as_instruction = false
OR provenance_status IN ('user_confirmed', 'imported')
)
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_agent_memories_idempotency_key
ON public.agent_memories (idempotency_key)
WHERE idempotency_key IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_agent_memories_scope
ON public.agent_memories (workspace_id, project_id, visibility);
CREATE INDEX IF NOT EXISTS idx_agent_memories_review
ON public.agent_memories (review_status, lifecycle_status, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_agent_memories_runtime_task
ON public.agent_memories (runtime_name, task_id, flow_id);
CREATE INDEX IF NOT EXISTS idx_agent_memories_content_hash
ON public.agent_memories (workspace_id, content_hash)
WHERE content_hash IS NOT NULL;
CREATE TABLE IF NOT EXISTS public.agent_memory_source_refs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
memory_id UUID NOT NULL REFERENCES public.agent_memories(id) ON DELETE CASCADE,
source_kind TEXT NOT NULL,
uri TEXT,
title TEXT,
source_timestamp TIMESTAMPTZ,
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_agent_memory_source_refs_memory
ON public.agent_memory_source_refs (memory_id);
CREATE TABLE IF NOT EXISTS public.agent_memory_artifacts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
memory_id UUID NOT NULL REFERENCES public.agent_memories(id) ON DELETE CASCADE,
artifact_kind TEXT NOT NULL,
uri TEXT NOT NULL,
description TEXT,
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_agent_memory_artifacts_memory
ON public.agent_memory_artifacts (memory_id);
CREATE TABLE IF NOT EXISTS public.agent_memory_relations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
from_memory_id UUID NOT NULL REFERENCES public.agent_memories(id) ON DELETE CASCADE,
to_memory_id UUID NOT NULL REFERENCES public.agent_memories(id) ON DELETE CASCADE,
relation TEXT NOT NULL CHECK (
relation IN ('related_to', 'supersedes', 'superseded_by', 'conflicts_with', 'merged_into')
),
confidence NUMERIC(3,2) DEFAULT 0.50 CHECK (confidence IS NULL OR (confidence >= 0 AND confidence <= 1)),
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (from_memory_id, to_memory_id, relation),
CHECK (from_memory_id <> to_memory_id)
);
CREATE TABLE IF NOT EXISTS public.agent_memory_review_actions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
memory_id UUID NOT NULL REFERENCES public.agent_memories(id) ON DELETE CASCADE,
action TEXT NOT NULL CHECK (
action IN (
'confirm',
'edit',
'evidence_only',
'restrict_scope',
'mark_stale',
'merge',
'reject',
'dispute',
'supersede'
)
),
actor_id TEXT,
actor_label TEXT,
notes TEXT,
before JSONB,
after JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_agent_memory_review_actions_memory
ON public.agent_memory_review_actions (memory_id, created_at DESC);
CREATE TABLE IF NOT EXISTS public.agent_memory_recall_traces (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
request_id UUID NOT NULL DEFAULT gen_random_uuid(),
workspace_id TEXT NOT NULL,
project_id TEXT,
runtime_name TEXT,
runtime_version TEXT,
task_id TEXT,
flow_id TEXT,
channel_kind TEXT,
channel_id TEXT,
query TEXT NOT NULL,
schema_version TEXT NOT NULL,
request_payload JSONB NOT NULL DEFAULT '{}'::jsonb,
response_policy JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (request_id)
);
CREATE INDEX IF NOT EXISTS idx_agent_memory_recall_traces_scope
ON public.agent_memory_recall_traces (workspace_id, project_id, created_at DESC);
CREATE TABLE IF NOT EXISTS public.agent_memory_recall_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
trace_id UUID NOT NULL REFERENCES public.agent_memory_recall_traces(id) ON DELETE CASCADE,
memory_id UUID NOT NULL REFERENCES public.agent_memories(id) ON DELETE CASCADE,
rank INTEGER NOT NULL,
similarity NUMERIC(5,4),
ranking_score NUMERIC(7,4),
returned BOOLEAN NOT NULL DEFAULT true,
used BOOLEAN,
ignored_reason TEXT,
use_policy_snapshot JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (trace_id, memory_id)
);
CREATE INDEX IF NOT EXISTS idx_agent_memory_recall_items_trace
ON public.agent_memory_recall_items (trace_id, rank);
CREATE TABLE IF NOT EXISTS public.agent_memory_audit_events (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
event_type TEXT NOT NULL CHECK (
event_type IN (
'recall_requested',
'memory_returned',
'memory_used',
'memory_ignored',
'memory_written',
'memory_confirmed',
'memory_edited',
'memory_rejected',
'memory_superseded',
'memory_disputed'
)
),
workspace_id TEXT,
project_id TEXT,
memory_id UUID REFERENCES public.agent_memories(id) ON DELETE SET NULL,
trace_id UUID REFERENCES public.agent_memory_recall_traces(id) ON DELETE SET NULL,
actor_kind TEXT NOT NULL DEFAULT 'system' CHECK (actor_kind IN ('user', 'agent', 'system', 'import')),
actor_label TEXT,
runtime_name TEXT,
task_id TEXT,
payload JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_agent_memory_audit_scope
ON public.agent_memory_audit_events (workspace_id, project_id, created_at DESC);
CREATE OR REPLACE FUNCTION public.agent_memories_set_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_agent_memories_updated_at ON public.agent_memories;
CREATE TRIGGER trg_agent_memories_updated_at
BEFORE UPDATE ON public.agent_memories
FOR EACH ROW EXECUTE FUNCTION public.agent_memories_set_updated_at();
CREATE OR REPLACE FUNCTION public.agent_memory_hash_text(p_content TEXT)
RETURNS TEXT
LANGUAGE plpgsql
IMMUTABLE
AS $$
BEGIN
RETURN encode(sha256(convert_to(lower(trim(regexp_replace(coalesce(p_content, ''), '\s+', ' ', 'g'))), 'UTF8')), 'hex');
END;
$$;
ALTER TABLE public.agent_memories ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.agent_memory_source_refs ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.agent_memory_artifacts ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.agent_memory_relations ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.agent_memory_review_actions ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.agent_memory_recall_traces ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.agent_memory_recall_items ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.agent_memory_audit_events ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS agent_memories_service_role_all ON public.agent_memories;
CREATE POLICY agent_memories_service_role_all ON public.agent_memories
FOR ALL TO service_role USING (true) WITH CHECK (true);
DROP POLICY IF EXISTS agent_memory_source_refs_service_role_all ON public.agent_memory_source_refs;
CREATE POLICY agent_memory_source_refs_service_role_all ON public.agent_memory_source_refs
FOR ALL TO service_role USING (true) WITH CHECK (true);
DROP POLICY IF EXISTS agent_memory_artifacts_service_role_all ON public.agent_memory_artifacts;
CREATE POLICY agent_memory_artifacts_service_role_all ON public.agent_memory_artifacts
FOR ALL TO service_role USING (true) WITH CHECK (true);
DROP POLICY IF EXISTS agent_memory_relations_service_role_all ON public.agent_memory_relations;
CREATE POLICY agent_memory_relations_service_role_all ON public.agent_memory_relations
FOR ALL TO service_role USING (true) WITH CHECK (true);
DROP POLICY IF EXISTS agent_memory_review_actions_service_role_all ON public.agent_memory_review_actions;
CREATE POLICY agent_memory_review_actions_service_role_all ON public.agent_memory_review_actions
FOR ALL TO service_role USING (true) WITH CHECK (true);
DROP POLICY IF EXISTS agent_memory_recall_traces_service_role_all ON public.agent_memory_recall_traces;
CREATE POLICY agent_memory_recall_traces_service_role_all ON public.agent_memory_recall_traces
FOR ALL TO service_role USING (true) WITH CHECK (true);
DROP POLICY IF EXISTS agent_memory_recall_items_service_role_all ON public.agent_memory_recall_items;
CREATE POLICY agent_memory_recall_items_service_role_all ON public.agent_memory_recall_items
FOR ALL TO service_role USING (true) WITH CHECK (true);
DROP POLICY IF EXISTS agent_memory_audit_events_service_role_all ON public.agent_memory_audit_events;
CREATE POLICY agent_memory_audit_events_service_role_all ON public.agent_memory_audit_events
FOR ALL TO service_role USING (true) WITH CHECK (true);
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memories TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memory_source_refs TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memory_artifacts TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memory_relations TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memory_review_actions TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memory_recall_traces TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memory_recall_items TO service_role;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE public.agent_memory_audit_events TO service_role;
GRANT EXECUTE ON FUNCTION public.agent_memory_hash_text(TEXT) TO service_role;
NOTIFY pgrst, 'reload schema';
COMMIT;