Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions docs/features/integrations/claude-code.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion integrations/claude-code-plugin/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "loom",
"displayName": "ClickHouse Loom",
"version": "1.2.68",
"version": "1.2.69",
"description": "Shared long-term memory and code intelligence for Claude Code, self-hosted on ClickHouse. MCP tools to save and recall decisions, preferences and corrections across sessions and teammates; lifecycle hooks that capture each session with no model call; a code index that answers where-is, who-calls and blast-radius questions with line-accurate units instead of whole-file reads.",
"author": {
"name": "ClickHouse"
Expand Down
61 changes: 33 additions & 28 deletions integrations/claude-code-plugin/README.md

Large diffs are not rendered by default.

58 changes: 46 additions & 12 deletions integrations/claude-code-plugin/hooks/HOOKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ receives on stdout.
| PreToolUse Read\|Grep\|Bash | loom-code-shunt | tool_name, tool_input, cwd | JSON permission decision | code.repos; one code.outline, code.symbol or tracker.duplicate_check before a deny; deny text | `LOOM_CODE_SHUNT=off` |
| PreToolUse Edit\|Write\|MultiEdit\|NotebookEdit | loom-file-context | tool_input, session_id | JSON additionalContext | memory.for_file | `LOOM_FILE_CONTEXT=off` |
| PostToolUse loom tools | loom-mentor-hint | tool_name, tool_response | JSON additionalContext | none | `LOOM_MENTOR_HINTS=off` |
| PostToolUse Edit\|Write\|MultiEdit | loom-blast-radius | tool_input, cwd | JSON additionalContext | code.impact | `LOOM_BLAST_RADIUS=off` |
| PostToolUse Edit\|Write\|MultiEdit | loom-blast-radius | tool_input, cwd, transcript head | JSON additionalContext | code.impact; code.test_gate for a file's first edit (`LOOM_TEST_GATE=off` or `block` stops that one) | `LOOM_BLAST_RADIUS=off` |
| PostToolUse Bash\|PowerShell | loom-test-gate | tool_input, cwd, transcript head | JSON additionalContext | code.test_gate for each changed file the command names, once per file | `LOOM_TEST_GATE=off` or `block` |
| PostToolUse, PostToolUseFailure, Stop, SessionEnd (timeout 10) | loom-record | the payload | none | after a tool call and at Stop, starts one detached process, which runs the event's `detached` rows; at SessionEnd, runs the event's `in loom-record` rows itself | each script's own |
| PostToolUse, PostToolUseFailure, detached | loom-capture | tool_*, session_id, cwd | none | observe.record; code.outcome; the recalled-keys stash | no token |
| PreCompact | loom-capture | session_id | none | observe.crystallize | no token |
Expand All @@ -109,7 +110,7 @@ receives on stdout.
| Stop, detached | loom-capture | session_id | none | observe.crystallize | no token |
| Stop, detached | loom-conversation | transcript, last_assistant_message | none | conversation.append via the flusher | `LOOM_CAPTURE_CONVERSATIONS=false` |
| Stop, detached | loom-usage | transcript | none | cost.record_turns | `LOOM_USAGE=off` |
| Stop | loom-test-gate | transcript, cwd | JSON block once | code.test_gate | `LOOM_TEST_GATE=off` |
| Stop | loom-test-gate | transcript, cwd | JSON block once | code.test_gate, when the transcript shows no test run since the last code change (`testedSince`); with LOOM_TEST_GATE=block, unless the named tests ran | `LOOM_TEST_GATE=off` |
| Stop | loom-validate-gate | transcript | JSON block once | namespace.validate | `LOOM_VALIDATE_GATE=off` |
| Stop | loom-namespace-health | cwd, session_id | JSON block once | none: tells the stored verdict once (namespace.audit is asked here by a wiring with no detached process, on the first Stop, and when the prompt's ask is overdue) | `LOOM_NAMESPACE_HEALTH=off` |
| Stop, detached | loom-working-memory | transcript, cwd | none | working_memory.set when the note changed | `LOOM_WORKING_MEMORY=off` |
Expand Down Expand Up @@ -251,7 +252,10 @@ detached (`lib/flush-job.mjs` holds its job shapes and delivery policy).
only (`diffHeaders`) and asks code.impact (depth 1) what the touched symbols
reach; injects one line — callers, tests reaching the change, and co-change
partner files this session has not touched. The same touched set is
reported once per session. LOOM_BLAST_RADIUS=off disables.
reported once per session. It is the one hook wired for an edit tool, so
it also carries the test gate's line for the file's first edit (see
loom-test-gate), which replaces its own count of tests where it names
the file's tests: one line per edit. LOOM_BLAST_RADIUS=off disables the radius, not that line.
- **loom-capture.** PostToolUse and PostToolUseFailure record one episodic
`observe.record` per tool call (zero-token heuristic typing, inputs and
outputs truncated to 800 characters, the session's run id attached);
Expand Down Expand Up @@ -322,15 +326,45 @@ detached (`lib/flush-job.mjs` holds its job shapes and delivery policy).
prompt. A wiring that starts the script at Stop only (Codex, a hand-wired
client) asks on the first Stop and waits for the answer. Later stops make
no call.
- **loom-test-gate.** With uncommitted changes this session made in a
loom-indexed repo (dirty paths the transcript shows an edit tool targeted
or a shell command named, written since the session's first row; a tree
left dirty before the session gates nothing), sends the change's paths, hunk ranges and function names only
(`diffHeaders`) to code.test_gate for its obligations and blocks the stop
once per distinct change with a reason naming them. A Claude Code stop
whose transcript already answers the ask to run tests is not blocked
(`runAnswered` in lib/test-gate.mjs); a Codex rollout answers nothing.
LOOM_TEST_GATE=off disables; LOOM_TEST_GATE_DEPTH sets hops (1-3).
- **loom-test-gate.** Names the tests of a changed file, and holds the
stop of a session that ran no test since its last code change. By default it names
them when the file changes: after a Bash or
PowerShell command (and, through loom-blast-radius, after an edit tool)
it takes the dirty paths the call named or targeted that were written
since the session's first row, asks code.test_gate about each one not
asked about yet this session (`adviceFiles`, `adviceLines` in
lib/test-gate.mjs; at most four per call; a comment-only change is not
asked about) and prints one line per file as additionalContext on that
tool result: the commands of the tests the index links to the file that
the checkout has, or the file's own command when it is a test. A file
with no such test gets the changed functions the index links no test
to. A file is asked about at its first change only, so a function
changed later in it is not named; one the server gave no answer for is
asked about once more. A test is named once per session, and not when
the call's own command runs it in this checkout as a whole command
(`runsHere`). The line costs no model request. A call that names no
unasked dirty file costs one git call and no request; in a checkout the
shared cache says is not indexed, no git call either.
At Stop, with uncommitted changes this session made in a loom-indexed
repo (dirty paths the transcript shows an edit tool targeted or a shell
command named, written since the session's first row; a tree left dirty
before the session gates nothing), it sends the change's paths, hunk
ranges and function names only (`diffHeaders`) to code.test_gate for its
obligations and blocks the stop once per distinct change with a reason
naming them. By default a stop passes, and asks the server nothing, when
the transcript holds a shell command that starts a test runner and
finished no earlier than the newest write among the changed code files,
documentation and prose left out by name
(`testedSince`, `lastCodeChange`): the command's text is read for a
runner's name only, not for which tests ran or how they went, so a run
that failed, was piped or ran between `git stash` and `git stash pop`
passes, in a Claude Code transcript or a Codex rollout. With no
transcript the default holds no stop.
With LOOM_TEST_GATE=block nothing is printed at edit time, and a Claude
Code stop passes only when its transcript already answers the ask to run
the named tests (`runAnswered` in lib/test-gate.mjs); a Codex rollout
answers nothing there.
LOOM_TEST_GATE=off disables both; LOOM_TEST_GATE_DEPTH sets hops (1-3).
- **loom-usage.** Reads the transcript's last MiB, dedupes assistant turns
by message id (sidechains skipped) and posts their raw usage counters to
cost.record_turns; the server prices them. Not wired for Codex, whose
Expand Down
10 changes: 10 additions & 0 deletions integrations/claude-code-plugin/hooks/hooks.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@
}
]
},
{
"matcher": "Bash|PowerShell",
"hooks": [
{
"type": "command",
"command": "node",
"args": ["${CLAUDE_PLUGIN_ROOT}/hooks/loom-test-gate.mjs", "PostToolUse"]
}
]
},
{
"matcher": "*",
"hooks": [
Expand Down
71 changes: 39 additions & 32 deletions integrations/claude-code-plugin/hooks/loom-blast-radius.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@
// the agent is still in the edit, which is when a missed sibling file is
// cheap to fix.
//
// The test gate's edit-time line rides the same context: the first edit of a
// file this session also asks `code.test_gate` for the file's tests and names
// their command (lib/test-gate.mjs), in place of the radius's own count of
// tests, or names the changed functions the index links no test to. This hook
// is the only one wired for an edit tool, so an edit gets one line.
// LOOM_TEST_GATE=block or off leaves the radius alone.
//
// Fail-open everywhere: no token, not an indexed repo, untracked file, empty
// diff, server slow → nothing is printed. One bounded request (3 s). The same
// touched-symbol set for a file is reported once per session.
// diff, server slow → nothing is printed. Each request is bounded (3 s) and
// the two go out together. The same touched-symbol set for a file is reported
// once per session.
//
// LOOM_BLAST_RADIUS=off disable
// LOOM_BLAST_RADIUS=off disable the radius; the tests line stays
// LOOM_BLAST_MAX_NAMES=5 callers named inline
// LOOM_BLAST_DEPTH=1 impact depth (1 or 2)
// LOOM_BLAST_PER_HOP=50 impacted rows requested per hop (counts stay exact)
Expand All @@ -35,15 +43,17 @@ import { relativeForLookup, targetPath } from "../lib/file-context.mjs";
import { resolveIndexedRepo } from "../lib/repo-scope.mjs";
import { readSessionState, writeSessionState } from "../lib/tmp-state.mjs";
import { blastPayload, blastSignature, decideEmit, impactBody, renderBlast } from "../lib/blast-radius.mjs";
import { adviceFiles, adviceLines, gateMode } from "../lib/test-gate.mjs";

const ENABLED = envSwitch("LOOM_BLAST_RADIUS", true);
const ADVISE = gateMode() === "advise";
const MAX_NAMES = Number(process.env.LOOM_BLAST_MAX_NAMES || 5);
const DEPTH = Math.min(2, Math.max(1, Number(process.env.LOOM_BLAST_DEPTH || 1)));
const PER_HOP = Math.min(500, Math.max(1, Number(process.env.LOOM_BLAST_PER_HOP || 50)));
const TOOLS = new Set(["Edit", "Write", "MultiEdit", "apply_patch"]);

async function main() {
if (!ENABLED) {
if (!ENABLED && !ADVISE) {
scheduleExit(0);
return;
}
Expand Down Expand Up @@ -87,37 +97,34 @@ async function main() {
// on it yet; empty = the edit was a no-op or already committed. The outline is
// sent in its place; with no `+++` path in it (a mode-only change, a binary file,
// a header dropped for a line end) the server has nothing to read.
const diff = git(["diff", ...DIFF_ARGS, "--unified=0", "HEAD", "--", rel], root, {
timeoutMs: 2000,
raw: true,
});
const diff = ENABLED
? git(["diff", ...DIFF_ARGS, "--unified=0", "HEAD", "--", rel], root, { timeoutMs: 2000, raw: true })
: "";
const outline = diffHeaders(diff, 200_000);
if (!/^\+\+\+ /m.test(outline)) {
scheduleExit(0);
return;
}
const files = ADVISE ? adviceFiles(payload, { root, cwd }).files : [];
const [impact, advice] = await Promise.all([
/^\+\+\+ /m.test(outline)
? postBounded("code.impact", impactBody(repo, outline, { depth: DEPTH, perHop: PER_HOP }), {
token,
serverUrl: resolveServerUrl(),
})
: null,
files.length ? adviceLines(payload, files, { root, repo, token }) : [],
]);

const impact = await postBounded("code.impact", impactBody(repo, outline, { depth: DEPTH, perHop: PER_HOP }), {
token,
serverUrl: resolveServerUrl(),
});
if (!impact) {
scheduleExit(0);
return;
let blast = "";
if (impact) {
const touchedPaths = git(["diff", "--name-only", "HEAD"], root, { timeoutMs: 2000 })
.split("\n")
.map((s) => s.trim())
.filter(Boolean);
const decision = decideEmit(readSessionState("blast", payload.session_id), rel, blastSignature(rel, impact));
writeSessionState("blast", payload.session_id, decision.cache);
// A tests line for this file names their command, so the radius does not count them too.
const named = advice.some((a) => a.rel === rel && a.tests);
if (decision.emit) blast = renderBlast(rel, impact, { maxNames: MAX_NAMES, touchedPaths, tests: !named });
}

const touchedPaths = git(["diff", "--name-only", "HEAD"], root, { timeoutMs: 2000 })
.split("\n")
.map((s) => s.trim())
.filter(Boolean);
const decision = decideEmit(readSessionState("blast", payload.session_id), rel, blastSignature(rel, impact));
writeSessionState("blast", payload.session_id, decision.cache);
if (!decision.emit) {
scheduleExit(0);
return;
}

const text = renderBlast(rel, impact, { maxNames: MAX_NAMES, touchedPaths });
const text = [blast, advice.map((a) => a.text).join("\n")].filter(Boolean).join(" ");
if (text) process.stdout.write(JSON.stringify(blastPayload(text)) + "\n");
scheduleExit(0);
}
Expand Down
Loading
Loading