-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenoun_store.py
More file actions
executable file
·392 lines (324 loc) · 14 KB
/
renoun_store.py
File metadata and controls
executable file
·392 lines (324 loc) · 14 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python3
"""
ReNoUn Longitudinal Storage Manager.
Manages $RENOUN_DATA_DIR/history/ directory for storing, querying, and exporting
analysis results over time.
Usage:
python3 renoun_store.py save --result result.json --name "session_12" --domain therapy --tags weekly,client_a
python3 renoun_store.py query --from 2026-01-01 --to 2026-03-01
python3 renoun_store.py query --constellation CLOSED_LOOP
python3 renoun_store.py query --dhs-below 0.45
python3 renoun_store.py query --domain therapy --tag client_a
python3 renoun_store.py trend --domain therapy --metric dhs
python3 renoun_store.py export --domain therapy --format csv --output trend.csv
python3 renoun_store.py list
Patent Pending #63/923,592 — core engine is proprietary.
"""
import os
import sys
import json
import csv as csv_module
import argparse
from pathlib import Path
from typing import Dict, Any, List, Optional
from datetime import datetime, date
# Use persistent volume if available (Railway), fall back to home directory
_DATA_DIR = os.environ.get("RENOUN_DATA_DIR", str(Path.home() / ".renoun"))
HISTORY_DIR = Path(_DATA_DIR) / "history"
INDEX_FILE = HISTORY_DIR / "index.json"
# ---------------------------------------------------------------------------
# Index Management
# ---------------------------------------------------------------------------
def ensure_history_dir():
"""Create the history directory if it doesn't exist."""
HISTORY_DIR.mkdir(parents=True, exist_ok=True)
def load_index() -> Dict[str, Any]:
"""Load the session index."""
if INDEX_FILE.exists():
return json.loads(INDEX_FILE.read_text(encoding="utf-8"))
return {"sessions": [], "created": datetime.utcnow().isoformat() + "Z"}
def save_index(index: Dict[str, Any]):
"""Save the session index."""
index["updated"] = datetime.utcnow().isoformat() + "Z"
INDEX_FILE.write_text(json.dumps(index, indent=2, default=str), encoding="utf-8")
# ---------------------------------------------------------------------------
# Save
# ---------------------------------------------------------------------------
def save_result(result_path: str, session_name: str, domain: str = "",
tags: List[str] = None) -> Dict[str, Any]:
"""Save an analysis result to the history store."""
ensure_history_dir()
# Load the result
result_data = json.loads(Path(result_path).read_text(encoding="utf-8"))
timestamp = datetime.utcnow()
filename = f"{timestamp.strftime('%Y-%m-%d')}_{session_name}.json"
# Build the stored record
record = {
"session_name": session_name,
"timestamp": timestamp.isoformat() + "Z",
"source": result_path,
"domain": domain,
"tags": tags or [],
"turn_count": (
result_data.get("_meta", {}).get("turn_count")
or result_data.get("turn_count")
or len(result_data.get("novelty_items", []))
or 0
),
"dhs": result_data.get("dialectical_health", 0.0),
"loop_strength": result_data.get("loop_strength", 0.0),
"dominant_constellation": None,
"result": result_data,
}
# Extract dominant constellation — handle both analyze and health_check formats
constellations = result_data.get("constellations", [])
if constellations:
# Full analyze output: constellations is a list of {detected, confidence, ...}
dominant = max(constellations, key=lambda c: c.get("confidence", 0))
record["dominant_constellation"] = dominant.get("detected")
elif result_data.get("dominant_constellation"):
# Health check or simplified output: dominant_constellation is a string or dict
dc = result_data["dominant_constellation"]
if isinstance(dc, dict):
record["dominant_constellation"] = dc.get("pattern")
else:
record["dominant_constellation"] = dc
# Write the record
record_path = HISTORY_DIR / filename
record_path.write_text(json.dumps(record, indent=2, default=str), encoding="utf-8")
# Update index
index = load_index()
index["sessions"].append({
"session_name": session_name,
"filename": filename,
"timestamp": record["timestamp"],
"domain": domain,
"tags": tags or [],
"dhs": record["dhs"],
"loop_strength": record["loop_strength"],
"dominant_constellation": record["dominant_constellation"],
"turn_count": record["turn_count"],
})
save_index(index)
return {
"status": "saved",
"filename": filename,
"path": str(record_path),
"session_name": session_name,
"dhs": record["dhs"],
}
# ---------------------------------------------------------------------------
# Query
# ---------------------------------------------------------------------------
def query_sessions(
from_date: Optional[str] = None,
to_date: Optional[str] = None,
domain: Optional[str] = None,
tag: Optional[str] = None,
constellation: Optional[str] = None,
dhs_below: Optional[float] = None,
dhs_above: Optional[float] = None,
) -> List[Dict[str, Any]]:
"""Query sessions from the index with optional filters."""
index = load_index()
results = index.get("sessions", [])
if from_date:
from_dt = datetime.fromisoformat(from_date)
results = [r for r in results if datetime.fromisoformat(r["timestamp"].rstrip("Z")) >= from_dt]
if to_date:
to_dt = datetime.fromisoformat(to_date)
results = [r for r in results if datetime.fromisoformat(r["timestamp"].rstrip("Z")) <= to_dt]
if domain:
results = [r for r in results if r.get("domain", "").lower() == domain.lower()]
if tag:
results = [r for r in results if tag in r.get("tags", [])]
if constellation:
results = [r for r in results if r.get("dominant_constellation") == constellation.upper()]
if dhs_below is not None:
results = [r for r in results if r.get("dhs", 1.0) < dhs_below]
if dhs_above is not None:
results = [r for r in results if r.get("dhs", 0.0) > dhs_above]
# Sort by timestamp
results.sort(key=lambda r: r.get("timestamp", ""))
return results
# ---------------------------------------------------------------------------
# Trend
# ---------------------------------------------------------------------------
def compute_trend(
domain: Optional[str] = None,
metric: str = "dhs",
from_date: Optional[str] = None,
to_date: Optional[str] = None,
) -> Dict[str, Any]:
"""Compute trend data for a metric across sessions."""
sessions = query_sessions(from_date=from_date, to_date=to_date, domain=domain)
if not sessions:
return {"error": "No sessions found matching criteria", "count": 0}
values = []
for s in sessions:
if metric == "dhs":
values.append(s.get("dhs", 0.0))
elif metric == "loop":
values.append(s.get("loop_strength", 0.0))
else:
values.append(s.get(metric, 0.0))
# Slope calculation
if len(values) >= 2:
slope = (values[-1] - values[0]) / (len(values) - 1)
else:
slope = 0.0
# Constellation distribution
const_freq: Dict[str, int] = {}
for s in sessions:
c = s.get("dominant_constellation")
if c:
const_freq[c] = const_freq.get(c, 0) + 1
return {
"metric": metric,
"domain": domain,
"session_count": len(sessions),
"date_range": {
"from": sessions[0].get("timestamp"),
"to": sessions[-1].get("timestamp"),
},
"values": [round(v, 3) for v in values],
"labels": [s.get("session_name", "") for s in sessions],
"min": round(min(values), 3),
"max": round(max(values), 3),
"mean": round(sum(values) / len(values), 3),
"slope": round(slope, 4),
"trend": "improving" if slope > 0.02 else ("declining" if slope < -0.02 else "stable"),
"constellation_distribution": const_freq,
"sessions": [
{
"name": s.get("session_name"),
"date": s.get("timestamp", "")[:10],
"dhs": s.get("dhs"),
"loop": s.get("loop_strength"),
"constellation": s.get("dominant_constellation"),
}
for s in sessions
],
}
# ---------------------------------------------------------------------------
# Export
# ---------------------------------------------------------------------------
def export_data(
domain: Optional[str] = None,
from_date: Optional[str] = None,
to_date: Optional[str] = None,
fmt: str = "csv",
output_path: Optional[str] = None,
) -> str:
"""Export session data to CSV or JSON."""
sessions = query_sessions(from_date=from_date, to_date=to_date, domain=domain)
if fmt == "json":
data = json.dumps(sessions, indent=2, default=str)
elif fmt == "csv":
if not sessions:
data = "No sessions found"
else:
from io import StringIO
output = StringIO()
fields = ["session_name", "timestamp", "domain", "dhs", "loop_strength",
"dominant_constellation", "turn_count", "tags"]
writer = csv_module.DictWriter(output, fieldnames=fields, extrasaction="ignore")
writer.writeheader()
for s in sessions:
row = dict(s)
row["tags"] = ";".join(row.get("tags", []))
writer.writerow(row)
data = output.getvalue()
else:
data = json.dumps(sessions, indent=2, default=str)
if output_path:
Path(output_path).write_text(data, encoding="utf-8")
return f"Exported {len(sessions)} sessions to {output_path}"
return data
# ---------------------------------------------------------------------------
# List
# ---------------------------------------------------------------------------
def list_sessions() -> List[Dict[str, Any]]:
"""List all stored sessions (summary view)."""
index = load_index()
sessions = index.get("sessions", [])
return [
{
"name": s.get("session_name"),
"date": s.get("timestamp", "")[:10],
"domain": s.get("domain", ""),
"dhs": s.get("dhs"),
"constellation": s.get("dominant_constellation"),
"turns": s.get("turn_count"),
}
for s in sessions
]
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(description="ReNoUn History Store")
subparsers = parser.add_subparsers(dest="command")
# Save
save_parser = subparsers.add_parser("save", help="Save an analysis result")
save_parser.add_argument("--result", "-r", required=True, help="Result JSON file path")
save_parser.add_argument("--name", "-n", required=True, help="Session name")
save_parser.add_argument("--domain", "-d", default="", help="Domain tag (e.g., therapy, podcast)")
save_parser.add_argument("--tags", "-t", default="", help="Comma-separated tags")
# Query
query_parser = subparsers.add_parser("query", help="Query stored sessions")
query_parser.add_argument("--from", dest="from_date", help="Start date (YYYY-MM-DD)")
query_parser.add_argument("--to", dest="to_date", help="End date (YYYY-MM-DD)")
query_parser.add_argument("--domain", "-d", help="Filter by domain")
query_parser.add_argument("--tag", help="Filter by tag")
query_parser.add_argument("--constellation", help="Filter by constellation type")
query_parser.add_argument("--dhs-below", type=float, help="Filter DHS below threshold")
query_parser.add_argument("--dhs-above", type=float, help="Filter DHS above threshold")
# Trend
trend_parser = subparsers.add_parser("trend", help="Compute metric trends")
trend_parser.add_argument("--domain", "-d", help="Filter by domain")
trend_parser.add_argument("--metric", "-m", default="dhs", help="Metric to trend (dhs, loop)")
trend_parser.add_argument("--from", dest="from_date", help="Start date")
trend_parser.add_argument("--to", dest="to_date", help="End date")
# Export
export_parser = subparsers.add_parser("export", help="Export session data")
export_parser.add_argument("--domain", "-d", help="Filter by domain")
export_parser.add_argument("--from", dest="from_date", help="Start date")
export_parser.add_argument("--to", dest="to_date", help="End date")
export_parser.add_argument("--format", dest="fmt", choices=["csv", "json"], default="csv")
export_parser.add_argument("--output", "-o", help="Output file path")
# List
subparsers.add_parser("list", help="List all stored sessions")
args = parser.parse_args()
if args.command == "save":
tags = [t.strip() for t in args.tags.split(",") if t.strip()] if args.tags else []
result = save_result(args.result, args.name, args.domain, tags)
print(json.dumps(result, indent=2))
elif args.command == "query":
results = query_sessions(
from_date=args.from_date, to_date=args.to_date,
domain=args.domain, tag=args.tag,
constellation=args.constellation,
dhs_below=args.dhs_below, dhs_above=args.dhs_above,
)
print(json.dumps(results, indent=2, default=str))
elif args.command == "trend":
result = compute_trend(
domain=args.domain, metric=args.metric,
from_date=args.from_date, to_date=args.to_date,
)
print(json.dumps(result, indent=2, default=str))
elif args.command == "export":
output = export_data(
domain=args.domain,
from_date=args.from_date, to_date=args.to_date,
fmt=args.fmt, output_path=args.output,
)
print(output)
elif args.command == "list":
sessions = list_sessions()
print(json.dumps(sessions, indent=2, default=str))
else:
parser.print_help()
if __name__ == "__main__":
main()