memindex 1.4.5 → 1.5.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/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.qoder-plugin/plugin.json +1 -1
- package/README.md +8 -2
- package/dist/bug-report/index.js +94 -83
- package/dist/npx-cli/index.js +300 -299
- package/dist/opencode-plugin/index.js +40 -40
- package/dist/pi-extension/TYPEBOX-PROVENANCE.json +4 -4
- package/dist/pi-extension/index.js +9 -9
- package/integrations/dsh/lib/index.js +19 -19
- package/integrations/dsh/package.json +1 -1
- package/integrations/openclaw/install.sh +16 -10
- package/integrations/openclaw/openclaw.plugin.json +1 -5
- package/integrations/openclaw/test-install.sh +20 -13
- package/integrations/pi/TYPEBOX-PROVENANCE.json +4 -4
- package/package.json +5 -5
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin/.codex-plugin/plugin.json +1 -1
- package/plugin/.qoder-plugin/plugin.json +1 -1
- package/plugin/hooks/codex-hooks.json +12 -0
- package/plugin/package.json +1 -1
- package/plugin/scripts/context-generator.cjs +65 -63
- package/plugin/scripts/context-generator.cjs.map +3 -3
- package/plugin/scripts/mcp-server.cjs +71 -70
- package/plugin/scripts/mcp-server.cjs.map +3 -3
- package/plugin/scripts/statusline.cjs +1 -1
- package/plugin/scripts/transcript-watcher.cjs +36 -35
- package/plugin/scripts/transcript-watcher.cjs.map +3 -3
- package/plugin/scripts/worker-service.cjs +714 -674
- package/plugin/scripts/worker-service.cjs.map +3 -3
- package/plugin/skills/cloud-sync/SKILL.md +5 -5
- package/plugin/skills/distill/SKILL.md +22 -0
- package/plugin/skills/how-it-works/SKILL.md +1 -1
- package/plugin/skills/how-it-works/onboarding-explainer.md +1 -1
- package/plugin/skills/import-chats/SKILL.md +22 -0
- package/plugin/skills/import-jira/SKILL.md +22 -0
- package/plugin/skills/import-reviews/SKILL.md +22 -0
- package/plugin/skills/mode-creator/SKILL.md +7 -1
- package/plugin/skills/pin/SKILL.md +26 -0
- package/plugin/skills/savings/SKILL.md +22 -0
- package/plugin/skills/timeline-report/SKILL.md +8 -8
- package/plugin/skills/weekly-digests/SKILL.md +5 -5
- package/plugin/skills/worker-request.mjs +45 -0
- package/plugin/sqlite/SessionStore.js +109 -109
- package/plugin/sqlite/SessionStore.js.map +3 -3
- package/plugin/sqlite/observations/files.js +12 -9
- package/plugin/sqlite/observations/files.js.map +3 -3
- package/plugin/ui/viewer-bundle.js +18 -18
- package/integrations/openclaw/skills/agent-cost-report/CHECKSUMS.txt +0 -50
- package/integrations/openclaw/skills/agent-cost-report/SKILL.md +0 -241
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/__init__.py +0 -1
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/behavior.py +0 -303
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/classify.py +0 -83
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/costs.py +0 -119
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/devices.py +0 -88
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/evidence.py +0 -160
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/labels.py +0 -115
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/measure.py +0 -89
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/mistakes.py +0 -310
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/patterns.py +0 -391
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/period.py +0 -98
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/prices.py +0 -95
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/render.py +0 -586
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rollup.py +0 -355
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rules.json +0 -4
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rules.py +0 -76
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/sample.py +0 -45
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/snapshot.py +0 -53
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/sync.py +0 -79
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/transcripts.py +0 -126
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr/wins.py +0 -341
- package/integrations/openclaw/skills/agent-cost-report/scripts/acr.py +0 -219
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/_db.py +0 -49
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/_paths.py +0 -9
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/codex/sessions/2026/09/18/rollout-codex-1.jsonl +0 -4
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/make_report_fixtures.py +0 -46
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/prices_sample.json +0 -6
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-a/sess-aaaa.jsonl +0 -8
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-b/sess-bbbb.jsonl +0 -1
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-1day.json +0 -823
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-30day.json +0 -2213
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-7day.json +0 -1252
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-empty.json +0 -536
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-session.json +0 -662
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_behavior.py +0 -299
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_devices.py +0 -99
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_labels.py +0 -96
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_measure.py +0 -120
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_pdf_windows.py +0 -54
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_period.py +0 -137
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_prices.py +0 -126
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_render.py +0 -238
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_render_encoding.py +0 -70
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_rollup.py +0 -263
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_snapshot.py +0 -63
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_sync.py +0 -52
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_transcript_encoding.py +0 -109
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_transcripts.py +0 -151
- package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_wins.py +0 -222
- package/plugin/skills/agent-cost-report/CHECKSUMS.txt +0 -50
- package/plugin/skills/agent-cost-report/SKILL.md +0 -241
- package/plugin/skills/agent-cost-report/scripts/acr/__init__.py +0 -1
- package/plugin/skills/agent-cost-report/scripts/acr/behavior.py +0 -303
- package/plugin/skills/agent-cost-report/scripts/acr/classify.py +0 -83
- package/plugin/skills/agent-cost-report/scripts/acr/costs.py +0 -119
- package/plugin/skills/agent-cost-report/scripts/acr/devices.py +0 -88
- package/plugin/skills/agent-cost-report/scripts/acr/evidence.py +0 -160
- package/plugin/skills/agent-cost-report/scripts/acr/labels.py +0 -115
- package/plugin/skills/agent-cost-report/scripts/acr/measure.py +0 -89
- package/plugin/skills/agent-cost-report/scripts/acr/mistakes.py +0 -310
- package/plugin/skills/agent-cost-report/scripts/acr/patterns.py +0 -391
- package/plugin/skills/agent-cost-report/scripts/acr/period.py +0 -98
- package/plugin/skills/agent-cost-report/scripts/acr/prices.py +0 -95
- package/plugin/skills/agent-cost-report/scripts/acr/render.py +0 -586
- package/plugin/skills/agent-cost-report/scripts/acr/rollup.py +0 -355
- package/plugin/skills/agent-cost-report/scripts/acr/rules.json +0 -4
- package/plugin/skills/agent-cost-report/scripts/acr/rules.py +0 -76
- package/plugin/skills/agent-cost-report/scripts/acr/sample.py +0 -45
- package/plugin/skills/agent-cost-report/scripts/acr/snapshot.py +0 -53
- package/plugin/skills/agent-cost-report/scripts/acr/sync.py +0 -79
- package/plugin/skills/agent-cost-report/scripts/acr/transcripts.py +0 -126
- package/plugin/skills/agent-cost-report/scripts/acr/wins.py +0 -341
- package/plugin/skills/agent-cost-report/scripts/acr.py +0 -219
- package/plugin/skills/agent-cost-report/scripts/tests/_db.py +0 -49
- package/plugin/skills/agent-cost-report/scripts/tests/_paths.py +0 -9
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/codex/sessions/2026/09/18/rollout-codex-1.jsonl +0 -4
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/make_report_fixtures.py +0 -46
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/prices_sample.json +0 -6
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-a/sess-aaaa.jsonl +0 -8
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-b/sess-bbbb.jsonl +0 -1
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-1day.json +0 -823
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-30day.json +0 -2213
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-7day.json +0 -1252
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-empty.json +0 -536
- package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-session.json +0 -662
- package/plugin/skills/agent-cost-report/scripts/tests/test_behavior.py +0 -299
- package/plugin/skills/agent-cost-report/scripts/tests/test_devices.py +0 -99
- package/plugin/skills/agent-cost-report/scripts/tests/test_labels.py +0 -96
- package/plugin/skills/agent-cost-report/scripts/tests/test_measure.py +0 -120
- package/plugin/skills/agent-cost-report/scripts/tests/test_pdf_windows.py +0 -54
- package/plugin/skills/agent-cost-report/scripts/tests/test_period.py +0 -137
- package/plugin/skills/agent-cost-report/scripts/tests/test_prices.py +0 -126
- package/plugin/skills/agent-cost-report/scripts/tests/test_render.py +0 -238
- package/plugin/skills/agent-cost-report/scripts/tests/test_render_encoding.py +0 -70
- package/plugin/skills/agent-cost-report/scripts/tests/test_rollup.py +0 -263
- package/plugin/skills/agent-cost-report/scripts/tests/test_snapshot.py +0 -63
- package/plugin/skills/agent-cost-report/scripts/tests/test_sync.py +0 -52
- package/plugin/skills/agent-cost-report/scripts/tests/test_transcript_encoding.py +0 -109
- package/plugin/skills/agent-cost-report/scripts/tests/test_transcripts.py +0 -151
- package/plugin/skills/agent-cost-report/scripts/tests/test_wins.py +0 -222
- package/plugin/skills/babysit/SKILL.md +0 -87
- package/plugin/skills/design-is/SKILL.md +0 -312
- package/plugin/skills/do/SKILL.md +0 -45
- package/plugin/skills/learn-codebase/SKILL.md +0 -21
- package/plugin/skills/make-plan/SKILL.md +0 -67
- package/plugin/skills/oh-my-issues/SKILL.md +0 -226
- package/plugin/skills/pathfinder/SKILL.md +0 -111
- package/plugin/skills/screenpipe/SKILL.md +0 -78
- package/plugin/skills/screenpipe/screenpipe.mjs +0 -196
- package/plugin/skills/smart-explore/SKILL.md +0 -193
- package/plugin/skills/standup/SKILL.md +0 -142
- package/plugin/skills/standup/agent-brief.md +0 -47
- package/plugin/skills/standup/standup.mjs +0 -586
- package/plugin/skills/what-the/SKILL.md +0 -6
- package/plugin/skills/wowerpoint/SKILL.md +0 -205
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
"""Measured token usage from Claude Code transcripts (plan Phase 1.4).
|
|
2
|
-
|
|
3
|
-
Claude Code reader copied verbatim from parse_transcripts.py:7-38
|
|
4
|
-
and the Codex reader from :39-57, then extended (plan 1.4) with `cwd`, `cache_write_5m`
|
|
5
|
-
(read directly from cache_creation.ephemeral_5m_input_tokens when present, else
|
|
6
|
-
cache_write - cache_write_1h as weekly_report.py:94) and `device = "local"`.
|
|
7
|
-
|
|
8
|
-
Every transcript is streamed line by line; no file is ever read whole.
|
|
9
|
-
"""
|
|
10
|
-
import datetime as dt
|
|
11
|
-
import glob
|
|
12
|
-
import json
|
|
13
|
-
import os
|
|
14
|
-
import platform
|
|
15
|
-
|
|
16
|
-
from .period import PT, session_block
|
|
17
|
-
|
|
18
|
-
CLAUDE_GLOB = os.path.join("~", ".claude", "projects", "**", "*.jsonl")
|
|
19
|
-
CODEX_GLOB = os.path.join("~", ".codex", "sessions", "**", "*.jsonl")
|
|
20
|
-
DEVICE = "local"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def host_label():
|
|
24
|
-
"""platform.node(); never a device UUID from settings."""
|
|
25
|
-
return platform.node()
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
def ts(x):
|
|
29
|
-
return dt.datetime.fromisoformat(x.replace("Z", "+00:00"))
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def _project_dir(f, pattern):
|
|
33
|
-
"""Project dir = path segment after `/.claude/projects/` (parse_transcripts.py:20).
|
|
34
|
-
For a non-default glob root (tests) the first segment relative to the root is the same thing."""
|
|
35
|
-
if "/.claude/projects/" in f:
|
|
36
|
-
return f.split("/.claude/projects/")[1].split("/")[0]
|
|
37
|
-
root = os.path.expanduser(pattern).split("**")[0]
|
|
38
|
-
return os.path.relpath(f, root).split(os.sep)[0]
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def collect_claude(s, e, session=None, pattern=CLAUDE_GLOB):
|
|
42
|
-
"""Claude Code assistant lines in [s, e) deduped on (message.id, requestId).
|
|
43
|
-
|
|
44
|
-
s/e: aware datetimes (end exclusive), or None when `session` is given (no period filter,
|
|
45
|
-
plan 1.2). Returns (rows, stats)."""
|
|
46
|
-
seen = set(); rows = []; dropped = 0
|
|
47
|
-
files = [f for f in glob.glob(os.path.expanduser(pattern), recursive=True)
|
|
48
|
-
if s is None or dt.datetime.fromtimestamp(os.path.getmtime(f), PT) >= s]
|
|
49
|
-
for f in files:
|
|
50
|
-
proj = _project_dir(f, pattern)
|
|
51
|
-
with open(f, encoding="utf-8", errors="ignore") as fh:
|
|
52
|
-
for line in fh:
|
|
53
|
-
if '"usage"' not in line: continue
|
|
54
|
-
if session is not None and session not in line: continue
|
|
55
|
-
try: d = json.loads(line)
|
|
56
|
-
except Exception: continue
|
|
57
|
-
m = d.get("message") or {}
|
|
58
|
-
u = m.get("usage")
|
|
59
|
-
if d.get("type") != "assistant" or not u or not d.get("timestamp"): continue
|
|
60
|
-
sid = d.get("sessionId") or d.get("session_id")
|
|
61
|
-
if session is not None and sid != session: continue
|
|
62
|
-
t = ts(d["timestamp"])
|
|
63
|
-
if s is not None and not (s <= t < e): continue
|
|
64
|
-
key = (m.get("id"), d.get("requestId"))
|
|
65
|
-
if key in seen: dropped += 1; continue
|
|
66
|
-
seen.add(key)
|
|
67
|
-
cc = u.get("cache_creation") or {}
|
|
68
|
-
cache_write = u.get("cache_creation_input_tokens", 0) or 0
|
|
69
|
-
cache_write_1h = cc.get("ephemeral_1h_input_tokens", 0) or 0
|
|
70
|
-
cw5 = cc.get("ephemeral_5m_input_tokens")
|
|
71
|
-
cache_write_5m = cw5 if cw5 is not None else cache_write - cache_write_1h
|
|
72
|
-
rows.append(dict(src="claude-code", file=f, dir=proj, cwd=d.get("cwd"), session=sid,
|
|
73
|
-
sidechain=bool(d.get("isSidechain")), model=m.get("model"), ts=t.astimezone(PT).isoformat(),
|
|
74
|
-
input=u.get("input_tokens", 0) or 0, output=u.get("output_tokens", 0) or 0,
|
|
75
|
-
cache_write=cache_write, cache_write_1h=cache_write_1h, cache_write_5m=cache_write_5m,
|
|
76
|
-
cache_read=u.get("cache_read_input_tokens", 0) or 0,
|
|
77
|
-
cost_usd_reported=d.get("costUSD"), device=DEVICE))
|
|
78
|
-
return rows, dict(files_seen=len(files), dedup_dropped=dropped)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def collect_codex(s, e, session=None, pattern=CODEX_GLOB):
|
|
82
|
-
"""Codex: token_count events carry a cumulative total_token_usage per session file (parse_transcripts.py:39-57
|
|
83
|
-
took the last one). Each event becomes one row holding the growth since the previous event, so usage before
|
|
84
|
-
the window is subtracted and every day is charged its own tokens; a session that starts before the window
|
|
85
|
-
contributes only its in-window growth."""
|
|
86
|
-
rows = []; files_seen = 0
|
|
87
|
-
for f in glob.glob(os.path.expanduser(pattern), recursive=True):
|
|
88
|
-
if s is not None and dt.datetime.fromtimestamp(os.path.getmtime(f), PT) < s: continue
|
|
89
|
-
if session is not None and os.path.basename(f) != session: continue
|
|
90
|
-
files_seen += 1
|
|
91
|
-
prev = dict(input_tokens=0, cached_input_tokens=0, output_tokens=0); model = None
|
|
92
|
-
with open(f, encoding="utf-8", errors="ignore") as fh:
|
|
93
|
-
for line in fh:
|
|
94
|
-
try: d = json.loads(line)
|
|
95
|
-
except Exception: continue
|
|
96
|
-
p = d.get("payload") or {}
|
|
97
|
-
if d.get("type") == "turn_context": model = p.get("model", model)
|
|
98
|
-
if p.get("type") == "token_count" and p.get("info"):
|
|
99
|
-
t = ts(d["timestamp"]); u = p["info"].get("total_token_usage") or {}
|
|
100
|
-
cur = {k: int(u.get(k, 0) or 0) for k in prev}
|
|
101
|
-
delta = {k: max(cur[k] - prev[k], 0) for k in prev}; prev = cur
|
|
102
|
-
if not any(delta.values()) or (s is not None and not (s <= t < e)): continue
|
|
103
|
-
rows.append(dict(src="codex", file=f, dir="codex", cwd=None, session=os.path.basename(f), sidechain=False, model=model,
|
|
104
|
-
ts=t.astimezone(PT).isoformat(), input=max(delta["input_tokens"] - delta["cached_input_tokens"], 0),
|
|
105
|
-
output=delta["output_tokens"], cache_write=0, cache_write_1h=0, cache_write_5m=0, cache_read=delta["cached_input_tokens"],
|
|
106
|
-
cost_usd_reported=None, device=DEVICE))
|
|
107
|
-
return rows, dict(files_seen=files_seen)
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
def collect(window=None, session=None, claude_glob=CLAUDE_GLOB, codex_glob=CODEX_GLOB, window_block=None):
|
|
111
|
-
"""Run both readers. `window` is a period.Window, or None with `session` set.
|
|
112
|
-
Returns the usage.json document: {window, collector, rows}."""
|
|
113
|
-
if window is None and session is None:
|
|
114
|
-
raise ValueError("collect needs a window or a session id")
|
|
115
|
-
s, e = (window.start, window.end) if window is not None else (None, None)
|
|
116
|
-
claude_rows, cstats = collect_claude(s, e, session=session, pattern=claude_glob)
|
|
117
|
-
codex_rows, xstats = collect_codex(s, e, session=session, pattern=codex_glob)
|
|
118
|
-
rows = claude_rows + codex_rows
|
|
119
|
-
if window_block is None:
|
|
120
|
-
window_block = window.block() if window is not None else session_block(session)
|
|
121
|
-
wblock = window_block
|
|
122
|
-
collector = dict(host_label=host_label(), glob=[claude_glob, codex_glob],
|
|
123
|
-
files_seen=cstats["files_seen"] + xstats["files_seen"],
|
|
124
|
-
files_seen_by_src={"claude-code": cstats["files_seen"], "codex": xstats["files_seen"]},
|
|
125
|
-
rows=len(rows), dedup_dropped=cstats["dedup_dropped"], window=wblock)
|
|
126
|
-
return dict(window=wblock, collector=collector, rows=rows)
|
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
"""Wins and what each cost (plan Phase 2.8, G11/G12/G13).
|
|
2
|
-
|
|
3
|
-
A win is a merged PR or a published version (praise wins are a Phase 2B hook, see detect_praise).
|
|
4
|
-
Sources: successful `gh pr merge` / `npm publish` / `gh release create` / tag-push tool calls found by
|
|
5
|
-
streaming the window's transcripts, and ship observations (evidence.SHIP_RE, weekly_report.py:65,74-75)
|
|
6
|
-
that name a PR number or a published version. Deduped by key repo#number or package@version.
|
|
7
|
-
Optional read-only confirmation: `gh pr view <n> --repo <repo> --json mergedAt,url,title,commits`
|
|
8
|
-
(one call per candidate PR; never a write command). mergedAt must fall in the window.
|
|
9
|
-
|
|
10
|
-
Cost: "unmeasured" (every usd null) unless the PR's commits carry `Claude-Session: <bare id>`
|
|
11
|
-
trailers (R4, G13: a `Session:` line in a PR body is ignored; non-bare values rejected). Then each turn
|
|
12
|
-
of a linked session is assigned to the first linked win merged after it, so no turn counts twice.
|
|
13
|
-
There is no lineage fallback (G11). Titles come from the PR, the package, or the observation.
|
|
14
|
-
"""
|
|
15
|
-
import collections
|
|
16
|
-
import datetime as dt
|
|
17
|
-
import json
|
|
18
|
-
import re
|
|
19
|
-
import subprocess
|
|
20
|
-
|
|
21
|
-
from .labels import active_minutes
|
|
22
|
-
from .period import PT
|
|
23
|
-
|
|
24
|
-
KEYS = ("gh pr merge", "npm publish", "gh release create", "git push")
|
|
25
|
-
SEGMENT_SPLIT = re.compile(r"[;&|\n]")
|
|
26
|
-
MERGE_CMD = re.compile(r"^gh\s+pr\s+merge\b(?P<rest>.*)")
|
|
27
|
-
PUBLISH_CMD = re.compile(r"^npm\s+publish\b")
|
|
28
|
-
RELEASE_CMD = re.compile(r"^gh\s+release\s+create\s+(?P<tag>[\w.-]+)")
|
|
29
|
-
TAG_PUSH_CMD = re.compile(r"^git\s+push\b(?P<rest>.*)")
|
|
30
|
-
FAILED = re.compile(r"^\W{0,3}(error|fatal|failed|graphql:|npm err!|could not|permission denied|rejected|x pull request)", re.I | re.M)
|
|
31
|
-
PR_NUM = re.compile(r"/pull/(\d+)|(?<![\w.#-])(\d+)(?![\w.-])")
|
|
32
|
-
REPO_FLAG = re.compile(r"(?:--repo|-R)[ =](\S+)")
|
|
33
|
-
PR_IN_TITLE = re.compile(r"(?:\bPR|pull request)\s*#\s*(\d+)", re.I)
|
|
34
|
-
PUBLISH_TITLE = re.compile(r"\b(published|released|npm publish|tagged)\b", re.I)
|
|
35
|
-
PKG_VER = re.compile(r"(@?[\w][\w./-]*)@(\d+\.\d+\.\d+[\w.-]*)")
|
|
36
|
-
VERSION = re.compile(r"\bv?(\d+\.\d+\.\d+(?:[-.][\w.]+)?)\b")
|
|
37
|
-
REPO_URL = re.compile(r"github\.com[:/]([\w.-]+)/([\w.-]+?)(?:\.git)?(?:[/\s]|$)")
|
|
38
|
-
TRAILER = re.compile(r"^Claude-Session:[ \t]*(.*?)[ \t]*$", re.M)
|
|
39
|
-
BARE_ID = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*$")
|
|
40
|
-
LINKED_LABEL = "ESTIMATED · session-linked"
|
|
41
|
-
ATTRIBUTION = "Claude-Session commit trailers (R4); each turn assigned to the first linked win merged after it"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def classify_command(cmd):
|
|
45
|
-
"""The first merge/publish verb that starts a command segment (heredoc bodies and quoted prose never count)."""
|
|
46
|
-
for seg in SEGMENT_SPLIT.split(cmd.split("<<")[0]):
|
|
47
|
-
seg = seg.strip().lstrip("( ").strip()
|
|
48
|
-
m = MERGE_CMD.match(seg)
|
|
49
|
-
if m:
|
|
50
|
-
rest = m.group("rest"); n = next((a or b for a, b in PR_NUM.findall(rest) if a or b), None)
|
|
51
|
-
rf = REPO_FLAG.search(rest); ru = REPO_URL.search(rest)
|
|
52
|
-
repo = rf.group(1) if rf else (f"{ru.group(1)}/{ru.group(2)}" if ru else None)
|
|
53
|
-
return dict(kind="pr", number=int(n) if n else None, repo=repo, cmd=cmd[:200])
|
|
54
|
-
if PUBLISH_CMD.match(seg): return dict(kind="publish", how="npm publish", cmd=cmd[:200])
|
|
55
|
-
m = RELEASE_CMD.match(seg)
|
|
56
|
-
if m: return dict(kind="publish", how="gh release create", tag=m.group("tag"), cmd=cmd[:200])
|
|
57
|
-
m = TAG_PUSH_CMD.match(seg)
|
|
58
|
-
if m:
|
|
59
|
-
rest = m.group("rest"); v = VERSION.search(rest)
|
|
60
|
-
if "--tags" in rest or v: return dict(kind="publish", how="git push tag", tag=v.group(0) if v else None, cmd=cmd[:200])
|
|
61
|
-
return None
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def _result_text(c, tur):
|
|
65
|
-
parts = []
|
|
66
|
-
if isinstance(tur, dict): parts += [str(tur.get("stdout") or ""), str(tur.get("stderr") or "")]
|
|
67
|
-
cc = c.get("content")
|
|
68
|
-
if isinstance(cc, str): parts.append(cc)
|
|
69
|
-
elif isinstance(cc, list): parts += [str(x.get("text") or "") for x in cc if isinstance(x, dict)]
|
|
70
|
-
return "\n".join(parts)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def _ts_ms(iso):
|
|
74
|
-
try: return int(dt.datetime.fromisoformat(iso.replace("Z", "+00:00")).timestamp() * 1000)
|
|
75
|
-
except Exception: return None
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def scan_transcripts(files, s_ms=None, e_ms=None):
|
|
79
|
-
"""Stream each transcript line by line; yield successful merge/publish tool calls inside the window."""
|
|
80
|
-
for f in files:
|
|
81
|
-
pending = {}
|
|
82
|
-
try: fh = open(f, encoding="utf-8", errors="ignore")
|
|
83
|
-
except OSError: continue
|
|
84
|
-
with fh:
|
|
85
|
-
for line in fh:
|
|
86
|
-
if not pending and not any(k in line for k in KEYS): continue
|
|
87
|
-
if '"tool_use"' not in line and '"tool_result"' not in line: continue
|
|
88
|
-
try: d = json.loads(line)
|
|
89
|
-
except Exception: continue
|
|
90
|
-
content = (d.get("message") or {}).get("content")
|
|
91
|
-
if not isinstance(content, list): continue
|
|
92
|
-
if d.get("type") == "assistant":
|
|
93
|
-
for c in content:
|
|
94
|
-
if isinstance(c, dict) and c.get("type") == "tool_use" and c.get("name") == "Bash":
|
|
95
|
-
cand = classify_command(str((c.get("input") or {}).get("command") or ""))
|
|
96
|
-
if cand:
|
|
97
|
-
cand.update(tool_use_id=c.get("id"), ts_ms=_ts_ms(d.get("timestamp") or ""), session=d.get("sessionId"),
|
|
98
|
-
cwd=d.get("cwd"), file=f, source="transcript")
|
|
99
|
-
pending[c.get("id")] = cand
|
|
100
|
-
elif d.get("type") == "user" and pending:
|
|
101
|
-
for c in content:
|
|
102
|
-
if isinstance(c, dict) and c.get("type") == "tool_result" and c.get("tool_use_id") in pending:
|
|
103
|
-
cand = pending.pop(c["tool_use_id"]); text = _result_text(c, d.get("toolUseResult"))
|
|
104
|
-
if c.get("is_error") or FAILED.search(text): continue
|
|
105
|
-
if cand["ts_ms"] is None or (s_ms is not None and not (s_ms <= cand["ts_ms"] < e_ms)): continue
|
|
106
|
-
cand["result"] = text[:2000]; yield cand
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
def parse_repo(url):
|
|
110
|
-
m = REPO_URL.search(url or "")
|
|
111
|
-
return f"{m.group(1)}/{m.group(2)}" if m else None
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def git_remote(cwd):
|
|
115
|
-
"""Read-only: `git -C cwd remote get-url origin`; None when cwd is gone or not a repo."""
|
|
116
|
-
try:
|
|
117
|
-
r = subprocess.run(["git", "-C", cwd, "remote", "get-url", "origin"], capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=5)
|
|
118
|
-
return parse_repo(r.stdout) if r.returncode == 0 else None
|
|
119
|
-
except Exception: return None
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
def gh_pr_view(number, repo):
|
|
123
|
-
"""Read-only: gh pr view <n> --repo <repo> --json mergedAt,url,title,commits. Returns dict or None."""
|
|
124
|
-
try:
|
|
125
|
-
r = subprocess.run(["gh", "pr", "view", str(number), "--repo", repo, "--json", "mergedAt,url,title,commits"],
|
|
126
|
-
capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=25)
|
|
127
|
-
return json.loads(r.stdout) if r.returncode == 0 and r.stdout.strip() else None
|
|
128
|
-
except Exception: return None
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
WINS_REPOS = ("imogenlabs/memindex",)
|
|
132
|
-
GH_SOURCE = "github_merged_prs"
|
|
133
|
-
GH_LIST_LIMIT = 200
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def _gh_status(returncode, stderr):
|
|
137
|
-
"""Map a failed `gh` call to an honest status; never a number."""
|
|
138
|
-
t = (stderr or "").lower()
|
|
139
|
-
if "rate limit" in t or "api rate limit" in t or "secondary rate" in t: return "rate-limited"
|
|
140
|
-
if "auth login" in t or "authentication" in t or "not logged in" in t or "http 401" in t or "bad credentials" in t: return "unauthenticated"
|
|
141
|
-
return f"gh error (exit {returncode})"
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
def gh_merged_prs(repo, s_ms, e_ms, run=subprocess.run):
|
|
145
|
-
"""Read-only: gh pr list --repo <repo> --state merged --json number,title,mergedAt,url, searched by merge date
|
|
146
|
-
(UTC days covering the PT window) and then filtered by mergedAt inside [s_ms, e_ms). Returns (rows, status)
|
|
147
|
-
with status "ok" or a reason: "gh missing" | "unauthenticated" | "rate-limited" | "gh error (exit n)"."""
|
|
148
|
-
a = dt.datetime.fromtimestamp(s_ms / 1000, dt.timezone.utc).date() - dt.timedelta(days=1)
|
|
149
|
-
b = dt.datetime.fromtimestamp(e_ms / 1000, dt.timezone.utc).date() + dt.timedelta(days=1)
|
|
150
|
-
cmd = ["gh", "pr", "list", "--repo", repo, "--state", "merged", "--limit", str(GH_LIST_LIMIT), "--json", "number,title,mergedAt,url",
|
|
151
|
-
"--search", f"merged:{a.isoformat()}..{b.isoformat()}"]
|
|
152
|
-
try:
|
|
153
|
-
r = run(cmd, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=40)
|
|
154
|
-
except FileNotFoundError: return [], "gh missing"
|
|
155
|
-
except (subprocess.TimeoutExpired, OSError) as ex: return [], f"gh error ({type(ex).__name__})"
|
|
156
|
-
if r.returncode != 0: return [], _gh_status(r.returncode, r.stderr)
|
|
157
|
-
try: rows = json.loads(r.stdout or "[]")
|
|
158
|
-
except ValueError: return [], "gh error (unparseable output)"
|
|
159
|
-
out = []
|
|
160
|
-
for x in rows:
|
|
161
|
-
if not x.get("mergedAt") or not x.get("number"): continue
|
|
162
|
-
ms = _ts_ms(x["mergedAt"])
|
|
163
|
-
if s_ms <= ms < e_ms: out.append(dict(number=int(x["number"]), title=x.get("title") or "", url=x.get("url"), ts_ms=ms))
|
|
164
|
-
return out, "ok"
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
def gh_list_candidates(repos, s_ms, e_ms, lister=gh_merged_prs):
|
|
168
|
-
"""Candidates from the merged-PR list per repo plus a per-repo status for the report."""
|
|
169
|
-
cands = []; status = {}
|
|
170
|
-
for repo in repos:
|
|
171
|
-
rows, st = lister(repo, s_ms, e_ms)
|
|
172
|
-
status[repo] = dict(status=st, found=len(rows) if st == "ok" else None)
|
|
173
|
-
for x in rows:
|
|
174
|
-
cands.append(dict(kind="pr", source=GH_SOURCE, repo=repo, number=x["number"], title=x["title"], url=x["url"], ts_ms=x["ts_ms"],
|
|
175
|
-
project=repo.split("/")[-1], sessions=[], evidence_ids=[], gh_listed=True))
|
|
176
|
-
return cands, status
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
def session_trailers(message):
|
|
180
|
-
"""Bare content_session_ids named by `Claude-Session:` trailers in one commit message (G13).
|
|
181
|
-
Non-bare values (URLs, ids with spaces or parentheses, anything else) are rejected; `Session:`
|
|
182
|
-
lines and PR bodies are never read."""
|
|
183
|
-
out = []
|
|
184
|
-
for v in TRAILER.findall(message or ""):
|
|
185
|
-
if v and BARE_ID.match(v) and v not in out: out.append(v)
|
|
186
|
-
return out
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
def detect_praise(*_args, **_kw):
|
|
190
|
-
"""Phase 2B hook (plan 2B.1 human-vs-bot tagger, 2B.4 sarcasm filter). Until those exist no user
|
|
191
|
-
prompt is read for praise, so this returns [] and wins.praise_n is 0. Praise wins are never costed."""
|
|
192
|
-
return []
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
def _pt_iso(ms):
|
|
196
|
-
return dt.datetime.fromtimestamp(ms / 1000, PT).strftime("%Y-%m-%dT%H:%M%z")
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
def observation_candidates(ship_obs, project_repo):
|
|
200
|
-
"""ship_obs: dicts(id, title, ts_ms, project, memory_session_id, content_session_id)."""
|
|
201
|
-
for o in ship_obs:
|
|
202
|
-
t = o["title"] or ""; base = (o["project"] or "").split("/")[0]
|
|
203
|
-
m = PR_IN_TITLE.search(t)
|
|
204
|
-
if m:
|
|
205
|
-
yield dict(kind="pr", number=int(m.group(1)), repo=project_repo(o["project"]), project=o["project"], ts_ms=o["ts_ms"],
|
|
206
|
-
title=t, evidence_ids=[o["id"]], sessions=[o["content_session_id"]], source="ship_observation"); continue
|
|
207
|
-
if PUBLISH_TITLE.search(t):
|
|
208
|
-
pv = PKG_VER.search(t); v = VERSION.search(t)
|
|
209
|
-
if pv: key = f"{pv.group(1)}@{pv.group(2)}"
|
|
210
|
-
elif v and base: key = f"{base}@{v.group(1)}"
|
|
211
|
-
else: continue
|
|
212
|
-
yield dict(kind="publish", key=key, project=o["project"], ts_ms=o["ts_ms"], title=t, evidence_ids=[o["id"]],
|
|
213
|
-
sessions=[o["content_session_id"]], source="ship_observation")
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
def _key(c, dropped):
|
|
217
|
-
if c["kind"] == "pr":
|
|
218
|
-
if not c.get("number"): dropped.append(dict(source=c["source"], reason="gh pr merge without a PR number", cmd=c.get("cmd"))); return None
|
|
219
|
-
return f"{c.get('repo') or (c.get('project') or '?').split('/')[0]}#{c['number']}"
|
|
220
|
-
if c.get("key"): return c["key"]
|
|
221
|
-
text = c.get("result") or ""; pv = PKG_VER.search(text)
|
|
222
|
-
if c.get("how") == "npm publish":
|
|
223
|
-
if pv: return f"{pv.group(1)}@{pv.group(2)}"
|
|
224
|
-
dropped.append(dict(source=c["source"], reason="npm publish result names no package@version", cmd=c.get("cmd"))); return None
|
|
225
|
-
if c.get("tag"): return f"{c.get('repo') or c.get('project') or '?'}@{c['tag']}"
|
|
226
|
-
dropped.append(dict(source=c["source"], reason="tag push without an identifiable tag", cmd=c.get("cmd"))); return None
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
def build(files, s_ms, e_ms, ship_obs, sessions_by_cs, rows_by_cs, ratio, days, gh=gh_pr_view, remote=git_remote, use_gh=True,
|
|
230
|
-
repos=WINS_REPOS, gh_list=gh_merged_prs):
|
|
231
|
-
"""Returns (wins_block, wins_by_day). sessions_by_cs: cs -> dict(project, memory_session_id, has_transcript,
|
|
232
|
-
observer_tokens, cwd); rows_by_cs: cs -> usage rows with ts_ms and x1e6 (None when unpriced)."""
|
|
233
|
-
repo_cache = {}
|
|
234
|
-
def project_repo(project):
|
|
235
|
-
"""Repo of a project from `git remote` of any transcript cwd in it; a worktree (`repo/wt`) falls back to its base project."""
|
|
236
|
-
if project in repo_cache: return repo_cache[project]
|
|
237
|
-
repo = None; base = (project or "").split("/")[0]
|
|
238
|
-
for cs, s in sessions_by_cs.items():
|
|
239
|
-
if (s.get("project") or "").split("/")[0] == base and s.get("cwd"):
|
|
240
|
-
repo = remote(s["cwd"]) if remote else None
|
|
241
|
-
if repo: break
|
|
242
|
-
repo_cache[project] = repo; return repo
|
|
243
|
-
cands = []; dropped = []; gh_unconfirmed = []
|
|
244
|
-
for c in scan_transcripts(files, s_ms, e_ms):
|
|
245
|
-
s = sessions_by_cs.get(c["session"]) or {}
|
|
246
|
-
c["project"] = s.get("project") or c.get("cwd"); c["sessions"] = [c["session"]]; c["evidence_ids"] = []
|
|
247
|
-
if c["kind"] == "pr" and not c.get("repo"): c["repo"] = (remote(c["cwd"]) if remote and c.get("cwd") else None) or project_repo(c["project"])
|
|
248
|
-
cands.append(c)
|
|
249
|
-
cands += list(observation_candidates(ship_obs, project_repo))
|
|
250
|
-
gh_status = {}
|
|
251
|
-
if use_gh and gh_list and s_ms is not None and e_ms is not None:
|
|
252
|
-
gh_cands, gh_status = gh_list_candidates(repos, s_ms, e_ms, lister=gh_list); cands += gh_cands
|
|
253
|
-
elif repos: gh_status = {r: dict(status="unavailable (gh disabled for this run)", found=None) for r in repos}
|
|
254
|
-
merged = collections.OrderedDict()
|
|
255
|
-
for c in cands:
|
|
256
|
-
key = _key(c, dropped)
|
|
257
|
-
if key is None: continue
|
|
258
|
-
w = merged.get(key)
|
|
259
|
-
if w is None:
|
|
260
|
-
w = merged[key] = dict(win_id=None, kind=c["kind"], key=key, title=c.get("title") or key, url=None, ts_ms=c["ts_ms"],
|
|
261
|
-
project=c.get("project"), repo=c.get("repo"), number=c.get("number"), evidence_ids=[], sessions=[],
|
|
262
|
-
sources=[], confirmed=False, linked_sessions=[], gh_listed=False)
|
|
263
|
-
for eid in c.get("evidence_ids", []):
|
|
264
|
-
if eid not in w["evidence_ids"]: w["evidence_ids"].append(eid)
|
|
265
|
-
for sid in c.get("sessions", []):
|
|
266
|
-
if sid and sid not in w["sessions"]: w["sessions"].append(sid)
|
|
267
|
-
if c["source"] not in w["sources"]: w["sources"].append(c["source"])
|
|
268
|
-
if c.get("gh_listed"):
|
|
269
|
-
w["gh_listed"] = True; w["confirmed"] = True; w["ts_ms"] = c["ts_ms"]; w["url"] = w["url"] or c.get("url"); w["title"] = c.get("title") or w["title"]
|
|
270
|
-
elif c["ts_ms"] and (w["ts_ms"] is None or c["ts_ms"] < w["ts_ms"]): w["ts_ms"] = c["ts_ms"]
|
|
271
|
-
items = []
|
|
272
|
-
for key, w in merged.items():
|
|
273
|
-
if w["kind"] == "pr" and use_gh and gh and w.get("repo") and w.get("number") and not (w["gh_listed"] and w["sources"] == [GH_SOURCE]):
|
|
274
|
-
info = gh(w["number"], w["repo"])
|
|
275
|
-
if info is not None:
|
|
276
|
-
if not info.get("mergedAt"): dropped.append(dict(key=key, reason="gh pr view: not merged")); continue
|
|
277
|
-
w["ts_ms"] = _ts_ms(info["mergedAt"]); w["url"] = info.get("url"); w["title"] = info.get("title") or w["title"]
|
|
278
|
-
w["confirmed"] = True; w["sources"].append("gh_confirm")
|
|
279
|
-
for cm in info.get("commits") or []:
|
|
280
|
-
for sid in session_trailers("\n".join([cm.get("messageHeadline") or "", cm.get("messageBody") or ""])):
|
|
281
|
-
if sid not in w["linked_sessions"]: w["linked_sessions"].append(sid)
|
|
282
|
-
else:
|
|
283
|
-
gh_unconfirmed.append(dict(key=key, reason="gh pr view returned nothing (gh error, no access, or PR not found); kept unconfirmed"))
|
|
284
|
-
if w["ts_ms"] is None: dropped.append(dict(key=key, reason="no timestamp")); continue
|
|
285
|
-
if s_ms is not None and not (s_ms <= w["ts_ms"] < e_ms): dropped.append(dict(key=key, reason="outside window", ts_pt=_pt_iso(w["ts_ms"]))); continue
|
|
286
|
-
items.append(w)
|
|
287
|
-
items.sort(key=lambda w: w["ts_ms"])
|
|
288
|
-
for i, w in enumerate(items, 1): w["win_id"] = f"W-{i}"
|
|
289
|
-
attribute(items, sessions_by_cs, rows_by_cs, ratio)
|
|
290
|
-
praise = detect_praise()
|
|
291
|
-
out = []
|
|
292
|
-
for w in items:
|
|
293
|
-
out.append(dict(win_id=w["win_id"], kind=w["kind"], key=w["key"], title=w["title"], url=w["url"], ts_pt=_pt_iso(w["ts_ms"]),
|
|
294
|
-
day_pt=dt.datetime.fromtimestamp(w["ts_ms"] / 1000, PT).date().isoformat(), project=w["project"],
|
|
295
|
-
evidence_ids=w["evidence_ids"], sessions=w["sessions"], sources=w["sources"], confirmed=w["confirmed"],
|
|
296
|
-
linked_sessions=w["linked_sessions"], usd=w["usd"], cost_status=w["cost_status"], cost_basis=w["cost_basis"],
|
|
297
|
-
cost_label=w["cost_label"], tokens=w["tokens"], sessions_n=w["sessions_n"], turns_n=w["turns_n"],
|
|
298
|
-
active_minutes=w["active_minutes"]))
|
|
299
|
-
linked = any(w["cost_status"] == "session_linked" for w in items)
|
|
300
|
-
total = round(sum(w["usd"] for w in items if w["usd"] is not None), 2) if linked else None
|
|
301
|
-
block = dict(items=out, cost_status="session_linked" if linked else "unmeasured", attribution_method=ATTRIBUTION if linked else None,
|
|
302
|
-
unattributed_usd=None, total_attributed_usd=total, praise_n=len(praise), dropped=dropped, gh_unconfirmed=gh_unconfirmed,
|
|
303
|
-
sources=dict(transcripts="box transcripts (gh pr merge / npm publish / release / tag push)", ship_observations="memindex ship observations",
|
|
304
|
-
**{GH_SOURCE: dict(label="GitHub merged PRs (read-only gh pr list)", repos={r: v for r, v in gh_status.items()},
|
|
305
|
-
status=("ok" if gh_status and all(v["status"] == "ok" for v in gh_status.values()) else "unavailable" if gh_status else "off"),
|
|
306
|
-
found=sum(v["found"] or 0 for v in gh_status.values()) if gh_status else 0)}),
|
|
307
|
-
note="finished line items (shipped/completed) are outcomes, not wins (G12); praise detection is a Phase 2B hook")
|
|
308
|
-
by_day = [dict(day_pt=d, count=sum(1 for w in out if w["day_pt"] == d),
|
|
309
|
-
usd=(round(sum(w["usd"] for w in out if w["day_pt"] == d and w["usd"] is not None), 2) if linked else None),
|
|
310
|
-
win_ids=[w["win_id"] for w in out if w["day_pt"] == d]) for d in days]
|
|
311
|
-
return block, by_day
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
def attribute(items, sessions_by_cs, rows_by_cs, ratio):
|
|
315
|
-
"""Assign each turn of a linked session to the first linked win merged after it (no double counting).
|
|
316
|
-
Replica sessions (no transcript on this box) add an extrapolated portion only when the win is linked
|
|
317
|
-
and a ratio exists; the item is then labeled cost_basis extrapolated. Unlinked wins stay unmeasured."""
|
|
318
|
-
by_session = collections.defaultdict(list)
|
|
319
|
-
for w in items:
|
|
320
|
-
w.update(usd=None, cost_status="unmeasured", cost_basis=None, cost_label="unmeasured (session not linked to PR)",
|
|
321
|
-
tokens=None, sessions_n=0, turns_n=0, active_minutes=None, _x1e6=0, _tok=collections.Counter(), _stamps=[])
|
|
322
|
-
for sid in w["linked_sessions"]: by_session[sid].append(w)
|
|
323
|
-
for sid, ws in by_session.items():
|
|
324
|
-
ws.sort(key=lambda w: w["ts_ms"])
|
|
325
|
-
for w in ws: w["sessions_n"] += 1
|
|
326
|
-
for r in rows_by_cs.get(sid, []):
|
|
327
|
-
tgt = next((w for w in ws if w["ts_ms"] >= r["ts_ms"]), None)
|
|
328
|
-
if tgt is None: continue
|
|
329
|
-
tgt["turns_n"] += 1; tgt["_stamps"].append(r["ts_ms"])
|
|
330
|
-
if r.get("x1e6") is not None: tgt["_x1e6"] += r["x1e6"]
|
|
331
|
-
for f in ("input", "output", "cache_write_5m", "cache_write_1h", "cache_read"): tgt["_tok"][f] += r[f]
|
|
332
|
-
s = sessions_by_cs.get(sid) or {}
|
|
333
|
-
if not s.get("has_transcript") and ratio is not None and s.get("observer_tokens"):
|
|
334
|
-
ws[-1]["_x1e6"] += int(round(s["observer_tokens"] * ratio)); ws[-1]["cost_basis"] = "extrapolated"
|
|
335
|
-
for w in items:
|
|
336
|
-
if w["linked_sessions"]:
|
|
337
|
-
w["usd"] = round(w["_x1e6"] / 1e6, 2); w["cost_status"] = "session_linked"
|
|
338
|
-
w["cost_basis"] = w["cost_basis"] or "estimated_usage"
|
|
339
|
-
w["cost_label"] = LINKED_LABEL + (" · EXTRAPOLATED (low confidence)" if w["cost_basis"] == "extrapolated" else "")
|
|
340
|
-
w["tokens"] = dict(w["_tok"]); w["active_minutes"] = round(active_minutes(w["_stamps"]), 1)
|
|
341
|
-
for k in ("_x1e6", "_tok", "_stamps"): w.pop(k, None)
|