recfold 1.0.0__tar.gz
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.
- recfold-1.0.0/.gitignore +220 -0
- recfold-1.0.0/LICENSE +21 -0
- recfold-1.0.0/PKG-INFO +173 -0
- recfold-1.0.0/README.md +161 -0
- recfold-1.0.0/docs/member6.md +94 -0
- recfold-1.0.0/docs/recfold_demo.ipynb +210 -0
- recfold-1.0.0/docs/renderer.md +203 -0
- recfold-1.0.0/pyproject.toml +28 -0
- recfold-1.0.0/src/recfold/__init__.py +0 -0
- recfold-1.0.0/src/recfold/analyzer.py +160 -0
- recfold-1.0.0/src/recfold/layout.py +254 -0
- recfold-1.0.0/src/recfold/models.py +51 -0
- recfold-1.0.0/src/recfold/renderer.py +505 -0
- recfold-1.0.0/src/recfold/snapshot.py +88 -0
- recfold-1.0.0/src/recfold/tracer.py +208 -0
- recfold-1.0.0/tests/test_analyzer.py +284 -0
- recfold-1.0.0/tests/test_layout.py +287 -0
- recfold-1.0.0/tests/test_models.py +114 -0
- recfold-1.0.0/tests/test_renderer.py +489 -0
- recfold-1.0.0/tests/test_snapshot.py +166 -0
- recfold-1.0.0/tests/test_tracer.py +160 -0
recfold-1.0.0/.gitignore
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
*.lcov
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
# Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# uv
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
# uv.lock
|
|
103
|
+
|
|
104
|
+
# poetry
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
106
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
107
|
+
# commonly ignored for libraries.
|
|
108
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
109
|
+
# poetry.lock
|
|
110
|
+
# poetry.toml
|
|
111
|
+
|
|
112
|
+
# pdm
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
114
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
115
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
116
|
+
# pdm.lock
|
|
117
|
+
# pdm.toml
|
|
118
|
+
.pdm-python
|
|
119
|
+
.pdm-build/
|
|
120
|
+
|
|
121
|
+
# pixi
|
|
122
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
123
|
+
# pixi.lock
|
|
124
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
125
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
126
|
+
.pixi/*
|
|
127
|
+
!.pixi/config.toml
|
|
128
|
+
|
|
129
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
130
|
+
__pypackages__/
|
|
131
|
+
|
|
132
|
+
# Celery stuff
|
|
133
|
+
celerybeat-schedule*
|
|
134
|
+
celerybeat.pid
|
|
135
|
+
|
|
136
|
+
# Redis
|
|
137
|
+
*.rdb
|
|
138
|
+
*.aof
|
|
139
|
+
*.pid
|
|
140
|
+
|
|
141
|
+
# RabbitMQ
|
|
142
|
+
mnesia/
|
|
143
|
+
rabbitmq/
|
|
144
|
+
rabbitmq-data/
|
|
145
|
+
|
|
146
|
+
# ActiveMQ
|
|
147
|
+
activemq-data/
|
|
148
|
+
|
|
149
|
+
# SageMath parsed files
|
|
150
|
+
*.sage.py
|
|
151
|
+
|
|
152
|
+
# Environments
|
|
153
|
+
.env
|
|
154
|
+
.envrc
|
|
155
|
+
.venv
|
|
156
|
+
env/
|
|
157
|
+
venv/
|
|
158
|
+
ENV/
|
|
159
|
+
env.bak/
|
|
160
|
+
venv.bak/
|
|
161
|
+
|
|
162
|
+
# Spyder project settings
|
|
163
|
+
.spyderproject
|
|
164
|
+
.spyproject
|
|
165
|
+
|
|
166
|
+
# Rope project settings
|
|
167
|
+
.ropeproject
|
|
168
|
+
|
|
169
|
+
# mkdocs/Zensical documentation
|
|
170
|
+
/site
|
|
171
|
+
|
|
172
|
+
# mypy
|
|
173
|
+
.mypy_cache/
|
|
174
|
+
.dmypy.json
|
|
175
|
+
dmypy.json
|
|
176
|
+
|
|
177
|
+
# Pyre type checker
|
|
178
|
+
.pyre/
|
|
179
|
+
|
|
180
|
+
# pytype static type analyzer
|
|
181
|
+
.pytype/
|
|
182
|
+
|
|
183
|
+
# Cython debug symbols
|
|
184
|
+
cython_debug/
|
|
185
|
+
|
|
186
|
+
# PyCharm
|
|
187
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
188
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
190
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
191
|
+
# .idea/
|
|
192
|
+
|
|
193
|
+
# Abstra
|
|
194
|
+
# Abstra is an AI-powered process automation framework.
|
|
195
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
196
|
+
# Learn more at https://abstra.io/docs
|
|
197
|
+
.abstra/
|
|
198
|
+
|
|
199
|
+
# Visual Studio Code
|
|
200
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore that
|
|
201
|
+
# can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
202
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer, you
|
|
203
|
+
# could uncomment the following to ignore the entire vscode folder
|
|
204
|
+
# .vscode/
|
|
205
|
+
# Temporary file for partial code execution
|
|
206
|
+
tempCodeRunnerFile.py
|
|
207
|
+
|
|
208
|
+
# Ruff stuff:
|
|
209
|
+
.ruff_cache/
|
|
210
|
+
|
|
211
|
+
# PyPI configuration file
|
|
212
|
+
.pypirc
|
|
213
|
+
|
|
214
|
+
# Marimo
|
|
215
|
+
marimo/_static/
|
|
216
|
+
marimo/_lsp/
|
|
217
|
+
__marimo__/
|
|
218
|
+
|
|
219
|
+
# Streamlit
|
|
220
|
+
.streamlit/secrets.toml
|
recfold-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zenith-20
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
recfold-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: recfold
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Zero-runtime-dependency recursion call-tree analyzer and state inspector for Python.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Zenith-20/recfold
|
|
6
|
+
Project-URL: Repository, https://github.com/Zenith-20/recfold
|
|
7
|
+
Author: Zenith-20, ssartayev, Alishnis, bekonty, exehamza, thecocogoat
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# recfold
|
|
14
|
+
|
|
15
|
+
`recfold` is a Python package for inspecting recursive function calls in Jupyter notebooks or standalone HTML files. It records traced calls in a call tree, folds structurally matching subtrees into a directed acyclic graph (DAG), and lets you inspect arguments, return values, errors, and boundary snapshots for individual calls.
|
|
16
|
+
|
|
17
|
+
## Scope and design choices
|
|
18
|
+
|
|
19
|
+
### No third-party runtime dependencies
|
|
20
|
+
|
|
21
|
+
`recfold` uses only the Python standard library at runtime. Its visualization is generated as self-contained HTML, SVG, CSS, and JavaScript. Runtime use does not require Graphviz, native C binaries, external CDNs, browser extensions, or `ipywidgets`. Notebook display still depends on the frontend permitting sandboxed `srcdoc` iframes and inline scripts.
|
|
22
|
+
|
|
23
|
+
### Tree-to-DAG folding and redundancy metrics
|
|
24
|
+
|
|
25
|
+
Tracing records calls that pass the configured depth and repeated-active-call guards. `recfold` then folds subtrees with matching structural keys into shared DAG nodes and calculates a structural redundancy ratio:
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
redundancy ratio = (recorded calls - unique folded subtrees) / recorded calls
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
For `fib(5)`, `recfold` records 15 calls and folds them into 6 unique subtrees. The ratio is `(15 - 6) / 15 = 0.6`, or 60%.
|
|
32
|
+
|
|
33
|
+
This is a post-hoc structural summary of the recorded trace, not an optimization. Folding does not memoize the traced function, speed up execution, or skip function calls. Recursive calls still execute, and the raw call tree remains in memory.
|
|
34
|
+
|
|
35
|
+
### Entry and exit snapshots
|
|
36
|
+
|
|
37
|
+
At call entry, `recfold` deep-copies positional and keyword arguments; at normal return, it deep-copies the result. If the function raises, the exit snapshot records the exception text. This can preserve a mutable argument, such as a backtracking path, as it appeared at each call boundary rather than exposing later mutations in earlier snapshots.
|
|
38
|
+
|
|
39
|
+
These are boundary snapshots, not a step-by-step debugger. The tracer does not automatically capture local variables or intermediate mutations. Each argument and result is copied separately; references shared across separately copied values are not preserved. Values that cannot be deep-copied are replaced with placeholder text.
|
|
40
|
+
|
|
41
|
+
### Structural views with occurrence records
|
|
42
|
+
|
|
43
|
+
The renderer provides interactive **Call tree** and **Folded DAG** views. Select a node and use the **Recorded occurrence** dropdown to inspect the corresponding call ID, depth, parent ID, arguments, result or error, and entry/exit snapshots. The occurrences retain the original call records; call IDs are assigned in call-entry order.
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
Python 3.9 or newer is required. Install from a local clone:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
git clone https://github.com/Zenith-20/recfold.git
|
|
51
|
+
cd recfold
|
|
52
|
+
python -m pip install -e .
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
If you are using JupyterHub, ensure the repository is on the server's filesystem and install it into your active kernel:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
%pip install -e /path/on/server/to/recfold
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Quick start
|
|
62
|
+
|
|
63
|
+
Run this in a Jupyter notebook cell:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from recfold.tracer import trace
|
|
67
|
+
from recfold.renderer import render_session
|
|
68
|
+
|
|
69
|
+
@trace
|
|
70
|
+
def fib(n):
|
|
71
|
+
if n < 2:
|
|
72
|
+
return n
|
|
73
|
+
return fib(n - 1) + fib(n - 2)
|
|
74
|
+
|
|
75
|
+
answer = fib(5)
|
|
76
|
+
print(answer) # 5
|
|
77
|
+
|
|
78
|
+
widget = render_session(fib.last_session, title="Fibonacci: repeated work")
|
|
79
|
+
widget # Keep as the last expression so Jupyter displays the widget
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
For a plain recursive function, `run_traced` returns the result and trace
|
|
83
|
+
session:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from recfold.tracer import run_traced
|
|
87
|
+
from recfold.renderer import render_session
|
|
88
|
+
|
|
89
|
+
def factorial(n):
|
|
90
|
+
if n < 2:
|
|
91
|
+
return 1
|
|
92
|
+
return n * factorial(n - 1)
|
|
93
|
+
|
|
94
|
+
answer, session = run_traced(factorial, 5)
|
|
95
|
+
widget = render_session(session)
|
|
96
|
+
widget
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Offline HTML export
|
|
100
|
+
|
|
101
|
+
To save the visualization as a standalone HTML document:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
widget.save("fib.html")
|
|
105
|
+
|
|
106
|
+
# Use overwrite=True to replace an existing file
|
|
107
|
+
widget.save("fib.html", overwrite=True)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
The exported document contains the graph views, styles, scripts, and rendered trace data. It makes no network requests and can be opened offline in a JavaScript-enabled browser. Review the included trace data before sharing.
|
|
111
|
+
|
|
112
|
+
## Current limitations
|
|
113
|
+
|
|
114
|
+
* **Synchronous execution only:** The decorator does not trace the execution of async function bodies or generator bodies. They return coroutine or generator objects when called; tracing does not await or iterate them.
|
|
115
|
+
* **Representation-based cycle guard:** The active-call guard compares function names and argument `repr()` strings, using the default 80-character limit. Distinct values with the same representation can be mistaken for a repeated active call. For terminating recursion where this is a concern, `@trace(detect_cycles=False, max_depth=...)` disables that guard while retaining a depth bound.
|
|
116
|
+
* **No automatic local-variable capture:** The tracer snapshots arguments at entry and return values or exceptions at exit. `snapshot_locals()` is available in `recfold.snapshot`, but the tracer does not call it.
|
|
117
|
+
* **Structural matching is not semantic proof:** Folding keys use function names, argument and result representations, exception text, and ordered child subtree keys. Matching keys do not establish mathematical equivalence or purity.
|
|
118
|
+
* **No virtualization:** Both graph views are rendered with all included nodes and edges. Large traces can require substantial memory and browser rendering work.
|
|
119
|
+
|
|
120
|
+
## Project structure
|
|
121
|
+
|
|
122
|
+
```text
|
|
123
|
+
models.py Dataclasses for calls, DAG nodes/edges, sessions, and statistics
|
|
124
|
+
snapshot.py Deep-copying and string truncation utilities
|
|
125
|
+
tracer.py @trace decorator, call stack tracking, and depth/cycle guards
|
|
126
|
+
analyzer.py Subtree folding logic and redundancy statistics
|
|
127
|
+
layout.py Pure-Python 2D node positioning and straight edge geometry
|
|
128
|
+
renderer.py Self-contained HTML/SVG generator and Jupyter widget wrapper
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
There is no console-script entry point or package-level re-export; import
|
|
132
|
+
functions from their modules as shown above.
|
|
133
|
+
|
|
134
|
+
## Development and testing
|
|
135
|
+
|
|
136
|
+
To run the unit test suite:
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
python -m pip install pytest
|
|
140
|
+
python -m pytest
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Optional browser tests
|
|
144
|
+
|
|
145
|
+
End-to-end UI tests (verifying click selection, panning, zooming, and keyboard navigation in Chromium) require Playwright:
|
|
146
|
+
|
|
147
|
+
```sh
|
|
148
|
+
python -m pip install playwright
|
|
149
|
+
python -m playwright install chromium
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Run them by setting the opt-in environment variable:
|
|
153
|
+
|
|
154
|
+
* **macOS / Linux:**
|
|
155
|
+
```sh
|
|
156
|
+
RECFOLD_BROWSER_TESTS=1 python -m pytest tests/test_renderer.py
|
|
157
|
+
```
|
|
158
|
+
* **Windows PowerShell:**
|
|
159
|
+
```powershell
|
|
160
|
+
$env:RECFOLD_BROWSER_TESTS = "1"
|
|
161
|
+
python -m pytest tests/test_renderer.py
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Without this flag, browser tests are skipped automatically.
|
|
165
|
+
|
|
166
|
+
## Further documentation
|
|
167
|
+
|
|
168
|
+
* [Renderer API, integration, and troubleshooting](docs/renderer.md)
|
|
169
|
+
* [Demo notebook: Fibonacci, backtracking, and error guards](docs/recfold_demo.ipynb)
|
|
170
|
+
|
|
171
|
+
## License
|
|
172
|
+
|
|
173
|
+
[MIT](LICENSE)
|
recfold-1.0.0/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# recfold
|
|
2
|
+
|
|
3
|
+
`recfold` is a Python package for inspecting recursive function calls in Jupyter notebooks or standalone HTML files. It records traced calls in a call tree, folds structurally matching subtrees into a directed acyclic graph (DAG), and lets you inspect arguments, return values, errors, and boundary snapshots for individual calls.
|
|
4
|
+
|
|
5
|
+
## Scope and design choices
|
|
6
|
+
|
|
7
|
+
### No third-party runtime dependencies
|
|
8
|
+
|
|
9
|
+
`recfold` uses only the Python standard library at runtime. Its visualization is generated as self-contained HTML, SVG, CSS, and JavaScript. Runtime use does not require Graphviz, native C binaries, external CDNs, browser extensions, or `ipywidgets`. Notebook display still depends on the frontend permitting sandboxed `srcdoc` iframes and inline scripts.
|
|
10
|
+
|
|
11
|
+
### Tree-to-DAG folding and redundancy metrics
|
|
12
|
+
|
|
13
|
+
Tracing records calls that pass the configured depth and repeated-active-call guards. `recfold` then folds subtrees with matching structural keys into shared DAG nodes and calculates a structural redundancy ratio:
|
|
14
|
+
|
|
15
|
+
```text
|
|
16
|
+
redundancy ratio = (recorded calls - unique folded subtrees) / recorded calls
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
For `fib(5)`, `recfold` records 15 calls and folds them into 6 unique subtrees. The ratio is `(15 - 6) / 15 = 0.6`, or 60%.
|
|
20
|
+
|
|
21
|
+
This is a post-hoc structural summary of the recorded trace, not an optimization. Folding does not memoize the traced function, speed up execution, or skip function calls. Recursive calls still execute, and the raw call tree remains in memory.
|
|
22
|
+
|
|
23
|
+
### Entry and exit snapshots
|
|
24
|
+
|
|
25
|
+
At call entry, `recfold` deep-copies positional and keyword arguments; at normal return, it deep-copies the result. If the function raises, the exit snapshot records the exception text. This can preserve a mutable argument, such as a backtracking path, as it appeared at each call boundary rather than exposing later mutations in earlier snapshots.
|
|
26
|
+
|
|
27
|
+
These are boundary snapshots, not a step-by-step debugger. The tracer does not automatically capture local variables or intermediate mutations. Each argument and result is copied separately; references shared across separately copied values are not preserved. Values that cannot be deep-copied are replaced with placeholder text.
|
|
28
|
+
|
|
29
|
+
### Structural views with occurrence records
|
|
30
|
+
|
|
31
|
+
The renderer provides interactive **Call tree** and **Folded DAG** views. Select a node and use the **Recorded occurrence** dropdown to inspect the corresponding call ID, depth, parent ID, arguments, result or error, and entry/exit snapshots. The occurrences retain the original call records; call IDs are assigned in call-entry order.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Python 3.9 or newer is required. Install from a local clone:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
git clone https://github.com/Zenith-20/recfold.git
|
|
39
|
+
cd recfold
|
|
40
|
+
python -m pip install -e .
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
If you are using JupyterHub, ensure the repository is on the server's filesystem and install it into your active kernel:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
%pip install -e /path/on/server/to/recfold
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Quick start
|
|
50
|
+
|
|
51
|
+
Run this in a Jupyter notebook cell:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from recfold.tracer import trace
|
|
55
|
+
from recfold.renderer import render_session
|
|
56
|
+
|
|
57
|
+
@trace
|
|
58
|
+
def fib(n):
|
|
59
|
+
if n < 2:
|
|
60
|
+
return n
|
|
61
|
+
return fib(n - 1) + fib(n - 2)
|
|
62
|
+
|
|
63
|
+
answer = fib(5)
|
|
64
|
+
print(answer) # 5
|
|
65
|
+
|
|
66
|
+
widget = render_session(fib.last_session, title="Fibonacci: repeated work")
|
|
67
|
+
widget # Keep as the last expression so Jupyter displays the widget
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
For a plain recursive function, `run_traced` returns the result and trace
|
|
71
|
+
session:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from recfold.tracer import run_traced
|
|
75
|
+
from recfold.renderer import render_session
|
|
76
|
+
|
|
77
|
+
def factorial(n):
|
|
78
|
+
if n < 2:
|
|
79
|
+
return 1
|
|
80
|
+
return n * factorial(n - 1)
|
|
81
|
+
|
|
82
|
+
answer, session = run_traced(factorial, 5)
|
|
83
|
+
widget = render_session(session)
|
|
84
|
+
widget
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Offline HTML export
|
|
88
|
+
|
|
89
|
+
To save the visualization as a standalone HTML document:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
widget.save("fib.html")
|
|
93
|
+
|
|
94
|
+
# Use overwrite=True to replace an existing file
|
|
95
|
+
widget.save("fib.html", overwrite=True)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The exported document contains the graph views, styles, scripts, and rendered trace data. It makes no network requests and can be opened offline in a JavaScript-enabled browser. Review the included trace data before sharing.
|
|
99
|
+
|
|
100
|
+
## Current limitations
|
|
101
|
+
|
|
102
|
+
* **Synchronous execution only:** The decorator does not trace the execution of async function bodies or generator bodies. They return coroutine or generator objects when called; tracing does not await or iterate them.
|
|
103
|
+
* **Representation-based cycle guard:** The active-call guard compares function names and argument `repr()` strings, using the default 80-character limit. Distinct values with the same representation can be mistaken for a repeated active call. For terminating recursion where this is a concern, `@trace(detect_cycles=False, max_depth=...)` disables that guard while retaining a depth bound.
|
|
104
|
+
* **No automatic local-variable capture:** The tracer snapshots arguments at entry and return values or exceptions at exit. `snapshot_locals()` is available in `recfold.snapshot`, but the tracer does not call it.
|
|
105
|
+
* **Structural matching is not semantic proof:** Folding keys use function names, argument and result representations, exception text, and ordered child subtree keys. Matching keys do not establish mathematical equivalence or purity.
|
|
106
|
+
* **No virtualization:** Both graph views are rendered with all included nodes and edges. Large traces can require substantial memory and browser rendering work.
|
|
107
|
+
|
|
108
|
+
## Project structure
|
|
109
|
+
|
|
110
|
+
```text
|
|
111
|
+
models.py Dataclasses for calls, DAG nodes/edges, sessions, and statistics
|
|
112
|
+
snapshot.py Deep-copying and string truncation utilities
|
|
113
|
+
tracer.py @trace decorator, call stack tracking, and depth/cycle guards
|
|
114
|
+
analyzer.py Subtree folding logic and redundancy statistics
|
|
115
|
+
layout.py Pure-Python 2D node positioning and straight edge geometry
|
|
116
|
+
renderer.py Self-contained HTML/SVG generator and Jupyter widget wrapper
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
There is no console-script entry point or package-level re-export; import
|
|
120
|
+
functions from their modules as shown above.
|
|
121
|
+
|
|
122
|
+
## Development and testing
|
|
123
|
+
|
|
124
|
+
To run the unit test suite:
|
|
125
|
+
|
|
126
|
+
```sh
|
|
127
|
+
python -m pip install pytest
|
|
128
|
+
python -m pytest
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Optional browser tests
|
|
132
|
+
|
|
133
|
+
End-to-end UI tests (verifying click selection, panning, zooming, and keyboard navigation in Chromium) require Playwright:
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
python -m pip install playwright
|
|
137
|
+
python -m playwright install chromium
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Run them by setting the opt-in environment variable:
|
|
141
|
+
|
|
142
|
+
* **macOS / Linux:**
|
|
143
|
+
```sh
|
|
144
|
+
RECFOLD_BROWSER_TESTS=1 python -m pytest tests/test_renderer.py
|
|
145
|
+
```
|
|
146
|
+
* **Windows PowerShell:**
|
|
147
|
+
```powershell
|
|
148
|
+
$env:RECFOLD_BROWSER_TESTS = "1"
|
|
149
|
+
python -m pytest tests/test_renderer.py
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Without this flag, browser tests are skipped automatically.
|
|
153
|
+
|
|
154
|
+
## Further documentation
|
|
155
|
+
|
|
156
|
+
* [Renderer API, integration, and troubleshooting](docs/renderer.md)
|
|
157
|
+
* [Demo notebook: Fibonacci, backtracking, and error guards](docs/recfold_demo.ipynb)
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Member 6: renderer and documentation
|
|
2
|
+
|
|
3
|
+
## Scope
|
|
4
|
+
|
|
5
|
+
Implemented the team's Member 6 brief in `src/recfold/renderer.py`, with tests in
|
|
6
|
+
`tests/test_renderer.py` and documentation in `README.md` and `docs/`.
|
|
7
|
+
No changes were made to the other members' modules, package exports, packaging
|
|
8
|
+
configuration, or release version. PyPI publication remains Member 1's task.
|
|
9
|
+
|
|
10
|
+
## Function-by-function implementation
|
|
11
|
+
|
|
12
|
+
Each implementation commit was preceded by new failing tests, then a passing
|
|
13
|
+
test run, followed by a commit and push. Tests and their function were committed
|
|
14
|
+
together. The commits are listed in implementation order:
|
|
15
|
+
|
|
16
|
+
| Commit | Function / increment |
|
|
17
|
+
| --- | --- |
|
|
18
|
+
| `5e9656e` | `build_render_data`: raw-tree validation, DAG/tree payloads, occurrence snapshots, supplied layout support |
|
|
19
|
+
| `37e4c8f` | `render_svg`: accessible SVG nodes, arrows, labels, escaping, errors, and unique marker IDs |
|
|
20
|
+
| `ae5d980` | `render_html`: offline interactive views, inspector, keyboard controls, zoom/pan, responsive styling |
|
|
21
|
+
| `f022b03` | `RecursionWidget._repr_html_`: isolated Jupyter iframe display |
|
|
22
|
+
| `91bd5e0` | `RecursionWidget.save`: standalone HTML export with explicit overwrite |
|
|
23
|
+
| `ef1dc2a` | `render_session`: notebook-friendly public entry point |
|
|
24
|
+
|
|
25
|
+
Follow-up commits add boundary/browser coverage and documentation. Those are
|
|
26
|
+
validation/documentation work, not bundled implementation of additional functions.
|
|
27
|
+
|
|
28
|
+
## Deliverables
|
|
29
|
+
|
|
30
|
+
- Toggle between original call tree and folded DAG.
|
|
31
|
+
- Select nodes by click or keyboard and highlight their incident edges.
|
|
32
|
+
- Inspect arguments, results, errors, parent IDs, and saved snapshots.
|
|
33
|
+
- Select individual occurrences of folded calls.
|
|
34
|
+
- View call counts, unique subtree counts, depth, and repeated-call percentage.
|
|
35
|
+
- Zoom, fit, and pan with buttons, pointer gestures, or keyboard shortcuts.
|
|
36
|
+
- Render multiple independent widgets in one notebook.
|
|
37
|
+
- Export a complete interactive HTML file that works offline.
|
|
38
|
+
- Use a precomputed layout without changing the supplied trace.
|
|
39
|
+
- Handle empty and partial/error traces, escape trace text, and reject malformed
|
|
40
|
+
raw trees before folding.
|
|
41
|
+
|
|
42
|
+
## Validation on 2026-11-16
|
|
43
|
+
|
|
44
|
+
- **137 tests passed** with the optional browser checks enabled: the original
|
|
45
|
+
98 tests plus 39 renderer test cases.
|
|
46
|
+
- The ordinary pytest run does not require Playwright; five browser tests are
|
|
47
|
+
explicitly opt-in through `RECFOLD_BROWSER_TESTS=1`.
|
|
48
|
+
- Chromium checks covered graph switching, occurrence selection, keyboard
|
|
49
|
+
selection, zoom/fit, pointer panning, mobile width, hostile trace text,
|
|
50
|
+
independent sandboxed widgets, empty traces, error details, and disclosures.
|
|
51
|
+
- Desktop and mobile screenshots were visually inspected.
|
|
52
|
+
- `docs/recfold_demo.ipynb` was validated and executed in a real Jupyter kernel:
|
|
53
|
+
all cells completed and four rich HTML widgets were produced. The committed
|
|
54
|
+
notebook is clean, with no saved outputs.
|
|
55
|
+
- In local **JupyterLab 4.6.6**, the notebook iframe loaded and its tree/DAG and
|
|
56
|
+
occurrence-selection controls worked. This does not establish compatibility
|
|
57
|
+
with every managed JupyterHub content-security policy.
|
|
58
|
+
- The wheel and source archive built successfully. A wheel smoke test under
|
|
59
|
+
Python's `-S` mode imported and rendered a trace without site-packages; wheel
|
|
60
|
+
metadata declared no runtime dependencies.
|
|
61
|
+
- Runtime validation used **Python 3.12**. The renderer also passed Python 3.9
|
|
62
|
+
syntax parsing; an actual Python 3.9 runtime was not tested.
|
|
63
|
+
- CodeRabbit review was unavailable because the local CLI was signed out;
|
|
64
|
+
manual review and the automated checks above were used instead.
|
|
65
|
+
|
|
66
|
+
## Integration
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
from recfold.renderer import render_session
|
|
70
|
+
|
|
71
|
+
render_session(your_decorated_function.last_session)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Alternatively, pass the session returned by `run_traced`, or provide a matching
|
|
75
|
+
`LayoutResult` as the second argument. See [the API guide](renderer.md).
|
|
76
|
+
|
|
77
|
+
The renderer uses only public interfaces from models, analyzer, layout, and
|
|
78
|
+
snapshot. It does not assume an automatic renderer hook in the decorator or
|
|
79
|
+
modify `__init__.py`; imports therefore come from `recfold.renderer` directly.
|
|
80
|
+
|
|
81
|
+
## Remaining upstream limitations
|
|
82
|
+
|
|
83
|
+
These belong to the existing engine and are documented rather than changed:
|
|
84
|
+
|
|
85
|
+
- The tracer's truncated representation keys can produce false cycle matches.
|
|
86
|
+
- Async/generator execution and arbitrary local-state changes are not traced.
|
|
87
|
+
- Plain recursive methods passed to `run_traced` may bypass the wrapper.
|
|
88
|
+
- Folding relies on representations and observed subtree structure.
|
|
89
|
+
- Large traces are not virtualized, and layout uses straight edges with
|
|
90
|
+
heuristic crossing reduction.
|
|
91
|
+
|
|
92
|
+
The project brief did not include a separate technical specification, so this
|
|
93
|
+
implementation follows the repository's current models and interfaces and the
|
|
94
|
+
team message's zero-dependency Jupyter HTML/SVG requirement.
|