Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions docs/analyst/explore-budget-decision.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
-- Frozen decision readout for explore_budget_v1. Read-only, aggregate output.
-- Run from the repository root: psql "$ANALYST_DATABASE_URL" -X -v ON_ERROR_STOP=1 -f docs/analyst/explore-budget-decision.sql
-- Defaults to the November 20 decision cutoff and the tracked file of restored IDs.
-- Override as_of (UTC) and restored_ids when the psql client runs on another host.
-- This example sends both parameters and SQL through stdin, so it also works
-- with an established read-only psql wrapper that accepts stdin only:
--
-- python3 - <<'PY' | psql "$ANALYST_DATABASE_URL" -X -v ON_ERROR_STOP=1
-- from pathlib import Path
-- ids = [int(line) for line in Path(
-- 'docs/analyst/data/2026-10-31-dedup-restored-meme-ids.txt'
-- ).read_text().splitlines() if line.strip()]
-- print("\\set as_of '2026-11-20 23:59:59'")
-- print("\\set restored_ids '{" + ','.join(map(str, ids)) + "}'")
-- print(Path('docs/analyst/explore-budget-decision.sql').read_text())
-- PY
--
-- Primary excludes sends of the restored memes. all_sends retains them for the
-- prespecified sensitivity analysis. Both exclude broadcasts and automated users.
-- Each scenario includes users with at least one qualifying feed send. Session
-- boundaries are computed after that scenario's exclusions, using a >30m gap.
-- Likes and reactions must have occurred before as_of. Observation time starts
-- at first assignment, with the original one-day minimum exposure denominator.
-- User roles, meme/reaction records and exclusion membership can change later,
-- so a historical cutoff is a reconstruction from the current stored records.
-- Protocol: experiments/2026-10-30-explore-budget.md.
\if :{?as_of}
\else
\set as_of '2026-11-20 23:59:59'
\endif
\if :{?restored_ids}
\else
\set restored_ids `python3 -c "from pathlib import Path; ids=Path('docs/analyst/data/2026-10-31-dedup-restored-meme-ids.txt').read_text().split(); assert len(set(ids)) == len(ids) == 7762; print('{'+','.join(str(int(x)) for x in ids)+'}')"`
\endif
WITH params AS (
SELECT
:'as_of'::timestamp AS cutoff,
:'restored_ids'::int[] AS restored_ids
),
assigned AS MATERIALIZED (
SELECT
ea.user_id,
ea.assigned_at,
ea.variant,
CASE WHEN mu.segment IN ('moderator', 'admin') THEN 'mod' ELSE 'user' END AS seg
FROM experiment_assignment ea
JOIN v_metric_user mu USING (user_id)
CROSS JOIN params
WHERE ea.experiment_id = 'explore_budget_v1'
AND mu.segment <> 'excluded'
AND ea.assigned_at < params.cutoff
),
sends AS MATERIALIZED (
SELECT
a.*,
r.sent_at,
r.reacted_at,
r.meme_id,
r.reaction_id,
r.recommended_by,
r.recommended_by LIKE 'broadcast%' AS is_push,
r.meme_id = ANY(params.restored_ids) AS restored
FROM assigned a
JOIN user_meme_reaction r
ON r.user_id = a.user_id
AND r.sent_at >= a.assigned_at
CROSS JOIN params
WHERE r.sent_at < params.cutoff
),
feed AS (
SELECT
scenario,
variant,
seg,
user_id,
assigned_at,
sent_at,
reaction_id,
reacted_at,
recommended_by,
restored,
COALESCE(
sent_at - LAG(sent_at) OVER (
PARTITION BY scenario, user_id ORDER BY sent_at, meme_id
) > INTERVAL '30 minutes',
TRUE
) AS session_start
FROM sends
CROSS JOIN (VALUES ('primary'), ('all_sends')) scenarios(scenario)
WHERE NOT is_push
AND (scenario = 'all_sends' OR NOT restored)
),
per_user AS (
SELECT
scenario,
variant,
seg,
user_id,
COUNT(*) AS feed_sends,
COUNT(*) FILTER (WHERE session_start) AS sessions,
COUNT(*) FILTER (
WHERE reaction_id = 1 AND reacted_at < (SELECT cutoff FROM params)
) AS likes,
COUNT(*) FILTER (
WHERE reaction_id IS NOT NULL AND reacted_at < (SELECT cutoff FROM params)
) AS reactions,
COUNT(*) FILTER (WHERE recommended_by = 'low_sent_pool') AS low_sent,
COUNT(*) FILTER (WHERE recommended_by = 'fresh_viral') AS fresh_viral,
COUNT(*) FILTER (WHERE recommended_by = 'last_resort') AS last_resort,
COUNT(*) FILTER (WHERE recommended_by = 'low_sent_pool' AND restored) AS restored_low,
GREATEST(
EXTRACT(EPOCH FROM (SELECT cutoff FROM params) - MIN(assigned_at)) / 604800.0,
1.0 / 7
) AS weeks
FROM feed
GROUP BY scenario, variant, seg, user_id
)
SELECT
scenario,
seg,
variant,
COUNT(*) AS n,
SUM(feed_sends) AS sends,
SUM(sessions) AS sessions,
AVG(likes / weeks) AS likes_user_wk,
AVG(feed_sends::numeric / sessions) AS memes_session,
AVG(likes::numeric / NULLIF(reactions, 0)) AS like_rate,
SUM(low_sent)::float / SUM(feed_sends) AS low_sent_share,
SUM(fresh_viral)::float / SUM(feed_sends) AS fresh_share,
SUM(last_resort) AS last_resort_sends,
SUM(restored_low) AS restored_low_sent,
SUM(low_sent) AS low_sent_sends
FROM per_user
GROUP BY scenario, seg, variant
ORDER BY scenario, seg, variant;
57 changes: 44 additions & 13 deletions docs/analyst/phash-shadow.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,41 @@
--
-- meme.image_hash = 64-bit dHash (images: uploaded bytes; video/animation:
-- Telegram thumbnail). v1 matches EXACT hash only; nothing is merged yet.
-- Live = status IN ('ok', 'published').
-- Live = status IN ('ok', 'published'). Usable hashes exclude NULL, 0 and -1.
-- All sections share one current, read-only snapshot. Current status/hash/file ID
-- can differ from their value at ingestion; this diagnostic cannot replay the
-- historical matcher or estimate precision across every past upload.
BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY;
SET LOCAL statement_timeout = '30s';
SELECT transaction_timestamp() AS checked_at;

