owlmem 0.9.35 → 0.9.36

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.
Files changed (2) hide show
  1. package/owlmem.mjs +79 -8
  2. package/package.json +1 -1
package/owlmem.mjs CHANGED
@@ -44,7 +44,7 @@ import { fileURLToPath } from "node:url";
44
44
  import { spawn, spawnSync } from "node:child_process";
45
45
  import { createConnection } from "node:net";
46
46
 
47
- export const VERSION = "0.9.35";
47
+ export const VERSION = "0.9.36";
48
48
  const BASE = (process.env.OWLMEM_URL || "https://owlmem.com").replace(/\/$/, "");
49
49
  const DIR = process.env.OWLMEM_HOME || join(process.env.XDG_CONFIG_HOME || join(homedir(), ".config"), "owlmem");
50
50
  const enc = new TextEncoder(), dec = new TextDecoder();
@@ -524,18 +524,27 @@ export const SESSION_START_CONTEXT = "Before reading any code or files, search O
524
524
  // `owlmem hook context-meter` shows the measured context length after each reply, so the
525
525
  // AI quotes a number instead of guessing one. It reads the hook JSON on stdin
526
526
  // (hook_event_name, transcript_path) and takes the length from the last non-sidechain
527
- // assistant usage in the transcript (input + cache read + cache creation). Stop prints
528
- // {"systemMessage": line}; UserPromptSubmit prints additionalContext. Anything else
529
- // prints nothing. Always offline, nothing on stderr, exit 0.
527
+ // assistant usage in the transcript (input + cache read + cache creation). A Codex rollout
528
+ // works too: the last token_count event's last_token_usage.input_tokens, with the window
529
+ // from that event (200k when it names none). Stop prints {"systemMessage": line};
530
+ // UserPromptSubmit prints additionalContext. Anything else prints nothing. Always
531
+ // offline, nothing on stderr, exit 0.
530
532
  function contextMeterLine(payload) {
531
533
  const p = payload?.transcript_path;
532
534
  if (typeof p !== "string" || !p) return null;
533
535
  let text;
534
536
  try { text = readFileSync(p, "utf8"); } catch { return null; }
535
- let tokens = null, over200k = false;
537
+ let tokens = null, over200k = false, codex = null;
536
538
  for (const line of text.split("\n")) {
537
539
  let e;
538
540
  try { e = JSON.parse(line); } catch { continue; } // junk lines are skipped
541
+ if (e?.type === "event_msg" && e?.payload?.type === "token_count") {
542
+ // A Codex rollout line: the context length is the last turn's input tokens (cached input
543
+ // is already inside input_tokens; output tokens don't count). Null info / zero: not a reading.
544
+ const info = e?.payload?.info, n = info?.last_token_usage?.input_tokens;
545
+ if (info && typeof n === "number" && n > 0) codex = { tokens: n, window: info.model_context_window || 200000 };
546
+ continue;
547
+ }
539
548
  if (e?.type !== "assistant" || e?.isSidechain) continue; // subagents don't count
540
549
  const u = e?.message?.usage;
541
550
  if (!u) continue;
@@ -544,8 +553,12 @@ function contextMeterLine(payload) {
544
553
  if (sum > 200000) over200k = true;
545
554
  tokens = sum; // keep the last one
546
555
  }
547
- if (tokens === null) return null;
548
- const window = over200k ? 1000000 : 200000;
556
+ let window;
557
+ if (codex) { tokens = codex.tokens; window = codex.window; } // a transcript is one app or the other
558
+ else {
559
+ if (tokens === null) return null;
560
+ window = over200k ? 1000000 : 200000;
561
+ }
549
562
  const n = Math.round(tokens / 1000), pct = Math.round(tokens * 100 / window);
550
563
  const icon = tokens < 80000 ? "🟢" : tokens < 150000 ? "🔵" : tokens < 180000 ? "🟡" : "🔴";
551
564
  const advice = tokens < 80000 ? "Keep going." : tokens < 150000 ? "You may clear at the next task boundary." : tokens < 180000 ? "Consider clearing, even mid-task." : "Hand off and clear now.";
@@ -805,6 +818,53 @@ export function planSessionStartHook(uninstall) {
805
818
  return { file, text: JSON.stringify(next, null, 2) + "\n" };
806
819
  }
807
820
 
821
+ // Codex hooks: $CODEX_HOME/hooks.json (default ~/.codex/hooks.json) has the same shape as
822
+ // Claude Code's settings.json "hooks". Codex gets only the context meter (Stop +
823
+ // UserPromptSubmit) — it has no SessionStart search-first hook. A hook written by a
824
+ // program does not run until the person trusts it in Codex with /hooks, so install says
825
+ // so; install never writes trust state (no trusted_hash / hooks.state in config.toml).
826
+ function codexHooksFile() {
827
+ return join(process.env.CODEX_HOME || join(home(), ".codex"), "hooks.json");
828
+ }
829
+ export function planCodexHooks(uninstall) {
830
+ const file = codexHooksFile();
831
+ const text = readText(file);
832
+ let obj = {};
833
+ if (text !== null && text.trim()) {
834
+ try { obj = JSON.parse(text); } catch { return null; } // bad JSON: leave alone
835
+ if (!obj || typeof obj !== "object" || Array.isArray(obj)) return null;
836
+ }
837
+ if (obj.hooks !== undefined && (typeof obj.hooks !== "object" || Array.isArray(obj.hooks) || obj.hooks === null)) return null;
838
+ const hooks = obj.hooks || {};
839
+ const events = [
840
+ { event: "Stop", ours: isContextMeterOurs, command: contextMeterCommand() },
841
+ { event: "UserPromptSubmit", ours: isContextMeterOurs, command: contextMeterCommand() },
842
+ ];
843
+ for (const { event } of events) {
844
+ if (hooks[event] !== undefined && !Array.isArray(hooks[event])) return null;
845
+ }
846
+ if (uninstall) {
847
+ if (!events.some(({ event, ours }) => (hooks[event] || []).some(ours))) return null;
848
+ const next = { ...obj, hooks: { ...hooks } };
849
+ for (const { event, ours } of events) {
850
+ if (hooks[event]) next.hooks[event] = hooks[event].filter((e) => !ours(e));
851
+ }
852
+ return { file, text: JSON.stringify(next, null, 2) + "\n" };
853
+ }
854
+ const next = { ...obj, hooks: { ...hooks } };
855
+ let changed = false;
856
+ for (const { event, ours, command } of events) {
857
+ const list = hooks[event] || [];
858
+ if (!list.some(ours)) {
859
+ next.hooks[event] = [...list, { hooks: [{ type: "command", command }] }];
860
+ changed = true;
861
+ }
862
+ }
863
+ if (!changed) return null; // already installed
864
+ return { file, text: JSON.stringify(next, null, 2) + "\n" };
865
+ }
866
+ const codexHooksPresent = () => (readText(codexHooksFile()) || "").includes("hook context-meter");
867
+
808
868
  export function plan(opts = {}) {
809
869
  const only = opts.only ? new Set(opts.only) : null;
810
870
  const out = [];
@@ -825,6 +885,14 @@ export function plan(opts = {}) {
825
885
  r.ops.push({ type: "write", file: hop.file, text: hop.text, hook: true });
826
886
  }
827
887
  }
888
+ if (c.id === "codex" && !opts.noRules && r.state !== "fail") {
889
+ const hop = planCodexHooks(opts.uninstall);
890
+ if (hop) {
891
+ if (!opts.uninstall) r.ops.unshift({ type: "copy", file: join(wakeDir(), "owlmem.mjs") });
892
+ r.ops.push({ type: "write", file: hop.file, text: hop.text, hook: true, codex: true });
893
+ }
894
+ if (!opts.uninstall) r.codexHooks = !!hop || codexHooksPresent();
895
+ }
828
896
  out.push(r);
829
897
  }
830
898
  return out;
@@ -873,7 +941,8 @@ function describe(r, done, uninstall) {
873
941
  for (const op of r.ops) {
874
942
  if (op.type === "run" && !done) lines.push(" run: " + op.argv.join(" "));
875
943
  if (op.rules) lines.push(` ${done ? "" : "will "}${uninstall ? (done ? "removed" : "remove") : (done ? "wrote" : "write")} the OwlMem rules block in ${tilde(op.file)}`);
876
- if (op.hook) lines.push(` ${done ? "" : "will "}${uninstall ? (done ? "removed" : "remove") : (done ? "added" : "add")} the SessionStart and context-meter hooks in ${tilde(op.file)}`);
944
+ if (op.hook && op.codex) lines.push(` ${done ? "" : "will "}${uninstall ? (done ? "removed" : "remove") : (done ? "added" : "add")} the context-meter hooks in ${tilde(op.file)} (Codex runs them only once you trust them with /hooks)`);
945
+ else if (op.hook) lines.push(` ${done ? "" : "will "}${uninstall ? (done ? "removed" : "remove") : (done ? "added" : "add")} the SessionStart and context-meter hooks in ${tilde(op.file)}`);
877
946
  }
878
947
  if (done && r.backups?.length) lines.push(" backup: " + r.backups.join(", "));
879
948
  return lines.join("\n");
@@ -965,6 +1034,8 @@ function nextSteps(items, out) {
965
1034
  out("Next: sign in once in each app. The first time an app uses OwlMem it opens the owlmem.com sign-in page —");
966
1035
  out("sign in with your email (new accounts get a free space) and click \"Allow\". An AI can't click it for you.");
967
1036
  for (const r of live) out(" • " + LOGIN[r.id]);
1037
+ if (live.some((r) => r.id === "codex" && r.codexHooks))
1038
+ out("Codex: the context-meter hooks in ~/.codex/hooks.json don't run until you trust them — open /hooks in Codex and trust the OwlMem entries.");
968
1039
  if (live.some((r) => ["cursor", "vscode", "vscode-insiders", "windsurf"].includes(r.id)))
969
1040
  out("Cursor, VS Code and Windsurf have no global rules file: add the rules from https://owlmem.com/docs#instructions to your project rules.");
970
1041
  if (live.some((r) => r.id === "claude-desktop")) out("Claude Desktop: restart it to load OwlMem. (Or add https://mcp.owlmem.com/mcp under Settings → Connectors instead.)");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "owlmem",
3
- "version": "0.9.35",
3
+ "version": "0.9.36",
4
4
  "description": "OwlMem command line: connect your AI apps to OwlMem in one command (npx -y owlmem install), an end-to-end encrypted vault for your AI agents, and a local MCP server.",
5
5
  "type": "module",
6
6
  "bin": { "owlmem": "owlmem.mjs" },