@giamme/forge 0.12.1 → 0.13.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 (107) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +1 -1
  3. package/SKILL.md +29 -12
  4. package/VERSION +1 -1
  5. package/docs/add-ons.md +11 -6
  6. package/docs/artifacts.md +12 -6
  7. package/docs/commands.md +179 -17
  8. package/docs/decomposed-runs.md +13 -5
  9. package/docs/forge-ui.md +7 -3
  10. package/docs/memory.md +2 -1
  11. package/docs/models.md +4 -2
  12. package/docs/reviews.md +49 -20
  13. package/docs/troubleshooting.md +3 -1
  14. package/forge +17 -1
  15. package/package.json +1 -1
  16. package/references/dataset.md +39 -13
  17. package/references/decision-models.md +759 -0
  18. package/references/decompose.md +166 -29
  19. package/references/harnesses.md +10 -3
  20. package/references/planning.md +8 -0
  21. package/references/solo.md +13 -3
  22. package/scripts/forge-artifact.sh +30 -4
  23. package/scripts/forge-config.py +238 -29
  24. package/scripts/forge-contract.py +95 -56
  25. package/scripts/forge-control.py +40 -9
  26. package/scripts/forge-decide.py +6 -0
  27. package/scripts/forge-dispatch.sh +38 -16
  28. package/scripts/forge-fail-first.py +2 -2
  29. package/scripts/forge-gate-addressed.sh +40 -0
  30. package/scripts/forge-gate-message.sh +64 -0
  31. package/scripts/forge-issues.py +321 -33
  32. package/scripts/forge-jev-options.sh +269 -42
  33. package/scripts/forge-lib-attribute.sh +447 -0
  34. package/scripts/forge-lib-gates.sh +32 -0
  35. package/scripts/forge-lib-plans.sh +17 -6
  36. package/scripts/forge-lib-review.sh +22 -17
  37. package/scripts/forge-lib-task.sh +217 -38
  38. package/scripts/forge-lib-verify.sh +45 -8
  39. package/scripts/forge-memory.sh +3 -3
  40. package/scripts/forge-parallel-options.sh +18 -5
  41. package/scripts/forge-parallel.sh +455 -303
  42. package/scripts/forge-plan-check.py +35 -1
  43. package/scripts/forge-pre-review.py +437 -0
  44. package/scripts/forge-prompt.py +25 -5
  45. package/scripts/forge-route.py +2 -2
  46. package/scripts/forge-runtime.py +10 -4
  47. package/scripts/forge-schedule.py +21 -5
  48. package/scripts/forge-shadow.py +2 -2
  49. package/scripts/forge-solo.sh +38 -9
  50. package/scripts/forge-submodules.py +389 -0
  51. package/scripts/forge_dataset/cli.py +12 -6
  52. package/scripts/forge_dataset/export.py +55 -45
  53. package/scripts/forge_dataset/harvest.py +33 -5
  54. package/scripts/{forge_jev/outcome.py → forge_dataset/states.py} +21 -7
  55. package/scripts/forge_decide/__init__.py +141 -0
  56. package/scripts/forge_decide/budget.py +149 -0
  57. package/scripts/forge_decide/cache.py +66 -0
  58. package/scripts/forge_decide/cli.py +953 -0
  59. package/scripts/forge_decide/doctor.py +221 -0
  60. package/scripts/forge_decide/label_addressed.py +75 -0
  61. package/scripts/forge_decide/label_attribution.py +62 -0
  62. package/scripts/forge_decide/label_coverage.py +81 -0
  63. package/scripts/forge_decide/label_message.py +38 -0
  64. package/scripts/forge_decide/label_prompt.py +139 -0
  65. package/scripts/forge_decide/labels.py +328 -0
  66. package/scripts/forge_decide/metrics.py +125 -0
  67. package/scripts/forge_decide/migrate.py +226 -0
  68. package/scripts/forge_decide/providers.py +292 -0
  69. package/scripts/forge_decide/replay.py +350 -0
  70. package/scripts/forge_decide/report.py +382 -0
  71. package/scripts/forge_decide/resolve.py +397 -0
  72. package/scripts/forge_decide/sites.py +377 -0
  73. package/scripts/forge_jev/__init__.py +202 -104
  74. package/scripts/forge_jev/addressed.py +171 -0
  75. package/scripts/forge_jev/attribution.py +198 -0
  76. package/scripts/forge_jev/cli.py +460 -382
  77. package/scripts/forge_jev/client.py +155 -77
  78. package/scripts/forge_jev/coupling.py +19 -87
  79. package/scripts/forge_jev/coverage.py +303 -0
  80. package/scripts/forge_jev/findings.py +219 -0
  81. package/scripts/forge_jev/gates.py +225 -27
  82. package/scripts/forge_jev/memory.py +65 -4
  83. package/scripts/forge_jev/message.py +134 -0
  84. package/scripts/forge_jev/plan_gates.py +294 -0
  85. package/scripts/forge_jev/questions.py +173 -313
  86. package/scripts/forge_jev/verify.py +130 -104
  87. package/ui/README.md +2 -2
  88. package/ui/docs/api.md +60 -32
  89. package/ui/forge_ui/control.py +60 -11
  90. package/ui/forge_ui/export.py +2 -1
  91. package/ui/forge_ui/models.py +1 -0
  92. package/ui/forge_ui/reader.py +87 -14
  93. package/ui/forge_ui/static/app.js +2 -2
  94. package/ui/forge_ui/static/controls.js +20 -13
  95. package/ui/forge_ui/static/panel.js +63 -17
  96. package/ui/forge_ui/static/runmodel.js +23 -1
  97. package/ui/forge_ui/static/scene3d.js +2 -15
  98. package/ui/forge_ui/static/settings.js +336 -41
  99. package/ui/forge_ui/static/style.css +19 -7
  100. package/references/jev.md +0 -865
  101. package/scripts/forge_jev/backtest.py +0 -322
  102. package/scripts/forge_jev/corpus.py +0 -156
  103. package/scripts/forge_jev/features.py +0 -24
  104. package/scripts/forge_jev/predict.py +0 -204
  105. package/scripts/forge_jev/review.py +0 -73
  106. package/scripts/forge_jev/routing.py +0 -271
  107. package/scripts/forge_jev/subset.py +0 -123
