#!/usr/bin/env python3
"""CHECK-sol-triage-seed-2026-09

Verifies the triage classification artifact for the 2026-09-26 inbox seed.
Exits 0 when the artifact is present, valid JSON, internally consistent
(item_count == len(items) == sum(buckets)), and every item is classified
with a known bucket. Nonzero otherwise.
"""
import json
import os
import sys

ART = '/root/troupe-standup/wizard-work/sol/triage-seed-2026-09.json'
ALLOWED_BUCKETS = {'archive_fix', 'push_branch', 'trial', 'decided_approved'}


def fail(msg):
    print('FAIL: %s' % msg)
    sys.exit(1)


def main():
    if not os.path.exists(ART):
        fail('artifact missing: ' + ART)
    try:
        art = json.load(open(ART))
    except Exception as exc:
        fail('artifact not valid JSON: %s' % exc)

    if not isinstance(art, dict):
        fail('artifact top level is not an object')
    for key in ('seed', 'buckets', 'item_count', 'items'):
        if key not in art:
            fail('missing key: %s' % key)

    buckets = art['buckets']
    items = art['items']
    if not isinstance(buckets, dict):
        fail('buckets is not an object')
    if not isinstance(items, list):
        fail('items is not a list')

    for b, c in buckets.items():
        if b not in ALLOWED_BUCKETS:
            fail('unknown bucket: %s' % b)
        if not isinstance(c, int):
            fail('bucket %s count not int' % b)

    if art.get('item_count') != len(items):
        fail('item_count %r != len(items) %d' % (art.get('item_count'), len(items)))

    actual = {}
    for it in items:
        for key in ('id', 'bucket'):
            if key not in it:
                fail('item missing key %s' % key)
        if it['bucket'] not in ALLOWED_BUCKETS:
            fail('item id=%s unknown bucket %r' % (it.get('id'), it['bucket']))
        actual[it['bucket']] = actual.get(it['bucket'], 0) + 1

    if actual != buckets:
        fail('bucket counts recomputed %r != artifact %r' % (actual, buckets))

    if sum(buckets.values()) != art.get('item_count'):
        fail('sum(buckets) != item_count')

    print('OK: %s buckets=%s items=%d' % (
        os.path.basename(ART), json.dumps(buckets), len(items)))
    sys.exit(0)


if __name__ == '__main__':
    main()
