@infernetprotocol/liquid-agent 0.0.0-stage → 0.1.67

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.
Files changed (58) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +15 -2
  3. package/fixtures/contract.json +6 -0
  4. package/fixtures/invalid/episode-extra-field.json +275 -0
  5. package/fixtures/invalid/episode-no-capture.json +265 -0
  6. package/fixtures/invalid/episode-unknown-action.json +274 -0
  7. package/fixtures/invalid/model-manifest-bad-hash.json +92 -0
  8. package/fixtures/invalid/predict-negative-seq.json +48 -0
  9. package/fixtures/invalid/proposal-bad-abstain.json +31 -0
  10. package/fixtures/invalid/proposal-confidence-range.json +31 -0
  11. package/fixtures/invalid/session-bad-seq.json +17 -0
  12. package/fixtures/invalid/train-job-bad-family.json +44 -0
  13. package/fixtures/invalid/train-job-zero-lr.json +44 -0
  14. package/fixtures/valid/action-proposal.json +27 -0
  15. package/fixtures/valid/dataset-bundle.json +3747 -0
  16. package/fixtures/valid/dataset-manifest.json +126 -0
  17. package/fixtures/valid/episode.json +270 -0
  18. package/fixtures/valid/inference-session.json +13 -0
  19. package/fixtures/valid/model-manifest.json +88 -0
  20. package/fixtures/valid/predict-request.json +44 -0
  21. package/fixtures/valid/train-job.json +40 -0
  22. package/package.json +28 -3
  23. package/runtime/infernet_liquid/__init__.py +6 -0
  24. package/runtime/infernet_liquid/__main__.py +95 -0
  25. package/runtime/infernet_liquid/bench.py +125 -0
  26. package/runtime/infernet_liquid/bundle.py +63 -0
  27. package/runtime/infernet_liquid/canonical.py +75 -0
  28. package/runtime/infernet_liquid/data.py +121 -0
  29. package/runtime/infernet_liquid/errors.py +13 -0
  30. package/runtime/infernet_liquid/evaluate.py +145 -0
  31. package/runtime/infernet_liquid/export.py +136 -0
  32. package/runtime/infernet_liquid/features.py +105 -0
  33. package/runtime/infernet_liquid/models.py +63 -0
  34. package/runtime/infernet_liquid/schema.py +155 -0
  35. package/runtime/infernet_liquid/secretscan.py +64 -0
  36. package/runtime/infernet_liquid/server.py +251 -0
  37. package/runtime/infernet_liquid/sessions.py +279 -0
  38. package/runtime/infernet_liquid/synth.py +194 -0
  39. package/runtime/infernet_liquid/train.py +136 -0
  40. package/runtime/pyproject.toml +14 -0
  41. package/runtime/requirements-cuda.lock +461 -0
  42. package/runtime/requirements.lock +344 -0
  43. package/schemas/action-proposal.schema.json +40 -0
  44. package/schemas/common.schema.json +88 -0
  45. package/schemas/dataset-bundle.schema.json +15 -0
  46. package/schemas/dataset-manifest.schema.json +100 -0
  47. package/schemas/episode.schema.json +71 -0
  48. package/schemas/inference-session.schema.json +23 -0
  49. package/schemas/model-manifest.schema.json +103 -0
  50. package/schemas/predict-request.schema.json +20 -0
  51. package/schemas/train-job.schema.json +75 -0
  52. package/src/canonical.js +49 -0
  53. package/src/contract.js +62 -0
  54. package/src/dataset.js +133 -0
  55. package/src/index.js +13 -0
  56. package/src/runtime.js +206 -0
  57. package/src/secrets.js +79 -0
  58. package/src/validate.js +161 -0
