loki-mode 7.7.16 → 7.7.18

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.
@@ -0,0 +1,450 @@
1
+ """v7.7.18: ingest non-`loki start` sessions into the memory store.
2
+
3
+ Diagnosis at ~/git/loki-plan/MEMORY-DIAGNOSIS-2026-05-27.md root cause:
4
+ `auto_capture_episode` only fires inside `run_autonomous()` reached via
5
+ `loki start <prd>`. The 167 release sessions in 2026 happened in regular
6
+ Claude Code, never producing episodes. This module + the MCP capture
7
+ tool in `mcp/server.py` + the `loki memory ingest` CLI together close
8
+ that gap.
9
+
10
+ Two entry points:
11
+ - `ingest_from_claude_transcript(path)` -> reads a Claude Code
12
+ session transcript JSONL, extracts tool_use traces, produces an
13
+ EpisodeTrace with populated action_log + files_read + files_modified.
14
+ - `ingest_from_summary(goal, outcome, files_modified, ...)` -> builds
15
+ an episode from explicit fields (used by the MCP capture tool the
16
+ agent calls at iteration close).
17
+
18
+ Both call `memory.engine.MemoryEngine.store_episode` under the hood.
19
+
20
+ Safety:
21
+ - Secret scrubber (mirrors memory/error_log.py + v7.7.10) applied
22
+ to goal text, tool inputs, file paths.
23
+ - Honors `LOKI_MEMORY_CAPTURE_DISABLED=true` env var (capture wedge
24
+ escape hatch).
25
+ - Never raises; returns None on any failure and logs to .errors.log.
26
+ """
27
+ from __future__ import annotations
28
+
29
+ import json
30
+ import os
31
+ import re
32
+ from datetime import datetime, timezone
33
+ from pathlib import Path
34
+ from typing import Any, Dict, List, Optional, Tuple
35
+
36
+ # Local imports deferred to call time to keep this module importable
37
+ # without the heavier memory.engine + memory.schemas dependency tree.
38
+
39
+
40
+ # Same scrubber regex set as memory/error_log.py + v7.7.10 USAGE.md regen.
41
+ _CREDENTIAL_KEYWORD_RE = re.compile(
42
+ r"(?i)(api[_-]?key|secret|password|token|private[_-]?key|credential|bearer)"
43
+ )
44
+ _HIGH_ENTROPY_TOKEN_RE = re.compile(
45
+ r"sk-[A-Za-z0-9_-]{16,}|pk_[A-Za-z0-9_-]{16,}|ghp_[A-Za-z0-9]{16,}|"
46
+ r"ghs_[A-Za-z0-9]{16,}|xox[bpoa]-[A-Za-z0-9-]{16,}|AIza[A-Za-z0-9_-]{32,}|"
47
+ r"AKIA[A-Z0-9]{12,}"
48
+ )
49
+
50
+ # Tool names that indicate a file was READ.
51
+ READ_TOOL_NAMES = frozenset({"Read", "ReadFile", "read_file", "Grep", "Glob"})
52
+ # Tool names that indicate a file was MODIFIED (created or edited).
53
+ WRITE_TOOL_NAMES = frozenset(
54
+ {"Edit", "Write", "MultiEdit", "Patch", "NotebookEdit", "ApplyDiff"}
55
+ )
56
+ # Tool names that are shell command executions (no specific file).
57
+ SHELL_TOOL_NAMES = frozenset({"Bash", "Shell", "Exec"})
58
+
59
+
60
+ def _scrub(text: str) -> str:
61
+ """Redact credential-shaped substrings. Single-pass tokenizer."""
62
+ if not text:
63
+ return text
64
+ out_tokens = []
65
+ for token in text.split():
66
+ if _CREDENTIAL_KEYWORD_RE.search(token):
67
+ out_tokens.append("[REDACTED]")
68
+ else:
69
+ out_tokens.append(_HIGH_ENTROPY_TOKEN_RE.sub("[REDACTED]", token))
70
+ return " ".join(out_tokens)
71
+
72
+
73
+ # v7.7.18 council fix (Opus 2): path-aware redaction for file paths.
74
+ # `_scrub` is whitespace-tokenized and misses sensitive paths like
75
+ # `/Users/me/.aws/credentials` (no whitespace, no credential keyword).
76
+ # Per Opus 2: "Document as known boundary or add path-aware redaction
77
+ # (e.g. drop `.aws/`, `.ssh/`, `credentials`, `.env*` segments)."
78
+ _SENSITIVE_PATH_SUBSTRINGS = (
79
+ "/.aws/",
80
+ "/.ssh/",
81
+ "/.gnupg/",
82
+ "/.docker/config",
83
+ "/credentials",
84
+ "/.netrc",
85
+ "/.npmrc",
86
+ "/.pypirc",
87
+ )
88
+ _SENSITIVE_PATH_BASENAMES = (
89
+ ".env",
90
+ ".env.local",
91
+ ".env.production",
92
+ ".env.development",
93
+ ".env.staging",
94
+ "credentials",
95
+ "credentials.json",
96
+ "secrets.json",
97
+ "id_rsa",
98
+ "id_ed25519",
99
+ "private.key",
100
+ )
101
+
102
+
103
+ def _scrub_path(path: str) -> str:
104
+ """Redact file paths that point to commonly-sensitive locations.
105
+
106
+ Returns the original path if benign; returns a `[REDACTED:<reason>]`
107
+ marker (with the directory portion preserved) if the path looks
108
+ sensitive. Preserves directory context so dashboards can still
109
+ surface "this session touched credential files" without leaking
110
+ the specific file name.
111
+ """
112
+ if not path:
113
+ return path
114
+ # Normalize separators for matching but keep original for return
115
+ norm = path.replace("\\", "/")
116
+ lowered = norm.lower()
117
+ for substr in _SENSITIVE_PATH_SUBSTRINGS:
118
+ if substr in lowered:
119
+ head = path.rsplit("/", 1)[0] if "/" in path else ""
120
+ return f"{head}/[REDACTED:sensitive-dir]" if head else "[REDACTED:sensitive-dir]"
121
+ basename = path.rsplit("/", 1)[-1] if "/" in path else path
122
+ if basename.lower() in _SENSITIVE_PATH_BASENAMES or basename.lower().startswith(".env"):
123
+ head = path.rsplit("/", 1)[0] if "/" in path else ""
124
+ return f"{head}/[REDACTED:sensitive-file]" if head else "[REDACTED:sensitive-file]"
125
+ return path
126
+
127
+
128
+ def _capture_disabled() -> bool:
129
+ """Honor `LOKI_MEMORY_CAPTURE_DISABLED=true` escape hatch."""
130
+ return os.environ.get("LOKI_MEMORY_CAPTURE_DISABLED", "").lower() in (
131
+ "true",
132
+ "1",
133
+ "yes",
134
+ )
135
+
136
+
137
+ def _log_to_errors(memory_base: str, function_name: str, exc: BaseException) -> None:
138
+ """Best-effort error log; never raises. Mirrors error_log.log_memory_error
139
+ but accessed lazily so this module imports without circular deps."""
140
+ try:
141
+ from memory.error_log import log_memory_error
142
+ log_memory_error(memory_base, function_name, exc)
143
+ except Exception:
144
+ return
145
+
146
+
147
+ def _parse_transcript_line(line: str) -> Optional[Dict[str, Any]]:
148
+ """Parse one JSONL line; return None on parse error."""
149
+ try:
150
+ return json.loads(line)
151
+ except (json.JSONDecodeError, ValueError):
152
+ return None
153
+
154
+
155
+ def _extract_tool_uses(transcript_entries: List[Dict[str, Any]]) -> List[Tuple[str, Dict[str, Any], Optional[str]]]:
156
+ """Walk transcript entries; yield (tool_name, input_dict, timestamp) tuples.
157
+
158
+ Claude Code transcript shape (observed v7.7.x): top-level entries
159
+ with `type: assistant|user|...`. Assistant entries have a nested
160
+ `message.content` array containing items with `type: text|tool_use`.
161
+ """
162
+ tool_uses = []
163
+ for entry in transcript_entries:
164
+ if entry.get("type") != "assistant":
165
+ continue
166
+ msg = entry.get("message") or {}
167
+ if not isinstance(msg, dict):
168
+ continue
169
+ content = msg.get("content") or []
170
+ if not isinstance(content, list):
171
+ continue
172
+ ts = entry.get("timestamp") or msg.get("timestamp")
173
+ for item in content:
174
+ if not isinstance(item, dict):
175
+ continue
176
+ if item.get("type") == "tool_use":
177
+ name = item.get("name", "")
178
+ inp = item.get("input", {})
179
+ if not isinstance(inp, dict):
180
+ inp = {}
181
+ tool_uses.append((name, inp, ts))
182
+ return tool_uses
183
+
184
+
185
+ def _extract_files(tool_uses: List[Tuple[str, Dict[str, Any], Optional[str]]]) -> Tuple[List[str], List[str]]:
186
+ """Return (files_read, files_modified) deduped, in first-seen order."""
187
+ read_seen: Dict[str, None] = {}
188
+ mod_seen: Dict[str, None] = {}
189
+ for name, inp, _ts in tool_uses:
190
+ path = inp.get("file_path") or inp.get("path") or inp.get("filepath")
191
+ if not isinstance(path, str) or not path:
192
+ continue
193
+ if name in READ_TOOL_NAMES and path not in read_seen:
194
+ read_seen[path] = None
195
+ if name in WRITE_TOOL_NAMES and path not in mod_seen:
196
+ mod_seen[path] = None
197
+ return list(read_seen.keys()), list(mod_seen.keys())
198
+
199
+
200
+ def _build_action_log(
201
+ tool_uses: List[Tuple[str, Dict[str, Any], Optional[str]]],
202
+ start_ts: Optional[datetime],
203
+ max_entries: int = 100,
204
+ ) -> List[Any]:
205
+ """Convert tool_use tuples to ActionEntry objects (scrubbed).
206
+
207
+ Returns a list of `memory.schemas.ActionEntry` instances. Defers the
208
+ import so this module is testable without the full schema chain.
209
+ Caps at `max_entries` to bound episode size on long sessions.
210
+ """
211
+ from memory.schemas import ActionEntry
212
+ entries = []
213
+ for i, (name, inp, ts) in enumerate(tool_uses[:max_entries]):
214
+ rel_ts = 0
215
+ if start_ts and ts:
216
+ try:
217
+ t = datetime.fromisoformat(str(ts).replace("Z", "+00:00"))
218
+ rel_ts = int((t - start_ts).total_seconds())
219
+ except (ValueError, TypeError):
220
+ rel_ts = i
221
+ # Choose a compact input representation
222
+ target = (
223
+ inp.get("file_path")
224
+ or inp.get("path")
225
+ or inp.get("command")
226
+ or inp.get("query")
227
+ or json.dumps(inp, default=str)[:200]
228
+ )
229
+ target = _scrub(str(target))[:300]
230
+ entries.append(ActionEntry(
231
+ tool=name or "?",
232
+ input=target,
233
+ output="", # transcript does not always include tool_result inline; v7.7.19 may enrich
234
+ timestamp=rel_ts,
235
+ ))
236
+ return entries
237
+
238
+
239
+ def _derive_goal(transcript_entries: List[Dict[str, Any]]) -> str:
240
+ """Best-effort goal extraction: first user message text, OR aiTitle, OR ''."""
241
+ for entry in transcript_entries:
242
+ if entry.get("type") == "user":
243
+ msg = entry.get("message") or {}
244
+ if isinstance(msg, dict):
245
+ content = msg.get("content")
246
+ if isinstance(content, str):
247
+ return _scrub(content)[:500]
248
+ if isinstance(content, list):
249
+ for item in content:
250
+ if isinstance(item, dict) and item.get("type") == "text":
251
+ t = item.get("text", "")
252
+ if t:
253
+ return _scrub(t)[:500]
254
+ for entry in transcript_entries:
255
+ if entry.get("type") == "ai-title":
256
+ title = entry.get("title") or entry.get("aiTitle")
257
+ if title:
258
+ return _scrub(str(title))[:500]
259
+ return ""
260
+
261
+
262
+ def _derive_timestamps(transcript_entries: List[Dict[str, Any]]) -> Tuple[Optional[datetime], Optional[datetime]]:
263
+ """Return (start, end) datetimes from first/last entry with timestamp."""
264
+ timestamps: List[datetime] = []
265
+ for entry in transcript_entries:
266
+ for key in ("timestamp", "ts", "createdAt"):
267
+ v = entry.get(key)
268
+ if v:
269
+ try:
270
+ timestamps.append(datetime.fromisoformat(str(v).replace("Z", "+00:00")))
271
+ break
272
+ except (ValueError, TypeError):
273
+ pass
274
+ if not timestamps:
275
+ return None, None
276
+ return min(timestamps), max(timestamps)
277
+
278
+
279
+ def ingest_from_claude_transcript(
280
+ transcript_path: str,
281
+ memory_base: str,
282
+ *,
283
+ task_id: Optional[str] = None,
284
+ agent: str = "claude-code",
285
+ phase: str = "INTERACTIVE",
286
+ outcome: str = "success",
287
+ ) -> Optional[str]:
288
+ """Read a Claude Code session transcript JSONL and store an Episode.
289
+
290
+ Args:
291
+ transcript_path: Path to a Claude Code session transcript JSONL
292
+ (typically under `~/.claude/projects/<dir>/<session>.jsonl`).
293
+ memory_base: Path to the project's `.loki/memory/` directory.
294
+ task_id: Override the task id. Default: `claude-session-<8-char>`
295
+ from the transcript's `sessionId` field or filename.
296
+ agent: Stamped on the episode. Default "claude-code".
297
+ phase: Stamped on the episode. Default "INTERACTIVE".
298
+ outcome: Stamped on the episode. Default "success".
299
+
300
+ Returns:
301
+ Path to the written episode JSON on success; None on failure
302
+ (silent fail, error logged to `.errors.log`).
303
+ """
304
+ if _capture_disabled():
305
+ return None
306
+ try:
307
+ path = Path(transcript_path)
308
+ if not path.is_file():
309
+ return None
310
+ # v7.7.18 council fix (Opus 2): cap transcript file size at 50 MB
311
+ # so a runaway transcript cannot OOM the ingester. Long sessions
312
+ # are rare; 50 MB is ~100k tool_use entries which is plenty.
313
+ try:
314
+ file_size = path.stat().st_size
315
+ if file_size > 50 * 1024 * 1024:
316
+ _log_to_errors(
317
+ memory_base,
318
+ "ingest_from_claude_transcript",
319
+ RuntimeError(
320
+ f"transcript too large ({file_size} bytes); skipping ingest"
321
+ ),
322
+ )
323
+ return None
324
+ except OSError:
325
+ pass
326
+ entries: List[Dict[str, Any]] = []
327
+ # Also cap entry count at 50k to bound memory regardless of file size.
328
+ MAX_ENTRIES = 50_000
329
+ with open(path, "r", encoding="utf-8", errors="replace") as f:
330
+ for line in f:
331
+ obj = _parse_transcript_line(line.strip())
332
+ if obj is not None:
333
+ entries.append(obj)
334
+ if len(entries) >= MAX_ENTRIES:
335
+ break
336
+ if not entries:
337
+ return None
338
+
339
+ tool_uses = _extract_tool_uses(entries)
340
+ files_read, files_modified = _extract_files(tool_uses)
341
+ start_ts, end_ts = _derive_timestamps(entries)
342
+ goal = _derive_goal(entries)
343
+ duration = 0
344
+ if start_ts and end_ts:
345
+ duration = max(0, int((end_ts - start_ts).total_seconds()))
346
+
347
+ # Derive task_id from transcript metadata or filename
348
+ if task_id is None:
349
+ session_id = None
350
+ for entry in entries:
351
+ sid = entry.get("sessionId") or entry.get("session_id")
352
+ if sid:
353
+ session_id = str(sid)
354
+ break
355
+ if not session_id:
356
+ session_id = path.stem
357
+ task_id = f"claude-session-{session_id[:12]}"
358
+
359
+ # Lazy imports
360
+ from memory.engine import MemoryEngine, create_storage
361
+ from memory.schemas import EpisodeTrace
362
+
363
+ storage = create_storage(memory_base)
364
+ engine = MemoryEngine(storage=storage, base_path=memory_base)
365
+ engine.initialize()
366
+
367
+ trace = EpisodeTrace.create(
368
+ task_id=task_id,
369
+ agent=agent,
370
+ phase=phase,
371
+ goal=goal,
372
+ )
373
+ trace.outcome = outcome
374
+ trace.duration_seconds = duration
375
+ # v7.7.18 council fix: apply BOTH scrubbers to file paths --
376
+ # _scrub catches inline credentials, _scrub_path catches sensitive
377
+ # paths the whitespace tokenizer misses (~/.aws/, .env, etc.).
378
+ trace.files_read = [_scrub_path(_scrub(p)) for p in files_read]
379
+ trace.files_modified = [_scrub_path(_scrub(p)) for p in files_modified]
380
+ trace.action_log = _build_action_log(tool_uses, start_ts)
381
+
382
+ engine.store_episode(trace)
383
+ # storage.py:439-442 writes to episodic/<date>/task-<id>.json.
384
+ # Reconstruct that path here so the caller gets the real file.
385
+ date_str = datetime.now(timezone.utc).strftime("%Y-%m-%d")
386
+ return str(Path(memory_base) / "episodic" / date_str / f"task-{trace.id}.json")
387
+ except Exception as e:
388
+ _log_to_errors(memory_base, "ingest_from_claude_transcript", e)
389
+ return None
390
+
391
+
392
+ def ingest_from_summary(
393
+ memory_base: str,
394
+ *,
395
+ goal: str,
396
+ outcome: str = "success",
397
+ files_modified: Optional[List[str]] = None,
398
+ files_read: Optional[List[str]] = None,
399
+ tool_calls_summary: Optional[str] = None,
400
+ task_id: Optional[str] = None,
401
+ agent: str = "claude-code-mcp",
402
+ phase: str = "INTERACTIVE",
403
+ duration_seconds: int = 0,
404
+ ) -> Optional[str]:
405
+ """Build an Episode from explicit summary fields.
406
+
407
+ Called by the MCP capture tool when the agent voluntarily reports
408
+ iteration close. Pre-validated inputs; minimal heuristics.
409
+
410
+ Returns episode path on success, None on failure.
411
+ """
412
+ if _capture_disabled():
413
+ return None
414
+ try:
415
+ from memory.engine import MemoryEngine, create_storage
416
+ from memory.schemas import EpisodeTrace, ActionEntry
417
+
418
+ storage = create_storage(memory_base)
419
+ engine = MemoryEngine(storage=storage, base_path=memory_base)
420
+ engine.initialize()
421
+
422
+ if task_id is None:
423
+ ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%S")
424
+ task_id = f"mcp-capture-{ts}"
425
+
426
+ trace = EpisodeTrace.create(
427
+ task_id=task_id,
428
+ agent=agent,
429
+ phase=phase,
430
+ goal=_scrub(goal)[:500],
431
+ )
432
+ trace.outcome = outcome if outcome in ("success", "failure", "partial") else "success"
433
+ trace.duration_seconds = max(0, int(duration_seconds))
434
+ trace.files_read = [_scrub_path(_scrub(p)) for p in (files_read or [])]
435
+ trace.files_modified = [_scrub_path(_scrub(p)) for p in (files_modified or [])]
436
+ if tool_calls_summary:
437
+ trace.action_log = [ActionEntry(
438
+ tool="mcp-summary",
439
+ input=_scrub(tool_calls_summary)[:1000],
440
+ output="",
441
+ timestamp=0,
442
+ )]
443
+ engine.store_episode(trace)
444
+ # storage.py:439-442 writes to episodic/<date>/task-<id>.json.
445
+ # Reconstruct that path here so the caller gets the real file.
446
+ date_str = datetime.now(timezone.utc).strftime("%Y-%m-%d")
447
+ return str(Path(memory_base) / "episodic" / date_str / f"task-{trace.id}.json")
448
+ except Exception as e:
449
+ _log_to_errors(memory_base, "ingest_from_summary", e)
450
+ return None
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loki-mode",
3
- "version": "7.7.16",
3
+ "version": "7.7.18",
4
4
  "description": "Loki Mode by Autonomi. Multi-agent autonomous SDLC framework. Spec to deployed app: PRD, GitHub issue, OpenAPI/JSON/YAML, or one-line brief. 4 AI providers (Claude Code, OpenAI Codex, Cline, Aider). 11 quality gates.",
5
5
  "keywords": [
6
6
  "agent",