pi-sleep-agent 0.8.2 → 0.8.4

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
@@ -33,6 +33,7 @@ Sleep adds a reviewable memory cycle to nine selectable coding-agent targets. It
33
33
  - **Repeated preference detection.** `mine` finds instructions you gave in multiple sessions without a model call.
34
34
  - **Project isolation.** A project's provisional memory is injected only into that project.
35
35
  - **Reviewable permanence.** Each approved memory in your index carries its review date and source, so you can tell when a saved instruction was pinned.
36
+ - **Relevance-ranked injection.** With a query, provisional memory is ordered by overlap with the live request and each line carries its age, so the most relevant record survives the `--max` cut and stale facts read as stale.
36
37
  - **Stale-value scan.** `doctor --stale <value>` finds which approved records still hold a value you know has changed (for example a deployment URL). Unreviewed provisional candidates are also flagged: a fact the agent proposed once and never rediscovered for 4+ weeks keeps getting injected into every session's context, so `doctor` surfaces it as `stale-candidate-unrediscovered` (a report line it never acts on, nothing is dropped).
37
38
  - **Safety gates.** Secrets are redacted, hostile text is quarantined, and machine-generated prompts cannot claim user provenance.
38
39
 
@@ -274,7 +275,7 @@ A dream pass performs four steps:
274
275
 
275
276
  1. **Mine.** Find instructions you repeated across separate sessions in the same project. This step is deterministic and makes no model call.
276
277
  2. **Consolidate.** When the sleep gate passes, ask the configured model to turn recent T0 observations into provisional T1 candidates.
277
- 3. **Hygiene.** Run the read-only `doctor` scan and fold any findings (duplicate headings, near-duplicate 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, records parked in quarantine awaiting review, or stale values when you pass `--stale <value>`) into the night's report.
278
279
  4. **Replay.** When replay is due, run selected tasks with provisional memory off and on, then write a comparison report.
279
280
 
280
281
  Every result stays reviewable. Candidates remain in T1 until you explicitly run `promote`.
@@ -318,9 +319,11 @@ Run the two dream components separately when you want more control:
318
319
  | `./sleep.py replay --run --limit 3` | Compare selected tasks with memory off and on |
319
320
  | `./sleep.py mine --dry-run` | Find preferences repeated across sessions |
320
321
  | `./sleep.py nightly --dry-run` | Preview the unattended nightly workflow |
321
- | `./sleep.py doctor` | Find duplicate or overlapping memory records, flag provisional candidates not rediscovered for 4+ weeks, and report records parked in quarantine awaiting a human (report only, never dropped or un-parked) |
322
+ | `./sleep.py doctor` | Find duplicate or overlapping memory records, flag contradicting candidates (same subject, one side carries a correction marker), flag provisional candidates not rediscovered for 4+ weeks, and report records parked in quarantine awaiting a human (report only, never dropped, merged or un-parked) |
322
323
  | `./sleep.py doctor --stale <value>` | Show which approved records still hold a stale value (e.g. a changed URL) |
323
- | `./sleep.py inject --project "$PWD"` | Print provisional context for the current project |
324
+ | `./sleep.py supersede <old> <new>` | Mark `<old>` invalid as of now and `<new>` valid as of now (`valid_until` / `valid_from`), so the window a memory was true is readable from the files |
325
+ | `./sleep.py inject --project "$PWD"` | Print provisional context for the current project, newest first, each line dated |
326
+ | `./sleep.py inject --project "$PWD" --query "..."` | Same, but rank records by overlap with the live request so the most relevant survive `--max` |
324
327
 
325
328
  Run `./sleep.py <command> --help` for command-specific options.
326
329
 
@@ -252,15 +252,21 @@ export default function sleep(pi, options = {}) {
252
252
  // request makes a duplicate impossible and makes stale memory impossible the same way.
253
253
  let block = "";
254
254
 
255
- async function loadBlock(ctx) {
255
+ // The live request text is passed as --query so the store can rank provisional records by
256
+ // overlap: with a capped --max, ordering decides what a model sees, and the newest line is
257
+ // not always the relevant one. Empty query = newest-first, the safe default.
258
+ async function loadBlock(ctx, query = "") {
256
259
  if (DISABLED()) return "";
257
260
  const cwd = ctx?.cwd || "";
261
+ const args = ["inject", "--project", cwd];
262
+ const q = String(query || "").trim().slice(0, 2000);
263
+ if (q) args.push("--query", q);
258
264
  // 5s, not the 20s default: this runs before the human's first reply is worth waiting on.
259
- const res = await sleepctl(["inject", "--project", cwd], 5000);
265
+ const res = await sleepctl(args, 5000);
260
266
  const text = (res.stdout || "").trim();
261
267
  block = text.includes("<provisional-memory") ? text : "";
262
268
  note("publish", block
263
- ? `cached ${block.length} chars for project=${cwd || "?"}`
269
+ ? `cached ${block.length} chars for project=${cwd || "?"} query=${q ? "yes" : "no"}`
264
270
  : `nothing to publish project=${cwd || "?"} ok=${res.ok} ${res.reason || ""}`.slice(0, 180));
265
271
  return block;
266
272
  }
@@ -288,6 +294,9 @@ export default function sleep(pi, options = {}) {
288
294
  pi_source: src, // rpc text wears the user's voice; Python decides what it is worth
289
295
  }, watchTimer);
290
296
  note("capture", `userprompt chars=${text.length} src=${src || "?"}`);
297
+ // Re-rank the provisional block against what this request is about, so the next
298
+ // request (the one that answers it) sees the most relevant memory first.
299
+ void loadBlock(ctx, text);
291
300
  });
