Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ All notable changes to this project are documented here. The format follows

### Added

- **Decisions that cite a file are revalidated** (#123; specs/memkernel-0-10-decision-revalidation). A new decision
records the hash of its `source` file (a readable file under the project root) as `source_hash`; revalidation
flags it `changed` or `missing`, and a decision from before this release, without a hash, only `missing`. A
flagged decision is **not hidden**: `recall` returns it, and every `recall` result now carries `needs_review` and
`review_reason` (additive). Flagged `code` memories stay hidden as before. The session-start `### Needs review`
list names each item's type, with a hint for both. `revalidate()`'s flagged items gain a `type` key.
- **A recall log and a repeated-mistake count** (#120; specs/memkernel-0-10-recall-log). With
`MEMKERNEL_RECALL_LOG=1`, each MCP `recall`, and each hook block that shows memories, appends one line (time,
query, filters, returned `ref_id`s, and the session id for hooks) to
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ Kiro (and the Kiro CLI, formerly Amazon Q Developer CLI) connects to MemKernel a
| Tool | What it does |
|------|-------------|
| `retain(content, type, session_id?, checkpoint?, source?, importance?, tags?, supersedes?)` | Store a memory. Returns `ref_id`. Idempotent — identical content + type returns the same `ref_id`. `supersedes` hides the memory it replaces. |
| `recall(query, type?, tier?, session_id?, checkpoint_only?, top_k?, include_needs_review?, order?)` | Search by natural language or identifier. Returns lightweight `ArtifactRef` list ranked by relevance + recency + importance. Records an access (reinforces retention). Code memories flagged `needs_review` are hidden unless `include_needs_review=true`. |
| `recall(query, type?, tier?, session_id?, checkpoint_only?, top_k?, include_needs_review?, order?)` | Search by natural language or identifier. Returns lightweight `ArtifactRef` list ranked by relevance + recency + importance. Records an access (reinforces retention). Each result carries `needs_review` and `review_reason`. Code memories flagged `needs_review` are hidden unless `include_needs_review=true`; flagged decisions are returned, marked. |
| `get(ref_id)` | Fetch full content of a specific memory. Records an access. |
| `forget(ref_id)` | Delete a memory from all indexes. |
| `promote(ref_id, type, content?, importance?, tags?)` | Turn a short-term/working memory into a long-term one (you supply the distilled text). Keeps provenance via `promoted_from`. |
| `consolidate(session_id?, limit?, synthesize?, revalidate?)` | Purge expired short-term memories and list the ones most worth promoting (ranked by importance × how often they were recalled). `synthesize=true` also asks an external reasoner to draft long-term statements (`suggested`); `revalidate=true` first checks every code memory against its source file (`revalidated`). The report always carries a `health` summary. |
| `consolidate(session_id?, limit?, synthesize?, revalidate?)` | Purge expired short-term memories and list the ones most worth promoting (ranked by importance × how often they were recalled). `synthesize=true` also asks an external reasoner to draft long-term statements (`suggested`); `revalidate=true` first checks every code memory, and every decision with a `source`, against its file (`revalidated`). The report always carries a `health` summary. |
| `ask(ref_id, question, mode?)` | Answer a question about one memory without loading it — for a `document` the full body is consulted. `mode`: `direct` \| `rag` \| `rlm` \| `auto`, passed to the reasoner unchanged; the built-in reasoner answers in `rag` mode with the best-matching chunks. Returns `answer`, `mode`, `citations`, `usage`. |
| `wiki_export(wiki_dir?)` | Compile long-term memories without a wiki page into draft markdown pages (`index.md`, `log.md` updated). |
| `wiki_import(wiki_dir?, adopt_legacy?, adopt_pages?, adopt_manifest?)` | Index wiki pages as long-term memories, each wiki keeping to its own: approved revisions only with wiki-studio ≥ 0.7.0, every page without it (idempotent; changed pages supersede; `corrects` supersedes the memories a page corrects). |
Expand Down Expand Up @@ -448,6 +448,8 @@ Rationale: [`docs/adr/ADR-008-llm-wiki-as-semantic-layer.md`](docs/adr/ADR-008-l

A `code` memory is a snapshot of a file. `MemoryService.revalidate()` hashes each live snapshot's whole source file and compares it with the stored content hash: a changed file flags the memory `needs_review` (`changed`), a vanished file flags it `missing`, a matching file clears the flag. Flagged memories leave default `recall` until the file is retained again (which supersedes the stale snapshot) or they are asked for with `include_needs_review=true`. Revalidation runs from `consolidate(revalidate=true)`, at every session start through `MemoryLifecycle.on_session_start()` (a `### Needs review` section lists what to re-retain), and from `POST /revalidate`. Sources resolve under `MEMKERNEL_PROJECT_ROOT` (default: the current directory); set it whenever the server or a hook is not started from the project directory. Nothing outside that root is ever read. 0.4 hashes whole files; per-chunk invalidation is a follow-up.

A `decision` that cites a file in `source` is revalidated too. When a decision is created, memkernel records the hash of that file (if it is a readable file under the project root) as `source_hash`; a retain that matches an existing decision returns it unchanged, with its own source and hash. Revalidation flags the decision `changed` when the file's hash differs and `missing` when the file is gone; a decision saved before 0.10, without a hash, is flagged only when its file is missing, and a present file clears only that flag. **A flagged decision is not hidden:** `recall` returns it with `needs_review: true` and its `review_reason`, so the agent re-checks the evidence instead of losing the decision (a file edit unrelated to the decision flags it too). It is listed under `### Needs review` at session start, marked `[decision]`; the list shows every memory still flagged, including one whose file could not be read this time. Only `source` is checked, not other paths the decision cites.

`MemoryService.health()` audits the store without changing it: orphaned chunk and document records, dangling `supersedes` / `superseded_by` / `promoted_from` / `parent_ref` links, supersessions from 0.2/0.3 that lack the durable marker, `code` memories whose source file is gone, memories flagged for review, expired short-term memories not yet purged and, in full mode only, near-duplicate pairs among the most recent long-term memories (cosine ≥ `MEMKERNEL_HEALTH_DUPLICATE_THRESHOLD`). The quick report (counts only, no embeddings, no content loads) is part of every `consolidate()` report and of the `memory://status` resource; the full report is `GET /health/store`. The pass never fixes anything: a fix mode is a later feature.

## Admin API
Expand All @@ -464,7 +466,7 @@ uvicorn memkernel.api.app:app --host 127.0.0.1 --port 8766
| `GET /stats` | Total memory count, per-type and per-tier breakdown, growth rate, last recall latency |
| `GET /collections` | List ChromaDB collections |
| `GET /health/store` | Full store health report (orphans, dangling links, stale sources, near-duplicates, …) |
| `POST /revalidate` | Check every code memory against its source file; flag changed or missing ones |
| `POST /revalidate` | Check every code memory, and every decision with a `source`, against its file; flag changed or missing ones |

---

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Origin alignment check — memkernel-0-10-decision-revalidation, review of #126

Origin: gosha70/memkernel#123 (origin/2026-11-13-issue-123.md).

Origin claim: as in plan.md.

Working claim:
> As approved, with the review's corrections: FR-003 covers the order="newest" path (checkpoints_newest_first) and
> FR-004 both result builders, with an acceptance test (SC-3c); plan and tasks cite the replay as SC-3b.

Mismatches:
- none. #123 asks that recall return flagged decisions; the newest path is part of recall.

Verdict: aligned
Confidence: high
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Origin alignment check — memkernel-0-10-decision-revalidation, review of #126, after DeepSeek

Origin: gosha70/memkernel#123 (origin/2026-11-13-issue-123.md).

Origin claim: as in plan.md.

Working claim:
> As approved, with the review's corrections: FR-003 covers the order="newest" path (checkpoints_newest_first) and
> FR-004 both result builders, with an acceptance test (SC-3c, both fields); the newest path ignores
> include_needs_review, as today; plan and tasks cite the replay as SC-3b.

Mismatches:
- none. #123 asks that recall return flagged decisions; the newest path is part of recall.

Verdict: aligned
Confidence: high
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Origin alignment check — memkernel-0-10-decision-revalidation, review of #126 (shared layer)

Origin: gosha70/memkernel#123 (origin/2026-11-13-issue-123.md).

Origin claim: as in plan.md.

Working claim:
> As approved, with: flagged decisions returned on recall's relevance and order="newest" paths; on the newest path
> through a keyword on checkpoints_newest_first() that only _newest_checkpoints() sets, so the lifecycle's
> checkpoint selection is unchanged (SC-3c checks both); both result builders fill the new fields.

Mismatches:
- none. #123 is about what recall returns; the lifecycle's choice of the latest checkpoint stays as it is.

Verdict: aligned
Confidence: high
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Origin alignment check — memkernel-0-10-decision-revalidation, build amendment A1

Origin: gosha70/memkernel#123 (origin/2026-11-13-issue-123.md).

Origin claim: as in plan.md.

Working claim:
> As approved and amended, with A1: the hook blocks' context lines label a flagged memory with its review reason,
> so a flagged decision shown at session start or after compaction is marked as recall marks it.

Mismatches:
- none. #123 asks that flagged decisions be returned marked; the hook blocks show memories too.

Verdict: aligned
Confidence: high
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Origin alignment check — memkernel-0-10-decision-revalidation, second review of #127

Origin: gosha70/memkernel#123 (origin/2026-11-13-issue-123.md).

Origin claim: as in plan.md.

Working claim:
> As amended, with: a hashless decision's present file clears only a missing flag (FR-002); the startup list shows
> durable flags, not only this pass's (FR-005); SC-4 tested on a record without the source_hash key.

Mismatches:
- none. Each keeps a flag until evidence disproves it, which is #123's "flag them, don't hide them".

Verdict: aligned
Confidence: high
7 changes: 4 additions & 3 deletions specs/memkernel-0-10-decision-revalidation/plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ origin:
`storage/chroma.py`).
2. **Revalidate**: extend `revalidate()` to live `decision` memories with a `source` (FR-002); `flagged` items gain
the memory's type, so the startup list can name them (D1).
3. **Recall**: `_filter_live` lets flagged decisions through (FR-003); `build_artifact_refs` fills `needs_review`
and `review_reason` (FR-004).
3. **Recall**: `_filter_live`, and `_newest_checkpoints()` through `checkpoints_newest_first(include_flagged_decisions=True)`,
let flagged decisions through; the default stays unchanged for the lifecycle (FR-003);
`build_artifact_refs()` and `_artifact_ref()` fill `needs_review` and `review_reason` (FR-004).
4. **Lifecycle**: the "Needs review" list per D1.
5. **Tests**: the #57 probe as a unit test (SC-1), the legacy and clearing cases (SC-2), the replay re-run (SC-3),
5. **Tests**: the #57 probe as a unit test (SC-1), the legacy and clearing cases (SC-2), the dedup cases (SC-3), the replay re-run (SC-3b), the newest-checkpoint path (SC-3c),
a 0.9.x store fixture (SC-4).
6. **Docs**: README and CHANGELOG.

Expand Down
28 changes: 23 additions & 5 deletions specs/memkernel-0-10-decision-revalidation/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ Status: approved by the owner, 2026-11-14 (D1–D4 with the dedup and hint clari
- **That comparison works for `code` only because its content is the file.** A `decision`'s content is its record
text, so it has no stored hash of the file it cites; one has to be recorded at retain.
- **`recall` hides every flagged memory, whatever its type,** unless `include_needs_review=True`
(`_filter_live`, the owner check for document parts too).
(`_filter_live`, the owner check for document parts too). The `order="newest"` path (with `checkpoint_only=True`)
bypasses `_filter_live`: `_newest_checkpoints()` takes `checkpoints_newest_first()`, which drops every flagged
record unconditionally, and builds its results with `_artifact_ref()`, not `build_artifact_refs()`.
`checkpoints_newest_first()` is shared: the lifecycle uses it to choose the latest checkpoint and in compaction
recovery (`lifecycle.py`, `_latest_checkpoint_memory`, `_compact_recovery`).
- **`recall` results do not carry the flag:** `ArtifactRef` has `ref_id`, `preview`, `score`, `token_count`, `type`,
`timestamp`, `tier`, `importance` (`core/models.py`; built in `retrieval/search.py`, `build_artifact_refs`).
`get` returns the full `Memory`, which has `needs_review` and `review_reason`.
Expand Down Expand Up @@ -43,15 +47,23 @@ Status: approved by the owner, 2026-11-14 (D1–D4 with the dedup and hint clari
- **FR-002 Revalidate decisions.** `revalidate()` branches on type: `code` is unchanged; live `decision` memories
with a `source` are checked against `source_hash`: missing file → `missing`; a different hash → `changed`; a match
clears the flag. A decision without a `source_hash` (retained before this change, or whose file was unreadable
then) is flagged only when the file is missing, and a present file clears a `missing` flag. Paths outside the
then) is flagged only when the file is missing; a present file clears only a `missing` flag, and any other flag
is kept and counted `unknown`, since nothing can disprove it. Paths outside the
project root or unreadable files count as `unknown`, as for `code`. A superseded or withdrawn decision is not
checked: `recall` already hides it. Each `flagged` item gains a `type` key (an additive change to the report).
- **FR-003 Flagged decisions are returned.** `recall` returns flagged `decision` memories, with the flag in the
result (FR-004); it keeps hiding other flagged memories unless `include_needs_review=True`.
result (FR-004), on both paths: relevance (`_filter_live`) and `order="newest"`. For the newest path,
`checkpoints_newest_first()` gains a keyword, `include_flagged_decisions: bool = False`, which only
`_newest_checkpoints()` sets; every other caller, the lifecycle's latest-checkpoint and compaction-recovery
choices included, keeps today's behaviour and never sees a flagged record. `recall` keeps hiding other flagged
memories on both paths. `include_needs_review=True` shows them on the relevance path;
the newest path ignores it, as it does today (`_newest_checkpoints` does not receive it).
- **FR-004 The mark is additive.** `ArtifactRef` gains `needs_review: bool = False` and `review_reason: str = ""`,
filled for every result; the preview and existing fields do not change.
filled for every result by both builders (`build_artifact_refs()` and `_artifact_ref()`); the preview and existing
fields do not change.
- **FR-005 Startup list (D1).** Flagged decisions appear in the session-start "Needs review" list, each line naming
its type; the hint becomes type-aware: re-check a flagged decision's evidence and retain a corrected one with
its type; the list shows this pass's flags and every other memory still flagged in the store (one whose file
could not be read this time keeps its flag); the hint becomes type-aware: re-check a flagged decision's evidence and retain a corrected one with
`supersedes`, or retain a flagged code file again to refresh it.
- **FR-006 Docs.** README (revalidation section, `recall` description), the `recall` tool's docstring (the new
fields, and `include_needs_review` no longer needed for decisions), CHANGELOG under `[Unreleased]`.
Expand Down Expand Up @@ -79,6 +91,10 @@ Status: approved by the owner, 2026-11-14 (D1–D4 with the dedup and hint clari
record with its original `source` and `source_hash`; retained again from the same `source`, the record is
unchanged; a dedup hit on a record retained before this change leaves it without a `source_hash`.
- **SC-3b** #57's replay still finds every seed record.
- **SC-3c** `recall(order="newest", checkpoint_only=True)` returns a flagged decision checkpoint with
`needs_review: true` and its `review_reason`, and still leaves out a flagged non-decision checkpoint; and the
lifecycle's checkpoint selection (`checkpoints_newest_first()` with its default, the latest checkpoint at session
start and in compaction recovery) still excludes every flagged record, the decision included.
- **SC-4** A store written by 0.9.x, holding a decision with a `source` and no `source_hash`, opens, and that
decision revalidates per FR-002 (flagged only when its file is missing). Fast suite, ruff, mypy strict,
coverage ≥ 80%; MCP tool set unchanged.
Expand All @@ -87,6 +103,8 @@ Status: approved by the owner, 2026-11-14 (D1–D4 with the dedup and hint clari

- **D1 Startup list.** Flagged decisions appear in the session-start "Needs review" list, each line naming it a
decision, with a type-aware hint (FR-005). The list keeps its 10-line cap.
- **A1 (the owner's, 2026-11-15, review of #127).** The hook blocks' context lines (session start, compaction
recovery, `on_post_compact`) mark a flagged memory in its label, e.g. `[decision, needs review: changed]`.
- **D2 The new result fields.** `needs_review` and `review_reason` on every `recall` result, for all
types (a flagged `code` memory shows them when `include_needs_review=True`).
- **D3 Which files get a hash.** Only a `source` under the project root that can be read, as `code`
Expand Down
17 changes: 9 additions & 8 deletions specs/memkernel-0-10-decision-revalidation/tasks.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Tasks: memkernel-0-10-decision-revalidation

## US1: hash and revalidate
- [ ] `source_hash` key, recorded at retain for decisions (FR-001, D3); loads empty for old records
- [ ] `revalidate()` checks decisions (FR-002); flagged items carry the type
- [ ] Unit tests for SC-1 (probe), SC-2 (legacy, clearing, unknown) and SC-3 (cross-source dedup, same-source dedup,
- [x] `source_hash` key, recorded at retain for decisions (FR-001, D3); loads empty for old records
- [x] `revalidate()` checks decisions (FR-002); flagged items carry the type
- [x] Unit tests for SC-1 (probe), SC-2 (legacy, clearing, unknown) and SC-3 (cross-source dedup, same-source dedup,
a legacy hashless dedup hit)

## US2: recall and startup
- [ ] `_filter_live` returns flagged decisions (FR-003); `ArtifactRef` fields (FR-004, D2)
- [ ] Startup "Needs review" list per D1, with a type-aware hint (FR-005)
- [ ] Tests: recall marks, code still hidden, the startup list; a 0.9.x store fixture (SC-4)
- [x] `_filter_live` and `_newest_checkpoints()` (via `include_flagged_decisions`, default off) return flagged decisions (FR-003); `ArtifactRef` fields from both
builders (FR-004, D2); a test for the newest-checkpoint path (SC-3c)
- [x] Startup "Needs review" list per D1, with a type-aware hint (FR-005)
- [x] Tests: recall marks, code still hidden, the startup list; a 0.9.x store fixture (SC-4)

## US3: docs and checks
- [ ] README, CHANGELOG (FR-006)
- [ ] #57 replay re-run (SC-3); fast suite, ruff, mypy strict, coverage
- [x] README, CHANGELOG (FR-006)
- [x] #57 replay re-run (SC-3b); fast suite, ruff, mypy strict, coverage
2 changes: 1 addition & 1 deletion src/memkernel/api/routes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ async def collections(request: Request) -> list[dict[str, Any]]:

@router.post("/revalidate")
async def revalidate(request: Request) -> dict[str, Any]:
"""Check every code memory against its source file; flag changed or missing ones (needs_review)."""
"""Check every code memory and every decision with a source against its file; flag changed or missing ones."""
svc = _svc(request)
return svc.revalidate()
Loading
Loading