-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-shell-queue-db.el
More file actions
380 lines (339 loc) · 16.5 KB
/
Copy pathagent-shell-queue-db.el
File metadata and controls
380 lines (339 loc) · 16.5 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
;;; agent-shell-queue-db.el --- SQLite backend for agent-shell-queue -*- lexical-binding: t -*-
;; Author: tycho garen
;; Maintainer: tychoish
;; Keywords: tools, agent-shell
;; This file is not part of GNU Emacs
;;; Commentary:
;; Provides a SQLite persistence backend for agent-shell-queue.
;;
;; Activation:
;; (require 'agent-shell-queue-db)
;; (agent-shell-queue-db-enable)
;;
;; This replaces the default file-based save/load with direct SQLite I/O.
;; All other queue operations (dispatch, capture, reload, etc.) are unchanged.
;;
;; The schema stores one row per queue item with an explicit position column
;; to preserve intra-bucket ordering. All items, regardless of status, are
;; persisted — matching the plain-file backend semantics.
;;
;; Requires Emacs 29+ (built-in sqlite support).
;;; Code:
(require 'cl-lib)
(require 'agent-shell-queue)
(declare-function agent-shell-queue--ensure-loaded "agent-shell-queue")
(declare-function agent-shell-queue--refresh-buffer "agent-shell-queue")
(declare-function agent-shell-queue-export "agent-shell-queue")
(declare-function agent-shell-queue-import "agent-shell-queue")
(declare-function agent-shell-queue--scope-label "agent-shell-queue")
(declare-function agent-shell-queue--item-to-yaml "agent-shell-queue")
(declare-function agent-shell-queue--executor-name "agent-shell-queue")
(declare-function agent-shell-queue--executor-from-plist "agent-shell-queue")
;;; Variables
(defvar agent-shell-queue-db-file nil
"Path to the SQLite database file used by the queue DB backend.
When nil, the path is derived from `user-emacs-directory' as
\"agent-shell-queue.db\". Set before calling `agent-shell-queue-db-enable'
to store the database at a custom location.")
(defvar agent-shell-queue-db--connection nil
"Live sqlite connection object, or nil when not open.")
(defvar agent-shell-queue-db--saved-state-file-function nil
"Saved value of `agent-shell-queue-state-file-function' before DB enable.")
(defvar agent-shell-queue-db--saved-save-function nil
"Saved value of `agent-shell-queue-save-function' before DB enable.")
(defvar agent-shell-queue-db--saved-load-function nil
"Saved value of `agent-shell-queue-load-function' before DB enable.")
;;; Schema
(defconst agent-shell-queue-db--create-items
"CREATE TABLE IF NOT EXISTS items (
id TEXT PRIMARY KEY NOT NULL,
bucket TEXT NOT NULL,
prompt TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'active',
kind TEXT NOT NULL DEFAULT 'prompt',
background INTEGER NOT NULL DEFAULT 0,
created REAL,
dispatched REAL,
completed REAL,
response TEXT,
position INTEGER NOT NULL DEFAULT 0,
executor TEXT
)"
"DDL for the items table.")
(defconst agent-shell-queue-db--migrate-add-executor
"ALTER TABLE items ADD COLUMN executor TEXT"
"DDL to add the executor column to existing databases that predate it.")
(defconst agent-shell-queue-db--insert-item
"INSERT OR REPLACE INTO items
(id, bucket, prompt, status, kind, background,
created, dispatched, completed, response, position, executor)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
"Parameterized INSERT for a single queue item.")
(defconst agent-shell-queue-db--select-items
"SELECT id, bucket, prompt, status, kind, background,
created, dispatched, completed, response, executor
FROM items
ORDER BY bucket, position"
"SELECT to reconstruct the full items alist on load.")
;;; Connection management
(defun agent-shell-queue-db--default-file ()
"Return the default SQLite database file path."
(expand-file-name "agent-shell-queue.db" user-emacs-directory))
(defun agent-shell-queue-db--file ()
"Return the database file path to use."
(or agent-shell-queue-db-file (agent-shell-queue-db--default-file)))
(defun agent-shell-queue-db--ensure-connection ()
"Return the open sqlite connection, opening and initialising it if necessary."
(unless (fboundp 'sqlite-open)
(error "agent-shell-queue-db requires Emacs 29+ (built-in sqlite support)"))
(when (or (null agent-shell-queue-db--connection)
(not (sqlitep agent-shell-queue-db--connection)))
(let ((file (agent-shell-queue-db--file)))
(make-directory (file-name-directory file) t)
(setq agent-shell-queue-db--connection (sqlite-open file))
(sqlite-execute agent-shell-queue-db--connection
"PRAGMA journal_mode = WAL")
(sqlite-execute agent-shell-queue-db--connection
"PRAGMA busy_timeout = 5000")
(sqlite-execute agent-shell-queue-db--connection
agent-shell-queue-db--create-items)
;; Migrate existing databases that predate the executor column.
(condition-case nil
(sqlite-execute agent-shell-queue-db--connection
agent-shell-queue-db--migrate-add-executor)
(error nil))))
agent-shell-queue-db--connection)
(defun agent-shell-queue-db--close ()
"Close the SQLite connection if open."
(when (and agent-shell-queue-db--connection
(sqlitep agent-shell-queue-db--connection))
(sqlite-close agent-shell-queue-db--connection))
(setq agent-shell-queue-db--connection nil))
;;; Save
(defun agent-shell-queue-db--save ()
"Persist all queue items to SQLite.
Called as `agent-shell-queue-save-function' when the DB backend is active."
(let ((conn (agent-shell-queue-db--ensure-connection)))
(condition-case err
(progn
(sqlite-execute conn "BEGIN TRANSACTION")
(sqlite-execute conn "DELETE FROM items")
(seq-do (lambda (pair)
(let ((pos 0))
(seq-do (lambda (item)
(sqlite-execute
conn
agent-shell-queue-db--insert-item
(list (agent-shell-queue-item-id item)
(car pair)
(agent-shell-queue-item-args item)
(symbol-name (agent-shell-queue-item-status item))
(symbol-name (or (agent-shell-queue-item-kind item) 'prompt))
(if (agent-shell-queue-item-background item) 1 0)
(agent-shell-queue-item-created item)
(agent-shell-queue-item-dispatched item)
(agent-shell-queue-item-completed item)
(agent-shell-queue-item-response item)
pos
(agent-shell-queue--executor-name (agent-shell-queue-item-executor item))))
(setq pos (1+ pos)))
(cdr pair))))
agent-shell-queue--items)
(sqlite-execute conn "COMMIT"))
(error
(ignore-errors (sqlite-execute conn "ROLLBACK"))
(error "agent-shell-queue-db: save failed: %s" err))))
(setq agent-shell-queue--last-flush-time (float-time))
(when (bound-and-true-p agent-shell-queue-auto-flush-interval)
(setq agent-shell-queue--next-flush-time
(time-add (current-time)
(seconds-to-time agent-shell-queue-auto-flush-interval)))))
;;; Load
(defun agent-shell-queue-db--load ()
"Populate `agent-shell-queue--items' from the SQLite database.
Called as `agent-shell-queue-load-function' when the DB backend is active."
(let ((conn (agent-shell-queue-db--ensure-connection))
(result nil))
(seq-do (lambda (row)
(cl-destructuring-bind
(id bucket prompt status-str kind-str bg created dispatched completed response executor-name)
row
(let* ((status (condition-case nil (intern (or status-str "active")) (error 'active)))
(kind (condition-case nil (intern (or kind-str "prompt")) (error 'prompt)))
(item (agent-shell-queue-item--make
:id id
:args prompt
:status status
:kind kind
:background (eql bg 1)
:created created
:dispatched dispatched
:completed completed
:response response
:executor (agent-shell-queue--executor-from-plist executor-name)))
(pair (assoc bucket result)))
(if pair
(setcdr pair (append (cdr pair) (list item)))
(setq result (append result (list (list bucket item))))))))
(sqlite-select conn agent-shell-queue-db--select-items))
(setq agent-shell-queue--items result)))
;;; Enable / disable
;;;###autoload
(defun agent-shell-queue-db-enable (&optional db-file)
"Activate the SQLite persistence backend for agent-shell-queue.
When DB-FILE is non-nil, use it as the database path; otherwise the default
path under `user-emacs-directory' is used (see `agent-shell-queue-db-file').
Sets `agent-shell-queue-save-function', `agent-shell-queue-load-function',
and `agent-shell-queue-state-file-function' to SQLite-aware variants.
Saves the previous values so `agent-shell-queue-db-disable' can restore them."
(interactive (list (when current-prefix-arg
(read-file-name "SQLite database file: " user-emacs-directory
nil nil "agent-shell-queue.db"))))
(unless (fboundp 'sqlite-open)
(error "agent-shell-queue-db requires Emacs 29+ (built-in sqlite support)"))
(when db-file
(setq agent-shell-queue-db-file db-file))
(setq agent-shell-queue-db--saved-state-file-function
agent-shell-queue-state-file-function
agent-shell-queue-db--saved-save-function
agent-shell-queue-save-function
agent-shell-queue-db--saved-load-function
agent-shell-queue-load-function)
(setq agent-shell-queue-state-file-function #'agent-shell-queue-db--file
agent-shell-queue-save-function #'agent-shell-queue-db--save
agent-shell-queue-load-function #'agent-shell-queue-db--load)
(agent-shell-queue-db--ensure-connection)
(message "agent-shell-queue: SQLite backend enabled — %s"
(agent-shell-queue-db--file)))
;;;###autoload
(defun agent-shell-queue-db-disable ()
"Deactivate the SQLite backend and revert to the previous persistence settings.
Closes the database connection."
(interactive)
(setq agent-shell-queue-state-file-function
(or agent-shell-queue-db--saved-state-file-function
#'agent-shell-queue--default-state-file)
agent-shell-queue-save-function
agent-shell-queue-db--saved-save-function
agent-shell-queue-load-function
agent-shell-queue-db--saved-load-function)
(agent-shell-queue-db--close)
(message "agent-shell-queue: SQLite backend disabled"))
;;; Show state
;;;###autoload
(defun agent-shell-queue-db-show-state ()
"Display the SQLite database contents in a read-only popup buffer.
Shows per-bucket item counts and a tabular dump of all persisted rows."
(interactive)
(let* ((conn (agent-shell-queue-db--ensure-connection))
(counts (sqlite-select
conn
"SELECT bucket, status, count(*) FROM items GROUP BY bucket, status ORDER BY bucket, status"))
(rows (sqlite-select conn agent-shell-queue-db--select-items))
(buf (get-buffer-create "*agent-shell-queue-db*")))
(with-current-buffer buf
(let ((inhibit-read-only t))
(erase-buffer)
(insert (format "SQLite database: %s\n" (agent-shell-queue-db--file)))
(insert (format "Items stored: %d\n\n" (length rows)))
(when counts
(insert "Bucket summary:\n")
(seq-do (lambda (row)
(insert (format " %-40s %-10s %d\n" (nth 0 row) (nth 1 row) (nth 2 row))))
counts)
(insert "\n"))
(when rows
(let ((sep (make-string 100 ?─)))
(insert (format "%-8s %-30s %-10s %-8s %-6s %s\n"
"ID" "Bucket" "Status" "Kind" "BG" "Prompt"))
(insert sep "\n")
(seq-do (lambda (row)
(cl-destructuring-bind
(id bucket prompt status-str kind-str bg _created
_dispatched _completed _response _executor)
row
(insert (format "%-8s %-30s %-10s %-8s %-6s %s\n"
id
(truncate-string-to-width bucket 30 nil nil "…")
status-str kind-str
(if (eql bg 1) "yes" "no")
(truncate-string-to-width prompt 40 nil nil "…")))))
rows)))
(goto-char (point-min)))
(setq buffer-read-only t)
(setq-local revert-buffer-function
(lambda (_ignore-auto _noconfirm)
(let ((inhibit-read-only t))
(call-interactively #'agent-shell-queue-db-show-state))))
(read-only-mode 1))
(pop-to-buffer buf)))
;;; Export
;;;###autoload
(defun agent-shell-queue-db-export ()
"Export current queue items from SQLite to a YAML buffer.
Loads current state from the DB then delegates to `agent-shell-queue-export'."
(interactive)
(unless (fboundp 'yaml-encode)
(error "agent-shell-queue-db-export requires the `yaml' package"))
(agent-shell-queue--ensure-loaded)
(agent-shell-queue-export))
;;; Import
;;;###autoload
(defun agent-shell-queue-db-import (&optional source)
"Import queue items from YAML into the SQLite database.
SOURCE follows the same convention as `agent-shell-queue-import':
nil or `clipboard' reads from the clipboard; `file' prompts for a file.
After importing, the DB is flushed immediately."
(interactive (list (if current-prefix-arg 'file 'clipboard)))
(agent-shell-queue-import source)
(agent-shell-queue-db--save)
(message "agent-shell-queue-db: import complete and flushed to database"))
;;; Done-log hook (optional: record completed items in a separate DB table)
(defconst agent-shell-queue-db--create-done
"CREATE TABLE IF NOT EXISTS done_items (
id TEXT NOT NULL,
bucket TEXT NOT NULL,
prompt TEXT NOT NULL,
kind TEXT NOT NULL DEFAULT 'prompt',
background INTEGER NOT NULL DEFAULT 0,
created REAL,
dispatched REAL,
completed REAL,
archived REAL NOT NULL,
instance TEXT
)"
"DDL for the optional done_items history table.")
(defun agent-shell-queue-db-enable-done-log ()
"Create the done_items table and register a hook to record completed items.
This supplements (does not replace) `agent-shell-queue-done-log-file'.
Call after `agent-shell-queue-db-enable'."
(interactive)
(let ((conn (agent-shell-queue-db--ensure-connection)))
(sqlite-execute conn agent-shell-queue-db--create-done))
(add-hook 'agent-shell-queue-item-done-hook #'agent-shell-queue-db--record-done))
(defun agent-shell-queue-db--record-done (buf-name item)
"Insert BUF-NAME / ITEM into the done_items table."
(when (and agent-shell-queue-db--connection
(sqlitep agent-shell-queue-db--connection))
(condition-case err
(let* ((conn agent-shell-queue-db--connection)
(instance (let ((n (bound-and-true-p agent-shell-queue-instance-name)))
(if (functionp n) (funcall n) (or n "")))))
(sqlite-execute
conn
"INSERT INTO done_items
(id, bucket, prompt, kind, background, created, dispatched, completed, archived, instance)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
(list (agent-shell-queue-item-id item)
buf-name
(agent-shell-queue-item-args item)
(symbol-name (or (agent-shell-queue-item-kind item) 'prompt))
(if (agent-shell-queue-item-background item) 1 0)
(agent-shell-queue-item-created item)
(agent-shell-queue-item-dispatched item)
(agent-shell-queue-item-completed item)
(float-time)
instance)))
(error (message "agent-shell-queue-db: done-log write failed: %s" err)))))
(provide 'agent-shell-queue-db)
;;; agent-shell-queue-db.el ends here