Prepare descriptive S2 volume-tail diagnostic for lightweight code review

This commit is contained in:
MeltAstra committed 2026-11-18 10:15:29 +08:00
1 parent a5f44f3a5a
commit b97674a1d4
15 files changed
+37045

No files matched your search

@@ -0,0 +1,8 @@
{
"status": "PASS",
"tests": 16,
"scope": "Synthetic arrays only; no scientific target or prediction values",
"log_sha256": "3d12870ac905775ea6a50a861ef2d64e297422975a29ff59c980daf1e8c0343f",
"real_data_read": false,
"unix": 1794968093.7768
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,12 @@
{
"status": "PREPARED_NO_TARGET_ARRAYS_READ",
"parts": 552,
"components": 204,
"source_file_existence_checked": true,
"array_values_read": false,
"source_hashes_rechecked_at_execution": true,
"review_pending": true,
"launched": false,
"preparation_cpu_seconds": 0.8492111539999999,
"unix": 1794968044.5063598
}
@@ -0,0 +1,20 @@
test_certificate_and_nonconverse (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_constant_truth_and_heavy_node (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_disjoint_tails (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_empty_exterior (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_identical_fractional_plateau (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_invalid_mass_and_nan (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_missing_light_review_refuses (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_monotone_tail_invariance (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_near_tail_without_branch_claim (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_nonuniform_and_ID_reorder (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_other_exact_peak (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_query_duplication (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_strict_equality_not_certified (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_wrong_light_review_plan_refuses (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_zero_remainder_boundary (tools.test_s2_tail_diagnostic.TailTests) ... ok
----------------------------------------------------------------------
Ran 15 tests in 0.039s
OK
@@ -0,0 +1,20 @@
test_certificate_and_nonconverse (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_constant_truth_and_heavy_node (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_disjoint_tails (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_empty_exterior (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_identical_fractional_plateau (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_invalid_mass_and_nan (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_missing_light_review_refuses (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_monotone_tail_invariance (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_near_tail_without_branch_claim (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_nonuniform_and_ID_reorder (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_other_exact_peak (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_query_duplication (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_strict_equality_not_certified (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_wrong_light_review_plan_refuses (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_zero_remainder_boundary (tools.test_s2_tail_diagnostic.TailTests) ... ok
----------------------------------------------------------------------
Ran 15 tests in 0.040s
OK
@@ -0,0 +1,21 @@
test_certificate_and_nonconverse (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_constant_truth_and_heavy_node (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_disjoint_tails (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_empty_exterior (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_identical_fractional_plateau (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_invalid_mass_and_nan (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_missing_light_review_refuses (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_monotone_tail_invariance (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_near_tail_without_branch_claim (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_nonuniform_and_ID_reorder (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_other_exact_peak (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_query_duplication (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_strict_JSON_output (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_strict_equality_not_certified (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_wrong_light_review_plan_refuses (tools.test_s2_tail_diagnostic.TailTests) ... ok
test_zero_remainder_boundary (tools.test_s2_tail_diagnostic.TailTests) ... ok
----------------------------------------------------------------------
Ran 16 tests in 0.040s
OK
@@ -0,0 +1,68 @@
{
"parent": "34aedf01d9a53e944aeadd6e1c00a74d2b6eae03",
"files": [
{
"source": "/home/frank14f/optane/MeltMind/artifacts/phase2-current-handoff-2026-11-17.json",
"path": "artifacts/phase2-current-handoff-2026-11-17.json",
"sha256": "0dc6858bae8aa27135f5d01105f3b56d00a782458f8218f8b34244610f37d096",
"bytes": 138772,
"oid": "52c104215c8492b1b41e7db6cd77e7fe318e002c"
},
{
"source": "/home/frank14f/optane/MeltMind/artifacts/phase3-item-c-tail-spec-reception-2026-11-18/receipt.json",
"path": "artifacts/phase3-item-c-tail-spec-reception-2026-11-18/receipt.json",
"sha256": "245d84a628919642ff20019ae3c166b9b4d1651c8940452104aafc320bce8a0c",
"bytes": 1195,
"oid": "b1c5d513f7bf77d50c9caabc91ee10e5f0e3129d"
},
{
"source": "/home/frank14f/optane/MeltMind/artifacts/phase3-item1-monitor-handoff-publication-2026-11-18/file-map.json",
"path": "artifacts/phase3-item1-monitor-handoff-publication-2026-11-18/file-map.json",
"sha256": "a2fb8d5baa1768ef7ea8812d19b5708b72fb06c8b2a3e3c36da579b022ae181c",
"bytes": 3006,
"oid": "a4e9c3e6056b130f12552ecbc36ff4cdd39fb1fa"
},
{
"source": "/home/frank14f/optane/MeltMind/artifacts/phase3-item1-monitor-handoff-publication-2026-11-18/http-readback.json",
"path": "artifacts/phase3-item1-monitor-handoff-publication-2026-11-18/http-readback.json",
"sha256": "f9f0798ac8eccdb28ea54326cefa1cf9a2b669c7a2a844381a9f20b07c2fc486",
"bytes": 808,
"oid": "5632cff54d575482f76883eedf05066caa0dabff"
},
{
"source": "/home/frank14f/optane/MeltMind/artifacts/phase3-item1-monitor-handoff-publication-2026-11-18/publication.json",
"path": "artifacts/phase3-item1-monitor-handoff-publication-2026-11-18/publication.json",
"sha256": "af8f6dadbb4207e08dddef9d95fb8d9607142db31f2799f99c1e877f34b2cbfa",
"bytes": 376,
"oid": "0854d809d4b111f372e0ab929ee1e4346794ddb3"
},
{
"source": "/home/frank14f/optane/MeltMind/docs/collaboration-stage-review-policy.md",
"path": "docs/collaboration-stage-review-policy.md",
"sha256": "78f6fecdd9f608d553632f154125f3631d22308e4494286d241291c231f507f4",
"bytes": 102914,
"oid": "3f770df4654d53c5f1f98039c1bf1e65d239545e"
},
{
"source": "/home/frank14f/optane/MeltMind/docs/phase2-live-handoff-2026-11-17.md",
"path": "docs/phase2-live-handoff-2026-11-17.md",
"sha256": "ca147b82bc35ec17fbc92d038185eb9a00e91227b5127972fef111351d259ce0",
"bytes": 69834,
"oid": "089825d34a2f0e070016071303d1c99b87fdc8a6"
},
{
"source": "/home/frank14f/optane/MeltMind/docs/phase3-item-c-tail-spec-reception-2026-11-18.md",
"path": "docs/phase3-item-c-tail-spec-reception-2026-11-18.md",
"sha256": "969fc718bdbe628d65bf19e5437efdf1469fc896e507d616b87047f59fd6e31a",
"bytes": 5619,
"oid": "239cd8bcc6c45ba1144155384b42ec3ec7a57236"
},
{
"source": "/home/frank14f/optane/MeltMind/references/2026-11-18/phase3-item-c/mold-tail-volume-peak-stability-spec.md",
"path": "references/2026-11-18/phase3-item-c/mold-tail-volume-peak-stability-spec.md",
"sha256": "05344bacd877de87cbde9eb18139ad124e6cd4a15659e3c469b655b3f5a3f521",
"bytes": 8440,
"oid": "45c736e759b460c140c46ef515eb4f5399dd6cd5"
}
]
}
@@ -0,0 +1,31 @@
{
"commit": "a5f44f3a5ad07ca2a0165a49e7c562b492be5b0d",
"status": "PASS",
"checked_files": [
{
"path": "references/2026-11-18/phase3-item-c/mold-tail-volume-peak-stability-spec.md",
"http_status": 200,
"sha256": "05344bacd877de87cbde9eb18139ad124e6cd4a15659e3c469b655b3f5a3f521",
"expected": "05344bacd877de87cbde9eb18139ad124e6cd4a15659e3c469b655b3f5a3f521",
"matched": true,
"bytes": 8440
},
{
"path": "docs/phase3-item-c-tail-spec-reception-2026-11-18.md",
"http_status": 200,
"sha256": "969fc718bdbe628d65bf19e5437efdf1469fc896e507d616b87047f59fd6e31a",
"expected": "969fc718bdbe628d65bf19e5437efdf1469fc896e507d616b87047f59fd6e31a",
"matched": true,
"bytes": 5619
},
{
"path": "artifacts/phase3-item-c-tail-spec-reception-2026-11-18/receipt.json",
"http_status": 200,
"sha256": "245d84a628919642ff20019ae3c166b9b4d1651c8940452104aafc320bce8a0c",
"expected": "245d84a628919642ff20019ae3c166b9b4d1651c8940452104aafc320bce8a0c",
"matched": true,
"bytes": 1195
}
],
"unix": 1794966258.0458703
}
@@ -0,0 +1,11 @@
{
"status": "REMOTE_REF_VERIFIED",
"commit": "a5f44f3a5ad07ca2a0165a49e7c562b492be5b0d",
"parent": "34aedf01d9a53e944aeadd6e1c00a74d2b6eae03",
"branch": "codex/phase2-small-evidence-20261117",
"files": 9,
"bytes": 330964,
"writer_head_unchanged": "95ef2592b91f437f5f3274be8599dc9e5139dba9",
"wall_seconds": 13.134515285491943,
"raw_bulk_not_included": true
}
@@ -0,0 +1,17 @@
# Existing S2 A/H2 tail diagnostic — light review before execution
Claude1794966078.809549/h-c907cdaf authorizes this descriptive-only CPU diagnostic on the already seen552 parts/204 components, under3600 CPU-seconds. It has no gate, does not reverse the S2 decision, starts no item c fit, and changes neither item1 nor its postfit barriers. Mold's full05344bac specification is normative; future c also retains that specification.
Entry `tools/s2_tail_diagnostic.py run artifacts/phase2-h2-s2-tail-diagnostic-2026-11-18`, original reproduction venv, project cwd. `prepare_s2_tail_diagnostic.py` reads metadata only. Before any target computation, MeltAssist performs a light code/spec check. An executor receipt records that actual review, source receipt hash, current plan hash and source map; the entry verifies it. No five-field implementation/proof ceremony or new checkpoint review. The unmodified shared CPU guard is reused, one attempt, CPU24 worker/25 coordinator, no GPU/model loading/inference or labels decoded from archives.
H2 full fields are the canonical frozen S2 manifest (including exact historical S1 fold0). Full A comes from the corresponding frozen H1 `A_seconds` fields already used to form the S2 baseline. Every case verifies both raw-ID arrays and complete node counts, geometry and cached lumped mass equality, original query membership, and exact A/H2 query values against stored S2 values before reading the existing seconds.npy target once. Input hashes are checked at execution. No predictor is run. The old S2 query last-location values are copied unchanged for comparison, separately from new geometry-domain diagnostics.
Each case normalizes time by original t_set, keeps negative predictions, obtains mass w from existing m_i=Σ|K|/4, and computes true/A/H2 fractional last1% volume tails. Raw-ID ordering does not cut a plateau. Stable compensated sums determine plateau mass, residual and total tail mass;10^-12 correction/volume bounds are enforced and recorded. `E_tail=(Miss+False)/2` is computed with the equivalent `1−I/.01` value and floating-point identity discrepancy also retained; min intersection ensures identical fractional tails have exactly zero directional loss. Constant truth stays in the roster with its marker. No query multiplicity enters volume measure.
Peak certificates and categories follow the same full/query domains, ε, r=.10Rg, strictγ>2ε, 64u near-boundary rule, exact raw-ID peak ties, peak-set/tail-support distances and true-time drop. Positive-infiniteγ from an empty exterior is stored as null plus explicit gamma_positive_infinity/trivial_radius=true so JSON contains no invalid infinity. The strict certificate and its boundary-robust interpretation are both kept. Failure of a certificate does not imply a branch error; no branch is identified from Euclidean distance.
Outputs include552 per-part JSON records, both arm tail membership threshold/volume/tie summaries, Miss/False/E_tail, query/full diagnostics, original query last scores, negative counts and target-access log. Summaries give component-equal paired H2−A differences and2000 bootstrap intervals/seed2026111408 for tail, original point and continuous diagnostics, shared draws on identical component support. Undefined values exclude the whole affected component for that metric with denominators reported; no missing component becomes a zero. Categories and certificates are reported as part counts with denominators; categories additionally have component-equal fractions. These are exploratory descriptions, not a decision rule or proof of mechanism. Full predictions are reused, not copied or rewritten.
Budgets:3600 total CPU-s, including180 setup reserve (covers metadata preparation and synthetic tests), a3300-CPU-s serial worker and actual coordinator/reaped-child accounting. The worker has7200 wall-seconds and4GiB RSS; guard output is capped at1GiB. Per-part diagnostic output is small; no full-field copies or pairwise distance matrices. A numerical/input/hash error stops the batch and preserves partial records; no silent retry or fabricated552 table. Review and publication overhead are disclosed separately from the scientific worker; they run no scientific computation.
Synthetic acceptance is required before review: fractional identical/disjoint tails, constant truth, one heavy node/nonuniform weights, ID permutation, monotone transformation, duplicate query records, zero-remainder boundary, strict certificate equality, sufficient-certificate/nonconverse/empty exterior, exact other-peak and near-tail categories, plus missing/mismatched review rejection. Synthetic arrays only; no real target values before the light review. No existing training or postfit source is edited.
+30
View File
@@ -0,0 +1,30 @@
"""Metadata-only preparation for Claude6078; never opens target arrays."""
import time
from pathlib import Path
from s2_tail_diagnostic import BASE,ROOT,read,save,sha
def main():
start=time.process_time()
if ROOT.exists():raise FileExistsError('Existing diagnostic root: inspect before any action')
s=Path('/home/frank14f/MeltMind-runs/phase2-h2-s2-postfit-2026-11-17');c=read(s/'contract.json');p=read(c['pilot_contract']);m=read(s/'predictions-manifest.json')['entries'];a=read(BASE/'artifacts/phase2-b13-e1b-2026-11-17/contract.json')['A_predictions'];oldrows={r['case']:r for r in c['rows']}
if read(s/'complete.json')['status']!='COMPLETE' or len(m)!=552 or len({e['case'] for e in m})!=552 or len({e['component_id'] for e in m})!=204:raise ValueError('Original completed S2 roster')
proof=read(s/'predictions-git-freeze.json')
if proof['files'][str(s/'predictions-manifest.json')]!=sha(s/'predictions-manifest.json'):raise ValueError('Original frozen prediction manifest')
parts=[]
for e in sorted(m,key=lambda e:e['case']):
case=e['case'];cache=p['parts'][case];aa=a[case];g=p['geometry'][case];row=oldrows[case]
if aa['protocol']!='grouped' or aa['pool']!=100 or aa['fold']!=e['fold'] or aa['nodes']!=e['nodes'] or g['nodes']!=e['nodes'] or row['component_id']!=e['component_id']:raise ValueError('A/H2 same domain and role')
sources={name:dict(path=d['path'],sha256=d['sha256']) for name,d in [('A',aa),('H2',e),('geometry',g)]}
for key,name in [('truth','seconds'),('mass','mass'),('raw_ids','raw_ids')]:sources[key]=dict(path=str((BASE/Path(cache['output'])/(name+'.npy')).resolve()),sha256=cache['output_sha256'][name+'.npy'])
for v in sources.values():
if not Path(v['path']).is_file():raise FileNotFoundError(v['path'])
parts.append(dict(case=case,component_id=e['component_id'],fold=e['fold'],nodes=e['nodes'],t_set=cache['t_set'],sources=sources))
spec=BASE/'references/2026-11-18/phase3-item-c/mold-tail-volume-peak-stability-spec.md'
if sha(spec)!='05344bacd877de87cbde9eb18139ad124e6cd4a15659e3c469b655b3f5a3f521':raise ValueError('Spec identity')
names=['s2_tail_diagnostic_math.py','s2_tail_diagnostic.py','prepare_s2_tail_diagnostic.py','test_s2_tail_diagnostic.py','r4bc_guard.py','s2_recovery_supervisor.py','s2_recovery_monitoring.py']
codes={str(BASE/'tools'/n):sha(BASE/'tools'/n) for n in names};codes[str(BASE/'docs/s2-tail-diagnostic-plan-2026-11-18.md')]=sha(BASE/'docs/s2-tail-diagnostic-plan-2026-11-18.md')
ROOT.mkdir();plan=dict(approval='1794966078.809549',spec_path=str(spec),spec_sha256=sha(spec),descriptive_only=True,gate=None,cpu_cap=3600,worker_cpu_cap=3300,setup_reserve=180,worker_cpu=24,coordinator_cpu=25,worker_rss_bytes=4*2**30,worker_wall_seconds=7200,parts=parts,code_sha256=codes,S2_score=dict(path=str(s/'score/result.json'),sha256=sha(s/'score/result.json')),metadata_sources={str(q):sha(q) for q in [s/'contract.json',s/'predictions-manifest.json',s/'predictions-git-freeze.json',s/'complete.json',Path(c['pilot_contract']),BASE/'artifacts/phase2-b13-e1b-2026-11-17/contract.json']},new_fits=0,new_predictions=0,no_retry=True,light_review_before_targets=True,original_S2_decision_unchanged=True)
save(ROOT/'plan.json',plan)
save(ROOT/'preparation.json',dict(status='PREPARED_NO_TARGET_ARRAYS_READ',parts=552,components=204,source_file_existence_checked=True,array_values_read=False,source_hashes_rechecked_at_execution=True,review_pending=True,launched=False,preparation_cpu_seconds=time.process_time()-start,unix=time.time()))
print(dict(root=str(ROOT),plan_sha256=sha(ROOT/'plan.json'),code_files=len(codes),parts=552,components=204,review_pending=True))
if __name__=='__main__':main()
+122
View File
@@ -0,0 +1,122 @@
"""One reviewed descriptive CPU pass over existing S2 predictions; no inference."""
import hashlib,json,math,os,resource,sys,time
from pathlib import Path
import numpy as np
from s2_tail_diagnostic_math import weights,tail_membership,tail_error,peak_diagnostic
BASE=Path('/home/frank14f/optane/MeltMind')
ROOT=BASE/'artifacts/phase2-h2-s2-tail-diagnostic-2026-11-18'
def sha(path):
h=hashlib.sha256()
with Path(path).open('rb') as f:
for b in iter(lambda:f.read(2**20),b''):h.update(b)
return h.hexdigest()
def read(path):return json.loads(Path(path).read_text())
def save(path,data):
p=Path(path);p.parent.mkdir(parents=True,exist_ok=True);tmp=p.with_suffix(p.suffix+'.tmp');tmp.write_text(json.dumps(data,indent=2,allow_nan=False)+'\n');tmp.replace(p)
def checked(path,h):
p=Path(path)
if sha(p)!=h:raise ValueError('Source hash mismatch: '+str(p))
return p
def config(root,review=True):
c=read(root/'plan.json')
if c['approval']!='1794966078.809549' or c['cpu_cap']!=3600 or c['spec_sha256']!='05344bacd877de87cbde9eb18139ad124e6cd4a15659e3c469b655b3f5a3f521':raise PermissionError('Fixed descriptive scope')
for p,h in c['code_sha256'].items():checked(p,h)
checked(c['spec_path'],c['spec_sha256'])
if review:
a=read(root/'review-accepted.json')
if a['verdict']!='PASS' or a['plan_sha256']!=sha(root/'plan.json') or a['code_sha256']!=c['code_sha256']:raise PermissionError('Actual lightweight review required')
checked(a['source_receipt'],a['source_receipt_sha256'])
return c
def field(row,arm,name):
if name in ['E_tail','Miss','False_alarm']:return row['arms'][arm]['tail'][name]
if name=='original_query_last':return row['arms'][arm]['original_query_last']
domain,key=name.split('.',1);return row['arms'][arm][domain][key]
def summarize(rows):
metrics=['E_tail','Miss','False_alarm','original_query_last']+[d+'.'+k for d in ['query','full'] for k in ['peak_distance_over_Rg','d_peakset_over_Rg','epsilon','true_time_drop_at_predicted_peak']]+['full.d_tail_over_Rg']
groups={}
for row in rows:groups.setdefault(row['component_id'],[]).append(row)
result={};all_ids=sorted(groups)
for metric in metrics:
ids=[];values={'A':[],'H2':[]};excluded=[]
for cid in all_ids:
part={arm:[field(r,arm,metric) for r in groups[cid]] for arm in values}
if any(v is None or not math.isfinite(v) for vs in part.values() for v in vs):excluded.append(cid);continue
ids.append(cid)
for arm in values:values[arm].append(float(np.mean(part[arm])))
if not ids:
result[metric]=dict(A=None,H2=None,difference=None,low=None,high=None,components=0,excluded=excluded);continue
a,h=np.array(values['A']),np.array(values['H2']);delta=h-a
rng=np.random.Generator(np.random.PCG64(np.random.SeedSequence(2026111408)));draw=rng.integers(0,len(ids),size=(2000,len(ids)))
boot=delta[draw].mean(1);lo,hi=np.percentile(boot,[2.5,97.5],method='linear')
result[metric]=dict(A=float(a.mean()),H2=float(h.mean()),difference=float(delta.mean()),low=float(lo),high=float(hi),components=len(ids),paired_parts=sum(len(groups[i]) for i in ids),excluded=excluded,undefined_bootstrap=int(np.sum(~np.isfinite(boot))))
diagnostics={}
for domain in ['query','full']:
diagnostics[domain]={}
for arm in ['A','H2']:
rs=[r['arms'][arm][domain] for r in rows];categories=sorted({r['category'] for r in rs})
diagnostics[domain][arm]=dict(parts=len(rs),certified=sum(r['certified'] for r in rs),stable_certificate=sum(r['stable_certificate'] for r in rs),near_boundary=sum(r['near_boundary'] for r in rs),trivial_radius=sum(r['trivial_radius'] for r in rs),constant_truth=sum(r['constant_truth'] for r in rs),categories={k:dict(parts=sum(r['category']==k for r in rs),component_mean_fraction=float(np.mean([np.mean([r['arms'][arm][domain]['category']==k for r in gr]) for gr in groups.values()]))) for k in categories})
ties=dict(truth_threshold_tied_parts=sum(r['truth_tail']['plateau_nodes']>1 for r in rows),arms={a:dict(prediction_threshold_tied_parts=sum(r['arms'][a]['prediction_tail']['plateau_nodes']>1 for r in rows),max_threshold_mass_correction=max(abs(r['arms'][a]['prediction_tail']['mass_correction']) for r in rows)) for a in ['A','H2']})
return dict(descriptive_only=True,gate=None,S2_decision_unchanged=True,parts=len(rows),components=len(groups),metrics=result,diagnostics=diagnostics,bootstrap=dict(replicates=2000,seed=2026111408,joint_indices_when_same_component_support=True,unit='evaluation component, not training randomness'),constant_truth_parts=sum(r['truth_tail']['constant_field'] for r in rows),tie_coverage=ties)
def worker(root):
c=config(root);os.sched_setaffinity(0,[24])
if os.environ.get('CUDA_VISIBLE_DEVICES','')!='':raise PermissionError('CPU only')
out=root/'output';out.mkdir(exist_ok=False);cpu=time.process_time();rows=[]
# Existing single-point scores are retained, not replaced by the new endpoint.
old={r['case']:r for r in read(checked(c['S2_score']['path'],c['S2_score']['sha256']))['rows']}
save(out/'review-consumed.json',dict(plan_sha256=sha(root/'plan.json'),review_acceptance_sha256=sha(root/'review-accepted.json'),before_target_arrays=True,unix=time.time()))
for item in c['parts']:
case=item['case'];source=item['sources']
for rec in source.values():checked(rec['path'],rec['sha256'])
with np.load(source['H2']['path'],allow_pickle=False) as z:
raw=z['raw_ids'].copy();h2=z['seconds'].astype(np.float64);qid=z['query_raw_ids'].copy();query_a=z['A'].copy();query_h=z['pred'].copy()
with np.load(source['A']['path'],allow_pickle=False) as z:
if not np.array_equal(raw,z['raw_ids']):raise ValueError('Full A/H2 raw-ID identity')
a=z['A_seconds'].astype(np.float64)
with np.load(source['geometry']['path'],allow_pickle=False) as z:
if not np.array_equal(raw,z['raw_ids']):raise ValueError('Geometry prediction raw-ID identity')
points=z['points'].copy();mass=z['mass'].copy();rg=float(z['rg'])
cache_raw=np.load(source['raw_ids']['path'],allow_pickle=False);cache_mass=np.load(source['mass']['path'],allow_pickle=False)
if not np.array_equal(raw,cache_raw) or not np.array_equal(mass,cache_mass) or len(raw)!=item['nodes'] or np.any(np.diff(raw)<=0):raise ValueError('Complete retained-tet domain/mass')
ix=np.searchsorted(raw,qid)
if np.any(ix>=len(raw)) or not np.array_equal(raw[ix],qid) or len(np.unique(qid))!=len(qid):raise ValueError('Exact unique query identity')
if not np.array_equal(a[ix],query_a) or not np.array_equal(h2[ix],query_h):raise ValueError('Original S2 A/H2 query values differ from reused full fields')
if not math.isfinite(item['t_set']) or item['t_set']<=0 or not np.isfinite(a).all() or not np.isfinite(h2).all():raise ValueError('Finite prediction / time denominator')
with (out/'target-access.jsonl').open('a') as f:f.write(json.dumps(dict(case=case,path=source['truth']['path'],sha256=source['truth']['sha256'],unix=time.time()))+'\n')
truth=np.load(source['truth']['path'],allow_pickle=False).astype(np.float64)/item['t_set']
if truth.shape!=raw.shape or not np.isfinite(truth).all():raise ValueError('Full target domain finite values')
w=weights(mass);ta,td=tail_membership(truth,w);arms={}
for arm,pred in [('A',a/item['t_set']),('H2',h2/item['t_set'])]:
pa,pd=tail_membership(pred,w)
arms[arm]=dict(tail=tail_error(ta,pa,w),prediction_tail=pd,full=peak_diagnostic(truth,pred,points,raw,rg,w=w,true_tail=ta),query=peak_diagnostic(truth[ix],pred[ix],points[ix],raw[ix],rg),original_query_last=old[case][arm]['last_location']['value'],negative_full_predictions=int(np.sum(pred<0)))
row=dict(case=case,component_id=item['component_id'],fold=item['fold'],nodes=len(raw),query_nodes=len(ix),t_set=item['t_set'],Rg=rg,truth_tail=td,arms=arms)
save(out/'parts'/(case+'.json'),row);rows.append(row);save(out/'progress.json',dict(parts=len(rows),worker_cpu_seconds=time.process_time()-cpu,unix=time.time()))
if len(rows)!=552 or len({r['component_id'] for r in rows})!=204:raise ValueError('Original complete S2 denominator')
summary=summarize(rows);save(out/'result.json',dict(status='COMPLETE',summary=summary,rows=rows,new_fits=0,new_predictions=0,target_arrays_read=552,new_decodes=0,negative_clipping=False,worker_cpu_seconds=time.process_time()-cpu))
def run(root):
import fcntl,psutil
from r4bc_guard import supervise
start=time.process_time();u0=resource.getrusage(resource.RUSAGE_CHILDREN);c=config(root);os.sched_setaffinity(0,[25]);lock=(root/'run.lock').open('a');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB)
if (root/'started.json').exists():raise FileExistsError('One diagnostic attempt; no duplicate or retry')
for p in psutil.process_iter(['pid','cmdline']):
if p.pid!=os.getpid() and any(Path(x).name=='s2_tail_diagnostic.py' for x in p.info['cmdline'] or []):raise RuntimeError('Existing diagnostic')
save(root/'started.json',dict(pid=os.getpid(),unix=time.time(),cpu_cap=3600,setup_reserve=180,descriptive_only=True,review_sha256=sha(root/'review-accepted.json'),cpu_start=dict(self=start,child_user=u0.ru_utime,child_system=u0.ru_stime)))
guard=supervise([sys.executable,str(BASE/'tools/s2_tail_diagnostic.py'),'worker',str(root)],root/'guard',dict(cpu_seconds=3300,wall_seconds=7200,rss_bytes=4*2**30,disk_bytes=2**30))
u=resource.getrusage(resource.RUSAGE_CHILDREN);charge=180+time.process_time()-start+max(guard['cpu_seconds'],u.ru_utime+u.ru_stime-u0.ru_utime-u0.ru_stime)
ok=guard['status']=='PASS' and not guard['monitor_errors'] and charge<=3600
if ok:
result=read(root/'output/result.json');ok=result['status']=='COMPLETE' and result['summary']['parts']==552 and result['summary']['components']==204
save(root/'complete.json',dict(status='COMPLETE' if ok else 'STOPPED',guard=guard,cumulative_cpu_seconds=charge,cpu_cap=3600,new_fits=0,new_predictions=0,descriptive_only=True,no_gate=True,S2_decision_unchanged=True,retry=False,output_sha256=sha(root/'output/result.json') if (root/'output/result.json').exists() else None))
if not ok:raise RuntimeError('Diagnostic guard/coverage failed; preserve receipts')
if __name__=='__main__':
root=Path(sys.argv[2]) if len(sys.argv)>2 else ROOT
if sys.argv[1]=='worker':worker(root)
elif sys.argv[1]=='run':run(root)
else:raise ValueError('worker or run')
+109
View File
@@ -0,0 +1,109 @@
"""05344bac discrete volume tail and same-domain peak certificates; CPU only."""
import math
import numpy as np
ALPHA = .01
RADIUS = .10
MASS_TOL = 1e-12
def weights(mass):
m = np.asarray(mass, np.float64)
if m.ndim != 1 or not len(m) or not np.isfinite(m).all() or np.any(m <= 0):
raise ValueError('Finite positive lumped mass required')
total = math.fsum(m)
if not math.isfinite(total) or total <= 0:
raise ValueError('Invalid volume denominator')
return m / total
def tail_membership(values, w):
s = np.asarray(values, np.float64)
if s.ndim != 1 or s.shape != w.shape or not np.isfinite(s).all():
raise ValueError('Finite full-domain field required')
# Locate a candidate boundary; all final plateau masses use compensated sums.
order = np.argsort(-s, kind='stable')
at = min(int(np.searchsorted(np.cumsum(w[order]), ALPHA)), len(s)-1)
q = float(s[order[at]])
above, tied = s > q, s == q
high, equal = math.fsum(w[above]), math.fsum(w[tied])
remainder = ALPHA-high
fixed = min(max(remainder, 0.), equal)
correction = fixed-remainder
if equal <= 0 or abs(correction) > MASS_TOL:
raise ValueError('Tail boundary exceeds fixed mass correction tolerance')
fraction = fixed/equal
a = above.astype(np.float64)
a[tied] = fraction
actual = math.fsum(w*a)
if abs(actual-ALPHA) > MASS_TOL:
raise ValueError('Tail volume differs from alpha')
return a, dict(threshold=q, plateau_volume=equal, plateau_nodes=int(np.sum(tied)), plateau_fraction=fraction,
mass_correction=correction, tail_volume=actual,
constant_field=bool(np.all(s == s[0])))
def tail_error(a, b, w):
intersection = math.fsum(w*np.minimum(a, b))
miss = math.fsum(w*np.maximum(a-b, 0.))/ALPHA
false = math.fsum(w*np.maximum(b-a, 0.))/ALPHA
# Mean directional loss is numerically robust at identical fractional tails.
error = (miss+false)/2
identity = 1-intersection/ALPHA
if abs(error-identity) > 2*MASS_TOL/ALPHA or abs(miss-false) > 2*MASS_TOL/ALPHA:
raise ValueError('Equal tail mass / intersection identity')
if not 0 <= error <= 1+2*MASS_TOL/ALPHA:
raise ValueError('Tail error outside range')
return dict(E_tail=error, Miss=miss, False_alarm=false, intersection=intersection,
intersection_formula=identity, finite_precision_identity_difference=error-identity)
def peak_diagnostic(truth, pred, points, raw, rg, *, w=None, true_tail=None):
t, p, x = np.asarray(truth, np.float64), np.asarray(pred, np.float64), np.asarray(points, np.float64)
raw = np.asarray(raw)
if t.ndim != 1 or p.shape != t.shape or raw.shape != t.shape or x.shape != (len(t),3) or not len(t):
raise ValueError('Peak domain shape')
if not all(np.isfinite(v).all() for v in [t,p,x]) or not math.isfinite(rg) or rg <= 0:
raise ValueError('Peak finite domain and positive Rg')
# Query duplicate records must not change maxima, epsilon or distances.
ids, first, inverse = np.unique(raw, return_index=True, return_inverse=True)
if len(ids) != len(raw):
if w is not None or true_tail is not None:
raise ValueError('Full volume domain requires unique IDs')
if not np.array_equal(t, t[first][inverse]) or not np.array_equal(p, p[first][inverse]) or not np.array_equal(x, x[first][inverse]):
raise ValueError('Conflicting duplicated query identity')
t,p,x,raw = t[first],p[first],x[first],ids
top, predicted_top = np.flatnonzero(t == t.max()), np.flatnonzero(p == p.max())
y = int(top[np.argmin(raw[top])]); yh = int(predicted_top[np.argmin(raw[predicted_top])])
epsilon = float(np.max(np.abs(p-t)))
distances = np.linalg.norm(x-x[y], axis=1)/rg
exterior = distances > RADIUS
trivial = not bool(exterior.any())
gamma = None if trivial else float(t[y]-np.max(t[exterior]))
margin = None if trivial else gamma-2*epsilon
eta = float(64*np.finfo(np.float64).eps*max(1.,float(np.max(np.abs(t))),float(np.max(np.abs(p))),2*epsilon))
certified = trivial or gamma > 2*epsilon
near = bool(not trivial and abs(margin) <= eta)
distance = float(distances[yh])
if certified and distance > RADIUS:
raise ValueError('Certificate contradicts observed peak distance')
peakset = float(np.min(np.linalg.norm(x[top]-x[yh], axis=1))/rg)
taildist = None if true_tail is None else float(np.min(np.linalg.norm(x[true_tail > 0]-x[yh], axis=1))/rg)
if distance > RADIUS and peakset == 0:
category = 'OTHER_EXACT_TRUE_PEAK'
elif peakset <= RADIUS:
category = 'NEAR_TRUE_PEAK'
elif taildist is None:
category = 'FAR_TRUE_PEAK_QUERY'
elif taildist <= RADIUS:
category = 'NEAR_TRUE_TAIL_AWAY_FROM_PEAK'
else:
category = 'FAR_TRUE_TAIL'
return dict(nodes=len(t),epsilon=epsilon,gamma=gamma,gamma_positive_infinity=trivial,
gamma_minus_2epsilon=margin,eta=eta,near_boundary=near,trivial_radius=trivial,
certified=certified,stable_certificate=certified and not near,
peak_distance_over_Rg=distance,d_peakset_over_Rg=peakset,d_tail_over_Rg=taildist,
true_peak_raw_id=int(raw[y]),predicted_peak_raw_id=int(raw[yh]),
true_peak_count=len(top),predicted_peak_count=len(predicted_top),
true_peak_volume=None if w is None else math.fsum(w[top]),
predicted_peak_volume=None if w is None else math.fsum(w[predicted_top]),
true_time_drop_at_predicted_peak=float(t[y]-t[yh]),
predicted_minus_true_peak_time=float(p[yh]-t[y]),
constant_truth=bool(np.all(t==t[0])),category=category,
topology_branch_identified=False)
+58
View File
@@ -0,0 +1,58 @@
import unittest,tempfile,json
from pathlib import Path
from unittest.mock import patch
import numpy as np
from s2_tail_diagnostic_math import weights,tail_membership,tail_error,peak_diagnostic
from s2_tail_diagnostic import config
class TailTests(unittest.TestCase):
def test_missing_light_review_refuses(self):
with tempfile.TemporaryDirectory() as d:
r=Path(d);(r/'plan.json').write_text(json.dumps(dict(approval='1794966078.809549',cpu_cap=3600,spec_sha256='05344bacd877de87cbde9eb18139ad124e6cd4a15659e3c469b655b3f5a3f521',spec_path='synthetic',code_sha256={})))
with patch('s2_tail_diagnostic.checked'):
with self.assertRaises(FileNotFoundError):config(r)
def test_wrong_light_review_plan_refuses(self):
with tempfile.TemporaryDirectory() as d:
r=Path(d);(r/'plan.json').write_text(json.dumps(dict(approval='1794966078.809549',cpu_cap=3600,spec_sha256='05344bacd877de87cbde9eb18139ad124e6cd4a15659e3c469b655b3f5a3f521',spec_path='synthetic',code_sha256={})));(r/'review-accepted.json').write_text(json.dumps(dict(verdict='PASS',plan_sha256='wrong',code_sha256={})))
with patch('s2_tail_diagnostic.checked'):
with self.assertRaises(PermissionError):config(r)
def test_identical_fractional_plateau(self):
w=weights([1,1,2]);a,d=tail_membership(np.array([0,1,1]),w)
self.assertEqual(a[1],a[2]);self.assertEqual(tail_error(a,a,w)['E_tail'],0)
def test_disjoint_tails(self):
w=weights(np.ones(200));a,_=tail_membership(np.arange(200),w);b,_=tail_membership(-np.arange(200),w)
self.assertAlmostEqual(tail_error(a,b,w)['E_tail'],1)
def test_constant_truth_and_heavy_node(self):
w=weights([9,1]);a,d=tail_membership(np.ones(2),w);np.testing.assert_allclose(a,.01);self.assertTrue(d['constant_field'])
b,_=tail_membership(np.array([1,0]),w);self.assertAlmostEqual(b[0],.01/.9);self.assertAlmostEqual(np.dot(w,b),.01)
def test_nonuniform_and_ID_reorder(self):
s=np.array([2.,3.,3.,1.]);w=weights([.1,.7,.1,.1]);a,_=tail_membership(s,w);perm=np.array([3,1,0,2]);b,_=tail_membership(s[perm],w[perm]);np.testing.assert_array_equal(a[perm],b)
def test_zero_remainder_boundary(self):
w=weights([.01,.02,.97]);a,d=tail_membership(np.array([2.,1.,0.]),w);np.testing.assert_allclose(a,[1,0,0]);self.assertAlmostEqual(d['tail_volume'],.01)
def test_invalid_mass_and_nan(self):
for mass in [[0,1],[-1,2],[float('nan'),1],[]]:
with self.assertRaises(ValueError):weights(mass)
with self.assertRaises(ValueError):tail_membership(np.array([np.nan]),weights([1]))
def diag(self,t,p,raw=None,x=None):
return peak_diagnostic(t,p,np.array(x if x is not None else [[0,0,0],[1,0,0]]),np.array(raw if raw is not None else [0,1]),1.)
def test_strict_equality_not_certified(self):
d=self.diag([1,0],[.5,.5]);self.assertFalse(d['certified']);self.assertTrue(d['near_boundary'])
def test_certificate_and_nonconverse(self):
d=self.diag([1,0],[.9,.1]);self.assertTrue(d['certified']);self.assertLessEqual(d['peak_distance_over_Rg'],.1)
d=self.diag([1,0],[4,0]);self.assertFalse(d['certified']);self.assertEqual(d['peak_distance_over_Rg'],0)
def test_query_duplication(self):
a=self.diag([1,0],[.9,.1]);b=self.diag([1,0,1],[.9,.1,.9],raw=[0,1,0],x=[[0,0,0],[1,0,0],[0,0,0]]);self.assertEqual(a,b)
def test_empty_exterior(self):
d=self.diag([1,0],[0,1],x=[[0,0,0],[.01,0,0]]);self.assertTrue(d['trivial_radius']);self.assertIsNone(d['gamma']);self.assertTrue(d['certified'])
def test_strict_JSON_output(self):
for d in [self.diag([1,0],[.5,.5]),self.diag([1,0],[0,1],x=[[0,0,0],[.01,0,0]])]:self.assertEqual(json.loads(json.dumps(d,allow_nan=False)),d)
def test_other_exact_peak(self):
d=self.diag([1,1],[0,1]);self.assertEqual(d['category'],'OTHER_EXACT_TRUE_PEAK');self.assertEqual(d['d_peakset_over_Rg'],0)
def test_near_tail_without_branch_claim(self):
t=np.array([1,.99,0.]);p=np.array([0,1,0.]);w=weights([.005,.005,.99]);a,_=tail_membership(t,w)
d=peak_diagnostic(t,p,np.array([[0,0,0],[1,0,0],[2,0,0]]),np.arange(3),1,w=w,true_tail=a)
self.assertEqual(d['category'],'NEAR_TRUE_TAIL_AWAY_FROM_PEAK');self.assertFalse(d['topology_branch_identified'])
def test_monotone_tail_invariance(self):
s=np.array([0.,1.,2.,2.]);w=weights([3,4,5,6]);a,_=tail_membership(s,w);b,_=tail_membership(np.exp(s),w);np.testing.assert_array_equal(a,b)
if __name__=='__main__':unittest.main()