agent-usage-tracker 0.1.0__py3-none-any.whl
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.
- agent_usage_tracker/__init__.py +8 -0
- agent_usage_tracker/backup.py +225 -0
- agent_usage_tracker/cli.py +479 -0
- agent_usage_tracker/collectors/__init__.py +115 -0
- agent_usage_tracker/collectors/_util.py +310 -0
- agent_usage_tracker/collectors/activity_common.py +294 -0
- agent_usage_tracker/collectors/activitywatch.py +394 -0
- agent_usage_tracker/collectors/claude_code.py +484 -0
- agent_usage_tracker/collectors/claude_code_activity.py +317 -0
- agent_usage_tracker/collectors/codex.py +612 -0
- agent_usage_tracker/collectors/commits.py +154 -0
- agent_usage_tracker/collectors/cursor.py +193 -0
- agent_usage_tracker/collectors/cursor_api.py +247 -0
- agent_usage_tracker/collectors/cursor_cli.py +592 -0
- agent_usage_tracker/collectors/cursor_sdk.py +95 -0
- agent_usage_tracker/collectors/generic_sdk.py +100 -0
- agent_usage_tracker/collectors/pi.py +397 -0
- agent_usage_tracker/collectors/session_digest.py +379 -0
- agent_usage_tracker/collectors/t3.py +490 -0
- agent_usage_tracker/config.py +392 -0
- agent_usage_tracker/coverage.py +615 -0
- agent_usage_tracker/coverage_catalog.json +588 -0
- agent_usage_tracker/cursor_run.py +62 -0
- agent_usage_tracker/diagnostics.py +74 -0
- agent_usage_tracker/episode_store.py +78 -0
- agent_usage_tracker/event.py +132 -0
- agent_usage_tracker/git_day_cache.py +128 -0
- agent_usage_tracker/git_days.py +499 -0
- agent_usage_tracker/git_safe.py +58 -0
- agent_usage_tracker/harness_inventory.py +208 -0
- agent_usage_tracker/history_steers.py +88 -0
- agent_usage_tracker/improve/__init__.py +1 -0
- agent_usage_tracker/improve/case_view.py +112 -0
- agent_usage_tracker/improve/cli.py +138 -0
- agent_usage_tracker/improve/desktop.py +347 -0
- agent_usage_tracker/improve/evidence.py +322 -0
- agent_usage_tracker/improve/judge.py +671 -0
- agent_usage_tracker/improve/proposals.py +72 -0
- agent_usage_tracker/improve/rubric.py +360 -0
- agent_usage_tracker/improve/rubrics/avoidable_recovery.md +20 -0
- agent_usage_tracker/improve/rubrics/code_quality.md +22 -0
- agent_usage_tracker/improve/rubrics/completion_supported.md +20 -0
- agent_usage_tracker/improve/rubrics/redundant_tests.md +21 -0
- agent_usage_tracker/improve/rubrics/verbosity.md +21 -0
- agent_usage_tracker/improve/runtime.py +117 -0
- agent_usage_tracker/improve/schedule.py +291 -0
- agent_usage_tracker/improve/search_check.py +563 -0
- agent_usage_tracker/improve/store.py +244 -0
- agent_usage_tracker/improve/studies.py +285 -0
- agent_usage_tracker/improve/study_cli.py +46 -0
- agent_usage_tracker/improve/study_decision.py +121 -0
- agent_usage_tracker/improve/study_followup.py +174 -0
- agent_usage_tracker/improve/t3.py +269 -0
- agent_usage_tracker/improve/trial.py +318 -0
- agent_usage_tracker/improve/validation.py +148 -0
- agent_usage_tracker/inspector.py +350 -0
- agent_usage_tracker/label/__init__.py +22 -0
- agent_usage_tracker/label/backends.py +742 -0
- agent_usage_tracker/label/bakeoff.py +661 -0
- agent_usage_tracker/label/baseline.py +173 -0
- agent_usage_tracker/label/clef.py +258 -0
- agent_usage_tracker/label/context.py +382 -0
- agent_usage_tracker/label/core.py +355 -0
- agent_usage_tracker/label/covered_run.py +92 -0
- agent_usage_tracker/label/evalset.py +154 -0
- agent_usage_tracker/label/gold.py +209 -0
- agent_usage_tracker/label/local.py +326 -0
- agent_usage_tracker/label/local_parallel.py +232 -0
- agent_usage_tracker/label/merge.py +57 -0
- agent_usage_tracker/label/outcome_coverage.py +57 -0
- agent_usage_tracker/label/replay.py +194 -0
- agent_usage_tracker/label/run.py +339 -0
- agent_usage_tracker/label/tasks.py +579 -0
- agent_usage_tracker/label/teach.py +145 -0
- agent_usage_tracker/local_page.py +128 -0
- agent_usage_tracker/manual_time.py +69 -0
- agent_usage_tracker/net.py +23 -0
- agent_usage_tracker/observation_cli.py +65 -0
- agent_usage_tracker/observation_report.py +116 -0
- agent_usage_tracker/observation_server.py +110 -0
- agent_usage_tracker/observations.py +365 -0
- agent_usage_tracker/outcome_git.py +383 -0
- agent_usage_tracker/outcomes.py +464 -0
- agent_usage_tracker/pricing.py +248 -0
- agent_usage_tracker/push.py +530 -0
- agent_usage_tracker/redact.py +45 -0
- agent_usage_tracker/repo_keys.py +165 -0
- agent_usage_tracker/review.py +252 -0
- agent_usage_tracker/scan.py +472 -0
- agent_usage_tracker/scheduled.py +164 -0
- agent_usage_tracker/sdk.py +79 -0
- agent_usage_tracker/sink.py +82 -0
- agent_usage_tracker/sink_stack/README.md +71 -0
- agent_usage_tracker/sink_stack/apply.sh +63 -0
- agent_usage_tracker/sink_stack/compose.yaml +94 -0
- agent_usage_tracker/sink_stack/env.example +20 -0
- agent_usage_tracker/sink_stack/grafana/dashboards/agents-effort.json +634 -0
- agent_usage_tracker/sink_stack/grafana/dashboards/agents-usage.json +1279 -0
- agent_usage_tracker/sink_stack/grafana/provisioning/dashboards/aut.yaml +9 -0
- agent_usage_tracker/sink_stack/grafana/provisioning/datasources/agents.yaml +16 -0
- agent_usage_tracker/sink_stack/sql/01_schema.sql +1186 -0
- agent_usage_tracker/sink_stack/sql/02_price.sql +60 -0
- agent_usage_tracker/sink_stack/sql/03_effort.sql +1180 -0
- agent_usage_tracker/sink_stack/sql/04_grants.sql +149 -0
- agent_usage_tracker/sink_stack/sql/refresh.sql +66 -0
- agent_usage_tracker/sources.py +180 -0
- agent_usage_tracker/state.py +191 -0
- agent_usage_tracker/telemetry.py +165 -0
- agent_usage_tracker-0.1.0.dist-info/METADATA +366 -0
- agent_usage_tracker-0.1.0.dist-info/RECORD +113 -0
- agent_usage_tracker-0.1.0.dist-info/WHEEL +4 -0
- agent_usage_tracker-0.1.0.dist-info/entry_points.txt +3 -0
- agent_usage_tracker-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,1180 @@
|
|
|
1
|
+
-- Phase 5: private effort, outcome, cost and audit views over the agents database.
|
|
2
|
+
--
|
|
3
|
+
-- Applied after 01_schema.sql and 02_price.sql (the cost views read
|
|
4
|
+
-- agents.model_price), before 04_grants.sql. Idempotent: CREATE OR REPLACE, IF NOT EXISTS.
|
|
5
|
+
--
|
|
6
|
+
-- Everything here is private: read by agents_ro (Grafana) only. Hours are private
|
|
7
|
+
-- numbers. No view carries a title, prompt text, branch name or machine: rows are counts, shares,
|
|
8
|
+
-- ids, enums and numbers. Days are local days in the sink time zone (AUT_TZ), weeks ISO weeks (Monday), months
|
|
9
|
+
-- calendar months. Views that read Phase 4 labels return zero rows (or NULL shares) until aut
|
|
10
|
+
-- writes a label row.
|
|
11
|
+
--
|
|
12
|
+
-- Slow views (they re-merge active time or read every usage row) have a *_live twin and a
|
|
13
|
+
-- materialized copy refreshed CONCURRENTLY by refresh.sql, after the
|
|
14
|
+
-- episode rebuild. Grafana reads the materialized copies.
|
|
15
|
+
--
|
|
16
|
+
-- Activity rows that are counted are read once per event_id (DISTINCT ON (event_id), earliest
|
|
17
|
+
-- ts): a Claude Code re-log stores a steer or turn again under another ts, and the table key is
|
|
18
|
+
-- (event_id, ts). event_id hashes the kind, so filtering by kind before the dedup is safe.
|
|
19
|
+
|
|
20
|
+
-- Run origin per session (event_session holds one row per usage event). scripted = any subagent
|
|
21
|
+
-- or automated row (the linker's rule), interactive = interactive rows and none scripted.
|
|
22
|
+
CREATE OR REPLACE VIEW agents.session_origin AS
|
|
23
|
+
SELECT session_id,
|
|
24
|
+
bool_or(run_origin IN ('subagent', 'automated')) AS scripted,
|
|
25
|
+
bool_or(run_origin = 'automated') AS automated,
|
|
26
|
+
bool_or(run_origin = 'interactive')
|
|
27
|
+
AND NOT bool_or(run_origin IN ('subagent', 'automated')) AS interactive
|
|
28
|
+
FROM agents.event_session
|
|
29
|
+
GROUP BY session_id;
|
|
30
|
+
|
|
31
|
+
-- Latest git state per commit, keyed by the first 7 hex digits (the linker's rule).
|
|
32
|
+
CREATE OR REPLACE VIEW agents.commit_state AS
|
|
33
|
+
SELECT DISTINCT ON (left(lower(sha), 7)) left(lower(sha), 7) AS k, state, survival
|
|
34
|
+
FROM agents.commit_outcome
|
|
35
|
+
ORDER BY left(lower(sha), 7), decided_at DESC;
|
|
36
|
+
|
|
37
|
+
-- The episodes effort counts: agents.episode minus (a) episodes with a reject correction on one
|
|
38
|
+
-- of their units (the linker drops those on its next run; this covers a reject made since) and
|
|
39
|
+
-- (b) episodes whose confident work_category labels (unit episode_day, not below_tau) are all
|
|
40
|
+
-- non-building: learning, collaboration or other (admin, a quick question, personal). An
|
|
41
|
+
-- episode with no confident label counts, so before labels exist every episode counts.
|
|
42
|
+
CREATE OR REPLACE VIEW agents.episode_counted AS
|
|
43
|
+
WITH lab AS (
|
|
44
|
+
SELECT split_part(unit_id, ':', 1) AS episode_id,
|
|
45
|
+
bool_or(label NOT IN ('learning', 'collaboration', 'other')) AS building
|
|
46
|
+
FROM agents.label_latest
|
|
47
|
+
WHERE task = 'work_category' AND unit = 'episode_day' AND NOT below_tau
|
|
48
|
+
GROUP BY 1
|
|
49
|
+
), rej AS (
|
|
50
|
+
SELECT DISTINCT m.episode_id
|
|
51
|
+
FROM agents.episode_correction c
|
|
52
|
+
JOIN agents.episode_member m ON m.session_id = c.a_session
|
|
53
|
+
WHERE c.action = 'reject'
|
|
54
|
+
AND (NOT c.attrs ? 'repo_keys'
|
|
55
|
+
OR EXISTS (SELECT 1 FROM unnest(m.repo_keys) k WHERE c.attrs->'repo_keys' ? k))
|
|
56
|
+
)
|
|
57
|
+
SELECT e.episode_id, e.first_ts, e.last_ts, e.n_sessions, e.n_seed_sessions, e.n_steers,
|
|
58
|
+
e.n_commits, e.n_prs, e.outcome
|
|
59
|
+
FROM agents.episode e
|
|
60
|
+
LEFT JOIN lab l ON l.episode_id = e.episode_id::text
|
|
61
|
+
WHERE coalesce(l.building, true)
|
|
62
|
+
AND e.episode_id NOT IN (SELECT episode_id FROM rej);
|
|
63
|
+
|
|
64
|
+
-- Local days a counted episode had a member row on (the linker's row kinds, inside the
|
|
65
|
+
-- episode's span, so a session with units in two episodes adds each its own days).
|
|
66
|
+
CREATE OR REPLACE VIEW agents.episode_activity_day AS
|
|
67
|
+
SELECT DISTINCT c.episode_id, (a.ts AT TIME ZONE '@AUT_TZ@')::date AS day
|
|
68
|
+
FROM agents.episode_counted c
|
|
69
|
+
JOIN agents.episode_member m USING (episode_id)
|
|
70
|
+
JOIN agents.activity_event a ON a.session_id = m.session_id
|
|
71
|
+
WHERE a.kind IN ('steer', 'link', 'commit', 'pr')
|
|
72
|
+
AND a.ts BETWEEN c.first_ts AND c.last_ts;
|
|
73
|
+
|
|
74
|
+
-- Interactive sessions steered in each 15-minute window (windows start on the quarter hour).
|
|
75
|
+
CREATE OR REPLACE VIEW agents.steer_window AS
|
|
76
|
+
SELECT date_bin('15 minutes', a.ts, '2000-01-01 00:00+00') AS window_start,
|
|
77
|
+
count(DISTINCT a.session_id)::int AS sessions
|
|
78
|
+
FROM (SELECT DISTINCT ON (event_id) ts, session_id FROM agents.activity_event
|
|
79
|
+
WHERE kind = 'steer' ORDER BY event_id, ts) a
|
|
80
|
+
JOIN agents.session_origin o USING (session_id)
|
|
81
|
+
WHERE o.interactive
|
|
82
|
+
GROUP BY 1;
|
|
83
|
+
|
|
84
|
+
-- A stretch of one interactive session being steered: steers at most 15 minutes apart, at least
|
|
85
|
+
-- two of them, at least a minute from first to last. A one-steer or seconds-long session is no
|
|
86
|
+
-- stretch, so a burst of them never counts as sessions at once.
|
|
87
|
+
CREATE OR REPLACE VIEW agents.steer_span AS
|
|
88
|
+
SELECT session_id, min(ts) AS start_ts, max(ts) AS end_ts
|
|
89
|
+
FROM (SELECT session_id, ts,
|
|
90
|
+
count(*) FILTER (WHERE prev IS NULL OR ts - prev > interval '15 minutes')
|
|
91
|
+
OVER (PARTITION BY session_id ORDER BY ts) AS stretch
|
|
92
|
+
FROM (SELECT a.session_id, a.ts, lag(a.ts) OVER (PARTITION BY a.session_id ORDER BY a.ts) AS prev
|
|
93
|
+
FROM (SELECT DISTINCT ON (event_id) ts, session_id FROM agents.activity_event
|
|
94
|
+
WHERE kind = 'steer' ORDER BY event_id, ts) a
|
|
95
|
+
JOIN agents.session_origin o USING (session_id)
|
|
96
|
+
WHERE o.interactive) g) n
|
|
97
|
+
GROUP BY session_id, stretch
|
|
98
|
+
HAVING count(*) >= 2 AND max(ts) - min(ts) >= interval '1 minute';
|
|
99
|
+
|
|
100
|
+
-- Stretches running at one moment, per local day: a line sweep, so two sessions steered one after
|
|
101
|
+
-- the other inside one 15-minute window are not at once. A day with any interactive steer is at
|
|
102
|
+
-- least 1. shortcut: read at the stretches' starts and ends, so a peak still running across
|
|
103
|
+
-- midnight shows on the day it was reached; read per moment if a day's peak ever matters that much.
|
|
104
|
+
CREATE OR REPLACE VIEW agents.steer_peak_day AS
|
|
105
|
+
SELECT (ts AT TIME ZONE '@AUT_TZ@')::date AS day, at_once
|
|
106
|
+
FROM (SELECT ts, sum(delta) OVER (ORDER BY ts, delta DESC ROWS UNBOUNDED PRECEDING)::int AS at_once
|
|
107
|
+
FROM (SELECT start_ts AS ts, 1 AS delta FROM agents.steer_span
|
|
108
|
+
UNION ALL
|
|
109
|
+
SELECT end_ts, -1 FROM agents.steer_span) e) sweep
|
|
110
|
+
UNION ALL
|
|
111
|
+
SELECT (window_start AT TIME ZONE '@AUT_TZ@')::date, 1 FROM agents.steer_window;
|
|
112
|
+
|
|
113
|
+
-- Effort per day, ISO week and month (grain 'day' | 'week' | 'month', period = its first day).
|
|
114
|
+
-- steers human steers outside scripted sessions; steers_by_agent splits them
|
|
115
|
+
-- interactive_sessions sessions with a row in the period whose run_origin is interactive
|
|
116
|
+
-- sessions_all every session with a row in the period, scripted ones included
|
|
117
|
+
-- projects distinct repo_key on link and commit rows
|
|
118
|
+
-- commits, prs distinct commits (7-hex prefix, superseded pre-amend SHAs left out)
|
|
119
|
+
-- and PRs (host, repo_key, number)
|
|
120
|
+
-- merged_commits of those commits, the ones whose latest git state is merged
|
|
121
|
+
-- episodes_active counted episodes with a member row in the period; episodes_started
|
|
122
|
+
-- by their first row
|
|
123
|
+
-- focus_blocks merged 'relevant' active intervals of 60 minutes or more, more than
|
|
124
|
+
-- half covered by steer islands and editor/dev/terminal window time,
|
|
125
|
+
-- counted in the period they start
|
|
126
|
+
-- parallel_peak most interactive sessions being steered at one moment (steer_peak_day)
|
|
127
|
+
-- active_minutes_* agents.active_daily summed (private: never published)
|
|
128
|
+
-- Reads agents.active_daily (materialized), so refresh that first.
|
|
129
|
+
CREATE OR REPLACE VIEW agents.effort_period_live AS
|
|
130
|
+
WITH g(grain) AS (VALUES ('day'), ('week'), ('month')),
|
|
131
|
+
act AS (
|
|
132
|
+
SELECT (a.ts AT TIME ZONE '@AUT_TZ@')::date AS day, a.kind, a.agent, a.session_id,
|
|
133
|
+
CASE WHEN a.kind IN ('link', 'commit') THEN nullif(a.attrs->>'repo_key', '') END AS repo_key,
|
|
134
|
+
CASE WHEN a.kind = 'commit' AND lower(a.attrs->>'sha') ~ '^[0-9a-f]{7,40}$'
|
|
135
|
+
THEN left(lower(a.attrs->>'sha'), 7) END AS sha7,
|
|
136
|
+
CASE WHEN a.kind = 'pr'
|
|
137
|
+
THEN concat_ws(':', a.attrs->>'host', a.attrs->>'repo_key', a.attrs->>'number') END AS pr,
|
|
138
|
+
coalesce(o.scripted, false) AS scripted, coalesce(o.interactive, false) AS interactive
|
|
139
|
+
FROM (SELECT DISTINCT ON (event_id) ts, kind, agent, session_id, attrs FROM agents.activity_event
|
|
140
|
+
WHERE session_id IS NOT NULL ORDER BY event_id, ts) a
|
|
141
|
+
LEFT JOIN agents.session_origin o USING (session_id)
|
|
142
|
+
), base AS (
|
|
143
|
+
SELECT g.grain, date_trunc(g.grain, x.day::timestamp)::date AS period,
|
|
144
|
+
count(*) FILTER (WHERE x.kind = 'steer' AND NOT x.scripted)::int AS steers,
|
|
145
|
+
count(DISTINCT x.session_id) FILTER (WHERE x.interactive)::int AS interactive_sessions,
|
|
146
|
+
count(DISTINCT x.session_id)::int AS sessions_all,
|
|
147
|
+
count(DISTINCT x.repo_key)::int AS projects,
|
|
148
|
+
count(DISTINCT x.sha7) FILTER (WHERE cs.state IS DISTINCT FROM 'superseded')::int AS commits,
|
|
149
|
+
count(DISTINCT x.pr)::int AS prs,
|
|
150
|
+
count(DISTINCT x.sha7) FILTER (WHERE cs.state = 'merged')::int AS merged_commits
|
|
151
|
+
FROM act x CROSS JOIN g
|
|
152
|
+
LEFT JOIN agents.commit_state cs ON cs.k = x.sha7
|
|
153
|
+
GROUP BY 1, 2
|
|
154
|
+
), by_agent AS (
|
|
155
|
+
SELECT grain, period, jsonb_object_agg(agent, n) AS steers_by_agent
|
|
156
|
+
FROM (SELECT g.grain, date_trunc(g.grain, x.day::timestamp)::date AS period, x.agent, count(*) AS n
|
|
157
|
+
FROM act x CROSS JOIN g
|
|
158
|
+
WHERE x.kind = 'steer' AND NOT x.scripted
|
|
159
|
+
GROUP BY 1, 2, 3) s
|
|
160
|
+
GROUP BY 1, 2
|
|
161
|
+
), ep AS (
|
|
162
|
+
SELECT g.grain, date_trunc(g.grain, d.day::timestamp)::date AS period,
|
|
163
|
+
count(DISTINCT d.episode_id)::int AS episodes_active
|
|
164
|
+
FROM agents.episode_activity_day d CROSS JOIN g
|
|
165
|
+
GROUP BY 1, 2
|
|
166
|
+
), started AS (
|
|
167
|
+
SELECT g.grain, date_trunc(g.grain, (c.first_ts AT TIME ZONE '@AUT_TZ@')::date::timestamp)::date AS period,
|
|
168
|
+
count(*)::int AS episodes_started
|
|
169
|
+
FROM agents.episode_counted c CROSS JOIN g
|
|
170
|
+
GROUP BY 1, 2
|
|
171
|
+
), fsrc AS (
|
|
172
|
+
SELECT start_ts, end_ts FROM agents.steer_interval
|
|
173
|
+
UNION ALL
|
|
174
|
+
SELECT ts, end_ts FROM agents.time_interval_effective WHERE category IN ('editor', 'dev', 'terminal')
|
|
175
|
+
), fprev AS (
|
|
176
|
+
SELECT start_ts, end_ts,
|
|
177
|
+
max(end_ts) OVER (ORDER BY start_ts, end_ts ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS prev_end
|
|
178
|
+
FROM fsrc
|
|
179
|
+
), focus AS MATERIALIZED (
|
|
180
|
+
-- focus time merged, so the overlaps summed below never count one minute twice
|
|
181
|
+
SELECT min(start_ts) AS s, max(end_ts) AS e
|
|
182
|
+
FROM (SELECT start_ts, end_ts,
|
|
183
|
+
count(*) FILTER (WHERE prev_end IS NULL OR start_ts > prev_end)
|
|
184
|
+
OVER (ORDER BY start_ts, end_ts ROWS UNBOUNDED PRECEDING) AS grp
|
|
185
|
+
FROM fprev) f
|
|
186
|
+
GROUP BY grp
|
|
187
|
+
), blocks AS (
|
|
188
|
+
SELECT (r.start_ts AT TIME ZONE '@AUT_TZ@')::date AS day
|
|
189
|
+
FROM agents.active_interval r
|
|
190
|
+
WHERE r.variant = 'relevant' AND r.end_ts - r.start_ts >= interval '60 minutes'
|
|
191
|
+
AND 2 * (SELECT coalesce(sum(extract(epoch FROM least(f.e, r.end_ts) - greatest(f.s, r.start_ts))), 0)
|
|
192
|
+
FROM focus f WHERE f.s < r.end_ts AND f.e > r.start_ts)
|
|
193
|
+
> extract(epoch FROM r.end_ts - r.start_ts)
|
|
194
|
+
), fb AS (
|
|
195
|
+
SELECT g.grain, date_trunc(g.grain, b.day::timestamp)::date AS period, count(*)::int AS focus_blocks
|
|
196
|
+
FROM blocks b CROSS JOIN g
|
|
197
|
+
GROUP BY 1, 2
|
|
198
|
+
), par AS (
|
|
199
|
+
SELECT g.grain, date_trunc(g.grain, d.day::timestamp)::date AS period, max(d.at_once)::int AS parallel_peak
|
|
200
|
+
FROM agents.steer_peak_day d CROSS JOIN g
|
|
201
|
+
GROUP BY 1, 2
|
|
202
|
+
), mins AS (
|
|
203
|
+
SELECT g.grain, date_trunc(g.grain, d.day::timestamp)::date AS period,
|
|
204
|
+
sum(d.minutes_all) AS active_minutes_all, sum(d.minutes_relevant) AS active_minutes_relevant
|
|
205
|
+
FROM agents.active_daily d CROSS JOIN g
|
|
206
|
+
GROUP BY 1, 2
|
|
207
|
+
), spine AS (
|
|
208
|
+
SELECT grain, period FROM base UNION SELECT grain, period FROM ep
|
|
209
|
+
UNION SELECT grain, period FROM started UNION SELECT grain, period FROM fb
|
|
210
|
+
UNION SELECT grain, period FROM mins
|
|
211
|
+
)
|
|
212
|
+
SELECT s.grain, s.period,
|
|
213
|
+
coalesce(b.steers, 0) AS steers, coalesce(a.steers_by_agent, '{}'::jsonb) AS steers_by_agent,
|
|
214
|
+
coalesce(b.interactive_sessions, 0) AS interactive_sessions,
|
|
215
|
+
coalesce(b.sessions_all, 0) AS sessions_all, coalesce(b.projects, 0) AS projects,
|
|
216
|
+
coalesce(b.commits, 0) AS commits, coalesce(b.prs, 0) AS prs,
|
|
217
|
+
coalesce(b.merged_commits, 0) AS merged_commits,
|
|
218
|
+
coalesce(e.episodes_active, 0) AS episodes_active,
|
|
219
|
+
coalesce(st.episodes_started, 0) AS episodes_started,
|
|
220
|
+
coalesce(f.focus_blocks, 0) AS focus_blocks, coalesce(p.parallel_peak, 0) AS parallel_peak,
|
|
221
|
+
coalesce(m.active_minutes_all, 0) AS active_minutes_all,
|
|
222
|
+
coalesce(m.active_minutes_relevant, 0) AS active_minutes_relevant
|
|
223
|
+
FROM spine s
|
|
224
|
+
LEFT JOIN base b USING (grain, period)
|
|
225
|
+
LEFT JOIN by_agent a USING (grain, period)
|
|
226
|
+
LEFT JOIN ep e USING (grain, period)
|
|
227
|
+
LEFT JOIN started st USING (grain, period)
|
|
228
|
+
LEFT JOIN fb f USING (grain, period)
|
|
229
|
+
LEFT JOIN par p USING (grain, period)
|
|
230
|
+
LEFT JOIN mins m USING (grain, period);
|
|
231
|
+
|
|
232
|
+
CREATE MATERIALIZED VIEW IF NOT EXISTS agents.effort_period AS
|
|
233
|
+
SELECT * FROM agents.effort_period_live
|
|
234
|
+
WITH DATA;
|
|
235
|
+
CREATE UNIQUE INDEX IF NOT EXISTS effort_period_key ON agents.effort_period (grain, period);
|
|
236
|
+
|
|
237
|
+
CREATE OR REPLACE VIEW agents.effort_daily AS
|
|
238
|
+
SELECT period AS day, steers, steers_by_agent, interactive_sessions, sessions_all, projects,
|
|
239
|
+
commits, prs, merged_commits, episodes_active, episodes_started, focus_blocks,
|
|
240
|
+
parallel_peak, active_minutes_all, active_minutes_relevant
|
|
241
|
+
FROM agents.effort_period WHERE grain = 'day';
|
|
242
|
+
|
|
243
|
+
CREATE OR REPLACE VIEW agents.effort_weekly AS
|
|
244
|
+
SELECT period AS week, steers, steers_by_agent, interactive_sessions, sessions_all, projects,
|
|
245
|
+
commits, prs, merged_commits, episodes_active, episodes_started, focus_blocks,
|
|
246
|
+
parallel_peak, active_minutes_all, active_minutes_relevant
|
|
247
|
+
FROM agents.effort_period WHERE grain = 'week';
|
|
248
|
+
|
|
249
|
+
CREATE OR REPLACE VIEW agents.effort_monthly AS
|
|
250
|
+
SELECT period AS month, steers, steers_by_agent, interactive_sessions, sessions_all, projects,
|
|
251
|
+
commits, prs, merged_commits, episodes_active, episodes_started, focus_blocks,
|
|
252
|
+
parallel_peak, active_minutes_all, active_minutes_relevant
|
|
253
|
+
FROM agents.effort_period WHERE grain = 'month';
|
|
254
|
+
|
|
255
|
+
-- ---------------------------------------------------------------------------------------------
|
|
256
|
+
-- Modules: counts and shares only.
|
|
257
|
+
-- ---------------------------------------------------------------------------------------------
|
|
258
|
+
|
|
259
|
+
-- Label shares per task and local month, by unit count. share is among confident
|
|
260
|
+
-- units; below_tau units are unknown: one row with label NULL, and unknown_share on every row of
|
|
261
|
+
-- the month. A month with only unknown units shows just that row.
|
|
262
|
+
-- shortcut: every unit weighs the same; label rows carry no duration, so work_category shares
|
|
263
|
+
-- are of labelled units, not of active minutes. Weight by minutes once aut writes them.
|
|
264
|
+
CREATE OR REPLACE VIEW agents.label_share_monthly AS
|
|
265
|
+
SELECT task, month, label, units,
|
|
266
|
+
CASE WHEN label IS NOT NULL
|
|
267
|
+
THEN round(units::numeric / nullif(sum(units) FILTER (WHERE label IS NOT NULL) OVER w, 0), 4)
|
|
268
|
+
END AS share,
|
|
269
|
+
round(coalesce(sum(units) FILTER (WHERE label IS NULL) OVER w, 0)::numeric / sum(units) OVER w, 4)
|
|
270
|
+
AS unknown_share
|
|
271
|
+
FROM (SELECT task, date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
272
|
+
CASE WHEN NOT below_tau THEN label END AS label, count(*)::int AS units
|
|
273
|
+
FROM agents.label_latest
|
|
274
|
+
GROUP BY 1, 2, 3) x
|
|
275
|
+
WINDOW w AS (PARTITION BY task, month);
|
|
276
|
+
|
|
277
|
+
-- unit_share, not share: a unit is an episode day or a window block, each counted once (the
|
|
278
|
+
-- shortcut above). One-time migration: CREATE OR REPLACE cannot rename a column; nothing
|
|
279
|
+
-- depends on the view and 04_grants.sql (run after this file) re-grants it.
|
|
280
|
+
DO $$
|
|
281
|
+
BEGIN
|
|
282
|
+
IF EXISTS (SELECT 1 FROM information_schema.columns
|
|
283
|
+
WHERE table_schema = 'agents' AND table_name = 'work_mix_monthly'
|
|
284
|
+
AND column_name = 'share') THEN
|
|
285
|
+
EXECUTE 'DROP VIEW agents.work_mix_monthly';
|
|
286
|
+
END IF;
|
|
287
|
+
END $$;
|
|
288
|
+
CREATE OR REPLACE VIEW agents.work_mix_monthly AS
|
|
289
|
+
SELECT month, label AS category, units, share AS unit_share, unknown_share
|
|
290
|
+
FROM agents.label_share_monthly WHERE task = 'work_category';
|
|
291
|
+
|
|
292
|
+
CREATE OR REPLACE VIEW agents.steer_intent_monthly AS
|
|
293
|
+
SELECT month, label AS intent, units, share, unknown_share
|
|
294
|
+
FROM agents.label_share_monthly WHERE task = 'steer_intent';
|
|
295
|
+
|
|
296
|
+
-- Outcomes of counted episodes by the month they started. final = the month is complete the way
|
|
297
|
+
-- agents.episode_monthly publishes it (14 days after the month, every episode quiet for 14 days,
|
|
298
|
+
-- none undecided); before that the counts can still move. The two sets differ on purpose:
|
|
299
|
+
-- episode_monthly (phase 3) counts every linked episode, this view only the counted ones (no
|
|
300
|
+
-- reject, building work). Phase 6 publishes the counted set, so it reads this view's final
|
|
301
|
+
-- months, not episode_monthly. The self-check pins the difference.
|
|
302
|
+
CREATE OR REPLACE VIEW agents.outcomes_monthly AS
|
|
303
|
+
SELECT month,
|
|
304
|
+
count(*)::int AS episodes,
|
|
305
|
+
count(*) FILTER (WHERE outcome = 'shipped')::int AS shipped,
|
|
306
|
+
count(*) FILTER (WHERE outcome = 'reworked')::int AS reworked,
|
|
307
|
+
count(*) FILTER (WHERE outcome = 'open')::int AS open,
|
|
308
|
+
count(*) FILTER (WHERE outcome = 'ruled_out')::int AS ruled_out,
|
|
309
|
+
count(*) FILTER (WHERE outcome = 'no_code')::int AS no_code,
|
|
310
|
+
count(*) FILTER (WHERE outcome = 'undecided')::int AS undecided,
|
|
311
|
+
round(count(*) FILTER (WHERE outcome = 'shipped')::numeric / count(*), 4) AS shipped_share,
|
|
312
|
+
round(count(*) FILTER (WHERE outcome = 'reworked')::numeric
|
|
313
|
+
/ nullif(count(*) FILTER (WHERE outcome IN ('shipped', 'reworked')), 0), 4) AS rework_share,
|
|
314
|
+
(month + interval '1 month' + interval '14 days') AT TIME ZONE '@AUT_TZ@' <= now()
|
|
315
|
+
AND max(last_ts) + interval '14 days' <= now()
|
|
316
|
+
AND count(*) FILTER (WHERE outcome = 'undecided') = 0 AS final
|
|
317
|
+
FROM (SELECT date_trunc('month', first_ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
318
|
+
last_ts, outcome
|
|
319
|
+
FROM agents.episode_counted) e
|
|
320
|
+
GROUP BY month;
|
|
321
|
+
|
|
322
|
+
-- Counted episodes active per ISO week, and returns: an active day after two or more whole days
|
|
323
|
+
-- with no activity on that episode.
|
|
324
|
+
CREATE OR REPLACE VIEW agents.episodes_revisits_weekly AS
|
|
325
|
+
SELECT date_trunc('week', day::timestamp)::date AS week,
|
|
326
|
+
count(DISTINCT episode_id)::int AS episodes_active,
|
|
327
|
+
count(*) FILTER (WHERE gap IS NULL)::int AS episodes_started,
|
|
328
|
+
count(*) FILTER (WHERE gap > 2)::int AS returns,
|
|
329
|
+
count(DISTINCT episode_id) FILTER (WHERE gap > 2)::int AS episodes_returning
|
|
330
|
+
FROM (SELECT episode_id, day, day - lag(day) OVER (PARTITION BY episode_id ORDER BY day) AS gap
|
|
331
|
+
FROM agents.episode_activity_day) d
|
|
332
|
+
GROUP BY 1;
|
|
333
|
+
|
|
334
|
+
-- Parallel steering per ISO week. parallel_peak: most sessions being steered at one moment
|
|
335
|
+
-- (steer_peak_day). The window columns: 15-minute windows with an interactive steer, of which those
|
|
336
|
+
-- with two or more sessions; they count sessions that merely share a window, so they stay private.
|
|
337
|
+
CREATE OR REPLACE VIEW agents.parallel_steering_weekly AS
|
|
338
|
+
SELECT date_trunc('week', w.window_start AT TIME ZONE '@AUT_TZ@')::date AS week,
|
|
339
|
+
max(p.peak)::int AS parallel_peak,
|
|
340
|
+
count(*)::int AS steered_windows,
|
|
341
|
+
count(*) FILTER (WHERE w.sessions >= 2)::int AS parallel_windows,
|
|
342
|
+
round(count(*) FILTER (WHERE w.sessions >= 2)::numeric / count(*), 4) AS parallel_share
|
|
343
|
+
FROM agents.steer_window w
|
|
344
|
+
JOIN (SELECT date_trunc('week', day::timestamp)::date AS week, max(at_once) AS peak
|
|
345
|
+
FROM agents.steer_peak_day GROUP BY 1) p
|
|
346
|
+
ON p.week = date_trunc('week', w.window_start AT TIME ZONE '@AUT_TZ@')::date
|
|
347
|
+
GROUP BY 1;
|
|
348
|
+
|
|
349
|
+
-- Commits by the month their first commit row landed, by latest git state. aut decides reverted
|
|
350
|
+
-- (a revert within 14 days of the merge) and reworked (under half the lines survive 14 days).
|
|
351
|
+
-- Superseded pre-amend SHAs are left out; a commit without an outcome row is undecided.
|
|
352
|
+
CREATE OR REPLACE VIEW agents.shipping_rework_monthly AS
|
|
353
|
+
SELECT c.month,
|
|
354
|
+
count(*)::int AS commits,
|
|
355
|
+
count(*) FILTER (WHERE s.state = 'merged')::int AS merged,
|
|
356
|
+
count(*) FILTER (WHERE s.state = 'reverted')::int AS reverted,
|
|
357
|
+
count(*) FILTER (WHERE s.state = 'reworked')::int AS reworked,
|
|
358
|
+
count(*) FILTER (WHERE s.state = 'open')::int AS open,
|
|
359
|
+
count(*) FILTER (WHERE s.state = 'abandoned')::int AS abandoned,
|
|
360
|
+
count(*) FILTER (WHERE coalesce(s.state, 'undecided') = 'undecided')::int AS undecided,
|
|
361
|
+
round(count(*) FILTER (WHERE s.state IN ('reverted', 'reworked'))::numeric
|
|
362
|
+
/ nullif(count(*) FILTER (WHERE s.state IN ('merged', 'reverted', 'reworked')), 0), 4)
|
|
363
|
+
AS rework_share
|
|
364
|
+
FROM (SELECT DISTINCT ON (left(lower(attrs->>'sha'), 7)) left(lower(attrs->>'sha'), 7) AS k,
|
|
365
|
+
date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date AS month
|
|
366
|
+
FROM agents.activity_event
|
|
367
|
+
WHERE kind = 'commit' AND lower(attrs->>'sha') ~ '^[0-9a-f]{7,40}$'
|
|
368
|
+
ORDER BY 1, ts) c
|
|
369
|
+
LEFT JOIN agents.commit_state s USING (k)
|
|
370
|
+
WHERE s.state IS DISTINCT FROM 'superseded'
|
|
371
|
+
GROUP BY c.month;
|
|
372
|
+
|
|
373
|
+
-- Skills built and reused: invoke rows by month. skills_new = skills invoked for the first time
|
|
374
|
+
-- ever; later_invocations = every invoke after a skill's first; skills_reused = distinct skills
|
|
375
|
+
-- invoked this month that first appeared in an earlier month. 'mcp' (every MCP tool collapsed)
|
|
376
|
+
-- is no skill and is left out.
|
|
377
|
+
CREATE OR REPLACE VIEW agents.built_reused_monthly AS
|
|
378
|
+
SELECT month,
|
|
379
|
+
count(*) FILTER (WHERE nth = 1)::int AS skills_new,
|
|
380
|
+
count(*)::int AS invocations,
|
|
381
|
+
count(*) FILTER (WHERE nth > 1)::int AS later_invocations,
|
|
382
|
+
count(DISTINCT skill) FILTER (WHERE first_month < month)::int AS skills_reused
|
|
383
|
+
FROM (SELECT attrs->>'skill' AS skill,
|
|
384
|
+
date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
385
|
+
row_number() OVER (PARTITION BY attrs->>'skill' ORDER BY ts, event_id) AS nth,
|
|
386
|
+
min(date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date)
|
|
387
|
+
OVER (PARTITION BY attrs->>'skill') AS first_month
|
|
388
|
+
FROM (SELECT DISTINCT ON (event_id) event_id, ts, attrs FROM agents.activity_event
|
|
389
|
+
WHERE kind = 'invoke' ORDER BY event_id, ts) a
|
|
390
|
+
WHERE coalesce(attrs->>'skill', '') NOT IN ('', 'mcp')) i
|
|
391
|
+
GROUP BY month;
|
|
392
|
+
|
|
393
|
+
-- Learning followed by doing: confident 'learning' episode-day labels by month, and how many were
|
|
394
|
+
-- followed within 30 days by a confident building-work episode-day on one of the same repos
|
|
395
|
+
-- (building work = episode_counted's rule: any category but learning, collaboration, other).
|
|
396
|
+
-- shortcut: aw_block labels carry no repo, so learning time outside an episode is not counted.
|
|
397
|
+
CREATE OR REPLACE VIEW agents.learning_to_doing_monthly AS
|
|
398
|
+
WITH ed AS (
|
|
399
|
+
SELECT DISTINCT l.unit_id, l.label, l.ts, k AS repo_key,
|
|
400
|
+
l.label NOT IN ('learning', 'collaboration', 'other') AS building
|
|
401
|
+
FROM agents.label_latest l
|
|
402
|
+
JOIN agents.episode_member m ON m.episode_id::text = split_part(l.unit_id, ':', 1)
|
|
403
|
+
CROSS JOIN LATERAL unnest(m.repo_keys) k
|
|
404
|
+
WHERE l.task = 'work_category' AND l.unit = 'episode_day' AND NOT l.below_tau
|
|
405
|
+
AND k <> ''
|
|
406
|
+
), learn AS (
|
|
407
|
+
SELECT le.unit_id, min(le.ts) AS ts,
|
|
408
|
+
bool_or(EXISTS (SELECT 1 FROM ed b
|
|
409
|
+
WHERE b.building AND b.repo_key = le.repo_key
|
|
410
|
+
AND b.ts > le.ts AND b.ts <= le.ts + interval '30 days')) AS followed
|
|
411
|
+
FROM ed le WHERE le.label = 'learning'
|
|
412
|
+
GROUP BY le.unit_id
|
|
413
|
+
)
|
|
414
|
+
SELECT date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
415
|
+
count(*)::int AS learning_days,
|
|
416
|
+
count(*) FILTER (WHERE followed)::int AS followed_by_building
|
|
417
|
+
FROM learn
|
|
418
|
+
GROUP BY 1;
|
|
419
|
+
|
|
420
|
+
-- Follow-through: commits (outside scripted sessions, superseded SHAs left out), and those on a
|
|
421
|
+
-- repo where a counted episode shipped and the repo then went quiet for more than 30 days. Each
|
|
422
|
+
-- commit is read against the latest shipped episode before it: quiet means the repo's first
|
|
423
|
+
-- activity after that episode (link, commit or PR outside scripted sessions) came over 30 days
|
|
424
|
+
-- later. A repo that keeps moving after it ships never counts; a later shipped episode starts over.
|
|
425
|
+
CREATE OR REPLACE VIEW agents.follow_through_commit AS
|
|
426
|
+
WITH act AS MATERIALIZED (
|
|
427
|
+
SELECT a.attrs->>'repo_key' AS repo_key, a.ts
|
|
428
|
+
FROM agents.activity_event a
|
|
429
|
+
LEFT JOIN agents.session_origin o USING (session_id)
|
|
430
|
+
WHERE a.kind IN ('link', 'commit', 'pr') AND coalesce(a.attrs->>'repo_key', '') <> ''
|
|
431
|
+
AND NOT coalesce(o.scripted, false)
|
|
432
|
+
), shipped AS (
|
|
433
|
+
SELECT s.repo_key, s.last_ts,
|
|
434
|
+
coalesce((SELECT min(x.ts) FROM act x WHERE x.repo_key = s.repo_key AND x.ts > s.last_ts)
|
|
435
|
+
> s.last_ts + interval '30 days', false) AS quiet
|
|
436
|
+
FROM (SELECT DISTINCT k AS repo_key, e.last_ts
|
|
437
|
+
FROM agents.episode_counted e
|
|
438
|
+
JOIN agents.episode_member m USING (episode_id)
|
|
439
|
+
CROSS JOIN LATERAL unnest(m.repo_keys) k
|
|
440
|
+
WHERE e.outcome = 'shipped' AND k <> '') s
|
|
441
|
+
), c AS (
|
|
442
|
+
SELECT DISTINCT ON (left(lower(a.attrs->>'sha'), 7)) a.ts, nullif(a.attrs->>'repo_key', '') AS repo_key
|
|
443
|
+
FROM agents.activity_event a
|
|
444
|
+
LEFT JOIN agents.session_origin o USING (session_id)
|
|
445
|
+
LEFT JOIN agents.commit_state cs ON cs.k = left(lower(a.attrs->>'sha'), 7)
|
|
446
|
+
WHERE a.kind = 'commit' AND lower(a.attrs->>'sha') ~ '^[0-9a-f]{7,40}$'
|
|
447
|
+
AND NOT coalesce(o.scripted, false) AND cs.state IS DISTINCT FROM 'superseded'
|
|
448
|
+
ORDER BY left(lower(a.attrs->>'sha'), 7), a.ts
|
|
449
|
+
)
|
|
450
|
+
SELECT c.ts, c.repo_key, coalesce(l.quiet, false) AS follow_through
|
|
451
|
+
FROM c
|
|
452
|
+
LEFT JOIN LATERAL (SELECT s.quiet FROM shipped s
|
|
453
|
+
WHERE s.repo_key = c.repo_key AND s.last_ts < c.ts
|
|
454
|
+
ORDER BY s.last_ts DESC LIMIT 1) l ON true;
|
|
455
|
+
|
|
456
|
+
CREATE OR REPLACE VIEW agents.follow_through_monthly AS
|
|
457
|
+
SELECT date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
458
|
+
count(*)::int AS commits,
|
|
459
|
+
count(*) FILTER (WHERE follow_through)::int AS follow_through_commits,
|
|
460
|
+
count(DISTINCT repo_key) FILTER (WHERE follow_through)::int AS follow_through_repos
|
|
461
|
+
FROM agents.follow_through_commit
|
|
462
|
+
GROUP BY 1;
|
|
463
|
+
|
|
464
|
+
-- ---------------------------------------------------------------------------------------------
|
|
465
|
+
-- Tokens, cost and efficiency (private). Cost is the 02_price.sql rule: API-equivalent,
|
|
466
|
+
-- NOTIONAL, reasoning tokens never added (they are part of output). An unpriced model costs
|
|
467
|
+
-- NULL, never a guess; unpriced_turns says how much of a figure is missing.
|
|
468
|
+
-- ---------------------------------------------------------------------------------------------
|
|
469
|
+
|
|
470
|
+
CREATE OR REPLACE VIEW agents.usage_cost AS
|
|
471
|
+
SELECT u.event_id, u.ts, u.agent, coalesce(u.model, '') AS model,
|
|
472
|
+
u.input_tokens, u.output_tokens, u.cache_read_tokens, u.cache_write_tokens,
|
|
473
|
+
u.reasoning_tokens, u.total_tokens,
|
|
474
|
+
u.input_tokens * p.input_cost + u.output_tokens * p.output_cost
|
|
475
|
+
+ u.cache_read_tokens * coalesce(p.cache_read_cost, p.input_cost)
|
|
476
|
+
+ u.cache_write_tokens * coalesce(p.cache_write_cost, p.input_cost) AS est_cost_usd
|
|
477
|
+
FROM agents.usage_event_dedup u
|
|
478
|
+
LEFT JOIN agents.model_price p ON p.model_key = lower(u.model)
|
|
479
|
+
WHERE u.kind = 'agent';
|
|
480
|
+
|
|
481
|
+
CREATE OR REPLACE VIEW agents.tokens_cost_by_model_daily_live AS
|
|
482
|
+
SELECT (ts AT TIME ZONE '@AUT_TZ@')::date AS day, agent, model,
|
|
483
|
+
count(*)::int AS turns,
|
|
484
|
+
sum(input_tokens)::bigint AS input_tokens,
|
|
485
|
+
sum(output_tokens)::bigint AS output_tokens,
|
|
486
|
+
sum(cache_read_tokens)::bigint AS cache_read_tokens,
|
|
487
|
+
sum(cache_write_tokens)::bigint AS cache_write_tokens,
|
|
488
|
+
sum(reasoning_tokens)::bigint AS reasoning_tokens,
|
|
489
|
+
sum(total_tokens)::bigint AS total_tokens,
|
|
490
|
+
sum(est_cost_usd) AS est_cost_usd,
|
|
491
|
+
count(*) FILTER (WHERE est_cost_usd IS NULL)::int AS unpriced_turns
|
|
492
|
+
FROM agents.usage_cost
|
|
493
|
+
GROUP BY 1, 2, 3;
|
|
494
|
+
|
|
495
|
+
CREATE MATERIALIZED VIEW IF NOT EXISTS agents.tokens_cost_by_model_daily AS
|
|
496
|
+
SELECT * FROM agents.tokens_cost_by_model_daily_live
|
|
497
|
+
WITH DATA;
|
|
498
|
+
CREATE UNIQUE INDEX IF NOT EXISTS tokens_cost_by_model_daily_key
|
|
499
|
+
ON agents.tokens_cost_by_model_daily (day, agent, model);
|
|
500
|
+
|
|
501
|
+
-- Tokens and cost per counted episode: the usage rows of its member sessions inside its span,
|
|
502
|
+
-- each row given to exactly one episode. A session that worked in two repos has a unit in each,
|
|
503
|
+
-- and their spans can overlap; such a row goes to the episode whose repo rows in that session
|
|
504
|
+
-- (link, commit, pr) lie nearest in time, the linker's _el_pos rule. The pick runs over every
|
|
505
|
+
-- episode, so a row of a rejected or non-building episode is never moved onto a counted one.
|
|
506
|
+
-- Every counted episode has a row: turns 0 and NULL tokens when no usage row fell in its span.
|
|
507
|
+
CREATE OR REPLACE VIEW agents.tokens_cost_by_episode_live AS
|
|
508
|
+
WITH cand AS (
|
|
509
|
+
SELECT e.episode_id, m.session_id, m.repo_keys, u.event_id, u.ts, u.total_tokens, u.est_cost_usd,
|
|
510
|
+
count(*) OVER (PARTITION BY u.event_id) AS n
|
|
511
|
+
FROM agents.episode e
|
|
512
|
+
JOIN agents.episode_member m USING (episode_id)
|
|
513
|
+
JOIN agents.event_session s ON s.session_id = m.session_id
|
|
514
|
+
JOIN agents.usage_cost u ON u.event_id = s.event_id AND u.ts BETWEEN e.first_ts AND e.last_ts
|
|
515
|
+
), pos AS MATERIALIZED (
|
|
516
|
+
SELECT session_id, ts, attrs->>'repo_key' AS repo_key
|
|
517
|
+
FROM agents.activity_event
|
|
518
|
+
WHERE kind IN ('link', 'commit', 'pr') AND coalesce(attrs->>'repo_key', '') <> ''
|
|
519
|
+
AND session_id IN (SELECT session_id FROM cand WHERE n > 1)
|
|
520
|
+
), one AS (
|
|
521
|
+
SELECT DISTINCT ON (c.event_id) c.episode_id, c.event_id, c.total_tokens, c.est_cost_usd
|
|
522
|
+
FROM cand c
|
|
523
|
+
ORDER BY c.event_id,
|
|
524
|
+
CASE WHEN c.n > 1 THEN (SELECT min(abs(extract(epoch FROM p.ts - c.ts))) FROM pos p
|
|
525
|
+
WHERE p.session_id = c.session_id AND p.repo_key = ANY (c.repo_keys))
|
|
526
|
+
END NULLS LAST,
|
|
527
|
+
c.episode_id
|
|
528
|
+
)
|
|
529
|
+
SELECT e.episode_id, date_trunc('month', e.first_ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
530
|
+
e.outcome,
|
|
531
|
+
count(o.event_id)::int AS turns,
|
|
532
|
+
sum(o.total_tokens)::bigint AS total_tokens,
|
|
533
|
+
sum(o.est_cost_usd) AS est_cost_usd,
|
|
534
|
+
count(o.event_id) FILTER (WHERE o.est_cost_usd IS NULL)::int AS unpriced_turns
|
|
535
|
+
FROM agents.episode_counted e
|
|
536
|
+
LEFT JOIN one o USING (episode_id)
|
|
537
|
+
GROUP BY 1, 2, 3;
|
|
538
|
+
|
|
539
|
+
CREATE MATERIALIZED VIEW IF NOT EXISTS agents.tokens_cost_by_episode AS
|
|
540
|
+
SELECT * FROM agents.tokens_cost_by_episode_live
|
|
541
|
+
WITH DATA;
|
|
542
|
+
CREATE UNIQUE INDEX IF NOT EXISTS tokens_cost_by_episode_key ON agents.tokens_cost_by_episode (episode_id);
|
|
543
|
+
|
|
544
|
+
-- Which sources reported per month: local days with data, by source. agents = usage rows
|
|
545
|
+
-- (tokens_cost_by_model_daily, refresh it first), activitywatch = window time, outcomes = git
|
|
546
|
+
-- outcome decisions, labels = label rows by their unit's day, manual = hand-entered active time. A
|
|
547
|
+
-- month a source never reported has no row for it.
|
|
548
|
+
CREATE OR REPLACE VIEW agents.coverage_monthly AS
|
|
549
|
+
SELECT date_trunc('month', day::timestamp)::date AS month, source, count(DISTINCT day)::int AS days
|
|
550
|
+
FROM (SELECT 'agents' AS source, day FROM agents.tokens_cost_by_model_daily
|
|
551
|
+
UNION ALL
|
|
552
|
+
SELECT 'activitywatch', day FROM agents.active_daily WHERE minutes_aw > 0
|
|
553
|
+
UNION ALL
|
|
554
|
+
SELECT 'manual', day FROM agents.active_daily WHERE minutes_manual > 0
|
|
555
|
+
UNION ALL
|
|
556
|
+
SELECT 'outcomes', (decided_at AT TIME ZONE '@AUT_TZ@')::date FROM agents.commit_outcome
|
|
557
|
+
UNION ALL
|
|
558
|
+
SELECT 'labels', (ts AT TIME ZONE '@AUT_TZ@')::date FROM agents.activity_event
|
|
559
|
+
WHERE kind = 'label') s
|
|
560
|
+
GROUP BY 1, 2;
|
|
561
|
+
|
|
562
|
+
-- Efficiency per month (episodes by their start month): median tokens and cost of a shipped
|
|
563
|
+
-- episode, and all counted episodes' tokens and cost per shipped episode. Every counted episode
|
|
564
|
+
-- is in tokens_cost_by_episode, so shipped and episodes match outcomes_monthly.
|
|
565
|
+
CREATE OR REPLACE VIEW agents.efficiency_by_episode AS
|
|
566
|
+
SELECT month,
|
|
567
|
+
count(*) FILTER (WHERE outcome = 'shipped')::int AS shipped,
|
|
568
|
+
count(*)::int AS episodes,
|
|
569
|
+
percentile_cont(0.5) WITHIN GROUP (ORDER BY total_tokens)
|
|
570
|
+
FILTER (WHERE outcome = 'shipped') AS median_tokens_shipped,
|
|
571
|
+
percentile_cont(0.5) WITHIN GROUP (ORDER BY est_cost_usd)
|
|
572
|
+
FILTER (WHERE outcome = 'shipped') AS median_cost_usd_shipped,
|
|
573
|
+
round(sum(total_tokens) / nullif(count(*) FILTER (WHERE outcome = 'shipped'), 0))
|
|
574
|
+
AS tokens_per_shipped,
|
|
575
|
+
round(sum(est_cost_usd) / nullif(count(*) FILTER (WHERE outcome = 'shipped'), 0), 2)
|
|
576
|
+
AS cost_usd_per_shipped
|
|
577
|
+
FROM agents.tokens_cost_by_episode
|
|
578
|
+
GROUP BY month;
|
|
579
|
+
|
|
580
|
+
-- ---------------------------------------------------------------------------------------------
|
|
581
|
+
-- Counting audit, per local day.
|
|
582
|
+
-- raw_usage_rows / dedup_usage_rows usage_event rows vs distinct event_id (by first ts)
|
|
583
|
+
-- dup_groups event_ids stored more than once
|
|
584
|
+
-- dup_unexplained of those, groups the documented re-log rule does not
|
|
585
|
+
-- explain: Claude Code re-logs one message with another ts
|
|
586
|
+
-- on one machine; any other agent, or two machines or agents
|
|
587
|
+
-- under one event_id, is unexplained (a monitoring check
|
|
588
|
+
-- should alert on it)
|
|
589
|
+
-- activity_ts_conflicts activity event_ids stored under two or more ts (the views
|
|
590
|
+
-- read them once; a monitoring check should alert on them)
|
|
591
|
+
-- steer_human per agent {steers, human}: steers vs human usage rows
|
|
592
|
+
-- (both one per event_id)
|
|
593
|
+
-- scripted_share automated sessions among sessions with a row that day
|
|
594
|
+
-- stale_collectors collectors (scanned in the 30 days before) with no ok
|
|
595
|
+
-- scan in the 80 hours before the day's end
|
|
596
|
+
-- links_without_session link rows with no event_session row for their session
|
|
597
|
+
-- overlap_share share of per-source active minutes (steer + aw + manual)
|
|
598
|
+
-- that overlap another source, from agents.active_daily
|
|
599
|
+
-- ---------------------------------------------------------------------------------------------
|
|
600
|
+
CREATE OR REPLACE VIEW agents.audit_daily_live AS
|
|
601
|
+
WITH u AS (
|
|
602
|
+
SELECT event_id, (min(ts) AT TIME ZONE '@AUT_TZ@')::date AS day, count(*) AS n,
|
|
603
|
+
count(DISTINCT machine) AS machines, count(DISTINCT agent) AS agents, min(agent) AS agent,
|
|
604
|
+
bool_or(kind = 'human') AS human
|
|
605
|
+
FROM agents.usage_event
|
|
606
|
+
GROUP BY event_id
|
|
607
|
+
), ud AS (
|
|
608
|
+
SELECT day, sum(n)::bigint AS raw_usage_rows, count(*)::bigint AS dedup_usage_rows,
|
|
609
|
+
count(*) FILTER (WHERE n > 1)::int AS dup_groups,
|
|
610
|
+
count(*) FILTER (WHERE n > 1 AND NOT (agent = 'claude_code' AND agents = 1 AND machines = 1))::int
|
|
611
|
+
AS dup_unexplained
|
|
612
|
+
FROM u GROUP BY day
|
|
613
|
+
), sh AS (
|
|
614
|
+
SELECT day, jsonb_object_agg(agent, jsonb_build_object('steers', steers, 'human', human)) AS steer_human
|
|
615
|
+
FROM (SELECT coalesce(s.day, h.day) AS day, coalesce(s.agent, h.agent) AS agent,
|
|
616
|
+
coalesce(s.n, 0) AS steers, coalesce(h.n, 0) AS human
|
|
617
|
+
FROM (SELECT (ts AT TIME ZONE '@AUT_TZ@')::date AS day, agent, count(*) AS n
|
|
618
|
+
FROM (SELECT DISTINCT ON (event_id) ts, agent FROM agents.activity_event
|
|
619
|
+
WHERE kind = 'steer' ORDER BY event_id, ts) d
|
|
620
|
+
GROUP BY 1, 2) s
|
|
621
|
+
FULL JOIN (SELECT day, agent, count(*) AS n FROM u WHERE human GROUP BY 1, 2) h
|
|
622
|
+
ON h.day = s.day AND h.agent = s.agent) x
|
|
623
|
+
GROUP BY day
|
|
624
|
+
), ac AS (
|
|
625
|
+
SELECT (ts AT TIME ZONE '@AUT_TZ@')::date AS day, count(*)::int AS activity_ts_conflicts
|
|
626
|
+
FROM (SELECT min(ts) AS ts FROM agents.activity_event GROUP BY event_id HAVING count(*) > 1) x
|
|
627
|
+
GROUP BY 1
|
|
628
|
+
), ss AS (
|
|
629
|
+
SELECT x.day,
|
|
630
|
+
round(count(*) FILTER (WHERE o.automated)::numeric / count(*), 4) AS scripted_share
|
|
631
|
+
FROM (SELECT DISTINCT (ts AT TIME ZONE '@AUT_TZ@')::date AS day, session_id
|
|
632
|
+
FROM agents.activity_event WHERE session_id IS NOT NULL) x
|
|
633
|
+
LEFT JOIN agents.session_origin o USING (session_id)
|
|
634
|
+
GROUP BY 1
|
|
635
|
+
), lk AS (
|
|
636
|
+
SELECT (a.ts AT TIME ZONE '@AUT_TZ@')::date AS day,
|
|
637
|
+
count(*) FILTER (WHERE o.session_id IS NULL)::int AS links_without_session
|
|
638
|
+
FROM agents.activity_event a
|
|
639
|
+
LEFT JOIN agents.session_origin o USING (session_id)
|
|
640
|
+
WHERE a.kind = 'link'
|
|
641
|
+
GROUP BY 1
|
|
642
|
+
), spine AS (
|
|
643
|
+
SELECT day FROM ud UNION SELECT day FROM sh UNION SELECT day FROM ac UNION SELECT day FROM ss
|
|
644
|
+
UNION SELECT day FROM lk UNION SELECT day FROM agents.active_daily
|
|
645
|
+
UNION SELECT (finished_at AT TIME ZONE '@AUT_TZ@')::date FROM agents.collector_scan
|
|
646
|
+
), cs AS (
|
|
647
|
+
-- the latest scan and the latest ok scan of each collector before the day's end (index
|
|
648
|
+
-- lookups on the primary key, newest first)
|
|
649
|
+
SELECT d.day, count(*) FILTER (WHERE lo.finished_at IS NULL
|
|
650
|
+
OR lo.finished_at < d.day_end - interval '80 hours')::int AS stale_collectors
|
|
651
|
+
FROM (SELECT day, least(((day + 1)::timestamp) AT TIME ZONE '@AUT_TZ@', now()) AS day_end
|
|
652
|
+
FROM spine) d
|
|
653
|
+
CROSS JOIN (SELECT DISTINCT machine, collector FROM agents.collector_scan) p
|
|
654
|
+
CROSS JOIN LATERAL (SELECT s.finished_at FROM agents.collector_scan s
|
|
655
|
+
WHERE s.machine = p.machine AND s.collector = p.collector
|
|
656
|
+
AND s.finished_at < d.day_end
|
|
657
|
+
ORDER BY s.finished_at DESC LIMIT 1) la
|
|
658
|
+
LEFT JOIN LATERAL (SELECT s.finished_at FROM agents.collector_scan s
|
|
659
|
+
WHERE s.machine = p.machine AND s.collector = p.collector
|
|
660
|
+
AND s.finished_at < d.day_end AND s.status = 'ok'
|
|
661
|
+
ORDER BY s.finished_at DESC LIMIT 1) lo ON true
|
|
662
|
+
WHERE la.finished_at > d.day_end - interval '30 days'
|
|
663
|
+
GROUP BY d.day
|
|
664
|
+
)
|
|
665
|
+
SELECT s.day,
|
|
666
|
+
coalesce(ud.raw_usage_rows, 0) AS raw_usage_rows,
|
|
667
|
+
coalesce(ud.dedup_usage_rows, 0) AS dedup_usage_rows,
|
|
668
|
+
coalesce(ud.dup_groups, 0) AS dup_groups,
|
|
669
|
+
coalesce(ud.dup_unexplained, 0) AS dup_unexplained,
|
|
670
|
+
coalesce(ac.activity_ts_conflicts, 0) AS activity_ts_conflicts,
|
|
671
|
+
coalesce(sh.steer_human, '{}'::jsonb) AS steer_human,
|
|
672
|
+
ss.scripted_share,
|
|
673
|
+
coalesce(cs.stale_collectors, 0) AS stale_collectors,
|
|
674
|
+
coalesce(lk.links_without_session, 0) AS links_without_session,
|
|
675
|
+
round(1 - ad.minutes_all
|
|
676
|
+
/ nullif(ad.minutes_steer + ad.minutes_aw + ad.minutes_manual, 0), 4) AS overlap_share
|
|
677
|
+
FROM spine s
|
|
678
|
+
LEFT JOIN ud USING (day) LEFT JOIN sh USING (day) LEFT JOIN ac USING (day)
|
|
679
|
+
LEFT JOIN ss USING (day) LEFT JOIN lk USING (day) LEFT JOIN cs USING (day)
|
|
680
|
+
LEFT JOIN agents.active_daily ad USING (day);
|
|
681
|
+
|
|
682
|
+
CREATE MATERIALIZED VIEW IF NOT EXISTS agents.audit_daily AS
|
|
683
|
+
SELECT * FROM agents.audit_daily_live
|
|
684
|
+
WITH DATA;
|
|
685
|
+
CREATE UNIQUE INDEX IF NOT EXISTS audit_daily_day_idx ON agents.audit_daily (day);
|
|
686
|
+
|
|
687
|
+
-- ---------------------------------------------------------------------------------------------
|
|
688
|
+
-- Interventions: an interrupt or a rejection followed by a steer in the same session. The turn
|
|
689
|
+
-- before is the case's own turn (its turn_id), the turn after is the next steer's; for each, the
|
|
690
|
+
-- turn rows' tool_errors, retry and runaway flags, and whether a commit row shares the turn.
|
|
691
|
+
-- Ids only. NULL numbers mean no turn row carried that turn_id.
|
|
692
|
+
-- ---------------------------------------------------------------------------------------------
|
|
693
|
+
CREATE OR REPLACE VIEW agents.intervention_cases AS
|
|
694
|
+
WITH ev AS (
|
|
695
|
+
SELECT event_id, ts, machine, agent, session_id, turn_id, kind,
|
|
696
|
+
count(*) FILTER (WHERE kind = 'steer') OVER (
|
|
697
|
+
PARTITION BY machine, agent, session_id ORDER BY ts, kind = 'steer', event_id
|
|
698
|
+
ROWS UNBOUNDED PRECEDING) AS n_steer
|
|
699
|
+
FROM (SELECT DISTINCT ON (event_id) event_id, ts, machine, agent, session_id, turn_id, kind
|
|
700
|
+
FROM agents.activity_event
|
|
701
|
+
WHERE kind IN ('interrupt', 'rejection', 'steer') AND session_id IS NOT NULL
|
|
702
|
+
ORDER BY event_id, ts) a
|
|
703
|
+
), turns AS (
|
|
704
|
+
SELECT machine, agent, session_id, turn_id,
|
|
705
|
+
sum(CASE WHEN attrs->>'tool_errors' ~ '^[0-9]+$' THEN (attrs->>'tool_errors')::int END)
|
|
706
|
+
FILTER (WHERE kind = 'turn') AS tool_errors,
|
|
707
|
+
bool_or(attrs->'retry_flag' = 'true') FILTER (WHERE kind = 'turn') AS retry,
|
|
708
|
+
bool_or(attrs->'runaway_flag' = 'true') FILTER (WHERE kind = 'turn') AS runaway,
|
|
709
|
+
bool_or(kind = 'commit') AS committed
|
|
710
|
+
FROM (SELECT DISTINCT ON (event_id) machine, agent, session_id, turn_id, kind, attrs
|
|
711
|
+
FROM agents.activity_event
|
|
712
|
+
WHERE kind IN ('turn', 'commit') AND session_id IS NOT NULL AND turn_id IS NOT NULL
|
|
713
|
+
ORDER BY event_id, ts) a
|
|
714
|
+
GROUP BY 1, 2, 3, 4
|
|
715
|
+
)
|
|
716
|
+
SELECT c.event_id, c.kind, c.ts, c.agent, c.session_id,
|
|
717
|
+
s.event_id AS steer_event_id, c.turn_id AS before_turn_id, s.turn_id AS after_turn_id,
|
|
718
|
+
b.tool_errors AS before_tool_errors, b.retry AS before_retry, b.runaway AS before_runaway,
|
|
719
|
+
coalesce(b.committed, false) AS before_commit,
|
|
720
|
+
a.tool_errors AS after_tool_errors, a.retry AS after_retry, a.runaway AS after_runaway,
|
|
721
|
+
coalesce(a.committed, false) AS after_commit
|
|
722
|
+
FROM ev c
|
|
723
|
+
JOIN ev s ON s.kind = 'steer' AND s.machine = c.machine AND s.agent = c.agent
|
|
724
|
+
AND s.session_id = c.session_id AND s.n_steer = c.n_steer + 1
|
|
725
|
+
LEFT JOIN turns b ON b.machine = c.machine AND b.agent = c.agent AND b.session_id = c.session_id
|
|
726
|
+
AND b.turn_id = c.turn_id
|
|
727
|
+
LEFT JOIN turns a ON a.machine = s.machine AND a.agent = s.agent AND a.session_id = s.session_id
|
|
728
|
+
AND a.turn_id = s.turn_id
|
|
729
|
+
WHERE c.kind IN ('interrupt', 'rejection');
|
|
730
|
+
|
|
731
|
+
CREATE OR REPLACE VIEW agents.intervention_monthly AS
|
|
732
|
+
SELECT date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
733
|
+
count(*)::int AS cases,
|
|
734
|
+
count(*) FILTER (WHERE kind = 'interrupt')::int AS interrupts,
|
|
735
|
+
count(*) FILTER (WHERE kind = 'rejection')::int AS rejections,
|
|
736
|
+
round(avg(before_tool_errors), 2) AS before_tool_errors_avg,
|
|
737
|
+
round(avg(after_tool_errors), 2) AS after_tool_errors_avg,
|
|
738
|
+
count(*) FILTER (WHERE before_retry)::int AS before_retries,
|
|
739
|
+
count(*) FILTER (WHERE after_retry)::int AS after_retries,
|
|
740
|
+
count(*) FILTER (WHERE before_runaway)::int AS before_runaways,
|
|
741
|
+
count(*) FILTER (WHERE after_runaway)::int AS after_runaways,
|
|
742
|
+
count(*) FILTER (WHERE before_commit)::int AS before_commits,
|
|
743
|
+
count(*) FILTER (WHERE after_commit)::int AS after_commits
|
|
744
|
+
FROM agents.intervention_cases
|
|
745
|
+
GROUP BY 1;
|
|
746
|
+
|
|
747
|
+
-- Every eval record aut wrote (one row per backend per bake-off), numbers and ids only; per-label
|
|
748
|
+
-- detail of the newest one is agents.eval_latest.
|
|
749
|
+
CREATE OR REPLACE VIEW agents.eval_history AS
|
|
750
|
+
SELECT ts AS evaluated_at,
|
|
751
|
+
attrs->>'task' AS task,
|
|
752
|
+
(attrs->>'task_ver')::int AS task_ver,
|
|
753
|
+
attrs->>'backend' AS backend,
|
|
754
|
+
attrs->>'model_id' AS model_id,
|
|
755
|
+
attrs->>'model_rev' AS model_rev,
|
|
756
|
+
(attrs->>'size_mb')::double precision AS size_mb,
|
|
757
|
+
(attrs->>'temperature')::double precision AS temperature,
|
|
758
|
+
(attrs->>'n')::int AS n,
|
|
759
|
+
(attrs->>'n_cal')::int AS n_cal,
|
|
760
|
+
(attrs->>'n_held')::int AS n_held,
|
|
761
|
+
(attrs->>'macro_f1')::double precision AS macro_f1,
|
|
762
|
+
(attrs->>'kappa')::double precision AS kappa,
|
|
763
|
+
(attrs->>'ece_raw')::double precision AS ece_raw,
|
|
764
|
+
(attrs->>'ece_cal')::double precision AS ece_cal,
|
|
765
|
+
(attrs->>'coverage')::double precision AS coverage,
|
|
766
|
+
(attrs->>'s_per_decision')::double precision AS s_per_decision
|
|
767
|
+
FROM agents.activity_event
|
|
768
|
+
WHERE kind = 'eval';
|
|
769
|
+
|
|
770
|
+
-- ---------------------------------------------------------------------------------------------
|
|
771
|
+
-- Phase 6: what the publisher reads (weeks are ISO weeks by their Monday, days local days).
|
|
772
|
+
-- The database keeps every label; only the publisher's one table merges them for the public file.
|
|
773
|
+
-- ---------------------------------------------------------------------------------------------
|
|
774
|
+
|
|
775
|
+
-- Commits per local day from the full git scan, the latest scan of each day. Not the same
|
|
776
|
+
-- count as the commit rows above, which hold only what agent logs printed. Never add the two.
|
|
777
|
+
CREATE OR REPLACE VIEW agents.git_commits_daily AS
|
|
778
|
+
WITH latest_complete AS (
|
|
779
|
+
SELECT DISTINCT ON (day) day, scanned_at
|
|
780
|
+
FROM agents.git_commit_day
|
|
781
|
+
WHERE complete
|
|
782
|
+
ORDER BY day, scanned_at DESC
|
|
783
|
+
), candidates AS (
|
|
784
|
+
SELECT g.day, g.commits, g.complete, g.scanned_at
|
|
785
|
+
FROM agents.git_commit_day g
|
|
786
|
+
LEFT JOIN latest_complete c USING (day)
|
|
787
|
+
WHERE c.day IS NULL OR g.scanned_at >= c.scanned_at
|
|
788
|
+
)
|
|
789
|
+
-- Partial reads cannot lower a count or downgrade unchanged complete coverage.
|
|
790
|
+
-- A newer complete scan can still apply the collector's explicit audited reset.
|
|
791
|
+
SELECT DISTINCT ON (day) day, commits, complete
|
|
792
|
+
FROM candidates
|
|
793
|
+
ORDER BY day, commits DESC, complete DESC, scanned_at DESC;
|
|
794
|
+
|
|
795
|
+
-- Counted steers per local day, read live: effort_period's steer rule without waiting for its refresh.
|
|
796
|
+
-- claude_code_steers: those of Claude Code, the only agent whose steer text the labeler reads, so the
|
|
797
|
+
-- publisher waits for labels only in weeks that have some.
|
|
798
|
+
CREATE OR REPLACE VIEW agents.steers_counted_daily AS
|
|
799
|
+
SELECT (a.ts AT TIME ZONE '@AUT_TZ@')::date AS day, count(*)::int AS steers,
|
|
800
|
+
count(*) FILTER (WHERE a.agent = 'claude_code')::int AS claude_code_steers
|
|
801
|
+
FROM (SELECT DISTINCT ON (event_id) ts, session_id, agent FROM agents.activity_event
|
|
802
|
+
WHERE kind = 'steer' AND session_id IS NOT NULL ORDER BY event_id, ts) a
|
|
803
|
+
LEFT JOIN agents.session_origin o USING (session_id)
|
|
804
|
+
WHERE NOT coalesce(o.scripted, false)
|
|
805
|
+
GROUP BY 1;
|
|
806
|
+
|
|
807
|
+
-- Steer estimates per local day (aut history-steers: attrs.count, one row per local day and agent,
|
|
808
|
+
-- stamped noon local). Kept apart from counted steers: no other view reads this kind.
|
|
809
|
+
CREATE OR REPLACE VIEW agents.steer_estimate_daily AS
|
|
810
|
+
SELECT (ts AT TIME ZONE '@AUT_TZ@')::date AS day, sum((attrs->>'count')::int)::int AS steers_estimated
|
|
811
|
+
FROM (SELECT DISTINCT ON (event_id) ts, attrs FROM agents.activity_event
|
|
812
|
+
WHERE kind = 'steer_estimate' AND attrs->>'count' ~ '^[0-9]+$' ORDER BY event_id, ts) a
|
|
813
|
+
GROUP BY 1;
|
|
814
|
+
|
|
815
|
+
-- The public session: interactive origin and at least one steer. Each count holds at its own grain
|
|
816
|
+
-- only: a session that spans days or weeks shows in each, so never add them into a total.
|
|
817
|
+
CREATE OR REPLACE VIEW agents.session_steer_day AS
|
|
818
|
+
SELECT DISTINCT (a.ts AT TIME ZONE '@AUT_TZ@')::date AS day, a.session_id
|
|
819
|
+
FROM (SELECT DISTINCT ON (event_id) ts, session_id FROM agents.activity_event
|
|
820
|
+
WHERE kind = 'steer' ORDER BY event_id, ts) a
|
|
821
|
+
JOIN agents.session_origin o USING (session_id)
|
|
822
|
+
WHERE o.interactive;
|
|
823
|
+
|
|
824
|
+
CREATE OR REPLACE VIEW agents.sessions_steered_daily AS
|
|
825
|
+
SELECT day, count(*)::int AS sessions
|
|
826
|
+
FROM agents.session_steer_day
|
|
827
|
+
GROUP BY 1;
|
|
828
|
+
|
|
829
|
+
CREATE OR REPLACE VIEW agents.sessions_steered_weekly AS
|
|
830
|
+
SELECT date_trunc('week', day::timestamp)::date AS week, count(DISTINCT session_id)::int AS sessions
|
|
831
|
+
FROM agents.session_steer_day
|
|
832
|
+
GROUP BY 1;
|
|
833
|
+
|
|
834
|
+
-- Twins of label_share_monthly, work_mix_monthly and steer_intent_monthly.
|
|
835
|
+
CREATE OR REPLACE VIEW agents.label_share_weekly AS
|
|
836
|
+
SELECT task, week, label, units,
|
|
837
|
+
CASE WHEN label IS NOT NULL
|
|
838
|
+
THEN round(units::numeric / nullif(sum(units) FILTER (WHERE label IS NOT NULL) OVER w, 0), 4)
|
|
839
|
+
END AS share,
|
|
840
|
+
round(coalesce(sum(units) FILTER (WHERE label IS NULL) OVER w, 0)::numeric / sum(units) OVER w, 4)
|
|
841
|
+
AS unknown_share
|
|
842
|
+
FROM (SELECT task, date_trunc('week', ts AT TIME ZONE '@AUT_TZ@')::date AS week,
|
|
843
|
+
CASE WHEN NOT below_tau THEN label END AS label, count(*)::int AS units
|
|
844
|
+
FROM agents.label_latest
|
|
845
|
+
GROUP BY 1, 2, 3) x
|
|
846
|
+
WINDOW w AS (PARTITION BY task, week);
|
|
847
|
+
|
|
848
|
+
CREATE OR REPLACE VIEW agents.work_mix_weekly AS
|
|
849
|
+
SELECT week, label AS category, units, share AS unit_share, unknown_share
|
|
850
|
+
FROM agents.label_share_weekly WHERE task = 'work_category';
|
|
851
|
+
|
|
852
|
+
CREATE OR REPLACE VIEW agents.steer_intent_weekly AS
|
|
853
|
+
SELECT week, label AS intent, units, share, unknown_share
|
|
854
|
+
FROM agents.label_share_weekly WHERE task = 'steer_intent';
|
|
855
|
+
|
|
856
|
+
-- Counted episodes by the week they started, each once. still_open: git open or undecided, or last
|
|
857
|
+
-- active under 14 days ago (a policy choice, it can change here), whatever the outcome says. The
|
|
858
|
+
-- soft_ columns split no_code by the confident soft_outcome label; no_code is what has none.
|
|
859
|
+
CREATE OR REPLACE VIEW agents.outcomes_weekly AS
|
|
860
|
+
SELECT week,
|
|
861
|
+
count(*)::int AS episodes,
|
|
862
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'shipped')::int AS shipped,
|
|
863
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'reworked')::int AS reworked,
|
|
864
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'ruled_out')::int AS ruled_out,
|
|
865
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'no_code' AND soft IS NULL)::int AS no_code,
|
|
866
|
+
count(*) FILTER (WHERE unsettled)::int AS still_open,
|
|
867
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'no_code' AND soft = 'resolved')::int AS soft_resolved,
|
|
868
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'no_code' AND soft = 'blocked')::int AS soft_blocked,
|
|
869
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'no_code' AND soft = 'ruled_out')::int AS soft_ruled_out,
|
|
870
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'no_code' AND soft = 'still_open')::int AS soft_still_open
|
|
871
|
+
FROM (SELECT date_trunc('week', c.first_ts AT TIME ZONE '@AUT_TZ@')::date AS week, c.outcome, l.label AS soft,
|
|
872
|
+
c.outcome IN ('open', 'undecided') OR c.last_ts + interval '14 days' > now() AS unsettled
|
|
873
|
+
FROM agents.episode_counted c
|
|
874
|
+
LEFT JOIN agents.label_latest l
|
|
875
|
+
ON l.task = 'soft_outcome' AND l.unit = 'episode' AND NOT l.below_tau AND l.unit_id = c.episode_id::text) e
|
|
876
|
+
GROUP BY week;
|
|
877
|
+
|
|
878
|
+
-- Twin of shipping_rework_monthly: commits by the week their first commit row landed.
|
|
879
|
+
CREATE OR REPLACE VIEW agents.shipping_rework_weekly AS
|
|
880
|
+
SELECT c.week,
|
|
881
|
+
count(*)::int AS commits,
|
|
882
|
+
count(*) FILTER (WHERE s.state = 'merged')::int AS merged,
|
|
883
|
+
count(*) FILTER (WHERE s.state = 'reverted')::int AS reverted,
|
|
884
|
+
count(*) FILTER (WHERE s.state = 'reworked')::int AS reworked,
|
|
885
|
+
count(*) FILTER (WHERE s.state = 'open')::int AS open,
|
|
886
|
+
count(*) FILTER (WHERE s.state = 'abandoned')::int AS abandoned,
|
|
887
|
+
count(*) FILTER (WHERE coalesce(s.state, 'undecided') = 'undecided')::int AS undecided
|
|
888
|
+
FROM (SELECT DISTINCT ON (left(lower(attrs->>'sha'), 7)) left(lower(attrs->>'sha'), 7) AS k,
|
|
889
|
+
date_trunc('week', ts AT TIME ZONE '@AUT_TZ@')::date AS week
|
|
890
|
+
FROM agents.activity_event
|
|
891
|
+
WHERE kind = 'commit' AND lower(attrs->>'sha') ~ '^[0-9a-f]{7,40}$'
|
|
892
|
+
ORDER BY 1, ts) c
|
|
893
|
+
LEFT JOIN agents.commit_state s USING (k)
|
|
894
|
+
WHERE s.state IS DISTINCT FROM 'superseded'
|
|
895
|
+
GROUP BY c.week;
|
|
896
|
+
|
|
897
|
+
-- Skill invokes with the week of their skill's first invoke. Twin of built_reused_monthly.
|
|
898
|
+
CREATE OR REPLACE VIEW agents.skill_invoke_week AS
|
|
899
|
+
SELECT attrs->>'skill' AS skill,
|
|
900
|
+
date_trunc('week', ts AT TIME ZONE '@AUT_TZ@')::date AS week,
|
|
901
|
+
row_number() OVER (PARTITION BY attrs->>'skill' ORDER BY ts, event_id) AS nth,
|
|
902
|
+
min(date_trunc('week', ts AT TIME ZONE '@AUT_TZ@')::date) OVER (PARTITION BY attrs->>'skill') AS first_week
|
|
903
|
+
FROM (SELECT DISTINCT ON (event_id) event_id, ts, attrs FROM agents.activity_event
|
|
904
|
+
WHERE kind = 'invoke' ORDER BY event_id, ts) a
|
|
905
|
+
WHERE coalesce(attrs->>'skill', '') NOT IN ('', 'mcp');
|
|
906
|
+
|
|
907
|
+
CREATE OR REPLACE VIEW agents.built_reused_weekly AS
|
|
908
|
+
SELECT week,
|
|
909
|
+
count(*) FILTER (WHERE nth = 1)::int AS skills_new,
|
|
910
|
+
count(*)::int AS invocations,
|
|
911
|
+
count(*) FILTER (WHERE nth > 1)::int AS later_invocations,
|
|
912
|
+
count(DISTINCT skill) FILTER (WHERE first_week < week)::int AS skills_reused
|
|
913
|
+
FROM agents.skill_invoke_week
|
|
914
|
+
GROUP BY week;
|
|
915
|
+
|
|
916
|
+
-- Real distinct counts over the 26 weeks ending with last_week, one row per week so the publisher
|
|
917
|
+
-- picks its window. Week rows cannot be added into these: a session or skill spans weeks.
|
|
918
|
+
CREATE OR REPLACE VIEW agents.distinct_26_weeks AS
|
|
919
|
+
WITH s AS MATERIALIZED (SELECT day, session_id FROM agents.session_steer_day),
|
|
920
|
+
r AS MATERIALIZED (SELECT DISTINCT skill, week FROM agents.skill_invoke_week WHERE first_week < week)
|
|
921
|
+
SELECT w.last_week,
|
|
922
|
+
(SELECT count(DISTINCT s.session_id) FROM s
|
|
923
|
+
WHERE s.day >= w.last_week - 175 AND s.day < w.last_week + 7)::int AS sessions,
|
|
924
|
+
(SELECT count(DISTINCT r.skill) FROM r
|
|
925
|
+
WHERE r.week >= w.last_week - 175 AND r.week <= w.last_week)::int AS skills_reused
|
|
926
|
+
FROM (SELECT generate_series(date_trunc('week', min(day)::timestamp),
|
|
927
|
+
now() AT TIME ZONE '@AUT_TZ@', interval '7 days')::date AS last_week
|
|
928
|
+
FROM s) w;
|
|
929
|
+
|
|
930
|
+
-- Twin of follow_through_monthly. Not proved public: the publisher reads it behind its own switch.
|
|
931
|
+
CREATE OR REPLACE VIEW agents.follow_through_weekly AS
|
|
932
|
+
SELECT date_trunc('week', ts AT TIME ZONE '@AUT_TZ@')::date AS week,
|
|
933
|
+
count(*)::int AS commits,
|
|
934
|
+
count(*) FILTER (WHERE follow_through)::int AS follow_through_commits,
|
|
935
|
+
count(DISTINCT repo_key) FILTER (WHERE follow_through)::int AS follow_through_repos
|
|
936
|
+
FROM agents.follow_through_commit
|
|
937
|
+
GROUP BY 1;
|
|
938
|
+
|
|
939
|
+
-- Provisional historical estimates stay separate from calibrated task names.
|
|
940
|
+
-- The importer sends only ids, enums and numbers, never review reasons or source text.
|
|
941
|
+
CREATE OR REPLACE VIEW agents.working_label_latest AS
|
|
942
|
+
SELECT DISTINCT ON (attrs->>'task', attrs->>'unit', attrs->>'unit_id')
|
|
943
|
+
substring(attrs->>'task' from 9) AS task,
|
|
944
|
+
attrs->>'unit_id' AS unit_id,
|
|
945
|
+
ts,
|
|
946
|
+
attrs->>'label' AS label,
|
|
947
|
+
(attrs->>'population_weight')::numeric AS population_weight,
|
|
948
|
+
attrs->>'label_source' AS label_source,
|
|
949
|
+
attrs->>'model_id' AS model_id,
|
|
950
|
+
attrs->>'baseline_id' AS baseline_id,
|
|
951
|
+
coalesce(attrs->'signals', '{}'::jsonb) AS signals
|
|
952
|
+
FROM agents.activity_event
|
|
953
|
+
WHERE kind = 'label'
|
|
954
|
+
AND attrs->>'label_policy' = 'working-sonnet-v1'
|
|
955
|
+
AND attrs->>'provisional' = 'true'
|
|
956
|
+
AND attrs->>'human_verified' = 'false'
|
|
957
|
+
AND attrs->>'task' IN ('working.steer_intent', 'working.soft_outcome')
|
|
958
|
+
ORDER BY attrs->>'task', attrs->>'unit', attrs->>'unit_id',
|
|
959
|
+
attrs->>'labelled_at' DESC NULLS LAST, event_id DESC;
|
|
960
|
+
|
|
961
|
+
CREATE OR REPLACE VIEW agents.working_label_monthly AS
|
|
962
|
+
SELECT task, month, label, units, estimated_units,
|
|
963
|
+
estimated_units / sum(estimated_units) OVER w AS estimated_share,
|
|
964
|
+
coalesce(sum(estimated_units) FILTER (WHERE label = 'unknown') OVER w, 0)
|
|
965
|
+
/ sum(estimated_units) OVER w AS unknown_share,
|
|
966
|
+
'provisional'::text AS quality
|
|
967
|
+
FROM (
|
|
968
|
+
SELECT task, date_trunc('month', ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
969
|
+
label, count(*)::int AS units, sum(population_weight) AS estimated_units
|
|
970
|
+
FROM agents.working_label_latest
|
|
971
|
+
GROUP BY 1, 2, 3
|
|
972
|
+
) x
|
|
973
|
+
WINDOW w AS (PARTITION BY task, month);
|
|
974
|
+
|
|
975
|
+
-- Signal counts describe the reviewed subset, never a population-weighted prevalence.
|
|
976
|
+
CREATE OR REPLACE VIEW agents.working_signal_monthly AS
|
|
977
|
+
SELECT date_trunc('month', l.ts AT TIME ZONE '@AUT_TZ@')::date AS month,
|
|
978
|
+
q.question, coalesce(l.signals->q.question->>'answer', 'not_reviewed') AS answer,
|
|
979
|
+
count(*)::int AS units,
|
|
980
|
+
CASE WHEN l.signals ? q.question THEN 'agent_review' ELSE 'none' END AS review_source,
|
|
981
|
+
'provisional'::text AS quality
|
|
982
|
+
FROM agents.working_label_latest l
|
|
983
|
+
CROSS JOIN LATERAL unnest(CASE WHEN l.task = 'steer_intent'
|
|
984
|
+
THEN ARRAY['explicit_halt', 'disputes_agent_work']
|
|
985
|
+
ELSE ARRAY['required_next_step', 'external_blocker'] END) q(question)
|
|
986
|
+
GROUP BY 1, 2, 3, 5;
|
|
987
|
+
|
|
988
|
+
-- Private reference estimates describe only the frozen sampled frame, not all agent activity.
|
|
989
|
+
CREATE OR REPLACE VIEW agents.working_label_weekly AS
|
|
990
|
+
SELECT task, date_trunc('week', ts AT TIME ZONE '@AUT_TZ@')::date AS week,
|
|
991
|
+
label, count(*)::int AS units, sum(population_weight) AS estimated_units
|
|
992
|
+
FROM agents.working_label_latest
|
|
993
|
+
GROUP BY 1, 2, 3;
|
|
994
|
+
|
|
995
|
+
-- Audited missing inputs cannot regain a classification through a model retry.
|
|
996
|
+
CREATE OR REPLACE VIEW agents.decision_input_status_latest AS
|
|
997
|
+
SELECT DISTINCT ON (c.attrs->>'task', c.attrs->>'task_ver', c.attrs->>'unit',
|
|
998
|
+
c.attrs->>'unit_id', c.attrs->>'input_hash')
|
|
999
|
+
substring(c.attrs->>'task' from 15) AS task,
|
|
1000
|
+
CASE c.attrs->>'task_ver' WHEN '1' THEN 1 WHEN '2' THEN 2 WHEN '3' THEN 3 END AS task_ver,
|
|
1001
|
+
c.attrs->>'unit' AS unit, c.attrs->>'unit_id' AS unit_id,
|
|
1002
|
+
c.attrs->>'input_hash' AS input_hash, c.attrs->>'state' AS input_state
|
|
1003
|
+
FROM agents.activity_event c
|
|
1004
|
+
JOIN agents.activity_event t ON t.event_id = c.attrs->>'target_event_id'
|
|
1005
|
+
AND t.kind = 'label' AND t.attrs->>'label_source' = 'local_decision'
|
|
1006
|
+
AND t.attrs->>'task' = c.attrs->>'task' AND t.attrs->>'task_ver' = c.attrs->>'task_ver'
|
|
1007
|
+
AND t.attrs->>'unit' = c.attrs->>'unit' AND t.attrs->>'unit_id' = c.attrs->>'unit_id'
|
|
1008
|
+
AND t.attrs->>'input_hash' = c.attrs->>'input_hash'
|
|
1009
|
+
WHERE c.kind = 'label_input_status' AND c.attrs->>'label_source' = 'local_input_guard'
|
|
1010
|
+
AND c.attrs->>'task_ver' IN ('1', '2', '3')
|
|
1011
|
+
AND ((c.attrs->>'input_contract' = 'episode-day-bounds-v1'
|
|
1012
|
+
AND c.attrs->>'task' IN ('working.local.work_category', 'working.local.writing_activity')
|
|
1013
|
+
AND c.attrs->>'unit' = 'episode_day')
|
|
1014
|
+
OR (c.attrs->>'input_contract' = 'codex-import-origin-v1'
|
|
1015
|
+
AND ((c.attrs->>'task' = 'working.local.work_category' AND c.attrs->>'unit' = 'episode_day')
|
|
1016
|
+
OR (c.attrs->>'task' = 'working.local.writing_activity' AND c.attrs->>'unit' = 'episode_day')
|
|
1017
|
+
OR (c.attrs->>'task' = 'working.local.steer_intent' AND c.attrs->>'unit' = 'steer')
|
|
1018
|
+
OR (c.attrs->>'task' = 'working.local.soft_outcome' AND c.attrs->>'unit' = 'episode'))))
|
|
1019
|
+
AND NULLIF(c.attrs->>'unit_id', '') IS NOT NULL
|
|
1020
|
+
AND c.attrs->>'input_hash' ~ '^[0-9a-f]{64}$'
|
|
1021
|
+
AND c.attrs->>'evidence_sha256' ~ '^[0-9a-f]{64}$'
|
|
1022
|
+
AND ((c.attrs->>'state' = 'unavailable' AND c.attrs->>'reason' = 'source_unavailable')
|
|
1023
|
+
OR (c.attrs->>'state' = 'available' AND c.attrs->>'reason' = 'audited_restore'))
|
|
1024
|
+
ORDER BY c.attrs->>'task', c.attrs->>'task_ver', c.attrs->>'unit',
|
|
1025
|
+
c.attrs->>'unit_id', c.attrs->>'input_hash', c.ts DESC,
|
|
1026
|
+
(c.attrs->>'state' = 'unavailable') DESC, c.event_id DESC;
|
|
1027
|
+
|
|
1028
|
+
-- Public classifications follow each unit's latest decision, including an unknown result.
|
|
1029
|
+
-- Reference-model labels remain in their separate private projections above.
|
|
1030
|
+
CREATE OR REPLACE VIEW agents.decision_label_latest AS
|
|
1031
|
+
WITH latest AS (
|
|
1032
|
+
SELECT DISTINCT ON (attrs->>'task', attrs->>'unit', attrs->>'unit_id')
|
|
1033
|
+
substring(attrs->>'task' from 15) AS task,
|
|
1034
|
+
attrs->>'unit' AS unit,
|
|
1035
|
+
attrs->>'unit_id' AS unit_id,
|
|
1036
|
+
ts,
|
|
1037
|
+
attrs->>'label' AS label,
|
|
1038
|
+
(attrs->>'labelled_at')::timestamptz AS labelled_at,
|
|
1039
|
+
(attrs->>'task_ver')::int AS task_ver,
|
|
1040
|
+
event_id, attrs->>'input_hash' AS input_hash
|
|
1041
|
+
FROM agents.activity_event
|
|
1042
|
+
WHERE kind = 'label'
|
|
1043
|
+
AND attrs->>'label_source' = 'local_decision'
|
|
1044
|
+
AND NULLIF(attrs->>'unit_id', '') IS NOT NULL
|
|
1045
|
+
AND ((attrs->>'task' = 'working.local.work_category'
|
|
1046
|
+
AND attrs->>'unit' IN ('episode_day', 'aw_block'))
|
|
1047
|
+
OR (attrs->>'task' = 'working.local.steer_intent' AND attrs->>'unit' = 'steer')
|
|
1048
|
+
OR (attrs->>'task' = 'working.local.writing_activity' AND attrs->>'unit' = 'episode_day')
|
|
1049
|
+
OR (attrs->>'task' = 'working.local.soft_outcome' AND attrs->>'unit' = 'episode'))
|
|
1050
|
+
ORDER BY attrs->>'task', attrs->>'unit', attrs->>'unit_id',
|
|
1051
|
+
(attrs->>'labelled_at')::timestamptz DESC NULLS LAST,
|
|
1052
|
+
(attrs->>'task_ver')::int DESC NULLS LAST, event_id DESC
|
|
1053
|
+
)
|
|
1054
|
+
SELECT l.task, l.unit, l.unit_id, l.ts, l.label, l.labelled_at, l.task_ver, l.event_id
|
|
1055
|
+
FROM latest l
|
|
1056
|
+
LEFT JOIN agents.episode e ON l.task = 'soft_outcome' AND e.episode_id::text = l.unit_id
|
|
1057
|
+
WHERE (l.task <> 'soft_outcome' OR e.outcome = 'no_code')
|
|
1058
|
+
AND NOT EXISTS (
|
|
1059
|
+
SELECT 1 FROM agents.decision_input_status_latest s
|
|
1060
|
+
WHERE s.task = l.task AND s.task_ver = l.task_ver AND s.unit = l.unit
|
|
1061
|
+
AND s.unit_id = l.unit_id AND s.input_state = 'unavailable'
|
|
1062
|
+
AND (s.input_hash = l.input_hash OR l.input_hash IS NULL OR l.input_hash !~ '^[0-9a-f]{64}$')
|
|
1063
|
+
);
|
|
1064
|
+
|
|
1065
|
+
-- Private acceptance selector; a Writing-capable taxonomy is not active by default.
|
|
1066
|
+
-- Created only when missing: an operator who switched it keeps the choice on the next apply.
|
|
1067
|
+
DO $$
|
|
1068
|
+
BEGIN
|
|
1069
|
+
IF to_regclass('agents.work_taxonomy_mode') IS NULL THEN
|
|
1070
|
+
CREATE VIEW agents.work_taxonomy_mode AS SELECT 2::integer AS version;
|
|
1071
|
+
END IF;
|
|
1072
|
+
END $$;
|
|
1073
|
+
|
|
1074
|
+
-- Keep each taxonomy's latest result before selecting the accepted version.
|
|
1075
|
+
-- A late legacy label cannot discard a Writing-capable result from this helper.
|
|
1076
|
+
CREATE OR REPLACE VIEW agents.decision_work_latest_by_taxonomy AS
|
|
1077
|
+
WITH latest AS (
|
|
1078
|
+
SELECT DISTINCT ON ((attrs->>'task_ver')::int, attrs->>'unit', attrs->>'unit_id')
|
|
1079
|
+
(attrs->>'task_ver')::int AS task_ver,
|
|
1080
|
+
attrs->>'unit' AS unit, attrs->>'unit_id' AS unit_id,
|
|
1081
|
+
ts, attrs->>'label' AS label,
|
|
1082
|
+
(attrs->>'labelled_at')::timestamptz AS labelled_at, event_id,
|
|
1083
|
+
attrs->>'input_hash' AS input_hash
|
|
1084
|
+
FROM agents.activity_event
|
|
1085
|
+
WHERE kind = 'label' AND attrs->>'label_source' = 'local_decision'
|
|
1086
|
+
AND attrs->>'task' = 'working.local.work_category'
|
|
1087
|
+
AND attrs->>'unit' IN ('episode_day', 'aw_block')
|
|
1088
|
+
AND NULLIF(attrs->>'unit_id', '') IS NOT NULL
|
|
1089
|
+
ORDER BY (attrs->>'task_ver')::int, attrs->>'unit', attrs->>'unit_id',
|
|
1090
|
+
(attrs->>'labelled_at')::timestamptz DESC NULLS LAST, event_id DESC
|
|
1091
|
+
)
|
|
1092
|
+
SELECT l.task_ver, l.unit, l.unit_id, l.ts, l.label, l.labelled_at, l.event_id
|
|
1093
|
+
FROM latest l
|
|
1094
|
+
WHERE NOT EXISTS (
|
|
1095
|
+
SELECT 1 FROM agents.decision_input_status_latest s
|
|
1096
|
+
WHERE s.task = 'work_category' AND s.task_ver = l.task_ver AND s.unit = l.unit
|
|
1097
|
+
AND s.unit_id = l.unit_id AND s.input_state = 'unavailable'
|
|
1098
|
+
AND (s.input_hash = l.input_hash OR l.input_hash IS NULL OR l.input_hash !~ '^[0-9a-f]{64}$')
|
|
1099
|
+
);
|
|
1100
|
+
|
|
1101
|
+
-- Private acceptance selector; stricter outcome evidence is not active by default.
|
|
1102
|
+
-- Created only when missing: an operator who switched it keeps the choice on the next apply.
|
|
1103
|
+
DO $$
|
|
1104
|
+
BEGIN
|
|
1105
|
+
IF to_regclass('agents.outcome_evidence_mode') IS NULL THEN
|
|
1106
|
+
CREATE VIEW agents.outcome_evidence_mode AS SELECT 2::integer AS version;
|
|
1107
|
+
END IF;
|
|
1108
|
+
END $$;
|
|
1109
|
+
|
|
1110
|
+
-- Select the evidence contract before latest labels can discard accepted results.
|
|
1111
|
+
CREATE OR REPLACE VIEW agents.decision_outcome_latest_by_contract AS
|
|
1112
|
+
WITH latest AS (
|
|
1113
|
+
SELECT DISTINCT ON ((attrs->>'task_ver')::int, attrs->>'unit_id')
|
|
1114
|
+
(attrs->>'task_ver')::int AS task_ver, attrs->>'unit_id' AS unit_id,
|
|
1115
|
+
ts, attrs->>'label' AS label,
|
|
1116
|
+
(attrs->>'labelled_at')::timestamptz AS labelled_at, event_id,
|
|
1117
|
+
attrs->>'input_hash' AS input_hash
|
|
1118
|
+
FROM agents.activity_event
|
|
1119
|
+
WHERE kind = 'label' AND attrs->>'label_source' = 'local_decision'
|
|
1120
|
+
AND attrs->>'task' = 'working.local.soft_outcome' AND attrs->>'unit' = 'episode'
|
|
1121
|
+
AND NULLIF(attrs->>'unit_id', '') IS NOT NULL
|
|
1122
|
+
ORDER BY (attrs->>'task_ver')::int, attrs->>'unit_id',
|
|
1123
|
+
(attrs->>'labelled_at')::timestamptz DESC NULLS LAST, event_id DESC
|
|
1124
|
+
)
|
|
1125
|
+
SELECT l.task_ver, l.unit_id, l.ts, l.label, l.labelled_at, l.event_id
|
|
1126
|
+
FROM latest l
|
|
1127
|
+
WHERE NOT EXISTS (
|
|
1128
|
+
SELECT 1 FROM agents.decision_input_status_latest s
|
|
1129
|
+
WHERE s.task = 'soft_outcome' AND s.task_ver = l.task_ver AND s.unit = 'episode'
|
|
1130
|
+
AND s.unit_id = l.unit_id AND s.input_state = 'unavailable'
|
|
1131
|
+
AND (s.input_hash = l.input_hash OR l.input_hash IS NULL OR l.input_hash !~ '^[0-9a-f]{64}$')
|
|
1132
|
+
);
|
|
1133
|
+
|
|
1134
|
+
-- Public outcome details use the same counted, settled cohort and first week as their Git parent.
|
|
1135
|
+
-- The private latest-label helper above retains raw no-code decisions for other consumers.
|
|
1136
|
+
CREATE OR REPLACE VIEW agents.decision_label_weekly AS
|
|
1137
|
+
WITH accepted_work AS (
|
|
1138
|
+
SELECT min(version) AS version FROM agents.work_taxonomy_mode
|
|
1139
|
+
HAVING count(*) = 1 AND min(version) IN (2, 3)
|
|
1140
|
+
), accepted_outcome AS (
|
|
1141
|
+
SELECT min(version) AS version FROM agents.outcome_evidence_mode
|
|
1142
|
+
HAVING count(*) = 1 AND min(version) IN (2, 3)
|
|
1143
|
+
), labels AS (
|
|
1144
|
+
SELECT task, ts, label, unit_id FROM agents.decision_label_latest WHERE task NOT IN ('work_category', 'soft_outcome')
|
|
1145
|
+
UNION ALL
|
|
1146
|
+
SELECT CASE m.version WHEN 2 THEN 'work_category' WHEN 3 THEN 'work_category@3' END,
|
|
1147
|
+
w.ts, w.label, w.unit_id
|
|
1148
|
+
FROM agents.decision_work_latest_by_taxonomy w CROSS JOIN accepted_work m
|
|
1149
|
+
WHERE w.task_ver = m.version
|
|
1150
|
+
UNION ALL
|
|
1151
|
+
SELECT 'soft_outcome', o.ts, o.label, o.unit_id
|
|
1152
|
+
FROM agents.decision_outcome_latest_by_contract o CROSS JOIN accepted_outcome m
|
|
1153
|
+
WHERE o.task_ver = m.version
|
|
1154
|
+
AND (m.version = 2 OR (o.labelled_at IS NOT NULL AND EXISTS (
|
|
1155
|
+
SELECT 1 FROM agents.episode e
|
|
1156
|
+
WHERE e.episode_id::text = o.unit_id AND o.labelled_at >= e.last_ts
|
|
1157
|
+
)))
|
|
1158
|
+
)
|
|
1159
|
+
SELECT l.task,
|
|
1160
|
+
date_trunc('week', (CASE WHEN l.task = 'soft_outcome' THEN c.first_ts ELSE l.ts END)
|
|
1161
|
+
AT TIME ZONE '@AUT_TZ@')::date AS week,
|
|
1162
|
+
l.label, count(*)::bigint AS units
|
|
1163
|
+
FROM labels l
|
|
1164
|
+
LEFT JOIN agents.episode_counted c ON l.task = 'soft_outcome' AND c.episode_id::text = l.unit_id
|
|
1165
|
+
WHERE l.task <> 'soft_outcome'
|
|
1166
|
+
OR (c.outcome = 'no_code' AND c.last_ts + interval '14 days' <= now())
|
|
1167
|
+
GROUP BY 1, 2, 3;
|
|
1168
|
+
|
|
1169
|
+
-- Git evidence stays separate from assessed outcomes for no-code episodes.
|
|
1170
|
+
CREATE OR REPLACE VIEW agents.git_outcomes_weekly AS
|
|
1171
|
+
SELECT week, count(*)::int AS episodes,
|
|
1172
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'shipped')::int AS shipped,
|
|
1173
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'reworked')::int AS reworked,
|
|
1174
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'ruled_out')::int AS ruled_out,
|
|
1175
|
+
count(*) FILTER (WHERE NOT unsettled AND outcome = 'no_code')::int AS no_code,
|
|
1176
|
+
count(*) FILTER (WHERE unsettled)::int AS still_open
|
|
1177
|
+
FROM (SELECT date_trunc('week', first_ts AT TIME ZONE '@AUT_TZ@')::date AS week,
|
|
1178
|
+
outcome, outcome IN ('open', 'undecided') OR last_ts + interval '14 days' > now() AS unsettled
|
|
1179
|
+
FROM agents.episode_counted) e
|
|
1180
|
+
GROUP BY 1;
|