# Semantic Graph PoC 0.1

## Goal

Test whether a program can be automatically lowered from source syntax into a new symbolic/graph representation,
then executed from that representation without reading the original source again.

## Experiment A — control flow

Original behavior contains arithmetic, an intermediate local value, a conditional branch, and two returns.

Pipeline:

```text
Python source
  -> Python AST
  -> anonymous slots + semantic operation nodes + branch blocks
  -> JSON semantic graph
  -> graph-only runtime
```

Test vectors: 6
All matched: **True**

**作者：** Neo.K  
**機構：** EveMissLab／一言諾科技有限公司  
**日期：** 2026-10-08（整理日期；原稿未標註日期）  
**版本：** 0.1  

## Experiment B — real RepoLumen function

Target: `repo_semantic/historical_timeline.py::_transition_id`

Original semantic behavior:

```text
two ordered inputs
  -> concatenate with NUL separator
  -> UTF-8 encode
  -> SHA-256
  -> hexadecimal digest
  -> first 24 characters
  -> prefix with `hist_`
```

Automatically produced semantic ops:

```text
arg -> to_str -> const -> arg -> to_str -> concat_many -> utf8_encode -> const -> slot -> sha256 -> hex_digest -> const -> slice -> add
```

Test vectors: 5
All matched: **True**

## Source-language decoupling check

The canonical graph deliberately removes local/source identifiers.

```json
{
  "branch_graph_contains_original_var_x": false,
  "branch_graph_contains_original_var_y": false,
  "branch_graph_contains_original_var_base": false,
  "transition_graph_contains_from_snapshot_id": false,
  "transition_graph_contains_to_snapshot_id": false,
  "transition_graph_contains_hashlib_name": false,
  "transition_graph_contains_hexdigest_name": false,
  "transition_graph_contains_encode_name": false
}
```

All values above should be `false`.

The graph retains only:
- ordered argument positions;
- anonymous local slots;
- constants that affect behavior;
- semantic operations;
- control-flow structure.

## Graph-only process

A separate script, `runtime_only.py`, loads only the JSON graphs and the generic semantic runtime.
It does not contain the original functions.

Observed output:

```text
branch graph: 10
RepoLumen transition graph: hist_4aa9461975c271a428d60476
```

## Result

Within the supported subset, the experiment establishes:

1. source syntax is not required after lowering;
2. original variable names are not required;
3. a program can be represented as typed semantic nodes plus control-flow structure;
4. the same graph can be executed by a generic graph runtime;
5. a real RepoLumen function can be canonicalized from source-specific spellings such as
   `.encode("utf-8")`, `hashlib.sha256(...)`, and `.hexdigest()` into
   `utf8_encode`, `sha256`, and `hex_digest` semantic operations.

This is **not yet** a proof that arbitrary Python or arbitrary binaries can be losslessly lifted.
The difficult next layers are mutation/aliasing, exceptions, generators/coroutines, dynamic dispatch,
reflection, external effects, concurrency, C/ABI memory, and runtime-generated code.

## Files

- `semantic_ir.py` — compiler + graph runtime
- `experiment.py` — compiler/equivalence experiment
- `runtime_only.py` — graph-only execution
- `graph_branch.json` — control-flow graph representation
- `graph_repolumen_transition.json` — RepoLumen function semantic graph
- `run_results.json` — equivalence results


## Stronger isolation / property test

A physically separate runtime directory contains only:

```text
- graph_branch.json
- graph_repolumen_transition.json
- graph_runtime.py
- isolated_runtime_only.py
```

It contains no compiler and no original function source.

Isolated execution result:

```text
10
hist_4aa9461975c271a428d60476
```

Random equivalence testing:

- arithmetic/control-flow graph: **10,000 / 10,000 matched**
- RepoLumen `_transition_id` graph: **1,000 / 1,000 matched**
- compiler/source present in isolated runtime directory: **False**

Therefore the semantic representation remains operationally executable after the source-language compiler and original source are absent.
