@giamme/forge 0.17.0 → 0.19.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 (110) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.md +1 -0
  3. package/SKILL.md +32 -5
  4. package/VERSION +1 -1
  5. package/docs/artifacts.md +33 -1
  6. package/docs/commands.md +147 -16
  7. package/docs/dataset.md +5 -0
  8. package/docs/decomposed-runs.md +122 -1
  9. package/docs/development.md +2 -1
  10. package/docs/forge-ui.md +98 -8
  11. package/docs/memory.md +27 -4
  12. package/docs/models.md +8 -0
  13. package/docs/reviews.md +122 -0
  14. package/docs/safety.md +8 -0
  15. package/forge +44 -4
  16. package/package.json +2 -1
  17. package/references/dataset.md +39 -1
  18. package/references/decompose.md +737 -16
  19. package/references/harnesses.md +18 -3
  20. package/references/planning.md +43 -14
  21. package/references/solo.md +2 -1
  22. package/references/ui.md +56 -6
  23. package/registry.tsv +8 -3
  24. package/scripts/forge-artifact.sh +14 -0
  25. package/scripts/forge-brief.py +217 -0
  26. package/scripts/forge-compare.py +13 -2
  27. package/scripts/forge-config.py +176 -9
  28. package/scripts/forge-contract.py +246 -28
  29. package/scripts/forge-control.py +200 -15
  30. package/scripts/forge-dispatch.sh +48 -15
  31. package/scripts/forge-du.py +167 -0
  32. package/scripts/forge-group.py +496 -0
  33. package/scripts/forge-groups.py +89 -0
  34. package/scripts/forge-handoff.py +1128 -0
  35. package/scripts/forge-lib-adopt.sh +1 -0
  36. package/scripts/forge-lib-group.sh +641 -0
  37. package/scripts/forge-lib-plans.sh +343 -4
  38. package/scripts/forge-lib-review.sh +29 -1
  39. package/scripts/forge-lib-task.sh +197 -4
  40. package/scripts/forge-memory.sh +245 -3
  41. package/scripts/forge-metrics.sh +7 -3
  42. package/scripts/forge-parallel-options.sh +4 -0
  43. package/scripts/forge-parallel.sh +1216 -41
  44. package/scripts/forge-planner.py +104 -8
  45. package/scripts/forge-prompt.py +120 -0
  46. package/scripts/forge-retro.py +115 -0
  47. package/scripts/forge-route.py +190 -12
  48. package/scripts/forge-runs-compact.py +443 -0
  49. package/scripts/forge-runs-rm.py +2 -0
  50. package/scripts/forge-runs.py +27 -8
  51. package/scripts/forge-schedule.py +633 -49
  52. package/scripts/forge-shadow.py +7 -1
  53. package/scripts/forge-solo.sh +4 -0
  54. package/scripts/forge-summarize.py +698 -0
  55. package/scripts/forge-ui-options.sh +10 -0
  56. package/scripts/forge-worktrees.py +243 -0
  57. package/scripts/forge_brief/__init__.py +831 -0
  58. package/scripts/forge_brief/answers.py +506 -0
  59. package/scripts/forge_brief/proposals.py +1630 -0
  60. package/scripts/forge_dataset/__init__.py +10 -0
  61. package/scripts/forge_dataset/efforts.py +4 -3
  62. package/scripts/forge_dataset/export.py +36 -10
  63. package/scripts/forge_dataset/facts.py +8 -4
  64. package/scripts/forge_dataset/forecast.py +8 -18
  65. package/scripts/forge_dataset/harvest.py +241 -12
  66. package/scripts/forge_dataset/history.py +13 -6
  67. package/scripts/forge_dataset/importer.py +65 -1
  68. package/scripts/forge_dataset/names.py +66 -0
  69. package/scripts/forge_dataset/reviewers.py +5 -2
  70. package/scripts/forge_dataset/risk.py +6 -3
  71. package/scripts/forge_dataset/routing.py +6 -1
  72. package/scripts/forge_decide/__init__.py +11 -1
  73. package/scripts/forge_groups/__init__.py +308 -0
  74. package/scripts/forge_groups/attribute.py +289 -0
  75. package/scripts/forge_groups/review.py +286 -0
  76. package/scripts/forge_locations.py +366 -0
  77. package/scripts/forge_owner.py +152 -0
  78. package/scripts/forge_registry.py +19 -0
  79. package/scripts/forge_retro/__init__.py +417 -0
  80. package/scripts/forge_retro/issues.py +1193 -0
  81. package/scripts/forge_retro/report.py +671 -0
  82. package/scripts/forge_side/__init__.py +482 -0
  83. package/scripts/forge_storage.py +515 -0
  84. package/ui/README.md +68 -8
  85. package/ui/docs/api.md +375 -31
  86. package/ui/forge_ui/control.py +451 -11
  87. package/ui/forge_ui/executions.py +8 -1
  88. package/ui/forge_ui/export.py +53 -7
  89. package/ui/forge_ui/groups.py +264 -0
  90. package/ui/forge_ui/links.py +337 -0
  91. package/ui/forge_ui/models.py +125 -10
  92. package/ui/forge_ui/projects.py +4 -1
  93. package/ui/forge_ui/reader.py +919 -45
  94. package/ui/forge_ui/server.py +4 -0
  95. package/ui/forge_ui/static/app.js +173 -11
  96. package/ui/forge_ui/static/briefview.js +605 -0
  97. package/ui/forge_ui/static/checksview.js +6 -2
  98. package/ui/forge_ui/static/controls.js +5 -0
  99. package/ui/forge_ui/static/diffview.js +86 -1
  100. package/ui/forge_ui/static/groupsview.js +332 -0
  101. package/ui/forge_ui/static/index.html +4 -0
  102. package/ui/forge_ui/static/panel.js +394 -8
  103. package/ui/forge_ui/static/projectsview.js +189 -10
  104. package/ui/forge_ui/static/runmodel.js +27 -4
  105. package/ui/forge_ui/static/storageview.js +352 -0
  106. package/ui/forge_ui/static/style.css +176 -6
  107. package/ui/forge_ui/static/worktreesview.js +235 -14
  108. package/ui/forge_ui/storage.py +150 -0
  109. package/ui/forge_ui/transcripts.py +35 -10
  110. package/ui/forge_ui/worktrees.py +154 -13
