pi-sleep-agent 0.8.13 → 0.8.15

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,14 @@ 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
+ - **Two clocks per record.** `ts` is when the record was written; `event_at` is when
38
+ the thing it describes actually happened, and it is written **only when the record
39
+ states a date** (a date equal to the capture day is not a second clock). An empty
40
+ `event_at` means the record does not say, never a guess, so `show` can answer
41
+ "when was this true" without inventing an event time.
42
+ - **Tagged records.** Consolidation writes 1-4 lowercase topic tags per record (`deploy|aws`),
43
+ normalized and capped in `store`, and `inject --topic` selects on them. A tag is a
44
+ hint a model proposed, so a wrong tag costs a missing filter hit, never a wrong fact.
37
45
  - **Served-record accounting.** Every injection stamps the records it actually printed (`injected`, `injected_at` on the record itself), so `doctor` can name a provisional that **no session has ever asked for** in 8+ weeks (`unused-candidate-never-served`). That is a different fact from "nobody re-learned it", and only the record can tell the two apart; report only, nothing is dropped.
38
46
  - **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.
39
47
  - **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).
@@ -338,6 +346,7 @@ name: it routes through its own config, so only `SLEEP_MODEL` reaches it.
338
346
  | `./sleep.py inject --project "$PWD"` | Print provisional context for the current project, newest first, each line dated |
339
347
  | `./sleep.py inject --project "$PWD" --query "..."` | Same, but rank records by overlap with the live request so the most relevant survive `--max` |
340
348
  | `./sleep.py inject --project "$PWD" --full` | Print each record's whole body instead of the index line |
349
+ | `./sleep.py inject --project "$PWD" --topic deploy` | Only records whose `tags` carry that topic (an index lookup, not a keyword search) |
341
350
 
342
351
  Run `./sleep.py <command> --help` for command-specific options.
343
352
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-sleep-agent",
3
- "version": "0.8.13",
3
+ "version": "0.8.15",
4
4
  "description": "A reviewable sleep and dream cycle for Pi agent memory.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -18,6 +18,9 @@ Each proposal is header fields, a blank line, then the observation:
18
18
  source: user | agent | inferred
19
19
  kind: fact | preference | procedure | warning
20
20
  trust: verified | observed | speculative
21
+ tags: <1-4 pipe-separated lowercase topics, e.g. deploy|aws or gitlab|auth>
22
+ event_at: <optional YYYY-MM-DD, ONLY when the observation itself states a date for
23
+ the thing you are describing; omit the line when it does not>
21
24
  evidence: <optional, comma separated file paths or session refs>
22
25
 
23
26
  <one observation, 1-3 sentences, written so a reader in a future session can
@@ -52,5 +55,12 @@ here.
52
55
 
53
56
  - You have read access only, and you do not need it: the buffer and the existing
54
57
  pool are already in your prompt. Do not run commands.
58
+ - The `tags` line is the only index a future session can filter on: use the
59
+ topic words a person would search for (deploy, gitlab, auth), not the
60
+ sentence's verbs. Never repeat the kind as a tag.
61
+ - `event_at` is only for a date the observation already states about the thing it
62
+ describes ("on 2026-10-14 the staging host moved"). Never derive it from what you
63
+ know, never write the session's own date, and omit the line otherwise: the record
64
+ keeps two separate clocks and an empty `event_at` means "the record does not say".
55
65
  - Proposals are cheap and refusals are cheap; a poisoned memory store is not. When
56
66
  in doubt, emit fewer and mark the remainder `speculative`.
package/sleepkit/cli.py CHANGED
@@ -155,6 +155,10 @@ def cmd_show(a) -> int:
155
155
  return 2
156
156
  meta, body = got
157
157
  _p(store.emit(meta, body))
158
+ event_at = str(meta.get("event_at") or "").strip()
159
+ if event_at:
160
+ _p("# two clocks: recorded %s, the recorded event happened %s"
161
+ % (str(meta.get("ts", "?"))[:10], event_at))
158
162
  # Show the supersede chain: with only the two pointer fields visible, answering
159
163
  # "which record is current, and what did it replace?" means reading other files.
160
164
  chain = store.supersession_chain(a.id)
@@ -535,8 +539,10 @@ def _index_line(body: str, cid: str) -> str:
535
539
 
536
540
  def cmd_inject(a) -> int:
537
541
  """Print the provisional-memory block for a session's context. Framed as context."""
542
+ topic = getattr(a, "topic", "") or ""
538
543
  pool = [c for c in store.list_candidates(status="provisional", with_body=True)
539
- if _within_project(c.get("cwd", ""), a.project)]
544
+ if _within_project(c.get("cwd", ""), a.project)
545
+ and store.matches_topic(c, topic)]
540
546
  full = bool(getattr(a, "full", False))
541
547
  recs = []
542
548
  served = []