\echo === Hash coverage (live memes) ===
SELECT type,
COUNT(*) AS live,
COUNT(image_hash) AS hashed,
ROUND(100.0 * COUNT(image_hash) / NULLIF(COUNT(*), 0), 1) AS hashed_pct
COUNT(*) FILTER (WHERE image_hash NOT IN (0, -1)) AS usable_hashes,
COUNT(*) FILTER (WHERE image_hash IN (0, -1)) AS degenerate_hashes,
COUNT(*) FILTER (WHERE image_hash IS NULL) AS unhashed,
ROUND(100.0 * COUNT(*) FILTER (WHERE image_hash NOT IN (0, -1))
/ NULLIF(COUNT(*), 0), 1) AS usable_hash_pct
FROM meme
WHERE status IN ('ok', 'published')
GROUP BY type
ORDER BY live DESC;

\echo === Exact-hash collision groups among live memes, by type ===
-- would_be_merges = memes beyond the first in each group.
-- extra_candidates counts rows beyond the first, before file-ID and time checks.
SELECT types,
COUNT(*) AS groups,
SUM(n) AS memes_in_groups,
SUM(n - 1) AS would_be_merges,
SUM(n - 1) AS extra_candidates,
COUNT(*) FILTER (WHERE file_ids < n) AS groups_sharing_file_id
FROM (
SELECT image_hash,
COUNT(*) AS n,
COUNT(DISTINCT telegram_file_id) AS file_ids,
STRING_AGG(DISTINCT type, '+' ORDER BY type) AS types
FROM meme
WHERE status IN ('ok', 'published') AND image_hash IS NOT NULL
WHERE status IN ('ok', 'published') AND image_hash NOT IN (0, -1)
GROUP BY image_hash
HAVING COUNT(*) > 1
) g
Expand All @@ -51,16 +60,38 @@ SELECT d.type,
COUNT(*) FILTER (WHERE d.image_hash = o.image_hash) AS same_hash
FROM meme d
JOIN meme o ON o.id = d.duplicate_of
WHERE d.image_hash IS NOT NULL AND o.image_hash IS NOT NULL
WHERE d.image_hash NOT IN (0, -1) AND o.image_hash NOT IN (0, -1)
GROUP BY d.type;

