YujunZhou/tellonce / codex
tellonce
VerifiedUse to view, explain, correct, delete or reorganize the user's saved preferences, or to record one by hand when automatic recording is off; learning and rule injection otherwise run automatically through Codex hooks.
hooks/userpromptsubmit-retrieve-inject.sh
View on GitHub#!/usr/bin/env bash
# Codex UserPromptSubmit hook: retrieve relevant memory rules + inject as
# additionalContext. Mirrors CC's memory-retrieve-inject.sh.
#
# Codex hook stdin: JSON with `prompt`, `cwd`, `session_id`, `transcript_path`.
# Output: JSON with hookSpecificOutput.additionalContext.
#
# Default backend is `progressive` (see the export below): a local one-line
# index of the saved rules, zero LLM calls — nothing leaves the machine.
# Legacy backends: PT_RETRIEVE_BACKEND=cli (local `codex exec` semantic
# retrieval) or =api (external provider via PT_RETRIEVE_API_PROVIDER /
# PT_RETRIEVE_MODEL; legacy B5_ names still work).
#
# NOTE (outer vs inner timeout): hooks.json gives this hook 40s while the
# inner _pt_timeout is 30s — the ~10s headroom covers bash+python startup so
# the platform doesn't kill the hook just before the inner subprocess
# finishes. Keep that margin if you change either value. Changing any hook
# definition in hooks.json requires a one-time re-trust in Codex (hooks are
# silently skipped until approved).
#
# Defensive: any failure -> exit 0 silently (never block codex turns).
set +e
# Portable timeout: GNU `timeout` is absent on stock macOS. Fall back to
# gtimeout (brew coreutils) or, failing that, run without a timeout.
_pt_timeout() {
_pt_secs="$1"; shift
if command -v timeout >/dev/null 2>&1; then timeout "${_pt_secs}" "$@"
elif command -v gtimeout >/dev/null 2>&1; then gtimeout "${_pt_secs}" "$@"
else "$@"; fi
}
# Recursion guard: if we're inside a nested codex exec spawned by
# retrieve_inject itself, exit immediately so we don't loop.
if [ "${B5_RETRIEVE_RECURSION_GUARD}" = "1" ]; then
exit 0
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SKILL_DIR="${SCRIPT_DIR}/.."
SHARED_LIB="${SKILL_DIR}/shared_lib"
if [[ ! -f "${SHARED_LIB}/retrieve_inject.py" ]]; then
exit 0
fi
# Default backend: progressive (local full-rule index; zero LLM, zero cold-start),
# matching CC/Copilot. progressive makes zero network egress. Opt into the
# small-model cli or an external api via PT_RETRIEVE_BACKEND=cli|api
# (PT_RETRIEVE_API_PROVIDER/PT_RETRIEVE_MODEL then apply; legacy B5_ names ok).
export B5_RETRIEVE_BACKEND="${PT_RETRIEVE_BACKEND:-${B5_RETRIEVE_BACKEND:-progressive}}"
export B5_RETRIEVE_CLI="${PT_RETRIEVE_CLI:-${B5_RETRIEVE_CLI:-codex}}"
# Capture stdin once so we can route to PYTHONPATH-augmented subprocess.
PT_STDIN="$(cat)"
if [[ -z "${PT_STDIN}" ]]; then
exit 0
fi
# Pass the pwd to path_config via env (codex hooks' cwd is the project, but
# subprocess sometimes resolves it differently).
CODEX_CWD="$(echo "${PT_STDIN}" | PYTHONIOENCODING=utf-8 python3 -c '
import json, sys
try:
d = json.load(sys.stdin)
print(d.get("cwd") or "")
except Exception:
print("")
' 2>/dev/null)"
if [[ -n "${CODEX_CWD}" && -d "${CODEX_CWD}" ]]; then
export B5_PROJECT_ROOT="${CODEX_CWD}"
fi
# Run retrieve_inject. PYTHONIOENCODING=utf-8 prevents stdout from crashing when LANG is not utf-8.
printf '%s' "${PT_STDIN}" | PYTHONIOENCODING=utf-8 PYTHONPATH="${SHARED_LIB}" \
_pt_timeout 30 python3 "${SHARED_LIB}/retrieve_inject.py" 2>/dev/null
exit 0
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |