@world-engines/tmw 0.1.0-alpha.0

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 (120) hide show
  1. package/LICENSE +46 -0
  2. package/dist/activation.d.ts +50 -0
  3. package/dist/activation.js +113 -0
  4. package/dist/audio-presentation-projection.d.ts +23 -0
  5. package/dist/audio-presentation-projection.js +39 -0
  6. package/dist/author-scenario-source-v3.d.ts +34 -0
  7. package/dist/author-scenario-source-v3.js +1 -0
  8. package/dist/authoring-runtime.d.ts +76 -0
  9. package/dist/authoring-runtime.js +282 -0
  10. package/dist/builder-diagnostics.d.ts +3 -0
  11. package/dist/builder-diagnostics.js +8 -0
  12. package/dist/builder-node.d.ts +178 -0
  13. package/dist/builder-node.js +212 -0
  14. package/dist/builder.d.ts +32 -0
  15. package/dist/builder.js +127 -0
  16. package/dist/capability-registry.d.ts +32 -0
  17. package/dist/capability-registry.js +33 -0
  18. package/dist/compile-target-profile.d.ts +16 -0
  19. package/dist/compile-target-profile.js +70 -0
  20. package/dist/compiled-plan.d.ts +44 -0
  21. package/dist/compiled-plan.js +1 -0
  22. package/dist/compiler-compatibility-gate.d.ts +96 -0
  23. package/dist/compiler-compatibility-gate.js +609 -0
  24. package/dist/compiler-release-authority.d.ts +212 -0
  25. package/dist/compiler-release-authority.js +676 -0
  26. package/dist/compiler-stage-cache.d.ts +42 -0
  27. package/dist/compiler-stage-cache.js +100 -0
  28. package/dist/compiler-stage-registry.d.ts +48 -0
  29. package/dist/compiler-stage-registry.js +126 -0
  30. package/dist/compiler.d.ts +7 -0
  31. package/dist/compiler.js +68 -0
  32. package/dist/condition-graph.d.ts +46 -0
  33. package/dist/condition-graph.js +135 -0
  34. package/dist/diagnostics.d.ts +20 -0
  35. package/dist/diagnostics.js +20 -0
  36. package/dist/durable-intent.d.ts +29 -0
  37. package/dist/durable-intent.js +60 -0
  38. package/dist/effect-flow.d.ts +69 -0
  39. package/dist/effect-flow.js +98 -0
  40. package/dist/event-fabric.d.ts +81 -0
  41. package/dist/event-fabric.js +227 -0
  42. package/dist/event-lifecycle.d.ts +59 -0
  43. package/dist/event-lifecycle.js +161 -0
  44. package/dist/evidence.d.ts +37 -0
  45. package/dist/evidence.js +92 -0
  46. package/dist/gameplay-state.d.ts +127 -0
  47. package/dist/gameplay-state.js +214 -0
  48. package/dist/index.d.ts +66 -0
  49. package/dist/index.js +52 -0
  50. package/dist/isolated-replay.d.ts +81 -0
  51. package/dist/isolated-replay.js +316 -0
  52. package/dist/music-candidate-set.d.ts +38 -0
  53. package/dist/music-candidate-set.js +102 -0
  54. package/dist/natural-language-chunking.d.ts +10 -0
  55. package/dist/natural-language-chunking.js +62 -0
  56. package/dist/natural-language-ingestion.d.ts +14 -0
  57. package/dist/natural-language-ingestion.js +128 -0
  58. package/dist/natural-language-structured-decode.d.ts +2 -0
  59. package/dist/natural-language-structured-decode.js +69 -0
  60. package/dist/natural-language-world-types.d.ts +142 -0
  61. package/dist/natural-language-world-types.js +2 -0
  62. package/dist/neutral-source-compiler.d.ts +135 -0
  63. package/dist/neutral-source-compiler.js +608 -0
  64. package/dist/presentation-intent.d.ts +42 -0
  65. package/dist/presentation-intent.js +137 -0
  66. package/dist/recall-query-envelope.d.ts +32 -0
  67. package/dist/recall-query-envelope.js +51 -0
  68. package/dist/rewrite-compensation.d.ts +28 -0
  69. package/dist/rewrite-compensation.js +85 -0
  70. package/dist/runtime-contract.d.ts +56 -0
  71. package/dist/runtime-contract.js +96 -0
  72. package/dist/runtime-domain-capabilities.d.ts +12 -0
  73. package/dist/runtime-domain-capabilities.js +20 -0
  74. package/dist/runtime-sav.d.ts +117 -0
  75. package/dist/runtime-sav.js +356 -0
  76. package/dist/runtime-trace.d.ts +44 -0
  77. package/dist/runtime-trace.js +119 -0
  78. package/dist/runtime.d.ts +238 -0
  79. package/dist/runtime.js +1887 -0
  80. package/dist/scene-author-source.d.ts +13 -0
  81. package/dist/scene-author-source.js +25 -0
  82. package/dist/scene-state.d.ts +275 -0
  83. package/dist/scene-state.js +334 -0
  84. package/dist/scheduler.d.ts +55 -0
  85. package/dist/scheduler.js +235 -0
  86. package/dist/selector-ast.d.ts +24 -0
  87. package/dist/selector-ast.js +18 -0
  88. package/dist/semantic-verdict.d.ts +48 -0
  89. package/dist/semantic-verdict.js +101 -0
  90. package/dist/simulator.d.ts +45 -0
  91. package/dist/simulator.js +58 -0
  92. package/dist/source-canonical.d.ts +4 -0
  93. package/dist/source-canonical.js +51 -0
  94. package/dist/source-crypto.d.ts +20 -0
  95. package/dist/source-crypto.js +17 -0
  96. package/dist/source-revision.d.ts +22 -0
  97. package/dist/source-revision.js +43 -0
  98. package/dist/spatial-impact-receipt.d.ts +38 -0
  99. package/dist/spatial-impact-receipt.js +67 -0
  100. package/dist/temporal-rewrite.d.ts +59 -0
  101. package/dist/temporal-rewrite.js +214 -0
  102. package/dist/time-domains.d.ts +37 -0
  103. package/dist/time-domains.js +142 -0
  104. package/dist/timeline-revision.d.ts +28 -0
  105. package/dist/timeline-revision.js +59 -0
  106. package/dist/trigger-document-v3.d.ts +99 -0
  107. package/dist/trigger-document-v3.js +334 -0
  108. package/dist/turn-commit.d.ts +56 -0
  109. package/dist/turn-commit.js +190 -0
  110. package/dist/validator.d.ts +7 -0
  111. package/dist/validator.js +142 -0
  112. package/dist/value-graph.d.ts +98 -0
  113. package/dist/value-graph.js +318 -0
  114. package/dist/view-event-delivery.d.ts +47 -0
  115. package/dist/view-event-delivery.js +197 -0
  116. package/dist/view-interaction-manifest-v3.d.ts +60 -0
  117. package/dist/view-interaction-manifest-v3.js +198 -0
  118. package/dist/view-payload-schema.d.ts +17 -0
  119. package/dist/view-payload-schema.js +84 -0
  120. package/package.json +62 -0