@@ -721,6 +727,9 @@ def main(argv=None) -> int:
721
727
  ij.add_argument("--query", default="",
722
728
  help="rank provisional records by overlap with this text "
723
729
  "(the live request); newest first when omitted")
730
+ ij.add_argument("--topic", default="",
731
+ help="only records tagged with this topic (matches the record's "
732
+ "tags field; records with no tags never match)")
724
733
  ij.add_argument("--full", action="store_true",
725
734
  help="print each record's whole body instead of the index line")
726
735
  ij.set_defaults(fn=cmd_inject)
@@ -15,7 +15,7 @@ from datetime import datetime
15
15
 
16
16
  from . import llm, paths, safety, store
17
17
 
18
- _HDR = re.compile(r"^(source|kind|trust|evidence)\s*:\s*(.+)$", re.M)
18
+ _HDR = re.compile(r"^(source|kind|trust|tags|event_at|evidence)\s*:\s*(.+)$", re.M)
19
19
 
20
20
 
21
21
  def pending_dir() -> str:
@@ -190,7 +190,8 @@ def distill(session: str | None = None, model: str | None = None,
190
190
 
191
191
  def parse_proposal(text: str) -> dict:
192
192
  """Split one proposal into header fields + body. Headers are optional."""
193
- fields = {"source": "agent", "kind": "fact", "trust": "observed", "evidence": ""}
193
+ fields = {"source": "agent", "kind": "fact", "trust": "observed",
194
+ "tags": "", "event_at": "", "evidence": ""}
194
195
  for m in _HDR.finditer(text):
195
196
  fields[m.group(1)] = m.group(2).strip()
196
197
  body = _HDR.sub("", text).strip()
@@ -204,7 +205,7 @@ def parse_proposal(text: str) -> dict:
204
205
  # fallback. Neither decides what becomes memory: every block still goes through
205
206
  # ingest_records(), which is where the gates live.
206
207
  _SEP_LINE = re.compile(r"^[^A-Za-z0-9\n]*PROPOSAL[^A-Za-z0-9\n]*$", re.I | re.M)
207
- _HDR_LINE = re.compile(r"^(source|kind|trust|evidence)\s*:.*$", re.M)
208
+ _HDR_LINE = re.compile(r"^(source|kind|trust|tags|event_at|evidence)\s*:.*$", re.M)
208
209
 
209
210
 
210
211
  def split_blocks(text: str) -> tuple:
@@ -306,7 +307,14 @@ def ingest_records(parsed: list, session: str = "", cwd: str = "",
306
307
  item["body"], kind=f.get("kind", "fact"), source=source, trust=trust,
307
308
  session=session or os.environ.get("SLEEP_SESSION", ""),
308
309
  cwd=cwd or os.getcwd(),
309
- evidence=[x for x in f.get("evidence", "").split(",") if x])
310
+ evidence=[x for x in f.get("evidence", "").split(",") if x],
311
+ # The proposal's own tag line is a hint, never authority: store.add_candidate
312
+ # normalizes and caps it, and a wrong tag only costs a bad topic filter.
313
+ tags=[x for x in str(f.get("tags", "")).split("|") if x.strip()],
314
+ # The model may state when the described thing happened. add_candidate
315
+ # validates it and keeps the two clocks apart; a date the record does not
316
+ # state stays empty rather than being backfilled from the capture time.
317
+ event_at=f.get("event_at", ""))
310
318
  (accepted if ok else refused).append({"body": item["body"][:60], "result": out})
311
319
  return {"proposals": len(parsed), "accepted": len(accepted), "refused": refused,
312
320
  "accepted_ids": [a["result"] for a in accepted]}
package/sleepkit/store.py CHANGED
@@ -53,7 +53,7 @@ IDX_END = "<!-- sleep:end -->"
53
53
 
54
54
  # ------------------------------------------------------------- frontmatter --
55
55
  def emit(record: dict, body: str = "") -> str:
56
- order = ["id", "ts", "source", "trust", "kind", "status", "session", "cwd",
56
+ order = ["id", "ts", "event_at", "source", "trust", "kind", "status", "session", "cwd",
57
57
  "tags", "evidence", "supersedes", "superseded_by",
58
58
  "valid_from", "valid_until", "redacted", "notes"]
59
59
  # Known fields first in a stable order, then anything else the record carries.
@@ -324,7 +324,8 @@ def record_injection(ids: list[str]) -> int:
324
324
  def add_candidate(text: str, kind: str = "fact", source: str = "agent",
325
325
  trust: str = "observed", session: str = "", cwd: str = "",
326
326
  evidence: list[str] | None = None, supersedes: list[str] | None = None,
327
- notes: str = "") -> tuple[bool, str]:
327
+ notes: str = "", tags: list[str] | None = None,
328
+ event_at: str = "") -> tuple[bool, str]:
328
329
  """T1: distilled, provisional. Never injected as a directive."""
329
330
  text = (text or "").strip()
330
331
  if not text:
@@ -360,16 +361,22 @@ def add_candidate(text: str, kind: str = "fact", source: str = "agent",
360
361
  return False, "duplicate of earlier refusal %s (%.2f); this idea was judged and rejected" % (
361
362
  ref_id, ref_score)
362
363
 
364
+ ts = paths.now()
365
+ # Trust the caller's own statement when it gives one; otherwise read a date out of
366
+ # the record's own wording. Never invent: an empty event_at means the record does
367
+ # not say when the described thing happened, which is a different fact from ts.
368
+ event_at = _valid_event_at(event_at, ts) or stated_event_at(red, ts)
363
369
  rec = {
364
370
  "id": paths.new_id("c"),
365
- "ts": paths.now(),
371
+ "ts": ts,
366
372
  "source": source if source in safety.SOURCES else "inferred",
367
373
  "trust": safety.cap_trust(source, trust),
368
374
  "kind": kind if kind in KINDS else "fact",
369
375
  "status": "provisional",
376
+ "event_at": event_at,
370
377
  "session": session or os.environ.get("SLEEP_SESSION", ""),
371
378
  "cwd": cwd or os.getcwd(),
372
- "tags": "|".join([]),
379
+ "tags": "|".join(_clean_tags(tags)),
373
380
  "evidence": "|".join(evidence or []),
374
381
  "supersedes": "|".join(supersedes or []),
375
382
  "superseded_by": "",
@@ -817,6 +824,77 @@ def prune_buffer(older_than_hours: float = 0.0, session: str | None = None) -> d
817
824
  return {"moved": moved, "remaining": buffer_counts()}
818
825
 
819
826
 
827
+ # Tags are the only index a record carries besides its project, so they are kept
828
+ # machine-shaped: lowercase, no spaces, bounded in number and length. A model may
829
+ # propose them, which is exactly why they are normalized here rather than trusted.
830
+ MAX_TAGS = 6
831
+ MAX_TAG_CHARS = 32
832
+
833
+
834
+ # The two clocks are separate: `ts` is when the record was written (always set), and
835
+ # `event_at` is when the thing being described actually happened (only set when the
836
+ # record states it). Backfilling event_at from ts would manufacture a fact nobody
837
+ # recorded, and every later "where was X in October" question would answer with the
838
+ # capture date. Empty means unknown, and unknown is reported as unknown.
839
+ _DATE_RE = re.compile(r"\b(\d{4}-\d{2}-\d{2})\b")
840
+
841
+
842
+ def _valid_event_at(value: str, ts: str = "") -> str:
843
+ """Accept a caller-stated event date only in YYYY-MM-DD form, never the capture day."""
844
+ m = _DATE_RE.fullmatch(str(value or "").strip())
845
+ if not m:
846
+ return ""
847
+ if ts and m.group(1) in ts[:10]:
848
+ return ""
849
+ return m.group(1)
850
+
851
+
852
+ def stated_event_at(text: str, fallback_ts: str = "") -> str:
853
+ """Return a date the record's own wording states, else "".
854
+
855
+ Deliberately a literal match, not an inference: a relative phrasing ("last week")
856
+ is left to the model that reads the record, and this only accepts a date the text
857
+ spells out. A date equal to the capture day is not an event time either - it is
858
+ the same clock, and writing it twice would invent a distinction.
859
+ """
860
+ m = _DATE_RE.search(text or "")
861
+ if not m:
862
+ return ""
863
+ value = m.group(1)
864
+ if fallback_ts and value in fallback_ts[:10]:
865
+ return ""
866
+ return value
867
+
868
+
869
+ def _clean_tags(tags) -> list:
870
+ out, seen = [], set()
871
+ for raw in tags or []:
872
+ tag = re.sub(r"[^a-z0-9._:+-]+", "-", str(raw).strip().lower()).strip("-")
873
+ if not tag or tag in seen:
874
+ continue
875
+ seen.add(tag)
876
+ out.append(tag[:MAX_TAG_CHARS])
877
+ if len(out) >= MAX_TAGS:
878
+ break
879
+ return out
880
+
881
+
882
+ def matches_topic(meta: dict, topic: str) -> bool:
883
+ """True when a record carries the topic as one of its tags (substring, either way).
884
+
885
+ Deliberately tag-only: matching the body would turn a topic filter into a keyword
886
+ search, which is what --query already does. Substring in both directions so
887
+ `deploy` selects `deploy` and `deployment`, not `predeployed`."""
888
+ want = str(topic or "").strip().lower()
889
+ if not want:
890
+ return True
891
+ for tag in meta.get("tags") or []:
892
+ tag = str(tag).strip().lower()
893
+ if want == tag or want in tag or tag in want:
894
+ return True
895
+ return False
896
+
897
+
820
898
  # A cheap pre-filter for the LLM adjudicator. Token Jaccard is a poor *decider* on
821
899
  # short observations (measured: no real pair in a live 11-record pool cleared 0.5),
822
900
  # but it is a fine *shortlist*: pairs that share at least a handful of content words