loki-mode 7.7.17 → 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.
package/SKILL.md CHANGED
@@ -3,7 +3,7 @@ name: loki-mode
3
3
  description: Multi-agent autonomous startup system. Triggers on "Loki Mode". Takes a spec (PRD, GitHub issue, OpenAPI doc, etc.) to deployed product with minimal human intervention. Requires --dangerously-skip-permissions flag.
4
4
  ---
5
5
 
6
- # Loki Mode v7.7.17
6
+ # Loki Mode v7.7.18
7
7
 
8
8
  **You are an autonomous agent. You make decisions. You do not ask questions. You do not stop.**
9
9
 
@@ -381,4 +381,4 @@ See `CHANGELOG.md` entries [7.5.7], [7.5.8], [7.5.13] for the per-fix list and r
381
381
 
382
382
  ---
383
383
 
384
- **v7.7.17 | [Autonomi](https://www.autonomi.dev/) flagship product | ~260 lines core**
384
+ **v7.7.18 | [Autonomi](https://www.autonomi.dev/) flagship product | ~260 lines core**
package/VERSION CHANGED
@@ -1 +1 @@
1
- 7.7.17
1
+ 7.7.18
package/autonomy/loki CHANGED
@@ -15034,6 +15034,88 @@ except Exception as e:
15034
15034
  echo " loki memory namespace stats"
15035
15035
  ;;
15036
15036
 
15037
+ ingest)
15038
+ # v7.7.18 capture wedge: ingest a Claude Code session transcript
15039
+ # into the project's .loki/memory/ store. Solves the diagnosis
15040
+ # root cause where memory only captured during `loki start`.
15041
+ #
15042
+ # Usage:
15043
+ # loki memory ingest --from-claude-transcript <path>
15044
+ # loki memory ingest --from-stdin (reads a JSON summary doc)
15045
+ shift # drop "ingest"
15046
+ local _ingest_mode=""
15047
+ local _ingest_path=""
15048
+ while [ $# -gt 0 ]; do
15049
+ case "$1" in
15050
+ --from-claude-transcript)
15051
+ _ingest_mode="transcript"
15052
+ _ingest_path="${2:-}"
15053
+ if [ $# -ge 2 ]; then shift 2; else shift; fi
15054
+ ;;
15055
+ --from-stdin)
15056
+ _ingest_mode="stdin"
15057
+ shift
15058
+ ;;
15059
+ -h|--help)
15060
+ echo "Usage: loki memory ingest --from-claude-transcript <path>"
15061
+ echo " loki memory ingest --from-stdin (JSON doc on stdin)"
15062
+ echo ""
15063
+ echo "Honors LOKI_MEMORY_CAPTURE_DISABLED=true (escape hatch)."
15064
+ return 0
15065
+ ;;
15066
+ *) shift ;;
15067
+ esac
15068
+ done
15069
+ if [ -z "$_ingest_mode" ]; then
15070
+ echo -e "${RED}Usage: loki memory ingest --from-claude-transcript <path> | --from-stdin${NC}"
15071
+ exit 1
15072
+ fi
15073
+ local _project_root_for_ingest="${SKILL_DIR:-$(pwd)}"
15074
+ local _target_memory_dir
15075
+ _target_memory_dir="$(pwd)/.loki/memory"
15076
+ mkdir -p "$_target_memory_dir" 2>/dev/null || true
15077
+ if [ "$_ingest_mode" = "transcript" ]; then
15078
+ if [ -z "$_ingest_path" ] || [ ! -f "$_ingest_path" ]; then
15079
+ echo -e "${RED}Transcript not found: $_ingest_path${NC}"
15080
+ exit 1
15081
+ fi
15082
+ PYTHONPATH="$_project_root_for_ingest" \
15083
+ python3 -c "
15084
+ import sys, json
15085
+ from memory.ingest import ingest_from_claude_transcript
15086
+ path = ingest_from_claude_transcript(sys.argv[1], sys.argv[2])
15087
+ print(json.dumps({'episode_path': path}))
15088
+ " "$_ingest_path" "$_target_memory_dir"
15089
+ else
15090
+ PYTHONPATH="$_project_root_for_ingest" \
15091
+ python3 -c "
15092
+ import sys, json
15093
+ from memory.ingest import ingest_from_summary
15094
+ doc = json.loads(sys.stdin.read())
15095
+ path = ingest_from_summary(
15096
+ sys.argv[1],
15097
+ goal=doc.get('goal',''),
15098
+ outcome=doc.get('outcome','success'),
15099
+ files_modified=doc.get('files_modified',[]),
15100
+ files_read=doc.get('files_read',[]),
15101
+ tool_calls_summary=doc.get('tool_calls_summary',''),
15102
+ duration_seconds=int(doc.get('duration_seconds',0) or 0),
15103
+ )
15104
+ print(json.dumps({'episode_path': path}))
15105
+ " "$_target_memory_dir"
15106
+ fi
15107
+ ;;
15108
+
15109
+ # NOTE (v7.7.18 council fix Opus 2): hook installer (enable-hook /
15110
+ # disable-hook) DEFERRED to v7.7.19. Claude Code SessionEnd schema
15111
+ # requires {matcher, hooks:[{type,command}]} nested format -- not
15112
+ # the {id, command} format originally drafted. Also only fires on
15113
+ # /clear (not normal exits), and payload is JSON on stdin (not the
15114
+ # $CLAUDE_TRANSCRIPT_PATH env var). The sample hook script at
15115
+ # claude/hooks/loki-session-end.sh ships for users who want to
15116
+ # install manually; the automated installer comes after we verify
15117
+ # the exact schema empirically against a real Claude Code install.
15118
+
15037
15119
  *)
15038
15120
  echo -e "${RED}Unknown memory command: $subcommand${NC}"
15039
15121
  echo "Run 'loki memory help' for usage."
@@ -7,7 +7,7 @@ Modules:
7
7
  control: Session control API (start/stop/pause/resume)
8
8
  """
9
9
 
10
- __version__ = "7.7.17"
10
+ __version__ = "7.7.18"
11
11
 
12
12
  # Expose the control app for easy import
13
13
  try:
@@ -2,7 +2,7 @@
2
2
 
3
3
  The flagship product of [Autonomi](https://www.autonomi.dev/). Complete installation instructions for all platforms and use cases.
4
4
 
5
- **Version:** v7.7.17
5
+ **Version:** v7.7.18
6
6
 
7
7
  ---
8
8
 
@@ -1,5 +1,5 @@
1
1
  // @bun
2
- var _7=Object.defineProperty;var I7=(K)=>K;function P7(K,$){this[K]=I7.bind(null,$)}var v=(K,$)=>{for(var Q in $)_7(K,Q,{get:$[Q],enumerable:!0,configurable:!0,set:P7.bind($,Q)})};var R=(K,$)=>()=>(K&&($=K(K=0)),$);var t=import.meta.require;var e1={};v(e1,{lokiDir:()=>P,homeLokiDir:()=>k1,findRepoRootForVersion:()=>N1,REPO_ROOT:()=>p});import{resolve as u,dirname as S1}from"path";import{fileURLToPath as L7}from"url";import{existsSync as J1}from"fs";import{homedir as R7}from"os";function E7(){let K=i1;for(let $=0;$<6;$++){if(J1(u(K,"VERSION"))&&J1(u(K,"autonomy/run.sh")))return K;let Q=S1(K);if(Q===K)break;K=Q}return u(i1,"..","..","..")}function N1(K){let $=K;for(let Q=0;Q<6;Q++){if(J1(u($,"VERSION"))&&J1(u($,"autonomy/run.sh")))return $;let X=S1($);if(X===$)break;$=X}return u(K,"..","..","..")}function P(){return process.env.LOKI_DIR??u(process.cwd(),".loki")}function k1(){return u(R7(),".loki")}var i1,p;var g=R(()=>{i1=S1(L7(import.meta.url));p=E7()});import{readFileSync as F7}from"fs";import{resolve as w7,dirname as x7}from"path";import{fileURLToPath as S7}from"url";function G1(){if(n!==null)return n;let K="7.7.17";if(typeof K==="string"&&K.length>0)return n=K,n;try{let $=x7(S7(import.meta.url)),Q=N1($);n=F7(w7(Q,"VERSION"),"utf-8").trim()}catch{n="unknown"}return n}var n=null;var D1=R(()=>{g()});var $0={};v($0,{runOrThrow:()=>N7,run:()=>S,commandVersion:()=>D7,commandExists:()=>h,ShellError:()=>C1});async function S(K,$={}){let Q=Bun.spawn({cmd:[...K],stdout:"pipe",stderr:"pipe",env:$.env?{...process.env,...$.env}:process.env,cwd:$.cwd}),X,Z;if($.timeoutMs&&$.timeoutMs>0)X=setTimeout(()=>{try{Q.kill("SIGTERM")}catch{}Z=setTimeout(()=>{try{Q.kill("SIGKILL")}catch{}},2000)},$.timeoutMs);try{let[W,z,q]=await Promise.all([new Response(Q.stdout).text(),new Response(Q.stderr).text(),Q.exited]);return{stdout:W,stderr:z,exitCode:q}}finally{if(X)clearTimeout(X);if(Z)clearTimeout(Z)}}async function N7(K,$={}){let Q=await S(K,$);if(Q.exitCode!==0)throw new C1(`command failed (${Q.exitCode}): ${K.join(" ")}`,Q.exitCode,Q.stdout,Q.stderr);return Q}async function h(K){let $=k7(K),Q=await S(["sh","-c",`command -v ${$}`],{timeoutMs:5000});if(Q.exitCode===0)return Q.stdout.trim()||null;return null}function k7(K){if(!/^[A-Za-z0-9._/-]+$/.test(K))throw Error(`refused to shell-escape suspect token: ${K}`);return K}async function D7(K,$="--version"){if(!await h(K))return null;let X=await S([K,$],{timeoutMs:5000});if(X.exitCode!==0)return null;return((X.stdout||X.stderr).split(/\r?\n/)[0]?.trim()??"")||null}var C1;var c=R(()=>{C1=class C1 extends Error{message;exitCode;stdout;stderr;constructor(K,$,Q,X){super(K);this.message=K;this.exitCode=$;this.stdout=Q;this.stderr=X;this.name="ShellError"}}});function l(K){return C7?"":K}var C7,E,b,F,T6,O,D,w,H;var a=R(()=>{C7=(process.env.NO_COLOR??"").length>0;E=l("\x1B[0;31m"),b=l("\x1B[0;32m"),F=l("\x1B[1;33m"),T6=l("\x1B[0;34m"),O=l("\x1B[0;36m"),D=l("\x1B[1m"),w=l("\x1B[2m"),H=l("\x1B[0m")});import{existsSync as c7}from"fs";async function i(){if(X1!==void 0)return X1;let K="/opt/homebrew/bin/python3.12";if(c7(K))return X1=K,K;let $=await h("python3.12");if($)return X1=$,$;let Q=await h("python3");return X1=Q,Q}async function s(K,$={}){let Q=await i();if(!Q)return{stdout:"",stderr:"python3 not found",exitCode:127};return S([Q,"-c",K],$)}var X1;var Z1=R(()=>{c()});var G0={};v(G0,{runStatus:()=>Q5});import{existsSync as k,readFileSync as W1,readdirSync as W0,statSync as H0}from"fs";import{resolve as x,basename as a7}from"path";async function r7(){if(await h("jq"))return!0;return process.stdout.write(`${E}Error: jq is required but not installed.${H}
2
+ var _7=Object.defineProperty;var I7=(K)=>K;function P7(K,$){this[K]=I7.bind(null,$)}var v=(K,$)=>{for(var Q in $)_7(K,Q,{get:$[Q],enumerable:!0,configurable:!0,set:P7.bind($,Q)})};var R=(K,$)=>()=>(K&&($=K(K=0)),$);var t=import.meta.require;var e1={};v(e1,{lokiDir:()=>P,homeLokiDir:()=>k1,findRepoRootForVersion:()=>N1,REPO_ROOT:()=>p});import{resolve as u,dirname as S1}from"path";import{fileURLToPath as L7}from"url";import{existsSync as J1}from"fs";import{homedir as R7}from"os";function E7(){let K=i1;for(let $=0;$<6;$++){if(J1(u(K,"VERSION"))&&J1(u(K,"autonomy/run.sh")))return K;let Q=S1(K);if(Q===K)break;K=Q}return u(i1,"..","..","..")}function N1(K){let $=K;for(let Q=0;Q<6;Q++){if(J1(u($,"VERSION"))&&J1(u($,"autonomy/run.sh")))return $;let X=S1($);if(X===$)break;$=X}return u(K,"..","..","..")}function P(){return process.env.LOKI_DIR??u(process.cwd(),".loki")}function k1(){return u(R7(),".loki")}var i1,p;var g=R(()=>{i1=S1(L7(import.meta.url));p=E7()});import{readFileSync as F7}from"fs";import{resolve as w7,dirname as x7}from"path";import{fileURLToPath as S7}from"url";function G1(){if(n!==null)return n;let K="7.7.18";if(typeof K==="string"&&K.length>0)return n=K,n;try{let $=x7(S7(import.meta.url)),Q=N1($);n=F7(w7(Q,"VERSION"),"utf-8").trim()}catch{n="unknown"}return n}var n=null;var D1=R(()=>{g()});var $0={};v($0,{runOrThrow:()=>N7,run:()=>S,commandVersion:()=>D7,commandExists:()=>h,ShellError:()=>C1});async function S(K,$={}){let Q=Bun.spawn({cmd:[...K],stdout:"pipe",stderr:"pipe",env:$.env?{...process.env,...$.env}:process.env,cwd:$.cwd}),X,Z;if($.timeoutMs&&$.timeoutMs>0)X=setTimeout(()=>{try{Q.kill("SIGTERM")}catch{}Z=setTimeout(()=>{try{Q.kill("SIGKILL")}catch{}},2000)},$.timeoutMs);try{let[W,z,q]=await Promise.all([new Response(Q.stdout).text(),new Response(Q.stderr).text(),Q.exited]);return{stdout:W,stderr:z,exitCode:q}}finally{if(X)clearTimeout(X);if(Z)clearTimeout(Z)}}async function N7(K,$={}){let Q=await S(K,$);if(Q.exitCode!==0)throw new C1(`command failed (${Q.exitCode}): ${K.join(" ")}`,Q.exitCode,Q.stdout,Q.stderr);return Q}async function h(K){let $=k7(K),Q=await S(["sh","-c",`command -v ${$}`],{timeoutMs:5000});if(Q.exitCode===0)return Q.stdout.trim()||null;return null}function k7(K){if(!/^[A-Za-z0-9._/-]+$/.test(K))throw Error(`refused to shell-escape suspect token: ${K}`);return K}async function D7(K,$="--version"){if(!await h(K))return null;let X=await S([K,$],{timeoutMs:5000});if(X.exitCode!==0)return null;return((X.stdout||X.stderr).split(/\r?\n/)[0]?.trim()??"")||null}var C1;var c=R(()=>{C1=class C1 extends Error{message;exitCode;stdout;stderr;constructor(K,$,Q,X){super(K);this.message=K;this.exitCode=$;this.stdout=Q;this.stderr=X;this.name="ShellError"}}});function l(K){return C7?"":K}var C7,E,b,F,T6,O,D,w,H;var a=R(()=>{C7=(process.env.NO_COLOR??"").length>0;E=l("\x1B[0;31m"),b=l("\x1B[0;32m"),F=l("\x1B[1;33m"),T6=l("\x1B[0;34m"),O=l("\x1B[0;36m"),D=l("\x1B[1m"),w=l("\x1B[2m"),H=l("\x1B[0m")});import{existsSync as c7}from"fs";async function i(){if(X1!==void 0)return X1;let K="/opt/homebrew/bin/python3.12";if(c7(K))return X1=K,K;let $=await h("python3.12");if($)return X1=$,$;let Q=await h("python3");return X1=Q,Q}async function s(K,$={}){let Q=await i();if(!Q)return{stdout:"",stderr:"python3 not found",exitCode:127};return S([Q,"-c",K],$)}var X1;var Z1=R(()=>{c()});var G0={};v(G0,{runStatus:()=>Q5});import{existsSync as k,readFileSync as W1,readdirSync as W0,statSync as H0}from"fs";import{resolve as x,basename as a7}from"path";async function r7(){if(await h("jq"))return!0;return process.stdout.write(`${E}Error: jq is required but not installed.${H}
3
3
  `),process.stdout.write(`Install with:
4
4
  `),process.stdout.write(` brew install jq (macOS)
5
5
  `),process.stdout.write(` apt install jq (Debian/Ubuntu)
@@ -585,4 +585,4 @@ Set LOKI_LEGACY_BASH=1 to force the bash CLI for every command.
585
585
  `),2}default:return process.stderr.write(`Unknown command: ${$}
586
586
  `),process.stderr.write(j7),2}}process.on("SIGINT",()=>process.exit(130));process.on("SIGTERM",()=>process.exit(143));var X6=await Q6(Bun.argv.slice(2));process.exit(X6);
587
587
 
588
- //# debugId=508CDF0AB125EA5664756E2164756E21
588
+ //# debugId=33B2B334220BB7A164756E2164756E21
package/mcp/__init__.py CHANGED
@@ -57,4 +57,4 @@ try:
57
57
  except ImportError:
58
58
  __all__ = ['mcp']
59
59
 
60
- __version__ = '7.7.17'
60
+ __version__ = '7.7.18'
package/mcp/server.py CHANGED
@@ -969,6 +969,94 @@ async def loki_metrics_efficiency() -> str:
969
969
  return json.dumps({"error": str(e)})
970
970
 
971
971
 
972
+ @mcp.tool()
973
+ async def loki_memory_capture_session_summary(
974
+ goal: str,
975
+ outcome: str = "success",
976
+ files_modified: Optional[List[str]] = None,
977
+ files_read: Optional[List[str]] = None,
978
+ tool_calls_summary: Optional[str] = None,
979
+ duration_seconds: int = 0,
980
+ ) -> str:
981
+ """v7.7.18 capture wedge: store an episode for the current agent session.
982
+
983
+ Call this voluntarily at iteration close (or session end) to write a
984
+ structured Episode into the project's .loki/memory/ store. Solves
985
+ the diagnosis root cause where memory only captured during `loki
986
+ start` sessions, missing all interactive Claude Code / Cursor /
987
+ Cline / Aider work.
988
+
989
+ Args:
990
+ goal: Short description of what the session tried to accomplish
991
+ (will be truncated to 500 chars, scrubbed for secrets).
992
+ outcome: One of "success" | "failure" | "partial". Default "success".
993
+ files_modified: List of file paths that were created or edited.
994
+ files_read: List of file paths that were read for context.
995
+ tool_calls_summary: Optional free-text summary of major actions
996
+ taken (truncated to 1000 chars, scrubbed).
997
+ duration_seconds: Approximate session duration. Default 0.
998
+
999
+ Returns:
1000
+ JSON: {"episode_path": "<path>"} on success, or
1001
+ {"error": "...", "disabled": true} if LOKI_MEMORY_CAPTURE_DISABLED
1002
+ env var blocks capture, or {"error": "..."} on failure.
1003
+ """
1004
+ _emit_tool_event_async(
1005
+ 'loki_memory_capture_session_summary', 'start',
1006
+ parameters={'goal_len': len(goal or ''), 'outcome': outcome,
1007
+ 'files_modified_count': len(files_modified or []),
1008
+ 'files_read_count': len(files_read or [])}
1009
+ )
1010
+ try:
1011
+ from memory.ingest import ingest_from_summary, _capture_disabled
1012
+
1013
+ if _capture_disabled():
1014
+ _emit_tool_event_async(
1015
+ 'loki_memory_capture_session_summary', 'complete',
1016
+ result_status='skipped', error='disabled via env'
1017
+ )
1018
+ return json.dumps({
1019
+ "error": "memory capture disabled via LOKI_MEMORY_CAPTURE_DISABLED",
1020
+ "disabled": True,
1021
+ })
1022
+
1023
+ base_path = safe_path_join('.loki', 'memory')
1024
+ path = ingest_from_summary(
1025
+ base_path,
1026
+ goal=goal,
1027
+ outcome=outcome,
1028
+ files_modified=files_modified,
1029
+ files_read=files_read,
1030
+ tool_calls_summary=tool_calls_summary,
1031
+ duration_seconds=duration_seconds,
1032
+ )
1033
+ if path is None:
1034
+ _emit_tool_event_async(
1035
+ 'loki_memory_capture_session_summary', 'complete',
1036
+ result_status='error', error='ingest_from_summary returned None'
1037
+ )
1038
+ return json.dumps({"error": "ingest failed (check .loki/memory/.errors.log)"})
1039
+ _emit_tool_event_async(
1040
+ 'loki_memory_capture_session_summary', 'complete',
1041
+ result_status='success', episode_path=path
1042
+ )
1043
+ return json.dumps({"episode_path": path})
1044
+ except PathTraversalError as e:
1045
+ logger.error(f"Path traversal attempt blocked: {e}")
1046
+ _emit_tool_event_async(
1047
+ 'loki_memory_capture_session_summary', 'complete',
1048
+ result_status='error', error='Access denied'
1049
+ )
1050
+ return json.dumps({"error": "Access denied"})
1051
+ except Exception as e:
1052
+ logger.error(f"loki_memory_capture_session_summary failed: {e}")
1053
+ _emit_tool_event_async(
1054
+ 'loki_memory_capture_session_summary', 'complete',
1055
+ result_status='error', error=str(e)
1056
+ )
1057
+ return json.dumps({"error": str(e)})
1058
+
1059
+
972
1060
  @mcp.tool()
973
1061
  async def loki_consolidate_memory(since_hours: int = 24) -> str:
974
1062
  """
@@ -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.17",
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",