@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
@@ -35,6 +35,7 @@ import time
35
35
  from forge_ui import reader
36
36
 
37
37
  RULES = Path(__file__).resolve().parents[2] / 'scripts' / 'forge_worktrees.py'
38
+ LOCATIONS = Path(__file__).resolve().parents[2] / 'scripts' / 'forge_locations.py'
38
39
  CACHE_S = 60
39
40
  _GIT_TIMEOUT = 10
40
41
  _SIZE_BUDGET_S = 120 # one tree's walk stops here and its size is a lower bound
@@ -67,22 +68,82 @@ def rules():
67
68
  return _STATE['rules']
68
69
 
69
70
 
71
+ _LAST_ERROR = threading.local()
72
+
73
+
74
+ def locations():
75
+ """forge's location list module (scripts/forge_locations.py), loaded read-only, or None. Only its `load` is used:
76
+ forge-ui never writes the list."""
77
+ with _LOCK:
78
+ if 'locations' not in _STATE:
79
+ try:
80
+ spec = importlib.util.spec_from_file_location(
81
+ 'forge_locations_for_ui', LOCATIONS, loader=_ReadOnlyLoader('forge_locations_for_ui', str(LOCATIONS)))
82
+ module = importlib.util.module_from_spec(spec)
83
+ spec.loader.exec_module(module)
84
+ if not callable(module.load):
85
+ raise TypeError('not the forge location module')
86
+ _STATE['locations'] = module
87
+ except Exception:
88
+ _STATE['locations'] = None
89
+ return _STATE['locations']
90
+
91
+
92
+ def _located():
93
+ """The location list's worktree entries, [{path, checkout, root, run_id, task, branch, wt_kind, created, gone}]."""
94
+ module = locations()
95
+ if module is None:
96
+ return []
97
+ try:
98
+ return [e for e in module.load().values() if e.get('kind') == 'worktree']
99
+ except Exception:
100
+ return []
101
+
102
+
70
103
  def _git(cwd, *args, timeout=_GIT_TIMEOUT):
71
- """(returncode, stdout text) of `git -C cwd <args>`, or (None, '') when git could not run."""
104
+ """(returncode, stdout text) of `git -C cwd <args>`, or (None, '') when git could not run. The first line of
105
+ what git said on stderr is kept in _LAST_ERROR.text, for a note when a listing fails."""
106
+ _LAST_ERROR.text = ''
72
107
  try:
73
108
  done = subprocess.run(['git', '--no-optional-locks', '-C', cwd, *args], stdin=subprocess.DEVNULL,
74
- stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, timeout=timeout, check=False,
109
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout, check=False,
75
110
  env=dict(os.environ, GIT_TERMINAL_PROMPT='0', LC_ALL='C'))
76
- except (OSError, ValueError, subprocess.SubprocessError):
111
+ except subprocess.TimeoutExpired:
112
+ _LAST_ERROR.text = 'git took over %d s' % timeout
113
+ return None, ''
114
+ except (OSError, ValueError, subprocess.SubprocessError) as exc:
115
+ _LAST_ERROR.text = str(exc)
77
116
  return None, ''
117
+ if done.returncode:
118
+ _LAST_ERROR.text = done.stderr.decode('utf-8', 'replace').strip().split('\n')[0][:300]
78
119
  return done.returncode, done.stdout.decode('utf-8', 'surrogateescape')
79
120
 
80
121
 
81
122
  def _isdir(path):
123
+ """An absolute path to a directory, through symlinks: a repository is often reached through one."""
124
+ try:
125
+ return os.path.isabs(path) and os.path.isdir(path)
126
+ except (OSError, ValueError):
127
+ return False
128
+
129
+
130
+ def _leftover(path):
131
+ """True when the folder is no git worktree any more: no `.git`, or a `.git` file whose gitdir is gone."""
132
+ dotgit = os.path.join(path, '.git')
82
133
  try:
83
- return os.path.isabs(path) and stat.S_ISDIR(os.lstat(path).st_mode)
134
+ if not os.path.lexists(dotgit):
135
+ return True
136
+ if os.path.isdir(dotgit):
137
+ return False
138
+ with open(dotgit, 'r', encoding='utf-8', errors='replace') as stream:
139
+ line = stream.read(4096).strip()
84
140
  except (OSError, ValueError):
