pi-sleep-agent 0.8.9 → 0.8.11

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
@@ -29,7 +29,7 @@ Sleep adds a reviewable memory cycle to nine selectable coding-agent targets. It
29
29
  - **Budgeted consolidation.** Sleep runs only after the record and time gates pass.
30
30
  - **Review before permanence.** New memories stay provisional until you promote or refuse them.
31
31
  - **Nightly dream cycle.** Mine repeated preferences, consolidate recent observations, and measure memory effects while you are away.
32
- - **Measured replay.** Run the same task with memory off and on before deciding whether a memory helps.
32
+ - **Measured replay.** Run the same task with memory off and on before deciding whether a memory helps. Each completed pass leaves a one-line quality summary (`helps / hurts / ties`) in `status` and in the nightly report, so the question stays answerable between passes; a pass whose two arms ran the same prompt says it measured nothing instead of reporting zeros.
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.
@@ -140,7 +140,7 @@ Use `--agents oh-my-pi` for Oh My Pi. `--extension` remains an alias for selecti
140
140
 
141
141
  The extension loads on the next Pi or OmO session and performs three jobs:
142
142
 
143
- - captures user messages and relevant tool activity into T0,
143
+ - captures user messages and relevant tool activity into T0, recording which tool ran and whether it succeeded,
144
144
  - injects matching T1 provisional memory into the agent context,
145
145
  - wakes the sleep policy after the agent settles.
146
146
 
@@ -317,6 +317,7 @@ Run the two dream components separately when you want more control:
317
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
+ | `./sleep.py status -v` | ... and the last measured memory quality (`3 helps / 1 hurts / 4 ties from 8 samples`, or "no replay measurement has run yet") |
320
321
  | `./sleep.py mine --dry-run` | Find preferences repeated across sessions |
321
322
  | `./sleep.py nightly --dry-run` | Preview the unattended nightly workflow |
322
323
  | `./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) |
@@ -381,6 +382,11 @@ Sleep separates memory into three tiers:
381
382
  | Tier | Meaning | Who writes it |
382
383
  |---|---|---|
383
384
  | T0 | Temporary session observations | Hooks, extension, or `capture` |
385
+
386
+ A tool event records the tool and its outcome (`Bash failed`, `Edit ok`) on the
387
+ record's `evidence` line, so a "fact" that came out of a failed command is
388
+ visible as such in review. A call with no file path keeps the tool and outcome
389
+ too (and claims no path it does not have).
384
390
  | T1 | Provisional memory shown as unverified background | Consolidation and `mine` |
385
391
  | T2 | Permanent memory | Only an explicit `promote` command |
386
392
 
@@ -404,7 +410,7 @@ candidates/ provisional T1 records
404
410
  reports/ replay reports
405
411
  logs/ hook and nightly logs
406
412
  .quarantine/ rejected unsafe input
407
- state.json sleep and replay timestamps
413
+ state.json sleep and replay timestamps, and the last measured memory-quality summary
408
414
  env optional scheduled-run environment
409
415
  ```
410
416
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-sleep-agent",
3
- "version": "0.8.9",
3
+ "version": "0.8.11",
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
@@ -66,6 +66,10 @@ def cmd_status(a) -> int:
66
66
  except OSError:
67
67
  permanent = []
68
68
  _p("sleep memory: T0=disposable captures, T1=provisional awaiting review, T2=permanent (promote-only)")
69
+ try:
70
+ _p(replay_mod.quality_line())
71
+ except Exception:
72
+ pass
69
73
  _p("T0 buffer %d records / %d files / %d bytes (captured while you worked)" % (bc["records"], bc["files"], bc["bytes"]))
70
74
  _p("T1 candidates %d provisional, %d refused, %d total (distilled, shown as unverified context)" % (len(prov), len(ref), len(cands)))
71
75
  _p(" quarantine %d parked (needs human eyes) (held out until you review)" % len(q))
@@ -430,7 +434,14 @@ def cmd_nightly(a) -> int:
430
434
  limit=a.replay_limit,
