pi-sleep-agent 0.8.6 → 0.8.8
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 +3 -3
- package/package.json +1 -1
- package/sleepkit/cli.py +9 -0
- package/sleepkit/store.py +162 -0
package/README.md
CHANGED
|
@@ -275,7 +275,7 @@ A dream pass performs four steps:
|
|
|
275
275
|
|
|
276
276
|
1. **Mine.** Find instructions you repeated across separate sessions in the same project. This step is deterministic and makes no model call.
|
|
277
277
|
2. **Consolidate.** When the sleep gate passes, ask the configured model to turn recent T0 observations into provisional T1 candidates.
|
|
278
|
-
3. **Hygiene.** Run the read-only `doctor` scan and fold any findings (duplicate headings, near-duplicate or contradicting candidates, records parked in quarantine awaiting review, or stale values when you pass `--stale <value>`) into the night's report.
|
|
278
|
+
3. **Hygiene.** Run the read-only `doctor` scan and fold any findings (duplicate headings, near-duplicate or contradicting candidates, broken supersede links, records parked in quarantine awaiting review, or stale values when you pass `--stale <value>`) into the night's report.
|
|
279
279
|
4. **Replay.** When replay is due, run selected tasks with provisional memory off and on, then write a comparison report.
|
|
280
280
|
|
|
281
281
|
Every result stays reviewable. Candidates remain in T1 until you explicitly run `promote`.
|
|
@@ -314,12 +314,12 @@ 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 |
|
|
321
321
|
| `./sleep.py nightly --dry-run` | Preview the unattended nightly workflow |
|
|
322
|
-
| `./sleep.py doctor` | Find duplicate or overlapping memory records, flag contradicting candidates (same subject, one side carries a correction marker) and print the `supersede <older> <newer>` command to run, flag provisional candidates not rediscovered for 4+ weeks, and report records parked in quarantine awaiting a human (report only: it never supersedes, drops, merges or un-parks anything itself) |
|
|
322
|
+
| `./sleep.py doctor` | Find duplicate or overlapping memory records, flag contradicting candidates (same subject, one side carries a correction marker) and print the `supersede <older> <newer>` command to run, flag provisional candidates not rediscovered for 4+ weeks, flag supersede pointers that name a record no longer in the store, and report records parked in quarantine awaiting a human (report only: it never supersedes, drops, merges or un-parks anything itself) |
|
|
323
323
|
| `./sleep.py doctor --stale <value>` | Show which approved records still hold a stale value (e.g. a changed URL) |
|
|
324
324
|
| `./sleep.py doctor --contradictions` | Ask a model to adjudicate shortlisted pairs of provisional records for conflicts (`--budget`, default $0.25); report-only, and each finding carries the `supersede <older> <newer>` command to run |
|
|
325
325
|
|
package/package.json
CHANGED
package/sleepkit/cli.py
CHANGED
|
@@ -151,6 +151,15 @@ def cmd_show(a) -> int:
|
|
|
151
151
|
return 2
|
|
152
152
|
meta, body = got
|
|
153
153
|
_p(store.emit(meta, body))
|
|
154
|
+
# Show the supersede chain: with only the two pointer fields visible, answering
|
|
155
|
+
# "which record is current, and what did it replace?" means reading other files.
|
|
156
|
+
chain = store.supersession_chain(a.id)
|
|
157
|
+
if len(chain["order"]) > 1 or chain["broken"]:
|
|
158
|
+
_p("# supersede chain (oldest -> newest):")
|
|
159
|
+
_p(" " + " -> ".join(chain["order"]))
|
|
160
|
+
_p(" current: %s" % chain["current"])
|
|
161
|
+
for gone in chain["broken"]:
|
|
162
|
+
_p(" BROKEN: %s is referenced but not in the store" % gone)
|
|
154
163
|
_p("# rendered for context:\n" + safety.render_for_context(
|
|
155
164
|
{"text": body, "source": meta.get("source"), "trust": meta.get("trust")}))
|
|
156
165
|
return 0
|
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":
|
|
@@ -566,6 +638,83 @@ def demote(cid: str, memory_dir: str | None = None, dry_run: bool = False,
|
|
|
566
638
|
return True, ("retracted %s" % (slug or cid)
|
|
567
639
|
+ (" (T2 default not found)" if not t2_ok else ""))
|
|
568
640
|
|
|
641
|
+
def _first_ref(value) -> str:
|
|
642
|
+
"""One id out of a supersede field, which parse() returns as a LIST.
|
|
643
|
+
|
|
644
|
+
store.parse() turns tags/evidence/supersedes/superseded_by into lists, so treating
|
|
645
|
+
the value as a string produced repr text like "['c1']" and every chain walk missed.
|
|
646
|
+
Accepts either shape and takes the first entry.
|
|
647
|
+
"""
|
|
648
|
+
if isinstance(value, (list, tuple)):
|
|
649
|
+
return str(value[0]).strip() if value else ""
|
|
650
|
+
return str(value or "").split("|")[0].strip()
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
def _archived_candidates() -> list:
|
|
654
|
+
"""Parsed frontmatter of archived candidates (promoted/superseded records live there)."""
|
|
655
|
+
out = []
|
|
656
|
+
d = paths.archive_dir("candidates")
|
|
657
|
+
if not os.path.isdir(d):
|
|
658
|
+
return out
|
|
659
|
+
for name in sorted(os.listdir(d)):
|
|
660
|
+
if not name.endswith(".md"):
|
|
661
|
+
continue
|
|
662
|
+
try:
|
|
663
|
+
with open(os.path.join(d, name), encoding="utf-8") as fh:
|
|
664
|
+
meta, _body = parse(fh.read())
|
|
665
|
+
except (OSError, ValueError):
|
|
666
|
+
continue
|
|
667
|
+
out.append(meta)
|
|
668
|
+
return out
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
def supersession_chain(cid: str, max_hops: int = 20) -> dict:
|
|
672
|
+
"""Walk a record's supersede links in both directions.
|
|
673
|
+
|
|
674
|
+
A supersede writes one pointer on each side, so the *chain* is only visible by
|
|
675
|
+
following them: after three changes, "which record is current?" costs reading files.
|
|
676
|
+
Returns {"order": [oldest..newest], "current": id, "broken": [ids]} - `broken` names
|
|
677
|
+
ids that are referenced but missing from the store, which is how a hand-edited or
|
|
678
|
+
partially archived chain shows up instead of silently reading as a two-record story.
|
|
679
|
+
"""
|
|
680
|
+
seen_up, chain = set(), []
|
|
681
|
+
node = cid
|
|
682
|
+
while node and node not in seen_up and len(chain) < max_hops:
|
|
683
|
+
seen_up.add(node)
|
|
684
|
+
chain.append(node)
|
|
685
|
+
got = get_candidate(node)
|
|
686
|
+
if not got:
|
|
687
|
+
break
|
|
688
|
+
node = _first_ref(got[0].get("supersedes"))
|
|
689
|
+
order = list(reversed(chain)) # oldest -> newest
|
|
690
|
+
missing = []
|
|
691
|
+
if order and not get_candidate(order[0]):
|
|
692
|
+
missing.append(order[0])
|
|
693
|
+
# Walk forward from the OLDEST end, not from the requested id: asking about the
|
|
694
|
+
# replaced record must still report what replaced it.
|
|
695
|
+
# Forward walk: follow superseded_by from whatever end we know, always extending the
|
|
696
|
+
# NEWEST side. It is not gated on the seed id (the oldest member has no supersedes and
|
|
697
|
+
# would otherwise stop the walk before it ever looked forward).
|
|
698
|
+
node = order[-1] if order else cid
|
|
699
|
+
seen_down = set(order)
|
|
700
|
+
while node and len(order) < max_hops:
|
|
701
|
+
got = get_candidate(node)
|
|
702
|
+
if not got:
|
|
703
|
+
break
|
|
704
|
+
nxt = _first_ref(got[0].get("superseded_by"))
|
|
705
|
+
if not nxt or nxt in seen_down:
|
|
706
|
+
break
|
|
707
|
+
seen_down.add(nxt)
|
|
708
|
+
if get_candidate(nxt):
|
|
709
|
+
order.append(nxt)
|
|
710
|
+
node = nxt
|
|
711
|
+
else:
|
|
712
|
+
missing.append(nxt)
|
|
713
|
+
break
|
|
714
|
+
current = order[-1] if order else cid
|
|
715
|
+
return {"order": order, "current": current, "broken": missing}
|
|
716
|
+
|
|
717
|
+
|
|
569
718
|
def supersede(old_id: str, new_id: str) -> bool:
|
|
570
719
|
"""Mark old_id invalid as of now and new_id valid as of now.
|
|
571
720
|
|
|
@@ -736,6 +885,19 @@ def doctor(memory_dir: str | None = None, targets: list[str] | None = None,
|
|
|
736
885
|
# Report the count and the newest ids; nothing is dropped or changed here.
|
|
737
886
|
# Path assembled without paths.home(): home() creates the whole tier skeleton as a
|
|
738
887
|
# side effect, and a read-only report that lays down directories is not read-only.
|
|
888
|
+
# A supersede writes a pointer on each side, so a link can dangle: the file was
|
|
889
|
+
# pruned, the archive lost it, or a hand edit dropped it. A dangling link reads as
|
|
890
|
+
# "this record replaced something" while the something is unrecoverable - worth one
|
|
891
|
+
# line, since it is invisible in every other view.
|
|
892
|
+
for c in list_candidates(with_body=False) + _archived_candidates():
|
|
893
|
+
cid = c.get("id")
|
|
894
|
+
if not cid:
|
|
895
|
+
continue
|
|
896
|
+
for field in ("supersedes", "superseded_by"):
|
|
897
|
+
target = _first_ref(c.get(field))
|
|
898
|
+
if target and not get_candidate(target):
|
|
899
|
+
out.append({"issue": "broken-supersede-link", "id": cid,
|
|
900
|
+
"field": field, "missing": target})
|
|
739
901
|
qdir = os.path.join(paths.SLEEP_HOME, ".quarantine")
|
|
740
902
|
if os.path.isdir(qdir):
|
|
741
903
|
parked = sorted(f for f in os.listdir(qdir) if f.endswith(".json"))
|