@@ -0,0 +1,137 @@
1
+ const SHA256_HEX = /^[a-f0-9]{64}$/;
2
+ const STABLE_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
3
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
+ const isTmwJsonValue = (value) => {
5
+ if (value === null || typeof value === "boolean" || typeof value === "string")
6
+ return true;
7
+ if (typeof value === "number")
8
+ return Number.isFinite(value);
9
+ if (Array.isArray(value))
10
+ return value.every(isTmwJsonValue);
11
+ return isRecord(value) && Object.values(value).every(isTmwJsonValue);
12
+ };
13
+ const exactKeys = (value, keys) => Object.keys(value).sort().join("\0") === [...keys].sort().join("\0");
14
+ export const parsePresentationPayloadV3 = (value) => {
15
+ if (!isRecord(value))
16
+ return null;
17
+ if (value.kind === "view") {
18
+ if (!exactKeys(value, ["kind", "overlay_id", "visible"]))
19
+ return null;
20
+ if (typeof value.overlay_id !== "string" || !STABLE_ID.test(value.overlay_id))
21
+ return null;
22
+ if (typeof value.visible !== "boolean")
23
+ return null;
24
+ }
25
+ else if (value.kind === "atmosphere") {
26
+ if (!exactKeys(value, ["atmosphere_id", "intensity", "kind"]))
27
+ return null;
28
+ if (value.atmosphere_id !== null
29
+ && (typeof value.atmosphere_id !== "string" || !STABLE_ID.test(value.atmosphere_id)))
30
+ return null;
31
+ if (typeof value.intensity !== "number" || !Number.isFinite(value.intensity)
32
+ || value.intensity < 0 || value.intensity > 1)
33
+ return null;
34
+ }
35
+ else if (value.kind === "music") {
36
+ if (!exactKeys(value, [
37
+ "candidate_set_id",
38
+ "candidate_set_revision",
39
+ "kind",
40
+ "preferred_resource_id"
41
+ ]))
42
+ return null;
43
+ if (typeof value.candidate_set_id !== "string" || !STABLE_ID.test(value.candidate_set_id))
44
+ return null;
45
+ if (typeof value.candidate_set_revision !== "number"
46
+ || !Number.isSafeInteger(value.candidate_set_revision)
47
+ || value.candidate_set_revision <= 0)
48
+ return null;
49
+ if (value.preferred_resource_id !== null
50
+ && (typeof value.preferred_resource_id !== "string" || !STABLE_ID.test(value.preferred_resource_id)))
51
+ return null;
52
+ }
53
+ else
54
+ return null;
55
+ return value;
56
+ };
57
+ export const parsePresentationIntentV3 = (value) => {
58
+ if (!isRecord(value) || !exactKeys(value, [
59
+ "activation_id",
60
+ "artifact_digest",
61
+ "dedupe_key",
62
+ "epoch",
63
+ "generation",
64
+ "intent_id",
65
+ "metadata",
66
+ "payload",
67
+ "schema",
68
+ "sequence",
69
+ "timeline_revision"
70
+ ]))
71
+ return null;
72
+ if (value.schema !== "presentation-intent.v3" || value.generation !== 3)
73
+ return null;
74
+ if (typeof value.intent_id !== "string" || !STABLE_ID.test(value.intent_id)
75
+ || typeof value.activation_id !== "string" || !STABLE_ID.test(value.activation_id)
76
+ || typeof value.dedupe_key !== "string" || !STABLE_ID.test(value.dedupe_key)
77
+ || typeof value.timeline_revision !== "string" || !STABLE_ID.test(value.timeline_revision))
78
+ return null;
79
+ if (!Number.isSafeInteger(value.epoch) || value.epoch <= 0)
80
+ return null;
81
+ if (typeof value.artifact_digest !== "string" || !SHA256_HEX.test(value.artifact_digest))
82
+ return null;
83
+ if (!Number.isSafeInteger(value.sequence) || value.sequence < 0)
84
+ return null;
85
+ if (parsePresentationPayloadV3(value.payload) === null
86
+ || !isRecord(value.metadata)
87
+ || !isTmwJsonValue(value.metadata))
88
+ return null;
89
+ return value;
90
+ };
91
+ export const committedPresentationActivationError = (activation) => {
92
+ const { candidate, receipt } = activation;
93
+ if (receipt.status !== "committed" && receipt.status !== "already_committed") {
94
+ return "presentation_activation_not_committed";
95
+ }
96
+ if (receipt.operationId !== candidate.operationId
97
+ || receipt.activeHead.revisionId !== candidate.timeline.revisionId
98
+ || receipt.activeHead.revisionSequence !== candidate.timeline.revisionSequence
99
+ || receipt.activeHead.ledgerHead !== candidate.timeline.ledgerHead) {
100
+ return "presentation_activation_mismatch";
101
+ }
102
+ if (receipt.atomicWriteCount !== 5)
103
+ return "presentation_activation_atomic_write_count_invalid";
104
+ if (!SHA256_HEX.test(receipt.candidateDigest) || !SHA256_HEX.test(receipt.receiptDigest)) {
105
+ return "presentation_activation_receipt_digest_invalid";
106
+ }
107
+ return null;
108
+ };
109
+ export const presentationIntentError = (intent, activation) => {
110
+ const { candidate, receipt } = activation;
111
+ const activationError = committedPresentationActivationError(activation);
112
+ if (activationError !== null)
113
+ return activationError;
114
+ if (intent.schema !== "presentation-intent.v3" || intent.generation !== 3)
115
+ return "presentation_intent_generation_invalid";
116
+ if (!STABLE_ID.test(intent.intent_id) || !STABLE_ID.test(intent.activation_id) || !STABLE_ID.test(intent.dedupe_key)) {
117
+ return "presentation_intent_identity_invalid";
118
+ }
119
+ if (!Number.isSafeInteger(intent.epoch) || intent.epoch <= 0 || intent.epoch !== candidate.epoch)
120
+ return "stale_content_epoch";
121
+ if (!SHA256_HEX.test(intent.artifact_digest) || intent.artifact_digest !== candidate.artifactDigest) {
122
+ return "presentation_artifact_mismatch";
123
+ }
124
+ if (!STABLE_ID.test(intent.timeline_revision))
125
+ return "presentation_timeline_revision_invalid";
126
+ if (intent.timeline_revision !== candidate.timeline.revisionId
127
+ || receipt.activeHead.revisionId !== candidate.timeline.revisionId
128
+ || intent.activation_id !== candidate.operationId
129
+ || receipt.operationId !== candidate.operationId) {
130
+ return "presentation_activation_mismatch";
131
+ }
132
+ if (!Number.isSafeInteger(intent.sequence) || intent.sequence < 0)
133
+ return "presentation_sequence_invalid";
134
+ if (parsePresentationPayloadV3(intent.payload) === null)
135
+ return "presentation_payload_invalid";
136
+ return null;
137
+ };
@@ -0,0 +1,32 @@
1
+ import { TMW_RUNTIME_GENERATION } from "./runtime-contract.js";
2
+ import { type TmwJsonValue } from "./runtime-trace.js";
3
+ export interface RecallQueryEnvelopeV3 {
4
+ readonly schemaVersion: 3;
5
+ readonly epoch: number;
6
+ readonly runtimeGeneration: typeof TMW_RUNTIME_GENERATION;
7
+ readonly runtimeContractHash: string;
8
+ readonly runtimeLeaseId: string;
9
+ readonly artifactDigest: string;
10
+ readonly artifactRevision: string;
11
+ readonly sourceRevision: string;
12
+ readonly timelineRevision: string;
13
+ readonly turnId: string;
14
+ readonly operationId: string;
15
+ readonly queryId: string;
16
+ readonly generation: number;
17
+ readonly query: TmwJsonValue;
18
+ readonly queryCanonicalJson: string;
19
+ readonly queryDigest: string;
20
+ }
21
+ export declare function canonicalRecallQueryEnvelope(input: RecallQueryEnvelopeV3): RecallQueryEnvelopeV3;
22
+ export declare function assertRecallQueryBinding(envelopeInput: RecallQueryEnvelopeV3, expected: Readonly<{
23
+ epoch: number;
24
+ runtimeContractHash: string;
25
+ runtimeLeaseId: string;
26
+ artifactDigest: string;
27
+ artifactRevision: string;
28
+ sourceRevision: string;
29
+ timelineRevision: string;
30
+ turnId: string;
31
+ generation: number;
32
+ }>): RecallQueryEnvelopeV3;
@@ -0,0 +1,51 @@
1
+ import { TMW_RUNTIME_GENERATION } from "./runtime-contract.js";
2
+ import { canonicalTmwJson, canonicalTmwJsonValue, requireTmwIdentifier, requireTmwSafeInteger, requireTmwSha256, } from "./runtime-trace.js";
3
+ export function canonicalRecallQueryEnvelope(input) {
4
+ if (input.schemaVersion !== 3 || input.runtimeGeneration !== 3) {
5
+ throw new Error("unsupported_runtime_generation");
6
+ }
7
+ const query = canonicalTmwJsonValue(input.query, "recall_query");
8
+ const queryCanonicalJson = canonicalTmwJson(query);
9
+ if (input.queryCanonicalJson !== queryCanonicalJson) {
10
+ throw new Error("recall_query_canonical_json_mismatch");
11
+ }
12
+ return Object.freeze({
13
+ schemaVersion: 3,
14
+ epoch: requireTmwSafeInteger(input.epoch, "recall_epoch", 1),
15
+ runtimeGeneration: TMW_RUNTIME_GENERATION,
16
+ runtimeContractHash: requireTmwSha256(input.runtimeContractHash, "recall_contract_hash"),
17
+ runtimeLeaseId: requireTmwIdentifier(input.runtimeLeaseId, "recall_runtime_lease_id"),
18
+ artifactDigest: requireTmwSha256(input.artifactDigest, "recall_artifact_digest"),
19
+ artifactRevision: requireTmwIdentifier(input.artifactRevision, "recall_artifact_revision"),
20
+ sourceRevision: requireTmwIdentifier(input.sourceRevision, "recall_source_revision"),
21
+ timelineRevision: requireTmwIdentifier(input.timelineRevision, "recall_timeline_revision"),
22
+ turnId: requireTmwIdentifier(input.turnId, "recall_turn_id"),
23
+ operationId: requireTmwIdentifier(input.operationId, "recall_operation_id"),
24
+ queryId: requireTmwIdentifier(input.queryId, "recall_query_id"),
25
+ generation: requireTmwSafeInteger(input.generation, "recall_generation", 1),
26
+ query,
27
+ queryCanonicalJson,
28
+ queryDigest: requireTmwSha256(input.queryDigest, "recall_query_digest"),
29
+ });
30
+ }
31
+ export function assertRecallQueryBinding(envelopeInput, expected) {
32
+ const envelope = canonicalRecallQueryEnvelope(envelopeInput);
33
+ if (envelope.epoch !== expected.epoch)
34
+ throw new Error("stale_content_epoch");
35
+ if (envelope.runtimeContractHash !== expected.runtimeContractHash) {
36
+ throw new Error("runtime_contract_hash_mismatch");
37
+ }
38
+ for (const [label, actual, wanted] of [
39
+ ["runtime_lease_id", envelope.runtimeLeaseId, expected.runtimeLeaseId],
40
+ ["artifact_digest", envelope.artifactDigest, expected.artifactDigest],
41
+ ["artifact_revision", envelope.artifactRevision, expected.artifactRevision],
42
+ ["source_revision", envelope.sourceRevision, expected.sourceRevision],
43
+ ["timeline_revision", envelope.timelineRevision, expected.timelineRevision],
44
+ ["turn_id", envelope.turnId, expected.turnId],
45
+ ["generation", envelope.generation, expected.generation],
46
+ ]) {
47
+ if (actual !== wanted)
48
+ throw new Error(`tmw_recall_${label}_mismatch`);
49
+ }
50
+ return envelope;
51
+ }
@@ -0,0 +1,28 @@
1
+ import type { TemporalRewriteCompensationProposalV3 } from "./temporal-rewrite.js";
2
+ export interface RewriteCompensationReceiptV3 {
3
+ readonly operationId: string;
4
+ readonly sourceTimelineRevision: string;
5
+ readonly targetTimelineRevision: string;
6
+ readonly externalEffectId: string;
7
+ readonly policy: TemporalRewriteCompensationProposalV3["policy"];
8
+ readonly status: "recorded";
9
+ readonly reasonCode: string;
10
+ readonly receiptDigest: string;
11
+ }
12
+ export interface RewriteCompensationReceiptSetV3 {
13
+ readonly receipts: ReadonlyArray<RewriteCompensationReceiptV3>;
14
+ readonly receiptSetDigest: string;
15
+ }
16
+ export declare function createRewriteCompensationReceipts(input: Readonly<{
17
+ operationId: string;
18
+ sourceTimelineRevision: string;
19
+ targetTimelineRevision: string;
20
+ proposals: ReadonlyArray<TemporalRewriteCompensationProposalV3>;
21
+ }>): Promise<RewriteCompensationReceiptSetV3>;
22
+ export declare function recordRewriteCompensations(input: Readonly<{
23
+ operationId: string;
24
+ sourceTimelineRevision: string;
25
+ targetTimelineRevision: string;
26
+ proposals: ReadonlyArray<TemporalRewriteCompensationProposalV3>;
27
+ receiptDigests: Readonly<Record<string, string>>;
28
+ }>): ReadonlyArray<RewriteCompensationReceiptV3>;
@@ -0,0 +1,85 @@
1
+ import { canonicalTmwJson, requireTmwIdentifier, requireTmwSha256, tmwSha256Hex, } from "./runtime-trace.js";
2
+ function canonicalPolicy(value) {
3
+ if (value !== "retain_fact" && value !== "project_only" && value !== "requires_confirmation") {
4
+ throw new Error("tmw_compensation_policy_invalid");
5
+ }
6
+ return value;
7
+ }
8
+ function canonicalReceiptImage(input) {
9
+ if (input.status !== "recorded")
10
+ throw new Error("tmw_compensation_status_invalid");
11
+ return Object.freeze({
12
+ operationId: requireTmwIdentifier(input.operationId, "compensation_operation_id"),
13
+ sourceTimelineRevision: requireTmwIdentifier(input.sourceTimelineRevision, "compensation_source_timeline"),
14
+ targetTimelineRevision: requireTmwIdentifier(input.targetTimelineRevision, "compensation_target_timeline"),
15
+ externalEffectId: requireTmwIdentifier(input.externalEffectId, "compensation_external_effect_id"),
16
+ policy: canonicalPolicy(input.policy),
17
+ status: "recorded",
18
+ reasonCode: requireTmwIdentifier(input.reasonCode, "compensation_reason_code"),
19
+ });
20
+ }
21
+ export async function createRewriteCompensationReceipts(input) {
22
+ const operationId = requireTmwIdentifier(input.operationId, "compensation_operation_id");
23
+ const sourceTimelineRevision = requireTmwIdentifier(input.sourceTimelineRevision, "compensation_source_timeline");
24
+ const targetTimelineRevision = requireTmwIdentifier(input.targetTimelineRevision, "compensation_target_timeline");
25
+ const seen = new Set();
26
+ const receipts = [];
27
+ for (const proposal of [...input.proposals].sort((left, right) => left.externalEffectId < right.externalEffectId
28
+ ? -1
29
+ : left.externalEffectId > right.externalEffectId
30
+ ? 1
31
+ : 0)) {
32
+ const externalEffectId = requireTmwIdentifier(proposal.externalEffectId, "compensation_external_effect_id");
33
+ if (seen.has(externalEffectId))
34
+ throw new Error("tmw_compensation_effect_duplicate");
35
+ seen.add(externalEffectId);
36
+ const image = canonicalReceiptImage({
37
+ operationId,
38
+ sourceTimelineRevision,
39
+ targetTimelineRevision,
40
+ externalEffectId,
41
+ policy: proposal.policy,
42
+ status: "recorded",
43
+ reasonCode: proposal.reasonCode,
44
+ });
45
+ receipts.push(Object.freeze({
46
+ ...image,
47
+ receiptDigest: await tmwSha256Hex(canonicalTmwJson(image)),
48
+ }));
49
+ }
50
+ const receiptSetDigest = await tmwSha256Hex(canonicalTmwJson(receipts.map((receipt) => receipt.receiptDigest)));
51
+ return Object.freeze({
52
+ receipts: Object.freeze(receipts),
53
+ receiptSetDigest,
54
+ });
55
+ }
56
+ export function recordRewriteCompensations(input) {
57
+ const operationId = requireTmwIdentifier(input.operationId, "compensation_operation_id");
58
+ const sourceTimelineRevision = requireTmwIdentifier(input.sourceTimelineRevision, "compensation_source_timeline");
59
+ const targetTimelineRevision = requireTmwIdentifier(input.targetTimelineRevision, "compensation_target_timeline");
60
+ const seen = new Set();
61
+ return Object.freeze([...input.proposals]
62
+ .sort((left, right) => left.externalEffectId < right.externalEffectId ? -1 : 1)
63
+ .map((proposal) => {
64
+ const externalEffectId = requireTmwIdentifier(proposal.externalEffectId, "compensation_external_effect_id");
65
+ if (seen.has(externalEffectId))
66
+ throw new Error("tmw_compensation_effect_duplicate");
67
+ seen.add(externalEffectId);
68
+ const receiptDigest = input.receiptDigests[externalEffectId];
69
+ if (receiptDigest === undefined)
70
+ throw new Error("tmw_compensation_receipt_missing");
71
+ const image = canonicalReceiptImage({
72
+ operationId,
73
+ sourceTimelineRevision,
74
+ targetTimelineRevision,
75
+ externalEffectId,
76
+ policy: canonicalPolicy(proposal.policy),
77
+ status: "recorded",
78
+ reasonCode: requireTmwIdentifier(proposal.reasonCode, "compensation_reason_code"),
79
+ });
80
+ return Object.freeze({
81
+ ...image,
82
+ receiptDigest: requireTmwSha256(receiptDigest, "compensation_receipt_digest"),
83
+ });
84
+ }));
85
+ }
@@ -0,0 +1,56 @@
1
+ export declare const TMW_RUNTIME_GENERATION: 3;
2
+ export declare const TRIGGER_DOCUMENT_V3_VERSION: 3;
3
+ export declare const TMW_COMPILED_PLAN_VERSION: 3;
4
+ export declare const AUTHOR_SCENARIO_SOURCE_V3_SCHEMA_VERSION: 3;
5
+ export declare const TMW_CONDITION_EXPRESSION_REVISION: 2;
6
+ export declare const TMW_SEMANTIC_JUDGE_RECEIPT_REVISION: 2;
7
+ export declare const TMW_SEMANTIC_CONFIDENCE_THRESHOLD_MILLIONTHS: 600000;
8
+ export declare const TMW_NODE_ENABLEMENT_REVISION: 1;
9
+ export declare const TMW_SCENE_SEMANTICS_REVISION: 1;
10
+ export declare const TMW_VALUE_GRAPH_SEMANTICS_REVISION: 1;
11
+ export declare const TMW_EVENT_LIFECYCLE_SEMANTICS_REVISION: 1;
12
+ /** committed event 到 View SDK 的显式、仅下行投影合同。 */
13
+ export declare const TMW_VIEW_EVENT_DELIVERY_SEMANTICS_REVISION: 1;
14
+ /** Last deployed canonical contract before Scene became Runtime SAV authority. */
15
+ export declare const TMW_PRE_SCENE_RUNTIME_CONTRACT_V3: Readonly<{
16
+ runtime_generation: 3;
17
+ trigger_document_version: 3;
18
+ compiled_plan_version: 3;
19
+ author_source_schema_version: 3;
20
+ condition_expression_revision: 2;
21
+ semantic_judge_receipt_revision: 2;
22
+ semantic_confidence_threshold_millionths: 600000;
23
+ node_enablement_revision: 1;
24
+ time_domains: readonly ["event", "condition", "effect", "presentation"];
25
+ rejection_codes: readonly ["content_epoch_required", "stale_content_epoch", "tmw3_required", "unsupported_runtime_generation", "unsupported_sav_generation", "sav_section_missing", "runtime_contract_hash_mismatch"];
26
+ }>;
27
+ export type TmwJsonPrimitive = null | boolean | number | string;
28
+ export type TmwJsonValue = TmwJsonPrimitive | readonly TmwJsonValue[] | {
29
+ readonly [key: string]: TmwJsonValue;
30
+ };
31
+ export declare const TMW_RUNTIME_CONTRACT_V3: Readonly<{
32
+ runtime_generation: 3;
33
+ trigger_document_version: 3;
34
+ compiled_plan_version: 3;
35
+ author_source_schema_version: 3;
36
+ condition_expression_revision: 2;
37
+ semantic_judge_receipt_revision: 2;
38
+ semantic_confidence_threshold_millionths: 600000;
39
+ node_enablement_revision: 1;
40
+ scene_semantics_revision: 1;
41
+ gameplay_state_semantics_revision: 1;
42
+ value_graph_semantics_revision: 1;
43
+ event_lifecycle_semantics_revision: 1;
44
+ view_event_delivery_semantics_revision: 1;
45
+ value_graph_schema_version: 1;
46
+ time_domains: readonly ["event", "condition", "effect", "presentation"];
47
+ rejection_codes: readonly ["content_epoch_required", "stale_content_epoch", "tmw3_required", "unsupported_runtime_generation", "unsupported_sav_generation", "sav_section_missing", "runtime_contract_hash_mismatch"];
48
+ }>;
49
+ export type TmwRuntimeRejectionCode = typeof TMW_RUNTIME_CONTRACT_V3.rejection_codes[number];
50
+ export declare function canonicalTmwJson(value: TmwJsonValue | object): string;
51
+ export declare function canonicalTmwBytes(value: TmwJsonValue | object): Uint8Array;
52
+ export declare function tmwSha256Hex(bytes: Uint8Array, subtle?: SubtleCrypto): Promise<string>;
53
+ export declare function canonicalRuntimeContractV3Bytes(): Uint8Array;
54
+ export declare function runtimeContractHashV3(subtle?: SubtleCrypto): Promise<string>;
55
+ export declare function preSceneRuntimeContractHashV3(subtle?: SubtleCrypto): Promise<string>;
56
+ export declare function isSha256Hex(value: unknown): value is string;
@@ -0,0 +1,96 @@
1
+ export const TMW_RUNTIME_GENERATION = 3;
2
+ export const TRIGGER_DOCUMENT_V3_VERSION = 3;
3
+ export const TMW_COMPILED_PLAN_VERSION = 3;
4
+ export const AUTHOR_SCENARIO_SOURCE_V3_SCHEMA_VERSION = 3;
5
+ export const TMW_CONDITION_EXPRESSION_REVISION = 2;
6
+ export const TMW_SEMANTIC_JUDGE_RECEIPT_REVISION = 2;
7
+ export const TMW_SEMANTIC_CONFIDENCE_THRESHOLD_MILLIONTHS = 600_000;
8
+ export const TMW_NODE_ENABLEMENT_REVISION = 1;
9
+ export const TMW_SCENE_SEMANTICS_REVISION = 1;
10
+ export const TMW_VALUE_GRAPH_SEMANTICS_REVISION = 1;
11
+ export const TMW_EVENT_LIFECYCLE_SEMANTICS_REVISION = 1;
12
+ /** committed event 到 View SDK 的显式、仅下行投影合同。 */
13
+ export const TMW_VIEW_EVENT_DELIVERY_SEMANTICS_REVISION = 1;
14
+ /** Last deployed canonical contract before Scene became Runtime SAV authority. */
15
+ export const TMW_PRE_SCENE_RUNTIME_CONTRACT_V3 = Object.freeze({
16
+ runtime_generation: TMW_RUNTIME_GENERATION,
17
+ trigger_document_version: TRIGGER_DOCUMENT_V3_VERSION,
18
+ compiled_plan_version: TMW_COMPILED_PLAN_VERSION,
19
+ author_source_schema_version: AUTHOR_SCENARIO_SOURCE_V3_SCHEMA_VERSION,
20
+ condition_expression_revision: TMW_CONDITION_EXPRESSION_REVISION,
21
+ semantic_judge_receipt_revision: TMW_SEMANTIC_JUDGE_RECEIPT_REVISION,
22
+ semantic_confidence_threshold_millionths: TMW_SEMANTIC_CONFIDENCE_THRESHOLD_MILLIONTHS,
23
+ node_enablement_revision: TMW_NODE_ENABLEMENT_REVISION,
24
+ time_domains: Object.freeze(["event", "condition", "effect", "presentation"]),
25
+ rejection_codes: Object.freeze([
26
+ "content_epoch_required", "stale_content_epoch", "tmw3_required",
27
+ "unsupported_runtime_generation", "unsupported_sav_generation", "sav_section_missing",
28
+ "runtime_contract_hash_mismatch",
29
+ ]),
30
+ });
31
+ export const TMW_RUNTIME_CONTRACT_V3 = Object.freeze({
32
+ runtime_generation: TMW_RUNTIME_GENERATION,
33
+ trigger_document_version: TRIGGER_DOCUMENT_V3_VERSION,
34
+ compiled_plan_version: TMW_COMPILED_PLAN_VERSION,
35
+ author_source_schema_version: AUTHOR_SCENARIO_SOURCE_V3_SCHEMA_VERSION,
36
+ condition_expression_revision: TMW_CONDITION_EXPRESSION_REVISION,
37
+ semantic_judge_receipt_revision: TMW_SEMANTIC_JUDGE_RECEIPT_REVISION,
38
+ semantic_confidence_threshold_millionths: TMW_SEMANTIC_CONFIDENCE_THRESHOLD_MILLIONTHS,
39
+ node_enablement_revision: TMW_NODE_ENABLEMENT_REVISION,
40
+ scene_semantics_revision: TMW_SCENE_SEMANTICS_REVISION,
41
+ gameplay_state_semantics_revision: 1,
42
+ value_graph_semantics_revision: TMW_VALUE_GRAPH_SEMANTICS_REVISION,
43
+ event_lifecycle_semantics_revision: TMW_EVENT_LIFECYCLE_SEMANTICS_REVISION,
44
+ view_event_delivery_semantics_revision: TMW_VIEW_EVENT_DELIVERY_SEMANTICS_REVISION,
45
+ value_graph_schema_version: 1,
46
+ time_domains: Object.freeze(["event", "condition", "effect", "presentation"]),
47
+ rejection_codes: Object.freeze([
48
+ "content_epoch_required",
49
+ "stale_content_epoch",
50
+ "tmw3_required",
51
+ "unsupported_runtime_generation",
52
+ "unsupported_sav_generation",
53
+ "sav_section_missing",
54
+ "runtime_contract_hash_mismatch",
55
+ ]),
56
+ });
57
+ function canonicalValue(value, path) {
58
+ if (value === null || typeof value === "boolean" || typeof value === "string")
59
+ return JSON.stringify(value);
60
+ if (typeof value === "number") {
61
+ if (!Number.isFinite(value))
62
+ throw new Error(`${path}: canonical_number_not_finite`);
63
+ return JSON.stringify(value);
64
+ }
65
+ if (Array.isArray(value)) {
66
+ return `[${value.map((item, index) => canonicalValue(item, `${path}/${index}`)).join(",")}]`;
67
+ }
68
+ if (typeof value !== "object" || value === null)
69
+ throw new Error(`${path}: canonical_value_invalid`);
70
+ const record = value;
71
+ return `{${Object.keys(record).sort().map((key) => `${JSON.stringify(key)}:${canonicalValue(record[key], `${path}/${key}`)}`).join(",")}}`;
72
+ }
73
+ export function canonicalTmwJson(value) {
74
+ return canonicalValue(value, "$");
75
+ }
76
+ export function canonicalTmwBytes(value) {
77
+ return new TextEncoder().encode(canonicalTmwJson(value));
78
+ }
79
+ export async function tmwSha256Hex(bytes, subtle = globalThis.crypto.subtle) {
80
+ const digestInput = new Uint8Array(bytes.byteLength);
81
+ digestInput.set(bytes);
82
+ const digest = await subtle.digest("SHA-256", digestInput);
83
+ return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
84
+ }
85
+ export function canonicalRuntimeContractV3Bytes() {
86
+ return canonicalTmwBytes(TMW_RUNTIME_CONTRACT_V3);
87
+ }
88
+ export async function runtimeContractHashV3(subtle) {
89
+ return tmwSha256Hex(canonicalRuntimeContractV3Bytes(), subtle);
90
+ }
91
+ export async function preSceneRuntimeContractHashV3(subtle) {
92
+ return tmwSha256Hex(canonicalTmwBytes(TMW_PRE_SCENE_RUNTIME_CONTRACT_V3), subtle);
93
+ }
94
+ export function isSha256Hex(value) {
95
+ return typeof value === "string" && /^[0-9a-f]{64}$/u.test(value);
96
+ }
@@ -0,0 +1,12 @@
1
+ import type { TmwClockDomain } from "./time-domains.js";
2
+ export type TmwRuntimeCapabilityTarget = "production" | "simulation";
3
+ export interface TmwRuntimeDomainCapability {
4
+ readonly target: TmwRuntimeCapabilityTarget;
5
+ readonly produced_clock_domains: readonly TmwClockDomain[];
6
+ }
7
+ /**
8
+ * TMW 时钟 producer 的单一 capability authority。
9
+ * production 不接受作者输入扩权;simulation 仅表示显式人工观察可用于预览。
10
+ */
11
+ export declare function getTmwRuntimeDomainCapability(target: TmwRuntimeCapabilityTarget): TmwRuntimeDomainCapability;
12
+ export declare function tmwRuntimeProducesClockDomain(target: TmwRuntimeCapabilityTarget, domain: TmwClockDomain): boolean;
@@ -0,0 +1,20 @@
1
+ const CAPABILITIES = Object.freeze({
2
+ production: Object.freeze({
3
+ target: "production",
4
+ produced_clock_domains: Object.freeze(["logical_seq", "world_time"]),
5
+ }),
6
+ simulation: Object.freeze({
7
+ target: "simulation",
8
+ produced_clock_domains: Object.freeze(["logical_seq", "world_time", "presentation_seq", "real_time"]),
9
+ }),
10
+ });
11
+ /**
12
+ * TMW 时钟 producer 的单一 capability authority。
13
+ * production 不接受作者输入扩权;simulation 仅表示显式人工观察可用于预览。
14
+ */
15
+ export function getTmwRuntimeDomainCapability(target) {
16
+ return CAPABILITIES[target];
17
+ }
18
+ export function tmwRuntimeProducesClockDomain(target, domain) {
19
+ return getTmwRuntimeDomainCapability(target).produced_clock_domains.includes(domain);
20
+ }
@@ -0,0 +1,117 @@
1
+ import { TMW_RUNTIME_GENERATION } from "./runtime-contract.js";
2
+ import { type DurableIntentV3 } from "./durable-intent.js";
3
+ import { type AudioPresentationProjectionV1 } from "./audio-presentation-projection.js";
4
+ import { type TmwGameplayStateV1 } from "./gameplay-state.js";
5
+ import { type SchedulerSnapshot } from "./scheduler.js";
6
+ import { type TmwPendingSceneClockCrossingV1, type TmwSceneStateV1 } from "./scene-state.js";
7
+ import { type TimelineRevisionV3 } from "./timeline-revision.js";
8
+ import { type TmwJsonValue } from "./runtime-trace.js";
9
+ export declare const TMW_RUNTIME_SAV_EXTENSION_ID: "tmw.runtime.v3";
10
+ export declare const TMW_RUNTIME_SAV_SCHEMA_VERSION: 3;
11
+ export declare const TMW_RUNTIME_SAV_CODEC = "application/vnd.world-engines.tmw-runtime-v3+json;canonical=1";
12
+ /** Structural mirror of SaveGeneration.extension_sections; protobuf remains authoritative. */
13
+ export interface RuntimeSavExtensionSectionV3 {
14
+ readonly extension_id: string;
15
+ readonly schema_version: number;
16
+ readonly payload: Uint8Array;
17
+ readonly codec: string;
18
+ readonly payload_sha256: Uint8Array;
19
+ readonly required: boolean;
20
+ }
21
+ export interface RuntimeSavBindingV3 {
22
+ readonly epoch: number;
23
+ readonly runtimeGeneration: typeof TMW_RUNTIME_GENERATION;
24
+ readonly runtimeContractHash: string;
25
+ readonly artifactDigest: string;
26
+ readonly sourceRevision: string;
27
+ readonly activeTimelineRevision: string;
28
+ readonly turnId: string;
29
+ readonly preActivationTerminalDigest: string;
30
+ }
31
+ export interface RuntimeSavRestoreScopeV3 {
32
+ readonly epoch: number;
33
+ readonly runtimeGeneration: typeof TMW_RUNTIME_GENERATION;
34
+ readonly runtimeContractHash: string;
35
+ readonly artifactDigest: string;
36
+ readonly sourceRevision: string;
37
+ }
38
+ export interface RuntimeSavPayloadV3 extends RuntimeSavBindingV3 {
39
+ readonly schemaVersion: 3;
40
+ readonly activeTimeline: TimelineRevisionV3;
41
+ readonly ledgerHead: string;
42
+ readonly eventLedger: TmwJsonValue;
43
+ readonly narrative: TmwJsonValue;
44
+ /** 玩法变量与实体状态随同 activation CAS 持久化;执行域变量不进入存档。 */
45
+ readonly gameplayState?: TmwGameplayStateV1;
46
+ readonly gameplayInitialState?: TmwGameplayStateV1;
47
+ /** Canonical transient scene authority. null means no active scene. */
48
+ readonly scene: TmwSceneStateV1 | null;
49
+ readonly pendingSceneClockCrossings: ReadonlyArray<TmwPendingSceneClockCrossingV1>;
50
+ readonly scheduler: SchedulerSnapshot;
51
+ readonly conditionState: TmwJsonValue;
52
+ readonly durableIntents: ReadonlyArray<DurableIntentV3>;
53
+ readonly presentationIntents: ReadonlyArray<TmwJsonValue>;
54
+ /** Optional for pre-Spec-017 saves; when present it is the committed plan/cue projection. */
55
+ readonly audioPresentation?: AudioPresentationProjectionV1 | null;
56
+ readonly semanticVerdictRefs: ReadonlyArray<string>;
57
+ readonly candidateSetRevisions: ReadonlyArray<string>;
58
+ readonly operationReceiptDigests: ReadonlyArray<string>;
59
+ /** Durable operation guard. Optional only for decoding pre-clock Runtime3 saves. */
60
+ readonly completedOperationIds?: ReadonlyArray<string>;
61
+ /** Exact WASM operations whose finalized readback must survive an activation/finalize crash. */
62
+ readonly worldMutationRecoveryOperations: ReadonlyArray<TmwJsonValue>;
63
+ }
64
+ export declare function canonicalRuntimeSavPayload(input: RuntimeSavPayloadV3): RuntimeSavPayloadV3;
65
+ export declare function runtimeSavSectionDigest(section: RuntimeSavExtensionSectionV3): Promise<string>;
66
+ export declare function runtimeSavPayloadDigest(input: RuntimeSavPayloadV3): Promise<string>;
67
+ export interface PreSceneRuntimeSavMigrationAuthorityV1 {
68
+ readonly expectedLegacyArtifactDigest: string;
69
+ readonly successorRuntimeContractHash: string;
70
+ readonly successorArtifactDigest: string;
71
+ readonly sceneId: string;
72
+ readonly presentEntityNodeIds: ReadonlyArray<string>;
73
+ readonly spatialRef: TmwSceneStateV1["spatialRef"];
74
+ readonly operationId: string;
75
+ readonly turnId: string;
76
+ }
77
+ /**
78
+ * Server-issued, immutable publication relation for the one explicit pre-Scene
79
+ * SAV upgrade. This is deliberately an identity record, not a hint carried by
80
+ * a SAV: callers must obtain it from the finalized-publication resolver.
81
+ */
82
+ export interface PreSceneRuntimeSavPublishedMigrationV1 {
83
+ readonly schemaVersion: 1;
84
+ readonly scenarioId: string;
85
+ readonly predecessor: Readonly<{
86
+ readonly contentEpoch: number;
87
+ readonly reviewedHeadDigest: string;
88
+ readonly runtimeContractHash: string;
89
+ readonly artifactDigest: string;
90
+ readonly sourceRevision: string;
91
+ }>;
92
+ readonly successor: Readonly<{
93
+ readonly contentEpoch: number;
94
+ readonly reviewedHeadDigest: string;
95
+ readonly runtimeContractHash: string;
96
+ readonly artifactDigest: string;
97
+ readonly sourceRevision: string;
98
+ readonly compiledPlanSha256: string;
99
+ }>;
100
+ }
101
+ /** Fail closed on a replay, cross-scenario, source, epoch, or plan identity drift. */
102
+ export declare function canonicalPreSceneRuntimeSavPublishedMigrationV1(input: PreSceneRuntimeSavPublishedMigrationV1): PreSceneRuntimeSavPublishedMigrationV1;
103
+ export interface PreSceneRuntimeSavMigrationReceiptV1 {
104
+ readonly legacyPayloadDigest: string;
105
+ readonly successorPayloadDigest: string;
106
+ readonly payload: RuntimeSavPayloadV3;
107
+ }
108
+ /** Explicit pre-Scene migration; normal decoders never synthesize Scene from a malformed field. */
109
+ export declare function migratePreSceneRuntimeSavV3(legacyInput: Omit<RuntimeSavPayloadV3, "scene" | "pendingSceneClockCrossings"> & Readonly<{
110
+ scene?: never;
111
+ }>, authority: PreSceneRuntimeSavMigrationAuthorityV1): Promise<PreSceneRuntimeSavMigrationReceiptV1>;
112
+ export declare function encodeRuntimeSavSection(input: RuntimeSavPayloadV3): Promise<RuntimeSavExtensionSectionV3>;
113
+ export declare function decodeRuntimeSavSection(section: RuntimeSavExtensionSectionV3, expected: RuntimeSavBindingV3): Promise<RuntimeSavPayloadV3>;
114
+ export declare function decodeRuntimeSavSectionForScope(section: RuntimeSavExtensionSectionV3, expected: RuntimeSavRestoreScopeV3): Promise<RuntimeSavPayloadV3>;
115
+ export declare function decodeRuntimeSavSectionSelfBound(section: RuntimeSavExtensionSectionV3): Promise<RuntimeSavPayloadV3>;
116
+ export declare function requireRuntimeSavSection(sections: ReadonlyArray<RuntimeSavExtensionSectionV3>, expected: RuntimeSavBindingV3): Promise<RuntimeSavPayloadV3>;
117
+ export declare function requireRuntimeSavSectionForScope(sections: ReadonlyArray<RuntimeSavExtensionSectionV3>, expected: RuntimeSavRestoreScopeV3): Promise<RuntimeSavPayloadV3>;