85
141
  return False
142
+ if not line.startswith('gitdir:'):
143
+ return False
144
+ target = line[len('gitdir:'):].strip()
145
+ target = target if os.path.isabs(target) else os.path.join(path, target)
146
+ return not os.path.exists(target)
86
147
 
87
148
 
88
149
  def _exists(path):
@@ -110,6 +171,9 @@ def commands(row):
110
171
  return [dict(label='see what is uncommitted', command='git -C %s status' % quote(path))]
111
172
  if st == 'missing' and repo:
112
173
  return [dict(label='forget the missing folder', command='git -C %s worktree prune' % quote(repo))]
174
+ if st == 'unknown' and row.get('leftover'):
175
+ return [dict(label='delete the leftover folder: git no longer knows it as a worktree (look inside first)',
176
+ command='rm -rf -- %s' % quote(path))]
113
177
  if st in ('failed', 'finished') and repo:
114
178
  out = [dict(label='remove the worktree', command='git -C %s worktree remove %s' % (quote(repo), quote(path)))]
115
179
  if st == 'finished' and branch and row.get('merged'):
@@ -133,7 +197,9 @@ def _note(row):
133
197
  if st == 'foreign':
134
198
  return row.get('why') or "not forge's: it fails forge's ownership rule"
135
199
  if st == 'unknown':
136
- return 'a folder git does not list as a worktree'
200
+ if row.get('leftover'):
201
+ return 'a folder left where a worktree was: git no longer knows it, and nothing of forge reads it'
202
+ return row.get('why') or 'a folder git does not list as a worktree'
137
203
  if st == 'removed':
138
204
  if row.get('derived'):
139
205
  return 'derived from the run dir or the dataset; removed at an unknown date'
@@ -168,19 +234,35 @@ def _runs(catalog):
168
234
 
169
235
  # ---- the computation ---------------------------------------------------------------------------
170
236
 
171
- def _repo_listing(R, repo):
172
- """(main working tree, [worktree records]) of the repository holding `repo`, or (None, [])."""
237
+ def _repo_listing(R, repo, failures=None):
238
+ """(main working tree, [worktree records]) of the repository holding `repo`, or (None, []). A failure is added
239
+ to `failures` as {repo: why}."""
173
240
  code, out = _git(repo, 'worktree', 'list', '--porcelain', '-z')
174
241
  z = True
175
242
  if code == 129:
176
243
  code, out = _git(repo, 'worktree', 'list', '--porcelain')
177
244
  z = False
178
245
  if code != 0:
246
+ if failures is not None:
247
+ failures[repo] = _LAST_ERROR.text or ('exit %s' % code if code is not None else 'git could not run')
179
248
  return None, []
180
249
  found = R.parse_porcelain(out, z=z)
181
250
  return (found[0]['path'], found) if found else (None, [])
182
251
 
183
252
 
253
+ def _forge_folders(folders):
254
+ """The worktree folders on disk under each `.forge-worktrees` folder: `<folder>/<run id>/<name>` and
255
+ `<folder>/<name>` that hold a `.git` whose repository is still there (a leftover is no repository to ask)."""
256
+ out = []
257
+ for folder in sorted(folders):
258
+ for first in _children(folder):
259
+ if _exists(os.path.join(first, '.git')):
260
+ out += [] if _leftover(first) else [first]
261
+ continue
262
+ out += [second for second in _children(first) if _exists(os.path.join(second, '.git')) and not _leftover(second)]
263
+ return out
264
+
265
+
184
266
  def _layout(path):
185
267
  """(the `.forge-worktrees` folder, run id, name) when `path` is `<x>/.forge-worktrees/<run id>/<name>`,
186
268
  (folder, None, name) when it sits directly in that folder, else None."""
@@ -212,6 +294,11 @@ def compute(catalog, history):
212
294
  if isinstance(a, dict) and a.get('repo_path') and a.get('run_id') and a.get('task'):
213
295
  dataset_tasks.setdefault((R.wt_root(a['repo_path'], a['run_id']), a['repo_path'], a['run_id']), set()).add(a['task'])
