nvs-runtime 1.0.0__py3-none-any.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.
- nvs_runtime/__init__.py +29 -0
- nvs_runtime/__version__.py +3 -0
- nvs_runtime/config/__init__.py +3 -0
- nvs_runtime/config/settings.py +26 -0
- nvs_runtime/core/__init__.py +6 -0
- nvs_runtime/core/clock.py +32 -0
- nvs_runtime/core/composition.py +92 -0
- nvs_runtime/core/errors.py +69 -0
- nvs_runtime/core/hext.py +43 -0
- nvs_runtime/core/models.py +151 -0
- nvs_runtime/core/protocols.py +77 -0
- nvs_runtime/core/runtime.py +373 -0
- nvs_runtime/events/__init__.py +4 -0
- nvs_runtime/events/bus.py +50 -0
- nvs_runtime/events/types.py +35 -0
- nvs_runtime/executors/__init__.py +2 -0
- nvs_runtime/governance/__init__.py +4 -0
- nvs_runtime/governance/engine.py +58 -0
- nvs_runtime/governance/quota.py +48 -0
- nvs_runtime/graph/__init__.py +3 -0
- nvs_runtime/graph/capability_graph.py +100 -0
- nvs_runtime/hidden_trajectory/__init__.py +62 -0
- nvs_runtime/hidden_trajectory/_gate_child.py +375 -0
- nvs_runtime/hidden_trajectory/adapter_loader.py +70 -0
- nvs_runtime/hidden_trajectory/canonical.py +22 -0
- nvs_runtime/hidden_trajectory/capture_archive.py +204 -0
- nvs_runtime/hidden_trajectory/capture_chain.py +319 -0
- nvs_runtime/hidden_trajectory/cli.py +765 -0
- nvs_runtime/hidden_trajectory/envelope.py +303 -0
- nvs_runtime/hidden_trajectory/formal_save.py +499 -0
- nvs_runtime/hidden_trajectory/formal_writer.py +590 -0
- nvs_runtime/hidden_trajectory/gate_client.py +460 -0
- nvs_runtime/hidden_trajectory/item_set.py +317 -0
- nvs_runtime/hidden_trajectory/k_measure.py +145 -0
- nvs_runtime/hidden_trajectory/model_catalog.py +56 -0
- nvs_runtime/hidden_trajectory/payload.py +382 -0
- nvs_runtime/hidden_trajectory/phase2/__init__.py +3 -0
- nvs_runtime/hidden_trajectory/phase2/evaluate.py +319 -0
- nvs_runtime/hidden_trajectory/phase2/geometry.py +80 -0
- nvs_runtime/hidden_trajectory/phase2/labels.py +72 -0
- nvs_runtime/hidden_trajectory/phase4/__init__.py +3 -0
- nvs_runtime/hidden_trajectory/phase4/normalize.py +171 -0
- nvs_runtime/hidden_trajectory/phase4/summary.py +270 -0
- nvs_runtime/hidden_trajectory/phase4b/__init__.py +3 -0
- nvs_runtime/hidden_trajectory/phase4b/load.py +46 -0
- nvs_runtime/hidden_trajectory/phase4b/metrics.py +96 -0
- nvs_runtime/hidden_trajectory/phase4b/scan.py +33 -0
- nvs_runtime/hidden_trajectory/record.py +334 -0
- nvs_runtime/hidden_trajectory/record_schema.py +781 -0
- nvs_runtime/hidden_trajectory/replay/__init__.py +6 -0
- nvs_runtime/hidden_trajectory/replay/risk_control.py +196 -0
- nvs_runtime/hidden_trajectory/rerun.py +302 -0
- nvs_runtime/hidden_trajectory/rsa/__init__.py +6 -0
- nvs_runtime/hidden_trajectory/rsa/analysis.py +248 -0
- nvs_runtime/hidden_trajectory/rsa/calibration.py +659 -0
- nvs_runtime/hidden_trajectory/rsa/compare.py +714 -0
- nvs_runtime/hidden_trajectory/rsa/readout.py +192 -0
- nvs_runtime/hidden_trajectory/run.py +362 -0
- nvs_runtime/hidden_trajectory/token_temporal.py +368 -0
- nvs_runtime/hidden_trajectory/window.py +344 -0
- nvs_runtime/integrations/__init__.py +4 -0
- nvs_runtime/integrations/hekb_formal.py +169 -0
- nvs_runtime/integrations/hekb_vnext.py +302 -0
- nvs_runtime/metrics/__init__.py +3 -0
- nvs_runtime/metrics/collector.py +63 -0
- nvs_runtime/observability/__init__.py +3 -0
- nvs_runtime/observability/store.py +44 -0
- nvs_runtime/planner/__init__.py +3 -0
- nvs_runtime/planner/planner.py +158 -0
- nvs_runtime/providers/__init__.py +19 -0
- nvs_runtime/providers/base.py +43 -0
- nvs_runtime/providers/edge.py +165 -0
- nvs_runtime/providers/grpc.py +58 -0
- nvs_runtime/providers/hekb.py +77 -0
- nvs_runtime/providers/http.py +59 -0
- nvs_runtime/providers/kernel.py +67 -0
- nvs_runtime/providers/local.py +88 -0
- nvs_runtime/providers/mcp.py +84 -0
- nvs_runtime/providers/registry.py +56 -0
- nvs_runtime/providers/replay.py +138 -0
- nvs_runtime/py.typed +0 -0
- nvs_runtime/security/__init__.py +4 -0
- nvs_runtime/security/cbac.py +106 -0
- nvs_runtime/security/signer.py +20 -0
- nvs_runtime/session/__init__.py +4 -0
- nvs_runtime/session/checkpoint.py +108 -0
- nvs_runtime/session/manager.py +149 -0
- nvs_runtime/transport/__init__.py +3 -0
- nvs_runtime/transport/memory.py +36 -0
- nvs_runtime-1.0.0.dist-info/METADATA +234 -0
- nvs_runtime-1.0.0.dist-info/RECORD +94 -0
- nvs_runtime-1.0.0.dist-info/WHEEL +4 -0
- nvs_runtime-1.0.0.dist-info/entry_points.txt +2 -0
- nvs_runtime-1.0.0.dist-info/licenses/LICENSE +190 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
"""Envelopes ``nvs.hidden-trajectory`` 0.1.0 (IC-00 1.1.0) from a RawObservationContract v2:
|
|
2
|
+
whether to build one, and building it -- one envelope per position.
|
|
3
|
+
|
|
4
|
+
Whether an envelope is built:
|
|
5
|
+
|
|
6
|
+
* for the whole capture (no position is built): the contract is synthetic, or does not say
|
|
7
|
+
whether it is; it failed verification; nvs_edge could not check it and its header is not that
|
|
8
|
+
of a contract v2; or it holds a value no payload can hold (``payload.session_obstacles``);
|
|
9
|
+
* for one position (the others are still built): the verification of its RAW failed, or the
|
|
10
|
+
position holds a value its payload cannot hold (``payload.position_obstacles``).
|
|
11
|
+
|
|
12
|
+
Nothing is filled in to get past a reason; every reason goes to the capture record.
|
|
13
|
+
|
|
14
|
+
The space id and the artifact ids are sensos's rules. They are not computed here: the caller
|
|
15
|
+
gives the two functions (``space_id_of``, ``seal``), which reach the sensos contracts.
|
|
16
|
+
|
|
17
|
+
Imports no numpy, no nvs_edge and no sensos contracts.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from collections.abc import Callable, Mapping
|
|
23
|
+
from dataclasses import dataclass
|
|
24
|
+
from typing import Any, Final
|
|
25
|
+
|
|
26
|
+
from nvs_observation.contract_check import NOT_VERIFIED, REJECTED, VERIFIED, ContractCheck
|
|
27
|
+
|
|
28
|
+
from nvs_runtime.__version__ import __version__
|
|
29
|
+
from nvs_runtime.hidden_trajectory import record as rec
|
|
30
|
+
from nvs_runtime.hidden_trajectory.payload import (
|
|
31
|
+
PAYLOAD_SCHEMA_ID,
|
|
32
|
+
PAYLOAD_SCHEMA_VERSION,
|
|
33
|
+
POLICY_ALL_ROWS,
|
|
34
|
+
build_payload,
|
|
35
|
+
position_obstacles,
|
|
36
|
+
session_obstacles,
|
|
37
|
+
token_index_bytes,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
IC00_VERSION: Final = "1.1.0"
|
|
41
|
+
KIND: Final = "observation"
|
|
42
|
+
PRODUCER_NAME: Final = "sensos-inference"
|
|
43
|
+
PARENT_ROLE: Final = "generated_text"
|
|
44
|
+
CONTRACT_NAME: Final = "RawObservationContract"
|
|
45
|
+
CONTRACT_VERSION: Final = 2
|
|
46
|
+
|
|
47
|
+
EXT_CONTRACT_SESSION: Final = "x-contract-session-id"
|
|
48
|
+
EXT_ASSEMBLED_BY: Final = "x-assembled-by"
|
|
49
|
+
ASSEMBLED_BY: Final = f"nvs-runtime/{__version__}"
|
|
50
|
+
|
|
51
|
+
REASON_SYNTHETIC: Final = "synthetic"
|
|
52
|
+
REASON_SYNTHETIC_NOT_STATED: Final = "synthetic_not_stated"
|
|
53
|
+
REASON_NOT_A_CONTRACT_V2: Final = "not_a_contract_v2"
|
|
54
|
+
REASON_NO_POSITIONS: Final = "no_positions"
|
|
55
|
+
REASON_G1: Final = "g1"
|
|
56
|
+
|
|
57
|
+
_FAIL: Final = "FAIL"
|
|
58
|
+
_PLACEHOLDER_CONTENT: Final = "cnt:" + "0" * 64
|
|
59
|
+
_PLACEHOLDER_ARTIFACT: Final = "art:" + "0" * 64
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True, slots=True)
|
|
63
|
+
class EnvelopeIds:
|
|
64
|
+
"""What the runtime that drives the capture decides: the ids, the time, the sequence."""
|
|
65
|
+
|
|
66
|
+
session_id: str
|
|
67
|
+
observation_id: str
|
|
68
|
+
produced_at: str
|
|
69
|
+
next_seq: Callable[[], int]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True, slots=True)
|
|
73
|
+
class ParentRef:
|
|
74
|
+
"""The ``generated_text`` artifact of the same observation, already stored."""
|
|
75
|
+
|
|
76
|
+
artifact_id: str
|
|
77
|
+
content_id: str
|
|
78
|
+
seq: int
|
|
79
|
+
observation_id: str
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True, slots=True)
|
|
83
|
+
class BuildResult:
|
|
84
|
+
"""``envelopes``: point -> sealed envelope. ``blobs``: uri -> bytes, for the Gate and the
|
|
85
|
+
store (``token_index`` always; ``points`` where the rows were given). ``record``: the
|
|
86
|
+
capture record."""
|
|
87
|
+
|
|
88
|
+
envelopes: dict[str, dict[str, Any]]
|
|
89
|
+
blobs: dict[str, bytes]
|
|
90
|
+
record: dict[str, Any]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _synthetic_refusal(document: Any) -> str | None:
|
|
94
|
+
origin = document.get("origin") if isinstance(document, Mapping) else None
|
|
95
|
+
value = origin.get("synthetic", None) if isinstance(origin, Mapping) else None
|
|
96
|
+
if value is True:
|
|
97
|
+
return REASON_SYNTHETIC
|
|
98
|
+
return None if value is False else REASON_SYNTHETIC_NOT_STATED
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def session_refusals(document: Any, check: ContractCheck) -> tuple[str, ...]:
|
|
102
|
+
"""Every reason no envelope is built for any position of ``document`` (empty: none)."""
|
|
103
|
+
out: list[str] = []
|
|
104
|
+
synthetic = _synthetic_refusal(document)
|
|
105
|
+
if synthetic is not None:
|
|
106
|
+
out.append(synthetic)
|
|
107
|
+
if check.status == REJECTED:
|
|
108
|
+
out.extend(check.reasons)
|
|
109
|
+
elif check.status == NOT_VERIFIED:
|
|
110
|
+
header = document.get("contract") if isinstance(document, Mapping) else None
|
|
111
|
+
if header != {"name": CONTRACT_NAME, "version": CONTRACT_VERSION}:
|
|
112
|
+
out.append(REASON_NOT_A_CONTRACT_V2)
|
|
113
|
+
elif check.status != VERIFIED:
|
|
114
|
+
raise ValueError(f"unknown contract status: {check.status!r}")
|
|
115
|
+
out.extend(session_obstacles(document))
|
|
116
|
+
if not rec.points_of_contract(
|
|
117
|
+
document.get("positions") if isinstance(document, Mapping) else None
|
|
118
|
+
):
|
|
119
|
+
out.append(REASON_NO_POSITIONS)
|
|
120
|
+
return tuple(out)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def contract_ref_of(check: ContractCheck) -> dict[str, Any] | None:
|
|
124
|
+
"""The payload's ``contract_ref``: the verified hash, else the recorded one, else ``None``."""
|
|
125
|
+
if check.status == REJECTED or check.contract_sha256 is None:
|
|
126
|
+
return None
|
|
127
|
+
return {
|
|
128
|
+
"name": CONTRACT_NAME,
|
|
129
|
+
"version": CONTRACT_VERSION,
|
|
130
|
+
"contract_sha256": check.contract_sha256,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _g1_failures(check: ContractCheck, point: str) -> list[str]:
|
|
135
|
+
"""``g1:<check>`` for every check of the position's RAW that failed in the report."""
|
|
136
|
+
if check.report is None:
|
|
137
|
+
return []
|
|
138
|
+
position = check.report.get("G1", {}).get("positions", {}).get(point, {})
|
|
139
|
+
checks = position.get("checks", {})
|
|
140
|
+
return [f"{REASON_G1}:{name}" for name, result in checks.items() if result == _FAIL]
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _extensions(
|
|
144
|
+
document: Mapping[str, Any],
|
|
145
|
+
point: str,
|
|
146
|
+
session_extensions: Mapping[str, Any] | None,
|
|
147
|
+
extensions: Mapping[str, Mapping[str, Any]] | None,
|
|
148
|
+
) -> dict[str, Any]:
|
|
149
|
+
out: dict[str, Any] = {
|
|
150
|
+
EXT_CONTRACT_SESSION: document.get("session_id"),
|
|
151
|
+
EXT_ASSEMBLED_BY: ASSEMBLED_BY,
|
|
152
|
+
}
|
|
153
|
+
for given in (session_extensions or {}, (extensions or {}).get(point, {})):
|
|
154
|
+
for key, value in given.items():
|
|
155
|
+
if not isinstance(key, str) or not key.startswith("x-"):
|
|
156
|
+
raise ValueError(f"an extension key starts with 'x-': {key!r}")
|
|
157
|
+
if key in (EXT_CONTRACT_SESSION, EXT_ASSEMBLED_BY):
|
|
158
|
+
raise ValueError(f"the extension {key!r} is written by nvs-runtime")
|
|
159
|
+
out[key] = value
|
|
160
|
+
return out
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def build_envelope(
|
|
164
|
+
document: Mapping[str, Any],
|
|
165
|
+
payload: Mapping[str, Any],
|
|
166
|
+
*,
|
|
167
|
+
ids: EnvelopeIds,
|
|
168
|
+
space_id: str,
|
|
169
|
+
seal: Callable[[dict[str, Any], Mapping[str, Any]], dict[str, Any]],
|
|
170
|
+
parent: ParentRef | None = None,
|
|
171
|
+
producer_build: str | None = None,
|
|
172
|
+
extensions: Mapping[str, Any] | None = None,
|
|
173
|
+
) -> dict[str, Any]:
|
|
174
|
+
"""The sealed envelope of one payload. ``seal(envelope, lookup)`` writes the two ids.
|
|
175
|
+
|
|
176
|
+
``producer.version`` is the capture package's version and ``producer.build`` the adapter
|
|
177
|
+
(unless ``producer_build`` is given): both enter the ids, so they come from the contract and
|
|
178
|
+
not from this assembler, whose version goes to the extensions.
|
|
179
|
+
"""
|
|
180
|
+
capture = document["origin"]["capture"]
|
|
181
|
+
parents: list[dict[str, Any]] = []
|
|
182
|
+
lookup: dict[str, Any] = {}
|
|
183
|
+
if parent is not None:
|
|
184
|
+
parents.append(
|
|
185
|
+
{"role": PARENT_ROLE, "ordered": False, "artifact_ids": [parent.artifact_id]}
|
|
186
|
+
)
|
|
187
|
+
lookup[parent.artifact_id] = {
|
|
188
|
+
"content_id": parent.content_id,
|
|
189
|
+
"seq": parent.seq,
|
|
190
|
+
"observation_id": parent.observation_id,
|
|
191
|
+
}
|
|
192
|
+
envelope: dict[str, Any] = {
|
|
193
|
+
"contract": {"id": "IC-00", "version": IC00_VERSION},
|
|
194
|
+
"content_id": _PLACEHOLDER_CONTENT,
|
|
195
|
+
"artifact_id": _PLACEHOLDER_ARTIFACT,
|
|
196
|
+
"kind": KIND,
|
|
197
|
+
"payload_schema": {"id": PAYLOAD_SCHEMA_ID, "version": PAYLOAD_SCHEMA_VERSION},
|
|
198
|
+
"payload": dict(payload),
|
|
199
|
+
"producer": {
|
|
200
|
+
"name": PRODUCER_NAME,
|
|
201
|
+
"version": capture["version"],
|
|
202
|
+
"build": capture["adapter"] if producer_build is None else producer_build,
|
|
203
|
+
},
|
|
204
|
+
"session_id": ids.session_id,
|
|
205
|
+
"observation_id": ids.observation_id,
|
|
206
|
+
"event_span": None,
|
|
207
|
+
"space_id": space_id,
|
|
208
|
+
"parents": parents,
|
|
209
|
+
"policy_refs": [],
|
|
210
|
+
"calibration_ref": None,
|
|
211
|
+
"rule_refs": [],
|
|
212
|
+
"observed_at": None,
|
|
213
|
+
"seq": ids.next_seq(),
|
|
214
|
+
"produced_at": ids.produced_at,
|
|
215
|
+
"extensions": dict(extensions or {}),
|
|
216
|
+
}
|
|
217
|
+
return seal(envelope, lookup)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def build_envelopes(
|
|
221
|
+
document: Mapping[str, Any],
|
|
222
|
+
check: ContractCheck,
|
|
223
|
+
*,
|
|
224
|
+
ids: EnvelopeIds,
|
|
225
|
+
space_id_of: Callable[[Mapping[str, Any]], str],
|
|
226
|
+
seal: Callable[[dict[str, Any], Mapping[str, Any]], dict[str, Any]],
|
|
227
|
+
rows: Mapping[str, bytes] | None = None,
|
|
228
|
+
parent: ParentRef | None = None,
|
|
229
|
+
producer_build: str | None = None,
|
|
230
|
+
session_extensions: Mapping[str, Any] | None = None,
|
|
231
|
+
extensions: Mapping[str, Mapping[str, Any]] | None = None,
|
|
232
|
+
copied: bool = False,
|
|
233
|
+
policy_name: str = POLICY_ALL_ROWS,
|
|
234
|
+
) -> BuildResult:
|
|
235
|
+
"""One envelope per position of ``document`` that can be built, and the capture record.
|
|
236
|
+
|
|
237
|
+
``check`` is ``check_contract_v2(document, ...)``. ``rows`` (point -> the rows' bytes) is
|
|
238
|
+
optional: without it the envelope refers to the rows by the contract's ``trajectory_id``.
|
|
239
|
+
``space_id_of(materials)`` and ``seal(envelope, lookup)`` reach the sensos contracts; they are
|
|
240
|
+
not called for a capture that is refused. ``extensions`` is keyed by point,
|
|
241
|
+
``session_extensions`` goes to every envelope.
|
|
242
|
+
"""
|
|
243
|
+
points = rec.points_of_contract(
|
|
244
|
+
document.get("positions") if isinstance(document, Mapping) else None
|
|
245
|
+
)
|
|
246
|
+
refusals = session_refusals(document, check)
|
|
247
|
+
envelopes: dict[str, dict[str, Any]] = {}
|
|
248
|
+
blobs: dict[str, bytes] = {}
|
|
249
|
+
entries: dict[str, dict[str, Any]] = {}
|
|
250
|
+
contract_ref = contract_ref_of(check)
|
|
251
|
+
for point in points:
|
|
252
|
+
if point in entries:
|
|
253
|
+
continue # a second position of the same name: the first one stands
|
|
254
|
+
if refusals:
|
|
255
|
+
entries[point] = rec.position_entry(
|
|
256
|
+
rec.POSITION_NOT_BUILT, (rec.REASON_SESSION_REFUSED,)
|
|
257
|
+
)
|
|
258
|
+
continue
|
|
259
|
+
failed = _g1_failures(check, point)
|
|
260
|
+
given = None if rows is None or failed else rows.get(point)
|
|
261
|
+
reasons = [*failed, *position_obstacles(document, point, rows_bytes=given)]
|
|
262
|
+
if reasons:
|
|
263
|
+
entries[point] = rec.position_entry(rec.POSITION_NOT_BUILT, reasons)
|
|
264
|
+
continue
|
|
265
|
+
payload = build_payload(
|
|
266
|
+
document,
|
|
267
|
+
point,
|
|
268
|
+
contract_ref=contract_ref,
|
|
269
|
+
rows_bytes=given,
|
|
270
|
+
copied=copied,
|
|
271
|
+
policy_name=policy_name,
|
|
272
|
+
)
|
|
273
|
+
position = next(p for p in document["positions"] if p.get("point") == point)
|
|
274
|
+
envelope = build_envelope(
|
|
275
|
+
document,
|
|
276
|
+
payload,
|
|
277
|
+
ids=ids,
|
|
278
|
+
space_id=space_id_of(dict(position["space"])),
|
|
279
|
+
seal=seal,
|
|
280
|
+
parent=parent,
|
|
281
|
+
producer_build=producer_build,
|
|
282
|
+
extensions=_extensions(document, point, session_extensions, extensions),
|
|
283
|
+
)
|
|
284
|
+
envelopes[point] = envelope
|
|
285
|
+
blobs[payload["token_index"]["uri"]] = token_index_bytes(payload["length"])
|
|
286
|
+
if given is not None:
|
|
287
|
+
blobs[payload["points"]["uri"]] = given
|
|
288
|
+
entries[point] = rec.position_entry(
|
|
289
|
+
rec.POSITION_ENVELOPED, artifact_id=envelope["artifact_id"]
|
|
290
|
+
)
|
|
291
|
+
contract_session = document.get("session_id") if isinstance(document, Mapping) else None
|
|
292
|
+
record = rec.build_record(
|
|
293
|
+
session_id=ids.session_id,
|
|
294
|
+
observation_id=ids.observation_id,
|
|
295
|
+
contract_session_id=contract_session if isinstance(contract_session, str) else None,
|
|
296
|
+
capture=rec.capture_block_of_contract(document),
|
|
297
|
+
contract_status=check.status,
|
|
298
|
+
contract_sha256=check.contract_sha256,
|
|
299
|
+
hash_source=check.hash_source,
|
|
300
|
+
refusals=refusals,
|
|
301
|
+
positions=entries,
|
|
302
|
+
)
|
|
303
|
+
return BuildResult(envelopes=envelopes, blobs=blobs, record=record)
|