pi-sleep-agent 0.1.1 → 0.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-sleep-agent",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "A reviewable sleep and dream cycle for Pi agent memory.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -72,27 +72,34 @@ def buffer_cwd(records: list) -> str:
72
72
  return addresses[0][0] if addresses else ""
73
73
 
74
74
 
75
- def distill(session: str | None = None, model: str | None = None,
76
- budget: float = 0.5, prompt_file: str | None = None,
77
- dry_run: bool = False) -> dict:
78
- model = llm.resolve_model(model)
79
- recs = store.read_buffer(session)
80
- if not recs:
81
- return {"ok": True, "reason": "buffer empty", "proposals": 0, "accepted": 0,
82
- "consumed": False}
83
- projects = buffer_projects(recs)
84
- if len(projects) > 1:
85
- return {"ok": False, "reason": "mixed-project buffer; consolidate one session at a time",
86
- "projects": projects, "buffer_records": len(recs), "proposals": 0,
87
- "accepted": 0, "refused": [], "accepted_ids": [], "llm_exit": None,
88
- "consumed": False}
89
- if dry_run:
90
- return {"ok": True, "dry_run": True, "buffer_records": len(recs),
91
- "would_send": _fmt_buffer(recs)[:2000]}
92
- # One address for the whole batch, taken from the observations and not from this
93
- # process: filing a candidate at the consolidator's own cwd files it where no
94
- # session that produced it will ever look. See buffer_cwd().
95
- origin = buffer_cwd(recs)
75
+ def _project_groups(records: list) -> tuple:
76
+ """Split the buffer into (projects, unaddressed).
77
+
78
+ Each project is (recorded_cwd, [records]): records are grouped by their
79
+ canonical project address, but the recorded spelling is kept for filing,
80
+ because on macOS realpath adds a `/private` prefix that would otherwise make
81
+ the address unreachable. Records with no cwd (or a root cwd) carry no project
82
+ address and are returned separately: they cannot be filed somewhere a session
83
+ will look, so they are left in the buffer rather than dangerously filed.
84
+ """
85
+ groups = {}
86
+ unaddressed = []
87
+ for rec in records:
88
+ cwd = (rec.get("cwd") or "").strip()
89
+ if not cwd or os.path.abspath(cwd) == os.path.sep:
90
+ unaddressed.append(rec)
91
+ continue
92
+ address = os.path.normpath(os.path.abspath(cwd))
93
+ key = os.path.realpath(os.path.abspath(cwd))
94
+ if key not in groups:
95
+ groups[key] = (address, [])
96
+ groups[key][1].append(rec)
97
+ return list(groups.values()), unaddressed
98
+
99
+
100
+ def _consolidate_project(recs, origin, session, model, budget, prompt_file) -> dict:
101
+ """One consolidation pass over a single project's records: the model proposes,
102
+ Python gates, and candidates are filed under that project's recorded cwd."""
96
103
  pf = prompt_file or os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
97
104
  "prompts", "consolidate.md")
98
105
  user = (
@@ -124,14 +131,63 @@ def distill(session: str | None = None, model: str | None = None,
124
131
  # The tail lands in state.json, where it outlives the night. Redact on the way in:
125
132
  # a model that echoes back a credential must not have it filed in plain text.
126
133
  ing["llm_tail"] = safety.redact((res["stdout"] or "")[-400:])[0]
127
- ing["buffer_records"] = len(recs)
128
- # "consumed" is what entitles a sleep to prune: the pass produced proposals.
129
134
  ing["consumed"] = bool(res["exit"] == 0 and ing["proposals"])
130
135
  if res["exit"] != 0:
131
136
  ing["ok"] = False
132
137
  return ing
133
138
 
134
139
 
140
+ def distill(session: str | None = None, model: str | None = None,
141
+ budget: float = 0.5, prompt_file: str | None = None,
142
+ dry_run: bool = False) -> dict:
143
+ model = llm.resolve_model(model)
144
+ recs = store.read_buffer(session)
145
+ if not recs:
146
+ return {"ok": True, "reason": "buffer empty", "proposals": 0, "accepted": 0,
147
+ "consumed": False}
148
+ projects, unaddressed = _project_groups(recs)
149
+ if dry_run:
150
+ return {"ok": True, "dry_run": True, "buffer_records": len(recs),
151
+ "projects": [o for o, _ in projects], "unaddressed": len(unaddressed),
152
+ "would_send": _fmt_buffer(recs)[:2000]}
153
+ if not projects:
154
+ return {"ok": False, "reason": "no project address in buffer",
155
+ "buffer_records": len(recs), "proposals": 0, "accepted": 0,
156
+ "refused": [], "accepted_ids": [], "llm_exit": None, "consumed": False}
157
+ # One consolidation pass per project, each filed under that project's own recorded
158
+ # cwd. A mixed buffer used to fail closed because a single `origin` cannot be both
159
+ # projects; running per-project lets each project's truths land where their own
160
+ # sessions will look, and unaddressed records stay in the buffer (never pruned).
161
+ if len(projects) == 1:
162
+ ing = _consolidate_project(projects[0][1], projects[0][0], session,
163
+ model, budget, prompt_file)
164
+ else:
165
+ ing = {"ok": True, "proposals": 0, "accepted": 0, "refused": [],
166
+ "accepted_ids": [], "llm_exit": None, "consumed": False,
167
+ "per_project": []}
168
+ any_bad = False
169
+ for origin, recs_proj in projects:
170
+ sub = _consolidate_project(recs_proj, origin, session, model, budget,
171
+ prompt_file)
172
+ sub["origin"] = origin
173
+ sub["buffer_records"] = len(recs_proj)
174
+ ing["per_project"].append(sub)
175
+ ing["proposals"] += sub["proposals"]
176
+ ing["accepted"] += sub["accepted"]
177
+ ing["refused"] += sub["refused"]
178
+ ing["accepted_ids"] += sub["accepted_ids"]
179
+ if sub.get("llm_exit") != 0:
180
+ any_bad = True
181
+ ing["consumed"] = ing["consumed"] or sub["consumed"]
182
+ if any_bad:
183
+ ing["ok"] = False
184
+ ing["buffer_records"] = len(recs)
185
+ ing["unaddressed"] = len(unaddressed)
186
+ # "consumed" is what entitles a sleep to prune: the pass produced proposals.
187
+ # Unaddressed records were never read by any pass, so they must not be pruned.
188
+ return ing
189
+
190
+
135
191
  def parse_proposal(text: str) -> dict:
136
192
  """Split one proposal into header fields + body. Headers are optional."""
137
193
  fields = {"source": "agent", "kind": "fact", "trust": "observed", "evidence": ""}