synth-code 0.0.1__py3-none-macosx_15_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- synth_code/__init__.py +0 -0
- synth_code/account.py +551 -0
- synth_code/account_http.py +86 -0
- synth_code/account_refusal.py +80 -0
- synth_code/accounting.py +148 -0
- synth_code/activity_grouping.py +149 -0
- synth_code/agent_panels.py +127 -0
- synth_code/approvals.py +397 -0
- synth_code/blobs.py +31 -0
- synth_code/cli.py +734 -0
- synth_code/client.py +413 -0
- synth_code/composer_delivery.py +139 -0
- synth_code/conversation.py +949 -0
- synth_code/daemon.py +34 -0
- synth_code/edition.py +306 -0
- synth_code/enrollment.py +304 -0
- synth_code/file_edit_facts.py +10 -0
- synth_code/freeze.py +234 -0
- synth_code/grant.py +59 -0
- synth_code/history.py +140 -0
- synth_code/history_generated.py +219 -0
- synth_code/history_wire.py +166 -0
- synth_code/image_input.py +109 -0
- synth_code/image_preview.py +105 -0
- synth_code/legacy_history.py +161 -0
- synth_code/live_turns.py +171 -0
- synth_code/model_picker.py +311 -0
- synth_code/native/manifest.json +1 -0
- synth_code/native/synth-code-runtime +0 -0
- synth_code/native.py +95 -0
- synth_code/oauth.py +284 -0
- synth_code/operator.py +116 -0
- synth_code/pack_source.py +65 -0
- synth_code/panel_request.py +291 -0
- synth_code/panels.py +40 -0
- synth_code/plugin_host.py +459 -0
- synth_code/protocol.py +35 -0
- synth_code/provider.py +313 -0
- synth_code/provider_fault.py +173 -0
- synth_code/provider_frames.py +571 -0
- synth_code/recommendation.py +324 -0
- synth_code/report.py +72 -0
- synth_code/research_events.py +44 -0
- synth_code/research_trace.py +449 -0
- synth_code/rex_view.py +58 -0
- synth_code/runtime_refusal.py +108 -0
- synth_code/schema/budgets.json +17 -0
- synth_code/schema/history-v2.json +1002 -0
- synth_code/session.py +291 -0
- synth_code/shoal.py +564 -0
- synth_code/side_panel.py +309 -0
- synth_code/skills.py +269 -0
- synth_code/slash.py +425 -0
- synth_code/source_view.py +135 -0
- synth_code/starter.json +13 -0
- synth_code/stdio.py +75 -0
- synth_code/stream.py +142 -0
- synth_code/subscriptions.py +267 -0
- synth_code/synth_authority.py +362 -0
- synth_code/synth_extra.py +32 -0
- synth_code/terminal.py +817 -0
- synth_code/terminal_journal.py +284 -0
- synth_code/terminal_layout.py +643 -0
- synth_code/terminal_surfaces.py +470 -0
- synth_code/terminal_viewport.py +125 -0
- synth_code/tool_render.py +86 -0
- synth_code/transcript_frames.py +212 -0
- synth_code/transcript_placement.py +63 -0
- synth_code/transcript_rows.py +456 -0
- synth_code/transport.py +243 -0
- synth_code/v001_history.py +468 -0
- synth_code/view.py +854 -0
- synth_code/views/__init__.py +93 -0
- synth_code/views/charts.py +196 -0
- synth_code/views/curses_adapter.py +116 -0
- synth_code/views/engine.py +170 -0
- synth_code/views/model.py +226 -0
- synth_code/views/primitives.py +270 -0
- synth_code/views/tiling.py +124 -0
- synth_code-0.0.1.dist-info/METADATA +50 -0
- synth_code-0.0.1.dist-info/RECORD +110 -0
- synth_code-0.0.1.dist-info/WHEEL +5 -0
- synth_code-0.0.1.dist-info/entry_points.txt +6 -0
- synth_code-0.0.1.dist-info/licenses/temper/LICENSE-APACHE +190 -0
- synth_code-0.0.1.dist-info/licenses/temper/LICENSE-MIT +21 -0
- synth_code-0.0.1.dist-info/licenses/temper/SOURCE.json +433 -0
- synth_code-0.0.1.dist-info/top_level.txt +2 -0
- synth_code_plugins/_sdk/__init__.py +19 -0
- synth_code_plugins/_sdk/callback_wire.py +240 -0
- synth_code_plugins/_sdk/callbacks.py +317 -0
- synth_code_plugins/_sdk/compliance.py +51 -0
- synth_code_plugins/_sdk/contracts.py +236 -0
- synth_code_plugins/_sdk/discovery.py +75 -0
- synth_code_plugins/_sdk/job_binding.py +71 -0
- synth_code_plugins/_sdk/lifecycle.py +50 -0
- synth_code_plugins/_sdk/lifecycle_generated.py +15 -0
- synth_code_plugins/_sdk/manifest.py +269 -0
- synth_code_plugins/_sdk/pack.py +332 -0
- synth_code_plugins/_sdk/push.py +98 -0
- synth_code_plugins/_sdk/records.py +26 -0
- synth_code_plugins/_sdk/registry.py +491 -0
- synth_code_plugins/_sdk/schemas.py +210 -0
- synth_code_plugins/_sdk/sources.py +230 -0
- synth_code_plugins/_sdk/surfaces.py +54 -0
- synth_code_plugins/_sdk/tools.py +172 -0
- synth_code_plugins/_sdk/worker_memory.py +49 -0
- synth_code_plugins/core/SKILL.md +6 -0
- synth_code_plugins/core/__init__.py +21 -0
- synth_code_plugins/core/plugin.toml +44 -0
- synth_code_plugins/core/source.py +18 -0
synth_code/freeze.py
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"""Policy freeze and paired scoring. Trusted verifier outputs enter only after freeze."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
import math
|
|
8
|
+
import os
|
|
9
|
+
import tempfile
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def digest(raw):
|
|
14
|
+
return hashlib.sha256(raw).hexdigest()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def canonical(value):
|
|
18
|
+
return json.dumps(
|
|
19
|
+
value, sort_keys=True, separators=(",", ":"), allow_nan=False
|
|
20
|
+
).encode()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def fixed_file(path, data):
|
|
24
|
+
path = Path(path)
|
|
25
|
+
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
26
|
+
with tempfile.NamedTemporaryFile(dir=path.parent, delete=False) as tmp:
|
|
27
|
+
tmp.write(data)
|
|
28
|
+
tmp.flush()
|
|
29
|
+
os.fsync(tmp.fileno())
|
|
30
|
+
try:
|
|
31
|
+
try:
|
|
32
|
+
os.link(tmp.name, path)
|
|
33
|
+
except FileExistsError:
|
|
34
|
+
if path.read_bytes() != data:
|
|
35
|
+
raise ValueError("Frozen input conflicts with existing bytes") from None
|
|
36
|
+
finally:
|
|
37
|
+
os.unlink(tmp.name)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def freeze(directory, game, incumbent, policies, wheel, profile, budget, traces):
|
|
41
|
+
if (
|
|
42
|
+
game not in ("nethack", "craftax_classic", "craftax_full")
|
|
43
|
+
or set(policies) != set(traces)
|
|
44
|
+
or len(policies) != 2
|
|
45
|
+
):
|
|
46
|
+
raise ValueError("Two named and traced policy arms are required")
|
|
47
|
+
allowed = {"tui", "harbor"} if game == "nethack" else {"core", "pair"}
|
|
48
|
+
if set(policies) != allowed:
|
|
49
|
+
raise ValueError("Wrong arms for the comparison")
|
|
50
|
+
if not isinstance(budget, dict) or any(
|
|
51
|
+
type(budget.get(k)) is not int or budget[k] <= 0
|
|
52
|
+
for k in ("rounds", "turns", "wall_seconds", "inputs_per_seed")
|
|
53
|
+
):
|
|
54
|
+
raise ValueError("Positive common budgets are required")
|
|
55
|
+
folder = Path(directory)
|
|
56
|
+
|
|
57
|
+
def retain(path):
|
|
58
|
+
raw = Path(path).read_bytes()
|
|
59
|
+
key = digest(raw)
|
|
60
|
+
fixed_file(folder / "blobs" / key, raw)
|
|
61
|
+
return key
|
|
62
|
+
|
|
63
|
+
metadata = {
|
|
64
|
+
"version": 1,
|
|
65
|
+
"game": game,
|
|
66
|
+
"baseline": retain(incumbent),
|
|
67
|
+
"policies": {k: retain(v) for k, v in policies.items()},
|
|
68
|
+
"traces": {k: retain(v) for k, v in traces.items()},
|
|
69
|
+
"wheel": retain(wheel),
|
|
70
|
+
"profile": retain(profile),
|
|
71
|
+
"budget": budget,
|
|
72
|
+
}
|
|
73
|
+
raw = canonical(metadata)
|
|
74
|
+
fixed_file(folder / "freeze.json", raw)
|
|
75
|
+
return {"freeze_sha256": digest(raw), **metadata}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def load_manifest(directory):
|
|
79
|
+
folder = Path(directory)
|
|
80
|
+
raw = (folder / "freeze.json").read_bytes()
|
|
81
|
+
manifest = json.loads(raw)
|
|
82
|
+
if raw != canonical(manifest):
|
|
83
|
+
raise ValueError("Noncanonical freeze")
|
|
84
|
+
for key in [
|
|
85
|
+
manifest["baseline"],
|
|
86
|
+
manifest["wheel"],
|
|
87
|
+
manifest["profile"],
|
|
88
|
+
*manifest["policies"].values(),
|
|
89
|
+
*manifest["traces"].values(),
|
|
90
|
+
]:
|
|
91
|
+
if (
|
|
92
|
+
len(key) != 64
|
|
93
|
+
or not all(c in "0123456789abcdef" for c in key)
|
|
94
|
+
or digest((folder / "blobs" / key).read_bytes()) != key
|
|
95
|
+
):
|
|
96
|
+
raise ValueError("Frozen input digest mismatch")
|
|
97
|
+
return digest(raw), manifest
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def read_verdict(path, freeze_hash, policy, cohort, evaluator):
|
|
101
|
+
# The verifier owns this envelope. Each opaque case ID must occur exactly once.
|
|
102
|
+
raw = Path(path).read_bytes()
|
|
103
|
+
data = json.loads(raw)
|
|
104
|
+
expected = {
|
|
105
|
+
"freeze_sha256": freeze_hash,
|
|
106
|
+
"policy_sha256": policy,
|
|
107
|
+
"cohort_sha256": cohort,
|
|
108
|
+
"evaluator_sha256": evaluator,
|
|
109
|
+
"split": "held_out",
|
|
110
|
+
}
|
|
111
|
+
if any(data.get(k) != v for k, v in expected.items()):
|
|
112
|
+
raise ValueError("Verdict differs from frozen inputs or sealed cohort")
|
|
113
|
+
rows = {}
|
|
114
|
+
for entry in data["cases"]:
|
|
115
|
+
case = entry["id"]
|
|
116
|
+
state = entry["outcome"]
|
|
117
|
+
score = entry.get("value")
|
|
118
|
+
if (
|
|
119
|
+
not isinstance(case, str)
|
|
120
|
+
or not case
|
|
121
|
+
or case in rows
|
|
122
|
+
or state
|
|
123
|
+
not in ("scored", "compile_failed", "refused", "not_scored", "driver_error")
|
|
124
|
+
):
|
|
125
|
+
raise ValueError("Invalid or duplicate verifier case")
|
|
126
|
+
if state == "scored":
|
|
127
|
+
if type(score) not in (int, float) or not math.isfinite(score):
|
|
128
|
+
raise ValueError("Invalid measured score")
|
|
129
|
+
elif score is not None:
|
|
130
|
+
raise ValueError("A failed case cannot carry a score")
|
|
131
|
+
rows[case] = (state, score)
|
|
132
|
+
if not rows:
|
|
133
|
+
raise ValueError("Empty held-out result")
|
|
134
|
+
return rows, digest(raw)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def compare(directory, cohort, evaluator, paths):
|
|
138
|
+
freeze_hash, m = load_manifest(directory)
|
|
139
|
+
if any(
|
|
140
|
+
len(h) != 64 or any(c not in "0123456789abcdef" for c in h)
|
|
141
|
+
for h in (cohort, evaluator)
|
|
142
|
+
):
|
|
143
|
+
raise ValueError("Expected evaluator and cohort SHA-256")
|
|
144
|
+
policies = {"baseline": m["baseline"], **m["policies"]}
|
|
145
|
+
if set(paths) != set(policies):
|
|
146
|
+
raise ValueError("Each frozen arm requires a verifier result")
|
|
147
|
+
rows = {}
|
|
148
|
+
receipts = {}
|
|
149
|
+
for arm, p in policies.items():
|
|
150
|
+
rows[arm], receipts[arm] = read_verdict(
|
|
151
|
+
paths[arm], freeze_hash, p, cohort, evaluator
|
|
152
|
+
)
|
|
153
|
+
baseline = rows["baseline"]
|
|
154
|
+
if any(set(r) != set(baseline) for r in rows.values()):
|
|
155
|
+
raise ValueError("Held-out cohorts differ")
|
|
156
|
+
comparisons = {}
|
|
157
|
+
for arm in m["policies"]:
|
|
158
|
+
pairs = [
|
|
159
|
+
{
|
|
160
|
+
"case": k,
|
|
161
|
+
"baseline": baseline[k][1],
|
|
162
|
+
"candidate": rows[arm][k][1],
|
|
163
|
+
"outcome": rows[arm][k][0],
|
|
164
|
+
"baseline_outcome": baseline[k][0],
|
|
165
|
+
}
|
|
166
|
+
for k in sorted(baseline)
|
|
167
|
+
]
|
|
168
|
+
changes = [
|
|
169
|
+
p["candidate"] - p["baseline"]
|
|
170
|
+
for p in pairs
|
|
171
|
+
if p["candidate"] is not None and p["baseline"] is not None
|
|
172
|
+
]
|
|
173
|
+
comparisons[arm] = {
|
|
174
|
+
"pairs": pairs,
|
|
175
|
+
"measured": len(changes),
|
|
176
|
+
"total": len(pairs),
|
|
177
|
+
"wins": sum(x > 0 for x in changes),
|
|
178
|
+
"ties": changes.count(0),
|
|
179
|
+
"losses": sum(x < 0 for x in changes),
|
|
180
|
+
"mean_change": sum(changes) / len(changes) if changes else None,
|
|
181
|
+
}
|
|
182
|
+
report = {
|
|
183
|
+
"game": m["game"],
|
|
184
|
+
"split": "held_out",
|
|
185
|
+
"freeze": freeze_hash,
|
|
186
|
+
"cohort": cohort,
|
|
187
|
+
"evaluator": evaluator,
|
|
188
|
+
"receipts": receipts,
|
|
189
|
+
"comparisons": comparisons,
|
|
190
|
+
}
|
|
191
|
+
fixed_file(
|
|
192
|
+
Path(directory) / "results" / (digest(canonical(receipts)) + ".json"),
|
|
193
|
+
canonical(report),
|
|
194
|
+
)
|
|
195
|
+
return report
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def dispatch(directory, arm, cohort_path, evaluator_digest, case_ids, destination):
|
|
199
|
+
"""Create a trusted runner's immutable job input after policies have frozen."""
|
|
200
|
+
key, m = load_manifest(directory)
|
|
201
|
+
policies = {"baseline": m["baseline"], **m["policies"]}
|
|
202
|
+
if (
|
|
203
|
+
arm not in policies
|
|
204
|
+
or len(evaluator_digest) != 64
|
|
205
|
+
or any(c not in "0123456789abcdef" for c in evaluator_digest)
|
|
206
|
+
):
|
|
207
|
+
raise ValueError("Unknown verifier or arm")
|
|
208
|
+
cohort = digest(Path(cohort_path).read_bytes())
|
|
209
|
+
seeds = json.loads(Path(case_ids).read_bytes())
|
|
210
|
+
if (
|
|
211
|
+
not isinstance(seeds, list)
|
|
212
|
+
or not 1 <= len(seeds) <= 50000
|
|
213
|
+
or any(type(n) is not int or n < 0 for n in seeds)
|
|
214
|
+
or len(set(seeds)) != len(seeds)
|
|
215
|
+
):
|
|
216
|
+
raise ValueError("Expected a distinct integer case roster")
|
|
217
|
+
job = {
|
|
218
|
+
"freeze_sha256": key,
|
|
219
|
+
"policy_sha256": policies[arm],
|
|
220
|
+
"cohort_sha256": cohort,
|
|
221
|
+
"evaluator_sha256": evaluator_digest,
|
|
222
|
+
"split": "held_out",
|
|
223
|
+
"cases": [digest(canonical([cohort, n])) for n in seeds],
|
|
224
|
+
}
|
|
225
|
+
output = Path(destination)
|
|
226
|
+
fixed_file(
|
|
227
|
+
output / "policy.rs", (Path(directory) / "blobs" / policies[arm]).read_bytes()
|
|
228
|
+
)
|
|
229
|
+
fixed_file(output / "dispatch.json", canonical(job))
|
|
230
|
+
return {
|
|
231
|
+
"job": str(output / "dispatch.json"),
|
|
232
|
+
"sha256": digest(canonical(job)),
|
|
233
|
+
"cases": len(seeds),
|
|
234
|
+
}
|
synth_code/grant.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Read an explicitly supplied customer grant. Its issuer owns refresh and expiry."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import stat
|
|
8
|
+
import time
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from .edition import beta_env
|
|
12
|
+
from .shoal import ShoalError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def read(origin):
|
|
16
|
+
path = beta_env("SYNTH_CODE_INFERENCE_GRANT_FILE")
|
|
17
|
+
if path:
|
|
18
|
+
fd = os.open(Path(path), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
|
|
19
|
+
with os.fdopen(fd) as f:
|
|
20
|
+
meta = os.fstat(f.fileno())
|
|
21
|
+
if (
|
|
22
|
+
not stat.S_ISREG(meta.st_mode)
|
|
23
|
+
or meta.st_uid != os.getuid()
|
|
24
|
+
or meta.st_mode & 0o077
|
|
25
|
+
or meta.st_size > 16384
|
|
26
|
+
):
|
|
27
|
+
raise ValueError("Expected a private customer grant file")
|
|
28
|
+
value = json.load(f)
|
|
29
|
+
if (
|
|
30
|
+
value["origin"].rstrip("/") != origin.rstrip("/")
|
|
31
|
+
or value.get("model") != "inkling-2-mini"
|
|
32
|
+
or not value.get("organization")
|
|
33
|
+
):
|
|
34
|
+
raise ShoalError("inference_grant_scope_mismatch", status=403)
|
|
35
|
+
if (
|
|
36
|
+
beta_env("SYNTH_CODE_ORG")
|
|
37
|
+
and beta_env("SYNTH_CODE_ORG") != value["organization"]
|
|
38
|
+
):
|
|
39
|
+
raise ShoalError("inference_grant_organization_mismatch", status=403)
|
|
40
|
+
expiry = value.get("expires_at")
|
|
41
|
+
if type(expiry) not in (int, float) or expiry <= time.time():
|
|
42
|
+
raise ShoalError("inference_grant_expired", status=401)
|
|
43
|
+
return value["token"], value["organization"]
|
|
44
|
+
token = beta_env("SYNTH_CODE_INFERENCE_SESSION")
|
|
45
|
+
from . import enrollment
|
|
46
|
+
|
|
47
|
+
if origin.rstrip("/") != enrollment.SHOAL:
|
|
48
|
+
raise ShoalError("inference_grant_origin_mismatch", status=403)
|
|
49
|
+
if not token:
|
|
50
|
+
token, lease = enrollment.active()
|
|
51
|
+
return token, lease["binding"]["org_id"]
|
|
52
|
+
return token, beta_env("SYNTH_CODE_ORG")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def available():
|
|
56
|
+
return bool(
|
|
57
|
+
beta_env("SYNTH_CODE_INFERENCE_SESSION")
|
|
58
|
+
or beta_env("SYNTH_CODE_INFERENCE_GRANT_FILE")
|
|
59
|
+
)
|
synth_code/history.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Cursor inspection, correlated evidence and metadata-only exports."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
from .blobs import read_blob
|
|
8
|
+
from .history_wire import read_event, read_scoped_event, steering_receipt
|
|
9
|
+
from .research_trace import PolicyTrace, seed_lines
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"completed_receipt",
|
|
13
|
+
"events",
|
|
14
|
+
"explain_seed",
|
|
15
|
+
"explain_turn",
|
|
16
|
+
"export",
|
|
17
|
+
"paired_grade_events",
|
|
18
|
+
"query",
|
|
19
|
+
"seed_lines",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def events(client, after=0, until=None):
|
|
24
|
+
if after < 0 or (until is not None and until < after):
|
|
25
|
+
raise ValueError("Invalid cursor")
|
|
26
|
+
while page := client.page(after):
|
|
27
|
+
for event in page:
|
|
28
|
+
if until is not None and event["seq"] > until:
|
|
29
|
+
return
|
|
30
|
+
yield event
|
|
31
|
+
after = page[-1]["seq"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def query(client, *, after=0, actor=None, turn=None, cause=None, until=None):
|
|
35
|
+
decode = read_event if client.viewer is None else read_scoped_event
|
|
36
|
+
for event in events(client, after=after, until=until):
|
|
37
|
+
durable = decode(event, client.run)
|
|
38
|
+
role, operation = durable["actor"], durable["turn"]
|
|
39
|
+
if (
|
|
40
|
+
(actor is not None and actor != role)
|
|
41
|
+
or (turn is not None and turn != operation)
|
|
42
|
+
or (cause is not None and durable["cause"] != cause)
|
|
43
|
+
):
|
|
44
|
+
continue
|
|
45
|
+
yield durable | {"operation": operation}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def export(client, after=0, until=None):
|
|
49
|
+
# Deliberately omit free text, context, IDs supplied by the user and provider payloads.
|
|
50
|
+
for e in events(client, after, until):
|
|
51
|
+
yield {key: e[key] for key in ("version", "seq", "kind", "cause")}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def completed_receipt(client, event: dict) -> dict:
|
|
55
|
+
"""Load the normalized provider receipt for an authoritative completed turn."""
|
|
56
|
+
payload = event["payload"]
|
|
57
|
+
if event["kind"] != "turn.finished" or payload["outcome"]["kind"] != "completed":
|
|
58
|
+
raise ValueError("Provider receipt requires a completed turn")
|
|
59
|
+
reference = payload["receipt_ref"]
|
|
60
|
+
if type(reference["bytes"]) is not int or not 0 < reference["bytes"] <= 2_097_152:
|
|
61
|
+
raise ValueError("Provider receipt exceeds the Engine v2 receipt budget")
|
|
62
|
+
parsed = json.loads(read_blob(client, reference))
|
|
63
|
+
if not isinstance(parsed, dict) or parsed.get("provider") != payload["provider"]:
|
|
64
|
+
raise ValueError(f"Provider receipt disagrees with turn {event['turn']}")
|
|
65
|
+
return parsed
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def explain_turn(client, turn_id):
|
|
69
|
+
"""Return committed evidence for one turn, including late steering receipts."""
|
|
70
|
+
reservation = None
|
|
71
|
+
outcome = None
|
|
72
|
+
steering = []
|
|
73
|
+
for event in events(client):
|
|
74
|
+
data = event["payload"]
|
|
75
|
+
if event["turn"] != turn_id:
|
|
76
|
+
continue
|
|
77
|
+
kind = event["kind"]
|
|
78
|
+
if kind == "turn.reserved":
|
|
79
|
+
reservation = event
|
|
80
|
+
elif steering_receipt(event):
|
|
81
|
+
steering.append(
|
|
82
|
+
{
|
|
83
|
+
"event": event["seq"],
|
|
84
|
+
"operation": data["operation"],
|
|
85
|
+
"outcome": data["outcome"],
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
elif kind == "turn.finished":
|
|
89
|
+
outcome = event
|
|
90
|
+
if reservation is None:
|
|
91
|
+
raise ValueError(f"Unknown turn {turn_id!r} in run {client.run!r}")
|
|
92
|
+
|
|
93
|
+
def source(seq):
|
|
94
|
+
return client.event(seq)
|
|
95
|
+
|
|
96
|
+
data = reservation["payload"]
|
|
97
|
+
messages = []
|
|
98
|
+
for entry in data["inbox"]:
|
|
99
|
+
message = source(entry["event"])
|
|
100
|
+
messages.append({"message": message, "trigger": source(message["cause"])})
|
|
101
|
+
return {
|
|
102
|
+
"run": client.run,
|
|
103
|
+
"turn": turn_id,
|
|
104
|
+
"actor": data["role"],
|
|
105
|
+
"profile": data["profile"],
|
|
106
|
+
"reservation": reservation["seq"],
|
|
107
|
+
"input": source(reservation["cause"]),
|
|
108
|
+
"checkpoint": data["checkpoint"],
|
|
109
|
+
"documents": data["documents"],
|
|
110
|
+
"inbox": messages,
|
|
111
|
+
"steering": steering,
|
|
112
|
+
"outcome": outcome,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def explain_seed(client, turn_id, seed):
|
|
117
|
+
"""Explain a seed comparison using committed policy-turn evidence."""
|
|
118
|
+
trace = PolicyTrace.from_events(
|
|
119
|
+
events(client),
|
|
120
|
+
client.run,
|
|
121
|
+
turn_id,
|
|
122
|
+
edit_content=lambda reference: _edit_content(client, reference),
|
|
123
|
+
)
|
|
124
|
+
return trace.for_seed(client.run, turn_id, seed)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def paired_grade_events(client, turn_id):
|
|
128
|
+
"""Return committed grades matched to the verified, applied policy edit."""
|
|
129
|
+
return PolicyTrace.from_events(
|
|
130
|
+
events(client),
|
|
131
|
+
client.run,
|
|
132
|
+
turn_id,
|
|
133
|
+
edit_content=lambda reference: _edit_content(client, reference),
|
|
134
|
+
).grades
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _edit_content(client, reference: dict) -> str:
|
|
138
|
+
if type(reference["bytes"]) is not int or not 0 <= reference["bytes"] <= 24_576:
|
|
139
|
+
raise ValueError("Generated edit reference exceeds the Engine v2 file limit")
|
|
140
|
+
return read_blob(client, reference).decode("utf-8")
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"""Generated from schema/history-v2.json. Do not edit."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Literal, NotRequired, TypedDict
|
|
6
|
+
|
|
7
|
+
SCHEMA_SHA256 = 'dbf64e431f909cad71fb1e904d2940349a23e8091e7ddbe99bb77fcd0273dc6d'
|
|
8
|
+
__all__ = ['Actor', 'BlobRef', 'Cause', 'DurableEvent', 'HistoryChange', 'HistorySchema', 'HistorySnapshot', 'InputPart', 'MessagePhase', 'Subject', 'TranscriptRow']
|
|
9
|
+
|
|
10
|
+
class Actor1(TypedDict):
|
|
11
|
+
kind: Literal['owner']
|
|
12
|
+
|
|
13
|
+
class Actor2(TypedDict):
|
|
14
|
+
kind: Literal['engine']
|
|
15
|
+
|
|
16
|
+
class Actor3(TypedDict):
|
|
17
|
+
id: str
|
|
18
|
+
kind: Literal['agent']
|
|
19
|
+
|
|
20
|
+
class Actor4(TypedDict):
|
|
21
|
+
id: str
|
|
22
|
+
kind: Literal['provider']
|
|
23
|
+
|
|
24
|
+
class Actor5(TypedDict):
|
|
25
|
+
id: str
|
|
26
|
+
kind: Literal['plugin']
|
|
27
|
+
|
|
28
|
+
Actor = Actor1 | Actor2 | Actor3 | Actor4 | Actor5
|
|
29
|
+
|
|
30
|
+
class BlobRef(TypedDict):
|
|
31
|
+
bytes: int
|
|
32
|
+
sha256: str
|
|
33
|
+
|
|
34
|
+
class Cause1(TypedDict):
|
|
35
|
+
id: str
|
|
36
|
+
kind: Literal['input']
|
|
37
|
+
|
|
38
|
+
class Cause2(TypedDict):
|
|
39
|
+
id: str
|
|
40
|
+
kind: Literal['turn']
|
|
41
|
+
|
|
42
|
+
class Cause3(TypedDict):
|
|
43
|
+
id: str
|
|
44
|
+
kind: Literal['tool']
|
|
45
|
+
|
|
46
|
+
class Cause4(TypedDict):
|
|
47
|
+
id: str
|
|
48
|
+
kind: Literal['effect']
|
|
49
|
+
|
|
50
|
+
class Cause5(TypedDict):
|
|
51
|
+
id: str
|
|
52
|
+
kind: Literal['node']
|
|
53
|
+
|
|
54
|
+
class Cause6(TypedDict):
|
|
55
|
+
kind: Literal['system']
|
|
56
|
+
reason: str
|
|
57
|
+
|
|
58
|
+
Cause = Cause1 | Cause2 | Cause3 | Cause4 | Cause5 | Cause6
|
|
59
|
+
|
|
60
|
+
class DurableEvent(TypedDict):
|
|
61
|
+
actor: str
|
|
62
|
+
actor_ref: Actor
|
|
63
|
+
at: str
|
|
64
|
+
cause: int
|
|
65
|
+
cause_ref: Cause
|
|
66
|
+
committed_at: str
|
|
67
|
+
event_id: str
|
|
68
|
+
history_change: NotRequired[HistoryChange | None]
|
|
69
|
+
id: str
|
|
70
|
+
kind: str
|
|
71
|
+
kind_version: int
|
|
72
|
+
links: NotRequired[list[Any] | None]
|
|
73
|
+
operation: NotRequired[Any]
|
|
74
|
+
payload: dict[str, Any]
|
|
75
|
+
profile: NotRequired[str | None]
|
|
76
|
+
run: str
|
|
77
|
+
schema: HistorySchema
|
|
78
|
+
seq: int
|
|
79
|
+
source_schema: NotRequired[Any]
|
|
80
|
+
subject: Subject
|
|
81
|
+
turn: NotRequired[str | None]
|
|
82
|
+
version: int
|
|
83
|
+
|
|
84
|
+
class HistoryChange(TypedDict):
|
|
85
|
+
cursor: int
|
|
86
|
+
display: bool
|
|
87
|
+
upsert: NotRequired[TranscriptRow | None]
|
|
88
|
+
|
|
89
|
+
HistorySchema = Literal['synth-code.v2']
|
|
90
|
+
|
|
91
|
+
class InputPart1(TypedDict):
|
|
92
|
+
text: str
|
|
93
|
+
type: Literal['text']
|
|
94
|
+
|
|
95
|
+
class InputPart2(TypedDict):
|
|
96
|
+
bytes: int
|
|
97
|
+
sha256: str
|
|
98
|
+
type: Literal['blob']
|
|
99
|
+
|
|
100
|
+
InputPart = InputPart1 | InputPart2
|
|
101
|
+
|
|
102
|
+
MessagePhase = Literal['commentary', 'final']
|
|
103
|
+
|
|
104
|
+
class Subject1(TypedDict):
|
|
105
|
+
id: str
|
|
106
|
+
kind: Literal['run']
|
|
107
|
+
|
|
108
|
+
class Subject2(TypedDict):
|
|
109
|
+
id: str
|
|
110
|
+
kind: Literal['input']
|
|
111
|
+
|
|
112
|
+
class Subject3(TypedDict):
|
|
113
|
+
id: str
|
|
114
|
+
kind: Literal['turn']
|
|
115
|
+
|
|
116
|
+
class Subject4(TypedDict):
|
|
117
|
+
id: str
|
|
118
|
+
kind: Literal['tool']
|
|
119
|
+
|
|
120
|
+
class Subject5(TypedDict):
|
|
121
|
+
id: str
|
|
122
|
+
kind: Literal['effect']
|
|
123
|
+
|
|
124
|
+
class Subject6(TypedDict):
|
|
125
|
+
id: str
|
|
126
|
+
kind: Literal['node']
|
|
127
|
+
|
|
128
|
+
class Subject7(TypedDict):
|
|
129
|
+
id: str
|
|
130
|
+
kind: Literal['provider']
|
|
131
|
+
|
|
132
|
+
class Subject8(TypedDict):
|
|
133
|
+
id: str
|
|
134
|
+
kind: Literal['viewer']
|
|
135
|
+
|
|
136
|
+
class Subject9(TypedDict):
|
|
137
|
+
id: str
|
|
138
|
+
kind: Literal['source']
|
|
139
|
+
|
|
140
|
+
class Subject10(TypedDict):
|
|
141
|
+
id: str
|
|
142
|
+
kind: Literal['message']
|
|
143
|
+
|
|
144
|
+
class Subject11(TypedDict):
|
|
145
|
+
id: str
|
|
146
|
+
kind: Literal['config']
|
|
147
|
+
|
|
148
|
+
class Subject12(TypedDict):
|
|
149
|
+
id: str
|
|
150
|
+
kind: Literal['approval']
|
|
151
|
+
|
|
152
|
+
class Subject13(TypedDict):
|
|
153
|
+
id: str
|
|
154
|
+
kind: Literal['artifact']
|
|
155
|
+
|
|
156
|
+
class Subject14(TypedDict):
|
|
157
|
+
id: str
|
|
158
|
+
kind: Literal['wake']
|
|
159
|
+
|
|
160
|
+
class Subject15(TypedDict):
|
|
161
|
+
id: str
|
|
162
|
+
kind: Literal['job']
|
|
163
|
+
|
|
164
|
+
class Subject16(TypedDict):
|
|
165
|
+
id: str
|
|
166
|
+
kind: Literal['machine']
|
|
167
|
+
|
|
168
|
+
Subject = Subject1 | Subject2 | Subject3 | Subject4 | Subject5 | Subject6 | Subject7 | Subject8 | Subject9 | Subject10 | Subject11 | Subject12 | Subject13 | Subject14 | Subject15 | Subject16
|
|
169
|
+
|
|
170
|
+
class TranscriptRow1(TypedDict):
|
|
171
|
+
id: str
|
|
172
|
+
kind: Literal['input']
|
|
173
|
+
parts: list[InputPart]
|
|
174
|
+
seq: int
|
|
175
|
+
target: str
|
|
176
|
+
|
|
177
|
+
class TranscriptRow2(TypedDict):
|
|
178
|
+
index: int
|
|
179
|
+
kind: Literal['message']
|
|
180
|
+
message_id: str
|
|
181
|
+
phase: MessagePhase
|
|
182
|
+
role: str
|
|
183
|
+
seq: int
|
|
184
|
+
stream_final: bool
|
|
185
|
+
text: NotRequired[str | None]
|
|
186
|
+
text_ref: NotRequired[BlobRef | None]
|
|
187
|
+
turn: str
|
|
188
|
+
|
|
189
|
+
class TranscriptRow3(TypedDict):
|
|
190
|
+
actions: list[Any]
|
|
191
|
+
finish: NotRequired[Any]
|
|
192
|
+
kind: Literal['tool']
|
|
193
|
+
parent_tool_id: NotRequired[str | None]
|
|
194
|
+
seq: int
|
|
195
|
+
target: str
|
|
196
|
+
tool_id: str
|
|
197
|
+
tool_kind: str
|
|
198
|
+
turn: str
|
|
199
|
+
|
|
200
|
+
class TranscriptRow4(TypedDict):
|
|
201
|
+
item: str
|
|
202
|
+
kind: Literal['reasoning']
|
|
203
|
+
seq: int
|
|
204
|
+
text: NotRequired[str | None]
|
|
205
|
+
text_ref: NotRequired[BlobRef | None]
|
|
206
|
+
turn: str
|
|
207
|
+
|
|
208
|
+
TranscriptRow = TranscriptRow1 | TranscriptRow2 | TranscriptRow3 | TranscriptRow4
|
|
209
|
+
|
|
210
|
+
class HistorySnapshot(TypedDict):
|
|
211
|
+
active_turns: list[DurableEvent]
|
|
212
|
+
approvals: list[DurableEvent]
|
|
213
|
+
before: NotRequired[int | None]
|
|
214
|
+
cursor: int
|
|
215
|
+
open_tools: list[TranscriptRow]
|
|
216
|
+
rows: list[DurableEvent]
|
|
217
|
+
run: str
|
|
218
|
+
schema: HistorySchema
|
|
219
|
+
transcript: list[TranscriptRow]
|