431
435
  interval_hours=a.replay_interval,
432
436
  dry_run=a.dry_run)
437
+ # The report is machine-read (launchd tails it, tests parse it), so stdout stays a
438
+ # single JSON document. The human-readable line rides inside it and is echoed as
439
+ # PLAIN TEXT only on a terminal, where a person is the one reading the night.
440
+ qline = rep["replay"].get("quality_line") or replay_mod.quality_line()
441
+ rep["replay"]["quality_line"] = qline
433
442
  _p(json.dumps(rep, indent=2, sort_keys=True, default=str))
443
+ if sys.stdout.isatty():
444
+ _p(qline)
434
445
  # "ran" only means the gate let the pass through. If the model call itself failed,
435
446
  # the night produced nothing and must not report success to launchd.
436
447
  slept = rep["sleep"].get("action") in ("skipped", "would-run") or (
@@ -110,6 +110,28 @@ def capture_from_stdin(stdin=None, kind: str = "preference",
110
110
  _PATH_KEYS = ("file_path", "path", "notebook_path", "filePath")
111
111
 
112
112
 
113
+ def tool_outcome(payload: dict) -> str:
114
+ """'ok' or 'failed' for a finished tool call.
115
+
116
+ Claude reports a failed call on the response (`is_error`) and on the process
117
+ (`interrupted`); an explicit success flag is trusted when present. When the
118
+ payload carries neither signal the answer is "ok", because a PostToolUse event
119
+ is delivered for a call that ran, and claiming a failure we cannot see would be
120
+ worse than the weaker true statement.
121
+ """
122
+ resp = payload.get("tool_response") or payload.get("toolResponse") or {}
123
+ if isinstance(resp, dict):
124
+ if resp.get("is_error") or resp.get("isError"):
125
+ return "failed"
126
+ if resp.get("interrupted"):
127
+ return "failed"
128
+ if resp.get("success") is False:
129
+ return "failed"
130
+ if payload.get("is_error") or payload.get("isError"):
131
+ return "failed"
132
+ return "ok"
133
+
134
+
113
135
  def tool_event(payload: dict) -> tuple[str, str]:
114
136
  """(tool_name, path) from a PostToolUse payload, or ("", "")."""
115
137
  name = str(payload.get("tool_name") or payload.get("toolName") or "")
@@ -135,13 +157,21 @@ def capture_tool_from_stdin(stdin=None) -> tuple[str, str]:
135
157
  if not isinstance(payload, dict):
136
158
  return "ignored", "hook payload not an object"
137
159
  name, path = tool_event(payload)
138
- if not path:
139
- return "ignored", "no path in tool event"
140
- text = "session touched %s via %s" % (path, name or "tool")
160
+ tool = name or "tool"
161
+ outcome = tool_outcome(payload)
162
+ # A call without a path (Bash, Grep, and every command-shaped tool) still says
163
+ # something worth keeping: which tool ran and whether it worked. Dropping those
164
+ # events meant the failure that produced a wrong "fact" left no trace at all.
165
+ if path:
166
+ text = "session touched %s via %s (%s)" % (path, tool, outcome)
167
+ evidence = [path, "%s %s" % (tool, outcome)]
168
+ else:
169
+ text = "session ran %s (%s)" % (tool, outcome)
170
+ evidence = ["%s %s" % (tool, outcome)]
141
171
  ok, detail = store.capture(text, kind="fact", source="agent",
142
172
  session=str(payload.get("session_id") or ""),
143
173
  cwd=str(payload.get("cwd") or ""),
144
- evidence=[path], tags=["hook:posttool"])
174
+ evidence=evidence, tags=["hook:posttool"])
145
175
  return ("captured" if ok else "refused"), detail
146
176
 
147
177
 
@@ -283,6 +283,49 @@ def replay_due(interval_hours: float) -> tuple:
283
283
  return False, "%.1f of %.1f h since the last pass" % (elapsed, interval_hours)
284
284
 
285
285
 
286
+ def record_quality(rep: dict) -> dict:
287
+ """Persist the memory-quality summary of a completed replay pass.
288
+
289
+ The dated report file holds the detail, but a skipped night leaves no trace at all,
290
+ so "is my memory actually helping?" had no answer between runs. A small rolling
291
+ summary in state.json keeps the last real measurement readable from `status` and
292
+ the nightly report without parsing report files.
293
+ """
294
+ tally = rep.get("verdict_tally") or {}
295
+ summary = {"ran": paths.now(), "tally": tally,
296
+ "helpful": tally.get("helps", 0),
297
+ "harmed": tally.get("hurts", 0),
298
+ "tie": tally.get("tie", 0),
299
+ "samples": rep.get("samples", 0),
300
+ "answered": rep.get("answered", 0),
301
+ "treatment_delivered": bool(rep.get("treatment_delivered"))}
302
+ paths.write_state({"replay_quality": summary})
303
+ return summary
304
+
305
+
306
+ def quality_line(state: dict | None = None) -> str:
307
+ """One line for the nightly report: what the last real measurement said.
308
+
309
+ A skip is not a quality signal, so the line states the last measurement that
310
+ actually happened and how long ago, instead of pretending the skip measured
311
+ anything. Never-run is reported as such rather than as a zero score.
312
+ """
313
+ from . import consolidate # lazy: hours_since lives there
314
+ st = state if state is not None else paths.read_state()
315
+ q = st.get("replay_quality") or {}
316
+ if not q or not q.get("ran"):
317
+ return "memory quality: no replay measurement has run yet"
318
+ age = consolidate.hours_since(q.get("ran"))
319
+ when = "just now" if age is None or age < 1 else (
320
+ "%.0fh ago" % age if age < 48 else "%.0fd ago" % (age / 24.0))
321
+ if not q.get("treatment_delivered"):
322
+ return ("memory quality: last pass (%s) delivered no treatment, so it measured "
323
+ "nothing" % when)
324
+ return ("memory quality: %d helps / %d hurts / %d ties from %d samples (last pass %s)"
325
+ % (q.get("helpful", 0), q.get("harmed", 0), q.get("tie", 0),
326
+ q.get("samples", 0), when))
327
+
328
+
286
329
  def mark_replayed() -> str:
287
330
  """Stamp last_replay so the next nightly pass waits out its interval."""
288
331
  ts = paths.now()
@@ -302,9 +345,10 @@ def nightly(tasks_path: str = DEFAULT_TASKS, model: str | None = None, budget: f
302
345
  """
303
346
  due, why = replay_due(interval_hours)
304
347
  if dry_run:
305
- return {"skipped": True, "reason": "dry run", "due": due, "detail": why}
348
+ return {"skipped": True, "reason": "dry run", "due": due, "detail": why,
349
+ "quality_line": quality_line()}
306
350
  if not due:
307
- return {"skipped": True, "reason": why}
351
+ return {"skipped": True, "reason": why, "quality_line": quality_line()}
308
352
  try:
309
353
  rep = run_suite(tasks_path=tasks_path, limit=limit, model=model, budget=budget)
310
354
  report = write_report(rep)
@@ -319,9 +363,11 @@ def nightly(tasks_path: str = DEFAULT_TASKS, model: str | None = None, budget: f
319
363
  return {"failed": True, "attempted": paths.now(), "report": report,
320
364
  "reason": "no model call answered: 0/%d samples exited 0; first answer "
321
365
  "began %r" % (rep["samples"], (first.get("answer") or "")[:80])}
366
+ summary = record_quality(rep)
322
367
  return {"ran": True, "report": report, "clock": mark_replayed(),
323
368
  "treatment_delivered": rep.get("treatment_delivered"),
324
- "verdict_tally": rep.get("verdict_tally")}
369
+ "verdict_tally": rep.get("verdict_tally"),
370
+ "quality": summary, "quality_line": quality_line()}
325
371
 
326
372
 
327
373
  def write_report(rep: dict) -> str: