pgc-compiler 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. compiler/VERSION +1 -0
  2. compiler/__init__.py +1 -0
  3. compiler/atoms/__init__.py +54 -0
  4. compiler/atoms/error_codes.py +160 -0
  5. compiler/atoms/errors.py +125 -0
  6. compiler/atoms/fqdn.py +204 -0
  7. compiler/atoms/invariants.py +106 -0
  8. compiler/atoms/phase.py +99 -0
  9. compiler/atoms/pipeline.py +51 -0
  10. compiler/atoms/snapshot_gate.py +51 -0
  11. compiler/atoms/sorting.py +125 -0
  12. compiler/cli.py +637 -0
  13. compiler/diagnostics/__init__.py +6 -0
  14. compiler/diagnostics/determination.py +148 -0
  15. compiler/diagnostics/machine_block_health.py +199 -0
  16. compiler/governance/__init__.py +6 -0
  17. compiler/governance_engine/__init__.py +0 -0
  18. compiler/governance_engine/artifact_kinds.py +221 -0
  19. compiler/governance_engine/assertions/__init__.py +0 -0
  20. compiler/governance_engine/assertions/handlers/__init__.py +200 -0
  21. compiler/governance_engine/assertions/handlers/assert_ac_declaration_well_formed_v0.py +70 -0
  22. compiler/governance_engine/assertions/handlers/assert_actor_authority_separation_v0.py +17 -0
  23. compiler/governance_engine/assertions/handlers/assert_artifact_content_hash_declared_v0.py +60 -0
  24. compiler/governance_engine/assertions/handlers/assert_assert_not_runtime_referenced_v0.py +49 -0
  25. compiler/governance_engine/assertions/handlers/assert_assert_parity_v0.py +169 -0
  26. compiler/governance_engine/assertions/handlers/assert_atom_output_purity_v0.py +186 -0
  27. compiler/governance_engine/assertions/handlers/assert_authority_constituted_v0.py +56 -0
  28. compiler/governance_engine/assertions/handlers/assert_authority_required_for_execution_v0.py +16 -0
  29. compiler/governance_engine/assertions/handlers/assert_authority_state_well_formed_v0.py +17 -0
  30. compiler/governance_engine/assertions/handlers/assert_binding_integrity_v0.py +64 -0
  31. compiler/governance_engine/assertions/handlers/assert_binding_surface_closed_v0.py +92 -0
  32. compiler/governance_engine/assertions/handlers/assert_cc_capability_binding_valid_v0.py +85 -0
  33. compiler/governance_engine/assertions/handlers/assert_cc_inputs_satisfied_v0.py +89 -0
  34. compiler/governance_engine/assertions/handlers/assert_cc_no_implicit_chaining_v0.py +89 -0
  35. compiler/governance_engine/assertions/handlers/assert_cc_no_missing_dependencies_v0.py +89 -0
  36. compiler/governance_engine/assertions/handlers/assert_cc_no_unused_outputs_v0.py +81 -0
  37. compiler/governance_engine/assertions/handlers/assert_cc_storage_op_conformance_v0.py +74 -0
  38. compiler/governance_engine/assertions/handlers/assert_compiler_governance_declared_v0.py +65 -0
  39. compiler/governance_engine/assertions/handlers/assert_compiler_no_execution_v0.py +86 -0
  40. compiler/governance_engine/assertions/handlers/assert_concern_not_authority_v0.py +51 -0
  41. compiler/governance_engine/assertions/handlers/assert_conformance_assertion_mode_valid_v0.py +35 -0
  42. compiler/governance_engine/assertions/handlers/assert_cryptographic_trust_declared_v0.py +51 -0
  43. compiler/governance_engine/assertions/handlers/assert_cs_isolated_execution_v0.py +35 -0
  44. compiler/governance_engine/assertions/handlers/assert_cs_surface_closed_v0.py +241 -0
  45. compiler/governance_engine/assertions/handlers/assert_cs_traceable_v0.py +35 -0
  46. compiler/governance_engine/assertions/handlers/assert_ct_output_contract_match_v0.py +167 -0
  47. compiler/governance_engine/assertions/handlers/assert_ct_surface_closed_v0.py +173 -0
  48. compiler/governance_engine/assertions/handlers/assert_ct_surface_derived_closed_v0.py +108 -0
  49. compiler/governance_engine/assertions/handlers/assert_ct_test_data_outcome_declared_v0.py +112 -0
  50. compiler/governance_engine/assertions/handlers/assert_ev_append_only_v0.py +70 -0
  51. compiler/governance_engine/assertions/handlers/assert_ev_schema_required_v0.py +59 -0
  52. compiler/governance_engine/assertions/handlers/assert_execution_placement_declared_v0.py +51 -0
  53. compiler/governance_engine/assertions/handlers/assert_execution_scheduling_declared_v0.py +51 -0
  54. compiler/governance_engine/assertions/handlers/assert_fqdn_namespace_authorized_v0.py +38 -0
  55. compiler/governance_engine/assertions/handlers/assert_fqdn_only_references_v0.py +89 -0
  56. compiler/governance_engine/assertions/handlers/assert_governance_declaration_resolves_v0.py +126 -0
  57. compiler/governance_engine/assertions/handlers/assert_handler_registry_closed_v0.py +75 -0
  58. compiler/governance_engine/assertions/handlers/assert_identity_authority_separation_v0.py +19 -0
  59. compiler/governance_engine/assertions/handlers/assert_identity_fqdn_consistency.py +75 -0
  60. compiler/governance_engine/assertions/handlers/assert_implementation_admissible_v0.py +66 -0
  61. compiler/governance_engine/assertions/handlers/assert_in_no_execution_logic_v0.py +82 -0
  62. compiler/governance_engine/assertions/handlers/assert_in_schema_required_v0.py +74 -0
  63. compiler/governance_engine/assertions/handlers/assert_in_workflow_binding_v0.py +101 -0
  64. compiler/governance_engine/assertions/handlers/assert_inspection_capability_read_only_v0.py +114 -0
  65. compiler/governance_engine/assertions/handlers/assert_no_ambient_authority_v0.py +17 -0
  66. compiler/governance_engine/assertions/handlers/assert_no_runtime_authorization_v0.py +18 -0
  67. compiler/governance_engine/assertions/handlers/assert_no_runtime_topology_synthesis_v0.py +18 -0
  68. compiler/governance_engine/assertions/handlers/assert_no_smart_execution_v0.py +102 -0
  69. compiler/governance_engine/assertions/handlers/assert_no_undeclared_behavior_surface_v0.py +52 -0
  70. compiler/governance_engine/assertions/handlers/assert_no_workflow_authorization_logic_v0.py +18 -0
  71. compiler/governance_engine/assertions/handlers/assert_protocol_surface_closed_v0.py +119 -0
  72. compiler/governance_engine/assertions/handlers/assert_rb_binding_policy_conformance_v0.py +124 -0
  73. compiler/governance_engine/assertions/handlers/assert_rb_cs_only_v0.py +68 -0
  74. compiler/governance_engine/assertions/handlers/assert_rb_no_logic_v0.py +81 -0
  75. compiler/governance_engine/assertions/handlers/assert_rb_parameters_declared_v0.py +117 -0
  76. compiler/governance_engine/assertions/handlers/assert_rb_storage_subdomain_owned_v0.py +118 -0
  77. compiler/governance_engine/assertions/handlers/assert_runtime_invariant_wired_v0.py +163 -0
  78. compiler/governance_engine/assertions/handlers/assert_schema_conformance_v0.py +64 -0
  79. compiler/governance_engine/assertions/handlers/assert_schema_description_well_formed_v0.py +97 -0
  80. compiler/governance_engine/assertions/handlers/assert_security_domain_declared_v0.py +51 -0
  81. compiler/governance_engine/assertions/handlers/assert_structure_paths_well_formed_v0.py +118 -0
  82. compiler/governance_engine/assertions/handlers/assert_superseded_not_referenced_v0.py +134 -0
  83. compiler/governance_engine/assertions/handlers/assert_test_data_match_ct_output_v0.py +134 -0
  84. compiler/governance_engine/assertions/handlers/assert_topology_acyclic_v0.py +53 -0
  85. compiler/governance_engine/assertions/handlers/assert_topology_authority_orthogonal_v0.py +110 -0
  86. compiler/governance_engine/assertions/handlers/assert_topology_capability_reference_unique_v0.py +63 -0
  87. compiler/governance_engine/assertions/handlers/assert_topology_contract_closed_v0.py +113 -0
  88. compiler/governance_engine/assertions/handlers/assert_topology_immutable_after_compilation_v0.py +17 -0
  89. compiler/governance_engine/assertions/handlers/assert_topology_input_reference_declared_v0.py +112 -0
  90. compiler/governance_engine/assertions/handlers/assert_topology_routing_complete_v0.py +82 -0
  91. compiler/governance_engine/assertions/handlers/assert_topology_step_declared_v0.py +77 -0
  92. compiler/governance_engine/assertions/handlers/assert_topology_step_id_unique_v0.py +60 -0
  93. compiler/governance_engine/assertions/handlers/assert_topology_surface_canonical_v0.py +180 -0
  94. compiler/governance_engine/assertions/handlers/assert_topology_transport_orthogonal_v0.py +113 -0
  95. compiler/governance_engine/assertions/handlers/assert_trace_authority_binding_required_v0.py +18 -0
  96. compiler/governance_engine/assertions/handlers/assert_transport_canonical_normalization_v0.py +83 -0
  97. compiler/governance_engine/assertions/handlers/assert_transport_no_dynamic_routing_v0.py +93 -0
  98. compiler/governance_engine/assertions/handlers/assert_transport_no_workflow_semantics_v0.py +106 -0
  99. compiler/governance_engine/assertions/handlers/assert_transport_operation_identity_independence_v0.py +56 -0
  100. compiler/governance_engine/assertions/handlers/assert_transport_response_projection_external_v0.py +38 -0
  101. compiler/governance_engine/assertions/handlers/assert_transport_result_class_protocol_independence_v0.py +48 -0
  102. compiler/governance_engine/assertions/handlers/assert_transport_target_exists_v0.py +132 -0
  103. compiler/governance_engine/assertions/handlers/assert_unique_artifact_id_v0.py +58 -0
  104. compiler/governance_engine/assertions/handlers/assert_vocabulary_symbols_well_formed_v0.py +163 -0
  105. compiler/governance_engine/assertions/handlers/assert_wf_announcement_distinct_v0.py +85 -0
  106. compiler/governance_engine/assertions/handlers/assert_wf_cc_only_nodes_v0.py +60 -0
  107. compiler/governance_engine/assertions/handlers/assert_wf_entry_intent_v0.py +68 -0
  108. compiler/governance_engine/assertions/handlers/assert_wf_execution_path_valid_v0.py +89 -0
  109. compiler/governance_engine/assertions/handlers/assert_wf_node_key_binding_unique_v0.py +107 -0
  110. compiler/governance_engine/platform_root.py +102 -0
  111. compiler/governance_engine/structure/__init__.py +56 -0
  112. compiler/governance_engine/structure/discovery/__init__.py +28 -0
  113. compiler/governance_engine/structure/discovery/artifact_locator.py +210 -0
  114. compiler/governance_engine/structure/exceptions.py +113 -0
  115. compiler/governance_engine/structure/loading/__init__.py +88 -0
  116. compiler/governance_engine/structure/loading/fs_reader.py +225 -0
  117. compiler/governance_engine/structure/loading/markdown_parser.py +148 -0
  118. compiler/governance_engine/structure/loading/protocol_loader.py +587 -0
  119. compiler/governance_engine/structure/loading/test_case_provider.py +125 -0
  120. compiler/governance_engine/structure/loading/trace_reader.py +98 -0
  121. compiler/governance_engine/structure/loading/vocabulary_loader.py +174 -0
  122. compiler/governance_engine/structure/loading/yaml_parser.py +209 -0
  123. compiler/governance_engine/structure/resolution/__init__.py +22 -0
  124. compiler/governance_engine/structure/resolution/domain_resolver.py +124 -0
  125. compiler/governance_engine/structure/resolution/layer_resolver.py +572 -0
  126. compiler/governance_engine/structure/resolution/path_registry.py +1054 -0
  127. compiler/graph/__init__.py +38 -0
  128. compiler/graph/addressing.py +153 -0
  129. compiler/graph/edge.py +100 -0
  130. compiler/graph/evidence.py +637 -0
  131. compiler/graph/graph.py +269 -0
  132. compiler/graph/hashing.py +89 -0
  133. compiler/graph/node.py +134 -0
  134. compiler/graph/query.py +215 -0
  135. compiler/graph/state.py +237 -0
  136. compiler/graph/trace.py +97 -0
  137. compiler/graph/types.py +71 -0
  138. compiler/projections/__init__.py +207 -0
  139. compiler/projections/artifact_index.py +225 -0
  140. compiler/projections/canonical.py +173 -0
  141. compiler/projections/dispatch.py +532 -0
  142. compiler/projections/evidence.py +170 -0
  143. compiler/projections/handlers.py +224 -0
  144. compiler/projections/ipm.py +388 -0
  145. compiler/projections/reverse.py +165 -0
  146. compiler/projections/tokenized.py +135 -0
  147. compiler/projections/vocabulary.py +108 -0
  148. compiler/stages/__init__.py +28 -0
  149. compiler/stages/s1_extract.py +867 -0
  150. compiler/stages/s2_canonicalize.py +516 -0
  151. compiler/stages/s3_semantic_addressing.py +66 -0
  152. compiler/stages/s4_govern.py +754 -0
  153. compiler/stages/s5_construct.py +537 -0
  154. compiler/stages/s6_project.py +94 -0
  155. compiler/stages/s7_materialize.py +1170 -0
  156. compiler/stages/s8_verify.py +643 -0
  157. compiler/stages/s9_attest.py +184 -0
  158. compiler/structure_loader.py +132 -0
  159. compiler/visualization/__init__.py +12 -0
  160. compiler/visualization/consumers/__init__.py +28 -0
  161. compiler/visualization/consumers/evidence_projection.py +162 -0
  162. compiler/visualization/consumers/evidence_query.py +296 -0
  163. compiler/visualization/consumers/evidence_reader.py +127 -0
  164. compiler/visualization/views/__init__.py +70 -0
  165. compiler/visualization/views/_png_writer.py +45 -0
  166. compiler/visualization/views/artifact_lineage.py +180 -0
  167. compiler/visualization/views/family_view.py +164 -0
  168. compiler/visualization/views/materialization_view.py +150 -0
  169. compiler/visualization/views/structural_dag.py +158 -0
  170. compiler/visualization/wf_graph_generator.py +534 -0
  171. pgc_compiler-2.0.0.dist-info/METADATA +203 -0
  172. pgc_compiler-2.0.0.dist-info/RECORD +177 -0
  173. pgc_compiler-2.0.0.dist-info/WHEEL +5 -0
  174. pgc_compiler-2.0.0.dist-info/entry_points.txt +2 -0
  175. pgc_compiler-2.0.0.dist-info/licenses/LICENSE +67 -0
  176. pgc_compiler-2.0.0.dist-info/licenses/NOTICE +11 -0
  177. pgc_compiler-2.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,516 @@
