@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.
- package/CHANGELOG.md +57 -0
- package/README.md +1 -0
- package/SKILL.md +32 -5
- package/VERSION +1 -1
- package/docs/artifacts.md +33 -1
- package/docs/commands.md +147 -16
- package/docs/dataset.md +5 -0
- package/docs/decomposed-runs.md +122 -1
- package/docs/development.md +2 -1
- package/docs/forge-ui.md +98 -8
- package/docs/memory.md +27 -4
- package/docs/models.md +8 -0
- package/docs/reviews.md +122 -0
- package/docs/safety.md +8 -0
- package/forge +44 -4
- package/package.json +2 -1
- package/references/dataset.md +39 -1
- package/references/decompose.md +737 -16
- package/references/harnesses.md +18 -3
- package/references/planning.md +43 -14
- package/references/solo.md +2 -1
- package/references/ui.md +56 -6
- package/registry.tsv +8 -3
- package/scripts/forge-artifact.sh +14 -0
- package/scripts/forge-brief.py +217 -0
- package/scripts/forge-compare.py +13 -2
- package/scripts/forge-config.py +176 -9
- package/scripts/forge-contract.py +246 -28
- package/scripts/forge-control.py +200 -15
- package/scripts/forge-dispatch.sh +48 -15
- package/scripts/forge-du.py +167 -0
- package/scripts/forge-group.py +496 -0
- package/scripts/forge-groups.py +89 -0
- package/scripts/forge-handoff.py +1128 -0
- package/scripts/forge-lib-adopt.sh +1 -0
- package/scripts/forge-lib-group.sh +641 -0
- package/scripts/forge-lib-plans.sh +343 -4
- package/scripts/forge-lib-review.sh +29 -1
- package/scripts/forge-lib-task.sh +197 -4
- package/scripts/forge-memory.sh +245 -3
- package/scripts/forge-metrics.sh +7 -3
- package/scripts/forge-parallel-options.sh +4 -0
- package/scripts/forge-parallel.sh +1216 -41
- package/scripts/forge-planner.py +104 -8
- package/scripts/forge-prompt.py +120 -0
- package/scripts/forge-retro.py +115 -0
- package/scripts/forge-route.py +190 -12
- package/scripts/forge-runs-compact.py +443 -0
- package/scripts/forge-runs-rm.py +2 -0
- package/scripts/forge-runs.py +27 -8
- package/scripts/forge-schedule.py +633 -49
- package/scripts/forge-shadow.py +7 -1
- package/scripts/forge-solo.sh +4 -0
- package/scripts/forge-summarize.py +698 -0
- package/scripts/forge-ui-options.sh +10 -0
- package/scripts/forge-worktrees.py +243 -0
- package/scripts/forge_brief/__init__.py +831 -0
- package/scripts/forge_brief/answers.py +506 -0
- package/scripts/forge_brief/proposals.py +1630 -0
- package/scripts/forge_dataset/__init__.py +10 -0
- package/scripts/forge_dataset/efforts.py +4 -3
- package/scripts/forge_dataset/export.py +36 -10
- package/scripts/forge_dataset/facts.py +8 -4
- package/scripts/forge_dataset/forecast.py +8 -18
- package/scripts/forge_dataset/harvest.py +241 -12
- package/scripts/forge_dataset/history.py +13 -6
- package/scripts/forge_dataset/importer.py +65 -1
- package/scripts/forge_dataset/names.py +66 -0
- package/scripts/forge_dataset/reviewers.py +5 -2
- package/scripts/forge_dataset/risk.py +6 -3
- package/scripts/forge_dataset/routing.py +6 -1
- package/scripts/forge_decide/__init__.py +11 -1
- package/scripts/forge_groups/__init__.py +308 -0
- package/scripts/forge_groups/attribute.py +289 -0
- package/scripts/forge_groups/review.py +286 -0
- package/scripts/forge_locations.py +366 -0
- package/scripts/forge_owner.py +152 -0
- package/scripts/forge_registry.py +19 -0
- package/scripts/forge_retro/__init__.py +417 -0
- package/scripts/forge_retro/issues.py +1193 -0
- package/scripts/forge_retro/report.py +671 -0
- package/scripts/forge_side/__init__.py +482 -0
- package/scripts/forge_storage.py +515 -0
- package/ui/README.md +68 -8
- package/ui/docs/api.md +375 -31
- package/ui/forge_ui/control.py +451 -11
- package/ui/forge_ui/executions.py +8 -1
- package/ui/forge_ui/export.py +53 -7
- package/ui/forge_ui/groups.py +264 -0
- package/ui/forge_ui/links.py +337 -0
- package/ui/forge_ui/models.py +125 -10
- package/ui/forge_ui/projects.py +4 -1
- package/ui/forge_ui/reader.py +919 -45
- package/ui/forge_ui/server.py +4 -0
- package/ui/forge_ui/static/app.js +173 -11
- package/ui/forge_ui/static/briefview.js +605 -0
- package/ui/forge_ui/static/checksview.js +6 -2
- package/ui/forge_ui/static/controls.js +5 -0
- package/ui/forge_ui/static/diffview.js +86 -1
- package/ui/forge_ui/static/groupsview.js +332 -0
- package/ui/forge_ui/static/index.html +4 -0
- package/ui/forge_ui/static/panel.js +394 -8
- package/ui/forge_ui/static/projectsview.js +189 -10
- package/ui/forge_ui/static/runmodel.js +27 -4
- package/ui/forge_ui/static/storageview.js +352 -0
- package/ui/forge_ui/static/style.css +176 -6
- package/ui/forge_ui/static/worktreesview.js +235 -14
- package/ui/forge_ui/storage.py +150 -0
- package/ui/forge_ui/transcripts.py +35 -10
- package/ui/forge_ui/worktrees.py +154 -13
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
"""Retro notes: what got in an agent's way, checked against the dispatch and stored locally.
|
|
2
|
+
|
|
3
|
+
A note is one line in a final message, `FORGE_RETRO: {json}`, or the same JSON object as one
|
|
4
|
+
line in a dwarf worktree's `.forge/retro-inbox.jsonl`. Parsing keeps a line it cannot read.
|
|
5
|
+
`record` adds forge's context, checks the evidence against that dispatch's log, and writes
|
|
6
|
+
the notes to the plan. Nothing here is written to project memory, the ledger, or a prompt.
|
|
7
|
+
|
|
8
|
+
Stored notes carry the agent's fields plus role, spec, alias, harness, effort, tier, task,
|
|
9
|
+
attempt, run_id, version, os, t and check (`verified` or `claimed`). An orchestrator note,
|
|
10
|
+
from `note`, is always `claimed`.
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import fcntl
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
import tempfile
|
|
19
|
+
import time
|
|
20
|
+
|
|
21
|
+
SKILL = Path(__file__).resolve().parents[2]
|
|
22
|
+
TYPES = ('environment', 'sandbox', 'tooling', 'prompt', 'forge-bug', 'other')
|
|
23
|
+
FIXABLE = ('user', 'forge', 'unsure')
|
|
24
|
+
MARKER = 'FORGE_RETRO:'
|
|
25
|
+
ERROR_CAP = 2048 # 2 KB, on the error text
|
|
26
|
+
ERROR_LINE = 120 # how much of the first error line the evidence check looks for
|
|
27
|
+
MALFORMED_CAP = 200
|
|
28
|
+
_EFFORT_UNSET = '<harness default>'
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def parse(text: str) -> list:
|
|
32
|
+
"""The retro notes in a final message, in order. A line that is not a note is ignored."""
|
|
33
|
+
notes = []
|
|
34
|
+
for line in (text or '').replace('\r\n', '\n').replace('\r', '\n').split('\n'):
|
|
35
|
+
start = line.find(MARKER)
|
|
36
|
+
if start < 0:
|
|
37
|
+
continue
|
|
38
|
+
payload = line[start + len(MARKER):].strip()
|
|
39
|
+
notes.append(_parse_payload(payload, line.strip()))
|
|
40
|
+
return notes
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def check(note, log_text) -> str:
|
|
44
|
+
"""`verified` when the log contains the command and the first error line, else `claimed`.
|
|
45
|
+
|
|
46
|
+
Both are compared with whitespace collapsed. The error contributes only its first
|
|
47
|
+
non-blank line, cut at 120 characters before collapsing. No evidence is `claimed`.
|
|
48
|
+
"""
|
|
49
|
+
evidence = note.get('evidence') if isinstance(note, dict) else None
|
|
50
|
+
if not isinstance(evidence, dict):
|
|
51
|
+
return 'claimed'
|
|
52
|
+
command = evidence.get('command')
|
|
53
|
+
error = evidence.get('error')
|
|
54
|
+
if not isinstance(command, str) or not isinstance(error, str):
|
|
55
|
+
return 'claimed'
|
|
56
|
+
command = _collapse(command)
|
|
57
|
+
first = next((line for line in error.splitlines() if line.strip()), '')
|
|
58
|
+
snippet = _collapse(first[:ERROR_LINE])
|
|
59
|
+
if not command or not snippet:
|
|
60
|
+
return 'claimed'
|
|
61
|
+
haystack = _collapse(log_text or '')
|
|
62
|
+
if command in haystack and snippet in haystack:
|
|
63
|
+
return 'verified'
|
|
64
|
+
return 'claimed'
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def record(plan, role, dispatch_dir, task=None, attempt=None, inbox=None) -> list:
|
|
68
|
+
"""Read one dispatch's notes, stamp them with forge's context, and store them.
|
|
69
|
+
|
|
70
|
+
Reads `<dispatch_dir>/<role>.last` and `<role>.log`. A dwarf also reads the worktree
|
|
71
|
+
inbox at `inbox`, or the path derived from the plan's repo, run id and task. The inbox
|
|
72
|
+
is deleted after the notes are stored. Returns the stored notes, which is empty when
|
|
73
|
+
the dispatch said nothing.
|
|
74
|
+
"""
|
|
75
|
+
plan = _require_plan(plan)
|
|
76
|
+
dispatch = Path(dispatch_dir)
|
|
77
|
+
resolved = _kv(dispatch / ('%s.resolved' % role))
|
|
78
|
+
last = _text(dispatch / ('%s.last' % role))
|
|
79
|
+
log_text = _text(dispatch / ('%s.log' % role))
|
|
80
|
+
notes = parse(last)
|
|
81
|
+
inbox_path = None
|
|
82
|
+
if role == 'dwarf':
|
|
83
|
+
inbox_path = Path(inbox) if inbox else _derived_inbox(plan, task)
|
|
84
|
+
inbox_notes, inbox_path = _load_inbox(inbox_path)
|
|
85
|
+
notes = inbox_notes + notes
|
|
86
|
+
if not notes:
|
|
87
|
+
_delete(inbox_path)
|
|
88
|
+
return []
|
|
89
|
+
context = _context(plan, role, resolved, task, attempt)
|
|
90
|
+
stored = [_stamp(_complete(note), context, check(note, log_text)) for note in notes]
|
|
91
|
+
_persist(plan, stored, task)
|
|
92
|
+
_delete(inbox_path)
|
|
93
|
+
for note in stored:
|
|
94
|
+
_emit(plan, note)
|
|
95
|
+
return stored
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def note(plan, **fields) -> list:
|
|
99
|
+
"""Record one orchestrator note. It is always `claimed`: there is no dispatch log."""
|
|
100
|
+
plan = _require_plan(plan)
|
|
101
|
+
agent = _from_object({
|
|
102
|
+
'type': fields.get('type'),
|
|
103
|
+
'fixable_by': fields.get('fixable_by'),
|
|
104
|
+
'summary': '' if fields.get('summary') is None else fields.get('summary'),
|
|
105
|
+
'blocking': fields.get('blocking') is True,
|
|
106
|
+
'tried': fields.get('tried'),
|
|
107
|
+
'evidence': _parts_evidence(fields),
|
|
108
|
+
})
|
|
109
|
+
stored = _stamp(agent, _context(plan, 'orchestrator', {}, None, None), 'claimed')
|
|
110
|
+
_persist(plan, [stored], None)
|
|
111
|
+
_emit(plan, stored)
|
|
112
|
+
return [stored]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
# --- parsing ---------------------------------------------------------------------
|
|
116
|
+
|
|
117
|
+
def _parse_payload(payload: str, raw: str) -> dict:
|
|
118
|
+
try:
|
|
119
|
+
obj = json.loads(payload) if payload else None
|
|
120
|
+
except json.JSONDecodeError:
|
|
121
|
+
obj = None
|
|
122
|
+
if not isinstance(obj, dict):
|
|
123
|
+
# Kept, not dropped. The summary is the text that failed to parse (not the marker),
|
|
124
|
+
# capped at 200 characters, so a later report can show what the agent wrote.
|
|
125
|
+
text = payload if payload else raw.strip()
|
|
126
|
+
return {'type': 'other', 'summary': text[:MALFORMED_CAP], 'malformed': True}
|
|
127
|
+
return _from_object(obj)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _from_object(obj: dict) -> dict:
|
|
131
|
+
return {
|
|
132
|
+
'type': obj.get('type') if obj.get('type') in TYPES else 'other',
|
|
133
|
+
'fixable_by': obj.get('fixable_by') if obj.get('fixable_by') in FIXABLE else 'unsure',
|
|
134
|
+
'summary': _one_line(obj.get('summary')),
|
|
135
|
+
'evidence': _evidence(obj.get('evidence')),
|
|
136
|
+
'blocking': obj.get('blocking') is True,
|
|
137
|
+
'tried': _tried(obj.get('tried')),
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _parts_evidence(fields: dict):
|
|
142
|
+
command, exit_code, error = fields.get('command'), fields.get('exit'), fields.get('error')
|
|
143
|
+
if command is None and exit_code is None and error is None:
|
|
144
|
+
return None
|
|
145
|
+
return {'command': command, 'exit': exit_code, 'error': error}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _evidence(value):
|
|
149
|
+
if not isinstance(value, dict):
|
|
150
|
+
return None
|
|
151
|
+
command, error = value.get('command'), value.get('error')
|
|
152
|
+
return {
|
|
153
|
+
'command': command if isinstance(command, str) else None,
|
|
154
|
+
'exit': _exit_code(value.get('exit')),
|
|
155
|
+
'error': _cap_error(error) if isinstance(error, str) else None,
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _exit_code(value):
|
|
160
|
+
if isinstance(value, bool) or not isinstance(value, int):
|
|
161
|
+
return None
|
|
162
|
+
return value
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _tried(value):
|
|
166
|
+
if not isinstance(value, str):
|
|
167
|
+
return None
|
|
168
|
+
text = _one_line(value)
|
|
169
|
+
return text or None
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _one_line(value) -> str:
|
|
173
|
+
if not isinstance(value, str):
|
|
174
|
+
return ''
|
|
175
|
+
return ' '.join(value.split())
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _cap_error(text: str) -> str:
|
|
179
|
+
raw = text.encode('utf-8', 'replace')
|
|
180
|
+
if len(raw) <= ERROR_CAP:
|
|
181
|
+
return text
|
|
182
|
+
return raw[:ERROR_CAP].decode('utf-8', 'ignore')
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _collapse(text: str) -> str:
|
|
186
|
+
return ' '.join(text.split())
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _complete(note: dict) -> dict:
|
|
190
|
+
note = dict(note)
|
|
191
|
+
note.setdefault('evidence', None)
|
|
192
|
+
note.setdefault('blocking', False)
|
|
193
|
+
note.setdefault('tried', None)
|
|
194
|
+
return note
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _parse_inbox(text: str) -> list:
|
|
198
|
+
"""One JSON object per line. A line that begins with the protocol marker is
|
|
199
|
+
accepted too. The marker inside a JSON string is part of the note, not a prefix.
|
|
200
|
+
"""
|
|
201
|
+
notes = []
|
|
202
|
+
for line in text.splitlines():
|
|
203
|
+
stripped = line.strip()
|
|
204
|
+
if not stripped:
|
|
205
|
+
continue
|
|
206
|
+
if stripped.startswith(MARKER):
|
|
207
|
+
notes.extend(parse(stripped))
|
|
208
|
+
else:
|
|
209
|
+
notes.append(_parse_payload(stripped, stripped))
|
|
210
|
+
return notes
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
# --- context ---------------------------------------------------------------------
|
|
214
|
+
|
|
215
|
+
def _context(plan: Path, role: str, resolved: dict, task, attempt) -> dict:
|
|
216
|
+
name = _task_name(task)
|
|
217
|
+
return {
|
|
218
|
+
'role': resolved.get('role') or role,
|
|
219
|
+
'spec': _spec(resolved),
|
|
220
|
+
'alias': _field(resolved, 'alias'),
|
|
221
|
+
'harness': _field(resolved, 'harness'),
|
|
222
|
+
'effort': _field(resolved, 'effort'),
|
|
223
|
+
'tier': _tier(plan, name),
|
|
224
|
+
'task': name,
|
|
225
|
+
'attempt': _attempt(attempt),
|
|
226
|
+
'run_id': _first_line(plan / 'run_id'),
|
|
227
|
+
'version': _first_line(SKILL / 'VERSION'),
|
|
228
|
+
'os': os.uname().sysname,
|
|
229
|
+
't': int(time.time()),
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _spec(resolved: dict):
|
|
234
|
+
"""The spec line, or alias:effort when the dispatch did not record one.
|
|
235
|
+
|
|
236
|
+
`<role>.resolved` names the alias, harness and effort. The spec the caller asked for
|
|
237
|
+
is not one of its lines today, so a missing `spec=` is rebuilt the way forge-ui does.
|
|
238
|
+
"""
|
|
239
|
+
spec = (resolved.get('spec') or '').strip()
|
|
240
|
+
if spec:
|
|
241
|
+
return spec
|
|
242
|
+
alias = (resolved.get('alias') or '').strip()
|
|
243
|
+
if not alias:
|
|
244
|
+
return None
|
|
245
|
+
effort = (resolved.get('effort') or '').strip()
|
|
246
|
+
if effort and effort != _EFFORT_UNSET:
|
|
247
|
+
return alias + ':' + effort
|
|
248
|
+
return alias
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _field(resolved: dict, key: str):
|
|
252
|
+
value = (resolved.get(key) or '').strip()
|
|
253
|
+
return value or None
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def _tier(plan: Path, task: str | None):
|
|
257
|
+
if not task:
|
|
258
|
+
return None
|
|
259
|
+
for line in _text(plan / 'tasks.tsv').splitlines():
|
|
260
|
+
if not line.strip() or line.startswith('#'):
|
|
261
|
+
continue
|
|
262
|
+
parts = line.split('\t')
|
|
263
|
+
if len(parts) >= 3 and parts[0] == task:
|
|
264
|
+
return parts[2].strip() or None
|
|
265
|
+
return None
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _attempt(value):
|
|
269
|
+
if value is None or isinstance(value, bool):
|
|
270
|
+
return None
|
|
271
|
+
if isinstance(value, int):
|
|
272
|
+
return value
|
|
273
|
+
if isinstance(value, str) and value.strip().lstrip('-').isdigit():
|
|
274
|
+
return int(value.strip())
|
|
275
|
+
return None
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _task_name(task):
|
|
279
|
+
if task is None:
|
|
280
|
+
return None
|
|
281
|
+
text = str(task).strip()
|
|
282
|
+
if not text or text in ('.', '..') or '/' in text or '\\' in text or '\x00' in text:
|
|
283
|
+
return None
|
|
284
|
+
return text
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _stamp(note: dict, context: dict, verdict: str) -> dict:
|
|
288
|
+
stored = _complete(note)
|
|
289
|
+
stored.update(context)
|
|
290
|
+
stored['check'] = verdict
|
|
291
|
+
return stored
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def _derived_inbox(plan: Path, task) -> Path | None:
|
|
295
|
+
name = _task_name(task)
|
|
296
|
+
repo, run_id = _first_line(plan / 'repo'), _first_line(plan / 'run_id')
|
|
297
|
+
if not name or not repo or not run_id:
|
|
298
|
+
return None
|
|
299
|
+
from forge_worktrees import wt_root
|
|
300
|
+
return Path(wt_root(repo, run_id)) / name / '.forge' / 'retro-inbox.jsonl'
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _load_inbox(path: Path | None):
|
|
304
|
+
if path is None or not path.is_file():
|
|
305
|
+
return [], None
|
|
306
|
+
try:
|
|
307
|
+
text = path.read_text(encoding='utf-8', errors='replace')
|
|
308
|
+
except OSError:
|
|
309
|
+
return [], None
|
|
310
|
+
return _parse_inbox(text), path
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def _delete(path: Path | None) -> None:
|
|
314
|
+
if path is None:
|
|
315
|
+
return
|
|
316
|
+
try:
|
|
317
|
+
path.unlink()
|
|
318
|
+
except OSError:
|
|
319
|
+
pass
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
# --- storage ---------------------------------------------------------------------
|
|
323
|
+
|
|
324
|
+
def _require_plan(plan) -> Path:
|
|
325
|
+
path = Path(plan)
|
|
326
|
+
if not path.is_dir():
|
|
327
|
+
raise FileNotFoundError(path)
|
|
328
|
+
return path
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def _persist(plan: Path, notes: list, task) -> None:
|
|
332
|
+
if not notes:
|
|
333
|
+
return
|
|
334
|
+
directory = plan / 'retro'
|
|
335
|
+
directory.mkdir(parents=True, exist_ok=True)
|
|
336
|
+
lock_path = directory / '.lock'
|
|
337
|
+
with open(lock_path, 'a', encoding='utf-8') as lock:
|
|
338
|
+
fcntl.flock(lock.fileno(), fcntl.LOCK_EX)
|
|
339
|
+
try:
|
|
340
|
+
_append(directory / 'notes.jsonl', notes)
|
|
341
|
+
name = _task_name(task)
|
|
342
|
+
if name:
|
|
343
|
+
_rewrite(plan / 'tasks' / name / 'retro.json', notes)
|
|
344
|
+
finally:
|
|
345
|
+
fcntl.flock(lock.fileno(), fcntl.LOCK_UN)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _append(path: Path, notes: list) -> None:
|
|
349
|
+
blob = ''.join(_dump(note) + '\n' for note in notes)
|
|
350
|
+
with path.open('a', encoding='utf-8', newline='\n') as handle:
|
|
351
|
+
handle.write(blob)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def _rewrite(path: Path, notes: list) -> None:
|
|
355
|
+
"""Replace the task's note array, including the notes already there, in one rename."""
|
|
356
|
+
existing = []
|
|
357
|
+
if path.is_file():
|
|
358
|
+
try:
|
|
359
|
+
loaded = json.loads(path.read_text(encoding='utf-8'))
|
|
360
|
+
except (OSError, json.JSONDecodeError):
|
|
361
|
+
loaded = None
|
|
362
|
+
if isinstance(loaded, list):
|
|
363
|
+
existing = loaded
|
|
364
|
+
existing.extend(notes)
|
|
365
|
+
_atomic(path, json.dumps(existing, ensure_ascii=False, indent=2, sort_keys=True) + '\n')
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _atomic(path: Path, text: str) -> None:
|
|
369
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
370
|
+
fd, tmp = tempfile.mkstemp(dir=str(path.parent), prefix='.' + path.name + '.')
|
|
371
|
+
try:
|
|
372
|
+
with os.fdopen(fd, 'w', encoding='utf-8', newline='\n') as handle:
|
|
373
|
+
handle.write(text)
|
|
374
|
+
os.replace(tmp, path)
|
|
375
|
+
except BaseException:
|
|
376
|
+
try:
|
|
377
|
+
os.unlink(tmp)
|
|
378
|
+
except OSError:
|
|
379
|
+
pass
|
|
380
|
+
raise
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def _dump(note: dict) -> str:
|
|
384
|
+
return json.dumps(note, ensure_ascii=False, sort_keys=True)
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def _emit(plan: Path, note: dict) -> None:
|
|
388
|
+
"""One retro_note dataset event. Fail-open: the plan files are the record."""
|
|
389
|
+
try:
|
|
390
|
+
from forge_dataset.capture import point
|
|
391
|
+
point('retro_note', str(plan), note.get('task'), dict(note))
|
|
392
|
+
except Exception:
|
|
393
|
+
return
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
def _text(path: Path) -> str:
|
|
397
|
+
try:
|
|
398
|
+
return path.read_text(encoding='utf-8', errors='replace')
|
|
399
|
+
except OSError:
|
|
400
|
+
return ''
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def _first_line(path: Path):
|
|
404
|
+
for line in _text(path).splitlines():
|
|
405
|
+
line = line.strip()
|
|
406
|
+
if line:
|
|
407
|
+
return line
|
|
408
|
+
return None
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _kv(path: Path) -> dict:
|
|
412
|
+
found = {}
|
|
413
|
+
for line in _text(path).splitlines():
|
|
414
|
+
key, sep, value = line.partition('=')
|
|
415
|
+
if sep and key.strip():
|
|
416
|
+
found[key.strip()] = value.strip()
|
|
417
|
+
return found
|