Skip to main content

Durable context for long-running agents.

Project description

Remaind

pypi python license

Durable context for long-running agents.

Remaind is a local-first context ledger, compaction pipeline, and resume substrate for AI agents. It preserves the meaningful state of an agent run across context resets: a future agent can start with a clean model context, load Remaind's local state, understand what happened before, know what must happen next, and continue safely without asking the user to reconstruct the work.

Remaind is not a wiki — it is a machine-readable and human-readable continuity layer: raw event ledger, structured state, compact handover, searchable memory, structured validation, safe rollback, and a mechanical resume gate.

Install

pip install remaind                 # core: rule-based compaction
pip install 'remaind[llm]'          # + the Anthropic-backed LLM compactor

Requires Python ≥ 3.11.

Quick start

# Bootstrap a .context/ in the current directory.
remaind init

# Inspect what's there.
remaind validate
remaind status

# After work happens (events appended by your agent harness),
# compact when token band climbs.
remaind compact

# Build a resume packet for a fresh agent run.
remaind resume --next-tool deploy_prod

# Roll back if something went wrong.
remaind rollback --to 2026-05-14T03:54:33Z

Use it as a library

import remaind is the stable public API — remaind.__all__ is the whole surface. The underscore-prefixed modules are internal.

import remaind

base = "./my-agent-run"
remaind.init(base)

state = remaind.status(base)
writer = remaind.EventWriter.open(base)
writer.append(remaind.EventInput(
    type="user_message", actor="user", summary="Asked to refactor auth",
    session_id=state["session_id"], task_id=state["task_id"],
    content="Please refactor src/auth/...", importance=3,
))

if remaind.compaction_status(base).compaction_needed:
    remaind.compact(base, compactor=remaind.LLMCompactor.from_context(base))

packet = remaind.resume(base).packet   # inject packet.content into a fresh context

Full walkthrough — the agent-loop integration, choosing a compactor (including local models via CallableCompactor), the resume-injection pattern, and an exception reference — is in docs/integration.md.

What lives in .context/

.context/
├── README.md
├── CONTRACT.md           # the contract — read this first
├── active/
│   ├── state.json        # derived working state (atomic replace)
│   ├── handover.md       # compact continuity document (atomic replace)
│   └── (resume_packet.md, history/  — runtime, git-ignored)
├── logs/
│   └── events.jsonl      # append-only raw timeline (source of truth)
├── schemas/
│   ├── event.schema.json         # JSON Schema Draft 2020-12
│   ├── state.schema.json
│   ├── memory.schema.json
│   ├── validation.schema.json
│   ├── thresholds.yaml           # 40k/60k/70k/80k band math
│   ├── redaction.yaml            # 9 default secret patterns
│   ├── tools.yaml                # mechanical risk flags
│   └── migrations/{state,events}/
└── (db/context.sqlite, artifacts/  — runtime, git-ignored)

Authority order

When sources disagree, lower wins:

  1. Latest explicit user instruction
  2. Raw event log (logs/events.jsonl)
  3. active/state.json
  4. active/handover.md
  5. Derived memories

A stale summary or memory MUST NOT override a newer user instruction.

Commands

Command What it does
remaind init Bootstrap .context/; --force backs up existing
remaind validate Walk the v1 checklist (structure, schemas, events, SQLite)
remaind status [--json] State + thresholds + event counts + compaction recommendation
remaind compact Run the compaction pipeline, gated by structured validation. --llm (Anthropic) or --compactor MODULE:FUNCTION (any model) for real summarization
remaind resume [--next-tool TOOL] Build a resume packet; consult the resume gate
remaind rollback --to <ts> Restore derived files from history; raw log untouched

Architecture

Phase Subject
1 Frozen contract — schemas, configs, layout
2 Migration interfaces — state migrations + event adapters (Protocols)
3 init + validate + schema/config loaders + JSONL streaming
4 Redaction engine + content-addressed artifact store + append-only event writer
5 Atomic state/handover writes + history snapshots + threshold band recompute
6 status human/JSON inspector
7 SQLite memory + FTS5 (memories, memories_fts, events_index)
8 chars/4 token estimator + compaction-needed surface
9 Source-event selection + reference compactor
10 Structured compaction validator (reject-on-any-false)
11 Resume packet builder + mechanical resume gate
12 Rollback (restores derived files; raw log untouched)

V1 non-goals

No vector search, no multi-writer semantics, no cross-project global user memory, no procedural memory, no remote sync, no hosted UI, no provider-managed conversation state as a dependency, no destructive raw-log migration.

Hard rules

  • Do not rewrite events.jsonl.
  • Do not let summaries become source of truth.
  • Do not store secrets in raw logs.
  • Do not store huge outputs inline (threshold: 4096 bytes).
  • Do not allow stale memory to override latest user instruction.
  • Do not accept compaction without structured validation.
  • Do not mutate files on resume if the resume packet is contradictory or unsafe.

Tests

.venv/bin/python -m pytest -q

177 tests covering every phase. Adding a feature? Add a test.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

remaind-0.1.3.tar.gz (2.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

remaind-0.1.3-py3-none-any.whl (69.5 kB view details)

Uploaded Python 3

File details

Details for the file remaind-0.1.3.tar.gz.

File metadata

  • Download URL: remaind-0.1.3.tar.gz
  • Upload date:
  • Size: 2.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for remaind-0.1.3.tar.gz
Algorithm Hash digest
SHA256 361795b83a3e114c1d3b0d59eb3a13d086d8999782693df4e3594513cdc1c17e
MD5 e4901d7127bfe95f4bacd0cf67fea1d8
BLAKE2b-256 f61fb8a466e30a44eb0cf6341d625a2d88a9378575e73273a87b896511734750

See more details on using hashes here.

Provenance

The following attestation bundles were made for remaind-0.1.3.tar.gz:

Publisher: release.yml on magpiexyz-lab/Remaind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file remaind-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: remaind-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 69.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for remaind-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 265e538d0b744ea894a68e0ffd1af4c4cceb2ec5a35fdcdad952fd94e5387513
MD5 8af1a73fa00fec06f154ca70e9850678
BLAKE2b-256 957c8543801af75c076984f55435f6b2d731a1fee70cb499c70f890bdc047de7

See more details on using hashes here.

Provenance

The following attestation bundles were made for remaind-0.1.3-py3-none-any.whl:

Publisher: release.yml on magpiexyz-lab/Remaind

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page