-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-shell-queue.el
More file actions
6662 lines (6109 loc) · 322 KB
/
Copy pathagent-shell-queue.el
File metadata and controls
6662 lines (6109 loc) · 322 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
;;; agent-shell-queue.el --- Persistent prompt queue for agent-shell -*- lexical-binding: t -*-
;; Author: tycho garen
;; Maintainer: tychoish
;; Keywords: tools, agent-shell
;; Version: 0.1.0
;; URL: https://github.com/tychoish/agent-shell-queue
;; Package-Requires: ((emacs "29.1") (transient "0.4") (agent-shell "0.1") (alert "1.2") (annotated-completing-read "0.1"))
;; This file is not part of GNU Emacs
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Implements a persistent prompt queue for agent-shell sessions, supporting
;; multi-session dispatch with pause, resume, and archive lifecycle management.
;; Queue state is serialized to plist, JSON, or YAML for session persistence
;; across Emacs restarts. Interactive capture, edit, and item-view buffers
;; allow queue manipulation without leaving Emacs. Fork operations split a
;; queue across multiple sessions for parallel workloads.
;;; Code:
(require 'cl-lib)
(require 'sprite-future nil t)
(require 'agent-shell)
(require 'annotated-completing-read)
(require 'transient)
(require 'alert)
(require 'savehist)
(require 'agent-shell-queue-org)
(require 'agent-shell-queue-persistence)
(defface agent-shell-queue-blocked-face
'((t :foreground "darkorange3"))
"Face for queue items that are paused, deferred, or blocked.")
(defface agent-shell-queue-unassigned-face
'((t :foreground "cornflowerblue"))
"Face for queue items not yet assigned to any shell.")
(defface agent-shell-queue-detached-face
'((t :foreground "orange" :slant italic))
"Face for queue items whose target shell buffer no longer exists.")
(defface agent-shell-queue-compact-face
'((t :foreground "steelblue3"))
"Face for compact (non-LLM manual) work items.")
(defface agent-shell-queue-draft-face
'((t :foreground "gray50" :slant italic))
"Face for queue items saved as drafts (not yet queued for dispatch).")
(defconst agent-shell-queue--unassigned-key "(unassigned)"
"Alist bucket key for items not yet assigned to any shell.")
(defconst agent-shell-queue--dir-prefix "dir:"
"Prefix string identifying a directory-scoped queue bucket.")
(defun agent-shell-queue--canonicalize-dir (dir)
"Return expanded, canonicalized directory path for DIR."
(file-name-as-directory (expand-file-name (or dir default-directory))))
(defun agent-shell-queue--dir-bucket-p (bucket-name)
"Return non-nil if BUCKET-NAME is a directory queue bucket string."
(and (stringp bucket-name)
(string-prefix-p agent-shell-queue--dir-prefix bucket-name)))
(defun agent-shell-queue--dir-from-bucket (bucket-name)
"Extract directory path from BUCKET-NAME string.
Returns nil if BUCKET-NAME is not a directory bucket."
(when (agent-shell-queue--dir-bucket-p bucket-name)
(substring bucket-name (length agent-shell-queue--dir-prefix))))
(defun agent-shell-queue--bucket-for-dir (dir)
"Return the directory queue bucket string for DIR."
(concat agent-shell-queue--dir-prefix (agent-shell-queue--canonicalize-dir dir)))
(defun agent-shell-queue--pick-shell-for-directory (dir item-id)
"Create an `agent-shell' buffer for DIR associated with ITEM-ID.
Creates a new shell in DIR via `agent-shell-new-shell' and appends `-ITEM-ID'
to its buffer name."
(let* ((canon-dir (agent-shell-queue--canonicalize-dir dir))
(before-bufs (agent-shell-buffers))
(default-directory canon-dir)
(buf (agent-shell-new-shell)))
(unless (and (bufferp buf) (buffer-live-p buf))
(setq buf (seq-find (lambda (b) (not (memq b before-bufs))) (agent-shell-buffers))))
(when (and buf (buffer-live-p buf))
(with-current-buffer buf
(when item-id
(rename-buffer (concat (buffer-name buf) "-" item-id) t))))
buf))
(declare-function shell-maker-busy "shell-maker")
(declare-function markdown-mode "markdown-mode")
(declare-function yaml-encode "yaml")
(declare-function yaml-parse-string "yaml")
(declare-function yaml-mode "yaml-mode")
(declare-function json-pretty-print-buffer "json")
(declare-function agent-shell-menu--session-shell-buffer "agent-shell-menu")
;; Macros
(defmacro with-agent-shell-queue (&rest body)
"Evaluate BODY inside the queue load/save/refresh lifecycle.
Ensures the queue is loaded before BODY runs, then persists state and
refreshes the queue display after BODY completes. Returns the value of
BODY's last form. Does not protect against errors — if BODY signals,
the save and refresh are skipped."
(declare (indent defun))
`(progn
(agent-shell-queue--ensure-loaded)
(prog1
(progn ,@body)
(agent-shell-queue--save)
(agent-shell-queue--refresh-buffer))))
(defmacro agent-shell-queue--defstruct (type-name &rest field-specs)
"Define a `cl-defstruct' TYPE-NAME with FIELD-SPECS and serializers.
Supported options:
:no-serialize t — skip this field in to-plist and from-plist
:alias KEYWORD — also try KEYWORD when reading from plist
:to-plist FUNC — call (FUNC raw-value) when writing to a plist
:from-plist FUNC — call (FUNC plist-value) when reading from a plist
Generates constructor TYPE-NAME--make plus:
TYPE-NAME-to-plist — struct → keyword-keyed plist
TYPE-NAME-from-plist — keyword-keyed plist → struct"
(declare (indent 1))
(let* ((sname (symbol-name type-name))
(ctor (intern (concat sname "--make")))
(to-fn (intern (concat sname "-to-plist")))
(from-fn (intern (concat sname "-from-plist")))
(parsed (seq-map (lambda (spec)
(if (symbolp spec)
(list spec nil nil nil nil)
(let ((sym (car spec))
(opts (cdr spec)))
(list sym
(plist-get opts :no-serialize)
(plist-get opts :alias)
(plist-get opts :to-plist)
(plist-get opts :from-plist)))))
field-specs))
(field-names (seq-map #'car parsed))
(serializable (seq-remove (lambda (p) (pcase-let ((`(,_ ,no-ser . ,_) p)) no-ser)) parsed)))
`(progn
(cl-defstruct (,type-name (:constructor ,ctor) (:copier nil))
,@field-names)
(defun ,to-fn (item)
,(format "Convert %s ITEM to a keyword-keyed plist." sname)
(list ,@(cl-mapcan
(lambda (p)
(pcase-let ((`(,f ,_ ,_ ,to-plist-fn ,_) p))
(let* ((kw (intern (concat ":" (symbol-name f))))
(raw `(,(intern (concat sname "-" (symbol-name f))) item)))
(list kw (if to-plist-fn `(,to-plist-fn ,raw) raw)))))
serializable)))
(defun ,from-fn (plist)
,(format "Reconstruct a %s from keyword-keyed PLIST." sname)
(,ctor ,@(cl-mapcan
(lambda (p)
(pcase-let ((`(,f ,_ ,alias ,_ ,from-plist-fn) p))
(let* ((kw (intern (concat ":" (symbol-name f))))
(raw (if alias
`(or (plist-get plist ,kw) (plist-get plist ,alias))
`(plist-get plist ,kw))))
(list kw (if from-plist-fn `(,from-plist-fn ,raw) raw)))))
serializable))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Registries
(cl-defstruct (agent-shell-queue-executor
(:constructor agent-shell-queue-executor--make)
(:copier nil))
"Registry entry pairing a serializable NAME with an EXECUTOR function,
an optional CAPTURE function, and an optional CREATE function.
EXECUTOR: (item args) — called by `agent-shell-queue-send-item' to dispatch.
CAPTURE: () — called during item creation to produce the args value;
nil means fall back to the standard text-capture buffer.
CREATE: () — called by `agent-shell-queue-buffer-open-shell' when the
associated buffer is dead; should create and return a new
buffer of the same type, or nil to decline."
name executor capture create)
(defvar agent-shell-queue--executors nil
"List of `agent-shell-queue-executor' entries.
Only executors present here survive serialization. Register entries with
`agent-shell-queue-register-executor'.")
(defun agent-shell-queue-register-executor (name executor &optional capture create)
"Register EXECUTOR (and optional CAPTURE and CREATE) under NAME.
NAME may be a string or symbol; it is coerced to a string. Re-registering
an existing name replaces the entry. The name is written into serialized
queue state, so it must be stable across Emacs restarts.
CREATE, when provided, is a zero-arg function called by
`agent-shell-queue-buffer-open-shell' when the associated buffer is dead; it
should create and return a new buffer of the same type.
Returns EXECUTOR."
(let ((name-str (cond
((symbolp name) (symbol-name name))
((stringp name) name)
(t (user-error "Impossible type for %s" name)))))
(setq agent-shell-queue--executors
(cons (agent-shell-queue-executor--make
:name name-str
:executor executor
:capture capture
:create create)
(seq-remove (lambda (e)
(equal name-str (agent-shell-queue-executor-name e)))
agent-shell-queue--executors)))
executor))
(defun agent-shell-queue--find-executor (name)
"Return the `agent-shell-queue-executor' entry for NAME, or nil."
(seq-find (lambda (e) (equal name (agent-shell-queue-executor-name e)))
agent-shell-queue--executors))
(defun agent-shell-queue--executor-name (fn)
"Return the registry name for FN, or nil if not registered."
(when-let* ((e (seq-find (lambda (e) (eq fn (agent-shell-queue-executor-executor e)))
agent-shell-queue--executors)))
(agent-shell-queue-executor-name e)))
(defun agent-shell-queue--executor-from-plist (name)
"Deserialize executor NAME via the registry.
Returns nil (kind-dispatch) when NAME is nil or not found; emits a
warning for non-nil names that have no registry entry."
(when name
(if-let* ((e (agent-shell-queue--find-executor name)))
(agent-shell-queue-executor-executor e)
(progn
(message "agent-shell-queue: unknown executor %S — item will use kind dispatch" name)
nil))))
(cl-defstruct (agent-shell-queue-item-type
(:constructor agent-shell-queue-item-type--make)
(:copier nil))
"Registry entry describing a queue item kind with its capabilities.
KIND: symbol — the :kind field value for items of this type.
LABEL: string — display name (Kind column and menus).
BUFFER-PRED: (lambda (buf)) → bool | nil means any buffer including unassigned.
DISPATCH-FN: (lambda (item buf-name)) — executes the item when dispatched.
INPUT-SPEC: plist describing how to collect user input:
(:kind capture :mode MODE) open capture buffer in MODE (nil → capture-mode)
(:kind read :prompt P :fn F) single read via function F called with P
(:kind none) no user input; args will be empty
(:kind special :fn F) zero-arg interactive function F handles everything"
kind label buffer-pred dispatch-fn input-spec)
(defvar agent-shell-queue--item-types nil
"List of `agent-shell-queue-item-type' entries.
Register entries with `agent-shell-queue-register-item-type'.
Built-in registrations are added at the end of this file.")
(cl-defun agent-shell-queue-register-item-type (&key kind label buffer-pred dispatch-fn input-spec)
"Register item type with KIND, LABEL, BUFFER-PRED, DISPATCH-FN, and INPUT-SPEC.
KIND is a symbol; re-registering an existing KIND replaces the entry."
(setq agent-shell-queue--item-types
(cons (agent-shell-queue-item-type--make
:kind kind :label label :buffer-pred buffer-pred
:dispatch-fn dispatch-fn :input-spec input-spec)
(seq-remove (lambda (e) (eq kind (agent-shell-queue-item-type-kind e)))
agent-shell-queue--item-types))))
(defun agent-shell-queue--type-for-kind (kind)
"Return the `agent-shell-queue-item-type' for KIND symbol, or nil."
(seq-find (lambda (e) (eq kind (agent-shell-queue-item-type-kind e)))
agent-shell-queue--item-types))
(defun agent-shell-queue--types-for-buffer (buf)
"Return item types compatible with BUF.
nil BUF (unassigned) accepts all types."
(if (null buf)
agent-shell-queue--item-types
(seq-filter (lambda (type)
(let ((pred (agent-shell-queue-item-type-buffer-pred type)))
(or (null pred) (funcall pred buf))))
agent-shell-queue--item-types)))
(defun agent-shell-queue--validate-kind-for-buffer (kind buf-or-nil)
"Signal `user-error' when KIND is incompatible with BUF-OR-NIL.
nil BUF-OR-NIL (unassigned) always accepts any kind. Returns t on success."
(when-let* ((buf buf-or-nil)
(type (agent-shell-queue--type-for-kind kind))
(pred (agent-shell-queue-item-type-buffer-pred type)))
(unless (funcall pred buf)
(user-error "Item kind '%s' cannot be assigned to buffer '%s'"
(agent-shell-queue-item-type-label type)
(buffer-name buf))))
t)
(defun agent-shell-queue--kind-needs-session-p (kind)
"Return non-nil when KIND requires `agent-shell' session-mode compatibility."
(when-let* ((type (agent-shell-queue--type-for-kind kind)))
(eq (agent-shell-queue-item-type-buffer-pred type)
#'agent-shell-queue--agent-shell-buffer-p)))
;; Data model
(agent-shell-queue--defstruct agent-shell-queue-item
id
(args :alias :prompt)
status
kind
background
created
dispatched
completed
response
outcome
directory
(executor
:to-plist agent-shell-queue--executor-name
:from-plist agent-shell-queue--executor-from-plist)
;; Interjection fields — v1: one interjection per item.
;; interjection-prompt: text user typed; interjection-result: agent reply.
(interjection-prompt :no-serialize t)
(interjection-result :no-serialize t)
;; Re-enqueue tracking: reenqueued-from is the ID of the item this was
;; cloned from; reenqueued-as is a list of IDs created by re-enqueueing this.
reenqueued-from
reenqueued-as
(delay-before :alias :delay-before-dispatch)
(delay-after :alias :delay-after-complete))
(defun agent-shell-queue--item-well-formed-p (item)
"Return non-nil when ITEM has the minimum shape the queue UI requires.
Guards against a malformed persisted item (nil id/args/status) reaching
`agent-shell-queue-buffer-refresh', which errors on `split-string' with a
non-string ARGS."
(and (agent-shell-queue-item-id item)
(stringp (agent-shell-queue-item-args item))
(agent-shell-queue-item-status item)))
(defun agent-shell-queue--migrate-item-if-stale (item)
"Return ITEM or a current-layout copy with missing slots defaulted to nil.
Compares the vector length of ITEM against a freshly constructed default
instance; if shorter, copies the available slots into the new struct by index.
New trailing slots are left at nil. Handles future field additions without
modification. Returns nil (logging via `message') when ITEM cannot be
migrated to something matching `agent-shell-queue--item-well-formed-p' --
e.g. a persisted item so truncated that no fields overlap the current layout."
(let* ((current (agent-shell-queue-item--make))
(old-len (length item))
(new-len (length current))
(migrated (if (= old-len new-len)
item
(dotimes (i (1- (min old-len new-len)))
(aset current (1+ i) (aref item (1+ i))))
current)))
(if (agent-shell-queue--item-well-formed-p migrated)
migrated
(message "agent-shell-queue: dropping malformed item during migration: %S" migrated)
nil)))
(defun agent-shell-queue--migrate-all-stale-items ()
"Upgrade every in-memory item to the current struct layout.
Replaces old-format items (missing the outcome slot) in the live store with
freshly constructed equivalents, dropping any that come out malformed. Safe
to call repeatedly; up-to-date items are returned unchanged by
`agent-shell-queue--migrate-item-if-stale'."
(seq-do (lambda (bucket)
(setcdr bucket (seq-keep #'agent-shell-queue--migrate-item-if-stale (cdr bucket))))
(agent-shell-queue-store-items agent-shell-queue--store)))
(defun agent-shell-queue--migrate-deferred-statuses ()
"Convert any remaining `deferred' items to `blocked.skip' after load."
(seq-do (lambda (bucket)
(seq-do (lambda (item)
(when (eq (agent-shell-queue-item-status item) 'deferred)
(setf (agent-shell-queue-item-status item) 'blocked.skip)))
(cdr bucket)))
(agent-shell-queue-store-items agent-shell-queue--store)))
(defun agent-shell-queue--blocked-status-p (status)
"Return non-nil if STATUS is any blocked.* symbol."
(and status (string-prefix-p "blocked." (symbol-name status))))
(defun agent-shell-queue--blocked-p (item)
"Return non-nil if ITEM has any blocked.* status."
(agent-shell-queue--blocked-status-p (agent-shell-queue-item-status item)))
(cl-defstruct (agent-shell-queue-store
(:constructor agent-shell-queue--make-store)
(:copier nil))
"Queue state bundle: items, serialization format, and file path."
items ; (BUFFER-NAME . ITEM-LIST) alist
format ; symbol: plist | json | yaml
file) ; string: absolute path to state file
(defvar agent-shell-queue--items nil
"Items alist used by format-specific serializers (e.g. org).
Bound dynamically by `agent-shell-queue--serialize-items' methods
before calling the format's serialize helper.")
(defvar agent-shell-queue--store
(agent-shell-queue--make-store :items nil :format 'plist :file nil)
"Live queue store. Items are loaded from disk by --load, written by --save.")
(defun agent-shell-queue--sanitize-bucket (pair)
"Return PAIR with any malformed items removed, logging drops."
(let* ((before (cdr pair))
(after (seq-filter #'agent-shell-queue--item-well-formed-p before))
(dropped (- (length before) (length after))))
(when (> dropped 0)
(message "agent-shell-queue: dropped %d malformed item%s from bucket %s"
dropped (if (= dropped 1) "" "s") (car pair)))
(cons (car pair) after)))
(defun agent-shell-queue--restore-store-items (items)
"Set the live store's items to ITEMS, dropping malformed ones.
Called by the persistence layer after deserializing from disk. Defined here
so that the setf on the store struct slot stays in the same file as the struct."
(setf (agent-shell-queue-store-items agent-shell-queue--store)
(seq-map #'agent-shell-queue--sanitize-bucket items)))
(defun agent-shell-queue--normalize-running-item (item)
"Reset ITEM's status from `running' to `active' for cross-session reload.
Defined here so setf on item struct slots stays in the same file as the struct."
(setf (agent-shell-queue-item-status item) 'active)
(setf (agent-shell-queue-item-dispatched item) nil))
(cl-defstruct (agent-shell-queue-queue
(:constructor agent-shell-queue-queue--make)
(:copier nil))
"Queue runtime state and reference to the active store."
(store 'agent-shell-queue--store) ; symbol naming the live store variable
(session-paused nil) ; list of buffer names paused from dispatch
(editing-ids nil) ; list of item IDs currently open in an edit buffer
(interjection-pending nil) ; boolean: blocks dispatch while interjection is in progress
(halted-sessions nil)) ; list of buffer/bucket names halted on task abort/interrupt
(defvar agent-shell-queue--queue
(agent-shell-queue-queue--make)
"The active queue object. Persisted via `savehist-additional-variables'.")
(defun agent-shell-queue--halted-on-abort-p (name)
"Return non-nil when bucket or buffer NAME is halted due to task abort/interrupt."
(and agent-shell-queue--queue
name
(member (if (stringp name) name (symbol-name name))
(agent-shell-queue-queue-halted-sessions agent-shell-queue--queue))))
(defun agent-shell-queue--mark-halted-on-abort (name)
"Mark buffer or bucket NAME as halted on abort."
(when name
(let ((sname (if (stringp name) name (symbol-name name))))
(cl-pushnew sname
(agent-shell-queue-queue-halted-sessions agent-shell-queue--queue)
:test #'equal))))
(defun agent-shell-queue--clear-halted-on-abort (name)
"Clear halted on abort status for buffer or bucket NAME."
(when name
(let ((sname (if (stringp name) name (symbol-name name))))
(setf (agent-shell-queue-queue-halted-sessions agent-shell-queue--queue)
(delete sname
(agent-shell-queue-queue-halted-sessions agent-shell-queue--queue))))))
(defun agent-shell-queue--response-has-question-p (response-text)
"Return non-nil if RESPONSE-TEXT ends with an open question prompt."
(when response-text
(let ((trimmed (string-trim response-text)))
(or (string-match-p "\\?\\s-*\\'" trimmed)
(string-match-p "\\[y/N\\]\\s-*\\'" trimmed)
(string-match-p "\\(Would you like\\|Do you want\\|Should I\\).*\\?\\s-*\\'" trimmed)))))
(defun agent-shell-queue--verify-recovery (buf item response-text)
"Verify whether ITEM turn on BUF satisfies the 3 recovery criteria:
1. Uninterrupted (status is done, outcome is not aborted/interrupted).
2. No question (response-text does not end with open question).
3. Not in plan mode (buf mode-id not in blocked session modes).
Returns non-nil when all three criteria are satisfied."
(and item
(eq (agent-shell-queue-item-status item) 'done)
(not (memq (agent-shell-queue-item-outcome item) '(aborted interrupted)))
(not (agent-shell-queue--response-has-question-p response-text))
(not (agent-shell-queue--session-mode-blocked-p buf))))
(defun agent-shell-queue-session-paused-p ()
"Return non-nil when the current buffer's session queue dispatch is paused."
(and agent-shell-queue--queue
(member (buffer-name (current-buffer))
(agent-shell-queue-queue-session-paused agent-shell-queue--queue))))
(defun agent-shell-queue--session-pause-name (name)
"Add NAME to the session-paused list and mark its active items blocked.
Shared by `agent-shell-queue-session-pause' (single buffer),
`agent-shell-queue-pause' (batch, every known buffer), and
`agent-shell-queue--on-interrupt'."
(agent-shell-queue--cancel-pause-timer name)
(cl-pushnew name (agent-shell-queue-queue-session-paused agent-shell-queue--queue) :test #'equal)
(seq-do (lambda (item)
(when (eq (agent-shell-queue-item-status item) 'active)
(setf (agent-shell-queue-item-status item) 'blocked.runner)))
(cdr (assoc name (agent-shell-queue-store-items agent-shell-queue--store)))))
(defun agent-shell-queue--session-unpause-name (name)
"Remove NAME from paused and halted lists, marking items active.
Shared by `agent-shell-queue-unpause-all-sessions' and
`agent-shell-queue-session-resume'."
(setf (agent-shell-queue-queue-session-paused agent-shell-queue--queue)
(seq-remove (lambda (n) (equal n name))
(agent-shell-queue-queue-session-paused agent-shell-queue--queue)))
(agent-shell-queue--clear-halted-on-abort name)
(seq-do (lambda (item)
(when (eq (agent-shell-queue-item-status item) 'blocked.runner)
(setf (agent-shell-queue-item-status item) 'active)))
(cdr (assoc name (agent-shell-queue-store-items agent-shell-queue--store)))))
;;; Prompt Composition and Enqueueing
(defvar agent-shell-queue-capture-mode-map
(let ((m (make-sparse-keymap)))
(define-key m (kbd "C-c C-c") #'agent-shell-queue-capture-confirm)
(define-key m (kbd "C-c C-k") #'agent-shell-queue-capture-cancel)
(define-key m (kbd "C-c C-s") #'agent-shell-queue-capture-save-draft)
(define-key m (kbd "C-c C-b") #'agent-shell-queue-capture-enable-background-task)
(define-key m (kbd "C-c M-b") #'agent-shell-queue-capture-disable-background-task)
(define-key m (kbd "C-c C-y") #'agent-shell-queue-capture-yank-kill)
(define-key m (kbd "C-c M-w") #'agent-shell-queue-capture-yank-clipboard)
(define-key m (kbd "C-c C-p") #'agent-shell-queue-capture-insert-thing-at-point)
(define-key m (kbd "C-c C-x") #'agent-shell-queue-capture-select-context)
(define-key m (kbd "C-c C-f") #'agent-shell-queue-insert-file)
(define-key m (kbd "C-c M-f") #'agent-shell-queue-insert-buffer)
m)
"Keymap for `agent-shell-queue-capture-mode'.")
(define-derived-mode agent-shell-queue-capture-mode markdown-mode "Queue-Capture"
"Mode for composing a queued `agent-shell' prompt.
\\{agent-shell-queue-capture-mode-map}"
(setq-local electric-indent-inhibit t))
(defvar-local agent-shell-queue--capture-target nil
"Target `agent-shell' buffer for this capture session.")
(defvar-local agent-shell-queue--capture-origin nil
"Buffer from which capture was launched, used for context insertion.")
(defvar-local agent-shell-queue--capture-background-task nil
"When non-nil, the captured prompt will be flagged for background execution.")
(defvar-local agent-shell-queue--capture-after-id nil
"When non-nil, the confirmed item will be inserted after this item ID.")
(defvar-local agent-shell-queue--capture-draft-id nil
"ID of the draft item saved from this capture buffer, or nil.
Set by `agent-shell-queue-capture-save-draft' and used to update rather
than duplicate the draft on subsequent saves.")
(defvar-local agent-shell-queue--capture-delay-before nil
"Pre-dispatch delay in seconds for confirmed capture item.")
(defvar-local agent-shell-queue--capture-delay-after nil
"Post-completion delay in seconds for confirmed capture item.")
(defvar-local agent-shell-queue--capture-kind 'prompt
"Kind of queue item to create when this capture buffer is confirmed.
Defaults to `prompt'; set to `emacs-lisp' for Emacs Lisp capture buffers.")
(defun agent-shell-queue--open-capture (target-buf &optional origin-buf initial-content kind mode
delay-before delay-after)
"Open a capture buffer targeting TARGET-BUF (nil for unassigned queue).
Multiple capture buffers can be open simultaneously; each is named after
its target. ORIGIN-BUF is used for context commands; defaults to current
buffer. INITIAL-CONTENT is inserted before display if non-nil.
KIND sets `agent-shell-queue--capture-kind' (defaults to `prompt').
MODE, when non-nil, is a major-mode function used instead of
`agent-shell-queue-capture-mode'; \\[agent-shell-queue-capture-confirm] and \\[agent-shell-queue-capture-cancel] are bound in mode map.
Optional DELAY-BEFORE and DELAY-AFTER specify per-task delays in seconds."
(let* ((bucket-name (if target-buf
(buffer-name target-buf)
agent-shell-queue--unassigned-key))
(kind-label (when (and kind (not (eq kind 'prompt)))
(concat " | " (symbol-name kind))))
(capture-buf (get-buffer-create
(if target-buf
(format "*agent-shell-queue-capture: %s*" (buffer-name target-buf))
"*agent-shell-queue-capture: unassigned*"))))
(with-current-buffer capture-buf
(erase-buffer)
(if mode
(progn
(funcall mode)
(use-local-map (copy-keymap (current-local-map)))
(local-set-key (kbd "C-c C-c") #'agent-shell-queue-capture-confirm)
(local-set-key (kbd "C-c C-k") #'agent-shell-queue-capture-cancel))
(agent-shell-queue-capture-mode))
(setq agent-shell-queue--capture-target target-buf
agent-shell-queue--capture-origin (or origin-buf (current-buffer))
agent-shell-queue--capture-background-task nil
agent-shell-queue--capture-after-id nil
agent-shell-queue--capture-kind (or kind 'prompt)
agent-shell-queue--capture-delay-before delay-before
agent-shell-queue--capture-delay-after delay-after)
(when (and initial-content (not (string-empty-p initial-content)))
(insert initial-content))
(let* ((bucket-items (cdr (assoc bucket-name (agent-shell-queue-store-items agent-shell-queue--store))))
(depth (agent-shell-queue--active-item-count bucket-items))
(state (agent-shell-queue--activity-state)))
(setq-local header-line-format
(concat
(propertize (format " %s%s | " bucket-name (or kind-label "")) 'face 'shadow)
state
(propertize (format " | depth: %d" depth) 'face 'shadow)))))
(pop-to-buffer capture-buf '(display-buffer-below-selected))
capture-buf))
(defun agent-shell-queue-capture-confirm ()
"Confirm capture: queue the buffer contents and close."
(interactive)
(let ((prompt (string-trim (buffer-string)))
(buf agent-shell-queue--capture-target)
(bg agent-shell-queue--capture-background-task)
(after-id agent-shell-queue--capture-after-id)
(kind agent-shell-queue--capture-kind)
(delay-before agent-shell-queue--capture-delay-before)
(delay-after agent-shell-queue--capture-delay-after))
(let ((use-blocked (agent-shell-queue--capture-plan-mode-choice buf)))
(agent-shell-queue--close-capture-window)
(unless (string-empty-p prompt)
(message "agent-shell: %s" prompt)
(cond
(after-id
(when-let* ((pair (agent-shell-queue--item-by-id after-id))
(bucket-name (car pair))
(item (agent-shell-queue--make-item prompt bg kind delay-before delay-after)))
(when use-blocked
(setf (agent-shell-queue-item-status item) 'blocked.skip))
(let ((items (cdr (assoc bucket-name (agent-shell-queue-store-items agent-shell-queue--store)))))
(if-let* ((idx (cl-position after-id items
:key #'agent-shell-queue-item-id :test #'equal))
(cell (assoc bucket-name (agent-shell-queue-store-items agent-shell-queue--store))))
(setcdr cell (append (cl-subseq items 0 (1+ idx))
(list item)
(cl-subseq items (1+ idx))))
(agent-shell-queue--add-item-to-bucket bucket-name item)))
(when buf (agent-shell-queue--ensure-subscription buf))
(agent-shell-queue--save)
(agent-shell-queue--refresh-buffer)))
(buf
(with-agent-shell-queue
(let ((item (agent-shell-queue--make-item prompt bg kind delay-before delay-after)))
(when use-blocked
(setf (agent-shell-queue-item-status item) 'blocked.skip))
(setf (agent-shell-queue-item-directory item)
(buffer-local-value 'default-directory buf))
(agent-shell-queue--add-item-to-bucket (buffer-name buf) item)
(agent-shell-queue--ensure-subscription buf)))
(unless use-blocked
(agent-shell-queue--send-next-for-buffer buf)))
(t
(with-agent-shell-queue
(let ((item (agent-shell-queue--make-item prompt bg kind delay-before delay-after)))
(when use-blocked
(setf (agent-shell-queue-item-status item) 'blocked.skip))
(agent-shell-queue--add-item-to-bucket agent-shell-queue--unassigned-key item)))))))))
(defun agent-shell-queue-capture-cancel ()
"Discard the capture buffer without queuing."
(interactive)
(agent-shell-queue--close-capture-window))
(defun agent-shell-queue-capture-save-draft ()
"Save capture buffer contents as a draft queue item without closing.
The item is stored with `draft' status and skipped by dispatch.
If a draft was previously saved from this buffer it is updated in place."
(interactive)
(let ((prompt (string-trim (buffer-string)))
(buf agent-shell-queue--capture-target)
(bg agent-shell-queue--capture-background-task))
(when (string-empty-p prompt)
(user-error "Buffer is empty — nothing to save as draft"))
(if-let* ((draft-id agent-shell-queue--capture-draft-id)
(_ (agent-shell-queue--item-by-id draft-id)))
(progn
(agent-shell-queue-edit draft-id prompt)
(message "agent-shell-queue: draft updated (%s)" draft-id))
(let* ((item (agent-shell-queue--make-item prompt bg))
(bucket-name (if buf (buffer-name buf) agent-shell-queue--unassigned-key)))
(setf (agent-shell-queue-item-status item) 'draft)
(agent-shell-queue--ensure-loaded)
(agent-shell-queue--add-item-to-bucket bucket-name item)
(when buf (agent-shell-queue--ensure-subscription buf))
(agent-shell-queue--save)
(agent-shell-queue--refresh-buffer)
(setq agent-shell-queue--capture-draft-id (agent-shell-queue-item-id item))
(message "agent-shell-queue: draft saved (%s)" agent-shell-queue--capture-draft-id)))))
(defun agent-shell-queue-capture-enable-background-task ()
"Flag this capture for background sub-agent execution."
(interactive)
(setq agent-shell-queue--capture-background-task t)
(message "Background: on"))
(defun agent-shell-queue-capture-disable-background-task ()
"Clear the background sub-agent flag from this capture."
(interactive)
(setq agent-shell-queue--capture-background-task nil)
(message "Background: off"))
(defun agent-shell-queue-capture-yank-kill ()
"Insert the most recent `kill-ring' entry at point."
(interactive)
(when kill-ring (insert (car kill-ring))))
(defun agent-shell-queue-capture-yank-clipboard ()
"Insert the current clipboard contents at point."
(interactive)
(when-let* ((sel (ignore-errors (gui-get-selection 'CLIPBOARD))))
(insert sel)))
(defun agent-shell-queue-capture-insert-thing-at-point ()
"Insert the thing at point from the buffer that opened this capture."
(interactive)
(when-let* ((origin agent-shell-queue--capture-origin)
(_ (buffer-live-p origin))
(thing (with-current-buffer origin
(or (thing-at-point 'url t)
(thing-at-point 'filename t)
(thing-at-point 'symbol t)
(thing-at-point 'word t)))))
(insert thing)))
(defun agent-shell-queue-capture-select-context ()
"Select a string from origin buffer context via ACR and insert it."
(interactive)
(when-let* ((origin agent-shell-queue--capture-origin)
(_ (buffer-live-p origin))
(text (with-current-buffer origin
(annotated-completing-read-context-from-point
:prompt "insert context: "
:history 'agent-shell-queue-capture-select-context)))
(_ (not (string-empty-p text))))
(insert text)))
(defun agent-shell-queue-insert-file ()
"Prompt for a file and insert its contents at point.
Works in both capture and edit buffers."
(interactive)
(let ((file (read-file-name "Insert file: ")))
(when (file-readable-p file)
(insert-file-contents file))))
(defun agent-shell-queue-insert-buffer ()
"Pick a buffer and insert its entire contents at point.
Works in both capture and edit buffers."
(interactive)
(when-let* ((name (annotated-completing-read
(map-into
(seq-map (lambda (buf)
(cons (buffer-name buf)
(with-current-buffer buf
(format "%-20s %s"
(symbol-name major-mode)
(or (buffer-file-name) "")))))
(seq-remove (lambda (b) (string-prefix-p " " (buffer-name b)))
(buffer-list)))
'(hash-table :test equal))
:prompt "Insert buffer: "
:require-match t))
(buf (get-buffer name)))
(insert (with-current-buffer buf (buffer-string)))))
;;;###autoload
(defun agent-shell-queue-capture (&optional buf)
"Open a capture buffer targeting BUF (nil add to the unassigned queue).
When called interactively from an `agent-shell' buffer, targets that buffer.
With a prefix argument, opens an unassigned capture instead."
(interactive
(list (cond
(current-prefix-arg nil)
((derived-mode-p 'agent-shell-mode) (current-buffer))
(t (agent-shell-queue--pick-buffer "Capture for: ")))))
(agent-shell-queue--open-capture buf (current-buffer)))
;;;###autoload
(defun agent-shell-queue-enqueue (prompt &optional buf background delay-before delay-after)
"Queue PROMPT for BUF, optionally flagged for BACKGROUND execution.
Send immediately if BUF is idle and no DELAY-BEFORE is set, otherwise
store in the queue. When called interactively, opens a capture buffer
for composing the prompt. Optional DELAY-BEFORE and DELAY-AFTER specify
pre-dispatch and post-completion delays in seconds."
(interactive
(let ((target (or (and (derived-mode-p 'agent-shell-mode) (current-buffer))
(agent-shell-queue--pick-buffer "Enqueue to: "))))
(agent-shell-queue--open-capture target (current-buffer))
(list nil nil nil nil nil)))
(when-let* ((buf (and prompt
(or buf
(and (derived-mode-p 'agent-shell-mode) (current-buffer))
(agent-shell-queue--pick-buffer "Enqueue to: ")))))
(with-current-buffer buf
(if (or (shell-maker-busy)
(and delay-before (numberp delay-before) (> delay-before 0)))
(agent-shell-queue-add prompt buf background delay-before delay-after)
(agent-shell-insert
:text (if background
(concat (agent-shell-queue--get-background-prefix buf) prompt)
prompt)
:submit t
:no-focus t)))))
;;;###autoload
(defun agent-shell-queue-enqueue-clear (&optional buf)
"Enqueue a clear command for BUF.
Uses the resolved clear command for BUF as the prompt."
(interactive
(list (or (and (derived-mode-p 'agent-shell-mode) (current-buffer))
(agent-shell-queue--pick-buffer "Clear queue for: "))))
(agent-shell-queue-enqueue (agent-shell-queue--get-clear-command buf) buf))
;;;###autoload
(defun agent-shell-queue-capture-unassigned ()
"Open a capture buffer to compose a prompt for the unassigned queue.
Unassigned items display in blue and can later be assigned to a shell via key t."
(interactive)
(agent-shell-queue--open-capture nil (current-buffer)))
(defun agent-shell-queue-buffer-capture-after ()
"Open a capture buffer for an item to be inserted after the item at point.
The confirmed item is spliced into the queue immediately after the current row,
rather than appended to the end."
(interactive)
(when-let* ((id (tabulated-list-get-id))
(pair (agent-shell-queue--item-by-id id)))
(let ((bucket-name (car pair)))
(with-current-buffer
(agent-shell-queue--open-capture
(unless (equal bucket-name agent-shell-queue--unassigned-key)
(get-buffer bucket-name))
(current-buffer))
(setq agent-shell-queue--capture-after-id id)))))
;;;###autoload
(defun agent-shell-queue-capture-from-region (&optional buf)
"Open a capture buffer pre-seeded with the active region text.
When no region is active, opens an empty capture. BUF is the target
`agent-shell' buffer; nil adds to the unassigned queue."
(interactive
(list (cond
(current-prefix-arg nil)
((derived-mode-p 'agent-shell-mode) (current-buffer))
(t (agent-shell-queue--pick-buffer "Capture for: ")))))
(agent-shell-queue--open-capture buf (current-buffer)
(when (use-region-p)
(buffer-substring-no-properties
(region-beginning) (region-end)))))
;;;###autoload
(defun agent-shell-queue-capture-from-context (&optional buf)
"Open a capture buffer pre-seeded with a string selected from context.
Candidates include `thing-at-point', active region, current line, and
kill ring. BUF is the target buffer; nil for unassigned queue."
(interactive
(list (cond
(current-prefix-arg nil)
((derived-mode-p 'agent-shell-mode) (current-buffer))
(t (agent-shell-queue--pick-buffer "Capture for: ")))))
(let ((text (annotated-completing-read-context-from-point
:prompt "seed capture: "
:history 'agent-shell-queue-capture-from-context)))
(agent-shell-queue--open-capture
buf (current-buffer)
(unless (string-empty-p text)
text))))
(defun agent-shell-queue-capture-from-clipboard (&optional buf)
"Open a capture buffer pre-seeded with the current clipboard contents.
BUF is the target `agent-shell' buffer; nil adds to the unassigned queue."
(interactive
(list (cond
(current-prefix-arg nil)
((derived-mode-p 'agent-shell-mode) (current-buffer))
(t (agent-shell-queue--pick-buffer "Capture for: ")))))
(agent-shell-queue--open-capture
buf (current-buffer)
(ignore-errors
(gui-get-selection 'CLIPBOARD))))
;;;###autoload
(defun agent-shell-queue-add-directory (prompt dir &optional background delay-before delay-after)
"Add a new active item for PROMPT in directory queue DIR.
Optional BACKGROUND, DELAY-BEFORE, and DELAY-AFTER configure task settings."
(interactive
(list (read-string "Prompt: ")
(read-directory-name "Directory queue: ")
current-prefix-arg))
(with-agent-shell-queue
(let* ((canon-dir (agent-shell-queue--canonicalize-dir dir))
(bucket (agent-shell-queue--bucket-for-dir canon-dir))
(item (agent-shell-queue--make-item prompt background 'prompt delay-before delay-after)))
(setf (agent-shell-queue-item-directory item) canon-dir)
(agent-shell-queue--add-item-to-bucket bucket item)
item)))
;;;###autoload
(defun agent-shell-queue-enqueue-directory (dir)
"Open a capture buffer for directory queue DIR."
(interactive (list (read-directory-name "Directory queue: ")))
(let ((default-directory (agent-shell-queue--canonicalize-dir dir)))
(agent-shell-queue-enqueue nil)))
;;;###autoload
(defun agent-shell-queue-enqueue-emacs (buf)
"Open an Emacs Lisp capture buffer to compose a form for BUF's queue.
The capture buffer is in `emacs-lisp-mode'. Confirm with \\[agent-shell-queue-capture-confirm],
cancel with \\[agent-shell-queue-capture-cancel]. When dispatched, the form is evaluated via
`eval'; errors are reported as messages and the item is marked done.
BUF may be nil to enqueue to the unassigned bucket."
(interactive (list (agent-shell-queue--pick-buffer-for-kind 'emacs-lisp "Target (or unassigned): ")))
(if buf
(agent-shell-queue--open-elisp-capture buf)
(agent-shell-queue--open-capture nil nil nil 'emacs-lisp 'emacs-lisp-mode)))
;;;###autoload
(defun agent-shell-queue-enqueue-emacs-command (command buf)
"Enqueue an interactive COMMAND to run in Emacs for BUF's queue.
COMMAND is selected via `read-command' (completing-read over all commands).
When dispatched, the command is invoked with `call-interactively'.
BUF may be nil to enqueue to the unassigned bucket."
(interactive
(list (read-command "Emacs command: ")
(agent-shell-queue--pick-buffer-for-kind 'emacs-command "Target (or unassigned): ")))
(agent-shell-queue--enqueue-args (symbol-name command) 'emacs-command buf))
;;;###autoload
(defun agent-shell-queue-enqueue-shell-eshell (buf)
"Open a shell capture buffer to compose a command for eshell BUF.
The capture buffer is in `sh-mode'. Confirm with \\[agent-shell-queue-capture-confirm].
BUF may be nil to enqueue to the unassigned bucket."
(interactive (list (agent-shell-queue--pick-buffer-for-kind 'shell-eshell "eshell buffer (or unassigned): ")))
(agent-shell-queue--open-capture buf nil nil 'shell-eshell 'sh-mode))
;;;###autoload
(defun agent-shell-queue-enqueue-shell-eat (buf)
"Open a shell capture buffer to compose a command for eat BUF.
The capture buffer is in `sh-mode'. Confirm with \\[agent-shell-queue-capture-confirm].
BUF may be nil to enqueue to the unassigned bucket."
(interactive (list (agent-shell-queue--pick-buffer-for-kind 'shell-eat "eat buffer (or unassigned): ")))
(agent-shell-queue--open-capture buf nil nil 'shell-eat 'sh-mode))
;; Capture buffers
(defun agent-shell-queue--open-elisp-capture (target-buf)
"Open an Emacs Lisp capture buffer targeting TARGET-BUF's queue.
The buffer is in `emacs-lisp-mode' with \\[agent-shell-queue-capture-confirm] and \\[agent-shell-queue-capture-cancel] bindings.
Items created from this buffer have kind `emacs-lisp'."
(let* ((capture-buf (get-buffer-create
(format "*agent-shell-queue-elisp: %s*"
(buffer-name target-buf))))
(bucket-name (buffer-name target-buf)))
(with-current-buffer capture-buf
(erase-buffer)
(emacs-lisp-mode)
(use-local-map (copy-keymap emacs-lisp-mode-map))
(local-set-key (kbd "C-c C-c") #'agent-shell-queue-capture-confirm)
(local-set-key (kbd "C-c C-k") #'agent-shell-queue-capture-cancel)
(setq agent-shell-queue--capture-target target-buf
agent-shell-queue--capture-origin (current-buffer)
agent-shell-queue--capture-background-task nil
agent-shell-queue--capture-after-id nil
agent-shell-queue--capture-kind 'emacs-lisp)
(let* ((bucket-items (cdr (assoc bucket-name (agent-shell-queue-store-items agent-shell-queue--store))))
(depth (agent-shell-queue--active-item-count bucket-items))
(state (agent-shell-queue--activity-state)))
(setq-local header-line-format
(concat
(propertize (format " %s | emacs-lisp | " bucket-name) 'face 'shadow)
state
(propertize (format " | depth: %d" depth) 'face 'shadow)))))
(pop-to-buffer capture-buf '(display-buffer-below-selected))
capture-buf))
(defun agent-shell-queue--close-capture-window ()
"Delete the current capture window and kill its buffer.
Explicitly deletes the window first so the split disappears regardless of
how the window was opened (i.e. independent of `quit-restore' state)."
(let ((win (selected-window))
(buf (current-buffer)))
(if (and (not (one-window-p)) (window-deletable-p win))
(progn (delete-window win) (kill-buffer buf))
(kill-buffer buf))))