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,83 +0,0 @@
|
|
|
1
|
-
"""Optional cheap classifier pass (plan 2B.4; G5 and G8 settled): OFF by default, enabled per run with
|
|
2
|
-
--classify, hard cap $2.00 per run, regular inference OPENROUTER_API_KEY only (never a management key,
|
|
3
|
-
never read from a settings file). It only sees candidates the heuristics could not settle. Its own
|
|
4
|
-
spend is reported separately ("classifier cost (separate)") and never added to agent cost.
|
|
5
|
-
|
|
6
|
-
Run path: OpenRouter chat completions (OpenAI-compatible, https://openrouter.ai/docs/api-reference/chat-completion),
|
|
7
|
-
urllib only. Input per candidate: the flagged text plus one message before and after, scrubbed, cut to
|
|
8
|
-
1,500 characters. Output JSON {label: yes|no|unsure, pattern, reason (≤20 words)}.
|
|
9
|
-
"""
|
|
10
|
-
import json
|
|
11
|
-
import os
|
|
12
|
-
import random
|
|
13
|
-
import urllib.request
|
|
14
|
-
|
|
15
|
-
from . import behavior, costs
|
|
16
|
-
|
|
17
|
-
DEFAULT_MODEL = "anthropic/claude-haiku-4.5"
|
|
18
|
-
CAP_USD = 2.00
|
|
19
|
-
URL = "https://openrouter.ai/api/v1/chat/completions"
|
|
20
|
-
CANDIDATE_CHARS = 1500
|
|
21
|
-
EST_IN_TOKENS = 1600
|
|
22
|
-
EST_OUT_TOKENS = 60
|
|
23
|
-
QUESTIONS = {
|
|
24
|
-
"frustration": "Is this human message a genuine complaint about the agent's work? Answer yes or no.",
|
|
25
|
-
"P1_invented_gates": "Was this a real gate (something only the user can do: cold Allow click, payment method, wet-ink signature, phone 2FA) or an invented one? yes = invented.",
|
|
26
|
-
"P4_over_engineering": "Was the extra scope asked for? yes = NOT asked for (over-engineering).",
|
|
27
|
-
"P5_not_asked": "Do the actions match the last human instruction? yes = they do NOT match.",
|
|
28
|
-
"P6_fake_output": "Is each number in this text sourced from a tool result shown? yes = unsourced.",
|
|
29
|
-
"P12_unclear": "Which pattern (P1..P11) does this complaint belong to, if any?",
|
|
30
|
-
"S2_hedging": "Was the answer already available to the agent when it hedged? yes = available (hedge counts).",
|
|
31
|
-
"praise": "Is this praise sincere (yes) or sarcastic (no)?",
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
class ClassifierError(RuntimeError):
|
|
36
|
-
pass
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def estimate_usd(n, rate):
|
|
40
|
-
return costs.usd(int(n * (EST_IN_TOKENS * rate["input_usd_per_mtok"] + EST_OUT_TOKENS * rate["output_usd_per_mtok"])))
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def _call(model, prompt, key, url=URL, timeout=60):
|
|
44
|
-
body = json.dumps(dict(model=model, max_tokens=120, temperature=0,
|
|
45
|
-
messages=[dict(role="system", content="Reply with one JSON object: {\"label\": \"yes|no|unsure\", \"pattern\": \"...\", \"reason\": \"<=20 words\"}"),
|
|
46
|
-
dict(role="user", content=prompt)])).encode()
|
|
47
|
-
req = urllib.request.Request(url, data=body, headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"})
|
|
48
|
-
with urllib.request.urlopen(req, timeout=timeout) as resp: d = json.load(resp)
|
|
49
|
-
u = d.get("usage") or {}
|
|
50
|
-
txt = ((d.get("choices") or [{}])[0].get("message") or {}).get("content") or "{}"
|
|
51
|
-
try: out = json.loads(txt[txt.find("{"): txt.rfind("}") + 1])
|
|
52
|
-
except Exception: out = dict(label="unsure", pattern=None, reason="unparseable")
|
|
53
|
-
return out, int(u.get("prompt_tokens") or 0), int(u.get("completion_tokens") or 0)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
def run(candidates, rate, model=DEFAULT_MODEL, cap_usd=CAP_USD, key=None, call=_call, seed=0):
|
|
57
|
-
"""candidates: [{id, kind, text, before, after}]. Returns (labels {id: {...}}, summary).
|
|
58
|
-
Prints the estimate first; over the cap it classifies a stratified random sample and scales up."""
|
|
59
|
-
key = key if key is not None else os.environ.get("OPENROUTER_API_KEY")
|
|
60
|
-
if not key: raise ClassifierError("OPENROUTER_API_KEY is not set (regular inference key, G5); classifier not run")
|
|
61
|
-
if not rate: raise ClassifierError(f"no list price for {model}; classifier not run")
|
|
62
|
-
est = estimate_usd(len(candidates), rate); sampled = False; chosen = list(candidates)
|
|
63
|
-
print(f"classifier: {len(candidates)} candidates, estimated ${est:.2f} at list, cap ${cap_usd:.2f}")
|
|
64
|
-
if est > cap_usd:
|
|
65
|
-
per = estimate_usd(1, rate) or 0.001; n = max(1, int(cap_usd / per * 0.9))
|
|
66
|
-
rnd = random.Random(seed); by_kind = {}
|
|
67
|
-
for c in candidates: by_kind.setdefault(c["kind"], []).append(c)
|
|
68
|
-
chosen = []
|
|
69
|
-
for kind, cs in by_kind.items():
|
|
70
|
-
k = max(1, round(n * len(cs) / len(candidates))); chosen += rnd.sample(cs, min(k, len(cs)))
|
|
71
|
-
sampled = True
|
|
72
|
-
labels = {}; spent = 0; in_tok = out_tok = 0; last = 0; stopped = False
|
|
73
|
-
cap_x1e6 = int(round(cap_usd * 1e6)); per_x1e6 = int(EST_IN_TOKENS * rate["input_usd_per_mtok"] + EST_OUT_TOKENS * rate["output_usd_per_mtok"])
|
|
74
|
-
for c in chosen:
|
|
75
|
-
if spent + max(per_x1e6, last) > cap_x1e6: stopped = True; break
|
|
76
|
-
prompt = f"{QUESTIONS.get(c['kind'], QUESTIONS['P12_unclear'])}\n\nBEFORE: {behavior.scrub(c.get('before',''), 400)}\nFLAGGED: {behavior.scrub(c['text'], CANDIDATE_CHARS)}\nAFTER: {behavior.scrub(c.get('after',''), 400)}"
|
|
77
|
-
out, i, o = call(model, prompt, key); in_tok += i; out_tok += o
|
|
78
|
-
last = int(i * rate["input_usd_per_mtok"] + o * rate["output_usd_per_mtok"]); spent += last
|
|
79
|
-
labels[c["id"]] = dict(label=out.get("label", "unsure"), pattern=out.get("pattern"), reason=str(out.get("reason", ""))[:120], label_source="classifier")
|
|
80
|
-
scale = (len(candidates) / len(labels)) if (sampled and labels) else 1.0
|
|
81
|
-
return labels, dict(ran=True, model=model, spend_usd=costs.usd(spent), cap_usd=cap_usd, sampled_n=len(labels) if sampled else 0,
|
|
82
|
-
candidates_n=len(candidates), estimated_usd=est, scale=round(scale, 2), input_tokens=in_tok, output_tokens=out_tok, stopped_at_cap=stopped,
|
|
83
|
-
note="estimated from a sample of %d" % len(labels) if sampled else None)
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
"""Agent cost from measured tokens, observer cost kept apart, extrapolation (plan Phase 2.2).
|
|
2
|
-
|
|
3
|
-
api_equiv() copied from weekly_report.py:91-105 with the Phase 1
|
|
4
|
-
rate() (1h cache writes at the listed cache_write_1h, else 2 x input). Money is accumulated in
|
|
5
|
-
integer micro-dollars (tokens x USD/MTok is exactly micro-dollars). The observer (note-taker) cost
|
|
6
|
-
is priced at the input rate only (:70-73) and lives only in spend.observer_note_taker_est_usd.
|
|
7
|
-
Extrapolation copied from :239-246. Unpriced = rate() None, output price None, or any negative price
|
|
8
|
-
(OpenRouter lists -1 for a few variable routers): such rows are never multiplied.
|
|
9
|
-
"""
|
|
10
|
-
import collections
|
|
11
|
-
|
|
12
|
-
from . import prices as prices_mod
|
|
13
|
-
|
|
14
|
-
TOKEN_FIELDS = ("input", "output", "cache_write_5m", "cache_write_1h", "cache_read")
|
|
15
|
-
PRICE_FIELDS = ("input_usd_per_mtok", "output_usd_per_mtok", "cache_read", "cache_write", "cache_write_1h")
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def usable_rate(rt):
|
|
19
|
-
"""A rate is usable only when every price is present and non-negative."""
|
|
20
|
-
if not rt: return False
|
|
21
|
-
return all(rt.get(f) is not None and rt[f] >= 0 for f in PRICE_FIELDS)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def api_equiv_x1e6(r, rt):
|
|
25
|
-
"""copied from weekly_report.py:91-105 (integer micro-dollars); None when unpriced."""
|
|
26
|
-
if not usable_rate(rt): return None
|
|
27
|
-
i = rt["input_usd_per_mtok"]
|
|
28
|
-
return int(round(r["input"] * i + r["output"] * rt["output_usd_per_mtok"] + r["cache_write_5m"] * rt["cache_write"]
|
|
29
|
-
+ r["cache_write_1h"] * rt["cache_write_1h"] + r["cache_read"] * rt["cache_read"]))
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def price_block(rt):
|
|
33
|
-
"""model_prices_usd_per_mtok for a line item / by_model row."""
|
|
34
|
-
if not rt: return None
|
|
35
|
-
return dict(input=rt["input_usd_per_mtok"], output=rt["output_usd_per_mtok"], cache_read=rt["cache_read"],
|
|
36
|
-
cache_write=rt["cache_write"], cache_write_1h=rt["cache_write_1h"], key=rt["key"], price_source=rt["price_source"])
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
class Pricer:
|
|
40
|
-
"""rate() with a per-model cache and the unpriced register."""
|
|
41
|
-
def __init__(self, prices):
|
|
42
|
-
self.models = prices["models"]; self._cache = {}
|
|
43
|
-
self.unpriced = collections.defaultdict(lambda: collections.defaultdict(collections.Counter))
|
|
44
|
-
|
|
45
|
-
def rate(self, model):
|
|
46
|
-
if model not in self._cache: self._cache[model] = prices_mod.rate(model, self.models)
|
|
47
|
-
return self._cache[model]
|
|
48
|
-
|
|
49
|
-
def usable(self, model):
|
|
50
|
-
return usable_rate(self.rate(model))
|
|
51
|
-
|
|
52
|
-
def reason(self, model):
|
|
53
|
-
rt = self.rate(model)
|
|
54
|
-
if rt is None: return "not in price list"
|
|
55
|
-
if rt.get("output_usd_per_mtok") is None: return "no output price listed"
|
|
56
|
-
return "negative (variable) price listed"
|
|
57
|
-
|
|
58
|
-
def observer_input_rate(self, model):
|
|
59
|
-
"""USD/MTok input rate for the note-taker model, or None (then the tokens are registered as unpriced)."""
|
|
60
|
-
rt = self.rate(model)
|
|
61
|
-
if rt is None or rt["input_usd_per_mtok"] is None or rt["input_usd_per_mtok"] < 0: return None
|
|
62
|
-
return rt["input_usd_per_mtok"]
|
|
63
|
-
|
|
64
|
-
def unpriced_list(self):
|
|
65
|
-
return [dict(model=m, role=role, calls=c["calls"], tokens=c["tokens"], reason=self.reason(m))
|
|
66
|
-
for m, roles in sorted(self.unpriced.items()) for role, c in sorted(roles.items())]
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def _zero():
|
|
70
|
-
c = collections.Counter(); c.update({f: 0 for f in TOKEN_FIELDS}); return c
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
def price_rows(rows, pricer):
|
|
74
|
-
"""Accumulate usage rows per transcript session and per (src, model) (weekly_report.py:98-105).
|
|
75
|
-
Returns (by_session, by_model, stamps_by_session). Unpriced rows count tokens and calls but no dollars."""
|
|
76
|
-
by_session = collections.defaultdict(_zero); by_model = collections.defaultdict(_zero); stamps = collections.defaultdict(list)
|
|
77
|
-
for r in rows:
|
|
78
|
-
rt = pricer.rate(r["model"]); c = api_equiv_x1e6(r, rt)
|
|
79
|
-
for tgt in (by_session[r["session"]], by_model[(r["src"], r["model"])]):
|
|
80
|
-
for f in TOKEN_FIELDS: tgt[f] += r[f]
|
|
81
|
-
tgt["calls"] += 1
|
|
82
|
-
if c is None: tgt["unpriced_calls"] += 1
|
|
83
|
-
else: tgt["usd_x1e6"] += c
|
|
84
|
-
if c is None:
|
|
85
|
-
u = pricer.unpriced[r["model"] or "<none>"]["agent"]; u["calls"] += 1; u["tokens"] += sum(r[f] for f in TOKEN_FIELDS)
|
|
86
|
-
stamps[r["session"]].append(r["ts"])
|
|
87
|
-
by_session[r["session"]]["model:" + (r["model"] or "")] += r["output"]
|
|
88
|
-
return by_session, by_model, stamps
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def dominant_model(counter):
|
|
92
|
-
ms = [(v, k[6:]) for k, v in counter.items() if k.startswith("model:")]
|
|
93
|
-
return max(ms)[1] if ms else None
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
def usd(x1e6):
|
|
97
|
-
return round(x1e6 / 1e6, 2)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
def extrapolation(items):
|
|
101
|
-
"""copied from weekly_report.py:239-246: ratio = measured agent USD per 1M observer tokens over sessions
|
|
102
|
-
with a transcript, applied to the observer tokens of sessions with none. Only those sessions are touched.
|
|
103
|
-
items: line-item dicts with has_transcript, observer_tokens, cost_x1e6 (measured micro-dollars)."""
|
|
104
|
-
m_obs = sum(i["observer_tokens"] for i in items if i["has_transcript"] and not i.get("orphan"))
|
|
105
|
-
m_usd = sum(i["cost_x1e6"] for i in items if i["has_transcript"] and not i.get("orphan")) / 1e6
|
|
106
|
-
u_obs = sum(i["observer_tokens"] for i in items if not i["has_transcript"])
|
|
107
|
-
ratio = m_usd / m_obs * 1e6 if m_obs else None
|
|
108
|
-
total = 0
|
|
109
|
-
for i in items:
|
|
110
|
-
if not i["has_transcript"] and ratio is not None:
|
|
111
|
-
i["extrapolated_x1e6"] = int(round(i["observer_tokens"] * ratio)); total += i["extrapolated_x1e6"]
|
|
112
|
-
else:
|
|
113
|
-
i["extrapolated_x1e6"] = 0
|
|
114
|
-
n_un = sum(1 for i in items if not i["has_transcript"])
|
|
115
|
-
if n_un == 0 and items: return 0.0, "all sessions measured (no extrapolation needed)", ratio
|
|
116
|
-
basis = (f"EXTRAPOLATED (low confidence): on this box, measured sessions cost ${ratio:.2f} API-equivalent per 1M observer tokens; "
|
|
117
|
-
f"applied to {u_obs:,} observer tokens from {n_un} sessions with no transcript here (remote devices). "
|
|
118
|
-
f"Assumes remote work looks like box work.") if ratio is not None else "n/a (no measured session to derive a ratio from)"
|
|
119
|
-
return (round(total / 1e6, 2) if ratio is not None else None), basis, ratio
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
"""Phase 5: per-device transcript export and merge (plan 5.2), so Mac sessions can move from
|
|
2
|
-
"extrapolated (low confidence)" to measured tokens without any transcript leaving the machine.
|
|
3
|
-
|
|
4
|
-
Export (runs on the machine that owns the transcripts): the Phase 1 collector rows for the window plus
|
|
5
|
-
that machine's local sdk_sessions map {memory_session_id -> content_session_id, project, started_at_epoch},
|
|
6
|
-
read from a read-only snapshot of its local memindex.db. Only ids, timestamps, token counts and model
|
|
7
|
-
names: no prompt text, no observation text, no settings, no file paths.
|
|
8
|
-
|
|
9
|
-
Merge (runs on the box): rows join to box-side sessions on memory_session_id (fallback content_session_id);
|
|
10
|
-
joined sessions flip from cost_basis extrapolated to estimated_usage with device=<label>. Whatever stays
|
|
11
|
-
unmeasured is extrapolated again and keeps its low-confidence label. Rows that match no box session are
|
|
12
|
-
counted as unmatched, like any other transcript.
|
|
13
|
-
"""
|
|
14
|
-
import datetime as dt
|
|
15
|
-
import json
|
|
16
|
-
import os
|
|
17
|
-
import platform
|
|
18
|
-
|
|
19
|
-
from . import period, snapshot
|
|
20
|
-
|
|
21
|
-
ROW_FIELDS = ("src", "session", "sidechain", "model", "ts", "input", "output", "cache_write", "cache_write_1h", "cache_write_5m", "cache_read", "cost_usd_reported")
|
|
22
|
-
FORBIDDEN = ("text", "prompt", "content", "title", "file", "cwd", "settings")
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def export(label, usage_doc, outdir, window_block, live_db=None):
|
|
26
|
-
"""Write <outdir>/device-usage-<label>.json from a collect() document and the local DB. The DB
|
|
27
|
-
snapshot is removed afterwards so the export directory holds only the small file."""
|
|
28
|
-
rows = [dict({k: r.get(k) for k in ROW_FIELDS}, device=label) for r in usage_doc["rows"]]
|
|
29
|
-
snap = snapshot.snapshot(outdir, live=live_db); db = snapshot.open_snapshot(snap)
|
|
30
|
-
try:
|
|
31
|
-
ids = sorted({r["session"] for r in rows if r["session"]})
|
|
32
|
-
q = "select memory_session_id, content_session_id, project, started_at_epoch from sdk_sessions where "
|
|
33
|
-
if window_block.get("start_epoch_ms") is not None:
|
|
34
|
-
cur = db.execute(q + "started_at_epoch >= ? and started_at_epoch < ?", (window_block["start_epoch_ms"], window_block["end_epoch_ms"]))
|
|
35
|
-
else:
|
|
36
|
-
cur = db.execute(q + f"content_session_id in ({','.join('?' * len(ids))})", ids) if ids else []
|
|
37
|
-
sessions = {r[0]: dict(content_session_id=r[1], project=r[2], started_at_epoch=r[3]) for r in cur if r[0]}
|
|
38
|
-
finally:
|
|
39
|
-
db.close()
|
|
40
|
-
try: os.remove(snap)
|
|
41
|
-
except OSError: pass
|
|
42
|
-
doc = dict(device=label, host_label=platform.node(), exported_at_pt=period.now_pt().strftime("%Y-%m-%d %H:%M PT"), window=window_block,
|
|
43
|
-
rows=rows, sessions=sessions, contents="usage rows (ids, timestamps, token counts, model names) and the local session id map; no text",
|
|
44
|
-
behavior_export=None)
|
|
45
|
-
assert not any(k in r for r in rows for k in FORBIDDEN)
|
|
46
|
-
path = os.path.join(outdir, f"device-usage-{label}.json")
|
|
47
|
-
with open(path, "w", encoding="utf-8") as fh: json.dump(doc, fh)
|
|
48
|
-
return path, doc
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def load(path):
|
|
52
|
-
with open(path, encoding="utf-8") as fh: d = json.load(fh)
|
|
53
|
-
for k in ("device", "window", "rows", "sessions"):
|
|
54
|
-
if k not in d: raise ValueError(f"{path}: not a device-usage export (missing {k})")
|
|
55
|
-
bad = [k for r in d["rows"] for k in FORBIDDEN if k in r]
|
|
56
|
-
if bad: raise ValueError(f"{path}: export carries forbidden fields {sorted(set(bad))}")
|
|
57
|
-
return d
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def merge(rows, exports, sess, window_block):
|
|
61
|
-
"""Append exported rows to `rows`, re-keyed to the box session's content_session_id when the export's
|
|
62
|
-
memory_session_id (or content id) is known on the box. Returns a summary per export."""
|
|
63
|
-
by_mem = {mid: v["content_session_id"] for mid, v in sess.items()}
|
|
64
|
-
by_cs = {v["content_session_id"] for v in sess.values()}
|
|
65
|
-
seen = {(r["session"], r["ts"], r["input"], r["output"], r["cache_read"]) for r in rows}
|
|
66
|
-
S, E = window_block.get("start_epoch_ms"), window_block.get("end_epoch_ms")
|
|
67
|
-
out = []
|
|
68
|
-
for d in exports:
|
|
69
|
-
label = d["device"]; mem_by_cs = {v["content_session_id"]: mid for mid, v in d["sessions"].items()}
|
|
70
|
-
joined = unmatched = dup = outside = 0
|
|
71
|
-
for r in d["rows"]:
|
|
72
|
-
if S is not None and not (S <= _ts_ms(r["ts"]) < E): outside += 1; continue
|
|
73
|
-
cs = r["session"]; mid = mem_by_cs.get(cs)
|
|
74
|
-
if mid in by_mem: cs2 = by_mem[mid]; joined += 1
|
|
75
|
-
elif cs in by_cs: cs2 = cs; joined += 1
|
|
76
|
-
else: cs2 = cs; unmatched += 1
|
|
77
|
-
key = (cs2, r["ts"], r["input"], r["output"], r["cache_read"])
|
|
78
|
-
if key in seen: dup += 1; continue
|
|
79
|
-
seen.add(key)
|
|
80
|
-
rows.append(dict(r, session=cs2, device=label, file=None, dir=f"device:{label}", cwd=None, export_memory_session_id=mid, export_session=cs))
|
|
81
|
-
same_window = (d["window"].get("start_pt"), d["window"].get("end_exclusive_pt")) == (window_block.get("start_pt"), window_block.get("end_exclusive_pt"))
|
|
82
|
-
out.append(dict(device=label, host_label=d.get("host_label"), exported_at_pt=d.get("exported_at_pt"), rows=len(d["rows"]), joined=joined, unmatched=unmatched, duplicates_skipped=dup,
|
|
83
|
-
outside_window=outside, window=d["window"], window_matches_report=same_window))
|
|
84
|
-
return out
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
def _ts_ms(iso):
|
|
88
|
-
return int(dt.datetime.fromisoformat(iso).timestamp() * 1000)
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
"""Sessions, devices, per-session evidence and the transcript join (plan Phase 2.1).
|
|
2
|
-
|
|
3
|
-
Copied from weekly_report.py:44-86 (sessions, devices, evidence),
|
|
4
|
-
:137 (join key) and :151-156 (unmatched transcripts). Not copied on purpose: :54-56 (seat names and
|
|
5
|
-
device UUIDs) and :158-168 (gateway log parsing). Devices are labeled local / remote-1 / remote-2 in
|
|
6
|
-
first-seen order; UUIDs only ever leave this module as short hashes.
|
|
7
|
-
Queries select named columns with a window or id filter; nothing loads a whole table.
|
|
8
|
-
"""
|
|
9
|
-
import collections
|
|
10
|
-
import datetime as dt
|
|
11
|
-
import hashlib
|
|
12
|
-
import re
|
|
13
|
-
|
|
14
|
-
from .period import PT
|
|
15
|
-
|
|
16
|
-
SHIP_RE = re.compile(r"\b(merged|published|released|shipped|npm publish|tagged v?\d|publish complete|committed)\b", re.I)
|
|
17
|
-
SHIP_TYPES = ("feature", "bugfix", "change")
|
|
18
|
-
COMPLETED_MIN_LEN = 20
|
|
19
|
-
LOCAL = "local"
|
|
20
|
-
PROMPT_CHARS = 1000
|
|
21
|
-
|
|
22
|
-
SESSION_COLS = ("id", "content_session_id", "memory_session_id", "project", "platform_source", "user_prompt",
|
|
23
|
-
"started_at_epoch", "completed_at_epoch", "status", "custom_title", "observed_model", "observed_billing")
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
class Scope:
|
|
27
|
-
"""kind = period | session | project. S/E are epoch ms (None for a session run)."""
|
|
28
|
-
def __init__(self, kind, S=None, E=None, session=None, project=None):
|
|
29
|
-
self.kind, self.S, self.E, self.session, self.project = kind, S, E, session, project
|
|
30
|
-
|
|
31
|
-
def block(self):
|
|
32
|
-
return dict(kind=self.kind, session=self.session, project=self.project)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def pt(ms):
|
|
36
|
-
return dt.datetime.fromtimestamp(ms / 1000, PT)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def day_pt(ms):
|
|
40
|
-
return pt(ms).date().isoformat()
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def device_hash(device_id):
|
|
44
|
-
"""Short hash for Details; the UUID itself is never written to any output."""
|
|
45
|
-
return LOCAL if device_id == LOCAL else hashlib.sha1(device_id.encode()).hexdigest()[:8]
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def session_key(r):
|
|
49
|
-
"""copied from weekly_report.py:45: memory_session_id or nomem-<id>."""
|
|
50
|
-
return r["memory_session_id"] or f"nomem-{r['id']}"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
def load_sessions(db, scope):
|
|
54
|
-
"""copied from weekly_report.py:45-47 (period), extended with the --session / --project scopes."""
|
|
55
|
-
cols = ", ".join(SESSION_COLS)
|
|
56
|
-
if scope.kind == "session":
|
|
57
|
-
sql, args = f"select {cols} from sdk_sessions where content_session_id=?", (scope.session,)
|
|
58
|
-
elif scope.kind == "project":
|
|
59
|
-
sql, args = (f"select {cols} from sdk_sessions where started_at_epoch>=? and started_at_epoch<? and (project=? or project like ?)",
|
|
60
|
-
(scope.S, scope.E, scope.project, scope.project + "/%"))
|
|
61
|
-
else:
|
|
62
|
-
sql, args = f"select {cols} from sdk_sessions where started_at_epoch>=? and started_at_epoch<?", (scope.S, scope.E)
|
|
63
|
-
sess = {}
|
|
64
|
-
for r in db.execute(sql, args):
|
|
65
|
-
d = dict(r); sess[session_key(d)] = d
|
|
66
|
-
return sess
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def _rows(db, table, cols, scope, id_col, ids):
|
|
70
|
-
"""Window-bounded (period/project) or id-bounded (session) rows of one table, streamed."""
|
|
71
|
-
c = ", ".join(cols)
|
|
72
|
-
if scope.kind == "session":
|
|
73
|
-
for i in ids:
|
|
74
|
-
yield from db.execute(f"select {c} from {table} where {id_col}=?", (i,))
|
|
75
|
-
else:
|
|
76
|
-
yield from db.execute(f"select {c} from {table} where created_at_epoch>=? and created_at_epoch<?", (scope.S, scope.E))
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
def new_evidence():
|
|
80
|
-
return dict(obs=0, types=collections.Counter(), obs_tok=0, obs_tok_dedup=0, obs_cost_x1e6=0, models=set(), sums=0,
|
|
81
|
-
completed_sums=0, completed_text=None, prompts=[], stamps=[], ship_titles=[], ship_ids=[], text=[],
|
|
82
|
-
obs_ids=[], sum_ids=[], devices=set())
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def load_evidence(db, scope, sess, observer_rate):
|
|
86
|
-
"""Per-session evidence (weekly_report.py:59-86) and the device registry (:48-53).
|
|
87
|
-
|
|
88
|
-
observer_rate(model) -> input USD/MTok or None; the observer cost is accumulated in integer
|
|
89
|
-
micro-dollars, deduped on (memory_session_id, created_at_epoch, discovery_tokens) (:70-73).
|
|
90
|
-
Returns (ev, devices, counts) where devices = {device_id: label} in first-seen order."""
|
|
91
|
-
bycs = {v["content_session_id"]: k for k, v in sess.items()}
|
|
92
|
-
mids = list(sess) if scope.kind == "session" else None
|
|
93
|
-
csids = [v["content_session_id"] for v in sess.values()] if scope.kind == "session" else None
|
|
94
|
-
ev = collections.defaultdict(new_evidence)
|
|
95
|
-
first_seen = {}
|
|
96
|
-
seen_reply = set(); observer_unpriced = collections.Counter(); counts = collections.Counter()
|
|
97
|
-
|
|
98
|
-
def in_scope(m):
|
|
99
|
-
return scope.kind != "project" or m in sess
|
|
100
|
-
|
|
101
|
-
def dev(m, device_id, t):
|
|
102
|
-
d = device_id or LOCAL
|
|
103
|
-
if d not in first_seen or t < first_seen[d]: first_seen[d] = t
|
|
104
|
-
if m is not None: ev[m]["devices"].add(d)
|
|
105
|
-
|
|
106
|
-
for o in _rows(db, "observations", ("id", "memory_session_id", "project", "type", "title", "discovery_tokens",
|
|
107
|
-
"created_at_epoch", "generated_by_model", "origin_device_id"), scope, "memory_session_id", mids or ()):
|
|
108
|
-
m = o["memory_session_id"]
|
|
109
|
-
if not in_scope(m): continue
|
|
110
|
-
counts["observations"] += 1; e = ev[m]
|
|
111
|
-
e["obs"] += 1; e["types"][o["type"]] += 1; e["obs_ids"].append(o["id"])
|
|
112
|
-
e["obs_tok"] += o["discovery_tokens"] or 0; e["models"].add(o["generated_by_model"]); e["stamps"].append(o["created_at_epoch"])
|
|
113
|
-
key = (m, o["created_at_epoch"], o["discovery_tokens"])
|
|
114
|
-
if key not in seen_reply:
|
|
115
|
-
seen_reply.add(key); tok = o["discovery_tokens"] or 0; e["obs_tok_dedup"] += tok
|
|
116
|
-
r = observer_rate(o["generated_by_model"])
|
|
117
|
-
if r is None: observer_unpriced[o["generated_by_model"]] += tok
|
|
118
|
-
else: e["obs_cost_x1e6"] += int(round(tok * r))
|
|
119
|
-
if o["type"] in SHIP_TYPES and SHIP_RE.search(o["title"] or ""):
|
|
120
|
-
e["ship_titles"].append(o["title"]); e["ship_ids"].append(o["id"])
|
|
121
|
-
e["text"].append(o["title"] or "")
|
|
122
|
-
dev(m, o["origin_device_id"], o["created_at_epoch"])
|
|
123
|
-
for s_ in _rows(db, "session_summaries", ("id", "memory_session_id", "request", "completed", "discovery_tokens",
|
|
124
|
-
"created_at_epoch", "origin_device_id"), scope, "memory_session_id", mids or ()):
|
|
125
|
-
m = s_["memory_session_id"]
|
|
126
|
-
if not in_scope(m): continue
|
|
127
|
-
counts["summaries"] += 1; e = ev[m]
|
|
128
|
-
e["sums"] += 1; e["sum_ids"].append(s_["id"]); e["stamps"].append(s_["created_at_epoch"])
|
|
129
|
-
if s_["completed"] and len(s_["completed"]) > COMPLETED_MIN_LEN:
|
|
130
|
-
e["completed_sums"] += 1
|
|
131
|
-
if e["completed_text"] is None: e["completed_text"] = s_["completed"]
|
|
132
|
-
e["text"].append((s_["request"] or "") + " " + (s_["completed"] or ""))
|
|
133
|
-
dev(m, s_["origin_device_id"], s_["created_at_epoch"])
|
|
134
|
-
for p in _rows(db, "user_prompts", ("content_session_id", f"substr(prompt_text,1,{PROMPT_CHARS}) as prompt_text",
|
|
135
|
-
"created_at_epoch", "origin_device_id"), scope, "content_session_id", csids or ()):
|
|
136
|
-
m = bycs.get(p["content_session_id"])
|
|
137
|
-
if not in_scope(m): continue
|
|
138
|
-
counts["prompts"] += 1
|
|
139
|
-
if m: ev[m]["prompts"].append(p["prompt_text"]); ev[m]["stamps"].append(p["created_at_epoch"])
|
|
140
|
-
dev(m, p["origin_device_id"], p["created_at_epoch"])
|
|
141
|
-
for t in _rows(db, "tool_uses", ("content_session_id", "created_at_epoch"), scope, "content_session_id", csids or ()):
|
|
142
|
-
m = bycs.get(t["content_session_id"])
|
|
143
|
-
if not in_scope(m): continue
|
|
144
|
-
counts["tool_uses"] += 1
|
|
145
|
-
if m: ev[m]["stamps"].append(t["created_at_epoch"])
|
|
146
|
-
labels = {LOCAL: LOCAL}; n = 0
|
|
147
|
-
for d, _ in sorted(first_seen.items(), key=lambda kv: kv[1]):
|
|
148
|
-
if d != LOCAL: n += 1; labels[d] = f"remote-{n}"
|
|
149
|
-
return ev, labels, counts, observer_unpriced
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
def join_transcripts(rows, sess):
|
|
153
|
-
"""Group usage rows by transcript session and split matched / unmatched.
|
|
154
|
-
Join key: usage.session == sdk_sessions.content_session_id (weekly_report.py:137; plan Phase 0 C)."""
|
|
155
|
-
bycs = {v["content_session_id"]: k for k, v in sess.items()}
|
|
156
|
-
by_session = collections.defaultdict(list)
|
|
157
|
-
for r in rows: by_session[r["session"]].append(r)
|
|
158
|
-
matched = {bycs[cs]: rs for cs, rs in by_session.items() if cs in bycs}
|
|
159
|
-
unmatched = {cs: rs for cs, rs in by_session.items() if cs not in bycs}
|
|
160
|
-
return matched, unmatched
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"""Keyword-drafted labels and the review step (plan Phase 2.3 rules, 2.4 review).
|
|
2
|
-
|
|
3
|
-
CAT_RULES / FAIL_RULES / classify() / active_minutes() copied from
|
|
4
|
-
weekly_report.py:109-133. The failure set is the closed list in
|
|
5
|
-
SKILL.md:80; Rework is a failure type only, never a category. Every drafted label starts as
|
|
6
|
-
label_source "keyword"; only review --apply may change it to llm | human.
|
|
7
|
-
"""
|
|
8
|
-
import collections
|
|
9
|
-
import re
|
|
10
|
-
|
|
11
|
-
CATEGORIES = ("Feature", "Bug fix", "Incident", "Maintenance", "Investigation", "Experiment")
|
|
12
|
-
FAILURE_TYPES = ("Looping", "Hedging", "Wrong turn", "Rework", "Regression", "Premature completion", "Unauthorized action",
|
|
13
|
-
"Suboptimal path", "Duplicate work", "Blocked work", "Missed requirement", "Unnecessary escalation",
|
|
14
|
-
"Context re-read", "Model thrash", "Fan-out waste", "Recovery after miss")
|
|
15
|
-
STATUSES = ("shipped", "completed", "in_progress", "abandoned", "blocked")
|
|
16
|
-
LABEL_SOURCES = ("keyword", "llm", "human")
|
|
17
|
-
|
|
18
|
-
CAT_RULES = [("Incident", r"emergency|bleed|outage|incident|brake|hotfix|crash-?loop"),
|
|
19
|
-
("Experiment", r"a/b|\bab\b|gaia-ab|experiment|benchmark|trial run|compare .* vs"),
|
|
20
|
-
("Investigation", r"make-plan|plan only|planning only|fact-?check|review|monitor|learn the codebase|learn-code|research|investigat|spec\b|product spec|status\b|dig\b|audit"),
|
|
21
|
-
("Maintenance", r"release|version bump|cleanup|clean up|disk|upgrade|rename|name new code workspaces|migrat|refactor"),
|
|
22
|
-
("Bug fix", r"\bfix|bug|broken|regression|timeout"),
|
|
23
|
-
("Feature", r"\bbuild|implement|feature|new skill|wowerpoint|deck|create|execute plans?/")]
|
|
24
|
-
DEFAULT_CATEGORY = "Investigation"
|
|
25
|
-
FAIL_RULES = [("Rework", r"\bsteer\b|reset from \w+|re-?plan|fix these gaps|verifier found|discard both|too granular|rewrite .* at product altitude|redo"),
|
|
26
|
-
("Recovery after miss", r"usage[- ]limit|resume the|relaunch|quota|dead token|401\b"),
|
|
27
|
-
("Wrong turn", r"not a real|misread|flawed|wrong (approach|path)|abandon|too scoped")]
|
|
28
|
-
LOOP_PREFIX, LOOP_MIN = 80, 3
|
|
29
|
-
assert all(c in CATEGORIES for c, _ in CAT_RULES) and all(f in FAILURE_TYPES for f, _ in FAIL_RULES)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def _first(rules, text):
|
|
33
|
-
for name, rx in rules:
|
|
34
|
-
m = re.search(rx, text)
|
|
35
|
-
if m: return name, m.group(0)
|
|
36
|
-
return None, None
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def classify(e, s):
|
|
40
|
-
"""copied from weekly_report.py:119-127, returning the matched keywords too (for labels.review.json)."""
|
|
41
|
-
head = " ".join([s.get("project") or "", s.get("user_prompt") or ""] + e["prompts"][:1]).lower()
|
|
42
|
-
tail = " ".join(e["text"][:5]).lower()
|
|
43
|
-
cat, kw = _first(CAT_RULES, head); where = "head"
|
|
44
|
-
if cat is None: cat, kw = _first(CAT_RULES, tail); where = "tail"
|
|
45
|
-
if cat is None: cat, kw, where = DEFAULT_CATEGORY, None, "default"
|
|
46
|
-
ptxt = " ".join([s.get("user_prompt") or ""] + e["prompts"]).lower()
|
|
47
|
-
fails = []; fkw = {}
|
|
48
|
-
for f, rx in FAIL_RULES:
|
|
49
|
-
m = re.search(rx, ptxt)
|
|
50
|
-
if m: fails.append(f); fkw[f] = m.group(0)
|
|
51
|
-
firsts = [x[:LOOP_PREFIX] for x in e["prompts"] if x]
|
|
52
|
-
if len(firsts) >= LOOP_MIN and max(collections.Counter(firsts).values()) >= LOOP_MIN:
|
|
53
|
-
fails.append("Looping"); fkw["Looping"] = f"same {LOOP_PREFIX}-char prompt prefix x{LOOP_MIN}+"
|
|
54
|
-
return dict(category=cat, failure_signals=fails, matched=dict(category_keyword=kw, category_where=where, failure_keywords=fkw),
|
|
55
|
-
head=head[:300], tail=tail[:300])
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
def active_minutes(stamps, gap_min=15):
|
|
59
|
-
"""copied from weekly_report.py:128-133 (stamps: epoch ms)."""
|
|
60
|
-
st = sorted(set(stamps)); tot = 0
|
|
61
|
-
for a, b in zip(st, st[1:]):
|
|
62
|
-
d = (b - a) / 60000
|
|
63
|
-
if d <= gap_min: tot += d
|
|
64
|
-
return tot
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
def review_entry(item, draft):
|
|
68
|
-
"""One labels.review.json entry per line item (plan 2.4)."""
|
|
69
|
-
return dict(work_item_id=item["work_item_id"], session_ids=item["session_ids"], title=item["title"],
|
|
70
|
-
draft_category=draft["category"], draft_failure_signals=draft["failure_signals"], matched=draft["matched"],
|
|
71
|
-
head=draft["head"], tail=draft["tail"], label_source="keyword",
|
|
72
|
-
category=None, failure_type=None, failure_signals=None, reviewed_by=None, note=None)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
class ReviewError(ValueError):
|
|
76
|
-
pass
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
HUMAN_REVIEWERS = ("user", "human")
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
def apply_review(line_items, reviewed, reviewed_at_pt):
|
|
83
|
-
"""Merge confirmed labels into line items. An entry counts as reviewed when it names a category
|
|
84
|
-
(and optionally failure_type / failure_signals) and a reviewed_by; label_source becomes
|
|
85
|
-
'human' for reviewed_by 'user' or 'human' (case-insensitive) or an explicit entry label_source, else 'llm'.
|
|
86
|
-
Unreviewed entries are left as keyword drafts. failure_signals / failure_type left null keep the draft's
|
|
87
|
-
signals; pass failure_signals: [] to clear them. Returns the number applied."""
|
|
88
|
-
entries = reviewed.get("items", reviewed) if isinstance(reviewed, dict) else reviewed
|
|
89
|
-
default_by = reviewed.get("reviewed_by") if isinstance(reviewed, dict) else None
|
|
90
|
-
by_id = {li["work_item_id"]: li for li in line_items}; n = 0
|
|
91
|
-
for en in entries:
|
|
92
|
-
en = dict(en)
|
|
93
|
-
if en.get("reviewed_by") is None:
|
|
94
|
-
en["reviewed_by"] = default_by
|
|
95
|
-
li = by_id.get(en.get("work_item_id"))
|
|
96
|
-
if li is None or not en.get("category") or not en.get("reviewed_by"): continue
|
|
97
|
-
if en["category"] not in CATEGORIES: raise ReviewError(f"{en['work_item_id']}: unknown category {en['category']!r}")
|
|
98
|
-
fails = en.get("failure_signals")
|
|
99
|
-
if fails is None and en.get("failure_type"): fails = [en["failure_type"]]
|
|
100
|
-
if fails is None: fails = list(li["failure_signals"])
|
|
101
|
-
for f in fails:
|
|
102
|
-
if f not in FAILURE_TYPES: raise ReviewError(f"{en['work_item_id']}: unknown failure type {f!r} (SKILL.md:80 is the set)")
|
|
103
|
-
src = en.get("label_source")
|
|
104
|
-
if src in (None, "keyword"): src = "human" if str(en["reviewed_by"]).strip().lower() in HUMAN_REVIEWERS else "llm"
|
|
105
|
-
if src not in ("llm", "human"): raise ReviewError(f"{en['work_item_id']}: label_source must be llm or human, got {src!r}")
|
|
106
|
-
if en.get("status") is not None:
|
|
107
|
-
if en["status"] not in STATUSES: raise ReviewError(f"{en['work_item_id']}: unknown status {en['status']!r}")
|
|
108
|
-
li["status"] = en["status"]
|
|
109
|
-
li["category"] = li["work_category"] = en["category"]; li["failure_signals"] = list(fails)
|
|
110
|
-
li["failure_type"] = fails[0] if fails else ""; li["label_source"] = src
|
|
111
|
-
li["reviewed_by"] = en["reviewed_by"]; li["reviewed_at_pt"] = reviewed_at_pt
|
|
112
|
-
if en.get("note"): li["notes"] = en["note"]
|
|
113
|
-
if en.get("title"): li["title"] = str(en["title"])[:120].replace("\n", " ")
|
|
114
|
-
n += 1
|
|
115
|
-
return n
|