292
301
 
293
302
  pi.on("tool_execution_start", async (event) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-sleep-agent",
3
- "version": "0.8.2",
3
+ "version": "0.8.4",
4
4
  "description": "A reviewable sleep and dream cycle for Pi agent memory.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/sleepkit/cli.py CHANGED
@@ -300,12 +300,48 @@ def _within_project(candidate_cwd: str, project: str) -> bool:
300
300
  return False
301
301
 
302
302
 
303
+ def _age_label(ts: str):
304
+ """Human-scale age for one record ("just now", "3h ago", "12d ago").
305
+
306
+ A model that can see a memory is three weeks old can discount it; one that sees
307
+ a bare fact cannot. Unparseable or missing timestamps yield None and the age is
308
+ simply omitted rather than guessed.
309
+ """
310
+ hours = store._hours_since(ts)
311
+ if hours is None:
312
+ return None
313
+ if hours < 1:
314
+ return "just now"
315
+ if hours < 48:
316
+ return "%dh ago" % int(hours)
317
+ return "%dd ago" % int(hours // 24)
318
+
319
+
320
+ def _inject_rank(records: list, query: str) -> list:
321
+ """Order records by overlap with ``query``, newest first as the tie-break.
322
+
323
+ Selection, not retrieval: there is no index and no embedding, only the same
324
+ token-overlap the dedupe gate already uses. With no query the order is by
325
+ timestamp descending, so the freshest memory is what survives a --max cut.
326
+ """
327
+ q = store._tokens(query) if query else set()
328
+ if q:
329
+ scored = []
330
+ for c in records:
331
+ body = c.get("_body", "")
332
+ overlap = len(q & store._tokens(body))
333
+ scored.append((overlap, c.get("ts", ""), c))
334
+ scored.sort(key=lambda t: (t[0], t[1]), reverse=True)
335
+ return [c for _, _, c in scored]
336
+ return sorted(records, key=lambda c: c.get("ts", ""), reverse=True)
337
+
338
+
303
339
  def cmd_inject(a) -> int:
304
340
  """Print the provisional-memory block for a session's context. Framed as context."""
341
+ pool = [c for c in store.list_candidates(status="provisional", with_body=True)
342
+ if _within_project(c.get("cwd", ""), a.project)]
305
343
  recs = []
306
- for c in store.list_candidates(status="provisional", with_body=True):
307
- if not _within_project(c.get("cwd", ""), a.project):
308
- continue
344
+ for c in _inject_rank(pool, getattr(a, "query", "") or ""):
309
345
  body = c.get("_body", "")
310
346
  src = c.get("source")
311
347
  trust = c.get("trust")
@@ -314,7 +350,11 @@ def cmd_inject(a) -> int:
314
350
  # its concrete targets masked so a wrong guess stays unactionable.
315
351
  if not (src == "user" and trust == "verified"):
316
352
  body, _ = safety.mask_unverified_targets(body)
317
- recs.append(safety.render_for_context({"text": body, "source": src, "trust": trust}))
353
+ line = safety.render_for_context({"text": body, "source": src, "trust": trust})
354
+ age = _age_label(c.get("ts", ""))
355
+ if age:
356
+ line += " (recorded %s)" % age
357
+ recs.append(line)
318
358
  if not recs:
319
359
  return 0
320
360
  _p("<provisional-memory unverified=\"true\">")
@@ -454,6 +494,9 @@ def main(argv=None) -> int:
454
494
  ij = sub.add_parser("inject", help="print the provisional-memory context block")
455
495
  ij.add_argument("--project", default="")
456
496
  ij.add_argument("--max", type=int, default=12)
497
+ ij.add_argument("--query", default="",
498
+ help="rank provisional records by overlap with this text "
499
+ "(the live request); newest first when omitted")
457
500
  ij.set_defaults(fn=cmd_inject)
458
501
 
459
502
  a = ap.parse_args(argv)
package/sleepkit/store.py CHANGED
@@ -47,7 +47,8 @@ IDX_END = "<!-- sleep:end -->"
47
47
  # ------------------------------------------------------------- frontmatter --
48
48
  def emit(record: dict, body: str = "") -> str:
49
49
  order = ["id", "ts", "source", "trust", "kind", "status", "session", "cwd",
50
- "tags", "evidence", "supersedes", "superseded_by", "redacted", "notes"]
50
+ "tags", "evidence", "supersedes", "superseded_by",
51
+ "valid_from", "valid_until", "redacted", "notes"]
51
52
  # Known fields first in a stable order, then anything else the record carries.
52
53
  # Dropping the remainder silently used to eat the review audit trail: `reviewed`
53
54
  # and `promoted_to`/`promoted_at` are written by set_status and are not in `order`,
@@ -174,6 +175,16 @@ def buffer_counts() -> dict:
174
175
  # already judged and rejected, so colliding with it should refuse the write.
175
176
  REFUSED_DUP_THRESHOLD = 0.42
176
177
 
178
+ # A contradiction is not a synonym for "similar": near-duplicates restate the same
179
+ # fact, contradicting records assert opposite things about the same subject. Only
180
+ # pairs that share most of their wording AND carry an explicit correction marker on
181
+ # one side are reported, so the doctor stays a signal rather than a guess.
182
+ CONTRADICTION_OVERLAP = 0.5
183
+ _CORRECTION = re.compile(
184
+ r"\b(?:no longer|not any ?more|instead of|rather than|replaced|deprecated|"
185
+ r"obsolete|stopped? (?:using|working)|used to|previously|but now|do(?:es)? not|"
186
+ r"don't|doesn't|isn't|aren't|wasn't|weren't)\b", re.I)
187
+
177
188
  _WORD = re.compile(r"[a-z0-9_]+")
178
189
 
179
190
 
@@ -426,6 +437,9 @@ def promote(cid: str, memory_dir: str | None = None, dry_run: bool = False) -> t
426
437
  out_meta = {k: v for k, v in meta.items() if not k.startswith("_")}
427
438
  out_meta["status"] = "promoted"
428
439
  out_meta["promoted_at"] = when
440
+ # The permanent copy's validity starts when the human approved it: a memory that
441
+ # outlives the record it came from needs its own start date, not the capture ts.
442
+ out_meta.setdefault("valid_from", when)
429
443
  with open(dest, "w", encoding="utf-8") as fh:
430
444
  fh.write(emit(out_meta, body))
431
445
  ok, why = append_index_line(mdir, line)
@@ -552,8 +566,17 @@ def demote(cid: str, memory_dir: str | None = None, dry_run: bool = False,
552
566
  + (" (T2 default not found)" if not t2_ok else ""))
553
567
 
554
568
  def supersede(old_id: str, new_id: str) -> bool:
555
- return (set_status(old_id, "superseded", {"superseded_by": new_id})
556
- and set_status(new_id, "provisional", {"supersedes": old_id}))
569
+ """Mark old_id invalid as of now and new_id valid as of now.
570
+
571
+ A bare superseded_by pointer says *which* record replaced this one; it does not
572
+ say *when* the old value stopped being true. That window is what a temporal
573
+ question actually asks ("what did we believe before the change?"), so both ends
574
+ are written here. The stamp is the review clock set_status already writes, so a
575
+ supersede and a refusal carry the same time vocabulary.
576
+ """
577
+ now = paths.now()
578
+ return (set_status(old_id, "superseded", {"superseded_by": new_id, "valid_until": now})
579
+ and set_status(new_id, "provisional", {"supersedes": old_id, "valid_from": now}))
557
580
 
558
581
 
559
582
  # ------------------------------------------------------------ pruning -------
@@ -626,6 +649,15 @@ def doctor(memory_dir: str | None = None, targets: list[str] | None = None,
626
649
  if sim >= 0.75:
627
650
  out.append({"issue": "near-duplicate-candidates",
628
651
  "pair": [pool[i][0], pool[j][0]], "similarity": round(sim, 2)})
652
+ # Overlapping wording with a correction marker on exactly one side: the two
653
+ # records are about the same thing but disagree, which is the state a person
654
+ # has to resolve (refuse one, or supersede). Report-only; nothing is merged,
655
+ # reordered or dropped, and a marker on BOTH sides is only a shared theme.
656
+ elif sim >= CONTRADICTION_OVERLAP:
657
+ mi, mj = bool(_CORRECTION.search(pool[i][1])), bool(_CORRECTION.search(pool[j][1]))
658
+ if mi != mj:
659
+ out.append({"issue": "contradicting-candidates",
660
+ "pair": [pool[i][0], pool[j][0]], "similarity": round(sim, 2)})
629
661
  cutoff_hours = STALE_CANDIDATE_WEEKS * 7 * 24
630
662
  for c in list_candidates():
631
663
  if c.get("status") != "provisional":