Repository navigation
Expand file tree
/
Copy pathdb.py
More file actions
1154 lines (950 loc) · 41.1 KB
/
Copy pathdb.py
File metadata and controls
1154 lines (950 loc) · 41.1 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
import json
import logging
import os
import shutil
import sqlite3
import time
import uuid
from datetime import datetime, timedelta, timezone
from pathlib import Path
log = logging.getLogger(__name__)
def config_dir() -> Path:
p = Path(os.environ.get("SHELLDECK_HOME") or Path.home() / ".config" / "shelldeck")
p.mkdir(parents=True, exist_ok=True)
return p
def db_path() -> Path:
return config_dir() / "shelldeck.db"
def _connect() -> sqlite3.Connection:
conn = sqlite3.connect(db_path(), timeout=10)
conn.execute("PRAGMA foreign_keys = ON")
return conn
def _import_termy_db() -> None:
"""One-time copy of the old termy database, so projects/bookmarks/tasks carry over."""
old = Path.home() / ".config" / "termy" / "termy.db"
if "SHELLDECK_HOME" in os.environ or db_path().exists() or not old.exists():
return
shutil.copy2(old, db_path())
log.info("imported %s", old)
def short_id() -> str:
return uuid.uuid4().hex[:8]
def _now() -> str:
return datetime.now(timezone.utc).isoformat()
def init_db() -> None:
_import_termy_db()
with _connect() as conn:
conn.execute("PRAGMA journal_mode = WAL")
conn.executescript(
"""
CREATE TABLE IF NOT EXISTS projects (
id TEXT PRIMARY KEY,
path TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
project_id TEXT NOT NULL,
cwd TEXT,
shell TEXT,
cols INTEGER,
rows INTEGER,
created_at TEXT NOT NULL,
name TEXT,
FOREIGN KEY (project_id) REFERENCES projects(id)
);
CREATE TABLE IF NOT EXISTS bookmarks (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
command TEXT NOT NULL,
project_id TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL
);
CREATE INDEX IF NOT EXISTS idx_bookmarks_project ON bookmarks(project_id);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT
);
CREATE TABLE IF NOT EXISTS tasks (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
description TEXT,
board_column TEXT DEFAULT 'backlog',
priority TEXT DEFAULT 'medium',
due_at TEXT,
reminder_at TEXT,
reminder_acknowledged INTEGER DEFAULT 0,
tags TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_tasks_column ON tasks(board_column);
CREATE TABLE IF NOT EXISTS scheduled_jobs (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
project_id TEXT NOT NULL,
command TEXT NOT NULL,
cron TEXT NOT NULL,
timezone TEXT DEFAULT 'UTC',
enabled INTEGER DEFAULT 1,
timeout_seconds INTEGER DEFAULT 60,
next_run_on TEXT,
last_run_on TEXT,
last_status TEXT DEFAULT 'never_run',
last_error TEXT,
last_duration_ms INTEGER,
total_runs INTEGER DEFAULT 0,
successful_runs INTEGER DEFAULT 0,
failed_runs INTEGER DEFAULT 0,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_scheduled_jobs_project ON scheduled_jobs(project_id);
CREATE TABLE IF NOT EXISTS job_runs (
id TEXT PRIMARY KEY,
job_id TEXT NOT NULL,
run_at TEXT NOT NULL,
status TEXT NOT NULL,
exit_code INTEGER,
output TEXT,
error TEXT,
duration_ms INTEGER,
created_at TEXT NOT NULL,
FOREIGN KEY (job_id) REFERENCES scheduled_jobs(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_job_runs_job_id ON job_runs(job_id);
-- browser logins: sha256 of the cookie token, never the token itself
CREATE TABLE IF NOT EXISTS auth_sessions (
token_hash TEXT PRIMARY KEY,
created_at REAL NOT NULL,
last_seen REAL NOT NULL,
client TEXT,
via TEXT,
kind TEXT
);
-- Expo push tokens of logged-in app devices (session_hash = auth_sessions.token_hash)
CREATE TABLE IF NOT EXISTS push_tokens (
token TEXT PRIMARY KEY,
session_hash TEXT NOT NULL,
created_at REAL NOT NULL
);
-- iOS app devices the host trusts: they pass the share gate without approval (the password is
-- still needed). token_hash = sha256 of the device cookie; topic + key carry the share URL via ntfy.
CREATE TABLE IF NOT EXISTS trusted_devices (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
token_hash TEXT NOT NULL UNIQUE,
topic TEXT NOT NULL,
key TEXT NOT NULL,
created_at REAL NOT NULL,
last_seen REAL
);
-- command history (sessions/projects may be deleted later; keep the text)
CREATE TABLE IF NOT EXISTS commands (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT,
project_id TEXT,
cwd TEXT,
command TEXT NOT NULL,
exit_code INTEGER,
duration_ms INTEGER,
started_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_commands_started ON commands(started_at);
-- remote systems: ssh terminals (typed into a local shell) and rdp desktops (mstsc / xfreerdp)
CREATE TABLE IF NOT EXISTS remotes (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
kind TEXT NOT NULL,
host TEXT NOT NULL,
user TEXT NOT NULL DEFAULT '',
port INTEGER,
identity TEXT NOT NULL DEFAULT '',
project_id TEXT,
created_at TEXT NOT NULL,
last_used_at TEXT
);
-- scratchpad: one markdown body per note, the title is its first line
CREATE TABLE IF NOT EXISTS scratch (
id TEXT PRIMARY KEY,
body TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
-- agent hand-offs; <project>/.shelldeck/handoff.md is rendered from these
CREATE TABLE IF NOT EXISTS handoffs (
id TEXT PRIMARY KEY,
project_id TEXT NOT NULL,
from_sid TEXT,
from_nick TEXT,
to_sid TEXT,
to_nick TEXT,
agent TEXT,
model TEXT,
task TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'open',
result TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_handoffs_project ON handoffs(project_id);
-- native agent conversations reported by built-in integrations (resume_argv is validated argv, never shell text)
CREATE TABLE IF NOT EXISTS agent_sessions (
session_id TEXT PRIMARY KEY,
agent TEXT NOT NULL,
source TEXT NOT NULL,
integration_version INTEGER,
native_session_id TEXT,
resume_argv_json TEXT,
metadata_json TEXT,
last_state TEXT NOT NULL DEFAULT 'unknown',
last_seen_at TEXT NOT NULL,
resume_enabled INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE
);
DROP TABLE IF EXISTS note_tags;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS mindmap_jobs;
DROP TABLE IF EXISTS mindmap_index_jobs;
DROP TABLE IF EXISTS mindmap_nodes;
"""
)
cols = {r[1] for r in conn.execute("PRAGMA table_info(sessions)")}
if "name" not in cols:
conn.execute("ALTER TABLE sessions ADD COLUMN name TEXT")
for col in ("nick", "parent"):
if col not in cols:
conn.execute(f"ALTER TABLE sessions ADD COLUMN {col} TEXT")
for (sid,) in conn.execute("SELECT id FROM sessions WHERE nick IS NULL ORDER BY created_at").fetchall():
conn.execute("UPDATE sessions SET nick = ? WHERE id = ?", (_free_nick(conn), sid))
cols = {r[1] for r in conn.execute("PRAGMA table_info(auth_sessions)")}
for col in ("via", "kind"): # kind: browser | remote (the iOS app; never takes over)
if col not in cols:
conn.execute(f"ALTER TABLE auth_sessions ADD COLUMN {col} TEXT")
conn.execute("DELETE FROM auth_sessions WHERE via LIKE 'share:%'") # shares end with the server
cols = {r[1] for r in conn.execute("PRAGMA table_info(projects)")}
if "color" not in cols:
conn.execute("ALTER TABLE projects ADD COLUMN color INTEGER")
# remote_id/remote_path: a folder on a remote machine (remotes table); NULL = this machine
# workspace: the named group it belongs to (sidebar filter); NULL = none
for col in ("remote_id", "remote_path", "workspace"):
if col not in cols:
conn.execute(f"ALTER TABLE projects ADD COLUMN {col} TEXT")
missing = conn.execute("SELECT id FROM projects WHERE color IS NULL ORDER BY created_at").fetchall()
for (pid,) in missing:
conn.execute("UPDATE projects SET color = ? WHERE id = ?", (_next_color(conn), pid))
PROJECT_COLORS = 8
def _next_color(conn: sqlite3.Connection) -> int:
"""Least-used palette slot, so each new project gets a distinct color."""
used = dict(conn.execute("SELECT color, COUNT(*) FROM projects WHERE color IS NOT NULL GROUP BY color").fetchall())
return min(range(PROJECT_COLORS), key=lambda c: (used.get(c, 0), c))
def ensure_project(path: str) -> dict:
path = str(Path(path).resolve())
name = Path(path).name or "root"
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM projects WHERE path = ?", (path,)).fetchone()
if row:
return dict(row)
pid = short_id()
color = _next_color(conn)
conn.execute(
"INSERT INTO projects (id, path, name, created_at, color) VALUES (?, ?, ?, ?, ?)",
(pid, path, name, _now(), color),
)
conn.commit()
return {"id": pid, "path": path, "name": name, "created_at": _now(), "color": color}
def ensure_remote_project(remote_id: str, path: str, name: str | None = None) -> dict:
"""A folder on a remote machine. `path` stays unique by keying it as ssh://<remote>/<folder>."""
key = f"ssh://{remote_id}/{path}"
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM projects WHERE path = ?", (key,)).fetchone()
if row:
return dict(row)
pid = short_id()
color = _next_color(conn)
name = name or (path.rstrip("/").rsplit("/", 1)[-1] or path)
conn.execute("INSERT INTO projects (id, path, name, created_at, color, remote_id, remote_path) VALUES (?, ?, ?, ?, ?, ?, ?)",
(pid, key, name, _now(), color, remote_id, path))
conn.commit()
return {"id": pid, "path": key, "name": name, "created_at": _now(), "color": color, "remote_id": remote_id, "remote_path": path}
def remote_projects(remote_id: str) -> list[dict]:
return [p for p in list_projects() if p.get("remote_id") == remote_id]
def list_projects() -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT * FROM projects ORDER BY created_at"
).fetchall()
return [dict(r) for r in rows]
def get_project(project_id: str) -> dict | None:
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute(
"SELECT * FROM projects WHERE id = ?", (project_id,)
).fetchone()
return dict(row) if row else None
def update_project(project_id: str, fields: dict) -> dict | None:
"""Set `name` and/or `workspace` (None clears it)."""
with _connect() as conn:
for col in ("name", "workspace"):
if col in fields:
conn.execute(f"UPDATE projects SET {col} = ? WHERE id = ?", (fields[col], project_id))
conn.commit()
return get_project(project_id)
# a person's name per terminal, so people and agents can say "ask Maya" instead of an id
NICKS = (
"Ada", "Alan", "Amara", "Anya", "Arjun", "Asha", "Aiko", "Bea", "Bruno", "Chen", "Chiara", "Dara", "Dev", "Diego",
"Elif", "Emil", "Esme", "Ezra", "Farah", "Felix", "Freya", "Gus", "Hana", "Hugo", "Ines", "Ivan", "Iris", "Jana",
"Jonas", "Juno", "Kai", "Kamal", "Kenji", "Kira", "Lars", "Leila", "Lena", "Leo", "Lina", "Luca", "Maya", "Mateo",
"Mei", "Milo", "Mira", "Nadia", "Nia", "Nico", "Noor", "Omar", "Oona", "Otto", "Priya", "Quinn", "Rafa", "Ravi",
"Rosa", "Rumi", "Sana", "Sami", "Sven", "Tara", "Theo", "Tomas", "Uma", "Vera", "Wen", "Yara", "Yusuf", "Zara",
"Zoe", "Ari", "Bo", "Cleo", "Dina", "Eli", "Fina", "Gil", "Hiro", "Ida", "Joss", "Kofi", "Lua", "Nell",
)
def _free_nick(conn: sqlite3.Connection) -> str:
"""A random name no terminal has yet (Maya2 once every name is taken)."""
import random
used = {r[0].casefold() for r in conn.execute("SELECT nick FROM sessions WHERE nick IS NOT NULL")}
free = [n for n in NICKS if n.casefold() not in used]
if free:
return random.choice(free)
base = random.choice(NICKS)
return next(f"{base}{i}" for i in range(2, 10_000) if f"{base}{i}".casefold() not in used)
def add_session(
project_id: str, cwd: str | None = None, shell: str | None = None, name: str | None = None, parent: str | None = None
) -> dict:
sid = short_id()
cwd = cwd or "."
shell = shell or ""
cols, rows = 120, 24
with _connect() as conn:
nick = _free_nick(conn)
conn.execute(
"INSERT INTO sessions (id, project_id, cwd, shell, cols, rows, created_at, name, nick, parent) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(sid, project_id, cwd, shell, cols, rows, _now(), name, nick, parent),
)
conn.commit()
return {
"id": sid,
"project_id": project_id,
"cwd": cwd,
"shell": shell,
"name": name,
"nick": nick,
"parent": parent,
"cols": cols,
"rows": rows,
}
def list_sessions(project_id: str | None = None) -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
if project_id:
rows = conn.execute(
"SELECT * FROM sessions WHERE project_id = ? ORDER BY created_at",
(project_id,),
).fetchall()
else:
rows = conn.execute(
"SELECT * FROM sessions ORDER BY created_at DESC"
).fetchall()
return [dict(r) for r in rows]
def get_session(session_id: str) -> dict | None:
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute(
"SELECT * FROM sessions WHERE id = ?", (session_id,)
).fetchone()
return dict(row) if row else None
def delete_session(session_id: str) -> None:
with _connect() as conn:
conn.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
conn.commit()
def rename_session(session_id: str, name: str) -> dict | None:
with _connect() as conn:
conn.execute("UPDATE sessions SET name = ? WHERE id = ?", (name, session_id))
conn.commit()
return get_session(session_id)
def update_session_cwd(session_id: str, cwd: str) -> None:
with _connect() as conn:
conn.execute("UPDATE sessions SET cwd = ? WHERE id = ?", (cwd, session_id))
conn.commit()
def update_session_size(session_id: str, cols: int, rows: int) -> None:
with _connect() as conn:
conn.execute(
"UPDATE sessions SET cols = ?, rows = ? WHERE id = ?",
(cols, rows, session_id),
)
conn.commit()
def delete_project(project_id: str) -> None:
with _connect() as conn:
conn.execute("DELETE FROM sessions WHERE project_id = ?", (project_id,))
conn.execute("DELETE FROM scheduled_jobs WHERE project_id = ?", (project_id,))
conn.execute("DELETE FROM projects WHERE id = ?", (project_id,))
conn.commit()
def list_sessions_with_project() -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT s.id, s.name, s.nick, s.parent, s.shell, s.cwd, s.created_at, p.id as project_id, p.name as project_name, p.path as project_path "
"FROM sessions s JOIN projects p ON s.project_id = p.id "
"ORDER BY p.name, s.created_at DESC"
).fetchall()
return [dict(r) for r in rows]
def add_auth_session(token_hash: str, now: float, client: str, via: str = "local", kind: str = "browser") -> None:
with _connect() as conn:
conn.execute(
"INSERT INTO auth_sessions (token_hash, created_at, last_seen, client, via, kind) VALUES (?, ?, ?, ?, ?, ?)",
(token_hash, now, now, client[:200], via, kind),
)
conn.commit()
def list_auth_sessions() -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
return [dict(r) for r in conn.execute("SELECT * FROM auth_sessions ORDER BY last_seen DESC")]
def delete_auth_sessions_via(via: str) -> list[str]:
"""Delete the logins made through `via` (e.g. one share); returns their hashes."""
with _connect() as conn:
gone = [r[0] for r in conn.execute("SELECT token_hash FROM auth_sessions WHERE via = ?", (via,))]
conn.execute("DELETE FROM auth_sessions WHERE via = ?", (via,))
conn.commit()
return gone
def add_push_token(token: str, session_hash: str) -> None:
with _connect() as conn:
conn.execute("INSERT OR REPLACE INTO push_tokens (token, session_hash, created_at) VALUES (?, ?, ?)", (token, session_hash, time.time()))
conn.commit()
def delete_push_token(token: str, session_hash: str) -> bool:
with _connect() as conn:
n = conn.execute("DELETE FROM push_tokens WHERE token = ? AND session_hash = ?", (token, session_hash)).rowcount
conn.commit()
return n > 0
def delete_push_tokens(session_hashes: list[str]) -> None:
with _connect() as conn:
conn.executemany("DELETE FROM push_tokens WHERE session_hash = ?", [(h,) for h in session_hashes])
conn.commit()
def add_trusted_device(name: str, token_hash: str, topic: str, key: str) -> dict:
device = {"id": short_id(), "name": name[:80], "token_hash": token_hash, "topic": topic, "key": key, "created_at": time.time()}
with _connect() as conn:
conn.execute("INSERT INTO trusted_devices (id, name, token_hash, topic, key, created_at) VALUES "
"(:id, :name, :token_hash, :topic, :key, :created_at)", device)
conn.commit()
return device
def list_trusted_devices() -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
return [dict(r) for r in conn.execute("SELECT * FROM trusted_devices ORDER BY created_at")]
def trusted_device(token_hash: str) -> dict | None:
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM trusted_devices WHERE token_hash = ?", (token_hash,)).fetchone()
return dict(row) if row else None
def touch_trusted_device(device_id: str) -> None:
with _connect() as conn:
conn.execute("UPDATE trusted_devices SET last_seen = ? WHERE id = ?", (time.time(), device_id))
conn.commit()
def delete_trusted_device(device_id: str) -> bool:
with _connect() as conn:
n = conn.execute("DELETE FROM trusted_devices WHERE id = ?", (device_id,)).rowcount
conn.commit()
return n > 0
def list_push_tokens() -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
return [dict(r) for r in conn.execute("SELECT * FROM push_tokens")]
def get_auth_session(token_hash: str) -> dict | None:
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM auth_sessions WHERE token_hash = ?", (token_hash,)).fetchone()
return dict(row) if row else None
def touch_auth_session(token_hash: str, now: float) -> None:
with _connect() as conn:
conn.execute("UPDATE auth_sessions SET last_seen = ? WHERE token_hash = ?", (now, token_hash))
conn.commit()
def delete_auth_sessions(keep: str | None = None, only: str | None = None) -> None:
"""Delete one session (`only`), or all except `keep`."""
with _connect() as conn:
if only:
conn.execute("DELETE FROM auth_sessions WHERE token_hash = ?", (only,))
else:
conn.execute("DELETE FROM auth_sessions WHERE token_hash != ?", (keep or "",))
conn.commit()
HISTORY_LIMIT = 20000
def add_command(session_id: str, project_id: str | None, cwd: str | None, command: str,
exit_code: int | None, duration_ms: int | None, started_at: str) -> None:
with _connect() as conn:
last = conn.execute(
"SELECT command, started_at FROM commands WHERE session_id = ? ORDER BY id DESC LIMIT 1", (session_id,)
).fetchone()
if last and last[0] == command and last[1] == started_at:
return # same command reported by a second window on this terminal
conn.execute(
"INSERT INTO commands (session_id, project_id, cwd, command, exit_code, duration_ms, started_at)"
" VALUES (?, ?, ?, ?, ?, ?, ?)",
(session_id, project_id, cwd, command, exit_code, duration_ms, started_at),
)
conn.execute("DELETE FROM commands WHERE id <= (SELECT MAX(id) FROM commands) - ?", (HISTORY_LIMIT,))
conn.commit()
def list_commands(q: str = "", project_id: str | None = None, failed: bool = False, limit: int = 300) -> list[dict]:
sql = "SELECT * FROM commands WHERE command LIKE ? ESCAPE '\\'"
like = "%" + q.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%"
args: list = [like]
if project_id:
sql += " AND project_id = ?"
args.append(project_id)
if failed:
sql += " AND exit_code IS NOT NULL AND exit_code != 0"
sql += " ORDER BY id DESC LIMIT ?"
args.append(limit)
with _connect() as conn:
conn.row_factory = sqlite3.Row
return [dict(r) for r in conn.execute(sql, args).fetchall()]
def clear_commands() -> None:
with _connect() as conn:
conn.execute("DELETE FROM commands")
conn.commit()
def list_bookmarks() -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT * FROM bookmarks ORDER BY updated_at DESC"
).fetchall()
return [dict(r) for r in rows]
def get_bookmark(bookmark_id: str) -> dict | None:
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute(
"SELECT * FROM bookmarks WHERE id = ?", (bookmark_id,)
).fetchone()
return dict(row) if row else None
def add_bookmark(name: str, command: str, project_id: str | None = None) -> dict:
bid = short_id()
now = _now()
with _connect() as conn:
conn.execute(
"INSERT INTO bookmarks (id, name, command, project_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
(bid, name, command, project_id, now, now),
)
conn.commit()
return {
"id": bid,
"name": name,
"command": command,
"project_id": project_id,
"created_at": now,
"updated_at": now,
}
def update_bookmark(
bookmark_id: str,
name: str,
command: str,
project_id: str | None = None,
) -> dict | None:
if not get_bookmark(bookmark_id):
return None
now = _now()
with _connect() as conn:
conn.execute(
"UPDATE bookmarks SET name = ?, command = ?, project_id = ?, updated_at = ? WHERE id = ?",
(name, command, project_id, now, bookmark_id),
)
conn.commit()
return get_bookmark(bookmark_id)
def delete_bookmark(bookmark_id: str) -> None:
with _connect() as conn:
conn.execute("DELETE FROM bookmarks WHERE id = ?", (bookmark_id,))
conn.commit()
def get_setting(key: str, default: str | None = None) -> str | None:
with _connect() as conn:
row = conn.execute(
"SELECT value FROM settings WHERE key = ?", (key,)
).fetchone()
return row[0] if row else default
def set_setting(key: str, value: str) -> None:
with _connect() as conn:
conn.execute(
"INSERT INTO settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = ?",
(key, value, value),
)
conn.commit()
def delete_setting(key: str) -> None:
with _connect() as conn:
conn.execute("DELETE FROM settings WHERE key = ?", (key,))
conn.commit()
# ---------------------------------------------------------------------------
# Tasks
# ---------------------------------------------------------------------------
def add_task(
title: str,
description: str = "",
board_column: str = "backlog",
priority: str = "medium",
due_at: str | None = None,
reminder_at: str | None = None,
tags: str | None = None,
) -> dict:
tid = short_id()
now = _now()
with _connect() as conn:
conn.execute(
"INSERT INTO tasks (id, title, description, board_column, priority, due_at, reminder_at, tags, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
tid,
title,
description,
board_column,
priority,
due_at,
reminder_at,
tags,
now,
now,
),
)
conn.commit()
return get_task(tid)
def get_task(task_id: str) -> dict | None:
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute("SELECT * FROM tasks WHERE id = ?", (task_id,)).fetchone()
return dict(row) if row else None
def list_tasks(board_column: str | None = None) -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
if board_column:
rows = conn.execute(
"SELECT * FROM tasks WHERE board_column = ? ORDER BY updated_at DESC",
(board_column,),
).fetchall()
else:
rows = conn.execute(
"SELECT * FROM tasks ORDER BY updated_at DESC"
).fetchall()
return [dict(r) for r in rows]
def update_task(
task_id: str,
title: str | None = None,
description: str | None = None,
board_column: str | None = None,
priority: str | None = None,
due_at: str | None = None,
reminder_at: str | None = None,
reminder_acknowledged: int | None = None,
tags: str | None = None,
) -> dict | None:
if not get_task(task_id):
return None
now = _now()
with _connect() as conn:
conn.execute(
"UPDATE tasks SET title = COALESCE(?, title), description = COALESCE(?, description), board_column = COALESCE(?, board_column), priority = COALESCE(?, priority), due_at = COALESCE(?, due_at), reminder_at = COALESCE(?, reminder_at), reminder_acknowledged = COALESCE(?, reminder_acknowledged), tags = COALESCE(?, tags), updated_at = ? WHERE id = ?",
(
title,
description,
board_column,
priority,
due_at,
reminder_at,
reminder_acknowledged,
tags,
now,
task_id,
),
)
conn.commit()
return get_task(task_id)
def move_task(task_id: str, board_column: str) -> dict | None:
return update_task(task_id, board_column=board_column)
def delete_task(task_id: str) -> None:
with _connect() as conn:
conn.execute("DELETE FROM tasks WHERE id = ?", (task_id,))
conn.commit()
def ack_reminder(task_id: str) -> dict | None:
return update_task(task_id, reminder_at=None, reminder_acknowledged=1)
def snooze_reminder(task_id: str, minutes: int = 15) -> dict | None:
t = get_task(task_id)
if not t:
return None
base = datetime.now(timezone.utc)
if t.get("reminder_at"):
try:
base = datetime.fromisoformat(t["reminder_at"]).astimezone(timezone.utc)
except ValueError:
pass
new_time = base + timedelta(minutes=minutes)
return update_task(
task_id, reminder_at=new_time.isoformat(), reminder_acknowledged=0
)
def list_active_alarms(now_iso: str | None = None) -> list[dict]:
now = now_iso or _now()
with _connect() as conn:
conn.row_factory = sqlite3.Row
rows = conn.execute(
"SELECT * FROM tasks WHERE board_column != 'done' AND reminder_at <= ? AND (reminder_acknowledged = 0 OR reminder_acknowledged IS NULL) ORDER BY reminder_at ASC",
(now,),
).fetchall()
return [dict(r) for r in rows]
# ---------------------------------------------------------------------------
# Scheduled jobs
# ---------------------------------------------------------------------------
def add_job(
name: str,
project_id: str,
command: str,
cron: str,
timezone: str = "UTC",
enabled: bool = True,
timeout_seconds: int = 60,
) -> dict:
jid = short_id()
now = _now()
with _connect() as conn:
conn.execute(
"INSERT INTO scheduled_jobs (id, name, project_id, command, cron, timezone, enabled, timeout_seconds, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(
jid,
name,
project_id,
command,
cron,
timezone,
int(enabled),
timeout_seconds,
now,
now,
),
)
conn.commit()
return get_job(jid)
def get_job(job_id: str) -> dict | None:
with _connect() as conn:
conn.row_factory = sqlite3.Row
row = conn.execute(
"SELECT * FROM scheduled_jobs WHERE id = ?", (job_id,)
).fetchone()
return dict(row) if row else None
def list_jobs() -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row
rows = conn.execute("SELECT * FROM scheduled_jobs ORDER BY name").fetchall()
return [dict(r) for r in rows]
def update_job(
job_id: str,
name: str | None = None,
project_id: str | None = None,
command: str | None = None,
cron: str | None = None,
timezone: str | None = None,
enabled: bool | None = None,
timeout_seconds: int | None = None,
next_run_on: str | None = None,
last_run_on: str | None = None,
last_status: str | None = None,
last_error: str | None = None,
last_duration_ms: int | None = None,
) -> dict | None:
if not get_job(job_id):
return None
now = _now()
with _connect() as conn:
conn.execute(
"UPDATE scheduled_jobs SET name = COALESCE(?, name), project_id = COALESCE(?, project_id), command = COALESCE(?, command), cron = COALESCE(?, cron), timezone = COALESCE(?, timezone), enabled = COALESCE(?, enabled), timeout_seconds = COALESCE(?, timeout_seconds), next_run_on = COALESCE(?, next_run_on), last_run_on = COALESCE(?, last_run_on), last_status = COALESCE(?, last_status), last_error = COALESCE(?, last_error), last_duration_ms = COALESCE(?, last_duration_ms), updated_at = ? WHERE id = ?",
(
name,
project_id,
command,
cron,
timezone,
int(enabled) if enabled is not None else None,
timeout_seconds,
next_run_on,
last_run_on,
last_status,
last_error,
last_duration_ms,
now,
job_id,
),
)
conn.commit()
return get_job(job_id)
def delete_job(job_id: str) -> None:
with _connect() as conn:
conn.execute("DELETE FROM job_runs WHERE job_id = ?", (job_id,))
conn.execute("DELETE FROM scheduled_jobs WHERE id = ?", (job_id,))
conn.commit()
def toggle_job(job_id: str) -> dict | None:
job = get_job(job_id)
if not job:
return None
return update_job(job_id, enabled=not bool(job.get("enabled")))
def update_job_after_run(
job_id: str,
last_status: str,
last_run_on: str,
last_duration_ms: int,
last_error: str | None,
next_run_on: str | None,
successful: bool,
) -> dict | None:
if successful:
col = "successful_runs"
else:
col = "failed_runs"
now = _now()
with _connect() as conn:
conn.execute(
f"UPDATE scheduled_jobs SET last_status = ?, last_run_on = ?, last_duration_ms = ?, last_error = ?, next_run_on = ?, total_runs = total_runs + 1, {col} = {col} + 1, updated_at = ? WHERE id = ?",
(
last_status,
last_run_on,
last_duration_ms,
last_error,
next_run_on,
now,
job_id,
),
)
conn.commit()
return get_job(job_id)
def add_job_run(
job_id: str,
run_at: str,
status: str,
exit_code: int | None,
output: str,
error: str | None,
duration_ms: int,
) -> dict:
rid = short_id()
now = _now()
with _connect() as conn:
conn.execute(
"INSERT INTO job_runs (id, job_id, run_at, status, exit_code, output, error, duration_ms, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
(rid, job_id, run_at, status, exit_code, output, error, duration_ms, now),
)
conn.commit()
return {
"id": rid,
"job_id": job_id,
"run_at": run_at,
"status": status,
"exit_code": exit_code,
"output": output,
"error": error,
"duration_ms": duration_ms,
"created_at": now,
}
def list_job_runs(job_id: str, limit: int = 50) -> list[dict]:
with _connect() as conn:
conn.row_factory = sqlite3.Row