loki-mode 11.128.2 → 11.130.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/README.md +1 -0
- package/SKILL.md +3 -3
- package/VERSION +1 -1
- package/autonomy/lib/help-core.sh +30 -42
- package/autonomy/lib/workspace.py +292 -19
- package/autonomy/lib/workspace_comment.py +40 -11
- package/autonomy/loki +129 -159
- package/autonomy/route.sh +3 -2
- package/autonomy/trigger-server.py +373 -32
- package/bin/loki +139 -18
- package/completions/_loki +11 -142
- package/completions/loki.bash +4 -2
- package/dashboard/__init__.py +1 -1
- package/docs/CLI-CORE-VERBS.md +61 -37
- package/docs/CLI-INVENTORY.md +152 -153
- package/docs/CLI-REFERENCE.md +20 -166
- package/docs/DEPLOYMENT.md +38 -3
- package/docs/INSTALLATION.md +7 -1
- package/docs/KNOWN-ISSUES.md +1 -1
- package/docs/PROOF-PASSPORT.md +21 -0
- package/docs/TESTING.md +1 -1
- package/docs/WORKSPACES.md +27 -0
- package/docs/cost-controls.md +74 -12
- package/docs/dev/architecture-reference.md +2 -2
- package/docs/environment-variables.md +7 -0
- package/docs/exit-codes.md +7 -0
- package/docs/merge-queue.md +44 -3
- package/docs/sentry-intake.md +1 -1
- package/docs/slack.md +1 -1
- package/docs/v10/GUIDE.md +33 -4
- package/loki-ts/dist/loki.js +1695 -1649
- package/mcp/__init__.py +1 -1
- package/memory/knowledge_graph.py +442 -56
- package/package.json +3 -3
- package/packages/control-plane/dist/server.js +2166 -1633
- package/plugins/loki-mode/.claude-plugin/plugin.json +1 -1
- package/schemas/loki-yaml.schema.json +1 -0
- package/web-app/dist/assets/{AdminPage-B-9a_urH.js → AdminPage-CNxvd-uy.js} +1 -1
- package/web-app/dist/assets/{Avatar-DWFQPBje.js → Avatar-DdLoiZKv.js} +1 -1
- package/web-app/dist/assets/{Badge-CuLYjGKc.js → Badge-DrdPaBOH.js} +1 -1
- package/web-app/dist/assets/{Button-Cws5TOeA.js → Button-BQ0UyQAk.js} +1 -1
- package/web-app/dist/assets/{CockpitPage-CCT1U0rq.js → CockpitPage-BLFBjBZW.js} +1 -1
- package/web-app/dist/assets/{ComparePage-ChcsGaeo.js → ComparePage-NHaUjY-d.js} +1 -1
- package/web-app/dist/assets/{ErrorBoundary-C5Mz6Y_d.js → ErrorBoundary-ClaDCylr.js} +1 -1
- package/web-app/dist/assets/{EvidenceReceiptPanel-jXRFyeG-.js → EvidenceReceiptPanel-BBW0IhXb.js} +1 -1
- package/web-app/dist/assets/{GitHubIssuesPanel-C_YjFJSB.js → GitHubIssuesPanel-B_dAtNuN.js} +1 -1
- package/web-app/dist/assets/{GitHubPRsPanel-B3bRY_lb.js → GitHubPRsPanel-Tv3JF9E0.js} +1 -1
- package/web-app/dist/assets/{HomePage-JLwBDYu8.js → HomePage-8fLInImK.js} +2 -2
- package/web-app/dist/assets/{LoginPage-BcDvWJr1.js → LoginPage-BGL-xm0l.js} +1 -1
- package/web-app/dist/assets/{MagicPage-Bj3qzygY.js → MagicPage-D7nHpNuT.js} +1 -1
- package/web-app/dist/assets/{MetricsPage-9k0NVtge.js → MetricsPage-BZh8ZOug.js} +1 -1
- package/web-app/dist/assets/{NotFoundPage-D81Z_SCM.js → NotFoundPage-DJ5YwC3-.js} +1 -1
- package/web-app/dist/assets/{ProjectPage-D9CkJI0B.js → ProjectPage-DYaGNgFt.js} +3 -3
- package/web-app/dist/assets/{ProjectsPage-vxSmYYzh.js → ProjectsPage-9GbGTW3g.js} +1 -1
- package/web-app/dist/assets/{SettingsPage-Cd2AS1Sg.js → SettingsPage-DdRI8LZ5.js} +1 -1
- package/web-app/dist/assets/{ShowcasePage-DOZCRLg5.js → ShowcasePage-ClEeMHAu.js} +1 -1
- package/web-app/dist/assets/{SystemSettingsPage-wl2UO5DF.js → SystemSettingsPage-rqj2byaP.js} +1 -1
- package/web-app/dist/assets/{TeamsPage-CA9FPLPm.js → TeamsPage-OaJ999dm.js} +1 -1
- package/web-app/dist/assets/{TemplatesPage-DLRTBM8i.js → TemplatesPage-CrqXt5pd.js} +1 -1
- package/web-app/dist/assets/{TerminalOutput-RqmPEs4o.js → TerminalOutput-DzOk3P-J.js} +1 -1
- package/web-app/dist/assets/{activity-CoyjHBZ4.js → activity-C3XPvqFU.js} +1 -1
- package/web-app/dist/assets/{bell-Dn2BCsQu.js → bell-BADR28Ic.js} +1 -1
- package/web-app/dist/assets/{bot-CNqPuD8h.js → bot-BOfl_osT.js} +1 -1
- package/web-app/dist/assets/{check-BWAwFpe2.js → check-DyeRhpqt.js} +1 -1
- package/web-app/dist/assets/{chevron-left-BgTjvxLB.js → chevron-left-DytHCita.js} +1 -1
- package/web-app/dist/assets/{circle-alert-CIYxFeDp.js → circle-alert-ej7tR0w9.js} +1 -1
- package/web-app/dist/assets/{clock-Dj6DRvL7.js → clock-CMmgiaJ3.js} +1 -1
- package/web-app/dist/assets/{cloud-DZBvW6-f.js → cloud-BmoOA5GK.js} +1 -1
- package/web-app/dist/assets/{code-xml-B0sAmzAO.js → code-xml-DjcElMOx.js} +1 -1
- package/web-app/dist/assets/{database-BkO44OAR.js → database-BxK55gai.js} +1 -1
- package/web-app/dist/assets/{dollar-sign-ChTZgVxB.js → dollar-sign-MxE9EOHB.js} +1 -1
- package/web-app/dist/assets/{file-code-corner-CQSIiG1T.js → file-code-corner-BYQ975Vf.js} +1 -1
- package/web-app/dist/assets/{file-plus-BAV2KC_M.js → file-plus-C0_fKf6I.js} +1 -1
- package/web-app/dist/assets/{globe-TUNeqVA9.js → globe-C7iJxA9K.js} +1 -1
- package/web-app/dist/assets/{hammer-CMD4wrup.js → hammer-CuVGrR3Z.js} +1 -1
- package/web-app/dist/assets/{index-B5lRV-ZJ.js → index-DvGbCL2t.js} +3 -3
- package/web-app/dist/assets/{layers-BxXkAn6j.js → layers-BWmxXnEV.js} +1 -1
- package/web-app/dist/assets/{loader-circle-CE46Yvcr.js → loader-circle-BrsqCmNy.js} +1 -1
- package/web-app/dist/assets/{lock-3uBPp5eV.js → lock-Dl3UsTMr.js} +1 -1
- package/web-app/dist/assets/{package-DjPZ3DLs.js → package-CCiRYdmb.js} +1 -1
- package/web-app/dist/assets/{plus-D8v6ZCYM.js → plus-d5BugnNw.js} +1 -1
- package/web-app/dist/assets/{refresh-cw-BSPJHBul.js → refresh-cw-DIkE6C0k.js} +1 -1
- package/web-app/dist/assets/{rotate-ccw-D0u6PH4j.js → rotate-ccw-DDhYD9MA.js} +1 -1
- package/web-app/dist/assets/{scroll-text-Dcwu9apa.js → scroll-text-jAoSqO2O.js} +1 -1
- package/web-app/dist/assets/{server-pd5YWyJw.js → server-D16MyCdB.js} +1 -1
- package/web-app/dist/assets/{shield-alert-C3TqM2Xm.js → shield-alert-C8W_Qcb5.js} +1 -1
- package/web-app/dist/assets/{thumbs-up-U1Wkm_Rg.js → thumbs-up-DdTbl2Vo.js} +1 -1
- package/web-app/dist/assets/{trash-2-BspH7NSu.js → trash-2-R-2Gvy6B.js} +1 -1
- package/web-app/dist/assets/{trending-down-DNg_ybr3.js → trending-down-Dk08-nlD.js} +1 -1
- package/web-app/dist/assets/{trending-up-CYzIu-lq.js → trending-up-DX8yONXb.js} +1 -1
- package/web-app/dist/assets/{upload-ChhyDeon.js → upload-6oDG_uiK.js} +1 -1
- package/web-app/dist/assets/{usePolling-ClMveR0q.js → usePolling-Co4817xB.js} +1 -1
- package/web-app/dist/assets/{user-BlFXAv0-.js → user-CjbWWW1W.js} +1 -1
- package/web-app/dist/index.html +1 -1
package/mcp/__init__.py
CHANGED
|
@@ -5,12 +5,17 @@ entity-relationship graph for cross-project knowledge sharing.
|
|
|
5
5
|
|
|
6
6
|
Storage: ~/.loki/knowledge/
|
|
7
7
|
patterns.jsonl - Cross-project semantic patterns
|
|
8
|
-
graph.json -
|
|
8
|
+
graph.json - Org knowledge graph: per repo services, CODEOWNERS rules and
|
|
9
|
+
decision docs (ingest_repo / merge_repo), cited by org_context
|
|
9
10
|
"""
|
|
10
11
|
|
|
12
|
+
import contextlib
|
|
11
13
|
import json
|
|
12
14
|
import logging
|
|
13
15
|
import os
|
|
16
|
+
import re
|
|
17
|
+
import subprocess
|
|
18
|
+
import sys
|
|
14
19
|
from pathlib import Path
|
|
15
20
|
from datetime import datetime, timezone
|
|
16
21
|
|
|
@@ -28,9 +33,10 @@ class OrganizationKnowledgeGraph:
|
|
|
28
33
|
"""Aggregates patterns and knowledge across multiple projects."""
|
|
29
34
|
|
|
30
35
|
def __init__(self, knowledge_dir=None):
|
|
31
|
-
self.knowledge_dir = Path(knowledge_dir or os.path.expanduser('~/.loki/knowledge'))
|
|
36
|
+
self.knowledge_dir = Path(knowledge_dir or os.environ.get('LOKI_KNOWLEDGE_DIR') or os.path.expanduser('~/.loki/knowledge'))
|
|
32
37
|
self.patterns_file = self.knowledge_dir / 'patterns.jsonl'
|
|
33
38
|
self.graph_file = self.knowledge_dir / 'graph.json'
|
|
39
|
+
self.lock_file = self.knowledge_dir / 'graph.lock'
|
|
34
40
|
self._graph = None
|
|
35
41
|
|
|
36
42
|
def ensure_dir(self):
|
|
@@ -138,68 +144,43 @@ class OrganizationKnowledgeGraph:
|
|
|
138
144
|
break
|
|
139
145
|
return patterns
|
|
140
146
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
147
|
+
@contextlib.contextmanager
|
|
148
|
+
def _graph_lock(self):
|
|
149
|
+
self.ensure_dir()
|
|
150
|
+
with open(self.lock_file, 'a') as lf:
|
|
151
|
+
if _HAS_FCNTL:
|
|
152
|
+
fcntl.flock(lf.fileno(), fcntl.LOCK_EX)
|
|
153
|
+
try:
|
|
154
|
+
yield
|
|
155
|
+
finally:
|
|
156
|
+
if _HAS_FCNTL:
|
|
157
|
+
fcntl.flock(lf.fileno(), fcntl.LOCK_UN)
|
|
152
158
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
'path': str(project_dir),
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
# Extract patterns from this project
|
|
164
|
-
memory_dir = project_dir / '.loki' / 'memory' / 'semantic'
|
|
165
|
-
if memory_dir.exists():
|
|
166
|
-
for pattern_file in memory_dir.glob('*.json'):
|
|
167
|
-
try:
|
|
168
|
-
with open(pattern_file) as f:
|
|
169
|
-
pattern = json.load(f)
|
|
170
|
-
identifier = pattern.get('name', pattern.get('pattern', pattern_file.stem))
|
|
171
|
-
pattern_id = 'pattern:' + identifier
|
|
172
|
-
graph['nodes'].append({
|
|
173
|
-
'id': pattern_id,
|
|
174
|
-
'type': 'pattern',
|
|
175
|
-
'name': identifier,
|
|
176
|
-
'category': pattern.get('category', ''),
|
|
177
|
-
})
|
|
178
|
-
graph['edges'].append({
|
|
179
|
-
'source': 'project:' + project_name,
|
|
180
|
-
'target': pattern_id,
|
|
181
|
-
'type': 'has_pattern',
|
|
182
|
-
})
|
|
183
|
-
except (json.JSONDecodeError, IOError):
|
|
184
|
-
continue
|
|
185
|
-
|
|
186
|
-
self._graph = graph
|
|
187
|
-
return graph
|
|
159
|
+
def _write_graph(self):
|
|
160
|
+
tmp = self.graph_file.with_name(self.graph_file.name + '.tmp')
|
|
161
|
+
with open(tmp, 'w') as f:
|
|
162
|
+
json.dump(self._graph, f, indent=2)
|
|
163
|
+
f.flush()
|
|
164
|
+
os.fsync(f.fileno())
|
|
165
|
+
os.replace(tmp, self.graph_file)
|
|
188
166
|
|
|
189
167
|
def save_graph(self):
|
|
190
|
-
"""Persist the graph
|
|
168
|
+
"""Persist the graph atomically (tmp file + os.replace under a flock)."""
|
|
191
169
|
if self._graph is None:
|
|
192
170
|
return
|
|
193
|
-
self.
|
|
194
|
-
|
|
195
|
-
json.dump(self._graph, f, indent=2)
|
|
171
|
+
with self._graph_lock():
|
|
172
|
+
self._write_graph()
|
|
196
173
|
|
|
197
174
|
def load_graph(self):
|
|
198
|
-
"""Load graph from disk."""
|
|
199
|
-
|
|
175
|
+
"""Load graph from disk; a missing or malformed file yields None."""
|
|
176
|
+
try:
|
|
177
|
+
with open(self.graph_file) as f:
|
|
178
|
+
data = json.load(f)
|
|
179
|
+
except (OSError, ValueError):
|
|
180
|
+
return None
|
|
181
|
+
if not isinstance(data, dict):
|
|
200
182
|
return None
|
|
201
|
-
|
|
202
|
-
self._graph = json.load(f)
|
|
183
|
+
self._graph = data
|
|
203
184
|
return self._graph
|
|
204
185
|
|
|
205
186
|
_STOPWORDS = {
|
|
@@ -254,3 +235,408 @@ class OrganizationKnowledgeGraph:
|
|
|
254
235
|
scored.append((score, p))
|
|
255
236
|
scored.sort(key=lambda x: -x[0])
|
|
256
237
|
return [p for _, p in scored[:max_results]]
|
|
238
|
+
|
|
239
|
+
# ------------------------------------------------------------------
|
|
240
|
+
# Org knowledge: services, owners and decisions read from repo files.
|
|
241
|
+
# Every node and edge carries repo + source {path, line} so a consumer
|
|
242
|
+
# of the graph can cite the file it came from. Stdlib only, no network.
|
|
243
|
+
# ------------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
_MAX_MANIFEST_BYTES = 256 * 1024
|
|
246
|
+
_MAX_DECISION_FILES = 200
|
|
247
|
+
_MAX_DECISION_BYTES = 64 * 1024
|
|
248
|
+
_MANIFEST_PARENTS = ('', 'packages', 'services', 'apps')
|
|
249
|
+
_DECISION_DIRS = ('docs/adr', 'docs/adrs', 'docs/decisions', 'doc/adr', 'adr',
|
|
250
|
+
'docs/architecture/decisions')
|
|
251
|
+
_OWNER_FILES = ('.github/CODEOWNERS', 'CODEOWNERS', 'docs/CODEOWNERS')
|
|
252
|
+
|
|
253
|
+
@staticmethod
|
|
254
|
+
def _git(repo_dir, *args):
|
|
255
|
+
try:
|
|
256
|
+
r = subprocess.run(['git', '-C', str(repo_dir)] + list(args),
|
|
257
|
+
capture_output=True, text=True, timeout=5)
|
|
258
|
+
except (OSError, subprocess.SubprocessError):
|
|
259
|
+
return ''
|
|
260
|
+
return r.stdout.strip() if r.returncode == 0 else ''
|
|
261
|
+
|
|
262
|
+
@classmethod
|
|
263
|
+
def _repo_id(cls, repo_dir):
|
|
264
|
+
url = cls._git(repo_dir, 'config', '--get', 'remote.origin.url')
|
|
265
|
+
if url:
|
|
266
|
+
url = re.sub(r'\.git$', '', url.rstrip('/'))
|
|
267
|
+
parts = [x for x in re.split(r'[/:]', url) if x]
|
|
268
|
+
if len(parts) >= 2:
|
|
269
|
+
return parts[-2] + '/' + parts[-1]
|
|
270
|
+
return Path(repo_dir).resolve().name
|
|
271
|
+
|
|
272
|
+
@staticmethod
|
|
273
|
+
def _read(repo_dir, rel, limit):
|
|
274
|
+
"""Read a repo file as text; refuse symlinks and paths outside the repo."""
|
|
275
|
+
path = Path(repo_dir) / rel
|
|
276
|
+
try:
|
|
277
|
+
if path.is_symlink() or not path.is_file():
|
|
278
|
+
return None
|
|
279
|
+
root = Path(repo_dir).resolve()
|
|
280
|
+
if root not in path.resolve().parents:
|
|
281
|
+
return None
|
|
282
|
+
with open(path, 'rb') as f:
|
|
283
|
+
return f.read(limit).decode('utf-8', 'replace')
|
|
284
|
+
except OSError:
|
|
285
|
+
return None
|
|
286
|
+
|
|
287
|
+
@staticmethod
|
|
288
|
+
def _norm_py(name):
|
|
289
|
+
return re.split(r'[<>=!~;\[ ]', name.strip().lower().replace('_', '-'), 1)[0]
|
|
290
|
+
|
|
291
|
+
@staticmethod
|
|
292
|
+
def _line_of(lines, pattern, start=0):
|
|
293
|
+
rx = re.compile(pattern)
|
|
294
|
+
for i in range(start, len(lines)):
|
|
295
|
+
if rx.search(lines[i]):
|
|
296
|
+
return i + 1
|
|
297
|
+
return None
|
|
298
|
+
|
|
299
|
+
def _parse_package_json(self, text):
|
|
300
|
+
try:
|
|
301
|
+
data = json.loads(text)
|
|
302
|
+
except ValueError:
|
|
303
|
+
return None
|
|
304
|
+
if not isinstance(data, dict) or not isinstance(data.get('name'), str):
|
|
305
|
+
return None
|
|
306
|
+
lines = text.splitlines()
|
|
307
|
+
deps, dep_lines = [], {}
|
|
308
|
+
for key in ('dependencies', 'devDependencies', 'peerDependencies'):
|
|
309
|
+
section = data.get(key)
|
|
310
|
+
if not isinstance(section, dict):
|
|
311
|
+
continue
|
|
312
|
+
start = (self._line_of(lines, r'"%s"\s*:' % key) or 1) - 1
|
|
313
|
+
for dep in section:
|
|
314
|
+
if dep not in dep_lines:
|
|
315
|
+
deps.append(dep)
|
|
316
|
+
dep_lines[dep] = self._line_of(lines, r'"%s"\s*:' % re.escape(dep), start) or start + 1
|
|
317
|
+
return data['name'], self._line_of(lines, r'"name"\s*:') or 1, deps, dep_lines
|
|
318
|
+
|
|
319
|
+
def _parse_pyproject(self, text):
|
|
320
|
+
lines = text.splitlines()
|
|
321
|
+
name, name_line, section = None, 1, ''
|
|
322
|
+
deps, dep_lines = [], {}
|
|
323
|
+
|
|
324
|
+
def add(raw, ln):
|
|
325
|
+
dep = self._norm_py(raw)
|
|
326
|
+
if dep and dep != 'python' and dep not in dep_lines:
|
|
327
|
+
deps.append(dep)
|
|
328
|
+
dep_lines[dep] = ln
|
|
329
|
+
|
|
330
|
+
in_array = False
|
|
331
|
+
for i, line in enumerate(lines, 1):
|
|
332
|
+
stripped = line.strip()
|
|
333
|
+
if in_array:
|
|
334
|
+
for m in re.finditer(r'["\']([^"\']+)["\']', stripped.split('#')[0]):
|
|
335
|
+
add(m.group(1), i)
|
|
336
|
+
if ']' in stripped.split('#')[0]:
|
|
337
|
+
in_array = False
|
|
338
|
+
continue
|
|
339
|
+
m = re.match(r'^\[([^\]]+)\]\s*$', stripped)
|
|
340
|
+
if m:
|
|
341
|
+
section = m.group(1).strip()
|
|
342
|
+
continue
|
|
343
|
+
if section in ('project', 'tool.poetry'):
|
|
344
|
+
m = re.match(r'^name\s*=\s*["\']([^"\']+)["\']', stripped)
|
|
345
|
+
if m and name is None:
|
|
346
|
+
name, name_line = self._norm_py(m.group(1)), i
|
|
347
|
+
if section == 'project' and re.match(r'^dependencies\s*=\s*\[', stripped):
|
|
348
|
+
body = stripped.split('=', 1)[1].split('#')[0]
|
|
349
|
+
for mm in re.finditer(r'["\']([^"\']+)["\']', body):
|
|
350
|
+
add(mm.group(1), i)
|
|
351
|
+
in_array = ']' not in body
|
|
352
|
+
elif section == 'tool.poetry.dependencies':
|
|
353
|
+
m = re.match(r'^([A-Za-z0-9_.\-]+)\s*=', stripped)
|
|
354
|
+
if m:
|
|
355
|
+
add(m.group(1), i)
|
|
356
|
+
if not name:
|
|
357
|
+
return None
|
|
358
|
+
return name, name_line, deps, dep_lines
|
|
359
|
+
|
|
360
|
+
def _parse_go_mod(self, text):
|
|
361
|
+
name, name_line, deps, dep_lines, in_block = None, 1, [], {}, False
|
|
362
|
+
for i, line in enumerate(text.splitlines(), 1):
|
|
363
|
+
stripped = line.split('//')[0].strip()
|
|
364
|
+
m = re.match(r'^module\s+(\S+)', stripped)
|
|
365
|
+
if m and name is None:
|
|
366
|
+
name, name_line = m.group(1).strip('"'), i
|
|
367
|
+
continue
|
|
368
|
+
if re.match(r'^require\s*\(', stripped):
|
|
369
|
+
in_block = True
|
|
370
|
+
continue
|
|
371
|
+
if in_block and stripped == ')':
|
|
372
|
+
in_block = False
|
|
373
|
+
continue
|
|
374
|
+
m = re.match(r'^require\s+(\S+)\s+\S+', stripped) or (in_block and re.match(r'^(\S+)\s+\S+', stripped))
|
|
375
|
+
if m and m.group(1) not in dep_lines:
|
|
376
|
+
deps.append(m.group(1))
|
|
377
|
+
dep_lines[m.group(1)] = i
|
|
378
|
+
if not name:
|
|
379
|
+
return None
|
|
380
|
+
return name, name_line, deps, dep_lines
|
|
381
|
+
|
|
382
|
+
def _ingest_services(self, repo_dir, repo):
|
|
383
|
+
nodes = []
|
|
384
|
+
root = Path(repo_dir)
|
|
385
|
+
dirs = []
|
|
386
|
+
for parent in self._MANIFEST_PARENTS:
|
|
387
|
+
if parent == '':
|
|
388
|
+
dirs.append('')
|
|
389
|
+
continue
|
|
390
|
+
base = root / parent
|
|
391
|
+
if base.is_dir() and not base.is_symlink():
|
|
392
|
+
try:
|
|
393
|
+
dirs.extend(parent + '/' + c.name for c in sorted(base.iterdir()) if c.is_dir() and not c.is_symlink())
|
|
394
|
+
except OSError:
|
|
395
|
+
pass
|
|
396
|
+
parsers = (('package.json', self._parse_package_json),
|
|
397
|
+
('pyproject.toml', self._parse_pyproject),
|
|
398
|
+
('go.mod', self._parse_go_mod))
|
|
399
|
+
for d in dirs:
|
|
400
|
+
for fname, parse in parsers:
|
|
401
|
+
rel = (d + '/' + fname) if d else fname
|
|
402
|
+
text = self._read(repo_dir, rel, self._MAX_MANIFEST_BYTES)
|
|
403
|
+
parsed = parse(text) if text else None
|
|
404
|
+
if not parsed:
|
|
405
|
+
continue
|
|
406
|
+
name, line, deps, dep_lines = parsed
|
|
407
|
+
nodes.append({'id': 'service:' + name, 'type': 'service', 'name': name,
|
|
408
|
+
'deps': deps, 'deps_lines': dep_lines, 'repo': repo,
|
|
409
|
+
'source': {'path': rel, 'line': line}})
|
|
410
|
+
return nodes
|
|
411
|
+
|
|
412
|
+
def _ingest_owners(self, repo_dir, repo):
|
|
413
|
+
for rel in self._OWNER_FILES:
|
|
414
|
+
text = self._read(repo_dir, rel, self._MAX_MANIFEST_BYTES)
|
|
415
|
+
if text is None:
|
|
416
|
+
continue
|
|
417
|
+
edges = []
|
|
418
|
+
for i, line in enumerate(text.splitlines(), 1):
|
|
419
|
+
stripped = line.split('#')[0].strip()
|
|
420
|
+
parts = stripped.split()
|
|
421
|
+
if len(parts) < 2:
|
|
422
|
+
continue
|
|
423
|
+
edges.append({'type': 'owns', 'pattern': parts[0], 'owners': parts[1:],
|
|
424
|
+
'repo': repo, 'source': {'path': rel, 'line': i}})
|
|
425
|
+
return edges
|
|
426
|
+
return []
|
|
427
|
+
|
|
428
|
+
def _ingest_decisions(self, repo_dir, repo):
|
|
429
|
+
root = Path(repo_dir)
|
|
430
|
+
files = []
|
|
431
|
+
for d in self._DECISION_DIRS:
|
|
432
|
+
base = root / d
|
|
433
|
+
if not base.is_dir() or base.is_symlink():
|
|
434
|
+
continue
|
|
435
|
+
try:
|
|
436
|
+
files.extend(sorted(base.rglob('*.md')))
|
|
437
|
+
except OSError:
|
|
438
|
+
continue
|
|
439
|
+
nodes, seen = [], set()
|
|
440
|
+
for path in files:
|
|
441
|
+
if len(nodes) >= self._MAX_DECISION_FILES:
|
|
442
|
+
break
|
|
443
|
+
rel = path.relative_to(root).as_posix()
|
|
444
|
+
if rel in seen:
|
|
445
|
+
continue
|
|
446
|
+
seen.add(rel)
|
|
447
|
+
text = self._read(repo_dir, rel, self._MAX_DECISION_BYTES)
|
|
448
|
+
if not text:
|
|
449
|
+
continue
|
|
450
|
+
lines = text.splitlines()
|
|
451
|
+
title, title_line = path.stem, 1
|
|
452
|
+
for i, line in enumerate(lines, 1):
|
|
453
|
+
if line.startswith('# '):
|
|
454
|
+
title, title_line = line[2:].strip(), i
|
|
455
|
+
break
|
|
456
|
+
status = ''
|
|
457
|
+
for i, line in enumerate(lines):
|
|
458
|
+
m = re.match(r'^\W*status\W*:\s*(.+)$', line.strip(), re.I)
|
|
459
|
+
if m:
|
|
460
|
+
status = m.group(1).strip(' *_')
|
|
461
|
+
break
|
|
462
|
+
if re.match(r'^##\s+status\s*$', line.strip(), re.I):
|
|
463
|
+
for nxt in lines[i + 1:]:
|
|
464
|
+
if nxt.strip():
|
|
465
|
+
status = nxt.strip(' *_')
|
|
466
|
+
break
|
|
467
|
+
break
|
|
468
|
+
body = None
|
|
469
|
+
for i, line in enumerate(lines):
|
|
470
|
+
if re.match(r'^##\s+decision\s*$', line.strip(), re.I):
|
|
471
|
+
body = lines[i + 1:]
|
|
472
|
+
break
|
|
473
|
+
if body is None:
|
|
474
|
+
body = lines[title_line:]
|
|
475
|
+
chunk = []
|
|
476
|
+
for line in body:
|
|
477
|
+
if line.startswith('## '):
|
|
478
|
+
break
|
|
479
|
+
chunk.append(line)
|
|
480
|
+
summary = re.sub(r'\s+', ' ', ' '.join(chunk)).strip()[:300]
|
|
481
|
+
nodes.append({'id': 'decision:%s:%s' % (repo, rel), 'type': 'decision',
|
|
482
|
+
'title': title, 'status': status, 'summary': summary,
|
|
483
|
+
'repo': repo, 'source': {'path': rel, 'line': title_line}})
|
|
484
|
+
return nodes
|
|
485
|
+
|
|
486
|
+
def ingest_repo(self, repo_dir):
|
|
487
|
+
"""Read services, CODEOWNERS rules and decision docs from one repo."""
|
|
488
|
+
repo = self._repo_id(repo_dir)
|
|
489
|
+
nodes = self._ingest_services(repo_dir, repo) + self._ingest_decisions(repo_dir, repo)
|
|
490
|
+
return {'repo': repo, 'nodes': nodes, 'edges': self._ingest_owners(repo_dir, repo)}
|
|
491
|
+
|
|
492
|
+
def merge_repo(self, repo_dir):
|
|
493
|
+
"""Replace this repo's facts in graph.json with a fresh ingest (idempotent)."""
|
|
494
|
+
res = self.ingest_repo(repo_dir)
|
|
495
|
+
repo = res['repo']
|
|
496
|
+
with self._graph_lock():
|
|
497
|
+
graph = self.load_graph()
|
|
498
|
+
if not graph or not isinstance(graph.get('repos'), dict):
|
|
499
|
+
graph = {'version': 2, 'repos': {}, 'nodes': [], 'edges': []}
|
|
500
|
+
graph['version'] = 2
|
|
501
|
+
graph['nodes'] = [n for n in graph.get('nodes', []) if n.get('repo') != repo] + res['nodes']
|
|
502
|
+
graph['edges'] = [e for e in graph.get('edges', []) if e.get('repo') != repo] + res['edges']
|
|
503
|
+
graph['repos'][repo] = {'path': str(Path(repo_dir).resolve()),
|
|
504
|
+
'ingested_at': datetime.now(timezone.utc).isoformat(),
|
|
505
|
+
'head_sha': self._git(repo_dir, 'rev-parse', 'HEAD')}
|
|
506
|
+
self._graph = graph
|
|
507
|
+
self._write_graph()
|
|
508
|
+
return {'repo': repo,
|
|
509
|
+
'services': sum(1 for n in res['nodes'] if n['type'] == 'service'),
|
|
510
|
+
'owners': len(res['edges']),
|
|
511
|
+
'decisions': sum(1 for n in res['nodes'] if n['type'] == 'decision'),
|
|
512
|
+
'repos': len(graph['repos'])}
|
|
513
|
+
|
|
514
|
+
@staticmethod
|
|
515
|
+
def _cite(item):
|
|
516
|
+
src = item.get('source') or {}
|
|
517
|
+
return ' [%s %s:%s]' % (item.get('repo', '?'), src.get('path', '?'), src.get('line', 1))
|
|
518
|
+
|
|
519
|
+
@staticmethod
|
|
520
|
+
def _clean(text):
|
|
521
|
+
return re.sub(r'\s+', ' ', re.sub(r'[\x00-\x1f\x7f]', ' ', str(text))).strip()
|
|
522
|
+
|
|
523
|
+
def org_context(self, task, repo_dir, max_bytes=1500):
|
|
524
|
+
"""Cited plain lines: this repo's services, owners, consumers, providers, decisions."""
|
|
525
|
+
graph = self.load_graph()
|
|
526
|
+
if not graph:
|
|
527
|
+
return ''
|
|
528
|
+
repo = self._repo_id(repo_dir)
|
|
529
|
+
nodes, edges = graph.get('nodes', []), graph.get('edges', [])
|
|
530
|
+
mine = [n for n in nodes if n.get('type') == 'service' and n.get('repo') == repo]
|
|
531
|
+
others = [n for n in nodes if n.get('type') == 'service' and n.get('repo') != repo]
|
|
532
|
+
task_tokens = self._tokenize(task)
|
|
533
|
+
lines = []
|
|
534
|
+
for n in mine:
|
|
535
|
+
lines.append('service %s (this repo)%s' % (n['name'], self._cite(n)))
|
|
536
|
+
rules = []
|
|
537
|
+
for e in edges:
|
|
538
|
+
if e.get('type') != 'owns' or e.get('repo') != repo:
|
|
539
|
+
continue
|
|
540
|
+
overlap = len(self._tokenize(e.get('pattern', '')) & task_tokens)
|
|
541
|
+
if overlap > 0:
|
|
542
|
+
rules.append((0, -overlap, e.get('source', {}).get('line', 0), e))
|
|
543
|
+
elif e.get('pattern') == '*':
|
|
544
|
+
rules.append((1, 0, e.get('source', {}).get('line', 0), e))
|
|
545
|
+
rules.sort(key=lambda r: r[:3])
|
|
546
|
+
for _, _, _, e in rules[:4]:
|
|
547
|
+
lines.append('owner %s: %s%s' % (e['pattern'], ' '.join(e['owners']), self._cite(e)))
|
|
548
|
+
my_names = {n['name'] for n in mine}
|
|
549
|
+
consumers = []
|
|
550
|
+
for o in others:
|
|
551
|
+
for dep in o.get('deps', []):
|
|
552
|
+
if dep in my_names:
|
|
553
|
+
ln = (o.get('deps_lines') or {}).get(dep, o['source']['line'])
|
|
554
|
+
consumers.append('consumer: %s in %s depends on %s [%s %s:%s]' % (
|
|
555
|
+
o['name'], o['repo'], dep, o['repo'], o['source']['path'], ln))
|
|
556
|
+
lines.extend(consumers[:5])
|
|
557
|
+
providers = []
|
|
558
|
+
my_deps = {d for n in mine for d in n.get('deps', [])}
|
|
559
|
+
for o in others:
|
|
560
|
+
if o['name'] in my_deps:
|
|
561
|
+
star = [e for e in edges if e.get('type') == 'owns' and e.get('repo') == o['repo']
|
|
562
|
+
and e.get('pattern') == '*']
|
|
563
|
+
owners = ' '.join(star[0]['owners']) if star else 'unknown'
|
|
564
|
+
providers.append('provider: %s from %s, owners %s%s' % (o['name'], o['repo'], owners, self._cite(o)))
|
|
565
|
+
lines.extend(providers[:3])
|
|
566
|
+
scored = []
|
|
567
|
+
for n in nodes:
|
|
568
|
+
if n.get('type') != 'decision':
|
|
569
|
+
continue
|
|
570
|
+
score = 2 * len(task_tokens & self._tokenize(n.get('title', ''))) + \
|
|
571
|
+
len(task_tokens & self._tokenize(n.get('summary', '')))
|
|
572
|
+
if score > 0:
|
|
573
|
+
scored.append((-score, n.get('id', ''), n))
|
|
574
|
+
scored.sort(key=lambda r: r[:2])
|
|
575
|
+
for _, _, n in scored[:3]:
|
|
576
|
+
lines.append('decision "%s" (%s): %s%s' % (n['title'], n.get('status') or 'no status',
|
|
577
|
+
n.get('summary', ''), self._cite(n)))
|
|
578
|
+
out, used = [], 0
|
|
579
|
+
for line in lines:
|
|
580
|
+
line = self._clean(line)
|
|
581
|
+
cost = len(line.encode('utf-8')) + 1
|
|
582
|
+
if used + cost > max_bytes:
|
|
583
|
+
break
|
|
584
|
+
out.append(line)
|
|
585
|
+
used += cost
|
|
586
|
+
return '\n'.join(out)
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def main(argv):
|
|
590
|
+
usage = ('usage: knowledge_graph.py ingest <path>... | brief --repo D --task T | '
|
|
591
|
+
'ask <question> [--repo D]')
|
|
592
|
+
if not argv:
|
|
593
|
+
print(usage, file=sys.stderr)
|
|
594
|
+
return 2
|
|
595
|
+
cmd, rest = argv[0], argv[1:]
|
|
596
|
+
kg = OrganizationKnowledgeGraph()
|
|
597
|
+
if cmd == 'ingest':
|
|
598
|
+
paths = rest or ['.']
|
|
599
|
+
for p in paths:
|
|
600
|
+
if not os.path.isdir(p):
|
|
601
|
+
print('Not a directory: %s' % p, file=sys.stderr)
|
|
602
|
+
return 1
|
|
603
|
+
r = kg.merge_repo(p)
|
|
604
|
+
print('Ingested %s: %d services, %d owner rules, %d decisions (graph: %d repos) -> %s' % (
|
|
605
|
+
r['repo'], r['services'], r['owners'], r['decisions'], r['repos'], kg.graph_file))
|
|
606
|
+
return 0
|
|
607
|
+
opts, words, i = {}, [], 0
|
|
608
|
+
while i < len(rest):
|
|
609
|
+
if rest[i] in ('--repo', '--task') and i + 1 < len(rest):
|
|
610
|
+
opts[rest[i]] = rest[i + 1]
|
|
611
|
+
i += 2
|
|
612
|
+
else:
|
|
613
|
+
words.append(rest[i])
|
|
614
|
+
i += 1
|
|
615
|
+
repo = opts.get('--repo', '.')
|
|
616
|
+
if cmd == 'brief':
|
|
617
|
+
# Never fail a run: every error yields an empty block.
|
|
618
|
+
try:
|
|
619
|
+
kg.merge_repo(repo)
|
|
620
|
+
except Exception as e: # noqa: BLE001
|
|
621
|
+
logger.warning('knowledge_graph: ingest failed: %s', e)
|
|
622
|
+
try:
|
|
623
|
+
out = kg.org_context(opts.get('--task', ''), repo)
|
|
624
|
+
except Exception as e: # noqa: BLE001
|
|
625
|
+
logger.warning('knowledge_graph: context failed: %s', e)
|
|
626
|
+
out = ''
|
|
627
|
+
if out:
|
|
628
|
+
print(out)
|
|
629
|
+
return 0
|
|
630
|
+
if cmd == 'ask':
|
|
631
|
+
if not words:
|
|
632
|
+
print(usage, file=sys.stderr)
|
|
633
|
+
return 2
|
|
634
|
+
out = kg.org_context(' '.join(words), repo)
|
|
635
|
+
print(out or 'No org knowledge found. Run: loki memory graph ingest <path>...')
|
|
636
|
+
return 0
|
|
637
|
+
print(usage, file=sys.stderr)
|
|
638
|
+
return 2
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
if __name__ == '__main__':
|
|
642
|
+
sys.exit(main(sys.argv[1:]))
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "loki-mode",
|
|
3
3
|
"mcpName": "io.github.asklokesh/loki-mode",
|
|
4
|
-
"version": "11.
|
|
4
|
+
"version": "11.130.0",
|
|
5
5
|
"description": "An autonomous software factory that knows what it is supposed to deliver, and proves it did.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"agent",
|
|
@@ -123,7 +123,7 @@
|
|
|
123
123
|
],
|
|
124
124
|
"scripts": {
|
|
125
125
|
"postinstall": "node bin/loki-postinstall.js || exit 0",
|
|
126
|
-
"prepack": "find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null; find . -name '*.pyc' -delete 2>/dev/null; if command -v bun >/dev/null 2>&1; then (cd loki-ts && bun install --production && bun run build) || echo '
|
|
126
|
+
"prepack": "find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null; find . -name '*.pyc' -delete 2>/dev/null; if command -v bun >/dev/null 2>&1; then (cd loki-ts && bun install --production && bun run build) || { echo 'FATAL: loki-ts build failed; refusing to pack without loki-ts/dist' >&2; exit 1; }; else echo 'WARN: bun not on PATH, packing without loki-ts/dist (loki builds it on first run from a git clone; the registry package needs bun at publish)' >&2; fi; find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null; find . -name '*.pyc' -delete 2>/dev/null; node tools/normalize-package-permissions.mjs .",
|
|
127
127
|
"prepublishOnly": "cd web-app && npm ci && npm run build && test -f dist/index.html && grep -q /lab/assets/ dist/index.html && cd ../packages/control-plane && bun install --frozen-lockfile && bun run build:all && test -f ui/dist/index.html && test -f dist/server.js && test -f dist/ask-tools-server.js",
|
|
128
128
|
"test": "bash scripts/local-ci.sh"
|
|
129
129
|
},
|
|
@@ -161,5 +161,5 @@
|
|
|
161
161
|
"dependencies": {
|
|
162
162
|
"@resvg/resvg-wasm": "^2.6.2"
|
|
163
163
|
},
|
|
164
|
-
"gitHead": "
|
|
164
|
+
"gitHead": "901e75da6fbd144fdda0f017e9d260b7e974f065"
|
|
165
165
|
}
|