-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbridge_server.py
More file actions
1744 lines (1600 loc) · 70.4 KB
/
bridge_server.py
File metadata and controls
1744 lines (1600 loc) · 70.4 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
"""Thin MCP server exposing only bridge sync tools.
Shares the same SQLite database as the main sqlite-kb server.
Exists because Claude Code 2.x has a tool-count limit per MCP server
(~9 tools visible out of 50), so bridge tools are split into a separate server.
"""
from __future__ import annotations
import json
import os
import socket
import sqlite3
import subprocess
import threading
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any
from fastmcp_compat import FastMCP
from db_utils import (
apply_task_mutation as _apply_task_mutation,
json_dumps as _json_dumps,
json_loads as _json_loads,
get_conn as _get_conn,
create_task_with_ledger as _create_task_with_ledger,
get_entity_id as _get_entity_id,
fts_sync_entity as _fts_sync,
TaskDAO,
PUBLISH_STANDBY_MINUTES as _PUBLISH_STANDBY_MINUTES,
bridge_change_summary as _bridge_change_summary,
promote_pending_public_entities as _promote_pending_public_entities,
_NOWIN,
now_iso as _now,
parse_iso_datetime_for_compare as _parse_iso_dt,
sanitize_task_enums as _sanitize_task_enums,
setup_logger,
merge_import_tasks as _merge_import_tasks,
export_task_files as _export_task_files,
export_index_json as _export_index_json,
migrate_to_per_task_files as _migrate_to_per_task_files,
export_entity_files as _export_entity_files,
export_entities_index as _export_entities_index,
load_remote_entities_for_import as _load_remote_entities_for_import,
load_remote_tasks_for_merge as _load_remote_tasks_for_merge,
import_remote_bridge_data as _import_remote_bridge_data,
EXTENDED_MEMORY_KEYS as _EXTENDED_MEMORY_KEYS, # noqa: F401
migrate_entities_to_per_files as _migrate_entities_to_per_files,
sync_task_attachments_from_remote as _sync_task_attachments_from_remote,
BRIDGE_REPO,
BRIDGE_SYNC_DELAY,
git_run as _git_run,
ensure_bridge_git_identity as _ensure_bridge_git_identity,
ensure_bridge_repo_ready as _ensure_bridge_repo_ready,
source_hash as _source_hash,
validate_github_username as _validate_github_user,
)
from schema import (
error as _error,
clamp_score as _clamp_score,
is_valid_timestamp as _is_valid_timestamp,
)
from runtime_parity import (
collect_runtime_parity as _collect_runtime_parity,
runtime_warning_summary as _runtime_warning_summary,
write_runtime_parity_manifest as _write_runtime_parity_manifest,
)
from surface_contract import (
BRIDGE_GIT_STAGE_PATHS as _BRIDGE_GIT_STAGE_PATHS,
BRIDGE_SHARED_PAYLOAD_KEYS as _BRIDGE_SHARED_PAYLOAD_KEYS,
build_surface_contract_report as _build_surface_contract_report,
)
# ── Logging (file-only, NEVER stdout — breaks MCP stdio) ────────────────
logger = setup_logger("sqlite-bridge", "bridge_server.log")
# ── FastMCP app ──────────────────────────────────────────────────────────
mcp = FastMCP(
"sqlite-bridge",
instructions=(
"Bridge sync tools: cross-machine push/pull, task assignment, shared task review, "
"recurring tasks, and bridge_doctor self-checks for runtime parity and surface contract. "
"Shares DB with sqlite-kb."
),
)
# ── Debounced bridge auto-sync ──────────────────────────────────────
_bridge_sync_timer: threading.Timer | None = None
_bridge_sync_lock = threading.Lock()
_BRIDGE_SYNC_DELAY = BRIDGE_SYNC_DELAY # shared with task_tray via db_utils
def _schedule_bridge_sync():
"""Schedule a debounced bridge sync. Resets timer on each call."""
global _bridge_sync_timer
with _bridge_sync_lock:
if _bridge_sync_timer is not None:
_bridge_sync_timer.cancel()
_bridge_sync_timer = threading.Timer(_BRIDGE_SYNC_DELAY, _run_bridge_sync)
_bridge_sync_timer.daemon = True # don't block process exit
_bridge_sync_timer.start()
def _run_bridge_sync():
"""Execute bridge sync in background thread."""
global _bridge_sync_timer
try:
import bridge_sync_worker
stats = bridge_sync_worker.main()
logger.info("auto-sync: %s", stats)
except Exception as exc:
logger.warning("auto-sync failed: %s", exc)
finally:
with _bridge_sync_lock:
_bridge_sync_timer = None
# ── Bridge helpers ────────────────────────────────────────────────────────
def _git(*args: str) -> subprocess.CompletedProcess:
"""Run git in BRIDGE_REPO. Thin wrapper around db_utils.git_run."""
timeouts = {"pull": 120, "push": 300, "commit": 60}
timeout = timeouts.get(args[0], 30) if args else 30
return _git_run(BRIDGE_REPO, *args, timeout=timeout)
def _tmp_write_path(path: Path) -> Path:
"""Use a per-target temp name so shared.json and shared.js never collide."""
return path.with_name(f"{path.name}.tmp")
def _bridge_repo_blocked_error(message: str) -> str:
"""Return a structured bridge repo preflight error."""
return json.dumps({"error": message, "blocked_by_repo_state": True})
def _is_known_collaborator(
conn: sqlite3.Connection,
github_user: str | None,
required_trust: str | None = None,
) -> bool:
"""Check collaborator membership, optionally enforcing a trust level."""
if not github_user:
return False
if required_trust is None:
row = conn.execute(
"SELECT 1 FROM collaborators WHERE github_user = ?",
(github_user,),
).fetchone()
else:
row = conn.execute(
"SELECT 1 FROM collaborators WHERE github_user = ? AND trust_level = ?",
(github_user, required_trust),
).fetchone()
return row is not None
def _write_shared_js(shared_path: Path, payload_text: str | None = None) -> None:
"""Write shared.js wrapper next to shared.json for file:// consumers."""
try:
raw = payload_text
if raw is None:
raw = shared_path.read_text(encoding="utf-8")
shared_path.with_name("shared.js").write_text(
f"window.__BRIDGE_DATA__ = {raw};",
encoding="utf-8",
)
except OSError as exc:
logger.warning("bridge shared.js generation failed: %s", exc)
def _push_to_assignee(assignee: str, tasks: list[dict]) -> None:
"""Push assigned tasks to another user's memory-bridge repo."""
import tempfile
_validate_github_user(assignee)
repo_url = f"https://github.com/{assignee}/memory-bridge.git"
with tempfile.TemporaryDirectory() as tmpdir:
clone = subprocess.run(
["git", "clone", "--depth=1", repo_url, tmpdir],
capture_output=True,
text=True,
timeout=30,
**_NOWIN,
)
if clone.returncode != 0:
logger.warning(
"_push_to_assignee: clone failed for %s: %s",
assignee,
clone.stderr.strip(),
)
return
shared_path = Path(tmpdir) / "shared.json"
existing: dict = {}
if shared_path.exists():
try:
existing = json.loads(shared_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError) as e:
logger.warning(
"_push_to_assignee: ignoring corrupt shared.json for %s: %s",
assignee,
e,
)
# Merge into shared_tasks array (upsert by id, last-write-wins)
shared_tasks = {t["id"]: t for t in existing.get("shared_tasks", [])}
for t in tasks:
if _parse_iso_dt(t.get("updated_at")) >= _parse_iso_dt(
shared_tasks.get(t["id"], {}).get("updated_at")
):
shared_tasks[t["id"]] = t
existing["shared_tasks"] = list(shared_tasks.values())
tmp_path = _tmp_write_path(shared_path)
tmp_path.write_text(
json.dumps(existing, indent=2, ensure_ascii=False), encoding="utf-8"
)
os.replace(tmp_path, shared_path)
subprocess.run(
["git", "-C", tmpdir, "add", "shared.json"],
capture_output=True,
timeout=10,
**_NOWIN,
)
hostname = socket.gethostname()
msg = f"bridge: shared {len(tasks)} tasks from {hostname} to {assignee}"
commit = subprocess.run(
["git", "-C", tmpdir, "commit", "-m", msg],
capture_output=True,
text=True,
timeout=10,
**_NOWIN,
)
if commit.returncode == 0:
push = subprocess.run(
["git", "-C", tmpdir, "push"],
capture_output=True,
text=True,
timeout=30,
**_NOWIN,
)
if push.returncode == 0:
logger.info(
"_push_to_assignee: pushed %d tasks to %s", len(tasks), assignee
)
else:
logger.warning(
"_push_to_assignee: push failed for %s: %s",
assignee,
push.stderr.strip(),
)
def _push_knowledge_to(conn: sqlite3.Connection, target_user: str) -> int:
"""Push shared knowledge (entities + relations) to a collaborator's repo."""
import tempfile
# Gather entities to share based on sharing_rules
rules = conn.execute(
"SELECT entity_name, share_type, priority FROM sharing_rules WHERE target_user IN (?, '*')",
(target_user,),
).fetchall()
if not rules:
return 0
entity_names: set[str] = set()
include_relations = False
priorities: dict[str, str] = {} # entity_name → priority
for r in rules:
if r["share_type"] in ("entity", "all"):
if r["entity_name"] == "*":
# All shared-tagged entities
rows = conn.execute(
"SELECT name FROM entities WHERE project LIKE 'shared%'"
).fetchall()
for row in rows:
entity_names.add(row["name"])
priorities[row["name"]] = r["priority"]
else:
entity_names.add(r["entity_name"])
priorities[r["entity_name"]] = r["priority"]
if r["share_type"] in ("relation", "all"):
include_relations = True
if not entity_names:
return 0
# Build knowledge payload
knowledge_out = []
entity_ids = set()
for ename in entity_names:
erow = conn.execute(
"SELECT id, name, entity_type, project FROM entities WHERE name = ?",
(ename,),
).fetchone()
if not erow:
continue
entity_ids.add(erow["id"])
obs = conn.execute(
"SELECT content, created_at FROM observations WHERE entity_id = ? ORDER BY id",
(erow["id"],),
).fetchall()
obs_list = [
{"content": o["content"], "createdAt": o["created_at"]} for o in obs
]
entry = {
"name": erow["name"],
"entityType": erow["entity_type"],
"project": erow["project"],
"observations": obs_list,
"priority": priorities.get(ename, "medium"),
"sharedBy": os.environ.get("GITHUB_USER", socket.gethostname()),
"sharedAt": _now(),
"sourceHash": _source_hash(erow["name"], erow["entity_type"], obs_list),
}
# Attach relations if requested
if include_relations:
rels = conn.execute(
"SELECT et.name AS to_name, r.relation_type "
"FROM relations r JOIN entities et ON r.to_id = et.id "
"WHERE r.from_id = ?",
(erow["id"],),
).fetchall()
entry["relations"] = [
{"to": r["to_name"], "relationType": r["relation_type"]}
for r in rels
if r["to_name"] in entity_names
]
knowledge_out.append(entry)
if not knowledge_out:
return 0
# Clone target repo, merge knowledge, push
_validate_github_user(target_user)
repo_url = f"https://github.com/{target_user}/memory-bridge.git"
with tempfile.TemporaryDirectory() as tmpdir:
clone = subprocess.run(
["git", "clone", "--depth=1", repo_url, tmpdir],
capture_output=True,
text=True,
timeout=30,
**_NOWIN,
)
if clone.returncode != 0:
logger.warning(
"_push_knowledge_to: clone failed for %s: %s",
target_user,
clone.stderr.strip(),
)
return 0
shared_path = Path(tmpdir) / "shared.json"
existing: dict = {}
if shared_path.exists():
try:
existing = json.loads(shared_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError) as e:
logger.warning(
"_push_knowledge_to: ignoring corrupt shared.json for %s: %s",
target_user,
e,
)
# Merge into shared_knowledge (dedup by sourceHash)
current = {e["sourceHash"]: e for e in existing.get("shared_knowledge", [])}
for entry in knowledge_out:
current[entry["sourceHash"]] = entry
existing["shared_knowledge"] = list(current.values())
tmp_path = _tmp_write_path(shared_path)
tmp_path.write_text(
json.dumps(existing, indent=2, ensure_ascii=False), encoding="utf-8"
)
os.replace(tmp_path, shared_path)
subprocess.run(
["git", "-C", tmpdir, "add", "shared.json"],
capture_output=True,
timeout=10,
**_NOWIN,
)
hostname = socket.gethostname()
msg = f"bridge: shared {len(knowledge_out)} entities from {hostname} to {target_user}"
commit = subprocess.run(
["git", "-C", tmpdir, "commit", "-m", msg],
capture_output=True,
text=True,
timeout=10,
**_NOWIN,
)
if commit.returncode == 0:
push = subprocess.run(
["git", "-C", tmpdir, "push"],
capture_output=True,
text=True,
timeout=30,
**_NOWIN,
)
if push.returncode == 0:
logger.info(
"_push_knowledge_to: pushed %d entities to %s",
len(knowledge_out),
target_user,
)
return len(knowledge_out)
else:
logger.warning(
"_push_knowledge_to: push failed for %s: %s",
target_user,
push.stderr.strip(),
)
return 0
# ═══════════════════════════════════════════════════════════════════════════
# Tool 1: bridge_push
# ═══════════════════════════════════════════════════════════════════════════
@mcp.tool()
def bridge_push(tag: str = "shared", force: bool = False) -> str:
"""Push tagged entities to the bridge git repo for cross-machine sync.
Exports entities where project LIKE '{tag}%' with their observations
and inter-relations to JSON. Git add, commit, push.
Incremental: skips full export if nothing changed since last push.
Set force=True to push regardless.
"""
if not Path(BRIDGE_REPO).is_dir():
return json.dumps(
{
"error": f"Bridge repo not found at {BRIDGE_REPO}. "
f"Run: mkdir -p {BRIDGE_REPO} && git -C {BRIDGE_REPO} init"
}
)
repo_ok, repo_msg = _ensure_bridge_repo_ready(BRIDGE_REPO)
if not repo_ok:
logger.warning("bridge_push: preflight blocked push: %s", repo_msg)
return _bridge_repo_blocked_error(repo_msg or "bridge repo is not ready")
identity = _ensure_bridge_git_identity(BRIDGE_REPO)
if identity.get("changed"):
logger.info(
"bridge_push: bridge git identity set to %s <%s>",
identity.get("user_name") or "",
identity.get("user_email") or "",
)
# v2.0.0: Pull before push (prevents overwriting remote changes)
pull_result = _git("pull", "--rebase", "--autostash")
if pull_result.returncode != 0:
logger.warning("bridge_push: git pull failed: %s", pull_result.stderr.strip())
# Auto-recover from merge conflicts: DB is source of truth, export will re-create
_stderr = pull_result.stderr or ""
if any(kw in _stderr for kw in ("unmerged", "conflict", "CONFLICT")):
logger.warning(
"bridge_push: merge conflict — aborting rebase, resetting to origin/main"
)
_git("rebase", "--abort")
_git("reset", "--hard", "origin/main")
logger.warning(
"bridge_push: reset to origin/main; export will re-create shared.json"
)
# v2.0.0: One-time migration shared.json → per-task files
_migrate_to_per_task_files(BRIDGE_REPO)
# v4: One-time migration shared.json → per-entity files
_migrate_entities_to_per_files(BRIDGE_REPO)
shared_path = Path(BRIDGE_REPO) / "shared.json"
export_started_at = _now()
remote_payload: dict[str, Any] = {}
if shared_path.exists():
try:
remote_payload = _json_loads(shared_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError, TypeError) as exc:
logger.warning("bridge_push: shared.json read failed before merge: %s", exc)
with _get_conn() as conn:
_import_remote_bridge_data(conn, BRIDGE_REPO, remote_payload, logger)
remote_tasks, _tasks_from_index = _load_remote_tasks_for_merge(
BRIDGE_REPO,
remote_payload,
logger,
)
if remote_tasks:
try:
_merge_import_tasks(conn, remote_tasks, import_content=True)
_sync_task_attachments_from_remote(conn, remote_tasks, BRIDGE_REPO)
except (sqlite3.Error, ValueError) as exc:
logger.warning("bridge_push: task merge failed: %s", exc)
# Incremental check: skip if no changes since last push
if not force:
last_push_row = conn.execute(
"SELECT value FROM bridge_meta WHERE key = 'last_push_at'"
).fetchone()
if last_push_row:
last_push_at = last_push_row["value"]
cutoff = (
datetime.now(timezone.utc)
- timedelta(minutes=_PUBLISH_STANDBY_MINUTES)
).isoformat()
change_summary = _bridge_change_summary(conn, last_push_at, cutoff)
if not any(change_summary.values()):
logger.info(
"bridge_push: no changes since %s, skipping", last_push_at
)
return json.dumps(
{
"pushed": 0,
"message": f"No changes since {last_push_at}. Use force=True to push anyway.",
}
)
# v0.7.0: Promote pending_public → public if standby elapsed
cutoff = (
datetime.now(timezone.utc) - timedelta(minutes=_PUBLISH_STANDBY_MINUTES)
).isoformat()
promoted_ent = _promote_pending_public_entities(conn, cutoff, export_started_at)
promoted_tasks = TaskDAO.promote_pending_public(
conn,
cutoff,
updated_at=export_started_at,
)
if promoted_ent or promoted_tasks:
logger.info(
"bridge_push: promoted %d entities, %d tasks to public",
promoted_ent,
promoted_tasks,
)
ent_rows = conn.execute(
"SELECT id, name, entity_type, project, created_at, updated_at "
"FROM entities WHERE project LIKE ? ORDER BY name",
(f"{tag}%",),
).fetchall()
entities_out = []
entity_ids = set()
for e in ent_rows:
entity_ids.add(e["id"])
obs = conn.execute(
"SELECT content, created_at FROM observations "
"WHERE entity_id = ? ORDER BY id",
(e["id"],),
).fetchall()
entities_out.append(
{
"name": e["name"],
"entityType": e["entity_type"],
"project": e["project"],
"observations": [
{"content": o["content"], "createdAt": o["created_at"]}
for o in obs
],
"createdAt": e["created_at"],
"updatedAt": e["updated_at"],
}
)
# Relations where BOTH endpoints are in the shared set
relations_out = []
if entity_ids:
ph = ",".join("?" * len(entity_ids))
ids = list(entity_ids)
rel_rows = conn.execute(
f"SELECT ef.name AS from_name, et.name AS to_name, r.relation_type, r.created_at "
f"FROM relations r "
f"JOIN entities ef ON r.from_id = ef.id "
f"JOIN entities et ON r.to_id = et.id "
f"WHERE r.from_id IN ({ph}) AND r.to_id IN ({ph})",
ids + ids,
).fetchall()
relations_out = [
{
"from": r["from_name"],
"to": r["to_name"],
"relationType": r["relation_type"],
"createdAt": r["created_at"],
}
for r in rel_rows
]
# Export all non-archived tasks for cross-machine sync
task_rows = conn.execute(
"SELECT id, title, description, status, priority, section, due_date, "
"project, parent_id, notes, recurring, type, assignee, shared_by, "
"created_at, updated_at "
"FROM tasks WHERE status NOT IN ('archived', 'cancelled') ORDER BY created_at"
).fetchall()
tasks_out = [dict(r) for r in task_rows]
# v2.0.0: Export per-task files + index.json
last_push_at = None
lp_row = conn.execute(
"SELECT value FROM bridge_meta WHERE key = 'last_push_at'"
).fetchone()
if lp_row:
last_push_at = lp_row["value"]
_export_task_files(conn, BRIDGE_REPO, changed_since=last_push_at)
_export_index_json(conn, BRIDGE_REPO)
# v4: Export per-entity files + entities_index.json
_, _entity_rows = _export_entity_files(conn, BRIDGE_REPO)
_export_entities_index(conn, BRIDGE_REPO, rows=_entity_rows)
# v0.7.0: Export public entities + tasks as public_knowledge
pub_ent_rows = conn.execute(
"SELECT id, name, entity_type, project, created_at, updated_at "
"FROM entities WHERE visibility='public' ORDER BY name"
).fetchall()
public_entities_out = []
for pe in pub_ent_rows:
obs = conn.execute(
"SELECT content, created_at FROM observations "
"WHERE entity_id = ? ORDER BY id",
(pe["id"],),
).fetchall()
public_entities_out.append(
{
"name": pe["name"],
"entityType": pe["entity_type"],
"project": pe["project"],
"observations": [
{"content": o["content"], "createdAt": o["created_at"]}
for o in obs
],
"createdAt": pe["created_at"],
"updatedAt": pe["updated_at"],
}
)
pub_task_rows = conn.execute(
"SELECT id, title, description, status, priority, section, "
"due_date, project, created_at, updated_at "
"FROM tasks WHERE visibility='public' ORDER BY created_at"
).fetchall()
public_tasks_out = [dict(r) for r in pub_task_rows]
# Build team_manifest from collaborators (same connection)
collab_rows = conn.execute(
"SELECT github_user FROM collaborators ORDER BY added_at"
).fetchall()
collaborator_list = [r["github_user"] for r in collab_rows]
hostname = socket.gethostname()
owner = os.environ.get("GITHUB_USER", hostname)
payload = {
"version": 4,
"pushed_at": _now(),
"machine_id": hostname,
"owner": owner,
"entities": [], # v4: entities now in entities/ directory
"relations": relations_out,
"tasks": tasks_out,
"team_manifest": {
"collaborators": collaborator_list,
"display_name": owner,
},
}
# v0.7.0: Add public_knowledge to payload
if public_entities_out or public_tasks_out:
payload["public_knowledge"] = {
"entities": public_entities_out,
"tasks": public_tasks_out,
}
# v0.9.0: Export knowledge_ratings
with _get_conn() as conn:
rating_rows = conn.execute(
"SELECT entity_name, rater_id, content_hash, specificity, falsifiability, "
"internal_consistency, novelty, verification_outcome, usefulness, "
"verification_context, rated_at FROM knowledge_ratings ORDER BY rated_at"
).fetchall()
if rating_rows:
payload["knowledge_ratings"] = [dict(r) for r in rating_rows]
# Merge remote tasks + preserve extra keys from remote
index_exists = (Path(BRIDGE_REPO) / "index.json").exists()
if shared_path.exists():
try:
existing = _json_loads(shared_path.read_text(encoding="utf-8"))
if not index_exists:
# Legacy merge: keep remote tasks that don't exist locally (by id)
local_ids = {t["id"] for t in tasks_out}
remote_tasks = existing.get("tasks", [])
merged_count = 0
for rt in remote_tasks:
if rt.get("id") and rt["id"] not in local_ids:
tasks_out.append(rt)
local_ids.add(rt["id"])
merged_count += 1
if merged_count:
payload["tasks"] = tasks_out
logger.info(
"bridge_push: merged %d remote-only tasks into payload",
merged_count,
)
# Update existing tasks where remote has newer updated_at
local_by_id = {t["id"]: t for t in tasks_out}
updated_count = 0
for rt in remote_tasks:
rt_id = rt.get("id")
if not rt_id or rt_id not in local_by_id:
continue
lt = local_by_id[rt_id]
r_upd = rt.get("updated_at", "")
l_upd = lt.get("updated_at", "")
if r_upd > l_upd:
_sanitize_task_enums(rt)
for field in (
"status",
"section",
"priority",
"due_date",
"notes",
"description",
"type",
):
if rt.get(field) is not None:
lt[field] = rt[field]
lt["updated_at"] = r_upd
updated_count += 1
if updated_count:
logger.info(
"bridge_push: updated %d tasks from newer remote data",
updated_count,
)
# Preserve extra keys (e.g. reading_tasks, shared_knowledge)
known_keys = set(_BRIDGE_SHARED_PAYLOAD_KEYS)
for key, val in existing.items():
if key not in known_keys and isinstance(val, (list, dict)):
payload[key] = val
logger.info(
"bridge_push: preserving extra key '%s' (%s)",
key,
f"{len(val)} items" if isinstance(val, list) else "dict",
)
except (json.JSONDecodeError, OSError) as e:
logger.warning("bridge_push: ignoring corrupt existing shared.json: %s", e)
payload_json = _json_dumps(payload)
tmp_path = _tmp_write_path(shared_path)
tmp_path.write_text(payload_json, encoding="utf-8")
os.replace(tmp_path, shared_path)
_write_shared_js(shared_path, payload_text=payload_json)
# Cross-account push: send assigned tasks to other users' repos
by_assignee: dict[str, list] = {}
for t in tasks_out:
if t.get("assignee"):
by_assignee.setdefault(t["assignee"], []).append(t)
for target_user, assigned_tasks in by_assignee.items():
try:
_push_to_assignee(target_user, assigned_tasks)
except Exception as exc:
logger.warning("bridge_push: failed to push to %s: %s", target_user, exc)
# Cross-account knowledge push: sharing_rules → collaborator repos
# Phase 1: collect targets inside short transaction (release WAL quickly)
knowledge_pushed = 0
push_targets: list[str] = []
with _get_conn() as conn:
rules = conn.execute(
"SELECT DISTINCT target_user FROM sharing_rules"
).fetchall()
for rule_row in rules:
target = rule_row["target_user"]
collab = conn.execute(
"SELECT trust_level FROM collaborators WHERE github_user = ?",
(target,),
).fetchone()
if collab:
push_targets.append(target)
# Phase 2: git operations outside transaction (no WAL lock during network I/O)
successful_targets: list[str] = []
for target in push_targets:
try:
with _get_conn() as conn:
pushed_n = _push_knowledge_to(conn, target)
knowledge_pushed += pushed_n
successful_targets.append(target)
except Exception as exc:
logger.warning("bridge_push: knowledge push to %s failed: %s", target, exc)
# Phase 3: update sync timestamps in short transaction
if successful_targets:
with _get_conn() as conn:
now = _now()
for target in successful_targets:
conn.execute(
"UPDATE collaborators SET last_sync_at = ? WHERE github_user = ?",
(now, target),
)
n_obs = sum(len(e["observations"]) for e in entities_out)
msg = (
f"bridge: push {len(entities_out)} entities, "
f"{len(tasks_out)} tasks from {hostname}"
)
_git("add", *_BRIDGE_GIT_STAGE_PATHS)
# Use --porcelain to check staged changes without locale-dependent text parsing
status_result = _git("status", "--porcelain")
if not status_result.stdout.strip():
logger.info("bridge_push: no changes to commit")
return json.dumps({"pushed": 0, "message": "No changes — already up to date"})
commit_result = _git("commit", "-m", msg)
if commit_result.returncode != 0:
logger.error("bridge_push: commit failed: %s", commit_result.stderr)
# Restore shared.json to last committed state to prevent dirty file
# from being overwritten by a future bridge_pull --rebase
_git("checkout", "--", "shared.json", "shared.js")
return _error(f"git commit failed: {commit_result.stderr.strip()}")
push_result = _git("push")
pushed = push_result.returncode == 0
logger.info(
"bridge_push: %d entities, %d observations, %d relations, %d tasks, push=%s",
len(entities_out),
n_obs,
len(relations_out),
len(tasks_out),
pushed,
)
result: dict[str, Any] = {
"entities": len(entities_out),
"observations": n_obs,
"relations": len(relations_out),
"tasks": len(tasks_out),
"pushed_to_remote": pushed,
"message": msg,
}
if knowledge_pushed:
result["knowledge_shared"] = knowledge_pushed
if promoted_ent or promoted_tasks:
result["promoted_to_public"] = {
"entities": promoted_ent,
"tasks": promoted_tasks,
}
# v0.7.0: Create GitHub release when public_knowledge is pushed
has_public = bool(public_entities_out or public_tasks_out)
if pushed and has_public:
n_pub_ent = len(public_entities_out)
n_pub_tasks = len(public_tasks_out)
tag_name = f"public-v{datetime.now(timezone.utc).strftime('%Y%m%d-%H%M%S')}"
release_title = f"Public Knowledge: {n_pub_ent} entities, {n_pub_tasks} tasks"
release_notes = (
f"## Public Knowledge Release\n\n"
f"- **{n_pub_ent}** public entities\n"
f"- **{n_pub_tasks}** public tasks\n\n"
f"Published from `{hostname}` at {_now()}"
)
gh_repo = os.environ.get("BRIDGE_GH_REPO", "")
if not gh_repo:
logger.warning(
"bridge_push: BRIDGE_GH_REPO not set, skipping GitHub release"
)
else:
try:
rel_result = subprocess.run(
[
"gh",
"release",
"create",
tag_name,
"--repo",
gh_repo,
"--title",
release_title,
"--notes",
release_notes,
],
capture_output=True,
text=True,
timeout=30,
**_NOWIN,
)
if rel_result.returncode == 0:
result["github_release"] = tag_name
logger.info("bridge_push: created GitHub release %s", tag_name)
else:
logger.warning(
"bridge_push: GitHub release failed: %s",
rel_result.stderr.strip(),
)
except Exception as exc:
logger.warning("bridge_push: GitHub release error: %s", exc)
if has_public:
result["public_knowledge"] = {
"entities": len(public_entities_out),
"tasks": len(public_tasks_out),
}
if pushed:
with _get_conn() as conn:
conn.execute(
"INSERT OR REPLACE INTO bridge_meta(key, value) VALUES('last_push_at', ?)",
(export_started_at,),
)
return json.dumps(result)
# ═══════════════════════════════════════════════════════════════════════════
# Tool 2: bridge_pull
# ═══════════════════════════════════════════════════════════════════════════
@mcp.tool()
def bridge_pull() -> str:
"""Pull shared entities from the bridge git repo into local memory.
Git pull, read shared.json, import new entities/observations/relations.
UNIQUE constraints handle deduplication automatically.
"""
if not Path(BRIDGE_REPO).is_dir():
return _error(f"Bridge repo not found at {BRIDGE_REPO}")
repo_ok, repo_msg = _ensure_bridge_repo_ready(BRIDGE_REPO)
if not repo_ok:
logger.warning("bridge_pull: preflight blocked pull: %s", repo_msg)
return _bridge_repo_blocked_error(repo_msg or "bridge repo is not ready")
pull_result = _git("pull", "--rebase", "--autostash")
git_pull_failed = pull_result.returncode != 0
if git_pull_failed:
logger.warning("bridge_pull: git pull failed, proceeding with local copy")
shared_path = Path(BRIDGE_REPO) / "shared.json"
_pull_index_path = Path(BRIDGE_REPO) / "index.json"
_has_index = _pull_index_path.exists()
if not shared_path.exists() and not _has_index:
return _error("No sync data found in bridge repo")
# Read shared.json for entities/relations (and legacy task fallback)
payload: dict = {}
if shared_path.exists():
try:
payload = _json_loads(shared_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError, TypeError) as exc:
if not _has_index:
return _error(f"Failed to read shared.json: {exc}")
logger.warning("bridge_pull: shared.json parse failed: %s", exc)
entities = _load_remote_entities_for_import(BRIDGE_REPO, payload, logger)
relations = payload.get("relations", [])
remote_tasks, tasks_from_index = _load_remote_tasks_for_merge(
BRIDGE_REPO,
payload,
logger,
)
# Stage shared_tasks for review (never auto-import from other accounts)
shared_tasks = payload.get("shared_tasks", [])
staged_count = 0
now = _now()
new_entities = 0
new_observations = 0
new_relations = 0
new_tasks = 0
updated_tasks = 0
payload_owner = payload.get("owner")
with _get_conn() as conn:
for ent in entities:
cur = conn.execute(
"INSERT OR IGNORE INTO entities "
"(name, entity_type, project, created_at, updated_at) "
"VALUES (?, ?, ?, ?, ?)",
(
ent["name"],
ent["entityType"],
ent.get("project"),
ent.get("createdAt", now),
ent.get("updatedAt", now),
),
)
new_entities += cur.rowcount
eid = _get_entity_id(conn, ent["name"])
if eid:
for obs in ent.get("observations", []):
content = obs["content"] if isinstance(obs, dict) else obs
created = (