Build Code4History Research
This commit is contained in:
+12
-13
@@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Code4History two-way workbench. All operations are local, stdlib-only.
|
||||
"""C4H-R local workbench. All operations are local, stdlib-only.
|
||||
|
||||
Use --root to target a test or separate project. No daemon or background process.
|
||||
This tool safely archives upload bytes and moves completed human/agent tasks;
|
||||
semantic historical interpretation remains the responsibility of the research skill.
|
||||
the agent handles historical interpretation.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import argparse
|
||||
@@ -154,7 +154,7 @@ def next_number(folder: Path, prefix: str) -> int:
|
||||
|
||||
def next_source_id(root: Path,kind: str):
|
||||
prefix, sub=SRC_KINDS[kind]
|
||||
folder=root/'01_sources/records'/sub
|
||||
folder=root/'sources/records'/sub
|
||||
return f'{prefix}-{next_number(folder,prefix):04d}'
|
||||
|
||||
|
||||
@@ -181,7 +181,7 @@ def intake(root:Path,filename:str,kind:str,slug:str,dry_run=False):
|
||||
# A previous round may have been interrupted after successful archival but
|
||||
# before upload cleanup, or the user may have uploaded identical bytes.
|
||||
# Reuse an existing verified accession; NEVER discard a changed sidecar.
|
||||
for prior in sorted((root/'01_sources/provenance').glob('SRC-*-intake.json')):
|
||||
for prior in sorted((root/'sources/provenance').glob('SRC-*-intake.json')):
|
||||
try:
|
||||
receipt_old=json.loads(prior.read_text(encoding='utf-8'))
|
||||
archived=root/receipt_old['archive_path']
|
||||
@@ -191,7 +191,7 @@ def intake(root:Path,filename:str,kind:str,slug:str,dry_run=False):
|
||||
if receipt_old.get('source_note_sha256')!=note_hash:
|
||||
raise ValueError('已归档相同原件,但此次来源说明不同;保留上传件供人工合并新的来源信息。')
|
||||
record_id=receipt_old['source_id']
|
||||
record_matches=list((root/'01_sources/records'/SRC_KINDS[kind][1]).glob(record_id+'-*.md'))
|
||||
record_matches=list((root/'sources/records'/SRC_KINDS[kind][1]).glob(record_id+'-*.md'))
|
||||
if not record_id.startswith(SRC_KINDS[kind][0]+'-') or not record_matches:
|
||||
continue
|
||||
if dry_run:return {'id':record_id,'reused':True,'sha256':digest}
|
||||
@@ -203,17 +203,16 @@ def intake(root:Path,filename:str,kind:str,slug:str,dry_run=False):
|
||||
except (KeyError, json.JSONDecodeError):
|
||||
continue
|
||||
ident=next_source_id(root,kind)
|
||||
suffix=Path(filename).suffix.lower()
|
||||
original=root/'01_sources/originals'/SRC_KINDS[kind][1]/f'{ident}--{filename}'
|
||||
meta_dest=root/'01_sources/provenance'/f'{ident}-upload-source.md'
|
||||
receipt=root/'01_sources/provenance'/f'{ident}-intake.json'
|
||||
record=root/'01_sources/records'/SRC_KINDS[kind][1]/f'{ident}-{slug}.md'
|
||||
original=root/'sources/originals'/SRC_KINDS[kind][1]/f'{ident}--{filename}'
|
||||
meta_dest=root/'sources/provenance'/f'{ident}-upload-source.md'
|
||||
receipt=root/'sources/provenance'/f'{ident}-intake.json'
|
||||
record=root/'sources/records'/SRC_KINDS[kind][1]/f'{ident}-{slug}.md'
|
||||
if any(p.exists() for p in (original,receipt,record,meta_dest)):
|
||||
raise FileExistsError('Generated destination is occupied; inspect before retrying')
|
||||
plan={'id':ident,'original_path':original.relative_to(root).as_posix(),'record_path':record.relative_to(root).as_posix(),'metadata_path':meta_dest.relative_to(root).as_posix() if has_meta else None,'sha256':digest,'metadata_sha256':note_hash,'metadata_missing':not has_meta}
|
||||
if dry_run:return plan
|
||||
# Validate all inputs, prepare copies and documentation before removing originals.
|
||||
template=root/'10_templates'/('primary.md' if kind=='primary' else 'secondary.md')
|
||||
template=root/'templates/source.md'
|
||||
card=template.read_text(encoding='utf-8').replace('__ID__',ident)
|
||||
for field,value in [('original_path',plan['original_path']),('sha256',digest),('original_filename',filename),('ingested_utc',stamp()),('upload_source_note',plan['metadata_path'] or ''),('source_metadata_status','provided_unverified' if has_meta else 'missing')]:
|
||||
card=yaml_set(card,field,value)
|
||||
@@ -227,9 +226,9 @@ def intake(root:Path,filename:str,kind:str,slug:str,dry_run=False):
|
||||
if sha256(original)!=digest or not record.is_file() or not receipt.is_file() or (has_meta and sha256(meta_dest)!=note_hash):
|
||||
raise IOError('Archive integrity check failed; upload retained')
|
||||
# Raw catalog updates can be retried by Agent if interrupted; receipt is durable.
|
||||
catalog=root/'01_sources/catalog.md'
|
||||
catalog=root/'sources/catalog.md'
|
||||
current=catalog.read_text(encoding='utf-8') if catalog.exists() else '# Source catalog\n'
|
||||
line=f'\n- `{ident}` · `{plan["original_path"]}` · sha256 `{digest}` · [SRC card]({record.relative_to(root).as_posix()}) · metadata: {"provided" if has_meta else "pending"}\n'
|
||||
line=f'\n- `{ident}` · `{plan["original_path"]}` · sha256 `{digest}` · [SRC card]({record.relative_to(catalog.parent).as_posix()}) · metadata: {"provided" if has_meta else "pending"}\n'
|
||||
atomic_replace(catalog,(current+line).encode('utf-8'))
|
||||
# Recheck bytes before removing inbox: if researcher modified during intake, retain.
|
||||
if sha256(source)!=digest or (has_meta and sha256(metadata)!=note_hash):
|
||||
|
||||
Reference in New Issue
Block a user