1
+ """
2
+ S2 CANONICALIZE — Type generic REFERENCES edges into specific edge kinds.
3
+
4
+ Input: State from S1 (nodes populated, all edges are REFERENCES)
5
+ Output: State with typed edges (WF_CONTAINS_NODE, CC_BINDS_CT, etc.)
6
+
7
+ This stage examines each REFERENCES edge, looks at the source and target
8
+ node kinds and frontmatter, and promotes it to the correct EdgeKind.
9
+
10
+ Also performs structural validation:
11
+ - All reference targets exist as nodes in the graph
12
+ - No dangling references
13
+ - Edge kinds are legal for their source/target node kinds
14
+ """
15
+
16
+ from compiler.graph.types import NodeKind, EdgeKind
17
+ from compiler.graph.node import Node
18
+ from compiler.graph.edge import Edge
19
+ from compiler.graph.graph import Graph, GraphBuilder
20
+ from compiler.graph.state import State
21
+ from compiler.graph.trace import TraceEvent
22
+ from compiler.graph.evidence import EventFamily
23
+ from compiler.atoms.errors import CompilerError
24
+ from compiler.atoms.error_codes import ErrorCode
25
+
26
+
27
+ def s2_canonicalize(state: State) -> State:
28
+ """
29
+ S2 CANONICALIZE: Promote REFERENCES edges to typed edge kinds.
30
+
31
+ Pure function: State → State.
32
+
33
+ Steps:
34
+ 1. Validate all edge targets exist in graph
35
+ 2. Classify each REFERENCES edge by source/target kind
36
+ 3. Build WF execution topology edges from frontmatter
37
+ 4. Rebuild graph with typed edges
38
+
39
+ Args:
40
+ state: State from S1 EXTRACT
41
+
42
+ Returns:
43
+ New State with typed edges
44
+ """
45
+ state = state.with_stage("S2_CANONICALIZE")
46
+ errors: list[CompilerError] = []
47
+ warnings: list[CompilerError] = []
48
+ trace: list[TraceEvent] = []
49
+ graph = state.graph
50
+
51
+ # Import surface: FQDNs provided by an already-compiled platform, resolved AGAINST (not
52
+ # discovered/emitted here). A reference to one of these is not dangling — it is external.
53
+ imported = _import_surface_fqdns(state.structure_config)
54
+
55
+ builder = GraphBuilder()
56
+
57
+ # Copy all nodes unchanged
58
+ for node in graph.nodes.values():
59
+ builder.add_node(node)
60
+
61
+ # --- Step 1: Validate edge targets exist (or resolve against the imported surface) ---
62
+ for edge in graph.edges:
63
+ if edge.target_fqdn not in graph.nodes and edge.target_fqdn not in imported:
64
+ errors.append(CompilerError(
65
+ code=ErrorCode.E104_INVALID_FQDN,
66
+ message=(
67
+ f"Dangling reference: {edge.source_fqdn} → {edge.target_fqdn} "
68
+ f"(target not found in graph or imported surface)"
69
+ ),
70
+ phase="S2_CANONICALIZE",
71
+ fqdn_id=edge.source_fqdn,
72
+ ))
73
+
74
+ if errors:
75
+ return state.with_errors(*errors)
76
+
77
+ # --- Step 2: Classify REFERENCES edges ---
78
+ for edge in graph.edges:
79
+ # Edges to the imported surface are resolved externally — drop them (the target is not
80
+ # a node in this domain's graph; it carries no downstream topology here).
81
+ if edge.target_fqdn not in graph.nodes:
82
+ continue
83
+
84
+ if edge.kind != EdgeKind.REFERENCES:
85
+ # Already typed (shouldn't happen after S1, but defensive)
86
+ builder.add_edge(edge)
87
+ continue
88
+
89
+ source_node = graph.nodes[edge.source_fqdn]
90
+ target_node = graph.nodes[edge.target_fqdn]
91
+ typed_kind = _classify_edge(source_node, target_node)
92
+
93
+ if typed_kind != EdgeKind.REFERENCES:
94
+ trace.append(TraceEvent.create(
95
+ stage="S2_CANONICALIZE",
96
+ operation="edge_typed",
97
+ subject_fqdn=edge.source_fqdn,
98
+ detail={"target": edge.target_fqdn, "kind": typed_kind.value},
99
+ family=EventFamily.TOPOLOGY.value,
100
+ ))
101
+
102
+ builder.add_edge(edge.replace(kind=typed_kind))
103
+
104
+ # --- Step 3: Build WF execution topology edges from frontmatter ---
105
+ _build_wf_topology_edges(graph, builder, errors, warnings)
106
+
107
+ # --- Step 4: Build CC pipeline binding edges from frontmatter ---
108
+ _build_cc_pipeline_edges(graph, builder, errors)
109
+
110
+ # --- Step 5: Build RB mapping edges from frontmatter ---
111
+ _build_rb_mapping_edges(graph, builder, errors)
112
+
113
+ # --- Step 6: Build TI invocation edges from frontmatter ---
114
+ _build_ti_invocation_edges(graph, builder, errors)
115
+
116
+ # --- Step 7: Drop bare duplicates shadowed by metadata-rich edges ---
117
+ # The same binding is legitimately declared twice (references header +
118
+ # frontmatter pipeline step); only the metadata-rich edge survives.
119
+ dropped = builder.drop_bare_duplicate_edges()
120
+ if dropped:
121
+ trace.append(TraceEvent.create(
122
+ stage="S2_CANONICALIZE",
123
+ operation="bare_duplicate_edges_dropped",
124
+ detail={"count": dropped},
125
+ family=EventFamily.TOPOLOGY.value,
126
+ ))
127
+
128
+ if errors:
129
+ state = state.with_errors(*errors)
130
+ if warnings:
131
+ state = state.with_warnings(*warnings)
132
+ if trace:
133
+ state = state.with_trace_events(*trace)
134
+
135
+ new_graph = builder.build()
136
+ state = state.with_graph(new_graph)
137
+
138
+ # Record canonicalization metadata
139
+ edge_kind_counts: dict[str, int] = {}
140
+ for edge in new_graph.edges:
141
+ k = edge.kind.value
142
+ edge_kind_counts[k] = edge_kind_counts.get(k, 0) + 1
143
+ state = state.with_metadata("edge_kind_counts", edge_kind_counts)
144
+
145
+ return state
146
+
147
+
148
+ def _import_surface_fqdns(structure_config) -> set[str]:
149
+ """FQDNs provided by an already-compiled platform surface (import_surface).
150
+
151
+ A domain compiles AGAINST the platform: references to these resolve externally and are neither
152
+ re-discovered nor re-emitted. Empty when no import_surface is declared (e.g. the platform build).
153
+ Reads the imported domain's compiled vocabulary — enforcing compile order (platform first).
154
+ """
155
+ import json
156
+ imp = (structure_config.get("artifact_discovery", {}) or {}).get("import_surface", {}) or {}
157
+ domain = imp.get("domain")
158
+ if not domain:
159
+ return set()
160
+ from compiler.governance_engine.platform_root import platform_root
161
+ vocab = platform_root() / "snapshot" / "compiled" / "vocabulary" / domain / "reverse.json"
162
+ if not vocab.exists():
163
+ raise FileNotFoundError(
164
+ f"import_surface: compiled surface for '{domain}' not found at {vocab}. "
165
+ f"Compile the '{domain}' structure before compiling this domain against it."
166
+ )
167
+ return set(json.loads(vocab.read_text(encoding="utf-8")).keys())
168
+
169
+
170
+ def _classify_edge(source: Node, target: Node) -> EdgeKind:
171
+ """
172
+ Determine the correct edge kind based on source and target node kinds.
173
+
174
+ Classification rules (source_kind → target_kind → edge_kind):
175
+ - WF → IN → WF_ADMITS_VIA_IN
176
+ - WF → RB → WF_BINDS_RB
177
+ - WF → CC → WF_CONTAINS_NODE (generic; START and NEXT come from frontmatter)
178
+ - CC → CT → CC_BINDS_CT
179
+ - CC → CS → CC_BINDS_CS
180
+ - RB → CT → RB_MAPS
181
+ - RB → CS → RB_MAPS
182
+ - * → GOVERNANCE → GOVERNED_BY
183
+ - * → ASSERT → ASSERTED_BY
184
+ - CT → CT → MOLECULE_COMPOSES_ATOM
185
+ - Otherwise → REFERENCES (unresolved)
186
+ """
187
+ sk = source.kind
188
+ tk = target.kind
189
+
190
+ # Governance edges
191
+ if tk == NodeKind.GOVERNANCE:
192
+ return EdgeKind.GOVERNED_BY
193
+ if tk == NodeKind.ASSERT:
194
+ return EdgeKind.ASSERTED_BY
195
+
196
+ # WF outgoing edges
197
+ if sk == NodeKind.WF:
198
+ if tk == NodeKind.IN:
199
+ return EdgeKind.WF_ADMITS_VIA_IN
200
+ if tk == NodeKind.RB:
201
+ return EdgeKind.WF_BINDS_RB
202
+ if tk == NodeKind.CC:
203
+ return EdgeKind.WF_CONTAINS_NODE
204
+
205
+ # CC binding edges
206
+ if sk == NodeKind.CC:
207
+ if tk == NodeKind.CT:
208
+ return EdgeKind.CC_BINDS_CT
209
+ if tk == NodeKind.CS:
210
+ return EdgeKind.CC_BINDS_CS
211
+
212
+ # RB mapping edges
213
+ if sk == NodeKind.RB:
214
+ if tk in (NodeKind.CT, NodeKind.CS):
215
+ return EdgeKind.RB_MAPS
216
+
217
+ # TI invocation entry point
218
+ if sk == NodeKind.TI and tk == NodeKind.WF:
219
+ return EdgeKind.TI_INVOKES_WF
220
+
221
+ # CT composition
222
+ if sk == NodeKind.CT and tk == NodeKind.CT:
223
+ return EdgeKind.MOLECULE_COMPOSES_ATOM
224
+
225
+ # Unresolvable — keep as generic reference
226
+ return EdgeKind.REFERENCES
227
+
228
+
229
+ def _build_wf_topology_edges(
230
+ graph: Graph,
231
+ builder: GraphBuilder,
232
+ errors: list[CompilerError],
233
+ warnings: list[CompilerError],
234
+ ) -> None:
235
+ """
236
+ Build WF execution topology edges from WF frontmatter.
237
+
238
+ WF topology uses local node keys (e.g. "entry", "lookup", "exit") which
239
+ reference artifacts via `code` or `fqdn_id` fields. This stage resolves
240
+ those local keys to graph-level FQDNs and builds:
241
+ - WF_START edge (from WF to the start node's resolved FQDN)
242
+ - WF_CONTAINS_NODE edges (for CC/IN nodes that reference graph artifacts)
243
+ - NODE_NEXT edges (from inline `next` maps or `transitions[]`)
244
+ """
245
+ # Build code → FQDN lookup once; depends only on graph, not on any specific WF
246
+ code_to_fqdn: dict[str, str] = {
247
+ graph_node.artifact_code: graph_fqdn
248
+ for graph_fqdn, graph_node in graph.nodes.items()
249
+ }
250
+
251
+ for fqdn, node in graph.nodes.items():
252
+ if node.kind != NodeKind.WF:
253
+ continue
254
+
255
+ core = node.frontmatter.get("core", {})
256
+ if not isinstance(core, dict):
257
+ continue
258
+
259
+ wf_nodes = core.get("nodes", {})
260
+ if not isinstance(wf_nodes, dict):
261
+ continue
262
+
263
+ # Build local-key → FQDN resolution map
264
+ key_to_fqdn: dict[str, str | None] = {}
265
+ for key, node_spec in wf_nodes.items():
266
+ if not isinstance(node_spec, dict):
267
+ key_to_fqdn[key] = None
268
+ continue
269
+ ref = node_spec.get("fqdn_id") or node_spec.get("code")
270
+ if ref and "::" in ref and ref in graph.nodes:
271
+ key_to_fqdn[key] = ref
272
+ elif ref and "::" not in ref:
273
+ # Short code — resolve via namespace prefix or artifact_code lookup
274
+ ns_fqdn = f"{node.namespace}::{ref}"
275
+ if ns_fqdn in graph.nodes:
276
+ key_to_fqdn[key] = ns_fqdn
277
+ elif ref in code_to_fqdn:
278
+ key_to_fqdn[key] = code_to_fqdn[ref]
279
+ else:
280
+ key_to_fqdn[key] = None
281
+ else:
282
+ key_to_fqdn[key] = None
283
+
284
+ # WF_START edge
285
+ start_node = core.get("start_node")
286
+ if isinstance(start_node, str) and start_node:
287
+ resolved_start = key_to_fqdn.get(start_node) if start_node in key_to_fqdn else (
288
+ start_node if start_node in graph.nodes else None
289
+ )
290
+ if resolved_start:
291
+ builder.add_edge(Edge.create(
292
+ source_fqdn=fqdn,
293
+ target_fqdn=resolved_start,
294
+ kind=EdgeKind.WF_START,
295
+ ))
296
+
297
+ # WF_CONTAINS_NODE edges from nodes map
298
+ for key, resolved_fqdn in key_to_fqdn.items():
299
+ if resolved_fqdn:
300
+ builder.add_edge(Edge.create(
301
+ source_fqdn=fqdn,
302
+ target_fqdn=resolved_fqdn,
303
+ kind=EdgeKind.WF_CONTAINS_NODE,
304
+ ))
305
+
306
+ # NODE_NEXT edges from inline `next` maps on each node
307
+ for key, node_spec in wf_nodes.items():
308
+ if not isinstance(node_spec, dict):
309
+ continue
310
+ next_map = node_spec.get("next", {})
311
+ if not isinstance(next_map, dict):
312
+ continue
313
+
314
+ source_resolved = key_to_fqdn.get(key)
315
+ for condition, target_key in next_map.items():
316
+ target_resolved = key_to_fqdn.get(target_key)
317
+ if source_resolved and target_resolved:
318
+ builder.add_edge(Edge.create(
319
+ source_fqdn=source_resolved,
320
+ target_fqdn=target_resolved,
321
+ kind=EdgeKind.NODE_NEXT,
322
+ metadata={"condition": condition, "wf_fqdn": fqdn},
323
+ ))
324
+
325
+ # NODE_NEXT edges from explicit transitions[] (FQDN-based)
326
+ transitions = core.get("transitions", [])
327
+ for transition in transitions:
328
+ if not isinstance(transition, dict):
329
+ continue
330
+
331
+ from_node = transition.get("from")
332
+ to_node = transition.get("to")
333
+ condition = transition.get("condition", "")
334
+
335
+ if not from_node or not to_node:
336
+ continue
337
+
338
+ # Resolve via key map or direct FQDN lookup
339
+ from_resolved = key_to_fqdn.get(from_node, from_node if from_node in graph.nodes else None)
340
+ to_resolved = key_to_fqdn.get(to_node, to_node if to_node in graph.nodes else None)
341
+
342
+ if not from_resolved:
343
+ errors.append(CompilerError(
344
+ code=ErrorCode.E104_INVALID_FQDN,
345
+ message=f"Transition from-node not found: {from_node}",
346
+ phase="S2_CANONICALIZE",
347
+ fqdn_id=fqdn,
348
+ ))
349
+ continue
350
+
351
+ if not to_resolved:
352
+ errors.append(CompilerError(
353
+ code=ErrorCode.E104_INVALID_FQDN,
354
+ message=f"Transition to-node not found: {to_node}",
355
+ phase="S2_CANONICALIZE",
356
+ fqdn_id=fqdn,
357
+ ))
358
+ continue
359
+
360
+ builder.add_edge(Edge.create(
361
+ source_fqdn=from_resolved,
362
+ target_fqdn=to_resolved,
363
+ kind=EdgeKind.NODE_NEXT,
364
+ metadata={"condition": condition, "wf_fqdn": fqdn},
365
+ ))
366
+
367
+
368
+ def _build_ti_invocation_edges(
369
+ graph: Graph,
370
+ builder: GraphBuilder,
371
+ errors: list[CompilerError],
372
+ ) -> None:
373
+ """
374
+ Build TI invocation edges from TI frontmatter.
375
+
376
+ TI artifacts declare their workflow entry point via `core.workflow`
377
+ (semantics: TI → invokes → WF; WF → binds → RB). These become
378
+ TI_INVOKES_WF edges carrying the declared route.
379
+ """
380
+ for fqdn, node in graph.nodes.items():
381
+ if node.kind != NodeKind.TI:
382
+ continue
383
+
384
+ core = node.frontmatter.get("core", {})
385
+ if not isinstance(core, dict):
386
+ continue
387
+
388
+ wf_ref = core.get("workflow")
389
+ if not isinstance(wf_ref, str) or not wf_ref:
390
+ continue # no declaration → no edge (zero inference)
391
+
392
+ if wf_ref not in graph.nodes:
393
+ errors.append(CompilerError(
394
+ code=ErrorCode.E104_INVALID_FQDN,
395
+ message=f"TI workflow target not found: {fqdn} → {wf_ref}",
396
+ phase="S2_CANONICALIZE",
397
+ fqdn_id=fqdn,
398
+ ))
399
+ continue
400
+
401
+ route = core.get("route") if isinstance(core.get("route"), dict) else {}
402
+ builder.add_edge(Edge.create(
403
+ source_fqdn=fqdn,
404
+ target_fqdn=wf_ref,
405
+ kind=EdgeKind.TI_INVOKES_WF,
406
+ metadata={
407
+ "route_path": route.get("path", ""),
408
+ "route_method": route.get("method", ""),
409
+ },
410
+ ))
411
+
412
+
413
+ def _build_cc_pipeline_edges(
414
+ graph: Graph,
415
+ builder: GraphBuilder,
416
+ errors: list[CompilerError],
417
+ ) -> None:
418
+ """
419
+ Build CC pipeline binding edges from CC frontmatter.
420
+
421
+ CC frontmatter declares pipeline steps that reference CT/CS artifacts.
422
+ These become CC_BINDS_CT / CC_BINDS_CS edges with step metadata.
423
+ """
424
+ for fqdn, node in graph.nodes.items():
425
+ if node.kind != NodeKind.CC:
426
+ continue
427
+
428
+ core = node.frontmatter.get("core", {})
429
+ if not isinstance(core, dict):
430
+ continue
431
+
432
+ pipeline = core.get("pipeline", [])
433
+ for idx, step in enumerate(pipeline):
434
+ if not isinstance(step, dict):
435
+ continue
436
+
437
+ # Each step may reference a transform or side_effect
438
+ transform_ref = step.get("transform")
439
+ if isinstance(transform_ref, str) and transform_ref:
440
+ if transform_ref in graph.nodes:
441
+ target = graph.nodes[transform_ref]
442
+ edge_kind = (
443
+ EdgeKind.CC_BINDS_CT if target.kind == NodeKind.CT
444
+ else EdgeKind.CC_BINDS_CS if target.kind == NodeKind.CS
445
+ else EdgeKind.REFERENCES
446
+ )
447
+ builder.add_edge(Edge.create(
448
+ source_fqdn=fqdn,
449
+ target_fqdn=transform_ref,
450
+ kind=edge_kind,
451
+ metadata={
452
+ "pipeline_index": idx,
453
+ "step_id": step.get("step", ""),
454
+ "inputs": step.get("inputs", {}),
455
+ "outputs": step.get("outputs", {}),
456
+ "on_result": step.get("on_result", {}),
457
+ },
458
+ ))
459
+
460
+ side_effect_ref = step.get("side_effect")
461
+ if isinstance(side_effect_ref, str) and side_effect_ref:
462
+ if side_effect_ref in graph.nodes:
463
+ builder.add_edge(Edge.create(
464
+ source_fqdn=fqdn,
465
+ target_fqdn=side_effect_ref,
466
+ kind=EdgeKind.CC_BINDS_CS,
467
+ metadata={
468
+ "pipeline_index": idx,
469
+ "step_id": step.get("step", ""),
470
+ "op": step.get("op", ""),
471
+ "inputs": step.get("inputs", {}),
472
+ "outputs": step.get("outputs", {}),
473
+ "on_result": step.get("on_result", {}),
474
+ "store": step.get("store", ""),
475
+ },
476
+ ))
477
+
478
+
479
+ def _build_rb_mapping_edges(
480
+ graph: Graph,
481
+ builder: GraphBuilder,
482
+ errors: list[CompilerError],
483
+ ) -> None:
484
+ """
485
+ Build RB mapping edges from RB frontmatter.
486
+
487
+ RB core.bindings maps artifact FQDNs to implementation handlers.
488
+ Each binding key → RB_MAPS edge.
489
+ """
490
+ for fqdn, node in graph.nodes.items():
491
+ if node.kind != NodeKind.RB:
492
+ continue
493
+
494
+ core = node.frontmatter.get("core", {})
495
+ if not isinstance(core, dict):
496
+ continue
497
+
498
+ bindings = core.get("bindings", {})
499
+ if not isinstance(bindings, dict):
500
+ continue
501
+
502
+ for target_fqdn, binding_config in bindings.items():
503
+ if target_fqdn not in graph.nodes:
504
+ # Already caught in S1 reference validation
505
+ continue
506
+
507
+ handler_ref = ""
508
+ if isinstance(binding_config, dict):
509
+ handler_ref = binding_config.get("handler", "")
510
+
511
+ builder.add_edge(Edge.create(
512
+ source_fqdn=fqdn,
513
+ target_fqdn=target_fqdn,
514
+ kind=EdgeKind.RB_MAPS,
515
+ metadata={"handler_ref": handler_ref},
516
+ ))
@@ -0,0 +1,66 @@
1
+ """
2
+ S3 SEMANTIC_ADDRESSING — Allocate deterministic machine addresses.
3
+
4
+ Input: State from S2 (typed edges, canonical FQDNs)
5
+ Output: State with all addresses populated (nodes, edges, address table)
6
+
7
+ This is NOT indexing. It is governed symbolic identity →
8
+ deterministic machine address allocation. The compiler owns
9
+ semantic addressing forever — addresses are compile-time allocated,
10
+ never runtime-generated.
11
+ """
12
+
13
+ from compiler.graph.addressing import allocate_addresses
14
+ from compiler.graph.state import State
15
+ from compiler.graph.trace import TraceEvent
16
+ from compiler.graph.evidence import EventFamily
17
+ from compiler.atoms.errors import CompilerError
18
+ from compiler.atoms.error_codes import ErrorCode
19
+
20
+
21
+ def s3_semantic_addressing(state: State) -> State:
22
+ """
23
+ S3 SEMANTIC_ADDRESSING: Allocate addresses to all governed identities.
24
+
25
+ Pure function: State → State.
26
+
27
+ Address space partitions:
28
+ Nodes: 0x0000 — 0x3FFF (16384 slots)
29
+ Edge kinds: 0x4000 — 0x40FF (256 slots)
30
+ Node kinds: 0x4100 — 0x41FF (256 slots)
31
+ Outcomes: 0x5000 — 0x5FFF (4096 slots)
32
+ Transitions: 0x6000 — 0x6FFF (4096 slots)
33
+ Vocabulary: 0x7000 — 0x7FFF (4096 slots)
34
+
35
+ Args:
36
+ state: State from S2 CANONICALIZE
37
+
38
+ Returns:
39
+ New State with addresses populated
40
+ """
41
+ state = state.with_stage("S3_SEMANTIC_ADDRESSING")
42
+
43
+ try:
44
+ addressed_graph, address_hash = allocate_addresses(state.graph)
45
+ except Exception as e:
46
+ return state.with_errors(CompilerError(
47
+ code=ErrorCode.E901_INTERNAL_ERROR,
48
+ message=f"Address allocation failed: {e}",
49
+ phase="S3_SEMANTIC_ADDRESSING",
50
+ ))
51
+
52
+ state = state.with_graph(addressed_graph)
53
+ state = state.with_metadata("address_hash", address_hash)
54
+ state = state.with_metadata("address_table_size", len(addressed_graph.address_table))
55
+
56
+ state = state.with_trace_events(TraceEvent.create(
57
+ stage="S3_SEMANTIC_ADDRESSING",
58
+ operation="addresses_allocated",
59
+ detail={
60
+ "address_table_size": len(addressed_graph.address_table),
61
+ "address_hash": address_hash,
62
+ },
63
+ family=EventFamily.ADDRESSING.value,
64
+ ))
65
+
66
+ return state