Metadata-Version: 2.4
Name: distill-mem
Version: 0.1.0
Summary: Distill to three semantic memory tiers; top-K recall by Recency/Importance/TF-IDF and cold-start injection. Zero dependencies, only reads Markdown mirrors.
Author: 素也设计工作室
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/18DU1019/distill-mem
Project-URL: Repository, https://github.com/18DU1019/distill-mem
Project-URL: Issues, https://github.com/18DU1019/distill-mem/issues
Keywords: memory,agent,retrieval,RAG,distill,injection
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# distill-mem

**Distill raw notes into three semantic memory tiers, then recall by
Recency / Importance / TF-IDF, or inject a cold-start context block.**

`distill_mem` is a zero-dependency Python library for **layered long-term
memory** in agents. It reads a folder of Markdown memory mirrors (grouped
into long / medium / short tiers) and produces either a top-K ranked recall
for a query, or a budget-bounded context block for cold-start injection.

Design pillars that set it apart from flat "memory-cache" libs:

- **Three semantic tiers** — long-term semantic facts, medium episodic,
  short working memory. Stable anchors and fresh activations are mixed by
  quota, never letting one crowd out the other.
- **Read-only, text-only** — it never writes back to your mirrors. It parses,
  scores, and returns text. No side effects, audit of your source untouched.
- **Governance-lean** — ranking is pure math over frontmatter; the mirror
  stays human-readable Markdown.
- **Zero dependencies** — stdlib only. Relevance uses character n-gram
  TF-IDF (no tokenizer, no vector DB, no network).

## Install

```bash
pip install distill-mem
```

Or from source: clone this repo and import the `distill_mem` package directly
(from a path on your `sys.path`).

## Mirror layout

Point a tool at the **root** of your mirrors. Under the root, three
directories select the tier by name:

```
<root>/
  01-长期记忆/    # long-term, default importance 0.9
  02-中期记忆/    # medium,  default importance 0.6
  03-短期记忆/    # short,   default importance 0.4
```

Each mirror is a Markdown file with a YAML-like frontmatter block:

```markdown
---
title: Position-sizing rule
summary: Never risk more than 1 ATR per unit on a single position
last_active: 2026-08-01
importance: 0.9
tag_relevance: position sizing, ATR
---

Body text, also scored by retrieval when present.
```

Supported frontmatter keys: `title`, `summary`, `updated`, `last_active`,
`importance`, `tag_relevance`.

## Scoring

For a query `q` and a note `t`:

```
score(t) = w_r·e^(-λ·Δdays) + w_i·importance + w_s·sim(q, t)
```

- Recency uses `last_active` (supports `%Y-%m-%d` and `%Y%m%d`).
- Importance comes from frontmatter if present, else the tier default.
- Relevance is character n-gram (2–4 chars) TF-IDF overlap, normalized on the
  query side. Latin/alphanumeric words are kept natively.

## API

```python
from pathlib import Path
from distill_mem import boot, recall

root = Path("/path/to/mirrors")

# Top-K recall, ranked by three-signal score
hits = recall.recall(root, "position sizing", k=5)

# Cold-start injection: long-type anchors + warm preheating, budget-bounded
block = boot.inject(root, k=6, budget=4000)
block = boot.inject(root, query="fund analysis", k=6, budget=4000)
```

## CLI

```bash
distill-mem inject --root /path/to/mirrors
distill-mem inject --root /path/to/mirrors --query "fund analysis"
distill-mem recall  --root /path/to/mirrors "position sizing" -k 10 --detail
```

## License

Apache-2.0. See [LICENSE](LICENSE).
