pi-sleep-agent 0.4.0 → 0.6.0
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 +7 -2
- package/package.json +1 -1
- package/sleepkit/cli.py +25 -2
- package/sleepkit/store.py +65 -4
package/README.md
CHANGED
|
@@ -32,6 +32,8 @@ Sleep adds a reviewable memory cycle to Pi, OmO, and Claude Code. It records use
|
|
|
32
32
|
- **Measured replay.** Run the same task with memory off and on before deciding whether a memory helps.
|
|
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
|
+
- **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
|
+
- **Stale-value scan.** `doctor --stale <value>` finds which approved records still hold a value you know has changed (for example a deployment URL).
|
|
35
37
|
- **Safety gates.** Secrets are redacted, hostile text is quarantined, and machine-generated prompts cannot claim user provenance.
|
|
36
38
|
|
|
37
39
|
## Install
|
|
@@ -196,11 +198,12 @@ session activity -> T0 buffer -> gated sleep -> T1 provisional memory
|
|
|
196
198
|
|
|
197
199
|
Dream is Sleep's unattended reflection phase. It does not update model weights and it never writes permanent memory on its own.
|
|
198
200
|
|
|
199
|
-
A dream pass performs
|
|
201
|
+
A dream pass performs four steps:
|
|
200
202
|
|
|
201
203
|
1. **Mine.** Find instructions you repeated across separate sessions in the same project. This step is deterministic and makes no model call.
|
|
202
204
|
2. **Consolidate.** When the sleep gate passes, ask the configured model to turn recent T0 observations into provisional T1 candidates.
|
|
203
|
-
3. **
|
|
205
|
+
3. **Hygiene.** Run the read-only `doctor` scan and fold any findings (duplicate headings, near-duplicate candidates, or stale values when you pass `--stale <value>`) into the night's report.
|
|
206
|
+
4. **Replay.** When replay is due, run selected tasks with provisional memory off and on, then write a comparison report.
|
|
204
207
|
|
|
205
208
|
Every result stays reviewable. Candidates remain in T1 until you explicitly run `promote`.
|
|
206
209
|
|
|
@@ -208,6 +211,7 @@ Preview the complete dream pass without model calls or writes:
|
|
|
208
211
|
|
|
209
212
|
```bash
|
|
210
213
|
./sleep.py nightly --dry-run
|
|
214
|
+
./sleep.py nightly --dry-run --stale https://old.internal/app # also scan approved memory for a changed value
|
|
211
215
|
```
|
|
212
216
|
|
|
213
217
|
Run it now:
|
|
@@ -242,6 +246,7 @@ Run the two dream components separately when you want more control:
|
|
|
242
246
|
| `./sleep.py mine --dry-run` | Find preferences repeated across sessions |
|
|
243
247
|
| `./sleep.py nightly --dry-run` | Preview the unattended nightly workflow |
|
|
244
248
|
| `./sleep.py doctor` | Find duplicate or overlapping memory records |
|
|
249
|
+
| `./sleep.py doctor --stale <value>` | Show which approved records still hold a stale value (e.g. a changed URL) |
|
|
245
250
|
| `./sleep.py inject --project "$PWD"` | Print provisional context for the current project |
|
|
246
251
|
|
|
247
252
|
Run `./sleep.py <command> --help` for command-specific options.
|
package/package.json
CHANGED
package/sleepkit/cli.py
CHANGED
|
@@ -61,7 +61,8 @@ def cmd_status(a) -> int:
|
|
|
61
61
|
q = [f for f in os.listdir(paths.quarantine_dir()) if f.endswith(".json")]
|
|
62
62
|
mdir = paths.memory_dir(a.cwd or None)
|
|
63
63
|
try:
|
|
64
|
-
permanent = [f for f in os.listdir(mdir)
|
|
64
|
+
permanent = [f for f in os.listdir(mdir)
|
|
65
|
+
if f.endswith(".md") and f != "MEMORY.md"]
|
|
65
66
|
except OSError:
|
|
66
67
|
permanent = []
|
|
67
68
|
_p("sleep memory: T0=disposable captures, T1=provisional awaiting review, T2=permanent (promote-only)")
|
|
@@ -69,6 +70,19 @@ def cmd_status(a) -> int:
|
|
|
69
70
|
_p("T1 candidates %d provisional, %d refused, %d total (distilled, shown as unverified context)" % (len(prov), len(ref), len(cands)))
|
|
70
71
|
_p(" quarantine %d parked (needs human eyes) (held out until you review)" % len(q))
|
|
71
72
|
_p("T2 canonical %s (%d permanent records; only you can promote)" % (mdir, len(permanent)))
|
|
73
|
+
if permanent:
|
|
74
|
+
_p("")
|
|
75
|
+
_p("permanent (last 5, promoted with review date + source):")
|
|
76
|
+
for f in permanent[-5:]:
|
|
77
|
+
try:
|
|
78
|
+
with open(os.path.join(mdir, f), encoding="utf-8") as fh:
|
|
79
|
+
m, b = store.parse(fh.read())
|
|
80
|
+
except (OSError, ValueError):
|
|
81
|
+
m, b = {}, ""
|
|
82
|
+
when = m.get("promoted_at", "?")[:10]
|
|
83
|
+
src = m.get("source", "?")
|
|
84
|
+
_p(" %-10s %-10s %-8s %s" % (f[:-3][:10], when, src,
|
|
85
|
+
b.replace("\n", " ")[:88]))
|
|
72
86
|
if prov:
|
|
73
87
|
_p("")
|
|
74
88
|
_p("provisional (context only, never directives):")
|
|
@@ -140,7 +154,7 @@ def cmd_prune(a) -> int:
|
|
|
140
154
|
|
|
141
155
|
|
|
142
156
|
def cmd_doctor(a) -> int:
|
|
143
|
-
issues = store.doctor()
|
|
157
|
+
issues = store.doctor(needle=a.stale)
|
|
144
158
|
if a.json:
|
|
145
159
|
_p(json.dumps(issues, indent=2, sort_keys=True, default=str))
|
|
146
160
|
elif not issues:
|
|
@@ -206,6 +220,10 @@ def cmd_nightly(a) -> int:
|
|
|
206
220
|
rep: dict = {"ts": paths.now()} # heterogeneous on purpose: three reports, one JSON
|
|
207
221
|
rep["mine"] = ({"skipped": "--no-mine"} if a.no_mine
|
|
208
222
|
else mine.mine(min_sessions=a.mine_min_sessions, dry_run=a.dry_run))
|
|
223
|
+
# The hygiene report lands in the night regardless of the gates: it is
|
|
224
|
+
# read-only, so the worst an unattended pass can do is surface a stale
|
|
225
|
+
# value (e.g. a changed deployment URL) for a person to act on later.
|
|
226
|
+
rep["doctor"] = {"issues": store.doctor(needle=a.stale)}
|
|
209
227
|
rep["sleep"] = consolidate.run_sleep(session=a.session, model=a.model,
|
|
210
228
|
budget_usd=a.budget,
|
|
211
229
|
min_records=a.min_records,
|
|
@@ -339,6 +357,9 @@ def main(argv=None) -> int:
|
|
|
339
357
|
|
|
340
358
|
dc = sub.add_parser("doctor", help="read-only hygiene report")
|
|
341
359
|
dc.add_argument("--json", action="store_true")
|
|
360
|
+
dc.add_argument("--stale", default=None,
|
|
361
|
+
help="value (e.g. a URL) to scan approved T2 memory for; "
|
|
362
|
+
"reports which permanent records still hold it")
|
|
342
363
|
dc.set_defaults(fn=cmd_doctor)
|
|
343
364
|
|
|
344
365
|
rp = sub.add_parser("replay", help="layer 3: A/B measure provisional memory, report-only")
|
|
@@ -352,6 +373,8 @@ def main(argv=None) -> int:
|
|
|
352
373
|
|
|
353
374
|
nt = sub.add_parser("nightly",
|
|
354
375
|
help="unattended pass: gated consolidation + replay when due")
|
|
376
|
+
nt.add_argument("--stale", default=None,
|
|
377
|
+
help="value (e.g. a URL) to scan approved T2 memory for each night")
|
|
355
378
|
nt.add_argument("--session")
|
|
356
379
|
nt.add_argument("--model", default=None)
|
|
357
380
|
nt.add_argument("--budget", type=float, default=1.00)
|
package/sleepkit/store.py
CHANGED
|
@@ -146,6 +146,15 @@ def buffer_counts() -> dict:
|
|
|
146
146
|
|
|
147
147
|
|
|
148
148
|
# ------------------------------------------------------- similarity/dedup ---
|
|
149
|
+
|
|
150
|
+
# A proposal that paraphrases an already-refused candidate must not reborn as a
|
|
151
|
+
# fresh provisional idea the reviewer then judges all over again. Token-set
|
|
152
|
+
# Jaccard (the general duplicate bar) falls off too fast to catch even a close
|
|
153
|
+
# restate, so against a *refused* idea we compare by character bigrams, which
|
|
154
|
+
# tolerate synonym swaps and reordering, and block on a lower bar. The idea was
|
|
155
|
+
# already judged and rejected, so colliding with it should refuse the write.
|
|
156
|
+
REFUSED_DUP_THRESHOLD = 0.42
|
|
157
|
+
|
|
149
158
|
_WORD = re.compile(r"[a-z0-9_]+")
|
|
150
159
|
|
|
151
160
|
|
|
@@ -153,6 +162,14 @@ def _tokens(text: str) -> set:
|
|
|
153
162
|
return set(_WORD.findall((text or "").lower()))
|
|
154
163
|
|
|
155
164
|
|
|
165
|
+
def _bigrams(text: str) -> set:
|
|
166
|
+
"""Character bigrams of the lowercased text, for paraphrase-resistant
|
|
167
|
+
similarity. A pair of observations that differ only in wording shares most
|
|
168
|
+
bigrams, where token Jaccard already diverged."""
|
|
169
|
+
t = (text or "").lower()
|
|
170
|
+
return set(t[i:i + 2] for i in range(len(t) - 1))
|
|
171
|
+
|
|
172
|
+
|
|
156
173
|
def similarity(a: str, b: str) -> float:
|
|
157
174
|
ta, tb = _tokens(a), _tokens(b)
|
|
158
175
|
if not ta or not tb:
|
|
@@ -169,6 +186,32 @@ def nearest(text: str, pool: list[tuple[str, str]], threshold: float = 0.6) -> t
|
|
|
169
186
|
return (best, score) if score >= threshold else ("", score)
|
|
170
187
|
|
|
171
188
|
|
|
189
|
+
def ngram_similarity(a: str, b: str) -> float:
|
|
190
|
+
ga, gb = _bigrams(a), _bigrams(b)
|
|
191
|
+
if not ga or not gb:
|
|
192
|
+
return 0.0
|
|
193
|
+
return len(ga & gb) / float(len(ga | gb))
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def refused_collision(text: str, threshold: float = REFUSED_DUP_THRESHOLD) -> tuple[str, float]:
|
|
197
|
+
"""Best matching already-refused candidate body, or ("", 0.0) under threshold.
|
|
198
|
+
|
|
199
|
+
Refusals are the one class we hard-block aggressively: the idea was judged and
|
|
200
|
+
rejected, so coming back paraphrased must not become a new provisional review.
|
|
201
|
+
Bigram Jaccard separates a restate of the same fact from a different fact about
|
|
202
|
+
the same subject far better than token Jaccard on short observations, which is
|
|
203
|
+
why this gate uses it instead of similarity().
|
|
204
|
+
"""
|
|
205
|
+
best, score = "", 0.0
|
|
206
|
+
for m in list_candidates(with_body=True):
|
|
207
|
+
if m.get("status") != "refused":
|
|
208
|
+
continue
|
|
209
|
+
s = ngram_similarity(text, m.get("_body", ""))
|
|
210
|
+
if s > score:
|
|
211
|
+
best, score = m["id"], s
|
|
212
|
+
return (best, score) if score >= threshold else ("", score)
|
|
213
|
+
|
|
214
|
+
|
|
172
215
|
# ------------------------------------------------------------- candidates ---
|
|
173
216
|
def add_candidate(text: str, kind: str = "fact", source: str = "agent",
|
|
174
217
|
trust: str = "observed", session: str = "", cwd: str = "",
|
|
@@ -196,6 +239,10 @@ def add_candidate(text: str, kind: str = "fact", source: str = "agent",
|
|
|
196
239
|
if dup:
|
|
197
240
|
return False, "duplicate of %s (%.2f); use `sleepctl supersede %s %s`" % (
|
|
198
241
|
dup, score, dup, "new-text")
|
|
242
|
+
ref_id, ref_score = refused_collision(red)
|
|
243
|
+
if ref_id:
|
|
244
|
+
return False, "duplicate of earlier refusal %s (%.2f); this idea was judged and rejected" % (
|
|
245
|
+
ref_id, ref_score)
|
|
199
246
|
|
|
200
247
|
rec = {
|
|
201
248
|
"id": paths.new_id("c"),
|
|
@@ -290,9 +337,11 @@ def promote(cid: str, memory_dir: str | None = None, dry_run: bool = False) -> t
|
|
|
290
337
|
# Expansion-only: if the name is taken, open a new file rather than merging.
|
|
291
338
|
if os.path.exists(dest):
|
|
292
339
|
dest = os.path.join(mdir, "%s-%s.md" % (slugify(body.split(".")[0][:60]), cid[-4:]))
|
|
293
|
-
|
|
340
|
+
when = paths.now()
|
|
341
|
+
src = meta.get("source", "?")
|
|
342
|
+
line = "- **%s**: [detail](%s). %s _(promoted %s, source=%s)_" % (
|
|
294
343
|
os.path.basename(dest)[:-3], os.path.basename(dest),
|
|
295
|
-
body.replace("\n", " ")[:
|
|
344
|
+
body.replace("\n", " ")[:160], when[:10], src)
|
|
296
345
|
if dry_run:
|
|
297
346
|
return True, "would write %s (+ index line)" % dest
|
|
298
347
|
os.makedirs(mdir, exist_ok=True)
|
|
@@ -303,7 +352,7 @@ def promote(cid: str, memory_dir: str | None = None, dry_run: bool = False) -> t
|
|
|
303
352
|
# reader inside the memory store never sees a record claiming to be provisional.
|
|
304
353
|
out_meta = {k: v for k, v in meta.items() if not k.startswith("_")}
|
|
305
354
|
out_meta["status"] = "promoted"
|
|
306
|
-
out_meta["promoted_at"] =
|
|
355
|
+
out_meta["promoted_at"] = when
|
|
307
356
|
with open(dest, "w", encoding="utf-8") as fh:
|
|
308
357
|
fh.write(emit(out_meta, body))
|
|
309
358
|
ok, why = append_index_line(mdir, line)
|
|
@@ -378,7 +427,8 @@ def prune_buffer(older_than_hours: float = 0.0, session: str | None = None) -> d
|
|
|
378
427
|
return {"moved": moved, "remaining": buffer_counts()}
|
|
379
428
|
|
|
380
429
|
|
|
381
|
-
def doctor(memory_dir: str | None = None, targets: list[str] | None = None
|
|
430
|
+
def doctor(memory_dir: str | None = None, targets: list[str] | None = None,
|
|
431
|
+
needle: str | None = None) -> list[dict]:
|
|
382
432
|
"""Read-only hygiene report.
|
|
383
433
|
|
|
384
434
|
Two checks, both about content rather than formatting:
|
|
@@ -408,6 +458,17 @@ def doctor(memory_dir: str | None = None, targets: list[str] | None = None) -> l
|
|
|
408
458
|
if n > 1:
|
|
409
459
|
out.append({"issue": "duplicate-heading-in-file", "heading": h,
|
|
410
460
|
"count": n, "file": p})
|
|
461
|
+
if needle:
|
|
462
|
+
nlow = needle.strip().lower()
|
|
463
|
+
if nlow:
|
|
464
|
+
for p, text in bodies.items():
|
|
465
|
+
if os.path.basename(p) == "MEMORY.md":
|
|
466
|
+
# The index only points at detail files; flagging it re-reports
|
|
467
|
+
# every hit once for the detail file it names, so skip it.
|
|
468
|
+
continue
|
|
469
|
+
if nlow in text.lower():
|
|
470
|
+
out.append({"issue": "stale-value-in-memory", "file": p,
|
|
471
|
+
"value": needle})
|
|
411
472
|
pool = [(c["id"], c.get("_body", "")) for c in list_candidates(with_body=True)]
|
|
412
473
|
for i in range(len(pool)):
|
|
413
474
|
for j in range(i + 1, len(pool)):
|