sibyl-memory-cli 0.3.6__tar.gz → 0.3.10__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/PKG-INFO +36 -1
  2. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/README.md +33 -0
  3. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/pyproject.toml +4 -1
  4. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli/cli.py +126 -2
  5. sibyl_memory_cli-0.3.10/src/sibyl_memory_cli/migrate.py +401 -0
  6. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli/setup.py +329 -6
  7. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli.egg-info/PKG-INFO +36 -1
  8. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli.egg-info/SOURCES.txt +7 -1
  9. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli.egg-info/requires.txt +3 -0
  10. sibyl_memory_cli-0.3.10/tests/test_hermes_install_args.py +31 -0
  11. sibyl_memory_cli-0.3.10/tests/test_migrate.py +150 -0
  12. sibyl_memory_cli-0.3.10/tests/test_migrate_adversarial.py +227 -0
  13. sibyl_memory_cli-0.3.10/tests/test_orchestrator.py +107 -0
  14. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/tests/test_setup.py +2 -2
  15. sibyl_memory_cli-0.3.10/tests/test_wiring_fix.py +172 -0
  16. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/setup.cfg +0 -0
  17. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli/__init__.py +0 -0
  18. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli/_aesthetic.py +0 -0
  19. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli/_banner.py +0 -0
  20. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli.egg-info/dependency_links.txt +0 -0
  21. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli.egg-info/entry_points.txt +0 -0
  22. {sibyl_memory_cli-0.3.6 → sibyl_memory_cli-0.3.10}/src/sibyl_memory_cli.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sibyl-memory-cli
3
- Version: 0.3.6
3
+ Version: 0.3.10
4
4
  Summary: Command-line interface for the Sibyl Memory Plugin. `sibyl init` activates, `sibyl upgrade` runs the staker / subscription flow, `sibyl status` shows current tier and DB stats, `sibyl whoami` gives a one-line account summary, `sibyl devices` lists active devices and supports per-device revoke.
5
5
  Author-email: "SIBYL, Sibyl Labs LLC" <sibyl@sibyllabs.org>
6
6
  License: MIT
@@ -21,6 +21,8 @@ Description-Content-Type: text/markdown
21
21
  Requires-Dist: sibyl-memory-client>=0.4.0
22
22
  Requires-Dist: sibyl-memory-hermes>=0.3.2
23
23
  Requires-Dist: pyyaml>=6.0
24
+ Provides-Extra: mcp
25
+ Requires-Dist: sibyl-memory-mcp>=0.1.2; extra == "mcp"
24
26
  Provides-Extra: dev
25
27
  Requires-Dist: pytest>=7.0; extra == "dev"
26
28
 
