<?php
require __DIR__.'/db.php';$check($lane==='C'&&$business==='','Only fixed empty-business nonproduction DM');$table='YDY_AGENT_CONVERSATION_LOG';$marker='CDXAC20261010_'.bin2hex(random_bytes(6));$report=['lane'=>'C','passed'=>false,'business_dml'=>0,'ddl'=>0,'marker'=>$marker];
try{
$report['constraints']=$q('SELECT CONSTRAINT_NAME,CONSTRAINT_TYPE,STATUS,R_CONSTRAINT_NAME,SEARCH_CONDITION FROM USER_CONSTRAINTS WHERE TABLE_NAME=? ORDER BY CONSTRAINT_NAME',[$table]);
$report['foreign_references']=$q("SELECT C.TABLE_NAME,C.CONSTRAINT_NAME FROM USER_CONSTRAINTS C JOIN USER_CONSTRAINTS P ON C.R_CONSTRAINT_NAME=P.CONSTRAINT_NAME WHERE P.TABLE_NAME=? AND C.CONSTRAINT_TYPE='R'",[$table]);
$report['triggers']=$q('SELECT TRIGGER_NAME,STATUS,TRIGGERING_EVENT FROM USER_TRIGGERS WHERE TABLE_NAME=?',[$table]);
$check($report['foreign_references']===[]&&$report['triggers']===[],'No FKs or triggers permitted');foreach($report['constraints'] as $c)$check($c['constraint_type']!=='R','No outbound FK');
$ddl=$db->query("SELECT DBMS_METADATA.GET_DDL('TABLE','YDY_AGENT_CONVERSATION_LOG','ZHCT_APP') FROM DUAL")->fetchColumn();if(is_resource($ddl))$ddl=stream_get_contents($ddl);$save('private-table-ddl.json',['ddl'=>$ddl]);$report['ddl_sha256']=hash('sha256',(string)$ddl);$report['id_identity_proven']=preg_match('/["`]?ID["`]?\s+BIGINT\s+(?:IDENTITY\s*\(|AUTO_INCREMENT\b)/i',(string)$ddl)===1;$check($report['id_identity_proven'],'ID identity must be proven');
$report['collision_count']=(int)$q('SELECT COUNT(*) AS CNT FROM '.$table.' WHERE USER_NAME=? OR QUESTION LIKE ?',['数据库兼容测试 '.$marker,'%'.$marker.'%'])[0]['cnt'];$check($report['collision_count']===0,'Marker absent');
$natural=$q('SELECT * FROM '.$table.' ORDER BY ID');$check(count($natural)<=100,'Natural cap100');foreach($natural as $r)$check(strlen((string)$r['question'])<=262144&&strlen((string)$r['answer'])<=262144,'Natural text cap256KiB');$check(strlen(json_encode($natural))<=2097152,'Natural snapshot cap2MiB');$save('private-natural-before.json',$natural);$report['natural_row_count']=count($natural);$report['natural_sha256']=$hash($natural);
$write=['user_uuid'=>'','user_name'=>'数据库兼容测试 '.$marker,'business'=>'','current_route'=>'/system/agentQuestionRecords','question'=>'数据库兼容测试问题 '.$marker,'answer'=>'数据库兼容测试回答 '.$marker,'suggestions_json'=>null,'result_json'=>null,'matched'=>0,'source'=>'cdx_test','llm_used'=>0,'llm_success'=>0,'llm_model'=>'','llm_elapsed_ms'=>0,'llm_usage_json'=>null,'llm_error'=>'','create_time'=>'2001-01-01 08:00:00'];
$pre=json_decode(file_get_contents(__DIR__.'/private-preflight.json'),true);foreach($pre['columns']as $col){$key=strtolower($col['column_name']);if($key==='id')continue;$check(array_key_exists($key,$write),'All non-ID columns explicitly specified');if($col['nullable']==='N')$check($write[$key]!==null,'Required nonnull');if(is_string($write[$key])&&in_array($col['data_type'],['VARCHAR','CHAR'],true))$check(strlen($write[$key])<=(int)$col['data_length'],'Length legal');}
$report['write_set']=$write;$save('private-fixture-write.json',$write);$report['passed']=true;
}catch(Throwable $e){$save('private-fixture-gate-error.json',['class'=>get_class($e),'message'=>$e->getMessage()]);$report['failure']=['class'=>get_class($e),'reason_sha256'=>hash('sha256',$e->getMessage())];}
$save('private-fixture-gate.json',$report);echo json_encode($report,JSON_UNESCAPED_UNICODE),"\n";exit($report['passed']?0:2);
