@giamme/forge 0.11.1 → 0.12.1
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 +56 -0
- package/SKILL.md +7 -3
- package/VERSION +1 -1
- package/docs/artifacts.md +50 -11
- package/docs/commands.md +56 -8
- package/docs/decomposed-runs.md +43 -2
- package/docs/forge-ui.md +55 -8
- package/docs/reviews.md +64 -6
- package/package.json +1 -1
- package/references/dataset.md +4 -2
- package/references/decompose.md +191 -17
- package/references/harnesses.md +13 -1
- package/references/ui.md +25 -4
- package/scripts/forge-contract.py +37 -7
- package/scripts/forge-control.py +268 -25
- package/scripts/forge-dispatch.sh +90 -3
- package/scripts/forge-guard.py +156 -15
- package/scripts/forge-issues.py +47 -3
- package/scripts/forge-lib-plans.sh +9 -3
- package/scripts/forge-lib-review.sh +24 -5
- package/scripts/forge-lib-task.sh +53 -5
- package/scripts/forge-lib-verify.sh +137 -6
- package/scripts/forge-parallel-options.sh +85 -0
- package/scripts/forge-parallel.sh +262 -31
- package/scripts/forge-ripwire.py +1 -1
- package/scripts/forge-route.py +157 -5
- package/scripts/forge-runtime.py +49 -1
- package/scripts/forge-schedule.py +735 -53
- package/scripts/forge-solo.sh +3 -1
- package/scripts/forge-sweep.py +542 -0
- package/scripts/forge-verify.py +878 -0
- package/scripts/forge_dataset/forecast.py +33 -5
- package/scripts/forge_dataset/harvest.py +10 -4
- package/scripts/forge_verify_reports.py +225 -0
- package/ui/README.md +58 -12
- package/ui/docs/api.md +277 -28
- package/ui/forge_ui/checks.py +331 -0
- package/ui/forge_ui/control.py +12 -2
- package/ui/forge_ui/export.py +25 -8
- package/ui/forge_ui/reader.py +657 -24
- package/ui/forge_ui/server.py +29 -0
- package/ui/forge_ui/static/app.js +15 -7
- package/ui/forge_ui/static/checksview.js +449 -0
- package/ui/forge_ui/static/controls.js +217 -33
- package/ui/forge_ui/static/convo.js +11 -2
- package/ui/forge_ui/static/findingsview.js +6 -4
- package/ui/forge_ui/static/gantt.js +14 -9
- package/ui/forge_ui/static/index.html +2 -0
- package/ui/forge_ui/static/panel.js +44 -1
- package/ui/forge_ui/static/runmodel.js +109 -12
- package/ui/forge_ui/static/scene3d.js +9 -5
- package/ui/forge_ui/static/style.css +113 -0
- package/ui/forge_ui/static/verifyview.js +324 -0
- package/ui/forge_ui/transcripts.py +225 -54
|
@@ -24,11 +24,15 @@ An event is a dict with `kind` and the keys that kind uses:
|
|
|
24
24
|
gap count events left out of the middle of a very long transcript
|
|
25
25
|
Every event may carry `t` (epoch seconds), `id` (the harness's own id for a tool call), `error` (true for a call
|
|
26
26
|
that failed, false for one answered without an error) and, while the execution runs, `running` (a call not
|
|
27
|
-
answered yet). Texts are capped.
|
|
28
|
-
|
|
27
|
+
answered yet). A command, edit or tool call that was answered may carry `ended` (epoch seconds). Texts are capped.
|
|
28
|
+
Times come from the harness when its lines carry them (claude, opencode); otherwise from `<role>.clock` beside the
|
|
29
|
+
log, which forge-dispatch.sh writes while the harness runs: `<epoch> <bytes>` each time the log grew, so a line is
|
|
30
|
+
timed by the first sample that saw the log reach its end (to within the clock's one second).
|
|
31
|
+
Nothing here opens any path but the log given, its clock and, for a Claude result, that one session file.
|
|
29
32
|
"""
|
|
30
33
|
from __future__ import annotations
|
|
31
34
|
|
|
35
|
+
import bisect
|
|
32
36
|
from datetime import datetime
|
|
33
37
|
import json
|
|
34
38
|
import os
|
|
@@ -44,6 +48,8 @@ CHARS_CAP = 4_000_000
|
|
|
44
48
|
_SESSION_ID = re.compile(r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
|
|
45
49
|
_ANSI = re.compile(r'\x1b\[[0-9;?]*[ -/]*[@-~]')
|
|
46
50
|
_SHELL = re.compile(r"(?:/\S+/)?(?:ba|z)?sh -l?c '((?:[^']|'\\'')*)'", re.S) # codex's wrapper around a command
|
|
51
|
+
_SHELL_DQ = re.compile(r'(?:/\S+/)?(?:ba|z)?sh -l?c "((?:[^"\\]|\\.)*)"', re.S) # the same, double-quoted
|
|
52
|
+
_DQ_ESCAPE = re.compile(r'\\([$`"\\\n])')
|
|
47
53
|
_TARGETS = ('file_path', 'filePath', 'target_file', 'notebook_path', 'path', 'target_directory', 'pattern', 'query', 'url')
|
|
48
54
|
_SHELL_TOOLS = frozenset(('bash', 'shell', 'run_terminal_command', 'run_shell_command', 'exec_command', 'terminal'))
|
|
49
55
|
_EDIT_TOOLS = frozenset(('edit', 'write', 'multiedit', 'notebookedit', 'search_replace', 'write_file', 'edit_file',
|
|
@@ -81,9 +87,14 @@ def _event(kind, **fields):
|
|
|
81
87
|
|
|
82
88
|
|
|
83
89
|
def _unwrap(command):
|
|
84
|
-
"""A command without the `bash -lc '…'` codex runs it in."""
|
|
85
|
-
|
|
86
|
-
|
|
90
|
+
"""A command without the `bash -lc '…'` (or `"…"`) codex runs it in."""
|
|
91
|
+
if not isinstance(command, str):
|
|
92
|
+
return command
|
|
93
|
+
m = _SHELL.fullmatch(command)
|
|
94
|
+
if m:
|
|
95
|
+
return m.group(1).replace("'\\''", "'")
|
|
96
|
+
m = _SHELL_DQ.fullmatch(command)
|
|
97
|
+
return _DQ_ESCAPE.sub(r'\1', m.group(1)) if m else command
|
|
87
98
|
|
|
88
99
|
|
|
89
100
|
def _tool_event(name, args, ident=None, t=None):
|
|
@@ -151,19 +162,32 @@ def _tokens(total, usage):
|
|
|
151
162
|
|
|
152
163
|
# ---------- the formats ----------
|
|
153
164
|
|
|
165
|
+
class _Lines:
|
|
166
|
+
"""(json or None, raw line) for each non-empty line; `end` is then the byte offset just past that line, so
|
|
167
|
+
an event can be timed by when the log reached it."""
|
|
168
|
+
|
|
169
|
+
def __init__(self, text):
|
|
170
|
+
self.text, self.end = text, 0
|
|
171
|
+
|
|
172
|
+
def __iter__(self):
|
|
173
|
+
pos = 0
|
|
174
|
+
for raw in self.text.split('\n'):
|
|
175
|
+
pos += len(raw.encode('utf-8', 'surrogatepass')) + 1
|
|
176
|
+
line = raw.strip()
|
|
177
|
+
if not line:
|
|
178
|
+
continue
|
|
179
|
+
self.end = pos
|
|
180
|
+
value = None
|
|
181
|
+
if line[0] == '{':
|
|
182
|
+
try:
|
|
183
|
+
value = json.loads(line)
|
|
184
|
+
except ValueError:
|
|
185
|
+
value = None
|
|
186
|
+
yield (value if isinstance(value, dict) else None), raw.rstrip('\r')
|
|
187
|
+
|
|
188
|
+
|
|
154
189
|
def _lines(text):
|
|
155
|
-
|
|
156
|
-
for raw in text.splitlines():
|
|
157
|
-
line = raw.strip()
|
|
158
|
-
if not line:
|
|
159
|
-
continue
|
|
160
|
-
value = None
|
|
161
|
-
if line[0] == '{':
|
|
162
|
-
try:
|
|
163
|
-
value = json.loads(line)
|
|
164
|
-
except ValueError:
|
|
165
|
-
value = None
|
|
166
|
-
yield (value if isinstance(value, dict) else None), raw
|
|
190
|
+
return iter(_Lines(text))
|
|
167
191
|
|
|
168
192
|
|
|
169
193
|
def detect(text):
|
|
@@ -208,40 +232,57 @@ def _stderr(events, pending):
|
|
|
208
232
|
pending.clear()
|
|
209
233
|
|
|
210
234
|
|
|
235
|
+
def _placed(events, mark, end):
|
|
236
|
+
"""The events a line added are placed at its end in the log (their `_at`), for the clock to time."""
|
|
237
|
+
for ev in events[mark:]:
|
|
238
|
+
ev.setdefault('_at', end)
|
|
239
|
+
|
|
240
|
+
|
|
211
241
|
def parse_codex(text):
|
|
212
242
|
events, by_id, tokens, meta, pending = [], {}, {}, {}, []
|
|
213
|
-
|
|
243
|
+
lines = _Lines(text)
|
|
244
|
+
for value, raw in lines:
|
|
214
245
|
if value is None:
|
|
215
246
|
pending.append(raw)
|
|
216
247
|
continue
|
|
217
248
|
_stderr(events, pending)
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
elif kind in ('item.started', 'item.updated', 'item.completed'):
|
|
222
|
-
item = value.get('item') or {}
|
|
223
|
-
ev = _codex_item(item)
|
|
224
|
-
if ev is None:
|
|
225
|
-
continue
|
|
226
|
-
if kind != 'item.completed':
|
|
227
|
-
ev['running'] = True
|
|
228
|
-
ident = item.get('id')
|
|
229
|
-
if ident in by_id: # started, then completed: the same place in the list
|
|
230
|
-
events[by_id[ident]] = ev
|
|
231
|
-
else:
|
|
232
|
-
if ident:
|
|
233
|
-
by_id[ident] = len(events)
|
|
234
|
-
events.append(ev)
|
|
235
|
-
elif kind == 'turn.completed':
|
|
236
|
-
_tokens(tokens, value.get('usage'))
|
|
237
|
-
elif kind == 'turn.failed':
|
|
238
|
-
events.append(_event('error', text=(value.get('error') or {}).get('message') or 'the turn failed'))
|
|
239
|
-
elif kind == 'error':
|
|
240
|
-
events.append(_event('error', text=value.get('message') or 'error'))
|
|
249
|
+
mark = len(events)
|
|
250
|
+
_codex_line(value, events, by_id, tokens, meta, lines.end)
|
|
251
|
+
_placed(events, mark, lines.end)
|
|
241
252
|
_stderr(events, pending)
|
|
242
253
|
return events, tokens, meta
|
|
243
254
|
|
|
244
255
|
|
|
256
|
+
def _codex_line(value, events, by_id, tokens, meta, end):
|
|
257
|
+
"""One codex event into the list: an item replaces the one it updates, in place."""
|
|
258
|
+
kind = value.get('type')
|
|
259
|
+
if kind == 'thread.started':
|
|
260
|
+
meta['session'] = value.get('thread_id')
|
|
261
|
+
elif kind in ('item.started', 'item.updated', 'item.completed'):
|
|
262
|
+
item = value.get('item') or {}
|
|
263
|
+
ev = _codex_item(item)
|
|
264
|
+
if ev is None:
|
|
265
|
+
return
|
|
266
|
+
if kind != 'item.completed':
|
|
267
|
+
ev['running'] = True
|
|
268
|
+
else:
|
|
269
|
+
ev['_done'] = end
|
|
270
|
+
ident = item.get('id')
|
|
271
|
+
if ident in by_id: # started, then completed: the same place in the list
|
|
272
|
+
ev['_at'] = events[by_id[ident]].get('_at', end)
|
|
273
|
+
events[by_id[ident]] = ev
|
|
274
|
+
else:
|
|
275
|
+
if ident:
|
|
276
|
+
by_id[ident] = len(events)
|
|
277
|
+
events.append(ev)
|
|
278
|
+
elif kind == 'turn.completed':
|
|
279
|
+
_tokens(tokens, value.get('usage'))
|
|
280
|
+
elif kind == 'turn.failed':
|
|
281
|
+
events.append(_event('error', text=(value.get('error') or {}).get('message') or 'the turn failed'))
|
|
282
|
+
elif kind == 'error':
|
|
283
|
+
events.append(_event('error', text=value.get('message') or 'error'))
|
|
284
|
+
|
|
285
|
+
|
|
245
286
|
def _codex_item(item):
|
|
246
287
|
kind = item.get('type') or item.get('item_type')
|
|
247
288
|
if kind in ('agent_message', 'assistant_message'):
|
|
@@ -277,7 +318,10 @@ def _codex_item(item):
|
|
|
277
318
|
def parse_claude(text, session_file=False):
|
|
278
319
|
"""Claude's message shape: grok's and claude's streams, and Claude's session files (one block per line there)."""
|
|
279
320
|
events, by_id, tokens, meta, pending, counted = [], {}, {}, {}, [], set()
|
|
280
|
-
|
|
321
|
+
lines, mark, at = _Lines(text), 0, 0
|
|
322
|
+
for value, raw in lines:
|
|
323
|
+
_placed(events, mark, at) # the previous line's events
|
|
324
|
+
mark, at = len(events), lines.end
|
|
281
325
|
if value is None:
|
|
282
326
|
if not session_file:
|
|
283
327
|
pending.append(raw)
|
|
@@ -309,8 +353,10 @@ def parse_claude(text, session_file=False):
|
|
|
309
353
|
ev['running'] = True # until its result comes
|
|
310
354
|
events.append(ev)
|
|
311
355
|
elif bt.endswith('_tool_result') and block.get('tool_use_id') in by_id: # a server tool's result
|
|
312
|
-
by_id[block['tool_use_id']]
|
|
313
|
-
|
|
356
|
+
done = by_id[block['tool_use_id']]
|
|
357
|
+
done.pop('running', None)
|
|
358
|
+
_answered(done, lines.end, t)
|
|
359
|
+
_put(done, 'output', _payload(block.get('content'))[0], OUTPUT_CAP)
|
|
314
360
|
elif kind == 'user' and message:
|
|
315
361
|
for block in _blocks(message.get('content')):
|
|
316
362
|
bt = block.get('type')
|
|
@@ -321,6 +367,7 @@ def parse_claude(text, session_file=False):
|
|
|
321
367
|
events.append(_event('tool', tool='result', output=out, t=t))
|
|
322
368
|
continue
|
|
323
369
|
ev.pop('running', None)
|
|
370
|
+
_answered(ev, lines.end, t)
|
|
324
371
|
_put(ev, 'output', out, OUTPUT_CAP)
|
|
325
372
|
if code is not None and ev['kind'] == 'command':
|
|
326
373
|
ev['exit'] = code
|
|
@@ -341,10 +388,18 @@ def parse_claude(text, session_file=False):
|
|
|
341
388
|
if error and not res:
|
|
342
389
|
res = '; '.join(str(e) for e in value.get('errors') or []) or value.get('subtype') or 'error'
|
|
343
390
|
_close(events, _event('result', text=res, error=True if error else None, tokens=dict(tokens) or None))
|
|
391
|
+
_placed(events, mark, at)
|
|
344
392
|
_stderr(events, pending)
|
|
345
393
|
return events, tokens, meta
|
|
346
394
|
|
|
347
395
|
|
|
396
|
+
def _answered(ev, end, t):
|
|
397
|
+
"""A call's answer came at the line ending at `end`, at `t` when the line says."""
|
|
398
|
+
ev['_done'] = end
|
|
399
|
+
if t is not None:
|
|
400
|
+
ev['ended'] = t
|
|
401
|
+
|
|
402
|
+
|
|
348
403
|
def _close(events, result):
|
|
349
404
|
"""Adds the final result; its text is left out when the agent's last message already said it."""
|
|
350
405
|
last = next((ev for ev in reversed(events) if ev['kind'] == 'message'), None)
|
|
@@ -391,7 +446,10 @@ def _payload(content):
|
|
|
391
446
|
|
|
392
447
|
def parse_opencode(text):
|
|
393
448
|
events, tokens, meta, pending = [], {}, {}, []
|
|
394
|
-
|
|
449
|
+
lines, mark, at = _Lines(text), 0, 0
|
|
450
|
+
for value, raw in lines:
|
|
451
|
+
_placed(events, mark, at)
|
|
452
|
+
mark, at = len(events), lines.end
|
|
395
453
|
if value is None:
|
|
396
454
|
pending.append(raw)
|
|
397
455
|
continue
|
|
@@ -404,7 +462,10 @@ def parse_opencode(text):
|
|
|
404
462
|
events.append(_event('reasoning', text=part.get('text'), t=t))
|
|
405
463
|
elif kind == 'tool_use':
|
|
406
464
|
state = part.get('state') if isinstance(part.get('state'), dict) else {}
|
|
407
|
-
|
|
465
|
+
span = state.get('time') if isinstance(state.get('time'), dict) else {}
|
|
466
|
+
ev = _tool_event(part.get('tool'), state.get('input'), part.get('callID'), _when(span.get('start')) or t)
|
|
467
|
+
if _when(span.get('end')) is not None:
|
|
468
|
+
ev['ended'] = _when(span.get('end'))
|
|
408
469
|
_put(ev, 'output', state.get('output') if state.get('status') != 'error' else state.get('error'), OUTPUT_CAP)
|
|
409
470
|
meta_ = state.get('metadata') if isinstance(state.get('metadata'), dict) else {}
|
|
410
471
|
if ev['kind'] == 'command' and isinstance(meta_.get('exit'), int):
|
|
@@ -420,6 +481,7 @@ def parse_opencode(text):
|
|
|
420
481
|
err = value.get('error') if isinstance(value.get('error'), dict) else {}
|
|
421
482
|
data = err.get('data') if isinstance(err.get('data'), dict) else {}
|
|
422
483
|
events.append(_event('error', text=data.get('message') or err.get('name') or 'error', t=t))
|
|
484
|
+
_placed(events, mark, at)
|
|
423
485
|
_stderr(events, pending)
|
|
424
486
|
return events, tokens, meta
|
|
425
487
|
|
|
@@ -427,7 +489,10 @@ def parse_opencode(text):
|
|
|
427
489
|
def parse_json(text):
|
|
428
490
|
"""JSON lines of a shape not known here (agy's stream, say): each shown as it is, text kept readable."""
|
|
429
491
|
events = []
|
|
430
|
-
|
|
492
|
+
lines, mark, at = _Lines(text), 0, 0
|
|
493
|
+
for value, raw in lines:
|
|
494
|
+
_placed(events, mark, at)
|
|
495
|
+
mark, at = len(events), lines.end
|
|
431
496
|
if value is None:
|
|
432
497
|
events.append(_event('text', text=_ANSI.sub('', raw)))
|
|
433
498
|
continue
|
|
@@ -445,6 +510,7 @@ def parse_json(text):
|
|
|
445
510
|
events.append(_event('error' if 'error' in kind else 'message', text=body, source=kind))
|
|
446
511
|
else:
|
|
447
512
|
events.append(_event('tool', tool=kind, input=value))
|
|
513
|
+
_placed(events, mark, at)
|
|
448
514
|
return _merge_text(events), {}, {}
|
|
449
515
|
|
|
450
516
|
|
|
@@ -497,19 +563,69 @@ def session_file(session_id, home=None, environ=None):
|
|
|
497
563
|
# ---------- one execution's transcript ----------
|
|
498
564
|
|
|
499
565
|
def _read(path):
|
|
500
|
-
"""The log's text (its end when it is over LOG_CAP)
|
|
566
|
+
"""The log's text (its end when it is over LOG_CAP), whether it was cut, and the byte offset it starts at."""
|
|
567
|
+
base = 0
|
|
501
568
|
try:
|
|
502
569
|
size = path.stat().st_size
|
|
503
570
|
with open(path, 'rb') as fh:
|
|
504
571
|
if size > LOG_CAP:
|
|
505
572
|
fh.seek(size - LOG_CAP)
|
|
506
573
|
data = fh.read()
|
|
507
|
-
|
|
574
|
+
skip = data.find(b'\n') + 1
|
|
575
|
+
data, base = data[skip:], size - LOG_CAP + skip
|
|
508
576
|
else:
|
|
509
577
|
data = fh.read()
|
|
510
578
|
except OSError:
|
|
511
|
-
return None, False
|
|
512
|
-
return data.decode('utf-8', 'replace'), size > LOG_CAP
|
|
579
|
+
return None, False, 0
|
|
580
|
+
return data.decode('utf-8', 'replace'), size > LOG_CAP, base
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
CLOCK_CAP = 8 << 20 # bytes of a clock read
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def clock_of(log):
|
|
587
|
+
"""The clock beside `log` (`<role>.clock` for `<role>.log`) as (sizes, times), sizes ascending; None without one."""
|
|
588
|
+
try:
|
|
589
|
+
path = log.with_name(log.stem + '.clock')
|
|
590
|
+
with open(path, 'rb') as fh:
|
|
591
|
+
if not stat.S_ISREG(os.fstat(fh.fileno()).st_mode):
|
|
592
|
+
return None
|
|
593
|
+
text = fh.read(CLOCK_CAP).decode('ascii', 'replace')
|
|
594
|
+
except (OSError, ValueError):
|
|
595
|
+
return None
|
|
596
|
+
sizes, times = [], []
|
|
597
|
+
for line in text.splitlines():
|
|
598
|
+
parts = line.split()
|
|
599
|
+
try:
|
|
600
|
+
t, size = float(parts[0]), int(parts[1])
|
|
601
|
+
except (IndexError, ValueError):
|
|
602
|
+
continue
|
|
603
|
+
if len(parts) == 2 and (not sizes or size > sizes[-1]) and t >= (times[-1] if times else 0):
|
|
604
|
+
sizes.append(size)
|
|
605
|
+
times.append(t)
|
|
606
|
+
return (sizes, times) if sizes else None
|
|
607
|
+
|
|
608
|
+
|
|
609
|
+
def _when_written(clock, offset):
|
|
610
|
+
"""When the log first reached `offset` bytes, by its clock; None past its last sample."""
|
|
611
|
+
i = bisect.bisect_left(clock[0], offset)
|
|
612
|
+
return clock[1][i] if i < len(clock[0]) else None
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def _time(events, clock, base):
|
|
616
|
+
"""Fills in `t` and a call's `ended` from where its lines are in the log, then drops those places."""
|
|
617
|
+
for ev in events:
|
|
618
|
+
at, done = ev.pop('_at', None), ev.pop('_done', None)
|
|
619
|
+
if clock is None:
|
|
620
|
+
continue
|
|
621
|
+
if ev.get('t') is None and at is not None:
|
|
622
|
+
when = _when_written(clock, base + at)
|
|
623
|
+
if when is not None:
|
|
624
|
+
ev['t'] = when
|
|
625
|
+
if ev.get('ended') is None and done is not None and ev['kind'] in ('command', 'edit', 'tool'):
|
|
626
|
+
when = _when_written(clock, base + done)
|
|
627
|
+
if when is not None:
|
|
628
|
+
ev['ended'] = when
|
|
513
629
|
|
|
514
630
|
|
|
515
631
|
def transcript(log, harness=None, running=False, home=None, environ=None):
|
|
@@ -528,12 +644,12 @@ def transcript(log, harness=None, running=False, home=None, environ=None):
|
|
|
528
644
|
if st is None or not stat.S_ISREG(st.st_mode):
|
|
529
645
|
out['warnings'].append(f'{log.name} is not readable')
|
|
530
646
|
return out
|
|
531
|
-
key = (str(log), st.st_size, st.st_mtime_ns, harness, running)
|
|
647
|
+
key = (str(log), st.st_size, st.st_mtime_ns, harness, running, _signature(log.with_name(log.stem + '.clock')))
|
|
532
648
|
if key in _CACHE:
|
|
533
649
|
answer, sid, seen = _CACHE[key]
|
|
534
650
|
if sid is None or _signature(session_file(sid, home, environ)) == seen:
|
|
535
651
|
return json.loads(answer)
|
|
536
|
-
text, cut = _read(log)
|
|
652
|
+
text, cut, base = _read(log)
|
|
537
653
|
if text is None:
|
|
538
654
|
out['warnings'].append(f'{log.name} is not readable')
|
|
539
655
|
return out
|
|
@@ -552,7 +668,7 @@ def transcript(log, harness=None, running=False, home=None, environ=None):
|
|
|
552
668
|
tail = parse_claude(json.dumps(result))[0]
|
|
553
669
|
events, tokens, meta = tail, {}, {}
|
|
554
670
|
if path is not None:
|
|
555
|
-
stext, scut = _read(path)
|
|
671
|
+
stext, scut, _ = _read(path)
|
|
556
672
|
if stext is not None:
|
|
557
673
|
events, tokens, meta = parse_claude(stext, session_file=True)
|
|
558
674
|
_close(events, tail[-1]) # the run's own result closes it
|
|
@@ -568,6 +684,8 @@ def transcript(log, harness=None, running=False, home=None, environ=None):
|
|
|
568
684
|
meta['cost'] = result['total_cost_usd']
|
|
569
685
|
else:
|
|
570
686
|
events, tokens, meta = PARSERS[fmt](text)
|
|
687
|
+
# a session file's lines carry their own times, and its places are not the log's
|
|
688
|
+
_time(events, clock_of(log) if out['source'] == 'log' else None, base)
|
|
571
689
|
if not running:
|
|
572
690
|
for ev in events: # a call cut off when the agent stopped
|
|
573
691
|
ev.pop('running', None)
|
|
@@ -583,6 +701,59 @@ def transcript(log, harness=None, running=False, home=None, environ=None):
|
|
|
583
701
|
return out
|
|
584
702
|
|
|
585
703
|
|
|
704
|
+
LIVE_TAIL = 256 << 10 # bytes of a running log read for what it does now
|
|
705
|
+
_LIVE: dict = {}
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
def live(log, harness=None):
|
|
709
|
+
"""What the agent writing `log` does now, read from the log's end: {call, last_output_at}.
|
|
710
|
+
|
|
711
|
+
`call` is the newest call not answered yet, {kind: command|tool|edit, command, tool, started}, or None while the
|
|
712
|
+
agent thinks or writes. `started` comes from the harness, the clock or, for a call on the log's last line, the
|
|
713
|
+
log's mtime (nothing was written after it). None when `log` is not a readable file.
|
|
714
|
+
"""
|
|
715
|
+
try:
|
|
716
|
+
st = log.stat()
|
|
717
|
+
except (OSError, AttributeError):
|
|
718
|
+
return None
|
|
719
|
+
if not stat.S_ISREG(st.st_mode):
|
|
720
|
+
return None
|
|
721
|
+
clock_path = log.with_name(log.stem + '.clock')
|
|
722
|
+
key = (str(log), st.st_size, st.st_mtime_ns, harness, _signature(clock_path))
|
|
723
|
+
if key in _LIVE:
|
|
724
|
+
return dict(_LIVE[key])
|
|
725
|
+
base = max(0, st.st_size - LIVE_TAIL)
|
|
726
|
+
try:
|
|
727
|
+
with open(log, 'rb') as fh:
|
|
728
|
+
fh.seek(base)
|
|
729
|
+
data = fh.read(LIVE_TAIL)
|
|
730
|
+
except OSError:
|
|
731
|
+
return None
|
|
732
|
+
if base:
|
|
733
|
+
skip = data.find(b'\n') + 1
|
|
734
|
+
data, base = data[skip:], base + skip
|
|
735
|
+
data = data[:data.rfind(b'\n') + 1] # a line still being written is not an event yet
|
|
736
|
+
text = data.decode('utf-8', 'replace')
|
|
737
|
+
fmt = detect(text)
|
|
738
|
+
call = None
|
|
739
|
+
if fmt in ('codex', 'claude', 'opencode'):
|
|
740
|
+
events = PARSERS[fmt](text)[0]
|
|
741
|
+
running = next((ev for ev in reversed(events) if ev.get('running')), None)
|
|
742
|
+
if running is not None:
|
|
743
|
+
at = running.get('_at')
|
|
744
|
+
last_line = at is not None and at >= len(data)
|
|
745
|
+
_time([running], clock_of(log), base)
|
|
746
|
+
started = running.get('t')
|
|
747
|
+
if started is None and last_line:
|
|
748
|
+
started = st.st_mtime
|
|
749
|
+
call = dict(kind=running['kind'], command=running.get('command'), tool=running.get('tool'), started=started)
|
|
750
|
+
answer = dict(call=call, last_output_at=st.st_mtime)
|
|
751
|
+
if len(_LIVE) > 64:
|
|
752
|
+
_LIVE.clear()
|
|
753
|
+
_LIVE[key] = answer
|
|
754
|
+
return dict(answer)
|
|
755
|
+
|
|
756
|
+
|
|
586
757
|
def _signature(path):
|
|
587
758
|
try:
|
|
588
759
|
st = path.stat() if path is not None else None
|