@giamme/forge 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/SKILL.md +16 -3
  3. package/VERSION +1 -1
  4. package/docs/artifacts.md +8 -0
  5. package/docs/commands.md +70 -6
  6. package/docs/dataset.md +5 -0
  7. package/docs/decomposed-runs.md +8 -0
  8. package/docs/forge-ui.md +23 -2
  9. package/docs/memory.md +2 -0
  10. package/docs/safety.md +8 -0
  11. package/forge +24 -1
  12. package/package.json +1 -1
  13. package/references/dataset.md +19 -0
  14. package/references/decompose.md +134 -1
  15. package/references/solo.md +2 -1
  16. package/references/ui.md +5 -2
  17. package/scripts/forge-artifact.sh +14 -0
  18. package/scripts/forge-brief.py +132 -0
  19. package/scripts/forge-compare.py +11 -1
  20. package/scripts/forge-config.py +176 -9
  21. package/scripts/forge-contract.py +46 -3
  22. package/scripts/forge-control.py +86 -13
  23. package/scripts/forge-dispatch.sh +14 -10
  24. package/scripts/forge-du.py +167 -0
  25. package/scripts/forge-lib-adopt.sh +1 -0
  26. package/scripts/forge-lib-plans.sh +5 -2
  27. package/scripts/forge-lib-review.sh +29 -1
  28. package/scripts/forge-metrics.sh +7 -3
  29. package/scripts/forge-parallel-options.sh +4 -0
  30. package/scripts/forge-parallel.sh +503 -9
  31. package/scripts/forge-prompt.py +120 -0
  32. package/scripts/forge-retro.py +115 -0
  33. package/scripts/forge-runs-compact.py +443 -0
  34. package/scripts/forge-runs-rm.py +2 -0
  35. package/scripts/forge-runs.py +18 -3
  36. package/scripts/forge-schedule.py +279 -4
  37. package/scripts/forge-shadow.py +7 -1
  38. package/scripts/forge-solo.sh +2 -0
  39. package/scripts/forge-summarize.py +698 -0
  40. package/scripts/forge-ui-options.sh +10 -0
  41. package/scripts/forge_brief/__init__.py +587 -0
  42. package/scripts/forge_brief/answers.py +459 -0
  43. package/scripts/forge_dataset/__init__.py +10 -0
  44. package/scripts/forge_dataset/export.py +18 -4
  45. package/scripts/forge_dataset/facts.py +6 -4
  46. package/scripts/forge_dataset/harvest.py +40 -6
  47. package/scripts/forge_dataset/history.py +5 -3
  48. package/scripts/forge_decide/__init__.py +11 -1
  49. package/scripts/forge_locations.py +366 -0
  50. package/scripts/forge_registry.py +19 -0
  51. package/scripts/forge_retro/__init__.py +417 -0
  52. package/scripts/forge_retro/issues.py +1193 -0
  53. package/scripts/forge_retro/report.py +671 -0
  54. package/scripts/forge_side/__init__.py +477 -0
  55. package/scripts/forge_storage.py +515 -0
  56. package/ui/README.md +68 -8
  57. package/ui/docs/api.md +205 -17
  58. package/ui/forge_ui/control.py +92 -11
  59. package/ui/forge_ui/export.py +24 -5
  60. package/ui/forge_ui/links.py +337 -0
  61. package/ui/forge_ui/models.py +57 -4
  62. package/ui/forge_ui/projects.py +4 -1
  63. package/ui/forge_ui/reader.py +661 -30
  64. package/ui/forge_ui/static/app.js +86 -4
  65. package/ui/forge_ui/static/briefview.js +302 -0
  66. package/ui/forge_ui/static/checksview.js +6 -2
  67. package/ui/forge_ui/static/controls.js +5 -0
  68. package/ui/forge_ui/static/index.html +3 -0
  69. package/ui/forge_ui/static/panel.js +268 -2
  70. package/ui/forge_ui/static/runmodel.js +13 -1
  71. package/ui/forge_ui/static/storageview.js +271 -0
  72. package/ui/forge_ui/static/style.css +99 -3
  73. package/ui/forge_ui/static/worktreesview.js +3 -3
  74. package/ui/forge_ui/storage.py +136 -0
  75. package/ui/forge_ui/transcripts.py +35 -10
  76. package/ui/forge_ui/worktrees.py +154 -13
