@giamme/forge 0.15.0 → 0.17.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 (80) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/VERSION +1 -1
  3. package/docs/artifacts.md +1 -1
  4. package/docs/commands.md +14 -3
  5. package/docs/dataset.md +8 -0
  6. package/docs/forge-ui.md +14 -0
  7. package/docs/memory.md +22 -0
  8. package/docs/reviews.md +22 -1
  9. package/forge +11 -0
  10. package/package.json +1 -1
  11. package/references/dataset.md +129 -7
  12. package/references/decision-models.md +15 -2
  13. package/references/decompose.md +95 -13
  14. package/references/harnesses.md +4 -1
  15. package/references/planning.md +3 -2
  16. package/references/solo.md +5 -1
  17. package/references/ui.md +10 -1
  18. package/scripts/forge-artifact.sh +53 -0
  19. package/scripts/forge-contract.py +107 -9
  20. package/scripts/forge-control.py +0 -2
  21. package/scripts/forge-dispatch.sh +28 -12
  22. package/scripts/forge-fail-first.py +3 -1
  23. package/scripts/forge-guard.py +247 -8
  24. package/scripts/forge-issues.py +16 -3
  25. package/scripts/forge-ledger.py +3 -1
  26. package/scripts/forge-lib-adopt.sh +266 -0
  27. package/scripts/forge-lib-attribute.sh +10 -2
  28. package/scripts/forge-lib-plans.sh +16 -4
  29. package/scripts/forge-lib-review.sh +8 -2
  30. package/scripts/forge-lib-task.sh +19 -5
  31. package/scripts/forge-memory.sh +41 -4
  32. package/scripts/forge-metrics.sh +29 -0
  33. package/scripts/forge-parallel.sh +99 -14
  34. package/scripts/forge-plan-check.py +25 -2
  35. package/scripts/forge-planner.py +20 -6
  36. package/scripts/forge-route.py +16 -2
  37. package/scripts/forge-runs-rm.py +23 -7
  38. package/scripts/forge-runtime.py +25 -2
  39. package/scripts/forge-schedule.py +5 -2
  40. package/scripts/forge-shadow.py +30 -9
  41. package/scripts/forge-solo.sh +6 -0
  42. package/scripts/forge-sweep.py +25 -10
  43. package/scripts/forge_dataset/cli.py +99 -0
  44. package/scripts/forge_dataset/export.py +1 -0
  45. package/scripts/forge_dataset/facts.py +278 -0
  46. package/scripts/forge_dataset/harvest.py +27 -3
  47. package/scripts/forge_dataset/history.py +126 -0
  48. package/scripts/forge_dataset/outcomes.py +206 -58
  49. package/scripts/forge_dataset/pairs.py +174 -0
  50. package/scripts/forge_dataset/risk.py +108 -0
  51. package/scripts/forge_dataset/tiers.py +427 -0
  52. package/scripts/forge_dataset/worktrees.py +95 -0
  53. package/scripts/forge_decide/cli.py +23 -2
  54. package/scripts/forge_decide/labels.py +30 -6
  55. package/scripts/forge_decide/replay.py +10 -4
  56. package/scripts/forge_memory_list.py +153 -0
  57. package/scripts/forge_worktrees.py +88 -0
  58. package/ui/README.md +31 -0
  59. package/ui/docs/api.md +92 -6
  60. package/ui/forge_ui/__init__.py +14 -1
  61. package/ui/forge_ui/__main__.py +1 -1
  62. package/ui/forge_ui/control.py +77 -0
  63. package/ui/forge_ui/export.py +9 -2
  64. package/ui/forge_ui/findings.py +11 -2
  65. package/ui/forge_ui/projects.py +571 -0
  66. package/ui/forge_ui/reader.py +96 -9
  67. package/ui/forge_ui/server.py +8 -0
  68. package/ui/forge_ui/static/app.js +105 -22
  69. package/ui/forge_ui/static/cloudui.js +6 -2
  70. package/ui/forge_ui/static/convo.js +1 -1
  71. package/ui/forge_ui/static/diffview.js +9 -5
  72. package/ui/forge_ui/static/files.js +1 -1
  73. package/ui/forge_ui/static/index.html +5 -1
  74. package/ui/forge_ui/static/panel.js +69 -2
  75. package/ui/forge_ui/static/projectsview.js +372 -0
  76. package/ui/forge_ui/static/settings.js +136 -4
  77. package/ui/forge_ui/static/style.css +94 -3
  78. package/ui/forge_ui/static/uikit.js +94 -1
  79. package/ui/forge_ui/static/worktreesview.js +250 -0
  80. package/ui/forge_ui/worktrees.py +570 -0
@@ -0,0 +1,174 @@
1
+ """`forge dataset pairs`: what a second reviewer of the same attempt saw, per pair of reviewer settings (#121).
2
+
3
+ `--shadow-qa` reviews a sampled attempt twice: the primary that decides, and a shadow that nothing reads.
4
+ `forge dataset status` says how often the two agree per model pair. This report splits the pairs by
5
+ what differs between the two reviewers, so an effort comparison is never mixed with a model one:
6
+
7
+ effort the same harness and model at another effort or harness (`sol:medium` beside `sol:high`)
8
+ model another model of the same family
9
+ family another model family
10
+ control identical harness, model and effort: two runs of one reviewer, so their disagreement is
11
+ noise. A control is never an effort comparison; it is the baseline the others are read
12
+ against.
13
+
14
+ For every pair of settings, the two sides are ordered (lower effort, then model, first) and it gives:
15
+ - pairs, verdict agreement and its Wilson 95% interval; `enough` from MIN_PAIRS pairs, the bar
16
+ `forge dataset routing` uses for its randomized arms
17
+ - blocking findings (CONFIRMED, at the attempt's threshold or worse, or unlabelled) found by only
18
+ one side: the other side named nothing within LINE_WINDOW lines of it in the same file. A finding
19
+ both name at different severities is a severity disagreement, not a unique find
20
+ - the medians, over pairs, of b's tokens and time as a multiple of a's
21
+
22
+ "Found more" is not "better": whether a unique find was real is judged afterwards (outcomes, hidden
23
+ tests). Advisory only: nothing that routes or gates reads this report.
24
+ """
25
+ from __future__ import annotations
26
+
27
+ import importlib.util
28
+ import json
29
+ import os
30
+ import statistics
31
+ from collections import defaultdict
32
+ from pathlib import Path
33
+
34
+ from . import connect, dataset_dir
35
+ from .export import Runs, Store, shadow_pairs
36
+ from .facts import LINE_WINDOW, _matched, _threshold
37
+ from .routing import wilson
38
+
39
+ MIN_PAIRS = 30
40
+ KINDS = ('effort', 'model', 'family', 'control')
41
+ EFFORT_RANK = dict(minimal=0, low=1, medium=2, high=3, xhigh=4, max=5)
42
+ _SHADOW = None
43
+
44
+
45
+ def family(model: str) -> str:
46
+ """forge-shadow.py's model family, the one its pick prefers to differ from."""
47
+ global _SHADOW
48
+ if _SHADOW is None:
49
+ spec = importlib.util.spec_from_file_location('forge_shadow', Path(__file__).resolve().parents[1] / 'forge-shadow.py')
50
+ _SHADOW = importlib.util.module_from_spec(spec)
51
+ spec.loader.exec_module(_SHADOW)
52
+ return _SHADOW.family(model)
53
+
54
+
55
+ def settings(d) -> tuple:
56
+ return d.get('harness'), d.get('model'), d.get('effort')
57
+
58
+
59
+ def kind(a: tuple, b: tuple) -> str:
60
+ if a == b:
61
+ return 'control'
62
+ if a[1] == b[1]:
63
+ return 'effort'
64
+ return 'model' if family(a[1] or '') == family(b[1] or '') else 'family'
65
+
66
+
67
+ def label(s: tuple) -> str:
68
+ harness, model, effort = s
69
+ return '%s%s%s' % (model or '?', ':' + effort if effort else '', ' (%s)' % harness if harness else '')
70
+
71
+
72
+ def _order(s: tuple):
73
+ return (EFFORT_RANK.get(s[2] or '', -1), s[1] or '', s[0] or '', s[2] or '')
74
+
75
+
76
+ def _blocking(f, rank) -> bool:
77
+ return f.get('status') == 'CONFIRMED' and (f.get('severity') is None or f['severity'] <= rank)
78
+
79
+
80
+ def _unique(mine, theirs, rank) -> int:
81
+ """Blocking findings of `mine` with nothing of `theirs` near them (unlocated ones always count)."""
82
+ matched = {id(a) for a, _ in _matched(mine, theirs)}
83
+ return sum(1 for f in mine if _blocking(f, rank) and id(f) not in matched)
84
+
85
+
86
+ def _tokens(d):
87
+ t = d.get('tokens') or {}
88
+ values = [t.get('input'), t.get('output')]
89
+ return sum(v for v in values if isinstance(v, (int, float))) if any(isinstance(v, (int, float)) for v in values) else None
90
+
91
+
92
+ def _time(d):
93
+ for key in ('elapsed_s', 'duration_s'):
94
+ value = d.get(key)
95
+ if isinstance(value, (int, float)) and value > 0:
96
+ return float(value)
97
+ return None
98
+
99
+
100
+ def _ratio(xs):
101
+ return round(statistics.median(xs), 2) if xs else None
102
+
103
+
104
+ def report(runs: Runs, repo: str = None) -> dict:
105
+ paths = runs.store.repo_paths
106
+ rows = defaultdict(lambda: dict(pairs=0, agree=0, only_a=0, only_b=0, tokens=[], time=[]))
107
+ for (run, task, n), (primary, shadow) in shadow_pairs(runs).items():
108
+ if repo and paths.get(primary['repo']) != repo:
109
+ continue
110
+ sides = [primary['d'], shadow['d']]
111
+ sides.sort(key=lambda d: _order(settings(d)))
112
+ a, b = sides
113
+ row = rows[(kind(settings(a), settings(b)), settings(a), settings(b))]
114
+ rank = _threshold(runs, run, task, n)
115
+ found_a = [f for f in a.get('findings') or [] if isinstance(f, dict)]
116
+ found_b = [f for f in b.get('findings') or [] if isinstance(f, dict)]
117
+ row['pairs'] += 1
118
+ row['agree'] += a.get('verdict') == b.get('verdict')
119
+ row['only_a'] += _unique(found_a, found_b, rank)
120
+ row['only_b'] += _unique(found_b, found_a, rank)
121
+ for key, measure in (('tokens', _tokens), ('time', _time)):
122
+ x, y = measure(a), measure(b)
123
+ if x and y:
124
+ row[key].append(y / x)
125
+ out = []
126
+ for (k, a, b), row in rows.items():
127
+ lo, hi = wilson(row['agree'], row['pairs'])
128
+ out.append(dict(kind=k, a=label(a), b=label(b), pairs=row['pairs'], agree=row['agree'],
129
+ agreement=round(row['agree'] / row['pairs'], 3), ci=[round(lo, 3), round(hi, 3)],
130
+ enough=row['pairs'] >= MIN_PAIRS, only_a=row['only_a'], only_b=row['only_b'],
131
+ tokens_ratio=_ratio(row['tokens']), time_ratio=_ratio(row['time'])))
132
+ out.sort(key=lambda r: (KINDS.index(r['kind']), -r['pairs'], r['a'], r['b']))
133
+ return dict(min_pairs=MIN_PAIRS, line_window=LINE_WINDOW, pairs=sum(r['pairs'] for r in out),
134
+ by_kind={k: sum(r['pairs'] for r in out if r['kind'] == k) for k in KINDS}, comparisons=out)
135
+
136
+
137
+ def load(repo: str = None) -> dict:
138
+ # Reading must not create a store: a fresh machine gets an empty report.
139
+ if not (dataset_dir() / 'dataset.sqlite').exists():
140
+ return dict(min_pairs=MIN_PAIRS, line_window=LINE_WINDOW, pairs=0, by_kind={k: 0 for k in KINDS}, comparisons=[])
141
+ db = connect()
142
+ try:
143
+ runs = Runs(Store(db))
144
+ finally:
145
+ db.close()
146
+ return report(runs, repo=repo)
147
+
148
+
149
+ HEADINGS = dict(effort='effort, within one model', model='another model, one family',
150
+ family='across families', control='controls: identical settings, so disagreement is noise')
151
+
152
+
153
+ def main(args) -> int:
154
+ rep = load(os.path.realpath(args.repo) if args.repo else None)
155
+ if args.json:
156
+ print(json.dumps(rep, indent=2, sort_keys=True))
157
+ return 0
158
+ print('pairs: %d usable shadow pairs (%s); advisory only' % (
159
+ rep['pairs'], ', '.join('%d %s' % (rep['by_kind'][k], k) for k in KINDS)))
160
+ if not rep['comparisons']:
161
+ print(' none yet: `plan --shadow-qa <spec>` reviews a sampled share of attempts twice')
162
+ for k in KINDS:
163
+ rows = [r for r in rep['comparisons'] if r['kind'] == k]
164
+ if not rows:
165
+ continue
166
+ print('%s:' % HEADINGS[k])
167
+ for r in rows:
168
+ sample = '' if r['enough'] else ' (under %d pairs)' % rep['min_pairs']
169
+ cost = ', b costs %sx tokens and %sx time' % (r['tokens_ratio'] if r['tokens_ratio'] is not None else '?',
170
+ r['time_ratio'] if r['time_ratio'] is not None else '?')
171
+ print(' a %s vs b %s: %d pairs, %d agree (%.0f%% [%.0f-%.0f%%]), blocking found only by a %d, only by b %d%s%s'
172
+ % (r['a'], r['b'], r['pairs'], r['agree'], 100 * r['agreement'], 100 * r['ci'][0], 100 * r['ci'][1],
173
+ r['only_a'], r['only_b'], cost, sample))
174
+ return 0
@@ -0,0 +1,108 @@
1
+ """`forge dataset risk`: a task's risk beside its difficulty (#122), measured in shadow.
2
+
3
+ Difficulty is what a task demands of the model; risk is what a mistake that gets through would cost
4
+ (what breaks, for whom, how hard it is to undo). The planner, or whoever writes the plan, records risk
5
+ in `tasks/<id>/risk`; nothing routes on it. This report says whether it should:
6
+
7
+ rated tasks with a risk, and how often it differs from their difficulty (the share that match
8
+ and Cohen's kappa: a rating that only echoes difficulty adds nothing, and the issue says
9
+ to stop there)
10
+ cells every (difficulty, risk) pair: tasks, first attempts that passed, tasks a primary review
11
+ found a CONFIRMED P0 or P1 in, and landed work that did not hold at the longest horizon
12
+ `forge dataset outcomes` followed (reverted, or under SURVIVES_AT of its lines kept).
13
+ Low difficulty with high risk is the case the question is about.
14
+
15
+ A cell under MIN_CELL tasks is flagged: outcomes take 14 days to come due and the interesting cell is
16
+ rare, so a handful of tasks is reported as such rather than read as an answer. Each task counts once,
17
+ as its run's latest plan revision rated it. Advisory only: nothing that routes or gates reads it.
18
+ """
19
+ from __future__ import annotations
20
+
21
+ import json
22
+ import os
23
+
24
+ from . import connect, dataset_dir
25
+ from .export import SURVIVES_AT, Runs, Store
26
+ from .facts import _kappa
27
+
28
+ LEVELS = ('low', 'medium', 'high')
29
+ MIN_CELL = 10
30
+
31
+
32
+ def _empty():
33
+ return dict(tasks=0, rated=0, same=0, kappa=None, min_cell=MIN_CELL, survives_at=SURVIVES_AT, cells=[])
34
+
35
+
36
+ def report(runs: Runs, repo: str = None) -> dict:
37
+ out = _empty()
38
+ paths = runs.store.repo_paths
39
+ cells = {}
40
+ pairs = []
41
+ for run, revisions in runs.plans.items():
42
+ plan = revisions[-1]
43
+ if repo and paths.get(plan['repo']) != repo:
44
+ continue
45
+ for task, row in (plan['d'].get('tasks') or {}).items():
46
+ out['tasks'] += 1
47
+ difficulty, risk = row.get('difficulty'), row.get('risk')
48
+ if difficulty not in LEVELS or risk not in LEVELS:
49
+ continue
50
+ out['rated'] += 1
51
+ out['same'] += difficulty == risk
52
+ pairs.append((difficulty, risk))
53
+ cell = cells.setdefault((difficulty, risk), dict(difficulty=difficulty, risk=risk, tasks=0, attempted=0,
54
+ first_pass=0, serious=0, landed=0, did_not_hold=0))
55
+ cell['tasks'] += 1
56
+ attempts = runs.attempts.get((run, task)) or []
57
+ if attempts:
58
+ cell['attempted'] += 1
59
+ cell['first_pass'] += attempts[0]['d'].get('status') == 'PASS'
60
+ reviews = runs.dispatches.get((run, task, 'qa')) or []
61
+ cell['serious'] += any(f.get('status') == 'CONFIRMED' and f.get('severity') in (0, 1)
62
+ for e in reviews for f in e['d'].get('findings') or [] if isinstance(f, dict))
63
+ landed = next((runs.survival[c] for c in (runs.reviewed.get((run, task, a['attempt'])) for a in attempts)
64
+ if c in runs.survival), None)
65
+ if landed:
66
+ cell['landed'] += 1
67
+ cell['did_not_hold'] += (landed['surviving'] / landed['added'] < SURVIVES_AT
68
+ or bool(landed.get('reverted_by')))
69
+ kappa = _kappa(pairs)
70
+ out['kappa'] = None if kappa is None else round(kappa, 3)
71
+ out['cells'] = [dict(cell, enough=cell['tasks'] >= MIN_CELL)
72
+ for _, cell in sorted(cells.items(), key=lambda kv: (LEVELS.index(kv[0][0]), LEVELS.index(kv[0][1])))]
73
+ return out
74
+
75
+
76
+ def load(repo: str = None) -> dict:
77
+ # Reading must not create a store: a fresh machine gets an empty report.
78
+ if not (dataset_dir() / 'dataset.sqlite').exists():
79
+ return _empty()
80
+ db = connect()
81
+ try:
82
+ runs = Runs(Store(db))
83
+ finally:
84
+ db.close()
85
+ return report(runs, repo=repo)
86
+
87
+
88
+ def _rate(k, n):
89
+ return '%d/%d' % (k, n) if n else '-'
90
+
91
+
92
+ def main(args) -> int:
93
+ rep = load(os.path.realpath(args.repo) if args.repo else None)
94
+ if args.json:
95
+ print(json.dumps(rep, indent=2, sort_keys=True))
96
+ return 0
97
+ print('risk: %d of %d tasks rated; advisory only, nothing routes on it' % (rep['rated'], rep['tasks']))
98
+ if not rep['rated']:
99
+ print(' none yet: the planner rates it, or write low, medium or high to tasks/<id>/risk before `plan`')
100
+ return 0
101
+ kappa = '' if rep['kappa'] is None else ', kappa %.2f' % rep['kappa']
102
+ print(' risk equals difficulty for %d of %d (%.0f%%%s)' % (rep['same'], rep['rated'], 100 * rep['same'] / rep['rated'], kappa))
103
+ print(' %-10s %-6s %5s %10s %8s %14s' % ('difficulty', 'risk', 'tasks', 'first pass', 'P0/P1', 'did not hold'))
104
+ for c in rep['cells']:
105
+ print(' %-10s %-6s %5d %10s %8s %14s%s' % (
106
+ c['difficulty'], c['risk'], c['tasks'], _rate(c['first_pass'], c['attempted']), _rate(c['serious'], c['tasks']),
107
+ _rate(c['did_not_hold'], c['landed']), '' if c['enough'] else ' (under %d tasks)' % rep['min_cell']))
108
+ return 0