214
296
  events = [e for e in (hist or {}).get('events') or [] if isinstance(e, dict) and isinstance(e.get('path'), str)]
297
+ located = _located()
298
+ located_tasks = {} # real wt_root -> task ids the location list names
299
+ for e in located:
300
+ if e.get('task'):
301
+ located_tasks.setdefault(os.path.dirname(e['path']), set()).add(e['task'])
215
302
 
216
303
  # every repository to ask: run checkouts and roots, then the dataset's
217
304
  candidates = []
@@ -223,11 +310,31 @@ def compute(catalog, history):
223
310
  for e in events:
224
311
  if e.get('repo_path'):
225
312
  candidates.append(e['repo_path'])
226
- roots, listed, asked = {}, {}, set()
313
+ # The repositories forge's location list names: each one's main working tree, then the checkout a run used.
314
+ for e in located:
315
+ candidates += [p for p in (e.get('root'), e.get('checkout')) if isinstance(p, str)]
316
+ # Then every worktree folder on disk, asked through its own .git: a checkout the dataset names may have
317
+ # been deleted by hand, and git answers for the whole repository from any of its worktrees.
318
+ known_folders = set()
319
+ for repo in candidates:
320
+ if repo and os.path.isabs(repo):
321
+ known_folders.add(os.path.join(os.path.dirname(_real(repo)), '.forge-worktrees'))
322
+ for e in events:
323
+ where = _layout(e['path']) if os.path.isabs(e['path']) else None
324
+ if where:
325
+ known_folders.add(where[0])
326
+ for (wroot, _repo, _rid) in dataset_tasks:
327
+ known_folders.add(os.path.dirname(wroot))
328
+ for e in located:
329
+ where = _layout(e['path'])
330
+ if where:
331
+ known_folders.add(where[0])
332
+ candidates += _forge_folders(known_folders)
333
+ roots, listed, asked, failures = {}, {}, set(), {}
227
334
  for repo in candidates:
228
335
  if not repo or not _isdir(repo) or _real(repo) in asked:
229
336
  continue
230
- root, found = _repo_listing(R, repo)
337
+ root, found = _repo_listing(R, repo, failures)
231
338
  for wt in found:
232
339
  asked.add(_real(wt['path']))
233
340
  asked.add(_real(repo))
@@ -242,8 +349,8 @@ def compute(catalog, history):
242
349
  def tasks_of(root_key, repo, run_id):
243
350
  run = by_root.get(root_key)
244
351
  if run is not None:
245
- return set(run['tasks']), run
246
- out = set()
352
+ return set(run['tasks']) | located_tasks.get(root_key, set()), run
353
+ out = set(located_tasks.get(root_key, set()))
247
354
  for (wroot, _repo, _rid), ids in dataset_tasks.items():
248
355
  if wroot == root_key:
249
356
  out |= ids
@@ -275,6 +382,9 @@ def compute(catalog, history):
275
382
  else:
276
383
  wroot = os.path.join(folder, run_id)
277
384
  ids, run = tasks_of(wroot, root, run_id)
385
+ if wt['branch'] == 'refs/heads/forge/%s/%s' % (run_id, name):
386
+ # forge names a task's worktree and branch after it: a task split or dropped since is still the run's
387
+ ids = set(ids) | {name}
278
388
  kind = R.owner(wt['path'], wt['branch'], run_id, ids, wroot)
279
389
  if run is not None:
280
390
  row.update(run=run['key'], run_path=run['path'])
@@ -336,6 +446,7 @@ def compute(catalog, history):
336
446
  folders.add(where[0])
337
447
  for run in runs:
338
448
  folders.add(os.path.join(os.path.dirname(_real(run['repo'])), '.forge-worktrees'))
449
+ folders |= {_real(f) for f in known_folders if _isdir(f)}
339
450
  for folder in sorted(folders):
340
451
  for first in _children(folder):
341
452
  if _real(first) in listed:
@@ -365,6 +476,13 @@ def compute(catalog, history):
365
476
  rows[real]['created'] = created['ts']
366
477
  continue
367
478
  e = created or removed
