MikelWL commited on
Commit
29dd5f0
·
1 Parent(s): ab31a88

Docs: align persistence contract

Browse files
Files changed (2) hide show
  1. docs/README.md +1 -0
  2. docs/persistence.md +348 -0
docs/README.md CHANGED
@@ -4,6 +4,7 @@ These short guides are all you need to extend the AI Survey Simulator:
4
 
5
  - `overview.md` — architecture summary, major components, and repository map.
6
  - `development.md` — setup, runtime instructions, and implementation guidelines.
 
7
  - `roadmap.md` — current status and prioritized future work.
8
 
9
  Keep documentation lean: update the relevant file when behavior changes or priorities shift.
 
4
 
5
  - `overview.md` — architecture summary, major components, and repository map.
6
  - `development.md` — setup, runtime instructions, and implementation guidelines.
7
+ - `persistence.md` — persistent run history + persona CRUD design (HF `/data` now, Railway later).
8
  - `roadmap.md` — current status and prioritized future work.
9
 
10
  Keep documentation lean: update the relevant file when behavior changes or priorities shift.
docs/persistence.md ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Persistence Design (HF `/data` now, Railway later)
2
+
3
+ This document describes a storage design that enables:
4
+
5
+ - **Run history** for all three modes (AI↔AI, Human↔AI, Upload Text): chronological list → click → **replay read-only**.
6
+ - **Persona CRUD** (create/update/delete) with **versioning**, while preserving historical run replay.
7
+ - **Portability** across deployment targets:
8
+ - Hugging Face Spaces: persistent filesystem under `/data`
9
+ - Future: Railway (likely Postgres), without rewriting app/business logic
10
+
11
+ This is intentionally **implementation-agnostic** at the interface level and **implementation-specific** only at the backend adapter level (SQLite first).
12
+
13
+ ---
14
+
15
+ ## Goals
16
+
17
+ 1. **Durable run replay**
18
+ - After a run ends, the transcript + analysis outputs are persisted and can be reloaded after restart/redeploy.
19
+ - Reloaded runs are **read-only**: no WebSocket streaming, no “resume”, no edits.
20
+
21
+ 2. **Stable historical fidelity**
22
+ - Reloading a run shows the same transcript and analysis output that was originally produced.
23
+ - Runs do **not drift** if personas/system prompts are edited later.
24
+
25
+ 3. **One shared history**
26
+ - This app is currently used by a team evaluating the tool, so history is **global/shared**, not per-user.
27
+
28
+ 4. **Provider portability**
29
+ - HF → Railway should be a storage-backend swap (SQLite-on-volume → Postgres), not a rewrite.
30
+
31
+ ---
32
+
33
+ ## Non-goals (for the first iteration)
34
+
35
+ - Live persistence during streaming (we can add later). For now: **persist at end-of-run only**.
36
+ - “Resume” or “continue” a prior run.
37
+ - Fine-grained multi-user auth and per-user histories.
38
+ - Full-text search, tagging, sharing links, etc.
39
+
40
+ ---
41
+
42
+ ## What “persistent storage” means in this project
43
+
44
+ The current runtime holds critical data in memory during a session:
45
+
46
+ - Transcript messages: `ConversationAI/backend/api/conversation_service.py` maintains `self.transcripts[conversation_id]`.
47
+ - Analysis results: after completion, `_run_resource_agent()` broadcasts `resource_agent_result` back to the UI.
48
+
49
+ Persistence means:
50
+
51
+ 1. When a session ends as a **sealed run** (conversation finished and analysis succeeded), we write a **Run record** to durable storage:
52
+ - transcript (messages)
53
+ - analysis outputs (resource agent JSON, evidence catalog, schema versions)
54
+ - configuration snapshot (LLM backend/model/params, selected personas, prompt additions, etc.)
55
+ - persona snapshots for historical fidelity (see below)
56
+ 2. We expose APIs to:
57
+ - list prior runs (chronological)
58
+ - fetch a specific run by `run_id`
59
+ 3. The UI can then “rehydrate” a panel from those persisted artifacts and render it read-only.
60
+
61
+ ---
62
+
63
+ ## Key design principles
64
+
65
+ ### 1) Runs are immutable once sealed
66
+
67
+ We treat a “Run” as a record of what happened.
68
+
69
+ - A run can transition from “active” → “sealed”
70
+ - Once sealed, it is **read-only** for all consumers
71
+ - The UI is allowed to render it, export it, and inspect it
72
+
73
+ This makes the system easy to reason about and prevents accidental data drift.
74
+
75
+ ### 2) Personas are mutable, but runs never drift
76
+
77
+ Personas and system prompts are editable over time (CRUD).
78
+ However, old runs must still open exactly as they were.
79
+
80
+ To guarantee this:
81
+
82
+ - We store a **persona snapshot** inside each run (the persona content used at runtime).
83
+ - Optionally, we also store a reference to the persona ID/version that the snapshot came from.
84
+
85
+ ### 3) Store blobs as JSON, but keep query fields as columns
86
+
87
+ For history lists and basic filtering, we want queryable columns (mode, status, timestamps).
88
+ For richer data (config, analysis output, persona content), JSON is fine and reduces schema churn.
89
+
90
+ ### 4) Storage adapter boundary (portability)
91
+
92
+ All application code should talk to a small interface (conceptually):
93
+
94
+ - `RunStore`
95
+ - `save_sealed_run(run_record)`
96
+ - `list_runs(mode?, limit?, offset?)`
97
+ - `get_run(run_id)`
98
+ - `PersonaStore`
99
+ - `list_personas(kind?, include_deleted?)`
100
+ - `get_persona(persona_id, version?)`
101
+ - `create_persona(payload)`
102
+ - `update_persona(persona_id, payload)` (creates a new version)
103
+ - `delete_persona(persona_id)` (soft delete)
104
+
105
+ HF and Railway differ only in the **implementation** of these interfaces.
106
+
107
+ ---
108
+
109
+ ## Storage backend choice: SQLite-first
110
+
111
+ SQLite is selected as the first implementation because it provides:
112
+
113
+ - Atomic writes and durability
114
+ - Indexes for fast run listing
115
+ - Natural support for persona versioning
116
+ - A smooth conceptual migration path to Postgres later
117
+
118
+ ### Database location
119
+
120
+ - Hugging Face Spaces: `DB_PATH=/data/converta/converta.db`
121
+ - Local dev fallback: `DB_PATH=ConversationAI/.localdata/converta.db` (or similar)
122
+
123
+ The storage module should create parent directories if missing.
124
+
125
+ Important: `DB_PATH` (env) is the canonical source of truth for storage location. Any legacy config values
126
+ (e.g. `config/default_config.yaml` SQLite path) should be treated as non-authoritative for persistence.
127
+
128
+ ---
129
+
130
+ ## Proposed SQLite schema (v1)
131
+
132
+ ### `runs` — top-level history entries
133
+
134
+ - `run_id TEXT PRIMARY KEY` (UUID)
135
+ - `mode TEXT NOT NULL` (`ai_to_ai|human_to_ai|text_analysis`)
136
+ - `status TEXT NOT NULL` (`completed` in v1; reserve `aborted|error` for later)
137
+ - `created_at TEXT NOT NULL` (ISO timestamp)
138
+ - `ended_at TEXT NOT NULL` (ISO timestamp)
139
+ - `title TEXT` (optional)
140
+ - `input_summary TEXT` (optional: filename/source label for text analysis)
141
+ - `config_json TEXT NOT NULL` (JSON blob)
142
+ - `sealed_at TEXT NOT NULL` (ISO timestamp; equals `ended_at` in v1)
143
+
144
+ Indexes:
145
+
146
+ - `INDEX runs_mode_created_at ON runs(mode, created_at DESC)`
147
+ - `INDEX runs_created_at ON runs(created_at DESC)`
148
+
149
+ ### `run_messages` — transcripts
150
+
151
+ - `run_id TEXT NOT NULL` (FK → `runs.run_id`)
152
+ - `message_index INTEGER NOT NULL`
153
+ - `role TEXT NOT NULL`
154
+ - `persona_label TEXT` (optional)
155
+ - `content TEXT NOT NULL`
156
+ - `timestamp TEXT` (ISO timestamp)
157
+
158
+ Primary key:
159
+
160
+ - `(run_id, message_index)`
161
+
162
+ ### `run_analyses` — analysis outputs per run
163
+
164
+ - `run_id TEXT NOT NULL` (FK)
165
+ - `analysis_key TEXT NOT NULL` (e.g. `resource_agent_v2`)
166
+ - `schema_version TEXT`
167
+ - `prompt_version TEXT`
168
+ - `result_json TEXT NOT NULL` (full JSON blob, including `evidence_catalog`)
169
+
170
+ Primary key:
171
+
172
+ - `(run_id, analysis_key)`
173
+
174
+ ### `personas` — stable identity and lifecycle
175
+
176
+ - `persona_id TEXT PRIMARY KEY` (UUID)
177
+ - `kind TEXT NOT NULL` (`surveyor|patient`)
178
+ - `name TEXT NOT NULL`
179
+ - `is_deleted INTEGER NOT NULL DEFAULT 0`
180
+ - `created_at TEXT NOT NULL`
181
+ - `updated_at TEXT NOT NULL`
182
+
183
+ ### `persona_versions` — append-only versions
184
+
185
+ - `persona_id TEXT NOT NULL` (FK)
186
+ - `version_id TEXT NOT NULL` (UUID)
187
+ - `created_at TEXT NOT NULL`
188
+ - `content_json TEXT NOT NULL` (persona definition + system prompt)
189
+
190
+ Primary key:
191
+
192
+ - `(persona_id, version_id)`
193
+
194
+ ### `run_persona_snapshots` — prevent drift
195
+
196
+ - `run_id TEXT NOT NULL` (FK)
197
+ - `role TEXT NOT NULL` (`surveyor|patient`)
198
+ - `persona_id TEXT` (nullable)
199
+ - `persona_version_id TEXT` (nullable)
200
+ - `snapshot_json TEXT NOT NULL`
201
+
202
+ Primary key:
203
+
204
+ - `(run_id, role)`
205
+
206
+ ---
207
+
208
+ ## What gets stored in `config_json` (recommended)
209
+
210
+ `config_json` should allow exact replay and debugging:
211
+
212
+ - LLM settings:
213
+ - `llm_backend`, `host`, `model`
214
+ - `timeout`, `max_retries`, `retry_delay`
215
+ - any generation params used (temperature, max_tokens, top_p, etc.)
216
+ - Mode-specific:
217
+ - AI↔AI: surveyor/patient persona IDs (and/or names), prompt additions
218
+ - Human↔AI: same + human mode flags
219
+ - Text analysis: `source_name`, optional file metadata (original filename, sha256)
220
+ - App versions:
221
+ - `analysis_prompt_version` and `schema_version` (duplicated in `run_analyses` is fine)
222
+ - optional git commit SHA (if available at runtime)
223
+
224
+ ---
225
+
226
+ ## Integration points (where persistence is hooked in)
227
+
228
+ This section maps “what to save” to the current code.
229
+
230
+ ### 1) AI↔AI and Human↔AI runs
231
+
232
+ Source of truth today:
233
+
234
+ - Transcript: `ConversationAI/backend/api/conversation_service.py` (`self.transcripts[conversation_id]`)
235
+ - Analysis: `_run_resource_agent(conversation_id)` broadcasts `resource_agent_result`
236
+
237
+ End-of-run save flow (conceptual):
238
+
239
+ 1. Run completes (or human chat ends)
240
+ 2. Resource agent analysis completes successfully
241
+ 3. Build a `RunRecord`:
242
+ - `run_id` (use the `conversation_id` or generate a new UUID; recommended: new `run_id` distinct from WS id)
243
+ - `mode`, `status`, timestamps
244
+ - `messages[]` from `self.transcripts`
245
+ - `analyses["resource_agent_v2"]` from the parsed JSON
246
+ - `persona snapshots` for surveyor/patient content actually used
247
+ 4. `RunStore.save_sealed_run(run_record)` writes the run to SQLite in a transaction.
248
+ 5. Memory cleanup proceeds as today.
249
+
250
+ Notes:
251
+
252
+ - Because we’re “end-only”, if the process dies mid-run, that run is lost. This is accepted for v1.
253
+ - If a run is stopped/aborted (e.g. user presses Stop in AI↔AI), it is not a sealed run and is not persisted in v1.
254
+
255
+ ### 2) Text analysis (“Upload Text”)
256
+
257
+ Source of truth today:
258
+
259
+ - Transcript is derived by parsing uploaded/pasted text into message-like units.
260
+ - Resource agent analysis is run and returned.
261
+
262
+ End-of-analysis save flow:
263
+
264
+ - Store as `mode=text_analysis` with `messages[]` using role `transcript` (or the derived roles if present).
265
+ - Store analysis output the same way as live conversations.
266
+ - Because Upload Text is analysis-driven, “sealed” effectively means “analysis succeeded”; if analysis fails, do not persist.
267
+
268
+ ---
269
+
270
+ ## API design for history and persona CRUD (v1)
271
+
272
+ ### Runs
273
+
274
+ - `GET /api/runs?mode=ai_to_ai&limit=50&offset=0`
275
+ - Returns run summaries: `run_id`, `mode`, `status`, `created_at`, `ended_at`, `title`, `input_summary`
276
+ - `GET /api/runs/{run_id}`
277
+ - Returns the full run record: transcript + analysis JSON + config snapshot
278
+
279
+ ### Personas
280
+
281
+ - `GET /api/personas` (already exists for current YAML personas; will evolve to use DB + defaults)
282
+ - `POST /api/personas`
283
+ - `PUT /api/personas/{persona_id}` (creates a new version)
284
+ - `DELETE /api/personas/{persona_id}` (soft delete)
285
+ - (optional) `GET /api/personas/{persona_id}/versions`
286
+
287
+ Important: keep existing YAML personas as “defaults”:
288
+
289
+ - On first run, load YAML defaults if DB is empty (or treat YAML as seed data).
290
+ - DB becomes the writable source; YAML remains the baseline defaults.
291
+
292
+ ---
293
+
294
+ ## UI behavior for read-only history
295
+
296
+ The UI should treat historical sessions as “render only”:
297
+
298
+ - No WebSocket connection
299
+ - Start/stop buttons disabled
300
+ - Transcript and analysis panels populated from `GET /api/runs/{run_id}`
301
+ - Export actions should be backed by server-canonical run data (avoid treating client-hydrated payloads as the source of truth)
302
+
303
+ Recommended UI structure:
304
+
305
+ - Add a “History” view per mode (or a unified history with filters)
306
+ - Clicking a run loads it into that panel, sets a `readOnly=true` flag, and renders accordingly
307
+
308
+ ---
309
+
310
+ ## Migration plan: SQLite → Postgres (Railway)
311
+
312
+ ### What stays the same
313
+
314
+ - The `RunStore` / `PersonaStore` interface
315
+ - The external API endpoints and payload shapes
316
+ - The UI behavior (list + get + render)
317
+ - The logical schema (tables/entities)
318
+
319
+ ### What changes
320
+
321
+ - Replace `SQLiteRunStore/SQLitePersonaStore` with `PostgresRunStore/PostgresPersonaStore`
322
+ - DB connection config:
323
+ - `DATABASE_URL` (Railway Postgres)
324
+ - migrations managed via Alembic (recommended for Postgres)
325
+
326
+ ### Suggested migration path
327
+
328
+ 1. Introduce the store interface now and keep it backend-agnostic.
329
+ 2. Implement SQLite store now (HF + local).
330
+ 3. When ready for Railway:
331
+ - Implement Postgres store behind the same interface
332
+ - Add migrations
333
+ - Add a one-time “export/import” command:
334
+ - export all SQLite runs/personas to JSON
335
+ - import into Postgres
336
+
337
+ ---
338
+
339
+ ## Verification checklist (for v1)
340
+
341
+ 1. Run an AI↔AI session to completion and confirm a row appears in `runs` with `status=completed`.
342
+ 2. Reload history via API and confirm:
343
+ - transcript message count and ordering match the original UI
344
+ - analysis boxes match the original output
345
+ 3. Edit a persona, run a new session, and confirm:
346
+ - the new run uses the updated persona
347
+ - the old run still replays with the old persona snapshot (no drift)
348
+ 4. Restart the container/app and confirm history remains available.