Repository navigation
Expand file tree
/
Copy pathcm_sync.py
More file actions
1396 lines (1175 loc) · 60.8 KB
/
Copy pathcm_sync.py
File metadata and controls
1396 lines (1175 loc) · 60.8 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""cm-sync: sync claude-mem memory between devices through a git repo.
Each device publishes only the rows it created to <repo>/claude-mem/devices/<id>/,
plus tombstones for rows it deleted. Other devices import them through the
claude-mem worker (POST /api/import, DELETE routes), never into SQLite directly.
The one exception is work state (claude-mem's "My Work"): no worker route keeps
its timestamps, so it is inserted the way claude-mem's own cloud sync does,
with origin_device_id / origin_local_id.
"""
import argparse
import hashlib
import json
import os
import re
import shutil
import socket
import sqlite3
import subprocess
import sys
import time
import urllib.error
import urllib.request
import uuid
from pathlib import Path
FORMAT = 1
BATCH_BYTES = 4 * 1024 * 1024
DEVICES_DIR = Path("claude-mem") / "devices"
LEGACY_EXPORT_DIR = Path("claude-mem") / "macbook-pro"
DEFAULT_REMOTE = "git@github.com:your-username/claude-memory.git"
KINDS = ("sessions", "observations", "summaries", "prompts", "work_state")
# Kinds that go through POST /api/import
IMPORT_KINDS = ("sessions", "observations", "summaries", "prompts")
# Columns /api/import reads per table
COLUMNS = {
"sessions": (
"content_session_id", "memory_session_id", "project", "platform_source", "user_prompt",
"custom_title", "started_at", "started_at_epoch", "completed_at", "completed_at_epoch", "status",
),
"observations": (
"memory_session_id", "project", "text", "type", "title", "subtitle", "facts", "narrative",
"concepts", "files_read", "files_modified", "prompt_number", "discovery_tokens",
"agent_type", "agent_id", "created_at", "created_at_epoch",
),
"summaries": (
"memory_session_id", "project", "request", "investigated", "learned", "completed",
"next_steps", "files_read", "files_edited", "notes", "prompt_number", "discovery_tokens",
"created_at", "created_at_epoch",
),
"prompts": (
"content_session_id", "platform_source", "prompt_number", "prompt_text", "created_at", "created_at_epoch",
),
"work_state": ("local_id", "project", "list_name", "fields", "created_at", "created_at_epoch"),
}
# Same columns the worker dedups on in importSdkSession / importObservation / importSessionSummary / importUserPrompt
KEY_COLUMNS = {
"sessions": ("platform_source", "content_session_id"),
"observations": (
"memory_session_id", "text", "type", "title", "subtitle", "facts", "narrative", "concepts",
"files_read", "files_modified", "prompt_number", "agent_type", "agent_id", "created_at_epoch",
),
"summaries": (
"memory_session_id", "request", "investigated", "learned", "completed", "next_steps",
"files_read", "files_edited", "notes", "prompt_number", "created_at_epoch",
),
"prompts": ("content_session_id", "prompt_number"),
"work_state": ("local_id", "project", "list_name", "fields", "created_at_epoch"),
}
SCAN_COLUMNS = {
"sessions": ("user_prompt", "custom_title"),
"observations": ("text", "title", "subtitle", "facts", "narrative", "concepts", "files_read", "files_modified"),
"summaries": ("request", "investigated", "learned", "completed", "next_steps", "notes"),
"prompts": ("prompt_text",),
"work_state": ("list_name", "fields"),
}
SECRET_PATTERNS = {
"private-key": re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----"),
"aws-access-key": re.compile(r"\b(AKIA|ASIA)[0-9A-Z]{16}\b"),
"github-token": re.compile(r"\b(ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{30,}\b|\bgithub_pat_[A-Za-z0-9_]{40,}\b"),
"slack-token": re.compile(r"\bxox[abpr]-[A-Za-z0-9-]{10,}\b"),
"anthropic-key": re.compile(r"\bsk-ant-[A-Za-z0-9_-]{20,}\b"),
"api-key": re.compile(r"\bsk-[A-Za-z0-9_-]{32,}\b"),
"jwt": re.compile(r"\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b"),
"azure-key": re.compile(r"(AccountKey|SharedAccessKey)=[A-Za-z0-9+/=]{20,}"),
"connection-string-password": re.compile(r"(?i)\b(password|pwd)=[^;\s\"']{6,}"),
"assigned-secret": re.compile(r"(?i)\b(password|passwd|secret|token|api[_-]?key)\b\s*[:=]\s*[\"']?[^\s\"'<>]{8,}"),
}
# Rows derived from Markdown memory files; those files sync on their own
DERIVED_SOURCES = ('"source":"memory-file"', '"source":"memory-import"')
# work_state_entries columns the direct insert writes; claude-mem before v15 lacks the origin ones
WORK_STATE_COLUMNS = {
"project", "list_name", "fields", "created_at", "created_at_epoch",
"synced_at", "origin_device_id", "origin_local_id", "sync_rev",
}
# claude-mem's My Rules: rules are work-state tasks in this list; MY-RULES.md holds the "on" ones these set
RULES_LIST = "my-rules"
RULES_ACTIVE_BY = ("owner", "approved")
QUIET = False
WINDOWS = os.name == "nt"
class CmSyncError(Exception):
pass
STYLES = {"bold": 1, "dim": 2, "red": 31, "green": 32, "yellow": 33, "blue": 34, "magenta": 35, "cyan": 36}
STEPS = {
"Pulled": ("↓", "cyan"),
"Imported": ("↓", "blue"),
"Deleted": ("✗", "yellow"),
"Exported": ("↑", "magenta"),
"Published": ("↑", "green"),
"Rules": ("↻", "green"),
}
def colored(stream):
"""ANSI colours only on a terminal and never with NO_COLOR; on Windows only where the terminal announces itself."""
if "NO_COLOR" in os.environ or os.environ.get("TERM") == "dumb" or not stream.isatty():
return False
return not WINDOWS or bool(os.environ.get("WT_SESSION") or os.environ.get("TERM"))
def paint(text, *styles, stream=None):
if not styles or not text or not colored(stream or sys.stdout):
return str(text)
return f"\033[{';'.join(str(STYLES[s]) for s in styles)}m{text}\033[0m"
def info(msg):
if not QUIET:
print(msg)
def warn(msg, style="yellow"):
print(paint(msg, style, stream=sys.stderr), file=sys.stderr)
def step(label, text):
"""One line of sync progress: a coloured arrow and label, padded to one column."""
glyph, color = STEPS[label]
info(paint(f"{glyph} {label + ':':<12}", color, "bold") + text)
def counted(*parts):
"""`(n, noun)` pairs as `n noun · n noun`, leaving out the zero counts."""
shown = [f"{paint(n, 'bold')} {noun}" for n, noun in parts if n]
return paint(" · ", "dim").join(shown) or paint("nothing", "dim")
# ------------------------------------------------------------------ config
class Config:
def __init__(self, repo=None, state_dir=None):
home = Path.home()
default_data = home / ".claude-mem"
data_dir = os.environ.get("CLAUDE_MEM_DATA_DIR")
if not data_dir:
data_dir = read_json(default_data / "settings.json", {}).get("CLAUDE_MEM_DATA_DIR") or str(default_data)
self.data_dir = Path(data_dir).expanduser()
self.settings_path = self.data_dir / "settings.json"
self.settings = read_json(self.settings_path, {})
self.db_path = self.data_dir / "claude-mem.db"
self.rules_path = self.data_dir / "MY-RULES.md"
self.work_view_dir = self.data_dir / "work-view"
self.claude_md = Path(os.environ.get("CLAUDE_CONFIG_DIR") or home / ".claude") / "CLAUDE.md"
host = self.setting("CLAUDE_MEM_WORKER_HOST") or "127.0.0.1"
if host == "localhost":
host = "127.0.0.1"
# claude-mem uses uid 77 where there is none (Windows)
getuid = getattr(os, "getuid", None)
uid = getuid() if getuid else 77
port = self.setting("CLAUDE_MEM_WORKER_PORT") or str(37700 + uid % 100)
self.worker_url = f"http://{host}:{port}"
self.repo = Path(repo or os.environ.get("CM_SYNC_REPO") or home / ".claude-memory").expanduser()
self.remote = os.environ.get("CM_SYNC_REMOTE") or DEFAULT_REMOTE
xdg_state = os.environ.get("XDG_STATE_HOME") or str(home / ".local" / "state")
self.state_dir = Path(state_dir or os.environ.get("CM_SYNC_STATE_DIR") or Path(xdg_state) / "cm-sync").expanduser()
def setting(self, name):
return os.environ.get(name) or self.settings.get(name)
@property
def redaction_enabled(self):
return str(self.setting("CLAUDE_MEM_REDACT_ENABLED")).lower() == "true"
@property
def devices_root(self):
return self.repo / DEVICES_DIR
def read_json(path, default):
try:
with open(path, encoding="utf-8") as f:
return json.load(f)
except (OSError, ValueError):
return default
def tilde(path):
"""`path` with the home directory shown as `~`, in forward slashes so it reads the same on Windows."""
path, home = Path(path), Path.home()
try:
return "~/" + path.relative_to(home).as_posix()
except ValueError:
return path.as_posix()
def write_text_atomic(path, text, mode=None):
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_name(f".{path.name}.tmp{os.getpid()}")
# LF on every platform, so files in the memory repo are byte-identical across devices
with open(tmp, "w", encoding="utf-8", newline="\n") as f:
f.write(text)
if mode is not None:
os.chmod(tmp, mode)
os.replace(tmp, path)
# ------------------------------------------------------------------ local state
class State:
def __init__(self, cfg):
self.dir = cfg.state_dir
self.device = read_json(self.dir / "device.json", None)
self.imported = set(self._lines("imported.keys"))
self.tombstoned = set(self._lines("tombstoned.keys"))
# key -> kind for every row this device has published, so deletions become tombstones
self.exported = dict(line.split(" ", 1) for line in self._lines("exported.keys") if " " in line)
def _lines(self, name):
try:
return (self.dir / name).read_text(encoding="utf-8").splitlines()
except OSError:
return []
def save(self):
write_text_atomic(self.dir / "imported.keys", "".join(f"{k}\n" for k in sorted(self.imported)))
write_text_atomic(self.dir / "tombstoned.keys", "".join(f"{k}\n" for k in sorted(self.tombstoned)))
write_text_atomic(self.dir / "exported.keys", "".join(f"{k} {v}\n" for k, v in sorted(self.exported.items())))
def create_device(self, name):
self.device = {"id": str(uuid.uuid4()), "name": name}
write_text_atomic(self.dir / "device.json", json.dumps(self.device, indent=2) + "\n")
def require_device(self):
if not self.device:
raise CmSyncError("no device id yet: run `cm-sync setup` first")
return self.device
# ------------------------------------------------------------------ worker
class Worker:
def __init__(self, url, timeout=60):
self.url = url
self.timeout = timeout
def _request(self, method, route, body=None):
data = None if body is None else json.dumps(body).encode("utf-8")
req = urllib.request.Request(self.url + route, data=data, method=method)
if data is not None:
req.add_header("Content-Type", "application/json")
try:
with urllib.request.urlopen(req, timeout=self.timeout) as resp:
raw = resp.read().decode("utf-8")
return resp.status, json.loads(raw) if raw else None
except urllib.error.HTTPError as e:
return e.code, e.read().decode("utf-8", errors="replace")
except (urllib.error.URLError, OSError) as e:
raise CmSyncError(f"claude-mem worker not reachable at {self.url} ({e})") from e
def health(self):
try:
status, body = self._request("GET", "/api/health")
except CmSyncError:
return None
return body if status == 200 and isinstance(body, dict) else None
def require_health(self):
health = self.health()
if not health:
raise CmSyncError(f"claude-mem worker not running at {self.url}: start a Claude Code session first")
return health
def sync_status(self):
try:
status, body = self._request("GET", "/api/sync/status")
except CmSyncError:
return None
return body if status == 200 else None
def import_batch(self, payload):
status, body = self._request("POST", "/api/import", payload)
if status != 200:
raise CmSyncError(f"POST /api/import failed ({status}): {body}")
return body
def delete(self, kind, row):
if kind == "sessions":
route = f"/api/sessions/{quote(row['platform_source'])}/{quote(row['content_session_id'])}"
else:
route = f"/api/{DELETE_ROUTES[kind]}/{row['id']}"
status, body = self._request("DELETE", route)
if status in (200, 204, 404):
return status != 404
raise CmSyncError(f"DELETE {route} failed ({status}): {body}")
def restart(self):
self._request("POST", "/api/admin/restart", {})
def load_rules(self):
"""Have the worker rewrite MY-RULES.md and load it in CLAUDE.md; None when claude-mem predates 15.1."""
route = "/api/work-view/rules/load-in-claude-code"
status, body = self._request("POST", route, {})
if status == 404:
return None
if status != 200:
raise CmSyncError(f"POST {route} failed ({status}): {body}")
return True
DELETE_ROUTES = {"observations": "observation", "summaries": "summary", "prompts": "prompt"}
def quote(value):
return urllib.request.quote(str(value), safe="")
# ------------------------------------------------------------------ rows and keys
def row_key(kind, row):
values = [kind] + [row.get(c) for c in KEY_COLUMNS[kind]]
blob = json.dumps(values, ensure_ascii=False, separators=(",", ":"))
return hashlib.sha256(blob.encode("utf-8")).hexdigest()[:32]
def connect_ro(db_path):
if not db_path.exists():
raise CmSyncError(f"claude-mem database not found: {db_path}")
con = sqlite3.connect(db_path.resolve().as_uri() + "?mode=ro", uri=True)
con.row_factory = sqlite3.Row
return con
def read_local(db_path):
"""All local rows per kind, with `id` and filter fields kept for internal use."""
con = connect_ro(db_path)
try:
def select(sql):
return [dict(r) for r in con.execute(sql)]
sessions = select(
"SELECT id, " + ", ".join(COLUMNS["sessions"]) + " FROM sdk_sessions"
)
observations = select(
"SELECT id, metadata, "
+ ", ".join("COALESCE(NULLIF(merged_into_project, ''), project) AS project" if c == "project" else c for c in COLUMNS["observations"])
+ " FROM observations"
)
summaries = select(
"SELECT id, "
+ ", ".join("COALESCE(NULLIF(merged_into_project, ''), project) AS project" if c == "project" else c for c in COLUMNS["summaries"])
+ " FROM session_summaries"
)
prompts = select(
"SELECT p.id, p.content_session_id, COALESCE(NULLIF(s.platform_source, ''), 'claude') AS platform_source, "
"p.prompt_number, p.prompt_text, p.created_at, p.created_at_epoch "
"FROM user_prompts p LEFT JOIN sdk_sessions s ON s.id = p.session_db_id"
)
# local_id is the row's id on the device that created it, so its key matches that device's tombstones
work_state = select(
"SELECT id, COALESCE(CAST(origin_local_id AS INTEGER), id) AS local_id, origin_device_id, origin_local_id, "
"project, list_name, fields, created_at, created_at_epoch FROM work_state_entries"
) if has_work_state(con) else []
finally:
con.close()
return {"sessions": sessions, "observations": observations, "summaries": summaries, "prompts": prompts,
"work_state": work_state}
def has_work_state(con):
"""True when work_state_entries exists with every column the direct insert writes."""
columns = {r[1] for r in con.execute("PRAGMA table_info(work_state_entries)")}
return WORK_STATE_COLUMNS <= columns
def is_derived(row):
metadata = (row.get("metadata") or "").replace(" ", "")
return any(s in metadata for s in DERIVED_SOURCES)
ROW_LABELS = {"sessions": "session", "observations": "observation", "summaries": "summary", "prompts": "prompt",
"work_state": "work-state entry"}
def public(kind, row):
return {c: row.get(c) for c in COLUMNS[kind]}
def scan_secrets(kind, row):
hits = []
for column in SCAN_COLUMNS[kind]:
value = row.get(column)
if not isinstance(value, str):
continue
for name, pattern in SECRET_PATTERNS.items():
if pattern.search(value):
hits.append((column, name))
return hits
# ------------------------------------------------------------------ export
def device_dir(cfg, device_id):
return cfg.devices_root / device_id
def read_jsonl(path):
try:
with open(path, encoding="utf-8") as f:
return [json.loads(line) for line in f if line.strip()]
except OSError:
return []
def jsonl(rows):
return "".join(json.dumps(r, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + "\n" for r in rows)
def pref_rule_id(preference):
return "pref_" + hashlib.sha256(str(preference).encode("utf-8")).hexdigest()[:12]
def resolved_rule_ids(db_path):
"""Set of rule IDs whose latest status in work_state_entries is on, off, dismissed, or replaced."""
if not db_path or not Path(db_path).exists():
return set()
con = connect_ro(Path(db_path))
try:
rows = con.execute(
"SELECT fields FROM work_state_entries WHERE list_name = ? ORDER BY created_at_epoch, "
"COALESCE(origin_device_id, ''), COALESCE(CAST(origin_local_id AS INTEGER), id)", (RULES_LIST,)).fetchall()
except sqlite3.OperationalError:
return set()
finally:
con.close()
rules = {}
for row in rows:
fields = json.loads(row["fields"])
task = str(fields.get("task") or "")
rule_id = fields.get("rule_id")
if not rule_id and task.startswith("Rule: "):
rule_id = task[len("Rule: "):].strip()
if not rule_id:
continue
rule = rules.setdefault(rule_id, {})
for key, value in fields.items():
if value is None:
rule.pop(key, None)
else:
rule[key] = value
return {rule_id for rule_id, fields in rules.items() if fields.get("status") in ("on", "off", "dismissed", "replaced")}
def export_work_view(cfg, out_dir, db_path):
wv_dir = cfg.work_view_dir
if not wv_dir or not wv_dir.is_dir():
return
dest_wv = out_dir / "work-view"
story_path = wv_dir / "story.json"
if story_path.is_file():
story = read_json(story_path, {})
resolved = resolved_rule_ids(db_path)
if "preferences" in story and isinstance(story["preferences"], list):
story["preferences"] = [
p for p in story["preferences"]
if pref_rule_id(p.get("preference", "")) not in resolved
]
write_if_changed(dest_wv / "story.json", json.dumps(story, indent=2, sort_keys=True) + "\n")
rg_path = wv_dir / "request-groups.json"
if rg_path.is_file():
rg = read_json(rg_path, {})
write_if_changed(dest_wv / "request-groups.json", json.dumps(rg, indent=2, sort_keys=True) + "\n")
al_path = wv_dir / "alignment.json"
if al_path.is_file():
al = read_json(al_path, {})
write_if_changed(dest_wv / "alignment.json", json.dumps(al, indent=2, sort_keys=True) + "\n")
prog_dir = wv_dir / "progress"
if prog_dir.is_dir():
dest_prog = dest_wv / "progress"
for p in sorted(prog_dir.glob("g_*.json")):
if p.is_file():
data = read_json(p, {})
write_if_changed(dest_prog / p.name, json.dumps(data, indent=2, sort_keys=True) + "\n")
def export(cfg, state, worker=None, with_prompts=False, allow_unredacted=False, exclude_flagged=False):
device = state.require_device()
if not cfg.redaction_enabled and not allow_unredacted:
raise CmSyncError(
"claude-mem redaction is off (CLAUDE_MEM_REDACT_ENABLED), so secrets may be in your memory.\n"
"Turn it on with `cm-sync setup`, or pass --allow-unredacted to export anyway (the secret scan still runs)."
)
local = read_local(cfg.db_path)
present = {kind: {row_key(kind, r) for r in rows} for kind, rows in local.items()}
all_present = set().union(*present.values())
blocked = state.imported | state.tombstoned
own = {kind: [] for kind in KINDS}
for kind in ("observations", "summaries", "prompts"):
if kind == "prompts" and not with_prompts:
continue
for row in local[kind]:
if kind == "observations" and (row.get("type") == "sensitive" or is_derived(row)):
continue
if row_key(kind, row) not in blocked:
own[kind].append(row)
# Rows with an origin came from another device (cm-sync or claude-mem cloud sync)
own["work_state"] = [r for r in local["work_state"]
if not r.get("origin_device_id") and row_key("work_state", r) not in blocked]
by_memory_id = {s["memory_session_id"]: s for s in local["sessions"] if s.get("memory_session_id")}
by_content_id = {(s["platform_source"], s["content_session_id"]): s for s in local["sessions"]}
needed = {}
for kind in ("observations", "summaries"):
for row in own[kind]:
session = by_memory_id.get(row["memory_session_id"])
if session:
needed[session["id"]] = session
for row in own["prompts"]:
session = by_content_id.get((row["platform_source"], row["content_session_id"]))
if session:
needed[session["id"]] = session
own["sessions"] = [dict(s, user_prompt=s["user_prompt"] if with_prompts else None) for s in needed.values()]
flagged = {}
for kind in KINDS:
for row in own[kind]:
for column, pattern in scan_secrets(kind, row):
flagged.setdefault((kind, row["id"]), (row, []))[1].append(f"{column} matches {pattern}")
if flagged:
lines = [f" {ROW_LABELS[kind]} #{row_id} ({(row.get('title') or row.get('custom_title') or row.get('request') or row.get('list_name') or '')[:60]}): {', '.join(hits)}"
for (kind, row_id), (row, hits) in flagged.items()]
listing = "possible secrets found:\n" + "\n".join(lines)
# No worker route deletes sessions or work state
deletable = {k: v for k, v in flagged.items() if k[0] not in ("sessions", "work_state")}
if not exclude_flagged and worker and deletable and ask_tty(
listing + f"\nDelete these {len(deletable)} rows from claude-mem now?", default=True):
for (kind, _), (row, _) in deletable.items():
worker.delete(kind, row)
all_present.discard(row_key(kind, row))
info(f"Deleted {len(deletable)} rows")
kept = [k for k in flagged if k not in deletable]
if kept:
warn("Kept local only (sessions and work state are never deleted here): "
+ ", ".join(f"{ROW_LABELS[kind]} #{i}" for kind, i in kept))
elif not exclude_flagged:
raise CmSyncError(
listing.replace("found:", "found, nothing was written:", 1) +
f"\nDelete those rows in the claude-mem viewer ({cfg.worker_url}), or pass --exclude-flagged to keep them local only."
)
else:
warn("Kept local only " + listing)
own = {kind: [r for r in rows if (kind, r["id"]) not in flagged] for kind, rows in own.items()}
published = {kind: sorted(((row_key(kind, r), public(kind, r)) for r in own[kind]),
key=lambda kr: (kr[1].get("created_at_epoch") or kr[1].get("started_at_epoch") or 0, kr[0]))
for kind in KINDS}
out = device_dir(cfg, device["id"])
old_tombstones = read_jsonl(out / "tombstones.jsonl")
known = {t["key"] for t in old_tombstones}
now = int(time.time())
new_tombstones = [{"kind": kind, "key": key, "at": now}
for key, kind in sorted(state.exported.items())
if key not in all_present and key not in known]
for kind in KINDS:
path = out / f"{kind}.jsonl"
if kind == "prompts" and not with_prompts:
if path.exists():
path.unlink()
continue
write_if_changed(path, jsonl(row for _, row in published[kind]))
if new_tombstones:
write_text_atomic(out / "tombstones.jsonl", jsonl(old_tombstones + new_tombstones))
export_work_view(cfg, out, cfg.db_path)
version = (worker.health() or {}).get("version") if worker else None
manifest = {
"format": FORMAT,
"device_id": device["id"],
"device_name": device["name"],
"claude_mem_version": version or read_json(out / "manifest.json", {}).get("claude_mem_version"),
"counts": {kind: len(published[kind]) for kind in KINDS},
}
write_if_changed(out / "manifest.json", json.dumps(manifest, indent=2, sort_keys=True) + "\n")
for t in new_tombstones:
state.exported.pop(t["key"], None)
for kind in KINDS:
for key, _ in published[kind]:
state.exported[key] = kind
state.save()
step("Exported", counted(*((len(published[k]), k) for k in KINDS if k != "prompts" or with_prompts), (len(new_tombstones), "new tombstones")))
info(" " * 14 + paint(f"→ {tilde(out.parent)}/{device['id'][:8]}…", "dim"))
return {"published": {k: len(v) for k, v in published.items()}, "tombstones": len(new_tombstones)}
def write_if_changed(path, text):
try:
if path.read_text(encoding="utf-8") == text:
return False
except OSError:
pass
write_text_atomic(path, text)
return True
# ------------------------------------------------------------------ import
def other_devices(cfg, own_id):
if not cfg.devices_root.is_dir():
return []
return sorted(p for p in cfg.devices_root.iterdir() if p.is_dir() and p.name != own_id and not p.name.startswith("."))
def batches(rows_by_kind, sessions, limit=BATCH_BYTES):
"""Yield (payload, keys_by_kind) batches under `limit` bytes, each carrying the sessions its rows need."""
by_memory_id = {s["memory_session_id"]: s for _, s in sessions if s.get("memory_session_id")}
by_content_id = {(s["platform_source"], s["content_session_id"]): s for _, s in sessions}
session_keys = {id(s): k for k, s in sessions}
def empty():
return {kind: [] for kind in IMPORT_KINDS}, {kind: [] for kind in IMPORT_KINDS}, set(), 2
payload, keys, included, size = empty()
for kind in ("observations", "summaries", "prompts"):
for key, row in rows_by_kind.get(kind, []):
if kind == "prompts":
session = by_content_id.get((row.get("platform_source"), row["content_session_id"]))
else:
session = by_memory_id.get(row["memory_session_id"])
def cost():
extra = [session] if session is not None and id(session) not in included else []
return extra, len(json.dumps(row)) + sum(len(json.dumps(s)) for s in extra) + 2
extra, row_size = cost()
if size + row_size > limit and any(payload[k] for k in IMPORT_KINDS):
yield payload, keys
payload, keys, included, size = empty()
extra, row_size = cost()
for s in extra:
payload["sessions"].append(s)
keys["sessions"].append(session_keys[id(s)])
included.add(id(s))
payload[kind].append(row)
keys[kind].append(key)
size += row_size
if any(payload[k] for k in IMPORT_KINDS):
yield payload, keys
def import_work_view(cfg, state, db_path, other_device_dirs):
wv_dir = cfg.work_view_dir
if not wv_dir:
return
other_wv_dirs = [d / "work-view" for d in other_device_dirs if (d / "work-view").is_dir()]
if not other_wv_dirs:
return
# 1. progress/
prog_names = set()
local_prog_dir = wv_dir / "progress"
if local_prog_dir.is_dir():
for p in local_prog_dir.glob("g_*.json"):
prog_names.add(p.name)
for d in other_wv_dirs:
prog_d = d / "progress"
if prog_d.is_dir():
for p in prog_d.glob("g_*.json"):
prog_names.add(p.name)
if prog_names:
dest_prog_dir = wv_dir / "progress"
for name in sorted(prog_names):
candidates = []
if (dest_prog_dir / name).is_file():
candidates.append(read_json(dest_prog_dir / name, {}))
for d in other_wv_dirs:
if (d / "progress" / name).is_file():
candidates.append(read_json(d / "progress" / name, {}))
if candidates:
best = max(candidates, key=lambda x: str(x.get("checkedAt") or x.get("changedAt") or x.get("lastActivityAt") or ""))
write_if_changed(dest_prog_dir / name, json.dumps(best, indent=2, sort_keys=True) + "\n")
# 2. request-groups.json
rg_candidates = []
if (wv_dir / "request-groups.json").is_file():
rg_candidates.append(read_json(wv_dir / "request-groups.json", {}))
for d in other_wv_dirs:
if (d / "request-groups.json").is_file():
rg_candidates.append(read_json(d / "request-groups.json", {}))
if rg_candidates:
best_rg = max(rg_candidates, key=lambda r: str(r.get("updatedAt") or ""))
merged_rg = dict(best_rg)
merged_groups = {}
merged_convs = {}
merged_ignored = dict(best_rg.get("ignored", {})) if isinstance(best_rg.get("ignored"), dict) else []
for cand in rg_candidates:
for gid, g in (cand.get("groups") or {}).items():
if gid not in merged_groups:
merged_groups[gid] = g
else:
t1 = str(merged_groups[gid].get("decidedAt") or merged_groups[gid].get("updatedAt") or "")
t2 = str(g.get("decidedAt") or g.get("updatedAt") or "")
if t2 > t1:
merged_groups[gid] = g
for cid, c in (cand.get("conversations") or {}).items():
merged_convs[cid] = c
if isinstance(merged_ignored, dict) and isinstance(cand.get("ignored"), dict):
merged_ignored.update(cand["ignored"])
elif isinstance(merged_ignored, list) and isinstance(cand.get("ignored"), list):
for x in cand["ignored"]:
if x not in merged_ignored:
merged_ignored.append(x)
merged_rg["groups"] = merged_groups
merged_rg["conversations"] = merged_convs
merged_rg["ignored"] = merged_ignored
max_up = max((str(c.get("updatedAt") or "") for c in rg_candidates), default="")
if max_up:
merged_rg["updatedAt"] = max_up
write_if_changed(wv_dir / "request-groups.json", json.dumps(merged_rg, indent=2, sort_keys=True) + "\n")
# 3. alignment.json
al_candidates = []
if (wv_dir / "alignment.json").is_file():
al_candidates.append(read_json(wv_dir / "alignment.json", {}))
for d in other_wv_dirs:
if (d / "alignment.json").is_file():
al_candidates.append(read_json(d / "alignment.json", {}))
if al_candidates:
best_al = max(al_candidates, key=lambda a: str(a.get("alignedAt") or ""))
write_if_changed(wv_dir / "alignment.json", json.dumps(best_al, indent=2, sort_keys=True) + "\n")
# 4. story.json
story_candidates = []
if (wv_dir / "story.json").is_file():
story_candidates.append(read_json(wv_dir / "story.json", {}))
for d in other_wv_dirs:
if (d / "story.json").is_file():
story_candidates.append(read_json(d / "story.json", {}))
if story_candidates:
best_story = dict(max(story_candidates, key=lambda s: str(s.get("writtenAt") or "")))
seen_prefs = {}
for cand in story_candidates:
for p in (cand.get("preferences") or []):
text = p.get("preference")
if not text:
continue
if text not in seen_prefs:
seen_prefs[text] = p
else:
if (p.get("timesSeen") or 0) > (seen_prefs[text].get("timesSeen") or 0):
seen_prefs[text] = p
resolved = resolved_rule_ids(db_path)
filtered = [
p for text, p in seen_prefs.items()
if pref_rule_id(text) not in resolved
]
filtered.sort(key=lambda p: (-(p.get("timesSeen") or 0), p.get("preference", "")))
best_story["preferences"] = filtered
write_if_changed(wv_dir / "story.json", json.dumps(best_story, indent=2, sort_keys=True) + "\n")
def import_all(cfg, state, worker, full=False):
device = state.require_device()
worker.require_health()
devices = other_devices(cfg, device["id"])
if not devices:
step("Imported", paint("nothing, no other devices in " + tilde(cfg.devices_root), "dim"))
return {"imported": 0, "skipped": 0, "rejected": 0, "deleted": 0}
tombstones = {}
for d in devices:
for t in read_jsonl(d / "tombstones.jsonl"):
tombstones[t["key"]] = t.get("kind", "observations")
blocked = set(tombstones) | state.tombstoned
known = set() if full else state.imported
totals = {"imported": 0, "skipped": 0, "rejected": 0, "deleted": 0}
for d in devices:
manifest = read_json(d / "manifest.json", {})
name = manifest.get("device_name") or d.name
sessions = [(row_key("sessions", s), s) for s in read_jsonl(d / "sessions.jsonl")]
rows = {}
for kind in ("observations", "summaries", "prompts", "work_state"):
rows[kind] = []
for row in read_jsonl(d / f"{kind}.jsonl"):
key = row_key(kind, row)
if key not in blocked and key not in known:
rows[kind].append((key, row))
if not any(rows.values()):
step("Imported", f"{name}: " + paint("up to date", "dim"))
continue
imported = skipped = rejected = 0
for payload, keys in batches(rows, sessions):
result = worker.import_batch(payload)
stats = result.get("stats", {})
imported += sum(stats.get(f"{k}Imported", 0) for k in ("observations", "summaries", "prompts"))
skipped += sum(stats.get(f"{k}Skipped", 0) for k in ("observations", "summaries", "prompts"))
bad = result.get("rejected") or {}
for kind in IMPORT_KINDS:
bad_idx = {r.get("index") for r in bad.get(kind, [])}
rejected += len(bad_idx) if kind != "sessions" else 0
for i, key in enumerate(keys[kind]):
if i not in bad_idx:
state.imported.add(key)
for r in bad.get(kind, [])[:5]:
warn(f" {name}: {kind}[{r.get('index')}] rejected: {r.get('reason')}")
if rows["work_state"]:
added = insert_work_state(cfg, d.name, [row for _, row in rows["work_state"]])
if added is not None:
imported += added
skipped += len(rows["work_state"]) - added
state.imported.update(key for key, _ in rows["work_state"])
step("Imported", f"{name}: " + counted((imported, "new"), (skipped, "already present"), (rejected, "rejected")))
totals["imported"] += imported
totals["skipped"] += skipped
totals["rejected"] += rejected
pending = {k: v for k, v in tombstones.items() if k not in state.tombstoned}
if pending:
local = read_local(cfg.db_path)
order = ("observations", "summaries", "prompts", "sessions", "work_state")
index = {kind: {row_key(kind, r): r for r in local[kind]} for kind in order}
for kind in order:
for key, t_kind in pending.items():
if t_kind != kind:
continue
row = index[kind].get(key)
if row is not None and (delete_work_state(cfg, row) if kind == "work_state" else worker.delete(kind, row)):
totals["deleted"] += 1
state.tombstoned.add(key)
if totals["deleted"]:
step("Deleted", f"{totals['deleted']} rows deleted on other devices")
state.save()
import_work_view(cfg, state, cfg.db_path, devices)
# Every import, not only when rule rows arrive: rows imported before cm-sync rebuilt rules never arrive again
rebuild_rules(cfg, worker)
return totals
def rules_loaded(cfg):
"""True when CLAUDE.md imports MY-RULES.md, written the way claude-mem writes the line."""
try:
lines = cfg.claude_md.read_text(encoding="utf-8").splitlines()
except OSError:
return False
return bool({line.strip() for line in lines} & {"@" + tilde(cfg.rules_path), "@" + str(cfg.rules_path)})
def rule_counts(db_path):
"""(active, total) My Rules, folded like claude-mem: each rule's fields from its entries, oldest first."""
con = connect_ro(db_path)
try:
rows = con.execute(
"SELECT fields FROM work_state_entries WHERE list_name = ? ORDER BY created_at_epoch, "
"COALESCE(origin_device_id, ''), COALESCE(CAST(origin_local_id AS INTEGER), id)", (RULES_LIST,)).fetchall()
except sqlite3.OperationalError:
return None
finally:
con.close()
rules = {}
for row in rows:
fields = json.loads(row["fields"])
task = str(fields.get("task") or "")
if not task.startswith("Rule: "):
continue
rule = rules.setdefault(task, {})
for key, value in fields.items():
if value is None:
rule.pop(key, None)
else:
rule[key] = value
active = sum(r.get("status") == "on" and r.get("set_by") in RULES_ACTIVE_BY for r in rules.values())
return active, len(rules)
def rebuild_rules(cfg, worker):
"""Rewrite MY-RULES.md through the worker: it rebuilds the file only for rules its own cloud sync applies."""
if not rules_loaded(cfg):
counts = rule_counts(cfg.db_path) if cfg.db_path.exists() else None
if counts and counts[0]:
info(paint(f" My Rules aren't loaded in {tilde(cfg.claude_md)}, run `cm-sync setup`", "dim"))
return
try:
rebuilt = worker.load_rules()
except CmSyncError as e:
warn(f"cm-sync: My Rules not rebuilt ({e})")
return
if rebuilt is None:
warn("cm-sync: My Rules not rebuilt, the worker needs claude-mem 15.1+")
return
step("Rules", "rebuilt " + tilde(cfg.rules_path))
def connect_rw(db_path):
# timeout waits out the worker's write locks; the database is in WAL mode, so its reads go on
return sqlite3.connect(db_path.resolve().as_uri() + "?mode=rw", uri=True, timeout=5)
def insert_work_state(cfg, origin_device, rows):
"""Insert another device's work-state rows the way claude-mem's cloud sync applies them.
Keeps created_at_epoch, which decides the latest value of every field. The unique
(origin_device_id, origin_local_id) index makes re-imports no-ops. Returns how many rows
were new, or None when this claude-mem has no origin columns.
"""
con = connect_rw(cfg.db_path)
try:
if not has_work_state(con):
warn("cm-sync: claude-mem has no work-state origin columns (needs v15+), work state not imported")
return None
now = int(time.time() * 1000)
with con:
before = con.total_changes
con.executemany(
"INSERT OR IGNORE INTO work_state_entries (project, list_name, fields, created_at, created_at_epoch, "
"synced_at, origin_device_id, origin_local_id, sync_rev) VALUES (?, ?, ?, ?, ?, ?, ?, ?, '1')",
[(r["project"], r["list_name"], r["fields"], r["created_at"], r["created_at_epoch"],
now, origin_device, str(r["local_id"])) for r in rows],
)
return con.total_changes - before
except sqlite3.Error as e:
raise CmSyncError(f"work-state import failed, nothing was written ({e})") from e
finally:
con.close()
def delete_work_state(cfg, row):
"""Delete a work-state row that came from another device; this device's own rows are never deleted."""
if not row.get("origin_device_id"):
return False
con = connect_rw(cfg.db_path)
try:
with con:
cur = con.execute("DELETE FROM work_state_entries WHERE origin_device_id = ? AND origin_local_id = ?",
(row["origin_device_id"], row["origin_local_id"]))
return cur.rowcount > 0
finally:
con.close()
# ------------------------------------------------------------------ refile
def refile(cfg, worker, session_id, project):
"""Move a session's observations and summaries to another project: import copies, then delete the originals."""
worker.require_health()
con = connect_ro(cfg.db_path)
try:
con_rows = con.execute("SELECT id, " + ", ".join(COLUMNS["sessions"]) + " FROM sdk_sessions WHERE content_session_id = ?", (session_id,))
sessions = [dict(r) for r in con_rows]
finally:
con.close()
if not sessions:
raise CmSyncError(f"no claude-mem session with id {session_id}")
source = sessions[0]
if source["project"] == project:
raise CmSyncError(f"session {session_id} already belongs to {project}")