ckb/tests/unit/test_sim_memory.py
Michał Kopeć 03ff809440 Improvement memory: attempts, expected outcomes, lesson context (D-0023)
Schema 1.8, after Google's Self-Improvement Memory paper (arXiv 2611.18856).
- Error book: one entry per recurring problem with every attempt, its
  expected and observed result and context; aliases on merge (ckb-lint §9).
- Decisions: expected_outcome, outcome, outcome_checked, ## Tried,
  append-only ## Outcome, an outcome-review procedure (ckb-decide); lint
  validates outcomes and closes a due review once outcome_checked >= review_on.
- Lessons: Context line, upstream lessons from linked/ in the session
  brief, adopt and merge rules (ckb-profile, session_start.py); ckb-init
  Step 8b carries lessons and problems into a new KB; ckb-upgrade b5.
- Deferred: a self-improvement loop over the template. Rejected: loading
  the whole record into context.
No VERSION change or changelog row: made on the merge from dev (D-0022).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 08:17:19 +01:00

109 lines
4.5 KiB
Python

"""Schema 1.8 (D-0023): decisions carry an expected outcome checked at review,
and the session brief lists upstream lessons from linked KBs and skips
struck-through ones."""
import json
import shutil
import subprocess
import sys
import tempfile
import unittest
from datetime import date
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
FIX = ROOT / "tests/fixtures/minimal-kb"
SCRIPTS = ROOT / ".agents/skills/ckb-lint/scripts"
LINT = SCRIPTS / "lint_report.py"
TODAY = "2026-11-18"
sys.path.insert(0, str(SCRIPTS))
import session_start # noqa: E402
DECISION = ("---\ntype: decision\ntldr: {n}\nstatus: accepted\ndecided_on: 2026-09-01\n"
"decided_by: A\n{extra}---\n\n# D-{n}\n")
class DecisionOutcomes(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.tmp = tempfile.TemporaryDirectory()
kb = Path(cls.tmp.name) / "kb"
shutil.copytree(str(FIX), str(kb))
d = kb / "wiki" / "decisions"
d.mkdir(exist_ok=True)
pages = {
"0091-expects.md": "review_on: 2026-11-01\nexpected_outcome: Fewer orphans.\n",
"0092-checked.md": "review_on: 2026-11-01\nexpected_outcome: Fewer orphans.\n"
"outcome: partly\noutcome_checked: 2026-11-10\n",
"0093-plain-due.md": "review_on: 2026-11-01\n",
"0094-bad-outcome.md": "outcome: great\noutcome_checked: 2026-11-10\n",
"0095-no-outcome.md": "outcome_checked: 2026-11-10\n",
"0096-old-check.md": "review_on: 2026-11-15\nexpected_outcome: X.\n"
"outcome: met\noutcome_checked: 2026-11-10\n",
}
for name, extra in pages.items():
(d / name).write_text(DECISION.format(n=name[:4], extra=extra), encoding="utf-8")
idx = d / "index.md"
old = idx.read_text(encoding="utf-8") if idx.is_file() else "# Decisions\n"
idx.write_text(old + "".join(f"\n- [{n}]({n})" for n in pages) + "\n", encoding="utf-8")
r = subprocess.run([sys.executable, str(LINT), "--json", "--today", TODAY], cwd=str(kb),
capture_output=True, text=True, encoding="utf-8")
cls.findings = json.loads(r.stdout)["trees"][0]["decisions"]
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def about(self, name):
return [f for f in self.findings if name in f]
def test_due_review_with_expectation_asks_for_outcome(self):
(f,) = self.about("0091")
self.assertIn("due for review", f)
self.assertIn("record `outcome`", f)
def test_outcome_checked_after_review_on_closes_it(self):
self.assertEqual(self.about("0092"), [])
def test_plain_due_review_unchanged(self):
(f,) = self.about("0093")
self.assertIn("due for review", f)
self.assertNotIn("expected_outcome", f)
def test_outcome_vocabulary(self):
self.assertTrue(any("`outcome: great`" in f for f in self.about("0094")))
def test_checked_without_outcome(self):
self.assertTrue(any("no `outcome`" in f for f in self.about("0095")))
def test_check_before_review_on_does_not_close_it(self):
self.assertTrue(any("due for review" in f for f in self.about("0096")))
LESSONS = ("---\ntype: lessons\ntldr: Lessons.\n---\n\n# Lessons\n\n"
"### 2026-11-01 — {a}\n- **Scope:** KB\n\n"
"### 2026-11-02 — ~~{b}~~ — dropped 2026-11-05: no longer true\n- **Scope:** KB\n")
class UpstreamLessons(unittest.TestCase):
def test_local_and_upstream_listed_struck_skipped(self):
with tempfile.TemporaryDirectory() as t:
root = Path(t)
(root / "wiki").mkdir()
(root / "wiki/lessons.md").write_text(LESSONS.format(a="Local rule", b="Dead local"), encoding="utf-8")
up = root / "linked" / "parent" / "wiki"
up.mkdir(parents=True)
(up / "lessons.md").write_text(LESSONS.format(a="Parent rule", b="Dead parent"), encoding="utf-8")
out = "\n".join(session_start.lessons_lines(root, None, date(2026, 11, 18)))
self.assertIn("lessons: 1 in wiki/lessons.md", out)
self.assertIn("Local rule", out)
self.assertIn("upstream lessons: 1 in linked/parent/wiki/lessons.md", out)
self.assertIn("Parent rule", out)
self.assertNotIn("Dead", out)
def test_nothing_without_lessons(self):
with tempfile.TemporaryDirectory() as t:
self.assertEqual(session_start.lessons_lines(Path(t), None, date(2026, 11, 18)), [])
if __name__ == "__main__":
unittest.main()