479
+ if os.path.isabs(e['path']) and _exists(e['path']):
480
+ # on disk, yet no repository listed it: never call it removed
481
+ _unknown(rows, e['path'], e.get('run_id'), by_root)
482
+ rows[real].update(repo=rows[real]['repo'] or e.get('repo_path'), task=rows[real]['task'] or e.get('task'),
483
+ branch=e.get('branch'), created=created['ts'] if created else None,
484
+ why='on disk, but git did not list it as a worktree of %s' % (e.get('repo_path') or 'its repository'))
485
+ continue
368
486
  root_key = R.wt_root(e['repo_path'], e['run_id']) if e.get('repo_path') and e.get('run_id') else None
369
487
  run = by_root.get(root_key) if root_key else None
370
488
  rows[real] = dict(path=e['path'], repo=e.get('repo_path'), git_dir=None, run=run['key'] if run else None,
@@ -372,6 +490,25 @@ def compute(catalog, history):
372
490
  branch=e.get('branch'), kind=e.get('kind'), status='removed', merged=None, head=None,
373
491
  commit=None, created=created['ts'] if created else None,
374
492
  removed=removed['ts'] if removed else None, derived=False, locked=None, size=None, why=None)
493
+ # what forge's location list recorded: its dates, and the worktrees it added that are not on disk any more
494
+ for e in located:
495
+ real = e['path']
496
+ if real in rows:
497
+ if rows[real]['created'] is None:
498
+ rows[real]['created'] = e.get('created')
499
+ continue
500
+ root_key = os.path.dirname(real)
501
+ run = by_root.get(root_key)
502
+ repo = e.get('root') or e.get('checkout')
503
+ if _exists(real):
504
+ _unknown(rows, real, e.get('run_id'), by_root)
505
+ rows[real].update(repo=rows[real]['repo'] or repo, branch=e.get('branch'), created=e.get('created'),
506
+ why='on disk, but git did not list it as a worktree of %s' % (repo or 'its repository'))
507
+ continue
508
+ rows[real] = dict(path=real, repo=repo, git_dir=None, run=run['key'] if run else None, run_id=e.get('run_id'),
509
+ run_path=run['path'] if run else None, task=e.get('task'), branch=e.get('branch'),
510
+ kind=e.get('wt_kind'), status='removed', merged=None, head=None, commit=None,
511
+ created=e.get('created'), removed=e.get('gone'), derived=False, locked=None, size=None, why=None)
375
512
  for run in runs:
376
513
  wroot = R.wt_root(run['repo'], run['run_id'])
377
514
  names = [t for t, s in run['tasks'].items() if s not in ('PENDING', 'BLOCKED', None)]
@@ -399,7 +536,11 @@ def compute(catalog, history):
399
536
  if not run['live'] and _isdir(run['path']):
400
537
  group['command'] = 'forge runs rm %s --worktrees --branches --dry-run' % quote(run['path'])
401
538
  run_groups.append(group)
402
- notes = []
539
+ # A path that is no repository any more (a checkout deleted and its folder reused) has nothing to list; a real
540
+ # failure (a timeout, git's safe.directory refusal) is named, since it hides that repository's worktrees.
541
+ notes = ['git could not list the worktrees of %s (%s): its worktrees are shown from what is on disk and the dataset'
542
+ % (repo, why) for repo, why in sorted(failures.items())
543
+ if _real(repo) not in roots and 'not a git repository' not in why]
403
544
  if hist is None:
404
545
  notes.append('the dataset could not be read: only what git and the run dirs say is listed')
405
546
  elif not hist.get('enabled'):
@@ -451,7 +592,7 @@ def _unknown(rows, path, run_id, by_root):
451
592
  rows[real] = dict(path=path, repo=run['root'] if run else None, git_dir=None, run=run['key'] if run else None,
452
593
  run_id=run_id, run_path=run['path'] if run else None, task=name if kind == 'task' else None,
453
594
  branch=None, kind=kind, status='unknown', merged=None, head=None, commit=None, created=None,
454
- removed=None, derived=False, locked=None, size=None, why=None)
595
+ removed=None, derived=False, locked=None, size=None, why=None, leftover=_leftover(path))
455
596
 
456
597
 
457
598
  def _derived(rows, path, repo, run_id, name, run):