File size: 1,026 Bytes
caf8059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Local immutable evidence objects; production registry remains an adapter boundary."""
import json
import re
from src.modules.skill.domain.models import canonical, digest, SkillError


class ArtifactCatalog:
    def __init__(self, repository):
        self.repository = repository

    def record(self, tenant, run_id, body):
        if not re.fullmatch(r'run-[a-f0-9]{32}', run_id):
            raise SkillError('INVALID_RUN_ID', '非法任务标识。')
        value = dict(body, run_id=run_id)
        key = digest(value)
        self.repository.put(tenant, run_id, key, canonical(value))
        return {'digest': key, 'manifest': value}

    def read(self, tenant, run_id, key):
        value = self.repository.get(tenant, run_id, key)
        if value is None:
            raise SkillError('ARTIFACT_NOT_FOUND', '证据不存在或不可见。', 404)
        body = json.loads(value)
        if digest(body) != key:
            raise SkillError('ARTIFACT_DIGEST_MISMATCH', '证据损坏。', 409)
        return body