pi-sleep-agent 0.8.6 → 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -314,7 +314,7 @@ Run the two dream components separately when you want more control:
314
314
  | `./sleep.py sleep --force` | Run consolidation now |
315
315
  | `./sleep.py show <id>` | Inspect a candidate and its rendered context |
316
316
  | `./sleep.py promote <id>` | Move an approved candidate to permanent memory |
317
- | `./sleep.py demote <id> --note "..."` | Un-approve a promoted memory: mark the approved record as retracted (kept on record, not deleted) and reopen the candidate as refused |
317
+ | `./sleep.py demote <id> --note "..."` | Un-approve a promoted memory: mark the approved record as retracted (kept on record, not deleted) and reopen the candidate as refused. Also accepts a permanent record's filename slug or the `id:` it carries, so a memory whose candidate record is gone is still reversible |
318
318
  | `./sleep.py refuse <id> --note "..."` | Record that a candidate is incorrect |
319
319
  | `./sleep.py replay --run --limit 3` | Compare selected tasks with memory off and on |
320
320
  | `./sleep.py mine --dry-run` | Find preferences repeated across sessions |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-sleep-agent",
3
- "version": "0.8.6",
3
+ "version": "0.8.7",
4
4
  "description": "A reviewable sleep and dream cycle for Pi agent memory.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/sleepkit/store.py CHANGED
@@ -518,6 +518,73 @@ def remove_index_line(memory_dir: str, line: str, slug: str) -> bool:
518
518
  return True
519
519
 
520
520
 
521
+ def _t2_by_slug_or_id(ref: str, memory_dir: str | None = None) -> tuple[str, str]:
522
+ """Find a permanent (T2) record by filename slug, or by the id inside it.
523
+
524
+ `demote` used to accept only a candidate id, so a memory whose candidate record is
525
+ gone (promoted before the archive existed, or written straight into the store) was
526
+ unreachable: `demote <slug>` answered "no such candidate" while the file sat in T2.
527
+ Returns (path, "") or ("", why).
528
+ """
529
+ mdir = memory_dir or paths.memory_dir()
530
+ if not os.path.isdir(mdir):
531
+ return "", "no memory dir: " + mdir
532
+ ref = (ref or "").strip()
533
+ if not ref:
534
+ return "", "empty reference"
535
+ for name in sorted(os.listdir(mdir)):
536
+ if not name.endswith(".md"):
537
+ continue
538
+ path = os.path.join(mdir, name)
539
+ try:
540
+ with open(path, encoding="utf-8") as fh:
541
+ meta, _body = parse(fh.read())
542
+ except (OSError, ValueError):
543
+ continue
544
+ if meta.get("status") == "retracted":
545
+ continue
546
+ if name[:-3] == ref or meta.get("id") == ref:
547
+ return path, ""
548
+ return "", "no permanent record matching %r" % ref
549
+
550
+
551
+ def demote_by_t2(ref: str, memory_dir: str | None = None, dry_run: bool = False,
552
+ note: str = "") -> tuple[bool, str]:
553
+ """Retract a permanent record that has no reachable candidate behind it.
554
+
555
+ Same contract as demote: the copy is MARKED retracted (never deleted), its index
556
+ line is dropped, and the verdict becomes visible in `status`. What differs is the
557
+ lookup - a T2 file is found by slug or by the id it carries, so a memory that was
558
+ promoted outside the normal path is still reversible.
559
+ """
560
+ path, why = _t2_by_slug_or_id(ref, memory_dir)
561
+ if not path:
562
+ return False, why
563
+ slug = os.path.basename(path)[:-3]
564
+ mdir = memory_dir or paths.memory_dir()
565
+ if dry_run:
566
+ return True, "would retract %s (drop index line, mark T2 retracted)" % slug
567
+ stamp = paths.now()
568
+ try:
569
+ with open(path, encoding="utf-8") as fh:
570
+ m2, b2 = parse(fh.read())
571
+ except (OSError, ValueError) as exc:
572
+ return False, "unreadable T2 record %s: %s" % (path, exc)
573
+ m2["status"] = "retracted"
574
+ m2["retracted_at"] = stamp
575
+ if note:
576
+ m2["note"] = note
577
+ with open(path, "w", encoding="utf-8") as fh:
578
+ fh.write(emit(m2, b2))
579
+ dropped = remove_index_line(mdir, "", slug) if mdir else False
580
+ # If a candidate is still reachable under the id the T2 copy carries, keep the two
581
+ # in step so a later `status` does not show a promoted candidate for a retracted file.
582
+ cid = m2.get("id") or ""
583
+ if cid and get_candidate(cid):
584
+ set_status(cid, "refused", {"notes": note or "retracted from T2", "reviewed": stamp})
585
+ return True, "retracted %s%s" % (slug, "" if dropped else " (index line not found)")
586
+
587
+
521
588
  def demote(cid: str, memory_dir: str | None = None, dry_run: bool = False,
522
589
  note: str = "") -> tuple[bool, str]:
523
590
  """T2 -> refused. Explicit human act: an approved memory turned out wrong.
@@ -531,6 +598,11 @@ def demote(cid: str, memory_dir: str | None = None, dry_run: bool = False,
531
598
  """
532
599
  got = get_candidate(cid)
533
600
  if not got:
601
+ # Not every permanent memory still has a candidate record (promoted before the
602
+ # archive existed, or written into the store directly). Fall back to the T2 file
603
+ # so "the permanent copy is wrong" is always actionable.
604
+ if _t2_by_slug_or_id(cid, memory_dir)[0]:
605
+ return demote_by_t2(cid, memory_dir=memory_dir, dry_run=dry_run, note=note)
534
606
  return False, "no such candidate: " + cid
535
607
  meta, body = got
536
608
  if meta.get("status") != "promoted":