@@ -61,6 +61,13 @@
61
61
  --sev-p1: light-dark(#783ebd, #c18dff);
62
62
  --sev-p2: light-dark(#335dbd, #88a9ff);
63
63
  --sev-p3: light-dark(#56635b, #a7b2ab);
64
+ /* the Disk screen's parts, a fixed categorical order checked for colour-blind separation on --module, both themes */
65
+ --disk-1: light-dark(#2a78d6, #3987e5);
66
+ --disk-2: light-dark(#eb6834, #d95926);
67
+ --disk-3: light-dark(#1baf7a, #199e70);
68
+ --disk-4: light-dark(#eda100, #c98500);
69
+ --disk-5: light-dark(#e87ba4, #d55181);
70
+ --disk-6: light-dark(#6250d6, #9085e9);
64
71
  /* the 3D stage: body, hairlines and the accent of what is live */
65
72
  --stage-accent: var(--lamp-amber);
66
73
  --stage-body: light-dark(#f3f5f0, #161c19);
@@ -337,7 +344,7 @@ body.no-tasks .tasks-module { display: none; }
337
344
  body[data-shell=workbench].no-tasks .run-view { grid-template-rows: auto minmax(0, 1fr) auto 0; }
338
345
  body[data-shell=immersive] .sidebar { position: absolute; z-index: 4; left: var(--gap); top: var(--gap); bottom: var(--gap); width: var(--runs-w); box-shadow: var(--ply-1); }
339
346
  body[data-shell=immersive][data-runs=closed] .sidebar { display: none; }
340
- body[data-shell=immersive]:is([data-view=models], [data-view=findings], [data-view=settings], [data-view=worktrees], [data-view=projects], [data-view=waiting], [data-view=loading], [data-view=first-run]) #main { left: calc(var(--L) - var(--gap)); }
347
+ body[data-shell=immersive]:is([data-view=models], [data-view=findings], [data-view=settings], [data-view=worktrees], [data-view=disk], [data-view=projects], [data-view=waiting], [data-view=loading], [data-view=first-run]) #main { left: calc(var(--L) - var(--gap)); }
341
348
  body[data-shell=immersive] .stage-row, body[data-shell=immersive] .stage-col { position: absolute; inset: 0; }
342
349
  body[data-shell=immersive] .stage-col .stage-slot { position: absolute; inset: 0; }
343
350
  body[data-shell=immersive] .insp-module { position: absolute; z-index: 3; right: var(--gap); top: var(--gap); bottom: var(--gap); width: var(--insp-w); box-shadow: var(--ply-1); }
@@ -392,7 +399,7 @@ body[data-shell=workbench] .map-cards-host { overflow: auto; }
392
399
  body[data-shell=workbench] .map-cards { padding: var(--s-4); }
393
400
 
394
401
  /* content views: the leaderboard and the waiting pages */
395
- body:is([data-view=models], [data-view=findings], [data-view=settings], [data-view=worktrees], [data-view=projects], [data-view=waiting], [data-view=loading], [data-view=first-run]) #main { padding: var(--s-6) var(--s-6) var(--s-8); }
402
+ body:is([data-view=models], [data-view=findings], [data-view=settings], [data-view=worktrees], [data-view=disk], [data-view=projects], [data-view=waiting], [data-view=loading], [data-view=first-run]) #main { padding: var(--s-6) var(--s-6) var(--s-8); }
396
403
 
397
404
  /* ---------- the stage: the panel the run is inlaid on ---------- */
398
405
  .stage-slot { position: relative; background: var(--stage-bg); }
@@ -545,7 +552,13 @@ dialog.keys-sheet::backdrop { background: light-dark(rgb(17 22 19 / .35), rgb(0
545
552
  .strip-wrap[hidden], .graph[hidden] { display: none; }
546
553
  .tasks-module { overflow: hidden; }
547
554
  .tasks-module .run-tasks { flex: 1; min-height: 0; overflow: auto; }
548
- body[data-tasks=closed] .tasks-module :is(.run-tasks, .splitter, .dt-tools) { display: none; }
555
+ /* the run's summaries switch, above the table (#133 B): a row of its own, its model beside the bank */
556
+ .tasks-switch { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-1) var(--s-2); padding: var(--s-1) var(--s-2); border-bottom: 1px solid var(--rule); font-size: var(--t-sm); }
557
+ .tasks-switch[hidden] { display: none; }
558
+ .tasks-switch .sum-msgs { flex-basis: 100%; }
559
+ .tasks-switch .sum-model { display: inline-flex; }
560
+ .tasks-switch .sum-state { font-weight: 600; }
561
+ body[data-tasks=closed] .tasks-module :is(.run-tasks, .splitter, .dt-tools, .tasks-switch, .run-questions) { display: none; }
549
562
  body[data-tasks=closed] .tasks-module .module-head { border-bottom: 0; }
550
563
  .run-tasks .dt, .run-tasks .dt-frame, .run-tasks .table-scroll { height: 100%; }
551
564
  .run-tasks .table-scroll { border: 0; border-radius: 0; }
@@ -556,6 +569,20 @@ body[data-tasks=closed] .tasks-module .module-head { border-bottom: 0; }
556
569
  .insp-head h2 { font-size: var(--t-md); line-height: 1.3; margin: 0 0 2px; }
557
570
  .insp-kind { color: var(--muted); font-size: var(--t-sm); }
558
571
  .insp-head .chip { margin-top: 4px; }
572
+ /* a task opens here: title, goal and links, then the text buttons, then one line per attempt */
573
+ .task-header { display: grid; gap: 6px; margin: 0 0 var(--s-3); min-width: 0; }
574
+ .task-header-top { display: grid; gap: 4px; padding-right: 72px; min-width: 0; } /* clear the Close key */
575
+ .task-header h2 { font-size: var(--t-md); font-weight: 650; line-height: 1.3; margin: 0; }
576
+ .task-header .task-goal { margin: 0; color: var(--ink); text-wrap: pretty; }
577
+ .task-links { margin: 0; }
578
+ .task-links a.chip::before { content: none; } /* a link is not a status: no lamp */
579
+ .task-link-text { color: var(--ink-2); font-family: var(--mono); font-size: var(--t-xs); font-variant-numeric: tabular-nums; }
580
+ .task-texts { flex-wrap: wrap; max-width: 100%; }
581
+ .inspector .task-reveal { margin: 0; }
582
+ .attempt-trail { list-style: none; margin: 0 0 var(--s-3); padding: 0; display: grid; gap: 2px; min-width: 0; }
583
+ .trail-line { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px; min-width: 0; font-size: var(--t-xs); }
584
+ .trail-summary { font-family: var(--mono); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
585
+ .trail-nav { display: inline-flex; flex-wrap: wrap; gap: 4px 10px; }
559
586
  .inspector > .insp-close { position: absolute; right: var(--s-3); top: var(--s-3); min-height: 28px; padding: 0 9px; }
560
587
  .insp-section { margin: var(--s-5) 0 0; }
561
588
  .inspector > .insp-section:first-child { margin-top: var(--s-1); }
@@ -765,9 +792,49 @@ p.ctl-request-refused { color: var(--warn-ink); font-weight: 600; }
765
792
  .ctl-changes li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; }
766
793
  .ctl-what { color: var(--ink); font-weight: 600; }
767
794
  .ctl[aria-busy=true] .ctl-row { opacity: .7; }
795
+
796
+ /* a task's summary (#133 B): its text, where it came from, and the task's own switch (a row like a control's) */
797
+ .sum-section .sum-text { margin: 0 0 var(--s-1); white-space: pre-line; overflow-wrap: anywhere; }
798
+ .sum-by { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; margin: 0 0 var(--s-2); font-size: var(--t-xs); }
799
+ .sum-error { margin: 0 0 var(--s-2); }
800
+ .sum-sources { list-style: none; margin: 0 0 var(--s-2); padding: 0; display: grid; gap: 2px; font-size: var(--t-xs); }
801
+ .sum-sources li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px; overflow-wrap: anywhere; }
802
+ .sum-switch { display: grid; gap: 6px; padding: var(--s-2) 10px; border: 1px solid var(--rule); border-radius: var(--r-key); background: var(--module-2); }
803
+ .sum-label { margin: 0; font-family: var(--font-plate); font-weight: 700; font-size: .786rem; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
804
+ .sum-bank { justify-self: start; flex-wrap: wrap; max-width: 100%; }
805
+ p.sum-note { margin: 0; font-size: var(--t-xs); }
806
+ .sum-msgs:empty { display: none; }
807
+ /* the one-sentence column: one line, cut off at the cell's edge */
808
+ .sum-col { max-width: 32ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
768
809
  /* who decided an escalation, on the line that reports it */
769
810
  .src-plate { font-size: .714rem; padding: 0 5px; vertical-align: 1px; }
770
811
 
812
+ /* brief questions (#135): the run's open questions above the task table, and each question in the inspector.
813
+ The tasks module is 208px tall, so the list is a short strip and scrolls; the table keeps the rest. */
814
+ .run-questions { display: flex; flex-direction: column; flex: 0 1 auto; max-height: 5.5rem; min-height: 0; overflow: hidden; border-bottom: 1px solid var(--rule); }
815
+ .run-questions[hidden] { display: none; }
816
+ .brief-list-head { display: flex; align-items: center; gap: var(--s-2); margin: 0; padding: 6px 10px; flex: none; }
817
+ .brief-list { list-style: none; margin: 0; padding: 0; overflow: auto; min-height: 0; flex: 1 1 auto; }
818
+ a.brief-open { display: grid; gap: 2px; min-width: 0; padding: 6px 10px; border-top: 1px solid var(--rule); color: inherit; text-decoration: none; }
819
+ a.brief-open:hover { text-decoration: none; background: var(--module-2); }
820
+ .brief-open-top { display: flex; flex-wrap: wrap; gap: 2px 8px; align-items: baseline; min-width: 0; }
821
+ .brief-open-task { color: var(--muted); }
822
+ .brief-open-text, .brief-text, .brief-got { overflow-wrap: anywhere; }
823
+ .brief-when { color: var(--muted); font-size: var(--t-xs); font-variant-numeric: tabular-nums; }
824
+ .brief-section .brief-questions { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--s-2); }
825
+ .brief-q { display: grid; gap: 4px; padding: var(--s-2) 10px; border: 1px solid var(--rule); border-radius: var(--r-key); background: var(--module-2); }
826
+ .brief-q p { margin: 0; }
827
+ .brief-text { white-space: pre-line; }
828
+ .brief-why, .brief-options, .brief-default, .brief-scope { font-size: var(--t-xs); color: var(--muted); overflow-wrap: anywhere; }
829
+ .brief-flags { display: flex; flex-wrap: wrap; gap: var(--s-1); }
830
+ .brief-box { display: grid; gap: 6px; margin-top: 2px; }
831
+ .brief-box textarea { width: 100%; min-height: 4.5rem; padding: 6px 10px; resize: vertical; }
832
+ .brief-box .brief-send { justify-self: start; }
833
+ .brief-msgs:empty { display: none; }
834
+ .brief-q .brief-notice { margin: 0; }
835
+ .brief-parked { margin-top: var(--s-2); }
836
+ .brief-parked > .brief-questions { margin-top: var(--s-2); }
837
+
771
838
  /* ---------- tables ---------- */
772
839
  .table-scroll { overflow: auto; max-width: 100%; border: 1px solid var(--rule); border-radius: var(--r-key); background: var(--module); }
773
840
  table { width: 100%; border-collapse: separate; border-spacing: 0; text-align: left; font-variant-numeric: tabular-nums; }
@@ -1326,6 +1393,35 @@ table.wt-grid { width: max-content; min-width: 100%; }
1326
1393
  .wt-group { display: grid; grid-template-columns: minmax(8em, max-content) 1fr; align-items: center; gap: var(--s-3); margin: var(--s-2) 0; }
1327
1394
  .wt-copy:focus-visible, .wt-cmd code:focus-visible, .wt-groups summary:focus-visible { outline: 2px solid var(--route); outline-offset: 2px; }
1328
1395
 
1396
+ /* ---------- disk (#/disk), on the worktrees screen's parts ---------- */
1397
+ .disk-commands { display: grid; gap: 4px; max-width: 48em; }
1398
+ .disk-extra { display: grid; grid-template-columns: repeat(auto-fit, minmax(22em, 1fr)); gap: var(--s-4); }
1399
+ .disk-block h2 { margin: 0 0 var(--s-2); }
1400
+ .disk-block p { margin: 0 0 var(--s-2); }
1401
+ .disk-list { margin: 0; padding-left: 1.2em; font-size: var(--t-sm); font-variant-numeric: tabular-nums; }
1402
+ .disk-list li { overflow-wrap: anywhere; }
1403
+ .disk-view { position: relative; }
1404
+ .disk-chart { display: grid; gap: var(--s-2); padding: var(--s-4); border-radius: var(--r-module); background: var(--module); box-shadow: var(--ply-1); }
1405
+ .disk-chart h2 { margin: 0; justify-self: start; }
1406
+ .disk-chart .disk-sub { margin-top: var(--s-3); }
1407
+ .disk-bar { display: flex; gap: 2px; height: 14px; min-width: 2px; }
1408
+ .disk-bar-whole { height: 22px; }
1409
+ .disk-seg { flex-basis: 0; min-width: 2px; height: 100%; cursor: default; }
1410
+ .disk-seg:first-child { border-radius: 4px 0 0 4px; }
1411
+ .disk-seg:last-child { border-radius: 0 4px 4px 0; }
1412
+ .disk-seg:only-child { border-radius: 4px; }
1413
+ .disk-seg:hover, .disk-seg:focus-visible { outline: 2px solid var(--route); outline-offset: 1px; position: relative; z-index: 1; }
1414
+ .disk-legend { display: flex; flex-wrap: wrap; gap: var(--s-2) var(--s-4); margin: 0; padding: 0; list-style: none; font-size: var(--t-sm); font-variant-numeric: tabular-nums; }
1415
+ .disk-legend li { display: inline-flex; align-items: center; gap: 6px; }
1416
+ .disk-swatch { width: 10px; height: 10px; border-radius: 2px; flex: none; }
1417
+ .disk-repos { display: grid; gap: 6px; }
1418
+ .disk-repo { display: grid; grid-template-columns: minmax(8em, 16em) minmax(0, 1fr) 6em; align-items: center; gap: var(--s-3); font-size: var(--t-sm); }
1419
+ .disk-repo-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
1420
+ .disk-repo-size { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-2); }
1421
+ .disk-track { min-width: 0; }
1422
+ .disk-tip { position: absolute; z-index: 5; display: grid; gap: 2px; max-width: 22em; padding: 6px 10px; border-radius: var(--r-key); background: var(--key); color: var(--ink); box-shadow: var(--ply-2); font-size: var(--t-sm); pointer-events: none; font-variant-numeric: tabular-nums; }
1423
+ @media (max-width: 640px) { .disk-repo { grid-template-columns: minmax(0, 1fr) 5em; } .disk-track { grid-column: 1 / -1; grid-row: 2; } }
1424
+
1329
1425
  /* ---------- projects (#/projects) ---------- */
1330
1426
  .projects-view { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s-3); max-width: 1600px; }
1331
1427
  .prj-head { display: flex; align-items: baseline; gap: var(--s-4); flex-wrap: wrap; }
@@ -57,7 +57,7 @@
57
57
  const UI = root.ForgeUI;
58
58
  const el = (tag, text, cls) => UI.el(tag, text, cls);
59
59
  const start = ctx.filter || {};
60
- const s = { data: null, error: null, filter: { repo: start.repo || '', run: start.run || '', status: 'all', kind: 'all' }, timer: null, seq: 0, loading: false };
60
+ const s = { data: null, error: null, filter: { repo: start.repo || '', run: start.run || '', status: 'disk', kind: 'all' }, timer: null, seq: 0, loading: false };
61
61
 
62
62
  const head = el('div', undefined, 'wt-head');
63
63
  const refreshKey = el('button', 'Read again'); refreshKey.type = 'button';
@@ -180,8 +180,8 @@
180
180
  notes.replaceChildren(...out);
181
181
  }
182
182
  function drawTotals(rows) {
183
- const t = totals(rows);
184
- sums.replaceChildren(el('strong', `${t.onDisk} on disk`), el('span', ` · ${humanBytes(t.bytes)}${t.pending ? ` so far, ${t.pending} still being measured` : ''} · ${t.count - t.onDisk} removed or missing · ${t.count} listed`));
183
+ const t = totals(rows), hidden = list(s.data && s.data.rows).length - list(rows).length;
184
+ sums.replaceChildren(el('strong', `${t.onDisk} on disk`), el('span', ` · ${humanBytes(t.bytes)}${t.pending ? ` so far, ${t.pending} still being measured` : ''} · ${t.count - t.onDisk} removed or missing · ${t.count} listed${hidden > 0 ? ` · ${hidden} more hidden by the filters` : ''}`));
185
185
  }
186
186
  function drawGroups() {
187
187
  const items = list(s.data && s.data.runs).filter(g => g.command && (!s.filter.run || g.key === s.filter.run) && (!s.filter.repo || g.repo === s.filter.repo));
@@ -0,0 +1,136 @@
1
+ """The Disk screen (#139): what forge takes on disk, in every repository it has runs for, and the command that
2
+ takes each part back. The same report as `forge du --all`, from the same rules (scripts/forge_storage.py,
3
+ loaded read-only the way worktrees.py loads forge_worktrees.py), never a second rule.
4
+
5
+ Read-only: the commands are text to copy, never run, so the screen works with --read-only too.
6
+
7
+ Cost: measuring visits every file of every run dir (tens of seconds for tens of GB), so none of it happens
8
+ on the normal poll. The screen asks /api/storage when it opens; the first ask starts one background
9
+ measurement and answers `measuring` until it is done. The answer is kept for CACHE_S; ?refresh=1 measures
10
+ again. Nothing is written: the footprint the storage.warn-gb check reads is `forge du`'s and the background
11
+ compaction's to keep.
12
+ """
13
+ from __future__ import annotations
14
+
15
+ import importlib.machinery
16
+ import importlib.util
17
+ from pathlib import Path
18
+ import shlex
19
+ import threading
20
+ import time
21
+
22
+ RULES = Path(__file__).resolve().parents[2] / 'scripts' / 'forge_storage.py'
23
+ CACHE_S = 600
24
+ _STATE = {}
25
+ _LOCK = threading.Lock()
26
+
27
+
28
+ class _ReadOnlyLoader(importlib.machinery.SourceFileLoader):
29
+ def set_data(self, *args, **kwargs):
30
+ pass # no .pyc beside forge's scripts
31
+
32
+
33
+ def rules():
34
+ """forge's storage rule module (scripts/forge_storage.py), or None when it cannot be loaded."""
35
+ with _LOCK:
36
+ if 'rules' not in _STATE:
37
+ try:
38
+ spec = importlib.util.spec_from_file_location(
39
+ 'forge_storage_for_ui', RULES, loader=_ReadOnlyLoader('forge_storage_for_ui', str(RULES)))
40
+ module = importlib.util.module_from_spec(spec)
41
+ spec.loader.exec_module(module)
42
+ if not (callable(module.report) and callable(module.scan)):
43
+ raise TypeError('not the forge storage module')
44
+ _STATE['rules'] = module
45
+ except Exception:
46
+ _STATE['rules'] = None
47
+ return _STATE['rules']
48
+
49
+
50
+ def data_home():
51
+ """forge's data home, as the dataset resolves it; None when the dataset package cannot be loaded."""
52
+ try:
53
+ import sys
54
+ scripts = str(RULES.parent)
55
+ if scripts not in sys.path:
56
+ sys.path.insert(0, scripts)
57
+ from forge_dataset import data_home as home
58
+ return home()
59
+ except Exception:
60
+ return None
61
+
62
+
63
+ def q(text):
64
+ return shlex.quote(str(text))
65
+
66
+
67
+ def with_commands(result):
68
+ """The report with the commands that take each part back: text to copy, never run here."""
69
+ for run in result['runs']:
70
+ run['commands'] = []
71
+ if run['reclaimable']:
72
+ run['commands'].append(dict(label='remove its review snapshots and gzip its log copies (the run stays, viewable)',
73
+ command='forge runs compact %s --logs' % q(run['path'])))
74
+ run['commands'].append(dict(label='delete the run dir, after the dataset and an archive keep its traces (dry run)',
75
+ command='forge runs rm %s --dry-run' % q(run['path'])))
76
+ commands = []
77
+ if result['totals']['reclaimable']:
78
+ commands.append(dict(label='every run: remove the leftover review snapshots and gzip the log copies',
79
+ command='forge runs compact --all --logs', bytes=result['totals']['reclaimable']))
80
+ commands.append(dict(label='the same report in the terminal', command='forge du --all'))
81
+ result['commands'] = commands
82
+ return result
83
+
84
+
85
+ class Screen:
86
+ """The cached answer of /api/storage for one server; one measurement at a time, in a background thread."""
87
+
88
+ def __init__(self):
89
+ self.lock = threading.Lock()
90
+ self.result = None
91
+ self.error = None
92
+ self.started = None
93
+ self.progress = None
94
+ self.thread = None
95
+
96
+ def _work(self, module):
97
+ try:
98
+ home = data_home()
99
+
100
+ def progress(index, count, _path):
101
+ with self.lock:
102
+ self.progress = dict(done=index, of=count)
103
+ result = with_commands(module.report(None, home=home, progress=progress))
104
+ with self.lock:
105
+ self.result, self.error = result, None
106
+ except Exception as exc: # noqa: BLE001 -- the screen says what went wrong instead
107
+ with self.lock:
108
+ self.error = '%s: %s' % (type(exc).__name__, exc)
109
+ finally:
110
+ with self.lock:
111
+ self.thread, self.progress = None, None
112
+
113
+ def answer(self, refresh=False, now=None):
114
+ now = time.time() if now is None else now
115
+ module = rules()
116
+ if module is None:
117
+ return dict(state='unavailable', error='scripts/forge_storage.py could not be loaded', data=None)
118
+ with self.lock:
119
+ stale = self.result is None or now - self.result['computed'] > CACHE_S
120
+ if (refresh or stale) and self.thread is None:
121
+ self.started = now
122
+ self.thread = threading.Thread(target=self._work, args=(module,), name='forge-ui-storage', daemon=True)
123
+ self.thread.start()
124
+ measuring = self.thread is not None
125
+ return dict(state='measuring' if measuring else ('error' if self.error and self.result is None else 'ready'),
126
+ error=self.error, started=self.started if measuring else None, progress=self.progress,
127
+ data=self.result)
128
+
129
+
130
+ def screen(server):
131
+ """The server's Screen, made on first use."""
132
+ with _LOCK:
133
+ found = getattr(server, 'storage_screen', None)
134
+ if found is None:
135
+ found = server.storage_screen = Screen()
136
+ return found
@@ -34,11 +34,13 @@ from __future__ import annotations
34
34
 
35
35
  import bisect
36
36
  from datetime import datetime
37
+ import gzip
37
38
  import json
38
39
  import os
39
40
  from pathlib import Path
40
41
  import re
41
42
  import stat
43
+ import zlib
42
44
 
43
45
  TEXT_CAP = 20000 # characters kept of one message, reasoning or result
44
46
  OUTPUT_CAP = 8000 # of a command's or tool's output: the head and the tail
@@ -562,31 +564,54 @@ def session_file(session_id, home=None, environ=None):
562
564
 
563
565
  # ---------- one execution's transcript ----------
564
566
 
567
+ GZ_CAP = 512 << 20 # bytes a gzipped log may expand to before it is refused
568
+
569
+
565
570
  def _read(path):
566
- """The log's text (its end when it is over LOG_CAP), whether it was cut, and the byte offset it starts at."""
571
+ """The log's text (its end when it is over LOG_CAP), whether it was cut, and the byte offset it starts at.
572
+
573
+ A `.gz` log (an execution record's copy that `forge runs compact --logs` gzipped, #139) reads as the
574
+ plain log it was: the same text and the same offsets, so its clock still places every line."""
567
575
  base = 0
568
576
  try:
569
- size = path.stat().st_size
570
- with open(path, 'rb') as fh:
577
+ if path.name.endswith('.gz'):
578
+ with gzip.open(path, 'rb') as fh:
579
+ data = fh.read(GZ_CAP + 1)
580
+ if len(data) > GZ_CAP:
581
+ return None, False, 0
582
+ size = len(data)
571
583
  if size > LOG_CAP:
572
- fh.seek(size - LOG_CAP)
573
- data = fh.read()
584
+ data = data[size - LOG_CAP:]
574
585
  skip = data.find(b'\n') + 1
575
586
  data, base = data[skip:], size - LOG_CAP + skip
576
- else:
577
- data = fh.read()
578
- except OSError:
587
+ else:
588
+ size = path.stat().st_size
589
+ with open(path, 'rb') as fh:
590
+ if size > LOG_CAP:
591
+ fh.seek(size - LOG_CAP)
592
+ data = fh.read()
593
+ skip = data.find(b'\n') + 1
594
+ data, base = data[skip:], size - LOG_CAP + skip
595
+ else:
596
+ data = fh.read()
597
+ except (OSError, EOFError, zlib.error):
579
598
  return None, False, 0
580
599
  return data.decode('utf-8', 'replace'), size > LOG_CAP, base
581
600
 
582
601
 
602
+ def clock_path(log):
603
+ """`<role>.clock` beside `<role>.log` or `<role>.log.gz`."""
604
+ name = log.name[:-3] if log.name.endswith('.gz') else log.name
605
+ return log.with_name(name.rsplit('.', 1)[0] + '.clock')
606
+
607
+
583
608
  CLOCK_CAP = 8 << 20 # bytes of a clock read
584
609
 
585
610
 
586
611
  def clock_of(log):
587
612
  """The clock beside `log` (`<role>.clock` for `<role>.log`) as (sizes, times), sizes ascending; None without one."""
588
613
  try:
589
- path = log.with_name(log.stem + '.clock')
614
+ path = clock_path(log)
590
615
  with open(path, 'rb') as fh:
591
616
  if not stat.S_ISREG(os.fstat(fh.fileno()).st_mode):
592
617
  return None
@@ -644,7 +669,7 @@ def transcript(log, harness=None, running=False, home=None, environ=None):
644
669
  if st is None or not stat.S_ISREG(st.st_mode):
645
670
  out['warnings'].append(f'{log.name} is not readable')
646
671
  return out
647
- key = (str(log), st.st_size, st.st_mtime_ns, harness, running, _signature(log.with_name(log.stem + '.clock')))
672
+ key = (str(log), st.st_size, st.st_mtime_ns, harness, running, _signature(clock_path(log)))
648
673
  if key in _CACHE:
649
674
  answer, sid, seen = _CACHE[key]
650
675
  if sid is None or _signature(session_file(sid, home, environ)) == seen: