Metadata-Version: 2.5
Name: memnet-llm
Version: 0.21.1
Summary: Mission working memory for LLM agents: session graph + pin_map, not a RAG corpus
Project-URL: Homepage, https://github.com/chouswei/MemNet
Project-URL: Repository, https://github.com/chouswei/MemNet
Project-URL: Changelog, https://github.com/chouswei/MemNet/blob/master/CHANGELOG.md
Project-URL: Documentation, https://github.com/chouswei/MemNet/blob/master/docs/LLM-GUIDE.md
Author: MemNet contributors
License-Expression: MIT
License-File: LICENSE
License-File: NOTICE
Keywords: agent,cli,knowledge-graph,llm,memory,working-memory
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.11
Requires-Dist: graphglot>=0.14.2
Requires-Dist: pydantic>=2.10
Requires-Dist: rich>=13.9
Requires-Dist: typer>=0.15
Provides-Extra: dev
Requires-Dist: antlr4-python3-runtime<4.14,>=4.13.2; extra == 'dev'
Requires-Dist: httpx>=0.27; extra == 'dev'
Requires-Dist: hypothesis>=6.100; extra == 'dev'
Requires-Dist: pytest-cov>=6.0; extra == 'dev'
Requires-Dist: pytest>=8.3; extra == 'dev'
Requires-Dist: python-multipart>=0.0.9; extra == 'dev'
Requires-Dist: ruff>=0.9; extra == 'dev'
Requires-Dist: starlette>=0.27; extra == 'dev'
Provides-Extra: mcp
Requires-Dist: mcp<2,>=1.8; extra == 'mcp'
Requires-Dist: uvicorn>=0.30; extra == 'mcp'
Description-Content-Type: text/markdown

# MemNet

MemNet is mission working memory for LLM agents. An agent keeps the state of its current job in a named session graph of nodes, edges, and properties. It reads a small, bounded slice of that graph when a turn needs memory, and it writes back only what changed.

The package is `memnet-llm`. It installs the `memnet` CLI and the `memnet-mcp` MCP server.

## What it is

MemNet sits between the LLM call pipeline and your data search. The host keeps the corpus and runs its own search, grep, or RAG. MemNet holds the working set for one mission: tasks, decisions, and locators that point into a few technical documents.

- Agents read and write in one language: openCypher-shaped GQL. The wire format is in [`docs/grammar/gql-wire-profile.md`](docs/grammar/gql-wire-profile.md).
- A read is a `pin_map`: a cue (a kind, a property, or a keyword) and then a bounded neighbourhood around the matching nodes.
- A write is a `mutate`: gated `CREATE`, `MERGE`, `SET`, and `DELETE` statements.
- The graph is in memory. Durability comes from snapshots, checkpoints, and optional live persist.

MemNet is not a notepad in the chat, not a vector store, not RAG, and not GraphRAG. A typical session holds tens of MiB. A corpus of gigabytes belongs in your search layer. The design reasoning is in [`docs/SHAPE.md`](docs/SHAPE.md).

## Install

MemNet needs Python 3.11 or later.

```bash
pip install memnet-llm
# or pin this release
pip install memnet-llm==0.21.1
```

Install the `mcp` extra on any host that runs `memnet-mcp` (stdio, streamable-http, or the product gateway):

```bash
pip install 'memnet-llm[mcp]'
```

Without the extra, `memnet-mcp` exits with status 2 and prints `@ERR: mcp_extra_missing|...`.

Version 1.0 is not claimed yet. The meaning of each part of the `a.b.c` version number is in [`docs/ROADMAP.md`](docs/ROADMAP.md). Release notes are in [`CHANGELOG.md`](CHANGELOG.md).

## Quickstart

This walk-through starts a local server, opens a session, writes three rows, and reads them back. Every `memnet` command except `serve` talks to a running server.

1. In terminal 1, start the server on a Unix socket:

   ```bash
   export MEMNET_IPC_SOCKET=/tmp/memnet.sock
   memnet serve --ipc
   ```

   On a host without Unix sockets, run `memnet serve` instead. It listens on TCP `127.0.0.1:18765`.

2. In terminal 2, set the same socket path and write a session map. The map declares the node kinds and their fields:

   ```bash
   export MEMNET_IPC_SOCKET=/tmp/memnet.sock
   cat > map.txt <<'EOF'
   SCHEMA TSK ; fields=id goal status
   SCHEMA NPC ; fields=id name status
   EOF
   ```

