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.
Files changed (166) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/.codex-plugin/plugin.json +1 -1
  4. package/.qoder-plugin/plugin.json +1 -1
  5. package/README.md +8 -2
  6. package/dist/bug-report/index.js +94 -83
  7. package/dist/npx-cli/index.js +300 -299
  8. package/dist/opencode-plugin/index.js +40 -40
  9. package/dist/pi-extension/TYPEBOX-PROVENANCE.json +4 -4
  10. package/dist/pi-extension/index.js +9 -9
  11. package/integrations/dsh/lib/index.js +19 -19
  12. package/integrations/dsh/package.json +1 -1
  13. package/integrations/openclaw/install.sh +16 -10
  14. package/integrations/openclaw/openclaw.plugin.json +1 -5
  15. package/integrations/openclaw/test-install.sh +20 -13
  16. package/integrations/pi/TYPEBOX-PROVENANCE.json +4 -4
  17. package/package.json +5 -5
  18. package/plugin/.claude-plugin/plugin.json +1 -1
  19. package/plugin/.codex-plugin/plugin.json +1 -1
  20. package/plugin/.qoder-plugin/plugin.json +1 -1
  21. package/plugin/hooks/codex-hooks.json +12 -0
  22. package/plugin/package.json +1 -1
  23. package/plugin/scripts/context-generator.cjs +65 -63
  24. package/plugin/scripts/context-generator.cjs.map +3 -3
  25. package/plugin/scripts/mcp-server.cjs +71 -70
  26. package/plugin/scripts/mcp-server.cjs.map +3 -3
  27. package/plugin/scripts/statusline.cjs +1 -1
  28. package/plugin/scripts/transcript-watcher.cjs +36 -35
  29. package/plugin/scripts/transcript-watcher.cjs.map +3 -3
  30. package/plugin/scripts/worker-service.cjs +714 -674
  31. package/plugin/scripts/worker-service.cjs.map +3 -3
  32. package/plugin/skills/cloud-sync/SKILL.md +5 -5
  33. package/plugin/skills/distill/SKILL.md +22 -0
  34. package/plugin/skills/how-it-works/SKILL.md +1 -1
  35. package/plugin/skills/how-it-works/onboarding-explainer.md +1 -1
  36. package/plugin/skills/import-chats/SKILL.md +22 -0
  37. package/plugin/skills/import-jira/SKILL.md +22 -0
  38. package/plugin/skills/import-reviews/SKILL.md +22 -0
  39. package/plugin/skills/mode-creator/SKILL.md +7 -1
  40. package/plugin/skills/pin/SKILL.md +26 -0
  41. package/plugin/skills/savings/SKILL.md +22 -0
  42. package/plugin/skills/timeline-report/SKILL.md +8 -8
  43. package/plugin/skills/weekly-digests/SKILL.md +5 -5
  44. package/plugin/skills/worker-request.mjs +45 -0
  45. package/plugin/sqlite/SessionStore.js +109 -109
  46. package/plugin/sqlite/SessionStore.js.map +3 -3
  47. package/plugin/sqlite/observations/files.js +12 -9
  48. package/plugin/sqlite/observations/files.js.map +3 -3
  49. package/plugin/ui/viewer-bundle.js +18 -18
  50. package/integrations/openclaw/skills/agent-cost-report/CHECKSUMS.txt +0 -50
  51. package/integrations/openclaw/skills/agent-cost-report/SKILL.md +0 -241
  52. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/__init__.py +0 -1
  53. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/behavior.py +0 -303
  54. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/classify.py +0 -83
  55. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/costs.py +0 -119
  56. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/devices.py +0 -88
  57. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/evidence.py +0 -160
  58. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/labels.py +0 -115
  59. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/measure.py +0 -89
  60. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/mistakes.py +0 -310
  61. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/patterns.py +0 -391
  62. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/period.py +0 -98
  63. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/prices.py +0 -95
  64. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/render.py +0 -586
  65. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rollup.py +0 -355
  66. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rules.json +0 -4
  67. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rules.py +0 -76
  68. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/sample.py +0 -45
  69. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/snapshot.py +0 -53
  70. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/sync.py +0 -79
  71. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/transcripts.py +0 -126
  72. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/wins.py +0 -341
  73. package/integrations/openclaw/skills/agent-cost-report/scripts/acr.py +0 -219
  74. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/_db.py +0 -49
  75. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/_paths.py +0 -9
  76. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/codex/sessions/2026/09/18/rollout-codex-1.jsonl +0 -4
  77. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/make_report_fixtures.py +0 -46
  78. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/prices_sample.json +0 -6
  79. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-a/sess-aaaa.jsonl +0 -8
  80. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-b/sess-bbbb.jsonl +0 -1
  81. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-1day.json +0 -823
  82. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-30day.json +0 -2213
  83. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-7day.json +0 -1252
  84. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-empty.json +0 -536
  85. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-session.json +0 -662
  86. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_behavior.py +0 -299
  87. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_devices.py +0 -99
  88. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_labels.py +0 -96
  89. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_measure.py +0 -120
  90. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_pdf_windows.py +0 -54
  91. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_period.py +0 -137
  92. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_prices.py +0 -126
  93. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_render.py +0 -238
  94. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_render_encoding.py +0 -70
  95. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_rollup.py +0 -263
  96. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_snapshot.py +0 -63
  97. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_sync.py +0 -52
  98. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_transcript_encoding.py +0 -109
  99. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_transcripts.py +0 -151
  100. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_wins.py +0 -222
  101. package/plugin/skills/agent-cost-report/CHECKSUMS.txt +0 -50
  102. package/plugin/skills/agent-cost-report/SKILL.md +0 -241
  103. package/plugin/skills/agent-cost-report/scripts/acr/__init__.py +0 -1
  104. package/plugin/skills/agent-cost-report/scripts/acr/behavior.py +0 -303
  105. package/plugin/skills/agent-cost-report/scripts/acr/classify.py +0 -83
  106. package/plugin/skills/agent-cost-report/scripts/acr/costs.py +0 -119
  107. package/plugin/skills/agent-cost-report/scripts/acr/devices.py +0 -88
  108. package/plugin/skills/agent-cost-report/scripts/acr/evidence.py +0 -160
  109. package/plugin/skills/agent-cost-report/scripts/acr/labels.py +0 -115
  110. package/plugin/skills/agent-cost-report/scripts/acr/measure.py +0 -89
  111. package/plugin/skills/agent-cost-report/scripts/acr/mistakes.py +0 -310
  112. package/plugin/skills/agent-cost-report/scripts/acr/patterns.py +0 -391
  113. package/plugin/skills/agent-cost-report/scripts/acr/period.py +0 -98
  114. package/plugin/skills/agent-cost-report/scripts/acr/prices.py +0 -95
  115. package/plugin/skills/agent-cost-report/scripts/acr/render.py +0 -586
  116. package/plugin/skills/agent-cost-report/scripts/acr/rollup.py +0 -355
  117. package/plugin/skills/agent-cost-report/scripts/acr/rules.json +0 -4
  118. package/plugin/skills/agent-cost-report/scripts/acr/rules.py +0 -76
  119. package/plugin/skills/agent-cost-report/scripts/acr/sample.py +0 -45
  120. package/plugin/skills/agent-cost-report/scripts/acr/snapshot.py +0 -53
  121. package/plugin/skills/agent-cost-report/scripts/acr/sync.py +0 -79
  122. package/plugin/skills/agent-cost-report/scripts/acr/transcripts.py +0 -126
  123. package/plugin/skills/agent-cost-report/scripts/acr/wins.py +0 -341
  124. package/plugin/skills/agent-cost-report/scripts/acr.py +0 -219
  125. package/plugin/skills/agent-cost-report/scripts/tests/_db.py +0 -49
  126. package/plugin/skills/agent-cost-report/scripts/tests/_paths.py +0 -9
  127. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/codex/sessions/2026/09/18/rollout-codex-1.jsonl +0 -4
  128. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/make_report_fixtures.py +0 -46
  129. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/prices_sample.json +0 -6
  130. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-a/sess-aaaa.jsonl +0 -8
  131. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-b/sess-bbbb.jsonl +0 -1
  132. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-1day.json +0 -823
  133. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-30day.json +0 -2213
  134. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-7day.json +0 -1252
  135. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-empty.json +0 -536
  136. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-session.json +0 -662
  137. package/plugin/skills/agent-cost-report/scripts/tests/test_behavior.py +0 -299
  138. package/plugin/skills/agent-cost-report/scripts/tests/test_devices.py +0 -99
  139. package/plugin/skills/agent-cost-report/scripts/tests/test_labels.py +0 -96
  140. package/plugin/skills/agent-cost-report/scripts/tests/test_measure.py +0 -120
  141. package/plugin/skills/agent-cost-report/scripts/tests/test_pdf_windows.py +0 -54
  142. package/plugin/skills/agent-cost-report/scripts/tests/test_period.py +0 -137
  143. package/plugin/skills/agent-cost-report/scripts/tests/test_prices.py +0 -126
  144. package/plugin/skills/agent-cost-report/scripts/tests/test_render.py +0 -238
  145. package/plugin/skills/agent-cost-report/scripts/tests/test_render_encoding.py +0 -70
  146. package/plugin/skills/agent-cost-report/scripts/tests/test_rollup.py +0 -263
  147. package/plugin/skills/agent-cost-report/scripts/tests/test_snapshot.py +0 -63
  148. package/plugin/skills/agent-cost-report/scripts/tests/test_sync.py +0 -52
  149. package/plugin/skills/agent-cost-report/scripts/tests/test_transcript_encoding.py +0 -109
  150. package/plugin/skills/agent-cost-report/scripts/tests/test_transcripts.py +0 -151
  151. package/plugin/skills/agent-cost-report/scripts/tests/test_wins.py +0 -222
  152. package/plugin/skills/babysit/SKILL.md +0 -87
  153. package/plugin/skills/design-is/SKILL.md +0 -312
  154. package/plugin/skills/do/SKILL.md +0 -45
  155. package/plugin/skills/learn-codebase/SKILL.md +0 -21
  156. package/plugin/skills/make-plan/SKILL.md +0 -67
  157. package/plugin/skills/oh-my-issues/SKILL.md +0 -226
  158. package/plugin/skills/pathfinder/SKILL.md +0 -111
  159. package/plugin/skills/screenpipe/SKILL.md +0 -78
  160. package/plugin/skills/screenpipe/screenpipe.mjs +0 -196
  161. package/plugin/skills/smart-explore/SKILL.md +0 -193
  162. package/plugin/skills/standup/SKILL.md +0 -142
  163. package/plugin/skills/standup/agent-brief.md +0 -47
  164. package/plugin/skills/standup/standup.mjs +0 -586
  165. package/plugin/skills/what-the/SKILL.md +0 -6
  166. package/plugin/skills/wowerpoint/SKILL.md +0 -205
