Repository navigation
Expand file tree
/
Copy pathboard.py
More file actions
954 lines (838 loc) · 42 KB
/
Copy pathboard.py
File metadata and controls
954 lines (838 loc) · 42 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
#!/usr/bin/env python3
"""scripts/board.py — the PyAutoGut Dashboard.
A clear overview of **everything in the Gut** — every condemned ref held
under ``refs/heads/archive/condemned/<name>`` on this repo's remote, read
against the Mind's ``condemned.md`` ledger — and a button on every voidable
row that removes it permanently.
**Truth is two sources, never one.** ``collect()`` lists the refs with
``git ls-remote`` and parses the ledger with the Brain's parser
(``agents/conductors/hygiene/_hygiene_condemned.py``, imported from a
checked-out PyAutoBrain like the theme is), then reconciles them. Drift in
either direction is shown, not hidden:
* **due** — ledger entry past its ``sweep-after`` date, ref present;
* **in transit** — ref present, ``sweep-after`` still ahead (days left);
* **held** — ref present, entry undated (``never``): kept until a human asks
in a session; never one-tap voidable, never in ``void: all-due``;
* **orphan refs** — a ref no ledger entry claims;
* **dangling entries** — an entry whose ``archive-ref`` is missing from the
remote (split out as **voided — entry still in condemned.md** when a recent
``void:`` issue names it: the void workflow cannot edit the Mind, so the
ledger row is retired by a session);
* **held on another repo** — an entry whose ``archive-ref`` says the ref
lives on a sibling repo's origin (``… on <Repo> origin``): read with a
best-effort ``ls-remote`` of that repo, flagged when due, and voidable with
the same button when dated and present — the void workflow deletes it on
the sibling with the org's ``pyautolabs-bot`` App token; undated sibling refs are
held like any other;
* **history-only** — ``archive-ref: n/a`` (bytes live in remote history):
listed for completeness, never voidable;
* **recently voided** — the last closed ``void:`` issues (best-effort).
**The button is a prefilled GitHub issue.** "Void permanently" opens
``issues/new?title=void:+<name>&labels=void&body=…``; pressing Submit is the
human's ``--yes``. ``.github/workflows/void.yml`` deletes the ref — with this
repo's own token when the ref is in the Gut, with a ``pyautolabs-bot`` App
token when it
is held on a sibling repo — comments the repo and pre-delete SHA, closes the
issue and re-renders this board. ``void: all-due`` voids every *due* row at
once, in the Gut and on sibling repos alike.
**Shape** (mirrors ``PyAutoHands/autohands/board.py``): ``collect()`` is the
only I/O and degrades per-section into ``errors[]``; ``render(snapshot,
fmt)`` is pure — ``md | md-brief | html | json | badge | state`` plus the
void workflow's ``due-names`` / ``void-plan``.
Owner and repo come from ``git remote`` — no instance names live here (the
tenant firewall).
Usage:
python scripts/board.py [--mind P] [--brain P] [--remote origin] [--repo P]
[--today YYYY-MM-DD]
(--collect OUT | --snapshot F)
(--md|--md-brief|--html|--json|--badge|--state|
--due-names|--void-plan)
"""
from __future__ import annotations
import datetime
import fnmatch
import html as _html
import importlib.util
import json
import os
import re
import subprocess
import sys
from pathlib import Path
from urllib.parse import quote_plus
GUT_HOME = Path(__file__).resolve().parents[1]
CHECKIN_PROMPT = (
"Use this chat as an ongoing place to review retained material, recover previous work and "
"manage cleanup through PyAutoGut. Read PyAutoGut/AGENTS.md and its current board. "
"Reconcile archive references with PyAutoMind/condemned.md, checking held entries, "
"retention dates, recovery evidence and unavailable sources.\n\n"
"When I give no particular direction, summarize what is retained, what is on hold, what "
"is eligible for a sweep and which discrepancies need attention. Explain why items are "
"retained and identify decisions needed from me. Eligibility for deletion is not "
"authorization to delete.\n\n"
"When I name a branch, stash, archived item or question, make that the main focus. Help "
"me understand what it contains, why it was retained, whether it includes unique work and "
"how it could be recovered. Bring in related records where useful; do not repeat the full "
"queue review on every follow-up.\n\n"
"Help me compare recovery, continued retention, a hold or eventual disposal. Inspect "
"relevant history and references before recommending an action. Keep missing evidence "
"explicit and verify recoverability rather than assuming an archive is sufficient.\n\n"
"When I request recovery, use the established procedure and preserve existing work. When "
"I request cleanup, identify the exact items and consequences before acting. Require "
"explicit human authorization for permanent voiding, reference deletion or destructive "
"cleanup; a general check-in grants none.\n\n"
"Keep retention records consistent with verified actions through the owning repository "
"procedures. Route implementation changes through the development workflow, and retain "
"decisions and approvals already given in this conversation.\n\n"
"After taking action, report what was recovered, retained, held or removed, how you "
"verified the outcome and what remains unresolved."
)
BOARD_KEY = "gut" # this board's entry in the Brain's palette table
NS = "refs/heads/archive/condemned/"
SCHEMA_VERSION = 1
PARSER_REL = Path("agents") / "conductors" / "hygiene" / "_hygiene_condemned.py"
LEDGER = "condemned.md"
RECENT_VOIDED = 10
BUCKETS = ("due", "transit", "held", "orphans", "dangling", "voided_pending",
"elsewhere", "history")
# The rows the one-tap button may void: refs a human has not asked to hold —
# in THIS repo (due / transit / orphans) or held on a sibling repo with a dated
# entry (elsewhere; see `foreign_voidable`). Held (undated) rows, here or on a
# sibling, are voided only in a session.
VOIDABLE = ("due", "transit", "orphans", "elsewhere")
# A void name is appended to NS and nothing else, so it cannot leave the
# namespace — but it must also be a sane ref tail: no traversal, no leading
# dash (an option to git), no globs, never `main`.
_NAME_RE = re.compile(r"^[A-Za-z0-9._][A-Za-z0-9._/-]*$")
def valid_name(name: str) -> bool:
"""True for a ref tail the void workflow may act on (its bash twin is
``valid_name`` in ``.github/workflows/void.yml``)."""
return bool(name) and name != "main" and name != "all-due" \
and ".." not in name and "//" not in name \
and not name.endswith("/") and not name.startswith("refs/") \
and bool(_NAME_RE.match(name))
# --- locating the Brain and the Mind ------------------------------------------
def _brain_home(brain: str | None = None) -> Path | None:
for cand in (brain, os.environ.get("PYAUTO_BRAIN"), GUT_HOME / "PyAutoBrain",
GUT_HOME.parent / "PyAutoBrain"):
if cand and ((Path(cand) / "board").is_dir()
or (Path(cand) / PARSER_REL).is_file()):
return Path(cand)
return None
def _mind_home(mind: str | None = None) -> Path | None:
for cand in (mind, os.environ.get("PYAUTO_MIND"), GUT_HOME / "PyAutoMind",
GUT_HOME.parent / "PyAutoMind"):
if cand and (Path(cand) / LEDGER).is_file():
return Path(cand)
return None
def load_parser(brain: str | Path | None = None):
"""The Brain's condemned.md parser (``parse_manifest``, ``classify``,
``ref_name``), loaded by path so the ledger is read one way everywhere."""
home = _brain_home(str(brain) if brain else None)
path = home / PARSER_REL if home else None
if not path or not path.is_file():
raise RuntimeError(
"the condemned.md parser (PyAutoBrain/" + PARSER_REL.as_posix() +
") is not in reach — pass --brain or set PYAUTO_BRAIN")
spec = importlib.util.spec_from_file_location(
f"_gut_condemned_parser_{abs(hash(str(path)))}", path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
for fn in ("parse_manifest", "classify", "ref_name"):
if not hasattr(mod, fn):
raise RuntimeError(f"{path} has no {fn}() — the Brain checkout "
"predates PyAutoGut#9")
return mod
_THEME_BRAIN: list = [None]
def theme():
"""The shared theme module (PyAutoBrain/board/_theme.py). Only the html
path needs it; every other surface renders with no Brain in reach."""
home = _brain_home(_THEME_BRAIN[0])
board = home / "board" if home else None
if board and (board / "_theme.py").is_file():
if str(board) not in sys.path:
sys.path.insert(0, str(board))
import _theme
return _theme
raise RuntimeError(
"the shared board theme (PyAutoBrain/board/_theme.py) is not in reach "
"— check PyAutoBrain out beside this repo or pass --brain")
# --- identity (derived, never hardcoded — tenant firewall) -------------------
def parse_owner_repo(url: str) -> tuple[str, str]:
m = re.search(r"github\.com[:/]+([^/]+)/([^/]+?)(?:\.git)?/?$", url.strip())
return (m.group(1), m.group(2)) if m else ("", "")
def _owner_repo(repo_path: Path, remote: str) -> tuple[str, str]:
out = subprocess.run(["git", "-C", str(repo_path), "remote", "get-url",
remote], capture_output=True, text=True).stdout
return parse_owner_repo(out)
# --- pure reconciliation --------------------------------------------------------
def parse_ls_remote(text: str) -> dict[str, str]:
"""``git ls-remote`` output → ``{name: sha}`` for refs under NS only."""
refs = {}
for line in (text or "").splitlines():
parts = line.split()
if len(parts) == 2 and parts[1].startswith(NS):
refs[parts[1][len(NS):]] = parts[0]
return refs
_HOST_RE = re.compile(r"\bon\s+`?([A-Za-z0-9_.-]+)`?\s+origin\b")
def ref_host(entry: dict) -> str | None:
"""The repo an entry's ``archive-ref`` says the ref lives on
(``… on <Repo> origin``), or None when it does not say."""
m = _HOST_RE.search(entry.get("archive-ref") or "")
return m.group(1) if m else None
def _date(s):
try:
return datetime.date.fromisoformat(str(s or "").strip())
except ValueError:
return None
def _clip(text: object, limit: int = 160) -> str:
text = " ".join(str(text or "").split())
return text if len(text) <= limit else text[: limit - 1] + "…"
def repo_url(snap: dict) -> str:
o, r = snap.get("owner"), snap.get("repo")
return f"https://github.com/{o}/{r}" if o and r else ""
def pages_url(snap: dict) -> str:
o, r = str(snap.get("owner") or "").lower(), snap.get("repo") or ""
return f"https://{o}.github.io/{r}/" if o and r else ""
def row_repo(snap: dict, row: dict) -> str:
"""The repo a row's ref lives on: its sibling host, else the Gut itself."""
return row.get("host") or snap.get("repo") or ""
def foreign_voidable(row: dict) -> bool:
"""A sibling-held row the button may void: present on the sibling (a SHA
was read this render) and dated — an undated sibling entry is held."""
return bool(row.get("sha")) and _date(row.get("sweep_after")) is not None
def void_issue_url(snap: dict, row: dict) -> str:
"""The one-tap button: a prefilled issue whose submission voids the ref."""
base = repo_url(snap)
if not base or not row.get("name") or not row.get("sha"):
return ""
where = row_repo(snap, row)
held_on = (f" on {where} (held on a sibling repo — deleted there with "
"the pyautolabs-bot App token)" if row.get("host") else
f" on {where}")
body = (f"Void `{NS}{row['name']}` permanently{held_on}.\n\n"
f"- repo: {where}\n"
f"- entry: {row.get('entry') or '(no ledger entry — orphan ref)'}\n"
f"- reason: {_clip(row.get('reason') or '-', 300)}\n"
f"- sweep-after: {row.get('sweep_after') or '-'}\n"
f"- pre-delete sha: {row['sha']}\n\n"
"Submitting this issue voids the ref permanently. It is processed "
"by the Gut Void workflow (void.yml); recovery is no longer "
"possible afterwards.")
return (f"{base}/issues/new?title={quote_plus('void: ' + row['name'])}"
f"&labels=void&body={quote_plus(body)}")
def due_rows(snap: dict) -> list[dict]:
"""The `void: all-due` set: due Gut rows, then overdue sibling-held rows
(present and dated). Never held, orphan, dangling or history rows."""
b = snap.get("buckets") or {}
return list(b.get("due") or []) + [
r for r in b.get("elsewhere") or []
if r.get("overdue") and foreign_voidable(r)]
def void_all_due_url(snap: dict) -> str:
base = repo_url(snap)
due = due_rows(snap)
if not base or not due:
return ""
names = [r["name"] + (f" (on {r['host']})" if r.get("host") else "")
for r in due]
shown = names[:40]
listing = "\n".join(f"- {n}" for n in shown)
if len(names) > len(shown):
listing += f"\n- … and {len(names) - len(shown)} more"
body = (f"Void every Gut ref whose ledger entry is past its sweep-after "
f"date — {len(names)} ref(s) as of {snap.get('today')}:\n\n"
f"{listing}\n\nThe workflow recomputes the due set from "
"condemned.md when it runs; undated (held) entries and refs with "
"no ledger entry are never included. Refs held on sibling repos "
"are deleted there with the pyautolabs-bot App token.\n\n"
"Submitting this issue "
"voids the refs permanently. Recovery is no longer possible "
"afterwards.")
return (f"{base}/issues/new?title={quote_plus('void: all-due')}"
f"&labels=void&body={quote_plus(body)}")
def _session_void(row: dict, why: str) -> str:
return (f"Void the Gut ref `{row['name']}` ({why}): in PyAutoGut run "
f"`bin/pyauto-gut void {row['name']} --yes`, then retire its "
"entry from PyAutoMind/condemned.md in a Mind commit.")
def _foreign_void(row: dict) -> str:
return (f"Void the condemned ref `{NS}{row['name']}` held on "
f"{row['host']} (ledger entry `{row['entry']}`, sweep-after "
f"{row.get('sweep_after') or '-'}): from a {row['host']} checkout "
f"run `git push origin --delete {NS}{row['name']}` after checking "
f"`git ls-remote origin {NS}{row['name']}` still reads "
f"{(row.get('sha') or '?')[:12]}, then retire the entry from "
"PyAutoMind/condemned.md.")
def _retire_prompt(row: dict, how: str) -> str:
return (f"Retire the `{row['entry']}` entry from PyAutoMind/condemned.md "
f"(and its contents line): its Gut ref "
f"`{NS}{row.get('name') or '?'}` is {how}. The bytes are gone, so "
"the ledger row is only bookkeeping now.")
def reconcile(entries, refs, parser, today, repo, voided_names=(),
foreign=None):
"""Entries × refs → the board's buckets (lists of row dicts). Pure.
``foreign`` maps a sibling repo name to its ``{name: sha}`` archive refs
(or None when that repo could not be listed) for entries whose ref lives
on another repo's origin.
"""
buckets = {b: [] for b in BUCKETS}
claimed: set[str] = set()
voided_names = set(voided_names)
foreign = foreign or {}
for e in entries:
base = {"entry": e.get("name", "?"), "type": e.get("type") or "-",
"reason": e.get("reason") or "", "condemned":
e.get("condemned") or "", "sweep_after":
(e.get("sweep-after") or "").strip(), "archive_ref":
_clip(e.get("archive-ref") or "", 240)}
name = parser.ref_name(e)
if not name:
buckets["history"].append({**base, "name": None, "sha": "",
"why": "archive-ref n/a — bytes live in "
"remote history"})
continue
names = (sorted(n for n in refs if fnmatch.fnmatchcase(n, name))
if "*" in name else ([name] if name in refs else []))
host = ref_host(e)
if not names:
if host and repo and host != repo:
listed = foreign.get(host)
sha = (listed or {}).get(name, "")
if listed is not None and not sha:
buckets["dangling"].append({**base, "name": name, "sha": "",
"host": host})
continue
row = {**base, "name": name, "sha": sha, "host": host,
"why": f"held on {host}, not in the Gut" +
("" if listed is not None else
" (could not be listed this render)")}
d = _date(base["sweep_after"])
if d is not None and d <= today and sha:
row["days"] = (today - d).days
row["overdue"] = True
buckets["elsewhere"].append(row)
elif name in voided_names:
buckets["voided_pending"].append({**base, "name": name,
"sha": ""})
else:
buckets["dangling"].append({**base, "name": name, "sha": ""})
continue
d = _date(base["sweep_after"])
for n in names:
claimed.add(n)
row = {**base, "name": n, "sha": refs[n]}
if d is None:
buckets["held"].append(row)
elif d <= today:
buckets["due"].append({**row, "days": (today - d).days})
else:
buckets["transit"].append({**row, "days": (d - today).days})
for n in sorted(set(refs) - claimed):
buckets["orphans"].append({"entry": "", "name": n, "sha": refs[n],
"type": "-", "reason": "",
"condemned": "", "sweep_after": ""})
buckets["due"].sort(key=lambda r: (-r["days"], r["name"]))
buckets["transit"].sort(key=lambda r: (r["days"], r["name"]))
buckets["elsewhere"].sort(key=lambda r: (-r.get("days", -1), r["name"]))
return buckets
def build_snapshot(ledger_text, ls_remote_text, parser, today, owner, repo,
voided=(), errors=(), refs_listed=True, generated=None,
foreign=None):
"""The whole snapshot from already-fetched inputs. Pure (tests call it)."""
refs = parse_ls_remote(ls_remote_text) if refs_listed else {}
entries = parser.parse_manifest(ledger_text or "")
voided = list(voided)
names = {n for v in voided for n in v.get("names") or []}
snap = {
"schema_version": SCHEMA_VERSION,
"generated": generated or datetime.datetime.now(
datetime.timezone.utc).isoformat(),
"today": today.isoformat(),
"owner": owner, "repo": repo,
"refs_listed": bool(refs_listed),
"ref_count": len(refs),
"entry_count": len(entries),
"buckets": reconcile(entries, refs, parser, today, repo, names,
foreign)
if refs_listed else {b: [] for b in BUCKETS},
"voided": voided[:RECENT_VOIDED],
"errors": list(errors),
}
for key, rows in snap["buckets"].items():
for row in rows:
ok = key in VOIDABLE and bool(row.get("sha")) and (
key != "elsewhere" or foreign_voidable(row))
row["void_url"] = void_issue_url(snap, row) if ok else ""
snap["void_all_due_url"] = void_all_due_url(snap)
return snap
# --- I/O ------------------------------------------------------------------------
def _recent_voided(owner: str, repo: str) -> list[dict]:
out = subprocess.run(
["gh", "api", f"repos/{owner}/{repo}/issues?state=closed&per_page=100"
"&sort=updated&direction=desc"],
capture_output=True, text=True, timeout=60)
if out.returncode != 0:
raise RuntimeError(_clip(out.stderr or "gh api failed", 200))
rows = []
for it in json.loads(out.stdout or "[]"):
title = str(it.get("title") or "")
if "pull_request" in it or not title.lower().startswith("void:"):
continue
if it.get("state_reason") not in (None, "completed"):
continue
target = title[5:].strip()
rows.append({"number": it.get("number"), "title": title,
"url": it.get("html_url") or "",
"closed_at": it.get("closed_at") or "",
"names": [] if target == "all-due" else [target]})
rows.sort(key=lambda r: r["closed_at"], reverse=True)
return rows[:RECENT_VOIDED]
def collect(mind=None, brain=None, remote="origin", repo_path=None,
today=None) -> dict:
"""Gather the snapshot. Degrades per-section into errors[]; never raises
for a source that cannot be read (a missing parser is the one exception:
without it the ledger cannot be read at all)."""
repo_path = Path(repo_path or GUT_HOME)
today = today or datetime.datetime.now(datetime.timezone.utc).date()
errors: list[str] = []
parser = load_parser(brain)
owner, repo = _owner_repo(repo_path, remote)
if not owner:
errors.append(f"could not derive owner/repo from git remote '{remote}'")
ls = subprocess.run(["git", "-C", str(repo_path), "ls-remote", "--heads",
remote, "archive/condemned/*"],
capture_output=True, text=True, timeout=120)
refs_listed = ls.returncode == 0
if not refs_listed:
errors.append("git ls-remote: " + _clip(ls.stderr or "failed", 200))
home = _mind_home(mind)
text = ""
if home is None:
errors.append(f"{LEDGER} not found — pass --mind")
else:
text = (home / LEDGER).read_text(encoding="utf-8")
foreign = {}
if owner:
hosts = {ref_host(e) for e in parser.parse_manifest(text)
if parser.ref_name(e)} - {None, repo}
for host in sorted(hosts):
out = subprocess.run(
["git", "ls-remote", "--heads",
f"https://github.com/{owner}/{host}.git",
"archive/condemned/*"],
capture_output=True, text=True, timeout=120)
if out.returncode == 0:
foreign[host] = parse_ls_remote(out.stdout)
else:
foreign[host] = None
errors.append(f"git ls-remote {host}: "
+ _clip(out.stderr or "failed", 160))
voided = []
if owner:
try:
voided = _recent_voided(owner, repo)
except (OSError, RuntimeError, ValueError,
subprocess.SubprocessError) as e:
errors.append(f"recently voided: {e}")
return build_snapshot(text, ls.stdout, parser, today, owner, repo, voided,
errors, refs_listed, foreign=foreign)
# --- counts / status ------------------------------------------------------------
def counts(snap: dict) -> dict:
b = snap.get("buckets") or {}
c = {k: len(b.get(k) or []) for k in BUCKETS}
c["due_elsewhere"] = sum(1 for r in b.get("elsewhere") or []
if r.get("overdue"))
return c
def status(snap: dict) -> str:
"""grey (nothing listed) · yellow (anything due, orphaned or dangling) ·
green (transit is clean). Never red: nothing in the Gut can break."""
if not snap.get("refs_listed"):
return "grey"
c = counts(snap)
if c["due"] or c["due_elsewhere"] or c["orphans"] or c["dangling"] \
or c["voided_pending"]:
return "yellow"
return "green"
def _summary(snap: dict) -> str:
c = counts(snap)
bits = [f"{c['due']} due", f"{c['transit']} in transit",
f"{c['held']} held"]
if c["due_elsewhere"]:
bits.append(f"{c['due_elsewhere']} due on other repos")
if c["orphans"]:
bits.append(f"{c['orphans']} orphan ref(s)")
if c["dangling"] + c["voided_pending"]:
bits.append(f"{c['dangling'] + c['voided_pending']} dangling entr"
f"{'y' if c['dangling'] + c['voided_pending'] == 1 else 'ies'}")
return " · ".join(bits)
# --- markdown -------------------------------------------------------------------
_TITLES = {
"due": "Due for voiding",
"transit": "In transit",
"held": "Held (undated — void only on explicit request)",
"orphans": "Orphan refs (no ledger entry)",
"dangling": "Dangling entries (ref missing)",
"voided_pending": "Voided — entry still in condemned.md",
"elsewhere": "Held on another repo",
"history": "History-only (not voidable here)",
}
def _render_md(snap: dict) -> str:
out = [f"# PyAutoGut Dashboard", "",
f"**{status(snap).upper()}** — {_summary(snap)} "
f"({snap.get('ref_count', 0)} refs, {snap.get('entry_count', 0)} "
f"ledger entries, as of {snap.get('today')})", ""]
if snap.get("void_all_due_url"):
out += [f"[Void all due →]({snap['void_all_due_url']})", ""]
for key in BUCKETS:
rows = (snap.get("buckets") or {}).get(key) or []
if not rows:
continue
out += [f"## {_TITLES[key]} ({len(rows)})", "",
"| ref | entry | sweep-after | sha | action |",
"|---|---|---|---|---|"]
for r in rows:
act = f"[void]({r['void_url']})" if r.get("void_url") else "-"
days = (f" ({r['days']}d "
f"{'over' if key in ('due', 'elsewhere') else 'left'})"
if "days" in r else "")
out.append(f"| `{r.get('name') or '-'}` | {r.get('entry') or '-'} "
f"| {_clip(r.get('sweep_after') or '-', 40)}{days} "
f"| {(r.get('sha') or '-')[:10]} | {act} |")
out.append("")
if snap.get("voided"):
out += ["## Recently voided", ""]
out += [f"- [#{v['number']}]({v['url']}) {v['title']} "
f"({str(v.get('closed_at'))[:10]})" for v in snap["voided"]]
out.append("")
if snap.get("errors"):
out += ["## Unavailable this render", ""]
out += [f"- {e}" for e in snap["errors"]]
out.append("")
return "\n".join(out)
def _render_md_brief(snap: dict) -> str:
bits = [f"\U0001f5d1️ **{status(snap).upper()}** — {_summary(snap)}"]
url = pages_url(snap)
if url:
bits.append(f"[dashboard →]({url})")
return " · ".join(bits)
# --- html -----------------------------------------------------------------------
_LEDE = ("Everything the Gut holds, read against the Mind's "
"<code>condemned.md</code>. <b>Void permanently</b> opens a prefilled "
"issue — submitting it is the yes. Tap \U0001f4cb to copy a command "
"for an AI assistant session instead.")
_EXTRA_CSS = """
.acts{display:flex;flex-wrap:wrap;gap:.35rem;margin-top:.4rem}
a.void{display:inline-block;padding:.34rem .7rem;border-radius:9px;
font-size:.85rem;font-weight:650;color:var(--bad);border:1px solid var(--bad);
background:transparent;white-space:nowrap}
a.void:hover{background:var(--bad);color:#fff;text-decoration:none}
a.void.all{margin:.3rem 0 .6rem}
.flag{color:var(--warn);font-weight:600;font-size:.85em}
.sha{font-size:.8em}
.errors{margin-top:1.2rem}
footer{margin-top:2rem;color:var(--muted);font-size:.82em}
"""
def _esc(v) -> str:
return _html.escape(str(v), quote=True)
def _chip(payload: str, label: str) -> str:
return (f"<button class='copy text' type='button' title='{_esc(label)}' "
f"data-cmd=\"{_esc(payload)}\">\U0001f4cb {_esc(label)}</button>")
def _row_html(key: str, r: dict) -> str:
name = r.get("name") or "-"
head = f"<b>{_esc(name)}</b>"
if r.get("entry") and r.get("entry") != name:
head += f" <span class='muted'>· {_esc(r['entry'])}</span>"
facts = []
if r.get("type") and r["type"] != "-":
facts.append(_esc(r["type"]))
if r.get("condemned"):
facts.append(f"condemned {_esc(r['condemned'])}")
if r.get("sweep_after"):
facts.append(f"sweep-after {_esc(_clip(r['sweep_after'], 60))}")
if "days" in r:
facts.append(f"<b class='warn'>{r['days']}d over</b>"
if key in ("due", "elsewhere") else f"{r['days']}d left")
if r.get("sha"):
facts.append(f"<code class='sha'>{_esc(r['sha'][:12])}</code>")
lines = [head]
if key == "orphans":
lines.append("<span class='flag'>no ledger entry</span>")
if r.get("why"):
lines.append(f"<span class='muted'>{_esc(r['why'])}</span>")
if r.get("reason"):
lines.append(f"<span class='muted'>{_esc(_clip(r['reason'], 180))}</span>")
if facts:
lines.append(f"<span class='facets'>{' · '.join(facts)}</span>")
acts = []
if r.get("void_url"):
acts.append(f"<a class='void' href=\"{_esc(r['void_url'])}\">"
"Void permanently</a>")
if key == "elsewhere" and r.get("host"):
lines.insert(1, f"<span class='flag'>on {_esc(r['host'])}</span>")
if key == "elsewhere" and r.get("sha"):
acts.append(_chip(_foreign_void(r), "void via session"))
elif r.get("sha"):
acts.append(_chip(f"bin/pyauto-gut recover {name}", "recover"))
why = {"due": "sweep-after passed", "orphans": "no ledger entry",
"held": "held — explicit human request"}.get(key, "in transit")
acts.append(_chip(_session_void(r, why), "void via session"))
if key == "dangling":
acts.append(_chip(_retire_prompt(r, "missing from the Gut remote"),
"retire entry"))
if key == "voided_pending":
acts.append(_chip(_retire_prompt(r, "voided (a closed void: issue)"),
"retire entry"))
body = "<br>".join(lines)
act = f"<span class='acts'>{''.join(acts)}</span>" if acts else ""
return f"<div class='task'><p>{body}{act}</p></div>"
_FOLD_AFTER = 8
def _section_html(key: str, rows: list, snap: dict) -> str:
if not rows:
return ""
head = (f"<h2 id='bucket-{key}'>{_esc(_TITLES[key])} <span class='muted'>({len(rows)})"
"</span></h2>")
extra = ""
# "Void all due" covers the Gut and sibling repos alike: it sits on the
# due section, or on the sibling section when nothing in the Gut is due.
b = snap.get("buckets") or {}
host = "due" if b.get("due") else "elsewhere"
if key == host and snap.get("void_all_due_url"):
extra = (f"<a class='void all' href=\"{_esc(snap['void_all_due_url'])}\">"
f"Void all due ({len(due_rows(snap))})</a>")
items = [_row_html(key, r) for r in rows]
if len(items) > _FOLD_AFTER + 2:
items = items[:_FOLD_AFTER] + [
f"<details><summary>{len(items) - _FOLD_AFTER} more</summary>"
+ "".join(items[_FOLD_AFTER:]) + "</details>"]
return head + extra + "".join(items)
def _boards_nav(snap: dict) -> str:
owner = str(snap.get("owner") or "").lower()
if not owner:
return ""
t_ = theme()
links = getattr(t_, "board_links", None)
return t_.boards_footer(links(f"https://{owner}.github.io", BOARD_KEY),
BOARD_KEY) if links else ""
def _render_html(snap: dict) -> str:
t_ = theme()
work_links = []
if repo_url(snap):
work_links.append({"label": "Gut repository", "href": repo_url(snap)})
# The board's publishing workflow checks out this owner's Mind ledger.
work_links.append({"label": "Mind retention ledger",
"href": f"https://github.com/{snap['owner']}/PyAutoMind/blob/main/condemned.md"})
panel = t_.orchestration_panel("gut", "", "", CHECKIN_PROMPT,
organ="gut", work_links=work_links,
refreshed_at=(snap.get("generated")
if snap.get("refs_listed") and not snap.get("errors") else None),
refresh_url=(repo_url(snap) + "/actions/workflows/gut_board.yml")
if repo_url(snap) else None)
st = status(snap)
tone = {"green": "ok", "yellow": "warn", "grey": "muted"}[st]
navigation = [{"href": "#inventory", "label": "Inventory"}]
navigation.extend(
{"href": "#bucket-" + key, "label": _TITLES[key], "count": len(rows)}
for key in BUCKETS
if (rows := (snap.get("buckets") or {}).get(key))
)
if snap.get("voided"):
navigation.append({"href": "#voided", "label": "Recently voided", "count": len(snap["voided"])})
sections = "".join(_section_html(k, (snap.get("buckets") or {}).get(k) or [],
snap) for k in BUCKETS)
if not snap.get("refs_listed"):
sections = ("<p class='muted'>The Gut's refs could not be listed this "
"render — nothing is shown rather than a guess.</p>" +
sections)
elif not sections:
sections = "<p class='muted'>The Gut is empty.</p>"
voided = ""
if snap.get("voided"):
voided = "<h2 id='voided'>Recently voided</h2>" + "".join(
f"<div class='task'><p><a href=\"{_esc(v['url'])}\">#{v['number']}"
f"</a> {_esc(v['title'])} <span class='muted'>"
f"{_esc(str(v.get('closed_at'))[:10])}</span></p></div>"
for v in snap["voided"])
errors = ""
if snap.get("errors"):
errors = ("<div class='errors'><p class='muted'>unavailable this "
"render:</p><ul class='muted'>" + "".join(
f"<li>{_esc(e)}</li>" for e in snap["errors"]) +
"</ul></div>")
gh = repo_url(snap)
gh_link = f' · <a href="{gh}">GitHub</a>' if gh else ""
return t_.section_layout(f"""<!doctype html>
<html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PyAutoGut Dashboard</title>
<style>{t_.css(BOARD_KEY)}{_EXTRA_CSS}</style>
</head>
<body>
{t_.hero(BOARD_KEY, "Dashboard", _LEDE, navigation=navigation)}
{panel}
<p class="verdict {tone}"><b class="{tone}">{st.upper()}</b>
<span class="muted">{_esc(_summary(snap))}</span></p>
<p class="muted">{snap.get('ref_count', 0)} refs on the remote ·
{snap.get('entry_count', 0)} ledger entries · as of {_esc(snap.get('today'))} ·
<a href="dashboard.md">markdown version</a>{gh_link}</p>
<section id="inventory">{sections}</section>
{voided}
{errors}
{_boards_nav(snap)}
<footer>Rendered by <code>scripts/board.py</code> from <code>git ls-remote</code>
+ the Mind's <code>condemned.md</code> · generated
{_esc(snap.get('generated') or '?')}.</footer>
<script>{t_.JS}</script>
</body></html>
""")
# --- badge / state --------------------------------------------------------------
def badge_endpoint(snap: dict) -> dict:
if not snap.get("refs_listed"):
return {"schemaVersion": 1, "label": "gut", "message": "unknown",
"color": "lightgrey"}
c = counts(snap)
return {"schemaVersion": 1, "label": "gut",
"message": f"{c['transit']} in transit · {c['due']} due",
"color": "yellow" if status(snap) == "yellow" else "green"}
def _iso_z(ts) -> str:
try:
t = datetime.datetime.fromisoformat(str(ts).replace("Z", "+00:00"))
if t.tzinfo is None:
t = t.replace(tzinfo=datetime.timezone.utc)
except (TypeError, ValueError):
t = datetime.datetime.now(datetime.timezone.utc)
return t.astimezone(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
STATE_ITEMS_MAX = 20
def to_state(snap: dict) -> dict:
"""The organ-cockpit feed (contract v1, owned by PyAutoBrain
``board/_state.py``): due rows first (yellow, url = the row's one-tap
void issue), then orphans / dangling / voided-pending (info), then
collection errors (info); capped at 20."""
st = status(snap)
b = snap.get("buckets") or {}
items = []
for r in b.get("due") or []:
items.append({"severity": "yellow",
"text": _clip(f"due: {r['name']} — sweep-after "
f"{r['sweep_after']} ({r['days']}d over)"),
"url": r.get("void_url") or None,
"prompt": _session_void(r, "sweep-after passed")})
for r in b.get("elsewhere") or []:
if r.get("overdue"):
items.append({"severity": "yellow",
"text": _clip(f"due on {r['host']}: {r['name']} — "
f"sweep-after {r['sweep_after']} "
f"({r['days']}d over)"),
"url": r.get("void_url") or None,
"prompt": _foreign_void(r)})
for r in b.get("orphans") or []:
items.append({"severity": "info",
"text": _clip(f"orphan ref: {r['name']} — no ledger entry"),
"url": r.get("void_url") or None,
"prompt": _session_void(r, "no ledger entry")})
for key, how in (("dangling", "missing from the Gut remote"),
("voided_pending", "voided (a closed void: issue)")):
for r in b.get(key) or []:
items.append({"severity": "info",
"text": _clip(f"{'dangling' if key == 'dangling' else 'voided'}"
f" entry: {r['entry']} — ref "
f"{r.get('name')} {how}"),
"url": None, "prompt": _retire_prompt(r, how)})
items += [{"severity": "info", "text": _clip(f"unavailable this render: {e}"),
"url": None, "prompt": None} for e in snap.get("errors") or []]
if st == "grey":
headline = "GREY — the Gut's refs could not be listed"
elif st == "green":
headline = _summary(snap)
else:
headline = f"YELLOW — {_summary(snap)}"
return {
"schema_version": 1,
"organ": BOARD_KEY,
"repo": snap.get("repo") or "PyAutoGut",
"status": st,
"headline": _clip(headline),
"updated": _iso_z(snap.get("generated")),
"pages_url": pages_url(snap) or "./",
"items": items[:STATE_ITEMS_MAX],
}
def render(snap: dict, fmt: str = "md") -> str:
if fmt == "md":
return _render_md(snap)
if fmt == "md-brief":
return _render_md_brief(snap)
if fmt == "html":
return _render_html(snap)
if fmt == "json":
return json.dumps({**snap, "pages_url": pages_url(snap)}, indent=2,
sort_keys=True)
if fmt == "badge":
return json.dumps(badge_endpoint(snap))
if fmt == "state":
return json.dumps(to_state(snap), indent=2)
if fmt == "due-names":
# The void workflow's `void: all-due` set: due Gut rows and overdue
# sibling-held rows — undated, orphan and history-only rows are never
# in it. (Names only; `void-plan` carries the repo per name.)
return "\n".join(r["name"] for r in due_rows(snap)
if valid_name(r["name"]))
if fmt == "void-plan":
# What void.yml may act on, by bucket, each entry {name, repo, sha}
# (repo = the Gut itself, or the sibling the ref is held on):
# `due` (the all-due set), `voidable` (due + transit + orphans +
# dated sibling-held — one-tap single voids) and `held` (refused:
# undated entries, here or on a sibling, are voided only in a session).
b = snap.get("buckets") or {}
def entry(r):
return {"name": r["name"], "repo": row_repo(snap, r),
"sha": r.get("sha") or ""}
def pick(rows):
out = {(e["repo"], e["name"]): e for e in map(entry, rows)
if e["sha"] and e["repo"] and valid_name(e["name"])}
return [out[k] for k in sorted(out, key=lambda k: (k[1], k[0]))]
voidable = [r for k in VOIDABLE for r in b.get(k) or []
if r.get("void_url")]
held = list(b.get("held") or []) + [
r for r in b.get("elsewhere") or []
if r.get("sha") and not foreign_voidable(r)]
return json.dumps({"refs_listed": bool(snap.get("refs_listed")),
"repo": snap.get("repo") or "",
"due": pick(due_rows(snap)),
"voidable": pick(voidable),
"held": pick(held)}, indent=2)
raise ValueError(f"unknown board fmt: {fmt!r}")
# --- CLI ------------------------------------------------------------------------
FORMATS = ("md", "md-brief", "html", "json", "badge", "state", "due-names",
"void-plan")
def main(argv=None) -> int:
import argparse
ap = argparse.ArgumentParser(prog="board.py", description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
g = ap.add_mutually_exclusive_group()
for f in FORMATS:
g.add_argument(f"--{f}", action="store_true")
ap.add_argument("--mind", help="PyAutoMind checkout (holds condemned.md)")
ap.add_argument("--brain", help="PyAutoBrain checkout (parser + theme)")
ap.add_argument("--remote", default="origin")
ap.add_argument("--repo", help="the PyAutoGut checkout to ls-remote from")
ap.add_argument("--today", help="pin the reference date (YYYY-MM-DD)")
src = ap.add_mutually_exclusive_group()
src.add_argument("--collect", metavar="OUT")
src.add_argument("--snapshot", metavar="F")
ns = ap.parse_args(argv)
_THEME_BRAIN[0] = ns.brain
today = _date(ns.today) if ns.today else None
if ns.collect:
snap = collect(ns.mind, ns.brain, ns.remote, ns.repo, today)
Path(ns.collect).write_text(json.dumps(snap, indent=2, sort_keys=True)
+ "\n", encoding="utf-8")
print(f"collected → {ns.collect} ({snap['ref_count']} refs, "
f"{snap['entry_count']} entries; {_summary(snap)}; "
f"{len(snap['errors'])} error(s))", file=sys.stderr)
return 0
snap = (json.loads(Path(ns.snapshot).read_text(encoding="utf-8"))
if ns.snapshot else collect(ns.mind, ns.brain, ns.remote, ns.repo,
today))
fmt = next((f for f in FORMATS if getattr(ns, f.replace("-", "_"))), "md")
print(render(snap, fmt))
return 0
if __name__ == "__main__":
sys.exit(main())