3. Open a session. The new session id goes to stderr as `MEMNET_SESSION=mn_…`:

   ```bash
   memnet session open --map-file map.txt 2> open.err
   SID=$(sed -n 's/^MEMNET_SESSION=//p' open.err)
   ```

   The server receives your arguments, not your shell environment. Pass `--session "$SID"` on every later command.

4. Write two nodes and an edge:

   ```bash
   memnet mutate --session "$SID" --stdin <<'EOF'
   CREATE (:TSK {id: 'TSK_report', goal: 'Write the test report', status: 'in_progress'})
   CREATE (:NPC {id: 'NPC_ana', name: 'Ana', status: 'active'})
   MATCH (n:NPC {id: 'NPC_ana'}), (t:TSK {id: 'TSK_report'}) CREATE (n)-[:helps]->(t)
   EOF
   ```

   The reply starts with `ok=3 fail=0`. You do not mint ids. The `id` property is an optional nickname that you can use as a cue.

5. Find the task, then read its neighbourhood:

   ```bash
   memnet query find --session "$SID" --kind TSK --limit 8
   memnet query pin-map --session "$SID" --kind TSK --depth 2
   ```

   The pin map is shaped GQL:

   ```cypher
   (:NPC {name: 'Ana', status: 'active'})
   (:TSK {goal: 'Write the test report', status: 'in_progress'})
   (:NPC {name: 'Ana', status: 'active'})-[:helps]->(:TSK {goal: 'Write the test report', status: 'in_progress'})
   ```

A single agent can skip the server. `memnet-mcp` on stdio runs the engine in its own process. Use a shared server when more than one process must see the same graph.

## Core concepts

### Sessions

A session is one graph with a time to live (TTL), a row cap, and a byte cap. You open it with a map and get back an opaque session id. Treat the id as a capability: whoever holds it can read and write the session. To hand work to another agent, pass the session id, and let that agent read the graph with `pin_map`. Do not paste the graph into the chat.

### Reads and writes

Call `pin_map` only when a turn needs something from memory, and give it a real cue. Drop earlier pin maps from the prompt before the next call. An empty cue returns the session outline, which is useful once at the start of a job. When a hard cap clips a result, the reply says so. Write with `mutate`, and send only what changed. The agent playbook in [`docs/LLM-GUIDE.md`](docs/LLM-GUIDE.md) describes the full loop.

### PROXY nodes

A `PROXY` node points at one node in a session, which can be a different session. It has the properties `target_session` and `target`, and an optional `target_backend`. The target does not need to exist when you write the `PROXY`.

`pin_map`, `find`, and walks never follow a `PROXY`. They show it as a plain node. To follow it one hop, run `memnet query follow --id <nickname>` or call the MCP tool `follow_proxy`. There are three results:

- `@STAT: follow|ok`, then the target node.
- `@STAT: follow|dangling|reason=target_missing`: the target session is on this server but the node is not.
- `@STAT: follow|not_reachable|...`, then `@FOLLOW: <target_session>|<target>|<target_backend>`: the target session is not on this server. This result does not mean that the target was deleted.

If the target is itself a `PROXY`, you get that `PROXY` back. Details are in the "PROXY nodes" section of [`docs/LLM-GUIDE.md`](docs/LLM-GUIDE.md).

### Session copy

`memnet session copy --from <session id>` (MCP `session_copy`) makes an exact in-memory copy of a live session under a new id. Reads on the copy return the same rows in the same order as the source. The copy gets a fresh TTL. After the copy, the two sessions are independent, and the source is not changed. No file is written. The reply names only the new id:

```text
@SESSION: <new session id>|<expires_at>|<ttl_minutes>
@STAT: copied|<rows>|-
@STAT: nodes|<n>|-
@STAT: edges|<n>|-
```

stderr gets `MEMNET_SESSION=<new session id>`. The copy runs the same checks as `session save` before it is registered, so a failed copy registers nothing.

### Durability and live persist

`memnet session save` and `memnet session load` write and read a snapshot file. `memnet admin checkpoint` saves every loaded session without stopping the server.

Live persist is off by default. With `MEMNET_PERSIST=full`, the server writes a snapshot of each session to disk after the session changes, and restores from those files on restart. Sessions larger than `MEMNET_PERSIST_FULL_MAX_ROWS` (default 20,000 rows) are skipped and rely on checkpoints. After each save, a session waits `MEMNET_PERSIST_SAVE_GAP_FACTOR` (default 99) times the duration of that save before its next save. This spacing limits the time spent saving one busy session to about 1% at the default. A value of `0` saves on every tick. A shutdown drain (SIGTERM or SIGINT) saves every changed session immediately. Operations and trade-offs are in [`docs/operations/live-persist.md`](docs/operations/live-persist.md).

