pi-sleep-agent 0.8.2 → 0.8.3

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
 
@@ -320,7 +321,8 @@ Run the two dream components separately when you want more control:
320
321
  | `./sleep.py nightly --dry-run` | Preview the unattended nightly workflow |
321
322
  | `./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
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 inject --project "$PWD"` | Print provisional context for the current project, newest first, each line dated |
325
+ | `./sleep.py inject --project "$PWD" --query "..."` | Same, but rank records by overlap with the live request so the most relevant survive `--max` |
324
326
 
325
327
  Run `./sleep.py <command> --help` for command-specific options.
326
328
 
@@ -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.3",
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)