Skip to main content

recfold

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.

Scope and design choices

No third-party runtime dependencies

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.

Tree-to-DAG folding and redundancy metrics

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:

redundancy ratio = (recorded calls - unique folded subtrees) / recorded calls

For fib(5), recfold records 15 calls and folds them into 6 unique subtrees. The ratio is (15 - 6) / 15 = 0.6, or 60%.

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.

Entry and exit snapshots

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.

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.

Structural views with occurrence records

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.

Installation

Python 3.9 or newer is required. Install from a local clone:

git clone https://github.com/Zenith-20/recfold.git
cd recfold
python -m pip install -e .

If you are using JupyterHub, ensure the repository is on the server's filesystem and install it into your active kernel:

%pip install -e /path/on/server/to/recfold

Quick start

Run this in a Jupyter notebook cell:

from recfold.tracer import trace
from recfold.renderer import render_session

@trace
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

answer = fib(5)
print(answer)  # 5

widget = render_session(fib.last_session, title="Fibonacci: repeated work")
widget  # Keep as the last expression so Jupyter displays the widget

For a plain recursive function, run_traced returns the result and trace session:

from recfold.tracer import run_traced
from recfold.renderer import render_session

def factorial(n):
    if n < 2:
        return 1
    return n * factorial(n - 1)

answer, session = run_traced(factorial, 5)
widget = render_session(session)
widget

Offline HTML export

To save the visualization as a standalone HTML document:

widget.save("fib.html")

# Use overwrite=True to replace an existing file
widget.save("fib.html", overwrite=True)

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.

Current limitations

  • 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.
  • 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.
  • 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.
  • 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.
  • No virtualization: Both graph views are rendered with all included nodes and edges. Large traces can require substantial memory and browser rendering work.

Project structure

models.py    Dataclasses for calls, DAG nodes/edges, sessions, and statistics
snapshot.py  Deep-copying and string truncation utilities
tracer.py    @trace decorator, call stack tracking, and depth/cycle guards
analyzer.py  Subtree folding logic and redundancy statistics
layout.py    Pure-Python 2D node positioning and straight edge geometry
renderer.py  Self-contained HTML/SVG generator and Jupyter widget wrapper

There is no console-script entry point or package-level re-export; import functions from their modules as shown above.

Development and testing

To run the unit test suite:

python -m pip install pytest
python -m pytest

Optional browser tests

End-to-end UI tests (verifying click selection, panning, zooming, and keyboard navigation in Chromium) require Playwright:

python -m pip install playwright
python -m playwright install chromium

Run them by setting the opt-in environment variable:

  • macOS / Linux:
    RECFOLD_BROWSER_TESTS=1 python -m pytest tests/test_renderer.py
    
  • Windows PowerShell:
    $env:RECFOLD_BROWSER_TESTS = "1"
    python -m pytest tests/test_renderer.py
    

Without this flag, browser tests are skipped automatically.

Further documentation

License

MIT

Metadata

Release files for recfold 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for recfold 1.0.0
File Size Uploaded Provenance Actions
recfold-1.0.0.tar.gz 42.6 kB Without provenance Details for recfold-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for recfold 1.0.0
File Interpreter ABI Platform Actions
recfold-1.0.0-py3-none-any.whl Python 3 none any Details for recfold-1.0.0-py3-none-any.whl

Total release size: 66.2 kB

Release files / recfold-1.0.0.tar.gz

Download URL recfold-1.0.0.tar.gz
Size 42.6 kB
Tags Source
SHA-256 checksum
How to use checksums
981895417c09dfe76e086812a8d4ab170faae8ad62d86ec7f0323efb118a088f
BLAKE2b-256 checksum
How to use checksums
77f5956681ec7a58c806f5b1001181a6ff59257832b0950b8f79ec3449129a58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / recfold-1.0.0-py3-none-any.whl

Download URL recfold-1.0.0-py3-none-any.whl
Size 23.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26e54be9b6695e0dc2383f891119b042316975e1e27b8e2ffab8204b5e43b644
BLAKE2b-256 checksum
How to use checksums
c0b8b2c75c1fb0ec55e2fdc5f84c9ef4d6bcfa85ccc3d8b27762fcc4d4838ae0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for recfold 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page