\echo === Sample same-hash live pairs for manual precision check (GET /admin/memes/{id}) ===
\echo === Deterministic incoming-match sample for manual precision check ===
-- Sample surviving live incoming memes created since shadow deployment. Choose
-- one candidate per incoming meme, with the runtime priority (published, then ID).
-- The creation-time restriction keeps only candidates that could already exist.
-- Current publication status and hash availability still limit historical support.
-- Each incoming event receives equal sampling weight, regardless of how many
-- matching candidates its hash has. The fixed seed makes the current snapshot
-- reproducible. Save IDs and media privately before labeling; changing status,
-- hashes or inventory can change the sample on a later run.
SELECT a.id AS meme_id, b.id AS candidate_id, a.type, b.type AS candidate_type,
a.status, b.status AS candidate_status, a.image_hash,
(a.duplicate_of = b.id OR b.duplicate_of = a.id) AS ocr_linked
a.created_at AS incoming_created_at, b.created_at AS candidate_created_at,
COALESCE(a.duplicate_of = b.id OR b.duplicate_of = a.id, FALSE) AS ocr_linked
FROM meme a
JOIN meme b ON b.image_hash = a.image_hash AND b.id < a.id
WHERE a.status IN ('ok', 'published') AND b.status IN ('ok', 'published')
AND a.telegram_file_id IS DISTINCT FROM b.telegram_file_id
ORDER BY random()
JOIN LATERAL (
SELECT candidate.id, candidate.type, candidate.status,
candidate.created_at, candidate.duplicate_of
FROM meme candidate
WHERE candidate.image_hash = a.image_hash
AND candidate.id != a.id
AND candidate.status IN ('ok', 'published')
AND candidate.telegram_file_id IS DISTINCT FROM a.telegram_file_id
AND (candidate.created_at, candidate.id) < (a.created_at, a.id)
ORDER BY CASE WHEN candidate.status = 'published' THEN 0 ELSE 1 END, candidate.id
LIMIT 1
) b ON TRUE
WHERE a.status IN ('ok', 'published')
AND a.created_at >= '2026-10-31'
AND a.image_hash NOT IN (0, -1)
ORDER BY MD5('ff-phash-review-20261122:' || a.id::text), a.id
LIMIT 50;
COMMIT;
102 changes: 102 additions & 0 deletions docs/analyst/readouts/2026-11-20-explore-budget.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"as_of_utc": "2026-11-20 23:59:59",
"restored_ids_count": 7762,
"rows": [
{
"scenario": "all_sends",
"seg": "mod",
"variant": "control",
"n": 86,
"sends": 84413,
"sessions": 1790,
"likes_user_wk": 67.04118,
"memes_session": 37.22828,
"restored_low_sent": 37943,
"low_sent_sends": 68111
},
{
"scenario": "all_sends",
"seg": "mod",
"variant": "treatment",
"n": 92,
"sends": 95855,
"sessions": 2531,
"likes_user_wk": 108.31965,
"memes_session": 33.90356,
"restored_low_sent": 3789,
"low_sent_sends": 53578
},
{
"scenario": "all_sends",
"seg": "user",
"variant": "control",
"n": 177,
"sends": 97229,
"sessions": 2341,
"likes_user_wk": 93.03081,
"memes_session": 30.94795,
"restored_low_sent": 0,
"low_sent_sends": 0
},
{
"scenario": "all_sends",
"seg": "user",
"variant": "treatment",
"n": 189,
"sends": 78880,
"sessions": 2419,
"likes_user_wk": 66.35057,
"memes_session": 27.02767,
"restored_low_sent": 0,
"low_sent_sends": 0
},
{
"scenario": "primary",
"seg": "mod",
"variant": "control",
"n": 84,
"sends": 42646,
"sessions": 1653,
"likes_user_wk": 41.32312,
"memes_session": 20.83254,
"restored_low_sent": 0,
"low_sent_sends": 30168
},
{
"scenario": "primary",
"seg": "mod",
"variant": "treatment",
"n": 92,
"sends": 81260,
"sessions": 2467,
"likes_user_wk": 92.30266,
"memes_session": 30.52127,
"restored_low_sent": 0,
"low_sent_sends": 49789
},
{
"scenario": "primary",
"seg": "user",
"variant": "control",
"n": 175,
"sends": 73815,
"sessions": 2267,
"likes_user_wk": 75.09695,
"memes_session": 24.25865,
"restored_low_sent": 0,
"low_sent_sends": 0
},
{
"scenario": "primary",
"seg": "user",
"variant": "treatment",
"n": 186,
"sends": 58022,
"sessions": 2357,
"likes_user_wk": 51.12947,
"memes_session": 22.53491,
"restored_low_sent": 0,
"low_sent_sends": 0
}
]
}
Loading