robert-github-agent 0.1.0b1__py3-none-any.whl
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.
- robert_agent/__init__.py +1 -0
- robert_agent/acceptance.py +394 -0
- robert_agent/acceptance_suite.py +287 -0
- robert_agent/audit_result.py +501 -0
- robert_agent/authorize.py +117 -0
- robert_agent/board.py +538 -0
- robert_agent/chat_status.py +338 -0
- robert_agent/cli/__init__.py +1 -0
- robert_agent/cli/exit_codes.py +5 -0
- robert_agent/cli/main.py +535 -0
- robert_agent/cli/output.py +20 -0
- robert_agent/common.py +6 -0
- robert_agent/controlled_e2e_acceptance.py +394 -0
- robert_agent/daemon.py +607 -0
- robert_agent/daemon_state.py +319 -0
- robert_agent/diagnostics.py +119 -0
- robert_agent/discover.py +775 -0
- robert_agent/dispatch.py +217 -0
- robert_agent/doctor.py +94 -0
- robert_agent/github_api.py +12 -0
- robert_agent/init_config.py +126 -0
- robert_agent/integrations/__init__.py +1 -0
- robert_agent/integrations/openclaw.py +285 -0
- robert_agent/live_discovery_acceptance.py +70 -0
- robert_agent/live_worker_acceptance.py +363 -0
- robert_agent/live_worktree_acceptance.py +344 -0
- robert_agent/loop_engine.py +285 -0
- robert_agent/memory_curator.py +105 -0
- robert_agent/migrate.py +157 -0
- robert_agent/notify.py +74 -0
- robert_agent/paths.py +16 -0
- robert_agent/production_write_canary.py +254 -0
- robert_agent/project_memory.py +503 -0
- robert_agent/publish.py +743 -0
- robert_agent/publish_dedupe_acceptance.py +270 -0
- robert_agent/redaction.py +41 -0
- robert_agent/render_prompt.py +544 -0
- robert_agent/resource_files.py +8 -0
- robert_agent/resources/__init__.py +1 -0
- robert_agent/resources/acceptance-matrix.md +120 -0
- robert_agent/resources/classifier.md +19 -0
- robert_agent/resources/command-matrix.md +38 -0
- robert_agent/resources/config.example.yml +20 -0
- robert_agent/resources/db/migrations/001_legacy_dd_to_robert.sql +14 -0
- robert_agent/resources/db/schema.sql +479 -0
- robert_agent/resources/failure-modes.md +36 -0
- robert_agent/resources/fixtures/actor-permission.json +1 -0
- robert_agent/resources/fixtures/discovery.json +1 -0
- robert_agent/resources/fixtures/redaction.json +1 -0
- robert_agent/resources/fixtures/review-followup.json +1 -0
- robert_agent/resources/fixtures/worker-results.json +1 -0
- robert_agent/resources/fixtures/workstreams.json +1 -0
- robert_agent/resources/github-events.md +77 -0
- robert_agent/resources/kanban-control.md +165 -0
- robert_agent/resources/redaction.md +29 -0
- robert_agent/resources/result-contract.md +56 -0
- robert_agent/resources/routes.yml +82 -0
- robert_agent/resources/schemas/config.schema.json +123 -0
- robert_agent/resources/schemas/event.schema.json +18 -0
- robert_agent/resources/schemas/result.schema.json +99 -0
- robert_agent/resources/schemas/route.schema.json +23 -0
- robert_agent/resources/schemas/snapshot.schema.json +17 -0
- robert_agent/resources/schemas/task.schema.json +15 -0
- robert_agent/resources/trust-model.md +16 -0
- robert_agent/resources/worker-prompt.md +121 -0
- robert_agent/resources/worker-protocol.md +47 -0
- robert_agent/resources/workflow.yml +50 -0
- robert_agent/resources/workstream-model.md +17 -0
- robert_agent/route.py +232 -0
- robert_agent/route_config.py +64 -0
- robert_agent/run_once.py +5375 -0
- robert_agent/runtime_knowledge.py +392 -0
- robert_agent/service.py +161 -0
- robert_agent/skills.py +28 -0
- robert_agent/status.py +1051 -0
- robert_agent/storage.py +376 -0
- robert_agent/summarize.py +100 -0
- robert_agent/supervise.py +72 -0
- robert_agent/usage.py +192 -0
- robert_agent/validate_config.py +616 -0
- robert_agent/wakeup.py +261 -0
- robert_agent/web.py +2893 -0
- robert_agent/web_assets/board.css +255 -0
- robert_agent/web_assets/board.html +165 -0
- robert_agent/web_assets/board.js +660 -0
- robert_agent/web_assets/github-shell.css +122 -0
- robert_agent/web_assets/index.html +103 -0
- robert_agent/web_assets/operations.css +168 -0
- robert_agent/web_assets/workbench.css +131 -0
- robert_agent/web_assets/workbench.js +581 -0
- robert_agent/work_items.py +1016 -0
- robert_agent/workbench.py +701 -0
- robert_agent/worker/__init__.py +1 -0
- robert_agent/worker/heartbeat.py +190 -0
- robert_agent/worker/result.py +321 -0
- robert_agent/worker/snapshot.py +98 -0
- robert_agent/worker_adapters/__init__.py +138 -0
- robert_agent/worker_adapters/cbc.py +68 -0
- robert_agent/worker_adapters/codex.py +56 -0
- robert_agent/worker_adapters/command.py +33 -0
- robert_agent/worker_adapters/tcodex.py +56 -0
- robert_agent/worker_heartbeat.py +6 -0
- robert_agent/worker_result.py +6 -0
- robert_agent/worker_snapshot.py +6 -0
- robert_agent/workstream.py +55 -0
- robert_agent/worktree.py +297 -0
- robert_github_agent-0.1.0b1.dist-info/METADATA +217 -0
- robert_github_agent-0.1.0b1.dist-info/RECORD +111 -0
- robert_github_agent-0.1.0b1.dist-info/WHEEL +4 -0
- robert_github_agent-0.1.0b1.dist-info/entry_points.txt +2 -0
- robert_github_agent-0.1.0b1.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
"""Project-memory helpers for DD GitHub agent tasks."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import re
|
|
5
|
+
import sqlite3
|
|
6
|
+
from uuid import uuid4
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
CONFIDENCE_SCORES = {"high": 6, "medium": 3, "low": 1}
|
|
10
|
+
TERM_WEIGHTS = {
|
|
11
|
+
"memory_thread_key": 50,
|
|
12
|
+
"workstream_id": 40,
|
|
13
|
+
"route_id": 16,
|
|
14
|
+
"expected_output": 16,
|
|
15
|
+
"path": 12,
|
|
16
|
+
"symbol": 12,
|
|
17
|
+
"keyword": 10,
|
|
18
|
+
"kind": 4,
|
|
19
|
+
}
|
|
20
|
+
TOKEN_RE = re.compile(r"[A-Za-z0-9_./#!-]+")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _id(prefix):
|
|
24
|
+
return f"{prefix}-{uuid4().hex[:12]}"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _json_list(value):
|
|
28
|
+
return value if isinstance(value, list) else []
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _text(value):
|
|
32
|
+
return value.strip() if isinstance(value, str) else ""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _lower_list(value):
|
|
36
|
+
terms = []
|
|
37
|
+
for item in _json_list(value):
|
|
38
|
+
if isinstance(item, str) and item.strip():
|
|
39
|
+
terms.append(item.strip().lower())
|
|
40
|
+
return terms
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _terms_from_text(value):
|
|
44
|
+
terms = set()
|
|
45
|
+
for token in TOKEN_RE.findall(value or ""):
|
|
46
|
+
normalized = token.strip(".,:;()[]{}<>\"'`").lower()
|
|
47
|
+
if not normalized:
|
|
48
|
+
continue
|
|
49
|
+
terms.add(normalized)
|
|
50
|
+
if "-" in normalized:
|
|
51
|
+
terms.update(part for part in normalized.split("-") if part)
|
|
52
|
+
if "/" in normalized:
|
|
53
|
+
terms.update(part for part in normalized.split("/") if part)
|
|
54
|
+
return terms
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _entry_terms(entry, payload, workstream_id, route_result=None):
|
|
58
|
+
route_result = route_result or {}
|
|
59
|
+
terms = {
|
|
60
|
+
"memory_thread_key": [entry["memory_thread_key"]],
|
|
61
|
+
"workstream_id": [workstream_id],
|
|
62
|
+
"kind": [entry["kind"]],
|
|
63
|
+
"expected_output": [
|
|
64
|
+
_text(route_result.get("expected_output")) or _text(payload.get("output_type"))
|
|
65
|
+
],
|
|
66
|
+
"route_id": [_text(route_result.get("route_id"))],
|
|
67
|
+
"path": _lower_list(entry.get("paths")),
|
|
68
|
+
"symbol": _lower_list(entry.get("symbols")),
|
|
69
|
+
"keyword": _lower_list(entry.get("keywords")),
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
term_type: [term for term in values if term]
|
|
73
|
+
for term_type, values in terms.items()
|
|
74
|
+
if any(values)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _query_terms(route_result, events):
|
|
79
|
+
route_result = route_result or {}
|
|
80
|
+
terms = set()
|
|
81
|
+
for key in ["route_id", "expected_output"]:
|
|
82
|
+
value = _text(route_result.get(key))
|
|
83
|
+
if value:
|
|
84
|
+
terms.add(value.lower())
|
|
85
|
+
terms.update(_terms_from_text(value))
|
|
86
|
+
for event in events or []:
|
|
87
|
+
for key in ["title", "body", "intent", "source_key", "source_type"]:
|
|
88
|
+
terms.update(_terms_from_text(_text(event.get(key))))
|
|
89
|
+
metadata = event.get("metadata") if isinstance(event, dict) else {}
|
|
90
|
+
if isinstance(metadata, dict):
|
|
91
|
+
for value in metadata.values():
|
|
92
|
+
if isinstance(value, str):
|
|
93
|
+
terms.update(_terms_from_text(value))
|
|
94
|
+
elif isinstance(value, dict):
|
|
95
|
+
for nested in value.values():
|
|
96
|
+
if isinstance(nested, str):
|
|
97
|
+
terms.update(_terms_from_text(nested))
|
|
98
|
+
return terms
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _runtime_boost_terms(runtime_knowledge):
|
|
102
|
+
terms = set()
|
|
103
|
+
for knowledge in runtime_knowledge or []:
|
|
104
|
+
if not isinstance(knowledge, dict):
|
|
105
|
+
continue
|
|
106
|
+
boost = knowledge.get("retrieval_boost") or {}
|
|
107
|
+
if not isinstance(boost, dict):
|
|
108
|
+
continue
|
|
109
|
+
for key in ["keywords", "paths", "symbols", "terms"]:
|
|
110
|
+
for value in _json_list(boost.get(key)):
|
|
111
|
+
if isinstance(value, str) and value.strip():
|
|
112
|
+
terms.add(value.strip().lower())
|
|
113
|
+
terms.update(_terms_from_text(value))
|
|
114
|
+
return terms
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _thread_key(payload, workstream_id):
|
|
118
|
+
explicit = _text(payload.get("memory_thread_key"))
|
|
119
|
+
if explicit:
|
|
120
|
+
return explicit
|
|
121
|
+
for action in payload.get("planned_github_actions") or []:
|
|
122
|
+
if not isinstance(action, dict) or action.get("type") != "open_pr":
|
|
123
|
+
continue
|
|
124
|
+
url = _text(action.get("url")) or _text(action.get("target_url"))
|
|
125
|
+
match = re.search(r"/pull/(\d+)/?$", url)
|
|
126
|
+
if not match:
|
|
127
|
+
continue
|
|
128
|
+
if workstream_id.startswith("github:") and "#" in workstream_id:
|
|
129
|
+
repo_name = workstream_id[len("github:") :].split("#", 1)[0]
|
|
130
|
+
return f"github:{repo_name}!{match.group(1)}"
|
|
131
|
+
return workstream_id
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _normalize_entry(raw_entry, payload, workstream_id):
|
|
135
|
+
if not isinstance(raw_entry, dict):
|
|
136
|
+
return None, "entry must be an object"
|
|
137
|
+
operation = _text(raw_entry.get("operation")) or "upsert"
|
|
138
|
+
if operation != "upsert":
|
|
139
|
+
return None, f"unsupported memory operation: {operation}"
|
|
140
|
+
title = _text(raw_entry.get("title"))
|
|
141
|
+
short_summary = _text(raw_entry.get("short_summary"))
|
|
142
|
+
if not title or not short_summary:
|
|
143
|
+
return None, "memory entry requires title and short_summary"
|
|
144
|
+
confidence = _text(raw_entry.get("confidence")) or "medium"
|
|
145
|
+
if confidence not in CONFIDENCE_SCORES:
|
|
146
|
+
confidence = "medium"
|
|
147
|
+
entry = {
|
|
148
|
+
"operation": operation,
|
|
149
|
+
"kind": _text(raw_entry.get("kind")) or "context",
|
|
150
|
+
"title": title,
|
|
151
|
+
"short_summary": short_summary,
|
|
152
|
+
"long_summary": _text(raw_entry.get("long_summary")),
|
|
153
|
+
"paths": _lower_list(raw_entry.get("paths")),
|
|
154
|
+
"symbols": _lower_list(raw_entry.get("symbols")),
|
|
155
|
+
"keywords": _lower_list(raw_entry.get("keywords")),
|
|
156
|
+
"evidence": _json_list(raw_entry.get("evidence")),
|
|
157
|
+
"confidence": confidence,
|
|
158
|
+
"memory_thread_key": _text(raw_entry.get("memory_thread_key"))
|
|
159
|
+
or _thread_key(payload, workstream_id),
|
|
160
|
+
}
|
|
161
|
+
return entry, None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _fts_exists(conn):
|
|
165
|
+
row = conn.execute(
|
|
166
|
+
"""
|
|
167
|
+
SELECT 1
|
|
168
|
+
FROM sqlite_master
|
|
169
|
+
WHERE name = 'project_memory_fts'
|
|
170
|
+
AND type = 'table'
|
|
171
|
+
LIMIT 1
|
|
172
|
+
"""
|
|
173
|
+
).fetchone()
|
|
174
|
+
return bool(row)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _sync_fts(conn, memory_id, repo_id, entry):
|
|
178
|
+
if not _fts_exists(conn):
|
|
179
|
+
return
|
|
180
|
+
try:
|
|
181
|
+
conn.execute("DELETE FROM project_memory_fts WHERE memory_id = ?", (memory_id,))
|
|
182
|
+
conn.execute(
|
|
183
|
+
"""
|
|
184
|
+
INSERT INTO project_memory_fts(
|
|
185
|
+
memory_id, repo_id, title, short_summary, long_summary, paths, symbols, keywords
|
|
186
|
+
)
|
|
187
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
188
|
+
""",
|
|
189
|
+
(
|
|
190
|
+
memory_id,
|
|
191
|
+
repo_id,
|
|
192
|
+
entry["title"],
|
|
193
|
+
entry["short_summary"],
|
|
194
|
+
entry["long_summary"],
|
|
195
|
+
" ".join(entry["paths"]),
|
|
196
|
+
" ".join(entry["symbols"]),
|
|
197
|
+
" ".join(entry["keywords"]),
|
|
198
|
+
),
|
|
199
|
+
)
|
|
200
|
+
except sqlite3.OperationalError:
|
|
201
|
+
return
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _replace_terms(conn, memory_id, repo_id, terms_by_type, run_now):
|
|
205
|
+
conn.execute("DELETE FROM project_memory_terms WHERE memory_id = ?", (memory_id,))
|
|
206
|
+
for term_type, terms in terms_by_type.items():
|
|
207
|
+
for term in sorted(set(terms)):
|
|
208
|
+
conn.execute(
|
|
209
|
+
"""
|
|
210
|
+
INSERT OR IGNORE INTO project_memory_terms(
|
|
211
|
+
memory_id, repo_id, term_type, term_value, created_at
|
|
212
|
+
)
|
|
213
|
+
VALUES (?, ?, ?, ?, ?)
|
|
214
|
+
""",
|
|
215
|
+
(memory_id, repo_id, term_type, term, run_now),
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _upsert_entry(conn, payload, repo_id, workstream_id, raw_entry, run_now):
|
|
220
|
+
entry, error = _normalize_entry(raw_entry, payload, workstream_id)
|
|
221
|
+
if error:
|
|
222
|
+
return None, error
|
|
223
|
+
row = conn.execute(
|
|
224
|
+
"""
|
|
225
|
+
SELECT memory_id, revision_count
|
|
226
|
+
FROM project_memory_entries
|
|
227
|
+
WHERE repo_id = ?
|
|
228
|
+
AND memory_thread_key = ?
|
|
229
|
+
AND kind = ?
|
|
230
|
+
AND title = ?
|
|
231
|
+
""",
|
|
232
|
+
(repo_id, entry["memory_thread_key"], entry["kind"], entry["title"]),
|
|
233
|
+
).fetchone()
|
|
234
|
+
if row:
|
|
235
|
+
memory_id, revision_count = row
|
|
236
|
+
else:
|
|
237
|
+
memory_id = _id("pmem")
|
|
238
|
+
revision_count = 0
|
|
239
|
+
conn.execute(
|
|
240
|
+
"""
|
|
241
|
+
INSERT INTO project_memory_entries(
|
|
242
|
+
memory_id, repo_id, memory_thread_key, kind, title, short_summary,
|
|
243
|
+
long_summary, confidence, source_task_id, source_result_id,
|
|
244
|
+
revision_count, created_at, updated_at, metadata_json
|
|
245
|
+
)
|
|
246
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
|
|
247
|
+
""",
|
|
248
|
+
(
|
|
249
|
+
memory_id,
|
|
250
|
+
repo_id,
|
|
251
|
+
entry["memory_thread_key"],
|
|
252
|
+
entry["kind"],
|
|
253
|
+
entry["title"],
|
|
254
|
+
entry["short_summary"],
|
|
255
|
+
entry["long_summary"],
|
|
256
|
+
entry["confidence"],
|
|
257
|
+
payload.get("task_id"),
|
|
258
|
+
payload.get("result_id"),
|
|
259
|
+
run_now,
|
|
260
|
+
run_now,
|
|
261
|
+
json.dumps(
|
|
262
|
+
{
|
|
263
|
+
"paths": entry["paths"],
|
|
264
|
+
"symbols": entry["symbols"],
|
|
265
|
+
"keywords": entry["keywords"],
|
|
266
|
+
"evidence": entry["evidence"],
|
|
267
|
+
},
|
|
268
|
+
ensure_ascii=False,
|
|
269
|
+
sort_keys=True,
|
|
270
|
+
),
|
|
271
|
+
),
|
|
272
|
+
)
|
|
273
|
+
revision_id = _id("pmemrev")
|
|
274
|
+
revision_payload = dict(entry)
|
|
275
|
+
revision_payload["result_id"] = payload.get("result_id")
|
|
276
|
+
revision_payload["task_id"] = payload.get("task_id")
|
|
277
|
+
revision_payload["attempt_id"] = payload.get("attempt_id")
|
|
278
|
+
conn.execute(
|
|
279
|
+
"""
|
|
280
|
+
INSERT INTO project_memory_revisions(
|
|
281
|
+
revision_id, memory_id, repo_id, result_id, task_id, attempt_id,
|
|
282
|
+
operation, title, short_summary, long_summary, payload_json, created_at
|
|
283
|
+
)
|
|
284
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
285
|
+
""",
|
|
286
|
+
(
|
|
287
|
+
revision_id,
|
|
288
|
+
memory_id,
|
|
289
|
+
repo_id,
|
|
290
|
+
payload.get("result_id"),
|
|
291
|
+
payload.get("task_id"),
|
|
292
|
+
payload.get("attempt_id"),
|
|
293
|
+
entry["operation"],
|
|
294
|
+
entry["title"],
|
|
295
|
+
entry["short_summary"],
|
|
296
|
+
entry["long_summary"],
|
|
297
|
+
json.dumps(revision_payload, ensure_ascii=False, sort_keys=True),
|
|
298
|
+
run_now,
|
|
299
|
+
),
|
|
300
|
+
)
|
|
301
|
+
conn.execute(
|
|
302
|
+
"""
|
|
303
|
+
UPDATE project_memory_entries
|
|
304
|
+
SET short_summary = ?,
|
|
305
|
+
long_summary = ?,
|
|
306
|
+
confidence = ?,
|
|
307
|
+
source_task_id = ?,
|
|
308
|
+
source_result_id = ?,
|
|
309
|
+
current_revision_id = ?,
|
|
310
|
+
revision_count = ?,
|
|
311
|
+
updated_at = ?,
|
|
312
|
+
metadata_json = ?
|
|
313
|
+
WHERE memory_id = ?
|
|
314
|
+
""",
|
|
315
|
+
(
|
|
316
|
+
entry["short_summary"],
|
|
317
|
+
entry["long_summary"],
|
|
318
|
+
entry["confidence"],
|
|
319
|
+
payload.get("task_id"),
|
|
320
|
+
payload.get("result_id"),
|
|
321
|
+
revision_id,
|
|
322
|
+
revision_count + 1,
|
|
323
|
+
run_now,
|
|
324
|
+
json.dumps(
|
|
325
|
+
{
|
|
326
|
+
"paths": entry["paths"],
|
|
327
|
+
"symbols": entry["symbols"],
|
|
328
|
+
"keywords": entry["keywords"],
|
|
329
|
+
"evidence": entry["evidence"],
|
|
330
|
+
},
|
|
331
|
+
ensure_ascii=False,
|
|
332
|
+
sort_keys=True,
|
|
333
|
+
),
|
|
334
|
+
memory_id,
|
|
335
|
+
),
|
|
336
|
+
)
|
|
337
|
+
_replace_terms(
|
|
338
|
+
conn,
|
|
339
|
+
memory_id,
|
|
340
|
+
repo_id,
|
|
341
|
+
_entry_terms(entry, payload, workstream_id),
|
|
342
|
+
run_now,
|
|
343
|
+
)
|
|
344
|
+
_sync_fts(conn, memory_id, repo_id, entry)
|
|
345
|
+
return memory_id, None
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def record_memory_delta(conn, result_payload, workstream_id, repo_id, run_now):
|
|
349
|
+
delta = result_payload.get("memory_delta")
|
|
350
|
+
if not isinstance(delta, dict):
|
|
351
|
+
return {"status": "skipped", "recorded_count": 0, "reason": "no_memory_delta"}
|
|
352
|
+
status = _text(delta.get("status")) or "none"
|
|
353
|
+
if status != "has_memory":
|
|
354
|
+
return {
|
|
355
|
+
"status": "skipped",
|
|
356
|
+
"recorded_count": 0,
|
|
357
|
+
"reason": _text(delta.get("reason")) or status,
|
|
358
|
+
}
|
|
359
|
+
entries = delta.get("entries")
|
|
360
|
+
if not isinstance(entries, list):
|
|
361
|
+
return {
|
|
362
|
+
"status": "skipped",
|
|
363
|
+
"recorded_count": 0,
|
|
364
|
+
"safe_error": "memory_delta.entries must be a list",
|
|
365
|
+
}
|
|
366
|
+
recorded = []
|
|
367
|
+
skipped = []
|
|
368
|
+
for raw_entry in entries:
|
|
369
|
+
memory_id, error = _upsert_entry(
|
|
370
|
+
conn,
|
|
371
|
+
result_payload,
|
|
372
|
+
repo_id,
|
|
373
|
+
workstream_id,
|
|
374
|
+
raw_entry,
|
|
375
|
+
run_now,
|
|
376
|
+
)
|
|
377
|
+
if memory_id:
|
|
378
|
+
recorded.append(memory_id)
|
|
379
|
+
else:
|
|
380
|
+
skipped.append(error)
|
|
381
|
+
if not recorded:
|
|
382
|
+
return {
|
|
383
|
+
"status": "skipped",
|
|
384
|
+
"recorded_count": 0,
|
|
385
|
+
"safe_error": "; ".join(error for error in skipped if error) or "no valid memory entries",
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
"status": "recorded",
|
|
389
|
+
"recorded_count": len(recorded),
|
|
390
|
+
"memory_ids": recorded,
|
|
391
|
+
"skipped_count": len(skipped),
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def _candidate_ids_from_terms(conn, repo_id, query_terms):
|
|
396
|
+
if not query_terms:
|
|
397
|
+
return {}
|
|
398
|
+
placeholders = ",".join("?" for _ in query_terms)
|
|
399
|
+
params = [repo_id, *sorted(query_terms)]
|
|
400
|
+
scores = {}
|
|
401
|
+
for memory_id, term_type in conn.execute(
|
|
402
|
+
f"""
|
|
403
|
+
SELECT memory_id, term_type
|
|
404
|
+
FROM project_memory_terms
|
|
405
|
+
WHERE repo_id = ?
|
|
406
|
+
AND term_value IN ({placeholders})
|
|
407
|
+
""",
|
|
408
|
+
params,
|
|
409
|
+
):
|
|
410
|
+
scores[memory_id] = scores.get(memory_id, 0) + TERM_WEIGHTS.get(term_type, 1)
|
|
411
|
+
return scores
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def _candidate_ids_from_threads(conn, repo_id, thread_keys):
|
|
415
|
+
scores = {}
|
|
416
|
+
for thread_key in sorted(key for key in thread_keys if key):
|
|
417
|
+
for (memory_id,) in conn.execute(
|
|
418
|
+
"""
|
|
419
|
+
SELECT memory_id
|
|
420
|
+
FROM project_memory_entries
|
|
421
|
+
WHERE repo_id = ?
|
|
422
|
+
AND memory_thread_key = ?
|
|
423
|
+
""",
|
|
424
|
+
(repo_id, thread_key),
|
|
425
|
+
):
|
|
426
|
+
scores[memory_id] = scores.get(memory_id, 0) + TERM_WEIGHTS["memory_thread_key"]
|
|
427
|
+
return scores
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def _thread_keys(workstream_id, events):
|
|
431
|
+
keys = {workstream_id}
|
|
432
|
+
for event in events or []:
|
|
433
|
+
for key in ["workstream_id", "origin_workstream_id"]:
|
|
434
|
+
value = _text(event.get(key)) if isinstance(event, dict) else ""
|
|
435
|
+
if value:
|
|
436
|
+
keys.add(value)
|
|
437
|
+
metadata = event.get("metadata") if isinstance(event, dict) else {}
|
|
438
|
+
if isinstance(metadata, dict):
|
|
439
|
+
dd_workstream = metadata.get("dd_workstream")
|
|
440
|
+
if isinstance(dd_workstream, dict):
|
|
441
|
+
for key in ["workstream_id", "origin_workstream_id"]:
|
|
442
|
+
value = _text(dd_workstream.get(key))
|
|
443
|
+
if value:
|
|
444
|
+
keys.add(value)
|
|
445
|
+
return keys
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
def retrieve_memories(conn, repo_id, workstream_id, route_result, events, limit=5, runtime_knowledge=None):
|
|
449
|
+
query_terms = _query_terms(route_result, events)
|
|
450
|
+
boost_terms = _runtime_boost_terms(runtime_knowledge)
|
|
451
|
+
thread_scores = _candidate_ids_from_threads(conn, repo_id, _thread_keys(workstream_id, events))
|
|
452
|
+
term_scores = _candidate_ids_from_terms(conn, repo_id, query_terms)
|
|
453
|
+
boost_scores = _candidate_ids_from_terms(conn, repo_id, boost_terms)
|
|
454
|
+
scores = dict(term_scores)
|
|
455
|
+
for memory_id, score in boost_scores.items():
|
|
456
|
+
scores[memory_id] = scores.get(memory_id, 0) + (score * 3)
|
|
457
|
+
for memory_id, score in thread_scores.items():
|
|
458
|
+
scores[memory_id] = scores.get(memory_id, 0) + score
|
|
459
|
+
if not scores:
|
|
460
|
+
return []
|
|
461
|
+
placeholders = ",".join("?" for _ in scores)
|
|
462
|
+
rows = conn.execute(
|
|
463
|
+
f"""
|
|
464
|
+
SELECT memory_id, memory_thread_key, kind, title, short_summary,
|
|
465
|
+
long_summary, confidence, updated_at, metadata_json
|
|
466
|
+
FROM project_memory_entries
|
|
467
|
+
WHERE memory_id IN ({placeholders})
|
|
468
|
+
""",
|
|
469
|
+
list(scores),
|
|
470
|
+
).fetchall()
|
|
471
|
+
memories = []
|
|
472
|
+
for row in rows:
|
|
473
|
+
(
|
|
474
|
+
memory_id,
|
|
475
|
+
memory_thread_key,
|
|
476
|
+
kind,
|
|
477
|
+
title,
|
|
478
|
+
short_summary,
|
|
479
|
+
long_summary,
|
|
480
|
+
confidence,
|
|
481
|
+
updated_at,
|
|
482
|
+
metadata_json,
|
|
483
|
+
) = row
|
|
484
|
+
metadata = json.loads(metadata_json or "{}")
|
|
485
|
+
score = scores[memory_id] + CONFIDENCE_SCORES.get(confidence, 0)
|
|
486
|
+
memories.append(
|
|
487
|
+
{
|
|
488
|
+
"memory_id": memory_id,
|
|
489
|
+
"memory_thread_key": memory_thread_key,
|
|
490
|
+
"kind": kind,
|
|
491
|
+
"title": title,
|
|
492
|
+
"short_summary": short_summary,
|
|
493
|
+
"long_summary": long_summary,
|
|
494
|
+
"confidence": confidence,
|
|
495
|
+
"paths": metadata.get("paths", []),
|
|
496
|
+
"symbols": metadata.get("symbols", []),
|
|
497
|
+
"keywords": metadata.get("keywords", []),
|
|
498
|
+
"score": score,
|
|
499
|
+
"updated_at": updated_at,
|
|
500
|
+
}
|
|
501
|
+
)
|
|
502
|
+
memories.sort(key=lambda item: (-item["score"], item["updated_at"], item["memory_id"]))
|
|
503
|
+
return memories[:limit]
|