@@ -0,0 +1,198 @@
1
+ """Integration attribution (#45): which merged task most likely broke the combined verification.
2
+
3
+ `forge-parallel.sh attribute` searches the merge order for a merge that breaks the run's
4
+ verification, one full verification per step. This asks one Choice, `culprit`, over every merged
5
+ task id plus `none`, given the end of the failing log and each merge's diff, and prints the
6
+ ranking: `<id>TAB<p>` per line, highest first, `none` included. The shell decides what to do with
7
+ it by the gate's mode (`on` probes the top suspect's boundary first; `shadow` only records it).
8
+ The gate never reaches a verdict by itself: a wrong guess only costs the shell one more step.
9
+
10
+ forge-jev.py attribution --plan P --repo R --log L
11
+
12
+ Exit 3 is no opinion: the gate is off, the log is missing, fewer than two merges, or the model
13
+ did not answer.
14
+ """
15
+ from __future__ import annotations
16
+
17
+ import re
18
+ import subprocess
19
+ from pathlib import Path
20
+
21
+ from . import site_ready
22
+ from .client import ask, probabilities
23
+ from .questions import culprit
24
+
25
+ SITE = 'jev-attribution'
26
+ LOG_LINES = 150
27
+ DIFF_CHARS = 3000
28
+ # The subject forge-lib-attribute.sh keeps: `<sha> forge: merge <id>` (an awk match on `%H %s`).
29
+ _MERGE = re.compile(r'([0-9a-f]+) forge: merge ([^ ]+) *')
30
+
31
+
32
+ def _read(path: Path) -> str:
33
+ try:
34
+ return path.read_text(errors='replace')
35
+ except OSError:
36
+ return ''
37
+
38
+
39
+ def _git(repo, *args) -> str | None:
40
+ try:
41
+ done = subprocess.run(['git', '-C', str(repo), *args], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
42
+ text=True, errors='replace', timeout=60)
43
+ except (OSError, subprocess.SubprocessError):
44
+ return None
45
+ return done.stdout if done.returncode == 0 else None
46
+
47
+
48
+ def merges(plan, repo) -> list[tuple[str, str]]:
49
+ """[(commit, task id)], oldest first: the integration branch's first-parent `forge: merge <id>`
50
+ commits after its base, as forge-lib-attribute.sh reads them. [] when the base or the branch
51
+ is missing."""
52
+ plan = Path(plan)
53
+ run_id = _read(plan / 'run_id').strip()
54
+ base = _read(plan / 'integration.base').strip()
55
+ if not run_id or not base:
56
+ return []
57
+ base_commit = (_git(repo, 'rev-parse', base + '^{commit}') or '').strip()
58
+ tip = (_git(repo, 'rev-parse', f'forge/{run_id}-integration^{{commit}}') or '').strip()
59
+ if not base_commit or not tip:
60
+ return []
61
+ listing = _git(repo, 'log', '--first-parent', '--reverse', '--format=%H %s', f'{base_commit}..{tip}') or ''
62
+ out = []
63
+ for line in listing.splitlines():
64
+ found = _MERGE.fullmatch(line)
65
+ if found:
66
+ out.append((found.group(1), found.group(2)))
67
+ return out
68
+
69
+
70
+ def _tasks_tsv(plan: Path) -> dict[str, tuple[str, list[str]]]:
71
+ """id -> (title, files): tasks.tsv columns 7 and 4."""
72
+ out = {}
73
+ for line in _read(plan / 'tasks.tsv').splitlines():
74
+ cells = line.split('\t')
75
+ if len(cells) < 7 or line.startswith('#'):
76
+ continue
77
+ files = [name.strip() for name in cells[3].split(',') if name.strip() and name.strip() != '-']
78
+ out[cells[0]] = (cells[6], files)
79
+ return out
80
+
81
+
82
+ def _diff(repo, commit: str) -> str:
83
+ text = _git(repo, 'diff', f'{commit}^1', commit) or ''
84
+ return text[:DIFF_CHARS] + ('\n[cut]' if len(text) > DIFF_CHARS else '')
85
+
86
+
87
+ def _tail(path: Path) -> str:
88
+ return '\n'.join(_read(path).splitlines()[-LOG_LINES:])
89
+
90
+
91
+ def state_of(plan, repo, log) -> dict | None:
92
+ """The request's state, or None with fewer than two merges."""
93
+ plan = Path(plan)
94
+ found = merges(plan, repo)
95
+ if len(found) < 2:
96
+ return None
97
+ listed = _tasks_tsv(plan)
98
+ tasks = []
99
+ for commit, task in found:
100
+ title, files = listed.get(task, ('', []))
101
+ tasks.append(dict(id=task, title=title, files=files, diff=_diff(repo, commit)))
102
+ return dict(run=_read(plan / 'run_id').strip() or plan.name, log=_tail(Path(log)), tasks=tasks)
103
+
104
+
105
+ def ranking(probs: dict, order: list) -> list[tuple[str, float]]:
106
+ """(option, p), highest first; equal probabilities keep the order the options were asked in."""
107
+ place = {option: i for i, option in enumerate(order)}
108
+ return sorted(probs.items(), key=lambda item: (-item[1], place.get(item[0], len(place))))
109
+
110
+
111
+ def main(args) -> int:
112
+ if not site_ready(SITE):
113
+ return 3
114
+ plan, log = Path(args.plan), Path(args.log)
115
+ if not log.is_file():
116
+ return 3
117
+ state = state_of(plan, args.repo, log)
118
+ if state is None:
119
+ return 3
120
+ ids = [task['id'] for task in state['tasks']]
121
+ result = ask(state, {'culprit': culprit(ids)}, site=SITE, run_dir=str(plan))
122
+ order = ranking(probabilities(result, 'culprit'), ids + ['none'])
123
+ if not order:
124
+ return 3
125
+ print('\n'.join(f'{option}\t{p:.3f}' for option, p in order))
126
+ return 0
127
+
128
+
129
+ # --- measurement: what forge_decide.sites reads for this site --------------------------------
130
+
131
+ PREFIX = 'culprit:'
132
+
133
+
134
+ def _options(body) -> list[str]:
135
+ questions = body.get('questions') if isinstance(body, dict) else None
136
+ question = questions.get('culprit') if isinstance(questions, dict) else None
137
+ criteria = question.get('criteria') if isinstance(question, dict) else None
138
+ return [str(option) for option in criteria] if isinstance(criteria, dict) else []
139
+
140
+
141
+ def decisions(body) -> list[str]:
142
+ """`culprit:<id>` per option of the request except `none`, in the order asked."""
143
+ return [PREFIX + option for option in _options(body) if option != 'none']
144
+
145
+
146
+ def score(answers, qid, request=None) -> float | None:
147
+ """The option's probability in the Choice's distribution."""
148
+ if not str(qid).startswith(PREFIX):
149
+ return None
150
+ probs = probabilities({'answers': answers or {}}, 'culprit')
151
+ return probs.get(str(qid)[len(PREFIX):])
152
+
153
+
154
+ def _asked_order(request) -> list[str]:
155
+ """The options in the order the run asked them: the merge order of the request's tasks, then
156
+ `none`. A stored request sorts its criteria, so they cannot say; its state's task list does."""
157
+ if not isinstance(request, dict):
158
+ return []
159
+ state = request.get('state')
160
+ tasks = state.get('tasks') if isinstance(state, dict) else None
161
+ if isinstance(tasks, list):
162
+ ids = [str(item['id']) for item in tasks if isinstance(item, dict) and 'id' in item]
163
+ if ids:
164
+ return ids + ['none']
165
+ return _options(request)
166
+
167
+
168
+ def fired(answers, threshold_value=None, request=None) -> list[str] | None:
169
+ """The top option when it is not `none` (there is no threshold). Ties break as `main` ranks
170
+ them, by the order the run asked the options in. None when options tie at the top and the
171
+ request does not give that order (the report has only the question names when it finds no
172
+ stored body): the winner cannot be known then."""
173
+ probs = probabilities({'answers': answers or {}}, 'culprit')
174
+ order = _asked_order(request)
175
+ ordered = ranking(probs, order)
176
+ if not ordered:
177
+ return []
178
+ tied = [option for option, p in ordered if p == ordered[0][1]]
179
+ if len(tied) > 1 and any(option not in order for option in tied):
180
+ return None
181
+ if ordered[0][0] == 'none':
182
+ return []
183
+ return [PREFIX + ordered[0][0]]
184
+
185
+
186
+ def baseline(body, qid) -> float:
187
+ """How many of the task's declared files appear in the failing log: the plain rule the
188
+ model's probability is measured against."""
189
+ state = body.get('state') if isinstance(body, dict) else None
190
+ if not isinstance(state, dict):
191
+ return 0.0
192
+ log = str(state.get('log') or '')
193
+ task = str(qid)[len(PREFIX):]
194
+ for item in state.get('tasks') if isinstance(state.get('tasks'), list) else []:
195
+ if isinstance(item, dict) and item.get('id') == task:
196
+ files = item.get('files')
197
+ return float(sum(1 for name in files if str(name) in log)) if isinstance(files, list) else 0.0
198
+ return 0.0