Metadata-Version: 2.4
Name: berylx-py
Version: 0.2.0
Summary: Immutable state workflows over a tagged Freer effect monad
Author: minamorl
License-Expression: MIT
Project-URL: Homepage, https://github.com/minamorl/berylx-py
Project-URL: Repository, https://github.com/minamorl/berylx-py
Project-URL: Issues, https://github.com/minamorl/berylx-py/issues
Project-URL: Documentation, https://github.com/minamorl/berylx-py#readme
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: hypothesis>=6; extra == "dev"
Requires-Dist: build>=1; extra == "dev"
Requires-Dist: ruff>=0.9; extra == "dev"
Requires-Dist: mypy>=1; extra == "dev"
Dynamic: license-file

# berylx-py

Immutable state workflows interpreted through a tagged Freer effect monad. Python
port of [minamorl/berylx-ts](https://github.com/minamorl/berylx-ts), frozen at
[`24aabcd6059547f15b5b2817592e3d0dd4f258bb`](https://github.com/minamorl/berylx-ts/tree/24aabcd6059547f15b5b2817592e3d0dd4f258bb)
(TS 0.4.0). The frozen revision equals upstream HEAD observed on 2026-10-29.

## Install

Python 3.11 or newer. Install from [PyPI](https://pypi.org/project/berylx-py/):

```bash
python -m pip install berylx-py
```

Or install from this source directory:

```bash
python -m pip install .
```

The public repository is https://github.com/minamorl/berylx-py .
Install directly from GitHub:

```bash
python -m pip install 'git+https://github.com/minamorl/berylx-py.git'
```

For development from this source directory:

```bash
python -m venv .venv
. .venv/bin/activate
python -m pip install -r requirements-dev.txt
python -m pip install -e .
```

## Releasing

Update `project.version` in `pyproject.toml`, then commit and push to `main`.
The publishing workflow checks whether that version already exists on PyPI. A new
version runs the complete Python 3.11–3.14 CI matrix, builds and checks the
artifacts, then publishes through the registered Trusted Publisher. An existing
version is skipped. PyPI or network errors fail the check instead of uploading.
The workflow can also be dispatched manually from `main` to retry an unpublished
version; no PyPI token needs to be stored in GitHub.

## Workflows

```python
from berylx import Root, Task, Ok

root = Root.of({'user': {'name': '  mina  '}, 'total': 0})
trim = Task.of('trim', lambda f: f.at('user').at('name').update(str.strip))
charge = Task.of('charge', lambda f: f.at('total').set(100))
result = root.call(trim.then(charge))
assert isinstance(result, Ok)
assert root.at('user').at('name').get() == 'mina'
assert root.state()['total'] == 100
```

`Focus`/`Lay` is a snapshot; `at` navigates, `set`/`put`/`update` return a new root
focus. Dicts and lists are defensively copied into read-only views. `Root` commits
only successful results automatically. An `Err` carries its partial focus, structured
error, cause and trace; `unwrap()` explicitly raises. `Catch` recovers at a sequence
boundary; `rescue_with` wraps a whole workflow.

`a.par(b)` starts each branch from the same snapshot. Default strict three-way merge
preserves independent changes to existing keys and reports `merge_conflict` for
incompatible same-path changes. `.reduce(Merge.deep())` explicitly selects
right-biased two-way merging. `.accumulate()` collects failures. The default
`short_circuit` selects the first failure **after every branch settles**; it does
not cancel work. `AsyncTask` runs with `await EffectTree.run_async(...)`.

Use `a.par(b, workers=2)` or `a.par(b).workers(2)` to limit concurrently
running branches of that parallel node. The value must be an integer of at least 1.
Without a limit, asynchronous branches all start concurrently. All branches
start from the same snapshot and settle before results are combined in definition
order, even after a failure. Each nested limited parallel node keeps its own limit;
limits multiply across levels, rather than sharing one global pool.
Each nested node applies its own reducer and error policy before its parent combines
the resulting branch.

Synchronous execution remains sequential regardless of `workers`. No thread or
process pool is created: the limit is useful for asynchronous, wait-heavy work;
the GIL means threads would not speed up pure Python computation.

## Monads and effect interpretation

```python
from berylx import darkcore as d

program = d.op('lookup', {'id': 7}, int).bind(lambda n: d.pure(n + 1))
assert d.run(program, {'lookup': lambda payload: 41}) == 42
assert d.run(program, {'lookup': lambda payload: 9}) == 10

stateful = d.State.get().bind(
    lambda n: d.State.put(n + 1).seq(d.State.get())
)
assert stateful.run(10) == (11, 11)
assert d.Err('failed').bind(lambda x: d.Ok(x + 1)) == d.Err('failed')
```

Darkcore supplies `Just/Nothing`, `Right/Left`, `Ok/Err`, `Success/Failure`, `State`
and `Effect`. These value result classes are distinct from workflow `Ok/Err`.
`Validation.bind` short-circuits; `Validation.ap` accumulates errors and intentionally
differs from the application operation induced by its bind.

Tasks compile into inspectable tagged payloads `(node, focus)` over the same Effect
substrate. Handler maps interpret the tree; `EffectTree.around`/`around_async` wrap
nested tasks, effects and recovery. Callbacks declaring a required second positional
parameter receive `Perform`; recovery callbacks receive it as their third.

```python
from berylx import Task, EffectTree

fetch = Task('fetch', lambda f, p: f.put('answer', p.perform('lookup', 7)))
handlers = EffectTree.real_handlers({'lookup': lambda key: 42})
assert EffectTree.run(fetch, {}, handlers).focus.get()['answer'] == 42
assert EffectTree.dry_run(fetch, {}).steps == ['fetch']
```

Dry-run skips task bodies but still evaluates predicates. It is a planning
interpreter, not proof of behavioral equivalence. Real and virtual IO handlers
interpret the same `darkcore.IOEffects` programs.

## Equivalence and differences

The supported runtime contract includes monad values/state/effects, immutable
focus, structured failures, sequence/parallel/branch/recovery, async interpretation,
Root history/subscriptions, scoped factories, graph output and Cray adapters.
Timeline scheduling and Root integration are exposed in `berylx.timeline` and
`berylx.timeline_berylx` (clock injection, human waits, recurring origins and
concurrent commit merge). See [the frozen contract](docs/equivalence.md) and
[API mapping](docs/api.md) for exact boundaries.

Python names are snake_case; `or_else`/`and_then` avoid keywords. Python exceptions,
truthiness and numbers follow Python rules. `None` combines JavaScript null and
undefined. Python generics do not reproduce TS compile-time path-key inference.
Browser/npm exports are not applicable. The port does not claim unrestricted
cross-language equivalence for arbitrary objects, numeric edge cases or deletions
through parallel's strict reducer. Timeline uses a separate deletion-aware merge.

## Validation

Regression/unit tests and independently captured TypeScript observations were
written and run RED before implementation. After all regression/unit gates pass,
Hypothesis checks generated monad/composition/state/failure/merge properties.

```bash
python -m pytest -q tests/test_regression.py tests/test_darkcore.py tests/test_oracle.py tests/test_boundaries.py tests/test_timeline.py
python -m pytest -q tests/test_properties.py
python -m pytest -q
ruff check src tests
ruff format --check src tests
mypy src/berylx
python -m build
```

The type gate checks implementation bodies; it does not prove TS-style path types.
[Recorded evidence](evidence/) includes phase gates. To regenerate the independent
oracle, build a separate TS checkout at the frozen revision, then run:

```bash
node scripts/ts_oracle.mjs /absolute/path/to/berylx-ts/dist/index.js
```

Its output must match `tests/fixtures/ts-observations.json`. Python compares state,
error envelopes, handler order, State and value-monad observations against that
fixture, without importing or executing the TS implementation.

MIT licensed; original TypeScript implementation copyright 2026 minamorl is
retained in [LICENSE](LICENSE).