@@ -38,11 +40,44 @@ This pulls in `sibyl-memory-client` (the local SDK) and `sibyl-memory-hermes` (t
38
40
 
39
41
  ```
40
42
  sibyl init Open the browser activation page. Writes ~/.sibyl-memory/credentials.json.
43
+ sibyl migrate Guided onboarding: back up your existing memory/agent files, wire Sibyl
44
+ into every detected harness, populate Sibyl Memory from the backup, and
45
+ optionally slim the originals. Backup-first, never destructive.
46
+ sibyl setup [target] Wire Sibyl as the memory provider for Hermes, Claude Code, and/or Codex.
47
+ target is one of: hermes | claude-code | codex (default: detect all).
41
48
  sibyl upgrade Open the upgrade page. Stake $SIBYL or subscribe in USDC.
42
49
  sibyl status Show local credentials, DB size, and the server's view of your tier.
43
50
  sibyl health Run the SibylMemoryProvider self-check (schema version, DB path, tenant).
44
51
  ```
45
52
 
53
+ ## Migrate (guided onboarding)
54
+
55
+ ```bash
56
+ $ sibyl migrate
57
+ ```
58
+
59
+ `sibyl migrate` moves your accumulated agent memory into Sibyl without risking
60
+ your existing files:
61
+
62
+ 1. **Back up first.** Every memory/agent file it finds (`CLAUDE.md`,
63
+ `AGENTS.md`, `.codex/config.toml`, `.hermes/*`, and similar) is copied to a
64
+ timestamped backup folder and byte-verified before anything else happens.
65
+ 2. **Wire Sibyl** into every detected harness — Claude Code (via
66
+ `claude mcp add --scope user`), Codex (via `~/.codex/config.toml`), Hermes.
67
+ 3. **Extract** — it prints a prompt you run in your own agent. The agent reads
68
+ only from the backup and writes structured memory through the `sibyl-memory`
69
+ tool. The extraction runs locally on your machine; Sibyl Labs never sees your
70
+ files or memory.
71
+ 4. **Verify** the new entries that landed in your local DB.
72
+ 5. **Optionally trim** the originals — only if you confirm, and only because a
73
+ verified backup exists. Your full pre-migration files are always preserved.
74
+
75
+ Flags: `--backup-dir PATH` (default: home), `--no-debloat` (skip the trim
76
+ step), `--yes` (skip the initial confirm; the trim step still asks separately).
77
+
78
+ > No warranty. Keep your backup until you've confirmed everything migrated.
79
+ > Sibyl Labs is not responsible for data loss.
80
+
46
81
  ## Activation
47
82
 
48
83
  ```bash
@@ -12,11 +12,44 @@ This pulls in `sibyl-memory-client` (the local SDK) and `sibyl-memory-hermes` (t
12
12
 
13
13
  ```
14
14
  sibyl init Open the browser activation page. Writes ~/.sibyl-memory/credentials.json.
15
+ sibyl migrate Guided onboarding: back up your existing memory/agent files, wire Sibyl
16
+ into every detected harness, populate Sibyl Memory from the backup, and
17
+ optionally slim the originals. Backup-first, never destructive.
18
+ sibyl setup [target] Wire Sibyl as the memory provider for Hermes, Claude Code, and/or Codex.
19
+ target is one of: hermes | claude-code | codex (default: detect all).
15
20
  sibyl upgrade Open the upgrade page. Stake $SIBYL or subscribe in USDC.
16
21
  sibyl status Show local credentials, DB size, and the server's view of your tier.
17
22
  sibyl health Run the SibylMemoryProvider self-check (schema version, DB path, tenant).
18
23
  ```
19
24
 
25
+ ## Migrate (guided onboarding)
26
+
27
+ ```bash
28
+ $ sibyl migrate
29
+ ```
30
+
31
+ `sibyl migrate` moves your accumulated agent memory into Sibyl without risking
32
+ your existing files:
33
+
34
+ 1. **Back up first.** Every memory/agent file it finds (`CLAUDE.md`,
35
+ `AGENTS.md`, `.codex/config.toml`, `.hermes/*`, and similar) is copied to a
36
+ timestamped backup folder and byte-verified before anything else happens.
37
+ 2. **Wire Sibyl** into every detected harness — Claude Code (via
38
+ `claude mcp add --scope user`), Codex (via `~/.codex/config.toml`), Hermes.
39
+ 3. **Extract** — it prints a prompt you run in your own agent. The agent reads
40
+ only from the backup and writes structured memory through the `sibyl-memory`
41
+ tool. The extraction runs locally on your machine; Sibyl Labs never sees your
42
+ files or memory.
43
+ 4. **Verify** the new entries that landed in your local DB.
44
+ 5. **Optionally trim** the originals — only if you confirm, and only because a
45
+ verified backup exists. Your full pre-migration files are always preserved.
46
+
47
+ Flags: `--backup-dir PATH` (default: home), `--no-debloat` (skip the trim
48
+ step), `--yes` (skip the initial confirm; the trim step still asks separately).
49
+
50
+ > No warranty. Keep your backup until you've confirmed everything migrated.
51
+ > Sibyl Labs is not responsible for data loss.
52
+
20
53
  ## Activation
21
54
 
22
55
  ```bash
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sibyl-memory-cli"
7
- version = "0.3.6"
7
+ version = "0.3.10"
8
8
  description = "Command-line interface for the Sibyl Memory Plugin. `sibyl init` activates, `sibyl upgrade` runs the staker / subscription flow, `sibyl status` shows current tier and DB stats, `sibyl whoami` gives a one-line account summary, `sibyl devices` lists active devices and supports per-device revoke."
9
9
  authors = [{ name = "SIBYL, Sibyl Labs LLC", email = "sibyl@sibyllabs.org" }]
10
10
  license = { text = "MIT" }
@@ -29,6 +29,9 @@ dependencies = [
29
29
  ]
30
30
 
31
31
  [project.optional-dependencies]
32
+ mcp = [
33
+ "sibyl-memory-mcp>=0.1.2",
34
+ ]
32
35
  dev = [
33
36
  "pytest>=7.0",
34
37
  ]
@@ -981,6 +981,108 @@ def cmd_update(args: argparse.Namespace) -> int:
981
981
  return 2 # exit 2 signals "outdated" without being a hard error
982
982
 
983
983
 
984
+ # ---- Guided migration (sibyl migrate) ----------------------------------
985
+
986
+ def _migrate_io():
987
+ """Interactive IO for the guided flow: prints narration live and reads real
988
+ stdin for pauses/confirms. Subclasses the testable GuidedIO seam in migrate.py
989
+ (whose .say() only buffers, for non-interactive tests)."""
990
+ from .migrate import GuidedIO
991
+
992
+ class _PrintingIO(GuidedIO):
993
+ def say(self, s: str = "") -> None:
994
+ super().say(s)
995
+ print(s)
996
+
997
+ return _PrintingIO()
998
+
999
+
1000
+ def cmd_migrate(args: argparse.Namespace) -> int:
1001
+ """`sibyl migrate` — guided onboarding. Backs up existing memory/agent files
1002
+ FIRST, wires Sibyl into every detected harness, hands the semantic extraction
1003
+ to the user's own agent (it holds the memory tools; Sibyl Labs never sees the
1004
+ files), verifies what landed, then optionally trims the originals — only on an
1005
+ explicit confirm and only because a verified backup exists."""
1006
+ from . import migrate as M
1007
+
1008
+ home = Path.home()
1009
+ cwd = Path.cwd()
1010
+ db_path = Path(args.db).expanduser()
1011
+ backup_parent = Path(args.backup_dir).expanduser() if getattr(args, "backup_dir", None) else home
1012
+
1013
+ print()
1014
+ print(bold("Sibyl Memory — guided migration"))
1015
+ print(dim("Back up existing memory, populate Sibyl Memory, optionally slim the originals."))
1016
+ print()
1017
+ print(yellow("Your files are copied to a timestamped backup FIRST and are never modified"))
1018
+ print(yellow("except by an explicit, confirmed trim at the very end. You run the extraction"))
1019
+ print(yellow("in your own agent — Sibyl Labs never sees your files or memory."))
1020
+ print(dim("No warranty: keep your backup. Sibyl Labs is not responsible for data loss."))
1021
+ print()
1022
+
1023
+ files = M.scan_memory_files(home, cwd)
1024
+ if not files:
1025
+ print(yellow("No memory/agent files found in your home or current project."))
1026
+ print(dim("Looked for CLAUDE.md, AGENTS.md, .codex/config.toml, .hermes/*, and similar."))
1027
+ print(dim("If your files live elsewhere, run this from that project directory."))
1028
+ return 0
1029
+
1030
+ print(dim("Will back up (originals untouched):"))
1031
+ for f in files:
1032
+ kind = "dir " if f.is_dir else "file"
1033
+ print(f" {kind} {f.rel} {dim(f'({f.size} bytes)')}")
1034
+ print()
1035
+ print(dim("After Sibyl is wired, if your agent was already open, restart it (or"))
1036
+ print(dim("reconnect the sibyl-memory MCP) before running the extraction prompt."))
1037
+ print()
1038
+
1039
+ if not args.yes:
1040
+ try:
1041
+ ans = input("Proceed? [Y/n]: ").strip().lower()
1042
+ except EOFError:
1043
+ ans = ""
1044
+ if ans.startswith("n"):
1045
+ print(dim("Aborted. Nothing was changed."))
1046
+ return 0
1047
+ print()
1048
+
1049
+ io = _migrate_io()
1050
+ report = M.run_guided_setup(
1051
+ home=home, cwd=cwd, db_path=db_path, backup_parent=backup_parent,
1052
+ io=io, debloat=not args.no_debloat,
1053
+ )
1054
+
1055
+ ph = report.get("phases", {})
1056
+ print()
1057
+ print(bold("Summary"))
1058
+ bk = ph.get("backup", {})
1059
+ if bk:
1060
+ print(f" {green('backup')} {bk.get('files', 0)} files")
1061
+ print(f" {dim('location')} {bk.get('dir', '')}")
1062
+ wire = ph.get("wire", {})
1063
+ if wire:
1064
+ wired = ", ".join(f"{n} ({s})" for n, s in wire.items())
1065
+ print(f" {green('wired')} {wired}")
1066
+ v = ph.get("verify", {})
1067
+ if v:
1068
+ cats = ", ".join(f"{k}:{n}" for k, n in (v.get("by_category") or {}).items())
1069
+ print(f" {green('extracted')} {v.get('new_total', 0)} new entries" + (f" {dim(cats)}" if cats else ""))
1070
+ db = ph.get("debloat")
1071
+ if db and db.get("written"):
1072
+ saved = max(0, db.get("before", 0) - db.get("after", 0))
1073
+ print(f" {green('trimmed')} CLAUDE.md {dim(f'(-{saved} bytes; full copy in backup)')}")
1074
+
1075
+ if not report.get("ok"):
1076
+ print()
1077
+ print(yellow("Migration did not complete. Your originals and backup are intact."))
1078
+ return 1
1079
+ print()
1080
+ print(green("Done. Your memory now lives in Sibyl and is recalled on demand."))
1081
+ if bk:
1082
+ print(dim(f"Backup retained at {bk.get('dir','')} — delete it once you've confirmed everything."))
1083
+ return 0
1084
+
1085
+
984
1086
  # ---- Dispatch ----------------------------------------------------------
985
1087
 
986
1088
  def build_parser() -> argparse.ArgumentParser:
@@ -1042,7 +1144,7 @@ def build_parser() -> argparse.ArgumentParser:
1042
1144
  help="Auto-detect Hermes / Claude Code and wire SIBYL as the memory provider",
1043
1145
  )
1044
1146
  p_setup.add_argument(
1045
- "target", nargs="?", choices=list(["hermes", "claude-code"]),
1147
+ "target", nargs="?", choices=list(["hermes", "claude-code", "codex"]),
1046
1148
  help="Wire just this framework (default: detect all)",
1047
1149
  )
1048
1150
  p_setup.add_argument(
@@ -1063,10 +1165,32 @@ def build_parser() -> argparse.ArgumentParser:
1063
1165
  )
1064
1166
  p_setup.add_argument(
1065
1167
  "--claude-settings", default=None,
1066
- help="Override ~/.claude/settings.json autodetection",
1168
+ help="Override ~/.claude.json autodetection",
1169
+ )
1170
+ p_setup.add_argument(
1171
+ "--codex-config", default=None,
1172
+ help="Override ~/.codex/config.toml autodetection",
1067
1173
  )
1068
1174
  p_setup.set_defaults(func=cmd_setup)
1069
1175
 
1176
+ p_migrate = sub.add_parser(
1177
+ "migrate",
1178
+ help="Guided: back up existing memory/agent files, wire Sibyl, populate Sibyl Memory, optionally slim the originals",
1179
+ )
1180
+ p_migrate.add_argument(
1181
+ "--backup-dir", default=None,
1182
+ help="Where to write the timestamped backup (default: your home directory)",
1183
+ )
1184
+ p_migrate.add_argument(
1185
+ "--no-debloat", action="store_true",
1186
+ help="Skip the optional trim step (back up + wire + extract + verify only)",
1187
+ )
1188
+ p_migrate.add_argument(
1189
+ "--yes", "-y", action="store_true",
1190
+ help="Skip the initial confirm (the trim step still always asks separately)",
1191
+ )
1192
+ p_migrate.set_defaults(func=cmd_migrate)
1193
+
1070
1194
  return p
1071
1195
 
1072
1196
 
@@ -0,0 +1,401 @@
1
+ """`sibyl setup` guided onboarding flow (v2): backup -> wire MCP -> extract -> verify -> debloat.
2
+
3
+ Design (operator-locked 2026-05-31): one dynamic, resumable, guided flow that gets a
4
+ user "set up and optimized" no matter which harness they run. The CLI does the
5
+ DETERMINISTIC work (back up files, detect state, verify the DB, trim files) and
6
+ CONDUCTS; the user's own harness does the semantic EXTRACTION (it has the memory
7
+ tools). Every gap (no plugin, MCP not wired) prints exact per-harness instructions.
8
+
9
+ This module adds the new phases on top of the existing wirers in setup.py
10
+ (HermesWirer / ClaudeCodeWirer) and adds CodexWirer so all three harnesses are
11
+ first-class. Nothing here touches live files except the explicitly-confirmed
12
+ debloat step, and only after a verified backup exists.
13
+ """
14
+ from __future__ import annotations
15
+
16
+ import json
17
+ import os
18
+ import shutil
19
+ import sqlite3
20
+ from dataclasses import dataclass, field
21
+ from datetime import datetime, timezone
22
+ from pathlib import Path
23
+ from typing import Callable, Optional
24
+
25
+ from . import _aesthetic as A
26
+
27
+ # ----------------------------------------------------------------------
28
+ # 1. Memory/agent file discovery (per harness)
29
+ # ----------------------------------------------------------------------
30
+ # Candidate memory + agent files we back up + (optionally) extract from.
31
+ # Globs are resolved relative to `home`. Directories are copied whole.
32
+
33
+ HARNESS_FILES: dict[str, list[str]] = {
34
+ "claude-code": ["CLAUDE.md", ".claude/CLAUDE.md", ".claude/settings.json"],
35
+ "codex": ["AGENTS.md", ".codex/config.toml", ".codex/AGENTS.md"],
36
+ "hermes": [".hermes/config.yaml", ".hermes/memory"],
37
+ "generic": ["AGENTS.md", "MEMORY.md", "memory.md", ".cursorrules", ".cursor/rules"],
38
+ }
39
+
40
+
41
+ @dataclass
42
+ class FoundFile:
43
+ harness: str
44
+ path: Path # absolute
45
+ rel: str # path relative to home (for backup layout)
46
+ is_dir: bool
47
+ size: int
48
+
49
+
50
+ def _backup_rel(p: Path, home: Path, cwd: Optional[Path]) -> str:
51
+ """Collision-free backup path for a source file. Files under home keep their
52
+ home-relative path; files outside home (a project elsewhere) get a `project/`
53
+ prefix; anything else `external/<name>`. This prevents a home file and a
54
+ same-named project file from clobbering each other in the backup (data-loss bug)."""
55
+ try:
56
+ if p.is_relative_to(home):
57
+ return str(p.relative_to(home))
58
+ except (ValueError, OSError):
59
+ pass
60
+ if cwd:
61
+ try:
62
+ cwd = Path(cwd)
63
+ if p.is_relative_to(cwd):
64
+ return "project/" + str(p.relative_to(cwd))
65
+ except (ValueError, OSError):
66
+ pass
67
+ return "external/" + p.name
68
+
69
+
70
+ def scan_memory_files(home: Optional[Path] = None, cwd: Optional[Path] = None) -> list[FoundFile]:
71
+ """Find existing memory/agent files across harnesses. De-dupes by resolved path.
72
+ Looks in both the user's home and the current project dir (CLAUDE.md lives in projects)."""
73
+ home = Path(home).expanduser() if home else Path.home()
74
+ roots = [home]
75
+ if cwd:
76
+ roots.append(Path(cwd))
77
+ seen: set[Path] = set()
78
+ found: list[FoundFile] = []
79
+ for harness, rels in HARNESS_FILES.items():
80
+ for rel in rels:
81
+ for root in roots:
82
+ p = (root / rel)
83
+ if not p.exists():
84
+ continue
85
+ try:
86
+ key = p.resolve()
87
+ except OSError:
88
+ key = p
89
+ if key in seen:
90
+ continue
91
+ seen.add(key)
92
+ is_dir = p.is_dir()
93
+ size = _tree_size(p) if is_dir else p.stat().st_size
94
+ found.append(FoundFile(harness, p, _backup_rel(p, home, cwd), is_dir, size))
95
+ return found
96
+
97
+
98
+ def _tree_size(p: Path) -> int:
99
+ return sum(f.stat().st_size for f in p.rglob("*") if f.is_file())
100
+
101
+
102
+ # ----------------------------------------------------------------------
103
+ # 2. Backup (deterministic, verified, timestamped) — the safety win
104
+ # ----------------------------------------------------------------------
105
+
106
+ @dataclass
107
+ class BackupResult:
108
+ backup_dir: Path
109
+ files: list[str] = field(default_factory=list)
110
+ total_bytes: int = 0
111
+ ok: bool = True
112
+ error: Optional[str] = None
113
+
114
+
115
+ def backup_dir_name(now: Optional[datetime] = None) -> str:
116
+ now = now or datetime.now(timezone.utc)
117
+ return "sibyl-migration-backup-" + now.strftime("%Y-%m-%dT%H_%M_%S")
118
+
119
+
120
+ def run_backup(files: list[FoundFile], dest_parent: Path, *, now: Optional[datetime] = None) -> BackupResult:
121
+ """Copy each found file/dir into a fresh timestamped backup folder under dest_parent.
122
+ Verifies byte counts. Never modifies sources. Aborts (ok=False) on first failure."""
123
+ dest_parent = Path(dest_parent).expanduser()
124
+ backup = dest_parent / backup_dir_name(now)
125
+ res = BackupResult(backup_dir=backup)
126
+ try:
127
+ backup.mkdir(parents=True, exist_ok=False)
128
+ except Exception as e:
129
+ res.ok = False; res.error = f"could not create backup dir: {e}"
130
+ return res
131
+ for f in files:
132
+ target = backup / f.rel
133
+ try:
134
+ target.parent.mkdir(parents=True, exist_ok=True)
135
+ if f.is_dir:
136
+ shutil.copytree(f.path, target, dirs_exist_ok=True)
137
+ src_sz, dst_sz = _tree_size(f.path), _tree_size(target)
138
+ else:
139
+ shutil.copy2(f.path, target)
140
+ src_sz, dst_sz = f.path.stat().st_size, target.stat().st_size
141
+ if src_sz != dst_sz:
142
+ res.ok = False; res.error = f"byte mismatch on {f.rel} ({src_sz} != {dst_sz})"
143
+ return res
144
+ res.files.append(f.rel); res.total_bytes += dst_sz
145
+ except Exception as e:
146
+ res.ok = False; res.error = f"copy failed on {f.rel}: {type(e).__name__}: {e}"
147
+ return res
148
+ return res
149
+
150
+
151
+ # ----------------------------------------------------------------------
152
+ # 3. Wirers live in setup.py (canonical). Codex now auto-wires config.toml;
153
+ # Claude Code registers via `claude mcp add --scope user`.
154
+ # ----------------------------------------------------------------------
155
+
156
+ from .setup import CodexWirer, ClaudeCodeWirer, HermesWirer # noqa: E402 (canonical wirers)
157
+
158
+
159
+ # Per-harness wiring instructions for the guided flow (no silent edits across the board;
160
+ # we print and let the user run them, matching the operator's 'walk them through' intent).
161
+ def wire_instructions(harness: str) -> list[str]:
162
+ if harness == "claude-code":
163
+ return ["Open a new terminal and run:",
164
+ " claude mcp add sibyl-memory -- sibyl-memory-mcp",
165
+ "Restart Claude Code (or /mcp -> reconnect sibyl-memory), then return here."]
166
+ if harness == "codex":
167
+ return CodexWirer().instructions()
168
+ if harness == "hermes":
169
+ return ["Open a new terminal and run:",
170
+ " sibyl-memory-hermes install-plugin",
171
+ "Then set memory.provider: sibyl in ~/.hermes/config.yaml and restart Hermes."]
172
+ return ["Register an MCP server named 'sibyl-memory' with command 'sibyl-memory-mcp' in your agent's MCP config, then restart it."]
173
+
174
+
175
+ # ----------------------------------------------------------------------
176
+ # 4. Extraction handoff — the harness does the semantic work, from the backup
177
+ # ----------------------------------------------------------------------
178
+
179
+ def extraction_prompt(harness: str, backup_dir: Path) -> str:
180
+ """Tailored backup-first prompt the user runs IN their harness. Reads only from
181
+ the backup; never edits live files. Mirrors the beta-page conventions."""
182
+ tool = "sibyl_remember" if harness in ("claude-code", "codex") else "your memory tool"
183
+ return (
184
+ f"Read ONLY from the backup folder at {backup_dir} (never touch my live files). "
185
+ "For every piece of accumulated memory in those files (facts and configs, preferences "
186
+ "and patterns, project context, people and relationship notes), write each one into Sibyl "
187
+ f"Memory using {tool}:\n"
188
+ " - facts/configs/env: structured key-value content\n"
189
+ " - preferences/patterns: tagged as preference\n"
190
+ " - project context/history: under a project namespace\n"
191
+ " - people/relationships: with the person's name as context\n"
192
+ "Do not edit, trim, or delete any live file. When done, tell me how many entries you wrote "
193
+ "in each category."
194
+ )
195
+
196
+
197
+ # ----------------------------------------------------------------------
198
+ # 5. Verify — count what actually landed in the local Sibyl DB
199
+ # ----------------------------------------------------------------------
200
+
201
+ def db_baseline(db_path: Path) -> int:
202
+ """Total entity count now, to diff against after extraction. 0 if no DB yet."""
203
+ db_path = Path(db_path).expanduser()
204
+ if not db_path.exists():
205
+ return 0
206
+ try:
207
+ con = sqlite3.connect(str(db_path)); con.row_factory = sqlite3.Row
208
+ n = con.execute("SELECT COUNT(*) c FROM entities").fetchone()["c"]
209
+ con.close()
210
+ return int(n)
211
+ except sqlite3.Error:
212
+ return 0
213
+
214
+
215
+ def verify_new_entries(db_path: Path, baseline_total: int) -> dict:
216
+ """Return {'new_total': N, 'by_category': {...}, 'ok': bool}. ok = new_total > 0."""
217
+ db_path = Path(db_path).expanduser()
218
+ out = {"new_total": 0, "by_category": {}, "ok": False}
219
+ if not db_path.exists():
220
+ return out
221
+ try:
222
+ con = sqlite3.connect(str(db_path)); con.row_factory = sqlite3.Row
223
+ total = con.execute("SELECT COUNT(*) c FROM entities").fetchone()["c"]
224
+ cats = con.execute("SELECT category, COUNT(*) c FROM entities GROUP BY category ORDER BY c DESC").fetchall()
225
+ con.close()
226
+ out["new_total"] = max(0, int(total) - int(baseline_total))
227
+ out["by_category"] = {r["category"]: int(r["c"]) for r in cats}
228
+ out["ok"] = out["new_total"] > 0
229
+ except sqlite3.Error as e:
230
+ out["error"] = str(e)
231
+ return out
232
+
233
+
234
+ # ----------------------------------------------------------------------
235
+ # 6. Debloat — confirmed trim of the live file; safe because backup exists
236
+ # ----------------------------------------------------------------------
237
+
238
+ KEEP_START, KEEP_END = "<!-- sibyl:keep -->", "<!-- /sibyl:keep -->"
239
+
240
+
241
+ def heuristic_lean(text: str) -> str:
242
+ """Conservative lean version when the agent didn't provide one.
243
+ If the file marks a keep-block, keep exactly that. Otherwise keep everything up to
244
+ the first H2 section (identity/rules usually live at the top) and append a pointer.
245
+ The full original is always in the backup, so this is reversible."""
246
+ if KEEP_START in text and KEEP_END in text:
247
+ core = text.split(KEEP_START, 1)[1].split(KEEP_END, 1)[0].strip()
248
+ else:
249
+ lines, core_lines = text.splitlines(), []
250
+ seen_h2 = 0
251
+ for ln in lines:
252
+ if ln.startswith("## "):
253
+ seen_h2 += 1
254
+ if seen_h2 > 1: # keep the first ## section (identity/core), trim the rest
255
+ break
256
+ core_lines.append(ln)
257
+ core = "\n".join(core_lines).strip()
258
+ pointer = ("\n\n<!-- The rest of this file's accumulated memory now lives in Sibyl Memory "
259
+ "and is recalled on demand. Full pre-migration backup is preserved. -->\n")
260
+ return core + pointer
261
+
262
+
263
+ def debloat_file(live_path: Path, lean_text: str, *, backup_exists: bool, dry_run: bool = False) -> dict:
264
+ """Atomically replace live_path with lean_text. REFUSES unless backup_exists is True.
265
+ Returns {before, after, written, error}."""
266
+ live_path = Path(live_path).expanduser()
267
+ out = {"before": 0, "after": len(lean_text.encode()), "written": False}
268
+ if not backup_exists:
269
+ out["error"] = "refused: no verified backup exists"; return out
270
+ if not live_path.exists():
271
+ out["error"] = "live file not found"; return out
272
+ out["before"] = live_path.stat().st_size
273
+ if dry_run:
274
+ return out
275
+ tmp = live_path.with_suffix(live_path.suffix + ".sibyl-tmp")
276
+ tmp.write_text(lean_text, encoding="utf-8")
277
+ os.replace(tmp, live_path)
278
+ out["written"] = True
279
+ return out
280
+
281
+
282
+ # ----------------------------------------------------------------------
283
+ # 7. Orchestrator — the guided, resumable flow
284
+ # ----------------------------------------------------------------------
285
+
286
+ def detect_state(home: Optional[Path] = None, cwd: Optional[Path] = None, db_path: Optional[Path] = None) -> dict:
287
+ """Snapshot for resumability: what's present, what's wired, how much memory exists."""
288
+ from .setup import HermesWirer, ClaudeCodeWirer
289
+ home = Path(home).expanduser() if home else Path.home()
290
+ db_path = Path(db_path).expanduser() if db_path else (home / ".sibyl-memory" / "memory.db")
291
+ wirers = {"claude-code": ClaudeCodeWirer(), "codex": CodexWirer(), "hermes": HermesWirer()}
292
+ return {
293
+ "files": scan_memory_files(home, cwd),
294
+ "harnesses": {n: {"present": w.is_present(), **w.current_state()} for n, w in wirers.items()},
295
+ "db_entries": db_baseline(db_path),
296
+ "db_path": db_path,
297
+ }
298
+
299
+
300
+ class GuidedIO:
301
+ """IO seam so the guided flow is testable non-interactively. Pass `scripted`
302
+ answers (list) to drive confirms/pauses without a TTY."""
303
+ def __init__(self, scripted=None):
304
+ self.scripted = list(scripted or [])
305
+ self.lines: list[str] = []
306
+
307
+ def say(self, s: str = "") -> None:
308
+ self.lines.append(str(s))
309
+
310
+ def confirm(self, q: str, *, default: bool = True) -> bool:
311
+ if self.scripted:
312
+ ans = self.scripted.pop(0)
313
+ else:
314
+ try:
315
+ ans = input(f"{q} [{'Y/n' if default else 'y/N'}]: ").strip()
316
+ except EOFError:
317
+ ans = ""
318
+ return default if not ans else ans.strip().lower().startswith("y")
319
+
320
+ def pause(self, q: str = "press Enter to continue") -> None:
321
+ if self.scripted:
322
+ self.scripted.pop(0)
323
+ return
324
+ try:
325
+ input(q)
326
+ except EOFError:
327
+ pass
328
+
329
+
330
+ def run_guided_setup(*, home=None, cwd=None, db_path=None, backup_parent=None,
331
+ io: Optional[GuidedIO] = None, wirers: Optional[dict] = None,
332
+ extract_fn: Optional[Callable[[Path, Path], None]] = None,
333
+ debloat: bool = True, now=None) -> dict:
334
+ """The assembled guided flow: backup -> auto-wire each harness (instructions on
335
+ failure) -> extraction handoff -> verify -> confirmed debloat. Returns a structured
336
+ report. `extract_fn(backup_dir, db_path)` performs/simulates extraction; default
337
+ prints the prompt for the user to run in their own harness. `wirers` is injectable
338
+ so tests (and isolation) never touch real config."""
339
+ from .setup import ALL_WIRERS
340
+ io = io or GuidedIO()
341
+ home = Path(home).expanduser() if home else Path.home()
342
+ db_path = Path(db_path).expanduser() if db_path else (home / ".sibyl-memory" / "memory.db")
343
+ backup_parent = Path(backup_parent).expanduser() if backup_parent else home
344
+ report: dict = {"ok": True, "phases": {}}
345
+
346
+ # 1. scan + backup (deterministic, first, never modifies sources)
347
+ files = scan_memory_files(home, cwd)
348
+ report["files"] = [f.rel for f in files]
349
+ if not files:
350
+ io.say("No memory/agent files found. Nothing to migrate.")
351
+ report["ok"] = False
352
+ return report
353
+ bk = run_backup(files, backup_parent, now=now)
354
+ report["phases"]["backup"] = {"ok": bk.ok, "dir": str(bk.backup_dir), "files": len(bk.files)}
355
+ if not bk.ok:
356
+ io.say(f"Backup failed: {bk.error}. Aborting; nothing else touched.")
357
+ report["ok"] = False
358
+ return report
359
+ io.say(f"Backed up {len(bk.files)} files -> {bk.backup_dir} (originals untouched)")
360
+
361
+ # 2. detect + auto-wire each present harness; fall back to instructions
362
+ if wirers is None:
363
+ wirers = {n: cls() for n, cls in ALL_WIRERS.items()}
364
+ detected = {n: w for n, w in wirers.items() if w.is_present()}
365
+ wire_report = {}
366
+ for name, w in detected.items():
367
+ if w.current_state().get("wired_with_sibyl"):
368
+ wire_report[name] = "already"
369
+ continue
370
+ outcome = w.wire()
371
+ wire_report[name] = outcome.status
372
+ if outcome.status not in ("wired", "already"):
373
+ io.say(f"{name}: auto-wire incomplete ({outcome.message}). Do this manually:")
374
+ for ln in wire_instructions(name):
375
+ io.say(" " + ln)
376
+ report["phases"]["wire"] = wire_report
377
+
378
+ # 3. extraction (the harness does it; default prints the prompt + pauses)
379
+ baseline = db_baseline(db_path)
380
+ target = next(iter(detected), "claude-code")
381
+ if extract_fn is not None:
382
+ extract_fn(bk.backup_dir, db_path)
383
+ else:
384
+ io.say("Run this in your agent (it reads the backup, writes to Sibyl):")
385
+ io.say(extraction_prompt(target, bk.backup_dir))
386
+ io.pause("After it finishes, press Enter to verify")
387
+
388
+ # 4. verify
389
+ v = verify_new_entries(db_path, baseline)
390
+ report["phases"]["verify"] = v
391
+ io.say(f"Verified {v['new_total']} new entries in Sibyl Memory.")
392
+
393
+ # 5. debloat (confirmed; safe because the backup exists)
394
+ cm = (Path(cwd) / "CLAUDE.md") if cwd else (home / "CLAUDE.md")
395
+ if debloat and v["ok"] and cm.exists():
396
+ if io.confirm(f"Trim {cm.name} to lean now? Full backup is safe at {bk.backup_dir}", default=False):
397
+ lean = heuristic_lean(cm.read_text(encoding="utf-8", errors="replace"))
398
+ d = debloat_file(cm, lean, backup_exists=bk.ok)
399
+ report["phases"]["debloat"] = {"written": d["written"], "before": d["before"], "after": d["after"]}
400
+ io.say(f"Trimmed {cm.name}. Backup safe at {bk.backup_dir}")
401
+ return report