Metadata-Version: 2.4
Name: algoisto
Version: 2.0.0
Summary: CRDTs, sync, and a deterministic chaos simulator — the local-first stack in pure Python.
Author-email: "K.A. Ishan Oshada" <ic31908@gmail.com>
License: MIT
Keywords: crdt,local-first,offline-first,sync,distributed,algorithms
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: System :: Distributed Computing
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENCE
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Dynamic: license-file

# algoisto

**CRDTs, sync, and a deterministic chaos simulator — the local-first stack in pure Python.**

Current version: **2.0.0**

`algoisto` provides the building blocks for applications that work offline and converge after replicas reconnect:

- `Register`: last-writer-wins register
- `Counter`: positive/negative PN-Counter
- `ORSet`: observed-remove set
- `Text`: tombstone-based RGA text
- `Replica`: one actor's local document state
- `Hub`: in-process state synchronization
- `Chaos`: seeded drop, reorder, and duplicate simulation

All modules are implemented in pure Python with no runtime dependencies.

## Installation

```bash
pip install algoisto
```

For development:

```bash
pip install -e ".[dev]"
pytest
```

## Two replicas, offline edits, convergence

```python
from algoisto import Hub, Replica

hub = Hub()
alice = hub.register(Replica("alice"))
bob = hub.register(Replica("bob"))

alice.text.insert(0, "hello ")
alice.commit()

bob.text.insert(0, "world")
bob.commit()

hub.flush()
hub.sync_all()

print(alice.text)  # converges to the same text
print(bob.text)
```

## Hostile network simulation

```python
from algoisto import Chaos, Hub, Replica

chaos = Chaos(
    seed=42,
    drop_rate=0.3,
    reorder_rate=0.5,
    duplicate_rate=0.2,
)
hub = Hub(chaos)
alice = hub.register(Replica("alice"))
bob = hub.register(Replica("bob"))

for _ in range(20):
    alice.text.insert(len(alice.text), "a")
    alice.commit()
    bob.counter.increment()
    bob.commit()
    hub.flush()

for _ in range(100):
    hub.sync_all()
    if str(alice.text) == str(bob.text):
        break

print(chaos.stats)
```

## Direct CRDT usage

```python
from algoisto import Counter, ORSet, Register, Text

name = Register("alice")
name.set("Ada")

visits = Counter("alice")
visits.increment(3)
visits.decrement()

tags = ORSet("alice")
tags.add("offline")
tags.remove("offline")

document = Text("alice")
document.insert(0, "hello")
document.insert(5, " world")
document.append("!")
document.replace("world", "local-first")
```

## Stable text anchors

`TextAnchor` stores a cursor position relative to an RGA element rather than a
raw numeric offset. Resolve it after edits or merges to find its current
position. Tombstones keep deleted anchor elements available for resolution;
the anchor's element must have been received by the replica resolving it.

```python
from algoisto import Text, TextAnchor

document = Text("alice")
document.append("hello world")
cursor = document.anchor_at(6)

document.insert(0, "Say ")
print(document.resolve_anchor(cursor))  # 10: still just before "world"

# Store and restore anchors alongside application state.
saved_cursor = cursor.to_dict()
restored_cursor = TextAnchor.from_dict(saved_cursor)
assert document.resolve_anchor(restored_cursor) == 10
```

`anchor_at(index)` accepts positions from `0` through `len(text)`, inclusive.
`resolve_anchor(anchor)` returns the corresponding current character boundary.

`Text.replace(old, new, count=-1)` replaces non-overlapping matches and returns
the number replaced. Empty search strings and counts below `-1` raise
`ValueError`; `count=0` performs no replacements.

`Text.append(text)` inserts at the current end. `Text.insert(index, text)` and
`Text.delete(index, length=1)` provide indexed editing.

## CLI

```bash
algoisto demo --seed 7 --rounds 20
algoisto version
```

The demo runs a seeded edit workload through a hostile in-process network and reports whether replicas converge.

## Examples

Ten runnable scripts are available in [`examples/`](./examples/):

| Script | Demonstrates |
|---|---|
| `01_crdt_primitives.py` | Register, counter, and set basics |
| `02_text_editing.py` | Append, replace, and text edits |
| `03_offline_replicas.py` | Editing on disconnected replicas and syncing |
| `04_concurrent_registers.py` | Deterministic register conflict resolution |
| `05_observed_remove_set.py` | Concurrent additions with observed-remove semantics |
| `06_counter_merge.py` | Merging independent counter updates |
| `07_collaborative_text.py` | Merging concurrent text edits |
| `08_snapshot_roundtrip.py` | Saving and restoring a replica snapshot as JSON |
| `09_seeded_network_chaos.py` | Reproducible lossy, reordered network delivery |
| `10_stable_cursor.py` | Persisting and resolving a cursor anchor through edits |

Run any example from the repository root:

```bash
python examples/03_offline_replicas.py
```

## API overview

| Type | Purpose |
|---|---|
| `Register` | Merge by Lamport timestamp; actor id breaks ties |
| `Counter` | Merge per-actor positive and negative totals |
| `ORSet` | Add unique tags and remove only observed tags |
| `Text` | RGA text with `insert`, `append`, `delete`, `replace`, and cursor anchors |
| `TextAnchor` | Serializable, element-relative cursor position for collaborative text |
| `Replica` | Hold and snapshot all CRDT document state |
| `Hub` | Queue, deliver, and merge state snapshots |
| `Chaos` | Add reproducible network faults from a seed |

## Why algoisto

The package name combines "algo" and "isto". It is a small, dependency-free toolkit for the algorithms behind local-first software: state convergence, synchronization, and deterministic distributed-system testing.
