forge-workflow 0.1.0-beta.6 → 0.1.0-beta.7
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.
- package/CHANGELOG.md +12 -0
- package/docs/guides/MIGRATION.md +3 -3
- package/docs/reference/RELEASE.md +3 -3
- package/lib/commands/merge.js +2 -2
- package/lib/commands/push.js +13 -4
- package/lib/commands/shepherd.js +1 -1
- package/lib/commands/validate.js +13 -1
- package/lib/kernel/migrations.js +1 -1
- package/lib/kernel/sqlite-driver.js +1 -1
- package/lib/memory/router.js +1 -1
- package/lib/memory/usage-evidence.js +1 -1
- package/lib/pr-monitor/flow-monitor.js +2 -2
- package/lib/pr-monitor/reconcile-executor.js +1 -1
- package/lib/project-memory.js +1 -1
- package/lib/validation-receipt.js +190 -0
- package/node_modules/@forge/contracts/compatibility-matrix.v1.json +1 -0
- package/node_modules/@forge/contracts/contract-baseline.v1.json +134 -0
- package/node_modules/@forge/contracts/fixtures/v1/canonical-hash.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/contract-inputs.v1.json +71 -0
- package/node_modules/@forge/contracts/fixtures/v1/feedback-secret-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/identity-conflict.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/malformed-envelope-inputs.v1.json +16 -0
- package/node_modules/@forge/contracts/fixtures/v1/malformed-missing-required.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/monitor-bounds-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/monitor-receipt-privacy-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/not-executed-receipt.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/privacy-redaction.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/retry-identical.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/stale-authority-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/structured-error-privacy-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/structured-error-stripe-live-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/structured-error-stripe-test-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/unknown-advisory-roundtrip.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/unknown-consequential-reject.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/valid-full.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/valid-minimal.json +1 -0
- package/node_modules/@forge/contracts/fixtures/v1/wrong-capability-digest.json +1 -0
- package/node_modules/@forge/contracts/index.js +32 -0
- package/node_modules/@forge/contracts/package.json +35 -0
- package/node_modules/@forge/contracts/schemas/v1/capability-manifest.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/claim-request.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/context-packet.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/delivery-receipt.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/feedback-report.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/lease-receipt.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/monitor-event.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/monitor-receipt.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/run-receipt.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/structured-error.schema.json +1 -0
- package/node_modules/@forge/contracts/schemas/v1/work-packet.schema.json +1 -0
- package/node_modules/@forge/contracts/src/baseline.js +23 -0
- package/node_modules/@forge/contracts/src/canonical.js +151 -0
- package/node_modules/@forge/contracts/src/definitions.js +176 -0
- package/node_modules/@forge/contracts/src/identity.js +42 -0
- package/node_modules/@forge/contracts/src/schema.js +72 -0
- package/node_modules/@forge/contracts/src/validate.js +305 -0
- package/node_modules/@forge/flow/index.js +114 -0
- package/node_modules/@forge/flow/package.json +34 -0
- package/node_modules/@forge/flow/src/bounded-loop.js +415 -0
- package/node_modules/@forge/flow/src/efficiency-supervisor.js +89 -0
- package/node_modules/@forge/flow/src/executor.js +279 -0
- package/node_modules/@forge/flow/src/monitor-durability.js +419 -0
- package/node_modules/@forge/flow/src/monitor-runtime.js +460 -0
- package/node_modules/@forge/flow/src/process-lifecycle.js +469 -0
- package/node_modules/@forge/flow/src/skill-runtime.js +343 -0
- package/node_modules/@forge/memory/index.js +331 -0
- package/node_modules/@forge/memory/package.json +34 -0
- package/node_modules/@forge/memory/src/authority-provider.js +67 -0
- package/node_modules/@forge/memory/src/backend-registry.js +166 -0
- package/node_modules/@forge/memory/src/feedback-intake.js +243 -0
- package/node_modules/@forge/memory/src/pr-lifecycle-authority.js +946 -0
- package/node_modules/@forge/memory/src/usage-evidence.js +205 -0
- package/package.json +9 -1
- package/packages/flow/node_modules/@forge/contracts/compatibility-matrix.v1.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/contract-baseline.v1.json +134 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/canonical-hash.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/contract-inputs.v1.json +71 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/feedback-secret-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/identity-conflict.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/malformed-envelope-inputs.v1.json +16 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/malformed-missing-required.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/monitor-bounds-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/monitor-receipt-privacy-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/not-executed-receipt.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/privacy-redaction.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/retry-identical.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/stale-authority-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/structured-error-privacy-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/structured-error-stripe-live-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/structured-error-stripe-test-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/unknown-advisory-roundtrip.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/unknown-consequential-reject.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/valid-full.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/valid-minimal.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/fixtures/v1/wrong-capability-digest.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/index.js +32 -0
- package/packages/flow/node_modules/@forge/contracts/package.json +35 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/capability-manifest.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/claim-request.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/context-packet.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/delivery-receipt.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/feedback-report.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/lease-receipt.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/monitor-event.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/monitor-receipt.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/run-receipt.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/structured-error.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/schemas/v1/work-packet.schema.json +1 -0
- package/packages/flow/node_modules/@forge/contracts/src/baseline.js +23 -0
- package/packages/flow/node_modules/@forge/contracts/src/canonical.js +151 -0
- package/packages/flow/node_modules/@forge/contracts/src/definitions.js +176 -0
- package/packages/flow/node_modules/@forge/contracts/src/identity.js +42 -0
- package/packages/flow/node_modules/@forge/contracts/src/schema.js +72 -0
- package/packages/flow/node_modules/@forge/contracts/src/validate.js +305 -0
- package/packages/memory/node_modules/@forge/contracts/compatibility-matrix.v1.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/contract-baseline.v1.json +134 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/canonical-hash.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/contract-inputs.v1.json +71 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/feedback-secret-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/identity-conflict.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/malformed-envelope-inputs.v1.json +16 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/malformed-missing-required.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/monitor-bounds-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/monitor-receipt-privacy-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/not-executed-receipt.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/privacy-redaction.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/retry-identical.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/stale-authority-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/structured-error-privacy-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/structured-error-stripe-live-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/structured-error-stripe-test-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/unknown-advisory-roundtrip.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/unknown-consequential-reject.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/valid-full.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/valid-minimal.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/fixtures/v1/wrong-capability-digest.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/index.js +32 -0
- package/packages/memory/node_modules/@forge/contracts/package.json +35 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/capability-manifest.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/claim-request.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/context-packet.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/delivery-receipt.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/feedback-report.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/lease-receipt.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/monitor-event.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/monitor-receipt.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/run-receipt.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/structured-error.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/schemas/v1/work-packet.schema.json +1 -0
- package/packages/memory/node_modules/@forge/contracts/src/baseline.js +23 -0
- package/packages/memory/node_modules/@forge/contracts/src/canonical.js +151 -0
- package/packages/memory/node_modules/@forge/contracts/src/definitions.js +176 -0
- package/packages/memory/node_modules/@forge/contracts/src/identity.js +42 -0
- package/packages/memory/node_modules/@forge/contracts/src/schema.js +72 -0
- package/packages/memory/node_modules/@forge/contracts/src/validate.js +305 -0
- package/scripts/test.js +1 -1
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function deepFreeze(value, seen = new WeakSet()) {
|
|
4
|
+
if (!value || typeof value !== "object" || seen.has(value)) return value;
|
|
5
|
+
seen.add(value);
|
|
6
|
+
for (const descriptor of Object.values(Object.getOwnPropertyDescriptors(value))) {
|
|
7
|
+
if (Object.hasOwn(descriptor, "value")) deepFreeze(descriptor.value, seen);
|
|
8
|
+
}
|
|
9
|
+
return Object.freeze(value);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const CONTRACTS = deepFreeze({
|
|
13
|
+
"forge.memory.work-packet.v1": {
|
|
14
|
+
name: "WorkPacket",
|
|
15
|
+
identity: ["issue_id", "expected_issue_revision", "packet_id", "packet_revision", "repository_id", "target_head"],
|
|
16
|
+
required: ["issue_id", "expected_issue_revision", "packet_id", "packet_revision", "repository_id", "target_head", "objective", "authority", "allowed_mutations", "workflow_config_revision", "capability_manifest_digest"],
|
|
17
|
+
optional: ["acceptance_criteria", "prohibited_actions", "dependencies", "constraints", "risk", "platform", "context_references", "expected_outputs", "target", "receipt_requirements", "idempotency_key", "token_budget", "budget_metric", "risk_manifest_digest"],
|
|
18
|
+
},
|
|
19
|
+
"forge.memory.context-packet.v1": {
|
|
20
|
+
name: "ContextPacket",
|
|
21
|
+
identity: ["work_packet_hash", "context_selection_revision", "privacy_scope_hash"],
|
|
22
|
+
required: ["work_packet_hash", "context_selection_revision", "privacy_scope_hash", "retention_class", "disclosure_class"],
|
|
23
|
+
optional: ["references", "summaries", "redaction_policy_revision"],
|
|
24
|
+
},
|
|
25
|
+
"forge.memory.claim-request.v1": {
|
|
26
|
+
name: "ClaimRequest",
|
|
27
|
+
identity: ["issue_id", "expected_issue_revision", "actor_id", "request_id"],
|
|
28
|
+
required: ["issue_id", "expected_issue_revision", "actor_id", "request_id", "requested_scope", "idempotency_key"],
|
|
29
|
+
optional: [],
|
|
30
|
+
},
|
|
31
|
+
"forge.memory.lease-receipt.v1": {
|
|
32
|
+
name: "LeaseReceipt",
|
|
33
|
+
identity: ["claim_request_id", "lease_id", "lease_epoch"],
|
|
34
|
+
required: ["claim_request_id", "lease_id", "lease_epoch", "issue_revision", "actor_id", "scope", "expires_at", "durable", "authority_signature"],
|
|
35
|
+
optional: [],
|
|
36
|
+
},
|
|
37
|
+
"forge.memory.capability-manifest.v1": {
|
|
38
|
+
name: "CapabilityManifest",
|
|
39
|
+
identity: ["provider_id", "manifest_revision", "config_revision"],
|
|
40
|
+
required: ["provider_id", "manifest_revision", "config_revision", "executable_identity", "provider_version", "probe_revision", "result_hash", "capabilities", "evaluator_status", "probed_at", "expires_at"],
|
|
41
|
+
optional: [],
|
|
42
|
+
},
|
|
43
|
+
"forge.memory.run-receipt.v1": {
|
|
44
|
+
name: "RunReceipt",
|
|
45
|
+
identity: ["packet_hash", "run_id", "attempt_id", "exact_head"],
|
|
46
|
+
required: ["packet_hash", "run_id", "attempt_id", "exact_head", "packet_revision", "manifest_digest", "workflow_config_revision", "status", "executor", "started_at", "ended_at", "evidence_refs", "validation", "cleanup"],
|
|
47
|
+
optional: ["lease_epoch", "tokens", "retries", "corrections", "mutations_attempted", "mutations_authorized", "structured_error", "active_time_ms", "passive_time_ms"],
|
|
48
|
+
},
|
|
49
|
+
"forge.memory.feedback-report.v1": {
|
|
50
|
+
name: "FeedbackReport",
|
|
51
|
+
identity: ["report_id", "product_version", "content_fingerprint"],
|
|
52
|
+
required: ["report_id", "product_version", "contract_version", "stable_error_code", "affected_capability", "redacted_reproduction_steps", "expected_classification", "actual_classification", "occurrence_count", "content_fingerprint", "consent_event_id", "redaction_policy_revision"],
|
|
53
|
+
optional: ["proposed_fix", "return_channel"],
|
|
54
|
+
},
|
|
55
|
+
"forge.memory.structured-error.v1": {
|
|
56
|
+
name: "StructuredError",
|
|
57
|
+
identity: ["parent_object_hash", "error_occurrence_id"],
|
|
58
|
+
required: ["parent_object_hash", "error_occurrence_id", "code", "terminal_classification", "safe_details"],
|
|
59
|
+
optional: ["retryable", "evidence_refs"],
|
|
60
|
+
},
|
|
61
|
+
"forge.memory.monitor-event.v1": {
|
|
62
|
+
name: "MonitorEvent",
|
|
63
|
+
identity: ["monitor_id", "event_id", "sequence"],
|
|
64
|
+
required: ["monitor_id", "event_id", "sequence", "subject_revision", "type", "actionability", "observed_at"],
|
|
65
|
+
optional: ["bounded_payload", "artifact_digest"],
|
|
66
|
+
},
|
|
67
|
+
"forge.memory.delivery-receipt.v1": {
|
|
68
|
+
name: "DeliveryReceipt",
|
|
69
|
+
identity: ["event_id", "target", "attempt"],
|
|
70
|
+
required: ["event_id", "target", "transport_tier", "attempt", "delivered_at", "acknowledged", "outcome"],
|
|
71
|
+
optional: [],
|
|
72
|
+
},
|
|
73
|
+
"forge.memory.monitor-receipt.v1": {
|
|
74
|
+
name: "MonitorReceipt",
|
|
75
|
+
identity: ["monitor_id", "owner_run_id", "last_sequence"],
|
|
76
|
+
required: ["monitor_id", "owner_run_id", "terminal_state", "terminal_reason", "last_sequence", "evidence_digest", "cancellation_acknowledged", "process_cleanup", "lease_cleanup"],
|
|
77
|
+
optional: ["undelivered_cursor"],
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const STRING = Object.freeze({ type: "string", minLength: 1 });
|
|
82
|
+
const INTEGER = Object.freeze({ type: "integer", minimum: 0 });
|
|
83
|
+
const POSITIVE_INTEGER = Object.freeze({ type: "integer", minimum: 1 });
|
|
84
|
+
const BOOLEAN = Object.freeze({ type: "boolean" });
|
|
85
|
+
const OBJECT = Object.freeze({ type: "object" });
|
|
86
|
+
const STRING_ARRAY = Object.freeze({ type: "array", items: { type: "string" } });
|
|
87
|
+
const OBJECT_ARRAY = Object.freeze({ type: "array", items: { type: "object" } });
|
|
88
|
+
const HASH = Object.freeze({ type: "string", pattern: "^[0-9a-f]{64}$" });
|
|
89
|
+
const HEAD = Object.freeze({ type: "string", pattern: "^[0-9a-f]{40}(?:[0-9a-f]{24})?$" });
|
|
90
|
+
const TIMESTAMP = Object.freeze({ type: "string", format: "date-time" });
|
|
91
|
+
const EXTENSION_ID_PATTERN = "^[A-Za-z0-9][A-Za-z0-9_-]*(?:[./][A-Za-z0-9][A-Za-z0-9_-]*)+$";
|
|
92
|
+
const EXTENSION_ID_MAX_LENGTH = 255;
|
|
93
|
+
const SECRET_PATTERN = "(?:gh[pousr]_[A-Za-z0-9]{20,}|sk_(?:live|test)_[A-Za-z0-9]{16,}|sk-[A-Za-z0-9]{16,}|AKIA[0-9A-Z]{16}|(?:api[_-]?key|token|secret|password)\\s*[:=]\\s*\\S{8,})";
|
|
94
|
+
const ABSOLUTE_USER_PATH_PATTERN = "(?:[A-Za-z]:\\\\Users\\\\[^\\\\\\s]+|/(?:Users|home)/[^/\\s]+/)";
|
|
95
|
+
const SAFE_REDACTED_STRING = Object.freeze({ type: "string", minLength: 1, maxLength: 4096, not: { pattern: `${SECRET_PATTERN}|${ABSOLUTE_USER_PATH_PATTERN}` } });
|
|
96
|
+
const SAFE_REDACTED_STEPS = Object.freeze({ type: "array", maxItems: 64, items: SAFE_REDACTED_STRING });
|
|
97
|
+
const BOUNDED_OBJECT = Object.freeze({
|
|
98
|
+
type: "object",
|
|
99
|
+
maxProperties: 64,
|
|
100
|
+
"x-forge-max-depth": 8,
|
|
101
|
+
"x-forge-max-items": 128,
|
|
102
|
+
"x-forge-max-serialized-bytes": 16384,
|
|
103
|
+
"x-forge-secret-pattern": SECRET_PATTERN,
|
|
104
|
+
"x-forge-absolute-user-path-pattern": ABSOLUTE_USER_PATH_PATTERN,
|
|
105
|
+
});
|
|
106
|
+
const BOUNDED_OBJECT_ARRAY = Object.freeze({ type: "array", maxItems: 128, items: BOUNDED_OBJECT });
|
|
107
|
+
|
|
108
|
+
const PAYLOAD_FIELDS = deepFreeze({
|
|
109
|
+
"forge.memory.work-packet.v1": {
|
|
110
|
+
issue_id: STRING, expected_issue_revision: INTEGER, packet_id: STRING, packet_revision: POSITIVE_INTEGER,
|
|
111
|
+
repository_id: STRING, target_head: HEAD, objective: STRING,
|
|
112
|
+
authority: { type: "object", required: ["kind", "issue_revision"], properties: { kind: STRING, issue_revision: INTEGER } },
|
|
113
|
+
allowed_mutations: STRING_ARRAY, workflow_config_revision: STRING, capability_manifest_digest: HASH,
|
|
114
|
+
acceptance_criteria: STRING_ARRAY, prohibited_actions: STRING_ARRAY, dependencies: STRING_ARRAY,
|
|
115
|
+
constraints: OBJECT, risk: OBJECT, platform: OBJECT, context_references: OBJECT_ARRAY,
|
|
116
|
+
expected_outputs: STRING_ARRAY, target: OBJECT, receipt_requirements: OBJECT, idempotency_key: STRING,
|
|
117
|
+
token_budget: POSITIVE_INTEGER, budget_metric: { type: "string", enum: ["provider_reported_total_tokens"] }, risk_manifest_digest: HASH,
|
|
118
|
+
},
|
|
119
|
+
"forge.memory.context-packet.v1": {
|
|
120
|
+
work_packet_hash: HASH, context_selection_revision: POSITIVE_INTEGER, privacy_scope_hash: HASH,
|
|
121
|
+
retention_class: { type: "string", enum: ["public_metadata", "local_sensitive", "remote_redacted", "restricted"] },
|
|
122
|
+
disclosure_class: { type: "string", enum: ["public_metadata", "local_sensitive", "remote_redacted", "restricted"] },
|
|
123
|
+
references: BOUNDED_OBJECT_ARRAY, summaries: BOUNDED_OBJECT_ARRAY, redaction_policy_revision: STRING,
|
|
124
|
+
},
|
|
125
|
+
"forge.memory.claim-request.v1": {
|
|
126
|
+
issue_id: STRING, expected_issue_revision: INTEGER, actor_id: STRING, request_id: STRING,
|
|
127
|
+
requested_scope: OBJECT, idempotency_key: STRING,
|
|
128
|
+
},
|
|
129
|
+
"forge.memory.lease-receipt.v1": {
|
|
130
|
+
claim_request_id: STRING, lease_id: STRING, lease_epoch: POSITIVE_INTEGER, issue_revision: INTEGER,
|
|
131
|
+
actor_id: STRING, scope: OBJECT, expires_at: TIMESTAMP, durable: BOOLEAN, authority_signature: STRING,
|
|
132
|
+
},
|
|
133
|
+
"forge.memory.capability-manifest.v1": {
|
|
134
|
+
provider_id: STRING, manifest_revision: POSITIVE_INTEGER, config_revision: STRING,
|
|
135
|
+
executable_identity: OBJECT, provider_version: STRING, probe_revision: STRING, result_hash: HASH,
|
|
136
|
+
capabilities: { type: "array", items: { type: "object", required: ["capability_id", "available"], properties: { capability_id: STRING, available: BOOLEAN } } },
|
|
137
|
+
evaluator_status: { type: "string", enum: ["approved", "unavailable", "quarantined"] }, probed_at: TIMESTAMP, expires_at: TIMESTAMP,
|
|
138
|
+
},
|
|
139
|
+
"forge.memory.run-receipt.v1": {
|
|
140
|
+
packet_hash: HASH, run_id: STRING, attempt_id: STRING, exact_head: HEAD, packet_revision: POSITIVE_INTEGER,
|
|
141
|
+
manifest_digest: HASH, workflow_config_revision: STRING, status: { type: "string", enum: ["PASS", "FAIL", "INCOMPLETE", "NOT_EXECUTED"] },
|
|
142
|
+
executor: OBJECT, started_at: TIMESTAMP, ended_at: TIMESTAMP, evidence_refs: OBJECT_ARRAY, validation: BOUNDED_OBJECT, cleanup: BOUNDED_OBJECT,
|
|
143
|
+
lease_epoch: POSITIVE_INTEGER, tokens: OBJECT, retries: INTEGER, corrections: INTEGER,
|
|
144
|
+
mutations_attempted: STRING_ARRAY, mutations_authorized: STRING_ARRAY, structured_error: BOUNDED_OBJECT,
|
|
145
|
+
active_time_ms: INTEGER, passive_time_ms: INTEGER,
|
|
146
|
+
},
|
|
147
|
+
"forge.memory.feedback-report.v1": {
|
|
148
|
+
report_id: STRING, product_version: STRING, contract_version: POSITIVE_INTEGER, stable_error_code: STRING,
|
|
149
|
+
affected_capability: STRING, redacted_reproduction_steps: SAFE_REDACTED_STEPS, expected_classification: STRING,
|
|
150
|
+
actual_classification: STRING, occurrence_count: POSITIVE_INTEGER, content_fingerprint: HASH,
|
|
151
|
+
consent_event_id: STRING, redaction_policy_revision: STRING, proposed_fix: SAFE_REDACTED_STRING, return_channel: BOUNDED_OBJECT,
|
|
152
|
+
},
|
|
153
|
+
"forge.memory.structured-error.v1": {
|
|
154
|
+
parent_object_hash: HASH, error_occurrence_id: STRING, code: STRING,
|
|
155
|
+
terminal_classification: { type: "string", enum: ["FAIL", "INCOMPLETE"] }, safe_details: BOUNDED_OBJECT,
|
|
156
|
+
retryable: BOOLEAN, evidence_refs: OBJECT_ARRAY,
|
|
157
|
+
},
|
|
158
|
+
"forge.memory.monitor-event.v1": {
|
|
159
|
+
monitor_id: STRING, event_id: STRING, sequence: INTEGER, subject_revision: STRING, type: STRING,
|
|
160
|
+
actionability: { type: "string", enum: ["advisory", "action_required", "terminal"] }, observed_at: TIMESTAMP,
|
|
161
|
+
bounded_payload: BOUNDED_OBJECT, artifact_digest: HASH,
|
|
162
|
+
},
|
|
163
|
+
"forge.memory.delivery-receipt.v1": {
|
|
164
|
+
event_id: STRING, target: STRING, transport_tier: { type: "string", enum: ["T0", "T1", "T2", "T3", "T4"] },
|
|
165
|
+
attempt: POSITIVE_INTEGER, delivered_at: TIMESTAMP, acknowledged: BOOLEAN,
|
|
166
|
+
outcome: { type: "string", enum: ["delivered", "pending", "failed", "acknowledged"] },
|
|
167
|
+
},
|
|
168
|
+
"forge.memory.monitor-receipt.v1": {
|
|
169
|
+
monitor_id: STRING, owner_run_id: STRING,
|
|
170
|
+
terminal_state: { type: "string", enum: ["PASS", "FAIL", "INCOMPLETE", "CANCELLED"] }, terminal_reason: STRING,
|
|
171
|
+
last_sequence: INTEGER, evidence_digest: HASH, cancellation_acknowledged: BOOLEAN,
|
|
172
|
+
process_cleanup: BOUNDED_OBJECT, lease_cleanup: BOUNDED_OBJECT, undelivered_cursor: INTEGER,
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
module.exports = { CONTRACTS, EXTENSION_ID_MAX_LENGTH, EXTENSION_ID_PATTERN, PAYLOAD_FIELDS };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { canonicalize, computeContentHash } = require("./canonical.js");
|
|
4
|
+
const { CONTRACTS } = require("./definitions.js");
|
|
5
|
+
|
|
6
|
+
function semanticIdentity(value) {
|
|
7
|
+
const definition = CONTRACTS[value?.schema_id];
|
|
8
|
+
if (!definition) throw new TypeError(`Unsupported schema_id: ${value?.schema_id}`);
|
|
9
|
+
const identity = {};
|
|
10
|
+
for (const field of definition.identity) {
|
|
11
|
+
if (!Object.hasOwn(value.payload ?? {}, field)) {
|
|
12
|
+
throw new TypeError(`Semantic identity requires payload.${field}`);
|
|
13
|
+
}
|
|
14
|
+
identity[field] = value.payload[field];
|
|
15
|
+
}
|
|
16
|
+
return `${value.schema_id}:${canonicalize(identity)}`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function classifySemanticAttempt(accepted, candidate) {
|
|
20
|
+
const acceptedIdentity = semanticIdentity(accepted);
|
|
21
|
+
const candidateIdentity = semanticIdentity(candidate);
|
|
22
|
+
if (acceptedIdentity !== candidateIdentity) {
|
|
23
|
+
return { status: "new-identity", identity: candidateIdentity };
|
|
24
|
+
}
|
|
25
|
+
const acceptedHash = computeContentHash(accepted);
|
|
26
|
+
const candidateHash = computeContentHash(candidate);
|
|
27
|
+
if (acceptedHash === candidateHash) {
|
|
28
|
+
return {
|
|
29
|
+
status: "retry-identical",
|
|
30
|
+
identity: candidateIdentity,
|
|
31
|
+
content_hash: candidateHash,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
status: "identity-conflict",
|
|
36
|
+
identity: candidateIdentity,
|
|
37
|
+
accepted_hash: acceptedHash,
|
|
38
|
+
candidate_hash: candidateHash,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = { classifySemanticAttempt, semanticIdentity };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
CONTRACTS,
|
|
5
|
+
EXTENSION_ID_MAX_LENGTH,
|
|
6
|
+
EXTENSION_ID_PATTERN,
|
|
7
|
+
PAYLOAD_FIELDS,
|
|
8
|
+
} = require("./definitions.js");
|
|
9
|
+
|
|
10
|
+
function generateJsonSchema(schemaId) {
|
|
11
|
+
const definition = Object.hasOwn(CONTRACTS, schemaId) ? CONTRACTS[schemaId] : undefined;
|
|
12
|
+
if (!definition) throw new TypeError(`Unsupported schema_id: ${schemaId}`);
|
|
13
|
+
const payloadProperties = {};
|
|
14
|
+
for (const field of [...definition.required, ...definition.optional]) payloadProperties[field] = structuredClone(PAYLOAD_FIELDS[schemaId][field]);
|
|
15
|
+
return {
|
|
16
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
17
|
+
$id: schemaId,
|
|
18
|
+
title: definition.name,
|
|
19
|
+
type: "object",
|
|
20
|
+
required: ["schema_id", "schema_version", "object_id", "created_at", "producer", "capabilities_used", "provenance", "content_hash", "payload", "extensions"],
|
|
21
|
+
properties: {
|
|
22
|
+
schema_id: { const: schemaId },
|
|
23
|
+
schema_version: { const: 1 },
|
|
24
|
+
object_id: { type: "string", format: "uuid" },
|
|
25
|
+
created_at: { type: "string", format: "date-time" },
|
|
26
|
+
producer: {
|
|
27
|
+
type: "object",
|
|
28
|
+
required: ["product_id", "product_version", "instance_id"],
|
|
29
|
+
properties: { product_id: { type: "string", minLength: 1 }, product_version: { type: "string", minLength: 1 }, instance_id: { type: "string", minLength: 1 } },
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
},
|
|
32
|
+
capabilities_used: {
|
|
33
|
+
type: "array",
|
|
34
|
+
items: {
|
|
35
|
+
type: "object",
|
|
36
|
+
required: ["capability_id", "manifest_digest"],
|
|
37
|
+
properties: { capability_id: { type: "string", minLength: 1 }, manifest_digest: { type: "string", pattern: "^[0-9a-f]{64}$" } },
|
|
38
|
+
additionalProperties: false,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
provenance: {
|
|
42
|
+
type: "object",
|
|
43
|
+
required: ["source_kind", "actor_class", "actor_id"],
|
|
44
|
+
properties: { source_kind: { type: "string", minLength: 1 }, actor_class: { type: "string", minLength: 1 }, actor_id: { type: "string", minLength: 1 } },
|
|
45
|
+
additionalProperties: false,
|
|
46
|
+
},
|
|
47
|
+
content_hash: { type: "string", pattern: "^[0-9a-f]{64}$" },
|
|
48
|
+
payload: { type: "object", required: definition.required, properties: payloadProperties, additionalProperties: false },
|
|
49
|
+
extensions: {
|
|
50
|
+
type: "object",
|
|
51
|
+
propertyNames: { pattern: EXTENSION_ID_PATTERN, maxLength: EXTENSION_ID_MAX_LENGTH },
|
|
52
|
+
additionalProperties: {
|
|
53
|
+
type: "object",
|
|
54
|
+
required: ["impact", "schema_version", "value"],
|
|
55
|
+
properties: {
|
|
56
|
+
impact: { const: "advisory" },
|
|
57
|
+
schema_version: { type: "integer", minimum: 1 },
|
|
58
|
+
value: {},
|
|
59
|
+
},
|
|
60
|
+
additionalProperties: false,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
additionalProperties: false,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function supportedSchemaVersions(schemaId) {
|
|
69
|
+
return Object.hasOwn(CONTRACTS, schemaId) ? [1] : [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = { generateJsonSchema, supportedSchemaVersions };
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { canonicalize, computeContentHash, preflightCanonicalValue } = require("./canonical.js");
|
|
4
|
+
const {
|
|
5
|
+
CONTRACTS,
|
|
6
|
+
EXTENSION_ID_MAX_LENGTH,
|
|
7
|
+
EXTENSION_ID_PATTERN,
|
|
8
|
+
PAYLOAD_FIELDS,
|
|
9
|
+
} = require("./definitions.js");
|
|
10
|
+
|
|
11
|
+
const ENVELOPE_FIELDS = Object.freeze([
|
|
12
|
+
"schema_id", "schema_version", "object_id", "created_at", "producer",
|
|
13
|
+
"capabilities_used", "provenance", "content_hash", "payload", "extensions",
|
|
14
|
+
]);
|
|
15
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
16
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
17
|
+
const RFC3339_UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/;
|
|
18
|
+
const LIVE_EVIDENCE_FIELDS = Object.freeze({
|
|
19
|
+
"forge.memory.work-packet.v1": ["issueRevision", "workflowConfigRevision", "capabilityManifestDigest", "exactHead"],
|
|
20
|
+
"forge.memory.context-packet.v1": ["workPacketHash", "privacyScopeHash", "redactionPolicyRevision", "allowedDisclosureClasses"],
|
|
21
|
+
"forge.memory.claim-request.v1": ["issueRevision", "actorId"],
|
|
22
|
+
"forge.memory.lease-receipt.v1": ["issueRevision", "actorId", "leaseEpoch", "observedAt"],
|
|
23
|
+
"forge.memory.capability-manifest.v1": ["providerId", "configRevision", "observedAt"],
|
|
24
|
+
"forge.memory.run-receipt.v1": ["packetHash", "workflowConfigRevision", "capabilityManifestDigest", "exactHead"],
|
|
25
|
+
"forge.memory.feedback-report.v1": ["consentEventId", "redactionPolicyRevision"],
|
|
26
|
+
"forge.memory.structured-error.v1": ["parentObjectHash"],
|
|
27
|
+
"forge.memory.monitor-event.v1": ["monitorId", "subjectRevision"],
|
|
28
|
+
"forge.memory.delivery-receipt.v1": ["eventId", "target"],
|
|
29
|
+
"forge.memory.monitor-receipt.v1": ["monitorId", "ownerRunId"],
|
|
30
|
+
});
|
|
31
|
+
const EXTENSION_ID = new RegExp(EXTENSION_ID_PATTERN);
|
|
32
|
+
const SECRET_PATTERN = /(?:gh[pousr]_[A-Za-z0-9]{20,}|sk_(?:live|test)_[A-Za-z0-9]{16,}|sk-[A-Za-z0-9]{16,}|AKIA[0-9A-Z]{16}|(?:api[_-]?key|token|secret|password)\s*[:=]\s*\S{8,})/i;
|
|
33
|
+
const ABSOLUTE_USER_PATH = /(?:[A-Za-z]:\\Users\\[^\\\s]+|\/(?:Users|home)\/[^/\s]+\/)/i;
|
|
34
|
+
const BOUNDED_LIMITS = Object.freeze({ maxDepth: 8, maxItems: 128, maxProperties: 64, maxBytes: 16_384 });
|
|
35
|
+
|
|
36
|
+
function error(errors, path, code) {
|
|
37
|
+
errors.push({ path, code });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function validateEnvelope(value, options = {}) {
|
|
41
|
+
const errors = [];
|
|
42
|
+
try {
|
|
43
|
+
preflightCanonicalValue(value);
|
|
44
|
+
} catch {
|
|
45
|
+
return { ok: false, errors: [{ path: "$", code: "NON_CANONICAL_VALUE" }] };
|
|
46
|
+
}
|
|
47
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
48
|
+
return { ok: false, errors: [{ path: "$", code: "INVALID_ENVELOPE" }] };
|
|
49
|
+
}
|
|
50
|
+
for (const field of ENVELOPE_FIELDS) {
|
|
51
|
+
if (!Object.hasOwn(value, field)) error(errors, `$.${field}`, "MISSING_REQUIRED");
|
|
52
|
+
}
|
|
53
|
+
if (errors.length > 0) return { ok: false, errors };
|
|
54
|
+
if (!Object.hasOwn(CONTRACTS, value.schema_id)) error(errors, "$.schema_id", "UNSUPPORTED_SCHEMA");
|
|
55
|
+
if (value.schema_version !== 1) error(errors, "$.schema_version", "UNSUPPORTED_VERSION");
|
|
56
|
+
if (typeof value.schema_id !== "string" || value.schema_id.length === 0) error(errors, "$.schema_id", "INVALID_TYPE");
|
|
57
|
+
if (!UUID.test(value.object_id)) error(errors, "$.object_id", "INVALID_UUID");
|
|
58
|
+
if (typeof value.created_at !== "string" || !RFC3339_UTC.test(value.created_at) || Number.isNaN(Date.parse(value.created_at))) error(errors, "$.created_at", "INVALID_TIMESTAMP");
|
|
59
|
+
if (!value.producer || typeof value.producer !== "object" || Array.isArray(value.producer)) error(errors, "$.producer", "INVALID_TYPE");
|
|
60
|
+
else for (const field of ["product_id", "product_version", "instance_id"]) if (typeof value.producer[field] !== "string" || value.producer[field].length === 0) error(errors, `$.producer.${field}`, "MISSING_REQUIRED");
|
|
61
|
+
if (!Array.isArray(value.capabilities_used)) error(errors, "$.capabilities_used", "INVALID_TYPE");
|
|
62
|
+
else {
|
|
63
|
+
const keys = value.capabilities_used.map((item) => `${item?.capability_id ?? ""}:${item?.manifest_digest ?? ""}`);
|
|
64
|
+
if (value.capabilities_used.some((item) => !item || typeof item !== "object" || Array.isArray(item) || typeof item.capability_id !== "string" || item.capability_id.length === 0 || !SHA256.test(item.manifest_digest))) {
|
|
65
|
+
error(errors, "$.capabilities_used", "INVALID_CAPABILITY_BINDING");
|
|
66
|
+
}
|
|
67
|
+
if (keys.join("\0") !== [...keys].sort().join("\0")) error(errors, "$.capabilities_used", "NOT_SORTED");
|
|
68
|
+
}
|
|
69
|
+
if (!value.provenance || typeof value.provenance !== "object" || Array.isArray(value.provenance)) error(errors, "$.provenance", "INVALID_TYPE");
|
|
70
|
+
else for (const field of ["source_kind", "actor_class", "actor_id"]) if (typeof value.provenance[field] !== "string" || value.provenance[field].length === 0) error(errors, `$.provenance.${field}`, "MISSING_REQUIRED");
|
|
71
|
+
if (!SHA256.test(value.content_hash)) error(errors, "$.content_hash", "INVALID_CONTENT_HASH");
|
|
72
|
+
if (!value.payload || typeof value.payload !== "object" || Array.isArray(value.payload)) error(errors, "$.payload", "INVALID_TYPE");
|
|
73
|
+
if (!value.extensions || typeof value.extensions !== "object" || Array.isArray(value.extensions)) error(errors, "$.extensions", "INVALID_TYPE");
|
|
74
|
+
if (options.verifyHash !== false && SHA256.test(value.content_hash)) {
|
|
75
|
+
try {
|
|
76
|
+
if (computeContentHash(value) !== value.content_hash) error(errors, "$.content_hash", "CONTENT_HASH_MISMATCH");
|
|
77
|
+
} catch {
|
|
78
|
+
error(errors, "$", "NON_CANONICAL_VALUE");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { ok: errors.length === 0, errors };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function validateExtensions(extensions, errors) {
|
|
85
|
+
if (!extensions || typeof extensions !== "object" || Array.isArray(extensions)) return;
|
|
86
|
+
for (const [extensionId, extension] of Object.entries(extensions)) {
|
|
87
|
+
const path = `$.extensions[${JSON.stringify(extensionId)}]`;
|
|
88
|
+
if (extensionId.length > EXTENSION_ID_MAX_LENGTH || !EXTENSION_ID.test(extensionId)) error(errors, path, "INVALID_EXTENSION_ID");
|
|
89
|
+
if (!extension || typeof extension !== "object" || Array.isArray(extension)) {
|
|
90
|
+
error(errors, path, "INVALID_EXTENSION");
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (extension.impact === "consequential") error(errors, path, "UNKNOWN_CONSEQUENTIAL_EXTENSION");
|
|
94
|
+
const allowed = new Set(["impact", "schema_version", "value"]);
|
|
95
|
+
const hasExtra = Object.keys(extension).some((field) => !allowed.has(field));
|
|
96
|
+
if (extension.impact !== "advisory" || !Number.isInteger(extension.schema_version) || extension.schema_version < 1 || !Object.hasOwn(extension, "value") || hasExtra) error(errors, path, "INVALID_EXTENSION");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function compare(errors, actual, expected, path, code) {
|
|
101
|
+
if (expected !== undefined && actual !== expected) error(errors, path, code);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isType(value, type) {
|
|
105
|
+
if (type === "integer") return Number.isInteger(value);
|
|
106
|
+
if (type === "array") return Array.isArray(value);
|
|
107
|
+
if (type === "object") return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
108
|
+
return typeof value === type;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function validateShape(value, shape, path, errors) {
|
|
112
|
+
if (!isType(value, shape.type)) {
|
|
113
|
+
error(errors, path, "INVALID_TYPE");
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (shape.enum && !shape.enum.includes(value)) error(errors, path, "INVALID_ENUM");
|
|
117
|
+
if (shape.minLength !== undefined && value.length < shape.minLength) error(errors, path, "INVALID_LENGTH");
|
|
118
|
+
if (shape.maxLength !== undefined && value.length > shape.maxLength) error(errors, path, "BOUNDED_VALUE_TOO_LARGE");
|
|
119
|
+
if (shape.minimum !== undefined && value < shape.minimum) error(errors, path, "OUT_OF_RANGE");
|
|
120
|
+
if (shape.pattern && !new RegExp(shape.pattern).test(value)) error(errors, path, "INVALID_FORMAT");
|
|
121
|
+
if (shape.not?.pattern && new RegExp(shape.not.pattern, "i").test(value)) error(errors, path, "PRIVACY_PATTERN_REJECTED");
|
|
122
|
+
if (shape.format === "date-time" && (!RFC3339_UTC.test(value) || Number.isNaN(Date.parse(value)))) error(errors, path, "INVALID_TIMESTAMP");
|
|
123
|
+
if (shape.type === "array" && shape.maxItems !== undefined && value.length > shape.maxItems) error(errors, path, "BOUNDED_VALUE_TOO_MANY_ITEMS");
|
|
124
|
+
if (shape.type === "array" && shape.items) value.forEach((item, index) => validateShape(item, shape.items, `${path}[${index}]`, errors));
|
|
125
|
+
if (shape.type === "object") {
|
|
126
|
+
if (shape.maxProperties !== undefined && Object.keys(value).length > shape.maxProperties) error(errors, path, "BOUNDED_VALUE_TOO_MANY_PROPERTIES");
|
|
127
|
+
for (const field of shape.required ?? []) {
|
|
128
|
+
if (!Object.hasOwn(value, field)) error(errors, `${path}.${field}`, "MISSING_REQUIRED");
|
|
129
|
+
}
|
|
130
|
+
for (const [field, fieldShape] of Object.entries(shape.properties ?? {})) {
|
|
131
|
+
if (Object.hasOwn(value, field)) validateShape(value[field], fieldShape, `${path}.${field}`, errors);
|
|
132
|
+
}
|
|
133
|
+
if (shape.additionalProperties === false) {
|
|
134
|
+
const allowed = new Set(Object.keys(shape.properties ?? {}));
|
|
135
|
+
for (const field of Object.keys(value)) if (!allowed.has(field)) error(errors, `${path}.${field}`, "UNKNOWN_FIELD");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function inspectPrivacyString(value, path, errors) {
|
|
141
|
+
if (SECRET_PATTERN.test(value)) error(errors, path, "PRIVACY_SECRET_PATTERN");
|
|
142
|
+
if (ABSOLUTE_USER_PATH.test(value)) error(errors, path, "PRIVACY_ABSOLUTE_PATH");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function inspectBoundedValue(value, path, errors, depth = 0) {
|
|
146
|
+
if (depth > BOUNDED_LIMITS.maxDepth) {
|
|
147
|
+
error(errors, path, "BOUNDED_VALUE_TOO_DEEP");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (typeof value === "string") {
|
|
151
|
+
inspectPrivacyString(value, path, errors);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (Array.isArray(value)) {
|
|
155
|
+
if (value.length > BOUNDED_LIMITS.maxItems) error(errors, path, "BOUNDED_VALUE_TOO_MANY_ITEMS");
|
|
156
|
+
value.forEach((item, index) => inspectBoundedValue(item, `${path}[${index}]`, errors, depth + 1));
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (!value || typeof value !== "object") return;
|
|
160
|
+
const entries = Object.entries(value);
|
|
161
|
+
if (entries.length > BOUNDED_LIMITS.maxProperties) error(errors, path, "BOUNDED_VALUE_TOO_MANY_PROPERTIES");
|
|
162
|
+
entries.forEach(([field, item]) => inspectBoundedValue(item, `${path}.${field}`, errors, depth + 1));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function validateBoundedPrivacy(value, errors) {
|
|
166
|
+
const targets = [];
|
|
167
|
+
if (value?.schema_id === "forge.memory.feedback-report.v1") {
|
|
168
|
+
targets.push([value.payload?.redacted_reproduction_steps, "$.payload.redacted_reproduction_steps"]);
|
|
169
|
+
if (value.payload?.proposed_fix !== undefined) targets.push([value.payload.proposed_fix, "$.payload.proposed_fix"]);
|
|
170
|
+
if (value.payload?.return_channel !== undefined) targets.push([value.payload.return_channel, "$.payload.return_channel"]);
|
|
171
|
+
} else if (value?.schema_id === "forge.memory.context-packet.v1") {
|
|
172
|
+
if (value.payload?.references !== undefined) targets.push([value.payload.references, "$.payload.references"]);
|
|
173
|
+
if (value.payload?.summaries !== undefined) targets.push([value.payload.summaries, "$.payload.summaries"]);
|
|
174
|
+
} else if (value?.schema_id === "forge.memory.monitor-event.v1" && value.payload?.bounded_payload !== undefined) {
|
|
175
|
+
targets.push([value.payload.bounded_payload, "$.payload.bounded_payload"]);
|
|
176
|
+
} else if (value?.schema_id === "forge.memory.monitor-receipt.v1") {
|
|
177
|
+
targets.push([value.payload?.process_cleanup, "$.payload.process_cleanup"], [value.payload?.lease_cleanup, "$.payload.lease_cleanup"]);
|
|
178
|
+
} else if (value?.schema_id === "forge.memory.structured-error.v1") {
|
|
179
|
+
targets.push([value.payload?.safe_details, "$.payload.safe_details"]);
|
|
180
|
+
} else if (value?.schema_id === "forge.memory.run-receipt.v1") {
|
|
181
|
+
targets.push(
|
|
182
|
+
[value.payload?.validation, "$.payload.validation"],
|
|
183
|
+
[value.payload?.cleanup, "$.payload.cleanup"],
|
|
184
|
+
);
|
|
185
|
+
if (value.payload?.structured_error !== undefined) targets.push([value.payload.structured_error, "$.payload.structured_error"]);
|
|
186
|
+
}
|
|
187
|
+
for (const [target, path] of targets) {
|
|
188
|
+
if (target === undefined) continue;
|
|
189
|
+
inspectBoundedValue(target, path, errors);
|
|
190
|
+
try {
|
|
191
|
+
canonicalize(target, { maxDepth: 64, maxNodes: 100_000, maxBytes: BOUNDED_LIMITS.maxBytes });
|
|
192
|
+
} catch (failure) {
|
|
193
|
+
if (failure.code === "CANONICAL_BYTE_LIMIT") error(errors, path, "BOUNDED_VALUE_TOO_LARGE");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function isStale(expiresAt, observedAt) {
|
|
199
|
+
const expires = Date.parse(expiresAt);
|
|
200
|
+
const observed = Date.parse(observedAt);
|
|
201
|
+
return Number.isNaN(expires) || Number.isNaN(observed) || expires <= observed;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function validateConsequentialEvidence(value, expected, errors) {
|
|
205
|
+
if (!value?.payload) return;
|
|
206
|
+
if (value.schema_id === "forge.memory.run-receipt.v1" && value.payload.status === "NOT_EXECUTED") {
|
|
207
|
+
error(errors, "$.payload.status", "NOT_EXECUTED_NO_TRANSITION");
|
|
208
|
+
}
|
|
209
|
+
const requiredEvidence = LIVE_EVIDENCE_FIELDS[value.schema_id] ?? [];
|
|
210
|
+
for (const field of requiredEvidence) {
|
|
211
|
+
if (!expected || !Object.hasOwn(expected, field)) error(errors, `$.live_expected.${field}`, "MISSING_LIVE_EVIDENCE");
|
|
212
|
+
}
|
|
213
|
+
if (!expected) return;
|
|
214
|
+
const payload = value.payload;
|
|
215
|
+
if (value.schema_id === "forge.memory.work-packet.v1") {
|
|
216
|
+
compare(errors, payload.expected_issue_revision, expected.issueRevision, "$.payload.expected_issue_revision", "STALE_ISSUE_REVISION");
|
|
217
|
+
compare(errors, payload.workflow_config_revision, expected.workflowConfigRevision, "$.payload.workflow_config_revision", "STALE_WORKFLOW_CONFIG");
|
|
218
|
+
compare(errors, payload.capability_manifest_digest, expected.capabilityManifestDigest, "$.payload.capability_manifest_digest", "WRONG_CAPABILITY_DIGEST");
|
|
219
|
+
compare(errors, payload.target_head, expected.exactHead, "$.payload.target_head", "STALE_EXACT_HEAD");
|
|
220
|
+
} else if (value.schema_id === "forge.memory.context-packet.v1") {
|
|
221
|
+
compare(errors, payload.work_packet_hash, expected.workPacketHash, "$.payload.work_packet_hash", "STALE_WORK_PACKET");
|
|
222
|
+
compare(errors, payload.privacy_scope_hash, expected.privacyScopeHash, "$.payload.privacy_scope_hash", "STALE_PRIVACY_SCOPE");
|
|
223
|
+
compare(errors, payload.redaction_policy_revision, expected.redactionPolicyRevision, "$.payload.redaction_policy_revision", "STALE_REDACTION_POLICY");
|
|
224
|
+
if (expected.allowedDisclosureClasses && !expected.allowedDisclosureClasses.includes(payload.disclosure_class)) {
|
|
225
|
+
error(errors, "$.payload.disclosure_class", "DISCLOSURE_NOT_ALLOWED");
|
|
226
|
+
}
|
|
227
|
+
} else if (value.schema_id === "forge.memory.run-receipt.v1") {
|
|
228
|
+
compare(errors, payload.packet_hash, expected.packetHash, "$.payload.packet_hash", "STALE_WORK_PACKET");
|
|
229
|
+
compare(errors, payload.workflow_config_revision, expected.workflowConfigRevision, "$.payload.workflow_config_revision", "STALE_WORKFLOW_CONFIG");
|
|
230
|
+
compare(errors, payload.manifest_digest, expected.capabilityManifestDigest, "$.payload.manifest_digest", "WRONG_CAPABILITY_DIGEST");
|
|
231
|
+
compare(errors, payload.exact_head, expected.exactHead, "$.payload.exact_head", "STALE_EXACT_HEAD");
|
|
232
|
+
compare(errors, payload.lease_epoch, expected.leaseEpoch, "$.payload.lease_epoch", "STALE_LEASE_EPOCH");
|
|
233
|
+
} else if (value.schema_id === "forge.memory.claim-request.v1") {
|
|
234
|
+
compare(errors, payload.expected_issue_revision, expected.issueRevision, "$.payload.expected_issue_revision", "STALE_ISSUE_REVISION");
|
|
235
|
+
compare(errors, payload.actor_id, expected.actorId, "$.payload.actor_id", "STALE_ACTOR");
|
|
236
|
+
} else if (value.schema_id === "forge.memory.lease-receipt.v1") {
|
|
237
|
+
compare(errors, payload.issue_revision, expected.issueRevision, "$.payload.issue_revision", "STALE_ISSUE_REVISION");
|
|
238
|
+
compare(errors, payload.actor_id, expected.actorId, "$.payload.actor_id", "STALE_ACTOR");
|
|
239
|
+
compare(errors, payload.lease_epoch, expected.leaseEpoch, "$.payload.lease_epoch", "STALE_LEASE_EPOCH");
|
|
240
|
+
if (isStale(payload.expires_at, expected.observedAt)) error(errors, "$.payload.expires_at", "STALE_LEASE");
|
|
241
|
+
} else if (value.schema_id === "forge.memory.capability-manifest.v1") {
|
|
242
|
+
compare(errors, payload.provider_id, expected.providerId, "$.payload.provider_id", "STALE_PROVIDER");
|
|
243
|
+
compare(errors, payload.config_revision, expected.configRevision, "$.payload.config_revision", "STALE_CAPABILITY_CONFIG");
|
|
244
|
+
if (isStale(payload.expires_at, expected.observedAt)) error(errors, "$.payload.expires_at", "STALE_CAPABILITY_MANIFEST");
|
|
245
|
+
} else if (value.schema_id === "forge.memory.feedback-report.v1") {
|
|
246
|
+
compare(errors, payload.consent_event_id, expected.consentEventId, "$.payload.consent_event_id", "STALE_CONSENT");
|
|
247
|
+
compare(errors, payload.redaction_policy_revision, expected.redactionPolicyRevision, "$.payload.redaction_policy_revision", "STALE_REDACTION_POLICY");
|
|
248
|
+
} else if (value.schema_id === "forge.memory.structured-error.v1") {
|
|
249
|
+
compare(errors, payload.parent_object_hash, expected.parentObjectHash, "$.payload.parent_object_hash", "STALE_PARENT_OBJECT");
|
|
250
|
+
} else if (value.schema_id === "forge.memory.monitor-event.v1") {
|
|
251
|
+
compare(errors, payload.monitor_id, expected.monitorId, "$.payload.monitor_id", "STALE_MONITOR");
|
|
252
|
+
compare(errors, payload.subject_revision, expected.subjectRevision, "$.payload.subject_revision", "STALE_SUBJECT");
|
|
253
|
+
} else if (value.schema_id === "forge.memory.delivery-receipt.v1") {
|
|
254
|
+
compare(errors, payload.event_id, expected.eventId, "$.payload.event_id", "STALE_EVENT");
|
|
255
|
+
compare(errors, payload.target, expected.target, "$.payload.target", "STALE_DELIVERY_TARGET");
|
|
256
|
+
} else if (value.schema_id === "forge.memory.monitor-receipt.v1") {
|
|
257
|
+
compare(errors, payload.monitor_id, expected.monitorId, "$.payload.monitor_id", "STALE_MONITOR");
|
|
258
|
+
compare(errors, payload.owner_run_id, expected.ownerRunId, "$.payload.owner_run_id", "STALE_OWNER_RUN");
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function validateContractStructure(value, options = {}) {
|
|
263
|
+
const envelope = validateEnvelope(value, options);
|
|
264
|
+
if (envelope.errors.some((item) => item.code === "NON_CANONICAL_VALUE")) return envelope;
|
|
265
|
+
const errors = [...envelope.errors];
|
|
266
|
+
const definition = Object.hasOwn(CONTRACTS, value?.schema_id) ? CONTRACTS[value.schema_id] : undefined;
|
|
267
|
+
if (definition && value?.payload && typeof value.payload === "object" && !Array.isArray(value.payload)) {
|
|
268
|
+
for (const field of definition.required) {
|
|
269
|
+
if (!Object.hasOwn(value.payload, field)) error(errors, `$.payload.${field}`, "MISSING_REQUIRED");
|
|
270
|
+
}
|
|
271
|
+
const allowed = new Set([...definition.required, ...definition.optional]);
|
|
272
|
+
for (const field of Object.keys(value.payload)) {
|
|
273
|
+
if (!allowed.has(field)) error(errors, `$.payload.${field}`, "UNKNOWN_PAYLOAD_FIELD");
|
|
274
|
+
else validateShape(value.payload[field], PAYLOAD_FIELDS[value.schema_id][field], `$.payload.${field}`, errors);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
validateExtensions(value?.extensions, errors);
|
|
278
|
+
if (!errors.some((item) => item.code === "NON_CANONICAL_VALUE")) validateBoundedPrivacy(value, errors);
|
|
279
|
+
return { ok: errors.length === 0, errors };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function validateContract(value, options = {}) {
|
|
283
|
+
const structural = validateContractStructure(value, options);
|
|
284
|
+
if (structural.errors.some((item) => item.code === "NON_CANONICAL_VALUE")) return structural;
|
|
285
|
+
const errors = [...structural.errors];
|
|
286
|
+
validateConsequentialEvidence(value, options.expected, errors);
|
|
287
|
+
return { ok: errors.length === 0, errors };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
class ContractValidationError extends Error {
|
|
291
|
+
constructor(errors) {
|
|
292
|
+
super("Contract validation failed");
|
|
293
|
+
this.name = "ContractValidationError";
|
|
294
|
+
this.errors = errors;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function parseContract(json, options = {}) {
|
|
299
|
+
const value = JSON.parse(json);
|
|
300
|
+
const result = validateContract(value, options);
|
|
301
|
+
if (!result.ok) throw new ContractValidationError(result.errors);
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
module.exports = { ContractValidationError, ENVELOPE_FIELDS, parseContract, validateContract, validateContractStructure, validateEnvelope };
|