#!/usr/bin/env python3
"""Two conversation reads, real B history and one explicitly authorized C fixture."""
import hashlib
import json
from pathlib import Path
import subprocess
import integrate_20261010 as tool

BASE=tool.STORE+'parallel-store-agent-conversation-readonly-20261010/'

def verify():
    m=tool.load(BASE+'manifest.json')
    private=Path(m['private_terminal_evidence']['path'])
    assert str(private)=='/private/tmp/cdx-conversation-private.kLIWZ0'
    assert private.is_dir() and not private.is_symlink() and private.stat().st_mode & 0o777==0o700
    for name,record in m['private_terminal_evidence']['files'].items():
        p=private/name
        assert p.is_file() and not p.is_symlink() and p.stat().st_mode & 0o777==0o600
        assert hashlib.sha256(p.read_bytes()).hexdigest()==record['sha256']
    r=subprocess.run(['node',str(tool.ROOT/BASE/'compare-private.js'),str(private)],check=True,capture_output=True,text=True)
    r=json.loads(r.stdout)
    assert r['passed'] and r['actual_http']==13 and r['CLI_assertions']==47 and r['CLI_failed']==4
    assert len(r['checks'])==38 and all(r['checks'].values())
    assert len(r['mutation_checks'])==68 and all(r['mutation_checks'].values())
    assert m['unique_operations']==2 and m['fixture_PDO_insert']==1 and m['business_API_DML']==0
    assert m['fixture_update_delete']==m['DDL']==m['actor_mutation']==0
    assert m['C_original66_full_columns_per_PK_hash_unchanged'] and m['retained_own_fixture']['id']==68
    assert hashlib.sha256((tool.ROOT.parent/'zhctproject/store/application/p/logic/AgentConversationLog.php').read_bytes()).hexdigest()==m['logic_candidate_SHA256']
    resource=tool.load(BASE+'resource-cleanup.json')
    assert resource['B']['worker_stopped'] and resource['B']['port_18321_closed'] and resource['B']['runtime_removed']
    assert resource['C']['worker_stopped'] and resource['C']['port_18322_closed'] and resource['C']['runtime_removed']
    assert resource['token_config_cache_source_package_original_auth_CLI_reports_removed']
    assert resource['C_natural_nonconsumed_LLM_original_text_removed'] and resource['db_cleanup_writes']==0
    assert resource['retained_test_data']['id']==68 and resource['retained_test_data']['rows']==1

if __name__=='__main__':
    audit=Path(__file__).with_name('integration-20261010-batch39.json')
    assert not audit.exists(),'Preserve original transition audit'
    items=[dict(repo='store',method='POST',path='/p/agentConversationLog/'+action,module='助手问答记录读取',terminal='current-PC',
        role='normal-active-type0-private-auth-original-business',risk='readonly-plus-one-approved-fixture-preparation',
        scope='store-primary-business-empty-B42-history-C-one-owned-row-natural66-excluded-PK-full-hash-stable',
        evidence=BASE+'REPORT.md',request_type='current-PC-form',
        parity='partial-evidence-complete-eight-field-native-per-lane-source-B-old-wire-C-only-seconds-fix',
        cleanup='C-marked-fixture68-retained-natural-source-and-actor-stable-workers-credentials-released-private-raw-preserved',
        boundary_empty_min_max='partial-evidence-B-nonempty-two-pages-detail-C-natural-empty-and-owned-positive-missing-id-keyword-miss')
        for action in ['lists','detail']]
    tool.main(items,audit,verify)
