Skip to content
Agent Memory
Hook

Hooks

What mempalace runs automatically, and when. A hook is a command Claude Code fires at a fixed moment, without you asking for it.

From plugin
mempalace
60k3 skills6 commands4 hooks1 MCP

Claude Code

> /plugin marketplace add MemPalace/mempalace
> /plugin install mempalace@mempalace

Where it lives

  • hooks/mempal_hook_run.shGitHub
    Read the script
    #!/bin/bash
    # Runs `mempalace hook run "$@"`, stdin included, through the first of
    # $MEMPAL_PYTHON, `mempalace` on PATH, python3, or python that can import
    # mempalace.
    #
    # This file is shared by every hook wrapper. The Claude Code and Codex plugins
    # install from their own folders, so each carries a byte-identical copy
    # (.claude-plugin/hooks/, .codex-plugin/hooks/); tests/test_hook_resolver.py
    # keeps the copies in step. Edit this file and copy it over.
    #
    # When no interpreter can import mempalace, the hook prints the empty reply
    # and exits 0 so the harness carries on. A leading --on-missing=error makes it
    # exit 1 with the message instead, for harnesses that should show the failure.
    set -uo pipefail
    
    on_missing=empty
    if [ "${1:-}" = "--on-missing=error" ]; then
        on_missing=error
        shift
    fi
    
    # Hooks run in the user's project directory, which may be untrusted. Python puts
    # that directory first on sys.path for `-c` and `-m`, so a project holding
    # mempalace/ or mempalace.py would run instead of the installed package.
    # PYTHONSAFEPATH stops that on Python 3.11+, for this hook and the mines it
    # spawns; the probe and launch below also drop the empty entry `-c` adds, which
    # covers 3.9 and 3.10. The working directory itself stays put.
    export PYTHONSAFEPATH=1
    drop_cwd='import sys
    if sys.path and sys.path[0] == "": del sys.path[0]'
    probe="${drop_cwd}
    import mempalace"
    launch="${drop_cwd}
    import runpy
    runpy.run_module(\"mempalace\", run_name=\"__main__\", alter_sys=True)"
    
    if [ -n "${MEMPAL_PYTHON:-}" ] && "${MEMPAL_PYTHON}" -c "${probe}" </dev/null >/dev/null 2>&1; then
        exec "${MEMPAL_PYTHON}" -c "${launch}" hook run "$@"
    fi
    if command -v mempalace >/dev/null 2>&1; then
        exec mempalace hook run "$@"
    fi
    for python_bin in python3 python; do
        if command -v "${python_bin}" >/dev/null 2>&1 \
            && "${python_bin}" -c "${probe}" </dev/null >/dev/null 2>&1; then
            exec "${python_bin}" -c "${launch}" hook run "$@"
        fi
    done
    
    echo "MemPalace hook error: could not find a runnable mempalace command or module; set MEMPAL_PYTHON" >&2
    if [ "$on_missing" = error ]; then
        exit 1
    fi
    printf '{}'
    
  • hooks/mempal_precompact_hook.shGitHub
    Read the script
    #!/bin/bash
    # Legacy Claude Code PreCompact hook. `mempalace hook run` does the mining;
    # mempal_hook_run.sh picks the interpreter. The supported install is the
    # Claude Code plugin (.claude-plugin/); see hooks/README.md.
    exec bash "$(dirname "$0")/mempal_hook_run.sh" --hook precompact --harness claude-code
    
  • hooks/mempal_save_hook.shGitHub
    Read the script
    #!/bin/bash
    # Legacy Claude Code Stop hook. `mempalace hook run` does the counting,
    # mining and saving; mempal_hook_run.sh picks the interpreter. The supported
    # install is the Claude Code plugin (.claude-plugin/); see hooks/README.md.
    exec bash "$(dirname "$0")/mempal_hook_run.sh" --hook stop --harness claude-code
    
  • hooks/mempal_session_end_hook.shGitHub
    Read the script
    #!/bin/bash
    # MemPalace SessionEnd Hook — final save on clean exit.
    #
    # Claude Code documents a default SessionEnd hook timeout of 1.5s; a per-hook
    # "timeout" in settings.local.json can raise it (up to 60s), but a
    # plugin-provided timeout cannot (https://code.claude.com/docs/en/hooks). A cold
    # `mempalace` start alone can exceed 1.5s, so we background the hook and return
    # immediately; the detached child finishes the save after the session has
    # exited. All logic lives in mempalace.hooks_cli for cross-harness extensibility;
    # mempal_hook_run.sh picks the interpreter (MEMPAL_PYTHON first).
    hook_run="$(dirname "$0")/mempal_hook_run.sh"
    
    # Capture stdin (the SessionEnd JSON) before backgrounding — the parent's
    # stdin is gone once we return. Forward it to the detached worker, which runs
    # the final mine on its own time and outlives this process.
    payload="$(cat)"
    (
      printf '%s' "$payload" | bash "$hook_run" --hook session-end --harness "${MEMPALACE_HOOK_HARNESS:-claude-code}"
    ) >/dev/null 2>&1 </dev/null &
    disown 2>/dev/null || true
    
    # Return immediately so the harness never blocks on session exit.
    printf '{}'
    

Read the script before you install anything that runs on your machine. This is the one part of a plugin that acts without being asked.

@mempalace
Ships withmempalace

Local-first AI memory. Verbatim storage, pluggable backend, 96.6% R@5 raw on LongMemEval — zero API calls.

60k3 skills1 MCP
Get the whole plugin

Stars on GitHub

Momentum

stars in total
60.2k
Star history of MemPalace/mempalace on GitHub, a chart by star-history.com

More plugins that ship hooks.

mempalace

mempalace

Gives Claude Code a private memory that stays on your machine. It stores past conversations word for word, so you can ask what was decided, what happened before, or who was involved and get the original context back. It can also hand tasks to other agents.

3 skills+360k
@milla-jovovich@milla-jovovich
Install Plugin
mksglu-context-mode

mksglu-context-mode

Keeps large tool outputs, logs, and files out of Claude Code's context window by processing them in a sandbox and indexing them for focused search. Includes diagnostics and a knowledge base indexer.

9 skills+233k
@mksglu@mksglu
Install Plugin
context-mode

context-mode

Keeps large command outputs, logs and data files out of your AI assistant's context by running processing in a sandbox and returning only the relevant parts. You also get a searchable index of local files and diagnostics for checking the setup.

9 skills+233k
@mksglu@mksglu
Install Plugin
ai-memory

ai-memory

Gives coding agents a git-backed wiki for long-term project memory, with skills for retrieval, durable notes, session handoffs between agents, cross-project messaging, and memory consolidation.

7 skills+214k
@akitaonrails@akitaonrails
Install Plugin
letta-code

letta-code

A coding agent harness whose agents keep memory and identity across sessions, so they learn and improve over time. You can run it in a terminal, in the browser, or through Telegram, Slack and Discord.

26 skills8 hooks3.8k
@letta-ai@letta-ai
Install Plugin
claude-subconscious

claude-subconscious

A background agent for Claude Code that watches your sessions and reads your files, builds up memory over time, and offers guidance back during your work.

4 hooks2.9k
@letta-ai@letta-ai
Install Plugin
token-optimizer

token-optimizer

Audits a Claude Code or Codex setup for context-window waste, fixes it with the user's approval, and measures the savings. It also tracks costs, helps resume prior sessions, and offers a usage dashboard.

5 skills+22.8k
@alexgreensh@alexgreensh
Install Plugin
claude-reflect

claude-reflect

A self-learning system for Claude Code that captures your corrections during sessions and reminds you to run /reflect to save them to CLAUDE.md as permanent memory. It also discovers recurring workflow patterns.

1 skill+21.9k
@bayramannakov@bayramannakov
Install Plugin

Questions about the hooks in mempalace.

Which coding agents can use the hooks in mempalace?
The hooks in mempalace install in Claude Code and Cursor: each runs Claude Code hooks from .claude/settings.json, so copy the hooks there.