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 +1 -1
- package/sleepkit/consolidate.py +79 -23
package/package.json
CHANGED
package/sleepkit/consolidate.py
CHANGED
|
@@ -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
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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": ""}
|