loki-mode 7.7.17 → 7.7.19
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 +2 -2
- package/VERSION +1 -1
- package/autonomy/loki +82 -0
- package/dashboard/__init__.py +1 -1
- package/docs/INSTALLATION.md +1 -1
- package/loki-ts/dist/loki.js +28 -28
- package/mcp/__init__.py +1 -1
- package/mcp/server.py +88 -0
- package/memory/ingest.py +450 -0
- package/package.json +1 -1
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.
|
|
6
|
+
# Loki Mode v7.7.19
|
|
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.
|
|
384
|
+
**v7.7.19 | [Autonomi](https://www.autonomi.dev/) flagship product | ~260 lines core**
|
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
7.7.
|
|
1
|
+
7.7.19
|
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."
|
package/dashboard/__init__.py
CHANGED
package/docs/INSTALLATION.md
CHANGED
package/loki-ts/dist/loki.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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(
|
|
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(o!==null)return o;let K="7.7.19";if(typeof K==="string"&&K.length>0)return o=K,o;try{let $=x7(S7(import.meta.url)),Q=N1($);o=F7(w7(Q,"VERSION"),"utf-8").trim()}catch{o="unknown"}return o}var o=null;var D1=R(()=>{g()});var $0={};v($0,{runOrThrow:()=>N7,run:()=>k,commandVersion:()=>D7,commandExists:()=>h,ShellError:()=>C1});async function k(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 k(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 k(["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 k([K,$],{timeoutMs:5000});if(X.exitCode!==0)return null;return((X.stdout||X.stderr).split(/\r?\n/)[0]?.trim()??"")||null}var C1;var n=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 c(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=c("\x1B[0;31m"),b=c("\x1B[0;32m"),F=c("\x1B[1;33m"),T6=c("\x1B[0;34m"),O=c("\x1B[0;36m"),D=c("\x1B[1m"),w=c("\x1B[2m"),H=c("\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 k([Q,"-c",K],$)}var X1;var Z1=R(()=>{n()});var G0={};v(G0,{runStatus:()=>Q5});import{existsSync as N,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)
|
|
6
6
|
`),process.stdout.write(` yum install jq (RHEL/CentOS)
|
|
7
|
-
`),!1}function B1(K){if(!Number.isFinite(K)||K<=0)return!1;try{return process.kill(K,0),!0}catch{return!1}}function M1(K){if(!
|
|
7
|
+
`),!1}function B1(K){if(!Number.isFinite(K)||K<=0)return!1;try{return process.kill(K,0),!0}catch{return!1}}function M1(K){if(!N(K))return null;try{let $=W1(K,"utf-8").trim();if(!$)return null;let Q=Number.parseInt($,10);return Number.isFinite(Q)?Q:null}catch{return null}}function t7(K){let $=[],Q=M1(x(K,"loki.pid"));if(Q!==null&&B1(Q))$.push(`global:${Q}`);let X=x(K,"sessions");if(N(X)){let Z=[];try{Z=W0(X)}catch{Z=[]}for(let W of Z){let z=x(X,W);try{if(!H0(z).isDirectory())continue}catch{continue}let q=x(z,"loki.pid"),U=M1(q);if(U!==null&&B1(U))$.push(`${W}:${U}`)}}if(N(K)){let Z=[];try{Z=W0(K)}catch{Z=[]}for(let W of Z){if(!W.startsWith("run-")||!W.endsWith(".pid"))continue;let z=x(K,W);try{if(!H0(z).isFile())continue}catch{continue}let q=a7(W,".pid").slice(4),U=M1(z);if(U!==null&&B1(U)){if(!$.some((G)=>G.startsWith(`${q}:`)))$.push(`${q}:${U}`)}}}return $}async function q0(K,$){let Q=await k(["jq","-r",K,$]);if(Q.exitCode!==0)return null;return Q.stdout.trim()}function U0(K,$){try{let Q=W1(K,"utf-8"),Z=JSON.parse(Q)[$];if(typeof Z==="number"){if($==="budget_used"){let W=Math.round(Z*100)/100;if(Number.isInteger(W))return String(W);return String(W)}return String(Z)}if(Z===void 0||Z===null)return"0";return String(Z)}catch{return"0"}}function V0(K,$,Q){try{let X=W1(K,"utf-8"),W=JSON.parse(X)[$];if(typeof W==="number"&&Number.isFinite(W))return W;return Q}catch{return Q}}async function i7(){let K=P();if(!await r7())return 1;if(!N(K))return process.stdout.write(`${D}Loki Mode Status${H}
|
|
8
8
|
`),process.stdout.write(`
|
|
9
9
|
`),process.stdout.write(`${F}No active session found.${H}
|
|
10
10
|
`),process.stdout.write(`Loki Mode has not been initialized in this directory.
|
|
@@ -16,7 +16,7 @@ var _7=Object.defineProperty;var I7=(K)=>K;function P7(K,$){this[K]=I7.bind(null
|
|
|
16
16
|
`),process.stdout.write(`${w}Current directory: ${process.cwd()}${H}
|
|
17
17
|
`),0;process.stdout.write(`${D}Loki Mode Status${H}
|
|
18
18
|
`),process.stdout.write(`
|
|
19
|
-
`);let $="",Q=x(K,"state","provider");if(
|
|
19
|
+
`);let $="",Q=x(K,"state","provider");if(N(Q))try{$=W1(Q,"utf-8").trim()}catch{$=""}let X=$||process.env.LOKI_PROVIDER||"claude",Z="full features";switch(X){case"codex":case"aider":Z="degraded mode";break;case"cline":Z="near-full mode";break;default:Z="full features";break}process.stdout.write(`${O}Provider:${H} ${X} (${Z})
|
|
20
20
|
`),process.stdout.write(`${w} Switch with: loki provider set <claude|codex|cline|aider>${H}
|
|
21
21
|
`),process.stdout.write(`
|
|
22
22
|
`);let W=t7(K);if(W.length>0){process.stdout.write(`${b}Active Sessions: ${W.length}${H}
|
|
@@ -26,31 +26,31 @@ var _7=Object.defineProperty;var I7=(K)=>K;function P7(K,$){this[K]=I7.bind(null
|
|
|
26
26
|
`),process.stdout.write(`${w} Stop specific: loki stop <session-id>${H}
|
|
27
27
|
`),process.stdout.write(`${w} Stop all: loki stop${H}
|
|
28
28
|
`),process.stdout.write(`
|
|
29
|
-
`)}if(
|
|
29
|
+
`)}if(N(x(K,"PAUSE")))process.stdout.write(`${F}Status: PAUSED${H}
|
|
30
30
|
`),process.stdout.write(`${w} Resume with: loki resume${H}
|
|
31
31
|
`),process.stdout.write(`
|
|
32
|
-
`);else if(
|
|
32
|
+
`);else if(N(x(K,"STOP")))process.stdout.write(`${E}Status: STOPPED${H}
|
|
33
33
|
`),process.stdout.write(`${w} Clear with: loki resume${H}
|
|
34
34
|
`),process.stdout.write(`
|
|
35
|
-
`);let z=x(K,"STATUS.txt");if(
|
|
35
|
+
`);let z=x(K,"STATUS.txt");if(N(z)){process.stdout.write(`${O}Session Info:${H}
|
|
36
36
|
`);try{process.stdout.write(W1(z,"utf-8"))}catch{}process.stdout.write(`
|
|
37
|
-
`)}let q=x(K,"state","orchestrator.json");if(
|
|
37
|
+
`)}let q=x(K,"state","orchestrator.json");if(N(q)){process.stdout.write(`${O}Orchestrator State:${H}
|
|
38
38
|
`);let J=await q0('.currentPhase // "unknown"',q);process.stdout.write(`${J??"unknown"}
|
|
39
|
-
`)}let U=x(K,"queue","pending.json");if(
|
|
40
|
-
`)}let V=x(K,"metrics","budget.json");if(
|
|
39
|
+
`)}let U=x(K,"queue","pending.json");if(N(U)){let J=await q0('if type == "array" then length elif .tasks then .tasks | length else 0 end',U);process.stdout.write(`${O}Pending Tasks:${H} ${J??"0"}
|
|
40
|
+
`)}let V=x(K,"metrics","budget.json");if(N(V)){let J=U0(V,"budget_limit"),Y=U0(V,"budget_used");if(J!=="0")process.stdout.write(`${O}Budget:${H} $${Y} / $${J}
|
|
41
41
|
`);else process.stdout.write(`${O}Cost:${H} $${Y} (no limit)
|
|
42
|
-
`)}let G=x(K,"state","context-usage.json");if(
|
|
43
|
-
`)}let B=x(K,"dashboard","dashboard.pid");if(
|
|
42
|
+
`)}let G=x(K,"state","context-usage.json");if(N(G)){let J=V0(G,"window_size",200000),Y=V0(G,"used_tokens",0),T=0;if(J>0)T=Math.floor(Y*100/J);process.stdout.write(`${O}Context:${H} ${T}% (${Y} / ${J} tokens)
|
|
43
|
+
`)}let B=x(K,"dashboard","dashboard.pid");if(N(B)){let J=M1(B);if(J!==null&&B1(J)){let Y=process.env.LOKI_DASHBOARD_PORT||"57374";process.stdout.write(`${O}Dashboard:${H} http://127.0.0.1:${Y}/
|
|
44
44
|
`)}}return await e7(K),process.stdout.write(`
|
|
45
45
|
`),process.stdout.write(`${w} Tip: loki context show - detailed token breakdown${H}
|
|
46
46
|
`),process.stdout.write(`${w} Tip: loki code overview - codebase intelligence${H}
|
|
47
|
-
`),0}async function e7(K){let $=x(K,"state"),Q=K5($),X=x($,"relevant-learnings.json"),Z=x(K,"escalations"),W=Q.length>0,z=
|
|
47
|
+
`),0}async function e7(K){let $=x(K,"state"),Q=K5($),X=x($,"relevant-learnings.json"),Z=x(K,"escalations"),W=Q.length>0,z=N(X),q=N(Z);if(!W&&!z&&!q)return;if(process.stdout.write(`
|
|
48
48
|
${O}Phase 1 artifacts:${H}
|
|
49
49
|
`),W){let U=Q[Q.length-1],V=J0(U);if(V&&Array.isArray(V.findings)){let G={Critical:0,High:0,Medium:0,Low:0};for(let J of V.findings){let Y=String(J.severity??"");if(Y in G)G[Y]=(G[Y]??0)+1}let B=Object.entries(G).filter(([,J])=>J>0).map(([J,Y])=>`${Y} ${J.toLowerCase()}`).join(", ");process.stdout.write(` Findings (iter ${V.iteration??"?"}): ${B||"none"} -- ${V.findings.length} total
|
|
50
50
|
`)}}if(z){let U=J0(X);if(U&&Array.isArray(U.learnings)&&U.learnings.length>0){let V=new Map;for(let B of U.learnings){let J=String(B.trigger??"unknown");V.set(J,(V.get(J)??0)+1)}let G=[...V.entries()].sort((B,J)=>J[1]-B[1]).slice(0,3).map(([B,J])=>`${J} ${B}`).join(", ");process.stdout.write(` Learnings: ${U.learnings.length} total (${G})
|
|
51
51
|
`)}}if(q){let U=0,V="";try{let B=(await import("fs")).readdirSync(Z).filter((J)=>J.endsWith(".md"));if(U=B.length,B.length>0)B.sort(),V=B[B.length-1]??""}catch{}if(U>0)process.stdout.write(` Escalations: ${U} handoff doc${U===1?"":"s"} (latest: ${V})
|
|
52
|
-
`)}}function K5(K){if(!
|
|
53
|
-
`),1;let $=p,Q=P(),X=process.env.LOKI_DASHBOARD_PORT||"57374",Z=process.env.LOKI_PROVIDER||"claude",W=await
|
|
52
|
+
`)}}function K5(K){if(!N(K))return[];try{return t("fs").readdirSync(K).filter((X)=>/^findings-\d+\.json$/.test(X)).sort((X,Z)=>{let W=Number.parseInt(X.replace(/[^0-9]/g,""),10)||0,z=Number.parseInt(Z.replace(/[^0-9]/g,""),10)||0;return W-z}).map((X)=>x(K,X))}catch{return[]}}function J0(K){try{let $=t("fs");return JSON.parse($.readFileSync(K,"utf-8"))}catch{return null}}async function $5(){let K=await i();if(!K)return process.stderr.write(`{"error": "Failed to generate JSON status. Ensure python3 is available."}
|
|
53
|
+
`),1;let $=p,Q=P(),X=process.env.LOKI_DASHBOARD_PORT||"57374",Z=process.env.LOKI_PROVIDER||"claude",W=await k([K,"-c",s7,$,Q,X,Z],{timeoutMs:30000});if(W.exitCode!==0)return process.stderr.write(`{"error": "Failed to generate JSON status. Ensure python3 is available."}
|
|
54
54
|
`),1;return process.stdout.write(W.stdout),0}async function Q5(K){let $=[...K];while($.length>0){let Q=$[0];if(Q==="--json")return $5();if(Q==="--help"||Q==="-h")return process.stdout.write(`Usage: loki status [--json]
|
|
55
55
|
`),0;return process.stdout.write(`${E}Unknown flag: ${Q}${H}
|
|
56
56
|
`),process.stdout.write(`Usage: loki status [--json]
|
|
@@ -309,10 +309,10 @@ if os.path.isfile(gate_count_file):
|
|
|
309
309
|
result['phase1'] = phase1
|
|
310
310
|
|
|
311
311
|
print(json.dumps(result, indent=2))
|
|
312
|
-
`;var B0=R(()=>{
|
|
312
|
+
`;var B0=R(()=>{n();Z1();a();g()});var T0={};v(T0,{runStats:()=>q5,computeStats:()=>O0});import{readdirSync as M0,readFileSync as X5,statSync as Y0}from"fs";import{join as l}from"path";function K1(K){try{if(!Y0(K).isFile())return null;return JSON.parse(X5(K,"utf-8"))}catch{return null}}function v1(K){try{return Y0(K).isDirectory()}catch{return!1}}function Z5(K){if(!v1(K))return[];try{let $=M0(K).filter((Q)=>Q.startsWith("iteration-")&&Q.endsWith(".json"));return $.sort(),$.map((Q)=>l(K,Q))}catch{return[]}}function $1(K){return Math.trunc(K).toLocaleString("en-US")}function b1(K){let $=Math.trunc(K);if($<60)return`${$}s`;let Q=Math.trunc($/3600),X=Math.trunc($%3600/60),Z=$%60;if(Q>0)return`${Q}h ${String(X).padStart(2,"0")}m`;return`${X}m ${String(Z).padStart(2,"0")}s`}function d(K,$=0){let Q=Math.pow(10,$);return Math.round(K*Q)/Q}function Q1(K,$){return K.toFixed($)}function y1(K,$){return K.length>=$?K:K+" ".repeat($-K.length)}function z5(K){let $="N/A",Q=0,X=K1(l(K,"state","orchestrator.json"));if(X&&typeof X==="object"){if(typeof X.currentPhase==="string")$=X.currentPhase;if(typeof X.currentIteration==="number")Q=X.currentIteration}let Z=l(K,"metrics","efficiency"),W=Z5(Z),z=[];for(let _ of W){let I=K1(_);if(I&&typeof I==="object")z.push(I)}if(z.length>0)Q=Math.max(Q,z.length);let q=z.reduce((_,I)=>_+(I.input_tokens??0),0),U=z.reduce((_,I)=>_+(I.output_tokens??0),0),V=q+U,G=z.reduce((_,I)=>_+(I.cost_usd??0),0),B=z.reduce((_,I)=>_+(I.duration_seconds??0),0),J=0,Y=0,T=K1(l(K,"metrics","budget.json"));if(T&&typeof T==="object"){if(typeof T.budget_limit==="number")J=T.budget_limit;if(typeof T.budget_used==="number")Y=T.budget_used}let j=0,C=0,S=K1(l(K,"state","quality-gates.json"));if(S&&typeof S==="object"){if(Array.isArray(S)){for(let _ of S)if(C+=1,_===!0)j+=1;else if(_&&typeof _==="object"){let I=_;if(I.passed===!0||I.status==="passed")j+=1}}else for(let _ of Object.values(S))if(typeof _==="boolean"){if(C+=1,_)j+=1}else if(_&&typeof _==="object"){C+=1;let I=_;if(I.passed===!0||I.status==="passed")j+=1}}let y={},A=K1(l(K,"quality","gate-failure-count.json"));if(A&&typeof A==="object"&&!Array.isArray(A)){let _={};for(let[I,m]of Object.entries(A))if(typeof m==="number")_[I]=m;y=_}let L=0,f=0,r1=0,x1=l(K,"quality");if(v1(x1)){let _=[];try{_=M0(x1)}catch{_=[]}for(let I of _){if(!I.endsWith(".json")||I==="gate-failure-count.json")continue;let m=K1(l(x1,I));if(!m||typeof m!=="object")continue;if(!(("verdict"in m)||("approved"in m)||("reviewers"in m)))continue;L+=1;let t1=(m.verdict??"").toString().toLowerCase();if(m.approved===!0||["approved","approve","pass"].includes(t1))f+=1;else if(["revision","revise","changes_requested","reject"].includes(t1))r1+=1}}return{phase:$,iterationCount:Q,iterations:z,totalInput:q,totalOutput:U,totalTokens:V,totalCost:G,totalDuration:B,budgetLimit:J,budgetUsed:Y,gatesPassed:j,gatesTotal:C,gateFailures:y,reviewsTotal:L,reviewsApproved:f,reviewsRevision:r1}}function W5(K,$){let Q=K.iterationCount,X={session:{iterations:Q,duration_seconds:K.totalDuration,phase:K.phase},tokens:{input:K.totalInput,output:K.totalOutput,total:K.totalTokens,cost_usd:d(K.totalCost,2)},quality:{gates_passed:K.gatesPassed,gates_total:K.gatesTotal,reviews_total:K.reviewsTotal,reviews_approved:K.reviewsApproved,reviews_revision:K.reviewsRevision,gate_failures:K.gateFailures},efficiency:{avg_tokens_per_iteration:Q>0?d(K.totalTokens/Q,0):0,avg_cost_per_iteration:Q>0?d(K.totalCost/Q,2):0,avg_duration_per_iteration:Q>0?d(K.totalDuration/Q,1):0},budget:{used:d(K.budgetUsed,2),limit:K.budgetLimit,percent:K.budgetLimit>0?d(K.budgetUsed/K.budgetLimit*100,1):0}};if($)X.iterations=K.iterations.map((z,q)=>({number:q+1,input_tokens:z.input_tokens??0,output_tokens:z.output_tokens??0,cost_usd:d(z.cost_usd??0,2),duration_seconds:z.duration_seconds??0}));let Z=JSON.stringify(X,null,2);function W(z,q){if(!q)return;let U=new RegExp(`("${z}": )(-?\\d+)(,?)$`,"m");Z=Z.replace(U,(V,G,B,J)=>`${G}${B}.0${J}`)}if(W("avg_duration_per_iteration",Q>0&&Number.isInteger(X.efficiency.avg_duration_per_iteration)),W("percent",K.budgetLimit>0&&Number.isInteger(X.budget.percent)),W("cost_usd",Q>0&&Number.isInteger(X.tokens.cost_usd)),$)Z=Z.replace(/("cost_usd": )(-?\d+)(,?)$/gm,(z,q,U,V)=>`${q}${U}.0${V}`);return Z}function H5(K,$){let Q=[];if(Q.push("Loki Mode Session Statistics"),Q.push("============================"),Q.push(""),Q.push("Session"),Q.push(` Iterations completed: ${K.iterationCount}`),Q.push(` Duration: ${b1(K.totalDuration)}`),Q.push(` Current phase: ${K.phase}`),Q.push(""),Q.push("Token Usage"),K.iterations.length>0)Q.push(` Input tokens: ${$1(K.totalInput)}`),Q.push(` Output tokens: ${$1(K.totalOutput)}`),Q.push(` Total tokens: ${$1(K.totalTokens)}`),Q.push(` Estimated cost: $${Q1(K.totalCost,2)}`);else Q.push(" N/A (no iteration metrics found)");if(Q.push(""),Q.push("Quality Gates"),K.gatesTotal>0){let X=Math.round(K.gatesPassed/K.gatesTotal*100);Q.push(` Gates passed: ${K.gatesPassed}/${K.gatesTotal} (${X}%)`)}else Q.push(" Gates passed: N/A");if(K.reviewsTotal>0){let X=[];if(K.reviewsApproved>0)X.push(`${K.reviewsApproved} approved`);if(K.reviewsRevision>0)X.push(`${K.reviewsRevision} revision requested`);let Z=X.length>0?X.join(", "):"N/A";Q.push(` Code reviews: ${K.reviewsTotal} (${Z})`)}if(Object.keys(K.gateFailures).length>0){let X=Object.entries(K.gateFailures).filter(([,Z])=>Z>0).map(([Z,W])=>`${Z} (${W})`);if(X.length>0)Q.push(` Gate failures: ${X.join(", ")}`)}if(Q.push(""),Q.push("Efficiency"),K.iterationCount>0&&K.iterations.length>0){let X=Math.round(K.totalTokens/K.iterationCount),Z=K.totalCost/K.iterationCount,W=K.totalDuration/K.iterationCount;Q.push(` Avg tokens/iteration: ${$1(X)}`),Q.push(` Avg cost/iteration: $${Q1(Z,2)}`),Q.push(` Avg duration/iteration: ${b1(W)}`)}else Q.push(" N/A (no iteration metrics found)");if(Q.push(""),Q.push("Budget"),K.budgetLimit>0){let X=d(K.budgetUsed/K.budgetLimit*100,1),Z=Number.isInteger(X)?`${X}.0`:`${X}`;Q.push(` Used: $${Q1(K.budgetUsed,2)} / $${Q1(K.budgetLimit,2)} (${Z}%)`)}else if(K.budgetUsed>0)Q.push(` Used: $${Q1(K.budgetUsed,2)} (no limit set)`);else Q.push(" N/A");if($&&K.iterations.length>0)Q.push(""),Q.push("Per-Iteration Breakdown"),K.iterations.forEach((X,Z)=>{let W=Z+1,z=y1($1(X.input_tokens??0),10),q=y1($1(X.output_tokens??0),10),U=X.cost_usd??0,V=b1(X.duration_seconds??0),G=y1(`${W}`,3);Q.push(` #${G} input: ${z} output: ${q} cost: $${Q1(U,2)} time: ${V}`)});return Q.join(`
|
|
313
313
|
`)}function O0(K){let $=!1,Q=!1;for(let z of K)if(z==="--json")$=!0;else if(z==="--efficiency")Q=!0;let X=P();if(!v1(X)){if($)return{exitCode:0,stdout:'{"error": "No active session"}'};return{exitCode:0,stdout:`${F}No active session found.${H}
|
|
314
|
-
Start a session with: loki start <prd>`}}let Z=z5(X);return{exitCode:0,stdout:$?W5(Z,Q):H5(Z,Q)}}async function q5(K){let $=O0(K);return console.log($.stdout),$.exitCode}var A0=R(()=>{g();a()});var w0={};v(w0,{runDoctor:()=>P5,pythonImportOk:()=>f1,httpReachable:()=>g1,checkTool:()=>L0,checkSkills:()=>R0,checkDisk:()=>m1,buildDoctorJson:()=>F0,_setPythonImportOkForTest:()=>Y5});import{existsSync as U5,lstatSync as V5,readlinkSync as J5,statfsSync as G5}from"fs";import{homedir as _0}from"os";import{resolve as j0}from"path";function M5(K){let $=K.match(B5);return $?$[1]:null}async function I0(K){try{let $=await
|
|
315
|
-
`);if(J>0&&
|
|
314
|
+
Start a session with: loki start <prd>`}}let Z=z5(X);return{exitCode:0,stdout:$?W5(Z,Q):H5(Z,Q)}}async function q5(K){let $=O0(K);return console.log($.stdout),$.exitCode}var A0=R(()=>{g();a()});var w0={};v(w0,{runDoctor:()=>P5,pythonImportOk:()=>f1,httpReachable:()=>g1,checkTool:()=>L0,checkSkills:()=>R0,checkDisk:()=>m1,buildDoctorJson:()=>F0,_setPythonImportOkForTest:()=>Y5});import{existsSync as U5,lstatSync as V5,readlinkSync as J5,statfsSync as G5}from"fs";import{homedir as _0}from"os";import{resolve as j0}from"path";function M5(K){let $=K.match(B5);return $?$[1]:null}async function I0(K){try{let $=await k([K,"--version"],{timeoutMs:5000}),Q=($.stdout||$.stderr||"").trim();return M5(Q)}catch{return null}}function P0(K,$){let Q=K.split(".").map((Z)=>parseInt(Z,10)),X=$.split(".").map((Z)=>parseInt(Z,10));while(Q.length<2)Q.push(0);while(X.length<2)X.push(0);for(let Z=0;Z<2;Z++){let W=Q[Z]??0,z=X[Z]??0;if(Number.isNaN(W)||Number.isNaN(z))return 0;if(W!==z)return W-z}return 0}async function L0(K,$,Q,X=null){let Z=await h($),W=Z!==null,z=W?await I0($):null,q="pass";if(!W)q=Q==="required"?"fail":"warn";else if(X&&z){if(P0(z,X)<0)q=Q==="required"?"fail":"warn"}return{name:K,command:$,found:W,version:z,required:Q,min_version:X,status:q,path:Z}}function m1(){let K=null;try{let Q=G5(_0()),X=Number(Q.bavail)*Number(Q.bsize);K=Math.round(X/1073741824*10)/10}catch{K=null}let $="pass";if(K!==null){if(K<1)$="fail";else if(K<5)$="warn"}return{available_gb:K,status:$}}async function g1(K,$=2000){try{return(await fetch(K,{signal:AbortSignal.timeout($)})).ok}catch{return!1}}async function f1(K,$=!1){let Q=`import ${K}`,X=$?30000:5000;if(!$)return(await s(Q,{timeoutMs:X})).exitCode===0;let Z=await i();if(!Z)return!1;return(await k([Z,"-c",Q],{timeoutMs:X})).exitCode===0}function Y5(K){T1.fn=K??f1}function R0(){let K=_0();return O5.map(({name:$,dir:Q})=>{let X=j0(K,Q),Z=X,W=j0(X,"SKILL.md");if(U5(W))return{name:$,path:Z,status:"pass",detail:""};try{if(V5(X).isSymbolicLink()){let q="unknown";try{q=J5(X)}catch{}return{name:$,path:Z,status:"fail",detail:`(broken symlink -> ${q})`}}}catch{}return{name:$,path:Z,status:"warn",detail:"(not found - run 'loki setup-skill')"}})}async function E0(){return Promise.all(T5.map(async(K)=>{return{...await L0(K.jsonName,K.cmd,K.required,K.min??null),displayName:K.displayName}}))}async function A5(){let $=await h("sentrux")!==null,Q=$?await I0("sentrux"):null;return{found:$,version:Q,status:$?"pass":"warn",required:"optional"}}async function j5(){let{openSync:K,statSync:$,readSync:Q,closeSync:X,existsSync:Z}=await import("fs"),{join:W}=await import("path"),z=65536,q=process.env.LOKI_DIR??".loki",U=W(q,"memory",".errors.log"),V=[],G=!1;try{if(Z(U)){G=!0;let B=$(U).size,J=Math.max(0,B-65536),Y=B-J,T=Buffer.alloc(Y),j=K(U,"r");try{Q(j,T,0,Y,J)}finally{X(j)}let S=T.toString("utf-8").split(`
|
|
315
|
+
`);if(J>0&&S.length>0)S=S.slice(1);S=S.map((y)=>y.trim()).filter((y)=>y.length>0),V=S.slice(-5)}}catch{V=[]}return{errors_log_path:G?U:null,recent_errors:V,recent_error_count:V.length,status:V.length===0?"pass":"warn"}}async function F0(){let $=(await E0()).map(({displayName:U,...V})=>V),Q=m1(),X=await A5(),Z=await j5(),W=0,z=0,q=0;for(let U of $)if(U.status==="pass")W++;else if(U.status==="fail")z++;else q++;if(Q.status==="pass")W++;else if(Q.status==="fail")z++;else q++;return{loki_mode_version:G1(),checks:$,disk:Q,sentrux:X,memory:Z,summary:{passed:W,failed:z,warnings:q,ok:z===0}}}function M(K){switch(K){case"pass":return`${b}PASS${H}`;case"fail":return`${E}FAIL${H}`;case"warn":return`${F}WARN${H}`}}function Y1(K){let $=K.version?` (v${K.version})`:"",Q=K.displayName;if(!K.found){let X=K.required==="required"?"not found":K.required==="recommended"?"not found (recommended)":"not found (optional)";return` ${M(K.status)} ${Q} - ${X}`}if(K.min_version&&K.version&&P0(K.version,K.min_version)<0){let X=K.required==="required"?"requires":"recommended";return` ${M(K.status)} ${Q}${$} - ${X} >= ${K.min_version}`}return` ${M(K.status)} ${Q}${$}`}function O1(K,$){if($==="pass")K.pass++;else if($==="fail")K.fail++;else K.warn++}function _5(){process.stdout.write(`${D}loki doctor${H} - Check system prerequisites
|
|
316
316
|
|
|
317
317
|
`),process.stdout.write(`Usage: loki doctor [--json]
|
|
318
318
|
|
|
@@ -363,7 +363,7 @@ Start a session with: loki start <prd>`}}let Z=z5(X);return{exitCode:0,stdout:$?
|
|
|
363
363
|
`),K.pass++;else process.stdout.write(` ${M("warn")} MiroFish - not running (loki start --mirofish-docker <image>)
|
|
364
364
|
`),K.warn++;if(process.env.LOKI_OTEL_ENDPOINT)process.stdout.write(` ${M("pass")} OTEL endpoint: ${process.env.LOKI_OTEL_ENDPOINT}
|
|
365
365
|
`),K.pass++;else process.stdout.write(` ${M("warn")} OTEL - not configured (set LOKI_OTEL_ENDPOINT)
|
|
366
|
-
`),K.warn++;if(await h("sentrux")){let A="unknown";try{let f=(await
|
|
366
|
+
`),K.warn++;if(await h("sentrux")){let A="unknown";try{let f=(await k(["sentrux","--version"],{timeoutMs:2000})).stdout.split(/\s+/).filter(Boolean).pop();if(f)A=f.replace(/^v/,"")}catch{}process.stdout.write(` ${M("pass")} sentrux ${A} (architectural drift gate: loki sentrux help)
|
|
367
367
|
`),K.pass++}else process.stdout.write(` ${M("warn")} sentrux - not installed (optional, brew install sentrux/tap/sentrux)
|
|
368
368
|
`),K.warn++;process.stdout.write(`
|
|
369
369
|
`),process.stdout.write(`${O}System:${H}
|
|
@@ -375,11 +375,11 @@ Start a session with: loki start <prd>`}}let Z=z5(X);return{exitCode:0,stdout:$?
|
|
|
375
375
|
`),K.warn++;else process.stdout.write(` ${M("pass")} Disk space: ${j}GB available
|
|
376
376
|
`),K.pass++;process.stdout.write(`
|
|
377
377
|
`),process.stdout.write(`${O}Runtime route:${H}
|
|
378
|
-
`);let C=process.versions.bun!==void 0,
|
|
378
|
+
`);let C=process.versions.bun!==void 0,S=process.argv[0]??"(unknown)";if(process.stdout.write(` ${M("pass")} Active runtime: ${C?"Bun":"Node"} (${S})
|
|
379
379
|
`),process.env.LOKI_LEGACY_BASH==="1"||process.env.LOKI_LEGACY_BASH==="true")process.stdout.write(` ${M("warn")} LOKI_LEGACY_BASH set: shim routes every command to autonomy/loki (bash)
|
|
380
380
|
`);if(process.env.LOKI_TS_ENTRY)process.stdout.write(` ${M("pass")} LOKI_TS_ENTRY override: ${process.env.LOKI_TS_ENTRY}
|
|
381
381
|
`);if(process.env.BUN_FROM_SOURCE==="1"||process.env.BUN_FROM_SOURCE==="true")process.stdout.write(` ${M("pass")} BUN_FROM_SOURCE set: shim prefers loki-ts/src/ over dist/
|
|
382
|
-
`);let y=await i();if(y!==null){let L=(await
|
|
382
|
+
`);let y=await i();if(y!==null){let L=(await k([y,"-c","import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')"],{timeoutMs:5000})).stdout.trim();if(L.startsWith("3.12"))process.stdout.write(` ${M("pass")} Python 3.12 (chromadb / sentence-transformers): ${L} at ${y}
|
|
383
383
|
`);else if(L)process.stdout.write(` ${M("warn")} Python 3.12 NOT found -- using ${L} at ${y}; chromadb / sentence-transformers may fail. Install python3.12 (brew install python@3.12 / apt install python3.12).
|
|
384
384
|
`);else process.stdout.write(` ${M("warn")} Python 3 found at ${y} but version probe failed; chromadb may not work.
|
|
385
385
|
`)}else process.stdout.write(` ${M("warn")} Python 3 not on PATH -- memory + MCP integrations disabled.
|
|
@@ -393,7 +393,7 @@ Start a session with: loki start <prd>`}}let Z=z5(X);return{exitCode:0,stdout:$?
|
|
|
393
393
|
`),0}async function P5(K){let $=!1;for(let Q of K)if(Q==="--json")$=!0;else if(Q==="--help"||Q==="-h")return _5(),0;else return process.stderr.write(`${E}Unknown option: ${Q}${H}
|
|
394
394
|
`),process.stderr.write(`Usage: loki doctor [--json]
|
|
395
395
|
`),1;if($){let Q=await F0();return process.stdout.write(JSON.stringify(Q,null,2)+`
|
|
396
|
-
`),0}return I5()}var B5,T1,O5,T5;var x0=R(()=>{
|
|
396
|
+
`),0}return I5()}var B5,T1,O5,T5;var x0=R(()=>{n();Z1();a();D1();B5=/(\d+\.\d+(?:\.\d+)*)/;T1={fn:f1};O5=[{name:"Claude Code",dir:".claude/skills/loki-mode"},{name:"Codex CLI",dir:".codex/skills/loki-mode"},{name:"Cline CLI",dir:".cline/skills/loki-mode"},{name:"Aider CLI",dir:".aider/skills/loki-mode"}];T5=[{displayName:"Node.js (>= 18)",jsonName:"Node.js",cmd:"node",required:"required",min:"18.0"},{displayName:"Python 3 (>= 3.8)",jsonName:"Python 3",cmd:"python3",required:"required",min:"3.8"},{displayName:"jq",jsonName:"jq",cmd:"jq",required:"required"},{displayName:"git",jsonName:"git",cmd:"git",required:"required"},{displayName:"curl",jsonName:"curl",cmd:"curl",required:"required"},{displayName:"bash (>= 4.0)",jsonName:"bash",cmd:"bash",required:"recommended",min:"4.0"},{displayName:"Bun (>= 1.3)",jsonName:"Bun",cmd:"bun",required:"recommended",min:"1.3"},{displayName:"Claude CLI",jsonName:"Claude CLI",cmd:"claude",required:"optional"},{displayName:"Codex CLI",jsonName:"Codex CLI",cmd:"codex",required:"optional"},{displayName:"Cline CLI",jsonName:"Cline CLI",cmd:"cline",required:"optional"},{displayName:"Aider CLI",jsonName:"Aider CLI",cmd:"aider",required:"optional"}]});import{existsSync as k0,mkdirSync as e6,readdirSync as L5,readFileSync as D0,renameSync as K8,writeFileSync as $8}from"fs";import{dirname as R5,join as E5,resolve as F5}from"path";import{fileURLToPath as w5}from"url";function x5(){try{let K=R5(w5(import.meta.url)),$=F5(K,"..","..","data","model-pricing.json");if(!k0($))return H1;let X=JSON.parse(D0($,"utf8")).pricing;if(!X||typeof X!=="object")return H1;let Z={};for(let[W,z]of Object.entries(X))if(z!==null&&typeof z==="object"&&typeof z.input==="number"&&typeof z.output==="number")Z[W]={input:z.input,output:z.output};for(let W of Object.keys(H1))if(!(W in Z))return H1;return Z}catch{return H1}}function S5(K){return Math.round((K+Number.EPSILON)*1e4)/1e4}function N5(K){let $=(K??N0).toLowerCase();return S0[$]??S0[N0]}function C0(K){let $=0;for(let Q of K){if(typeof Q.cost_usd==="number"&&Number.isFinite(Q.cost_usd)){$+=Q.cost_usd;continue}let X=N5(Q.model),Z=typeof Q.input_tokens==="number"?Q.input_tokens:0,W=typeof Q.output_tokens==="number"?Q.output_tokens:0;$+=Z/1e6*X.input+W/1e6*X.output}return S5($)}function h0(K){if(!k0(K))return[];let $=[],Q;try{Q=L5(K)}catch{return[]}for(let X of Q){if(!X.endsWith(".json"))continue;let Z=E5(K,X);try{let W=D0(Z,"utf8"),z=JSON.parse(W);if(z&&typeof z==="object")$.push(z)}catch{}}return $}var H1,S0,N0="sonnet";var b0=R(()=>{g();H1={opus:{input:5,output:25},sonnet:{input:3,output:15},haiku:{input:1,output:5},"gpt-5.3-codex":{input:1.5,output:12}};S0=Object.freeze(x5())});import{existsSync as A1,readdirSync as k5,readFileSync as D5,statSync as C5}from"fs";import{join as j1}from"path";function h5(K){let $=[],Q=j1(K,"votes");if(!A1(Q))return $;let X;try{X=k5(Q)}catch{return $}for(let Z of X){if(!Z.startsWith("round-")||!Z.endsWith(".json"))continue;try{let W=j1(Q,Z);if(!C5(W).isFile())continue;let z=JSON.parse(D5(W,"utf8"));$.push({iteration:typeof z.iteration==="number"?z.iteration:void 0,verdict:typeof z.verdict==="string"?z.verdict:void 0,complete_votes:typeof z.complete_votes==="number"?z.complete_votes:void 0,total_members:typeof z.total_members==="number"?z.total_members:void 0,threshold:typeof z.threshold==="number"?z.threshold:void 0})}catch{}}return $}function b5(){return{iteration_count:0,total_cost_usd:0,avg_cost_per_iteration:null,total_input_tokens:0,total_output_tokens:0,total_duration_ms:0,avg_duration_ms_per_iteration:null,model_breakdown:{},phase_breakdown:{},status_breakdown:{}}}function y5(){return{council_rounds:0,unanimous_rate:null,approval_rate:null,iteration_success_rate:null}}function v5(K){let $=b5();if(K.length===0)return $;$.iteration_count=K.length,$.total_cost_usd=Math.round(C0(K)*1e4)/1e4;for(let Q of K){if(typeof Q.input_tokens==="number")$.total_input_tokens+=Q.input_tokens;if(typeof Q.output_tokens==="number")$.total_output_tokens+=Q.output_tokens;let X=Q;if(typeof X.duration_ms==="number")$.total_duration_ms+=X.duration_ms;if(typeof Q.model==="string")$.model_breakdown[Q.model]=($.model_breakdown[Q.model]??0)+1;if(typeof X.phase==="string")$.phase_breakdown[X.phase]=($.phase_breakdown[X.phase]??0)+1;if(typeof X.status==="string")$.status_breakdown[X.status]=($.status_breakdown[X.status]??0)+1}return $.avg_cost_per_iteration=Math.round($.total_cost_usd/$.iteration_count*1e4)/1e4,$.avg_duration_ms_per_iteration=Math.round($.total_duration_ms/$.iteration_count),$}function g5(K,$,Q){let X=y5();if(X.council_rounds=K.length,K.length>0){let Z=0,W=0;for(let z of K){if(typeof z.complete_votes==="number"&&typeof z.total_members==="number"&&z.total_members>0&&z.complete_votes===z.total_members)Z+=1;if(z.verdict==="COMPLETE")W+=1}X.unanimous_rate=Math.round(Z/K.length*1e4)/1e4,X.approval_rate=Math.round(W/K.length*1e4)/1e4}if(Q>0)X.iteration_success_rate=Math.round($/Q*1e4)/1e4;return X}function y0(K){let $=[],Q=j1(K,"metrics","efficiency"),X=j1(K,"council"),Z=A1(Q)?h0(Q):[];if(!A1(Q))$.push("no .loki/metrics/efficiency/ dir (efficiency KPIs zeroed)");else if(Z.length===0)$.push(".loki/metrics/efficiency/ exists but no iteration files found");let W=h5(X);if(!A1(X))$.push("no .loki/council/ dir (accuracy KPIs zeroed)");else if(W.length===0)$.push(".loki/council/ exists but no round-N.json files found");let z=v5(Z),q=z.status_breakdown.success??0,U=g5(W,q,z.iteration_count);return{schema_version:1,generated_at:new Date().toISOString(),loki_dir:K,efficiency:z,accuracy:U,notes:$}}function v0(K){return JSON.stringify(K,null,2)}function g0(K){let $=[];$.push(`Loki Mode KPIs (snapshot at ${K.generated_at})`),$.push(`Source: ${K.loki_dir}`),$.push(""),$.push("Efficiency"),$.push(` Iterations: ${K.efficiency.iteration_count}`),$.push(` Total cost USD: ${K.efficiency.total_cost_usd}`),$.push(` Avg cost per iter: ${K.efficiency.avg_cost_per_iteration??"n/a"}`),$.push(` Total input tokens: ${K.efficiency.total_input_tokens}`),$.push(` Total output tokens: ${K.efficiency.total_output_tokens}`),$.push(` Total duration (ms): ${K.efficiency.total_duration_ms}`),$.push(` Avg duration / iter: ${K.efficiency.avg_duration_ms_per_iteration??"n/a"}`);let Q=Object.entries(K.efficiency.model_breakdown).sort((W,z)=>W[0].localeCompare(z[0]));if(Q.length>0)$.push(` Model breakdown: ${Q.map(([W,z])=>`${W}=${z}`).join(", ")}`);let X=Object.entries(K.efficiency.phase_breakdown).sort((W,z)=>W[0].localeCompare(z[0]));if(X.length>0)$.push(` Phase breakdown: ${X.map(([W,z])=>`${W}=${z}`).join(", ")}`);let Z=Object.entries(K.efficiency.status_breakdown).sort((W,z)=>W[0].localeCompare(z[0]));if(Z.length>0)$.push(` Status breakdown: ${Z.map(([W,z])=>`${W}=${z}`).join(", ")}`);if($.push(""),$.push("Accuracy"),$.push(` Council rounds: ${K.accuracy.council_rounds}`),$.push(` Unanimous rate: ${K.accuracy.unanimous_rate??"n/a"}`),$.push(` Approval rate: ${K.accuracy.approval_rate??"n/a"}`),$.push(` Iter success rate: ${K.accuracy.iteration_success_rate??"n/a"}`),K.notes.length>0){$.push(""),$.push("Notes");for(let W of K.notes)$.push(` - ${W}`)}return $.join(`
|
|
397
397
|
`)}var m0=R(()=>{b0()});var f0={};v(f0,{runKpis:()=>f5});function f5(K){let $=!1;for(let X of K){if(X==="--help"||X==="-h"||X==="help")return process.stdout.write(m5),0;if(X==="--json"){$=!0;continue}return process.stderr.write(`loki kpis: unknown arg: ${X}
|
|
398
398
|
Run 'loki kpis --help' for usage.
|
|
399
399
|
`),1}let Q=y0(P());return process.stdout.write($?v0(Q)+`
|
|
@@ -418,8 +418,8 @@ iteration success rate.
|
|
|
418
418
|
This is the Phase K MVP -- read-only derivation. Per-iteration
|
|
419
419
|
emission, dashboard panel, and the loki-bench harness are deferred
|
|
420
420
|
follow-ups (see project_v7_5_18_arc_status.md).
|
|
421
|
-
`;var u0=R(()=>{m0();g()});import{closeSync as
|
|
422
|
-
`),p5(Q,K)}async function p0(K,$){let Q=_1.get(K)??Promise.resolve(),X=()=>{},Z=new Promise((z)=>{X=z}),W=Q.catch(()=>{}).then(()=>Z);_1.set(K,W);try{return await Q.catch(()=>{}),await $()}finally{if(X(),_1.get(K)===W)_1.delete(K)}}var d5=0,_1;var I1=R(()=>{_1=new Map});import{existsSync as P1,mkdirSync as o5,copyFileSync as n5,readFileSync as a5,readdirSync as s5,statSync as r5,writeFileSync as
|
|
421
|
+
`;var u0=R(()=>{m0();g()});import{closeSync as B8,fstatSync as M8,lstatSync as Y8,mkdirSync as u5,openSync as O8,readSync as T8,renameSync as p5,rmSync as A8,statSync as j8,unlinkSync as _8,writeFileSync as c5,writeSync as I8}from"fs";import{dirname as l5}from"path";function q1(K,$){u5(l5(K),{recursive:!0});let Q=`${K}.tmp.${process.pid}.${++d5}`;c5(Q,`${JSON.stringify($,null,2)}
|
|
422
|
+
`),p5(Q,K)}async function p0(K,$){let Q=_1.get(K)??Promise.resolve(),X=()=>{},Z=new Promise((z)=>{X=z}),W=Q.catch(()=>{}).then(()=>Z);_1.set(K,W);try{return await Q.catch(()=>{}),await $()}finally{if(X(),_1.get(K)===W)_1.delete(K)}}var d5=0,_1;var I1=R(()=>{_1=new Map});import{existsSync as P1,mkdirSync as o5,copyFileSync as n5,readFileSync as a5,readdirSync as s5,statSync as r5,writeFileSync as F8,renameSync as t5,appendFileSync as w8,rmSync as x8}from"fs";import{join as r,dirname as i5}from"path";function L1(K){return r(K,"state","checkpoints")}function KK(K){let $=L1(K);if(!P1($))return[];return s5($).filter((Q)=>Q.startsWith("cp-")).filter((Q)=>{try{return r5(r($,Q)).isDirectory()}catch{return!1}})}function $K(K){return[...K].sort(($,Q)=>{let X=c0($),Z=c0(Q);return X-Z})}function c0(K){let $=K.split("-");if($.length<3)return 0;let Q=$[$.length-1],X=Number.parseInt(Q??"0",10);return Number.isFinite(X)?X:0}function p1(K){let $=K??P(),Q=$K(KK($)),X=[];for(let Z of Q){let W=l0($,Z);if(W)X.push(W)}return X}function l0(K,$){let Q=r(L1(K),$,"metadata.json");if(!P1(Q))return null;try{let X=JSON.parse(a5(Q,"utf-8"));return QK(X,Q)}catch{return null}}function QK(K,$){let Q=zK(K,$);return Q.ok?Q.value:null}function zK(K,$){if(K===null||typeof K!=="object")return console.warn(`[checkpoint] invalid metadata at ${$}: not an object`),{ok:!1,reason:"invalid_type",field:"<root>"};let Q=K,X=["id","timestamp","task_id","task_description","git_sha","git_branch","provider","phase"];for(let Z of X){if(!(Z in Q))return console.warn(`[checkpoint] invalid metadata at ${$}: field "${Z}" missing`),{ok:!1,reason:"missing_field",field:Z};if(typeof Q[Z]!=="string")return console.warn(`[checkpoint] invalid metadata at ${$}: field "${Z}" not a string`),{ok:!1,reason:"invalid_type",field:Z}}if(!Object.prototype.hasOwnProperty.call(Q,"iteration"))return console.warn(`[checkpoint] invalid metadata at ${$}: field "iteration" missing`),{ok:!1,reason:"missing_field",field:"iteration"};if(typeof Q.iteration!=="number"||!Number.isFinite(Q.iteration))return console.warn(`[checkpoint] invalid metadata at ${$}: field "iteration" not a finite number`),{ok:!1,reason:"invalid_type",field:"iteration"};for(let Z of ZK){let W=Q[Z];if(XK.test(W))return console.warn(`[checkpoint] invalid metadata at ${$}: field "${Z}" contains control characters`),{ok:!1,reason:"control_chars",field:Z}}return{ok:!0,value:{id:Q.id,timestamp:Q.timestamp,iteration:Q.iteration,task_id:Q.task_id,task_description:Q.task_description,git_sha:Q.git_sha,git_branch:Q.git_branch,provider:Q.provider,phase:Q.phase}}}function c1(K,$){if(!WK.test(K))throw new d0(K);let Q=$??P(),X=r(L1(Q),K);if(!P1(X))throw new u1(K);let Z=l0(Q,K);if(!Z)throw new u1(K);return Z}function o0(K,$){let Q=c1(K,$),X=$??P(),Z=r(L1(X),K),W=[];for(let z of HK){let q=r(Z,z);if(!P1(q))continue;W.push({from:q,to:r(X,z)})}return{id:K,metadata:Q,restore:W}}function n0(K){let $=[],Q=0;for(let X of K.restore)try{o5(i5(X.to),{recursive:!0});let Z=`${X.to}.tmp.${process.pid}.${++e5}`;n5(X.from,Z),t5(Z,X.to),Q+=1}catch(Z){$.push(`${X.from} -> ${X.to}: ${Z.message}`)}return{restored:Q,errors:$}}var C8,e5=0,XK,ZK,WK,u1,d0,HK;var a0=R(()=>{g();n();I1();C8=Promise.resolve();XK=/[\x00-\x08\x0a-\x1f\x7f-\x9f]/,ZK=["id","task_id","git_sha","git_branch","provider","phase"];WK=/^[a-zA-Z0-9_-]+$/;u1=class u1 extends Error{id;constructor(K){super(`Checkpoint not found: ${K}`);this.id=K;this.name="CheckpointNotFoundError"}};d0=class d0 extends Error{id;constructor(K){super(`Invalid checkpoint ID: must be alphanumeric, hyphens, underscores only (got: ${K})`);this.id=K;this.name="InvalidCheckpointIdError"}};HK=["state/orchestrator.json","queue/pending.json","queue/completed.json","queue/in-progress.json","queue/current-task.json"]});var t0={};v(t0,{runRollback:()=>qK});async function qK(K){let $=K[0],Q=K.slice(1);if($===void 0||$==="help"||$==="--help"||$==="-h")return process.stdout.write(s0),$===void 0?1:0;switch($){case"list":{let X=[...p1()].reverse();if(X.length===0)return process.stdout.write(`${F}No checkpoints found.${H}
|
|
423
423
|
`),0;process.stdout.write(`${D}Checkpoints${H} (${X.length}, newest first):
|
|
424
424
|
`);for(let Z of X)process.stdout.write(` ${O}${Z.id}${H} iter=${Z.iteration} ${Z.git_branch||"(no branch)"}@${(Z.git_sha||"").slice(0,7)} ${Z.timestamp}
|
|
425
425
|
`);return 0}case"show":{let X=Q[0];if(!X)return process.stderr.write(`${E}Missing checkpoint id.${H} Use \`loki rollback list\`.
|
|
@@ -487,7 +487,7 @@ except Exception as e:
|
|
|
487
487
|
`),1}}async function iK(K){let $=s1(K[0]);if($===null)return process.stderr.write(`override: missing or invalid <iter>
|
|
488
488
|
`),2;let Q=P();try{let X=await Promise.resolve().then(() => (G7(),J7)),Z=X.loadCounterEvidence(Q,$);if(Z===null||Z.evidence.length===0)return process.stdout.write(`override: no counter-evidence for iter ${$} (skip)
|
|
489
489
|
`),0;let z=(await Promise.resolve().then(() => (E1(),d1))).loadPreviousFindings(Q,$),q=z.findings.filter((j)=>j.severity==="Critical"||j.severity==="High");if(q.length===0)return process.stdout.write(`override: no blocking findings for iter ${$} (skip)
|
|
490
|
-
`),0;let U=new Set(["duplicate-code-path","file-exists","test-passes","grep-miss","out-of-scope"]),V=async(j)=>{let C=U.has(j.evidence.proofType);return{judge:j.judge,verdict:C?"APPROVE_OVERRIDE":"REJECT_OVERRIDE",reasoning:C?`[stub] proofType=${j.evidence.proofType} trusted`:`[stub] proofType=${j.evidence.proofType} requires manual review`}},G=await X.runOverrideCouncil(q,Z,V);await X.recordOverrideOutcome(Q,$,G,q);let B=V1(Q,"quality","reviews");if(dK(B))try{let j=nK(B).filter((
|
|
490
|
+
`),0;let U=new Set(["duplicate-code-path","file-exists","test-passes","grep-miss","out-of-scope"]),V=async(j)=>{let C=U.has(j.evidence.proofType);return{judge:j.judge,verdict:C?"APPROVE_OVERRIDE":"REJECT_OVERRIDE",reasoning:C?`[stub] proofType=${j.evidence.proofType} trusted`:`[stub] proofType=${j.evidence.proofType} requires manual review`}},G=await X.runOverrideCouncil(q,Z,V);await X.recordOverrideOutcome(Q,$,G,q);let B=V1(Q,"quality","reviews");if(dK(B))try{let j=nK(B).filter((S)=>S.startsWith("review-")).sort(),C=j[j.length-1];if(C&&aK(V1(B,C)).isDirectory())q1(V1(B,C,`override-${$}.json`),{review_id:z.reviewId,iteration:$,approved_finding_ids:Array.from(G.approvedFindingIds),rejected_finding_ids:Array.from(G.rejectedFindingIds),votes:G.votes})}catch{}let J=G.approvedFindingIds.size,Y=G.rejectedFindingIds.size;if(Y===0&&J>0)process.stdout.write(`override: LIFTED -- ${J} approved, ${Y} rejected
|
|
491
491
|
`);else process.stdout.write(`override: BLOCKED -- ${J} approved, ${Y} rejected
|
|
492
492
|
`);return 0}catch(X){return process.stderr.write(`override: ${X.message}
|
|
493
493
|
`),1}}async function eK(K){let $=K[0],Q=Number.parseInt(K[1]??"0",10),X=s1(K[2]);if(!$||!Number.isFinite(Q)||X===null)return process.stderr.write(`handoff: usage: handoff <gate> <consecutive-failures> <iter>
|
|
@@ -503,7 +503,7 @@ Subcommands:
|
|
|
503
503
|
This command is invoked by autonomy/run.sh between iterations. Users
|
|
504
504
|
should not run it directly -- run \`loki start\` instead.
|
|
505
505
|
`,K6;var A7=R(()=>{g();I1();K6=a1});D1();function K0(){return process.stdout.write(`Loki Mode v${G1()}
|
|
506
|
-
`),0}
|
|
506
|
+
`),0}n();a();g();import{readFileSync as h7,existsSync as b7}from"fs";import{resolve as y7}from"path";var v7=["claude","codex","cline","aider"];function Q0(){let K=y7(P(),"state","provider");if(!b7(K))return"";try{return h7(K,"utf-8").trim()}catch{return""}}function g7(K,$){return K||$||process.env.LOKI_PROVIDER||"claude"}function m7(K){let $=Q0(),Q=g7(K,$);switch(process.stdout.write(`${D}Current Provider${H}
|
|
507
507
|
`),process.stdout.write(`
|
|
508
508
|
`),process.stdout.write(`${O}Provider:${H} ${Q}
|
|
509
509
|
`),Q){case"claude":process.stdout.write(`${b}Status:${H} Full features (subagents, parallel, MCP)
|
|
@@ -537,7 +537,7 @@ should not run it directly -- run \`loki start\` instead.
|
|
|
537
537
|
`),process.stdout.write(` loki provider list
|
|
538
538
|
`),process.stdout.write(` loki provider info codex
|
|
539
539
|
`),process.stdout.write(` loki provider models
|
|
540
|
-
`),0}async function X0(K){let $=K[0]??"show",Q=K.slice(1);switch($){case"show":case"current":return m7(Q[0]);case"list":return f7();case"set":case"info":case"models":return p7(["provider",$,...Q]);default:return u7()}}async function p7(K){let{run:$}=await Promise.resolve().then(() => (
|
|
540
|
+
`),0}async function X0(K){let $=K[0]??"show",Q=K.slice(1);switch($){case"show":case"current":return m7(Q[0]);case"list":return f7();case"set":case"info":case"models":return p7(["provider",$,...Q]);default:return u7()}}async function p7(K){let{run:$}=await Promise.resolve().then(() => (n(),$0)),{resolve:Q}=await import("path"),{REPO_ROOT:X}=await Promise.resolve().then(() => (g(),e1)),Z=Q(X,"autonomy","loki"),W=await $([Z,...K],{env:{LOKI_LEGACY_BASH:"1"},timeoutMs:3600000});return process.stdout.write(W.stdout),process.stderr.write(W.stderr),W.exitCode}a();g();Z1();import{existsSync as Z0,readFileSync as l7}from"fs";import{resolve as e}from"path";import{mkdir as d7}from"fs/promises";var z1=e(k1(),"learnings");function h1(K){if(!Z0(K))return 0;try{let $=l7(K,"utf-8"),Q=0;for(let X of $.split(`
|
|
541
541
|
`))if(X.includes('"description"'))Q++;return Q}catch{return 0}}async function o7(){await d7(z1,{recursive:!0});let K=h1(e(z1,"patterns.jsonl")),$=h1(e(z1,"mistakes.jsonl")),Q=h1(e(z1,"successes.jsonl"));return process.stdout.write(`${D}Cross-Project Learnings${H}
|
|
542
542
|
`),process.stdout.write(`
|
|
543
543
|
`),process.stdout.write(` Patterns: ${b}${K}${H}
|
|
@@ -559,7 +559,7 @@ except Exception as e:
|
|
|
559
559
|
print(f'Error: {e}')
|
|
560
560
|
`.trim(),Z=await s(X,{cwd:p});return process.stdout.write(Z.stdout),0}let $=e(P(),"memory","index.json");if(!Z0($))return process.stdout.write(`No index found
|
|
561
561
|
`),0;let Q=await s(`import json, sys; sys.stdout.write(json.dumps(json.load(open(${JSON.stringify($)})), indent=4) + "\\n")`);if(Q.exitCode!==0)return process.stdout.write(`No index found
|
|
562
|
-
`),0;return process.stdout.write(Q.stdout),0}async function z0(K){switch(K[0]??"list"){case"list":case"ls":return o7();case"index":return n7(K[1]==="rebuild");default:{let Q=e(p,"autonomy","loki"),X=
|
|
562
|
+
`),0;return process.stdout.write(Q.stdout),0}async function z0(K){switch(K[0]??"list"){case"list":case"ls":return o7();case"index":return n7(K[1]==="rebuild");default:{let Q=e(p,"autonomy","loki"),X=3600000,Z=Bun.spawn({cmd:[Q,"memory",...K],stdin:"inherit",stdout:"inherit",stderr:"inherit",env:{...process.env,LOKI_LEGACY_BASH:"1"}}),W=setTimeout(()=>{try{Z.kill("SIGKILL")}catch{}},3600000);try{return await Z.exited}finally{clearTimeout(W)}}}}var j7=`Loki Mode (TypeScript port, Phase 2 of bash->Bun migration)
|
|
563
563
|
|
|
564
564
|
Usage: loki <command> [args...]
|
|
565
565
|
|
|
@@ -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=
|
|
588
|
+
//# debugId=2A67FE71C904504464756E2164756E21
|
package/mcp/__init__.py
CHANGED
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
|
"""
|
package/memory/ingest.py
ADDED
|
@@ -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.
|
|
3
|
+
"version": "7.7.19",
|
|
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",
|