memindex 1.4.4 → 1.4.6

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 (149) 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 +9 -1
  6. package/dist/bug-report/index.js +94 -83
  7. package/dist/npx-cli/index.js +250 -249
  8. package/dist/pi-extension/TYPEBOX-PROVENANCE.json +4 -4
  9. package/dist/pi-extension/index.js +9 -9
  10. package/integrations/dsh/package.json +1 -1
  11. package/integrations/openclaw/openclaw.plugin.json +1 -5
  12. package/integrations/pi/TYPEBOX-PROVENANCE.json +4 -4
  13. package/package.json +5 -5
  14. package/plugin/.claude-plugin/plugin.json +1 -1
  15. package/plugin/.codex-plugin/plugin.json +1 -1
  16. package/plugin/.qoder-plugin/plugin.json +1 -1
  17. package/plugin/package.json +1 -1
  18. package/plugin/scripts/context-generator.cjs +62 -61
  19. package/plugin/scripts/context-generator.cjs.map +3 -3
  20. package/plugin/scripts/mcp-server.cjs +2 -2
  21. package/plugin/scripts/transcript-watcher.cjs +2 -2
  22. package/plugin/scripts/transcript-watcher.cjs.map +1 -1
  23. package/plugin/scripts/worker-service.cjs +677 -669
  24. package/plugin/scripts/worker-service.cjs.map +3 -3
  25. package/plugin/skills/distill/SKILL.md +22 -0
  26. package/plugin/skills/how-it-works/SKILL.md +1 -1
  27. package/plugin/skills/how-it-works/onboarding-explainer.md +1 -1
  28. package/plugin/skills/import-chats/SKILL.md +22 -0
  29. package/plugin/skills/import-jira/SKILL.md +22 -0
  30. package/plugin/skills/import-reviews/SKILL.md +22 -0
  31. package/plugin/skills/pin/SKILL.md +26 -0
  32. package/plugin/skills/savings/SKILL.md +22 -0
  33. package/integrations/openclaw/skills/agent-cost-report/CHECKSUMS.txt +0 -50
  34. package/integrations/openclaw/skills/agent-cost-report/SKILL.md +0 -241
  35. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/__init__.py +0 -1
  36. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/behavior.py +0 -303
  37. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/classify.py +0 -83
  38. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/costs.py +0 -119
  39. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/devices.py +0 -88
  40. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/evidence.py +0 -160
  41. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/labels.py +0 -115
  42. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/measure.py +0 -89
  43. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/mistakes.py +0 -310
  44. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/patterns.py +0 -391
  45. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/period.py +0 -98
  46. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/prices.py +0 -95
  47. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/render.py +0 -586
  48. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rollup.py +0 -355
  49. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rules.json +0 -4
  50. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/rules.py +0 -76
  51. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/sample.py +0 -45
  52. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/snapshot.py +0 -53
  53. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/sync.py +0 -79
  54. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/transcripts.py +0 -126
  55. package/integrations/openclaw/skills/agent-cost-report/scripts/acr/wins.py +0 -341
  56. package/integrations/openclaw/skills/agent-cost-report/scripts/acr.py +0 -219
  57. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/_db.py +0 -49
  58. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/_paths.py +0 -9
  59. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/codex/sessions/2026/09/18/rollout-codex-1.jsonl +0 -4
  60. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/make_report_fixtures.py +0 -46
  61. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/prices_sample.json +0 -6
  62. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-a/sess-aaaa.jsonl +0 -8
  63. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-b/sess-bbbb.jsonl +0 -1
  64. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-1day.json +0 -823
  65. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-30day.json +0 -2213
  66. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-7day.json +0 -1252
  67. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-empty.json +0 -536
  68. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/fixtures/report-session.json +0 -662
  69. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_behavior.py +0 -299
  70. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_devices.py +0 -99
  71. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_labels.py +0 -96
  72. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_measure.py +0 -120
  73. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_pdf_windows.py +0 -54
  74. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_period.py +0 -137
  75. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_prices.py +0 -126
  76. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_render.py +0 -238
  77. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_render_encoding.py +0 -70
  78. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_rollup.py +0 -263
  79. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_snapshot.py +0 -63
  80. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_sync.py +0 -52
  81. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_transcript_encoding.py +0 -109
  82. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_transcripts.py +0 -151
  83. package/integrations/openclaw/skills/agent-cost-report/scripts/tests/test_wins.py +0 -222
  84. package/plugin/skills/agent-cost-report/CHECKSUMS.txt +0 -50
  85. package/plugin/skills/agent-cost-report/SKILL.md +0 -241
  86. package/plugin/skills/agent-cost-report/scripts/acr/__init__.py +0 -1
  87. package/plugin/skills/agent-cost-report/scripts/acr/behavior.py +0 -303
  88. package/plugin/skills/agent-cost-report/scripts/acr/classify.py +0 -83
  89. package/plugin/skills/agent-cost-report/scripts/acr/costs.py +0 -119
  90. package/plugin/skills/agent-cost-report/scripts/acr/devices.py +0 -88
  91. package/plugin/skills/agent-cost-report/scripts/acr/evidence.py +0 -160
  92. package/plugin/skills/agent-cost-report/scripts/acr/labels.py +0 -115
  93. package/plugin/skills/agent-cost-report/scripts/acr/measure.py +0 -89
  94. package/plugin/skills/agent-cost-report/scripts/acr/mistakes.py +0 -310
  95. package/plugin/skills/agent-cost-report/scripts/acr/patterns.py +0 -391
  96. package/plugin/skills/agent-cost-report/scripts/acr/period.py +0 -98
  97. package/plugin/skills/agent-cost-report/scripts/acr/prices.py +0 -95
  98. package/plugin/skills/agent-cost-report/scripts/acr/render.py +0 -586
  99. package/plugin/skills/agent-cost-report/scripts/acr/rollup.py +0 -355
  100. package/plugin/skills/agent-cost-report/scripts/acr/rules.json +0 -4
  101. package/plugin/skills/agent-cost-report/scripts/acr/rules.py +0 -76
  102. package/plugin/skills/agent-cost-report/scripts/acr/sample.py +0 -45
  103. package/plugin/skills/agent-cost-report/scripts/acr/snapshot.py +0 -53
  104. package/plugin/skills/agent-cost-report/scripts/acr/sync.py +0 -79
  105. package/plugin/skills/agent-cost-report/scripts/acr/transcripts.py +0 -126
  106. package/plugin/skills/agent-cost-report/scripts/acr/wins.py +0 -341
  107. package/plugin/skills/agent-cost-report/scripts/acr.py +0 -219
  108. package/plugin/skills/agent-cost-report/scripts/tests/_db.py +0 -49
  109. package/plugin/skills/agent-cost-report/scripts/tests/_paths.py +0 -9
  110. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/codex/sessions/2026/09/18/rollout-codex-1.jsonl +0 -4
  111. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/make_report_fixtures.py +0 -46
  112. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/prices_sample.json +0 -6
  113. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-a/sess-aaaa.jsonl +0 -8
  114. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/projects/-workspace-proj-b/sess-bbbb.jsonl +0 -1
  115. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-1day.json +0 -823
  116. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-30day.json +0 -2213
  117. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-7day.json +0 -1252
  118. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-empty.json +0 -536
  119. package/plugin/skills/agent-cost-report/scripts/tests/fixtures/report-session.json +0 -662
  120. package/plugin/skills/agent-cost-report/scripts/tests/test_behavior.py +0 -299
  121. package/plugin/skills/agent-cost-report/scripts/tests/test_devices.py +0 -99
  122. package/plugin/skills/agent-cost-report/scripts/tests/test_labels.py +0 -96
  123. package/plugin/skills/agent-cost-report/scripts/tests/test_measure.py +0 -120
  124. package/plugin/skills/agent-cost-report/scripts/tests/test_pdf_windows.py +0 -54
  125. package/plugin/skills/agent-cost-report/scripts/tests/test_period.py +0 -137
  126. package/plugin/skills/agent-cost-report/scripts/tests/test_prices.py +0 -126
  127. package/plugin/skills/agent-cost-report/scripts/tests/test_render.py +0 -238
  128. package/plugin/skills/agent-cost-report/scripts/tests/test_render_encoding.py +0 -70
  129. package/plugin/skills/agent-cost-report/scripts/tests/test_rollup.py +0 -263
  130. package/plugin/skills/agent-cost-report/scripts/tests/test_snapshot.py +0 -63
  131. package/plugin/skills/agent-cost-report/scripts/tests/test_sync.py +0 -52
  132. package/plugin/skills/agent-cost-report/scripts/tests/test_transcript_encoding.py +0 -109
  133. package/plugin/skills/agent-cost-report/scripts/tests/test_transcripts.py +0 -151
  134. package/plugin/skills/agent-cost-report/scripts/tests/test_wins.py +0 -222
  135. package/plugin/skills/babysit/SKILL.md +0 -87
  136. package/plugin/skills/design-is/SKILL.md +0 -312
  137. package/plugin/skills/do/SKILL.md +0 -45
  138. package/plugin/skills/learn-codebase/SKILL.md +0 -21
  139. package/plugin/skills/make-plan/SKILL.md +0 -67
  140. package/plugin/skills/oh-my-issues/SKILL.md +0 -226
  141. package/plugin/skills/pathfinder/SKILL.md +0 -111
  142. package/plugin/skills/screenpipe/SKILL.md +0 -78
  143. package/plugin/skills/screenpipe/screenpipe.mjs +0 -196
  144. package/plugin/skills/smart-explore/SKILL.md +0 -193
  145. package/plugin/skills/standup/SKILL.md +0 -142
  146. package/plugin/skills/standup/agent-brief.md +0 -47
  147. package/plugin/skills/standup/standup.mjs +0 -586
  148. package/plugin/skills/what-the/SKILL.md +0 -6
  149. package/plugin/skills/wowerpoint/SKILL.md +0 -205
