Skip to content

Verify

Verifying runs: the deterministic checks run again, and rubric versions compared across runs.

replay shows that a run's graph follows from its recorded calls; verify looks at the checks one by one, from the run directory alone, without the source. Each recompute-numbers, rederive-maths and rederive-maths-numeric check of the graph is run again from the inputs its record holds, and any field of the record the re-run does not reproduce is reported: a result, a detail, the digest of the check's files. The files in checks/c-NNN/ are compared with the digest the record holds, and every check record, citation checks included, with the inputs.json and output.json its check wrote; a check of a type the runner does not have is reported. Citation checks rest on an agent's judgement, so they are not run again; for an agent-tool run, replay reads each one's source again from its bytes. verify reads runs of either format: those the agent tools make, and those the earlier model-calling passes made.

A rubric's version names its values, so two runs that record one rubric name and version with different file digests used values that changed without a new version. rubric_reuse reports each such name and version.

Difference dataclass

A field of a check record that differs from what verifying it gives.

Attributes:

Name Type Description
check str

The check node's id.

field str

The record's field, or files for the files of the check's directory.

recorded Any

The recorded value; for files, the record's log digest.

found Any

The value the re-run gives; for files, the digest of the files in the directory.

Source code in src/credencegraph_extract/verify.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@dataclass(frozen=True)
class Difference:
    """A field of a check record that differs from what verifying it gives.

    Attributes:
        check: The check node's id.
        field: The record's field, or ``files`` for the files of the check's directory.
        recorded: The recorded value; for ``files``, the record's log digest.
        found: The value the re-run gives; for ``files``, the digest of the files in the directory.
    """

    check: str
    field: str
    recorded: Any
    found: Any

Verification dataclass

What verifying a run found.

Attributes:

Name Type Description
rerun tuple[str, ...]

The ids of the checks run again, in graph order.

not_rerun tuple[str, ...]

The ids of the checks judged by a model, which are not run again.

differences tuple[Difference, ...]

Every difference found, in graph order.

Source code in src/credencegraph_extract/verify.py
63
64
65
66
67
68
69
70
71
72
73
74
75
@dataclass(frozen=True)
class Verification:
    """What verifying a run found.

    Attributes:
        rerun: The ids of the checks run again, in graph order.
        not_rerun: The ids of the checks judged by a model, which are not run again.
        differences: Every difference found, in graph order.
    """

    rerun: tuple[str, ...]
    not_rerun: tuple[str, ...]
    differences: tuple[Difference, ...]

rubric_reuse(manifests)

Report each rubric name and version that runs record with more than one file digest.

Parameters:

Name Type Description Default
manifests Sequence[tuple[str, Mapping[str, Any]]]

Each run's name, as the report should give it, and its manifest.

required

Returns:

Type Description
list[str]

One warning per such name and version, naming each digest and the runs that record it.

Source code in src/credencegraph_extract/verify.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def rubric_reuse(manifests: Sequence[tuple[str, Mapping[str, Any]]]) -> list[str]:
    """Report each rubric name and version that runs record with more than one file digest.

    Args:
        manifests: Each run's name, as the report should give it, and its manifest.

    Returns:
        One warning per such name and version, naming each digest and the runs that record it.
    """
    seen: dict[tuple[str, str], dict[str, list[str]]] = {}
    for name, manifest in manifests:
        rubric = manifest["rubric"]
        key = (str(rubric.get("name")), str(rubric.get("version")))
        seen.setdefault(key, {}).setdefault(rubric["digest"], []).append(name)
    warnings = []
    for (rubric_name, version), digests in seen.items():
        if len(digests) > 1:
            listing = "; ".join(f"{digest} in {', '.join(runs)}" for digest, runs in digests.items())
            warnings.append(
                f"rubric {rubric_name!r} version {version!r} was used with {len(digests)} different files "
                f"({listing}): values changed without a new version"
            )
    return warnings

run_manifest(run_dir)

Read the manifest of a run of either format: a model-calling run (1) or an agent-tool run (2).

An agent-tool run is first reconciled with its call log, which rewrites any derived file a crash left behind; it has a graph only while it is finished, so an unfinished one is refused.