@@ -0,0 +1,71 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://infernetprotocol.com/schemas/liquid-agent/v1/episode.schema.json",
4
+ "title": "EpisodeV1",
5
+ "description": "One consented, redacted demonstration of a single task. content_hash is sha256 over the canonical JSON of the episode without content_hash.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["schema_version", "kind", "episode_id", "task", "created_at", "capture", "steps", "labels", "content_hash"],
9
+ "properties": {
10
+ "schema_version": { "$ref": "common.schema.json#/$defs/schemaVersion" },
11
+ "kind": { "const": "episode" },
12
+ "episode_id": { "type": "string", "pattern": "^ep_[A-Za-z0-9_-]{4,64}$" },
13
+ "task": { "$ref": "common.schema.json#/$defs/taskId" },
14
+ "created_at": { "$ref": "common.schema.json#/$defs/timestamp" },
15
+ "split_group": {
16
+ "description": "Opaque grouping key (recording session or hashed account) used so one group never spans train and test.",
17
+ "type": "string",
18
+ "maxLength": 128
19
+ },
20
+ "variant": { "description": "UI layout variant label, for generalisation reports.", "type": "string", "maxLength": 64 },
21
+ "capture": {
22
+ "type": "object",
23
+ "additionalProperties": false,
24
+ "required": ["consent_id", "redaction_profile", "device_local", "source"],
25
+ "properties": {
26
+ "consent_id": { "type": "string", "minLength": 1, "maxLength": 128 },
27
+ "redaction_profile": { "enum": ["strict", "standard"] },
28
+ "device_local": { "type": "boolean" },
29
+ "source": { "enum": ["w3bs-desktop", "synthetic", "import"] },
30
+ "app_scope": { "type": "array", "maxItems": 32, "items": { "type": "string", "maxLength": 64 } }
31
+ }
32
+ },
33
+ "steps": {
34
+ "type": "array",
35
+ "minItems": 1,
36
+ "maxItems": 512,
37
+ "items": {
38
+ "type": "object",
39
+ "additionalProperties": false,
40
+ "required": ["t_ms", "observation", "action", "result"],
41
+ "properties": {
42
+ "t_ms": { "type": "integer", "minimum": 0 },
43
+ "observation": { "$ref": "common.schema.json#/$defs/observation" },
44
+ "action": { "$ref": "common.schema.json#/$defs/action" },
45
+ "result": {
46
+ "type": "object",
47
+ "additionalProperties": false,
48
+ "required": ["status"],
49
+ "properties": {
50
+ "status": { "enum": ["ok", "error", "blocked", "skipped"] },
51
+ "error": { "type": "string", "maxLength": 500 }
52
+ }
53
+ }
54
+ }
55
+ }
56
+ },
57
+ "labels": {
58
+ "type": "object",
59
+ "additionalProperties": false,
60
+ "required": ["success", "reviewed", "risk"],
61
+ "properties": {
62
+ "success": { "type": "boolean" },
63
+ "reviewed": { "type": "boolean" },
64
+ "risk": { "enum": ["low", "medium", "high"] },
65
+ "goal": { "type": "string", "maxLength": 500 },
66
+ "failure_reason": { "type": "string", "maxLength": 500 }
67
+ }
68
+ },
69
+ "content_hash": { "$ref": "common.schema.json#/$defs/sha256" }
70
+ }
71
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://infernetprotocol.com/schemas/liquid-agent/v1/inference-session.schema.json",
4
+ "title": "InferenceSessionV1",
5
+ "description": "A sticky, stateful inference session on one serving worker. sequence_index is the last accepted step (-1 before the first); the next request must carry sequence_index + 1.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["schema_version", "kind", "session_id", "model_hash", "created_at", "expires_at", "ttl_s", "sequence_index", "state"],
9
+ "properties": {
10
+ "schema_version": { "$ref": "common.schema.json#/$defs/schemaVersion" },
11
+ "kind": { "const": "inference_session" },
12
+ "session_id": { "type": "string", "pattern": "^[A-Za-z0-9_.:-]{1,128}$" },
13
+ "model_hash": { "$ref": "common.schema.json#/$defs/sha256" },
14
+ "created_at": { "$ref": "common.schema.json#/$defs/timestamp" },
15
+ "expires_at": { "$ref": "common.schema.json#/$defs/timestamp" },
16
+ "ttl_s": { "type": "integer", "minimum": 1, "maximum": 86400 },
17
+ "sequence_index": { "type": "integer", "minimum": -1 },
18
+ "state": { "enum": ["active", "closed", "expired"] },
19
+ "checkpoint_ref": { "$ref": "common.schema.json#/$defs/sha256OrNull" },
20
+ "worker": { "description": "Serving worker identity for affinity (peer id or 'local').", "type": "string", "maxLength": 128 },
21
+ "account_scope": { "description": "Opaque hash of the account/tenant; sessions never share state across scopes.", "type": "string", "maxLength": 128 }
22
+ }
23
+ }
@@ -0,0 +1,103 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://infernetprotocol.com/schemas/liquid-agent/v1/model-manifest.schema.json",
4
+ "title": "ModelManifestV1",
5
+ "description": "Describes one exported temporal policy. The model is identified everywhere by weights.weights_hash (the model hash). manifest_hash covers every field except manifest_hash and signature.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["schema_version", "kind", "model_id", "created_at", "architecture", "weights", "runtime", "input_schema", "output_schema", "training", "eval", "license", "manifest_hash"],
9
+ "properties": {
10
+ "schema_version": { "$ref": "common.schema.json#/$defs/schemaVersion" },
11
+ "kind": { "const": "model_manifest" },
12
+ "model_id": { "type": "string", "pattern": "^lm_[A-Za-z0-9_-]{4,64}$" },
13
+ "name": { "type": "string", "maxLength": 160 },
14
+ "created_at": { "$ref": "common.schema.json#/$defs/timestamp" },
15
+ "architecture": {
16
+ "type": "object",
17
+ "additionalProperties": false,
18
+ "required": ["family", "hidden_units", "feature_dim", "params"],
19
+ "properties": {
20
+ "family": { "$ref": "common.schema.json#/$defs/family" },
21
+ "hidden_units": { "type": "integer", "minimum": 1 },
22
+ "feature_dim": { "type": "integer", "minimum": 1 },
23
+ "backbone_units": { "type": "integer", "minimum": 1 },
24
+ "params": { "type": "integer", "minimum": 1 },
25
+ "research": { "type": "boolean" }
26
+ }
27
+ },
28
+ "weights": {
29
+ "type": "object",
30
+ "additionalProperties": false,
31
+ "required": ["format", "file", "weights_hash", "size_bytes"],
32
+ "properties": {
33
+ "format": { "const": "safetensors" },
34
+ "file": { "type": "string", "pattern": "^[A-Za-z0-9._-]+$" },
35
+ "weights_hash": { "$ref": "common.schema.json#/$defs/sha256" },
36
+ "size_bytes": { "type": "integer", "minimum": 1 }
37
+ }
38
+ },
39
+ "runtime": { "$ref": "common.schema.json#/$defs/runtimeRef" },
40
+ "input_schema": {
41
+ "type": "object",
42
+ "additionalProperties": false,
43
+ "required": ["encoding", "feature_dim", "time_delta"],
44
+ "properties": {
45
+ "encoding": { "const": "hashed-bag/v1" },
46
+ "feature_dim": { "type": "integer", "minimum": 1 },
47
+ "time_delta": { "const": "log1p_seconds" },
48
+ "tasks": { "type": "array", "items": { "$ref": "common.schema.json#/$defs/taskId" } }
49
+ }
50
+ },
51
+ "output_schema": {
52
+ "type": "object",
53
+ "additionalProperties": false,
54
+ "required": ["action_types", "targets"],
55
+ "properties": {
56
+ "action_types": { "type": "array", "minItems": 1, "items": { "$ref": "common.schema.json#/$defs/actionType" } },
57
+ "targets": { "type": "array", "minItems": 1, "items": { "type": "string", "maxLength": 230 } }
58
+ }
59
+ },
60
+ "training": {
61
+ "type": "object",
62
+ "additionalProperties": false,
63
+ "required": ["dataset_hash", "seed", "epochs", "device", "started_at", "finished_at"],
64
+ "properties": {
65
+ "train_job_id": { "type": "string", "pattern": "^tj_[A-Za-z0-9_-]{4,64}$" },
66
+ "dataset_hash": { "$ref": "common.schema.json#/$defs/sha256" },
67
+ "seed": { "type": "integer", "minimum": 0 },
68
+ "epochs": { "type": "integer", "minimum": 1 },
69
+ "learning_rate": { "type": "number" },
70
+ "device": { "type": "string", "maxLength": 32 },
71
+ "started_at": { "$ref": "common.schema.json#/$defs/timestamp" },
72
+ "finished_at": { "$ref": "common.schema.json#/$defs/timestamp" },
73
+ "train_seconds": { "type": "number", "minimum": 0 }
74
+ }
75
+ },
76
+ "eval": {
77
+ "type": "object",
78
+ "additionalProperties": false,
79
+ "required": ["split", "metrics"],
80
+ "properties": {
81
+ "split": { "enum": ["validation", "test"] },
82
+ "episodes": { "type": "integer", "minimum": 0 },
83
+ "metrics": {
84
+ "type": "object",
85
+ "additionalProperties": { "type": "number" },
86
+ "required": ["action_accuracy", "target_accuracy", "step_accuracy", "episode_success"]
87
+ }
88
+ }
89
+ },
90
+ "license": { "type": "string", "minLength": 1, "maxLength": 64 },
91
+ "manifest_hash": { "$ref": "common.schema.json#/$defs/sha256" },
92
+ "signature": {
93
+ "type": "object",
94
+ "additionalProperties": false,
95
+ "required": ["alg", "public_key", "sig"],
96
+ "properties": {
97
+ "alg": { "const": "ed25519" },
98
+ "public_key": { "type": "string", "maxLength": 128 },
99
+ "sig": { "type": "string", "maxLength": 256 }
100
+ }
101
+ }
102
+ }
103
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://infernetprotocol.com/schemas/liquid-agent/v1/predict-request.schema.json",
4
+ "title": "PredictRequestV1",
5
+ "description": "One observed step sent to a session. prev_action is what was actually executed after the previous proposal (null at the start).",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["schema_version", "kind", "session_id", "sequence_index", "dt_ms", "observation"],
9
+ "properties": {
10
+ "schema_version": { "$ref": "common.schema.json#/$defs/schemaVersion" },
11
+ "kind": { "const": "predict_request" },
12
+ "session_id": { "type": "string", "pattern": "^[A-Za-z0-9_.:-]{1,128}$" },
13
+ "model_hash": { "$ref": "common.schema.json#/$defs/sha256" },
14
+ "sequence_index": { "type": "integer", "minimum": 0 },
15
+ "dt_ms": { "type": "integer", "minimum": 0 },
16
+ "task": { "$ref": "common.schema.json#/$defs/taskId" },
17
+ "observation": { "$ref": "common.schema.json#/$defs/observation" },
18
+ "prev_action": { "anyOf": [{ "$ref": "common.schema.json#/$defs/action" }, { "type": "null" }] }
19
+ }
20
+ }
@@ -0,0 +1,75 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://infernetprotocol.com/schemas/liquid-agent/v1/train-job.schema.json",
4
+ "title": "TrainJobV1",
5
+ "description": "A single-node training request for workload infernet.temporal.train.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": ["schema_version", "kind", "job_id", "idempotency_key", "created_at", "dataset_hash", "model_type", "hyperparameters", "resources", "limits", "privacy"],
9
+ "properties": {
10
+ "schema_version": { "$ref": "common.schema.json#/$defs/schemaVersion" },
11
+ "kind": { "const": "train_job" },
12
+ "job_id": { "type": "string", "pattern": "^tj_[A-Za-z0-9_-]{4,64}$" },
13
+ "idempotency_key": { "type": "string", "minLength": 8, "maxLength": 128 },
14
+ "created_at": { "$ref": "common.schema.json#/$defs/timestamp" },
15
+ "dataset_hash": { "$ref": "common.schema.json#/$defs/sha256" },
16
+ "dataset_ref": { "description": "Where the worker reads the bundle: a local path, https URL or c0mpute:// content address. Hash-verified before use.", "type": "string", "maxLength": 2048 },
17
+ "model_type": { "$ref": "common.schema.json#/$defs/family" },
18
+ "baselines": { "type": "array", "uniqueItems": true, "maxItems": 3, "items": { "$ref": "common.schema.json#/$defs/family" } },
19
+ "hyperparameters": {
20
+ "type": "object",
21
+ "additionalProperties": false,
22
+ "required": ["hidden_units", "epochs", "learning_rate", "seed"],
23
+ "properties": {
24
+ "hidden_units": { "type": "integer", "minimum": 4, "maximum": 1024 },
25
+ "epochs": { "type": "integer", "minimum": 1, "maximum": 1000 },
26
+ "learning_rate": { "type": "number", "exclusiveMinimum": 0, "maximum": 1 },
27
+ "batch_size": { "type": "integer", "minimum": 1, "maximum": 4096 },
28
+ "seed": { "type": "integer", "minimum": 0 },
29
+ "feature_dim": { "type": "integer", "minimum": 32, "maximum": 8192 },
30
+ "backbone_units": { "type": "integer", "minimum": 4, "maximum": 1024 },
31
+ "early_stop_patience": { "type": "integer", "minimum": 0, "maximum": 1000 }
32
+ }
33
+ },
34
+ "resources": {
35
+ "type": "object",
36
+ "additionalProperties": false,
37
+ "required": ["device"],
38
+ "properties": {
39
+ "device": { "enum": ["auto", "cpu", "cuda", "mps"] },
40
+ "gpu_required": { "type": "boolean" },
41
+ "min_ram_mb": { "type": "integer", "minimum": 0 },
42
+ "max_threads": { "type": "integer", "minimum": 1, "maximum": 512 }
43
+ }
44
+ },
45
+ "limits": {
46
+ "type": "object",
47
+ "additionalProperties": false,
48
+ "required": ["timeout_s", "max_retries", "max_budget_usd"],
49
+ "properties": {
50
+ "timeout_s": { "type": "integer", "minimum": 1, "maximum": 604800 },
51
+ "max_retries": { "type": "integer", "minimum": 0, "maximum": 5 },
52
+ "max_budget_usd": { "type": "number", "minimum": 0 }
53
+ }
54
+ },
55
+ "privacy": {
56
+ "type": "object",
57
+ "additionalProperties": false,
58
+ "required": ["mode", "upload_approved"],
59
+ "properties": {
60
+ "mode": { "enum": ["local_only", "trusted_worker"] },
61
+ "upload_approved": { "type": "boolean" },
62
+ "worker_allowlist": { "type": "array", "maxItems": 64, "items": { "type": "string", "maxLength": 128 } }
63
+ }
64
+ },
65
+ "retention": {
66
+ "type": "object",
67
+ "additionalProperties": false,
68
+ "properties": {
69
+ "artifact_days": { "anyOf": [{ "type": "integer", "minimum": 0 }, { "type": "null" }] },
70
+ "delete_dataset_after": { "type": "boolean" }
71
+ }
72
+ },
73
+ "runtime": { "$ref": "common.schema.json#/$defs/runtimeRef" }
74
+ }
75
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Canonical JSON + content hashes for liquid-agent/v1.
3
+ *
4
+ * Canonical form: object keys sorted, no whitespace, numbers as JSON.stringify
5
+ * prints them, non-finite numbers rejected. The Python runtime
6
+ * (runtime/infernet_liquid/canonical.py) produces byte-identical output; the
7
+ * fixtures pin hashes both sides check.
8
+ */
9
+
10
+ import { createHash } from "node:crypto";
11
+
12
+ export function canonicalize(value) {
13
+ if (value === null) return "null";
14
+ switch (typeof value) {
15
+ case "boolean":
16
+ return value ? "true" : "false";
17
+ case "number":
18
+ if (!Number.isFinite(value)) throw new TypeError("canonical JSON: non-finite number");
19
+ return JSON.stringify(value);
20
+ case "string":
21
+ return JSON.stringify(value);
22
+ case "object": {
23
+ if (Array.isArray(value)) return `[${value.map(canonicalize).join(",")}]`;
24
+ const keys = Object.keys(value).filter((k) => value[k] !== undefined).sort();
25
+ return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalize(value[k])}`).join(",")}}`;
26
+ }
27
+ default:
28
+ throw new TypeError(`canonical JSON: unsupported type ${typeof value}`);
29
+ }
30
+ }
31
+
32
+ export function sha256Hex(data) {
33
+ return createHash("sha256").update(data).digest("hex");
34
+ }
35
+
36
+ export function hashCanonical(value) {
37
+ return `sha256:${sha256Hex(canonicalize(value))}`;
38
+ }
39
+
40
+ /** Hash of an object with the named self-referencing fields left out. */
41
+ export function hashWithout(value, fields) {
42
+ const copy = { ...value };
43
+ for (const f of fields) delete copy[f];
44
+ return hashCanonical(copy);
45
+ }
46
+
47
+ export const episodeHash = (episode) => hashWithout(episode, ["content_hash"]);
48
+ export const datasetHash = (manifest) => hashCanonical({ episodes: manifest.episodes, splits: manifest.splits });
49
+ export const manifestHash = (manifest) => hashWithout(manifest, ["manifest_hash", "signature"]);
@@ -0,0 +1,62 @@
1
+ /**
2
+ * liquid-agent/v1 constants shared by the CLI, the training backend, c0mpute
3
+ * and W3BS. Workload ids follow c0mpute's `<plugin>.<family>.<op>` naming.
4
+ */
5
+
6
+ export const SCHEMA_VERSION = "liquid-agent/v1";
7
+
8
+ /** Runtime protocol spoken over the infernet-liquid subprocess's stdio. */
9
+ export const RUNTIME_PROTOCOL_VERSION = 1;
10
+
11
+ export const WORKLOADS = Object.freeze({
12
+ TRAIN: "infernet.temporal.train",
13
+ INFER: "infernet.temporal.infer"
14
+ });
15
+
16
+ export const MODEL_FAMILY = "temporal-policy";
17
+ export const FAMILIES = Object.freeze(["cfc", "ltc", "gru"]);
18
+ /** LTC is slow to train (ODE unfolds) and kept for research comparisons. */
19
+ export const RESEARCH_FAMILIES = Object.freeze(["ltc"]);
20
+
21
+ export const ACTION_TYPES = Object.freeze([
22
+ "click", "double_click", "type", "key", "scroll", "navigate", "select", "focus",
23
+ "wait", "submit", "send", "post", "publish", "delete", "done"
24
+ ]);
25
+
26
+ /**
27
+ * Actions with an effect outside the local UI: posting, sending, deleting,
28
+ * submitting a form. These always need a person's approval at action time
29
+ * unless a narrower, separately authorised rule says otherwise.
30
+ */
31
+ export const EXTERNAL_WRITE_ACTIONS = Object.freeze(["submit", "send", "post", "publish", "delete"]);
32
+
33
+ /** Ops of the runtime contract, mapped to the PRD adapter names. */
34
+ export const RUNTIME_OPS = Object.freeze([
35
+ "health", "capabilities", "validate", "train", "evaluate", "load", "unload",
36
+ "open_session", "predict", "reset", "close_session", "checkpoint", "restore", "shutdown"
37
+ ]);
38
+
39
+ /** Failure taxonomy. `retriable` tells a dispatcher whether another try can help. */
40
+ export const ERROR_CODES = Object.freeze({
41
+ E_BAD_REQUEST: { retriable: false },
42
+ E_SCHEMA: { retriable: false },
43
+ E_HASH_MISMATCH: { retriable: false },
44
+ E_SECRET_DETECTED: { retriable: false },
45
+ E_CONSENT: { retriable: false },
46
+ E_MODEL_HASH: { retriable: false },
47
+ E_NOT_LOADED: { retriable: false },
48
+ E_SESSION_UNKNOWN: { retriable: false },
49
+ E_SESSION_EXPIRED: { retriable: false },
50
+ E_SEQ_DUPLICATE: { retriable: false },
51
+ E_SEQ_LATE: { retriable: false },
52
+ E_SEQ_GAP: { retriable: false },
53
+ E_CAPACITY: { retriable: true },
54
+ E_TIMEOUT: { retriable: true },
55
+ E_CANCELLED: { retriable: false },
56
+ E_RUNTIME_MISSING: { retriable: false },
57
+ E_RUNTIME: { retriable: true }
58
+ });
59
+
60
+ export function isExternalWrite(action) {
61
+ return !!action && EXTERNAL_WRITE_ACTIONS.includes(action.type);
62
+ }
package/src/dataset.js ADDED
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Build liquid-agent/v1 episodes, manifests and bundles.
3
+ *
4
+ * Splits are assigned per split_group (recording session, account or variant)
5
+ * so one group never straddles train and test, deterministically from a seed.
6
+ */
7
+
8
+ import { SCHEMA_VERSION } from "./contract.js";
9
+ import { datasetHash, episodeHash, sha256Hex } from "./canonical.js";
10
+ import { assertNoSecrets, SCANNER_VERSION } from "./secrets.js";
11
+ import { assertValid } from "./validate.js";
12
+
13
+ export function sealEpisode(episode) {
14
+ const e = { ...episode, schema_version: SCHEMA_VERSION, kind: "episode" };
15
+ delete e.content_hash;
16
+ return { ...e, content_hash: episodeHash(e) };
17
+ }
18
+
19
+ function groupOf(ep, strategy) {
20
+ switch (strategy) {
21
+ case "by_task":
22
+ return ep.task;
23
+ case "by_variant":
24
+ return ep.variant ?? ep.split_group ?? ep.episode_id;
25
+ default:
26
+ return ep.split_group ?? ep.episode_id;
27
+ }
28
+ }
29
+
30
+ /** Stable split for a group: hash(seed, group) → [0,1). */
31
+ function bucket(seed, group) {
32
+ return parseInt(sha256Hex(`${seed}:${group}`).slice(0, 8), 16) / 0x100000000;
33
+ }
34
+
35
+ export function assignSplits(episodes, { strategy = "by_session", seed = 0, validation = 0.15, test = 0.2 } = {}) {
36
+ return episodes.map((ep) => {
37
+ const g = groupOf(ep, strategy);
38
+ const b = bucket(seed, g);
39
+ const split = b < test ? "test" : b < test + validation ? "validation" : "train";
40
+ return { ep, split, group: g };
41
+ });
42
+ }
43
+
44
+ /**
45
+ * Build a DatasetBundleV1 from sealed episodes. Throws on schema errors, hash
46
+ * mismatches, unreviewed episodes or anything the secret scan flags.
47
+ */
48
+ export function buildBundle(episodes, opts = {}) {
49
+ const {
50
+ datasetId,
51
+ name,
52
+ version = 1,
53
+ license = "UNLICENSED-private",
54
+ provenance = { source: "w3bs" },
55
+ consent,
56
+ retention = { days: null, delete_on_revoke: true },
57
+ redactionProfile = "strict",
58
+ strategy = "by_session",
59
+ seed = 0,
60
+ validation,
61
+ test,
62
+ requireReviewed = true,
63
+ createdAt = new Date().toISOString()
64
+ } = opts;
65
+ if (!consent?.consent_id) {
66
+ const err = new Error("dataset needs a consent record (consent_id, purposes, granted_at)");
67
+ err.code = "E_CONSENT";
68
+ throw err;
69
+ }
70
+ for (const ep of episodes) {
71
+ assertValid(ep, "episode");
72
+ if (episodeHash(ep) !== ep.content_hash) {
73
+ const err = new Error(`episode ${ep.episode_id} content_hash does not match its content`);
74
+ err.code = "E_HASH_MISMATCH";
75
+ throw err;
76
+ }
77
+ if (requireReviewed && !ep.labels.reviewed) {
78
+ const err = new Error(`episode ${ep.episode_id} has not been reviewed`);
79
+ err.code = "E_CONSENT";
80
+ throw err;
81
+ }
82
+ assertNoSecrets(ep, `episode ${ep.episode_id}`);
83
+ }
84
+ const assigned = assignSplits(episodes, { strategy, seed, validation, test });
85
+ const counts = { train: 0, validation: 0, test: 0 };
86
+ for (const a of assigned) counts[a.split] += 1;
87
+ const manifest = {
88
+ schema_version: SCHEMA_VERSION,
89
+ kind: "dataset_manifest",
90
+ dataset_id: datasetId ?? `ds_${sha256Hex(assigned.map((a) => a.ep.content_hash).join(",")).slice(0, 16)}`,
91
+ ...(name ? { name } : {}),
92
+ version,
93
+ created_at: createdAt,
94
+ episodes: assigned.map((a) => ({
95
+ episode_id: a.ep.episode_id,
96
+ content_hash: a.ep.content_hash,
97
+ task: a.ep.task,
98
+ split: a.split,
99
+ split_group: String(a.group).slice(0, 128)
100
+ })),
101
+ splits: { strategy, seed, counts },
102
+ license,
103
+ provenance,
104
+ consent,
105
+ retention,
106
+ redaction: { profile: redactionProfile, secret_scan: "passed", scanner_version: SCANNER_VERSION }
107
+ };
108
+ manifest.dataset_hash = datasetHash(manifest);
109
+ const bundle = { schema_version: SCHEMA_VERSION, kind: "dataset_bundle", manifest, episodes };
110
+ assertValid(bundle, "dataset_bundle");
111
+ return bundle;
112
+ }
113
+
114
+ /**
115
+ * Full integrity check of a bundle: schema, every episode hash, manifest ↔
116
+ * episode agreement, dataset_hash, secret scan. Returns the dataset hash.
117
+ */
118
+ export function verifyBundle(bundle) {
119
+ assertValid(bundle, "dataset_bundle");
120
+ const byId = new Map(bundle.episodes.map((e) => [e.episode_id, e]));
121
+ if (byId.size !== bundle.episodes.length) throw Object.assign(new Error("duplicate episode_id in bundle"), { code: "E_SCHEMA" });
122
+ for (const entry of bundle.manifest.episodes) {
123
+ const ep = byId.get(entry.episode_id);
124
+ if (!ep) throw Object.assign(new Error(`manifest names ${entry.episode_id} but the bundle lacks it`), { code: "E_HASH_MISMATCH" });
125
+ if (ep.content_hash !== entry.content_hash || episodeHash(ep) !== ep.content_hash) {
126
+ throw Object.assign(new Error(`episode ${entry.episode_id} does not match its hash`), { code: "E_HASH_MISMATCH" });
127
+ }
128
+ }
129
+ if (bundle.manifest.episodes.length !== bundle.episodes.length) throw Object.assign(new Error("bundle carries episodes the manifest does not name"), { code: "E_HASH_MISMATCH" });
130
+ if (datasetHash(bundle.manifest) !== bundle.manifest.dataset_hash) throw Object.assign(new Error("dataset_hash does not match the manifest"), { code: "E_HASH_MISMATCH" });
131
+ assertNoSecrets(bundle.episodes, "dataset");
132
+ return bundle.manifest.dataset_hash;
133
+ }
package/src/index.js ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @infernetprotocol/liquid-agent — the liquid-agent/v1 contract (schemas,
3
+ * canonical hashing, validation, secret scan, dataset bundles) and the client
4
+ * for the isolated `infernet-liquid` Python runtime behind Infernet's
5
+ * temporal-policy model family (CfC/LTC liquid networks, GRU baseline).
6
+ */
7
+
8
+ export * from "./contract.js";
9
+ export * from "./canonical.js";
10
+ export * from "./validate.js";
11
+ export * from "./secrets.js";
12
+ export * from "./dataset.js";
13
+ export * from "./runtime.js";