@@ -41,6 +41,16 @@ forge_ui_register() {
41
41
  return 0
42
42
  }
43
43
 
44
+ # forge_storage_check RUN_DIR REPO — at run start (#139): warn on stderr when this repository's forge
45
+ # files (run dirs and worktrees) passed storage.warn-gb when they were last measured, and start the
46
+ # background compaction of its idle runs (storage.compact auto), never RUN_DIR itself. Never blocks
47
+ # or fails the run; callers skip it in a --dry-run. FORGE_STORAGE_COMPACT=off and
48
+ # FORGE_STORAGE_WARN_GB=off turn either part off.
49
+ forge_storage_check() {
50
+ python3 -I "$SKILL_DIR/scripts/forge-runs-compact.py" --start-check --repo "$2" --exclude "$1" </dev/null >/dev/null || true
51
+ return 0
52
+ }
53
+
44
54
  # forge_repo_identity CHECKOUT DIR — write DIR/repo.identity, the repository CHECKOUT belongs to:
45
55
  # {"root", "worktree", "branch"}. root is the main working tree, the same for the main checkout
46
56
  # and every linked worktree; worktree is CHECKOUT's physical path when it differs from root, else
@@ -0,0 +1,243 @@
1
+ #!/usr/bin/env python3
2
+ """forge worktrees rm PATH... [--branches] [--dry-run] [--json] [--actor A]
3
+
4
+ Remove worktrees by the rules of forge-ui's Worktrees screen (#147): the statuses come from
5
+ ui/forge_ui/worktrees.py `compute`, the one rule set, never a copy of it.
6
+
7
+ failed / finished `git -C <repo> worktree remove <path>`, without the force flag. With --branches
8
+ a `finished` worktree whose branch is merged also gets the safe branch delete
9
+ (`git branch -d <branch>`, which refuses an unmerged branch).
10
+ unknown + leftover the folder is deleted (shutil.rmtree), after checking again that git does
11
+ not list it as a worktree.
12
+ anything else skipped with a reason: in_use, dirty, locked, needed, missing (run
13
+ `git worktree prune`), foreign, removed, and a path that is no row.
14
+
15
+ Each removal is recorded the way `forge runs rm` records one: a `worktree_removed` dataset event
16
+ (with the actor) and a `gone` entry in forge's location list.
17
+
18
+ --dry-run computes the same actions and changes nothing (`would-remove`).
19
+ --actor is who asked: `cli` (the default) or `ui`.
20
+
21
+ --json prints exactly one object on stdout, on failure too:
22
+ {"ok": bool, "dry_run": bool,
23
+ "rows": [{"path", "action": "remove"|"remove+branch"|"delete-folder"|"none",
24
+ "status": "removed"|"skipped"|"failed"|"would-remove", "reason": str|null}]}
25
+
26
+ Exit codes: 0 no row failed (a skip is not a failure), 4 a row failed, 2 usage.
27
+ """
28
+ from __future__ import annotations
29
+
30
+ import argparse
31
+ import importlib.util
32
+ import json
33
+ import os
34
+ import shutil
35
+ import subprocess
36
+ import sys
37
+ from pathlib import Path
38
+ from types import SimpleNamespace
39
+
40
+ SKILL = Path(__file__).resolve().parent.parent
41
+ # `-I` keeps the script's own directory off sys.path, so both are added by hand (as forge-ui.py does).
42
+ sys.path.insert(0, str(SKILL / 'scripts'))
43
+ sys.path.insert(0, str(SKILL / 'ui'))
44
+
45
+ import forge_worktrees # noqa: E402
46
+ from forge_dataset import harvest # noqa: E402
47
+ from forge_ui import reader, worktrees # noqa: E402
48
+
49
+ OK, USAGE, FAILED = 0, 2, 4
50
+ ACTORS = ('cli', 'ui')
51
+ HISTORY_TIMEOUT_S = 120
52
+
53
+ SKIPS = {
54
+ 'in_use': 'its run is active',
55
+ 'dirty': 'it has uncommitted changes',
56
+ 'locked': 'it is locked by git',
57
+ 'needed': 'the integration worktree of a run not integrated yet',
58
+ 'missing': 'its folder is gone: run git worktree prune',
59
+ 'foreign': "not forge's worktree",
60
+ 'removed': 'already removed',
61
+ 'unknown': 'a folder git does not list, and not a leftover: look inside before deleting it',
62
+ }
63
+
64
+
65
+ def load_runs_rm():
66
+ """scripts/forge-runs-rm.py as a module: the recording of a removal is its code, reused."""
67
+ path = SKILL / 'scripts' / 'forge-runs-rm.py'
68
+ spec = importlib.util.spec_from_file_location('forge_runs_rm_for_worktrees', path)
69
+ module = importlib.util.module_from_spec(spec)
70
+ sys.modules[spec.name] = module
71
+ spec.loader.exec_module(module)
72
+ return module
73
+
74
+
75
+ def history():
76
+ """`forge dataset worktrees --json` through this checkout's launcher, or None."""
77
+ try:
78
+ done = subprocess.run([str(SKILL / 'forge'), 'dataset', 'worktrees', '--json'], capture_output=True, text=True,
79
+ stdin=subprocess.DEVNULL, timeout=HISTORY_TIMEOUT_S)
80
+ if done.returncode != 0:
81
+ return None
82
+ found = json.loads(done.stdout)
83
+ except (OSError, ValueError, subprocess.SubprocessError):
84
+ return None
85
+ return found if isinstance(found, dict) else None
86
+
87
+
88
+ def real(path):
89
+ try:
90
+ return os.path.realpath(path)
91
+ except (OSError, ValueError):
92
+ return path
93
+
94
+
95
+ def first_line(text):
96
+ lines = (text or '').strip().splitlines()
97
+ return lines[0] if lines else ''
98
+
99
+
100
+ def plan(path, row, branches):
101
+ """(action, skip reason) for one path: what would be done to it, or why nothing is."""
102
+ if row is None:
103
+ return 'none', 'not a worktree forge knows'
104
+ status = row['status']
105
+ if status in ('failed', 'finished'):
106
+ if not (row.get('git_dir') or row.get('repo')):
107
+ return 'none', 'its repository is not known'
108
+ if branches and status == 'finished' and row.get('merged') and row.get('branch'):
109
+ return 'remove+branch', None
110
+ return 'remove', None
111
+ if status == 'unknown' and row.get('leftover'):
112
+ return 'delete-folder', None
113
+ return 'none', SKIPS.get(status, 'status %s' % status)
114
+
115
+
116
+ def is_registered(repo, path):
117
+ """True when git lists `path` as a worktree of `repo`; True too when git cannot say (never delete then)."""
118
+ code, out = worktrees._git(repo, 'worktree', 'list', '--porcelain', '-z')
119
+ z = True
120
+ if code == 129:
121
+ code, out = worktrees._git(repo, 'worktree', 'list', '--porcelain')
122
+ z = False
123
+ if code != 0:
124
+ return True
125
+ return any(real(wt['path']) == real(path) for wt in forge_worktrees.parse_porcelain(out, z=z))
126
+
127
+
128
+ def record(rm, row, actor):
129
+ """The dataset event and the location-list entry of a removal, as `forge runs rm` writes them."""
130
+ by = 'forge worktrees rm (%s)' % actor
131
+ run_path = row.get('run_path')
132
+ try:
133
+ uid = harvest.run_uid(Path(run_path), create=False) if run_path else None
134
+ except Exception: # noqa: BLE001 -- an identity we cannot read only costs the event its run column
135
+ uid = None
136
+ run = SimpleNamespace(uid=uid, run_id=row.get('run_id'), mode='parallel', path=run_path or row['path'],
137
+ repo=row.get('repo'))
138
+ kind = row.get('kind') or 'task'
139
+ event = rm.event(run, 'worktree_removed', path=row['path'], branch=row.get('branch') or '-', kind=kind,
140
+ repo_path=row.get('repo'), by=by, actor=actor)
141
+ if kind == 'task' and row.get('branch'):
142
+ event['task'] = row['branch'].split('/', 2)[-1]
143
+ elif row.get('task'):
144
+ event['task'] = row['task']
145
+ rm.record([event], repo=row.get('repo'), where='event:worktree_removed')
146
+ rm.forge_locations.gone('worktree', row['path'], by=by)
147
+
148
+
149
+ def act(rm, row, action, actor):
150
+ """Do one planned action. Returns None on success, else the failure text."""
151
+ path = row['path']
152
+ repo = row.get('git_dir') or row.get('repo')
153
+ if action == 'delete-folder':
154
+ if worktrees._layout(path) is None:
155
+ return 'not inside a .forge-worktrees folder'
156
+ if not worktrees._leftover(path):
157
+ return 'it is a worktree again: not deleted'
158
+ if row.get('repo') and is_registered(row['repo'], path):
159
+ return 'git lists it as a worktree: not deleted'
160
+ try:
161
+ shutil.rmtree(path) # never follows a symlink: it refuses one
162
+ except OSError as error:
163
+ return '%s: %s' % (path, error.strerror or error)
164
+ record(rm, row, actor)
165
+ return None
166
+ code, _, err = rm.git(repo, 'worktree', 'remove', path)
167
+ if code != 0:
168
+ return first_line(err) or 'git worktree remove exit %d' % code
169
+ record(rm, row, actor)
170
+ if action == 'remove+branch':
171
+ code, _, err = rm.git(row.get('repo') or repo, 'branch', '-d', row['branch'])
172
+ if code != 0:
173
+ return 'worktree removed, but the branch stays: ' + (first_line(err) or 'git branch -d exit %d' % code)
174
+ return None
175
+
176
+
177
+ def run(args):
178
+ paths = []
179
+ for given in args.paths:
180
+ if real(given) not in {real(p) for p in paths}:
181
+ paths.append(given)
182
+ rows, error = {}, None
183
+ try:
184
+ computed = worktrees.compute(reader.Catalog(), history())
185
+ rows = {real(r['path']): r for r in computed['rows']}
186
+ if not computed['rows'] and any('could not be loaded' in n for n in computed.get('notes') or []):
187
+ error = first_line(computed['notes'][0])
188
+ except Exception as exc: # noqa: BLE001 -- reported per row, still one JSON object
189
+ error = 'could not compute the worktree states: %s' % exc
190
+ out = []
191
+ rm = None
192
+ for given in paths:
193
+ if error:
194
+ out.append(dict(path=given, action='none', status='failed', reason=error))
195
+ continue
196
+ row = rows.get(real(given))
197
+ action, why = plan(given, row, args.branches)
198
+ if action == 'none':
199
+ out.append(dict(path=given, action='none', status='skipped', reason=why))
200
+ elif args.dry_run:
201
+ out.append(dict(path=given, action=action, status='would-remove', reason=None))
202
+ else:
203
+ try:
204
+ rm = rm or load_runs_rm()
205
+ failure = act(rm, row, action, args.actor)
206
+ except Exception as exc: # noqa: BLE001 -- one row's failure never stops the others
207
+ failure = str(exc) or exc.__class__.__name__
208
+ out.append(dict(path=given, action=action, status='failed' if failure else 'removed', reason=failure))
209
+ return dict(ok=not any(r['status'] == 'failed' for r in out), dry_run=bool(args.dry_run), rows=out)
210
+
211
+
212
+ def show(result):
213
+ for r in result['rows']:
214
+ extra = '' if r['action'] == 'none' else ' (%s)' % r['action']
215
+ print('%-12s %s%s%s' % (r['status'], r['path'], extra, ': ' + r['reason'] if r['reason'] else ''))
216
+
217
+
218
+ def parser():
219
+ top = argparse.ArgumentParser(prog='forge worktrees', description='Worktrees forge created, by forge-ui\'s rules.')
220
+ sub = top.add_subparsers(dest='command', required=True, metavar='{rm}')
221
+ rm = sub.add_parser('rm', help='remove worktrees (a dirty, locked or live one is skipped, nothing is forced)',
222
+ description='Remove worktrees by the rules of forge-ui\'s Worktrees screen. Exit 0 when no row '
223
+ 'failed, 4 when one did, 2 for usage.')
224
+ rm.add_argument('paths', nargs='+', metavar='PATH')
225
+ rm.add_argument('--branches', action='store_true', help='also delete the merged branch of a finished worktree (git branch -d)')
226
+ rm.add_argument('--dry-run', action='store_true', help='say what would be removed, change nothing')
227
+ rm.add_argument('--json', action='store_true', help='print one JSON object: ok, dry_run, rows')
228
+ rm.add_argument('--actor', default='cli', choices=ACTORS, help='who asked (recorded with the removal; default cli)')
229
+ return top
230
+
231
+
232
+ def main(argv=None):
233
+ args = parser().parse_args(argv)
234
+ result = run(args)
235
+ if args.json:
236
+ print(json.dumps(result))
237
+ else:
238
+ show(result)
239
+ return OK if result['ok'] else FAILED
240
+
241
+
242
+ if __name__ == '__main__':
243
+ sys.exit(main())