@@ -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
@@ -1,89 +0,0 @@
1
- """Phase 4: measured OpenRouter spend via the per-key endpoint (plan 4.1-4.4; G5 settled: a regular
2
- inference OPENROUTER_API_KEY only, env var only, never a settings file, never a management key).
3
-
4
- GET https://openrouter.ai/api/v1/key with `Authorization: Bearer <key>` returns data.usage (lifetime),
5
- usage_daily (current UTC day), usage_weekly (current UTC week Mon-Sun), usage_monthly (current UTC month),
6
- limit, limit_remaining, is_free_tier (verified 2026-09-25, plan Phase 0 E). Snapshots of the current
7
- UTC calendar buckets, not a range query, so the figure is shown with its own bucket label and is never
8
- re-cut to the PT window. The key is read once from the environment, never printed, never written.
9
- """
10
- import datetime as dt
11
- import json
12
- import os
13
- import urllib.error
14
- import urllib.request
15
-
16
- from .period import PT
17
-
18
- URL = "https://openrouter.ai/api/v1/key"
19
- ENV = "OPENROUTER_API_KEY"
20
- UNAVAILABLE = dict(status="unavailable", reason=f"{ENV} not provided")
21
- NOTE_TAKER = "measured total includes note-taker calls if they share this key; the estimate excludes them"
22
-
23
-
24
- def fetch(key, url=URL, opener=urllib.request.urlopen, timeout=30):
25
- req = urllib.request.Request(url, headers={"Authorization": f"Bearer {key}"})
26
- with opener(req, timeout=timeout) as resp: return json.load(resp)
27
-
28
-
29
- def measure(outdir, env=None, opener=urllib.request.urlopen, now=None, url=URL):
30
- """Writes <outdir>/measured.json and returns it. No key material in the output, ever."""
31
- env = os.environ if env is None else env
32
- key = env.get(ENV)
33
- now = now or dt.datetime.now(dt.timezone.utc)
34
- if not key: out = dict(UNAVAILABLE)
35
- else:
36
- try:
37
- d = (fetch(key, url=url, opener=opener) or {}).get("data") or {}
38
- out = dict(status="ok", source=url, fetched_at_utc=now.isoformat(timespec="seconds"), fetched_at_pt=now.astimezone(PT).isoformat(timespec="minutes"),
39
- usage_daily=d.get("usage_daily"), usage_weekly=d.get("usage_weekly"), usage_monthly=d.get("usage_monthly"), usage_lifetime=d.get("usage"),
40
- limit=d.get("limit"), limit_remaining=d.get("limit_remaining"), is_free_tier=d.get("is_free_tier"), key_hint=None)
41
- except urllib.error.HTTPError as ex: out = dict(status="error", http_status=ex.code)
42
- except (urllib.error.URLError, OSError, ValueError) as ex: out = dict(status="error", http_status=None, reason=type(ex).__name__)
43
- del key
44
- os.makedirs(outdir, exist_ok=True)
45
- with open(os.path.join(outdir, "measured.json"), "w", encoding="utf-8") as fh: json.dump(out, fh, indent=1)
46
- return out
47
-
48
-
49
- def _week_start(t):
50
- return (t - dt.timedelta(days=t.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
51
-
52
-
53
- def bucket_rule(measured, window_block, now=None):
54
- """Plan 4.2: pick the UTC bucket (week, else month) whose figure is shown, say whether it fully covers
55
- the PT report window (bucket_start_utc <= window_start_utc and now >= window_end_utc), and label it."""
56
- if not measured or measured.get("status") != "ok": return None
57
- fetched = dt.datetime.fromisoformat(measured["fetched_at_utc"]); now = now or fetched
58
- if window_block.get("start_epoch_ms") is None or window_block.get("end_epoch_ms") is None:
59
- week0 = _week_start(fetched)
60
- return dict(bucket="usage_weekly", bucket_start_utc=week0.isoformat(timespec="seconds"), usd=measured.get("usage_weekly"),
61
- label=f"OpenRouter measured, current UTC week {week0:%a} {week0.day} {week0:%b} – now", covers_window=False, window_inside_bucket=False,
62
- note="a session run has no period window; the measured bucket is shown for reference only", note_taker=NOTE_TAKER)
63
- ws = dt.datetime.fromtimestamp(window_block["start_epoch_ms"] / 1000, dt.timezone.utc); we = dt.datetime.fromtimestamp(window_block["end_epoch_ms"] / 1000, dt.timezone.utc)
64
- week0 = _week_start(fetched); month0 = fetched.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
65
- if ws >= week0: name, start, usd = "usage_weekly", week0, measured.get("usage_weekly")
66
- else: name, start, usd = "usage_monthly", month0, measured.get("usage_monthly")
67
- covers = usd is not None and start <= ws and now >= we
68
- inside = start <= ws and we <= now + dt.timedelta(seconds=1)
69
- label = f"OpenRouter measured, current UTC {'week' if name == 'usage_weekly' else 'month'} {start:%a} {start.day} {start:%b} – now"
70
- note = None if covers else "measured bucket does not match the report window; shown for reference"
71
- return dict(bucket=name, bucket_start_utc=start.isoformat(timespec="seconds"), usd=usd, label=label, covers_window=bool(covers), window_inside_bucket=bool(inside), note=note, note_taker=NOTE_TAKER)
72
-
73
-
74
- def apply(report, measured, now=None):
75
- """Mutates report['spend']: the total switches to MEASURED only when the bucket fully covers the window;
76
- line items keep cost_basis estimated_usage (per-session measured cost is not available, plan 4.2)."""
77
- s = report["spend"]
78
- if not measured or measured.get("status") != "ok":
79
- s["agent_measured_usd"] = None
80
- s["measured_status"] = "unavailable" if not measured or measured.get("status") == "unavailable" else f"error (http {measured.get('http_status')})"
81
- s["measured_reference"] = None
82
- return s
83
- b = bucket_rule(measured, report["window"], now=now)
84
- s["measured_reference"] = b
85
- if b and b["covers_window"]:
86
- s["agent_measured_usd"] = round(float(b["usd"]), 2); s["measured_status"] = "ok (period bucket)"; s["measured_label"] = "MEASURED"
87
- else:
88
- s["agent_measured_usd"] = None; s["measured_status"] = "reference only (bucket does not cover the window)"
89
- return s