### Several agents on one session

Parallel workers need a shared server, not the in-process default. Access control (CapsPolicy ACL) is off until you run `memnet session acl-enable`. Neighbourhood reserves (`memnet reserve`, `extend`, and `release`) let one agent hold part of the graph for a time. `memnet import-slice` merges a bounded slice from a member session into the lead session. The operating rules are in [`docs/operations/multi-agent-sessions.md`](docs/operations/multi-agent-sessions.md).

## CLI and MCP reference

Run `memnet --help`, or `memnet <command> --help`, for the flags of each command. Help also needs a running server: without one, the CLI prints `@ERR: serve_required|...`. To print help without a server, set `MEMNET_TEST_INLINE=1`. That variable is for tests and local help only.

| Document | Contents |
|----------|----------|
| [`docs/LLM-GUIDE.md`](docs/LLM-GUIDE.md) | Agent playbook, MCP tool table, and CLI quick reference |
| [`parts/memnet-mcp/README.md`](parts/memnet-mcp/README.md) | MCP server tools, transports, and HTTP settings |
| [`docs/grammar/gql-wire-profile.md`](docs/grammar/gql-wire-profile.md) | GQL wire format for reads and writes |
| [`docs/cap-contract.md`](docs/cap-contract.md) | Hard caps, refusals, and the exact error lines |
| [`docs/operations/README.md`](docs/operations/README.md) | Operations guides: safe upgrade, backup, request log, usage report, and product gateway |
| [`docs/README.md`](docs/README.md) | Full documentation index |

## Configuration

MemNet reads its settings from environment variables. Set server variables on the `memnet serve` process. [`.env.example`](.env.example) lists them with comments.

| Variable | Default | Purpose |
|----------|---------|---------|
| `MEMNET_IPC_SOCKET` | unset | Unix socket path for `memnet serve --ipc` and for CLI clients |
| `MEMNET_SERVE_HOST`, `MEMNET_SERVE_PORT` | `127.0.0.1`, `18765` | TCP bind address of `memnet serve` |
| `MEMNET_SERVE_ALLOW_REMOTE` | unset | Set to `1` to allow a non-loopback bind |
| `MEMNET_SESSION_TTL_MINUTES` | `60` | Session TTL when `--ttl` is not given |
| `MEMNET_STATE_DIR` | `~/.local/state/memnet` | Root for checkpoint, upgrade, and live persist files |
| `MEMNET_SAVE_ON_EXPIRE` | off | Save a session when its TTL ends. Needs `MEMNET_EXPIRE_SNAPSHOT_DIR` |
| `MEMNET_EXPIRE_SNAPSHOT_DIR` | unset | Directory for expire-time snapshots. `session load --session <id>` reads from it |
| `MEMNET_REQUEST_LOG` | unset | Write one JSON line per server request to this file |
| `MEMNET_PERSIST` | `off` | `full` turns on live persist |
| `MEMNET_PERSIST_SAVE_GAP_FACTOR` | `99` | Wait between saves of one session, as a multiple of its last save time. `0` saves on every tick |
| `MEMNET_MCP_TRANSPORT` | in-process | `tcp` makes `memnet-mcp` use a running `memnet serve` |

An invalid persist value stops the server at start with `@ERR: config_conflict|...`. The other persist variables are in [`docs/operations/live-persist.md`](docs/operations/live-persist.md). The MCP HTTP variables are in [`parts/memnet-mcp/README.md`](parts/memnet-mcp/README.md).

## Known limits

- One session lives on one server. Connecting several servers is open work ([#47](https://github.com/chouswei/MemNet/issues/47)).
- `pin_map` output can be written out as GQL, but reading SysML files or edited pin maps back in is not built ([#66](https://github.com/chouswei/MemNet/issues/66)).
- MemNet has no search or RAG tool. The host runs its own search and commits locators with `mutate`.

## Contributing

Clone the repository and install it in editable mode with the development and MCP extras:

```bash
pip install -e ".[dev,mcp]"
pytest
ruff check parts/common/memnet parts/memnet-mcp/software ops/tip_access_portal tests
ruff format --check parts/common/memnet parts/memnet-mcp/software ops/tip_access_portal tests
```

The tests run in-process and need no server. CI runs the same checks on Python 3.11 and 3.12 ([`.github/workflows/ci.yml`](.github/workflows/ci.yml)). Open pull requests against `master`. Write new docs in British English. Repository layout and contributor rules are in [`LAYOUT.md`](LAYOUT.md) and [`AGENTS.md`](AGENTS.md).

## Licence

MemNet is released under the MIT licence. See [`LICENSE`](LICENSE).