@@ -1,310 +0,0 @@
1
- """Phase 2B assembly (plan 2B.2, 2B.5, 2B.9): run the behavior pass, price the same-session union
2
- of wasted turns once (the low figure = the headline), keep the upper bound in Details only, and hand
3
- `behavior`, `spend.mistakes_*`, `timeline.mistakes_by_day` and per-line-item fields to the report.
4
-
5
- One source of truth: the ribbon's waste sliver, line-item `wasted_cost` and the mistakes line all read
6
- the union set built here. There is no second waste calculation anywhere else.
7
- """
8
- import collections
9
- import datetime as dt
10
- import hashlib
11
-
12
- from . import behavior, costs, patterns, prices, rules
13
- from . import classify as classify_mod
14
- from .period import PT
15
-
16
- UPPER_LABEL = "upper bound, likely 5–10× too high (behavior study)"
17
- MISTAKES_BASIS = "same-session wasted turns, each turn once"
18
- TILE_MAX = 4
19
-
20
-
21
- def classify_pass(flags, sessions, pricer, opts):
22
- """2B.4: only candidates the heuristics could not settle (P1 asks with no boost). A 'yes' confirms
23
- the flag with label_source classifier; 'no' drops it. Spend is reported separately, never added."""
24
- cands = []; by_id = {}
25
- for i, f in enumerate(flags):
26
- if f["pattern"] == "P1_invented_gates" and f.get("unconfirmed"):
27
- S = sessions.get(f["session"]); users = [u for u in (S["users"] if S else []) if u["text"] and not u["results"]]
28
- before = next((u["text"] for u in reversed(users) if u["ts_ms"] < (f["ts_ms"] or 0)), "")
29
- after = next((u["text"] for u in users if u["ts_ms"] > (f["ts_ms"] or 0)), "")
30
- cid = f"c{i}"; by_id[cid] = f; cands.append(dict(id=cid, kind="P1_invented_gates", text=f["excerpt"], before=before, after=after))
31
- model = opts.get("model") or classify_mod.DEFAULT_MODEL
32
- try:
33
- labels, summary = classify_mod.run(cands, pricer.rate(model), model=model, cap_usd=opts.get("cap_usd") or classify_mod.CAP_USD, call=opts.get("call") or classify_mod._call)
34
- except classify_mod.ClassifierError as ex:
35
- return dict(ran=False, model=model, spend_usd=None, cap_usd=opts.get("cap_usd") or classify_mod.CAP_USD, sampled_n=0, error=str(ex))
36
- for cid, lab in labels.items():
37
- f = by_id[cid]; f["label_source"] = "classifier"; f["classifier"] = lab
38
- if lab["label"] == "yes": f.pop("unconfirmed", None); f["basis"] += "; classifier: invented gate"
39
- return summary
40
-
41
-
42
- def _day_list(s_ms, e_ms):
43
- d = dt.datetime.fromtimestamp(s_ms / 1000, PT).date(); end = dt.datetime.fromtimestamp(e_ms / 1000, PT).date()
44
- out = []
45
- while d < end: out.append(d.isoformat()); d += dt.timedelta(days=1)
46
- return out
47
-
48
-
49
- def default_model(sessions, pricer):
50
- """P3 comparison basis: the most-used priced Claude model in the window, else the plan's fallback."""
51
- c = collections.Counter()
52
- for S in sessions.values():
53
- for T in S["turns"].values():
54
- if T["model"] and T["usage"]:
55
- m = prices.norm(T["model"])
56
- if m.startswith("anthropic/claude") and pricer.usable(m): c[m] += 1
57
- return c.most_common(1)[0][0] if c else patterns.P3_FALLBACK_DEFAULT
58
-
59
-
60
- def mistake_id(session, first_key):
61
- return "MK-" + hashlib.sha1(f"{session}|{first_key}".encode()).hexdigest()[:10]
62
-
63
-
64
- def add_usage_turns(sessions, usage_rows, src="codex"):
65
- """Codex rows (usage.json) have a model id and tokens but no readable text on the box; they join as
66
- text-less turns so P3 (wrong model) and the union see every logged model (R3)."""
67
- for i, r in enumerate(usage_rows):
68
- if r.get("src") != src: continue
69
- S = sessions.setdefault(r["session"], behavior.new_session()); S.setdefault("tag", "unknown")
70
- key = (f"{src}:{r['session']}", i)
71
- S["turns"][key] = dict(key=key, ts_ms=int(behavior.transcripts.ts(r["ts"]).timestamp() * 1000), model=r["model"], text="", tool_uses=[],
72
- usage={f: r[f] for f in behavior.FIELDS}, sidechain=False, file=r.get("file"), uuid=None)
73
-
74
-
75
- UNTAGGED = "unavailable (prompts not tagged human vs bot)"
76
-
77
-
78
- ALTERNATES = ("P4_over_engineering", "P5_not_asked", "P8_wrong_tool", "P10_memory_loss")
79
- PRECISION_GATE = 0.70
80
-
81
-
82
- def precision_gate(pats, precision):
83
- """Phase 8.5 / 2B.5: a tile pattern whose spot-check precision is under 70% moves to Details as low
84
- confidence; the next pattern by ranking takes its place only if it passes."""
85
- if not precision: return
86
- freed = 0
87
- for p in pats:
88
- pr = precision.get(p["key"])
89
- if pr is None: continue
90
- p["precision"] = pr
91
- if p["placement"] == "tile" and pr < PRECISION_GATE: p["placement"] = "details"; p["confidence"] = "low"; p["gate"] = f"spot-check precision {pr:.0%} < 70%: moved to Details"; freed += 1
92
- for key in ALTERNATES:
93
- if freed <= 0: break
94
- p = next((x for x in pats if x["key"] == key), None)
95
- if p and precision.get(key) is not None and precision[key] >= PRECISION_GATE: p["placement"] = "tile"; p["gate"] = f"promoted: precision {precision[key]:.0%}"; freed -= 1
96
-
97
-
98
- def run(db, scope, window_block, pricer, session_projects, items, classify=None, rules_dir=None, now_ms=None, usage_rows=(),
99
- glob_pattern=None, tagger=True, precision=None):
100
- """session_projects: {content_session_id: project}. items: Phase 2 line items (mutated: behavior_counts,
101
- wasted_cost, recovery_cost, productive_cost, failure_signals). tagger=False models the R2 gate: no count
102
- that reads user messages runs, and those counts render UNTAGGED."""
103
- s_ms, e_ms = scope.S, scope.E
104
- sessions, scan_stats = behavior.scan(s_ms, e_ms, session=scope.session if scope.kind == "session" else None, **({"pattern": glob_pattern} if glob_pattern else {}))
105
- if scope.kind == "project":
106
- sessions = {sid: S for sid, S in sessions.items() if sid in session_projects}
107
- for S in sessions.values(): behavior.tag_user_turns(S)
108
- add_usage_turns(sessions, usage_rows)
109
- behavior.price_turns(sessions, pricer)
110
- if tagger:
111
- prompt_rows = behavior.load_prompt_rows(db, scope)
112
- if scope.kind == "project": prompt_rows = [r for r in prompt_rows if r["session"] in session_projects]
113
- msgs, author_tags = behavior.human_messages(sessions, prompt_rows)
114
- else:
115
- prompt_rows = []; msgs = {}; author_tags = UNTAGGED
116
- for S in sessions.values():
117
- for u in S["users"]: u["author"] = "unknown" if u.get("author") != "tool" else "tool"
118
- tagger_ran = tagger
119
- dm = default_model(sessions, pricer)
120
-
121
- flags = []; denials = []; stats = collections.Counter()
122
- for sid, S in sessions.items():
123
- f, d, st = patterns.run_session(S, sid, msgs, dm, pricer); flags += f; denials += d
124
- for k, v in st.items(): stats[k] += v
125
- turn_by_key = {T["key"]: T for S in sessions.values() for T in S["turns"].values()}
126
- classifier_block = classify_pass(flags, sessions, pricer, classify) if classify and classify.get("enabled") else None
127
- flagged = collections.defaultdict(set)
128
- for f in flags:
129
- if f.get("unconfirmed"): continue
130
- if f["key"] is not None: flagged[f["pattern"]].add(f["key"])
131
- for k in f.get("extra_keys", []) + f.get("wasted_extra", []): flagged[f["pattern"]].add(k)
132
- flagged["S2_hedging"] = {f["key"] for f in flags if f["pattern"] == "S2_hedging" and f.get("priced")}
133
- flagged["P11_jargon"] = set()
134
- flagged["P9_bad_outbound"] = set()
135
- redo_keys = collections.defaultdict(set)
136
- for f in flags:
137
- for k in f.get("recovery_keys", []): redo_keys["S1_tool_errors"].add(k)
138
-
139
- episodes = []; ep_flag_keys = set(); ep_redo_keys = set(); project_fallback = set()
140
- for ep in behavior.find_episodes(msgs, phrase_hit=patterns.human_phrase_patterns):
141
- S = sessions.get(ep["session"])
142
- wasted, redo, last_agent = behavior.episode_windows(ep, S, msgs)
143
- wkeys = {T["key"] for T in wasted}
144
- pat = next((p for p in patterns.TILE_ORDER + tuple(k for k, *_ in patterns.PATTERNS) if flagged.get(p) and flagged[p] & wkeys), None)
145
- if pat is None:
146
- hp = collections.Counter(p for m in ep["messages"] for p in patterns.human_phrase_patterns(m["text"]))
147
- order = [k for k, *_ in patterns.PATTERNS]
148
- pat = min(hp, key=lambda k: (-hp[k], order.index(k))) if hp else "P12_unclear"
149
- measured = S is not None and bool(S["turns"])
150
- if measured:
151
- ep_flag_keys |= wkeys; ep_redo_keys |= {T["key"] for T in redo}
152
- flagged[pat] |= wkeys
153
- else:
154
- proj = session_projects.get(ep["session"])
155
- if proj:
156
- for sid2, S2 in sessions.items():
157
- if session_projects.get(sid2) == proj:
158
- for T in S2["turns"].values():
159
- if ep["first_ts"] - behavior.WASTED_CAP_MS <= T["ts_ms"] < ep["first_ts"]: project_fallback.add(T["key"])
160
- model = None
161
- if S:
162
- mc = collections.Counter(prices.norm(T["model"]) for T in S["turns"].values() if T["model"])
163
- model = mc.most_common(1)[0][0] if mc else None
164
- low = behavior.turn_cost(wasted) if measured else None
165
- high = (low + behavior.turn_cost(redo)) if measured else None
166
- episodes.append(dict(episode_id=ep["episode_id"], session=ep["session"], ts_pt=behavior.pt_iso(ep["first_ts"]), day_pt=behavior.day_pt(ep["first_ts"]),
167
- pattern=pat, messages_n=len(ep["messages"]), low_usd=costs.usd(low) if measured else None, high_usd=costs.usd(high) if measured else None,
168
- cost_status="measured_tokens" if measured else "unmeasured", user_minutes=behavior.user_minutes(ep, last_agent),
169
- model=model, model_status="observed" if model else "assumed", source=ep["messages"][0]["source"],
170
- device=ep["messages"][0].get("device", "local"), excerpt=behavior.scrub(ep["messages"][0]["text"]), wasted_keys=[list(k) for k in wkeys]))
171
-
172
- span_keys = collections.defaultdict(set)
173
- for sid, S in sessions.items():
174
- for ft, keys in behavior.failure_spans(S, msgs).items(): span_keys[ft] |= keys
175
- for f in flags:
176
- if f["pattern"] == "S1_tool_errors" and f.get("loop"): span_keys["Looping"].add(f["key"])
177
-
178
- union = set().union(*flagged.values()) | ep_flag_keys | set().union(*span_keys.values()) if (flagged or span_keys) else set(ep_flag_keys)
179
- union = {k for k in union if k in turn_by_key}
180
- redo_all = (ep_redo_keys | set().union(*redo_keys.values()) if redo_keys else ep_redo_keys) - union
181
- high_set = union | redo_all | (project_fallback - union)
182
- low_x1e6 = sum(turn_by_key[k]["x1e6"] or 0 for k in union)
183
- high_x1e6 = sum(turn_by_key[k]["x1e6"] or 0 for k in high_set)
184
- unmeasured_n = sum(1 for e in episodes if e["cost_status"] == "unmeasured")
185
-
186
- by_ep = collections.Counter(e["pattern"] for e in episodes)
187
- ex_by_pat = collections.defaultdict(list)
188
- for f in flags:
189
- if not f.get("unconfirmed") and len(ex_by_pat[f["pattern"]]) < 10:
190
- ex_by_pat[f["pattern"]].append(dict(content_session_id=f["session"], ts_pt=behavior.pt_iso(f["ts_ms"]) if f["ts_ms"] else None, excerpt=f["excerpt"], basis=f["basis"], label_source=f["label_source"]))
191
- pats = []
192
- for key, name, mi, ft, place in patterns.PATTERNS:
193
- keys = flagged.get(key, set()); fl = [f for f in flags if f["pattern"] == key and not f.get("unconfirmed")]
194
- count = len(fl) + by_ep.get(key, 0) if key not in ("P12_unclear",) else by_ep.get(key, 0)
195
- low = None if key == "P9_bad_outbound" else costs.usd(sum(turn_by_key[k]["x1e6"] or 0 for k in keys if k in turn_by_key))
196
- hk = keys | {k for k in redo_all if key == "S1_tool_errors"} | set().union(*[set(map(tuple, e["wasted_keys"])) for e in episodes if e["pattern"] == key] or [set()])
197
- high = None if key == "P9_bad_outbound" else costs.usd(sum(turn_by_key[k]["x1e6"] or 0 for k in hk if k in turn_by_key))
198
- p = dict(key=key, name=name, source_ref=f"metrics-ideas.md:{mi}" if mi else "plan 2B.3 supporting metric", failure_type=ft, placement=place if key in patterns.TILE_ORDER else "details",
199
- count=count, count_basis="heuristic", low_usd=low, high_usd=high, high_label=UPPER_LABEL, unmeasured_n=sum(1 for e in episodes if e["pattern"] == key and e["cost_status"] == "unmeasured"),
200
- user_minutes=round(sum(e["user_minutes"] for e in episodes if e["pattern"] == key) + sum(f.get("user_minutes", 0) for f in fl), 1),
201
- agent_minutes=None, waiting_minutes=round(sum(f.get("waiting_minutes") or 0 for f in fl), 1) if key == "P1_invented_gates" else None,
202
- confidence="high" if key in ("P3_wrong_model", "S1_tool_errors", "P9_bad_outbound") else "medium", overlaps=True, examples=ex_by_pat.get(key, []),
203
- detection="H" if key != "P12_unclear" else "base detector; M attributes when it can")
204
- if key == "P3_wrong_model":
205
- p["delta_usd"] = costs.usd(sum(max(f.get("delta_x1e6") or 0, 0) for f in fl)); p["default_model"] = dm; p["model_not_logged"] = stats["model_not_logged"]
206
- if key == "P1_invented_gates":
207
- p["unconfirmed_n"] = sum(1 for f in flags if f["pattern"] == key and f.get("unconfirmed")); p["never_answered_n"] = sum(1 for f in fl if f.get("never_answered"))
208
- if key == "P9_bad_outbound":
209
- p["incidents"] = len(fl); p["recipients"] = sum(f.get("recipients") or 0 for f in fl); p["cost_note"] = "incidents × recipients, never dollars"
210
- if key == "S1_tool_errors": p["error_chains"] = stats["error_chains"]; p["loops"] = stats["loops"]; p["recovered_usd"] = costs.usd(sum(turn_by_key[k]["x1e6"] or 0 for k in redo_keys["S1_tool_errors"] if k in turn_by_key))
211
- if key == "S2_hedging": p["availability_unknown_n"] = stats["hedge_availability_unknown"]; p["cost_note"] = "text only, not priced" if not keys else "follow-up round trip priced as recovery"
212
- if key == "P11_jargon": p["cost_note"] = "user-minutes = words ÷ 200; dollars only for a clarification round trip"
213
- pats.append(p)
214
- precision_gate(pats, precision)
215
- tiles = [p for p in pats if p["placement"] == "tile"]
216
- assert len(tiles) <= 5 and len({("tile2" if p["key"] in patterns.MERGED_TILE else p["key"]) for p in tiles}) <= TILE_MAX
217
-
218
- per_session = collections.defaultdict(lambda: dict(low=0, redo=0, counts=collections.Counter(), failures=set()))
219
- for k in union:
220
- T = turn_by_key[k]; sid = next(s for s, S in sessions.items() if k in S["turns"]); per_session[sid]["low"] += T["x1e6"] or 0
221
- for k in redo_all:
222
- T = turn_by_key[k]; sid = next(s for s, S in sessions.items() if k in S["turns"]); per_session[sid]["redo"] += T["x1e6"] or 0
223
- for f in flags:
224
- if f.get("unconfirmed"): continue
225
- per_session[f["session"]]["counts"][f["pattern"]] += 1
226
- ft = f.get("failure_type") or dict((k, t) for k, _, _, t, _ in patterns.PATTERNS).get(f["pattern"])
227
- if ft: per_session[f["session"]]["failures"].add(ft)
228
- for e in episodes: per_session[e["session"]]["counts"][e["pattern"]] += 1
229
- def _reconciled(field, total_x1e6):
230
- """Round per item, then give the cent remainder to the largest item so the items sum to the total exactly (8.6)."""
231
- vals = {li["content_session_id"]: (per_session[li["content_session_id"]][field] if li["content_session_id"] in per_session else 0) for li in items}
232
- rounded = {k: costs.usd(v) for k, v in vals.items()}; diff = round(costs.usd(total_x1e6) - sum(rounded.values()), 2)
233
- if diff and vals:
234
- k = max(vals, key=vals.get); rounded[k] = round(rounded[k] + diff, 2)
235
- return rounded
236
- in_items = {li["content_session_id"] for li in items}
237
- low_in_items = sum(v["low"] for k, v in per_session.items() if k in in_items); redo_in_items = sum(v["redo"] for k, v in per_session.items() if k in in_items)
238
- wasted_by = _reconciled("low", low_in_items); redo_by = _reconciled("redo", redo_in_items)
239
- for li in items:
240
- ps = per_session.get(li["content_session_id"])
241
- li["behavior_counts"] = dict(ps["counts"]) if ps else {}
242
- li["wasted_cost"] = wasted_by.get(li["content_session_id"], 0.0)
243
- li["recovery_cost"] = redo_by.get(li["content_session_id"], 0.0)
244
- li["productive_cost"] = round(max(li["attributed_usd"] - li["wasted_cost"], 0.0), 2)
245
- if ps and ps["failures"]:
246
- li["failure_signals"] = sorted(set(li["failure_signals"]) | ps["failures"]); li["failure_type"] = li["failure_type"] or li["failure_signals"][0]
247
-
248
- days = _day_list(s_ms, e_ms) if scope.kind != "session" else sorted({behavior.day_pt(T["ts_ms"]) for T in turn_by_key.values()} | {e["day_pt"] for e in episodes})
249
- by_day = {d: dict(day_pt=d, usd=0, high_usd=0, unmeasured_n=0, user_minutes=0.0, turns_n=0, by_pattern=collections.Counter(), outbound_incidents=0, top_examples=[]) for d in days}
250
- key_pats = collections.defaultdict(set)
251
- for p, ks in flagged.items():
252
- for k in ks: key_pats[k].add(p)
253
- for k in union:
254
- T = turn_by_key[k]; d = by_day.get(behavior.day_pt(T["ts_ms"]))
255
- if d is None: continue
256
- d["usd"] += T["x1e6"] or 0; d["turns_n"] += 1
257
- for p in key_pats.get(k, ()): d["by_pattern"][p] += T["x1e6"] or 0
258
- for k in high_set:
259
- T = turn_by_key[k]; d = by_day.get(behavior.day_pt(T["ts_ms"]))
260
- if d is not None: d["high_usd"] += T["x1e6"] or 0
261
- for e in episodes:
262
- d = by_day.get(e["day_pt"])
263
- if d is None: continue
264
- d["user_minutes"] += e["user_minutes"]
265
- if e["cost_status"] == "unmeasured": d["unmeasured_n"] += 1
266
- if len(d["top_examples"]) < 3: d["top_examples"].append(dict(mistake_id=mistake_id(e["session"], e["episode_id"]), episode_id=e["episode_id"], pattern=e["pattern"], content_session_id=e["session"], ts_pt=e["ts_pt"]))
267
- for f in flags:
268
- if f["pattern"] == "P9_bad_outbound" and f["ts_ms"] and behavior.day_pt(f["ts_ms"]) in by_day: by_day[behavior.day_pt(f["ts_ms"])]["outbound_incidents"] += 1
269
- for d in by_day.values():
270
- d["usd"] = costs.usd(d["usd"]); d["high_usd"] = costs.usd(d["high_usd"]); d["user_minutes"] = round(d["user_minutes"], 1)
271
- d["by_pattern"] = {p: costs.usd(v) for p, v in d["by_pattern"].items()}
272
- clusters = []
273
- for sid, S in sessions.items():
274
- run = []
275
- for T in behavior.ordered_turns(S):
276
- if T["key"] in union: run.append(T)
277
- elif run: clusters.append((sid, run)); run = []
278
- if run: clusters.append((sid, run))
279
- mistake_ids = [dict(mistake_id=mistake_id(sid, r[0]["key"]), content_session_id=sid, ts_pt=behavior.pt_iso(r[0]["ts_ms"]), turns_n=len(r),
280
- usd=costs.usd(sum(T["x1e6"] or 0 for T in r)), patterns=sorted(set().union(*[key_pats.get(T["key"], set()) for T in r]))) for sid, r in clusters]
281
-
282
- rule_rows = rules.effectiveness(episodes, msgs, s_ms, e_ms, rules_dir=rules_dir)
283
-
284
- outbound = dict(incidents=sum(1 for f in flags if f["pattern"] == "P9_bad_outbound"), recipients=sum(f.get("recipients") or 0 for f in flags if f["pattern"] == "P9_bad_outbound"), user_minutes=None)
285
- user_known = [e["user_minutes"] for e in episodes if e["user_minutes"] is not None]
286
- unmatched_x1e6 = sum(turn_by_key[k]["x1e6"] or 0 for k in union if next(s for s, S in sessions.items() if k in S["turns"]) not in session_projects)
287
- spend = dict(mistakes_estimated_usd=costs.usd(low_x1e6), mistakes_label="ESTIMATED", mistakes_basis=MISTAKES_BASIS, mistakes_turns_n=len(union),
288
- mistakes_unmatched_usd=costs.usd(unmatched_x1e6),
289
- mistakes_episodes_n=len(episodes), mistakes_unmeasured_n=unmeasured_n, mistakes_user_minutes=round(sum(user_known), 1), mistakes_user_minutes_known_n=len(user_known),
290
- mistakes_high_usd=costs.usd(high_x1e6), mistakes_high_label=UPPER_LABEL)
291
- block = dict(patterns=pats, outbound=outbound, episodes=[{k: v for k, v in e.items() if k != "wasted_keys"} for e in episodes],
292
- union_low_usd=costs.usd(low_x1e6), union_high_usd=costs.usd(high_x1e6), union_turns_n=len(union), high_turns_n=len(high_set),
293
- author_tags=author_tags, tagger_ran=tagger_ran, session_tags=collections.Counter(S.get("tag", "unknown") for S in sessions.values()),
294
- human_message_counts=("computed from human-tagged turns" if tagger else UNTAGGED),
295
- episodes_status=("measured" if tagger else UNTAGGED),
296
- rule_effectiveness=rule_rows, coverage=dict(box="measured", mac="unavailable (no Phase 5 export)", grok_bot="unavailable"),
297
- classifier=classifier_block or dict(ran=False, model=None, spend_usd=None, cap_usd=classify_mod.CAP_USD, sampled_n=0, note="off by default (G8); enable with --classify"),
298
- permission_denials=dict(count=len(denials), examples=denials[:10], note="blocked by a rule; not priced as agent waste"),
299
- label_sources=collections.Counter(f["label_source"] for f in flags if not f.get("unconfirmed")),
300
- unconfirmed=dict(P1_candidates=[dict(content_session_id=f["session"], ts_pt=behavior.pt_iso(f["ts_ms"]), excerpt=f["excerpt"]) for f in flags if f.get("unconfirmed")][:50]),
301
- default_model=dm, scan=scan_stats, mistake_clusters=mistake_ids, prompt_rows_n=len(prompt_rows),
302
- not_in_mistakes_line="session-level keyword failure signals with no turn span stay in Details (2B.9)")
303
- raw = dict(flags=[dict(pattern=f["pattern"], session=f["session"], ts_pt=behavior.pt_iso(f["ts_ms"]) if f.get("ts_ms") else None, excerpt=f["excerpt"], basis=f["basis"],
304
- label_source=f["label_source"], unconfirmed=bool(f.get("unconfirmed"))) for f in flags],
305
- user_turns=[dict(session=sid, ts_pt=behavior.pt_iso(m["ts_ms"]), author=m["author"], source=m["source"], excerpt=behavior.scrub(m["text"])) for sid, ms in msgs.items() for m in ms],
306
- episodes=block["episodes"],
307
- sessions=[dict(session=sid, tag=S.get("tag", "unknown"), turns=len(S["turns"]), tool_errors=sum(1 for u in S["users"] for r in u["results"] if r["is_error"]),
308
- last_text=behavior.scrub(next((behavior.user_facing(T["text"]) for T in reversed(behavior.ordered_turns(S)) if T["text"].strip()), ""))) for sid, S in sessions.items()])
309
- block["_raw"] = raw
310
- return block, spend, list(by_day.values())