Parameters:

Name Type Description Default
run_dir Path

The run directory.

required

Returns:

Type Description
dict[str, Any]

The manifest.

Raises:

Type Description
RunError

If the directory is not a run, or is an unfinished agent-tool run.

Source code in src/credencegraph_extract/verify.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def run_manifest(run_dir: Path) -> dict[str, Any]:
    """Read the manifest of a run of either format: a model-calling run (1) or an agent-tool run (2).

    An agent-tool run is first reconciled with its call log, which rewrites any derived file a crash left
    behind; it has a graph only while it is finished, so an unfinished one is refused.

    Args:
        run_dir: The run directory.

    Returns:
        The manifest.

    Raises:
        RunError: If the directory is not a run, or is an unfinished agent-tool run.
    """
    try:
        version = json.loads((run_dir / MANIFEST).read_bytes()).get("version")
    except (OSError, ValueError, AttributeError):
        version = None
    if version != session.VERSION:
        return read_manifest(run_dir)
    session.reconcile(run_dir)
    manifest = session.read_manifest(run_dir)
    if not manifest.get("finished"):
        raise RunError("unfinished-run", "the run is not finished: call finish_run before verifying it")
    return manifest

verify_run(run_dir)

Run a run's deterministic checks again and compare each with its record.

Parameters:

Name Type Description Default
run_dir Path

The run directory.

required

Returns:

Type Description
Verification

The checks run again, those not, and every difference found.

Raises:

Type Description
RunError

If the run directory is malformed or incomplete, or its graph.json or rubric.toml differs from the one the manifest records.

Source code in src/credencegraph_extract/verify.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def verify_run(run_dir: Path) -> Verification:
    """Run a run's deterministic checks again and compare each with its record.

    Args:
        run_dir: The run directory.

    Returns:
        The checks run again, those not, and every difference found.

    Raises:
        RunError: If the run directory is malformed or incomplete, or its ``graph.json`` or
            ``rubric.toml`` differs from the one the manifest records.
    """
    manifest = run_manifest(run_dir)
    graph_bytes = read_run_file(run_dir, GRAPH)
    if bytes_digest(graph_bytes) != manifest["graph_digest"]:
        raise RunError("record-mismatch", f"{GRAPH} differs from the graph the run wrote")
    rubric_bytes = read_run_file(run_dir, RUBRIC)
    if bytes_digest(rubric_bytes) != manifest["rubric"]["digest"]:
        raise RunError("record-mismatch", f"{RUBRIC} differs from the rubric the run used")
    try:
        graph: Graph = loads(graph_bytes.decode("utf-8"))
        rubric: Rubric = loads_rubric(rubric_bytes.decode("utf-8"))
    except (UnicodeDecodeError, CredenceGraphError, RubricError) as error:
        raise RunError("invalid-record", f"cannot read the run's graph or rubric: {error}") from None
    rerun_ids: list[str] = []
    not_rerun: list[str] = []
    differences: list[Difference] = []
    for node in graph:
        recorded = node.attributes.get("check")
        if not isinstance(recorded, Mapping):
            continue
        kind = recorded.get("type")
        if kind == CITATION:
            not_rerun.append(node.id)
        elif kind not in DETERMINISTIC:
            differences.append(Difference(node.id, "type", kind, f"one of {list(IMPLEMENTED)}"))
        else:
            rerun_ids.append(node.id)
            found, _ = rerun(graph, rubric, node)
            differences.extend(
                Difference(node.id, key, recorded.get(key), value)
                for key, value in found.items()
                if plain(recorded.get(key)) != plain(value)
            )
        log = recorded.get("log")
        digest = log.get("digest") if isinstance(log, Mapping) else None
        files = _files(run_dir, node.id)
        on_disk = directory_digest(files) if files is not None else None
        if on_disk != digest:
            differences.append(Difference(node.id, "files", digest, on_disk))
        if files is not None:
            differences.extend(_file_differences(node.id, recorded, files))
    return Verification(tuple(rerun_ids), tuple(not_rerun), tuple(differences))