pi-sleep-agent 0.8.10 → 0.8.12

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
@@ -34,6 +34,7 @@ Sleep adds a reviewable memory cycle to nine selectable coding-agent targets. It
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
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.
37
+ - **Indexed by default.** A long observation is injected as its opening claim plus a pointer (`[+714 chars: sleep.py show <id>]`) instead of its whole narrative; `inject --full` prints the complete bodies. Measured on a live pool of 11 records: 5581 -> 3377 characters per injection.
37
38
  - **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).
38
39
  - **Safety gates.** Secrets are redacted, hostile text is quarantined, and machine-generated prompts cannot claim user provenance.
39
40
 
@@ -140,7 +141,7 @@ Use `--agents oh-my-pi` for Oh My Pi. `--extension` remains an alias for selecti
140
141
 
141
142
  The extension loads on the next Pi or OmO session and performs three jobs:
142
143
 
143
- - captures user messages and relevant tool activity into T0,
144
+ - captures user messages and relevant tool activity into T0, recording which tool ran and whether it succeeded,
144
145
  - injects matching T1 provisional memory into the agent context,
145
146
  - wakes the sleep policy after the agent settles.
146
147
 
@@ -335,6 +336,7 @@ name: it routes through its own config, so only `SLEEP_MODEL` reaches it.
335
336
  | `./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 |
336
337
  | `./sleep.py inject --project "$PWD"` | Print provisional context for the current project, newest first, each line dated |
337
338
  | `./sleep.py inject --project "$PWD" --query "..."` | Same, but rank records by overlap with the live request so the most relevant survive `--max` |
339
+ | `./sleep.py inject --project "$PWD" --full` | Print each record's whole body instead of the index line |
338
340
 
339
341
  Run `./sleep.py <command> --help` for command-specific options.
340
342
 
@@ -382,6 +384,11 @@ Sleep separates memory into three tiers:
382
384
  | Tier | Meaning | Who writes it |
383
385
  |---|---|---|
384
386
  | T0 | Temporary session observations | Hooks, extension, or `capture` |
387
+
388
+ A tool event records the tool and its outcome (`Bash failed`, `Edit ok`) on the
389
+ record's `evidence` line, so a "fact" that came out of a failed command is
390
+ visible as such in review. A call with no file path keeps the tool and outcome
391
+ too (and claims no path it does not have).
385
392
  | T1 | Provisional memory shown as unverified background | Consolidation and `mine` |
386
393
  | T2 | Permanent memory | Only an explicit `promote` command |
387
394
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-sleep-agent",
3
- "version": "0.8.10",
3
+ "version": "0.8.12",
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
@@ -506,10 +506,38 @@ def _inject_rank(records: list, query: str) -> list:
506
506
  return sorted(records, key=lambda c: c.get("ts", ""), reverse=True)
507
507
 
508
508
 
509
+ INDEX_CHARS = 160
510
+
511
+
512
+ def _index_line(body: str, cid: str) -> str:
513
+ """First sentence up to INDEX_CHARS, plus how much text and how to read the rest.
514
+
515
+ The pointer is the whole point: an index that hides the remainder without naming a
516
+ way to open it turns a disclosure step into a dead end.
517
+ """
518
+ text = " ".join((body or "").split())
519
+ if len(text) <= INDEX_CHARS:
520
+ return text
521
+ cut = text[:INDEX_CHARS]
522
+ stop = max(cut.rfind(". "), cut.rfind("; "))
523
+ if stop > 60:
524
+ cut = cut[: stop + 1]
525
+ else:
526
+ # no sentence ended inside the window: break on a word boundary rather than
527
+ # mid-token, then close any parenthesis the cut left open
528
+ space = cut.rfind(" ")
529
+ if space > 40:
530
+ cut = cut[:space]
531
+ cut = cut.rstrip() + ("…" if cut.count("(") > cut.count(")") else "")
532
+ rest = len(text) - len(cut.rstrip("…"))
533
+ return "%s [+%d chars: sleep.py show %s]" % (cut.rstrip(), rest, cid)
534
+
535
+
509
536
  def cmd_inject(a) -> int:
510
537
  """Print the provisional-memory block for a session's context. Framed as context."""
511
538
  pool = [c for c in store.list_candidates(status="provisional", with_body=True)
512
539
  if _within_project(c.get("cwd", ""), a.project)]
540
+ full = bool(getattr(a, "full", False))
513
541
  recs = []
514
542
  for c in _inject_rank(pool, getattr(a, "query", "") or ""):
515
543
  body = c.get("_body", "")
@@ -520,7 +548,16 @@ def cmd_inject(a) -> int:
520
548
  # its concrete targets masked so a wrong guess stays unactionable.
521
549
  if not (src == "user" and trust == "verified"):
522
550
  body, _ = safety.mask_unverified_targets(body)
523
- line = safety.render_for_context({"text": body, "source": src, "trust": trust})
551
+ if full:
552
+ shown = body
553
+ else:
554
+ # Progressive disclosure: the body of a long observation can run past 1000
555
+ # characters (measured: 11 records = 5581 chars carried in full), and most of
556
+ # that is narrative a session does not need to act. The index carries what a
557
+ # session needs to judge relevance - the claim's opening and how to open the
558
+ # rest - so the full text is one command away instead of always in context.
559
+ shown = _index_line(body, c.get("id", ""))
560
+ line = safety.render_for_context({"text": shown, "source": src, "trust": trust})
524
561
  age = _age_label(c.get("ts", ""))
525
562
  if age:
526
563
  line += " (recorded %s)" % age
@@ -676,6 +713,8 @@ def main(argv=None) -> int:
676
713
  ij.add_argument("--query", default="",
677
714
  help="rank provisional records by overlap with this text "
678
715
  "(the live request); newest first when omitted")
716
+ ij.add_argument("--full", action="store_true",
717
+ help="print each record's whole body instead of the index line")
679
718
  ij.set_defaults(fn=cmd_inject)
680
719
 
681
720
  a = ap.parse_args(argv)
@@ -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