diff --git a/.gitignore b/.gitignore index 36626b5..bd156fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ dist/ *.egg-info/ +/target/ diff --git a/README.md b/README.md index d723342..74fec01 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,16 @@ The skill calls LORE's CLI against the shared store when a task needs recall. This skill-only setup does not install the automatic SessionStart hook; install the full Codex plugin for startup context injection. +### oh-my-pi + +[`omp/lore.ts`](omp/lore.ts) injects the shared user and project snapshot with the first prompt and after clear and compaction. It reads context only: no transcript indexing, review or tool evidence. `LORE_RS` overrides the carrier path. + +```sh +./task install +mkdir -p ~/.omp/agent/extensions +ln -s "$(pwd)/omp/lore.ts" ~/.omp/agent/extensions/lore.ts +``` + ## Memory and graph context Claude Code refreshes curated memory on prompt submission when its content diff --git a/omp/lore.ts b/omp/lore.ts new file mode 100644 index 0000000..2ffcfd8 --- /dev/null +++ b/omp/lore.ts @@ -0,0 +1,45 @@ +import type { ExtensionAPI } from "@oh-my-pi/pi-coding-agent"; + +const CARRIER = process.env.LORE_RS || "lore-rs"; +const HOOK_TIMEOUT_MS = 15_000; + +export default function lore(pi: ExtensionAPI): void { + let injected = false; + const reinject = () => { + injected = false; + }; + pi.on("session_start", reinject); + pi.on("session_switch", reinject); + pi.on("session_clear", reinject); + pi.on("session_compact", reinject); + + pi.on("before_agent_start", async (event, ctx) => { + if (event.reason === "revive") return; + const hookEvent = injected ? "prompt" : "session-start"; + // Set before awaiting: a compaction during the hook must still re-arm injection. + injected = true; + try { + const context = await runHook(hookEvent, { cwd: ctx.cwd, prompt: event.prompt ?? "" }); + if (context) return { message: { customType: "lore", content: context, display: false } }; + } catch (error) { + pi.logger.warn(`LORE ${hookEvent} hook failed: ${error}`); + } + }); +} + +async function runHook(event: string, input: object): Promise { + const child = Bun.spawn([CARRIER, "hook", "--engine", "omp", "--event", event], { + stdin: new Blob([JSON.stringify(input)]), + stdout: "pipe", + stderr: "pipe", + timeout: HOOK_TIMEOUT_MS, + }); + const [stdout, stderr, code] = await Promise.all([ + new Response(child.stdout).text(), + new Response(child.stderr).text(), + child.exited, + ]); + if (code !== 0) throw new Error(`${CARRIER} exited ${code}: ${stderr.trim()}`); + if (!stdout.trim()) return undefined; + return JSON.parse(stdout).hookSpecificOutput?.additionalContext; +} diff --git a/rust/lore-core/src/context.rs b/rust/lore-core/src/context.rs index 71d8576..5159005 100644 --- a/rust/lore-core/src/context.rs +++ b/rust/lore-core/src/context.rs @@ -137,6 +137,9 @@ fn freshness_rule(policy: RefreshPolicy, engine: &str) -> String { if engine == "codex" { return "- In Codex, this snapshot is injected at startup, resume, clear, and compaction. Writes land in the files immediately; read them during this session with lore-rs memory show.".into(); } + if engine == "omp" { + return "- In oh-my-pi, this snapshot is injected with the first prompt and again after clear and compaction. Writes land in the files immediately; read them during this session with lore-rs memory show.".into(); + } if policy.on_change { "- This snapshot re-injects on your NEXT prompt whenever its content changed (change-detected each prompt; LORE_REFRESH_ON_CHANGE=0 opts out); the refresh supersedes every earlier copy in the conversation.".into() } else if let Some(interval) = policy.interval_secs { diff --git a/rust/lore-core/src/standalone.rs b/rust/lore-core/src/standalone.rs index 94a3250..0c0e7c3 100644 --- a/rust/lore-core/src/standalone.rs +++ b/rust/lore-core/src/standalone.rs @@ -1250,7 +1250,7 @@ pub fn hook(cfg: &Config, args: &[String]) -> Result<()> { } let engine = options["engine"] .as_str() - .filter(|s| matches!(*s, "claude" | "codex")) + .filter(|s| matches!(*s, "claude" | "codex" | "omp")) .ok_or(Error::InvalidRequest)?; let event = options["event"] .as_str() @@ -1304,7 +1304,7 @@ pub fn hook(cfg: &Config, args: &[String]) -> Result<()> { return Ok(()); } let mut snapshot = crate::context::snapshot_for_engine(cfg, &req, engine)?; - if engine == "codex" { + if engine != "claude" { if let Ok(Value::String(graph)) = crate::context::graph_context_op(cfg, &req) { if !graph.is_empty() { let text = snapshot.as_str().ok_or(Error::InvalidRequest)?; @@ -1317,7 +1317,7 @@ pub fn hook(cfg: &Config, args: &[String]) -> Result<()> { ) } "prompt" => { - if engine == "codex" { + if engine != "claude" { if cfg.disabled("LORE_DISABLE_INJECT") { return Ok(()); } @@ -1346,7 +1346,7 @@ pub fn hook(cfg: &Config, args: &[String]) -> Result<()> { output(frame) } } - "pre-compact" | "session-end" => { + "pre-compact" | "session-end" if engine != "omp" => { let sid = session.ok_or(Error::InvalidRequest)?; let transcript = input["transcript_path"] .as_str() diff --git a/rust/lore-core/tests/omp_hooks.rs b/rust/lore-core/tests/omp_hooks.rs new file mode 100644 index 0000000..b9cc07d --- /dev/null +++ b/rust/lore-core/tests/omp_hooks.rs @@ -0,0 +1,51 @@ +use lore_core::{config::Config, files}; +use serde_json::{json, Value}; +use std::{ + io::Write, + path::Path, + process::{Command, Output, Stdio}, +}; + +fn hook(home: &Path, cfg: &Config, event: &str) -> Output { + let input = json!({"cwd":home,"session_id":"omp-fixture","prompt":"fixture"}); + let mut child = Command::new(env!("CARGO_BIN_EXE_lore-rs")) + .args(["hook", "--engine", "omp", "--event", event]) + .env_clear() + .env("HOME", home) + .env("LORE_ROOT", &cfg.root) + .env("LORE_PROJECTS_DIR", &cfg.projects) + .env("LORE_DISABLE_SYNC", "1") + .env("LORE_CLAUDE_BIN", "/provider/must/never/be/launched") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + write!(child.stdin.as_mut().unwrap(), "{input}").unwrap(); + drop(child.stdin.take()); + child.wait_with_output().unwrap() +} + +#[test] +fn omp_startup_injects_memory_and_refuses_transcript_review() { + let home = tempfile::tempdir().unwrap(); + let cfg = Config::for_root(home.path().join("store")); + files::private_dir(&cfg.root).unwrap(); + std::fs::write(cfg.root.join("USER.md"), "- Curated startup fixture\n").unwrap(); + + let output = hook(home.path(), &cfg, "session-start"); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + let frame: Value = serde_json::from_slice(&output.stdout).unwrap(); + assert_eq!(frame["hookSpecificOutput"]["hookEventName"], "SessionStart"); + let text = frame["hookSpecificOutput"]["additionalContext"] + .as_str() + .unwrap(); + assert!(text.contains("Curated startup fixture")); + assert!(text.contains("In oh-my-pi, this snapshot is injected")); + + assert!(!hook(home.path(), &cfg, "session-end").status.success()); +}