batonpass-cli 0.5.0__py3-none-any.whl
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.
- batonpass/__init__.py +7 -0
- batonpass/__main__.py +3 -0
- batonpass/archive/__init__.py +4 -0
- batonpass/archive/adapters.py +616 -0
- batonpass/archive/export.py +71 -0
- batonpass/archive/runner.py +128 -0
- batonpass/cli.py +579 -0
- batonpass/discover/__init__.py +36 -0
- batonpass/discover/probe.py +1052 -0
- batonpass/doctor.py +314 -0
- batonpass/identity/__init__.py +9 -0
- batonpass/identity/artifacts.py +293 -0
- batonpass/identity/project.py +245 -0
- batonpass/identity/tasks.py +423 -0
- batonpass/mcp.py +273 -0
- batonpass/paths.py +26 -0
- batonpass/recall/__init__.py +45 -0
- batonpass/recall/index.py +147 -0
- batonpass/recall/query.py +215 -0
- batonpass/store/__init__.py +5 -0
- batonpass/store/codecs.py +31 -0
- batonpass/store/content.py +186 -0
- batonpass/store/db.py +95 -0
- batonpass/store/schema.py +152 -0
- batonpass/store/text.py +55 -0
- batonpass/store/write.py +92 -0
- batonpass/sync/__init__.py +15 -0
- batonpass/sync/channel.py +213 -0
- batonpass/sync/crypt.py +216 -0
- batonpass/sync/pull.py +186 -0
- batonpass/sync/push.py +276 -0
- batonpass/sync/status.py +56 -0
- batonpass/sync/wire.py +205 -0
- batonpass_cli-0.5.0.dist-info/METADATA +324 -0
- batonpass_cli-0.5.0.dist-info/RECORD +39 -0
- batonpass_cli-0.5.0.dist-info/WHEEL +5 -0
- batonpass_cli-0.5.0.dist-info/entry_points.txt +2 -0
- batonpass_cli-0.5.0.dist-info/licenses/LICENSE +21 -0
- batonpass_cli-0.5.0.dist-info/top_level.txt +1 -0
batonpass/cli.py
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
"""Command line.
|
|
2
|
+
|
|
3
|
+
Two verbs do the work — `discover` looks, `archive` keeps. Everything else is
|
|
4
|
+
an inspection tool. Nothing here writes outside ~/.batonpass.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import sqlite3
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
from . import __version__
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _fmt_bytes(n):
|
|
19
|
+
n = float(n or 0)
|
|
20
|
+
for unit in ("B", "KB", "MB", "GB", "TB"):
|
|
21
|
+
if n < 1024 or unit == "TB":
|
|
22
|
+
return "%.1f %s" % (n, unit) if unit != "B" else "%d B" % n
|
|
23
|
+
n /= 1024.0
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def cmd_discover(args):
|
|
27
|
+
from .discover import main as discover_main
|
|
28
|
+
argv = []
|
|
29
|
+
if args.only:
|
|
30
|
+
argv += ["--only", args.only]
|
|
31
|
+
if args.json:
|
|
32
|
+
argv.append("--json")
|
|
33
|
+
if getattr(args, "report", None):
|
|
34
|
+
argv += ["--report", args.report]
|
|
35
|
+
return discover_main(argv)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def cmd_archive(args):
|
|
39
|
+
from .archive.runner import run_archive
|
|
40
|
+
only = [s.strip() for s in args.only.split(",") if s.strip()] if args.only else None
|
|
41
|
+
out = run_archive(only=only, level=args.level, limit=args.limit,
|
|
42
|
+
db=args.db, dry_run=args.dry_run)
|
|
43
|
+
if args.json:
|
|
44
|
+
print(json.dumps(out, ensure_ascii=False, indent=2, default=str))
|
|
45
|
+
return 0
|
|
46
|
+
head = "dry-run" if args.dry_run else "archived"
|
|
47
|
+
print("batonpass %s — device %s\n" % (head, out["device"]))
|
|
48
|
+
for s in out["sources"]:
|
|
49
|
+
flag = " " if not s["errors"] else "! "
|
|
50
|
+
print("%s%-8s %-52s %5d sess %7d msg %7d part %s" % (
|
|
51
|
+
flag, s["adapter"], _tail(s["path"], 52), s["sessions"], s["messages"],
|
|
52
|
+
s["parts"], _fmt_bytes(s["bytes_in"])))
|
|
53
|
+
if s["declared_drops"]:
|
|
54
|
+
print(" · %d structural parts skipped (step-start/step-finish) — declared"
|
|
55
|
+
% s["declared_drops"])
|
|
56
|
+
if s["empty_text"]:
|
|
57
|
+
print(" · %d rows carry no prose (structured parts only) — kept, not dropped"
|
|
58
|
+
% s["empty_text"])
|
|
59
|
+
for n in s["notes"]:
|
|
60
|
+
print(" · %s" % n)
|
|
61
|
+
for e in s["errors"]:
|
|
62
|
+
print(" ERROR %s" % e)
|
|
63
|
+
t = out["totals"]
|
|
64
|
+
print("\n%d sessions, %d messages, %d parts, %d new rows" % (
|
|
65
|
+
t["sessions"], t["messages"], t["parts"], t["new_rows"]))
|
|
66
|
+
return 0 if not any(s["errors"] for s in out["sources"]) else 1
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _tail(s, n):
|
|
70
|
+
s = str(s)
|
|
71
|
+
return s if len(s) <= n else "…" + s[-(n - 1):]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def cmd_export(args):
|
|
75
|
+
from .archive.export import export_session
|
|
76
|
+
doc = export_session(args.sid, db=args.db, as_json=not args.text)
|
|
77
|
+
if args.out:
|
|
78
|
+
with open(args.out, "w", encoding="utf-8") as fh:
|
|
79
|
+
fh.write(doc)
|
|
80
|
+
print("wrote %s (%s)" % (args.out, _fmt_bytes(len(doc.encode()))))
|
|
81
|
+
else:
|
|
82
|
+
print(doc)
|
|
83
|
+
return 0
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def cmd_stats(args):
|
|
87
|
+
from .archive.export import stats
|
|
88
|
+
st = stats(db=args.db)
|
|
89
|
+
if args.json:
|
|
90
|
+
print(json.dumps(st, ensure_ascii=False, indent=2))
|
|
91
|
+
return 0
|
|
92
|
+
print("sessions %d" % st["sessions"])
|
|
93
|
+
print("messages %d" % st["messages"])
|
|
94
|
+
print("parts %d" % st["parts"])
|
|
95
|
+
if st["raw_records"]:
|
|
96
|
+
print("raw %d records from %d sources" % (
|
|
97
|
+
st["raw_records"], st["raw_sources"]))
|
|
98
|
+
else:
|
|
99
|
+
# Level 1 keeps the parsed rows and drops the raw blobs; "0 records" reads
|
|
100
|
+
# like a bug when it is the level you chose.
|
|
101
|
+
print("raw not copied (level 1 keeps parsed rows; "
|
|
102
|
+
"`archive --level 2` also keeps the originals)")
|
|
103
|
+
print("plaintext %s in %d unique blobs" % (_fmt_bytes(st["plain_bytes"]), st["content_unique"]))
|
|
104
|
+
print("stored %s (%.2fx)" % (_fmt_bytes(st["stored_bytes"]), st["ratio"]))
|
|
105
|
+
print("library %s" % _fmt_bytes(st["db_bytes"]))
|
|
106
|
+
for row in st["by_source"]:
|
|
107
|
+
print(" %-8s %4d sessions %7d msgs" % (row["source"], row["n"], row["msgs"] or 0))
|
|
108
|
+
return 0
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def cmd_mcp(args):
|
|
112
|
+
from . import mcp
|
|
113
|
+
|
|
114
|
+
# stdout is the protocol: nothing else may be printed to it.
|
|
115
|
+
return mcp.serve(db_path=args.db)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def cmd_doctor(args):
|
|
119
|
+
from . import doctor
|
|
120
|
+
|
|
121
|
+
res = doctor.run(db_path=args.db, channel=args.channel, sources=args.sources)
|
|
122
|
+
if args.json:
|
|
123
|
+
print(json.dumps(res, ensure_ascii=False, indent=2, default=str))
|
|
124
|
+
else:
|
|
125
|
+
print(doctor.render(res))
|
|
126
|
+
return 1 if any(c["level"] == doctor.FAIL for c in res["checks"]) else 0
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def cmd_sync(args):
|
|
130
|
+
from .store.db import connect, db_path
|
|
131
|
+
from .sync import init_channel, pull, push, status
|
|
132
|
+
from .sync.channel import device_name
|
|
133
|
+
|
|
134
|
+
action = args.action
|
|
135
|
+
channel = args.channel
|
|
136
|
+
|
|
137
|
+
if action == "init":
|
|
138
|
+
info = init_channel(channel, device=args.device or device_name())
|
|
139
|
+
print("channel %s" % info["path"])
|
|
140
|
+
print("device %s" % (args.device or device_name()))
|
|
141
|
+
if getattr(args, "encrypt", False):
|
|
142
|
+
from .sync import crypt
|
|
143
|
+
|
|
144
|
+
try:
|
|
145
|
+
res = crypt.enable(channel)
|
|
146
|
+
except RuntimeError as e:
|
|
147
|
+
print("\nencryption not enabled: %s" % e)
|
|
148
|
+
return 2
|
|
149
|
+
print("sealed %s · %s key %s" % (
|
|
150
|
+
"already" if res["already"] else "yes", crypt.ALGO,
|
|
151
|
+
crypt.fingerprint(channel)))
|
|
152
|
+
if res["sealed"]:
|
|
153
|
+
print(" %d blob(s) already in the channel were re-sealed" %
|
|
154
|
+
res["sealed"])
|
|
155
|
+
print("passphrase is not in the channel: keep BATONPASS_KEY set on "
|
|
156
|
+
"every machine, or the blobs stay closed")
|
|
157
|
+
print("\nnothing left to do — `batonpass sync push %s` fills it" % channel)
|
|
158
|
+
return 0
|
|
159
|
+
|
|
160
|
+
if action == "compact":
|
|
161
|
+
from .sync import channel as ch
|
|
162
|
+
|
|
163
|
+
devs = [args.device or device_name()]
|
|
164
|
+
sdir = os.path.join(os.path.expanduser(channel), "stream")
|
|
165
|
+
if args.all and os.path.isdir(sdir):
|
|
166
|
+
devs = sorted(x for x in os.listdir(sdir)
|
|
167
|
+
if os.path.isdir(os.path.join(sdir, x)))
|
|
168
|
+
tiers = ["t0", "t1", "t2"] if args.tier is None else ["t%d" % args.tier]
|
|
169
|
+
rows = [ch.compact(channel, d, t, force=args.force)
|
|
170
|
+
for d in devs for t in tiers]
|
|
171
|
+
if args.json:
|
|
172
|
+
print(json.dumps(rows, ensure_ascii=False, indent=2, default=str))
|
|
173
|
+
return 0
|
|
174
|
+
for r in rows:
|
|
175
|
+
if not r["compacted"]:
|
|
176
|
+
print(" %-16s %s %8s unchanged (%s)" % (
|
|
177
|
+
r["device"], r["tier"], _fmt_bytes(r["before"]), r.get("reason")))
|
|
178
|
+
continue
|
|
179
|
+
print(" %-16s %s %8s → %8s gen %d %s lines" % (
|
|
180
|
+
r["device"], r["tier"], _fmt_bytes(r["before"]), _fmt_bytes(r["after"]),
|
|
181
|
+
r["gen"], format(r.get("lines_after", 0), ",")))
|
|
182
|
+
return 0
|
|
183
|
+
|
|
184
|
+
if action == "status":
|
|
185
|
+
conn = connect(args.db)
|
|
186
|
+
st = status(channel, conn)
|
|
187
|
+
if args.json:
|
|
188
|
+
print(json.dumps(st, ensure_ascii=False, indent=2, default=str))
|
|
189
|
+
return 0
|
|
190
|
+
print("channel %s" % st["channel"])
|
|
191
|
+
if not st["exists"]:
|
|
192
|
+
print(" (no channel here yet — run `batonpass sync init %s`)" % channel)
|
|
193
|
+
return 1
|
|
194
|
+
# Which channel is this, and can anyone read it? Both answers change what
|
|
195
|
+
# you do next, and neither is visible from the folder listing.
|
|
196
|
+
print(" identity %s" % (st.get("channel_id") or "?")[:12])
|
|
197
|
+
from .sync import crypt
|
|
198
|
+
|
|
199
|
+
m = crypt.marker(channel)
|
|
200
|
+
if m.get("enc"):
|
|
201
|
+
try:
|
|
202
|
+
print(" sealed %s, key %s" % (m["enc"], crypt.fingerprint(channel)))
|
|
203
|
+
except RuntimeError:
|
|
204
|
+
print(" sealed %s, but this machine has no key — the blobs stay closed"
|
|
205
|
+
% m["enc"])
|
|
206
|
+
else:
|
|
207
|
+
print(" sealed no (anyone holding this folder can read it)")
|
|
208
|
+
print("\ndevices")
|
|
209
|
+
for d in st["devices"]:
|
|
210
|
+
print(" %-16s last seen %s pushes %s" % (
|
|
211
|
+
d.get("device"), d.get("last_seen"), d.get("pushes")))
|
|
212
|
+
print("\nstreams")
|
|
213
|
+
for s in st["streams"]:
|
|
214
|
+
pct = 100.0 if not s["bytes"] else (100.0 * (s["read"] or 0) / s["bytes"])
|
|
215
|
+
print(" %-16s %s %8s read %5.1f%% pending %s" % (
|
|
216
|
+
s["device"], s["tier"], _fmt_bytes(s["bytes"]), pct,
|
|
217
|
+
_fmt_bytes(s["pending"])))
|
|
218
|
+
if st["offsets"]:
|
|
219
|
+
print("\nlocal library has read %d stream(s) up to a stored offset"
|
|
220
|
+
% len(st["offsets"]))
|
|
221
|
+
return 0
|
|
222
|
+
|
|
223
|
+
conn = connect(args.db or str(db_path()))
|
|
224
|
+
if action == "push":
|
|
225
|
+
st = push(channel, tier=args.tier, device=args.device, conn=conn,
|
|
226
|
+
sid_prefix=args.sid)
|
|
227
|
+
if args.json:
|
|
228
|
+
print(json.dumps(st, ensure_ascii=False, indent=2, default=str))
|
|
229
|
+
return 0 if not st["errors"] else 1
|
|
230
|
+
print("pushed to %s as %s (tier %d)" % (st["channel"], st["device"], st["tier"]))
|
|
231
|
+
print(" %d sessions · %d re-sent · %d up to date" % (
|
|
232
|
+
st["sessions"], st["sessions_sent"], st["skipped"]))
|
|
233
|
+
if st["lines"][1] or st["lines"][0]:
|
|
234
|
+
print(" lines: t0 %d, t1 %d, t2 %d" % (
|
|
235
|
+
st["lines"][0], st["lines"][1], st["lines"][2]))
|
|
236
|
+
if st["tier"] >= 2:
|
|
237
|
+
print(" %d messages, %d parts" % (st["messages"], st["parts"]))
|
|
238
|
+
print(" blobs: %d new, %s" % (st["blobs_new"], _fmt_bytes(st["blob_bytes"])))
|
|
239
|
+
shown = st["errors"][:5] # five of the same sentence is enough
|
|
240
|
+
for e in shown:
|
|
241
|
+
print(" ERROR %s" % e)
|
|
242
|
+
if len(st["errors"]) > len(shown):
|
|
243
|
+
print(" … and %d more, same reason" % (len(st["errors"]) - len(shown)))
|
|
244
|
+
if st["errors"]:
|
|
245
|
+
print("\n %d blob(s) stayed behind. The channel carries what this machine "
|
|
246
|
+
"could read,\n so it is smaller than the library, not wrong — "
|
|
247
|
+
"`batonpass doctor` names the missing piece." % len(st["errors"]))
|
|
248
|
+
return 0 if not st["errors"] else 1
|
|
249
|
+
|
|
250
|
+
st = pull(channel, tier=args.tier, device=args.device, conn=conn, sid_prefix=args.sid)
|
|
251
|
+
if args.json:
|
|
252
|
+
print(json.dumps(st, ensure_ascii=False, indent=2, default=str))
|
|
253
|
+
return 0
|
|
254
|
+
print("pulled from %s (up to tier %d)" % (st["channel"], st["tier"]))
|
|
255
|
+
print(" devices seen: %s" % (", ".join(st["devices"]) or "none"))
|
|
256
|
+
print(" %d sessions seen · %d new · %d summaries" % (
|
|
257
|
+
st["sessions_seen"], st["sessions_new"], st["heads"]))
|
|
258
|
+
if st["tier"] >= 2:
|
|
259
|
+
print(" %d new messages · %d new parts · %d blobs materialised" % (
|
|
260
|
+
st["messages_new"], st["parts_new"], st["blobs_new"]))
|
|
261
|
+
if st["bad_lines"]:
|
|
262
|
+
print(" %d unreadable line(s) skipped" % st["bad_lines"])
|
|
263
|
+
if st["missing"]:
|
|
264
|
+
print(" %d blob(s) referenced but absent — re-run pull with --tier 2"
|
|
265
|
+
% len(st["missing"]))
|
|
266
|
+
return 0
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
# ── identity & recall ───────────────────────────────────────────
|
|
271
|
+
|
|
272
|
+
def _when(iso):
|
|
273
|
+
return (iso or "")[:16].replace("T", " ")
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def cmd_index(args):
|
|
277
|
+
from .recall import build_index, index_stats
|
|
278
|
+
from .store.db import connect
|
|
279
|
+
conn = connect(args.db)
|
|
280
|
+
st = build_index(conn, force=args.rebuild)
|
|
281
|
+
after = index_stats(conn)
|
|
282
|
+
if args.json:
|
|
283
|
+
print(json.dumps({"built": st, "index": after}, ensure_ascii=False, indent=2))
|
|
284
|
+
return 0
|
|
285
|
+
print("indexed %d messages across %d sessions (%d empty)" % (
|
|
286
|
+
st["docs"], st["sessions"], st["skipped"]))
|
|
287
|
+
print("index %s · %d docs · %d/%d sessions searchable" % (
|
|
288
|
+
_fmt_bytes(after["bytes"]), after["docs"], after["sessions_indexed"],
|
|
289
|
+
after["sessions_total"]))
|
|
290
|
+
return 0
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def cmd_find(args):
|
|
294
|
+
from .recall import ensure_index, search
|
|
295
|
+
from .store.db import connect
|
|
296
|
+
conn = connect(args.db)
|
|
297
|
+
ensure_index(conn)
|
|
298
|
+
hits = search(conn, args.query, at=args.at, project=args.project,
|
|
299
|
+
device=args.device, limit=args.limit, explain=args.explain)
|
|
300
|
+
if args.json:
|
|
301
|
+
print(json.dumps(hits, ensure_ascii=False, indent=2))
|
|
302
|
+
return 0 if hits else 1
|
|
303
|
+
if not hits:
|
|
304
|
+
# The silence contract: never invent a hit to look helpful.
|
|
305
|
+
print("nothing matched %r%s" % (
|
|
306
|
+
args.query, " in " + args.at if args.at else ""))
|
|
307
|
+
from .recall import undecodable_hint
|
|
308
|
+
|
|
309
|
+
hint = undecodable_hint(conn)
|
|
310
|
+
print(hint or "try: a shorter word · batonpass tasks · drop --at")
|
|
311
|
+
return 1
|
|
312
|
+
print("%d hit%s%s" % (len(hits), "" if len(hits) == 1 else "s",
|
|
313
|
+
" (window %s)" % args.at if args.at else ""))
|
|
314
|
+
for i, h in enumerate(hits, 1):
|
|
315
|
+
print("\n%d) %s" % (i, h["project"] or "(no project)"))
|
|
316
|
+
print(" %s" % (h["title"] or "(untitled)")[:100])
|
|
317
|
+
print(" %s · %s · %d messages · %s" % (
|
|
318
|
+
_when(h["updated_at"]), h["device"] or "?", h["messages"], h["sid"][:12]))
|
|
319
|
+
if h["snippet"]:
|
|
320
|
+
print(" “%s”" % h["snippet"][:180])
|
|
321
|
+
if args.explain:
|
|
322
|
+
print(" matched on: %s" % ", ".join(h.get("why") or []))
|
|
323
|
+
print("\nbatonpass export <sid> --text to read one in full")
|
|
324
|
+
return 0
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def cmd_tasks(args):
|
|
328
|
+
from .identity import projects
|
|
329
|
+
from .store.db import connect
|
|
330
|
+
conn = connect(args.db)
|
|
331
|
+
rows = projects(conn, min_sessions=args.min, include_candidates=args.all)
|
|
332
|
+
if args.json:
|
|
333
|
+
print(json.dumps(rows, ensure_ascii=False, indent=2))
|
|
334
|
+
return 0
|
|
335
|
+
if not rows:
|
|
336
|
+
print("no projects yet — run `batonpass archive` first")
|
|
337
|
+
return 1
|
|
338
|
+
print("%-30s %5s %5s %-17s %s" % ("PROJECT", "SESS", "FILES", "LAST", "MACHINES"))
|
|
339
|
+
for p in rows[:args.limit]:
|
|
340
|
+
machines = p.get("device_list") or ""
|
|
341
|
+
print("%-30s %5d %5d %-17s %s%s" % (
|
|
342
|
+
p["label"][:30], p["sessions"], p["with_files"], _when(p["last_at"]),
|
|
343
|
+
machines[:30] or "?", " ⚠ stale" if p["stale"] else ""))
|
|
344
|
+
print("\n%d projects · batonpass card <name> for the full card" % len(rows))
|
|
345
|
+
return 0
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def cmd_card(args):
|
|
349
|
+
from .identity import card, projects, resolve
|
|
350
|
+
from .store.db import connect
|
|
351
|
+
conn = connect(args.db)
|
|
352
|
+
hits = resolve(conn, args.hint)
|
|
353
|
+
if not hits:
|
|
354
|
+
print("no project matches %r" % args.hint)
|
|
355
|
+
for p in projects(conn)[:10]:
|
|
356
|
+
print(" candidate: %s (%d sessions)" % (p["label"], p["sessions"]))
|
|
357
|
+
return 1
|
|
358
|
+
if len(hits) > 1 and not args.pick:
|
|
359
|
+
print("%d projects match %r — pick one with --pick:" % (len(hits), args.hint))
|
|
360
|
+
for i, p in enumerate(hits, 1):
|
|
361
|
+
print(" %d) %-28s %3d sessions %s" % (i, p["label"], p["sessions"],
|
|
362
|
+
_when(p["last_at"])))
|
|
363
|
+
return 2
|
|
364
|
+
target = hits[0] if not args.pick else hits[args.pick - 1]
|
|
365
|
+
print(card(conn, target["project_key"], args.sessions))
|
|
366
|
+
return 0
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def cmd_resume(args):
|
|
370
|
+
from .identity import resume_hint, projects
|
|
371
|
+
from .identity.project import derive, display
|
|
372
|
+
from .store.db import connect
|
|
373
|
+
import os
|
|
374
|
+
conn = connect(args.db)
|
|
375
|
+
cwd = args.cwd or os.getcwd()
|
|
376
|
+
if args.hint:
|
|
377
|
+
from .identity import card, resolve
|
|
378
|
+
hits = resolve(conn, args.hint)
|
|
379
|
+
print(card(conn, hits[0]["project_key"], args.sessions) if hits
|
|
380
|
+
else "no project matches %r" % args.hint)
|
|
381
|
+
return 0 if hits else 1
|
|
382
|
+
text = resume_hint(conn, cwd)
|
|
383
|
+
if not text:
|
|
384
|
+
key, _tier, _c = derive(cwd, "")
|
|
385
|
+
print("nothing recorded for %s yet." % (display(key) if key else cwd))
|
|
386
|
+
rows = projects(conn)[:8]
|
|
387
|
+
if rows:
|
|
388
|
+
print("\nmost recent work elsewhere:")
|
|
389
|
+
for p in rows:
|
|
390
|
+
print(" %-28s %3d sessions %s" % (p["label"], p["sessions"],
|
|
391
|
+
_when(p["last_at"])))
|
|
392
|
+
return 1
|
|
393
|
+
print(text)
|
|
394
|
+
return 0
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def cmd_identity(args):
|
|
398
|
+
from .identity import build
|
|
399
|
+
from .store.db import connect
|
|
400
|
+
conn = connect(args.db)
|
|
401
|
+
st = build(conn, extract_artifacts=not args.no_artifacts, limit=args.limit)
|
|
402
|
+
if args.json:
|
|
403
|
+
print(json.dumps(st, ensure_ascii=False, indent=2))
|
|
404
|
+
return 0
|
|
405
|
+
print("identity rebuilt: %d sessions%s" % (
|
|
406
|
+
st["sessions"], "" if not st["merged_subprojects"]
|
|
407
|
+
else " · %d sub-project(s) folded into their parent" % st["merged_subprojects"]))
|
|
408
|
+
n_projects = conn.execute(
|
|
409
|
+
"SELECT COUNT(DISTINCT project_key) c FROM session "
|
|
410
|
+
"WHERE project_key IS NOT NULL AND project_key<>''").fetchone()["c"]
|
|
411
|
+
print(" %d projects · %d ungrouped sessions · %d forks linked" % (
|
|
412
|
+
n_projects, st["unknown"], st["forks"]))
|
|
413
|
+
print(" artifacts %d in %d sessions" % (st["artifacts"], st["with_artifacts"]))
|
|
414
|
+
tiers = st["tiers"]
|
|
415
|
+
print(" confidence: high %d · medium %d · low %d" % (
|
|
416
|
+
tiers.get(1, 0), tiers.get(2, 0), tiers.get(3, 0)))
|
|
417
|
+
if st.get("kept_remote"):
|
|
418
|
+
# Sessions that came from another machine: our disk cannot out-vote their
|
|
419
|
+
# answer, so their project key stands as it arrived.
|
|
420
|
+
print(" kept %d project key(s) that arrived from another machine" % st["kept_remote"])
|
|
421
|
+
return 0
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def build_parser():
|
|
425
|
+
p = argparse.ArgumentParser(
|
|
426
|
+
prog="batonpass",
|
|
427
|
+
description="Never drop the baton. Read-only archive of the AI history on this machine.")
|
|
428
|
+
p.add_argument("--version", action="version", version="batonpass " + __version__)
|
|
429
|
+
sub = p.add_subparsers(dest="cmd")
|
|
430
|
+
|
|
431
|
+
d = sub.add_parser("discover", help="inventory local AI history (read-only, writes nothing)")
|
|
432
|
+
d.add_argument("--only", default=None)
|
|
433
|
+
d.add_argument("--report", default=None)
|
|
434
|
+
d.add_argument("--json", action="store_true")
|
|
435
|
+
d.set_defaults(func=cmd_discover)
|
|
436
|
+
|
|
437
|
+
a = sub.add_parser("archive", help="copy discovered history into the library")
|
|
438
|
+
a.add_argument("--only", default=None, help="comma separated adapters")
|
|
439
|
+
a.add_argument("--db", default=None)
|
|
440
|
+
a.add_argument("--level", type=int, default=1, choices=[1, 2, 3],
|
|
441
|
+
help="1 text only, 2 +raw, 3 +trained dictionary")
|
|
442
|
+
a.add_argument("--limit", type=int, default=None, help="max sessions per source")
|
|
443
|
+
a.add_argument("--dry-run", action="store_true")
|
|
444
|
+
a.add_argument("--json", action="store_true")
|
|
445
|
+
a.set_defaults(func=cmd_archive)
|
|
446
|
+
|
|
447
|
+
e = sub.add_parser("export", help="print one session as text or json")
|
|
448
|
+
e.add_argument("sid")
|
|
449
|
+
e.add_argument("--db", default=None)
|
|
450
|
+
e.add_argument("--out", default=None)
|
|
451
|
+
e.add_argument("--text", action="store_true")
|
|
452
|
+
e.set_defaults(func=cmd_export)
|
|
453
|
+
|
|
454
|
+
s = sub.add_parser("stats", help="what is in the library")
|
|
455
|
+
s.add_argument("--db", default=None)
|
|
456
|
+
s.add_argument("--json", action="store_true")
|
|
457
|
+
s.set_defaults(func=cmd_stats)
|
|
458
|
+
|
|
459
|
+
m = sub.add_parser("mcp", help="run the MCP server on stdio (three tools)")
|
|
460
|
+
m.add_argument("--db", default=None)
|
|
461
|
+
m.set_defaults(func=cmd_mcp)
|
|
462
|
+
|
|
463
|
+
d = sub.add_parser("doctor", help="check that everything can actually work")
|
|
464
|
+
d.add_argument("--db", default=None)
|
|
465
|
+
d.add_argument("--channel", default=None,
|
|
466
|
+
help="also check a channel folder")
|
|
467
|
+
d.add_argument("--sources", action="store_true",
|
|
468
|
+
help="also scan the machine for sources (a few seconds)")
|
|
469
|
+
d.add_argument("--json", action="store_true")
|
|
470
|
+
d.set_defaults(func=cmd_doctor)
|
|
471
|
+
|
|
472
|
+
y = sub.add_parser("sync", help="exchange history with your other machines")
|
|
473
|
+
ysub = y.add_subparsers(dest="action")
|
|
474
|
+
for name, hlp in (("init", "create a channel folder"),
|
|
475
|
+
("status", "who is in the channel, what is unread")):
|
|
476
|
+
q = ysub.add_parser(name, help=hlp)
|
|
477
|
+
q.add_argument("channel")
|
|
478
|
+
q.add_argument("--device", default=None)
|
|
479
|
+
q.add_argument("--db", default=None)
|
|
480
|
+
q.add_argument("--json", action="store_true")
|
|
481
|
+
if name == "init":
|
|
482
|
+
q.add_argument("--encrypt", action="store_true",
|
|
483
|
+
help="seal the payload with a passphrase (BATONPASS_KEY)")
|
|
484
|
+
q.set_defaults(func=cmd_sync)
|
|
485
|
+
for name, hlp in (("push", "write this machine's history into the channel"),
|
|
486
|
+
("pull", "read the channel into this machine's library")):
|
|
487
|
+
q = ysub.add_parser(name, help=hlp)
|
|
488
|
+
q.add_argument("channel")
|
|
489
|
+
q.add_argument("--tier", type=int, default=1 if name == "push" else 0,
|
|
490
|
+
choices=[0, 1, 2],
|
|
491
|
+
help="0 catalog, 1 summaries, 2 full messages")
|
|
492
|
+
q.add_argument("--sid", default=None, help="only this session prefix")
|
|
493
|
+
q.add_argument("--device", default=None)
|
|
494
|
+
q.add_argument("--db", default=None)
|
|
495
|
+
q.add_argument("--json", action="store_true")
|
|
496
|
+
q.set_defaults(func=cmd_sync)
|
|
497
|
+
q = ysub.add_parser("compact",
|
|
498
|
+
help="rewrite a stream as the set of facts it holds")
|
|
499
|
+
q.add_argument("channel")
|
|
500
|
+
q.add_argument("--tier", type=int, default=None, choices=[0, 1, 2])
|
|
501
|
+
q.add_argument("--device", default=None)
|
|
502
|
+
q.add_argument("--all", action="store_true",
|
|
503
|
+
help="every device's file, not just this machine's")
|
|
504
|
+
q.add_argument("--force", action="store_true", help="ignore the size thresholds")
|
|
505
|
+
q.add_argument("--db", default=None)
|
|
506
|
+
q.add_argument("--json", action="store_true")
|
|
507
|
+
q.set_defaults(func=cmd_sync)
|
|
508
|
+
y.set_defaults(func=None)
|
|
509
|
+
|
|
510
|
+
i = sub.add_parser("index", help="build the local search index (never synced)")
|
|
511
|
+
i.add_argument("--db", default=None)
|
|
512
|
+
i.add_argument("--rebuild", action="store_true", help="throw it away and redo")
|
|
513
|
+
i.add_argument("--json", action="store_true")
|
|
514
|
+
i.set_defaults(func=cmd_index)
|
|
515
|
+
|
|
516
|
+
f = sub.add_parser("find", help="find it again from a half-remembered word")
|
|
517
|
+
f.add_argument("query")
|
|
518
|
+
f.add_argument("--at", default=None,
|
|
519
|
+
help="7d · 12h · today · yesterday · 2026-09 · 2026-09-20 · "
|
|
520
|
+
"2026-09-01..2026-09-20")
|
|
521
|
+
f.add_argument("--project", default=None)
|
|
522
|
+
f.add_argument("--device", default=None)
|
|
523
|
+
f.add_argument("--limit", type=int, default=5)
|
|
524
|
+
f.add_argument("--explain", action="store_true", help="which signal matched")
|
|
525
|
+
f.add_argument("--db", default=None)
|
|
526
|
+
f.add_argument("--json", action="store_true")
|
|
527
|
+
f.set_defaults(func=cmd_find)
|
|
528
|
+
|
|
529
|
+
t = sub.add_parser("tasks", help="what is in flight, one line per project")
|
|
530
|
+
t.add_argument("--min", type=int, default=1, help="sessions required to show")
|
|
531
|
+
t.add_argument("--limit", type=int, default=30)
|
|
532
|
+
t.add_argument("--all", action="store_true", help="include low-confidence clusters")
|
|
533
|
+
t.add_argument("--db", default=None)
|
|
534
|
+
t.add_argument("--json", action="store_true")
|
|
535
|
+
t.set_defaults(func=cmd_tasks)
|
|
536
|
+
|
|
537
|
+
c = sub.add_parser("card", help="the handover card for one project")
|
|
538
|
+
c.add_argument("hint")
|
|
539
|
+
c.add_argument("--sessions", type=int, default=4)
|
|
540
|
+
c.add_argument("--pick", type=int, default=None, help="when several match, choose 1..n")
|
|
541
|
+
c.add_argument("--db", default=None)
|
|
542
|
+
c.set_defaults(func=cmd_card)
|
|
543
|
+
|
|
544
|
+
r = sub.add_parser("resume", help="pick up where this directory left off")
|
|
545
|
+
r.add_argument("hint", nargs="?", default=None)
|
|
546
|
+
r.add_argument("--cwd", default=None)
|
|
547
|
+
r.add_argument("--sessions", type=int, default=4)
|
|
548
|
+
r.add_argument("--db", default=None)
|
|
549
|
+
r.set_defaults(func=cmd_resume)
|
|
550
|
+
|
|
551
|
+
n = sub.add_parser("identity", help="recompute project identity and artifacts")
|
|
552
|
+
n.add_argument("--db", default=None)
|
|
553
|
+
n.add_argument("--no-artifacts", action="store_true")
|
|
554
|
+
n.add_argument("--limit", type=int, default=None)
|
|
555
|
+
n.add_argument("--json", action="store_true")
|
|
556
|
+
n.set_defaults(func=cmd_identity)
|
|
557
|
+
return p
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def main(argv=None):
|
|
561
|
+
argv = sys.argv[1:] if argv is None else argv
|
|
562
|
+
parser = build_parser()
|
|
563
|
+
args = parser.parse_args(argv)
|
|
564
|
+
if not getattr(args, "func", None):
|
|
565
|
+
parser.print_help()
|
|
566
|
+
return 2
|
|
567
|
+
try:
|
|
568
|
+
return args.func(args) or 0
|
|
569
|
+
except BrokenPipeError:
|
|
570
|
+
# `batonpass export … | head` is a normal thing to do.
|
|
571
|
+
try:
|
|
572
|
+
sys.stdout.close()
|
|
573
|
+
finally:
|
|
574
|
+
return 0
|
|
575
|
+
except (RuntimeError, OSError, sqlite3.OperationalError) as e:
|
|
576
|
+
# A missing optional dependency, an unreadable disk and a half-written
|
|
577
|
+
# library are all one-liners to a human, not a stack trace to read.
|
|
578
|
+
print("batonpass: %s" % e, file=sys.stderr)
|
|
579
|
+
return 2
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Source discovery — the read-only half of batonpass.
|
|
2
|
+
|
|
3
|
+
`probe.py` is the scanner: it walks the well-known locations, identifies what it
|
|
4
|
+
finds, and explains itself. It opens every database with `mode=ro` and never
|
|
5
|
+
imports a network module, which is what lets the README claim "zero writes" and
|
|
6
|
+
mean it literally.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
|
|
13
|
+
from .probe import ( # noqa: F401
|
|
14
|
+
VERSION,
|
|
15
|
+
main,
|
|
16
|
+
probe_roots,
|
|
17
|
+
render,
|
|
18
|
+
resolve,
|
|
19
|
+
run,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = ["VERSION", "main", "probe_roots", "render", "resolve", "run", "sources"]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def sources(deep=False):
|
|
26
|
+
"""Discovered, recognised sources as plain dicts — what `archive` consumes."""
|
|
27
|
+
args = argparse.Namespace(only=None, explain=False, deep=deep,
|
|
28
|
+
report=None, json_path=None)
|
|
29
|
+
summary = run(args)
|
|
30
|
+
return [s for s in summary["sources"] if s.get("adapter")]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def summary(deep=False):
|
|
34
|
+
args = argparse.Namespace(only=None, explain=False, deep=deep,
|
|
35
|
+
report=None, json_path=None)
|
|
36
|
+
return run(args)
|